1 A Brief Intro to Verilog Brought to you by: Sat Garcia 2 Meet your 141(L) TA  Sat Garcia sat@cs.ucsd.edu 2nd Year Ph.D. Student Office Hours: (Tentative)  Place: EBU3b B225 (basement)  Monday: 3-4pm  Wednesday: 11am-Noon  Please come to my office hours. I get lonely there by myself! 2 3 What is Verilog?  Verilog is: A hardware design language (HDL) Tool for specifying hardware circuits Syntactically, a lot like C or Java An alternative to VHDL (and more widely used) What you'll be using in 141L HELLA COOL!* * If you are totally into hardware design languages 4 Verilog in the Design Process Behavioral Algorithm Register Transfer Level Gate Level Manual Logic Synthesis Auto Place + Route Test Results Simulate Test Results Simulate Test Results Adapted from Arvind & Asanovic’s MIT 6.375 lecture 3 5 Ways To Use Verilog  Structural Level  Lower level  Has all the details in it (which gates to use, etc)  Is always synthesizable  Functional Level  Higher Level  Easier to write  Gate level, RTL level, high-level behavioral  Not always synthesizable  We’ll be sticking with functional mostly 6 Data Types in Verilog  Basic type: bit vector Values: 0, 1, X (don't care), Z (high impedence)  Bit vectors expressed in multiple ways: binary: 4'b11_10 ( _ is just for readability) hex: 16'h034f decimal: 32'd270 other formats but these are the most useful 4 7 Data types (continued)  Connect things together with: wire Single wire:  wire my_wire; “Array” of wires  wire[7:0] my_wire;  Why not wire[0:7]?  For procedural assignments, we'll use reg Again, can either have a single reg or an array  reg[3:0] accum; // 4 bit “reg” reg is not necessarily a hardware register 8 A simple example (comb. circuit)  Let's design a 1 bit full adder FA ba s cin cout module FA( input a, b, cin, output s, cout); assign s = a ^ b ^ c; assign cout = (a & b) | (a & cin) | (b & cin); endmodule  Ok, but what if we want more than 1 bit FA? *** Note: red means new concept, blue and green are just pretty colors :-p Adapted from Arvind & Asanovic’s MIT 6.375 lecture 5 9 A 4-bit Full Adder  We can use 1 bit FA to build a 4 bit full adder module 4bitFA( input [3:0] A, B, input cin, output [3:0] S, output cout); wire c0, c1, c2; FA fa0(A[0],B[0],cin,S[0],c0); // implicit binding FA fa1(.a(A[1]), .b(B[1]), .cin(c0), .s(S[1]), .cout(c1)); // explicit binding FA fa2(A[2],B[2],c1,S[2],c2); FA fa3(A[3],B[3],c2,S[3],cout); endmodule FA FA FA FA Adapted from Arvind & Asanovic’s MIT 6.375 lecture 10 Testing the adder `timescale 1ns/1ns // Add this to the top of your file to set time scale module testbench(); reg [3:0] A, B; reg C0; wire [3:0] S; wire C4; 4bitFA uut (.B(B), .A(A), .cin(C0), .S(S), .cout(C4)); // instantiate adder initial // initial blocks run only at the beginning of simulation (only use in testbenches) begin $monitor($time,"A=%b,B=%b, c_in=%b, c_out=%b, sum = %b\n",A,B,C0,C4,S); end initial begin A = 4'd0; B = 4'd0; C0 = 1'b0; #50 A = 4'd3; B = 4'd4; // wait 50 ns before next assignment #50 A = 4'b0001; B = 4'b0010; // don’t use #n outside of testbenches end endmodule 6 11 Verilog RTL Operators  Avoid using %, **, and / because you'll run into problems when trying to synthesis ~ & | ^ ^~Bitwise == != === !=== Equality > < >= <=Relational ! && ||Logical + - * / % **Arithmetic & ~& | ~| ^ ^~ Reduction >> << >>> <<