src/state.c

changeset 18
320dc6206f52
child 33
ae97a3146978
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/state.c	Mon Nov 29 00:20:40 2010 +0000
     1.3 @@ -0,0 +1,74 @@
     1.4 +#define _STATE_C
     1.5 +#include <stddef.h>
     1.6 +#include <malloc.h>
     1.7 +#include <stdio.h>
     1.8 +#include "state.h"
     1.9 +
    1.10 +int state_init(size_t ramsize)
    1.11 +{
    1.12 +	// Free RAM if it's allocated
    1.13 +	if (state.ram != NULL)
    1.14 +		free(state.ram);
    1.15 +
    1.16 +	// Initialise hardware registers
    1.17 +	state.romlmap = false;
    1.18 +
    1.19 +	// Allocate RAM, making sure the user has specified a valid RAM amount first
    1.20 +	// Basically: 512KiB minimum, 4MiB maximum, in increments of 512KiB.
    1.21 +	if ((ramsize < 512*1024) || ((ramsize % (512*1024)) != 0))
    1.22 +		return -1;
    1.23 +	state.ram = malloc(state.ram_size);
    1.24 +	if (state.ram == NULL)
    1.25 +		return -2;
    1.26 +	state.ram_size = ramsize;
    1.27 +
    1.28 +	// Load ROMs
    1.29 +	FILE *r14c, *r15c;
    1.30 +	r14c = fopen("roms/14c.bin", "rb");
    1.31 +//	if (r14c == NULL) FAIL("unable to open roms/14c.bin");
    1.32 +	r15c = fopen("roms/15c.bin", "rb");
    1.33 +//	if (r15c == NULL) FAIL("unable to open roms/15c.bin");
    1.34 +
    1.35 +	// get ROM file size
    1.36 +	fseek(r14c, 0, SEEK_END);
    1.37 +	size_t romlen = ftell(r14c);
    1.38 +	fseek(r14c, 0, SEEK_SET);
    1.39 +	fseek(r15c, 0, SEEK_END);
    1.40 +	size_t romlen2 = ftell(r15c);
    1.41 +	fseek(r15c, 0, SEEK_SET);
    1.42 +//	if (romlen2 != romlen) FAIL("ROMs are not the same size!");
    1.43 +//	if ((romlen + romlen2) > ROM_SIZE) FAIL("ROMs are too large to fit in memory!");
    1.44 +
    1.45 +	// sanity checks completed; load the ROMs!
    1.46 +	uint8_t *romdat1, *romdat2;
    1.47 +	romdat1 = malloc(romlen);
    1.48 +	romdat2 = malloc(romlen2);
    1.49 +	fread(romdat1, 1, romlen, r15c);
    1.50 +	fread(romdat2, 1, romlen2, r14c);
    1.51 +
    1.52 +	// convert the ROM data
    1.53 +	for (size_t i=0; i<(romlen + romlen2); i+=2) {
    1.54 +		state.rom[i+0] = romdat1[i/2];
    1.55 +		state.rom[i+1] = romdat2[i/2];
    1.56 +	}
    1.57 +
    1.58 +	// TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
    1.59 +
    1.60 +	// free the data arrays and close the files
    1.61 +	free(romdat1);
    1.62 +	free(romdat2);
    1.63 +	fclose(r14c);
    1.64 +	fclose(r15c);
    1.65 +
    1.66 +	return 0;
    1.67 +}
    1.68 +
    1.69 +void state_done()
    1.70 +{
    1.71 +	if (state.ram != NULL) {
    1.72 +		free(state.ram);
    1.73 +		state.ram = NULL;
    1.74 +	}
    1.75 +}
    1.76 +
    1.77 +