;My very first program in low level language
;i've never before porgrammed anything except gwbasic, and
;a little of qbasic that came with dos...I don't even own a graph 
;link or TI-85 calculator... please tell me if this works....
;this is an etch-a-sketch program...this is just like the drawing
;program that is inside the TI-85 to begin with (the one you use
;to draw the *.85i files

#INCLUDE "TI-85.H"

.org 0
.db "Etch-a-Sketch",0

	ld a, 4
	out (5), a

	CALL_ROM(CLEARLCD)
	ld b, 63
	ld c, 31

Welcome Screen:
	ld hl, (PROGRAM_ADDR)
	ld de, String1
	add hl,de
	ROM_CALL(D_ZM_STR) 
	ld hl, (PORGRAM_ADDR)
	ld de, String2
	add hl,de
	ROM_CALL(D_ZM_STR)
	ld hl, (PROGRAM_ADDR)
	ld de, String3
	add hl,de
	ROM_CALL(D_ZM_STR)
	ld hl, (PROGRAM_ADDR)
	ld de, String4
	add hl,de
	ROM_CALL(D_ZM_STR)
	ld hl, (PROGRAM_ADDR)
	ld de, String5
	add hl,de
	ROM_CALL(D_ZM_STR)
	ld hl, (PROGRAM_ADDR)
	ld de, String6
	add hl,de
	ROM_CALL(D_ZM_STR)

WaitTillDoneReading:		
	call GET_KEY
	cp $09			;enter key pressed?
	jr z, KeyPress
	jr WaitTillDoneReading

KeyPress:
	CALL GET_KEY
	cp $37			;exit key
	call z, exitkeypressed
	cp $2			;left key
	call z, leftkeypressed
	cp $3			;right key
	call z, rightkeypressed
	cp $4			;up key
	call z, upkeypressed
	cp $1			;down key
	call z, downkeypressed
	jr nz, KeyPress

exitkeypressed:
	ret

leftkeypressed:
	dec b
	CALL_(wall)		;is end of screen here?
	CALL_(PutPixel)
	ret

rightkeypressed:
	inc b
	CALL_(wall)
	CALL_(PutPixel)
	ret

upkeypressed:
	inc c
	CALL_(wall)
	CALL_(PutPixel)
	ret

downkeypressed:
	dec c
	CALL_(wall)
	CALL_(PutPixel)
	ret

wall:
	ld a, 1			;if b=1 then go left no further
	cp b
	call z, noleft 
	cp c
	call z, nodown		;if c=1 then go down no further
	ld a, 126
	cp b
	call z, noright
	ld a, 62
	cp c
	call z, noup
	ret
noleft:
	inc b
	ret
noright:
	dec b
	ret
noup:
	dec c
	ret
nodown:
	inc c
	ret 

PutPixel:

   push bc                  ;Save away, so it can be used again
   ROM_CALL(FIND_PIXEL)     ;Get the pixel offset
   ld   de,$FC00
   add  hl,de               ;Point into the graphics memory
   or   (HL)                ;A now contains the modified screen byte
   ld   (HL),A              ;Write back to screen to update
   pop  bc                  ;Restore
   ret

String1:	.db "Welcome to Etch-a-Sketch",0
String2:	.db "By: The Player",0
String3:	.db "Easy Controls!",0
String4:	.db "Up--move up      Down--move down",0
String5:	.db "Left--move left  Right--move right",0
String6:	.db "Press [ENTER] to continue",0

