picorv32.v 96 KB

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