forked from shift-crops/x86emu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
203 lines (179 loc) · 4.08 KB
/
main.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
#include <thread>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <GLFW/glfw3.h>
#include "common.hpp"
#include "instruction/base.hpp"
#include "emulator/emulator.hpp"
#include "emulator/exception.hpp"
#define MEMORY_SIZE (4*MB)
struct Setting {
size_t mem_size;
const char *image_name;
uint32_t load_addr;
size_t load_size;
bool ui_enable;
bool ui_full;
bool ui_vm;
};
void run_emulator(const Setting set);
void help(const char *name);
__attribute__((constructor))
void init(void){
setbuf(stdout, NULL);
setbuf(stderr, NULL);
}
int main(int argc, char *argv[]){
Setting set = {
.mem_size = MEMORY_SIZE,
.image_name = "sample/kernel.img",
.load_addr = 0x0,
.load_size = (size_t)-1,
.ui_enable = true,
.ui_full = false,
.ui_vm = false,
};
char opt;
struct option long_options[] = {
{"memory", required_argument, NULL, 1},
{"load_addr", required_argument, NULL, 3},
{"load_size", required_argument, NULL, 4},
{"full", no_argument, NULL, 5},
{"VM", no_argument, NULL, 6},
{"no_graphic", no_argument, NULL, 7},
{"help", no_argument, NULL, 'h'},
{0, 0, 0, 0}};
while((opt=getopt_long(argc, argv, DEBUG_OPT"m:z:a:s:h", long_options, NULL)) != -1){
switch(opt){
uint32_t v;
case 'm':
case 1:
v = atoi(optarg);
if(v)
set.mem_size = v*MB;
else
WARN("memory size is zero");
break;
case 'a':
case 3:
set.load_addr = strtol(optarg, NULL, 0);
break;
case 's':
case 4:
set.load_size = strtol(optarg, NULL, 0);
break;
case 5:
set.ui_full = true;
break;
case 6:
set.ui_vm = true;
break;
case 7:
set.ui_enable = false;
break;
#ifdef DEBUG
case 'v':
set_debuglv(optarg);
break;
#endif
case 'h':
case '?':
help(argv[0]);
}
}
if(argc > optind)
set.image_name = argv[optind];
/*
std::thread th1 = std::thread(set);
std::thread th2 = std::thread(set);
th1.join();
th2.join();
*/
run_emulator(set);
}
void run_emulator(const Setting set){
EmuSetting emuset = {
.mem_size = set.mem_size,
.uiset = {
.enable = set.ui_enable,
.full = set.ui_full,
.vm = set.ui_vm,
},
};
Emulator emu = Emulator(emuset);
InstrData instr;
Instr16 instr16(&emu, &instr);
Instr32 instr32(&emu, &instr);
if(!emu.insert_floppy(0, set.image_name, false)){
WARN("cannot load image '%s'", set.image_name);
return;
}
emu.load_binary("bios/bios.bin", 0xf0000, 0, 0x2800);
emu.load_binary("bios/crt0.bin", 0xffff0, 0, 0x10);
if(set.load_addr)
emu.load_binary(set.image_name, set.load_addr, 0x200, set.load_size);
//while(!emu.is_halt()){
//while(true){
while(emu.is_running()){
bool is_mode32;
uint8_t prefix;
bool chsz_op, chsz_ad;
memset(&instr, 0, sizeof(InstrData));
try{
if(emu.chk_irq()) emu.do_halt(false);
if(emu.is_halt()){
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
emu.hundle_interrupt();
is_mode32 = emu.is_mode32();
prefix = (is_mode32 ? instr32.parse_prefix() : instr16.parse_prefix());
chsz_op = prefix & CHSZ_OP;
chsz_ad = prefix & CHSZ_AD;
if(is_mode32 ^ chsz_op){
instr32.set_chsz_ad(!(is_mode32 ^ chsz_ad));
instr32.parse();
instr32.exec();
}
else{
instr16.set_chsz_ad(is_mode32 ^ chsz_ad);
instr16.parse();
instr16.exec();
}
}
catch(exception_t n){
emu.queue_interrupt(n, true);
//INFO(3, "Exception %d", n);
ERROR("Exception %d", n);
}
catch(...){
emu.dump_regs();
emu.stop();
}
}
}
__attribute__((noreturn))
void help(const char *name){
MSG( "Simple x86 Emulator\n\n"
"Usage :\n"
"\t%s [options] <disk image>\n\n", name);
MSG( "Options : \n"
"\t-m MB, --memory=MB\n"
"\t\tMemory size (MB)\n\n"
"\t-a ADDR, --load_addr=ADDR\n"
"\t\tAddress to preload disk image\n\n"
"\t-s SIZE, --load_size=SIZE\n"
"\t\tSize to load (Enabled when 'load_addr' is specified)\n\n"
"\t--full\n"
"\t\tfull screen\n\n"
"\t--VM\n"
"\t\trunning on VMware or VirtualBox\n\n"
#ifdef DEBUG
"\t-v [1..4]\n"
"\t\tverbose level\n\n"
#endif
"\t-h, --help\n"
"\t\tshow this help\n");
_exit(0);
}