2022-05-27 15:52:45 +00:00
2021-10-21 07:59:53 +00:00
.feature string_escapes ; Allow c-style string escapes when using ca65
.feature org_per_seg
2022-05-27 15:52:45 +00:00
DEBUG = 1
PORTB = $ 6 0 0 0 ; PB0: SCK/SCL, PB1: RF CS, PB2: RF CE, PB3: SDA, PB4,PB5: MISO ,PB6: PS/2 Clock In, PB7: MOSI/T1 Out (Tape drive output)
2021-10-21 07:59:53 +00:00
PORTA = $ 6 0 0 1
DDRB = $ 6 0 0 2
DDRA = $ 6 0 0 3
T1 C L = $ 6 0 0 4
T1 C H = $ 6 0 0 5
T1 L L = $ 6 0 0 6
T1 L H = $ 6 0 0 7
T2 C L = $ 6 0 0 8
T2 C H = $ 6 0 0 9
SR1 = $ 6 0 0 A
2022-05-27 15:52:45 +00:00
ACR = $ 6 0 0 B ; [7] PB.7 T1 OUT, [6] T1 mode , [5] T2, [4:2] Shift register control, [1] PB Latch enable, [0] PA Latch Enable
2022-04-03 12:13:47 +00:00
PCR = $ 6 0 0 C ; [7:5] CB2 Control, [4] CBl Control, [3:1] CA2 Control, [0] CAl Control
IFR = $ 6 0 0 D ; [7:0] IRQ Tl T2 CBl CB2 SR CA1 CA2
IER = $ 6 0 0 E ; [7:0] S/C Tl T2 CBl CB2 SR CA1 CA2
2021-10-21 07:59:53 +00:00
PORTANHS = $ 6 0 0 F
2022-04-03 12:13:47 +00:00
CTRL = $ 5 0 0 0
2021-10-21 07:59:53 +00:00
2022-05-27 15:52:45 +00:00
;If using port A instead of the shift register then change this to PORTA
KEYBOARD = $ 0 C
TIMEOUT = 7 9 9 8 ; Should be around 4ms
2021-10-21 07:59:53 +00:00
kb_ w p t r = $ 0 0 0 0
kb_ r p t r = $ 0 0 0 1
kb_ f l a g s = $ 0 0 0 2
2022-04-03 12:13:47 +00:00
kb_ l a s t = $ 0 0 0 4
2022-05-27 15:52:45 +00:00
kbshift = $ 0 7
kbbit = $ 0 9 ; Used to count bits
2021-10-21 07:59:53 +00:00
RELEASE = % 0 0 0 0 0 0 0 1
SHIFT = % 0 0 0 0 0 0 1 0
2022-04-03 12:13:47 +00:00
ECODE = % 0 0 0 0 0 1 0 0
CRSR = % 0 1 0 0 0 0 0 0
CRSRF = % 1 0 0 0 0 0 0 0
2022-05-27 15:52:45 +00:00
KTIMEOUT= % 0 0 1 0 0 0 0 0
2021-10-21 07:59:53 +00:00
kb_ b u f f e r = $ 0 2 0 0 ; 256-byte kb buffer 0200-02ff
2022-04-03 12:13:47 +00:00
;Custom keyboard mappings
DN_ A R R _ K E Y = $ F 3
UP_ A R R _ K E Y = $ F 4
PGUP_ K E Y = $ F 6
PGDN_ K E Y = $ F 5
L_ A R R _ K E Y = $ F 2
R_ A R R _ K E Y = $ F 1
HOME_ K E Y = $ F 0
ESC_ K E Y = $ 1 b
2022-05-27 15:52:45 +00:00
DEBUGP = $ 1 9
DEBUGPH = $ 1 A
2022-04-03 12:13:47 +00:00
CRSRT = $ 1 B
CRSRCHR = $ 1 C
RF_ E R R = $ 1 D
ERRS = $ 1 F
2022-05-27 15:52:45 +00:00
TMPL = $ 3 C
TMPH = $ 3 D
LASTKB = $ 3 E
LASTKBH = $ 3 F
2021-10-21 07:59:53 +00:00
MILLIS = $ 4 0
MILLISH = $ 4 1
HXH = $ 4 4
MONCNT = $ 4 6
2022-04-03 12:13:47 +00:00
SLB = $ 4 7 ; Size low byte
SHB = $ 4 8
SVP = $ 4 2 ; Save pointer
SVPH = $ 4 3
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
TMP = $ 5 0 ;
TMP2 = $ 5 1 ;
2021-10-21 07:59:53 +00:00
CRSRPNT = $ 5 2
CRSRPNT2 = C R S R P N T + 1
2022-04-03 12:13:47 +00:00
2021-10-21 07:59:53 +00:00
SCREENSTARTH = $ 2 0
2022-05-27 15:52:45 +00:00
SCREENSTARTL = $ 4 D
LINESTART = 1 3
LINEEND = 6 1
NUMLINES = 2 9
2021-10-21 07:59:53 +00:00
MONH = $ 5 9
MONL = $ 5 8
ABUF = $ 5 A
;to $61
2022-04-03 12:13:47 +00:00
outb = $ 5 6
inb = $ 5 7
RF_ S T S = $ 6 8
; CE = $69
MSGBUF = $ 9 0
;+32bytes == To $AF
2021-10-21 07:59:53 +00:00
uservia = P O R T B
2022-05-27 15:52:45 +00:00
mosi = % 1 0 0 0 0 0 0 0
miso = % 0 0 1 0 0 0 0 0
SDA = 8 ; PB3 bitmask
SDA_ I N V = $ F 7
SCL = 1 ; PB0 bitmask
SCL_ I N V = $ F E
2021-10-21 07:59:53 +00:00
.segment " RODATA"
2022-04-03 12:13:47 +00:00
;.org $8000 ; Not strictly needed with CA65 but shows correct address in listing.txt
.list on ; Does this work?
2021-10-21 07:59:53 +00:00
nmi :
reset :
cld ; Because you never know
;CLEAR RAM
2022-04-03 12:13:47 +00:00
sei
2021-10-21 07:59:53 +00:00
ldx #$ 0
clearram :
lda #0
sta $ 0 0 ,x
inx
bne c l e a r r a m
2022-04-03 12:13:47 +00:00
;Assumes x and A are 0 from above
clearstack :
sta $ 0 1 0 0 ,x
inx
bne c l e a r s t a c k
2022-05-27 15:52:45 +00:00
lda #8
sta k b b i t
lda #10
sta k b s h i f t
2022-04-03 12:13:47 +00:00
noclear : ;Soft reset point - BRK
2022-05-27 15:52:45 +00:00
lda #$ 03 ;
2022-04-03 12:13:47 +00:00
sta C T R L ; Enable video, bank 0
2022-05-27 15:52:45 +00:00
;debug, ca1 disabled
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
jsr c l r s c n
2021-10-21 07:59:53 +00:00
lda #S C R E E N S T A R T L
sta C R S R P N T
lda #S C R E E N S T A R T H
sta C R S R P N T 2
tax
txs
2022-05-27 15:52:45 +00:00
;lda #2
;sta PORTB ; Set SPI CS high
lda #% 10010111 ; Port B DDR for SPI
2021-10-21 07:59:53 +00:00
sta D D R B
2022-05-27 15:52:45 +00:00
lda #$ 0 c
sta $ 1 9
2021-10-21 07:59:53 +00:00
2022-05-27 15:52:45 +00:00
lda #0
2022-04-03 12:13:47 +00:00
sta D D R A ; Port A all inputs for keyboard
2021-10-21 07:59:53 +00:00
sta k b _ r p t r ; Init keyboard pointers before enabling interrupts
sta k b _ w p t r
2022-04-03 12:13:47 +00:00
sta R F _ E R R ; Reset RF error
2022-05-27 15:52:45 +00:00
sta $ 1 A
;cli
lda #% 11000000 ; Set T1
sta I E R
LDA #% 01000000
STA A C R ; T1 continuous, PB7 disabled
;cli ; Enable interrupts Let's do this when KB is ready
LDA #< T I M E O U T
STA T 1 C L ; Set low byte of timer1 counter
LDA #> T I M E O U T
STA T 1 C H ; Set high byte of timer1 counter
lda #01
sta $ 5 0 0 0
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
ldx #$ 10 ; Read first tx addr byte = should be default, if not then no module connected
jsr r w _ r e g
2022-05-27 15:52:45 +00:00
sta $ 0 f
2022-04-03 12:13:47 +00:00
cmp #$ E 7
2022-05-27 15:52:45 +00:00
bne n o r f m o d u l e
jsr i n i t r f24
bne w e l c o m e ; BRA
norfmodule :
;debug, disable RF
2022-04-03 12:13:47 +00:00
inc R F _ E R R ; No module, set RF_ERR
welcome :
jsr P R I M M
message : .asciiz " GREETINGS PROFESSOR FALKEN.", "\n", "SHALL WE PLAY A GAME?", "\n "
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
welcomedone :
2022-05-27 15:52:45 +00:00
;Let's enable the keyboard
LDA #% 01101100
STA A C R ; T1 continuous, T2 count, PB7 disabled, Shift In External
lda #% 11100000 ; Set T1 + T2
sta I E R
lda k b b i t
sta T 2 C L
lda #0
sta T 2 C H
cli
main : ; loop
;Debug
LDA L A S T K B
sta E R R S
lda M I L L I S ; Don't do RF if key just pressed
cmp L A S T K B
lda M I L L I S H
sbc L A S T K B H
bcc s k i p r f
notyet :
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
lda R F _ E R R
2022-05-27 15:52:45 +00:00
;DEBUG
;lda #1
2022-04-03 12:13:47 +00:00
bne s k i p r f
bit A C R ; If ACR.7 is set then we can't use SPI since MOSI is outputting TM1.
bpl r f s t u f f
skiprf :
jmp n o m s g
rfstuff :
;jsr readrf24regs ; Debug - we can also just do an rf_nop to read rf24 status (RF_STS)
jsr r f _ n o p ; Not debug
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
;If msg received, put it in MSGBUF
bit R F _ S T S
bvs g t g m ; Check irq
lda R F _ S T S
cmp #$ 0 e ; Check if fifo empty
bne g t g m
jmp n o m s g ; No msg received
gtgm :
jsr g e t m e s s a g e
lda $ 9 0
bne d a t a p a c k e t ; Check for control message
lda $ 9 1
cmp #$ 31 ; Trust but verify
beq c t r l m s g
jsr i n i t r f24 ; Junk package. Reset radio.
jmp n o m s g
ctrlmsg :
lda $ 9 2
sta $ d0 ; Data size low byte
lda $ 9 3
sta $ d1 ; Data size high byte
jsr P R I M M
.asciiz " Receiving $ "
lda $ d1
jsr p r i n t b y t e
lda $ d0
jsr p r i n t b y t e
jsr P R I M M
.asciiz " bytes. \ n "
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
datapacket :
getmsg :
inc $ d2
lda $ 9 0
2022-05-27 15:52:45 +00:00
;cmp #1
2022-04-03 12:13:47 +00:00
bne n e x t p a c k e t ; Data package with ID > 1
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
lda $ d1 ; Size high byte
sta S H B
inc S H B
lda $ d0 ; Size low byte
sta S L B
lda #0
sta S V P
lda #3
2022-05-27 15:52:45 +00:00
sta S V P H ; Save pointer starts at $0300
jmp m a i n
2022-04-03 12:13:47 +00:00
nextpacket :
ldx #2
fetchpacket :
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
lda $ 9 0 ,x
ldy #0
sta ( S V P ) ,y
inc S V P
bne n n h b
2022-05-27 15:52:45 +00:00
inc S V P H
2022-04-03 12:13:47 +00:00
nnhb :
dec S L B
bne m o v e a l o n g
dec S H B
beq t x d o n e ; 0 bytes left - All done!
movealong :
dec S H B
lda S H B
jsr p r i n t b y t e
inc S H B
lda S L B
jsr p r i n t b y t e
jsr P R I M M
2022-05-27 15:52:45 +00:00
.asciiz " bytes l e f t ( h e x ) "
2022-04-03 12:13:47 +00:00
lda C R S R P N T
and #% 11000000 ; keep only section bits
ora #L I N E S T A R T ;
sta C R S R P N T
inx
cpx #32
bne f e t c h p a c k e t
jmp m a i n
txdone :
lda #3
sta $ 3 1
lda #0
sta k b _ r p t r ; Reset the keyboard pointers here.
sta k b _ w p t r
sta $ 3 0
2022-05-27 15:52:45 +00:00
;lda CRSRPNT ; Let's return to beginning of the line
;and #%11000000 ; keep only section bits
;ora #LINESTART ;
;sta CRSRPNT
;jsr PRIMM
;.asciiz " bytes left(hex)\n"
2022-04-03 12:13:47 +00:00
jsr P R I M M
.asciiz " \ nData l o a d e d i n t o R A M a t $ 0 3 0 0 . \ n P r e s s F 5 o r t y p e \ " r u n \ " t o s t a r t e x e c u t i n g a t $ 0 3 0 0 . \ n "
jsr r f _ n o p
2022-05-27 15:52:45 +00:00
; jsr resetkb
; jmp main
2021-10-21 07:59:53 +00:00
nomsg :
2022-04-03 12:13:47 +00:00
bit C R S R T
bmi i s n e g
lda k b _ f l a g s
and #$ 7 f
sta k b _ f l a g s ; Reset flip
sec
bcs s k i p p e d c u r s o r ; BRA
isneg :
bit k b _ f l a g s ; Same as last?
bpl f l i p
bmi s k i p p e d c u r s o r ; We already flipped
flip :
2022-05-27 15:52:45 +00:00
ldy #0
2022-04-03 12:13:47 +00:00
lda k b _ f l a g s
eor #C R S R
ora #C R S R F ; S e t f l i p b i t
sta k b _ f l a g s
bit k b _ f l a g s
bvs c u r s o r o f f
cursoron :
lda #' _ '
sta ( C R S R P N T ) ,y
bne c u r s o r d o n e ; BRA
cursoroff :
2022-05-27 15:52:45 +00:00
jsr r e s e t k b
2022-04-03 12:13:47 +00:00
lda C R S R C H R
sta ( C R S R P N T ) ,y
cursordone :
skippedcursor :
.if DEBUG
lda C R S R P N T ; Save cursor..
sta $ 5 4
2021-10-21 07:59:53 +00:00
pha
lda C R S R P N T 2
2022-04-03 12:13:47 +00:00
sta $ 5 5
2021-10-21 07:59:53 +00:00
pha
2022-04-03 12:13:47 +00:00
2022-05-27 15:52:45 +00:00
LDA #$ 4 e ; Print debug in bottom right corner of screen
2022-04-03 12:13:47 +00:00
sta C R S R P N T
lda #$ 27
sta C R S R P N T 2
2022-05-27 15:52:45 +00:00
;lda #$6f
;sta CRSRPNT
lda ( D E B U G P ) ,y
jsr p r i n t b y t e
inc C R S R P N T ; Space
;LDA #$72 ; Debug ERRS and show cursor pointer next to timer on screen
;sta CRSRPNT
;lda #$27
;sta CRSRPNT2
2022-04-03 12:13:47 +00:00
lda E R R S
jsr p r i n t b y t e
2022-05-27 15:52:45 +00:00
inc C R S R P N T ; Space
2022-04-03 12:13:47 +00:00
lda $ 5 5
jsr p r i n t b y t e
lda $ 5 4
jsr p r i n t b y t e
2022-05-27 15:52:45 +00:00
inc C R S R P N T ; Space
lda M I L L I S H
jsr p r i n t b y t e
inc C R S R P N T ; Space
lda I E R
jsr p r i n t b y t e
inc C R S R P N T ; Space
lda I F R
jsr p r i n t b y t e
inc C R S R P N T ; Space
lda k b s h i f t
jsr p r i n t b y t e
inc C R S R P N T ; Space
2022-04-03 12:13:47 +00:00
2021-10-21 07:59:53 +00:00
pla
sta C R S R P N T 2
pla
sta C R S R P N T
2022-04-03 12:13:47 +00:00
.endif
2022-05-27 15:52:45 +00:00
sei
lda k b _ r p t r
cmp k b _ w p t r
cli
bne k e y _ p r e s s e d
2021-10-21 07:59:53 +00:00
jmp m a i n
key_pressed :
ldx k b _ r p t r
lda k b _ b u f f e r , x
cmp #$ 0 a ; enter - go new line
beq e n t e r _ p r e s s e d
cmp #$ 1 b ; escape - clear display
beq e s c
cmp #$ 08
beq b a c k
cmp #$ F E ; S c a n c o d e $ 05
beq f1
cmp #$ F D
beq f2
cmp #$ F C
beq f3
cmp #$ F B
beq f4
cmp #$ F A
beq f5
cmp #$ F 9
beq f6
cmp #$ F 8
beq f7
cmp #$ F 7
beq f8
2022-04-03 12:13:47 +00:00
cmp #L _ A R R _ K E Y
beq g o l r u d a r r
cmp #R _ A R R _ K E Y
beq g o l r u d a r r
cmp #U P _ A R R _ K E Y
beq g o l r u d a r r
cmp #D N _ A R R _ K E Y
beq g o l r u d a r r
2021-10-21 07:59:53 +00:00
jsr p r i n t k
2022-04-03 12:13:47 +00:00
printedkey :
inc k b _ r p t r
2021-10-21 07:59:53 +00:00
jmp m a i n
2022-04-03 12:13:47 +00:00
golrudarr :
jmp l r u d a r r
2021-10-21 07:59:53 +00:00
back :
jmp b a c k s p a c e _ p r e s s e d
f8 :
jmp f8 _ p r e s s e d
f7 :
jmp f7 _ p r e s s e d
f6 :
jmp f6 _ p r e s s e d
f5 :
jmp f5 _ p r e s s e d
f4 :
jmp f4 _ p r e s s e d
f3 :
jmp f3 _ p r e s s e d
f2 :
jmp f2 _ p r e s s e d
f1 :
jmp f1 _ p r e s s e d
esc :
jmp e s c _ p r e s s e d
enter_pressed :
2022-04-03 12:13:47 +00:00
lda C R S R C H R
sta ( C R S R P N T ) ,y
2021-10-21 07:59:53 +00:00
ldx #0
lda k b _ b u f f e r ,x
parsecmd :
cmp #' r '
beq r e a d
cmp #' w '
beq w r i t e
cmp #' H '
beq s t r a n g e g a m e
cmp #' h '
beq s t r a n g e g a m e
jmp e r r
2022-04-03 12:13:47 +00:00
read :
jmp j r e a d
2021-10-21 07:59:53 +00:00
strangegame :
2022-04-03 12:13:47 +00:00
jsr P R I M M
.asciiz " \ n" , " A S T R A N G E G A M E . T H E O N L Y W I N N I N G " , " \ n " , " M O V E I S N O T T O P L A Y . " , " \ n "
2021-10-21 07:59:53 +00:00
wrotegtnw :
2022-04-03 12:13:47 +00:00
jmp e o m
jerr :
jmp e r r
2021-10-21 07:59:53 +00:00
write :
ldy #$ F F
inx
lda k b _ b u f f e r ,x
cmp #' r '
2022-04-03 12:13:47 +00:00
bne j e r r
2021-10-21 07:59:53 +00:00
ldx #6
jsr a s c t o b y t e
sta M O N H ; Maybe we'll find a better place
inx
jsr a s c t o b y t e
sta M O N L
nextbyte :
inx
lda k b _ b u f f e r ,x
cmp #' '
beq n e x t b y t e
cmp ' . '
beq n e x t b y t e
cmp ' ,'
beq n e x t b y t e
cmp ' $ '
beq n e x t b y t e
cmp #$ 0 a
beq e x i t w r i t e
stbyte :
jsr a s c t o b y t e
iny
sta ( M O N L ) ,y
jmp n e x t b y t e
exitwrite :
2022-04-03 12:13:47 +00:00
; inc kb_rptr
; jmp newmon ; Previous behavior
jsr P R I M M
.asciiz " \ nWrote "
iny
tya
jsr p r i n t b y t e
jsr P R I M M
.asciiz " bytes. P r e s s F 1 t o e n t e r m o n i t o r \ n "
jmp e o m
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
jread :
2021-10-21 07:59:53 +00:00
inx
lda k b _ b u f f e r ,x
cmp #' u '
beq r u n
cmp #' e '
bne e r r
ldx #5
jsr a s c t o b y t e
sta M O N H
inx
jsr a s c t o b y t e
sta M O N L
2022-04-03 12:13:47 +00:00
; inc kb_rptr
; jmp newmon
ldy #0
lda ( M O N L ) ,y
jsr P R I M M
.asciiz " \ nRead : "
jsr p r i n t b y t e
jsr P R I M M
.asciiz " \ nPress F 1 t o e n t e r m o n i t o r \ n "
jmp e o m
2021-10-21 07:59:53 +00:00
err :
lda C R S R P N T
jsr c r n l
sta C R S R P N T
bcc c l k b p t r
inc C R S R P N T 2
2022-04-03 12:13:47 +00:00
lda ( C R S R P N T ) ,y
sta C R S R C H R
2021-10-21 07:59:53 +00:00
clkbptr :
; Clear keyboard pointers if this is the end of message
inc k b _ r p t r
sei
lda k b _ r p t r
cmp k b _ w p t r
cli
bne n o t d o n e
eom :
2022-04-03 12:13:47 +00:00
lda ( C R S R P N T ) ,y ; Save new char under cursor ; Here or in err:?
sta C R S R C H R
2021-10-21 07:59:53 +00:00
lda #0
sta k b _ r p t r
sta k b _ w p t r
notdone :
jmp m a i n
run :
lda #0
sta k b _ r p t r
sta k b _ w p t r
jmp ( $ 0 0 3 0 )
esc_pressed :
jsr c l r s c n
lda #S C R E E N S T A R T L
sta C R S R P N T
lda #S C R E E N S T A R T H
sta C R S R P N T 2
jmp c l k b p t r
backspace_pressed :
2022-04-03 12:13:47 +00:00
ldy #0
lda C R S R C H R
sta ( C R S R P N T ) ,y
normcrsr :
2021-10-21 07:59:53 +00:00
dec C R S R P N T
lda C R S R P N T
cmp #255
bne n o t u n d e r
dec C R S R P N T 2
notunder :
lda #' '
sta ( C R S R P N T ) ,y
dec k b _ r p t r
dec k b _ w p t r
dec k b _ w p t r
jmp m a i n
f1_pressed :
lda #0
sta k b _ r p t r
sta k b _ w p t r
jsr c l r s c n
jmp m o n m o n
f2_pressed :
jmp e o m
f3_pressed :
2022-04-03 12:13:47 +00:00
2021-10-21 07:59:53 +00:00
jmp e o m
2022-04-03 12:13:47 +00:00
2021-10-21 07:59:53 +00:00
f4_pressed :
2022-04-03 12:13:47 +00:00
jsr g e t m e s s a g e
jsr P R I M M
.asciiz " Got m e s s a g e ! "
jsr n e w l i n e
2021-10-21 07:59:53 +00:00
jmp e o m
f5_pressed :
2022-04-03 12:13:47 +00:00
jsr r f _ n o p
jmp r u n
2021-10-21 07:59:53 +00:00
f6_pressed :
2022-04-03 12:13:47 +00:00
jsr r e a d r f24 r e g s
jsr P R I M M
.asciiz " Read R F 2 4 c o n f i g u r a t i o n ! "
jsr n e w l i n e
jmp e o m
2021-10-21 07:59:53 +00:00
f7_pressed :
2022-04-03 12:13:47 +00:00
jsr i n i t r f24
jmp e o m
2021-10-21 07:59:53 +00:00
f8_pressed :
2022-04-03 12:13:47 +00:00
jmp r e s e t
2021-10-21 07:59:53 +00:00
newmon :
jsr c l r s c n
monmon :
2022-04-03 12:13:47 +00:00
lda #29
2021-10-21 07:59:53 +00:00
sta M O N C N T
lda M O N H
pha
lda M O N L
pha
jsr m o n
pla
sta M O N L
pla
sta M O N H
sei
lda k b _ r p t r
cmp k b _ w p t r
cli
beq m o n m o n
ldx k b _ r p t r
inc k b _ r p t r
lda k b _ b u f f e r , x
2022-04-03 12:13:47 +00:00
cmp #E S C _ K E Y ; e s c a p e - e x i t
2021-10-21 07:59:53 +00:00
beq e x i t m o n
2022-04-03 12:13:47 +00:00
cmp #P G U P _ K E Y
beq m o n p g u p
cmp #P G D N _ K E Y
beq m o n p g d n
cmp #U P _ A R R _ K E Y
beq m o n u p
cmp #D N _ A R R _ K E Y
beq m o n d n
2021-10-21 07:59:53 +00:00
; Let's loop here until keypress
jmp m o n m o n ; No data - restart monitor
2022-04-03 12:13:47 +00:00
mondn : ; Arrow keys invert the natural direction because pressing down naturally means you want to see another line
clc
lda M O N L
adc #$ 08
sta M O N L
bcc m o n m o n
inc M O N H
jmp m o n m o n
monup : ; Pressing up means you want to see a line higher up == lower
sec
lda M O N L
sbc #$ 08
sta M O N L
bcs m o n m o n
dec M O N H
jmp m o n m o n
monpgdn :
clc
lda M O N L
adc #$ D 8
sta M O N L
bcc m o n m o n
inc M O N H
jmp m o n m o n
monpgup :
sec
lda M O N L
sbc #$ D 8
sta M O N L
bcs m o n m o n
dec M O N H
jmp m o n m o n
2021-10-21 07:59:53 +00:00
exitmon :
jsr c l r s c n ; Clear screen on monitor exit
2022-04-03 12:13:47 +00:00
lda #S C R E E N S T A R T L
sta C R S R P N T
lda #S C R E E N S T A R T H
sta C R S R P N T 2
2021-10-21 07:59:53 +00:00
jmp e o m
; lda #0
; sta MILLISH
asctohex :
cmp #$ 60
bcc c a p s
2022-04-03 12:13:47 +00:00
sbc #$ 21
2021-10-21 07:59:53 +00:00
caps :
sbc #' 0 ' - 1
cmp #10
bcc n o t h e x
2022-04-03 12:13:47 +00:00
sbc #7
2021-10-21 07:59:53 +00:00
nothex :
rts
clrscn :
lda #0
sta C R S R P N T
2022-04-03 12:13:47 +00:00
lda #$ 20 ; Clear before screen to after screen
sta C R S R P N T 2
2021-10-21 07:59:53 +00:00
ldy #0
tya
ldx #9
clrloop :
sta ( C R S R P N T ) ,y
iny
bne c l r l o o p
2022-04-03 12:13:47 +00:00
inc C R S R P N T 2 ; increasing HI-byte of the clearing address.
2021-10-21 07:59:53 +00:00
dex
bne c l r l o o p
rts
crnl : ; Carriage return new line - needs cursor pointer in A
and #% 11000000 ; keep only section bits
ora #L I N E S T A R T ;
clc
adc #$ 40 ; CR
rts
2022-04-03 12:13:47 +00:00
newline :
lda C R S R P N T
jsr c r n l
sta C R S R P N T
and #$ E 0 ; If msn is 0 then ++section
bne n e w l i n e d o n e
inc C R S R P N T 2
newlinedone :
rts
2021-10-21 07:59:53 +00:00
hextoa :
; wozmon-style
; and #%00001111 ; Mask LSD for hex print.
; Already masked when we get here.
ora #' 0 ' ; Add '0'.
cmp #' 9 ' + 1 ; Is it a decimal digit?
bcc a s c r ; Yes, output it.
adc #$ 06 ; Add offset for letter.
ascr :
rts
bytetoa : ;This SR puts LSB in A and MSB in HXH - as ascii using hextoa.
pha
lsr
lsr
lsr
lsr
clc
jsr h e x t o a
sta H X H
pla
and #$ 0 F
jsr h e x t o a
rts
asctobyte : ; Reads two hex characters from keyboard buffer, x indexed, and returns a byte in A
lda k b _ b u f f e r ,x ; MSD
jsr a s c t o h e x
asl a
asl a
asl a
asl a
sta T M P
inx
lda k b _ b u f f e r ,x ; LSD
jsr a s c t o h e x
ora T M P
; Return value in A
rts
2022-05-27 15:52:45 +00:00
resetkb :
lda I F R
and #% 00100000
bne n o t t h i s t i m e
sei
LDA #% 01000000
STA A C R ; T1 continuous - disable Shift register
LDA #% 01101100
STA A C R ; T1 continuous, T2 count, PB7 disabled, Shift In External
lda #% 11100000 ; Set T1 + T2
sta I E R
lda k b b i t
sta T 2 C L
lda #0
sta T 2 C H
notthistime :
cli
rts
2021-10-21 07:59:53 +00:00
mon :
; Print line starting address
; Print 8 ascii hex bytes separated by ' '
; Print same 8 bytes as ASCII
;newline
;Let's start by resetting start pos
lda #S C R E E N S T A R T H ; I f w e a r e , t h e n r e s e t t o t o p o f s c r e e n
sta C R S R P N T 2
lda #S C R E E N S T A R T L
sta C R S R P N T
nextline :
ldx #8
lda #' $ '
jsr p r i n t a
lda M O N H
jsr p r i n t b y t e
lda M O N L
jsr p r i n t b y t e
lda #' : '
jsr p r i n t a
lda #' '
jsr p r i n t a
ldy #0
nexthex :
lda ( M O N L ) ,y
sta A B U F ,x
jsr p r i n t b y t e
inc C R S R P N T ; Make a space
inc M O N L
bne n o t o f
inc M O N H
notof :
dex
bne n e x t h e x
; Print ascii
ldx #8
printabuf :
lda A B U F ,x
2022-05-27 15:52:45 +00:00
cmp #$ 0 A
bne n o t n l
tya ; Zero A
notnl :
2021-10-21 07:59:53 +00:00
jsr p r i n t a
dex
bne p r i n t a b u f
; Get ready for a new line
lda C R S R P N T
jsr c r n l ; Carriage Return New Line
sta C R S R P N T
and #$ E 0 ; If msn is 0 then ++section
bne g o n e x t
inc C R S R P N T 2
gonext :
dec M O N C N T
bne n e x t l i n e ; Was this the last line? No, nextline.
rts
printbyte :
jsr b y t e t o a
pha
lda H X H
jsr p r i n t a
pla
jsr p r i n t a
rts
2022-05-27 15:52:45 +00:00
printfast :
sta ( C R S R P N T ) ,y
inc C R S R P N T
bne p r i n t e d f a s t
inc C R S R P N T 2
printedfast :
rts
2021-10-21 07:59:53 +00:00
printa :
2022-04-03 12:13:47 +00:00
sta T M P 2 ; save A
TYA ; copy Y
PHA ; save Y
TXA ; copy X
PHA ; save X
2021-10-21 07:59:53 +00:00
ldy #0
2022-04-03 12:13:47 +00:00
lda T M P 2
cmp #$ 0 A ; Do we need this?
beq p r i n t n e w l i n e
2021-10-21 07:59:53 +00:00
sta ( C R S R P N T ) ,y
inc C R S R P N T
bne p r i n t e d a
inc C R S R P N T 2
printeda :
2022-04-03 12:13:47 +00:00
PLA ; pull value
TAX ; restore X
PLA ; pull value
TAY ; restore Y
2021-10-21 07:59:53 +00:00
rts
2022-04-03 12:13:47 +00:00
printnewline :
jsr n e w l i n e
jmp p r i n t e d a
lrudarr :
; bit MILLIS
; bpl correctchr Not worth testing...
pha
ldy #0
lda C R S R C H R ; Make sure we leave the character in the position and not the cursor.
sta ( C R S R P N T ) ,y
correctchr :
pla
cmp #R _ A R R _ K E Y
beq r a r r
cmp #U P _ A R R _ K E Y
beq u p a r r
cmp #D N _ A R R _ K E Y
beq d n a r r
larr :
dec C R S R P N T ; Move cursor left
lda C R S R P N T
cmp #255
bne c h e c k l i n e
dec C R S R P N T 2
checkline :
and #% 00111111
cmp #L I N E S T A R T
bcs c h e c k e d a r r o w s
lda C R S R P N T
sec
sbc #$ 40
ora #$ 3 E
sta C R S R P N T
bcs c h e c k e d a r r o w s
dec C R S R P N T 2
bcc c h e c k e d a r r o w s ; BRA
rarr :
inc C R S R P N T
lda C R S R P N T
and #$ 3 F ; Discard MS bits since we only care about current line
2022-05-27 15:52:45 +00:00
cmp #L I N E E N D
2022-04-03 12:13:47 +00:00
bcc c h e c k e d a r r o w s ; A < 62 == Not Front porch
jsr n e w l i n e
ora #L I N E S T A R T
sta C R S R P N T
jmp c h e c k e d a r r o w s
uparr :
lda C R S R P N T
sec
sbc #$ 40
sta C R S R P N T
bcs c h e c k e d a r r o w s
dec C R S R P N T 2
bcc c h e c k e d a r r o w s ; BRA
dnarr :
lda C R S R P N T
clc
adc #$ 40
sta C R S R P N T
bcc c h e c k e d a r r o w s
inc C R S R P N T 2
jmp c h e c k e d a r r o w s
checkedarrows :
jsr c h e c k b o t t o m
lda ( C R S R P N T ) ,y ; Save new char under cursor
sta C R S R C H R
lda k b _ f l a g s
ora #C R S R
sta k b _ f l a g s
lda #' _ '
sta ( C R S R P N T ) ,y
jmp p r i n t e d k e y
2021-10-21 07:59:53 +00:00
printk :
pha
checkl : ; This is user input, so we have to make sure we don't hit VGA blanking by mistake
lda C R S R P N T
and #$ 3 F ; Discard MS bits since we only care about current line
2022-05-27 15:52:45 +00:00
cmp #L I N E E N D
2021-10-21 07:59:53 +00:00
bcc c h s ; A < 62 == Not Front porch
2022-04-03 12:13:47 +00:00
jsr n e w l i n e
2021-10-21 07:59:53 +00:00
chs :
2022-04-03 12:13:47 +00:00
cmp #L I N E S T A R T
bcs c h s2
ora C R S R P N T
2021-10-21 07:59:53 +00:00
sta C R S R P N T
2022-04-03 12:13:47 +00:00
chs2 :
jsr c h e c k b o t t o m
2021-10-21 07:59:53 +00:00
rpa :
pla
jsr p r i n t a
rts
2022-04-03 12:13:47 +00:00
checkbottom :
lda C R S R P N T 2 ; Check if we're off screen
cmp #$ 20
bcc r e s e t c u r s o r ; Off screen
cmp #$ 27
bcc c h e c k e d b o t t o m ; if we're not
lda C R S R P N T ; Check LSB as well if we're above $2700
cmp #$ 80
bcc c h e c k e d b o t t o m
resetcursor :
lda #S C R E E N S T A R T H ; I f w e a r e , t h e n r e s e t t o t o p o f s c r e e n ; O r s t a r t s c r o l l i n g ?
sta C R S R P N T 2
lda #S C R E E N S T A R T L
sta C R S R P N T
checkedbottom :
rts
2021-10-21 07:59:53 +00:00
; IRQ vector points here ; Thanks to Ben Eater for a very useful PS2->Ascii interface
2022-04-03 12:13:47 +00:00
;IFR is IRQ Tl T2 CBl CB2 SR CA1 CA2
2021-10-21 07:59:53 +00:00
irq :
2022-05-27 15:52:45 +00:00
pha ; Save A
txa
pha ; Save X
; lda #%00100000 ; We need T2 to fire super fast, so we check it first.;
2022-04-03 12:13:47 +00:00
; and IFR
; bne t2_irq
;Alt approach
2022-05-27 15:52:45 +00:00
lda I E R
and I F R ; We only care about active IRQ's
asl ; IRQ in C
asl ; T1 flag in C
bcs t 1 _ i r q
asl ; T2
bcs t 2 _ i r q
;asl ; CB1
;asl ; CB2
;asl ; SR
;asl ; CA1
2022-04-03 12:13:47 +00:00
; bcs keyboard_interrupt
; asl ; CA2
2022-05-27 15:52:45 +00:00
; bit IFR ; T1 as fast as possible
; bvs t1_irq
2022-04-03 12:13:47 +00:00
2021-10-21 07:59:53 +00:00
tsx
lda $ 0 1 0 3 ,x ; Pull status register off stack and check break flag
and #$ 10
bne h i t b r k
2022-04-03 12:13:47 +00:00
2022-05-27 15:52:45 +00:00
; lda IFR
; and #2
; bne keyboard_interrupt
2022-04-03 12:13:47 +00:00
inc E R R S ;Should never end up here...
2022-05-27 15:52:45 +00:00
jmp e x i t
2021-10-21 07:59:53 +00:00
hitbrk :
; jmp reset
inc $ 0 8
2022-04-03 12:13:47 +00:00
lda #< m a i n
sta $ 1 0 4 ,x ; Return to main instead of breakpoint
lda #> m a i n
sta $ 1 0 5 ,x
jmp e x i t
2021-10-21 07:59:53 +00:00
2022-04-03 12:13:47 +00:00
t1_irq :
bit T 1 C L ; Clear irq
gmillis :
inc C R S R T
inc C R S R T
inc M I L L I S
bne t 1 _ i r q _ e x i t
inc M I L L I S + 1
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
t1_irq_exit :
2022-05-27 15:52:45 +00:00
; pla
; rti
jmp e x i t
2022-04-03 12:13:47 +00:00
2022-05-27 15:52:45 +00:00
; cb1_IRQ:
; dec kbbit
; lda PORTB ; clear IRQ
; pla
; rti
2022-04-03 12:13:47 +00:00
2022-05-27 15:52:45 +00:00
t2_irq :
lda #$ 81
sta $ 5 0 0 0 ; Ouput only register debug
lda S R 1 ;
tax
lda r e v e r s e b i t s , x
sta K E Y B O A R D
LDA #% 01100000
STA A C R ; T1 continuous - disable Shift register
LDA #% 01101100
STA A C R ; T1+T2, reenable Shift register
lda k b s h i f t ; If we count 11(10 + one we miss?) falling edges we should end up at the same index in the shifted data
sta T 2 C L
lda #0
sta T 2 C H
clc
lda M I L L I S
adc #25
sta L A S T K B
lda M I L L I S H
adc #0 ; C
sta L A S T K B H
setrferr :
; inc RF_ERR
; Fall through to scancode -> ascii parsing -> key buffer
keyboard_interrupt :
lda k b _ f l a g s
and #R E L E A S E ; c h e c k i f w e ' r e r e l e a s i n g a k e y
beq r e a d _ k e y ; otherwise, read the key
lda k b _ f l a g s
eor #R E L E A S E ; f l i p t h e r e l e a s i n g b i t
sta k b _ f l a g s
; lda PORTA ; read key value that's being released
lda K E Y B O A R D
cmp #$ 12 ; left shift
beq s h i f t _ u p
cmp #$ 59 ; right shift
beq s h i f t _ u p
jmp e x i t
; lda #%00010000
2022-04-03 12:13:47 +00:00
; eor PORTB
; sta PORTB
; lda #0
; sta T2CH ; pla + rti should be enough time for IRQ line to go high again before leaving IRQ - at 2 mhz anyway
; pla
; rti
ekey_up :
lda k b _ f l a g s
and #% 11111011 ; Turn off ecode
sta k b _ f l a g s
jmp e x i t
2021-10-21 07:59:53 +00:00
shift_up :
lda k b _ f l a g s
eor #S H I F T ; f l i p t h e s h i f t b i t
sta k b _ f l a g s
jmp e x i t
2022-04-03 12:13:47 +00:00
break :
jmp n o c l e a r
2021-10-21 07:59:53 +00:00
read_key :
2022-05-27 15:52:45 +00:00
;lda PORTA
lda $ 0 c
2022-04-03 12:13:47 +00:00
cmp #$ 77 ; Either numlock or pause/break - we reset without clearing ram
beq b r e a k
2021-10-21 07:59:53 +00:00
cmp #$ f 0 ; if releasing a key
beq k e y _ r e l e a s e ; set the releasing bit
cmp #$ 12 ; left shift
beq s h i f t _ d o w n
cmp #$ 59 ; right shift
beq s h i f t _ d o w n
2022-04-03 12:13:47 +00:00
cmp #$ E 0
beq e k e y _ d o w n
2021-10-21 07:59:53 +00:00
tax
lda k b _ f l a g s
2022-04-03 12:13:47 +00:00
and #E C O D E
bne e c o d e _ k e y
lda k b _ f l a g s
2021-10-21 07:59:53 +00:00
and #S H I F T
bne s h i f t e d _ k e y
2022-04-03 12:13:47 +00:00
txa
sta k b _ l a s t
2021-10-21 07:59:53 +00:00
lda k e y m a p , x ; map to character code
jmp p u s h _ k e y
2022-04-03 12:13:47 +00:00
ekey_down :
sta k b _ l a s t
lda k b _ f l a g s
ora #E C O D E
sta k b _ f l a g s
jmp e x i t
ecode_key :
txa
sta k b _ l a s t
lda k e y m a p _ e c o d e ,x
jmp p u s h _ k e y
2021-10-21 07:59:53 +00:00
shifted_key :
lda k e y m a p _ s h i f t e d , x ; map to character code
push_key :
ldx k b _ w p t r
sta k b _ b u f f e r , x
inc k b _ w p t r
jmp e x i t
shift_down :
lda k b _ f l a g s
ora #S H I F T
sta k b _ f l a g s
jmp e x i t
key_release :
lda k b _ f l a g s
ora #R E L E A S E
sta k b _ f l a g s
2022-04-03 12:13:47 +00:00
lda k b _ l a s t
cmp #$ e 0
bne e x i t
jmp e k e y _ u p
2021-10-21 07:59:53 +00:00
exit :
2022-05-27 15:52:45 +00:00
lda #1
sta $ 5 0 0 0 ; OOR debug
2021-10-21 07:59:53 +00:00
pla
tax
pla
rti
; Thanks to Ben Eater for a very useful PS2->Ascii interface
keymap :
.byte " ? ? ? " , $ FA,$ F C ,$ F E ,$ F D ," ? ? ? " ,$ F 7 ,$ F 9 ,$ F B ," ` ? " ; 00-0F
;F1, F2, F3,F4, F5, F6, F7, F8 key bound to $FE, $FD, $FC, $FB, $FA, $F9, $F8, $F7 for no particular reason
.byte " ? ? ? ? ? q1 ? ? ? z s a w2 ? " ; 10-1F
.byte " ? cxde4 3 ? ? v f t r5 ? " ; 20-2F
.byte " ? nbhgy6 ? ? ? m j u 7 8 ? " ; 30-3F
.byte " ? , kio0 9 ? ? . / l ;p-?" ; 40-4F
.byte " ? ? \ ' ? [ = ? ? ? ? " , $ 0 a," ] ? \ \ ? ? " ; 50-5F
.byte " ? ? ? ? ? ? " , $ 0 8 , " ? ? 1 ? 4 7 ? ? ? " ; 60-6F
2022-04-03 12:13:47 +00:00
.byte " 0 .2568 " , ESC_ K E Y ," ? ? + 3 - * 9 ? ? " ; 70-7F
2021-10-21 07:59:53 +00:00
.byte " ? ? ? " , $ F8 ," ? ? ? ? ? ? ? ? ? ? ? ? " ; 80-8F $F8 = F7 key
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; 90-9F
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; A0-AF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; B0-BF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; C0-CF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; D0-DF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; E0-EF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; F0-FF
keymap_shifted :
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ~ ? " ; 00-0F
.byte " ? ? ? ? ? Q! ? ? ? Z S A W @?" ; 10-1F
.byte " ? CXDE#$ ? ? V F T R % ? " ; 20 - 2 F
.byte " ? NBHGY^ ? ? ? M J U & * ? " ; 30-3F
.byte " ? < KIO) ( ? ? > ? L : P _ ? " ; 40-4F
.byte " ? ? \ " ? { + ? ? ? ? ? } ? | ? ? " ; 50-5F
.byte " ? ? ? ? ? ? ? ? ? 1 ? 4 7 ? ? ? " ; 60-6F
.byte " 0 .2568 ? ? ? + 3 - * 9 ? ? " ; 70-7F
.byte " ? ? ? " , $ F8 ," ? ? ? ? ? ? ? ? ? ? ? ? " ; 80-8F $F8 = F7 key
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; 90-9F
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; A0-AF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; B0-BF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; C0-CF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; D0-DF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; E0-EF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; F0-FF
2022-04-03 12:13:47 +00:00
keymap_ecode :
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 00-0F
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 10-1F
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 20-2F
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 30-3F
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 40-4F
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 50-5F
.byte " 0 1 2 3 4 5 6 7 8 9 A" ,L _ A R R _ K E Y , H O M E _ K E Y , " D E F " ; 60-6F
.byte " 0 1 " , DN_ A R R _ K E Y ," 3 " ,R _ A R R _ K E Y , U P _ A R R _ K E Y , " 6 7 8 9 " ,P G D N _ K E Y ," B C " ,P G U P _ K E Y ," E F " ; 70-7F $F6 = PGUP(7D), $F5 = PGDOWN(7A), $F4 = UP Arr.(75), Down arr.(72) = $F3
.byte " 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 " ; 80-8F $F8 = F7 key
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; 90-9F
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; A0-AF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; B0-BF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; C0-CF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; D0-DF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; E0-EF
.byte " ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? " ; F0-FF
2022-05-27 15:52:45 +00:00
reversebits :
.byte 0 , 1 2 8 , 6 4 , 1 9 2 , 3 2 , 1 6 0 , 9 6 , 2 2 4 , 1 6 , 1 4 4 , 8 0 , 2 0 8 , 4 8 , 1 7 6 , 1 1 2 , 2 4 0
.byte 8 , 1 3 6 , 7 2 , 2 0 0 , 4 0 , 1 6 8 , 1 0 4 , 2 3 2 , 2 4 , 1 5 2 , 8 8 , 2 1 6 , 5 6 , 1 8 4 , 1 2 0
.byte 2 4 8 , 4 , 1 3 2 , 6 8 , 1 9 6 , 3 6 , 1 6 4 , 1 0 0 , 2 2 8 , 2 0 , 1 4 8 , 8 4 , 2 1 2 , 5 2 , 1 8 0
.byte 1 1 6 , 2 4 4 , 1 2 , 1 4 0 , 7 6 , 2 0 4 , 4 4 , 1 7 2 , 1 0 8 , 2 3 6 , 2 8 , 1 5 6 , 9 2 , 2 2 0 , 6 0
.byte 1 8 8 , 1 2 4 , 2 5 2 , 2 , 1 3 0 , 6 6 , 1 9 4 , 3 4 , 1 6 2 , 9 8 , 2 2 6 , 1 8 , 1 4 6 , 8 2 , 2 1 0 , 5 0
.byte 1 7 8 , 1 1 4 , 2 4 2 , 1 0 , 1 3 8 , 7 4 , 2 0 2 , 4 2 , 1 7 0 , 1 0 6 , 2 3 4 , 2 6 , 1 5 4 , 9 0 , 2 1 8
.byte 5 8 , 1 8 6 , 1 2 2 , 2 5 0 , 6 , 1 3 4 , 7 0 , 1 9 8 , 3 8 , 1 6 6 , 1 0 2 , 2 3 0 , 2 2 , 1 5 0 , 8 6 , 2 1 4
.byte 5 4 , 1 8 2 , 1 1 8 , 2 4 6 , 1 4 , 1 4 2 , 7 8 , 2 0 6 , 4 6 , 1 7 4 , 1 1 0 , 2 3 8 , 3 0 , 1 5 8 , 9 4
.byte 2 2 2 , 6 2 , 1 9 0 , 1 2 6 , 2 5 4 , 1 , 1 2 9 , 6 5 , 1 9 3 , 3 3 , 1 6 1 , 9 7 , 2 2 5 , 1 7 , 1 4 5 , 8 1
.byte 2 0 9 , 4 9 , 1 7 7 , 1 1 3 , 2 4 1 , 9 , 1 3 7 , 7 3 , 2 0 1 , 4 1 , 1 6 9 , 1 0 5 , 2 3 3 , 2 5 , 1 5 3 , 8 9
.byte 2 1 7 , 5 7 , 1 8 5 , 1 2 1 , 2 4 9 , 5 , 1 3 3 , 6 9 , 1 9 7 , 3 7 , 1 6 5 , 1 0 1 , 2 2 9 , 2 1 , 1 4 9 , 8 5
.byte 2 1 3 , 5 3 , 1 8 1 , 1 1 7 , 2 4 5 , 1 3 , 1 4 1 , 7 7 , 2 0 5 , 4 5 , 1 7 3 , 1 0 9 , 2 3 7 , 2 9 , 1 5 7
.byte 9 3 , 2 2 1 , 6 1 , 1 8 9 , 1 2 5 , 2 5 3 , 3 , 1 3 1 , 6 7 , 1 9 5 , 3 5 , 1 6 3 , 9 9 , 2 2 7 , 1 9 , 1 4 7
.byte 8 3 , 2 1 1 , 5 1 , 1 7 9 , 1 1 5 , 2 4 3 , 1 1 , 1 3 9 , 7 5 , 2 0 3 , 4 3 , 1 7 1 , 1 0 7 , 2 3 5 , 2 7
.byte 1 5 5 , 9 1 , 2 1 9 , 5 9 , 1 8 7 , 1 2 3 , 2 5 1 , 7 , 1 3 5 , 7 1 , 1 9 9 , 3 9 , 1 6 7 , 1 0 3 , 2 3 1 , 2 3
.byte 1 5 1 , 8 7 , 2 1 5 , 5 5 , 1 8 3 , 1 1 9 , 2 4 7 , 1 5 , 1 4 3 , 7 9 , 2 0 7 , 4 7 , 1 7 5 , 1 1 1 , 2 3 9
.byte 3 1 , 1 5 9 , 9 5 , 2 2 3 , 6 3 , 1 9 1 , 1 2 7 , 2 5 5
2022-04-03 12:13:47 +00:00
spibyte :
;SR borrowed from http://www.cyberspice.org.uk/blog/2009/08/25/bit-banging-spi-in-6502-assembler/
;I think GW used this way too?
2022-05-27 15:52:45 +00:00
;Bit 5 – MISO (Input data from the peripheral to the computer) ;
;Bit 7 – MOSI (Output data from the computer to the peripheral) We should prefer PB7 to be an output and not use PB6 because of T1 and T2 hw features(T1 out to PB7 and T2 in to PB6)
;Bit 1 – CS (Chip select)
2022-04-03 12:13:47 +00:00
;Bit 0 – SCLK (Output SPI clock)
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
sta o u t b
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
lda P O R T B
2022-05-27 15:52:45 +00:00
and #% 01011100
2022-04-03 12:13:47 +00:00
sta $ 1 E ; Save bits not used by SPI
2022-05-27 15:52:45 +00:00
2022-04-03 12:13:47 +00:00
TXA
PHA
TYA
PHA
ldy #0
sty i n b
ldx #8
2022-05-27 15:52:45 +00:00
; sei ; No IRQs during transfer plz
2022-04-03 12:13:47 +00:00
spibytelp :
tya ; (2) set A to 0
asl o u t b ; (5) shift MSB in to carry
bcc s p i b y t e 1 ; (2)
ora #m o s i ; ( 2 ) s e t M O S I i f M S B s e t
spibyte1 :
ora $ 1 E ; Debug - set preserved port B bits. If interrupts are enabled be careful when using this.
sta u s e r v i a ; (4) output (MOSI, SCS low, SCLK low)
tya ; (2) set A to 0 (Do it here for delay reasons)
inc u s e r v i a ; (6) toggle clock high (SCLK is bit 0)
clc ; (2) clear C (Not affected by bit)
2022-05-27 15:52:45 +00:00
;bit uservia ; (4) copy MISO (bit 7) in to N (and MOSI in to V) ; 4 cycles
;bpl spibyte2 ; (2) bvc if miso bit 6, bpl if miso bit 7 ; 2/3 cycles
lda #m i s o ; 2 c y c l e s
and u s e r v i a ; 4 cycles
beq s p i b y t e 2 ; 2/3 cycles
2022-04-03 12:13:47 +00:00
sec ; (2) set C if MISO bit is set (i.e. N) / Or V for miso on bit 6
spibyte2 :
rol i n b ; (5) copy C (i.e. MISO bit) in to bit 0 of result
dec u s e r v i a ; (6) toggle clock low (SCLK is bit 0)
dex ; (2) next bit
bne s p i b y t e l p ; (2) loop
2022-05-27 15:52:45 +00:00
; cli ; IRQs ok from here
2022-04-03 12:13:47 +00:00
PLA
TAY
PLA
TAX
lda i n b ; get result
rts
2022-05-27 15:52:45 +00:00
i2c_start : ; Let's assume i2c addr is in A and RW bit is in C
rol ; Move address to top bits and RW bit to bit0
sta o u t b ; Save addr + rw bit
lda #S C L _ I N V ; S t a r t w i t h S C L a s I N P U T H I G H
and D D R B
sta D D R B
lda #S D A ; I n s u r e S D A i s o u t p u t l o w b e f o r e S C L i s L O W
ora D D R B
sta D D R B
lda #S D A _ I N V
and P O R T B
sta P O R T B
lda #S C L _ I N V
and P O R T B
sta P O R T B
inc D D R B ; Set SCL as OUTPUT LOW
; Fall through to send address + RW bit
; From here on we can assume OUTPUTs are LOW and INPUTS are HIGH.
; Maybe some of the juggling above is not necessary but let's not assume for now
i2cbyteout :
lda #S D A _ I N V ; I n c a s e t h i s i s a d a t a b y t e w e s e t S D A L O W
and P O R T B
sta P O R T B
ldx #8
i2caddrloop : ; At start of loop SDA and SCL are both OUTPUT LOW
asl o u t b ; Put MSB in carry
inc D D R B ; Set SCL LOW by setting it to OUTPUT
bcc s e t i 2 c b i t 0 ; If data bit was low
lda D D R B ; else set it high
and #S D A _ I N V
sta D D R B
bcs w a s1 ; BRA
seti2cbit0 :
lda D D R B
ora #S D A
sta D D R B
was1 :
dec D D R B ; Let SCL go HIGH by setting it to input
dex
bne i 2 c a d d r l o o p
inc D D R B ; Set SCL low after last bit
lda D D R B ; Set SDA to INPUT
and #S D A _ I N V
sta D D R B
dec D D R B ; SCL high
; NOP here?
lda P O R T B ; Check ACK bit
clc
and #S D A
bne n a c k
sec ; Set carry to indicate ACK
nack :
inc D D R B ; SCL low
rts
i2cbytein : ; Assume SCL is LOW
lda D D R B ; Set SDA to input
and #S D A _ I N V
sta D D R B
ldx #8
byteinloop :
clc ; Clearing here for more even cycle
dec D D R B ; SCL high
; NOP?
lda P O R T B
and #S D A
beq g o t 0
sec
got0 :
rol i n b ; Shift carry into input byte
inc D D R B ; SCL low
dex
bne b y t e i n l o o p
lda D D R B ; Send ACK
ora #S D A
sta D D R B
dec D D R B ; SCL high
nop ; Probably can't just toggle
inc D D R B ; Set clock low
lda D D R B ; Set SDA to input (release it) - maybe not necessary
and #S D A _ I N V
sta D D R B
rts ; Input byte in inb
i2c_stop :
lda D D R B ; SDA low
ora #S D A
sta D D R B
dec D D R B ; SCL high
lda D D R B ; Set SDA high after SCL == Stop condition.
and #S D A _ I N V
sta D D R B
rts
i2c_test :
lda #$ 77 ; Address $77 = BMP180 address
clc ; Write
jsr i 2 c _ s t a r t
; Fail check here. C == 1 == ACK
bcc f a i l e d
lda #$ D 0 ; BMP180 register $D0 == chipID == $55
sta o u t b
jsr i 2 c b y t e o u t
lda #$ 77 ; Address
sec ; Read
jsr i 2 c _ s t a r t
jsr i 2 c b y t e i n
jsr i 2 c _ s t o p
failed :
rts
2022-04-03 12:13:47 +00:00
rf_nop :
lda P O R T B
and #% 11111101 ; Set CS Low
sta P O R T B
lda #$ F F
jsr s p i b y t e
sta R F _ S T S
lda P O R T B
ora #2 ; Set CS high
sta P O R T B
rts
;rw_reg takes command in x and data in A. Returns data in A.
rw_reg :
pha
lda P O R T B
and #% 11111101 ; Set CS Low
sta P O R T B
txa
jsr s p i b y t e
sta R F _ S T S
pla
jsr s p i b y t e
; A has return value
pha
lda P O R T B
ora #2 ; Set CS high
sta P O R T B
pla ; A has return value
rts
initrf24 :
ldx #$ E 2 ; Flush RX
lda #0
jsr r w _ r e g
ldx #$ 27 ; Clear RX_DR - $20 | $07, write address $07
lda #$ 40 ; %01000000 RX_DR mask
jsr r w _ r e g
ldx #$ 3 D ; Set FEATURE register - $20 | $1D, write address $1D
lda #4
jsr r w _ r e g
ldx #$ 3 C ; Set DYNDP register
lda #$ 3 F
jsr r w _ r e g
ldx #$ 20 ; Power up, RX
lda #$ 0 F
jsr r w _ r e g
lda P O R T B ; Set CE high
ora #4 ; CE is PB.2
sta P O R T B
rts
readrf24regs :
ldx #31
readrf24 :
lda #0 ; Let's read first byte of all registers
jsr r w _ r e g
sta $ 7 0 ,x
dex
bpl r e a d r f24
rts
getmessage :
; Here we read the msg
ldx #$ 60 ; Get top of RX fifo length - R_RX_PL_WID
lda #0
jsr r w _ r e g
sta $ 8 1 ; Overwrite P0 payload length location in mem
ldy #0
lda P O R T B
and #% 11111101 ; Set CS Low
sta P O R T B
readpayload :
lda #$ 61
jsr s p i b y t e
sta R F _ S T S
bytes :
jsr s p i b y t e
sta $ 9 0 , y ;
iny
cpy $ 8 1
bne b y t e s
lda P O R T B
ora #2 ; Set CS high
sta P O R T B
ldx #$ 27 ; Clear RX_DR
lda #$ 40
jsr r w _ r e g
rts
; Inline printing routine from http://6502.org/source/io/primm.htm
.segment " PRIMM"
.org $ ffc8
PRIMM :
PHA ; save A
TYA ; copy Y
PHA ; save Y
TXA ; copy X
PHA ; save X
TSX ; get stack pointer
LDA $ 0 1 0 4 ,X ; get return address low byte (+4 to
; correct pointer)
STA $ B C ; save in page zero
LDA $ 0 1 0 5 ,X ; get return address high byte (+5 to
; correct pointer)
STA $ B D ; save in page zero
LDY #$ 01 ; set index (+1 to allow for return
; address offset)
PRIM2 :
LDA ( $ B C ) ,Y ; get byte from string
BEQ P R I M 3 ; exit if null (end of text)
JSR p r i n t k ; else display character
INY ; increment index
BNE P R I M 2 ; loop (exit if 256th character)
PRIM3 :
TYA ; copy index
CLC ; clear carry
ADC $ B C ; add string pointer low byte to index
STA $ 0 1 0 4 ,X ; put on stack as return address low byte
; (+4 to correct pointer, X is unchanged)
LDA #$ 00 ; clear A
ADC $ B D ; add string pointer high byte
STA $ 0 1 0 5 ,X ; put on stack as return address high byte
; (+5 to correct pointer, X is unchanged)
PLA ; pull value
TAX ; restore X
PLA ; pull value
TAY ; restore Y
PLA ; restore A
RTS
2021-10-21 07:59:53 +00:00
.segment " VECTORS"
.ORG $ fffa
.word nmi,r e s e t ,i r q
.reloc
2022-05-27 15:52:45 +00:00
.export PRIMM, p r i n t k