comparison src/main.c @ 0:7e44f7d5810b

Initial commit. CPU working well enough for simple hello world program.
author Michael Pavone <pavone@retrodev.com>
date Tue, 22 Mar 2016 22:44:02 -0700
parents
children 5176efdda5ae
comparison
equal deleted inserted replaced
-1:000000000000 0:7e44f7d5810b
1 #include <stdint.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "cpu.h"
6
7 #define CYCLES_PER_FRAME ((48000*25)/60)
8
9 uint8_t rom[48 * 1024];
10 uint8_t ram[16 * 1024];
11
12 enum {
13 PORT_CONTROLLER_1,
14 PORT_CONTROLLER_2,
15 PORT_CONTROLLER_3,
16 PORT_CONTROLLER_4,
17 PORT_FREQUENCY_A,
18 PORT_FREQUENCY_B,
19 PORT_FREQUENCY_C,
20 PORT_FREQUENCY_D,
21 PORT_VOLUME_AB,
22 PORT_VOLUME_CD,
23 PORT_TIMER,
24 PORT_SERIAL,
25 };
26
27 void debug_port_write(cpu *context, uint8_t port, uint16_t value)
28 {
29 putchar(value);
30 }
31
32 uint16_t debug_port_read(cpu *context, uint8_t port)
33 {
34 return getchar();
35 }
36
37 memory_region regions[] = {
38 {rom, 0, sizeof(rom)-1, MEM_READ},
39 {ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ},
40 };
41
42 void run_console(cpu *context)
43 {
44 for(;;)
45 {
46 run_cpu(context, CYCLES_PER_FRAME);
47 context->cycles -= CYCLES_PER_FRAME;
48 }
49 }
50
51
52
53 int main(int argc, char **argv)
54 {
55 if (argc < 2) {
56 fputs("usage: s16 FILE\n", stderr);
57 return 1;
58 }
59 FILE *f = fopen(argv[1], "rb");
60 if (!f) {
61 fprintf(stderr, "Failed to open %s for reading\n", argv[1]);
62 return 1;
63 }
64
65 size_t read;
66 if ((read = fread(rom, 1, sizeof(rom), f)) < sizeof(rom)) {
67 memset(rom + read, 0xFF, sizeof(rom)-read);
68 }
69 cpu *context = alloc_cpu(1, sizeof(regions)/sizeof(memory_region), regions);
70 context->port_handlers[PORT_SERIAL].write = debug_port_write;
71 context->port_handlers[PORT_SERIAL].read = debug_port_read;
72 run_console(context);
73 return 0;
74 }