-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
275 lines (225 loc) · 5.82 KB
/
main.c
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
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#ifdef CPU_RELAX
#include <poll.h>
#endif /* CPU_RELAX */
#include "i8088.h"
#include "i8088_trace.h"
#include "mem.h"
#include "io.h"
#include "fe2010.h"
#include "mos5720.h"
#include "fdc9268.h"
#include "m6242.h"
#include "xthdc.h"
#include "i8250.h"
#include "dp8390.h"
#include "net.h"
#include "edfs.h"
#include "console.h"
#include "debugger.h"
#include "panic.h"
#define BIOS_ROM_FILENAME "rom/cbm-pc10sd-bios-v4.38-318085-05-C72A.bin"
#define BIOS_ROM_ADDRESS 0xF8000
static i8088_t cpu;
static mem_t mem;
static io_t io;
static fe2010_t fe2010;
static mos5720_t mos5720;
static fdc9268_t fdc9268;
static m6242_t m6242;
static xthdc_t xthdc;
static i8250_t i8250;
static dp8390_t dp8390;
static net_t net;
static bool debugger_break = false;
static char panic_msg[80];
void panic(const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(panic_msg, sizeof(panic_msg), format, args);
va_end(args);
debugger_break = true;
}
static void sig_handler(int sig)
{
switch (sig) {
case SIGINT:
debugger_break = true;
return;
}
}
static void display_help(const char *progname)
{
fprintf(stdout, "Usage: %s <options>\n", progname);
fprintf(stdout, "Options:\n"
" -h Display this help.\n"
" -d Break into debugger on start.\n"
" -a FILE Load floppy image FILE into floppy drive A:\n"
" -b FILE Load floppy image FILE into floppy drive B:\n"
" -w FILE Load hard disk image FILE for C:\n"
" -s SPT Override SPT sectors-per-track for floppy images.\n"
" -r FILE Use FILE for BIOS ROM instead of the default.\n"
" -x ADDR Load BIOS ROM at (hex) ADDR instead of the default.\n"
" -t TTY Passthrough COM1 to TTY device.\n"
" -e DIR Serve EtherDFS requests from DIR root.\n"
"\n");
fprintf(stdout,
"Default BIOS ROM '%s' @ 0x%05x\n", BIOS_ROM_FILENAME, BIOS_ROM_ADDRESS);
fprintf(stdout,
"Using Ctrl+C will break into debugger, use 'q' from there to quit.\n\n");
}
int main(int argc, char *argv[])
{
int c;
uint32_t cycle;
char *bios_rom_filename = BIOS_ROM_FILENAME;
uint32_t bios_rom_address = BIOS_ROM_ADDRESS;
char *floppy_a_image = NULL;
char *floppy_b_image = NULL;
char *hard_disk_image = NULL;
char *tty_device = NULL;
char *edfs_root = NULL;
int floppy_image_spt = 0;
panic_msg[0] = '\0';
signal(SIGINT, sig_handler);
while ((c = getopt(argc, argv, "hda:b:w:s:r:x:t:e:")) != -1) {
switch (c) {
case 'h':
display_help(argv[0]);
return EXIT_SUCCESS;
case 'd':
debugger_break = true;
break;
case 'a':
floppy_a_image = optarg;
break;
case 'b':
floppy_b_image = optarg;
break;
case 'w':
hard_disk_image = optarg;
break;
case 's':
floppy_image_spt = atoi(optarg);
break;
case 'r':
bios_rom_filename = optarg;
break;
case 'x':
sscanf(optarg, "%x", &bios_rom_address);
break;
case 't':
tty_device = optarg;
break;
case 'e':
edfs_root = optarg;
break;
case '?':
default:
display_help(argv[0]);
return EXIT_FAILURE;
}
}
i8088_trace_init();
i8088_init(&cpu, &io);
mem_init(&mem);
io_init(&io);
fe2010_init(&fe2010, &io, &cpu, &mem);
mos5720_init(&mos5720, &io, &fe2010);
fdc9268_init(&fdc9268, &io, &fe2010);
m6242_init(&m6242, &io);
net_init(&net);
dp8390_init(&dp8390, &io, &fe2010, &net);
if (tty_device) {
if (i8250_init(&i8250, &io, &fe2010, &mos5720, tty_device) != 0) {
return EXIT_FAILURE;
}
}
if (edfs_root) {
edfs_init(edfs_root);
}
console_init(&io);
if (mem_load_rom(&mem, bios_rom_filename, bios_rom_address) != 0) {
return EXIT_FAILURE;
}
if (floppy_a_image) {
if (fdc9268_image_load(&fdc9268, 0, floppy_a_image,
floppy_image_spt) != 0) {
return EXIT_FAILURE;
}
}
if (floppy_b_image) {
if (fdc9268_image_load(&fdc9268, 1, floppy_b_image,
floppy_image_spt) != 0) {
return EXIT_FAILURE;
}
}
if (hard_disk_image) {
xthdc_init(&xthdc, &io, &fe2010);
if (xthdc_image_load(&xthdc, hard_disk_image) != 0) {
return EXIT_FAILURE;
}
}
cycle = 0;
i8088_reset(&cpu);
while (1) {
i8088_execute(&cpu, &mem);
fe2010_execute(&fe2010);
if ((cycle % 10000) == 0) {
console_execute_keyboard(&fe2010, &mos5720);
console_execute_screen(&mem);
net_execute(&net);
dp8390_execute(&dp8390);
}
if (tty_device) {
if ((cycle % 100) == 0) {
i8250_execute(&i8250);
}
}
#ifdef CPU_RELAX
/* Check if BIOS int16h gets called for keyboard services. */
if (cpu.cs == (mem.m[0x5A] + (mem.m[0x5B] * 0x100)) &&
cpu.ip == (mem.m[0x58] + (mem.m[0x59] * 0x100))) {
console_execute_screen(&mem);
struct pollfd fds[1];
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;
#if CPU_RELAX == CPM
/* CP/M-86 calls with AH=0 and waits indefinitely. */
if (cpu.ah == 0) {
while (poll(fds, 1, 10) == 0);
}
#else /* CPU_RELAX == DOS */
/* DOS typically calls with AH=1 to poll once in while. */
poll(fds, 1, 1);
#endif
}
#endif /* CPU_RELAX */
#ifdef BREAKPOINT
if (cpu.ip == debugger_breakpoint_ip) {
if (cpu.cs == debugger_breakpoint_cs || debugger_breakpoint_cs == -1) {
debugger_break = true;
}
}
#endif /* BREAKPOINT */
if (debugger_break) {
console_pause();
if (panic_msg[0] != '\0') {
fprintf(stdout, "%s", panic_msg);
panic_msg[0] = '\0';
}
debugger_break = debugger(&cpu, &mem, &fe2010, &fdc9268, &xthdc);
if (! debugger_break) {
console_resume();
}
}
cycle++;
}
return EXIT_SUCCESS;
}