Write a VHDL Entity and Architecture for a Simple Counter
Write a VHDL counter with an entity, an architecture and a clocked process using numeric_std, then simulate it with GHDL and read the printed result.

VHDL describes the same hardware as Verilog, with a different temperament. It is strongly typed, verbose and strict, and it splits every design unit into an interface and an implementation. This post writes a clocked counter in VHDL-2008, explains the entity and architecture split, and simulates it with GHDL 4.0. It assumes you know what a flip-flop and a clock edge are, and shows what each VHDL construct maps to. The language is defined by IEEE Std 1076-2008.
Entity: the interface
An entity declares a block's name and ports, and nothing about what it does. It is the equivalent of a Verilog module header.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter is
generic (
WIDTH : positive := 8
);
port (
clk : in std_logic;
rst : in std_logic;
count : out std_logic_vector(WIDTH - 1 downto 0)
);
end entity counter;The library and use lines import packages. ieee.std_logic_1164 provides std_logic, the standard nine-valued logic type that models 0, 1, unknown, high impedance and more. ieee.numeric_std provides unsigned and signed types with arithmetic. Older code uses std_logic_unsigned or std_logic_arith, which are not part of the IEEE standard, so avoid them.
generic is the VHDL parameter. WIDTH : positive := 8 has a type and a default. Ports are in, out or inout, each with a type. std_logic_vector(WIDTH - 1 downto 0) is a bus with the most significant bit on the left, written as downto, the usual convention.
Architecture: the implementation
An architecture gives the entity its behaviour. One entity can have several architectures, for example a behavioural one and a gate-level one, and a configuration selects which one is used.
architecture rtl of counter is
signal value : unsigned(WIDTH - 1 downto 0) := (others => '0');
begin
process (clk)
begin
if rising_edge(clk) then
if rst = '1' then
value <= (others => '0');
else
value <= value + 1;
end if;
end if;
end process;
count <= std_logic_vector(value);
end architecture rtl;Each part has a purpose.
The signal value is the internal register, of type unsigned. Using unsigned for the arithmetic is the VHDL way: std_logic_vector has no +, so you hold the number as unsigned and convert at the port. The initial value (others => '0') is an aggregate that sets every bit to '0'.
The process (clk) has a sensitivity list with the clock only, because a clocked process needs nothing else. The condition rising_edge(clk) is the correct edge test. Older style clk'event and clk = '1' also matches transitions from unknown, which is a trap.
The assignment <= is a signal assignment and it is always scheduled for the end of the delta cycle, like Verilog's non-blocking assignment. VHDL separates signals, which use <=, from variables, which use := and update at once. So the blocking versus non-blocking question is settled by the type of object, not by the operator.
The line count <= std_logic_vector(value); is a concurrent assignment outside the process. It keeps count equal to value at all times, a plain wire from the register to the port.
value + 1 uses the + from numeric_std that adds an integer to an unsigned. When the counter holds all ones, the result wraps to zero, with no overflow error.
A testbench in VHDL
A VHDL testbench is an entity with no ports. It instantiates the design, generates a clock and checks outputs. To print without relying on tool-specific message formats, use std.textio.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use std.textio.all;
entity counter_tb is
end entity counter_tb;
architecture sim of counter_tb is
constant PERIOD : time := 10 ns;
signal clk : std_logic := '0';
signal rst : std_logic := '1';
signal count : std_logic_vector(3 downto 0);
signal done : boolean := false;
begin
dut : entity work.counter
generic map (WIDTH => 4)
port map (clk => clk, rst => rst, count => count);
clk <= not clk after PERIOD / 2 when not done else clk;
stimulus : process
variable l : line;
begin
wait for 12 ns;
rst <= '0';
wait for 5 * PERIOD;
write(l, string'("count after 5 edges: "));
write(l, to_integer(unsigned(count)));
writeline(output, l);
done <= true;
wait;
end process;
end architecture sim;entity work.counter instantiates the design directly without a component declaration, a VHDL-93 feature. generic map (WIDTH => 4) overrides the generic, and port map connects named ports. The clock line uses a conditional signal assignment with after, and stops toggling when done becomes true so the simulation ends. The stimulus process releases reset at 12 ns, waits 50 ns, and writes the count. Rising edges at 5, 15, 25, 35, 45 and 55 ns occur before 62 ns. The 5 ns edge saw reset high. The five edges at 15 to 55 ns increment, so the count is 5.
Analysing, elaborating and running with GHDL
GHDL has three steps: analyse each file, elaborate the top unit, and run.
ghdl -a --std=08 counter.vhd counter_rtl.vhd counter_tb.vhd
ghdl -e --std=08 counter_tb
ghdl -r --std=08 counter_tbcount after 5 edges: 5Analysis (-a) parses files in dependency order: entity, then architecture, then testbench. Elaboration (-e) links the design from the named top. Running (-r) simulates until no events remain, which is why the clock must stop when done is set. A simulation that never ends is the usual cause of an apparent hang with a free-running clock.
You can also stop at a fixed time with ghdl -r --std=08 counter_tb --stop-time=100ns.
Comparing with the Verilog version
| Idea | Verilog | VHDL |
|---|---|---|
| Interface | module ports | entity ports and generics |
| Body | module body | architecture |
| Register assignment | <= in always | <= on a signal in a process |
| Edge test | posedge clk | rising_edge(clk) |
| Number type | reg [7:0], unsigned by default | unsigned(7 downto 0) from numeric_std |
| Reset value | 8'd0 | (others => '0') |
| Type conversion | implicit | explicit std_logic_vector(...), to_integer(...) |
VHDL's strictness is a feature. You cannot add a std_logic_vector to an integer by accident, and a width mismatch is a compile error rather than a silent truncation. The price is more typing.
Synthesising it
Yosys can read VHDL through the ghdl plugin, and commercial tools read it natively. Vivado and Quartus both accept VHDL-2008 in recent versions, with some constructs unsupported, so check each tool's list. The counter above uses only widely supported features, so it synthesises as the same register and incrementer as the Verilog version.
Common mistakes
**Using
std_logic_vectorfor arithmetic.** Convert tounsigned, add, convert back.**Missing
use ieee.numeric_std.all.** Without it,unsignedis undeclared and the error points at the declaration.Forgetting that signals update after the process. Reading
valueright aftervalue <= value + 1;in the same process gives the old value.Incomplete sensitivity lists in combinational processes. VHDL-2008 allows
process (all), which is the equivalent of Verilog's@*.A clock that never stops. The simulator never finishes, so stop it with a flag or a time limit.
Reading the analysis errors
GHDL's error messages are literal, and a few are worth recognising on sight. A message that a name is not declared usually means a missing use clause for numeric_std, or a signal used before its declaration in the architecture's declarative part. A message about a type mismatch on + points to arithmetic on a std_logic_vector, which has no operators. An unbound entity message means the file defining the entity was not analysed before the file that instantiates it, so reorder the files on the command line. Because analysis checks types strictly, these are caught before any simulation runs.
One more habit is worth building early. Give every architecture a meaningful name such as rtl for synthesisable code and sim for testbenches, because a configuration can then pick between them, and a reader knows at a glance which one describes hardware. The names have no effect on synthesis.


