Skip to content

Commit 5f78e58

Browse files
committed
[apple] remove indirect jmp in sp_dispatch to fix page boundary bug
1 parent 3ae67a2 commit 5f78e58

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [atari] fix appkey arrays/pointers
1010
- [make] Use new makefile structure
1111
- [coco] Use same makefiles as other targets
12+
- [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
1213

1314
## [4.2.0] - 2024-06-19
1415

apple2/src/bus/sp_data.s

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.export _sp_count
22
.export _sp_cmdlist
33
.export _sp_dest
4-
.export _sp_dispatch_fn
54
.export _sp_error
65
.export _sp_is_init
76
.export _sp_payload
@@ -17,8 +16,6 @@ _sp_is_init: .byte 0
1716
_sp_dest: .res 1
1817
_sp_error: .res 1
1918
_sp_count: .res 2
20-
.align 2
21-
_sp_dispatch_fn: .res 2
2219
_sp_cmdlist: .res 10
2320

2421
_sp_payload: .res SP_PAYLOAD_SIZE

apple2/src/bus/sp_dispatch.s

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
.export _sp_dispatch
2+
.export _sp_dispatch_address
23

34
.import _sp_count
45
.import _sp_cmdlist
5-
.import _sp_dispatch_fn
66
.import _sp_error
77

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

1010
; int8_t sp_dispatch(uint8_t cmd)
1111
;
1212
; returns any error code from the smart port _sp_dispatch function
13-
.proc _sp_dispatch
13+
_sp_dispatch:
1414
sta dispatch_data
1515
lda #<_sp_cmdlist
1616
sta dispatch_data+1
1717
lda #>_sp_cmdlist
1818
sta dispatch_data+2
1919
2020
; the SP dispatch alters the return address by 3 bytes to skip the data below.
21-
; it returs with any error codes
22-
jsr do_jmp
21+
; it returs with any error codes.
22+
.byte $20 ; JSR - making this a byte so we can get exact location of address being called
23+
_sp_dispatch_address:
24+
; overwritten in sp_init to correct address
25+
.word $0000
2326

2427
dispatch_data:
2528
.byte $00 ; command
@@ -36,7 +39,3 @@ dispatch_data:
3639
lda _sp_error
3740
rts
3841

39-
do_jmp:
40-
jmp (_sp_dispatch_fn)
41-
42-
.endproc

apple2/src/bus/sp_init.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ uint8_t sp_init() {
3333
// If a match is found, calculate the dispatch function address
3434
offset = read_memory(base + 0xFF);
3535
dispatch_address = base + offset + 3;
36-
sp_dispatch_fn[0] = dispatch_address & 0xFF;
37-
sp_dispatch_fn[1] = dispatch_address >> 8;
36+
sp_dispatch_address[0] = dispatch_address & 0xFF;
37+
sp_dispatch_address[1] = dispatch_address >> 8;
3838

3939
// now find and return the network id. it's stored in sp_network after calling sp_get_network_id.
4040
// 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() {
4848
}
4949
}
5050

51-
// If no match is found, ensure dispatch function is cleared, sp_is_init is already 0, then return 0 for network not found.
52-
sp_dispatch_fn[0] = 0;
53-
sp_dispatch_fn[1] = 0;
51+
// no match is found, return 0 for network not found.
5452
return 0;
5553
}

apple2/src/include/fujinet-bus-apple2.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ extern uint8_t sp_payload[];
6666
// cmd data that is communicated to the SP device
6767
extern uint8_t sp_cmdlist[10];
6868

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

7272
// invoke smartport command
7373
int8_t sp_dispatch(uint8_t cmd);

0 commit comments

Comments
 (0)