;!TODO сделать выбор рамдрайва по значению bit 0..3 регистра А
; например: A = #6E означает: 6 - RAM Drive, #E - диск E:
;
;[]===========================================================[]
;Function: Set Current Media Parameters
;        A - Disk
;        H - Heads
;        L - Sectors
;       DE - Cylinders
;       IX - Capacity sector in bytes
;        B - Flags
;Return: None
;[]===========================================================[]
;!FIXIT
RMD_5x_SETMED:
        and a
        ret
;        A - Disk
;        H - Heads
;        L - Sectors
;       DE - Cylinders
;       IX - Capacity sector in bytes
;        B - Flags
;        DB 'E' - 'A' = 4
;
;
;
;[]===========================================================[]
;Function: Get Current Media Parameters
;	A - Disk
;Return:
;	H - Heads
;	L - Sectors per cylinder
;      DE - Cylinders
;      IX - Capacity sector in bytes
;	B - Flags: ramblock ID
;[]===========================================================[]
RMD_5x_GETMED:
.SectorSize     EQU     512                                             ;!HARDCODE
        AND     #0F
        CALL    GET_RAMD_ST                                             ; получение идентификатора блока
        RET     C
        SCF
        RET     Z
	;
	LD	C,A
	LD	B,#FF
.loop:	INC	B
	PUSH	BC
	LD	A,C
        CALL    EMM.GetMemPage                                          ; получить физический номер страницы из блока
	POP	BC
	JR	NC,.loop
	; CF==1, A==0 - ошибка, A==#FF - ok
	INC	A
	RET	NZ	; если выходит, то с флагами CF==1, ZF==1

        LD      E,B
        LD      B,C
	;  тут в E количество страниц в рамдиске, B - ID рамдиска
        LD      HL,1*256 + #4000/.SectorSize
        LD      D,0
        LD      IX,.SectorSize
        RET

////////////////////////////////////////////////////////////////////////////////////
RMD_5x_LONG_WRITE:
        LD      C,#FF
        JP      RAM_DRV_READ_WRITE
RMD_5x_LONG_READ:
        LD      C,0
        JP      RAM_DRV_READ_WRITE
RMD_5x_WRITE:
        LD      C,#FF
        JP      RAM_DRV_NO_LONG
RMD_5x_READ:
        LD      C,0
        ;JP      RAM_DRV_NO_LONG        
RAM_DRV_NO_LONG:
	EX	AF,AF'
	IN	A,(SLOT3)
	EX	AF,AF'
; НА входе рег С = 0 если чтение, либо #FF, если запись
RAM_DRV_READ_WRITE:
        PUSH    HL
        PUSH    AF
        LD      L,B
        IN      A,(SLOT3)
        LD      H,A

;        LD      A,H
;        OR      L
;        JR      NZ,.error_stack2        ; if sector_high > 0
;        POP     AF

        POP     AF
        EX      (SP),HL
        PUSH    AF

       LD      A,H
       OR      L
       JR      NZ,.error_stack2        ; if sector_high > 0
       POP     AF

	EX	AF,AF'
	OUT	(SLOT3),A
        LD      A,C                     ; в рег C команда чтения или записи
	EX	AF,AF'
; A' = command

        PUSH    BC                      ; Sector_counter
        PUSH    DE                      ; Address
        PUSH    IX                      ; Sector_low

        AND     #0F                     ;номер рамдиска
        ;LD      C,BIOS.GET_RAMD_ST
        ;RST_to_BIOS
        CALL    GET_RAMD_ST
; A = Memory Block ID

        POP     DE                      ; Sector_low
        POP     HL                      ; Address
        POP     BC                      ; Sector_counter
        JR      C,.error_stack1

        SLA     E
        RL      D                       ; Sector_low * 2
        JR      C,.error_stack1


        PUSH    BC
        SLA     B                       ; Sector_counter * 2    - типа у дискеты сектор 512 байтов
        JR      C,.error_stack2

        PUSH    DE
        //PUSH    BC
        ;LD      C,BIOS.BLK_RD_WR
        ;RST_to_BIOS
        CALL    BLK_RD_WR
        JR      C,.error_stack3
; hl = address + Sector_low * 256 * Sector_counter

        //POP     BC
        //SRL     B

        POP     DE
; de = Sector_low * 2

        SRL     D
        RR      E
; de = Sector_low

        POP     BC
; b  =  Sector_counter

        LD      A,E
        ADD     A,B
        LD      E,A
        LD      A,D
        ADC     A,0
        LD      D,A
; de = Sector_counter + Sector_low

        PUSH    DE
        POP     IX
; ix = Sector_counter + Sector_low

        EX      DE,HL
; de = address + Sector_low * 256 * Sector_counter

        LD      HL,0

        POP     AF
        OUT     (SLOT3),A
        AND     A
;Return:
;   HL:IX - Sector + Sector counter
;   DE    - Address + (Sector counter * Size sector)
        RET
.error_stack3:
        POP     BC
.error_stack2:
        POP     DE
.error_stack1:
        POP     AF
        OUT     (SLOT3),A
        SCF
        RET
////////////////////////////////////////////////////////////////////////////////////