src/state.c

Mon, 06 Dec 2010 08:27:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 06 Dec 2010 08:27:21 +0000
changeset 76
2ef98ea1e944
parent 75
976dfa068839
child 77
e7898cbae0c6
child 78
c149c13aff1c
permissions
-rw-r--r--

move edge-sensitive FDC IRQ to main()

     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;
    16 	state.idmarw = state.dmaen = state.dmaenb = false;
    17 	state.dma_count = state.dma_address = 0;
    18 	state.pie = 0;
    19 	state.leds = 0;
    20 	state.genstat = 0;				// FIXME: check this
    21 	state.bsr0 = state.bsr1 = 0;	// FIXME: check this
    23 	// Allocate RAM, making sure the user has specified a valid RAM amount first
    24 	// Basically: 512KiB minimum, 4MiB maximum, in increments of 512KiB.
    25 	if ((ramsize < 512*1024) || ((ramsize % (512*1024)) != 0))
    26 		return -1;
    27 	state.ram = malloc(ramsize);
    28 	if (state.ram == NULL)
    29 		return -2;
    30 	state.ram_size = ramsize;
    32 	// Load ROMs
    33 	FILE *r14c, *r15c;
    34 	r14c = fopen("roms/14c.bin", "rb");
    35 //	if (r14c == NULL) FAIL("unable to open roms/14c.bin");
    36 	r15c = fopen("roms/15c.bin", "rb");
    37 //	if (r15c == NULL) FAIL("unable to open roms/15c.bin");
    39 	// get ROM file size
    40 	fseek(r14c, 0, SEEK_END);
    41 	size_t romlen = ftell(r14c);
    42 	fseek(r14c, 0, SEEK_SET);
    43 	fseek(r15c, 0, SEEK_END);
    44 	size_t romlen2 = ftell(r15c);
    45 	fseek(r15c, 0, SEEK_SET);
    46 //	if (romlen2 != romlen) FAIL("ROMs are not the same size!");
    47 //	if ((romlen + romlen2) > ROM_SIZE) FAIL("ROMs are too large to fit in memory!");
    49 	// sanity checks completed; load the ROMs!
    50 	uint8_t *romdat1, *romdat2;
    51 	romdat1 = malloc(romlen);
    52 	romdat2 = malloc(romlen2);
    53 	fread(romdat1, 1, romlen, r15c);
    54 	fread(romdat2, 1, romlen2, r14c);
    56 	// convert the ROM data
    57 	for (size_t i=0; i<(romlen + romlen2); i+=2) {
    58 		state.rom[i+0] = romdat1[i/2];
    59 		state.rom[i+1] = romdat2[i/2];
    60 	}
    62 	// TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
    64 	// free the data arrays and close the files
    65 	free(romdat1);
    66 	free(romdat2);
    67 	fclose(r14c);
    68 	fclose(r15c);
    70 	// Initialise the disc controller
    71 	wd2797_init(&state.fdc_ctx);
    73 	return 0;
    74 }
    76 void state_done()
    77 {
    78 	if (state.ram != NULL) {
    79 		free(state.ram);
    80 		state.ram = NULL;
    81 	}
    83 	// Deinitialise the disc controller
    84 	wd2797_done(&state.fdc_ctx);
    85 }