-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
matcher.v
146 lines (138 loc) · 4.53 KB
/
matcher.v
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
143
144
145
146
module matcher(
input clk,
input rst_n,
input left_valid,
input [31:0] left_keypoint,
input [15:0] left_descriptor_channel_1,
input [15:0] left_descriptor_channel_2,
input [15:0] left_descriptor_channel_3,
input [15:0] left_descriptor_channel_4,
input [15:0] left_done,
input right_valid,
input [31:0] right_keypoint,
input [15:0] right_descriptor_channel_1,
input [15:0] right_descriptor_channel_2,
input [15:0] right_descriptor_channel_3,
input [15:0] right_descriptor_channel_4,
input [15:0] right_done,
output data_valid,
output [31:0] left_point,
output [31:0] right_point);
reg [31:0] left_keypoints[0:(N*M)/9];
reg [15:0] left_descriptor1[0:(N*M)/9];
reg [15:0] left_descriptor2[0:(N*M)/9];
reg [15:0] left_descriptor3[0:(N*M)/9];
reg [15:0] left_descriptor4[0:(N*M)/9];
reg [31:0] right_keypoints[0:(N*M)/9];
reg [15:0] right_descriptor1[0:(N*M)/9];
reg [15:0] right_descriptor2[0:(N*M)/9];
reg [15:0] right_descriptor3[0:(N*M)/9];
reg [15:0] right_descriptor4[0:(N*M)/9];
reg [2:0] PS, NS;
reg flag1;
reg flag2;
parameter IDLE = 3'b000, STORE = 3'b001, LOAD = 3'b010, MATCH = 3'b011, DONE = 3'b100;
parameter N = 450, M = 450;
integer i, j, p, q, left_limit, right_limit, least_error, error, error_channel1, error_channel2, error_channel3, error_channel4, sample_keypoint1, sample_keypoint2, final_keypoint1, final_keypoint2;
always @(posedge clk or negedge rst_n)
begin
if (~rst_n)
PS <= IDLE;
else
PS <= NS;
end
always @(posedge clk)
begin
if (left_valid)
begin
left_keypoints[i] <= left_keypoint;
left_descriptor1[i] <= left_descriptor_channel_1;
left_descriptor2[i] <= left_descriptor_channel_2;
left_descriptor3[i] <= left_descriptor_channel_3;
left_descriptor4[i] <= left_descriptor_channel_4;
i <= i + 1;
left_limit <= i;
end
else
begin
i <= i;
left_limit <= i;
end
end
always @(posedge clk)
begin
if (right_valid)
begin
right_keypoints[j] <= right_keypoint;
right_descriptor1[j] <= right_descriptor_channel_1;
right_descriptor2[j] <= right_descriptor_channel_2;
right_descriptor3[j] <= right_descriptor_channel_3;
right_descriptor4[j] <= right_descriptor_channel_4;
j <= j + 1;
right_limit <= j;
end
else
begin
j <= j;
right_limit <= j;
end
end
always @(left_valid, right_valid, left_done, right_done, PS, i, j, p, q)
begin
case (PS)
IDLE: begin
i = 0;
j = 0;
p = 0;
q = 0;
left_limit = 0;
right_limit = 0;
flag1 = 1'b0;
flag2 = 1'b0;
least_error = 32'h0fffffff;
error = 0;
error_channel1 = 0;
error_channel2 = 0;
error_channel3 = 0;
error_channel4 = 0;
sample_keypoint1 = 0;
sample_keypoint2 = 0;
final_keypoint1 = 0;
final_keypoint2 = 0;
if (left_valid || right_valid)
NS = STORE;
else
NS = IDLE;
end
STORE: begin
flag1 = (left_done == 1) ? 1'b1 : flag1;
flag2 = (right_done == 1) ? 1'b1 : flag2;
NS = (flag1 == 1 && flag2 == 1) ? LOAD : STORE;
end
LOAD: begin
error_channel1 = (left_descriptor1[p] > right_descriptor1[q]) ? left_descriptor1[p]-right_descriptor1[q] : right_descriptor1[q]-left_descriptor1[p];
error_channel2 = (left_descriptor2[p] > right_descriptor2[q]) ? left_descriptor2[p]-right_descriptor2[q] : right_descriptor2[q]-left_descriptor2[p];
error_channel3 = (left_descriptor3[p] > right_descriptor3[q]) ? left_descriptor3[p]-right_descriptor3[q] : right_descriptor3[q]-left_descriptor3[p];
error_channel4 = (left_descriptor4[p] > right_descriptor4[q]) ? left_descriptor4[p]-right_descriptor4[q] : right_descriptor4[q]-left_descriptor4[p];
error = error_channel1 + error_channel2 + error_channel3 + error_channel4;
sample_keypoint1 = left_keypoints[p];
sample_keypoint2 = right_keypoints[q];
p = (p == left_limit-1) ? 0 : p + 1;
q = (p == 0) ? q + 1 : q;
NS = (q == right_limit) ? DONE : MATCH;
end
MATCH: begin
final_keypoint1 = (least_error < error) ? final_keypoint1 : sample_keypoint1;
final_keypoint2 = (least_error < error) ? final_keypoint2 : sample_keypoint2;
least_error = (least_error < error) ? least_error : error;
NS = LOAD;
end
DONE: begin
NS = IDLE;
end
endcase
end
assign data_valid = (PS == DONE) ? 1'b1 : 1'b0;
assign left_point = (PS == DONE) ? final_keypoint1 : 32'hzzzzzzzz;
assign right_point = (PS == DONE) ? final_keypoint2 : 32'hzzzzzzzz;
endmodule