1;
2;	ZX IF1 & Microdrive functions
3;
4;	int if1_write_sector (int drive, int sector, struct M_CHAN buffer);
5;
6;	Writes the specified sector to the specified drive.
7;
8;
9;	$Id: if1_write_sector.asm,v 1.3 2016-07-01 22:08:20 dom Exp $
10;
11
12		SECTION code_clib
13		PUBLIC 	if1_write_sector
14		PUBLIC 	_if1_write_sector
15
16		EXTERN 	if1_rommap
17
18		EXTERN	MAKE_M
19		EXTERN	MOTOR
20
21
22if1_write_sector:
23_if1_write_sector:
24
25		call	if1_rommap
26		push	ix	;save callers
27		ld	ix,4
28		add	ix,sp
29		ld	a,(ix+4)
30		ld	hl,-1
31		and	a		; drive no. = 0 ?
32		jr	z,write_sector_exit		; yes, return -1
33		dec	a
34		cp	8		; drive no. >8 ?
35		jr	nc,write_sector_exit		; yes, return -1
36		inc	a
37		push	af
38
39		ld	a,(ix+2)	; sector number
40		push	af
41
42		ld	l,(ix+0)	; buffer
43		ld	h,(ix+1)
44		ld	(mdvbuffer+1),hl  ; Self modifying code  :oP
45
46		call	MAKE_M
47
48		pop	af		; sector number
49		ld	(ix+0Dh),a	; CHREC
50
51		pop	af
52		ld	(ix+19h),A	; CHDRIV
53
54		call	MOTOR
55		ld	hl,-1
56IF !OLDIF1MOTOR
57		jr	nz,write_sector_exit		; microdrive not present
58ENDIF
59		in	a,($ef)
60		and	1		; test the write-protect tab
61		jr	z,write_sector_exit		; drive 'write' protected
62
63		push	ix
64		pop	hl
65		ld	de,37h		; point to 12 bytes of data block preamble
66		add	hl,de
67
68		push	hl
69mdvbuffer:	ld	hl,0
70		add	hl,de
71		pop	de
72
73		ld	bc,21ch
74		ldir
75
76		set	0,(ix+18h)	; set CHFLAG to "write" mode
77		call	1
78
79		RST	8
80		defb	22h		; Open a temp "M" channel
81
82		rst	8
83		defb	2Ah		; Write a sector to drive
84
85		xor	a
86		rst	8
87		defb	21h		; Switch microdrive motor off (a=0)
88
89		RST	8
90		defb	2Ch		; Reclaim an "M" channel
91
92		ld	hl,0
93write_sector_exit:
94		pop	ix
95		ret
96