// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %esi, %r15d
testl %esi, %esi
sete %al
leal -0x14b(%r15), %ecx
cmpl $0xffffff2d, %ecx       # imm = 0xFFFFFF2D
setae %cl
orb %al, %cl
je 0x3a64
movl %edx, %r14d
testl %edx, %edx
sete %al
leal -0x14b(%r14), %ecx
cmpl $0xffffff2d, %ecx       # imm = 0xFFFFFF2D
setae %cl
orb %al, %cl
je 0x3a64
movl %edi, %ebx
leaq 0x6(%rsp), %r9
movb $0x20, (%r9)
pushq $0x6a
popq %rsi
pushq $0x10
popq %rdx
pushq $0x1
popq %rcx
movl %ecx, %r8d
callq 0x354f
pushq $-0x1
popq %rbp
testl %eax, %eax
je 0x3985
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
testl %r15d, %r15d
je 0x39ed
leaq 0x4743(%rip), %rdi      # 0x80d4
movl %r15d, %esi
xorl %eax, %eax
callq 0x3040
leaq 0x6(%rsp), %r9
movb $0x0, (%r9)
pushq $0x6a
popq %rsi
pushq $0x1
popq %rcx
movl %ebx, %edi
xorl %edx, %edx
movl %ecx, %r8d
callq 0x354f
testl %eax, %eax
jne 0x3978
imull $0x213, %r15d, %eax     # imm = 0x213
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
addb $-0x3c, %al
leaq 0x6(%rsp), %r9
movb %al, (%r9)
pushq $0x6a
popq %rsi
pushq $0x1
popq %rcx
movl %ebx, %edi
movl $0xd8, %edx
movl %ecx, %r8d
callq 0x354f
testl %eax, %eax
jne 0x3978
testl %r14d, %r14d
je 0x3a5d
leaq 0x46f0(%rip), %rdi      # 0x80e9
movl %r14d, %esi
xorl %eax, %eax
callq 0x3040
leaq 0x6(%rsp), %r9
movb $0x1, (%r9)
pushq $0x6a
popq %rsi
pushq $0x1
popq %rcx
movl %ebx, %edi
xorl %edx, %edx
movl %ecx, %r8d
callq 0x354f
testl %eax, %eax
jne 0x3978
imull $0x213, %r14d, %eax     # imm = 0x213
movl $0x3e8, %ecx            # imm = 0x3E8
xorl %edx, %edx
divl %ecx
addb $-0x3c, %al
leaq 0x6(%rsp), %r9
movb %al, (%r9)
pushq $0x6a
popq %rsi
pushq $0x1
popq %rcx
movl %ebx, %edi
movl $0xd8, %edx
movl %ecx, %r8d
callq 0x354f
testl %eax, %eax
jne 0x3978
xorl %ebp, %ebp
jmp 0x3978
leaq 0x5229(%rip), %rdi      # 0x8c94
callq 0x3310
pushq $0x1
popq %rdi
callq 0x3260