;--------------------------------------------------------
; File Created by SDCC : free open source ISO C Compiler 
; Version 4.4.0 #14620 (MINGW32)
;--------------------------------------------------------
	.module diskio
	.optsdcc -mz80
	
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
	.globl _bcd2bin
	.globl _rtc_fitted
	.globl _getrtc
	.globl _ch375_disk_capacity
	.globl _ch375_write
	.globl _ch375_read
	.globl _ch375_probe
	.globl _disk_status
	.globl _disk_initialize
	.globl _disk_read
	.globl _disk_write
	.globl _disk_ioctl
	.globl _get_fattime
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
;--------------------------------------------------------
; ram data
;--------------------------------------------------------
	.area _DATA
;--------------------------------------------------------
; ram data
;--------------------------------------------------------
	.area _INITIALIZED
;--------------------------------------------------------
; absolute external ram data
;--------------------------------------------------------
	.area _DABS (ABS)
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
	.area _HOME
	.area _GSINIT
	.area _GSFINAL
	.area _GSINIT
;--------------------------------------------------------
; Home
;--------------------------------------------------------
	.area _HOME
	.area _HOME
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area _CODE
;diskio.c:28: DSTATUS disk_status (
;	---------------------------------
; Function disk_status
; ---------------------------------
_disk_status::
;diskio.c:34: switch (pdrv) {
;diskio.c:41: return stat;
	or	a,a
;diskio.c:42: }
	ret	Z
;diskio.c:43: return STA_NOINIT;
	ld	a, #0x01
;diskio.c:44: }
	ret
;diskio.c:52: DSTATUS disk_initialize (
;	---------------------------------
; Function disk_initialize
; ---------------------------------
_disk_initialize::
;diskio.c:59: switch (pdrv) {
	or	a, a
	jp	Z,_ch375_probe
;diskio.c:64: return( ch375_probe());
;diskio.c:65: }
;diskio.c:66: return STA_NOINIT;
	ld	a, #0x01
;diskio.c:67: }
	ret
;diskio.c:75: DRESULT disk_read (
;	---------------------------------
; Function disk_read
; ---------------------------------
_disk_read::
	ld	c, e
	ld	b, d
;diskio.c:83: switch (pdrv) {
	or	a, a
	jr	NZ, 00102$
;diskio.c:90: return ch375_read(buff, sector, count);
;	spillPairReg hl
;	spillPairReg hl
	ld	hl, #6
	add	hl, sp
;	spillPairReg hl
;	spillPairReg hl
	ld	e, (hl)
	inc	hl
	ld	d, (hl)
	push	de
	ld	iy, #4
	add	iy, sp
;	spillPairReg hl
;	spillPairReg hl
	ld	l, 2 (iy)
	ld	h, 3 (iy)
;	spillPairReg hl
;	spillPairReg hl
	push	hl
	ld	a, 0 (iy)
	ld	h, 1 (iy)
;	spillPairReg hl
;	spillPairReg hl
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	push	hl
	ld	l, c
;	spillPairReg hl
;	spillPairReg hl
	ld	h, b
;	spillPairReg hl
;	spillPairReg hl
	call	_ch375_read
	jr	00103$
;diskio.c:91: }
00102$:
;diskio.c:93: return RES_PARERR;
	ld	a, #0x04
00103$:
;diskio.c:94: }
	pop	hl
	pop	bc
	pop	bc
	pop	bc
	jp	(hl)
;diskio.c:104: DRESULT disk_write (
;	---------------------------------
; Function disk_write
; ---------------------------------
_disk_write::
	ld	c, e
	ld	b, d
;diskio.c:112: switch (pdrv) {
	or	a, a
	jr	NZ, 00102$
;diskio.c:117: return ch375_write(buff, sector, count);
;	spillPairReg hl
;	spillPairReg hl
	ld	hl, #6
	add	hl, sp
;	spillPairReg hl
;	spillPairReg hl
	ld	e, (hl)
	inc	hl
	ld	d, (hl)
	push	de
	ld	iy, #4
	add	iy, sp
;	spillPairReg hl
;	spillPairReg hl
	ld	l, 2 (iy)
	ld	h, 3 (iy)
;	spillPairReg hl
;	spillPairReg hl
	push	hl
	ld	a, 0 (iy)
	ld	h, 1 (iy)
;	spillPairReg hl
;	spillPairReg hl
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	push	hl
	ld	l, c
;	spillPairReg hl
;	spillPairReg hl
	ld	h, b
;	spillPairReg hl
;	spillPairReg hl
	call	_ch375_write
	jr	00103$
;diskio.c:119: }
00102$:
;diskio.c:121: return RES_PARERR;
	ld	a, #0x04
00103$:
;diskio.c:122: }
	pop	hl
	pop	bc
	pop	bc
	pop	bc
	jp	(hl)
;diskio.c:131: DRESULT disk_ioctl (
;	---------------------------------
; Function disk_ioctl
; ---------------------------------
_disk_ioctl::
	push	ix
	ld	ix,#0
	add	ix,sp
	push	af
	push	af
	ld	c, l
;diskio.c:138: switch (pdrv) {
	or	a, a
	jr	NZ, 00108$
;diskio.c:141: switch (cmd)
	ld	a, #0x04
	sub	a, c
	jr	C, 00108$
;diskio.c:147: *((DWORD *)buff) = ch375_disk_capacity();
	ld	e, 4 (ix)
	ld	d, 5 (ix)
;diskio.c:141: switch (cmd)
	ld	b, #0x00
	ld	hl, #00127$
	add	hl, bc
	add	hl, bc
	add	hl, bc
	jp	(hl)
00127$:
	jp	00102$
	jp	00103$
	jp	00104$
	jp	00105$
	jp	00106$
;diskio.c:143: case CTRL_SYNC:
00102$:
;diskio.c:144: return RES_OK;
	xor	a, a
	jr	00109$
;diskio.c:146: case GET_SECTOR_COUNT:
00103$:
;diskio.c:147: *((DWORD *)buff) = ch375_disk_capacity();
	push	de
	call	_ch375_disk_capacity
	ld	-4 (ix), e
	ld	-3 (ix), d
	ld	-2 (ix), l
	ld	-1 (ix), h
	pop	de
	ld	hl, #0
	add	hl, sp
	ld	bc, #0x0004
	ldir
;diskio.c:148: return RES_OK;
	xor	a, a
	jr	00109$
;diskio.c:150: case GET_SECTOR_SIZE:
00104$:
;diskio.c:151: *((WORD *)buff) = 512;
	ld	l, 4 (ix)
;	spillPairReg hl
;	spillPairReg hl
	ld	h, 5 (ix)
;	spillPairReg hl
;	spillPairReg hl
	ld	(hl), #0x00
	inc	hl
	ld	(hl), #0x02
;diskio.c:152: return RES_OK;
	xor	a, a
	jr	00109$
;diskio.c:154: case GET_BLOCK_SIZE:
00105$:
;diskio.c:155: *((DWORD *)buff) = 1;
	ld	a, #0x01
	ld	(de), a
	inc	de
	xor	a, a
	ld	(de), a
	inc	de
	ld	(de), a
	inc	de
	ld	(de), a
;diskio.c:156: return RES_OK;
	xor	a, a
	jr	00109$
;diskio.c:158: case CTRL_TRIM:
00106$:
;diskio.c:159: return RES_OK;
	xor	a, a
	jr	00109$
;diskio.c:163: }
00108$:
;diskio.c:165: return RES_PARERR;
	ld	a, #0x04
00109$:
;diskio.c:166: }
	ld	sp, ix
	pop	ix
	pop	hl
	pop	bc
	jp	(hl)
;diskio.c:168: BYTE bcd2bin(BYTE val)
;	---------------------------------
; Function bcd2bin
; ---------------------------------
_bcd2bin::
;diskio.c:170: return (((val >> 4) * 10) + (val & 0x0F));
	ld	b, a
	rlca
	rlca
	rlca
	rlca
	and	a, #0x0f
	ld	e, a
	add	a, a
	add	a, a
	add	a, e
	add	a, a
	ld	c, a
	ld	a, b
	and	a, #0x0f
	add	a, c
;diskio.c:171: }
	ret
;diskio.c:173: DWORD get_fattime (void)
;	---------------------------------
; Function get_fattime
; ---------------------------------
_get_fattime::
	call	___sdcc_enter_ix
	ld	hl, #-22
	add	hl, sp
	ld	sp, hl
;diskio.c:178: if(rtc_fitted())
	call	_rtc_fitted
	or	a, a
	jp	Z, 00102$
;diskio.c:180: getrtc(&timestamp);
	ld	hl, #0
	add	hl, sp
	call	_getrtc
;diskio.c:184: fattime |= (((DWORD)bcd2bin(timestamp.year) + 20) << 25);
	ld	a, -22 (ix)
	call	_bcd2bin
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	ld	h, #0x00
;	spillPairReg hl
;	spillPairReg hl
	push	de
	ld	de, #0x0014
	add	hl, de
	pop	de
	jr	NC, 00113$
00113$:
	ld	-5 (ix), l
	xor	a, a
	ld	-8 (ix), a
	ld	-7 (ix), a
	ld	-6 (ix), a
	sla	-5 (ix)
;diskio.c:185: fattime |= ((DWORD)bcd2bin(timestamp.month) << 21);
	ld	a, -21 (ix)
	call	_bcd2bin
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	ld	h, #0x00
;	spillPairReg hl
;	spillPairReg hl
	ld	de, #0x0000
	ld	b, #0x05
00116$:
	add	hl, hl
	djnz	00116$
	ld	-4 (ix), e
	ld	-3 (ix), d
	ld	-2 (ix), l
	ld	a, -5 (ix)
	or	a, h
	ld	-1 (ix), a
;diskio.c:186: fattime |= ((DWORD)bcd2bin(timestamp.day) << 16);
	ld	a, -20 (ix)
	call	_bcd2bin
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
;	spillPairReg hl
;	spillPairReg hl
	ld	bc, #0x0000
	ld	a, -2 (ix)
	or	a, l
	ld	e, a
	ld	d, -1 (ix)
;diskio.c:187: fattime |= ((WORD)bcd2bin(timestamp.time.h) << 11);
	ld	l, -18 (ix)
;	spillPairReg hl
;	spillPairReg hl
	push	bc
	push	de
	ld	a, l
	call	_bcd2bin
	pop	de
	pop	bc
;	spillPairReg hl
;	spillPairReg hl
;	spillPairReg hl
;	spillPairReg hl
	add	a, a
	add	a, a
	add	a, a
	ld	h, a
;	spillPairReg hl
;	spillPairReg hl
	ld	l, #0x00
;	spillPairReg hl
;	spillPairReg hl
	push	hl
	pop	iy
	ld	hl, #0x0000
	push	iy
	pop	bc
	ld	a, e
	or	a, l
	ld	e, a
;diskio.c:188: fattime |= ((WORD)bcd2bin(timestamp.time.m) << 5);
	ld	l, -17 (ix)
;	spillPairReg hl
;	spillPairReg hl
	push	bc
	push	de
	ld	a, l
	call	_bcd2bin
	pop	de
	pop	bc
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	ld	h, #0x00
;	spillPairReg hl
;	spillPairReg hl
	add	hl, hl
	add	hl, hl
	add	hl, hl
	add	hl, hl
	add	hl, hl
	ld	c, l
	ld	a, b
	or	a, h
	ld	b, a
;diskio.c:189: fattime |= ((WORD)bcd2bin(timestamp.time.s) >> 1);
	ld	l, -16 (ix)
;	spillPairReg hl
;	spillPairReg hl
	push	bc
	push	de
	ld	a, l
	call	_bcd2bin
	pop	de
	pop	bc
	ld	l, a
;	spillPairReg hl
;	spillPairReg hl
	ld	h, #0x00
;	spillPairReg hl
;	spillPairReg hl
	srl	h
	rr	l
	ld	a, c
	or	a, l
	ld	c, a
	jr	00103$
00102$:
;diskio.c:199: fattime |= ((WORD)3 >> 1);
	ld	bc, #0x0841
	ld	de, #0x2821
00103$:
;diskio.c:202: return fattime;
	ex	de, hl
	ld	e, c
	ld	d, b
;diskio.c:203: }
	ld	sp, ix
	pop	ix
	ret
	.area _CODE
	.area _INITIALIZER
	.area _CABS (ABS)
