	title 'boot-loader harddisk fuer prof-80' 
   
;       --               CP/M 3.0            --
;       _______________________________________
   
;       Autor Wilfried Schmitten, Heidebloeck 16, 2000 Hamburg 74
   
;       letzte Aenderung   1.3.88 Aenderung der HD-Organisation
;       Initialisierung geandert               6.2.89 / Wilfried
	 

;      Assembliere rmac hboot
;      Linke       link hboot[lfa00]

	maclib	z80   
	maclib	cdef 
	maclib	syspage 
   
	jmp	boot          ;wichtig
boot:	call	hdinit0	
	lxi	b,p$ram$table	;Kachel	0 und 1
	mvi	e,2		;werden	bank 0
	outp	e 
	mvi	b,16 
	outp	e		;fertig
	call	hdread 
	xra	a 
	sta	epromz		;Einschalttiefe	Eprom
	jmp	100h		;Lader anspringen	
   
hdinit0:			 
	in	hdconfig 
	sui	hd$jumper		;HD ueberhaupt da?
	jnz	0		;kein HD boot moeglich

	out	hdreset	
	out	hdmask 
        mvi     b,0
dl1:    djnz    dl1            ;kurzer Delay        
        mvi     d,80           ;Schleifenzaehler
        
hdi1:	call    checkr
        push    psw
        lxi     b,-1
hddel:  dcx     b
        mov     a,c
        ora     b
        jrnz    hddel          ;Delay
        pop     psw
        jrz     setparam
        mvi     c,'B'          ;Kennung Boot Lader
        call    gripout
        dcr     d
        jrnz    hdi1
        mvi     c,'*'
        call    gripout
        jmp     0
   
;       Laufwerksparameter setzen
	 
ksetp:	db     12,0,0,0,0,0

setparam:   
        lxi     h,ksetp
        call    command
	lxi	h,drivedata 
	mvi	b,8		;8 Eintraege
	mvi	c,hddata 
setloop2:   
	call	waitreq	
	outi  
	jrnz	setloop2 
errcheck:	 
	call	waitreq	
	in	hddata 
	ani	2 
	rz	 
        jr      reqsense
        
krcheck:       db      0,0,0,0,0,0

checkr:        
        lxi    h,krcheck
        call   command
        call   waitreq
        in     hddata
        ani    2
        ret


        jr     errcheck


kreqs:  db     3,0,0,0,0,0        

reqsense:   
        mvi     b,0
reqdly  djnz    reqdly         ;Delay notwendig ?
        lxi     h,kreqs
	call	command
	call    waitreq
	ani     2
	jnz     0              ;Fehlerausgang
	      	
	lxi	h,sensedata	;Ergebnispuffer
	mvi	c,hddata 
req1:	
        ini
req2:   in      hdstatus
        rrc
        jrnc    req2
	ani     3
	xri     1
	jrz     req1    
	ini  
	jmp     0                 ;hier bei Fehler


waitreq:   
	in	hdstatus	;warten	auf
	rrc 
	rc			;request transfer
	jr	waitreq	
   
   
gripout:   
	in	gripc 
	ani	gripout$mask 
	jrz	gripout	
	mov	a,c 
	ani	07fh 
	out	gripd 
	ret  
   
command:   
	out	hdselect	;Controller mit	dummy ansprechen
	mvi	b,6		; 6 byte
	mvi	c,hddata 
comloop:   
	call	waitreq	
	outi  
	jrnz	comloop	
	ret  
   
hdread:	  
	mvi	a,8		;Read command
	sta	cdbcom		;Command ablegen
   
	xra	a 
	sta	cdbhead		;head 0
	sta	cdbcylinder	;track 0
	inr	a 
	inr	a 
	sta	cdbsector	;sector	2
	mvi	a,12		;12x512	= 6 K
	sta	cdbcount 
	mvi	a,1 
	sta	cdbcontrol 
        lxi     h,cdbcom
	call	command	
	call	waitreq	
hdrwc:	 
	ani	2
	jrnz	hderr1 
	lxi	h,100h		;DMA adress
	mvi	c,hddata 
rwpatch:   
	ini  
rwloop:	  
	in	hdstatus 
	rrc  
	jrnc	rwloop		;kein Data request
	ani	3
	xri     1 
	jrz	rwpatch		;naechstes byte	Data
hderr1:	  
	jmp	errcheck 
   
   
drivedata:   
if parkcyl > cylinder
        db      high    parkcyl
        db      low     parkcyl
else        
	db	high	cylinder 
	db	low	cylinder 
endif
	db	hdhead 
	db	high	redwrcur 
	db	low	redwrcur 
	db	high	wrprecomp 
	db	low	wrprecomp 
	db	0 
   
;------------------------------------------------------
;       in SYSPAGE.LIB vereinbart
;------------------------------------------------------
;
;       command descriptor block
   
;cdbcom:        operation code
;cdbhead:       head, bit 7=cylinder 2~10
;cdbsector:     sector, bit7,6=cylinder 2~9,2~8
;cdbcylinder:   cylinder 2~7 - 2~0
;cdbcount:      Block count
;cdbcontrol:    bit7=1=disable retry                    
;               bit6=1=disable error correction
;               bit2,1,0 step option
;sensedata      5 byte
   
end
   

               bit6=1=