// File: /workspace/github_cpp_makefile_star2_O3/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 $0xe8, %rsp
movq %rsi, %r13
movq %rdi, %rbx
movzbl 0x1(%rsi), %ebp
movzbl 0x2(%rsi), %r14d
leaq 0x1666c(%rip), %rdi     # 0x2b3d9
movl %ebp, %esi
xorl %eax, %eax
callq 0x3030
testl %ebp, %ebp
je 0x14d88
cmpl $0x2, %ebp
jne 0x14d98
leaq 0x1668a(%rip), %rsi     # 0x2b410
jmp 0x14d8f
leaq 0x1665a(%rip), %rsi     # 0x2b3e9
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpl $0x3, %ebp
je 0x14db6
cmpb $0x0, 0x2c3(%rbx)
je 0x14db6
leaq 0x1668d(%rip), %rsi     # 0x2b43a
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpb $0x4, %bpl
jb 0x14dd1
leaq 0x166b2(%rip), %rsi     # 0x2b475
movl $0x1, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x37cc
testl %ebp, %ebp
je 0x15278
cmpl $0x1, %ebp
jne 0x14df5
cmpb $0x0, 0x3(%r13)
je 0x14df5
leaq 0x166b1(%rip), %rsi     # 0x2b49d
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpb $0x4, %r14b
jb 0x15278
cmpb $0x2, %bpl
movq %r13, 0x10(%rsp)
ja 0x14e3d
leal -0x4(%r14), %esi
cmpl $0x8, %esi
jb 0x14e34
shrl $0x3, %esi
leaq 0x16692(%rip), %rdi     # 0x2b4af
xorl %eax, %eax
callq 0x3030
leaq 0x166a5(%rip), %rsi     # 0x2b4d0
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpl $0x1, %ebp
je 0x151ad
cmpb $0x0, 0x3(%r13)
js 0x14e59
leaq 0x166aa(%rip), %rsi     # 0x2b4f5
movl $0x1, %edi
xorl %eax, %eax
callq 0x37cc
jmp 0x14e65
leaq 0x19758(%rip), %rdi     # 0x2e5b8
callq 0x3380
movb 0x3(%r13), %al
testb $0x40, %al
je 0x14e7d
leaq 0x1976d(%rip), %rdi     # 0x2e5e1
callq 0x3380
movb 0x3(%r13), %al
testb $0x20, %al
je 0x14e91
leaq 0x1976f(%rip), %rdi     # 0x2e5f7
callq 0x3380
movb 0x3(%r13), %al
testb $0x10, %al
je 0x14ea5
leaq 0x19772(%rip), %rdi     # 0x2e60e
callq 0x3380
movb 0x3(%r13), %al
andb $0xf, %al
movzbl %al, %esi
leaq 0x1669c(%rip), %rdi     # 0x2b54d
xorl %eax, %eax
callq 0x3030
movb 0x2d5(%rbx), %al
movb 0x3(%r13), %cl
cmpb $0x1, %al
jne 0x14ece
movb %cl, 0x2c2(%rbx)
jmp 0x14eec
cmpb 0x2c2(%rbx), %cl
je 0x14eec
leaq 0x1668d(%rip), %rsi     # 0x2b56a
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
movb 0x2d5(%rbx), %al
testb $0x1, %al
je 0x15098
movq %rbx, (%rsp)
movzbl 0x3(%r13), %ebx
movq (%rsp), %rax
leaq 0x2a8(%rax), %rdi
movq (%rsp), %rax
movq 0x2a8(%rax), %rsi
movq %rdi, 0x8(%rsp)
callq 0xdf82
andl $0xf, %ebx
movq %rbx, %rax
movq (%rsp), %rbx
movl %ebp, 0x1c(%rsp)
jne 0x14f36
cmpb $0x0, 0x2c5(%rbx)
je 0x14f57
movl 0x180(%rbx), %ecx
movq %rax, %rbp
cmpl %ebp, %ecx
jae 0x14f61
leaq 0x1665b(%rip), %rsi     # 0x2b5a5
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x37cc
jmp 0x14f61
movb $0x1, 0x2c1(%rbx)
movq %rax, %rbp
movl 0x180(%rbx), %eax
cmpl %ebp, %eax
cmovbl %eax, %ebp
testl %ebp, %ebp
je 0x15068
negl %ebp
movl $0x81, %r13d
leaq 0x40(%rsp), %r15
leaq 0x50(%rsp), %r12
movq %rbp, %rbx
leal -0x80(%r13), %edx
movq %r15, %rdi
leaq 0x151eb(%rip), %rsi     # 0x2a182
xorl %eax, %eax
callq 0x3070
leaq 0x30(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r15, %rdi
callq 0x3110
leaq (%rsp,%rax), %rdx
addq $0x40, %rdx
leaq 0x20(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x8d58
movq %r12, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0x15b38
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0x15ede
movq 0xc8(%rsp), %rdi
leaq 0xd8(%rsp), %rax
cmpq %rax, %rdi
movq %rbx, %rbp
je 0x1500b
movq 0xd8(%rsp), %rsi
incq %rsi
callq 0x3260
movq 0xa8(%rsp), %rdi
leaq 0xb8(%rsp), %rax
cmpq %rax, %rdi
movq (%rsp), %rbx
je 0x15034
movq 0xb8(%rsp), %rsi
incq %rsi
callq 0x3260
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15050
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3260
leal 0x1(%rbp,%r13), %eax
movl %r13d, %ecx
incl %ecx
movl %ecx, %r13d
cmpl $0x81, %eax
jne 0x14f86
cmpb $0x0, 0x2c3(%rbx)
movq 0x10(%rsp), %r13
movl 0x1c(%rsp), %ebp
je 0x15098
movzbl 0x978(%rbx), %eax
incl %eax
cmpl %eax, 0x8(%rbx)
je 0x15098
leaq 0x16547(%rip), %rsi     # 0x2b5d6
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpb $0x3, %bpl
jb 0x151ad
movl $0x4, %r12d
cmpl $0x4, %r14d
je 0x15183
leaq 0x1e0(%rbx), %rax
movq %rax, 0x8(%rsp)
leaq 0x1e8(%rbx), %rax
movq %rax, (%rsp)
movl $0x4, %r12d
movl %r12d, %eax
leaq (%rax,%r13), %rbp
movzbl (%r13,%rax), %eax
movl %eax, %r15d
andl $-0x20, %r15d
shll $0x3, %r15d
movl %r15d, 0x50(%rsp)
cmpb $-0x20, %al
jb 0x15101
movzbl 0x1(%rbp), %r15d
orl $0x700, %r15d           # imm = 0x700
movl %r15d, 0x50(%rsp)
movq 0x1f0(%rbx), %rax
testq %rax, %rax
je 0x15151
movq (%rsp), %rcx
xorl %edx, %edx
cmpl %r15d, 0x20(%rax)
setb %dl
cmovaeq %rax, %rcx
movq 0x10(%rax,%rdx,8), %rax
testq %rax, %rax
jne 0x15111
cmpq (%rsp), %rcx
je 0x15151
movl 0x20(%rcx), %r13d
xorl %edx, %edx
cmpl %r13d, %r15d
setae %dl
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x12cda
cmpl %r13d, %r15d
movq 0x10(%rsp), %r13
jb 0x1515e
jmp 0x1516d
movq %rbx, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x12cda
movq 0x8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x1600c
movzbl (%rbp), %eax
andl $0x1f, %eax
addl %eax, %r12d
incl %r12d
cmpl %r14d, %r12d
jb 0x150cf
movq $0x0, 0x38(%rbx)
movq 0x30(%rbx), %rax
movb $0x0, (%rax)
cmpl %r14d, %r12d
je 0x151ad
leaq 0x164b1(%rip), %rsi     # 0x2b64f
xorl %edi, %edi
movl %r14d, %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x37cc
leaq 0x30(%rbx), %r12
movq 0x38(%rbx), %rdx
leaq 0x10e30(%rip), %rcx     # 0x25fec
movl $0x1b, %r8d
movq %r12, %rdi
xorl %esi, %esi
callq 0x3390
movb $0x0, 0xc4(%rbx)
cmpb $0x6d, %r14b
ja 0x15234
movb $0x1, %bpl
leaq 0xfc28(%rip), %r15      # 0x24e0b
addq %r14, %r13
movq %r13, %rdi
movl $0x12, %esi
xorl %edx, %edx
callq 0x5ba5
testb %al, %al
jne 0x15237
testb $0x1, %bpl
je 0x1520d
movq (%r12), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3030
movq %rbx, %rdi
movq %r13, %rsi
callq 0xbe42
leaq 0x12(%r14), %rax
xorl %ebp, %ebp
cmpq $0x5c, %r14
movq %rax, %r14
movq 0x10(%rsp), %r13
jb 0x151e3
addq %rax, %r13
movq %rax, %r14
jmp 0x15237
addq %r14, %r13
movl $0x7f, %esi
subl %r14d, %esi
movq %r13, %rdi
xorl %edx, %edx
callq 0x5ba5
testb %al, %al
jne 0x15278
movq 0x38(%rbx), %rdx
leaq 0x16418(%rip), %rcx     # 0x2b670
movl $0x7, %r8d
movq %r12, %rdi
xorl %esi, %esi
callq 0x3390
leaq 0x16409(%rip), %rsi     # 0x2b678
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
movq $0x0, 0x38(%rbx)
movq 0x30(%rbx), %rax
movb $0x0, (%rax)
cmpb $0x1, 0x8e(%rbx)
jne 0x152ac
cmpb $0x1, 0x8f(%rbx)
jne 0x152ac
leaq 0x16402(%rip), %rsi     # 0x2b6a2
movl $0x1, %edi
xorl %eax, %eax
callq 0x37cc
cmpb $0x0, 0x2c0(%rbx)
jne 0x152ce
cmpb $0x0, 0xc5(%rbx)
jne 0x152ce
leaq 0x16437(%rip), %rsi     # 0x2b6fc
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
movl 0x2dc(%rbx), %eax
notl %eax
testl %eax, 0x2d8(%rbx)
je 0x152ee
leaq 0x16475(%rip), %rsi     # 0x2b75a
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
cmpb $0x0, 0x2c4(%rbx)
jne 0x15307
leaq 0x164a0(%rip), %rsi     # 0x2b79e
xorl %edi, %edi
xorl %eax, %eax
callq 0x37cc
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x1532b
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xdbdc
movq 0x20(%rsp), %rdi
leaq 0x30(%rsp), %rax
cmpq %rax, %rdi
je 0x15347
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x3260
movq %rbx, %rdi
callq 0x3400
nop