src/memory.c

Tue, 14 Dec 2010 02:41:40 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Tue, 14 Dec 2010 02:41:40 +0000
changeset 57
feb84193a43a
parent 56
b3f309d46e97
child 59
d2e3b9e5d082
permissions
-rw-r--r--

add CHS param checking (FDC), fix DMA address setting (DMA_XFER)

* Floppy controller wasn't checking CHS values. Fixed.
* DMA ADDRESS COUNT register was implemented completely wrong -- shifts and ANDmasks were wrong. Fixed.

Boot PROM and Loader now run to the point of booting the kernel!

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@40 5 #include "musashi/m68k.h"
philpem@40 6 #include "state.h"
philpem@40 7 #include "memory.h"
philpem@40 8
philpem@40 9 /******************
philpem@40 10 * Memory mapping
philpem@40 11 ******************/
philpem@40 12
philpem@40 13 #define MAPRAM(addr) (((uint16_t)state.map[addr*2] << 8) + ((uint16_t)state.map[(addr*2)+1]))
philpem@40 14
philpem@40 15 uint32_t mapAddr(uint32_t addr, bool writing)
philpem@40 16 {
philpem@40 17 if (addr < 0x400000) {
philpem@40 18 // RAM access. Check against the Map RAM
philpem@40 19 // Start by getting the original page address
philpem@40 20 uint16_t page = (addr >> 12) & 0x3FF;
philpem@40 21
philpem@40 22 // Look it up in the map RAM and get the physical page address
philpem@40 23 uint32_t new_page_addr = MAPRAM(page) & 0x3FF;
philpem@40 24
philpem@40 25 // Update the Page Status bits
philpem@40 26 uint8_t pagebits = (MAPRAM(page) >> 13) & 0x03;
philpem@40 27 if (pagebits != 0) {
philpem@40 28 if (writing)
philpem@40 29 state.map[page*2] |= 0x60; // Page written to (dirty)
philpem@40 30 else
philpem@40 31 state.map[page*2] |= 0x40; // Page accessed but not written
philpem@40 32 }
philpem@40 33
philpem@40 34 // Return the address with the new physical page spliced in
philpem@40 35 return (new_page_addr << 12) + (addr & 0xFFF);
philpem@40 36 } else {
philpem@40 37 // I/O, VRAM or MapRAM space; no mapping is performed or required
philpem@40 38 // TODO: assert here?
philpem@40 39 return addr;
philpem@40 40 }
philpem@40 41 }
philpem@40 42
philpem@40 43 MEM_STATUS checkMemoryAccess(uint32_t addr, bool writing)
philpem@40 44 {
philpem@40 45 // Are we in Supervisor mode?
philpem@40 46 if (m68k_get_reg(NULL, M68K_REG_SR) & 0x2000)
philpem@40 47 // Yes. We can do anything we like.
philpem@40 48 return MEM_ALLOWED;
philpem@40 49
philpem@40 50 // If we're here, then we must be in User mode.
philpem@40 51 // Check that the user didn't access memory outside of the RAM area
philpem@40 52 if (addr >= 0x400000)
philpem@40 53 return MEM_UIE;
philpem@40 54
philpem@40 55 // This leaves us with Page Fault checking. Get the page bits for this page.
philpem@40 56 uint16_t page = (addr >> 12) & 0x3FF;
philpem@40 57 uint8_t pagebits = (MAPRAM(page) >> 13) & 0x07;
philpem@40 58
philpem@40 59 // Check page is present
philpem@40 60 if ((pagebits & 0x03) == 0)
philpem@40 61 return MEM_PAGEFAULT;
philpem@40 62
philpem@40 63 // User attempt to access the kernel
philpem@40 64 // A19, A20, A21, A22 low (kernel access): RAM addr before paging; not in Supervisor mode
philpem@40 65 if (((addr >> 19) & 0x0F) == 0)
philpem@40 66 return MEM_KERNEL;
philpem@40 67
philpem@40 68 // Check page is write enabled
philpem@40 69 if ((pagebits & 0x04) == 0)
philpem@40 70 return MEM_PAGE_NO_WE;
philpem@40 71
philpem@40 72 // Page access allowed.
philpem@40 73 return MEM_ALLOWED;
philpem@40 74 }
philpem@40 75
philpem@40 76 #undef MAPRAM
philpem@40 77
philpem@40 78
philpem@40 79 /********************************************************
philpem@40 80 * m68k memory read/write support functions for Musashi
philpem@40 81 ********************************************************/
philpem@40 82
philpem@40 83 /**
philpem@40 84 * @brief Check memory access permissions for a write operation.
philpem@40 85 * @note This used to be a single macro (merged with ACCESS_CHECK_RD), but
philpem@40 86 * gcc throws warnings when you have a return-with-value in a void
philpem@40 87 * function, even if the return-with-value is completely unreachable.
philpem@40 88 * Similarly it doesn't like it if you have a return without a value
philpem@40 89 * in a non-void function, even if it's impossible to ever reach the
philpem@40 90 * return-with-no-value. UGH!
philpem@40 91 */
philpem@40 92 #define ACCESS_CHECK_WR(address, bits) do { \
philpem@40 93 bool fault = false; \
philpem@40 94 /* MEM_STATUS st; */ \
philpem@40 95 switch (checkMemoryAccess(address, true)) { \
philpem@40 96 case MEM_ALLOWED: \
philpem@40 97 /* Access allowed */ \
philpem@40 98 break; \
philpem@40 99 case MEM_PAGEFAULT: \
philpem@40 100 /* Page fault */ \
philpem@44 101 state.genstat = 0x8BFF | (state.pie ? 0x0400 : 0); \
philpem@40 102 fault = true; \
philpem@40 103 break; \
philpem@40 104 case MEM_UIE: \
philpem@40 105 /* User access to memory above 4MB */ \
philpem@44 106 state.genstat = 0x9AFF | (state.pie ? 0x0400 : 0); \
philpem@40 107 fault = true; \
philpem@40 108 break; \
philpem@40 109 case MEM_KERNEL: \
philpem@40 110 case MEM_PAGE_NO_WE: \
philpem@40 111 /* kernel access or page not write enabled */ \
philpem@40 112 /* TODO: which regs need setting? */ \
philpem@40 113 fault = true; \
philpem@40 114 break; \
philpem@40 115 } \
philpem@40 116 \
philpem@40 117 if (fault) { \
philpem@40 118 if (bits >= 16) \
philpem@40 119 state.bsr0 = 0x7F00; \
philpem@40 120 else \
philpem@40 121 state.bsr0 = (address & 1) ? 0x7D00 : 0x7E00; \
philpem@40 122 state.bsr0 |= (address >> 16); \
philpem@40 123 state.bsr1 = address & 0xffff; \
philpem@40 124 printf("ERR: BusError WR\n"); \
philpem@40 125 m68k_pulse_bus_error(); \
philpem@40 126 return; \
philpem@40 127 } \
philpem@40 128 } while (false)
philpem@40 129
philpem@40 130 /**
philpem@40 131 * @brief Check memory access permissions for a read operation.
philpem@40 132 * @note This used to be a single macro (merged with ACCESS_CHECK_WR), but
philpem@40 133 * gcc throws warnings when you have a return-with-value in a void
philpem@40 134 * function, even if the return-with-value is completely unreachable.
philpem@40 135 * Similarly it doesn't like it if you have a return without a value
philpem@40 136 * in a non-void function, even if it's impossible to ever reach the
philpem@40 137 * return-with-no-value. UGH!
philpem@40 138 */
philpem@40 139 #define ACCESS_CHECK_RD(address, bits) do { \
philpem@40 140 bool fault = false; \
philpem@40 141 /* MEM_STATUS st; */ \
philpem@40 142 switch (checkMemoryAccess(address, false)) { \
philpem@40 143 case MEM_ALLOWED: \
philpem@40 144 /* Access allowed */ \
philpem@40 145 break; \
philpem@40 146 case MEM_PAGEFAULT: \
philpem@40 147 /* Page fault */ \
philpem@44 148 state.genstat = 0xCBFF | (state.pie ? 0x0400 : 0); \
philpem@40 149 fault = true; \
philpem@40 150 break; \
philpem@40 151 case MEM_UIE: \
philpem@40 152 /* User access to memory above 4MB */ \
philpem@44 153 state.genstat = 0xDAFF | (state.pie ? 0x0400 : 0); \
philpem@40 154 fault = true; \
philpem@40 155 break; \
philpem@40 156 case MEM_KERNEL: \
philpem@40 157 case MEM_PAGE_NO_WE: \
philpem@40 158 /* kernel access or page not write enabled */ \
philpem@40 159 /* TODO: which regs need setting? */ \
philpem@40 160 fault = true; \
philpem@40 161 break; \
philpem@40 162 } \
philpem@40 163 \
philpem@40 164 if (fault) { \
philpem@40 165 if (bits >= 16) \
philpem@40 166 state.bsr0 = 0x7F00; \
philpem@40 167 else \
philpem@40 168 state.bsr0 = (address & 1) ? 0x7D00 : 0x7E00; \
philpem@40 169 state.bsr0 |= (address >> 16); \
philpem@40 170 state.bsr1 = address & 0xffff; \
philpem@40 171 printf("ERR: BusError RD\n"); \
philpem@40 172 m68k_pulse_bus_error(); \
philpem@40 173 return 0xFFFFFFFF; \
philpem@40 174 } \
philpem@40 175 } while (false)
philpem@40 176
philpem@40 177 // Logging macros
philpem@40 178 #define LOG_NOT_HANDLED_R(bits) \
philpem@40 179 do { \
philpem@40 180 if (!handled) \
philpem@40 181 printf("unhandled read%02d, addr=0x%08X\n", bits, address); \
philpem@40 182 } while (0);
philpem@40 183
philpem@40 184 #define LOG_NOT_HANDLED_W(bits) \
philpem@40 185 do { \
philpem@40 186 if (!handled) \
philpem@40 187 printf("unhandled write%02d, addr=0x%08X, data=0x%08X\n", bits, address, value); \
philpem@40 188 } while (0);
philpem@40 189
philpem@40 190 /**
philpem@40 191 * @brief Read M68K memory, 32-bit
philpem@40 192 */
philpem@40 193 uint32_t m68k_read_memory_32(uint32_t address)
philpem@40 194 {
philpem@40 195 uint32_t data = 0xFFFFFFFF;
philpem@40 196 bool handled = false;
philpem@40 197
philpem@40 198 // If ROMLMAP is set, force system to access ROM
philpem@40 199 if (!state.romlmap)
philpem@40 200 address |= 0x800000;
philpem@40 201
philpem@40 202 // Check access permissions
philpem@40 203 ACCESS_CHECK_RD(address, 32);
philpem@40 204
philpem@40 205 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 206 // ROM access
philpem@40 207 data = RD32(state.rom, address, ROM_SIZE - 1);
philpem@40 208 handled = true;
philpem@40 209 } else if (address <= (state.ram_size - 1)) {
philpem@40 210 // RAM access
philpem@40 211 data = RD32(state.ram, mapAddr(address, false), state.ram_size - 1);
philpem@40 212 handled = true;
philpem@40 213 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 214 // I/O register space, zone A
philpem@40 215 switch (address & 0x0F0000) {
philpem@40 216 case 0x000000: // Map RAM access
philpem@40 217 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD32 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 218 data = RD32(state.map, address, 0x7FF);
philpem@40 219 handled = true;
philpem@40 220 break;
philpem@40 221 case 0x010000: // General Status Register
philpem@40 222 data = ((uint32_t)state.genstat << 16) + (uint32_t)state.genstat;
philpem@40 223 handled = true;
philpem@40 224 break;
philpem@40 225 case 0x020000: // Video RAM
philpem@40 226 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD32 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 227 data = RD32(state.vram, address, 0x7FFF);
philpem@40 228 handled = true;
philpem@40 229 break;
philpem@40 230 case 0x030000: // Bus Status Register 0
philpem@40 231 data = ((uint32_t)state.bsr0 << 16) + (uint32_t)state.bsr0;
philpem@40 232 handled = true;
philpem@40 233 break;
philpem@40 234 case 0x040000: // Bus Status Register 1
philpem@40 235 data = ((uint32_t)state.bsr1 << 16) + (uint32_t)state.bsr1;
philpem@40 236 handled = true;
philpem@40 237 break;
philpem@40 238 case 0x050000: // Phone status
philpem@40 239 break;
philpem@40 240 case 0x060000: // DMA Count
philpem@55 241 // TODO: U/OERR- is always inactive (bit set)... or should it be = DMAEN+?
philpem@55 242 // Bit 14 is always unused, so leave it set
philpem@55 243 data = (state.dma_count & 0x3fff) | 0xC000;
philpem@53 244 handled = true;
philpem@40 245 break;
philpem@40 246 case 0x070000: // Line Printer Status Register
philpem@53 247 data = 0x00120012; // no parity error, no line printer error, no irqs from FDD or HDD
philpem@53 248 data |= (state.fdc_ctx.irql) ? 0x00080008 : 0; // FIXME! HACKHACKHACK! shouldn't peek inside FDC structs like this
philpem@40 249 break;
philpem@40 250 case 0x080000: // Real Time Clock
philpem@40 251 break;
philpem@40 252 case 0x090000: // Phone registers
philpem@40 253 switch (address & 0x0FF000) {
philpem@40 254 case 0x090000: // Handset relay
philpem@40 255 case 0x098000:
philpem@40 256 break;
philpem@40 257 case 0x091000: // Line select 2
philpem@40 258 case 0x099000:
philpem@40 259 break;
philpem@40 260 case 0x092000: // Hook relay 1
philpem@40 261 case 0x09A000:
philpem@40 262 break;
philpem@40 263 case 0x093000: // Hook relay 2
philpem@40 264 case 0x09B000:
philpem@40 265 break;
philpem@40 266 case 0x094000: // Line 1 hold
philpem@40 267 case 0x09C000:
philpem@40 268 break;
philpem@40 269 case 0x095000: // Line 2 hold
philpem@40 270 case 0x09D000:
philpem@40 271 break;
philpem@40 272 case 0x096000: // Line 1 A-lead
philpem@40 273 case 0x09E000:
philpem@40 274 break;
philpem@40 275 case 0x097000: // Line 2 A-lead
philpem@40 276 case 0x09F000:
philpem@40 277 break;
philpem@40 278 }
philpem@40 279 break;
philpem@46 280 case 0x0A0000: // Miscellaneous Control Register -- write only!
philpem@46 281 handled = true;
philpem@40 282 break;
philpem@40 283 case 0x0B0000: // TM/DIALWR
philpem@40 284 break;
philpem@46 285 case 0x0C0000: // Clear Status Register -- write only!
philpem@43 286 handled = true;
philpem@40 287 break;
philpem@40 288 case 0x0D0000: // DMA Address Register
philpem@40 289 break;
philpem@40 290 case 0x0E0000: // Disk Control Register
philpem@40 291 break;
philpem@40 292 case 0x0F0000: // Line Printer Data Register
philpem@40 293 break;
philpem@40 294 }
philpem@40 295 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 296 // I/O register space, zone B
philpem@40 297 switch (address & 0xF00000) {
philpem@40 298 case 0xC00000: // Expansion slots
philpem@40 299 case 0xD00000:
philpem@40 300 switch (address & 0xFC0000) {
philpem@40 301 case 0xC00000: // Expansion slot 0
philpem@40 302 case 0xC40000: // Expansion slot 1
philpem@40 303 case 0xC80000: // Expansion slot 2
philpem@40 304 case 0xCC0000: // Expansion slot 3
philpem@40 305 case 0xD00000: // Expansion slot 4
philpem@40 306 case 0xD40000: // Expansion slot 5
philpem@40 307 case 0xD80000: // Expansion slot 6
philpem@40 308 case 0xDC0000: // Expansion slot 7
philpem@40 309 fprintf(stderr, "NOTE: RD32 from expansion card space, addr=0x%08X\n", address);
philpem@40 310 break;
philpem@40 311 }
philpem@40 312 break;
philpem@40 313 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 314 case 0xF00000:
philpem@40 315 switch (address & 0x070000) {
philpem@40 316 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 317 break;
philpem@40 318 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@52 319 data = wd2797_read_reg(&state.fdc_ctx, (address >> 1) & 3);
philpem@53 320 printf("WD279X: rd32 %02X ==> %02X\n", (address >> 1) & 3, data);
philpem@52 321 handled = true;
philpem@40 322 break;
philpem@40 323 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 324 break;
philpem@40 325 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 326 break;
philpem@40 327 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 328 switch (address & 0x077000) {
philpem@40 329 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@44 330 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@40 331 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 332 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@44 333 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@44 334 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@44 335 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@44 336 // All write-only registers... TODO: bus error?
philpem@44 337 handled = true;
philpem@40 338 break;
philpem@44 339 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video [FIXME: not in TRM]
philpem@40 340 break;
philpem@40 341 }
philpem@40 342 break;
philpem@40 343 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 344 break;
philpem@40 345 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 346 switch (address & 0x07F000) {
philpem@40 347 default:
philpem@40 348 break;
philpem@40 349 }
philpem@40 350 break;
philpem@40 351 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 352 break;
philpem@40 353 }
philpem@40 354 }
philpem@40 355 }
philpem@40 356
philpem@40 357 LOG_NOT_HANDLED_R(32);
philpem@40 358 return data;
philpem@40 359 }
philpem@40 360
philpem@40 361 /**
philpem@40 362 * @brief Read M68K memory, 16-bit
philpem@40 363 */
philpem@40 364 uint32_t m68k_read_memory_16(uint32_t address)
philpem@40 365 {
philpem@40 366 uint16_t data = 0xFFFF;
philpem@40 367 bool handled = false;
philpem@40 368
philpem@40 369 // If ROMLMAP is set, force system to access ROM
philpem@40 370 if (!state.romlmap)
philpem@40 371 address |= 0x800000;
philpem@40 372
philpem@40 373 // Check access permissions
philpem@40 374 ACCESS_CHECK_RD(address, 16);
philpem@40 375
philpem@40 376 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 377 // ROM access
philpem@40 378 data = RD16(state.rom, address, ROM_SIZE - 1);
philpem@40 379 handled = true;
philpem@40 380 } else if (address <= (state.ram_size - 1)) {
philpem@40 381 // RAM access
philpem@40 382 data = RD16(state.ram, mapAddr(address, false), state.ram_size - 1);
philpem@40 383 handled = true;
philpem@40 384 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 385 // I/O register space, zone A
philpem@40 386 switch (address & 0x0F0000) {
philpem@40 387 case 0x000000: // Map RAM access
philpem@40 388 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD16 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 389 data = RD16(state.map, address, 0x7FF);
philpem@40 390 handled = true;
philpem@40 391 break;
philpem@40 392 case 0x010000: // General Status Register
philpem@40 393 data = state.genstat;
philpem@40 394 handled = true;
philpem@40 395 break;
philpem@40 396 case 0x020000: // Video RAM
philpem@40 397 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD16 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 398 data = RD16(state.vram, address, 0x7FFF);
philpem@40 399 handled = true;
philpem@40 400 break;
philpem@40 401 case 0x030000: // Bus Status Register 0
philpem@40 402 data = state.bsr0;
philpem@40 403 handled = true;
philpem@40 404 break;
philpem@40 405 case 0x040000: // Bus Status Register 1
philpem@40 406 data = state.bsr1;
philpem@40 407 handled = true;
philpem@40 408 break;
philpem@40 409 case 0x050000: // Phone status
philpem@40 410 break;
philpem@40 411 case 0x060000: // DMA Count
philpem@55 412 // TODO: U/OERR- is always inactive (bit set)... or should it be = DMAEN+?
philpem@55 413 // Bit 14 is always unused, so leave it set
philpem@55 414 data = (state.dma_count & 0x3fff) | 0xC000;
philpem@53 415 handled = true;
philpem@40 416 break;
philpem@40 417 case 0x070000: // Line Printer Status Register
philpem@53 418 data = 0x0012; // no parity error, no line printer error, no irqs from FDD or HDD
philpem@53 419 data |= (state.fdc_ctx.irql) ? 0x0008 : 0; // FIXME! HACKHACKHACK! shouldn't peek inside FDC structs like this
philpem@40 420 break;
philpem@40 421 case 0x080000: // Real Time Clock
philpem@40 422 break;
philpem@40 423 case 0x090000: // Phone registers
philpem@40 424 switch (address & 0x0FF000) {
philpem@40 425 case 0x090000: // Handset relay
philpem@40 426 case 0x098000:
philpem@40 427 break;
philpem@40 428 case 0x091000: // Line select 2
philpem@40 429 case 0x099000:
philpem@40 430 break;
philpem@40 431 case 0x092000: // Hook relay 1
philpem@40 432 case 0x09A000:
philpem@40 433 break;
philpem@40 434 case 0x093000: // Hook relay 2
philpem@40 435 case 0x09B000:
philpem@40 436 break;
philpem@40 437 case 0x094000: // Line 1 hold
philpem@40 438 case 0x09C000:
philpem@40 439 break;
philpem@40 440 case 0x095000: // Line 2 hold
philpem@40 441 case 0x09D000:
philpem@40 442 break;
philpem@40 443 case 0x096000: // Line 1 A-lead
philpem@40 444 case 0x09E000:
philpem@40 445 break;
philpem@40 446 case 0x097000: // Line 2 A-lead
philpem@40 447 case 0x09F000:
philpem@40 448 break;
philpem@40 449 }
philpem@40 450 break;
philpem@46 451 case 0x0A0000: // Miscellaneous Control Register -- write only!
philpem@46 452 handled = true;
philpem@40 453 break;
philpem@40 454 case 0x0B0000: // TM/DIALWR
philpem@40 455 break;
philpem@46 456 case 0x0C0000: // Clear Status Register -- write only!
philpem@43 457 handled = true;
philpem@40 458 break;
philpem@40 459 case 0x0D0000: // DMA Address Register
philpem@40 460 break;
philpem@40 461 case 0x0E0000: // Disk Control Register
philpem@40 462 break;
philpem@40 463 case 0x0F0000: // Line Printer Data Register
philpem@40 464 break;
philpem@40 465 }
philpem@40 466 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 467 // I/O register space, zone B
philpem@40 468 switch (address & 0xF00000) {
philpem@40 469 case 0xC00000: // Expansion slots
philpem@40 470 case 0xD00000:
philpem@40 471 switch (address & 0xFC0000) {
philpem@40 472 case 0xC00000: // Expansion slot 0
philpem@40 473 case 0xC40000: // Expansion slot 1
philpem@40 474 case 0xC80000: // Expansion slot 2
philpem@40 475 case 0xCC0000: // Expansion slot 3
philpem@40 476 case 0xD00000: // Expansion slot 4
philpem@40 477 case 0xD40000: // Expansion slot 5
philpem@40 478 case 0xD80000: // Expansion slot 6
philpem@40 479 case 0xDC0000: // Expansion slot 7
philpem@40 480 fprintf(stderr, "NOTE: RD16 from expansion card space, addr=0x%08X\n", address);
philpem@40 481 break;
philpem@40 482 }
philpem@40 483 break;
philpem@40 484 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 485 case 0xF00000:
philpem@40 486 switch (address & 0x070000) {
philpem@40 487 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 488 break;
philpem@40 489 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@52 490 data = wd2797_read_reg(&state.fdc_ctx, (address >> 1) & 3);
philpem@53 491 printf("WD279X: rd16 %02X ==> %02X\n", (address >> 1) & 3, data);
philpem@52 492 handled = true;
philpem@40 493 break;
philpem@40 494 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 495 break;
philpem@40 496 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 497 break;
philpem@40 498 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 499 switch (address & 0x077000) {
philpem@40 500 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@44 501 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@40 502 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 503 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 504 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 505 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 506 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@44 507 // All write-only registers... TODO: bus error?
philpem@44 508 handled = true;
philpem@40 509 break;
philpem@40 510 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@40 511 break;
philpem@40 512 }
philpem@40 513 break;
philpem@40 514 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 515 break;
philpem@40 516 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 517 switch (address & 0x07F000) {
philpem@40 518 default:
philpem@40 519 break;
philpem@40 520 }
philpem@40 521 break;
philpem@40 522 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 523 break;
philpem@40 524 }
philpem@40 525 }
philpem@40 526 }
philpem@40 527
philpem@46 528 LOG_NOT_HANDLED_R(16);
philpem@40 529 return data;
philpem@40 530 }
philpem@40 531
philpem@40 532 /**
philpem@40 533 * @brief Read M68K memory, 8-bit
philpem@40 534 */
philpem@40 535 uint32_t m68k_read_memory_8(uint32_t address)
philpem@40 536 {
philpem@40 537 uint8_t data = 0xFF;
philpem@40 538 bool handled = false;
philpem@40 539
philpem@40 540 // If ROMLMAP is set, force system to access ROM
philpem@40 541 if (!state.romlmap)
philpem@40 542 address |= 0x800000;
philpem@40 543
philpem@40 544 // Check access permissions
philpem@40 545 ACCESS_CHECK_RD(address, 8);
philpem@40 546
philpem@40 547 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 548 // ROM access
philpem@40 549 data = RD8(state.rom, address, ROM_SIZE - 1);
philpem@40 550 handled = true;
philpem@40 551 } else if (address <= (state.ram_size - 1)) {
philpem@40 552 // RAM access
philpem@40 553 data = RD8(state.ram, mapAddr(address, false), state.ram_size - 1);
philpem@40 554 handled = true;
philpem@40 555 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 556 // I/O register space, zone A
philpem@40 557 switch (address & 0x0F0000) {
philpem@40 558 case 0x000000: // Map RAM access
philpem@40 559 if (address > 0x4007FF) fprintf(stderr, "NOTE: RD8 from MapRAM mirror, addr=0x%08X\n", address);
philpem@40 560 data = RD8(state.map, address, 0x7FF);
philpem@40 561 handled = true;
philpem@40 562 break;
philpem@40 563 case 0x010000: // General Status Register
philpem@40 564 if ((address & 1) == 0)
philpem@40 565 data = (state.genstat >> 8) & 0xff;
philpem@40 566 else
philpem@40 567 data = (state.genstat) & 0xff;
philpem@40 568 handled = true;
philpem@40 569 break;
philpem@40 570 case 0x020000: // Video RAM
philpem@40 571 if (address > 0x427FFF) fprintf(stderr, "NOTE: RD8 from VideoRAM mirror, addr=0x%08X\n", address);
philpem@40 572 data = RD8(state.vram, address, 0x7FFF);
philpem@40 573 handled = true;
philpem@40 574 break;
philpem@40 575 case 0x030000: // Bus Status Register 0
philpem@40 576 if ((address & 1) == 0)
philpem@40 577 data = (state.bsr0 >> 8) & 0xff;
philpem@40 578 else
philpem@40 579 data = (state.bsr0) & 0xff;
philpem@40 580 handled = true;
philpem@40 581 break;
philpem@40 582 case 0x040000: // Bus Status Register 1
philpem@40 583 if ((address & 1) == 0)
philpem@40 584 data = (state.bsr1 >> 8) & 0xff;
philpem@40 585 else
philpem@40 586 data = (state.bsr1) & 0xff;
philpem@40 587 handled = true;
philpem@40 588 break;
philpem@40 589 case 0x050000: // Phone status
philpem@40 590 break;
philpem@40 591 case 0x060000: // DMA Count
philpem@53 592 // TODO: how to handle this in 8bit mode?
philpem@40 593 break;
philpem@40 594 case 0x070000: // Line Printer Status Register
philpem@53 595 printf("\tLPSR RD8 fdc irql=%d, irqe=%d\n", state.fdc_ctx.irql, state.fdc_ctx.irqe);
philpem@53 596 if (address & 1) {
philpem@53 597 data = 0x12; // no parity error, no line printer error, no irqs from FDD or HDD
philpem@53 598 data |= (state.fdc_ctx.irql) ? 0x08 : 0; // FIXME! HACKHACKHACK! shouldn't peek inside FDC structs like this
philpem@57 599 // data |= 0x04; // HDD interrupt, i.e. command complete -- HACKHACKHACK!
philpem@53 600 } else {
philpem@53 601 data = 0;
philpem@53 602 }
philpem@57 603 handled = true;
philpem@40 604 break;
philpem@40 605 case 0x080000: // Real Time Clock
philpem@40 606 break;
philpem@40 607 case 0x090000: // Phone registers
philpem@40 608 switch (address & 0x0FF000) {
philpem@40 609 case 0x090000: // Handset relay
philpem@40 610 case 0x098000:
philpem@40 611 break;
philpem@40 612 case 0x091000: // Line select 2
philpem@40 613 case 0x099000:
philpem@40 614 break;
philpem@40 615 case 0x092000: // Hook relay 1
philpem@40 616 case 0x09A000:
philpem@40 617 break;
philpem@40 618 case 0x093000: // Hook relay 2
philpem@40 619 case 0x09B000:
philpem@40 620 break;
philpem@40 621 case 0x094000: // Line 1 hold
philpem@40 622 case 0x09C000:
philpem@40 623 break;
philpem@40 624 case 0x095000: // Line 2 hold
philpem@40 625 case 0x09D000:
philpem@40 626 break;
philpem@40 627 case 0x096000: // Line 1 A-lead
philpem@40 628 case 0x09E000:
philpem@40 629 break;
philpem@40 630 case 0x097000: // Line 2 A-lead
philpem@40 631 case 0x09F000:
philpem@40 632 break;
philpem@40 633 }
philpem@40 634 break;
philpem@46 635 case 0x0A0000: // Miscellaneous Control Register -- write only!
philpem@46 636 handled = true;
philpem@40 637 break;
philpem@40 638 case 0x0B0000: // TM/DIALWR
philpem@40 639 break;
philpem@46 640 case 0x0C0000: // Clear Status Register -- write only!
philpem@43 641 handled = true;
philpem@40 642 break;
philpem@40 643 case 0x0D0000: // DMA Address Register
philpem@40 644 break;
philpem@40 645 case 0x0E0000: // Disk Control Register
philpem@40 646 break;
philpem@40 647 case 0x0F0000: // Line Printer Data Register
philpem@40 648 break;
philpem@40 649 }
philpem@40 650 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 651 // I/O register space, zone B
philpem@40 652 switch (address & 0xF00000) {
philpem@40 653 case 0xC00000: // Expansion slots
philpem@40 654 case 0xD00000:
philpem@40 655 switch (address & 0xFC0000) {
philpem@40 656 case 0xC00000: // Expansion slot 0
philpem@40 657 case 0xC40000: // Expansion slot 1
philpem@40 658 case 0xC80000: // Expansion slot 2
philpem@40 659 case 0xCC0000: // Expansion slot 3
philpem@40 660 case 0xD00000: // Expansion slot 4
philpem@40 661 case 0xD40000: // Expansion slot 5
philpem@40 662 case 0xD80000: // Expansion slot 6
philpem@40 663 case 0xDC0000: // Expansion slot 7
philpem@40 664 fprintf(stderr, "NOTE: RD8 from expansion card space, addr=0x%08X\n", address);
philpem@40 665 break;
philpem@40 666 }
philpem@40 667 break;
philpem@40 668 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 669 case 0xF00000:
philpem@40 670 switch (address & 0x070000) {
philpem@40 671 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 672 break;
philpem@40 673 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@52 674 data = wd2797_read_reg(&state.fdc_ctx, (address >> 1) & 3);
philpem@53 675 printf("WD279X: rd8 %02X ==> %02X\n", (address >> 1) & 3, data);
philpem@52 676 handled = true;
philpem@40 677 break;
philpem@40 678 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 679 break;
philpem@40 680 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 681 break;
philpem@40 682 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 683 switch (address & 0x077000) {
philpem@40 684 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@44 685 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@40 686 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 687 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 688 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 689 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 690 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@44 691 // All write-only registers... TODO: bus error?
philpem@44 692 handled = true;
philpem@40 693 break;
philpem@40 694 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@40 695 break;
philpem@40 696 }
philpem@40 697 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 698 break;
philpem@40 699 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 700 switch (address & 0x07F000) {
philpem@40 701 default:
philpem@40 702 break;
philpem@40 703 }
philpem@40 704 break;
philpem@40 705 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 706 break;
philpem@40 707 }
philpem@40 708 }
philpem@40 709 }
philpem@40 710
philpem@40 711 LOG_NOT_HANDLED_R(8);
philpem@40 712
philpem@40 713 return data;
philpem@40 714 }
philpem@40 715
philpem@40 716 /**
philpem@40 717 * @brief Write M68K memory, 32-bit
philpem@40 718 */
philpem@40 719 void m68k_write_memory_32(uint32_t address, uint32_t value)
philpem@40 720 {
philpem@40 721 bool handled = false;
philpem@40 722
philpem@40 723 // If ROMLMAP is set, force system to access ROM
philpem@40 724 if (!state.romlmap)
philpem@40 725 address |= 0x800000;
philpem@40 726
philpem@40 727 // Check access permissions
philpem@40 728 ACCESS_CHECK_WR(address, 32);
philpem@40 729
philpem@40 730 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 731 // ROM access
philpem@40 732 handled = true;
philpem@40 733 } else if (address <= (state.ram_size - 1)) {
philpem@40 734 // RAM access
philpem@40 735 WR32(state.ram, mapAddr(address, false), state.ram_size - 1, value);
philpem@40 736 handled = true;
philpem@40 737 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 738 // I/O register space, zone A
philpem@40 739 switch (address & 0x0F0000) {
philpem@40 740 case 0x000000: // Map RAM access
philpem@40 741 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR32 to MapRAM mirror, addr=0x%08X, data=0x%08X\n", address, value);
philpem@40 742 WR32(state.map, address, 0x7FF, value);
philpem@40 743 handled = true;
philpem@40 744 break;
philpem@40 745 case 0x010000: // General Status Register
philpem@40 746 state.genstat = (value & 0xffff);
philpem@40 747 handled = true;
philpem@40 748 break;
philpem@40 749 case 0x020000: // Video RAM
philpem@40 750 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR32 to VideoRAM mirror, addr=0x%08X, data=0x%08X\n", address, value);
philpem@40 751 WR32(state.vram, address, 0x7FFF, value);
philpem@40 752 handled = true;
philpem@40 753 break;
philpem@40 754 case 0x030000: // Bus Status Register 0
philpem@40 755 break;
philpem@40 756 case 0x040000: // Bus Status Register 1
philpem@40 757 break;
philpem@40 758 case 0x050000: // Phone status
philpem@40 759 break;
philpem@40 760 case 0x060000: // DMA Count
philpem@53 761 printf("WR32 dmacount %08X\n", value);
philpem@53 762 state.dma_count = (value & 0x3FFF);
philpem@53 763 state.idmarw = ((value & 0x4000) == 0x4000);
philpem@53 764 state.dmaen = ((value & 0x8000) == 0x8000);
philpem@53 765 printf("\tcount %04X, idmarw %d, dmaen %d\n", state.dma_count, state.idmarw, state.dmaen);
philpem@55 766 // This handles the "dummy DMA transfer" mentioned in the docs
philpem@55 767 // TODO: access check, peripheral access
philpem@55 768 if (!state.idmarw)
philpem@55 769 WR32(state.ram, mapAddr(address, false), state.ram_size - 1, 0xDEAD);
philpem@55 770 state.dma_count++;
philpem@53 771 handled = true;
philpem@40 772 break;
philpem@40 773 case 0x070000: // Line Printer Status Register
philpem@40 774 break;
philpem@40 775 case 0x080000: // Real Time Clock
philpem@40 776 break;
philpem@40 777 case 0x090000: // Phone registers
philpem@40 778 switch (address & 0x0FF000) {
philpem@40 779 case 0x090000: // Handset relay
philpem@40 780 case 0x098000:
philpem@40 781 break;
philpem@40 782 case 0x091000: // Line select 2
philpem@40 783 case 0x099000:
philpem@40 784 break;
philpem@40 785 case 0x092000: // Hook relay 1
philpem@40 786 case 0x09A000:
philpem@40 787 break;
philpem@40 788 case 0x093000: // Hook relay 2
philpem@40 789 case 0x09B000:
philpem@40 790 break;
philpem@40 791 case 0x094000: // Line 1 hold
philpem@40 792 case 0x09C000:
philpem@40 793 break;
philpem@40 794 case 0x095000: // Line 2 hold
philpem@40 795 case 0x09D000:
philpem@40 796 break;
philpem@40 797 case 0x096000: // Line 1 A-lead
philpem@40 798 case 0x09E000:
philpem@40 799 break;
philpem@40 800 case 0x097000: // Line 2 A-lead
philpem@40 801 case 0x09F000:
philpem@40 802 break;
philpem@40 803 }
philpem@40 804 break;
philpem@40 805 case 0x0A0000: // Miscellaneous Control Register
philpem@46 806 // TODO: handle the ctrl bits properly
philpem@52 807 // TODO: &0x8000 --> dismiss 60hz intr
philpem@52 808 state.dma_reading = (value & 0x4000);
philpem@46 809 state.leds = (~value & 0xF00) >> 8;
philpem@46 810 printf("LEDs: %s %s %s %s\n",
philpem@46 811 (state.leds & 8) ? "R" : "-",
philpem@46 812 (state.leds & 4) ? "G" : "-",
philpem@46 813 (state.leds & 2) ? "Y" : "-",
philpem@46 814 (state.leds & 1) ? "R" : "-");
philpem@46 815 handled = true;
philpem@40 816 break;
philpem@40 817 case 0x0B0000: // TM/DIALWR
philpem@40 818 break;
philpem@43 819 case 0x0C0000: // Clear Status Register
philpem@43 820 state.genstat = 0xFFFF;
philpem@43 821 state.bsr0 = 0xFFFF;
philpem@43 822 state.bsr1 = 0xFFFF;
philpem@43 823 handled = true;
philpem@40 824 break;
philpem@40 825 case 0x0D0000: // DMA Address Register
philpem@52 826 if (address & 0x004000) {
philpem@52 827 // A14 high -- set most significant bits
philpem@57 828 state.dma_address = (state.dma_address & 0x1fe) | ((address & 0x3ffe) << 8);
philpem@52 829 } else {
philpem@52 830 // A14 low -- set least significant bits
philpem@57 831 state.dma_address = (state.dma_address & 0x3ffe00) | (address & 0x1fe);
philpem@52 832 }
philpem@57 833 printf("WR32 DMA_ADDR %s, now %08X\n", address & 0x004000 ? "HI" : "LO", state.dma_address);
philpem@53 834 handled = true;
philpem@40 835 break;
philpem@40 836 case 0x0E0000: // Disk Control Register
philpem@52 837 // B7 = FDD controller reset
philpem@52 838 if ((value & 0x80) == 0) wd2797_reset(&state.fdc_ctx);
philpem@52 839 // B6 = drive 0 select -- TODO
philpem@52 840 // B5 = motor enable -- TODO
philpem@52 841 // B4 = HDD controller reset -- TODO
philpem@52 842 // B3 = HDD0 select -- TODO
philpem@52 843 // B2,1,0 = HDD0 head select
philpem@53 844 handled = true;
philpem@40 845 break;
philpem@40 846 case 0x0F0000: // Line Printer Data Register
philpem@40 847 break;
philpem@40 848 }
philpem@40 849 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 850 // I/O register space, zone B
philpem@40 851 switch (address & 0xF00000) {
philpem@40 852 case 0xC00000: // Expansion slots
philpem@40 853 case 0xD00000:
philpem@40 854 switch (address & 0xFC0000) {
philpem@40 855 case 0xC00000: // Expansion slot 0
philpem@40 856 case 0xC40000: // Expansion slot 1
philpem@40 857 case 0xC80000: // Expansion slot 2
philpem@40 858 case 0xCC0000: // Expansion slot 3
philpem@40 859 case 0xD00000: // Expansion slot 4
philpem@40 860 case 0xD40000: // Expansion slot 5
philpem@40 861 case 0xD80000: // Expansion slot 6
philpem@40 862 case 0xDC0000: // Expansion slot 7
philpem@40 863 fprintf(stderr, "NOTE: WR32 to expansion card space, addr=0x%08X, data=0x%08X\n", address, value);
philpem@40 864 handled = true;
philpem@40 865 break;
philpem@40 866 }
philpem@40 867 break;
philpem@40 868 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 869 case 0xF00000:
philpem@40 870 switch (address & 0x070000) {
philpem@40 871 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 872 break;
philpem@40 873 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@53 874 printf("WD279X: wr32 %02X ==> %02X\n", (address >> 1) & 3, value);
philpem@52 875 wd2797_write_reg(&state.fdc_ctx, (address >> 1) & 3, value);
philpem@53 876 handled = true;
philpem@40 877 break;
philpem@40 878 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 879 break;
philpem@40 880 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 881 break;
philpem@40 882 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 883 switch (address & 0x077000) {
philpem@40 884 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@40 885 break;
philpem@44 886 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@44 887 state.pie = ((value & 0x8000) == 0x8000);
philpem@44 888 handled = true;
philpem@40 889 break;
philpem@40 890 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 891 break;
philpem@40 892 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 893 state.romlmap = ((value & 0x8000) == 0x8000);
philpem@44 894 handled = true;
philpem@40 895 break;
philpem@40 896 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 897 break;
philpem@40 898 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 899 break;
philpem@40 900 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@40 901 break;
philpem@40 902 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@40 903 break;
philpem@40 904 }
philpem@40 905 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 906 break;
philpem@40 907 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 908 switch (address & 0x07F000) {
philpem@40 909 default:
philpem@40 910 break;
philpem@40 911 }
philpem@40 912 break;
philpem@40 913 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 914 break;
philpem@40 915 }
philpem@40 916 }
philpem@40 917 }
philpem@40 918
philpem@40 919 LOG_NOT_HANDLED_W(32);
philpem@40 920 }
philpem@40 921
philpem@40 922 /**
philpem@40 923 * @brief Write M68K memory, 16-bit
philpem@40 924 */
philpem@40 925 void m68k_write_memory_16(uint32_t address, uint32_t value)
philpem@40 926 {
philpem@40 927 bool handled = false;
philpem@40 928
philpem@40 929 // If ROMLMAP is set, force system to access ROM
philpem@40 930 if (!state.romlmap)
philpem@40 931 address |= 0x800000;
philpem@40 932
philpem@40 933 // Check access permissions
philpem@40 934 ACCESS_CHECK_WR(address, 16);
philpem@40 935
philpem@40 936 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 937 // ROM access
philpem@40 938 handled = true;
philpem@40 939 } else if (address <= (state.ram_size - 1)) {
philpem@40 940 // RAM access
philpem@40 941 WR16(state.ram, mapAddr(address, false), state.ram_size - 1, value);
philpem@40 942 handled = true;
philpem@40 943 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 944 // I/O register space, zone A
philpem@40 945 switch (address & 0x0F0000) {
philpem@40 946 case 0x000000: // Map RAM access
philpem@40 947 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR16 to MapRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 948 WR16(state.map, address, 0x7FF, value);
philpem@40 949 handled = true;
philpem@40 950 break;
philpem@40 951 case 0x010000: // General Status Register (read only)
philpem@40 952 handled = true;
philpem@40 953 break;
philpem@40 954 case 0x020000: // Video RAM
philpem@40 955 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR16 to VideoRAM mirror, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 956 WR16(state.vram, address, 0x7FFF, value);
philpem@40 957 handled = true;
philpem@40 958 break;
philpem@40 959 case 0x030000: // Bus Status Register 0 (read only)
philpem@40 960 handled = true;
philpem@40 961 break;
philpem@40 962 case 0x040000: // Bus Status Register 1 (read only)
philpem@40 963 handled = true;
philpem@40 964 break;
philpem@40 965 case 0x050000: // Phone status
philpem@40 966 break;
philpem@40 967 case 0x060000: // DMA Count
philpem@53 968 printf("WR16 dmacount %08X\n", value);
philpem@53 969 state.dma_count = (value & 0x3FFF);
philpem@53 970 state.idmarw = ((value & 0x4000) == 0x4000);
philpem@53 971 state.dmaen = ((value & 0x8000) == 0x8000);
philpem@53 972 printf("\tcount %04X, idmarw %d, dmaen %d\n", state.dma_count, state.idmarw, state.dmaen);
philpem@55 973 // This handles the "dummy DMA transfer" mentioned in the docs
philpem@55 974 // TODO: access check, peripheral access
philpem@55 975 if (!state.idmarw)
philpem@55 976 WR32(state.ram, mapAddr(address, false), state.ram_size - 1, 0xDEAD);
philpem@55 977 state.dma_count++;
philpem@53 978 handled = true;
philpem@40 979 break;
philpem@40 980 case 0x070000: // Line Printer Status Register
philpem@40 981 break;
philpem@40 982 case 0x080000: // Real Time Clock
philpem@40 983 break;
philpem@40 984 case 0x090000: // Phone registers
philpem@40 985 switch (address & 0x0FF000) {
philpem@40 986 case 0x090000: // Handset relay
philpem@40 987 case 0x098000:
philpem@40 988 break;
philpem@40 989 case 0x091000: // Line select 2
philpem@40 990 case 0x099000:
philpem@40 991 break;
philpem@40 992 case 0x092000: // Hook relay 1
philpem@40 993 case 0x09A000:
philpem@40 994 break;
philpem@40 995 case 0x093000: // Hook relay 2
philpem@40 996 case 0x09B000:
philpem@40 997 break;
philpem@40 998 case 0x094000: // Line 1 hold
philpem@40 999 case 0x09C000:
philpem@40 1000 break;
philpem@40 1001 case 0x095000: // Line 2 hold
philpem@40 1002 case 0x09D000:
philpem@40 1003 break;
philpem@40 1004 case 0x096000: // Line 1 A-lead
philpem@40 1005 case 0x09E000:
philpem@40 1006 break;
philpem@40 1007 case 0x097000: // Line 2 A-lead
philpem@40 1008 case 0x09F000:
philpem@40 1009 break;
philpem@40 1010 }
philpem@40 1011 break;
philpem@40 1012 case 0x0A0000: // Miscellaneous Control Register
philpem@46 1013 // TODO: handle the ctrl bits properly
philpem@52 1014 // TODO: &0x8000 --> dismiss 60hz intr
philpem@52 1015 state.dma_reading = (value & 0x4000);
philpem@46 1016 state.leds = (~value & 0xF00) >> 8;
philpem@46 1017 printf("LEDs: %s %s %s %s\n",
philpem@46 1018 (state.leds & 8) ? "R" : "-",
philpem@46 1019 (state.leds & 4) ? "G" : "-",
philpem@46 1020 (state.leds & 2) ? "Y" : "-",
philpem@46 1021 (state.leds & 1) ? "R" : "-");
philpem@46 1022 handled = true;
philpem@40 1023 break;
philpem@40 1024 case 0x0B0000: // TM/DIALWR
philpem@40 1025 break;
philpem@43 1026 case 0x0C0000: // Clear Status Register
philpem@43 1027 state.genstat = 0xFFFF;
philpem@43 1028 state.bsr0 = 0xFFFF;
philpem@43 1029 state.bsr1 = 0xFFFF;
philpem@43 1030 handled = true;
philpem@40 1031 break;
philpem@40 1032 case 0x0D0000: // DMA Address Register
philpem@52 1033 if (address & 0x004000) {
philpem@52 1034 // A14 high -- set most significant bits
philpem@57 1035 state.dma_address = (state.dma_address & 0x1fe) | ((address & 0x3ffe) << 8);
philpem@52 1036 } else {
philpem@52 1037 // A14 low -- set least significant bits
philpem@57 1038 state.dma_address = (state.dma_address & 0x3ffe00) | (address & 0x1fe);
philpem@52 1039 }
philpem@57 1040 printf("WR16 DMA_ADDR %s, now %08X\n", address & 0x004000 ? "HI" : "LO", state.dma_address);
philpem@53 1041 handled = true;
philpem@40 1042 break;
philpem@40 1043 case 0x0E0000: // Disk Control Register
philpem@52 1044 // B7 = FDD controller reset
philpem@52 1045 if ((value & 0x80) == 0) wd2797_reset(&state.fdc_ctx);
philpem@52 1046 // B6 = drive 0 select -- TODO
philpem@52 1047 // B5 = motor enable -- TODO
philpem@52 1048 // B4 = HDD controller reset -- TODO
philpem@52 1049 // B3 = HDD0 select -- TODO
philpem@52 1050 // B2,1,0 = HDD0 head select
philpem@53 1051 handled = true;
philpem@40 1052 break;
philpem@40 1053 case 0x0F0000: // Line Printer Data Register
philpem@40 1054 break;
philpem@40 1055 }
philpem@40 1056 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 1057 // I/O register space, zone B
philpem@40 1058 switch (address & 0xF00000) {
philpem@40 1059 case 0xC00000: // Expansion slots
philpem@40 1060 case 0xD00000:
philpem@40 1061 switch (address & 0xFC0000) {
philpem@40 1062 case 0xC00000: // Expansion slot 0
philpem@40 1063 case 0xC40000: // Expansion slot 1
philpem@40 1064 case 0xC80000: // Expansion slot 2
philpem@40 1065 case 0xCC0000: // Expansion slot 3
philpem@40 1066 case 0xD00000: // Expansion slot 4
philpem@40 1067 case 0xD40000: // Expansion slot 5
philpem@40 1068 case 0xD80000: // Expansion slot 6
philpem@40 1069 case 0xDC0000: // Expansion slot 7
philpem@40 1070 fprintf(stderr, "NOTE: WR16 to expansion card space, addr=0x%08X, data=0x%04X\n", address, value);
philpem@40 1071 break;
philpem@40 1072 }
philpem@40 1073 break;
philpem@40 1074 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 1075 case 0xF00000:
philpem@40 1076 switch (address & 0x070000) {
philpem@40 1077 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 1078 break;
philpem@40 1079 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@53 1080 printf("WD279X: wr16 %02X ==> %02X\n", (address >> 1) & 3, value);
philpem@52 1081 wd2797_write_reg(&state.fdc_ctx, (address >> 1) & 3, value);
philpem@53 1082 handled = true;
philpem@40 1083 break;
philpem@40 1084 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 1085 break;
philpem@40 1086 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 1087 break;
philpem@40 1088 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 1089 switch (address & 0x077000) {
philpem@40 1090 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@40 1091 break;
philpem@44 1092 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@44 1093 state.pie = ((value & 0x8000) == 0x8000);
philpem@44 1094 handled = true;
philpem@40 1095 break;
philpem@40 1096 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 1097 break;
philpem@40 1098 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 1099 state.romlmap = ((value & 0x8000) == 0x8000);
philpem@40 1100 handled = true;
philpem@40 1101 break;
philpem@40 1102 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 1103 break;
philpem@40 1104 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 1105 break;
philpem@40 1106 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@40 1107 break;
philpem@40 1108 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@40 1109 break;
philpem@40 1110 }
philpem@40 1111 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 1112 break;
philpem@40 1113 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 1114 switch (address & 0x07F000) {
philpem@40 1115 default:
philpem@40 1116 break;
philpem@40 1117 }
philpem@40 1118 break;
philpem@40 1119 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 1120 break;
philpem@40 1121 }
philpem@40 1122 }
philpem@40 1123 }
philpem@40 1124
philpem@40 1125 LOG_NOT_HANDLED_W(16);
philpem@40 1126 }
philpem@40 1127
philpem@40 1128 /**
philpem@40 1129 * @brief Write M68K memory, 8-bit
philpem@40 1130 */
philpem@40 1131 void m68k_write_memory_8(uint32_t address, uint32_t value)
philpem@40 1132 {
philpem@40 1133 bool handled = false;
philpem@40 1134
philpem@40 1135 // If ROMLMAP is set, force system to access ROM
philpem@40 1136 if (!state.romlmap)
philpem@40 1137 address |= 0x800000;
philpem@40 1138
philpem@40 1139 // Check access permissions
philpem@40 1140 ACCESS_CHECK_WR(address, 8);
philpem@40 1141
philpem@40 1142 if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
philpem@40 1143 // ROM access (read only!)
philpem@40 1144 handled = true;
philpem@40 1145 } else if (address <= (state.ram_size - 1)) {
philpem@40 1146 // RAM access
philpem@40 1147 WR8(state.ram, mapAddr(address, false), state.ram_size - 1, value);
philpem@40 1148 handled = true;
philpem@40 1149 } else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
philpem@40 1150 // I/O register space, zone A
philpem@40 1151 switch (address & 0x0F0000) {
philpem@40 1152 case 0x000000: // Map RAM access
philpem@40 1153 if (address > 0x4007FF) fprintf(stderr, "NOTE: WR8 to MapRAM mirror, addr=%08X, data=%02X\n", address, value);
philpem@40 1154 WR8(state.map, address, 0x7FF, value);
philpem@40 1155 handled = true;
philpem@40 1156 break;
philpem@40 1157 case 0x010000: // General Status Register
philpem@40 1158 handled = true;
philpem@40 1159 break;
philpem@40 1160 case 0x020000: // Video RAM
philpem@46 1161 if (address > 0x427FFF) fprintf(stderr, "NOTE: WR8 to VideoRAM mirror, addr=%08X, data=0x%02X\n", address, value);
philpem@40 1162 WR8(state.vram, address, 0x7FFF, value);
philpem@40 1163 handled = true;
philpem@40 1164 break;
philpem@40 1165 case 0x030000: // Bus Status Register 0
philpem@40 1166 handled = true;
philpem@40 1167 break;
philpem@40 1168 case 0x040000: // Bus Status Register 1
philpem@40 1169 handled = true;
philpem@40 1170 break;
philpem@40 1171 case 0x050000: // Phone status
philpem@40 1172 break;
philpem@40 1173 case 0x060000: // DMA Count
philpem@53 1174 // TODO: how to handle this in 8bit mode?
philpem@40 1175 break;
philpem@40 1176 case 0x070000: // Line Printer Status Register
philpem@40 1177 break;
philpem@40 1178 case 0x080000: // Real Time Clock
philpem@40 1179 break;
philpem@40 1180 case 0x090000: // Phone registers
philpem@40 1181 switch (address & 0x0FF000) {
philpem@40 1182 case 0x090000: // Handset relay
philpem@40 1183 case 0x098000:
philpem@40 1184 break;
philpem@40 1185 case 0x091000: // Line select 2
philpem@40 1186 case 0x099000:
philpem@40 1187 break;
philpem@40 1188 case 0x092000: // Hook relay 1
philpem@40 1189 case 0x09A000:
philpem@40 1190 break;
philpem@40 1191 case 0x093000: // Hook relay 2
philpem@40 1192 case 0x09B000:
philpem@40 1193 break;
philpem@40 1194 case 0x094000: // Line 1 hold
philpem@40 1195 case 0x09C000:
philpem@40 1196 break;
philpem@40 1197 case 0x095000: // Line 2 hold
philpem@40 1198 case 0x09D000:
philpem@40 1199 break;
philpem@40 1200 case 0x096000: // Line 1 A-lead
philpem@40 1201 case 0x09E000:
philpem@40 1202 break;
philpem@40 1203 case 0x097000: // Line 2 A-lead
philpem@40 1204 case 0x09F000:
philpem@40 1205 break;
philpem@40 1206 }
philpem@40 1207 break;
philpem@40 1208 case 0x0A0000: // Miscellaneous Control Register
philpem@53 1209 // TODO: how to handle this in 8bit mode?
philpem@53 1210 /*
philpem@46 1211 // TODO: handle the ctrl bits properly
philpem@52 1212 if ((address & 1) == 0) {
philpem@52 1213 // low byte
philpem@52 1214 } else {
philpem@52 1215 // hight byte
philpem@52 1216 // TODO: &0x8000 --> dismiss 60hz intr
philpem@52 1217 state.dma_reading = (value & 0x40);
philpem@46 1218 state.leds = (~value & 0xF);
philpem@52 1219 }
philpem@46 1220 printf("LEDs: %s %s %s %s\n",
philpem@46 1221 (state.leds & 8) ? "R" : "-",
philpem@46 1222 (state.leds & 4) ? "G" : "-",
philpem@46 1223 (state.leds & 2) ? "Y" : "-",
philpem@46 1224 (state.leds & 1) ? "R" : "-");
philpem@46 1225 handled = true;
philpem@53 1226 */
philpem@40 1227 break;
philpem@40 1228 case 0x0B0000: // TM/DIALWR
philpem@40 1229 break;
philpem@43 1230 case 0x0C0000: // Clear Status Register
philpem@43 1231 state.genstat = 0xFFFF;
philpem@43 1232 state.bsr0 = 0xFFFF;
philpem@43 1233 state.bsr1 = 0xFFFF;
philpem@43 1234 handled = true;
philpem@40 1235 break;
philpem@40 1236 case 0x0D0000: // DMA Address Register
philpem@52 1237 if (address & 0x004000) {
philpem@52 1238 // A14 high -- set most significant bits
philpem@57 1239 state.dma_address = (state.dma_address & 0x1fe) | ((address & 0x3ffe) << 8);
philpem@52 1240 } else {
philpem@52 1241 // A14 low -- set least significant bits
philpem@57 1242 state.dma_address = (state.dma_address & 0x3ffe00) | (address & 0x1fe);
philpem@52 1243 }
philpem@57 1244 printf("WR08 DMA_ADDR %s, now %08X\n", address & 0x004000 ? "HI" : "LO", state.dma_address);
philpem@53 1245 handled = true;
philpem@40 1246 break;
philpem@40 1247 case 0x0E0000: // Disk Control Register
philpem@52 1248 // B7 = FDD controller reset
philpem@52 1249 if ((value & 0x80) == 0) wd2797_reset(&state.fdc_ctx);
philpem@52 1250 // B6 = drive 0 select -- TODO
philpem@52 1251 // B5 = motor enable -- TODO
philpem@52 1252 // B4 = HDD controller reset -- TODO
philpem@52 1253 // B3 = HDD0 select -- TODO
philpem@52 1254 // B2,1,0 = HDD0 head select
philpem@53 1255 handled = true;
philpem@40 1256 break;
philpem@40 1257 case 0x0F0000: // Line Printer Data Register
philpem@40 1258 break;
philpem@40 1259 }
philpem@40 1260 } else if ((address >= 0xC00000) && (address <= 0xFFFFFF)) {
philpem@40 1261 // I/O register space, zone B
philpem@40 1262 switch (address & 0xF00000) {
philpem@40 1263 case 0xC00000: // Expansion slots
philpem@40 1264 case 0xD00000:
philpem@40 1265 switch (address & 0xFC0000) {
philpem@40 1266 case 0xC00000: // Expansion slot 0
philpem@40 1267 case 0xC40000: // Expansion slot 1
philpem@40 1268 case 0xC80000: // Expansion slot 2
philpem@40 1269 case 0xCC0000: // Expansion slot 3
philpem@40 1270 case 0xD00000: // Expansion slot 4
philpem@40 1271 case 0xD40000: // Expansion slot 5
philpem@40 1272 case 0xD80000: // Expansion slot 6
philpem@40 1273 case 0xDC0000: // Expansion slot 7
philpem@40 1274 fprintf(stderr, "NOTE: WR8 to expansion card space, addr=0x%08X, data=0x%08X\n", address, value);
philpem@40 1275 break;
philpem@40 1276 }
philpem@40 1277 break;
philpem@40 1278 case 0xE00000: // HDC, FDC, MCR2 and RTC data bits
philpem@40 1279 case 0xF00000:
philpem@40 1280 switch (address & 0x070000) {
philpem@40 1281 case 0x000000: // [ef][08]xxxx ==> WD1010 hard disc controller
philpem@40 1282 break;
philpem@40 1283 case 0x010000: // [ef][19]xxxx ==> WD2797 floppy disc controller
philpem@53 1284 printf("WD279X: wr8 %02X ==> %02X\n", (address >> 1) & 3, value);
philpem@52 1285 wd2797_write_reg(&state.fdc_ctx, (address >> 1) & 3, value);
philpem@53 1286 handled = true;
philpem@40 1287 break;
philpem@40 1288 case 0x020000: // [ef][2a]xxxx ==> Miscellaneous Control Register 2
philpem@40 1289 break;
philpem@40 1290 case 0x030000: // [ef][3b]xxxx ==> Real Time Clock data bits
philpem@40 1291 break;
philpem@40 1292 case 0x040000: // [ef][4c]xxxx ==> General Control Register
philpem@40 1293 switch (address & 0x077000) {
philpem@40 1294 case 0x040000: // [ef][4c][08]xxx ==> EE
philpem@40 1295 break;
philpem@44 1296 case 0x041000: // [ef][4c][19]xxx ==> PIE
philpem@44 1297 if ((address & 1) == 0)
philpem@44 1298 state.pie = ((value & 0x80) == 0x80);
philpem@44 1299 handled = true;
philpem@40 1300 break;
philpem@40 1301 case 0x042000: // [ef][4c][2A]xxx ==> BP
philpem@40 1302 break;
philpem@40 1303 case 0x043000: // [ef][4c][3B]xxx ==> ROMLMAP
philpem@40 1304 if ((address & 1) == 0)
philpem@40 1305 state.romlmap = ((value & 0x80) == 0x80);
philpem@40 1306 handled = true;
philpem@40 1307 break;
philpem@40 1308 case 0x044000: // [ef][4c][4C]xxx ==> L1 MODEM
philpem@40 1309 break;
philpem@40 1310 case 0x045000: // [ef][4c][5D]xxx ==> L2 MODEM
philpem@40 1311 break;
philpem@40 1312 case 0x046000: // [ef][4c][6E]xxx ==> D/N CONNECT
philpem@40 1313 break;
philpem@40 1314 case 0x047000: // [ef][4c][7F]xxx ==> Whole screen reverse video
philpem@40 1315 break;
philpem@40 1316 }
philpem@40 1317 case 0x050000: // [ef][5d]xxxx ==> 8274
philpem@40 1318 break;
philpem@40 1319 case 0x060000: // [ef][6e]xxxx ==> Control regs
philpem@40 1320 switch (address & 0x07F000) {
philpem@40 1321 default:
philpem@40 1322 break;
philpem@40 1323 }
philpem@40 1324 break;
philpem@40 1325 case 0x070000: // [ef][7f]xxxx ==> 6850 Keyboard Controller
philpem@40 1326 break;
philpem@40 1327 default:
philpem@40 1328 fprintf(stderr, "NOTE: WR8 to undefined E/F-block space, addr=0x%08X, data=0x%08X\n", address, value);
philpem@40 1329 break;
philpem@40 1330 }
philpem@40 1331 }
philpem@40 1332 }
philpem@40 1333
philpem@40 1334 LOG_NOT_HANDLED_W(8);
philpem@40 1335 }
philpem@40 1336
philpem@40 1337
philpem@40 1338 // for the disassembler
philpem@40 1339 uint32_t m68k_read_disassembler_32(uint32_t addr) { return m68k_read_memory_32(addr); }
philpem@40 1340 uint32_t m68k_read_disassembler_16(uint32_t addr) { return m68k_read_memory_16(addr); }
philpem@40 1341 uint32_t m68k_read_disassembler_8 (uint32_t addr) { return m68k_read_memory_8 (addr); }
philpem@40 1342
philpem@40 1343