From 3a96d2fa54c6e327bc95aaf7ee5400f40c55d6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szostak?= Date: Wed, 1 May 2024 12:48:15 +0200 Subject: [PATCH] Implemented Bits to Bytes converter and also extracting number from instructions to not pass whole instruction to function every time --- src/shardware.adb | 75 ++++++++++++++++++++++++++++++++++---------- src/shardware.ads | 13 +++++--- src/sillymachine.adb | 10 +++--- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/shardware.adb b/src/shardware.adb index 067ac27..4aef910 100644 --- a/src/shardware.adb +++ b/src/shardware.adb @@ -2,6 +2,16 @@ with Interfaces; use Interfaces; package body Shardware is + function TempBytes (Instruction : ByteArr) return ByteArr is + TheBytes : ByteArr (1 .. 4); + begin + TheBytes (1) := Instruction (7); + TheBytes (2) := Instruction (8); + TheBytes (3) := Instruction (9); + TheBytes (4) := Instruction (10); + return TheBytes; + end TempBytes; + function UnToSignedInt (Value : Unsigned_32) return Integer is Diff1 : Integer := 0; Diff2 : Integer := 0; @@ -15,7 +25,7 @@ package body Shardware is end if; end UnToSignedInt; - function BytesToBits (Instruction : ByteArr) return BitArr is + function BytesToBits (TheBytes : ByteArr) return BitArr is CurByte : Byte := 0; Buff : BitArr (1 .. 32); Counter : Integer := 0; @@ -23,8 +33,8 @@ package body Shardware is for i in 1 .. 32 loop Buff (i) := False; end loop; - for i in 7 .. 10 loop - CurByte := Instruction (i); + for i in 1 .. 4 loop + CurByte := TheBytes (i); for j in reverse 1 .. 8 loop if CurByte mod 2 > 0 then Buff (j + Counter) := True; @@ -38,29 +48,60 @@ package body Shardware is return Buff; end BytesToBits; - function My_ShiftU (Value : Byte; Amount : Integer) return Unsigned_32 is + function BitsToBytes (BinNumber : BitArr) return ByteArr is + ReverseBin : BitArr (1 .. 32); + I : Integer := 1; + CurByte : Byte := 0; + Buff : ByteArr (1 .. 4); + Counter : Integer := 24; + begin + for k in reverse 1 .. 32 loop + ReverseBin (I) := BinNumber (k); -- TODO: Unbloat this mess somehow? + I := I + 1; + end loop; + for i in 1 .. 4 loop + CurByte := 0; + for j in reverse 1 .. 8 loop + if ReverseBin (j + Counter) then + CurByte := CurByte + (2 ** (j - 1)); + end if; + end loop; + Counter := Counter - 8; + Buff (i) := CurByte; + end loop; + return Buff; + end BitsToBytes; + + function MyShiftU (Value : Byte; Amount : Integer) return Unsigned_32 is begin return Unsigned_32(Shift_Left(Unsigned_128(Value), Amount)); - end My_ShiftU; + end MyShiftU; - function Bytes_To_I (Instruction : ByteArr) return Integer is + function BytesToI (TheBytes : ByteArr) return Integer is begin - return UnToSignedInt(Bytes_To_U(Instruction)); - end Bytes_To_I; + return UnToSignedInt(BytesToU(TheBytes)); + end BytesToI; - function Bytes_To_F (Instruction : ByteArr) return Float is + function BytesToF (TheBytes : ByteArr) return Float is Result : Float := 0.0; + TheBits : BitArr (1 .. 32); + BitBuff : BitArr (1 .. 32); + Mantissa : Float := 0.0; begin - return Result; - end Bytes_To_F; + TheBits := BytesToBits(TheBytes); - function Bytes_To_U (Instruction : ByteArr) return Unsigned_32 is + --BitsToBytes(BitBuff); + + return Result; + end BytesToF; + + function BytesToU (TheBytes : ByteArr) return Unsigned_32 is begin - return Unsigned_32(My_ShiftU(Instruction (7), 24) - + My_ShiftU(Instruction (8), 16) - + My_ShiftU(Instruction (9), 8) - + Unsigned_32(Instruction (10))); - end Bytes_To_U; + return Unsigned_32(MyShiftU(TheBytes (1), 24) + + MyShiftU(TheBytes (2), 16) + + MyShiftU(TheBytes (3), 8) + + Unsigned_32(TheBytes (4))); + end BytesToU; procedure MovI (Register : in out Integer; Value : Integer) is begin diff --git a/src/shardware.ads b/src/shardware.ads index e2131ec..9a1d814 100644 --- a/src/shardware.ads +++ b/src/shardware.ads @@ -13,15 +13,18 @@ package Shardware is type RegArrF is array (Integer range <>) of Float; type RegArrU is array (Integer range <>) of Unsigned_32; + function TempBytes (Instruction : ByteArr) return ByteArr; + function UnToSignedInt (Value : Unsigned_32) return Integer; - function BytesToBits (Instruction : ByteArr) return BitArr; + function BytesToBits (TheBytes : ByteArr) return BitArr; + function BitsToBytes (BinNumber : BitArr) return ByteArr; - function My_ShiftU (Value : Byte; Amount : Integer) return Unsigned_32; + function MyShiftU (Value : Byte; Amount : Integer) return Unsigned_32; - function Bytes_To_I (Instruction : ByteArr) return Integer; - function Bytes_To_F (Instruction : ByteArr) return Float; - function Bytes_To_U (Instruction : ByteArr) return Unsigned_32; + function BytesToI (TheBytes : ByteArr) return Integer; + function BytesToF (TheBytes : ByteArr) return Float; + function BytesToU (TheBytes : ByteArr) return Unsigned_32; procedure MovI (Register : in out Integer; Value : Integer); procedure AddI (Register : in out Integer; Value : Integer); diff --git a/src/sillymachine.adb b/src/sillymachine.adb index a5859bf..518d22b 100644 --- a/src/sillymachine.adb +++ b/src/sillymachine.adb @@ -113,25 +113,25 @@ begin Instruction (i) := MemoryArr (PC + i); end loop; - case My_ShiftU(Instruction (0), 8) + Unsigned_32(Instruction (1)) is -- execute the instruction + case MyShiftU(Instruction (0), 8) + Unsigned_32(Instruction (1)) is -- execute the instruction when 0 => null; -- NOP when 1 => null; -- TODO: TRAP when 2 => MovU(RegisterU(Integer(Instruction (2))), - Bytes_To_U(Instruction)); -- Move Unsigned_32 to Register + BytesToU(TempBytes(Instruction))); -- Move Unsigned_32 to Register when 3 => MovU(RegisterU(Integer(Instruction (2))), -- Move Register to Register (Unsigned_32) RegisterU(Integer(Instruction (3)))); when 4 => null; -- TODO: Move Memory to Register when 5 => AddU(RegisterU(Integer(Instruction (2))), -- Add Unsigned_32 to Register (Only positive values for now) - Bytes_To_U(Instruction)); + BytesToU(TempBytes(Instruction))); when 6 => AddU(RegisterU(Integer(Instruction (2))), -- Add Register to Register (Unsigned_32) RegisterU(Integer(Instruction (3)))); when 7 => MovI(RegisterI(Integer(Instruction (2))), -- Move Int to Register - Bytes_To_I(Instruction)); + BytesToI(TempBytes(Instruction))); when 8 => MovI(RegisterI(Integer(Instruction (2))), -- Move Register to Register (Int) RegisterI(Integer(Instruction (3)))); when 9 => null; -- TODO: Move Memory to Register when 10 => AddI(RegisterI(Integer(Instruction (2))), -- Add Int to Register - Bytes_To_I(Instruction)); + BytesToI(TempBytes(Instruction))); when 11 => AddI(RegisterI(Integer(Instruction (2))), -- Add Register to Register (Int) RegisterI(Integer(Instruction (3)))); when 65535 => goto THE_END; -- exit opcode