// File: /workspace/github_cpp_makefile_star2_O3/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 0x40550c
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 0x542d20
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 0x47d660
jmp 0x404768
cmpl $-0x1, %eax
je 0x40482f
movl $0x20, %edx
movq $0x5f4580, %r15         # imm = 0x5F4580
movq %r15, %rdi
leaq 0x17c905(%rip), %rsi    # 0x5810fa
callq 0x476110
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq %r15, %rdi
callq 0x475c10
movb $0x1, %cl
movq %rcx, -0xb0(%rbp)
movq %rax, %rdi
callq 0x4751e0
jmp 0x404768
cmpq $0x0, -0xa0(%rbp)
je 0x404892
testb $0x1, -0xb0(%rbp)
je 0x40492d
movq (%rbx), %rdi
callq 0x4d29a0
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 0x476110
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x475c10
movq %rax, %rdi
callq 0x4751e0
movq (%rbx), %rdi
callq 0x4d29a0
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 0x40d020
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
jmp 0x404e88
cmpq $0x0, -0x60(%rbp)
jne 0x404979
movq -0xa8(%rbp), %rdi
callq 0x4f7760
movq %rax, %rbx
movq %rax, %rdi
callq 0x545b00
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 0x47d660
movq %rbx, %rdi
callq 0x4f5430
cmpq $0x0, -0x40(%rbp)
jne 0x4049da
movq -0xa8(%rbp), %rdi
callq 0x4f7760
movq %rax, %rbx
movq %rax, %rdi
callq 0x4d29a0
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 0x47d660
movq %rbx, %rdi
callq 0x4f5430
movq -0x80(%rbp), %rdx
testq %rdx, %rdx
jne 0x404a55
movq -0xa8(%rbp), %rdi
callq 0x4f7760
movq %rax, %rbx
movq %rax, %rdi
callq 0x545b00
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 0x47d660
leaq 0x183772(%rip), %rsi    # 0x58819c
leaq -0x88(%rbp), %rdi
callq 0x47e1c0
leaq 0x17c6a9(%rip), %rsi    # 0x5810e6
leaq -0x88(%rbp), %rdi
callq 0x47e1c0
movq %rbx, %rdi
callq 0x4f5430
movq -0x80(%rbp), %rdx
movslq -0xc8(%rbp), %rcx
movb $0x1, %r15b
subq %rcx, %rdx
jbe 0x404ac4
leaq -0x1c0(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x47f240
leaq -0x110(%rbp), %rdi
leaq -0x1c0(%rbp), %rbx
movq %rbx, %rsi
callq 0x47ca60
movq (%rbx), %rdi
leaq -0x1b0(%rbp), %rax
cmpq %rax, %rdi
je 0x404aab
movq -0x1b0(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x110(%rbp), %rdi
movq -0xd0(%rbp), %rsi
callq 0x4010d8
testl %eax, %eax
setne %r15b
movl $0x40, %edi
callq 0x40dec0
movq %rax, %r14
movq %rax, %rdi
callq 0x40c6d4
movq %r14, 0x1eeb78(%rip)    # 0x5f3658
movl $0x88, %edi
callq 0x40dec0
movq %rax, %r14
movq %rax, %rdi
callq 0x405642
movq %r14, 0x1eeb64(%rip)    # 0x5f3660
movq 0x1eeb55(%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 0x47c640
movq -0x1a0(%rbp), %rdi
cmpq %r12, %rdi
je 0x404b65
movq -0x190(%rbp), %rsi
incq %rsi
callq 0x40d020
movq 0x1eeaf4(%rip), %rdi    # 0x5f3660
callq 0x4089ea
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testb %al, %al
je 0x404e46
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 0x4050cc
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x404bcd
movq -0x170(%rbp), %rsi
incq %rsi
callq 0x40d020
movq 0x1eea8c(%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 0x407d2e
movl %eax, %r14d
movq -0x140(%rbp), %rdi
cmpq %r12, %rdi
je 0x404c7d
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x160(%rbp), %rdi
cmpq %r13, %rdi
leaq -0xe0(%rbp), %r12
je 0x404c9f
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0xf0(%rbp), %rdi
cmpq %r12, %rdi
je 0x404cba
movq -0xe0(%rbp), %rsi
incq %rsi
callq 0x40d020
testb %r14b, %r14b
je 0x404d4f
testb %r15b, %r15b
je 0x404db3
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17c461(%rip), %rsi    # 0x58113b
movl $0x1b, %edx
callq 0x476110
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x476110
movq %rax, %rbx
leaq 0x18349a(%rip), %rsi    # 0x58819c
movl $0x1, %edx
movq %rax, %rdi
callq 0x476110
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %rbx, %rdi
callq 0x476110
movq %rax, %rbx
leaq 0x17c42e(%rip), %rsi    # 0x581157
movl $0x4, %edx
movq %rax, %rdi
callq 0x476110
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457260
jmp 0x404e31
leaq 0x17c425(%rip), %rax    # 0x58117b
leaq 0x17c442(%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 0x476110
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x475c10
movq %rax, %rdi
callq 0x4751e0
jmp 0x404e46
movq $0x5f4580, %rdi         # imm = 0x5F4580
leaq 0x17c39b(%rip), %rsi    # 0x58115c
movl $0x1e, %edx
callq 0x476110
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
movq $0x5f4580, %rdi         # imm = 0x5F4580
callq 0x476110
movq %rax, %rbx
leaq 0x1833b3(%rip), %rsi    # 0x58819c
movl $0x1, %edx
movq %rax, %rdi
callq 0x476110
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq %rbx, %rdi
callq 0x476110
movq %rax, %rbx
leaq 0x17c2de(%rip), %rsi    # 0x5810ee
movl $0x2, %edx
movq %rax, %rdi
callq 0x476110
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x457260
movsbl %al, %esi
movq %rbx, %rdi
callq 0x475c10
xorl %ebx, %ebx
movq %rax, %rdi
callq 0x4751e0
movq 0x1ee813(%rip), %r14    # 0x5f3660
testq %r14, %r14
je 0x404e5a
movq %r14, %rdi
callq 0x405696
movl $0x88, %esi
movq %r14, %rdi
callq 0x40d020
movq 0x1ee7ea(%rip), %r14    # 0x5f3658
testq %r14, %r14
je 0x404e7b
movq %r14, %rdi
callq 0x40c788
movl $0x40, %esi
movq %r14, %rdi
callq 0x40d020
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x404eaa
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x404ecc
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x404ee5
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x404efe
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x404f1a
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x404f3c
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x40d020
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 0x404f73
movq -0x130(%rbp), %rsi
incq %rsi
callq 0x40d020
jmp 0x404f73
movq %rax, %rbx
movq -0x160(%rbp), %rdi
cmpq %r13, %rdi
je 0x404f93
movq -0x150(%rbp), %rsi
incq %rsi
callq 0x40d020
jmp 0x404f93
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x405010
movq -0xe0(%rbp), %rsi
jmp 0x404fdf
movq %rax, %rbx
movq -0x180(%rbp), %rdi
cmpq %r14, %rdi
je 0x405010
movq -0x170(%rbp), %rsi
jmp 0x404fdf
jmp 0x40500d
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
cmpq %r12, %rdi
je 0x405010
movq -0x190(%rbp), %rsi
incq %rsi
jmp 0x404ff9
movq %rax, %rbx
movl $0x88, %esi
jmp 0x404ff6
movq %rax, %rbx
movl $0x40, %esi
movq %r14, %rdi
callq 0x40d020
jmp 0x405010
jmp 0x40500d
jmp 0x40500d
movq %rax, %rbx
jmp 0x405032
jmp 0x40500d
jmp 0x40500d
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
leaq -0xc0(%rbp), %rax
cmpq %rax, %rdi
je 0x405032
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x110(%rbp), %rdi
leaq -0x100(%rbp), %rax
cmpq %rax, %rdi
je 0x405054
movq -0x100(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x40506d
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x405086
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x4050a2
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x40d020
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0x4050c4
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x40d020
movq %rbx, %rdi
callq 0x4ad8c0