Outline O tli
Three Verilog code styles for FSMs Verilog codes for Moore and Mealy Two examples
Sequence detector Control circuit for swapping
3 parallel blocks: 1) always block: combinational circuit for next state 2) always block: update states 3) assign: i combinational circuit for output y Flip-flops
w clock
Output z circuit
resetn
w clock
Y Flip-flops
Output z circuit
resetn
2 parallel blocks: 1) always block: combinational circuit for next state output 2) always block: update states d t t t merge Y Flip-flops y
w clock
Output z circuit
resetn
w clock
Y Flip-flops
Output z circuit
resetn
2 parallel blocks: 1) always block: sequential circuit for next state and update 2) assign bl k i block: for output assign cannot be put inside the always block merge
w clock
Y Flip-flops
Output z circuit
resetn
endmodule
// Define outputs assign R2out = (y == B); assign R3in = (y == B); assign R1out = (y == C); assign R2in = (y == C); assign R3out = (y == D); assign R1in = (y == D); assign Done = (y == D); endmodule
2 parallel blocks: 1) always block: combinational circuit for next state and output 2) always block: update states
w clock
Y Flip-flops
Output z circuit
endmodule
resetn
Design example: D i l
a sequential circuit has two inputs, w1 and w2, and an output, z. q p , , p , Its function is to compare the input sequences on the two inputs. If w1 =w2 during any four consecutive clock cycles, the circuit produces z=1; otherwise, z=0. For example w1: 0 1 1 0 1 1 1 0 0 0 1 1 0 w2: 1 1 1 0 1 0 1 0 0 0 1 1 1 z: 0 0 0 0 1 0 0 0 0 1 1 1 0
state di t t diagram
w = w1 w2
Mealy FSM W=1 A W 0 W=0 W=1 B W=0 W=1 W=1 D W=0 / z=1 C W=0 3 equal 4 equal 2 equal 1 equal resetn W1 != w2
: Verilog code
module prob8_9 (Clock, Resetn, w1, w2, z); input Clock, Resetn, w1, w2; output z; reg z; reg [2 1] y, Y [2:1] Y; wire w; parameter [2:1] A = 2'b00, B = 2'b01, C = 2'b10, D = 2'b11; // Define the next state and output combinational circuits assign w = w1 ^ w2; g ; always @(w or y) case (y) A: if (w) begin Y = A; z = 0; else begin Y = B; z = 0; B: if (w) begin Y = A; z = 0; else begin Y = C; z = 0; C: if (w) begin Y = A; z = 0; else begin Y = D; z = 0; D: if (w) begin Y = A; z = 0; else begin Y = D; z = 1; endcase // Define the sequential block always @(negedge Resetn or posedge Clock) if (Resetn == 0) y <= A; else y <= Y; endmodule
simulation i l ti
Specify input signals w1 and w2:
Assume that w1 and w2 may change their values only after the positive edges of clock with a small delay. This assumption is reasonable because w1 and w2 most likely come from a circuit with the same clock. Otherwise, Otherwise the simulation result may be not what you expected.
simulation i l ti
simulation problem i l ti bl
If inputs are not specified appropriately.