// File: /workspace/github_cpp_makefile_star2_O1/stacksmith[P]picoasm/main.cpp
// Function: main
int main(int argc, char **argv)
{

  string strSrcFile;
  string strTplFile;
  string strOutputDir;
  string strEntityName;

  string strTemp;
  int iCompareLen;
  const string strVerilogExt = LA_PICOASM_VERILOG_EXT;

  const char optstring[] = "i:t:d:m:";
  bool bOptErr = false;
  bool bVHDL = true;
  int optch;

  char *cpTemp;
  char *cpBase;
  char *cpExt;

  bool bRet; 
  int iRet = 0;

  while ((optch = getopt(argc, argv, optstring)) != -1){
    switch (optch)
      {
      case 'i': // input source file
	strSrcFile = optarg;
	break;

      case 't': // input template file
	strTplFile = optarg;
	break;

      case 'm': // entity or module name
	strEntityName = optarg;
	break;

      case 'd': // output directory
	strOutputDir = optarg;
	break;

      default:
	cout << "ERR: Unknown command line option" << endl; 
	bOptErr = true;
	break;

      } // switch
  } // while

  // check cmd line options
  if (strSrcFile.empty()){
    cout << "ERR: Input source file missing." << endl; 
    usage(basename(argv[0]));
    return (-1);
  }
  
  if (bOptErr){
    usage(basename(argv[0]));
    return (-1);
  }

  // build optional parms if needed
  if (strOutputDir.empty()){
    cpTemp = strdup(strSrcFile.c_str());
    strOutputDir = dirname(cpTemp);
    free(cpTemp);
  }
  
  if (strEntityName.empty()){
    // build entity name from source file name with no path or extension 
    cpTemp = strdup(strSrcFile.c_str());
    cpBase = basename(cpTemp);
    cpExt = strrchr(cpBase, '.');
    if (cpExt != NULL){
      *cpExt = '\0';
    }
    strEntityName = cpBase;
    free(cpTemp);
  }

  if (strTplFile.empty()){
    // build template file from source file dir + default name
    cpTemp = strdup(strSrcFile.c_str());
    cpBase = dirname(cpTemp);
    strTplFile = cpBase;
    strTplFile += "/";
    strTplFile += LA_PICOASM_DEF_TPL;
    free(cpTemp);
  }

  // determine VHDL or verilog by looking for verilog file extension
  // on template file.
  iCompareLen = strVerilogExt.size();
  if ((strTplFile.size()) > iCompareLen){
    strTemp = strTplFile.substr(strTplFile.size() - iCompareLen, iCompareLen); 
    if (strcasecmp(strTemp.c_str(), strVerilogExt.c_str()) == 0){
      bVHDL = false;   // its verilog
    }
  }

  // debug - show parms
//   cout << "DEBUG: In=" << strSrcFile << endl <<
//     "    OutDir=" << strOutputDir << endl <<
//     "    Entity=" << strEntityName << endl <<
//     "    TemplateFile=" << strTplFile << endl <<
//     "    VHDL=" << bVHDL << endl;  

  m_picoBlaze = new CPicoBlaze();
  m_assembler = new CAssembler();

  m_assembler->setCode(m_picoBlaze->code);
  m_assembler->setFilename(strSrcFile);

  if (m_assembler->assemble() == true){
    // m_picoBlaze->code->Print();
    printListing(strSrcFile);
    if (m_assembler->exportVHDL(strTplFile, 
				strOutputDir, 
				strEntityName,
				bVHDL) == true){
      if (bVHDL){
	cout << "Generated VHDL entity file " 
	     << strOutputDir << "/" << strEntityName 
	     << LA_PICOASM_VHDL_EXT << endl;
      } else {
	cout << "Generated verilog module file " 
	     << strOutputDir << "/" << strEntityName 
	     << LA_PICOASM_VERILOG_EXT << endl;
      }
    } else {
      if (bVHDL){
	cout << "ERR: VHDL entity file not generated" << endl;
      } else {
	cout << "ERR: Verilog module file not generated" << endl;
      }
      iRet = -1;
    }
  } else {
    iRet = -1;
  }

  delete m_assembler;
  delete m_picoBlaze;

  return(iRet);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %rbx
movl %edi, %r14d
leaq -0x98(%rbp), %rcx
movq %rcx, -0x10(%rcx)
xorl %eax, %eax
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x78(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x58(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x38(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0x100(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
leaq -0xc0(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x17c9bc(%rip), %rsi    # 0x5810ee
leaq 0x17c9b7(%rip), %rdx    # 0x5810f0
leaq -0xd0(%rbp), %rdi
callq 0x405508
movabsq $0x3a6d3a643a743a69, %rax # imm = 0x3A6D3A643A743A69
leaq -0x120(%rbp), %rcx
movq %rax, (%rcx)
movb $0x0, 0x8(%rcx)
movq $0x0, -0xb0(%rbp)
movl %r14d, %edi
movq %rbx, %rsi
leaq -0x120(%rbp), %rdx
callq 0x542da0
cmpl $0x6c, %eax
jg 0x404792
cmpl $0x64, %eax
je 0x4047a5
cmpl $0x69, %eax
jne 0x4047da
leaq -0xa8(%rbp), %r13
jmp 0x4047af
cmpl $0x6d, %eax
je 0x4047ab
leaq -0x88(%rbp), %r13
cmpl $0x74, %eax
je 0x4047af
jmp 0x4047df
leaq -0x68(%rbp), %r13
jmp 0x4047af
leaq -0x48(%rbp), %r13
movq $0x5fb760, %rax         # imm = 0x5FB760
movq (%rax), %r12
movq 0x8(%r13), %r15
movq %r12, %rdi
callq 0x401118
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r12, %rcx
movq %rax, %r8
callq 0x47d700
jmp 0x404768
cmpl $-0x1, %eax
je 0x40482f
movl $0x20, %edx
movq $0x5f4580, %r15         # imm = 0x5F4580
movq %r15, %rdi
leaq 0x17c905(%rip), %rsi    # 0x5810fa
callq 0x4761b0
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq %r15, %rdi
callq 0x475cb0
movb $0x1, %cl
movq %rcx, -0xb0(%rbp)
movq %rax, %rdi
callq 0x475280
jmp 0x404768
cmpq $0x0, -0xa0(%rbp)
je 0x404892
testb $0x1, -0xb0(%rbp)
je 0x40492d
movq (%rbx), %rdi
callq 0x4d2a40
leaq -0x1e0(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
movq %rax, %rsi
callq 0x405430
leaq -0x1e0(%rbp), %rbx
movq %rbx, %rdi
callq 0x404648
movq (%rbx), %rdi
leaq -0x1d0(%rbp), %rax
cmpq %rax, %rdi
je 0x404923
movq -0x1d0(%rbp), %rsi
jmp 0x40491b
movq $0x5f4580, %r14         # imm = 0x5F4580
leaq 0x17c87b(%rip), %rsi    # 0x58111b
movl $0x1f, %edx
movq %r14, %rdi
callq 0x4761b0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x475cb0
movq %rax, %rdi
callq 0x475280
movq (%rbx), %rdi
callq 0x4d2a40
leaq -0x200(%rbp), %rdi
leaq -0x1c0(%rbp), %rdx
movq %rax, %rsi
callq 0x405430
leaq -0x200(%rbp), %rbx
movq %rbx, %rdi
callq 0x404648
movq (%rbx), %rdi
leaq -0x1f0(%rbp), %rax
cmpq %rax, %rdi
je 0x404923
movq -0x1f0(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x404e86
cmpq $0x0, -0x60(%rbp)
jne 0x404979
movq -0xa8(%rbp), %rdi
callq 0x4f7800
movq %rax, %rbx
movq %rax, %rdi
callq 0x545b80
movq %rax, %r14
leaq -0x68(%rbp), %r15
movq 0x8(%r15), %r12
movq %rax, %rdi
callq 0x401118
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x47d700
movq %rbx, %rdi
callq 0x4f54d0
cmpq $0x0, -0x40(%rbp)
jne 0x4049da
movq -0xa8(%rbp), %rdi
callq 0x4f7800
movq %rax, %rbx
movq %rax, %rdi
callq 0x4d2a40
movq %rax, %r14
movq %rax, %rdi
movl $0x2e, %esi
callq 0x401048
testq %rax, %rax
je 0x4049af
movb $0x0, (%rax)
leaq -0x48(%rbp), %r15
movq 0x8(%r15), %r12
movq %r14, %rdi
callq 0x401118
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x47d700
movq %rbx, %rdi
callq 0x4f54d0
cmpq $0x0, -0x80(%rbp)
jne 0x404a4f
movq -0xa8(%rbp), %rdi
callq 0x4f7800
movq %rax, %rbx
movq %rax, %rdi
callq 0x545b80
movq %rax, %r14
leaq -0x88(%rbp), %r15
movq 0x8(%r15), %r12
movq %rax, %rdi
callq 0x401118
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x47d700
leaq 0x183774(%rip), %rsi    # 0x58819c
leaq -0x88(%rbp), %rdi
callq 0x47e260
leaq 0x17c6ab(%rip), %rsi    # 0x5810e6
leaq -0x88(%rbp), %rdi
callq 0x47e260
movq %rbx, %rdi
callq 0x4f54d0
movslq -0xc8(%rbp), %rcx
movq -0x80(%rbp), %rdx
movb $0x1, %r15b
subq %rcx, %rdx
jbe 0x404ac2
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x47f2e0
leaq -0x110(%rbp), %rdi
leaq -0x1c0(%rbp), %rbx
movq %rbx, %rsi
callq 0x47cb00
movq (%rbx), %rdi
leaq -0x1b0(%rbp), %rax
cmpq %rax, %rdi
je 0x404aa9
movq -0x1b0(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x110(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x4010d8
testl %eax, %eax
setne %r15b
movl $0x40, %edi
callq 0x40df60
movq %rax, %r14
movq %rax, %rdi
callq 0x40c746
movq %r14, 0x1eeb7a(%rip)    # 0x5f3658
movl $0x88, %edi
callq 0x40df60
movq %rax, %r14
movq %rax, %rdi
callq 0x40563a
movq %r14, 0x1eeb66(%rip)    # 0x5f3660
movq 0x1eeb57(%rip), %rax    # 0x5f3658
movq 0x38(%rax), %rax
movq %rax, 0x80(%r14)
leaq -0x190(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x1a0(%rbp), %rdi
callq 0x40546e
addq $0x60, %r14
leaq -0x1a0(%rbp), %rsi
movq %r14, %rdi
callq 0x47c6e0
movq -0x1a0(%rbp), %rdi
cmpq %r12, %rdi
je 0x404b63
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq 0x1eeaf6(%rip), %rdi    # 0x5f3660
callq 0x408a18
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x404e44
leaq -0x170(%rbp), %r14
movq %r14, -0x10(%r14)
movq -0xa8(%rbp), %rsi
movq -0xa0(%rbp), %rdx
addq %rsi, %rdx
leaq -0x180(%rbp), %rdi
callq 0x40546e
leaq -0x180(%rbp), %rdi
callq 0x4050ca
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x404bcb
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq 0x1eea8e(%rip), %r14    # 0x5f3660
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
movq -0x88(%rbp), %rsi
movq -0x80(%rbp), %rdx
addq %rsi, %rdx
leaq -0xf0(%rbp), %rdi
callq 0x40546e
leaq -0x150(%rbp), %r13
movq %r13, -0x10(%r13)
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
addq %rsi, %rdx
leaq -0x160(%rbp), %rdi
callq 0x40546e
leaq -0x130(%rbp), %r12
movq %r12, -0x10(%r12)
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
addq %rsi, %rdx
leaq -0x140(%rbp), %rdi
callq 0x40546e
movzbl %r15b, %r8d
leaq -0xf0(%rbp), %rsi
leaq -0x160(%rbp), %rdx
leaq -0x140(%rbp), %rcx
movq %r14, %rdi
callq 0x407d66
movl %eax, %r14d
movq -0x140(%rbp), %rdi
cmpq %r12, %rdi
je 0x404c7b
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x160(%rbp), %rdi
cmpq %r13, %rdi
leaq -0xe0(%rbp), %r12
je 0x404c9d
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0xf0(%rbp), %rdi
cmpq %r12, %rdi
je 0x404cb8
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x40d0c0
testb %r14b, %r14b
je 0x404d4d
testb %r15b, %r15b
je 0x404db1
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17c463(%rip), %rsi    # 0x58113b
movl $0x1b, %edx
callq 0x4761b0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x4761b0
movq %rax, %rbx
leaq 0x18349c(%rip), %rsi    # 0x58819c
movl $0x1, %edx
movq %rax, %rdi
callq 0x4761b0
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %rbx, %rdi
callq 0x4761b0
movq %rax, %rbx
leaq 0x17c430(%rip), %rsi    # 0x581157
movl $0x4, %edx
movq %rax, %rdi
callq 0x4761b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457300
jmp 0x404e2f
leaq 0x17c427(%rip), %rax    # 0x58117b
leaq 0x17c444(%rip), %rsi    # 0x58119f
testb %r15b, %r15b
cmovneq %rax, %rsi
xorb $0x1, %r15b
movzbl %r15b, %eax
leaq (%rax,%rax,2), %rdx
addq $0x23, %rdx
movq $0x5f4580, %r14         # imm = 0x5F4580
movq %r14, %rdi
callq 0x4761b0
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x475cb0
movq %rax, %rdi
callq 0x475280
jmp 0x404e44
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17c39d(%rip), %rsi    # 0x58115c
movl $0x1e, %edx
callq 0x4761b0
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x4761b0
movq %rax, %rbx
leaq 0x1833b5(%rip), %rsi    # 0x58819c
movl $0x1, %edx
movq %rax, %rdi
callq 0x4761b0
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %rbx, %rdi
callq 0x4761b0
movq %rax, %rbx
leaq 0x17c2e0(%rip), %rsi    # 0x5810ee
movl $0x2, %edx
movq %rax, %rdi
callq 0x4761b0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457300
movsbl %al, %esi
movq %rbx, %rdi
callq 0x475cb0
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x475280
movq 0x1ee815(%rip), %r14    # 0x5f3660
testq %r14, %r14
je 0x404e65
movq %r14, %rdi
callq 0x40568e
movl $0x88, %esi
movq %r14, %rdi
callq 0x40d0c0
movq 0x1ee7ec(%rip), %r14    # 0x5f3658
testq %r14, %r14
je 0x404e86
movq %r14, %rdi
callq 0x40c7fa
movl $0x40, %esi
movq %r14, %rdi
callq 0x40d0c0
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x404ea8
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x404eca
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x404ee3
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x404efc
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x404f18
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x404f3a
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movl %ebx, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x140(%rbp), %rdi
cmpq %r12, %rdi
je 0x404f71
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x40d0c0
jmp 0x404f71
movq %rax, %rbx
movq -0x160(%rbp), %rdi
cmpq %r13, %rdi
je 0x404f91
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x40d0c0
jmp 0x404f91
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x40500e
movq -0xe0(%rbp), %rsi
jmp 0x404fdd
movq %rax, %rbx
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x40500e
movq -0x170(%rbp), %rsi
jmp 0x404fdd
jmp 0x40500b
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
cmpq %r12, %rdi
je 0x40500e
movq -0x190(%rbp), %rsi
incq %rsi
jmp 0x404ff7
movq %rax, %rbx
movl $0x88, %esi
jmp 0x404ff4
movq %rax, %rbx
movl $0x40, %esi
movq %r14, %rdi
callq 0x40d0c0
jmp 0x40500e
jmp 0x40500b
jmp 0x40500b
movq %rax, %rbx
jmp 0x405030
jmp 0x40500b
jmp 0x40500b
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x405030
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x405052
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x40506b
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x405084
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x4050a0
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x4050c2
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x40d0c0
movq %rbx, %rdi
callq 0x4ad960