;XIX System 19 Bootsector - Version 1.00
;
;The purpose of this bootsector is simply to load
;the second stage of the XIX Bootloader, normally
;called the Bootloader.
;
;This is the case, in order to circumvent the now-
;restricting 512 (actually ~440) byte limit of the
;MBR, and to allow future implementations of error
;handling, error logging, hardware diagnostics,
;portability, and multi-boot support.
;
;For full documentation, read the source-enclosed
;bootsector.img.doc file.

[BITS 16]		;Assembler Directive denoting 16-bit Real-Mode
[ORG 0x7C00]		;Assembler Directive denoting residence at 0x7C00 in memory

begin:		;function that jumps over the data section and into the boot sequence
	jmp boot


;Section for Functions
prntstr:	;function that will print a string, loaded into SI, on screen
	mov ah, 0x0E	;place 0x0E (teletype function) into AH

	mov bh, 0x00	;set page number to page 1
	mov bl, 0x000E	;set attribute to Green Text, Black Background, non-Flashing

	.nxtchar	;label to jump to if AL is not equal to Null Char
	lodsb		;load the string residing in SI, into AL for printing
	or al, al	;check to see if AL = Null Character

	jz .ret		;if zero, jump to label to return to main program

	int 0x10	;call int 0x10 to print the string
	jmp .nxtchar

	.ret		;label to jump to if AL = Null Character
ret

readdsk:	;function that will reset the disk, then read it into 0x8000
	.rstdsk
	mov ah, 0x00		;place 0x00 (reset function) into AH
	mov dl, [bootdrv]	;reset drive

	int 0x13		;call int 0x13 to reset drive
	jc .rstdsk		;jmp to .rstdsk if carry flag is set (reset failed)

	mov ax, 0x8000		;load 0x8000 into AX register (can't directly manipulate ES
	mov es, ax		;set ES to contents of AX (location of memory to read to)
	xor bx, bx		;set offset to 0x0000

	.rddsk
	mov ah, 0x02		;place 0x02 (read function) into AH
	mov al, 0x00		;read from cylinder 0
	mov ch, 0x01		;read track 1
	mov cl, 0x02		;read from sector 2
	mov dh, 0x00		;read from head 0
	mov dl, [bootdrv]	;read from drive

	int 0x13	;call int 0x13 to read from disk
	jc .rddsk	;if carry flag is set (read unsuccessfull) try again
ret


;Section for Strings
sysmess db 'XIX System 19 Bootsector - Version 1.00',13,10,10,0	;string to print system information
								;13 = character return
								;10 = new line
								;0 = null character (end string)

dsksucc db 'Disk Reading Successfull, Will now Load Stage 2',13,10,0


;Section for Variables
bootdrv db 0	;variable to store drive number


;Main Program
boot:
	mov [bootdrv], dl	;load the boot-drive number into the bootdrv variable

	xor ax, ax		;refresh all segment registers
    	mov ds, ax
    	mov es, ax
    	mov ss, ax
    	mov sp, 0x7C00		;stack extends downward from 0x7C00
    	sti			;enable interupts
    	cld			;clear the direction flag

	lea si, [sysmess]	;load effective address of string sysmess into SI
	call prntstr		;call the function to print the string

	call readdsk		;call the function to load from disk into memory
	lea si, [dsksucc]	;dsksucc into SI
	call prntstr		;call the function to print the string

	jmp 0x8000:0x0000	;jump to instruction residing in 0x8000 (stage 2 bootloader)

	hlt			;halt the processor from executing this instruction


;Necessities for the Bootsector
times 510-($-$$) db 0		;fill unused space upto 510 bytes (0x1FE)
signature dw 0xAA55		;append the bootloader signature to last 2 bytes (0x1FE-0x200) 