src/state.c

Wed, 29 Dec 2010 01:38:54 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 29 Dec 2010 01:38:54 +0000
changeset 74
51cbc7a44cd9
parent 62
c895256b528d
child 77
e7898cbae0c6
child 78
c149c13aff1c
permissions
-rw-r--r--

add first cut keyboard driver

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@62 8 int state_init(size_t base_ram_size, size_t exp_ram_size)
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@62 13 if (state.exp_ram != NULL)
philpem@62 14 free(state.exp_ram);
philpem@18 15
philpem@18 16 // Initialise hardware registers
philpem@18 17 state.romlmap = false;
philpem@18 18
philpem@62 19 // Allocate Base RAM, making sure the user has specified a valid RAM amount first
philpem@62 20 // Basically: 512KiB minimum, 2MiB maximum, in increments of 512KiB.
philpem@62 21 if ((base_ram_size < 512*1024) || (base_ram_size > 2048*1024) || ((base_ram_size % (512*1024)) != 0))
philpem@18 22 return -1;
philpem@62 23 state.base_ram = malloc(base_ram_size);
philpem@60 24 if (state.base_ram == NULL)
philpem@18 25 return -2;
philpem@62 26 state.base_ram_size = base_ram_size;
philpem@62 27
philpem@62 28 // Now allocate expansion RAM
philpem@62 29 // The difference here is that we can have zero bytes of Expansion RAM; we're not limited to having a minimum of 512KiB.
philpem@62 30 if ((exp_ram_size > 2048*1024) || ((exp_ram_size % (512*1024)) != 0))
philpem@62 31 return -1;
philpem@62 32 state.exp_ram = malloc(exp_ram_size);
philpem@62 33 if (state.exp_ram == NULL)
philpem@62 34 return -2;
philpem@62 35 state.exp_ram_size = exp_ram_size;
philpem@18 36
philpem@18 37 // Load ROMs
philpem@18 38 FILE *r14c, *r15c;
philpem@18 39 r14c = fopen("roms/14c.bin", "rb");
philpem@55 40 if (r14c == NULL) {
philpem@55 41 fprintf(stderr, "[state] Error loading roms/14c.bin.\n");
philpem@55 42 return -3;
philpem@55 43 }
philpem@18 44 r15c = fopen("roms/15c.bin", "rb");
philpem@55 45 if (r15c == NULL) {
philpem@55 46 fprintf(stderr, "[state] Error loading roms/15c.bin.\n");
philpem@55 47 return -3;
philpem@55 48 }
philpem@18 49
philpem@18 50 // get ROM file size
philpem@18 51 fseek(r14c, 0, SEEK_END);
philpem@18 52 size_t romlen = ftell(r14c);
philpem@18 53 fseek(r14c, 0, SEEK_SET);
philpem@18 54 fseek(r15c, 0, SEEK_END);
philpem@18 55 size_t romlen2 = ftell(r15c);
philpem@18 56 fseek(r15c, 0, SEEK_SET);
philpem@55 57 if (romlen2 != romlen) {
philpem@55 58 fprintf(stderr, "[state] ROMs are not the same size!\n");
philpem@55 59 return -3;
philpem@55 60 }
philpem@55 61 if ((romlen + romlen2) > ROM_SIZE) {
philpem@55 62 fprintf(stderr, "[state] ROM files are too large!\n");
philpem@55 63 return -3;
philpem@55 64 }
philpem@18 65
philpem@18 66 // sanity checks completed; load the ROMs!
philpem@18 67 uint8_t *romdat1, *romdat2;
philpem@18 68 romdat1 = malloc(romlen);
philpem@18 69 romdat2 = malloc(romlen2);
philpem@18 70 fread(romdat1, 1, romlen, r15c);
philpem@18 71 fread(romdat2, 1, romlen2, r14c);
philpem@18 72
philpem@18 73 // convert the ROM data
philpem@18 74 for (size_t i=0; i<(romlen + romlen2); i+=2) {
philpem@18 75 state.rom[i+0] = romdat1[i/2];
philpem@18 76 state.rom[i+1] = romdat2[i/2];
philpem@18 77 }
philpem@18 78
philpem@18 79 // TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
philpem@18 80
philpem@18 81 // free the data arrays and close the files
philpem@18 82 free(romdat1);
philpem@18 83 free(romdat2);
philpem@18 84 fclose(r14c);
philpem@18 85 fclose(r15c);
philpem@18 86
philpem@52 87 // Initialise the disc controller
philpem@52 88 wd2797_init(&state.fdc_ctx);
philpem@52 89
philpem@18 90 return 0;
philpem@18 91 }
philpem@18 92
philpem@18 93 void state_done()
philpem@18 94 {
philpem@60 95 if (state.base_ram != NULL) {
philpem@60 96 free(state.base_ram);
philpem@60 97 state.base_ram = NULL;
philpem@18 98 }
philpem@62 99
philpem@62 100 if (state.exp_ram != NULL) {
philpem@62 101 free(state.exp_ram);
philpem@62 102 state.exp_ram = NULL;
philpem@62 103 }
philpem@62 104
philpem@52 105 // Deinitialise the disc controller
philpem@52 106 wd2797_done(&state.fdc_ctx);
philpem@18 107 }
philpem@18 108
philpem@18 109