Compare commits

..

5 Commits

Author SHA1 Message Date
Matthias@Dell
302e49a9f7 add keyboard 2023-12-27 16:56:22 +01:00
Matthias@Dell
3d773390bf wip 2023-12-27 16:56:14 +01:00
Matthias@Dell
27a901c7c0 add test 2023-12-27 16:56:10 +01:00
Matthias@Dell
4d5edeecfe use res for ARGX 2023-12-27 16:55:56 +01:00
Matthias@Dell
fc0dba6c9c add memcopy16 2023-12-27 16:55:40 +01:00
18 changed files with 543 additions and 202 deletions

View File

@ -7,6 +7,7 @@ MEMORY {
VIA1: start = $6000, size = $000f, type = rw, file = "", fill = yes; VIA1: start = $6000, size = $000f, type = rw, file = "", fill = yes;
VIA2: start = $7000, size = $000f, type = rw, file = "", fill = yes; VIA2: start = $7000, size = $000f, type = rw, file = "", fill = yes;
ROM: start = $8000, size = $8000, type = ro, file = %O, fill = yes; ROM: start = $8000, size = $8000, type = ro, file = %O, fill = yes;
TEST: start = $9000, size = $1000, type = rw, file = "../test.bin";
} }
SEGMENTS { SEGMENTS {
VIA1: load = VIA1, type = bss; VIA1: load = VIA1, type = bss;
@ -16,5 +17,6 @@ SEGMENTS {
CODE: load = ROM, type = ro; CODE: load = ROM, type = ro;
RODATA: load = ROM, type = ro; RODATA: load = ROM, type = ro;
RESET_VECTOR: load = ROM, type = ro, start = $FFFA; RESET_VECTOR: load = ROM, type = ro, start = $FFFA;
SPI: load = SPI, type = rw; SPI: load = SPI, type = rw, define = yes;
TEST: load = TEST,type = rw;
} }

View File

@ -65,7 +65,7 @@ irq:
@irq_io1: @irq_io1:
lda IO1 + IO::IFR lda IO1 + IO::IFR
sta irq_via_ifr sta irq_via_ifr
bbr7 irq_via_ifr,@irq_io2 ; skip bbr7 irq_via_ifr,@irq_io1 ; skip
bbs2 irq_via_ifr,@irq_kb1 ; shit reg -> first 8 bits bbs2 irq_via_ifr,@irq_kb1 ; shit reg -> first 8 bits
bbs5 irq_via_ifr,@irq_kb2 ; timer -> last 3 bits bbs5 irq_via_ifr,@irq_kb2 ; timer -> last 3 bits
@irq_io2: @irq_io2:
@ -74,6 +74,7 @@ irq:
bbr7 irq_via_ifr,@irq_return ; skip bbr7 irq_via_ifr,@irq_return ; skip
bbs2 irq_via_ifr,@irq_spi_p ; check SR bbs2 irq_via_ifr,@irq_spi_p ; check SR
bbs1 irq_via_ifr,@irq_keypad ; check CA1 bbs1 irq_via_ifr,@irq_keypad ; check CA1
; this SHOULD never be reached ; this SHOULD never be reached
jsr lcd::clear jsr lcd::clear
Print "Unknown IRQ" Print "Unknown IRQ"
@ -81,23 +82,18 @@ irq:
lda #$ff lda #$ff
sta IO1 + IO::IFR sta IO1 + IO::IFR
sta IO2 + IO::IFR sta IO2 + IO::IFR
rti
; bra @irq_return
@irq_keypad:
lda #<@irq_return
pha
lda #>@irq_return
pha
jmp (spi_p::irq_handler)
@irq_spi_p:
jsr spi_p::read
bra @irq_return bra @irq_return
@irq_keypad:
jsr kp::read_irq
bra @irq_return
@irq_spi_p:
JsrIndirect (spi_p::irq_handler), @irq_return
@irq_kb1: @irq_kb1:
; PrintNC "<30>" Print "$3000"
jsr $3000 jsr $3000
bra @irq_return bra @irq_return
@irq_kb2: @irq_kb2:
; PrintNC "<31>" Print "$3100"
jsr $3100 jsr $3100
bra @irq_return bra @irq_return
; @irq_dht: ; @irq_dht:

View File

@ -3,15 +3,16 @@ INCLUDE_MEMCOPY = 1
.include "system.h65" .include "system.h65"
.export memcopy .export memcopy,memcopy16
.code .code
;******************************************************************************** ;;********************************************************************************
; @function Copy a block of memory to a different address ;; @function Copy a block of memory to a different address
; @param ARG0-1: Source address ;; @param ARG0-1: Source address
; @param ARG2-3: Target address ;; @param ARG2-3: Target address
; @param y: Number of bytes to copy ;; @param Y: Number of bytes to copy
;******************************************************************************** ;; @modifies: A,Y
;;********************************************************************************
.proc memcopy .proc memcopy
cpy #0 cpy #0
beq @rts beq @rts
@ -24,5 +25,41 @@ INCLUDE_MEMCOPY = 1
@rts: @rts:
rts rts
.endproc .endproc
;;********************************************************************************
;; @function Copy a block of memory to a different address
;; @param ARG0-1: Source address
;; @param ARG2-3: Target address
;; @param ARG5-6: Number of bytes to copy (LE)
;; @modifies: A,Y
;;********************************************************************************
.proc memcopy16
lda ARG6
beq @last_page ; no full page
ldy #$ff
bra @copy_byte
@next_page: ; y is 0
lda ARG6
beq @rts ; done
inc ARG1
inc ARG3
dec ARG6
beq @last_page
ldy #$ff
@copy_byte:
lda (ARG0),y
sta (ARG2),y
@dec_y:
dey
beq @next_page
bra @copy_byte
@last_page:
ldy ARG5
dey
bra @copy_byte
@rts:
rts
.endproc
.endif ; guard .endif ; guard

View File

@ -4,43 +4,56 @@
.include "lcd.h65" .include "lcd.h65"
.include "chars.h65" .include "chars.h65"
.import home:absolute .import home:absolute
.import SPI_IO .import CODE_START: absolute
.import __SPI_SIZE__
.export spi_menu .export spi_menu
.bss .bss
trans_bytes: .res 1 trans_bytes: .res 2 ; used to check if screen needs to be updated
trans_pages: .res 1 status_char: .res 1
status: .res 1
status_str: .res 17 ; 16 + null status_str: .res 17 ; 16 + null
.code .code
.proc spi_menu .proc spi_menu
stz trans_bytes stz trans_bytes
stz trans_pages stz trans_bytes+1
lda #'X'
sta status
@print_menu: @print_menu:
jsr lcd::clear jsr lcd::clear
Print MENU Print MENU
@update_status:
lda spi_p::status
cmp #spi_p::STATUS::ERROR
beq @status_ERROR
cmp #spi_p::STATUS::DONE
beq @status_DONE
bra @status_XFER
@status_ERROR:
lda #'E'
bra @update_status_end
@status_DONE:
lda #'O'
bra @update_status_end
@status_XFER:
lda #'X'
@update_status_end:
sta status_char
@print_status: @print_status:
lda #lcd::LINE4 lda #lcd::LINE4
jsr lcd::set_position jsr lcd::set_position
Strf FMT_STATUS,status_str,trans_pages,trans_bytes,status Strf "%x%xb Status: %c",status_str,trans_bytes+1,trans_bytes,status_char
PrintNC status_str PrintNC status_str
@loop: @loop:
; check if a byte has been transferred ; check if a byte has been transferred
@check_byte: @check_received:
lda spi_p::bytes_written lda spi_p::recv_bytes
cmp trans_bytes cmp trans_bytes
beq @read_keypad beq @read_keypad
@byte_written: @byte_received:
sta trans_bytes sta trans_bytes
@check_page: lda spi_p::recv_bytes+1
lda spi_p::pages_written lda trans_bytes+1
cmp trans_pages
beq @read_keypad
@page_written:
sta trans_pages
bra @print_status bra @print_status
@read_keypad: @read_keypad:
lda kp::_DEBUG_VAL lda kp::_DEBUG_VAL
@ -57,22 +70,26 @@ status_str: .res 17 ; 16 + null
beq @spi_end beq @spi_end
cmp #'C' cmp #'C'
beq @spi_jump beq @spi_jump
bra @loop bra @update_status ; any other key
@spi_begin: @spi_begin:
lda #'@' lda #<CODE_START
sta status sta ARG0
jsr spi_p::begin lda #>CODE_START
sta ARG1
lda #<__SPI_SIZE__
sta ARG2
lda #>__SPI_SIZE__
sta ARG3
jsr spi_p::begin_read
jmp @print_menu jmp @print_menu
@spi_end: @spi_end:
lda #'X'
sta status
jsr spi_p::end jsr spi_p::end
jmp @print_menu jmp @print_menu
@spi_jump: @spi_jump:
jsr spi_p::end jsr spi_p::end
jsr lcd::clear jsr lcd::clear
Print START Print START
jmp spi_p::CODE_START jmp CODE_START
@return_home: @return_home:
jsr spi_p::end jsr spi_p::end
jmp home jmp home

View File

@ -3,6 +3,7 @@
.include "lcd.h65" .include "lcd.h65"
.include "math.h65" .include "math.h65"
.include "keypad.h65" .include "keypad.h65"
.include "keyboard.h65"
.include "chars.h65" .include "chars.h65"
.import homeloop:absolute .import homeloop:absolute
.import home:absolute .import home:absolute
@ -12,9 +13,6 @@
.import memcopy .import memcopy
.scope kb
KB_IO = IO1
.endscope
CODE_START: CODE_START:
@ -60,7 +58,7 @@ CODE_START:
; PrintNC $3000 ; PrintNC $3000
jsr kbinit jsr kb::init
lda #'%' lda #'%'
jsr lcd::print_char jsr lcd::print_char
@ -110,7 +108,7 @@ CODE_START:
jsr lcd::print_char jsr lcd::print_char
bra @loop bra @loop
@lB: @lB:
Strf fmt_str, out_str, keycode Strf fmt_str, out_str, kb::keycode
Print out_str Print out_str
bra @loop bra @loop
@lC: @lC:
@ -168,11 +166,11 @@ CODE_START:
; .endproc ; .endproc
.proc irq_on_timer .proc irq_timer_handler
; lda #'<' ; lda #'<'
; jsr lcd::print_char ; jsr lcd::print_char
lda kb::KB_IO + IO::SR lda kb::KB_IO + IO::SR
sta key_read + 1 sta kb::key_read + 1
lda kb::KB_IO + IO::T2CL ; clear interrupt flag lda kb::KB_IO + IO::T2CL ; clear interrupt flag
@ -190,16 +188,16 @@ CODE_START:
ror ror
ror ror
ror ror
lda key_read ; not affecting carry lda kb::key_read ; not affecting carry
rol ; rotate carry into byte, rotate startbit into carry rol ; rotate carry into byte, rotate startbit into carry
; TODO byte is inverted, maybe consider wasting 256 bytes for a bit reverse lookup table? ; TODO byte is inverted, maybe consider wasting 256 bytes for a bit reverse lookup table?
sta keycode sta kb::keycode
Strf fmt_str2, out_str, keycode, key_read, key_read+1 Strf fmt_str2, out_str, kb::keycode, kb::key_read, kb::key_read+1
PrintNC out_str PrintNC out_str
stz key_read stz kb::key_read
stz key_read+1 stz kb::key_read+1
rts rts
.endproc .endproc
@ -209,7 +207,7 @@ CODE_START:
kb_irq1: kb_irq1:
; lda #'!' ; lda #'!'
; jsr lcd::print_char ; jsr lcd::print_char
jsr kb::on_shift_reg_irq jsr kb::irq_shift_reg_handler
; lda #':' ; lda #':'
; jsr lcd::print_char ; jsr lcd::print_char
rts rts
@ -218,7 +216,7 @@ kb_irq2:
; lda #'?' ; lda #'?'
; jsr lcd::print_char ; jsr lcd::print_char
; jsr kb::on_timer_irq ; jsr kb::on_timer_irq
jsr irq_on_timer jsr irq_timer_handler
; lda #';' ; lda #';'
; jsr lcd::print_char ; jsr lcd::print_char
rts rts

View File

@ -1,7 +1,7 @@
;******************************************************************************** ;;********************************************************************************
; @module irq_handler ;; @module irq_handler
; @type system ;; @type system
;******************************************************************************** ;;********************************************************************************
.ifndef INCLUDE_IRQ_HANDLER .ifndef INCLUDE_IRQ_HANDLER
INCLUDE_IRQ_HANDLER = 1 INCLUDE_IRQ_HANDLER = 1

23
system/keyboard.h65 Normal file
View File

@ -0,0 +1,23 @@
;;********************************************************************************
;; @module keyboard
;; @type drive
;; @details:
;; Support for a PS2 Keyboard using the shift register of a 6522 VIA
;; Pressing a key causes 11 bits to be sent: 1 start - 8 keycode - 1 parity - 1 stop
;; The VIA is set up to interrupt after 8 bits have been shifted into the shift register
;; from the external clock pulses of the keyboard (additional hardware required to
;; address the hardware bug of the VIA, where bit get lost when the external clock
;; transition happens during falling edge of PHI2). After reading the shift register,
;; the VIAs T2 timer is set to interrupt after the last 3 bits have been shifted in,
;; which takes about ~230ms.
;;********************************************************************************
.ifndef INCLUDE_KEYBOARD
INCLUDE_KEYBOARD = 1
.include "system.h65"
.scope kb
Import kb,init,irq_shift_reg_handler,irq_timer_handler,keycode,key_read
KB_IO = IO1
.endscope
.endif

106
system/keyboard.s65 Normal file
View File

@ -0,0 +1,106 @@
.include "keyboard.h65"
.include "string.h65"
.include "lcd.h65"
Export kb,init,irq_shift_reg_handler,irq_timer_handler,keycode,key_read
.bss
key_read: .res 2
keycode: .res 1
.code
;;********************************************************************************
;; @function Initialize the PS2 keyboard
;; @modifies: A
;;********************************************************************************
.proc init
; - use the shift register interrupts to read the first 8 bits
; set shift register to shift in under external clock on CB1
; - configure timer for timing the read of the last 3 bits
; timer 2 one shot mode is sufficient, leaves T1 available
lda #(IO::ACR::SR_SIN_PHIE | IO::ACR::T2_IRQ_LOAD)
tsb kb::KB_IO + IO::ACR
; the 3 last bits take about 230us, at @1MHz => wait 230 cycles and then the shift register
; (this could be shorter since the it takes a few cycles after the interrupt)
lda #230
sta kb::KB_IO + IO::T2CL
stz key_read
stz key_read+1
; enable SR interrupts
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
sta kb::KB_IO + IO::IER
; load SR to reset
lda kb::KB_IO + IO::SR
rts
.endproc
;;********************************************************************************
;; @function Read the first 8 bits an
;; @modifies: A
;; @details
;; - read shift register
;; - disable shift register interrupts
;; - reset shift register
;; - enable timer 2 interrupts
;; - start timer 2
;; IO::SR has to be read before the next bit is shifted in, which happens ~75us after the irq
;; at 1MHz, handling this interrupt takes about 50us (without any additional debug code),
;; so it should work
;;********************************************************************************
.proc irq_shift_reg_handler
lda kb::KB_IO + IO::SR
sta key_read
stz kb::KB_IO + IO::SR
; disable SR interrupts
lda #IO::IRQ::SR
sta kb::KB_IO + IO::IER
; enable timer interrupts
lda #(IO::IRQ::IRQ | IO::IRQ::T2)
sta kb::KB_IO + IO::IER
; start timer, low order count already in latch after init
lda #1
sta kb::KB_IO + IO::T2CH
rts
.endproc
;;********************************************************************************
;; @function Read the last 3 bits after after timer 2 is up
;; @modifies: A
;; @details
;; - read shift register
;; - disable timer 2 interrupts
;; - enable shift register interrupts
;; - reset shift register
;;********************************************************************************
.proc irq_timer_handler
lda kb::KB_IO + IO::SR
sta key_read + 1
lda kb::KB_IO + IO::T2CL ; clear interrupt flag
; disable timer interrupts
lda #(IO::IRQ::T2)
sta kb::KB_IO + IO::IER
; enable shift register interrupts
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
sta kb::KB_IO + IO::IER
; reset SR
stz kb::KB_IO + IO::SR
; rotate bit 2 (last bit of keycode) into the carry
lda key_read+1
ror
ror
ror
lda key_read ; not affecting carry
rol ; rotate carry into byte, rotate startbit into carry
; TODO byte is inverted, maybe consider wasting 256 bytes for a bit reverse lookup table?
sta keycode
stz key_read
stz key_read+1
rts
.endproc

View File

@ -22,7 +22,7 @@ charcount: .res 1
lda #$ff ; RB 0-7 output lda #$ff ; RB 0-7 output
sta lcd::LCD_IO+IO::DDRB sta lcd::LCD_IO+IO::DDRB
MaskedWrite lcd::LCD_IO+IO::DDRA, (lcd::RS | lcd::RW | lcd::E), lcd::RA_MASK MaskedWrite lcd::LCD_IO+IO::DDRA, #(lcd::RS | lcd::RW | lcd::E), #lcd::RA_MASK
; lda #(lcd::RS | lcd::RW | lcd::E) ; RA 5-7 output ; lda #(lcd::RS | lcd::RW | lcd::E) ; RA 5-7 output
; sta lcd::LCD_IO+IO::DDRA ; sta lcd::LCD_IO+IO::DDRA

View File

@ -12,7 +12,9 @@ INCLUDE_SPI = 1
.scope spi_p .scope spi_p
SPI_IO = IO2 SPI_IO = IO2
Import spi_p, begin_read, irq_read_byte, begin_write, irq_write_byte, end, recv_bytes, sent_bytes, status Import spi_p, irq_handler, end, status
Import spi_p, begin_read, irq_read_byte, recv_bytes
Import spi_p, begin_write, irq_write_byte, sent_bytes
.enum STATUS .enum STATUS
XFER_SIZEL = %10000000 XFER_SIZEL = %10000000
@ -24,3 +26,17 @@ Import spi_p, begin_read, irq_read_byte, begin_write, irq_write_byte, end, recv_
.endscope .endscope
.endif ; guard .endif ; guard
; .struct SPI_P_Pins
; ; VIA addresses
; DDR_a .word ; address of the data direction register
; R_a .word ; address of the register
; ; pin mask
; SCLK_p .byte ; Serial Clock
; POCI_p .byte ; Peripheral Out / Controller In
; PICO_p .byte ; Peripheral In / Controller Out
; CSB_p .byte ; Chip Select
; ; settings
; CPOL .byte ; Clock Polarity
; CPHA .byte ; Clock Phase
; .endstruct

View File

@ -1,6 +1,9 @@
.include "spi.h65" .include "spi.h65"
.include "system.h65"
Export spi_p, begin_read, irq_read_byte, begin_write, irq_write_byte, end, recv_bytes, sent_bytes, status, spi_irq Export spi_p, irq_handler, end, status
Export spi_p, begin_read, irq_read_byte, recv_bytes
Export spi_p, begin_write, irq_write_byte, sent_bytes
.zeropage .zeropage
buffer_start: .res 2 buffer_start: .res 2
@ -17,19 +20,14 @@ SPI_IO := spi_p::SPI_IO
; spi-transferred code will be placed here ; spi-transferred code will be placed here
; SPI_P = as peripheral ; SPI_P = as peripheral
.code .code
; .struct SPI_P_Pins
; ; VIA addresses ;;********************************************************************************
; DDR_a .word ; address of the data direction register ;; @function Begin listening for SPI transfers
; R_a .word ; address of the register ;;********************************************************************************
; ; pin mask .proc init
; SCLK_p .byte ; Serial Clock lda #spi_p::STATUS::DONE
; POCI_p .byte ; Peripheral Out / Controller In sta status
; PICO_p .byte ; Peripheral In / Controller Out .endproc
; CSB_p .byte ; Chip Select
; ; settings
; CPOL .byte ; Clock Polarity
; CPHA .byte ; Clock Phase
; .endstruct
;;******************************************************************************** ;;********************************************************************************
@ -48,7 +46,7 @@ SPI_IO := spi_p::SPI_IO
.proc begin_read .proc begin_read
stz recv_bytes stz recv_bytes
stz recv_bytes+1 stz recv_bytes+1
lda #%spi_p::STATUS::XFER_SIZEL lda #spi_p::STATUS::XFER_SIZEL
sta status sta status
; store address in zp ; store address in zp
@ -186,7 +184,7 @@ SPI_IO := spi_p::SPI_IO
; write the first byte ; write the first byte
@write_size1: @write_size1:
lda buffer_size lda buffer_size
sta SPI_IO+IO_SR sta SPI_IO+IO::SR
lda #spi_p::STATUS::XFER_SIZEH lda #spi_p::STATUS::XFER_SIZEH
sta status sta status
rts rts
@ -236,11 +234,9 @@ SPI_IO := spi_p::SPI_IO
rts rts
@write_size2: @write_size2:
lda buffer_size+1 lda buffer_size+1
sta SPI_IO+IO_SR sta SPI_IO+IO::SR
lda #spi_p::STATUS::XFER lda #spi_p::STATUS::XFER
sta status sta status
rts rts
.endproc .endproc
.endproc

View File

@ -8,43 +8,57 @@
.ifndef INCLUDE_SYSTEM .ifndef INCLUDE_SYSTEM
INCLUDE_SYSTEM = 1 INCLUDE_SYSTEM = 1
; reserved RAM addresses ; reserved RAM addresses
; 00-0f - free ; 00-0f - free use (Z0-Z15)
; 10-1f - arguments / return values ; 10-1f - arguments / return values (ARG0-ARG15)
; 20-ff - free ; 20-ff - free
; 0100 - 01FF Stack ; 0100 - 01FF Stack
; 0200,0201 keybuffer write/read pointer ; 0200,0201 keybuffer write/read pointer
; 0202-02ff keybuffer ; 0202-02ff keybuffer
; 0300 lcd character counter ; 0300 lcd character counter
; 0400, 0401, 0402 dht status, dht bit, dht_bit_rot
; 0403 value offset
; 0405-04a0 rh high/low, temp high/low, checksum
.include "io_W65C22.h65" .include "io_W65C22.h65"
.include "utility.h65" .include "utility.h65"
.importzp Z0,Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8,Z9,Z10,Z11,Z12,Z13,Z14,Z15
.importzp ARG0,ARG1,ARG2,ARG3,ARG4,ARG5,ARG6,ARG7,ARG8,ARG9,ARG10,ARG11,ARG12,ARG13,ARG14,ARG15
; ARGUMENTS ; Z0 = $0
; a,x,y can also be used ; Z1 = $1
ARG0 = $10 ; Z2 = $2
ARG1 = $11 ; Z3 = $3
ARG2 = $12 ; Z4 = $4
ARG3 = $13 ; Z5 = $5
ARG4 = $14 ; Z6 = $6
ARG5 = $15 ; Z7 = $7
ARG6 = $16 ; Z8 = $8
ARG7 = $17 ; Z9 = $9
ARG8 = $18 ; Z10 = $a
ARG9 = $19 ; Z11 = $b
ARG10 = $1a ; Z12 = $c
ARG11 = $1b ; Z13 = $d
ARG12 = $1c ; Z14 = $e
ARG13 = $1d ; Z15 = $f
ARG14 = $1e ; ; ARGUMENTS
ARG15 = $1f ; ; a,x,y can also be used
; ARG0 = $10
; ARG1 = $11
; ARG2 = $12
; ARG3 = $13
; ARG4 = $14
; ARG5 = $15
; ARG6 = $16
; ARG7 = $17
; ARG8 = $18
; ARG9 = $19
; ARG10 = $1a
; ARG11 = $1b
; ARG12 = $1c
; ARG13 = $1d
; ARG14 = $1e
; ARG15 = $1f
; RETURN VALUE ; RETURN VALUE
; in a
.segment "VIA1" .segment "VIA1"
; IO1: .res 16 ; IO1: .res 16
IO1 = $6000 IO1 = $6000

View File

@ -1,3 +1,42 @@
; .include "system.h65"
.exportzp Z0,Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8,Z9,Z10,Z11,Z12,Z13,Z14,Z15
.exportzp ARG0,ARG1,ARG2,ARG3,ARG4,ARG5,ARG6,ARG7,ARG8,ARG9,ARG10,ARG11,ARG12,ARG13,ARG14,ARG15
.zeropage .zeropage
.org $10 .org $0
.res 16 .assert * = $0, error, "Code not at 0"
Z0: .res 1
Z1: .res 1
Z2: .res 1
Z3: .res 1
Z4: .res 1
Z5: .res 1
Z6: .res 1
Z7: .res 1
Z8: .res 1
Z9: .res 1
Z10: .res 1
Z11: .res 1
Z12: .res 1
Z13: .res 1
Z14: .res 1
Z15: .res 1
; ARGUMENTS
; a,x,y can also be used
.assert * = $10, error, "Code not at 10"
ARG0: .res 1
ARG1: .res 1
ARG2: .res 1
ARG3: .res 1
ARG4: .res 1
ARG5: .res 1
ARG6: .res 1
ARG7: .res 1
ARG8: .res 1
ARG9: .res 1
ARG10: .res 1
ARG11: .res 1
ARG12: .res 1
ARG13: .res 1
ARG14: .res 1
ARG15: .res 1

View File

@ -1,55 +1,8 @@
;.include "system/system.h65" .include "system/system.h65"
;.segment "CODE"
;;********************************************************************************
;; Interrupts
;;********************************************************************************
;nmi:
; rti
;irq:
; .repeat 20
; .endrepeat
; rti
;;********************************************************************************
;; Reset sequence
;;********************************************************************************
;reset:
; sei
; ; setup io2 bank a 1-3
; lda #%11111111
; sta IO1 + IO_DDRA
; sta IO1 + IO_DDRB
;@loop:
; lda #%00000000
; sta IO1 + IO_RA
; .repeat 3
; nop
; .endrepeat
; lda #%11111111
; sta IO1 + IO_RA
; .repeat 15
; nop
; .endrepeat
; lda #%00000000
; sta IO1 + IO_RB
; .repeat 5
; nop
; .endrepeat
; lda #%11111111
; sta IO1 + IO_RB
; .repeat 10
; nop
; .endrepeat
; bra @loop
;;********************************************************************************
;; reset vector
;;********************************************************************************
;.segment "RESET_VECTOR"
; .word nmi
; .word reset
; .word irq
.segment "TEST"
label:
JsrIndirect $2022
.byte 0,1,2,3,4
JsrIndirect {($2022,x)}, label
.byte 5,6,7,8,9

View File

@ -1,6 +1,6 @@
.include "string.h65" .include "string.h65"
.include "math.h65" .include "math.h65"
Export str,hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str Export str,hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str, uint_to_hex_str
.code .code
; ;
@ -130,6 +130,55 @@ Export str,hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str
.endproc .endproc
;;********************************************************************************
;; @function Convert a 1 byte number into two hex characters
;; @param A: Size of the number in bytes
;; @param X: Offset onto ARG4, so that the number starts at `ARG4+x` (BE)
;; @param ARG2-3: Pointer to output string
;; @param Y: Offset onto the string in ARG2-3
;; @returns A: 0
;; @returns X: Offset onto ARG4, past the end of the number
;; @returns Y: Offset onto the the string in ARG2-3, past the end of the number
;; @returns N: Clear if success, else set
;; @modifies A,X,Y
;;********************************************************************************
.proc uint_to_hex_str
@loop:
cmp #0
beq @rts
pha
lda ARG4,x
phx
pha
and #%00001111
tax
lda HEX_CHARS_UPPER,x
sta (ARG2),y
pla
iny
beq @overflow2
div A,16
and #%00001111
tax
lda HEX_CHARS_UPPER,x
sta (ARG2),y
iny
beq @overflow2
plx
pla
inx
beq @overflow
bra @loop
@overflow2:
plx
pla
@overflow:
lda #$ff ; set n
@rts:
rts
.endproc
.rodata .rodata
HEX_CHARS_UPPER: .byte "0123456789ABCDEF" HEX_CHARS_UPPER: .byte "0123456789ABCDEF"
HEX_CHARS_LOWER: .byte "0123456789abcdef" HEX_CHARS_LOWER: .byte "0123456789abcdef"

View File

@ -12,7 +12,8 @@ INCLUDE_STRING = 1
.scope str .scope str
Import str, strf Import str, strf
Import str, hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str Import str, hex_char_to_uint8, hex_str_to_uint
Import str, uint8_to_hex_str, uint_to_hex_str
.macro _StrfStoreArg arg .macro _StrfStoreArg arg
@ -35,7 +36,7 @@ Import str, hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str
;; @param out: Output string address ;; @param out: Output string address
;; @param x0-x9: Additional parameters ;; @param x0-x9: Additional parameters
;; @warning Addresses as additional paramters must be passed like this `#<addr,#>addr` ;; @warning Addresses as additional paramters must be passed like this `#<addr,#>addr`
;; @modifies: A, X, Y ;; @modifies: A, X, Y, ARG4, ARG5
;; @see strf ;; @see strf
;;******************************************************************************** ;;********************************************************************************
.macro Strf fmt,out,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9 .macro Strf fmt,out,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9
@ -73,8 +74,5 @@ Import str, hex_char_to_uint8, hex_str_to_uint, uint8_to_hex_str
; .out .sprintf("info: Strf: called with %d arguments", @N_ARGS) ; .out .sprintf("info: Strf: called with %d arguments", @N_ARGS)
.endmacro .endmacro
; TODO allocate zp memory
fmt_idx = $30
out_idx = $31
.endscope .endscope
.endif ; guard .endif ; guard

View File

@ -1,24 +1,30 @@
.include "string.h65" .include "string.h65"
Export str, strf Export str, strf
.zeropage
fmt_idx: .res 1
out_idx: .res 1
pad_zero:.res 1
digits: .res 1
; addr: .res 2
.code .code
;;******************************************************************************** ;;********************************************************************************
;; @function Format a string ;; @function Format a string
;; @details ;; @details
;; If there is no value to be read, the Pz will be set ;; If there is no value to be read, the Pz will be set
;; Formats: ;; Formats:
;; - x: unsigned hex integer (1 byte) -> 2 chars ;; - x: unsigned hex integer (1 byte) -> 2 chars
;; - X: unsigned hex integer (2 byte) -> 4 chars TODO ;; - X: unsigned hex integer (2 byte) -> 4 chars TODO
;; - u: unsigned decimal integer (1 byte) TODO ;; - u: unsigned decimal integer (1 byte) TODO
;; - U: unsigned decimal integer (2 bytes) TODO ;; - U: unsigned decimal integer (2 bytes) TODO
;; - s: null-terminated string (2 bytes ptr)
;; - c: char
;; @param ARG0-1: Format string address ;; @param ARG0-1: Format string address
;; @param ARG2-3: Output string address ;; @param ARG2-3: Output string address
;; @param ARG4+: Additional parameters ;; @param ARG4+: Additional parameters
;; @returns ;; @returns
;; @modifies: A, X, Y ;; @modifies: A, X, Y, ARG4, ARG5
;;******************************************************************************** ;;********************************************************************************
out_idx := str::out_idx
fmt_idx := str::fmt_idx
.proc strf .proc strf
stz out_idx stz out_idx
stz fmt_idx stz fmt_idx
@ -27,7 +33,7 @@ fmt_idx := str::fmt_idx
@loop: @loop:
ldy fmt_idx ldy fmt_idx
lda (ARG0),y lda (ARG0),y
beq @null jeq @null
cmp #'%' cmp #'%'
beq @percent beq @percent
@normal_char: ; store A in output string @normal_char: ; store A in output string
@ -42,29 +48,88 @@ fmt_idx := str::fmt_idx
sty fmt_idx sty fmt_idx
lda (ARG0),y lda (ARG0),y
beq @null beq @null
; padding
cmp #'0'
beq @percent_zero
stz pad_zero
; formats ; formats
cmp #'x' cmp #'x'
beq @format_hex1 beq @format_hex1
cmp #'X'
beq @format_hex2
cmp #'c'
beq @format_char
bra @normal_char bra @normal_char
@percent_zero:
sta pad_zero
bra @percent
; @percent_num:
; sta pad_zero
; bra @percent
@format_hex1: ; 1 byte hex -> 2 chars @format_hex1: ; 1 byte hex -> 2 chars
lda ARG4,x lda ARG4,x
phx phx
jsr str::uint8_to_hex_str jsr str::uint8_to_hex_str
ldy out_idx ldy out_idx
sta (ARG2),y ; most sig digit sta (ARG2),y ; most sig digit
txa
plx
iny iny
beq @out_overflow beq @out_overflow
txa
sta (ARG2),y ; least sig digit sta (ARG2),y ; least sig digit
iny iny
beq @out_overflow beq @out_overflow
sty out_idx sty out_idx
plx
inx ; 1 byte of args handeled inx ; 1 byte of args handeled
; bra @format_return bra @format_return
@format_return: ; increment fmt_idx to swallow the formating char
inc fmt_idx @format_hex2: ; 2 byte hex -> 4 chars
bra @loop lda #2
ldy out_idx
jsr str::uint_to_hex_str
bmi @out_overflow ; might also be x overflow
sty out_idx
bra @format_return
@format_char: ; 1 char
lda ARG4,x
ldy out_idx
sta (ARG2),y
inc out_idx
beq @out_overflow
inx
bra @format_return
@format_string: ; string
ldy #0
cpx #0
beq @format_string_loop
; store the pointer to the string at arg4-5
@format_string_move_ptr:
lda ARG4,x
sta ARG4
lda ARG5,x
sta ARG5
@format_string_loop:
lda (ARG4),y
beq @format_string_end ; if null
phy
ldy out_idx
sta (ARG2),y
inc out_idx
beq @out_overflow
ply
iny
@format_string_end:
inx
inx
bra @format_return
@format_return: ; increment fmt_idx to account for the formating char
inc fmt_idx ; at this point, out_idx will always overflow first
jmp @loop
@out_overflow: ; store 0 in last position @out_overflow: ; store 0 in last position
ldy #$ff ldy #$ff
sty out_idx sty out_idx

View File

@ -8,25 +8,57 @@ INCLUDE_UTILITY = 1
.feature underline_in_numbers .feature underline_in_numbers
;******************************************************************************** ;;********************************************************************************
; @macro Update a byte in memory using a mask ;; @macro Update a byte in memory using a mask
; @param addr Address of the byte to update ;; @param addr Address of the byte to update
; @param value New value ;; @param value New value
; @param mask Mask of the bits to affect by the new value ;; @param mask Mask of the bits to affect by the new value
; @details ;; @details
; xor #value with addr -> only bits that need to flip are 1 ;; xor #value with addr -> only bits that need to flip are 1
; and result with #mask -> only selected bits that need to flip stay 1 ;; and result with #mask -> only selected bits that need to flip stay 1
; xor result with addr -> flips selected bits ;; xor result with addr -> flips selected bits
;******************************************************************************** ;;********************************************************************************
.macro MaskedWrite addr,value,mask .macro MaskedWrite addr,value,mask
lda #value lda value
eor addr eor addr
and #mask and mask
eor addr eor addr
sta addr sta addr
.endmacro .endmacro
;;********************************************************************************
;; @macro Jump to the subroutine and let the routine return at another location
;; @details
;; By using a indirect address `(addr)` and no ret_val, this macro behaves
;; like an indirect version of jsr.
;; @param addr Target of the jump (can be: `addr`, `(addr)` or `{(addr,x)}`
;; @param ret_addr Return address (optional)
;; @details
;;********************************************************************************
.macro JsrIndirect addr,ret_addr
; -1 because rts increments it
.if .blank(ret_addr)
lda #<(:+ - 1)
pha
lda #>(:+ - 1)
pha
.else
lda #<(ret_addr -1)
pha
lda #>(ret_addr -1)
pha
.endif
jmp addr
.if .blank(ret_addr)
:
.endif
.endmacro
_n_genlabel .set 0 _n_genlabel .set 0
;;******************************************************************************** ;;********************************************************************************
;; @macro Generate a unique label ;; @macro Generate a unique label