2023-02-13 16:49:17 +00:00
|
|
|
/* alu.v - implementation of the ALU of the 9086 CPU
|
|
|
|
|
|
|
|
This file is part of the 9086 project.
|
|
|
|
|
|
|
|
Copyright (c) 2023 Efthymios Kritikos
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
2023-02-11 14:43:53 +00:00
|
|
|
`include "alu_header.v"
|
2023-02-09 20:16:50 +00:00
|
|
|
|
2023-02-19 00:20:53 +00:00
|
|
|
module ALU(input [15:0]A,input [15:0]B,output reg [15:0]OUT,input [`ALU_OP_BITS-1:0]op,output wire [7:0]FLAGS,input Wbit);
|
2023-02-11 14:43:53 +00:00
|
|
|
reg C_FLAG;
|
2023-02-14 14:48:35 +00:00
|
|
|
wire signed [15:0]SIGNED_B;
|
2023-02-19 00:20:53 +00:00
|
|
|
wire signed [7:0]SIGNED_8B;
|
2023-02-14 14:48:35 +00:00
|
|
|
assign SIGNED_B=B;
|
2023-02-19 00:20:53 +00:00
|
|
|
assign SIGNED_8B=B[7:0];
|
2023-02-11 14:43:53 +00:00
|
|
|
|
2023-03-04 06:22:28 +00:00
|
|
|
assign FLAGS={(Wbit==1)?OUT[15:15]:OUT[7:7],(Wbit==1) ? (OUT[15:0]=='h0000) : (OUT[7:0]=='h00),5'b0,C_FLAG};
|
2023-02-12 01:05:39 +00:00
|
|
|
|
2023-02-11 14:43:53 +00:00
|
|
|
always @ ( * ) begin
|
2023-02-12 01:28:37 +00:00
|
|
|
if(Wbit==1)begin
|
|
|
|
case (op)
|
|
|
|
`ALU_OP_ADD: {C_FLAG,OUT}=A+B;
|
2023-02-14 14:48:35 +00:00
|
|
|
`ALU_OP_ADD_SIGNED_B: {C_FLAG,OUT}=A+SIGNED_B;
|
2023-02-12 01:28:37 +00:00
|
|
|
`ALU_OP_SUB: {C_FLAG,OUT}=A-B;
|
2023-02-24 02:18:48 +00:00
|
|
|
`ALU_OP_SUB_REVERSE: {C_FLAG,OUT}=B-A;
|
2023-03-04 06:22:28 +00:00
|
|
|
`ALU_OP_AND: begin C_FLAG=0;OUT=A&B; end
|
|
|
|
`ALU_OP_OR: begin C_FLAG=0;OUT=A|B; end
|
|
|
|
`ALU_OP_XOR: begin C_FLAG=0;OUT=A^B; end
|
2023-03-08 07:26:28 +00:00
|
|
|
`ALU_OP_SHIFT_LEFT: begin C_FLAG=(A&16'h8000)==16'h8000;OUT=A<<B; end
|
2023-02-12 01:28:37 +00:00
|
|
|
endcase
|
|
|
|
end else begin
|
|
|
|
case (op)
|
|
|
|
`ALU_OP_ADD: {C_FLAG,OUT[7:0]}=A[7:0]+B[7:0];
|
2023-02-19 00:20:53 +00:00
|
|
|
`ALU_OP_ADD_SIGNED_B: {C_FLAG,OUT[7:0]}=A[7:0]+SIGNED_8B;
|
2023-02-12 01:28:37 +00:00
|
|
|
`ALU_OP_SUB: {C_FLAG,OUT[7:0]}=A[7:0]-B[7:0];
|
2023-02-24 02:18:48 +00:00
|
|
|
`ALU_OP_SUB_REVERSE: {C_FLAG,OUT[7:0]}=B[7:0]-A[7:0];
|
2023-03-04 06:22:28 +00:00
|
|
|
`ALU_OP_AND: begin C_FLAG=0;OUT=A&B; end
|
|
|
|
`ALU_OP_OR: begin C_FLAG=0;OUT=A|B; end
|
|
|
|
`ALU_OP_XOR: begin C_FLAG=0;OUT=A^B; end
|
2023-03-08 07:26:28 +00:00
|
|
|
`ALU_OP_SHIFT_LEFT: begin C_FLAG=(A&16'h80)==16'h80;OUT=A<<B; end
|
2023-02-12 01:28:37 +00:00
|
|
|
endcase
|
|
|
|
end
|
2023-02-11 14:43:53 +00:00
|
|
|
end
|
2023-02-09 20:16:50 +00:00
|
|
|
|
|
|
|
endmodule
|