// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
cmpl $0x3, %edx
ja 0xd756
leaq 0x16a12(%rip), %rsi     # 0x2414f
xorl %edi, %edi
movl %ebp, %edx
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x362c
movq %rsi, %rbx
movzwl 0x2(%rsi), %r12d
movzbl 0x1(%rsi), %r15d
testb $0x20, (%rsi)
movq %rdi, (%rsp)
movb 0x2ce(%rdi), %al
jne 0xd77f
testb %al, %al
je 0xd793
leaq 0x16ef8(%rip), %rsi     # 0x24675
jmp 0xd78a
testb %al, %al
jne 0xd793
leaq 0x16eac(%rip), %rsi     # 0x24636
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
shll $0x8, %r12d
movzbl (%rbx), %eax
movl %eax, %esi
andl $0x1f, %esi
testb $0x40, %al
jne 0xd7cf
testl %esi, %esi
je 0xd7b9
leaq 0x16f1e(%rip), %rsi     # 0x246cc
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
movb (%rbx), %al
testb $0x20, %al
je 0xd7df
leaq 0x16f3b(%rip), %rsi     # 0x246ff
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
jmp 0xd7df
incl %esi
leaq 0x16edd(%rip), %rdi     # 0x246b5
xorl %eax, %eax
callq 0x3030
orl %r15d, %r12d
leaq 0x1a113(%rip), %rdi     # 0x278fc
callq 0x33b0
leaq 0x2647b(%rip), %r13     # 0x33c70
leaq 0x16f29(%rip), %r15     # 0x24725
xorl %r14d, %r14d
cmpq $0x15, %r14
je 0xd822
btl %r14d, %r12d
jae 0xd819
movq (%r13), %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x3030
incq %r14
addq $0x8, %r13
jmp 0xd7ff
movb (%rbx), %al
andb $-0x60, %al
negb %al
jno 0xd83a
leaq 0x16efe(%rip), %rsi     # 0x2472f
xorl %edi, %edi
xorl %eax, %eax
callq 0x362c
movb $0x1, %al
movq (%rsp), %rcx
cmpb $0x0, 0x2cc(%rcx)
jne 0xd84e
movb (%rbx), %al
shrb $0x7, %al
cmpl $0x7, %ebp
setb %cl
movl %eax, %edx
xorb $0x1, %dl
orb %cl, %dl
jne 0xd895
movzbl 0x4(%rbx), %esi
leaq 0x16ef4(%rip), %rdi     # 0x2475c
xorl %eax, %eax
callq 0x3030
movzbl 0x5(%rbx), %esi
leaq 0x16ef3(%rip), %rdi     # 0x2476d
xorl %eax, %eax
callq 0x3030
movzbl 0x6(%rbx), %esi
leaq 0x16ef2(%rip), %rdi     # 0x2477e
xorl %eax, %eax
callq 0x3030
jmp 0xd8b0
cmpl $0x7, %ebp
setb %cl
orb %al, %cl
jne 0xd8b0
leaq 0x16ee9(%rip), %rsi     # 0x2478f
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x362c
movb (%rbx), %al
testb $-0x80, %al
sete %cl
cmpl $0xa, %ebp
setb %dl
orb %cl, %dl
jne 0xd929
movsbl 0x7(%rbx), %eax
cvtsi2sd %eax, %xmm0
mulsd 0x1666f(%rip), %xmm0    # 0x23f40
leaq 0x16f16(%rip), %rdi     # 0x247ee
movb $0x1, %al
callq 0x3030
movsbl 0x8(%rbx), %eax
cvtsi2sd %eax, %xmm0
mulsd 0x16651(%rip), %xmm0    # 0x23f40
leaq 0x16f13(%rip), %rdi     # 0x24809
movb $0x1, %al
callq 0x3030
movsbl 0x9(%rbx), %eax
cvtsi2sd %eax, %xmm0
mulsd 0x16633(%rip), %xmm0    # 0x23f40
leaq 0x16f10(%rip), %rdi     # 0x24824
movb $0x1, %al
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3030
cmpl $0xa, %ebp
setb %cl
shrb $0x7, %al
orb %cl, %al
jne 0xd955
leaq 0x16f02(%rip), %rsi     # 0x2483f
pushq $0x1
popq %rdi
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x362c
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq