restrict external port list further in test_issuer
[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 FastRegs
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
19 from nmigen.cli import rtlil
20 from nmigen.cli import main
21 import sys
22
23 from soc.decoder.decode2execute1 import Data
24 from soc.experiment.testmem import TestMemory # test only for instructions
25 from soc.regfile.regfiles import FastRegs
26 from soc.simple.core import NonProductionCore
27 from soc.config.test.test_loadstore import TestMemPspec
28 from soc.config.ifetch import ConfigFetchUnit
29 from soc.decoder.power_enums import MicrOp
30
31
32 class TestIssuer(Elaboratable):
33 """TestIssuer - reads instructions from TestMemory and issues them
34
35 efficiency and speed is not the main goal here: functional correctness is.
36 """
37 def __init__(self, pspec):
38 # main instruction core
39 self.core = core = NonProductionCore(pspec)
40
41 # Test Instruction memory
42 self.imem = ConfigFetchUnit(pspec).fu
43 # one-row cache of instruction read
44 self.iline = Signal(64) # one instruction line
45 self.iprev_adr = Signal(64) # previous address: if different, do read
46
47 # instruction go/monitor
48 self.go_insn_i = Signal()
49 self.pc_o = Signal(64, reset_less=True)
50 self.pc_i = Data(64, "pc_i") # set "ok" to indicate "please change me"
51 self.core_start_i = Signal()
52 self.core_stop_i = Signal()
53 self.core_bigendian_i = Signal()
54 self.busy_o = Signal(reset_less=True)
55 self.halted_o = Signal(reset_less=True)
56 self.memerr_o = Signal(reset_less=True)
57
58 # FAST regfile read /write ports for PC and MSR
59 self.fast_r_pc = self.core.regs.rf['fast'].r_ports['cia'] # PC rd
60 self.fast_w_pc = self.core.regs.rf['fast'].w_ports['d_wr1'] # PC wr
61 self.fast_r_msr = self.core.regs.rf['fast'].r_ports['msr'] # MSR rd
62
63 # hack method of keeping an eye on whether branch/trap set the PC
64 self.fast_nia = self.core.regs.rf['fast'].w_ports['nia']
65 self.fast_nia.wen.name = 'fast_nia_wen'
66
67 def elaborate(self, platform):
68 m = Module()
69 comb, sync = m.d.comb, m.d.sync
70
71 m.submodules.core = core = self.core
72 m.submodules.imem = imem = self.imem
73
74 # busy/halted signals from core
75 comb += self.busy_o.eq(core.busy_o)
76 comb += self.halted_o.eq(core.core_terminated_o)
77 comb += core.core_start_i.eq(self.core_start_i)
78 comb += core.core_stop_i.eq(self.core_stop_i)
79 comb += core.bigendian_i.eq(self.core_bigendian_i)
80
81 # temporary hack: says "go" immediately for both address gen and ST
82 l0 = core.l0
83 ldst = core.fus.fus['ldst0']
84 m.d.comb += ldst.ad.go_i.eq(ldst.ad.rel_o) # link addr-go direct to rel
85 m.d.comb += ldst.st.go_i.eq(ldst.st.rel_o) # link store-go direct to rel
86
87 # PC and instruction from I-Memory
88 current_insn = Signal(32) # current fetched instruction (note sync)
89 cur_pc = Signal(64) # current PC (note it is reset/sync)
90 pc_changed = Signal() # note write to PC
91 comb += self.pc_o.eq(cur_pc)
92 ilatch = Signal(32)
93
94 # MSR (temp and latched)
95 cur_msr = Signal(64) # current MSR (note it is reset/sync)
96 msr = Signal(64, reset_less=True)
97
98 # next instruction (+4 on current)
99 nia = Signal(64, reset_less=True)
100 comb += nia.eq(cur_pc + 4)
101
102 # temporaries
103 core_busy_o = core.busy_o # core is busy
104 core_ivalid_i = core.ivalid_i # instruction is valid
105 core_issue_i = core.issue_i # instruction is issued
106 core_be_i = core.bigendian_i # bigendian mode
107 core_opcode_i = core.raw_opcode_i # raw opcode
108
109 insn_type = core.pdecode2.e.do.insn_type
110 insn_msr = core.pdecode2.msr
111 insn_cia = core.pdecode2.cia
112
113 # only run if not in halted state
114 with m.If(~core.core_terminated_o):
115
116 # actually use a nmigen FSM for the first time (w00t)
117 # this FSM is perhaps unusual in that it detects conditions
118 # then "holds" information, combinatorially, for the core
119 # (as opposed to using sync - which would be on a clock's delay)
120 # this includes the actual opcode, valid flags and so on.
121 with m.FSM() as fsm:
122
123 # waiting (zzz)
124 with m.State("IDLE"):
125 sync += pc_changed.eq(0)
126 with m.If(self.go_insn_i):
127 # instruction allowed to go: start by reading the PC
128 pc = Signal(64, reset_less=True)
129 with m.If(self.pc_i.ok):
130 # incoming override (start from pc_i)
131 comb += pc.eq(self.pc_i.data)
132 with m.Else():
133 # otherwise read FastRegs regfile for PC
134 comb += self.fast_r_pc.ren.eq(1<<FastRegs.PC)
135 comb += pc.eq(self.fast_r_pc.data_o)
136 # capture the PC and also drop it into Insn Memory
137 # we have joined a pair of combinatorial memory
138 # lookups together. this is Generally Bad.
139 comb += self.imem.a_pc_i.eq(pc)
140 comb += self.imem.a_valid_i.eq(1)
141 comb += self.imem.f_valid_i.eq(1)
142 sync += cur_pc.eq(pc)
143 m.next = "INSN_READ" # move to "wait for bus" phase
144
145 # waiting for instruction bus (stays there until not busy)
146 with m.State("INSN_READ"):
147 with m.If(self.imem.f_busy_o): # zzz...
148 # busy: stay in wait-read
149 comb += self.imem.a_valid_i.eq(1)
150 comb += self.imem.f_valid_i.eq(1)
151 with m.Else():
152 # not busy: instruction fetched
153 f_instr_o = self.imem.f_instr_o
154 if f_instr_o.width == 32:
155 insn = f_instr_o
156 else:
157 insn = f_instr_o.word_select(cur_pc[2], 32)
158 comb += current_insn.eq(insn)
159 comb += core_ivalid_i.eq(1) # instruction is valid
160 comb += core_issue_i.eq(1) # and issued
161 comb += core_opcode_i.eq(current_insn) # actual opcode
162 sync += ilatch.eq(current_insn) # latch current insn
163
164 # read MSR, latch it, and put it in decode "state"
165 comb += self.fast_r_msr.ren.eq(1<<FastRegs.MSR)
166 comb += msr.eq(self.fast_r_msr.data_o)
167 comb += insn_msr.eq(msr)
168 sync += cur_msr.eq(msr) # latch current MSR
169
170 # also drop PC into decode "state"
171 comb += insn_cia.eq(cur_pc)
172
173 m.next = "INSN_ACTIVE" # move to "wait completion"
174
175 # instruction started: must wait till it finishes
176 with m.State("INSN_ACTIVE"):
177 with m.If(core.core_terminated_o):
178 m.next = "IDLE" # back to idle, immediately (OP_ATTN)
179 with m.Else():
180 with m.If(insn_type != MicrOp.OP_NOP):
181 comb += core_ivalid_i.eq(1) # instruction is valid
182 comb += core_opcode_i.eq(ilatch) # actual opcode
183 comb += insn_msr.eq(cur_msr) # and MSR
184 comb += insn_cia.eq(cur_pc) # and PC
185 with m.If(self.fast_nia.wen):
186 sync += pc_changed.eq(1)
187 with m.If(~core_busy_o): # instruction done!
188 # ok here we are not reading the branch unit. TODO
189 # this just blithely overwrites whatever pipeline
190 # updated the PC
191 with m.If(~pc_changed):
192 comb += self.fast_w_pc.wen.eq(1<<FastRegs.PC)
193 comb += self.fast_w_pc.data_i.eq(nia)
194 m.next = "IDLE" # back to idle
195
196 return m
197
198 def __iter__(self):
199 yield from self.pc_i.ports()
200 yield self.pc_o
201 yield self.go_insn_i
202 yield self.memerr_o
203 yield from self.core.ports()
204 yield from self.imem.ports()
205 yield self.core_start_i
206 yield self.core_stop_i
207 yield self.core_bigendian_i
208 yield self.busy_o
209 yield self.halted_o
210
211 def ports(self):
212 return list(self)
213
214 def external_ports(self):
215 return self.pc_i.ports() + [self.pc_o,
216 self.go_insn_i,
217 self.memerr_o,
218 self.core_start_i,
219 self.core_stop_i,
220 self.core_bigendian_i,
221 self.busy_o,
222 self.halted_o,
223 ] + \
224 list(self.imem.ibus.fields.values()) + \
225 list(self.core.l0.cmpi.lsmem.lsi.dbus.fields.values())
226
227
228 def ports(self):
229 return list(self)
230
231
232 if __name__ == '__main__':
233 units = {'alu': 1, 'cr': 1, 'branch': 1, 'trap': 1, 'logical': 1,
234 'spr': 1,
235 'mul': 1,
236 'shiftrot': 1}
237 pspec = TestMemPspec(ldst_ifacetype='bare_wb',
238 imem_ifacetype='bare_wb',
239 addr_wid=48,
240 mask_wid=8,
241 reg_wid=64,
242 units=units)
243 dut = TestIssuer(pspec)
244 vl = main(dut, ports=dut.ports(), name="test_issuer")
245
246 if len(sys.argv) == 1:
247 vl = rtlil.convert(dut, ports=dut.external_ports(), name="test_issuer")
248 with open("test_issuer.il", "w") as f:
249 f.write(vl)