-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
76 lines (56 loc) · 1.36 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
#include "6502.h"
#include "tui.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#define nsleep(t) nanosleep((const struct timespec[]){{0, t * 1000L}}, NULL)
#define INPUT_FILE_PATH "6502_functional_test.bin"
/* TODO: Add support for command line arguments */
/* TODO: Add proper system monitor (memory dump, cpu registers) */
static bool brk = false;
void CPU_brk(uint16_t pc) {
if (CPU_get_pc () == pc) {
printf("BRK: %2X reached\n", pc);
brk = true;
}
}
void sig_handler(int signo) {
if (signo == SIGINT) brk = true;
if (signo == SIGQUIT) brk = true;
}
int main(void) {
INS ins;
/* Initialize memory to 0 */
MEM_init();
/* Load program to memory */
if(MEM_load_from_file(INPUT_FILE_PATH))
return 1;
/* set the first address that the pc should be set to */
MEM_set_pc_start(0x0400);
/* Initialize cpu registers to 0 */
CPU_init();
/* initialize registers from memmory */
CPU_reset();
set_input_mode();
signal(SIGINT, sig_handler);
signal(SIGQUIT, sig_handler);
atexit(reset_input_mode);
do {
/* set break point at pc 0x336d */
CPU_brk(0x336d);
CPU_fetch(&ins);
CPU_dump();
if(CPU_exec(ins) != 0) {
/* exit if invalid instruction */
char c;
read(STDIN_FILENO, &c, 1);
brk = true;
}
nsleep(1000);
} while(!brk);
reset_input_mode();
return 0;
}