// Original Coco 3 Plus contributions: Copyright (c) 2026 Brian K. Holman. // Original component notices/terms retained; see COPYRIGHT.md and LICENSING.md. // SPDX-License-Identifier: BSD-3-Clause // Two-stage parallel decimal adjust, VCCPlus/MAME TMS7000; see LICENSE. // No operation-select mux precedes the nibble arithmetic. Both results are // available two rising edges after stable operands, including invalid BCD. module ssc_tms7000_bcd(input wire clk,reset,input wire [7:0] destination,source, input wire carry,output reg [7:0] add_result,sub_result, output reg add_carry,sub_carry); reg [7:0] add_base,sub_base; reg [2:0] add_adjust,sub_adjust; wire [5:0] high_sum={2'd0,destination[7:4]}+{2'd0,source[7:4]}; wire [5:0] low_sum={2'd0,destination[3:0]}+{2'd0,source[3:0]}+{5'd0,carry}; wire low_borrow={1'b0,destination[3:0]}<({1'b0,source[3:0]}+{4'd0,!carry}); function [7:0] correction(input [2:0] index); begin case(index) 1:correction=8'h06;3,5:correction=8'h66;4:correction=8'h60;default:correction=0;endcase end endfunction always @(posedge clk)begin if(reset)begin add_base<=0;sub_base<=0;add_adjust<=0;sub_adjust<=0; add_result<=0;sub_result<=0;add_carry<=0;sub_carry<=0; end else begin add_base<=destination+source+{7'd0,carry}; sub_base<=destination-source-{7'd0,!carry}; add_adjust<={(high_sum>9),(high_sum==9),(low_sum>=10)}; sub_adjust<={(destination[7:4]2);sub_carry<=(sub_adjust<=2); end end endmodule