Docsity

VHDL Implementation of a 3-bit Binary Counter, Slides of Digital Systems Design

Partial preview of the text

Download VHDL Implementation of a 3-bit Binary Counter and more Slides Digital Systems Design in PDF only on Docsity!

L16 – VHDL for State Machines

with binary encoding

VHDL – binary encoded states

• Another example

– Counter – enumeration states

– Binary encoded states

– Gray Code

– Controlled counters

• Ref: text Unit 10, 17, 20

Start the HDL

• This is a problem that one should be able to

do without a state diagram to start.

• The entity

– ENTITY cnt3bit IS

– PORT(clk : IN bit;

– cnt : OUT bit_vector(2 downto 0));

– END cnt3bit;

The ARCHITECTURE

  • ARCHITECTURE one OF cnt3bit IS
  • TYPE state_type IS (s0,s1,s2,s3,s4,s5,s6,s7);
  • SIGNAL state,next_state : state_type;
  • BEGIN
  • --F/F process
  • PROCESS
  • BEGIN
  • WAIT UNTIL clk='1' AND clk'event;
  • state <= next_state;
  • END PROCESS;

The output process

  • --output process
  • PROCESS (state)
  • BEGIN
  • CASE state IS
  • WHEN s0 => cnt <= "000";
  • WHEN s1 => cnt <= "001";
  • WHEN s2 => cnt <= "010";
  • WHEN s3 => cnt <= "011";
  • WHEN s4 => cnt <= "100";
  • WHEN s5 => cnt <= "101";
  • WHEN s6 => cnt <= "110";
  • WHEN s7 => cnt <= "111";
  • END CASE;
  • END PROCESS;
  • END one; --end of the architecture

Notes on code

• Standard VHDL coding style

– An enumeration type for coding of the state

– Signals for state and state type

– 3 processes for modeling

st

process – latching of next_state into state

nd

process – generation of next_state

  • Here simple as no control signals

rd

process – generation of output given the state

– a Moore machine.

In the testbench arch

  • BEGIN
  • -- As tb has clk will never go quiescent
  • -- set up clk 50% duty cycle 10 ns period
  • clk <= not clk AFTER 5 ns;
  • -- instantiate component
  • u1 : cnt3bit PORT MAP (clk,cnt);
  • END one;

• Testbench is very simple – apply the clk and

instantiate the component.

The VHDL simulation

• Waveform shows binary counting output

Compare to a binary encoding

• Code the binary coding straight into VHDL

• ENTITY is the same

• Declarative region of the ARCHITECTURE

– Declarations are different

– ARCHITECTURE two OF cnt3bit IS

– SIGNAL state,next_state : bit_vector(2 downto 0);

– BEGIN

And in the processes

– -- latching of next_state

– PROCESS

– BEGIN

– WAIT UNTIL clk='1' AND clk'event;

– state <= next_state;

– END PROCESS;

– No difference here as both state and next_state

are of the same type. (just bit_vector now)

Output Process

• For this style the output process is simply

assigning the state to the output as this is a

Moore machine. This can be done with a

concurrent signal assignment statement.

– cnt <= state;

– END one;

Modify the testbench

  • In the declarative region add the 2 nd

arch and cnt for the 2

nd

arch

  • (note change in instantiation labels)
    • ARCHITECTURE one OF testcnt3bit IS
    • COMPONENT cnt3bit IS
    • PORT (clk : IN bit;
    • cnt : OUT bit_vector(2 downto 0));
    • END COMPONENT;
    • FOR u1 : cnt3bit USE ENTITY work.cnt3bit(one);
    • FOR binenc : cnt3bit USE ENTITY work.cnt3bit(two);
    • -- declare signal for stimulus and response of the DUT
    • SIGNAL clk : bit;
    • SIGNAL cnt,cntbin : bit_vector(2 downto 0);
    • BEGIN

Simulation

• Result from simulation

Results from Quartis

• Generating the FPGA for cnt3bitbin

• This time encoding prevented choice of

one-hot methodology.

• Conclusion:

– Tool is not always right!!