AppleIISd/Firmware/src/Helper.s

285 lines
6.2 KiB
ArmAsm
Raw Normal View History

2017-12-03 21:18:37 +00:00
;*******************************
;
; Apple][Sd Firmware
2021-04-18 09:27:11 +00:00
; Version 1.2.3
2017-12-17 19:21:30 +00:00
; Helper functions
2017-12-03 21:18:37 +00:00
;
2021-02-20 12:59:37 +00:00
; (c) Florian Reitz, 2017 - 2021
2017-12-03 21:18:37 +00:00
;
; X register usually contains SLOT16
; Y register is used for counting or SLOT
;
;*******************************
.export SDCMD
2020-06-04 16:06:07 +00:00
.export GETR1
.export GETR3
2017-12-03 21:18:37 +00:00
.export GETBLOCK
2020-06-04 16:06:07 +00:00
.export COMMAND
2017-12-03 21:18:37 +00:00
.export CARDDET
.export WRPROT
2020-06-04 16:06:07 +00:00
.export INITED
2017-12-03 21:18:37 +00:00
.include "AppleIISd.inc"
.segment "EXTROM"
;*******************************
;
; Send SD command
; Call with command in CMDHI and CMDLO
;
;*******************************
2019-06-16 15:40:48 +00:00
SDCMD: .IFPC02
PHY
.ELSE
TYA
PHA
.ENDIF
2017-12-03 21:18:37 +00:00
LDY #0
@LOOP: LDA (CMDLO),Y
STA DATA,X
2021-05-09 12:03:37 +00:00
@WAIT: LDA CTRL,X ; TC is in N
2017-12-03 21:18:37 +00:00
BPL @WAIT
2021-05-09 12:03:37 +00:00
2017-12-03 21:18:37 +00:00
INY
CPY #6
BCC @LOOP
2019-06-16 15:40:48 +00:00
.IFPC02
2017-12-03 21:18:37 +00:00
PLY
2019-06-16 15:40:48 +00:00
.ELSE
PLA
TAY
.ENDIF
2017-12-03 21:18:37 +00:00
RTS
;*******************************
;
; Get R1
; R1 is in A
;
;*******************************
GETR1: LDA #DUMMY
STA DATA,X
2021-05-09 12:03:37 +00:00
@WAIT: LDA CTRL,X ; TC is in N
2017-12-03 21:18:37 +00:00
BPL @WAIT
2021-05-09 12:03:37 +00:00
2017-12-03 21:18:37 +00:00
LDA DATA,X ; get response
2018-07-07 15:28:30 +00:00
BMI GETR1 ; wait for MSB=0
2017-12-03 21:18:37 +00:00
PHA
LDA #DUMMY
STA DATA,X ; send another dummy
PLA ; restore R1
RTS
;*******************************
;
2017-12-17 19:21:30 +00:00
; Get R3 or R7
2017-12-03 21:18:37 +00:00
; R1 is in A
; R3 is in scratchpad ram
;
;*******************************
GETR3: JSR GETR1 ; get R1 first
PHA ; save R1
2019-06-16 15:40:48 +00:00
.IFPC02
2017-12-03 21:18:37 +00:00
PHY ; save Y
2019-06-16 15:40:48 +00:00
.ELSE
TYA
PHA
.ENDIF
2017-12-03 21:18:37 +00:00
LDY #04 ; load counter
2017-12-17 19:21:30 +00:00
JMP @WAIT ; first byte is already there
2017-12-03 21:18:37 +00:00
@LOOP: LDA #DUMMY ; send dummy
STA DATA,X
2021-05-09 12:03:37 +00:00
@WAIT: LDA CTRL,X ; TC is in N
2017-12-03 21:18:37 +00:00
BPL @WAIT
2021-05-09 12:03:37 +00:00
2017-12-03 21:18:37 +00:00
LDA DATA,X
PHA
DEY
BNE @LOOP ; do 4 times
LDY SLOT
PLA
STA R33,Y ; save R3
PLA
STA R32,Y
PLA
STA R31,Y
PLA
2017-12-17 19:21:30 +00:00
STA R30,Y ; R30 is MSB
2019-06-16 15:40:48 +00:00
.IFPC02
2017-12-03 21:18:37 +00:00
PLY ; restore Y
2019-06-16 15:40:48 +00:00
.ELSE
PLA
TAY
.ENDIF
2017-12-03 21:18:37 +00:00
LDA #DUMMY
STA DATA,X ; send another dummy
PLA ; restore R1
RTS
;*******************************
;
; Calculate block address
; Unit number is in $43 DSSS0000
; Block no is in $46-47
; Address is in R30-R33
;
;*******************************
2019-06-16 15:40:48 +00:00
GETBLOCK: .IFPC02
PHX ; save X
2017-12-03 21:18:37 +00:00
PHY ; save Y
2019-06-16 15:40:48 +00:00
.ELSE
TXA
PHA ; save X
TYA
PHA ; save y
.ENDIF
2018-05-13 15:58:41 +00:00
LDX SLOT ; SLOT is now in X
2018-05-22 19:53:11 +00:00
LDY SLOT16
2018-05-13 15:58:41 +00:00
LDA BLOCKNUM ; store block num
2017-12-03 21:18:37 +00:00
STA R33,X ; in R30-R33
2018-05-13 15:58:41 +00:00
LDA BLOCKNUM+1
2017-12-03 21:18:37 +00:00
STA R32,X
2019-06-16 15:40:48 +00:00
.IFPC02
2017-12-17 20:27:04 +00:00
STZ R31,X
STZ R30,X
2019-06-16 15:40:48 +00:00
.ELSE
LDA #0
STA R31,X
STA R30,X
.ENDIF
2017-12-03 21:18:37 +00:00
2018-05-22 19:53:11 +00:00
TYA ; get SLOT16
2018-05-13 15:58:41 +00:00
EOR DSNUMBER
AND #$70 ; check only slot bits
BEQ @DRIVE ; it is our slot
LDA #2 ; it is a phantom slot
2017-12-03 21:18:37 +00:00
STA R31,X
2018-05-13 15:58:41 +00:00
@DRIVE: BIT DSNUMBER ; drive number
BPL @SDHC ; D1
LDA R31,X ; D2
2019-06-16 15:40:48 +00:00
.IFPC02
2017-12-17 20:27:04 +00:00
INC A
2019-06-16 15:40:48 +00:00
.ELSE
ADC #1
.ENDIF
2017-12-17 20:27:04 +00:00
STA R31,X
2017-12-03 21:18:37 +00:00
@SDHC: LDA #SDHC
AND SS,Y ; if card is SDHC,
BNE @END ; use block addressing
LDY #9 ; ASL can't be used with Y
@LOOP: ASL R33,X ; mul block num
ROL R32,X ; by 512 to get
ROL R31,X ; real address
ROL R30,X
DEY
BNE @LOOP
2019-06-16 15:40:48 +00:00
@END: .IFPC02
PLY ; restore Y
2017-12-03 21:18:37 +00:00
PLX ; restore X
2019-06-16 15:40:48 +00:00
.ELSE
PLA
TAY ; restore y
PLA
TAX ; restore x
.ENDIF
2017-12-03 21:18:37 +00:00
RTS
;*******************************
;
; Send SD command
2019-06-16 15:40:48 +00:00
; X must contain SLOT16
2017-12-03 21:18:37 +00:00
; Cmd is in A
;
2019-06-16 15:40:48 +00:00
; Y is destroyed
;
2017-12-03 21:18:37 +00:00
;*******************************
2019-06-16 15:40:48 +00:00
COMMAND: LDY SLOT
2017-12-03 21:18:37 +00:00
STA DATA,X ; send command
LDA R30,Y ; get arg from R30 on
STA DATA,X
LDA R31,Y
STA DATA,X
LDA R32,Y
STA DATA,X
LDA R33,Y
STA DATA,X
LDA #DUMMY
STA DATA,X ; dummy crc
JSR GETR1
RTS
;*******************************
;
; Check for card detect
2018-05-28 22:36:39 +00:00
; X must contain SLOT16
2017-12-03 21:18:37 +00:00
;
; C Clear - card in slot
; Set - no card in slot
;
;*******************************
CARDDET: PHA
LDA #CD ; 0: card in
2019-06-16 15:40:48 +00:00
AND SS,X ; 1: card out
2017-12-03 21:18:37 +00:00
CLC
BEQ @DONE ; card is in
SEC ; card is out
@DONE: PLA
RTS
;*******************************
;
; Check for write protect
2018-05-28 22:36:39 +00:00
; X must contain SLOT16
2017-12-03 21:18:37 +00:00
;
; C Clear - card not protected
; Set - card write protected
;
;*******************************
WRPROT: PHA
LDA #WP ; 0: write enabled
2019-06-16 15:40:48 +00:00
AND SS,X ; 1: write disabled
2017-12-03 21:18:37 +00:00
CLC
BEQ @DONE
SEC
@DONE: PLA
RTS
2020-06-04 16:06:07 +00:00
;*******************************
;
; Check if card is initialized
; X must contain SLOT16
;
; C Clear - card initialized
; Set - card not initialized
;
;*******************************
INITED: PHA
LDA #CARD_INIT ; 0: card not initialized
2021-04-18 09:27:11 +00:00
AND SS,X ; 1: card initialized
2020-06-04 16:06:07 +00:00
CLC
BNE @DONE
SEC
@DONE: PLA
RTS