33

I'm attempting to build a Z80 homebrew computer to teach myself the basis of electronic design. I planned to start from something simple, like a ROM, some SRAM, a video chip and a Z80-PIO. I'd like to build a machine with straightforward I/O, that it, the design requirement: one should be able to use the computer by simply plugging a PS/2 keyboard, rather than hooking it to a RS232, USB-TTL stick or some exotic CRT terminal.

But how to incorporate a PS/2 keyboard input to my computer is unclear to me. According to my research, this problem has been solved by the following approaches.

  1. Shift Register. A simple shift register like 74HC595N should be able to translate the serial data from the keyboard to parallel, and it can be clocked by the keyboard, so it has no problem to follow the PS/2 protocol. But PS/2 is 8-bit data, 1-bit parity, not 7-bit data! Both the Z80 and the shift register can only handle 8-bit of data , so the parity bit is ignored (or requires additional parts), not elegant. I don't think parity is only for farmers, especially when bidirectional transmission is also needed for controlling Caps Locks, etc. So a parity bit is needed, also, we need to follow the timing, have some meaningful interrupts control - all of these requires extra parts, which makes it difficult to design and program (especially when routing it on a single-layer board).

  2. USART. Although PS/2 is not designed as a USART-compatible protocol, there are reports of success by simply running the USART at 12,000 bauds and connecting the DATA line to a USART interface, and handling the keymap decoding in software. It sounds convenient since the USART is general-purpose, may perform buffering, parity check, and interrupt generation. However, PS/2 is clocked by the keyboard and its frequency lies at anywhere between 10.0–16.7 kHz, again, this method is NOT in compliance of the protocol at all, and could only work by chance, so it's more of a clever trick than a proper solution.

    1. SPI. Obviously, putting the widely-used SPI interface in slave mode can allow the serial transmission to be clocked by the keyboard. But I think it's just another clever trick to exploit the functionality on a modern microcontroller than a standard solution. Historically, it seems a SPI controller is never used in this way.
  3. PIC, Atmel, or ARM microcontroller. It's the most common, easiest, flexible solution used in various retrocomputing projects. But to me, having a microcontroller with processing power comparable to (or greater than) the Z80 CPU defeats the spirit of a retrocomputing project. I don't have a problem of using one for inherently computational intensive tasks, such as Ethernet or Wi-Fi, but for a mere keyboard, seriously?

  4. 8042. In the original IBM PC/AT design, the underlying keyboard control sequence is managed by the 8042 connected to the Intel 8255 PIO. A 8042 is just a version of the MCS-48 (8048) microcontroller designed for interfacing peripheral devices, IBM must have written a customized firmware. It's difficult to find a 8048 today, nevertheless, MCS-51 (8051) is also a retro chip so this one can be used, too. But is there a reference implementation of the original 8042 firmware available under a free and open source license?

  5. I think there are also controllers specifically made to control inputs like PS/2 keyboards, but they typically integrates more functionalities, and I'm not aware of a chip from the late 80s. If there is one, I think I could just use that, provides it was a common design choice of that time.

Currently, I'm considering to interface the PS/2 keyboard by implementing the 8042 keyboard controller on a 8051 MCU if there's a free reference implementation available, if not, I think it should be okay for me to implement something simpler, too.

But I think programming for two separate chips makes my project more difficult, especially for the initial prototype. I must have missed something... I knew most keyboards from that era were parallel ones, not PS/2, but there must be some additional and possibly simpler methods to interface a PS/2 keyboard, are there? How was it being done historically?

  • 4
    This is very well-researched. Thanks for the question; I'll check back later to find out the answer. ☺ – wizzwizz4 Mar 20 '19 at 22:46
  • 1
    Send me your address and I provide you a dozen of 8741 chips. – Janka Mar 21 '19 at 7:42
  • 9
    Regarding your point 3 -- yes, seriously! By choosing to use a PS/2 keyboard, you've already got a CPU "comparable to the Z80" in the keyboard itself! If you want historical accuracy, find or make a parallel keyboard. Otherwise, bite the bullet and use a microcontroller to provide the interface -- as it was properly done on the original PS/2! Even the older PC/AT had a dedicated micro on the motherboard for the keyboard interface. – Dave Tweed Mar 21 '19 at 11:22
  • 1
  • 1
    @DaveTweed My objective here is NOT historical accuracy, I'm not trying to recreate any historical system. Rather, the goal is to construct the system in such a way that it only uses hardware available in the late 80s. Or, use historical methods to solve present problems, so... if there's Cortex-M3 in my keyboard, fine, as long as I'm not putting my own Cortex in. Use a microcontroller is fine, but a microcontroller after 1990 is not. – 比尔盖子 Mar 22 '19 at 16:53
21

I'm attempting to build a Z80 homebrew computer to teach myself the basis of electronic design. I planned to start from something simple, like a ROM, some SRAM, a video chip and a Z80-PIO.

So there's a Z80-PIO? Isn't that already the solution to be used?

Not to mention, that adding another micro controller for a job the main CPU can do as well is neither simple nor true to the spirit of early systems. I mean, adding another whole computer system, even of greater performance than the main system, just for a keyboard interface?

A Z80 with a PIO can do everything (*1) a separate system can do. In this case it needs 4 port bits. Two for receiving data/clock from the keyboard/mouse, two to overwrite these to initiate an output sequence and control output data. Externally two drivers for the input signal, two resistors and two transistors, as shown, may be helpful.

External circuitry for PS/2

It may be helpful if the clock input pin can be set to interrupt the CPU so a response to a key press can be handled without polling, but it'll work as well by pulling clock low and only releasing it when ready to receive a byte (*2).

Everything after that is simply bit-banging the protocol. The CPU got 5 microseconds per bit to react, which can be done on a tight looped 4 MHz Z80. Of course it will need some fine tuning, but isn't that exactly the goal of your project?


On a sidenote:

USART. [...] However, PS/2 is clocked by the keyboard and its frequency lies at anywhere between 10.0–16.7 kHz, again, this method is NOT in compliance of the protocol at all, and could only work by chance, so it's more of a clever trick than a proper solution

Of course it is a proper solution - all that's needed is to clock the USART externally. Important here: the Z80 SIO is well fitted to do the required 8E1 data format. Neither a hack nor anything clever is needed - maybe except to detect the acknowledge bit on host to kbd transfers.


And further:

How was it being done historically?

This depends a lot on the machine we're talking about.

Parallel keyboards where somewhat common, when it came to ready made units to be added as component. These where either

  • hard encoded (diodes!) or
  • via a decoder circuit (think Apple II).

Serial was (in the beginning) even more common, usually when keyboards had to be complete OEM units just to be plugged in (DEC, Apple Lisa).

Home/custom build solutions for embedded keyboards operated by scanning a matrix from the main CPU

  • via some latches,
  • a PIO like port (PET),
  • some combinations of both (Tandy MC-10). Or worse:
  • By mapping the matrix as data into an address range (TRS-80 M1)

*1 - Well, next to that is, as separate controllers may be faster in some special cases - except, PS/2 is none of them.

*2 - Yes, very easy and a protocol conform way. Drawback here is that each poll will have to wait at least for 100 (better 200) microseconds after releasing the clock line to check if the keyboard wants to send at all. So not really cool.

  • 1
    "I mean, adding another whole computer system, even of greater performance than the main system, just for a keyboard interface?" - that's how PS/2 was used in the PC series, isn't it? – user253751 Feb 11 '20 at 15:59
12

Just use Z84C40 SIO chip. It's a part of standard Z80 family chips and has two channels. I used it for PS/2 keyboard and USB connection to my breadboard Z80 computer running at 2.4576 MHz. Z84C40 can divide system clock by factor of 16, 32 or 64 thus I run my UART-USB bridge at 38,400 baud without use of 4-bit counter as system clock divider. Luckily PS/2 keyboards generate clock themselves. So all you need to connect a keyboard is this chip and PS/2 connector (female).

LCD 20x4 chars

Update: I finally made shield board for TFT screen which provides back compatibility with previous LCD screen (no need to rearrange connection wires). Also made a convenient EEPROM programmer shield for Arduino MEGA. So now I have a lot of fun coding my Z80 breadboard machine which runs at 4 MHz (at 5 MHz it runs unstable due to TFT). When I need to perform USB transmission I switch it back to 2.4576 MHz. TFT 320x240

TFT shield

EEPROM programmer shield

  • 2
    That's a beautifully simple design. Do you have a writeup somewhere? – occipita Oct 9 '20 at 3:21
  • this is pretty much exactly what i was planning to build, if there's any schematics/writeup/blogposts/whatever about it i could take a look at to save me some headache, that'd be extremely appreciated :) – nonchip Oct 10 '20 at 21:17
  • Well.. my design was inspired by Ben Eater's one (youtube blogger), but his system is based on 6502 CPU and still is not completely developed. I didn't post yet my Z80 system anywhere but here. I probably could draw some schematics, but it would be kind of spoiling all the fun for you. As far as I understand, the essence of this kind of hobby is not to just GET a retro system, but to elaborate the design by yourself, build it and make it work. Besides my design is so simple and straightforward so there's practically nothing to explain, just get the chips and download corresponding datasheets. – Georgy Batog Oct 12 '20 at 1:03
  • @GeorgyBatog was thinking mostly about the SIO part because i frankly have no clue how PS/2 actually works, if you have any pointers for that that'd be really great :) the remainder is already kinda planned out on my end, got the z80, some rom, some ram, some GALs for bank switching and address decoding magic, my clock is a canned crystal resonator w/ integrated driver because i'm lazy, and for debugging i'll have an stm32 bluepill, but i wanna phase that out as soon as possible (don't like a stronger computer being IO for the one i'm actually working on) which is why i'm looking for the PS/2 – nonchip Oct 12 '20 at 14:56
  • @nonchip I think you won't have any problem with finding datasheet for SIO. The chip id is clearly seen on the photo. Just prepare to have some mess with SIO setup registers. Also you gonna need a PIO chip for parallel interface i.e. display. Initially I used LCD 20x4 chars, but now I'm switching to color TFT 320x240. Right now I'm considering to use leftover pins of PIO for driving SD card reader embedded into my TFT module. Concerning GALs for address decoding you gonna need only NAND and NOT. Not sure about bank switching in such a system.. 32k "ought to be enough for anybody". – Georgy Batog Oct 12 '20 at 20:55
10

PS/2 is a SPI protocol. Talking to stuff like your keyboard is literally what SPI peripherals are designed for.

So the most obvious choice, if your micro has a SPI peripheral, is to use it. If you think that sounds too easy, wait until you start looking at the registers to set it up. It can be a lot harder than you think, if you're not used to following specs to the absolute letter!

But let's suppose you want to do it the hard way. Then you're looking at shift registers and some logic, which is how the peripheral works anyway. It's not just the parity bit you need to check. For a valid frame, there must be start and stop bits too. Two 8-bit shift registers are fine - just ignore the lower bits.

You could interrupt on a keyboard clock edge, read in both shift registers, and work out whether the top 11 bits form a valid message. That's cheap on components, but puts more load on the processor. At 16kHz, it's probably not too bad though.

Or you can decode in hardware - basically build yourself a small version of the SPI peripheral you've got in your micro. You can do this in stages of increasing complexity.

  1. The most basic version would just use a 4-bit counter to count clocks, interrupting the micro and resetting itself after every 11. Assuming there are no framing errors, that will work. With any framing errors, it will always be wrong though.

  2. Then we look at the start bit. Now the counter needs the data line to be low before it'll start counting. Once the counter is non-zero, it keeps counting until it reaches 11. OR together the data line and the 4 counter bits, and you're set. Now if there's a framing error, repeated sends will eventually sync up and you're sorted.

The neat thing now is that you can get back to only using an 8-bit shift register, by only enabling the shift register clock when the counter is between 1 and 9. Other bits won't come through.

  1. Then the stop bit. The 11th bit has to be high. So only interrupt the processor when you get to 11 clocks AND the data line is currently high.

  2. And then parity, which is not a good way of error-checking, but anyway. You want a second counter, clocked normally, and enabled when the counter is between 1 and 9 and the data line is high (or low, depending on how you want to do it). Then you need a flip-flop which captures this on clock 10, and you AND this with the conditions from step 3.

And now you have a working SPI receiver.

  1. For full compliance with PS/2, you should also check the length of the initial clock low pulse, which makes you better at detecting framing errors. Trigger a monostable on the falling clock edge, feed the monostable into the S and enable inputs of an SR flip-flop, and feed the clock input to the R input. If the clock stays low for that time, the flip-flop output stays set. If not, the flip-flop output gets cleared at some point. Use this as another condition for starting the counter. Note that this is actually something you can't do with a regular SPI peripheral.

A naive version might disable this detection whilst the counter is non-zero. After all, we know we've got a frame start, right? The correct version keeps it running though, because what happens if you lose the connection mid-frame and then get it back again? So if you get an extended low clock pulse at any time, it will reset the counter back to 1 to pick up the new frame, and just overwrite anything currently in the shift registers.

I'm not going to get onto the Tx side, because that's a whole nother world of pain. Left as an exercise for the reader, as they say. :)

  • Another variation is to use the shift register as the "bit counter", so that when a particular bit becomes 1, the next clock will capture the state of the shift register and clear it. – supercat Aug 17 '20 at 21:35
7

If you really want to do it the shift-register way, there are 9 bit shift registers available. If you do a search at digi-key for '9 bit shift register', then sort the resulting table by number of logic elements, you can see a few varieties on that theme.

Alternately, you could use chain two 8 bit shift registers and ignore the other 7 bits of the second shift register.

  • 3
    Or even an 8-bit shift register and a single D-type latch (eg half of a 7474). – occipita Apr 24 '20 at 8:55
  • The 9-bit shift registers at Digi-Key are ECL parts, which cannot directly use CMOS, NMOS, PMOS, or TTL logic levels. – supercat Oct 8 '20 at 19:05
1

If you want a "pure TTL solution", maybe an evolution of this project can help you:

https://shepherdingelectrons.blogspot.com/2020/07/uart-transceiver-for-breadboard-computer.html

1

Worth checking out this PS2 Controller built from TTL chips: https://github.com/DerULF1/8bit-computer/blob/master/Schematics/PS2Controler.pdf

as part of this 8-bit computer: https://github.com/DerULF1/8bit-computer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.