Fri, 04 Mar 2011 00:41:52 +0000
add Error Enable bit to system state
1 #define _STATE_C
2 #include <stddef.h>
3 #include <malloc.h>
4 #include <stdio.h>
5 #include "wd279x.h"
6 #include "keyboard.h"
7 #include "state.h"
9 int state_init(size_t base_ram_size, size_t exp_ram_size)
10 {
11 // Free RAM if it's allocated
12 if (state.base_ram != NULL)
13 free(state.base_ram);
14 if (state.exp_ram != NULL)
15 free(state.exp_ram);
17 // Initialise hardware registers
18 state.romlmap = false;
19 state.idmarw = state.dmaen = state.dmaenb = false;
20 state.dma_count = state.dma_address = 0;
21 state.pie = 0;
22 state.ee = 0;
23 state.leds = 0;
24 state.genstat = 0; // FIXME: check this
25 state.bsr0 = state.bsr1 = 0; // FIXME: check this
26 state.timer_enabled = state.timer_asserted = false;
27 // Allocate Base RAM, making sure the user has specified a valid RAM amount first
28 // Basically: 512KiB minimum, 2MiB maximum, in increments of 512KiB.
29 if ((base_ram_size < 512*1024) || (base_ram_size > 2048*1024) || ((base_ram_size % (512*1024)) != 0))
30 return -1;
31 state.base_ram = malloc(base_ram_size);
32 if (state.base_ram == NULL)
33 return -2;
34 state.base_ram_size = base_ram_size;
36 // Now allocate expansion RAM
37 // The difference here is that we can have zero bytes of Expansion RAM; we're not limited to having a minimum of 512KiB.
38 if ((exp_ram_size > 2048*1024) || ((exp_ram_size % (512*1024)) != 0))
39 return -1;
40 state.exp_ram = malloc(exp_ram_size);
41 if (state.exp_ram == NULL)
42 return -2;
43 state.exp_ram_size = exp_ram_size;
45 // Load ROMs
46 FILE *r14c, *r15c;
47 r14c = fopen("roms/14c.bin", "rb");
48 if (r14c == NULL) {
49 fprintf(stderr, "[state] Error loading roms/14c.bin.\n");
50 return -3;
51 }
52 r15c = fopen("roms/15c.bin", "rb");
53 if (r15c == NULL) {
54 fprintf(stderr, "[state] Error loading roms/15c.bin.\n");
55 return -3;
56 }
58 // get ROM file size
59 fseek(r14c, 0, SEEK_END);
60 size_t romlen = ftell(r14c);
61 fseek(r14c, 0, SEEK_SET);
62 fseek(r15c, 0, SEEK_END);
63 size_t romlen2 = ftell(r15c);
64 fseek(r15c, 0, SEEK_SET);
65 if (romlen2 != romlen) {
66 fprintf(stderr, "[state] ROMs are not the same size!\n");
67 return -3;
68 }
69 if ((romlen + romlen2) > ROM_SIZE) {
70 fprintf(stderr, "[state] ROM files are too large!\n");
71 return -3;
72 }
74 // sanity checks completed; load the ROMs!
75 uint8_t *romdat1, *romdat2;
76 romdat1 = malloc(romlen);
77 romdat2 = malloc(romlen2);
78 fread(romdat1, 1, romlen, r15c);
79 fread(romdat2, 1, romlen2, r14c);
81 // convert the ROM data
82 for (size_t i=0; i<(romlen + romlen2); i+=2) {
83 state.rom[i+0] = romdat1[i/2];
84 state.rom[i+1] = romdat2[i/2];
85 }
87 // TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
89 // free the data arrays and close the files
90 free(romdat1);
91 free(romdat2);
92 fclose(r14c);
93 fclose(r15c);
95 // Initialise the disc controller
96 wd2797_init(&state.fdc_ctx);
97 // Initialise the keyboard controller
98 keyboard_init(&state.kbd);
100 return 0;
101 }
103 void state_done()
104 {
105 if (state.base_ram != NULL) {
106 free(state.base_ram);
107 state.base_ram = NULL;
108 }
110 if (state.exp_ram != NULL) {
111 free(state.exp_ram);
112 state.exp_ram = NULL;
113 }
115 // Deinitialise the disc controller
116 wd2797_done(&state.fdc_ctx);
117 }