Skip to content

Instantly share code, notes, and snippets.

@raxoft
Last active March 4, 2020 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raxoft/f70995bb53f6880bd2a7 to your computer and use it in GitHub Desktop.
Save raxoft/f70995bb53f6880bd2a7 to your computer and use it in GitHub Desktop.
Kempston mouse driver for ZX Spectrum.
; Kempston mouse driver with master/slave support.
; Written by Patrik Rak in 2013, and revised in 2016.
; Based on a driver originally written for testing ZXDS, see the original WoS post
; in the thread at http://www.worldofspectrum.org/forums/discussion/43026/mouse-systems-for-zx-spectrum
updatemouse
ld hl,ports
ld de,mouseinput
ld c,0xDF
.loop ld b,(hl)
inc hl
in a,(c)
ld (de),a
inc de
ld a,(mouseinput+6)%256
cp e
jr nz,.loop
ex de,hl
call .pair
.pair ld c,255
ld a,(de)
call .update
ld c,191
ld a,(de)
cpl
.update inc de
ld b,a
sub (hl)
ld (hl),b
inc hl
jp m,.dec
add a,(hl)
jr c,.clamp
cp c
jr c,.ok
.clamp ld a,c
jr .ok
.dec add a,(hl)
jr c,.ok
xor a
.ok ld (hl),a
inc hl
ret
ports db 0xFB ; mouse 1 X port
db 0xFF ; mouse 1 Y port
db 0x0B ; mouse 2 X port
db 0x0F ; mouse 2 Y port
db 0xFA ; mouse 1 button port
db 0x0A ; mouse 2 button port
mouseinput
mouse1x db 0
mouse1y db 0
mouse2x db 0
mouse2y db 0
mouse1b db 0 ; mouse 1 buttons
mouse2b db 0 ; mouse 2 buttons
old1x db 0
pos1x db 0 ; mouse 1 X
old1y db 0
pos1y db 0 ; mouse 1 Y
old2x db 0
pos2x db 0 ; mouse 2 X
old2y db 0
pos2y db 0 ; mouse 2 Y
; EOF ;
@raxoft
Copy link
Author

raxoft commented Apr 19, 2017

OK, updated to use the ports Velesoft suggested, so it works with interfaces which use another line(s) from A12-15 range than just A15 for master/slave selection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment