UNPKG

nes-emu

Version:

A NES emulator

199 lines (198 loc) 5.43 kB
"use strict"; var _2 = _interopRequireDefault(require(".")); var _helpers = require("../../helpers"); var _createTestContext = _interopRequireDefault(require("../../helpers/createTestContext")); var _lodash = _interopRequireDefault(require("lodash")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const should = require("chai").Should(); describe("instructions", () => { describe("data", () => { let cpu, memory, context; beforeEach(() => { ({ cpu, memory, context } = (0, _createTestContext.default)()); }); [{ instruction: "CLC", flag: "c" }, { instruction: "CLD", flag: "d" }, { instruction: "CLI", flag: "i" }, { instruction: "CLV", flag: "v" }].forEach(_ref => { let { instruction, flag } = _ref; describe(instruction, () => { it("clears the flag", () => { cpu.flags[flag] = true; _2.default[instruction].execute(context); cpu.flags[flag].should.equal(false); }); }); }); [{ instruction: "LDA", register: "a" }, { instruction: "LDX", register: "x" }, { instruction: "LDY", register: "y" }].forEach(_ref2 => { let { instruction, register } = _ref2; describe(instruction, () => { it("works with a positive value", () => { _2.default[instruction].execute(context, 5); cpu.registers[register].value.should.equal(5); cpu.flags.z.should.equal(false); cpu.flags.n.should.equal(false); }); it("works with a negative value", () => { const value = _helpers.Byte.toSignedByte(-5); _2.default[instruction].execute(context, value); cpu.registers[register].value.should.equal(value); cpu.flags.z.should.equal(false); cpu.flags.n.should.equal(true); }); it("works with a zero value", () => { _2.default[instruction].execute(context, 0); cpu.registers[register].value.should.equal(0); cpu.flags.z.should.equal(true); cpu.flags.n.should.equal(false); }); }); }); describe("PHA", () => { it("pushes the accumulator into the stack", () => { cpu.registers.a.value = 88; _2.default.PHA.execute(context); cpu.stack.pop().should.equal(88); }); }); describe("PHP", () => { it("pushes the flags into the stack", () => { cpu.flags.c = true; cpu.flags.v = true; _2.default.PHP.execute(context); cpu.stack.pop().should.equal(0b01110101); }); }); describe("PLA", () => { it("sets the A register with a value from the stack", () => { cpu.stack.push(76); _2.default.PLA.execute(context); cpu.registers.a.value.should.equal(76); }); }); describe("PLP", () => { it("sets the flags with a value from the stack", () => { cpu.stack.push(0b01000101); _2.default.PLP.execute(context); cpu.flags.should.include({ n: false, v: true, d: false, i: true, z: false, c: true }); }); }); [{ instruction: "SEC", flag: "c" }, { instruction: "SED", flag: "d" }, { instruction: "SEI", flag: "i" }].forEach(_ref3 => { let { instruction, flag } = _ref3; describe(instruction, () => { it("sets the flag", () => { cpu.flags[flag] = false; _2.default[instruction].execute(context); cpu.flags[flag].should.equal(true); }); }); }); [{ instruction: "STA", register: "a" }, { instruction: "STX", register: "x" }, { instruction: "STY", register: "y" }].forEach(_ref4 => { let { instruction, register } = _ref4; describe(instruction, () => { it("writes the byte into the memory address", () => { cpu.registers[register].value = 123; _2.default[instruction].execute(context, 0x1349); memory.readAt(0x1349).should.equal(123); }); }); }); [{ instruction: "TAX", sourceRegister: "registers.a", targetRegister: "registers.x" }, { instruction: "TAY", sourceRegister: "registers.a", targetRegister: "registers.y" }, { instruction: "TSX", sourceRegister: "sp", targetRegister: "registers.x" }, { instruction: "TXA", sourceRegister: "registers.x", targetRegister: "registers.a" }, { instruction: "TXS", sourceRegister: "registers.x", targetRegister: "sp" }, { instruction: "TYA", sourceRegister: "registers.y", targetRegister: "registers.a" }].forEach(_ref5 => { let { instruction, sourceRegister, targetRegister } = _ref5; describe(instruction, () => { it("transfers the content", () => { _lodash.default.set(cpu, "".concat(sourceRegister, ".value"), 123); _2.default[instruction].execute(context); _lodash.default.get(cpu, "".concat(targetRegister, ".value")).should.equal(123); }); }); }); }); });