src/state.c

Wed, 02 Mar 2011 07:16:32 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 02 Mar 2011 07:16:32 +0000
changeset 97
240e195e4bed
parent 80
9581358e92b0
child 101
e20f02519835
permissions
-rw-r--r--

Add 60Hz timer tick patch from Andrew Warkentin <andreww591 gmail com>

... I have also attached a patch that adds the 60Hz timer interrupt (I'm not sure if it's totally correct, though, since the cursor blinks rather slowly).

Received-From: Andrew Warkentin <andreww591 gmail com>
Signed-Off-By: Philip Pemberton <philpem@philpem.me.uk>

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