Hardware — connecting a mouse to the ZX Spectrum with the Kempston Mouse Interface

Modern #07

(c) Vladimir Larkov, 1996. St. Petersburg.

                 ┌──────────────────────────┐
                 ├─── ZX-SPECTRUM AND MOUSE ┤
                 └──────────────────────────┘

The subject of connecting a mouse to the Spectrum has already been discussed more than once in the pages of the computer press. These were promotional articles from St. Petersburg manufacturers and sellers of mouse interfaces. I will not argue with any of them; I simply want to suggest that those who know how to hold a soldering iron can build this device themselves.

To begin with, we need to answer the question: why the Kempston Mouse Interface in particular? Some enthusiasts manage to solder a mouse to the Kempston joystick port, while others use AY-mouse. So what makes the Kempston Mouse Interface better?

First of all, it should be noted that any attempt to connect a mouse to a joystick port is an insult to the mouse. A mouse has one wonderful quality: in addition to the direction of movement, it also responds to the speed of that movement. As a result, the cursor moves in proportion to the movement of your hand: when you move the mouse slowly, the cursor moves slowly too; when you move the mouse quickly, the cursor moves quickly. This makes working with a mouse incomparably more convenient than with a joystick. A joystick, on the other hand, cannot respond to movement speed; it only registers direction.

Therefore, when a mouse is connected to a joystick port, the cursor will move at a constant speed, regardless of how fast you move the mouse. You may not have enough desk space to move the cursor from one edge of the screen to the other. The AY-mouse option seems to be free of this drawback. However, only a small number of programs have been adapted for AY-mouse.

So why are we campaigning so hard for the Kempston Mouse Interface? The answer is banal: it is a matter of standardization. This device was invented quite some time ago; it is just as much a standard as the Kempston Joystick or Sinclair Joystick, and it is supported by commercial programs such as Art Studio or Artist II. So why reinvent the wheel and create a whole bunch of our own standards, when it is simpler and more logical to use what has already been invented—especially since software support for this option already exists?

The Kempston Mouse Interface became most widespread in St. Petersburg, which is understandable: its Russian equivalent was made there. Alongside the already mentioned Art Studio and Artist II, commercial support for the Kempston Mouse can be found in Victory Road, Battle Command, and Carrier Command.

Programs were adapted to the Kempston Mouse Interface very quickly: F Commander v4.01, Instrument v2.01, DCU 2.32, etc.

New programs began to be written with the Kempston Mouse Interface in mind, and mouse support can also be found in FUT v2.01, ZXWORD v2.5m, ZxZip/ZxUnzip v1.02, Instrument v3.01, FPM v3.5, and others.

Games were also adapted for the Kempston Mouse Interface, for example: Hero Quest, Dragon Spirit, Space Crusade, A New Whole Ball Game, The A Team 1 & 2, Operation Wolf, Operation Thunderbolt, World Cup Challenge, Gunsmoke, R-Type, Gemini Wing, 1943 The Battle Begin, Zombi, Armagedon Man, etc...

Article: by Vladimir Larkov, 1996, St. Petersburg
Fragment 2/6.

The new electronic magazine ZX-Format has supported the Kempston Mouse Interface from its very first issue. Taking the wishes of his acquaintances to heart, Slava Mednonogov added mouse support to his new game UFO II, and after trying it out with a mouse, was very pleased.

I think it’s clear where I’m going with this. The number of mouse programs is steadily increasing; practically all new St. Petersburg authors’ programs support the mouse. Commercial programs where mouse control would make sense are also being adapted for the Kempston Mouse Interface. What other arguments do you need in favor of this device? It seems there are enough already. Just try spending some time in the mouse version of Art Studio, and after that you’ll hardly be dragged back. You’ll be tearing your hair out watching the arrow crawl along when controlled by a joystick or keys, while with a mouse it simply flies. And you’ll be amazed when you realize how much time you used to waste just moving around menus.

So, practically all St. Petersburg programmers support the Kempston Mouse Interface standard. That’s why we’re urging all users to build this device for themselves, and all programmers to support this particular interface, so users won’t have to heat up their soldering irons every time something like a Sinclair Joystick Mouse or, say, a ZX Lprint III Mouse appears...

That concludes the introduction; let’s move on to the circuit itself. The circuit was designed by Mikhail Kondratyev, after studying the drivers in the Art Studio and Artist programs. The original version contained 11 chips, because the author used the most readily available chips he had on hand. Later, the number of chips was reduced to eight. For a while, the circuit was a commercial product, and the rights to it were transferred to Sergey Zонов. Last year, Sergey Zонов allowed it to be distributed, after which the Kempston Mouse Interface circuit was made public on the FidoNet network.

The most commonly used mouse is an ordinary gray passive mouse from Poisk. Despite its low price, it works quite well. Those who want to can also use branded passive mice.

If there are no passive mice anywhere in sight, active ones can be used too, although you’ll have to tinker with them a little (see the notes on the circuit).

Kempston Mouse Interface circuit:

Article: by Vladimir Larkov, 1996, St. Petersburg
Fragment 3/6.

                 DD1              DD5
             5┌──┬──┬─┐ 6     2┌──┬──┬─┐18 D0║
         ┌────oCa│CT│0├────────┤D1│|>│1├─────╢
     Xb ─┴─ 15├──┤  │ │11     4│  │  │ │16 D1║
     ──┬──────/C │  │1├────────┤D2│  │2├─────╢
     Xa│    10│  │  │ │14     6│  │  │ │14 D2║
     ───┬─────┤U/D  │2├────────┤D3│  │3├─────╢
       ││    9├──┤  │ │ 2     8│  │  │ │12 D3║
       ││ ┌───┤R │  │3├────────┤D4│  │4├─────╢
       ││ │  1│  │  │ │      11│  │  │ │ 9 D4║
       ││ ├───┤S │  │ │  ┌─────┤D5│  │5├─────╢
       ││─┴─ 4│  │  │ │  │   13│  │  │ │ 7 D5║
       ││   ──┤0 │  │ │  │┌────┤D6│  │6├─────╢
       ││   12│  │  │ │  ││  15│  │  │ │ 5 D6║
       ││   ──┤1 │  │ │  ││┌───┤D7│  │7├─────╢
       ││   13│  │  │ │  │││ 17│  │  │ │ 3 D7║
       ││   ──┤2 │  │ │  │││┌──┤D8│  │8├─────╢
       ││    3│  │  ├─┤ 7││││19├──┤  │ │     ║
       ││   ──┤3 │  │Co─┐││││┌─oE2│  │ │     ║
       ││     └──┴──┴─┘ ││││││1│  │  │ │     ║
       ││  ┌────────────┘││││├─oE1│  │ │     ║
       ││  │     DD2     │││││ └──┴──┴─┘     ║
       ││  │ 5┌──┬──┬─┐ 6│││││ /XCS          ║
       ││  └──oCa│CT│0├──┘│││└────           ║
       ││   15├──┤  │ │11 │││                ║
       └──────/C │  │1├───┘││                ║
        │   10│  │  │ │14  ││                ║
        └─────┤U/D  │2├────┘│                ║
             9├──┤  │ │ 2   │                ║
          ┌───┤R │  │3├─────┘                ║
          │  1│  │  │ │                      ║
          ├───┤S │  │ │                      ║
         ─┴─ 4│  │  │ │                      ║
            ──┤0 │  │ │                      ║
            12│  │  │ │                      ║
            ──┤1 │  │ │                      ║
            13│  │  │ │                      ║
            ──┤2 │  │ │                      ║
             3│  │  ├─┤ 7                    ║
            ──┤3 │  │Co──                    ║
              └──┴──┴─┘                      ║
                 DD3              DD6        ║
             5┌──┬──┬─┐ 6     2┌──┬──┬─┐18 D0║
         ┌────oCa│CT│0├────────┤D1│|>│1├─────╢
     Ya ─┴─ 15├──┤  │ │11     4│  │  │ │16 D1║
     ──┬──────/C │  │1├────────┤D2│  │2├─────╢
     Yb│    10│  │  │ │14     6│  │  │ │14 D2║
     ───┬─────┤U/D  │2├────────┤D3│  │3├─────╢
       ││    9├──┤  │ │ 2     8│  │  │ │12 D3║
       ││ ┌───┤R │  │3├────────┤D4│  │4├─────╢
       ││ │  1│  │  │ │      11│  │  │ │ 9 D4║
       ││ ├───┤S │  │ │  ┌─────┤D5│  │5├─────╢
       ││─┴─ 4│  │  │ │  │   13│  │  │ │ 7 D5║
       ││   ──┤0 │  │ │  │┌────┤D6│  │6├─────╢
       ││   12│  │  │ │  ││  15│  │  │ │ 5 D6║
       ││   ──┤1 │  │ │  ││┌───┤D7│  │7├─────╢
       ││   13│  │  │ │  │││ 17│  │  │ │ 3 D7║
       ││   ──┤2 │  │ │  │││┌──┤D8│  │8├─────╢
       ││    3│  │  ├─┤ 7││││19├──┤  │ │     ║
       ││   ──┤3 │  │Co─┐││││┌─oE2│  │ │     ║
       ││     └──┴──┴─┘ ││││││1│  │  │ │     ║
       ││ ┌─────────────┘││││├─oE1│  │ │     ║
       ││ │      DD4     │││││ └──┴──┴─┘     ║
       ││ │  5┌──┬──┬─┐ 6│││││  /YCS         ║
       ││ └───oCa│CT│0├──┘│││└──────         ║
       ││   15├──┤  │ │11 │││                ║
       └──────/C │  │1├───┘││ ╔══════════════╝
        │   10│  │  │ │14  ││ ║
        └─────┤U/D  │2├────┘│ ║
             9├──┤  │ │ 2   │ ║
          ┌───┤R │  │3├─────┘ ║
          │  1│  │  │ │       ║
          ├───┤S │  │ │       ║
         ─┴─ 4│  │  │ │       ║
            ──┤0 │  │ │       ║
            12│  │  │ │       ║
            ──┤1 │  │ │       ║
            13│  │  │ │       ║
            ──┤2 │  │ │       ║

Article: by Vladimir Lar'kov, 1996, St. Petersburg
Fragment 4/6.

            ──┤2 │  │ │       ║
             3│  │  ├─┤ 7     ║
            ──┤3 │  │Co──     ║
              └──┴──┴─┘       ║
                         ╔════╝
               DD7.2     ║        DD8
     RIGHB   5┌─┬──┐6  D0║A10 1┌─┬──┬─┐15
     ─────────┤D│|>├─────╫─────┤1│DC│0o──
             4│ │  │     ║A8  2│ │  │ │14
           ┌──oE│  │     ╟─────┤2│  │1o──
           │  └─┴──┘     ║A7  3│ │  │ │13
           │   DD7.3     ╟─────┤4│  │2o──
     LEFTB │ 9┌─┬──┐8  D1║/RD 5├─┤  │ │12
     ─────────┤D│|>├─────╫─────o&│  │3o──
           │10│ │  │     ║A0  6│ │  │ │11 /BCS
           ├──oE│  │     ╟─────┤ │  │4o───────
           │  └─┴──┘     ║    4│ │  │ │10
           │   DD7.4     ║  ┌──oS│  │5o──
     MIDLB │12┌─┬──┐11 D2║  │  │ │  │ │ 9 /XCS
     ─────────┤D│|>├─────╢  │  │ │  │6o───────
           │13│ │  │     ║  │  │ │  │ │ 7 /YCS
           ├──oE│  │     ║  │  │ │  │7o───────
           │  └─┴──┘     ║  │  └─┴──┴─┘
           │             ║  └────────────┐
           │   /BCS      ║        DD7.1  │
           └────────     ║/IORQ 2┌─┬──┐3 │
                         ╟───────┤D│|>├──┘
     DD1..DD4 - КИE11 ║A5    1│ │  │
     DD5, DD6 - КАП5  ╟───────oE│  │
     DD7      - КЛП8  ║       └─┴──┘
     DD8      - КИД7

Notes:

  • The thick line indicates the Spectrum bus;
  • the following signals go to the mouse: Xa, Xb, Ya, Yb, RIGHTB, MIDLB, LEFTB, +5V, GND;
  • /BCS, /XCS, /YCS are internal circuit signals, connected to the corresponding signals with the same names in the paired circuit;
  • the circuit is shown for a passive-type mouse (the mouse for the Poisk MMP-9B). For other mice, the Xa, Xb and Ya, Yb signals must be brought out; these are taken from the optocouplers after the transistor–Schmitt trigger chain;
  • the computer must provide port 0DFh blocking;
  • the decoding circuit (DD7.1, DD8) can be replaced with a PLM chip of the PT4 (PT11) type. The remaining unused output can be used for port blocking.

                        _  _  _

So, let us now explain how a programmer should work with the Kempston Mouse Interface. Nothing difficult to understand is involved, since the interface is quite simple and straightforward. There are three ports; their addresses are #FADF, #FBDF and #FFDF. Each is 8 bits wide, i.e. one byte.

#FADF — button port. The three least significant bits are used:

  • bit 0 — right button state;
  • bit 1 — left button state;
  • bit 2 — middle button state.

When no buttons are pressed, all bits are 1. When a button is pressed, the corresponding bit is set to 0.

By old tradition, the left button is used as Select (Fire), the right as Cancel. The middle button is usually used for additional functions.

#FBDF — X-coordinate port;
#FFDF — Y-coordinate port.

As the mouse is moved, the values in the ports change, and do so cyclically: when they reach #FF, further incrementing makes them wrap around to zero and continue upwards, again reaching #FF. Movement in the other direction works similarly, except that the values change in reverse.

This little diagram should help make it clear:

                y^.
                 │.
                 │2
                 │1
                 │..
                 ┼─────────> x

Thus, the port values must be read, and the direction of mouse movement is determined from the difference between the current and previous values.

Article: by Vladimir Larkov, 1996, St. Petersburg
Fragment 5/6.

If very precise manipulations are required, the driver is written with this in mind: the pointer moves by one point when the value read from the port changes by 2, 3, 5, or more units.

Recently, a smarter driver has become fashionable. It waits for a button to be pressed at startup, and whichever mouse button is pressed first becomes the left button (as far as the driver is concerned). This applies only to the two outer buttons; the middle button is still the middle one, even in Africa. This is useful for people accustomed to holding the mouse in their left hand, or in case someone has often stepped on your mouse and one of its buttons works worse than the others. A driver like this is considered good form. And, if you noticed, this is exactly how the ZX-Format driver works.

You can test a freshly assembled interface straight from BASIC by using PRINT IN on the ports: 79 - X, 03 - Y. When you move the mouse, the values should change cyclically. When reading from port 23 - Fire, the three lowest bits should toggle when the buttons are pressed.

Finally, an example of a Kempston Mouse driver, as used in ZX-Format:

;MOUSE DRIVER WITH FIRE BUTTON AUTOCONFIG
;(C) Andrey Rachkin'95

        JR       MDRV

DIRECTZ DEFB  0 ;FIRE
        DEFB  0 ;UP
        DEFB  0 ;DOWN
        DEFB  0 ;RIGHT
        DEFB  0 ;LEFT
        DEFB  0 ;CANCEL

MCOORD  DEFW  0 ;LAST CURSOR COORDS
                ;IN PIXELZ
MPORTS  DEFW  0 ;LAST READED MAUSY COORDS

NONDEF  AND   3 ;HERE COMEZ BUTTONZ
                ;CONTROL IF FIRE BUTON
                ;NOT DEFINED
        JR    Z,MDRV4 ;IF NONE BUTTON
                      ;PUSHED
        CP    1
        JR    Z,NONDEF_
        XOR   A
        LD    (MDRV3+2),A
        LD    A,5
        LD    (MDRV2+2),A
NONDEF_ LD    HL,0
        LD    (MDRV1),HL
        POP   IX

; ***** MAIN PROC OF MOUSEDRIVER *****
MDRV    PUSH  IX
        LD    HL,DIRECTZ
        PUSH  HL
        POP   IX
        XOR   A
        LD    (HL),A ;CLEARING
        INC   HL     ;OF
        LD    (HL),A ;DIRECTZ
        INC   HL     ;BUFER
        LD    (HL),A
        INC   HL
        LD    (HL),A
        INC   HL
        LD    (HL),A
        INC   HL
        LD    (HL),A
        INC   HL

Article: by Vladimir Larkov, 1996, St. Petersburg

Fragment 6/6

        LD    BC,#FADF ;BUTTONZ CONTROL
        IN    A,(C)    ;READING FROM PORT
                       ;OF BUTTONS
        CPL
MDRV1   JR    NONDEF ;JR UNTIL FIRE
                     ;BUTTON NOT DEFINED
        RRA
MDRV2   RL    (IX+0) ;FIRE
        RRA
MDRV3   RL    (IX+5) ;CANCEL
; ***** COORDS CONTROL *****
MDRV4   LD    HL,(MCOORD) ;FORM LAST
                          ;CURSOR COORDS
        LD    DE,(MPORTS) ;FROM LAST
                          ;READED MOUSE
                          ;COORDS
        LD    BC,#FBDF
        IN    A,(C) ;READING FROM
                    ;PORT X-COORD (0-#FF)
        LD    (MPORTS),A
        SUB   E
        JR    Z,MDRV9
        JP    P,MDRV6
        LD    (IX+4),1 ;MOVE LEFT
        ADD   A,L
        JR    C,MDRV5
        XOR   A        ;MIN X-COORD
MDRV5   LD    L,A
        JR    MDRV9
MDRV6   ADD   A,L
        LD    (IX+3),1 ;MOVE RIGHT
        JR    C,MDRV7
        CP    #FE      ;MAX X-COORD
        JR    C,MDRV8
MDRV7   LD    A,#FE    ;MAX X-COORD
MDRV8   LD    L,A
MDRV9   LD    B,#FF
        IN    A,(C) ;READING FROM
                    ;PORT Y-COORD (0-#FF)
        LD    (MPORTS+1),A
        SUB   D
        JR    Z,MDRV14
        NEG
        JP    P,MDRV11
        LD    (IX+1),1 ;MOVE UP
        ADD   A,H
        JR    C,MDRV10
        XOR   A        ;MIN Y-COORD
MDRV10  LD    H,A
        JR    MDRV14
MDRV11  ADD   A,H
        LD    (IX+2),1 ;MOVE DOWN
        JR    C,MDRV12
        CP    #BF      ;MAX Y-COORD
        JR    C,MDRV13
MDRV12  LD    A,#BF    ;MAX Y-COORD
MDRV13  LD    H,A

MDRV14  LD    (MCOORD),HL ;NEW CURSOR
                          ;POSITION
                          ;IN PIXELZ
        POP      IX
        RET

                          °∙· End ·∙°

Share your thoughts about the article