7F0CB7E0	;image-specific doohick
	accepts: A0:p->samples, A1:sizeval1, A2:t#1val * sizeval2, A3:first 3 bits of compressed data, SP+10: bitsample size (t#3val)
ADDIU	SP,SP,FFF0
SW	S1,0008 (SP)
SW	S0,0004 (SP)
OR	S0,A0,R0	;S0=A0: p->samples
OR	S1,A3,R0	;S1=A3: first three bits of compressed data: 0-7
SW	S2,000C (SP)
BLEZ	A2,7F0CBAE0	;quit if the fieldsize is silly
OR	V0,R0,R0	;V0=0	fieldcount
LW	T1,0020 (SP)	;T1=bitsample size
//7F0CB804:	loop for fieldsize
//
//
//retrieve sample and previous sample
BLEZ	A1,7F0CBAD4	;branch if sizeval1 is stupid
OR	V1,R0,R0	;V1=0
MULTU	V0,A1
SLL	T2,T1,0x1	;T2=bitsamplesize*2
OR	S2,S1,R0	;S2=S1: 3bits
MFLO	A3		;A3=fieldcount * sizeval1
ADDU	T0,A3,S0	;T0=&samples[fieldcount * sizeval1]
NOP	
//7F0CB824:	loop for all samples in segment
LBU	T6,0000 (T0)	;T6=samples[fieldcount * sizeval1]
SLTIU	AT,S2,0007	;TRUE if 3bit value is, in fact, 3bits
BLEZ	V1,7F0CB83C	;if firest value, then you can't have a previous value, now can you?
ADDU	A0,T6,T2	;A0=cur + bitsamplesize*2
BEQ	R0,R0,7F0CB840
LBU	T3,FFFF (T0)	;T3=previous sample
//7F0CB83C:
OR	T3,R0,R0	;T3=0	default previous sample ;*)
//7F0CB840:	determine backtrack sample if applicable
BLEZ	V0,7F0CB864	;branch if fieldcount <=0
OR	T4,R0,R0	;T4=0
ADDIU	T7,V0,FFFF	;T7=fieldcount-1
MULTU	T7,A1
MFLO	T8		;T8=(fieldcount-1)*sizeval1
ADDU	T9,T8,V1	;T9=(fieldcount-1)*sizeval1 + sampcount
ADDU	T6,T9,S0	;T6=&samples[(fieldcount-1)*sizeval1 + sampcount]
BEQ	R0,R0,7F0CB864
LBU	T4,0000 (T6)	;T4=backtrack sample
//7F0CB864:	determine previous backtrack sample if applicable
BLEZ	V1,7F0CB88C	;branch if sampcount <=0
OR	T5,R0,R0	;T5=0
BLEZ	V0,7F0CB88C	;branch if fieldcount <=0
ADDIU	T7,V0,FFFF	;T7=fieldcount-1
MULTU	T7,A1
MFLO	T8		;T8=(fieldcount-1)*sizeval1
ADDU	T9,T8,V1	;T9=(fieldcount-1)*sizeval1 + sampcount
ADDU	T6,T9,S0	;T6=&samples[(fieldcount-1)*sizeval1 + sampcount]
BEQ	R0,R0,7F0CB88C
LBU	T5,FFFF (T6)	;T5=previous backtrack sample
//7F0CB88C:
BEQ	AT,R0,7F0CBAC8	;skip processing if command is invalid
SLL	T7,S2,0x2	;T7=3bit value to command offset
LUI	AT,8006
ADDU	AT,AT,T7
LW	T7,BE64 (AT)	;T7=8005BE64+offset: TLB pointer
JR	T7
NOP

7F0CB8A8	img0
ADDU	T8,A0,T3	;T8=cur + bitsamplesize*2 + pre
DIV	T8,T1
ADDU	T6,S0,A3	;T6=&sample[fieldcount*width]
ADDU	T7,T6,V1	;T7=&sample[fieldcount*width+sampcount(c)]
MFHI	T9		;T9=(cur + bitsamplesize*2 + pre)%bitsamplesize
SB	T9,0000 (T7)	;store converted to final
BNEZ	T1,7F0CB8CC
NOP	
BREAK	
//7F0CB8CC:
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CB8E4
LUI	AT,8000
BNE	T8,AT,7F0CB8E4
NOP	
BREAK	
//7F0CB8E4:
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001

7F0CB8EC	img1
ADDU	T8,A0,T4	;T8=cur + bitsamplesize*2 + bcur
DIV	T8,T1
ADDU	T9,S0,A3
ADDU	T7,T9,V1	;T7=&sample[fieldcount*width + sampcount]
MFHI	T6		;T6=(cur + bitsamplesize*2 + bcur)%bitsamplesize
SB	T6,0000 (T7)	;save val to cur
BNEZ	T1,7F0CB910
NOP	
BREAK	
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CB928
LUI	AT,8000
BNE	T8,AT,7F0CB928
NOP	
BREAK	
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001

7F0CB930	img2
ADDU	T8,A0,T5	;T8=cur + bitsamplesize*2 + bpre
DIV	T8,T1
ADDU	T6,S0,A3
ADDU	T7,T6,V1	;T7=&sample[fieldcount*width + sampcount]
MFHI	T9		;T9=(cur + bitsamplesize*2 + bpre)%bitsamplesize
SB	T9,0000 (T7)
BNEZ	T1,7F0CB954
NOP	
BREAK	
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CB96C
LUI	AT,8000
BNE	T8,AT,7F0CB96C
NOP	
BREAK	
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001

7F0CB974	img3
ADDU	T8,T3,T4	;T8=pre+bcur
SUBU	T6,T8,T5	;T6=pre+bcur-bpre
ADDU	T9,T6,A0	;T9=pre + bcur - bpre + cur+bitsamplesize*2
DIV	T9,T1
ADDU	T8,S0,A3
ADDU	T6,T8,V1	;T6=&sample[fieldcount*width + sampcount]
MFHI	T7		;(pre + bcur - bpre + cur+bitsamplesize*2)%bitsamplesize
SB	T7,0000 (T6)
BNEZ	T1,7F0CB9A0
NOP	
BREAK	
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CB9B8
LUI	AT,8000
BNE	T9,AT,7F0CB9B8
NOP	
BREAK	
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001

7F0CB9C0	img4
SUBU	T9,T4,T5	;T9=bcur-bpre
BGEZ	T9,7F0CB9D4
SRA	T8,T9,0x1	;T8=(bcur-bpre)/2
ADDIU	AT,T9,0001
SRA	T8,AT,0x1	;T8=(bcur-bpre+1)/2
//7F0CB9D4:
ADDU	T7,T8,T3	;T7=T8+pre
ADDU	T6,T7,A0	;T6=T8+pre+cur+bitsamplesize*2
DIV	T6,T1
ADDU	T8,S0,A3
ADDU	T7,T8,V1	;T7=&sample[fieldcount*width + sampcount]
MFHI	T9		;T9=(T8+pre+cur+bitsamplesize*2)%bitcount
SB	T9,0000 (T7)
BNEZ	T1,7F0CB9FC
NOP	
BREAK	
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CBA14
LUI	AT,8000
BNE	T6,AT,7F0CBA14
NOP	
BREAK	
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001

7F0CBA1C	img5
SUBU	T6,T3,T5	;T6= pre - bpre
BGEZ	T6,7F0CBA30	;skip if positive
SRA	T8,T6,0x1	;T8= (pre - bpre) / 2
ADDIU	AT,T6,0001	;AT= (prevsamp - prevback)+1
SRA	T8,AT,0x1	;T8= (prevsamp - prevback+1) /2
//7F0CBA30:	positive values = difference/2
ADDU	T9,T8,T4	;T9=(difference/2) + backtrack sample
ADDU	T7,T9,A0	;T7=(difference/2) + backtrack sample + sample + samplesize*2
DIV	T7,T1		;T1=samplesize
ADDU	T8,S0,A3	;T8=p->sample + fieldcount * sizeval1
ADDU	T9,T8,V1	;T9=&sample[fieldcount * sizeval1+sampcount]
MFHI	T6		;T6=((difference/2) + backtrack sample + sample + samplesize*2) % samplesize
SB	T6,0000 (T9)	;store value -> sample[fieldcount * sizeval1+sampcount]
BNEZ	T1,7F0CBA58	;break if division by zero occured
NOP	
BREAK	
//7F0CBA58:	test posthumously for division by -1
ADDIU	AT,R0,FFFF	;AT=-1
BNE	T1,AT,7F0CBA70	;break if either value = -1
LUI	AT,8000
BNE	T7,AT,7F0CBA70
NOP	
BREAK	
//7F0CBA70:	go forth patsy!
BEQ	R0,R0,7F0CBACC
ADDIU	V1,V1,0001	;V1++	sampcount++

7F0CBA78:	img6
ADDU	T7,T3,T4	;T7=pre+bcur
BGEZ	T7,7F0CBA8C	;if pre+bcur<0, add 1
SRA	T8,T7,0x1
ADDIU	AT,T7,0001
SRA	T8,AT,0x1	;T8=val/2
//7F0CBA8C:
ADDU	T6,T8,A0	;T6=cur + bitsamplesize*2 + val/2
DIV	T6,T1
MFHI	T9
ADDU	T7,S0,A3
ADDU	T8,T7,V1
SB	T9,0000 (T8)
BNEZ	T1,7F0CBAB0
NOP	
BREAK	
ADDIU	AT,R0,FFFF
BNE	T1,AT,7F0CBAC8
LUI	AT,8000
BNE	T6,AT,7F0CBAC8
NOP	
BREAK	
ADDIU	V1,V1,0001
//7F0CBACC:	loop for samples in segment
BNE	V1,A1,7F0CB824	;loop while there are more samples in segment
ADDIU	T0,T0,0001	;T0++	samples[cur+1]
//7F0CBAD4:	loop for fieldsize
ADDIU	V0,V0,0001	;V0++	fieldcount++
BNE	V0,A2,7F0CB804	;loop if there are more elements in the field
NOP	
//7F0CBAE0:	!quit!
LW	S0,0004 (SP)
LW	S1,0008 (SP)
LW	S2,000C (SP)
JR	RA
ADDIU	SP,SP,0010
