diff src/vdp.c @ 8:5176efdda5ae

Initial work on VDP emulation
author Michael Pavone <pavone@retrodev.com>
date Sun, 27 Mar 2016 00:24:31 -0700
parents
children 04d8efe7a1f0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/vdp.c	Sun Mar 27 00:24:31 2016 -0700
@@ -0,0 +1,70 @@
+#include <stdint.h>
+#include <string.h>
+#include "vdp.h"
+
+void vdp_init(vdp *context, uint32_t clock_div)
+{
+	memset(context, 0, sizeof(vdp));
+	context->clock_inc = clock_div;
+}
+
+void vdp_run(vdp *context, uint32_t target)
+{
+	while (context->cycles < target)
+	{
+		context->hcounter++;
+		if (context->hcounter == 416) {
+			context->hcounter = 0;
+			context->vcounter++;
+			if (context->vcounter == 262) {
+				context->vcounter = 0;
+			}
+		}
+		//TODO: do rendering stuff here
+		if (context->status & VDP_STATUS_FIFO) {
+			switch (context->fifo_dest)
+			{
+			case FIFO_DEST_VRAM:
+				if (!(context->status & VDP_STATUS_VRAM)) {
+					context->vram[context->dest_offset++] = context->fifo;
+					context->dest_offset &= sizeof(context->vram)/2-1;
+					context->status &= ~VDP_STATUS_FIFO;
+				}
+				break;
+			case FIFO_DEST_SRAM:
+				if (!(context->status & VDP_STATUS_SRAM)) {
+					context->sram[context->dest_offset++] = context->fifo;
+					context->dest_offset &= sizeof(context->sram)/2-1;
+					context->status &= ~VDP_STATUS_FIFO;
+				}
+				break;
+			case FIFO_DEST_CRAM:
+				context->cram[context->dest_offset++] = context->fifo;
+				context->dest_offset &= sizeof(context->cram)/2-1;
+				context->status &= ~VDP_STATUS_FIFO;
+				break;
+			}
+		}
+		context->cycles += context->clock_inc;
+	}
+}
+void vdp_write_address(vdp *context, uint16_t value)
+{
+	context->status &= ~VDP_STATUS_FIFO;
+	if (value & 0x8000) {
+		context->fifo_dest = FIFO_DEST_VRAM;
+		context->dest_offset = (value & (sizeof(context->vram) -1))/2;
+	} else if (value & 0xFF00 == 0xFE00) {
+		context->fifo_dest = FIFO_DEST_SRAM;
+		context->dest_offset = (value & (sizeof(context->sram) -1))/2;
+	} else if (value & 0xFF00 == 0xFF00) {
+		context->fifo_dest = FIFO_DEST_CRAM;
+		context->dest_offset = (value & (sizeof(context->cram) -1))/2;
+	} 
+}
+
+void vdp_write_data(vdp *context, uint16_t value)
+{
+	context->fifo = value;
+	context->status |= VDP_STATUS_FIFO;
+}