Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.39 KB | None | 0 0
  1. library ieee;
  2. library altera_mf;
  3. use ieee.std_logic_1164.all;
  4. use ieee.std_logic_unsigned.all;
  5. use altera_mf.altera_mf_components.all;
  6. entity Pipeline is
  7. port(
  8. clock : in std_logic;
  9. reset : in std_logic;
  10. IO_DATA : out std_logic_vector(31 downto 0);
  11. IO_WRITE : out std_logic;
  12. dbg_fbufopcode : out std_logic_vector(31 downto 0);
  13. dbg_dbufopcode : out std_logic_vector(6 downto 0);
  14. dbg_ebufopcode : out std_logic_vector(6 downto 0);
  15. dbg_mbufopcode : out std_logic_vector(6 downto 0)
  16. );
  17. end Pipeline;
  18.  
  19. architecture a of Pipeline is
  20.  
  21. signal exresult : std_logic_vector(31 downto 0);
  22. signal branchmux : std_logic;
  23.  
  24. signal fetchPC : std_logic_vector(31 downto 0);
  25. signal fetchinstruction : std_logic_vector(31 downto 0);
  26.  
  27. signal fbufPC : std_logic_vector(31 downto 0);
  28. signal fbufinstruction : std_logic_vector(31 downto 0);
  29.  
  30. signal decodeopcode : std_logic_vector(6 downto 0);
  31. signal decoderd : std_logic_vector(4 downto 0);
  32. signal decoders1 : std_logic_vector(4 downto 0);
  33. signal decoders2 : std_logic_vector(4 downto 0);
  34. signal decodereadrs1 : std_logic_vector(31 downto 0);
  35. signal decodereadrs2 : std_logic_vector(31 downto 0);
  36. signal decodeimm : std_logic_vector(31 downto 0);
  37. signal decodefunct3 : std_logic_vector(2 downto 0);
  38. signal decodefunct7 : std_logic_vector(6 downto 0);
  39.  
  40. signal dbufPC : std_logic_vector(31 downto 0);
  41. signal dbufopcode : std_logic_vector(6 downto 0);
  42. signal dbufrd : std_logic_vector(4 downto 0);
  43. signal dbufreadrs1 : std_logic_vector(31 downto 0);
  44. signal dbufreadrs2 : std_logic_vector(31 downto 0);
  45. signal dbufimm : std_logic_vector(31 downto 0);
  46. signal dbuffunct3 : std_logic_vector(2 downto 0);
  47. signal dbuffunct7 : std_logic_vector(6 downto 0);
  48.  
  49.  
  50. signal ebufopcode : std_logic_vector(6 downto 0);
  51. signal ebufrd : std_logic_vector(4 downto 0);
  52. signal ebufreadrs1 : std_logic_vector(31 downto 0);
  53. signal ebufreadrs2 : std_logic_vector(31 downto 0);
  54. signal ebufimm : std_logic_vector(31 downto 0);
  55. signal ebufresult : std_logic_vector(31 downto 0);
  56. signal ebuffunct3 : std_logic_vector(2 downto 0);
  57. signal ebuffunct7 : std_logic_vector(6 downto 0);
  58.  
  59. signal memresult : std_logic_vector(31 downto 0);
  60. signal mbufopcode : std_logic_vector(6 downto 0);
  61. signal mbufrd : std_logic_vector(4 downto 0);
  62. signal mbufresult : std_logic_vector(31 downto 0);
  63.  
  64. signal wren : std_logic;
  65. signal wrdata : std_logic_vector(31 downto 0);
  66. signal wraddress : std_logic_vector(4 downto 0);
  67. begin
  68. IO_DATA <= exresult;
  69.  
  70. dbg_fbufopcode <= fbufinstruction;
  71. dbg_dbufopcode <= dbufopcode;
  72. dbg_ebufopcode <= ebufopcode;
  73. dbg_mbufopcode <= mbufopcode;
  74.  
  75. fetch : entity work.InstructionFetch
  76. PORT MAP (
  77. clock,
  78. reset,
  79. exresult,
  80. branchmux,
  81. fetchPC,
  82. fetchinstruction
  83. );
  84.  
  85. fetchbuffer : entity work.FBUF
  86. PORT MAP (
  87. clock,
  88. reset,
  89. fetchPC,
  90. fetchinstruction,
  91. fbufPC,
  92. fbufinstruction
  93. );
  94.  
  95. decode : entity work.InstructionDecode
  96. PORT MAP (
  97. fbufinstruction,
  98. decodeopcode,
  99. decoderd,
  100. decoders1,
  101. decoders2,
  102. decodeimm,
  103. decodefunct3,
  104. decodefunct7
  105. );
  106.  
  107. dbuf : entity work.DBUF
  108. PORT MAP (
  109. clock,
  110. reset,
  111. fbufPC,
  112. decodeopcode,
  113. decoderd,
  114. decodereadrs1,
  115. decodereadrs2,
  116. decodeimm,
  117. decodefunct3,
  118. decodefunct7,
  119. dbufPC,
  120. dbufopcode,
  121. dbufrd,
  122. dbufreadrs1,
  123. dbufreadrs2,
  124. dbufimm,
  125. dbuffunct3,
  126. dbuffunct7
  127. );
  128.  
  129. ex : entity work.Execute
  130. PORT MAP (
  131. dbufPC,
  132. dbufopcode,
  133. dbufrd,
  134. dbufreadrs1,
  135. dbufreadrs2,
  136. dbufimm,
  137. dbuffunct3,
  138. dbuffunct7,
  139. branchmux,
  140. exresult,
  141. IO_WRITE
  142. );
  143.  
  144. ebuf : entity work.EBUF
  145. PORT MAP (
  146. clock,
  147. reset,
  148. dbufopcode,
  149. dbufrd,
  150. dbufreadrs1,
  151. dbufreadrs2,
  152. dbufimm,
  153. dbuffunct3,
  154. dbuffunct7,
  155. exresult,
  156. ebufopcode,
  157. ebufrd,
  158. ebufreadrs1,
  159. ebufreadrs2,
  160. ebufimm,
  161. ebufresult,
  162. ebuffunct3,
  163. ebuffunct7
  164. );
  165.  
  166. mem : entity work.Memory
  167. PORT MAP (
  168. clock,
  169. reset,
  170. ebufopcode,
  171. ebuffunct3,
  172. ebufreadrs2,
  173. ebufresult,
  174. memresult
  175. );
  176.  
  177. mbuf : entity work.MBUF
  178. PORT MAP (
  179. clock,
  180. reset,
  181. ebufopcode,
  182. ebufrd,
  183. memresult,
  184. mbufopcode,
  185. mbufrd,
  186. mbufresult
  187. );
  188.  
  189. wb : entity work.Writeback
  190. PORT MAP (
  191. mbufopcode,
  192. mbufrd,
  193. mbufresult,
  194. wren,
  195. wrdata,
  196. wraddress
  197. );
  198.  
  199. regfile : entity work.DPRF
  200. PORT MAP (
  201. clock,
  202. reset,
  203. wren,
  204. decoders1,
  205. decoders2,
  206. wraddress,
  207. wrdata,
  208. decodereadrs1,
  209. decodereadrs2
  210. );
  211.  
  212. end a;
  213. library ieee;
  214. library altera_mf;
  215. use ieee.std_logic_1164.all;
  216. use ieee.std_logic_unsigned.all;
  217. use altera_mf.altera_mf_components.all;
  218. entity InstructionFetch is
  219. port(
  220. clock : in std_logic;
  221. reset : in std_logic;
  222. branch : in std_logic_vector(31 downto 0);
  223. branch_mux : in std_logic;
  224. PC : out std_logic_vector(31 downto 0);
  225. instruction : out std_logic_vector(31 downto 0)
  226. );
  227. end InstructionFetch;
  228.  
  229. architecture a of InstructionFetch is
  230.  
  231. signal program_counter : std_logic_vector(31 downto 0);
  232. signal data : std_logic_vector(31 downto 0);
  233. signal wren : std_logic;
  234. begin
  235.  
  236. altsyncram_component : altsyncram
  237. GENERIC MAP (
  238. clock_enable_input_a => "BYPASS",
  239. clock_enable_output_a => "BYPASS",
  240. init_file => "program.mif",
  241. intended_device_family => "MAX 10",
  242. lpm_hint => "ENABLE_RUNTIME_MOD=NO",
  243. lpm_type => "altsyncram",
  244. numwords_a => 1024,
  245. operation_mode => "SINGLE_PORT",
  246. outdata_aclr_a => "CLEAR0",
  247. outdata_reg_a => "UNREGISTERED",
  248. power_up_uninitialized => "FALSE",
  249. read_during_write_mode_port_a => "NEW_DATA_NO_NBE_READ",
  250. widthad_a => 10,
  251. width_a => 32,
  252. width_byteena_a => 1
  253. )
  254. PORT MAP (
  255. aclr0 => reset,
  256. address_a => program_counter(9 downto 0),
  257. clock0 => clock,
  258. data_a => data,
  259. wren_a => wren,
  260. q_a => instruction
  261. );
  262.  
  263. PC <= program_counter;
  264.  
  265. process (clock, reset)
  266. begin
  267. if (rising_edge(clock)) then
  268. if (reset = '1') then
  269. program_counter <= (others => '0');
  270. else
  271. case branch_mux is
  272. when '0' =>
  273. program_counter <= program_counter + 1;
  274. when '1' =>
  275. program_counter <= branch;
  276. end case;
  277. end if;
  278. end if;
  279. end process;
  280. end a;
  281. library ieee;
  282. use ieee.std_logic_1164.all;
  283. use ieee.std_logic_unsigned.all;
  284. entity FBUF is
  285. port(
  286. clock : in std_logic;
  287. reset : in std_logic;
  288. PCin : in std_logic_vector(31 downto 0);
  289. instructionin : in std_logic_vector(31 downto 0);
  290. PCout : out std_logic_vector(31 downto 0);
  291. instructionout : out std_logic_vector(31 downto 0)
  292. );
  293. end FBUF;
  294.  
  295. architecture a of FBUF is
  296.  
  297. signal PC : std_logic_vector(31 downto 0);
  298. signal instruction : std_logic_vector(31 downto 0);
  299. begin
  300.  
  301.  
  302. PCout <= PC;
  303. instructionout <= instruction;
  304.  
  305. process (clock, reset)
  306. begin
  307. if (rising_edge(clock)) then
  308. if (reset = '1') then
  309. PC <= (others => '0');
  310. instruction <= (others => '0');
  311. else
  312. PC <= PCin;
  313. instruction <= std_logic_vector(instructionin);
  314. end if;
  315. end if;
  316. end process;
  317. end a;
  318. library ieee;
  319. use ieee.std_logic_1164.all;
  320. use ieee.std_logic_unsigned.all;
  321. entity InstructionDecode is
  322. port(
  323. instruction : in std_logic_vector(31 downto 0);
  324. opcode : out std_logic_vector(6 downto 0);
  325. rd : out std_logic_vector(4 downto 0);
  326. rs1 : out std_logic_vector(4 downto 0);
  327. rs2 : out std_logic_vector(4 downto 0);
  328. imm : out std_logic_vector(31 downto 0);
  329. funct3 : out std_logic_vector(2 downto 0);
  330. funct7 : out std_logic_vector(6 downto 0)
  331. );
  332. end InstructionDecode;
  333.  
  334. architecture a of InstructionDecode is
  335.  
  336. begin
  337.  
  338. opcode <= instruction(6 downto 0);
  339. rd <= instruction(11 downto 7);
  340. rs1 <= instruction(19 downto 15);
  341. rs2 <= instruction(24 downto 20);
  342. funct3 <= instruction(14 downto 12);
  343. funct7 <= instruction(31 downto 25);
  344. with instruction(6 downto 0) select
  345. imm <= instruction(31 downto 12) & (11 downto 0 => '0') when "0110111",
  346. instruction(31 downto 12) & (11 downto 0 => '0') when "0010111",
  347. (11 downto 0 => instruction(31)) & instruction(19 downto 12) & instruction(20) & instruction(30 downto 21) & '0' when "1101111",
  348. (19 downto 0 => instruction(31)) & instruction(31 downto 20) when "1100111",
  349. (19 downto 0 => instruction(31)) & instruction(7) & instruction(30 downto 25) & instruction(11 downto 8) & '0' when "1100011",
  350. (19 downto 0 => instruction(31)) & instruction(31 downto 20) when "0000011",
  351. (19 downto 0 => instruction(31)) & instruction(31 downto 25) & instruction(11 downto 7) when "0100011",
  352. (19 downto 0 => instruction(31)) & instruction(31 downto 20) when "0010011",
  353. (others => '0') when others;
  354.  
  355. end a;
  356. library ieee;
  357. use ieee.std_logic_1164.all;
  358. use ieee.std_logic_unsigned.all;
  359. entity DBUF is
  360. port(
  361. clock : in std_logic;
  362. reset : in std_logic;
  363. PCin : in std_logic_vector(31 downto 0);
  364. opcodein : in std_logic_vector(6 downto 0);
  365. rdin : in std_logic_vector(4 downto 0);
  366. readrs1in : in std_logic_vector(31 downto 0);
  367. readrs2in : in std_logic_vector(31 downto 0);
  368. immin : in std_logic_vector(31 downto 0);
  369. funct3in : in std_logic_vector(2 downto 0);
  370. funct7in : in std_logic_vector(6 downto 0);
  371. PCout : out std_logic_vector(31 downto 0);
  372. opcodeout : out std_logic_vector(6 downto 0);
  373. rdout : out std_logic_vector(4 downto 0);
  374. readrs1out : out std_logic_vector(31 downto 0);
  375. readrs2out : out std_logic_vector(31 downto 0);
  376. immout : out std_logic_vector(31 downto 0);
  377. funct3out : out std_logic_vector(2 downto 0);
  378. funct7out : out std_logic_vector(6 downto 0)
  379. );
  380. end DBUF;
  381.  
  382. architecture a of DBUF is
  383.  
  384. signal PC : std_logic_vector(31 downto 0);
  385. signal opcode : std_logic_vector(6 downto 0);
  386. signal rd : std_logic_vector(4 downto 0);
  387. signal readrs1 : std_logic_vector(31 downto 0);
  388. signal readrs2 : std_logic_vector(31 downto 0);
  389. signal imm : std_logic_vector(31 downto 0);
  390. signal funct3 : std_logic_vector(2 downto 0);
  391. signal funct7 : std_logic_vector(6 downto 0);
  392.  
  393. begin
  394.  
  395. PCout <= PC;
  396. opcodeout <= opcode;
  397. rdout <= rd;
  398. readrs1out <= readrs1;
  399. readrs2out <= readrs2;
  400. immout <= imm;
  401. funct3out <= funct3;
  402. funct7out <= funct7;
  403.  
  404. process (clock, reset)
  405. begin
  406.  
  407.  
  408. if (rising_edge(clock)) then
  409. if (reset = '1') then
  410. PC <= (others => '0');
  411. opcode <= (others => '0');
  412. rd <= (others => '0');
  413. readrs1 <= (others => '0');
  414. readrs2 <= (others => '0');
  415. imm <= (others => '0');
  416. funct3 <= (others => '0');
  417. funct7 <= (others => '0');
  418. else
  419. PC <= PCin;
  420. opcode <= opcodein;
  421. rd <= rdin;
  422. readrs1 <= readrs1in;
  423. readrs2 <= readrs2in;
  424. imm <= immin;
  425. funct3 <= funct3in;
  426. funct7 <= funct7in;
  427. end if;
  428. end if;
  429. end process;
  430.  
  431. end a;
  432. library ieee;
  433. use ieee.std_logic_1164.all;
  434. use ieee.std_logic_unsigned.all;
  435. use ieee.numeric_std.all;
  436. entity Execute is
  437. port(
  438. PC : in std_logic_vector(31 downto 0);
  439. opcode : in std_logic_vector(6 downto 0);
  440. rd : in std_logic_vector(4 downto 0);
  441. readrs1,
  442. readrs2 : in std_logic_vector(31 downto 0);
  443.  
  444. imm : in std_logic_vector(31 downto 0);
  445. funct3 : in std_logic_vector(2 downto 0);
  446. funct7 : in std_logic_vector(6 downto 0);
  447. branchmux : out std_logic;
  448. result : out std_logic_vector(31 downto 0);
  449. IO_WRITE : out std_logic
  450. );
  451. end Execute;
  452.  
  453. architecture a of Execute is
  454. signal exresulti : std_logic_vector(31 downto 0);
  455. signal exresultr : std_logic_vector(31 downto 0);
  456. signal subimm : std_logic_vector(31 downto 0);
  457. signal comparisonimm : std_logic;
  458. signal subreg : std_logic_vector(31 downto 0);
  459. signal comparisonreg : std_logic;
  460. begin
  461. subimm <= readrs1 - imm;
  462. comparisonimm <= '1' when (unsigned(readrs1) < unsigned(imm)) else '0';
  463. subreg <= readrs1 - readrs2;
  464. comparisonreg <= '1' when (unsigned(readrs1) < unsigned(readrs2)) else '0';
  465. exresulti <= (readrs1 + imm) when funct3 = "000" else
  466. (30 downto 0 => '0') & subimm(31) when funct3 = "010" else
  467. (30 downto 0 => '0') & comparisonimm when funct3 = "011" else
  468. (readrs1 xor imm) when funct3 = "100" else
  469. (readrs1 or imm) when funct3 = "110" else
  470. (readrs1 and imm) when funct3 = "111" else
  471. std_logic_vector(shift_left(unsigned(readrs1), to_integer(unsigned(imm(4 downto 0))))) when funct3 = "001" else
  472. std_logic_vector(shift_right(unsigned(readrs1), to_integer(unsigned(imm(4 downto 0))))) when funct7(5) = '1' and funct3 = "101" else
  473. std_logic_vector(shift_right(signed(readrs1), to_integer(unsigned(imm(4 downto 0))))) when funct7(5) = '0' and funct3 = "101" else
  474. (others => '0');
  475.  
  476. exresultr <= (readrs1 + readrs2) when funct3 = "000" and funct7(5) = '0' else
  477. subreg when funct3 = "000" and funct7(5) = '1' else
  478. std_logic_vector(shift_left(unsigned(readrs1), to_integer(unsigned(readrs2(4 downto 0))))) when funct3 = "001" else
  479. (30 downto 0 => '0') & subreg(31) when funct3 = "010" else
  480. (30 downto 0 => '0') & comparisonreg when funct3 = "011" else
  481. (readrs1 xor readrs2) when funct3 = "100" else
  482. std_logic_vector(shift_right(unsigned(readrs1), to_integer(unsigned(readrs2)))) when funct3 = "101" and funct7(5) = '0' else
  483. std_logic_vector(shift_right(signed(readrs1), to_integer(unsigned(readrs2)))) when funct3 = "101" and funct7(5) = '1' else
  484. (readrs1 or readrs2) when funct3 = "110" else
  485. (readrs1 and readrs2) when funct3 = "111" else
  486. (others => '0');
  487. branchmux <= '1' when opcode = "1101111" or opcode = "1100111" or opcode = "1100011" else '0';
  488. IO_WRITE <= '1' when opcode = "0000001" else '0';
  489. with opcode select
  490. result <= imm when "0110111",
  491. imm + PC when "0010111",
  492. imm + PC when "1101111",
  493. readrs1 + imm when "1100111",
  494. readrs2 - readrs1 when "1100011",
  495. readrs1 + imm when "0000011",
  496. readrs1 + imm when "0100011",
  497.  
  498. exresulti when "0010011",
  499. exresultr when "0110011",
  500. readrs1 when "0000001",
  501. (others => '0') when others;
  502.  
  503. end a;
  504. library ieee;
  505. use ieee.std_logic_1164.all;
  506. use ieee.std_logic_unsigned.all;
  507. entity EBUF is
  508. port(
  509. clock : in std_logic;
  510. reset : in std_logic;
  511. opcodein : in std_logic_vector(6 downto 0);
  512. rdin : in std_logic_vector(4 downto 0);
  513. readrs1in : in std_logic_vector(31 downto 0);
  514. readrs2in : in std_logic_vector(31 downto 0);
  515. immin : in std_logic_vector(31 downto 0);
  516.  
  517. funct3in : in std_logic_vector(2 downto 0);
  518. funct7in : in std_logic_vector(6 downto 0);
  519. exresultin : in std_logic_vector(31 downto 0);
  520. opcodeout : out std_logic_vector(6 downto 0);
  521. rdout : out std_logic_vector(4 downto 0);
  522. readrs1out : out std_logic_vector(31 downto 0);
  523. readrs2out : out std_logic_vector(31 downto 0);
  524. immout : out std_logic_vector(31 downto 0);
  525. exresultout : out std_logic_vector(31 downto 0);
  526. funct3out : out std_logic_vector(2 downto 0);
  527. funct7out : out std_logic_vector(6 downto 0)
  528. );
  529. end EBUF;
  530.  
  531. architecture a of EBUF is
  532.  
  533. signal PC : std_logic_vector(31 downto 0);
  534. signal opcode : std_logic_vector(6 downto 0) := "0000000";
  535. signal rd : std_logic_vector(4 downto 0);
  536. signal readrs1 : std_logic_vector(31 downto 0);
  537. signal readrs2 : std_logic_vector(31 downto 0);
  538. signal imm : std_logic_vector(31 downto 0);
  539. signal exresult : std_logic_vector(31 downto 0);
  540. signal funct3 : std_logic_vector(2 downto 0);
  541. signal funct7 : std_logic_vector(6 downto 0);
  542.  
  543. begin
  544.  
  545. opcodeout <= opcode;
  546. rdout <= rd;
  547. readrs1out <= readrs1;
  548. readrs2out <= readrs2;
  549. immout <= imm;
  550. exresultout <= exresult;
  551. funct3out <= funct3;
  552. funct7out <= funct7;
  553.  
  554. process (clock, reset)
  555. begin
  556.  
  557.  
  558. if (rising_edge(clock)) then
  559. if (reset = '1') then
  560. opcode <= (others => '0');
  561. rd <= (others => '0');
  562. readrs1 <= (others => '0');
  563. readrs2 <= (others => '0');
  564. imm <= (others => '0');
  565. exresult <= (others => '0');
  566. funct3 <= (others => '0');
  567. funct7 <= (others => '0');
  568. else
  569. opcode <= opcodein;
  570. rd <= rdin;
  571. readrs1 <= readrs1in;
  572. readrs2 <= readrs2in;
  573. imm <= immin;
  574. exresult <= exresultin;
  575. funct3 <= funct3in;
  576. funct7 <= funct7in;
  577. end if;
  578. end if;
  579. end process;
  580.  
  581. end a;
  582. library ieee;
  583. library altera_mf;
  584. use ieee.std_logic_1164.all;
  585. use ieee.std_logic_unsigned.all;
  586. use altera_mf.altera_mf_components.all;
  587. entity Memory is
  588. port(
  589. clock : in std_logic;
  590. reset : in std_logic;
  591. opcode : in std_logic_vector(6 downto 0);
  592. funct3 : in std_logic_vector(2 downto 0);
  593. readrs2 : in std_logic_vector(31 downto 0);
  594. ex_result : in std_logic_vector(31 downto 0);
  595. mem_result : out std_logic_vector(31 downto 0)
  596. );
  597. end Memory;
  598.  
  599. architecture a of Memory is
  600.  
  601. signal read_data : std_logic_vector(31 downto 0);
  602. signal mem_out : std_logic_vector(31 downto 0);
  603. signal byteena : std_logic_vector(3 downto 0);
  604. signal wren : std_logic;
  605. begin
  606.  
  607.  
  608. altsyncram_component : altsyncram
  609. GENERIC MAP (
  610. byte_size => 8,
  611. clock_enable_input_a => "BYPASS",
  612. clock_enable_output_a => "BYPASS",
  613. init_file => "memory.mif",
  614. intended_device_family => "MAX 10",
  615. lpm_hint => "ENABLE_RUNTIME_MOD=NO",
  616. lpm_type => "altsyncram",
  617. numwords_a => 1024,
  618. operation_mode => "SINGLE_PORT",
  619. outdata_aclr_a => "CLEAR0",
  620. outdata_reg_a => "UNREGISTERED",
  621. power_up_uninitialized => "FALSE",
  622. read_during_write_mode_port_a => "OLD_DATA",
  623. widthad_a => 10,
  624. width_a => 32,
  625. width_byteena_a => 4
  626. )
  627. PORT MAP (
  628. aclr0 => reset,
  629. address_a => ex_result(9 downto 0),
  630. byteena_a => byteena,
  631. clock0 => clock,
  632. data_a => readrs2,
  633. wren_a => wren,
  634. q_a => read_data
  635. );
  636. wren <= '1' when opcode = "0100011" else '0';
  637.  
  638. byteena <= "0001" when funct3(1 downto 0) = "00" else
  639. "0011" when funct3(1 downto 0) = "01" else
  640. "1111" when funct3(1 downto 0) = "10" else
  641. "0000";
  642.  
  643. mem_out <= (31 downto 8 => read_data(7) and (not funct3(2))) & read_data(7 downto 0) when funct3(1 downto 0) = "00" else
  644. (31 downto 16 => read_data(7) and (not funct3(2))) & read_data(15 downto 0) when funct3(1 downto 0) = "01" else
  645. read_data;
  646.  
  647. mem_result <= mem_out when opcode = "0000011" else
  648. ex_result;
  649. end a;
  650. library ieee;
  651. use ieee.std_logic_1164.all;
  652. use ieee.std_logic_unsigned.all;
  653. entity MBUF is
  654. port(
  655. clock : in std_logic;
  656. reset : in std_logic;
  657. opcodein : in std_logic_vector(6 downto 0);
  658. rdin : in std_logic_vector(4 downto 0);
  659. memresultin : in std_logic_vector(31 downto 0);
  660. opcodeout : out std_logic_vector(6 downto 0);
  661. rdout : out std_logic_vector(4 downto 0);
  662. memresultout : out std_logic_vector(31 downto 0)
  663. );
  664. end MBUF;
  665.  
  666. architecture a of MBUF is
  667.  
  668. signal opcode : std_logic_vector(6 downto 0);
  669. signal rd : std_logic_vector(4 downto 0);
  670. signal memresult : std_logic_vector(31 downto 0);
  671.  
  672. begin
  673.  
  674. opcodeout <= opcode;
  675. rdout <= rd;
  676. memresultout <= memresult;
  677.  
  678. process (clock, reset)
  679. begin
  680. if (rising_edge(clock)) then
  681. if (reset = '1') then
  682. opcode <= (others => '0');
  683. rd <= (others => '0');
  684. memresult <= (others => '0');
  685. else
  686. opcode <= opcodein;
  687. rd <= rdin;
  688. memresult <= memresultin;
  689. end if;
  690. end if;
  691. end process;
  692.  
  693. end a;
  694. library ieee;
  695. use ieee.std_logic_1164.all;
  696. use ieee.std_logic_unsigned.all;
  697. entity Writeback is
  698. port(
  699. opcode : in std_logic_vector(6 downto 0);
  700. rd : in std_logic_vector(4 downto 0);
  701.  
  702. mem_result : in std_logic_vector(31 downto 0);
  703. wren : out std_logic;
  704. wrdata : out std_logic_vector(31 downto 0);
  705. wraddress : out std_logic_vector(4 downto 0)
  706. );
  707. end Writeback;
  708.  
  709. architecture a of Writeback is
  710.  
  711. begin
  712.  
  713. wrdata <= mem_result;
  714. wraddress <= rd;
  715. wren <= '1' when opcode = "0110111" or
  716. opcode = "0010111" or
  717. opcode = "1101111" or
  718. opcode = "1100111" or
  719. opcode = "0000011" or
  720. opcode = "0010011" or
  721. opcode = "0110011" else
  722. '0';
  723.  
  724.  
  725. end a;
  726. library ieee;
  727. use ieee.std_logic_1164.all;
  728. use ieee.std_logic_unsigned.all;
  729. use ieee.numeric_std.all;
  730. entity DPRF is
  731. port(
  732. clock : in std_logic;
  733. reset : in std_logic;
  734. wren : in std_logic;
  735. rs1 : in std_logic_vector(4 downto 0);
  736. rs2 : in std_logic_vector(4 downto 0);
  737. wraddress : in std_logic_vector(4 downto 0);
  738. wrdata : in std_logic_vector(31 downto 0);
  739. readrs1 : out std_logic_vector(31 downto 0);
  740. readrs2 : out std_logic_vector(31 downto 0)
  741. );
  742. end DPRF;
  743.  
  744. architecture a of DPRF is
  745.  
  746. type RegisterFile is array (31 downto 0) of std_logic_vector(31 downto 0);
  747. signal registers : RegisterFile;
  748. begin
  749. readrs1 <= registers(to_integer(unsigned(rs1)));
  750. readrs2 <= registers(to_integer(unsigned(rs2)));
  751.  
  752. process (clock, reset)
  753. begin
  754. if (reset = '1') then
  755. for i in registers'range loop
  756. registers(i) <= (others => '0');
  757. end loop;
  758. elsif (rising_edge(clock) and wren = '1' and wraddress /= "00000") then
  759. registers(to_integer(unsigned(wraddress))) <= wrdata;
  760. end if;
  761. end process;
  762.  
  763. end a;
  764.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement