Chapter 3 Keywords 1
Verilog HDL:Digital Design and Modeling Chapter 3 Keywords Chapter - - PDF document
Verilog HDL:Digital Design and Modeling Chapter 3 Keywords Chapter - - PDF document
Chapter 3 Keywords 1 Verilog HDL:Digital Design and Modeling Chapter 3 Keywords Chapter 3 Keywords 2 Page 80 //4:1 multiplexer using a always @ (sel or data) //case statement begin module mux_4_1_case (sel, case (sel) data,
Chapter 3 Keywords 2
Page 80
//4:1 multiplexer using a //case statement module mux_4_1_case (sel, data, out); input [1:0] sel; input [3:0] data;
- utput out;
reg out; always @ (sel or data) begin case (sel) (0) : out = data[0]; (1) : out = data[1]; (2) : out = data[2]; (3) : out = data[3]; endcase end endmodule
Figure 3.3 Multiplexer design using a case statement. Page 81
//example of defparam module def_param1; parameter x1 = 0; initial $display ("value=%d", x1); endmodule
(a)
//define top level module //for defparam1 module top_level; defparam value1.x1 = 4; defparam value2.x1 = 8; def_param1 value1 ( ); def_param1 value2 ( ); endmodule
(b)
value = 8 value = 4 value = 0
(c) Figure 3.4 Verilog code to illustrate the use of the defparam keyword: (a) param- eter keyword, (b) defparam keyword to change the value, and (c) the outputs.
Chapter 3 Keywords 3
Page 86
//example of the repeat keyword module repeat_example; integer count; initial begin count = 0; repeat (8) begin $display ("count = %d", count); count = count + 1; end end endmodule
Figure 3.5 Example of the repeat keyword for loop control.
count = 0 count = 1 count = 2 count = 3 count = 4 count = 5 count = 6 count = 7
Figure 3.6 Outputs for the Verilog code of Figure 3.5.
Chapter 3 Keywords 4
Page 88
//illustrates the use of the while statement module while_example; integer count; initial begin count = 0; while (count < 16) begin $display ("count = %d", count); count = count + 1; end end endmodule
Figure 3.7 Example of the while keyword for loop control. Page 89
count = 0 count = 1 count = 2 count = 3 count = 4 count = 5 count = 6 count = 7 count = 8 count = 9 count = 10 count = 11 count = 12 count = 13 count = 14 count = 15
Figure 3.8 Outputs for the Verilog code of Figure 3.7.
Chapter 3 Keywords 5
Page 91
//example of specify block with delays module specify_block (x1, x2, z1); input x1, x2;
- utput z1;
nor (z1, x1, x2); specify specparam tplh = 0.55 : 0.90 : 1.20, //min : typ : max tphl = 0.50 : 0.70 : 1.55; (x1 => z1) = tplh, tphl; (x2 => z1) = tplh, tphl; endspecify endmodule
Figure 3.10 Example of a specify block. Page 96
//used-defined primitive for a 2-input OR gate primitive udp_or2 (out, a, b); //list output first //declarations
- utput out;
//must be output (not reg) //for comb logic input a, b; //state table definition table //inputs are in same order as input list // a b :
- ut;
comment is for readability : 0; 1 : 1; 1 : 1; 1 1 : 1; endtable endprimitive
Figure 3.12 A UDP for a 2-input OR gate.
Chapter 3 Keywords 6
Page 98
//module showing use of wire //connecting logic primitives module log_diag_eqn4 (x1, x2, x3, z1, z2); input x1, x2, x3;
- utput z1, z2;
wire x1, x2, x3; wire z1, z2; //define internal nets as wire wire net1,net2,net3,net4,net5; //instantiate the built-in //primitives and inst1 (net1, x1, ~x2); and inst2 (net2, ~x1, x2); not inst3 (net3, ~x3);
- r inst4 (net4, net1, net2);
- r inst5 (net5, net2, net3);
assign z1 = net4; assign z2 = net5; endmodule
Figure 3.14 Module showing the use of wires to connect logic primitives. Page 99
//logic diagram test bench module log_diag_eqn4_tb; reg x1, x2, x3; wire z1, z2; //display variables initial $monitor ("x1x2x3 = %b, z1 = %b, z2 = %b", {x1, x2, x3}, z1, z2); //apply stimulus initial begin #0 x1=1'b0; x2=1'b0; x3=1'b0; #10 x1=1'b0; x2=1'b0; x3=1'b1; #10 x1=1'b0; x2=1'b1; x3=1'b0; #10 x1=1'b0; x2=1'b1; x3=1'b1; #10 x1=1'b1; x2=1'b0; x3=1'b0; #10 x1=1'b1; x2=1'b0; x3=1'b1; #10 x1=1'b1; x2=1'b1; x3=1'b0; #10 x1=1'b1; x2=1'b1; x3=1'b1; #10 $stop; end //instantiate the module into //the test bench log_diag_eqn4 inst1 ( .x1(x1), .x2(x2), .x3(x3), .z1(z1), .z2(z2) ); endmodule
Figure 3.15 Test bench for the module of Figure 3.14.
Chapter 3 Keywords 7
Page 99
x1x2x3 = 000, z1 = 0, z2 = 0 x1x2x3 = 001, z1 = 0, z2 = 1 x1x2x3 = 010, z1 = 1, z2 = 1 x1x2x3 = 011, z1 = 1, z2 = 1 x1x2x3 = 100, z1 = 1, z2 = 0 x1x2x3 = 101, z1 = 1, z2 = 1 x1x2x3 = 110, z1 = 0, z2 = 0 x1x2x3 = 111, z1 = 0, z2 = 1
Figure 3.16 Binary outputs for the module of Figure 3.14. Figure 3.17 Waveforms for the test bench of Figure 3.15.
Chapter 3 Keywords 8
Page 105
//procedure for loading memory with //binary data from file icache.instr module mem_load (pc, ic_data_out); //list inputs and outputs input pc;
- utput ic_data_out;
//list wire and reg wire [2:0] pc; //a program counter to address 8 words reg [7:0] ic_data_out; //define memory size //instr_cache is an array of eight 8-bit regs reg [7:0] instr_cache [0:7]; //define memory contents //load instr_cache from file icache.instr initial begin $readmemb ("icache.instr", instr_cache); end //use a program counter to access the instr_cache always @ (pc) begin ic_data_out = instr_cache [pc]; end endmodule
Figure 3.12 Verilog module to illustrate the use of $readmemb to load an instruc- tion cache.
Chapter 3 Keywords 9
Page 106
//mem_load test bench module mem_load_tb; integer i;//used to display contents reg [2:0] pc; wire [7:0] ic_data_out; //assign values to the program counter initial begin #0 pc = 3'b000; #10 pc = 3'b001; #10 pc = 3'b010; #10 pc = 3'b011; #10 pc = 3'b100; #10 pc = 3'b101; #10 pc = 3'b110; #10 pc = 3'b111; #15 $stop; end //display the contents of the instruction cache initial begin for (i=0; i<8; i=i+1) begin #10 $display ("address %h = %b", i, ic_data_out); end #150 $stop; end //instantiate the module into the test bench mem_load inst1 ( .pc(pc), .ic_data_out(ic_data_out) ); endmodule
Figure 3.22 Test bench for the module of Figure 3.21.
Chapter 3 Keywords 10
Page 107
0000_1000 0000_1001 0000_1010 0000_1011 0000_1100 0000_1101 0000_1110 0000_1111
Figure 3.23 Instruction cache file icache.instr saved as a separate file. It is saved in the project folder without the .v extension.
address 00000000 = 00001000 address 00000001 = 00001001 address 00000002 = 00001010 address 00000003 = 00001011 address 00000004 = 00001100 address 00000005 = 00001101 address 00000006 = 00001110 address 00000007 = 00001111