TITLE	'CPM 3 COLD START LOADER FOR Z280 8/27/89'

MACLIB	Z80
MACLIB	Z280



; ###############################################

;
ORG	0100H
;
; 5/22/87: set up for hard disk with 512 byte sectors;
;THESE DEFINE THE LOCATION OF EVERYTHING:
;
	CPMLDR	EQU	800H
	SECSIZ	EQU	512
	NSECTS	EQU	16	
;
;
;
;THE CPMLDR IS STORED IN
;TRANSLATED PHYSICAL 512 BYTE SECTORS 1 TO NSECTS.
;AT ENTRY ACC CONTAINS 11H TO TURN OFF THE ROM.
;
CSTART:	

; This assumes 
; that the "premap" code
; has been executed, which leaves the I/O page at 0ffh
 
; Finish mapping out the ROM

	outw
	lxi	h, 80fah
	outw
	mvi	l,0
	mvi	c,08
	ldctl

ERROR:	LXI	SP,0080H
;
	MVI	B,NSECTS
	PUSH	B
;
	MVI	A,1	;START AT TRACK 0, PHYSICAL SECTOR 1.
	STA	SECNUM
;
	LXI	H,CPMLDR
	SHLD	DMAADR
;
LOADL:	CALL	READ
;
	LHLD	DMAADR	;INCREMENT THE DMA ADDRESS
	LXI	D,SECSIZ
	DAD	D
	SHLD	DMAADR
;
	LXI	H,SECNUM	;INCREMENT THE SECTOR NUMBER
	INR	M
;
	POP	B	;DECREMENT AND LOOP ON THE COUNTER
	DCR	B
	PUSH	B
	JRNZ	LOADL
;
DONE:	JMP	CPMLDR	;FINISHED
;
;
;
WDATA	EQU	0D8H
WCMD	EQU	0D9H
WREAD	EQU  08H
WWRITE	EQU 0AH
;
READ:
	MVI	A,WREAD
	CALL	COMCODE
	JNZ	A1
	HLT
;
A1:	LHLD	DMAADR
	MVI	B,0
	MVI	D,2
	MVI	E,3
;
LOOP1:	
L2:	IN	WCMD
	ANA	E
	XRA	D
	JZ	L2
;
	IN	WDATA
	MOV	M,A
	INX	H
	DJNZ	LOOP1
;
L3:	
L4:	IN	WCMD	;DO NEXT 256 BYTES
	ANA	E
	XRA	D
	JZ	L4
;
	IN	WDATA
	MOV	M,A
	INX	H
	DJNZ	L3
;
	CALL WCHECK
	RET
;
;
;
;
; COMMON CODE FOR WINCHESTER READ OR WRITE
;
COMCODE:
	STA	CMDBLK+0	;STORE OPCODE
	XRA	A
	STA	CMDBLK+1	;SELECT DRIVE 0
	STA	CMDBLK+2
	STA	CMDBLK+5
	MVI	A,1
	STA	CMDBLK+4	;TRANSFER COUNT
	CALL	WFREE
;
	LDA	SECNUM	;PLACE IN COMMAND BLOCK
	STA	CMDBLK+3
;
; SELECT CONTROLLER
;
	MVI	A,1
	OUT	WDATA
	OUT	WCMD	;ASSERT SELECT
A2:	IN	WCMD	;WAIT FOR BSY TO BE ASSERTED
	ANI	01
	JNZ	A2
	XRA	A
	OUT	WCMD	;DEASSERT IT
;
	CALL	WWAIT	;WAIT FOR REQ
	LXI	H,CMDBLK
	MVI	B,6
LOOP2:	CALL	WWAIT
	MOV	A,M
	OUT	WDATA
	INX	H
	DCR	B
	JNZ	LOOP2
;	
	CALL	WWAIT	
	IN	WCMD	;SEE IF IT REALLY WANTS DATA
	ANI	10H
	RET
;
CMDBLK:	DB	0,0,0,0,1,0
;
; THIS READS IN THE STATUS AND MESSAGE BYTES
;
WCHECK:
	CALL	WWAIT
	IN	WCMD
	ANI	00011000B
	JZ	OK
	HLT
OK:	IN	WDATA
	ANA	A
	JZ	A3
	CALL	WRESET
	HLT
A3:	CALL	WWAIT
	IN	WDATA
	RET
;
;
; THIS WAITS FOR REQ TO BE ASSERTED:
;
WWAIT:
	XTHL
	XTHL
	XTHL
	XTHL
	IN	WCMD
	ANI	01
	JNZ	A4	;JUMP IF NOT EVEN BUSY
	IN	WCMD
	ANI	02	;MASK OUT REQ
	JNZ	WWAIT
	RET
A4:	
	HLT
;
;
WRESET:
	MVI	A,2
	OUT	WCMD
	XTHL
	XTHL
	XTHL
	XTHL
	XRA	A
	OUT	WCMD
	RET
;	
WFREE:	IN	WCMD
;	ANI	1FH
;	CPI	1FH
;	CZ	WRESET
A5:	IN	WCMD
	ANI	1FH
	CPI	1FH
	JNZ	A5
	RET
;

;
;
DMAADR:	DS	2
SECNUM:	DS	1
;
;
END
