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.
 
 
 
 
 

146 lines
4.4 KiB

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