Lab 1 - ALU (Arithmetic/Logical Unit) : Similar Development Chain
Lab 1 - ALU (Arithmetic/Logical Unit) : Similar Development Chain
CSE 372 (Martin): Synthesizable Verilog 1 CSE 372 (Martin): Synthesizable Verilog 2
CSE 372 (Martin): Synthesizable Verilog 5 CSE 372 (Martin): Synthesizable Verilog 6
CSE 372 (Martin): Synthesizable Verilog 7 CSE 372 (Martin): Synthesizable Verilog 8
Structural vs Behavioral HDL Constructs Verilog Structural Example
Structural
• Structural constructs specify actual hardware structures module mux2to1(S, A, B, Out);
• Low-level, direct correspondence to hardware S
input S, A, B;
• Primitive gates (e.g., and, or, not) output Out; A
• Hierarchical structures via modules wire S_, AnS_, BnS; Out
• Analogous to programming software in assembly not (S_, S); B
CSE 372 (Martin): Synthesizable Verilog 9 CSE 372 (Martin): Synthesizable Verilog 10
CSE 372 (Martin): Synthesizable Verilog 11 CSE 372 (Martin): Synthesizable Verilog 12
Wire Assignment Verilog “Behavioral” Example
“Behavioral” (Synthesizable)
• Wire assignment: “continuous assignment” module mux2to1(S, A, B, Out);
• Connect combinational logic block or other wire to wire input S
input S, A, B;
• Order of statements not important, executed totally in parallel output Out; A
• When right-hand-side changes, it is re-evaluated and re-assigned wire S_, AnS_, BnS; Out
• Designated by the keyword assign assign S_ = ~S; B
wire c; assign AnS_ = A & S_;
assign c = a | b; assign BnS =B & S;
assign Out = AnS_ | BnS;
endmodule
• Can be combined with declaration
wire c = a | b; // same thing module mux2to1(S, A, B, Out);
• Basic operators input S, A, B;
• Not: ~ Or: | And: & Xor: ^ output Out;
assign Out = (~S & A) | (S & B);
• Can be combined: (a & b) | (c ^ d)
endmodule
CSE 372 (Martin): Synthesizable Verilog 13 CSE 372 (Martin): Synthesizable Verilog 14
CSE 372 (Martin): Synthesizable Verilog 15 CSE 372 (Martin): Synthesizable Verilog 16
Vectors of Wires Vector Constants
• Wire vectors:
wire [7:0] W1; // 8 bits, w1[7] is MSB • Constants:
wire [0:7] W2; // 8 bits, w2[0] is MSB • assign x = 3’b011
• Also called “arrays” or “busses” • The “3” is the number of bits
• The “b” means “binary” - “h” for hex, “d” for decimal
• Operations • The “011” are the digits (in binary in this case)
• Bit select: W1[3]
• Range select: W1[3:2]
• Concatenate: {<expr>[,<expr>]*}
vec = {x, y, z};
{carry, sum} = vec[0:1];
• e.g., swap high and low-order bytes of 16-bit vector
wire [15:0] w1, w2;
assign w2 = {w1[7:0], w1[15:8]}
CSE 372 (Martin): Synthesizable Verilog 17 CSE 372 (Martin): Synthesizable Verilog 18
CSE 372 (Martin): Synthesizable Verilog 19 CSE 372 (Martin): Synthesizable Verilog 20
Conditional Operator Hierarchical Design using Modules
• Verilog supports the ?: conditional operator • Interface specification
• Almost never useful in C (in my opinion) module mux2to1(S, A, B, O);
• Much more useful (and common) in Verilog input S, A, B;
output O;
• Examples: • Can also have inout: bidirectional wire (we will not need)
assign out = S ? B : A; • Alternative: Verilog 2001 interface specification
module mux2to1(input S, A, B, output O);
assign out = (sel == 2'b00) ? a : • Declarations
(sel == 2'b01) ? b :
• Internal wires, i.e., “local” variables
(sel == 2'b10) ? c :
(sel == 2'b11) ? d : 1'b0; • Wires also known as “nets” or “signals”
wire S_, AnS_, BnS;
• What do these do? • Implementation: primitive and module instantiations
and (AnS_, A, S_);
CSE 372 (Martin): Synthesizable Verilog 21 CSE 372 (Martin): Synthesizable Verilog 22
CSE 372 (Martin): Synthesizable Verilog 25 CSE 372 (Martin): Synthesizable Verilog 26
• Parameter vs `define
• Parameter only for “per instance” constants
• `define for “global” constants
CSE 372 (Martin): Synthesizable Verilog 29 CSE 372 (Martin): Synthesizable Verilog 30
CSE 372 (Martin): Synthesizable Verilog 31 CSE 372 (Martin): Synthesizable Verilog 32
Sequential Logic in Verilog Sequential Logic in Verilog
• How do we specify state-holding constructs in Verilog? • How do we specify state-holding constructs in Verilog?
module dff (out, in, wen, rst, clk); module register (out, in, wen, rst, clk);
wen = write enable parameter n = 1; wen = write enable
output out; rst = reset output [n-1:0] out; rst = reset
input in; clk = clock input [n-1:0] in; clk = clock
input wen, rst, clk; input wen, rst, clk;
• Ramifications:
• Never do logic operations on the clocks
• If you want to add a “write enable” to a flip-flop:
• Use a mux to route the old value back into it
• (or use the flip-flop with write enable we give you!)
• Do not just “and” the write-enable signal with the clock!
CSE 372 (Martin): Synthesizable Verilog 35 CSE 372 (Martin): Synthesizable Verilog 36
Levels of Simulation Testbenches
• Functional (or Behavioral) Simulation • How does one test code?
• Simulates Verilog abstractly
• No timing information, can’t detect “timing bugs” • In C/Java?
• Post-synthesis Timing Simulation • Write test code in C/Java to test C/Java
• Simulating devices generated via synthesis • “Test harness”, “unit testing”
• Gates, transistors, FPGA logical units (LUTs or lookup tables)
• No interconnect delay • For Verilog/VHDL?
• Not all internal signals may still exist • Write test code in Verilog to test Verilog
• Synthesis might have optimized or changed the design
• Verilog has advanced “behavioral” commands to facilitate this:
• Slower
• Delay for n units of time
• Layout Timing Simulation • Full high-level constructs: if, while, sequential assignment, ints
• After synthesis, the tool “places and routes” the logic blocks
• Input/output: file I/O, output to display, etc.
• Includes all sources of delay
• Even slower
• Example as part of Lab 1
CSE 372 (Martin): Synthesizable Verilog 37 CSE 372 (Martin): Synthesizable Verilog 38