147 lines
3.8 KiB
ObjectPascal
147 lines
3.8 KiB
ObjectPascal
program Emulator;
|
|
|
|
uses Crt;
|
|
|
|
const
|
|
IO = $ffff;
|
|
|
|
var
|
|
Halt: boolean; //Halt flag
|
|
Op: 0 .. $f; //Opcode
|
|
X, Y: 0 .. 3; //Register arguments
|
|
Addr, IP, RP: word; //Address argument and instruction and return pointers
|
|
R: array [0 .. 3] of byte; //General-purpose registers
|
|
Mem: array [0 .. $ffef] of byte; //Memory
|
|
Prog: file of byte; //Program file
|
|
Ch: ansichar; //Character for input and output
|
|
|
|
begin
|
|
|
|
//Initialise the halt flag and the pointers
|
|
Halt := false;
|
|
IP := 0;
|
|
RP := $fff0;
|
|
|
|
//Read a program file and check for errors
|
|
if ParamCount <> 1 then begin
|
|
writeln ('Usage: emulator program');
|
|
exit;
|
|
end;
|
|
{$i-}
|
|
assign (Prog, ParamStr (1));
|
|
reset (Prog);
|
|
{$i+}
|
|
if IOResult <> 0 then begin
|
|
writeln ('Usage: emulator program');
|
|
exit;
|
|
end;
|
|
repeat
|
|
read (Prog, Mem [IP]);
|
|
IP := IP + 1;
|
|
until (eof (Prog)) or (IP = $fff0);
|
|
|
|
//Reinitialise the instruction pointer
|
|
IP := 0;
|
|
|
|
//Begin the main loop
|
|
while (Halt = false) do begin
|
|
|
|
//Fetch the instruction and increment the instruction pointer
|
|
//Opcode
|
|
Op := Mem [IP] and $f0 shr 4;
|
|
//Register arguments
|
|
X := Mem [IP] and $c shr 2;
|
|
Y := Mem [IP] and 3;
|
|
IP := IP + 1;
|
|
if IP > $ffef then break;
|
|
//Address argument
|
|
if Op >= $a then begin
|
|
Addr := Mem [IP];
|
|
Addr := Addr shl 8;
|
|
IP := IP + 1;
|
|
if IP > $ffef then break;
|
|
Addr := Addr + Mem [IP];
|
|
IP := IP + 1;
|
|
if IP > $ffef then break;
|
|
end;
|
|
|
|
//Decode and execute the instruction
|
|
//Halt
|
|
if Op = 0 then Halt := true
|
|
//Ret
|
|
else if Op = 1 then begin
|
|
IP := Mem [RP];
|
|
RP := RP + 1;
|
|
if RP > $fff0 then break;
|
|
end
|
|
//Shl
|
|
else if Op = 2 then R [X] := R [X] shl 1
|
|
//Shr
|
|
else if Op = 3 then R [X] := R [X] shr 1
|
|
//Rol
|
|
else if Op = 4 then R [X] := RolByte (R [X])
|
|
//Ror
|
|
else if Op = 5 then R [X] := RorByte (R [X])
|
|
//Nand
|
|
else if Op = 6 then R [X] := not (R [X] and R [Y])
|
|
//And
|
|
else if Op = 7 then R [X] := R [X] and R [Y]
|
|
//Or
|
|
else if Op = 8 then R [X] := R [X] or R [Y]
|
|
//Xor
|
|
else if Op = 9 then R [X] := R [X] xor R [Y]
|
|
//Load
|
|
else if Op = $a then begin
|
|
//Input
|
|
if Addr = IO then begin
|
|
Ch := ReadKey;
|
|
write (Ch);
|
|
R [X] := byte (Ch);
|
|
end
|
|
//Regular load
|
|
else R [X] := Mem [Addr];
|
|
end
|
|
//Store
|
|
else if Op = $b then begin
|
|
//Output
|
|
if Addr = IO then begin
|
|
Ch := ansichar (R [X]);
|
|
write (Ch);
|
|
end
|
|
//Regular store
|
|
else Mem [Addr] := R [X];
|
|
end
|
|
//Breq
|
|
else if Op = $c then begin
|
|
if R [X] = R [Y] then IP := Addr;
|
|
end
|
|
//Brneq
|
|
else if Op = $d then begin
|
|
if R [X] <> R [Y] then IP := Addr;
|
|
end
|
|
//Cleq
|
|
else if Op = $e then begin
|
|
if R [X] = R [Y] then begin
|
|
RP := RP - 1;
|
|
if RP > $fff0 then break;
|
|
Mem [RP] := IP;
|
|
IP := Addr;
|
|
if IP > $ffef then break;
|
|
end;
|
|
end
|
|
//Clneq
|
|
else if Op = $f then begin
|
|
if R [X] <> R [Y] then begin
|
|
RP := RP - 1;
|
|
if RP > $fff0 then break;
|
|
Mem [RP] := IP;
|
|
IP := Addr;
|
|
if IP > $ffef then break;
|
|
end;
|
|
end;
|
|
|
|
//End the main loop
|
|
end
|
|
|
|
end.
|