digital design: an embedded systems approach using vhdl chapter 4 sequential basics portions of this...

77
Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded Systems Approach Using VHDL, by Peter J. Ashenden, published by Morgan Kaufmann Publishers, Copyright 2007 Elsevier Inc. All rights reserved.

Upload: dorothy-waters

Post on 27-Dec-2015

237 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design:An Embedded Systems Approach Using VHDL

Chapter 4Sequential Basics

Portions of this work are from the book, Digital Design: An Embedded Systems Approach Using VHDL, by Peter J. Ashenden, published by Morgan Kaufmann Publishers, Copyright 2007 Elsevier Inc. All rights reserved.

Page 2: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 2

VHDL

Sequential Basics

Sequential circuits Outputs depend on current inputs and

previous inputs Store state: an abstraction of the

history of inputs Usually governed by a periodic

clock signal

Page 3: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 3

VHDL

D-Flipflops

1-bit storage element We will treat it as a basic component

Other kinds of flipflops SR (set/reset), JK, T (toggle)

D Q

clkD

clk

Q

Page 4: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 4

VHDL

Registers

Store a multi-bit encoded value One D-flipflop per bit Stores a new value on

each clock cycle

signal d, q: ...;...

reg: process (clk) isbegin if rising_edge(clk) then q <= d; end if;end process reg;

sensitivity list

D Q

clk

D Q

clk

D Q

clk

d(0)

…… …

d(1)

d(n)

n n

q(0)

q(1)

q(n)clk

D Q

clk

Page 5: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 5

VHDL

Pipelines Using RegistersTotal delay = Delay1 + Delay2 + Delay3

Interval between outputs > Total delay

Clock period = max(Delay1, Delay2, Delay3)

Total delay = 3 × clock period

Interval between outputs = 1 clock period

D Q

clk

combin-ational

circuit 1

D Q

clk

combin-ational

circuit 2

D Q

clk

combin-ational

circuit 3

d_in

clk

d_out

combin-ational

circuit 1

combin-ational

circuit 2

combin-ational

circuit 3d_in

Page 6: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 6

VHDL

Pipeline Example

Compute the average of corresponding numbers in three input streams New values arrive on each clock edge

library ieee;use ieee.std_logic_1164.all, ieee.fixed_pkg.all;

entity average_pipeline is port ( clk : in std_logic; a, b, c : in sfixed(5 downto -8); avg : out sfixed(5 downto -8) );end entity average_pipeline;

Page 7: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 7

VHDL

Pipeline Example

architecture rtl of average_pipeline is

signal a_plus_b, sum, sum_div_3 : sfixed(5 downto -8); signal saved_a_plus_b, saved_c, saved_sum : sfixed(5 downto -8);

begin

a_plus_b <= a + b;

reg1 : process (clk) is begin if rising_edge(clk) then saved_a_plus_b <= a_plus_b; saved_c <= c; end if; end process reg1;

...

Page 8: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 8

VHDL

Pipeline Example

sum <= saved_a_plus_b + saved_c;

reg2 : process (clk) is begin if rising_edge(clk) then saved_sum <= sum; end if; end process reg2;

sum_div_3 <= saved_sum * to_fixed(1.0/3.0, sum_div_3'left, sum_div_3'right);

reg3 : process (clk) is begin if rising_edge(clk) then avg <= sum_div_3; end if; end process reg3;

end architecture average_pipeline;

Page 9: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 9

VHDL

D-Flipflop with Enable

Storage controlled by a clock-enable stores only when CE = 1 on a rising

edge of the clock

CE is a synchronous control input

DCE

Q

clk D

CE

clk

Q

Page 10: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 10

VHDL

Register with Enable

One flipflop per bit clk and CE wired in common

signal d, q: ...;...

reg: process (clk) isbegin if rising_edge(clk) then if ce = '1' then q <= d; end if; end if;end process reg;

Page 11: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 11

VHDL Register with Synchronous Reset

Reset input forces stored value to 0 reset input must be stable around rising

edge of clk

DCE

Q

clkreset

D

CE

reset

clk

Q

1 2 3 4 5 6 7 8

Page 12: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 12

VHDL

Synch Reset in VHDL

flipflop: process (clk) isbegin if rising_edge(clk) then if reset = '1' then q <= '0'; elsif ce = '1' then q <= d; end if; end if;end process flipflop;

reg: process (clk) isbegin if rising_edge(clk) then if reset = '1' then q <= "00000000"; elsif ce = '1' then q <= d; end if; end if;end process reg;

single-bit data vector data

Page 13: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 13

VHDL Register with Asynchronous Reset

Reset input forces stored value to 0 reset can become 1 at any time, and

effect is immediate reset should return to 0 synchronously

DCE

Q

clkreset

D

CE

reset

clk

Q

1 2 3 4 5 6 7 8

Page 14: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 14

VHDL

Asynch Reset in VHDL

reg: process (clk, reset) isbegin if reset = '1' then q <= "00000000"; elsif rising_edge(clk) then if ce = '1' then q <= d; end if; end if;end process reg;

reset is an asynchronous control input here include it in the sensitivity list so that the

process responds to changes immediately

Page 15: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 15

VHDL

Example: Accumulator

Sum a sequence of signed numbers A new number arrives when data_en = 1 Clear sum to 0 on synch reset

library ieee;use ieee.std_logic_1164.all, ieee.numeric_std.all;

entity accumulator is port ( clk, reset, data_en : in std_logic; data_in : in signed(15 downto 0); data_out : out signed(19 downto 0) );end entity accumulator;

Page 16: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 16

VHDL

Example: Accumulator

architecture rtl of accumulator is

signal sum, new_sum : signed(19 downto 0);

begin

new_sum <= sum + resize(data_in, sum'length);

reg: process (clk) is begin if rising_edge(clk) then if reset = '1' then sum <= (others => '0'); elsif data_en = '1' then sum <= new_sum; end if; end if; end process reg;

data_out <= sum;

end architecture rtl;

Page 17: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 17

VHDL Flipflop and Register Variations

library ieee;use ieee.std_logic_1164.all;

entity flipflop_n is port ( clk_n, CE, pre_n, clr_n, D : in std_logic; Q, Q_n : out std_logic );end entity flipflop_n;

architecture behavour of flipflop_n isbegin

ff: process (clk_n, pre_n, clr_n) is begin assert not ( pre_n = '0' and clr_n = '0') report "Illegal inputs:" & "pre_n and clr_n both '0'"; if pre_n = '0' then Q <= '1'; Q_n <= '0'; elsif clr_n = '0' then Q <= '0'; Q_n <= '1'; elsif falling_edge(clk_n) then if CE = '1' then Q <= D; Q_n <= not D; end if; end if; end process ff;

end architecture behavour;

DCE

Q

Qclk

pre

clr

Page 18: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 18

VHDL

Shift Registers

Performs shift operation on stored data Arithmetic scaling Serial transfer

of data

DD_in

CEload_en

Q

clk

D

CE

Q

clk

0

1

D

CE

Q

clk

0

1

D

CE

Q

clk

0

1

Q(n–1)

Q(n–2)

Q(0)

D(n–1)

D(n–2)

D(0)

clkCE

load_en

Page 19: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 19

VHDL Example: Sequential Multiplier 16×16 multiply over 16 clock cycles,

using one adder Shift register for multiplier bits Shift register for lsb’s of accumulated product

17-bit reg

resetCE

D Q

clk

D

16-bit reg

CE

Q

clk

D_in

15-bitshift reg

CE

Q

clk

16-bitshift regD_inD

CEload_en

Q

clk x

16-bitadder

c0

y

c16

s15...0

16 15

031...16

P(14...0)

P(31...15)

y(15...0)

x(15...0)

y_load_eny_ce

x_ce

P_resetP_ce

clk

Page 20: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 20

VHDL

Latches

Level-sensitive storage Data transmitted while enable is '1'

transparent latch Data stored while enable is '0'

D Q

LED

LE

Q

Page 21: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 21

VHDL

Feedback Latches

Feedback in gate circuits produces latching behavior Example: reset/set (RS) latch

S

R

Q

Current RTL synthesis tools don’t accept VHDL models with unclocked feedback

+V

Q

Q

R

S

Page 22: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 22

VHDL

Latches in VHDL

Latching behavior is usually an error!

mux_block : process (sel, a1, b1, a2, b2) isbegin if sel = '0' then z1 <= a1; z2 <= b1; else z1 <= a2; z3 <= b2; end if;end process mux_block;

Oops!Should bez2 <= ...

Values must be stored for z2 while sel = '1' for z3 while sel = '0'

Page 23: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 23

VHDL

Counters

Stores an unsigned integer value increments or decrements the value

Used to count occurrences of events repetitions of a processing step

Used as timers count elapsed time intervals by

incrementing periodically

Page 24: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 24

VHDL

Free-Running Counter

Increments every rising edge of clk up to 2n–1, then wraps back to 0 i.e., counts modulo 2n

This counter is synchronous all outputs governed by clock edge

D Q

clk

+1 Q

clk

Page 25: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 25

VHDL Example: Periodic Control Signal

Count modulo 16 clock cycles Control output = '1' every 8th and 12th cycle decode count values 0111 and 1011

+1

clk

ctrl

0

1

2

3

0

1

2

3

D Q

clk

D Q

clk

D Q

clk

D Q

clk

Page 26: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 26

VHDL Example: Periodic Control Signal

library ieee; use ieee.std_logic_1164.all, ieee.numeric_std.all;

entity decoded_counter is port ( clk : in std_logic; ctrl : out std_logic );end entity decoded_counter;

architecture rtl of decoded_counter is

signal count_value : unsigned(3 downto 0);

begin

counter : process (clk) is begin if rising_edge(clk) then count_value <= count_value + 1; end if; end process counter;

ctrl <= '1' when count_value = "0111" or count_value = "1011" else '0';

end architecture rtl;

Page 27: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 27

VHDL

Count Enable and Reset

Use a register with control inputs

Increments when CE = '1' on rising clock edge

Reset: synch or asynch

+1Q

clk

CEreset

clk

D

CE

Q

reset

Page 28: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 28

VHDL

Terminal Count Status signal indicating final count value

TC is '1' for one cycle in every 2n cycles frequency = clock frequency / 2n

Called a clock divider

counter

… …

Q0Q1

Qnclk

… TC

Page 29: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 29

VHDL

Divider Example Alarm clock beep: 500Hz from 1MHz clock

counttone2

tone

clk

10-bitcounter

Q

TCclk

D

CE

Q

clk

tone

tone2

count

clk

1 100 2 2 10 2 10

1023 1023 1023

Page 30: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 30

VHDL

Divide by k

Decode k–1 as terminal count and reset counter register Counter increments modulo k

Example: decade counter Terminal count = 9

clk Q0Q1Q2Q3

Q0Q1Q2Q3

clk

reset

counter

Page 31: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 31

VHDL

Decade Counter in VHDL

library ieee; use ieee.std_logic_1164.all, ieee.numeric_std.all;

entity decade_counter is port ( clk : in std_logic; q : out unsigned(3 downto 0) );end entity decade_counter;

architecture rtl of decade_counter is

signal count_value : unsigned(3 downto 0);

begin

count : process (clk) is begin if rising_edge(clk) then count_value <= (count_value + 1) mod 10; end if; end process count;

q <= count_value;

end architecture rtl;

Page 32: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 32

VHDL

Down Counter with Load

Load a starting value, then decrement Terminal count = 0

Useful for interval timer

D Q

clk

–1

=0?

Q

TC

clkload

D

0

1

Page 33: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 33

VHDL

Loadable Counter in VHDLlibrary ieee; use ieee.std_logic_1164.all, ieee.numeric_std.all;

entity interval_timer is port ( clk, load : in std_logic; data : in unsigned(9 downto 0); tc : out std_logic);end entity interval_timer;

architecture rtl of interval_timer is

signal count_value : unsigned(9 downto 0);

begin

count : process (clk) is begin if rising_edge(clk) then if load = '1' then count_value <= data; else count_value <= count_value - 1; end if; end if; end process count;

tc <= '1' when count_value = 0 else '0';

end architecture rtl;

Page 34: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 34

VHDL Reloading Counter in VHDLarchitecture repetitive of interval_timer is

signal load_value, count_value : unsigned(9 downto 0);

begin

count : process (clk) is begin if rising_edge(clk) then if load = '1' then load_value <= data; count_value <= data; elsif count_value = 0 then count_value <= load_value; else count_value <= count_value - 1; end if; end if; end process count;

tc <= '1' when count_value = 0 else '0';

end architecture repetitive;

Page 35: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 35

VHDL

Ripple Counter

Each bit toggles between 0 and 1 when previous bit changes from 1 to 0

D

Q

Q

clk

D

Q

Q

clk

D

Q

Q

clk

D

Q

Q

clk

Q0

Q1

Q2

Qn

clk

Q1

Q0

Q0

clk

Q1

Q2

Q2

Page 36: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 36

VHDL

Ripple or Synch Counter?

Ripple counter is ok if length is short clock period long relative to flipflop

delay transient wrong values can be tolerated area must be minimal

E.g., alarm clock Otherwise use a synchronous

counter

Page 37: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 37

VHDL

Datapaths and Control Digital systems perform sequences

of operations on encoded data Datapath

Combinational circuits for operations Registers for storing intermediate

results Control section: control sequencing

Generates control signals Selecting operations to perform Enabling registers at the right times

Uses status signals from datapath

Page 38: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 38

VHDL Example: Complex Multiplier

Cartesian form, fixed-point operands: 4 pre-, 12 post-binary-point bits result: 8 pre-, 24 post-binary-point bits

Subject to tight area constraints

ir jaaa ir jbbb

)()( riiriirrir babajbabajppabp

4 multiplies, 1 add, 1 subtract Perform sequentially using 1

multiplier, 1 adder/subtracter

Page 39: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 39

VHDL Complex Multiplier Datapath

0

1

0

1

D

CE

Q

clk

D

CE

Q

clk

× ±

D

CE

Q

clk

D

CE

Q

clk

p_r

p_i

a_r

a_i

b_r

b_i

a_sel

b_selpp1_cepp2_ce

subp_r_cep_i_ce

clk

Page 40: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 40

VHDL Complex Multiplier in VHDL

library ieee; use ieee.std_logic_1164.all, ieee.fixed_pkg.all;

entity multiplier is port ( clk, reset : in std_logic; input_rdy : in std_logic; a_r, a_i, b_r, b_i : in sfixed(3 downto -12); p_r, p_i : out sfixed(7 downto -24) );end entity multiplier;

architecture rtl of multiplier is

signal a_sel, b_sel, pp1_ce, pp2_ce, sub, p_r_ce, p_i_ce : std_logic; -- control signals signal a_operand, b_operand : sfixed(3 downto -12); signal pp, pp1, pp2, sum : sfixed(7 downto -24); ...

begin

Page 41: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 41

VHDL Complex Multiplier in VHDL

a_operand <= a_r when a_sel = '0' else a_i; -- mux b_operand <= b_r when b_sel = '0' else b_i; -- mux

pp <= a_operand * b_operand; -- multiplier

pp1_reg : process (clk) is -- partial product register 1 begin if rising_edge(clk) then if pp1_ce = '1' then pp1 <= pp; end if; end if; end process pp1_reg;

pp2_reg : process (clk) is -- partial product register 2 begin if rising_edge(clk) then if pp2_ce = '1' then pp2 <= pp; end if; end if; end process pp2_reg;

Page 42: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 42

VHDL Complex Multiplier in VHDL

sum <= pp1 + pp2 when sub = '0' else pp1 - pp2; -- add/sub

p_r_reg : process (clk) is -- result real-part register begin if rising_edge(clk) then if p_r_ce = '1' then p_r <= sum; end if; end if; end process p_r_reg;

p_i_reg : process (clk) is -- result imag-part register begin if rising_edge(clk) then if p_i_ce = '1' then p_i <= sum; end if; end if; end process p_i_reg;

... -- control circuit

end architecture rtl;

Page 43: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 43

VHDL Multiplier Control Sequence

Avoid resource conflict First attempt

1. a_r * b_r → pp1_reg2. a_i * b_i → pp2_reg3. pp1 – pp2 → p_r_reg4. a_r * b_i → pp1_reg5. a_i * b_r → pp2_reg6. pp1 + pp2 → p_i_reg

Takes 6 clock cycles

Page 44: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 44

VHDL Multiplier Control Sequence

Merge steps where no resource conflict

Revised attempt1. a_r * b_r → pp1_reg2. a_i * b_i → pp2_reg3. pp1 – pp2 → p_r_reg

a_r * b_i → pp1_reg4. a_i * b_r → pp2_reg5. pp1 + pp2 → p_i_reg

Takes 5 clock cycles

Page 45: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 45

VHDL

Multiplier Control Signals

Step a_sel b_selpp1_c

epp2_c

esub p_r_ce p_i_ce

1 0 0 1 0 – 0 0

2 1 1 0 1 – 0 0

3 0 1 1 0 1 1 0

4 1 0 0 1 – 0 0

5 – – 0 0 0 0 1

Page 46: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 46

VHDL

Finite-State Machines

Used the implement control sequencing Based on mathematical automaton theory

A FSM is defined by set of inputs: Σ set of outputs: Γ set of states: S initial state: s0 S transition function: δ: S × Σ → S output function: ω: S × Σ → Γ or ω: S → Γ

Page 47: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 47

VHDL

FSM in Hardware

Mealy FSM: ω: S × Σ → Γ Moore FSM: ω: S → Γ

Mealy FSM only

D

reset

Q

clk

current_state

outputsinputs

clk

reset

nextstatelogic

outputlogic

Page 48: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 48

VHDL FSM Example: Multiplier Control

One state per step Separate idle state?

Wait for input_rdy = '1' Then proceed to steps 1, 2, ... But this wastes a cycle!

Use step 1 as idle state Repeat step 1 if input_rdy ≠ '1' Proceed to step 2 otherwise

Output function Defined by table on slide 43 Moore or Mealy?

current_state

input_rdy

next_state

step1 0 step1

step1 1 step2

step2 – step3

step3 – step4

step4 – step5

step5 – step1

Transition function

Page 49: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 49

VHDL

State Encoding

Encoded in binary N states: use at least log2N bits

Encoded value used in circuits for transition and output function encoding affects circuit complexity

Optimal encoding is hard to find CAD tools can do this well

One-hot works well in FPGAs Often use 000...0 for idle state

reset state register to idle

Page 50: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 50

VHDL

FSMs in VHDL

Use an enumeration type for state values abstract, avoids specifying encoding

type multiplier_state is (step1, step2, step3, step4, step5);

signal current_state, next_state : multiplier_state;

...

Page 51: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 51

VHDL

Multiplier Control in VHDL

state_reg : process (clk, reset) isbegin if reset = '1' then current_state <= step1; elsif rising_edge(clk) then current_state <= next_state; end if;end process state_reg;

next_state_logic : process (current_state, input_rdy) isbegin case current_state is when step1 => if input_rdy = '0' then next_state <= step1; else next_state <= step2; end if; when step2 => next_state <= step3; when step3 => next_state <= step4; when step4 => next_state <= step5; when step5 => next_state <= step1; end case;end process next_state_logic;

Page 52: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 52

VHDL

Multiplier Control in VHDL

output_logic : process (current_state) isbegin case current_state is when step1 => a_sel <= '0'; b_sel <= '0'; pp1_ce <= '1'; pp2_ce <= '0'; sub <= '0'; p_r_ce <= '0'; p_i_ce <= '0'; when step2 => a_sel <= '1'; b_sel <= '1'; pp1_ce <= '0'; pp2_ce <= '1'; sub <= '0'; p_r_ce <= '0'; p_i_ce <= '0'; when step3 => a_sel <= '0'; b_sel <= '1'; pp1_ce <= '1'; pp2_ce <= '0'; sub <= '1'; p_r_ce <= '1'; p_i_ce <= '0'; when step4 => a_sel <= '1'; b_sel <= '0'; pp1_ce <= '0'; pp2_ce <= '1'; sub <= '0'; p_r_ce <= '0'; p_i_ce <= '0'; when step5 => a_sel <= '0'; b_sel <= '0'; pp1_ce <= '0'; pp2_ce <= '0'; sub <= '0'; p_r_ce <= '0'; p_i_ce <= '1'; end case;end process output_logic;

Moore FSM

Page 53: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 53

VHDL

State Transition Diagrams

Bubbles to represent states Arcs to represent transitions

Example S = {s1, s2, s3} Inputs (a1, a2):

Σ = {(0,0), (0,1), (1,0), (1,1)}

δ defined by diagram

s1 s2

s3

0, 0

0, 0

0, 1

1, 0

0, 1

1, 0

1, 1

1, 1

Page 54: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 54

VHDL

State Transition Diagrams

Annotate diagram to define output function Annotate states for

Moore-style outputs Annotate arcs for

Mealy-style outputs Example

x1, x2: Moore-style y1, y2, y3: Mealy-style

s1 s2

s3

0, 0 / 0, 0, 01, 0 0, 0

0, 1

0, 0 / 0, 0, 0

0, 1 / 0, 1, 1

/ 0, 1, 1

1, 0 / 1, 0, 0

0, 1 / 0, 1, 1

1, 0 / 1, 0, 0

1, 1 / 1, 1, 1

1, 1 / 1, 1, 1

Page 55: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 55

VHDL

Multiplier Control Diagram Input: input_rdy Outputs

a_sel, b_sel, pp1_ce, pp2_ce, sub, p_r_ce, p_i_ce

step10, 0, 1, 0, –, 0, 0

01 step2

1, 1, 0, 1, –, 0, 0

step41, 0, 0, 1, –, 0, 0

step5–, –, 0, 0, 0, 0, 1

step30, 1, 1, 0, 1, 1, 0

Page 56: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 56

VHDL

Bubble Diagrams or VHDL?

Many CAD tools provide editors for bubble diagrams Automatically generate VHDL for

simulation and synthesis Diagrams are visually appealing

but can become unwieldy for complex FSMs

Your choice... or your manager's!

Page 57: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 57

VHDL

Register Transfer Level

RTL — a level of abstraction data stored in registers transferred via circuits that operate

on data

control section

outputsinputs

Page 58: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 58

VHDL Clocked Synchronous Timing

Registers driven by a common clock Combinational circuits operate during

clock cycles (between rising clock edges)

tco + tpd + tsu < tc

Q1 D2tpdt

cotsu

Q1

clk

D2

tco

tc

tpd

tsu

t

Page 59: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 59

VHDL

Control Path Timing

tco + tpd-s + tpd-o + tpd-c + tsu < tc

tco + tpd-s + tpd-ns + tsu < tc

Ignore tpd-s for a Moore FSM

tpd-s

tpd-c

tpd-o

tpd-ns

tco t

su

tsu

Page 60: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 60

VHDL

Timing Constraints

Inequalities must hold for all paths If tco and tsu the same for all paths

Combinational delays make the difference Critical path

The combinational path between registers with the longest delay

Determines minimum clock period for the entire system

Focus on it to improve performance Reducing delay may make another path

critical

Page 61: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 61

VHDL Interpretation of Constraints

1. Clock period depends on delays System can operate at any frequency

up to a maximum OK for systems where high

performance is not the main requirement

2. Delays must fit within a target clock period

Optimize critical paths to reduce delays if necessary

May require revising RTL organization

Page 62: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 62

VHDL

Clock Skew

Need to ensure clock edges arrive at all registers at the same time Use CAD tools to insert clock buffers

and route clock signal paths

Q1 D2Q1

clk1

clk2

D2

th

Page 63: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 63

VHDL

Off-Chip Connections

Delays going off-chip and inter-chip Input and output pad delays, wire delays

Same timing rules apply Use input and output registers to avoid

adding external delay to critical path

Q1 D2

Page 64: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 64

VHDL

Asynchronous Inputs External inputs can change at any time

Might violate setup/hold time constraints Can induce metastable state in a flipflop

Unbounded time to recover May violate setup/hold time of

subsequent flipflop21

2

ffk

eMTBF

f

tk

02k

0 1 0 1

Page 65: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 65

VHDL

Synchronizers

If input changes outside setup/hold window Change is simply delayed by one cycle

If input changes during setup/hold window First flipflop has a whole cycle to resolve

metastability See data sheets for metastability parameters

D Q

clk

D Q

clk

clk

asynch_insynch_in

Page 66: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 66

VHDL Switch Inputs and Debouncing Switches and push-buttons suffer from

contact bounce Takes up to 10ms to settle

Need to debounce to avoid false triggering

Requires two inputs and two resistors

Must use a break-before-make double-throw switchQ

R

S

+V

Page 67: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 67

VHDL Switch Inputs and Debouncing Alternative

Use a single-throw switch Sample input at intervals longer than bounce

time Look for two successive samples with the same

value

Assumption Extra circuitry inside the

chip is cheaper than extra components and connections outside

+V

Page 68: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 68

VHDL

Debouncing in VHDL

library ieee; use ieee.std_logic_1164.all;

entity debouncer is port ( clk, reset : in std_logic; -- clk frequency = 50MHz pb : in std_logic; pb_debounced : out std_logic );end entity debouncer;

architecture rtl of debouncer is

signal count500000 : integer range 0 to 499999; signal clk_100Hz : std_logic; signal pb_sampled : std_logic;

begin

Page 69: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 69

VHDL

Debouncing in VHDL

div_100Hz : process (clk, reset) is begin if reset = '1' then clk_100Hz <= '0'; count500000 <= 0; elsif rising_edge(clk) then if count500000 = 499999 then count500000 <= 0; clk_100Hz <= '1'; else count500000 <= count500000 + 1; clk_100Hz <= '0'; end if; end if; end process div_100Hz;

debounce_pb : process (clk) is begin if rising_edge(clk) then if clk_100Hz = '1' then if pb = pb_sampled then pb_debounced <= pb; end if; pb_sampled <= pb; end if; end if; end process debounce_pb;

end architecture rtl;

Page 70: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 70

VHDL Verifying Sequential Circuits

DUV may take multiple and varying number of cycles to produce output

Checker needs to synchronize with test generator ensure DUV outputs occur when expected ensure DUV outputs are correct ensure no spurious outputs occur

Design UnderVerification

(DUV)Apply

Test Cases Checker

Verification Testbench

Page 71: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 71

VHDL Example: Multiplier Testbench

entity multiplier_testbench is

end entity multiplier_testbench;

library ieee; use ieee.std_logic_1164.all, ieee.fixed_pkg.all, ieee.math_complex.all;

architecture verify of multiplier_testbench is

constant t_c : time := 50 ns; signal clk, reset : std_logic; signal input_rdy : std_logic; signal a_r, a_i, b_r, b_i : sfixed(3 downto -12); signal p_r, p_i : sfixed(7 downto -24); signal a, b : complex;

begin

duv : entity work.multiplier(rtl) port map ( clk, reset, input_rdy, a_r, a_i, b_r, b_i, p_r, p_i );

clk_gen : process is begin wait for t_c / 2; clk <= '1'; wait for t_c / 2; clk <= ‘0'; end process clk_gen;

reset <= '1', '0' after 2 * t_c ns;

Page 72: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 72

VHDL Example: Multiplier Testbench

apply_test_cases : process is begin wait until falling_edge(clk) and reset = '0'; a <= cmplx(0.0, 0.0); b <= cmplx(1.0, 2.0); input_rdy <= '1'; wait until falling_edge(clk); input_rdy <= '0'; for i in 1 to 5 loop wait until falling_edge(clk); end loop; a <= cmplx(1.0, 1.0); b <= cmplx(1.0, 1.0); input_rdy <= '1'; wait until falling_edge(clk); input_rdy <= '0'; for i in 1 to 6 loop wait until falling_edge(clk); end loop; -- further test cases ... wait; end process apply_test_cases;

a_r <= to_sfixed(a.re, a_r'left, a_r'right); a_i <= to_sfixed(a.im, a_i'left, a_i'right); b_r <= to_sfixed(b.re, b_r'left, b_r'right); b_i <= to_sfixed(b.im, b_i'left, b_i'right);

Page 73: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 73

VHDL Example: Multiplier Testbench

check_outputs : process is variable p : complex; begin wait until rising_edge(clk) and input_rdy = '1'; p := a * b; for i in 1 to 5 loop wait until falling_edge(clk); end loop; assert abs (to_real(p_r) - p.re) < 2.0**(-12) and abs (to_real(p_i) - p.im) < 2.0**(-12); end process check_outputs;

end architecture verify;

Page 74: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 74

VHDL

Asynchronous Timing Clocked synchronous timing requires

global clock distribution with minimal skew path delay between registers < clock period

Hard to achieve in complex multi-GHz systems

Globally asynch, local synch (GALS) systems Divide the systems into local clock domains Inter-domain signals treated as asynch inputs Simplifies clock managements and constraints Delays inter-domain communication

Delay-insensitive asynchronous systems no clock signals

Page 75: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 75

VHDL

Other Clock-Related Issues

Inter-chip clocking Distributing high-speed clocks on PCBs is hard Often use slower off-chip clock, with on-chip

clock a multiple of off-chip clock Synchronize on-chip with phase-locked loop

(PLL) In multi-PCB systems

treat off-PCB signals as asynch inputs Low power design

Continuous clocking wastes power Clock gating: turn off clock to idle subsystems

Page 76: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 76

VHDL

Summary

Registers for storing data synchronous and asynchronous

control clock enable, reset, preset

Latches: level-sensitive usually unintentional in VHDL

Counters free-running dividers, terminal count,

reset, load, up/down

Page 77: Digital Design: An Embedded Systems Approach Using VHDL Chapter 4 Sequential Basics Portions of this work are from the book, Digital Design: An Embedded

Digital Design — Chapter 4 — Sequential Basics 77

VHDL

Summary

RTL organization of digital systems datapath and control section

Finite-State Machine (FSM) states, inputs, transition/output functions Moore and Mealy FSMs bubble diagrams

Clocked synch timing and constraints critical path and optimization

Asynch inputs, switch debouncing Verification of sequential systems