;this file contains code designed to print text to the screen

.text
#include libExtnd.txt
#include libLabel.txt
#include libASCII.txt

.data
textColor:
0x01
.text


#global setBgCol
setBgCol:
	andi	r1, r1, 15;sanity check

	rd	r2, r0, textColor
	andi	r2, r2, 0x0f
	sli	r1, r1, 4
	or	r1, r1, r2

	wr	r1, r0, textColor
	ret

#global setFontCol
setFontCol:
	andi	r1, r1, 15;sanity check

	rd	r2, r0, textColor
	andi	r2, r2, 0xf0
	or	r1, r1, r2

	wr	r1, r0, textColor
	ret

;takes a char in r1 and puts it at the screen pointer
#global putChar
putChar:
	push	ra
	rd	r2, r0, screen_pointer
	
	;make sure screen pointer is in bounds
	movi	r3, screen_max
	bl	r2, r3, 5
	wr	r0, r0, screen_pointer
	push	r1
	call	scr_scroll
	pop	r1
	mov	r2, r0
	
	;add color to character
	rd	r3, r0, textColor
	sli	r3, r3, 8
	or	r1, r1, r3
	
	;get address
	andi	r3, r2, 1
	sri	r2, r2, 1
	;take scrolling into account
	rd	r5, r0, GLOBAL_SCREEN_SCROLL
	add	r2, r2, r5
	addi	r2, r2, 496
	andi	r2, r2, 2047

	;set character at proper position
	sli	r3, r3, 4
	shl	r1, r1, r3

	;read destination character
	rd	r4, r2, GLOBAL_SCREEN

	;clear space for the character <- TODO:remove, like, 5 instructions here
	movi	r5, 0xffff
	shl	r5, r5, r3
	not	r5
	and	r4, r4, r5
	
	;merge character and write back
	or	r1, r1, r4
	wr	r1, r2, GLOBAL_SCREEN

	;increment screen_pointer and write back
	rd	r1, r0, screen_pointer
	addi	r1, r1, 1
	wr	r1, r0, screen_pointer

	;return
	pop	ra
	ret

;the same as putChar, but takes formatting into account
#global printChar
printChar:
	push	ra
	
	;if tab, align to nearest four chars
	movi	r2, 9
	bne	r1, r2, printc_n
	rd	r3, r0, screen_pointer
	andi	r3, r3, 3
	movi	r4, 4
	sub	r3, r4, r3
printc_tab_loop:
	beq	r3, r0, printc_end
	push	r3
	movi	r1, 32
	call	putChar
	pop	r3
	subi	r3, r3, 1
	br	printc_tab_loop	

	;if newline, scroll screen upwards
printc_n:
	movi	r2, 10
	bne	r2, r1, printc_r
	call	scr_scroll
	br	printc_end

	;if carrige return, set screen_pointer to 0
printc_r:
	movi	r2, 13
	bne	r1, r2, printc_put
	wr	r0, r0, screen_pointer
	br	printc_end

	;else call putchar
printc_put:
	call	putChar

printc_end:	
	pop	ra
	ret

;prints the string r1 points to until it finds a null character
;this function has no regard for wrapping around the screen etc.
;this functions now uses string instead of #string_compact strings
#global printStr
printStr:
	push	a4
	push	ra
	mov	a4, r1

printsLoop:
	rd	r1, a4, 0
	beq	r1, r0, prints_end
	call	printChar
	addi	a4, a4, 1
	br	printsLoop

prints_end:
	pop	ra
	pop	a4

	ret

;same as above, except it prints a newline+carrige return after the text
#global printStrLn
printStrLn:
	push	ra

	call	printStr
	movi	r1, 10
	call	printChar
	movi	r1, 13
	call	printChar

	pop	ra
	ret

;character to fill with in r1
#global fillScreen
fillScreen:
	sli	r1, r1, 16
	rd	r2, r0, textColor
	sli	r2, r2, 8
	or	r1, r1, r2
	sli	r2, r2, 16
	or	r1, r1, r2

	rd	r2, r0, GLOBAL_SCREEN_SCROLL
	mov	r3, r2
fillScreenLoop:
	wr	r1, r2, GLOBAL_SCREEN+0
	wr	r1, r2, GLOBAL_SCREEN+1
	wr	r1, r2, GLOBAL_SCREEN+2
	wr	r1, r2, GLOBAL_SCREEN+3
	wr	r1, r2, GLOBAL_SCREEN+4
	wr	r1, r2, GLOBAL_SCREEN+5
	wr	r1, r2, GLOBAL_SCREEN+6
	wr	r1, r2, GLOBAL_SCREEN+7

	addi	r2, r2, 8
	andi	r2, r2, 2047
	bne	r2, r3, fillScreenLoop

	ret

#global scr_scroll
scr_scroll:
	rd	r1, r0, GLOBAL_SCREEN_SCROLL
	addi	r1, r1, 16
	wr	r1, r0, GLOBAL_SCREEN_SCROLL
	addi	r1, r1, 496
	andi	r1, r1, 2047
	movi	r2, 16
scrollClearLine:
	;clear last line on the screen
	wr	r0, r1, GLOBAL_SCREEN
	addi	r1, r1, 1
	subi	r2, r2, 1
	bi	r2, scrollClearLine
	ret

#global scr_scroll_down
scr_scroll_down:
	rd	r1, r0, GLOBAL_SCREEN_SCROLL
	subi	r1, r1, 16
	wr	r1, r0, GLOBAL_SCREEN_SCROLL
	ret


#global moveCursorBack
moveCursorBack:
	rd	r1, r0, screen_pointer
	subi	r1, r1, 1
	bl	r1, r0, 2
	wr	r1, r0, screen_pointer
	ret
	call	scr_scroll_down
	movi	r1, 31
	wr	r1, r0, screen_pointer
	
	ret

;*************************************************************************************
;input code below

.data
;set value after "input_function" to pointer to function that reads a keypress
;function returns keycode in r1
;r2 is either 1 or 0 depending on if there was a keypress to read or not.
;function is also responsible for resetting all neccessary flags by itself
#global libt_input_function
libt_input_function:
	get_keypress
;called when the code needs to wait for input
#global libt_input_wait_function
libt_input_wait_function:
	null_function
.text

;does nothing
null_function:
	ret


;get keyboard keypress, non-blocking
;does not unset the keyboard when reading
;return 1 in r2 if success, 0 if fail
;return keycode in r1
#global get_keypress
get_keypress:
	rd	r2, r0, GLOBAL_KEYBOARD_STATUS
	andi	r2, r2, 1
	rd	r1, r0, GLOBAL_KEYBOARD
	beq	r2, r0, 1
	wr	r0, r0, GLOBAL_KEYBOARD_STATUS;make sure we don't read this char again
	ret

;wait until a text-key has been pressed, then return it in r1
;guaranteed to be a ascii char
#global readChar
readChar:
	push	ra
	push	a4
	movi	a4, 255
	br	rdc_nowait;we should not sleep every time we look for a keypress
rdc_loop:
	rd	r3, r0, libt_input_wait_function
	callr	r3
rdc_nowait:
	rd	r3, r0, libt_input_function
	callr	r3

	beq	r2, r0, rdc_loop
	bl	r1, r0, rdc_loop
	bl	a4, r1, rdc_loop
	pop	a4
	pop	ra
	ret


;rudimentary string editing tool.
;pointer to string to be edited in r1
;maximum string length in r2
#global editString
editString:
	;print the string and then turn everything over to readStr, which has the functionality we need
	push	a4
	push	a5
	push	a6
	mov	a5, r1
	mov	a6, r2
	call	printStr
	movi	a4, 0
	mov	r1, a5
	subi	r2, a6, 1
editString_getStringSizeLoop:
	rd	r5, r3, 1
	beq	r5, r0, editString_endloop
	addi	a4, a4, 1
	br	editString_getStringSizeLoop
editString_endloop:
	br	readStrLoop
	

;reads an enire string and stores it in the area pointed to by r1
;max length of string stored in r2
;enter is used to signify end of input
;the string is null terminated
;returns the pointer to the string in r1 unchanged
#global readString
readString:
	push	ra
	push	a4
	push	a5
	push	a6
	movi	a4, 0
	mov	a5, r1
	mov	a6, r2
	subi	a6, a6, 1;leave space for null termination
readStrLoop:
	;read characters until enter is pressed.
	;if backspace is pressed, remove one char from the stack
	call	readChar
	;make sure we did not press enter
	movi	r3, C_LF
	beq	r3, r1, readStrEnd
	
	;handle backspace
	movi	r3, C_BS
	bne	r3, r1, readStrChar
	beq	a4, r0, readStrLoop
	
	;remove last input char from string
	subi	a4, a4, 1
	;remove printed character from screen
	call	moveCursorBack
	movi	r1, 0
	call	putChar;remove last printed char
	call	moveCursorBack;make sure we are now printing to recently emptied space
	
	br	readStrLoop
readStrChar:
	beq	a4, a6, readStrLoop

	;print character
	push	r1
	call	printChar
	pop	r1
	
	;set character at current location in thing
	wrr	r1, a4, a5
	addi	a4, a4, 1

	;jump back to loop
	br	readStrLoop
readStrEnd:
	
	;insert null termination
	wrr	r0, a4, a5

	;print newline
	movi	r1, C_LF
	call	printChar
	movi	r1, C_CR
	call	printChar

	;return pointer
	mov	r1, a5

	pop	a6
	pop	a5
	pop	a4
	pop	ra
	ret

;*************************************************************

.data
screen_pointer:
	0
#define screen_max 32