picorv32.v 110 KB

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