2
0

picorv32.v 92 KB

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