	.def   _InoutIsr       
	.def   _fir_asm_init
	.sect   "mycode"
			     
	.ref    _h
	.ref    _R_in       

_fir_asm_init  

	MVKL   	0x018C0000,B12
	MVKH    0x018C0000,B12
      
	MVKL    0xFFFF0000,A10  ; Use A10 as a mask
	MVKH    0xFFFF0000,A10
	MVKL    0x0000FFFF,A11  ; Use A11 as a mask
	MVKH    0x0000FFFF,A11

	MVKL    0x0450,B2     	; B5, A6 and  A7 used for circular addressing
	MVKLH   0x8,B2          ; N=7 -> 2^N+1 = 128 *4 each location is 4 bytes
	MVC     B2,AMR
	       
	MVKL    _R_in,A7
	MVKH    _R_in,A7
	MVKL    _h,B5
	MVKH    _h,B5
	MV      A7,A6

	MVK     63,B9   		; 127/2 = 63
	MVKH    63,B9
	MV      B9,A9    
	
	B       B3
	NOP     5


_InoutIsr     

	ZERO    A5
	LDH     *B6,A4
	LDH     *++B6,A15
	NOP     3
    INTSP   A4,A4
    INTSP   A15,A15
    NOP    	3   			; Convert fron int to Float.

	STW   	A4,*A6++     	; (A4 = SP float = 32 = STW) Pre-increment to offset x[n]
    STW   	A15,*A6++

	MVK     60,B0       	; Set the loop counter to 128 -8 = 120
	MVKH    60,B0


		ZERO 	.L1 	A8 				; sum0 = 0
|| 		ZERO 	.L2 	B8 				; sum1 = 0
|| 		LDDW  	.D1T1	*A7++,A3:A2		; load ai & ai + 1 from memory
|| 		LDDW	.D2T2   * B5++,B7:B6 	; load bi & bi + 1 from memory
	
		LDDW 	.D1 	*A7++,A3:A2		;* load ai & ai + 1 from memory
|| 		LDDW 	.D2 	*B5++,B7:B6 	;* load bi & bi + 1 from memory

	   	LDDW .D1 *A7++,A3:A2			;** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;** load bi & bi + 1 from memory

	   	LDDW .D1 *A7++,A3:A2			;*** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;*** load bi & bi + 1 from memory
||[A1] 	SUB .S1 A1,1,A1 				; decrement loop counter

	   	LDDW .D1 *A7++,A3:A2			;**** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;**** load bi & bi + 1 from memory
||[A1] 	B .S2 LOOP 						; branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;* decrement loop counter
	
	   	LDDW .D1 *A7++,A3:A2			;***** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;***** load bi & bi + 1 from memory
|| 		MPYSP .M1X A2,B6,A5 			; pi = a0 b0
|| 		MPYSP .M2X A3,B7,B10 			; pi1 = a1 b1
||[A1] 	B .S2 LOOP 						;* branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;** decrement loop counter

	   	LDDW .D1 *A7++,A3:A2			;****** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;****** load bi & bi + 1 from memory
|| 		MPYSP .M1X A2,B6,A5 			;* pi = a0 b0
|| 		MPYSP .M2X A3,B7,B10 			;* pi1 = a1 b1
||[A1] 	B .S2 LOOP 						;** branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;*** decrement loop counter


       	LDDW .D1 *A7++,A3:A2			;******* load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;******* load bi & bi + 1 from memory
|| 		MPYSP .M1X A2,B6,A5 			;** pi = a0 b0
|| 		MPYSP .M2X A3,B7,B10 			;** pi1 = a1 b1
||[A1] 	B .S2 LOOP 						;*** branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;**** decrement loop counter

	   	LDDW .D1 *A7++,A3:A2			;******** load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;******** load bi & bi + 1 from memory
|| 		MPYSP .M1X A2,B6,A5 			;*** pi = a0 b0
|| 		MPYSP .M2X A3,B7,B10 			;*** pi1 = a1 b1
||[A1] 	B .S2 LOOP 						;**** branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;***** decrement loop counter

LOOP
	   	LDDW .D1 *A7++,A3:A2			;********* load ai & ai + 1 from memory
|| 		LDDW .D2 *B5++,B7:B6 			;********* load bi & bi + 1 from memory
|| 		MPYSP .M1X A2,B6,A5 			;**** pi = a0 b0
|| 		MPYSP .M2X A3,B7,B10 			;**** pi1 = a1 b1
|| 		ADDSP .L1 A5,A8,A8 				; sum0 += (ai bi)
|| 		ADDSP .L2 B10,B8,B8 			; sum1 += (ai+1 bi+1)
||[A1] 	B .S2 LOOP 						;***** branch to loop
||[A1] 	SUB .S1 A1,1,A1 				;****** decrement loop counter

		; Branch occurs here
		ADDSP .L1X A8,B8,A0 			; sum(0) = sum0(0) + sum1(0)
		ADDSP .L2X A8,B8,B0 			; sum(1) = sum0(1) + sum1(1)
		ADDSP .L1X A8,B8,A0 			; sum(2) = sum0(2) + sum1(2)
		ADDSP .L2X A8,B8,B0 			; sum(3) = sum0(3) + sum1(3)
		
		NOP 							; wait for B0
		
		ADDSP .L1X A0,B0,A5 			; sum(01) = sum(0) + sum(1)
		NOP 							; wait for next B0
	
		ADDSP .L2X A0,B0,B5 			; sum(23) = sum(2) + sum(3)
		NOP 3
		ADDSP .L1X A5,B5,A4 			; sum = sum(01) + sum(23)

		SPTRUNC  A4,A4
		NOP	1

		MVKL 	0xFFFFFFFC,A0
		MVKH 	0xFFFFFFFC,A0
		SHR   	A4, 16, A4
		AND		A4, A0, A4
	   	STW   	A4,*B12[1]      			; Output the input and the filtered signals
	   
	   	NOP  	1

       	B   	.S2   IRP
	   	NOP     5