decode.vhd 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use work.core_pkg.all;
  5. use work.op_pkg.all;
  6. entity decode is
  7. port (
  8. clk : in std_logic;
  9. res_n : in std_logic;
  10. stall : in std_logic;
  11. flush : in std_logic;
  12. -- from fetch
  13. pc_in : in pc_type;
  14. instr : in instr_type;
  15. -- from writeback
  16. reg_write : in reg_write_type;
  17. -- towards next stages
  18. pc_out : out pc_type;
  19. exec_op : out exec_op_type;
  20. mem_op : out mem_op_type;
  21. wb_op : out wb_op_type;
  22. -- exceptions
  23. exc_dec : out std_logic
  24. );
  25. end entity;
  26. architecture rtl of decode is
  27. subtype opcode_range_t is natural range 6 downto 0;
  28. constant OPC_LOAD : std_logic_vector(opcode_range_t) := "0000011";
  29. constant OPC_STORE : std_logic_vector(opcode_range_t) := "0100011";
  30. constant OPC_BRANCH : std_logic_vector(opcode_range_t) := "1100011";
  31. constant OPC_JALR : std_logic_vector(opcode_range_t) := "1100111";
  32. constant OPC_JAL : std_logic_vector(opcode_range_t) := "1101111";
  33. constant OPC_OP_IMM : std_logic_vector(opcode_range_t) := "0010011";
  34. constant OPC_OP : std_logic_vector(opcode_range_t) := "0110011";
  35. constant OPC_AUIPC : std_logic_vector(opcode_range_t) := "0010111";
  36. constant OPC_LUI : std_logic_vector(opcode_range_t) := "0110111";
  37. constant OPC_NOP : std_logic_vector(opcode_range_t) := "0001111";
  38. type internal_t is record
  39. pc : pc_type;
  40. instr : instr_type;
  41. end record;
  42. constant INSTR_NOP : instr_type := X"0000000F";
  43. constant INITIAL_INTERNAL : internal_t := (
  44. pc => (others => '0'),
  45. instr => INSTR_NOP
  46. );
  47. signal internal : internal_t;
  48. type reg_t is record
  49. -- regfile rd signals
  50. rdaddr1, rdaddr2 : reg_adr_type;
  51. rddata1, rddata2 : data_type;
  52. -- regfile wr signals
  53. wraddr : reg_adr_type;
  54. wrdata : data_type;
  55. regwrite : std_logic;
  56. end record;
  57. signal reg : reg_t;
  58. -- function helper range types
  59. subtype funct3_R_fmt_range_t is natural range 14 downto 12;
  60. subtype funct3_I_fmt_range_t is natural range 14 downto 12;
  61. subtype funct3_S_fmt_range_t is natural range 14 downto 12;
  62. subtype funct3_B_fmt_range_t is natural range 14 downto 12;
  63. subtype funct7_R_fmt_range_t is natural range 31 downto 25;
  64. -- functions
  65. subtype r_funct_range_t is natural range 3 + 7 - 1 downto 0;
  66. constant FUNCT_ADD : std_logic_vector(r_funct_range_t) := "000" & "0000000";
  67. constant FUNCT_SUB : std_logic_vector(r_funct_range_t) := "000" & "0100000";
  68. constant FUNCT_SLL : std_logic_vector(r_funct_range_t) := "001" & "0000000";
  69. constant FUNCT_SLT : std_logic_vector(r_funct_range_t) := "010" & "0000000";
  70. constant FUNCT_SLTU : std_logic_vector(r_funct_range_t) := "011" & "0000000";
  71. constant FUNCT_XOR : std_logic_vector(r_funct_range_t) := "100" & "0000000";
  72. constant FUNCT_SRL : std_logic_vector(r_funct_range_t) := "101" & "0000000";
  73. constant FUNCT_SRA : std_logic_vector(r_funct_range_t) := "101" & "0100000";
  74. constant FUNCT_OR : std_logic_vector(r_funct_range_t) := "110" & "0000000";
  75. constant FUNCT_AND : std_logic_vector(r_funct_range_t) := "111" & "0000000";
  76. -- immedate functions
  77. subtype i_funct_range_t is natural range 3 - 1 downto 0;
  78. constant FUNCT_ADDI : std_logic_vector(i_funct_range_t) := "000";
  79. constant FUNCT_SLLI : std_logic_vector(i_funct_range_t) := "001";
  80. constant FUNCT_SLTI : std_logic_vector(i_funct_range_t) := "010";
  81. constant FUNCT_SLTIU : std_logic_vector(i_funct_range_t) := "011";
  82. constant FUNCT_XORI : std_logic_vector(i_funct_range_t) := "100";
  83. constant FUNCT_SRI : std_logic_vector(i_funct_range_t) := "101";
  84. constant FUNCT_ORI : std_logic_vector(i_funct_range_t) := "110";
  85. constant FUNCT_ANDI : std_logic_vector(i_funct_range_t) := "111";
  86. -- store functions
  87. subtype s_funct_range_t is natural range 3 - 1 downto 0;
  88. -- byte, half word, word
  89. constant FUNCT_SB : std_logic_vector(s_funct_range_t) := "000";
  90. constant FUNCT_SH : std_logic_vector(s_funct_range_t) := "001";
  91. constant FUNCT_SW : std_logic_vector(s_funct_range_t) := "010";
  92. -- load functions
  93. -- byte, half word, word, unsigned byte, unsigned half word
  94. -- subtype i_funct_range_t already defined above
  95. constant FUNCT_LB : std_logic_vector(i_funct_range_t) := "000";
  96. constant FUNCT_LH : std_logic_vector(i_funct_range_t) := "001";
  97. constant FUNCT_LW : std_logic_vector(i_funct_range_t) := "010";
  98. constant FUNCT_LBU : std_logic_vector(i_funct_range_t) := "100";
  99. constant FUNCT_LHU : std_logic_vector(i_funct_range_t) := "101";
  100. -- branch functions
  101. subtype b_funct_range_t is natural range 3 - 1 downto 0;
  102. constant FUNCT_BEQ : std_logic_vector(b_funct_range_t) := "000";
  103. constant FUNCT_BNE : std_logic_vector(b_funct_range_t) := "001";
  104. constant FUNCT_BLT : std_logic_vector(b_funct_range_t) := "100";
  105. constant FUNCT_BGE : std_logic_vector(b_funct_range_t) := "101";
  106. constant FUNCT_BLTU : std_logic_vector(b_funct_range_t) := "110";
  107. constant FUNCT_BGEU : std_logic_vector(b_funct_range_t) := "111";
  108. -- rs1/rs2 range helper types
  109. subtype rs1_range_t is natural range 19 downto 15;
  110. subtype rs2_range_t is natural range 24 downto 20;
  111. -- rd range helper
  112. subtype rd_range_t is natural range 11 downto 7;
  113. -- immediate calculation helper functions
  114. function calculate_immediate_I_fmt(instr : instr_type) return data_type is
  115. variable imm : data_type;
  116. begin
  117. imm(31 downto 11) := (others => instr(31));
  118. imm(10 downto 5) := instr(30 downto 25);
  119. imm(4 downto 1) := instr(24 downto 21);
  120. imm(0) := instr(20);
  121. return imm;
  122. end function;
  123. function calculate_immediate_S_fmt(instr : instr_type) return data_type is
  124. variable imm : data_type;
  125. begin
  126. imm(31 downto 11) := (others => instr(31));
  127. imm(10 downto 5) := instr(30 downto 25);
  128. imm(4 downto 1) := instr(11 downto 8);
  129. imm(0) := instr(7);
  130. return imm;
  131. end function;
  132. function calculate_immediate_B_fmt(instr : instr_type) return data_type is
  133. variable imm : data_type;
  134. begin
  135. imm(31 downto 12) := (others => instr(31));
  136. imm(11) := instr(7);
  137. imm(10 downto 5) := instr(30 downto 25);
  138. imm(4 downto 1) := instr(11 downto 8);
  139. imm(0) := '0';
  140. return imm;
  141. end function;
  142. function calculate_immediate_U_fmt(instr : instr_type) return data_type is
  143. variable imm : data_type;
  144. begin
  145. imm(31 downto 20) := instr(31 downto 20);
  146. imm(19 downto 12) := instr(19 downto 12);
  147. imm(11 downto 0) := (others => '0');
  148. return imm;
  149. end function;
  150. function calculate_immediate_J_fmt(instr : instr_type) return data_type is
  151. variable imm : data_type;
  152. begin
  153. imm(31 downto 20) := (others => instr(31));
  154. imm(19 downto 12) := instr(19 downto 12);
  155. imm(11) := instr(20);
  156. imm(10 downto 5) := instr(30 downto 25);
  157. imm(4 downto 1) := instr(24 downto 21);
  158. imm(0) := '0';
  159. return imm;
  160. end function;
  161. function calculate_imm_aluop(instr : instr_type) return alu_op_type is
  162. variable op : alu_op_type;
  163. begin
  164. case instr(funct3_I_fmt_range_t) is
  165. when FUNCT_ADDI =>
  166. op := ALU_ADD;
  167. when FUNCT_SLLI =>
  168. op := ALU_SLL;
  169. when FUNCT_SLTI =>
  170. op := ALU_SLT;
  171. when FUNCT_SLTIU =>
  172. op := ALU_SLTU;
  173. when FUNCT_XORI =>
  174. op := ALU_XOR;
  175. -- covers both SRLI and SRAI with imm[10] as a toggle
  176. when FUNCT_SRI =>
  177. if instr(30) = '1' then
  178. op := ALU_SRA;
  179. else
  180. op := ALU_SRL;
  181. end if;
  182. when FUNCT_ORI =>
  183. op := ALU_OR;
  184. when FUNCT_ANDI =>
  185. op := ALU_AND;
  186. -- unknown function
  187. when others =>
  188. -- is already default assigned, but just to be explicit about it
  189. op := ALU_NOP;
  190. end case;
  191. return op;
  192. end function;
  193. function calculate_aluop(instr : instr_type) return alu_op_type is
  194. variable op : alu_op_type;
  195. begin
  196. case instr(funct3_R_fmt_range_t) & instr(funct7_R_fmt_range_t) is
  197. when FUNCT_ADD =>
  198. op := ALU_ADD;
  199. when FUNCT_SUB =>
  200. op := ALU_SUB;
  201. when FUNCT_SLL =>
  202. op := ALU_SLL;
  203. when FUNCT_SLT =>
  204. op := ALU_SLT;
  205. when FUNCT_SLTU =>
  206. op := ALU_SLTU;
  207. when FUNCT_XOR =>
  208. op := ALU_XOR;
  209. when FUNCT_SRL =>
  210. op := ALU_SRL;
  211. when FUNCT_SRA =>
  212. op := ALU_SRA;
  213. when FUNCT_OR =>
  214. op := ALU_OR;
  215. when FUNCT_AND =>
  216. op := ALU_AND;
  217. -- unknown function
  218. when others =>
  219. -- is already default assigned, but just to be explicit about it
  220. op := ALU_NOP;
  221. end case;
  222. return op;
  223. end function;
  224. function calculate_branch_aluop(instr : instr_type) return alu_op_type is
  225. variable op : alu_op_type;
  226. begin
  227. case instr(funct3_B_fmt_range_t) is
  228. when FUNCT_BEQ =>
  229. op := ALU_SUB;
  230. when FUNCT_BNE =>
  231. op := ALU_SUB;
  232. when FUNCT_BLT =>
  233. op := ALU_SLT;
  234. when FUNCT_BGE =>
  235. op := ALU_SLT;
  236. when FUNCT_BLTU =>
  237. op := ALU_SLTU;
  238. when FUNCT_BGEU =>
  239. op := ALU_SLTU;
  240. when others =>
  241. op := ALU_NOP;
  242. end case;
  243. return op;
  244. end function;
  245. function calculate_branch_branchop(instr : instr_type) return branch_type is
  246. variable op : branch_type;
  247. begin
  248. case instr(funct3_B_fmt_range_t) is
  249. when FUNCT_BEQ =>
  250. op := BR_CND;
  251. when FUNCT_BNE =>
  252. op := BR_CNDI;
  253. when FUNCT_BLT =>
  254. op := BR_CNDI;
  255. when FUNCT_BGE =>
  256. op := BR_CND;
  257. when FUNCT_BLTU =>
  258. op := BR_CNDI;
  259. when FUNCT_BGEU =>
  260. op := BR_CND;
  261. when others =>
  262. op := BR_NOP;
  263. end case;
  264. return op;
  265. end function;
  266. function is_branch_funct3_valid(instr : instr_type) return std_logic is
  267. variable funct3 : std_logic_vector(b_funct_range_t);
  268. variable valid : std_logic;
  269. begin
  270. funct3 := instr(funct3_B_fmt_range_t);
  271. if (funct3 = FUNCT_BEQ) or (funct3 = FUNCT_BNE) or (funct3 = FUNCT_BLT) or (funct3 = FUNCT_BGE) or (funct3 = FUNCT_BLTU) or (funct3 = FUNCT_BGEU) then
  272. valid := '1';
  273. else
  274. valid := '0';
  275. end if;
  276. return valid;
  277. end function;
  278. function is_load_funct3_valid(instr : instr_type) return std_logic is
  279. variable funct3 : std_logic_vector(i_funct_range_t);
  280. variable valid : std_logic;
  281. begin
  282. funct3 := instr(funct3_I_fmt_range_t);
  283. if (funct3 = FUNCT_LB) or (funct3 = FUNCT_LH) or (funct3 = FUNCT_LBU) or (funct3 = FUNCT_LHU) or (funct3 = FUNCT_LW) then
  284. valid := '1';
  285. else
  286. valid := '0';
  287. end if;
  288. return valid;
  289. end function;
  290. function is_store_funct3_valid(instr : instr_type) return std_logic is
  291. variable funct3 : std_logic_vector(s_funct_range_t);
  292. variable valid : std_logic;
  293. begin
  294. funct3 := instr(funct3_S_fmt_range_t);
  295. if (funct3 = FUNCT_SB) or (funct3 = FUNCT_SH) or (funct3 = FUNCT_SW) then
  296. valid := '1';
  297. else
  298. valid := '0';
  299. end if;
  300. return valid;
  301. end function;
  302. function calculate_store_memop(instr : instr_type) return memtype_type is
  303. variable op : memtype_type;
  304. begin
  305. case instr(funct3_S_fmt_range_t) is
  306. when FUNCT_SB =>
  307. op := MEM_B;
  308. when FUNCT_SH =>
  309. op := MEM_H;
  310. -- nop is done not via op_type only
  311. -- so default case encompasses FUNCT_SW
  312. when others =>
  313. op := MEM_W;
  314. end case;
  315. return op;
  316. end function;
  317. function calculate_load_memop(instr : instr_type) return memtype_type is
  318. variable op : memtype_type;
  319. begin
  320. case instr(funct3_I_fmt_range_t) is
  321. when FUNCT_LB =>
  322. op := MEM_B;
  323. when FUNCT_LH =>
  324. op := MEM_H;
  325. when FUNCT_LBU =>
  326. op := MEM_BU;
  327. when FUNCT_LHU =>
  328. op := MEM_HU;
  329. -- nop is done not via op_type only
  330. -- so default case encompasses FUNCT_LW
  331. when others =>
  332. op := MEM_W;
  333. end case;
  334. return op;
  335. end function;
  336. begin
  337. -- structural
  338. regfile_inst : entity work.regfile
  339. port map (
  340. clk => clk,
  341. res_n => res_n,
  342. stall => stall,
  343. rdaddr1 => reg.rdaddr1,
  344. rdaddr2 => reg.rdaddr2,
  345. rddata1 => reg.rddata1,
  346. rddata2 => reg.rddata2,
  347. wraddr => reg.wraddr,
  348. wrdata => reg.wrdata,
  349. regwrite => reg.regwrite
  350. );
  351. -- concurrent
  352. reg.rdaddr1 <= instr(rs1_range_t);
  353. reg.rdaddr2 <= instr(rs2_range_t);
  354. reg.regwrite <= reg_write.write;
  355. reg.wraddr <= reg_write.reg;
  356. reg.wrdata <= reg_write.data;
  357. -- sequential
  358. sync : process(clk, res_n, flush, stall)
  359. begin
  360. if res_n = '0' then
  361. internal <= INITIAL_INTERNAL;
  362. elsif stall = '0' and rising_edge(clk) then
  363. internal.pc <= pc_in;
  364. internal.instr <= instr;
  365. end if;
  366. end process;
  367. async : process(all)
  368. begin
  369. -- default assignments
  370. pc_out <= internal.pc;
  371. exc_dec <= '0';
  372. -- exec_op
  373. exec_op.aluop <= ALU_NOP;
  374. exec_op.alusrc1 <= '0';
  375. exec_op.alusrc2 <= '0';
  376. exec_op.alusrc3 <= '0';
  377. exec_op.rs1 <= internal.instr(rs1_range_t);
  378. exec_op.rs2 <= internal.instr(rs2_range_t);
  379. exec_op.readdata1 <= reg.rddata1;
  380. exec_op.readdata2 <= reg.rddata2;
  381. exec_op.imm <= (others => '0');
  382. -- mem_op
  383. mem_op.branch <= BR_NOP;
  384. mem_op.mem <= MEMU_NOP;
  385. -- wb_op
  386. wb_op <= WB_NOP;
  387. wb_op.rd <= internal.instr(rd_range_t);
  388. if flush = '0' then
  389. -- determine instr by opcode
  390. case internal.instr(opcode_range_t) is
  391. when OPC_LUI =>
  392. exec_op.alusrc2 <= '1';
  393. exec_op.imm <= calculate_immediate_U_fmt(internal.instr);
  394. wb_op.write <= '1';
  395. wb_op.src <= WBS_ALU;
  396. when OPC_AUIPC =>
  397. exec_op.aluop <= ALU_ADD;
  398. exec_op.alusrc1 <= '1';
  399. exec_op.alusrc2 <= '1';
  400. exec_op.imm <= calculate_immediate_U_fmt(internal.instr);
  401. wb_op.write <= '1';
  402. wb_op.src <= WBS_ALU;
  403. when OPC_JAL =>
  404. exec_op.alusrc3 <= '1';
  405. exec_op.imm <= calculate_immediate_J_fmt(internal.instr);
  406. mem_op.branch <= BR_BR;
  407. wb_op.write <= '1';
  408. wb_op.src <= WBS_OPC;
  409. when OPC_JALR =>
  410. exec_op.aluop <= ALU_NOP;
  411. exec_op.alusrc1 <= '1';
  412. exec_op.alusrc2 <= '1';
  413. exec_op.alusrc3 <= '1';
  414. exec_op.imm <= calculate_immediate_I_fmt(internal.instr);
  415. mem_op.branch <= BR_BR;
  416. wb_op.write <= '1';
  417. wb_op.src <= WBS_OPC;
  418. if not(internal.instr(funct3_I_fmt_range_t) = "000") then
  419. exc_dec <= '1';
  420. else
  421. exc_dec <= '0';
  422. end if;
  423. when OPC_BRANCH =>
  424. exec_op.aluop <= calculate_branch_aluop(internal.instr);
  425. exec_op.alusrc3 <= '1';
  426. exec_op.imm <= calculate_immediate_B_fmt(internal.instr);
  427. mem_op.branch <= calculate_branch_branchop(internal.instr);
  428. exc_dec <= not(is_branch_funct3_valid(internal.instr));
  429. when OPC_LOAD =>
  430. exec_op.aluop <= ALU_ADD;
  431. exec_op.alusrc2 <= '1';
  432. exec_op.imm <= calculate_immediate_I_fmt(internal.instr);
  433. mem_op.mem.memread <= '1';
  434. mem_op.mem.memwrite <= '0';
  435. mem_op.mem.memtype <= calculate_load_memop(internal.instr);
  436. wb_op.write <= '1';
  437. wb_op.src <= WBS_MEM;
  438. exc_dec <= not(is_load_funct3_valid(internal.instr));
  439. when OPC_STORE =>
  440. exec_op.aluop <= ALU_ADD;
  441. exec_op.alusrc2 <= '1';
  442. exec_op.imm <= calculate_immediate_S_fmt(internal.instr);
  443. mem_op.mem.memread <= '0';
  444. mem_op.mem.memwrite <= '1';
  445. mem_op.mem.memtype <= calculate_store_memop(internal.instr);
  446. exc_dec <= not(is_store_funct3_valid(internal.instr));
  447. when OPC_OP_IMM =>
  448. exec_op.aluop <= calculate_imm_aluop(internal.instr);
  449. exec_op.alusrc2 <= '1';
  450. exec_op.imm <= calculate_immediate_I_fmt(internal.instr);
  451. wb_op.write <= '1';
  452. wb_op.src <= WBS_ALU;
  453. when OPC_OP =>
  454. exec_op.aluop <= calculate_aluop(internal.instr);
  455. if (calculate_aluop(internal.instr) = ALU_NOP) then
  456. exc_dec <= '1';
  457. else
  458. exc_dec <= '0';
  459. end if;
  460. wb_op.write <= '1';
  461. wb_op.src <= WBS_ALU;
  462. -- nop opcode
  463. -- case exists so that it will not register as an exception case
  464. when OPC_NOP =>
  465. -- except if funct3 is wrong
  466. if not(internal.instr(funct3_I_fmt_range_t) = "000") then
  467. exc_dec <= '1';
  468. else
  469. exc_dec <= '0';
  470. end if;
  471. null;
  472. -- unrecognized opcodes throw an exception
  473. when others =>
  474. exc_dec <= '1';
  475. end case;
  476. end if;
  477. end process;
  478. end architecture;