Ben Eater 6502 Kit — Day 17 – Using subroutines to make the Hello World even more compact!
Now we have some RAM we have stack and can use subroutines to make the program more compact. I already used macros and include files so I can just rewrite the macros and both programs, the one with repeated calls and the one with a loop, will benefit from just one change:
.scope LCD
E = %10000000
RW = %01000000
RS = %00100000
.pushseg
.segment "CODE"
;;
; Send Data to LCD display
;
.proc Control
VIA_Out_B
VIA_Out_A #0 ; Clear RS, RW, E bits
VIA_Out_A #LCD::E ; Set E bit to send instruction
VIA_Out_A #0 ; Clear E bit
RTS
.endproc
.macro LCD_Control Instruction
LDA Instruction
JSR LCD::Control
.endmacro
;;
; Send Data to LCD display
;
.proc Data
VIA_Out_B
VIA_Out_A #LCD::RS ; Set RS, Clear RW, E bits
VIA_Out_A #(LCD::RS | LCD::E) ; Set E bit to send data
VIA_Out_A #LCD::RS ; Clear E bit
RTS
.endproc
.macro LCD_Data Character
LDA Character
JSR LCD::Data
.endmacro
.popseg
.endscope
Note how I keep the macros compatible so there are no changes needed to the actual programs. I also use some of the more
advanced features of the CA65 assembler like procedures (.proc
), modules
(.scope
) and segmented relocatable code (.segment
). The result:
Loop | Procedure | Size |
---|---|---|
❌ | ❌ | 342 bytes |
✅ | ❌ | 135 bytes |
❌ | ✅ | 133 bytes |
✅ | ✅ | 84 bytes |
If you wonder about the knife on many of the pictures: It’s the SOG kilowatt. A wire stripping knife for electricians. The best I ever had. I also use it the gently pry the ROM gently from the breadboard.
But no matter how gentle I pry the ROM, it’s already in a very sad state. To anyone doing the project i would suggest to get a high quality DIL socket to protect the ROM. have ordered one, as well as a few spare EEPROMs.
You find the source code for the macros on GitLab: 6502Tutorial — Kit/Library