;; quick diagnostic bootsector
;; prints all registers and hangs

org 0x7C00
bits 16

BS_jmpBoot:
	jmp start
	nop

;; FAT fields, labeled here for convenience
BS_OEMName:
	dd 16843009
	dd 16843009
BPB_BytsPerSec:
	dw 257
BPB_SecPerClus:
	db 1
BPB_ResvdSecCnt:
	dw 257
BPB_NumFATs:
	db 1
BPB_RootEntCnt:
	dw 257
BPB_TotSec16:
	dw 257
BPB_Media:
	db 1
BPB_FATSz16:
	dw 257
BPB_SecPerTrk:
	dw 257
BPB_NumHeads:
	dw 257
BPB_HiddSec:
	dd 16843009
BPB_TotSec32:
	dd 16843009
BPB_FATSz32:
	dd 16843009
BPB_ExtFlags:
	;; Bits 0-3 = Active FAT, 7 = !FAT mirroring
	dw 257
BPB_FSVer:
	dw 257
BPB_RootClus:
	dd 16843009
BPB_FSInfo:
	dw 257
BPB_BkBootSec:
	dw 257
BPB_Reserved:
	dd 16843009
	dd 16843009
	dd 16843009
BS_DrvNum:
	db 1
BS_Reseved1:
	db 1
BS_BootSig:
	db 1
BS_VolID:
	dd 16843009
BS_VolLab:
	dd 16843009
	dd 16843009
	dw 257
	db 1
BS_FilSysType:
	dd 16843009
	dd 16843009

start:
	pusha
	push cs
	push ds
	push es
	push fs
	push gs
	push ss
	
	jmp 0x0000:.here
.here:
	xor ax, ax
	mov ds, ax
	mov si, DiagBoot
	call putStr
	
	mov cx, 14
	mov si, ssIs
	
.loop:
	mov dx, cx
	call putStr
	pop cx
	push si
	call putCX
	pop si
	mov cx, dx
	loop .loop
	
	mov si, stackDumpIs
	call putStr
	
.stackDumpLoop:
	pop cx
	call putCX
	mov ax, 0x0E20
	xor bx, bx
	int 10h
	cmp sp, bp
	jnz .stackDumpLoop

	;; we can hang now
	cli
	hlt
	
putStr:
	;; ah = useTeletype
	mov ah, 0x0E
	;; bh = use page 0, bl = useless in Bochs
	xor bx, bx
loopPut:
	;; lodsb == mov al, si \ inc si
	;; too bad it doesn't do "or al, al" also
	lodsb
	;; check if al == 0
	or al, al
	;; if so, we're done
	jz donePut
	;; otherwise, let's put it
	int 10h
	;; and go again for the next character
	jmp loopPut
donePut:
	ret

putCX:
	mov si, .hexValues
	
	xor ax, ax
	mov al, ch
	and al, 0xF0
	shr al, 4
	mov di, si
	add di, ax
	mov al, [di]
	
	mov ah, 0x0E
	xor bx, bx
	int 10h
	
	xor ax, ax
	mov al, ch
	and al, 0x0F
	mov di, si
	add di, ax
	mov al, [di]
	
	mov ah, 0x0E
	xor bx, bx
	int 10h
	
	xor ax, ax
	mov al, cl
	and al, 0xF0
	shr al, 4
	mov di, si
	add di, ax
	mov al, [di]
	
	mov ah, 0x0E
	xor bx, bx
	int 10h
	
	xor ax, ax
	mov al, cl
	and al, 0x0F
	mov di, si
	add di, ax
	mov al, [di]
	
	mov ah, 0x0E
	xor bx, bx
	int 10h
	
	ret
	
.hexValues:
	db "0123456789ABCDEF"

movAXIP:
	pop ax
	push ax
	ret

DiagBoot:
	db "Diagnostic Boot"
newLine:
	db 13, 10, 0

ssIs:
	db 13, 10, "SS: 0x", 0
gsIs:
	db "  GS: 0x", 0
fsIs:
	db 13, 10, "FS: 0x", 0
esIs:
	db "  ES: 0x", 0
dsIs:
	db 13, 10, "DS: 0x", 0
csIs:
	db "  CS: 0x", 0
	
diIs:
	db 13, 10, "DI: 0x", 0
siIs:
	db "  SI: 0x", 0

bpIs:
	db 13, 10, "BP: 0x", 0
spIs:
	db "  SP: 0x", 0
	
bxIs:
	db 13, 10, "BX: 0x", 0
dxIs:
	db "  DX: 0x", 0

cxIs:
	db 13, 10, "CX: 0x", 0
axIs:
	db "  AX: 0x", 0

stackDumpIs:
	db 13, 10, "Stack Dump: ", 13, 10, 0

	;; this is here in case I make changes
	times 510-($-$$) db 0
	
	;; Standard end of boot sector
	db 0x55, 0xAA