BFLY_DIR	.macro
		DADST	*AR5,A			;A=Old_Met(2*j)+T // Old_Met(2*j+1)-T
		DSADT	*AR5+,B		;B=Old_Met(2*j)-T // Old_Met(2*j+1)+T
		CMPS	A,*AR4+		;New_Met(j)=(Max (Old_Met(2*j)+T,Old_Met(2*j+1)-T)
						;TRN=TRN<<1
						;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1
		CMPS	B,*AR3+		;New_Met(j+2^(k-2))=(Max (Old_Met(2*j)-T,Old_Met(2*j+1)+T)
						;TRN=TRN<<1
						;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1		
		.endm

BFLY_REV	.macro
		DSADT	*AR5,A			;A=Old_Met(2*j)-T // Old_Met(2*j+1)+T
		DADST	*AR5+,B		;B=Old_Met(2*j)+T // Old_Met(2*j+1)-T
		CMPS	A,*AR4+		;New_Met(j)=(Max (Old_Met(2*j)+T,Old_Met(2*j+1)-T)
						;TRN=TRN<<1
						;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1
		CMPS	B,*AR3+		;New_Met(j+2^(k-2))=(Max (Old_Met(2*j)-T,Old_Met(2*j+1)+T)
						;TRN=TRN<<1
						;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1		
		.endm


	.mmregs

_c_int00:
_main:
	.def	_main;
	.def	_c_int00;

K 		.set	7
n 		.set	2
MASK  		.set	3
ONE  		.set	1
TRANS_END  	.set	300h
NBWORDS  	.set	8
OUTPUT  	.set	200h
SUBCONST	.set	2000h
SUBCONS2	.set	2000h
INPUT		.equ 	088h
INDAT		.equ	080h
BITS_OUT	.set	090h

	.sect	main
	.text



	STM	#400h,AR6	;Path Decisions

NEXTSET
	SSBX 	SXM		;Sign Extension mode on
	SSBX	C16		;Dual 16-bit accumulator mode on
	STM	#80h,BK		;address of end of transtion table
	STM	INPUT,AR7	;Set Address to write data to
	STM	021h,AR0	;Offset For Circular Buffer
	STM	#081h,AR1	;Input soft decisions
	STM	#180h,AR2	;Distance calculations
	STM	#160h,AR3	;Lower new metrics
	STM	#140h,AR4	;Upper new metrics
	STM	#100h,AR5	;Old metrics
	ST	#64,*AR1-	;Do j=0:64

LOOP64			
; Rate 1/2 local distance calculation
;
	LD	*AR7,16,A		;A=SD(2*i)
	MVDK	*AR7,080h		;
	SUB	*AR1,16,A,B		;B=SD(2*i)-SD(2*i+1)
	STH	B,*AR2+			;temp(0)=difference
	ADD	*AR1+,16,A,B		;B=SD(2*i)+SD(2*i+1)
	STH	B,*AR2			;temp(1)=sum

	
	;Code generated by MATLAB to choose butterflies and distance calculations

	LD *AR2-,T 
	BFLY_REV 
	LD *AR2+,T 
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	ST	TRN,*AR6+	;Store current bits into Path Decision Matrix
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2,T 
	BFLY_REV 
	ST	TRN,*AR6+	;Store current bits into Path Decision Matrix
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	ST	TRN,*AR6+	;Store current bits into Path Decision Matrix
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2+,T
	BFLY_REV 
	LD *AR2-,T
	BFLY_REV 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
	BFLY_DIR 
	LD *AR2+,T
	BFLY_DIR 
	LD *AR2-,T
;	BFLY_DIR 
;Perform Final Butterfly Manually to Implement Circular Buffer

	DADST	*AR5,A			;A=Old_Met(2*j)+T // Old_Met(2*j+1)-T
	DSADT	*AR5+%,B		;B=Old_Met(2*j)-T // Old_Met(2*j+1)+T
	CMPS	A,*AR4+0%		;New_Met(j)=(Max (Old_Met(2*j)+T,Old_Met(2*j+1)-T)
					;TRN=TRN<<1
					;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1
	CMPS	B,*AR3+0%		;New_Met(j+2^(k-2))=(Max (Old_Met(2*j)-T,Old_Met(2*j+1)+T)
					;TRN=TRN<<1
					;If (Old_Met(2*j)+T =< Old_Met(2*j+1)-T) Then TRN[0]=1		
	ST	TRN,*AR6+	;Store current bits into Path Decision Matrix
	
;Decrement Loop Counter till 64 Iterations have been processed

	LD	*AR1,0,A	;
	SUB	#1,A		;
	BCD	LOOP64,ANEQ	;
	nop			;	
	STL	A,*AR1-		;
	;
	; Traceback routine
	;
	; A accumulator = State Value
	; B accumulator = temp storage
	; K=constraint length
	; MASK = 2^(K-5)-1
	; ONE = 1
	;

TBACK	RSBX	OVM		;turn off overflow mode
		
	LD	#SUBCONST,A	;If The First Value is > 2000h
	SUB	*AR5,A		;Subtract 2000h from all values
	BC	SUBMET,AGT	;Too Keep from Overflowing
	LD	#SUBCONS2,A	;
	STM	#63,BRC		;Load block repeat counter for loop
	RPTB	SUBMET-3	;do i=0 to 63
	LD	*AR5,B		;
	SUB	A,B		;
	STL	B,*AR5+		;Subtract constant from metrics and continue
	MAR	*+AR5(040h)%	;Reset AR5 Pointer

SUBMET	STM	#200h,BK	;address of end of transtion table
	STM	#NBWORDS-1,AR1	;number of output words to compute
	STM	#BITS_OUT,AR7	;
	MVMM	AR1,AR4		;Copy A1 in AR4 (to use in output reversal)
	STM	#OUTPUT+NBWORDS-1,AR3	;address pointer for output bits (end of buffer)
	LD	#0,A		;A contains initial state value (State=0 in this example)
	STM	#15,BRC		;Load block repeat counter for inner loop
				;do i=0 to NBWORDS-1
BACK	RPTB	TBEND-1		;do j=0 to 15
				;Calculate bit position in transtion word
	SFTL	A,-(K-2),B	;B=A>>(K-2)
	AND	#ONE,B		;B=B&1=msb of state
	ADD	A,1,B		;B=B+A<<1 = 2*state + msb of state
	STLM	B,T		;T=B (bit position)
				;Calculate correct transition word
	SFTL	A,-3,B		;B=A/8 = state/8
	AND	#MASK,B		;B=B & MASK = (K-5) lsb's of state/8
	STLM	B,AR0		;AR0 = index for transition word
	MAR	*+AR6(-2^(K-5))%	;reset pointer to start of table
	MAR	*AR6+0		;add offset to point to correct transition word
	BITT	*AR6-0		;test bit in transition word, reset to table start

	ROLTC 	A 		; Rotate decision in A
				; enddo (j loop)
TBEND 	STL 	A,*AR3- 	; Store packed output
	BANZD 	BACK,*AR1-	; repeat j loop if frame not finished
	STM 	#15,BRC 	; Init block repeat counter for next word
				; enddo (i loop)

; Reverse order of bits within words to output in correct order
;
	MAR 	*AR3+ 		; Get start of output buffer
	LD 	*AR3+,A 		; load first word into A
RVS 	SFTA 	A,-1,A 		; A>>1, A[0]-> C
	STM 	#15,BRC 	;
	RPTB 	RVS2-1 		; Do i=0,15
	ROL 	B 		; B<<1, B[0] = C
	SFTA 	A,-1,A 		; A>>1, A[0]->C
				; enddo
RVS2 	BANZD 	RVS,*AR4- 	; done with all words?
	STL 	B,*AR7	 	; save just completed word
	LD 	*AR3+,A 		; load next word

	B 	NEXTSET		;Branch to next bit
