1
+ library ieee;
2
+ use ieee.std_logic_1164.all ;
3
+ use ieee.std_logic_unsigned.all ;
4
+ entity binary_bcd is
5
+ generic (N: positive := 6 );
6
+ port (
7
+ clk, reset: in std_logic ;
8
+ binary_in: in std_logic_vector (N- 1 downto 0 );
9
+ -- bcd0, bcd1, bcd2, bcd3, bcd4: out std_logic_vector(3 downto 0)
10
+ bcd0, bcd1: out std_logic_vector (3 downto 0 )
11
+ );
12
+ end binary_bcd ;
13
+ architecture behaviour of binary_bcd is
14
+ type states is (start, shift, done);
15
+ signal state, state_next: states;
16
+ signal binary, binary_next: std_logic_vector (N- 1 downto 0 );
17
+ signal bcds, bcds_reg, bcds_next: std_logic_vector (19 downto 0 );
18
+ -- output register keep output constant during conversion
19
+ signal bcds_out_reg, bcds_out_reg_next: std_logic_vector (19 downto 0 );
20
+ -- need to keep track of shifts
21
+ signal shift_counter, shift_counter_next: natural range 0 to N;
22
+ begin
23
+ process (clk, reset)
24
+ begin
25
+ if reset = '1' then
26
+ binary <= (others => '0' );
27
+ bcds <= (others => '0' );
28
+ state <= start;
29
+ bcds_out_reg <= (others => '0' );
30
+ shift_counter <= 0 ;
31
+ elsif falling_edge (clk) then
32
+ binary <= binary_next;
33
+ bcds <= bcds_next;
34
+ state <= state_next;
35
+ bcds_out_reg <= bcds_out_reg_next;
36
+ shift_counter <= shift_counter_next;
37
+ end if ;
38
+ end process ;
39
+ convert:
40
+ process (state, binary, binary_in, bcds, bcds_reg, shift_counter)
41
+ begin
42
+ state_next <= state;
43
+ bcds_next <= bcds;
44
+ binary_next <= binary;
45
+ shift_counter_next <= shift_counter;
46
+ case state is
47
+ when start =>
48
+ state_next <= shift;
49
+ binary_next <= binary_in;
50
+ bcds_next <= (others => '0' );
51
+ shift_counter_next <= 0 ;
52
+ when shift =>
53
+ if shift_counter = N then
54
+ state_next <= done;
55
+ else
56
+ binary_next <= binary(N- 2 downto 0 ) & 'L' ;
57
+ bcds_next <= bcds_reg(18 downto 0 ) & binary(N- 1 );
58
+ shift_counter_next <= shift_counter + 1 ;
59
+ end if ;
60
+ when done =>
61
+ state_next <= start;
62
+ end case ;
63
+ end process ;
64
+ bcds_reg(19 downto 16 ) <= bcds(19 downto 16 ) + 3 when bcds(19 downto 16 ) > 4 else
65
+ bcds(19 downto 16 );
66
+ bcds_reg(15 downto 12 ) <= bcds(15 downto 12 ) + 3 when bcds(15 downto 12 ) > 4 else
67
+ bcds(15 downto 12 );
68
+ bcds_reg(11 downto 8 ) <= bcds(11 downto 8 ) + 3 when bcds(11 downto 8 ) > 4 else
69
+ bcds(11 downto 8 );
70
+ bcds_reg(7 downto 4 ) <= bcds(7 downto 4 ) + 3 when bcds(7 downto 4 ) > 4 else
71
+ bcds(7 downto 4 );
72
+ bcds_reg(3 downto 0 ) <= bcds(3 downto 0 ) + 3 when bcds(3 downto 0 ) > 4 else
73
+ bcds(3 downto 0 );
74
+ bcds_out_reg_next <= bcds when state = done else
75
+ bcds_out_reg;
76
+ -- bcd4 <= bcds_out_reg(19 downto 16);
77
+ -- bcd3 <= bcds_out_reg(15 downto 12);
78
+ -- bcd2 <= bcds_out_reg(11 downto 8);
79
+ bcd1 <= bcds_out_reg(7 downto 4 );
80
+ bcd0 <= bcds_out_reg(3 downto 0 );
81
+ end behaviour ;
0 commit comments