Mirror of CollapseOS
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

125 linhas
4.8KB

  1. # PS/2 keyboard on the SMS
  2. Using the shell with a D-pad on the SMS is doable, but not fun
  3. at all! We're going to build an adapter for a PS/2 keyboard to
  4. plug as a SMS controller.
  5. The PS/2 logic will be the same as the regular PS/2 adapter (see
  6. doc/hw/ps2.txt) but instead of interfacing directly with the
  7. bus, we interface with the SMS' controller subsystem (that is,
  8. what we poke on ports 0x3f and 0xdc).
  9. How will we achieve that? A naive approach would be "let's limit
  10. ourselves to 7bit ASCII and put TH, TR and TL as inputs". That
  11. could work, except that the SMS will have no way reliable way
  12. (except timers) of knowing whether polling two identical values
  13. is the result of a repeat character or because there is no new
  14. value yet.
  15. On the AVR side, there's not way to know whether the value has
  16. been read, so we can't to like on the RC2014 and reset the value
  17. to zero when a RO request is made.
  18. We need communication between the SMS and the PS/2 adapter to be
  19. bi-directional. That bring the number of usable pins down to 6,
  20. a bit low for a proper character range. So we'll fetch each
  21. character in two 4bit nibbles. TH is used to select which nibble
  22. we want.
  23. TH going up also tells the AVR MCU that we're done reading the
  24. character and that the next one can come up.
  25. As always, the main problem is that the AVR MCU is too slow to
  26. keep up with the rapid z80 polling pace. In the regular adapter,
  27. I hooked CE directly on the AVR, but that was a bit tight
  28. because the MCU is barely fast enough to handle this signal
  29. properly. I did that because I had no proper IC on hand to build
  30. a SR latch.
  31. In this recipe, I do have a SR latch on hand, so I'll use it. TH
  32. triggering will also trigger that latch, indicating to the MCU
  33. that it can load the next character in the '164. When it's done,
  34. we signal the SMS that the next char is ready by resetting the
  35. latch. That means that we have to hook the latch's output to TR.
  36. Nibble selection on TH doesn't involve the AVR at all. All 8
  37. bits are pre-loaded on the '164. We use a 4-channel multiplexer
  38. to make TH select either the low or high bits.
  39. # Gathering parts
  40. * A SMS that can run Collapse OS
  41. * A PS/2 keyboard. A USB keyboard + PS/2 adapter should work,
  42. but I haven't tried it yet.
  43. * A PS/2 female connector.
  44. * A SMS controller you can cannibalize for the DB-9 connection.
  45. A stock DB-9 connector isn't deep enough.
  46. * ATtiny85/45/25 (main MCU for the device)
  47. * 74xx164 (shift register)
  48. * 74xx157 (multiplexer)
  49. * A NOR SR-latch. I used a 4043.
  50. * Proto board, wires, IC sockets, etc.
  51. # Historical note
  52. As I was building this prototype, I was wondering how I would
  53. debug it. I could obviously not hope for it to work as a
  54. keyboard adapter on the first time, right on port A, driving the
  55. shell. I braced myself mentally for a logic analyzer session and
  56. some kind of arduino-based probe to test bit banging results.
  57. And then I thought "why not use the genesis?". Sure, driving the
  58. shell with the D-pad isn't fun at all, but it's possible. So I
  59. hacked myself a temporary debug kernel with a "a" command doing
  60. a probe on port B. It worked really well!
  61. It was a bit less precise than logic analyzers and a bit of
  62. poking-around and crossing-fingers was involved, but overall, I
  63. think it was much less effort than creating a full test setup.
  64. There's a certain satisfaction to debug a device entirely on
  65. your target machine...
  66. # Building the PS/2 interface
  67. See schematic at img/ps2-to-sms.png. The PS/2-to-AVR part is
  68. identical to doc/hw/ps2.txt.
  69. We control the '164 from the AVR in a similar way to what we did
  70. in rc2014/ps2, that is, sharing the DATA line with PS/2 (PB1).
  71. We clock the '164 with PB3. Because the '164, unlike the '595,
  72. is unbuffered, no need for special RCLK provisions.
  73. Most of the wiring is between the '164 and the '157. Place them
  74. close. The 4 outputs on the '157 are hooked to the first 4 lines
  75. on the DB-9 (Up, Down, Left, Right).
  76. In my prototype, I placed a 1uf decoupling cap next to the AVR.
  77. I used a 10K resistor as a pull-down for the TH line (it's not
  78. always driven).
  79. If you use a 4043, don't forget to wire EN. On the '157, don't
  80. forget to wire ~G.
  81. The code expects a SR-latch that works like a 4043, that is, S
  82. and R are triggered high, S makes Q high, R makes Q low. R is
  83. hooked to PB4. S is hooked to TH (and also the A/B on the '157).
  84. Q is hooked to PB0 and TL.
  85. The code for the ATtiny is in code/smsps2ctl.fs. You can build
  86. it with /cvm/avra.sh.
  87. # Building the binary
  88. We start with the base SMS xcomp and add a few things:
  89. 1. at the top: "SYSVARS 0xa2 + CONSTANT PS2_MEM"
  90. 2. After VDP load: "621 LOAD : (ps2kc) (ps2kcB) ;" (that binds
  91. us to port B)
  92. 3. Right after: "411 414 LOADR" (that gives us "(key)")
  93. 4. After "VDP$": "PS2$".
  94. Rebuild, send to SMS, then run with your keyboard interface
  95. plugged to PortB. It should mostly work. There are still a few
  96. glitches to iron out...