Sequential Logic Digital state: the D-Register Timing constraints for D-Registers Specifying registers in Verilog Blocking and nonblocking assignments Examples Reminder: Lab #2 due Thursday 1
Use Explicit Port Declarations module mux32two (input [31:0] i0,i1, input sel, output [31:0] out); assign out = sel? i1 : i0; module mux32two adder_mux(.i0(b),.i1(32'd1),.sel(f[0]),.out(addmux_out)); mux32two adder_mux(b, 32'd1, f[0], addmux_out); 2 Order of the ports matters! 6.111 Fall 2015
Verilog Summary Verilog Hardware description language not software program. A convention: lowercase for variables, UPPERCASE for parameters module blob #(parameter WIDTH = 64, // default width: 64 pixels HEIGHT = 64, // default height: 64 pixels COLOR = 3'b111) // default color: white (input [10:0] x,hcount, input [9:0] y,vcount, output reg [2:0] pixel); module wires wire a,b,z; // three 1-bit wires wire [31:0] memdata; // a 32-bit bus wire [7:0] b1,b2,b3,b4; // four 8-bit buses wire [WIDTH-1:0] input; // parameterized bus 3
Examples parameter MSB = 7; // defines msb as a constant value 7 parameter E = 25, F = 9; // defines two constant numbers parameter BYTE_SIZE = 8, BYTE_MASK = BYTE_SIZE - 1; parameter [31:0] DEC_CONST = 1 b1; // value converted to 32 bits parameter NEWCONST = 3 h4; // implied range of [2:0] parameter NEWCONS = 4; // implied range of at least [31:0] 4
Something We Can t Build (Yet) What if you were given the following design specification: button When the button is pushed: 1) Turn on the light if it is off 2) Turn off the light if it is on The light should change state within a second of the button press light What makes this circuit so different from those we ve discussed before? 1. State i.e. the circuit has memory 2. The output was changed by a input event (pushing a button) rather than an input value 5
Digital State One model of what we d like to build Memory Device LOAD Current State Combinational Logic Next State Input Output Plan: Build a Sequential Circuit with stored digital STATE Memory stores CURRENT state, produced at output Combinational Logic computes NEXT state (from input, current state) OUTPUT bit (from input, current state) State changes on LOAD control input When Output deps on input and current state, circuit is called a Mealy machine. If Output deps only on the current state, circuit is called a Moore machine. 6
Our next building block: the D register The edge-triggered D register: on the rising edge of CLK, the value of D is saved in the register and then shortly afterwards appears on Q. D CLK Q 7
D-Register Timing - I t PD Q t CD CLK D t PD : maximum propagation delay, CLK Q t CD : minimum contamination delay, CLK Q t SETUP t HOLD t SETUP : setup time How long D must be stable before the rising edge of CLK t HOLD : hold time How long D must be stable after the rising edge of CLK 8
D-Register Internals 74LS74 t PD Q t CD CLK D CLK t SETUP t SETUP = 20ns t HOLD t PD-HL = 40ns t HOLD = 5ns t PD-LH = 25ns 9
D-Register Timing - II D Q logic D Q reg1 reg2 CLK t CLK CLK t PD,reg1 t PD,logic t CD,reg1 t CD,logic t SETUP,reg2 t PD,reg1 + t PD,logic + t SETUP,reg2 t CLK The good news: you can choose t CLK so that this constraint is satisfied! t CD,reg1 + t CD,logic t HOLD,reg2 The bad news: you have to change your design if this constraint isn t met. 10
Single-clock Synchronous Circuits Does that symbol register? We ll use Registers in a highly constrained way to build digital systems: Single-clock Synchronous Discipline No combinational cycles Single clock signal shared among all clocked devices (one clock domain) Only care about value of combinational circuits just before rising edge of clock Clock period greater than every combinational delay Change saved state after noiseinducing logic transitions have stopped! 11
Clocks are Not Perfect: Clock Skew CLout In D Q Combinational Logic D Q clk1 Wire delay clk2 clk1 clk2 δ>0 t skew = t clk2 t clk1 12
Positive and Negative Skew In R1 D Q Combinational Logic R2 D Q Combinational Logic R3 D Q CLK1 1 T CLK T CLK 3 CLK t CLK1 delay t CLK2 (a) Positive skew delay t CLK3 CLK2 2 t h 4 Launching edge arrives before the receiving edge In R1 D Q Combinational Logic R2 D Q Combinational Logic R3 D Q CLK1 1 T CLK + T CLK 3 t CLK1 t CLK2 t CLK3 delay (b) Negative skew delay CLK CLK2 2 4 Receiving edge arrives before the launching edge Adapted from J. Rabaey, A. Chandrakasan, B. Nikolic, Digital Integrated Circuits: A Design Perspective Copyright 2003 Prentice Hall/Pearson. 13
D-Register Timing With Skew CLK CLK reg1 D Q logic D Q reg1 reg2 ±skew t PD,reg1 +t PD,logic t CD,reg1 +t CD,logic t SETUP,reg2 In the real world the clock signal arrives at different registers at different times. The difference in arrival times (pos or neg) is called the clock skew t skew. t skew = t Rn,clk2 t Rn,clk1 We can update our two timing constraints to reflect the worst-case skew Setup time: t Rn,clk = t Rn+1,clk t Rn,clk1 +t PD,reg1 +t PD,logic +t SETUP,reg2 t Rn+1,clk2 t PD,reg1 +t PD,logic + t SETUP,reg2 t CLK + t skew Hold time: t Rn,clk1 +t CD,reg1 +t CD,logic t Rn,clk2 +t HOLD,reg2 t CD,reg1 +t CD,logic t HOLD,reg2 + t skew CLK reg2 t HOLD,reg2 CLK reg2 rising edge might fall anywhere in this region. Thus clock skew increases the minimum cycle time of our design and makes it harder to meet register hold times. Which skew is tougher to deal with (pos or neg)? 14
Delay Estimation : Simple RC Networks V DD V in 50% V in V out t C L V out t phl t plh 90% V DD V DD 50% 10% t R on (a) Low-to-high (b) High-to-low review t f R t r v out V out V out vin C C L C L R on t p = ln (2) = 0.69 RC 15
RC Equation V s = 5 V R V s = 5 V Switch is closed t<0 Switch opens t>0 + V c - V s = V R + V C V s = i R R+ V c i R = V s = dv RC dt c V c C dv dt c V c 5 1 e t RC V c Vs 1 e t RC 16
Clocks are Not Perfect: Clock Jitter t pd, t su, t hold t clk 2t jitter > t pd + t su + t logic Typical crystal oscillator 100mhz (10ns) Jitter: 1ps 17
Sequential Circuit Timing Clock Input t CD,R = 1ns t PD,R = 3ns t S,R = 2ns t H,R = 2ns Current State Combinational Logic t CD,L =? t PD,L = 5ns New State Output Questions: Constraints on t CD for the logic? Minimum clock period? Setup, Hold times for Inputs? > 1 ns > 10 ns (t PD,R +t PD,L + t SETUP,R ) t SETUP,Input = t PD,L +t SETUP,R t HOLD,Input = t HOLD,R -t CD,L This is a simple Finite State Machine more on next time! 18
The Sequential always Block Edge-triggered circuits are described using a sequential always block Combinational Sequential module comb(input a, b, sel, output reg out); always @(*) begin if (sel) out = b; else out = a; module module seq(input a, b, sel, clk, output reg out); always @(posedge clk) begin if (sel) out <= b; else out <= a; module 19
Importance of the Sensitivity List The use of posedge and negedge makes an always block sequential (edge-triggered) Unlike a combinational always block, the sensitivity list does determine behavior for synthesis! D-Register with synchronous clear module dff_sync_clear( input d, clearb, clock, output reg q ); always @(posedge clock) begin if (!clearb) q <= 1'b0; else q <= d; module D-Register with asynchronous clear module dff_sync_clear( input d, clearb, clock, output reg q ); always @(negedge clearb or posedge clock) begin if (!clearb) q <= 1'b0; else q <= d; module always block entered only at each positive clock edge always block entered immediately when (active-low) clearb is asserted Note: The following is incorrect syntax: always @(clear or negedge clock) If one signal in the sensitivity list uses posedge/negedge, then all signals must. Assign any signal or variable from only one always block. Be wary of race conditions: always blocks with same trigger execute concurrently 20
Blocking vs. Nonblocking Assignments Verilog supports two types of assignments within always blocks, with subtly different behaviors. Blocking assignment (=): evaluation and assignment are immediate always @(*) begin x = a b; // 1. evaluate a b, assign result to x y = a ^ b ^ c; // 2. evaluate a^b^c, assign result to y z = b & ~c; // 3. evaluate b&(~c), assign result to z Nonblocking assignment (<=): all assignments deferred to of simulation time step after all right-hand sides have been evaluated (even those in other active always blocks) always @(*) begin x <= a b; // 1. evaluate a b, but defer assignment to x y <= a ^ b ^ c; // 2. evaluate a^b^c, but defer assignment to y z <= b & ~c; // 3. evaluate b&(~c), but defer assignment to z // 4. of time step: assign new values to x, y and z Sometimes, as above, both produce the same result. Sometimes, not! 21
Blocking vs. Nonblocking Assignments Guaranteed question on job interviews with Verilog questions. Blocking assignment (=): evaluation and assignment are immediate; subsequent statements affected. Nonblocking assignment (<=): all assignments deferred to of simulation time step after all right-hand sides have been evaluated (even those in other active always blocks) Sometimes, as above, both produce the same result. Sometimes, not! 22
Assignment Styles for Sequential Logic What we want: Register Based Digital Delay Line Will nonblocking and blocking assignments both produce the desired result? ( old means value before clock edge, new means the value after most recent assignment) module nonblocking( input in, clk, output reg out ); reg q1, q2; always @(posedge clk) begin q1 <= in; q2 <= q1; // uses old q1 out <= q2; // uses old q2 module module blocking( input in, clk, output reg out ); reg q1, q2; always @(posedge clk) begin q1 = in; q2 = q1; // uses new q1 out = q2; // uses new q2 module 23
Use Nonblocking for Sequential Logic always @(posedge clk) begin q1 <= in; q2 <= q1; // uses old q1 out <= q2; // uses old q2 always @(posedge clk) begin q1 = in; q2 = q1; // uses new q1 out = q2; // uses new q2 At each rising clock edge, q1, q2, and out simultaneously receive the old values of in, q1, and q2. At each rising clock edge, q1 = in. After that, q2 = q1. After that, out = q2. Therefore out = in. Blocking assignments do not reflect the intrinsic behavior of multistage sequential logic Guideline: use nonblocking assignments for sequential always blocks 24
always block Sequential always block: always @(posedge clock) use <= Combinatorial always block: always @ * use = Results of operators (LHS) inside always block (sequential and combinatorial) must be declared as reg Equivalent Verilog reg z always @ * z = x && y same as example of combinatorial always block assign z = x && y // z not a reg case statements must be used within an always block; include default case 25
Sequential always block style // There are two styles for creating this sample divider. The // first uses sequential always block for state assignment and // a combinational always block for next-state. This style ts // to result in fewer errors. // // An alternate approach is to use a single always block. An example // of a divide by 5 counter will illustrate the differences ////////////////////////////////// // Sequential always block with a // combinational always block reg [3:0] count1, next_count1; always @(posedge clk) count1 <= next_count1; always @* begin if (reset) next_count1 = 0; else next_count1 = (count1 == 4)? 0 : count1 + 1; assign enable1 = (count1 == 4); ////////////////////////////////// ///////////////////////////////// // Single always block // reg [3:0] count2; always @(posedge clk) begin if (reset) count2 <= 0; else count2 <= (count2 == 4)? 0 : count2 + 1; assign enable2 = (count2 == 4); ////////////////////////////////// 26
Coding Guidelines The following helpful guidelines are from the Cummings paper. If followed, they ensure your simulation results will match what they synthesized hardware will do: 1. When modeling sequential logic, use nonblocking assignments. 2. When modeling latches, use nonblocking assignments. 3. When modeling combinational logic with an always block, use blocking assignments. 4. When modeling both sequential and combinational logic within the same always block, use nonblocking assignments. 5. Do not mix blocking and nonblocking assignments in the same always block. 6. Do not make assignments to the same variable from more than one always block. 7. Use $strobe to display values that have been assigned using nonblocking assignments. 8. Do not make assignments using #0 delays. For more info see: http://www.sunburst-design.com/papers/cummingssnug2002boston_nbawithdelays.pdf #1 thing we will be checking in your Verilog submissions! 27
Guideline 4: Sequential and combinatorial logic in the same always block module nbex1 (output reg q, input clk, rst_n, input a, b); reg y; always @(a or b) y = a ^ b; always @(posedge clk or negedge rst_n) if (!rst_n) q <= 1'b0; else q <= y; module Combinatorial logic module nbex2 (output q, input clk, rst_n, input a, b); reg q; always @(posedge clk or negedge rst_n) if (!rst_n) q <= 1'b0; else q <= a ^ b; module Combinatorial logic 6.111 Fall 2008 Lecture 4 28
= vs. <= inside always module main; reg a,b,clk; A B C always @(posedge clk) begin a = b; // blocking assignment b = a; // execute sequentially always @(posedge clk) begin a <= b; // non-blocking assignment b <= a; // eval all RHSs first always @(posedge clk) a = b; always @(posedge clk) b = a; initial begin clk = 0; a = 0; b = 1; D #10 clk = 1; #10 $display("a=%d b=%d\n",a,b); $finish; E module always @(posedge clk) a <= b; always @(posedge clk) b <= a; always @(posedge clk) begin a <= b; b = a; // urk! Be consistent! Rule: always change state using <= (e.g., inside always @(posedge clk) ) 29
Implementation for on/off button button light module onoff(input button, output reg light); always @(posedge button) light <= ~light; module 30
Synchronous on/off button When designing a system that accepts many inputs it would be hard to have input changes serve as the system clock (which input would we use?). So we ll use a single clock of some fixed frequency and have the inputs control what state changes happen on rising clock edges. For most of our lab designs we ll use a 27MHz system clock (37ns clock period). module onoff_sync(input clk, button, output reg light); always @ (posedge clk) begin if (button) light <= ~light; module 31
Resetting to a known state Usually one can t rely on registers powering-on to a particular initial state*. So most designs have a RESET signal that when asserted initializes all the state to known, mutually consistent initial values. module onoff_sync(input clk, reset, button, output reg light); always @ (posedge clk) begin if (reset) light <= 0; else if (button) light <= ~light; module * Actually, our FPGAs will reset all registers to 0 when the device is programmed. But it s nice to be able to press a reset button to return to a known state rather than starting from scratch by reprogramming the device. 32
Clocks are fast, we re slow! The circuit on the last slide toggles the light on every rising clock edge for which button is 1. But clocks are fast (27MHz!) and our fingers are slow, so how do we press the button for just one clock edge? Answer: we can t, but we can add some state that remembers what button was last clock cycle and then detect the clock cycles when button changes from 0 to 1. module onoff_sync(input clk, reset, button, output reg light); reg old_button; // state of button last clk always @ (posedge clk) begin if (reset) begin light <= 0; old_button <= 0; else if (old_button==0 && button==1) // button changed from 0 to 1 light <= ~light; old_button <= button; module 33
Asynchronous Inputs in Sequential Systems What about external signals? Clock Sequential System Can t guarantee setup and hold times will be met! When an asynchronous signal causes a setup/hold violation... Q D Clock I II III? Transition is missed on first clock cycle, but caught on next clock cycle. Transition is caught on first clock cycle. Output is metastable for an indeterminate amount of time. Q: Which cases are problematic? 34
This imag e canno t curre ntly be displa yed. Asynchronous Inputs in Sequential Systems All of them can be, if more than one happens simultaneously within the same circuit. Guideline: ensure that external signals directly feed exactly one flip-flop D Q Sequential System Async Input Clocked Synchronous System D Q Clock Q0 D Q Q1 Clock Clock This prevents the possibility of I and II occurring in different places in the circuit, but what about metastability? 35
Handling Metastability Preventing metastability turns out to be an impossible problem High gain of digital devices makes it likely that metastable conditions will resolve themselves quickly Solution to metastability: allow time for signals to stabilize Can be metastable right after sampling Very unlikely to be metastable for >1 clock cycle Extremely unlikely to be metastable for >2 clock cycles D Q D Q D Q Complicated Sequential Logic System Clock How many registers are necessary? Deps on many design parameters (clock speed, device speeds, ) In 6.111, a pair of synchronization registers is sufficient 36
One last little problem Mechanical buttons exhibit contact bounce when they change position, leading to multiple output transitions before finally stabilizing in the new position: We need a debouncing circuit! // Switch Debounce Module // use your system clock for the clock input // to produce a synchronous, debounced output // DELAY =.01 sec with a 27Mhz clock module debounce #(parameter DELAY=270000-1) (input reset, clock, bouncey, output reg steady); reg [18:0] count; reg old; always @(posedge clock) module 37
One last little problem Mechanical buttons exhibit contact bounce when they change position, leading to multiple output transitions before finally stabilizing in the new position: We need a debouncing circuit! // Switch Debounce Module // use your system clock for the clock input // to produce a synchronous, debounced output // DELAY =.01 sec with a 27Mhz clock module debounce #(parameter DELAY=270000-1) (input reset, clock, bouncey, output reg steady); reg [18:0] count; reg old; always @(posedge clock) if (reset) // return to known state begin count <= 0; old <= bouncey; steady <= bouncey; else if (bouncey!= old) // input changed begin old <= bouncey; count <= 0; else if (count == DELAY) // stable! steady <= old; else // waiting count <= count+1; module 38
On/off button: final answer module onoff_sync(input clk, reset, button_in, output reg light); // synchronizer reg button,btemp; always @(posedge clk) {button,btemp} <= {btemp,button_in}; // debounce push button wire bpressed; debounce db1(.clock(clk),.reset(reset),.bouncey(button),.steady(bpressed)); reg old_bpressed; // state last clk cycle always @ (posedge clk) begin if (reset) begin light <= 0; old_bpressed <= 0; else if (old_bpressed==0 && bpressed==1) // button changed from 0 to 1 light <= ~light; old_bpressed <= bpressed; module 39
Example: A Simple Counter Isn t this a lot like Exercise 1 in Lab 2? +1 1 0 0 1 0 4 4 count enb clr clk // 4-bit counter with enable and synchronous clear module counter(input clk,enb,clr, output reg [3:0] count); always @(posedge clk) begin count <= clr? 4 b0 : (enb? count+1 : count); module 40