4 - Bit ALU Using Verilog
4 - Bit ALU Using Verilog
Code:
ALU (Top) Module:
Logical Module:
Comparator Module:
//This is the Comparator Block.Here we have used the xor output from the logical
block and the subtractor output and the overflow output from the adder
subtractor block to compare A and B.
//Main Module of comparator block
module Comparatorf(Subtract,of,Xor,outp,Op); //Collections of Inputs
and outputs
input [3:0]Subtract,Xor,Op; //Taking 4-bit input of the opcode and one input
from logical block and other from Adder Subtractor Block
input of; //1 Bit input of Overflow from Adder Subtractor Block.
reg AgB; //1 Bit Register for A>B Condition
reg AlB; //1 Bit Register for A<B Condition
reg AeB; //1 Bit Register for A=B Condition
reg AneB; //1 Bit Register for A!=B Condition
reg Nor; //1 Bit register for storing nor value of Xor.
output reg [3:0]outp; //4 - Bit Output.
reg compare; //1 Bit register for storing the xor(ed) value of the MSB of the
Subtractor input and the overflow.
always @(*)
begin
compare = (of^Subtract[3]); //XOR operation of Overflow and MSB of
Subtractor input.
Nor = ~|Xor; //Nor Operation of all the four bits of the Xor
input.
if(Nor == 1'b1) //If the Nor value is 1 then A will be equal to B hence AequalB
is 1 and all other registers are 0.
begin
AgB = 0;
AlB = 0;
AeB = 1;
AneB =0;
end
else //Else AnotequalB will be 1.
begin
AeB =0;
AneB =1;
AgB = 0;
AlB = 0;
end
if(AneB && compare) //Now if A is Not equal to B and compare is 1 then A will
be less then B.
begin
AlB = 1;
AeB =0;
AneB =1;
AgB = 0;
end
else if (AneB && (~compare)) //Now if A is Not equal to B and compare is
0 then A will be greater then B.
begin
AlB = 0;
AeB =0;
AneB =1;
AgB = 1;
end
if(Op == 4'b1001) //Now here depending upon the Opcode we are giving the
outputs.We are assigning the zeroth bit to the required output and all other bits
to 0.
begin
outp[0] = AeB;
outp[3:1]=3'b000;
end
else if(Op == 4'b1011)
begin
outp[0] = AneB;
outp[3:1]=3'b000;
end
else if(Op == 4'b1101)
begin
outp[0] = AgB;
outp[3:1]=3'b000;
end
else if(Op == 4'b1111)
begin
outp[3:1]=3'b000;
outp[0] = AlB;
end
else //Else in every other Opcode the output will
be zero.
outp = 4'b0000;
end
endmodule
Shifter Module:
//There is only One module for shifter Block.Here we are shifting the input B.0th
bit and 1th bit of input A is taken as Sa.
//Here we are shifting 0 bit for Sa=00 , 1 bit for Sa=01 , 2 bit for Sa = 10 and 3 bit
for Sa = 11.
//Main Module.
module Shifterf(Sa,B,Cin,Op,fout); //Collection of inputs and
outputs.
input [1:0]Sa; //Here Sa is of 2 bits for selecting the number of
bits we want to shift.
input [3:0]B; //4-bit input B and Opcode.
input [3:0]Op;
input Cin; //Cin from the adder Subtractor Block.
reg [3:0] X; //A 4 bit register for storing the flipped value of B temporarily.
reg [3:0] out1; //A 4 bit registers for temporarily storing the output.
reg [3:0] outp;
reg [3:0] out;
output [3:0]fout; //4-Bit Shifted Output.
reg temp; //A temporary 1 bit register for selecting the value which we want to
fill in the (Shifted) vacancies.
//Flipping the inputs for right shifting
always @ (*)
//Flipping the inputs if we want to right shift else storing the same value in the
temporary register X.
begin
if(Op[2] == 0) //For Left Shift Op[2] will be 0.
X = B;
else if(Op[2] == 1) //For Right Shift Op[2] will be 1.
begin
X[3] = B[0];
X[2] = B[1];
X[1] = B[2];
X[0] = B[3];
end
//Storing the value to be filled in the shifted vacancies to temp.
if(Op == 4'b0101) //For arithmetic storing the MSB of the input B.
temp = X[0]; //Here for arithmetic we have to fill the MSB of the input B.But
the inputs are flipped for arithmetic right shift and hence we have used X[0]
which is the MSB of the input B.
else
temp = Cin; //else carry will be filled in the shifted vacancies.
//Left Shift
if(Sa == 2'b00) //Shifting the input as per Sa.
out = X;
if(Sa == 2'b01)
begin
out[0] = temp;
out[1] = X[0];
out[2] = X[1];
out[3] = X[2];
end
if(Sa == 2'b10)
begin
out[0] = temp;
out[1] = temp;
out[2] = X[0];
out[3] = X[1];
end
if(Sa == 2'b11)
begin
out[0] = temp;
out[1] = temp;
out[2] = temp;
out[3] = X[0];
end
//Here again if the Opcode is for right shift then we are flipping the left shifted
output to perform the right shifting.
if(Op[2] == 1)
begin
outp[3] = out[0];
outp[2] = out[1];
outp[1] = out[2];
outp[0] = out[3];
end
else if(Op[2] == 0) //If the Opcode is for Left shift then no flipping will take place.
begin
outp = out;
end
end
assign fout = outp; //Assigning the outp to the final Output fout.
Endmodule
MUX Module:
UCF: