2
0

picorv32.v 98 KB

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