các bạn xem cho mình đoạn code và testbench nhé , mình chạy thử trên modelsim nhưng thấy có kết quả đúng kết quả sai , nó sai chỗ bộ tính toán số học
code
CODE TESTBENCH :
CẢM ƠN CÁC BẠN
code
module alu1(a,b,cin,y,sel);
input [3:0]a,b;
input [4:0]sel;
input cin;
output reg[4:0]y;
reg [4:0]y1,y2,TAM;
always @(a,b,cin,sel)
begin
begin
case ({sel[1:0],cin})
000 : y1 = a;
001 : y1 = a + 1;
010 : y1 = b + a;
011 : y1 = a + b + cin;
100 : y1 = a + ~b;
101 : y1 = a - ~b + 1;
110 : y1 = a - 1;
111 : y1 = b;
endcase
end
begin
case (sel[1:0])
00 : y2 = a & b;
01 : y2 = a | b;
10 : y2 = a ^ b;
11 : y2 = !a;
endcase
end
begin
if (sel[2]==1)
TAM = y2;
else
TAM = y1;
end
begin
case(sel[4:3])
00 : y = TAM;
01 : y = TAM<<1;
10 : y = TAM>>1;
11 : y = 0;
endcase
end
end
endmodule
input [3:0]a,b;
input [4:0]sel;
input cin;
output reg[4:0]y;
reg [4:0]y1,y2,TAM;
always @(a,b,cin,sel)
begin
begin
case ({sel[1:0],cin})
000 : y1 = a;
001 : y1 = a + 1;
010 : y1 = b + a;
011 : y1 = a + b + cin;
100 : y1 = a + ~b;
101 : y1 = a - ~b + 1;
110 : y1 = a - 1;
111 : y1 = b;
endcase
end
begin
case (sel[1:0])
00 : y2 = a & b;
01 : y2 = a | b;
10 : y2 = a ^ b;
11 : y2 = !a;
endcase
end
begin
if (sel[2]==1)
TAM = y2;
else
TAM = y1;
end
begin
case(sel[4:3])
00 : y = TAM;
01 : y = TAM<<1;
10 : y = TAM>>1;
11 : y = 0;
endcase
end
end
endmodule
module test();
reg [3:0]a,b;
reg [4:0]sel;
reg cin;
wire [4:0]y;
alu1 uut(a,b,cin,y,sel);
initial
begin
a = 4'b0111;
b = 4'b1010;
end
initial
begin
////////////////////NGO RA y = a;
{sel[1:0],cin} = 3'b111;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a + b;
#10 {sel[1:0],cin} = 3'b010;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = b ;
#10 {sel[1:0],cin} = 3'b111;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a + 1 ;
#10 {sel[1:0],cin} = 3'b001;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a | b ;
#10 sel[1:0]=2'b01;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
/////////////////// y = a & b;
#10 sel[1:0]=2'b00;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
/////////////////// y = ! a ;
#10 sel[1:0]=2'b11;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
// mot so truong hop dung mot so truong hop sai
end
initial
#100$finish;
endmodule
reg [3:0]a,b;
reg [4:0]sel;
reg cin;
wire [4:0]y;
alu1 uut(a,b,cin,y,sel);
initial
begin
a = 4'b0111;
b = 4'b1010;
end
initial
begin
////////////////////NGO RA y = a;
{sel[1:0],cin} = 3'b111;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a + b;
#10 {sel[1:0],cin} = 3'b010;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = b ;
#10 {sel[1:0],cin} = 3'b111;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a + 1 ;
#10 {sel[1:0],cin} = 3'b001;
sel[2] = 1'b0;
sel[4:3]= 2'b00;
/////////////////// y = a | b ;
#10 sel[1:0]=2'b01;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
/////////////////// y = a & b;
#10 sel[1:0]=2'b00;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
/////////////////// y = ! a ;
#10 sel[1:0]=2'b11;
cin = 1'bz;
sel[2] =1'b1;
sel[4:3] = 2'b00;
// mot so truong hop dung mot so truong hop sai
end
initial
#100$finish;
endmodule
Comment