// File: /workspace/github_cpp_makefile_star2_O3/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 0x18ee
movq %r14, %rdi
callq 0x18ee
xorl %r14d, %r14d
leaq 0xa7d(%rip), %r15       # 0x2079
movq 0x29cd(%rip), %r13      # 0x3fd0
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 $0x1, %ecx
cmpl $0x68, %eax
je 0x161a
cmpl $0x6f, %eax
jg 0x166e
cmpl $-0x1, %eax
je 0x16c2
cmpl $0x64, %eax
jne 0x1842
movq (%r13), %rsi
testq %rsi, %rsi
je 0x1869
leaq 0x30(%rsp), %rdi
callq 0x10b0
movl $0x1, %eax
movq %rax, 0x8(%rsp)
jmp 0x1617
cmpl $0x70, %eax
je 0x16a1
cmpl $0x77, %eax
jne 0x1845
movq (%r13), %rsi
testq %rsi, %rsi
je 0x1857
leaq 0x30(%rsp), %rdi
callq 0x10b0
movl $0x1, %eax
movq %rax, (%rsp)
jmp 0x1617
movq (%r13), %rdi
testq %rdi, %rdi
je 0x1860
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movq %rax, %r14
decq %r14
jmp 0x1617
testl %r12d, %r12d
setne %al
cmpl $0x1, %ebp
sete %cl
orb %al, %cl
jne 0x181c
movq 0x2903(%rip), %rax      # 0x3fe0
movslq (%rax), %rax
cmpl %ebp, %eax
jge 0x1872
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 0x8(%rsp), %rcx
movl %eax, %ebp
testl %eax, %eax
js 0x187b
movq (%rsp), %rdx
leal (%rdx,%rcx), %eax
cmpl $0x2, %eax
jb 0x1737
leaq 0xe26(%rip), %rdi       # 0x2553
callq 0x1130
jmp 0x181c
movl %edx, %eax
orl %ecx, %eax
je 0x181c
movslq %r14d, %rax
leaq 0xe9b(%rip), %rcx       # 0x25e6
movzwl (%rcx,%rax,2), %r14d
movl %ebp, %edi
movl %r14d, %esi
callq 0x124c
movq 0x8(%rsp), %r12
testl %eax, %eax
jne 0x1889
leaq 0x30(%rsp), %rdi
movl $0x42, %esi
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x1090
movl %eax, %r15d
testl %eax, %eax
js 0x189c
cmpl $0x0, (%rsp)
je 0x17d4
leaq 0x17(%rsp), %rsi
movl $0x9, %edx
movl %r15d, %edi
callq 0x1120
cmpq $0x9, %rax
jne 0x18a5
leaq 0x17(%rsp), %rcx
movl %ebp, %edi
movl %r14d, %esi
movl $0x9000, %edx           # imm = 0x9000
movl $0x9, %r8d
callq 0x13da
testl %eax, %eax
je 0x1824
leaq 0xd2b(%rip), %rdi       # 0x24fa
jmp 0x1882
testl %r12d, %r12d
je 0x181c
leaq 0x17(%rsp), %rcx
movl %ebp, %edi
movl %r14d, %esi
movl $0x9000, %edx           # imm = 0x9000
movl $0x9, %r8d
callq 0x1452
testl %eax, %eax
jne 0x18ae
leaq 0x17(%rsp), %rsi
movl $0x9, %edx
movl %r15d, %edi
callq 0x1060
cmpq $0x9, %rax
je 0x1824
leaq 0xc88(%rip), %rdi       # 0x24a2
jmp 0x1882
movq (%rbx), %rdi
callq 0x14eb
leaq 0x20(%rsp), %rdi
callq 0x18cc
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 0x14eb
movl $0x1, %edi
callq 0x10d0
leaq 0xd6a(%rip), %rdi       # 0x25c8
jmp 0x1882
leaq 0xd27(%rip), %rdi       # 0x258e
jmp 0x1882
leaq 0xd3b(%rip), %rdi       # 0x25ab
jmp 0x1882
leaq 0xc14(%rip), %rdi       # 0x248d
jmp 0x1882
leaq 0xcf3(%rip), %rdi       # 0x2575
callq 0x1130
jmp 0x184d
leaq 0x7f1(%rip), %rdi       # 0x2081
movl %r14d, %esi
xorl %eax, %eax
callq 0x1030
jmp 0x184d
leaq 0xc92(%rip), %rdi       # 0x2535
jmp 0x1882
leaq 0xc67(%rip), %rdi       # 0x2513
jmp 0x1882
leaq 0xc17(%rip), %rdi       # 0x24cc
jmp 0x1882
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x18cc
movq %rbx, %rdi
callq 0x1140