Professional Documents
Culture Documents
always @(sel)
begin
if (sel[0]) code = "000";
else if (sel[1]) code = "001";
else if (sel[2]) code = "010";
else if (sel[3]) code = "011";
else if (sel[4]) code = "100";
else if (sel[5]) code = "101";
else if (sel[6]) code = "110";
else if (sel[7]) code = "111";
else code = 3'bxxx;
end
endmodule
MUX
input[1:0] select;
input[3:0] d;
output q;
reg q;
wire[1:0] select;
wire[3:0] d;
always @( select or d )
begin
if( select == 0)
q = d[0];
if( select == 1)
q = d[1];
if( select == 2)
q = d[2];
if( select == 3)
q = d[3];
end
endmodule
Decoder
verilog code for 4 bit comparator is:
module comp(a,b,y);
input [3:0]a;
input [3:0]b;
output [2:0]y;
reg [2:0]y;
always @(a or b)
begin
if(a=b)
y2='1';
y1='0';
y0='0';
else if(a<b)
y2<='0';
y1<='1';
y0<='1';
else
y2<='0';
Y1<='0';
Y0<='1';
end if;
end if;
end module
Nets
A net signifies a connection from one circuit unit to another. Such a net carries the
value of the signal it is connected to and transmits to the circuit blocks connected
to it. If the driving end of a net is left floating, the net goes to the high impedance
state. A net can be specified in different ways.
wire: It represents a simple wire doing an interconnection. Only one output is
connected to a wire and is driven by that.
tri: It represents a simple signal line as a wire. Unlike the wire, a tri can be
driven by more than one signal outputs.
Functionally, wire and tri are identical. Distinct nomenclatures are provided
for the convenience of assigning roles. Other types of nets are discussed in
Chapter 5.
SCALARS AND VECTORS 41
3.10.2 Variable Data Type
A variable is an abstraction for a storage device. It can be declared through the
keyword reg and stores the value of a logic level: 0, 1, x, or z. A net or wire
connected to a reg takes on the value stored in the reg and can be used as input
to other circuit elements. But the output of a circuit cannot be connected to a reg.
The value stored in a reg is changed through a fresh assignment in the program.
time, integer, real, and realtime are the other variable types of data;
these are dealt with later.