// File: rpavlik[P]edid-decode/parse-base-block.cpp
// Function: edid_state::check_base_block()
void edid_state::check_base_block()
{
	data_block = "Base EDID";

	/*
	 * Allow for regular rounding of vertical and horizontal frequencies.
	 * The spec says that the pixelclock shall be rounded up, so there is
	 * no need to take rounding into account.
	 */
	if (base.has_display_range_descriptor &&
	    (min_vert_freq_hz + 0.5 < base.min_display_vert_freq_hz ||
	     (max_vert_freq_hz >= base.max_display_vert_freq_hz + 0.5 && base.max_display_vert_freq_hz) ||
	     min_hor_freq_hz + 500 < base.min_display_hor_freq_hz ||
	     (max_hor_freq_hz >= base.max_display_hor_freq_hz + 500 && base.max_display_hor_freq_hz) ||
	     (max_pixclk_khz > base.max_display_pixclk_khz && base.max_display_pixclk_khz))) {
		/*
		 * Check if it is really out of range, or if it could be a rounding error.
		 * The EDID spec is not very clear about rounding.
		 */
		bool out_of_range =
			min_vert_freq_hz + 1.0 <= base.min_display_vert_freq_hz ||
			(max_vert_freq_hz >= base.max_display_vert_freq_hz + 1.0 && base.max_display_vert_freq_hz) ||
			min_hor_freq_hz + 1000 <= base.min_display_hor_freq_hz ||
			(max_hor_freq_hz >= base.max_display_hor_freq_hz + 1000 && base.max_display_hor_freq_hz) ||
			(max_pixclk_khz >= base.max_display_pixclk_khz + 10000 && base.max_display_pixclk_khz);

		std::string err("Some timings are out of range of the Monitor Ranges:\n");
		char buf[512];

		if (min_vert_freq_hz + 0.5 < base.min_display_vert_freq_hz ||
		    (max_vert_freq_hz >= base.max_display_vert_freq_hz + 0.5 && base.max_display_vert_freq_hz)) {
			sprintf(buf, "    Vertical Freq: %.3f - %.3f Hz (Monitor: %u.000 - %u.000 Hz)\n",
				min_vert_freq_hz, max_vert_freq_hz,
				base.min_display_vert_freq_hz, base.max_display_vert_freq_hz);
			err += buf;
		}

		if (min_hor_freq_hz + 500 < base.min_display_hor_freq_hz ||
		    (max_hor_freq_hz >= base.max_display_hor_freq_hz + 500 && base.max_display_hor_freq_hz)) {
			sprintf(buf, "    Horizontal Freq: %.3f - %.3f kHz (Monitor: %.3f - %.3f kHz)\n",
				min_hor_freq_hz / 1000.0, max_hor_freq_hz / 1000.0,
				base.min_display_hor_freq_hz / 1000.0, base.max_display_hor_freq_hz / 1000.0);
			err += buf;
		}

		if (max_pixclk_khz >= base.max_display_pixclk_khz && base.max_display_pixclk_khz) {
			sprintf(buf, "    Maximum Clock: %.3f MHz (Monitor: %.3f MHz)\n",
				max_pixclk_khz / 1000.0, base.max_display_pixclk_khz / 1000.0);
			err += buf;
		}

		if (!out_of_range)
			err += "    Could be due to a Monitor Range off-by-one rounding issue\n";

		/*
		 * EDID 1.4 states (in an Errata) that explicitly defined
		 * timings supersede the monitor range definition.
		 */
		msg(!out_of_range || base.edid_minor >= 4, "%s", err.c_str());
	}
	// The base block will only go up to 255x255 cm for the display size,
	// so don't fail if one or more image sizes exceeds that.
	if (!base.max_display_width_mm && !base.max_display_height_mm &&
	    dtd_max_hsize_mm && dtd_max_vsize_mm &&
	    dtd_max_hsize_mm <= 2559 && dtd_max_vsize_mm <= 2559) {
		fail("The DTD image sizes all fit inside 255x255cm, but no display size was set.\n");
	}
	// Secondary GTF curves start at a specific frequency. Any legacy timings
	// that have a positive hsync and negative vsync must be less than that
	// frequency to avoid confusion.
	if (base.supports_sec_gtf && base.max_pos_neg_hor_freq_khz >= base.sec_gtf_start_freq)
		fail("Second GTF start frequency %u is less than the highest P/N frequency %u.\n",
		     base.sec_gtf_start_freq, base.max_pos_neg_hor_freq_khz);
	if (base.edid_minor == 3 && num_blocks > 2 && !block_map.saw_block_1)
		fail("EDID 1.3 requires a Block Map Extension in Block 1 if there are more than 2 blocks in the EDID.\n");
	if (base.edid_minor == 3 && num_blocks > 128 && !block_map.saw_block_128)
		fail("EDID 1.3 requires a Block Map Extension in Block 128 if there are more than 128 blocks in the EDID.\n");
	if (block_map.saw_block_128 && num_blocks > 255)
		fail("If there is a Block Map Extension in Block 128 then the maximum number of blocks is 255.\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x290, %rsp            # imm = 0x290
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x258(%rbp)
addq $0x30, %rdi
leaq 0x1b3ef(%rip), %rsi     # 0x2f614
callq 0x4420
movq -0x258(%rbp), %rax
testb $0x1, 0x8d(%rax)
je 0x146fa
movq -0x258(%rbp), %rax
movsd 0x60(%rax), %xmm1
movsd 0x1adc6(%rip), %xmm0    # 0x2f018
addsd %xmm0, %xmm1
movl 0x168(%rax), %eax
cvtsi2sd %rax, %xmm0
ucomisd %xmm1, %xmm0
ja 0x1430f
movq -0x258(%rbp), %rax
movsd 0x68(%rax), %xmm0
movl 0x16c(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x1ad8e(%rip), %xmm2    # 0x2f018
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x142a4
movq -0x258(%rbp), %rax
cmpl $0x0, 0x16c(%rax)
jne 0x1430f
movq -0x258(%rbp), %rcx
movl 0x54(%rcx), %eax
addl $0x1f4, %eax            # imm = 0x1F4
cmpl 0x160(%rcx), %eax
jb 0x1430f
movq -0x258(%rbp), %rcx
movl 0x58(%rcx), %eax
movl 0x164(%rcx), %ecx
addl $0x1f4, %ecx            # imm = 0x1F4
cmpl %ecx, %eax
jb 0x142e5
movq -0x258(%rbp), %rax
cmpl $0x0, 0x164(%rax)
jne 0x1430f
movq -0x258(%rbp), %rcx
movl 0x70(%rcx), %eax
cmpl 0x170(%rcx), %eax
jbe 0x146fa
movq -0x258(%rbp), %rax
cmpl $0x0, 0x170(%rax)
je 0x146fa
movq -0x258(%rbp), %rax
movsd 0x60(%rax), %xmm1
movsd 0x1f565(%rip), %xmm0    # 0x33888
addsd %xmm0, %xmm1
movl 0x168(%rax), %eax
cvtsi2sd %rax, %xmm0
movb $0x1, %al
ucomisd %xmm1, %xmm0
movb %al, -0x259(%rbp)
jae 0x14420
movq -0x258(%rbp), %rax
movsd 0x68(%rax), %xmm0
movl 0x16c(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x1f525(%rip), %xmm2    # 0x33888
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14389
movq -0x258(%rbp), %rcx
movb $0x1, %al
cmpl $0x0, 0x16c(%rcx)
movb %al, -0x259(%rbp)
jne 0x14420
movq -0x258(%rbp), %rdx
movl 0x54(%rdx), %ecx
addl $0x3e8, %ecx            # imm = 0x3E8
movb $0x1, %al
cmpl 0x160(%rdx), %ecx
movb %al, -0x259(%rbp)
jbe 0x14420
movq -0x258(%rbp), %rcx
movl 0x58(%rcx), %eax
movl 0x164(%rcx), %ecx
addl $0x3e8, %ecx            # imm = 0x3E8
cmpl %ecx, %eax
jb 0x143db
movq -0x258(%rbp), %rcx
movb $0x1, %al
cmpl $0x0, 0x164(%rcx)
movb %al, -0x259(%rbp)
jne 0x14420
movq -0x258(%rbp), %rax
movl 0x70(%rax), %ecx
movl 0x170(%rax), %edx
addl $0x2710, %edx           # imm = 0x2710
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0x25a(%rbp)
jb 0x14414
movq -0x258(%rbp), %rax
cmpl $0x0, 0x170(%rax)
setne %al
movb %al, -0x25a(%rbp)
movb -0x25a(%rbp), %al
movb %al, -0x259(%rbp)
movb -0x259(%rbp), %al
andb $0x1, %al
movb %al, -0x9(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0x268(%rbp)
callq 0x4560
movq -0x268(%rbp), %rdx
leaq 0x21257(%rip), %rsi     # 0x356a0
leaq -0x30(%rbp), %rdi
callq 0xb510
jmp 0x14454
leaq -0x31(%rbp), %rdi
callq 0x4370
movq -0x258(%rbp), %rax
movsd 0x60(%rax), %xmm1
movsd 0x1aba7(%rip), %xmm0    # 0x2f018
addsd %xmm0, %xmm1
movl 0x168(%rax), %eax
cvtsi2sd %rax, %xmm0
ucomisd %xmm1, %xmm0
ja 0x144c7
movq -0x258(%rbp), %rax
movsd 0x68(%rax), %xmm0
movl 0x16c(%rax), %eax
cvtsi2sd %rax, %xmm1
movsd 0x1ab73(%rip), %xmm2    # 0x2f018
addsd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x14548
movq -0x258(%rbp), %rax
cmpl $0x0, 0x16c(%rax)
je 0x14548
movq -0x258(%rbp), %rax
movsd 0x60(%rax), %xmm0
movsd 0x68(%rax), %xmm1
movl 0x168(%rax), %edx
movl 0x16c(%rax), %ecx
leaq 0x211eb(%rip), %rsi     # 0x356d6
leaq -0x250(%rbp), %rdi
movq %rdi, -0x270(%rbp)
movb $0x2, %al
callq 0x40a0
movq -0x270(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x4300
jmp 0x14512
jmp 0x14548
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x31(%rbp), %rdi
callq 0x4370
jmp 0x14862
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x41f0
jmp 0x14862
movq -0x258(%rbp), %rcx
movl 0x54(%rcx), %eax
addl $0x1f4, %eax            # imm = 0x1F4
cmpl 0x160(%rcx), %eax
jb 0x1458d
movq -0x258(%rbp), %rcx
movl 0x58(%rcx), %eax
movl 0x164(%rcx), %ecx
addl $0x1f4, %ecx            # imm = 0x1F4
cmpl %ecx, %eax
jb 0x14602
movq -0x258(%rbp), %rax
cmpl $0x0, 0x164(%rax)
je 0x14602
movq -0x258(%rbp), %rax
movl 0x54(%rax), %edx
movl 0x58(%rax), %ecx
cvtsi2sd %rdx, %xmm0
movsd 0x1aa79(%rip), %xmm4    # 0x2f020
divsd %xmm4, %xmm0
cvtsi2sd %rcx, %xmm1
divsd %xmm4, %xmm1
movl 0x160(%rax), %ecx
cvtsi2sd %rcx, %xmm2
divsd %xmm4, %xmm2
movl 0x164(%rax), %eax
cvtsi2sd %rax, %xmm3
divsd %xmm4, %xmm3
leaq 0x2113e(%rip), %rsi     # 0x35717
leaq -0x250(%rbp), %rdi
movq %rdi, -0x278(%rbp)
movb $0x4, %al
callq 0x40a0
movq -0x278(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x4300
jmp 0x14600
jmp 0x14602
movq -0x258(%rbp), %rcx
movl 0x70(%rcx), %eax
cmpl 0x170(%rcx), %eax
jb 0x1467e
movq -0x258(%rbp), %rax
cmpl $0x0, 0x170(%rax)
je 0x1467e
movq -0x258(%rbp), %rax
movl 0x70(%rax), %ecx
movl 0x170(%rax), %eax
cvtsi2sd %rcx, %xmm0
movsd 0x1a9df(%rip), %xmm2    # 0x2f020
divsd %xmm2, %xmm0
cvtsi2sd %rax, %xmm1
divsd %xmm2, %xmm1
leaq 0x21103(%rip), %rsi     # 0x35758
leaq -0x250(%rbp), %rdi
movq %rdi, -0x280(%rbp)
movb $0x2, %al
callq 0x40a0
movq -0x280(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x4300
jmp 0x1467c
jmp 0x1467e
testb $0x1, -0x9(%rbp)
jne 0x14698
leaq 0x210fe(%rip), %rsi     # 0x35789
leaq -0x30(%rbp), %rdi
callq 0x4300
jmp 0x14696
jmp 0x14698
movb $0x1, %al
testb $0x1, -0x9(%rbp)
movb %al, -0x281(%rbp)
je 0x146bd
movq -0x258(%rbp), %rax
cmpl $0x4, 0x88(%rax)
setae %al
movb %al, -0x281(%rbp)
movb -0x281(%rbp), %al
movb %al, -0x282(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x4150
movq %rax, %rdx
movb -0x282(%rbp), %al
movzbl %al, %edi
andl $0x1, %edi
leaq 0x1b8d4(%rip), %rsi     # 0x2ffbc
xorl %eax, %eax
callq 0x47d0
jmp 0x146f1
leaq -0x30(%rbp), %rdi
callq 0x41f0
movq -0x258(%rbp), %rax
cmpl $0x0, 0x174(%rax)
jne 0x14764
movq -0x258(%rbp), %rax
cmpl $0x0, 0x178(%rax)
jne 0x14764
movq -0x258(%rbp), %rax
cmpl $0x0, 0x74(%rax)
je 0x14764
movq -0x258(%rbp), %rax
cmpl $0x0, 0x78(%rax)
je 0x14764
movq -0x258(%rbp), %rax
cmpl $0x9ff, 0x74(%rax)      # imm = 0x9FF
ja 0x14764
movq -0x258(%rbp), %rax
cmpl $0x9ff, 0x78(%rax)      # imm = 0x9FF
ja 0x14764
xorl %edi, %edi
leaq 0x2106b(%rip), %rsi     # 0x357c8
movb $0x0, %al
callq 0x47d0
movq -0x258(%rbp), %rax
testb $0x1, 0x92(%rax)
je 0x147ac
movq -0x258(%rbp), %rcx
movl 0x17c(%rcx), %eax
cmpl 0x94(%rcx), %eax
jb 0x147ac
movq -0x258(%rbp), %rax
movl 0x94(%rax), %edx
movl 0x17c(%rax), %ecx
xorl %edi, %edi
leaq 0x2106f(%rip), %rsi     # 0x35814
movb $0x0, %al
callq 0x47d0
movq -0x258(%rbp), %rax
cmpl $0x3, 0x88(%rax)
jne 0x147e9
movq -0x258(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jbe 0x147e9
movq -0x258(%rbp), %rax
testb $0x1, 0x978(%rax)
jne 0x147e9
xorl %edi, %edi
leaq 0x2107c(%rip), %rsi     # 0x3585e
movb $0x0, %al
callq 0x47d0
movq -0x258(%rbp), %rax
cmpl $0x3, 0x88(%rax)
jne 0x14829
movq -0x258(%rbp), %rax
cmpl $0x80, 0x4(%rax)
jbe 0x14829
movq -0x258(%rbp), %rax
testb $0x1, 0x979(%rax)
jne 0x14829
xorl %edi, %edi
leaq 0x2109d(%rip), %rsi     # 0x358bf
movb $0x0, %al
callq 0x47d0
movq -0x258(%rbp), %rax
testb $0x1, 0x979(%rax)
je 0x14859
movq -0x258(%rbp), %rax
cmpl $0xff, 0x4(%rax)
jbe 0x14859
xorl %edi, %edi
leaq 0x210d2(%rip), %rsi     # 0x35924
movb $0x0, %al
callq 0x47d0
addq $0x290, %rsp            # imm = 0x290
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x4550
nopl (%rax,%rax)