Encrypted Alu Code
Encrypted Alu Code
//port declaration
input Cin,K0,K1 ;
input [3:0]Sel ;
wire altb,aeqb,agtb ;
wire [3:0]la,lr,ln,lxr,lxn,lnd,lnr;
comparator ins4(.a(A),.b(B),.k0(K0),.k1(K1),.altb(altb),.aeqb(aeqb),.agtb(agtb));
always @*
begin
case(Sel)
4'd5 : ALU_out = la ;
4'd6 : ALU_out = lr ;
4'd7 : ALU_out = ln ;
endcase
end
endmodule
//half adder
module half_adder(a,b,sum,carry) ;
//port declaration
input a,b;
output sum,carry ;
//functional declaration
assign sum = a ^ b ;
endmodule
//full adder
//port declaration
output sum,carry ;
wire c1,c2,s1,w1,w2 ;
//functional declaration
//instanciation
half_adder ha1(.a(a),.b(b),.sum(s1),.carry(c1)) ;
//kg0
assign w1 = c1 ^ k0 ;//xor
//kg1
assign w2 = ! ( s1 ^ k1 ) ;//xnor
half_adder ha2(.a(w2),.b(cin),.sum(sum),.carry(c2)) ;
endmodule
//adder
//port declaration
input [3:0]a,b ;
output carry ;
wire c1,c2,c3 ;
//functional declaration
//instanciation
endmodule
//subtractor
module subtractor(a,b,k0,k1,diff) ;
//port declaration
input [3:0]a,b ;
input k0,k1;
//output carry ;
wire c1,c2,c3,c4 ;
wire [3:0]b1 ;
//functional declaration
//2's complement of b ;
assign b1 = ~b + 1 ;
//instanciation
endmodule
input [1:0]a ,b ;
output [3:0]p ;
half_adder ha1(.a(a[1]&&b[0]),.b(a[0]&&b[1]),.sum(p[1]),.carry(hc1)) ;
half_adder ha2(.a(a[1]&&b[1]),.b(hc1),.sum(p[2]),.carry(p[3])) ;
endmodule
//multiplier
input [3:0]a ,b ;
input k0,k1 ;
output [7:0]product ;
wire [1:0]w1,s2 ;
wire ca1,ca2,gc ;
endmodule
//comparator
input [3:0]a,b ;
input k0,k1 ;
always @ *
begin
begin
altb = a < b ;
aeqb = a == b ;
agtb = a > b ;
end
else
begin
altb = 1'bx ;
aeqb = 1'bx ;
agtb = 1'bx ;
end
end
endmodule
//test bench
module en_alu_tb ;
//port declaration
reg Cin,K0,K1 ;
reg [3:0]Sel ;
wire [7:0]ALU_out ;
integer i ;
initial
begin
A = 15 ;
B = 10 ;
Cin = 0 ;
K0 = 0 ;//
K1 = 1 ;
begin
Sel = i ;
#3 ;
end
end
endmodule