| 1 |
66 |
robfinch |
`timescale 1ns / 1ps
|
| 2 |
|
|
// ============================================================================
|
| 3 |
|
|
// __
|
| 4 |
|
|
// \\__/ o\ (C) 2012-2022 Robert Finch, Waterloo
|
| 5 |
|
|
// \ __ / All rights reserved.
|
| 6 |
|
|
// \/_// robfinch@finitron.ca
|
| 7 |
|
|
// ||
|
| 8 |
|
|
//
|
| 9 |
|
|
// BCDSub8NClk.sv
|
| 10 |
|
|
//
|
| 11 |
|
|
// BSD 3-Clause License
|
| 12 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 13 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 14 |
|
|
//
|
| 15 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
| 16 |
|
|
// list of conditions and the following disclaimer.
|
| 17 |
|
|
//
|
| 18 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 19 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 20 |
|
|
// and/or other materials provided with the distribution.
|
| 21 |
|
|
//
|
| 22 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
| 23 |
|
|
// contributors may be used to endorse or promote products derived from
|
| 24 |
|
|
// this software without specific prior written permission.
|
| 25 |
|
|
//
|
| 26 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 27 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 28 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 29 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 30 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 31 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 32 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 33 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 34 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 35 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 36 |
|
|
//
|
| 37 |
|
|
// ============================================================================
|
| 38 |
|
|
//
|
| 39 |
|
|
// The following added breaks up the carry chain.
|
| 40 |
|
|
//
|
| 41 |
|
|
// The adder is organized into three rows and N*2 columns of digits. The first
|
| 42 |
|
|
// row is set to the input a, b operands. The carry for the first row and column
|
| 43 |
|
|
// is set to the carry input. The operands in a row are added without taking
|
| 44 |
|
|
// carry into consideration. But the carry out is recorded and added as the 'B'
|
| 45 |
|
|
// operand in the next row. The sum from one row is fed as the 'A' operand into
|
| 46 |
|
|
// the next row. Values (sum and carry) are moved between rows in a clocked
|
| 47 |
|
|
// fashion. The first row contains full BCD adders. After that only a single
|
| 48 |
|
|
// bit is added.
|
| 49 |
|
|
//
|
| 50 |
|
|
// There cannot be more than three rows required. The worst case scenario occurs
|
| 51 |
|
|
// when all the inputs are at a max and there is a carry input. This generates a
|
| 52 |
|
|
// carry output from each digit in the second row.
|
| 53 |
|
|
//
|
| 54 |
|
|
// In that case
|
| 55 |
|
|
// 99999999
|
| 56 |
|
|
// + 99999999
|
| 57 |
|
|
// + 1 (carry in)
|
| 58 |
|
|
//---------------
|
| 59 |
|
|
// 188888889 <- first row result
|
| 60 |
|
|
// + 11111110 <- carries out of first row
|
| 61 |
|
|
//---------------
|
| 62 |
|
|
// 199999999 <- second row result
|
| 63 |
|
|
|
| 64 |
70 |
robfinch |
module BCDSub8NClk(clk, a, b, o, ci, co);
|
| 65 |
66 |
robfinch |
parameter N=33;
|
| 66 |
|
|
input clk;
|
| 67 |
|
|
input [N*8-1:0] a;
|
| 68 |
|
|
input [N*8-1:0] b;
|
| 69 |
|
|
output reg [N*8-1:0] o;
|
| 70 |
|
|
input ci;
|
| 71 |
|
|
output reg co;
|
| 72 |
|
|
|
| 73 |
|
|
reg [N-1:0] c [0:2];
|
| 74 |
|
|
wire [N*8-1:0] o1 [0:2];
|
| 75 |
|
|
reg [N*8-1:0] o2 [0:2];
|
| 76 |
|
|
wire [N:0] d [0:2];
|
| 77 |
|
|
|
| 78 |
|
|
genvar g,k;
|
| 79 |
|
|
generate begin : gBCDadd
|
| 80 |
|
|
for (g = 0; g < N; g = g + 1) begin
|
| 81 |
|
|
for (k = 0; k < 3; k = k + 1) begin
|
| 82 |
70 |
robfinch |
initial begin
|
| 83 |
|
|
c[k][g] <= 'b0;
|
| 84 |
|
|
end
|
| 85 |
66 |
robfinch |
BCDSub u1 (
|
| 86 |
|
|
.ci(k==0 && g==0 ? ci : 1'b0),
|
| 87 |
|
|
.a(k==0 ? a[g*8+7:g*8] : o2[k-1][g*8+7:g*8]),
|
| 88 |
|
|
.b(k==0 ? b[g*8+7:g*8] : {7'h00,c[k-1][g]}),
|
| 89 |
|
|
.o(o1[k][g*8+7:g*8]),
|
| 90 |
|
|
.c(d[k][g])
|
| 91 |
|
|
);
|
| 92 |
|
|
always_ff @(posedge clk)
|
| 93 |
|
|
o2[k] <= o1[k];
|
| 94 |
|
|
always_ff @(posedge clk)
|
| 95 |
|
|
c[k][g] <= d[k][g];
|
| 96 |
|
|
end
|
| 97 |
|
|
end
|
| 98 |
|
|
always_ff @(posedge clk)
|
| 99 |
|
|
begin
|
| 100 |
|
|
o <= o1[2];
|
| 101 |
70 |
robfinch |
co <= c[2][N-1]|c[1][N-1]|c[0][N-1];
|
| 102 |
66 |
robfinch |
end
|
| 103 |
|
|
end
|
| 104 |
|
|
endgenerate
|
| 105 |
|
|
endmodule
|
| 106 |
|
|
|