-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSM.vhd
143 lines (134 loc) · 3.89 KB
/
FSM.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
ENTITY FSM IS
PORT (
clk, start, rst, load_ack, compute_done, move_done, flag_in, worse : in std_logic;
outer_address : in std_logic_vector (15 downto 0);
load_en, compute_en, move, flag_out, done : out std_logic;
total_sum_bak_out : out std_logic_vector (31 downto 0);
address : out std_logic_vector (15 downto 0);
total_sum_bak_in, total_sum_new : in std_logic_vector (31 downto 0);
in_direction : in std_logic;
out_direction : out std_logic
);
END FSM;
ARCHITECTURE FSM_A OF FSM IS
type state_type is (do_nothing, load_focus_matrix, set_direction, move_lens, compute_total_contrast, check, finish);
signal state : state_type;
signal next_state : state_type;
BEGIN
PROCESS (state, start, load_ack, move_done, compute_done, worse, flag_in, total_sum_bak_in, in_direction, total_sum_new)
BEGIN
CASE state is
when do_nothing =>
load_en <= '0';
compute_en <= '0';
move <= '0';
flag_out <= '0';
done <= '0';
out_direction <= '0';
total_sum_bak_out <= "11111111111111111111111111111111";
if start = '0' then
next_state <= do_nothing;
else
next_state <= load_focus_matrix;
end if;
when load_focus_matrix =>
load_en <= '1';
compute_en <= '0';
move <= '0';
flag_out <= flag_in;
done <= '0';
out_direction <= in_direction;
total_sum_bak_out <= total_sum_bak_in;
if load_ack = '1' then
next_state <= compute_total_contrast;
else next_state <= load_focus_matrix;
end if;
when set_direction =>
out_direction <= not in_direction;
load_en <= '0';
compute_en <= '0';
move <= '0';
flag_out <= flag_in;
done <= '0';
total_sum_bak_out <= total_sum_bak_in;
next_state <= move_lens;
when move_lens =>
load_en <= '0';
compute_en <= '0';
move <= '1';
flag_out <= flag_in;
done <= '0';
out_direction <= in_direction;
total_sum_bak_out <= total_sum_bak_in;
if move_done = '1' then
next_state <= load_focus_matrix;
else
next_state <= move_lens;
end if;
when compute_total_contrast =>
load_en <= '0';
compute_en <= '1';
move <= '0';
flag_out <= flag_in;
done <= '0';
out_direction <= in_direction;
total_sum_bak_out <= total_sum_bak_in;
if compute_done = '1' then
next_state <= check;
else next_state <= compute_total_contrast;
end if;
when check =>
load_en <= '0';
compute_en <= '0';
move <= '0';
done <= '0';
out_direction <= in_direction;
total_sum_bak_out <= total_sum_new;
if total_sum_bak_in = "11111111111111111111111111111111" then
flag_out <= '0';
next_state <= set_direction;
elsif flag_in = '0' and worse = '1' then
flag_out <= '1';
next_state <= set_direction;
elsif flag_in = '0' and worse = '0' then
flag_out <= '1';
next_state <= move_lens;
elsif flag_in = '1' and worse = '1' then
flag_out <= flag_in;
next_state <= finish;
else
flag_out <= flag_in;
next_state <= move_lens;
end if;
when finish =>
load_en <= '0';
compute_en <= '0';
move <= '0';
flag_out <= flag_in;
out_direction <= in_direction;
done <= '1';
total_sum_bak_out <= total_sum_bak_in;
next_state <= finish;
when others =>
load_en <= '0';
compute_en <= '0';
move <= '0';
flag_out <= '0';
done <= '0';
out_direction <= '0';
total_sum_bak_out <= "11111111111111111111111111111111";
next_state <= do_nothing;
END CASE;
END PROCESS;
address <= outer_address;
PROCESS (clk, rst)
BEGIN
IF rst = '1' THEN
state <= do_nothing;
ELSIF rising_edge(clk) THEN
state <= next_state;
END IF;
END PROCESS;
End FSM_A;