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