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.
- Use BASIC to load in a hex dump of assembled code.
- Use the SCM ROM image to load in a hex dump of assembled code.
- 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.