document rb as sh
[soc.git] / src / soc / fu / shift_rot / main_stage.py
1 # This stage is intended to do most of the work of executing shift
2 # instructions, as well as carry and overflow generation. This module
3 # however should not gate the carry or overflow, that's up to the
4 # output stage
5 from nmigen import (Module, Signal, Cat, Repl, Mux, Const)
6 from nmutil.pipemodbase import PipeModBase
7 from soc.fu.logical.pipe_data import LogicalOutputData
8 from soc.fu.shift_rot.pipe_data import ShiftRotInputData
9 from ieee754.part.partsig import PartitionedSignal
10 from soc.decoder.power_enums import MicrOp
11 from soc.fu.shift_rot.rotator import Rotator
12
13 from soc.decoder.power_fields import DecodeFields
14 from soc.decoder.power_fieldsn import SignalBitRange
15
16
17 class ShiftRotMainStage(PipeModBase):
18 def __init__(self, pspec):
19 super().__init__(pspec, "main")
20 self.fields = DecodeFields(SignalBitRange, [self.i.ctx.op.insn])
21 self.fields.create_specs()
22
23 def ispec(self):
24 return ShiftRotInputData(self.pspec)
25
26 def ospec(self):
27 return LogicalOutputData(self.pspec)
28
29 def elaborate(self, platform):
30 m = Module()
31 comb = m.d.comb
32 op = self.i.ctx.op
33 o = self.o.o
34
35 # NOTE: the sh field immediate is read in by PowerDecode2
36 # (actually DecodeRB), whereupon by way of rb "immediate" mode
37 # it ends up in self.i.rb.
38
39 # obtain me and mb fields from instruction.
40 m_fields = self.fields.instrs['M']
41 md_fields = self.fields.instrs['MD']
42 mb = Signal(m_fields['MB'][0:-1].shape())
43 me = Signal(m_fields['ME'][0:-1].shape())
44 mb_extra = Signal(1, reset_less=True)
45 comb += mb.eq(m_fields['MB'][0:-1])
46 comb += me.eq(m_fields['ME'][0:-1])
47 comb += mb_extra.eq(md_fields['mb'][0:-1][0])
48
49 # set up microwatt rotator module
50 m.submodules.rotator = rotator = Rotator()
51 comb += [
52 rotator.me.eq(me),
53 rotator.mb.eq(mb),
54 rotator.mb_extra.eq(mb_extra),
55 rotator.rs.eq(self.i.rs),
56 rotator.ra.eq(self.i.a),
57 rotator.shift.eq(self.i.rb), # can also be sh (in immediate mode)
58 rotator.is_32bit.eq(op.is_32bit),
59 rotator.arith.eq(op.is_signed),
60 ]
61
62 comb += o.ok.eq(1) # defaults to enabled
63
64 # instruction rotate type
65 mode = Signal(4, reset_less=True)
66 with m.Switch(op.insn_type):
67 with m.Case(MicrOp.OP_SHL): comb += mode.eq(0b0000) # L-shift
68 with m.Case(MicrOp.OP_SHR): comb += mode.eq(0b0001) # R-shift
69 with m.Case(MicrOp.OP_RLC): comb += mode.eq(0b0110) # clear LR
70 with m.Case(MicrOp.OP_RLCL): comb += mode.eq(0b0010) # clear L
71 with m.Case(MicrOp.OP_RLCR): comb += mode.eq(0b0100) # clear R
72 with m.Case(MicrOp.OP_EXTSWSLI): comb += mode.eq(0b1000) # L-ext
73 with m.Default():
74 comb += o.ok.eq(0) # otherwise disable
75
76 comb += Cat(rotator.right_shift,
77 rotator.clear_left,
78 rotator.clear_right,
79 rotator.sign_ext_rs).eq(mode)
80
81 # outputs from the microwatt rotator module
82 comb += [o.data.eq(rotator.result_o),
83 self.o.xer_ca.data.eq(Repl(rotator.carry_out_o, 2))]
84
85 ###### sticky overflow and context, both pass-through #####
86
87 comb += self.o.ctx.eq(self.i.ctx)
88
89 return m