src/wd279x.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 79
674226015c8a
child 111
4c85846b09cd
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 #ifndef _WD279X_H
     2 #define _WD279X_H
     4 #include <stdbool.h>
     5 #include <stddef.h>
     6 #include <stdint.h>
     7 #include <stdio.h>
     9 /// WD279x registers
    10 typedef enum {
    11 	WD2797_REG_STATUS		= 0,		///< Status register
    12 	WD2797_REG_COMMAND		= 0,		///< Command register
    13 	WD2797_REG_TRACK		= 1,		///< Track register
    14 	WD2797_REG_SECTOR		= 2,		///< Sector register
    15 	WD2797_REG_DATA			= 3			///< Data register
    16 } WD2797_REG;
    18 /// WD279x emulator error codes
    19 typedef enum {
    20 	WD2797_ERR_OK			= 0,		///< Operation succeeded
    21 	WD2797_ERR_BAD_GEOM		= -1,		///< Bad geometry, or image file too small
    22 	WD2797_ERR_NO_MEMORY	= -2		///< Out of memory
    23 } WD2797_ERR;
    25 typedef struct {
    26 	// Current track, head and sector
    27 	int						track, head, sector;
    28 	// Geometry of current disc
    29 	int						geom_secsz, geom_spt, geom_heads, geom_tracks;
    30 	// IRQ status
    31 	bool					irq;
    32 	// Status of last command
    33 	uint8_t					status;
    34 	// Last command uses DRQ bit?
    35 	bool					cmd_has_drq;
    36 	// The last value written to the data register
    37 	uint8_t					data_reg;
    38 	// Last step direction. -1 for "towards zero", 1 for "away from zero"
    39 	int						last_step_dir;
    40 	// Data buffer, current DRQ pointer and length
    41 	uint8_t					*data;
    42 	size_t					data_pos, data_len;
    43 	// Current disc image file
    44 	FILE					*disc_image;
    45 } WD2797_CTX;
    47 /**
    48  * @brief	Initialise a WD2797 context.
    49  * @param	ctx		WD2797 context.
    50  *
    51  * This must be run once when the context is created.
    52  */
    53 void wd2797_init(WD2797_CTX *ctx);
    55 /**
    56  * @brief	Reset a WD2797 context.
    57  * @param	ctx		WD2797 context.
    58  *
    59  * This should be run if the WD2797 needs to be reset (nRST line toggled).
    60  */
    61 void wd2797_reset(WD2797_CTX *ctx);
    63 /**
    64  * Deinitialise a WD2797 context.
    65  * @param	ctx		WD2797 context.
    66  */
    67 void wd2797_done(WD2797_CTX *ctx);
    69 /**
    70  * @brief	Read IRQ Rising Edge status. Clears Rising Edge status if it is set.
    71  * @note	No more IRQs will be sent until the Status Register is read, or a new command is written to the CR.
    72  * @param	ctx		WD2797 context.
    73  */
    74 bool wd2797_get_irq(WD2797_CTX *ctx);
    76 /**
    77  * @brief	Read DRQ status.
    78  * @param	ctx		WD2797 context.
    79  */
    80 bool wd2797_get_drq(WD2797_CTX *ctx);
    82 /**
    83  * @brief	Assign a disc image to the WD2797.
    84  * @param	ctx		WD2797 context.
    85  * @param	fp		Disc image file, already opened in "r+b" mode.
    86  * @param	secsz	Sector size: either 128, 256, 512 or 1024.
    87  * @param	spt		Sectors per track.
    88  * @param	heads	Number of heads (1 or 2).
    89  * @return	Error code; WD279X_E_OK if everything worked OK.
    90  */
    91 WD2797_ERR wd2797_load(WD2797_CTX *ctx, FILE *fp, int secsz, int spt, int heads);
    93 /**
    94  * @brief	Deassign the current image file.
    95  * @param	ctx		WD2797 context.
    96  */
    97 void wd2797_unload(WD2797_CTX *ctx);
    99 /**
   100  * @brief	Read WD279x register.
   101  * @param	ctx		WD2797 context
   102  * @param	addr	Register address (0, 1, 2 or 3)
   103  */
   104 uint8_t wd2797_read_reg(WD2797_CTX *ctx, uint8_t addr);
   106 /**
   107  * @brief	Write WD279X register
   108  * @param	ctx		WD2797 context
   109  * @param	addr	Register address (0, 1, 2 or 3)
   110  * @param	val		Value to write
   111  */
   112 void wd2797_write_reg(WD2797_CTX *ctx, uint8_t addr, uint8_t val);
   115 #endif