ECE 274 Digital Logic RTL Design using Verilog Verilog for Digital Design Ch. 5.

Slides:



Advertisements
Similar presentations
Simulation executable (simv)
Advertisements

Verilog Overview. University of Jordan Computer Engineering Department CPE 439: Computer Design Lab.
Synchronous Sequential Logic
Combinational Logic.
Chapter 11 Verilog HDL Application-Specific Integrated Circuits Michael John Sebastian Smith Addison Wesley, 1997.
Verilog Intro: Part 1.
Hardware Description Language (HDL)
Give qualifications of instructors: DAP
Slide 1 7. Verilog: Combinational always statements. VHDL: Combinational Processes: To avoid (I.E. DO NOT What in your HDL code?) Cases that generate Synthesis.
CS 151 Digital Systems Design Lecture 37 Register Transfer Level
OUTLINE Introduction Basics of the Verilog Language Gate-level modeling Data-flow modeling Behavioral modeling Task and function.
Register-Transfer Level (RTL) Design
Chapter 9: Hardware Description Languages
Verilog Sequential Circuits Ibrahim Korpeoglu. Verilog can be used to describe storage elements and sequential circuits as well. So far continuous assignment.
ELEN 468 Lecture 161 ELEN 468 Advanced Logic Design Lecture 16 Synthesis of Language Construct II.
Today’s Lecture Process model –initial & always statements Assignments –Continuous & procedural assignments Timing Control System tasks.
Overview Logistics Last lecture Today HW5 due today
RTL Coding tips Lecture 7,8 Prepared by: Engr. Qazi Zia, Assistant Professor EED, COMSATS Attock.
Introduction to VHDL (part 2)
Advanced FPGA Based System Design Lecture-9 & 10 VHDL Sequential Code By: Dr Imtiaz Hussain 1.
ECE 2372 Modern Digital System Design
ECE 551 Digital System Design & Synthesis Fall 2011 Midterm Exam Overview.
CS 3850 Lecture 3 The Verilog Language. 3.1 Lexical Conventions The lexical conventions are close to the programming language C++. Comments are designated.
VHDL Introduction. V- VHSIC Very High Speed Integrated Circuit H- Hardware D- Description L- Language.
Chapter 11: System Design Methodology Digital System Designs and Practices Using Verilog HDL and 2008, John Wiley11-1 Ders 8: FSM Gerçekleme ve.
Digital System 數位系統 Verilog HDL Ping-Liang Lai (賴秉樑)  
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Slide 1 6. VHDL/Verilog Behavioral Description. Slide 2 Verilog for Synthesis: Behavioral description Instead of instantiating components, describe them.
Register Transfer Level & Design with ASM
1 CSE-308 Digital System Design (DSD) N-W.F.P. University of Engineering & Technology, Peshawar.
1 COMP541 Sequential Circuits Montek Singh Feb 1, 2012.
Anurag Dwivedi. Basic Block - Gates Gates -> Flip Flops.
Electrical and Computer Engineering University of Cyprus LAB 1: VHDL.
Slide 1 2. Verilog Elements. Slide 2 Why (V)HDL? (VHDL, Verilog etc.), Karen Parnell, Nick Mehta, “Programmable Logic Design Quick Start Handbook”, Xilinx.
3/4/20031 ECE 551: Digital System Design * & Synthesis Lecture Set 3 3.1: Verilog - User-Defined Primitives (UDPs) (In separate file) 3.2: Verilog – Operators,
1 Hardware description languages: introduction intellectual property (IP) introduction to VHDL and Verilog entities and architectural bodies behavioral,
Introduction to VHDL Simulation … Synthesis …. The digital design process… Initial specification Block diagram Final product Circuit equations Logic design.
Hardware Description Languages Digital Logic Design Instructor: Kasım Sinan YILDIRIM.
Behavioral Modelling - 1. Verilog Behavioral Modelling Behavioral Models represent functionality of the digital hardware. It describes how the circuit.
The Verilog Hardware Description Language. GUIDELINES How to write HDL code: How to write HDL code:
Verilog A Hardware Description Language (HDL ) is a machine readable and human readable language for describing hardware. Verilog and VHDL are HDLs.
M.Mohajjel. Structured Procedures Two basic structured procedure statements always initial All behavioral statements appear only inside these blocks Each.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL State Machines Anselmo Lastra.
1 Part III: VHDL CODING. 2 Design StructureData TypesOperators and AttributesConcurrent DesignSequential DesignSignals and VariablesState Machines A VHDL.
Introduction to ASIC flow and Verilog HDL
04/26/20031 ECE 551: Digital System Design & Synthesis Lecture Set : Introduction to VHDL 12.2: VHDL versus Verilog (Separate File)
Chapter 11: System Design Methodology Digital System Designs and Practices Using Verilog HDL and 2008, John Wiley11-1 Chapter 11: System Design.
ECE 274 Digital Logic Combinational Logic Design using Verilog Verilog for Digital Design Ch. 1 & 2.
Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 1 Verilog for Digital Design Chapter 3: Sequential Logic Design.
Verilog for Digital Design Copyright © 2007 Frank Vahid and Roman Lysecky 1 Verilog for Digital Design Chapter 5: RTL Design.
EMT 351/4 DIGITAL IC DESIGN Verilog Behavioral Modeling  Finite State Machine -Moore & Mealy Machine -State Encoding Techniques.
Pusat Pengajian Kejuruteraan Mikroelektronik EMT 351/4 DIGITAL IC DESIGN Verilog Behavioural Modeling (Part 4) Week #
Lecture 5. Verilog HDL #3 Prof. Taeweon Suh Computer Science & Engineering Korea University COSE221, COMP211 Logic Design.
Introduction to Verilog COE 202 Digital Logic Design Dr. Muhamed Mudawar King Fahd University of Petroleum and Minerals.
Structural Description
Overview Logistics Last lecture Today HW5 due today
EMT 351/4 DIGITAL IC DESIGN Week # Synthesis of Sequential Logic 10.
‘if-else’ & ‘case’ Statements
Introduction to Verilog
Developing More Advanced Testbenches
FSM MODELING MOORE FSM MELAY FSM. Introduction to DIGITAL CIRCUITS MODELING & VERIFICATION using VERILOG [Part-2]
332:437 Lecture 8 Verilog and Finite State Machines
Test Fixture (Testbench)
Verilog for Digital Design
COE 202 Introduction to Verilog
Dr. Tassadaq Hussain Introduction to Verilog – Part-3 Expressing Sequential Circuits and FSM.
The Verilog Hardware Description Language
332:437 Lecture 8 Verilog and Finite State Machines
Sequntial-Circuit Building Blocks
Presentation transcript:

ECE 274 Digital Logic RTL Design using Verilog Verilog for Digital Design Ch. 5

2 High-Level State Machine Behavior  Register-transfer level (RTL) design captures desired system behavior using high-level state machine  Earlier example – 3 cycles high, used FSM  What if 512 cycles high? 512-state FSM?  Better solution – High-level state machine that uses register to count cycles  Declare explicit register Cnt (2 bits for 3- cycles high)  Initialize Cnt to 2 (2, 1, 0  3 counts)  "On" state  Sets X=1  Configures Cnt for decrement on next cycle  Transitions to Off when Cnt is 0  Note that transition conditions use current value of Cnt, not next (decremented) value  For 512 cycles high, just initialize Cnt to 511 Inputs: B; Outputs: X On2On1On3 Off X=1 X=0 B' B 3 cycles with X=1 Inputs: B; Outputs: X; On Off X=1 X=0 B' B 3 cycles with X=1 Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

3 Combinational logic State register State x b clk FSM inputs FSM outputs StateNext High-Level State Machine Behavior  Module ports same as FSM  Same two-procedure approach as FSM  One for combinational logic, one for registers  Registers now include explicit registers (Cnt)  Two reg variables per explicit register (current and next), just like for state register `timescale 1 ns/1 ns module LaserTimer(B, X, Clk, Rst); input B; output reg X; input Clk, Rst; parameter S_Off = 0, S_On = 1; reg [0:0] State, StateNext; reg [1:0] Cnt, CntNext; // CombLogic Cnt, B) begin... end // Regs Clk) begin... end endmodule vldd_ch5_LaserTimerHLSM.v Combinational logic State register State X B Clk HLSM inputs HLSM outputs StateNext Cnt register Cnt CntNext

4 High-Level State Machine Behavior  CombLogic process  Describes actions and transitions... reg [0:0] State, StateNext; reg [1:0] Cnt, CntNext; // CombLogic Cnt, B) begin case (State) S_Off: begin X <= 0; CntNext <= 2; if (B == 0) StateNext <= S_Off; else StateNext <= S_On; end S_On: begin X <= 1; CntNext <= Cnt - 1; if (Cnt == 0) StateNext <= S_Off; else StateNext <= S_On; end endcase end... vldd_ch5_LaserTimerHLSM.v Combinational logic State register State X B Clk HLSM inputs HLSM outputs StateNext Cnt register Cnt CntNext Note: Writes are to "next" variable, reads are from "current" variable. See target architecture to understand why. Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

5 High-Level State Machine Behavior  Regs process  Updates registers on rising clock... // Regs Clk) begin if (Rst == 1 ) begin State <= S_Off; Cnt <= 0; end else begin State <= StateNext; Cnt <= CntNext; end... vldd_ch5_LaserTimerHLSM.v Combinational logic State register State X B Clk HLSM inputs HLSM outputs StateNext Cnt register Cnt CntNext Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

6 Simulating the HLSM  Use same testbench as in Chapter 3  Waveforms below also show Cnt and State variables, even though not a port on the LaserTime module  Simulators allow one to zoom into modules to select internal variables/nets to show  Note reset behavior  Until Rst=1 and rising clock, Cnt and State undefined  Upon Rst=1 and rising clock, Cnt set to 0, and State set to S_Off (which is defined as 0)  Note how system enters S_On on first rising clock after B becomes 1, causing Cnt to be initialized to 2  Cnt is decremented in S_On  Cnt wrapped from 0 to 3, but the 3 was never used Rst Clk X B State Cnt x X X 1 for 3 cycles Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

7 Top-Down Design: HLSM to Controller and Datapath  Recall from Chapters 2 & 3  Top-down design  Capture behavior, and simulate  Capture structure (circuit), simulate again  Gets behavior right first, unfettered by complexity of creating structure Capture behavior Simulate W_s P_s S_s K_s Capture structure Simulate W_s P_s S_s K_s Should be the same

8 Top-Down Design: HLSM to Controller and Datapath  Recall from Chapters 2 & 3  Top-down design  Capture behavior, and simulate  Capture structure (circuit), simulate again  Gets behavior right first, unfettered by complexity of creating structure  At RTL level  Capture behavior  HLSM  Capture structure: Controller and datapath ControllerDatapath Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

9 Top-Down Design: HLSM to Controller and Datapath  Deriving a datapath from the HLSM Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0 Cnt "10" 2x1 ="00" S Ld Clk Cnt_Ld Cnt_Eq_0 Cnt_Sel 01 Datapath

10 Top-Down Design: HLSM to Controller and Datapath  Deriving a controller  Replace HLSM by FSM that uses the datapath Inputs: B, Cnt_Eq_0; Outputs: X, Cnt_Sel, Cnt_Ld; On Off X=1 X=0 B' B Cnt_Sel=1, Cnt_Ld=1 Cnt_Sel=0, Cnt_Ld=1 (Cnt_Eq_0)' Cnt_Eq_0 Controller Cnt "10" 2x1 ="00" S Ld Clk Cnt_Ld Cnt_Eq_0 Cnt_Sel 01 Datapath Inputs: B; Outputs: X; On Off X=1 X=0 B' B Register: Cnt(2) Cnt=2 Cnt=Cnt-1 (Cnt=0)' Cnt=0

11 Top-Down Design: HLSM to Controller and Datapath  Describe controller and datapath in VHDL  One option: structural datapath, behavioral (FSM) controller  Let's instead describe both behaviorally Inputs: B, Cnt_Eq_0; Outputs: X, Cnt_Sel, Cnt_Ld; On Off X=1 X=0 B' B Cnt_Sel=1, Cnt_Ld=1 Cnt_Sel=0, Cnt_Ld=1 (Cnt_Eq_0)' Cnt_Eq_0 Controller Cnt "10" 2x1 ="00" S Ld Clk Cnt_Ld Cnt_Eq_0 Cnt_Sel 01 Datapath

12 Describing a Datapath Behaviorally  Two procedures  Combinational part and register part  Current and next signals shared between the two parts  Just like for FSM behavior Cnt "10" 2x1 ="00" S Ld 01 Datapath CntCntNext DpCombLogic DpRegs... // Shared variables reg Cnt_Eq_0, Cnt_Sel, Cnt_Ld; // Controller variables reg [0:0] State, StateNext; // Datapath variables reg [1:0] Cnt, CntNext; // Datapath Procedures // // DP CombLogic Cnt) begin if (Cnt_Sel==1) CntNext <= 2; else CntNext <= Cnt - 1; Cnt_Eq_0 <= (Cnt==0)?1:0; end // DP Regs Clk) begin if (Rst == 1 ) Cnt <= 0; else if (Cnt_Ld==1) Cnt <= CntNext; end... vldd_ch5_LaserTimerCtrlDpBeh.v Note use of previously-introduced conditional operator Clk Cnt_Ld Cnt_Eq_0 Cnt_Sel

13 Describing the Controller Behaviorally  Standard approach for describing FSM  Two procedures... // Controller Procedures // // Ctrl CombLogic Cnt_Eq_0, B) begin case (State) S_Off: begin X <= 0; Cnt_Sel <= 1; Cnt_Ld <= 1; if (B == 0) StateNext <= S_Off; else StateNext <= S_On; end S_On: begin X <= 1; Cnt_Sel <= 0; Cnt_Ld <= 1; if (Cnt_Eq_0 == 1) StateNext <= S_Off; else StateNext <= S_On; end endcase end // Ctrl Regs Clk) begin if (Rst == 1 ) begin State <= S_Off; end else begin State <= StateNext; end... vldd_ch5_LaserTimerCtrlDpBeh.v Inputs: B, Cnt_Eq_0; Outputs: X, Cnt_Sel, Cnt_Ld; On Off X=1 X=0 B' B Cnt_Sel=1, Cnt_Ld=1 Cnt_Sel=0, Cnt_Ld=1 (Cnt_Eq_0)' Cnt_Eq_0 Controller

14 Controller and Datapath Behavior  Result is one module with four procedures  Datapath procedures (2)  Combinational logic  Registers  Controller procedures (2)  Combinational logic  Registers... module LaserTimer(B, X, Clk, Rst);... parameter S_Off = 0, S_On = 1; // Shared variables reg Cnt_Eq_0, Cnt_Sel, Cnt_Ld; // Controller variables reg [0:0] State, StateNext; // Datapath variables reg [1:0] Cnt, CntNext; // Datapath Procedures // // DP CombLogic Cnt) begin... end // DP Regs Clk) begin... end // Controller Procedures // // Ctrl CombLogic Cnt_Eq_0, B) begin... end // Ctrl Regs Clk) begin... end endmodule vldd_ch5_LaserTimerCtrlDpBeh.v Inputs: B, Cnt_Eq_0; Outputs: X, Cnt_Sel, Cnt_Ld; On Off X=1 X=0 B' B Cnt_Sel=1, Cnt_Ld=1 Cnt_Sel=0, Cnt_Ld=1 (Cnt_Eq_0)' Cnt_Eq_0 Controller Cnt "10" 2x1 ="00" S Ld Clk Cnt_Ld Cnt_Eq_0 Cnt_Se l 01 Datapath

15 One-Procedure State Machine Description  Previously described HLSM using two procedures  One for combinational logic, one for registers  Required "current" and "next" signals for each register  A one-procedure description is possible too  One procedure per HLSM  One variable per register  Simpler code with clear grouping of functionality  But may change timing... module LaserTimer(B, X, Clk, Rst);... reg [0:0] State, StateNext; reg [1:0] Cnt, CntNext; // CombLogic Cnt, B) begin... end // Regs Clk) begin... end endmodule... module LaserTimer(B, X, Clk, Rst);... reg [0:0] State; reg [1:0] Cnt; Clk) begin end endmodule vldd_ch5_LaserTimerHLSM.v vhdd_ch5_LaserTimerHLSMOneProc.v

16 One-Procedure State Machine Description  Procedure sensitive to clock only  If not reset, then executes current state's actions, and assigns next value of state  Why didn't we describe with one procedure before?  Main reason – Procedure synchronous to clock only  Inputs become synchronous  Changes the state machine's timing  e.g., In state S_off, and B changes to 1 in middle of clock cycle  Previous two procedure model  Change in b immediately noticed by combinational logic procedure, which configures next state to be S_On. State changes to S_On on next rising clock.  One procedure model  Change in b not noticed until next rising clock, so next state still S_Off. After rising clock, procedure configures next state to be S_On. State changes to S_On on the next rising clock – or two rising clocks after b changed  See next slide for timing diagrams... module LaserTimer(B, X, Clk, Rst);... parameter S_Off = 0, S_On = 1; reg [0:0] State; reg [1:0] Cnt; Clk) begin if (Rst == 1 ) begin State <= S_Off; Cnt <= 0; end else begin case (State) S_Off: begin X <= 0; Cnt <= 2; if (B == 0) State <= S_Off; else State <= S_On; end S_On: begin X <= 1; Cnt <= Cnt - 1; if (Cnt == 0) State <= S_Off; else State <= S_On; end endcase end endmodule vldd_ch5_LaserTimerHLSMOneProc.v

17 Timing Differences Between Two and One Procedure Descriptions  Previous two procedure description  Change in B immediately noticed by combinational logic procedure, which configures next state to be S_On. State changes to S_On (1) on next rising clock (setting X=1).  One procedure description  Change in B not noticed until next rising clock, so next state still S_Off (0). After rising clock, procedure configures next state to be S_On (1). State changes to S_On on the next rising clock (setting X=1) – two rising clocks after b changed  Likewise, initial reset also delayed Synchronization of input can delay impact of input changes Rst Clk X B State Rst Clk X B State

18 Algorithmic-Level Behavior  Higher than HLSM?  HLSM high-level, but even higher sometimes better  Algorithmic-level  Behavior described as sequence of steps  Similar to sequential program  SAD example  Compute sum of absolute differences of corresponding pairs in two arrays  Common task in video compression to determine difference between two successive video frames B A Go SAD Sad_Out 256-byte array 32-bits

19 Algorithmic-Level Behavior for SAD  Most easily described as an algorithm  Input is two arrays A and B  Wait until Go is '1'  Use for loop to step through each array item  Adds absolute difference to running sum  Update output after loop completes  Note: No intention of providing this description to synthesis tool  Merely used for early system simulation Wait until Go equals 1 Sum = 0 for I in 0 to 255 loop Sum = Sum + |A(I) - B(I)| end loop SAD_Out = Sum Note: Above algorithm written in pseudo- code, not in a particular language B A Go SAD SAD_Out 256-byte array 32-bits

20 Algorithmic-Level Behavior for SAD  Description uses several language features to be described on next several slides  User-defined function  Returns a value to be used in an expression  This function named "ABS"  Will compute absolute value  Returns integer value  One input argument, integer  Contents assign return value to be positive version of input argument  This function contains only one statement, but may contain any number  ABS can then be called by later parts of the description `timescale 1 ns/1 ns module SAD(Go, SAD_Out); input Go; output reg [31:0] SAD_Out; reg [7:0] A [0:255]; reg [7:0] B [0:255]; integer Sum; integer I; function integer ABS; input integer IntVal; begin ABS = (IntVal>=0)?IntVal:-IntVal; end endfunction // Initialize Arrays initial $readmemh("MemA.txt", A); initial $readmemh("MemB.txt", B); always begin if (!(Go==1)) end Sum = 0; for (I=0; I<=255; I=I+1) begin Sum = Sum + ABS(A[I] - B[I]); end #50; SAD_Out <= Sum; end endmodule vldd_ch5_SADAlg.v

21 Algorithmic-Level Behavior for SAD  Description declares A and B as 256- element arrays of bytes  Initializes those arrays using built-in system task $readmemh  Reads file of hex numbers (first argument)  Each number placed into subsequent element of array (second argument)  Number of numbers in file should match number of elements  No length or base format for numbers  Separated by white space  e.g., 00 FF A  $readmemb: Reads file of binary numbers  Note: Called as only statement of "initial" procedure  Could have used begin-end block: initial begin $readmemh("MemA.txt", A); end `timescale 1 ns/1 ns module SAD(Go, SAD_Out); input Go; output reg [31:0] SAD_Out; reg [7:0] A [0:255]; reg [7:0] B [0:255]; integer Sum; integer I; function integer ABS; input integer IntVal; begin ABS = (IntVal>=0)?IntVal:-IntVal; end endfunction // Initialize Arrays initial $readmemh("MemA.txt", A); initial $readmemh("MemB.txt", B); always begin if (!(Go==1)) end Sum = 0; for (I=0; I<=255; I=I+1) begin Sum = Sum + ABS(A[I] - B[I]); end #50; SAD_Out <= Sum; end endmodule vldd_ch5_SADAlg.v

22 Event Control with an Expression  – Event control with an expression  If Go not 1, wait until Go becomes 1  Previous event control expressions were either one event, such Clk), or a list of events such  But expression can be a longer expression too,  Waits not just for change on Go, but for a change on Go such that Go equals 1... always begin if (!(Go==1)) end Sum = 0; for (I=0; I<=255; I=I+1) begin Sum = Sum + ABS(A[I] - B[I]); end #50; SAD_Out <= Sum; end... vldd_ch5_SADAlg.v Delay of 50 ns added just so that result doesn't appear instantaneously

23 Algorithmic-Level Behavior  Testbench  Setup similar to previous testbenches  Activate SAD with Go_s <= 1  Simulation generates SAD_out  SAD_out is uninitialized at first, resulting in unknown value  50 ns later, result is 4  Which is correct for the value with which we initialized the memories A and B  More thorough algorithmic-level description would be good  Perhaps try different sets of values `timescale 1 ns/1 ns module Testbench(); reg Go_s; wire [31:0] SAD_Out_s; SAD CompToTest(Go_s, SAD_Out_s); // Vector Procedure initial begin Go_s <= 0; #10 Go_s <= 1; #10 Go_s <= 0; #60 if (SAD_Out_s != 4) begin $display("SAD failed -- should equal 4"); end endmodule SAD_Out Go vldd_ch5_SADAlgTB.v xxxxxx

24 Convert Algorithm to HLSM !Go S0 Go S1 Sum = 0 I = 0 S3 Sum=Sum+ABS(A[I]-B[I]) I=I+1 S4 SAD_Reg = Sum S2 I<256 (I<256)’ Local registers: Sum, SAD_Reg (32 bits); I (integer)

25 Describe HLSM in Verilog `timescale 1 ns/1 ns module SAD(Go, SAD_Out, Clk, Rst); input Go; output [31:0] SAD_Out; input Clk, Rst; parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3, S4 = 4; reg [7:0] A [0:255]; reg [7:0] B [0:255]; reg [2:0] State; integer Sum, SAD_Reg; integer I; function integer ABS; input integer IntVal; begin ABS = (IntVal>=0)?IntVal:-IntVal; end endfunction // Initialize Arrays initial $readmemh("MemA.txt", A); initial $readmemh("MemB.txt", B); // High-level state machine Clk) begin if (Rst==1) begin State <= S0; Sum <= 0; SAD_Reg <= 0; I <= 0; end else begin case (State) S0: begin if (Go==1) State <= S1; else State <= S0; end S1: begin Sum <= 0; I <= 0; State <= S2; end S2: begin if (!(I==255)) State <= S3; else State <= S4; end S3: begin Sum <= Sum + ABS(A[I]-B[I]); I <= I + 1; State <= S2; end S4: begin SAD_Reg <= Sum; State <= S0; end endcase end vldd_ch5_SADHLSM.v Clk and Rst inputs and functionality introduced

26 Describe HLSM in Verilog  Use similar testbench as for algorithmic-level description  Clk, Rst introduced  Minor change – wait for (256*2+3) * (20 ns) for SAD result to appear  HLSM involves better clock cycle accuracy than algorithmic model  Implementation – Proceed to convert HLSM to controller/datapath, as before `timescale 1 ns/1 ns module Testbench(); reg Go_s; reg Clk_s, Rst_s; wire [31:0] SAD_Out_s; SAD CompToTest(Go_s, SAD_Out_s, Clk_s, Rst_s); // Clock Procedure always begin Clk_s <= 0; #10; Clk_s <= 1; #10; end // Note: Procedure repeats // Vector Procedure initial begin Rst_s <= 1; Go_s <= Clk_s); Rst_s <= 0; Go_s <= Clk_s); Go_s <= 0; #((256*2+3)*20) if (SAD_Out_s != 4) begin $display("SAD failed -- should equal 4"); end endmodule vldd_ch5_SADHLSMTB.v Go SAD_Out Clk Rst time=10,290 ns

27 Accessing Memory  Memory may be initially accessed as array for simplicity  SAD example  Declared two 256-item arrays  Eventually, may want to describe memory as external component  Closer to real implementation module SAD(Go, SAD_Out); input Go; output reg [31:0] SAD_Out; reg [7:0] A [0:255]; reg [7:0] B [0:255]; B A Go SAD SAD_Out B A Go SAD SAD_Out 256-byte memory

28 Simple Memory Entity  Simple read-only memory  Addr and data ports only  Declares array named Memory for storage  Procedure uses continuous assignment statement to always output Memory data element corresponding to current address input value `timescale 1 ns/1 ns module SADMem(Addr, Data); input [7:0] Addr; output [7:0] Data; reg [7:0] Memory [0:255]; assign Data = Memory[Addr]; endmodule vldd_ch5_SADMem.v

29 Accessing Memory  Modify SAD's HLSM with extra state (S3a) for reading the memories  Extra state necessary due to use of fully- synchronous HLSM (modeled as one procedure sensitive only to clock)  A_addr & B_addr are output ports connected to memory address inputs  A_data and B_data are input ports connected to memory data outputs !Go S0 Go S1 Sum = 0 I = 0 S3 Sum=Sum+ABS(A_Data - B_Data) I=I+1 S4 SAD_Reg = Sum S2 I<256 (I<256)’ Local registers: Sum, SAD_Reg (32 bits); I (integer) S3a A_Addr = I B_Addr = I

30 `timescale 1 ns/1 ns module SAD(Go, A_Addr, A_Data, B_Addr, B_Data, SAD_Out, Clk, Rst); input Go; input [7:0] A_Data, B_Data; output reg [7:0] A_Addr, B_Addr; output [31:0] SAD_Out; input Clk, Rst; parameter S0 = 0, S1 = 1, S2 = 2, S3 = 3, S3a = 4, S4 = 5; reg [2:0] State; reg [31:0] Sum, SAD_Reg; integer I; function [7:0] ABSDiff; input [7:0] A; input [7:0] B; begin if (A>B) ABSDiff = A - B; else ABSDiff = B - A; end endfunction vldd_ch5_SADHLSM_Mem.v Clk) begin if (Rst==1) begin A_Addr <= 0; B_Addr <= 0; State <= S0; Sum <= 0; SAD_Reg <= 0; I <= 0; end else begin case (State) S0: begin if (Go==1) State <= S1; else State <= S0; end S1: begin Sum <= 0; I <= 0; State <= S2; end S2: begin if (!(I==255)) State <= S3a; else State <= S4; end Created AbsDiff function for improved readability S3a: begin A_Addr <= I; B_Addr <= I; State <= S3; end S3: begin Sum <= Sum + ABSDiff(A_Data, B_Data); I <= I + 1; State <= S2; end S4: begin SAD_Reg <= Sum; State <= S0; end endcase end assign SAD_Out = SAD_Reg; endmodule

31 Testbench  Instantiate and connect modules for SAD, SADMemA, and SADMemB  Note two instances of same memory  Initialize memories  Note how we can access SADMemA.Memory (and B) from testbench  Vector procedure adjusted from earlier to account for extra state  ((256*2+3)*20) changed to ((256*3+3)*ClkPeriod)  Also note use of parameter ClkPeriod – allows us to change period in one place module Testbench(); reg Go_s; wire [7:0] A_Addr_s, B_Addr_s; wire [7:0] A_Data_s, B_Data_s; reg Clk_s, Rst_s; wire [31:0] SAD_Out_s; parameter ClkPeriod = 20; SAD CompToTest(Go_s, A_Addr_s, A_Data_s, B_Addr_s, B_Data_s, SAD_Out_s, Clk_s, Rst_s); SADMem SADMemA(A_Addr_s, A_Data_s); SADMem SADMemB(B_Addr_s, B_Data_s); // Clock Procedure always begin Clk_s <= 0; #(ClkPeriod/2); Clk_s <= 1; #(ClkPeriod/2); end // Initialize Arrays initial $readmemh("MemA.txt", SADMemA.Memory); initial $readmemh("MemB.txt", SADMemB.Memory); // Vector Procedure initial begin... // Reset behavior not shown Go_s <= Clk_s); Go_s <= 0; #((256*3+3)*ClkPeriod) if (SAD_Out_s != 4) begin $display("SAD failed -- should equal 4"); end endmodule vldd_ch5_SADHLSM_MemTB.v

32 Waveforms  Waveforms now include address and data lines with memories  We also added some internal signals, State, I, Sum, and SAD_Reg  SAD_Out result appears later than previously, due to extra state ns A_Data B_Addr A_Addr Go Rst Clk Sum B_Data State I SAD_Reg SAD_Out