您的当前位置:首页正文

vhdl语言例程集锦

2024-07-16 来源:年旅网

 

vhdl语言例程集锦

 

Examples of VHDL Descriptions

Advanced Electronic Design Automation

Examples of VHDL Descriptions

Author: Ian Elliott of Northumbria University

This file contains a selection of VHDL source files which serve to illustrate the diversity and power of the language when used to describe various types of hardware. The examples range from simple combinational logic, described in

terms of basic logic gates, to more complex systems, such as a behavioural model of a microprocessor and associated memory. All of the examples can be simulated using any IEEE compliant VHDL simulator and many can be

synthesised using current synthesis tools.

Use the hierarchical links below to navigate your way through the examples:

l Combinational Logic

l Counters

l Shift Registers

l Memory

l State Machines

l Registers

l Systems

l ADC and DAC

l Arithmetic

Combinational Logic

l Exclusive-OR Gate (Dataflow style)

l Exclusive-OR Gate (Behavioural style)

l Exclusive-OR Gate (Structural style)

l Miscell aneous Logic Gates

l Three-input Majority Voter

l Magnitude Comparator

l Quad 2-input Nand (74x00)

l BCD to Seven Segment Decoder

l Dual 2-to-4 Decoder

l Octal Bus Transceiver

l Quad 2-input OR

l 8-bit Identity Comparator

l Hamming Encoder

l Hamming Decoder

l 2-to-4 Decoder with Testbench and Configuration

l Multiplexer 16-to-4 using Selected Signal Assignment Statement

l Multiplexer 16-to-4 using Conditional Signal Assignment Statement

l Multiplexer 16-to-4 using if-then-elsif-else Statement

l M68008 Address Decoder

l Highest Priority Encoder

l N-input AND Gate

Counters

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 1 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 1 ]

Examples of VHDL Descriptions

l Counter using a Conversion Function

l Generated Binary Up Counter

l Counter using Multiple Wait Statements

l Synchronous Down Counter with Parallel Load

l Mod-16 Counter using JK Flip-flops

l Pseudo Random Bit Sequence Generator

l Universal Counter/Register

l n-Bit Synchronous Counter

Shift Registers

l Universal Shift Register/Counter

l TTL164 Shift Register

l Behavioural description of an 8-bit Shift Register

l Structural Description of an 8-bit Shift Register

Memory

l ROM-based Waveform Generator

l A First-in First-out Memory

l Behavioural model of a 16-word, 8-bit Random Access Memory

l Behavioural model of a 256-word, 8-bit Read Only Memory

State Machines

l Classic 2-Process State Machine and Test Bench

l State Machine using Variable

l State Machine with Asynchronous Reset

l Pattern Detector FSM with Test Bench

l State Machine with Moore and Mealy outputs

l Moore State Machine with Explicit State encoding

l Mealy State Machine with Registered Outputs

l Moore State Machine with Concurrent Output Logic

Systems

l Pelican Crossing Controller

l Simple Microprocessor System

l Booth Multiplier

l Lottery Number Generator

l Digital Delay Unit

l Chess Clock

ADC and DAC

l Package defining a Basic Analogue type

l 16-bit Analogue to Digital Converter

l 16-bit Digital to Analogue Converter

l 8-bit Analogue to Digital Converter

l 8-bit Unipolar Successive Approximation ADC

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 2 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 7 ]

Examples of VHDL Descriptions

Arithmetic

l 8-bit Unsigned Multiplier

l n-bit Adder using the Generate Statement

l A Variety of Adder Styles

l Booth Multiplier

Registers

l Universal Register

l Octal D-Type Register with 3-State Outputs

l Quad D-Type Flip-flop

l 8-bit Register with Synchronous Load and Clear

Universal Register

Description - This design is a universal register which can be used as a straightforward storage register, a bi-directional shift register, an up counter and a down counter. The register can be loaded from a set of parallel data inputs

and the mode is controlled by a 3-bit input. The 'termcnt' (terminal count) output goes high when the register contains zero.

LIBRARY ieee;

USE ieee.Std_logic_1164.ALL;

USE ieee.Std_logic_unsigned.ALL;

ENTITY unicntr IS

GENERIC(n : Positive := 8); --size of counter/shifter

PORT(clock, serinl, serinr : IN Std_logic; --serial inputs

mode : IN Std_logic_vector(2 DOWNTO 0); --mode control

datain : IN Std_logic_vector((n-1) DOWNTO 0); --parallel inputs

dataout : OUT Std_logic_vector((n-1) DOWNTO 0); --parallel outputs

termcnt : OUT Std_logic); --terminal count output

END unicntr;

ARCHITECTURE v1 OF unicntr IS

SIGNAL int_reg : Std_logic_vector((n-1) DOWNTO 0);

BEGIN

main_proc : PROCESS

BEGIN

WAIT UNTIL rising_edge(clock);

CASE mode IS

--reset

WHEN "000" => int_reg <= (OTHERS => '0');

--parallel load

WHEN "001" => int_reg <= datain;

--count up

WHEN "010" => int_reg <= int_reg + 1;

--count down

WHEN "011" => int_reg <= int_reg - 1;

--shift left

WHEN "100" => int_reg <= int_reg((n-2) DOWNTO 0) & serinl;

--shift right

WHEN "101" => int_reg <= serinr & int_reg((n-1) DOWNTO 1);

--do nothing

WHEN OTHERS => NULL;

END CASE;

END PROCESS;

det_zero : PROCESS(int_reg) --detects when count is 0

BEGIN

termcnt <= '1';

FOR i IN int_reg'Range LOOP

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 3 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

IF int_reg(i) = '1' THEN

termcnt <= '0';

EXIT;

END IF;

END LOOP;

END PROCESS;

--connect internal register to dataout port

dataout <= int_reg;

END v1;

Octal D-Type Register with 3-State Outputs

Simple model of an Octal D-type register with three-state outputs using two concurrent statements.

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY ttl374 IS

PORT(clock, oebar : IN std_logic;

data : IN std_logic_vector(7 DOWNTO 0);

qout : OUT std_logic_vector(7 DOWNTO 0));

END ENTITY ttl374;

ARCHITECTURE using_1164 OF ttl374 IS

--internal flip-flop outputs

SIGNAL qint : std_logic_vector(7 DOWNTO 0);

BEGIN

qint <= data WHEN rising_edge(clock); --d-type flip flops

qout <= qint WHEN oebar = '0' ELSE "ZZZZZZZZ"; --three-state buffers

END ARCHITECTURE using_1164;

Exclusive-OR Gate (Dataflow style)

-- 2 input exclusive or

-- Modeled at the RTL level.

entity x_or is

port (

in1 : in bit ;

in2 : in bit ;

out1 : out bit);

end x_or;

architecture rtl of x_or is

begin

out1 <= in1 xor in2 after 10 ns;

end rtl;

Exclusive-OR Gate (Behavioural style)

-- Exclusive or gate

-- modeled at the behavioral level.

entity x_or is

port (

in1 : in bit ;

in2 : in bit ;

out1 : out bit) ;

end x_or;

architecture behavior of x_or is

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 4 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

begin

process(in1, in2)

begin

if in1 = in2 then

out1 <= '0' after 10 ns;

else out1 <= '1' after 10 ns;

end if;

end process;

end behavior;

Exclusive-OR Gate (Structural style)

-- 2 input exclusive-or gate.

-- Modeled at the structural level.

entity x_or is

port (

in1 : in bit ;

in2 : in bit ;

out1 : out bit) ;

end x_or;

entity and_gate is

port (

a : in bit ;

b : in bit ;

c : out bit) ;

end and_gate;

architecture behavior of and_gate is

begin

process(a,b)

begin

c <= a and b after 5 ns;

end process;

end behavior;

entity or_gate is

port (

d : in bit ;

e : in bit ;

f : out bit) ;

end or_gate;

architecture behavior of or_gate is

begin

process(d,e)

begin

f <= d or e after 4 ns;

end process;

end behavior;

entity inverter is

port (

g : in bit ;

h : out bit) ;

end inverter;

architecture behavior of inverter is

begin

process(g)

begin

h <= not g after 3 ns;

end process;

end behavior;

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 5 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

architecture structural of x_or is

-- signal declarations

signal t1, t2, t3, t4 : bit;

-- local component declarations

component and_gate

port (a, b : in bit; c : out bit) ;

end component;

component or_gate

port (d, e : in bit; f : out bit) ;

end component;

component inverter

port (g : in bit; h : out bit) ;

end component;

begin

-- component instantiation statements

u0: and_gate port map ( a => t1, b => in2, c => t3);

u1: and_gate port map ( a => in1, b => t2, c => t4);

u2: inverter port map ( g => in1, h => t1);

u3: inverter port map ( g => in2, h => t2);

u4: or_gate port map ( d => t3, e => t4, f => out1);

end structural;

Three-input Majority Voter

The entity declaration is followed by three alternative architectures which achieve the same functionality in different ways.

ENTITY maj IS

PORT(a,b,c : IN BIT; m : OUT BIT);

END maj;

--Dataflow style architecture

ARCHITECTURE concurrent OF maj IS

BEGIN

--selected signal assignment statement (concurrent)

WITH a&b&c SELECT

m <= '1' WHEN "110"|"101"|"011"|"111",'0' WHEN OTHERS;

END concurrent;

--Structural style architecture

ARCHITECTURE structure OF maj IS

--declare components used in architecture

COMPONENT and2 PORT(in1, in2 : IN BIT; out1 : OUT BIT);

END COMPONENT;

COMPONENT or3 PORT(in1, in2, in3 : IN BIT; out1 : OUT BIT);

END COMPONENT;

--declare local signals

SIGNAL w1, w2, w3 : BIT;

BEGIN

--component instantiation statements.

--ports of component are mapped to signals

--within architecture by position.

gate1 : and2 PORT MAP (a, b, w1);

gate2 : and2 PORT MAP (b, c, w2);

gate3 : and2 PORT MAP (a, c, w3);

gate4 : or3 PORT MAP (w1, w2, w3, m);

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 6 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

END structure;

--Behavioural style architecture using a look-up table

ARCHITECTURE using_table OF maj IS

BEGIN

PROCESS(a,b,c)

CONSTANT lookuptable : BIT_VECTOR(0 TO 7) := "00010111";

VARIABLE index : NATURAL;

BEGIN

index := 0; --index must be cleared each time process executes

IF a = '1' THEN index := index + 1; END IF;

IF b = '1' THEN index := index + 2; END IF;

IF c = '1' THEN index := index + 4; END IF;

m <= lookuptable(index);

END PROCESS;

END using_table;

Magnitude Comparator

--VHDL description of a 4-bit magnitude comparator with expansion inputs

--first architecture demonstrates use of relational operators on

--bit vectors (=,>,<).Second architecture shows sequential behaviour

--description.Both descriptions do not fully model behaviour of real

--device for all possible combinations of inputs.

ENTITY mag4comp IS

GENERIC(eqdel,gtdel,ltdel : TIME := 10 ns); --output delay parameters

PORT(a,b : IN BIT_VECTOR(3 DOWNTO 0); --input words, DOWNTO ordering

needed for comparison operators

aeqbin,agtbin,altbin : IN BIT; --expansion inputs

aeqbout,agtbout,altbout : OUT BIT); --outputs

END mag4comp;

ARCHITECTURE dataflow OF mag4comp IS

--this architecture assumes that only one of the expansion inputs

--is active at any time,if more than one expansion input is active,

--more than one output may be active.

BEGIN

aeqbout <= '1' AFTER eqdel WHEN ((a = b) AND (aeqbin = '1'))

ELSE '0' AFTER eqdel;

agtbout <= '1' AFTER gtdel WHEN ((a > b) OR ((a = b) AND (agtbin = '1')))

ELSE '0' AFTER gtdel;

altbout <= '1' AFTER ltdel WHEN ((a < b) OR ((a = b) AND (altbin = '1')))

ELSE '0' AFTER ltdel;

END dataflow;

ARCHITECTURE behaviour OF mag4comp IS

BEGIN

PROCESS(a,b,aeqbin,agtbin,altbin)

BEGIN

IF (a > b) THEN

agtbout <= '1' AFTER gtdel;

aeqbout <= '0' AFTER eqdel;

altbout <= '0' AFTER ltdel;

ELSIF (a < b) THEN

altbout <= '1' AFTER ltdel;

aeqbout <= '0' AFTER eqdel;

agtbout <= '0' AFTER gtdel;

ELSE --a=b,expansion inputs have priority ordering

IF (aeqbin = '1') THEN

aeqbout <= '1' AFTER eqdel;

agtbout <= '0' AFTER gtdel;

altbout <= '0' AFTER ltdel;

ELSIF (agtbin = '1') THEN

agtbout <= '1' AFTER gtdel;

altbout <= '0' AFTER ltdel;

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 7 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

aeqbout <= '0' AFTER eqdel;

ELSIF (altbin = '1') THEN

agtbout <= '0' AFTER gtdel;

altbout <= '1' AFTER ltdel;

aeqbout <= '0' AFTER eqdel;

ELSE

agtbout <= '0' AFTER gtdel;

altbout <= '0' AFTER ltdel;

aeqbout <= '0' AFTER eqdel;

END IF;

END IF;

END PROCESS;

END behaviour;

8-bit Register with Synchronous Load and Clear

The design entity shows the standard way of describing a register using a synchronous process, ie. a process containing a single wait statement which is triggered by a rising edge on the clock input.

library ieee;

use ieee.std_logic_1164.all;

entity reg8 is

port(clock, clear, load : in std_logic;

d : in std_logic_vector(7 downto 0);

q : out std_logic_vector(7 downto 0));

end entity reg8;

architecture v1 of reg8 is

begin

reg_proc : process

begin

wait until rising_edge(clock);

if clear = '1' then

q <= (others => '0');

elsif load = '1' then

q <= d;

end if;

end process;

end architecture v1;

BCD to Seven Segment Decoder

The use of the std_logic literal '-' (don't care) is primarily for the synthesis tool. This example illustrates the use of the selected signal assignment.

LIBRARY ieee;

USE ieee.std_logic_1164.ALL;

ENTITY seg7dec IS

PORT(bcdin : IN std_logic_vector(3 DOWNTO 0);

segout : OUT std_logic_vector(6 DOWNTO 0));

END seg7dec;

ARCHITECTURE ver3 OF seg7dec IS

BEGIN

WITH bcdin SELECT

segout <= "1000000" WHEN X"0",

"1100111" WHEN X"1",

"1101101" WHEN X"2",

"0000011" WHEN X"3",

"0100101" WHEN X"4",

"0001001" WHEN X"5",

"0001000" WHEN X"6",

"1100011" WHEN X"7",

"0000000" WHEN X"8",

h t t p : / / w w w . a m i . b o l t o n . a c . u k / c o u r s e w a r e / a d v e d a / v h d l / v h d l e x m p . h t m l ( 8 o f 6 7 ) [ 2 3 / 1 / 2 0 0 2 4 : 1 5 : 0 8 ]

Examples of VHDL Descriptions

显示全文