.nolist
    #include    "ti83plus.inc"
.list
.org    $9D93
.db    t2ByteTok, tAsmCmp
;blank the screen
	LD DE,PlotSScreen
	LD BC,$02F4
	XOR A
blank:
	LD (DE),A
	INC DE
	DEC BC
	CP C
	JP NZ, blank
	CP B
	JP NZ, blank
	b_call(_GrBufCpy)
	
loop:

;ant is turning vert to horiz
	
	LD HL, (currbyte)	;get location of current byte
	LD A, (currbit)		;get bitmask for current bit
	AND (HL)			;test if bit is on using Z flag
	JP NZ, hpxon
	
;hpxoff:	;if pixel off, flip dirpar
	LD A, (dirpar)
	XOR %0000001
	LD (dirpar), A
hpxon:	;if pixel on, don't flip dirpar

	;toggle current pixel
	LD A, (currbit)		;get bitmask for current bit again
	XOR (HL)			;this is the new byte, with pixel flipped
	LD (HL), A			;put the new byte in the graph buffer
	
	;jump depending on dirpar
	LD A, (dirpar)
	AND A				;dummy operation to set Z flag
	JP NZ, goright
	
;goleft: ;dirpar = 0: try to move one pixel left; if at left side of screen, halt
	LD A, (xpos)
	DEC A
	RET M				;stop if xpos < 0
	LD (xpos), A		;update x position
	LD A, (currbit)		;get bitmask for current bit again
	RLCA				;move one pixel left
	LD (currbit), A		;save currbit
	JP NC, horizdone	;if byte didn't change, we're done
	DEC HL				;bit flipped from 7 to 0, so decrement HL
	JP horizdone
	
goright: ;dirpar = 1: try to move one pixel right; if at right side of screen, halt
	LD A, (xpos)
	INC A
	CP 95
	RET P				;stop if xpos > 95
	LD (xpos), A		;update x position
	LD A, (currbit)		;get bitmask for current bit again
	RRCA				;move one pixel right
	LD (currbit), A		;save currbit
	JP NC, horizdone	;if byte didn't change, we're done
	INC HL				;bit flipped from 0 to 7, so increment HL

horizdone:
	LD (currbyte), HL	;update currbyte
;	b_call(_GrBufCpy)	;update screen
	
;ant is turning horiz to vert
	
;	LD HL, (currbyte)	;get location of current byte
	LD A, (currbit)		;get bitmask for current bit
	AND (HL)			;test if bit is on using Z flag
	JP Z, vpxoff
	
;vpxon:	;if pixel on, flip dirpar
	LD A, (dirpar)
	XOR %0000001
	LD (dirpar), A
vpxoff:	;if pixel off, don't flip dirpar

	;toggle current pixel
	LD A, (currbit)		;get bitmask for current bit again
	XOR (HL)			;this is the new byte, with pixel flipped
	LD (HL), A			;put the new byte in the graph buffer
	
	;jump depending on dirpar
	LD A, (dirpar)
	AND A				;dummy operation to set Z flag
	JP NZ, godown
	
;goup: ;dirpar = 0: try to move one pixel up; if at top of screen, halt
	LD A, (ypos)
	DEC A
	RET M				;stop if ypos < 0
	LD (ypos), A		;update y position
	LD BC, -12
	ADD HL, BC			;add -12 to HL: decrease row
	JP vertdone
	
godown: ;dirpar = 1: try to move one pixel down; if at bottom of screen, halt
	LD A, (ypos)
	INC A
	CP 63
	RET P				;stop if ypos > 63
	LD (ypos), A		;update y position
	LD BC, 12
	ADD HL, BC			;add 12 to HL: increase row
	
vertdone:
	LD (currbyte), HL	;update currbyte
	b_call(_GrBufCpy)	;update screen
	
	b_call(_GetCSC)		;exit if key pressed
	AND A				;dummy operation to set Z flag
    JP Z, loop
	
    RET

xpos: .db 47
ypos: .db 31
	
dirpar: .db 0	;0=up/left, 1=down/right

currbyte: .dw PlotSScreen + 377 ;starting y is 31, starting x is 47, [47/8] = 5, 31*12+5 = 377
currbit: .db %00000001 ;last bit of byte 5 in row 31

	
.end
.end