src/state.h

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).

philpem@18 1 #ifndef _STATE_H
philpem@18 2 #define _STATE_H
philpem@18 3
philpem@18 4 #include <stddef.h>
philpem@18 5 #include <stdint.h>
philpem@18 6 #include <stdbool.h>
philpem@52 7 #include "wd279x.h"
philpem@80 8 #include "keyboard.h"
philpem@18 9
philpem@18 10 // Maximum size of the Boot PROMs. Must be a binary power of two.
philpem@18 11 #define ROM_SIZE 32768
philpem@18 12
philpem@18 13 /**
philpem@55 14 * State error codes
philpem@55 15 */
philpem@55 16 typedef enum {
philpem@55 17 STATE_E_OK = 0, ///< Operation succeeded
philpem@55 18 STATE_E_BAD_RAMSIZE = -1, ///< Bad RAM size specified (not a multiple of 512K, or less than 512K)
philpem@55 19 STATE_E_NO_MEMORY = -2, ///< Out of memory while allocating state variables
philpem@55 20 STATE_E_ROM_LOAD_FAIL = -3 ///< Error loading ROMs
philpem@55 21 } STATE_ERR;
philpem@55 22
philpem@55 23 /**
philpem@18 24 * @brief Emulator state storage
philpem@18 25 *
philpem@18 26 * This structure stores the internal state of the emulator.
philpem@18 27 */
philpem@18 28 typedef struct {
philpem@18 29 // Boot PROM can be up to 32Kbytes total size
philpem@18 30 uint8_t rom[ROM_SIZE]; ///< Boot PROM data buffer
philpem@18 31
philpem@52 32 //// Main system RAM
philpem@60 33 uint8_t *base_ram; ///< Base RAM data buffer
philpem@60 34 size_t base_ram_size; ///< Size of Base RAM buffer in bytes
philpem@62 35 uint8_t *exp_ram; ///< Expansion RAM data buffer
philpem@62 36 size_t exp_ram_size; ///< Size of Expansion RAM buffer in bytes
philpem@18 37
philpem@52 38 /// Video RAM
philpem@52 39 uint8_t vram[0x8000];
philpem@24 40
philpem@52 41 /// Map RAM
philpem@52 42 uint8_t map[0x800];
philpem@26 43
philpem@52 44 //// Registers
philpem@37 45 uint16_t genstat; ///< General Status Register
philpem@37 46 uint16_t bsr0; ///< Bus Status Register 0
philpem@37 47 uint16_t bsr1; ///< Bus Status Register 1
philpem@32 48
philpem@52 49 //// MISCELLANEOUS CONTROL REGISTER
philpem@52 50 bool dma_reading; ///< True if Disc DMA reads from the controller, false otherwise
philpem@46 51 uint8_t leds; ///< LED status, 1=on, in order red3/green2/yellow1/red0 from bit3 to bit0
philpem@46 52
philpem@97 53 bool timer_enabled;
philpem@97 54 bool timer_asserted;
philpem@97 55
philpem@52 56 //// GENERAL CONTROL REGISTER
philpem@18 57 /// GENCON.ROMLMAP -- false ORs the address with 0x800000, forcing the
philpem@18 58 /// 68010 to access ROM instead of RAM when booting. TRM page 2-36.
philpem@24 59 bool romlmap;
philpem@44 60 /// GENCON.PIE -- Parity Error Check Enable
philpem@44 61 bool pie;
philpem@101 62 /// GENCON.EE -- Error Enable
philpem@101 63 bool ee;
philpem@52 64
philpem@52 65 /// DMA Address Register
philpem@52 66 uint32_t dma_address;
philpem@52 67
philpem@53 68 /// DMA count
philpem@53 69 uint32_t dma_count;
philpem@53 70
philpem@53 71 /// DMA direction
philpem@53 72 bool idmarw;
philpem@53 73 /// DMA enable
philpem@53 74 bool dmaen;
philpem@53 75 bool dmaenb;
philpem@53 76
philpem@52 77 /// Floppy disc controller context
philpem@52 78 WD2797_CTX fdc_ctx;
philpem@95 79 /// Current disc image file
philpem@95 80 FILE *fdc_disc;
philpem@80 81
philpem@80 82 /// Keyboard controller context
philpem@80 83 KEYBOARD_STATE kbd;
philpem@18 84 } S_state;
philpem@18 85
philpem@18 86 // Global emulator state. Yes, I know global variables are evil, please don't
philpem@18 87 // email me and lecture me about it. -philpem
philpem@18 88 #ifndef _STATE_C
philpem@18 89 extern S_state state;
philpem@18 90 #else
philpem@18 91 S_state state;
philpem@18 92 #endif
philpem@18 93
philpem@18 94 /**
philpem@18 95 * @brief Initialise system state
philpem@18 96 *
philpem@62 97 * @param base_ram_size Base RAM size in bytes -- must be a multiple of 512KiB, min 512KiB, max 2MiB.
philpem@62 98 * @param exp_ram_size Expansion RAM size in bytes -- must be a multiple of 512KiB, min 0, max 2MiB.
philpem@18 99 *
philpem@18 100 * Initialises the emulator's internal state.
philpem@18 101 */
philpem@62 102 int state_init(size_t base_ram_size, size_t exp_ram_size);
philpem@18 103
philpem@18 104 /**
philpem@18 105 * @brief Deinitialise system state
philpem@18 106 *
philpem@18 107 * Deinitialises the saved state, and frees all memory. Call this function
philpem@18 108 * before exiting your program to avoid memory leaks.
philpem@18 109 */
philpem@18 110 void state_done();
philpem@18 111
philpem@18 112 #endif