(C) Droy 2002

While I was writing some tips to solve problems about Spectrum' keyboard, some guys asked me for the possibility of using a PC-keyboard coupled by adapter. One market solution was launched by 'Sintech', but isn't available the scheme nor the program used by inner microcontroler. This lack of information pushed me to do an innovating project: I would do the design, prototype, programming, debugging, etc.

This adapter, developed for me has the name IMAR-3 and his article give all information to be constructed by electronic amateurs.

PC-Keyboard adapter for Spectrum

  • What this adapter do? Do I need to load something in my Spectrum ?
  • How this adapter works?
  • How a PC-keyboard works?
  • Inside the adapter
  • Keyboard mapping
  • Making of
  • 1. Using preperfored boards
  • 2. Using Printed Circuit Boards
  • 3. PIC programming
  • Power-On time
  • Known problems
  • Questions and Answers
  • History
  • Downloads
  • Acknolewments
  • Disclaimer

  • What this adapter do? Do I need to load something in my Spectrum?

    This adapter is connected to expansion bus same as joysticks adapters, giving us the possibility of connecting an standard keyboard, type AT or PS/2. (Old keyboards 94-keys are unuseful). This heart of the adapter is a microcontroller PIC 16F84. This PIC should be programmed before insertion on the board ( a few steps forward will be explained how to program it). Once the PIC be programmed an inserted, is ready for working, and you don't need to open nor load anything in your Spectrum. .

    How this adapter works?

    If you opened your Spectrum sometime, you found two flat cables, 8 and 5 contacts which are part of bottom of keyboard, a matrix under rubber keys. The matrix is divided in 8 half-rows with 5 keys each. The following figure shows this:

    As you can see in the figure, every half-row is connected to one line in the upper-half of ADDRESS BUS, and the other end of every key is tied in a five-group to a line of DATA BUS, D0 to D4, through the ULA. When a key is pressed, the whole half-row is read (5 keys) and the I/O port writes a '0' in the address line of this half-row. For clarify: Let's assume that we want to know if is press the key 'W'. Then we read the port IN (64510)binary is 11111011 11111110. This reading set to '0' the line A0, telling to ULA that the keyboard is being read, and also the line A10 is set to '0', meaning that half-row number 2 is being read. In response, a number will be set, which binary bits are tied ,from D0 to D4 with keys 'QWERT' one to one, because DATA BUS is forced to '1' throu resistors R65-R69. (one key released means '1' and if pressed, is set to '0', got from address bus).

    In this example, is the bit D1 which shows the status of key 'W'. You can see this 'online' writing following program and see the changing status depending the key you press in the half-row 'QWERT'. If the value is set to binary, is more clear to understand. In a emulator works fine, indeed!.

    10 REM example of read of keys without command INKEY$

    20 CLS 30 PRINT AT 10,1;“Byte read in half-row 2:”;IN(64510)

    40 GOTO 300

    Keep in mind that bits D5 to D7 will remain always to '1'. You can repeat for remaining half-rows.This table contains the reading address:

    Adress       Keys        Half-row

    FEFE-65278     V  C  X  Z  CS     000 1
    FDFE-65022     G  F  D  S  A      001 2
    FBFE-64510     T  R  E  W  Q      010 3
    F7FE-63486     5  4  3  2  1      011 4
    EFFE-61438     6  7  8  9  0      100 5
    DFFE-57342     Y  U  I  O  P      101 6
    BFFE-49150     H  J  K  L Enter   110 7
    7FFE-32766     B  N  M SS Space   111 8
         D4 D3 D2 D1  D0
    How a PC-keyboard works?

    In the Spectrum, his keyboard is part of inner hardware. PC-keyboards does not: is independient of the host. You can see that only 4-wires cable is enought to communicate them. The host is tipically a PC, but in our case is the controller which does the host role. Communication beetween PC and keyboard is done using a 5-pin DIN connector 180º or using a connector PS/2 (mini-din 6-pin). In both cases one wire is the 'clock' signal, other wire is 'data' and the other two wires are power supply +5V and GND.

    DIN 5 PINES                                     Mini DIN 6 Pines PS/2

    Host and keyboard 'speaks' in serial mode: 1 start bit, 8 data bits, odd parity and 1 stop bit. This is bi-directional: Keyboard says the host about keys activity and host send back configuration commands, lights on (blockNum, etc), restart, re-send, etc. This means that data signal can be used by both parts, and clock signal is driven by keyboard.

    LSB:    Less Significative Bit

    MSB:   Most Significative Bit
    P Odd:  Odd Parity
    ACK:    Acknowledge
    EIn the upper figure, blue color means a keyboard-controlled signal and red color is a host-controlled. When a key is press, the keyboard send a 'scancode': One single code of each key using a map. There are 3 modes or 'maps', depending the kind of keyboard:

    Scan code set 1: Is used for keyboards type XT 83-keys

    Scan code set 2: The most common: used for keyboards 102-keys

    Scan code set 3: Used only in PS/2 keyboards The Scan code set 1 is out of use.

    The Scan code set 3 is the one which allows easily be used in this adapter, reason by is the choice. In order to use also older keyboards, I wrote another program shorter (less key functions as erase,escape,colon, etc) but can be used in keyboards type AT, Scan code set 2. In addition to Scan code set, keyboards can be configured in work modes: Make: when a key is press, the scan code is sent, and after releasing none code is sent. Make/Break: when a key is press, the scan code is sent and after relasing, again a scan code is sent, with a releasing-code 'F0' leading. Typematic: When a key is press, the scan code is sent, and after a delay, is re-sent till the key is released. The AT-type keyboards allows the delay of self-repeating (typematic), but not everyone lets the non-typematic. The PS/2-type keyboards always allows both modes. Following is an extract of Scan code 3, used by the IMAR-3 adapter:

    Scan Codes Set 3

    V=42  C=33  X=34  Z=26  Shift=18
    G=52  F=43  D=35  S=27  A=28
    T=44  R=45  E=36  W=29  Q=21
     5=46  4=37   3=38   2=30   1=22
    6=54   7=61   8=62   9=70    0=69
    Y=53  U=60  I=67   O=68   P=77
    H=51  J=59  K=66  L=75   Enter=90
    B=50  N=49  M=58  Ctl=17  Space=41
    TAB=13  CAPS=20  Shift=18/89  CTL=17/88   ALT=25/57
    UP=99  DONW=96  LEFT=97   RIGHT=106   <>=19
    ESC=8  ,=65  :=73   ;=74  “=82
    When there are two numbers, means the same key, in left and right side (i.e. 'control', etc). More info about PC-keyboards in:

    Much and best quality info abot keyboards in:

    Inside the Adapter
    The 'heart' of adapter is the well known microcontroller PIC 16F84. This is a cheap chip, common to find and easy to program. Is made of a microprocessor, an internal flash eprom memory which keeps the program (just 1 Kbyte of 12 bits each), an internal RAM memory for data (68 bytes of 8 bits), and 13 I/O ports configurable as Input or Output. There are several models, but the most common is 16F84A-04/P. The meaning of xxFxxA-04/P is 'program memory= Flash Eprom, clock speed 4Mhz and plastic body. The scheme shows that clock and data lines are tied to PIC I/O ports RA1 and RA2, being the real control of communication beetween adapter and keyboard.

    The memory chip RAM 6116 is used to store in its first 8 cells the status of every 8 hal-rows, so, when Spectrum wants to read one of them, the adapter brings the content of the address in RAM where this status is stored: In this way, Spectrum 'believes' that are its own keys that are being pressed. When we pressa key, PIC gets his ScanCode.

    Then, looks in his internal EEprom, where are previously loaded the codes por each ScanCode linked to each Spectrum' key and if the 'compare' is found, means thay keystroke is right. From memory address where ScanCode is, data for Key and for Half-row are developed. Once this value is calculated, program check if in port RA4 the controller is trying to read the keyboard. If answer is 'not', this access is blocked through port RA2 while the new status of the half-row is written in the address associated in RAM of controller. When Spectrum reads a half-row, uses a machine instruction IN A,(FE). This one activates (set the signal to 'low' level) for lines IORQ/RD and A0. Logic gates OR (parts of IC5A & IC5B) activates the transceiver (IC3 - 74LS245); after this, the spectrum can get data read from the RAM port (IC1-6116).

    This means that when a half-row is bein read, the priority encoder (IC7- 74LS148), do a code made of 3 address bits the number of the half-row which is being read, and then, multiplexer quad two-entries (IC4 - 74LS157) give access to spectrum's memory or in this controller to PIC which is under logic control of IC5A , B , C and also by I/O port RA2 of PIC.

    Keyboard mapping:
    The 'special' keys used by Spectrum has his counterparts in PC-keyboard as follows:


    Teclado PS/2 o AT

    Symbol Shift

    Crtl (Left and Right)

    Caps Shift

    Shift (Left and Right)
    Caps Lock (Caps Shift + 2)
    Caps Lock
    Extended Mode (Caps Shift + Symbol Shift)
    TAB (Edition mode only)
    Flechas del cursor (Edition mode only)
    Backspace (Caps Shift + 0)
    Borrar (Backspace)
    Dot (.)
    Dot (.) (Edition mode only)
    Colon (,)
    Colon (,) (Edition mode only) 
    Semicolon (;)
    Semicolon (;) (Edition mode only) 
    C (")
    Shift + 2 (Edition mode only)
    Break (Caps Shift + Space)
    ESC (Edition mode only)

    You can choose betwen normal mode and edition mode pushing F12. The Joystick Interface II movement is mapped to the arrows keys, and fire button function to both Alt keys and the zero numeric. F1 will be very useful for PlusD and Disciple users.

    F1 = LOAD d1;"snap "S

    Making of:

    As stated in previous gadgets shown on 'Trastos de Droy', this controller can be made using preperfored boards in case you are not keen to use acid, developer, etc. I made this prototipe in this way because I could make some last minute changes during develpment instead of beguin again after each change. Given this design is finished, if you choice a Printed Circuit Board, is up to you the dessign, but can be useful to use EAGLE 4.0 and the scheme provided.

    1. Pre-perfored boards.

    In section 'PlusD' (Trastos de Droy) you'll find a detailed description of making a Bus Adapter to use flat cable beetween several devices atached to one Spectrum. This adapter was used in the controller, so now i'm ready to connect it trough PlusD via a flat cable.

    As I use to do, a picture of all components before any soldering. If you never worked before with preperfored boards, in another section of 'Trastos de Droy' you'll find know how.

    The basic is to arrange all components in the board and stick them with tape till I find the best way to inter-connection themselves. After a few tries, keeping in mind the extra holes for set apart the stripes, it looks like this picture:

    The 100nF capacitors are for electric noise reduction (generated for TTL technology), so they must be connected as near to power pines in each IC as you can. As is undesirable to forget any wire, I do the following: Upon the wires are being connected, I overwrite in red colour in the scheme to be sure that both ends of circuit are connected. Don't forget the power connections for IC's, because they usually aren't specified in schemes. Anyway, here you are the pinout:

    Integrated Circuit
    PIC 16F84

    Little time after you have soldered all connections, and spent some time in doing the shape of box, this can looks so:

    Little time after you have soldered all connections, and spent some time in doing the shape of box, this can looks so.

    Pay attention about how the side of box is used as heat sink of IC 7805, out of board. Don't worry because some components of picture aren't the same as previous pictures: Is because the 'dynamic' changes experienced along the making time. Anyway, the idea is to follow the scheme of this ultimate version..

    For multi-connection purposes through the flat cable, I connect inside a 34-pin male connector to outside. See following picture:

    2. Using Printed Circuit Boards.
    On the Download section you can find all the files you need to build the print circuit board and program the PIC controller. The box could be more dificult but take a look of the following photos to take some ideas about it.

    In this design, I looked for two targets: one is make its manufacturing as easy as possible, and the other is that expansion bus of the Spectrum can be propagated just in case you'll want to connect another more adapter in addition of IMAR-3. You'll have to use the photograpic method because the routes are really thin. If you are able to optimize this dessign, do it using Eagle, and if it works, will be published in this topic with your name and my aknowledgments.

    As you can see at the bottom, the expansion bus is propagated using a small board that is soldered over the true pins of expansion connector. You need to do this board only in case of use of more adapters at a time. If other adapters has it's own propagate connections, this part can be avoided also. For the ones that prefers to add this part, a must is to tin-plate contacts. Is highly recommended to use ***¿FLUSHING PASTE?***. Don't forget to cut out the slot in the extension board.

    The voltage regulator LM7805 must be screwed on a heat sink. A tip: in the aluminium workshops (windows, blinds, etc), they give to me for free the small leftover parts, and here are use in double fuction: heat sink and angle iron to stand the expnasion board. Next pictures illustrates in detail the machining.

    The front is covered using a fiberglass board (one that had cupper but excessive acid turned it clean by mistake and so has a second life). this material is cheap ans easy to use. The back is protected using clear plastic. naturally that other materials can be used but the clear plastic is a touch of class !!.

    IMAR-3 proudly connected to an Spectrum 48K Plus.

    IMAR-3 assembled to an Spectrum 48K and to an clonic Interface II using bus propagate.

    Front view: remark on thanks to the fact of side exit of keyboard is possible the assembly of several adapters, as well if IMAR-3 is the first, as is in another position, without being an obstacle .

    Announcement for potentials buyers / purchasers:

    Altought I'm not keen of selling adapters, I'll change some ot them by Spectrum's hardware. Strange as it may seem, after reading an article about Interface II' cartridges, I' don't have an authentic one. If you are prone to change it, just e-mail me. I just want hardware, preferably Sinclair, but nothing about tapes of programs.

    3. PIC programming
    The family of microcontrollers PIC is one of the most used. Just a sight in the Web and you'll find heaps of info. You need write the program in the PIC. To accomplish this, you have to download in the 'dumps' section of this page the program or buy one; Aren't expensives: about 5 or 6 €, and then you have choices to program it:
    Power-On time

    Before aplying power for first time, it's a good idea to check all the board before. Pay attention at key in the back connector of Spectrum to avoid a shifted contact. Inmediately after power be on, all lights in keyboard lit, then dark, and finally, Caps Lock will remain On. If this happens, means that PIC is working fine. If unfortunately the sequence is wrong, check the PIC connections, cause is the only IC doing the task of keyboard initialization. You can remove the other IC's from it's sockets to help the debugging. Before e-mailing me, be sure that all IC's have the power connected (see above), is the most common failure. If you are the proud owner of a oscilloscope, look for PIC clock pulses at pin 15-16. Re-check solderings, point-to-point connections, keys of IC's. If after a cute checking of all points it doesn't work, don't cut yourself the veins: e-mail me, I can help.

    Known Problems:

    After prototype was made I tried several trade programs to check quality: I expected to find only performance difference, because my PIC program wasn't still optimized. But... near a third of them had lacks of controllers. Even one of my favourites -Ultimate ¡ yes, the one of Knight Lore! - showed same problem. I had to diassemble this programs to study how his programmers did the keyboard reads.

    I had wrongly supposed that commercial software read the half-rows in the same way as ROM routines did, i.e. one at a time, as is own BASIC editor does. Really don't do that. If you are a Knight Lore nut, you'll know that all upper row (half-row 1-5 and 6-0) has same function, to grip something, and half-row Q-T and Y-P are used for jumping. Half-row A-G and H-L are for moving. His programmer toke advantage of reading simultaneously two half-rows for better performance, because doesn't matter which key is pressed, but what row (2 half-rows) has any pressed key (doesn't matter which one). This trick of simultaneous reading of two half-rows pushed me to change the first prototype and swap the 74LS245 (transceiver) for one 74LS148. In paragraph 'how it works', is explained that 74LS148 is a priority encoder, giving a 3-bit address (000 to 111), depending on what of his 8 inputs is set to '0' (connected to A8-A15); this points to the half-row being read. As is 'prioritized',in case of 2 half-rows read at the same time, the address taken will be that has higher priority, so, at least it will get the status of one of them. In short terms: if program reads two half-rows simultaneously, doesn't matter which key is, but what half-row has a key pressed. For example: If running a program, the second row (Q to P) are for 'advance', probable the two half-rows be read at same time. As only the higher priority is addressed, the 'advance' will run only with keys Q to T. then, the remaining keys Y to P are useless in this program.
    This problem surfaced only two games produced by Fairlight and Fairlight II. I had to dissasembly them to know what the problem was. The programs check from time to time the key's status to take decissions. But this programs checks ALL the keys, so is easier to take the reading of 5 half-rows at same time. The important is if any key is press, instead which one. If any is, then each half-row is carefully read to know which and act upon. This is a 'saving time' way when no keys are pressed. Smart, isn't it? In my opinion, this way to save time is bad for performance, because the saved time is only when no actions are needed, but when we needs to move the 'sprite' is when this trick makes one more reading, rendering a penalty just when is worst. perhaps this is the reason why this single read hasn't be spreaded. This is a hard task to solve for my interface, but if there aren't more programs like those, i'm afraid i will not spend time in this topic.

    Questions and Answers

    Do I need to load some program at Spectrum?
    No. It would hard to write LOAD"" if the Spectrum's keyboard was broken. Just power on and is ready to use.
    Is it harmful if I push simultaneously both keyboards?
    No, diodes D1 to D5 isolates both circuits.
    Do I need an extra-power supply?
    No. This adapter is fed by 9V from bus, before is converted to 5V, hence no overload on 7805, but there is a drain of 150 mA (including PC keyboard) out of external Power supply of Spectrum. In case of several adapters simoultaneously working you have to add the extra current drained to each one, plus 600 mA taken by Spectrum, until the total current supplied by the external device. There should be a label with the max. current available. (Note: 1.6A = 1600 mA)
    May I connect another adapters?
    As you can imagine, not every adapter in the market were tested, but I suppose it shouldn't be any problems. I worked stressing a 'PlusD', succesfully.
    When I connect IMAR-3 behind the ZX Printer, or some other adapter (write here the name of you adapter) the PC keyboard doesn't work. What is happenning?
    By economical reasons, many adapters doesn't propagate the expansion bus in the totality to his out connector, so some electronic signals may be missing. This explains why some market adapters (ex. PlusD) doesn't work.

    May I use a Xtal faster or slower than 4 MHz ?

    No, the loops in the control program are calculated for 4 MHz, if you want to use a PIC with diferent speed, you has to compile the source again changing the speed field.

    Do I have to use a DIN 5-pin connector or a Mini DIN 6-pin depending upon ver sion (AT or PS/2) of keyboard ?

    The best is to use the connector keyboard type that you'll use commonly. It is possible to use the typical conversion plug. Altought is weird, there are PS/2 keyboards provided of DIN 5 connector.

    Will the IMAR-3 implement new functions as 'shot autorepeat', 'joystick emulation', etc?

    No. perhaps in the future i'll do improves on the control program of PIC. Anyway new implementation as discarded. See in 'history' topic the entry July 5th 2002. .

    When I'm writing programs in basic, the very quick pusing of keys doesn't show all characters in the screen. Is my adapter failing? Is a dessign failure or speed problems?

    It is not a dessign nor contruction of the adapter. Is a Spectrum' problem, indeed!. The rubber keys hides this. The inner Basic editor reads the keyboard from time to time, each 20 milliseconds. If you press and release the key before this lap, the half-row has not been read yet. This behaviour recalled me when I was changing a genuine broken keyboard for switches (see section 'tips for solve membrane problems). If one key was pressed 10 times, I only got 6 or 7 characters in the screen, instead of 10 as expected. Here you are the oscilograme of signal which enables the reading: ease to see 8 pulses, one to each half-row.

    Techical data: The reading of keyboard give a train of 8 pulses, 700 nanoseconds each, separates apart 16 microseconds beetween them. Time beetween trains of pulses is 20 milliseconds. Using the Basic editor, each program reads the keyboard using it's free will, usually faster, not periodical, not every half-row .

    If I press simultaneously several keys, looks like the last ones doesn't work. Is it a damage or a fault?

    Neither of both, really the problem is caused by PC keyboard. For dessign, there are a maximum number of keys that can be pushed at same time before a error arose. Typically are 5 or 6, depending on which keys be. Your only chance is to try another keyboard an cross your fingers. Anyhow, I've never found a program that needs more than 5 keys pushed at same time.

    I've got an older keyboard, the one that have the function keys at left side and I want to use it with IMAR-3 interface. Are you planning include a project for XT keyboards?

    No, there is a very big difference beetween XT and AT - PS/2 keyboards. I would needed to do all work again. If you've got an old XT, for sure doesn't work.

    I'm a Fairlight fan , but it doesn't run. What can I do?

    You have to use this POKE to solve this:

     POKE 62470,24

    ¿Can I connect the IMAR-3 to the Spectrum PLUS 128K, +2 or +3?

    No, it is designed for 48K and 48K+ only.

    Why two modes, Normal and Edition?

    Normal mode is just to play games and it doesn't implement any special funcion. Edition mode implement some special functions very usefull to editing programs. You can select mode with F12

    Is it Freeware the software running inside the PIC?

    Not really... is GreatGuyWare. That means, that in case you uses it, you have to send me an e-mail saying ' You are a great guy sharing you time and your work with the whole world, and in adition, you are not earning money. A true contributor'. This helps me to know if people really uses my hard work that I'm sharing here. In this way I'm stimulated to promote the birth of new projects like this one.



    24/4/2002 Begin the desing.

    26/4/2002 Finish the desing. Version A
    27/4/2002 Build a prototipe to connect a LCD and a Keyboard to a PIC to study the keyboard's comunications.
    28/4/2002 Get a preliminary program of comunication.
    04/5/2002 Build a IMAR-3 prototipe. Versión A
    05/6/2002 Testing with the Spectrum
    15/6/2002 Fisrt working program (Only make)
    20/6/2002 Working with double keys, a bit slow.
    20/6/2002 Autorepeat and backspace.
    23/6/2002 WriteM bug fixed
    24/6/2002 Begin test with comercial software.
    25/6/2002 Problem with Knight Lore isolated
    01/6/2002 Finish redesing versión B
    02/7/2002 Build new prototipe Versión B.
    03/7/2002 Manic Miner problem isolated.
    04/7/2002 Minor hardware corrections. Now Versión C
    07/7/2002 Fairlight I and II problem isolated (not fixed)
    08/7/2002 Debug and Tunning
    25/7/2002 First version of this article.
    10/8/2002 The control program is rewritten in JAL (An amazing Freeware compiler).
    11/8/2002 Julio Medina alert us about a mistake in the power pin asignation (Sorry for the inconvenience. Thanks Julio).
    13/8/2002 Added support for AT and PS/2 keyboards.
    15/8/2002 Simulation of Sinclair Joystick added.
    16/8/2002 Edition mode added (Cursor movement,colon, semicolon, Etc).
    01/9/2002 Minor hardware corrections. Now Version D.
    05/9/2002 Available PCB Desing (EAGLE 4.0 and PDF)

    Downloads (New ! 5/9/2002) Electronic schemme in PDF format (New ! 5/9/2002) Electronic schemme in Eagle 4.0 format
    Library for Eagle with the Spectrum slot connectors (New ! 5/9/2002) PCB in PDF format (New! 5/9/2002) PCB in EAGLE format  File to program the PIC. Valid for PS/2 and AT keyboards:
    Document to build a PIC programmer

    Agradecimientos / Thanks to:

    Mi mujer, Maria Jesús, que sigue mostrando una paciencia infinita con mis cacharros, e incluso ha repasado y corregido este texto.
    To my brother Eduardo for his help persuading my father to buy the new model ZX-Spectrum instead of the old ZX-81 when I was very young.
    Thanks to Wouter van Ooijen for his amazing compiler JAL, and for make it freeware.
    Thanks to all the people that have contributed in this great PC Keyboard FAQ.
    Thanks to Craig Peacock for his excelent article about interfacing the AT keyboard, and for the time diagrams of the keyboard that you can see in this page. Good job Craig, thanks.

    Thanks to Fernando G. Fonseca for his work translating this article and for his friendship.

    Madrid, July the 25th, 2002

    I share the information here without any warranty nor responsability for the damages it could cause to your computer.

    What I can tell you is that "IT WORKS !!!" but .... up to you.