-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cs
329 lines (287 loc) · 9.32 KB
/
main.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using DotXT;
string test = "";
TMode mode = TMode.NotSet;
bool intercept_int = false;
ushort initial_cs = 0;
ushort initial_ip = 0;
bool set_initial_ip = false;
bool load_bios = true;
uint load_test_at = 0xffffffff;
bool debugger = false;
bool prompt = true;
for(int i=0; i<args.Length; i++)
{
if (args[i] == "-h") {
Console.WriteLine("-t file load 'file' in RAM");
Console.WriteLine("-T addr sets the load-address for -t");
Console.WriteLine("-x type set type for -T: floppy, binary, blank");
Console.WriteLine("-l file log to file");
Console.WriteLine("-i intercept some of the BIOS calls");
Console.WriteLine("-B disable loading of the BIOS ROM images");
Console.WriteLine("-d enable debugger");
Console.WriteLine("-P skip prompt");
Console.WriteLine("-o cs,ip start address (in hexadecimal)");
System.Environment.Exit(0);
}
else if (args[i] == "-t")
test = args[++i];
else if (args[i] == "-T")
load_test_at = (uint)Convert.ToInt32(args[++i], 16);
else if (args[i] == "-x") {
string type = args[++i];
if (type == "floppy")
mode = TMode.Floppy;
else if (type == "binary")
mode = TMode.Binary;
else if (type == "blank")
mode = TMode.Blank;
else {
Console.WriteLine($"{type} is not understood");
System.Environment.Exit(1);
}
}
else if (args[i] == "-l")
Log.SetLogFile(args[++i]);
else if (args[i] == "-i")
intercept_int = true;
else if (args[i] == "-B")
load_bios = false;
else if (args[i] == "-d")
debugger = true;
else if (args[i] == "-P")
prompt = false;
else if (args[i] == "-o")
{
string[] parts = args[++i].Split(',');
initial_cs = (ushort)Convert.ToInt32(parts[0], 16);
initial_ip = (ushort)Convert.ToInt32(parts[1], 16);
set_initial_ip = true;
}
else
{
Console.WriteLine($"{args[i]} is not understood");
System.Environment.Exit(1);
}
}
if (test == "")
{
Console.WriteLine("DotXT, (C) 2023 by Folkert van Heusden");
Console.WriteLine("Released in the public domain");
}
#if DEBUG
Console.WriteLine("Debug mode");
#endif
List<Device> devices = new();
if (mode != TMode.Blank)
{
devices.Add(new MDA());
devices.Add(new CGA());
devices.Add(new i8253());
devices.Add(new FloppyDisk());
devices.Add(new Keyboard());
}
uint ram_size = 256 * 1024;
if (test != "")
ram_size = 640 * 1024;
if (mode == TMode.Blank)
ram_size = 1024 * 1024;
// Bus gets the devices for memory mapped i/o
Bus b = new Bus(ram_size, load_bios, ref devices);
var p = new P8086(ref b, test, mode, load_test_at, intercept_int, !debugger, ref devices);
if (set_initial_ip)
p.set_ip(initial_cs, initial_ip);
if (debugger)
{
bool echo_state = true;
Log.EchoToConsole(echo_state);
for(;;)
{
if (prompt)
Console.Write("==>");
string line = Console.ReadLine();
//Log.DoLog(line);
string[] parts = line.Split(' ');
if (line == "s")
p.Tick();
else if (line == "S")
{
do {
p.Tick();
}
while(p.IsProcessingRep());
}
else if (line == "q")
break;
else if (line == "echo")
{
echo_state = !echo_state;
Console.WriteLine(echo_state ? "echo on" : "echo off");
Log.EchoToConsole(echo_state);
}
else if (parts[0] == "ef")
{
if (parts.Length != 2)
Console.WriteLine("usage: ef 0xhex_address");
else
{
int address = Convert.ToInt32(parts[1], 16);
Console.WriteLine($"{address:X6} {p.HexDump((uint)address, false)}");
}
}
else if (parts[0] == "dolog")
{
Log.DoLog(line);
}
else if (parts[0] == "reset")
{
b.ClearMemory();
}
else if (parts[0] == "set")
{
if (parts.Length != 4)
Console.WriteLine("usage: set [reg|ram] [regname|address] value");
else if (parts[1] == "reg")
{
string regname = parts[2];
ushort value = (ushort)Convert.ToInt32(parts[3], 10);
try
{
if (regname == "ax")
p.SetAX(value);
else if (regname == "bx")
p.SetBX(value);
else if (regname == "cx")
p.SetCX(value);
else if (regname == "dx")
p.SetDX(value);
else if (regname == "ss")
p.SetSS(value);
else if (regname == "cs")
p.SetCS(value);
else if (regname == "ds")
p.SetDS(value);
else if (regname == "es")
p.SetES(value);
else if (regname == "sp")
p.SetSP(value);
else if (regname == "bp")
p.SetBP(value);
else if (regname == "si")
p.SetSI(value);
else if (regname == "di")
p.SetDI(value);
else if (regname == "ip")
p.SetIP(value);
else if (regname == "flags")
p.SetFlags(value);
else
{
Console.WriteLine($"Register {regname} not known");
continue;
}
Console.WriteLine($"<SET {regname} {value}");
}
catch (Exception e)
{
Console.WriteLine($"<SET -1 -1 FAILED {e}");
}
}
else if (parts[1] == "ram" || parts[1] == "mem")
{
try
{
uint addr = (uint)Convert.ToInt32(parts[2], 10);
byte value = (byte)Convert.ToInt32(parts[3], 10);
b.WriteByte(addr, value);
Console.WriteLine($"<SET {addr} {value}");
}
catch (Exception e)
{
Console.WriteLine($"<SET -1 -1 FAILED {e}");
}
}
}
else if (parts[0] == "get")
{
if (parts.Length != 3)
Console.WriteLine("usage: get [reg|ram] [regname|address]");
else if (parts[1] == "reg")
{
try
{
string regname = parts[2];
ushort value = 0;
if (regname == "ax")
value = p.GetAX();
else if (regname == "bx")
value = p.GetBX();
else if (regname == "cx")
value = p.GetCX();
else if (regname == "dx")
value = p.GetDX();
else if (regname == "ss")
value = p.GetSS();
else if (regname == "cs")
value = p.GetCS();
else if (regname == "ds")
value = p.GetDS();
else if (regname == "es")
value = p.GetES();
else if (regname == "sp")
value = p.GetSP();
else if (regname == "bp")
value = p.GetBP();
else if (regname == "si")
value = p.GetSI();
else if (regname == "di")
value = p.GetDI();
else if (regname == "ip")
value = p.GetIP();
else if (regname == "flags")
value = p.GetFlags();
else
{
Console.WriteLine($"Register {regname} not known");
continue;
}
Console.WriteLine($">GET {regname} {value}");
}
catch (Exception e)
{
Console.WriteLine($">GET -1 -1 FAILED {e}");
}
}
else if (parts[1] == "ram" || parts[1] == "mem")
{
try
{
uint addr = (uint)Convert.ToInt32(parts[2], 10);
ushort value = b.ReadByte(addr);
Console.WriteLine($">GET {addr} {value}");
}
catch (Exception e)
{
Console.WriteLine($">GET -1 -1 FAILED {e}");
}
}
}
else if (line == "c")
{
for (;;)
{
if (p.Tick() == false)
break;
}
}
else if (line != "")
{
Console.WriteLine($"\"{line}\" not understood");
}
Console.Out.Flush();
}
}
else
{
for (;;)
p.Tick();
}