Note that I posted this thread earlier, I'm reposting because the issue has evolved some and I have a clearer idea of what the problem is.
I'm trying to resolve a segmentation fault that occurs in the traversal of a string array. My code is doing the following:
- open a file
- read hex strings from binary file compiled from simple c++ program
- filter out any strings that are not 8 bits long (everything else is garbage)
- convert strings to decimal representation of hex string
- load decimal values into array to be used by Verilog module
The rest is erroneous.
Here is my code:
#include "VMIPS.h"
#include "VMIPS_MIPS.h"//required to explicitly access signals from submodules
#include <verilated.h>
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
void outputLine( const string );
static inline int hexCharValue(char);
string array[274815];
unsigned int main_time = 0;
int i=0;
int k=0;//used for sorted array augmentation
int z = 0;//used for stream in counter
int main(int argc, char **argv)
{
//define local variables/arrays
int temp;//hex conversaion accumulator
string Hex;//dump variable for stream in
//define objects
ifstream inClientFile( "DAN_FILE.txt",ios::in );//stream in object
Verilated::commandArgs(argc, argv);
VMIPS *top = new VMIPS;
top->CLK = 0;
//popen();
//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;
cout << "Z:" << z << endl;
int DataMem[z];
int InstructionMemory[z];
string tempInstructionMemory[z];
//cut out undesired strings from array
int u = 0;
for(i=0;i<z;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-1;j++ )//(z-u);j++ )//(z-u);j++ )//(z-u);j++ )
{
for( int y=7;y>=0;y--)
{
InstructionMemory[j]+=hexCharValue(tempInstructionMemory[j][y])<<(4*(7-y));
}
temp = 0;
//out << "Index:" << j << " HEX:" << tempInstructionMemory[j] << " DEC:" << InstructionMemory[j]<<endl;
}
//MIPS I processor interface
while (!Verilated::gotFinish())
{
top->CLK=!(top->CLK);
top->Iin = InstructionMemory[top->Iaddr];
top->eval();
top->Din = DataMem[top->Daddr];
DataMem[top->Daddr] = top->Dout;
main_time++;
printf("PCOut:%d",top->v->__PVT__PCout);//example of inner register access
//printf("\nIaddr:%d Iin:%d CLK:%d Time:%d",top->Iaddr,top->Iin,top->CLK,main_time);
if(top->CLK)
{
printf("(%d)\n****************************************\n",main_time);
}
if(main_time>9)
{
printf("\n********** Complete **********\n\n");
return 0;
}
}
return 0;
}
//stream in helper function
void outputLine( const string Hex )
{
array[i] = Hex;
}
//hex conversion helper function
static inline int hexCharValue(char ch)
{
if (ch>='0' && ch<='9')return ch-'0';
if (ch>='a' && ch<='f')return ch-'a'+10;
return 0;
}
/*
***** 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 ..
*/
So the issue seems to be occurring at line 77. This line converts string hex numbers to decimal integers. If one were to spell out this line (which is how I originally had done it.) the code would be the following:
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;
Now, if I comment out the lines 5 and 11 I no longer get the segfault. If I change the argument of the pow function to anything between 0-5 and 8+ then the seg fault doesn't occur either. Any ideas?