src/memory.c

Thu, 03 Mar 2011 13:05:21 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 03 Mar 2011 13:05:21 +0000
changeset 100
d6f699f89303
parent 97
240e195e4bed
child 102
4e1c29899aca
permissions
-rw-r--r--

Fix broken pagebit update code (was failing S4TEST 19 Map Translation test)

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@40 11 /******************
philpem@40 12 * Memory mapping
philpem@40 13 ******************/
philpem@40 14
philpem@40 15 #define MAPRAM(addr) (((uint16_t)state.map[addr*2] << 8) + ((uint16_t)state.map[(addr*2)+1]))
philpem@40 16
philpem@59 17 uint32_t mapAddr(uint32_t addr, bool writing)/*{{{*/
philpem@40 18 {
philpem@40 19 if (addr < 0x400000) {
philpem@40 20 // RAM access. Check against the Map RAM
philpem@40 21 // Start by getting the original page address
philpem@40 22 uint16_t page = (addr >> 12) & 0x3FF;
philpem@40 23
philpem@40 24 // Look it up in the map RAM and get the physical page address
philpem@40 25 uint32_t new_page_addr = MAPRAM(page) & 0x3FF;
philpem@40 26
philpem@40 27 // Update the Page Status bits
philpem@40 28 uint8_t pagebits = (MAPRAM(page) >> 13) & 0x03;
philpem@100 29 // Pagebits --
philpem@100 30 // 0 = not present
philpem@100 31 // 1 = present but not accessed
philpem@100 32 // 2 = present, accessed (read from)
philpem@100 33 // 3 = present, dirty (written to)
philpem@100 34 switch (pagebits) {
philpem@100 35 case 0:
philpem@100 36 // Page not present
philpem@100 37 // This should cause a page fault
philpem@100 38 LOGS("Whoa! Pagebit update, when the page is not present!");
philpem@100 39 break;
philpem@100 40
philpem@100 41 case 1:
philpem@100 42 // Page present -- first access
philpem@100 43 state.map[page*2] &= 0x1F; // turn off "present" bit
philpem@100 44 if (writing)
philpem@100 45 state.map[page*2] |= 0x60; // Page written to (dirty)
philpem@100 46 else
philpem@100 47 state.map[page*2] |= 0x40; // Page accessed but not written
philpem@100 48 break;
philpem@100 49
philpem@100 50 case 2:
philpem@100 51 case 3:
philpem@100 52 // Page present, 2nd or later access
philpem@100 53 if (writing)
philpem@100 54 state.map[page*2] |= 0x60; // Page written to (dirty)
philpem@100 55 break;
philpem@40 56 }
philpem@40 57
philpem@40 58 // Return the address with the new physical page spliced in
philpem@40 59 return (new_page_addr << 12) + (addr & 0xFFF);
philpem@40 60 } else {
philpem@40 61 // I/O, VRAM or MapRAM space; no mapping is performed or required
philpem@40 62 // TODO: assert here?
philpem@40 63 return addr;
philpem@40 64 }
philpem@59 65 }/*}}}*/
philpem@40 66
philpem@59 67 MEM_STATUS checkMemoryAccess(uint32_t addr, bool writing)/*{{{*/
philpem@40 68 {
philpem@40 69 // Are we in Supervisor mode?
philpem@40 70 if (m68k_get_reg(NULL, M68K_REG_SR) & 0x2000)
philpem@40 71 // Yes. We can do anything we like.
philpem@40 72 return MEM_ALLOWED;
philpem@40 73
philpem@40 74 // If we're here, then we must be in User mode.
philpem@40 75 // Check that the user didn't access memory outside of the RAM area
philpem@40 76 if (addr >= 0x400000)
philpem@40 77 return MEM_UIE;
philpem@40 78
philpem@40 79 // This leaves us with Page Fault checking. Get the page bits for this page.
philpem@40 80 uint16_t page = (addr >> 12) & 0x3FF;
philpem@40 81 uint8_t pagebits = (MAPRAM(page) >> 13) & 0x07;
philpem@40 82
philpem@40 83 // Check page is present
philpem@40 84 if ((pagebits & 0x03) == 0)
philpem@40 85 return MEM_PAGEFAULT;
philpem@40 86
philpem@40 87 // User attempt to access the kernel
philpem@40 88 // A19, A20, A21, A22 low (kernel access): RAM addr before paging; not in Supervisor mode
philpem@40 89 if (((addr >> 19) & 0x0F) == 0)
philpem@40 90 return MEM_KERNEL;
philpem@40 91
philpem@40 92 // Check page is write enabled
philpem@68 93 if (writing && ((pagebits & 0x04) == 0))
philpem@40 94 return MEM_PAGE_NO_WE;
philpem@40 95
philpem@40 96 // Page access allowed.
philpem@40 97 return MEM_ALLOWED;
philpem@59 98 }/*}}}*/
philpem@40 99
philpem@40 100 #undef MAPRAM
philpem@40 101
philpem@40 102
philpem@40 103 /********************************************************
philpem@40 104 * m68k memory read/write support functions for Musashi
philpem@40 105 ********************************************************/
philpem@40 106
philpem@40 107 /**
philpem@40 108 * @brief Check memory access permissions for a write operation.
philpem@40 109 * @note This used to be a single macro (merged with ACCESS_CHECK_RD), but
philpem@40 110 * gcc throws warnings when you have a return-with-value in a void
philpem@40 111 * function, even if the return-with-value is completely unreachable.
philpem@40 112 * Similarly it doesn't like it if you have a return without a value
philpem@40 113 * in a non-void function, even if it's impossible to ever reach the
philpem@40 114 * return-with-no-value. UGH!
philpem@40 115 */
philpem@59 116 /*{{{ macro: ACCESS_CHECK_WR(address, bits)*/
philpem@59 117 #define ACCESS_CHECK_WR(address, bits) \
philpem@59 118 do { \
philpem@40 119 bool fault = false; \
philpem@40 120 /* MEM_STATUS st; */ \
philpem@40 121 switch (checkMemoryAccess(address, true)) { \
philpem@40 122 case MEM_ALLOWED: \
philpem@40 123 /* Access allowed */ \
philpem@40 124 break; \
philpem@40 125 case MEM_PAGEFAULT: \
philpem@40 126 /* Page fault */ \
philpem@44 127 state.genstat = 0x8BFF | (state.pie ? 0x0400 : 0); \
philpem@40 128 fault = true; \
philpem@40 129 break; \
philpem@40 130 case MEM_UIE: \
philpem@40 131 /* User access to memory above 4MB */ \
philpem@44 132 state.genstat = 0x9AFF | (state.pie ? 0x0400 : 0); \
philpem@40 133 fault = true; \
philpem@40 134 break; \
philpem@40 135 case MEM_KERNEL: \
philpem@40 136 case MEM_PAGE_NO_WE: \
philpem@40 137 /* kernel access or page not write enabled */ \
philpem@68 138 /* FIXME: which regs need setting? */ \
philpem@40 139 fault = true; \
philpem@40 140 break; \
philpem@40 141 } \
philpem@40 142 \
philpem@40 143 if (fault) { \
philpem@40 144 if (bits >= 16) \
philpem@68 145 state.bsr0 = 0x7C00; \
philpem@40 146 else \
philpem@40 147 state.bsr0 = (address & 1) ? 0x7D00 : 0x7E00; \
philpem@40 148 state.bsr0 |= (address >> 16); \
philpem@40 149 state.bsr1 = address & 0xffff; \
philpem@40 150 printf("ERR: BusError WR\n"); \
philpem@40 151 m68k_pulse_bus_error(); \
philpem@40 152 return; \
philpem@40 153 } \
philpem@70 154 } while (0)
philpem@59 155 /*}}}*/
philpem@40 156
philpem@40 157 /**
philpem@40 158 * @brief Check memory access permissions for a read operation.
philpem@40 159 * @note This used to be a single macro (merged with ACCESS_CHECK_WR), but
philpem@40 160 * gcc throws warnings when you have a return-with-value in a void
philpem@40 161 * function, even if the return-with-value is completely unreachable.
philpem@40 162 * Similarly it doesn't like it if you have a return without a value
philpem@40 163 * in a non-void function, even if it's impossible to ever reach the
philpem@40 164 * return-with-no-value. UGH!
philpem@40 165 */
philpem@59 166 /*{{{ macro: ACCESS_CHECK_RD(address, bits)*/
philpem@59 167 #define ACCESS_CHECK_RD(address, bits) \
philpem@59 168 do { \
philpem@40 169 bool fault = false; \
philpem@40 170 /* MEM_STATUS st; */ \
philpem@40 171 switch (checkMemoryAccess(address, false)) { \
philpem@40 172 case MEM_ALLOWED: \
philpem@40 173 /* Access allowed */ \
philpem@40 174 break; \
philpem@40 175 case MEM_PAGEFAULT: \
philpem@40 176 /* Page fault */ \
philpem@44 177 state.genstat = 0xCBFF | (state.pie ? 0x0400 : 0); \
philpem@40 178 fault = true; \
philpem@40 179 break; \
philpem@40 180 case MEM_UIE: \
philpem@40 181 /* User access to memory above 4MB */ \
philpem@44 182 state.genstat = 0xDAFF | (state.pie ? 0x0400 : 0); \
philpem@40 183 fault = true; \
philpem@40 184 break; \
philpem@40 185 case MEM_KERNEL: \
philpem@40 186 case MEM_PAGE_NO_WE: \
philpem@40 187 /* kernel access or page not write enabled */ \
philpem@68 188 /* FIXME: which regs need setting? */ \
philpem@40 189 fault = true; \
philpem@40 190 break; \
philpem@40 191 } \
philpem@40 192 \
philpem@40 193 if (fault) { \
philpem@40 194 if (bits >= 16) \
philpem@68 195 state.bsr0 = 0x7C00; \
philpem@40 196 else \
philpem@40 197 state.bsr0 = (address & 1) ? 0x7D00 : 0x7E00; \
philpem@40 198 state.bsr0 |= (address >> 16); \
philpem@40 199 state.bsr1 = address & 0xffff; \
philpem@40 200 printf("ERR: BusError RD\n"); \
philpem@40 201 m68k_pulse_bus_error(); \
philpem@40 202 return 0xFFFFFFFF; \
philpem@40 203 } \
philpem@70 204 } while (0)
philpem@59 205 /*}}}*/
philpem@40 206
philpem@40 207 // Logging macros
philpem@59 208 #define LOG_NOT_HANDLED_R(bits) \
philpem@64 209 if (!handled) printf("unhandled read%02d, addr=0x%08X\n", bits, address);
philpem@40 210
philpem@59 211 #define LOG_NOT_HANDLED_W(bits) \
philpem@64 212 if (!handled) printf("unhandled write%02d, addr=0x%08X, data=0x%08X\n", bits, address, data);
philpem@59 213
philpem@59 214 /********************************************************
philpem@59 215 * I/O read/write functions
philpem@59 216 ********************************************************/
philpem@40 217
philpem@40 218 /**
philpem@59 219 * Issue a warning if a read operation is made with an invalid size
philpem@40 220 */
philpem@66 221 inline static void ENFORCE_SIZE(int bits, uint32_t address, bool read, int allowed, char *regname)
philpem@40 222 {
philpem@59 223 assert((bits == 8) || (bits == 16) || (bits == 32));
philpem@59 224 if ((bits & allowed) == 0) {
philpem@66 225 printf("WARNING: %s 0x%08X (%s) with invalid size %d!\n", read ? "read from" : "write to", address, regname, bits);
philpem@59 226 }
philpem@59 227 }
philpem@59 228
philpem@66 229 inline static void ENFORCE_SIZE_R(int bits, uint32_t address, int allowed, char *regname)
philpem@40 230 {
philpem@66 231 ENFORCE_SIZE(bits, address, true, allowed, regname);
philpem@66 232 }
philpem@66 233
philpem@66 234 inline static void ENFORCE_SIZE_W(int bits, uint32_t address, int allowed, char *regname)
philpem@66 235 {
philpem@66 236 ENFORCE_SIZE(bits, address, false, allowed, regname);
philpem@66 237 }
philpem@66 238
philpem@59 239 void IoWrite(uint32_t address, uint32_t data, int bits)/*{{{*/
philpem@59 240 {
philpem@40 241 bool handled = false;
philpem@40 242
philpem@59 243 if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 244 // I/O register space, zone A
philpem@40 245 switch (address & 0x0F0000) {
philpem@40 246 case 0x010000: // General Status Register
philpem@59 247 if (bits == 16)
philpem@59 248 state.genstat = (data & 0xffff);
philpem@59 249 else if (bits == 8) {
philpem@59 250 if (address & 0)
philpem@59 251 state.genstat = data;
philpem@59 252 else
philpem@59 253 state.genstat = data << 8;
philpem@59 254 }
philpem@40 255 handled = true;
philpem@40 256 break;
philpem@40 257 case 0x030000: // Bus Status Register 0
philpem@40 258 break;
philpem@40 259 case 0x040000: // Bus Status Register 1
philpem@40 260 break;
philpem@40 261 case 0x050000: // Phone status
philpem@40 262 break;
philpem@40 263 case 0x060000: // DMA Count
philpem@66 264 ENFORCE_SIZE_W(bits, address, 16, "DMACOUNT");
philpem@59 265 state.dma_count = (data & 0x3FFF);
philpem@59 266 state.idmarw = ((data & 0x4000) == 0x4000);
philpem@59 267 state.dmaen = ((data & 0x8000) == 0x8000);
philpem@59 268 // This handles the "dummy DMA transfer" mentioned in the docs
philpem@59 269 // TODO: access check, peripheral access
philpem@59 270 if (!state.idmarw)
philpem@60 271 WR32(state.base_ram, mapAddr(address, true), state.base_ram_size - 1, 0xDEAD);
philpem@59 272 state.dma_count++;
philpem@53 273 handled = true;
philpem@40 274 break;
philpem@40 275 case 0x070000: // Line Printer Status Register
philpem@40 276 break;
philpem@40 277 case 0x080000: // Real Time Clock
philpem@40 278 break;
philpem@40 279 case 0x090000: // Phone registers
philpem@40 280 switch (address & 0x0FF000) {
philpem@40 281 case 0x090000: // Handset relay
philpem@40 282 case 0x098000:
philpem@40 283 break;
philpem@40 284 case 0x091000: // Line select 2
philpem@40 285 case 0x099000:
philpem@40 286 break;
philpem@40 287 case 0x092000: // Hook relay 1
philpem@40 288 case 0x09A000:
philpem@40 289 break;
philpem@40 290 case 0x093000: // Hook relay 2
philpem@40 291 case 0x09B000:
philpem@40 292 break;
philpem@40 293 case 0x094000: // Line 1 hold
philpem@40 294 case 0x09C000:
philpem@40 295 break;
philpem@40 296 case 0x095000: // Line 2 hold
philpem@40 297 case 0x09D000:
philpem@40 298 break;
philpem@40 299 case 0x096000: // Line 1 A-lead
philpem@40 300 case 0x09E000:
philpem@40 301 break;
philpem@40 302 case 0x097000: // Line 2 A-lead
philpem@40 303 case 0x09F000:
philpem@40 304 break;
philpem@40 305 }
philpem@40 306 break;
philpem@59 307 case 0x0A0000: // Miscellaneous Control Register
philpem@66 308 ENFORCE_SIZE_W(bits, address, 16, "MISCCON");
philpem@59 309 // TODO: handle the ctrl bits properly
philpem@59 310 // TODO: &0x8000 --> dismiss 60hz intr
philpem@97 311 if (data & 0x8000){
philpem@97 312 state.timer_enabled = 1;
philpem@97 313 }else{
philpem@97 314 state.timer_enabled = 0;
philpem@97 315 state.timer_asserted = 0;
philpem@97 316 }
philpem@59 317 state.dma_reading = (data & 0x4000);
philpem@72 318 if (state.leds != ((~data & 0xF00) >> 8)) {
philpem@72 319 state.leds = (~data & 0xF00) >> 8;
philpem@72 320 printf("LEDs: %s %s %s %s\n",
philpem@72 321 (state.leds & 8) ? "R" : "-",
philpem@72 322 (state.leds & 4) ? "G" : "-",
philpem@72 323 (state.leds & 2) ? "Y" : "-",
philpem@72 324 (state.leds & 1) ? "R" : "-");
philpem@72 325 }
philpem@46 326 handled = true;
philpem@40 327 break;
philpem@40 328 case 0x0B0000: // TM/DIALWR
philpem@40 329 break;
philpem@59 330 case 0x0C0000: // Clear Status Register
philpem@59 331 state.genstat = 0xFFFF;
philpem@59 332 state.bsr0 = 0xFFFF;
philpem@59 333 state.bsr1 = 0xFFFF;
philpem@43 334 handled = true;
philpem@40 335 break;
philpem@40 336 case 0x0D0000: // DMA Address Register
philpem@59 337 if (address & 0x004000) {
philpem@59 338 // A14 high -- set most significant bits
philpem@59 339 state.dma_address = (state.dma_address & 0x1fe) | ((address & 0x3ffe) << 8);
philpem@59 340 } else {
philpem@59 341 // A14 low -- set least significant bits
philpem@59 342 state.dma_address = (state.dma_address & 0x3ffe00) | (address & 0x1fe);
philpem@59 343 }
philpem@59 344 handled = true;
philpem@40 345 break;
philpem@40 346 case 0x0E0000: // Disk Control Register
philpem@66 347 ENFORCE_SIZE_W(bits, address, 16, "DISKCON");
philpem@59 348 // B7 = FDD controller reset
philpem@59 349 if ((data & 0x80) == 0) wd2797_reset(&state.fdc_ctx);
philpem@59 350 // B6 = drive 0 select -- TODO
philpem@59 351 // B5 = motor enable -- TODO
philpem@59 352 // B4 = HDD controller reset -- TODO
philpem@59 353 // B3 = HDD0 select -- TODO
philpem@59 354 // B2,1,0 = HDD0 head select
philpem@59 355 handled = true;
philpem@40 356 break;
philpem@40 357 case 0x0F0000: // Line Printer Data Register
philpem@40 358 break;
philpem@40 359 }
philpem@40 360 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 361 // I/O register space, zone B
philpem@40 362 switch (address & 0xF00000) {
philpem@40 363 case 0xC00000: // Expansion slots
philpem@40 364 case 0xD00000:
philpem@40 365 switch (address & 0xFC0000) {
philpem@40 366 case 0xC00000: // Expansion slot 0
philpem@40 367 case 0xC40000: // Expansion slot 1
philpem@40 368 case 0xC80000: // Expansion slot 2
philpem@40 369 case 0xCC0000: // Expansion slot 3
philpem@40 370 case 0xD00000: // Expansion slot 4
philpem@40 371 case 0xD40000: // Expansion slot 5
philpem@40 372 case 0xD80000: // Expansion slot 6
philpem@40 373 case 0xDC0000: // Expansion slot 7
philpem@59 374 fprintf(stderr, "NOTE: WR%d to expansion card space, addr=0x%08X, data=0x%08X\n", bits, address, data);
philpem@59 375 handled = true;
philpem@40 376 break;
philpem@40 377 }
philpem@40 378 break;
philpem@40 379 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 380 case 0xF00000:
philpem@40 381 switch (address & 0x070000) {
philpem@40 382 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 383 break;
philpem@40 384 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@66 385 ENFORCE_SIZE_W(bits, address, 16, "FDC REGISTERS");
philpem@59 386 wd2797_write_reg(&state.fdc_ctx, (address >> 1) & 3, data);
philpem@52 387 handled = true;
philpem@40 388 break;
philpem@40 389 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 390 break;
philpem@40 391 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 392 break;
philpem@40 393 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 394 switch (address & 0x077000) {
philpem@40 395 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@59 396 break;
philpem@44 397 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@66 398 ENFORCE_SIZE_W(bits, address, 16, "PIE");
philpem@59 399 state.pie = ((data & 0x8000) == 0x8000);
philpem@59 400 handled = true;
philpem@59 401 break;
philpem@40 402 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@59 403 break;
philpem@40 404 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@66 405 ENFORCE_SIZE_W(bits, address, 16, "ROMLMAP");
philpem@59 406 state.romlmap = ((data & 0x8000) == 0x8000);
philpem@44 407 handled = true;
philpem@40 408 break;
philpem@59 409 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@66 410 ENFORCE_SIZE_W(bits, address, 16, "L1 MODEM");
philpem@59 411 break;
philpem@59 412 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@66 413 ENFORCE_SIZE_W(bits, address, 16, "L2 MODEM");
philpem@59 414 break;
philpem@59 415 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@66 416 ENFORCE_SIZE_W(bits, address, 16, "D/N CONNECT");
philpem@59 417 break;
philpem@59 418 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@66 419 ENFORCE_SIZE_W(bits, address, 16, "WHOLE SCREEN REVERSE VIDEO");
philpem@40 420 break;
philpem@40 421 }
philpem@40 422 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 423 break;
philpem@40 424 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 425 switch (address & 0x07F000) {
philpem@40 426 default:
philpem@40 427 break;
philpem@40 428 }
philpem@40 429 break;
philpem@40 430 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@84 431 // TODO: figure out which sizes are valid (probably just 8 and 16)
philpem@84 432 // ENFORCE_SIZE_W(bits, address, 16, "KEYBOARD CONTROLLER");
philpem@93 433 if (bits == 8) {
philpem@93 434 printf("KBD WR %02X => %02X\n", (address >> 1) & 3, data);
philpem@93 435 keyboard_write(&state.kbd, (address >> 1) & 3, data);
philpem@93 436 handled = true;
philpem@93 437 } else if (bits == 16) {
philpem@93 438 printf("KBD WR %02X => %04X\n", (address >> 1) & 3, data);
philpem@93 439 keyboard_write(&state.kbd, (address >> 1) & 3, data >> 8);
philpem@93 440 handled = true;
philpem@93 441 }
philpem@40 442 break;
philpem@40 443 }
philpem@40 444 }
philpem@40 445 }
philpem@40 446
philpem@64 447 LOG_NOT_HANDLED_W(bits);
philpem@59 448 }/*}}}*/
philpem@40 449
philpem@59 450 uint32_t IoRead(uint32_t address, int bits)/*{{{*/
philpem@59 451 {
philpem@59 452 bool handled = false;
philpem@59 453 uint32_t data = 0xFFFFFFFF;
philpem@40 454
philpem@59 455 if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 456 // I/O register space, zone A
philpem@40 457 switch (address & 0x0F0000) {
philpem@40 458 case 0x010000: // General Status Register
philpem@66 459 ENFORCE_SIZE_R(bits, address, 16, "GENSTAT");
philpem@59 460 return ((uint32_t)state.genstat << 16) + (uint32_t)state.genstat;
philpem@40 461 break;
philpem@40 462 case 0x030000: // Bus Status Register 0
philpem@66 463 ENFORCE_SIZE_R(bits, address, 16, "BSR0");
philpem@59 464 return ((uint32_t)state.bsr0 << 16) + (uint32_t)state.bsr0;
philpem@40 465 break;
philpem@40 466 case 0x040000: // Bus Status Register 1
philpem@66 467 ENFORCE_SIZE_R(bits, address, 16, "BSR1");
philpem@59 468 return ((uint32_t)state.bsr1 << 16) + (uint32_t)state.bsr1;
philpem@40 469 break;
philpem@40 470 case 0x050000: // Phone status
philpem@66 471 ENFORCE_SIZE_R(bits, address, 8 | 16, "PHONE STATUS");
philpem@40 472 break;
philpem@40 473 case 0x060000: // DMA Count
philpem@55 474 // TODO: U/OERR- is always inactive (bit set)... or should it be = DMAEN+?
philpem@55 475 // Bit 14 is always unused, so leave it set
philpem@66 476 ENFORCE_SIZE_R(bits, address, 16, "DMACOUNT");
philpem@59 477 return (state.dma_count & 0x3fff) | 0xC000;
philpem@40 478 break;
philpem@40 479 case 0x070000: // Line Printer Status Register
philpem@53 480 data = 0x00120012; // no parity error, no line printer error, no irqs from FDD or HDD
philpem@78 481 data |= wd2797_get_irq(&state.fdc_ctx) ? 0x00080008 : 0;
philpem@59 482 return data;
philpem@40 483 break;
philpem@40 484 case 0x080000: // Real Time Clock
philpem@59 485 printf("READ NOTIMP: Realtime Clock\n");
philpem@40 486 break;
philpem@40 487 case 0x090000: // Phone registers
philpem@40 488 switch (address & 0x0FF000) {
philpem@40 489 case 0x090000: // Handset relay
philpem@40 490 case 0x098000:
philpem@40 491 break;
philpem@40 492 case 0x091000: // Line select 2
philpem@40 493 case 0x099000:
philpem@40 494 break;
philpem@40 495 case 0x092000: // Hook relay 1
philpem@40 496 case 0x09A000:
philpem@40 497 break;
philpem@40 498 case 0x093000: // Hook relay 2
philpem@40 499 case 0x09B000:
philpem@40 500 break;
philpem@40 501 case 0x094000: // Line 1 hold
philpem@40 502 case 0x09C000:
philpem@40 503 break;
philpem@40 504 case 0x095000: // Line 2 hold
philpem@40 505 case 0x09D000:
philpem@40 506 break;
philpem@40 507 case 0x096000: // Line 1 A-lead
philpem@40 508 case 0x09E000:
philpem@40 509 break;
philpem@40 510 case 0x097000: // Line 2 A-lead
philpem@40 511 case 0x09F000:
philpem@40 512 break;
philpem@40 513 }
philpem@40 514 break;
philpem@46 515 case 0x0A0000: // Miscellaneous Control Register -- write only!
philpem@46 516 handled = true;
philpem@40 517 break;
philpem@40 518 case 0x0B0000: // TM/DIALWR
philpem@40 519 break;
philpem@46 520 case 0x0C0000: // Clear Status Register -- write only!
philpem@43 521 handled = true;
philpem@40 522 break;
philpem@40 523 case 0x0D0000: // DMA Address Register
philpem@40 524 break;
philpem@40 525 case 0x0E0000: // Disk Control Register
philpem@40 526 break;
philpem@40 527 case 0x0F0000: // Line Printer Data Register
philpem@40 528 break;
philpem@40 529 }
philpem@40 530 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 531 // I/O register space, zone B
philpem@40 532 switch (address & 0xF00000) {
philpem@40 533 case 0xC00000: // Expansion slots
philpem@40 534 case 0xD00000:
philpem@40 535 switch (address & 0xFC0000) {
philpem@40 536 case 0xC00000: // Expansion slot 0
philpem@40 537 case 0xC40000: // Expansion slot 1
philpem@40 538 case 0xC80000: // Expansion slot 2
philpem@40 539 case 0xCC0000: // Expansion slot 3
philpem@40 540 case 0xD00000: // Expansion slot 4
philpem@40 541 case 0xD40000: // Expansion slot 5
philpem@40 542 case 0xD80000: // Expansion slot 6
philpem@40 543 case 0xDC0000: // Expansion slot 7
philpem@65 544 fprintf(stderr, "NOTE: RD%d from expansion card space, addr=0x%08X\n", bits, address);
philpem@65 545 handled = true;
philpem@40 546 break;
philpem@40 547 }
philpem@40 548 break;
philpem@40 549 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 550 case 0xF00000:
philpem@40 551 switch (address & 0x070000) {
philpem@40 552 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 553 break;
philpem@40 554 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@66 555 ENFORCE_SIZE_R(bits, address, 16, "FDC REGISTERS");
philpem@59 556 return wd2797_read_reg(&state.fdc_ctx, (address >> 1) & 3);
philpem@40 557 break;
philpem@40 558 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 559 break;
philpem@40 560 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 561 break;
philpem@40 562 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 563 switch (address & 0x077000) {
philpem@40 564 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@44 565 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@40 566 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 567 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 568 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 569 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 570 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@44 571 // All write-only registers... TODO: bus error?
philpem@44 572 handled = true;
philpem@40 573 break;
philpem@44 574 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video [FIXME: not in TRM]
philpem@40 575 break;
philpem@40 576 }
philpem@40 577 break;
philpem@40 578 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 579 break;
philpem@40 580 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 581 switch (address & 0x07F000) {
philpem@40 582 default:
philpem@40 583 break;
philpem@40 584 }
philpem@40 585 break;
philpem@40 586 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@84 587 // TODO: figure out which sizes are valid (probably just 8 and 16)
philpem@84 588 //ENFORCE_SIZE_R(bits, address, 16, "KEYBOARD CONTROLLER");
philpem@84 589 {
philpem@93 590 if (bits == 8) {
philpem@93 591 return keyboard_read(&state.kbd, (address >> 1) & 3);
philpem@93 592 } else {
philpem@93 593 return keyboard_read(&state.kbd, (address >> 1) & 3) << 8;
philpem@93 594 }
philpem@84 595 return data;
philpem@84 596 }
philpem@40 597 break;
philpem@40 598 }
philpem@40 599 }
philpem@40 600 }
philpem@40 601
philpem@64 602 LOG_NOT_HANDLED_R(bits);
philpem@64 603
philpem@59 604 return data;
philpem@59 605 }/*}}}*/
philpem@40 606
philpem@59 607
philpem@59 608 /********************************************************
philpem@59 609 * m68k memory read/write support functions for Musashi
philpem@59 610 ********************************************************/
philpem@59 611
philpem@59 612 /**
philpem@59 613 * @brief Read M68K memory, 32-bit
philpem@59 614 */
philpem@59 615 uint32_t m68k_read_memory_32(uint32_t address)/*{{{*/
philpem@59 616 {
philpem@59 617 uint32_t data = 0xFFFFFFFF;
philpem@59 618
philpem@59 619 // If ROMLMAP is set, force system to access ROM
philpem@59 620 if (!state.romlmap)
philpem@59 621 address |= 0x800000;
philpem@59 622
philpem@59 623 // Check access permissions
philpem@59 624 ACCESS_CHECK_RD(address, 32);
philpem@59 625
philpem@59 626 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@59 627 // ROM access
philpem@60 628 return RD32(state.rom, address, ROM_SIZE - 1);
philpem@60 629 } else if (address <= 0x3fffff) {
philpem@59 630 // RAM access
philpem@60 631 uint32_t newAddr = mapAddr(address, false);
philpem@63 632 if (newAddr <= 0x1fffff) {
philpem@60 633 return RD32(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 634 } else {
philpem@63 635 if (newAddr <= (state.exp_ram_size + 0x200000 - 1))
philpem@63 636 return RD32(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 637 else
philpem@63 638 return 0xffffffff;
philpem@63 639 }
philpem@59 640 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@59 641 // I/O register space, zone A
philpem@59 642 switch (address & 0x0F0000) {
philpem@59 643 case 0x000000: // Map RAM access
philpem@59 644 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD32 from MapRAM mirror, addr=0x%08X\n", address);
philpem@60 645 return RD32(state.map, address, 0x7FF);
philpem@59 646 break;
philpem@59 647 case 0x020000: // Video RAM
philpem@59 648 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD32 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@60 649 return RD32(state.vram, address, 0x7FFF);
philpem@59 650 break;
philpem@59 651 default:
philpem@60 652 return IoRead(address, 32);
philpem@59 653 }
philpem@59 654 } else {
philpem@60 655 return IoRead(address, 32);
philpem@59 656 }
philpem@59 657
philpem@40 658 return data;
philpem@59 659 }/*}}}*/
philpem@40 660
philpem@40 661 /**
philpem@40 662 * @brief Read M68K memory, 16-bit
philpem@40 663 */
philpem@59 664 uint32_t m68k_read_memory_16(uint32_t address)/*{{{*/
philpem@40 665 {
philpem@40 666 uint16_t data = 0xFFFF;
philpem@40 667
philpem@40 668 // If ROMLMAP is set, force system to access ROM
philpem@40 669 if (!state.romlmap)
philpem@40 670 address |= 0x800000;
philpem@40 671
philpem@40 672 // Check access permissions
philpem@40 673 ACCESS_CHECK_RD(address, 16);
philpem@40 674
philpem@40 675 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 676 // ROM access
philpem@40 677 data = RD16(state.rom, address, ROM_SIZE - 1);
philpem@60 678 } else if (address <= 0x3fffff) {
philpem@40 679 // RAM access
philpem@60 680 uint32_t newAddr = mapAddr(address, false);
philpem@63 681 if (newAddr <= 0x1fffff) {
philpem@60 682 return RD16(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 683 } else {
philpem@63 684 if (newAddr <= (state.exp_ram_size + 0x200000 - 1))
philpem@63 685 return RD16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 686 else
philpem@63 687 return 0xffff;
philpem@63 688 }
philpem@40 689 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 690 // I/O register space, zone A
philpem@40 691 switch (address & 0x0F0000) {
philpem@40 692 case 0x000000: // Map RAM access
philpem@40 693 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD16 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 694 data = RD16(state.map, address, 0x7FF);
philpem@40 695 break;
philpem@40 696 case 0x020000: // Video RAM
philpem@40 697 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD16 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 698 data = RD16(state.vram, address, 0x7FFF);
philpem@40 699 break;
philpem@59 700 default:
philpem@59 701 data = IoRead(address, 16);
philpem@40 702 }
philpem@59 703 } else {
philpem@59 704 data = IoRead(address, 16);
philpem@40 705 }
philpem@40 706
philpem@40 707 return data;
philpem@59 708 }/*}}}*/
philpem@40 709
philpem@40 710 /**
philpem@40 711 * @brief Read M68K memory, 8-bit
philpem@40 712 */
philpem@59 713 uint32_t m68k_read_memory_8(uint32_t address)/*{{{*/
philpem@40 714 {
philpem@40 715 uint8_t data = 0xFF;
philpem@40 716
philpem@40 717 // If ROMLMAP is set, force system to access ROM
philpem@40 718 if (!state.romlmap)
philpem@40 719 address |= 0x800000;
philpem@40 720
philpem@40 721 // Check access permissions
philpem@40 722 ACCESS_CHECK_RD(address, 8);
philpem@40 723
philpem@40 724 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 725 // ROM access
philpem@40 726 data = RD8(state.rom, address, ROM_SIZE - 1);
philpem@60 727 } else if (address <= 0x3fffff) {
philpem@40 728 // RAM access
philpem@60 729 uint32_t newAddr = mapAddr(address, false);
philpem@63 730 if (newAddr <= 0x1fffff) {
philpem@60 731 return RD8(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@63 732 } else {
philpem@63 733 if (newAddr <= (state.exp_ram_size + 0x200000 - 1))
philpem@63 734 return RD8(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@63 735 else
philpem@63 736 return 0xff;
philpem@63 737 }
philpem@40 738 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 739 // I/O register space, zone A
philpem@40 740 switch (address & 0x0F0000) {
philpem@40 741 case 0x000000: // Map RAM access
philpem@40 742 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD8 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 743 data = RD8(state.map, address, 0x7FF);
philpem@40 744 break;
philpem@40 745 case 0x020000: // Video RAM
philpem@40 746 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD8 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 747 data = RD8(state.vram, address, 0x7FFF);
philpem@40 748 break;
philpem@59 749 default:
philpem@59 750 data = IoRead(address, 8);
philpem@40 751 }
philpem@59 752 } else {
philpem@59 753 data = IoRead(address, 8);
philpem@40 754 }
philpem@40 755
philpem@40 756 return data;
philpem@59 757 }/*}}}*/
philpem@40 758
philpem@40 759 /**
philpem@40 760 * @brief Write M68K memory, 32-bit
philpem@40 761 */
philpem@59 762 void m68k_write_memory_32(uint32_t address, uint32_t value)/*{{{*/
philpem@40 763 {
philpem@40 764 // If ROMLMAP is set, force system to access ROM
philpem@40 765 if (!state.romlmap)
philpem@40 766 address |= 0x800000;
philpem@40 767
philpem@40 768 // Check access permissions
philpem@40 769 ACCESS_CHECK_WR(address, 32);
philpem@40 770
philpem@40 771 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 772 // ROM access
philpem@60 773 } else if (address <= 0x3FFFFF) {
philpem@40 774 // RAM access
philpem@60 775 uint32_t newAddr = mapAddr(address, true);
philpem@70 776 if (newAddr <= 0x1fffff)
philpem@60 777 WR32(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@70 778 else
philpem@65 779 WR32(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@40 780 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 781 // I/O register space, zone A
philpem@40 782 switch (address & 0x0F0000) {
philpem@40 783 case 0x000000: // Map RAM access
philpem@59 784 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD32 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 785 WR32(state.map, address, 0x7FF, value);
philpem@40 786 break;
philpem@40 787 case 0x020000: // Video RAM
philpem@59 788 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD32 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 789 WR32(state.vram, address, 0x7FFF, value);
philpem@40 790 break;
philpem@59 791 default:
philpem@59 792 IoWrite(address, value, 32);
philpem@40 793 }
philpem@59 794 } else {
philpem@59 795 IoWrite(address, value, 32);
philpem@40 796 }
philpem@59 797 }/*}}}*/
philpem@40 798
philpem@40 799 /**
philpem@40 800 * @brief Write M68K memory, 16-bit
philpem@40 801 */
philpem@59 802 void m68k_write_memory_16(uint32_t address, uint32_t value)/*{{{*/
philpem@40 803 {
philpem@40 804 // If ROMLMAP is set, force system to access ROM
philpem@40 805 if (!state.romlmap)
philpem@40 806 address |= 0x800000;
philpem@40 807
philpem@40 808 // Check access permissions
philpem@40 809 ACCESS_CHECK_WR(address, 16);
philpem@40 810
philpem@40 811 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 812 // ROM access
philpem@60 813 } else if (address <= 0x3FFFFF) {
philpem@40 814 // RAM access
philpem@60 815 uint32_t newAddr = mapAddr(address, true);
philpem@70 816 if (newAddr <= 0x1fffff)
philpem@60 817 WR16(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@70 818 else
philpem@65 819 WR16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@40 820 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 821 // I/O register space, zone A
philpem@40 822 switch (address & 0x0F0000) {
philpem@40 823 case 0x000000: // Map RAM access
philpem@40 824 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR16 to MapRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 825 WR16(state.map, address, 0x7FF, value);
philpem@40 826 break;
philpem@40 827 case 0x020000: // Video RAM
philpem@40 828 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR16 to VideoRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 829 WR16(state.vram, address, 0x7FFF, value);
philpem@40 830 break;
philpem@59 831 default:
philpem@59 832 IoWrite(address, value, 16);
philpem@40 833 }
philpem@59 834 } else {
philpem@59 835 IoWrite(address, value, 16);
philpem@40 836 }
philpem@59 837 }/*}}}*/
philpem@40 838
philpem@40 839 /**
philpem@40 840 * @brief Write M68K memory, 8-bit
philpem@40 841 */
philpem@59 842 void m68k_write_memory_8(uint32_t address, uint32_t value)/*{{{*/
philpem@40 843 {
philpem@40 844 // If ROMLMAP is set, force system to access ROM
philpem@40 845 if (!state.romlmap)
philpem@40 846 address |= 0x800000;
philpem@40 847
philpem@40 848 // Check access permissions
philpem@40 849 ACCESS_CHECK_WR(address, 8);
philpem@40 850
philpem@40 851 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 852 // ROM access (read only!)
philpem@60 853 } else if (address <= 0x3FFFFF) {
philpem@40 854 // RAM access
philpem@60 855 uint32_t newAddr = mapAddr(address, true);
philpem@70 856 if (newAddr <= 0x1fffff)
philpem@60 857 WR8(state.base_ram, newAddr, state.base_ram_size - 1, value);
philpem@70 858 else
philpem@65 859 WR8(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, value);
philpem@40 860 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 861 // I/O register space, zone A
philpem@40 862 switch (address & 0x0F0000) {
philpem@40 863 case 0x000000: // Map RAM access
philpem@59 864 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR8 to MapRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 865 WR8(state.map, address, 0x7FF, value);
philpem@40 866 break;
philpem@40 867 case 0x020000: // Video RAM
philpem@59 868 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR8 to VideoRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 869 WR8(state.vram, address, 0x7FFF, value);
philpem@40 870 break;
philpem@59 871 default:
philpem@59 872 IoWrite(address, value, 8);
philpem@40 873 }
philpem@59 874 } else {
philpem@59 875 IoWrite(address, value, 8);
philpem@40 876 }
philpem@59 877 }/*}}}*/
philpem@40 878
philpem@40 879
philpem@40 880 // for the disassembler
philpem@40 881 uint32_t m68k_read_disassembler_32(uint32_t addr) { return m68k_read_memory_32(addr); }
philpem@40 882 uint32_t m68k_read_disassembler_16(uint32_t addr) { return m68k_read_memory_16(addr); }
philpem@40 883 uint32_t m68k_read_disassembler_8 (uint32_t addr) { return m68k_read_memory_8 (addr); }
philpem@40 884