I'm having an annoying issue with this code. I get a segmentation fault because of the loop wrapped in comments. Any ideas? This is the goal of this program:
First I wrote a simple c++ program and compiled a binary file for it. I then use readelf to dump the hex instructions into a file which I then read with this program. I read each hex entry as a string, cull the lines in the binary file I don't want, and then convert each hex to a decimal number which I then place into an array. This array represents instruction memory in a processor (I designed a simple MIPS I processor and and trying to run the simple c++ program on it). I use Verilator along with the Verilog modules to test the processor. I'm stuck on this issue though. Any help would be greatly appreciated. I'm still quite new to C++ so it's likely I'm missing something obvious...
Thanks, DS
#include "VMIPS.h"
#include <verilated.h>
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
void outputLine( const string );
string array[274815];
unsigned int main_time = 0;
int i=0;
int k=0;
int z = 0;
int main(int argc, char **argv)
{
//define local variables/arrays
int temp;
char Hex[8];
bool x = 0;
//define objects
ifstream inClientFile( "DAN_FILE.txt",ios::in );
Verilated::commandArgs(argc, argv);
VMIPS *top = new VMIPS;
top->CLK = 0;
//test if instruction file can be opened
if ( !inClientFile )
{
cerr << "File couldn't be opened" << endl;
exit( 1 );
}
/////////////////////////////////////////////////////////////////////////////////
//fill string array with all file values and determines length of program
while ( inClientFile >> Hex )
{
//fills array[..] with strings
outputLine( Hex );
i++;
}
/////////////////////////////////////////////////////////////////////////////////
z = i;
int DataMem[z];
int InstructionMemory[z];
string tempInstructionMemory[z];
//cut out undesired strings from array
int u = 0;
for(i=0;i<274815;i++)
{
if ((array[i].length() == 8)&&((array[i].find("fs24")!=1)&&(array[i].find("f0")!=1)))
{
tempInstructionMemory[k] = array[i];
k++;
}
else
{
u++;
}
}
//convert string hex to numerical decimal
for( int j=0;j<(z-u);j++ )//(z-u);j++ )
{
for( int y=7;y>=0;y--)
{
if( tempInstructionMemory[j].at(y) == '0' ) temp = (0*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '1' ) temp = (1*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '2' ) temp = (2*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '3' ) temp = (3*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '4' ) temp = (4*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '5' ) temp = (5*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '6' ) temp = (6*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '7' ) temp = (7*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '8' ) temp = (8*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == '9' ) temp = (9*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'a' ) temp = (10*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'b' ) temp = (11*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'c' ) temp = (12*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'd' ) temp = (13*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'e' ) temp = (14*pow(16.,(int)(7-y)));
else if( tempInstructionMemory[j].at(y) == 'f' ) temp = (15*pow(16.,(int)(7-y)));
InstructionMemory[j]=InstructionMemory[j]+temp;
}
temp = 0;
//cout << "Index:" << j << " HEX:" << tempInstructionMemory[j] << " DEC:" << InstructionMemory[j]<<endl;
}
//MIPS I processor interface
while (!Verilated::gotFinish())
{
x=!x;
top->CLK=x;
top->Iin = InstructionMemory[top->Iaddr];
top->eval();
top->Din = DataMem[top->Daddr];
DataMem[top->Daddr] = top->Dout;
main_time++;
//printf("\nIaddr:%d Iin:%d CLK:%d Time:%d",top->Iaddr,top->Iin,top->CLK,main_time);
if(x)
{
printf("(%d)\n****************************************\n",main_time);
}
if(main_time>90)
{
printf("\n********** Complete **********\n\n");
return 0;
}
}
return 0;
}
void outputLine( const string Hex )
{
array[i] = Hex;
}
/*
***** Root Command Call *****
cd Desktop && cd verilator-3.802 && export VERILATOR_ROOT=//Users/dansnyder/Desktop/verilator-3.802 && cd test_MIPS && $VERILATOR_ROOT/bin/verilator --cc MIPS.v --exe sim_main.cpp && cd obj_dir/ && make -j -f VMIPS.mk VMIPS && cd .. && obj_dir/VMIPS && cd .. && cd .. && cd ..
*/