I am trying to perform subtraction when ALX is 1 and ALY is 0 using instantaneous carry look ahead adder but it does not work properly. It works fine for addition.
- if ALX is 0 and ALY is also 0 it will load input to output
- if ALX is 0 and ALY is 1 it will add input to output
- if ALX is 1 and ALY is 0 it will sub input to output
- if ALX is 1 and ALY is also 1 it will and input to output
Here is some of my alu module
module alu (CLK, RST, ALE, ALX , ALY, iDATA, oDATA , flags,aDATA);
input wire CLK, ALE, ALX, ALY,RST;
input wire [3:0] iDATA;
output reg [3:0] oDATA;
output reg [3:0] flags;
output reg [2:0] aDATA;
//declare variables
reg [3:0] nextflags;
wire [3:0] ALU;
wire [3:0] SUM;
wire CIN;
wire claCF,claZF,claNF,claVF;
cla4 test(iDATA,oDATA,CIN,SUM,claCF,claZF,claNF,claVF);
assign CIN = ALX;
assign ALU = ALX ? (ALY ? iDATA & oDATA : SUM) : (ALY ? SUM : iDATA ); //loop
always @ (posedge CLK, posedge RST) begin
if (RST) begin
oDATA <= 0;
flags <= 0;
end
else if(ALE) begin
oDATA <= ALU;
flags <= nextflags;
end
else begin
oDATA <= oDATA;
flags <= flags;
end
always @ (*) begin
aDATA[2:0] = {ALE,ALX,~CIN};
nextflags[3] = ALX ? ( ALY ? flags[3]: claCF ) : (ALY ? claCF : 0 );
nextflags[2] = (ALU==0);
nextflags[1] = ALU[3];
nextflags[0] = ALX ? ( ALY ? flags[0] : claVF ) : (ALY ? claVF : 0 );
end
endmodule
Here is my output
time: 0 OUTPUT:0000 ADATA:001 C:0 Z:0 N:0 V:0 time: 1 OUTPUT:0000 ADATA:110 C:0 Z:0 N:0 V:0 time: 2 OUTPUT:0001 ADATA:110 C:1 Z:0 N:0 V:0 time: 4 OUTPUT:0000 ADATA:110 C:1 Z:1 N:0 V:0 time: 6 OUTPUT:0001 ADATA:110 C:1 Z:0 N:0 V:0 time: 8 OUTPUT:0000 ADATA:110 C:1 Z:1 N:0 V:0 time: 10 OUTPUT:0001 ADATA:110 C:1 Z:0 N:0 V:0 time: 12 OUTPUT:0000 ADATA:110 C:1 Z:1 N:0 V:0 time: 14 OUTPUT:0001 ADATA:110 C:1 Z:0 N:0 V:0 time: 16 OUTPUT:0000 ADATA:110 C:1 Z:1 N:0 V:0 time: 18 OUTPUT:0001 ADATA:110 C:1 Z:0 N:0 V:0 time: 20 OUTPUT:0000 ADATA:110 C:1 Z:1 N:0 V:0