UNPKG

nes-emu

Version:

A NES emulator

57 lines (56 loc) 1.85 kB
"use strict"; var _Stack = _interopRequireDefault(require("./Stack")); var _constants = _interopRequireDefault(require("../constants")); var _createTestContext = _interopRequireDefault(require("../helpers/createTestContext")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const should = require("chai").Should(); describe("Stack", () => { let cpu, memory, context, stack; beforeEach(() => { ({ cpu, memory, context } = (0, _createTestContext.default)()); stack = new _Stack.default().loadContext(context); cpu.sp.value = 0xff; }); it("can push and pop values", () => { stack.push(23); stack.push(24); stack.push(25); stack.pop().should.equal(25); stack.pop().should.equal(24); stack.pop().should.equal(23); }); it("can push and pop 16-bit values", () => { stack.push2Bytes(0xfe30); stack.push2Bytes(0xcd45); stack.push2Bytes(0x1234); stack.pop2Bytes().should.equal(0x1234); stack.pop2Bytes().should.equal(0xcd45); stack.pop2Bytes().should.equal(0xfe30); }); it("updates the memory and sp on push", () => { stack.push(23); memory.readAt(_constants.default.CPU_STACK_START_ADDRESS + 0xff).should.equal(23); cpu.sp.value.should.equal(0xfe); }); it("reads from the memory and updates sp on pop", () => { stack.push(23); memory.writeAt(_constants.default.CPU_STACK_START_ADDRESS + 0xff, 30); stack.pop().should.equal(30); cpu.sp.value.should.equal(0xff); }); it("ignores stack underflows", () => { memory.writeAt(0x0100, 32); stack.pop().should.equal(32); cpu.sp.value.should.equal(0); }); it("ignores stack overflows", () => { cpu.sp.value = 0; stack.push(32); memory.readAt(0x0100).should.equal(32); cpu.sp.value.should.equal(0xff); }); });