-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
93 lines (81 loc) · 1.76 KB
/
main.c
File metadata and controls
93 lines (81 loc) · 1.76 KB
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
#include <stdio.h>
#include <string.h>
#include "e652.h"
#include "disasm.h"
/*
* Execution loop.
*/
int exec_loop (void);
/*
* Just load the ROM anywhere in memory.
*/
int loadrom (byte *mem, int offset, char *path);
/*
* Print the last value of registers.
*/
void dump_state (void);
/*
* Memory.
*/
static byte emu_mem[MLEN];
int main (int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: %s ROM\n", argv[0]);
return 1;
}
memset(emu_mem, 0, MLEN);
if (loadrom(emu_mem, 0, argv[1]) != 0)
return -1;
E.m = emu_mem;
e652_reset();
E.PC = 0x400;
exec_loop();
return 0;
}
int exec_loop (void)
{
int v;
for (;;) {
printf("A=%02X X=%02X Y=%02X S=%02X P=%02X: ",
E.A, E.X, E.Y, E.S, E.P);
print_disasm(E.m + E.PC, E.PC);
v = e652_execnext();
if (v == H_DBUG) {
dump_state();
return v;
}
}
}
int loadrom (byte *mem, int offset, char *path)
{
FILE *fp = fopen(path, "rb");
if (!fp) {
perror("e652: failed to open ROM");
return -1;
}
fread(mem + offset, 1, MLEN - offset, fp);
fclose(fp);
return 0;
}
void dump_state (void)
{
printf("[state dump]\n");
printf(" PC 0x%04X %d\n", E.PC, E.PC);
printf(" A 0x%02X %d\n", E.A, E.A);
printf(" X 0x%02X %d\n", E.X, E.X);
printf(" Y 0x%02X %d\n", E.Y, E.Y);
printf(" S 0x%02X %d\n", E.S, E.S);
printf(" P 0x%02X %d\n", E.P, E.P);
/* print flags */
printf(" flags ");
if (E.P == 0) printf(" NOFLAGS");
if (E.P & EN) printf(" NEG");
if (E.P & EV) printf(" OVF");
if (E.P & EB) printf(" BRK");
if (E.P & ED) printf(" DEC");
if (E.P & EI) printf(" ITD");
if (E.P & EZ) printf(" ZRO");
if (E.P & EC) printf(" CAR");
fputc('\n', stdout);
}