;******** biosboot.8kn for cpm.sys + cpmldr.sys*****
;* 	Copyright 1984, Digital Research Inc.
;*
;* 821013 S. Savitzky (Zilog) -- adapt for nonseg.
;* 820930 S. Savitzky (Zilog) -- created
;* 840813 R. Weiser (DRI) -- conditional assembly
;*
;****************************************************

__text:	.sect

;****************************************************
;*
;* NOTE -- THIS CODE IS HIGHLY SYSTEM-DEPENDENT
;*
;*	This module contains both the bootstrap
;*	writer, and the code that receives control
;*	after being booted.
;*
;*	The main function of the latter is to make
;*	sure that the system, whose entry point is
;*	called "bios", is passed a valid stack 
;*	and PSA pointer.
;*
;*	Although this code runs segmented, it must
;*	be linked with non-segmented code, so it
;*	looks rather odd.
;*
;****************************************************


;****************************************************
;*
;* 	CP/M - 8000 on the Olivetti M20.
;*
;*	Olivetti's peculiar format, has a lot of
;*	Olivetti's file system in it.
;*
;*	Track 0 is unused except for sector 0, since 
;*	it is single density and thus has smaller 
;*	sectors.
;*
;*	A total of 10 tracks are reserved from CP/M,
;*	leaving 9 tracks for the system proper.
;*
;*	The first sector on track 1 is the PCOS file
;*	descriptor block; the second is the boot file
;*	header and the start of the system code.
;*
;*	This leaves something under 28K for the 
;*	system (BIOS+BDOS+CCP).  It is assumed that
;*	the system starts at its lowest address,
;*	and that data follows immediately after code.
;*
;*	For now, we assume that the system starts at
;*		<<11>>0000 (hex) for normal system
;*		<<10>>0000 (hex) for boot system
;*
;****************************************************


;****************************************************
;*
;*   Globals
;*
;****************************************************

	.if LOADER
	.global _startld ;entry to read system tracks
	.endif

;****************************************************
;*
;* Externals
;*
;****************************************************

	.global	bios
	.if LOADER
	.else		; no warm boots in loader bios
	.global _wboot
	.endif

;****************************************************
;*
;* Constants
;*
;****************************************************


	.if LOADER
BOOTSYS	.equ 	0A000000h	;system address on boot
BOOTSTK	.equ	BOOTSYS+0BFFEh	;system stack top on boot
	.else
SYSTEM	.equ	0B000000h	; system address
SYSSTK	.equ	SYSTEM+0BFFEh	; system stack top
	.endif
;rtc_ext	.equ	02000022h	; real-time clock
				;  ext. call addr

BPT	.equ	16		; #blocks in a track
BPS	.equ	256		; #bytes in a sector
NBLKS	.equ	9*16		; #blocks in boot 
HDRSIZE	.equ	24		; #bytes in header
FILSIZE .equ	256*(NBLKS-1)	; file data size
SYSSIZE	.equ	FILSIZE-HDRSIZE	; total system size
S1SIZE	.equ	BPS-HDRSIZE	; data in sector 1

SEG4	.equ	04000000h
SEG2	.equ	02000000h

SYSPSA	.equ	SEG2+100h	; system PSA
BOOTPSA	.equ	SEG4+100h	; PSA in PROM for boot

sscall	.macro		;short segmented call
	.word	05f00h
	.word	?1
	.endm























;****************************************************
;*
;* Entry Points and post-boot Initialization
;*
;****************************************************


;* transfer vector 

	.if LOADER
	.else		; no warm boot in the loader bios
	jr	wboot
	.endif
	jr	entry

;* post-boot init.

entry:				;SEGMENTED
	.if LOADER
_startld:
	.endif


	DI	VI,NVI

	.if LOADER
	ldl	rr14, #BOOTSTK	;init boot stack pointer 
	.else
	ldl	rr14, #SYSSTK	;init normat stack pointer
	.endif
	ldl	rr2, #SYSPSA	; copy PROM's PSA
	ldctl	r4, psapseg
	ldctl	r5, psapoff
	ld	r0, #570/2
	ldir	@r2, @r4, r0

	ldl	rr2, #SYSPSA	; shift PSA pointer
	ldctl	psapseg, r2
	ldctl	psapoff, r3

	ld	r2,#142h	;CROCK-- turn off
	ld	r3,#1feh	; usart interrupts
	out	@r2,r3

	ldar   	r2, $		; go
	ld	r3,#bios
	jp	@r2


	.if LOADER
	.else			;no warmboot in loader bios

wboot:	ldar	r2, $
	ld	r3,#_wboot
	jp	@r2
	.endif
ldar	r2, $
	ld	r3,#_wboot
	jp	@r2
	.endif
