;	Logical disk format utility for my 8080 microcomputer
;	JWD - v1.0   06/19/2024   
;
	CPU 8080        ; Specify architecture
	PAGE 0
;
; BIOS equates
;
ccp		equ 0df00h		;base of ccp
bdos	equ ccp+800h	;base of bdos
bios	equ ccp+1600h	;base of bios
;
;	bios access constants
;
boot	set	bios+3*0	;cold boot function
wboot	set	bios+3*1	;warm boot function
const	set	bios+3*2	;console status function
conin	set	bios+3*3	;console input function
conout	set	bios+3*4	;console output function
list	set	bios+3*5	;list output function
punch	set	bios+3*6	;punch output function
reader	set	bios+3*7	;reader input function
home	set	bios+3*8	;disk home function
seldsk	set	bios+3*9	;select disk function
settrk	set	bios+3*10	;set track function
setsec	set	bios+3*11	;set sector function
setdma	set	bios+3*12	;set dma function
read	set	bios+3*13	;read disk function
write	set	bios+3*14	;write disk function
listst	set	bios+3*15	;list status function
sectran	set	bios+3*16	;sector translate
;
; BDOS call address
;
bdosfun equ 5           ;Address of BDOS function call
;
; Write types
;
wrall	equ	0		;write to allocated
wrdir	equ	1		;write to directory
wrual	equ	2		;write to unallocated
;
; Driver storage
;
cdisk equ 4h        ;Current drive address
;
; Disk buffer
;
userdma	equ	080h
;
;
   org 100h
;
;
format  lxi d, frmtmsg  ;Point to start message
        call print      ;Print it
        lxi d, userdma  ;Point to default disk buffer
        mvi b, 80h      ;Disk buffer length
        mvi a, 0e5h     ;Format character
frmt01  stax d          ;Save it in disk buffer
        inx d           ;Point to next buffer character
        dcr b           ;Count down
        jnz frmt01      ;Do it again
        lda cdisk       ;Get the current disk
        adi 'A'         ;Calculate drive letter
        mov c, a        ;Transfer to register c
        call conout     ;Print it
frmt02  lxi d, frmtqry  ;Ask to be sure
        call print      ;Print query
        call conin      ;Get yes or no
		ani 0dfh        ;Make it upper case
        cpi 'Y'         ;Is it 'Y'?
        jz frmt03       ;Do format
        cpi 'N'         ;Is it 'N'?
        jz frmtex       ;Go back to dos
        jmp frmt02      ;Ask again
frmt03  lda cdisk       ;Get the current disk
        mov c, a        ;Transfer to register c
        call seldsk     ;Select the disk
        lxi b, 1        ;Select track 1 (directory)
        call settrk
        lxi h, seccnt   ;Point to sector counter
        mvi c, 0        ;Select sector 0
        mov m, c        ;Save it in the counter
frmt04  call setsec     ;Select the sector
        mvi c, wrdir    ;Don't buffer
        call write      ;Write the formatted sector
        mvi c, '.'      ;Status character
        call conout     ;Print it
        lxi h, seccnt   ;Point to sector counter
        mov c, m        ;Get current sector
        inr c           ;Count up
		mov a, c        ;Put sector count in A
        jz frmtex       ;Exit if end of directory
        mov m, c        ;Save sector counter
        jmp frmt04      ;Do next sector        
frmtex  lxi d, frmtend  ;End status message
        call print      ;Print it
        jmp wboot       ;Hand control back CP/M
;
print:  ldax d          ;Print a message on the console
        cpi 0           ;End of string?
        jz pend         ;Exit if end of string
        mov c, a        ;Move shar into C for console out
        call conout     ;Print it
        inx d           ;Point to next character
        jmp print       ;Repeat
pend:   ret             ;And exit
;
frmtmsg db 'Disk formatter v1.0'
        dw 0d0ah
        db 'Formatting drive '
        db 0
frmtqry dw 0d0ah
        db 'ARE YOU SURE (Y/N)?'
        dw 0d0ah
        db 0
frmtend dw 0d0ah
        db 'Format complete.  Returning to CP/M.'
        dw 0d0ah
        db 0
;		
seccnt  DB 0
;
   end