// File: /workspace/github_cpp_makefile_star2_O2/rpavlik[P]edid-decode/parse-cta-block.cpp
// Function: edid_state::parse_cta_block(unsigned char const*)
void edid_state::parse_cta_block(const unsigned char *x)
{
	unsigned version = x[1];
	unsigned offset = x[2];
	const unsigned char *detailed;

	// See Table 52 of CTA-861-G for a description of Byte 3

	printf("  Revision: %u\n", version);
	if (version == 0)
		fail("Invalid CTA-861 Extension revision 0.\n");
	if (version == 2)
		fail("Deprecated CTA-861 Extension revision 2.\n");
	if (cta.has_hdmi && version != 3)
		fail("The HDMI Specification requires CTA Extension revision 3.\n");
	if (version > 3)
		warn("Unknown CTA-861 Extension revision %u.\n", version);

	if (version >= 1) do {
		if (version == 1 && x[3] != 0)
			fail("Non-zero byte 3.\n");

		if (offset < 4)
			break;

		if (version < 3 && ((offset - 4) / 8)) {
			printf("  8-byte timing descriptors: %u\n", (offset - 4) / 8);
			fail("8-byte descriptors were never used.\n");
		}

		if (version >= 2) {
			if (x[3] & 0x80)
				printf("  Underscans IT Video Formats by default\n");
			else
				warn("IT Video Formats are overscanned by default, but normally this should be underscanned.\n");
			if (x[3] & 0x40)
				printf("  Basic audio support\n");
			if (x[3] & 0x20)
				printf("  Supports YCbCr 4:4:4\n");
			if (x[3] & 0x10)
				printf("  Supports YCbCr 4:2:2\n");
			// Disable this test: this fails a lot of EDIDs, and there are
			// also some corner cases where you only want to receive 4:4:4
			// and refuse a fallback to 4:2:2.
//			if ((x[3] & 0x30) && (x[3] & 0x30) != 0x30)
//				msg(!cta.has_hdmi, "If YCbCr support is indicated, then both 4:2:2 and 4:4:4 %s be supported.\n",
//				    cta.has_hdmi ? "shall" : "should");
			printf("  Native detailed modes: %u\n", x[3] & 0x0f);
			if (cta.first_block)
				cta.byte3 = x[3];
			else if (x[3] != cta.byte3)
				fail("Byte 3 must be the same for all CTA-861 Extension Blocks.\n");
			if (cta.first_block) {
				unsigned native_dtds = x[3] & 0x0f;

				cta.native_timings.clear();
				if (!native_dtds && !cta.has_vfpdb) {
					cta.first_svd_might_be_preferred = true;
				} else if (native_dtds > cta.preparsed_total_dtds) {
					fail("There are more Native DTDs (%u) than DTDs (%u).\n",
					     native_dtds, cta.preparsed_total_dtds);
				}
				if (native_dtds > cta.preparsed_total_dtds)
					native_dtds = cta.preparsed_total_dtds;
				for (unsigned i = 0; i < native_dtds; i++) {
					char type[16];

					sprintf(type, "DTD %3u", i + 1);
					cta.native_timings.push_back(timings_ext(i + 129, type));
				}
				if (cta.has_hdmi && block_nr != (block_map.saw_block_1 ? 2 : 1))
					fail("The HDMI Specification requires that the first Extension Block (that is not a Block Map) is an CTA-861 Extension Block.\n");
			}
		}
		if (version >= 3) {
			unsigned i;

			for (i = 4; i < offset; i += (x[i] & 0x1f) + 1) {
				unsigned tag = (x[i] & 0xe0) << 3;

				if (tag == 0x700)
					tag |= x[i + 1];
				bool duplicate = cta.found_tags.find(tag) != cta.found_tags.end();

				cta_block(x + i, duplicate);
				if (!duplicate)
					cta.found_tags.insert(tag);
			}

			data_block.clear();
			if (i != offset)
				fail("Offset is %u, but should be %u.\n", offset, i);
		}

		data_block = "Detailed Timing Descriptors";
		base.seen_non_detailed_descriptor = false;
		bool first = true;
		for (detailed = x + offset; detailed + 17 < x + 127; detailed += 18) {
			if (memchk(detailed, 18))
				break;
			if (first) {
				first = false;
				printf("  %s:\n", data_block.c_str());
			}
			detailed_block(detailed);
		}
		if (!memchk(detailed, x + 127 - detailed)) {
			data_block = "Padding";
			fail("CTA-861 padding contains non-zero bytes.\n");
		}
	} while (0);

	data_block.clear();
	if (base.has_serial_number && base.has_serial_string)
		warn("Display Product Serial Number is set, so the Serial Number in the Base EDID should be 0.\n");
	if (!cta.has_vic_1 && !base.has_640x480p60_est_timing)
		fail("Required 640x480p60 timings are missing in the established timings"
		     " and the SVD list (VIC 1).\n");
	if ((cta.supported_hdmi_vic_vsb_codes & cta.supported_hdmi_vic_codes) !=
	    cta.supported_hdmi_vic_codes)
		fail("HDMI VIC Codes must have their CTA-861 VIC equivalents in the VSB.\n");
	if (!cta.has_vcdb)
		fail("Missing VCDB, needed for Set Selectable RGB Quantization to avoid interop issues.\n");
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rdi, %rbx
movzbl 0x1(%rsi), %ebp
movq %rsi, (%rsp)
movzbl 0x2(%rsi), %eax
movq %rax, 0x10(%rsp)
leaq 0x13fe9(%rip), %rdi     # 0x253d9
movl %ebp, %esi
xorl %eax, %eax
callq 0x3030
testl %ebp, %ebp
je 0x1140b
cmpl $0x2, %ebp
jne 0x1141b
leaq 0x14007(%rip), %rsi     # 0x25410
jmp 0x11412
leaq 0x13fd7(%rip), %rsi     # 0x253e9
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpl $0x3, %ebp
je 0x11439
cmpb $0x0, 0x2c3(%rbx)
je 0x11439
leaq 0x1400a(%rip), %rsi     # 0x2543a
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpb $0x4, %bpl
jb 0x11452
leaq 0x1402f(%rip), %rsi     # 0x25475
pushq $0x1
popq %rdi
movl %ebp, %edx
xorl %eax, %eax
callq 0x362c
testl %ebp, %ebp
je 0x11803
cmpl $0x1, %ebp
jne 0x11479
movq (%rsp), %rax
cmpb $0x0, 0x3(%rax)
je 0x11479
leaq 0x1402d(%rip), %rsi     # 0x2549d
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpb $0x4, 0x10(%rsp)
jb 0x11803
cmpb $0x2, %bpl
movq %rbx, 0x18(%rsp)
ja 0x114c6
movq 0x10(%rsp), %rax
leal -0x4(%rax), %esi
cmpl $0x8, %esi
jb 0x114bd
shrl $0x3, %esi
leaq 0x14009(%rip), %rdi     # 0x254af
xorl %eax, %eax
callq 0x3030
leaq 0x1401c(%rip), %rsi     # 0x254d0
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpl $0x1, %ebp
je 0x11759
movq (%rsp), %rax
cmpb $0x0, 0x3(%rax)
js 0x114e3
leaq 0x1401e(%rip), %rsi     # 0x254f5
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x362c
jmp 0x114ef
leaq 0x170d3(%rip), %rdi     # 0x285bd
callq 0x33b0
movq (%rsp), %rax
movb 0x3(%rax), %al
testb $0x40, %al
je 0x1150d
leaq 0x170e5(%rip), %rdi     # 0x285e6
callq 0x33b0
movq (%rsp), %rax
movb 0x3(%rax), %al
testb $0x20, %al
je 0x11524
leaq 0x170e4(%rip), %rdi     # 0x285fc
callq 0x33b0
movq (%rsp), %rax
movb 0x3(%rax), %al
testb $0x10, %al
je 0x1153b
leaq 0x170e4(%rip), %rdi     # 0x28613
callq 0x33b0
movq (%rsp), %rax
movb 0x3(%rax), %al
andb $0xf, %al
movzbl %al, %esi
leaq 0x14006(%rip), %rdi     # 0x2554d
xorl %eax, %eax
callq 0x3030
movb 0x2d5(%rbx), %al
movq (%rsp), %rcx
movb 0x3(%rcx), %cl
cmpb $0x1, %al
jne 0x11567
movb %cl, 0x2c2(%rbx)
jmp 0x11585
cmpb 0x2c2(%rbx), %cl
je 0x11585
leaq 0x13ff4(%rip), %rsi     # 0x2556a
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
movb 0x2d5(%rbx), %al
testb $0x1, %al
je 0x11692
movq (%rsp), %rax
movzbl 0x3(%rax), %r12d
leaq 0x2a8(%rbx), %rdi
movq %rdi, 0x28(%rsp)
callq 0xbc14
andl $0xf, %r12d
movl %ebp, 0x24(%rsp)
jne 0x115ba
cmpb $0x0, 0x2c5(%rbx)
je 0x115da
movl 0x180(%rbx), %ecx
cmpl %r12d, %ecx
jae 0x115e1
leaq 0x13fd9(%rip), %rsi     # 0x255a5
xorl %edi, %edi
movl %r12d, %edx
xorl %eax, %eax
callq 0x362c
jmp 0x115e1
movb $0x1, 0x2c1(%rbx)
movl 0x180(%rbx), %eax
cmpl %r12d, %eax
cmovbl %eax, %r12d
leaq 0x30(%rsp), %rbp
leaq 0x40(%rsp), %r15
leaq 0x60(%rsp), %r13
xorl %ebx, %ebx
subl $0x1, %r12d
jb 0x11662
leal 0x1(%rbx), %r14d
movq %rbp, %rdi
leaq 0x12b6f(%rip), %rsi     # 0x24182
movl %r14d, %edx
xorl %eax, %eax
callq 0x3070
movq %r15, %rdi
movq %rbp, %rsi
leaq 0xf(%rsp), %rdx
callq 0x75c4
addl $0x81, %ebx
movq %r13, %rdi
movl %ebx, %esi
movq %r15, %rdx
callq 0x11e20
movq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x120e8
movq %r13, %rdi
callq 0x78cc
movq %r15, %rdi
callq 0x3130
movl %r14d, %ebx
jmp 0x115ff
movq 0x18(%rsp), %rbx
cmpb $0x0, 0x2c3(%rbx)
movl 0x24(%rsp), %ebp
je 0x11692
movzbl 0x978(%rbx), %eax
incl %eax
cmpl %eax, 0x8(%rbx)
je 0x11692
leaq 0x13f4d(%rip), %rsi     # 0x255d6
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpb $0x3, %bpl
jb 0x11759
leaq 0x1e0(%rbx), %r13
leaq 0x1e8(%rbx), %rbp
pushq $0x4
popq %r12
leaq 0x60(%rsp), %r15
cmpl 0x10(%rsp), %r12d
jae 0x11729
movl %r12d, %eax
movq (%rsp), %rcx
leaq (%rcx,%rax), %r14
movzbl (%rcx,%rax), %eax
movl %eax, %ecx
andl $-0x20, %ecx
shll $0x3, %ecx
movl %ecx, 0x60(%rsp)
cmpb $-0x20, %al
jb 0x116e7
movzbl 0x1(%r14), %eax
orl $0x700, %eax            # imm = 0x700
movl %eax, 0x60(%rsp)
movq %r13, %rdi
movq %r15, %rsi
callq 0x1224a
movq %rax, %rbx
xorl %edx, %edx
cmpq %rbp, %rax
setne %dl
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0xfff8
cmpq %rbp, %rbx
jne 0x1171a
movq %r13, %rdi
movq %r15, %rsi
callq 0x1229c
movzbl (%r14), %eax
andl $0x1f, %eax
addl %eax, %r12d
incl %r12d
jmp 0x116b3
movq 0x18(%rsp), %rbx
andq $0x0, 0x38(%rbx)
movq 0x30(%rbx), %rax
movb $0x0, (%rax)
cmpl 0x10(%rsp), %r12d
je 0x11759
leaq 0x13f07(%rip), %rsi     # 0x2564f
xorl %edi, %edi
movq 0x10(%rsp), %rdx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x362c
leaq 0x30(%rbx), %r12
leaq 0xe888(%rip), %rsi      # 0x1ffec
movq %r12, %rdi
callq 0x3460
movb $0x0, 0xc4(%rbx)
movl 0x10(%rsp), %ebx
movb $0x1, %r13b
pushq $0x12
popq %rbp
leaq 0xd687(%rip), %r15      # 0x1ee0b
movq (%rsp), %rax
leaq (%rax,%rbx), %r14
cmpq $0x6d, %rbx
ja 0x117cc
movq %r14, %rdi
movl %ebp, %esi
xorl %edx, %edx
callq 0x4fa5
testb %al, %al
jne 0x117cc
testb $0x1, %r13b
je 0x117b6
movq (%r12), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3030
movq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0xa112
addq $0x12, %rbx
xorl %r13d, %r13d
jmp 0x11784
pushq $0x7f
popq %rsi
subl %ebx, %esi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4fa5
testb %al, %al
movq 0x18(%rsp), %rbx
jne 0x11803
leaq 0x13e85(%rip), %rsi     # 0x25670
movq %r12, %rdi
callq 0x3460
leaq 0x13e7e(%rip), %rsi     # 0x25678
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
andq $0x0, 0x38(%rbx)
movq 0x30(%rbx), %rax
movb $0x0, (%rax)
cmpb $0x1, 0x8e(%rbx)
jne 0x11832
cmpb $0x1, 0x8f(%rbx)
jne 0x11832
leaq 0x13e7a(%rip), %rsi     # 0x256a2
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x362c
cmpb $0x0, 0x2c0(%rbx)
jne 0x11854
cmpb $0x0, 0xc5(%rbx)
jne 0x11854
leaq 0x13eb1(%rip), %rsi     # 0x256fc
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
movl 0x2dc(%rbx), %eax
notl %eax
testl %eax, 0x2d8(%rbx)
je 0x11874
leaq 0x13eef(%rip), %rsi     # 0x2575a
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
cmpb $0x0, 0x2c4(%rbx)
jne 0x1188d
leaq 0x13f1a(%rip), %rsi     # 0x2579e
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x118b1
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x78cc
leaq 0x40(%rsp), %rdi
callq 0x3130
movq %rbx, %rdi
callq 0x3430
nop