OpenCores
URL https://opencores.org/ocsvn/ft816float/ft816float/trunk

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DDBinToBCDFract.sv] - Blame information for rev 80

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 80 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 DDBinToBCDFract(rst, clk, ld, bin, bcd, done);
41
parameter WID = 128;
42
parameter DEP = 2;              // cascade 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 [WID+3:0] bin10;                                                    // working binary value
55
reg [BCDWID-1:0] bcdwt;
56
reg [BCDWID-1:0] bcdw [0:DEP-1];        // working bcd value
57
reg [7:0] bitcnt;
58
reg [2:0] state;
59
parameter IDLE = 3'd0;
60
parameter CHK5 = 3'd1;
61
parameter SHFT = 3'd2;
62
parameter DONE = 3'd3;
63
 
64
function [BCDWID-1:0] fnRow;
65
input [BCDWID-1:0] i;
66
input lsb;
67
begin
68
        fnRow = 'd0;
69
        for (k = 0; k < BCDWID; k = k + 4)
70
                if (((i >> k) & 4'hF) > 4'd4)
71
                        fnRow = fnRow | (((i >> k) & 4'hF) + 4'd3) << k;
72
                else
73
                        fnRow = fnRow | ((i >> k) & 4'hf) << k;
74
        fnRow = {fnRow,lsb};
75
end
76
endfunction
77
 
78
always_comb
79
        bcdw[0] = fnRow(bcdwt,binw[WID-1]);
80
generate begin : gRows
81
        for (n = 1; n < DEP; n = n + 1)
82
                always_comb
83
                        bcdw[n] = fnRow(bcdw[n-1],binw[WID-1-n]);
84
end
85
endgenerate
86
 
87
always_ff @(posedge clk)
88
        if (WID % DEP) begin
89
                $display("Width must be a multiple of DEP, DEP must be at least 2.");
90
                $finish;
91
        end
92
 
93
always_ff @(posedge clk)
94
if (rst) begin
95
        state <= IDLE;
96
        done <= 1'b1;
97
        bcdwt <= 'd0;
98
        binw <= 'd0;
99
        bitcnt <= BCDWID/4;
100
end
101
else begin
102
        if (ld) begin
103
                done <= 1'b0;
104
                bin10 <= bin;
105
                bcdwt <= 'd0;
106
                state <= SHFT;
107
                bcd <= 'd0;
108
        end
109
        else
110
        case(state)
111
        IDLE:   ;
112
        SHFT:
113
                begin
114
                        bitcnt <= bitcnt - 2'd1;
115
                        if (bitcnt==8'd1) begin
116
                                state <= DONE;
117
                        end
118
                        bin10 <= {bin10[WID-1:0],3'b0} + {bin10[WID-1:0],1'b0};
119
                        bcdwt <= {bcdwt,bin10[WID+3:WID]};
120
                end
121
        DONE:
122
                begin
123
                        bcd <= bcdwt;
124
                        done <= 1'b1;
125
                        state <= IDLE;
126
                end
127
        default:
128
                state <= IDLE;
129
        endcase
130
end
131
 
132
 
133
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.