       INCLUDE "EXEC/TYPES.I"
      INCLUDE "DEVICES/INPUTEVENT.I"
      INCLUDE "DEVICES/KEYBOARD.I"
      INCLUDE "DEVICES/TIMER.I"
      INCLUDE "HARDWARE/CIA.I"


 XREF  AbsExecBase        ;library routines
 XREF  LVOOpenDevice
 XREF  LVOCloseDevice
 XREF  LVOAllocSignal
 XREF  LVOFindTask
 XREF  LVOFreeSignal
 XREF  LVODoIO
 XREF  LVOSendIO
 XREF  LVOWaitPort
 XREF  LVOGetMsg
 XREF  LVOAddPort
 XREF  LVORemPort
 XREF  LVOReplyMsg


 XDEF   _key                ;used by score.a
 XDEF   _addscore
 
 XREF   _writedigit         ;used from score.a
 XREF   _energy
 XREF   _xpos
 XREF   _ypos
 

            CSECT text,code

_key:                       ;reads RAWKEY CODE
  clr.l  d0
  clr.l  d2
  move.l #$BFE001,a0        ;CIA address
  move.b ciasdr(a0),d0      ;reads key register
  move.b d2,ciasdr(a0)      ;clears register
  eori.b #$ff,d0            ;inverts answer
  move.b d0,d1          
  ror.b  #1,d0              ;rotates 1 to right
  and.b  #1,d1
  tst.b  d1                 ;tests for key up or down
  beq    keydown
  ori.b  #128,d0
keydown:
  ext.w  d0
  move.w d0,numberpressed
  rts 


_addscore:                  ;process number
 move.w  numberpressed,d0
 andi.w   #127,d0
 cmp.w   numberpressed,d0   ;discard key releases
 bne     noscore
 cmp.w   #16,d0             ;letter Q for quit
 beq     ended
 cmp.w   #11,d0             ;other letters
 bge     noscore
 cmp.w   #10,d0             ;number zero
 bne     nonzero
 clr.w   d0
nonzero:  
 add.w   d0,score           ;add to score
 add.b   #48,d0             ;display number
 move.b  d0,dnumber
 jsr     dispnumber
 cmp.w   #9999,score        ;scoreboard overflow?
 bmi     notbig   
 clr.w   score
notbig:
 jsr     dispscore          ;display score
noscore:
 rts

ended:                      ;quit option
 move.l #217,_energy
 rts

dispscore:                  
  clr.l  d0
  move.w score,d0           ;input score in hex
  lea    dscore,a0          ;input ASCII string address
  jsr    bindec             ;put display version into chosen string
  move.l #48,_ypos          ;x and y start of score display
  move.l #220,_xpos
  lea    dscore,a5          ;string address
  jsr    _writedigit        ;display the string
  rts

dispnumber:                 ;display single digit number
  move.l #12,_ypos
  move.l #291,_xpos
  lea    dnumber,a5
  jsr    _writedigit
  rts

bindec:                ;4 digit binary number to decimal ASCII string
 movem.l d0-d1/a0,-(sp)
 adda.l  #4,a0         ;last digits place
 move.w  #3,d1         ;count
bdloop:
 ext.l   d0
 and.l   #$ffff,d0
 divu    #10,d0         ;divide by 10, answer in d0
 swap    d0             ;swap answer and remainder
 move.b  d0,-(a0)       ;put remainder in string
 add.b   #48,(a0)       ;calculate ASCII value of remainder 
 swap    d0             ;put answer ready for next division
 dbra    d1,bdloop
 movem.l (sp)+,d0-d1/a0
 rts

 CSECT   DATA,data
score:
 dc.w    0
dscore:
 dc.b    '0000',0
 ds.w    0
dnumber:
 dc.b    '0',0
 ds.w    0
numberpressed:
 dc.w    0
 END

