	TTL	CopyBlockVerRound

		AREA	|.drectve|, DRECTVE
	DCB	"-defaultlib:coredll.lib "
	DCB	"-defaultlib:corelibc.lib "
	
	EXPORT	|CopyBlockVerRound|

	AREA	|.pdata|, PDATA
|$T224|	DCD	|CopyBlockVerRound|
	DCD	0x40000300
	
	AREA	|.text|, CODE
	
|CopyBlockVerRound| PROC

;;; 
;;; void CopyBlockVerRound(unsigned char * Src, unsigned char * Dst, int Stride)
;;; {
;;; 	int dy, dx;
;;; 
;;; 	for (dy = 8; dy != 0; dy--) {
;;; 		for (dx = 0; dx < 8; dx++) {
;;; 			Dst[dx] = (Src[dx] + Src[dx+Stride]) >> 1; // ver interpolation with rounding
;;; 		}
;;; 		Src += Stride;
;;; 		Dst += Stride;
;;; 	}
;;; }
;;; 
;;; 
	
;;; r0 is the src pointer
;;; r1 is the test pointer
;;; r2 is the stride (x and y)
;;; 
;;; 
;;; r8 is the source preload pointer
;;; 
;;; r9 is inner loop counter
;;; r10 incrementing src pointer
;;; r11 incrementing dst pointer
;;; r12 is outer loop counter

; 	b	mytarget	
	
	stmdb     sp!, {r4 - r10, r11, lr}  ; stmfd

	
;;; start the data in (first two strides)

	& 0xf5d0f000		; pld[r0] loop 1 src beginning  
	& 0xf5d0f007		; pld[r0,#f] loop 1 src end
	add	r8,r2,r0	; 
	& 0xf5d8f000		; pld[r8] loop 2 src beginning
	& 0xf5d8f007		; pld[r8,#f] loop 2 src end
	add	r8,r2,r8	; r8 has pointer to next src line

	mov	r12,#8		; outer loop counter
	
cbvr_outer

	;; preload the n+2 stride
	& 0xf5d8f000		; pld[r8] loop N+2 src beginning
	& 0xf5d8f007		; pld[r8,#f] loop N+2 src end
	add	r8,r2,r8	; r8 has pointer to next src line

	;; get started on loop n
	
	mov	r10,r0		; copy of src address
	mov	r11,r1		; copy of dst address
	
	mov	r9,#4		; inner loop counter (4 loops, 2 bytes each)

cbvr_inner

	ldrb	r4,[r10,r2]	; get byte from next line
	ldrb	r3,[r10],#1	; get byte from current line (increment byte)
	
	ldrb	r6,[r10,r2]	; get byte from next line
	ldrb	r5,[r10],#1	; get byte from current line (increment byte)

	;; calculate dst[0-1]

	;; dst[0] = (src[0] + src[0 + stride]) >> 1
	add	r3,r4,r3	
	mov	r3,r3,ASR #1

	;; dst[1] = (src[n] + src[n + stride]) >> 1
	add	r5,r6,r5	
	mov	r5,r5,ASR #1

	;; store off first 2 destination words
	strb	r3,[r11],#1
	strb	r5,[r11],#1
	
	subs	r9,r9,#1
	bne	cbvr_inner

;;; adjust the stride
	add	r0,r0,r2	; src stride
	add	r1,r1,r2	; dst stride
	
	subs	r12,r12,#1
	bne	cbvr_outer

	ldmia     sp!, {r4 - r10, r11, pc}  ; ldmfd
|$M31912|
	
	ENDP  ; |CopyBlockVerRound|

	END