// File: rpavlik[P]edid-decode/parse-cta-block.cpp
// Function: edid_state::cta_rcdb(unsigned char const*, unsigned int)
void edid_state::cta_rcdb(const unsigned char *x, unsigned length)
{
	unsigned spm = ((x[3] << 16) | (x[2] << 8) | x[1]);
	unsigned i;

	if (length < 4) {
		fail("Empty Data Block with length %u.\n", length);
		return;
	}

	if ((x[0] & 0x20) && !cta.has_sldb)
		fail("'SLD' flag is 1, but no Speaker Location Data Block is found.\n");
	else if (!(x[0] & 0x20) && cta.has_sldb)
		fail("'SLD' flag is 0, but a Speaker Location Data Block is present.\n");

	if (x[0] & 0x40) {
		printf("    Speaker count: %u\n", (x[0] & 0x1f) + 1);
	} else {
		if (x[0] & 0x1f)
			fail("'Speaker' flag is 0, but 'Speaker Count' is != 0.\n");
		if (x[0] & 0x20)
			fail("'SLD' flag is 1, but 'Speaker' is 0.\n");
	}

	printf("    Speaker Presence Mask:\n");
	for (i = 0; i < ARRAY_SIZE(speaker_map); i++) {
		if ((spm >> i) & 1)
			printf("      %s\n", speaker_map[i]);
	}

	if ((x[0] & 0xa0) == 0x80)
		fail("'Display' flag set, but not the 'SLD' flag.\n");

	bool valid_max = cta.preparsed_sld_has_coord || (x[0] & 0x80);

	if (valid_max && length >= 7) {
		printf("    Xmax: %u dm\n", x[4]);
		printf("    Ymax: %u dm\n", x[5]);
		printf("    Zmax: %u dm\n", x[6]);
	} else if (!valid_max && length >= 7) {
		// The RCDB should have been truncated.
		warn("'Display' flag is 0 and 'Coord' is 0 for all SLDs, but the Max coordinates are still present.\n");
	}
	if ((x[0] & 0x80) && length >= 10) {
		printf("    DisplayX: %.3f * Xmax\n", decode_uchar_as_double(x[7]));
		printf("    DisplayY: %.3f * Ymax\n", decode_uchar_as_double(x[8]));
		printf("    DisplayZ: %.3f * Zmax\n", decode_uchar_as_double(x[9]));
	} else if (!(x[0] & 0x80) && length >= 10) {
		// The RCDB should have been truncated.
		warn("'Display' flag is 0, but the Display coordinates are still present.\n");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
shll $0x10, %eax
movq -0x10(%rbp), %rcx
movzbl 0x2(%rcx), %ecx
shll $0x8, %ecx
orl %ecx, %eax
movq -0x10(%rbp), %rcx
movzbl 0x1(%rcx), %ecx
orl %ecx, %eax
movl %eax, -0x18(%rbp)
cmpl $0x4, -0x14(%rbp)
jae 0x17ede
movl -0x14(%rbp), %edx
xorl %edi, %edi
leaq 0x216dd(%rip), %rsi     # 0x395af
movb $0x0, %al
callq 0x47d0
jmp 0x18158
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x17f0c
movq -0x28(%rbp), %rax
testb $0x1, 0x2ce(%rax)
jne 0x17f0c
xorl %edi, %edi
leaq 0x21f07(%rip), %rsi     # 0x39e0a
movb $0x0, %al
callq 0x47d0
jmp 0x17f3a
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x17f38
movq -0x28(%rbp), %rax
testb $0x1, 0x2ce(%rax)
je 0x17f38
xorl %edi, %edi
leaq 0x21f18(%rip), %rsi     # 0x39e49
movb $0x0, %al
callq 0x47d0
jmp 0x17f3a
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x17f66
movq -0x10(%rbp), %rax
movzbl (%rax), %esi
andl $0x1f, %esi
addl $0x1, %esi
leaq 0x21f2c(%rip), %rdi     # 0x39e89
movb $0x0, %al
callq 0x4050
jmp 0x17fa6
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x1f, %eax
cmpl $0x0, %eax
je 0x17f85
xorl %edi, %edi
leaq 0x21f22(%rip), %rsi     # 0x39ea0
movb $0x0, %al
callq 0x47d0
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
je 0x17fa4
xorl %edi, %edi
leaq 0x21f36(%rip), %rsi     # 0x39ed3
movb $0x0, %al
callq 0x47d0
jmp 0x17fa6
leaq 0x21f4c(%rip), %rdi     # 0x39ef9
movb $0x0, %al
callq 0x4050
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
cmpq $0x15, %rax
jae 0x17fff
movl -0x18(%rbp), %eax
movl -0x1c(%rbp), %ecx
shrl %cl, %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x17ff2
movl -0x1c(%rbp), %eax
movl %eax, %ecx
leaq 0x336c0(%rip), %rax     # 0x4b6a0
movq (%rax,%rcx,8), %rsi
leaq 0x21f2a(%rip), %rdi     # 0x39f15
movb $0x0, %al
callq 0x4050
jmp 0x17ff4
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x17fbb
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0xa0, %eax
cmpl $0x80, %eax
jne 0x18022
xorl %edi, %edi
leaq 0x21f04(%rip), %rsi     # 0x39f1f
movb $0x0, %al
callq 0x47d0
movq -0x28(%rbp), %rcx
movb $0x1, %al
testb $0x1, 0x2cc(%rcx)
movb %al, -0x29(%rbp)
jne 0x18049
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
movb %al, -0x1d(%rbp)
testb $0x1, -0x1d(%rbp)
je 0x180a1
cmpl $0x7, -0x14(%rbp)
jb 0x180a1
movq -0x10(%rbp), %rax
movzbl 0x4(%rax), %esi
leaq 0x21ee0(%rip), %rdi     # 0x39f4c
movb $0x0, %al
callq 0x4050
movq -0x10(%rbp), %rax
movzbl 0x5(%rax), %esi
leaq 0x21edb(%rip), %rdi     # 0x39f5d
movb $0x0, %al
callq 0x4050
movq -0x10(%rbp), %rax
movzbl 0x6(%rax), %esi
leaq 0x21ed6(%rip), %rdi     # 0x39f6e
movb $0x0, %al
callq 0x4050
jmp 0x180c2
testb $0x1, -0x1d(%rbp)
jne 0x180c0
cmpl $0x7, -0x14(%rbp)
jb 0x180c0
movl $0x1, %edi
leaq 0x21ec6(%rip), %rsi     # 0x39f7f
movb $0x0, %al
callq 0x47d0
jmp 0x180c2
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x1812c
cmpl $0xa, -0x14(%rbp)
jb 0x1812c
movq -0x10(%rbp), %rax
movzbl 0x7(%rax), %edi
callq 0x18160
leaq 0x21ef1(%rip), %rdi     # 0x39fde
movb $0x1, %al
callq 0x4050
movq -0x10(%rbp), %rax
movzbl 0x8(%rax), %edi
callq 0x18160
leaq 0x21ef1(%rip), %rdi     # 0x39ff9
movb $0x1, %al
callq 0x4050
movq -0x10(%rbp), %rax
movzbl 0x9(%rax), %edi
callq 0x18160
leaq 0x21ef1(%rip), %rdi     # 0x3a014
movb $0x1, %al
callq 0x4050
jmp 0x18158
movq -0x10(%rbp), %rax
movzbl (%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
jne 0x18156
cmpl $0xa, -0x14(%rbp)
jb 0x18156
movl $0x1, %edi
leaq 0x21ee0(%rip), %rsi     # 0x3a02f
movb $0x0, %al
callq 0x47d0
jmp 0x18158
addq $0x30, %rsp
popq %rbp
retq
nop