comparison src/main.c @ 24:4c9dbfa30a66

Implemented audio
author Michael Pavone <pavone@retrodev.com>
date Thu, 31 Mar 2016 00:07:37 -0700
parents 04fc17376999
children fb14515266f4
comparison
equal deleted inserted replaced
23:a085f17b79e9 24:4c9dbfa30a66
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <string.h> 4 #include <string.h>
5 #include "cpu.h" 5 #include "cpu.h"
6 #include "vdp.h" 6 #include "vdp.h"
7 #include "audio.h"
7 #include "system.h" 8 #include "system.h"
8 9
9 #define CYCLES_PER_FRAME (832*262) 10 #define CYCLES_PER_FRAME (832*262)
11 #define MASTER_CLOCK 13056000
10 12
11 uint8_t rom[48 * 1024]; 13 uint8_t rom[48 * 1024];
12 uint8_t ram[16 * 1024]; 14 uint8_t ram[16 * 1024];
13 15
14 enum { 16 enum {
29 PORT_VRAM_ADDRESS, 31 PORT_VRAM_ADDRESS,
30 PORT_VRAM_DATA 32 PORT_VRAM_DATA
31 }; 33 };
32 34
33 typedef struct { 35 typedef struct {
34 cpu *proc; 36 cpu *proc;
35 vdp video; 37 vdp video;
38 audio *audio;
36 } console; 39 } console;
37 40
38 void debug_port_write(cpu *context, uint8_t port, uint16_t value) 41 void debug_port_write(cpu *context, uint8_t port, uint16_t value)
39 { 42 {
40 putchar(value); 43 putchar(value);
92 console *system = context->system; 95 console *system = context->system;
93 vdp_run(&system->video, context->cycles); 96 vdp_run(&system->video, context->cycles);
94 vdp_write_data(&system->video, value); 97 vdp_write_data(&system->video, value);
95 } 98 }
96 99
100 void frequency_port_write(cpu *context, uint8_t port, uint16_t value)
101 {
102 console *system = context->system;
103 audio_run(system->audio, context->cycles);
104 audio_write_freq(system->audio, port - PORT_FREQUENCY_A, value);
105 }
106
107 void volume_port_write(cpu *context, uint8_t port, uint16_t value)
108 {
109 console *system = context->system;
110 audio_run(system->audio, context->cycles);
111 audio_write_vol(system->audio, port - PORT_VOLUME_AB, value);
112 }
113
97 memory_region regions[] = { 114 memory_region regions[] = {
98 {rom, 0, sizeof(rom)-1, MEM_READ}, 115 {rom, 0, sizeof(rom)-1, MEM_READ},
99 {ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ}, 116 {ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ},
100 }; 117 };
101 118
102 void run_console(console *context) 119 void run_console(console *context)
103 { 120 {
104 for(;;) 121 for(;;)
105 { 122 {
106 run_cpu(context->proc, CYCLES_PER_FRAME); 123 run_cpu(context->proc, CYCLES_PER_FRAME);
124 audio_run(context->audio, CYCLES_PER_FRAME);
107 vdp_run(&context->video, CYCLES_PER_FRAME); 125 vdp_run(&context->video, CYCLES_PER_FRAME);
108 context->proc->cycles -= CYCLES_PER_FRAME; 126 context->proc->cycles -= CYCLES_PER_FRAME;
109 context->video.cycles -= CYCLES_PER_FRAME; 127 context->video.cycles -= CYCLES_PER_FRAME;
128 context->audio->cycles -= CYCLES_PER_FRAME;
110 system_poll_events(); 129 system_poll_events();
111 } 130 }
112 } 131 }
113 132
114 int main(int argc, char **argv) 133 int main(int argc, char **argv)
129 } 148 }
130 console context; 149 console context;
131 context.proc = alloc_cpu(10, sizeof(regions)/sizeof(memory_region), regions); 150 context.proc = alloc_cpu(10, sizeof(regions)/sizeof(memory_region), regions);
132 context.proc->system = &context; 151 context.proc->system = &context;
133 vdp_init(&context.video, 2); 152 vdp_init(&context.video, 2);
153 context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write;
154 context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write;
155 context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write;
156 context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write;
157 context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write;
158 context.proc->port_handlers[PORT_VOLUME_CD].write = volume_port_write;
134 context.proc->port_handlers[PORT_SERIAL].write = debug_port_write; 159 context.proc->port_handlers[PORT_SERIAL].write = debug_port_write;
135 context.proc->port_handlers[PORT_SERIAL].read = debug_port_read; 160 context.proc->port_handlers[PORT_SERIAL].read = debug_port_read;
136 context.proc->port_handlers[PORT_VERTICAL].write = vertical_port_write; 161 context.proc->port_handlers[PORT_VERTICAL].write = vertical_port_write;
137 context.proc->port_handlers[PORT_VERTICAL].read = vertical_port_read; 162 context.proc->port_handlers[PORT_VERTICAL].read = vertical_port_read;
138 context.proc->port_handlers[PORT_HORIZONTAL].write = horizontal_port_write; 163 context.proc->port_handlers[PORT_HORIZONTAL].write = horizontal_port_write;
139 context.proc->port_handlers[PORT_HORIZONTAL].read = horizontal_port_read; 164 context.proc->port_handlers[PORT_HORIZONTAL].read = horizontal_port_read;
140 context.proc->port_handlers[PORT_VRAM_ADDRESS].write = address_port_write; 165 context.proc->port_handlers[PORT_VRAM_ADDRESS].write = address_port_write;
141 context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read; 166 context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read;
142 context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write; 167 context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write;
143 168
144 if (!system_init(640, 480)) { 169 if (!system_init(640, 480, 48000)) {
145 return 1; 170 return 1;
146 } 171 }
172
173 context.audio = alloc_audio(MASTER_CLOCK, 17, system_sample_rate(), system_buffer_size());
147 174
148 run_console(&context); 175 run_console(&context);
149 return 0; 176 return 0;
150 } 177 }