picorv32.v 93 KB

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