



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




module t_leading_zero_detector;
reg [8:0] A; wire [7:0] Astim; wire [2:0] count;
assign Astim=A[7:0];
leading_zero_detector DUT(.count(count),.A(Astim));
initial $monitor("%t A:%b count:%d",$time,Astim,count);
initial begin
for(A=0;A<=8'hff;A=A+1) begin #10; end
#10 $stop; end
endmodule
`default_nettype none
module ALU (output wire [3:0] Y, output wire cout, overflow, zero, input wire [3:0] A, B, input wire cin, input wire [1:0] op);
wire [4:0] negB; wire [4:0] negA;
assign negB = -({B[3],B}); assign negA = -({A[3],A});
assign zero = (Y==4'd0);
assign {cout,Y} = (op==2'b00)? A+B+cin : (op==2'b01)? A+negB[3:0] : (op==2'b10)? A^B: (op==2'b11)? negA[3:0] : 5'bx;
assign overflow = (op==2'b00)? ((cout+A[3]+B[3])!=Y[3]) : (op==2'b01)? ((cout+A[3]+negB[4])!=Y[3]) : (op==2'b10)? 1'bx : (op==2'b11)? (negA[4]!=Y[3]) : 1'bx;
endmodule
module t_ALU; reg signed [3:0] A; reg signed [3:0] B; reg cin; reg [1:0] op;
wire signed [3:0] Y; wire cout, overflow, zero;
ALU DUT(.Y(Y), .cout(cout), .overflow(overflow), .zero(zero),.A(A), .B(B), .cin(cin), .op(op));
initial $monitor("%t, Y:%d, cout:%b, overflow:%b, zero:%b, A:%d,B:%d, cin:%b, op:%b",$time,Y,cout,overflow,zero,A,B,cin,op);
initial begin #10 A=4'd0; B=4'd0; cin=1'b0; op=2'b00; #10 A=4'd2; B=4'd3; cin=1'b0; op=2'b00; #10 A=4'd4; B=4'd2; cin=1'b1; op=2'b00; #10 A=-4'd3; B=4'd5; cin=1'b0; op=2'b00; #10 A=-4'd7; B=4'd8; cin=1'b1; op=2'b00; #10 A=4'd4; B=4'd5; cin=1'b0; op=2'b00; #10 A=-4'd8; B=-4'd8; cin=1'b0; op=2'b00; #10 A=4'd0; B=-4'd8; cin=1'bx; op=2'b01; #10 A=4'd5; B=4'd4; cin=1'bx; op=2'b01; #10 A=-4'd8; B=-4'd8; cin=1'bx; op=2'b01; #10 A=4'd0; B=4'd0; cin=1'bx; op=2'b01; #10 A=-4'd8; B=4'd5; cin=1'bx; op=2'b01; #10 A=4'b1101; B=4'b0110; cin=1'bx; op=2'b10;