broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
367 lines (356 loc) • 17.8 kB
JavaScript
"use strict";
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Mapper2 = _interopRequireDefault(require("../lib/Mapper"));
var _InMemoryRegister5 = _interopRequireDefault(require("../lib/InMemoryRegister"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* It provides bank-switching for PRG and CHR ROM.
* CPU $6000-$7FFF: 8 KB PRG RAM bank (optional)
* CPU $8000-$BFFF: 16 KB PRG ROM bank, either switchable or fixed to the first bank
* CPU $C000-$FFFF: 16 KB PRG ROM bank, either fixed to the last bank or switchable
* PPU $0000-$0FFF: 4 KB switchable CHR ROM bank
* PPU $1000-$1FFF: 4 KB switchable CHR ROM bank
* Through writes to the MMC1 control register, it is possible for the program to
* swap the fixed and switchable PRG ROM banks or to set up 32 KB PRG bankswitching.
*/
var MMC1 = /*#__PURE__*/function (_Mapper) {
_inherits(MMC1, _Mapper);
var _super = _createSuper(MMC1);
function MMC1() {
_classCallCheck(this, MMC1);
return _super.apply(this, arguments);
}
_createClass(MMC1, [{
key: "chrRomPageSize",
value: function chrRomPageSize() {
return 4 * 1024;
}
}, {
key: "onLoad",
value: function onLoad() {
this.prgRam = new Uint8Array(0x2000);
this._prgRomBank0 = this.$getPrgPage(0);
this._prgRomBank1 = this.$getPrgPage(this.prgPages.length - 1);
this._chrRomBank0 = this.$getChrPage(0);
this._chrRomBank1 = this.$getChrPage(0);
this._state = {
load: new LoadRegister(),
control: new ControlRegister(),
chrBank0: new CHRBank0Register(),
chrBank1: new CHRBank1Register(),
prgBank: new PRGBankRegister()
};
}
}, {
key: "cpuRead",
value: function cpuRead(address) {
if (address >= 0x4020 && address <= 0x5fff) {
// Unused
return 0;
} else if (address >= 0x6000 && address <= 0x7fff) {
// CPU $6000-$7FFF: 8 KB PRG RAM bank (optional)
return this.prgRam[address - 0x6000];
} else if (address >= 0x8000 && address <= 0xbfff) {
// CPU $8000-$BFFF: 16 KB PRG ROM bank, either switchable or fixed to the first bank
return this._prgRomBank0[address - 0x8000];
} else if (address >= 0xc000 && address <= 0xffff) {
// CPU $C000-$FFFF: 16 KB PRG ROM bank, either fixed to the last bank or switchable
return this._prgRomBank1[address - 0xc000];
}
}
}, {
key: "cpuWrite",
value: function cpuWrite(address, input) {
if (address >= 0x6000 && address <= 0x7fff) {
// CPU $6000-$7FFF: 8 KB PRG RAM bank (optional)
this.prgRam[address - 0x6000] = input;
} else if (address >= 0x8000) {
// Load
var value = this._state.load.write(input);
if (value == null) return;
if (address >= 0x8000 && address < 0xa000) {
var _this$ppu$memory, _this$ppu$memory$chan;
// Control
this._state.control.setValue(value);
(_this$ppu$memory = this.ppu.memory) === null || _this$ppu$memory === void 0 ? void 0 : (_this$ppu$memory$chan = _this$ppu$memory.changeNameTableMirroringTo) === null || _this$ppu$memory$chan === void 0 ? void 0 : _this$ppu$memory$chan.call(_this$ppu$memory, this._state.control.mirroring);
} else if (address >= 0xa000 && address < 0xc000) {
// CHR bank 0
this._state.chrBank0.setValue(value);
} else if (address >= 0xc000 && address < 0xe000) {
// CHR bank 1
this._state.chrBank1.setValue(value);
} else {
// PRG bank
this._state.prgBank.setValue(value);
}
this._loadBanks();
}
}
}, {
key: "ppuRead",
value: function ppuRead(address) {
if (address >= 0x0000 && address <= 0x0fff) {
// PPU $0000-$0FFF: 4 KB switchable CHR ROM bank
return this._chrRomBank0[address];
} else if (address >= 0x1000 && address <= 0x1fff) {
// PPU $1000-$1FFF: 4 KB switchable CHR ROM bank
return this._chrRomBank1[address - 0x1000];
}
}
}, {
key: "ppuWrite",
value: function ppuWrite(address, value) {
if (!this.cartridge.header.usesChrRam) {
return; // (only CHR-RAM is writable)
}
// [!!!]
if (this.cartridge.isBroken) address += 3;
if (address >= 0x0000 && address <= 0x0fff) {
// PPU $0000-$0FFF: 4 KB switchable CHR RAM bank
this._chrRomBank0[address] = value;
} else if (address >= 0x1000 && address <= 0x1fff) {
// PPU $1000-$1FFF: 4 KB switchable CHR RAM bank
this._chrRomBank1[address - 0x1000] = value;
}
}
/** Returns a snapshot of the current state. */
}, {
key: "getSaveState",
value: function getSaveState() {
return _objectSpread(_objectSpread({}, _get(_getPrototypeOf(MMC1.prototype), "getSaveState", this).call(this)), {}, {
loadShiftRegister: this._state.load.shiftRegister,
loadWriteCounter: this._state.load.writeCounter,
control: this._state.control.value,
chrBank0: this._state.chrBank0.value,
chrBank1: this._state.chrBank1.value,
prgBank: this._state.prgBank.value
});
}
/** Restores state from a snapshot. */
}, {
key: "setSaveState",
value: function setSaveState(saveState) {
_get(_getPrototypeOf(MMC1.prototype), "setSaveState", this).call(this, saveState);
this._state.load.shiftRegister = saveState.loadShiftRegister;
this._state.load.writeCounter = saveState.loadWriteCounter;
this._state.control.setValue(saveState.control);
this._state.chrBank0.setValue(saveState.chrBank0);
this._state.chrBank1.setValue(saveState.chrBank1);
this._state.prgBank.setValue(saveState.prgBank);
this._loadBanks();
}
}, {
key: "_loadBanks",
value: function _loadBanks() {
var _this$_state = this._state,
control = _this$_state.control,
chrBank0 = _this$_state.chrBank0,
chrBank1 = _this$_state.chrBank1,
prgBank = _this$_state.prgBank;
if (control.isPrgRom32Kb) {
// 32KB PRG
var page = prgBank.prgRomBankId & 14;
this._prgRomBank0 = this.$getPrgPage(page);
this._prgRomBank1 = this.$getPrgPage(page + 1);
} else {
// 16KB PRG
this._prgRomBank0 = this.$getPrgPage(control.isFirstPrgAreaSwitchable ? prgBank.prgRomBankId : 0);
this._prgRomBank1 = this.$getPrgPage(control.isSecondPrgAreaSwitchable ? prgBank.prgRomBankId : this.prgPages.length - 1);
}
if (control.isChrRom8Kb) {
// 8KB CHR
var _page = chrBank0.value & 30;
this._chrRomBank0 = this.$getChrPage(_page);
this._chrRomBank1 = this.$getChrPage(_page + 1);
} else {
// 4KB CHR
this._chrRomBank0 = this.$getChrPage(chrBank0.value);
this._chrRomBank1 = this.$getChrPage(chrBank1.value);
}
}
}]);
return MMC1;
}(_Mapper2.default);
/**
* Load Register ($8000-$FFFF)
* MMC1 has a quirk, where it will take 5 writes to collect all the bits for the actual write
* that sets a bank. These 5 writes all are made to the Load register ($8000-$FFFF) that
* consists of a shift register. Each write to the load register shifts the rightmost bit onto
* said shift register, and on the 5th write, the whole shift register is evaluated.
*/
exports.default = MMC1;
var LoadRegister = /*#__PURE__*/function () {
function LoadRegister() {
_classCallCheck(this, LoadRegister);
this.shiftRegister = 0;
this.writeCounter = 0;
}
/** Writes a bit. Returns null 4 times, then the full value. */
_createClass(LoadRegister, [{
key: "write",
value: function write(value) {
if (value & 128) {
// reset
this.shiftRegister = 16;
this.writeCounter = 0;
return null;
} else {
// shift 4 times, writing on the 5th
var bit = value & 1;
this.shiftRegister = (this.shiftRegister >> 1 | bit << 4) & 31;
this.writeCounter++;
if (this.writeCounter === 5) {
var _value = this.shiftRegister;
this.shiftRegister = 0;
this.writeCounter = 0;
return _value;
}
return null;
}
}
}]);
return LoadRegister;
}();
/**
* Control ($8000-$9FFF)
* -----
* CPPMM
* |||||
* |||++- Mirroring (0: one-screen, lower bank; 1: one-screen, upper bank;
* ||| 2: vertical; 3: horizontal)
* |++--- PRG ROM bank mode (0, 1: switch 32 KB at $8000, ignoring low bit of bank number;
* | 2: fix first bank at $8000 and switch 16 KB bank at $C000;
* | 3: fix last bank at $C000 and switch 16 KB bank at $8000)
* +----- CHR ROM bank mode (0: switch 8 KB at a time; 1: switch two separate 4 KB banks)
*/
var ControlRegister = /*#__PURE__*/function (_InMemoryRegister) {
_inherits(ControlRegister, _InMemoryRegister);
var _super2 = _createSuper(ControlRegister);
function ControlRegister() {
_classCallCheck(this, ControlRegister);
return _super2.apply(this, arguments);
}
_createClass(ControlRegister, [{
key: "onLoad",
value: function onLoad() {
this.addField("mirroringId", 0, 2).addField("prgRomBankMode", 2, 2).addField("chrRomBankMode", 4);
}
/** Returns the mirroring type. */
}, {
key: "mirroring",
get: function get() {
switch (this.mirroringId) {
case 0:
return "ONE_SCREEN_LOWER_BANK";
case 1:
return "ONE_SCREEN_UPPER_BANK";
case 2:
return "VERTICAL";
case 3:
default:
return "HORIZONTAL";
}
}
/** Returns whether the PRG ROM area is 32KB or not (16KB otherwise). */
}, {
key: "isPrgRom32Kb",
get: function get() {
return this.prgRomBankMode <= 1;
}
/** Returns whether the CHR ROM area is 8KB or not (4KB otherwise). */
}, {
key: "isChrRom8Kb",
get: function get() {
return this.chrRomBankMode === 0;
}
/** Returns true if the first PRG ROM area ($8000-$BFFF) is switchable. Otherwise, it's fixed to first bank. */
}, {
key: "isFirstPrgAreaSwitchable",
get: function get() {
return this.prgRomBankMode !== 2;
}
/** Returns true if the second PRG ROM area ($C000-$FFFF) is switchable. Otherwise, it's fixed to last bank. */
}, {
key: "isSecondPrgAreaSwitchable",
get: function get() {
return this.prgRomBankMode !== 3;
}
}]);
return ControlRegister;
}(_InMemoryRegister5.default);
/**
* CHR bank 0 ($A000-$BFFF)
* -----
* CCCCC
* |||||
* +++++- Select 4 KB or 8 KB CHR bank at PPU $0000 (low bit ignored in 8 KB mode)
*/
var CHRBank0Register = /*#__PURE__*/function (_InMemoryRegister2) {
_inherits(CHRBank0Register, _InMemoryRegister2);
var _super3 = _createSuper(CHRBank0Register);
function CHRBank0Register() {
_classCallCheck(this, CHRBank0Register);
return _super3.apply(this, arguments);
}
return _createClass(CHRBank0Register);
}(_InMemoryRegister5.default);
/**
* CHR bank 1 ($C000-$DFFF)
* -----
* CCCCC
* |||||
* +++++- Select 4 KB CHR bank at PPU $1000 (ignored in 8 KB mode)
*/
var CHRBank1Register = /*#__PURE__*/function (_InMemoryRegister3) {
_inherits(CHRBank1Register, _InMemoryRegister3);
var _super4 = _createSuper(CHRBank1Register);
function CHRBank1Register() {
_classCallCheck(this, CHRBank1Register);
return _super4.apply(this, arguments);
}
return _createClass(CHRBank1Register);
}(_InMemoryRegister5.default);
/**
* PRG bank ($E000-$FFFF)
* -----
* RPPPP
* |||||
* |++++- Select 16 KB PRG ROM bank (low bit ignored in 32 KB mode)
* +----- PRG RAM chip enable (0: enabled; 1: disabled) (unused on this emulator)
*/
var PRGBankRegister = /*#__PURE__*/function (_InMemoryRegister4) {
_inherits(PRGBankRegister, _InMemoryRegister4);
var _super5 = _createSuper(PRGBankRegister);
function PRGBankRegister() {
_classCallCheck(this, PRGBankRegister);
return _super5.apply(this, arguments);
}
_createClass(PRGBankRegister, [{
key: "onLoad",
value: function onLoad() {
this.addField("prgRomBankId", 0, 4);
}
}]);
return PRGBankRegister;
}(_InMemoryRegister5.default);