forked from payonel/ocvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
212 lines (185 loc) · 4.94 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
203
204
205
206
207
208
209
210
211
212
#include <string>
#include <memory>
#include "model/host.h"
#include "model/client.h"
#include "io/frame.h"
#include "model/log.h"
#include "components/computer.h"
#include "drivers/fs_utils.h"
using std::cerr;
void usage()
{
cerr << "ocvm [ENV_PATH] [OPTIONS]\n"
" ENV_PATH (optional) VM env path. Default ./tmp\n"
"OPTIONS\n"
" --frame=TYPE Term emulator type. Can be 'ansi' (default) or 'basic'.\n"
" --log-allocs[=PATH] Enable logging mallocs and stacks.\n"
" Optional custom path, default stack.log\n"
" --bios=PATH Path to custom eeprom bios code\n"
" --machine=PATH Path to custom machine.lua\n"
" --fonts=PATH Path to custom fonts.hex\n";
::exit(1);
}
struct Args
{
vector<string> indexed;
map<string, string> named;
enum
{
LogAllocKey = 0,
FrameKey,
BiosKey,
MachineKey,
FontsKey
};
const string keys[FontsKey+1] =
{
"log-allocs",
"frame",
"bios",
"machine",
"fonts"
};
string get(int n) const
{
n--;
if (n < 0 || n >= static_cast<int>(indexed.size()))
return "";
return indexed.at(n);
}
string get(string name) const
{
const auto& it = named.find(name);
if (it == named.end())
return "";
return it->second;
}
bool valid_key(const string& key)
{
if (key == "help") // no error message, but report usage
return false;
for (size_t i = 0; i < sizeof(keys); i++)
{
if (key == keys[i])
return true;
}
cerr << "bad argument [" << key << "]\n";
return false;
}
string defaults(const string& key)
{
if (key == keys[LogAllocKey])
return "stack.log";
return "";
}
string client_env_path() const
{
string value = get(1);
return value.empty() ? "tmp" : value;
}
string frame_type() const
{
string value = get(keys[Args::FrameKey]);
return value.empty() ? "ansi" : value;
}
string stack_log() const
{
return get(keys[Args::LogAllocKey]);
}
string bios_path() const
{
string value = get(keys[Args::BiosKey]);
return value.empty() ? fs_utils::make_proc_path("system/bios.lua") : value;
}
string machine_path() const
{
string value = get(keys[Args::MachineKey]);
return value.empty() ? fs_utils::make_proc_path("system/machine.lua") : value;
}
string fonts_path() const
{
string value = get(keys[Args::FontsKey]);
return value.empty() ? fs_utils::make_proc_path("system/font.hex") : value;
}
};
bool valid_arg_index(size_t size)
{
static size_t expected = 1;
if (size > expected)
{
cerr << "too many arguments, expected " << expected << endl;
}
return size <= 1;
}
Args load_args(int argc, char** argv)
{
Args args;
// start from 1, argv[0] is the prog name
for (int i = 1; i < argc; i++)
{
string t = argv[i];
if (t.find("--") == 0)
{
string key = t.substr(2);
string value;
size_t equal_index = key.find("=");
if (equal_index != string::npos)
{
value = key.substr(equal_index + 1);
key = key.substr(0, equal_index);
if (!args.valid_key(key))
{
usage();
}
args.named[key] = value;
}
else
{
string def = args.defaults(key);
if (def.empty())
usage();
args.named[key] = def;
}
}
else if (t.find("-") == 0)
usage();
else
{
args.indexed.push_back(t);
if (!valid_arg_index(args.indexed.size()))
{
usage();
}
}
}
return args;
}
int main(int argc, char** argv)
{
auto args = load_args(argc, argv);
string client_env_path = fs_utils::make_pwd_path(args.client_env_path());
// init host config
// // prepares component factories such as screen, keyboard, and filesystem
Host host(args.frame_type());
host.stackLog(args.stack_log());
host.biosPath(args.bios_path());
host.machinePath(args.machine_path());
host.fontsPath(args.fonts_path());
RunState run;
do
{
// init client config
Client client(&host, client_env_path);
// init lua environment
// // creates instances of host components
if (!client.load())
break;
do
{
run = client.run();
}
while (run == RunState::Continue);
}
while (run == RunState::Reboot);
return 0;
}