Advertisement
Guest User

Untitled

a guest
May 27th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.05 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3.  
  4. entity system_core is
  5. port(
  6. num : in std_logic_vector(7 downto 0); --ulazni broj koji ucitavamo (prvo je a a onda b)
  7. next_num : in std_logic;
  8. plus : in std_logic;
  9. minus : in std_logic;
  10. clk : in std_logic;
  11. reset : in std_logic; -- aktivan na logicku nulu
  12. display_num : out std_logic_vector(7 downto 0) --izlazni broj koji svetli
  13. );
  14. end system_core;
  15.  
  16. architecture system_core_behav of system_core is
  17.  
  18. type state_type is (init, read_a, read_b, add, sub); --stanja
  19. signal state_reg, next_state : state_type;
  20.  
  21.  
  22. signal timeout_cnt : integer range 0 to 20 :=0; --indeks brojaca koji broji kolko jos treba cekati
  23. signal isteklo : std_logic := '0'; -- true/false dal je vreme isteklo. znaci uporedjujemo brojace sa 10 pa ako je jednako onda je isteklo='1' pa se deava nesto
  24.  
  25.  
  26.  
  27. signal a : std_logic_vector(7 downto 0); -- ko sto smo imali sumu u automatu tako i ovde neki signal u koji pamtimo
  28. signal b : std_logic_vector(7 downto 0);
  29.  
  30.  
  31. signal sel : std_logic; -- ovaj ce da sluzi za biranje oduz/sab jer nas add_sub_4b se podesava sa sel (1 signal) a ovde imamo 2 pa treba to regulisati
  32.  
  33.  
  34. signal result : std_logic_vector(7 downto 0); --ovde se pamti rezultat pa se stavi na izlaz
  35.  
  36. component add_sub_4bit is
  37. port
  38. (
  39. a : in std_logic_vector(7 downto 0);
  40. b : in std_logic_vector(7 downto 0);
  41. sel : in std_logic; -- ADD if '0', SUB if '1'
  42.  
  43. s : out std_logic_vector(7 downto 0)
  44. );
  45. end component;
  46.  
  47. begin
  48.  
  49.  
  50. --sekv kako se menjaju stanja
  51. state_transition : process (clk, reset)
  52. begin
  53.  
  54. if (reset = '0') then
  55. state_reg <= init;
  56. elsif (rising_edge(clk)) then
  57. state_reg <= next_state;
  58. end if;
  59. end process state_transition;
  60.  
  61.  
  62.  
  63.  
  64.  
  65. --komb kakav je izlaz u kom stanju
  66. display_logic : process (state_reg, num, result)
  67. begin
  68. if (state_reg = init) then
  69. display_num <= "00000000";
  70. elsif (state_reg = read_a) then
  71. display_num <= num;
  72. elsif (state_reg = read_b) then
  73. display_num <= num;
  74. else
  75. display_num <= result; --u result u zavisnosti od stanja stavljamo sab/oduz pa on ovde ni ne razmislja o tome sta je to
  76. end if;
  77. end process display_logic;
  78.  
  79.  
  80.  
  81. -- komb odredjuje next_state na osnovu prethodnog stanja i ulaza
  82.  
  83. next_state_logic : process (state_reg, next_num, isteklo, plus, minus)
  84. begin
  85. next_state <= state_reg;
  86. case (state_reg) is
  87. when init =>
  88. if next_num = '1' then
  89. next_state <= read_a; --inace je next_state <= state_reg; zato to pisemo na pocetku
  90. end if;
  91. when read_a =>
  92. if next_num = '1' then
  93. next_state <= read_b;
  94. end if;
  95. when read_b =>
  96. if plus = '1' then
  97. next_state <= add; --za svaki if je next_state <= state_reg; else grana!!
  98. elsif minus ='1' then
  99. next_state <= sub;
  100. end if;
  101. when add =>
  102. if isteklo = '1' then
  103. next_state <= init;
  104. end if;
  105. when sub =>
  106. if isteklo = '1' then
  107. next_state <= init;
  108. end if;
  109. end case;
  110. end process next_state_logic;
  111.  
  112.  
  113.  
  114.  
  115. --sekv upravlja add_sub_4bit ,postavlja sel u zavisnosti od koje je next_state
  116. sel_logic : process(clk, reset)
  117. begin
  118. if (reset = '0') then
  119. sel <= '0';
  120. elsif (rising_edge(clk)) then
  121. if (next_state = add) then
  122. sel <= '0';
  123. elsif (next_state = sub) then
  124. sel <= '1';
  125. end if;
  126. end if;
  127. end process sel_logic;
  128.  
  129.  
  130.  
  131.  
  132.  
  133. --sekv ucitavanjec pomocnih signala a i b u zavisnosti od clk i sta je num u read_a i read_b
  134.  
  135. load_a_b : process(clk, reset)
  136. begin
  137. if (reset = '0') then
  138. a <= "00000000";
  139. b <= "00000000";
  140. elsif (rising_edge(clk)) then
  141. if (state_reg = read_a and next_state = read_b) then --znaci da je pre uzlazne ivice clk , next_num vec potao 1 , ako nije ceka se na to pre promene
  142. a <= num;
  143. elsif (state_reg = read_b and (next_state = add or next_state = sub)) then
  144. b <= num;
  145. end if;
  146. end if;
  147. end process load_a_b;
  148.  
  149.  
  150.  
  151.  
  152. --definisi preslikavanje ulaza i izlaza
  153.  
  154. ADDSUB4B : add_sub_4bit port map (
  155. a => a,
  156. b => b,
  157. sel => sel,
  158. s=> result
  159. );
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. --sekv brojac povecava timeout_cnt dok smo u add ili sub za svaki takt, kada predjemo u init brojanje je resetuje
  168. --u sustini brojac broji koliko smo taktova ostali u stanju koje prikazuje na izlazu rezultat
  169. cnt_process: process(state_reg, clk)
  170. variable cnt : integer range 0 to 20;
  171.  
  172. begin
  173. if (state_reg = init) then
  174. cnt := 0;
  175. elsif (state_reg = add or state_reg = sub) then
  176. if(rising_edge(clk)) then
  177. cnt := cnt + 1;
  178. end if;
  179. end if;
  180. timeout_cnt <= cnt;
  181. end process cnt_process;
  182.  
  183.  
  184.  
  185.  
  186. --komb postavljanje u zavisnosti od timeout_cnt dal je isteklo='1'
  187. isteci: process(timeout_cnt)
  188. begin
  189. if (timeout_cnt=10) then
  190. isteklo <= '1';
  191. else
  192. isteklo <= '0';
  193. end if;
  194. end process isteci;
  195.  
  196.  
  197.  
  198.  
  199.  
  200. end system_core_behav;
  201.  
  202.  
  203.  
  204.  
  205. test bench:
  206.  
  207.  
  208.  
  209. LIBRARY ieee;
  210. USE ieee.std_logic_1164.all;
  211.  
  212. ENTITY system_core_vhd_tst IS
  213. END system_core_vhd_tst;
  214. ARCHITECTURE system_core_arch OF system_core_vhd_tst IS
  215. -- constants
  216. constant clk_period: time :=20 ns;
  217. -- signals
  218. SIGNAL clk : STD_LOGIC;
  219. SIGNAL display_num : STD_LOGIC_VECTOR(7 DOWNTO 0);
  220. SIGNAL minus : STD_LOGIC;
  221. SIGNAL next_num : STD_LOGIC;
  222. SIGNAL num : STD_LOGIC_VECTOR(7 DOWNTO 0);
  223. SIGNAL plus : STD_LOGIC;
  224. SIGNAL reset : STD_LOGIC;
  225. COMPONENT system_core
  226. PORT (
  227. clk : IN STD_LOGIC:='0';
  228. display_num : OUT STD_LOGIC_VECTOR(7 DOWNTO 0);
  229. minus : IN STD_LOGIC:='0';
  230. next_num : IN STD_LOGIC:='0';
  231. num : IN STD_LOGIC_VECTOR(7 DOWNTO 0):="00000000";
  232. plus : IN STD_LOGIC:='0';
  233. reset : IN STD_LOGIC:='1'
  234. );
  235. END COMPONENT;
  236. BEGIN
  237. i1 : system_core
  238. PORT MAP (
  239. -- list connections between master ports and signals
  240. clk => clk,
  241. display_num => display_num,
  242. minus => minus,
  243. next_num => next_num,
  244. num => num,
  245. plus => plus,
  246. reset => reset
  247. );
  248. clk_gen : PROCESS
  249. -- variable declarations
  250. BEGIN
  251. wait for clk_period/2;
  252. clk<='0';
  253. wait for clk_period/2;
  254. clk<='1';
  255.  
  256.  
  257. END PROCESS clk_gen;
  258. always : PROCESS
  259.  
  260. BEGIN
  261.  
  262.  
  263. minus<='0';
  264. plus<='0';
  265. next_num<='0';
  266.  
  267. num<="10000001";
  268. reset<='0';
  269. wait for clk_period*3;
  270. reset<='1';
  271. next_num<='1';
  272.  
  273. wait for clk_period;
  274.  
  275. next_num<='0';
  276.  
  277. wait for 3*clk_period;
  278.  
  279. num<="00000001";
  280. next_num<='1';
  281. wait for clk_period;
  282.  
  283. next_num<='0';
  284.  
  285. wait for 2*clk_period;
  286.  
  287. plus<='1';
  288.  
  289. wait for clk_period;
  290.  
  291. plus<='0';
  292.  
  293.  
  294.  
  295.  
  296. WAIT;
  297. END PROCESS always;
  298. END system_core_arch;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement