COMPACT FLASH IF PROGRAMMING REFERENCE - LBA MODE

 Port addresses for CF interface:

  Data register :   #2B
  Parameter reg.:   #2F
  Sector count r.:  #6B
  Start sector r.:  #6F
  Cylinder low  :   #AB
  Cylinder high :   #AF
  Head reg.     :   #EB
  Command/status:   #EF

 Example routines for read & write:

;I/O port addresses
DAT EQU #2B
PAR EQU #2F
SEC EQU #6B
STA EQU #6F
ZYL EQU #AB
ZYH EQU #AF
HEA EQU #EB
COM EQU #EF

;Enter parameters for Read & Write: HL address, BC start sector,
;A sector count, DE offset (in 256 sectors step - up to 8GB addessable)
;Out parameters: If zero flag set - OK , else error

REABE      CALL REABL ;block read with error test. Entry point.
ERRT      ;error testing
           EI
           RET Z  ;return to caller if no error
           IN A,(PAR)
           BIT 6,A
           JR NZ,ECCE
           RST 8
           DEFB #1E ;Disk error - new error message code, this is an example & requires changes in ROM.
ECCE       RST 8
           DEFB #1F  ;ECC error...
    
REABL      CALL CALC
           LD A,#20 ;read command
           OUT (COM),A
CWR        CALL WAIDR
SELL       INI
           INI
           INI
           INI
           INI
           INI
           INI
           INI ;8 bytes in 1 pass
           DJNZ SELL
           IN A,(COM)
           BIT 0,A  ;is error ?
           RET NZ
           DEC E  ;decrease sector counter
           JR NZ,CWR
           RET ;with zero flag set - no error

WRIBE      CALL WRIBL ;Block write with error test. Entry point.
           JR ERRT
WRIBL      CALL CALC
           LD A,#30  ;write command
           OUT (COM),A
WRIT       CALL WAIDR
SWRL 
           OUTI
           OUTI
           OUTI
           OUTI
           OUTI
           OUTI
           OUTI
           OUTI
           DJNZ SWRL
           IN A,(COM)
           BIT 0,A
           RET NZ
           DEC E
           JR NZ,WRIT
           RET

WAIDR      LD BC,50000 ;time out counter prepare
WAIDL      IN A,(COM)
           BIT 3,A ; ready ?
           JR NZ,SET64
           DEC BC
           LD A,B
           OR C
           JR NZ,WAIDL
           POP DE  ;drop call.
           INC A ; reset zero flag
           RET  ;time out
SET64      LD BC,#402B  ;set port address (C) & loop counter : 8x#40=512
           RET

CALC       OR A ;avoids zero sector count - it transfers 256 sectors!
           JR NZ,CONTIN
           RST 8
           DEFB #19 ; parameter error
CONTIN     DI
           PUSH HL
           PUSH DE
           LD E,A
ISBYS IN A,(COM)
           BIT 7,A
           JR NZ,ISBYS ; Wait until not busy
           LD A,#E0 ; LBA mode set, bits 24-27 always zero, still up to 8GB
           OUT (HEA),A
           POP HL
           LD A,H
           LD H,L
           LD L,0
           ADD HL,BC
           JR NC,NOCAR
           INC A ; Advance high byte
NOCAR OUT (ZYH),A
           LD A,H
           OUT (ZYL),A
           LD A,L
           OUT (STA),A
           LD A,E
           OUT (SEC),A
           POP HL
           RET

;END

   With this routines transfer speed with Flash drives is about 150 KB per second by read and
 something less by write.
              
 

     PP