src/state.c

Sun, 05 Dec 2010 16:20:00 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Sun, 05 Dec 2010 16:20:00 +0000
changeset 52
a350dfa92895
parent 33
ae97a3146978
child 55
ba6b8e570062
child 75
976dfa068839
permissions
-rw-r--r--

add preliminary WD279x emulation to core

     1 #define _STATE_C
     2 #include <stddef.h>
     3 #include <malloc.h>
     4 #include <stdio.h>
     5 #include "wd279x.h"
     6 #include "state.h"
     8 int state_init(size_t ramsize)
     9 {
    10 	// Free RAM if it's allocated
    11 	if (state.ram != NULL)
    12 		free(state.ram);
    14 	// Initialise hardware registers
    15 	state.romlmap = false;
    17 	// Allocate RAM, making sure the user has specified a valid RAM amount first
    18 	// Basically: 512KiB minimum, 4MiB maximum, in increments of 512KiB.
    19 	if ((ramsize < 512*1024) || ((ramsize % (512*1024)) != 0))
    20 		return -1;
    21 	state.ram = malloc(ramsize);
    22 	if (state.ram == NULL)
    23 		return -2;
    24 	state.ram_size = ramsize;
    26 	// Load ROMs
    27 	FILE *r14c, *r15c;
    28 	r14c = fopen("roms/14c.bin", "rb");
    29 //	if (r14c == NULL) FAIL("unable to open roms/14c.bin");
    30 	r15c = fopen("roms/15c.bin", "rb");
    31 //	if (r15c == NULL) FAIL("unable to open roms/15c.bin");
    33 	// get ROM file size
    34 	fseek(r14c, 0, SEEK_END);
    35 	size_t romlen = ftell(r14c);
    36 	fseek(r14c, 0, SEEK_SET);
    37 	fseek(r15c, 0, SEEK_END);
    38 	size_t romlen2 = ftell(r15c);
    39 	fseek(r15c, 0, SEEK_SET);
    40 //	if (romlen2 != romlen) FAIL("ROMs are not the same size!");
    41 //	if ((romlen + romlen2) > ROM_SIZE) FAIL("ROMs are too large to fit in memory!");
    43 	// sanity checks completed; load the ROMs!
    44 	uint8_t *romdat1, *romdat2;
    45 	romdat1 = malloc(romlen);
    46 	romdat2 = malloc(romlen2);
    47 	fread(romdat1, 1, romlen, r15c);
    48 	fread(romdat2, 1, romlen2, r14c);
    50 	// convert the ROM data
    51 	for (size_t i=0; i<(romlen + romlen2); i+=2) {
    52 		state.rom[i+0] = romdat1[i/2];
    53 		state.rom[i+1] = romdat2[i/2];
    54 	}
    56 	// TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
    58 	// free the data arrays and close the files
    59 	free(romdat1);
    60 	free(romdat2);
    61 	fclose(r14c);
    62 	fclose(r15c);
    64 	// Initialise the disc controller
    65 	wd2797_init(&state.fdc_ctx);
    67 	return 0;
    68 }
    70 void state_done()
    71 {
    72 	if (state.ram != NULL) {
    73 		free(state.ram);
    74 		state.ram = NULL;
    75 	}
    77 	// Deinitialise the disc controller
    78 	wd2797_done(&state.fdc_ctx);
    79 }