src/state.c

Wed, 29 Dec 2010 09:04:43 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 29 Dec 2010 09:04:43 +0000
changeset 78
c149c13aff1c
parent 62
c895256b528d
parent 75
976dfa068839
child 79
674226015c8a
permissions
-rw-r--r--

merge heads (properly this time)

     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 base_ram_size, size_t exp_ram_size)
     9 {
    10 	// Free RAM if it's allocated
    11 	if (state.base_ram != NULL)
    12 		free(state.base_ram);
    13 	if (state.exp_ram != NULL)
    14 		free(state.exp_ram);
    16 	// Initialise hardware registers
    17 	state.romlmap = false;
    18 	state.idmarw = state.dmaen = state.dmaenb = false;
    19 	state.dma_count = state.dma_address = 0;
    20 	state.pie = 0;
    21 	state.leds = 0;
    22 	state.genstat = 0;				// FIXME: check this
    23 	state.bsr0 = state.bsr1 = 0;	// FIXME: check this
    25 	// Allocate Base RAM, making sure the user has specified a valid RAM amount first
    26 	// Basically: 512KiB minimum, 2MiB maximum, in increments of 512KiB.
    27 	if ((base_ram_size < 512*1024) || (base_ram_size > 2048*1024) || ((base_ram_size % (512*1024)) != 0))
    28 		return -1;
    29 	state.base_ram = malloc(base_ram_size);
    30 	if (state.base_ram == NULL)
    31 		return -2;
    32 	state.base_ram_size = base_ram_size;
    34 	// Now allocate expansion RAM
    35 	// The difference here is that we can have zero bytes of Expansion RAM; we're not limited to having a minimum of 512KiB.
    36 	if ((exp_ram_size > 2048*1024) || ((exp_ram_size % (512*1024)) != 0))
    37 		return -1;
    38 	state.exp_ram = malloc(exp_ram_size);
    39 	if (state.exp_ram == NULL)
    40 		return -2;
    41 	state.exp_ram_size = exp_ram_size;
    43 	// Load ROMs
    44 	FILE *r14c, *r15c;
    45 	r14c = fopen("roms/14c.bin", "rb");
    46 	if (r14c == NULL) {
    47 		fprintf(stderr, "[state] Error loading roms/14c.bin.\n");
    48 		return -3;
    49 	}
    50 	r15c = fopen("roms/15c.bin", "rb");
    51 	if (r15c == NULL) {
    52 		fprintf(stderr, "[state] Error loading roms/15c.bin.\n");
    53 		return -3;
    54 	}
    56 	// get ROM file size
    57 	fseek(r14c, 0, SEEK_END);
    58 	size_t romlen = ftell(r14c);
    59 	fseek(r14c, 0, SEEK_SET);
    60 	fseek(r15c, 0, SEEK_END);
    61 	size_t romlen2 = ftell(r15c);
    62 	fseek(r15c, 0, SEEK_SET);
    63 	if (romlen2 != romlen) {
    64 		fprintf(stderr, "[state] ROMs are not the same size!\n");
    65 		return -3;
    66 	}
    67 	if ((romlen + romlen2) > ROM_SIZE) {
    68 		fprintf(stderr, "[state] ROM files are too large!\n");
    69 		return -3;
    70 	}
    72 	// sanity checks completed; load the ROMs!
    73 	uint8_t *romdat1, *romdat2;
    74 	romdat1 = malloc(romlen);
    75 	romdat2 = malloc(romlen2);
    76 	fread(romdat1, 1, romlen, r15c);
    77 	fread(romdat2, 1, romlen2, r14c);
    79 	// convert the ROM data
    80 	for (size_t i=0; i<(romlen + romlen2); i+=2) {
    81 		state.rom[i+0] = romdat1[i/2];
    82 		state.rom[i+1] = romdat2[i/2];
    83 	}
    85 	// TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
    87 	// free the data arrays and close the files
    88 	free(romdat1);
    89 	free(romdat2);
    90 	fclose(r14c);
    91 	fclose(r15c);
    93 	// Initialise the disc controller
    94 	wd2797_init(&state.fdc_ctx);
    96 	return 0;
    97 }
    99 void state_done()
   100 {
   101 	if (state.base_ram != NULL) {
   102 		free(state.base_ram);
   103 		state.base_ram = NULL;
   104 	}
   106 	if (state.exp_ram != NULL) {
   107 		free(state.exp_ram);
   108 		state.exp_ram = NULL;
   109 	}
   111 	// Deinitialise the disc controller
   112 	wd2797_done(&state.fdc_ctx);
   113 }