Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
Here is the code
DONT FORGET TO PRES THE HEPLED ME BUTTON
module alu(CLK,RESET,A,B,Y,OP,C,N,V,Z);
//****************************************************
// 8 bit arithmetic logic unit
//
// parameter:
// CLK.......system clock
// RESET.....System Reset
// A.........A input
// B.........B input
// OP........operation to perform
// Y.........8 bit result output
// C.........carry status
// V.........overflow status
// N.........sign status
// Z.........zero status
//
//****************************************************
input CLK,RESET;
input [7:0] A;
input [7:0] B;
input [15:0] OP;
output [7:0] Y;
output C;
output V;
output N;
output Z;
//------------------------------------------------------
// internal nodes
//------------------------------------------------------
reg [7:0] Y,LogicUnit,AluNoShift;
wire [7:0] ArithUnit;
reg shiftout;
wire C,V,N,Z;
wire carryout;
wire ovf;
reg Neg; //negative status from ALU
reg Zero; //Zero Status from ALU
//-----------------------------------------------------------
// Arithmetic unit
//
// gona use myAddSub for this part...it seems to work right...
// rather than trying to infer the thing.
//
// operation of the adder subtractor is as follows
//
// OP[0] OP[1] carry | operation
// 0 0 0 | Y = A - 1;
// 0 0 1 | Y = A;
// 0 1 0 | Y = A - B - 1;
// 0 1 1 | Y = A - B;
// 1 0 0 | Y = A;
// 1 0 1 | Y = A + 1;
// 1 1 0 | Y = A + B;
// 1 1 1 | Y = A + B + 1;
//
//------------------------------------------------------------
//---------------------------------------------
// Logic Unit
// OP[1] OP[0] | operation
// 0 0 | Y = A and B
// 0 1 | Y = A or B
// 1 0 | Y = A xor B
// 1 1 | Y = Not A
//---------------------------------------------
always @ (A or B or OP[1:0])
begin
case (OP[1:0])
2'b00:LogicUnit = A & B;
2'b01:LogicUnit = A | B;
2'b10:LogicUnit = A ^ B;
2'b11:LogicUnit = !A;
default:LogicUnit = 8'bx;
endcase
end
//----------------------------------------------
// Select between logic and arithmatic
// OP[2] | operation
// 0 | Arithmetic operation
// 1 | Logical Operation
//----------------------------------------------
always @ (OP[2] or LogicUnit or ArithUnit)
begin
if(OP[2])
AluNoShift = LogicUnit;
else
AluNoShift = ArithUnit[7:0];
end
//-----------------------------------------------
// Shift operations
//
// OP[3] OP[4] OP[5] | operation
// 0 0 0 | NOP
// 1 0 0 | Shift Left (ASL)
// 0 1 0 | Arithmentic Shift Right (ASR..new)
// 1 1 0 | Logical Shift Right (LSR)
// 0 0 1 | Rotate Left (ROL)
// 1 0 1 | Rotate Right (ROR)
// 0 1 1 | NOP
// 1 1 1 | NOP
//-----------------------------------------------
always @ (OP[5:3] or AluNoShift or C)
begin
case(OP[5:3])
3'b000: begin
Y = AluNoShift; //do not shift output
shiftout = 0;
end
3'b001: begin
Y = {AluNoShift[6:0],1'b0}; //ASL
shiftout = AluNoShift[7];
end
3'b010: begin
Y = {AluNoShift[7],AluNoShift[7:1]}; //ASR
shiftout = AluNoShift[0];
end
3'b011: begin
Y = {1'b0,AluNoShift[7:1]}; //LSR
shiftout = AluNoShift[0];
end
3'b100: begin
Y = {AluNoShift[6:0],C};
shiftout = AluNoShift[7];
end
3'b101: begin
Y = {C,AluNoShift[7:1]}; //LSR
shiftout = AluNoShift[0];
end
3'b110: begin
Y = AluNoShift; //do not shift output
shiftout = 0;
end
3'b111: begin
Y = AluNoShift; //do not shift output
shiftout = 0;
end
default: begin
Y = 8'bx;
shiftout = 0;
end
endcase
end
//-----------------------------------------------------------
// Generate the status bits for the status registers
//
//-----------------------------------------------------------
always @(Y)
begin
if(!Y[0] & !Y[1] & !Y[2] & !Y[3] & !Y[4] & !Y[5] & !Y[6] & !Y[7])
Zero = 1;
else
Zero = 0;
end
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.