.include "./m328Pdef.inc"
-
+.include "./util.inc"
.include "./tbc.inc"
; ------------------------------------------------------------------------------
; 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"
; ------------------------------------------------------------------------------
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)
.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
--- /dev/null
+; ------------------------------------------------------------------------------
+; 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
+
.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
--- /dev/null
+; 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_ */
+