Implemented moving integer (only positive for now) to register

This commit is contained in:
Bartłomiej Szostak 2024-04-21 16:05:52 +02:00
parent 90b495d03a
commit f929c99b04
No known key found for this signature in database
GPG Key ID: 562DACF230A18086
3 changed files with 56 additions and 10 deletions

20
src/shardware.adb Normal file
View File

@ -0,0 +1,20 @@
with Interfaces; use Interfaces;
package body Shardware is
function My_Shift (Value : Byte; Amount : Integer) return Integer is
begin
if Value > 127 and Amount > 23 then
return Integer(Shift_Left(Unsigned_128(Value), 23));
else
return Integer(Shift_Left(Unsigned_128(Value), Amount));
end if;
end My_Shift;
procedure Mov (Register : in out Integer; Value : Integer) is
begin
Register := Value;
end Mov;
end Shardware;

15
src/shardware.ads Normal file
View File

@ -0,0 +1,15 @@
with Ada.Sequential_IO;
package Shardware is
type Byte is mod 256;
package Bin_IO is new Ada.Sequential_IO (Byte);
type ByteArr is array (Integer range <>) of Byte;
type RegArr is array (Integer range <>) of Integer;
function My_Shift (Value : Byte; Amount : Integer) return Integer;
procedure Mov (Register : in out Integer; Value : Integer);
end Shardware;

View File

@ -1,13 +1,10 @@
with Shardware; use Shardware;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Sequential_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Directories;
with Interfaces; use Interfaces;
procedure sillymachine is
type Byte is mod 256;
package Bin_IO is new Ada.Sequential_IO (Byte);
type ByteArr is array (Integer range <>) of Byte;
F : Bin_IO.File_Type;
FileName : String (1 .. 255) := (others => ascii.nul);
@ -23,6 +20,8 @@ procedure sillymachine is
PC : Integer := 0; -- Program Counter
Instruction : ByteArr (0 .. 15);
Register : RegArr (0 .. 27);
begin
ValidHeader (0) := 6;
@ -57,7 +56,7 @@ begin
Bin_IO.Read(F, Value);
if ValidHeader (I) = Value then
Value := Value; -- useless line so compiler does not complain
null;
else
New_Line(1);
Put_Line("Error: Please use a valid sm binary file");
@ -84,6 +83,10 @@ begin
declare
MemoryArr : ByteArr (0 .. MemorySize);
begin
for i in 0 .. 27 loop -- zero initialise registers
Register (i) := 0;
end loop;
for i in 0 .. MemorySize loop -- zero initialise memory
MemoryArr (i) := 0;
end loop;
@ -103,13 +106,21 @@ begin
Instruction (i) := MemoryArr (PC + i);
end loop;
case Shift_Left(Unsigned_128(Instruction (0)), 8) + Unsigned_128(Instruction (1)) is -- execute the instruction
when 0 => Put_Line("-- japko --");
when 1 => Put_Line("-- banan --");
case My_Shift(Instruction (0), 8) + Integer(Instruction (1)) is -- execute the instruction
when 0 => Mov(Register(Integer(Instruction (2))), -- Move Int to Register (Only positive values for now)
Integer(My_Shift(Instruction (7), 24)
+ My_Shift(Instruction (8), 16)
+ My_Shift(Instruction (9), 8)
+ Integer(Instruction (10)))
);
when 1 => Mov(Register(Integer(Instruction (2))), -- Move Register to Register
Register(Integer(Instruction (3))));
when 65535 => goto THE_END; -- exit opcode
when others => New_Line(1);
when others => null;
end case;
Put_Line(Integer'Image(Register(0)));
PC := PC + 16; -- increment program counter to next instruction
end loop;