array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1...

29
Array aggregates olumns is range 1 to 4; ow is array (columns) of std_logic; le r1 : row := ('1', '0', '1', '1'); le r2 : row := (1 => '1', 2 => '0', others => '1'); le r3 : row := (1 => '1', 2 => '0', 3 to 4 => '1'); le r4 : row := (2 => '0', others => '1'); le r5 : row := (1 | 3 | 4 => '1', 2 => '0'); le r6 : row := ('0', 2 | 3 => '1', 4 => '0'); -- erro associação posicional associação nomeada 1 0 1 1 r1 – r5

Upload: internet

Post on 17-Apr-2015

112 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Array aggregates

type columns is range 1 to 4;type row is array (columns) of std_logic;

variable r1 : row := ('1', '0', '1', '1');variable r2 : row := (1 => '1', 2 => '0', others => '1');variable r3 : row := (1 => '1', 2 => '0', 3 to 4 => '1');variable r4 : row := (2 => '0', others => '1');variable r5 : row := (1 | 3 | 4 => '1', 2 => '0');variable r6 : row := ('0', 2 | 3 => '1', 4 => '0'); -- erro

associação posicional

associação nomeada

1 0 1 1

r1 – r5

Page 2: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Array aggregates

variable m3 : matrix2 := ( ('0', '0', '0', '0'), ('1', '1', '1', '1'));variable m4 : matrix1 := ((others => '0'), (others => '1'));variable m5 : matrix1 := (others => (others => '1'));variable m6 : matrix1 := (1 => (1|2 => '1', 3|4 => '0'), 2=> (1 => '0', others => '1'));

0 0 0 0

1 1 1 1

m3, m4

1 1 1 1

1 1 1 1

m5

1 1 0 0

0 1 1 1

m6

type rows is range 1 to 2; type columns is range 1 to 4;

type row is array (columns) of std_logic;

type matrix1 is array (rows) of row; type matrix2 is array (rows, columns) of std_logic;

Page 3: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

type mapping is array (character range 'a' to 'b', 1 to 3) of std_logic;

variable mapa : mapping := ('a' => (others => '0'), 'b' => (1 => '1', 2 => '0', 3 => '1'));

0 0 0

1 0 1

‘a’

‘b’

1 2 3

Page 4: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

entity bcd_code is Port ( bin : in std_logic_vector(3 downto 0); bcd : out std_logic_vector(4 downto 0));end bcd_code;

architecture Behavioral of bcd_code is type codes is array (0 to 15) of std_logic_vector(4 downto 0); constant code : codes := ( "00000", "00001", "00010", "00011", "00100", "00101", "00110", "00111",

"01000", "01001", "10000", "10001", "10010", "10011", "10100", "10101“ );

begin bcd <= code(conv_integer(bin));end Behavioral;

1111 1 5

0000 0 0

0001 0 1

1110 1 4

...

Page 5: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

type type_name is array (range) of type_of_elements;

type RAM is array (9 downto 0) of std_logic_vector(7 downto 0);signal my_RAM : RAM;

architecture Behavioral of VHDL_test istype RAM is array (9 downto 0) of std_logic_vector(7 downto 0);signal my_RAM : RAM;beginprocess(clk,reset)variable tmp : integer range 0 to 9;begin

if reset = '1' then tmp := 0;elsif rising_edge(clk) then

my_RAM(tmp) <= data_in + conv_std_logic_vector(tmp,8);if (tmp < 9) then tmp := tmp + 1; else tmp := 0; end if;

end if;end process;

my_RAM(0) = “00110000”my_RAM(1) = “00110001”my_RAM(2) = “00110010”my_RAM(3) = “00110011”my_RAM(4) = “00110100”my_RAM(5) = “00110101”my_RAM(6) = “00110110”my_RAM(7) = “00110111”my_RAM(8) = “00111000”my_RAM(9) = “00111001”

data_in = 00110000

Page 6: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Agregados na atribuição

signal reg : std_logic_vector(3 downto 0);signal ms, b2, b1, ls : std_logic;

ms <= reg(3);b2 <= reg(2);b1 <= reg(1);ls <= reg(0);

(ms, b2, b1, ls) <= reg;=

Page 7: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Atributos de arraystype'left(N); -- índice esquerdo da dimensão N do tipo (array) typetype'right(N); -- índice direito da dimensão N do tipo (array) typetype‘low(N); -- índice menor da dimensão N do tipo (array) typetype‘high(N); -- índice maior da dimensão N do tipo (array) typetype‘range(N); -- gama de índices da dimensão N do tipo (array) typetype‘reverse_range(N); -- gama de índices invertida da dimensão N -//-type‘length(N); -- número de índices da dimensão N do tipo (array) typetype‘ascending(N); -- true se gama de índices da dimensão N do tipo

(array) type é uma gama ascendente

type matrix2 is array (rows, columns) of std_logic;type matriz is array (1 to 3, 3 downto 0) of std_logic;type mapping is array (character range 'a' to 'b', 1 to 3) of std_logic;

variable m2 : matrix2;variable mapa : mapping;

mapa'left(1) = 'a'mapping'right(1) = 'b'mapping‘high(2) = 3mapa'length = 2mapa'ascending(2) = truefor i in mapa'reverse_range(1) loop --'b' downto 'a'end loop;

matriz'ascending(2) = falsem2'low(2) = 1

O número N pode ser omitido quando se refere à primeira dimensão.

Page 8: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Arrays não restringidos

Os arrays não restringidos não especificam os limites para os seus índices, indicando apenas o tipo dos índices e o tipo de elementos a guardar.

Quando se declara um objecto deste tipo é necessário indicar a gama de índices possíveis.

type inteiro is array (natural range <> ) of integer;variable v1 : inteiro (3 to 8);variable v2 : inteiro (10 downto 0);

1. Indicação explícita

subtype small_int is inteiro (0 to 64);variable v3 : small_int;

2. Sub-tipos

constant c1 : inteiro := (1 => 1, 2 => 2, 3 => 3);constant c2 : inteiro := (1, 2, 3);

3. Inicialização de constantes com agregados

Page 9: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Arrays não restringidos predefinidos

type string is array (positive range <>) of character;

constant linha1 : string := "Os nomes sao";constant linha2 : string (1 to 12) := "Os nomes sao";constant linha3 : string (30 downto 28) := "Ola";

type bit_vector is array (natural range <>) of bit;

type std_logic_vector is array (natural range <>) of std_logic;

É possível utilizar strings e bit strings para inicializar arrays cujos elementos são do tipo enumerativo que inclui caracteres.

signal s4 : std_logic_vector(3 downto 0);s4 <= "010-";s4 <= x"f";s4(s4'high - 1 downto s2'low) <= o"5";

std_logic_1164

Page 10: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Portas não restringidasentity porta_or is Port ( input : in std_logic_vector; result : out std_logic);end porta_or;

architecture beh of porta_or isbegin

process (input) is variable temp : std_logic; begin temp := '0';

for i in input'range loop temp := temp or input(i); if (temp = '1') then

exit; end if; end loop;

result <= temp; end process;end beh;

entity calc is Port ( vector1 : in std_logic_vector(1 downto 0); vector2 : in std_logic_vector(7 downto 0); vector3 : in std_logic_vector(0 to 15); r1 : out std_logic; r2 : out std_logic; r3 : out std_logic);end calc;

architecture Behavioral of calc isbegin v1: entity work.porta_or(beh)

port map (input => vector1, result => r1);

v2: entity work.porta_or(beh) port map (input => vector2, result => r2);

v3: entity work.porta_or(beh) port map (input => vector3, result => r3);

end Behavioral;

Page 11: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Operações sobre arrays

Operações lógicas not, and, or, nand, nor, xor, xnor podem ser aplicadas a um ou a dois arrays unidimensionais desde que estas operações sejam definidas para os elementos dos arrays.

Operações de deslocamento sll, srl, sla, sra, rol, ror podem ser aplicadas a um array unidimensional (operando esquerdo) de bits ou valores booleanos.

Operações comparativas podem ser aplicadas a dois arrays unidimensionais cujos elementos são de qualquer tipo discreto.

entity arr_op is Port ( x : in std_logic_vector(7 downto 0); y : in std_logic_vector(7 downto 0); gr : out std_logic);end arr_op;

architecture Beh of arr_op isbeginprocess (x, y) is begin if (x > y) then gr <= '1'; else gr <= '0'; end if; end process;end Beh;

O operador de concatenação & pode ser aplicado a dois arrays unidimensionais e a valores escalares.

cmd <= "0011" & digit_dec;cmd <= "0011000" & ‘1';

Page 12: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Array slices

Servem para especificar um subconjunto de elementos num array.

A direcção de índices num slice deve ser idêntica à direcção de índices no array.

signal x : std_logic_vector(7 downto 0);signal y : std_logic_vector(7 downto 0);

signal s1 : std_logic_vector(3 downto 0);signal s2 : std_logic_vector(0 to 3);

s1 <= x (7 downto 4);s1 <= y (0 to 3); -- erro

s2 <= x (7 downto 4);s2 <= y (3 downto 0);y (0 downto 3); -- null slice

Page 13: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Conversão entre tipos

Um valor do tipo array pode ser convertido num outro valor de outro tipo array desde que ambos os tipos tenham:

- o mesmo tipo de elementos;- o mesmo número de dimensões;- os tipos de índices que podem ser convertidos.

type arr1 is array (‘b’ downto ‘a’, 1 to 2) of std_logic;type arr2 is array (‘a’ to ‘b’, 3 to 4) of std_logic;

variable v1 : arr1;variable v2 : arr2;

v2 := (v1); -- errov2 := arr2(v1);

Page 14: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Quando atribuímos um valor de um subtipo a um objecto de um outro subtipo do mesmo tipo, a conversão ocorre automaticamente.

subtype BE is std_logic_vector(0 to 3);subtype LE is std_logic_vector(3 downto 0);subtype M is std_logic_vector(20 to 23);

variable b : BE;variable l : LE;variable m : M;

b := x"b";l := b;m := b;

1 0 1 1

0 1 2 3

b

1 0 1 1

3 2 1 0

l

1 0 1 1

20 21 22 23

m

Page 15: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Records

- composição de elementos de vários tipos.

entity rec is Port ( clk : in std_logic; d : in std_logic_vector(7 downto 0));end rec;

architecture Behavioral of rec is type sp is record start_bit : std_logic; data_bits : std_logic_vector (7 downto 0); parity_bit : std_ulogic; stop_bit : std_logic; end record sp; signal s1, s2 : sp;

begin s1.data_bits <= d; s2 <= s1;end Behavioral;

Page 16: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Agregados em records

constant c1 : sp := (start_bit => '1', data_bits => x"f5", parity_bit => '0', stop_bit => '1');

constant c2 : sp := ('1', x"f5", '0', '1');

constant c3 : sp := (data_bits => x"f5", parity_bit => '0', stop_bit => '1', start_bit => '1');

type sp is record start_bit : std_logic; data_bits : std_logic_vector (7 downto 0); parity_bit : std_ulogic; stop_bit : std_logic; end record sp;

associações posicionais e nomeadas

Page 17: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

code of entityarchitecture Behavioral of VHDL_test is-- declaração de bit3,…,bit0 e my_busbegin

bit3 <= '0';bit2 <= '1';bit1 <= '1';bit0 <= '1';my_bus <= (0=>bit3, 2=>bit2, 1=>bit1, 3=>bit0);data_out <= my_bus;

end Behavioral;

Isto é uma associaçãonomeada

Example inVHDL_AF.zip

Page 18: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

code of entity

architecture Behavioral of VHDL_test issignal my_bus : std_logic_vector(3 downto 0);begin

my_bus <= (1 downto 0 => '1', others => '0');data_out <= my_bus; -- valor 3

end Behavioral;

Os elementos podem ser agrupados. Palavra chave others indica os restantes elementos

Example inVHDL_AF.zip

Page 19: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

code of entity

architecture Behavioral of VHDL_test istype my_packet is record

first_bit : std_logic;second_bit : std_logic;data : std_logic_vector (1 downto 0);

end record;signal record_data : my_packet;

beginrecord_data <= ('0', '1', "01");data_out <= record_data.first_bit & record_data.second_bit &

record_data.data; -- valor 5end Behavioral;

Example inVHDL_AF.zip

Page 20: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

code of entity

architecture Behavioral of VHDL_test istype my_array is array(3 downto 0) of std_logic;type my_packet is array(0 to 5) of my_array;signal my_data : my_packet := (others => "0110");begin data_out <= my_data(2)(3) & my_data(2)(2) & my_data(2)(1) & my_data(2)(0);

-- valor 6end Behavioral; 3 2 1 0

std_logic

my_array

my_array

my_array

my_array

my_array

my_array

my_array

my_packetmy_dataExample in

VHDL_AF.zip

Page 21: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

architecture Behavioral of VHDL_test istype my_array is array(3 downto 0) of std_logic;type my_packet is array(0 to 5) of my_array;signal my_data : my_packet := (others => "0110");process(reset,clk)variable my_ind : integer range 0 to 5;begin

if reset = '1' then my_data <= (5=>"1000", 3=>"1001", others => "0100"); my_ind := 0;elsif rising_edge(clk) then

data_out <= my_data(my_ind)(3) & my_data(my_ind)(2) & my_data(my_ind)(1) & my_data(my_ind)(0);

index_out <= conv_std_logic_vector(my_ind,4);if my_ind = 5 then my_ind := 0;

else my_ind := my_ind + 1;end if;

end if;end process;end Behavioral;

inicialização

saída de dados para índice my_ind

output todisplayindex on LCD

a sequência dos índices0,1,2,3,4,5

Example inVHDL_AF.zip

Page 22: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

type type_name is array (range) of type_of_elements;

type RAM is array (9 downto 0) of std_logic_vector(7 downto 0);signal my_RAM : RAM;

architecture Behavioral of VHDL_test istype RAM is array (9 downto 0) of std_logic_vector(7 downto 0);signal my_RAM : RAM;beginprocess(clk,reset)variable tmp : integer range 0 to 9;begin

if reset = '1' then tmp := 0;elsif rising_edge(clk) then

my_RAM(tmp) <= data_in + conv_std_logic_vector(tmp,8);if (tmp < 9) then tmp := tmp + 1; else tmp := 0; end if;

end if;end process;

my_RAM(0) = “00110000”my_RAM(1) = “00110001”my_RAM(2) = “00110010”my_RAM(3) = “00110011”my_RAM(4) = “00110100”my_RAM(5) = “00110101”my_RAM(6) = “00110110”my_RAM(7) = “00110111”my_RAM(8) = “00111000”my_RAM(9) = “00111001”

data_in = 00110000

Page 23: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

objecto’nome_de_atributo;

process(clk,reset)begin

if falling_edge(clk) then for i in my_RAM'reverse_range loop -- o resultado é “00111001” (ASCII – 9) data_out <= my_RAM(i); end loop;end if;

end process;

process(clk,reset)begin

if falling_edge(clk) then for i in my_RAM'range loop -- o resultado é “00110000” (ASCII – 0)

data_out <= my_RAM(i); end loop;end if;

end process;

Page 24: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

object’attribute_name;

process(clk,reset)variable tmp : integer range 0 to 9;begin if reset = '1' then tmp := 0; elsif falling_edge(clk) then

data_out <= my_RAM(tmp);index_out <= "000" & my_RAM(tmp)(my_RAM(tmp)'right);index_out <= "000" & my_RAM(tmp)(my_RAM(tmp)'left); index_out <= x"30" + conv_std_logic_vector((my_RAM'length)-1,8);

--my_RAM'high - my_RAM'low + 1 - o resultado é 9index_out <= x"30" + conv_std_logic_vector(my_RAM(0)'length,8); if (tmp < 9) then tmp := tmp + 1; else tmp := 0; end if;end if;

end process;

index_out <= "000" & my_RAM(tmp)(my_RAM(tmp)'right);

bit direitobit esquerdo

o resultado é “00111000” (ASCII – 8)

length

Page 25: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

type nome_de tipo is especificação_de_tipo;

Tipos escalares predefinidos são: integer, real, bit, Boolean, character

subtype word8 is std_logic_vector (8 downto 1);type serial_pac is array (0 to 8) of word8;constant line_RS : serial_pac := (x"1B", --delete displayx"44", --delete displayx"4C", --delete displayx"1B", --draw line (ESC)x"47", --draw line (code of this operation)x"30", -- x1 left pointx"15", -- y1 upper pointx"E6", -- x2 right point x"72"); -- y2 bottom point

type positive_integers is range 0 to integer'high;type FSM_states is (start, increment, save, decrement,repeat);signal state : FSM_states;signal p_i : positive_integers;

Page 26: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

type serial_package is recordstart_bit : std_logic;data_bits : std_logic_vector (7 downto 0);parity_bit : std_ulogic;stop_bit : std_logic;number : integer range 0 to 127;

end record;

signal my_sp : serial_package;

my_sp.number <= 10;my_sp.start_bit <= '1';my_sp.data_bits <= (others => '1');

Atribuição de bits individuais

Page 27: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

use IEEE.NUMERIC_STD.ALL; -- para to_unsigned-- . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .index_out <= conv_std_logic_vector(index,4);for_op1 <= conv_integer(data_in(7 downto 5));index_out <= std_logic_vector(to_unsigned(character'pos('6'),4));index_out <= std_logic_vector(to_unsigned(character'pos(line2(1)), 8));

target_type(expressão) function(expressão)

Só as expressões relacionadas podem ser convertidas

type my_array is array (0 to 5) of std_logic;signal mem1 : my_array;signal mem2 : std_logic_vector (0 to 5);

-- mem1 <= mem2; erromem1 <= my_array(mem2); -- OKmem2 <= std_logic_vector(mem1); -- OK

exemplos:

exemplos:

Page 28: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

Para conversão de tipos não relacionados pode-se usar funções definidas peloutilizador, por exemplo:

architecture Behavioral of VHDL_test is-- . . . . . . . . . . . . . . . . . . . . .function Bollean_to_std_logic(input : Boolean)

return std_logic isbegin

if input then return '1';else return '0';

end if;end Bollean_to_std_logic;-- . . . . . . . . . . . . . . . . . . . . .begin

index_out <= "000" & Bollean_to_std_logic(true);

Page 29: Array aggregates type columns is range 1 to 4; type row is array (columns) of std_logic; variable r1 : row := ('1', '0', '1', '1'); variable r2 : row :=

subtype tiny is integer range 0 to 63;subtype part_of_std is std_logic range '1' to '-';

subtype nome_de_subtipo is tipo_baserange intervalo_de_valores;

1) '1' - logical 12) '0' - logical 03) 'H' – weak 14) 'L' – weak 05) 'X' – unknown6) 'U' – uninitialized7) 'Z' – high impedance8) '-' – don't care9) 'W' – weak unknown

std_logic values

subtype word8 is std_logic_vector (8 downto 1);type serial_pac is array (0 to 8) of word8;constant line_RS : serial_pac := (x"1B", --delete displayx"44", --delete displayx"4C", --delete displayx"1B", --draw line (ESC)x"47", --draw line (code of this operation)x"30", -- x1 left pointx"15", -- y1 upper pointx"E6", -- x2 right point x"72"); -- y2 bottom point