l15 – specification of state machines. vhdl state machines state machine basics vhdl for...

28
L15 – Specification of State Machines

Upload: peyton-rex

Post on 29-Mar-2015

253 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

L15 – Specification of State Machines

Page 2: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light controller Example – counter Example – gray code counter Ref: text Unit 10, 17, 20

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 2

Page 3: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

State Machine Basics Mealy machine – outputs are a function of current

state and current inputs.

Moore machine – outputs are a function of the current state only.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 3

NextStateLogic

StateMemory

(F/Fs)

OutputLogic

OutputsInputs

CurrentState

Excitation(next state)

Mealy Machine

clk

NextStateLogic

StateMemory(F/Fs)

OutputLogic

OutputsInputs

CurrentState

Excitation(next state)

Moore Machine

clk

Page 4: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

State machine design To implement the state machine have option

to use Traditional methodology – state graph, state

table, state assignment, K-maps, implementation HDL methodology

HDL description directly from word description State graph and then the HDL description

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 4

Page 5: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

In HDLs need state elements HDL code for the F/Fs

A simple rising edge D Flip Flop ARCHITECTURE xxx OF yyy IS BEGIN PROCESS BEGIN WAIT UNTIL clk=‘1’ AND clk’event; state <= next_state; END PROCESS;

Semantics : Process runs at time 0 and then holds for clock to have an event (change value) and the new value is a ‘1’.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 5

Page 6: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Another Form This is an alternative to the previous HDL

ARCHITECTURE xxx OF yyy IS BEGIN PROCESS (clk) BEGIN IF (clk=‘1’ AND clk’event) THEN state <= next_state; END IF; END PROCESS;

Semantics – Process runs once at time 0 and then holds until signal clk has an event. It then executes the IF statement.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 6

Page 7: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

D F/F with reset ENTITY dff IS PORT (clk,reset,din : IN bit; qout : OUT bit); END dff;

ARCHITECTURE one of dff IS -- Entity had sig clk,reset,din: IN and qout:OUT -- td_reset and td_in are constants. BEGIN PROCESS (clk) BEGIN IF (clk=‘0’ AND clk’event) THEN IF (reset =‘1’) THEN qout <=‘0’ AFTER td_reset; ELSE qout <= din AFTER td_in; END IF; END IF; END PROCESS; END one;

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 7

Page 8: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

More complete D F/F PROCESS (clk,preset,clear,next_state) BEGIN -- active low preset and clear

IF (preset = ‘0’) THEN state <= pr_state; --preset state ELSIF (clear = ‘0’) THEN state <= clr_state; --clear state ELSIF (clk = ‘1’ AND clk’event) THEN --rising edge state <= next_state; END IF;

END PROCESS; Semantics – runs once at startup. Then whenever any of the

signals in the sensitivity list change value. Asynchronous preset has priority over clear and a clock edge. clear has priority over a clock edge.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 8

Page 9: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

VHDL for finite state machines Use a process to describe the next state logic, often in

a case statement used for determination of the next state. Will see this in the example.

Use a process to represent the latching/loading of the calculated next_state such that it becomes the current_state. This is the only process that generates sequential elements (F/Fs). The other processes represent combinational logic.

Use a third process to represent the generation of the outputs from the current state and possibly the inputs. This process will have as its inputs, the current state and, depending on the type of state machine, the state machine inputs.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 9

Page 10: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Notes on VHDL style The style applies to any HDL – VHDL, Verilog,

System C Documents well Easily maintainable – excellent during development

as changes are easily made Style maps to physical logic – using this style can

predict the number of state elements (~) that should be produced by synthesis

All three styles on the last slide simulate equally well, but this style also synthesizes well. Works in XILINX and Altera tools.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 10

Page 11: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Example The T-Bird tail light problem

The turn signal indicator had a light sequence on each lamp.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 11

Page 12: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

State machine description State Diagram and Transition Table Output is associated with state – a Moore

machine

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 12

Page 13: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Goal is a HDL description Often you still start with a state diagram and

state table Where to start with the code?

As with all HDL – start with the interface WHAT ARE THE INPUTS AND OUTPUTS?

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 13

Page 14: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

ENTITY for the controller Inputs are a signal for

Right turn signal Left turn signal Hazard Clock

Outputs are signals for the taillights lc, lb, la rc, rb, ra

HDL code for the entity

ENTITY t_bird IS PORT(rts,lts,haz : IN bit; clk : IN bit; lc,lb,la : OUT bit; ra,rb,rc : OUT bit); END t_bird;

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 14

Page 15: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

For the tail light controller Inputs are signals for

Right Turn Signal Left Turn Signal Hazard Clock

Outputs are the signals for the lights la,lb,la ra,rb,rc

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 15

Page 16: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

The ARCHITECTURE Will use 3 processes In declarative region of ARCHITECTURE

will declare the state_type for the states. ARCHITECTURE state_machine OF t_bird IS TYPE state_type IS (idle,l1,l2,l3,r1,r2,r3,lr3); SIGNAL state,next_state : state_type; BEGIN

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 16

Page 17: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

1st Process – sequential elements Use a process to specify the sequential elements Here need only simple D F/Fs

-- Process to specify F/Fs PROCESS BEGIN WAIT UNTIL clk=‘1’ AND clk’event; state <= next_state; END PROCESS;

This is the only part of the description that should result in state elements from synthesis.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 17

Page 18: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

2nd Process – next state generation What is the next state

given the current state and the value present on the inputs?

This process can be of considerable size.

Work well using a case statement.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 18

-- Next State Logic Process PROCESS (state,lts,rts,haz) BEGIN CASE state IS WHEN idle => IF (haz=’1’ OR (lts=’1’ AND rts=’1’) THEN next_state <= lr3; ESLIF(haz=”0’ AND (lts=’0’ AND rts=’1’)) THEN next_state <= r1; ELSIF(haz=’0’ AND (lts=’1’ AND rts=’0’)) THEN next_state <= l1; ELSE next_state <= idle; END IF;

Page 19: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Next State Process Code continued A separate action for

each state that based on the inputs directs the value assigned to next_state.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 19

WHEN l1 => IF (haz = ‘1’) THEN next_state <= lr3; ELSE next_state <= l2; END IF; WHEN l2 => IF (haz = ‘1’) THEN next_state <= lr3; ELSE next_state <= l3; END IF; WHEN l3 => next_state <= idle; WHEN r1 => IF (haz = ‘1’) THEN next_state <= lr3; ELSE next_state <= r2; END IF;

WHEN r2 => IF (haz = ‘1’) THEN next_state <= lr3; ELSE next_state <= r3; END IF; WHEN r3 => next_state <= idle; WHEN lr3 => next_state <= idle; END CASE; END PROCESS;

Page 20: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

3rd Process – Output signals A separate process is

used to generate the final outputs.

Works great for Moore type implementations.

Outputs are directly assigned to.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 20

-- State Machine Outputs Process PROCESS (state) BEGIN CASE state IS WHEN idle =>

lc<=’0’;lb<=’0’;la<=’0’;ra<=’0’;rb<=’0’;rc<=’0’; WHEN l1 =>

lc<=’0’;lb<=’0’;la<=’1’;ra<=’0’;rb<=’0’;rc<=’0’; WHEN l2 =>

lc<=’0’;lb<=’1’;la<=’1’;ra<=’0’;rb<=’0’;rc<=’0’; WHEN l3 =>

lc<=’1’;lb<=’1’;la<=’1’;ra<=’0’;rb<=’0’;rc<=’0’; WHEN r1 =>

lc<=’0’;lb<=’0’;la<=’0’;ra<=’1’;rb<=’0’;rc<=’0’; WHEN r2 =>

lc<=’0’;lb<=’0’;la<=’0’;ra<=’1’;rb<=’1’;rc<=’0’; WHEN r3 =>

lc<=’0’;lb<=’0’;la<=’0’;ra<=’1’;rb<=’1’;rc<=’1’; WHEN lr3 =>

lc<=’1’;lb<=’1’;la<=’1’;ra<=’1’;rb<=’1’;rc<=’1’; END CASE:

END PROCESS; END state_machine;

Page 21: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Complete VHDL code (no output) ENTITY t_bird IS PORT(rts,lts,haz, : IN bit; clk : IN bit; lc,lb,la : OUT bit; ra,rb,rc : OUT bit); END t_bird; ARCHITECTURE state_mach OF t_bird IS

TYPE state_type IS (idle,l1,l2,l3,r1,r2,r3,lr3);

SIGNAL state,next_state : state_type; BEGIN

-- Process to specify F/Fs PROCESS BEGIN WAIT UNTIL clk=‘1’ AND

clk’event; state <= next_state; END PROCESS;

-- next state logic PROCESS BEGIN CASE state IS WHEN idle => IF(haz=‘1’ OR (lts=‘1’ AND rts =‘1’) THEN next_state <= lr3; ELSIF (haz=‘0’ AND (lts=‘0’ AND rts=‘1’) THEN next_state<=r1; ELSIF (haz=‘0’ AND (lts=‘’1’ AND rts=‘0’) THEN next_state<=l1; ELSE next_state <= idle; END IF: WHEN l1 => IF (haz=‘1’) THEN next_state <= lr3; ELSE next_state <= l2; END IF; WHEN l2 => IF (haz=‘1’) THEN next_state <= lr3; ELSE next_state <= l3; END IF; WHEN l3 => next_state <= idle; WHEN r1 => IF (haz=‘1’) THEN next_state <= lr3; ELSE next_state <= r2; END IF; WHEN r2 => IF (haz=‘1’) THEN next_state <= lr3; ELSE next_state <= r3; END IF; WHEN r3 => next_state <= idle; WHEN lr3 => next_state <= idle; END CASE; END PROCESS;

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 21

Page 22: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Having HDL description Simulate it in a test suite to verify the design

meets specifications. This is the HDL topic of verification.

For this course will construct simple testbenches to do simple check.

HDL code can also be synthesized.

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 22

Page 23: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Results of synthesis From a Mentor graphics tool (several years

back)

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 23

Page 24: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

From FPGA tools When done in

Quartis – ALTERA FPGA tool

Use the state machine VHDL code for synthesis

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 24

ENTITY t_bird IS PORT(clk,lts,rts,haz : IN bit; lc,lb,la,ra,rb,rc : OUT bit);END t_bird;

ARCHITECTURE one OF t_bird IS TYPE state_type IS (idle,l1,l2,l3,r1,r2,r3,lr3); SIGNAL state,next_state : state_type;BEGIN --process for F/F PROCESS BEGIN WAIT UNTIL clk='1' AND clk'event; state <= next_state; END PROCESS; --next state generation PROCESS (state,rts,lts,haz) BEGIN CASE state IS WHEN idle => IF (haz='1' OR (lts='1' AND rts='1')) THEN next_state <= lr3; ELSIF (haz='0' AND lts='0' AND rts='1') THEN next_state <= r1; ELSIF (haz='0' AND lts='1' AND rts='0') THEN next_state <= l1; ELSE next_state <= idle; END IF; WHEN l1 => IF (haz='1') THEN next_state <= lr3; ELSE next_state <= l2; END IF; WHEN l2 => IF (haz='1') THEN next_state <= lr3; ELSE next_state <= l3; END IF; WHEN l3 => next_state <= idle; WHEN r1 => IF (haz='1') THEN next_state <= lr3; ELSE next_state <= r2; END IF; WHEN r2 => IF (haz='1') THEN next_state <= lr3; ELSE next_state <= r3; END IF; WHEN r3 => next_state <= idle; WHEN lr3 => next_state <= idle; END CASE; END PROCESS; -- the output process PROCESS (state) BEGIN CASE state IS WHEN idle => lc<='0'; lb<='0'; la<='0'; ra<='0'; rb<='0'; rc<='0'; WHEN l1 => lc<='0'; lb<='0'; la<='1'; ra<='0'; rb<='0'; rc<='0'; WHEN l2 => lc<='0'; lb<='1'; la<='1'; ra<='0'; rb<='0'; rc<='0'; WHEN l3 => lc<='1'; lb<='1'; la<='1'; ra<='0'; rb<='0'; rc<='0'; WHEN r1 => lc<='0'; lb<='0'; la<='0'; ra<='1'; rb<='0'; rc<='0'; WHEN r2 => lc<='0'; lb<='0'; la<='0'; ra<='1'; rb<='1'; rc<='0'; WHEN r3 => lc<='0'; lb<='0'; la<='0'; ra<='1'; rb<='1'; rc<='1'; WHEN lr3 => lc<='1'; lb<='1'; la<='1'; ra<='1'; rb<='1'; rc<='1'; END CASE; END PROCESS;END one;

Page 25: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

FPGA results From the report

Combination LUTs – 15 Dedicated logic registers – 8 (did a one hot

encoding) Total pins 10

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 25

Page 26: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

The schematic Big block for state elements

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 26

Page 27: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

The implementation The one hot state machine state diagram

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 27

Page 28: L15 – Specification of State Machines. VHDL State Machines State Machine Basics VHDL for sequential elements VHDL for state machines Example – Tail light

Lecture summary VHDL for state machines T_bird tail light controller example

VHDL Synthesis results

9/2/2012 – ECE 3561 Lect 9

Copyright 2012 - Joanne DeGroat, ECE, OSU 28