// File: /workspace/github_cpp_makefile_star2_O1/SYZYGYfpga[P]brain-tools/src/sequencer-brain.cpp
// Function: main
int main (int argc, char *argv[])
{
	// Options flags
	int hflag = 0;
	int wflag = 0;
	int dflag = 0;
	char i2c_filename[200];
	char seq_filename[200];
	uint8_t seq_buf[9];
	int i2c_file;
	int seq_file;
	int periph_num = 0;
	int curr_opt;
	int err;
	json json_handler;
	uint16_t peripheral_address[] = {0x30, 0x31, 0x32, 0x33};

	// Parse args
	while ((curr_opt = getopt(argc, argv, "w:d:p:h")) != -1) {
		switch(curr_opt)
		{
			case 'w':
				wflag = 1;
				if (optarg){ 
					strcpy(seq_filename, optarg);
				} else {
					printf("No argument specified for -w\n");
					exit(EXIT_FAILURE);
				}
				break;
			case 'd':
				dflag = 1;
				if (optarg){ 
					strcpy(seq_filename, optarg);
				} else {
					printf("No argument specified for -d\n");
					exit(EXIT_FAILURE);
				}
				break;
			case 'p':
				if (optarg){ 
					periph_num = strtol(optarg, NULL, 0) - 1;
				} else {
					printf("No argument specified for -p\n");
					exit(EXIT_FAILURE);
				}
				break;
			case 'h':
				hflag = 1;
				break;
			case '?':
				printHelp(argv[0]);
				exit(EXIT_FAILURE);
			default:
				printHelp(argv[0]);
				exit(EXIT_FAILURE);
		}
	}

	if ((hflag == 1) || (argc == 1)) {
		printHelp(argv[0]);
		return 0;
	}

	// Extract i2c device
	if (optind < argc) {
		strcpy(i2c_filename, argv[optind]);
	} else {
		printf("I2C Device required.\n");
		exit(EXIT_FAILURE);
	}

	// Open I2C file
	i2c_file = open(i2c_filename, O_RDWR);
	if (i2c_file < 0) {
		printf("Error opening i2c device\n");
		exit(EXIT_FAILURE);
	}

	if ((hflag + wflag + dflag) > 1) {
		printf("Invalid set of options specified.\n");
		printHelp(argv[0]);
		return 0;
	}

	if ((wflag == 1) || (dflag == 1)) {
		if (i2cDetect(i2c_file, peripheral_address[periph_num]) != 0) {
			printf("Peripheral at %X not found\n", peripheral_address[periph_num]);
			exit(EXIT_FAILURE);
		}

		seq_file = open(seq_filename, O_RDWR | O_CREAT, 0666);
		if (seq_file < 0) {
			printf("Error opening sequencing file\n");
			exit(EXIT_FAILURE);
		}
	}

	if (wflag == 1) { // Write sequencer registers from a file to a peripheral
		if (read(seq_file, seq_buf, SEQ_LENGTH) != SEQ_LENGTH) {
			printf("Error reading from sequencer file\n");
			exit(EXIT_FAILURE);
		}

		if (writeMCU(i2c_file, peripheral_address[periph_num], 0x9000,
		             seq_buf, SEQ_LENGTH) != 0) {
			printf("Error writing to the MCU\n");
			exit(EXIT_FAILURE);
		}
	} else if (dflag == 1) { // Dump sequencer registers from a peripheral to a file
		err = dumpSeq(i2c_file, peripheral_address[periph_num], seq_buf);

		if (err < 0) {
			printf("Error reading sequencer registers from device\n");
			exit(EXIT_FAILURE);
		}
		
		if (write(seq_file, seq_buf, SEQ_LENGTH) != SEQ_LENGTH) {
			printf("Error writing to sequencer registers file\n");
			exit(EXIT_FAILURE);
		}
	} else {
		printHelp(argv[0]);
		return 0;
	}

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x20(%rsp), %r14
movb $0x0, (%r14)
movq $0x0, 0x8(%r14)
movq %r14, %rdi
callq 0x1902
movq %r14, %rdi
callq 0x1902
xorl %r14d, %r14d
leaq 0xa7a(%rip), %r15       # 0x2079
movq $0x0, 0x18(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %r12d, %ecx
movl %ecx, %r12d
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1040
movl $0x1, %ecx
cmpl $0x68, %eax
je 0x1611
cmpl $0x6f, %eax
jg 0x166b
cmpl $-0x1, %eax
je 0x16c8
cmpl $0x64, %eax
jne 0x1856
movq 0x2987(%rip), %rax      # 0x3fd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x187d
leaq 0x30(%rsp), %rdi
callq 0x10b0
movl $0x1, %eax
movq %rax, 0x18(%rsp)
jmp 0x160e
cmpl $0x70, %eax
je 0x16a1
cmpl $0x77, %eax
jne 0x1859
movq 0x2950(%rip), %rax      # 0x3fd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x186b
leaq 0x30(%rsp), %rdi
callq 0x10b0
movl $0x1, %r13d
jmp 0x160e
movq 0x2928(%rip), %rax      # 0x3fd0
movq (%rax), %rdi
testq %rdi, %rdi
je 0x1874
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq %rax, %r14
decl %r14d
jmp 0x160e
testl %r12d, %r12d
setne %al
cmpl $0x1, %ebp
sete %cl
orb %al, %cl
jne 0x1731
movq 0x2901(%rip), %rax      # 0x3fe0
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x1886
movq (%rbx,%rax,8), %rsi
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x10b0
movq %r15, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x1090
movq 0x18(%rsp), %r12
movl %eax, %ebp
testl %eax, %eax
js 0x188f
leal (%r12,%r13), %eax
cmpl $0x2, %eax
jb 0x1757
leaq 0xe27(%rip), %rdi       # 0x2553
callq 0x1130
movq (%rbx), %rdi
callq 0x14ee
leaq 0x20(%rsp), %rdi
callq 0x18e0
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %r13d, %eax
orl %r12d, %eax
je 0x17a1
movslq %r14d, %rax
leaq 0xe7d(%rip), %rcx       # 0x25e6
movzwl (%rcx,%rax,2), %r15d
movl %ebp, %edi
movl %r15d, %esi
callq 0x124c
testl %eax, %eax
jne 0x18a1
leaq 0x30(%rsp), %rdi
movl $0x42, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x1090
movl %eax, %r15d
testl %eax, %eax
js 0x18b4
testl %r13d, %r13d
je 0x17fb
leaq 0xf(%rsp), %rsi
movl $0x9, %edx
movl %r15d, %edi
callq 0x1120
cmpq $0x9, %rax
jne 0x1898
movslq %r14d, %rax
leaq 0xe1a(%rip), %rcx       # 0x25e6
movzwl (%rcx,%rax,2), %esi
leaq 0xf(%rsp), %rcx
movl %ebp, %edi
movl $0x9000, %edx           # imm = 0x9000
movl $0x9, %r8d
callq 0x13dd
testl %eax, %eax
je 0x1739
leaq 0xd04(%rip), %rdi       # 0x24fa
jmp 0x18c4
testl %r12d, %r12d
je 0x1731
movslq %r14d, %rax
leaq 0xdd8(%rip), %rcx       # 0x25e6
movzwl (%rcx,%rax,2), %esi
leaq 0xf(%rsp), %rcx
movl %ebp, %edi
movl $0x9000, %edx           # imm = 0x9000
movl $0x9, %r8d
callq 0x1455
testl %eax, %eax
jne 0x18bd
leaq 0xf(%rsp), %rsi
movl $0x9, %edx
movl %r15d, %edi
callq 0x1060
cmpq $0x9, %rax
je 0x1739
leaq 0xc4e(%rip), %rdi       # 0x24a2
jmp 0x18c4
cmpl $0x3f, %eax
movq (%rbx), %rdi
callq 0x14ee
movl $0x1, %edi
callq 0x10d0
leaq 0xd56(%rip), %rdi       # 0x25c8
jmp 0x18c4
leaq 0xd13(%rip), %rdi       # 0x258e
jmp 0x18c4
leaq 0xd27(%rip), %rdi       # 0x25ab
jmp 0x18c4
leaq 0xc00(%rip), %rdi       # 0x248d
jmp 0x18c4
leaq 0xcdf(%rip), %rdi       # 0x2575
jmp 0x18c4
leaq 0xc74(%rip), %rdi       # 0x2513
jmp 0x18c4
leaq 0x7d9(%rip), %rdi       # 0x2081
movl %r15d, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x1861
leaq 0xc7a(%rip), %rdi       # 0x2535
jmp 0x18c4
leaq 0xc08(%rip), %rdi       # 0x24cc
callq 0x1130
jmp 0x1861
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x18e0
movq %rbx, %rdi
callq 0x1140