- •Глава 6. Примеры проектирования цифровых устройств с использованием языков описания аппаратуры vhdLиVerilog.
- •6.1. Общие сведения
- •6.2. Триггеры и регистры (Flip-Flops, Registers)
- •6.3. Построение устройств потоковой обработки данных (Datapath logic)
- •6.4. Счетчики
- •6.5. Арифметические устройства
- •6.6.Конечные автоматы (Finite State Machine)
- •6.7. Элементы ввода – вывода
- •6.8.Параметризация
- •6.9. Спрецифика проектирования устройств с учетом архитектурных особенностей плис
- •6.10. Совместное использование ресурсов
- •6.12. Дублирование регистра.
- •6.13.Создание описаний с учетом особенностей архитектуры плис (Technology Specific Coding Techniques).
6.4. Счетчики
Счетчики являются достаточно широко распространенными устройствами, их классификация и принципы построения изложены в литературе [34-38]. Следует помнить, что большинство программ синтеза не позволяют получить приемлимых результатов по быстродействию при разрядности счетчика более 8 бит, в этом случае часто применяются специфические приемы синтеза, зависящие от технологии, по которой выполнена ПЛИС.
Рассмотрим пример построения 8 разрядного счетчика, считающего в прямом направлении и имеющего цепи разрешения счета и асинхронного сброса.
Пример описания на VHDL
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;
entity counter8 is
port (clk, en, rst : in std_logic;
count : out std_logic_vector (7 downto 0));
end counter8;
architecture behav of counter8 is
signal cnt: std_logic_vector (7 downto 0);
begin
process (clk, en, cnt, rst)
begin
if (rst = '0') then
cnt <= (others => '0');
elsif (clk'event and clk = '1') then
if (en = '1') then
cnt <= cnt + '1';
end if;
end process;
count <= cnt;
end behav;
Пример на Verilog
module count_en (en, clock, reset, out);
parameter Width = 8;
input clock, reset, en;
output [Width-1:0] out;
reg [Width-1:0] out;
always @(posedge clock or negedge reset)
if(!reset)
out = 8'b0;
else if(en)
out =out +1;
endmodule
Другой пример иллюстрирует построение 8 разрядного счетчика с загрузкой и асинхронным сбросом (8-bit Up Counter with Load and Asynchronous Reset)
Пример на VHDL
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;
entity counter is
port (clk, reset, load: in std_logic;
data: in std_logic_vector (7 downto 0);
count: out std_logic_vector (7 downto 0));
end counter;
architecture behave of counter is
signal count_i : std_logic_vector (7 downto 0);
begin
process (clk, reset)
begin
if (reset = '0') then
count_i <= (others => '0');
elsif (clk'event and clk = '1') then
if load = '1' then
count_i <= data;
else
count_i <= count_i + '1';
end if;
end if;
end process;
count <= count_i;
end behave;
Описание на Verilog
module count_load (out, data, load, clk, reset);
parameter Width = 8;
input load, clk, reset;
input [Width-1:0] data;
output [Width-1:0] out;
reg [Width-1:0] out;
always @(posedge clk or negedge reset)
if(!reset)
out = 8'b0;
else if(load)
out = data;
else
out =out +1;
endmodule
Пример построения счетчика с предварительной загрузкой, входами разрешения и остановом счета (8-bit Up Counter with Load, Count Enable, Terminal Count and Asynchronous Reset) приведен ниже.
Описание на Verilog
module count_load (out, cout, data, load, clk, en, reset);
parameter Width = 8;
input load, clk, en, reset;
input [Width-1:0] data;
output cout; // carry out
output [Width-1:0] out;
reg [Width-1:0] out;
always @(posedge clk or negedge reset)
if(!reset)
out = 8'b0;
else if(load)
out = data;
else if(en)
out =out +1;
// cout=1 when all out bits equal 1
assign cout = &out;
endmodule
Следующий пример – счетчик с произвольным модулем счета и всеми остальными функциями – сброс, загрузка и т.п. (N-bit Up Counter with Load, Count Enable, and Asynchronous Reset)
Пример на VHDL
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;
entity counter is
generic (width : integer := n);
port (data : in std_logic_vector (width-1 downto 0);
load, en, clk, rst : in std_logic;
q :out std_logic_vector (width-1 downto 0));
end counter;
architecture behave of counter is
signal count : std_logic_vector (width-1 downto 0);
begin
process(clk, rst)
begin
if rst = '1' then
count <= (others => '0');
elsif (clk'event and clk = '1') then
if load = '1' then
count <= data;
elsif en = '1' then
count <= count + 1;
end if;
end if;
end process;
q <= count;
end behave;