src/state.c

Mon, 06 Dec 2010 01:43:04 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 06 Dec 2010 01:43:04 +0000
changeset 54
57c6ef81ae81
parent 52
a350dfa92895
child 55
ba6b8e570062
child 75
976dfa068839
permissions
-rw-r--r--

fix side-select bug in WDC FDC driver, was causing all reads to occur on side0... now the Loader boots!

Loader will boot, but immediately gives up on the floppy drive... Not sure why.

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