sim: add framework for emulating system calls

This commit is contained in:
Mikael Pettersson
2020-07-08 13:25:41 +02:00
parent aab824fe87
commit dcfc247527
2 changed files with 62 additions and 0 deletions

View File

@@ -25,6 +25,9 @@
-module(sim_core).
-export([ run/6
, next_pc/2
, set_ac/3
, set_flag/2
, format_error/1
]).
@@ -65,6 +68,7 @@ run(Core, Mem) ->
insn_fetch(Core, Mem).
%% Sequential control flow: increment PC but stay in current section.
-spec next_pc(#core{}, sim_mem:mem()) -> {#core{}, sim_mem:mem(), ok | {error, {module(), term()}}}.
next_pc(#core{pc_offset = PCOffset} = Core, Mem) ->
PCOffset1 = (PCOffset + 1) band ((1 bsl 18) - 1),
insn_fetch(Core#core{pc_offset = PCOffset1}, Mem).
@@ -212,6 +216,7 @@ global_indirect_word(Core, Mem, IR, MB, I) ->
dispatch(Core, Mem, IR, EA) ->
%% Dispatch on the opcode (top 9 bits).
case IR bsr 4 of
8#104 -> sim_kernel:handle_JSYS(Core, Mem, IR, EA);
8#201 -> handle_MOVEI(Core, Mem, IR, EA);
_ ->
PC = (Core#core.pc_section bsl 18) bor Core#core.pc_offset,
@@ -283,6 +288,10 @@ set_ac(#core{acs = ACS} = Core, Nr, Val) ->
do_set_ac(ACS, Nr, Val) -> setelement(Nr + 1, ACS, Val).
-spec set_flag(#core{}, 0..12) -> #core{}.
set_flag(#core{flags = Flags} = Core, Flag) ->
Core#core{flags = Flags bor (1 bsl Flag)}.
%% Error Formatting ============================================================
-spec format_error(term()) -> io_lib:chars().

View File

@@ -0,0 +1,53 @@
%%% -*- erlang-indent-level: 2 -*-
%%%
%%% simulator for pdp10-elf
%%% Copyright (C) 2020 Mikael Pettersson
%%%
%%% This file is part of pdp10-tools.
%%%
%%% pdp10-tools is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% pdp10-tools is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with pdp10-tools. If not, see <http://www.gnu.org/licenses/>.
%%%
%%%=============================================================================
%%%
%%% Simulate traps to the kernel.
-module(sim_kernel).
-export([ handle_JSYS/4
]).
-include("sim_core.hrl").
-define(ENOSYS, 38).
%% JSYS ========================================================================
%%
%% System calls are made using JSYS with the system call number in the low 18
%% bits of EA, and the parameters in AC1 to AC7.
%% A successful call clears the Overflow flag and returns a value in AC1.
%% A failed call sets the Overflow flag and returns an error code in AC1.
%% System call numbers are from Linux' <asm-generic/unistd.h>, and error codes
%% are from Linux' <asm-generic/errno.h>.
-spec handle_JSYS(#core{}, sim_mem:mem(), IR :: word(), #ea{})
-> {#core{}, sim_mem:mem(), ok | {error, {module(), term()}}}.
handle_JSYS(Core, Mem, _IR, #ea{offset = Nr} = _EA) ->
case Nr of
_ -> return_errno(Core, Mem, ?ENOSYS)
end.
return_errno(Core0, Mem, Errno) ->
Core1 = sim_core:set_ac(Core0, 1, Errno),
Core2 = sim_core:set_flag(Core1, ?PDP10_PF_OVERFLOW),
sim_core:next_pc(Core2, Mem).