-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.c
74 lines (64 loc) · 1.75 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
#include "cpu.h"
#include "emu.h"
#include "functions.h"
#include "io.h"
#include "opcodes.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void usage() {
printf("x6502: a simple 6502 emulator\n");
printf("usage: x6502 [OPTION]... [FILE] \n");
printf("options:\n");
printf("\t-b addr\t\tthe base address at which code will be loaded\n");
printf("\t-d file\t\ta binary file to back the block device\n");
printf("\t\t\t(optional, defaults to zero)\n");
}
int main(int argc, char *argv[]) {
int base_addr = 0x1000;
char *blck0_file = NULL;
int c;
while ((c = getopt(argc, argv, "hb:d:")) != -1) {
switch (c) {
case 'b':
base_addr = atoi(optarg);
break;
case 'd':
blck0_file = optarg;
break;
case 'h':
usage();
return 0;
case '?':
if (optopt == 'b' || optopt == 'd') {
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
}
usage();
return -1;
}
}
if (optind == argc) {
printf("no input file specified.\n");
usage();
return -1;
}
if (blck0_file != NULL) {
debugf("using %s as a backing file for block device 0\n", blck0_file);
FILE *blck0 = fopen(blck0_file, "r+");
if (blck0 == NULL) {
fprintf(stderr, "block file %s does not exist.\n", blck0_file);
return -1;
}
set_block_source(blck0);
}
FILE *in_f = fopen(argv[optind], "r");
int b;
int i = base_addr;
cpu *m = new_cpu(base_addr);
while ((b = fgetc(in_f)) != EOF) {
m->mem[i++] = (uint8_t) b;
}
main_loop(m);
return 0;
}