// File: /workspace/github_c_makefile_star2_O0/eridanhq[P]miraclelib/miraclelib.c
// Function: sys_on_tx
void sys_on_tx(void *mapped_base)
{
	int pwr_supp_reg;
	
	pwr_supp_reg = read_reg_tx(36,mapped_base); // Check state of power supplies
	//printf("pwr_supp_reg = %d\n", pwr_supp_reg);
	if (pwr_supp_reg == 1 || pwr_supp_reg == 5) // Acceptable power states
	{
		printf("Turning ON system...");
		write_reg_tx(36,5,mapped_base); // ensure 5V supply and filter is on
		usleep(100000); // 100ms
		//printf("pwr_supp_reg = %d\n", pwr_supp_reg);
		write_reg_tx(36,7,mapped_base); // Turn on 25V supply
		usleep(100000); // 100ms
		//printf("pwr_supp_reg = %d\n", pwr_supp_reg);
		write_reg_tx(36,15,mapped_base); // Turn on comparator
		usleep(100000); // 100ms
		//printf("pwr_supp_reg = %d\n", pwr_supp_reg);
		write_reg_tx(36,14,mapped_base); // Turn off filter
		//printf("pwr_supp_reg = %d\n", pwr_supp_reg);
		
		// Enabled AD9173 output
		write_reg_tx(32,0x1,mapped_base); // CS select bit
		write_reg_tx(33,0x000840,mapped_base); // Page DAC0 for TXEN control update.
		write_reg_tx(33,0x05960C,mapped_base); // SPI turn on TXENx feature
		printf("done.\n");
	}
	else if (pwr_supp_reg == 14)
	{
		printf("System is already on.\n");
	}
	else if (pwr_supp_reg == 0) // Cannot connect to interposer
	{
		printf("CANNOT CONNECT TO MIRACLE. CHECK FMC CONNECTION,\n");
		printf("MAKE SURE MIRACLE POWER IS ON, AND TRY AGAIN.\n");
	}
	else // Weird power states.  Ideally should never get to this state
	{
		printf("SYSTEM IN UNDEFINED STATE\n");
		printf("SAFELY TURNING OFF...");
		safe_off_tx(mapped_base);
		printf("done.\n");
		printf("PLEASE TRY AGAIN.\n");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rsi
movl $0x24, %edi
callq 0x1680
movl %eax, -0xc(%rbp)
cmpl $0x1, -0xc(%rbp)
je 0x18ad
cmpl $0x5, -0xc(%rbp)
jne 0x196e
leaq 0x1934(%rip), %rdi      # 0x31e8
movb $0x0, %al
callq 0x1090
movq -0x8(%rbp), %rdx
movl $0x24, %edi
movl $0x5, %esi
callq 0x1640
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x1120
movq -0x8(%rbp), %rdx
movl $0x24, %edi
movl $0x7, %esi
callq 0x1640
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x1120
movq -0x8(%rbp), %rdx
movl $0x24, %edi
movl $0xf, %esi
callq 0x1640
movl $0x186a0, %edi          # imm = 0x186A0
callq 0x1120
movq -0x8(%rbp), %rdx
movl $0x24, %edi
movl $0xe, %esi
callq 0x1640
movq -0x8(%rbp), %rdx
movl $0x20, %edi
movl $0x1, %esi
callq 0x1640
movq -0x8(%rbp), %rdx
movl $0x21, %edi
movl $0x840, %esi            # imm = 0x840
callq 0x1640
movq -0x8(%rbp), %rdx
movl $0x21, %edi
movl $0x5960c, %esi          # imm = 0x5960C
callq 0x1640
leaq 0x17d2(%rip), %rdi      # 0x3137
movb $0x0, %al
callq 0x1090
jmp 0x19ed
cmpl $0xe, -0xc(%rbp)
jne 0x1984
leaq 0x1882(%rip), %rdi      # 0x31fd
movb $0x0, %al
callq 0x1090
jmp 0x19eb
cmpl $0x0, -0xc(%rbp)
jne 0x19a8
leaq 0x17c5(%rip), %rdi      # 0x3156
movb $0x0, %al
callq 0x1090
leaq 0x17e9(%rip), %rdi      # 0x3188
movb $0x0, %al
callq 0x1090
jmp 0x19e9
leaq 0x1808(%rip), %rdi      # 0x31b7
movb $0x0, %al
callq 0x1090
leaq 0x1815(%rip), %rdi      # 0x31d2
movb $0x0, %al
callq 0x1090
movq -0x8(%rbp), %rdi
callq 0x16c0
leaq 0x1763(%rip), %rdi      # 0x3137
movb $0x0, %al
callq 0x1090
leaq 0x1832(%rip), %rdi      # 0x3214
movb $0x0, %al
callq 0x1090
jmp 0x19eb
jmp 0x19ed
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)