An emulator, assembler, and disassembler for the Sega Game Gear
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mmu.c 5.6 KiB

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