// File: /workspace/github_cpp_makefile_star2_O2/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
xorl %r14d, %r14d
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x18d4
leaq 0xa7f(%rip), %r15       # 0x2099
pushq $0x1
popq %r13
movq $0x0, 0x8(%rsp)
movq $0x0, (%rsp)
xorl %r12d, %r12d
movl %r12d, %ecx
movl %ecx, %r12d
movl %ebp, %edi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1040
movl %r13d, %ecx
cmpl $0x68, %eax
je 0x1635
cmpl $-0x1, %eax
je 0x16e0
cmpl $0x64, %eax
je 0x16b6
cmpl $0x70, %eax
je 0x168f
cmpl $0x77, %eax
jne 0x184b
movq 0x3960(%rip), %rax      # 0x4fd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x185e
leaq 0x30(%rsp), %rdi
callq 0x10f0
movl %r13d, %eax
movq %rax, (%rsp)
jmp 0x1632
movq 0x393a(%rip), %rax      # 0x4fd0
movq (%rax), %rdi
testq %rdi, %rdi
je 0x1867
xorl %esi, %esi
xorl %edx, %edx
callq 0x1150
movq %rax, %r14
decq %r14
jmp 0x1632
movq 0x3913(%rip), %rax      # 0x4fd0
movq (%rax), %rsi
testq %rsi, %rsi
je 0x1870
leaq 0x30(%rsp), %rdi
callq 0x10f0
movl %r13d, %eax
movq %rax, 0x8(%rsp)
jmp 0x1632
testl %r12d, %r12d
setne %al
cmpl $0x1, %ebp
sete %cl
orb %al, %cl
jne 0x1825
movq 0x38e5(%rip), %rax      # 0x4fe0
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x1879
movq (%rbx,%rax,8), %rsi
leaq 0x100(%rsp), %r15
movq %r15, %rdi
callq 0x10f0
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x10c0
movq 0x8(%rsp), %rcx
movl %eax, %ebp
testl %eax, %eax
js 0x1882
movq (%rsp), %rdx
leal (%rdx,%rcx), %eax
cmpl $0x2, %eax
jb 0x1753
leaq 0xfa6(%rip), %rdi       # 0x26ef
callq 0x1180
jmp 0x1825
movl %edx, %eax
orl %ecx, %eax
je 0x1825
movslq %r14d, %rax
leaq 0x101b(%rip), %rcx      # 0x2782
movzwl (%rcx,%rax,2), %r14d
movl %ebp, %edi
movl %r14d, %esi
callq 0x129c
movq 0x8(%rsp), %r12
testl %eax, %eax
jne 0x1890
leaq 0x30(%rsp), %rdi
pushq $0x42
popq %rsi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x10c0
movl %eax, %r15d
testl %eax, %eax
js 0x18a3
cmpl $0x0, (%rsp)
je 0x17ea
leaq 0x17(%rsp), %rsi
pushq $0x9
popq %rdx
movl %r15d, %edi
callq 0x1170
cmpq $0x9, %rax
jne 0x18ac
leaq 0x17(%rsp), %rcx
pushq $0x9
popq %r8
movl %ebp, %edi
movl %r14d, %esi
movl $0x9000, %edx           # imm = 0x9000
callq 0x141f
testl %eax, %eax
je 0x182d
leaq 0xeb1(%rip), %rdi       # 0x2696
jmp 0x1889
testl %r12d, %r12d
je 0x1825
leaq 0x17(%rsp), %rdx
movl %ebp, %edi
movl %r14d, %esi
callq 0x14fb
testl %eax, %eax
js 0x18b5
leaq 0x17(%rsp), %rsi
pushq $0x9
popq %rdx
movl %r15d, %edi
callq 0x1060
cmpq $0x9, %rax
je 0x182d
leaq 0xe1b(%rip), %rdi       # 0x263e
jmp 0x1889
movq (%rbx), %rdi
callq 0x151a
leaq 0x20(%rsp), %rdi
callq 0x18f0
xorl %eax, %eax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x3f, %eax
movq (%rbx), %rdi
callq 0x151a
pushq $0x1
popq %rdi
callq 0x1120
leaq 0xeff(%rip), %rdi       # 0x2764
jmp 0x1889
leaq 0xebc(%rip), %rdi       # 0x272a
jmp 0x1889
leaq 0xed0(%rip), %rdi       # 0x2747
jmp 0x1889
leaq 0xda9(%rip), %rdi       # 0x2629
jmp 0x1889
leaq 0xe88(%rip), %rdi       # 0x2711
callq 0x1180
jmp 0x1856
leaq 0x80a(%rip), %rdi       # 0x20a1
movl %r14d, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x1856
leaq 0xe27(%rip), %rdi       # 0x26d1
jmp 0x1889
leaq 0xdfc(%rip), %rdi       # 0x26af
jmp 0x1889
leaq 0xdac(%rip), %rdi       # 0x2668
jmp 0x1889
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x18f0
movq %rbx, %rdi
callq 0x1190
nop