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