CPU emulator

Hello,

I have to write an assembly/CPU simulator in C++, I wrote down all the functions, declarations, etc. The program will read from a file (instructions.txt) in the form of the command followed by a value in Hex. For example, c = 5 + 6 would be:

1
2
3
4
LODD 0x0005
LODD 0x0006
ADDD
STOD C


I know I have to write a switch statement to read from the file and decide which function to run but I'm having a difficult time figuring out how.

Here are the functions/declarations if anyone needs them:
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
int pc, ac, sp, ir, tir, zero, add1, sub1, Amask, Bmask;
int A, B, C, D, E, F;
int Alatch, Blatch, MAR, MBR;
int AMUX, ALU, shifter, Abus, Bbus, Cbus;
int addressOut, data_in, data_out;
int address;
int m[4095];
char inst[4];
int c = 0;

void LODD (int address)
{
    ac=m[address];
}

void LOCO (int address)
{
    if(0<=address&&address<=4095)
       ac=address;
}

void LODL (int address)
{
    ac=m[sp+address];
}

void STOD (int address)
{
m[address]=ac;
}

void SUBD (int address)
{
    ac=ac-m[address];
}

void STOL (int address)
{
    m[address+sp]=ac;
}

void SUBL (int address)
{
    ac=ac-m[sp-address];
}

void SWAP (int address)
{
    int tmp;
    tmp=ac;
    ac=sp;
    sp=tmp;
}

void STOP (int address)
{
    cout <<"The sum of three numbers is : "<< m[1004];
    exit (0);
}

void ADDD (int address)
{
ac=ac+m[address];
}

void ADDL (int address)
{
    ac=ac+m[sp+address];
}

void JPOS (int address)
{
    if(ac>=0)
    pc=address;
}

void JZER (int address)
{
    if(ac=0)
    pc=address;
}

void JUMP (int address)
{
    pc=address;
}

void JNEG (int address)
{
    if(ac<0)
    pc=address;
}

void JNZE (int address)
{
    if(ac!=0)
    pc=address;
}

void PSHI (int address)
{
    sp=sp-1;
    m[sp]=m[ac];
}

void I (int address)
{
    m[ac]=m[sp];
    sp=sp+1;
}

void PUSH (int address)
{
    sp=sp-1;
    m[sp]=ac;
}

void POP (int address)
{
    ac=m[sp];
    sp=sp+1;
}

void CALL (int address)
{
    sp=sp-1;
    m[sp]=pc;
    pc=address;
}

void RETN (int address)
{
    pc=m[sp];
    sp=sp+1;
}

void INSP (int address)
{
    if(0<=address&&address<=255)
    sp=sp+address;
}

void DESP (int address)
{
    if(0<=address&&address<=255)
    sp=sp-address;
}


int main()
{
    ifstream fin;
    ofstream fout;

    //open input file
    fin.open("Instructions.txt");
    assert (!fin.fail());
In your instructions LODD takes a value, but your code treats the parameter as an address.

Maybe you should step back and make sure you understand what needs to be done before writing any further code.
Topic archived. No new replies allowed.