src/state.c

Tue, 15 Nov 2011 10:12:37 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 15 Nov 2011 10:12:37 +0000
changeset 109
2f8afb9e5baa
parent 101
e20f02519835
child 112
a392eb8f9806
permissions
-rw-r--r--

[musashi] Fix handling of bus errors

Patch-Author: Andrew Warkentin <andreww591!gmail>
Patch-MessageID: <4EC200CE.2020304@gmail.com>

I have fixed the first page fault test failure in FreeBee (the page fault test now hangs rather than errors out, because it is trying to read from the hard drive to test DMA page faults).

There were actually two bugs (the first bug was masking the second one).

First, the ancient version of Musashi that you used is unable to properly resume from bus errors that happen in the middle of certain instructions (some instructions are fetched in stages, with the PC being advanced to each part of the instruction, so basically what happens is the CPU core attempts to read the memory location referenced by the first operand, the bus error occurs, causing the PC to jump to the exception vector, but the faulting instruction is still in the middle of being fetched, so the PC is then advanced past the beginning of the exception handler). I fixed this by delaying the jump to the bus error vector until after the faulting instruction finishes.

The second bug is simpler - you had the UDS and LDS bits in BSR0 inverted (they are supposed to be active low).

     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 }