// File: /workspace/github_cpp_makefile_star2_O2/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
leaq -0x60(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rsi, %r14
movl %edi, %r15d
xorl %ecx, %ecx
leaq -0x40(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq -0xa0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq -0x80(%rbp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq -0x1a0(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x17c842(%rip), %rsi    # 0x5810ee
leaq -0xf0(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x40512e
movabsq $0x3a6d3a643a743a69, %rax # imm = 0x3A6D3A643A743A69
leaq -0x1c0(%rbp), %r12
movq %rax, (%r12)
movb $0x0, 0x8(%r12)
movl $0x0, -0x2c(%rbp)
leaq -0x50(%rbp), %r13
movq $0x5fc760, %rbx         # imm = 0x5FC760
movl %r15d, %edi
movq %r14, %rsi
movq %r12, %rdx
callq 0x542820
cmpl $0x64, %eax
je 0x404947
cmpl $0x69, %eax
je 0x404938
cmpl $0x6d, %eax
je 0x40493e
movq %r13, %rdi
cmpl $0x74, %eax
je 0x40494e
cmpl $-0x1, %eax
je 0x404958
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c7d6(%rip), %rsi    # 0x5810fa
callq 0x4755f0
movb $0x1, %cl
movl %ecx, -0x2c(%rbp)
movq %rax, %rdi
callq 0x474ed0
jmp 0x4048ec
leaq -0x70(%rbp), %rdi
jmp 0x40494e
leaq -0x90(%rbp), %rdi
jmp 0x40494e
leaq -0xb0(%rbp), %rdi
movq (%rbx), %rsi
callq 0x47c970
jmp 0x4048ec
cmpq $0x0, -0x68(%rbp)
je 0x404990
testb $0x1, -0x2c(%rbp)
je 0x4049e9
movq (%r14), %rdi
callq 0x4d24b0
leaq -0x1e0(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
movq %rax, %rsi
callq 0x40512e
leaq -0x1e0(%rbp), %rbx
jmp 0x4049d0
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c77d(%rip), %rsi    # 0x58111b
callq 0x4755f0
movq %rax, %rdi
callq 0x474ed0
movq (%r14), %rdi
callq 0x4d24b0
leaq -0x200(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
movq %rax, %rsi
callq 0x40512e
leaq -0x200(%rbp), %rbx
movq %rbx, %rdi
callq 0x4047c8
movq %rbx, %rdi
callq 0x47b9c0
pushq $-0x1
popq %r14
jmp 0x404d65
cmpq $0x0, -0xa8(%rbp)
jne 0x404a1e
movq -0x70(%rbp), %rdi
callq 0x4f7270
movq %rax, %rbx
movq %rax, %rdi
callq 0x545600
leaq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x47c970
movq %rbx, %rdi
callq 0x4f4f40
cmpq $0x0, -0x88(%rbp)
jne 0x404a69
movq -0x70(%rbp), %rdi
callq 0x4f7270
movq %rax, %rbx
movq %rax, %rdi
callq 0x4d24b0
movq %rax, %r14
pushq $0x2e
popq %rsi
movq %rax, %rdi
callq 0x401048
testq %rax, %rax
je 0x404a52
movb $0x0, (%rax)
leaq -0x90(%rbp), %rdi
movq %r14, %rsi
callq 0x47c970
movq %rbx, %rdi
callq 0x4f4f40
movq -0x48(%rbp), %rdx
testq %rdx, %rdx
jne 0x404abe
movq -0x70(%rbp), %rdi
callq 0x4f7270
movq %rax, %rbx
movq %rax, %rdi
callq 0x545600
leaq -0x50(%rbp), %rdi
movq %rax, %rsi
callq 0x47c970
leaq 0x183703(%rip), %rsi    # 0x58819c
leaq -0x50(%rbp), %rdi
callq 0x47d170
leaq 0x17c63d(%rip), %rsi    # 0x5810e6
leaq -0x50(%rbp), %rdi
callq 0x47d170
movq %rbx, %rdi
callq 0x4f4f40
movq -0x48(%rbp), %rdx
movslq -0xe8(%rbp), %rcx
movb $0x1, %r15b
subq %rcx, %rdx
jbe 0x404b13
leaq -0xd0(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x47e2c0
leaq -0x1b0(%rbp), %rbx
leaq -0xd0(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x47ba10
movq %r14, %rdi
callq 0x47b9c0
movq (%rbx), %rdi
movq -0xf0(%rbp), %rsi
callq 0x4010d8
testl %eax, %eax
setne %r15b
pushq $0x40
popq %rdi
callq 0x40ce70
movq %rax, %r14
movq %rax, %rdi
callq 0x40af30
movq %r14, 0x1efb2b(%rip)    # 0x5f4658
movl $0x88, %edi
callq 0x40ce70
movq %rax, %r14
movq %rax, %rdi
callq 0x405280
movq %r14, 0x1efb17(%rip)    # 0x5f4660
movq 0x1efb08(%rip), %rax    # 0x5f4658
movq 0x38(%rax), %rax
movq %rax, 0x80(%r14)
leaq -0x190(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x47e0a0
addq $0x60, %r14
leaq -0x190(%rbp), %rsi
movq %r14, %rdi
callq 0x47b5f0
leaq -0x190(%rbp), %rdi
callq 0x47b9c0
movq 0x1efacf(%rip), %rdi    # 0x5f4660
callq 0x4074d0
pushq $-0x1
popq %r14
testb %al, %al
je 0x404d25
leaq -0x170(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x47e0a0
leaq -0x170(%rbp), %rdi
callq 0x404e7d
leaq -0x170(%rbp), %rdi
callq 0x47b9c0
movq 0x1efa8f(%rip), %rbx    # 0x5f4660
leaq -0x150(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x47e0a0
leaq -0x130(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x47e0a0
leaq -0x110(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x47e0a0
movzbl %r15b, %r8d
leaq -0x150(%rbp), %rsi
leaq -0x130(%rbp), %rdx
leaq -0x110(%rbp), %rcx
movq %rbx, %rdi
callq 0x406cfc
movl %eax, %ebx
leaq -0x110(%rbp), %rdi
callq 0x47b9c0
leaq -0x130(%rbp), %rdi
callq 0x47b9c0
leaq -0x150(%rbp), %rdi
callq 0x47b9c0
testb %bl, %bl
je 0x404ca0
testb %r15b, %r15b
je 0x404ccb
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c4d6(%rip), %rsi    # 0x58113b
callq 0x4755f0
leaq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x47e340
leaq 0x18351c(%rip), %rsi    # 0x58819c
movq %rax, %rdi
callq 0x4755f0
leaq -0x90(%rbp), %rsi
movq %rax, %rdi
callq 0x47e340
leaq 0x17c4b9(%rip), %rsi    # 0x581157
jmp 0x404d12
leaq 0x17c4d4(%rip), %rax    # 0x58117b
leaq 0x17c4f1(%rip), %rsi    # 0x58119f
testb %r15b, %r15b
cmovneq %rax, %rsi
movq $0x5f5580, %rdi         # imm = 0x5F5580
callq 0x4755f0
movq %rax, %rdi
callq 0x474ed0
jmp 0x404d25
movq $0x5f5580, %rdi         # imm = 0x5F5580
leaq 0x17c483(%rip), %rsi    # 0x58115c
callq 0x4755f0
leaq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0x47e340
leaq 0x1834a8(%rip), %rsi    # 0x58819c
movq %rax, %rdi
callq 0x4755f0
leaq -0x90(%rbp), %rsi
movq %rax, %rdi
callq 0x47e340
leaq 0x17c3dc(%rip), %rsi    # 0x5810ee
movq %rax, %rdi
callq 0x4755f0
xorl %r14d, %r14d
movq %rax, %rdi
callq 0x474ed0
movq 0x1ef934(%rip), %rbx    # 0x5f4660
testq %rbx, %rbx
je 0x404d39
movq %rbx, %rdi
callq 0x4052d6
movl $0x88, %esi
movq %rbx, %rdi
callq 0x40bfd0
movq 0x1ef90b(%rip), %rbx    # 0x5f4658
testq %rbx, %rbx
je 0x404d5a
movq %rbx, %rdi
callq 0x40afcc
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0x40bfd0
leaq -0xf0(%rbp), %rdi
callq 0x47b9c0
leaq -0x1b0(%rbp), %rdi
callq 0x47b9c0
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
leaq -0xb0(%rbp), %rdi
callq 0x47b9c0
leaq -0x50(%rbp), %rdi
callq 0x47b9c0
leaq -0x70(%rbp), %rdi
callq 0x47b9c0
movl %r14d, %eax
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
callq 0x47b9c0
jmp 0x404dd0
movq %rax, %rbx
leaq -0x130(%rbp), %rdi
callq 0x47b9c0
jmp 0x404de1
movq %rax, %rbx
leaq -0x150(%rbp), %rdi
jmp 0x404e02
movq %rax, %rbx
leaq -0x170(%rbp), %rdi
jmp 0x404e02
jmp 0x404e30
movq %rax, %rbx
leaq -0x190(%rbp), %rdi
callq 0x47b9c0
jmp 0x404e33
movq %rax, %rbx
movl $0x88, %esi
jmp 0x404e19
movq %rax, %rbx
pushq $0x40
popq %rsi
movq %r14, %rdi
callq 0x40bfd0
jmp 0x404e33
jmp 0x404e30
jmp 0x404e30
movq %rax, %rbx
jmp 0x404e3f
jmp 0x404e30
jmp 0x404e30
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x47b9c0
leaq -0x1b0(%rbp), %rdi
callq 0x47b9c0
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
leaq -0xb0(%rbp), %rdi
callq 0x47b9c0
leaq -0x50(%rbp), %rdi
callq 0x47b9c0
leaq -0x70(%rbp), %rdi
callq 0x47b9c0
movq %rbx, %rdi
callq 0x4ad3d0