************************************************************
* Coyota - Boisy's Pick-Up Truck CoCo App
*
* This application was designed to run in my Toyota '92
* Pick-up truck using a CoCo 3 with Speech/Sound Pak.
*

         nam     GoCo
         ttl     Boisy's Pick-Up Truck CoCo App

Level    equ     2

         ifp1
         use     os9defs.l2v3
         use     scfdefs
         endc

rev      set     $00
edition  set     $01

* This timeout value is the number of ticks that this application
* will sleep for at startup.  This is necessary to give the audio
* amplifier time to warm up; we don't want audio being played 
* before we can hear it.
timeout  set     60

         mod     Size,Name,Prgrm+Objct,Reent+rev,Start,Fin

Name     fcs     "AutoEx"
         fcb     edition

SSCPath  rmb     1
HiResEnt rmb     2
TimePckt rmb     6
HiJoyNum rmb     2
HiButton rmb     2
HiJoyX   rmb     2
HiJoyY   rmb     2
ButtonL  rmb     1
JoyXL    rmb     2
JoyYL    rmb     2
InBuff   rmb     128
Stack    rmb     200
Fin      equ     .

SSCDev   fcs     "/ssp"
HiResMod fcc     "hires"
CR       fcb     C$CR
Bell     fcb     C$BELL

CopyrMsg fcb     C$CR,C$LF
         fcc     "Coyota V"
         fcb     48+edition
         fcc     "."
         fcb     48+rev
         fcb     C$CR,C$LF
         fcc     "(C) 1998 Planet 9 Systems"
         fcb     C$CR,C$LF,C$LF
         fcc     "1992 TOYOTA 1/2 Ton Pick-Up"
         fcb     C$CR,C$LF
         fcc     "VIN: 4TARN81A5NZ047604"
         fcb     C$CR,C$LF
CopyrML  equ     *-CopyrMsg

* Version information -- spoken
VerMsg   fcc     "Coyowta Version "
         fcb     48+edition
         fcc     " point "
         fcb     48+rev
         fcb     C$CR
         fcc     "O S9 Levul 2 Version 3"
         fcb     C$CR
VerMsgL  equ     *-VerMsg

* VEHICLE information -- spoken
* "1992 Toyota half-ton pick-up"
IDMsg    fcc     "nine teen nine t2 Toyowta haf ton pick up"
         fcb     C$CR
         fcc     "VIN number 4T A R N81A5N Z047604"
         fcb     C$CR
IDMsgL   equ     *-IDMsg

       
Months   fdb     Jan-Months
         fdb     Feb-Months
         fdb     Mar-Months
         fdb     Apr-Months
         fdb     May-Months
         fdb     Jun-Months
         fdb     Jul-Months
         fdb     Aug-Months
         fdb     Sep-Months
         fdb     Oct-Months
         fdb     Nov-Months
         fdb     Dec-Months

* Months -- spoken
Jan      fcc     "January"
         fcb     C$CR
Feb      fcc     "Febuary"
         fcb     C$CR
Mar      fcc     "March"
         fcb     C$CR
Apr      fcc     "April"
         fcb     C$CR
May      fcc     "May"
         fcb     C$CR
Jun      fcc     "June"
         fcb     C$CR
Jul      fcc     "July"
         fcb     C$CR
Aug      fcc     "August"
         fcb     C$CR
Sep      fcc     "September"
         fcb     C$CR
Oct      fcc     "Octowber"
         fcb     C$CR
Nov      fcc     "November"
         fcb     C$CR
Dec      fcc     "December"
         fcb     C$CR

* Introduction message when powered on
StartMsg fcc     "System On-line"
         fcb     C$CR
StartML  equ     *-StartMsg


Start    equ     *
* Write Copyright Message
         lda     #1
         sta     SSCPath,u              assume stdout for now
         leax    CopyrMsg,pcr
         ldy     #CopyrML
         os9     I$Write

         clra
         leax    HiResMod,pcr
         pshs    u
         os9     F$Link
         puls    u
         lbcs    error
         sty     HiResEnt,u


* Open a path to the Speech/Sound Pak
         lda     #WRITE.		write mode
         leax    SSCDev,pcr		point to device name
         os9     I$Open			open it
         bcs     Loop                   ignore error, data will go to stdout
         sta     SSCPath,u		..else save path

* Wait for timeout
         ldx     #timeout
         os9     F$Sleep

* Send out welcome message
Welcome  leax    StartMsg,pcr		point to start msg
         ldy     #StartML		load max count
         os9     I$WritLn		write it
         lbcs    error			branch if error


* Command LOOP
*
* Supported key commands:
*   X=00, Y=00  - Say Vehicle Motto
*   X=00, Y=63  - Say Date
*   X=63, Y=00  - Say Vehicle ID information
*   X=63, Y=63  - Say Software Version information
*   Q  - Quit application

Loop
* Check for char waiting on keyboard
*         clra
*         ldb     #SS.Ready
*         os9     I$GetStt
*         bcc     ReadChar
* No char at this point, read joysticks
* Read right joystick port (hi-res mouse)
         ldd     #0           right joystick port
         lbsr    GetHiRes
* Read left joystick port (joystick)
         lda     #1
         ldb     #SS.Joy
         ldx     #$0001
         os9     I$GetStt
         stx     JoyXL,u
         sty     JoyYL,u
         sta     ButtonL,u
         beq     Loop
         bsr     ChkJoyX
         bra     Loop

* Act upon button press
ChkJoyX  lda     JoyXL+1,u
         bne     ChkJoyY
         ldb     JoyYL+1,u
         beq     SayILove    X=00, Y=00
         cmpb    #63
         lbeq    SayDate     X=00, Y=63
* here, the joystick has no meaningful value
         rts 

* here, X != 0
ChkJoyY  cmpa    #63
         blt     Ret         if joystick X != 63, no meaningful value
         ldb     JoyYL+1,u
         beq     SayID       X=63, Y=00
         cmpb    #63
         beq     SayVrsn     X=00, Y=63
* here, the joystick has no meaningful value
Ret      rts 

* GetHiRes subroutine
* D  = Joystick port number (0 = right, 1 = left)
GetHiRes leas    -18,s
         std     HiJoyNum,u
         ldd     #4           set parameter count
         std     ,s
         ldd     #2           set param sizes (all 2)
         std     4,s
         std     8,s
         std     12,s
         std     16,s
         leax    HiJoyNum,u
         stx     2,s
         leax    HiJoyX,u
         stx     6,s
         leax    HiJoyY,u
         stx     10,s
         leax    HiButton,u
         stx     14,s
         jsr     [HiResEnt,u]
         leas    18,s
         rts

ILove    fcc     "I love what u do 4 me Toyowta"
         fcb     C$CR
ILoveL   equ     *-ILove

* Speak "I love what you do for me"
SayILove lda     SSCPath,u
         leax    ILove,pcr
         ldy     #ILoveL
         os9     I$Write
         rts

* Speak version information
SayVrsn  lda     SSCPath,u
         leax    VerMsg,pcr
         ldy     #VerMsgL
         os9     I$Write
         rts

* Speak vechicle's ID information
SayID    lda     SSCPath,u
         leax    IDMsg,pcr
         ldy     #IDMsgL
         os9     I$Write
         rts

* Read a line of input from standard in and write it out
SpeakTxt clra
         leax    InBuff,u
         ldy     #80
         os9     I$ReadLn
         lda     SSCPath,u
         os9     I$WritLn
         rts

SayDate  leax    TimePckt,u
         os9     F$Time
         bcs     SayDateX
* Say month
*         lda     1,x
*         deca
*         asla
*         leax    Months,pcr
*         ldd     a,x
*         leax    d,x
*         lda     SSCPath,u
*         ldy     #80
*         os9     I$WritLn

* Say day
*         leax    InBuff,u
*         leay    TimePckt,u
*         ldb     2,y
*         bsr     Str2Num
*         tfr     y,d
*         leax    d,x
*         lda     #C$CR
*         sta     ,x
*         leay    1,y
*         leax    InBuff,u
*         lda     SSCPath,u
*         os9     I$WritLn

* Say hour
         leax    InBuff,u
         leay    TimePckt,u
         ldb     3,y
         bsr     Str2Num
         tfr     y,d
         leax    d,x
         lda     #C$CR
         sta     ,x
         leay    1,y
         leax    InBuff,u
         lda     SSCPath,u
         os9     I$WritLn

* Say minute
         leax    InBuff,u
         leay    TimePckt,u
         ldb     4,y
         bsr     Str2Num
         tfr     y,d
         leax    d,x
         lda     #C$CR
         sta     ,x
         leay    1,y
         leax    InBuff,u
         lda     SSCPath,u
         os9     I$WritLn

SayDateX rts

exit     clrb				no error, clear B
error    os9     F$Exit			exit


************************************************************
* Str2Num:  Converts a one byte representation to its string
*           counterpart in the range of 0-255.
*
*  Entry:  X  - Address to store text digits
*          B  - Byte to convert
*
*  Exit:   Y  - Length of numerical string
*
Str2Num  pshs  a,b,x
         ldy   #$0000
         lda   #$2f
         inca
p1       subb  #$64
         bcc   p1
         sta   ,x+
         leay  1,y
         cmpa  #$30
         bne   p2
         leax  -1,x
p2       lda   #$3a
p3       deca
         addb  #$0a
         bcc   p3
         sta   ,x+
         addb  #$30
         stb   ,x+
         leay  1,y
         puls  pc,a,b,x

         emod
size     equ     *
         end

