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.
 
 
 
 
 

164 lines
4.9 KiB

  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), 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(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. Write a byte of memory to the given address.
  118. Return true if the byte was written, and false if it wasn't. Writes will
  119. fail when attempting to write to read-only memory.
  120. */
  121. bool mmu_write_byte(MMU *mmu, uint16_t addr, uint8_t value)
  122. {
  123. if (addr < 0xC000) { // TODO: implement writes to on-cartridge RAM
  124. return false;
  125. } else if (addr < 0xE000) { // System RAM (0xC000 - 0xDFFF)
  126. mmu->system_ram[addr - 0xC000] = value;
  127. return true;
  128. } else { // System RAM, mirrored (0xE000 - 0xFFFF)
  129. if (addr == 0xFFFC) {
  130. // TODO: handle cartridge RAM mapping control
  131. } else if (addr == 0xFFFD)
  132. map_slot(mmu, 0, value & 0x3F);
  133. else if (addr == 0xFFFE)
  134. map_slot(mmu, 1, value & 0x3F);
  135. else if (addr == 0xFFFF)
  136. map_slot(mmu, 2, value & 0x3F);
  137. mmu->system_ram[addr - 0xE000] = value;
  138. return true;
  139. }
  140. }