From 2ab18ce9dbdf3a089d452897ea39f4c4429dff0e Mon Sep 17 00:00:00 2001 From: Mikael Pettersson Date: Mon, 3 Aug 2020 19:04:38 +0200 Subject: [PATCH] sim: sim_arithmetic: handle AOJ{,L,E,LE,A,GE,N,G}, add unit tests --- erlang/apps/sim/src/sim_arithmetic.erl | 88 ++++++++++ erlang/apps/sim/src/sim_core.erl | 8 + erlang/apps/sim/test/sim_arithmetic_tests.erl | 157 ++++++++++++++++++ 3 files changed, 253 insertions(+) diff --git a/erlang/apps/sim/src/sim_arithmetic.erl b/erlang/apps/sim/src/sim_arithmetic.erl index 8070cd9..ea6abc7 100644 --- a/erlang/apps/sim/src/sim_arithmetic.erl +++ b/erlang/apps/sim/src/sim_arithmetic.erl @@ -26,6 +26,14 @@ -export([ handle_AOBJN/4 , handle_AOBJP/4 + , handle_AOJ/4 + , handle_AOJA/4 + , handle_AOJE/4 + , handle_AOJG/4 + , handle_AOJGE/4 + , handle_AOJL/4 + , handle_AOJLE/4 + , handle_AOJN/4 , handle_CAIE/4 , handle_CAIG/4 , handle_CAIGE/4 @@ -388,6 +396,72 @@ handle_SKIPG(Core, Mem, IR, EA) -> fun(Core1, Mem1) -> ?FUNCTION_NAME(Core1, Mem1, IR, EA) end) end. +%% AOJ - Add One to AC and Jump if Condition Satisfied + +-spec handle_AOJ(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJ(Core, Mem, IR, _EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + sim_core:next_pc(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem). + +-spec handle_AOJL(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJL(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_L(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + +-spec handle_AOJE(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJE(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_E(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + +-spec handle_AOJLE(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJLE(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_LE(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + +-spec handle_AOJA(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJA(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, EA). + +-spec handle_AOJGE(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJGE(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_GE(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + +-spec handle_AOJN(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJN(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_N(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + +-spec handle_AOJG(#core{}, sim_mem:mem(), IR :: word(), #ea{}) + -> {#core{}, sim_mem:mem(), {ok, integer()} | {error, {module(), term()}}}. +handle_AOJG(Core, Mem, IR, EA) -> + AC = IR band 8#17, + CA = sim_core:get_ac(Core, AC), + {Word, Flags} = add1(CA), + jump_if_G(sim_core:set_flags(sim_core:set_ac(Core, AC, Word), Flags), Mem, Word, EA). + %% Miscellaneous =============================================================== jump_if_E(Core, Mem, X, EA) -> @@ -453,6 +527,20 @@ skip_if_N(Core, Mem, X, Y) -> false -> sim_core:next_pc(Core, Mem) end. +add1(Word) -> + Add1 = (Word + 1) band ((1 bsl 36) - 1), + Flags = + case Add1 of + 8#400000000000 -> % (2^35 - 1) + 1 + (1 bsl ?PDP10_PF_TRAP_1) bor + (1 bsl ?PDP10_PF_OVERFLOW) bor + (1 bsl ?PDP10_PF_CARRY_1); + 0 -> % -1 + 1 + (1 bsl ?PDP10_PF_CARRY_0) bor (1 bsl ?PDP10_PF_CARRY_1); + _ -> 0 + end, + {Add1, Flags}. + %% Sign-extend a uint36_t() to the full width of its representation type. -spec sext36(uint36_t()) -> integer(). sext36(X) -> diff --git a/erlang/apps/sim/src/sim_core.erl b/erlang/apps/sim/src/sim_core.erl index e6d1ca2..6dec169 100644 --- a/erlang/apps/sim/src/sim_core.erl +++ b/erlang/apps/sim/src/sim_core.erl @@ -320,6 +320,14 @@ dispatch(Core, Mem, IR, EA) -> 8#335 -> sim_arithmetic:handle_SKIPGE(Core, Mem, IR, EA); 8#336 -> sim_arithmetic:handle_SKIPN(Core, Mem, IR, EA); 8#337 -> sim_arithmetic:handle_SKIPG(Core, Mem, IR, EA); + 8#340 -> sim_arithmetic:handle_AOJ(Core, Mem, IR, EA); + 8#341 -> sim_arithmetic:handle_AOJL(Core, Mem, IR, EA); + 8#342 -> sim_arithmetic:handle_AOJE(Core, Mem, IR, EA); + 8#343 -> sim_arithmetic:handle_AOJLE(Core, Mem, IR, EA); + 8#344 -> sim_arithmetic:handle_AOJA(Core, Mem, IR, EA); + 8#345 -> sim_arithmetic:handle_AOJGE(Core, Mem, IR, EA); + 8#346 -> sim_arithmetic:handle_AOJN(Core, Mem, IR, EA); + 8#347 -> sim_arithmetic:handle_AOJG(Core, Mem, IR, EA); 8#400 -> sim_boolean:handle_SETZ(Core, Mem, IR, EA); 8#401 -> sim_boolean:handle_SETZ(Core, Mem, IR, EA); % SETZI = SETZ 8#402 -> sim_boolean:handle_SETZM(Core, Mem, IR, EA); diff --git a/erlang/apps/sim/test/sim_arithmetic_tests.erl b/erlang/apps/sim/test/sim_arithmetic_tests.erl index fab7802..791abf1 100644 --- a/erlang/apps/sim/test/sim_arithmetic_tests.erl +++ b/erlang/apps/sim/test/sim_arithmetic_tests.erl @@ -28,6 +28,10 @@ -include_lib("eunit/include/eunit.hrl"). -define(DEFAULT_FLAGS, (1 bsl ?PDP10_PF_USER)). +-define(CARRY_FLAGS, % for AOJ/AOS/SOJ/SOS when carry 0 and 1 get set + (?DEFAULT_FLAGS bor + (1 bsl ?PDP10_PF_CARRY_0) bor + (1 bsl ?PDP10_PF_CARRY_1))). -define(LOW18(X), ((X) band ((1 bsl 18) - 1))). -define(LOW36(X), ((X) band ((1 bsl 36) - 1))). @@ -83,6 +87,14 @@ -define(OP_SKIPGE, 8#335). -define(OP_SKIPN, 8#336). -define(OP_SKIPG, 8#337). +-define(OP_AOJ, 8#340). +-define(OP_AOJL, 8#341). +-define(OP_AOJE, 8#342). +-define(OP_AOJLE, 8#343). +-define(OP_AOJA, 8#344). +-define(OP_AOJGE, 8#345). +-define(OP_AOJN, 8#346). +-define(OP_AOJG, 8#347). %% 2.6.1 Add One to Both Halves of AC and Jump ================================= @@ -674,6 +686,151 @@ skipg_test() -> expect(Prog2, [], {1, 8#101}, ?DEFAULT_FLAGS, % no skip [{?AC(1), ?LOW36(-2)}]). % AC(1) = -2 +%% AOJ - Add One to AC and Jump if Condition Satisfied + +aoj_test() -> + Prog = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJ, 1, 0, 0, 0)} % 1,,101/ AOJ 1, + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + ], + expect(Prog, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), 3}]). % AC(1) = 3 + +aojl_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 2)} % 1,,100/ MOVNI 1,2 + , {1, 8#101, ?INSN(?OP_AOJL, 1, 0, 0, 8#103)} % 1,,101/ AOJL 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), ?LOW36(-1)}]), % AC(1) = -1 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJL, 1, 0, 0, 8#103)} % 1,,101/ AOJL 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), 3}]). % AC(1) = 3 + +aoje_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 1)} % 1,,100/ MOVNI 1,1 + , {1, 8#101, ?INSN(?OP_AOJE, 1, 0, 0, 8#103)} % 1,,101/ AOJE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?CARRY_FLAGS, % jump, carry 0 and 1 + [ {?AC(1), 0}]), % AC(1) = 0 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJE, 1, 0, 0, 8#103)} % 1,,101/ AOJE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), 3}]). % AC(1) = 3 + +aojle_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 2)} % 1,,100/ MOVNI 1,2 + , {1, 8#101, ?INSN(?OP_AOJLE, 1, 0, 0, 8#103)} % 1,,101/ AOJLE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), ?LOW36(-1)}]), % AC(1) = -1 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 1)} % 1,,100/ MOVNI 1,1 + , {1, 8#101, ?INSN(?OP_AOJLE, 1, 0, 0, 8#103)} % 1,,101/ AOJLE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#103}, ?CARRY_FLAGS, % jump, carry 0 and 1 + [ {?AC(1), 0}]), % AC(1) = 0 + Prog3 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJLE, 1, 0, 0, 8#103)} % 1,,101/ AOJLE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog3, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), 3}]). % AC(1) = 3 + +aoja_test() -> + Prog = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJA, 1, 0, 0, 8#103)} % 1,,101/ AOJA 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), 3}]). % AC(1) = 3 + +aojge_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJGE, 1, 0, 0, 8#103)} % 1,,101/ AOJGE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), 3}]), % AC(1) = 3 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 1)} % 1,,100/ MOVNI 1,1 + , {1, 8#101, ?INSN(?OP_AOJGE, 1, 0, 0, 8#103)} % 1,,101/ AOJGE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#103}, ?CARRY_FLAGS, % jump, carry 0 and 1 + [ {?AC(1), 0}]), % AC(1) = 0 + Prog3 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 2)} % 1,,100/ MOVNI 1,2 + , {1, 8#101, ?INSN(?OP_AOJGE, 1, 0, 0, 8#103)} % 1,,101/ AOJGE 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog3, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), ?LOW36(-1)}]). % AC(1) = -1 + +aojn_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJN, 1, 0, 0, 8#103)} % 1,,101/ AOJN 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), 3}]), % AC(1) = 3 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 1)} % 1,,100/ MOVNI 1,1 + , {1, 8#101, ?INSN(?OP_AOJN, 1, 0, 0, 8#103)} % 1,,101/ AOJN 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#102}, ?CARRY_FLAGS, % no jump, carry 0 and 1 + [ {?AC(1), 0}]). % AC(1) = 0 + +aojg_test() -> + Prog1 = + [ {1, 8#100, ?INSN(?OP_MOVEI, 1, 0, 0, 2)} % 1,,100/ MOVEI 1,2 + , {1, 8#101, ?INSN(?OP_AOJG, 1, 0, 0, 8#103)} % 1,,101/ AOJG 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog1, [], {1, 8#103}, ?DEFAULT_FLAGS, % jump + [ {?AC(1), 3}]), % AC(1) = 3 + Prog2 = + [ {1, 8#100, ?INSN(?OP_MOVNI, 1, 0, 0, 2)} % 1,,100/ MOVNI 1,2 + , {1, 8#101, ?INSN(?OP_AOJG, 1, 0, 0, 8#103)} % 1,,101/ AOJG 1,103 + , {1, 8#102, ?INSN_INVALID} % 1,,102/ + , {1, 8#103, ?INSN_INVALID} % 1,,103/ + ], + expect(Prog2, [], {1, 8#102}, ?DEFAULT_FLAGS, % no jump + [ {?AC(1), ?LOW36(-1)}]). % AC(1) = -1 + %% Common code to run short sequences ========================================== expect(Prog, ACs, ExpectedPC, ExpectedFlags, ExpectedEs) ->