Composite Date Types: Arrays
Composite Date Types: Arrays
Arrays
– Single and multi-dimensional
– Single Type
Records
– Mixed types
1
Component
2
Component- Half Adder
library IEEE;
use IEEE.std_logic_1164.all;
entity xHA is
port( A, B: in std_logic;
SUM, CARRY: out std_logic);
end xHA;
library IEEE;
use IEEE.std_logic_1164.all;
entity xFA is
port(
X,Y,Z : in std_logic;
SUMM : out std_logic;
COUT : out std_logic );
end xFA;
4
Component - Full Adder1
begin
architecture xFA_a of xFA is
ha1 : xha port map
component xha port
( A => X,
( A, B : in std_logic;
B => Y,
SUM : out std_logic;
SUM => S2,
CARRY : out std_logic);
CARRY=> S1 );
end component xha;
ha2 : xha port map
( A => S2,
signal S1,S2,S3 : std_logic;
B => Z,
SUM => SUMM,
CARRY=> S3 );
cout <= S1 OR S3;
end xFA_a;
5
Component - Full Adder2
library IEEE; architecture xxFA_a of xxFA is
use IEEE.std_logic_1164.all; component xha port
( A, B : in std_logic;
entity xxFA is SUM : out std_logic ;
port( X,Y,Z : in std_logic; CARRY : out std_logic);
SUMM :out std_logic; end component xha;
COUT : out std_logic);
end xxFA; signal S1,S2,S3 : std_logic;
begin
HA1 : xha port map(X, Y, S2, S1);
HA2 : xha port map(S2, Z, SUMM, S3);
cout <= S1 OR S3;
end xxFA_a;
6
Array
7
Array
– Constrained
» the bounds for an index are established when the
type is defined
– Unconstrained
» the bounds are established after the type is defined
– Each position in the array has a scalar index
value associated with it
8
Array Definition Syntax
discrete_range is an index
– name of previously declared type with optional
range constraint
9
Array Declaration, e.g.,
10
Array Declaration, e.g.,
type 2D_FFT is array
( 1 to 128, 1 to 128 ) of real ;
11
Array Declaration, e.g.,
subtype coeff_ram_address is
integer range 0 to 63;
12
Array Example
architecture EXAMPLE of ARRAYS is
signal Z_BUS : bit_vector (3 downto 0);
signal C_BUS : bit_vector (0 to 3);
begin
Z_BUS <= C_BUS;
end EXAMPLE;
13
Array Example
architecture EXAMPLE of ASSIGNMENT is
signal Z_BUS : bit_vector (3 downto 0);
signal BIG_BUS : bit_vector (15 downto 0);
begin
-- legal assignments:
Z_BUS(3) <= ‘1’;
Z_BUS <= ”1100”;
Z_BUS <= b ”1100”;
Z_BUS <= x ”c”;
Z_BUS <= X ” C”;
BIG_BUS <= B ”0000 _ 0001_0010_0011”;
end EXAMPLE;
14
Array Example
architecture EXAMPLE_1 of CONCATENATION is
signal BYTE : bit_vector (7 downto 0);
signal A_BUS, B_BUS : bit_vector (3 downto 0);
begin
BYTE <= A_BUS & B_BUS;
end EXAMPLE;
15
Array Example
architecture EXAMPLE_2 of CONCATENATION is
signal Z_BUS : bit_vector (3 downto 0);
signal A_BIT, B_BIT, C_BIT, D_BIT : bit;
begin
Z_BUS <= A_BIT & B_BIT & C_BIT & D_BIT;
end EXAMPLE;
16
Array Example
architecture EXAMPLE of AGGREGATES is
signal BYTE : bit_vector (7 downto 0);
signal Z_BUS : bit_vector (3 downto 0);
signal A_BIT, B_BIT, C_BIT, D_BIT : bit;
begin
Z_BUS <= ( A_BIT, B_BIT, C_BIT, D_BIT ) ;
( A_BIT, B_BIT, C_BIT, D_BIT ) <= bit_vector(”1011”);
( A_BIT, B_BIT, C_BIT, D_BIT ) <= BYTE(3 downto 0);
BYTE <= ( 7 => ‘1’, 5 downto 1 => ‘1’, 6 => B_BIT, others => ‘0’) ;
end EXAMPLE;
17
Array Example
architecture EXAMPLE of SLICES is
signal BYTE : bit_vector (7 downto 0);
signal A_BUS: bit_vector (3 downto 0);
signal Z_BUS : bit_vector (3 downto 0);
signal A_BIT : bit;
begin
BYTE (5 downto 2) <= A_BUS;
BYTE (5 downto 0) <= A_BUS; -- Wrong
18
Memory Example
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
entity ram6116 is
port(address: in unsigned(7 downto 0);
data: inout std_logic_vector(7 downto 0);
WE_b, CS_b, OE_b: in std_ulogic);
end entity ram6116;
19
Memory Example
architecture simple_ram of ram6116 is
type ram_type is array (0 to 2**8-1) of std_logic_vector(7 downto 0);
signal ram1: ram_type:= (others => (others => ’0’));
begin
process
begin
data <= (others => ‘Z’); -- chip is not selected
if (CS_b = ‘0’) then
if rising_edge(WE_b) then -- write
ram1(conv_integer(address’delayed)) <= data;
wait for 0 ns;
end if;
if WE_b = ‘1’ and OE_b = ‘0’ then -- read
data <= ram1(conv_integer(address));
else
data <= (others => ‘Z’);
end if;
end if;
wait on WE_b, CS_b, OE_b, address;
end process; end simple_ram;
20
Memory Example (page 84)
entity coeff_ram is
port(rd, wr : in bit;
addr : in coeff_ram_address;
d_in : in real;
d_out : out real );
end entity coeff_ram;
21
Memory Example ..
architecture abstract of coeff_ram is
begin
memory: process is
type coeff_array is array (coeff_ram_address)
of real;
variable coeff : coeff_array;
begin
for index in coeff_ram_address loop
coeff(index) := 0.0;
end loop;
22
Memory Example ..
loop
wait on rd, wr, addr, d_in;
if rd = '1' then
d_out <= coeff(addr);
end if;
if wr = '1' then
coeff(addr) := d_in;
end if;
end loop;
end process memory;
end architecture abstract;
23
Unconstrained Declaration
type Detector_Array is array
( natural range <> ) of natural ;
The symbol ‘<>’ is called a box and can be
thought of as a place-holder for the index
range.
Box is filled in later when the type is used.
variable X_Ray_Detector : Detector_Array
( 1 to 64 ) ;
24
Predefined Unconstrained Types
25
Predefined Unconstrained Types
26
Unconstrained Array Ports
27
Unconstrained Array Port, e.g.,
entity And_Multiple is
port ( i : in bit_vector ;
y : out bit ) ;
28
AND, e.g.,
architecture And_Multiple_B of And_Multiple is
begin
And_Reducer : process ( i ) is
variable Result : bit ;
begin
Result := ‘1’ ;
for Index in i’Range loop
Result := Result and i ( Index ) ;
end loop ;
y <= Result ;
end process And_Reducer ;
end architecture And_Multiple_B ;
29
AND, e.g.,
signal count_value :
bit_vector ( 7 downto 0 ) ;
signal terminal_count : bit ;
tc_gate : entity work.And_Multiple
( And_Multiple_B )
port map ( i => count_value ,
y => terminal_count ) ;
30
AND, e.g.,
31
Array References
Arrays Can Be Equated, Rather Than
Having to Transfer Element by Element
Refer to Individual Elements By
– Single Index Value, e.g., A ( 5 )
– Range: a contiguous sequence of a one-
dimensional array can be referred to by using it
as an index. e.g., A( 5 to 15 )
– Previously defined subtype
– Index types do not have to be the same
32
Array Aggregate Syntax
33
Array Aggregate
variable FLIR_Status :
Sensor_Status := ( 0 sec , 0 sec , 0 sec );
variable FLIR_Status :
Sensor_Status := ( On => 5 sec ) ;
35
Array Aggregate, e.g.,
36
Array Aggregate, e.g.,
38
Array Operations, e.g.,
constant Bit_Mask : Large_Word
( 8 to 15 => ‘1’ ) ;
39
Array Operations
40
Array Operations
41
Array Operations, e.g.,
42
Array Operations
43
Array Operations
44
Array Type Conversions
45
Array Type Conversions, e.g.,
Ashenden Example
46
Array Aggregate, e.g.,
47
Array Aggregate, e.g.,
Variable FLIR_Status :
Sensor_Status := ( 0 sec ,
0 sec ,
0 sec ) ;
( Stdby_Time,
On_Time,
Off_Time ) := Flir_Status ;
48
Records
49
Records
50
Record e.g.,*
type instruction is
record
op_code : processor_op ;
address_mode : mode ;
operand1 : integer range 0 to 15 ;
operand2 : integer range 0 to 15 ;
end record ;
51
End of Lecture
52