From f929c99b049b431100e329821226963aca0a0693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szostak?= Date: Sun, 21 Apr 2024 16:05:52 +0200 Subject: [PATCH] Implemented moving integer (only positive for now) to register --- src/shardware.adb | 20 ++++++++++++++++++++ src/shardware.ads | 15 +++++++++++++++ src/sillymachine.adb | 31 +++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 src/shardware.adb create mode 100644 src/shardware.ads diff --git a/src/shardware.adb b/src/shardware.adb new file mode 100644 index 0000000..c34e089 --- /dev/null +++ b/src/shardware.adb @@ -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; diff --git a/src/shardware.ads b/src/shardware.ads new file mode 100644 index 0000000..13ad843 --- /dev/null +++ b/src/shardware.ads @@ -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; diff --git a/src/sillymachine.adb b/src/sillymachine.adb index c49b106..53df613 100644 --- a/src/sillymachine.adb +++ b/src/sillymachine.adb @@ -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;