2
0

picorv32.v 95 KB

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