Introduction to Verilog
Awirespecifies a combinational signal.Think of it as an actual wire.Areg(register) holds a value.A reg need not necessarily correspond to an actual register in an implementation, although it often will.
Constants is represented by prefixing the value with a decimal number specifying its size in bits.For example:4’b0100 specifies a 4-bit binary constant with the value 4, as does 4’d4.
The possible values for a register or wire in Verilog are0 or 1, representing logical false or truex, representing unknown, the initial value given to all registers and to any wire not connected to somethingz, representing the high-impedance state fortristategates
Verilog provides the full set of unary and binary operators from C, includingthe arithmetic operators (+, –, *, /),the logical operators (&, |, ~),the comparison operators (==, !=, >, <, <=, >=),the shift operators (<<, >>)Conditional operator (?, which is used in the form condition ? expr1 :expr2 and returns expr1 if the condition is true and expr2 if it is false).
Structure of a Verilog Program
AVerilogprogram is structured as a set of modules, which may represent anything from a collection of logic gates to a complete system.A module specifies its input and output ports, which describe the incoming and outgoing connections of a module.A module may also declare additional variables.The body of a module consists ofinitial constructs, which can initializeregvariablescontinuous assignments, which define only combinational logicalways constructs, which can define either sequential or combinational logicinstances of other modules, which are used to implement the module being defined
Key things to remember
A module in Verilog is NOT a function in software.A function is a piece of code that can be called.A module defines a functionality. A module can be USED, NOT CALLED. Once you use a module, a physical piece of hardware will be allocated in the chip. If you use a module twice, there will be two pieces of such hardware.It is like building a car which needs 4 tires, not just getting one tire and call it4 times.Verilog is a Hardware Description Language.Youdescribewhat you need.
The half-adder. Example of continuous assignments
module half_adder (A,B,Sum,Carry);input A,B;output Sum, Carry;assign Carry = A & B;assign Sum = A ^ B;endmodule
assign: continuous assignments. Any change in the input is reflected immediately in the output.Wires may be assigned values only with continuous assignments.
Behavioral description – The always block
moduletwo_one_Selector(A,B,Sel,O);inputA,B,Sel;outputregO;always @(A, B,Sel)if (Sel== 0)O <= A;elseO <= B;endmodule
always @(A, B,Sel) – means that the block is reevaluated every time any one of the signals in the list changes valueNOT A FUNCTION CALLIf no sensitive list, always evaluatedAlways keep in mind that it is used to describe the behavior of a piece of hardware you wish to design. Basically, it is used to tell Verilog what kind of gates should be used.
Always block continued
Only reg variables can be assigned values in the always block – output reg O;When we want to describe combinational logic using an always block, care must be taken to ensure that the reg does not synthesize into a register.
reg variables can be assigned values in the always block in two ways:``=’’ the blocking assignment. Just like C. The assignment will be carried out one-by-one. One thing does not happen until the one before it happens.This is the behavior of the circuit!``<=’’ the nonblocking assignment. All assignment happen at the same time.
A Sample Verilog code
modulehalf_adder(A,B,Sum,Carry);input A,B;output Sum, Carry;assign Sum = A ^ B;assign Carry = A & B;endmodulemoduletwo_one_Selector(A,B,Sel,O);inputA,B,Sel;outputregO;//output O;always @(A, B,Sel)if (Sel== 0)O <= A;elseO <= B;endmodule
modulehalf_adder_test_bench();wireA,B,S,C,Sel,O;regosc;initial beginosc= 0;endalways begin#10osc= ~osc;Endassign A=1;assign B=0;assignSel=osc;half_adderA1(A, B, S, C);two_one_SelectorS1(A,B,Sel,O);endmodule
One-bit Full Adder
module full_adder (A,B,Cin,Sum, Cout);input A,B,Cin;output Sum, Cout;assign Sum = (A & B & Cin) | (~A & ~B & Cin) | (~A & B & ~Cin) | (A & ~B & ~Cin);assign Cout = (A & Cin) | (A & B) | (B & Cin);endmodule
module four_bit_adder (A,B,Cin,Sum, Cout);input [3:0] A;input [3:0] B;input Cin;output [3:0] Sum;output Cout;wire C0, C1, C2;full_adder FA1(A, B, Cin, Sum, C0);full_adder FA2(A, B, C0, Sum, C1);full_adder FA3(A, B, C1, Sum, C2);full_adder FA4(A, B, C2, Sum, Cout);endmodule
module MIPSALU (ALUctl, A, B, ALUOut, Zero);input [3:0] ALUctl;input [31:0] A,B;output reg [31:0] ALUOut;output Zero;assign Zero = (ALUOut==0); //Zero is true if ALUOut is 0; goes anywherealways @(ALUctl, A, B) //reevaluate if these changecase (ALUctl)0: ALUOut <= A & B;1: ALUOut <= A | B;2: ALUOut <= A + B;6: ALUOut <= A - B;7: ALUOut <= A < B ? 1:0;12: ALUOut <= ~(A | B); // result is nordefault: ALUOut <= 0; //default to 0, should not happen;endcaseendmodule
Instructions about the Verilog Simulator
You may download and install AlteraModelSimby first going to page:https://www.altera.com/download/sw/dnl-sw-index.jspthen download a version of“ModelSim-Altera Edition (includes Starter Edition).”Best part: It is free software and no license is required!To run a simulation, you mayOpen your Verilog file.In the menu bar, click “Compile.” In the drop-down menu, click “Compile.” A dialogue window should pop-up.Select the file to compile and clickthe “Compile” button. If there is no problem with your code, the compile should pass, and then you should click the “Done” button to close the dialogue window.In the menu bar, click “Simulate.” In the drop-down menu, click “Start Simulation.” A dialogue window should pop-up. There should be a list showing up in the window. Click on the “+” sign on “work” which is the first item in the list. Click on “test_bench.” Then click the “OK” button. The dialogue should disappear.After a little while, left click on “test_bench” in the “work space” window to select it, then right click. In the menu, select “Add Wave”.There will be a new window waveform popping up. First, find the box showing “100ps” and change it to “1000 ps.” Then click the run simulation sign right next to the box. The waveform should be ready!