1 |
13 |
muhammedko |
----------------------------------------------------------------------------------
|
2 |
|
|
-- Company:
|
3 |
|
|
-- Engineer: MUHAMMED KOCAOGLU
|
4 |
|
|
--
|
5 |
|
|
-- Create Date: 01/13/2022 10:56:24 PM
|
6 |
|
|
-- Design Name:
|
7 |
|
|
-- Module Name: ScharrFilter - Behavioral
|
8 |
|
|
-- Project Name:
|
9 |
|
|
-- Target Devices:
|
10 |
|
|
-- Tool Versions:
|
11 |
|
|
-- Description:
|
12 |
|
|
--
|
13 |
|
|
-- Dependencies:
|
14 |
|
|
--
|
15 |
|
|
-- Revision:
|
16 |
|
|
-- Revision 0.01 - File Created
|
17 |
|
|
-- Additional Comments:
|
18 |
|
|
--
|
19 |
|
|
----------------------------------------------------------------------------------
|
20 |
|
|
LIBRARY IEEE;
|
21 |
|
|
USE IEEE.STD_LOGIC_1164.ALL;
|
22 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
23 |
|
|
USE work.OperatorOverloading_pkg.ALL;
|
24 |
|
|
USE work.EdgeDetection_pkg.ALL;
|
25 |
|
|
|
26 |
|
|
|
27 |
|
|
ENTITY ScharrFilter IS
|
28 |
|
|
PORT (
|
29 |
|
|
CLK : IN STD_LOGIC;
|
30 |
|
|
EdgeDetection_Enable : IN STD_LOGIC;
|
31 |
|
|
EdgeDetection_Disable : IN STD_LOGIC;
|
32 |
|
|
EdgeDetection_Din : IN array2D(0 TO 2)(7 DOWNTO 0);
|
33 |
|
|
EdgeDetection_Dout : OUT STD_LOGIC_VECTOR(31 DOWNTO 0);
|
34 |
|
|
EdgeDetection_Ready : OUT STD_LOGIC
|
35 |
|
|
);
|
36 |
|
|
END ScharrFilter;
|
37 |
|
|
|
38 |
|
|
ARCHITECTURE Behavioral OF ScharrFilter IS
|
39 |
|
|
|
40 |
|
|
signal Din_Buffer : array3D(0 to 2)(0 to 2)(7 downto 0) := (others => (others => (others => '0')));
|
41 |
|
|
|
42 |
|
|
signal MultArray_Reg_x : array3D(0 to 2)(0 to 2)(16 downto 0) := (others => (others => (others => '0')));
|
43 |
|
|
signal AddArray_Layer1_x : array2D(0 to 2)(16 downto 0) := (others => (others => '0'));
|
44 |
|
|
signal AddArray_Layer2_x : std_logic_vector(16 downto 0) := (others => '0');
|
45 |
|
|
signal Convolution_Res_x : std_logic_vector(33 downto 0) := (others => '0');
|
46 |
|
|
|
47 |
|
|
signal MultArray_Reg_y : array3D(0 to 2)(0 to 2)(16 downto 0) := (others => (others => (others => '0')));
|
48 |
|
|
signal AddArray_Layer1_y : array2D(0 to 2)(16 downto 0) := (others => (others => '0'));
|
49 |
|
|
signal AddArray_Layer2_y : std_logic_vector(16 downto 0) := (others => '0');
|
50 |
|
|
signal Convolution_Res_y : std_logic_vector(33 downto 0) := (others => '0');
|
51 |
|
|
|
52 |
|
|
signal EdgeDetection_Dout_Reg : STD_LOGIC_VECTOR(33 DOWNTO 0) := (others => '0');
|
53 |
|
|
|
54 |
|
|
TYPE states IS (
|
55 |
|
|
S_IDLE,
|
56 |
|
|
S_CONVOLVE
|
57 |
|
|
);
|
58 |
|
|
SIGNAL state : states := S_IDLE;
|
59 |
|
|
|
60 |
|
|
constant Coeff_x : array3D(0 to 2)(0 to 2)(7 downto 0) := (
|
61 |
|
|
(x"03", x"00", x"fd"),
|
62 |
|
|
(x"0A", x"00", x"f6"),
|
63 |
|
|
(x"03", x"00", x"fd")
|
64 |
|
|
);
|
65 |
|
|
constant Coeff_y : array3D(0 to 2)(0 to 2)(7 downto 0) := (
|
66 |
|
|
(x"03", x"0A", x"03"),
|
67 |
|
|
(x"00", x"00", x"00"),
|
68 |
|
|
(x"fd", x"f6", x"fd")
|
69 |
|
|
);
|
70 |
|
|
|
71 |
|
|
signal cntr : integer range 0 to 7 := 0;
|
72 |
|
|
signal cntrDisable : integer range 0 to 7 := 0;
|
73 |
|
|
|
74 |
|
|
BEGIN
|
75 |
|
|
|
76 |
|
|
P_MAIN : PROCESS (CLK)
|
77 |
|
|
BEGIN
|
78 |
|
|
IF rising_edge(CLK) THEN
|
79 |
|
|
CASE state IS
|
80 |
|
|
WHEN S_IDLE =>
|
81 |
|
|
EdgeDetection_Ready <= '0';
|
82 |
|
|
IF EdgeDetection_Enable THEN
|
83 |
|
|
state <= S_CONVOLVE;
|
84 |
|
|
Din_Buffer(0) <= EdgeDetection_Din;
|
85 |
|
|
Din_Buffer(1 to 2) <= Din_Buffer(0 to 1);
|
86 |
|
|
cntr <= 0;
|
87 |
|
|
END IF;
|
88 |
|
|
|
89 |
|
|
WHEN S_CONVOLVE =>
|
90 |
|
|
Din_Buffer(0) <= EdgeDetection_Din;
|
91 |
|
|
Din_Buffer(1 to 2) <= Din_Buffer(0 to 1);
|
92 |
|
|
|
93 |
|
|
MultArray_Reg_x <= Coeff_x * ("0" & Din_Buffer);
|
94 |
|
|
AddArray_Layer1_x(0) <= std_logic_vector(signed(MultArray_Reg_x(0)(0)) + signed(MultArray_Reg_x(0)(1)) + signed(MultArray_Reg_x(0)(2)));
|
95 |
|
|
AddArray_Layer1_x(1) <= std_logic_vector(signed(MultArray_Reg_x(1)(0)) + signed(MultArray_Reg_x(1)(1)) + signed(MultArray_Reg_x(1)(2)));
|
96 |
|
|
AddArray_Layer1_x(2) <= std_logic_vector(signed(MultArray_Reg_x(2)(0)) + signed(MultArray_Reg_x(2)(1)) + signed(MultArray_Reg_x(2)(2)));
|
97 |
|
|
AddArray_Layer2_x <= std_logic_vector(signed(AddArray_Layer1_x(0)) + signed(AddArray_Layer1_x(1)) + signed(AddArray_Layer1_x(2)));
|
98 |
|
|
Convolution_Res_x <= std_logic_vector(signed(AddArray_Layer2_x) * signed(AddArray_Layer2_x));
|
99 |
|
|
|
100 |
|
|
MultArray_Reg_y <= Coeff_y * ("0" & Din_Buffer);
|
101 |
|
|
AddArray_Layer1_y(0) <= std_logic_vector(signed(MultArray_Reg_y(0)(0)) + signed(MultArray_Reg_y(0)(1)) + signed(MultArray_Reg_y(0)(2)));
|
102 |
|
|
AddArray_Layer1_y(1) <= std_logic_vector(signed(MultArray_Reg_y(1)(0)) + signed(MultArray_Reg_y(1)(1)) + signed(MultArray_Reg_y(1)(2)));
|
103 |
|
|
AddArray_Layer1_y(2) <= std_logic_vector(signed(MultArray_Reg_y(2)(0)) + signed(MultArray_Reg_y(2)(1)) + signed(MultArray_Reg_y(2)(2)));
|
104 |
|
|
AddArray_Layer2_y <= std_logic_vector(signed(AddArray_Layer1_y(0)) + signed(AddArray_Layer1_y(1)) + signed(AddArray_Layer1_y(2)));
|
105 |
|
|
Convolution_Res_y <= std_logic_vector(signed(AddArray_Layer2_y) * signed(AddArray_Layer2_y));
|
106 |
|
|
|
107 |
|
|
EdgeDetection_Dout_Reg <= std_logic_vector(unsigned(Convolution_Res_x) + unsigned(Convolution_Res_y));
|
108 |
|
|
|
109 |
|
|
if cntr = 5 then
|
110 |
|
|
cntr <= 0;
|
111 |
|
|
EdgeDetection_Ready <= '1';
|
112 |
|
|
else
|
113 |
|
|
cntr <= cntr + 1;
|
114 |
|
|
end if;
|
115 |
|
|
|
116 |
|
|
if EdgeDetection_Disable = '1' then
|
117 |
|
|
cntrDisable <= cntrDisable + 1;
|
118 |
|
|
end if;
|
119 |
|
|
|
120 |
|
|
if cntrDisable = 6 then
|
121 |
|
|
state <= S_IDLE;
|
122 |
|
|
EdgeDetection_Ready <= '0';
|
123 |
|
|
cntr <= 0;
|
124 |
|
|
cntrDisable <= 0;
|
125 |
|
|
end if;
|
126 |
|
|
END CASE;
|
127 |
|
|
END IF;
|
128 |
|
|
END PROCESS;
|
129 |
|
|
EdgeDetection_Dout <= EdgeDetection_Dout_Reg(31 downto 0);
|
130 |
|
|
END Behavioral;
|