picorv32.v 90 KB

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