i needed help processing a file to memory in a VM

i needed help processing a file to memory in a VM.
i needed help in the load function, i cant figure out how to process 2 hex digits from file into mem on the vm interface

file im processing is

01 40
02 01
63 12
f0 00

i basically need to show it in mem[] on a screen that looks like

Regs 00 00 00 00 00 00 00 00

0 1 2 3 4 5 6 7 8 9 a b c d e f
+------------------------------------------------
0 | 01 40 20 01 63 12 f0 00 00 00 00 00 00 00 00 00
1 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
2 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
3 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
4 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
5 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
6 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
8 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
9 | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
a | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
b | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
c | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
d | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
e | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
f | 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <fstream>
#include <string>
#include "cpu.h"
#include "cell.h"
#include "console_graphics.h"

using namespace std;

//-----------------------------------------------------------------------
Cpu::Cpu()
{
	//clear the screen;
	cls(DARKGRAY);

	// initialize and display registers 
	int top=1;
	int left=14;
	printText("Reg: ",top,left,Fontcolor,Backgroundcolor);
	left+=7;
	for(int ndx=0 ; ndx<MAX_REG ; ndx++){
		reg[ndx].setPosition(top,left+(ndx*5));
		reg[ndx].setColors(Fontcolor,Backgroundcolor);
		reg[ndx].setValue(0);
	}

	// draw memory border
	cout << endl;
	printText("  0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f ", 3, 6,Fontcolor,Backgroundcolor);
	drawBox(4, 5, 18, 66, Fontcolor, Backgroundcolor);
	printText(" 0", 5, 3,Fontcolor,Backgroundcolor);
	printText(" 1", 6, 3,Fontcolor,Backgroundcolor);
	printText(" 2", 7, 3,Fontcolor,Backgroundcolor);
	printText(" 3", 8, 3,Fontcolor,Backgroundcolor);
	printText(" 4", 9, 3,Fontcolor,Backgroundcolor);
	printText(" 5", 10, 3,Fontcolor,Backgroundcolor);
	printText(" 6", 11, 3,Fontcolor,Backgroundcolor);
	printText(" 7", 12, 3,Fontcolor,Backgroundcolor);
	printText(" 8", 13, 3,Fontcolor,Backgroundcolor);
	printText(" 9", 14, 3,Fontcolor,Backgroundcolor);
	printText(" a", 15, 3,Fontcolor,Backgroundcolor);
	printText(" b", 16, 3,Fontcolor,Backgroundcolor);
	printText(" c", 17, 3,Fontcolor,Backgroundcolor);
	printText(" d", 18, 3,Fontcolor,Backgroundcolor);
	printText(" e", 19, 3,Fontcolor,Backgroundcolor);
	printText(" f", 20, 3,Fontcolor,Backgroundcolor);

	// initialize and display memory cells
	int memTop=4;
	int memTempleft=6;
	int memLeft=6;
	int temp=-16;//used to drop the line. is -16 because ifstatement processes when ndx==0

	for(int ndx=0 ; ndx<=MAX_MEM ; ndx++){

		memLeft=memTempleft+((ndx-temp)*4);

		// if statement to drop the new line
		if (ndx % 16 == 0){
			memTop+=1;
			memLeft=6;
			temp+=16;
		}

		mem[ndx].setPosition(memTop,memLeft);
		mem[ndx].setColors(Fontcolor,Backgroundcolor);
		mem[ndx].setValue(0);
	}

}

//-----------------------------------------------------------------------
bool Cpu::load(char filename[])
{


	ifstream file;
	file.open (filename);
	string fileProcessArray;
	int tempValue=0;
	char charProcessArray[100];

	if (file.is_open())
	{
		getline(file,fileProcessArray);

		for(int ndx=0;ndx<=1;ndx++){//right here im having trouble<=====
			charProcessArray[ndx]=fileProcessArray[ndx];
		}
			tempValue=hexstrToInt(charProcessArray);
			mem[0].setValue(tempValue);

			file.close();
	}

	else cout << "Unable to open file"; 

	return true;
}

int hexstrToInt(char inStr[])
{
	int result=0;
	int ndx=0;
	int digitValue=0;

	//convert characters until we reach a non hex digit (e.g. it's not 0-f)
	while (isxdigit(inStr[ndx])) {             // (in cctype)
		if (inStr[ndx]>='0' && inStr[ndx]<='9')
			digitValue = inStr[ndx] - '0';
		else { //it’s a-f or A-F
			inStr[ndx] = toupper(inStr[ndx]);
			digitValue = inStr[ndx] - 'A' + 10;
		}
		result = 16*result + digitValue;
		ndx++;
	}

	return result;
}
Topic archived. No new replies allowed.