#
# array reverse
#
# reverses a 2N-entry array in place (N divisible by # threads)
#
# register usage:
# 	r1	- loop index
#	r2	- address of first item in array (base address)
#	r3	- address of last item in array
#	r4,r5	- temps for LW/SW (exchange)
#	r11	- address of i entry
#	r12	- address of n-i entry
#	r13	- N-1
#	r14	- N
#

	movi	r14, 6

	movi	r2, array
	addi	r13, r14, -1	| add	r3, r2, r14
	add	r1, r0, r13	| add	r3, r3, r13

loop:	add	r11, r2, r1	| sub	r12, r3, r1
	nop			| lw	r4, r11, 0
	nop			| lw	r5, r12, 0
	nop			| sw	r4, r12, 0
	addi    r1, r1, -1	| sw	r5, r11, 0
	nop			| blz	r1, next
	bne	r0, r14, loop

# at this point the array should be backwards in the cache
# now load a bunch of garbage into the cache to force writebacks

next:	movi	r2, pad
	movi	r3, 32
	movi	r5, 8
	add	r1, r0, r0
flush:	add	r4, r2, r1
	add	r1, r1, r5	| lw r0, r4, 0
	bne	r1, r3, flush	| lw r0, r4, 4

	halt
	nop
	nop
	nop
	nop

array:	.mfill	12, 1, 1

pad:	.mfill	32, 0xabcdef
