* NAM	INITER
*
* INITIALIZE A DISK FOR CP-68 OPERATING SYSTEM
*
* FOR ICOM 8 INCH FLOPPY DISKS (AND FD3712)
*
* TRACK 0, SECTORS 1,2	BOOTSTRAP
* TRACK 0, SECTOR  3	HEADER OF FREE-SPACE LIST
* TRACK 0, SECTORS 4-26	DIRECTORY SPACE
* TRACKS 1-76		FREE SPACE
*
* UPDATED BY MIKE DOUGLAS TO WORK WITH THE FD3712
* DRIVE CABINET AND UIO BOARD IN AN ALTAIR 680
*
* AUGUST 2022

* DISK ATTRIBUTES

SECSIZ	EQU	128 		128 BYTES PER SECTOR
TRKSIZ	EQU	26		LAST SECTOR IS 26
DSKSIZ	EQU	76		LAST TRACK IS 76

* FCB DEFINITIONS

FCBSTA	EQU	5		ERROR STATUS FLAG
FCBDBA	EQU	7		DATA BUFFER ADDRESS
FCBDRV	EQU	9		DRIVE NUMBER
FCBTRK	EQU	10		TRACK NUMBER
FCBSCT	EQU	11		SECTOR NUMBER
FCBTlK	EQU	12		TRACK LINK POINTER
FCBSLK	EQU	13		SECTOR LINK POINTER

* COMMAND-LINE INTERPRETER BASE-PAGE LOCATIONS

DESCRA	EQU	$20		ADDRESS OF TOKEN
VALUE	EQU	$27		VALUE OF NUMERIC TOKEN

* LOCATION OF BOOT CODE IN MEMORY TO WRITE TO DISK

BOOT	EQU	$2400

* START OF PROGRAM

	ORG	$2100

INITR	LDAA	VALUE+1		GET DRIVE TO INIT 
	ANDA	#$03		MAX 4 DRIVES
	LDX	#FCBSPC		POINT TO FCB
	STAA	FCBDRV,X	STORE DRIVE NUM
	ADDA	#$30		MAKE DRV NUM ASCII
	STAA	DRVNO		PUT IN PROMPT LINE
	LDX	#PROMPT      
	SWI	PRTMSG		OUTPUT PROMPT 
	 FCB 49
	SWI	GTCMD		GET USER RESPONSE
	 FCB 48
	LDX	DESCRA	
	LDAA	0,X		GET FIRST CHAR OF RESPONSE
	CMPA	#'Y'		WAS IT 'YES'?
	BEQ	INITR2		BR IF YES
	RTS			IF NOT, QUIT
			     
INITR2	LDX	#FCBSPC		POINT TO FCB		      
	CLR	FCBTRK,X	TRACK=0
	LDAA	#1		SECTOR=1
	STAA	FCBSCT,X
	SWI	TXAB  
	 FCB 2	 
	LDX	#BOOT		POINT TO BOOT CODE
	SWI	XABX	   
	 FCB 4
	STAA	FCBDBA,X
	STAB	FCBDBA+1,X
	JSR	WRTBLK		WRITE FIRST 128 BYTES OF BOOT PROG
	TST	FCBSTA,X	DISK ERROR?
	BNE	INITQ		FATAL ERROR, QUIT

	INC	FCBSCT,X	SECTOR=2
	SWI	TXAB
	 FCB 2	 
	LDX	#BOOT+$80	2ND HALF OF BOOT LOADER
	SWI	XABX
	 FCB 4
	STAA	FCBDBA,X
	STAB	FCBDBA+1,X
	JSR	WRTBLK		WRITE SECOND 128 BYTES OF BOOT PROG
	TST	FCBSTA,X	DISK ERROR?
	BNE	INITQ		YES

* INITIALIZE HEAD OF FREE-SPACE BLOCK
* ALL BYTES ZERO EXCEPT LAST TWO BYTES = TRACK 1, SECTOR 1

	INC	FCBSCT,X	SECTOR=3
	SWI	TXAB     
	 FCB 2	 
	LDX	#BUFFER		
	SWI	XABX		   
	 FCB 4
	STAA	FCBDBA,X		   
	STAB	FCBDBA+1,X
	SWI	PSHX
	 FCB 5

* CLEAR OUT BUFFER EXCEPT FOR LAST 2 BYTES

	LDX	#BUFFER
	LDAB	#SECSIZ-2
	CLRA

INITR3	STAA	0,X
	INX
	DECB
	BNE	INITR3

	LDAA	#1		POINT TO TRACK 1, SECTOR 1
	STAA	0,X
	STAA	1,X
	SWI	PULX
	 FCB 6
	BSR	WRTBLK		WRITE FREE SPACE BLOCK
	TST	FCBSTA,X
	BNE	INITQ		ERROR

	INC	FCBSCT,X	SECTOR = 4 (DIRECTORY)
	CLR	BUFFER+SECSIZ-2	MAKE ALL BYTES ZERO
	CLR	BUFFER+SECSIZ-1
	
* INITIALIZE DIRECTORY SECTORS TO ZERO

INITR4	BSR	WRTBLK		WRITE A DIRECTORY BLOCK
	TST	FCBSTA,X
	BNE	INITQ		ERROR

	LDAA	FCBSCT,X	GET CURRENT SECT
	INCA			NEXT SECTOR
	CMPA	#TRKSIZ		END OF TRACK?
	BEQ	INITR5		BR IF YES
	STAA	FCBSCT,X
	BRA	INITR4		NO, CONT WRITING
	  
* FATAL ERROR MESSAGE

INITQ	LDX	#QMSG		MSG INITIALIZATION FAILED
	SWI	PRTMSG
	 FCB 49
	RTS

* INITIALIZE REST OF DISK (FREE SPACE), A=TRACK, B=SECTOR

INITR5	LDAA	#1		INIT REST OF DISK 
	STAA	FCBSCT,X	SECTOR=1
	STAA	FCBTRK,X	TRACK=1
	TAB

INITR6	INCB			MAKE SECTOR LINKAGE
	CMPB	#TRKSIZ+1	END OF TRACK?
	BNE	INITR7		NO

	LDAB	#1		WRAP TO SECTOR 1 OF NEXT TRACK
	INCA
	CMPA	#DSKSIZ+1	END OF DISK?
	BNE	INITR7		NO

	CLRA			LAST SECTOR POINTS TO 0,0
	CLRB

INITR7	STAA	BUFFER		TRACK LINK
	PSHB			SAVE LSEC
	BSR	GETSC		GET PSEC 
	STAB	BUFFER+1	SECTOR LINK
	PULB			RESTORE LSEC
	BSR	WRTBLK		WRITE SECTOR
	TSTA			DONE?
	BNE	INITR8		NO

	TSTB			DONE?
	BNE	INITR8		NO

	RTS			YES! DONE

INITR8	STAA	FCBTRK,X     
	PSHB			SAVE LSEC
	BSR	GETSC		GET PSEC
	STAB	FCBSCT,X     
	PULB			GET LSEC
	BRA	INITR6		KEEP WRITING

* CONVERT LSEC TO PSEC, LSEC IN B

GETSC	SWI	PSHX		CONVERT LSEC (LOGICAL SECT) TO PSEC (PHYSICAL SECT)
	 FCB 5
	LDX	#TBL		X=PHYSICAL TO LOGICAL TABLE
	SWI	ADDBX
	 FCB 10
	DEX			SECTOR STARTS AT 1
	LDAB	0,X		GET PSEC
	SWI	PULX		RESTORE X-REG
	 FCB 6 
	RTS  

* WRITE A SECTOR WITH ERROR CHECKING
    
WRTBLK	PSHA
	CLRA
	CLR	FCBSTA,X	CLEAR ERROR FLAG
	SWI	IOHDR	      
	 FCB 19
	STAA	FCBSTA,X	
	TSTA			ERROR?
	BNE	WRTERR		YES
	PULA
	RTS  
	    
WRTERR	TAB	      
	BSR	OUTHL		CONVERT LEFT DIGIT
	STAA	ERTYPE
	TBA  
	BSR	OUTHR		CONVERT RIGHT DIGIT
	STAA	ERTYPE+1
	SWI	PSHX		SAVE X
	 FCB 5
	LDAA	FCBSCT,X
	BSR	OUTHL		MAKE SECT NUM HEX
	STAA	SECT
	LDAA	FCBSCT,X
	BSR	OUTHR
	STAA	SECT+1
	LDAA	FCBTRK,X
	BSR	OUTHL	 	MAKE TRACK NUM HEX
	STAA	TRACK
	LDAA	FCBTRK,X
	BSR	OUTHR
	STAA	TRACK+1
	LDX	#DERROR
	SWI	PRTMSG		PRINT ERROR MESSAGE
	 FCB 49
	SWI	WARMSTART
	 FCB 31
	RTS  

* CONVERT BINARY TO HEX-ASCII HERE

OUTHL	LSRA
	LSRA
	LSRA
	LSRA
OUTHR	ANDA	#$0F		GET NIBBLE
	ADDA	#$30		MAKE ASCII
	CMPA	#$39		IS >9?
	BLS	OUTH1		NO
	ADDA	#$07		YES
OUTH1	RTS  

* MESSAGES

PROMPT	FCC	'INIT. DISK IN DRIVE '
DRVNO 	FCB	0
	FCC	' ? '
	FCB	$04

DERROR	FCC	'DISK ERROR:'
ERTYPE	FCB	0,0
	FCC	' AT SECTOR '
SECT	FCB	0,0
	FCC	' TRACK '
TRACK	FCB	0,0		  
	FCB	$0D

QMSG	FCC	'INITIALIZATION FAILED' 
	FCB	$0D		

* PHYSICAL TO LOGICAL TABLE

	FCB	0
TBL	FCB	1,10,19,2,11,20,3,12,21,4,13,22,5
	FCB	14,23,6,15,24,7,16,25,8,17,26,9,18

* FCB

FCBSPC	FCB	0,0
	FCC	'DSK'
	FCB	0
	FCB	$FF
	RMB	35
	
BUFFER	RMB	SECSIZ		SECTOR BUFFER

	END
