// File: rpavlik[P]edid-decode/parse-displayid-block.cpp
// Function: edid_state::parse_displayid_transfer_characteristics(unsigned char const*)
void edid_state::parse_displayid_transfer_characteristics(const unsigned char *x)
{
	check_displayid_datablock_revision(x[1], 0xf0, 1);

	unsigned xfer_id = x[1] >> 4;
	bool first_is_white = x[3] & 0x80;
	bool four_param = x[3] & 0x20;

	if (xfer_id) {
		printf("    Transfer Characteristics Data Block Identifier: %u\n", xfer_id);
		if (!(dispid.preparsed_color_ids & (1 << xfer_id)))
			fail("Missing Color Characteristics Data Block using Identifier %u.\n", xfer_id);
	}
	if (first_is_white)
		printf("    The first curve is the 'white' transfer characteristic\n");
	if (x[3] & 0x40)
		printf("    Individual response curves\n");

	unsigned offset = 4;
	unsigned len = x[2] - 1;

	for (unsigned i = 0; len; i++) {
		if ((x[3] & 0x80) && !i)
			printf("    White curve:      ");
		else
			printf("    Response curve #%u:",
			       i - first_is_white);
		unsigned samples = x[offset];
		if (four_param) {
			if (samples != 5)
				fail("Expected 5 samples.\n");
			printf(" A0=%u A1=%u A2=%u A3=%u Gamma=%.2f\n",
			       x[offset + 1], x[offset + 2], x[offset + 3], x[offset + 4],
			       (double)(x[offset + 5] + 100.0) / 100.0);
			samples++;
		} else {
			double sum = 0;

			// The spec is not very clear about the number of samples:
			// should this be interpreted as the actual number of
			// samples stored in this Data Block, or as the number of
			// samples in the curve, but where the last sample is not
			// actually stored since it is always 0x3ff.
			//
			// The ATP Manager interprets this as the latter, so that's
			// what we implement here.
			for (unsigned j = offset + 1; j < offset + samples; j++) {
				sum += x[j];
				printf(" %.2f", sum * 100.0 / 1023.0);
			}
			printf(" 100.00\n");
		}
		offset += samples;
		len -= samples;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x40(%rbp)
movq -0x10(%rbp), %rax
movl $0xf0, %edx
movl $0x1, %ecx
movzbl 0x1(%rax), %esi
callq 0x21b00
movq -0x10(%rbp), %rax
movzbl 0x1(%rax), %eax
sarl $0x4, %eax
movl %eax, -0x14(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x15(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
setne %al
andb $0x1, %al
movb %al, -0x16(%rbp)
cmpl $0x0, -0x14(%rbp)
je 0x24715
movl -0x14(%rbp), %esi
leaq 0x1a856(%rip), %rdi     # 0x3ef31
movb $0x0, %al
callq 0x4050
movq -0x40(%rbp), %rax
movzwl 0x912(%rax), %eax
movl -0x14(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
jne 0x24713
movl -0x14(%rbp), %edx
xorl %edi, %edi
leaq 0x1a85d(%rip), %rsi     # 0x3ef69
movb $0x0, %al
callq 0x47d0
jmp 0x24715
testb $0x1, -0x15(%rbp)
je 0x24729
leaq 0x1a886(%rip), %rdi     # 0x3efa8
movb $0x0, %al
callq 0x4050
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x40, %eax
cmpl $0x0, %eax
je 0x24747
leaq 0x1a8a4(%rip), %rdi     # 0x3efe4
movb $0x0, %al
callq 0x4050
movl $0x4, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movzbl 0x2(%rax), %eax
subl $0x1, %eax
movl %eax, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
cmpl $0x0, -0x20(%rbp)
je 0x248fd
movq -0x10(%rbp), %rax
movzbl 0x3(%rax), %eax
andl $0x80, %eax
cmpl $0x0, %eax
je 0x24795
cmpl $0x0, -0x24(%rbp)
jne 0x24795
leaq 0x1a878(%rip), %rdi     # 0x3f004
movb $0x0, %al
callq 0x4050
jmp 0x247b0
movl -0x24(%rbp), %esi
movb -0x15(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
subl %eax, %esi
leaq 0x1a872(%rip), %rdi     # 0x3f01b
movb $0x0, %al
callq 0x4050
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x28(%rbp)
testb $0x1, -0x16(%rbp)
je 0x24864
cmpl $0x5, -0x28(%rbp)
je 0x247de
xorl %edi, %edi
leaq 0x1a85c(%rip), %rsi     # 0x3f033
movb $0x0, %al
callq 0x47d0
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x1, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %esi
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x2, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %edx
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %ecx
addl $0x3, %ecx
movl %ecx, %ecx
movzbl (%rax,%rcx), %ecx
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %edi
addl $0x4, %edi
movl %edi, %edi
movzbl (%rax,%rdi), %r8d
movq -0x10(%rbp), %rax
movl -0x1c(%rbp), %edi
addl $0x5, %edi
movl %edi, %edi
movzbl (%rax,%rdi), %eax
cvtsi2sd %eax, %xmm0
movsd 0xa7fd(%rip), %xmm1     # 0x2f038
addsd %xmm1, %xmm0
movsd 0xa7f1(%rip), %xmm1     # 0x2f038
divsd %xmm1, %xmm0
leaq 0x1a7f6(%rip), %rdi     # 0x3f048
movb $0x1, %al
callq 0x4050
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x248db
xorps %xmm0, %xmm0
movsd %xmm0, -0x30(%rbp)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x1c(%rbp), %ecx
addl -0x28(%rbp), %ecx
cmpl %ecx, %eax
jae 0x248cd
movq -0x10(%rbp), %rax
movl -0x34(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cvtsi2sd %eax, %xmm0
addsd -0x30(%rbp), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd 0xa795(%rip), %xmm0     # 0x2f038
mulsd -0x30(%rbp), %xmm0
movsd 0x192b0(%rip), %xmm1    # 0x3db60
divsd %xmm1, %xmm0
leaq 0x10227(%rip), %rdi     # 0x34ae2
movb $0x1, %al
callq 0x4050
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x24875
leaq 0x1a799(%rip), %rdi     # 0x3f06d
movb $0x0, %al
callq 0x4050
movl -0x28(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x1c(%rbp)
movl -0x28(%rbp), %ecx
movl -0x20(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x20(%rbp)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x24763
addq $0x40, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)