| e8555c89 Andy McCall |
1 |
#ifndef VDPVRAM_HH |
| 2 |
#define VDPVRAM_HH |
| 3 |
|
| 4 |
#include "VDP.hh" |
| 5 |
#include "VDPCmdEngine.hh" |
| 6 |
#include "VRAMObserver.hh" |
| 7 |
|
| 8 |
#include "Ram.hh" |
| 9 |
#include "SimpleDebuggable.hh" |
| 10 |
|
| 11 |
#include "Math.hh" |
| 12 |
|
| 13 |
#include <cassert> |
| 14 |
#include <cstdint> |
| 15 |
|
| 16 |
namespace openmsx { |
| 17 |
|
| 18 |
class DisplayMode; |
| 19 |
class SpriteChecker; |
| 20 |
class Renderer; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
class DummyVRAMObserver final : public VRAMObserver |
| 121 |
{ |
| 122 |
public: |
| 123 |
void updateVRAM(unsigned , EmuTime ) override {} |
| 124 |
void updateWindow(bool , EmuTime ) override {} |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
class VRAMWindow |
| 138 |
{ |
| 139 |
public: |
| 140 |
VRAMWindow(const VRAMWindow&) = delete; |
| 141 |
VRAMWindow(VRAMWindow&&) = delete; |
| 142 |
VRAMWindow& operator=(const VRAMWindow&) = delete; |
| 143 |
VRAMWindow& operator=(VRAMWindow&&) = delete; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
[[nodiscard]] unsigned getMask() const { |
| 151 |
assert(isEnabled()); |
| 152 |
return effectiveBaseMask; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
void setMask(unsigned newBaseMask, unsigned newIndexMask, |
| 169 |
unsigned newSizeMask, EmuTime time) { |
| 170 |
origBaseMask = newBaseMask; |
| 171 |
newBaseMask &= newSizeMask; |
| 172 |
if (isEnabled() && |
| 173 |
(newBaseMask == effectiveBaseMask) && |
| 174 |
(newIndexMask == indexMask)) { |
| 175 |
return; |
| 176 |
} |
| 177 |
observer->updateWindow(true, time); |
| 178 |
effectiveBaseMask = newBaseMask; |
| 179 |
indexMask = newIndexMask; |
| 180 |
baseAddr = effectiveBaseMask & indexMask; |
| 181 |
combiMask = ~effectiveBaseMask | indexMask; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void setMask(unsigned newBaseMask, unsigned newIndexMask, |
| 188 |
EmuTime time) { |
| 189 |
setMask(newBaseMask, newIndexMask, sizeMask, time); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
void disable(EmuTime time) { |
| 196 |
observer->updateWindow(false, time); |
| 197 |
baseAddr = unsigned(-1); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
[[nodiscard]] bool isContinuous(unsigned index, unsigned size) const { |
| 204 |
assert(isEnabled()); |
| 205 |
unsigned endIndex = index + size - 1; |
| 206 |
unsigned areaBits = Math::floodRight(index ^ endIndex); |
| 207 |
if ((areaBits & effectiveBaseMask) != areaBits) return false; |
| 208 |
if ((areaBits & ~indexMask) != areaBits) return false; |
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
[[nodiscard]] bool isContinuous(unsigned mask) const { |
| 221 |
assert(isEnabled()); |
| 222 |
assert((mask & ~indexMask) == mask); |
| 223 |
return (mask & effectiveBaseMask) == mask; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
template<size_t size> |
| 231 |
[[nodiscard]] std::span<const uint8_t, size> getReadArea(unsigned index) const { |
| 232 |
assert(isContinuous(index, size)); |
| 233 |
return std::span<const uint8_t, size>{ |
| 234 |
&data[effectiveBaseMask & (indexMask | index)], |
| 235 |
size}; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
template<size_t size> |
| 247 |
[[nodiscard]] std::pair<std::span<const uint8_t, size / 2>, std::span<const uint8_t, size / 2>> |
| 248 |
getReadAreaPlanar(unsigned index) const { |
| 249 |
assert((index & 1) == 0); |
| 250 |
assert((size & 1) == 0); |
| 251 |
unsigned endIndex = index + size - 1; |
| 252 |
unsigned areaBits = Math::floodRight(index ^ endIndex); |
| 253 |
areaBits = ((areaBits << 16) | (areaBits >> 1)) & 0x1FFFF & sizeMask; |
| 254 |
(void)areaBits; |
| 255 |
assert((areaBits & effectiveBaseMask) == areaBits); |
| 256 |
assert((areaBits & ~indexMask) == areaBits); |
| 257 |
assert(isEnabled()); |
| 258 |
unsigned addr = effectiveBaseMask & (indexMask | (index >> 1)); |
| 259 |
const uint8_t* ptr0 = &data[addr | 0x00000]; |
| 260 |
const uint8_t* ptr1 = &data[addr | 0x10000]; |
| 261 |
return {std::span<const uint8_t, size / 2>{ptr0, size / 2}, |
| 262 |
std::span<const uint8_t, size / 2>{ptr1, size / 2}}; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
[[nodiscard]] uint8_t readNP(unsigned index) const { |
| 269 |
assert(isEnabled()); |
| 270 |
return data[effectiveBaseMask & index]; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
[[nodiscard]] uint8_t readPlanar(unsigned index) const { |
| 277 |
assert(isEnabled()); |
| 278 |
index = ((index & 1) << 16) | ((index & 0x1FFFE) >> 1); |
| 279 |
unsigned addr = effectiveBaseMask & index; |
| 280 |
return data[addr]; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
[[nodiscard]] bool hasObserver() const { |
| 286 |
return observer != &dummyObserver; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
void setObserver(VRAMObserver* newObserver) { |
| 295 |
observer = newObserver; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
void resetObserver() { |
| 301 |
observer = &dummyObserver; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
[[nodiscard]] bool isInside(unsigned address) const { |
| 312 |
return (address & combiMask) == baseAddr; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
void notify(unsigned address, EmuTime time) { |
| 321 |
if (isInside(address)) { |
| 322 |
observer->updateVRAM(address - baseAddr, time); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
void setSizeMask(unsigned newSizeMask, EmuTime time) { |
| 331 |
if (isEnabled()) { |
| 332 |
setMask(origBaseMask, indexMask, newSizeMask, time); |
| 333 |
} |
| 334 |
|
| 335 |
sizeMask = newSizeMask; |
| 336 |
} |
| 337 |
|
| 338 |
template<typename Archive> |
| 339 |
void serialize(Archive& ar, unsigned version); |
| 340 |
|
| 341 |
private: |
| 342 |
[[nodiscard]] bool isEnabled() const { |
| 343 |
return baseAddr != unsigned(-1); |
| 344 |
} |
| 345 |
|
| 346 |
private: |
| 347 |
|
| 348 |
|
| 349 |
friend class VDPVRAM; |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
explicit VRAMWindow(Ram& vram); |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
uint8_t* data; |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
VRAMObserver* observer = &dummyObserver; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
unsigned origBaseMask = 0; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
unsigned effectiveBaseMask = 0; |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
unsigned indexMask = 0; |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
unsigned baseAddr = unsigned(-1); |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
unsigned combiMask = 0; |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
unsigned sizeMask; |
| 393 |
|
| 394 |
static inline DummyVRAMObserver dummyObserver; |
| 395 |
}; |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
class VDPVRAM |
| 402 |
{ |
| 403 |
public: |
| 404 |
VDPVRAM(const VDPVRAM&) = delete; |
| 405 |
VDPVRAM(VDPVRAM&&) = delete; |
| 406 |
VDPVRAM& operator=(const VDPVRAM&) = delete; |
| 407 |
VDPVRAM& operator=(VDPVRAM&&) = delete; |
| 408 |
|
| 409 |
VDPVRAM(VDP& vdp, unsigned size, EmuTime time); |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
void clear(); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
void sync(EmuTime time) { |
| 420 |
assert(vdp.isInsideFrame(time)); |
| 421 |
cmdEngine->sync(time); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
void cmdWrite(unsigned address, uint8_t value, EmuTime time) { |
| 431 |
#ifdef DEBUG |
| 432 |
|
| 433 |
assert(time >= vramTime); |
| 434 |
#endif |
| 435 |
assert(vdp.isInsideFrame(time)); |
| 436 |
|
| 437 |
|
| 438 |
address &= sizeMask; |
| 439 |
if (address >= actualSize) [[unlikely]] { |
| 440 |
|
| 441 |
assert(address < 0x30000); |
| 442 |
|
| 443 |
|
| 444 |
return; |
| 445 |
} |
| 446 |
|
| 447 |
writeCommon(address, value, time); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
void cpuWrite(unsigned address, uint8_t value, EmuTime time) { |
| 456 |
#ifdef DEBUG |
| 457 |
|
| 458 |
assert(time >= vramTime); |
| 459 |
#endif |
| 460 |
assert(vdp.isInsideFrame(time)); |
| 461 |
|
| 462 |
|
| 463 |
address &= sizeMask; |
| 464 |
if (address >= actualSize) [[unlikely]] { |
| 465 |
|
| 466 |
assert(address < 0x30000); |
| 467 |
|
| 468 |
|
| 469 |
return; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
if (cmdReadWindow .isInside(address) || |
| 478 |
cmdWriteWindow.isInside(address)) { |
| 479 |
cmdEngine->sync(time); |
| 480 |
} |
| 481 |
writeCommon(address, value, time); |
| 482 |
|
| 483 |
cmdEngine->stealAccessSlot(time); |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
[[nodiscard]] uint8_t cpuRead(unsigned address, EmuTime time) { |
| 492 |
#ifdef DEBUG |
| 493 |
|
| 494 |
assert(time >= vramTime); |
| 495 |
#endif |
| 496 |
assert(vdp.isInsideFrame(time)); |
| 497 |
|
| 498 |
address &= sizeMask; |
| 499 |
if (cmdWriteWindow.isInside(address)) { |
| 500 |
cmdEngine->sync(time); |
| 501 |
} |
| 502 |
cmdEngine->stealAccessSlot(time); |
| 503 |
|
| 504 |
#ifdef DEBUG |
| 505 |
vramTime = time; |
| 506 |
#endif |
| 507 |
return data[address]; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
void updateDisplayMode(DisplayMode mode, bool cmdBit, EmuTime time); |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
void updateDisplayEnabled(bool enabled, EmuTime time); |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
void updateSpritesEnabled(bool enabled, EmuTime time); |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
void updateVRMode(bool mode, EmuTime time); |
| 539 |
|
| 540 |
void setRenderer(Renderer* renderer, EmuTime time); |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
[[nodiscard]] unsigned getSize() const { |
| 545 |
return actualSize; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
void setSpriteChecker(SpriteChecker* newSpriteChecker) { |
| 551 |
spriteChecker = newSpriteChecker; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
void setCmdEngine(VDPCmdEngine* newCmdEngine) { |
| 557 |
cmdEngine = newCmdEngine; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
void change4k8kMapping(bool mapping8k); |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
[[nodiscard]] std::span<const uint8_t> getData() const { |
| 568 |
return {data.data(), data.size()}; |
| 569 |
} |
| 570 |
|
| 571 |
template<typename Archive> |
| 572 |
void serialize(Archive& ar, unsigned version); |
| 573 |
|
| 574 |
private: |
| 575 |
|
| 576 |
|
| 577 |
void writeCommon(unsigned address, uint8_t value, EmuTime time) { |
| 578 |
#ifdef DEBUG |
| 579 |
assert(time >= vramTime); |
| 580 |
vramTime = time; |
| 581 |
#endif |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
if (data[address] == value) return; |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
bitmapVisibleWindow.notify(address, time); |
| 592 |
spriteAttribTable.notify(address, time); |
| 593 |
spritePatternTable.notify(address, time); |
| 594 |
|
| 595 |
data[address] = value; |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
assert(!bitmapCacheWindow.hasObserver()); |
| 604 |
assert(!nameTable.hasObserver()); |
| 605 |
|
| 606 |
|
| 607 |
assert(!colorTable.hasObserver()); |
| 608 |
assert(!patternTable.hasObserver()); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
void setSizeMask(EmuTime time); |
| 624 |
|
| 625 |
private: |
| 626 |
|
| 627 |
|
| 628 |
VDP& vdp; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
Ram data; |
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
class LogicalVRAMDebuggable final : public SimpleDebuggable { |
| 640 |
public: |
| 641 |
explicit LogicalVRAMDebuggable(const VDP& vdp); |
| 642 |
[[nodiscard]] uint8_t read(unsigned address, EmuTime time) override; |
| 643 |
void write(unsigned address, uint8_t value, EmuTime time) override; |
| 644 |
private: |
| 645 |
unsigned transform(unsigned address); |
| 646 |
} logicalVRAMDebug; |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
struct PhysicalVRAMDebuggable final : SimpleDebuggable { |
| 653 |
PhysicalVRAMDebuggable(const VDP& vdp, unsigned actualSize); |
| 654 |
[[nodiscard]] uint8_t read(unsigned address, EmuTime time) override; |
| 655 |
void write(unsigned address, uint8_t value, EmuTime time) override; |
| 656 |
} physicalVRAMDebug; |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
Renderer* renderer; |
| 662 |
|
| 663 |
VDPCmdEngine* cmdEngine; |
| 664 |
SpriteChecker* spriteChecker; |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
#ifdef DEBUG |
| 671 |
EmuTime vramTime = EmuTime::zero(); |
| 672 |
#endif |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
unsigned sizeMask; |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
const unsigned actualSize; |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
bool vrMode; |
| 688 |
|
| 689 |
public: |
| 690 |
VRAMWindow cmdReadWindow; |
| 691 |
VRAMWindow cmdWriteWindow; |
| 692 |
VRAMWindow nameTable; |
| 693 |
VRAMWindow colorTable; |
| 694 |
VRAMWindow patternTable; |
| 695 |
VRAMWindow bitmapVisibleWindow; |
| 696 |
VRAMWindow bitmapCacheWindow; |
| 697 |
VRAMWindow spriteAttribTable; |
| 698 |
VRAMWindow spritePatternTable; |
| 699 |
}; |
| 700 |
|
| 701 |
} |
| 702 |
|
| 703 |
#endif |