EEPROM programming routines for the 68HC11 (in assembler)

eeprom.s
; ---------------------------------------------------------------------------
; Function	eepr_write
; Description	Write data byte in Akku A to EEPROM address given in X
; Last update	2005-10-16
; Stack usage	1 byte (plus jsr delay plus delay routine)
; Cycle count	
; Code size	? bytes
; ---------------------------------------------------------------------------

	include	"hc11regs.i"

; Note: We only need to erase if databit is 1 but eeprom is 0 (we can
; only program 1-bits into 0-bits). So ERASE = DATA AND !EEPR
; or, since we can complement data more easily, !ERASE = !DATA OR EEPR
eepr_write:
	cmpa	0,X			; maybe we already have what we want? 
	beq	_eepr_wr_done		; then we are done!
	pshb
	tab
	comb
	orab	0,X
	bne	_eepr_wr_pgm
_eepr_wr_erase:
	ldab	#%00010110	; BYTE,ERASE,EELAT=1
	stab	REGBASE+PPROG
	stab	0,X		; write any dummy data
	ldab	#%00010111	; EEPGM=1
	stab	REGBASE+PPROG
	jsr	delay10ms
	clr	REGBASE+PPROG	; BYTE,ERASE,EELAT,EEPGM=0
_eepr_wr_pgm:
	ldab	#%00000010
	stab	REGBASE+PPROG	; EELAT=1
	staa	0,X
	ldab	#%00000011
	stab	REGBASE+PPROG	; EELAT=1, EEPGM=1
	jsr	delay10ms
	clr	REGBASE+PPROG	; EELAT=0, EEPGM=0
	ldaa	0,X		; read back for verifying
	pulb
_eepr_wr_done:
	rts