picorv32.v 94 KB

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