Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use ieee.numeric_std_unsigned.all ;
  5.  
  6. package ostate_pkg is
  7. subtype mode_ty is std_logic_vector(1 downto 0);
  8. constant o_idle: mode_ty := "10";
  9. constant o_busy: mode_ty := "11";
  10. constant o_reset: mode_ty := "01";
  11.  
  12. subtype state_ty is std_logic_vector(1 downto 0);
  13. constant resetState : state_ty := "00";
  14. constant firstFill : state_ty := "01";
  15. constant fetchPixel : state_ty := "10";
  16. constant result : state_ty := "11";
  17.  
  18. subtype cal_state_ty is std_logic_vector(2 downto 0);
  19. constant cycle_00 : cal_state_ty := "000";
  20. constant cycle_01 : cal_state_ty := "001";
  21. constant cycle_02 : cal_state_ty := "010";
  22. constant cycle_03 : cal_state_ty := "011";
  23. constant cycle_04 : cal_state_ty := "100";
  24. constant cycle_05 : cal_state_ty := "101";
  25. constant cycle_06 : cal_state_ty := "110";
  26. constant cycle_07 : cal_state_ty := "111";
  27. end ostate_pkg;
  28.  
  29. library ieee;
  30. use ieee.std_logic_1164.all;
  31. use ieee.numeric_std.all;
  32.  
  33. use work.util.all;
  34. use work.kirsch_synth_pkg.all;
  35. use work.ostate_pkg.all;
  36. use ieee.numeric_std_unsigned.all;
  37.  
  38. entity kirsch is
  39. port (
  40. clk : in std_logic;
  41. reset : in std_logic;
  42. i_valid : in std_logic;
  43. i_pixel : in unsigned(7 downto 0);
  44. o_valid : out std_logic;
  45. o_edge : out std_logic;
  46. o_dir : out std_logic_vector(2 downto 0);
  47. o_mode : out work.ostate_pkg.mode_ty;
  48. o_row : out unsigned(7 downto 0);
  49. o_col : out unsigned(7 downto 0)
  50. );
  51. end kirsch;
  52.  
  53.  
  54. architecture main of kirsch is
  55. -- mem enetity port signal
  56. signal row0_read, row1_read, row2_read : std_logic_vector(7 downto 0);
  57. signal row_wr_en : unsigned(2 downto 0) := "100";
  58.  
  59. -- intermediate signal
  60. signal state : state_ty := resetState;
  61. signal cycle : cal_state_ty := cycle_00;
  62.  
  63. signal col_index, row_index : unsigned(7 downto 0) := "00000000";
  64.  
  65. signal rdy_calc : std_logic := '0';
  66. signal first_process : std_logic := '1';
  67.  
  68. signal ra, rb, rc, rd, re, rf, rg, rh, ri : unsigned(7 downto 0) := "00000000";
  69.  
  70. signal last_pixel : std_logic;
  71.  
  72. signal da : std_logic_vector(2 downto 0);
  73. signal db : std_logic_vector(2 downto 0);
  74. signal dc : std_logic_vector(2 downto 0);
  75. signal dd : std_logic_vector(2 downto 0);
  76. signal de : std_logic_vector(2 downto 0);
  77.  
  78. signal r0 : unsigned(7 downto 0) := "00000000";
  79. signal r1 : unsigned(8 downto 0) := "000000000";
  80. signal r2 : unsigned(9 downto 0) := "0000000000";
  81. signal r3 : unsigned(9 downto 0) := "0000000000";
  82. signal r4_a : unsigned(12 downto 0):= "0000000000000";
  83. signal r4_b : unsigned(12 downto 0):= "0000000000000";
  84.  
  85. signal r_out : signed(14 downto 0):= "000000000000000";
  86.  
  87. begin
  88. row0: entity WORK.mem
  89. port map (
  90. address => col_index,
  91. clock => clk,
  92. data => std_logic_vector(i_pixel),
  93. wren => row_wr_en(0),
  94. q => row0_read
  95. );
  96.  
  97. row1: entity WORK.mem
  98. port map (
  99. address => col_index,
  100. clock => clk,
  101. data => std_logic_vector(i_pixel),
  102. wren => row_wr_en(1),
  103. q => row1_read
  104. );
  105.  
  106. row2: entity WORK.mem
  107. port map (
  108. address => col_index,
  109. clock => clk,
  110. data => std_logic_vector(i_pixel),
  111. wren => row_wr_en(2),
  112. q => row2_read
  113. );
  114. process
  115. begin
  116. wait until rising_edge(clk);
  117. if (reset = '1') then
  118. -- this is in reset for both the mode and our internal statemachine
  119. state <= resetState;
  120. o_mode <= o_reset;
  121. last_pixel <= '0';
  122.  
  123. col_index <= to_unsigned(0, 8);
  124. row_index <= to_unsigned(0, 8);
  125.  
  126. row_wr_en <= to_unsigned(1, 3);
  127.  
  128. rdy_calc <= '0';
  129.  
  130. else
  131. case state is
  132. when resetState =>
  133. o_mode <= o_idle;
  134. rdy_calc <= '0';
  135. if (i_valid = '1') then
  136. state <= firstFill;
  137. col_index <= to_unsigned(1,8);
  138. o_col <= to_unsigned(1,8);
  139. end if;
  140.  
  141. -- need to fill up to at least the first 2 row
  142. when firstFill =>
  143. o_mode <= o_busy;
  144. rdy_calc <= '0';
  145.  
  146. if (i_valid = '1') then
  147. col_index <= col_index + 1;
  148. o_col <= col_index + 1;
  149.  
  150. -- at the end of row
  151. if (col_index = to_unsigned(255, 8)) then
  152. row_wr_en <= row_wr_en rol 1;
  153. row_index <= row_index + to_unsigned(1, 8);
  154. o_row <= row_index + to_unsigned(1, 8);
  155.  
  156. if (row_index = to_unsigned(1, 8)) then
  157. state <= fetchPixel;
  158. end if;
  159. end if;
  160. end if;
  161.  
  162. -- wait for this to fill the column 0 and then start calculating
  163. when fetchPixel =>
  164. -- finished filling up column 0
  165. if (i_valid = '1') then
  166. col_index <= col_index + 1;
  167. o_col <= col_index + 1;
  168. rdy_calc <= '1';
  169.  
  170. -- at the end of row
  171. if (col_index = to_unsigned(255, 8)) then
  172. row_wr_en <= row_wr_en rol 1;
  173. row_index <= row_index + 1;
  174. o_row <= row_index + 1;
  175. end if;
  176. -- if we are filling column 0/1, do not start calculation
  177. if (col_index = to_unsigned(0, 8)) then
  178. rg <= unsigned(i_pixel);
  179. rdy_calc <= '0';
  180. elsif (col_index = to_unsigned(1, 8)) then
  181. rf <= unsigned(i_pixel);
  182. rdy_calc <= '0';
  183. elsif (col_index = to_unsigned(2, 8)) then
  184. re <= unsigned(i_pixel);
  185. else
  186. ra <= rb;
  187. rb <= rc;
  188. rh <= ri;
  189. ri <= rd;
  190. rg <= rf;
  191. rf <= re;
  192. re <= unsigned(i_pixel);
  193. if (col_index = to_unsigned(255,8) and row_index = to_unsigned(255,8)) then
  194. last_pixel <= '1';
  195. state <= result;
  196. end if;
  197. end if;
  198.  
  199. case row_wr_en is
  200. when to_unsigned(4, 3) =>
  201. -- currently writing row 2
  202. -- row0 a b c
  203. -- row1 h i d
  204. -- row2 g f e
  205. if (col_index = to_unsigned(0, 8)) then
  206. rh <= unsigned(row1_read);
  207. ra <= unsigned(row0_read);
  208. elsif (col_index = to_unsigned(1, 8)) then
  209. ri <= unsigned(row1_read);
  210. rb <= unsigned(row0_read);
  211. else
  212. rc <= unsigned(row0_read);
  213. rd <= unsigned(row1_read);
  214. end if;
  215.  
  216. when to_unsigned(2, 3) =>
  217. -- currently writing row 1
  218. -- row0 h i d
  219. -- row1 g f e
  220. -- row2 a b c
  221. if (col_index = to_unsigned(0, 8)) then
  222. rh <= unsigned(row0_read);
  223. ra <= unsigned(row2_read);
  224. elsif (col_index = to_unsigned(1, 8)) then
  225. ri <= unsigned(row0_read);
  226. rb <= unsigned(row2_read);
  227. else
  228. rc <= unsigned(row2_read);
  229. rd <= unsigned(row0_read);
  230. end if;
  231.  
  232. when others => -- when 1 =>
  233. -- currently writing row 0
  234. -- row0 g f e
  235. -- row1 a b c
  236. -- row2 h i d
  237. if (col_index = to_unsigned(0, 8)) then
  238. ra <= unsigned(row1_read);
  239. rh <= unsigned(row2_read);
  240. elsif (col_index = to_unsigned(1, 8)) then
  241. rb <= unsigned(row1_read);
  242. ri <= unsigned(row2_read);
  243. else
  244. rc <= unsigned(row1_read);
  245. rd <= unsigned(row2_read);
  246. end if;
  247. end case;
  248. end if;
  249.  
  250. when others =>
  251. if (cycle = cycle_03) then
  252. last_pixel <= '0';
  253. o_mode <= o_idle;
  254. state <= resetState;
  255. rdy_calc <= '0';
  256. end if;
  257. end case;
  258. end if;
  259. end process;
  260.  
  261. process
  262. begin
  263. wait until rising_edge(clk);
  264. if (reset = '1') then
  265. first_process <= '1';
  266.  
  267. else
  268.  
  269. if (rdy_calc) then
  270. o_valid <= '0';
  271. o_edge <= '0';
  272. o_dir <= "000";
  273.  
  274. case cycle is
  275. when cycle_00 =>
  276. cycle <= cycle_01;
  277. if rg >= rb then
  278. r0 <= rg;
  279. da <= "001"; --W
  280. else
  281. r0 <= rb;
  282. da <= "100"; --NW
  283. end if;
  284.  
  285. if r3 < r2 then
  286. r3 <= r2;
  287. de <= dc;
  288. end if;
  289.  
  290. r1 <= "0"&ra + rh;
  291. r2 <= "00"&r0 + r1;
  292. r4_a <= (others => '0');
  293. r4_b <= "0000"&r1 + r4_b;
  294.  
  295. when cycle_01 =>
  296. cycle <= cycle_02;
  297.  
  298. if ra >= rd then
  299. r0 <= ra;
  300. db <= "010"; --N
  301. else
  302. r0 <= rd;
  303. db <= "110"; --NE
  304. end if;
  305.  
  306. if r3 < r2 then
  307. r3 <= r2;
  308. de <= dd;
  309. end if;
  310.  
  311. r1 <= "0"&rb + rc;
  312. r2 <= "00"&r0 + r1;
  313. r4_b <= r4_b;
  314. r4_a <= "0000"&r1 + r4_a;
  315.  
  316. r_out <= signed(unsigned(r4_b&"00")) - signed(unsigned(r4_b));
  317.  
  318. when cycle_02 =>
  319. cycle <= cycle_03;
  320.  
  321. if rc >= rf then
  322. r0 <= rc;
  323. dc <= "000"; --E
  324. else
  325. r0 <= rf;
  326. dc <= "101"; --SE
  327. end if;
  328.  
  329. r3 <= r2;
  330. de <= da;
  331.  
  332. r1 <= "0"&re + rd;
  333. r2 <= "00"&r0 + r1;
  334. r4_a <= "0000"&r1 + r4_a;
  335.  
  336. -- output signal for "red" set
  337. if (first_process = '1') then
  338. first_process <= '0';
  339. else
  340. o_valid <= '1';
  341. if (signed(unsigned("0"&r3&"000")) - r_out) > to_signed(383, 13) then
  342. o_edge <= '1';
  343. o_dir <= de;
  344. else
  345. o_edge <= '0';
  346. o_dir <= "000";
  347. end if;
  348.  
  349. end if;
  350.  
  351. when cycle_03 =>
  352. if (i_valid) then
  353. cycle <= cycle_04;
  354.  
  355. if re >= rh then
  356. r0 <= re;
  357. dd <= "011"; --S
  358. else
  359. r0 <= rh;
  360. dd <= "111"; --SW
  361. end if;
  362.  
  363. if r3 < r2 then
  364. r3 <= r2;
  365. de <= db;
  366. end if;
  367.  
  368. r1 <= "0"&rf + rg;
  369. r2 <= "00"&r0 + r1;
  370. r4_b <= (others => '0');
  371. r4_a <= "0000"&r1 + r4_a;
  372. end if;
  373. if (last_pixel) then
  374. cycle <= cycle_00;
  375. end if;
  376.  
  377. when cycle_04 =>
  378. cycle <= cycle_05;
  379.  
  380. if rg >= rb then
  381. r0 <= rg;
  382. da <= "001"; --W
  383. else
  384. r0 <= rb;
  385. da <= "100"; --NW
  386. end if;
  387.  
  388. if r3 < r2 then
  389. r3 <= r2;
  390. de <= dc;
  391. end if;
  392.  
  393. r1 <= "0"&ra + rh;
  394. r2 <= "00"&r0 + r1;
  395. r4_b <= (others => '0');
  396. r4_a <= "0000"&r1 + r4_a;
  397.  
  398. when cycle_05 =>
  399. cycle <= cycle_06;
  400.  
  401. if ra >= rd then
  402. r0 <= ra;
  403. db <= "010"; --N
  404. else
  405. r0 <= rd;
  406. db <= "110"; --NE
  407. end if;
  408.  
  409. if r3 < r2 then
  410. r3 <= r2;
  411. de <= dd;
  412. end if;
  413.  
  414. r1 <= "0"&rb + rc;
  415. r2 <= "00"&r0 + r1;
  416. r4_a <= r4_a;
  417. r4_b <= "0000"&r1 + r4_b;
  418.  
  419. r_out <= signed(unsigned(r4_a&"00")) - signed(unsigned(r4_a));
  420.  
  421. when cycle_06 =>
  422. cycle <= cycle_07;
  423.  
  424. if rc >= rf then
  425. r0 <= rc;
  426. dc <= "000"; --E
  427. else
  428. r0 <= rf;
  429. dc <= "101"; --SE
  430. end if;
  431. r3 <= r2;
  432. de <= da;
  433.  
  434. r1 <= "0"&re + rd;
  435. r2 <= "00"&r0 + r1;
  436. r4_b <= "0000"&r1 + r4_b;
  437.  
  438. -- output signal for "black" set
  439. o_valid <= '1';
  440. if (signed(unsigned("0"&r3&"000")) - r_out) > to_signed(383, 13) then
  441. o_edge <= '1';
  442. o_dir <= de;
  443. else
  444. o_edge <= '0';
  445. o_dir <= "000";
  446. end if;
  447.  
  448. when others =>
  449. if (i_valid or last_pixel) then
  450. cycle <= cycle_00;
  451.  
  452. if re >= rh then
  453. r0 <= re;
  454. dd <= "011"; --S
  455. else
  456. r0 <= rh;
  457. dd <= "111"; --SW
  458. end if;
  459.  
  460. if r3 < r2 then
  461. r3 <= r2;
  462. de <= db;
  463. end if;
  464.  
  465. r1 <= "0"&rf + rg;
  466. r2 <= "00"&r0 + r1;
  467. r4_a <= (others => '0');
  468. r4_b <= "0000"&r1 + r4_b;
  469. end if;
  470. end case;
  471. end if;
  472. end if;
  473. end process;
  474.  
  475. end architecture main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement