Compare commits

..

11 Commits

Author SHA1 Message Date
321759f70d fix kp values 2023-12-08 00:14:02 +01:00
ef23ccdc15 update 2023-12-08 00:13:50 +01:00
d5d02ece73 start strf (sprintf) 2023-12-08 00:13:43 +01:00
b92694aeb5 add div by pow(2) 2023-12-08 00:13:29 +01:00
43dc08e5fd fix spi init 2023-12-08 00:13:10 +01:00
e1a8f96ac9 add spi menu 2023-12-08 00:13:02 +01:00
6f11898806 use operators 2023-12-08 00:03:52 +01:00
e5d8c355ba fix addr mode 2023-12-08 00:03:29 +01:00
c4c9b74754 rm struct 2023-12-08 00:02:52 +01:00
81feb2306c rename home 2023-12-08 00:02:22 +01:00
c85d849ee3 add default 2023-12-08 00:01:30 +01:00
12 changed files with 631 additions and 101 deletions

View File

@ -20,6 +20,7 @@ $(BUILD_DIR):
mkdir $@
.PHONY: default test clean
.DEFAULT_GOAL = default
default: $(ROM)
test: ../test.bin

View File

@ -1,13 +1,19 @@
# Project details
## Address Space
### RAM: $0-$7fff
### (EEP)ROM: $8000-$ffff
|Name|From|To|r/w|
|---|---:|---:|:---:|
|ZEROPAGE |$00 |$ff |`rw`|
|STACK |$100 |$1ff |`rw`|
|RAM |$200 |$4fff |`rw`|
|SPI |$5000 |$5fff |`rw`|
|VIA1 |$6000 |$600f |`rw`|
|VIA2 |$7000 |$700f |`rw`|
|ROM |$8000 |$ffff |`r `|
## Naming conventions
leading underscors `_` indicate a "private" label/variable, that is meant for internal use within the module only.
### Labels
- `namespace_fname` for exported subroutines
- `_namespace_fname` or `_namespace_fname_sub` for internal labels
- `(_)namespace_Macroname`
- `(_)NAMESPACE_SYMBOLNAME` for constant symbols
- `(_)NAMESPACE_symbolname` for variables
- **scopes**: snake case
- **subroutines** and **variables**: snake case (`scope::(_)fname_snake_case` or `scope::(_)varname_2`)
- **macros**: camel case (`(_)GoodMacroname`)
- **constants** (eg. in ROM): upper case (`scope::(_)NICE_SYMBOLNAME`)

View File

@ -2,6 +2,7 @@
.code
.macro DEBUG_LED_OFF nr
pha
lda IO1 + IO::RA
.if nr = 0
and #%11111110
@ -9,9 +10,11 @@
and #%11111101
.endif
sta IO1 + IO::RA
pla
.endmacro
.macro DEBUG_LED_ON nr
pha
lda IO1 + IO::RA
.if nr = 0
ora #%00000001
@ -19,6 +22,7 @@
ora #%00000010
.endif
sta IO1 + IO::RA
pla
.endmacro
;********************************************************************************
@ -37,6 +41,7 @@ SPI_IO = IO2
; Printer
.include "programs/printer.s65"
.include "programs/print_slow.s65"
.include "programs/spi-menu.s65"
; Digital Humidity and Temerature Sensor
; .include "dht.s65"
@ -61,8 +66,8 @@ irq:
lda IO2+IO::IFR
sta IRQ_VAR
bbr7 IRQ_VAR,@irq_return
bbs1 IRQ_VAR,@irq_keypad ; check CB1
bbs2 IRQ_VAR,@irq_spi_p ; check SR
bbs1 IRQ_VAR,@irq_keypad ; check CA1
; this should never be reached
jsr lcd_clear
Print str_irq_unknown
@ -75,7 +80,7 @@ irq:
jsr kp_read_on_irq
bra @irq_return
@irq_spi_p:
jsr spi_p_read
jsr spi_p::read
bra @irq_return
; @irq_dht:
; lda IO1 + IO::T1CL ;T1L2 ; clear interrupt flag
@ -89,14 +94,15 @@ irq:
reset:
jsr lcd_init
; TODO debug stuff
stz IO2 + IO::DDRB
lda #$ff
sta IO1 + IO::DDRA
DEBUG_LED_OFF 0
DEBUG_LED_OFF 1
jsr kp_init
jsr spi_p_init
jsr kp_init
; ; INIT DHT
@ -112,48 +118,61 @@ reset:
; enable interrupts
cli
.proc return_home
Print message_menu
.endproc
bra home
.proc home
jsr rb_keypad_read
beq home
Print message_menu
; jsr rb_keypad_read
@loop:
lda _KP_DEBUG_VAL
beq @loop
; TODO debug
sta 0
stz _KP_DEBUG_VAL
lda 0
; beq home
cmp #'A'
jeq printer
cmp #'B'
jeq SPI_CODE_START
; jeq dht_request
jeq spi_menu::spi_menu
cmp #'C'
jeq print_1
cmp #'D'
jeq print_2
cmp #'0'
bne @l1
DEBUG_LED_OFF 0
jmp home
@l1:
cmp #'#'
bne @l2
DEBUG_LED_ON 0
jmp home
@l2:
cmp #'8'
bne @l3
DEBUG_LED_OFF 1
jmp home
@l3:
cmp #'9'
bne @l4
DEBUG_LED_ON 1
jmp home
@l4:
cmp #'1'
beq @debug0_on
cmp #'2'
beq @debug0_off
cmp #'4'
beq @debug1_on
cmp #'5'
beq @debug1_off
cmp #'7'
beq @print_rb
cmp #'*' ; print home menu again if not visible (message 1 and 2 jmp to home)
jeq return_home
jmp home
beq home
jmp @loop
@debug0_off:
DEBUG_LED_OFF 0
jmp @loop
@debug0_on:
DEBUG_LED_ON 0
jmp @loop
@debug1_off:
DEBUG_LED_OFF 1
jmp @loop
@debug1_on:
DEBUG_LED_ON 1
jmp @loop
@print_rb:
jsr lcd_clear
Print str_io2
lda IO2 + IO::RB
jsr lcd_char
lda #'''
jsr lcd_char
jmp @loop
.endproc
@ -181,13 +200,19 @@ message_2:
message_menu:
.byte "<A> Printer "
; .byte "<B> Temperatur "
.byte "<B> Run from SPI"
.byte "<B> SPI-Menu "
.byte "<C> Text 1 "
.asciiz "<D> Text 2 "
str_spi_begin:
.asciiz "---BEGIN SPI---"
str_spi_start:
.asciiz "---START SPI---"
str_irq:
.asciiz "IRQ detected! "
str_irq_unknown:
.asciiz "Unknown IRQ src!"
str_io2:
.asciiz "IO2::RB='"
;********************************************************************************
; reset vector

View File

@ -12,8 +12,8 @@ INCLUDE_MEMCOPY = 1
cpy
loop:
beq @loop_end
lda (ARG0,y)
sta (ARG2,y)
lda (ARG0),y
sta (ARG2),y
dey
bra @loop
loop_end:

View File

@ -33,9 +33,9 @@ INCLUDE_PRINT_SLOW = 1
;********************************************************************************
.macro PrintSlow message,time_cs
jsr lcd_clear
lda #.LOBYTE(message)
lda #<message
sta ARG0
lda #.HIBYTE(message)
lda #>message
sta ARG1
phx
ldx #time_cs

View File

@ -10,7 +10,7 @@ printer:
jsr rb_keypad_read
beq @printer_loop
cmp #'*'
jeq return_home
jeq home
jsr lcd_char
bra @printer_loop
.endif ; guard

92
programs/spi-menu.s65 Normal file
View File

@ -0,0 +1,92 @@
.include "system/spi.s65"
.include "util/string.s65"
.ifndef INCLUDE_SPI_MENU
INCLUDE_SPI_MENU = 1
.scope spi_menu
.bss
trans_bytes: .res 1
trans_pages: .res 1
status: .res 1
status_str: .res 17 ; 16 + null
.code
.proc spi_menu
stz trans_bytes
stz trans_pages
lda #'X'
sta status
@print_menu:
jsr lcd_clear
Print MENU
@print_status:
Strf FMT_STATUS,status_str,trans_pages,trans_bytes,status
lda #(LCD_CMD_SET_ADDRESS | LCD_LINE4)
jsr _lcd_cmd
Print status_str
@loop:
; check if a byte has been transferred
@check_byte:
lda spi_p::bytes_written
cmp trans_bytes
bne @byte_written
@check_page:
lda spi_p::pages_written
cmp trans_pages
bne @page_written
bra @read_keypad
@byte_written:
sta trans_bytes
bra @print_status
@page_written:
sta trans_pages
bra @print_status
@read_keypad:
lda _KP_DEBUG_VAL
beq @loop
; TODO debug
sta 0
stz _KP_DEBUG_VAL
lda 0
cmp #'*'
beq @return_home
cmp #'A'
beq @spi_begin
cmp #'B'
beq @spi_end
cmp #'C'
beq @spi_jump
bra @loop
@spi_begin:
lda #'@'
sta status
jsr spi_p::begin
jmp @print_menu
@spi_end:
lda #'X'
sta status
jsr spi_p::end
jmp @print_menu
@spi_jump:
jsr spi_p::end
jsr lcd_clear
Print START
jmp spi_p::SPI_CODE_START
@return_home:
jsr spi_p::end
jmp home
.endproc
.rodata
MENU:
.byte "A> Beg. Transfer"
.byte "B> Stop Transfer"
.asciiz "C> Jump Home <*"
; .asciiz "0b0p Status: X"
FMT_STATUS: .asciiz "%x%xb Status:%x"
BEGIN: .asciiz "---BEGIN SPI---"
START: .asciiz "---START SPI---"
.endscope
.endif ; guard

View File

@ -102,25 +102,5 @@ INCLUDE_IOW65C22 = 1
IRQ = %10000000
.endenum
; TODO: leave?
; .struct VIA_Pins
; RB .byte ; $0
; RA .byte ; $1
; DDRB .byte ; $2
; DDRA .byte ; $3
; T1CL .byte ; $4
; T1CH .byte ; $5
; T1LL .byte ; $6
; T1LH .byte ; $7
; T2CL .byte ; $8
; T2CH .byte ; $9
; SR .byte ; $a
; ACR .byte ; $b
; PCR .byte ; $c
; IFR .byte ; $d
; IER .byte ; $e
; RANH .byte ; $f ; no handshake
; .endstruct
.endif
.endscope
.endscope ; IO
.endif ; guard

View File

@ -18,6 +18,7 @@ INCLUDE_KEYPAD = 1
.zeropage
_KP_COLUMN: .res 1 ; for storing stuff
_KP_DEBUG_VAL: .res 1 ; for storing the last char DEBUG
.bss ; reserve space or ringbuffer
KP_BUF_SIZE = 10
@ -90,6 +91,8 @@ RBUF_MEM_END = RBUF_MEM_START + KP_BUF_SIZE - 1
rts
@kp_write:
lda _KP_VALUES,x
; TODO use ringbuffer
sta _KP_DEBUG_VAL
jsr rb_keypad_write
rts
.endproc
@ -97,9 +100,9 @@ RBUF_MEM_END = RBUF_MEM_START + KP_BUF_SIZE - 1
.rodata
_KP_VALUES:
.byte "174*"
.byte "396#"
.byte "2850"
.byte "ABCD"
.byte "396#"
.byte "ACBD"
; keypad conencted so that
; row 1 = PA0
; row 2 = PA2

View File

@ -10,30 +10,148 @@
.ifndef INCLUDE_SPI
INCLUDE_SPI = 1
.scope spi_p
.zeropage
code_page: .res 2 ; SPI_CODE_START + pages_written * 256
.bss
SPI_PAGES_WRITTEN: .res 1
SPI_BYTES_WRITTEN: .res 1
bytes_written: .res 1
pages_written: .res 1
.include "util/math.s65"
; spi-transferred code will be placed here
.segment "SPI"
SPI_CODE_START: ; .res $1000
lda $1000
sta $1000
lda $1000
sta $1000
lda $1000
sta $1000
SPI_CODE_START:
lda '$'
jsr lcd_char
lda #<TEST_FMT
sta ARG0
lda #>TEST_FMT
sta ARG1
lda #<TEST_OUT
sta ARG2
lda #>TEST_OUT
sta ARG3
lda #$a9
sta ARG4
lda #$3c
sta ARG5
lda #$10
sta ARG6
jsr strf
Print TEST_OUT
@spiloop:
lda IO1 + IO::RA
lda IO1 + IO::RA
bra SPI_CODE_START
; jsr lcd_clear
; PrintSlow msg,20
; jmp home
; msg:
; .asciiz "YO DAS WAR SPI"
lda IO2 + IO::RA
nop
bra @spiloop
jmp home
; TODO allocate zp memory
fmt_idx = $30
out_idx = $31
;********************************************************************************
; @function Format a string
; @details
; If there is no value to be read, the Pz will be set
; Formats:
; - u: unsigned decimal integer (1 byte)
; - U: unsigned decimal integer (2 bytes)
; @param ARG0-1: Format string address
; @param ARG2-3: Output string address
; @param ARG4+: Additional parameters
; @returns
; @modifies: A, Y
;********************************************************************************
.proc strf
stz out_idx
stz fmt_idx
ldy #0 ; position in string
ldx #0 ; index of format args
@loop:
ldy fmt_idx
lda (ARG0),y
beq @null
cmp #'%'
beq @percent
@normal_char: ; store A in output string
ldy out_idx
sta (ARG2),y
inc fmt_idx
inc out_idx
beq @out_overflow
bra @loop
@percent: ; check for format in next position
iny
sty fmt_idx
lda (ARG0),y
beq @null
; formats
cmp #'x'
beq @format_hex1
bra @normal_char
@format_hex1: ; 1 byte hex -> 2 chars
lda ARG4,x
phx
jsr int8_to_hex_str
ldy out_idx
sta (ARG2),y ; most sig digit
iny
beq @out_overflow
txa
sta (ARG2),y ; least sig digit
iny
beq @out_overflow
sty out_idx
plx
inx ; 1 byte of args handeled
; bra @format_return
@format_return: ; increment fmt_idx to swallow the formating char
inc fmt_idx
bra @loop
@out_overflow: ; store 0 in last position
ldy #$ff
sty out_idx
@store_null:
lda #0
@null: ; store and return
ldy out_idx
sta (ARG2),y
@rts:
rts
.endproc
;********************************************************************************
; @function Convert a 1 byte number into two hex characters
; @param A: Number to convert
; @returns A: Most significant digit
; @returns X: Least significant digit
; @modifies X,Y,A
;********************************************************************************
.proc int8_to_hex_str
pha
and #%00001111
tay
ldx HEX_CHARS,y
pla
div A,16
and #%00001111
tay
lda HEX_CHARS,y
rts
.endproc
HEX_CHARS: .byte "0123456789ABCDEF"
TEST_FMT: .asciiz "%x -> %x -> %x:)"
TEST_OUT: .asciiz "TESTOUT"
; SPI_P = as peripheral
.code
.struct SPI_P_Pins
; VIA addresses
@ -50,12 +168,20 @@ SPI_CODE_START: ; .res $1000
.endstruct
;********************************************************************************
; @function Initialize the IO Adapter for SPI
; @param ARG0-1 Address of the SPI_Pins struct
; @function Begin listening for SPI transfers
; @details
; - initialize variables
; - configure shift register to shift in under external clock
; - enable shift register interrupts
;********************************************************************************
.proc spi_p_init
stz SPI_PAGES_WRITTEN
stz SPI_BYTES_WRITTEN
.proc begin
stz pages_written
stz bytes_written
; store address in zp
lda #<SPI_CODE_START
sta code_page
lda #>SPI_CODE_START
sta code_page + 1
; todo USE MASKS
; set Shift register to shift in under external clock on CB1
lda #IO::ACR::SR_SIN_PHIE
@ -64,21 +190,49 @@ SPI_CODE_START: ; .res $1000
; enable SR interrupts
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
sta SPI_IO + IO::IER
DEBUG_LED_ON 1
; load SR to reset
lda SPI_IO + IO::SR
rts
.endproc
.proc spi_p_read
DEBUG_LED_ON 0
; print received byte
;********************************************************************************
; @function Stop listening for SPI transfers
; @details
; Disables shift register interrupts
;********************************************************************************
.proc end
; disable SR interrupts
lda #IO::IRQ::SR
sta SPI_IO + IO::IER
rts
.endproc
;********************************************************************************
; @function Read a byte from SPI
; @details
; Reads a byte from the shift register and stores it in the SPI code buffer
;********************************************************************************
.proc read
ldx bytes_written
lda SPI_IO + IO::SR
sta SPI_CODE_START,x
inc SPI_BYTES_WRITTEN
jsr lcd_char
inc bytes_written
beq @new_page
rts
@new_page:
inc pages_written
inc code_page + 1
rts
.endproc
;********************************************************************************
; @function Initialize the IO Adapter for SPI
; @param ARG0-1 Address of the SPI_Pins struct
;********************************************************************************
;********************************************************************************
; @function Read bytes
; @param X Number of bytes to send
@ -96,8 +250,8 @@ SPI_CODE_START: ; .res $1000
.proc send_data
.endproc
.endscope
_send_byte:
.endif

106
util/math.s65 Normal file
View File

@ -0,0 +1,106 @@
;********************************************************************************
; @module math
; @type utility
; @details
; Math library
;********************************************************************************
.ifndef INCLUDE_MATH
INCLUDE_MATH = 1
.scope math
;********************************************************************************
; @macro Divide a num (or A) by a factor
; @param num: Memory or A
; @param divider: must be power of 2
;********************************************************************************
.macro div num,divider
.if divider = 2
lsr num
.elseif divider = 4
lsr num
lsr num
.elseif divider = 8
lsr num
lsr num
lsr num
.elseif divider = 16
.repeat 4
lsr num
.endrepeat
.elseif divider = 32
.repeat 5
lsr num
.endrepeat
.elseif divider = 64
.repeat 6
lsr num
.endrepeat
.elseif divider = 128
.repeat 7
lsr num
.endrepeat
.else
.error "div macro: divider % 2 != 0"
.endif
.endmacro
; .macro Amult1 factor
; phx
; ldx factor
; AmultX
; plx
; .endmacro
; https://www.llx.com/Neil/a2/mult.html
; .macro mult num1,num2,result
; lda #0
; ldx #8
; L1:
; lsr num2
; bcc L2
; clc
; adc num1
; L2:
; ror
; ror result
; dex
; bne L1
; sta result+1
; .endmacro
; 00110101
; 10010011
; -------^
; =00110101
; 00110101
; 01001001
; -------^
; =00110101
; 00110101
; 00100100
; -------^
; =00000000
; 00110101
; 00010010
; -------^
; =00000000
; 00110101
; 00001001
; -------^
; 00110101
; 00110101
; 00000100
; -------^
; =00000000
; 00110101
; 00000010
; -------^
; =00000000
; 00110101
; 00000001
; 00110101
.endscope
.endif ; guard

163
util/string.s65 Normal file
View File

@ -0,0 +1,163 @@
;********************************************************************************
; @module string
; @type utility
; @details
; String utility
;********************************************************************************
.ifndef INCLUDE_STRING
INCLUDE_STRING = 1
.scope str
.code
.macro _StrfStoreArg arg
.if (.not .blank(arg))
.if .match(arg, x)
stx ARG4 + @N_ARGS
.elseif .match(arg, y)
sty ARG4 + @N_ARGS
.else
lda arg
sta ARG4 + @N_ARGS
.endif
@N_ARGS .set (@N_ARGS + 1)
.endif
.endmacro
;********************************************************************************
; @function Macro for passing arguments to strf
; @param fmt: Format string address
; @param out: Output string address
; @param x0-x9: Additional parameters
; @warning Addresses as additional paramteres must be passed like this `#<addr,#>addr`
; @modifies: A, X, Y
; @see strf
;********************************************************************************
.macro Strf fmt,out,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
@N_ARGS .set 0 ; @ so that it doesnt break cheap labels
lda #<fmt
sta ARG0
lda #>fmt
sta ARG1
lda #<out
sta ARG2
lda #>out
sta ARG3
_StrfStoreArg x0
_StrfStoreArg x1
_StrfStoreArg x2
_StrfStoreArg x3
_StrfStoreArg x4
_StrfStoreArg x5
_StrfStoreArg x6
_StrfStoreArg x7
jsr strf
.out .sprintf("info: Strf: called with %d arguments", @N_ARGS)
.endmacro
; TODO allocate zp memory
fmt_idx = $30
out_idx = $31
;********************************************************************************
; @function Format a string
; @details
; If there is no value to be read, the Pz will be set
; Formats:
; - x: unsigned hex integer (1 byte) -> 2 chars
; - X: unsigned hex integer (2 byte) -> 4 chars TODO
; - u: unsigned decimal integer (1 byte) TODO
; - U: unsigned decimal integer (2 bytes) TODO
; @param ARG0-1: Format string address
; @param ARG2-3: Output string address
; @param ARG4+: Additional parameters
; @returns
; @modifies: A, X, Y
;********************************************************************************
.proc strf
stz out_idx
stz fmt_idx
ldy #0 ; position in string
ldx #0 ; index of format args
@loop:
ldy fmt_idx
lda (ARG0),y
beq @null
cmp #'%'
beq @percent
@normal_char: ; store A in output string
ldy out_idx
sta (ARG2),y
inc fmt_idx
inc out_idx
beq @out_overflow
bra @loop
@percent: ; check for format in next position
iny
sty fmt_idx
lda (ARG0),y
beq @null
; formats
cmp #'x'
beq @format_hex1
bra @normal_char
@format_hex1: ; 1 byte hex -> 2 chars
lda ARG4,x
phx
jsr int8_to_hex_str
ldy out_idx
sta (ARG2),y ; most sig digit
iny
beq @out_overflow
txa
sta (ARG2),y ; least sig digit
iny
beq @out_overflow
sty out_idx
plx
inx ; 1 byte of args handeled
; bra @format_return
@format_return: ; increment fmt_idx to swallow the formating char
inc fmt_idx
bra @loop
@out_overflow: ; store 0 in last position
ldy #$ff
sty out_idx
@store_null:
lda #0
@null: ; store and return
ldy out_idx
sta (ARG2),y
@rts:
rts
.endproc
;********************************************************************************
; @function Convert a 1 byte number into two hex characters
; @param A: Number to convert
; @returns A: Most significant digit
; @returns X: Least significant digit
; @modifies A,X,Y
;********************************************************************************
.proc int8_to_hex_str
pha
and #%00001111
tay
ldx HEX_CHARS_UPPER,y
pla
div A,16
and #%00001111
tay
lda HEX_CHARS_UPPER,y
rts
.endproc
.rodata
HEX_CHARS_UPPER: .byte "0123456789ABCDEF"
HEX_CHARS_LOWER: .byte "0123456789abcdef"
.export strf
.endscope
.endif ; guard