|
| 1 | +package eu.rekawek.coffeegb.core.memory.cart.type; |
| 2 | + |
| 3 | +import eu.rekawek.coffeegb.core.events.EventBus; |
| 4 | +import eu.rekawek.coffeegb.core.memento.Memento; |
| 5 | +import eu.rekawek.coffeegb.core.memory.cart.MemoryController; |
| 6 | +import eu.rekawek.coffeegb.core.memory.cart.Rom; |
| 7 | +import eu.rekawek.coffeegb.core.memory.cart.battery.Battery; |
| 8 | + |
| 9 | +/** |
| 10 | + * MBC7 Mapper implementation. |
| 11 | + * Used by Kirby's Tilt 'n' Tumble. |
| 12 | + * Features ROM banking, accelerometer (sensor), and EEPROM. |
| 13 | + */ |
| 14 | +public class Mbc7 implements MemoryController { |
| 15 | + |
| 16 | + private final int romBanks; |
| 17 | + |
| 18 | + private final int[] cartridge; |
| 19 | + |
| 20 | + private int selectedRomBank = 1; |
| 21 | + |
| 22 | + private boolean ramWriteEnabled1; |
| 23 | + |
| 24 | + private boolean ramWriteEnabled2; |
| 25 | + |
| 26 | + private double x, y = 0; |
| 27 | + |
| 28 | + private int latchX = 0x8000; |
| 29 | + |
| 30 | + private int latchY = 0x8000; |
| 31 | + |
| 32 | + private int latchState = 0; |
| 33 | + |
| 34 | + private final Mbc7Eeprom eeprom; |
| 35 | + |
| 36 | + public Mbc7(Rom rom, Battery battery) { |
| 37 | + this.cartridge = rom.getRom(); |
| 38 | + this.romBanks = rom.getRomBanks(); |
| 39 | + eeprom = new Mbc7Eeprom(battery); |
| 40 | + } |
| 41 | + |
| 42 | + public void init(EventBus eventBus) { |
| 43 | + eventBus.register(event -> { |
| 44 | + synchronized (this) { |
| 45 | + x = event.x(); |
| 46 | + y = event.y(); |
| 47 | + } |
| 48 | + }, AccelerometerEvent.class); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean accepts(int address) { |
| 53 | + return (address >= 0x0000 && address < 0x8000) || (address >= 0xa000 && address < 0xc000); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void setByte(int address, int value) { |
| 58 | + if (address >= 0x0000 && address < 0x2000) { |
| 59 | + if (value == 0x0a) { |
| 60 | + ramWriteEnabled1 = true; |
| 61 | + } else if (value == 0x00) { |
| 62 | + ramWriteEnabled1 = false; |
| 63 | + ramWriteEnabled2 = false; |
| 64 | + } |
| 65 | + } else if (address >= 0x2000 && address < 0x3000) { |
| 66 | + selectedRomBank = (selectedRomBank & 0x100) | value; |
| 67 | + } else if (address >= 0x3000 && address < 0x4000) { |
| 68 | + selectedRomBank = (selectedRomBank & 0x0ff) | ((value & 1) << 8); |
| 69 | + } else if (address >= 0x4000 && address < 0x6000) { |
| 70 | + if (value == 0x40 && ramWriteEnabled1) { |
| 71 | + ramWriteEnabled2 = true; |
| 72 | + } |
| 73 | + } else if (address >= 0xa000 && address < 0xb000 && ramWriteEnabled2) { |
| 74 | + int a = (address >> 4) & 0xf; |
| 75 | + if (a == 0) { |
| 76 | + if (value == 0x55) { |
| 77 | + latchState = 1; |
| 78 | + latchX = 0x8000; |
| 79 | + latchY = 0x8000; |
| 80 | + } |
| 81 | + } else if (a == 1) { |
| 82 | + if (value == 0xaa) { |
| 83 | + synchronized (this) { |
| 84 | + latchX = 0x81d0 + (int) (x * 0x70); |
| 85 | + latchY = 0x81d0 + (int) (y * 0x70); |
| 86 | + } |
| 87 | + latchState = 0; |
| 88 | + } |
| 89 | + } else if (a == 8) { |
| 90 | + writeEeprom(value); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public int getByte(int address) { |
| 97 | + if (address >= 0x0000 && address < 0x4000) { |
| 98 | + return getRomByte(0, address); |
| 99 | + } else if (address >= 0x4000 && address < 0x8000) { |
| 100 | + return getRomByte(selectedRomBank % romBanks, address - 0x4000); |
| 101 | + } else if (address >= 0xa000 && address < 0xc000) { |
| 102 | + if (!ramWriteEnabled2) { |
| 103 | + return 0xff; |
| 104 | + } |
| 105 | + return switch ((address >> 4) & 0xf) { |
| 106 | + case 2 -> latchX & 0xff; |
| 107 | + case 3 -> (latchX >> 8) & 0xff; |
| 108 | + case 4 -> latchY & 0xff; |
| 109 | + case 5 -> (latchY >> 8) & 0xff; |
| 110 | + case 6 -> 0; |
| 111 | + case 8 -> readEeprom(); |
| 112 | + default -> 0xff; |
| 113 | + }; |
| 114 | + } else { |
| 115 | + throw new IllegalArgumentException(Integer.toHexString(address)); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private void writeEeprom(int value) { |
| 120 | + eeprom.write(value); |
| 121 | + } |
| 122 | + |
| 123 | + private int readEeprom() { |
| 124 | + return eeprom.read(); |
| 125 | + } |
| 126 | + |
| 127 | + private int getRomByte(int bank, int address) { |
| 128 | + int cartOffset = bank * 0x4000 + address; |
| 129 | + if (cartOffset < cartridge.length) { |
| 130 | + return cartridge[cartOffset]; |
| 131 | + } else { |
| 132 | + return 0xff; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void flushRam() { |
| 138 | + eeprom.flush(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public Memento<MemoryController> saveToMemento() { |
| 143 | + return new Mbc7Memento( |
| 144 | + selectedRomBank, |
| 145 | + ramWriteEnabled1, |
| 146 | + ramWriteEnabled2, |
| 147 | + x, |
| 148 | + y, |
| 149 | + latchX, |
| 150 | + latchY, |
| 151 | + latchState, |
| 152 | + eeprom.saveToMemento() |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void restoreFromMemento(Memento<MemoryController> memento) { |
| 158 | + if (!(memento instanceof Mbc7Memento mem)) { |
| 159 | + throw new IllegalArgumentException("Invalid memento type"); |
| 160 | + } |
| 161 | + this.selectedRomBank = mem.selectedRomBank; |
| 162 | + this.ramWriteEnabled1 = mem.ramWriteEnabled1; |
| 163 | + this.ramWriteEnabled2 = mem.ramWriteEnabled2; |
| 164 | + this.x = mem.x; |
| 165 | + this.y = mem.y; |
| 166 | + this.latchX = mem.latchX; |
| 167 | + this.latchY = mem.latchY; |
| 168 | + this.latchState = mem.latchState; |
| 169 | + this.eeprom.restoreFromMemento(mem.eepromMemento); |
| 170 | + } |
| 171 | + |
| 172 | + private record Mbc7Memento( |
| 173 | + int selectedRomBank, |
| 174 | + boolean ramWriteEnabled1, |
| 175 | + boolean ramWriteEnabled2, |
| 176 | + double x, |
| 177 | + double y, |
| 178 | + int latchX, |
| 179 | + int latchY, |
| 180 | + int latchState, |
| 181 | + Memento<Mbc7Eeprom> eepromMemento |
| 182 | + ) implements Memento<MemoryController> { |
| 183 | + } |
| 184 | +} |
0 commit comments