src/state.c

Tue, 28 Dec 2010 17:23:04 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 28 Dec 2010 17:23:04 +0000
changeset 60
96f3df0b3cbb
parent 55
ba6b8e570062
child 62
c895256b528d
permissions
-rw-r--r--

fixes to Base RAM addressing -- 512K detected OK now, still need to deal with Expansion RAM detect issues

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@60 11 if (state.base_ram != NULL)
philpem@60 12 free(state.base_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@60 21 state.base_ram = malloc(ramsize);
philpem@60 22 if (state.base_ram == NULL)
philpem@18 23 return -2;
philpem@60 24 state.base_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@55 29 if (r14c == NULL) {
philpem@55 30 fprintf(stderr, "[state] Error loading roms/14c.bin.\n");
philpem@55 31 return -3;
philpem@55 32 }
philpem@18 33 r15c = fopen("roms/15c.bin", "rb");
philpem@55 34 if (r15c == NULL) {
philpem@55 35 fprintf(stderr, "[state] Error loading roms/15c.bin.\n");
philpem@55 36 return -3;
philpem@55 37 }
philpem@18 38
philpem@18 39 // get ROM file size
philpem@18 40 fseek(r14c, 0, SEEK_END);
philpem@18 41 size_t romlen = ftell(r14c);
philpem@18 42 fseek(r14c, 0, SEEK_SET);
philpem@18 43 fseek(r15c, 0, SEEK_END);
philpem@18 44 size_t romlen2 = ftell(r15c);
philpem@18 45 fseek(r15c, 0, SEEK_SET);
philpem@55 46 if (romlen2 != romlen) {
philpem@55 47 fprintf(stderr, "[state] ROMs are not the same size!\n");
philpem@55 48 return -3;
philpem@55 49 }
philpem@55 50 if ((romlen + romlen2) > ROM_SIZE) {
philpem@55 51 fprintf(stderr, "[state] ROM files are too large!\n");
philpem@55 52 return -3;
philpem@55 53 }
philpem@18 54
philpem@18 55 // sanity checks completed; load the ROMs!
philpem@18 56 uint8_t *romdat1, *romdat2;
philpem@18 57 romdat1 = malloc(romlen);
philpem@18 58 romdat2 = malloc(romlen2);
philpem@18 59 fread(romdat1, 1, romlen, r15c);
philpem@18 60 fread(romdat2, 1, romlen2, r14c);
philpem@18 61
philpem@18 62 // convert the ROM data
philpem@18 63 for (size_t i=0; i<(romlen + romlen2); i+=2) {
philpem@18 64 state.rom[i+0] = romdat1[i/2];
philpem@18 65 state.rom[i+1] = romdat2[i/2];
philpem@18 66 }
philpem@18 67
philpem@18 68 // TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
philpem@18 69
philpem@18 70 // free the data arrays and close the files
philpem@18 71 free(romdat1);
philpem@18 72 free(romdat2);
philpem@18 73 fclose(r14c);
philpem@18 74 fclose(r15c);
philpem@18 75
philpem@52 76 // Initialise the disc controller
philpem@52 77 wd2797_init(&state.fdc_ctx);
philpem@52 78
philpem@18 79 return 0;
philpem@18 80 }
philpem@18 81
philpem@18 82 void state_done()
philpem@18 83 {
philpem@60 84 if (state.base_ram != NULL) {
philpem@60 85 free(state.base_ram);
philpem@60 86 state.base_ram = NULL;
philpem@18 87 }
philpem@52 88
philpem@52 89 // Deinitialise the disc controller
philpem@52 90 wd2797_done(&state.fdc_ctx);
philpem@18 91 }
philpem@18 92
philpem@18 93