src/memory.c

Fri, 18 Jan 2013 17:03:48 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Fri, 18 Jan 2013 17:03:48 +0000
branch
experimental_memory_mapper_v2
changeset 128
3246b74d96bc
parent 121
15ae2788e848
child 132
8a7dc9b5b1db
permissions
-rw-r--r--

experimental memory mapper, not quite working

philpem@40 1 #include <stdio.h>
philpem@40 2 #include <stdlib.h>
philpem@40 3 #include <stdint.h>
philpem@40 4 #include <stdbool.h>
philpem@59 5 #include <assert.h>
philpem@40 6 #include "musashi/m68k.h"
philpem@40 7 #include "state.h"
philpem@100 8 #include "utils.h"
philpem@40 9 #include "memory.h"
philpem@40 10
philpem@119 11 // The value which will be returned if the CPU attempts to read from empty memory
philpem@119 12 // TODO (FIXME?) - need to figure out if R/W ops wrap around. This seems to appease the UNIX kernel and P4TEST.
philpem@119 13 #define EMPTY 0xFFFFFFFFUL
philpem@119 14 // #define EMPTY 0x55555555UL
philpem@119 15
philpem@40 16 /******************
philpem@40 17 * Memory mapping
philpem@40 18 ******************/
philpem@40 19
philpem@128 20 /// Set a page bit
philpem@128 21 #define MAP_SET_PAGEBIT(addr, bit) state.map[(((addr) >> 12) & 0x3FF)*2] |= (bit << 2)
philpem@128 22 /// Clear a page bit
philpem@128 23 #define MAP_CLR_PAGEBIT(addr, bit) state.map[(((addr) >> 12) & 0x3FF)*2] &= ~(bit << 2)
philpem@40 24
philpem@40 25
philpem@40 26 /********************************************************
philpem@40 27 * m68k memory read/write support functions for Musashi
philpem@40 28 ********************************************************/
philpem@40 29
philpem@40 30 /**
philpem@40 31 * @brief Check memory access permissions for a write operation.
philpem@40 32 * @note This used to be a single macro (merged with ACCESS_CHECK_RD), but
philpem@40 33 * gcc throws warnings when you have a return-with-value in a void
philpem@40 34 * function, even if the return-with-value is completely unreachable.
philpem@40 35 * Similarly it doesn't like it if you have a return without a value
philpem@40 36 * in a non-void function, even if it's impossible to ever reach the
philpem@40 37 * return-with-no-value. UGH!
philpem@40 38 */
philpem@59 39 /*{{{ macro: ACCESS_CHECK_WR(address, bits)*/
philpem@59 40 #define ACCESS_CHECK_WR(address, bits) \
philpem@59 41 do { \
philpem@128 42 if (access_check_cpu(address, bits, true)) { \
philpem@40 43 return; \
philpem@40 44 } \
philpem@70 45 } while (0)
philpem@59 46 /*}}}*/
philpem@40 47
philpem@40 48 /**
philpem@40 49 * @brief Check memory access permissions for a read operation.
philpem@40 50 * @note This used to be a single macro (merged with ACCESS_CHECK_WR), but
philpem@40 51 * gcc throws warnings when you have a return-with-value in a void
philpem@40 52 * function, even if the return-with-value is completely unreachable.
philpem@40 53 * Similarly it doesn't like it if you have a return without a value
philpem@40 54 * in a non-void function, even if it's impossible to ever reach the
philpem@40 55 * return-with-no-value. UGH!
philpem@40 56 */
philpem@59 57 /*{{{ macro: ACCESS_CHECK_RD(address, bits)*/
philpem@59 58 #define ACCESS_CHECK_RD(address, bits) \
philpem@59 59 do { \
philpem@128 60 if (access_check_cpu(address, bits, false)) { \
philpem@128 61 if (bits == 32) \
philpem@128 62 return EMPTY & 0xFFFFFFFF; \
philpem@40 63 else \
philpem@128 64 return EMPTY & ((1UL << bits)-1); \
philpem@40 65 } \
philpem@70 66 } while (0)
philpem@59 67 /*}}}*/
philpem@40 68
philpem@128 69
philpem@128 70 /**
philpem@128 71 * Update the page bits for a given memory address
philpem@128 72 *
philpem@128 73 * @param addr Memory address being accessed
philpem@128 74 * @param l7intr Set to <i>true</i> if a level-seven interrupt has been
philpem@128 75 * signalled (even if <b>ENABLE ERROR</b> isn't set).
philpem@128 76 * @param write Set to <i>true</i> if the address is being written to.
philpem@128 77 */
philpem@128 78 static void update_page_bits(uint32_t addr, bool l7intr, bool write)
philpem@112 79 {
philpem@128 80 bool ps0_state = false;
philpem@128 81
philpem@128 82 // Don't try and update pagebits for non-RAM addresses
philpem@128 83 if (addr > 0x3FFFFF)
philpem@128 84 return;
philpem@128 85
philpem@128 86 if (l7intr) {
philpem@128 87 // if (!(MAP_PAGEBITS(addr) & PAGE_BIT_PS0)) {
philpem@128 88 // FIXME FUCKUP The ruddy TRM is wrong AGAIN! If above line is uncommented, Really Bad Things Happen.
philpem@128 89 if ((MAP_PAGEBITS(addr) & PAGE_BIT_PS0)) {
philpem@128 90 // Level 7 interrupt, PS0 clear, PS1 don't-care. Set PS0.
philpem@128 91 ps0_state = true;
philpem@128 92 }
philpem@128 93 } else {
philpem@128 94 // No L7 interrupt
philpem@128 95 if ((write && !(MAP_PAGEBITS(addr) & PAGE_BIT_PS1) && (MAP_PAGEBITS(addr) & PAGE_BIT_PS0)) ||
philpem@128 96 (write && (MAP_PAGEBITS(addr) & PAGE_BIT_PS1) && !(MAP_PAGEBITS(addr) & PAGE_BIT_PS0)))
philpem@128 97 {
philpem@128 98 // No L7 interrupt, PS[1:0] = 0b01, write
philpem@128 99 // No L7 interrupt, PS[1:0] = 0b10, write
philpem@128 100 ps0_state = true;
philpem@128 101 }
philpem@128 102 }
philpem@112 103
philpem@128 104 #ifdef MAPRAM_BIT_TEST
philpem@128 105 LOG("Starting Mapram Bit Test");
philpem@128 106 state.map[0] = state.map[1] = 0;
philpem@128 107 LOG("Start = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 108 MAP_SET_PAGEBIT(0, PAGE_BIT_WE);
philpem@128 109 LOG("Set WE = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 110 MAP_SET_PAGEBIT(0, PAGE_BIT_PS1);
philpem@128 111 LOG("Set PS1 = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 112 MAP_SET_PAGEBIT(0, PAGE_BIT_PS0);
philpem@128 113 LOG("Set PS0 = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 114
philpem@128 115 MAP_CLR_PAGEBIT(0, PAGE_BIT_WE);
philpem@128 116 LOG("Clr WE = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 117 MAP_CLR_PAGEBIT(0, PAGE_BIT_PS1);
philpem@128 118 LOG("Clr PS1 = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 119 MAP_CLR_PAGEBIT(0, PAGE_BIT_PS0);
philpem@128 120 LOG("Clr PS0 = %04X %02X", MAPRAM_ADDR(0), MAP_PAGEBITS(0));
philpem@128 121 exit(-1);
philpem@128 122 #endif
philpem@128 123
philpem@128 124 uint16_t old_pagebits = MAP_PAGEBITS(addr);
philpem@112 125
philpem@128 126 // PS1 is always set on access
philpem@128 127 MAP_SET_PAGEBIT(addr, PAGE_BIT_PS1);
philpem@128 128
philpem@128 129 uint16_t new_pagebit1 = MAP_PAGEBITS(addr);
philpem@128 130
philpem@128 131 // Update PS0
philpem@128 132 if (ps0_state) {
philpem@128 133 MAP_SET_PAGEBIT(addr, PAGE_BIT_PS0);
philpem@128 134 } else {
philpem@128 135 MAP_CLR_PAGEBIT(addr, PAGE_BIT_PS0);
philpem@128 136 }
philpem@112 137
philpem@128 138 uint16_t new_pagebit2 = MAP_PAGEBITS(addr);
philpem@128 139 switch (addr) {
philpem@128 140 case 0x000000:
philpem@128 141 case 0x001000:
philpem@128 142 case 0x002000:
philpem@128 143 case 0x003000:
philpem@128 144 case 0x004000:
philpem@128 145 case 0x033000:
philpem@128 146 case 0x034000:
philpem@128 147 case 0x035000:
philpem@128 148 LOG("Addr %08X MapNew %04X Pagebit update -- ps0 %d, %02X => %02X => %02X", addr, MAPRAM_ADDR(addr), ps0_state, old_pagebits, new_pagebit1, new_pagebit2);
philpem@128 149 default:
philpem@112 150 break;
philpem@112 151 }
philpem@128 152 }
philpem@128 153
philpem@128 154 bool access_check_dma(void)
philpem@128 155 {
philpem@128 156 // TODO FIXME BUGBUG Sanity check - Make sure DMAC is only accessing RAM addresses
philpem@128 157
philpem@128 158 // DMA access check -- make sure the page is mapped in
philpem@128 159 if (!(MAP_PAGEBITS(state.dma_address) & PAGE_BIT_PS0) && !(MAP_PAGEBITS(state.dma_address) & PAGE_BIT_PS1)) {
philpem@128 160 // DMA access to page which is not mapped in.
philpem@128 161 // Level 7 interrupt, page fault, DMA invoked
philpem@128 162 state.genstat = 0xABFF
philpem@128 163 | (state.dma_reading ? 0x4000 : 0)
philpem@128 164 | (state.pie ? 0x0400 : 0);
philpem@128 165
philpem@128 166 // XXX: Check all this stuff.
philpem@112 167 state.bsr0 = 0x3C00;
philpem@112 168 state.bsr0 |= (state.dma_address >> 16);
philpem@112 169 state.bsr1 = state.dma_address & 0xffff;
philpem@128 170
philpem@128 171 // Update page bits for this transfer
philpem@128 172 update_page_bits(state.dma_address, true, !state.dma_reading);
philpem@128 173
philpem@128 174 // XXX: is this right?
philpem@128 175 // Fire a Level 7 interrupt
philpem@128 176 /*if (state.ee)*/ m68k_set_irq(7);
philpem@128 177
philpem@128 178 LOG("BUS ERROR FROM DMA: genstat=%04X, bsr0=%04X, bsr1=%04X\n", state.genstat, state.bsr0, state.bsr1);
philpem@128 179 return false;
philpem@128 180 } else {
philpem@128 181 // No errors. Just update the page bits.
philpem@128 182 update_page_bits(state.dma_address, false, !state.dma_reading);
philpem@128 183 return true;
philpem@112 184 }
philpem@128 185 }
philpem@128 186
philpem@128 187 /**
philpem@128 188 * Check memory access permissions for a CPU memory access.
philpem@128 189 *
philpem@128 190 * @param addr Virtual memory address being accessed (from CPU address bus).
philpem@128 191 * @param bits Word size of this transfer (8, 16 or 32 bits).
philpem@128 192 * @param write <i>true</i> if this is a write operation, <i>false</i> if it is a read operation.
philpem@128 193 * @return <i>true</i> if the access was denied and a level-7 interrupt and/or bus error raised.
philpem@128 194 * <i>false</i> if the access was allowed.
philpem@128 195 */
philpem@128 196 bool access_check_cpu(uint32_t addr, int bits, bool write)
philpem@128 197 {
philpem@128 198 bool supervisor = (m68k_get_reg(NULL, M68K_REG_SR) & 0x2000);
philpem@128 199 bool fault = false;
philpem@128 200
philpem@128 201 // TODO FIXME BUGBUG? Do we need to check for supervisor access here?
philpem@128 202 if ((addr >= 0x000000) && (addr <= 0x3FFFFF) && !(MAP_PAGEBITS(addr) & PAGE_BIT_PS1) && !(MAP_PAGEBITS(addr) & PAGE_BIT_PS0)) {
philpem@128 203 // (A) Page Fault -- user access to page which is not mapped in
philpem@128 204 // Level 7 Interrupt, Bus Error, regs=PAGEFAULT
philpem@128 205 if (write) {
philpem@128 206 state.genstat = 0x8BFF | (state.pie ? 0x0400 : 0);
philpem@128 207 } else {
philpem@128 208 state.genstat = 0xCBFF | (state.pie ? 0x0400 : 0);
philpem@128 209 }
philpem@128 210 fault = true;
philpem@128 211 } else if (!supervisor && (addr >= 0x000000) && (addr <= 0x07FFFF)) {
philpem@128 212 // (B) User attempted to access the kernel
philpem@128 213 // Level 7 Interrupt, Bus Error, regs=KERNEL
philpem@128 214 if (write) {
philpem@128 215 // XXX: BUGBUG? Is this correct?
philpem@128 216 state.genstat = 0x9BFF | (state.pie ? 0x0400 : 0);
philpem@128 217 } else {
philpem@128 218 state.genstat = 0xDBFF | (state.pie ? 0x0400 : 0);
philpem@128 219 }
philpem@128 220 fault = true;
philpem@128 221 } else if (!supervisor && write && (addr >= 0x000000) && (addr <= 0x3FFFFF) && !(MAP_PAGEBITS(addr) & PAGE_BIT_WE)) {
philpem@128 222 // (C) User attempted to write to a page which is not write enabled
philpem@128 223 // Level 7 Interrupt, Bus Error, regs=WRITE_EN
philpem@128 224 if (write) {
philpem@128 225 // XXX: BUGBUG? Is this correct?
philpem@128 226 state.genstat = 0x9BFF | (state.pie ? 0x0400 : 0);
philpem@128 227 } else {
philpem@128 228 state.genstat = 0xDBFF | (state.pie ? 0x0400 : 0);
philpem@128 229 }
philpem@128 230 fault = true;
philpem@128 231 } else if (!supervisor && (addr >= 0x400000) && (addr <= 0xFFFFFF)) {
philpem@128 232 // (D) UIE - user I/O exception
philpem@128 233 // Bus Error only, regs=UIE
philpem@128 234 if (write) {
philpem@128 235 state.genstat = 0x9AFF | (state.pie ? 0x0400 : 0);
philpem@128 236 } else {
philpem@128 237 state.genstat = 0xDAFF | (state.pie ? 0x0400 : 0);
philpem@128 238 }
philpem@128 239 fault = true;
philpem@128 240 }
philpem@128 241
philpem@128 242 // Update the page bits first
philpem@128 243 update_page_bits(addr, fault, write);
philpem@128 244
philpem@128 245 if (fault) {
philpem@128 246 if (bits >= 16)
philpem@128 247 state.bsr0 = 0x7C00;
philpem@128 248 else
philpem@128 249 state.bsr0 = (addr & 1) ? 0x7E00 : 0x7D00;
philpem@128 250 // FIXME? Physical or virtual address here?
philpem@128 251 state.bsr0 |= (addr >> 16);
philpem@128 252 state.bsr1 = addr & 0xffff;
philpem@128 253
philpem@128 254 LOG("CPU Bus Error or L7Intr while %s, vaddr %08X, map %08X, pagebits 0x%02X bsr0=%04X bsr1=%04X genstat=%04X",
philpem@128 255 write ? "writing" : "reading", addr,
philpem@128 256 MAPRAM_ADDR(addr & 0x3fffff),
philpem@128 257 MAP_PAGEBITS(addr & 0x3fffff),
philpem@128 258 state.bsr0, state.bsr1, state.genstat);
philpem@128 259
philpem@128 260 // FIXME? BUGBUG? Does EE disable one or both of these?
philpem@128 261 // /*if (state.ee)*/ m68k_set_irq(7);
philpem@128 262 /*if (state.ee)*/ m68k_pulse_bus_error();
philpem@128 263 }
philpem@128 264
philpem@128 265 return fault;
philpem@112 266 }
philpem@112 267
philpem@40 268 // Logging macros
philpem@59 269 #define LOG_NOT_HANDLED_R(bits) \
philpem@128 270 if (!handled) fprintf(stderr, "unhandled read%02d, addr=0x%08X\n", bits, address);
philpem@40 271
philpem@59 272 #define LOG_NOT_HANDLED_W(bits) \
philpem@128 273 if (!handled) fprintf(stderr, "unhandled write%02d, addr=0x%08X, data=0x%08X\n", bits, address, data);
philpem@59 274
philpem@59 275 /********************************************************
philpem@59 276 * I/O read/write functions
philpem@59 277 ********************************************************/
philpem@40 278
philpem@40 279 /**
philpem@59 280 * Issue a warning if a read operation is made with an invalid size
philpem@40 281 */
philpem@66 282 inline static void ENFORCE_SIZE(int bits, uint32_t address, bool read, int allowed, char *regname)
philpem@40 283 {
philpem@59 284 assert((bits == 8) || (bits == 16) || (bits == 32));
philpem@59 285 if ((bits & allowed) == 0) {
philpem@128 286 LOG("WARNING: %s 0x%08X (%s) with invalid size %d!\n", read ? "read from" : "write to", address, regname, bits);
philpem@59 287 }
philpem@59 288 }
philpem@59 289
philpem@66 290 inline static void ENFORCE_SIZE_R(int bits, uint32_t address, int allowed, char *regname)
philpem@40 291 {
philpem@66 292 ENFORCE_SIZE(bits, address, true, allowed, regname);
philpem@66 293 }
philpem@66 294
philpem@66 295 inline static void ENFORCE_SIZE_W(int bits, uint32_t address, int allowed, char *regname)
philpem@66 296 {
philpem@66 297 ENFORCE_SIZE(bits, address, false, allowed, regname);
philpem@66 298 }
philpem@66 299
philpem@59 300 void IoWrite(uint32_t address, uint32_t data, int bits)/*{{{*/
philpem@59 301 {
philpem@40 302 bool handled = false;
philpem@40 303
philpem@59 304 if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 305 // I/O register space, zone A
philpem@40 306 switch (address & 0x0F0000) {
philpem@40 307 case 0x010000: // General Status Register
philpem@59 308 if (bits == 16)
philpem@59 309 state.genstat = (data & 0xffff);
philpem@59 310 else if (bits == 8) {
philpem@59 311 if (address & 0)
philpem@59 312 state.genstat = data;
philpem@59 313 else
philpem@59 314 state.genstat = data << 8;
philpem@59 315 }
philpem@40 316 handled = true;
philpem@40 317 break;
philpem@40 318 case 0x030000: // Bus Status Register 0
philpem@40 319 break;
philpem@40 320 case 0x040000: // Bus Status Register 1
philpem@40 321 break;
philpem@40 322 case 0x050000: // Phone status
philpem@40 323 break;
philpem@40 324 case 0x060000: // DMA Count
philpem@66 325 ENFORCE_SIZE_W(bits, address, 16, "DMACOUNT");
philpem@59 326 state.dma_count = (data & 0x3FFF);
philpem@59 327 state.idmarw = ((data & 0x4000) == 0x4000);
philpem@59 328 state.dmaen = ((data & 0x8000) == 0x8000);
philpem@59 329 // This handles the "dummy DMA transfer" mentioned in the docs
philpem@112 330 // disabled because it causes the floppy test to fail
philpem@112 331 #if 0
philpem@112 332 if (!state.idmarw){
philpem@112 333 if (access_check_dma(true)){
philpem@112 334 uint32_t newAddr = mapAddr(state.dma_address, true);
philpem@112 335 // RAM access
philpem@112 336 if (newAddr <= 0x1fffff)
philpem@112 337 WR16(state.base_ram, newAddr, state.base_ram_size - 1, 0xFF);
philpem@112 338 else if (address <= 0x3FFFFF)
philpem@112 339 WR16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, 0xFF);
philpem@112 340 }
philpem@112 341 }
philpem@112 342 #endif
philpem@59 343 state.dma_count++;
philpem@53 344 handled = true;
philpem@40 345 break;
philpem@40 346 case 0x070000: // Line Printer Status Register
philpem@40 347 break;
philpem@40 348 case 0x080000: // Real Time Clock
philpem@128 349 LOGS("REAL TIME CLOCK WRITE");
philpem@40 350 break;
philpem@40 351 case 0x090000: // Phone registers
philpem@40 352 switch (address & 0x0FF000) {
philpem@40 353 case 0x090000: // Handset relay
philpem@40 354 case 0x098000:
philpem@40 355 break;
philpem@40 356 case 0x091000: // Line select 2
philpem@40 357 case 0x099000:
philpem@40 358 break;
philpem@40 359 case 0x092000: // Hook relay 1
philpem@40 360 case 0x09A000:
philpem@40 361 break;
philpem@40 362 case 0x093000: // Hook relay 2
philpem@40 363 case 0x09B000:
philpem@40 364 break;
philpem@40 365 case 0x094000: // Line 1 hold
philpem@40 366 case 0x09C000:
philpem@40 367 break;
philpem@40 368 case 0x095000: // Line 2 hold
philpem@40 369 case 0x09D000:
philpem@40 370 break;
philpem@40 371 case 0x096000: // Line 1 A-lead
philpem@40 372 case 0x09E000:
philpem@40 373 break;
philpem@40 374 case 0x097000: // Line 2 A-lead
philpem@40 375 case 0x09F000:
philpem@40 376 break;
philpem@40 377 }
philpem@40 378 break;
philpem@59 379 case 0x0A0000: // Miscellaneous Control Register
philpem@66 380 ENFORCE_SIZE_W(bits, address, 16, "MISCCON");
philpem@59 381 // TODO: handle the ctrl bits properly
philpem@97 382 if (data & 0x8000){
philpem@97 383 state.timer_enabled = 1;
philpem@97 384 }else{
philpem@97 385 state.timer_enabled = 0;
philpem@97 386 state.timer_asserted = 0;
philpem@97 387 }
philpem@59 388 state.dma_reading = (data & 0x4000);
philpem@72 389 if (state.leds != ((~data & 0xF00) >> 8)) {
philpem@72 390 state.leds = (~data & 0xF00) >> 8;
philpem@117 391 #ifdef SHOW_LEDS
philpem@72 392 printf("LEDs: %s %s %s %s\n",
philpem@72 393 (state.leds & 8) ? "R" : "-",
philpem@72 394 (state.leds & 4) ? "G" : "-",
philpem@72 395 (state.leds & 2) ? "Y" : "-",
philpem@72 396 (state.leds & 1) ? "R" : "-");
philpem@117 397 #endif
philpem@72 398 }
philpem@46 399 handled = true;
philpem@40 400 break;
philpem@40 401 case 0x0B0000: // TM/DIALWR
philpem@40 402 break;
philpem@59 403 case 0x0C0000: // Clear Status Register
philpem@59 404 state.genstat = 0xFFFF;
philpem@59 405 state.bsr0 = 0xFFFF;
philpem@59 406 state.bsr1 = 0xFFFF;
philpem@43 407 handled = true;
philpem@40 408 break;
philpem@40 409 case 0x0D0000: // DMA Address Register
philpem@59 410 if (address & 0x004000) {
philpem@59 411 // A14 high -- set most significant bits
philpem@59 412 state.dma_address = (state.dma_address & 0x1fe) | ((address & 0x3ffe) << 8);
philpem@59 413 } else {
philpem@59 414 // A14 low -- set least significant bits
philpem@59 415 state.dma_address = (state.dma_address & 0x3ffe00) | (address & 0x1fe);
philpem@59 416 }
philpem@59 417 handled = true;
philpem@40 418 break;
philpem@40 419 case 0x0E0000: // Disk Control Register
philpem@112 420 {
philpem@112 421 bool fd_selected;
philpem@112 422 bool hd_selected;
philpem@112 423 ENFORCE_SIZE_W(bits, address, 16, "DISKCON");
philpem@112 424 // B7 = FDD controller reset
philpem@112 425 if ((data & 0x80) == 0) wd2797_reset(&state.fdc_ctx);
philpem@112 426 // B6 = drive 0 select
philpem@112 427 fd_selected = (data & 0x40) != 0;
philpem@112 428 // B5 = motor enable -- TODO
philpem@112 429 // B4 = HDD controller reset
philpem@112 430 if ((data & 0x10) == 0) wd2010_reset(&state.hdc_ctx);
philpem@112 431 // B3 = HDD0 select
philpem@112 432 hd_selected = (data & 0x08) != 0;
philpem@112 433 // B2,1,0 = HDD0 head select -- TODO?
philpem@112 434 if (hd_selected && !state.hd_selected){
philpem@112 435 state.fd_selected = false;
philpem@112 436 state.hd_selected = true;
philpem@112 437 }else if (fd_selected && !state.fd_selected){
philpem@112 438 state.hd_selected = false;
philpem@112 439 state.fd_selected = true;
philpem@112 440 }
philpem@112 441 handled = true;
philpem@112 442 break;
philpem@112 443 }
philpem@40 444 case 0x0F0000: // Line Printer Data Register
philpem@40 445 break;
philpem@40 446 }
philpem@40 447 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 448 // I/O register space, zone B
philpem@40 449 switch (address & 0xF00000) {
philpem@40 450 case 0xC00000: // Expansion slots
philpem@40 451 case 0xD00000:
philpem@40 452 switch (address & 0xFC0000) {
philpem@40 453 case 0xC00000: // Expansion slot 0
philpem@40 454 case 0xC40000: // Expansion slot 1
philpem@40 455 case 0xC80000: // Expansion slot 2
philpem@40 456 case 0xCC0000: // Expansion slot 3
philpem@40 457 case 0xD00000: // Expansion slot 4
philpem@40 458 case 0xD40000: // Expansion slot 5
philpem@40 459 case 0xD80000: // Expansion slot 6
philpem@40 460 case 0xDC0000: // Expansion slot 7
philpem@59 461 fprintf(stderr, "NOTE: WR%d to expansion card space, addr=0x%08X, data=0x%08X\n", bits, address, data);
philpem@59 462 handled = true;
philpem@40 463 break;
philpem@40 464 }
philpem@40 465 break;
philpem@40 466 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 467 case 0xF00000:
philpem@40 468 switch (address & 0x070000) {
philpem@112 469 case 0x000000: // [ef][08]xxxx ==> WD2010 hard disc controller
philpem@112 470 wd2010_write_reg(&state.hdc_ctx, (address >> 1) & 7, data);
philpem@112 471 handled = true;
philpem@40 472 break;
philpem@40 473 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@112 474 /*ENFORCE_SIZE_W(bits, address, 16, "FDC REGISTERS");*/
philpem@59 475 wd2797_write_reg(&state.fdc_ctx, (address >> 1) & 3, data);
philpem@52 476 handled = true;
philpem@40 477 break;
philpem@40 478 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@116 479 // MCR2 - UNIX PC Rev. P5.1 HDD head select b3 and potential HDD#2 select
philpem@116 480 wd2010_write_reg(&state.hdc_ctx, UNIXPC_REG_MCR2, data);
philpem@116 481 handled = true;
philpem@40 482 break;
philpem@40 483 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@128 484 LOGS("REAL TIME CLOCK DATA WRITE");
philpem@40 485 break;
philpem@40 486 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 487 switch (address & 0x077000) {
philpem@40 488 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@102 489 // Error Enable. If =0, Level7 intrs and bus errors are masked.
philpem@102 490 ENFORCE_SIZE_W(bits, address, 16, "EE");
philpem@102 491 state.ee = ((data & 0x8000) == 0x8000);
philpem@102 492 handled = true;
philpem@59 493 break;
philpem@44 494 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@66 495 ENFORCE_SIZE_W(bits, address, 16, "PIE");
philpem@59 496 state.pie = ((data & 0x8000) == 0x8000);
philpem@59 497 handled = true;
philpem@59 498 break;
philpem@40 499 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@59 500 break;
philpem@40 501 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@66 502 ENFORCE_SIZE_W(bits, address, 16, "ROMLMAP");
philpem@59 503 state.romlmap = ((data & 0x8000) == 0x8000);
philpem@44 504 handled = true;
philpem@40 505 break;
philpem@59 506 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@66 507 ENFORCE_SIZE_W(bits, address, 16, "L1 MODEM");
philpem@59 508 break;
philpem@59 509 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@66 510 ENFORCE_SIZE_W(bits, address, 16, "L2 MODEM");
philpem@59 511 break;
philpem@59 512 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@66 513 ENFORCE_SIZE_W(bits, address, 16, "D/N CONNECT");
philpem@59 514 break;
philpem@59 515 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@66 516 ENFORCE_SIZE_W(bits, address, 16, "WHOLE SCREEN REVERSE VIDEO");
philpem@40 517 break;
philpem@40 518 }
philpem@40 519 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 520 break;
philpem@40 521 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 522 switch (address & 0x07F000) {
philpem@40 523 default:
philpem@40 524 break;
philpem@40 525 }
philpem@40 526 break;
philpem@40 527 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@84 528 // TODO: figure out which sizes are valid (probably just 8 and 16)
philpem@84 529 // ENFORCE_SIZE_W(bits, address, 16, "KEYBOARD CONTROLLER");
philpem@93 530 if (bits == 8) {
philpem@128 531 #ifdef LOG_KEYBOARD_WRITES
philpem@128 532 LOG("KBD WR %02X => %02X\n", (address >> 1) & 3, data);
philpem@128 533 #endif
philpem@93 534 keyboard_write(&state.kbd, (address >> 1) & 3, data);
philpem@93 535 handled = true;
philpem@93 536 } else if (bits == 16) {
philpem@128 537 #ifdef LOG_KEYBOARD_WRITES
philpem@128 538 LOG("KBD WR %02X => %04X\n", (address >> 1) & 3, data);
philpem@128 539 #endif
philpem@93 540 keyboard_write(&state.kbd, (address >> 1) & 3, data >> 8);
philpem@93 541 handled = true;
philpem@93 542 }
philpem@40 543 break;
philpem@40 544 }
philpem@40 545 }
philpem@40 546 }
philpem@40 547
philpem@64 548 LOG_NOT_HANDLED_W(bits);
philpem@59 549 }/*}}}*/
philpem@40 550
philpem@59 551 uint32_t IoRead(uint32_t address, int bits)/*{{{*/
philpem@59 552 {
philpem@59 553 bool handled = false;
philpem@119 554 uint32_t data = EMPTY & 0xFFFFFFFF;
philpem@40 555
philpem@59 556 if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 557 // I/O register space, zone A
philpem@40 558 switch (address & 0x0F0000) {
philpem@40 559 case 0x010000: // General Status Register
philpem@116 560 /* ENFORCE_SIZE_R(bits, address, 16, "GENSTAT"); */
philpem@116 561 if (bits == 32) {
philpem@116 562 return ((uint32_t)state.genstat << 16) + (uint32_t)state.genstat;
philpem@116 563 } else if (bits == 16) {
philpem@116 564 return (uint16_t)state.genstat;
philpem@116 565 } else {
philpem@116 566 return (uint8_t)(state.genstat & 0xff);
philpem@116 567 }
philpem@40 568 break;
philpem@40 569 case 0x030000: // Bus Status Register 0
philpem@66 570 ENFORCE_SIZE_R(bits, address, 16, "BSR0");
philpem@59 571 return ((uint32_t)state.bsr0 << 16) + (uint32_t)state.bsr0;
philpem@40 572 break;
philpem@40 573 case 0x040000: // Bus Status Register 1
philpem@66 574 ENFORCE_SIZE_R(bits, address, 16, "BSR1");
philpem@59 575 return ((uint32_t)state.bsr1 << 16) + (uint32_t)state.bsr1;
philpem@40 576 break;
philpem@40 577 case 0x050000: // Phone status
philpem@66 578 ENFORCE_SIZE_R(bits, address, 8 | 16, "PHONE STATUS");
philpem@40 579 break;
philpem@40 580 case 0x060000: // DMA Count
philpem@55 581 // TODO: U/OERR- is always inactive (bit set)... or should it be = DMAEN+?
philpem@55 582 // Bit 14 is always unused, so leave it set
philpem@66 583 ENFORCE_SIZE_R(bits, address, 16, "DMACOUNT");
philpem@59 584 return (state.dma_count & 0x3fff) | 0xC000;
philpem@40 585 break;
philpem@40 586 case 0x070000: // Line Printer Status Register
philpem@53 587 data = 0x00120012; // no parity error, no line printer error, no irqs from FDD or HDD
philpem@78 588 data |= wd2797_get_irq(&state.fdc_ctx) ? 0x00080008 : 0;
philpem@112 589 data |= wd2010_get_irq(&state.hdc_ctx) ? 0x00040004 : 0;
philpem@59 590 return data;
philpem@40 591 break;
philpem@40 592 case 0x080000: // Real Time Clock
philpem@128 593 LOGS("REAL TIME CLOCK READ");
philpem@40 594 break;
philpem@40 595 case 0x090000: // Phone registers
philpem@40 596 switch (address & 0x0FF000) {
philpem@40 597 case 0x090000: // Handset relay
philpem@40 598 case 0x098000:
philpem@40 599 break;
philpem@40 600 case 0x091000: // Line select 2
philpem@40 601 case 0x099000:
philpem@40 602 break;
philpem@40 603 case 0x092000: // Hook relay 1
philpem@40 604 case 0x09A000:
philpem@40 605 break;
philpem@40 606 case 0x093000: // Hook relay 2
philpem@40 607 case 0x09B000:
philpem@40 608 break;
philpem@40 609 case 0x094000: // Line 1 hold
philpem@40 610 case 0x09C000:
philpem@40 611 break;
philpem@40 612 case 0x095000: // Line 2 hold
philpem@40 613 case 0x09D000:
philpem@40 614 break;
philpem@40 615 case 0x096000: // Line 1 A-lead
philpem@40 616 case 0x09E000:
philpem@40 617 break;
philpem@40 618 case 0x097000: // Line 2 A-lead
philpem@40 619 case 0x09F000:
philpem@40 620 break;
philpem@40 621 }
philpem@40 622 break;
philpem@46 623 case 0x0A0000: // Miscellaneous Control Register -- write only!
philpem@46 624 handled = true;
philpem@40 625 break;
philpem@40 626 case 0x0B0000: // TM/DIALWR
philpem@40 627 break;
philpem@46 628 case 0x0C0000: // Clear Status Register -- write only!
philpem@43 629 handled = true;
philpem@40 630 break;
philpem@40 631 case 0x0D0000: // DMA Address Register
philpem@40 632 break;
philpem@40 633 case 0x0E0000: // Disk Control Register
philpem@40 634 break;
philpem@40 635 case 0x0F0000: // Line Printer Data Register
philpem@40 636 break;
philpem@40 637 }
philpem@40 638 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 639 // I/O register space, zone B
philpem@40 640 switch (address & 0xF00000) {
philpem@40 641 case 0xC00000: // Expansion slots
philpem@40 642 case 0xD00000:
philpem@40 643 switch (address & 0xFC0000) {
philpem@40 644 case 0xC00000: // Expansion slot 0
philpem@40 645 case 0xC40000: // Expansion slot 1
philpem@40 646 case 0xC80000: // Expansion slot 2
philpem@40 647 case 0xCC0000: // Expansion slot 3
philpem@40 648 case 0xD00000: // Expansion slot 4
philpem@40 649 case 0xD40000: // Expansion slot 5
philpem@40 650 case 0xD80000: // Expansion slot 6
philpem@40 651 case 0xDC0000: // Expansion slot 7
philpem@65 652 fprintf(stderr, "NOTE: RD%d from expansion card space, addr=0x%08X\n", bits, address);
philpem@65 653 handled = true;
philpem@40 654 break;
philpem@40 655 }
philpem@40 656 break;
philpem@40 657 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 658 case 0xF00000:
philpem@40 659 switch (address & 0x070000) {
philpem@40 660 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@112 661 return (wd2010_read_reg(&state.hdc_ctx, (address >> 1) & 7));
philpem@112 662
philpem@40 663 break;
philpem@40 664 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@112 665 /*ENFORCE_SIZE_R(bits, address, 16, "FDC REGISTERS");*/
philpem@59 666 return wd2797_read_reg(&state.fdc_ctx, (address >> 1) & 3);
philpem@40 667 break;
philpem@40 668 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 669 break;
philpem@40 670 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@128 671 LOGS("REAL TIME CLOCK DATA READ");
philpem@40 672 break;
philpem@40 673 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 674 switch (address & 0x077000) {
philpem@40 675 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@44 676 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@40 677 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 678 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 679 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 680 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 681 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@44 682 // All write-only registers... TODO: bus error?
philpem@44 683 handled = true;
philpem@40 684 break;
philpem@44 685 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video [FIXME: not in TRM]
philpem@40 686 break;
philpem@40 687 }
philpem@40 688 break;
philpem@40 689 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 690 break;
philpem@40 691 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 692 switch (address & 0x07F000) {
philpem@40 693 default:
philpem@40 694 break;
philpem@40 695 }
philpem@40 696 break;
philpem@40 697 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@84 698 // TODO: figure out which sizes are valid (probably just 8 and 16)
philpem@84 699 //ENFORCE_SIZE_R(bits, address, 16, "KEYBOARD CONTROLLER");
philpem@84 700 {
philpem@93 701 if (bits == 8) {
philpem@93 702 return keyboard_read(&state.kbd, (address >> 1) & 3);
philpem@93 703 } else {
philpem@93 704 return keyboard_read(&state.kbd, (address >> 1) & 3) << 8;
philpem@93 705 }
philpem@84 706 return data;
philpem@84 707 }
philpem@40 708 break;
philpem@40 709 }
philpem@40 710 }
philpem@40 711 }
philpem@40 712
philpem@64 713 LOG_NOT_HANDLED_R(bits);
philpem@64 714
philpem@59 715 return data;
philpem@59 716 }/*}}}*/
philpem@40 717
philpem@59 718
philpem@59 719 /********************************************************
philpem@59 720 * m68k memory read/write support functions for Musashi
philpem@59 721 ********************************************************/
philpem@59 722
philpem@59 723 /**
philpem@59 724 * @brief Read M68K memory, 32-bit
philpem@59 725 */
philpem@59 726 uint32_t m68k_read_memory_32(uint32_t address)/*{{{*/
philpem@59 727 {
philpem@119 728 uint32_t data = EMPTY & 0xFFFFFFFF;
philpem@59 729
philpem@59 730 // If ROMLMAP is set, force system to access ROM
philpem@59 731 if (!state.romlmap)
philpem@59 732 address |= 0x800000;
philpem@59 733
philpem@59 734 // Check access permissions
philpem@59 735 ACCESS_CHECK_RD(address, 32);
philpem@59 736
philpem@59 737 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@59 738 // ROM access
philpem@60 739 return RD32(state.rom, address, ROM_SIZE - 1);
philpem@60 740 } else if (address <= 0x3fffff) {
philpem@59 741 // RAM access
philpem@128 742 uint32_t newAddr = MAP_ADDR(address);
philpem@128 743
philpem@63 744 if (newAddr <= 0x1fffff) {
philpem@119 745 if (newAddr >= state.base_ram_size)
philpem@119 746 return EMPTY & 0xffffffff;
philpem@119 747 else
philpem@119 748 return RD32(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 749 } else {
philpem@119 750 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@63 751 return RD32(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 752 else
philpem@119 753 return EMPTY & 0xffffffff;
philpem@63 754 }
philpem@59 755 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@59 756 // I/O register space, zone A
philpem@59 757 switch (address & 0x0F0000) {
philpem@59 758 case 0x000000: // Map RAM access
philpem@59 759 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD32 from MapRAM mirror, addr=0x%08X\n", address);
philpem@60 760 return RD32(state.map, address, 0x7FF);
philpem@59 761 break;
philpem@59 762 case 0x020000: // Video RAM
philpem@59 763 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD32 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@60 764 return RD32(state.vram, address, 0x7FFF);
philpem@59 765 break;
philpem@59 766 default:
philpem@60 767 return IoRead(address, 32);
philpem@59 768 }
philpem@59 769 } else {
philpem@60 770 return IoRead(address, 32);
philpem@59 771 }
philpem@59 772
philpem@40 773 return data;
philpem@59 774 }/*}}}*/
philpem@40 775
philpem@40 776 /**
philpem@40 777 * @brief Read M68K memory, 16-bit
philpem@40 778 */
philpem@59 779 uint32_t m68k_read_memory_16(uint32_t address)/*{{{*/
philpem@40 780 {
philpem@119 781 uint16_t data = EMPTY & 0xFFFF;
philpem@40 782
philpem@40 783 // If ROMLMAP is set, force system to access ROM
philpem@40 784 if (!state.romlmap)
philpem@40 785 address |= 0x800000;
philpem@40 786
philpem@40 787 // Check access permissions
philpem@40 788 ACCESS_CHECK_RD(address, 16);
philpem@40 789
philpem@40 790 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 791 // ROM access
philpem@40 792 data = RD16(state.rom, address, ROM_SIZE - 1);
philpem@60 793 } else if (address <= 0x3fffff) {
philpem@40 794 // RAM access
philpem@128 795 uint32_t newAddr = MAP_ADDR(address);
philpem@128 796
philpem@63 797 if (newAddr <= 0x1fffff) {
philpem@119 798 if (newAddr >= state.base_ram_size)
philpem@119 799 return EMPTY & 0xffff;
philpem@119 800 else
philpem@119 801 return RD16(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 802 } else {
philpem@119 803 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@63 804 return RD16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 805 else
philpem@119 806 return EMPTY & 0xffff;
philpem@63 807 }
philpem@40 808 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 809 // I/O register space, zone A
philpem@40 810 switch (address & 0x0F0000) {
philpem@40 811 case 0x000000: // Map RAM access
philpem@40 812 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD16 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 813 data = RD16(state.map, address, 0x7FF);
philpem@40 814 break;
philpem@40 815 case 0x020000: // Video RAM
philpem@40 816 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD16 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 817 data = RD16(state.vram, address, 0x7FFF);
philpem@40 818 break;
philpem@59 819 default:
philpem@59 820 data = IoRead(address, 16);
philpem@40 821 }
philpem@59 822 } else {
philpem@59 823 data = IoRead(address, 16);
philpem@40 824 }
philpem@40 825
philpem@40 826 return data;
philpem@59 827 }/*}}}*/
philpem@40 828
philpem@40 829 /**
philpem@40 830 * @brief Read M68K memory, 8-bit
philpem@40 831 */
philpem@59 832 uint32_t m68k_read_memory_8(uint32_t address)/*{{{*/
philpem@40 833 {
philpem@119 834 uint8_t data = EMPTY & 0xFF;
philpem@40 835
philpem@40 836 // If ROMLMAP is set, force system to access ROM
philpem@40 837 if (!state.romlmap)
philpem@40 838 address |= 0x800000;
philpem@40 839
philpem@40 840 // Check access permissions
philpem@40 841 ACCESS_CHECK_RD(address, 8);
philpem@40 842
philpem@40 843 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 844 // ROM access
philpem@40 845 data = RD8(state.rom, address, ROM_SIZE - 1);
philpem@60 846 } else if (address <= 0x3fffff) {
philpem@40 847 // RAM access
philpem@128 848 uint32_t newAddr = MAP_ADDR(address);
philpem@128 849
philpem@63 850 if (newAddr <= 0x1fffff) {
philpem@119 851 if (newAddr >= state.base_ram_size)
philpem@119 852 return EMPTY & 0xff;
philpem@119 853 else
philpem@119 854 return RD8(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 855 } else {
philpem@119 856 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@63 857 return RD8(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 858 else
philpem@119 859 return EMPTY & 0xff;
philpem@63 860 }
philpem@40 861 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 862 // I/O register space, zone A
philpem@40 863 switch (address & 0x0F0000) {
philpem@40 864 case 0x000000: // Map RAM access
philpem@40 865 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD8 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 866 data = RD8(state.map, address, 0x7FF);
philpem@40 867 break;
philpem@40 868 case 0x020000: // Video RAM
philpem@40 869 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD8 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 870 data = RD8(state.vram, address, 0x7FFF);
philpem@40 871 break;
philpem@59 872 default:
philpem@59 873 data = IoRead(address, 8);
philpem@40 874 }
philpem@59 875 } else {
philpem@59 876 data = IoRead(address, 8);
philpem@40 877 }
philpem@40 878
philpem@40 879 return data;
philpem@59 880 }/*}}}*/
philpem@40 881
philpem@40 882 /**
philpem@40 883 * @brief Write M68K memory, 32-bit
philpem@40 884 */
philpem@59 885 void m68k_write_memory_32(uint32_t address, uint32_t value)/*{{{*/
philpem@40 886 {
philpem@40 887 // If ROMLMAP is set, force system to access ROM
philpem@40 888 if (!state.romlmap)
philpem@40 889 address |= 0x800000;
philpem@40 890
philpem@40 891 // Check access permissions
philpem@40 892 ACCESS_CHECK_WR(address, 32);
philpem@40 893
philpem@40 894 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 895 // ROM access
philpem@60 896 } else if (address <= 0x3FFFFF) {
philpem@40 897 // RAM access
philpem@128 898 uint32_t newAddr = MAP_ADDR(address);
philpem@128 899
philpem@119 900 if (newAddr <= 0x1fffff) {
philpem@119 901 if (newAddr < state.base_ram_size) {
philpem@119 902 WR32(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@119 903 }
philpem@119 904 } else {
philpem@119 905 if ((newAddr - 0x200000) < state.exp_ram_size) {
philpem@119 906 WR32(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@119 907 }
philpem@119 908 }
philpem@40 909 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 910 // I/O register space, zone A
philpem@40 911 switch (address & 0x0F0000) {
philpem@40 912 case 0x000000: // Map RAM access
philpem@105 913 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR32 to MapRAM mirror, addr=0x%08X\n", address);
philpem@40 914 WR32(state.map, address, 0x7FF, value);
philpem@40 915 break;
philpem@40 916 case 0x020000: // Video RAM
philpem@105 917 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR32 to VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 918 WR32(state.vram, address, 0x7FFF, value);
philpem@40 919 break;
philpem@59 920 default:
philpem@59 921 IoWrite(address, value, 32);
philpem@40 922 }
philpem@59 923 } else {
philpem@59 924 IoWrite(address, value, 32);
philpem@40 925 }
philpem@59 926 }/*}}}*/
philpem@40 927
philpem@40 928 /**
philpem@40 929 * @brief Write M68K memory, 16-bit
philpem@40 930 */
philpem@59 931 void m68k_write_memory_16(uint32_t address, uint32_t value)/*{{{*/
philpem@40 932 {
philpem@40 933 // If ROMLMAP is set, force system to access ROM
philpem@40 934 if (!state.romlmap)
philpem@40 935 address |= 0x800000;
philpem@40 936
philpem@40 937 // Check access permissions
philpem@40 938 ACCESS_CHECK_WR(address, 16);
philpem@40 939
philpem@40 940 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 941 // ROM access
philpem@60 942 } else if (address <= 0x3FFFFF) {
philpem@40 943 // RAM access
philpem@128 944 uint32_t newAddr = MAP_ADDR(address);
philpem@112 945
philpem@119 946 if (newAddr <= 0x1fffff) {
philpem@119 947 if (newAddr < state.base_ram_size) {
philpem@119 948 WR16(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@119 949 }
philpem@119 950 } else {
philpem@119 951 if ((newAddr - 0x200000) < state.exp_ram_size) {
philpem@119 952 WR16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@119 953 }
philpem@119 954 }
philpem@40 955 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 956 // I/O register space, zone A
philpem@40 957 switch (address & 0x0F0000) {
philpem@40 958 case 0x000000: // Map RAM access
philpem@40 959 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR16 to MapRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 960 WR16(state.map, address, 0x7FF, value);
philpem@40 961 break;
philpem@40 962 case 0x020000: // Video RAM
philpem@40 963 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR16 to VideoRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 964 WR16(state.vram, address, 0x7FFF, value);
philpem@40 965 break;
philpem@59 966 default:
philpem@59 967 IoWrite(address, value, 16);
philpem@40 968 }
philpem@59 969 } else {
philpem@59 970 IoWrite(address, value, 16);
philpem@40 971 }
philpem@59 972 }/*}}}*/
philpem@40 973
philpem@40 974 /**
philpem@40 975 * @brief Write M68K memory, 8-bit
philpem@40 976 */
philpem@59 977 void m68k_write_memory_8(uint32_t address, uint32_t value)/*{{{*/
philpem@40 978 {
philpem@40 979 // If ROMLMAP is set, force system to access ROM
philpem@40 980 if (!state.romlmap)
philpem@40 981 address |= 0x800000;
philpem@40 982
philpem@40 983 // Check access permissions
philpem@40 984 ACCESS_CHECK_WR(address, 8);
philpem@40 985
philpem@40 986 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 987 // ROM access (read only!)
philpem@60 988 } else if (address <= 0x3FFFFF) {
philpem@40 989 // RAM access
philpem@128 990 uint32_t newAddr = MAP_ADDR(address);
philpem@128 991
philpem@119 992 if (newAddr <= 0x1fffff) {
philpem@119 993 if (newAddr < state.base_ram_size) {
philpem@119 994 WR8(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@119 995 }
philpem@119 996 } else {
philpem@119 997 if ((newAddr - 0x200000) < state.exp_ram_size) {
philpem@119 998 WR8(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@119 999 }
philpem@119 1000 }
philpem@40 1001 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 1002 // I/O register space, zone A
philpem@40 1003 switch (address & 0x0F0000) {
philpem@40 1004 case 0x000000: // Map RAM access
philpem@59 1005 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR8 to MapRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 1006 WR8(state.map, address, 0x7FF, value);
philpem@40 1007 break;
philpem@40 1008 case 0x020000: // Video RAM
philpem@59 1009 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR8 to VideoRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 1010 WR8(state.vram, address, 0x7FFF, value);
philpem@40 1011 break;
philpem@59 1012 default:
philpem@59 1013 IoWrite(address, value, 8);
philpem@40 1014 }
philpem@59 1015 } else {
philpem@59 1016 IoWrite(address, value, 8);
philpem@40 1017 }
philpem@59 1018 }/*}}}*/
philpem@40 1019
philpem@40 1020
philpem@40 1021 // for the disassembler
philpem@121 1022 uint32_t m68k_read_disassembler_32(uint32_t addr)
philpem@121 1023 {
philpem@121 1024 if (addr < 0x400000) {
philpem@128 1025 // XXX FIXME BUGBUG update this to use the new mapper macros!
philpem@121 1026 uint16_t page = (addr >> 12) & 0x3FF;
philpem@121 1027 uint32_t new_page_addr = MAPRAM(page) & 0x3FF;
philpem@121 1028 uint32_t newAddr = (new_page_addr << 12) + (addr & 0xFFF);
philpem@121 1029 if (newAddr <= 0x1fffff) {
philpem@121 1030 if (newAddr >= state.base_ram_size)
philpem@121 1031 return EMPTY;
philpem@121 1032 else
philpem@121 1033 return RD32(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@121 1034 } else {
philpem@121 1035 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@121 1036 return RD32(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@121 1037 else
philpem@121 1038 return EMPTY;
philpem@121 1039 }
philpem@121 1040 } else {
philpem@128 1041 LOG("WARNING: Disassembler RD32 out of range 0x%08X\n", addr);
philpem@121 1042 return EMPTY;
philpem@121 1043 }
philpem@121 1044 }
philpem@40 1045
philpem@121 1046 uint32_t m68k_read_disassembler_16(uint32_t addr)
philpem@121 1047 {
philpem@121 1048 if (addr < 0x400000) {
philpem@121 1049 uint16_t page = (addr >> 12) & 0x3FF;
philpem@121 1050 uint32_t new_page_addr = MAPRAM(page) & 0x3FF;
philpem@121 1051 uint32_t newAddr = (new_page_addr << 12) + (addr & 0xFFF);
philpem@121 1052 if (newAddr <= 0x1fffff) {
philpem@121 1053 if (newAddr >= state.base_ram_size)
philpem@121 1054 return EMPTY & 0xffff;
philpem@121 1055 else
philpem@121 1056 return RD16(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@121 1057 } else {
philpem@121 1058 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@121 1059 return RD16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@121 1060 else
philpem@121 1061 return EMPTY & 0xffff;
philpem@121 1062 }
philpem@121 1063 } else {
philpem@128 1064 LOG("WARNING: Disassembler RD16 out of range 0x%08X\n", addr);
philpem@121 1065 return EMPTY & 0xffff;
philpem@121 1066 }
philpem@121 1067 }
philpem@121 1068
philpem@121 1069 uint32_t m68k_read_disassembler_8 (uint32_t addr)
philpem@121 1070 {
philpem@121 1071 if (addr < 0x400000) {
philpem@121 1072 uint16_t page = (addr >> 12) & 0x3FF;
philpem@121 1073 uint32_t new_page_addr = MAPRAM(page) & 0x3FF;
philpem@121 1074 uint32_t newAddr = (new_page_addr << 12) + (addr & 0xFFF);
philpem@121 1075 if (newAddr <= 0x1fffff) {
philpem@121 1076 if (newAddr >= state.base_ram_size)
philpem@121 1077 return EMPTY & 0xff;
philpem@121 1078 else
philpem@121 1079 return RD8(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@121 1080 } else {
philpem@121 1081 if ((newAddr <= (state.exp_ram_size + 0x200000 - 1)) && (newAddr >= 0x200000))
philpem@121 1082 return RD8(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@121 1083 else
philpem@121 1084 return EMPTY & 0xff;
philpem@121 1085 }
philpem@121 1086 } else {
philpem@128 1087 LOG("WARNING: Disassembler RD8 out of range 0x%08X\n", addr);
philpem@121 1088 return EMPTY & 0xff;
philpem@121 1089 }
philpem@121 1090 }
philpem@121 1091