diff 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
line wrap: on
line diff
--- a/src/main.c	Wed Mar 30 20:31:04 2016 -0700
+++ b/src/main.c	Thu Mar 31 00:07:37 2016 -0700
@@ -4,9 +4,11 @@
 #include <string.h>
 #include "cpu.h"
 #include "vdp.h"
+#include "audio.h"
 #include "system.h"
 
 #define CYCLES_PER_FRAME (832*262)
+#define MASTER_CLOCK 13056000
 
 uint8_t rom[48 * 1024];
 uint8_t ram[16 * 1024];
@@ -31,8 +33,9 @@
 };
 
 typedef struct {
-	cpu *proc;
-	vdp video;
+	cpu   *proc;
+	vdp   video;
+	audio *audio;
 } console;
 
 void debug_port_write(cpu *context, uint8_t port, uint16_t value)
@@ -94,6 +97,20 @@
 	vdp_write_data(&system->video, value);
 }
 
+void frequency_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	audio_run(system->audio, context->cycles);
+	audio_write_freq(system->audio, port - PORT_FREQUENCY_A, value);
+}
+
+void volume_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	audio_run(system->audio, context->cycles);
+	audio_write_vol(system->audio, port - PORT_VOLUME_AB, value);
+}
+
 memory_region regions[] = {
 	{rom, 0, sizeof(rom)-1, MEM_READ},
 	{ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ},
@@ -104,9 +121,11 @@
 	for(;;)
 	{
 		run_cpu(context->proc, CYCLES_PER_FRAME);
+		audio_run(context->audio, CYCLES_PER_FRAME);
 		vdp_run(&context->video, CYCLES_PER_FRAME);
 		context->proc->cycles -= CYCLES_PER_FRAME;
 		context->video.cycles -= CYCLES_PER_FRAME;
+		context->audio->cycles -= CYCLES_PER_FRAME;
 		system_poll_events();
 	}
 }
@@ -131,6 +150,12 @@
 	context.proc = alloc_cpu(10, sizeof(regions)/sizeof(memory_region), regions);
 	context.proc->system = &context;
 	vdp_init(&context.video, 2);
+	context.proc->port_handlers[PORT_FREQUENCY_A].write = frequency_port_write;
+	context.proc->port_handlers[PORT_FREQUENCY_B].write = frequency_port_write;
+	context.proc->port_handlers[PORT_FREQUENCY_C].write = frequency_port_write;
+	context.proc->port_handlers[PORT_FREQUENCY_D].write = frequency_port_write;
+	context.proc->port_handlers[PORT_VOLUME_AB].write = volume_port_write;
+	context.proc->port_handlers[PORT_VOLUME_CD].write = volume_port_write;
 	context.proc->port_handlers[PORT_SERIAL].write = debug_port_write;
 	context.proc->port_handlers[PORT_SERIAL].read = debug_port_read;
 	context.proc->port_handlers[PORT_VERTICAL].write = vertical_port_write;
@@ -141,10 +166,12 @@
 	context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read;
 	context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write;
 	
-	if (!system_init(640, 480)) {
+	if (!system_init(640, 480, 48000)) {
 		return 1;
 	}
 	
+	context.audio = alloc_audio(MASTER_CLOCK, 17, system_sample_rate(), system_buffer_size());
+	
 	run_console(&context);
 	return 0;
 }