// File: /workspace/github_cpp_makefile_star2_O2/stacksmith[P]picoasm/cassembler.cpp
// Function: CAssembler::exportVHDL(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool)
bool CAssembler::exportVHDL( string templateFile, string outputDir, string entityName, bool bVHDL)
{
	int addr, i, j, k, l, n ;
	unsigned char INIT[ 32 ][ 64 ] ;		/* 32 * 64 = 2048 bytes */
	unsigned char INITP[ 32 ][ 8 ] ;		/* 32 * 8 =   256 bytes */
	unsigned int d ;                        /*			 2304 Bytes Total = (18b * 1024 ) / 8 (1 instr = 18 bits )*/
	
	CInstruction *instr ;
	
	// ajg112 - Open Mem file
	string memFileName = outputDir + "/" + entityName + ".mem";
	FILE * memFile = fopen( memFileName.c_str(), "w" ) ;		
	if (memFile == NULL) {
		error(NO_LINE_NR, string( "Unable to open mem file '" + memFileName + "'" ).c_str());
		return FALSE ;
	}

	// ajg112 - Specify start address
	fprintf(memFile, "@0\n");

	for ( i = 0 ; i < 32 ; i++ )
		for( j = 0 ; j < 32 ; j++ )
			INIT[ i ][ j ] = 0 ;
			
	for ( i = 0 ; i < 32 ; i++ )
		for ( j = 0 ; j < 8 ; j++ )
			INITP[ i ][ j ] = 0 ;
	
	for ( addr = i = j = k = l = 0, n = 0 ; addr < 1024 ; addr++ ) {			
		instr = m_code->getInstruction( addr ) ;
		
		if ( instr == NULL ) d = 0 ;
		else d = instr->getHexCode() ;

		//cout << d <<"\n";

		// ajg112 - Output memory value
		fprintf(memFile, "%05X\n", d);
						
		INIT[ i++ ][ j ] =  d ;		// instruction( 15 downto 0 )
		INIT[ i++ ][ j ] =  d >> 8;
		
		INITP[ k ][ l ] |= ( ( d >> 16 ) & 0x3 ) << n ;	// instruction( 17 downto 16 ) ;
		n += 2 ;
				
		if ( n >= 8 ) {
			n = 0 ;
			k++ ;
			if ( k >= 32 ) {
				l++ ;
				k = 0 ;
			}
		}
		
		if ( i >= 32 ) {
			i = 0 ;
			j++ ;
		}
	}

	// ajg112 - Close Mem File
	fclose(memFile);
	
	FILE * infile = fopen( templateFile.c_str(), "r" ) ;
	if ( infile == NULL ) {
		error( NO_LINE_NR, string( "Unable to open template file '" + templateFile + "'" ).c_str() ) ;
		return FALSE ;
	}

	// RDC 02/02/2007 set VHDL or verilog file extension
	string exportExt;
	if (bVHDL){
	  exportExt = ".vhd"; 
	} else {
	  exportExt = ".v"; 
	}

	// RDC 02/02/2007 set VHDL or verilog file extension
	// string exportFile = outputDir + "/" + entityName + ".vhd" ;
	string exportFile = outputDir + "/" + entityName + exportExt;

	FILE * outfile = fopen( exportFile.c_str(), "w" ) ;		
	if ( outfile == NULL ) {
	  // RDC 02/02/2007 set VHDL or verilog file extension
	  // error( NO_LINE_NR , string( "Unable to open output file '" + exportFile + ".vhd'").c_str() ) ;
	  error( NO_LINE_NR , string( "Unable to open output file '" + exportFile + exportExt + "'").c_str() ) ;
	  return FALSE ;
	}
		
	bool store = false, copy = false;
	char varname[ 64 ] ;
	int p = 0 ;
	int line, c ;
	while ( ( c = fgetc( infile ) ) != EOF ) {
		if ( store && p < 64 )
			varname[ p++ ] = c ;
		
		if ( c == '{' ) {
			store = true ;		
			p = 0 ;
		}
		
		if ( !store && copy )
			fputc( c, outfile ) ;
		
		if ( c == '}' ) {
			store = false ;
			if ( p > 0 )
				varname[ p - 1 ] = '\0' ;
			else
				varname[ 0 ] = '\0' ;
			if ( strncmp( "INIT_", varname, 5 ) == 0 ) {
				sscanf( varname, "INIT_%02X", &line ) ;
				if ( line >= 0 && line < 64 ) {
					for( j = 31 ; j >= 0 ; j-- ) 
						fprintf( outfile, "%02X", INIT[ j ][ line ] ) ;
				}
			} else if ( strncmp( "INITP_", varname, 6 ) == 0 ) {
				sscanf( varname, "INITP_%02X", &line ) ;
				if ( line >= 0 && line < 8 ) 
					for( j = 31 ; j >= 0 ; j-- )
		 				fprintf( outfile, "%02X", INITP[ j ][ line ] ) ;
			} else if ( strcmp( "name", varname ) == 0 ) {
				fprintf( outfile, entityName.c_str() ) ;				
			} else if ( strcmp( "begin template", varname ) == 0 ) {
				copy = true ;
			}
		}
	}
		
	fclose( infile ) ;
	fclose( outfile ) ;
	
	return TRUE ;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa28, %rsp            # imm = 0xA28
movl %r8d, %r12d
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r13
movq %rdi, -0x68(%rbp)
leaq 0x181475(%rip), %rdx    # 0x58819c
leaq -0x60(%rbp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x407993
leaq -0xd0(%rbp), %rdi
movq %rbx, %rsi
movq %r14, -0xd8(%rbp)
movq %r14, %rdx
callq 0x40796a
leaq 0x17a9f2(%rip), %rdx    # 0x581748
leaq -0x128(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x4078e2
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
leaq -0x60(%rbp), %rdi
callq 0x47b9c0
movq -0x128(%rbp), %rdi
leaq 0x17ed7d(%rip), %rsi    # 0x585b09
callq 0x4e3010
testq %rax, %rax
je 0x4071c0
leaq 0x17a9c6(%rip), %rdi    # 0x581767
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
movq %rax, -0x40(%rbp)
movq %rax, %rcx
callq 0x4e3340
leaq -0xa50(%rbp), %rax
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x406ddc
xorl %edx, %edx
cmpq $0x20, %rdx
je 0x406dd3
movb $0x0, (%rax,%rdx)
incq %rdx
jmp 0x406dc4
incq %rcx
addq $0x40, %rax
jmp 0x406dbc
leaq -0x250(%rbp), %rax
xorl %ecx, %ecx
cmpq $0x20, %rcx
je 0x406e05
xorl %edx, %edx
cmpq $0x8, %rdx
je 0x406dfc
movb $0x0, (%rax,%rdx)
incq %rdx
jmp 0x406ded
incq %rcx
addq $0x8, %rax
jmp 0x406de5
movq %r13, -0xe8(%rbp)
movl %r12d, -0x6c(%rbp)
movq %r15, -0xe0(%rbp)
movl $0x0, -0x2c(%rbp)
xorl %r13d, %r13d
movq $0x0, -0x38(%rbp)
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
cmpl $0x400, %r14d           # imm = 0x400
je 0x406efd
movq -0x68(%rbp), %rax
movq 0x80(%rax), %rdi
movzwl %r14w, %esi
callq 0x40ae7e
testq %rax, %rax
je 0x406e5d
movl 0x1c(%rax), %ebx
jmp 0x406e5f
xorl %ebx, %ebx
movq -0x40(%rbp), %rdi
leaq 0x17a901(%rip), %rsi    # 0x58176b
movl %ebx, %edx
xorl %eax, %eax
callq 0x4d4190
movslq %r12d, %rax
movl %r15d, %ecx
shlq $0x6, %rax
addq %rbp, %rax
addq $-0xa50, %rax           # imm = 0xF5B0
movb %bl, (%rcx,%rax)
movb %bh, 0x40(%rcx,%rax)
shrl $0x10, %ebx
andl $0x3, %ebx
movl %r13d, %ecx
shll %cl, %ebx
movslq -0x38(%rbp), %rax
movslq -0x2c(%rbp), %rcx
leaq -0x250(,%rax,8), %rax
addq %rbp, %rax
orb %bl, (%rcx,%rax)
cmpl $0x6, %r13d
jge 0x406eba
addl $0x2, %r13d
jmp 0x406ed9
xorl %r13d, %r13d
xorl %eax, %eax
movq -0x38(%rbp), %rcx
cmpl $0x1f, %ecx
leal 0x1(%rcx), %ecx
setge %al
cmovgel %r13d, %ecx
addl %eax, -0x2c(%rbp)
movl %ecx, %eax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpl $0x1e, %r12d
leal 0x2(%r12), %ecx
movl $0x0, %edx
cmovgel %edx, %ecx
setge %al
addl %eax, %r15d
incl %r14d
movl %ecx, %r12d
jmp 0x406e32
movq -0x40(%rbp), %rdi
callq 0x4e2780
movq -0xe8(%rbp), %rbx
movq (%rbx), %rdi
leaq 0x17f648(%rip), %rsi    # 0x58655f
callq 0x4e3010
movq %rax, -0x38(%rbp)
testq %rax, %rax
je 0x407218
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x17a217(%rip), %rax    # 0x581157
leaq 0x17a1a7(%rip), %rsi    # 0x5810ee
cmpb $0x0, -0x6c(%rbp)
cmovneq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0x47c970
leaq 0x18123d(%rip), %rdx    # 0x58819c
leaq -0x90(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x407993
leaq -0xd0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
movq -0xd8(%rbp), %rdx
callq 0x40796a
leaq -0x108(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x40796a
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
movq -0x108(%rbp), %rdi
leaq 0x17eb40(%rip), %rsi    # 0x585b09
callq 0x4e3010
testq %rax, %rax
setne %bl
je 0x40726d
movq %rax, %r12
movl %ebx, -0x40(%rbp)
xorl %eax, %eax
xorl %r14d, %r14d
movl %eax, -0x68(%rbp)
xorb $0x1, %al
movb %al, -0x2c(%rbp)
xorl %r15d, %r15d
movq -0x38(%rbp), %rdi
callq 0x4e7870
cmpl $-0x1, %eax
je 0x4071ff
movl %eax, %ebx
testb $0x1, %r15b
je 0x40701d
cmpl $0x3f, %r14d
jg 0x40701d
movslq %r14d, %rax
incl %r14d
movb %bl, -0xd0(%rbp,%rax)
movl %r14d, %r13d
cmpl $0x7b, %ebx
movzbl %r15b, %r15d
pushq $0x1
popq %rax
cmovel %eax, %r15d
movl $0x0, %eax
cmovel %eax, %r14d
movl %r15d, %eax
orb -0x2c(%rbp), %al
testb $0x1, %al
jne 0x40704b
movl %ebx, %edi
movq %r12, %rsi
callq 0x4e7730
cmpl $0x7d, %ebx
jne 0x406ff0
testl %r13d, %r13d
jle 0x407063
decl %r13d
movb $0x0, -0xd0(%rbp,%r13)
jmp 0x40706a
movb $0x0, -0xd0(%rbp)
leaq -0xd0(%rbp), %rbx
leaq 0x17a735(%rip), %rdi    # 0x5817ad
movq %rbx, %rsi
pushq $0x5
popq %rdx
callq 0x401080
testl %eax, %eax
je 0x4070d3
leaq 0x17a72f(%rip), %rdi    # 0x5817bd
movq %rbx, %rsi
pushq $0x6
popq %rdx
callq 0x401080
testl %eax, %eax
je 0x4070fd
leaq 0x17a29e(%rip), %rdi    # 0x581342
movq %rbx, %rsi
pushq $0x5
popq %rdx
callq 0x401080
testl %eax, %eax
jne 0x40719d
movq -0xd8(%rbp), %rax
movq (%rax), %rsi
xorl %r15d, %r15d
movq %r12, %rdi
xorl %eax, %eax
callq 0x4d4190
jmp 0x406ff0
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x17a6d3(%rip), %rsi    # 0x5817b3
leaq -0x90(%rbp), %rdx
xorl %eax, %eax
callq 0x4d44b0
cmpl $0x3f, -0x90(%rbp)
ja 0x406ff0
jmp 0x407160
xorl %r15d, %r15d
movq %rbx, %rdi
leaq 0x17a6ba(%rip), %rsi    # 0x5817c4
leaq -0x90(%rbp), %rdx
xorl %eax, %eax
callq 0x4d44b0
cmpl $0x7, -0x90(%rbp)
ja 0x406ff0
pushq $0x1f
popq %rbx
leaq 0x17a69b(%rip), %r15    # 0x5817ca
testl %ebx, %ebx
js 0x406fed
movl %ebx, %eax
movslq -0x90(%rbp), %rcx
leaq -0x250(,%rax,8), %rax
addq %rbp, %rax
movzbl (%rcx,%rax), %edx
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x4d4190
decl %ebx
jmp 0x40712f
pushq $0x1f
popq %rbx
leaq 0x17a660(%rip), %r15    # 0x5817ca
testl %ebx, %ebx
js 0x406fed
movl %ebx, %eax
movslq -0x90(%rbp), %rcx
shlq $0x6, %rax
addq %rbp, %rax
addq $-0xa50, %rax           # imm = 0xF5B0
movzbl (%rcx,%rax), %edx
movq %r12, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x4d4190
decl %ebx
jmp 0x40716a
leaq 0x17a62b(%rip), %rdi    # 0x5817cf
movq %rbx, %rsi
pushq $0xf
popq %rdx
callq 0x401080
testl %eax, %eax
sete %al
movl -0x68(%rbp), %ecx
orb %al, %cl
movl %ecx, %eax
jmp 0x406fe5
leaq 0x17a586(%rip), %rsi    # 0x58174d
leaq -0x60(%rbp), %rdi
leaq -0x128(%rbp), %rdx
callq 0x4079cb
leaq 0x17a001(%rip), %rdx    # 0x5811df
leaq -0xd0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4078e2
movq -0xd0(%rbp), %rdx
pushq $-0x1
popq %rsi
callq 0x405312
jmp 0x407251
movq -0x38(%rbp), %rdi
callq 0x4e2780
movq %r12, %rdi
callq 0x4e2780
movl -0x40(%rbp), %ebx
jmp 0x4072eb
leaq 0x17a552(%rip), %rsi    # 0x581771
leaq -0x60(%rbp), %rdi
movq %rbx, %rdx
callq 0x4079cb
leaq 0x179fad(%rip), %rdx    # 0x5811df
leaq -0xd0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x4078e2
movq -0xd0(%rbp), %rdx
pushq $-0x1
popq %rsi
callq 0x405312
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
leaq -0x60(%rbp), %rdi
callq 0x47b9c0
xorl %ebx, %ebx
jmp 0x407300
leaq 0x17a51c(%rip), %rsi    # 0x581790
leaq -0x148(%rbp), %rdi
leaq -0x108(%rbp), %rdx
callq 0x4079cb
leaq -0x90(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0x60(%rbp), %rdx
callq 0x40796a
leaq 0x179f3a(%rip), %rdx    # 0x5811df
leaq -0xd0(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x4078e2
movq -0xd0(%rbp), %rdx
pushq $-0x1
popq %rsi
callq 0x405312
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
leaq -0x148(%rbp), %rdi
callq 0x47b9c0
leaq -0x108(%rbp), %rdi
callq 0x47b9c0
leaq -0x60(%rbp), %rdi
callq 0x47b9c0
leaq -0x128(%rbp), %rdi
callq 0x47b9c0
movl %ebx, %eax
addq $0xa28, %rsp            # imm = 0xA28
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
jmp 0x407334
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
jmp 0x407345
movq %rax, %rbx
leaq -0x148(%rbp), %rdi
callq 0x47b9c0
jmp 0x407356
movq %rax, %rbx
leaq -0x108(%rbp), %rdi
jmp 0x407390
jmp 0x407365
jmp 0x407399
jmp 0x4073c1
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
jmp 0x407390
jmp 0x407399
jmp 0x4073c1
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
jmp 0x407389
movq %rax, %rbx
leaq -0x90(%rbp), %rdi
callq 0x47b9c0
jmp 0x40739c
jmp 0x407399
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x47b9c0
jmp 0x4073c4
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x47b9c0
jmp 0x4073bb
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
jmp 0x4073cb
movq %rax, %rbx
leaq -0x128(%rbp), %rdi
callq 0x47b9c0
movq %rbx, %rdi
callq 0x4ad3d0