memwipehi:					; this routine clears all ram from $a0 to $0180
	load_a #00				; and $0120 to $01ff
	load_x #e0
loophi:
	dec_x					
	store_a $a0,x			; write 00 to ram at $[a0 + x]
	store_a $0120,x			; write 00 to ram at $[0120 + x]
	comp_x #00				 
	bne loophi				; loop while x != 00
	nop
	nop
memwipelo:					; this routine clears ram from $0000 to $0080
	load_a #00			
	load_x #80
looplo:
	dec_x					; while x != 0, decrement x, write 0 to ram at x
	store_a $00,x
	comp_x #00
	bne looplo
	nop
	nop
	nop
	nop
	nop
vars:
	define head $018c		; position of wyrm's head 
	define moveinc $018d	; number added to position to move it
	define length $018e		; length of wyrm
	define food $018f		; position of food
	define tailarr $01a0	; array of previous head positions
	define playarea $0000	; top left corner of play area
	define areasize 80		; size of play area
	define dielen 40		; wyrm is killed at this length to avoid overflow
	define keyin $019f		; address of latest keypress code
	define wkey 77			; key code for w key 
	define akey 61			; key code for a
	define skey 73			; key code for s
	define dkey 64			; key code for d
init:
	load_a #01		; start with movement x+1
	store_a moveinc
	load_a #02		; start with length 3
	store_a length
	load_a #62 		; set initial food position
	store_a food
	tax
	load_a #ff
drawinit:
	store_a playarea,x 	; draw initial food
	load_x #60
	store_x head		; draw initial wyrm head		
	load_y #00
loop:
	cli					; clear interrupt flag on each loop
arrcheck:
	comp_y length	; is the tail position array full?
	bmi arradd		; 	no, add this position to array
	txa				; 	yes, array is full, continue
	tay
	load_x tailarr		; get end of tail position		
	load_a #00		
	store_a playarea,x	; clear end of tail position
	tya
	tax
	load_y #00
arrshift:			; shift the tail position array
	load_a head		; get wyrm's head position
	comp tailarr,y	; check for head on tail collision
	beq ded			; tail collision, rip wyrm
	inc_y			; no collision, wyrm lives, move it
	load_a tailarr,y	; tailarr[y] = tailarr[y+1]
	dec_y
	store_a tailarr,y
	inc_y
	comp_y length	
	bmi arrshift		; loop while y < array length
	dec_y
	store_x tailarr,y
	inc_y
	jump updatex
arradd:
	store_x tailarr,y 	; add this last position to the array
	inc_y
updatex:			; calc next value of wyrm's head (x)
	txa
	clc
	adc moveinc		; x = x + moveinc
	tax
	comp_x #areasize
	bpl xmax		; new x > 0x80?
	comp_x #00
	bmi xmin		; new x < 0x00?
	comp_x food 	; check for food
	bne draw		;   not food, skip to draw
	load_a length	
	comp #dielen	; it's food, kill wyrm if too long
	beq ded
	jsr foodget		; wyrm lives, run food get subroutine
draw:
	store_x head	; set current head pos
	load_a #ff
	store_a playarea,x	; draw head at x
	jump loop
xmax: 				; wrap x down to 0x00
	txa
	clc
	adc #80
	tax
	jump draw
xmin:				; wrap x up to 0x7f
	txa
	clc
	adc #80
	tax
	jump draw
foodget:
	load_a length
	inc_a
	store_a length 	; wyrm length += 1
	txa
	nop
	push_a
	load_a food
	clc
	adc #29			; add 29 for new food location
	comp #7f
	bmi foodset		; check if new food too high
	clc
	adc #80 		; new food too high, wrap down
foodset:
	store_a food 	; set new food location
	tax
	load_a #ff
	store_a playarea,x 	; draw new food
	pull_a
	tax
	rts
ded:				; wyrm ded. wipe play area and start over
	jump memwipelo
isr:				; interrupt subroutine to handle keypress
	push_a
	load_a keyin
	comp #wkey
	beq wkeypress
	comp #akey
	beq akeypress
	comp #skey
	beq skeypress
	comp #dkey
	beq dkeypress
	pull_a
	rti
wkeypress:
	load_a #f0
	store_a moveinc
	pull_a
	rti
akeypress:
	load_a #ff
	store_a moveinc
	pull_a
	rti
skeypress:
	load_a #10
	store_a moveinc
	pull_a
	rti
dkeypress:
	load_a #01
	store_a moveinc
	pull_a
	rti