;***************************************************************;
;								;
;	Module:		XROM boot sector			;
;	Written:	x86vet	22-11-25			;
;								;
;	Adds XROM code into a boot sector			;
;								;
;***************************************************************;

name		XRBS

include		xrbs.inc

		assume	cs:cseg

COMPLETE	equ	05555h			;Complete control


cseg		segment

		org	0H			;Extension Vector table

xrom_main	label	near

bixt_type	dw	COMPLETE		;Identification code

bixt_size	db	0EBh			;Boot jump vector

		db	'DIP 2.0'
		db	0			;Page reg

		org	0Bh

		;Drive BPB goes here, from 0Bh to 23h

		;This is a 128 KB CCM BPB:


		dw	200h		;Sector size (bytes)
		db	1		;Sectors per cluster
		dw	1		;Number of reserved sectors
		db	1		;Number of FAT's
		dw	80h		;Number of root entries
		dw	100h		;Total number of sectors
		db	0FFh		;Media descriptor byte
		dw	1		;Sectors per FAT
		dw	8		;Sectors per track
		dw	2		;Number of heads
		dw	0		;Number of hidden sectors

		;End of BPB

		org	24h


xrbs_boot	label	near

;***************************************************************;
;								;
;	PPC Boot sector code - simplified					;
;								;
;***************************************************************;

		mov	ax,0007h		;Set Video mode to text
		int	10h

		mov	bx,ax			;Attribute 0007h
		xor	dx,dx			;Row,col= 0

		push	cs			;Segment for text
		pop	es
		mov	bp,40h			;Message offset
		mov	cx,8			;Length of text

		mov	ax,1301h		;Attrib in BX, Update cursor
		int	10h			;Print message

		xor	ah,ah			;Wait for a key
		int	16h

		int	19h			;Re-boot system

boot_mess	db	'#Flash'		;Max of 6 bytes long
		db	0Dh,0Ah 		; +2 for CR LF


		;Excute boot code

		org	48h

		pop	bp			;Balance stack
		pop	bp
		jmp	short xrbs_boot

xrbs_sign	db	'XRBS'			;XROM boot sector


		;ROM extension vectors

		org	50h
bixt_preb:	jmp	short xrbs_vect		;Pre-bios jmp vector

		org	55h
bixt_bext:	jmp	short xrbs_exit		;Bios-ext jmp vector

		org	5Ah
bixt_pdos:	jmp	short xrbs_exit 	;Pre-dos jmp vector

		org	5Fh
bixt_dext:	jmp	short xrbs_exit 	;Dos-ext jmp vector

		org	64h
bixt_ados:	jmp	short xrbs_exit 	;Post-dos jmp vector

		org	69h
bixt_pwdn:	jmp	short xrbs_pwdn		;Power down jmp vector

		org	6Eh
bixt_pwup:	jmp	short xrbs_exit		;Power up jmp vector


		;Code to enable a return from a pre-BIOS invocation

xrbs_retn	proc	FAR

xrbs_vect	label	near

		jmp	dword ptr cs:preb_retn	;Pre-BIOS extension

preb_retn	dw	0
		dw	0FFFEh

xrbs_retn	endp

		;Code to flag PWDN pending and disable it for now

JMP_1030	equ	22C8h			;Version specific offsets
JMP_1052	equ	20CEh
JMP_107X	equ	2131h
JMP_113X	equ	219Ah

xrbs_pwdn	proc	far

		push	bp			;Preserve regs
		mov	bp,sp			;Get stack frame AT START

		push	ax
		push	bx
		push	ds

		xor	bx,bx			;Access status flag
		mov	ds,bx
		mov	bx,IACA_BYTE		; in IACA

		;Check if PWDN request occurred within driver

		test	BPT ds:[bx],ACTIVE	;Drive active?
		jz	xrbs_done		;No, so bypass everything

		;Flag power down pending

		or	BPT ds:[bx],PENDING	;Flag power down pending

		;Disable power down for this invocation

		mov	ah,2Ch			;Get BIOS version
		int	61h			; in 1.XX? format

		mov	ax,0E000h		;Bug in service!
		mov	ds,ax
		mov	ax,ds:[bx+2]		;Get XX bytes

		cmp	ax,'30'			;For 1.[03]0
		je	vers_1030

		cmp	ax,'50'			;For 1.[05]2
		je	vers_1052

		cmp	ax,'70'			;For 1.[07]X
		je	vers_107X

		cmp	ax,'31'			;For 1.[13]X
		je	vers_113X

		;Assume later version with "Prevent" support

		mov	ah,'P'			;Prevent power down
		jmp	short xrbs_retc
vers_1030:
		mov	[bp+10h],JMP_1030	;Patch return address
		jmp	short xrbs_done
vers_1052:
		mov	[bp+14h],JMP_1052	;Patch return address
		jmp	short xrbs_done
vers_107X:
		mov	[bp+14h],JMP_107X	;Patch return address
		jmp	short xrbs_done
vers_113X:
		mov	[bp+14h],JMP_113X	;Patch return address
xrbs_done:
		xor	ah,ah			;No user code
xrbs_retc:
		pop	ds			;Restore regs
		pop	bx
		mov	bh,ah			;Return user code
		pop	ax

		pop	bp
xrbs_exit:
		ret				;FAR return

xrbs_pwdn	endp


		org	1FEh

		dw	0AA55h			;Boot sector end

cseg		ends
		end	xrom_main


