Verilog HDL: Module
Verilog HDL: Module
Verilog is a concurrent programming language unlike C, which is sequential in nature. initial block - executes once at time 0. If there is more then one block, each execute concurrently always block executes continuously Modeling Levels Switch-Level, Gate-Level, Dataflow, Behavioral Assignments Blocking assignment: = executed in order they appear in a block Nonblocking assignment: <= allow scheduling of assignments without blocking execution of statements that follow in a sequential block Continuous assignment assign A = B; connects nets permanently. Example 1: #10 A = 1b1; counter = 0; Example 2: A <= #10 1b1; counter <= 0;
Sequential Black begin-end Parallel Blocks fork-join Module definition: module <module_name> (<module_terminal_list>); <module_terminal_definitions> <functionality_of_module> endmodule // semicolon required!!!
// no semicolon!!!
Gate-Level Modeling
Example 1: Full Adder
module FullAdder(X, Y, Cin, Cout, Sum); input X, Y, Cin; // input terminal definitions output Cout, Sum; // output terminal definitions wire w1, w2, w3, w4; xor #(10) (w1, X, Y); xor #(10) xor2(Sum, w1, Cin); and and and #(10) (w2, X, Y); #(10) (w3, X, Cin); #(10) (w4, Y, Cin); // internal net declarations // delay time of 10 units // with instance name
// // // // //
Dataflow Modeling
Example 4: 4-to-1 Multiplexer
module mux4_to_1(in, out, sel); input [3:0] in; output out; input [1:0]sel;
// continuous assignment with delay assign #10 out = (~sel[1] & ~sel[0] (~sel[1] & sel[0] ( sel[1] & ~sel[0] ( sel[1] & sel[0] endmodule
Behavioral Modeling
All behavioral statements must be in initial or always blocks Example 6: Clock Generator
module ClkGen; reg clk; initial clk = 1b0; always #10 clk = ~clk; initial #1000 $finish; endmodule //or $stop to end simulation
Example 10: Ripple-Carry Counter (with active high reset) 4-bit Ripple-carry counter. Instantiates 4 negative edge triggered T-flipflops from D-flipflop
module RCC(Q, CLK, RESET); output [3:0]Q; input CLK, RESET;
module TFF(Q, CLK, RESET); output Q; input CLK, RESET; wire D, QN; DFF dff(D, !CLK, Q, QN, RESET, 1b1); assign D = QN; endmodule
module LCD_DRV(DATA, CLK, SEGMENTS, COM); input [3:0] DATA; // BCD input input CLK; // 60-100Hz clock input output [6:0] SEGMENTS; // LCD A-G segment lines output COM; // LCD COM line always @(DATA or CLK) begin assign COM = CLK; case (DATA) 4b0000: if(CLK == 1b0) SEGMENTS = DSP0; else SEGMENTS = DSP0 ^ 4b0001: if(CLK == 1b0) SEGMENTS = DSP1; else SEGMENTS = DSP1 ^ 4b0010: if(CLK == 1b0) SEGMENTS = DSP2; else SEGMENTS = DSP2 ^ 4b0011: if(CLK == 1b0) SEGMENTS = DSP3; else SEGMENTS = DSP3 ^ 4b0100: if(CLK == 1b0) SEGMENTS = DSP4; else SEGMENTS = DSP4 ^ 4b0101: if(CLK == 1b0) SEGMENTS = DSP5; else SEGMENTS = DSP5 ^ 4b0110: if(CLK == 1b0)
7b1111111;
7b1111111;
7b1111111;
7b1111111;
7b1111111;
7b1111111;
4b0111:
4b1000:
4b1001:
default:
SEGMENTS = DSP6; else SEGMENTS = DSP6 ^ 7b1111111; if(CLK == 1b0) SEGMENTS = DSP7; else SEGMENTS = DSP7 ^ 7b1111111; if(CLK == 1b0) SEGMENTS = DSP8; else SEGMENTS = DSP8 ^ 7b1111111; if(CLK == 1b0) SEGMENTS = DSP9; else SEGMENTS = DSP9 ^ 7b1111111; if(CLK == 1b0) SEGMENTS = BLANK; else SEGMENTS = BLANK ^ 7b1111111;
Example 12: State Machine Two roads intersect: the highway and the country road. On the highway the green light is always on unless the sensor on the country road detects a car. The green light on the country road stays on until all cars leave that road. Model the traffic lights there. Set 3 clock cycle delay for yellow to red signal change and 2 for red to green for both directions. S0 Highway = Green, Country = Red S1 Highway = Yellow, Country = Red S2 Highway = Red, Country = Red S3 Highway = Red, Country = Green S4 Highway = Red, Country = Yellow
define RED 2d0 define YELLOW 2d1 define GREEN 2d2 define define define define define S0 S1 S2 S3 S4 3d0 3d1 3d2 3d3 3d4
// delays in clock cycles define Y2RDELAY 3 define R2GDELAY 2 module sig_control(highway_signal, country_signal, sensor, clock) output [1:0] highway_signal, country_signal; reg [1:0] highway_signal, country_signal; input sensor, clock; reg[2:0] state, nextstate; initial begin state = S0; nextstate = S0; highway_signal = GREEN; country_signal = RED;
end always @(posedge clock) state = nextstate; always @(state) begin case (state) S0: begin highway_signal country_signal end S1: begin highway_signal country_signal end S2: begin highway_signal country_signal end S3: begin highway_signal country_signal end S4: begin highway_signal country_signal end endcase end
= GREEN; = RED; = YELLOW; = RED; = RED; = RED; = RED; = GREEN; = RED; = YELLOW;
always @(state or sensor) begin case(state) S0: if(sensor) nextstate = S1; else nextstate = S0; S1: begin repeat(Y2RDELAY) @(posedge clock) nextstate = S2; end S2: begin repeat(R2GDELAY) @(posedge clock) nextstate = S3; end S3: if(sensor) nextstate = S3; else nextstate = S4; S4: begin repeat(Y2RDELAY) @(posedge clock) nextstate = S0; end default: nextstate = S0; endcase end endmodule
begin left_addr = shift(addr, LEFT_SHIFT); right_addr = shift(addr, RIGHT_SHIFT); end // Define the shift function. The output is a 32-bit value function [31:0] shift; input [31:0] address; input control; begin shift = (control == LEFT_SHIFT) ? (address << 1) : (address >> 1); end endfunction endmodule
Switch-Level Modeling
Example 15: 2-to-1 Multiplexer
module mux(out, in, sel); output out; input[1:0]in; input sel; wire sel_b; //declare power and ground supply1 pwr; supply2 gnd; // implement the NOT gate pmos(sel_b, pwr, sel); nmos(sel_b, gnd, sel); // implement 2 pass gate switch gates cmos(out, in[0], sel_b, sel); cmos(out, in[1], sel, sel_b); endmodule