Sequential Logic. Reminder: Lab #2 due Thursday Fall 2016 Lecture 4

Similar documents
Sequential Logic. Use Explicit Port Declarations. Verilog Summary. Examples

Sequential Logic. Reminder: Lab #2 due Thursday Fall 2017 Lecture 4

L5: Simple Sequential Circuits and Verilog

L5: Simple Sequential Circuits and Verilog

L5: Simple Sequential Circuits and Verilog

L5: Simple Sequential Circuits and Verilog

Graduate Institute of Electronics Engineering, NTU. Lecturer: Chihhao Chao Date:

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

ECE 551: Digital System *

Techniques for Digital Systems Lab. Verilog HDL. Tajana Simunic Rosing. Source: Eric Crabill, Xilinx

EECS150 - Digital Design Lecture 20 - Finite State Machines Revisited

Modeling Sequential Circuits in Verilog

ECE 2300 Digital Logic & Computer Organization. More Sequential Logic Verilog

EECS 270 Verilog Reference: Sequential Logic

Quick Introduction to SystemVerilog: Sequental Logic

Logic Circuits II ECE 2411 Thursday 4:45pm-7:20pm. Lecture 3

Last Lecture. Talked about combinational logic always statements. e.g., module ex2(input logic a, b, c, output logic f); logic t; // internal signal

In the previous lecture, we examined how to analyse a FSM using state table, state diagram and waveforms. In this lecture we will learn how to design

In the previous lecture, we examined how to analyse a FSM using state table, state diagram and waveforms. In this lecture we will learn how to design

ECE 574: Modeling and Synthesis of Digital Systems using Verilog and VHDL. Fall 2017 Final Exam (6.00 to 8.30pm) Verilog SOLUTIONS

Sequential Logic Design

Nonblocking Assignments in Verilog Synthesis; Coding Styles That Kill!

ECEN 468 Advanced Logic Design

5.14 Algorithmic State Machine (ASM) Charts

EE 231 Fall EE 231 Homework 8 Due October 20, 2010

Sequential Circuit Design: Principle

FPGA Design Challenge :Techkriti 14 Digital Design using Verilog Part 1

Digital Integrated Circuits

Writing Circuit Descriptions 8

Recommended Design Techniques for ECE241 Project Franjo Plavec Department of Electrical and Computer Engineering University of Toronto

In this lecture, we will go beyond the basic Verilog syntax and examine how flipflops and other clocked circuits are specified.

Lab 7 (All Sections) Prelab: Introduction to Verilog

Synthesizable Verilog

ECE 2300 Digital Logic & Computer Organization. More Finite State Machines

Lab 7 (Sections 300, 301 and 302) Prelab: Introduction to Verilog

Lecture 3. Behavioral Modeling Sequential Circuits. Registers Counters Finite State Machines

Verilog Sequential Logic. Verilog for Synthesis Rev C (module 3 and 4)

MCMASTER UNIVERSITY EMBEDDED SYSTEMS

Sequential Circuit Design: Principle

Verilog for High Performance

ECE 2300 Digital Logic & Computer Organization. More Verilog Finite State Machines

Blocking(=) vs Nonblocking (<=) Assignment. Lecture 3: Modeling Sequential Logic in Verilog HDL. Procedural assignments

EN164: Design of Computing Systems Lecture 07: Lab Foundations / Verilog 3

EEL 4783: HDL in Digital System Design

Debouncing a Switch. A Design Example. Page 1

a, b sum module add32 sum vector bus sum[31:0] sum[0] sum[31]. sum[7:0] sum sum overflow module add32_carry assign

ECE331: Hardware Organization and Design

The Verilog Language COMS W Prof. Stephen A. Edwards Fall 2002 Columbia University Department of Computer Science

Modeling Synchronous Logic Circuits. Debdeep Mukhopadhyay IIT Madras

CSCB58 - Lab 3. Prelab /3 Part I (in-lab) /2 Part II (in-lab) /2 TOTAL /8

Verilog for Synthesis Ing. Pullini Antonio

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Sciences

Timing and Verification

EE178 Lecture Verilog FSM Examples. Eric Crabill SJSU / Xilinx Fall 2007

Verilog introduction. Embedded and Ambient Systems Lab

Memory Controller. System Integration Issues. Encoding numbers 1GB RAM FSM. Communicating FSMs Clocking, theory and practice. Combinational Logic

Department of Computer Science and Electrical Engineering. CMPE 415 Verilog Events Timing and Testbenches Prof. Ryan Robucci

Control in Digital Systems

C A R L E T O N U N I V E R S I T Y. FINAL EXAMINATION April Duration: 3 Hours No. of Students: 108

Finite State Machines

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL

Testbenches for Sequential Circuits... also, Components

ECE 353 Lab 4. Verilog Review. Professor Daniel Holcomb UMass Amherst Fall 2017

Verilog Execution Semantics

Introduction to Verilog/System Verilog

Lecture 15: System Modeling and Verilog

Subject: Scheduling Region Questions and Problems of new SystemVerilog commands

EN2911X: Reconfigurable Computing Lecture 06: Verilog (3)

VHDL. VHDL History. Why VHDL? Introduction to Structured VLSI Design. Very High Speed Integrated Circuit (VHSIC) Hardware Description Language

Two HDLs used today VHDL. Why VHDL? Introduction to Structured VLSI Design

Last Lecture: Divide by 3 FSM

EECS150 - Digital Design Lecture 5 - Verilog Logic Synthesis

Lecture 12 VHDL Synthesis

Laboratory Exercise 3 Davide Rossi DEI University of Bologna AA

Introduction. Overview. Top-level module. EE108a Lab 3: Bike light

CSE 502: Computer Architecture

Verilog Nonblocking Assignments with Delays - Myths & Mysteries

Behavioral Modeling and Timing Constraints

Behavioral Modeling and Timing Constraints

ECE 4514 Digital Design II. Spring Lecture 13: Logic Synthesis

271/471 Verilog Tutorial

Lecture 32: SystemVerilog

DIGITAL SYSTEM DESIGN

Date Performed: Marks Obtained: /10. Group Members (ID):. Experiment # 11. Introduction to Verilog II Sequential Circuits

Mealy and Moore examples

N-input EX-NOR gate. N-output inverter. N-input NOR gate

Parallel versus serial execution

Graduate Institute of Electronics Engineering, NTU Design of Datapath Controllers

ECE 353 Lab 4. Verilog Review. Professor Daniel Holcomb With material by Professor Moritz and Kundu UMass Amherst Fall 2016

Note: Closed book no notes or other material allowed, no calculators or other electronic devices.

VHDL: RTL Synthesis Basics. 1 of 59

Verilog Overview. Verilog Overview. Simple Example. Simple Example. Simple Example. Simple Example

Digital Design with FPGAs. By Neeraj Kulkarni

Verilog Behavioral Modeling

Verilog Synthesis and FSMs. UCB EECS150 Fall 2010 Lab Lecture #3

ARM 64-bit Register File

Verilog Coding Guideline

Nonblocking Assignments in Verilog Synthesis, Coding Styles That Kill!

COPYRIGHTED MATERIAL. Architecting Speed. Chapter 1. Sophisticated tool optimizations are often not good enough to meet most design

Lab 2 Designing with Verilog

Transcription:

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