-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin_to_bcd_convertor.vhd
115 lines (99 loc) · 3.42 KB
/
bin_to_bcd_convertor.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 10:35:00 01/06/2019
-- Design Name:
-- Module Name: bin_to_bcd_convertor - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity bin_to_bcd_convertor is
Port ( BIN_CNT_IN : in std_logic_vector(7 downto 0);
LSD_OUT : out std_logic_vector(3 downto 0);
MSD_OUT : out std_logic_vector(3 downto 0);
MMSD_OUT : out std_logic_vector(3 downto 0));
end bin_to_bcd_convertor;
architecture convertion_behavioral of bin_to_bcd_convertor is
begin
process(bin_cnt_in)
variable cnt_tot : INTEGER range 0 to 255 := 0;
variable lsd,msd,mmsd : INTEGER range 0 to 9 := 0;
begin
cnt_tot := 0;
if (bin_cnt_in(7) = '1') then cnt_tot := cnt_tot + 128; end if;
if (bin_cnt_in(6) = '1') then cnt_tot := cnt_tot + 64; end if;
if (bin_cnt_in(5) = '1') then cnt_tot := cnt_tot + 32; end if;
if (bin_cnt_in(4) = '1') then cnt_tot := cnt_tot + 16; end if;
if (bin_cnt_in(3) = '1') then cnt_tot := cnt_tot + 8; end if;
if (bin_cnt_in(2) = '1') then cnt_tot := cnt_tot + 4; end if;
if (bin_cnt_in(1) = '1') then cnt_tot := cnt_tot + 2; end if;
if (bin_cnt_in(0) = '1') then cnt_tot := cnt_tot + 1; end if;
msd := 0;
mmsd := 0;
lsd := 0;
for I in 1 to 2 loop
exit when (cnt_tot >= 0 and cnt_tot < 100);
mmsd := mmsd + 1; -- increment the mmds count
cnt_tot := cnt_tot - 100;
end loop;
for I in 1 to 9 loop
exit when (cnt_tot >= 0 and cnt_tot < 10);
msd := msd + 1; -- increment the mds count
cnt_tot := cnt_tot - 10;
end loop;
lsd := cnt_tot;
case lsd is
when 9 => lsd_out <= "1001";
when 8 => lsd_out <= "1000";
when 7 => lsd_out <= "0111";
when 6 => lsd_out <= "0110";
when 5 => lsd_out <= "0101";
when 4 => lsd_out <= "0100";
when 3 => lsd_out <= "0011";
when 2 => lsd_out <= "0010";
when 1 => lsd_out <= "0001";
when 0 => lsd_out <= "0000";
when others => lsd_out <= "0000";
end case;
case msd is
when 9 => msd_out <= "1001";
when 8 => msd_out <= "1000";
when 7 => msd_out <= "0111";
when 6 => msd_out <= "0110";
when 5 => msd_out <= "0101";
when 4 => msd_out <= "0100";
when 3 => msd_out <= "0011";
when 2 => msd_out <= "0010";
when 1 => msd_out <= "0001";
when 0 => msd_out <= "0000";
when others => msd_out <= "0000";
end case;
case mmsd is
when 2 => mmsd_out <= "0010";
when 1 => mmsd_out <= "0001";
when 0 => mmsd_out <= "0000";
when others => mmsd_out <= "0000";
end case;
end process;
end convertion_behavioral;