include "m8c.inc"
include "psocapi.inc"

export	_DPCM_INIT
export	_DPCMDecoder1
export	_DPCMDecoder2
export	DPCMdata
export	DPCMdata2
export	scale
export	scale2
export	code
export	code2
export	dlevel
export	dlevel2

area		bss(RAM)
code:		blk 1
code2:		blk 1
dlevel:		blk 2
dlevel2:	blk 2
scale:		blk 1
scale2:		blk 1
DPCMdata:	blk 2
DPCMdata2:	blk 2
area	text(ROM,REL)
_DPCM_INIT:
		mov		[scale],0
		mov		[scale2],0
		mov		[DPCMdata],0x40
		mov		[DPCMdata+1],0
		mov		[DPCMdata2],0x40
		mov		[DPCMdata2+1],0
		ret

_DPCMDecoder1:
		mov		[code],a
		and		a,0x7f
		mov		reg[232],a
		mov		reg[233],a
		
		
		mov		a,reg[234]
		mov		[dlevel],a
		mov		a,reg[235]
		mov		[dlevel+1],a
		
		
		tst		[code],0x80
		jz		_DPCM_LP1
		
		mov		a,[dlevel+1]
		sub		[DPCMdata+1],a
		mov		a,[dlevel]
		sbb		[DPCMdata],a
		
		tst		[DPCMdata],128
		jz		_DPCM_LP2
		mov		[DPCMdata],0
		mov		[DPCMdata+1],0
		jmp		_DPCM_LP2
		
_DPCM_LP1:
		mov		a,[dlevel+1]
		add		[DPCMdata+1],a
		mov		a,[dlevel]
		adc		[DPCMdata],a
		
;		mov		a,[DPCMdata]
;		xor		a,0x80
;		sbb		a,0x83				;sbb	a,129
;		jc		_DPCM_LP2
		
		cmp		[DPCMdata],0x80
		jc		_DPCM_LP2
		mov		[DPCMdata],0x7f		;mov	[DPCMdata],0
		mov		[DPCMdata+1],255
		
_DPCM_LP2:
;		mov		x,[DPCMdata]
;		mov		a,[DPCMdata+1]
		
		mov		a,[DPCMdata+1]	
		mov		reg[PRS8_SEED_REG],a
		
;		mov		a,[DPCMdata]
;		lcall	PWM8_A_WritePulseWidth
;		mov		a,[DPCMdata]
;		lcall	PWM8_B_WritePulseWidth
		mov		a,[DPCMdata]
		mov		reg[PWM8_A_COMPARE_REG],a
		mov		a,[DPCMdata]
		mov		reg[PWM8_B_COMPARE_REG],a		
		
		
		
		ret
__end_of_DPCMDecoder1:




_DPCMDecoder2:
		mov		[code2],a
		and		a,0x7f
		mov		reg[232],a
		mov		reg[233],a
		
		
		mov		a,reg[234]
		mov		[dlevel2],a
		mov		a,reg[235]
		mov		[dlevel2+1],a
		
		

		
		tst		[code2],0x80
		jz		_DPCM_2_LP1
		
		mov		a,[dlevel2+1]
		sub		[DPCMdata2+1],a
		mov		a,[dlevel2]
		sbb		[DPCMdata2],a
		
		tst		[DPCMdata2],128
		jz		_DPCM_2_LP2
		mov		[DPCMdata2],0
		mov		[DPCMdata2+1],0
		jmp		_DPCM_2_LP2
		
_DPCM_2_LP1:
		mov		a,[dlevel2+1]
		add		[DPCMdata2+1],a
		mov		a,[dlevel2]
		adc		[DPCMdata2],a
		
;		mov		a,[DPCMdata]
;		xor		a,0x80
;		sbb		a,0x83				;sbb	a,129
;		jc		_DPCM_2_LP2
		
		cmp		[DPCMdata2],0x80
		jc		_DPCM_2_LP2
		mov		[DPCMdata2],0x7f
		mov		[DPCMdata2+1],0xff
		
_DPCM_2_LP2:
;		mov		x,[DPCMdata2]
;		mov		a,[DPCMdata2+1]

		mov		a,[DPCMdata2+1]	
		mov		reg[PRS8_B_SEED_REG],a		
		
;		mov		a,[DPCMdata2]
;		lcall	PWM8_C_WritePulseWidth
;		mov		a,[DPCMdata2]
;		lcall	PWM8_D_WritePulseWidth

		mov		a,[DPCMdata2]
		mov		reg[PWM8_C_COMPARE_REG],a
		mov		a,[DPCMdata2]
		mov		reg[PWM8_D_COMPARE_REG],a		
		
		ret
__end_of_DPCMDecoder2:
