-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart1.vhd
More file actions
80 lines (63 loc) · 2.25 KB
/
Copy pathpart1.vhd
File metadata and controls
80 lines (63 loc) · 2.25 KB
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
library IEEE;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity three_k_plus_one is
port (
reset : in std_logic;
clk : in std_logic;
number_out : out unsigned(6 downto 0);
term_out : out unsigned(6 downto 0);
done_out : out std_logic);
end three_k_plus_one;
architecture behavioral of three_k_plus_one is
signal number : unsigned(6 downto 0);
signal term : unsigned(6 downto 0);
signal length : unsigned(6 downto 0);
signal done : std_logic;
type state_type is (INIT, CHECK_TERM, CALC_NEXT, DONE_STATE);
signal state : state_type;
begin
process(clk, reset)
begin
if reset = '1' then
-- Initial values
number <= to_unsigned(1, 7);
term <= to_unsigned(1, 7);
length <= to_unsigned(1, 7);
done <= '0';
state <= INIT;
elsif rising_edge(clk) then
if done = '0' then
case state is
when INIT => number <= number + 1;
term <= number + 1;
length <= to_unsigned(1, 7);
state <= CHECK_TERM;
when CHECK_TERM =>
if term = 1 then
if length >= 9 then
state <= DONE_STATE;
else
state <= INIT;
end if;
else
state <= CALC_NEXT;
end if;
when CALC_NEXT =>
length <= length + 1;
if term(0) = '0' then
term <= term / 2;
else
term <= resize((term * 3) + 1, 7);
end if;
state <= CHECK_TERM;
when DONE_STATE =>
done <= '1';
end case;
end if;
end if;
end process;
number_out <= number;
term_out <= term;
done_out <= done;
end behavioral;