;512DevOS 0.02
;Copyright (C) 2005 Anthony De Feo <xdatadrainx90@gmail.com>
;Assistance provided by Benjamin David Lunt, Christoffer Bubach, and Daniel Rowell Faulkner
;
;This program is free software; you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation; either version 2 of the License, or
;(at your option) any later version.
;
;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with this program; if not, write to the Free Software
;Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use16
ORG 0x7C00

xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp,0x8000

mov WORD [0x21*4], PutStri
mov WORD [0x21*4+2], ax
mov WORD [0x22*4], getchari
mov WORD [0x22*4+2], ax

main:
mov ax,0x0003
int 0x10

mov si,welcome
int 0x21
;-------------------------------------
;get command key and compare routine
;-------------------------------------
CmdPrompt:
mov si,prompt
int 0x21
int 0x22
push ax
mov ah, 0x0E
mov bl, 0x07
int 0x10
pop ax
cmp al, 0
je sectcomp
mov si,welcome
cmp al,'V'
je Do_str_display
cmp al,'S'
je halted
mov si,helpm
cmp al,'H'
je Do_str_display
cmp al,'R'
je reboot
cmp al,'C'
je cls
cmp al,'O'
je reload
cmp al,'F'
je savrstart
mov si,welcome
cmp al,'v'
je Do_str_display
cmp al,'s'
je halted
mov si,helpm
cmp al,'h'
je Do_str_display
cmp al,'r'
je reboot
cmp al,'c'
je cls
cmp al,'o'
je reload
cmp al,'f'
je savrstart
invalid:
mov si,invalidm
int 0x21
jmp CmdPrompt

sectcomp:		;sector loading f1-f3 key compare routine
cmp ah, 0x3B	;3B=code for F1, which is the sector that the OS is on.  the reload (int 0x19) function can handle this
je reload
cmp ah, 0x3C	;3C=code for F2
je intload2
cmp ah, 0x3D	;3D=code for F3
je intload3
jmp CmdPrompt
intload2:
mov cl, 0x2
jmp loadsect
intload3:
mov cl, 0x3
jmp loadsect
;------------------------------------
;Misc. OS/kernel/command functions
;------------------------------------
Do_str_display:
int  0x21
jmp  CmdPrompt

halted:
mov si,haltedm
int 0x21
halt:
jmp halt

cls:		;clearscreen function, resets video mode
mov ah, 0x0F
int 0x10
mov ax,0x0003
int 0x10
jmp CmdPrompt

reboot: 	  ;reboot function
db 0EAh
dw 0000h
dw 0FFFFh

reload: 	;reloads the bootsector and jumps to it
int 0x19

PutStr: 	;string output function for use by kernel
push ax
push bx
push si
mov ah,0x0E
mov bx,0x0015
nextchar:
lodsb
or al,al
jz return
int 0x10
jmp nextchar
return:
pop ax
pop bx
pop si
ret

loadsect:
mov bx, 0x2000
mov es, bx
xor bx, bx
mov ah, 02
mov al, 01
mov ch, 01
mov dh, 01
mov dl, 00
int 0x13
jc loadsect
mov ah, 0x0E
mov al, "A"
int 0x10
xor ax, ax
int 0x16
jmp 0x2000:0x0000

savrstart:
mov si, scrsavrm
int 0x21
mov ah, 0x01
int 0x16
jnz svreturn
jmp savrstart
svreturn:
jmp CmdPrompt

;----------------------------------------------------------------------------------
;Interrupt functions 0x21 and 0x22, string output and character input
;respectively
;----------------------------------------------------------------------------------
PutStri:
call PutStr
iret

getchari:
xor ax, ax
int 0x16
iret
;-------------------------
;Data
;-------------------------
welcome db 13,10,'512DevOS 0.02', 13,10,0
prompt db '>',0
helpm db 13,10,'O=reload C=cls R=reboot H=help V=ver S=halt F=scrsavr ',13,10,0
invalidm db 13,10,'?',13,10,0
haltedm db 13,10,'Halted',13,10
scrsavrm db '1001101',0


times 510-($-$$) db 0
dw 0xAA55
