Skip to content

Commit

Permalink
Move width calculation function to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
Sławomir Siluk committed Nov 15, 2020
1 parent 9bbb6d1 commit 1678d0c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions vunit/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ def add_vhdl_builtins(self, external=None):
"dictionary",
"run",
"path",
"helpers",
):
self._add_files(VHDL_PATH / path / "src" / "*.vhd")

Expand Down
22 changes: 22 additions & 0 deletions vunit/vhdl/helpers/src/helpers_pkg.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this file,
-- You can obtain one at http://mozilla.org/MPL/2.0/.
--
-- Copyright (c) 2014-2020, Lars Asplund lars.anders.asplund@gmail.com

package helpers_pkg is
function calc_width(d : positive) return natural;
end package;

package body helpers_pkg is
function calc_width(d : positive) return natural is
variable i : natural;
variable v : natural := d-1;
begin
while v > 0 loop
v := v/2;
i := i + 1;
end loop;
return i;
end;
end package body;
21 changes: 5 additions & 16 deletions vunit/vhdl/verification_components/src/avalon_stream_pkg.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ieee.std_logic_1164.all;
use work.logger_pkg.all;
use work.stream_master_pkg.all;
use work.stream_slave_pkg.all;
use work.helpers_pkg.all;
context work.com_context;
context work.data_types_context;

Expand Down Expand Up @@ -45,7 +46,6 @@ package avalon_stream_pkg is
channel_length : natural := 0) return avalon_sink_t;
impure function data_length(source : avalon_source_t) return natural;
impure function data_length(source : avalon_sink_t) return natural;
function calc_empty_length(data_width : positive) return natural;
impure function empty_length(source : avalon_source_t) return natural;
impure function empty_length(source : avalon_sink_t) return natural;
impure function channel_length(source : avalon_source_t) return natural;
Expand Down Expand Up @@ -147,27 +147,16 @@ package body avalon_stream_pkg is
return source.p_data_length;
end;

function calc_empty_length(data_width : positive) return natural is
variable v : natural;
variable i : natural;
begin
assert data_width mod 8 = 0;
v := data_width/8-1;
while v > 0 loop
v := v/2;
i := i + 1;
end loop;
return i;
end;

impure function empty_length(source : avalon_source_t) return natural is
begin
return calc_empty_length(source.p_data_length);
assert source.p_data_length mod 8 = 0;
return calc_width(source.p_data_length/8);
end;

impure function empty_length(source : avalon_sink_t) return natural is
begin
return calc_empty_length(source.p_data_length);
assert source.p_data_length mod 8 = 0;
return calc_width(source.p_data_length/8);
end;

impure function channel_length(source : avalon_source_t) return natural is
Expand Down
13 changes: 7 additions & 6 deletions vunit/vhdl/verification_components/test/tb_avalon_stream_pkg.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ context work.data_types_context;
use work.avalon_stream_pkg.all;
use work.stream_master_pkg.all;
use work.stream_slave_pkg.all;
use work.helpers_pkg.all;

entity tb_avalon_stream_pkg is
generic (runner_cfg : string);
Expand Down Expand Up @@ -78,12 +79,12 @@ begin
end loop;

elsif run("calculate empty length") then
check_equal(calc_empty_length(8), 0);
check_equal(calc_empty_length(16), 1);
check_equal(calc_empty_length(24), 2);
check_equal(calc_empty_length(32), 2);
check_equal(calc_empty_length(40), 3);
check_equal(calc_empty_length(256), 5);
check_equal(calc_width(8/8), 0);
check_equal(calc_width(16/8), 1);
check_equal(calc_width(24/8), 2);
check_equal(calc_width(32/8), 2);
check_equal(calc_width(40/8), 3);
check_equal(calc_width(256/8), 5);
end if;
wait until rising_edge(clk);
test_runner_cleanup(runner);
Expand Down

0 comments on commit 1678d0c

Please sign in to comment.