Skip to content

Commit

Permalink
add byte_vector_* as an alias of string_*
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed Sep 20, 2019
1 parent c588ec1 commit 01c4880
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
103 changes: 103 additions & 0 deletions vunit/vhdl/data_types/src/byte_vector_ptr_pkg.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-- 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-2019, Lars Asplund [email protected]
--
-- The purpose of this package is to provide a byte vector access type (pointer)
-- that can itself be used in arrays and returned from functions unlike a
-- real access type. This is achieved by letting the actual value be a handle
-- into a singleton datastructure of string access types.
--

use work.types_pkg.all;
use work.string_ptr_pkg.all;

package byte_vector_ptr_pkg is

alias val_t is byte_t;

alias storage_mode_t is work.string_ptr_pkg.storage_mode_t;
alias byte_vector_ptr_t is string_ptr_t;
alias null_byte_vector_ptr is null_string_ptr;

alias new_byte_vector_ptr is new_string_ptr[string, storage_mode_t, integer return ptr_t];

alias is_external is is_external[ptr_t return boolean];
alias deallocate is deallocate[ptr_t];
alias length is length[ptr_t return integer];

impure function new_byte_vector_ptr (
length : natural := 0;
mode : storage_mode_t := internal;
id : integer := 0;
value : val_t := 0
) return ptr_t;

procedure set (
ptr : ptr_t;
index : natural;
value : val_t
);

impure function get (
ptr : ptr_t;
index : natural
) return val_t;

procedure reallocate (
ptr : ptr_t;
length : natural;
value : val_t := 0
);

procedure resize (
ptr : ptr_t;
length : natural;
drop : natural := 0;
value : val_t := 0
);
end package;

package body byte_vector_ptr_pkg is
impure function new_byte_vector_ptr (
length : natural := 0;
mode : storage_mode_t := internal;
id : integer := 0;
value : val_t := 0
) return ptr_t is begin
return work.string_ptr_pkg.new_string_ptr(length, mode, id, character'val(value));
end;

procedure set (
ptr : ptr_t;
index : natural;
value : val_t
) is begin
work.string_ptr_pkg.set(ptr, index+1, character'val(value));
end;

impure function get (
ptr : ptr_t;
index : natural
) return val_t is begin
return character'pos(work.string_ptr_pkg.get(ptr, index+1));
end;

procedure reallocate (
ptr : ptr_t;
length : natural;
value : val_t := 0
) is begin
work.string_ptr_pkg.reallocate(ptr, length, character'val(value));
end;

procedure resize (
ptr : ptr_t;
length : natural;
drop : natural := 0;
value : val_t := 0
) is begin
work.string_ptr_pkg.resize(ptr, length, drop, character'val(value));
end;
end package body;
1 change: 1 addition & 0 deletions vunit/vhdl/data_types/src/data_types_context.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

context data_types_context is
library vunit_lib;
use vunit_lib.byte_vector_ptr_pkg.all;
use vunit_lib.integer_vector_ptr_pkg.all;
use vunit_lib.integer_vector_ptr_pool_pkg.all;
use vunit_lib.integer_array_pkg.all;
Expand Down
9 changes: 9 additions & 0 deletions vunit/vhdl/data_types/src/types.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ package types_pkg is
type extstring_access_vector_t is array (natural range <>) of extstring_access_t;
type extstring_access_vector_access_t is access extstring_access_vector_t;

alias byte_vector_access_t is string_access_t;
alias byte_vector_access_vector_t is string_access_vector_t;
alias byte_vector_access_vector_access_t is string_access_vector_access_t;

alias extbytevec_access_t is extstring_access_t;
alias extbytevec_access_vector_t is extstring_access_vector_t;
alias extbytevec_access_vector_access_t is extstring_access_vector_access_t;

type integer_vector_t is array (natural range <>) of integer;
type integer_vector_access_t is access integer_vector_t;
type integer_vector_access_vector_t is array (natural range <>) of integer_vector_access_t;
type integer_vector_access_vector_access_t is access integer_vector_access_vector_t;
end package;

46 changes: 46 additions & 0 deletions vunit/vhdl/data_types/test/tb_byte_vector_ptr.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
-- 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-2019, Lars Asplund [email protected]

library vunit_lib;
--context vunit_lib.vunit_context;
use vunit_lib.check_pkg.all;
use vunit_lib.run_pkg.all;

use work.byte_vector_ptr_pkg.all;

entity tb_byte_vector_ptr is
generic (runner_cfg : string);
end;

architecture a of tb_byte_vector_ptr is
begin
main : process
variable ptr, ptr2 : byte_vector_ptr_t;
constant a_random_value : natural := 7;
constant another_random_value : natural := 9;
begin
test_runner_setup(runner, runner_cfg);

while test_suite loop
if run("test_element_access") then
ptr := new_byte_vector_ptr(1);
set(ptr, 0, a_random_value);
assert get(ptr, 0) = a_random_value;

ptr2 := new_byte_vector_ptr(2);
set(ptr2, 0, another_random_value);
set(ptr2, 1, a_random_value);
assert get(ptr2, 0) = another_random_value;
assert get(ptr2, 1) = a_random_value;

assert get(ptr, 0) = a_random_value report
"Checking that ptr was not affected by ptr2";
end if;
end loop;

test_runner_cleanup(runner);
end process;
end architecture;

0 comments on commit 01c4880

Please sign in to comment.