nes-emu
Version:
A NES emulator
48 lines (42 loc) • 1.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _constants = _interopRequireDefault(require("../constants"));
var _helpers = require("../helpers");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/** An in-memory stack, controlled by the CPU. */
class Stack {
constructor() {
_helpers.WithContext.apply(this);
}
/** Pushes a `value` into the stack. */
push(value) {
this.context.memoryBus.cpu.writeAt(this.currentAddress, value);
this.context.cpu.sp.decrement();
}
/** Pulls a value from the stack. */
pop() {
this.context.cpu.sp.increment();
return this.context.cpu.memory.readAt(this.currentAddress);
}
/** Pushes a 16-bit `value` into the stack. */
push2Bytes(value) {
const low = _helpers.Byte.lowPartOf(value);
const high = _helpers.Byte.highPartOf(value);
this.push(high);
this.push(low);
}
/** Pulls a 16-bit `value` from the stack. */
pop2Bytes() {
const low = this.pop();
const high = this.pop();
return _helpers.Byte.to16Bit(high, low);
}
/** Returns the current address of the stack. */
get currentAddress() {
return _constants.default.CPU_STACK_START_ADDRESS + this.context.cpu.sp.value;
}
}
exports.default = Stack;
;