@@ -13512,53 +13512,60 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
13512
13512
13513
13513
var _this = _possibleConstructorReturn(this, (MBC.__proto__ || Object.getPrototypeOf(MBC)).call(this));
13514
13514
13515
+ _this.romSizes = [0x00008000, // 32K
13516
+ 0x00010000, // 64K
13517
+ 0x00020000, // 128K
13518
+ 0x00040000, // 256K
13519
+ 0x00080000, // 512K
13520
+ 0x00100000, // 1024K
13521
+ 0x00200000, // 2048K
13522
+ 0x00400000, // 4096K
13523
+ 0x00800000 // 8192K
13524
+ ];
13525
+ _this.ramSizes = [0x00000000, // 0K
13526
+ 0x00002000, // 2K // Changed to 2000 to avoid problems
13527
+ 0x00002000, // 8K
13528
+ 0x00008000, // 32K
13529
+ 0x00020000, // 128K
13530
+ 0x00010000 // 64K
13531
+ ];
13532
+
13515
13533
_this.cartridge = cartridge;
13516
13534
_this.MBCRAMBanksEnabled = false; // MBC RAM Access Control.
13517
13535
_this.currentRAMBankPosition = -0xa000; // MBC Position Adder;
13518
13536
_this.currentMBCRAMBank = 0; // MBC Currently Indexed RAM Bank
13519
13537
_this.ROMBankEdge = Math.floor(cartridge.rom.length / 0x4000);
13520
- _this.numRAMBanks = 0; // How many RAM banks were actually allocated?
13521
13538
return _this;
13522
13539
}
13523
13540
13524
13541
_createClass(MBC, [{
13542
+ key: "setupROM",
13543
+ value: function setupROM() {
13544
+ this.romSize = this.romSizes[this.cartridge.romSizeType];
13545
+ console.log("ROM size 0x" + this.romSize.toString(16));
13546
+ }
13547
+ }, {
13525
13548
key: "setupRAM",
13526
13549
value: function setupRAM() {
13527
- // TODO: set banks amount on specific mbc type
13528
- // Setup the auxilliary/switchable RAM:
13529
- if (this.cartridge.hasMBC2) {
13530
- this.numRAMBanks = 1 / 16;
13531
- } else if (this.cartridge.hasMBC1 || this.cartridge.cRUMBLE || this.cartridge.hasMBC3 || this.cartridge.cHuC3) {
13532
- this.numRAMBanks = 4;
13533
- } else if (this.cartridge.hasMBC5) {
13534
- this.numRAMBanks = 16;
13535
- } else if (this.cartridge.hasSRAM) {
13536
- this.numRAMBanks = 1;
13537
- }
13538
-
13539
- this.allocatedRamBytes = this.numRAMBanks * 0x2000;
13540
-
13541
- console.log("Actual bytes of MBC RAM allocated: 0x" + this.allocatedRamBytes.toString(16));
13542
-
13543
- if (this.numRAMBanks > 0) {
13544
- this.RAM = util.getTypedArray(this.allocatedRamBytes, 0, "uint8"); // Switchable RAM (Used by games for more RAM) for the main memory range 0xA000 - 0xC000.
13545
- }
13550
+ this.ramSize = this.ramSizes[this.cartridge.ramSizeType];
13551
+ console.log("RAM size 0x" + this.ramSize.toString(16));
13552
+ this.RAM = util.getTypedArray(this.ramSize, 0, "uint8"); // Switchable RAM (Used by games for more RAM) for the main memory range 0xA000 - 0xC000.
13546
13553
}
13547
13554
}, {
13548
13555
key: "loadSRAM",
13549
13556
value: function loadSRAM(data) {
13550
- if (data.length !== this.allocatedRamBytes ) return;
13557
+ if (data.length !== this.ramSize ) return;
13551
13558
this.RAM = data.slice(0);
13552
13559
}
13553
13560
}, {
13554
13561
key: "getSRAM",
13555
13562
value: function getSRAM() {
13556
- return new Uint8Array(this.RAM.buffer.slice(0, this.allocatedRamBytes ));
13563
+ return new Uint8Array(this.RAM.buffer.slice(0, this.ramSize ));
13557
13564
}
13558
13565
}, {
13559
13566
key: "cutSRAMFromBatteryFileArray",
13560
13567
value: function cutSRAMFromBatteryFileArray(data) {
13561
- return new Uint8Array(data.buffer.slice(0, this.allocatedRamBytes ));
13568
+ return new Uint8Array(data.buffer.slice(0, this.ramSize ));
13562
13569
}
13563
13570
}, {
13564
13571
key: "saveState",
@@ -13817,7 +13824,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
13817
13824
}, {
13818
13825
key: "cutBatteryFileArray",
13819
13826
value: function cutBatteryFileArray(data) {
13820
- return new Uint32Array(data.buffer.slice(this.mbc.allocatedRamBytes , this.mbc.allocatedRamBytes + 4 * 12));
13827
+ return new Uint32Array(data.buffer.slice(this.mbc.ramSize , this.mbc.ramSize + 4 * 12));
13821
13828
}
13822
13829
}, {
13823
13830
key: "extract",
@@ -14135,15 +14142,6 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
14135
14142
this.cHuC3 = false; // Does the cartridge use HuC3 (Hudson Soft / modified MBC3)?
14136
14143
this.cHuC1 = false; // Does the cartridge use HuC1 (Hudson Soft / modified MBC1)?
14137
14144
this.hasRTC = false; // Does the cartridge have an RTC?
14138
-
14139
- this.ROMBanks = [
14140
- // 1 Bank = 16 KBytes = 256 Kbits
14141
- 2, 4, 8, 16, 32, 64, 128, 256, 512];
14142
- this.ROMBanks[0x52] = 72;
14143
- this.ROMBanks[0x53] = 80;
14144
- this.ROMBanks[0x54] = 96;
14145
-
14146
- this.RAMBanks = [0, 1, 2, 4, 16]; // Used to map the RAM banks to maximum size the MBC used can do.
14147
14145
}
14148
14146
14149
14147
_createClass(Cartridge, [{
@@ -14225,30 +14223,8 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
14225
14223
console.log("Cartridge Type Name: " + this.typeName);
14226
14224
}
14227
14225
14228
- this.romSize = this.rom.getByte(0x148);
14229
- this.ramSize = this.rom.getByte(0x149);
14230
-
14231
- // ROM and RAM banks
14232
- this.numROMBanks = this.ROMBanks[this.romSize];
14233
-
14234
- console.log(this.numROMBanks + " ROM banks.");
14235
-
14236
- switch (this.RAMBanks[this.ramSize]) {
14237
- case 0:
14238
- console.log("No RAM banking requested for allocation or MBC is of type 2.");
14239
- break;
14240
- case 2:
14241
- console.log("1 RAM bank requested for allocation.");
14242
- break;
14243
- case 3:
14244
- console.log("4 RAM banks requested for allocation.");
14245
- break;
14246
- case 4:
14247
- console.log("16 RAM banks requested for allocation.");
14248
- break;
14249
- default:
14250
- console.log("RAM bank amount requested is unknown, will use maximum allowed by specified MBC type.");
14251
- }
14226
+ this.romSizeType = this.rom.getByte(0x148);
14227
+ this.ramSizeType = this.rom.getByte(0x149);
14252
14228
14253
14229
// Check the GB/GBC mode byte:
14254
14230
if (!this.gameboy.usedBootROM) {
@@ -19109,6 +19085,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
19109
19085
this.init();
19110
19086
this.cartridgeSlot.insertCartridge(cartridge);
19111
19087
this.cartridgeSlot.readCartridge();
19088
+ this.cartridgeSlot.cartridge.mbc.setupROM();
19112
19089
19113
19090
if (this.cartridgeSlot.cartridge && this.cartridgeSlot.cartridge.mbc) {
19114
19091
this.cartridgeSlot.cartridge.mbc.on("ramWrite", function () {
@@ -21656,7 +21633,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
21656
21633
} else if (index < 0xa000) {
21657
21634
this.memoryReader[index] = this.cartridgeSlot.cartridge.useGBCMode ? this.VRAMCHRReadCGBCPU : this.VRAMCHRReadDMGCPU;
21658
21635
} else if (index >= 0xa000 && index < 0xc000) {
21659
- if (this.cartridgeSlot.cartridge.mbc.numRAMBanks === 1 / 16 && index < 0xa200 || this.cartridgeSlot.cartridge.mbc.numRAMBanks >= 1 ) {
21636
+ if (this.cartridgeSlot.cartridge.ramSize !== 0 ) {
21660
21637
if (this.cartridgeSlot.cartridge.hasMBC7) {
21661
21638
this.memoryReader[index] = this.memoryReadMBC7;
21662
21639
} else if (!this.cartridgeSlot.cartridge.hasMBC3) {
@@ -22193,7 +22170,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
22193
22170
} else if (index < 0xa000) {
22194
22171
this.memoryWriter[index] = this.cartridgeSlot.cartridge.useGBCMode ? this.VRAMGBCCHRMAPWrite : this.VRAMGBCHRMAPWrite;
22195
22172
} else if (index < 0xc000) {
22196
- if (this.cartridgeSlot.cartridge.mbc.numRAMBanks === 1 / 16 && index < 0xa200 || this.cartridgeSlot.cartridge.mbc.numRAMBanks >= 1 ) {
22173
+ if (this.cartridgeSlot.cartridge.ramSize !== 0 ) {
22197
22174
if (!this.cartridgeSlot.cartridge.hasMBC3) {
22198
22175
this.memoryWriter[index] = this.memoryWriteMBCRAM;
22199
22176
} else {
@@ -23799,30 +23776,38 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
23799
23776
return _context3.abrupt("return");
23800
23777
23801
23778
case 2:
23779
+ if (this.core.cartridgeSlot.cartridge.hasRTC) {
23780
+ _context3.next = 4;
23781
+ break;
23782
+ }
23783
+
23784
+ return _context3.abrupt("return");
23785
+
23786
+ case 4:
23802
23787
name = this.core.cartridgeSlot.cartridge.name;
23803
23788
23804
23789
if (rtc) {
23805
- _context3.next = 7 ;
23790
+ _context3.next = 9 ;
23806
23791
break;
23807
23792
}
23808
23793
23809
23794
rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.get();
23810
23795
23811
23796
if (rtc) {
23812
- _context3.next = 7 ;
23797
+ _context3.next = 9 ;
23813
23798
break;
23814
23799
}
23815
23800
23816
23801
return _context3.abrupt("return", false);
23817
23802
23818
- case 7 :
23819
- _context3.next = 9 ;
23803
+ case 9 :
23804
+ _context3.next = 11 ;
23820
23805
return this.storage.setRTC(name, rtc.buffer);
23821
23806
23822
- case 9 :
23807
+ case 11 :
23823
23808
this.emit("rtcSaved", { name: name, rtc: rtc });
23824
23809
23825
- case 10 :
23810
+ case 12 :
23826
23811
case "end":
23827
23812
return _context3.stop();
23828
23813
}
@@ -23868,7 +23853,8 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
23868
23853
}, {
23869
23854
key: "loadRTC",
23870
23855
value: function loadRTC(rtc) {
23871
- if (!this.core.cartridgeSlot.cartridge || !this.core.cartridgeSlot.cartridge.hasRTC) return;
23856
+ if (!this.core.cartridgeSlot.cartridge) return;
23857
+ if (!this.core.cartridgeSlot.cartridge.hasRTC) return;
23872
23858
var name = this.core.cartridgeSlot.cartridge.name;
23873
23859
23874
23860
if (!rtc) {
@@ -23886,9 +23872,10 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
23886
23872
if (!this.core.cartridgeSlot.cartridge) return;
23887
23873
23888
23874
var sram = this.core.cartridgeSlot.cartridge.mbc.getSRAM();
23889
- var rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.get();
23875
+ var rtc = null;
23876
+ if (this.core.cartridgeSlot.cartridge.mbc.rtc) rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.get();
23890
23877
23891
- return concatArrayBuffers(sram.buffer, rtc.buffer);
23878
+ return rtc ? concatArrayBuffers(sram.buffer, rtc.buffer) : sram.buffer ;
23892
23879
}
23893
23880
}, {
23894
23881
key: "loadBatteryFileArrayBuffer",
@@ -23904,23 +23891,30 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
23904
23891
}
23905
23892
23906
23893
sram = this.core.cartridgeSlot.cartridge.mbc.cutSRAMFromBatteryFileArray(data);
23907
- rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.cutBatteryFileArray(data);
23894
+ rtc = null;
23895
+
23896
+ if (this.core.cartridgeSlot.cartridge.hasRTC) rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.cutBatteryFileArray(data);
23908
23897
23909
23898
this.core.cartridgeSlot.cartridge.mbc.loadSRAM(sram);
23910
- this.core.cartridgeSlot.cartridge.mbc.rtc.load(rtc);
23899
+ if (rtc) this.core.cartridgeSlot.cartridge.mbc.rtc.load(rtc);
23911
23900
23912
- _context4.next = 7 ;
23901
+ _context4.next = 8 ;
23913
23902
return this.saveSRAM(sram);
23914
23903
23915
- case 7:
23916
- _context4.next = 9;
23904
+ case 8:
23905
+ if (!rtc) {
23906
+ _context4.next = 11;
23907
+ break;
23908
+ }
23909
+
23910
+ _context4.next = 11;
23917
23911
return this.saveRTC(rtc);
23918
23912
23919
- case 9 :
23913
+ case 11 :
23920
23914
23921
23915
this.restart();
23922
23916
23923
- case 10 :
23917
+ case 12 :
23924
23918
case "end":
23925
23919
return _context4.stop();
23926
23920
}
0 commit comments