Académique Documents
Professionnel Documents
Culture Documents
`
Ex1: 2to4 decoder
output[3:0]O;
input[1:0]I;
endmodule
output[3:0]O;
input[1:0]I;
endmodule
output [3:0]O;
input [1:0]I;
reg [3:0]O;
always@(I)
begin
O[0] = (!I(1)) & (!I(0));
O[1] = (!I(1)) & (I(0));
O[2] = (I(1)) & (!I(0));
O[3] = I(1) & I(0);
end
endmodule
output[3:0]O;
input[1:0]I;
reg[3:0]O;
always@(I)
begin
case(I)
2'b00: O = 4'h0;
2'b01: O = 4'h1;
2'b10: O = 4'h2;
2'b11: O = 4'h4;
default: O = 4'h0;
endcase
end
endmodule
output [3:0]O;
input [1:0]I;
reg [3:0]O;
always@(I)
begin
if(I == 2'b00) O = 4’b0001;
else if(I==2'b01) O = 4’b0010;
else if(I==2'b10) O = 4’b0100;
else if(I==2'b11) O = 4’b1000;
else O = 4’b0000;
end
endmodule
wire [3:0]O;
reg [1:0]I;
initial
begin
I = 2’b00;
#5 I = 2’b00;
#5 I = 2’b00;
#5 I = 2’b00;
#10 $stop;
end
endmodule
Ex2: 8 to 3 encoder (with and without priority)
Priority Encoder
Behavioural model using case statement
input [7:0]I;
output [7:0]Y;
reg [7:0]Y;
always@( I)
casex(I)
7’b1???????: Y = 3’b111;
7’b?1??????: Y = 3’b110;
7’b??1?????: Y = 3’b101;
7’b???1????: Y = 3’b100;
7’b????1???: Y = 3’b011;
7’b?????1??: Y = 3’b010;
7’b??????1?: Y = 3’b001;
7’b???????1: Y = 3’b000;
default : Y = 3’b000;
endcase
endmodule
input [7:0]I;
output [7:0]Y;
reg [7:0]Y;
always@( I)
if(I[7]) Y = 3’b111;
else begin
Y = 3’b000;
$display(“error”);
end
endmodule
testbench
module tb_priorityenc;
reg [7:0]I;
wire [7:0]Y;
intial
begin
I = 8’b00000000
#100 $stop;
end
always #3 I = I + 1;
endmodule
Encoder (Without Priority)
input [7:0]I;
output [7:0]Y;
reg [7:0]Y;
always@( I)
case(I)
7’b10000000: Y = 3’b111;
7’b01000000: Y = 3’b110;
7’b00100000: Y = 3’b101;
7’b00010000: Y = 3’b100;
7’b00001000: Y = 3’b011;
7’b00000100: Y = 3’b010;
7’b00000010: Y = 3’b001;
7’b00000001: Y = 3’b000;
default : Y = 3’b000;
endcase
endmodule
input [7:0]I;
output [7:0]Y;
reg [7:0]Y;
always@( I)
else begin
Y = 3’b000;
$display(“error”);
end
endmodule
testbench
module tb_ encoder;
reg [7:0]I;
wire [7:0]Y;
intial
begin
I = 8’b00000000
#100 $stop;
end
always #3 I = I + 1;
endmodule
EX3: 8x1 MUX and 1X8 DMUX
input [7:0]I;
input [2:0]S;
output Y;
assign Y = (I[0] & ((!S[2]) & (!S[1]) & (!S[0]))) | (I[1] & ((!S[2]) & (!S[1]) & (S[0]))) |
(I[2] & ((!S[2]) & (S[1]) & (!S[0]))) | (I[3] & ((!S[2]) & (S[1]) & (S[0]))) |
(I[4] & ((S[2]) & (!S[1]) & (!S[0]))) | (I[5] & ((S[2]) & (!S[1]) & (S[0]))) |
(I[6] & ((S[2]) & (S[1]) & (!S[0]))) | (I[7] & ((S[2]) & (S[1]) & (S[0])));
endmodule
not g1(NS2,S[2]);
not g2(NS1,S[1]);
not g3(NS0,S[0]);
and g4(W[0],I[0],NS2,NS1,NS0);
and g5(W[1],I[1],NS2,NS1,S[0]);
and g6(W[2],I[2],NS2,S[1],NS0);
and g7(W[3],I[3],NS2,S[1],S[0]);
and g8(W[4],I[4],S[2],NS1,NS0);
and g9(W[5],I[5],S[2],NS1,S[0]);
and g10(W[6],I[6],S[2],S[1],NS0);
and g11(W[7],I[7],S[2],S[1],S[0]);
endmodule
always@(I,S)
Y = (I[0] & ((!S[2]) & (!S[1]) & (!S[0]))) | (I[1] & ((!S[2]) & (!S[1]) & (S[0]))) |
(I[2] & ((!S[2]) & (S[1]) & (!S[0]))) | (I[3] & ((!S[2]) & (S[1]) & (S[0]))) |
(I[4] & ((S[2]) & (!S[1]) & (!S[0]))) | (I[5] & ((S[2]) & (!S[1]) & (S[0]))) |
(I[6] & ((S[2]) & (S[1]) & (!S[0]))) | (I[7] & ((S[2]) & (S[1]) & (S[0])));
endmodule
always@(I,S)
if ( S == 3’b000) Y = I[0];
else if (S == 3’b001) Y = I[1];
else if (S == 3’b010) Y = I[2];
else if (S == 3’b011) Y = I[3];
else if (S == 3’b100) Y = I[4];
else if (S == 3’b101) Y = I[5];
else if (S == 3’b110) Y = I[6];
else if (S == 3’b111) Y = I[7];
else Y = 1’bz;
endmodule
always@(I,S)
case(S)
3’b000: Y = I[0];
3’b001: Y = I[1];
3’b010: Y = I[2];
3’b011: Y = I[3];
3’b100: Y = I[4];
3’b101: Y = I[5];
3’b110: Y = I[6];
3’b111: Y = I[7];
default : Y = 1’bz;
endcase
endmodule
1:8 DeMultiplexer – Data Flow Model
input I;
input [2:0]S;
output [7:0]Y;
endmodule
not g1(NS2,S[2]);
not g2(NS1,S[1]);
not g3(NS0,S[0]);
and g4(Y[0],I,NS2,NS1,NS0);
and g5(Y[1],I,NS2,NS1,S[0]);
and g6(Y[2],I,NS2,S[1],NS0);
and g7(Y[3],I,NS2,S[1],S[0]);
and g8(Y[4],I,S[2],NS1,NS0);
and g9(Y[5],I,S[2],NS1,S[0]);
and g10(Y[6],I,S[2],S[1],NS0);
and g11(Y[7],I,S[2],S[1],S[0]);
endmodule
always@(I,S)
begin
Y[0] = (I[0] & ((!S[2]) & (!S[1]) & (!S[0])));
Y[1] = (I[1] & ((!S[2]) & (!S[1]) & (S[0])));
Y[2] = (I[2] & ((!S[2]) & (S[1]) & (!S[0]))) ;
Y[3] = (I[3] & ((!S[2]) & (S[1]) & (S[0])));
Y[4] = (I[4] & ((S[2]) & (!S[1]) & (!S[0])));
Y[5] = (I[5] & ((S[2]) & (!S[1]) & (S[0])));
Y[6] = (I[6] & ((S[2]) & (S[1]) & (!S[0])));
Y[7] = (I[7] & ((S[2]) & (S[1]) & (S[0])));
end
endmodule
if ( S == 3’b000) Y[0] = I;
else if (S == 3’b001) Y[1] = I;
else if (S == 3’b010) Y[2] = I;
else if (S == 3’b011) Y[3] = I;
else if (S == 3’b100) Y[4] = I;
else if (S == 3’b101) Y[5] = I;
else if (S == 3’b110) Y[6] = I;
else if (S == 3’b111) Y[7] = I;
else Y = 8’bzzzzzzzz;
endmodule
always@(I,S)
case(S)
3’b000: Y[0] = I;
3’b001: Y[1] = I;
3’b010: Y[2] = I;
3’b011: Y[3] = I;
3’b100: Y[4] = I;
3’b101: Y[5] = I;
3’b110: Y[6] = I;
3’b111: Y[7] = I;
default : Y = 8’bzzzzzzzz;
endcase
endmodule
Ex 4: 4 bit binary to gray code converter
TEST BENCH
module tb_bin2gray;
reg [3:0]B;
wire [3:0]G;
bin2gray_beh1 DUT(B, G);
intial
begin
B = 4’b0000;
#100 $stop;
end
always #5 B = B + 4’b0001;
endmodule
Ex 5. Design of 4-bit comparator
always @ (a or b)
begin
equal = 0;
lower = 0;
greater = 0;
if (a<b) lower = 1;
else if (a==b) equal = 1;
else greater = 1;
end
endmodule
always @ (a or b)
begin
if(a[3] == b[3]) x3 = 1;
if(a[2] == b[2]) x2 = 1;
if(a[1] == b[1]) x1 = 1;
if(a[0] == b[0]) x0 = 1;
equal = x3 & x2 & x1 & x0;
if ((a[3] > b[3]) |(x3 & (a[2] > b[2])) | (x3 & x2 & (a[1] > b[1])) |
(x3 & x2 & x1 & (a[0] > b[0])) )
greater = 1;
if ((a[3] < b[3]) |(x3 & (a[2] < b[2])) | (x3 & x2 & (a[1] < b[1])) |
(x3 & x2 & x1 & (a[0] < b[0])) )
lower = 1;
endmodule
Test bench for comparator
module tb_comparator ;
endmodule
Ex 6: Full adder using 3 modeling styles
input A, B;
output SUM, COUT;
assign SUM = A ^ B;
assign COUT = A & B;
endmodule
input A, B;
output SUM, COUT;
endmodule
input A, B;
output SUM, COUT;
always@(A, B)
begin
SUM = A ^ B;
COUT = A & B;
end
endmodule
input A, B;
output SUM, COUT;
wire [1:0] X = {A, B};
always@(X)
begin
case(X)
2'b00: begin
SUM = 0;
COUT = 0;
end
2'b01: begin
SUM = 1;
COUT = 0;
end
2'b10: begin
SUM = 1;
COUT = 0;
end
2'b11: begin
SUM = 0;
COUT = 1;
end
default: begin
$display ("error");
SUM = 0;
COUT = 0;
end
endcase
end
endmodule
input A, B;
output SUM, COUT;
wire [1:0] X = {A, B};
always@(X)
begin
if(X==2'b00)
begin SUM = 0; COUT = 0; end
else if(X==2'b01)
begin SUM = 1; COUT = 0; end
else if(s==2'b10)
begin SUM = 1; COUT = 0; end
else
begin SUM = 0; COUT = 1; end
end
end
endmodule
input A, B, CIN;
output SUM, COUT;
endmodule
input A, B, CIN;
output SUM, COUT;
endmodule
input A, B, CIN;
output SUM, COUT;
always@(A, B, CIN)
begin
SUM = A ^ B ^ CIN;
COUT = A & B | B & CIN | A & CIN;
end
endmodule
input A, B, CIN;
output SUM, COUT;
always@(X)
begin
case(X)
3'b000: begin
SUM = 0;
COUT = 0;
end
3'b001: begin
SUM = 1;
COUT = 0;
end
3'b010: begin
SUM = 1;
COUT = 0;
end
3'b011: begin
SUM = 0;
COUT = 1;
end
3'b100: begin
SUM = 1;
COUT = 0;
end
3'b101: begin
SUM = 0;
COUT = 1;
end
3'b110: begin
SUM = 0;
COUT = 1;
end
3'b111: begin
SUM = 1;
COUT = 1;
end
default: begin
$display ("error");
SUM = 0;
COUT = 0;
end
endcase
end
endmodule
input A, B, CIN;
output SUM, COUT;
wire [2:0] X = {A, B, CIN};
always@(X)
begin
if(X==3'b000)
begin SUM = 0; COUT = 0; end
else if(X==3'b001)
begin SUM = 1; COUT = 0; end
else if(s==3'b010)
begin SUM = 1; COUT = 0; end
else if(X==3'b011)
begin SUM = 0; COUT = 1; end
else if(X==3'b100)
begin SUM = 1; COUT = 0; end
else if(X==3'b101)
begin SUM = 0; COUT = 1; end
else if(s==3'b110)
begin SUM = 0; COUT = 1; end
else
begin SUM = 1; COUT = 1; end
end
end
endmodule