Verilog Assignment1
Verilog Assignment1
binary to gray
endmodule
2.Carry look ahead
module CLA_Adder(a,b,cin,sum,cout);
input[3:0] a,b;
input cin;
output [3:0] sum;
output cout;
wire p0,p1,p2,p3,g0,g1,g2,g3,c1,c2,c3,c4;
assign p0=(a[0]^b[0]),
p1=(a[1]^b[1]),
p2=(a[2]^b[2]),
p3=(a[3]^b[3]);
assign g0=(a[0]&b[0]),
g1=(a[1]&b[1]),
g2=(a[2]&b[2]),
g3=(a[3]&b[3]);
assign c0=cin,
c1=g0|(p0&cin),
l c2=g1|(p1&g0)|(p1&p0&cin),
c3=g2|(p2&g1)|(p2&p1&g0)|(p1&p1&p0&cin),
c4=g3|(p3&g2)|(p3&p2&g1)|(p3&p2&p1&g0)|(p3&p2&p
1&p0&cin);
assign sum[0]=p0^c0,
sum[1]=p1^c1,
sum[2]=p2^c2,
sum[3]=p3^c3;
assign cout=c4;
endmodule
3.Comparator
xnor u1(tmp1,A[1],B[1]);
xnor u2(tmp2,A[0],B[0]);
and u3(A_equal_B,tmp1,tmp2);
module Decoder(a,b,c,d0,d1,d2,d3,d4,d5,d6,d7);
input a,b,c;
output d0,d1,d2,d3,d4,d5,d6,d7;
assign d0=(~a&~b&~c),
d1=(~a&~b&c),
d2=(~a&b&~c),
d3=(~a&b&c),
d4=(a&~b&~c),
d5=(a&~b&c),
d6=(a&b&~c),
d7=(a&b&c);
endmodule
5. encoder
module Encoder(d0,d1,d2,d3,d4,d5,d6,d7,a,b,c);
input d0,d1,d2,d3,d4,d5,d6,d7;
output a,b,c;
or(a,d4,d5,d6,d7);
or(b,d2,d3,d6,d7);
or(c,d1,d3,d5,d7);
endmodule
6. ripplecarry adder
endmodule
gray tobinary_tb
8. mux2
module mux2(a,b,s,y);
input a,b,s;
output y;
assign y=((~s)&a)|(s&b);
endmodule
mux2_tb
module mux2_tb();
reg a,b,s;
wire y;
mux2 dut_any_name
(
.a(a),
.b(b),
.s(s),
.y(y));
initial
begin
a=1'b0;
b=1'b0;
s=1'b0;
#10
a=1'b0;
b=1'b1;
s=1'b1;
#10
$finish;
end
endmodule
9. mux4
module mux4(a,b,c,d,s,y);
input [2:0]a,b,c,d;
input [1:0]s;
output [3:0]y;
assign y= s[1]? (s[0]?d:c): (s[0]?b:a);
endmodule
mux4_tb
module mux4_tb();
reg [2:0] a,b,c,d;
reg [1:0]s;
wire [3:0]y;
mux4 aaa (.a(a),.b(b),.c(c),.d(d),.s(s),.y(y))
initial
begin
a=2’b01;
b=2’b00;
c=2’b01;
d=2’b10;
s=1’b0;
#10
a=2’b10
c=2’b11;
d=2’b00;
s=1’b1;
#10
$finish;
end
endmodule