An emulator, assembler, and disassembler for the Sega Game Gear
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2014-2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. Released under the terms of the MIT License. See LICENSE for details. */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "logging.h"
  6. #include "util.h"
  7. #include "z80.h"
  8. /*
  9. Initialize a MMU object. This must be called before using the MMU.
  10. */
  11. void mmu_init(MMU *mmu)
  12. {
  13. mmu->system_ram = cr_malloc(sizeof(uint8_t) * MMU_SYSTEM_RAM_SIZE);
  14. for (size_t slot = 0; slot < MMU_NUM_SLOTS; slot++)
  15. mmu->map_slots[slot] = NULL;
  16. for (size_t bank = 0; bank < MMU_NUM_ROM_BANKS; bank++)
  17. mmu->rom_banks[bank] = NULL;
  18. }
  19. /*
  20. Free memory previously allocated by the MMU.
  21. */
  22. void mmu_free(MMU *mmu)
  23. {
  24. free(mmu->system_ram);
  25. }
  26. /*
  27. Load a block of ROM into the MMU.
  28. size must be a multiple of MMU_ROM_BANK_SIZE (16 KB), the load will fail
  29. silently. It should also be a power of two, or problems might occur with
  30. ROM mirroring logic. It should not be larger than
  31. MMU_ROM_BANK_SIZE * MMU_NUM_ROM_BANKS, or the extra banks will be ignored.
  32. This function will still work if called while the system is running, but it
  33. will likely cause unexpected behavior.
  34. */
  35. void mmu_load_rom(MMU *mmu, const uint8_t *data, size_t size)
  36. {
  37. if (size % MMU_ROM_BANK_SIZE)
  38. return;
  39. size_t banks = size / MMU_ROM_BANK_SIZE, bank, mirror;
  40. if (banks > MMU_NUM_ROM_BANKS)
  41. banks = MMU_NUM_ROM_BANKS;
  42. for (bank = 0; bank < banks; bank++) {
  43. for (mirror = bank; mirror < MMU_NUM_ROM_BANKS; mirror += banks)
  44. mmu->rom_banks[mirror] = data + (bank * MMU_ROM_BANK_SIZE);
  45. }
  46. #ifdef DEBUG_MODE
  47. char temp_str[64];
  48. DEBUG("Dumping MMU bank table:")
  49. for (size_t group = 0; group < MMU_NUM_ROM_BANKS / 8; group++) {
  50. for (size_t elem = 0; elem < 8; elem++) {
  51. bank = 8 * group + elem;
  52. snprintf(temp_str + 6 * elem, 7, "%02zX=%02zX ", bank,
  53. (mmu->rom_banks[bank] - data) >> 14);
  54. }
  55. temp_str[47] = '\0';
  56. DEBUG("- %s", temp_str)
  57. }
  58. #endif
  59. }
  60. /*
  61. Map the given RAM slot to the given ROM bank.
  62. */
  63. static inline void map_slot(MMU *mmu, size_t slot, size_t bank)
  64. {
  65. DEBUG("MMU mapping memory slot %zu to bank 0x%02zX", slot, bank)
  66. mmu->map_slots[slot] = mmu->rom_banks[bank];
  67. }
  68. /*
  69. Power on the MMU, setting initial memory values.
  70. This must be called before memory is read from or written to. If no ROM has
  71. been loaded, those regions will be read as 0xFF and will not accept writes.
  72. */
  73. void mmu_power(MMU *mmu)
  74. {
  75. for (size_t slot = 0; slot < MMU_NUM_SLOTS; slot++)
  76. map_slot(mmu, slot, slot);
  77. memset(mmu->system_ram, 0xFF, MMU_SYSTEM_RAM_SIZE);
  78. }
  79. /*
  80. Read a byte from a memory bank, or return 0xFF if the bank is not mapped.
  81. */
  82. static inline uint8_t bank_byte_read(const uint8_t* bank, uint16_t addr)
  83. {
  84. return bank ? bank[addr] : 0xFF;
  85. }
  86. /*
  87. Read a byte of memory from the given address.
  88. Memory region information is based on:
  89. - http://www.smspower.org/Development/MemoryMap
  90. - http://www.smspower.org/Development/Mappers
  91. */
  92. uint8_t mmu_read_byte(MMU *mmu, uint16_t addr)
  93. {
  94. if (addr < 0x0400) // First kilobyte is unpaged, for interrupt handlers
  95. return bank_byte_read(mmu->rom_banks[0], addr);
  96. else if (addr < 0x4000) // Slot 0 (0x0400 - 0x3FFF)
  97. return bank_byte_read(mmu->map_slots[0], addr);
  98. else if (addr < 0x8000) // Slot 1 (0x4000 - 0x7FFF)
  99. return bank_byte_read(mmu->map_slots[1], addr - 0x4000);
  100. else if (addr < 0xC000) // Slot 2 (0x8000 - 0xBFFF)
  101. return bank_byte_read(mmu->map_slots[2], addr - 0x8000);
  102. else if (addr < 0xE000) // System RAM (0xC000 - 0xDFFF)
  103. return mmu->system_ram[addr - 0xC000];
  104. else // System RAM, mirrored (0xE000 - 0xFFFF)
  105. return mmu->system_ram[addr - 0xE000];
  106. }
  107. /*
  108. Write a byte of memory to the given address.
  109. Return true if the byte was written, and false if it wasn't. Writes will
  110. fail when attempting to write to read-only memory.
  111. */
  112. bool mmu_write_byte(MMU *mmu, uint16_t addr, uint8_t value)
  113. {
  114. if (addr < 0xC000) { // TODO: implement writes to on-cartridge RAM
  115. return false;
  116. } else if (addr < 0xE000) { // System RAM (0xC000 - 0xDFFF)
  117. mmu->system_ram[addr - 0xC000] = value;
  118. return true;
  119. } else { // System RAM, mirrored (0xE000 - 0xFFFF)
  120. if (addr == 0xFFFC) {
  121. // TODO: handle cartridge RAM mapping control
  122. } else if (addr == 0xFFFD)
  123. map_slot(mmu, 0, value & 0x3F);
  124. else if (addr == 0xFFFE)
  125. map_slot(mmu, 1, value & 0x3F);
  126. else if (addr == 0xFFFF)
  127. map_slot(mmu, 2, value & 0x3F);
  128. mmu->system_ram[addr - 0xE000] = value;
  129. return true;
  130. }
  131. }