src/state.h

Mon, 14 Jan 2013 09:22:12 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Mon, 14 Jan 2013 09:22:12 +0000
changeset 118
feee84e0b3bf
parent 112
a392eb8f9806
child 142
cfa30b1cd92f
child 151
b63a3999e2e7
permissions
-rw-r--r--

More bus error fixes for FreeBee

I have fixed two more bus error handling bugs in FreeBee. First, the CPU core was executing the instruction regardless of whether a bus error occurs when fetching the opcode (which caused it to execute a bogus instruction in such cases). The other one was related to one of my previous fixes - the jump to the bus error vector was at the beginning of the main loop, so it wouldn't be called immediately after the bus error occurred if the timeslot expired, causing the return address to be off.

With these fixes, Unix now runs enough to get into userspace and run the install script (it is also possible to break out and get a shell prompt). However, many commands segfault semi-randomly (or more specifically, it seems that some child processes forked by the shell might be segfaulting before they can exec the command program), so installing the system isn't possible yet. I am not sure exactly what the bug is, but it seems to be related to some function in the shell returning null when the code calling it is assuming that it won't. What the function is, or why it is returning null, I'm not sure (the shell is built without the shared libc and is stripped, making identifying the function harder). I suspect that the function might be in libc, but that is hard to tell.

Author: Andrew Warkentin <andreww591 gmail com>

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