Mercurial > repos > simple16
annotate src/main.c @ 25:fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 31 Mar 2016 23:25:52 -0700 |
parents | 4c9dbfa30a66 |
children | 083347ccd508 |
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" |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
6 #include "vdp.h" |
24 | 7 #include "audio.h" |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
8 #include "timer.h" |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
9 #include "system.h" |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
11 #define CYCLES_PER_FRAME (832*262) |
24 | 12 #define MASTER_CLOCK 13056000 |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 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
|
15 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
|
16 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 enum { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 PORT_CONTROLLER_1, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 PORT_CONTROLLER_2, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 PORT_CONTROLLER_3, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 PORT_CONTROLLER_4, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 PORT_FREQUENCY_A, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 PORT_FREQUENCY_B, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 PORT_FREQUENCY_C, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 PORT_FREQUENCY_D, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 PORT_VOLUME_AB, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 PORT_VOLUME_CD, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 PORT_TIMER, |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 PORT_SERIAL, |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
30 PORT_VERTICAL, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
31 PORT_HORIZONTAL, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
32 PORT_VRAM_ADDRESS, |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
33 PORT_VRAM_DATA |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 }; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
36 typedef struct { |
24 | 37 cpu *proc; |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
38 audio *audio; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
39 timer timer; |
24 | 40 vdp video; |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
41 } console; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
42 |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 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
|
44 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 putchar(value); |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 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
|
49 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 return getchar(); |
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 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
53 void vertical_port_write(cpu *context, uint8_t port, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
54 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
55 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
56 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
57 system->video.vscroll = value; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
58 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
59 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
60 uint16_t vertical_port_read(cpu *context, uint8_t port) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
61 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
62 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
63 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
64 return system->video.vcounter; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
65 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
66 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
67 void horizontal_port_write(cpu *context, uint8_t port, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
68 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
69 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
70 vdp_run(&system->video, context->cycles); |
19
04fc17376999
Sort of working tile rendering and tile test ROM
Michael Pavone <pavone@retrodev.com>
parents:
16
diff
changeset
|
71 vdp_write_hscroll(&system->video, value); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
72 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
73 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
74 uint16_t horizontal_port_read(cpu *context, uint8_t port) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
75 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
76 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
77 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
78 return system->video.hcounter; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
79 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
80 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
81 void address_port_write(cpu *context, uint8_t port, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
82 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
83 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
84 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
85 vdp_write_address(&system->video, value); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
86 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
87 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
88 uint16_t address_port_read(cpu *context, uint8_t port) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
89 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
90 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
91 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
92 return system->video.status; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
93 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
94 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
95 void data_port_write(cpu *context, uint8_t port, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
96 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
97 console *system = context->system; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
98 vdp_run(&system->video, context->cycles); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
99 vdp_write_data(&system->video, value); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
100 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
101 |
24 | 102 void frequency_port_write(cpu *context, uint8_t port, uint16_t value) |
103 { | |
104 console *system = context->system; | |
105 audio_run(system->audio, context->cycles); | |
106 audio_write_freq(system->audio, port - PORT_FREQUENCY_A, value); | |
107 } | |
108 | |
109 void volume_port_write(cpu *context, uint8_t port, uint16_t value) | |
110 { | |
111 console *system = context->system; | |
112 audio_run(system->audio, context->cycles); | |
113 audio_write_vol(system->audio, port - PORT_VOLUME_AB, value); | |
114 } | |
115 | |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
116 void timer_port_write(cpu *context, uint8_t port, uint16_t value) |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
117 { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
118 console *system = context->system; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
119 timer_run(&system->timer, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
120 timer_write(&system->timer, value); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
121 uint32_t next_int = next_interrupt_cycle(context, (~context->pending_interrupts) & 3); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
122 if (next_int < context->current_target) { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
123 context->current_target = next_int; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
124 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
125 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
126 |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
127 uint32_t next_interrupt_cycle(cpu *context, uint8_t mask) |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
128 { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
129 console *system = context->system; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
130 uint32_t next = 0xFFFFFFFF; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
131 if (mask & 1) { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
132 timer_run(&system->timer, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
133 next = timer_next_interrupt(&system->timer); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
134 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
135 if (mask & 2) { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
136 vdp_run(&system->video, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
137 //TODO: VBlank interrupt |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
138 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
139 return next; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
140 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
141 |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
142 uint8_t get_current_interrupts(cpu *context) |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
143 { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
144 console *system = context->system; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
145 timer_run(&system->timer, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
146 uint8_t bits = 0; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
147 if (system->timer.pending) { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
148 bits |= 1; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
149 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
150 vdp_run(&system->video, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
151 //TODO: VBlank interrupt |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
152 return bits; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
153 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
154 |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
155 void ack_interrupt(cpu *context, int which) |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
156 { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
157 console *system = context->system; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
158 if (which == 0) { |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
159 timer_run(&system->timer, context->cycles); |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
160 system->timer.pending = 0; |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
161 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
162 //TODO: VBlank interrupt |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
163 } |
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
164 |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 memory_region regions[] = { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 {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
|
167 {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
|
168 }; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
170 void run_console(console *context) |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 for(;;) |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 { |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
174 run_cpu(context->proc, CYCLES_PER_FRAME); |
24 | 175 audio_run(context->audio, CYCLES_PER_FRAME); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
176 vdp_run(&context->video, CYCLES_PER_FRAME); |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
177 timer_run(&context->timer, CYCLES_PER_FRAME); |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
178 context->proc->cycles -= CYCLES_PER_FRAME; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
179 context->video.cycles -= CYCLES_PER_FRAME; |
24 | 180 context->audio->cycles -= CYCLES_PER_FRAME; |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
181 context->timer.cycles -= CYCLES_PER_FRAME; |
16
ae58e7c3c328
Poll events regularly to avoid unresponsive app warnings. Handle quit event
Michael Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
182 system_poll_events(); |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 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
|
187 { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 if (argc < 2) { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 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
|
190 return 1; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 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
|
193 if (!f) { |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 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
|
195 return 1; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 } |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 size_t read; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 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
|
200 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
|
201 } |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
202 console context; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
203 context.proc = alloc_cpu(10, sizeof(regions)/sizeof(memory_region), regions); |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
204 context.proc->system = &context; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
205 vdp_init(&context.video, 2); |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
206 timer_init(&context.timer, 16); |
24 | 207 context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write; |
208 context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write; | |
209 context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write; | |
210 context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write; | |
211 context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write; | |
212 context.proc->port_handlers[PORT_VOLUME_CD].write = volume_port_write; | |
25
fb14515266f4
Implemented timer and timer interrupts. Added get/setvbr instructions. Fixed assembler bug. Moved mnemonics into a separate source file
Michael Pavone <pavone@retrodev.com>
parents:
24
diff
changeset
|
213 context.proc->port_handlers[PORT_TIMER].write = timer_port_write; |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
214 context.proc->port_handlers[PORT_SERIAL].write = debug_port_write; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
215 context.proc->port_handlers[PORT_SERIAL].read = debug_port_read; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
216 context.proc->port_handlers[PORT_VERTICAL].write = vertical_port_write; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
217 context.proc->port_handlers[PORT_VERTICAL].read = vertical_port_read; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
218 context.proc->port_handlers[PORT_HORIZONTAL].write = horizontal_port_write; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
219 context.proc->port_handlers[PORT_HORIZONTAL].read = horizontal_port_read; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
220 context.proc->port_handlers[PORT_VRAM_ADDRESS].write = address_port_write; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
221 context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
222 context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write; |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
223 |
24 | 224 if (!system_init(640, 480, 48000)) { |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
225 return 1; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
226 } |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
227 |
24 | 228 context.audio = alloc_audio(MASTER_CLOCK, 17, system_sample_rate(), system_buffer_size()); |
229 | |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
0
diff
changeset
|
230 run_console(&context); |
0
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 return 0; |
7e44f7d5810b
Initial commit. CPU working well enough for simple hello world program.
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 } |