make core_rst a member of TestIssuerInternal
[soc.git] / src / soc / simple / issuer.py
1 """simple core issuer
2
3 not in any way intended for production use. this runs a FSM that:
4
5 * reads the Program Counter from StateRegs
6 * reads an instruction from a fixed-size Test Memory
7 * issues it to the Simple Core
8 * waits for it to complete
9 * increments the PC
10 * does it all over again
11
12 the purpose of this module is to verify the functional correctness
13 of the Function Units in the absolute simplest and clearest possible
14 way, and to at provide something that can be further incrementally
15 improved.
16 """
17
18 from nmigen import (Elaboratable, Module, Signal, ClockSignal, ResetSignal,
19 ClockDomain, DomainRenamer, Mux, Const, Repl, Cat)
20 from nmigen.cli import rtlil
21 from nmigen.cli import main
22 import sys
23
24 from nmigen.lib.coding import PriorityEncoder
25
26 from openpower.decoder.power_decoder import create_pdecode
27 from openpower.decoder.power_decoder2 import PowerDecode2, SVP64PrefixDecoder
28 from openpower.decoder.decode2execute1 import IssuerDecode2ToOperand
29 from openpower.decoder.decode2execute1 import Data
30 from openpower.decoder.power_enums import (MicrOp, SVP64PredInt, SVP64PredCR,
31 SVP64PredMode)
32 from openpower.state import CoreState
33 from openpower.consts import (CR, SVP64CROffs)
34 from soc.experiment.testmem import TestMemory # test only for instructions
35 from soc.regfile.regfiles import StateRegs, FastRegs
36 from soc.simple.core import NonProductionCore
37 from soc.config.test.test_loadstore import TestMemPspec
38 from soc.config.ifetch import ConfigFetchUnit
39 from soc.debug.dmi import CoreDebug, DMIInterface
40 from soc.debug.jtag import JTAG
41 from soc.config.pinouts import get_pinspecs
42 from soc.interrupts.xics import XICS_ICP, XICS_ICS
43 from soc.bus.simple_gpio import SimpleGPIO
44 from soc.bus.SPBlock512W64B8W import SPBlock512W64B8W
45 from soc.clock.select import ClockSelect
46 from soc.clock.dummypll import DummyPLL
47 from openpower.sv.svstate import SVSTATERec
48
49
50 from nmutil.util import rising_edge
51
52 def get_insn(f_instr_o, pc):
53 if f_instr_o.width == 32:
54 return f_instr_o
55 else:
56 # 64-bit: bit 2 of pc decides which word to select
57 return f_instr_o.word_select(pc[2], 32)
58
59 # gets state input or reads from state regfile
60 def state_get(m, core_rst, state_i, name, regfile, regnum):
61 comb = m.d.comb
62 sync = m.d.sync
63 # read the PC
64 res = Signal(64, reset_less=True, name=name)
65 res_ok_delay = Signal(name="%s_ok_delay" % name)
66 with m.If(~core_rst):
67 sync += res_ok_delay.eq(~state_i.ok)
68 with m.If(state_i.ok):
69 # incoming override (start from pc_i)
70 comb += res.eq(state_i.data)
71 with m.Else():
72 # otherwise read StateRegs regfile for PC...
73 comb += regfile.ren.eq(1<<regnum)
74 # ... but on a 1-clock delay
75 with m.If(res_ok_delay):
76 comb += res.eq(regfile.data_o)
77 return res
78
79 def get_predint(m, mask, name):
80 """decode SVP64 predicate integer mask field to reg number and invert
81 this is identical to the equivalent function in ISACaller except that
82 it doesn't read the INT directly, it just decodes "what needs to be done"
83 i.e. which INT reg, whether it is shifted and whether it is bit-inverted.
84
85 * all1s is set to indicate that no mask is to be applied.
86 * regread indicates the GPR register number to be read
87 * invert is set to indicate that the register value is to be inverted
88 * unary indicates that the contents of the register is to be shifted 1<<r3
89 """
90 comb = m.d.comb
91 regread = Signal(5, name=name+"regread")
92 invert = Signal(name=name+"invert")
93 unary = Signal(name=name+"unary")
94 all1s = Signal(name=name+"all1s")
95 with m.Switch(mask):
96 with m.Case(SVP64PredInt.ALWAYS.value):
97 comb += all1s.eq(1) # use 0b1111 (all ones)
98 with m.Case(SVP64PredInt.R3_UNARY.value):
99 comb += regread.eq(3)
100 comb += unary.eq(1) # 1<<r3 - shift r3 (single bit)
101 with m.Case(SVP64PredInt.R3.value):
102 comb += regread.eq(3)
103 with m.Case(SVP64PredInt.R3_N.value):
104 comb += regread.eq(3)
105 comb += invert.eq(1)
106 with m.Case(SVP64PredInt.R10.value):
107 comb += regread.eq(10)
108 with m.Case(SVP64PredInt.R10_N.value):
109 comb += regread.eq(10)
110 comb += invert.eq(1)
111 with m.Case(SVP64PredInt.R30.value):
112 comb += regread.eq(30)
113 with m.Case(SVP64PredInt.R30_N.value):
114 comb += regread.eq(30)
115 comb += invert.eq(1)
116 return regread, invert, unary, all1s
117
118 def get_predcr(m, mask, name):
119 """decode SVP64 predicate CR to reg number field and invert status
120 this is identical to _get_predcr in ISACaller
121 """
122 comb = m.d.comb
123 idx = Signal(2, name=name+"idx")
124 invert = Signal(name=name+"crinvert")
125 with m.Switch(mask):
126 with m.Case(SVP64PredCR.LT.value):
127 comb += idx.eq(CR.LT)
128 comb += invert.eq(0)
129 with m.Case(SVP64PredCR.GE.value):
130 comb += idx.eq(CR.LT)
131 comb += invert.eq(1)
132 with m.Case(SVP64PredCR.GT.value):
133 comb += idx.eq(CR.GT)
134 comb += invert.eq(0)
135 with m.Case(SVP64PredCR.LE.value):
136 comb += idx.eq(CR.GT)
137 comb += invert.eq(1)
138 with m.Case(SVP64PredCR.EQ.value):
139 comb += idx.eq(CR.EQ)
140 comb += invert.eq(0)
141 with m.Case(SVP64PredCR.NE.value):
142 comb += idx.eq(CR.EQ)
143 comb += invert.eq(1)
144 with m.Case(SVP64PredCR.SO.value):
145 comb += idx.eq(CR.SO)
146 comb += invert.eq(0)
147 with m.Case(SVP64PredCR.NS.value):
148 comb += idx.eq(CR.SO)
149 comb += invert.eq(1)
150 return idx, invert
151
152
153 class TestIssuerInternal(Elaboratable):
154 """TestIssuer - reads instructions from TestMemory and issues them
155
156 efficiency and speed is not the main goal here: functional correctness
157 and code clarity is. optimisations (which almost 100% interfere with
158 easy understanding) come later.
159 """
160 def __init__(self, pspec):
161
162 # test is SVP64 is to be enabled
163 self.svp64_en = hasattr(pspec, "svp64") and (pspec.svp64 == True)
164
165 # and if regfiles are reduced
166 self.regreduce_en = (hasattr(pspec, "regreduce") and
167 (pspec.regreduce == True))
168
169 # JTAG interface. add this right at the start because if it's
170 # added it *modifies* the pspec, by adding enable/disable signals
171 # for parts of the rest of the core
172 self.jtag_en = hasattr(pspec, "debug") and pspec.debug == 'jtag'
173 if self.jtag_en:
174 # XXX MUST keep this up-to-date with litex, and
175 # soc-cocotb-sim, and err.. all needs sorting out, argh
176 subset = ['uart',
177 'mtwi',
178 'eint', 'gpio', 'mspi0',
179 # 'mspi1', - disabled for now
180 # 'pwm', 'sd0', - disabled for now
181 'sdr']
182 self.jtag = JTAG(get_pinspecs(subset=subset))
183 # add signals to pspec to enable/disable icache and dcache
184 # (or data and intstruction wishbone if icache/dcache not included)
185 # https://bugs.libre-soc.org/show_bug.cgi?id=520
186 # TODO: do we actually care if these are not domain-synchronised?
187 # honestly probably not.
188 pspec.wb_icache_en = self.jtag.wb_icache_en
189 pspec.wb_dcache_en = self.jtag.wb_dcache_en
190 self.wb_sram_en = self.jtag.wb_sram_en
191 else:
192 self.wb_sram_en = Const(1)
193
194 # add 4k sram blocks?
195 self.sram4x4k = (hasattr(pspec, "sram4x4kblock") and
196 pspec.sram4x4kblock == True)
197 if self.sram4x4k:
198 self.sram4k = []
199 for i in range(4):
200 self.sram4k.append(SPBlock512W64B8W(name="sram4k_%d" % i,
201 #features={'err'}
202 ))
203
204 # add interrupt controller?
205 self.xics = hasattr(pspec, "xics") and pspec.xics == True
206 if self.xics:
207 self.xics_icp = XICS_ICP()
208 self.xics_ics = XICS_ICS()
209 self.int_level_i = self.xics_ics.int_level_i
210
211 # add GPIO peripheral?
212 self.gpio = hasattr(pspec, "gpio") and pspec.gpio == True
213 if self.gpio:
214 self.simple_gpio = SimpleGPIO()
215 self.gpio_o = self.simple_gpio.gpio_o
216
217 # main instruction core. suitable for prototyping / demo only
218 self.core = core = NonProductionCore(pspec)
219 self.core_rst = ResetSignal("coresync")
220
221 # instruction decoder. goes into Trap Record
222 pdecode = create_pdecode()
223 self.cur_state = CoreState("cur") # current state (MSR/PC/SVSTATE)
224 self.pdecode2 = PowerDecode2(pdecode, state=self.cur_state,
225 opkls=IssuerDecode2ToOperand,
226 svp64_en=self.svp64_en,
227 regreduce_en=self.regreduce_en)
228 if self.svp64_en:
229 self.svp64 = SVP64PrefixDecoder() # for decoding SVP64 prefix
230
231 # Test Instruction memory
232 self.imem = ConfigFetchUnit(pspec).fu
233
234 # DMI interface
235 self.dbg = CoreDebug()
236
237 # instruction go/monitor
238 self.pc_o = Signal(64, reset_less=True)
239 self.pc_i = Data(64, "pc_i") # set "ok" to indicate "please change me"
240 self.svstate_i = Data(32, "svstate_i") # ditto
241 self.core_bigendian_i = Signal() # TODO: set based on MSR.LE
242 self.busy_o = Signal(reset_less=True)
243 self.memerr_o = Signal(reset_less=True)
244
245 # STATE regfile read /write ports for PC, MSR, SVSTATE
246 staterf = self.core.regs.rf['state']
247 self.state_r_pc = staterf.r_ports['cia'] # PC rd
248 self.state_w_pc = staterf.w_ports['d_wr1'] # PC wr
249 self.state_r_msr = staterf.r_ports['msr'] # MSR rd
250 self.state_r_sv = staterf.r_ports['sv'] # SVSTATE rd
251 self.state_w_sv = staterf.w_ports['sv'] # SVSTATE wr
252
253 # DMI interface access
254 intrf = self.core.regs.rf['int']
255 crrf = self.core.regs.rf['cr']
256 xerrf = self.core.regs.rf['xer']
257 self.int_r = intrf.r_ports['dmi'] # INT read
258 self.cr_r = crrf.r_ports['full_cr_dbg'] # CR read
259 self.xer_r = xerrf.r_ports['full_xer'] # XER read
260
261 if self.svp64_en:
262 # for predication
263 self.int_pred = intrf.r_ports['pred'] # INT predicate read
264 self.cr_pred = crrf.r_ports['cr_pred'] # CR predicate read
265
266 # hack method of keeping an eye on whether branch/trap set the PC
267 self.state_nia = self.core.regs.rf['state'].w_ports['nia']
268 self.state_nia.wen.name = 'state_nia_wen'
269
270 # pulse to synchronize the simulator at instruction end
271 self.insn_done = Signal()
272
273 if self.svp64_en:
274 # store copies of predicate masks
275 self.srcmask = Signal(64)
276 self.dstmask = Signal(64)
277
278 def fetch_fsm(self, m, core, pc, svstate, nia, is_svp64_mode,
279 fetch_pc_ready_o, fetch_pc_valid_i,
280 fetch_insn_valid_o, fetch_insn_ready_i):
281 """fetch FSM
282
283 this FSM performs fetch of raw instruction data, partial-decodes
284 it 32-bit at a time to detect SVP64 prefixes, and will optionally
285 read a 2nd 32-bit quantity if that occurs.
286 """
287 comb = m.d.comb
288 sync = m.d.sync
289 pdecode2 = self.pdecode2
290 cur_state = self.cur_state
291 dec_opcode_i = pdecode2.dec.raw_opcode_in # raw opcode
292
293 msr_read = Signal(reset=1)
294
295 with m.FSM(name='fetch_fsm'):
296
297 # waiting (zzz)
298 with m.State("IDLE"):
299 comb += fetch_pc_ready_o.eq(1)
300 with m.If(fetch_pc_valid_i):
301 # instruction allowed to go: start by reading the PC
302 # capture the PC and also drop it into Insn Memory
303 # we have joined a pair of combinatorial memory
304 # lookups together. this is Generally Bad.
305 comb += self.imem.a_pc_i.eq(pc)
306 comb += self.imem.a_valid_i.eq(1)
307 comb += self.imem.f_valid_i.eq(1)
308 sync += cur_state.pc.eq(pc)
309 sync += cur_state.svstate.eq(svstate) # and svstate
310
311 # initiate read of MSR. arrives one clock later
312 comb += self.state_r_msr.ren.eq(1 << StateRegs.MSR)
313 sync += msr_read.eq(0)
314
315 m.next = "INSN_READ" # move to "wait for bus" phase
316
317 # dummy pause to find out why simulation is not keeping up
318 with m.State("INSN_READ"):
319 # one cycle later, msr/sv read arrives. valid only once.
320 with m.If(~msr_read):
321 sync += msr_read.eq(1) # yeah don't read it again
322 sync += cur_state.msr.eq(self.state_r_msr.data_o)
323 with m.If(self.imem.f_busy_o): # zzz...
324 # busy: stay in wait-read
325 comb += self.imem.a_valid_i.eq(1)
326 comb += self.imem.f_valid_i.eq(1)
327 with m.Else():
328 # not busy: instruction fetched
329 insn = get_insn(self.imem.f_instr_o, cur_state.pc)
330 if self.svp64_en:
331 svp64 = self.svp64
332 # decode the SVP64 prefix, if any
333 comb += svp64.raw_opcode_in.eq(insn)
334 comb += svp64.bigendian.eq(self.core_bigendian_i)
335 # pass the decoded prefix (if any) to PowerDecoder2
336 sync += pdecode2.sv_rm.eq(svp64.svp64_rm)
337 # remember whether this is a prefixed instruction, so
338 # the FSM can readily loop when VL==0
339 sync += is_svp64_mode.eq(svp64.is_svp64_mode)
340 # calculate the address of the following instruction
341 insn_size = Mux(svp64.is_svp64_mode, 8, 4)
342 sync += nia.eq(cur_state.pc + insn_size)
343 with m.If(~svp64.is_svp64_mode):
344 # with no prefix, store the instruction
345 # and hand it directly to the next FSM
346 sync += dec_opcode_i.eq(insn)
347 m.next = "INSN_READY"
348 with m.Else():
349 # fetch the rest of the instruction from memory
350 comb += self.imem.a_pc_i.eq(cur_state.pc + 4)
351 comb += self.imem.a_valid_i.eq(1)
352 comb += self.imem.f_valid_i.eq(1)
353 m.next = "INSN_READ2"
354 else:
355 # not SVP64 - 32-bit only
356 sync += nia.eq(cur_state.pc + 4)
357 sync += dec_opcode_i.eq(insn)
358 m.next = "INSN_READY"
359
360 with m.State("INSN_READ2"):
361 with m.If(self.imem.f_busy_o): # zzz...
362 # busy: stay in wait-read
363 comb += self.imem.a_valid_i.eq(1)
364 comb += self.imem.f_valid_i.eq(1)
365 with m.Else():
366 # not busy: instruction fetched
367 insn = get_insn(self.imem.f_instr_o, cur_state.pc+4)
368 sync += dec_opcode_i.eq(insn)
369 m.next = "INSN_READY"
370 # TODO: probably can start looking at pdecode2.rm_dec
371 # here or maybe even in INSN_READ state, if svp64_mode
372 # detected, in order to trigger - and wait for - the
373 # predicate reading.
374 if self.svp64_en:
375 pmode = pdecode2.rm_dec.predmode
376 """
377 if pmode != SVP64PredMode.ALWAYS.value:
378 fire predicate loading FSM and wait before
379 moving to INSN_READY
380 else:
381 sync += self.srcmask.eq(-1) # set to all 1s
382 sync += self.dstmask.eq(-1) # set to all 1s
383 m.next = "INSN_READY"
384 """
385
386 with m.State("INSN_READY"):
387 # hand over the instruction, to be decoded
388 comb += fetch_insn_valid_o.eq(1)
389 with m.If(fetch_insn_ready_i):
390 m.next = "IDLE"
391
392 def fetch_predicate_fsm(self, m,
393 pred_insn_valid_i, pred_insn_ready_o,
394 pred_mask_valid_o, pred_mask_ready_i):
395 """fetch_predicate_fsm - obtains (constructs in the case of CR)
396 src/dest predicate masks
397
398 https://bugs.libre-soc.org/show_bug.cgi?id=617
399 the predicates can be read here, by using IntRegs r_ports['pred']
400 or CRRegs r_ports['pred']. in the case of CRs it will have to
401 be done through multiple reads, extracting one relevant at a time.
402 later, a faster way would be to use the 32-bit-wide CR port but
403 this is more complex decoding, here. equivalent code used in
404 ISACaller is "from openpower.decoder.isa.caller import get_predcr"
405
406 note: this ENTIRE FSM is not to be called when svp64 is disabled
407 """
408 comb = m.d.comb
409 sync = m.d.sync
410 pdecode2 = self.pdecode2
411 rm_dec = pdecode2.rm_dec # SVP64RMModeDecode
412 predmode = rm_dec.predmode
413 srcpred, dstpred = rm_dec.srcpred, rm_dec.dstpred
414 cr_pred, int_pred = self.cr_pred, self.int_pred # read regfiles
415 # get src/dst step, so we can skip already used mask bits
416 cur_state = self.cur_state
417 srcstep = cur_state.svstate.srcstep
418 dststep = cur_state.svstate.dststep
419 cur_vl = cur_state.svstate.vl
420
421 # decode predicates
422 sregread, sinvert, sunary, sall1s = get_predint(m, srcpred, 's')
423 dregread, dinvert, dunary, dall1s = get_predint(m, dstpred, 'd')
424 sidx, scrinvert = get_predcr(m, srcpred, 's')
425 didx, dcrinvert = get_predcr(m, dstpred, 'd')
426
427 # store fetched masks, for either intpred or crpred
428 # when src/dst step is not zero, the skipped mask bits need to be
429 # shifted-out, before actually storing them in src/dest mask
430 new_srcmask = Signal(64, reset_less=True)
431 new_dstmask = Signal(64, reset_less=True)
432
433 with m.FSM(name="fetch_predicate"):
434
435 with m.State("FETCH_PRED_IDLE"):
436 comb += pred_insn_ready_o.eq(1)
437 with m.If(pred_insn_valid_i):
438 with m.If(predmode == SVP64PredMode.INT):
439 # skip fetching destination mask register, when zero
440 with m.If(dall1s):
441 sync += new_dstmask.eq(-1)
442 # directly go to fetch source mask register
443 # guaranteed not to be zero (otherwise predmode
444 # would be SVP64PredMode.ALWAYS, not INT)
445 comb += int_pred.addr.eq(sregread)
446 comb += int_pred.ren.eq(1)
447 m.next = "INT_SRC_READ"
448 # fetch destination predicate register
449 with m.Else():
450 comb += int_pred.addr.eq(dregread)
451 comb += int_pred.ren.eq(1)
452 m.next = "INT_DST_READ"
453 with m.Elif(predmode == SVP64PredMode.CR):
454 # go fetch masks from the CR register file
455 sync += new_srcmask.eq(0)
456 sync += new_dstmask.eq(0)
457 m.next = "CR_READ"
458 with m.Else():
459 sync += self.srcmask.eq(-1)
460 sync += self.dstmask.eq(-1)
461 m.next = "FETCH_PRED_DONE"
462
463 with m.State("INT_DST_READ"):
464 # store destination mask
465 inv = Repl(dinvert, 64)
466 with m.If(dunary):
467 # set selected mask bit for 1<<r3 mode
468 dst_shift = Signal(range(64))
469 comb += dst_shift.eq(self.int_pred.data_o & 0b111111)
470 sync += new_dstmask.eq(1 << dst_shift)
471 with m.Else():
472 # invert mask if requested
473 sync += new_dstmask.eq(self.int_pred.data_o ^ inv)
474 # skip fetching source mask register, when zero
475 with m.If(sall1s):
476 sync += new_srcmask.eq(-1)
477 m.next = "FETCH_PRED_SHIFT_MASK"
478 # fetch source predicate register
479 with m.Else():
480 comb += int_pred.addr.eq(sregread)
481 comb += int_pred.ren.eq(1)
482 m.next = "INT_SRC_READ"
483
484 with m.State("INT_SRC_READ"):
485 # store source mask
486 inv = Repl(sinvert, 64)
487 with m.If(sunary):
488 # set selected mask bit for 1<<r3 mode
489 src_shift = Signal(range(64))
490 comb += src_shift.eq(self.int_pred.data_o & 0b111111)
491 sync += new_srcmask.eq(1 << src_shift)
492 with m.Else():
493 # invert mask if requested
494 sync += new_srcmask.eq(self.int_pred.data_o ^ inv)
495 m.next = "FETCH_PRED_SHIFT_MASK"
496
497 # fetch masks from the CR register file
498 # implements the following loop:
499 # idx, inv = get_predcr(mask)
500 # mask = 0
501 # for cr_idx in range(vl):
502 # cr = crl[cr_idx + SVP64CROffs.CRPred] # takes one cycle
503 # if cr[idx] ^ inv:
504 # mask |= 1 << cr_idx
505 # return mask
506 with m.State("CR_READ"):
507 # CR index to be read, which will be ready by the next cycle
508 cr_idx = Signal.like(cur_vl, reset_less=True)
509 # submit the read operation to the regfile
510 with m.If(cr_idx != cur_vl):
511 # the CR read port is unary ...
512 # ren = 1 << cr_idx
513 # ... in MSB0 convention ...
514 # ren = 1 << (7 - cr_idx)
515 # ... and with an offset:
516 # ren = 1 << (7 - off - cr_idx)
517 idx = SVP64CROffs.CRPred + cr_idx
518 comb += cr_pred.ren.eq(1 << (7 - idx))
519 # signal data valid in the next cycle
520 cr_read = Signal(reset_less=True)
521 sync += cr_read.eq(1)
522 # load the next index
523 sync += cr_idx.eq(cr_idx + 1)
524 with m.Else():
525 # exit on loop end
526 sync += cr_read.eq(0)
527 sync += cr_idx.eq(0)
528 m.next = "FETCH_PRED_SHIFT_MASK"
529 with m.If(cr_read):
530 # compensate for the one cycle delay on the regfile
531 cur_cr_idx = Signal.like(cur_vl)
532 comb += cur_cr_idx.eq(cr_idx - 1)
533 # read the CR field, select the appropriate bit
534 cr_field = Signal(4)
535 scr_bit = Signal()
536 dcr_bit = Signal()
537 comb += cr_field.eq(cr_pred.data_o)
538 comb += scr_bit.eq(cr_field.bit_select(sidx, 1) ^ scrinvert)
539 comb += dcr_bit.eq(cr_field.bit_select(didx, 1) ^ dcrinvert)
540 # set the corresponding mask bit
541 bit_to_set = Signal.like(self.srcmask)
542 comb += bit_to_set.eq(1 << cur_cr_idx)
543 with m.If(scr_bit):
544 sync += new_srcmask.eq(new_srcmask | bit_to_set)
545 with m.If(dcr_bit):
546 sync += new_dstmask.eq(new_dstmask | bit_to_set)
547
548 with m.State("FETCH_PRED_SHIFT_MASK"):
549 # shift-out skipped mask bits
550 sync += self.srcmask.eq(new_srcmask >> srcstep)
551 sync += self.dstmask.eq(new_dstmask >> dststep)
552 m.next = "FETCH_PRED_DONE"
553
554 with m.State("FETCH_PRED_DONE"):
555 comb += pred_mask_valid_o.eq(1)
556 with m.If(pred_mask_ready_i):
557 m.next = "FETCH_PRED_IDLE"
558
559 def issue_fsm(self, m, core, pc_changed, sv_changed, nia,
560 dbg, core_rst, is_svp64_mode,
561 fetch_pc_ready_o, fetch_pc_valid_i,
562 fetch_insn_valid_o, fetch_insn_ready_i,
563 pred_insn_valid_i, pred_insn_ready_o,
564 pred_mask_valid_o, pred_mask_ready_i,
565 exec_insn_valid_i, exec_insn_ready_o,
566 exec_pc_valid_o, exec_pc_ready_i):
567 """issue FSM
568
569 decode / issue FSM. this interacts with the "fetch" FSM
570 through fetch_insn_ready/valid (incoming) and fetch_pc_ready/valid
571 (outgoing). also interacts with the "execute" FSM
572 through exec_insn_ready/valid (outgoing) and exec_pc_ready/valid
573 (incoming).
574 SVP64 RM prefixes have already been set up by the
575 "fetch" phase, so execute is fairly straightforward.
576 """
577
578 comb = m.d.comb
579 sync = m.d.sync
580 pdecode2 = self.pdecode2
581 cur_state = self.cur_state
582
583 # temporaries
584 dec_opcode_i = pdecode2.dec.raw_opcode_in # raw opcode
585
586 # for updating svstate (things like srcstep etc.)
587 update_svstate = Signal() # set this (below) if updating
588 new_svstate = SVSTATERec("new_svstate")
589 comb += new_svstate.eq(cur_state.svstate)
590
591 # precalculate srcstep+1 and dststep+1
592 cur_srcstep = cur_state.svstate.srcstep
593 cur_dststep = cur_state.svstate.dststep
594 next_srcstep = Signal.like(cur_srcstep)
595 next_dststep = Signal.like(cur_dststep)
596 comb += next_srcstep.eq(cur_state.svstate.srcstep+1)
597 comb += next_dststep.eq(cur_state.svstate.dststep+1)
598
599 # note if an exception happened. in a pipelined or OoO design
600 # this needs to be accompanied by "shadowing" (or stalling)
601 el = []
602 for exc in core.fus.excs.values():
603 el.append(exc.happened)
604 exc_happened = Signal()
605 if len(el) > 0: # at least one exception
606 comb += exc_happened.eq(Cat(*el).bool())
607
608 with m.FSM(name="issue_fsm"):
609
610 # sync with the "fetch" phase which is reading the instruction
611 # at this point, there is no instruction running, that
612 # could inadvertently update the PC.
613 with m.State("ISSUE_START"):
614 # wait on "core stop" release, before next fetch
615 # need to do this here, in case we are in a VL==0 loop
616 with m.If(~dbg.core_stop_o & ~core_rst):
617 comb += fetch_pc_valid_i.eq(1) # tell fetch to start
618 with m.If(fetch_pc_ready_o): # fetch acknowledged us
619 m.next = "INSN_WAIT"
620 with m.Else():
621 # tell core it's stopped, and acknowledge debug handshake
622 comb += dbg.core_stopped_i.eq(1)
623 # while stopped, allow updating the PC and SVSTATE
624 with m.If(self.pc_i.ok):
625 comb += self.state_w_pc.wen.eq(1 << StateRegs.PC)
626 comb += self.state_w_pc.data_i.eq(self.pc_i.data)
627 sync += pc_changed.eq(1)
628 with m.If(self.svstate_i.ok):
629 comb += new_svstate.eq(self.svstate_i.data)
630 comb += update_svstate.eq(1)
631 sync += sv_changed.eq(1)
632
633 # wait for an instruction to arrive from Fetch
634 with m.State("INSN_WAIT"):
635 comb += fetch_insn_ready_i.eq(1)
636 with m.If(fetch_insn_valid_o):
637 # loop into ISSUE_START if it's a SVP64 instruction
638 # and VL == 0. this because VL==0 is a for-loop
639 # from 0 to 0 i.e. always, always a NOP.
640 cur_vl = cur_state.svstate.vl
641 with m.If(is_svp64_mode & (cur_vl == 0)):
642 # update the PC before fetching the next instruction
643 # since we are in a VL==0 loop, no instruction was
644 # executed that we could be overwriting
645 comb += self.state_w_pc.wen.eq(1 << StateRegs.PC)
646 comb += self.state_w_pc.data_i.eq(nia)
647 comb += self.insn_done.eq(1)
648 m.next = "ISSUE_START"
649 with m.Else():
650 if self.svp64_en:
651 m.next = "PRED_START" # start fetching predicate
652 else:
653 m.next = "DECODE_SV" # skip predication
654
655 with m.State("PRED_START"):
656 comb += pred_insn_valid_i.eq(1) # tell fetch_pred to start
657 with m.If(pred_insn_ready_o): # fetch_pred acknowledged us
658 m.next = "MASK_WAIT"
659
660 with m.State("MASK_WAIT"):
661 comb += pred_mask_ready_i.eq(1) # ready to receive the masks
662 with m.If(pred_mask_valid_o): # predication masks are ready
663 m.next = "PRED_SKIP"
664
665 # skip zeros in predicate
666 with m.State("PRED_SKIP"):
667 with m.If(~is_svp64_mode):
668 m.next = "DECODE_SV" # nothing to do
669 with m.Else():
670 if self.svp64_en:
671 pred_src_zero = pdecode2.rm_dec.pred_sz
672 pred_dst_zero = pdecode2.rm_dec.pred_dz
673
674 # new srcstep, after skipping zeros
675 skip_srcstep = Signal.like(cur_srcstep)
676 # value to be added to the current srcstep
677 src_delta = Signal.like(cur_srcstep)
678 # add leading zeros to srcstep, if not in zero mode
679 with m.If(~pred_src_zero):
680 # priority encoder (count leading zeros)
681 # append guard bit, in case the mask is all zeros
682 pri_enc_src = PriorityEncoder(65)
683 m.submodules.pri_enc_src = pri_enc_src
684 comb += pri_enc_src.i.eq(Cat(self.srcmask,
685 Const(1, 1)))
686 comb += src_delta.eq(pri_enc_src.o)
687 # apply delta to srcstep
688 comb += skip_srcstep.eq(cur_srcstep + src_delta)
689 # shift-out all leading zeros from the mask
690 # plus the leading "one" bit
691 # TODO count leading zeros and shift-out the zero
692 # bits, in the same step, in hardware
693 sync += self.srcmask.eq(self.srcmask >> (src_delta+1))
694
695 # same as above, but for dststep
696 skip_dststep = Signal.like(cur_dststep)
697 dst_delta = Signal.like(cur_dststep)
698 with m.If(~pred_dst_zero):
699 pri_enc_dst = PriorityEncoder(65)
700 m.submodules.pri_enc_dst = pri_enc_dst
701 comb += pri_enc_dst.i.eq(Cat(self.dstmask,
702 Const(1, 1)))
703 comb += dst_delta.eq(pri_enc_dst.o)
704 comb += skip_dststep.eq(cur_dststep + dst_delta)
705 sync += self.dstmask.eq(self.dstmask >> (dst_delta+1))
706
707 # TODO: initialize mask[VL]=1 to avoid passing past VL
708 with m.If((skip_srcstep >= cur_vl) |
709 (skip_dststep >= cur_vl)):
710 # end of VL loop. Update PC and reset src/dst step
711 comb += self.state_w_pc.wen.eq(1 << StateRegs.PC)
712 comb += self.state_w_pc.data_i.eq(nia)
713 comb += new_svstate.srcstep.eq(0)
714 comb += new_svstate.dststep.eq(0)
715 comb += update_svstate.eq(1)
716 # synchronize with the simulator
717 comb += self.insn_done.eq(1)
718 # go back to Issue
719 m.next = "ISSUE_START"
720 with m.Else():
721 # update new src/dst step
722 comb += new_svstate.srcstep.eq(skip_srcstep)
723 comb += new_svstate.dststep.eq(skip_dststep)
724 comb += update_svstate.eq(1)
725 # proceed to Decode
726 m.next = "DECODE_SV"
727
728 # pass predicate mask bits through to satellite decoders
729 # TODO: for SIMD this will be *multiple* bits
730 sync += core.sv_pred_sm.eq(self.srcmask[0])
731 sync += core.sv_pred_dm.eq(self.dstmask[0])
732
733 # after src/dst step have been updated, we are ready
734 # to decode the instruction
735 with m.State("DECODE_SV"):
736 # decode the instruction
737 sync += core.e.eq(pdecode2.e)
738 sync += core.state.eq(cur_state)
739 sync += core.raw_insn_i.eq(dec_opcode_i)
740 sync += core.bigendian_i.eq(self.core_bigendian_i)
741 if self.svp64_en:
742 sync += core.sv_rm.eq(pdecode2.sv_rm)
743 # set RA_OR_ZERO detection in satellite decoders
744 sync += core.sv_a_nz.eq(pdecode2.sv_a_nz)
745
746 m.next = "INSN_EXECUTE" # move to "execute"
747
748 # handshake with execution FSM, move to "wait" once acknowledged
749 with m.State("INSN_EXECUTE"):
750 comb += exec_insn_valid_i.eq(1) # trigger execute
751 with m.If(exec_insn_ready_o): # execute acknowledged us
752 m.next = "EXECUTE_WAIT"
753
754 with m.State("EXECUTE_WAIT"):
755 # wait on "core stop" release, at instruction end
756 # need to do this here, in case we are in a VL>1 loop
757 with m.If(~dbg.core_stop_o & ~core_rst):
758 comb += exec_pc_ready_i.eq(1)
759 # see https://bugs.libre-soc.org/show_bug.cgi?id=636
760 #with m.If(exec_pc_valid_o & exc_happened):
761 # probably something like this:
762 # sync += pdecode2.ldst_exc.eq(core.fus.get_exc("ldst0")
763 # TODO: the exception info needs to be blatted
764 # into pdecode.ldst_exc, and the instruction "re-run".
765 # when ldst_exc.happened is set, the PowerDecoder2
766 # reacts very differently: it re-writes the instruction
767 # with a "trap" (calls PowerDecoder2.trap()) which
768 # will *overwrite* whatever was requested and jump the
769 # PC to the exception address, as well as alter MSR.
770 # nothing else needs to be done other than to note
771 # the change of PC and MSR (and, later, SVSTATE)
772 #with m.Elif(exec_pc_valid_o):
773 with m.If(exec_pc_valid_o): # replace with Elif (above)
774
775 # was this the last loop iteration?
776 is_last = Signal()
777 cur_vl = cur_state.svstate.vl
778 comb += is_last.eq(next_srcstep == cur_vl)
779
780 # if either PC or SVSTATE were changed by the previous
781 # instruction, go directly back to Fetch, without
782 # updating either PC or SVSTATE
783 with m.If(pc_changed | sv_changed):
784 m.next = "ISSUE_START"
785
786 # also return to Fetch, when no output was a vector
787 # (regardless of SRCSTEP and VL), or when the last
788 # instruction was really the last one of the VL loop
789 with m.Elif((~pdecode2.loop_continue) | is_last):
790 # before going back to fetch, update the PC state
791 # register with the NIA.
792 # ok here we are not reading the branch unit.
793 # TODO: this just blithely overwrites whatever
794 # pipeline updated the PC
795 comb += self.state_w_pc.wen.eq(1 << StateRegs.PC)
796 comb += self.state_w_pc.data_i.eq(nia)
797 # reset SRCSTEP before returning to Fetch
798 if self.svp64_en:
799 with m.If(pdecode2.loop_continue):
800 comb += new_svstate.srcstep.eq(0)
801 comb += new_svstate.dststep.eq(0)
802 comb += update_svstate.eq(1)
803 else:
804 comb += new_svstate.srcstep.eq(0)
805 comb += new_svstate.dststep.eq(0)
806 comb += update_svstate.eq(1)
807 m.next = "ISSUE_START"
808
809 # returning to Execute? then, first update SRCSTEP
810 with m.Else():
811 comb += new_svstate.srcstep.eq(next_srcstep)
812 comb += new_svstate.dststep.eq(next_dststep)
813 comb += update_svstate.eq(1)
814 # return to mask skip loop
815 m.next = "PRED_SKIP"
816
817 with m.Else():
818 comb += dbg.core_stopped_i.eq(1)
819 # while stopped, allow updating the PC and SVSTATE
820 with m.If(self.pc_i.ok):
821 comb += self.state_w_pc.wen.eq(1 << StateRegs.PC)
822 comb += self.state_w_pc.data_i.eq(self.pc_i.data)
823 sync += pc_changed.eq(1)
824 with m.If(self.svstate_i.ok):
825 comb += new_svstate.eq(self.svstate_i.data)
826 comb += update_svstate.eq(1)
827 sync += sv_changed.eq(1)
828
829 # check if svstate needs updating: if so, write it to State Regfile
830 with m.If(update_svstate):
831 comb += self.state_w_sv.wen.eq(1<<StateRegs.SVSTATE)
832 comb += self.state_w_sv.data_i.eq(new_svstate)
833 sync += cur_state.svstate.eq(new_svstate) # for next clock
834
835 def execute_fsm(self, m, core, pc_changed, sv_changed,
836 exec_insn_valid_i, exec_insn_ready_o,
837 exec_pc_valid_o, exec_pc_ready_i):
838 """execute FSM
839
840 execute FSM. this interacts with the "issue" FSM
841 through exec_insn_ready/valid (incoming) and exec_pc_ready/valid
842 (outgoing). SVP64 RM prefixes have already been set up by the
843 "issue" phase, so execute is fairly straightforward.
844 """
845
846 comb = m.d.comb
847 sync = m.d.sync
848 pdecode2 = self.pdecode2
849
850 # temporaries
851 core_busy_o = core.busy_o # core is busy
852 core_ivalid_i = core.ivalid_i # instruction is valid
853 core_issue_i = core.issue_i # instruction is issued
854 insn_type = core.e.do.insn_type # instruction MicroOp type
855
856 with m.FSM(name="exec_fsm"):
857
858 # waiting for instruction bus (stays there until not busy)
859 with m.State("INSN_START"):
860 comb += exec_insn_ready_o.eq(1)
861 with m.If(exec_insn_valid_i):
862 comb += core_ivalid_i.eq(1) # instruction is valid
863 comb += core_issue_i.eq(1) # and issued
864 sync += sv_changed.eq(0)
865 sync += pc_changed.eq(0)
866 m.next = "INSN_ACTIVE" # move to "wait completion"
867
868 # instruction started: must wait till it finishes
869 with m.State("INSN_ACTIVE"):
870 with m.If(insn_type != MicrOp.OP_NOP):
871 comb += core_ivalid_i.eq(1) # instruction is valid
872 # note changes to PC and SVSTATE
873 with m.If(self.state_nia.wen & (1<<StateRegs.SVSTATE)):
874 sync += sv_changed.eq(1)
875 with m.If(self.state_nia.wen & (1<<StateRegs.PC)):
876 sync += pc_changed.eq(1)
877 with m.If(~core_busy_o): # instruction done!
878 comb += exec_pc_valid_o.eq(1)
879 with m.If(exec_pc_ready_i):
880 comb += self.insn_done.eq(1)
881 m.next = "INSN_START" # back to fetch
882
883 def setup_peripherals(self, m):
884 comb, sync = m.d.comb, m.d.sync
885
886 m.submodules.core = core = DomainRenamer("coresync")(self.core)
887 m.submodules.imem = imem = self.imem
888 m.submodules.dbg = dbg = self.dbg
889 if self.jtag_en:
890 m.submodules.jtag = jtag = self.jtag
891 # TODO: UART2GDB mux, here, from external pin
892 # see https://bugs.libre-soc.org/show_bug.cgi?id=499
893 sync += dbg.dmi.connect_to(jtag.dmi)
894
895 cur_state = self.cur_state
896
897 # 4x 4k SRAM blocks. these simply "exist", they get routed in litex
898 if self.sram4x4k:
899 for i, sram in enumerate(self.sram4k):
900 m.submodules["sram4k_%d" % i] = sram
901 comb += sram.enable.eq(self.wb_sram_en)
902
903 # XICS interrupt handler
904 if self.xics:
905 m.submodules.xics_icp = icp = self.xics_icp
906 m.submodules.xics_ics = ics = self.xics_ics
907 comb += icp.ics_i.eq(ics.icp_o) # connect ICS to ICP
908 sync += cur_state.eint.eq(icp.core_irq_o) # connect ICP to core
909
910 # GPIO test peripheral
911 if self.gpio:
912 m.submodules.simple_gpio = simple_gpio = self.simple_gpio
913
914 # connect one GPIO output to ICS bit 15 (like in microwatt soc.vhdl)
915 # XXX causes litex ECP5 test to get wrong idea about input and output
916 # (but works with verilator sim *sigh*)
917 #if self.gpio and self.xics:
918 # comb += self.int_level_i[15].eq(simple_gpio.gpio_o[0])
919
920 # instruction decoder
921 pdecode = create_pdecode()
922 m.submodules.dec2 = pdecode2 = self.pdecode2
923 if self.svp64_en:
924 m.submodules.svp64 = svp64 = self.svp64
925
926 # convenience
927 dmi, d_reg, d_cr, d_xer, = dbg.dmi, dbg.d_gpr, dbg.d_cr, dbg.d_xer
928 intrf = self.core.regs.rf['int']
929
930 # clock delay power-on reset
931 cd_por = ClockDomain(reset_less=True)
932 cd_sync = ClockDomain()
933 core_sync = ClockDomain("coresync")
934 m.domains += cd_por, cd_sync, core_sync
935
936 ti_rst = Signal(reset_less=True)
937 delay = Signal(range(4), reset=3)
938 with m.If(delay != 0):
939 m.d.por += delay.eq(delay - 1)
940 comb += cd_por.clk.eq(ClockSignal())
941
942 # power-on reset delay
943 core_rst = ResetSignal("coresync")
944 comb += ti_rst.eq(delay != 0 | dbg.core_rst_o | ResetSignal())
945 comb += core_rst.eq(ti_rst)
946
947 # busy/halted signals from core
948 comb += self.busy_o.eq(core.busy_o)
949 comb += pdecode2.dec.bigendian.eq(self.core_bigendian_i)
950
951 # temporary hack: says "go" immediately for both address gen and ST
952 l0 = core.l0
953 ldst = core.fus.fus['ldst0']
954 st_go_edge = rising_edge(m, ldst.st.rel_o)
955 m.d.comb += ldst.ad.go_i.eq(ldst.ad.rel_o) # link addr-go direct to rel
956 m.d.comb += ldst.st.go_i.eq(st_go_edge) # link store-go to rising rel
957
958 return core_rst
959
960 def elaborate(self, platform):
961 m = Module()
962 # convenience
963 comb, sync = m.d.comb, m.d.sync
964 cur_state = self.cur_state
965 pdecode2 = self.pdecode2
966 dbg = self.dbg
967 core = self.core
968
969 # set up peripherals and core
970 core_rst = self.core_rst
971 self.setup_peripherals(m)
972
973 # reset current state if core reset requested
974 with m.If(core_rst):
975 m.d.sync += self.cur_state.eq(0)
976
977 # PC and instruction from I-Memory
978 comb += self.pc_o.eq(cur_state.pc)
979 pc_changed = Signal() # note write to PC
980 sv_changed = Signal() # note write to SVSTATE
981
982 # read state either from incoming override or from regfile
983 # TODO: really should be doing MSR in the same way
984 pc = state_get(m, core_rst, self.pc_i,
985 "pc", # read PC
986 self.state_r_pc, StateRegs.PC)
987 svstate = state_get(m, core_rst, self.svstate_i,
988 "svstate", # read SVSTATE
989 self.state_r_sv, StateRegs.SVSTATE)
990
991 # don't write pc every cycle
992 comb += self.state_w_pc.wen.eq(0)
993 comb += self.state_w_pc.data_i.eq(0)
994
995 # don't read msr every cycle
996 comb += self.state_r_msr.ren.eq(0)
997
998 # address of the next instruction, in the absence of a branch
999 # depends on the instruction size
1000 nia = Signal(64)
1001
1002 # connect up debug signals
1003 # TODO comb += core.icache_rst_i.eq(dbg.icache_rst_o)
1004 comb += dbg.terminate_i.eq(core.core_terminate_o)
1005 comb += dbg.state.pc.eq(pc)
1006 comb += dbg.state.svstate.eq(svstate)
1007 comb += dbg.state.msr.eq(cur_state.msr)
1008
1009 # pass the prefix mode from Fetch to Issue, so the latter can loop
1010 # on VL==0
1011 is_svp64_mode = Signal()
1012
1013 # there are *THREE^WFOUR-if-SVP64-enabled* FSMs, fetch (32/64-bit)
1014 # issue, decode/execute, now joined by "Predicate fetch/calculate".
1015 # these are the handshake signals between each
1016
1017 # fetch FSM can run as soon as the PC is valid
1018 fetch_pc_valid_i = Signal() # Execute tells Fetch "start next read"
1019 fetch_pc_ready_o = Signal() # Fetch Tells SVSTATE "proceed"
1020
1021 # fetch FSM hands over the instruction to be decoded / issued
1022 fetch_insn_valid_o = Signal()
1023 fetch_insn_ready_i = Signal()
1024
1025 # predicate fetch FSM decodes and fetches the predicate
1026 pred_insn_valid_i = Signal()
1027 pred_insn_ready_o = Signal()
1028
1029 # predicate fetch FSM delivers the masks
1030 pred_mask_valid_o = Signal()
1031 pred_mask_ready_i = Signal()
1032
1033 # issue FSM delivers the instruction to the be executed
1034 exec_insn_valid_i = Signal()
1035 exec_insn_ready_o = Signal()
1036
1037 # execute FSM, hands over the PC/SVSTATE back to the issue FSM
1038 exec_pc_valid_o = Signal()
1039 exec_pc_ready_i = Signal()
1040
1041 # the FSMs here are perhaps unusual in that they detect conditions
1042 # then "hold" information, combinatorially, for the core
1043 # (as opposed to using sync - which would be on a clock's delay)
1044 # this includes the actual opcode, valid flags and so on.
1045
1046 # Fetch, then predicate fetch, then Issue, then Execute.
1047 # Issue is where the VL for-loop # lives. the ready/valid
1048 # signalling is used to communicate between the four.
1049
1050 self.fetch_fsm(m, core, pc, svstate, nia, is_svp64_mode,
1051 fetch_pc_ready_o, fetch_pc_valid_i,
1052 fetch_insn_valid_o, fetch_insn_ready_i)
1053
1054 self.issue_fsm(m, core, pc_changed, sv_changed, nia,
1055 dbg, core_rst, is_svp64_mode,
1056 fetch_pc_ready_o, fetch_pc_valid_i,
1057 fetch_insn_valid_o, fetch_insn_ready_i,
1058 pred_insn_valid_i, pred_insn_ready_o,
1059 pred_mask_valid_o, pred_mask_ready_i,
1060 exec_insn_valid_i, exec_insn_ready_o,
1061 exec_pc_valid_o, exec_pc_ready_i)
1062
1063 if self.svp64_en:
1064 self.fetch_predicate_fsm(m,
1065 pred_insn_valid_i, pred_insn_ready_o,
1066 pred_mask_valid_o, pred_mask_ready_i)
1067
1068 self.execute_fsm(m, core, pc_changed, sv_changed,
1069 exec_insn_valid_i, exec_insn_ready_o,
1070 exec_pc_valid_o, exec_pc_ready_i)
1071
1072 # whatever was done above, over-ride it if core reset is held
1073 with m.If(core_rst):
1074 sync += nia.eq(0)
1075
1076 # this bit doesn't have to be in the FSM: connect up to read
1077 # regfiles on demand from DMI
1078 self.do_dmi(m, dbg)
1079
1080 # DEC and TB inc/dec FSM. copy of DEC is put into CoreState,
1081 # (which uses that in PowerDecoder2 to raise 0x900 exception)
1082 self.tb_dec_fsm(m, cur_state.dec)
1083
1084 return m
1085
1086 def do_dmi(self, m, dbg):
1087 """deals with DMI debug requests
1088
1089 currently only provides read requests for the INT regfile, CR and XER
1090 it will later also deal with *writing* to these regfiles.
1091 """
1092 comb = m.d.comb
1093 sync = m.d.sync
1094 dmi, d_reg, d_cr, d_xer, = dbg.dmi, dbg.d_gpr, dbg.d_cr, dbg.d_xer
1095 intrf = self.core.regs.rf['int']
1096
1097 with m.If(d_reg.req): # request for regfile access being made
1098 # TODO: error-check this
1099 # XXX should this be combinatorial? sync better?
1100 if intrf.unary:
1101 comb += self.int_r.ren.eq(1<<d_reg.addr)
1102 else:
1103 comb += self.int_r.addr.eq(d_reg.addr)
1104 comb += self.int_r.ren.eq(1)
1105 d_reg_delay = Signal()
1106 sync += d_reg_delay.eq(d_reg.req)
1107 with m.If(d_reg_delay):
1108 # data arrives one clock later
1109 comb += d_reg.data.eq(self.int_r.data_o)
1110 comb += d_reg.ack.eq(1)
1111
1112 # sigh same thing for CR debug
1113 with m.If(d_cr.req): # request for regfile access being made
1114 comb += self.cr_r.ren.eq(0b11111111) # enable all
1115 d_cr_delay = Signal()
1116 sync += d_cr_delay.eq(d_cr.req)
1117 with m.If(d_cr_delay):
1118 # data arrives one clock later
1119 comb += d_cr.data.eq(self.cr_r.data_o)
1120 comb += d_cr.ack.eq(1)
1121
1122 # aaand XER...
1123 with m.If(d_xer.req): # request for regfile access being made
1124 comb += self.xer_r.ren.eq(0b111111) # enable all
1125 d_xer_delay = Signal()
1126 sync += d_xer_delay.eq(d_xer.req)
1127 with m.If(d_xer_delay):
1128 # data arrives one clock later
1129 comb += d_xer.data.eq(self.xer_r.data_o)
1130 comb += d_xer.ack.eq(1)
1131
1132 def tb_dec_fsm(self, m, spr_dec):
1133 """tb_dec_fsm
1134
1135 this is a FSM for updating either dec or tb. it runs alternately
1136 DEC, TB, DEC, TB. note that SPR pipeline could have written a new
1137 value to DEC, however the regfile has "passthrough" on it so this
1138 *should* be ok.
1139
1140 see v3.0B p1097-1099 for Timeer Resource and p1065 and p1076
1141 """
1142
1143 comb, sync = m.d.comb, m.d.sync
1144 fast_rf = self.core.regs.rf['fast']
1145 fast_r_dectb = fast_rf.r_ports['issue'] # DEC/TB
1146 fast_w_dectb = fast_rf.w_ports['issue'] # DEC/TB
1147
1148 with m.FSM() as fsm:
1149
1150 # initiates read of current DEC
1151 with m.State("DEC_READ"):
1152 comb += fast_r_dectb.addr.eq(FastRegs.DEC)
1153 comb += fast_r_dectb.ren.eq(1)
1154 m.next = "DEC_WRITE"
1155
1156 # waits for DEC read to arrive (1 cycle), updates with new value
1157 with m.State("DEC_WRITE"):
1158 new_dec = Signal(64)
1159 # TODO: MSR.LPCR 32-bit decrement mode
1160 comb += new_dec.eq(fast_r_dectb.data_o - 1)
1161 comb += fast_w_dectb.addr.eq(FastRegs.DEC)
1162 comb += fast_w_dectb.wen.eq(1)
1163 comb += fast_w_dectb.data_i.eq(new_dec)
1164 sync += spr_dec.eq(new_dec) # copy into cur_state for decoder
1165 m.next = "TB_READ"
1166
1167 # initiates read of current TB
1168 with m.State("TB_READ"):
1169 comb += fast_r_dectb.addr.eq(FastRegs.TB)
1170 comb += fast_r_dectb.ren.eq(1)
1171 m.next = "TB_WRITE"
1172
1173 # waits for read TB to arrive, initiates write of current TB
1174 with m.State("TB_WRITE"):
1175 new_tb = Signal(64)
1176 comb += new_tb.eq(fast_r_dectb.data_o + 1)
1177 comb += fast_w_dectb.addr.eq(FastRegs.TB)
1178 comb += fast_w_dectb.wen.eq(1)
1179 comb += fast_w_dectb.data_i.eq(new_tb)
1180 m.next = "DEC_READ"
1181
1182 return m
1183
1184 def __iter__(self):
1185 yield from self.pc_i.ports()
1186 yield self.pc_o
1187 yield self.memerr_o
1188 yield from self.core.ports()
1189 yield from self.imem.ports()
1190 yield self.core_bigendian_i
1191 yield self.busy_o
1192
1193 def ports(self):
1194 return list(self)
1195
1196 def external_ports(self):
1197 ports = self.pc_i.ports()
1198 ports += [self.pc_o, self.memerr_o, self.core_bigendian_i, self.busy_o,
1199 ]
1200
1201 if self.jtag_en:
1202 ports += list(self.jtag.external_ports())
1203 else:
1204 # don't add DMI if JTAG is enabled
1205 ports += list(self.dbg.dmi.ports())
1206
1207 ports += list(self.imem.ibus.fields.values())
1208 ports += list(self.core.l0.cmpi.wb_bus().fields.values())
1209
1210 if self.sram4x4k:
1211 for sram in self.sram4k:
1212 ports += list(sram.bus.fields.values())
1213
1214 if self.xics:
1215 ports += list(self.xics_icp.bus.fields.values())
1216 ports += list(self.xics_ics.bus.fields.values())
1217 ports.append(self.int_level_i)
1218
1219 if self.gpio:
1220 ports += list(self.simple_gpio.bus.fields.values())
1221 ports.append(self.gpio_o)
1222
1223 return ports
1224
1225 def ports(self):
1226 return list(self)
1227
1228
1229 class TestIssuer(Elaboratable):
1230 def __init__(self, pspec):
1231 self.ti = TestIssuerInternal(pspec)
1232
1233 self.pll = DummyPLL(instance=True)
1234
1235 # PLL direct clock or not
1236 self.pll_en = hasattr(pspec, "use_pll") and pspec.use_pll
1237 if self.pll_en:
1238 self.pll_test_o = Signal(reset_less=True)
1239 self.pll_vco_o = Signal(reset_less=True)
1240 self.clk_sel_i = Signal(2, reset_less=True)
1241
1242 def elaborate(self, platform):
1243 m = Module()
1244 comb = m.d.comb
1245
1246 # TestIssuer runs at direct clock
1247 m.submodules.ti = ti = self.ti
1248 cd_int = ClockDomain("coresync")
1249
1250 if self.pll_en:
1251 # ClockSelect runs at PLL output internal clock rate
1252 m.submodules.wrappll = pll = self.pll
1253
1254 # add clock domains from PLL
1255 cd_pll = ClockDomain("pllclk")
1256 m.domains += cd_pll
1257
1258 # PLL clock established. has the side-effect of running clklsel
1259 # at the PLL's speed (see DomainRenamer("pllclk") above)
1260 pllclk = ClockSignal("pllclk")
1261 comb += pllclk.eq(pll.clk_pll_o)
1262
1263 # wire up external 24mhz to PLL
1264 comb += pll.clk_24_i.eq(ClockSignal())
1265
1266 # output 18 mhz PLL test signal, and analog oscillator out
1267 comb += self.pll_test_o.eq(pll.pll_test_o)
1268 comb += self.pll_vco_o.eq(pll.pll_vco_o)
1269
1270 # input to pll clock selection
1271 comb += pll.clk_sel_i.eq(self.clk_sel_i)
1272
1273 # now wire up ResetSignals. don't mind them being in this domain
1274 pll_rst = ResetSignal("pllclk")
1275 comb += pll_rst.eq(ResetSignal())
1276
1277 # internal clock is set to selector clock-out. has the side-effect of
1278 # running TestIssuer at this speed (see DomainRenamer("intclk") above)
1279 intclk = ClockSignal("coresync")
1280 if self.pll_en:
1281 comb += intclk.eq(pllclk)
1282 else:
1283 comb += intclk.eq(ClockSignal())
1284
1285 return m
1286
1287 def ports(self):
1288 return list(self.ti.ports()) + list(self.pll.ports()) + \
1289 [ClockSignal(), ResetSignal()]
1290
1291 def external_ports(self):
1292 ports = self.ti.external_ports()
1293 ports.append(ClockSignal())
1294 ports.append(ResetSignal())
1295 if self.pll_en:
1296 ports.append(self.clk_sel_i)
1297 ports.append(self.pll_test_o)
1298 ports.append(self.pll_vco_o)
1299 return ports
1300
1301
1302 if __name__ == '__main__':
1303 units = {'alu': 1, 'cr': 1, 'branch': 1, 'trap': 1, 'logical': 1,
1304 'spr': 1,
1305 'div': 1,
1306 'mul': 1,
1307 'shiftrot': 1
1308 }
1309 pspec = TestMemPspec(ldst_ifacetype='bare_wb',
1310 imem_ifacetype='bare_wb',
1311 addr_wid=48,
1312 mask_wid=8,
1313 reg_wid=64,
1314 units=units)
1315 dut = TestIssuer(pspec)
1316 vl = main(dut, ports=dut.ports(), name="test_issuer")
1317
1318 if len(sys.argv) == 1:
1319 vl = rtlil.convert(dut, ports=dut.external_ports(), name="test_issuer")
1320 with open("test_issuer.il", "w") as f:
1321 f.write(vl)