Line 1... |
Line 1... |
module BCDSubtract(clk, a, b, o, co);
|
`timescale 1ns / 1ps
|
|
// ============================================================================
|
|
// __
|
|
// \\__/ o\ (C) 2022 Robert Finch, Waterloo
|
|
// \ __ / All rights reserved.
|
|
// \/_// robfinch@finitron.ca
|
|
// ||
|
|
//
|
|
// BCDSubtract.sv
|
|
//
|
|
//
|
|
// BSD 3-Clause License
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
// list of conditions and the following disclaimer.
|
|
//
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
|
// contributors may be used to endorse or promote products derived from
|
|
// this software without specific prior written permission.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
//
|
|
// ============================================================================
|
|
|
|
module BCDSubtract(clk, a, b, o, sgn);
|
parameter N=25;
|
parameter N=25;
|
input clk;
|
input clk;
|
input [N*4-1:0] a;
|
input [N*4-1:0] a;
|
input [N*4-1:0] b;
|
input [N*4-1:0] b;
|
output reg [N*4-1:0] o;
|
output reg [N*4-1:0] o;
|
output reg co;
|
output reg sgn;
|
|
|
wire [(N+1)*4-1:0] bc;
|
wire [(N+1)*4-1:0] bc;
|
wire [(N+1)*4-1:0] o1, o2, o3;
|
wire [(N+1)*4-1:0] o1, o2, o3;
|
wire c;
|
wire c;
|
|
|
BCDNinesComplementN #(N+1) u1 (.i({4'h0,b}), .o(bc));
|
BCDNinesComplementN #(N+1) u1 (.i({4'h0,b}), .o(bc));
|
BCDAdd8NClk #(.N(N/2+1)) u2 (.clk(clk), .a({8'h00,a}), .b(bc), .o(o1), .ci(1'b0), .co(c));
|
BCDAddNClk #(.N(N+1)) u2 (.clk(clk), .a({8'h00,a}), .b(bc), .o(o1), .ci(1'b0), .co(c));
|
BCDNinesComplementN #(N) u3 (.i(o1), .o(o2));
|
BCDNinesComplementN #(N) u3 (.i(o1), .o(o2));
|
BCDAdd8NClk #(.N(N/2+1)) u4 (.clk(clk), .a(o1), .b({{N*8{1'b0}},1'b1}), .o(o3), .ci(1'b0), .co());
|
BCDAddNClk #(.N(N+1)) u4 (.clk(clk), .a(o1), .b('d0), .o(o3), .ci(c), .co());
|
|
|
always_ff @(posedge clk)
|
always_ff @(posedge clk)
|
if (c)
|
if (c)
|
o <= o3;
|
o <= o3;
|
else
|
else
|
o <= o2;
|
o <= o2;
|
always_ff @(posedge clk)
|
always_ff @(posedge clk)
|
co <= c;
|
sgn <= ~c;
|
|
|
endmodule
|
endmodule
|
|
|
module BCDNinesComplement(i, o);
|
module BCDNinesComplement(i, o);
|
input [3:0] i;
|
input [3:0] i;
|