; Licensed by the Delight Corporation. (C) 2013.
; History: 04 October 2013: Created by TimRyaz
BITS 16

buffer equ 24576

MAIN:
cli 
mov ax, 0x0000
mov ss, ax
mov sp, 0xFFFF
sti

mov ax, 2000h
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax

mov [bootdrive], dl

setup_bg:
mov dx, 0
call    move_cursor

mov ah, 09h
mov al, ''
mov bh, 0
mov bl, 00000111b
mov cx, 2400
int 10h

mov si, version
call    PRINT

call    newline

mov si, copyright
call    PRINT

cmd:
call    newline

mov si, prompt
call    PRINT

mov di, input_buffer

mov al, 0
mov cx, 256
rep stosb

mov ax, input_buffer
mov di, input_buffer

.loop:
call    keyboard

cmp al, 13
je .done

cmp al, 8
je .backspace

jmp .character

.backspace:
mov ah, 0Eh
mov al, 8
int 10h
mov al, 32
int 10h
mov al, 8
int 10h
dec di
jmp .loop

.character:
mov ah, 0Eh
int 10h
stosb
jmp .loop

.done:
mov ax, 0
stosb

call newline

mov si, input_buffer
cmp BYTE [si], 0

je cmd

mov di, help_string
call compare
jc help

mov di, help_uc
call compare
jc help

mov di, dir_string
call compare
jc dir

mov di, dir_uc
call compare
jc dir
;what if it is not a true command or file?
mov si, input_buffer
call    PRINT

mov si, no_command

bootdrive      db  0
exit_string	   db  'exit',0
exit_uc		   db  'EXIT',0
dirlist          times 2048 db 0
dir_string     db  'dir', 0
dir_uc         db  'DIR', 0
no_command     db  'Bad command or file name', 0
help_uc        db  'HELP', 0
help_string    db  'help', 0
input_buffer  times    256 db 0
prompt         db  'C>', 0 
version        db  'Thunder-DOS 0.01 Build 1 (build date 2043-10-13-13)', 0
copyright      db  '(C) 2013 Delight Corporation. All rights reserved', 0
help_message   db	'HELP',9,9,9,'Show this message',13,10,'DIR',9,9,9,9,'Show the contents of your directory',13,10,'EXIT',9,9,9,'Shut down your computer',13,10,'format',9,9,9,'Format the disk (parameter required',13,10,'edit',9,9,9,'Edit the contents of a file',0
filename	   db	"COM     COM",0

reset_floppy:
mov ah, 0
mov dl, BYTE [bootdrive]
int 13h
ret

dir:
pusha
mov di, dirlist

call    reset_floppy

mov bx, buffer
mov cl, 2
mov ch, 0
mov dh, 1
mov ah, 2
mov al, 16
pusha
load_root:
int 13h
jnc loaded_root
call    reset_floppy
jmp load_root


loaded_root:
popa
mov si, buffer

compare_entry:
mov al, [si+11]
cmp al, 0Fh
je .skip_entry
cmp al, 18h
je .skip_entry
cmp al, 229
je .skip_entry
cmp al, 0
je .done

mov dx, si
mov cx, 0

.save_character:
mov BYTE al, [si]
cmp al, ' '
je .space
mov BYTE [di], al
inc di
inc si
inc cx
cmp cx, 8
je .add_dot
cmp cx, 11
je .string_copied
jmp .save_character

.add_dot:
mov BYTE [di], '.'
inc di
jmp .save_character

.space:
inc si
inc cx
cmp cx, 8
je .add_dot
jmp .save_character

.string_copied:
mov BYTE [di], ','
inc di
mov si, dx

.skip_entry:
add si, 32
jmp compare_entry

.done:
mov si, dirlist
mov ah, 0Eh
.print:
lodsb
cmp al, 0
je .done_printing
cmp al, ','
je .comma
int 10h
jmp .print

.comma:
call newline
jmp .print

.done_printing:
popa
jmp cmd

delete:
mov si, delete
mov dl, 0
mov di, 0

help:
mov si, help_message
call    PRINT
jmp cmd

compare:
pusha

.loop:
mov al, [si]
mov ah, [di]

cmp al, ah
jne .not_equal

cmp al, 0
je .equal

inc si
inc di
jmp .loop

.not_equal:
popa
clc
ret

.equal:
popa
stc
ret

keyboard:
pusha
mov ax, 0
mov ah, 10h
int 14h
mov [.buffer], ax
popa
mov ax, [.buffer]
ret

.buffer dw 0

newline:
pusha
mov ah, 0Eh
mov al, 13
int 10h
mov al, 10
int 10h
popa
ret

PRINT:
lodsb
cmp    al, 0
je     Done
mov    ah, 0eh
int    10h
jmp    PRINT

Done:
ret

move_cursor:
pusha
mov ah, 02h
mov bh, 0
int 10h
popa
ret

exit:
mov	ax, 5307h
mov	cx, 3
mov	bx, 1
int	15h