Lecture 8 Model Datapath FSM 20210427
Lecture 8 Model Datapath FSM 20210427
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
COUNTER AS FSM
VLSI Signal Processing Lab.
COUNTER 當 FSM 控制
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Counter 當 FSM
• Suppose you want to build an FSM with the following state
diagram
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
default: next = 0 ;
endcase
end
30 31 0 endmodule
31 0 0
表格很好,但這個變化很規則,可以更簡單描述
next = r ? 0 : state + 1 ;
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
30 31 0
VLSI Signal Processing Lab.
31 0 0
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
DFF #(5) count(clk, next, out) ; DFF #(5) count(clk, next, out) ;
…
6'd30: next = 31 ; Between these 2 coding examples?
6'd31: next = 0 ;
default: next = 0 ;
endcase
end
endmodule
5'd5: next = 6 ;
5'd6: next = 7 ; Between these 2 coding examples?
…
5'd30: next = 31 ;
5'd31: next = 0 ;
endcase
end
endmodule
(c) 2005-2012 W. J. Dally
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Sequential Datapath
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Table Version
30 0 31 29 in
VLSI Signal Processing Lab.
31 0 0 30 in
x y 0 0 0 1 y
q x 0 0 0 0 q
always_comb begin
casez({rst, up, down, load})
4'b1???: next = {n{1'b0}} ; //reset
4'b0100: next = out + 1'b1 ;
4'b0010: next = out - 1'b1 ; Up or down ,需要兩個加減法器,可以更簡化嗎 ?
VLSI Signal Processing Lab.
4'b0001: next = in ;
4’b0000: next = out ;
default: next = {n{1’bx}} ;//unknown
endcase
State Next State
end
rst up down load else
endmodule
q 0 q+1 q-1 in q
Check p.11 async rst
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Data path
VLSI Signal Processing Lab.
• How do we make the output go high in the same cycle as the third
input 1?
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
control flag
8051
VLSI Signal Processing Lab.
controller
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
SHIFT REGISTERS
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always_comb begin
casez({left,right,load})
3'b1??: next = {out[n-2:0],sin} ; // left
VLSI Signal Processing Lab.
所有數位設計都可拆成資料流計算與控制兩部分
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Datapath/Control Partitioning
Datapath – determined by a function – e.g., mux, arithmetic, …
Control – determined by state diagram or state table
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
• 規格
– The vending machine accepts nickels, dimes, and quarters.
Whenever a coin is deposited into the coin slot, a pulse appears
for one clock cycle on one of three lines indicating the type of
coin: nickel, dime, or quarter.
– The price of the item is set on an n-bit switch internal to the
machine (in units of nickels), and is input to the controller on the
n-bit signal price.
– When sufficient coins have been deposited to purchase a soft
VLSI Signal Processing Lab.
– After serving, the machine returns change (if any) to the user. It
does this one nickel at a time, asserting the signal change, for
exactly one cycle and waiting for signal done to indicate that a
nickel has been dispensed before dispensing the next nickel or
returning to its original state. Any time the signal done is
asserted, we must wait for done to go low before proceeding.
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
• In this diagram, edges from a state to itself are omitted. If the conditions on
all edges leading out of the current state are not satisfied, the FSM stays in
that state.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
//variable declaration
logic [2:0] value;
logic [n-1:0] amount;
//state machine
parameter [2:0] DEPOSIT = 0,
SERVE1 = 1,
SERVE2 = 2,
CHANG1 = 3,
CHANG2 = 4;
reg [2:0] state, state_nxt;
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
always_comb begin
amount_nxt = amount;
state_nxt = DEPOSIT;
serve = 0;
change = 0;
case(state)
DEPOSIT: begin
if(dispense & enough) state_nxt = SERVE1;
else state_nxt = DEPOSIT;
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
SERVE2: begin
if(~done & zero) state_nxt = DEPOSIT;
else if(~done & ~zero) state_nxt = CHANG1;
else state_nxt = SERVE2;
amount_nxt = amount - 1;
end
CHANG2: begin
if(~done & ~zero) state_nxt = CHANG1;
VLSI Signal Processing Lab.
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
//----------------------------------------------------------------------
// VendingMachine - Top level module
// Just hooks together control and datapath
//----------------------------------------------------------------------
module VendingMachine(clk, rst, nickel, dime, quarter, dispense, done, price,
serve, change) ;
parameter n = `DWIDTH ;
input clk, rst, nickel, dime, quarter, dispense, done ;
input [n-1:0] price ;
output serve, change ;
endmodule
// outputs
wire first ; // true during first cycle of serve1 or change1
wire serve1 = (state == `SERVE1) ;
wire change1 = (state == `CHANGE1) ;
wire serve = serve1 & first ;
wire change = change1 & first ; // state register
DFF #(`SWIDTH) state_reg(clk, next, state) ;
// datapath controls
wire dep = (state == `DEPOSIT) ; // next state logic
// price, 1, 2, 5 always @(state or zero or dispense or done or enough) begin
wire [3:0] selval = {(dep & dispense), casex({dispense, enough, done, zero, state})
((dep & nickel)| change), {4'b11xx,`DEPOSIT}: next1 = `SERVE1 ; // dispense & enough
(dep & dime), {4'b0xxx,`DEPOSIT}: next1 = `DEPOSIT ;
(dep & quarter)} ; {4'bx0xx,`DEPOSIT}: next1 = `DEPOSIT ;
// amount, sum, 0 {4'bxx1x,`SERVE1}: next1 = `SERVE2 ; // done
wire selv = (dep & (nickel | dime | quarter | {4'bxx0x,`SERVE1}: next1 = `SERVE1 ;
(dispense & enough))) | {4'bxx01,`SERVE2}: next1 = `DEPOSIT ; // ~done & zero
(change) ; {4'bxx00,`SERVE2}: next1 = `CHANGE1 ; // ~done & ~zero
wire [2:0] selnext = {!(selv | rst),selv,rst} ; {4'bxx1x,`SERVE2}: next1 = `SERVE2 ; // done
{4'bxx1x,`CHANGE1}: next1 = `CHANGE2 ; // done
// subtract {4'bxx0x,`CHANGE1}: next1 = `CHANGE1 ; // ~done
VLSI Signal Processing Lab.
wire sub = (dep & dispense) | change ; {4'bxx00,`CHANGE2}: next1 = `CHANGE1 ; // ~done & ~zero
{4'bxx01,`CHANGE2}: next1 = `DEPOSIT ; // ~done & zero
// only do actions on first cycle of serve1 or change1 {4'bxx1x,`CHANGE2}: next1 = `CHANGE2 ; // done
wire nfirst = !(serve1 | change1) ; endcase
DFF #(1) first_reg(clk, nfirst, first) ; end
// comparators
wire enough = (amount >= price) ;
wire zero = (amount == 0) ;
endmodule (c) 2005-2012 W. J. Dally
N C T U . E E , Hsinchu, Taiwan
Platform Based Design Group
Waveforms for
Deposit vending
Deposit Dime Attempt Dispense
machine
Nickel Dispense
Two
Quarters
VLSI Signal Processing Lab.
Summary
• Datapath state machines
– Next state function specified by an expression, not a table
• next = rst ? 0 : (inc ? next + 1 : next) ;
– Common “idioms”
• Counters
• Shift registers