Thursday, July 4, 2013

Verilog Datatypes: Wires,regs and ports

Wires

Wires aka nets are used to interconnect modules. They can be 1 bit or wider. Wire having more than one bit is called vector or bus. On other hand the wires carry output of one module to the input of other module. See how modules are interconnected here.
wire [7:0] test_wire ;
The above statement declares a wire named test_wire with a width of 8 bit.

Regs

Regs are used in procedural statements. In procedural statements the output is assigned to Regs. The reg types can be either sequential or combinational. It can be single bit or vector type. Procedural statements are those statements which appear inside the always block
reg [7:0] test_reg ;
The declaration of a 8 bit reg named test_reg is as above.

Ports

Inside a module the results or data is available on either Wires or Regs. Procedural statements keep the data in regs and assignment statements keep the values in wires. This data are tapped outside the module through ports, exactly output ports. Likewise input to a module is passed through port known as input port.
input  [7:0] test_input ,
output [7:0] test_output ,
output reg [7:0] test_output_reg ,
The first one is a input port. The second one is output port of wire type and the last one is output port of reg type.


Tuesday, July 2, 2013

Equality operators in Verilog

These operators are used to check the equivalence, greater than or less than. But two additional operators are here which may seem unfamiliar.

Operator Description
== Equivalence
=== Literal equivalence
!= Inequivaity
!== Literal inequivaity
< Less than
> Greater than
Less than or equal to
Greater than or equal to

The following sample code and outputs demonstrates the operators.
module equality_operator ;
    initial begin 
        $display("%b",1'bz==1'bx) ;
        $display("%b",1'bz===1'bx) ;
        $finish ;
    end
endmodule 
Download
$ x
$ 0
Here I am only pointing out the difference between the equivalence operator and the Literal equivalence operator. While comparing two values we expect either a logical one or zero. But in simulation both unknowns(X) and high impedence(Z) values are present.
To get either logical one or zero we should use Literal equivalence operator in simulation, otherwise the output may be X or Z.
But in case of hardware, code for synthesis, we can use equivalence operator, beacause hardware has either logical one or zero.

Concatenation operator in Verilog

Concatenation operator is used to make a larger operand from smaller operands or to split a large operand into smaller ones. It is illegal to use unsized numbers inside the concatenation operator.

The following sample code and outputs demonstrates the operators.
module concatenation_operator ;
    reg [1:0] a ;
    reg [1:0] b ;
    reg [3:0] c ;
    initial begin 
        c = {2'b10,2'b11} ;
        $display("c={2'b10,2'b11}=%b",{2'b10,2'b11}) ;
        {a,b} = c ;
        $display("a = %b \nb = %b",a,b) ;
        $finish ;
    end
endmodule 
Download
$ c={2'b10,2'b11}=1011
$ a = 10
$ b = 11
Two 2-bit numbers are concatenated to form a 4-bit number and is assigned to a 4-bit register c. Now the larger register c is split into two 2-bit values and are assigned to two 2-bit register a and b.

Monday, July 1, 2013

Ternary operator in Verilog

Ternary operator is used to check "if then else"condition in Verilog. It has three operands.
w = x ? y : z ;
The format of Ternary operator is as shown above. If x is one then y is assigned to w and else z is assigned. Ternary operator can be nested to make multi level if statements.
The following sample code and outputs demonstrates the operators.
module ternary_operators ;
    reg [1:0] a,b,;
    reg d ;
    initial begin 
        a = 0 ;
        b = 1 ;
        c = 2 ;
        d = 1 ;
        a = d ? b : c ;
        $display(" a = %d",a) ;
        $finish ;
    end
endmodule 
Download
$ a = 1
In the above example value is assigned to a with a ternary_operator. Here the value of d is checked initially which is found to be 1. Hence a is assigned with the value of d which is 1. If d were 0 then a should have been 2. It simply work as a 2X1 Mux.

Reduction operators in Verilog

Reduction operators are unary operators as they work on a single operand. They work with a multiple-bit operand and reduce them to single bit. The operation performed depends on the type of reduction operator.

Operator Description
& Reduction AND
| Reduction OR
^ Reduction XOR
~& Reduction NAND
~| Reduction NOR
~^ Reduction XNOR

The following sample code and outputs demonstrates the operators.
module reduction_operators ;
    initial begin 
        $display(" &3'b101 = %b",&3'b101) ;
        $display(" |3'b101 = %b",|3'b101) ;
        $display(" ^3'b101 = %b",^3'b101) ;
        $finish ;
    end
endmodule 
Download
$ &3'b101 = 0
$ |3'b101 = 1
$ ^3'b101 = 0
The reduction & operator performs ANDing of all bits of the operand. The last three operators in the table performs negation of the result produced by the reduction operator.

Negation operators in Verilog

They are unary operators and are of two types, bitwise negation and logical negation.
Bitwise negation gives the complement of number as each bit is complemented.
I have mentioned in my earlier post that logical operators consider all non zero operands as logical one and others as zero. This single bit result is then complemented with a logical negation operator.

Operator Description
~ Bitwise negation
! Logical negation

The following sample code and outputs demonstrates the operators.
module negation_operator ;
    initial begin 
        $display(" ~3'b101 = %b",~3'b101) ;
        $display(" !1'b0   = %b",!1'b0) ;
        $display(" !2'b10  = %b",!2'b10) ;
        $finish ;
    end
endmodule 
Download
$ ~3'b101 = 013
$ !1'b0 = 1
$ !2'b10 = 0
Its posiible to say that the Bitwise negation gives the 1's complement of a number. It toggles every bit of the operand.

Logical operators in Verilog

This post is about Logical operators in Verilog. They are used to evaluate true or false condition in statement. The result of these statement will be either 1 or 0.

Operator Description
&& Logical AND
|| Logical OR

The following sample code and outputs demonstrates the operators.
module logical_operators ;
    initial begin 
        $display("2'b00 && 2'b11 = %b",2'b00 && 2'b11) ;
        $display("2'b11 && 2'b11 = %b",2'b11 && 2'b11) ;
        $display("2'b00 || 2'b11 = %b",2'b00 || 2'b11) ;
        $display("2'b11 || 2'b11 = %b",2'b11 || 2'b11) ;
        $finish ;
    end
endmodule 
Download
$ 2'b00 && 2'b11 = 0
$ 2'b11 && 2'b11 = 1
$ 2'b00 || 2'b11 = 1
$ 2'b11 || 2'b11 = 1
It is clear from the example that the operator considers the multibit non zero values as logical 1 and others as zero. Then it just performs logical AND or OR operation on the operands.

Bitwise operators and Shift operators in Verilog

This post is about Verilog Bitwise operators. They include both logical operators and shift operators..

Operator Description
| OR
& AND
^ Exclusive OR
<< Shift left
>> Shift right

The following sample code and outputs demonstrates the operators.
module bitwise_operators ;
    initial begin 
        $display(" 2'b11|2'b10  = %b",2'b11|2'b10) ;
        $display(" 2'b11&2'b10  = %b",2'b11&2'b10) ;
        $display(" 2'b11^2'b10  = %b",2'b11^2'b10) ;
        $display(" 3'b101<<1    = %b",3'b101<<1) ;
        $display(" 3'b101>>1    = %b",3'b101>>1) ;
        $finish ;
    end
endmodule 
Download
$ 2'b11|2'b10 = 11
$ 2'b11&2'b10 = 10
$ 2'b11^2'b10 = 01
$ 3'b101<<1 = 010
$ 3'b101>>1 = 010
Bitwise operators work on two operands of same size(bit width) and the result is of same size. Here logical operations are performed on the corresponding bits of each operands. The syntax of left shift operator is given below
  number << n
The number is shifted left n times and the new positions are filled with zeros.

Arithmetic operators in Verilog

This post is about Verilog Arithmetic operators.

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
** Power

The following sample code and outputs demonstrates the arithmetic operators.
module arithmetic ;
reg [1:0] a ;
reg [1:0] b ;
reg [1:0] result ;

    initial begin 
        a = 2'd3 ;
        b = 2'd2 ; 
        result = a + b ;
        $display("a + b  = %b",result) ;
        result = b - a ;
        $display("b - a  = %b",result) ;
        $display("2 * 3  = %5b",2*3) ;
        $display("3 / 2  = %5b",3/2) ;
        $display("3 %s 2 = %5b","%",3%2) ;
        $display("3 ** 2 = %5d",3**2) ;
        $finish ;
    end
endmodule 
Download
$ a + b = 01
$ b - a = 11
$ 2 * 3 = 00110
$ 3 / 2 = 00001
$ 3 % 2 = 00001
$ 3 **2 = 9
In the example of addition operator two numbers are added and the output is assigned to a 2 bit register. But verilog is not capable of handling overflow itself and the result is 01 instead of 101.
The tricky part in subraction is that the negetive number is represented in 2's complement format. That why -1 appears as 11.
Division gives the results as integers, ie, no fractional part is present.
Modulus operator gives the remainder of Division.
And the last one is the raise to operator.