// File: /workspace/github_cpp_makefile_star2_O3/probonopd[P]decodeir/DecodeIR.cpp
// Function: Signal::tryTDC()
void Signal::tryTDC()	// GD 2009  Added tryTDC

// TDC-38		{38k,315,msb}<-1,1|1,-1>(1:1,D:5,S:5,F:7,-89m)+
// TDC-56		{56.3k,213,msb}<-1,1|1,-1>(1:1,D:5,S:5,F:7,-89m)+
// V2.39 has revised TDC to allow 7-bit OBC as an example has been found of this.
// In 2.38, F:7 was F:6,~F:1

// OrtekMCE		{38.6k,480}<1,-1|-1,1>(4,-1,D:5,P:2,F:6,C:4,-48m)+
// P is position code:	0 for first frame of repeat sequence
//						1 for all repeats up to last one
//						2 for last repeat
// C is checksum, 3 more than the number of 1 bits in D, P, F together

{
	if ( nFrameL < 8 ) return;

	int HB = *pFrame < 260 ? 213 : *pFrame < 400 ? 315 : 480;
	static int FC = 0;		// count of frames bypassed
	int P = 0;				// position count
	static int FP = 0;	// first P value

	if ( nTotDur < 14.*HB )
		return;
	if ( sortOn.min1 < HB * .3 )
		return;
	if ( sortOn.max2 > HB * 2.4 )
		return;
	if (   !framed(40*HB)
		|| nMaxDur > HB*4.5
        || sortBurst.max2 > HB*4.5  // nominal 2,-2
		|| *pFrame < HB*.5 )
	{
		return;
	}

    minGlitch = HB*.25;
	nMinShort = nMinShort2 = HB*.5;				// Nominally 1
	nMaxShort = nMaxShort2 = nMinLong = HB*1.5;	// 1 vs. 2
	nMaxLong = HB*2.5;							// Nominally 2

	cleanup();
	nBit = 1;
	nState = 1;
	++pBit;

	do
	{	
		if ( ! phaseBit() ) return;
	}	while (pBit < pFrameEnd);

	if ( nBit != 18 ) return;		// wrong number of bits

	if ( HB == 480 )
	{	int chksum = -1;	// checksum is checked with equivalent formula on uncomplemented data
		for ( int ndx = 1; ndx < 14; ndx++ )
		{
			chksum += getLsb(ndx,1);
		}

		if ( chksum != getLsb(14,4) ) return;		// wrong checksum

		setPreempt(prByOrtekMCE);					// avoid RC5, RC6 or Q2 decode of same frame
		P = getLsb(6,2);

		// look to see if there is a following frame, usually with different position code P
		float* pFrameEnd1 = pFrameEnd;					// save for later restoration
		float* pFrameEnd2 = pFrameEnd + 1;
		for ( ; pFrameEnd2 < pMainLimit; pFrameEnd2++ )
		{
			if ( *pFrameEnd2 > 40 * HB ) break;
		};
		pBit = pFrameEnd + 2;
		pFrameEnd = pFrameEnd2;							// temp change for use by phaseBit{}
		nBit++;
		nState = 1;

		while ( pBit < pFrameEnd )
		{	
			if ( ! phaseBit() ) break;
		}	
		pFrameEnd = pFrameEnd1;							// restore original value

		if ( nBit == 36 )
		{
			if (	( ( P ==3 || P == 2 ) &&  
						( getLsb(24,2) == 2 || getLsb(24,2) == 1 ) )	// check valid sequence of position count
				&&	getLsb(1,5) == getLsb(19,5)
				&&	getLsb(8,6) == getLsb(26,6)
				&&	getLsb(14,4) - getLsb(32,4) == (getLsb(6,2) == 3 && getLsb(24,2) < 3) )
			{											
				if ( FP == 0 ) FP = P;					// next frame is valid repeat
				if ( FC == 0 ) setzContext();			// save the context value
				FC++;									// increment count of frames skipped
				return;									// wait for repeat to come round
			}
		}

		P = getLsb(6,2);
		*pDevice = 31 - getLsb(1,5);
		*pOBC = 63 - getLsb(8,6);
		pHex[0] = 0xFC - msb(*pOBC);	// need to encode as comp
		nFrameCount = FC;
		strcpy( pProtocol, "OrtekMCE" );
		nNote_out = 6;
//	changed next line to call "strcpy" (was "sprintf"), per instructions
//	by mathdon, for smoother compile in Unix environments--alex750		
		strcpy( pMisc, FP == 3 && P == 1 ? "" :
			FP == 2 && P == 1 ? (nNote_out = 5, nAuxNote_out = 1, "no start frame") : 
			FP == 1 && P == 1 ? (nNote_out = 0, nAuxNote_out = 6, "only end frame") : 		
			FP == 3 && P == 2 ? (nNote_out = 4, nAuxNote_out = 0, "no end frame") : 
			FP == 2 && P == 2 ? (nNote_out = 1, nAuxNote_out = 2, "no start and end frames") :
			(nNote_out = 0, nAuxNote_out = 5, "only start frame") );
	}
	else
	{
		makeMsb();

//		if ( getMsb(16,1) == getMsb(17,1) ) return;	// GD 2.39 protocol revised to allow 7-bit OBC
		*pDevice = getMsb(1,5);
		*pSubDevice = getMsb(6,5);
		*pOBC = getMsb(11,7);						// GD 2.39 In 2.38 was getMsb(11,6)
		pHex[0] = getMsb(11,7) << 1;
		strcpy( pProtocol, (HB == 213) ? "TDC-56" : "TDC-38"  );
	}
	FC = 0;		// reset static variables
	FP = 0;
}

// Assembly:
cmpl $0x8, 0x48(%rdi)
jl 0x525a
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movss (%rax), %xmm3
movss 0xd3c7(%rip), %xmm0     # 0x12518
ucomiss %xmm3, %xmm0
movl $0x13b, %ecx            # imm = 0x13B
movl $0x1e0, %edx            # imm = 0x1E0
cmoval %ecx, %edx
movss 0xd3b3(%rip), %xmm0     # 0x1251c
ucomiss %xmm3, %xmm0
movl $0xd5, %r14d
cmovbel %edx, %r14d
xorps %xmm0, %xmm0
cvtss2sd 0x98(%rdi), %xmm0
movl %r14d, %edx
shll $0x4, %edx
subl %r14d, %edx
subl %r14d, %edx
cvtsi2sd %edx, %xmm1
ucomisd %xmm0, %xmm1
ja 0x524c
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x180(%rbx), %xmm1
movsd 0xcfa2(%rip), %xmm2     # 0x12158
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
ja 0x524c
xorps %xmm1, %xmm1
cvtss2sd 0x188(%rbx), %xmm1
movsd 0xcf89(%rip), %xmm2     # 0x12160
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x524c
leal (,%r14,8), %ecx
leal (%rcx,%rcx,4), %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
movss 0x140(%rbx), %xmm1
movss %xmm2, -0x2c(%rbp)
ucomiss %xmm2, %xmm1
jb 0x524c
xorps %xmm2, %xmm2
cvtss2sd 0x9c(%rbx), %xmm2
movsd 0xcf50(%rip), %xmm1     # 0x12168
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
ja 0x524c
xorps %xmm2, %xmm2
cvtss2sd 0x1e0(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x524c
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
movsd 0xce8e(%rip), %xmm1     # 0x120d0
mulsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x525b
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0xcf0d(%rip), %xmm2     # 0x12170
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm2, 0xe0(%rbx)
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0xf4(%rbx)
movsd 0xcef1(%rip), %xmm2     # 0x12178
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm1, 0xe4(%rbx)
movss %xmm2, 0xec(%rbx)
movss %xmm2, 0xf8(%rbx)
movss %xmm2, 0xe8(%rbx)
mulsd 0xcec9(%rip), %xmm0     # 0x12180
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0xf0(%rbx)
leaq 0xa0(%rbx), %r15
xorps %xmm0, %xmm0
movups %xmm0, 0xa0(%rbx)
movabsq $0x100000001, %rcx      # imm = 0x100000001
movq %rcx, 0xc0(%rbx)
addq $0x4, %rax
movq %rax, 0xc8(%rbx)
movss %xmm3, -0x30(%rbp)
movq %rbx, %rdi
callq 0x106cc
movss 0xd217(%rip), %xmm0     # 0x1251c
movss -0x30(%rbp), %xmm3
testl %eax, %eax
je 0x524c
movq 0x40(%rbx), %r12
cmpq %r12, 0xc8(%rbx)
jb 0x52f5
cmpl $0x12, 0xc0(%rbx)
jne 0x524c
cmpl $0x1e0, %r14d           # imm = 0x1E0
jne 0x539b
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movl $0x1, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0xfd2a
addl %eax, %r13d
incl %r14d
cmpl $0xe, %r14d
jne 0x5341
movzbl 0xa1(%rbx), %eax
shrl $0x6, %eax
movzbl 0xa2(%rbx), %ecx
andl $0x3, %ecx
leal (%rax,%rcx,4), %eax
cmpl %eax, %r13d
jne 0x524c
movl 0x54(%rbx), %eax
cmpl $0x2, %eax
jg 0x5495
movl 0x48(%rbx), %eax
movl $0x3, 0x54(%rbx)
movl %eax, 0x50(%rbx)
jmp 0x54a6
movl $0xa2, %ecx
movl $0x33000000, %eax       # imm = 0x33000000
movl $0x55000000, %edx       # imm = 0x55000000
movzbl (%rbx,%rcx), %esi
bswapl %esi
movl %esi, %edi
shll $0x4, %edi
shrl $0x4, %esi
andl $0xf000000, %esi        # imm = 0xF000000
orl %edi, %esi
movl %esi, %edi
andl %eax, %edi
shrl $0x2, %esi
andl %eax, %esi
leal (%rsi,%rdi,4), %esi
movl %esi, %edi
andl %edx, %edi
shrl %esi
andl %edx, %esi
leal (%rsi,%rdi,2), %esi
shrl $0x18, %esi
movb %sil, (%rbx,%rcx)
leaq -0x1(%rcx), %rsi
addq $-0x9f, %rcx
cmpq $0x1, %rcx
movq %rsi, %rcx
ja 0x53aa
movl 0xa0(%rbx), %eax
shrl $0x2, %eax
andl $0x1f, %eax
movq 0x160(%rbx), %rcx
movl %eax, (%rcx)
movl 0xa0(%rbx), %eax
andl $0x3, %eax
movzbl 0xa1(%rbx), %ecx
shrl $0x5, %ecx
leal (%rcx,%rax,8), %eax
movq 0x168(%rbx), %rcx
movl %eax, (%rcx)
movzbl 0xa1(%rbx), %eax
andl $0x1f, %eax
movzbl 0xa2(%rbx), %ecx
shrl $0x6, %ecx
leal (%rcx,%rax,4), %eax
movq 0x170(%rbx), %rcx
movl %eax, (%rcx)
movb 0xa1(%rbx), %al
movzbl 0xa2(%rbx), %ecx
shlb $0x3, %al
shrl $0x5, %ecx
andl $0x6, %ecx
movzbl %al, %eax
orl %ecx, %eax
ucomiss %xmm3, %xmm0
movq 0x178(%rbx), %rcx
movl %eax, (%rcx)
movq 0x148(%rbx), %rax
leaq 0xd8f3(%rip), %rcx      # 0x12d6e
leaq 0xd8f3(%rip), %rdx      # 0x12d75
cmovaq %rcx, %rdx
movl (%rdx), %ecx
movl 0x3(%rdx), %edx
movl %edx, 0x3(%rax)
movl %ecx, (%rax)
jmp 0x573e
cmpl $0x3, %eax
jne 0x54a6
movl 0x48(%rbx), %eax
cmpl 0x50(%rbx), %eax
jg 0x538c
movzbl 0xa0(%rbx), %r13d
movl %r13d, %r14d
shrl $0x6, %r14d
movq 0x10(%rbx), %rax
leaq 0x4(%r12), %rdx
movss -0x2c(%rbp), %xmm1
movq %rdx, %rcx
cmpq %rax, %rdx
jae 0x54d8
movss (%rcx), %xmm0
leaq 0x4(%rcx), %rdx
ucomiss %xmm1, %xmm0
jbe 0x54c3
leaq 0x8(%r12), %rax
movq %rax, 0xc8(%rbx)
movq %rcx, 0x40(%rbx)
movabsq $0x100000013, %rax      # imm = 0x100000013
movq %rax, 0xc0(%rbx)
movq 0xc8(%rbx), %rax
cmpq 0x40(%rbx), %rax
jae 0x5512
movq %rbx, %rdi
callq 0x106cc
testl %eax, %eax
jne 0x54f9
movq %r12, 0x40(%rbx)
testb %r13b, %r13b
jns 0x55d3
cmpl $0x24, 0xc0(%rbx)
jne 0x55d3
movzbl 0xa3(%rbx), %edx
movl %edx, %ecx
andl $0x3, %ecx
decl %ecx
movb 0xa0(%rbx), %al
cmpl $0x1, %ecx
ja 0x55d6
movzbl %al, %ecx
movl %ecx, %edi
shrl %edi
andl $0x1f, %edi
movzbl 0xa2(%rbx), %esi
movl %esi, %r8d
shrl $0x3, %r8d
cmpl %r8d, %edi
jne 0x55d6
movzbl 0xa1(%rbx), %edi
movl %edi, %r8d
andl $0x3f, %r8d
shrl $0x2, %edx
cmpl %edx, %r8d
jne 0x55d6
shrl $0x6, %edi
andl $0x3, %esi
leal (%rdi,%rsi,4), %edx
movzbl 0xa4(%rbx), %esi
andl $0xf, %esi
subl %esi, %edx
notl %ecx
xorl %esi, %esi
testb $-0x40, %cl
sete %sil
cmpl %esi, %edx
jne 0x55d6
cmpl $0x0, 0x10d51(%rip)     # 0x162f8
jne 0x55b0
movl %r14d, 0x10d48(%rip)    # 0x162f8
movl 0x10d3d(%rip), %r14d    # 0x162f4
testl %r14d, %r14d
jne 0x55c4
movq %rbx, %rdi
callq 0x26e8
incl %r14d
movl %r14d, 0x10d26(%rip)    # 0x162f4
jmp 0x524c
movb (%r15), %al
movzbl %al, %ecx
movl %ecx, %eax
shrl $0x6, %eax
notl %ecx
shrl %ecx
andl $0x1f, %ecx
movq 0x160(%rbx), %rdx
movl %ecx, (%rdx)
movzbl 0xa1(%rbx), %ecx
notl %ecx
andl $0x3f, %ecx
movq 0x170(%rbx), %rdx
movl %ecx, (%rdx)
bswapl %ecx
movl %ecx, %edx
andl $0xc000000, %edx        # imm = 0xC000000
movl %ecx, %esi
shll $0x4, %esi
shrl $0x4, %ecx
andl $0x3000000, %ecx        # imm = 0x3000000
orl %esi, %ecx
shll $0x2, %ecx
leal (%rcx,%rdx,4), %ecx
movl %ecx, %edx
andl $0x54000000, %edx       # imm = 0x54000000
shrl %ecx
andl $0x55555540, %ecx       # imm = 0x55555540
leal (%rcx,%rdx,2), %ecx
shrl $0x18, %ecx
xorl $0xfc, %ecx
movq 0x178(%rbx), %rdx
movl %ecx, (%rdx)
movl 0x10ca6(%rip), %ecx     # 0x162f4
movl %ecx, 0x60(%rbx)
movq 0x148(%rbx), %rcx
movabsq $0x45434d6b6574724f, %rdx # imm = 0x45434D6B6574724F
movq %rdx, (%rcx)
movb $0x0, 0x8(%rcx)
movl $0x6, 0x88(%rbx)
movq 0x150(%rbx), %rdi
movl 0x10c78(%rip), %esi     # 0x162f8
movl %esi, %ecx
xorl $0x3, %ecx
movl %eax, %r8d
xorl $0x1, %r8d
movl %r8d, %edx
orl %ecx, %edx
jne 0x569f
leaq 0xda6b(%rip), %rsi      # 0x13105
jmp 0x5739
movl %esi, %edx
xorl $0x2, %edx
movl %r8d, %r9d
orl %edx, %r9d
jne 0x56c6
movabsq $0x100000005, %rax      # imm = 0x100000005
movq %rax, 0x88(%rbx)
leaq 0xd663(%rip), %rsi      # 0x12d27
jmp 0x5739
xorl $0x1, %esi
orl %esi, %r8d
jne 0x56e8
movabsq $0x600000000, %rax      # imm = 0x600000000
movq %rax, 0x88(%rbx)
leaq 0xd650(%rip), %rsi      # 0x12d36
jmp 0x5739
xorl $0x2, %eax
orl %eax, %ecx
jne 0x5703
movq $0x4, 0x88(%rbx)
leaq 0xd6db(%rip), %rsi      # 0x12ddc
jmp 0x5739
orl %edx, %eax
jne 0x5721
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x88(%rbx)
leaq 0xd626(%rip), %rsi      # 0x12d45
jmp 0x5739
movabsq $0x500000000, %rax      # imm = 0x500000000
movq %rax, 0x88(%rbx)
leaq 0xd624(%rip), %rsi      # 0x12d5d
callq 0x2100
xorl %eax, %eax
movl %eax, 0x10bae(%rip)     # 0x162f4
movl %eax, 0x10bac(%rip)     # 0x162f8
jmp 0x524c
nop