RC2024 – Part 16 – Retro Challenge Completed

It’s the 31st of October 2024, so this is the end of the Retro Challenge 2024.

When I signed up for the challenge I wanted to look at adding rotary encoders to the RC2014 platform. This I achieved. I now have several working modules and example applications, including a music player.

I ended up designing and building 4 versions of the rotary encoder module that plugs into the RC2014. They all worked, but they all improved on the previous versions.

One mistake I consistently made on my early boards was not setting a suitable spoke width to my ground plane in EasyEDA. This made the boards hard to solder when connected to ground as the heat from my soldering iron was being wicked away. I finally fixed this on the last two versions of the module.

Spencer Owen (the designer of the RC2014) has been very supportive through the development of the module. He has even assigned port D7 (and A7 as backup) for rotary encoders. This means others can design their own rotary encoder modules and use the same port to avoid clashes with other modules.

I have learnt a lot about Z80 assembly language programming. I am certainly no expert, but I know a lot more than I did a month ago. I even now own a copy of Rodnay Zak’s classic book Programming The Z80.

I built a development environment that allowed me to quickly build and test new programs directly on the RC2014 from my Macbook. When I first started out I was running multiple commands by hand each time, and having to cut and paste intermediate hex code into a terminal. Now, I just run a single build task in Visual Studio Code, and my program is assembled, sent to the RC2014, and run there. I even build a module that allows me to quickly change ROMs on the RC2014.

As well as learning how to decode the input from my rotary encoders, I have learnt how to program the LCD module, and the sound module. These extra modules all work together through my new assembly language programs.

My code has all targeted the RC2014 Classic 2 computer. This was the first RC2014 I bought. However, I did also buy a RC2014 Zed Pro Pride last year. This supports CP/M. I was too nervous to built it initially, but through building my rotary encoder modules I gained confidence and decided to just get on with building it. It took me far longer than I expected. I completed it only a few days before the end of the Retro Challenge. This means I’ve not had time to look at CP/M development yet. This could be next year’s challenge!

All in all, I’ve had a great time designing and building the rotary encoder module and supporting code. I’ve learnt a lot along the way, and I’m really happy with the outcome.

I’m looking forward to doing more with the RC2014 going forward.

RC2024 – Part 15 – Building A Music Player That Uses The Rotary Encoder Module

If you have been following along with my Retro Challenge 2024 posts, you’ll know that I have designed and built a rotary encoder module for the RC2014 computer.

I wanted to put together all my learning and build a music player for the RC2014. This will run using my RC2014 Classic 2 computer. It uses the LCD Driver Module, and the YM2149 Sound Card Module from Z80Kits. In addition, it also uses my ROM board, and of course the Rotary Encoder Module.

The plan is to encode a few of the example tunes provided with the SDK for the YM2149 Sound Card into one program. I will then display the tunes on the LCD, and use the Rotary Encoder Module to allow the different tunes to be selected and played. The ROM board is used to allow me easier access to the SCM ROM to load my assembled program.

I used the example code I wrote earlier for the LCD to display the 3 track titles. I also used the example code for the rotary encoder to move up and down inside the menu.

Moving an arrow in the LCD

I did have to develop some new functionality to move an error up and down in the LCD. I didn’t want to rewrite the whole screen, just select a character and either write an arrow or a space to that location.

I firstly refactored my Z80 code to send a command or data to the LCD screen. This was based on a suggestion I received on social media. The code sends a byte then waits for the LCD to say it’s ready for the next byte.

LCD_R   EQU 218
LCD_D   EQU 219

; Send a command byte to the LCD.
; A - Command in
; A, C registers used.
send_command:
    out (LCD_R),a
    jr lcd_busy

; Send a data byte to the LCD
; A - Byte in
; A, C registers used.
send_data:
    out (LCD_D),a

lcd_busy:
    in a,(LCD_R)
    rlca
    jr c,lcd_busy
    ret

The LCD layout isn’t sequential in the 4×20 character display I am using. Line 3 follows line 1 by 20 characters. Line 2 is then offset by 64 characters, followed by line 4 at 84 characters.

LineOffset
10
264
320
484

The command to move an arrow is the offset from the above table OR’d with $80 (which is the command to set the DDRAM in the LCD module). So to draw an arrow on line 2, and to wipe line’s 3 and 4 I could use the following code.

    ld a,$80|64     ; $80 is the set address command, 64 is the offset.
    call .draw_arrow
    ld a,$80|20     
    call .wipe_arrow
    ld a,$80|84
    call .wipe_arrow

.draw_arrow:
    call send_command
; show an arrow
    ld a,%01111110	; this is the arrow character from the manual
    call send_data
    ret

.wipe_arrow:
    call send_command
; show a space
    ld a,' '
    call send_data
    ret

Sending Debug Information To The Serial Port

While I was developing the code I needed to send some debugging information to the serial port to make sure I knew I was moving through the lines correctly.

The code to play the selected track had a small piece of code that could send to the serial port.

TX:  
    push af
.txbusy     
    in a,($80)          ; read serial status
    bit 1,a             ; check status bit 1
    jr z, .txbusy       ; loop if zero (serial is busy)
    pop af
    out ($81), a        ; transmit the character
    ret

To send a single character I could load the character into register a and then call TX. So to send the character ‘R’ to the serial port, I could do the following.

    ld a,'R'
    call TX

I was keeping track of the current track as either 1, 2, or 3. To send this to the serial port I needed to convert the number into it’s ASCII character code. This turned out to be very simple due to the way the designed of ASCII chose the code for the digits. The ASCII code for $1 is $31, for $2 is $32, and $3 is $33. I just have to OR $30 to the value to convert it to ASCII. So to send the character ‘1’ to the serial port, I could do the following.

    ld a,$1
    or $30
    call TX

Building The Music Player

Thankfully, the work I’ve completed over the course of the Retro Challenge month worked well together. I was able to tweak the existing PTPlayer example code from the sound module to insert my rotary encoder detection routines into the main loop. In the loop I would then move the arrow in the display if necessary, and change the current track if the rotary encoder was pressed.

Here is a video of the music player in action.

The one thing I have found is that if I turn the rotary encoder too fast, it doesn’t always correctly pick up the turn. This is because there is a lot more happening in the loop playing the music so the encoder isn’t being sampled as frequently as in my test code. A possible solution to this could be to look at using interrupts, but I won’t have time to do this before the end of the Retro Challenge.

RC2024 – Part 14 – New Rotary Encoder PCBs

Last week I wrote about refining my Rotary Encoder Module for the RC2014. The updated PCBs have arrived so I have soldered them up.

There are two versions, one that allows any address between 0 and FF to be selected. The other uses only address D7 or A7. These are the addresses that Spencer has reserved for Rotary Encoders on the RC2014.

Apart from the addressing, the main changes were to add pull up resistors to the input pins. This means when a rotary isn’t attached, they are held high. This matches the behaviour when a rotary encoder is attached. To save space on the PCB, I swapped from using separate resistors to using resistor arrays.

They work perfectly.

I do like the look of the two new boards. They look like a real product.

RC2024 – Part 11 – Building And Testing The Updated PCB

The new version of my Rotary Encoder Module PCB arrived yesterday. I couldn’t wait to solder it up.

There were only a few small changes to the first PCB. I covered these when I wrote about building and testing the first PCB.

The Rotary Encoder Module for the RC2014.

The biggest change I wanted to make was to introduce a sensible spoke size to the ground plane. Imagine my disappointment when I still struggled to solder to ground. I went back and looked at my design. The ground plane spoke sizes looked sensible at 0.25mm. Then I realised what I had done. I had placed a ground plane on both the front and back of the PCB. I never changed the spoke sizes on the back. This meant as I was soldering the heat was being wicked away in the large copper ground plane.

I did eventually complete the PCB, and this time I used blue switches for the address port select and for the LEDs. This helps me tell the two different versions of the boards apart.

Plugging it in and running my tests, I was happy to see everything still worked.

Ports

Reading Shelia Dixon’s update on her RetroChallenge for the RC2014, I saw she was using the same port as me.

She mentioned she had spoken to Spencer Owen about the best port to use. I have followed her example and reached out to Spencer. He has kindly reserved port D7 for rotary encoders on the RC2014. There is also A7 as a reserve port in case of clashes. This port can be used by other rotary encoder modules in the future, not just mine.

One thing Spencer mentioned was about making it simpler to set an address port on the board. Rather than having 8 switches that allow any 8 bit port address to be used, a simple jumper to allow either D7 or A7 could be used.

I have taken this on board and decided to design two more versions of the PCB.

The first is essentially the same as my existing board, but with a single ground plane to the rear of the PCB. This also has smaller spoke sizes for ground so should be easier to solder.

The second is a version without the full 8 bit port select switch. Instead I now have a single jumper to allow either D7 or A7 to be selected.

This is what the binary forms of D7 and A7 look like. They are the same apart from bits A4, A5, and A6.

D7 1101 0111
A7 1010 0111

To allow the switching bits A6, A5, and A4 must be inverted when the jumper is swapped. I can just connect the jumper to either GND or +5V, that will allow A6 and A4 to swap, but A5 must always be the inverse of them. This will need to use an inverter.

Rather than adding an extra chip to invert a single value, I decided I could swap the 74HCT32 OR chip to a 74HCT02 NOR gate. I was only using one of the existing OR gates, so swapping that over to a NOR, then running that through another joined NOR gate will give me the same result. I can also use one of the spare gates as an inverter for A5.

When no rotary encoder is connected

At present, when no rotary encoder to port 1 or 2, the output for the Schmitt Trigger is high. When a rotary encoder is present it is low. This could cause a false reading as I check for high in my code.

To solve this, I have added a pull up resistor array between the rotary encoder pins and the Schmitt Trigger. This is 100k resistor connected to +5v.

To test this works I added a 100k resistor array to a breadboard and connected it between the PCB and Rotary Encoder. When the encoder was present it worked as expected. When I removed the encoder, it also worked.

I have added this array to both of the new PCBs.

The final change was to remove the ground hook from the top right of the board. This was only for testing and ground is available on the debug port anyway.

Will the new PCBs arrive in time

There are only 9 days left before the official end of the 2024 RetroChallenge. I have ordered the new PCBs from JLCPCB, but I don’t know if they will arrive in time. I have also had to order some NOR gates from AliExpress, so that will also be over a week to arrive.

Will they arrive before the end of the challenge? I hope so!

The existing PCBs work, I just know they could be better.

RC2024 – Part 10 – Using The Rotary Encoder To Scroll The LCD

So far in this year’s Retro Challenge I’ve designed and built my own Rotary Encoder Module for the RC2014 computer. I’ve also worked out how to control an LCD screen from Z80 assembly language. I now want to combine the two and use the rotary encoder to scroll text on the LCD screen.

I’m building this on the RC2014 Classic 2, so I don’t have access to a file system. I will have to hardcode the text into the program.

I’ve chosen to use the classic hacker song, Puff The Fractal Dragon.

The LCD screen is 20 characters wide, so I will make things easy for myself and ensure every line is 20 characters long. I will pad shorter lines with spaces if necessary.

I’m going to need a pointer to store my current position in the text. I’m calling this puffpointer. I also need to know the start of the text, I’m calling this puff. I’ll also need to know 4 lines before the end of the text. I’m calling this maxpuff. This is calculated in the assembler as the end of the text – 80. The 80 is 4 lines * 20 characters.

I’m using the right turn to scroll down the text, and the left turn to scroll back to the top.

In the right turn I need see if I’m at the end of the text or not. I need to compare puffpointer to maxpuff to see if they match. If they do, I’m at the button so I don’t want to go any further.

The Z80 doesn’t allow us to directly compare 16bit values, so we have to do a bit of a workaround. We can instead clear the a register, then load the values we want to compare into de and hl register pairs. We can then subtract de from hl, and add de back to hl. If they are the same value the Z flag will be set so can test this. In this case, if Z is set we don’t want to do anything else so we can jump back to the main program loop.

    or a
    ld de,maxpuff
    ld hl,(puffpointer)
    sbc hl, de
    add hl, de
    jp z,loop

So if we are get past this point, we are safe to scroll down. We load the pointer to the current line in the text and add 20 to it. This moves us down a line. We then save it, and call our display routine.

    ld hl,(puffpointer)
    ld bc,20
    add hl,bc
    ld (puffpointer),hl
    call show_four_lines

When turning left do a very similar procedure, except we check if puffpointer is at the start of the text. If it isn’t we subtract 20 from puffpointer.

Our final code looks like this.

    OUTPUT LCDScroll.z80

    ORG $9000

ROTARYENCODER EQU $DE
LCD_R   EQU 218
LCD_D   EQU 219

; The input bits from the rotary encoder.
CLK1    EQU %00000001
DT1     EQU %00000010
SW1     EQU %00000100

; show the inital first 4 lines on the LCD.
    call setup_LCD

    ld hl,(puffpointer)      ; the address of the text
    call show_four_lines

loop:
; load the last clk value into register b
    ld  a,(lastclk)
    ld  b,a

; read the input port and store in "input"
    in  a,(ROTARYENCODER)
    ld  (input),a

; now check if the switch on first rotary encoder has been
; pressed. If it has jump to end
    and SW1
    cp  SW1
    jr  z, end

; now see if clk1 matches the lastclk. If it does loop
    ld  a,(input)
    and CLK1
    ld  (lastclk),a
    cp  b
    jr  z, loop

; now work out what direction we are moving.
; if CLK1 is 1 then we can can check DT1 to get the
; direction of rotation. If it's 0, we need to go 
; back to the start of the loop.
    ld  a,(input)          
    and CLK1
    cp  CLK1 
    jr  nz, loop            

; this is where we check DT1. If 1 we are turning left.
    ld  a, (input)
    and DT1
    cp  0 
    jr  nz, left

; we must be turning right, so we need to advance 
; our text. We see if we are at the maximum, and
; if not we advance a line and display.
right:
    or a
    ld de,maxpuff
    ld hl,(puffpointer)
    sbc hl, de
    add hl, de
    jp z,loop

    ld hl,(puffpointer)
    ld bc,20
    add hl,bc
    ld (puffpointer),hl
    call show_four_lines

    jr  loop

; we must be turning left, so we need to go
; back. We see if we are at the start of the 
; text and if not we go back a line and display.
left:
    or a
    ld de,puff
    ld hl,(puffpointer)
    sbc hl, de
    add hl, de
    jp z,loop

    ld hl,(puffpointer)
    ld bc,20
    sub hl,bc
    ld (puffpointer),hl
    call show_four_lines

    jp  loop

; the switch has been pressed, so we clear the output
; and exit.
end:
    call clear_screen

    ret


; Sends a command byte to the LCD.
; A - Command in
; A, C registers used.
send_command:
    out (LCD_R),a
.lcd_busy:
    in a,(LCD_R)
    rlca
    jr c,.lcd_busy
    ret

; Sends a data byte to the LCD
; A - Byte in
; A, C registers used.
send_data:
    out (LCD_D),a
.lcd_busy:
    in a,(LCD_R)
    rlca
    jr c,.lcd_busy
    ret

; setup the LCD screen
setup_LCD:
    ld a,56         ; Function 8 bit, 2 lines, 5x8 dot font
    call send_command
    ld a,12         ; Display on, cursor off, no blink
    call send_command

    call clear_screen

    ret

; clear the LCD screen
clear_screen:
    ld a,1          ; clear the display
    call send_command
    ret

; Display 4 lines of consecutive text on the LCD
; lines are shown 1-20,41-60,21-40,61-80 so we 
; need to jump around to display in order.
; HL - address of text to display on the LCD
; A, B, C, D, E, H, L registers used.
show_four_lines:

; show the first 20 lines
    ld b,20
.line1loop:
    ld a,(hl)
    inc hl
    call send_data
    djnz .line1loop

; jump forward 20 characters, and show
    ld de,20
    add hl,de
    ld b,20
.line2loop:
    ld a,(hl)
    inc hl
    call send_data
    djnz .line2loop    

; jump back 40 characters, and show
    ld de,40
    sub hl,de
    ld b,20
.line3loop:
    ld a,(hl)
    inc hl
    call send_data
    djnz .line3loop

; jump forward 20 characters, and show
    ld de,20
    add hl,de
    ld b,20
.line4loop:
    ld a,(hl)
    inc hl
    call send_data
    djnz .line4loop 

    ret      


; stores the current input from the rotary encode.
input:
    db  0
; stores the last value of CLK1.
lastclk:
    db  0

; stores a pointer to our current position in the text.
puffpointer:
    dw  puff
; the text to show, each line must be 20 bytes long.
puff:
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "                    "
    db "No plain fanfold    "
    db "paper could hold    "
    db "that fractal Puff   "
    db "                    "
    db "He grew so fast no  "
    db "plotting pack could "
    db "shrink him far      "
    db "enough.             "
    db "Compiles and        "
    db "simulations grew so "
    db "quickly tame        "
    db "And swapped out all "
    db "their data space    "
    db "when Puff pushed    "
    db "his stack frame.    "
    db "                    "
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "                    "
    db "Puff, he grew so    "
    db "quickly, while      "
    db "others moved like   "
    db "snails              "
    db "And mini-Puffs      "
    db "would perch         "
    db "themselves on his   "
    db "gigantic tail.      "
    db "All the student     "
    db "hackers loved that  "
    db "fractal Puff        "
    db "But DCS did not     "
    db "like Puff, and      "
    db "finally said,       "
    db "\"Enough!\"           "
    db "                    "
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "                    "
    db "Puff used more      "
    db "resources than DCS  "
    db "could spare.        "
    db "The operator killed "
    db "Puff's job -- he    "
    db "didn't seem to care."
    db "A gloom fell on the "
    db "hackers; it seemed  "
    db "to be the end,      "
    db "But Puff trapped    "
    db "the exception, and  "
    db "grew from naught    "
    db "again!              "
    db "                    "
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
    db "Puff the fractal    "
    db "dragon was written  "
    db "in C,               "
    db "And frolicked while "
    db "processes switched  "
    db "in mainframe memory."
puffend:
maxpuff EQU puffend - 80

Here’s a video of the rotary encoder in action scrolling through the text of Puff The Fractal Dragon.

RC2024 – Part 8 – Refining The Rotary Encoder Z80 Assembly Language Program

In my last Retro Challenge post I was able to read the rotary encoder from Z80 assembly language on my RC2014 Classic 2 computer. However, the code was too sensitive. This was because I was looking for all changes to the CLK1 signal, instead of just the transition from low to high.

This turned out to be simpler than I though, and I was able to remove a large(ish) block of code. The old code I had a check_case1 and check_case2 that can be removed and replaced with the following.

; now work out what direction we are moving.
; if CLK1 is 1 then we can can check DT1 to get the
; direction of rotation. If it's 0, we need to go 
; back to the start of the loop.
    ld  a,(input)          
    and CLK1
    cp  CLK1 
    jr  nz, loop            

; this is where we check DT1. If 1 we are turning left.
    ld  a, (input)
    and DT1
    cp  0 
    jr  nz, left

In the first part I’m loading the input data from memory. I then mask off everything but the CLK1 bit and see if it is high. If it’s not, I go back to the start of the program.

In the second part I do the same for the DT1 bit, but check if it’s low. If high, I jump to the turning left code. If it is low, I carry on the to the turning right code, which directly follows.

The completed code looks like this.

    OUTPUT rotaryencoderledcontrol2.z80

; On the RC2014 Classic 2 running from BASIC the Z80
; code runs from address $9000. 
    ORG $9000

; The input and output ports to use.
INPUT_PORT  EQU $DE
OUTPUT_PORT EQU $03

; The input bits from the rotary encoder.
CLK1    EQU %00000001
DT1     EQU %00000010
SW1     EQU %00000100

; show the inital led value
    ld  a,(output)
    out (OUTPUT_PORT),a

loop:
; load the last clk value into register b
    ld  a,(lastclk)
    ld  b,a

; read the input port and store in "input"
    in  a,(INPUT_PORT)
    ld  (input),a

; now check if the switch on first rotary encode has been
; pressed. If it has jump to end
    and SW1
    cp  SW1
    jr  z, end

; now see if clk1 matches the lastclk. If it does loop
    ld  a,(input)
    and CLK1
    ld  (lastclk),a
    cp  b
    jr  z, loop

; now work out what direction we are moving.
; if CLK1 is 1 then we can can check DT1 to get the
; direction of rotation. If it's 0, we need to go 
; back to the start of the loop.
    ld  a,(input)          
    and CLK1
    cp  CLK1 
    jr  nz, loop            

; this is where we check DT1. If 1 we are turning left.
    ld  a, (input)
    and DT1
    cp  0 
    jr  nz, left

; we must be turning right so rotate the output to the right
; and store it before going back to the start of the loop.
right:
    ld  a,(output)
    rrca
    out (OUTPUT_PORT),a
    ld  (output),a

    jr  loop

; we must be turning left so rotate the output to the left
; and store it before going back to the start of the loop.
left:
    ld  a,(output)
    rlca
    out (OUTPUT_PORT),a
    ld  (output),a

    jp  loop

; the switch has been pressed, so we clear the output
; and exit.
end:
    ld  a,0
    out (OUTPUT_PORT),a

    ret

input:
    db  0
output:
    db  %00000001
lastclk:
    db  0

I also swapped the JP instructions to JR instructions because the code is small. This saves a few bytes.

This is only a small refinement, but it makes the Rotary Encoder Module so much more usable.

RC2024 – Part 7 – Reading The Rotary Encoder Using Z80 Assembly Language

Previously as part of this year’s Retro Challenge, I learnt how to build and run Z80 assembly language programs on my RC2014 Classic 2 computer.

I now want to recreate the BASIC program that moves LEDs using Z80 Assembly Language.

I am using IO address $DE for my Rotary Encoder module. I also have the Digital I/O module using IO address $03. I can define these as constants so I can easily change them if necessary.

I need to store values for the input, the output, and the last value of the CLK pin. The input will just be the value from the IN operation. The output value will be the byte we want to show on the LED output. I will set this to be %00000001 initially. When I turn the encoder, I want this to shift to either the left or right. If it reaches the edge, I want it to wrap. The Z80 operations RLCA and RRCA will do this for me.

To check if a bit is high or low, I can use an AND operation to mask out out other values. For example, to check if the SW1 is being pressed, I can do the following.

    SW1         EQU %00000100
    INPUT_PORT  EQU $DE

    in a,(INPUT_PORT)
    and SW1
    cp SW1
    jp z, end    ; jump to end if SW is high.

I can repeat this logic to check the values of CLK1 and DT1.

This is the code I have come up with.

    OUTPUT rotaryencoderledcontrol.z80

; On the RC2014 Classic 2 running from BASIC the Z80
; code runs from address $9000. 
    ORG $9000

; The input and output ports to use.
INPUT_PORT  EQU $DE
OUTPUT_PORT EQU $03

; The input bits from the rotary encoder.
CLK1    EQU %00000001
DT1     EQU %00000010
SW1     EQU %00000100

; show the inital led value
    ld a,(output)
    out (OUTPUT_PORT),a

loop:
; load the last clk value into register b
    ld a,(lastclk)
    ld b,a

; read the input port and store in "input"
    in a,(INPUT_PORT)
    ld (input),a

; now check if the switch on first rotary encode has been
; pressed. If it has jump to end
    and SW1
    cp SW1
    jp z, end

; now see if clk1 matches the lastclk. If it does loop
    ld a,(input)
    and CLK1
    ld (lastclk),a
    cp b
    jp z, loop

; now work out what direction we are moving
check_case1:
    ld  a,(input)          
    and CLK1
    cp  CLK1             
    jr  nz, check_case2

    ld  a,(input)         
    and DT1
    cp  DT1
    jr  z, left     ; if both CLK and DT are high then left

check_case2:
    ld  a, (input)
    and CLK1
    cp  0 
    jr  nz, right

    ld  a, (input)
    and DT1
    cp  0 
    jr  nz, right  

; we must be turning left so rotate the output to the left
; and store it before going back to the start of the loop.
left:
    ld a,(output)
    rlca
    out (OUTPUT_PORT),a
    ld (output),a

    jp loop

; we must be turning right so rotate the output to the right
; and store it before going back to the start of the loop.
right:
    ld a,(output)
    rrca
    out (OUTPUT_PORT),a
    ld (output),a

    jp loop

; the switch has been pressed, so we clear the output
; and exit.
end:
    ld a,0
    out (OUTPUT_PORT),a

    ret

input:
    db  0
output:
    db  %00000001
lastclk:
    db  0

The LED successfully moves left and right depending on how I turn the rotary encoder. However, because I am checking in both high and low states of CLK1, it is moving two steps per turn. This will be too senstive to use in an application, so my next job is to change this to check once per turn.

RC2024 – Part 6 – Getting Z80 Assembly Language Programs On To The RC2014 Classic 2

As part of this year’s Retro Challenge, I am building a rotary encoder module for the RC2014 computer.

I have built a custom PCB, and I can use it from BASIC. However, I would also like to be able to use it from Z80 machine code.

To run Z80 machine code on my RC2014 Classic 2 I have a few options.

  1. Use BASIC to load in a hex dump of assembled code.
  2. Use the SCM ROM image to load in a hex dump of assembled code.
  3. Burn my assembled code into a ROM and insert that into the RC2014.

I have designed a new ROM PCB to help me do options 2 and 3 in the future. For now, I will use BASIC to load in assembled code and run it.

Before I can load assembled code, I need to write and assemble it.

I am going to do this on an Apple Macbook Pro. I’m going to need an assembler, and something to create hex dumps from the assembled code.

I am going to use SJASMPLUS as my Z80 assembler. On a Mac this needs to be built from the source code. In a terminal window the following should work.

make clean
make
sudo make install

To create the hex files, I am going to use the z88dk-appmake command from z88dk. z88dk is also provides an assembler and a C compiler that can build applications for the RC2014. I’m not going to use these at the moment. There are installation instructions and a binary that can easily be installed on a Mac.

You can use any text editor you want, but I’m going to be using Visual Studio Code. I’m also using the Z80 Assembly extension for syntax highlighting.

I’m going to write a simple Z80 assembly language program to read the the input from the rotary encoder and show it on the Digital I/O module’s LEDs. It’s going to exit when the rotary encoder’s switch is pressed.

The Rotary Encoder module is on input address $DE. The Digital I/O module is on input address $03.

    OUTPUT rotaryencodertest.z80

; On the RC2014 Classic 2 running from BASIC the Z80
; code runs from address $9000. 
    ORG $9000

; The input and output ports to use.
INPUT_PORT  EQU $DE
OUTPUT_PORT EQU $03

; The input bits from the rotary encoder.
CLK1    EQU $1
DT1     EQU $2
SW1     EQU $4


loop:
; read the input port
    in a,(INPUT_PORT)
; send the input directly to the output port
    out (OUTPUT_PORT),a

; now check if the switch on first rotary encode has been
; pressed. If it hasn't, loop back.
    and SW1
    cp SW1
    jp nz, loop

; the switch has been pressed, so we clear the output
; and exit.
    ld a,0
    out (OUTPUT_PORT),a

    ret

I’ve saved this as rotaryencodertest.s.

To assemble to code I need to use the following line in a terminal…

sjasmplus rotaryencodertest.s

To convert the output to intel format hex, I need to use the following line in a terminal…

z88dk-appmake +hex --org 0x9000 -b rotaryencodertest.z80

I should now I have a file called rotaryencodertest.ihx.

To load this onto the RC2014 Classic 2, I can use the example hexload.bas program. I’ll include the full code here.

new
clear
10 REM Created by Filippo Bergamasco,
11 REM and modified by DaveP for the RC2014
12 REM Adapted for z88dk by feilipu
20 REM Version 1.0
30 Print "Loading Data"
40 let mb=&H8900
50 print "Start Address: ";hex$(mb)
60 REM Go to READ Subroutine.
70 GOSUB 1000
80 print "End Address:   ";hex$(mb-1)

90 REM Change USR(0) Pointer for HexLoad
100 GOSUB 1100

110 REM RUN THE HEXLOAD CODE!
120 print usr(0)

130 REM Change USR(0) Pointer to 0x9000
140 GOSUB 1200

150 REM RUN THE PROGRAMME CODE!
160 print usr(0)
170 END 

1000 REM Routine to load Data
1010 REM Needs var mb set to start location
1020 read a
1030 if a>255 then RETURN
1040 rem print HEX$(mb),a
1050 poke mb, a
1060 let mb=mb+1
1070 goto 1020

1100 REM Location of usr address &H8049
1110 print "USR(0) -> HexLoad"
1120 let mb=&H8049 
1130 doke mb, &H8900
1140 RETURN 

1200 REM Location of usr address &H8049
1210 print "USR(0) -> 0x9000, z88dk default"
1220 let mb=&H8049
1230 doke mb, &H9000
1240 RETURN

9010 data 33,116,137,205,109,137,215,254,58,32,251,14
9040 data 0,205,83,137,71,205,83,137,87,205,83,137
9070 data 95,205,83,137,254,1,40,23,254,0,32,33
9100 data 205,83,137,18,19,16,249,205,83,137,121,183
9130 data 32,26,62,35,207,24,207,205,83,137,121,183
9160 data 32,14,33,206,137,205,109,137,201,33,172,137
9190 data 205,109,137,201,33,189,137,205,109,137,201,205
9220 data 100,137,7,7,7,7,111,205,100,137,181,111
9250 data 129,79,125,201,215,214,48,254,10,216,214,7
9280 data 201,126,183,200,207,35,24,249,72,69,88,32
9310 data 76,79,65,68,69,82,32,98,121,32,70,105
9340 data 108,105,112,112,111,32,66,101,114,103,97,109
9370 data 97,115,99,111,32,38,32,102,101,105,108,105
9400 data 112,117,32,102,111,114,32,122,56,56,100,107
9430 data 10,13,58,0,10,13,73,110,118,97,108,105
9460 data 100,32,84,121,112,101,10,13,0,10,13,66
9490 data 97,100,32,67,104,101,99,107,115,117,109,10
9520 data 13,0,10,13,68,111,110,101,10,13,0,0
9550 data 999
9999 END
run

Paste this into a terminal connected to the RC2014 and it will prompt you to enter hex. Cut and paste the rotaryencodertext.ihx and it should load and execute.

Now turning the rotary encoder will show the binary input on the output LEDs. Pressing the switch on the rotary encoder attached to port 1 will return you to BASIC.

This is it running. Ignore the poor soldering on the Digital I/O board. I did that a few years ago and (I think) I have improved since then.

RC2024 – Part 5 – Building And Testing The PCB

My prototype PCBs from JLCPCB has arrived!

Rotary Encoder for RC2014

The parts I needed to solder onto the board have also arrived from AliExpress and CPC Farnell.

I found one very annoying mistake try to solder up the board. I had forgotten to add a sensible spoke width to my copper ground plane. This means the ground plane is soaking up the heat from my soldering iron anytime I try to solder any pin connected to ground. I was able to solder up the board, but those pins took between 10 and 20 seconds to solder. I’m not confident the connection will last.

In the next iteration of the board I will set the spoke width in the EasyEDA CopperArea properties to 0.3mm. This should still give a good ground connection, but also be easy to solder.

I’m not happy with the position of some of the text on the board. Once wired up, pins obscure the Port 1 and Port 2 text. The debug port is also obscured. I will move these so they are visible when connected.

The debug port is a bit too high so the pins are clear of the top of the board. I will move them down.

I also realised that Spencer calls all his PCBs “modules”, so I will call mine “Rotary Encoder Module”.

Rotary Encoder for RC2014

It was with great excitement I plugged the module into my RC2014 and turned it on. The LEDs for Port 1 were all off, but the LEDs for Port 2 were all on. That didn’t seem right, but then I realised I had only plugged a rotary encoder into Port 1. As Port 2 is returning logic 0, the 74HCT14 was inverting this to logic 1 so the LEDs lit. So it was working as expected. Adding another rotary encoder to Port 2 fixed that.

The address was set to 0 on the address switches. This meant I could use the same BASIC program I used before to test the rotary encoders.

Turning the encoders the LEDs were flashing in the expected order. The BASIC program was returning “Left” and “Right” depending how I turned the encoder. It was working!

I decided to test the address switches and so set it them to 01111011, which is hex DE, and decimal 222.

I modified the BASIC program to read address DE instead of 0.

30 LET IN = INP(&HDE)

Running the BASIC program, the module now responds to address DE as expected.

One final test was to plug in my RC2014 Digital IO module. This is set to address 3. I modified the BASIC program so the OUT statement used address 3.

40 OUT 3,2^COUNTER

I can now use my rotary encoder module to successfully move the LEDs on the Digital I/O module.

Despite the issues with soldering to the ground plane, my module works as I hoped. I am going to tweak the layout and order replacement PCBs from JLCPCB.