Range Over Built in VHDL On this page Based on the provided input, here is the translated Go code example to VHDL:
Example: Range over Built-in Types# Range iterates over elements in a variety of built-in data structures. Let’s see how to use range
with some of the data structures we’ve already learned.
Here we use range
to sum the numbers in a slice. Arrays work like this too.
library ieee ;
use ieee.std_logic_1164. all ;
use ieee.numeric_std. all ;
entity range_example is
end range_example ;
architecture Behavioral of range_example is
begin
process
variable nums : integer_array ( 0 to 2 ) := ( 2 , 3 , 4 );
variable sum : integer := 0 ;
variable i : integer ;
begin
-- Summing the numbers
for i in nums 'range loop
sum := sum + nums ( i );
end loop ;
report "sum: " & integer 'image ( sum );
-- Using index and value
for i in nums 'range loop
if nums ( i ) = 3 then
report "index: " & integer 'image ( i );
end if ;
end loop ;
-- Map equivalent in VHDL (array of records)
type kv is record
key : string ( 1 to 1 );
value : string ( 1 to 6 );
end record ;
type kv_array is array ( 0 to 1 ) of kv ;
constant kvs : kv_array := ( ( key => "a" , value => "apple" ), ( key => "b" , value => "banana" ) );
-- Iterating over key/value pairs
for j in kvs 'range loop
report kvs ( j ). key & " -> " & kvs ( j ). value ;
end loop ;
-- Iterating over keys only
for j in kvs 'range loop
report "key: " & kvs ( j ). key ;
end loop ;
-- Iterating over string in VHDL
constant str : string := "go" ;
for i in str 'range loop
report integer 'image ( i ) & " " & integer 'image ( character 'pos ( str ( i )));
end loop ;
wait ;
end process ;
end Behavioral ;
To run this example, save the code in a file, compile it, and see the output using your VHDL simulation tools.
# If using GHDL, for instance:
$ ghdl -a range_example.vhdl
$ ghdl -e range_example
$ ghdl -r range_example
sum: 9
index: 1
a -> apple
b -> banana
key: a
key: b
1 103
2 111
In the above code, we follow the VHDL syntax and conventions to achieve similar functionalities described in the initial code example.