;****************************************************************************
;
;  CP/M cold boot loader for CP/M on the Polymorphic Poly-8813
;
;    The first 256 bytes of this module look like the 1st sector of a
;    System-88 (Exec) directory. The Poly boot ROMs look in the directory
;    for the file 'Exec.OV' and load it into the overlay area at 2000h
;    and jump to it. The program Exec.OV starts at offset 100h in this
;    module which becomes the 2nd sector on the disk. Exec.OV loads the
;    CP/M image into high RAM.
;
;    Version History
;
;    Ver     Date	  Description
;    ---   ---------	---------------------
;    1.0   11/07/19	Original, Mike Douglas     
;
;--------------------------------------------------------------------------
;
;    To patch changes made to this loader into a CP/M image saved from
;    MOVCPM (e.g., CPMxx.COM), use the following commands:
;
;	A>DDT CPMxx.COM
;	-IBOOT.HEX
;	-REA00		(puts file at 900h in RAM)
;	-G0	    	(Go zero, not "oh")
;	A>SYSGEN
;
;****************************************************************************
; CP/M size & locations

MEMSIZE	equ	56		;set memory size in K bytes
BIOSLEN	equ	1300h		;total memory required by BIOS
CCPLEN	equ	0800h		;CP/M 2.2 fixed length
BDOSLEN	equ	0E00h		;CP/M 2.2 fixed length

CCPBASE	equ	MEMSIZE*1024 - BIOSLEN - BDOSLEN - CCPLEN
BIOSBAS	equ	(CCPBASE+CCPLEN+BDOSLEN) ;base address of the BIOS

; Location to place the last 180h bytes of the CP/M image

BOOTRKS	equ	3		;number of boot tracks
NUMSEC	equ	10		;number of physical sectors per track
PSECLEN	equ	256		;physical sector length
LAST180	equ	CCPBASE+(BOOTRKS*NUMSEC-2)*PSECLEN

; Poly PROM equates

OVRLY	equ	2000h		;System-88 overlay location
SBUF	equ	2800h		;ROM reads directory sectors to SBUF
Dio	equ	0406h		;disk I/O
Msg	equ	040Ch		;display text string
Err	equ	040Fh		;display error from DE

;-----------------------------------------------------------------------
; Boot Image Sector 0
;     Sector 0 looks like the 1st sector of the four sector directory
;     used by System-88 (Exec). The Poly boot ROMs look in the directory
;     for the file 'Exec.OV' and load it into the overlay area at 2000h
;     and jump to it. The file Exec.OV is in sector 1 - which is
;     technically still part of the directory - but the boot ROMs don't
;     care as long as the checksum for sectors 0-3 is correct. When
;     SYSGEN writes the boot tracks, it computes the checksum of sectors
;     0-3 and puts the checksum in byte 0 of sector 0.
;
;     A dummy file in the directory shows "CPM-DISK" when the disk
;     is examined under System-88.
;
; NOTE:
;     Bytes 40h-FFh of this sector contain bytes 000h-0BFh of the last
;     180h bytes of the CP/M image.
;-----------------------------------------------------------------------
	org	OVRLY-100h	;sector 0, 100h bytes before Exec.OV
	
chkSum	db	0		;directory checksum
dskName	db	'CP/M 2.2'	;disk name
	dw	2		;number of files in directory
	dw	2BFFh		;next directory address (full)
	dw	350		;next disk address (full)
	
;  Directory entry #1 points to Exec.OV which is the file loaded by
;     the boot ROM. This Exec loads CP/M from the boot tracks.

	db	44h		;system file, four byte name
	db	'Exec'		;file name
	db	'OV'		;overlay extension
	dw	1		;disk address of file
	dw	1		;length in sectors
	dw	2000h		;load address
	dw	2000h		;execute address	
		
;  Directory entry 2 shows "CPM-DISK" when directory listed
;     if disk is examined under System-88 (Exec).

	db	8		;eight byte name
	db	'CPM-DISK'	;file name
	db	'  '		;overlay extension
	dw	4		;disk address of file
	dw	1		;length in sectors
	dw	0		;load address
	dw	0		;execute address
	
	dw	0		;no more entries
	
; Bytes 000h-0BFh of the last 180h bytes of the CP/M image are
;    inserted here starting at offset 40h


;-----------------------------------------------------------------------
; Boot Image Sector 1
;     Sector 1 contains the code for Exec.OV that is loaded into the
;     overlay area by the boot ROM. Exec.OV, in turn, loads the rest
;     of the boot tracks containing CP/M into high memory.
;
; NOTE:
;     Bytes 40h-FFh of this sector contain bytes 0C0h-17Fh of the last
;     180h bytes of the CP/M image.
;-----------------------------------------------------------------------
	org	OVRLY

	db	'Exec'		;overlay name
	jmp	start		;jump opcode required here
	
; Move the last 180h bytes of the CP/M image from the first two
;    physical sectors into high RAM

start	lxi	h,SBUF+40h	;HL->first C0h bytes of last 180h
	lxi	d,LAST180	;DE->where to put last 180h bytes
	mvi	b,0C0h		;B=number of bytes to move
	call	moveH2D

	lxi	h,SBUF+140h	;HL->second C0h bytes of last 180h
	mvi	b,0C0h		;B=number of bytes to move
	call	moveH2D

; Load the remaining 28 sectors of the three boot tracks into high RAM

	mvi	b,1		;B=read command
	mvi	c,1		;C=drive to use
	lxi	d,CCPBASE	;DE->where to put the CP/M image
	lxi	h,2		;HL=starting disk address
	mvi	a,BOOTRKS*NUMSEC-2   ;read 3 boot tracks less secs 0,1
	call	DIO		;issue read command (in PROM)
	jc	Err		;disk I/O error

	jmp	BIOSBAS		;enter BIOS cold start entry point

; moveH2D - Move B bytes from (HL) to (DE)

moveH2D	mov	a,m		;move byte from (HL) to (DE)
	stax	d
	inx	h		;increment pointers
	inx	d
	dcr	b		;decrement byte count
	jnz	moveH2D

	ret
	
; Bytes 0C0h-17Fh of the last 180h bytes of the CP/M image are
;    inserted here starting at offset 40h

	end

