// File: SYZYGYfpga[P]brain-tools/src/smartvio-brain.cpp
// Function: readDNA(int, unsigned int*, unsigned int*)
int readDNA (int i2c_file, uint32_t *svio1, uint32_t *svio2)
{
	uint8_t i;
	int vmin;
	uint8_t dna_buf[64];

	for (i = 0; i < SVIO_NUM_PORTS; i++) {
		// Skip ports referring to the FPGA
		if (0x00 == svio.ports[i].i2c_addr) {
			continue;
		}

		if (i2cDetect(i2c_file, svio.ports[i].i2c_addr) != 0) {
			// Device is not present
			continue;
		}

		// Read the full DNA Header
		if (readMCU(i2c_file, svio.ports[i].i2c_addr, 0x8000, dna_buf,
		            SZG_DNA_HEADER_LENGTH_V1) != 0) {
			return -1;
		}

		if (szgParsePortDNA(i, &svio, dna_buf, SZG_DNA_HEADER_LENGTH_V1) != 0) {
			return -1;
		}

		if (svio.ports[i].attr & SZG_ATTR_LVDS) {
			switch (svio.ports[i].group) {
				case 0:
					svio.ports[0].ranges[0].min = 250;
					svio.ports[0].ranges[0].max = 250;
					break;
				case 1:
					svio.ports[2].ranges[0].min = 250;
					svio.ports[2].ranges[0].max = 250;
					break;
			}
		}
	}

	// Find a solution
	for (i = 0; i < SVIO_NUM_GROUPS; i++) {
		vmin = szgSolveSmartVIOGroup(svio.ports, svio.group_masks[i]);
		if (vmin > 0) {
			svio.svio_results[i] = vmin;
		}
	}

	*svio1 = svio.svio_results[0];
	*svio2 = svio.svio_results[1];

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movb $0x0, -0x19(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x6, %eax
jge 0x3b84
movzbl -0x19(%rbp), %eax
leaq 0x1c829(%rip), %rcx     # 0x20278
addq $0x18, %rcx
imulq $0x6c, %rax, %rax
addq %rax, %rcx
xorl %eax, %eax
cmpl (%rcx), %eax
jne 0x3a65
jmp 0x3b77
movl -0x8(%rbp), %edi
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
leaq 0x1c803(%rip), %rax     # 0x20278
addq $0x18, %rax
imulq $0x6c, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %esi
callq 0x35d0
cmpl $0x0, %eax
je 0x3a91
jmp 0x3b77
movl -0x8(%rbp), %edi
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
leaq 0x1c7d7(%rip), %rax     # 0x20278
addq $0x18, %rax
imulq $0x6c, %rcx, %rcx
addq %rcx, %rax
movl (%rax), %eax
leaq -0x60(%rbp), %rcx
movl $0x8000, %edx           # imm = 0x8000
movl $0x28, %r8d
movzwl %ax, %esi
callq 0x38d0
cmpl $0x0, %eax
je 0x3ad6
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bfa
movzbl -0x19(%rbp), %edi
leaq -0x60(%rbp), %rdx
leaq 0x1c793(%rip), %rsi     # 0x20278
movl $0x28, %ecx
callq 0x12e40
cmpl $0x0, %eax
je 0x3b00
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3bfa
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
leaq 0x1c76b(%rip), %rax     # 0x20278
addq $0x18, %rax
imulq $0x6c, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x3b75
movzbl -0x19(%rbp), %eax
imulq $0x6c, %rax, %rax
leaq 0x1c746(%rip), %rcx     # 0x20278
movl 0x20(%rax,%rcx), %eax
movl %eax, -0x64(%rbp)
testl %eax, %eax
je 0x3b49
jmp 0x3b3f
movl -0x64(%rbp), %eax
subl $0x1, %eax
je 0x3b5f
jmp 0x3b73
movl $0xfa, 0x1c761(%rip)    # 0x202b4
movl $0xfa, 0x1c75b(%rip)    # 0x202b8
jmp 0x3b73
movl $0xfa, 0x1c823(%rip)    # 0x2038c
movl $0xfa, 0x1c81d(%rip)    # 0x20390
jmp 0x3b75
jmp 0x3b77
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x3a37
movb $0x0, -0x19(%rbp)
movzbl -0x19(%rbp), %eax
cmpl $0x2, %eax
jge 0x3bdb
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
leaq 0x1c6da(%rip), %rax     # 0x20278
movl 0x10(%rax,%rcx,4), %esi
leaq 0x1c6cf(%rip), %rdi     # 0x20278
addq $0x18, %rdi
callq 0x13240
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x20(%rbp)
jle 0x3bcf
movl -0x20(%rbp), %edx
movzbl -0x19(%rbp), %eax
movl %eax, %ecx
leaq 0x1c6ad(%rip), %rax     # 0x20278
movl %edx, 0x8(%rax,%rcx,4)
jmp 0x3bd1
movb -0x19(%rbp), %al
addb $0x1, %al
movb %al, -0x19(%rbp)
jmp 0x3b88
movl 0x1c69f(%rip), %ecx     # 0x20280
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl 0x1c697(%rip), %ecx     # 0x20284
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)