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