Skip to content

Commit

Permalink
[apple] remove indirect jmp in sp_dispatch to fix page boundary bug
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Jun 30, 2024
1 parent 3ae67a2 commit 5f78e58
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [atari] fix appkey arrays/pointers
- [make] Use new makefile structure
- [coco] Use same makefiles as other targets
- [apple2] change sp_init/sp_dispatch to use self modifying code to fix page boundary issue and replace _sp_dispatch_fn for _sp_dispatch_address

## [4.2.0] - 2024-06-19

Expand Down
3 changes: 0 additions & 3 deletions apple2/src/bus/sp_data.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.export _sp_count
.export _sp_cmdlist
.export _sp_dest
.export _sp_dispatch_fn
.export _sp_error
.export _sp_is_init
.export _sp_payload
Expand All @@ -17,8 +16,6 @@ _sp_is_init: .byte 0
_sp_dest: .res 1
_sp_error: .res 1
_sp_count: .res 2
.align 2
_sp_dispatch_fn: .res 2
_sp_cmdlist: .res 10

_sp_payload: .res SP_PAYLOAD_SIZE
15 changes: 7 additions & 8 deletions apple2/src/bus/sp_dispatch.s
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
.export _sp_dispatch
.export _sp_dispatch_address

.import _sp_count
.import _sp_cmdlist
.import _sp_dispatch_fn
.import _sp_error

; KEEP THIS FILE AS ASM AS IT DOES TRICKS WITH DATA AND INDIRECT CALLS TO DISPATCH FUNCTION

; int8_t sp_dispatch(uint8_t cmd)
;
; returns any error code from the smart port _sp_dispatch function
.proc _sp_dispatch
_sp_dispatch:
sta dispatch_data
lda #<_sp_cmdlist
sta dispatch_data+1
lda #>_sp_cmdlist
sta dispatch_data+2

; the SP dispatch alters the return address by 3 bytes to skip the data below.
; it returs with any error codes
jsr do_jmp
; it returs with any error codes.
.byte $20 ; JSR - making this a byte so we can get exact location of address being called
_sp_dispatch_address:
; overwritten in sp_init to correct address
.word $0000

dispatch_data:
.byte $00 ; command
Expand All @@ -36,7 +39,3 @@ dispatch_data:
lda _sp_error
rts

do_jmp:
jmp (_sp_dispatch_fn)

.endproc
8 changes: 3 additions & 5 deletions apple2/src/bus/sp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ uint8_t sp_init() {
// If a match is found, calculate the dispatch function address
offset = read_memory(base + 0xFF);
dispatch_address = base + offset + 3;
sp_dispatch_fn[0] = dispatch_address & 0xFF;
sp_dispatch_fn[1] = dispatch_address >> 8;
sp_dispatch_address[0] = dispatch_address & 0xFF;
sp_dispatch_address[1] = dispatch_address >> 8;

// now find and return the network id. it's stored in sp_network after calling sp_get_network_id.
// we need to set sp_is_init to 1 to stop sp_get_network_id from calling init again and recursing.
Expand All @@ -48,8 +48,6 @@ uint8_t sp_init() {
}
}

// If no match is found, ensure dispatch function is cleared, sp_is_init is already 0, then return 0 for network not found.
sp_dispatch_fn[0] = 0;
sp_dispatch_fn[1] = 0;
// no match is found, return 0 for network not found.
return 0;
}
4 changes: 2 additions & 2 deletions apple2/src/include/fujinet-bus-apple2.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ extern uint8_t sp_payload[];
// cmd data that is communicated to the SP device
extern uint8_t sp_cmdlist[10];

// the dispatch function used for doing SP calls for a particular card
extern uint8_t sp_dispatch_fn[2];
// the location of the dispatch function to be written by sp_init
extern uint8_t sp_dispatch_address[2];

// invoke smartport command
int8_t sp_dispatch(uint8_t cmd);
Expand Down

0 comments on commit 5f78e58

Please sign in to comment.