-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemuz80.c
330 lines (289 loc) · 7.89 KB
/
emuz80.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
/*
* Fake mini machine to run compiler tests
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "libz80/z80.h"
#include "z80dis.h"
#include "exec.h"
#include "syscalls.h"
#include "mapfile.h"
#include "emumon.h"
// Now visible globally for syscalls.c
uint8_t ram[65536];
FILE *logfile=NULL;
char *mapfile = NULL;
char **Argv = NULL;
char *Emuname;
Z80Context cpu_z80;
// Default environment variables
static char *default_envp[] = {
"PATH=/bin:/usr/bin:.",
"HOME=/",
"TERM=vt100",
"USER=root",
NULL
};
uint8_t mem_read(int unused, uint16_t addr)
{
return ram[addr];
}
void mem_write(int unused, uint16_t addr, uint8_t val)
{
ram[addr] = val;
}
uint8_t io_read(int unused, uint16_t port)
{
return 0xFF;
}
/* Writes to certain ports act like system calls */
/* 0xFD: print out the 8-bit value as a char if printable, else hex */
/* 0xFF: exit() with the val as the exit value */
/* 0xFE: putchar(val) */
/* 0xFB/FC: print out the 16-bit value as a decimal */
static unsigned char fcval = 0;
void io_write(int unused, uint16_t port, uint8_t value)
{
int x;
switch(port & 0xFF) {
case 0xFD:
if (value < 32 || value > 127)
printf("\\x%02X", value);
else
putchar(value);
fflush(stdout);
return;
case 0xFF:
if (value)
fprintf(stderr, "***FAIL %d\n", value);
exit(value);
case 0xFE:
putchar(value);
break;
case 0xFC:
/* Make the value signed */
x = (fcval << 8) | value;
if (x > 0x8000)
x -= 0x10000;
printf("%d\n", x);
break;
case 0xFB:
fcval = value; /* Save high byte for now */
break;
default:
fprintf(stderr, "***BAD PORT %d\n", port);
exit(1);
}
}
static unsigned int nbytes;
uint8_t z80dis_byte(uint16_t addr)
{
uint8_t r = mem_read(0, addr);
if (logfile!=NULL)
fprintf(logfile, "%02X ", r);
nbytes++;
return r;
}
uint8_t z80dis_byte_quiet(uint16_t addr)
{
return mem_read(0, addr);
}
// Put the Z80 state into a buffer of size 80
void z80_state_tobuf(char *buf) {
snprintf(buf, 80, "[ %02X:%02X %04X %04X %04X %04X %04X %04X ]",
cpu_z80.R1.br.A, cpu_z80.R1.br.F,
cpu_z80.R1.wr.BC, cpu_z80.R1.wr.DE, cpu_z80.R1.wr.HL,
cpu_z80.R1.wr.IX, cpu_z80.R1.wr.IY, cpu_z80.R1.wr.SP);
}
static void z80_trace(unsigned unused)
{
static uint32_t lastpc = -1;
int offset;
char *sym;
char buf[256];
if (logfile==NULL)
return;
nbytes = 0;
/* Spot XXXR repeating instructions and squash the trace */
if (cpu_z80.M1PC == lastpc && z80dis_byte_quiet(lastpc) == 0xED &&
(z80dis_byte_quiet(lastpc + 1) & 0xF4) == 0xB0) {
return;
}
lastpc = cpu_z80.M1PC;
if (logfile!=NULL) {
// See if we have a symbol at this address
sym=NULL;
if (mapfile_loaded)
sym= get_symbol_and_offset(lastpc, &offset);
if (sym!=NULL)
fprintf(logfile, "%12s+%04X: ", sym, offset);
else
fprintf(logfile, "%04X: ", lastpc);
z80_disasm(buf, lastpc);
while(nbytes++ < 6)
fprintf(logfile, " ");
fprintf(logfile, "%-16s ", buf);
z80_state_tobuf(buf);
fprintf(logfile, "%s\n", buf);
}
}
/* FUZIX executable header */
static struct exec E;
/* Load an executable into memory */
/* and return the initial PC value */
static uint16_t load_executable(char *filename) {
int fd;
int cnt;
int loadaddr;
int bssend;
struct stat S;
/* Open the file */
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror(filename);
exit(1);
}
/* Get the file's size */
if (fstat(fd, &S) == -1) {
perror(filename);
exit(1);
}
/* Read in a possble FUZIX header */
cnt = read(fd, &E, sizeof(E));
if (cnt == sizeof(E)) {
/* Check the magic number and CPU */
if ((le16toh(E.a_magic) == EXEC_MAGIC) && (E.a_cpu == A_8080)
&& (E.a_cpufeat == AF_8080_Z80)) {
/* Determine the load address. */
/* N.B. Add on the entry size so we */
/* don't have to lseek back to the start */
loadaddr = (E.a_base << 8) + E.a_entry;
/* Determine the first address after the BSS */
/* XXX Little-endian so lo/hi swapped. Should fix this somehow */
bssend = (E.a_endlo << 8) + E.a_endhi + 1;
set_initial_brk(bssend);
// printf("Set initial brk to 0x%x\n", bssend);
/* Now read in the rest of the file */
cnt = read(fd, &ram[loadaddr], 0xfff0);
close(fd);
return(loadaddr);
}
}
/* It's not a FUZIX binary. Instead, exec it as a native binary. */
/* envp[] is the one the emulator's main() got */
execv(filename, Argv);
fprintf(stderr, "Failed to exec native binary %s\n", filename);
exit(1);
}
void usage(char *name) {
fprintf(stderr, "Usage: %s [-M] [-d logfile] [-m mapfile] [-b addr] executable <arguments>\n\n", name);
fprintf(stderr, "\t-d: write debugging information to logfile\n");
fprintf(stderr, "\t-m: load a mapfile with symbol information\n");
fprintf(stderr, "\t-M: start in the monitor\n");
fprintf(stderr, "\t-b: set breakpoint at address (decimal or $hex)\n\n");
fprintf(stderr, "\tIf the FUZIXROOT environment variable is set,\n");
fprintf(stderr, "\tuse that as the executable's root directory.\n");
exit(1);
}
int main(int argc, char *argv[])
{
int fd, pc, sp, opt;
int start_in_monitor=0;
char *fuzixroot;
char **brkstr; // Array of breakpoint strings
int i, brkcnt=0;
int breakpoint;
if (argc<2) usage(argv[0]);
Emuname=argv[0];
// Create an array to hold any breakpoint string pointers
brkstr= (char **)malloc(argc * sizeof(char *));
if (brkstr==NULL) exit(1);
// Initialise the monitor before we try to set
// up any command-line breakpoints
// monitor_init();
while ((opt = getopt(argc, argv, "+d:m:Mb:")) != -1) {
switch (opt) {
case 'd':
logfile= fopen(optarg, "w+");
if (logfile==NULL) {
fprintf(stderr, "Unable to open %s\n", optarg); exit(1);
}
break;
case 'm':
mapfile = optarg;
read_mapfile(mapfile);
break;
case 'M':
start_in_monitor=1;
break;
case 'b':
// Cache the pointer for now
brkstr[brkcnt++]= optarg;
break;
default:
usage(argv[0]);
}
}
// Clear the memory
memset(ram, 0, 0x10000);
// Load the executable file
Argv= &argv[optind];
pc=load_executable(argv[optind]);
// If we didn't load a map file, append
// ".map" to the executable filename.
// If that file exists, load that as a
// map file.
if (mapfile_loaded==0) {
mapfile= (char *)malloc(strlen(argv[optind]) + 4);
if (mapfile != NULL) {
strcpy(mapfile, argv[optind]);
strcat(mapfile, ".map");
fd= open(mapfile, O_RDONLY);
if (fd!=-1) {
close(fd);
read_mapfile(mapfile);
}
free(mapfile);
}
}
// Now that we might have a map file,
// parse any breakpoint strings and set them
for (i=0; i<brkcnt; i++) {
breakpoint= parse_addr(brkstr[i], NULL);
if (breakpoint != -1)
set_breakpoint(breakpoint, BRK_INST);
}
// Put the args and envp on the stack.
// Start the stack below the emulator special locations.
sp= set_arg_env(0xFFFF, &argv[optind], default_envp);
// If we have a FUZIXROOT environment variable,
// use that as the executable's root directory.
fuzixroot= getenv("FUZIXROOT");
if (fuzixroot != NULL)
set_fuzix_root(fuzixroot);
else
set_fuzix_root("");
// Reset the CPU state. Set the stack pointer at the arguments.
Z80RESET(&cpu_z80);
cpu_z80.PC= pc;
cpu_z80.R1.wr.SP= sp;
cpu_z80.ioRead = io_read;
cpu_z80.ioWrite = io_write;
cpu_z80.memRead = mem_read;
cpu_z80.memWrite = mem_write;
cpu_z80.trace = z80_trace;
// Start in the monitor if needed
if (start_in_monitor) {
pc= monitor(pc);
// Change the start address if the monitor says so
if (pc!=-1)
cpu_z80.PC= pc;
}
while(1)
Z80ExecuteTStates(&cpu_z80, 1000);
}