Chapter 4:: Hardware Description Languages: Digital Design and Computer Architecture
Chapter 4:: Hardware Description Languages: Digital Design and Computer Architecture
4-<1>
Introduction Hardware description language (HDL): allows designer to specify logic function only. Then a computer-aided design (CAD) tool produces or synthesizes the optimized gates. Most commercial designs built using HDLs Two leading HDLs:
Verilog
developed in 1984 by Gateway Design Automation became an IEEE standard (1364) in 1995
VHDL
Developed in 1981 by the Department of Defense Became an IEEE standard (1076) in 1987
Copyright 2007 Elsevier 4-<2>
HDL to Gates
Simulation
Input values are applied to the circuit Outputs checked for correctness Millions of dollars saved by debugging in simulation instead of hardware
Synthesis
Transforms HDL code into a netlist describing the hardware (i.e., a list of gates and the wires connecting them)
IMPORTANT: When describing circuits using an HDL, its critical to think of the hardware the code should produce.
Copyright 2007 Elsevier 4-<3>
Verilog Modules
4-<5>
4-<6>
Synthesis:
4-<7>
Verilog Syntax
Case sensitive
Example: reset and Reset are not the same signal.
4-<8>
// internal signal
and3 andgate(a, b, c, n1); // instance of and3 inv inverter(n1, y); // instance of inverter endmodule
Copyright 2007 Elsevier 4-<9>
Bitwise Operators
module gates(input [3:0] a, b, output [3:0] y1, y2, y3, y4, y5); /* Five different two-input logic gates acting on 4 bit busses */ assign y1 = a & b; // AND assign y2 = a | b; // OR assign y3 = a ^ b; // XOR assign y4 = ~(a & b); // NAND assign y5 = ~(a | b); // NOR endmodule
// /**/
Copyright 2007 Elsevier
Reduction Operators
module and8(input [7:0] a, output y); assign y = &a; // &a is much easier to write than // assign y = a[7] & a[6] & a[5] & a[4] & // a[3] & a[2] & a[1] & a[0]; endmodule
4-<11>
Conditional Assignment
module mux2(input [3:0] d0, d1, input s, output [3:0] y); assign y = s ? d1 : d0; endmodule
? :
Copyright 2007 Elsevier
is also called a ternary operator because it operates on 3 inputs: s, d1, and d0.
4-<12>
Internal Variables
module fulladder(input a, b, cin, output s, cout); wire p, g; // internal nodes assign p = a ^ b; assign g = a & b; assign s = p ^ cin; assign cout = g | (p & cin); endmodule
4-<13>
Precedence
Defines the order of operations
Highest
NOT mult, div, mod add,sub shift arithmetic shift equal, not equal AND, NAND XOR, XNOR OR, XOR ternary operator
4-<14>
Lowest
Copyright 2007 Elsevier
?:
Numbers
Format: N'Bvalue N = number of bits, B = base N'B is optional but recommended (default is decimal) Number
3b101 b11 8b11 8b1010_1011 3d6 6o42 8hAB 42
Copyright 2007 Elsevier
# Bits
3 unsized 8 8 3 6 8 Unsized
Base
binary binary binary binary decimal octal hexadecimal decimal
Decimal Equivalent
5 3 3 171 6 34 171 42
Stored
101 000011 00000011 10101011 110 100010 10101011 000101010
4-<15>
4-<16>
Synthesis:
4-<17>
Synthesis:
4-<18>
Tri-State Buffers Z value is the tri-stated value This example implements tri-state drivers driving BusOut
module tstate (EnA, EnB, BusA, BusB, BusOut); input EnA, EnB; input [7:0] BusA, BusB; output [7:0] BusOut; assign BusOut = EnA ? BusA : 8bZ; assign BusOut = EnB ? BusB : 8bZ; endmodule
Verilog - 19
Delays
module example(input a, b, c, output y); wire ab, bb, cb, n1, n2, n3; assign #1 {ab, bb, cb} = ~{a, b, c}; assign #2 n1 = ab & bb & cb; assign #2 n2 = a & bb & cb; assign #2 n3 = a & bb & c; assign #4 y = n1 | n2 | n3; endmodule
4-<20>
Delays
module example(input a, b, c, output y); wire ab, bb, cb, n1, n2, n3; assign #1 {ab, bb, cb} = ~{a, b, c}; assign #2 n1 = ab & bb & cb; assign #2 n2 = a & bb & cb; assign #2 n3 = a & bb & c; assign #4 y = n1 | n2 | n3; endmodule Delay annotation is ignored by synthesis! Only useful for simulation/modeling But may cause simulation to work when synthesis doesnt
Beware!!
4-<21>
Transport Delay
X <= #3 A ;
Current value of A is assigned to X, after 3 time units
Verilog - 22
Parameterized Modules
2:1 mux:
module mux2 #(parameter width = 8) // name and default value (input [width-1:0] d0, d1, input s, output [width-1:0] y); assign y = s ? d1 : d0; endmodule
Named Parameters
2:1 mux:
module mux2 #(parameter width = 8) // name and default value (input [width-1:0] d0, d1, input s, output [width-1:0] y);
Always Statement
General Structure:
always @ (sensitivity list) statement;
Whenever the event in the sensitivity list occurs, the statement is executed This is dangerous For combinational logic use the following!
always @ (*) statement;
4-<25>
Reminder: Variables assigned in an always statement must be declared as reg (even if theyre not actually registered!)
4-<26>
This hardware could be described with assign statements using fewer lines of code, so its better to use assign statements in this case.
Copyright 2007 Elsevier 4-<27>
4-<29>
y y y y y
= = = = =
// ? = dont care
4-<30>
for loops
Remember always block is executed at compile time!
module what ( input [8:0] data, output reg [3:0] count ); integer i; always @(*) begin count = 0; for (i=0; i<9; i=i+1) begin count = count + data[i]; end end endmodule
Copyright 2007 Elsevier 4-<31>
while loops
module what( input [15:0] in, output reg [4:0] out); integer i; always @(*) begin: count out = 0; i = 15; while (i >= 0 && ~in[i]) begin out = out + 1; i = i - 1; end end endmodule
Copyright 2007 Elsevier 4-<32>
module what( input [15:0] in, output reg [4:0] out ); integer i; always @(*) begin: count out = 0; for (i = 15; i >= 0; i = i - 1) begin if (~in[i]) disable count; out = out + 1; end end endmodule
Copyright 2007 Elsevier 4-<33>