-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DOS/FAT32] enable selection of auto_tx and the fast write loop via U0>B #306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
.include "lib.inc" | ||
.include "sdcard.inc" | ||
|
||
.export sector_buffer, sector_buffer_end, sector_lba | ||
.export sector_buffer, sector_buffer_end, sector_lba, sdcard_set_fast_mode | ||
|
||
.bss | ||
cmd_idx = sdcard_param | ||
|
@@ -22,14 +22,11 @@ sdcard_param: | |
sector_lba: | ||
.res 4 ; dword (part of sdcard_param) - LBA of sector to read/write | ||
.res 1 | ||
sd_fast: | ||
.res 1 ; Bitmask: WR------ where R = auto_tx and W = fast writes | ||
|
||
timeout_cnt: .byte 0 | ||
|
||
; XXX disabled for now; on real hardware, this returns | ||
; XXX all 0xFE bytes with all tested SD cards | ||
;FAST_READ=1 | ||
;FAST_WRITE=1 | ||
|
||
.code | ||
|
||
;----------------------------------------------------------------------------- | ||
|
@@ -329,8 +326,39 @@ sdcard_read_sector: | |
clc | ||
rts | ||
|
||
.ifdef FAST_READ | ||
@start: ; Enable auto-tx mode | ||
@start: | ||
bit sd_fast | ||
bvs @fast | ||
|
||
@slow: | ||
; Read 512 bytes of sector data | ||
ldx #$FF | ||
ldy #0 | ||
@3: stx SPI_DATA ; 4 | ||
@4: bit SPI_CTRL ; 4 | ||
bmi @4 ; 2 + 1 if branch | ||
|
||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 0, y | ||
iny | ||
bne @3 | ||
|
||
; Y already 0 at this point | ||
@5: stx SPI_DATA ; 4 | ||
@6: bit SPI_CTRL ; 4 | ||
bmi @6 ; 2 + 1 if branch | ||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 256, y | ||
iny | ||
bne @5 | ||
|
||
; Read CRC bytes | ||
jsr spi_read | ||
jsr spi_read | ||
|
||
jmp @after | ||
@fast: | ||
; Enable auto-tx mode | ||
lda SPI_CTRL | ||
ora #SPI_CTRL_AUTOTX | ||
sta SPI_CTRL | ||
|
@@ -341,7 +369,7 @@ sdcard_read_sector: | |
|
||
; Efficiently read first 256 bytes (hide SPI transfer time) | ||
ldy #0 ; 2 | ||
@3: lda SPI_DATA ; 4 | ||
@3f: lda SPI_DATA ; 4 | ||
sta sector_buffer + 0, y ; 5 | ||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 1, y ; 5 | ||
|
@@ -361,10 +389,10 @@ sdcard_read_sector: | |
clc ; 2 | ||
adc #8 ; 2 | ||
tay ; 2 | ||
bne @3 ; 2+1 | ||
bne @3f ; 2+1 | ||
|
||
; Efficiently read second 256 bytes (hide SPI transfer time) | ||
@4: lda SPI_DATA ; 4 | ||
@4f: lda SPI_DATA ; 4 | ||
sta sector_buffer + 256 + 0, y ; 5 | ||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 256 + 1, y ; 5 | ||
|
@@ -384,7 +412,7 @@ sdcard_read_sector: | |
clc ; 2 | ||
adc #8 ; 2 | ||
tay ; 2 | ||
bne @4 ; 2+1 | ||
bne @4f ; 2+1 | ||
|
||
; Disable auto-tx mode | ||
lda SPI_CTRL | ||
|
@@ -393,33 +421,7 @@ sdcard_read_sector: | |
|
||
; Next read is now already done (first CRC byte), read second CRC byte | ||
jsr spi_read | ||
|
||
.else | ||
@start: ; Read 512 bytes of sector data | ||
ldx #$FF | ||
ldy #0 | ||
@3: stx SPI_DATA ; 4 | ||
@4: bit SPI_CTRL ; 4 | ||
bmi @4 ; 2 + 1 if branch | ||
|
||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 0, y | ||
iny | ||
bne @3 | ||
|
||
; Y already 0 at this point | ||
@5: stx SPI_DATA ; 4 | ||
@6: bit SPI_CTRL ; 4 | ||
bmi @6 ; 2 + 1 if branch | ||
lda SPI_DATA ; 4 | ||
sta sector_buffer + 256, y | ||
iny | ||
bne @5 | ||
|
||
; Read CRC bytes | ||
jsr spi_read | ||
jsr spi_read | ||
.endif | ||
@after: | ||
; Success | ||
jsr deselect | ||
sec | ||
|
@@ -448,22 +450,24 @@ sdcard_write_sector: | |
lda #$FE | ||
jsr spi_write | ||
|
||
.ifdef FAST_WRITE | ||
bit sd_fast | ||
bpl @slow | ||
; Send 512 bytes of sector data | ||
; NOTE: Direct access of SPI registers to speed up. | ||
; Make sure 9 CPU clock cycles take longer than 640 ns (eg. CPU max 14MHz) | ||
ldy #0 | ||
@1: lda sector_buffer, y ; 4 | ||
@1f: lda sector_buffer, y ; 4 | ||
sta SPI_DATA ; 4 | ||
iny ; 2 | ||
bne @1 ; 2 + 1 | ||
bne @1f ; 2 + 1 | ||
|
||
; Y already 0 at this point | ||
@2: lda sector_buffer + 256, y ; 4 | ||
@2f: lda sector_buffer + 256, y ; 4 | ||
sta SPI_DATA ; 4 | ||
iny ; 2 | ||
bne @2 ; 2 + 1 | ||
.else | ||
bne @2f ; 2 + 1 | ||
bra @after | ||
@slow: | ||
; Send 512 bytes of sector data | ||
ldy #0 | ||
@1: lda sector_buffer, y ; 4 | ||
|
@@ -476,7 +480,7 @@ sdcard_write_sector: | |
spi_write_macro | ||
iny ; 2 | ||
bne @2 ; 2 + 1 | ||
.endif | ||
@after: | ||
; Dummy CRC | ||
lda #0 | ||
jsr spi_write | ||
|
@@ -558,3 +562,24 @@ sdcard_check_alive: | |
jsr deselect | ||
plp | ||
rts | ||
|
||
;----------------------------------------------------------------------------- | ||
; sdcard_set_fast_mode | ||
; | ||
; .A = 0: fast mode off | ||
; .A = 1: fast reads | ||
; .A = 2: fast writes | ||
; .A = 3: fast reads and writes | ||
;----------------------------------------------------------------------------- | ||
sdcard_set_fast_mode: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something for possible consideration: there could be a situation where for some reason the user's system cannot tolerate fast read or write (e.g. the system is overclocked or we enable a slower SPI bus to a Wi-Fi peripheral). It might be good to have a hard override that prevents setting fast reads or writes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you see this working? For now, the variable gets initialized to 0 at boot (with the most recent push) and the user has to explicitly change it. |
||
pha | ||
lda #$c0 | ||
trb sd_fast | ||
pla | ||
and #3 | ||
lsr | ||
ror | ||
ror | ||
tsb sd_fast | ||
sec | ||
rts |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,4 @@ fat32_set_time = $C066 | |
sdcard_init = $C069 | ||
sdcard_check_alive = $C06C | ||
|
||
sdcard_set_fast_mode = $C06F |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: since fast is hoped to eventually be the default, perhaps the branch should go to slow and fast should be a cycle faster?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The branch would be too far to jump over the fast routine, so nothing would be saved here.