diff --git a/Changelog.md b/Changelog.md index 2ac4c0b..31ab595 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/apple2/src/bus/sp_data.s b/apple2/src/bus/sp_data.s index 274f3e3..5c097a4 100644 --- a/apple2/src/bus/sp_data.s +++ b/apple2/src/bus/sp_data.s @@ -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 @@ -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 diff --git a/apple2/src/bus/sp_dispatch.s b/apple2/src/bus/sp_dispatch.s index ec4f5e5..b9e31e8 100644 --- a/apple2/src/bus/sp_dispatch.s +++ b/apple2/src/bus/sp_dispatch.s @@ -1,8 +1,8 @@ .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 @@ -10,7 +10,7 @@ ; 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 @@ -18,8 +18,11 @@ 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 @@ -36,7 +39,3 @@ dispatch_data: lda _sp_error rts -do_jmp: - jmp (_sp_dispatch_fn) - -.endproc diff --git a/apple2/src/bus/sp_init.c b/apple2/src/bus/sp_init.c index bc30dc3..41949db 100644 --- a/apple2/src/bus/sp_init.c +++ b/apple2/src/bus/sp_init.c @@ -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. @@ -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; } diff --git a/apple2/src/include/fujinet-bus-apple2.h b/apple2/src/include/fujinet-bus-apple2.h index 5bee372..085b9e5 100644 --- a/apple2/src/include/fujinet-bus-apple2.h +++ b/apple2/src/include/fujinet-bus-apple2.h @@ -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);