-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrixvm.cpp
188 lines (158 loc) · 4.85 KB
/
matrixvm.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
#include <machine/dladapter.h>
#include <machine/motherboard.h>
#include <machine/cpu.h>
#include <dev/basicinterruptcontroller.h>
#include <dev/timerdevice.h>
#include <dev/displaydevice.h>
#include <dev/x11displaymanager.h>
#include <dev/nulldisplaymanager.h>
#include <getopt.h>
#include <stdexcept>
#include <cassert>
using namespace std;
using namespace machine;
struct Options
{
int graphics;
Options()
: graphics(1)
{ }
} options;
void readFile(const char* filename, uint8_t*& contents, int& fileSize);
int getFileLength(FILE* file);
void motherboardException(Motherboard& mb, exception& e)
{
printf("Motherboard exception: %s\n", e.what());
mb.abort();
}
void parseArgs(int argc, char** argv)
{
int c;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
{"nographic", no_argument, &options.graphics, 0},
/* These options don't set a flag.
We distinguish them by their indices. */
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long(argc, argv, "", long_options, &option_index);
/* Detect the end of the options. */
if (c < 0)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
default:
abort();
}
}
if (optind < argc)
{
/* remaining command line arguments (not options) */
}
}
int main(int argc, char** argv)
{
parseArgs(argc, argv);
const static int NUM_CPUS = 4;
Motherboard* mb = new Motherboard;
mb->setMemorySize(10 * 1024 * 1024);
InterruptController* ic = new BasicInterruptController(*mb);
mb->setInterruptController(ic);
mb->setExceptionReport(motherboardException);
/* Initialize dynamic library loader */
DlAdapter* dlLoader = static_cast<DlAdapter*>( DlAdapter::getInstance() );
assert(dlLoader);
/* Create CPUs from dynamically linked libraries */
for (int i = 0; i < NUM_CPUS; ++i)
{
Cpu* cpu = dynamic_cast<Cpu*>(
dlLoader->loadDevice("basiccpu/" + DlAdapter::getLibraryName("basiccpu"), *mb, 0)
);
if (cpu)
mb->addCpu(cpu, i == 0);
else
throw runtime_error("Could not load cpu device");
}
/* Initialize interval timer */
TimerDevice* timer = new TimerDevice;
mb->addDevice(timer);
/* Initialize guest-to-host display output device */
DisplayDeviceArgs ddargs;
if (options.graphics)
ddargs.displayManager = new X11DisplayManager;
else
ddargs.displayManager = new NullDisplayManager;
Device* displayDevice = dynamic_cast<Device*>(
dlLoader->loadDevice("dev/" + DlAdapter::getLibraryName("displaydevice"), *mb, &ddargs)
);
if (displayDevice)
mb->addDevice(displayDevice);
else
throw runtime_error("Could not load display device");
/* Initialize guest-to-host character output device */
Device* charOutputDevice = dynamic_cast<Device*>(
dlLoader->loadDevice("dev/" + DlAdapter::getLibraryName("charoutputdevice"), *mb, 0)
);
if (charOutputDevice)
mb->addDevice(charOutputDevice);
else
throw runtime_error("Could not load character output device");
/* read bios */
uint8_t* bios;
int biosSize;
readFile("basiccpu/bios", bios, biosSize);
// convert to vector
vector<uint8_t> biosProgram;
biosProgram.reserve(biosSize);
biosProgram.insert(biosProgram.end(), bios, bios + biosSize);
// set motherboard bios
mb->setBios(biosProgram, 7000000);
/* Start the emulator */
if (mb->start());
else
fprintf(stderr, "Emulator aborted\n");
/* Cleanup */
delete ddargs.displayManager;
delete[] bios;
delete ic;
delete mb;
dlLoader->cleanup();
return 0;
}
// An abomination mix of C and C++; sry
void readFile(const char* filename, uint8_t*& contents, int& fileSize)
{
FILE* file = fopen(filename, "rb");
if (!file)
throw runtime_error("Could not open bios file");
fileSize = getFileLength(file);
contents = new uint8_t[fileSize];
size_t numItems = fread(contents, fileSize, 1, file);
fclose(file);
if (numItems < 1)
throw runtime_error("Failed to read bios file");
}
int getFileLength(FILE* file)
{
// Save current position
int fpi = ftell(file);
fseek(file, 0, SEEK_END);
int length = ftell(file);
// Return fpi
fseek(file, fpi, SEEK_SET);
return length;
}