Hi,
I've ported the keyboard relaying program that I wrote to assembly
language, and I've found a problem: pressing ESCAPE breaks into SAM
BASIC. Could someone tell me how to stop it from doing that, please?
Also, this is my first real assembly language program, so if anyone can
give feedback, or spot any bugs, I'd be grateful!
Other than that, it seems to work much, much faster than the BASIC
version -- it's actually quite usable. Here's the code:
LD A,#06 ; switch to stream 6
CALL jsetstrm
LD BC,8 ; clear the lastscan buffer
LD HL,lastscan
LD D,H
LD E,L
INC DE
LDIR
keybloop: CALL scan ; contiuously check the keyboard
CALL test
JR keybloop
scan: LD HL,scanbuf ; output buffer
LD B,%11111110 ; first scan line
scanloop: LD C,hikey ; high-order 3 bits of scan line
IN A,(C)
AND %11100000
LD (HL),A
LD C,lokey ; low-order 5 bits of scan line
IN A,(C)
AND %00011111
OR (HL)
LD (HL),A ; save the scan line
INC HL ; next scan line
SCF
RL B
JR C,scanloop
IN A,(C) ; last scan line (RDMSEL)
AND %00011111 ; low-order 5 bits only
LD (HL),A
RET
test: LD HL,scanbuf ; test each scan line
LD DE,lastscan
LD B,9 ; 9 scan lines
testloop: LD A,(DE)
LD C,(HL) ; B is always 0 - no need to save it
CP C
JP Z,testnext ; skip this if the scan line has not changed
LD A,C
LD (DE),A ; update this scan line in the lastscan buffer
LD A,9 ; print the scan line number that has changed
SUB B
CALL print
LD A,#2C ; print a comma
RST #10
LD A,(DE) ; print the value of the scan line
CALL print
LD A,#0d ; print a \r
RST #10
testnext: INC HL ; test the next scan line
INC DE
DJNZ testloop
RET
print: PUSH AF ; print the value held in A
PUSH BC
PUSH DE
PUSH HL
LD D,A
LD C,0
LD E,0
LD A,0
CALL jstkstore
CALL jstrs
CALL #0013
POP HL
POP DE
POP BC
POP AF
RET
jsetstrm: EQU #0112 ; ROM routines
jstkstore: EQU #0127
jstrs: EQU #017e
hikey: EQU 249 ; I/O ports
lokey: EQU 254
scanbuf: DEFS 9 ; buffers
lastscan: DEFS 9
And here's the BASIC loader:
10 LOAD "keyb.cde" CODE
20 PRINT "SAM Coupe Keyboard Relay"
30 CLOSE #6
40 OPEN #6;"b"
50 CALL 32768
--
Stuart Brady