Skip to content

Commit

Permalink
[apple2][bus] Refactor _sp_get_xxx functions
Browse files Browse the repository at this point in the history
  • Loading branch information
markjfisher committed Sep 15, 2024
1 parent cb3f477 commit 3950b12
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 243 deletions.
44 changes: 6 additions & 38 deletions apple2/src/bus/cc65/sp_find_clock.s
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
.export _sp_get_clock_id
.export _sp_clock_id

.import _sp_find_device

.import return0
.import return1

.macpack cpu

sp_find_clock:
lda #$13
jsr _sp_find_device

bmi not_found
beq not_found

sta _sp_clock_id
jmp return1

not_found:
.if (.cpu .bitand ::CPU_ISET_65SC02)
stz _sp_clock_id
.else
lda #$00
sta _sp_clock_id
.endif
jmp return0

.import sp_find_device_type

; uint8_t sp_get_clock_id()
_sp_get_clock_id:
ldx #$00 ; prep the return hi byte for C callers
lda _sp_clock_id
bne :+ ; if it's already set, just exit

; otherwise we need to try and find it from SP
jsr sp_find_clock

; return whatever was set in sp_clock
ldx #$00
lda _sp_clock_id
: rts
lda #$13
ldx #<_sp_clock_id
ldy #>_sp_clock_id
jmp sp_find_device_type

.data
_sp_clock_id: .byte $00
_sp_clock_id: .byte $00
44 changes: 6 additions & 38 deletions apple2/src/bus/cc65/sp_find_cpm.s
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
.export _sp_get_cpm_id
.export _sp_cpm_id

.import _sp_find_device

.import return0
.import return1

.macpack cpu

sp_find_cpm:
lda #$12
jsr _sp_find_device

bmi not_found
beq not_found

sta _sp_cpm_id
jmp return1

not_found:
.if (.cpu .bitand ::CPU_ISET_65SC02)
stz _sp_cpm_id
.else
lda #$00
sta _sp_cpm_id
.endif
jmp return0

.import sp_find_device_type

; uint8_t sp_get_cpm_id()
_sp_get_cpm_id:
ldx #$00 ; prep the return hi byte for C callers
lda _sp_cpm_id
bne :+ ; if it's already set, just exit

; otherwise we need to try and find it from SP
jsr sp_find_cpm

; return whatever was set in sp_cpm
ldx #$00
lda _sp_cpm_id
: rts
lda #$12
ldx #<_sp_cpm_id
ldy #>_sp_cpm_id
jmp sp_find_device_type

.data
_sp_cpm_id: .byte $00
_sp_cpm_id: .byte $00
66 changes: 50 additions & 16 deletions apple2/src/bus/cc65/sp_find_device.s
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.export _sp_find_device
.export sp_find_device_type

.export device_type_id
.export device_count
Expand All @@ -15,10 +15,13 @@
.import pusha
.import negax
.import return0
.import return1

.include "sp.inc"
.include "zp.inc"

; int sp_find_device(uint8_t type_id);
; bool sp_find_device();
; device_type_id contains the type id to search for
; where type_id is the internal fujinet device type_id:
; $10 = fujinet
; $11 = network
Expand All @@ -28,8 +31,7 @@
; $15 = modem
; $01 = floppy disk
; $02 = hard disk
_sp_find_device:
sta device_type_id ; save the type_id
sp_find_device:
; check if we've already run sp_init
lda _sp_is_init
bne have_init
Expand All @@ -40,6 +42,7 @@ _sp_find_device:
jsr _sp_init
bne restore_type

return_error:
; not found, so return 0 as the network device, and is an error
jmp return0

Expand All @@ -53,20 +56,12 @@ have_init:
jsr _sp_status ; sp_status(0,0) fetches the device count

beq status_ok_1

; there was an error, negate A/X and return it
jmp negax
; implicit RTS
bne return_error

status_ok_1:
ldx #$00 ; prep the return value if it's going to be 0
lda _sp_payload ; device count in sp_payload[0]
beq :+ ; if the count is zero, just return 0, don't touch X as it's already 0
bpl have_count

; byte extend the negative value into X, as A is already negative
dex
: rts
beq return_error ; if the count is <= 0, just return 0
bmi return_error

have_count:
; now repeatedly call sp_status(i, 3) to get the DIB status for the device, which contains name and its device type
Expand Down Expand Up @@ -100,7 +95,46 @@ not_found_yet:
beq device_loop

; we have checked all devices, non had the type we were looking for
jmp return0
bcs return_error

; A = device type (e.g. $11 = network)
; X/Y = location to save the id
sp_find_device_type:
sta device_type_id ; the type to search for
stx id_loc1 ; the low location of the address to save the id at
sty id_loc1 + 1 ; the high location of the address to save the id at

lda $ffff ; don't use 0000, compiler thinks you want ZP!
id_loc1 = *-2

; is it already set?
beq :+

; the id was set in the indicated location, and is now in A, so return it, setting X to 0 for high byte for C callers
ldx #$00
cmp #$00 ; force status bits for the return
rts

; no ID set, so fetch it, write x/y to the location the id needs to be saved to
: stx id_loc2
sty id_loc2 + 1

jsr sp_find_device

beq not_found

; found it from searching, so return success after setting the id
jsr set_id
jmp return1

not_found:
tax ; set x to 0

set_id:
sta $ffff
id_loc2 = *-2

rts


.bss
Expand Down
47 changes: 14 additions & 33 deletions apple2/src/bus/cc65/sp_find_fuji.s
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.export _sp_get_fuji_id
.export _sp_fuji_id

.import _sp_find_device
.import sp_find_device_type
.import _sp_is_init
.import _sp_payload
.import _sp_status
Expand All @@ -15,57 +15,35 @@

.macpack cpu

sp_find_fuji:
; uint8_t sp_get_fuji_id()
_sp_get_fuji_id:
lda #$10
jsr _sp_find_device
ldx #<_sp_fuji_id
ldy #>_sp_fuji_id
jsr sp_find_device_type

bmi not_found_by_id
beq not_found_by_id

sta _sp_fuji_id
jmp return1
rts

not_found_by_id:
tax ; set X to 0
; if _sp_is_init is still 0, we didn't find the fujinet at all, so exit out
lda _sp_is_init
beq :+

; we need to loop through all the devices again for JEFF'S HACK
jsr try_fallback

; either ID is in A, or it's 0 for failure, either way, we use that to set fuji id and return
: sta _sp_fuji_id
rts

; uint8_t sp_get_fuji_id()
_sp_get_fuji_id:
ldx #$00 ; prep the return hi byte for C callers
lda _sp_fuji_id
bne :+ ; if it's already set, just exit

; otherwise we need to try and find it from SP
jsr sp_find_fuji

; return whatever was set in sp_fuji
ldx #$00
lda _sp_fuji_id
: rts


; similar to sp_find_device, but for hack check
; assumes device_count is set from previous search by ID
try_fallback:

lda #$01
sta device_id_idx

device_loop:
jsr pusha ; the current Device ID
lda #$03
jsr _sp_status ; sp_status(id,3) DIB request

bne not_found_yet ; there wasn't a valid DIB for this device index


; FALLBACK CHECK FOR DISK_0
; try old style, where it's the first disk that supports the status/control for fuji device - PIEPMEIER!
; the sp_payload contains following for disk_0 after a DIB status
Expand All @@ -86,7 +64,7 @@ device_loop:
; found it, so return the current index
ldx #$00
lda device_id_idx
rts
bne :+

not_found_yet:
inc device_id_idx
Expand All @@ -96,8 +74,11 @@ not_found_yet:
beq device_loop

; we have checked all devices, non had the type we were looking for
jmp return0
lda #$00
tax

: sta _sp_fuji_id
rts

.data
_sp_fuji_id: .byte $00
44 changes: 6 additions & 38 deletions apple2/src/bus/cc65/sp_find_modem.s
Original file line number Diff line number Diff line change
@@ -1,46 +1,14 @@
.export _sp_get_modem_id
.export _sp_modem_id

.import _sp_find_device

.import return0
.import return1

.macpack cpu

sp_find_modem:
lda #$15
jsr _sp_find_device

bmi not_found
beq not_found

sta _sp_modem_id
jmp return1

not_found:
.if (.cpu .bitand ::CPU_ISET_65SC02)
stz _sp_modem_id
.else
lda #$00
sta _sp_modem_id
.endif
jmp return0

.import sp_find_device_type

; uint8_t sp_get_modem_id()
_sp_get_modem_id:
ldx #$00 ; prep the return hi byte for C callers
lda _sp_modem_id
bne :+ ; if it's already set, just exit

; otherwise we need to try and find it from SP
jsr sp_find_modem

; return whatever was set in sp_modem
ldx #$00
lda _sp_modem_id
: rts
lda #$15
ldx #<_sp_modem_id
ldy #>_sp_modem_id
jmp sp_find_device_type

.data
_sp_modem_id: .byte $00
_sp_modem_id: .byte $00
Loading

0 comments on commit 3950b12

Please sign in to comment.