You are on page 1of 3

BEHAVIORAL LEVEL

module alu(alu,carry,zero,ctl,a,b,cin);
input [3:0] a,b;
input cin;
output [3:0] alu;
output carry;
output zero;
input [3:0] ctl;
wire [4:0] result;
assign result = alu_out(a,b,cin,ctl);
assign alu = result[3:0];
assign carry = result[4] ;
assign zero = z_flag(result) ;
function [4:0] alu_out;
input [3:0] a,b ;
input cin ;
input [3:0] ctl ;
case ( ctl )
4'b0000: alu_out=b;
4'b0001: alu_out=b+4'b0001;
4'b0010: alu_out=b-4'b0001;
4'b0011: alu_out=a+b;
4'b0100: alu_out=a+b+cin;
4'b0101: alu_out=a-b;
4'b0110: alu_out=a-b+(~cin);
4'b0111: alu_out=a&b;
4'b1000: alu_out=a|b;
4'b1001: alu_out=a^b;
4'b1010: alu_out={b[3:0],1'b0};
4'b1011: alu_out={b[0],1'b0,b[3:1]};
4'b1100: alu_out={b[3:0],cin};
4'b1101: alu_out={b[0],cin,b[3:1]};
default: begin
alu_out=9'bxxxxxxxxx;
$display("Illegal CONTROL detected!!");
end
endcase
endfunction

function z_flag ;
input [4:0] a4 ;
begin
z_flag = ~(a4[0]|a4[1]|a4[2]|a4[3]) ;
end
endfunction
endmodule

TEST BENCH
module stimulus;
reg [3:0]a;
reg cin;
reg [3:0]b;
reg [3:0] ctl;
wire [3:0]alu;
wire carry;
wire zero;
alu a1(alu,carry,zero,ctl,a,b,cin);
initial begin
a[0]=0;a[1]=0;a[2]=1;a[3]=1;b[0]=0;b[1]=1;b[2]=0;b[3]=1;
ctl[0]=1;ctl[1]=1;ctl[2]=0;ctl[3]=0;cin=0;
#5 a[0]=0;a[1]=0;a[2]=1;a[3]=1;b[0]=0;b[1]=1;b[2]=0;b[3]=1;
ctl[0]=1;ctl[1]=1;ctl[2]=1;ctl[3]=0;cin=0;
#5 $finish;end
initial begin $monitor($time,"alu=%b carry=%b zero=
%b",alu,carry,zero);end
endmodule

You might also like