view src/main.c @ 0:7e44f7d5810b

Initial commit. CPU working well enough for simple hello world program.
author Michael Pavone <pavone@retrodev.com>
date Tue, 22 Mar 2016 22:44:02 -0700
parents
children 5176efdda5ae
line wrap: on
line source

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cpu.h"

#define CYCLES_PER_FRAME ((48000*25)/60)

uint8_t rom[48 * 1024];
uint8_t ram[16 * 1024];

enum {
	PORT_CONTROLLER_1,
	PORT_CONTROLLER_2,
	PORT_CONTROLLER_3,
	PORT_CONTROLLER_4,
	PORT_FREQUENCY_A,
	PORT_FREQUENCY_B,
	PORT_FREQUENCY_C,
	PORT_FREQUENCY_D,
	PORT_VOLUME_AB,
	PORT_VOLUME_CD,
	PORT_TIMER,
	PORT_SERIAL,
};

void debug_port_write(cpu *context, uint8_t port, uint16_t value)
{
	putchar(value);
}

uint16_t debug_port_read(cpu *context, uint8_t port)
{
	return getchar();
}

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)
{
	for(;;)
	{
		run_cpu(context, CYCLES_PER_FRAME);
		context->cycles -= CYCLES_PER_FRAME;
	}
}



int main(int argc, char **argv)
{
	if (argc < 2) {
		fputs("usage: s16 FILE\n", stderr);
		return 1;
	}
	FILE *f = fopen(argv[1], "rb");
	if (!f) {
		fprintf(stderr, "Failed to open %s for reading\n", argv[1]);
		return 1;
	}
	
	size_t read;
	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);
	return 0;
}