The joystick 1 reading routine can be mixed with the mouse routine as it is the same procedure done four times. The mouse has to be read in time, although the Neos chip inside MSX mice admits some delay stretching.
The procedure is:
-Put RTS low
-Delay
-Read a nibble formed with the four direction lines, higher four bits of X displacement since last lecture
-Put RTS high
-Delay
-Read a nibble, lower four bits of X displacement since last lecture
-Put RTS low
-Delay
-Read a nibble, higher four bits of Y displacement since last lecture
-Put RTS high
-Delay
-Read a nibble, lower four bits of Y displacement since last lecture
-Read mouse buttons, row 0, B6h port.
Observe that the RTS signal is what triggers the mouse reading. It must begin and end high.
This is the simple delay routine:
WAIT: nop
nop
nop
dec b
jr nz, WAIT
ret
On the original Boxoft routine the delays where 8, 5, 5, 5 cycles between lectures, but experimenting with the values we have realized that 3, 2, 2, 2 cycles are safe enough for a Neos or a MSX mouse. EnterMice admits even lower delays, working the same on a 4Mhz or a 10Mhz Z80.
This as a minimum, but I've not tested the routine for maximum values. Then, super big delays can be used to do other tasks, like reading the keyboard matrix, joystick 2 or play sound.
The beginning of lectures have to be synchronised with video frames. If an error occurs, the program only must wait till the next frame and the mouse(or EnterMice) will be ready again.