Verilog 1 Fundamentals FA FA module adder input
![Verilog 1 - Fundamentals FA FA module adder( input [3: 0] A, B, output Verilog 1 - Fundamentals FA FA module adder( input [3: 0] A, B, output](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-1.jpg)
















![Connecting modules A B adder cout FA FA S module adder( input [3: 0] Connecting modules A B adder cout FA FA S module adder( input [3: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-18.jpg)
![Alternative syntax Connecting ports by ordered list FA fa 0( A[0], B[0], 1’b 0, Alternative syntax Connecting ports by ordered list FA fa 0( A[0], B[0], 1’b 0,](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-19.jpg)














![Register module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] d, input Register module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] d, input](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-34.jpg)
![Register in terms of Flipflops module register 2 ( input clk, input [1: 0] Register in terms of Flipflops module register 2 ( input clk, input [1: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-35.jpg)
![Static Elaboration: Generate module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] Static Elaboration: Generate module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-36.jpg)

- Slides: 37
![Verilog 1 Fundamentals FA FA module adder input 3 0 A B output Verilog 1 - Fundamentals FA FA module adder( input [3: 0] A, B, output](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-1.jpg)
Verilog 1 - Fundamentals FA FA module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], 1’b 0, c 1, c 2, c 0, c 1, c 2, cout, S[0] S[1] S[2] S[3] ); ); endmodule 6. 375 Complex Digital Systems Arvind February 6, 2009 1

Verilog Fundamentals History of hardware design languages Data types Structural Verilog Simple behaviors FA FA module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], 1’b 0, c 1, c 2, c 0, c 1, c 2, cout, S[0] S[1] S[2] S[3] ); ); endmodule 2

Originally designers used breadboards for prototyping Number of Gates in Design 107 Solderless Breadboard 106 tangentsoft. net/elec/breadboard. html No symbolic execution or testing 105 104 103 102 Printed circuit board home. cogeco. ca 10 3

HDLs enabled logic level simulation and testing Number of Gates in Design Gate Level Description Simulate 107 Test Results Manual 106 105 104 103 102 HDL = Hardware Description Language 10 4

Designers began to use HDLs for higher level design Behavioral Algorithm Register Transfer Level Simulate Gate Level Manual Test Results Number of Gates in Design Test Results HDL models offered “precise” & executable specification but the translation between the levels remained manual 107 106 105 104 103 102 10 5

HDLs led to tools for automatic translation Behavioral Algorithm Simulate Test Results Number of Gates in Design Manual Register Transfer Level Logic Synthesis Gate Level Simulate Test Results Auto Place + Route HDLs: Verilog, VHDL… Tools: Spice, Model. Sim, Design. Compiler, … 107 106 105 104 103 102 10 6

Raising the abstraction further … Guarded Atomic Actions Simulate Test Results Number of Gates in Design GAA Compiler Register Transfer Level Logic Synthesis Gate Level Auto Place + Route Simulate Test Results Bluespec and associated tools 107 106 105 104 103 102 10 7

The current situation Behavioral Level C, C++, System. C Bluespec Behavioral RTL MATLAB Verilog, VHDL, System. Verilog Structural RTL Register Transfer Level Verilog, VHDL, System. Verilog Logic Synthesis Simulators and other tools are available at all levels but not compilers from the behavioral level to RTL Gate Level 8

Common misconceptions The only behavioural languages are C, C++ RTL languages are necessarily lower-level than behavioral languages n n Yes- in the sense that C or System. C is farther away from hardware No- in the sense that HDLs can incorporate the most advanced language ideas. Bluespec is a modern high-level language and at the same time can describe hardware to the same level of precision as RTL 9

Verilog Fundamentals History of hardware design languages Data types Structural Verilog Simple behaviors FA FA module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], 1’b 0, c 1, c 2, c 0, c 1, c 2, cout, S[0] S[1] S[2] S[3] ); ); endmodule 10

Bit-vector is the only data type in Verilog A bit can take on one of four values Value Meaning 0 Logic zero 1 Logic one X Unknown logic value Z High impedance, floating An X bit might be a 0, 1, Z, or in transition. We can set bits to be X in situations where we don’t care what the value is. This can help catch bugs and improve synthesis quality. 11

“wire” is used to denote a hardware net Absolutely no type safety when connecting nets! small_net ? instruction memory_req instruction wire [15: 0] instruction; wire [15: 0] memory_req; wire [ 7: 0] small_net; 12

Bit literals 4’b 10_11 Underscores are ignored Base format (d, b, o, h) Decimal number representing size in bits Binary literals n 8’b 0000_0000 n 8’b 0 xx 0_1 xx 1 Hexadecimal literals n 32’h 0 a 34_def 1 n 16’haxxx Decimal literals n 32’d 42 We’ll learn how to actually assign literals to nets a little later 13

Verilog Fundamentals History of hardware design languages Data types Structural Verilog Simple behaviors FA FA module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], 1’b 0, c 1, c 2, c 0, c 1, c 2, cout, S[0] S[1] S[2] S[3] ); ); endmodule 14

A Verilog module has a name and a port list A B 4 4 adder 4 cout sum module adder( A, B, cout, sum ); input [3: 0] A; input [3: 0] B; output cout; output [3: 0] sum; // HDL modeling of // adder functionality endmodule Ports must have a direction (or be bidirectional) and a bitwidth Note the semicolon at the end of the port list! 15

Alternate syntax A B 4 4 adder 4 cout sum Traditional Verilog-1995 Syntax module adder( A, B, cout, sum ); input [3: 0] A; input [3: 0] B; output cout; output [3: 0] sum; ANSI C Style Verilog-2001 Syntax module adder( input [3: 0] A, input [3: 0] B, output cout, output [3: 0] sum ); 16

A module can instantiate other modules A B adder cout FA FA FA S module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0(. . . ); FA fa 1(. . . ); FA fa 2(. . . ); FA fa 3(. . . ); endmodule FA a cout FA b cin c module FA( input a, b, cin output cout, sum ); // HDL modeling of 1 bit // full adder functionality endmodule 17
![Connecting modules A B adder cout FA FA S module adder input 3 0 Connecting modules A B adder cout FA FA S module adder( input [3: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-18.jpg)
Connecting modules A B adder cout FA FA S module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], endmodule 1’b 0, c 1, c 2, c 0, S[0] ); c 1, S[1] ); c 2, S[2] ); cout, S[3] ); Carry Chain 18
![Alternative syntax Connecting ports by ordered list FA fa 0 A0 B0 1b 0 Alternative syntax Connecting ports by ordered list FA fa 0( A[0], B[0], 1’b 0,](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-19.jpg)
Alternative syntax Connecting ports by ordered list FA fa 0( A[0], B[0], 1’b 0, c 0, S[0] ); Connecting ports by name (compact) FA fa 0(. a(A[0]), . b(B[0]), . cin(1’b 0), . cout(c 0), . sum(S[0]) ); Argument order does not matter when ports are connected by name FA fa 0 (. a. cin. b. cout. sum (A[0]), (1’b 0), (B[0]), (c 0), (S[0]) ); Connecting ports by name yields clearer and less buggy code. 19

Verilog Fundamentals History of hardware design languages Data types Structural Verilog Simple behaviors FA FA module adder( input [3: 0] A, B, output cout, output [3: 0] S ); wire c 0, c 1, c 2; FA fa 0( A[0], B[0], FA fa 1( A[1], B[1], FA fa 2( A[2], B[2], FA fa 3( A[3], B[3], 1’b 0, c 1, c 2, c 0, c 1, c 2, cout, S[0] S[1] S[2] S[3] ); ); endmodule 20

A module’s behavior can be described in many different ways but it should not matter from outside Example: mux 4 21

mux 4: Gate-level structural Verilog module mux 4(input a, b, c, d, input [1: 0] sel, output out); wire [1: 0] sel_b; b d a c sel[1]sel[0] not 0( sel_b[0], sel[0] ); not 1( sel_b[1], sel[1] ); wire n 0, n 1, n 2, and 0( n 0, c, and 1( n 1, a, and 2( n 2, d, and 3( n 3, b, n 3; sel[1] sel_b[1] ); ); wire x 0, x 1; nor 0( x 0, n 1 ); nor 1( x 1, n 2, n 3 ); wire y 0, y 1; or or 0( y 0, x 0, sel[0] ); or or 1( y 1, x 1, sel_b[0] ); nand 0( out, y 0, y 1 ); endmodule out 22

mux 4: Using continuous assignments module mux 4( input a, b, c, d input [1: 0] sel, output out ); Language defined operators wire out, t 0, t 1; assign out = ~( (t 0 | sel[0]) & (t 1 | ~sel[0]) ); assign t 1 = ~( (sel[1] & d) | (~sel[1] & b) ); assign t 0 = ~( (sel[1] & c) | (~sel[1] & a) ); endmodule The order of these continuous assignment statements does not matter. They essentially happen in parallel! 23

mux 4: Behavioral style // Four input multiplexer module mux 4( input a, b, c, d input [1: 0] sel, output out ); assign out = ( ( endmodule sel sel == == 0 1 2 3 ) ) ? ? a b c d : : 1’bx; If input is undefined we want to propagate that information. 24

mux 4: Using “always block” module mux 4( input a, b, c, d input [1: 0] sel, output out ); Motivated by simulation reg out, t 0, t 1; always @( a or b or c or d or sel ) begin t 0 = ~( (sel[1] & c) | (~sel[1] & a) ); t 1 = ~( (sel[1] & d) | (~sel[1] & b) ); out = ~( (t 0 | sel[0]) & (t 1 | ~sel[0]) ); endmodule The order of these procedural assignment statements DOES matter. They essentially happen sequentially! 25

“Always blocks” permit more advanced sequential idioms module mux 4( input a, b, c, d input [1: 0] sel, output out ); reg out; always @( * ) begin if ( sel == 2’d 0 ) out = a; else if ( sel == 2’d 1 ) out = b else if ( sel == 2’d 2 ) out = c else if ( sel == 2’d 3 ) out = d else out = 1’bx; endmodule mux 4( input a, b, c, d input [1: 0] sel, output out ); reg out; always @( * ) begin case ( sel ) 2’d 0 : out = a; 2’d 1 : out = b; 2’d 2 : out = c; 2’d 3 : out = d; default : out = 1’bx; endcase endmodule Typically we will use always blocks only to describe sequential circuits 26

What happens if the case statement is not complete? module mux 3( input a, b, c input [1: 0] sel, output out ); reg out; always @( * ) begin case ( sel ) 2’d 0 : out = a; 2’d 1 : out = b; 2’d 2 : out = c; endcase end If sel = 3, mux will output the previous value! What have we created? endmodule 27

What happens if the case statement is not complete? module mux 3( input a, b, c input [1: 0] sel, output out ); reg out; always @( * ) We CAN prevent creating begin state with a default case ( sel ) statement 2’d 0 : out = a; 2’d 1 : out = b; 2’d 2 : out = c; default : out = 1’bx; endcase endmodule 28

Parameterized mux 4 default value module mux 4 #( parameter WIDTH = 1 ) ( input[WIDTH-1: 0] a, b, c, d input [1: 0] sel, output[WIDTH-1: 0] out ); wire [WIDTH-1: 0] out, t 0, t 1; assign t 0 = (sel[1]? c : a); assign t 1 = (sel[1]? d : b); Instantiation Syntax assign out = (sel[0]? t 0: t 1); mux 4#(32) alu_mux endmodule Parameterization is a good practice for reusable modules Writing a muxn is challenging (. a (op 1), . b (op 2), . c (op 3), . d (op 4), . sel (alu_mux_sel), . out (alu_mux_out) ); 29

Verilog Registers “reg” Wires are line names – they do not represent storage and can be assigned only once Regs are imperative variables (as in C): n n “nonblocking” assignment r <= v can be assigned multiple times and holds values between assignments 30

flip-flops module FF 0 (input clk, input output reg q); always @( posedge clk ) begin q <= d; endmodule d, module FF (input clk, input d, input en, output reg q); always @( posedge clk ) begin if ( en ) q <= d; endmodule next_X clk D Q X enable 31

flip-flops with reset always @( posedge clk) begin if (~reset. N) Q <= 0; else if ( enable ) Q <= D; end synchronous reset always @( posedge clk or negedge reset. N) begin if (~reset. N) Q <= 0; else if ( enable ) Q <= D; end asynchronous reset next_X clk D reset. N X Q enable What is the difference? 32

Latches versus flip-flops module latch ( input clk, input d, output reg q ); always @( clk or d ) begin if ( clk ) q <= d; endmodule flipflop ( input clk, input d, output reg q ); always @( posedge clk ) begin q <= d; endmodule Edge-triggered always block 33
![Register module registerparameter WIDTH 1 input clk input WIDTH1 0 d input Register module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] d, input](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-34.jpg)
Register module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] d, input en, output [WIDTH-1: 0] q ); always @( posedge clk ) begin if (en) q <= d; endmodule 34
![Register in terms of Flipflops module register 2 input clk input 1 0 Register in terms of Flipflops module register 2 ( input clk, input [1: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-35.jpg)
Register in terms of Flipflops module register 2 ( input clk, input [1: 0] d, input en, output [1: 0] q ); always @(posedge clk) begin if (en) q <= d; endmodule register 2 ( input clk, input [1: 0] d, input en, output [1: 0] q ); FF ff 0 (. clk(clk), . d(d[0]), . en(en), . q(q[0])); FF ff 1 (. clk(clk), . d(d[1]), . en(en), . q(q[1])); endmodule Do they behave the same? yes 35
![Static Elaboration Generate module registerparameter WIDTH 1 input clk input WIDTH1 0 Static Elaboration: Generate module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0]](https://slidetodoc.com/presentation_image/3d0c0a988163bd7d07a1b98b1a031caf/image-36.jpg)
Static Elaboration: Generate module register#(parameter WIDTH = 1) ( input clk, input [WIDTH-1: 0] d, input en, genvars disappear after output [WIDTH-1: 0] q static elaboration ); Generated names will have genvar i; reg. E[i]. prefix generate for (i =0; i < WIDTH; i = i + 1) begin: reg. E FF ff(. clk(clk), . d(d[i]), . en(en), . q(q[i])); endgenerate endmodule 36

Three abstraction levels for functional descriptions Behavioral Algorithm V Abstract algorithmic description V Describes how data flows between state elements for each cycle V Low-level netlist of primitive gates Manual Register Transfer Level Logic Synthesis Gate Level Auto Place + Route Next time Some examples 37