// File: SYZYGYfpga[P]brain-tools/src/smartvio-brain.cpp
// Function: applyVIO(int, unsigned int, unsigned int)
int applyVIO (int i2c_file, uint32_t svio1, uint32_t svio2)
{
	uint8_t temp_data[2];
	
	// Bounds check to be sure that everything is good to go
	if ((svio1 != 0) && ((svio1 < 120) || (svio1 > 330))) {
		printf("Invalid SmartVIO solution\n");
		exit(EXIT_FAILURE);
	}
	if ((svio2 != 0) && ((svio2 < 120) || (svio2 > 330))) {
		printf("Invalid SmartVIO solution\n");
		exit(EXIT_FAILURE);
	}

	// Disable write protect on TPS65400
	temp_data[0] = 0x20;
	if (i2cWrite(i2c_file, 0x6a, 0x10, 1, 1, temp_data) != 0) {
		return -1;
	}

	// TPS65400 VREF = VOUT * 531 - 60
	if (svio1 != 0) {
		printf("Setting VIO1 to: %d\n", svio1);
		temp_data[0] = 0x0;
		if (i2cWrite(i2c_file, 0x6a, 0x00, 1, 1, temp_data) != 0) {
			return -1;
		}
		temp_data[0] = svio1 * 531 / 1000 - 60;
		if (i2cWrite(i2c_file, 0x6a, 0xd8, 1, 1, temp_data) != 0) {
			return -1;
		}
	}
	if (svio2 != 0) {
		printf("Setting VIO2 to: %d\n", svio2);
		temp_data[0] = 0x1;
		if (i2cWrite(i2c_file, 0x6a, 0x00, 1, 1, temp_data) != 0) {
			return -1;
		}
		temp_data[0] = svio2 * 531 / 1000 - 60;
		if (i2cWrite(i2c_file, 0x6a, 0xd8, 1, 1, temp_data) != 0) {
			return -1;
		}
	}

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
cmpl $0x0, -0xc(%rbp)
je 0x3c4e
cmpl $0x78, -0xc(%rbp)
jb 0x3c36
cmpl $0x14a, -0xc(%rbp)      # imm = 0x14A
jbe 0x3c4e
leaq 0x10723(%rip), %rdi     # 0x14360
movb $0x0, %al
callq 0x3050
movl $0x1, %edi
callq 0x3320
cmpl $0x0, -0x10(%rbp)
je 0x3c7b
cmpl $0x78, -0x10(%rbp)
jb 0x3c63
cmpl $0x14a, -0x10(%rbp)     # imm = 0x14A
jbe 0x3c7b
leaq 0x106f6(%rip), %rdi     # 0x14360
movb $0x0, %al
callq 0x3050
movl $0x1, %edi
callq 0x3320
movb $0x20, -0x12(%rbp)
movl -0x8(%rbp), %edi
leaq -0x12(%rbp), %r9
movl $0x6a, %esi
movl $0x10, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x3640
cmpl $0x0, %eax
je 0x3caf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3dd8
cmpl $0x0, -0xc(%rbp)
je 0x3d43
movl -0xc(%rbp), %esi
leaq 0x106b8(%rip), %rdi     # 0x1437b
movb $0x0, %al
callq 0x3050
movb $0x0, -0x12(%rbp)
movl -0x8(%rbp), %edi
leaq -0x12(%rbp), %r9
movl $0x6a, %esi
xorl %edx, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x3640
cmpl $0x0, %eax
je 0x3cfb
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3dd8
imull $0x213, -0xc(%rbp), %eax # imm = 0x213
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
subl $0x3c, %eax
movb %al, -0x12(%rbp)
movl -0x8(%rbp), %edi
leaq -0x12(%rbp), %r9
movl $0x6a, %esi
movl $0xd8, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x3640
cmpl $0x0, %eax
je 0x3d41
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3dd8
jmp 0x3d43
cmpl $0x0, -0x10(%rbp)
je 0x3dd1
movl -0x10(%rbp), %esi
leaq 0x10639(%rip), %rdi     # 0x14390
movb $0x0, %al
callq 0x3050
movb $0x1, -0x12(%rbp)
movl -0x8(%rbp), %edi
leaq -0x12(%rbp), %r9
movl $0x6a, %esi
xorl %edx, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x3640
cmpl $0x0, %eax
je 0x3d8c
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3dd8
imull $0x213, -0x10(%rbp), %eax # imm = 0x213
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
subl $0x3c, %eax
movb %al, -0x12(%rbp)
movl -0x8(%rbp), %edi
leaq -0x12(%rbp), %r9
movl $0x6a, %esi
movl $0xd8, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x3640
cmpl $0x0, %eax
je 0x3dcf
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x3dd8
jmp 0x3dd1
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)