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

Subversion Repositories signed_unsigned_multiplier_and_divider

[/] [signed_unsigned_multiplier_and_divider/] [trunk/] [bin2bcd.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zpekic
----------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer: 
4
-- 
5
-- Create Date:    18:02:11 03/25/2018 
6
-- Design Name: 
7
-- Module Name:    bin2bcd - Behavioral 
8
-- Project Name: 
9
-- Target Devices: 
10
-- Tool versions: 
11
-- Description: 
12
--
13
-- Dependencies: 
14
--
15
-- Revision: 
16
-- Revision 0.01 - File Created
17
-- Additional Comments: 
18
--
19
----------------------------------------------------------------------------------
20
library IEEE;
21
use IEEE.STD_LOGIC_1164.ALL;
22
 
23
-- Uncomment the following library declaration if using
24
-- arithmetic functions with Signed or Unsigned values
25
use IEEE.NUMERIC_STD.ALL;
26
 
27
-- Uncomment the following library declaration if instantiating
28
-- any Xilinx primitives in this code.
29
--library UNISIM;
30
--use UNISIM.VComponents.all;
31
 
32
entity bin2bcd is
33
    Port ( reset : in  STD_LOGIC;               -- Initialize when high, note that it will drive ready low
34
           clk : in  STD_LOGIC;                 -- Drives the FSM
35
           start : in  STD_LOGIC;               -- Keep high for at least 1 clock cycle to start conversion
36
           bin : in  STD_LOGIC_VECTOR (15 downto 0);             -- 16-bit unsigned binary input value
37
                          ready : out  STD_LOGIC;               -- pulse high when conversion is done
38
           bcd : buffer STD_LOGIC_VECTOR (23 downto 0);  -- 6 BCD digits output
39
                          debug: out STD_LOGIC_VECTOR(3 downto 0));              -- debug port, leave open
40
end bin2bcd;
41
 
42
architecture Behavioral of bin2bcd is
43
 
44
component bcddigitadder is
45
    Port ( ci : in  STD_LOGIC;
46
           a : in  STD_LOGIC_VECTOR (3 downto 0);
47
           b : in  STD_LOGIC_VECTOR (3 downto 0);
48
           y : out  STD_LOGIC_VECTOR (3 downto 0);
49
           cout : out  STD_LOGIC);
50
end component;
51
 
52
type state is (st_reset, st_readytostart, st_loadbinval, st_checkzero,
53
                                        st_delay, st_update,
54
                                        st_shiftdown, st_done);
55
 
56
type rom24x16 is array(0 to 15) of std_logic_vector(23 downto 0);
57
constant bcd_lookup: rom24x16 := (
58
 
59
        1 =>    X"000002",
60
        2 =>    X"000004",
61
        3 =>    X"000008",
62
        4 =>    X"000016",
63
        5 =>    X"000032",
64
        6 =>    X"000064",
65
        7 =>    X"000128",
66
        8 =>    X"000256",
67
        9 =>    X"000512",
68
        10 =>   X"001024",
69
        11 => X"002048",
70
        12 =>   X"004096",
71
        13 =>   X"008192",
72
        14 =>   X"016384",
73
        15 =>   X"032768"
74
);
75
 
76
signal state_current, state_next: state;
77
 
78
signal power: integer range 0 to 15;     -- index to bcd lookup table (2^power in bcd format)
79
signal zero: std_logic;
80
signal ripple_carry: std_logic_vector(6 downto 0);
81
signal bin_val: std_logic_vector(15 downto 0);
82
signal power_val: std_logic_vector(23 downto 0);
83
signal bcd_plus_power: std_logic_vector(23 downto 0);
84
 
85
begin
86
 
87
debug <= std_logic_vector(to_unsigned(power, 4));
88
 
89
power_val <= bcd_lookup(power);
90
ripple_carry(0) <= '0';
91
zero <= '1' when bin_val = X"0000" else '0';
92
 
93
gen_digit_adders: for i in 0 to 5 generate -- six output digits
94
        digadd: bcddigitadder port map
95
                                (
96
                                        ci => ripple_carry(i),
97
                                        a => bcd(3 + 4 * i downto 4 * i),
98
                                        b => power_val(3 + 4 * i downto 4 * i),
99
                                        y => bcd_plus_power(3 + 4 * i downto 4 * i),
100
                                        cout => ripple_carry(i + 1)
101
                                );
102
end generate;
103
 
104
-- FSM
105
drive: process(reset, clk, state_next)
106
begin
107
        if (reset = '1') then
108
                state_current <= st_reset;
109
        else
110
                if (rising_edge(clk)) then
111
                        state_current <= state_next;
112
                end if;
113
        end if;
114
end process;
115
 
116
execute: process(clk, state_current)
117
begin
118
        if (rising_edge(clk)) then
119
                case state_current is
120
                        when st_reset =>
121
                                ready <= '0';
122
 
123
                        when st_readytostart =>
124
                                ready <= '1';
125
                                power <= 0;
126
 
127
                        when st_loadbinval =>
128
                                ready <= '0';
129
                                bin_val <= bin;
130
                                bcd <= X"000000";
131
 
132
                        when st_checkzero =>
133
 
134
                        when st_delay =>
135
 
136
                        when st_update =>
137
                                bcd <= bcd_plus_power;
138
 
139
                        when st_shiftdown =>
140
                                bin_val <= '0' & bin_val(15 downto 1);
141
                                power <= power + 1;
142
 
143
                        when st_done =>
144
                                ready <= '1';
145
 
146
                        when others =>
147
                                null;
148
 
149
                end case;
150
        end if;
151
end process;
152
 
153
sequence: process(state_current, zero, bin_val)
154
begin
155
        case state_current is
156
                when st_reset =>
157
                        state_next <= st_readytostart;
158
 
159
                when st_readytostart =>
160
                        if (start = '1') then
161
                                state_next <= st_loadbinval;
162
                        else
163
                                state_next <= st_readytostart; -- continue waiting for start
164
                        end if;
165
 
166
                when st_loadbinval =>
167
                        state_next <= st_checkzero;
168
 
169
                when st_checkzero =>
170
                        if (zero = '1') then
171
                                state_next <= st_done;
172
                        else
173
                                if (bin_val(0) = '1') then
174
                                        state_next <= st_delay;
175
                                else
176
                                        state_next <= st_shiftdown;
177
                                end if;
178
                        end if;
179
 
180
                when st_delay =>
181
                        state_next <= st_update;
182
 
183
                when st_update =>
184
                        state_next <= st_shiftdown;
185
 
186
                when st_shiftdown =>
187
                        state_next <= st_checkzero;
188
 
189
                when st_done =>
190
                        state_next <= st_readytostart;
191
 
192
                when others =>
193
                        null;
194
        end case;
195
end process;
196
 
197
 
198
end Behavioral;
199
 

powered by: WebSVN 2.1.0

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