forked from bcd/exec09
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.c
448 lines (379 loc) · 9.84 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright 2001 by Arto Salmi and Joze Fabcic
* Copyright 2006-2008 by Brian Dominy <brian@oddchange.com>
*
* This file is part of GCC6809.
*
* GCC6809 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GCC6809 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GCC6809; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include "6809.h"
#include "command.h"
#include "symtab.h"
#include "machine.h"
#include "monitor.h"
/* The total number of cycles that have executed */
unsigned long total = 0;
/* The frequency of the emulated CPU, in megahertz */
unsigned int mhz = 1;
/* When nonzero, indicates that the machine's tick routine should be
triggered periodically, every so many cycles. Typically this is
used by the machine to generate a timer interrupt. Off By default.
*/
unsigned int cycles_per_tick = 0;
/* Nonzero if debugging support is turned on */
int debug_enabled = 0;
/* Nonzero if tracing is enabled */
int trace_enabled = 0;
/* Nonzero if SWI2 should be reported with a postbyte */
int os9call = 0;
/* When nonzero, causes the program to print the total number of cycles
on a successful exit. */
int dump_cycles_on_success = 0;
/* When nonzero, indicates the total number of cycles before an automated
exit. This is to help speed through test cases that never finish. */
int max_cycles = INT_MAX;
/* When nonzero, says that the state of the machine is persistent
across runs of the simulator. */
int machine_persistent = 0;
/* When nonzero, says that the simulator is slowed down to match what a real
processor would run like. */
int machine_realtime = 0;
static int binary = 0;
char *exename;
const char *machine_name = "simple";
const char *prog_name = NULL;
FILE *stat_file = NULL;
struct timeval time_started;
/**
* Return elapsed real time in milliseconds.
*/
long
time_diff (struct timeval *old, struct timeval *new)
{
long ms = (new->tv_usec - old->tv_usec) / 1000;
ms += (new->tv_sec - old->tv_sec) * 1000;
return ms;
}
long
get_elapsed_realtime (void)
{
struct timeval now;
gettimeofday (&now, NULL);
return time_diff (&time_started, &now);
}
/*
* Check if the CPU should idle.
*/
void
idle_loop (void)
{
struct timeval now;
static struct timeval last = { 0, 0 };
int real_ms;
static unsigned long last_cycles = 0;
unsigned long cycles;
int sim_ms;
const int cycles_per_ms = 2000;
static int period = 30;
static int count = 30;
int delay;
static int total_ms_elapsed = 0;
static int cumulative_delay = 0;
if (--count > 0)
return;
if (last.tv_sec == 0 && last.tv_usec == 0)
gettimeofday (&last, NULL);
gettimeofday (&now, NULL);
real_ms = time_diff (&last, &now);
last = now;
cycles = get_cycles ();
sim_ms = (cycles - last_cycles) / cycles_per_ms;
if (sim_ms < 0)
sim_ms += cycles_per_ms;
last_cycles = cycles;
total_ms_elapsed += sim_ms;
if (total_ms_elapsed > 100)
{
total_ms_elapsed -= 100;
if (machine->periodic) machine->periodic ();
command_periodic ();
}
delay = sim_ms - real_ms;
cumulative_delay += delay;
if (cumulative_delay > 0)
{
usleep (50 * 1000UL);
cumulative_delay -= 50;
}
count = period;
}
int do_help (const char *arg __attribute__((unused)));
#define NO_NEG 0
#define HAS_NEG 1
#define NO_ARG 0
#define HAS_ARG 1
struct option
{
char o_short;
const char *o_long;
const char *help;
unsigned int can_negate : 1;
unsigned int takes_arg : 1;
int *int_value;
int default_value; /* value to set if option is present */
const char **string_value;
int (*handler) (const char *arg);
} option_table[] = {
{ 'd', "debug", "Enter the monitor immediately",
HAS_NEG, NO_ARG, &debug_enabled, 1, NULL, NULL },
{ 'h', "help", NULL,
NO_NEG, NO_ARG, NULL, 0, 0, do_help },
{ 'b', "binary", "",
NO_NEG, NO_ARG, &binary, 1, NULL, NULL },
{ 'M', "mhz", "", NO_NEG, HAS_ARG },
{ '-', "68a09", "Emulate the 68A09 variation (1.5Mhz)" },
{ '-', "68b09", "Emulate the 68B09 variation (2Mhz)" },
{ 'R', "realtime", "Limit simulation speed to match realtime",
HAS_NEG, NO_ARG, &machine_realtime, 0, NULL, NULL },
{ 'I', "tIckfreq", "Automatically calls the machine's tick every so many cycles",
NO_NEG, HAS_ARG, &cycles_per_tick, 0, NULL, NULL },
{ 'C', "cycledump", "",
HAS_NEG, NO_ARG, &dump_cycles_on_success, 1, NULL, NULL},
{ 'o', "os9call", "Treat SWI2 as an OS9/NitrOS9 system call and report postbyte",
HAS_NEG, NO_ARG, &os9call, 1, NULL, NULL},
{ 't', "loadmap", "" },
{ 'T', "trace", "",
NO_NEG, NO_ARG, &trace_enabled, 1, NULL, NULL },
{ 'm', "maxcycles", "Set maximum number of cycles to run (0 to disable)",
NO_NEG, HAS_ARG, &max_cycles, 0, NULL, NULL },
{ 's', "machine", "Specify the machine (exact hardware) to emulate",
NO_NEG, HAS_ARG, NULL, 0, &machine_name, NULL },
{ 'p', "persistent", "Use persistent machine state",
NO_NEG, NO_ARG, &machine_persistent, 1, NULL, NULL },
{ '\0', NULL },
};
int
do_help (const char *arg __attribute__((unused)))
{
struct option *opt = option_table;
printf ("Motorola 6809 Simulator Version %s\n", PACKAGE_VERSION);
printf ("m6809-run [options] [program]\n\n");
printf ("Options:\n");
while (opt->o_long != NULL)
{
if (opt->help)
{
if (opt->o_short == '-')
printf (" --%-16.16s %s\n", opt->o_long, opt->help);
else
printf (" -%c, --%-16.16s%s\n", opt->o_short, opt->o_long, opt->help);
}
opt++;
}
exit (0);
}
void usage (void)
{
do_help (NULL);
}
/**
* Returns positive if an argument was taken.
* Returns zero if no argument was taken.
* Returns negative on error.
*/
int
process_option (struct option *opt, const char *arg)
{
int rc;
//printf ("Processing option '%s'\n", opt->o_long);
if (opt->takes_arg)
{
if (!arg)
{
//printf (" Takes argument but none given.\n");
rc = 0;
}
else
{
if (opt->int_value)
{
*(opt->int_value) = strtoul (arg, NULL, 0);
//printf (" Integer argument '%d' taken.\n", *(opt->int_value));
}
else if (opt->string_value)
{
*(opt->string_value) = arg;
//printf (" String argument '%s' taken.\n", *(opt->string_value));
}
rc = 1;
}
}
else
{
if (arg)
{
//printf (" Takes no argument but one given, ignored.\n");
}
if (opt->int_value)
{
*(opt->int_value) = opt->default_value;
//printf (" Integer argument 1 implied.\n");
}
rc = 0;
}
if (opt->handler)
{
rc = opt->handler (arg);
//printf (" Handler called, rc=%d\n", rc);
}
if (rc < 0)
sim_exit (0x70);
return rc;
}
void
process_plain_argument (const char *arg)
{
//printf ("plain argument '%s'\n", arg);
if (!prog_name)
prog_name = arg;
}
void
parse_args (int argc, char *argv[])
{
int argn = 1;
struct option *opt;
next_arg:
while (argn < argc)
{
char *arg = argv[argn];
if (arg[0] == '-')
{
if (arg[1] == '-')
{
char *rest = strchr (arg+2, '=');
if (rest)
*rest++ = '\0';
opt = option_table;
while (opt->o_long != NULL)
{
if (!strcmp (opt->o_long, arg+2))
{
argn++;
(void)process_option (opt, rest);
goto next_arg;
}
opt++;
}
printf ("long option '%s' not recognized.\n", arg+2);
}
else
{
opt = option_table;
while (opt->o_long != NULL)
{
if (opt->o_short == arg[1])
{
argn++;
if (process_option (opt, argv[argn]))
argn++;
goto next_arg;
}
opt++;
}
printf ("short option '%c' not recognized.\n", arg[1]);
}
argn++;
}
else
{
process_plain_argument (argv[argn++]);
}
}
}
int
main (int argc, char *argv[])
{
gettimeofday (&time_started, NULL);
exename = argv[0];
/* TODO - enable different options by default
based on the executable name. */
parse_args (argc, argv);
sym_init ();
if (binary)
{
machine_init (machine_name, prog_name);
}
else
{
/* The machine loader cannot deal with image files,
so initialize the machine first, passing it a NULL
filename, then load the image file afterwards. */
machine_init (machine_name, NULL);
if (prog_name && load_image (prog_name))
usage ();
}
/* Try to load a map file */
if (prog_name)
load_map_file (prog_name);
/* Enable debugging if no executable given yet. */
if (!prog_name)
{
debug_enabled = 1;
}
else
{
/* OK, ready to run. Reset the CPU first. */
cpu_reset ();
}
monitor_init ();
command_init ();
keybuffering_defaults ();
keybuffering (0);
/* Now, iterate through the instructions.
Without -I, we can just call cpu_execute() and let it run
for a long time; otherwise, we need to come back here
periodically and call the machine's ->tick() routine */
//[NAC HACK 2017Mar30] need to schedule this properly instead of this one-or-the-other approach
//.. need to track the rate of each and work out who's next.
for (cpu_quit = 1; cpu_quit != 0;)
{
/* Call each device that needs periodic processing. */
machine_update ();
if (cycles_per_tick == 0)
{
/* Simulate some CPU time, either 1ms worth or up to the
next possible tick */
total += cpu_execute (mhz * 1024);
}
else
{
total += cpu_execute (cycles_per_tick);
if (machine->tick) machine->tick ();
}
idle_loop ();
/* Check for a rogue program that won't end */
if ((max_cycles > 0) && (total > max_cycles))
{
sim_error ("maximum cycle count exceeded at %s\n",
monitor_addr_name (get_pc ()));
}
}
sim_exit (0);
keybuffering (1);
return 0;
}