Skip to content

Commit

Permalink
[MONITOR] populate stack frame so that monitor's view of the register…
Browse files Browse the repository at this point in the history
…s at BRK are correct (#162)

* [MONITOR] populate stack frame so that monitor's view of the registers at BRK are correct

* we don't need to preserve A here, this was left here due to trying a different approach

* comment __irq to inform that the monitor depends on the current behavior
  • Loading branch information
mooinglemur committed Aug 9, 2023
1 parent 4da5cc4 commit 9222393
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
3 changes: 3 additions & 0 deletions kernal/drivers/x16/memory.s
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ __jmpfr:
.assert * = irq, error, "irq must be at specific address"
.export __irq
__irq:
; If this stack preserve order is ever changed, check
; and update the MONITOR entry code as it makes assumptions
; about what happens here upon BRK.
pha
lda rom_bank ;save ROM bank
pha
Expand Down
76 changes: 67 additions & 9 deletions monitor/monitor.s
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,61 @@ command_index := ram_code_end + 15 ; index from "command_names", or 'C'/'S' in E


monitor:
lda #<brk_entry
sta cbinv
lda #>brk_entry
sta cbinv + 1 ; BRK vector
lda #'C'
sta entry_type
lda #DEFAULT_BANK
sta bank
stz bank_flags
ldx #<(__monitor_ram_code_SIZE__ - 1)
: lda __monitor_ram_code_LOAD__,x
sta __monitor_ram_code_RUN__,x
dex
bpl :-
brk ; <- nice!
; we were almost certainly called by jsrfar, pop these off
pla ; jsrfar return LSB
pla ; jsrfar return MSB
pla ; caller bank
sta bank
plx ; kernsup return LSB
ply ; kernsup return MSB
; if we were called by BASIC, the caller bank will be 4
cmp #BANK_BASIC
bne @end
@basic:
; We were called from BASIC
; in the case of BRK, the RAM __irq pushes things in this order
; (original real RTI-style return)
; A, rombank, ret h, ret l, P, A (dummy copy of ret l), X, Y
; load the values from BASIC as this is our best guess
; which assumes MONITOR was called explicitly rather than
; via a BRK instruction.
; Populate the stack as described above
phy
phx
lda $030f
pha
lda $030c
pha
lda bank
pha
phy
phx
php
lda $030f
pha
ldx $030d
phx
ldy $030e
phy
@end:

php
sei
lda #<brk_entry
sta cbinv
lda #>brk_entry
sta cbinv + 1 ; BRK vector
plp

bra brk_entry2

.segment "monitor_ram_code"
; code that will be copied to $0220
Expand All @@ -200,14 +240,32 @@ brk_entry2:
.ifp02
cld
.endif
; in the case of BRK, the RAM __irq pushes things in this order
; (original real RTI-style return)
; A, rombank, ret h, ret l, P, A (dummy copy of ret l), X, Y
; let's pull them off in reverse order
pla
sta reg_y
pla
sta reg_x
pla
sta reg_a
; dummy A

; pull off RTI style return (which includes fake P flags)
pla
sta reg_p
pla
pla

; pull off preserved rom bank
pla
sta bank

pla
sta reg_a ; real preserved A from __irq

pla
sta reg_p ; this is the real processor flag reg at BRK

pla
sta reg_pc_lo
pla
Expand Down

0 comments on commit 9222393

Please sign in to comment.