Skip to content

Commit

Permalink
Merge pull request #9 from oswald3141/sizing_new_funcs
Browse files Browse the repository at this point in the history
Add resizing functions to sizing package
  • Loading branch information
kevinpt authored Jun 4, 2023
2 parents 7f344f4 + e8e5cb6 commit ff82545
Show file tree
Hide file tree
Showing 2 changed files with 443 additions and 0 deletions.
124 changes: 124 additions & 0 deletions rtl/extras/sizing.vhdl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
--# For convenenience, base-2 functions are also provided along with the array
--# sizing functions.
--#
--# Additionally the package provides functions for vectors resizing with
--# interfaces which show clearly, how resizing should be performed (extending
--# or truncating, most significant bits or least significant bits).
--#
--# This package does not supports VHDL-2008. Use sizing_2008 instead.
--#
--# EXAMPLE USAGE:
--# constant MAX_COUNT : natural := 1000;
--# constant COUNT_SIZE : natural := bit_size(MAX_COUNT);
Expand All @@ -56,6 +62,10 @@
--# -- counter will resize itself as MAX_COUNT is changed
--------------------------------------------------------------------

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

package sizing is
--## Compute the integer result of the function floor(log(n)).
--#
Expand Down Expand Up @@ -125,6 +135,58 @@ package sizing is
--# Returns:
--# Number of bits.
function signed_size(n : integer) return natural;

type resize_method is (truncate_LSBs, truncate_MSBs, extend_LSBs, extend_MSBs);
--## Resizizng function for std_ulogic_vector clearly stating how exactly the
--# resizing will be done.
--#
--# Args:
--# s: Vector to resize
--# new_size: New vector size
--# method: Resizizng method
--# extension: Bit used for extension
--# Returns:
--# Resized vector.
function change_size (s : std_ulogic_vector; new_size : positive; method : resize_method;
extension : std_ulogic := '0' ) return std_ulogic_vector;

--## Resizizng function for std_logic_vector clearly stating how exactly the
--# resizing will be done.
--#
--# Args:
--# s: Vector to resize
--# new_size: New vector size
--# method: Resizizng method
--# extension: Bit used for extension
--# Returns:
--# Resized vector.
function change_size (s : std_logic_vector; new_size : positive; method : resize_method;
extension : std_ulogic := '0' ) return std_logic_vector;

--## Resizizng function for unsigned number clearly stating how exactly the
--# resizing will be done. Extends numbers with zeros.
--#
--# Args:
--# s: Vector to resize
--# new_size: New vector size
--# method: Resizizng method
--# Returns:
--# Resized number.
function change_size (s : unsigned; new_size : positive; method : resize_method)
return unsigned;

--## Resizizng function for signed number clearly stating how exactly the
--# resizing will be done. Extends most significant bits with a sign bit,
--# and least significant bits with zeros.
--#
--# Args:
--# s: Vector to resize
--# new_size: New vector size
--# method: Resizizng method
--# Returns:
--# Resized number.
function change_size (s : signed; new_size : positive; method : resize_method)
return signed;

end package;

Expand Down Expand Up @@ -210,5 +272,67 @@ package body sizing is
return bit_size(-1 - n) + 1;
end if;
end function;


--## Resizizng function for std_ulogic_vector clearly stating how exactly the
--# resizing will be done.
function change_size (s : std_ulogic_vector; new_size : positive; method : resize_method;
extension : std_ulogic := '0' ) return std_ulogic_vector is

variable v : std_ulogic_vector(new_size-1 downto 0);
begin

case method is
when truncate_LSBs =>
return s( s'high downto (s'length - new_size) );
when truncate_MSBs =>
return s(new_size-1 downto 0);
when extend_LSBs =>
v(v'high downto new_size - s'length) := s;
v(new_size - s'length - 1 downto 0) := (others => extension);
return v;
when extend_MSBs =>
v(new_size-1 downto s'high+1) := (others => extension);
v(s'high downto 0) := s;
return v;
end case;

end function;

--## Resizizng function for std_logic_vector clearly stating how exactly the
--# resizing will be done.
function change_size (s : std_logic_vector; new_size : positive; method : resize_method;
extension : std_ulogic := '0' ) return std_logic_vector is
begin
return std_logic_vector(
change_size(std_ulogic_vector(s), new_size, method, extension));
end function;

--## Resizizng function for unsigned number clearly stating how exactly the
--# resizing will be done. Extends numbers with zeros.
function change_size (s : unsigned; new_size : positive; method : resize_method)
return unsigned is
begin
return unsigned(
change_size(std_ulogic_vector(s), new_size, method, '0'));
end function;

--## Resizizng function for signed number clearly stating how exactly the
--# resizing will be done. Extends most significant bits with a sign bit,
--# and least significant bits with zeros.
function change_size (s : signed; new_size : positive; method : resize_method)
return signed is
begin

case method is
when extend_MSBs =>
return signed(
change_size(std_ulogic_vector(s), new_size, method, s(s'high)));
when others =>
return signed(
change_size(std_ulogic_vector(s), new_size, method, '0'));
end case;

end function;

end package body;
Loading

0 comments on commit ff82545

Please sign in to comment.