Fixing PS/2 Keyboard handling (Part I)

The way the PS/2 keyboard is handled has always been something we were never quite happy with. The key points being:

  • The PS/2 controller had no way of signalling that there has been a new keystroke, the buffer had to be polled via SPI.
  • The PS/2 controller had no way of talking to the keyboard and had to rely for the keyboard to initialize itself properly. Also, typematic rate and delay could not be set, as couldn’t the states of the keyboard LEDs.

Although mid- to long term, we likely might “upgrade” to USB anyway, but not without having done PS/2 right first. So, I will talk about integrating IRQ handling, and in a follow up post Marko will talk about how he got the PS/2 controller talking to the keyboard.

Luckily, during the design of the IO-board, we have been clever enough to hook IO-pins PC0 to PC2 to RESET_TRIG, NMI and IRQ, respectively. So on the hardware-side, we are very much ready.
First problem to solve is how to emulate an open collector output on the AVR controller. As it seems, a common way to do that is to disable the internal pullup of the pin, and have it configured as input to be “tri state”. When active, the pin gets activated as an output, and will pull the IRQ line low.

// pull IRQ line
DDRC |= (1 << IRQ);

// release IRQ line
DDRC &= ~(1 << IRQ);

Now that we know how to handle the IRQ-line, we need to figure out, WHEN to pull it. Obviously when a key was hit. And when to release it?

Finally, we decided to go the most simple way. The PS/2 controller will pull the IRQ line as long as there are more than 0 chars in the buffer. Once the buffer is empty, the IRQ-line will be released. This way, we do not need an interrupt register and hence no time consuming check of the latter, but need to do a little buffering on the steckOS-side.

This is all the code that’s needed on the PS/2 controller side:

    if (kb_buffcnt > 0)
    {
        DDRC |= (1 << IRQ);     // pull IRQ line
    }
    else
    {
        DDRC &= ~(1 << IRQ); // release IRQ line
    }

Now, we need to add a little handling code to the steckOS IRQ-handler. Since we do not have an interrupt register, we just check the keyboard last, after every “known” interrupt source has been handled.
To get around having to implement another keyboard buffer, we just use a single memory location, labelled “key”. The IRQ handler will only fetch a byte from the keyboard when the target location is zero (0), otherwise it will just exit.
The system getkey-routine will load the contents from that location into the A register, and overwrite the location with 0 again to enable fetching the next char from the buffer.

The SPI check code is the last bit in the IRQ-handler routine:

@check_spi:
lda key
bne @exit
jsr fetchkey
bcc @exit
sta key

@exit:
restore
rti

That’s basically all that’s needed. The former getkey-routine has been renamed to fetchkey, and the new getkey routine only handles the ZP buffer location while retaining the old behaviour including setting the carry flag when a byte has been received. This way, existing programs using the keyboard do not have to be modified.

Now, we finally have a chance of reacting to keystrokes during program execution without having to explicitly poll the keyboard. This enables us to handle Ctrl-C and such much more elegantly. Also, any REPL-like program (like the shell) does not have to constantly poll the SPI bus.

Posted in Allgemein

Connecting SNES Controller to the Steckschwein

Recently, Michael Steil published a blog post about connecting NES and SNES Controller to a 6502-based system
showing how to use NES and SNES controllers on a C64 without the need for any special hardware, by just connecting them to the C64’s user port.

Why not use his approach and adapt it to the Steckschwein? The Steckschwein has a User Port, too, albeit a very different one as the C64. Basically, the Steckschwein-User-Port consists of the complete Port A of the VIA, plus the /RESET and /IRQ lines. Also of course, VCC and GND.

User Port:
      |---------GND
      | |-------PA6  
      | | |-----PA4
      | | | |---PA2 (DATA1)
      | | | | |-PA0 (CLK)
o o X o o o o o
o o X o o o o o
      | | | | |-PA1 (LATCH)
      | | | |---PA3 (DATA2)
      | | |-----PA5
      | | |-----PA7
      |---------VCC

SNES Controller:
 /---------------------
| 7  6  5 | 4  3  2  1 |
 \---------------------

Pin Description
1   +5V
2  CLK
3  LATCH
4  DATA
5  –
6  –
7  GND

snes

Simple adapter to connect one SNES controller

As for the code, we use Michael’s code with only a few modifications respective to the different pinout, and with a handful of optimizations. Having a 65c02 instead of the 6510 in the C64 gives us the STZ instruction, also using PA0 as clock pin takes just an INC instruction followed by STZ to pulse the clock line.

nes_data = via1porta
nes_ddr = via1ddra
; zero page
controller1 = $00 ; 3 bytes
controller2 = $03 ; 3 bytes

bit_clk   = %00000001 ; PA0 : CLK (both controllers)
bit_latch = %00000010 ; PA1 : LATCH (both controllers)
bit_data1 = %00000100 ; PA2 : DATA (controller #1)
bit_data2 = %00001000 ; PA3 : DATA (controller #2)

query_controllers:
    lda #$ff-bit_data1-bit_data2
    sta nes_ddr
    lda #$00
    sta nes_data

    ; pulse latch
    lda #bit_latch
    sta nes_data
    ;lda #0
    ;sta nes_data
    stz nes_data

    ; read 3x 8 bits
    ldx #0
l2: ldy #8
l1: lda nes_data
    cmp #bit_data2
    rol controller2,x
    and #bit_data1
    cmp #bit_data1
    rol controller1,x
    ;lda #bit_clk
    ;sta nes_data
    inc nes_data
    ;lda #0
    ;sta nes_data
    stz nes_data

    dey
    bne l1
    inx
    cpx #3
    bne l2
    rts

Small test program to output a different character for each button:

 

Also, instead of the original Nintendo SNES controller, I use an 8bitdo SN30 Bluetooth controller with the SNES receiver. One could say this is the first time a Bluetooth device has been connected to the Steckschwein.

IMG_5814

Bluetooth SNES receiver from 8bitdo

Up next: Patching our games!

Posted in Allgemein, experiment, joystick

Chuck Peddle, 1937 – 2019

Chuck Peddle, the main designer of the 6502, has passed away on Dec. 15th, 2019.

Peddle was one of the engineers that developed the 6800 at Motorola. He later went to MOS in order to implement his vision of an 8bit CPU for way less than $300, which was Motorola’s price for the 6800.

This idea of a cheap but powerful CPU materialized as the 6501, and finally the 6502.
That very chip, which started the microcomputer revolution, and on which both Marko and myself began to write our first code ever at an early age. BASIC at first, followed by assembly language later.

Learning to code assembly on this small and elegant CPU provided the both of us with profound knowledge and experience about the inner workings of a computer. Knowledge which is still valuable in our respective careers in IT, and also of course when working on our pet project, the Steckschwein. Things would have gone quite different without Chuck Peddle’s elegant little CPU.

Thanks, Chuck!

 

 

Posted in 6502, 65c02, assembly, basic, urschleim, wdc

Steckschwein emulator

Back from the VCFB (Vintage Computer Festival Berlin) 2019 where we had good talks, met interesting people and got new ideas. Especially from Michael Steil who just asked the simple question “How you can develop software for the Steckschwein without an emulator?” Continue reading

Posted in Allgemein

Markos Pacman Talk at VCFb

Marko talked about his Pacman port to the Steckschwein at VCFb. Basically it’s the same talk he did at VCFe in April, but this time, there’s a video. Enjoy!

https://media.ccc.de/v/vcfb19_-_111_-_de_-_201910121615_-_pac-man_auf_dem_steckschwein_-_marko_lauke

Posted in Allgemein

Weird bug in SD card code

Frank van den Hoef, who is adapting the Steckschwein SPI & FAT32 code for his tiny65 machine made me aware of a classic mistake for a 6502 assembly coder to make. Namely in our sdcard driver, when waiting for the “proper” response from the card (which should have bit 7 cleared). The routine handling this looked like this:

1  sd_cmd_response_wait:
2 	ldy #sd_cmd_response_retries
3 @l:	dey
4         beq sd_block_cmd_timeout ; y already 0? then invalid response or timeout
5         jsr spi_r_byte
6         bit #80	; bit 7 clear
7         bne @l  ; no, next byte
8         cmp #$00 ; got cmd response, check if $00 to set z flag accordingly
9         rts
10 sd_block_cmd_timeout:
11        debug "sd_block_cmd_timeout"
12        lda #$1f ; make up error code distinct from possible sd card responses to mark timeout
13        rts

Classic. Obviously, line 6 should read:

          bit #$80 ; bit 7 clear

With that fixed, the sd card init routine now fails, which is odd since we fixed something that was obviously broken.

sd_fail.jpg

Ok, now what? Enabling Marko’s mighty debugging macros, it becomes apparent that the sd card init fails right after sending CMD0 to the card. This command is the first command of the init sequence and is supposed to put the card into “idle mode”. Which the card confirms with an answer of $01. Which is what the init code is expecting, and not getting. Instead, we get $3F, which does not make a lot of sense.

But why did it work before the fix?
Assuming that the card did not change it’s behaviour at the same time I fixed the code, let’s check what actually happened. Before the fix, we were ANDing $3F with 80:

00111111 $3f
01010000 80 (no $, decimal)

In this case, the BNE after the BIT #80 would take the branch to @l, causing the next byte being read, until finally the card responds with $01:

00000001 $01
01010000 80 (no $, decimal)

Now the BNE does not take the branch, and the routine exits.

Now, with the fixed code,  ANDing $3F with $80, to check if bit 7 is clear, which it is:

00111111 $3F
10000000 $80

Alright, exit the loop and return $3f as response of the card. Which isn’t $01, so init failed.

At this point, I have no explanation for the card responding $3F. I assume that the card might be not ready to process commands at this point, so I added code to repeat sending CMD0 until we get $01 or we run out of retries.

 

 

Posted in assembly, code, debugging, murphy, SD-Karte, SPI

Forth Benchmarks

The main motivation to get Forth up and running on the Steckschwein was to participate at The Ultimate Benchmark, in order to crush all 8bit competition to dust.

So the plan was to benchmark the Steckschwein live at the VCFe. Unfortunately, Carsten could not be there, so no Forth benchmark competition this year.
Recently, Carsten presented his benchmark results using TaliForth2, which led us to run the same benchmarks he did and send the results to Carsten, who was kind enough to include them on his site:

https://theultimatebenchmark.org/#sec-7

 

Posted in Allgemein

NVRAM improvements

As real computer is not a real computer without a real time clock, the Steckschwein is no exception here. As we know, we use the Maxim DS1306 RTC, which is a very common RTC which comes as DIP IC and has an SPI interface. And of course it supports battery backup in various configurations. And this is where things get interesting.

Continue reading

Posted in bios, RTC, UART

VCFe 20.0 – over and out

VCFe 20.0 is over, all of a sudden, and we had lots of fun as always.

Many thanks to everyone stopping by at our table, especially to those who voted for us at the poll. We got the fo(u)rth place.

Special thanks to Michael Steil, Andre Fachat und Marco Baye for hanging and hacking with us.

Very special thanks to Andre Fachat for his inpromptu 6502 talk right before our turn. I think we did prepare a real feast for every 6502 afficionado.

Posted in Allgemein

Vintage Computer Festival Europe

Das VCFe steht wieder vor der Tür. Am kommenden Wochenende, also vom 27.4. bis 28.4., findet das Vintage Computer Festival Europe in München statt.

Das diesjährige Motto ist “Pixelritter – Programmieren am Limit”.
Und auch auf dem Steckschwein werden wir Pixel zeigen, teilweise am Limit programmiert.

Veranstaltungsort ist das Kulturzentrum Trudering

Wasserburger Landstrasse 32
81825 München

Posted in Allgemein