Skip to content

Commit 53a8a8e

Browse files
committed
refactor rom & ram sizes
1 parent 36cb8d1 commit 53a8a8e

File tree

12 files changed

+370
-427
lines changed

12 files changed

+370
-427
lines changed

dist/jsgbc-core.js

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13512,53 +13512,60 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
1351213512

1351313513
var _this = _possibleConstructorReturn(this, (MBC.__proto__ || Object.getPrototypeOf(MBC)).call(this));
1351413514

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+
1351513533
_this.cartridge = cartridge;
1351613534
_this.MBCRAMBanksEnabled = false; // MBC RAM Access Control.
1351713535
_this.currentRAMBankPosition = -0xa000; // MBC Position Adder;
1351813536
_this.currentMBCRAMBank = 0; // MBC Currently Indexed RAM Bank
1351913537
_this.ROMBankEdge = Math.floor(cartridge.rom.length / 0x4000);
13520-
_this.numRAMBanks = 0; // How many RAM banks were actually allocated?
1352113538
return _this;
1352213539
}
1352313540

1352413541
_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+
}, {
1352513548
key: "setupRAM",
1352613549
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.
1354613553
}
1354713554
}, {
1354813555
key: "loadSRAM",
1354913556
value: function loadSRAM(data) {
13550-
if (data.length !== this.allocatedRamBytes) return;
13557+
if (data.length !== this.ramSize) return;
1355113558
this.RAM = data.slice(0);
1355213559
}
1355313560
}, {
1355413561
key: "getSRAM",
1355513562
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));
1355713564
}
1355813565
}, {
1355913566
key: "cutSRAMFromBatteryFileArray",
1356013567
value: function cutSRAMFromBatteryFileArray(data) {
13561-
return new Uint8Array(data.buffer.slice(0, this.allocatedRamBytes));
13568+
return new Uint8Array(data.buffer.slice(0, this.ramSize));
1356213569
}
1356313570
}, {
1356413571
key: "saveState",
@@ -13817,7 +13824,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
1381713824
}, {
1381813825
key: "cutBatteryFileArray",
1381913826
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));
1382113828
}
1382213829
}, {
1382313830
key: "extract",
@@ -14135,15 +14142,6 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
1413514142
this.cHuC3 = false; // Does the cartridge use HuC3 (Hudson Soft / modified MBC3)?
1413614143
this.cHuC1 = false; // Does the cartridge use HuC1 (Hudson Soft / modified MBC1)?
1413714144
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.
1414714145
}
1414814146

1414914147
_createClass(Cartridge, [{
@@ -14225,30 +14223,8 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
1422514223
console.log("Cartridge Type Name: " + this.typeName);
1422614224
}
1422714225

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);
1425214228

1425314229
// Check the GB/GBC mode byte:
1425414230
if (!this.gameboy.usedBootROM) {
@@ -19109,6 +19085,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
1910919085
this.init();
1911019086
this.cartridgeSlot.insertCartridge(cartridge);
1911119087
this.cartridgeSlot.readCartridge();
19088+
this.cartridgeSlot.cartridge.mbc.setupROM();
1911219089

1911319090
if (this.cartridgeSlot.cartridge && this.cartridgeSlot.cartridge.mbc) {
1911419091
this.cartridgeSlot.cartridge.mbc.on("ramWrite", function () {
@@ -21656,7 +21633,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2165621633
} else if (index < 0xa000) {
2165721634
this.memoryReader[index] = this.cartridgeSlot.cartridge.useGBCMode ? this.VRAMCHRReadCGBCPU : this.VRAMCHRReadDMGCPU;
2165821635
} 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) {
2166021637
if (this.cartridgeSlot.cartridge.hasMBC7) {
2166121638
this.memoryReader[index] = this.memoryReadMBC7;
2166221639
} else if (!this.cartridgeSlot.cartridge.hasMBC3) {
@@ -22193,7 +22170,7 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2219322170
} else if (index < 0xa000) {
2219422171
this.memoryWriter[index] = this.cartridgeSlot.cartridge.useGBCMode ? this.VRAMGBCCHRMAPWrite : this.VRAMGBCHRMAPWrite;
2219522172
} 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) {
2219722174
if (!this.cartridgeSlot.cartridge.hasMBC3) {
2219822175
this.memoryWriter[index] = this.memoryWriteMBCRAM;
2219922176
} else {
@@ -23799,30 +23776,38 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2379923776
return _context3.abrupt("return");
2380023777

2380123778
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:
2380223787
name = this.core.cartridgeSlot.cartridge.name;
2380323788

2380423789
if (rtc) {
23805-
_context3.next = 7;
23790+
_context3.next = 9;
2380623791
break;
2380723792
}
2380823793

2380923794
rtc = this.core.cartridgeSlot.cartridge.mbc.rtc.get();
2381023795

2381123796
if (rtc) {
23812-
_context3.next = 7;
23797+
_context3.next = 9;
2381323798
break;
2381423799
}
2381523800

2381623801
return _context3.abrupt("return", false);
2381723802

23818-
case 7:
23819-
_context3.next = 9;
23803+
case 9:
23804+
_context3.next = 11;
2382023805
return this.storage.setRTC(name, rtc.buffer);
2382123806

23822-
case 9:
23807+
case 11:
2382323808
this.emit("rtcSaved", { name: name, rtc: rtc });
2382423809

23825-
case 10:
23810+
case 12:
2382623811
case "end":
2382723812
return _context3.stop();
2382823813
}
@@ -23868,7 +23853,8 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2386823853
}, {
2386923854
key: "loadRTC",
2387023855
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;
2387223858
var name = this.core.cartridgeSlot.cartridge.name;
2387323859

2387423860
if (!rtc) {
@@ -23886,9 +23872,10 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2388623872
if (!this.core.cartridgeSlot.cartridge) return;
2388723873

2388823874
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();
2389023877

23891-
return concatArrayBuffers(sram.buffer, rtc.buffer);
23878+
return rtc ? concatArrayBuffers(sram.buffer, rtc.buffer) : sram.buffer;
2389223879
}
2389323880
}, {
2389423881
key: "loadBatteryFileArrayBuffer",
@@ -23904,23 +23891,30 @@ $__System.register('a', ['f', '13', '10', '12'], function (_export, _context5) {
2390423891
}
2390523892

2390623893
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);
2390823897

2390923898
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);
2391123900

23912-
_context4.next = 7;
23901+
_context4.next = 8;
2391323902
return this.saveSRAM(sram);
2391423903

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;
2391723911
return this.saveRTC(rtc);
2391823912

23919-
case 9:
23913+
case 11:
2392023914

2392123915
this.restart();
2392223916

23923-
case 10:
23917+
case 12:
2392423918
case "end":
2392523919
return _context4.stop();
2392623920
}

dist/jsgbc-core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jsgbc-core.min.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)