



; ------------------= BLOCK FILL SUBROUTINE =-------------------
; fills memory from a low addr through a high address with
; a constant value.
; cmd: fill <addr1> <addr2> <val>
; --------------------------------------------------------------

block_fill_sub:
	pshy					; save the initial register state
	pshx
	pshd

	pshy					; push Y(cmd buffer addr) because we need it
	ldy		#newline		; loads pointer to null terminated string
	jsr		print_string		; prints the null terminated string
						; see "serial.asm"
	puly     				; restore the cmd buffer addr in Y
 

	jsr		parse_address		; returns addr in X
	cmpb		#$ff			; if the parsed address returns $ff
	beq		bf_derr			; it will print an error message and restore state
	pshx					; else, store the address

	jsr		parse_address		; returns end addr in X
	cmpb		#$ff			; if the parsed address returns $ff
	beq		bf_done1		; it will print an error message and restore state
	pshx					; else, store the address

	jsr		parse_byte		; gets a byte from the cmd line ret in A
	cmpb		#$ff			; if the parsed address returns $ff
						; it will print an error message and restore state
	beq		bf_done2		; else, continue with the block fill


	pulx					; pull off the high address (end)
	psha					; save the fill number
	xgdx					; D is now the high addr
	std		tempw			; save it in RAM(used in comparing the fill pointer
						; to the termination address)

*						* not sure why he doesn't just save
*						* it to memory from the x register becuase he
*						* kills what is in the D register with the next line
     
	pula					; restore the fill number to A
	pulx					; puts the low address into X
						; this will serve as the fill pointer

bf_loop:
	staa		0,x			; puts the fill number into memory
	inx					; increments the fill pointer
	cpx		tempw			; compare it with termination address

*						* this looks like the high address input is not
*						* filled in. ask dr. schwartz what he wants to happen
*						* here.

	bgt		bf_done			; branch to done if pointer addr >= end addr
	bra		bf_loop			; otherwise continue filling


						; start of the routines if there is an error
bf_done2:
	pulx					; gets rid of the stuff on the stack
						; so that state can be restored
bf_done1:
	pulx					; gets rid of the stuff on the stack
						; so that state can be restored
bf_derr:
	ldy		error_msg		; loads a pointer to the error message in Y
	jsr		print_string		; prints a null terminated string that begins
						; at the address in Y. see "serial.asm" for more

bf_done:
	puld					; restore the initial register state
	pulx
	puly
	rts
