; Command-line interface core
	;Initialize
	ld hl,0
	ld (cursorX),hl
	call ClearGraphBuffer
	ld hl,CEPTIC_Name
	call VPutStr
	;Later put a speed check here!
	ld hl,14*256
	ld (cursorX),hl
CLI_Input_Restart:
	xor a
	ld (userInputLen),a		;Nothing typed yet
	call CLI_Show_Prompt
	call GrBufCpy
CLI_Input_Loop:
	bit onKey,(iy+keyFlags)
	jp nz,CLI_TurnOff
	ei
	halt
	halt
	;Now we will check for a byte being sent over the link port (silent linking)
	in a,(0)
	and 3
	cp 3
	jr z,CLI_NoSilentLink
	in a,(9)
	bit 6,a
	jr nz,CLI_NoSilentLink
	bit 4,a
	jp nz,SilentLink
CLI_NoSilentLink:
	call GetCSC
	or a
	jp z,CLI_Input_Loop
	cp skAlpha
	jr nz,CLI_Input_NotAlpha
	ld a,(iy+keyFlags)
	xor 1			;Toggle the alpha flag which is bit 0
	ld (iy+keyFlags),a
CLI_Input_NotAlpha:
	cp skClear			;Clear the current line
	jp z,CLI_Input_Clear
	cp skDel
	jr z,CLI_Input_Backspace
	cp skMode			;This toggles between flash and RAM
	jp z,CLI_Input_ToggleDrive
	cp skEnter			;They've pressed enter
	jp z,CLI_Parser
	ld b,a
	ld a,(userInputLen)
	cp 50			;If the user already entered 50 characters, then no more
	jr z,CLI_Input_Loop
	ld a,b
	;The user pressed a key. Now we'll check if it's a valid one, and display it if so.
	bit alphaOn,(iy+keyFlags)
	jr z,CLI_Input_NonAlphaKey
	ld hl,CLI_ValidKeys
	ld bc,27
	cpir
	jr nz,CLI_Input_Loop
	ld de,26
	jr CLI_Input_AlphaJumpIn
CLI_Input_NonAlphaKey:
	;The only keys we want are 0-9
	ld hl,CLI_NumberKeys
	ld bc,10
	cpir
	jr nz,CLI_Input_Loop		;It is a bad key
	ld de,9
CLI_Input_AlphaJumpIn:
	add hl,de
	ld a,(hl)		;ASCII representation
	ld b,a
	ld a,(userInputLen)
	ld d,0
	ld e,a
	ld hl,userInput
	add hl,de
	ld a,b
	ld (hl),a		;We've loaded it in
	ld hl,userInputLen
	inc (hl)
	call VPutMap
	jr nc,CLI_Input_AllDone
	;We need to wrap
	call CLI_Newline
	ld a,c
	call VPutMap
CLI_Input_AllDone:
	call GrBufCpy
	jp CLI_Input_Loop
CLI_Input_Backspace:
	;Backspace at the command prompt
	ld a,(userInputLen)
	or a
	jp z,CLI_Input_Loop		;We can't backspace with nothing there!
	dec a
	ld d,0
	ld e,a
	ld hl,userInput
	add hl,de
	ld a,(hl)
	call CharacterWidth
	inc b
	ld a,(cursorX)
	sub b
	jr c,CLI_Input_Backspace_UpALine
	ld (cursorX),a
	push af
	ld a,1Fh		;white box
	call VPutMap
	ld a,1Fh
	call VPutMap
	pop af
	ld (cursorX),a
	ld hl,userInputLen
	dec (hl)
	jp CLI_Input_Loop
CLI_Input_Backspace_UpALine:
	call Cursor_Clear
	;This means we need to erase and then move up a line
	ld hl,userInput
	ld a,(userInputLen)
	ld b,a
	dec b
	ld a,(promptOffset)
CLI_Input_Backspace_UpALine_Loop:
	push bc
	push af
	ld a,(hl)
	push hl
	call CharacterWidth
	pop hl
	pop af
	inc hl
	add a,b
	inc a
	cp 97
	jr c,CLI_Input_Backspace_UpALine_OK
	ld a,b
CLI_Input_Backspace_UpALine_OK:
	pop bc
	djnz CLI_Input_Backspace_UpALine_Loop
	;Now A is the X cursor
	push af
	ld a,(cursorY)
	sub 7
	ld (cursorY),a
	ld a,1Fh			;clear box
	call VPutMap
	pop af
	ld (cursorX),a
	ld hl,userInputLen
	dec (hl)
	jp CLI_Input_Loop
CLI_NumberKeys:
	DB sk1,sk2,sk3,sk4,sk5,sk6,sk7,sk8,sk9,sk0
	DB "1234567890"
CLI_ValidKeys:
	DB skMath,skApps,skPrgm,skInverse
	DB skSin,skCos,skTan,skPower
	DB skSquared,skComma,skLParen,skRParen
	DB skDivide,skLog,sk7,sk8
	DB sk9,skMultiply,skLn,sk4
	DB sk5,sk6,skMinus,skSto
	DB sk1,sk2,sk0
	DB "abcdefghijklmnopqrstuvwxyz "
CLI_Input_ToggleDrive:
	ld a,(iy+memoryFlags)
	xor 1				;toggle driveActive flag
	ld (iy+memoryFlags),a
CLI_Input_Clear:
	call Cursor_Clear
	ld hl,0
	ld (cursorX),hl
	call ClearGraphBuffer
	jp CLI_Input_Restart
CLI_NewPrompt:
	call CLI_Newline
CLI_Show_Prompt:
	;This displays the prompt before the cursor
	res cursorEnabled,(iy+miscFlags)
	call Cursor_Clear
	bit driveActive,(iy+memoryFlags)		;0 for flash, 1 for RAM
	jr nz,CLI_Show_Prompt_RAM
	ld hl,CLI_Memory_Flash
	jr CLI_Show_Prompt_Folder
CLI_Show_Prompt_RAM:
	ld hl,CLI_Memory_RAM
CLI_Show_Prompt_Folder:
	call VPutStr
	;Now display the name of the current folder after the prompt
	call DisplayCurrentFolder
	ld a,':'
	call VPutMap
	ld a,(cursorX)
	ld (promptOffset),a
	set cursorEnabled,(iy+miscFlags)
	ret
CLI_Memory_Flash:
	DB "flash",0
CLI_Memory_RAM:
	DB "ram",0
CLI_Parser:
	;Turn off the cursor
	res cursorEnabled,(iy+miscFlags)
	call Cursor_Clear
	;We will now parse the user's input
	ld hl,CLI_Commands
CLI_Parser_Loop:
	ld de,userInput
	ld a,(hl)
	or a
	jr z,CLI_NewPrompt
	inc hl
	ld b,a
	cp a
	jr c,CLI_Parser_MoveOn
	;Now check it
CLI_Parser_SubLoop:
	ld a,(de)
	cp (hl)
	jr nz,CLI_Parser_MoveOn
	inc hl
	inc de
	djnz CLI_Parser_SubLoop
	;We have a match
	ld e,(hl)
	inc hl
	ld d,(hl)
	ex de,hl
	jp (hl)
CLI_Parser_MoveOn:
	ld c,b
	ld b,0
	add hl,bc
	jr CLI_Parser_Loop
CLI_Commands:
	DB 6,"format"
	DW CLI_Format
	DB 0
CLI_Format:
	;This will format your flash/RAM
	bit driveActive,(iy+memoryFlags)
	jr z,CLI_Format_Flash
	;Do RAM format here
	ld hl,variableTable-17
	ld (varTableEnd),hl
	ld hl,RAM_Root_Entry+17
	ld de,variableTable
	ld bc,18
	lddr
	call CLI_Newline
	ld hl,CLI_Format_Complete
	call VPutStr
	call CLI_Newline
	jp CLI_Input_Restart
CLI_Format_Flash:
	;Now clear all the flash pages
	;64KB sectors a time
	ld a,04h
CLI_Format_Flash_Loop:
	push af
	call Flash_Erase
CLI_Format_Flash_ShowProgress:
	ld a,'.'
	call VPutMap
	jr nc,CLI_Format_Flash_ProgressOK
	call CLI_Newline
	jr CLI_Format_Flash_ShowProgress
CLI_Format_Flash_ProgressOK:
	pop af
	add a,4			;64KB
	cp 7Ch
	jr c,CLI_Format_Flash_Loop
	;Done formatting flash.
	;Display a message about it
	call CLI_Newline
	ld hl,CLI_Format_Complete
	call VPutStr
	call CLI_Newline
	jp CLI_Input_Restart
CLI_Format_Complete:
	DB "Format complete.",0
CLI_TurnOff:
	res onKey,(iy+keyFlags)
	ld a,02h
	out (10h),a		;Turn off LCD
	ld a,08h
	out (3),a
	call LCD_Delay
	ld a,36h
	out (4),a
	ld a,01h
	out (3),a
	call RAM_FAT_Checksum
	ld (RAMChecksum),hl
	ex af,af'
	exx
	ei
	halt		;It halts until ON is pressed
	ld a,0Bh
	out (3),a
	ld a,03h
	out (10h),a			;Turn the LCD back on
	res onKey,(iy+keyFlags)
	jp CLI_Input_Loop
CLI_Newline:
	xor a
	ld (cursorX),a
	ld a,(cursorY)
	add a,7
	ld (cursorY),a
	ret
 INCLUDE "silentlink.asm"
CEPTIC_Name:
	DB "CEPTIC/0.1",0


