Skip to content

Commit

Permalink
Add new MMC_MemoryMapped driver (for BeebFPGA)
Browse files Browse the repository at this point in the history
Change-Id: I3046b2d5f81e3a940a4200d889e304a18900551f
  • Loading branch information
hoglet67 committed Dec 21, 2023
1 parent e2a4ec9 commit 82c7b3b
Show file tree
Hide file tree
Showing 5 changed files with 294 additions and 37 deletions.
38 changes: 19 additions & 19 deletions src/MMC.asm
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ write_block =&58


;; **** Set-up MMC command sequence ****
;; C=0 for read, C=1 for write
;; C=0 for read, C=1 for write
.MMC_SetupRW
LDA #write_block
BCS MMC_SetCommand
LDA #read_single_block

;; **** Reset MMC Command Sequence ****
;; A=cmd, token=&FF

Expand Down Expand Up @@ -125,10 +125,10 @@ write_block =&58
JMP iok

.isdhc
JSR UP_ReadByteX
JSR UP_ReadByteX
JSR UP_ReadByteX
JSR UP_ReadByteX
JSR MMC_GetByte
JSR MMC_GetByte
JSR MMC_GetByte
JSR MMC_GetByte
.isdhc2
LDA #&77
JSR MMC_SetCommand
Expand All @@ -145,12 +145,12 @@ write_block =&58
JSR MMC_DoCommand
CMP #&00
BNE ifail
JSR UP_ReadByteX
JSR MMC_GetByte
AND #&40
PHA
JSR UP_ReadByteX
JSR UP_ReadByteX
JSR UP_ReadByteX
JSR MMC_GetByte
JSR MMC_GetByte
JSR MMC_GetByte
PLA
BNE iok
LDA #2
Expand Down Expand Up @@ -217,7 +217,7 @@ write_block =&58
EQUB &CD
EQUS "Card?"
EQUB &00

}

;; Translate the sector number into a SPI Command Address
Expand Down Expand Up @@ -257,9 +257,9 @@ write_block =&58
INY
DEX
BNE loop
}
}


.setAddressFromStack
{
;; Process the drive number
Expand All @@ -275,7 +275,7 @@ write_block =&58
ROL A
ROL A
AND #&07
CMP numdrives% ;; check against number of ADFS partitions found
CMP numdrives% ;; check against number of ADFS partitions found
BCS invalidDrive

ASL A ;; Shift into bits 4-2 to index the drive table
Expand All @@ -295,7 +295,7 @@ write_block =&58
INY
TYA
AND #&03
BNE addDriveOffset
BNE addDriveOffset

LDX #3 ;; sector number is 4 bytes
;;
Expand Down Expand Up @@ -393,7 +393,7 @@ write_block =&58
;; Partition entry 1 is 1CE
;; Partition entry 2 is 1DE
;; Partition entry 3 is 1EE

;; Partition entry has following structure
;; 00 = status (whether bootable)
;; 01-03 = CHS address of first absolute sector in partition
Expand All @@ -413,7 +413,7 @@ write_block =&58
DEX
BNE loop
STZ numdrives% ;; clear the number of drives

.testPartition
LDY #&04
LDA (datptr%),Y
Expand All @@ -427,7 +427,7 @@ write_block =&58
INX
INY
CPY #&0C
BNE copyLBA
BNE copyLBA
CPX #(MAX_DRIVES * 4)
BEQ done

Expand All @@ -449,7 +449,7 @@ write_block =&58
EQUB &CD
EQUS "No MBR!"
EQUB &00

.noADFS
JSR L8372
EQUB &CD
Expand Down
214 changes: 214 additions & 0 deletions src/MMC_MemoryMapped.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
;; ADFS MMC Card Driver for the Memory Mapped Interface in BeebFPGA
;; (C) 2023 David Banks


_MASTER_ =? TRUE

IF _MASTER_
mmc%=&FEDC
ELSE
mmc%=&FE1C
ENDIF

;; Shifting starts on a write to mmc%
;; On BeebFPGA it only takes 1us to complete

;; TODO: Is this used elsewhere ???

MACRO SHIFT_DELAY
NOP
NOP
NOP
ENDMACRO

;; RESET DEVICE
.MMC_DEVICE_RESET
RTS

;; Read byte (User Port)
;; Write FF
.MMC_GetByte
LDA #&FF
STA mmc%
SHIFT_DELAY
LDA mmc%
RTS


;; *** Send &FF to MMC Y times ***
;; Y=0=256
.MMC_16Clocks
LDY #2
.MMC_Clocks
{
LDA #&FF
.clk1
STA mmc%
SHIFT_DELAY
DEY
BNE clk1
RTS ; A=&FF, Y=0
}

;; *** Send command to MMC ***
;; On exit A=result, Z=result=0
.MMC_DoCommand
{
LDX #0
LDY #8
.dcmd1
LDA cmdseq%,X
STA mmc%
SHIFT_DELAY
INX
DEY
BNE dcmd1
LDX #&FF
\ Wait for response, Y=0
.wR1mm
STX mmc%
SHIFT_DELAY
LDA mmc%
BPL dcmdex
DEY
BNE wR1mm
CMP #0
.dcmdex
RTS
} ; A=result, X=X+8, Y=?


;; *** Wait for data token ***
.MMC_WaitForData
{
LDX #&FF
.wl1
STX mmc%
SHIFT_DELAY
LDA mmc%
CMP #&FE ;\ data token
BNE wl1
RTS ; A=&FE, X=&FF, Y unchanged
}


;; *** Read 256 bytes to datptr ***
.MMC_Read256
LDX #0

.MMC_ReadX
LDY #0
BIT &CD
BVS MMC_ReadToTube

.MMC_ReadToMemory
LDA #&FF ; Read byte
STA mmc%
SHIFT_DELAY
LDA mmc%
STA (datptr%),Y
LDA #&FF ; Skip dummy byte
STA mmc%
SHIFT_DELAY
INY
DEX
BNE MMC_ReadToMemory
RTS

.MMC_ReadToTube
LDA #&FF ; Read first byte
STA mmc%
SHIFT_DELAY
LDA mmc%
STA TUBE_R3_DATA
LDA #&FF ; Skip dummy byte
STA mmc%
SHIFT_DELAY
INY
DEX
BNE MMC_ReadToTube
RTS

;; **** Send Data Token to card ****
.MMC_SendingData
LDX #&FF
STX mmc%
SHIFT_DELAY
STX mmc%
SHIFT_DELAY
DEX
STX mmc%
SHIFT_DELAY
RTS

;; **** Complete Write Operation *****
.MMC_EndWrite
{
JSR MMC_16Clocks
LDX #&FF
STX mmc%
SHIFT_DELAY
LDA mmc%
TAY
AND #&1F
CMP #5
BNE error

LDA #&FF
.ew1
STX mmc%
SHIFT_DELAY
CMP mmc%
BNE ew1
RTS
.error
JMP errWrite
}

;; **** Write 256 bytes from dataptr% ****
.MMC_Write256
{
LDY #0
BIT &CD
BVS MMC_WriteFromTube
.MMC_WriteFromMemory
LDA (datptr%),Y ; Write byte
STA mmc%
SHIFT_DELAY
LDA #0 ; Write dummy byte
STA mmc%
SHIFT_DELAY
INY
BNE MMC_WriteFromMemory
RTS

.MMC_WriteFromTube
LDA TUBE_R3_DATA ; Write byte
STA mmc%
SHIFT_DELAY
LDA #0 ; Write dummy byte
STA mmc%
SHIFT_DELAY
INY
BNE MMC_WriteFromTube
RTS
}

;; *** Read 512 byte sector to datptr
.MMC_Read512
{
LDX #2
LDY #0
.loop
LDA #&FF
STA mmc%
SHIFT_DELAY
LDA mmc%
STA (datptr%),Y
INY
BNE loop
INC datptr%+1
DEX
BNE loop
RTS
}
Loading

0 comments on commit 82c7b3b

Please sign in to comment.