src/state.c

Wed, 29 Dec 2010 01:38:54 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 29 Dec 2010 01:38:54 +0000
changeset 74
51cbc7a44cd9
parent 62
c895256b528d
child 77
e7898cbae0c6
child 78
c149c13aff1c
permissions
-rw-r--r--

add first cut keyboard driver

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