2
0

picorv32.v 94 KB

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