Implemented Bits to Bytes converter and also extracting number from instructions to not pass whole instruction to function every time

This commit is contained in:
Bartłomiej Szostak 2024-05-01 12:48:15 +02:00
parent 26528c5f96
commit 3a96d2fa54
No known key found for this signature in database
GPG Key ID: 562DACF230A18086
3 changed files with 71 additions and 27 deletions

View File

@ -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

View File

@ -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);

View File

@ -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