-
Notifications
You must be signed in to change notification settings - Fork 1
/
VirtualMachine.cpp
266 lines (209 loc) · 6.87 KB
/
VirtualMachine.cpp
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#ifndef VM_VirtualMachine
#define VM_VirtualMachine
// This file defines the virtual machine object.
#ifndef MODE
#define Mode_Release 1
#define Mode_Debug 2
#define MODE Mode_Release
#endif
#include <stdint.h>
#include <stdio.h>
#include "HwError.hpp"
#include "Component.hpp"
#include "Memory.cpp"
#include "InstructionType.hpp"
#include "Pipeline.hpp"
#include "ControlUnit.cpp"
#include "BUS.cpp"
#include "MachineControls.cpp"
#include "StageIF.cpp"
#include "StageID.cpp"
#include "StageEX.cpp"
#include "StageMEM.cpp"
#include "StageWB.cpp"
#if MODE == Mode_Debug
#include <stdio.h>
#include "DebugPrint.hpp"
#endif
// Specs are:
// 1MiB of ram (= 2^20 Bytes)
// memory begins at 0x00 and ends at 0xffffffff
// printer begins at 0x100000000 and ends at 0x100000011
// power begins at 0x100000020 and ends at 0x100000020
class VirtualMachine : public Component::Component_t
{
private:
Instruction::InstructionParser Parser;
Stage::IF IF;
Stage::ID ID;
Stage::EX EX;
Stage::MEM MEM;
Stage::WB WB;
Component::MachineMemory Memory;
Component::Printer Printer;
Component::MachinePower Power;
Component::Keyboard Keyboard;
Component::BUS BUS;
public:
const uint64_t MemBaseAddress = 0;
const uint64_t MemSize = 0x100000000;
const uint64_t PrinterBaseAddress = 0x100000000;
const uint64_t PrinterSize = 0x12;
const uint64_t PowerBaseAddress = 0x100000020;
const uint64_t PowerSize = 0x1;
const uint64_t KeyboardBaseAddress = 0x100000030; // Leave room for future expansion.
const uint64_t KeyboardSize = 0x1;
VirtualMachine()
{
Memory.SetNewMemorySize( MemSize );
Component::IntegerRegisterFile * RegF = ID.GetRegFForWB();
WB.SetRegF( RegF );
Memory.SetRange( MemBaseAddress, MemSize );
Printer.SetRange( PrinterBaseAddress, PrinterSize );
Power.SetRange( PowerBaseAddress, PowerSize );
Keyboard.SetRange( KeyboardBaseAddress, KeyboardSize );
BUS.AddPeripheral( &Memory, 0 );
BUS.AddPeripheral( &Printer, 1 );
BUS.AddPeripheral( &Power, 2 );
BUS.AddPeripheral( &Keyboard, 3 );
IF.SetMemoryAccess( &BUS );
MEM.SetMemoryAccess( &BUS );
}
Component::Component_ID GetID() override
{
return Component::Component_ID_VirtualMachine;
}
HwError Setup() override
{
HwError result = HwError_NoError;
result = Memory.Setup();
if( result != HwError_NoError )
return result;
result = Printer.Setup();
if( result != HwError_NoError )
return result;
result = Power.Setup();
if( result != HwError_NoError )
return result;
result = Keyboard.Setup();
if( result != HwError_NoError )
return result;
result = BUS.Setup();
if( result != HwError_NoError )
return result;
result = IF.Setup();
if( result != HwError_NoError )
return result;
result = ID.Setup();
if( result != HwError_NoError )
return result;
result = EX.Setup();
if( result != HwError_NoError )
return result;
result = MEM.Setup();
if( result != HwError_NoError )
return result;
result = WB.Setup();
if( result != HwError_NoError )
return result;
return result;
}
Component::MemoryAccess * GetMem()
{
return &BUS;
}
Component::IntegerRegisterFile * RegF()
{
return ID.GetRegFForWB();
}
uint64_t GetPCVal()
{
return IF.GetPCReg();
}
void SetPCVal( uint64_t val )
{
IF.ReplacePC( val );
}
// return whether or not to keep going.
bool PerformExecutionRound()
{
HwError result = HwError_NoError;
Stage::Pipeline_IFtoID IFtoID = IF.Perform( 0, 0 );
#if MODE == Mode_Debug
PrintPipeIFtoID( IFtoID );
#endif
result = IF.LastHwError();
if( result != HwError_NoError )
{
#if MODE == Mode_Debug
printf( "Error on IF\n" );
#endif
SetLastHwError( result );
return false;
}
Instruction::ParsedInstruction lInstruction = Parser.ParseInstruction( IFtoID.Instruction.Instruction );
Stage::Pipeline_IDtoEX IDtoEX = ID.Perform( IFtoID, lInstruction );
#if MODE == Mode_Debug
PrintPipeIDtoEX( IDtoEX );
#endif
result = ID.LastHwError();
if( result != HwError_NoError )
{
#if MODE == Mode_Debug
printf( "Error on ID\n" );
#endif
SetLastHwError( result );
return false;
}
Stage::Pipeline_EXtoMEM EXtoMEM = EX.Perform( IDtoEX );
#if MODE == Mode_Debug
PrintPipeEXtoMEM( EXtoMEM );
#endif
result = EX.LastHwError();
if( result != HwError_NoError )
{
#if MODE == Mode_Debug
printf( "Error on EX\n" );
#endif
SetLastHwError( result );
return false;
}
Stage::Pipeline_MEMtoWB MEMtoWB = MEM.Perform( EXtoMEM );
#if MODE == Mode_Debug
PrintPipeMEMtoWB( MEMtoWB );
#endif
result = MEM.LastHwError();
if( result != HwError_NoError )
{
#if MODE == Mode_Debug
printf( "Error on MEM\n" );
#endif
SetLastHwError( result );
return false;
}
WB.Perform( MEMtoWB );
result = WB.LastHwError();
if( result != HwError_NoError )
{
#if MODE == Mode_Debug
printf( "Error on WB\n" );
#endif
SetLastHwError( result );
return false;
}
if( EXtoMEM.PCResult.ReplacePC )
{
#if MODE == Mode_Debug
printf( "Replacing PC from %ld to %ld\n", IF.GetPCReg(), EXtoMEM.PCResult.PCVal );
#endif
uint64_t NewPCVal = EXtoMEM.PCResult.PCVal;
IF.ReplacePC( NewPCVal );
}
return (bool)Power.state;
}
void Perform()
{
while( PerformExecutionRound() );
}
};
#endif