diff src/main.c @ 8:5176efdda5ae

Initial work on VDP emulation
author Michael Pavone <pavone@retrodev.com>
date Sun, 27 Mar 2016 00:24:31 -0700
parents 7e44f7d5810b
children 04d8efe7a1f0
line wrap: on
line diff
--- a/src/main.c	Sat Mar 26 23:36:33 2016 -0700
+++ b/src/main.c	Sun Mar 27 00:24:31 2016 -0700
@@ -3,8 +3,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include "cpu.h"
+#include "vdp.h"
 
-#define CYCLES_PER_FRAME ((48000*25)/60)
+#define CYCLES_PER_FRAME (832*262)
 
 uint8_t rom[48 * 1024];
 uint8_t ram[16 * 1024];
@@ -22,8 +23,17 @@
 	PORT_VOLUME_CD,
 	PORT_TIMER,
 	PORT_SERIAL,
+	PORT_VERTICAL,
+	PORT_HORIZONTAL,
+	PORT_VRAM_ADDRESS,
+	PORT_VRAM_DATA
 };
 
+typedef struct {
+	cpu *proc;
+	vdp video;
+} console;
+
 void debug_port_write(cpu *context, uint8_t port, uint16_t value)
 {
 	putchar(value);
@@ -34,17 +44,68 @@
 	return getchar();
 }
 
+void vertical_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	system->video.vscroll = value;
+}
+
+uint16_t vertical_port_read(cpu *context, uint8_t port)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	return system->video.vcounter;
+}
+
+void horizontal_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	system->video.hscroll = value;
+}
+
+uint16_t horizontal_port_read(cpu *context, uint8_t port)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	return system->video.hcounter;
+}
+
+void address_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	vdp_write_address(&system->video, value);
+}
+
+uint16_t address_port_read(cpu *context, uint8_t port)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	return system->video.status;
+}
+
+void data_port_write(cpu *context, uint8_t port, uint16_t value)
+{
+	console *system = context->system;
+	vdp_run(&system->video, context->cycles);
+	vdp_write_data(&system->video, value);
+}
+
 memory_region regions[] = {
 	{rom, 0, sizeof(rom)-1, MEM_READ},
 	{ram, sizeof(rom), sizeof(rom)-1+sizeof(ram), MEM_READ},
 };
 
-void run_console(cpu *context)
+void run_console(console *context)
 {
 	for(;;)
 	{
-		run_cpu(context, CYCLES_PER_FRAME);
-		context->cycles -= CYCLES_PER_FRAME;
+		run_cpu(context->proc, CYCLES_PER_FRAME);
+		vdp_run(&context->video, CYCLES_PER_FRAME);
+		context->proc->cycles -= CYCLES_PER_FRAME;
+		context->video.cycles -= CYCLES_PER_FRAME;
 	}
 }
 
@@ -66,9 +127,19 @@
 	if ((read = fread(rom, 1, sizeof(rom), f)) < sizeof(rom))  {
 		memset(rom + read, 0xFF, sizeof(rom)-read);
 	}
-	cpu *context = alloc_cpu(1, sizeof(regions)/sizeof(memory_region), regions);
-	context->port_handlers[PORT_SERIAL].write = debug_port_write;
-	context->port_handlers[PORT_SERIAL].read = debug_port_read;
-	run_console(context);
+	console context;
+	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_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;
+	context.proc->port_handlers[PORT_VERTICAL].read = vertical_port_read;
+	context.proc->port_handlers[PORT_HORIZONTAL].write = horizontal_port_write;
+	context.proc->port_handlers[PORT_HORIZONTAL].read = horizontal_port_read;
+	context.proc->port_handlers[PORT_VRAM_ADDRESS].write = address_port_write;
+	context.proc->port_handlers[PORT_VRAM_ADDRESS].read = address_port_read;
+	context.proc->port_handlers[PORT_VRAM_DATA].write = data_port_write;
+	run_console(&context);
 	return 0;
 }