Docsity

HDL Digital Design with Verilog: Reset, Conditionals, Cases, Loops, and Blocks, Slides of Data Communication Systems and Computer Networks

Partial preview of the text

Download HDL Digital Design with Verilog: Reset, Conditionals, Cases, Loops, and Blocks and more Slides Data Communication Systems and Computer Networks in PDF only on Docsity!

  • SS-CARE School of Engineering Spring
    • HDL Based Digital Design CE
      • Lecture

HDL Based Digital Design using Verilog By M. Mohsin Rahmatullah @ SS-CARE School of Engineering

Asynchronous reset^ Asynchronous reset

always @ (posedge clk or negedge rst_n) „

So whenever the reset is asserted theregisters are reset without waiting for thepositive edge of clk.

Conditional statements^ Conditional statements^ „

Conditional statements are usedfor making decisions based uponcertain conditions

If, If-else

case

Conditional Statement Examples //Type 1 statements if(!lock) buffer = data; if(enable) out = in; //Type 2 statements if (number_queued < MAX_Q_DEPTH) begin data_queue = data; number_queued = number_queued + 1; end else $display("Queue Full. Try again"); //Type 3 statements //Execute statements based on ALU control signal. if (alu_control == 0) y = x + z; else if(alu_control == 1) y = x - z; else if(alu_control == 2) y = x * z; else $display("Invalid ALU control signal");

module mux4_1(in1, in2, in3, in4, sel, out); input [1:0] sel; input [15:0] in1, in2, in3, in3; output [15:0] out; reg [15:0] out; always @(in1 or in2 or in3 or in4 or sel) case (sel) 2'b00: out = in1; 2'b01: out = in2; 2'b10: out = in3; 2'b11: out = in4; default: out = 16'bx;

Case statement Example^ Case statement Example endcase endmodule

casex, casez Keywords „

There are two variations of the casestatement. They are denoted by keywords,casex and casez.

„

casez treats all z values in the casealternatives or the case expression as don'tcares. All bit positions with z can alsorepresented by? in that position.

„

casex treats all x and z values in the caseitem or the case expression as don't cares

HDL Based Digital Design using Verilog By M. Mohsin Rahmatullah @ SS-CARE School of Engineering

Loop Statements^ Loop Statements „

There are four types of looping statements in Verilog: while, for,repeat, and forever „ repeat „ while „ for „ forever forever @(posedgeforever @( posedge clock) x = y; clock) x = y;

Sequential and Parallel Blocks „

Sequential Blocks „ The keywords begin and end are used to group statements intosequential blocks. „ The statements in a sequential block are processed in the orderthey are specified //Illustration 1: Sequential block without delay reg x, y; reg [1:0] z, w; initial begin x = 1'b0; y = 1'b1; z = {x, y}; w = {y, x}; end //Illustration 2: Sequential blocks with delay reg x, y; reg [1:0] z, w; initial begin x = 1'b0; //completes at simulation time 0 #5 y = 1'b1; //completes at simulation time 5 #10 z = {x, y}; //completes at simulation time 15 #20 w = {y, x}; //completes at simulation time 35 end

Sequential and Parallel Blocks „

Nested blocks

„

Blocks can be nested. Sequential andparallel blocks can be mixed

initial begin x = 1'b0; fork #5 y = 1'b1; #10 z = {x, y}; join #20 w = {y, x}; end

task and function „

Verilog provides tasks and functions to break uplarge behavioral designs into smaller pieces. „ Tasks and functions allow the designer to abstractVerilog code that is used at many places in thedesign „ Tasks have input, output, and inout arguments;functions have input arguments „ Both tasks and functions must be defined in amodule and are local to the module „ Tasks are used for common Verilog code thatcontains delays, timing, event constructs, ormultiple output arguments „ Functions are used when common Verilog code ispurely combinational, executes in zero simulationtime, and provides exactly one output