From 19a297f42a2f321ead4c03290d38a8ccdbf8dc18 Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 May 2023 00:23:41 -0500 Subject: test & fix indirection & predecrement --- src/vm/instruction.test.js | 128 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 1 deletion(-) (limited to 'src/vm/instruction.test.js') diff --git a/src/vm/instruction.test.js b/src/vm/instruction.test.js index 3535b76..fd402e4 100644 --- a/src/vm/instruction.test.js +++ b/src/vm/instruction.test.js @@ -1,7 +1,7 @@ 'use strict'; const { Core } = require('./core.js'); -const { DAT, MOV } = require('./instruction.js'); +const { DAT, MOV, ADD } = require('./instruction.js'); const CORESIZE = 8000; @@ -47,5 +47,131 @@ test('MOV correctly moves a B-field', () => { a: { mode: 'immediate', value: 0 }, b: { mode: 'immediate', value: 100 }, }); +}); + +test('MOV correctly handles indirection', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc-1] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 2 }, + }; + core.data[pc] = { + opcode: 'MOV', + a: { mode: 'immediate', value: 100 }, + b: { mode: 'indirect', value: -1 }, + }; + const ins = core.data[pc]; + expect(core.data[pc+1].opcode).toBe('DAT'); + expect(MOV(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc-1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 2 }, + }); + + expect(core.data[pc+1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 100 }, + }); + +}); + + + + +test('MOV correctly handles predecrement indirection', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'MOV', + a: { mode: 'immediate', value: 100 }, + b: { mode: 'predecrement', value: -1 }, + }; + const ins = core.data[pc]; + expect(core.data[pc+1].opcode).toBe('DAT'); + expect(MOV(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc-1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: -1 }, + }); + + expect(core.data[pc-2]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 0 }, + b: { mode: 'immediate', value: 100 }, + }); + +}); + + +test('ADD correctly adds two full instructions', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'ADD', + a: { mode: 'direct', value: 1 }, + b: { mode: 'direct', value: 2 }, + }; + core.data[pc+1] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }; + core.data[pc+2] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 4 }, + }; + const ins = core.data[pc]; + + expect(ADD(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc+1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }); + expect(core.data[pc+2]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 13 }, + b: { mode: 'immediate', value: 24 }, + }); +}); + + +test('ADD correctly adds B-field', () => { + const core = new Core(CORESIZE); + const pc = 20; + core.data[pc] = { + opcode: 'ADD', + a: { mode: 'immediate', value: 1 }, + b: { mode: 'direct', value: 2 }, + }; + core.data[pc+1] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }; + core.data[pc+2] = { + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 4 }, + }; + const ins = core.data[pc]; + + expect(ADD(core, pc, ins)).toEqual([pc+1]); + expect(core.data[pc+1]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 10 }, + b: { mode: 'immediate', value: 20 }, + }); + expect(core.data[pc+2]).toEqual({ + opcode: 'DAT', + a: { mode: 'immediate', value: 3 }, + b: { mode: 'immediate', value: 5 }, + }); }); -- cgit v1.2.1