view src/vdp.c @ 21:91ded3b12d96

Only run rendering hardware when display is enabled
author Michael Pavone <pavone@retrodev.com>
date Tue, 29 Mar 2016 19:59:26 -0700
parents 04fc17376999
children 407725d9a02f
line wrap: on
line source

#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "vdp.h"
#include "system.h"

void vdp_init(vdp *context, uint32_t clock_div)
{
	memset(context, 0, sizeof(vdp));
	//clock div specifies the pixel clock divider
	//but our emulation step is half that fast
	context->clock_inc = clock_div*2;
	context->drawbuffer = context->linebuffers;
	context->readbuffer = context->linebuffers+320;
}

void vdp_run(vdp *context, uint32_t target)
{
	while (context->cycles < target)
	{
		context->hcounter+=2;
		if (context->hcounter == 416) {
			context->hcounter = 0;
			context->vcounter++;
			if (context->vcounter == 262) {
				context->vcounter = 0;
			}
		}
		context->status &= ~(VDP_STATUS_VRAM|VDP_STATUS_SRAM);
		//Render to linebuffer
		if ((context->status & VDP_STATUS_ENABLED) && context->vcounter > 15 && context->vcounter < 240 && context->hcounter < 406) {
			if (context->hcounter < 246) {
				context->status |= VDP_STATUS_VRAM;
				if (!context->hcounter) {
					//flip linebuffers
					if (context->drawbuffer == context->linebuffers) {
						context->drawbuffer = context->linebuffers + 328;
						context->readbuffer = context->linebuffers;
					} else {
						context->drawbuffer = context->linebuffers;
						context->readbuffer = context->linebuffers + 328;
					}
					context->draw_dest = 0;
				}
				if (context->draw_counter) {
					context->draw_counter--;
					uint16_t pixels = context->vram[context->draw_source++];
					for (int i = 0; i < 4; i++)
					{
						uint8_t pixel = ((pixels >> ((3-i) * 4)) & 0xF) | context->palpriority;
						context->drawbuffer[context->draw_dest++] = pixel;
					}
				} else {
					//00VV VVVV VVHH HHHH
					uint16_t vpos = (context->vscroll & 0x7FF) + context->vcounter - 16;
					uint16_t vmask = (context->vscroll >> 2) & 0x3E00;
					uint16_t vcoarse = (vpos << 3) & 0x3FC0;
					uint16_t vfine = vpos & 7;
					uint16_t hcoarse = ((context->hscroll >> 3) + context->hcounter/6) & 0x3F;
					uint16_t tableaddress = hcoarse | (vcoarse & ~vmask) | ((context->vscroll << 3) & vmask);
					//printf("VCounter: %X, VScroll: %X, HCounter: %X, Table: %X\n", context->vcounter, context->vscroll, context->hcounter, tableaddress);
					uint16_t entry = context->vram[tableaddress];
					context->draw_source = (entry & 0x3FF) * 16;
					if (entry & 0x1000) {
						context->draw_source += 14 - vfine * 2;
					} else {
						context->draw_source += vfine * 2;
					}
					context->palpriority = entry >> 9 & 0x70;
					context->draw_counter = 2;
					//TODO: handle horizontal flip
				}
				//TODO: Scan sprite table
			} else {
				//TODO: Render sprites
			}
		}
		//Draw to framebuffer
		if (context->vcounter > 8 && context->vcounter < 249 && context->hcounter < 320) {
			if (!context->hcounter && context->vcounter == 9) {
				context->framebuffer = system_get_framebuffer(&context->pitch);
				//pitch is in terms of bytes, but we want it in terms of pixels
				context->pitch /= sizeof(uint16_t);
			}
			uint16_t *dest = context->framebuffer + (context->vcounter - 9) * context->pitch + context->hcounter;
			if (context->status & VDP_STATUS_ENABLED && context->vcounter > 16 && context->vcounter < 241) {
				*dest = context->cram[0x3F & context->readbuffer[context->hcounter]];
				dest++;
				*dest = context->cram[0x3F & context->readbuffer[context->hcounter+1]];
			} else {
				//Display is disabled or we're in the border area, draw the background color
				*dest = *context->cram;
				dest++;
				*dest = *context->cram;
			}
		} else if(!context->hcounter && context->vcounter == 249) {
			system_framebuffer_updated();
			context->framebuffer = NULL;
		}
		//Handle the FIFO
		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;
}

void vdp_write_hscroll(vdp *context, uint16_t value)
{
	context->hscroll = value & 0x1FF;
	if (value & 0x8000) {
		context->status |= VDP_STATUS_ENABLED;
	} else {
		context->status &= ~VDP_STATUS_ENABLED;
	}
}