// File: rpavlik[P]edid-decode/parse-cta-block.cpp
// Function: edid_state::cta_vcdb(unsigned char const*, unsigned int)
void edid_state::cta_vcdb(const unsigned char *x, unsigned length)
{
	unsigned char d = x[0];

	cta.has_vcdb = true;
	if (length < 1) {
		fail("Empty Data Block with length %u.\n", length);
		return;
	}
	printf("    YCbCr quantization: %s\n",
	       (d & 0x80) ? "Selectable (via AVI YQ)" : "No Data");
	printf("    RGB quantization: %s\n",
	       (d & 0x40) ? "Selectable (via AVI Q)" : "No Data");
	/*
	 * If this bit is not set then that will result in interoperability
	 * problems (specifically with PCs/laptops) that quite often do not
	 * follow the default rules with respect to RGB Quantization Range
	 * handling.
	 *
	 * Starting with the CTA-861-H spec this bit is now required to be
	 * 1 for new designs.
	 */
	if (!(d & 0x40))
		fail("Set Selectable RGB Quantization to avoid interop issues.\n");
	/*
	 * Since most YCbCr formats use limited range, the interop issues are
	 * less noticable than for RGB formats.
	 *
	 * Starting with the CTA-861-H spec this bit is now required to be
	 * 1 for new designs, but just warn about it (for now).
	 */
	if ((cta.byte3 & 0x30) && !(d & 0x80))
		warn("Set Selectable YCbCr Quantization to avoid interop issues.\n");

	unsigned char s_pt = (d >> 4) & 0x03;
	unsigned char s_it = (d >> 2) & 0x03;
	unsigned char s_ce = d & 0x03;

	printf("    PT scan behavior: ");
	switch (s_pt) {
	case 0: printf("No Data\n"); break;
	case 1: printf("Always Overscanned\n"); break;
	case 2: printf("Always Underscanned\n"); break;
	case 3: printf("Supports both over- and underscan\n"); break;
	}
	printf("    IT scan behavior: ");
	switch (s_it) {
	case 0: printf("IT video formats not supported\n"); break;
	case 1:
		printf("Always Overscanned\n");
		// See Table 52 of CTA-861-G for a description of Byte 3
		if (cta.byte3 & 0x80)
			fail("IT video formats are always overscanned, but bit 7 of Byte 3 of the CTA-861 Extension header is set to underscanned.\n");
		break;
	case 2:
		printf("Always Underscanned\n");
		// See Table 52 of CTA-861-G for a description of Byte 3
		if (!(cta.byte3 & 0x80))
			fail("IT video formats are always underscanned, but bit 7 of Byte 3 of the CTA-861 Extension header is set to overscanned.\n");
		break;
	case 3: printf("Supports both over- and underscan\n"); break;
	}
	if (s_it < 2)
		warn("IT scan behavior is expected to support underscanned.\n");
	printf("    CE scan behavior: ");
	switch (s_ce) {
	case 0: printf("CE video formats not supported\n"); break;
	case 1: printf("Always Overscanned\n"); break;
	case 2: printf("Always Underscanned\n"); break;
	case 3: printf("Supports both over- and underscan\n"); break;
	}
	if (s_ce == 0)
		warn("'CE video formats not supported' makes no sense.\n");
	else if (s_pt == s_it && s_pt == s_ce)
		warn("S_PT is equal to S_IT and S_CE, so should be set to 0 instead.\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x10(%rbp), %rcx
movb (%rcx), %cl
movb %cl, -0x15(%rbp)
movb $0x1, 0x2c4(%rax)
cmpl $0x1, -0x14(%rbp)
jae 0x18429
movl -0x14(%rbp), %edx
xorl %edi, %edi
leaq 0x21192(%rip), %rsi     # 0x395af
movb $0x0, %al
callq 0x47d0
jmp 0x186ed
movzbl -0x15(%rbp), %ecx
andl $0x80, %ecx
leaq 0x21d48(%rip), %rsi     # 0x3a182
leaq 0x21d29(%rip), %rax     # 0x3a16a
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x21cff(%rip), %rdi     # 0x3a14e
movb $0x0, %al
callq 0x4050
movzbl -0x15(%rbp), %ecx
andl $0x40, %ecx
leaq 0x21d1e(%rip), %rsi     # 0x3a182
leaq 0x21d39(%rip), %rax     # 0x3a1a4
cmpl $0x0, %ecx
cmovneq %rax, %rsi
leaq 0x21d11(%rip), %rdi     # 0x3a18a
movb $0x0, %al
callq 0x4050
movzbl -0x15(%rbp), %eax
andl $0x40, %eax
cmpl $0x0, %eax
jne 0x1849c
xorl %edi, %edi
leaq 0x22c61(%rip), %rsi     # 0x3b0f6
movb $0x0, %al
callq 0x47d0
movq -0x20(%rbp), %rax
movzbl 0x2c2(%rax), %eax
andl $0x30, %eax
cmpl $0x0, %eax
je 0x184d0
movzbl -0x15(%rbp), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x184d0
movl $0x1, %edi
leaq 0x21cf2(%rip), %rsi     # 0x3a1bb
movb $0x0, %al
callq 0x47d0
movb -0x15(%rbp), %al
shrb $0x4, %al
andb $0x3, %al
movb %al, -0x16(%rbp)
movb -0x15(%rbp), %al
shrb $0x2, %al
andb $0x3, %al
movb %al, -0x17(%rbp)
movb -0x15(%rbp), %al
andb $0x3, %al
movb %al, -0x18(%rbp)
leaq 0x21d02(%rip), %rdi     # 0x3a1f7
xorl %eax, %eax
callq 0x4050
movzbl -0x16(%rbp), %eax
movq %rax, -0x28(%rbp)
subq $0x3, %rax
ja 0x1855c
movq -0x28(%rbp), %rax
leaq 0x1d587(%rip), %rcx     # 0x35a9c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x21ce9(%rip), %rdi     # 0x3a20e
movb $0x0, %al
callq 0x4050
jmp 0x1855c
leaq 0x21ce2(%rip), %rdi     # 0x3a217
movb $0x0, %al
callq 0x4050
jmp 0x1855c
leaq 0x21ce6(%rip), %rdi     # 0x3a22b
movb $0x0, %al
callq 0x4050
jmp 0x1855c
leaq 0x21ceb(%rip), %rdi     # 0x3a240
movb $0x0, %al
callq 0x4050
leaq 0x21d00(%rip), %rdi     # 0x3a263
xorl %eax, %eax
callq 0x4050
movzbl -0x17(%rbp), %eax
movq %rax, -0x30(%rbp)
subq $0x3, %rax
ja 0x18618
movq -0x30(%rbp), %rax
leaq 0x1d525(%rip), %rcx     # 0x35aac
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x21ce3(%rip), %rdi     # 0x3a27a
movb $0x0, %al
callq 0x4050
jmp 0x18618
leaq 0x21c70(%rip), %rdi     # 0x3a217
movb $0x0, %al
callq 0x4050
movq -0x20(%rbp), %rax
movzbl 0x2c2(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x185d3
xorl %edi, %edi
leaq 0x21cce(%rip), %rsi     # 0x3a29a
movb $0x0, %al
callq 0x47d0
jmp 0x18618
leaq 0x21c4f(%rip), %rdi     # 0x3a22b
movb $0x0, %al
callq 0x4050
movq -0x20(%rbp), %rax
movzbl 0x2c2(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x18608
xorl %edi, %edi
leaq 0x21d0f(%rip), %rsi     # 0x3a310
movb $0x0, %al
callq 0x47d0
jmp 0x18618
leaq 0x21c2f(%rip), %rdi     # 0x3a240
movb $0x0, %al
callq 0x4050
movzbl -0x17(%rbp), %eax
cmpl $0x2, %eax
jge 0x18634
movl $0x1, %edi
leaq 0x21d59(%rip), %rsi     # 0x3a386
movb $0x0, %al
callq 0x47d0
leaq 0x21d82(%rip), %rdi     # 0x3a3bd
xorl %eax, %eax
callq 0x4050
movzbl -0x18(%rbp), %eax
movq %rax, -0x38(%rbp)
subq $0x3, %rax
ja 0x186a2
movq -0x38(%rbp), %rax
leaq 0x1d461(%rip), %rcx     # 0x35abc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x21d69(%rip), %rdi     # 0x3a3d4
movb $0x0, %al
callq 0x4050
jmp 0x186a2
leaq 0x21b9c(%rip), %rdi     # 0x3a217
movb $0x0, %al
callq 0x4050
jmp 0x186a2
leaq 0x21ba0(%rip), %rdi     # 0x3a22b
movb $0x0, %al
callq 0x4050
jmp 0x186a2
leaq 0x21ba5(%rip), %rdi     # 0x3a240
movb $0x0, %al
callq 0x4050
movzbl -0x18(%rbp), %eax
cmpl $0x0, %eax
jne 0x186c0
movl $0x1, %edi
leaq 0x21d3d(%rip), %rsi     # 0x3a3f4
movb $0x0, %al
callq 0x47d0
jmp 0x186ed
movzbl -0x16(%rbp), %eax
movzbl -0x17(%rbp), %ecx
cmpl %ecx, %eax
jne 0x186eb
movzbl -0x16(%rbp), %eax
movzbl -0x18(%rbp), %ecx
cmpl %ecx, %eax
jne 0x186eb
movl $0x1, %edi
leaq 0x21d42(%rip), %rsi     # 0x3a426
movb $0x0, %al
callq 0x47d0
jmp 0x186ed
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)