| 1 |
59 |
robfinch |
`timescale 1ns / 1ps
|
| 2 |
|
|
// ============================================================================
|
| 3 |
|
|
// __
|
| 4 |
|
|
// \\__/ o\ (C) 2022 Robert Finch, Waterloo
|
| 5 |
|
|
// \ __ / All rights reserved.
|
| 6 |
|
|
// \/_// robfinch@finitron.ca
|
| 7 |
|
|
// ||
|
| 8 |
|
|
//
|
| 9 |
|
|
// DDBinToBCD.sv
|
| 10 |
|
|
// Uses the Dubble Dabble algorithm
|
| 11 |
|
|
//
|
| 12 |
|
|
// BSD 3-Clause License
|
| 13 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 14 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 15 |
|
|
//
|
| 16 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
| 17 |
|
|
// list of conditions and the following disclaimer.
|
| 18 |
|
|
//
|
| 19 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 20 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 21 |
|
|
// and/or other materials provided with the distribution.
|
| 22 |
|
|
//
|
| 23 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
| 24 |
|
|
// contributors may be used to endorse or promote products derived from
|
| 25 |
|
|
// this software without specific prior written permission.
|
| 26 |
|
|
//
|
| 27 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 28 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 29 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 30 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 31 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 32 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 33 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 34 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 35 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 36 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 37 |
|
|
//
|
| 38 |
|
|
// ============================================================================
|
| 39 |
|
|
//
|
| 40 |
|
|
module DDBinToBCD(rst, clk, ld, bin, bcd, done);
|
| 41 |
|
|
parameter WID = 128;
|
| 42 |
|
|
parameter DEP = 2; // pipeline depth
|
| 43 |
|
|
localparam BCDWID = ((WID+(WID-4)/3)+3) & -4;
|
| 44 |
|
|
input rst;
|
| 45 |
|
|
input clk;
|
| 46 |
|
|
input ld;
|
| 47 |
|
|
input [WID-1:0] bin;
|
| 48 |
|
|
output reg [BCDWID-1:0] bcd;
|
| 49 |
|
|
output reg done;
|
| 50 |
|
|
|
| 51 |
|
|
integer k;
|
| 52 |
|
|
genvar n,g;
|
| 53 |
|
|
reg [WID-1:0] binw; // working binary value
|
| 54 |
|
|
reg [BCDWID-1:0] bcdwt;
|
| 55 |
|
|
reg [BCDWID-1:0] bcdw [0:DEP-1]; // working bcd value
|
| 56 |
|
|
reg [7:0] bitcnt;
|
| 57 |
|
|
reg [2:0] state;
|
| 58 |
|
|
parameter IDLE = 3'd0;
|
| 59 |
|
|
parameter CHK5 = 3'd1;
|
| 60 |
|
|
parameter SHFT = 3'd2;
|
| 61 |
|
|
parameter DONE = 3'd3;
|
| 62 |
|
|
|
| 63 |
|
|
function [BCDWID-1:0] fnRow;
|
| 64 |
|
|
input [BCDWID-1:0] i;
|
| 65 |
|
|
input lsb;
|
| 66 |
|
|
begin
|
| 67 |
|
|
fnRow = 'd0;
|
| 68 |
|
|
for (k = 0; k < BCDWID; k = k + 4)
|
| 69 |
|
|
if (((i >> k) & 4'hF) > 4'd4)
|
| 70 |
|
|
fnRow = fnRow | (((i >> k) & 4'hF) + 4'd3) << k;
|
| 71 |
|
|
else
|
| 72 |
|
|
fnRow = fnRow | ((i >> k) & 4'hf) << k;
|
| 73 |
|
|
fnRow = {fnRow,lsb};
|
| 74 |
|
|
end
|
| 75 |
|
|
endfunction
|
| 76 |
|
|
|
| 77 |
|
|
always_comb
|
| 78 |
|
|
bcdw[0] = fnRow(bcdwt,binw[WID-1]);
|
| 79 |
|
|
generate begin : gRows
|
| 80 |
|
|
for (n = 1; n < DEP; n = n + 1)
|
| 81 |
|
|
always_comb
|
| 82 |
|
|
bcdw[n] = fnRow(bcdw[n-1],binw[WID-1-n]);
|
| 83 |
|
|
end
|
| 84 |
|
|
endgenerate
|
| 85 |
|
|
|
| 86 |
|
|
always_ff @(posedge clk)
|
| 87 |
|
|
if (rst) begin
|
| 88 |
|
|
state <= IDLE;
|
| 89 |
|
|
done <= 1'b1;
|
| 90 |
|
|
bcdwt <= 'd0;
|
| 91 |
|
|
binw <= 'd0;
|
| 92 |
|
|
bitcnt <= 'd0;
|
| 93 |
|
|
end
|
| 94 |
|
|
else begin
|
| 95 |
|
|
if (ld) begin
|
| 96 |
|
|
done <= 1'b0;
|
| 97 |
|
|
bitcnt <= (WID+DEP-1)/DEP-1;
|
| 98 |
|
|
binw <= bin << DEP;
|
| 99 |
|
|
bcdwt <= 'd0;
|
| 100 |
|
|
state <= SHFT;
|
| 101 |
|
|
end
|
| 102 |
|
|
else
|
| 103 |
|
|
case(state)
|
| 104 |
|
|
IDLE: ;
|
| 105 |
|
|
SHFT:
|
| 106 |
|
|
begin
|
| 107 |
|
|
bitcnt <= bitcnt - 2'd1;
|
| 108 |
|
|
if (bitcnt==8'd1) begin
|
| 109 |
|
|
state <= DONE;
|
| 110 |
|
|
end
|
| 111 |
|
|
bcdwt <= bcdw[DEP-1];
|
| 112 |
|
|
binw <= binw << DEP;
|
| 113 |
|
|
end
|
| 114 |
|
|
DONE:
|
| 115 |
|
|
begin
|
| 116 |
|
|
bcd <= bcdwt;
|
| 117 |
|
|
done <= 1'b1;
|
| 118 |
|
|
state <= IDLE;
|
| 119 |
|
|
end
|
| 120 |
|
|
default:
|
| 121 |
|
|
state <= IDLE;
|
| 122 |
|
|
endcase
|
| 123 |
|
|
end
|
| 124 |
|
|
|
| 125 |
|
|
|
| 126 |
|
|
endmodule
|