; check 32 bytes of memory starting at a0.
; returns in d0 the erroneous bits set
; this routine disables interrupts and may thus affect timing critical programs
; on a 7MHz 68000, interrupts are disabled for approximately 200 microseconds
; BOOL __asm CheckMemBlock( register __a0 address );

	INCLUDE "exec/funcdef.i"
	INCLUDE "exec/exec_lib.i"

	xdef _CheckMemBlock
	
	section text,code

_CheckMemBlock:
	movem.l	d1-d4/a1/a2/a6,-(sp)
	move.l	4,a6
	moveq.l	#0,d0
	lea	_CheckMemBlock(pc),a1
	lea	end(pc),a2
	cmpa.l	a0,a2
	bmi	cont			; block starts higher than end of routine
	movea.l	a0,a2
	adda.l	#32,a2
	cmpa.l	a2,a1
	bmi	exit			; block and routine overlap.. assumed memory good

cont:
	moveq.l	#7,d2
	jsr	_LVODisable(a6)		; appr 1400 cycles with interrupts disabled
loop:
	move.l	(a0),d1

	moveq.l #0,d3			; check 00000000000000000000000000000000
	move.l	d3,(a0)
	move.l	(a0),d4
	eor.l	d4,d3
	or.l	d3,d0

	moveq.l	#-1,d3			; check 11111111111111111111111111111111
	move.l	d3,(a0)
	move.l	(a0),d4
	eor.l	d4,d3
	or.l	d3,d0

	move.l	#$55555555,d3		; check 01010101010101010101010101010101
	move.l	d3,(a0)
	move.l	(a0),d4
	eor.l	d4,d3
	or.l	d3,d0

	move.l	#$aaaaaaaa,d3		; check 10101010101010101010101010101010
	move.l	d3,(a0)
	move.l	(a0),d4
	eor.l	d4,d3
	or.l	d3,d0

	move.l	d1,(a0)+
	dbf	d2,loop

	jsr	_LVOEnable(a6)
exit:
	movem.l	(sp)+,d1-d4/a1/a2/a6
	rts
end:
	end
