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