picorv32.v 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082
  1. /*
  2. * PicoRV32 -- A Small RISC-V (RV32I) Processor Core
  3. *
  4. * Copyright (C) 2015 Clifford Wolf <clifford@clifford.at>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. * Changes by hpa 2021:
  19. * - maskirq instruction takes a mask in rs2.
  20. * - retirq opcode changed to mret; no functional change.
  21. * - qregs replaced with a full register bank switch. In general,
  22. * non-power-of-two register files don't save anything, especially in
  23. * FPGAs.
  24. * - getq and setq replaced with new instructions addqxi and addxqi
  25. * for cross-bank register accesses if needed,
  26. * e.g. for stack setup (addqxi sp,sp,frame_size).
  27. * - PROGADDR_RESET and PROGADDR_IRQ changed to ports (allows external
  28. * implementation of vectorized interrupts or fallback reset.)
  29. * - maskirq, waitirq and timer require func3 == 3'b000.
  30. */
  31. /* verilator lint_off WIDTH */
  32. /* verilator lint_off PINMISSING */
  33. /* verilator lint_off CASEOVERLAP */
  34. /* verilator lint_off CASEINCOMPLETE */
  35. `timescale 1 ns / 1 ps
  36. // `default_nettype none
  37. // `define DEBUGNETS
  38. // `define DEBUGREGS
  39. // `define DEBUGASM
  40. // `define DEBUG
  41. `ifdef DEBUG
  42. `define debug(debug_command) debug_command
  43. `else
  44. `define debug(debug_command)
  45. `endif
  46. `ifdef FORMAL
  47. `define FORMAL_KEEP (* keep *)
  48. `define assert(assert_expr) assert(assert_expr)
  49. `else
  50. `ifdef DEBUGNETS
  51. `define FORMAL_KEEP (* keep *)
  52. `else
  53. `define FORMAL_KEEP
  54. `endif
  55. `define assert(assert_expr) empty_statement
  56. `endif
  57. // uncomment this for register file in extra module
  58. // `define PICORV32_REGS picorv32_regs
  59. // this macro can be used to check if the verilog files in your
  60. // design are read in the correct order.
  61. `define PICORV32_V
  62. /***************************************************************
  63. * picorv32
  64. ***************************************************************/
  65. module picorv32 #(
  66. parameter [ 0:0] ENABLE_COUNTERS = 1,
  67. parameter [ 0:0] ENABLE_COUNTERS64 = 1,
  68. parameter [ 0:0] ENABLE_REGS_16_31 = 1,
  69. parameter [ 0:0] ENABLE_REGS_DUALPORT = 1,
  70. parameter [ 0:0] LATCHED_MEM_RDATA = 0,
  71. parameter [ 0:0] TWO_STAGE_SHIFT = 1,
  72. parameter [ 0:0] BARREL_SHIFTER = 0,
  73. parameter [ 0:0] TWO_CYCLE_COMPARE = 0,
  74. parameter [ 0:0] TWO_CYCLE_ALU = 0,
  75. parameter [ 0:0] COMPRESSED_ISA = 0,
  76. parameter [ 0:0] CATCH_MISALIGN = 1,
  77. parameter [ 0:0] CATCH_ILLINSN = 1,
  78. parameter [ 0:0] ENABLE_PCPI = 0,
  79. parameter [ 0:0] ENABLE_MUL = 0,
  80. parameter [ 0:0] ENABLE_FAST_MUL = 0,
  81. parameter [ 0:0] ENABLE_DIV = 0,
  82. parameter [ 0:0] ENABLE_IRQ = 0,
  83. parameter [ 0:0] ENABLE_IRQ_QREGS = 1,
  84. parameter [ 0:0] ENABLE_IRQ_TIMER = 1,
  85. parameter [ 0:0] ENABLE_TRACE = 0,
  86. parameter [ 0:0] REGS_INIT_ZERO = 0,
  87. parameter [31:0] MASKED_IRQ = 32'h 0000_0000,
  88. parameter [31:0] LATCHED_IRQ = 32'h ffff_ffff,
  89. parameter [31:0] STACKADDR = 32'h ffff_ffff,
  90. parameter [ 4:0] RA_IRQ_REG = ENABLE_IRQ_QREGS ? 26 : 3,
  91. parameter [ 4:0] MASK_IRQ_REG = ENABLE_IRQ_QREGS ? 27 : 4
  92. ) (
  93. input clk, resetn,
  94. output reg trap,
  95. input [31:0] progaddr_reset,
  96. input [31:0] progaddr_irq,
  97. output reg mem_valid,
  98. output reg mem_instr,
  99. input mem_ready,
  100. output reg [31:0] mem_addr,
  101. output reg [31:0] mem_wdata,
  102. output reg [ 3:0] mem_wstrb,
  103. input [31:0] mem_rdata,
  104. // Look-Ahead Interface
  105. output mem_la_read,
  106. output mem_la_write,
  107. output [31:0] mem_la_addr,
  108. output reg [31:0] mem_la_wdata,
  109. output reg [ 3:0] mem_la_wstrb,
  110. // Pico Co-Processor Interface (PCPI)
  111. output reg pcpi_valid,
  112. output reg [31:0] pcpi_insn,
  113. output [31:0] pcpi_rs1,
  114. output [31:0] pcpi_rs2,
  115. input pcpi_wr,
  116. input [31:0] pcpi_rd,
  117. input pcpi_wait,
  118. input pcpi_ready,
  119. // IRQ Interface
  120. input [31:0] irq,
  121. output reg [31:0] eoi,
  122. `ifdef RISCV_FORMAL
  123. output reg rvfi_valid,
  124. output reg [63:0] rvfi_order,
  125. output reg [31:0] rvfi_insn,
  126. output reg rvfi_trap,
  127. output reg rvfi_halt,
  128. output reg rvfi_intr,
  129. output reg [ 1:0] rvfi_mode,
  130. output reg [ 1:0] rvfi_ixl,
  131. output reg [ 4:0] rvfi_rs1_addr,
  132. output reg [ 4:0] rvfi_rs2_addr,
  133. output reg [31:0] rvfi_rs1_rdata,
  134. output reg [31:0] rvfi_rs2_rdata,
  135. output reg [ 4:0] rvfi_rd_addr,
  136. output reg [31:0] rvfi_rd_wdata,
  137. output reg [31:0] rvfi_pc_rdata,
  138. output reg [31:0] rvfi_pc_wdata,
  139. output reg [31:0] rvfi_mem_addr,
  140. output reg [ 3:0] rvfi_mem_rmask,
  141. output reg [ 3:0] rvfi_mem_wmask,
  142. output reg [31:0] rvfi_mem_rdata,
  143. output reg [31:0] rvfi_mem_wdata,
  144. output reg [63:0] rvfi_csr_mcycle_rmask,
  145. output reg [63:0] rvfi_csr_mcycle_wmask,
  146. output reg [63:0] rvfi_csr_mcycle_rdata,
  147. output reg [63:0] rvfi_csr_mcycle_wdata,
  148. output reg [63:0] rvfi_csr_minstret_rmask,
  149. output reg [63:0] rvfi_csr_minstret_wmask,
  150. output reg [63:0] rvfi_csr_minstret_rdata,
  151. output reg [63:0] rvfi_csr_minstret_wdata,
  152. `endif
  153. // Trace Interface
  154. output reg trace_valid,
  155. output reg [35:0] trace_data
  156. );
  157. localparam integer irq_timer = 0;
  158. localparam integer irq_ebreak = 1;
  159. localparam integer irq_buserror = 2;
  160. localparam integer xreg_count = ENABLE_REGS_16_31 ? 32 : 16;
  161. localparam integer qreg_count = (ENABLE_IRQ && ENABLE_IRQ_QREGS) ? xreg_count : 0;
  162. localparam integer qreg_offset = qreg_count; // 0 for no qregs
  163. localparam integer regfile_size = xreg_count + qreg_count;
  164. localparam integer regindex_bits = $clog2(regfile_size);
  165. localparam WITH_PCPI = ENABLE_PCPI || ENABLE_MUL || ENABLE_FAST_MUL || ENABLE_DIV;
  166. localparam [35:0] TRACE_BRANCH = {4'b 0001, 32'b 0};
  167. localparam [35:0] TRACE_ADDR = {4'b 0010, 32'b 0};
  168. localparam [35:0] TRACE_IRQ = {4'b 1000, 32'b 0};
  169. reg [63:0] count_cycle, count_instr;
  170. reg [31:0] reg_pc, reg_next_pc, reg_op1, reg_op2, reg_out;
  171. reg [4:0] reg_sh;
  172. reg [31:0] next_insn_opcode;
  173. reg [31:0] dbg_insn_opcode;
  174. reg [31:0] dbg_insn_addr;
  175. wire dbg_mem_valid = mem_valid;
  176. wire dbg_mem_instr = mem_instr;
  177. wire dbg_mem_ready = mem_ready;
  178. wire [31:0] dbg_mem_addr = mem_addr;
  179. wire [31:0] dbg_mem_wdata = mem_wdata;
  180. wire [ 3:0] dbg_mem_wstrb = mem_wstrb;
  181. wire [31:0] dbg_mem_rdata = mem_rdata;
  182. assign pcpi_rs1 = reg_op1;
  183. assign pcpi_rs2 = reg_op2;
  184. wire [31:0] next_pc;
  185. reg irq_delay;
  186. reg irq_active;
  187. reg [31:0] irq_mask;
  188. reg [31:0] irq_pending;
  189. reg [31:0] timer;
  190. `ifndef PICORV32_REGS
  191. reg [31:0] cpuregs [0:regfile_size-1];
  192. integer i;
  193. initial begin
  194. if (REGS_INIT_ZERO) begin
  195. for (i = 0; i < regfile_size; i = i+1)
  196. cpuregs[i] = 0;
  197. end
  198. end
  199. `endif
  200. task empty_statement;
  201. // This task is used by the `assert directive in non-formal mode to
  202. // avoid empty statement (which are unsupported by plain Verilog syntax).
  203. begin end
  204. endtask
  205. `ifdef DEBUGREGS
  206. `define dr_reg(x) cpuregs[x | (irq_active ? qreg_offset : 0)]
  207. wire [31:0] dbg_reg_x0 = 0;
  208. wire [31:0] dbg_reg_x1 = `dr_reg(1);
  209. wire [31:0] dbg_reg_x2 = `dr_reg(2);
  210. wire [31:0] dbg_reg_x3 = `dr_reg(3);
  211. wire [31:0] dbg_reg_x4 = `dr_reg(4);
  212. wire [31:0] dbg_reg_x5 = `dr_reg(5);
  213. wire [31:0] dbg_reg_x6 = `dr_reg(6);
  214. wire [31:0] dbg_reg_x7 = `dr_reg(7);
  215. wire [31:0] dbg_reg_x8 = `dr_reg(8);
  216. wire [31:0] dbg_reg_x9 = `dr_reg(9);
  217. wire [31:0] dbg_reg_x10 = `dr_reg(10);
  218. wire [31:0] dbg_reg_x11 = `dr_reg(11);
  219. wire [31:0] dbg_reg_x12 = `dr_reg(12);
  220. wire [31:0] dbg_reg_x13 = `dr_reg(13);
  221. wire [31:0] dbg_reg_x14 = `dr_reg(14);
  222. wire [31:0] dbg_reg_x15 = `dr_reg(15);
  223. wire [31:0] dbg_reg_x16 = `dr_reg(16);
  224. wire [31:0] dbg_reg_x17 = `dr_reg(17);
  225. wire [31:0] dbg_reg_x18 = `dr_reg(18);
  226. wire [31:0] dbg_reg_x19 = `dr_reg(19);
  227. wire [31:0] dbg_reg_x20 = `dr_reg(20);
  228. wire [31:0] dbg_reg_x21 = `dr_reg(21);
  229. wire [31:0] dbg_reg_x22 = `dr_reg(22);
  230. wire [31:0] dbg_reg_x23 = `dr_reg(23);
  231. wire [31:0] dbg_reg_x24 = `dr_reg(24);
  232. wire [31:0] dbg_reg_x25 = `dr_reg(25);
  233. wire [31:0] dbg_reg_x26 = `dr_reg(26);
  234. wire [31:0] dbg_reg_x27 = `dr_reg(27);
  235. wire [31:0] dbg_reg_x28 = `dr_reg(28);
  236. wire [31:0] dbg_reg_x29 = `dr_reg(29);
  237. wire [31:0] dbg_reg_x30 = `dr_reg(30);
  238. wire [31:0] dbg_reg_x31 = `dr_reg(31);
  239. `endif
  240. // Internal PCPI Cores
  241. wire pcpi_mul_wr;
  242. wire [31:0] pcpi_mul_rd;
  243. wire pcpi_mul_wait;
  244. wire pcpi_mul_ready;
  245. wire pcpi_div_wr;
  246. wire [31:0] pcpi_div_rd;
  247. wire pcpi_div_wait;
  248. wire pcpi_div_ready;
  249. reg pcpi_int_wr;
  250. reg [31:0] pcpi_int_rd;
  251. reg pcpi_int_wait;
  252. reg pcpi_int_ready;
  253. generate if (ENABLE_FAST_MUL) begin
  254. picorv32_pcpi_fast_mul pcpi_mul (
  255. .clk (clk ),
  256. .resetn (resetn ),
  257. .pcpi_valid(pcpi_valid ),
  258. .pcpi_insn (pcpi_insn ),
  259. .pcpi_rs1 (pcpi_rs1 ),
  260. .pcpi_rs2 (pcpi_rs2 ),
  261. .pcpi_wr (pcpi_mul_wr ),
  262. .pcpi_rd (pcpi_mul_rd ),
  263. .pcpi_wait (pcpi_mul_wait ),
  264. .pcpi_ready(pcpi_mul_ready )
  265. );
  266. end else if (ENABLE_MUL) begin
  267. picorv32_pcpi_mul pcpi_mul (
  268. .clk (clk ),
  269. .resetn (resetn ),
  270. .pcpi_valid(pcpi_valid ),
  271. .pcpi_insn (pcpi_insn ),
  272. .pcpi_rs1 (pcpi_rs1 ),
  273. .pcpi_rs2 (pcpi_rs2 ),
  274. .pcpi_wr (pcpi_mul_wr ),
  275. .pcpi_rd (pcpi_mul_rd ),
  276. .pcpi_wait (pcpi_mul_wait ),
  277. .pcpi_ready(pcpi_mul_ready )
  278. );
  279. end else begin
  280. assign pcpi_mul_wr = 0;
  281. assign pcpi_mul_rd = 32'bx;
  282. assign pcpi_mul_wait = 0;
  283. assign pcpi_mul_ready = 0;
  284. end endgenerate
  285. generate if (ENABLE_DIV) begin
  286. picorv32_pcpi_div pcpi_div (
  287. .clk (clk ),
  288. .resetn (resetn ),
  289. .pcpi_valid(pcpi_valid ),
  290. .pcpi_insn (pcpi_insn ),
  291. .pcpi_rs1 (pcpi_rs1 ),
  292. .pcpi_rs2 (pcpi_rs2 ),
  293. .pcpi_wr (pcpi_div_wr ),
  294. .pcpi_rd (pcpi_div_rd ),
  295. .pcpi_wait (pcpi_div_wait ),
  296. .pcpi_ready(pcpi_div_ready )
  297. );
  298. end else begin
  299. assign pcpi_div_wr = 0;
  300. assign pcpi_div_rd = 32'bx;
  301. assign pcpi_div_wait = 0;
  302. assign pcpi_div_ready = 0;
  303. end endgenerate
  304. always @* begin
  305. pcpi_int_wr = 0;
  306. pcpi_int_rd = 32'bx;
  307. pcpi_int_wait = |{ENABLE_PCPI && pcpi_wait, (ENABLE_MUL || ENABLE_FAST_MUL) && pcpi_mul_wait, ENABLE_DIV && pcpi_div_wait};
  308. pcpi_int_ready = |{ENABLE_PCPI && pcpi_ready, (ENABLE_MUL || ENABLE_FAST_MUL) && pcpi_mul_ready, ENABLE_DIV && pcpi_div_ready};
  309. (* parallel_case *)
  310. case (1'b1)
  311. ENABLE_PCPI && pcpi_ready: begin
  312. pcpi_int_wr = ENABLE_PCPI ? pcpi_wr : 0;
  313. pcpi_int_rd = ENABLE_PCPI ? pcpi_rd : 0;
  314. end
  315. (ENABLE_MUL || ENABLE_FAST_MUL) && pcpi_mul_ready: begin
  316. pcpi_int_wr = pcpi_mul_wr;
  317. pcpi_int_rd = pcpi_mul_rd;
  318. end
  319. ENABLE_DIV && pcpi_div_ready: begin
  320. pcpi_int_wr = pcpi_div_wr;
  321. pcpi_int_rd = pcpi_div_rd;
  322. end
  323. endcase
  324. end
  325. // Memory Interface
  326. reg [1:0] mem_state;
  327. reg [1:0] mem_wordsize;
  328. reg [31:0] mem_rdata_word;
  329. reg [31:0] mem_rdata_q;
  330. reg mem_do_prefetch;
  331. reg mem_do_rinst;
  332. reg mem_do_rdata;
  333. reg mem_do_wdata;
  334. wire mem_xfer;
  335. reg mem_la_secondword, mem_la_firstword_reg, last_mem_valid;
  336. wire mem_la_firstword = COMPRESSED_ISA && (mem_do_prefetch || mem_do_rinst) && next_pc[1] && !mem_la_secondword;
  337. wire mem_la_firstword_xfer = COMPRESSED_ISA && mem_xfer && (!last_mem_valid ? mem_la_firstword : mem_la_firstword_reg);
  338. reg prefetched_high_word;
  339. reg clear_prefetched_high_word;
  340. reg [15:0] mem_16bit_buffer;
  341. wire [31:0] mem_rdata_latched_noshuffle;
  342. wire [31:0] mem_rdata_latched;
  343. wire mem_la_use_prefetched_high_word = COMPRESSED_ISA && mem_la_firstword && prefetched_high_word && !clear_prefetched_high_word;
  344. assign mem_xfer = (mem_valid && mem_ready) || (mem_la_use_prefetched_high_word && mem_do_rinst);
  345. wire mem_busy = |{mem_do_prefetch, mem_do_rinst, mem_do_rdata, mem_do_wdata};
  346. wire mem_done = resetn && ((mem_xfer && |mem_state && (mem_do_rinst || mem_do_rdata || mem_do_wdata)) || (&mem_state && mem_do_rinst)) &&
  347. (!mem_la_firstword || (~&mem_rdata_latched[1:0] && mem_xfer));
  348. assign mem_la_write = resetn && !mem_state && mem_do_wdata;
  349. assign mem_la_read = resetn && ((!mem_la_use_prefetched_high_word && !mem_state && (mem_do_rinst || mem_do_prefetch || mem_do_rdata)) ||
  350. (COMPRESSED_ISA && mem_xfer && (!last_mem_valid ? mem_la_firstword : mem_la_firstword_reg) && !mem_la_secondword && &mem_rdata_latched[1:0]));
  351. assign mem_la_addr = (mem_do_prefetch || mem_do_rinst) ? {next_pc[31:2] + mem_la_firstword_xfer, 2'b00} : {reg_op1[31:2], 2'b00};
  352. assign mem_rdata_latched_noshuffle = (mem_xfer || LATCHED_MEM_RDATA) ? mem_rdata : mem_rdata_q;
  353. assign mem_rdata_latched = COMPRESSED_ISA && mem_la_use_prefetched_high_word ? {16'bx, mem_16bit_buffer} :
  354. COMPRESSED_ISA && mem_la_secondword ? {mem_rdata_latched_noshuffle[15:0], mem_16bit_buffer} :
  355. COMPRESSED_ISA && mem_la_firstword ? {16'bx, mem_rdata_latched_noshuffle[31:16]} : mem_rdata_latched_noshuffle;
  356. always @(posedge clk) begin
  357. if (!resetn) begin
  358. mem_la_firstword_reg <= 0;
  359. last_mem_valid <= 0;
  360. end else begin
  361. if (!last_mem_valid)
  362. mem_la_firstword_reg <= mem_la_firstword;
  363. last_mem_valid <= mem_valid && !mem_ready;
  364. end
  365. end
  366. always @* begin
  367. (* full_case *)
  368. case (mem_wordsize)
  369. 0: begin
  370. mem_la_wdata = reg_op2;
  371. mem_la_wstrb = 4'b1111;
  372. mem_rdata_word = mem_rdata;
  373. end
  374. 1: begin
  375. mem_la_wdata = {2{reg_op2[15:0]}};
  376. mem_la_wstrb = reg_op1[1] ? 4'b1100 : 4'b0011;
  377. case (reg_op1[1])
  378. 1'b0: mem_rdata_word = {16'b0, mem_rdata[15: 0]};
  379. 1'b1: mem_rdata_word = {16'b0, mem_rdata[31:16]};
  380. endcase
  381. end
  382. 2: begin
  383. mem_la_wdata = {4{reg_op2[7:0]}};
  384. mem_la_wstrb = 4'b0001 << reg_op1[1:0];
  385. case (reg_op1[1:0])
  386. 2'b00: mem_rdata_word = {24'b0, mem_rdata[ 7: 0]};
  387. 2'b01: mem_rdata_word = {24'b0, mem_rdata[15: 8]};
  388. 2'b10: mem_rdata_word = {24'b0, mem_rdata[23:16]};
  389. 2'b11: mem_rdata_word = {24'b0, mem_rdata[31:24]};
  390. endcase
  391. end
  392. endcase
  393. end
  394. always @(posedge clk) begin
  395. if (mem_xfer) begin
  396. mem_rdata_q <= COMPRESSED_ISA ? mem_rdata_latched : mem_rdata;
  397. next_insn_opcode <= COMPRESSED_ISA ? mem_rdata_latched : mem_rdata;
  398. end
  399. if (COMPRESSED_ISA && mem_done && (mem_do_prefetch || mem_do_rinst)) begin
  400. case (mem_rdata_latched[1:0])
  401. 2'b00: begin // Quadrant 0
  402. case (mem_rdata_latched[15:13])
  403. 3'b000: begin // C.ADDI4SPN
  404. mem_rdata_q[14:12] <= 3'b000;
  405. mem_rdata_q[31:20] <= {2'b0, mem_rdata_latched[10:7], mem_rdata_latched[12:11], mem_rdata_latched[5], mem_rdata_latched[6], 2'b00};
  406. end
  407. 3'b010: begin // C.LW
  408. mem_rdata_q[31:20] <= {5'b0, mem_rdata_latched[5], mem_rdata_latched[12:10], mem_rdata_latched[6], 2'b00};
  409. mem_rdata_q[14:12] <= 3'b 010;
  410. end
  411. 3'b 110: begin // C.SW
  412. {mem_rdata_q[31:25], mem_rdata_q[11:7]} <= {5'b0, mem_rdata_latched[5], mem_rdata_latched[12:10], mem_rdata_latched[6], 2'b00};
  413. mem_rdata_q[14:12] <= 3'b 010;
  414. end
  415. endcase
  416. end
  417. 2'b01: begin // Quadrant 1
  418. case (mem_rdata_latched[15:13])
  419. 3'b 000: begin // C.ADDI
  420. mem_rdata_q[14:12] <= 3'b000;
  421. mem_rdata_q[31:20] <= $signed({mem_rdata_latched[12], mem_rdata_latched[6:2]});
  422. end
  423. 3'b 010: begin // C.LI
  424. mem_rdata_q[14:12] <= 3'b000;
  425. mem_rdata_q[31:20] <= $signed({mem_rdata_latched[12], mem_rdata_latched[6:2]});
  426. end
  427. 3'b 011: begin
  428. if (mem_rdata_latched[11:7] == 2) begin // C.ADDI16SP
  429. mem_rdata_q[14:12] <= 3'b000;
  430. mem_rdata_q[31:20] <= $signed({mem_rdata_latched[12], mem_rdata_latched[4:3],
  431. mem_rdata_latched[5], mem_rdata_latched[2], mem_rdata_latched[6], 4'b 0000});
  432. end else begin // C.LUI
  433. mem_rdata_q[31:12] <= $signed({mem_rdata_latched[12], mem_rdata_latched[6:2]});
  434. end
  435. end
  436. 3'b100: begin
  437. if (mem_rdata_latched[11:10] == 2'b00) begin // C.SRLI
  438. mem_rdata_q[31:25] <= 7'b0000000;
  439. mem_rdata_q[14:12] <= 3'b 101;
  440. end
  441. if (mem_rdata_latched[11:10] == 2'b01) begin // C.SRAI
  442. mem_rdata_q[31:25] <= 7'b0100000;
  443. mem_rdata_q[14:12] <= 3'b 101;
  444. end
  445. if (mem_rdata_latched[11:10] == 2'b10) begin // C.ANDI
  446. mem_rdata_q[14:12] <= 3'b111;
  447. mem_rdata_q[31:20] <= $signed({mem_rdata_latched[12], mem_rdata_latched[6:2]});
  448. end
  449. if (mem_rdata_latched[12:10] == 3'b011) begin // C.SUB, C.XOR, C.OR, C.AND
  450. if (mem_rdata_latched[6:5] == 2'b00) mem_rdata_q[14:12] <= 3'b000;
  451. if (mem_rdata_latched[6:5] == 2'b01) mem_rdata_q[14:12] <= 3'b100;
  452. if (mem_rdata_latched[6:5] == 2'b10) mem_rdata_q[14:12] <= 3'b110;
  453. if (mem_rdata_latched[6:5] == 2'b11) mem_rdata_q[14:12] <= 3'b111;
  454. mem_rdata_q[31:25] <= mem_rdata_latched[6:5] == 2'b00 ? 7'b0100000 : 7'b0000000;
  455. end
  456. end
  457. 3'b 110: begin // C.BEQZ
  458. mem_rdata_q[14:12] <= 3'b000;
  459. { mem_rdata_q[31], mem_rdata_q[7], mem_rdata_q[30:25], mem_rdata_q[11:8] } <=
  460. $signed({mem_rdata_latched[12], mem_rdata_latched[6:5], mem_rdata_latched[2],
  461. mem_rdata_latched[11:10], mem_rdata_latched[4:3]});
  462. end
  463. 3'b 111: begin // C.BNEZ
  464. mem_rdata_q[14:12] <= 3'b001;
  465. { mem_rdata_q[31], mem_rdata_q[7], mem_rdata_q[30:25], mem_rdata_q[11:8] } <=
  466. $signed({mem_rdata_latched[12], mem_rdata_latched[6:5], mem_rdata_latched[2],
  467. mem_rdata_latched[11:10], mem_rdata_latched[4:3]});
  468. end
  469. endcase
  470. end
  471. 2'b10: begin // Quadrant 2
  472. case (mem_rdata_latched[15:13])
  473. 3'b000: begin // C.SLLI
  474. mem_rdata_q[31:25] <= 7'b0000000;
  475. mem_rdata_q[14:12] <= 3'b 001;
  476. end
  477. 3'b010: begin // C.LWSP
  478. mem_rdata_q[31:20] <= {4'b0, mem_rdata_latched[3:2], mem_rdata_latched[12], mem_rdata_latched[6:4], 2'b00};
  479. mem_rdata_q[14:12] <= 3'b 010;
  480. end
  481. 3'b100: begin
  482. if (mem_rdata_latched[12] == 0 && mem_rdata_latched[6:2] == 0) begin // C.JR
  483. mem_rdata_q[14:12] <= 3'b000;
  484. mem_rdata_q[31:20] <= 12'b0;
  485. end
  486. if (mem_rdata_latched[12] == 0 && mem_rdata_latched[6:2] != 0) begin // C.MV
  487. mem_rdata_q[14:12] <= 3'b000;
  488. mem_rdata_q[31:25] <= 7'b0000000;
  489. end
  490. if (mem_rdata_latched[12] != 0 && mem_rdata_latched[11:7] != 0 && mem_rdata_latched[6:2] == 0) begin // C.JALR
  491. mem_rdata_q[14:12] <= 3'b000;
  492. mem_rdata_q[31:20] <= 12'b0;
  493. end
  494. if (mem_rdata_latched[12] != 0 && mem_rdata_latched[6:2] != 0) begin // C.ADD
  495. mem_rdata_q[14:12] <= 3'b000;
  496. mem_rdata_q[31:25] <= 7'b0000000;
  497. end
  498. end
  499. 3'b110: begin // C.SWSP
  500. {mem_rdata_q[31:25], mem_rdata_q[11:7]} <= {4'b0, mem_rdata_latched[8:7], mem_rdata_latched[12:9], 2'b00};
  501. mem_rdata_q[14:12] <= 3'b 010;
  502. end
  503. endcase
  504. end
  505. endcase
  506. end
  507. end
  508. always @(posedge clk) begin
  509. if (resetn && !trap) begin
  510. if (mem_do_prefetch || mem_do_rinst || mem_do_rdata)
  511. `assert(!mem_do_wdata);
  512. if (mem_do_prefetch || mem_do_rinst)
  513. `assert(!mem_do_rdata);
  514. if (mem_do_rdata)
  515. `assert(!mem_do_prefetch && !mem_do_rinst);
  516. if (mem_do_wdata)
  517. `assert(!(mem_do_prefetch || mem_do_rinst || mem_do_rdata));
  518. if (mem_state == 2 || mem_state == 3)
  519. `assert(mem_valid || mem_do_prefetch);
  520. end
  521. end
  522. always @(posedge clk) begin
  523. if (!resetn || trap) begin
  524. if (!resetn)
  525. mem_state <= 0;
  526. if (!resetn || mem_ready)
  527. mem_valid <= 0;
  528. mem_la_secondword <= 0;
  529. prefetched_high_word <= 0;
  530. end else begin
  531. if (mem_la_read || mem_la_write) begin
  532. mem_addr <= mem_la_addr;
  533. mem_wstrb <= mem_la_wstrb & {4{mem_la_write}};
  534. end
  535. if (mem_la_write) begin
  536. mem_wdata <= mem_la_wdata;
  537. end
  538. case (mem_state)
  539. 0: begin
  540. if (mem_do_prefetch || mem_do_rinst || mem_do_rdata) begin
  541. mem_valid <= !mem_la_use_prefetched_high_word;
  542. mem_instr <= mem_do_prefetch || mem_do_rinst;
  543. mem_wstrb <= 0;
  544. mem_state <= 1;
  545. end
  546. if (mem_do_wdata) begin
  547. mem_valid <= 1;
  548. mem_instr <= 0;
  549. mem_state <= 2;
  550. end
  551. end
  552. 1: begin
  553. `assert(mem_wstrb == 0);
  554. `assert(mem_do_prefetch || mem_do_rinst || mem_do_rdata);
  555. `assert(mem_valid == !mem_la_use_prefetched_high_word);
  556. `assert(mem_instr == (mem_do_prefetch || mem_do_rinst));
  557. if (mem_xfer) begin
  558. if (COMPRESSED_ISA && mem_la_read) begin
  559. mem_valid <= 1;
  560. mem_la_secondword <= 1;
  561. if (!mem_la_use_prefetched_high_word)
  562. mem_16bit_buffer <= mem_rdata[31:16];
  563. end else begin
  564. mem_valid <= 0;
  565. mem_la_secondword <= 0;
  566. if (COMPRESSED_ISA && !mem_do_rdata) begin
  567. if (~&mem_rdata[1:0] || mem_la_secondword) begin
  568. mem_16bit_buffer <= mem_rdata[31:16];
  569. prefetched_high_word <= 1;
  570. end else begin
  571. prefetched_high_word <= 0;
  572. end
  573. end
  574. mem_state <= mem_do_rinst || mem_do_rdata ? 0 : 3;
  575. end
  576. end
  577. end
  578. 2: begin
  579. `assert(mem_wstrb != 0);
  580. `assert(mem_do_wdata);
  581. if (mem_xfer) begin
  582. mem_valid <= 0;
  583. mem_state <= 0;
  584. end
  585. end
  586. 3: begin
  587. `assert(mem_wstrb == 0);
  588. `assert(mem_do_prefetch);
  589. if (mem_do_rinst) begin
  590. mem_state <= 0;
  591. end
  592. end
  593. endcase
  594. end
  595. if (clear_prefetched_high_word)
  596. prefetched_high_word <= 0;
  597. end
  598. // Instruction Decoder
  599. reg instr_lui, instr_auipc, instr_jal, instr_jalr;
  600. reg instr_beq, instr_bne, instr_blt, instr_bge, instr_bltu, instr_bgeu;
  601. reg instr_lb, instr_lh, instr_lw, instr_lbu, instr_lhu, instr_sb, instr_sh, instr_sw;
  602. reg instr_addi, instr_slti, instr_sltiu, instr_xori, instr_ori, instr_andi, instr_slli, instr_srli, instr_srai;
  603. reg instr_add, instr_sub, instr_sll, instr_slt, instr_sltu, instr_xor, instr_srl, instr_sra, instr_or, instr_and;
  604. reg instr_rdcycle, instr_rdcycleh, instr_rdinstr, instr_rdinstrh, instr_ecall_ebreak;
  605. reg instr_addqxi, instr_addxqi, instr_retirq, instr_maskirq, instr_waitirq, instr_timer;
  606. wire instr_trap;
  607. reg [regindex_bits-1:0] decoded_rd, decoded_rs1, decoded_rs2;
  608. reg [31:0] decoded_imm, decoded_imm_j;
  609. reg decoder_trigger;
  610. reg decoder_trigger_q;
  611. reg decoder_pseudo_trigger;
  612. reg decoder_pseudo_trigger_q;
  613. reg compressed_instr;
  614. reg is_lui_auipc_jal;
  615. reg is_lb_lh_lw_lbu_lhu;
  616. reg is_slli_srli_srai;
  617. reg is_jalr_addi_slti_sltiu_xori_ori_andi_addqxi;
  618. reg is_sb_sh_sw;
  619. reg is_sll_srl_sra;
  620. reg is_lui_auipc_jal_jalr_addi_add_sub_addqxi;
  621. reg is_slti_blt_slt;
  622. reg is_sltiu_bltu_sltu;
  623. reg is_beq_bne_blt_bge_bltu_bgeu;
  624. reg is_lbu_lhu_lw;
  625. reg is_alu_reg_imm;
  626. reg is_alu_reg_reg;
  627. reg is_compare;
  628. reg is_addqxi;
  629. assign instr_trap = (CATCH_ILLINSN || WITH_PCPI) && !{instr_lui, instr_auipc, instr_jal, instr_jalr,
  630. instr_beq, instr_bne, instr_blt, instr_bge, instr_bltu, instr_bgeu,
  631. instr_lb, instr_lh, instr_lw, instr_lbu, instr_lhu, instr_sb, instr_sh, instr_sw,
  632. instr_addi, instr_slti, instr_sltiu, instr_xori, instr_ori, instr_andi, instr_slli, instr_srli, instr_srai,
  633. instr_add, instr_sub, instr_sll, instr_slt, instr_sltu, instr_xor, instr_srl, instr_sra, instr_or, instr_and,
  634. instr_rdcycle, instr_rdcycleh, instr_rdinstr, instr_rdinstrh,
  635. instr_addqxi, instr_retirq, instr_maskirq, instr_waitirq, instr_timer};
  636. wire is_rdcycle_rdcycleh_rdinstr_rdinstrh;
  637. assign is_rdcycle_rdcycleh_rdinstr_rdinstrh = |{instr_rdcycle, instr_rdcycleh, instr_rdinstr, instr_rdinstrh};
  638. reg [63:0] new_ascii_instr;
  639. `FORMAL_KEEP reg [63:0] dbg_ascii_instr;
  640. `FORMAL_KEEP reg [31:0] dbg_insn_imm;
  641. `FORMAL_KEEP reg [4:0] dbg_insn_rs1;
  642. `FORMAL_KEEP reg [4:0] dbg_insn_rs2;
  643. `FORMAL_KEEP reg [4:0] dbg_insn_rd;
  644. `FORMAL_KEEP reg [31:0] dbg_rs1val;
  645. `FORMAL_KEEP reg [31:0] dbg_rs2val;
  646. `FORMAL_KEEP reg dbg_rs1val_valid;
  647. `FORMAL_KEEP reg dbg_rs2val_valid;
  648. always @* begin
  649. new_ascii_instr = "";
  650. if (instr_lui) new_ascii_instr = "lui";
  651. if (instr_auipc) new_ascii_instr = "auipc";
  652. if (instr_jal) new_ascii_instr = "jal";
  653. if (instr_jalr) new_ascii_instr = "jalr";
  654. if (instr_beq) new_ascii_instr = "beq";
  655. if (instr_bne) new_ascii_instr = "bne";
  656. if (instr_blt) new_ascii_instr = "blt";
  657. if (instr_bge) new_ascii_instr = "bge";
  658. if (instr_bltu) new_ascii_instr = "bltu";
  659. if (instr_bgeu) new_ascii_instr = "bgeu";
  660. if (instr_lb) new_ascii_instr = "lb";
  661. if (instr_lh) new_ascii_instr = "lh";
  662. if (instr_lw) new_ascii_instr = "lw";
  663. if (instr_lbu) new_ascii_instr = "lbu";
  664. if (instr_lhu) new_ascii_instr = "lhu";
  665. if (instr_sb) new_ascii_instr = "sb";
  666. if (instr_sh) new_ascii_instr = "sh";
  667. if (instr_sw) new_ascii_instr = "sw";
  668. if (instr_addi) new_ascii_instr = "addi";
  669. if (instr_slti) new_ascii_instr = "slti";
  670. if (instr_sltiu) new_ascii_instr = "sltiu";
  671. if (instr_xori) new_ascii_instr = "xori";
  672. if (instr_ori) new_ascii_instr = "ori";
  673. if (instr_andi) new_ascii_instr = "andi";
  674. if (instr_slli) new_ascii_instr = "slli";
  675. if (instr_srli) new_ascii_instr = "srli";
  676. if (instr_srai) new_ascii_instr = "srai";
  677. if (instr_add) new_ascii_instr = "add";
  678. if (instr_sub) new_ascii_instr = "sub";
  679. if (instr_sll) new_ascii_instr = "sll";
  680. if (instr_slt) new_ascii_instr = "slt";
  681. if (instr_sltu) new_ascii_instr = "sltu";
  682. if (instr_xor) new_ascii_instr = "xor";
  683. if (instr_srl) new_ascii_instr = "srl";
  684. if (instr_sra) new_ascii_instr = "sra";
  685. if (instr_or) new_ascii_instr = "or";
  686. if (instr_and) new_ascii_instr = "and";
  687. if (instr_rdcycle) new_ascii_instr = "rdcycle";
  688. if (instr_rdcycleh) new_ascii_instr = "rdcycleh";
  689. if (instr_rdinstr) new_ascii_instr = "rdinstr";
  690. if (instr_rdinstrh) new_ascii_instr = "rdinstrh";
  691. if (instr_addqxi) new_ascii_instr = "addqxi";
  692. if (instr_addxqi) new_ascii_instr = "addxqi";
  693. if (instr_retirq) new_ascii_instr = "retirq";
  694. if (instr_maskirq) new_ascii_instr = "maskirq";
  695. if (instr_waitirq) new_ascii_instr = "waitirq";
  696. if (instr_timer) new_ascii_instr = "timer";
  697. end
  698. reg [63:0] q_ascii_instr;
  699. reg [31:0] q_insn_imm;
  700. reg [31:0] q_insn_opcode;
  701. reg [4:0] q_insn_rs1;
  702. reg [4:0] q_insn_rs2;
  703. reg [4:0] q_insn_rd;
  704. reg dbg_next;
  705. wire launch_next_insn;
  706. reg dbg_valid_insn;
  707. reg [63:0] cached_ascii_instr;
  708. reg [31:0] cached_insn_imm;
  709. reg [31:0] cached_insn_opcode;
  710. reg [4:0] cached_insn_rs1;
  711. reg [4:0] cached_insn_rs2;
  712. reg [4:0] cached_insn_rd;
  713. always @(posedge clk) begin
  714. q_ascii_instr <= dbg_ascii_instr;
  715. q_insn_imm <= dbg_insn_imm;
  716. q_insn_opcode <= dbg_insn_opcode;
  717. q_insn_rs1 <= dbg_insn_rs1;
  718. q_insn_rs2 <= dbg_insn_rs2;
  719. q_insn_rd <= dbg_insn_rd;
  720. dbg_next <= launch_next_insn;
  721. if (!resetn || trap)
  722. dbg_valid_insn <= 0;
  723. else if (launch_next_insn)
  724. dbg_valid_insn <= 1;
  725. if (decoder_trigger_q) begin
  726. cached_ascii_instr <= new_ascii_instr;
  727. cached_insn_imm <= decoded_imm;
  728. if (&next_insn_opcode[1:0])
  729. cached_insn_opcode <= next_insn_opcode;
  730. else
  731. cached_insn_opcode <= {16'b0, next_insn_opcode[15:0]};
  732. cached_insn_rs1 <= decoded_rs1;
  733. cached_insn_rs2 <= decoded_rs2;
  734. cached_insn_rd <= decoded_rd;
  735. end
  736. if (launch_next_insn) begin
  737. dbg_insn_addr <= next_pc;
  738. end
  739. end
  740. always @* begin
  741. dbg_ascii_instr = q_ascii_instr;
  742. dbg_insn_imm = q_insn_imm;
  743. dbg_insn_opcode = q_insn_opcode;
  744. dbg_insn_rs1 = q_insn_rs1;
  745. dbg_insn_rs2 = q_insn_rs2;
  746. dbg_insn_rd = q_insn_rd;
  747. if (dbg_next) begin
  748. if (decoder_pseudo_trigger_q) begin
  749. dbg_ascii_instr = cached_ascii_instr;
  750. dbg_insn_imm = cached_insn_imm;
  751. dbg_insn_opcode = cached_insn_opcode;
  752. dbg_insn_rs1 = cached_insn_rs1;
  753. dbg_insn_rs2 = cached_insn_rs2;
  754. dbg_insn_rd = cached_insn_rd;
  755. end else begin
  756. dbg_ascii_instr = new_ascii_instr;
  757. if (&next_insn_opcode[1:0])
  758. dbg_insn_opcode = next_insn_opcode;
  759. else
  760. dbg_insn_opcode = {16'b0, next_insn_opcode[15:0]};
  761. dbg_insn_imm = decoded_imm;
  762. dbg_insn_rs1 = decoded_rs1;
  763. dbg_insn_rs2 = decoded_rs2;
  764. dbg_insn_rd = decoded_rd;
  765. end
  766. end
  767. end
  768. `ifdef DEBUGASM
  769. always @(posedge clk) begin
  770. if (dbg_next) begin
  771. $display("debugasm %x %x %s", dbg_insn_addr, dbg_insn_opcode, dbg_ascii_instr ? dbg_ascii_instr : "*");
  772. end
  773. end
  774. `endif
  775. `ifdef DEBUG
  776. always @(posedge clk) begin
  777. if (dbg_next) begin
  778. if (&dbg_insn_opcode[1:0])
  779. $display("DECODE: 0x%08x 0x%08x %-0s", dbg_insn_addr, dbg_insn_opcode, dbg_ascii_instr ? dbg_ascii_instr : "UNKNOWN");
  780. else
  781. $display("DECODE: 0x%08x 0x%04x %-0s", dbg_insn_addr, dbg_insn_opcode[15:0], dbg_ascii_instr ? dbg_ascii_instr : "UNKNOWN");
  782. end
  783. end
  784. `endif
  785. // hpa: retirq opcode changed to mret, so
  786. // __attribute__((interrupt)) works in gcc
  787. wire instr_la_retirq = ENABLE_IRQ &&
  788. (mem_rdata_latched[6:0] == 7'b1110011 && mem_rdata_latched[31:25] == 7'b0011000);
  789. always @(posedge clk) begin
  790. is_lui_auipc_jal <= |{instr_lui, instr_auipc, instr_jal};
  791. is_lui_auipc_jal_jalr_addi_add_sub_addqxi <= |{instr_lui, instr_auipc, instr_jal, instr_jalr, instr_addi, instr_add, instr_sub, instr_addqxi};
  792. is_slti_blt_slt <= |{instr_slti, instr_blt, instr_slt};
  793. is_sltiu_bltu_sltu <= |{instr_sltiu, instr_bltu, instr_sltu};
  794. is_lbu_lhu_lw <= |{instr_lbu, instr_lhu, instr_lw};
  795. is_compare <= |{is_beq_bne_blt_bge_bltu_bgeu, instr_slti, instr_slt, instr_sltiu, instr_sltu};
  796. if (mem_do_rinst && mem_done) begin
  797. instr_lui <= mem_rdata_latched[6:0] == 7'b0110111;
  798. instr_auipc <= mem_rdata_latched[6:0] == 7'b0010111;
  799. instr_jal <= mem_rdata_latched[6:0] == 7'b1101111;
  800. instr_jalr <= mem_rdata_latched[6:0] == 7'b1100111 && mem_rdata_latched[14:12] == 3'b000;
  801. instr_retirq <= instr_la_retirq;
  802. instr_waitirq <= mem_rdata_latched[6:0] == 7'b0001011 && mem_rdata_latched[14:12] == 3'b000 && mem_rdata_latched[31:25] == 7'b0000100 && ENABLE_IRQ;
  803. is_beq_bne_blt_bge_bltu_bgeu <= mem_rdata_latched[6:0] == 7'b1100011;
  804. is_lb_lh_lw_lbu_lhu <= mem_rdata_latched[6:0] == 7'b0000011;
  805. is_sb_sh_sw <= mem_rdata_latched[6:0] == 7'b0100011;
  806. is_alu_reg_imm <= mem_rdata_latched[6:0] == 7'b0010011;
  807. is_alu_reg_reg <= mem_rdata_latched[6:0] == 7'b0110011;
  808. { decoded_imm_j[31:20], decoded_imm_j[10:1], decoded_imm_j[11], decoded_imm_j[19:12], decoded_imm_j[0] } <= $signed({mem_rdata_latched[31:12], 1'b0});
  809. decoded_rd <= mem_rdata_latched[11:7];
  810. decoded_rs1 <= mem_rdata_latched[19:15];
  811. decoded_rs2 <= mem_rdata_latched[24:20];
  812. if (instr_la_retirq)
  813. decoded_rs1 <= RA_IRQ_REG;
  814. compressed_instr <= 0;
  815. if (COMPRESSED_ISA && mem_rdata_latched[1:0] != 2'b11) begin
  816. compressed_instr <= 1;
  817. decoded_rd <= 0;
  818. decoded_rs1 <= 0;
  819. decoded_rs2 <= 0;
  820. { decoded_imm_j[31:11], decoded_imm_j[4], decoded_imm_j[9:8], decoded_imm_j[10], decoded_imm_j[6],
  821. decoded_imm_j[7], decoded_imm_j[3:1], decoded_imm_j[5], decoded_imm_j[0] } <= $signed({mem_rdata_latched[12:2], 1'b0});
  822. case (mem_rdata_latched[1:0])
  823. 2'b00: begin // Quadrant 0
  824. case (mem_rdata_latched[15:13])
  825. 3'b000: begin // C.ADDI4SPN
  826. is_alu_reg_imm <= |mem_rdata_latched[12:5];
  827. decoded_rs1 <= 2;
  828. decoded_rd <= 8 + mem_rdata_latched[4:2];
  829. end
  830. 3'b010: begin // C.LW
  831. is_lb_lh_lw_lbu_lhu <= 1;
  832. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  833. decoded_rd <= 8 + mem_rdata_latched[4:2];
  834. end
  835. 3'b110: begin // C.SW
  836. is_sb_sh_sw <= 1;
  837. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  838. decoded_rs2 <= 8 + mem_rdata_latched[4:2];
  839. end
  840. endcase
  841. end
  842. 2'b01: begin // Quadrant 1
  843. case (mem_rdata_latched[15:13])
  844. 3'b000: begin // C.NOP / C.ADDI
  845. is_alu_reg_imm <= 1;
  846. decoded_rd <= mem_rdata_latched[11:7];
  847. decoded_rs1 <= mem_rdata_latched[11:7];
  848. end
  849. 3'b001: begin // C.JAL
  850. instr_jal <= 1;
  851. decoded_rd <= 1;
  852. end
  853. 3'b 010: begin // C.LI
  854. is_alu_reg_imm <= 1;
  855. decoded_rd <= mem_rdata_latched[11:7];
  856. decoded_rs1 <= 0;
  857. end
  858. 3'b 011: begin
  859. if (mem_rdata_latched[12] || mem_rdata_latched[6:2]) begin
  860. if (mem_rdata_latched[11:7] == 2) begin // C.ADDI16SP
  861. is_alu_reg_imm <= 1;
  862. decoded_rd <= mem_rdata_latched[11:7];
  863. decoded_rs1 <= mem_rdata_latched[11:7];
  864. end else begin // C.LUI
  865. instr_lui <= 1;
  866. decoded_rd <= mem_rdata_latched[11:7];
  867. decoded_rs1 <= 0;
  868. end
  869. end
  870. end
  871. 3'b100: begin
  872. if (!mem_rdata_latched[11] && !mem_rdata_latched[12]) begin // C.SRLI, C.SRAI
  873. is_alu_reg_imm <= 1;
  874. decoded_rd <= 8 + mem_rdata_latched[9:7];
  875. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  876. decoded_rs2 <= {mem_rdata_latched[12], mem_rdata_latched[6:2]};
  877. end
  878. if (mem_rdata_latched[11:10] == 2'b10) begin // C.ANDI
  879. is_alu_reg_imm <= 1;
  880. decoded_rd <= 8 + mem_rdata_latched[9:7];
  881. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  882. end
  883. if (mem_rdata_latched[12:10] == 3'b011) begin // C.SUB, C.XOR, C.OR, C.AND
  884. is_alu_reg_reg <= 1;
  885. decoded_rd <= 8 + mem_rdata_latched[9:7];
  886. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  887. decoded_rs2 <= 8 + mem_rdata_latched[4:2];
  888. end
  889. end
  890. 3'b101: begin // C.J
  891. instr_jal <= 1;
  892. end
  893. 3'b110: begin // C.BEQZ
  894. is_beq_bne_blt_bge_bltu_bgeu <= 1;
  895. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  896. decoded_rs2 <= 0;
  897. end
  898. 3'b111: begin // C.BNEZ
  899. is_beq_bne_blt_bge_bltu_bgeu <= 1;
  900. decoded_rs1 <= 8 + mem_rdata_latched[9:7];
  901. decoded_rs2 <= 0;
  902. end
  903. endcase
  904. end
  905. 2'b10: begin // Quadrant 2
  906. case (mem_rdata_latched[15:13])
  907. 3'b000: begin // C.SLLI
  908. if (!mem_rdata_latched[12]) begin
  909. is_alu_reg_imm <= 1;
  910. decoded_rd <= mem_rdata_latched[11:7];
  911. decoded_rs1 <= mem_rdata_latched[11:7];
  912. decoded_rs2 <= {mem_rdata_latched[12], mem_rdata_latched[6:2]};
  913. end
  914. end
  915. 3'b010: begin // C.LWSP
  916. if (mem_rdata_latched[11:7]) begin
  917. is_lb_lh_lw_lbu_lhu <= 1;
  918. decoded_rd <= mem_rdata_latched[11:7];
  919. decoded_rs1 <= 2;
  920. end
  921. end
  922. 3'b100: begin
  923. if (mem_rdata_latched[12] == 0 && mem_rdata_latched[11:7] != 0 && mem_rdata_latched[6:2] == 0) begin // C.JR
  924. instr_jalr <= 1;
  925. decoded_rd <= 0;
  926. decoded_rs1 <= mem_rdata_latched[11:7];
  927. end
  928. if (mem_rdata_latched[12] == 0 && mem_rdata_latched[6:2] != 0) begin // C.MV
  929. is_alu_reg_reg <= 1;
  930. decoded_rd <= mem_rdata_latched[11:7];
  931. decoded_rs1 <= 0;
  932. decoded_rs2 <= mem_rdata_latched[6:2];
  933. end
  934. if (mem_rdata_latched[12] != 0 && mem_rdata_latched[11:7] != 0 && mem_rdata_latched[6:2] == 0) begin // C.JALR
  935. instr_jalr <= 1;
  936. decoded_rd <= 1;
  937. decoded_rs1 <= mem_rdata_latched[11:7];
  938. end
  939. if (mem_rdata_latched[12] != 0 && mem_rdata_latched[6:2] != 0) begin // C.ADD
  940. is_alu_reg_reg <= 1;
  941. decoded_rd <= mem_rdata_latched[11:7];
  942. decoded_rs1 <= mem_rdata_latched[11:7];
  943. decoded_rs2 <= mem_rdata_latched[6:2];
  944. end
  945. end
  946. 3'b110: begin // C.SWSP
  947. is_sb_sh_sw <= 1;
  948. decoded_rs1 <= 2;
  949. decoded_rs2 <= mem_rdata_latched[6:2];
  950. end
  951. endcase
  952. end
  953. endcase
  954. end
  955. // hpa: IRQ bank switch support
  956. is_addqxi <= 0;
  957. if (ENABLE_IRQ && ENABLE_IRQ_QREGS)
  958. begin
  959. decoded_rd [regindex_bits-1] <= irq_active;
  960. decoded_rs1[regindex_bits-1] <= irq_active;
  961. decoded_rs2[regindex_bits-1] <= irq_active;
  962. // addqxi, addxqi
  963. if (mem_rdata_latched[6:0] == 7'b0001011 && mem_rdata_latched[14:13] == 2'b01) begin
  964. is_addqxi <= 1; // True for both addqxi and addxqi
  965. decoded_rd [regindex_bits-1] <= ~mem_rdata_latched[12]; // addxqi
  966. decoded_rs1[regindex_bits-1] <= mem_rdata_latched[12]; // addqxi
  967. end
  968. end
  969. end // if (mem_do_rinst && mem_done)
  970. if (decoder_trigger && !decoder_pseudo_trigger) begin
  971. pcpi_insn <= WITH_PCPI ? mem_rdata_q : 'bx;
  972. instr_beq <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b000;
  973. instr_bne <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b001;
  974. instr_blt <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b100;
  975. instr_bge <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b101;
  976. instr_bltu <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b110;
  977. instr_bgeu <= is_beq_bne_blt_bge_bltu_bgeu && mem_rdata_q[14:12] == 3'b111;
  978. instr_lb <= is_lb_lh_lw_lbu_lhu && mem_rdata_q[14:12] == 3'b000;
  979. instr_lh <= is_lb_lh_lw_lbu_lhu && mem_rdata_q[14:12] == 3'b001;
  980. instr_lw <= is_lb_lh_lw_lbu_lhu && mem_rdata_q[14:12] == 3'b010;
  981. instr_lbu <= is_lb_lh_lw_lbu_lhu && mem_rdata_q[14:12] == 3'b100;
  982. instr_lhu <= is_lb_lh_lw_lbu_lhu && mem_rdata_q[14:12] == 3'b101;
  983. instr_sb <= is_sb_sh_sw && mem_rdata_q[14:12] == 3'b000;
  984. instr_sh <= is_sb_sh_sw && mem_rdata_q[14:12] == 3'b001;
  985. instr_sw <= is_sb_sh_sw && mem_rdata_q[14:12] == 3'b010;
  986. instr_addi <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b000;
  987. instr_slti <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b010;
  988. instr_sltiu <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b011;
  989. instr_xori <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b100;
  990. instr_ori <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b110;
  991. instr_andi <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b111;
  992. instr_slli <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b001 && mem_rdata_q[31:25] == 7'b0000000;
  993. instr_srli <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0000000;
  994. instr_srai <= is_alu_reg_imm && mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0100000;
  995. instr_add <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b000 && mem_rdata_q[31:25] == 7'b0000000;
  996. instr_sub <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b000 && mem_rdata_q[31:25] == 7'b0100000;
  997. instr_sll <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b001 && mem_rdata_q[31:25] == 7'b0000000;
  998. instr_slt <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b010 && mem_rdata_q[31:25] == 7'b0000000;
  999. instr_sltu <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b011 && mem_rdata_q[31:25] == 7'b0000000;
  1000. instr_xor <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b100 && mem_rdata_q[31:25] == 7'b0000000;
  1001. instr_srl <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0000000;
  1002. instr_sra <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0100000;
  1003. instr_or <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b110 && mem_rdata_q[31:25] == 7'b0000000;
  1004. instr_and <= is_alu_reg_reg && mem_rdata_q[14:12] == 3'b111 && mem_rdata_q[31:25] == 7'b0000000;
  1005. instr_rdcycle <= ((mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11000000000000000010) ||
  1006. (mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11000000000100000010)) && ENABLE_COUNTERS;
  1007. instr_rdcycleh <= ((mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11001000000000000010) ||
  1008. (mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11001000000100000010)) && ENABLE_COUNTERS && ENABLE_COUNTERS64;
  1009. instr_rdinstr <= (mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11000000001000000010) && ENABLE_COUNTERS;
  1010. instr_rdinstrh <= (mem_rdata_q[6:0] == 7'b1110011 && mem_rdata_q[31:12] == 'b11001000001000000010) && ENABLE_COUNTERS && ENABLE_COUNTERS64;
  1011. instr_ecall_ebreak <= ((mem_rdata_q[6:0] == 7'b1110011 && !mem_rdata_q[31:21] && !mem_rdata_q[19:7]) ||
  1012. (COMPRESSED_ISA && mem_rdata_q[15:0] == 16'h9002));
  1013. instr_maskirq <= mem_rdata_q[6:0] == 7'b0001011 && mem_rdata_q[14:12] == 3'b000 && mem_rdata_q[31:25] == 7'b0000011 && ENABLE_IRQ;
  1014. instr_timer <= mem_rdata_q[6:0] == 7'b0001011 && mem_rdata_q[14:12] == 3'b000 && mem_rdata_q[31:25] == 7'b0000101 && ENABLE_IRQ && ENABLE_IRQ_TIMER;
  1015. // instr_addqxi includes addxqi; instr_addxqi is only used for debug
  1016. instr_addqxi <= mem_rdata_q[6:0] == 7'b0001011 && mem_rdata_q[14:13] == 2'b01 && ENABLE_IRQ && ENABLE_IRQ_QREGS;
  1017. instr_addxqi <= mem_rdata_q[6:0] == 7'b0001011 && mem_rdata_q[14:12] == 3'b011 && ENABLE_IRQ && ENABLE_IRQ_QREGS;
  1018. is_slli_srli_srai <= is_alu_reg_imm && |{
  1019. mem_rdata_q[14:12] == 3'b001 && mem_rdata_q[31:25] == 7'b0000000,
  1020. mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0000000,
  1021. mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0100000
  1022. };
  1023. is_jalr_addi_slti_sltiu_xori_ori_andi_addqxi <= instr_jalr || is_addqxi || is_alu_reg_imm && |{
  1024. mem_rdata_q[14:12] == 3'b000,
  1025. mem_rdata_q[14:12] == 3'b010,
  1026. mem_rdata_q[14:12] == 3'b011,
  1027. mem_rdata_q[14:12] == 3'b100,
  1028. mem_rdata_q[14:12] == 3'b110,
  1029. mem_rdata_q[14:12] == 3'b111
  1030. };
  1031. is_sll_srl_sra <= is_alu_reg_reg && |{
  1032. mem_rdata_q[14:12] == 3'b001 && mem_rdata_q[31:25] == 7'b0000000,
  1033. mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0000000,
  1034. mem_rdata_q[14:12] == 3'b101 && mem_rdata_q[31:25] == 7'b0100000
  1035. };
  1036. is_lui_auipc_jal_jalr_addi_add_sub_addqxi <= 0;
  1037. is_compare <= 0;
  1038. (* parallel_case *)
  1039. case (1'b1)
  1040. instr_jal:
  1041. decoded_imm <= decoded_imm_j;
  1042. |{instr_lui, instr_auipc}:
  1043. decoded_imm <= mem_rdata_q[31:12] << 12;
  1044. |{instr_jalr, is_lb_lh_lw_lbu_lhu, is_alu_reg_imm, is_addqxi}:
  1045. decoded_imm <= $signed(mem_rdata_q[31:20]);
  1046. is_beq_bne_blt_bge_bltu_bgeu:
  1047. decoded_imm <= $signed({mem_rdata_q[31], mem_rdata_q[7], mem_rdata_q[30:25], mem_rdata_q[11:8], 1'b0});
  1048. is_sb_sh_sw:
  1049. decoded_imm <= $signed({mem_rdata_q[31:25], mem_rdata_q[11:7]});
  1050. default:
  1051. decoded_imm <= 1'bx;
  1052. endcase
  1053. end
  1054. if (!resetn) begin
  1055. is_beq_bne_blt_bge_bltu_bgeu <= 0;
  1056. is_compare <= 0;
  1057. instr_beq <= 0;
  1058. instr_bne <= 0;
  1059. instr_blt <= 0;
  1060. instr_bge <= 0;
  1061. instr_bltu <= 0;
  1062. instr_bgeu <= 0;
  1063. instr_addi <= 0;
  1064. instr_slti <= 0;
  1065. instr_sltiu <= 0;
  1066. instr_xori <= 0;
  1067. instr_ori <= 0;
  1068. instr_andi <= 0;
  1069. instr_add <= 0;
  1070. instr_sub <= 0;
  1071. instr_sll <= 0;
  1072. instr_slt <= 0;
  1073. instr_sltu <= 0;
  1074. instr_xor <= 0;
  1075. instr_srl <= 0;
  1076. instr_sra <= 0;
  1077. instr_or <= 0;
  1078. instr_and <= 0;
  1079. instr_addqxi <= 0;
  1080. end
  1081. end
  1082. // Main State Machine
  1083. localparam cpu_state_trap = 8'b10000000;
  1084. localparam cpu_state_fetch = 8'b01000000;
  1085. localparam cpu_state_ld_rs1 = 8'b00100000;
  1086. localparam cpu_state_ld_rs2 = 8'b00010000;
  1087. localparam cpu_state_exec = 8'b00001000;
  1088. localparam cpu_state_shift = 8'b00000100;
  1089. localparam cpu_state_stmem = 8'b00000010;
  1090. localparam cpu_state_ldmem = 8'b00000001;
  1091. reg [7:0] cpu_state;
  1092. reg [1:0] irq_state;
  1093. `FORMAL_KEEP reg [127:0] dbg_ascii_state;
  1094. always @* begin
  1095. dbg_ascii_state = "";
  1096. if (cpu_state == cpu_state_trap) dbg_ascii_state = "trap";
  1097. if (cpu_state == cpu_state_fetch) dbg_ascii_state = "fetch";
  1098. if (cpu_state == cpu_state_ld_rs1) dbg_ascii_state = "ld_rs1";
  1099. if (cpu_state == cpu_state_ld_rs2) dbg_ascii_state = "ld_rs2";
  1100. if (cpu_state == cpu_state_exec) dbg_ascii_state = "exec";
  1101. if (cpu_state == cpu_state_shift) dbg_ascii_state = "shift";
  1102. if (cpu_state == cpu_state_stmem) dbg_ascii_state = "stmem";
  1103. if (cpu_state == cpu_state_ldmem) dbg_ascii_state = "ldmem";
  1104. end
  1105. reg set_mem_do_rinst;
  1106. reg set_mem_do_rdata;
  1107. reg set_mem_do_wdata;
  1108. reg latched_store;
  1109. reg latched_stalu;
  1110. reg latched_branch;
  1111. reg latched_compr;
  1112. reg latched_trace;
  1113. reg latched_is_lu;
  1114. reg latched_is_lh;
  1115. reg latched_is_lb;
  1116. reg [regindex_bits-1:0] latched_rd;
  1117. reg [31:0] current_pc;
  1118. assign next_pc = latched_store && latched_branch ? reg_out & ~1 : reg_next_pc;
  1119. reg [3:0] pcpi_timeout_counter;
  1120. reg pcpi_timeout;
  1121. reg [31:0] next_irq_pending;
  1122. reg do_waitirq;
  1123. reg [31:0] alu_out, alu_out_q;
  1124. reg alu_out_0, alu_out_0_q;
  1125. reg alu_wait, alu_wait_2;
  1126. reg [31:0] alu_add_sub;
  1127. reg [31:0] alu_shl, alu_shr;
  1128. reg alu_eq, alu_ltu, alu_lts;
  1129. generate if (TWO_CYCLE_ALU) begin
  1130. always @(posedge clk) begin
  1131. alu_add_sub <= instr_sub ? reg_op1 - reg_op2 : reg_op1 + reg_op2;
  1132. alu_eq <= reg_op1 == reg_op2;
  1133. alu_lts <= $signed(reg_op1) < $signed(reg_op2);
  1134. alu_ltu <= reg_op1 < reg_op2;
  1135. alu_shl <= reg_op1 << reg_op2[4:0];
  1136. alu_shr <= $signed({instr_sra || instr_srai ? reg_op1[31] : 1'b0, reg_op1}) >>> reg_op2[4:0];
  1137. end
  1138. end else begin
  1139. always @* begin
  1140. alu_add_sub = instr_sub ? reg_op1 - reg_op2 : reg_op1 + reg_op2;
  1141. alu_eq = reg_op1 == reg_op2;
  1142. alu_lts = $signed(reg_op1) < $signed(reg_op2);
  1143. alu_ltu = reg_op1 < reg_op2;
  1144. alu_shl = reg_op1 << reg_op2[4:0];
  1145. alu_shr = $signed({instr_sra || instr_srai ? reg_op1[31] : 1'b0, reg_op1}) >>> reg_op2[4:0];
  1146. end
  1147. end endgenerate
  1148. always @* begin
  1149. alu_out_0 = 'bx;
  1150. (* parallel_case, full_case *)
  1151. case (1'b1)
  1152. instr_beq:
  1153. alu_out_0 = alu_eq;
  1154. instr_bne:
  1155. alu_out_0 = !alu_eq;
  1156. instr_bge:
  1157. alu_out_0 = !alu_lts;
  1158. instr_bgeu:
  1159. alu_out_0 = !alu_ltu;
  1160. is_slti_blt_slt && (!TWO_CYCLE_COMPARE || !{instr_beq,instr_bne,instr_bge,instr_bgeu}):
  1161. alu_out_0 = alu_lts;
  1162. is_sltiu_bltu_sltu && (!TWO_CYCLE_COMPARE || !{instr_beq,instr_bne,instr_bge,instr_bgeu}):
  1163. alu_out_0 = alu_ltu;
  1164. endcase
  1165. alu_out = 'bx;
  1166. (* parallel_case, full_case *)
  1167. case (1'b1)
  1168. is_lui_auipc_jal_jalr_addi_add_sub_addqxi:
  1169. alu_out = alu_add_sub;
  1170. is_compare:
  1171. alu_out = alu_out_0;
  1172. instr_xori || instr_xor:
  1173. alu_out = reg_op1 ^ reg_op2;
  1174. instr_ori || instr_or:
  1175. alu_out = reg_op1 | reg_op2;
  1176. instr_andi || instr_and:
  1177. alu_out = reg_op1 & reg_op2;
  1178. BARREL_SHIFTER && (instr_sll || instr_slli):
  1179. alu_out = alu_shl;
  1180. BARREL_SHIFTER && (instr_srl || instr_srli || instr_sra || instr_srai):
  1181. alu_out = alu_shr;
  1182. endcase
  1183. `ifdef RISCV_FORMAL_BLACKBOX_ALU
  1184. alu_out_0 = $anyseq;
  1185. alu_out = $anyseq;
  1186. `endif
  1187. end
  1188. reg clear_prefetched_high_word_q;
  1189. always @(posedge clk) clear_prefetched_high_word_q <= clear_prefetched_high_word;
  1190. always @* begin
  1191. clear_prefetched_high_word = clear_prefetched_high_word_q;
  1192. if (!prefetched_high_word)
  1193. clear_prefetched_high_word = 0;
  1194. if (latched_branch || irq_state || !resetn)
  1195. clear_prefetched_high_word = COMPRESSED_ISA;
  1196. end
  1197. reg cpuregs_write;
  1198. reg [31:0] cpuregs_wrdata;
  1199. reg [31:0] cpuregs_rs1;
  1200. reg [31:0] cpuregs_rs2;
  1201. reg [regindex_bits-1:0] decoded_rs;
  1202. always @* begin
  1203. cpuregs_write = 0;
  1204. cpuregs_wrdata = 'bx;
  1205. if (cpu_state == cpu_state_fetch) begin
  1206. (* parallel_case *)
  1207. case (1'b1)
  1208. latched_branch: begin
  1209. cpuregs_wrdata = reg_pc + (latched_compr ? 2 : 4);
  1210. cpuregs_write = 1;
  1211. end
  1212. latched_store && !latched_branch: begin
  1213. cpuregs_wrdata = latched_stalu ? alu_out_q : reg_out;
  1214. cpuregs_write = 1;
  1215. end
  1216. ENABLE_IRQ && irq_state[0]: begin
  1217. cpuregs_wrdata = reg_next_pc | latched_compr;
  1218. cpuregs_write = 1;
  1219. end
  1220. ENABLE_IRQ && irq_state[1]: begin
  1221. cpuregs_wrdata = irq_pending & ~irq_mask;
  1222. cpuregs_write = 1;
  1223. end
  1224. endcase
  1225. end
  1226. end
  1227. `ifndef PICORV32_REGS
  1228. always @(posedge clk) begin
  1229. if (resetn && cpuregs_write && (latched_rd & 5'h1f))
  1230. `ifdef PICORV32_TESTBUG_001
  1231. cpuregs[latched_rd ^ 1] <= cpuregs_wrdata;
  1232. `elsif PICORV32_TESTBUG_002
  1233. cpuregs[latched_rd] <= cpuregs_wrdata ^ 1;
  1234. `else
  1235. cpuregs[latched_rd] <= cpuregs_wrdata;
  1236. `endif
  1237. end
  1238. // hpa: if REGS_INIT_ZERO, then there is no reason not to simply
  1239. // read from the register file even for x0; the above code
  1240. // ensures that we never *write* to x0, which is a simple
  1241. // write enable thing.
  1242. always @* begin
  1243. decoded_rs = 'bx;
  1244. if (ENABLE_REGS_DUALPORT) begin
  1245. `ifndef RISCV_FORMAL_BLACKBOX_REGS
  1246. cpuregs_rs1 = cpuregs[decoded_rs1];
  1247. cpuregs_rs2 = cpuregs[decoded_rs2];
  1248. if (!REGS_INIT_ZERO) begin
  1249. if (!(decoded_rs1 & 5'h1f)) cpuregs_rs1 = 32'h0;
  1250. if (!(decoded_rs2 & 5'h1f)) cpuregs_rs2 = 32'h0;
  1251. end
  1252. `else
  1253. cpuregs_rs1 = (decoded_rs1 & 5'h1f) ? $anyseq : 32'h0;
  1254. cpuregs_rs2 = (decoded_rs2 & 5'h1f) ? $anyseq : 32'h0;
  1255. `endif
  1256. end else begin
  1257. decoded_rs = (cpu_state == cpu_state_ld_rs2) ? decoded_rs2 : decoded_rs1;
  1258. `ifndef RISCV_FORMAL_BLACKBOX_REGS
  1259. cpuregs_rs1 = cpuregs[decoded_rs];
  1260. if (!REGS_INIT_ZERO)
  1261. if (!(decoded_rs & 5'h1f)) cpuregs_rs1 = 32'h0;
  1262. `else
  1263. cpuregs_rs1 = decoded_rs & 5'h1f ? $anyseq : 0;
  1264. `endif
  1265. cpuregs_rs2 = cpuregs_rs1;
  1266. end
  1267. end
  1268. `else
  1269. wire[31:0] cpuregs_rdata1;
  1270. wire[31:0] cpuregs_rdata2;
  1271. wire [5:0] cpuregs_waddr = latched_rd;
  1272. wire [5:0] cpuregs_raddr1 = ENABLE_REGS_DUALPORT ? decoded_rs1 : decoded_rs;
  1273. wire [5:0] cpuregs_raddr2 = ENABLE_REGS_DUALPORT ? decoded_rs2 : 0;
  1274. `PICORV32_REGS cpuregs (
  1275. .clk(clk),
  1276. .wen(resetn && cpuregs_write && latched_rd),
  1277. .waddr(cpuregs_waddr),
  1278. .raddr1(cpuregs_raddr1),
  1279. .raddr2(cpuregs_raddr2),
  1280. .wdata(cpuregs_wrdata),
  1281. .rdata1(cpuregs_rdata1),
  1282. .rdata2(cpuregs_rdata2)
  1283. );
  1284. always @* begin
  1285. decoded_rs = 'bx;
  1286. if (ENABLE_REGS_DUALPORT) begin
  1287. cpuregs_rs1 = decoded_rs1 & 4'h1f ? cpuregs_rdata1 : 0;
  1288. cpuregs_rs2 = decoded_rs2 & 4'h1f ? cpuregs_rdata2 : 0;
  1289. end else begin
  1290. decoded_rs = (cpu_state == cpu_state_ld_rs2) ? decoded_rs2 : decoded_rs1;
  1291. cpuregs_rs1 = decoded_rs & 4'h1f ? cpuregs_rdata1 : 0;
  1292. cpuregs_rs2 = cpuregs_rs1;
  1293. end
  1294. end
  1295. `endif
  1296. assign launch_next_insn = cpu_state == cpu_state_fetch && decoder_trigger && (!ENABLE_IRQ || irq_delay || irq_active || !(irq_pending & ~irq_mask));
  1297. always @(posedge clk) begin
  1298. trap <= 0;
  1299. reg_sh <= 'bx;
  1300. reg_out <= 'bx;
  1301. set_mem_do_rinst = 0;
  1302. set_mem_do_rdata = 0;
  1303. set_mem_do_wdata = 0;
  1304. alu_out_0_q <= alu_out_0;
  1305. alu_out_q <= alu_out;
  1306. alu_wait <= 0;
  1307. alu_wait_2 <= 0;
  1308. if (launch_next_insn) begin
  1309. dbg_rs1val <= 'bx;
  1310. dbg_rs2val <= 'bx;
  1311. dbg_rs1val_valid <= 0;
  1312. dbg_rs2val_valid <= 0;
  1313. end
  1314. if (WITH_PCPI && CATCH_ILLINSN) begin
  1315. if (resetn && pcpi_valid && !pcpi_int_wait) begin
  1316. if (pcpi_timeout_counter)
  1317. pcpi_timeout_counter <= pcpi_timeout_counter - 1;
  1318. end else
  1319. pcpi_timeout_counter <= ~0;
  1320. pcpi_timeout <= !pcpi_timeout_counter;
  1321. end
  1322. if (ENABLE_COUNTERS) begin
  1323. count_cycle <= resetn ? count_cycle + 1 : 0;
  1324. if (!ENABLE_COUNTERS64) count_cycle[63:32] <= 0;
  1325. end else begin
  1326. count_cycle <= 'bx;
  1327. count_instr <= 'bx;
  1328. end
  1329. next_irq_pending = ENABLE_IRQ ? (irq_pending & LATCHED_IRQ & ~MASKED_IRQ) : 'bx;
  1330. if (ENABLE_IRQ && ENABLE_IRQ_TIMER && timer) begin
  1331. timer <= timer - 1;
  1332. end
  1333. decoder_trigger <= mem_do_rinst && mem_done;
  1334. decoder_trigger_q <= decoder_trigger;
  1335. decoder_pseudo_trigger <= 0;
  1336. decoder_pseudo_trigger_q <= decoder_pseudo_trigger;
  1337. do_waitirq <= 0;
  1338. trace_valid <= 0;
  1339. if (!ENABLE_TRACE)
  1340. trace_data <= 'bx;
  1341. if (!resetn) begin
  1342. reg_pc <= progaddr_reset;
  1343. reg_next_pc <= progaddr_reset;
  1344. if (ENABLE_COUNTERS)
  1345. count_instr <= 0;
  1346. latched_store <= 0;
  1347. latched_stalu <= 0;
  1348. latched_branch <= 0;
  1349. latched_trace <= 0;
  1350. latched_is_lu <= 0;
  1351. latched_is_lh <= 0;
  1352. latched_is_lb <= 0;
  1353. pcpi_valid <= 0;
  1354. pcpi_timeout <= 0;
  1355. irq_active <= 0;
  1356. irq_delay <= 0;
  1357. irq_mask <= ~0;
  1358. next_irq_pending = 0;
  1359. irq_state <= 0;
  1360. eoi <= 0;
  1361. timer <= 0;
  1362. if (~STACKADDR) begin
  1363. latched_store <= 1;
  1364. latched_rd <= 2;
  1365. reg_out <= STACKADDR;
  1366. end
  1367. cpu_state <= cpu_state_fetch;
  1368. end else
  1369. (* parallel_case, full_case *)
  1370. case (cpu_state)
  1371. cpu_state_trap: begin
  1372. trap <= 1;
  1373. end
  1374. cpu_state_fetch: begin
  1375. mem_do_rinst <= !decoder_trigger && !do_waitirq;
  1376. mem_wordsize <= 0;
  1377. current_pc = reg_next_pc;
  1378. (* parallel_case *)
  1379. case (1'b1)
  1380. latched_branch: begin
  1381. current_pc = latched_store ? (latched_stalu ? alu_out_q : reg_out) & ~1 : reg_next_pc;
  1382. `debug($display("ST_RD: %2d 0x%08x, BRANCH 0x%08x", latched_rd, reg_pc + (latched_compr ? 2 : 4), current_pc);)
  1383. end
  1384. latched_store && !latched_branch: begin
  1385. `debug($display("ST_RD: %2d 0x%08x", latched_rd, latched_stalu ? alu_out_q : reg_out);)
  1386. end
  1387. ENABLE_IRQ && irq_state[0]: begin
  1388. current_pc = progaddr_irq;
  1389. irq_active <= 1;
  1390. mem_do_rinst <= 1;
  1391. end
  1392. ENABLE_IRQ && irq_state[1]: begin
  1393. eoi <= irq_pending & ~irq_mask;
  1394. next_irq_pending = next_irq_pending & irq_mask;
  1395. end
  1396. endcase
  1397. if (ENABLE_TRACE && latched_trace) begin
  1398. latched_trace <= 0;
  1399. trace_valid <= 1;
  1400. if (latched_branch)
  1401. trace_data <= (irq_active ? TRACE_IRQ : 0) | TRACE_BRANCH | (current_pc & 32'hfffffffe);
  1402. else
  1403. trace_data <= (irq_active ? TRACE_IRQ : 0) | (latched_stalu ? alu_out_q : reg_out);
  1404. end
  1405. reg_pc <= current_pc;
  1406. reg_next_pc <= current_pc;
  1407. latched_store <= 0;
  1408. latched_stalu <= 0;
  1409. latched_branch <= 0;
  1410. latched_is_lu <= 0;
  1411. latched_is_lh <= 0;
  1412. latched_is_lb <= 0;
  1413. latched_rd <= decoded_rd;
  1414. latched_compr <= compressed_instr;
  1415. if (ENABLE_IRQ && ((decoder_trigger && !irq_active && !irq_delay && |(irq_pending & ~irq_mask)) || irq_state)) begin
  1416. irq_state <=
  1417. irq_state == 2'b00 ? 2'b01 :
  1418. irq_state == 2'b01 ? 2'b10 : 2'b00;
  1419. latched_compr <= latched_compr;
  1420. latched_rd <= qreg_offset |
  1421. (irq_state[0] ? MASK_IRQ_REG : RA_IRQ_REG);
  1422. end else
  1423. if (ENABLE_IRQ && (decoder_trigger || do_waitirq) && instr_waitirq) begin
  1424. if (irq_pending) begin
  1425. latched_store <= 1;
  1426. reg_out <= irq_pending;
  1427. reg_next_pc <= current_pc + (compressed_instr ? 2 : 4);
  1428. mem_do_rinst <= 1;
  1429. end else
  1430. do_waitirq <= 1;
  1431. end else
  1432. if (decoder_trigger) begin
  1433. `debug($display("-- %-0t pc: 0x%08x irq: %x", $time, current_pc, irq_active);)
  1434. irq_delay <= irq_active;
  1435. reg_next_pc <= current_pc + (compressed_instr ? 2 : 4);
  1436. if (ENABLE_TRACE)
  1437. latched_trace <= 1;
  1438. if (ENABLE_COUNTERS) begin
  1439. count_instr <= count_instr + 1;
  1440. if (!ENABLE_COUNTERS64) count_instr[63:32] <= 0;
  1441. end
  1442. if (instr_jal) begin
  1443. mem_do_rinst <= 1;
  1444. reg_next_pc <= current_pc + decoded_imm_j;
  1445. latched_branch <= 1;
  1446. end else begin
  1447. mem_do_rinst <= 0;
  1448. mem_do_prefetch <= !instr_jalr && !instr_retirq;
  1449. cpu_state <= cpu_state_ld_rs1;
  1450. end
  1451. end
  1452. end
  1453. cpu_state_ld_rs1: begin
  1454. reg_op1 <= 'bx;
  1455. reg_op2 <= 'bx;
  1456. (* parallel_case *)
  1457. case (1'b1)
  1458. (CATCH_ILLINSN || WITH_PCPI) && instr_trap: begin
  1459. if (WITH_PCPI) begin
  1460. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1461. reg_op1 <= cpuregs_rs1;
  1462. dbg_rs1val <= cpuregs_rs1;
  1463. dbg_rs1val_valid <= 1;
  1464. if (ENABLE_REGS_DUALPORT) begin
  1465. pcpi_valid <= 1;
  1466. `debug($display("LD_RS2: %2d 0x%08x", decoded_rs2, cpuregs_rs2);)
  1467. reg_sh <= cpuregs_rs2;
  1468. reg_op2 <= cpuregs_rs2;
  1469. dbg_rs2val <= cpuregs_rs2;
  1470. dbg_rs2val_valid <= 1;
  1471. if (pcpi_int_ready) begin
  1472. mem_do_rinst <= 1;
  1473. pcpi_valid <= 0;
  1474. reg_out <= pcpi_int_rd;
  1475. latched_store <= pcpi_int_wr;
  1476. cpu_state <= cpu_state_fetch;
  1477. end else
  1478. if (CATCH_ILLINSN && (pcpi_timeout || instr_ecall_ebreak)) begin
  1479. pcpi_valid <= 0;
  1480. `debug($display("EBREAK OR UNSUPPORTED INSN AT 0x%08x", reg_pc);)
  1481. if (ENABLE_IRQ && !irq_mask[irq_ebreak] && !irq_active) begin
  1482. next_irq_pending[irq_ebreak] = 1;
  1483. cpu_state <= cpu_state_fetch;
  1484. end else
  1485. cpu_state <= cpu_state_trap;
  1486. end
  1487. end else begin
  1488. cpu_state <= cpu_state_ld_rs2;
  1489. end
  1490. end else begin
  1491. `debug($display("EBREAK OR UNSUPPORTED INSN AT 0x%08x", reg_pc);)
  1492. if (ENABLE_IRQ && !irq_mask[irq_ebreak] && !irq_active) begin
  1493. next_irq_pending[irq_ebreak] = 1;
  1494. cpu_state <= cpu_state_fetch;
  1495. end else
  1496. cpu_state <= cpu_state_trap;
  1497. end
  1498. end
  1499. ENABLE_COUNTERS && is_rdcycle_rdcycleh_rdinstr_rdinstrh: begin
  1500. (* parallel_case, full_case *)
  1501. case (1'b1)
  1502. instr_rdcycle:
  1503. reg_out <= count_cycle[31:0];
  1504. instr_rdcycleh && ENABLE_COUNTERS64:
  1505. reg_out <= count_cycle[63:32];
  1506. instr_rdinstr:
  1507. reg_out <= count_instr[31:0];
  1508. instr_rdinstrh && ENABLE_COUNTERS64:
  1509. reg_out <= count_instr[63:32];
  1510. endcase
  1511. latched_store <= 1;
  1512. cpu_state <= cpu_state_fetch;
  1513. end
  1514. is_lui_auipc_jal: begin
  1515. reg_op1 <= instr_lui ? 0 : reg_pc;
  1516. reg_op2 <= decoded_imm;
  1517. if (TWO_CYCLE_ALU)
  1518. alu_wait <= 1;
  1519. else
  1520. mem_do_rinst <= mem_do_prefetch;
  1521. cpu_state <= cpu_state_exec;
  1522. end
  1523. ENABLE_IRQ && instr_retirq: begin
  1524. eoi <= 0;
  1525. irq_active <= 0;
  1526. latched_branch <= 1;
  1527. latched_store <= 1;
  1528. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1529. reg_out <= CATCH_MISALIGN ? (cpuregs_rs1 & 32'h fffffffe) : cpuregs_rs1;
  1530. dbg_rs1val <= cpuregs_rs1;
  1531. dbg_rs1val_valid <= 1;
  1532. cpu_state <= cpu_state_fetch;
  1533. end
  1534. ENABLE_IRQ && instr_maskirq: begin
  1535. latched_store <= 1;
  1536. reg_out <= irq_mask;
  1537. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1538. // hpa: allow rs2 to specify bits to be preserved
  1539. // XXX: support !ENABLE REGS_DUALPORT
  1540. `debug($display("LD_RS2: %2d 0x%08x", decoded_rs2, cpuregs_rs2);)
  1541. irq_mask <= ((irq_mask & cpuregs_rs2) ^ cpuregs_rs1) | MASKED_IRQ;
  1542. dbg_rs1val <= cpuregs_rs1;
  1543. dbg_rs1val_valid <= 1;
  1544. cpu_state <= cpu_state_fetch;
  1545. end
  1546. ENABLE_IRQ && ENABLE_IRQ_TIMER && instr_timer: begin
  1547. latched_store <= 1;
  1548. reg_out <= timer;
  1549. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1550. timer <= cpuregs_rs1;
  1551. dbg_rs1val <= cpuregs_rs1;
  1552. dbg_rs1val_valid <= 1;
  1553. cpu_state <= cpu_state_fetch;
  1554. end
  1555. is_lb_lh_lw_lbu_lhu && !instr_trap: begin
  1556. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1557. reg_op1 <= cpuregs_rs1;
  1558. dbg_rs1val <= cpuregs_rs1;
  1559. dbg_rs1val_valid <= 1;
  1560. cpu_state <= cpu_state_ldmem;
  1561. mem_do_rinst <= 1;
  1562. end
  1563. is_slli_srli_srai && !BARREL_SHIFTER: begin
  1564. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1565. reg_op1 <= cpuregs_rs1;
  1566. dbg_rs1val <= cpuregs_rs1;
  1567. dbg_rs1val_valid <= 1;
  1568. reg_sh <= decoded_rs2;
  1569. cpu_state <= cpu_state_shift;
  1570. end
  1571. is_jalr_addi_slti_sltiu_xori_ori_andi_addqxi, is_slli_srli_srai && BARREL_SHIFTER: begin
  1572. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1573. reg_op1 <= cpuregs_rs1;
  1574. dbg_rs1val <= cpuregs_rs1;
  1575. dbg_rs1val_valid <= 1;
  1576. reg_op2 <= is_slli_srli_srai && BARREL_SHIFTER ? decoded_rs2 : decoded_imm;
  1577. if (TWO_CYCLE_ALU)
  1578. alu_wait <= 1;
  1579. else
  1580. mem_do_rinst <= mem_do_prefetch;
  1581. cpu_state <= cpu_state_exec;
  1582. end
  1583. default: begin
  1584. `debug($display("LD_RS1: %2d 0x%08x", decoded_rs1, cpuregs_rs1);)
  1585. reg_op1 <= cpuregs_rs1;
  1586. dbg_rs1val <= cpuregs_rs1;
  1587. dbg_rs1val_valid <= 1;
  1588. if (ENABLE_REGS_DUALPORT) begin
  1589. `debug($display("LD_RS2: %2d 0x%08x", decoded_rs2, cpuregs_rs2);)
  1590. reg_sh <= cpuregs_rs2;
  1591. reg_op2 <= cpuregs_rs2;
  1592. dbg_rs2val <= cpuregs_rs2;
  1593. dbg_rs2val_valid <= 1;
  1594. (* parallel_case *)
  1595. case (1'b1)
  1596. is_sb_sh_sw: begin
  1597. cpu_state <= cpu_state_stmem;
  1598. mem_do_rinst <= 1;
  1599. end
  1600. is_sll_srl_sra && !BARREL_SHIFTER: begin
  1601. cpu_state <= cpu_state_shift;
  1602. end
  1603. default: begin
  1604. if (TWO_CYCLE_ALU || (TWO_CYCLE_COMPARE && is_beq_bne_blt_bge_bltu_bgeu)) begin
  1605. alu_wait_2 <= TWO_CYCLE_ALU && (TWO_CYCLE_COMPARE && is_beq_bne_blt_bge_bltu_bgeu);
  1606. alu_wait <= 1;
  1607. end else
  1608. mem_do_rinst <= mem_do_prefetch;
  1609. cpu_state <= cpu_state_exec;
  1610. end
  1611. endcase
  1612. end else
  1613. cpu_state <= cpu_state_ld_rs2;
  1614. end
  1615. endcase
  1616. end
  1617. cpu_state_ld_rs2: begin
  1618. `debug($display("LD_RS2: %2d 0x%08x", decoded_rs2, cpuregs_rs2);)
  1619. reg_sh <= cpuregs_rs2;
  1620. reg_op2 <= cpuregs_rs2;
  1621. dbg_rs2val <= cpuregs_rs2;
  1622. dbg_rs2val_valid <= 1;
  1623. (* parallel_case *)
  1624. case (1'b1)
  1625. WITH_PCPI && instr_trap: begin
  1626. pcpi_valid <= 1;
  1627. if (pcpi_int_ready) begin
  1628. mem_do_rinst <= 1;
  1629. pcpi_valid <= 0;
  1630. reg_out <= pcpi_int_rd;
  1631. latched_store <= pcpi_int_wr;
  1632. cpu_state <= cpu_state_fetch;
  1633. end else
  1634. if (CATCH_ILLINSN && (pcpi_timeout || instr_ecall_ebreak)) begin
  1635. pcpi_valid <= 0;
  1636. `debug($display("EBREAK OR UNSUPPORTED INSN AT 0x%08x", reg_pc);)
  1637. if (ENABLE_IRQ && !irq_mask[irq_ebreak] && !irq_active) begin
  1638. next_irq_pending[irq_ebreak] = 1;
  1639. cpu_state <= cpu_state_fetch;
  1640. end else
  1641. cpu_state <= cpu_state_trap;
  1642. end
  1643. end
  1644. is_sb_sh_sw: begin
  1645. cpu_state <= cpu_state_stmem;
  1646. mem_do_rinst <= 1;
  1647. end
  1648. is_sll_srl_sra && !BARREL_SHIFTER: begin
  1649. cpu_state <= cpu_state_shift;
  1650. end
  1651. default: begin
  1652. if (TWO_CYCLE_ALU || (TWO_CYCLE_COMPARE && is_beq_bne_blt_bge_bltu_bgeu)) begin
  1653. alu_wait_2 <= TWO_CYCLE_ALU && (TWO_CYCLE_COMPARE && is_beq_bne_blt_bge_bltu_bgeu);
  1654. alu_wait <= 1;
  1655. end else
  1656. mem_do_rinst <= mem_do_prefetch;
  1657. cpu_state <= cpu_state_exec;
  1658. end
  1659. endcase
  1660. end
  1661. cpu_state_exec: begin
  1662. reg_out <= reg_pc + decoded_imm;
  1663. if ((TWO_CYCLE_ALU || TWO_CYCLE_COMPARE) && (alu_wait || alu_wait_2)) begin
  1664. mem_do_rinst <= mem_do_prefetch && !alu_wait_2;
  1665. alu_wait <= alu_wait_2;
  1666. end else
  1667. if (is_beq_bne_blt_bge_bltu_bgeu) begin
  1668. latched_rd <= 0;
  1669. latched_store <= TWO_CYCLE_COMPARE ? alu_out_0_q : alu_out_0;
  1670. latched_branch <= TWO_CYCLE_COMPARE ? alu_out_0_q : alu_out_0;
  1671. if (mem_done)
  1672. cpu_state <= cpu_state_fetch;
  1673. if (TWO_CYCLE_COMPARE ? alu_out_0_q : alu_out_0) begin
  1674. decoder_trigger <= 0;
  1675. set_mem_do_rinst = 1;
  1676. end
  1677. end else begin
  1678. latched_branch <= instr_jalr;
  1679. latched_store <= 1;
  1680. latched_stalu <= 1;
  1681. cpu_state <= cpu_state_fetch;
  1682. end
  1683. end
  1684. cpu_state_shift: begin
  1685. latched_store <= 1;
  1686. if (reg_sh == 0) begin
  1687. reg_out <= reg_op1;
  1688. mem_do_rinst <= mem_do_prefetch;
  1689. cpu_state <= cpu_state_fetch;
  1690. end else if (TWO_STAGE_SHIFT && reg_sh >= 4) begin
  1691. (* parallel_case, full_case *)
  1692. case (1'b1)
  1693. instr_slli || instr_sll: reg_op1 <= reg_op1 << 4;
  1694. instr_srli || instr_srl: reg_op1 <= reg_op1 >> 4;
  1695. instr_srai || instr_sra: reg_op1 <= $signed(reg_op1) >>> 4;
  1696. endcase
  1697. reg_sh <= reg_sh - 4;
  1698. end else begin
  1699. (* parallel_case, full_case *)
  1700. case (1'b1)
  1701. instr_slli || instr_sll: reg_op1 <= reg_op1 << 1;
  1702. instr_srli || instr_srl: reg_op1 <= reg_op1 >> 1;
  1703. instr_srai || instr_sra: reg_op1 <= $signed(reg_op1) >>> 1;
  1704. endcase
  1705. reg_sh <= reg_sh - 1;
  1706. end
  1707. end
  1708. cpu_state_stmem: begin
  1709. if (ENABLE_TRACE)
  1710. reg_out <= reg_op2;
  1711. if (!mem_do_prefetch || mem_done) begin
  1712. if (!mem_do_wdata) begin
  1713. (* parallel_case, full_case *)
  1714. case (1'b1)
  1715. instr_sb: mem_wordsize <= 2;
  1716. instr_sh: mem_wordsize <= 1;
  1717. instr_sw: mem_wordsize <= 0;
  1718. endcase
  1719. if (ENABLE_TRACE) begin
  1720. trace_valid <= 1;
  1721. trace_data <= (irq_active ? TRACE_IRQ : 0) | TRACE_ADDR | ((reg_op1 + decoded_imm) & 32'hffffffff);
  1722. end
  1723. reg_op1 <= reg_op1 + decoded_imm;
  1724. set_mem_do_wdata = 1;
  1725. end
  1726. if (!mem_do_prefetch && mem_done) begin
  1727. cpu_state <= cpu_state_fetch;
  1728. decoder_trigger <= 1;
  1729. decoder_pseudo_trigger <= 1;
  1730. end
  1731. end
  1732. end
  1733. cpu_state_ldmem: begin
  1734. latched_store <= 1;
  1735. if (!mem_do_prefetch || mem_done) begin
  1736. if (!mem_do_rdata) begin
  1737. (* parallel_case, full_case *)
  1738. case (1'b1)
  1739. instr_lb || instr_lbu: mem_wordsize <= 2;
  1740. instr_lh || instr_lhu: mem_wordsize <= 1;
  1741. instr_lw: mem_wordsize <= 0;
  1742. endcase
  1743. latched_is_lu <= is_lbu_lhu_lw;
  1744. latched_is_lh <= instr_lh;
  1745. latched_is_lb <= instr_lb;
  1746. if (ENABLE_TRACE) begin
  1747. trace_valid <= 1;
  1748. trace_data <= (irq_active ? TRACE_IRQ : 0) | TRACE_ADDR | ((reg_op1 + decoded_imm) & 32'hffffffff);
  1749. end
  1750. reg_op1 <= reg_op1 + decoded_imm;
  1751. set_mem_do_rdata = 1;
  1752. end
  1753. if (!mem_do_prefetch && mem_done) begin
  1754. (* parallel_case, full_case *)
  1755. case (1'b1)
  1756. latched_is_lu: reg_out <= mem_rdata_word;
  1757. latched_is_lh: reg_out <= $signed(mem_rdata_word[15:0]);
  1758. latched_is_lb: reg_out <= $signed(mem_rdata_word[7:0]);
  1759. endcase
  1760. decoder_trigger <= 1;
  1761. decoder_pseudo_trigger <= 1;
  1762. cpu_state <= cpu_state_fetch;
  1763. end
  1764. end
  1765. end
  1766. endcase
  1767. if (ENABLE_IRQ) begin
  1768. next_irq_pending = next_irq_pending | irq;
  1769. if(ENABLE_IRQ_TIMER && timer)
  1770. if (timer - 1 == 0)
  1771. next_irq_pending[irq_timer] = 1;
  1772. end
  1773. if (CATCH_MISALIGN && resetn && (mem_do_rdata || mem_do_wdata)) begin
  1774. if (mem_wordsize == 0 && reg_op1[1:0] != 0) begin
  1775. `debug($display("MISALIGNED WORD: 0x%08x", reg_op1);)
  1776. if (ENABLE_IRQ && !irq_mask[irq_buserror] && !irq_active) begin
  1777. next_irq_pending[irq_buserror] = 1;
  1778. end else
  1779. cpu_state <= cpu_state_trap;
  1780. end
  1781. if (mem_wordsize == 1 && reg_op1[0] != 0) begin
  1782. `debug($display("MISALIGNED HALFWORD: 0x%08x", reg_op1);)
  1783. if (ENABLE_IRQ && !irq_mask[irq_buserror] && !irq_active) begin
  1784. next_irq_pending[irq_buserror] = 1;
  1785. end else
  1786. cpu_state <= cpu_state_trap;
  1787. end
  1788. end
  1789. if (CATCH_MISALIGN && resetn && mem_do_rinst && (COMPRESSED_ISA ? reg_pc[0] : |reg_pc[1:0])) begin
  1790. `debug($display("MISALIGNED INSTRUCTION: 0x%08x", reg_pc);)
  1791. if (ENABLE_IRQ && !irq_mask[irq_buserror] && !irq_active) begin
  1792. next_irq_pending[irq_buserror] = 1;
  1793. end else
  1794. cpu_state <= cpu_state_trap;
  1795. end
  1796. if (!CATCH_ILLINSN && decoder_trigger_q && !decoder_pseudo_trigger_q && instr_ecall_ebreak) begin
  1797. cpu_state <= cpu_state_trap;
  1798. end
  1799. if (!resetn || mem_done) begin
  1800. mem_do_prefetch <= 0;
  1801. mem_do_rinst <= 0;
  1802. mem_do_rdata <= 0;
  1803. mem_do_wdata <= 0;
  1804. end
  1805. if (set_mem_do_rinst)
  1806. mem_do_rinst <= 1;
  1807. if (set_mem_do_rdata)
  1808. mem_do_rdata <= 1;
  1809. if (set_mem_do_wdata)
  1810. mem_do_wdata <= 1;
  1811. irq_pending <= next_irq_pending & ~MASKED_IRQ;
  1812. if (!CATCH_MISALIGN) begin
  1813. if (COMPRESSED_ISA) begin
  1814. reg_pc[0] <= 0;
  1815. reg_next_pc[0] <= 0;
  1816. end else begin
  1817. reg_pc[1:0] <= 0;
  1818. reg_next_pc[1:0] <= 0;
  1819. end
  1820. end
  1821. current_pc = 'bx;
  1822. end
  1823. `ifdef RISCV_FORMAL
  1824. reg dbg_irq_call;
  1825. reg dbg_irq_enter;
  1826. reg [31:0] dbg_irq_ret;
  1827. always @(posedge clk) begin
  1828. rvfi_valid <= resetn && (launch_next_insn || trap) && dbg_valid_insn;
  1829. rvfi_order <= resetn ? rvfi_order + rvfi_valid : 0;
  1830. rvfi_insn <= dbg_insn_opcode;
  1831. rvfi_rs1_addr <= dbg_rs1val_valid ? dbg_insn_rs1 : 0;
  1832. rvfi_rs2_addr <= dbg_rs2val_valid ? dbg_insn_rs2 : 0;
  1833. rvfi_pc_rdata <= dbg_insn_addr;
  1834. rvfi_rs1_rdata <= dbg_rs1val_valid ? dbg_rs1val : 0;
  1835. rvfi_rs2_rdata <= dbg_rs2val_valid ? dbg_rs2val : 0;
  1836. rvfi_trap <= trap;
  1837. rvfi_halt <= trap;
  1838. rvfi_intr <= dbg_irq_enter;
  1839. rvfi_mode <= 3;
  1840. rvfi_ixl <= 1;
  1841. if (!resetn) begin
  1842. dbg_irq_call <= 0;
  1843. dbg_irq_enter <= 0;
  1844. end else
  1845. if (rvfi_valid) begin
  1846. dbg_irq_call <= 0;
  1847. dbg_irq_enter <= dbg_irq_call;
  1848. end else
  1849. if (irq_state == 1) begin
  1850. dbg_irq_call <= 1;
  1851. dbg_irq_ret <= next_pc;
  1852. end
  1853. if (!resetn) begin
  1854. rvfi_rd_addr <= 0;
  1855. rvfi_rd_wdata <= 0;
  1856. end else
  1857. if (cpuregs_write && !irq_state) begin
  1858. `ifdef PICORV32_TESTBUG_003
  1859. rvfi_rd_addr <= latched_rd ^ 1;
  1860. `else
  1861. rvfi_rd_addr <= latched_rd;
  1862. `endif
  1863. `ifdef PICORV32_TESTBUG_004
  1864. rvfi_rd_wdata <= latched_rd ? cpuregs_wrdata ^ 1 : 0;
  1865. `else
  1866. rvfi_rd_wdata <= latched_rd ? cpuregs_wrdata : 0;
  1867. `endif
  1868. end else
  1869. if (rvfi_valid) begin
  1870. rvfi_rd_addr <= 0;
  1871. rvfi_rd_wdata <= 0;
  1872. end
  1873. casez (dbg_insn_opcode)
  1874. /* hpa: XXX: update this */
  1875. 32'b 0000000_?????_000??_???_?????_0001011: begin // getq
  1876. rvfi_rs1_addr <= 0;
  1877. rvfi_rs1_rdata <= 0;
  1878. end
  1879. 32'b 0000001_?????_?????_???_000??_0001011: begin // setq
  1880. rvfi_rd_addr <= 0;
  1881. rvfi_rd_wdata <= 0;
  1882. end
  1883. 32'b 0000010_?????_00000_???_00000_0001011: begin // retirq
  1884. rvfi_rs1_addr <= 0;
  1885. rvfi_rs1_rdata <= 0;
  1886. end
  1887. endcase
  1888. if (!dbg_irq_call) begin
  1889. if (dbg_mem_instr) begin
  1890. rvfi_mem_addr <= 0;
  1891. rvfi_mem_rmask <= 0;
  1892. rvfi_mem_wmask <= 0;
  1893. rvfi_mem_rdata <= 0;
  1894. rvfi_mem_wdata <= 0;
  1895. end else
  1896. if (dbg_mem_valid && dbg_mem_ready) begin
  1897. rvfi_mem_addr <= dbg_mem_addr;
  1898. rvfi_mem_rmask <= dbg_mem_wstrb ? 0 : ~0;
  1899. rvfi_mem_wmask <= dbg_mem_wstrb;
  1900. rvfi_mem_rdata <= dbg_mem_rdata;
  1901. rvfi_mem_wdata <= dbg_mem_wdata;
  1902. end
  1903. end
  1904. end
  1905. always @* begin
  1906. `ifdef PICORV32_TESTBUG_005
  1907. rvfi_pc_wdata = (dbg_irq_call ? dbg_irq_ret : dbg_insn_addr) ^ 4;
  1908. `else
  1909. rvfi_pc_wdata = dbg_irq_call ? dbg_irq_ret : dbg_insn_addr;
  1910. `endif
  1911. rvfi_csr_mcycle_rmask = 0;
  1912. rvfi_csr_mcycle_wmask = 0;
  1913. rvfi_csr_mcycle_rdata = 0;
  1914. rvfi_csr_mcycle_wdata = 0;
  1915. rvfi_csr_minstret_rmask = 0;
  1916. rvfi_csr_minstret_wmask = 0;
  1917. rvfi_csr_minstret_rdata = 0;
  1918. rvfi_csr_minstret_wdata = 0;
  1919. if (rvfi_valid && rvfi_insn[6:0] == 7'b 1110011 && rvfi_insn[13:12] == 3'b010) begin
  1920. if (rvfi_insn[31:20] == 12'h C00) begin
  1921. rvfi_csr_mcycle_rmask = 64'h 0000_0000_FFFF_FFFF;
  1922. rvfi_csr_mcycle_rdata = {32'h 0000_0000, rvfi_rd_wdata};
  1923. end
  1924. if (rvfi_insn[31:20] == 12'h C80) begin
  1925. rvfi_csr_mcycle_rmask = 64'h FFFF_FFFF_0000_0000;
  1926. rvfi_csr_mcycle_rdata = {rvfi_rd_wdata, 32'h 0000_0000};
  1927. end
  1928. if (rvfi_insn[31:20] == 12'h C02) begin
  1929. rvfi_csr_minstret_rmask = 64'h 0000_0000_FFFF_FFFF;
  1930. rvfi_csr_minstret_rdata = {32'h 0000_0000, rvfi_rd_wdata};
  1931. end
  1932. if (rvfi_insn[31:20] == 12'h C82) begin
  1933. rvfi_csr_minstret_rmask = 64'h FFFF_FFFF_0000_0000;
  1934. rvfi_csr_minstret_rdata = {rvfi_rd_wdata, 32'h 0000_0000};
  1935. end
  1936. end
  1937. end
  1938. `endif
  1939. // Formal Verification
  1940. `ifdef FORMAL
  1941. reg [3:0] last_mem_nowait;
  1942. always @(posedge clk)
  1943. last_mem_nowait <= {last_mem_nowait, mem_ready || !mem_valid};
  1944. // stall the memory interface for max 4 cycles
  1945. restrict property (|last_mem_nowait || mem_ready || !mem_valid);
  1946. // resetn low in first cycle, after that resetn high
  1947. restrict property (resetn != $initstate);
  1948. // this just makes it much easier to read traces. uncomment as needed.
  1949. // assume property (mem_valid || !mem_ready);
  1950. reg ok;
  1951. always @* begin
  1952. if (resetn) begin
  1953. // instruction fetches are read-only
  1954. if (mem_valid && mem_instr)
  1955. assert (mem_wstrb == 0);
  1956. // cpu_state must be valid
  1957. ok = 0;
  1958. if (cpu_state == cpu_state_trap) ok = 1;
  1959. if (cpu_state == cpu_state_fetch) ok = 1;
  1960. if (cpu_state == cpu_state_ld_rs1) ok = 1;
  1961. if (cpu_state == cpu_state_ld_rs2) ok = !ENABLE_REGS_DUALPORT;
  1962. if (cpu_state == cpu_state_exec) ok = 1;
  1963. if (cpu_state == cpu_state_shift) ok = 1;
  1964. if (cpu_state == cpu_state_stmem) ok = 1;
  1965. if (cpu_state == cpu_state_ldmem) ok = 1;
  1966. assert (ok);
  1967. end
  1968. end
  1969. reg last_mem_la_read = 0;
  1970. reg last_mem_la_write = 0;
  1971. reg [31:0] last_mem_la_addr;
  1972. reg [31:0] last_mem_la_wdata;
  1973. reg [3:0] last_mem_la_wstrb = 0;
  1974. always @(posedge clk) begin
  1975. last_mem_la_read <= mem_la_read;
  1976. last_mem_la_write <= mem_la_write;
  1977. last_mem_la_addr <= mem_la_addr;
  1978. last_mem_la_wdata <= mem_la_wdata;
  1979. last_mem_la_wstrb <= mem_la_wstrb;
  1980. if (last_mem_la_read) begin
  1981. assert(mem_valid);
  1982. assert(mem_addr == last_mem_la_addr);
  1983. assert(mem_wstrb == 0);
  1984. end
  1985. if (last_mem_la_write) begin
  1986. assert(mem_valid);
  1987. assert(mem_addr == last_mem_la_addr);
  1988. assert(mem_wdata == last_mem_la_wdata);
  1989. assert(mem_wstrb == last_mem_la_wstrb);
  1990. end
  1991. if (mem_la_read || mem_la_write) begin
  1992. assert(!mem_valid || mem_ready);
  1993. end
  1994. end
  1995. `endif
  1996. endmodule
  1997. // This is a simple example implementation of PICORV32_REGS.
  1998. // Use the PICORV32_REGS mechanism if you want to use custom
  1999. // memory resources to implement the processor register file.
  2000. // Note that your implementation must match the requirements of
  2001. // the PicoRV32 configuration. (e.g. QREGS, etc)
  2002. module picorv32_regs (
  2003. input clk, wen,
  2004. input [5:0] waddr,
  2005. input [5:0] raddr1,
  2006. input [5:0] raddr2,
  2007. input [31:0] wdata,
  2008. output [31:0] rdata1,
  2009. output [31:0] rdata2
  2010. );
  2011. reg [31:0] regs [0:30];
  2012. always @(posedge clk)
  2013. if (wen) regs[~waddr[4:0]] <= wdata;
  2014. assign rdata1 = regs[~raddr1[4:0]];
  2015. assign rdata2 = regs[~raddr2[4:0]];
  2016. endmodule
  2017. /***************************************************************
  2018. * picorv32_pcpi_mul
  2019. ***************************************************************/
  2020. module picorv32_pcpi_mul #(
  2021. parameter STEPS_AT_ONCE = 1,
  2022. parameter CARRY_CHAIN = 4
  2023. ) (
  2024. input clk, resetn,
  2025. input pcpi_valid,
  2026. input [31:0] pcpi_insn,
  2027. input [31:0] pcpi_rs1,
  2028. input [31:0] pcpi_rs2,
  2029. output reg pcpi_wr,
  2030. output reg [31:0] pcpi_rd,
  2031. output reg pcpi_wait,
  2032. output reg pcpi_ready
  2033. );
  2034. reg instr_mul, instr_mulh, instr_mulhsu, instr_mulhu;
  2035. wire instr_any_mul = |{instr_mul, instr_mulh, instr_mulhsu, instr_mulhu};
  2036. wire instr_any_mulh = |{instr_mulh, instr_mulhsu, instr_mulhu};
  2037. wire instr_rs1_signed = |{instr_mulh, instr_mulhsu};
  2038. wire instr_rs2_signed = |{instr_mulh};
  2039. reg pcpi_wait_q;
  2040. wire mul_start = pcpi_wait && !pcpi_wait_q;
  2041. always @(posedge clk) begin
  2042. instr_mul <= 0;
  2043. instr_mulh <= 0;
  2044. instr_mulhsu <= 0;
  2045. instr_mulhu <= 0;
  2046. if (resetn && pcpi_valid && pcpi_insn[6:0] == 7'b0110011 && pcpi_insn[31:25] == 7'b0000001) begin
  2047. case (pcpi_insn[14:12])
  2048. 3'b000: instr_mul <= 1;
  2049. 3'b001: instr_mulh <= 1;
  2050. 3'b010: instr_mulhsu <= 1;
  2051. 3'b011: instr_mulhu <= 1;
  2052. endcase
  2053. end
  2054. pcpi_wait <= instr_any_mul;
  2055. pcpi_wait_q <= pcpi_wait;
  2056. end
  2057. reg [63:0] rs1, rs2, rd, rdx;
  2058. reg [63:0] next_rs1, next_rs2, this_rs2;
  2059. reg [63:0] next_rd, next_rdx, next_rdt;
  2060. reg [6:0] mul_counter;
  2061. reg mul_waiting;
  2062. reg mul_finish;
  2063. integer i, j;
  2064. // carry save accumulator
  2065. always @* begin
  2066. next_rd = rd;
  2067. next_rdx = rdx;
  2068. next_rs1 = rs1;
  2069. next_rs2 = rs2;
  2070. for (i = 0; i < STEPS_AT_ONCE; i=i+1) begin
  2071. this_rs2 = next_rs1[0] ? next_rs2 : 0;
  2072. if (CARRY_CHAIN == 0) begin
  2073. next_rdt = next_rd ^ next_rdx ^ this_rs2;
  2074. next_rdx = ((next_rd & next_rdx) | (next_rd & this_rs2) | (next_rdx & this_rs2)) << 1;
  2075. next_rd = next_rdt;
  2076. end else begin
  2077. next_rdt = 0;
  2078. for (j = 0; j < 64; j = j + CARRY_CHAIN)
  2079. {next_rdt[j+CARRY_CHAIN-1], next_rd[j +: CARRY_CHAIN]} =
  2080. next_rd[j +: CARRY_CHAIN] + next_rdx[j +: CARRY_CHAIN] + this_rs2[j +: CARRY_CHAIN];
  2081. next_rdx = next_rdt << 1;
  2082. end
  2083. next_rs1 = next_rs1 >> 1;
  2084. next_rs2 = next_rs2 << 1;
  2085. end
  2086. end
  2087. always @(posedge clk) begin
  2088. mul_finish <= 0;
  2089. if (!resetn) begin
  2090. mul_waiting <= 1;
  2091. end else
  2092. if (mul_waiting) begin
  2093. if (instr_rs1_signed)
  2094. rs1 <= $signed(pcpi_rs1);
  2095. else
  2096. rs1 <= $unsigned(pcpi_rs1);
  2097. if (instr_rs2_signed)
  2098. rs2 <= $signed(pcpi_rs2);
  2099. else
  2100. rs2 <= $unsigned(pcpi_rs2);
  2101. rd <= 0;
  2102. rdx <= 0;
  2103. mul_counter <= (instr_any_mulh ? 63 - STEPS_AT_ONCE : 31 - STEPS_AT_ONCE);
  2104. mul_waiting <= !mul_start;
  2105. end else begin
  2106. rd <= next_rd;
  2107. rdx <= next_rdx;
  2108. rs1 <= next_rs1;
  2109. rs2 <= next_rs2;
  2110. mul_counter <= mul_counter - STEPS_AT_ONCE;
  2111. if (mul_counter[6]) begin
  2112. mul_finish <= 1;
  2113. mul_waiting <= 1;
  2114. end
  2115. end
  2116. end
  2117. always @(posedge clk) begin
  2118. pcpi_wr <= 0;
  2119. pcpi_ready <= 0;
  2120. if (mul_finish && resetn) begin
  2121. pcpi_wr <= 1;
  2122. pcpi_ready <= 1;
  2123. pcpi_rd <= instr_any_mulh ? rd >> 32 : rd;
  2124. end
  2125. end
  2126. endmodule
  2127. module picorv32_pcpi_fast_mul #(
  2128. parameter EXTRA_MUL_FFS = 0,
  2129. parameter EXTRA_INSN_FFS = 0,
  2130. parameter MUL_CLKGATE = 0
  2131. ) (
  2132. input clk, resetn,
  2133. input pcpi_valid,
  2134. input [31:0] pcpi_insn,
  2135. input [31:0] pcpi_rs1,
  2136. input [31:0] pcpi_rs2,
  2137. output pcpi_wr,
  2138. output [31:0] pcpi_rd,
  2139. output pcpi_wait,
  2140. output pcpi_ready
  2141. );
  2142. reg instr_mul, instr_mulh, instr_mulhsu, instr_mulhu;
  2143. wire instr_any_mul = |{instr_mul, instr_mulh, instr_mulhsu, instr_mulhu};
  2144. wire instr_any_mulh = |{instr_mulh, instr_mulhsu, instr_mulhu};
  2145. wire instr_rs1_signed = |{instr_mulh, instr_mulhsu};
  2146. wire instr_rs2_signed = |{instr_mulh};
  2147. reg shift_out;
  2148. reg [3:0] active;
  2149. reg [32:0] rs1, rs2, rs1_q, rs2_q;
  2150. reg [63:0] rd, rd_q;
  2151. wire pcpi_insn_valid = pcpi_valid && pcpi_insn[6:0] == 7'b0110011 && pcpi_insn[31:25] == 7'b0000001;
  2152. reg pcpi_insn_valid_q;
  2153. always @* begin
  2154. instr_mul = 0;
  2155. instr_mulh = 0;
  2156. instr_mulhsu = 0;
  2157. instr_mulhu = 0;
  2158. if (resetn && (EXTRA_INSN_FFS ? pcpi_insn_valid_q : pcpi_insn_valid)) begin
  2159. case (pcpi_insn[14:12])
  2160. 3'b000: instr_mul = 1;
  2161. 3'b001: instr_mulh = 1;
  2162. 3'b010: instr_mulhsu = 1;
  2163. 3'b011: instr_mulhu = 1;
  2164. endcase
  2165. end
  2166. end
  2167. always @(posedge clk) begin
  2168. pcpi_insn_valid_q <= pcpi_insn_valid;
  2169. if (!MUL_CLKGATE || active[0]) begin
  2170. rs1_q <= rs1;
  2171. rs2_q <= rs2;
  2172. end
  2173. if (!MUL_CLKGATE || active[1]) begin
  2174. rd <= $signed(EXTRA_MUL_FFS ? rs1_q : rs1) * $signed(EXTRA_MUL_FFS ? rs2_q : rs2);
  2175. end
  2176. if (!MUL_CLKGATE || active[2]) begin
  2177. rd_q <= rd;
  2178. end
  2179. end
  2180. always @(posedge clk) begin
  2181. if (instr_any_mul && !(EXTRA_MUL_FFS ? active[3:0] : active[1:0])) begin
  2182. if (instr_rs1_signed)
  2183. rs1 <= $signed(pcpi_rs1);
  2184. else
  2185. rs1 <= $unsigned(pcpi_rs1);
  2186. if (instr_rs2_signed)
  2187. rs2 <= $signed(pcpi_rs2);
  2188. else
  2189. rs2 <= $unsigned(pcpi_rs2);
  2190. active[0] <= 1;
  2191. end else begin
  2192. active[0] <= 0;
  2193. end
  2194. active[3:1] <= active;
  2195. shift_out <= instr_any_mulh;
  2196. if (!resetn)
  2197. active <= 0;
  2198. end
  2199. assign pcpi_wr = active[EXTRA_MUL_FFS ? 3 : 1];
  2200. assign pcpi_wait = 0;
  2201. assign pcpi_ready = active[EXTRA_MUL_FFS ? 3 : 1];
  2202. `ifdef RISCV_FORMAL_ALTOPS
  2203. assign pcpi_rd =
  2204. instr_mul ? (pcpi_rs1 + pcpi_rs2) ^ 32'h5876063e :
  2205. instr_mulh ? (pcpi_rs1 + pcpi_rs2) ^ 32'hf6583fb7 :
  2206. instr_mulhsu ? (pcpi_rs1 - pcpi_rs2) ^ 32'hecfbe137 :
  2207. instr_mulhu ? (pcpi_rs1 + pcpi_rs2) ^ 32'h949ce5e8 : 1'bx;
  2208. `else
  2209. assign pcpi_rd = shift_out ? (EXTRA_MUL_FFS ? rd_q : rd) >> 32 : (EXTRA_MUL_FFS ? rd_q : rd);
  2210. `endif
  2211. endmodule
  2212. /***************************************************************
  2213. * picorv32_pcpi_div
  2214. ***************************************************************/
  2215. module picorv32_pcpi_div (
  2216. input clk, resetn,
  2217. input pcpi_valid,
  2218. input [31:0] pcpi_insn,
  2219. input [31:0] pcpi_rs1,
  2220. input [31:0] pcpi_rs2,
  2221. output reg pcpi_wr,
  2222. output reg [31:0] pcpi_rd,
  2223. output reg pcpi_wait,
  2224. output reg pcpi_ready
  2225. );
  2226. reg instr_div, instr_divu, instr_rem, instr_remu;
  2227. wire instr_any_div_rem = |{instr_div, instr_divu, instr_rem, instr_remu};
  2228. reg pcpi_wait_q;
  2229. wire start = pcpi_wait && !pcpi_wait_q;
  2230. always @(posedge clk) begin
  2231. instr_div <= 0;
  2232. instr_divu <= 0;
  2233. instr_rem <= 0;
  2234. instr_remu <= 0;
  2235. if (resetn && pcpi_valid && !pcpi_ready && pcpi_insn[6:0] == 7'b0110011 && pcpi_insn[31:25] == 7'b0000001) begin
  2236. case (pcpi_insn[14:12])
  2237. 3'b100: instr_div <= 1;
  2238. 3'b101: instr_divu <= 1;
  2239. 3'b110: instr_rem <= 1;
  2240. 3'b111: instr_remu <= 1;
  2241. endcase
  2242. end
  2243. pcpi_wait <= instr_any_div_rem && resetn;
  2244. pcpi_wait_q <= pcpi_wait && resetn;
  2245. end
  2246. reg [31:0] dividend;
  2247. reg [62:0] divisor;
  2248. reg [31:0] quotient;
  2249. reg [31:0] quotient_msk;
  2250. reg running;
  2251. reg outsign;
  2252. always @(posedge clk) begin
  2253. pcpi_ready <= 0;
  2254. pcpi_wr <= 0;
  2255. pcpi_rd <= 'bx;
  2256. if (!resetn) begin
  2257. running <= 0;
  2258. end else
  2259. if (start) begin
  2260. running <= 1;
  2261. dividend <= (instr_div || instr_rem) && pcpi_rs1[31] ? -pcpi_rs1 : pcpi_rs1;
  2262. divisor <= ((instr_div || instr_rem) && pcpi_rs2[31] ? -pcpi_rs2 : pcpi_rs2) << 31;
  2263. outsign <= (instr_div && (pcpi_rs1[31] != pcpi_rs2[31]) && |pcpi_rs2) || (instr_rem && pcpi_rs1[31]);
  2264. quotient <= 0;
  2265. quotient_msk <= 1 << 31;
  2266. end else
  2267. if (!quotient_msk && running) begin
  2268. running <= 0;
  2269. pcpi_ready <= 1;
  2270. pcpi_wr <= 1;
  2271. `ifdef RISCV_FORMAL_ALTOPS
  2272. case (1)
  2273. instr_div: pcpi_rd <= (pcpi_rs1 - pcpi_rs2) ^ 32'h7f8529ec;
  2274. instr_divu: pcpi_rd <= (pcpi_rs1 - pcpi_rs2) ^ 32'h10e8fd70;
  2275. instr_rem: pcpi_rd <= (pcpi_rs1 - pcpi_rs2) ^ 32'h8da68fa5;
  2276. instr_remu: pcpi_rd <= (pcpi_rs1 - pcpi_rs2) ^ 32'h3138d0e1;
  2277. endcase
  2278. `else
  2279. if (instr_div || instr_divu)
  2280. pcpi_rd <= outsign ? -quotient : quotient;
  2281. else
  2282. pcpi_rd <= outsign ? -dividend : dividend;
  2283. `endif
  2284. end else begin
  2285. if (divisor <= dividend) begin
  2286. dividend <= dividend - divisor;
  2287. quotient <= quotient | quotient_msk;
  2288. end
  2289. divisor <= divisor >> 1;
  2290. `ifdef RISCV_FORMAL_ALTOPS
  2291. quotient_msk <= quotient_msk >> 5;
  2292. `else
  2293. quotient_msk <= quotient_msk >> 1;
  2294. `endif
  2295. end
  2296. end
  2297. endmodule
  2298. /***************************************************************
  2299. * picorv32_axi
  2300. ***************************************************************/
  2301. module picorv32_axi #(
  2302. parameter [ 0:0] ENABLE_COUNTERS = 1,
  2303. parameter [ 0:0] ENABLE_COUNTERS64 = 1,
  2304. parameter [ 0:0] ENABLE_REGS_16_31 = 1,
  2305. parameter [ 0:0] ENABLE_REGS_DUALPORT = 1,
  2306. parameter [ 0:0] TWO_STAGE_SHIFT = 1,
  2307. parameter [ 0:0] BARREL_SHIFTER = 0,
  2308. parameter [ 0:0] TWO_CYCLE_COMPARE = 0,
  2309. parameter [ 0:0] TWO_CYCLE_ALU = 0,
  2310. parameter [ 0:0] COMPRESSED_ISA = 0,
  2311. parameter [ 0:0] CATCH_MISALIGN = 1,
  2312. parameter [ 0:0] CATCH_ILLINSN = 1,
  2313. parameter [ 0:0] ENABLE_PCPI = 0,
  2314. parameter [ 0:0] ENABLE_MUL = 0,
  2315. parameter [ 0:0] ENABLE_FAST_MUL = 0,
  2316. parameter [ 0:0] ENABLE_DIV = 0,
  2317. parameter [ 0:0] ENABLE_IRQ = 0,
  2318. parameter [ 0:0] ENABLE_IRQ_QREGS = 1,
  2319. parameter [ 0:0] ENABLE_IRQ_TIMER = 1,
  2320. parameter [ 0:0] ENABLE_TRACE = 0,
  2321. parameter [ 0:0] REGS_INIT_ZERO = 0,
  2322. parameter [31:0] MASKED_IRQ = 32'h 0000_0000,
  2323. parameter [31:0] LATCHED_IRQ = 32'h ffff_ffff,
  2324. parameter [31:0] PROGADDR_RESET = 32'h 0000_0000,
  2325. parameter [31:0] PROGADDR_IRQ = 32'h 0000_0010,
  2326. parameter [31:0] STACKADDR = 32'h ffff_ffff
  2327. ) (
  2328. input clk, resetn,
  2329. output trap,
  2330. // AXI4-lite master memory interface
  2331. output mem_axi_awvalid,
  2332. input mem_axi_awready,
  2333. output [31:0] mem_axi_awaddr,
  2334. output [ 2:0] mem_axi_awprot,
  2335. output mem_axi_wvalid,
  2336. input mem_axi_wready,
  2337. output [31:0] mem_axi_wdata,
  2338. output [ 3:0] mem_axi_wstrb,
  2339. input mem_axi_bvalid,
  2340. output mem_axi_bready,
  2341. output mem_axi_arvalid,
  2342. input mem_axi_arready,
  2343. output [31:0] mem_axi_araddr,
  2344. output [ 2:0] mem_axi_arprot,
  2345. input mem_axi_rvalid,
  2346. output mem_axi_rready,
  2347. input [31:0] mem_axi_rdata,
  2348. // Pico Co-Processor Interface (PCPI)
  2349. output pcpi_valid,
  2350. output [31:0] pcpi_insn,
  2351. output [31:0] pcpi_rs1,
  2352. output [31:0] pcpi_rs2,
  2353. input pcpi_wr,
  2354. input [31:0] pcpi_rd,
  2355. input pcpi_wait,
  2356. input pcpi_ready,
  2357. // IRQ interface
  2358. input [31:0] irq,
  2359. output [31:0] eoi,
  2360. `ifdef RISCV_FORMAL
  2361. output rvfi_valid,
  2362. output [63:0] rvfi_order,
  2363. output [31:0] rvfi_insn,
  2364. output rvfi_trap,
  2365. output rvfi_halt,
  2366. output rvfi_intr,
  2367. output [ 4:0] rvfi_rs1_addr,
  2368. output [ 4:0] rvfi_rs2_addr,
  2369. output [31:0] rvfi_rs1_rdata,
  2370. output [31:0] rvfi_rs2_rdata,
  2371. output [ 4:0] rvfi_rd_addr,
  2372. output [31:0] rvfi_rd_wdata,
  2373. output [31:0] rvfi_pc_rdata,
  2374. output [31:0] rvfi_pc_wdata,
  2375. output [31:0] rvfi_mem_addr,
  2376. output [ 3:0] rvfi_mem_rmask,
  2377. output [ 3:0] rvfi_mem_wmask,
  2378. output [31:0] rvfi_mem_rdata,
  2379. output [31:0] rvfi_mem_wdata,
  2380. `endif
  2381. // Trace Interface
  2382. output trace_valid,
  2383. output [35:0] trace_data
  2384. );
  2385. wire mem_valid;
  2386. wire [31:0] mem_addr;
  2387. wire [31:0] mem_wdata;
  2388. wire [ 3:0] mem_wstrb;
  2389. wire mem_instr;
  2390. wire mem_ready;
  2391. wire [31:0] mem_rdata;
  2392. picorv32_axi_adapter axi_adapter (
  2393. .clk (clk ),
  2394. .resetn (resetn ),
  2395. .mem_axi_awvalid(mem_axi_awvalid),
  2396. .mem_axi_awready(mem_axi_awready),
  2397. .mem_axi_awaddr (mem_axi_awaddr ),
  2398. .mem_axi_awprot (mem_axi_awprot ),
  2399. .mem_axi_wvalid (mem_axi_wvalid ),
  2400. .mem_axi_wready (mem_axi_wready ),
  2401. .mem_axi_wdata (mem_axi_wdata ),
  2402. .mem_axi_wstrb (mem_axi_wstrb ),
  2403. .mem_axi_bvalid (mem_axi_bvalid ),
  2404. .mem_axi_bready (mem_axi_bready ),
  2405. .mem_axi_arvalid(mem_axi_arvalid),
  2406. .mem_axi_arready(mem_axi_arready),
  2407. .mem_axi_araddr (mem_axi_araddr ),
  2408. .mem_axi_arprot (mem_axi_arprot ),
  2409. .mem_axi_rvalid (mem_axi_rvalid ),
  2410. .mem_axi_rready (mem_axi_rready ),
  2411. .mem_axi_rdata (mem_axi_rdata ),
  2412. .mem_valid (mem_valid ),
  2413. .mem_instr (mem_instr ),
  2414. .mem_ready (mem_ready ),
  2415. .mem_addr (mem_addr ),
  2416. .mem_wdata (mem_wdata ),
  2417. .mem_wstrb (mem_wstrb ),
  2418. .mem_rdata (mem_rdata )
  2419. );
  2420. picorv32 #(
  2421. .ENABLE_COUNTERS (ENABLE_COUNTERS ),
  2422. .ENABLE_COUNTERS64 (ENABLE_COUNTERS64 ),
  2423. .ENABLE_REGS_16_31 (ENABLE_REGS_16_31 ),
  2424. .ENABLE_REGS_DUALPORT(ENABLE_REGS_DUALPORT),
  2425. .TWO_STAGE_SHIFT (TWO_STAGE_SHIFT ),
  2426. .BARREL_SHIFTER (BARREL_SHIFTER ),
  2427. .TWO_CYCLE_COMPARE (TWO_CYCLE_COMPARE ),
  2428. .TWO_CYCLE_ALU (TWO_CYCLE_ALU ),
  2429. .COMPRESSED_ISA (COMPRESSED_ISA ),
  2430. .CATCH_MISALIGN (CATCH_MISALIGN ),
  2431. .CATCH_ILLINSN (CATCH_ILLINSN ),
  2432. .ENABLE_PCPI (ENABLE_PCPI ),
  2433. .ENABLE_MUL (ENABLE_MUL ),
  2434. .ENABLE_FAST_MUL (ENABLE_FAST_MUL ),
  2435. .ENABLE_DIV (ENABLE_DIV ),
  2436. .ENABLE_IRQ (ENABLE_IRQ ),
  2437. .ENABLE_IRQ_QREGS (ENABLE_IRQ_QREGS ),
  2438. .ENABLE_IRQ_TIMER (ENABLE_IRQ_TIMER ),
  2439. .ENABLE_TRACE (ENABLE_TRACE ),
  2440. .REGS_INIT_ZERO (REGS_INIT_ZERO ),
  2441. .MASKED_IRQ (MASKED_IRQ ),
  2442. .LATCHED_IRQ (LATCHED_IRQ ),
  2443. .PROGADDR_RESET (PROGADDR_RESET ),
  2444. .PROGADDR_IRQ (PROGADDR_IRQ ),
  2445. .STACKADDR (STACKADDR )
  2446. ) picorv32_core (
  2447. .clk (clk ),
  2448. .resetn (resetn),
  2449. .trap (trap ),
  2450. .mem_valid(mem_valid),
  2451. .mem_addr (mem_addr ),
  2452. .mem_wdata(mem_wdata),
  2453. .mem_wstrb(mem_wstrb),
  2454. .mem_instr(mem_instr),
  2455. .mem_ready(mem_ready),
  2456. .mem_rdata(mem_rdata),
  2457. .pcpi_valid(pcpi_valid),
  2458. .pcpi_insn (pcpi_insn ),
  2459. .pcpi_rs1 (pcpi_rs1 ),
  2460. .pcpi_rs2 (pcpi_rs2 ),
  2461. .pcpi_wr (pcpi_wr ),
  2462. .pcpi_rd (pcpi_rd ),
  2463. .pcpi_wait (pcpi_wait ),
  2464. .pcpi_ready(pcpi_ready),
  2465. .irq(irq),
  2466. .eoi(eoi),
  2467. `ifdef RISCV_FORMAL
  2468. .rvfi_valid (rvfi_valid ),
  2469. .rvfi_order (rvfi_order ),
  2470. .rvfi_insn (rvfi_insn ),
  2471. .rvfi_trap (rvfi_trap ),
  2472. .rvfi_halt (rvfi_halt ),
  2473. .rvfi_intr (rvfi_intr ),
  2474. .rvfi_rs1_addr (rvfi_rs1_addr ),
  2475. .rvfi_rs2_addr (rvfi_rs2_addr ),
  2476. .rvfi_rs1_rdata(rvfi_rs1_rdata),
  2477. .rvfi_rs2_rdata(rvfi_rs2_rdata),
  2478. .rvfi_rd_addr (rvfi_rd_addr ),
  2479. .rvfi_rd_wdata (rvfi_rd_wdata ),
  2480. .rvfi_pc_rdata (rvfi_pc_rdata ),
  2481. .rvfi_pc_wdata (rvfi_pc_wdata ),
  2482. .rvfi_mem_addr (rvfi_mem_addr ),
  2483. .rvfi_mem_rmask(rvfi_mem_rmask),
  2484. .rvfi_mem_wmask(rvfi_mem_wmask),
  2485. .rvfi_mem_rdata(rvfi_mem_rdata),
  2486. .rvfi_mem_wdata(rvfi_mem_wdata),
  2487. `endif
  2488. .trace_valid(trace_valid),
  2489. .trace_data (trace_data)
  2490. );
  2491. endmodule
  2492. /***************************************************************
  2493. * picorv32_axi_adapter
  2494. ***************************************************************/
  2495. module picorv32_axi_adapter (
  2496. input clk, resetn,
  2497. // AXI4-lite master memory interface
  2498. output mem_axi_awvalid,
  2499. input mem_axi_awready,
  2500. output [31:0] mem_axi_awaddr,
  2501. output [ 2:0] mem_axi_awprot,
  2502. output mem_axi_wvalid,
  2503. input mem_axi_wready,
  2504. output [31:0] mem_axi_wdata,
  2505. output [ 3:0] mem_axi_wstrb,
  2506. input mem_axi_bvalid,
  2507. output mem_axi_bready,
  2508. output mem_axi_arvalid,
  2509. input mem_axi_arready,
  2510. output [31:0] mem_axi_araddr,
  2511. output [ 2:0] mem_axi_arprot,
  2512. input mem_axi_rvalid,
  2513. output mem_axi_rready,
  2514. input [31:0] mem_axi_rdata,
  2515. // Native PicoRV32 memory interface
  2516. input mem_valid,
  2517. input mem_instr,
  2518. output mem_ready,
  2519. input [31:0] mem_addr,
  2520. input [31:0] mem_wdata,
  2521. input [ 3:0] mem_wstrb,
  2522. output [31:0] mem_rdata
  2523. );
  2524. reg ack_awvalid;
  2525. reg ack_arvalid;
  2526. reg ack_wvalid;
  2527. reg xfer_done;
  2528. assign mem_axi_awvalid = mem_valid && |mem_wstrb && !ack_awvalid;
  2529. assign mem_axi_awaddr = mem_addr;
  2530. assign mem_axi_awprot = 0;
  2531. assign mem_axi_arvalid = mem_valid && !mem_wstrb && !ack_arvalid;
  2532. assign mem_axi_araddr = mem_addr;
  2533. assign mem_axi_arprot = mem_instr ? 3'b100 : 3'b000;
  2534. assign mem_axi_wvalid = mem_valid && |mem_wstrb && !ack_wvalid;
  2535. assign mem_axi_wdata = mem_wdata;
  2536. assign mem_axi_wstrb = mem_wstrb;
  2537. assign mem_ready = mem_axi_bvalid || mem_axi_rvalid;
  2538. assign mem_axi_bready = mem_valid && |mem_wstrb;
  2539. assign mem_axi_rready = mem_valid && !mem_wstrb;
  2540. assign mem_rdata = mem_axi_rdata;
  2541. always @(posedge clk) begin
  2542. if (!resetn) begin
  2543. ack_awvalid <= 0;
  2544. end else begin
  2545. xfer_done <= mem_valid && mem_ready;
  2546. if (mem_axi_awready && mem_axi_awvalid)
  2547. ack_awvalid <= 1;
  2548. if (mem_axi_arready && mem_axi_arvalid)
  2549. ack_arvalid <= 1;
  2550. if (mem_axi_wready && mem_axi_wvalid)
  2551. ack_wvalid <= 1;
  2552. if (xfer_done || !mem_valid) begin
  2553. ack_awvalid <= 0;
  2554. ack_arvalid <= 0;
  2555. ack_wvalid <= 0;
  2556. end
  2557. end
  2558. end
  2559. endmodule
  2560. /***************************************************************
  2561. * picorv32_wb
  2562. ***************************************************************/
  2563. module picorv32_wb #(
  2564. parameter [ 0:0] ENABLE_COUNTERS = 1,
  2565. parameter [ 0:0] ENABLE_COUNTERS64 = 1,
  2566. parameter [ 0:0] ENABLE_REGS_16_31 = 1,
  2567. parameter [ 0:0] ENABLE_REGS_DUALPORT = 1,
  2568. parameter [ 0:0] TWO_STAGE_SHIFT = 1,
  2569. parameter [ 0:0] BARREL_SHIFTER = 0,
  2570. parameter [ 0:0] TWO_CYCLE_COMPARE = 0,
  2571. parameter [ 0:0] TWO_CYCLE_ALU = 0,
  2572. parameter [ 0:0] COMPRESSED_ISA = 0,
  2573. parameter [ 0:0] CATCH_MISALIGN = 1,
  2574. parameter [ 0:0] CATCH_ILLINSN = 1,
  2575. parameter [ 0:0] ENABLE_PCPI = 0,
  2576. parameter [ 0:0] ENABLE_MUL = 0,
  2577. parameter [ 0:0] ENABLE_FAST_MUL = 0,
  2578. parameter [ 0:0] ENABLE_DIV = 0,
  2579. parameter [ 0:0] ENABLE_IRQ = 0,
  2580. parameter [ 0:0] ENABLE_IRQ_QREGS = 1,
  2581. parameter [ 0:0] ENABLE_IRQ_TIMER = 1,
  2582. parameter [ 0:0] ENABLE_TRACE = 0,
  2583. parameter [ 0:0] REGS_INIT_ZERO = 0,
  2584. parameter [31:0] MASKED_IRQ = 32'h 0000_0000,
  2585. parameter [31:0] LATCHED_IRQ = 32'h ffff_ffff,
  2586. parameter [31:0] PROGADDR_RESET = 32'h 0000_0000,
  2587. parameter [31:0] PROGADDR_IRQ = 32'h 0000_0010,
  2588. parameter [31:0] STACKADDR = 32'h ffff_ffff
  2589. ) (
  2590. output trap,
  2591. // Wishbone interfaces
  2592. input wb_rst_i,
  2593. input wb_clk_i,
  2594. output reg [31:0] wbm_adr_o,
  2595. output reg [31:0] wbm_dat_o,
  2596. input [31:0] wbm_dat_i,
  2597. output reg wbm_we_o,
  2598. output reg [3:0] wbm_sel_o,
  2599. output reg wbm_stb_o,
  2600. input wbm_ack_i,
  2601. output reg wbm_cyc_o,
  2602. // Pico Co-Processor Interface (PCPI)
  2603. output pcpi_valid,
  2604. output [31:0] pcpi_insn,
  2605. output [31:0] pcpi_rs1,
  2606. output [31:0] pcpi_rs2,
  2607. input pcpi_wr,
  2608. input [31:0] pcpi_rd,
  2609. input pcpi_wait,
  2610. input pcpi_ready,
  2611. // IRQ interface
  2612. input [31:0] irq,
  2613. output [31:0] eoi,
  2614. `ifdef RISCV_FORMAL
  2615. output rvfi_valid,
  2616. output [63:0] rvfi_order,
  2617. output [31:0] rvfi_insn,
  2618. output rvfi_trap,
  2619. output rvfi_halt,
  2620. output rvfi_intr,
  2621. output [ 4:0] rvfi_rs1_addr,
  2622. output [ 4:0] rvfi_rs2_addr,
  2623. output [31:0] rvfi_rs1_rdata,
  2624. output [31:0] rvfi_rs2_rdata,
  2625. output [ 4:0] rvfi_rd_addr,
  2626. output [31:0] rvfi_rd_wdata,
  2627. output [31:0] rvfi_pc_rdata,
  2628. output [31:0] rvfi_pc_wdata,
  2629. output [31:0] rvfi_mem_addr,
  2630. output [ 3:0] rvfi_mem_rmask,
  2631. output [ 3:0] rvfi_mem_wmask,
  2632. output [31:0] rvfi_mem_rdata,
  2633. output [31:0] rvfi_mem_wdata,
  2634. `endif
  2635. // Trace Interface
  2636. output trace_valid,
  2637. output [35:0] trace_data,
  2638. output mem_instr
  2639. );
  2640. wire mem_valid;
  2641. wire [31:0] mem_addr;
  2642. wire [31:0] mem_wdata;
  2643. wire [ 3:0] mem_wstrb;
  2644. reg mem_ready;
  2645. reg [31:0] mem_rdata;
  2646. wire clk;
  2647. wire resetn;
  2648. assign clk = wb_clk_i;
  2649. assign resetn = ~wb_rst_i;
  2650. picorv32 #(
  2651. .ENABLE_COUNTERS (ENABLE_COUNTERS ),
  2652. .ENABLE_COUNTERS64 (ENABLE_COUNTERS64 ),
  2653. .ENABLE_REGS_16_31 (ENABLE_REGS_16_31 ),
  2654. .ENABLE_REGS_DUALPORT(ENABLE_REGS_DUALPORT),
  2655. .TWO_STAGE_SHIFT (TWO_STAGE_SHIFT ),
  2656. .BARREL_SHIFTER (BARREL_SHIFTER ),
  2657. .TWO_CYCLE_COMPARE (TWO_CYCLE_COMPARE ),
  2658. .TWO_CYCLE_ALU (TWO_CYCLE_ALU ),
  2659. .COMPRESSED_ISA (COMPRESSED_ISA ),
  2660. .CATCH_MISALIGN (CATCH_MISALIGN ),
  2661. .CATCH_ILLINSN (CATCH_ILLINSN ),
  2662. .ENABLE_PCPI (ENABLE_PCPI ),
  2663. .ENABLE_MUL (ENABLE_MUL ),
  2664. .ENABLE_FAST_MUL (ENABLE_FAST_MUL ),
  2665. .ENABLE_DIV (ENABLE_DIV ),
  2666. .ENABLE_IRQ (ENABLE_IRQ ),
  2667. .ENABLE_IRQ_QREGS (ENABLE_IRQ_QREGS ),
  2668. .ENABLE_IRQ_TIMER (ENABLE_IRQ_TIMER ),
  2669. .ENABLE_TRACE (ENABLE_TRACE ),
  2670. .REGS_INIT_ZERO (REGS_INIT_ZERO ),
  2671. .MASKED_IRQ (MASKED_IRQ ),
  2672. .LATCHED_IRQ (LATCHED_IRQ ),
  2673. .PROGADDR_RESET (PROGADDR_RESET ),
  2674. .PROGADDR_IRQ (PROGADDR_IRQ ),
  2675. .STACKADDR (STACKADDR )
  2676. ) picorv32_core (
  2677. .clk (clk ),
  2678. .resetn (resetn),
  2679. .trap (trap ),
  2680. .mem_valid(mem_valid),
  2681. .mem_addr (mem_addr ),
  2682. .mem_wdata(mem_wdata),
  2683. .mem_wstrb(mem_wstrb),
  2684. .mem_instr(mem_instr),
  2685. .mem_ready(mem_ready),
  2686. .mem_rdata(mem_rdata),
  2687. .pcpi_valid(pcpi_valid),
  2688. .pcpi_insn (pcpi_insn ),
  2689. .pcpi_rs1 (pcpi_rs1 ),
  2690. .pcpi_rs2 (pcpi_rs2 ),
  2691. .pcpi_wr (pcpi_wr ),
  2692. .pcpi_rd (pcpi_rd ),
  2693. .pcpi_wait (pcpi_wait ),
  2694. .pcpi_ready(pcpi_ready),
  2695. .irq(irq),
  2696. .eoi(eoi),
  2697. `ifdef RISCV_FORMAL
  2698. .rvfi_valid (rvfi_valid ),
  2699. .rvfi_order (rvfi_order ),
  2700. .rvfi_insn (rvfi_insn ),
  2701. .rvfi_trap (rvfi_trap ),
  2702. .rvfi_halt (rvfi_halt ),
  2703. .rvfi_intr (rvfi_intr ),
  2704. .rvfi_rs1_addr (rvfi_rs1_addr ),
  2705. .rvfi_rs2_addr (rvfi_rs2_addr ),
  2706. .rvfi_rs1_rdata(rvfi_rs1_rdata),
  2707. .rvfi_rs2_rdata(rvfi_rs2_rdata),
  2708. .rvfi_rd_addr (rvfi_rd_addr ),
  2709. .rvfi_rd_wdata (rvfi_rd_wdata ),
  2710. .rvfi_pc_rdata (rvfi_pc_rdata ),
  2711. .rvfi_pc_wdata (rvfi_pc_wdata ),
  2712. .rvfi_mem_addr (rvfi_mem_addr ),
  2713. .rvfi_mem_rmask(rvfi_mem_rmask),
  2714. .rvfi_mem_wmask(rvfi_mem_wmask),
  2715. .rvfi_mem_rdata(rvfi_mem_rdata),
  2716. .rvfi_mem_wdata(rvfi_mem_wdata),
  2717. `endif
  2718. .trace_valid(trace_valid),
  2719. .trace_data (trace_data)
  2720. );
  2721. localparam IDLE = 2'b00;
  2722. localparam WBSTART = 2'b01;
  2723. localparam WBEND = 2'b10;
  2724. reg [1:0] state;
  2725. wire we;
  2726. assign we = (mem_wstrb[0] | mem_wstrb[1] | mem_wstrb[2] | mem_wstrb[3]);
  2727. always @(posedge wb_clk_i) begin
  2728. if (wb_rst_i) begin
  2729. wbm_adr_o <= 0;
  2730. wbm_dat_o <= 0;
  2731. wbm_we_o <= 0;
  2732. wbm_sel_o <= 0;
  2733. wbm_stb_o <= 0;
  2734. wbm_cyc_o <= 0;
  2735. state <= IDLE;
  2736. end else begin
  2737. case (state)
  2738. IDLE: begin
  2739. if (mem_valid) begin
  2740. wbm_adr_o <= mem_addr;
  2741. wbm_dat_o <= mem_wdata;
  2742. wbm_we_o <= we;
  2743. wbm_sel_o <= mem_wstrb;
  2744. wbm_stb_o <= 1'b1;
  2745. wbm_cyc_o <= 1'b1;
  2746. state <= WBSTART;
  2747. end else begin
  2748. mem_ready <= 1'b0;
  2749. wbm_stb_o <= 1'b0;
  2750. wbm_cyc_o <= 1'b0;
  2751. wbm_we_o <= 1'b0;
  2752. end
  2753. end
  2754. WBSTART:begin
  2755. if (wbm_ack_i) begin
  2756. mem_rdata <= wbm_dat_i;
  2757. mem_ready <= 1'b1;
  2758. state <= WBEND;
  2759. wbm_stb_o <= 1'b0;
  2760. wbm_cyc_o <= 1'b0;
  2761. wbm_we_o <= 1'b0;
  2762. end
  2763. end
  2764. WBEND: begin
  2765. mem_ready <= 1'b0;
  2766. state <= IDLE;
  2767. end
  2768. default:
  2769. state <= IDLE;
  2770. endcase
  2771. end
  2772. end
  2773. endmodule