2
0

picorv32.v 99 KB

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