src/main.c

changeset 18
320dc6206f52
parent 17
138cb2576dbc
child 20
bea459bc22a8
     1.1 --- a/src/main.c	Sun Nov 28 23:29:00 2010 +0000
     1.2 +++ b/src/main.c	Mon Nov 29 00:20:40 2010 +0000
     1.3 @@ -4,10 +4,11 @@
     1.4  #include <stdbool.h>
     1.5  #include <malloc.h>
     1.6  #include <string.h>
     1.7 +
     1.8  #include "musashi/m68k.h"
     1.9 +
    1.10  #include "version.h"
    1.11 -
    1.12 -void state_done(void);
    1.13 +#include "state.h"
    1.14  
    1.15  void FAIL(char *err)
    1.16  {
    1.17 @@ -16,85 +17,6 @@
    1.18  	exit(EXIT_FAILURE);
    1.19  }
    1.20  
    1.21 -// Maximum size of the Boot PROMs. Must be a binary power of two.
    1.22 -#define ROM_SIZE 32768
    1.23 -
    1.24 -struct {
    1.25 -	// Boot PROM can be up to 32Kbytes total size
    1.26 -	uint8_t		rom[ROM_SIZE];
    1.27 -
    1.28 -	// Main system RAM
    1.29 -	uint8_t		*ram;
    1.30 -	size_t		ram_size;			// number of RAM bytes allocated
    1.31 -
    1.32 -	// GENERAL CONTROL REGISTER
    1.33 -	bool		romlmap;
    1.34 -} state;
    1.35 -
    1.36 -int state_init()
    1.37 -{
    1.38 -	// Free RAM if it's allocated
    1.39 -	if (state.ram != NULL)
    1.40 -		free(state.ram);
    1.41 -
    1.42 -	// Initialise hardware registers
    1.43 -	state.romlmap = false;
    1.44 -
    1.45 -	// Allocate RAM, making sure the user has specified a valid RAM amount first
    1.46 -	// Basically: 512KiB minimum, 4MiB maximum, in increments of 512KiB.
    1.47 -	if ((state.ram_size < 512*1024) || ((state.ram_size % (512*1024)) != 0))
    1.48 -		return -1;
    1.49 -	state.ram = malloc(state.ram_size);
    1.50 -	if (state.ram == NULL)
    1.51 -		return -2;
    1.52 -
    1.53 -	// Load ROMs
    1.54 -	FILE *r14c, *r15c;
    1.55 -	r14c = fopen("roms/14c.bin", "rb");
    1.56 -	if (r14c == NULL) FAIL("unable to open roms/14c.bin");
    1.57 -	r15c = fopen("roms/15c.bin", "rb");
    1.58 -	if (r15c == NULL) FAIL("unable to open roms/15c.bin");
    1.59 -
    1.60 -	// get ROM file size
    1.61 -	fseek(r14c, 0, SEEK_END);
    1.62 -	size_t romlen = ftell(r14c);
    1.63 -	fseek(r14c, 0, SEEK_SET);
    1.64 -	fseek(r15c, 0, SEEK_END);
    1.65 -	size_t romlen2 = ftell(r15c);
    1.66 -	fseek(r15c, 0, SEEK_SET);
    1.67 -	if (romlen2 != romlen) FAIL("ROMs are not the same size!");
    1.68 -	if ((romlen + romlen2) > ROM_SIZE) FAIL("ROMs are too large to fit in memory!");
    1.69 -
    1.70 -	// sanity checks completed; load the ROMs!
    1.71 -	uint8_t *romdat1, *romdat2;
    1.72 -	romdat1 = malloc(romlen);
    1.73 -	romdat2 = malloc(romlen2);
    1.74 -	fread(romdat1, 1, romlen, r15c);
    1.75 -	fread(romdat2, 1, romlen2, r14c);
    1.76 -
    1.77 -	// convert the ROM data
    1.78 -	for (size_t i=0; i<(romlen + romlen2); i+=2) {
    1.79 -		state.rom[i+0] = romdat1[i/2];
    1.80 -		state.rom[i+1] = romdat2[i/2];
    1.81 -	}
    1.82 -
    1.83 -	// TODO: if ROM buffer not filled, repeat the ROM data we read until it is (wraparound emulation)
    1.84 -
    1.85 -	// free the data arrays and close the files
    1.86 -	free(romdat1);
    1.87 -	free(romdat2);
    1.88 -	fclose(r14c);
    1.89 -	fclose(r15c);
    1.90 -
    1.91 -	return 0;
    1.92 -}
    1.93 -
    1.94 -void state_done()
    1.95 -{
    1.96 -	if (state.ram != NULL)
    1.97 -		free(state.ram);
    1.98 -}
    1.99 -
   1.100  // read m68k memory
   1.101  uint32_t m68k_read_memory_32(uint32_t address)
   1.102  {
   1.103 @@ -255,8 +177,7 @@
   1.104  
   1.105  	// set up system state
   1.106  	// 512K of RAM
   1.107 -	state.ram_size = 512*1024;
   1.108 -	state_init();
   1.109 +	state_init(512*1024);
   1.110  
   1.111  	// set up musashi
   1.112  	m68k_set_cpu_type(M68K_CPU_TYPE_68010);