// File: /workspace/github_cpp_makefile_star2_O1/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 0x517d
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x10, %rsp
movq %rdi, %rbx
movq 0x38(%rdi), %rax
movss (%rax), %xmm3
movss 0xd4a2(%rip), %xmm0     # 0x12518
ucomiss %xmm3, %xmm0
movl $0x13b, %ecx            # imm = 0x13B
movl $0x1e0, %edx            # imm = 0x1E0
cmoval %ecx, %edx
movss 0xd48e(%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 0x5171
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
xorps %xmm1, %xmm1
cvtss2sd 0x180(%rbx), %xmm1
movsd 0xd07d(%rip), %xmm2     # 0x12158
mulsd %xmm0, %xmm2
ucomisd %xmm1, %xmm2
ja 0x5171
xorps %xmm1, %xmm1
cvtss2sd 0x188(%rbx), %xmm1
movsd 0xd064(%rip), %xmm2     # 0x12160
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
ja 0x5171
leal (,%r14,8), %ecx
leal (%rcx,%rcx,4), %ecx
xorps %xmm2, %xmm2
cvtsi2ss %ecx, %xmm2
movss 0x140(%rbx), %xmm1
movss %xmm2, -0x24(%rbp)
ucomiss %xmm2, %xmm1
jb 0x5171
xorps %xmm2, %xmm2
cvtss2sd 0x9c(%rbx), %xmm2
movsd 0xd02b(%rip), %xmm1     # 0x12168
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
ja 0x5171
xorps %xmm2, %xmm2
cvtss2sd 0x1e0(%rbx), %xmm2
ucomisd %xmm1, %xmm2
ja 0x5171
xorps %xmm2, %xmm2
cvtss2sd %xmm3, %xmm2
movsd 0xcf69(%rip), %xmm1     # 0x120d0
mulsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x517e
addq $0x10, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movsd 0xcfea(%rip), %xmm2     # 0x12170
mulsd %xmm0, %xmm2
cvtsd2ss %xmm2, %xmm2
movss %xmm2, 0xe0(%rbx)
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0xf4(%rbx)
movss %xmm1, 0xe4(%rbx)
movsd 0xcfc6(%rip), %xmm1     # 0x12178
mulsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss %xmm1, 0xec(%rbx)
movss %xmm1, 0xf8(%rbx)
mulsd 0xcfae(%rip), %xmm0     # 0x12180
cvtsd2ss %xmm0, %xmm0
movss %xmm1, 0xe8(%rbx)
movss %xmm0, 0xf0(%rbx)
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, -0x28(%rbp)
movq %rbx, %rdi
callq 0x10840
movss 0xd2fb(%rip), %xmm0     # 0x1251c
movss -0x28(%rbp), %xmm3
testl %eax, %eax
je 0x5171
movq 0x40(%rbx), %r15
cmpq %r15, 0xc8(%rbx)
jb 0x5211
cmpl $0x12, 0xc0(%rbx)
jne 0x5171
cmpl $0x1e0, %r14d           # imm = 0x1E0
jne 0x52b5
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movl $0x1, %r14d
movq %rbx, %rdi
movl %r14d, %esi
movl $0x1, %edx
callq 0xfe7e
addl %eax, %r12d
incl %r14d
cmpl $0xe, %r14d
jne 0x525d
movzbl 0xa1(%rbx), %eax
shrl $0x6, %eax
movzbl 0xa2(%rbx), %ecx
andl $0x3, %ecx
leal (%rax,%rcx,4), %eax
cmpl %eax, %r12d
jne 0x5171
cmpl $0x3, 0x54(%rbx)
jge 0x53af
movl $0x3, 0x54(%rbx)
movl 0x48(%rbx), %eax
movl %eax, 0x50(%rbx)
jmp 0x53bd
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 0x52c4
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 0xd9e2(%rip), %rcx      # 0x12d77
leaq 0xd9e2(%rip), %rdx      # 0x12d7e
cmovaq %rcx, %rdx
movl (%rdx), %ecx
movl 0x3(%rdx), %edx
movl %edx, 0x3(%rax)
movl %ecx, (%rax)
jmp 0x565b
jne 0x53bd
movl 0x48(%rbx), %eax
cmpl 0x50(%rbx), %eax
jg 0x52a3
movzbl 0xa0(%rbx), %r12d
movl %r12d, %r14d
shrl $0x6, %r14d
movq 0x10(%rbx), %rax
leaq 0x4(%r15), %rdx
movss -0x24(%rbp), %xmm1
movq %rdx, %rcx
cmpq %rax, %rdx
jae 0x53ee
movss (%rcx), %xmm0
leaq 0x4(%rcx), %rdx
ucomiss %xmm1, %xmm0
jbe 0x53d9
leaq 0x8(%r15), %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 0x5427
movq %rbx, %rdi
callq 0x10840
testl %eax, %eax
jne 0x540e
movq %r15, 0x40(%rbx)
testb %r12b, %r12b
jns 0x54ef
cmpl $0x24, 0xc0(%rbx)
jne 0x54ef
movzbl 0xa3(%rbx), %edx
movl %edx, %eax
andl $0x3, %eax
leal -0x1(%rax), %ecx
cmpl $0x1, %ecx
ja 0x54ef
movzbl 0xa0(%rbx), %ecx
movl %ecx, %edi
shrl %edi
andl $0x1f, %edi
movzbl 0xa2(%rbx), %esi
movl %esi, %r8d
shrl $0x3, %r8d
cmpl %r8d, %edi
jne 0x54ef
movzbl 0xa1(%rbx), %edi
movl %edi, %r8d
andl $0x3f, %r8d
shrl $0x2, %edx
cmpl %edx, %r8d
jne 0x54ef
shrl $0x6, %edi
andl $0x3, %esi
leal (%rdi,%rsi,4), %edx
movzbl 0xa4(%rbx), %esi
andl $0xf, %esi
subl %esi, %edx
notl %ecx
testb $-0x40, %cl
sete %cl
cmpl $0x3, %eax
setne %al
andb %cl, %al
movzbl %al, %eax
cmpl %eax, %edx
jne 0x54ef
cmpl $0x0, 0x10e35(%rip)     # 0x162f8
jne 0x54cc
movl %r14d, 0x10e2c(%rip)    # 0x162f8
movl 0x10e21(%rip), %r14d    # 0x162f4
testl %r14d, %r14d
jne 0x54e0
movq %rbx, %rdi
callq 0x26ec
incl %r14d
movl %r14d, 0x10e0a(%rip)    # 0x162f4
jmp 0x5171
movzbl 0xa0(%rbx), %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 0x10d89(%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 0x10d5b(%rip), %esi     # 0x162f8
movl %esi, %ecx
xorl $0x3, %ecx
movl %eax, %r8d
xorl $0x1, %r8d
movl %r8d, %edx
orl %ecx, %edx
jne 0x55bc
leaq 0xdb57(%rip), %rsi      # 0x1310e
jmp 0x5656
movl %esi, %edx
xorl $0x2, %edx
movl %r8d, %r9d
orl %edx, %r9d
jne 0x55e3
movabsq $0x100000005, %rax      # imm = 0x100000005
movq %rax, 0x88(%rbx)
leaq 0xd74f(%rip), %rsi      # 0x12d30
jmp 0x5656
xorl $0x1, %esi
orl %esi, %r8d
jne 0x5605
movabsq $0x600000000, %rax      # imm = 0x600000000
movq %rax, 0x88(%rbx)
leaq 0xd73c(%rip), %rsi      # 0x12d3f
jmp 0x5656
xorl $0x2, %eax
orl %eax, %ecx
jne 0x5620
movq $0x4, 0x88(%rbx)
leaq 0xd7c7(%rip), %rsi      # 0x12de5
jmp 0x5656
orl %edx, %eax
jne 0x563e
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0x88(%rbx)
leaq 0xd712(%rip), %rsi      # 0x12d4e
jmp 0x5656
movabsq $0x500000000, %rax      # imm = 0x500000000
movq %rax, 0x88(%rbx)
leaq 0xd710(%rip), %rsi      # 0x12d66
callq 0x2100
xorl %eax, %eax
movl %eax, 0x10c91(%rip)     # 0x162f4
movl %eax, 0x10c8f(%rip)     # 0x162f8
jmp 0x5171