	TTL	CopyBlockHorVer

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

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

;;; 
;;; void CopyBlockHorVer(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+1] +
;;; 	   Src[dx+Stride] + Src[dx+Stride+1] +2) >> 2; // horver 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
	
cbhv_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 for preload

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

	ldrb	r4,[r10,r2]	; src[n + stride]
	ldrb	r3,[r10],#1	; src[n]
	
cbhv_inner

	ldrb	r6,[r10,r2]	; src[n + stride + 1]
	ldrb	r5,[r10],#1	; src[n + 1], increment pointer for next loop

	;; calculate dst[n]

	;; dst[n] = (src[n] + src[n + stride] +src[n] + src[n + stride] + 2) >> 2
	add	r3,r4,r3	; src[n] + src[n+stride]
	add	r3,r6,r3	; + src[n + stride + 1]
	add	r3,r5,r3	; + src[n + 1]
	add	r3,r3,#2	; + 2

 	mov	r3,r3,ASR #2	; >> 2

	;; store off the first destination byte
	strb	r3,[r11],#1

	mov	r4,r6
	mov	r3,r5
		
	subs	r9,r9,#1
	bne	cbhv_inner

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

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

	END