From d1fc79211e3d0a4d795181b8e54e33bf50a9d97c Mon Sep 17 00:00:00 2001 From: user Date: Wed, 16 Aug 2023 17:53:38 -0500 Subject: [PATCH] Separate bytecode and utilities to separate files --- asm_2/interp.asm | 119 ++++++---------------------------------------- asm_2/program.asm | 114 ++++++++++++++++++++++++++++++++++++++++++++ asm_2/tbc.inc | 15 ------ asm_2/util.inc | 32 +++++++++++++ 4 files changed, 160 insertions(+), 120 deletions(-) create mode 100644 asm_2/program.asm create mode 100644 asm_2/util.inc diff --git a/asm_2/interp.asm b/asm_2/interp.asm index 7949b8f..aac5c40 100644 --- a/asm_2/interp.asm +++ b/asm_2/interp.asm @@ -1,5 +1,5 @@ .include "./m328Pdef.inc" - +.include "./util.inc" .include "./tbc.inc" @@ -105,109 +105,7 @@ ; ------------------------------------------------------------------------------ ; Bytecode - -; Pins for Nokia 5110 LCD -.equ P_LED = 0x4 -.equ P_SCLK = 0x5 -.equ P_MOSI = 0x3 -.equ P_DC = 0x0 -.equ P_RST = 0x1 -.equ P_SCE = 0x2 - - -.cseg - -entry: - ; Set up ports and SPI - T_EXT ext_init - - ; Reset the LCD - T_JAL lcd_reset - - ; Do something with the LCD - T_MOVI A0, 0x21 - T_JAL lcd_cmd - T_MOVI A0, 0xb8 - T_JAL lcd_cmd - T_MOVI A0, 0x04 - T_JAL lcd_cmd - T_MOVI A0, 0x14 - T_JAL lcd_cmd - T_MOVI A0, 0x20 - T_JAL lcd_cmd - T_MOVI A0, 0x09 - T_JAL lcd_cmd - - ; Idle while blinking the LCD backlight - T_MOVS S0, 0 - T_MOVS S1, 1 -led_loop: - T_DOUT S1, P_LED - T_MOVI A0, 20000 - T_JAL delay - - T_DOUT S0, P_LED - T_MOVI A0, 20000 - T_JAL delay - - T_JMP led_loop - - -halt: - T_JMP halt - -delay: - T_SUBS A0, 1 - T_BNZ delay - T_JALR TM, RA - -lcd_reset: - T_MOVS TM, 0 - T_DOUT TM, P_RST - T_MOVS TM, 1 - T_DOUT TM, P_RST - T_JALR TM, RA - -lcd_cmd: - T_MOVI TM, spi_buf - T_STB A0, TM, 0 - - T_MOVS A0, 0 - T_DOUT A0, P_DC - T_DOUT A0, P_SCE - T_SPI TM, 1 - T_MOVS A0, 1 - T_DOUT A0, P_SCE - - T_JALR TM, RA - -ext_init: - movw ZL, r24 - - ; Ports B and C all outputs - ldi r25, 0xff - out DDRB, r25 - out DDRC, r25 - - ; Set up SPI - ldi r25, (0 << SPIE) | \ - (1 << SPE) | \ - (0 << DORD) | \ - (1 << MSTR) | \ - (0 << CPOL) | \ - (0 << CPHA) | \ - (0b01 << SPR0) - out SPCR, r25 - ldi r25, (0 << SPI2X) - out SPSR, r25 - - ijmp - - -.dseg - -spi_buf: - .byte 16 +.include "./program.asm" ; ------------------------------------------------------------------------------ @@ -257,7 +155,13 @@ _zreg_loop: rjmp _zreg_loop _zreg_done: - ; TODO: Set stack pointer + ; Set stack pointer + ldi ZL, LOW(interp_regs + (0xd * 4)) + ldi ZH, HIGH(interp_regs + (0xd * 4)) + ldi r24, LOW(stack) + ldi r25, HIGH(stack) + st Z+, r24 + st Z+, r25 ; Instruction pointer ldi r25, LOW(entry) @@ -1305,6 +1209,11 @@ exec_jmp: .dseg ; 16 general purpose 32-bit "registers" (V0-VF) +.org RAMEND+1 - (16*4) - 16 + +stack: + .byte 16 ; Guard against accidentally clobbering regs + interp_regs: .byte 16*4 diff --git a/asm_2/program.asm b/asm_2/program.asm new file mode 100644 index 0000000..873e307 --- /dev/null +++ b/asm_2/program.asm @@ -0,0 +1,114 @@ +; ------------------------------------------------------------------------------ +; Bytecode program, to be included with interpreter +; ------------------------------------------------------------------------------ + + +; Pins for Nokia 5110 LCD +.equ P_LED = 0x4 +.equ P_SCLK = 0x5 +.equ P_MOSI = 0x3 +.equ P_DC = 0x0 +.equ P_RST = 0x1 +.equ P_SCE = 0x2 + + +.cseg + +entry: + ; Set up ports and SPI + T_EXT ext_init + + ; Reset the LCD + T_JAL lcd_reset + + ; Do something with the LCD + T_MOVI A0, 0x21 + T_JAL lcd_cmd + T_MOVI A0, 0xb8 + T_JAL lcd_cmd + T_MOVI A0, 0x04 + T_JAL lcd_cmd + T_MOVI A0, 0x14 + T_JAL lcd_cmd + T_MOVI A0, 0x20 + T_JAL lcd_cmd + T_MOVI A0, 0x09 + T_JAL lcd_cmd + + ; Idle while blinking the LCD backlight + T_MOVS S0, 0 + T_MOVS S1, 1 +led_loop: + T_DOUT S1, P_LED + T_MOVI A0, 20000 + T_JAL delay + + T_DOUT S0, P_LED + T_MOVI A0, 20000 + T_JAL delay + + T_JMP led_loop + + +halt: + T_JMP halt + +delay: + T_SUBS A0, 1 + T_BNZ delay + T_JALR TM, RA + +lcd_putc: + T_JALR TM, RA + +lcd_gotoxy: + T_JALR TM, RA + +lcd_reset: + T_MOVS TM, 0 + T_DOUT TM, P_RST + T_MOVS TM, 1 + T_DOUT TM, P_RST + T_JALR TM, RA + +lcd_cmd: + T_MOVI TM, spi_buf + T_STB A0, TM, 0 + + T_MOVS A0, 0 + T_DOUT A0, P_DC + T_DOUT A0, P_SCE + T_SPI TM, 1 + T_MOVS A0, 1 + T_DOUT A0, P_SCE + + T_JALR TM, RA + +ext_init: + movw ZL, r24 + + ; Ports B and C all outputs + ldi r25, 0xff + out DDRB, r25 + out DDRC, r25 + + ; Set up SPI + ldi r25, (0 << SPIE) | \ + (1 << SPE) | \ + (0 << DORD) | \ + (1 << MSTR) | \ + (0 << CPOL) | \ + (0 << CPHA) | \ + (0b01 << SPR0) + out SPCR, r25 + ldi r25, (0 << SPI2X) + out SPSR, r25 + + ijmp + + +.dseg + +spi_buf: + .byte 16 + diff --git a/asm_2/tbc.inc b/asm_2/tbc.inc index 3241cbf..7523e17 100644 --- a/asm_2/tbc.inc +++ b/asm_2/tbc.inc @@ -121,21 +121,6 @@ .equ FL = Vf -; ------------------------------------------------------------------------------ -; The assembler packaged with avr_sim has a bug where PC evaluates to PC-1. -; The avra assembler gets it correct, but I can't get the supposedly predefined -; __AVRASM_VERSION__ macro to work, so for now just manually enter the error in -; PC in this symbol. - -; Use with AVRA assembler -; "AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)" -.equ PCERROR = 0 - -; Use with assembler packaged with avr_sim -; "gavrasm Gerd's AVR assembler version 5.4 (C)2022 by DG4FAC" -;.equ PCERROR = -1 - - ; ------------------------------------------------------------------------------ ; Instruction building macros diff --git a/asm_2/util.inc b/asm_2/util.inc new file mode 100644 index 0000000..6bebd1c --- /dev/null +++ b/asm_2/util.inc @@ -0,0 +1,32 @@ +; Include guards are broken in AVRA, so these won't save you. +#ifndef _UTIL_INC_ +#define _UTIL_INC_ + + +; ------------------------------------------------------------------------------ +; The assembler packaged with avr_sim has a bug where PC evaluates to PC-1. +; The avra assembler gets it correct, but I can't get the supposedly predefined +; __AVRASM_VERSION__ macro to work, so for now just manually enter the error in +; PC in this symbol. + +; Use with AVRA assembler +; "AVRA: advanced AVR macro assembler Version 1.3.0 Build 1 (8 May 2010)" +.equ PCERROR = 0 + +; Use with assembler packaged with avr_sim +; "gavrasm Gerd's AVR assembler version 5.4 (C)2022 by DG4FAC" +;.equ PCERROR = -1 + + +; ------------------------------------------------------------------------------ +; The AVR assembler doesn't come with an alignment directive + +.macro ALIGN + .if ((PC-PCERROR) % @0) != 0 + .org (PC-PCERROR) + (@0 - ((PC-PCERROR) % @0)) + .endif +.endmacro + + +.endif /* _UTIL_INC_ */ + -- 2.43.0