; replace ZRBLK with code to read block from CF card.

; NOTE; in progress NOT executable code.

; 1 how big is a block? 512 bytes
; 2 is the addressing compatible? yes either LBA or CHS
; 3 which registers does ZRBLK use? start address HL, finish address DE, sector
;   B, track, C
; 4 which registers do we need to send to OUT (C),r to set card to give us data?
; Base port ($80)     Write data
; +1                  Features
; +2                  Sector count
; +3                  Sector no.
; +4                  Cylinder High no.
; +5                  Cylinder Low no.
; +6                  Select Head no.
; +7                  Command
; 5 which registers does INIR use? start address HL, byte counter B, port C
;   (16bit data comes in through port $80 High/Low)
;
;  For testing the other registers and memory would need to be set as in EFL ie.
;  MemLoad:              DW 0300H
;  LD (MemLoad)
;
;PROCEDURE
;---------

;1. Set referances to ports (base port for any IDE adaptor may vary, but
;   offsets are the same)
  CF_Data_Port          EQU $80
  CF_Error_Features     EQU (CF_Data_Port+1)
  CF_Sector_Count       EQU (CF_Data_Port+2)
  CF_Sector_Number      EQU (CF_Data_Port+3)
  CF_Cylinder_Low       EQU (CF_Data_Port+4)
  CF_Cylinder_High      EQU (CF_Data_Port+5)
  CF_Select_Head        EQU (CF_Data_Port+6)
  CF_Status_Command     EQU (CF_Data_Port+7)

;2. check card ready by reading status from port CF_port +7 and comparing it to
;   80 (binary 01010000)
CFReadBlock:    PUSH BC
                LD C,CF_Status_Command  ;load C with address of status port
                IN A,(C)                ;read status data into A
                CP 80                   ;compare status bits with 0101000
                JP Z,CFBlockRead2       ;if so continue
                RET                     ;else return
;3. send sector, track, head and read command to card
CFReadBlock2:   LD A,B
                OUT (CF_Sector_Number),A  ;send sector number (B) to CF card
                LD A,C
                OUT (CF_Cylinder_Low),A   ;send track number (C) to CF card
                LD A,$A0
                OUT (CF_Select_Head),A    ;send head number $A0 (top stop card 
                                          ;using LBA mode, smallest nibble selects head)
                LD A,$20
                OUT (CF_Status_Command),A ;send READ command to CF card
;4. read 2 chunks of 256 bytes in from port $80
CFReadBlock3:   LD C,CF_Data_Port         ;select port to read from (reg. C=port no.)
                LD HL,(MemLoad)           ;get memory address to write block to
                LD B,255                  ;set counter to 255 (INIR counts with B not BC)
                INIR                      ;get 255 bytes and write to (MemLoad)
                                          ;in the WRONG H/L order
                LD B,255                  ;set counter to 255
                INIR                      ;get second half of block
                                          ;in the WRONG H/L order
                RET

; NOTE: the above routine is a concept IDE replacement for ZRBLK
;       at the moment it doesn't reverse the high/low byte order from the
;       CF card. Bytes sent H/L from card and written into memory in ascending
;       order and neeed to be swapped.

;       CF card will send 512 bytes per read command. If multiples are needed
;       then there are several other commands that might be more efficent
;
;       For a better adaptor we could read and write H/L to the ports in the
;       correct order and let the adaptor take care of things. Or use another
;       port to cycle the DMACK or CS0(??) pins on the card to give us 16bits
;       of data per cycle. but It's all the same in the end.


