broken-neees
Version:
A really broken NEEES emulator that introduces glitches and random bugs on purpose!
110 lines (108 loc) • 5.34 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Register = require("./Register");
var _FlagsRegister = _interopRequireDefault(require("./FlagsRegister"));
var _Stack = _interopRequireDefault(require("./Stack"));
var _instructions = _interopRequireDefault(require("./instructions"));
var _addressingModes = _interopRequireDefault(require("./addressingModes"));
var _defineOperations = _interopRequireDefault(require("../lib/cpu/defineOperations"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
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); }
var CPU = /*#__PURE__*/function () {
function CPU(cpuMemory) {
_classCallCheck(this, CPU);
this.memory = cpuMemory;
this.cycle = 0;
this.extraCycles = 0;
this.a = new _Register.Register8Bit();
this.x = new _Register.Register8Bit();
this.y = new _Register.Register8Bit();
this.sp = new _Register.Register8Bit();
this.pc = new _Register.Register16Bit();
this.flags = new _FlagsRegister.default();
this.stack = new _Stack.default(this.memory, this.sp);
this.operations = (0, _defineOperations.default)(_instructions.default, _addressingModes.default);
}
_createClass(CPU, [{
key: "step",
value: function step() {
var originalPC = this.pc.getValue();
var operation = this._fetchOperation();
var input = this._fetchInput(operation);
var argument = this._fetchArgument(operation, input);
if (this.logger != null) {
this.logger(this, originalPC, operation, input, argument);
}
operation.instruction.run(this, argument);
return this._addCycles(operation);
}
}, {
key: "interrupt",
value: function interrupt(_interrupt, withBFlag) {
if (_interrupt.id === "IRQ" && this.flags.i) return 0;
this.stack.push16(this.pc.getValue());
this.stack.push(this.flags.getValue() | (withBFlag && 16));
this.cycle += 7;
this.flags.i = true;
this.pc.setValue(this.memory.read16(_interrupt.vector));
return 7;
}
}, {
key: "_fetchOperation",
value: function _fetchOperation() {
var opcode = this.memory.read(this.pc.getValue());
var operation = this.operations[opcode];
if (operation == null) throw new Error("Invalid opcode: 0x" + opcode.toString(16));
this.pc.increment();
return operation;
}
}, {
key: "_fetchInput",
value: function _fetchInput(operation) {
var input = null;
if (operation.addressingMode.inputSize === 1) {
input = this.memory.read(this.pc.getValue());
this.pc.increment();
} else if (operation.addressingMode.inputSize === 2) {
input = this.memory.read16(this.pc.getValue());
this.pc.increment();
this.pc.increment();
}
return input;
}
}, {
key: "_fetchArgument",
value: function _fetchArgument(operation, input) {
var arg = operation.instruction.argument === "value" ? operation.addressingMode.getValue(this, input, operation.hasPageCrossPenalty) : operation.addressingMode.getAddress(this, input, operation.hasPageCrossPenalty);
// [!!!]
if (!this.unbroken) {
if (Math.random() < 0.0005 && operation.id === 0x69 /* adc immediate */) return arg + 1;
}
return arg;
}
}, {
key: "_addCycles",
value: function _addCycles(operation) {
var cycles;
if (this.unbroken) {
cycles = operation.cycles + this.extraCycles;
} else {
// [!!!]
cycles = Math.floor(Math.random() * 10);
}
this.cycle += cycles;
this.extraCycles = 0;
return cycles;
}
}]);
return CPU;
}();
exports.default = CPU;