-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add byte_vector_* as an alias of string_*
- Loading branch information
Showing
4 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |