XSVHD: Sequential VHDL Examples: Xsvhd.1 T-Bird Tail Lights
XSVHD: Sequential VHDL Examples: Xsvhd.1 T-Bird Tail Lights
XSVHD: Sequential VHDL Examples: Xsvhd.1 T-Bird Tail Lights
XSvhd1
CALIFORNIA
ZOTTFFS
LC
LB
LA
RA
RB
RC
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd2
IDLE MUSINGS
LR3;
L1;
R1;
IDLE;
if;
if;
if;
if;
In VHDL state machines, its not necessary to make an explicit assignment of a next
state if its the same state that the machine is already in. In the execution of a process,
a VHDL signal keeps its value if no assignment is made to it. Thus, in
Table XSvhd-1, the final else clause in the IDLE state could be omitted, with no
effect on the machines behavior.
Separately, the robustness of the state machine in Table XSvhd-1 could be
improved by replacing the null statement in the when others case with a
transition to the IDLE state.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd3
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd4
<=
<=
<=
<=
<=
'1'
'1'
'1'
'1'
'1'
when
when
when
when
when
Sreg
Sreg
Sreg
Sreg
Sreg
=
=
=
=
=
S1
S2
S3
S4
SERR
else
else
else
else
else
<= SERR;
<= SOK;
<= S2;
<= SERR;
<= SOK;
<= S3;
<= SERR;
<= SOK;
<= S4;
<= SERR;
<= SOK;
<= S1;
G4='0'
'0';
'0';
'0';
'0';
'0';
end Vggame_arch;
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd5
Ta ble XS v hd- 3
VHDL architecture
for guessing game
using output-coded
state assignment.
NSRED
NSYELLOW
NSGREEN
NSGREEN
EWRED
EWYELLOW
EWRED
EWYELLOW
EWGREEN
EWGREEN
NSCAR
EWCAR
EWCAR
NSCAR
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd6
STD_LOGIC;
STD_LOGIC;
STD_LOGIC;
STD_LOGIC );
XSvhd7
end Vsvale_arch;
has the sensors and signals shown in Figure XSvhd-2 below. The state machine
that controls the traffic signals uses a 1-Hz clock and a timer and has four inputs:
NSCAR Asserted when a car on the north-south road is over either sensor
XSvhd8
While writing the program, we took the opportunity to add two inputs that
werent in the original specification. The OVERRIDE input may be asserted by
the police to disable the controller and put the signals into a flashing-red mode at
a rate determined by the FLASHCLK input. This allows them to manually clear
up the traffic snarls created by this wonderful invention.
Like most of our other examples, Table XSvhd-4 does not give a specific
state assignment. And like many of our other examples, this state machine works
well with an output-coded state assignment. Many of the states can be identified
T ab le X S v hd- 6 Definitions for Sunnyvale traffic-lights machine with output-coded
state assignment.
library IEEE;
use IEEE.std_logic_1164.all;
entity Vsvale is
port ( CLOCK, RESET, NSCAR, EWCAR, TMSHORT, TMLONG: in
OVERRIDE, FLASHCLK:
in
NSRED, NSYELLOW, NSGREEN:
out
EWRED, EWYELLOW, EWGREEN, TMRESET:
out
end;
STD_LOGIC;
STD_LOGIC;
STD_LOGIC;
STD_LOGIC );
<=
<=
<=
<=
<=
<=
<=
end Vsvaleoc_arch;
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd9
by a unique combination of light-output values. But there are three pairs of states
that are not distinguishable by looking at the lights alone: (NSWAIT, NSWAIT2),
(EWWAIT, EWWAIT2), and (NSDELAY, EWDELAY). We can handle these by adding
one more state variable, EXTRA, that has different values for the two states in
each pair. This idea is realized in the modified program in Table XSvhd-5.
XSvhd.4 A Synchronous System Design Example
This subsection presents a representative example of a synchronous system
design in VHDL. The example is a shift-and-add multiplier for unsigned integers using the algorithm of Section 2.8. Besides illustrating synchronous design
(using a single clock), this example also shows the hierarchical possibilities of
design with VHDL.
As shown in Figure XSvhd-3, the multiplier design has five individual
modules nested up to three levels deep. The top level is broken into a datapath
and a control unit, and the control unit contains both a state machine and a
counter. The design also creates and all of the entities use the MPYdefs package
shown in Table XSvhd-6. By changing the constant MPYwidth in this package,
the designer can create a shift-and-add multiplier of any desired width; well use
a width of 8 in the rest of this discussion.
package MPYdefs is
constant MPYwidth: integer := 8;
constant MPYmsb:
integer := MPYwidth-1;
constant PRODmsb:
integer := 2*MPYwidth-1;
constant MaxCnt:
integer := MPYmsb;
subtype CNTRrange is integer range 0 to MaxCnt;
type SMstate is (IDLE, INIT, RUN, VAIT);
shift-and-add multiplier
T ab le X S v h d - 7
Common definitions
for shift-and-add
multiplier.
end MPYdefs;
MPYdefs
MPY8x8
MPYdata
F i g u r e X S vh d- 3
VHDL entities and
package used in the
shift-and-add
multiplier.
MPYctrl
MPYsm
MPYcntr
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
HP7
HP0
HPROD
MC7
+
F8
MPY7
XSvhd10
MPY0
MPY/LPROD
MC0
shift
MCND
F0
F i g u r e X S vh d- 4
Registers and
functions used by
the shift-and-add
multiplication
algorithm.
XSvhd11
CLOCK
RESET
START
DONE
INP
mcnd
mpy
mcnd
PROD
SM
Count
mpy
VALID
IDLE
INIT
RUN
RUN
RUN
RUN
RUN
RUN
RUN
RUN
VAIT
IDLE
INIT
multiplication.
START An input that is asserted prior to a rising clock edge to begin a
multiplication. START must be negated before asserting it again
will start a new multiplication.
DONE An output that is asserted when the multiplication is done and
PROD[15..0] is valid.
A timing diagram for the multiplier system is shown in Figure XSvhd-5.
The first six waveforms show the input/output behavior and how a multiplication
takes place in 10 or more clock periods as described below:
1. START is asserted. The multiplicand is placed on the INP bus and is
loaded into the MCND register at the end of this clock period.
2. The multiplier is placed on the INP bus and is loaded into the MPY
register at the end of the clock period.
310. One shift-and-add step is performed at each of the next eight clock ticks.
Immediately following the eighth clock tick, DONE should be asserted
and the 16-bit product should be available on PROD[15..0]. A new
multiplication can also be started during this clock tick, but it may be
started later.
Our control unit MPYcntrl for running the multiplication is based on a
decomposed state machine, as introduced in Section 7.8. A top-level state
machine MPYsm controls the overall operation, while a counter MPYcntr counts
the eight shift-and-add steps. These three VHDL entities are shown in tables on
the next three pages.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd12
in STD_LOGIC;
in STD_LOGIC;
out STD_LOGIC;
out SMstate
);
end component;
component MPYcntr port (RESET, CLK: in STD_LOGIC;
SM:
in SMstate;
MAX:
out STD_LOGIC );
end component;
signal MAX: STD_LOGIC;
signal SMi: SMstate;
begin
U1: MPYsm
port map (RESET=>RESET, CLK=>CLK, START=>START, MAX=>MAX, SM=>SMi);
U2: MPYcntr port map (RESET=>RESET, CLK=>CLK, SM=>SMi, MAX=>MAX);
process (CLK) -- implement DONE output function
begin
if CLK'event and CLK='1' then
if RESET='1' then DONE <= '0';
elsif (SMi=RUN and MAX='1') or (SMi=VAIT) then DONE <= '1';
else DONE <= '0';
end if;
end if;
end process;
SM <= SMi; -- Output copy of SM state, visible to other entities
end MPYctrl_arch;
As shown in Table XSvhd-7, the control unit MPYctrl instantiates the state
machine and the counter, and also has a small process to implement the DONE
output function which requires a register. Notice how input signals RESET, CLK,
and START simply flow through MPYctrl and become inputs of MPYsm and
MPYcntr. Also notice how a local signal, SMi, is declared to receive the state
from MPYsm and deliver it both to SMcntr and the output of MPYctrl.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd13
in STD_LOGIC;
in STD_LOGIC;
in STD_LOGIC;
out SMstate );
The MPYsm state machine has four states for multiplier control. Multiplication begins when START is asserted. The machine goes to the INIT state and then
the RUN state, and stays in the RUN state until the MAX input, produced by the
MPYcntr entity, is asserted after eight clock ticks. Then it goes to the IDLE or the
VAIT state, depending on whether or not START has been negated yet. (VAIT is
named strangely to avoid conflict with the VHDL keyword wait.)
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
library IEEE;
use IEEE.std_logic_1164.all;
use work.MPYdefs.all;
XSvhd14
Ta ble XS v hd- 10
VHDL counter entity
MPYcntr.
entity MPYcntr is
port (RESET, CLK: in STD_LOGIC;
SM:
in SMState;
MAX:
out STD_LOGIC );
end;
architecture MPYcntr_arch of MPYcntr is
signal Count: CNTRrange;
begin
process (CLK)
begin
if CLK'event and CLK='1' then
if RESET='1' then Count <= 0;
elsif SM=RUN then Count <= (Count + 1) mod MPYwidth;
else Count <= 0;
end if;
end if;
end process;
MAX <= '1' when Count = MaxCnt else '0';
end MPYcntr_arch;
The MPYcntr entity counts from 0 to MaxCnt (MPYwidth-1) when the state
machine is in the RUN state. The state-machine states and counter values during
an 8-bit multiplication sequence were shown in the last two waveforms in
Figure XSvhd-5.
The multiplier data path logic is defined in the MPYdata entity, shown in
Table XSvhd-10. This entity declares local registers MPY, MCND, and HPROD. Note
that these all have type UNSIGNED from the std_logic_arith package, so that
the addition operation is supported. Note the use of concatenation to pad the
addends to nine bits in the addition operation that assigns a value to F near the
end of the module.
STOP
COMPLAINING!
In Table XSvhd-9, notice the mod operation that forces the counter state back to 0
after state MaxCnt (MPYwidth-1). This adds a little bit of logic to the counters
realization if MPYwidth is not a power of two, and its not really needed for proper
system operation. However, during simulation, the VHDL simulator will complain
if Count takes on a value outside CNTRrange. So this extra code and logic were
added simply to make the simulator stop complaining.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use work.MPYdefs.all;
XSvhd15
Ta ble XS v hd- 11
VHDL data-path
entity MPYdata.
entity MPYdata is
port (RESET, CLK, START: in STD_LOGIC;
INP:
in UNSIGNED (MPYmsb downto 0);
SM:
in SMstate;
PROD:
out UNSIGNED (PRODmsb downto 0) );
end;
architecture MPYdata_arch of MPYdata is
signal MPY, MCND, HPROD: UNSIGNED (MPYmsb downto 0);
signal F: UNSIGNED (MPYmsb+1 downto 0);
begin
process (CLK) -- implement registers
begin
if CLK'event and CLK='1' then
if RESET='1' then
MPY <= (others=>'0'); MCND <= (others=>'0');
HPROD <= (others=>'0'); -- clear registers on reset
elsif (SM=IDLE and START='1') then -- load MCND, clear HPROD
MCND <= INP; HPROD <= (others => '0');
elsif SM=INIT
then MPY <= INP; -- load MPY
elsif SM=RUN then
MPY <= F(0) & MPY(MPYmsb downto 1);
HPROD <= F(MPYmsb+1 downto 1);
end if;
end if;
end process;
F <= ('0' & HPROD) + ('0' & MCND) when MPY(0)='1'
else ('0' & HPROD);
PROD <= HPROD & MPY;
end MPYdata_arch;
Besides the RESET, CLK, and INP inputs and the PROD output, which you
would naturally need for the data path, this entity also has START and the statemachine state SM as inputs. These are needed to determine when to load the MPY
and MCND registers, and when to update the partial product (in the RUN state).
The last statement in this module produces the PROD output as a combinational concatenation of the HPROD and MPY registers.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd16
in STD_LOGIC;
in STD_LOGIC;
in UNSIGNED (MPYmsb downto 0);
out STD_LOGIC;
out UNSIGNED (PRODmsb downto 0) );
VERY VERBOSE
VHDL
Comparing this examples roughly five pages of VHDL in six individual files with
the ABEL description of the same function in less than two pages in Section XSabl.6,
you probably get the sense that hierarchy might be nice but it has a price. However,
this verboseness is due much more to the nature of VHDL than to hierarchy.
For example, the component port definitions had to be replicated in every
entity that instantiates another, the library definitions were repeated everywhere, and
VHDL is just downright verbose even for the simplest things (e.g., compare VHDLs
MPYmsb downto 0 with ABELs MPYmsb..0 or Verilogs MPYmsb:0). The
same design in Verilog is much more compact, as shown in Section XSver.4.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd17
MPY8x8_tb of MPY8x8_tb is
RST, START, DONE: STD_LOGIC;
UNSIGNED (MPYmsb downto 0);
UNSIGNED (PRODmsb downto 0);
XSvhd18
Exercises
XSvhd.1 Design a clocked synchronous state machine that checks parity on a serial
byte-data line with timing similar to Figure XSbb-3 in Section XSbb.1. The
circuit should have three inputs, RESET, SYNC, and DATA, in addition to
CLOCK, and one Moore-type output, ERROR. The ERROR output should be
asserted if any DATA byte received since reset had odd parity. Using VHDL,
devise a state machine that does the job using no more than four states.
Include comments to describe each states meaning and use. Write a VHDL
test bench that checks your machine for proper operation by applying three
bytes in succession with even, odd, and even parity.
XSvhd.2 Enhance the state machine in the preceding exercise by also asserting
ERROR if SYNC was not asserted within eight clock ticks after the machine
starts up after reset, or if any SYNC pulses fail to be exactly eight clock ticks
apart. Augment the test bench to check for proper machine operation in the
newly defined conditions.
XSvhd.3 Using VHDL, design a clocked synchronous state machine with two inputs,
INIT and X, and one Moore-type output Z. As long as INIT is asserted, Z is
continuously 0. Once INIT is negated, Z should remain 0 until X has been 0
for two successive ticks and 1 for two successive ticks, regardless of the order
of occurrence. Then Z should go to 1 and remain 1 until INIT is asserted
again. Write a VHDL test bench that checks your design for proper operation. (Hint: No more than ten states are required.)
XSvhd.4 Using VHDL, design a parallel-to-serial conversion circuit with eight 2.048Mbps, 32-channel serial links and a single 2.048-MHz, 8-bit, parallel data
bus that carries 256 bytes per frame. Each serial link should have the frame
format defined in Figure XSbb-3 in Section XSbb.1. Each serial data line
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.
XSvhd19
SDATAi should have its own sync signal SYNCi; the sync pulses should be
staggered so that SYNCi + 1 has a pulse one tick after SYNCi. Show the
XSvhd.5
XSvhd.6
XSvhd.7
XSvhd.8
XSvhd.9
XSvhd.10
timing of the parallel bus and the serial links, and write a table or formula
that shows which parallel-bus timeslots are transmitted on which serial links
and timeslots. Create a test bench that checks your design by applying at least
one frames worth of sequential parallel data (256 bytes, 0 through 255) to
your circuit, and checking for the corresponding data on the serial data lines.
In the same environment as the preceding exercise, design a serial-to-parallel
conversion circuit that converts eight serial data lines into a parallel 8-bit data
bus. Create a test bench that connects the SDATAi outputs of the preceding
exercise with the SDATAi inputs of this one. It should check the two circuits
together by applying random parallel data bytes to the inputs of the first and
looking for them at the output of the second, a certain number of clock ticks
later.
Redesign the T-bird tail-lights machine of Section XSvhd.1 to include
parking-light and brake-light functions. When the BRAKE input is asserted,
all of the lights should go on immediately, and stay on until BRAKE is
negated, independent of any other function. When the PARK input is
asserted, each lamp is turned on at 50% brightness at all times when it
would otherwise be off. This is achieved by driving the lamp with a 100-Hz
signal DIMCLK with a 50% duty cycle. Partition the VHDL design into as
many entities as you feel are appropriate, but target the design to a single
CPLD or FPGA. Also, write a short description of how your system works.
The operation of the guessing game in Section XSvhd.2 is very predictable;
its easy for a player to learn the rate at which the lights change and always
hit the button at the right time. The game is more fun if the rate of change is
more variable.
Modify the VHDL guessing-game program of Table XSvhd-2 so that in
states S1S4, the machine advances only if a new input, SEN, is asserted. (SEN
is intended to be hooked up to a pseudorandom bit-stream generator.) Button
pushes should be recognized whether or not SEN is asserted.
Also, add another process to the program to provide a pseudorandom bitstream generator using an 8-bit LFSR. After how many clock ticks does the
bit sequence repeat? What is the maximum number of 0s that occur in a row?
What is the maximum number of 1s? How can you double these numbers?
Modify the behavior of the VHDL traffic-light-controller machine in
Table XSvhd-4 to have more reasonable behavior, the kind youd like to see
for traffic lights in your own home town.
Using VHDL, design a data unit and a control-unit state machine for multiplying 8-bit twos-complement numbers using the algorithm discussed in
Section 2.8.
Using VHDL, design a data unit and control-unit state machine for dividing
8-bit unsigned numbers using the shift-and-subtract algorithm discussed in
Section 2.9.
Supplementary material to accompany Digital Design Principles and Practices, Fourth Edition, by John F. Wakerly.
ISBN 0-13-186389-4. 2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
This material is protected under all copyright laws as they currently exist. No portion of this material may be
reproduced, in any form or by any means, without permission in writing by the publisher.