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

Subversion Repositories ft816float

[/] [ft816float/] [trunk/] [rtl/] [verilog2/] [DDBCDToBin.sv] - Blame information for rev 70

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
//      DDBCDToBin.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 DDBCDToBin(rst, clk, ld, bcd, bin, 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 [BCDWID-1:0] bcd;
48
output reg [WID-1:0] bin;
49
output reg done;
50
 
51
integer k;
52
genvar n,g;
53
reg [WID-1:0] binw, binwt;                      // working binary value
54
reg [BCDWID-1:0] bcdwt;
55
reg [BCDWID-1:0] bcdw [0:DEP];  // 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
begin
66
        fnRow = 'd0;
67
        for (k = 0; k < BCDWID; k = k + 4)
68
                if (((i >> k) & 4'hF) >= 4'd8)
69
                        fnRow = fnRow | (((i >> k) & 4'hF) - 4'd3) << k;
70
                else
71
                        fnRow = fnRow | ((i >> k) & 4'hf) << k;
72
end
73
endfunction
74
 
75
always_comb
76
        bcdw[0] = bcdwt;
77
generate begin : gRows
78
        for (n = 0; n < DEP; n = n + 1)
79
                always_comb
80
                begin
81
                        binwt[WID-DEP+n] = bcdw[n][0];
82
                        bcdw[n+1] = fnRow({1'b0,bcdw[n][BCDWID-1:1]});
83
                end
84
end
85
endgenerate
86
 
87
always_ff @(posedge clk)
88
if (rst) begin
89
        state <= IDLE;
90
        done <= 1'b1;
91
        bcdwt <= 'd0;
92
        binw <= 'd0;
93
        bitcnt <= 'd0;
94
        bin <= 'd0;
95
end
96
else begin
97
        if (ld) begin
98
                done <= 1'b0;
99
                bitcnt <= (WID+DEP-1)/DEP-1;
100
                binw <= 'd0;
101
                bcdwt <= bcd;
102
                state <= SHFT;
103
        end
104
        else
105
        case(state)
106
        IDLE:   ;
107
        SHFT:
108
                begin
109
                        bitcnt <= bitcnt - 2'd1;
110
                        if (bitcnt==8'd0) begin
111
                                state <= DONE;
112
                        end
113
                        bcdwt <= bcdw[DEP];
114
                        binw <= {binwt[WID-1:WID-DEP],binw[WID-1:DEP]};
115
                end
116
        DONE:
117
                begin
118
                        bin <= binw;
119
                        done <= 1'b1;
120
                        state <= IDLE;
121
                end
122
        default:
123
                state <= IDLE;
124
        endcase
125
end
126
 
127
 
128
endmodule

powered by: WebSVN 2.1.0

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