0629217eaa3bfb021d37adf5c3dbe029b6982741
1 """Power ISA Decoder second stage
3 based on Anton Blanchard microwatt decode2.vhdl
5 Note: OP_TRAP is used for exceptions and interrupts (micro-code style) by
6 over-riding the internal opcode when an exception is needed.
9 from nmigen
import Module
, Elaboratable
, Signal
, Mux
, Const
, Cat
, Repl
, Record
10 from nmigen
.cli
import rtlil
12 from nmutil
.iocontrol
import RecordObject
13 from nmutil
.extend
import exts
15 from soc
.decoder
.power_regspec_map
import regspec_decode_read
16 from soc
.decoder
.power_regspec_map
import regspec_decode_write
17 from soc
.decoder
.power_decoder
import create_pdecode
18 from soc
.decoder
.power_enums
import (MicrOp
, CryIn
, Function
,
20 LdstLen
, In1Sel
, In2Sel
, In3Sel
,
21 OutSel
, SPR
, RC
, LDSTMode
)
22 from soc
.decoder
.decode2execute1
import Decode2ToExecute1Type
, Data
23 from soc
.consts
import MSR
25 from soc
.regfile
.regfiles
import FastRegs
26 from soc
.consts
import TT
27 from soc
.config
.state
import CoreState
30 def decode_spr_num(spr
):
31 return Cat(spr
[5:10], spr
[0:5])
34 def instr_is_priv(m
, op
, insn
):
35 """determines if the instruction is privileged or not
38 is_priv_insn
= Signal(reset_less
=True)
40 with m
.Case(MicrOp
.OP_ATTN
, MicrOp
.OP_MFMSR
, MicrOp
.OP_MTMSRD
,
41 MicrOp
.OP_MTMSR
, MicrOp
.OP_RFID
):
42 comb
+= is_priv_insn
.eq(1)
44 #with m.Case(MicrOp.OP_TLBIE) : comb += is_priv_insn.eq(1)
45 with m
.Case(MicrOp
.OP_MFSPR
, MicrOp
.OP_MTSPR
):
46 with m
.If(insn
[20]): # field XFX.spr[-1] i think
47 comb
+= is_priv_insn
.eq(1)
51 class SPRMap(Elaboratable
):
52 """SPRMap: maps POWER9 SPR numbers to internal enum values
56 self
.spr_i
= Signal(10, reset_less
=True)
57 self
.spr_o
= Signal(SPR
, reset_less
=True)
59 def elaborate(self
, platform
):
61 with m
.Switch(self
.spr_i
):
62 for i
, x
in enumerate(SPR
):
64 m
.d
.comb
+= self
.spr_o
.eq(i
)
68 class DecodeA(Elaboratable
):
69 """DecodeA from instruction
71 decodes register RA, whether immediate-zero, implicit and
75 def __init__(self
, dec
):
77 self
.sel_in
= Signal(In1Sel
, reset_less
=True)
78 self
.insn_in
= Signal(32, reset_less
=True)
79 self
.reg_out
= Data(5, name
="reg_a")
80 self
.immz_out
= Signal(reset_less
=True)
81 self
.spr_out
= Data(SPR
, "spr_a")
82 self
.fast_out
= Data(3, "fast_a")
84 def elaborate(self
, platform
):
87 m
.submodules
.sprmap
= sprmap
= SPRMap()
89 # select Register A field
90 ra
= Signal(5, reset_less
=True)
91 comb
+= ra
.eq(self
.dec
.RA
)
92 with m
.If((self
.sel_in
== In1Sel
.RA
) |
93 ((self
.sel_in
== In1Sel
.RA_OR_ZERO
) &
94 (ra
!= Const(0, 5)))):
95 comb
+= self
.reg_out
.data
.eq(ra
)
96 comb
+= self
.reg_out
.ok
.eq(1)
98 # zero immediate requested
99 with m
.If((self
.sel_in
== In1Sel
.RA_OR_ZERO
) &
100 (self
.reg_out
.data
== Const(0, 5))):
101 comb
+= self
.immz_out
.eq(1)
103 # some Logic/ALU ops have RS as the 3rd arg, but no "RA".
104 with m
.If(self
.sel_in
== In1Sel
.RS
):
105 comb
+= self
.reg_out
.data
.eq(self
.dec
.RS
)
106 comb
+= self
.reg_out
.ok
.eq(1)
108 # decode Fast-SPR based on instruction type
110 with m
.Switch(op
.internal_op
):
112 # BC or BCREG: implicit register (CTR) NOTE: same in DecodeOut
113 with m
.Case(MicrOp
.OP_BC
):
114 with m
.If(~self
.dec
.BO
[2]): # 3.0B p38 BO2=0, use CTR reg
116 comb
+= self
.fast_out
.data
.eq(FastRegs
.CTR
)
117 comb
+= self
.fast_out
.ok
.eq(1)
118 with m
.Case(MicrOp
.OP_BCREG
):
119 xo9
= self
.dec
.FormXL
.XO
[9] # 3.0B p38 top bit of XO
120 xo5
= self
.dec
.FormXL
.XO
[5] # 3.0B p38
121 with m
.If(xo9
& ~xo5
):
123 comb
+= self
.fast_out
.data
.eq(FastRegs
.CTR
)
124 comb
+= self
.fast_out
.ok
.eq(1)
126 # MFSPR move from SPRs
127 with m
.Case(MicrOp
.OP_MFSPR
):
128 spr
= Signal(10, reset_less
=True)
129 comb
+= spr
.eq(decode_spr_num(self
.dec
.SPR
)) # from XFX
132 with m
.Case(SPR
.CTR
.value
):
133 comb
+= self
.fast_out
.data
.eq(FastRegs
.CTR
)
134 comb
+= self
.fast_out
.ok
.eq(1)
135 with m
.Case(SPR
.LR
.value
):
136 comb
+= self
.fast_out
.data
.eq(FastRegs
.LR
)
137 comb
+= self
.fast_out
.ok
.eq(1)
138 with m
.Case(SPR
.TAR
.value
):
139 comb
+= self
.fast_out
.data
.eq(FastRegs
.TAR
)
140 comb
+= self
.fast_out
.ok
.eq(1)
141 with m
.Case(SPR
.SRR0
.value
):
142 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR0
)
143 comb
+= self
.fast_out
.ok
.eq(1)
144 with m
.Case(SPR
.SRR1
.value
):
145 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR1
)
146 comb
+= self
.fast_out
.ok
.eq(1)
147 with m
.Case(SPR
.XER
.value
):
149 # : map to internal SPR numbers
150 # XXX TODO: dec and tb not to go through mapping.
152 comb
+= sprmap
.spr_i
.eq(spr
)
153 comb
+= self
.spr_out
.data
.eq(sprmap
.spr_o
)
154 comb
+= self
.spr_out
.ok
.eq(1)
159 class DecodeB(Elaboratable
):
160 """DecodeB from instruction
162 decodes register RB, different forms of immediate (signed, unsigned),
163 and implicit SPRs. register B is basically "lane 2" into the CompUnits.
164 by industry-standard convention, "lane 2" is where fully-decoded
165 immediates are muxed in.
168 def __init__(self
, dec
):
170 self
.sel_in
= Signal(In2Sel
, reset_less
=True)
171 self
.insn_in
= Signal(32, reset_less
=True)
172 self
.reg_out
= Data(5, "reg_b")
173 self
.imm_out
= Data(64, "imm_b")
174 self
.fast_out
= Data(3, "fast_b")
176 def elaborate(self
, platform
):
180 # select Register B field
181 with m
.Switch(self
.sel_in
):
182 with m
.Case(In2Sel
.RB
):
183 comb
+= self
.reg_out
.data
.eq(self
.dec
.RB
)
184 comb
+= self
.reg_out
.ok
.eq(1)
185 with m
.Case(In2Sel
.RS
):
186 # for M-Form shiftrot
187 comb
+= self
.reg_out
.data
.eq(self
.dec
.RS
)
188 comb
+= self
.reg_out
.ok
.eq(1)
189 with m
.Case(In2Sel
.CONST_UI
): # unsigned
190 comb
+= self
.imm_out
.data
.eq(self
.dec
.UI
)
191 comb
+= self
.imm_out
.ok
.eq(1)
192 with m
.Case(In2Sel
.CONST_SI
): # sign-extended 16-bit
193 si
= Signal(16, reset_less
=True)
194 comb
+= si
.eq(self
.dec
.SI
)
195 comb
+= self
.imm_out
.data
.eq(exts(si
, 16, 64))
196 comb
+= self
.imm_out
.ok
.eq(1)
197 with m
.Case(In2Sel
.CONST_SI_HI
): # sign-extended 16+16=32 bit
198 si_hi
= Signal(32, reset_less
=True)
199 comb
+= si_hi
.eq(self
.dec
.SI
<< 16)
200 comb
+= self
.imm_out
.data
.eq(exts(si_hi
, 32, 64))
201 comb
+= self
.imm_out
.ok
.eq(1)
202 with m
.Case(In2Sel
.CONST_UI_HI
): # unsigned
203 ui
= Signal(16, reset_less
=True)
204 comb
+= ui
.eq(self
.dec
.UI
)
205 comb
+= self
.imm_out
.data
.eq(ui
<< 16)
206 comb
+= self
.imm_out
.ok
.eq(1)
207 with m
.Case(In2Sel
.CONST_LI
): # sign-extend 24+2=26 bit
208 li
= Signal(26, reset_less
=True)
209 comb
+= li
.eq(self
.dec
.LI
<< 2)
210 comb
+= self
.imm_out
.data
.eq(exts(li
, 26, 64))
211 comb
+= self
.imm_out
.ok
.eq(1)
212 with m
.Case(In2Sel
.CONST_BD
): # sign-extend (14+2)=16 bit
213 bd
= Signal(16, reset_less
=True)
214 comb
+= bd
.eq(self
.dec
.BD
<< 2)
215 comb
+= self
.imm_out
.data
.eq(exts(bd
, 16, 64))
216 comb
+= self
.imm_out
.ok
.eq(1)
217 with m
.Case(In2Sel
.CONST_DS
): # sign-extended (14+2=16) bit
218 ds
= Signal(16, reset_less
=True)
219 comb
+= ds
.eq(self
.dec
.DS
<< 2)
220 comb
+= self
.imm_out
.data
.eq(exts(ds
, 16, 64))
221 comb
+= self
.imm_out
.ok
.eq(1)
222 with m
.Case(In2Sel
.CONST_M1
): # signed (-1)
223 comb
+= self
.imm_out
.data
.eq(~
Const(0, 64)) # all 1s
224 comb
+= self
.imm_out
.ok
.eq(1)
225 with m
.Case(In2Sel
.CONST_SH
): # unsigned - for shift
226 comb
+= self
.imm_out
.data
.eq(self
.dec
.sh
)
227 comb
+= self
.imm_out
.ok
.eq(1)
228 with m
.Case(In2Sel
.CONST_SH32
): # unsigned - for shift
229 comb
+= self
.imm_out
.data
.eq(self
.dec
.SH32
)
230 comb
+= self
.imm_out
.ok
.eq(1)
232 # decode SPR2 based on instruction type
234 # BCREG implicitly uses LR or TAR for 2nd reg
235 # CTR however is already in fast_spr1 *not* 2.
236 with m
.If(op
.internal_op
== MicrOp
.OP_BCREG
):
237 xo9
= self
.dec
.FormXL
.XO
[9] # 3.0B p38 top bit of XO
238 xo5
= self
.dec
.FormXL
.XO
[5] # 3.0B p38
240 comb
+= self
.fast_out
.data
.eq(FastRegs
.LR
)
241 comb
+= self
.fast_out
.ok
.eq(1)
243 comb
+= self
.fast_out
.data
.eq(FastRegs
.TAR
)
244 comb
+= self
.fast_out
.ok
.eq(1)
249 class DecodeC(Elaboratable
):
250 """DecodeC from instruction
252 decodes register RC. this is "lane 3" into some CompUnits (not many)
255 def __init__(self
, dec
):
257 self
.sel_in
= Signal(In3Sel
, reset_less
=True)
258 self
.insn_in
= Signal(32, reset_less
=True)
259 self
.reg_out
= Data(5, "reg_c")
261 def elaborate(self
, platform
):
265 # select Register C field
266 with m
.Switch(self
.sel_in
):
267 with m
.Case(In3Sel
.RB
):
268 # for M-Form shiftrot
269 comb
+= self
.reg_out
.data
.eq(self
.dec
.RB
)
270 comb
+= self
.reg_out
.ok
.eq(1)
271 with m
.Case(In3Sel
.RS
):
272 comb
+= self
.reg_out
.data
.eq(self
.dec
.RS
)
273 comb
+= self
.reg_out
.ok
.eq(1)
278 class DecodeOut(Elaboratable
):
279 """DecodeOut from instruction
281 decodes output register RA, RT or SPR
284 def __init__(self
, dec
):
286 self
.sel_in
= Signal(OutSel
, reset_less
=True)
287 self
.insn_in
= Signal(32, reset_less
=True)
288 self
.reg_out
= Data(5, "reg_o")
289 self
.spr_out
= Data(SPR
, "spr_o")
290 self
.fast_out
= Data(3, "fast_o")
292 def elaborate(self
, platform
):
295 m
.submodules
.sprmap
= sprmap
= SPRMap()
298 # select Register out field
299 with m
.Switch(self
.sel_in
):
300 with m
.Case(OutSel
.RT
):
301 comb
+= self
.reg_out
.data
.eq(self
.dec
.RT
)
302 comb
+= self
.reg_out
.ok
.eq(1)
303 with m
.Case(OutSel
.RA
):
304 comb
+= self
.reg_out
.data
.eq(self
.dec
.RA
)
305 comb
+= self
.reg_out
.ok
.eq(1)
306 with m
.Case(OutSel
.SPR
):
307 spr
= Signal(10, reset_less
=True)
308 comb
+= spr
.eq(decode_spr_num(self
.dec
.SPR
)) # from XFX
309 # TODO MTSPR 1st spr (fast)
310 with m
.If(op
.internal_op
== MicrOp
.OP_MTSPR
):
313 with m
.Case(SPR
.CTR
.value
):
314 comb
+= self
.fast_out
.data
.eq(FastRegs
.CTR
)
315 comb
+= self
.fast_out
.ok
.eq(1)
316 with m
.Case(SPR
.LR
.value
):
317 comb
+= self
.fast_out
.data
.eq(FastRegs
.LR
)
318 comb
+= self
.fast_out
.ok
.eq(1)
319 with m
.Case(SPR
.TAR
.value
):
320 comb
+= self
.fast_out
.data
.eq(FastRegs
.TAR
)
321 comb
+= self
.fast_out
.ok
.eq(1)
322 with m
.Case(SPR
.SRR0
.value
):
323 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR0
)
324 comb
+= self
.fast_out
.ok
.eq(1)
325 with m
.Case(SPR
.SRR1
.value
):
326 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR1
)
327 comb
+= self
.fast_out
.ok
.eq(1)
328 with m
.Case(SPR
.XER
.value
):
330 # : map to internal SPR numbers
331 # XXX TODO: dec and tb not to go through mapping.
333 comb
+= sprmap
.spr_i
.eq(spr
)
334 comb
+= self
.spr_out
.data
.eq(sprmap
.spr_o
)
335 comb
+= self
.spr_out
.ok
.eq(1)
337 with m
.Switch(op
.internal_op
):
339 # BC or BCREG: implicit register (CTR) NOTE: same in DecodeA
340 with m
.Case(MicrOp
.OP_BC
, MicrOp
.OP_BCREG
):
341 with m
.If(~self
.dec
.BO
[2]): # 3.0B p38 BO2=0, use CTR reg
343 comb
+= self
.fast_out
.data
.eq(FastRegs
.CTR
)
344 comb
+= self
.fast_out
.ok
.eq(1)
346 # RFID 1st spr (fast)
347 with m
.Case(MicrOp
.OP_RFID
):
348 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR0
) # constant: SRR0
349 comb
+= self
.fast_out
.ok
.eq(1)
354 class DecodeOut2(Elaboratable
):
355 """DecodeOut2 from instruction
357 decodes output registers
360 def __init__(self
, dec
):
362 self
.sel_in
= Signal(OutSel
, reset_less
=True)
363 self
.lk
= Signal(reset_less
=True)
364 self
.insn_in
= Signal(32, reset_less
=True)
365 self
.reg_out
= Data(5, "reg_o")
366 self
.fast_out
= Data(3, "fast_o")
368 def elaborate(self
, platform
):
372 # update mode LD/ST uses read-reg A also as an output
373 with m
.If(self
.dec
.op
.upd
== LDSTMode
.update
):
374 comb
+= self
.reg_out
.eq(self
.dec
.RA
)
375 comb
+= self
.reg_out
.ok
.eq(1)
377 # B, BC or BCREG: potential implicit register (LR) output
378 # these give bl, bcl, bclrl, etc.
380 with m
.Switch(op
.internal_op
):
382 # BC* implicit register (LR)
383 with m
.Case(MicrOp
.OP_BC
, MicrOp
.OP_B
, MicrOp
.OP_BCREG
):
384 with m
.If(self
.lk
): # "link" mode
385 comb
+= self
.fast_out
.data
.eq(FastRegs
.LR
) # constant: LR
386 comb
+= self
.fast_out
.ok
.eq(1)
388 # RFID 2nd spr (fast)
389 with m
.Case(MicrOp
.OP_RFID
):
390 comb
+= self
.fast_out
.data
.eq(FastRegs
.SRR1
) # constant: SRR1
391 comb
+= self
.fast_out
.ok
.eq(1)
396 class DecodeRC(Elaboratable
):
397 """DecodeRc from instruction
399 decodes Record bit Rc
402 def __init__(self
, dec
):
404 self
.sel_in
= Signal(RC
, reset_less
=True)
405 self
.insn_in
= Signal(32, reset_less
=True)
406 self
.rc_out
= Data(1, "rc")
408 def elaborate(self
, platform
):
412 # select Record bit out field
413 with m
.Switch(self
.sel_in
):
415 comb
+= self
.rc_out
.data
.eq(self
.dec
.Rc
)
416 comb
+= self
.rc_out
.ok
.eq(1)
418 comb
+= self
.rc_out
.data
.eq(1)
419 comb
+= self
.rc_out
.ok
.eq(1)
420 with m
.Case(RC
.NONE
):
421 comb
+= self
.rc_out
.data
.eq(0)
422 comb
+= self
.rc_out
.ok
.eq(1)
427 class DecodeOE(Elaboratable
):
428 """DecodeOE from instruction
430 decodes OE field: uses RC decode detection which might not be good
432 -- For now, use "rc" in the decode table to decide whether oe exists.
433 -- This is not entirely correct architecturally: For mulhd and
434 -- mulhdu, the OE field is reserved. It remains to be seen what an
435 -- actual POWER9 does if we set it on those instructions, for now we
436 -- test that further down when assigning to the multiplier oe input.
439 def __init__(self
, dec
):
441 self
.sel_in
= Signal(RC
, reset_less
=True)
442 self
.insn_in
= Signal(32, reset_less
=True)
443 self
.oe_out
= Data(1, "oe")
445 def elaborate(self
, platform
):
450 with m
.Switch(op
.internal_op
):
452 # mulhw, mulhwu, mulhd, mulhdu - these *ignore* OE
454 # XXX ARGH! ignoring OE causes incompatibility with microwatt
455 # http://lists.libre-soc.org/pipermail/libre-soc-dev/2020-August/000302.html
456 with m
.Case(#MicrOp.OP_MUL_H64, MicrOp.OP_MUL_H32,
457 MicrOp
.OP_SHL
, MicrOp
.OP_SHR
, MicrOp
.OP_RLC
,
458 MicrOp
.OP_RLCL
, MicrOp
.OP_RLCR
,
462 # all other ops decode OE field
464 # select OE bit out field
465 with m
.Switch(self
.sel_in
):
467 comb
+= self
.oe_out
.data
.eq(self
.dec
.OE
)
468 comb
+= self
.oe_out
.ok
.eq(1)
473 class DecodeCRIn(Elaboratable
):
474 """Decodes input CR from instruction
476 CR indices - insn fields - (not the data *in* the CR) require only 3
477 bits because they refer to CR0-CR7
480 def __init__(self
, dec
):
482 self
.sel_in
= Signal(CRInSel
, reset_less
=True)
483 self
.insn_in
= Signal(32, reset_less
=True)
484 self
.cr_bitfield
= Data(3, "cr_bitfield")
485 self
.cr_bitfield_b
= Data(3, "cr_bitfield_b")
486 self
.cr_bitfield_o
= Data(3, "cr_bitfield_o")
487 self
.whole_reg
= Signal(reset_less
=True)
489 def elaborate(self
, platform
):
493 comb
+= self
.cr_bitfield
.ok
.eq(0)
494 comb
+= self
.cr_bitfield_b
.ok
.eq(0)
495 comb
+= self
.whole_reg
.eq(0)
496 with m
.Switch(self
.sel_in
):
497 with m
.Case(CRInSel
.NONE
):
498 pass # No bitfield activated
499 with m
.Case(CRInSel
.CR0
):
500 comb
+= self
.cr_bitfield
.data
.eq(0)
501 comb
+= self
.cr_bitfield
.ok
.eq(1)
502 with m
.Case(CRInSel
.BI
):
503 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.BI
[2:5])
504 comb
+= self
.cr_bitfield
.ok
.eq(1)
505 with m
.Case(CRInSel
.BFA
):
506 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.FormX
.BFA
)
507 comb
+= self
.cr_bitfield
.ok
.eq(1)
508 with m
.Case(CRInSel
.BA_BB
):
509 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.BA
[2:5])
510 comb
+= self
.cr_bitfield
.ok
.eq(1)
511 comb
+= self
.cr_bitfield_b
.data
.eq(self
.dec
.BB
[2:5])
512 comb
+= self
.cr_bitfield_b
.ok
.eq(1)
513 comb
+= self
.cr_bitfield_o
.data
.eq(self
.dec
.BT
[2:5])
514 comb
+= self
.cr_bitfield_o
.ok
.eq(1)
515 with m
.Case(CRInSel
.BC
):
516 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.BC
[2:5])
517 comb
+= self
.cr_bitfield
.ok
.eq(1)
518 with m
.Case(CRInSel
.WHOLE_REG
):
519 comb
+= self
.whole_reg
.eq(1)
524 class DecodeCROut(Elaboratable
):
525 """Decodes input CR from instruction
527 CR indices - insn fields - (not the data *in* the CR) require only 3
528 bits because they refer to CR0-CR7
531 def __init__(self
, dec
):
533 self
.rc_in
= Signal(reset_less
=True)
534 self
.sel_in
= Signal(CROutSel
, reset_less
=True)
535 self
.insn_in
= Signal(32, reset_less
=True)
536 self
.cr_bitfield
= Data(3, "cr_bitfield")
537 self
.whole_reg
= Signal(reset_less
=True)
539 def elaborate(self
, platform
):
543 comb
+= self
.cr_bitfield
.ok
.eq(0)
544 comb
+= self
.whole_reg
.eq(0)
545 with m
.Switch(self
.sel_in
):
546 with m
.Case(CROutSel
.NONE
):
547 pass # No bitfield activated
548 with m
.Case(CROutSel
.CR0
):
549 comb
+= self
.cr_bitfield
.data
.eq(0)
550 comb
+= self
.cr_bitfield
.ok
.eq(self
.rc_in
) # only when RC=1
551 with m
.Case(CROutSel
.BF
):
552 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.FormX
.BF
)
553 comb
+= self
.cr_bitfield
.ok
.eq(1)
554 with m
.Case(CROutSel
.BT
):
555 comb
+= self
.cr_bitfield
.data
.eq(self
.dec
.FormXL
.BT
[2:5])
556 comb
+= self
.cr_bitfield
.ok
.eq(1)
557 with m
.Case(CROutSel
.WHOLE_REG
):
558 comb
+= self
.whole_reg
.eq(1)
563 class PowerDecode2(Elaboratable
):
564 """PowerDecode2: the main instruction decoder.
566 whilst PowerDecode is responsible for decoding the actual opcode, this
567 module encapsulates further specialist, sparse information and
568 expansion of fields that is inconvenient to have in the CSV files.
569 for example: the encoding of the immediates, which are detected
570 and expanded out to their full value from an annotated (enum)
573 implicit register usage is also set up, here. for example: OP_BC
574 requires implicitly reading CTR, OP_RFID requires implicitly writing
577 in addition, PowerDecoder2 is responsible for detecting whether
578 instructions are illegal (or privileged) or not, and instead of
579 just leaving at that, *replacing* the instruction to execute with
580 a suitable alternative (trap).
583 def __init__(self
, dec
):
586 self
.e
= Decode2ToExecute1Type()
588 # state information needed by the Decoder (TODO: this as a Record)
589 self
.state
= CoreState("dec2")
592 return self
.dec
.ports() + self
.e
.ports()
594 def elaborate(self
, platform
):
597 e_out
, op
, do_out
= self
.e
, self
.dec
.op
, self
.e
.do
598 msr
, cia
= self
.state
.msr
, self
.state
.pc
600 # fill in for a normal instruction (not an exception)
601 # copy over if non-exception, non-privileged etc. is detected
602 e
= Decode2ToExecute1Type()
605 # set up submodule decoders
606 m
.submodules
.dec
= self
.dec
607 m
.submodules
.dec_a
= dec_a
= DecodeA(self
.dec
)
608 m
.submodules
.dec_b
= dec_b
= DecodeB(self
.dec
)
609 m
.submodules
.dec_c
= dec_c
= DecodeC(self
.dec
)
610 m
.submodules
.dec_o
= dec_o
= DecodeOut(self
.dec
)
611 m
.submodules
.dec_o2
= dec_o2
= DecodeOut2(self
.dec
)
612 m
.submodules
.dec_rc
= dec_rc
= DecodeRC(self
.dec
)
613 m
.submodules
.dec_oe
= dec_oe
= DecodeOE(self
.dec
)
614 m
.submodules
.dec_cr_in
= dec_cr_in
= DecodeCRIn(self
.dec
)
615 m
.submodules
.dec_cr_out
= dec_cr_out
= DecodeCROut(self
.dec
)
617 # copy instruction through...
618 for i
in [do
.insn
, dec_a
.insn_in
, dec_b
.insn_in
,
619 dec_c
.insn_in
, dec_o
.insn_in
, dec_o2
.insn_in
, dec_rc
.insn_in
,
620 dec_oe
.insn_in
, dec_cr_in
.insn_in
, dec_cr_out
.insn_in
]:
621 comb
+= i
.eq(self
.dec
.opcode_in
)
623 # ...and subdecoders' input fields
624 comb
+= dec_a
.sel_in
.eq(op
.in1_sel
)
625 comb
+= dec_b
.sel_in
.eq(op
.in2_sel
)
626 comb
+= dec_c
.sel_in
.eq(op
.in3_sel
)
627 comb
+= dec_o
.sel_in
.eq(op
.out_sel
)
628 comb
+= dec_o2
.sel_in
.eq(op
.out_sel
)
629 comb
+= dec_o2
.lk
.eq(do
.lk
)
630 comb
+= dec_rc
.sel_in
.eq(op
.rc_sel
)
631 comb
+= dec_oe
.sel_in
.eq(op
.rc_sel
) # XXX should be OE sel
632 comb
+= dec_cr_in
.sel_in
.eq(op
.cr_in
)
633 comb
+= dec_cr_out
.sel_in
.eq(op
.cr_out
)
634 comb
+= dec_cr_out
.rc_in
.eq(dec_rc
.rc_out
.data
)
637 comb
+= do
.msr
.eq(msr
)
638 comb
+= do
.cia
.eq(cia
)
640 # set up instruction, pick fn unit
641 # no op: defaults to OP_ILLEGAL
642 comb
+= do
.insn_type
.eq(op
.internal_op
)
643 comb
+= do
.fn_unit
.eq(op
.function_unit
)
645 # registers a, b, c and out and out2 (LD/ST EA)
646 comb
+= e
.read_reg1
.eq(dec_a
.reg_out
)
647 comb
+= e
.read_reg2
.eq(dec_b
.reg_out
)
648 comb
+= e
.read_reg3
.eq(dec_c
.reg_out
)
649 comb
+= e
.write_reg
.eq(dec_o
.reg_out
)
650 comb
+= e
.write_ea
.eq(dec_o2
.reg_out
)
651 comb
+= do
.imm_data
.eq(dec_b
.imm_out
) # immediate in RB (usually)
652 comb
+= do
.zero_a
.eq(dec_a
.immz_out
) # RA==0 detected
655 comb
+= do
.rc
.eq(dec_rc
.rc_out
)
656 comb
+= do
.oe
.eq(dec_oe
.oe_out
)
659 comb
+= e
.read_spr1
.eq(dec_a
.spr_out
)
660 comb
+= e
.write_spr
.eq(dec_o
.spr_out
)
663 comb
+= e
.read_fast1
.eq(dec_a
.fast_out
)
664 comb
+= e
.read_fast2
.eq(dec_b
.fast_out
)
665 comb
+= e
.write_fast1
.eq(dec_o
.fast_out
)
666 comb
+= e
.write_fast2
.eq(dec_o2
.fast_out
)
668 # condition registers (CR)
669 comb
+= e
.read_cr1
.eq(dec_cr_in
.cr_bitfield
)
670 comb
+= e
.read_cr2
.eq(dec_cr_in
.cr_bitfield_b
)
671 comb
+= e
.read_cr3
.eq(dec_cr_in
.cr_bitfield_o
)
672 comb
+= e
.write_cr
.eq(dec_cr_out
.cr_bitfield
)
674 comb
+= do
.read_cr_whole
.eq(dec_cr_in
.whole_reg
)
675 comb
+= do
.write_cr_whole
.eq(dec_cr_out
.whole_reg
)
676 comb
+= do
.write_cr0
.eq(dec_cr_out
.cr_bitfield
.ok
)
678 # decoded/selected instruction flags
679 comb
+= do
.data_len
.eq(op
.ldst_len
)
680 comb
+= do
.invert_in
.eq(op
.inv_a
)
681 comb
+= do
.invert_out
.eq(op
.inv_out
)
682 comb
+= do
.input_carry
.eq(op
.cry_in
) # carry comes in
683 comb
+= do
.output_carry
.eq(op
.cry_out
) # carry goes out
684 comb
+= do
.is_32bit
.eq(op
.is_32b
)
685 comb
+= do
.is_signed
.eq(op
.sgn
)
687 comb
+= do
.lk
.eq(self
.dec
.LK
) # XXX TODO: accessor
689 comb
+= do
.byte_reverse
.eq(op
.br
)
690 comb
+= do
.sign_extend
.eq(op
.sgn_ext
)
691 comb
+= do
.ldst_mode
.eq(op
.upd
) # LD/ST mode (update, cache-inhibit)
693 # These should be removed eventually
694 comb
+= do
.input_cr
.eq(op
.cr_in
) # condition reg comes in
695 comb
+= do
.output_cr
.eq(op
.cr_out
) # condition reg goes in
697 # sigh this is exactly the sort of thing for which the
698 # decoder is designed to not need. MTSPR, MFSPR and others need
699 # access to the XER bits. however setting e.oe is not appropriate
700 with m
.If(op
.internal_op
== MicrOp
.OP_MFSPR
):
701 comb
+= e
.xer_in
.eq(1)
702 with m
.If(op
.internal_op
== MicrOp
.OP_MTSPR
):
703 comb
+= e
.xer_out
.eq(1)
705 # set the trapaddr to 0x700 for a td/tw/tdi/twi operation
706 with m
.If(op
.internal_op
== MicrOp
.OP_TRAP
):
707 # *DO NOT* call self.trap here. that would reset absolutely
708 # rverything including destroying read of RA and RB.
709 comb
+= do
.trapaddr
.eq(0x70) # addr=0x700 (strip first nibble)
711 # TODO: get msr, then can do privileged instruction
712 with m
.If(instr_is_priv(m
, op
.internal_op
, e
.do
.insn
) & msr
[MSR
.PR
]):
713 # privileged instruction trap
714 self
.trap(m
, TT
.PRIV
, 0x700)
716 # illegal instruction must redirect to trap. this is done by
717 # *overwriting* the decoded instruction and starting again.
718 # (note: the same goes for interrupts and for privileged operations,
719 # just with different trapaddr and traptype)
720 with m
.Elif(op
.internal_op
== MicrOp
.OP_ILLEGAL
):
721 # illegal instruction trap
722 self
.trap(m
, TT
.ILLEG
, 0x700)
724 # no exception, just copy things to the output
728 # trap: (note e.insn_type so this includes OP_ILLEGAL) set up fast regs
729 # Note: OP_SC could actually be modified to just be a trap
730 with m
.If((do_out
.insn_type
== MicrOp
.OP_TRAP
) |
731 (do_out
.insn_type
== MicrOp
.OP_SC
)):
732 # TRAP write fast1 = SRR0
733 comb
+= e_out
.write_fast1
.data
.eq(FastRegs
.SRR0
) # constant: SRR0
734 comb
+= e_out
.write_fast1
.ok
.eq(1)
735 # TRAP write fast2 = SRR1
736 comb
+= e_out
.write_fast2
.data
.eq(FastRegs
.SRR1
) # constant: SRR1
737 comb
+= e_out
.write_fast2
.ok
.eq(1)
739 # RFID: needs to read SRR0/1
740 with m
.If(do_out
.insn_type
== MicrOp
.OP_RFID
):
741 # TRAP read fast1 = SRR0
742 comb
+= e_out
.read_fast1
.data
.eq(FastRegs
.SRR0
) # constant: SRR0
743 comb
+= e_out
.read_fast1
.ok
.eq(1)
744 # TRAP read fast2 = SRR1
745 comb
+= e_out
.read_fast2
.data
.eq(FastRegs
.SRR1
) # constant: SRR1
746 comb
+= e_out
.read_fast2
.ok
.eq(1)
750 def trap(self
, m
, traptype
, trapaddr
):
751 """trap: this basically "rewrites" the decoded instruction as a trap
754 op
, do
, e
= self
.dec
.op
, self
.e
.do
, self
.e
755 comb
+= e
.eq(0) # reset eeeeeverything
758 comb
+= do
.insn
.eq(self
.dec
.opcode_in
)
759 comb
+= do
.insn_type
.eq(MicrOp
.OP_TRAP
)
760 comb
+= do
.fn_unit
.eq(Function
.TRAP
)
761 comb
+= do
.trapaddr
.eq(trapaddr
>> 4) # cut bottom 4 bits
762 comb
+= do
.traptype
.eq(traptype
) # request type
763 comb
+= do
.msr
.eq(self
.state
.msr
) # copy of MSR "state"
764 comb
+= do
.cia
.eq(self
.state
.pc
) # copy of PC "state"
767 def get_rdflags(e
, cu
):
769 for idx
in range(cu
.n_src
):
770 regfile
, regname
, _
= cu
.get_in_spec(idx
)
771 rdflag
, read
= regspec_decode_read(e
, regfile
, regname
)
773 print("rdflags", rdl
)
777 if __name__
== '__main__':
778 pdecode
= create_pdecode()
779 dec2
= PowerDecode2(pdecode
)
780 vl
= rtlil
.convert(dec2
, ports
=dec2
.ports() + pdecode
.ports())
781 with
open("dec2.il", "w") as f
: