2 from nmigen
import (Module
, Signal
, Cat
, Repl
, Mux
, Const
, signed
)
3 from nmutil
.pipemodbase
import PipeModBase
4 from nmutil
.clz
import CLZ
5 from soc
.fu
.trap
.pipe_data
import TrapInputData
, TrapOutputData
6 from soc
.decoder
.power_enums
import InternalOp
8 from soc
.decoder
.power_fields
import DecodeFields
9 from soc
.decoder
.power_fieldsn
import SignalBitRange
12 def array_of(count
, bitwidth
):
14 for i
in range(count
):
15 res
.append(Signal(bitwidth
, reset_less
=True))
19 class LogicalMainStage(PipeModBase
):
20 def __init__(self
, pspec
):
21 super().__init
__(pspec
, "main")
22 self
.fields
= DecodeFields(SignalBitRange
, [self
.i
.ctx
.op
.insn
])
23 self
.fields
.create_specs()
26 return TrapInputData(self
.pspec
)
29 return TrapOutputData(self
.pspec
)
31 def elaborate(self
, platform
):
36 i_fields
= self
.fields
.FormD
37 to
= Signal(i_fields
.TO
[0:-1].shape())
38 comb
+= to
.eq(i_fields
.TO
[0:-1])
40 a_signed
= Signal(signed(64), reset_less
=True)
41 b_signed
= Signal(signed(64), reset_less
=True)
43 a
= Signal(64, reset_less
=True)
44 b
= Signal(64, reset_less
=True)
46 with m
.If(self
.i
.ctx
.op
.is_32bit
):
47 comb
+= a_signed
.eq(self
.i
.a
[0:32],
48 Repl(self
.i
.a
[32], 32))
49 comb
+= b_signed
.eq(self
.i
.b
[0:32],
50 Repl(self
.i
.b
[32], 32))
51 comb
+= a
.eq(self
.i
.a
[0:32])
52 comb
+= b
.eq(self
.i
.b
[0:32])
54 comb
+= a_signed
.eq(self
.i
.a
)
55 comb
+= b_signed
.eq(self
.i
.b
)
56 comb
+= a
.eq(self
.i
.a
)
57 comb
+= b
.eq(self
.i
.b
)
61 lt_unsigned
= Signal()
62 gt_unsigned
= Signal()
65 comb
+= lt_signed
.eq(a_signed
< b_signed
)
66 comb
+= gt_signed
.eq(a_signed
> b_signed
)
67 comb
+= lt_unsigned
.eq(a
< b
)
68 comb
+= gt_unsigned
.eq(a
> b
)
69 comb
+= equal
.eq(a
== b
)
72 # They're in reverse bit order because POWER. Check Book 1,
73 # Appendix C.6 for chart
74 comb
+= trap_bits
.eq(Cat(gt_unsigned
, lt_unsigned
, equal
,
75 gt_signed
, lt_signed
))
76 should_trap
= Signal()
77 comb
+= should_trap
.eq((trap_bits
& to
).any())
80 with m
.Case(InternalOp
.OP_TRAP
):
81 with m
.If(should_trap
):
82 comb
+= self
.o
.nia
.eq(0x700)
83 comb
+= self
.o
.srr1
.eq(self
.i
.msr
)
84 comb
+= self
.o
.srr1
[63-46].eq(1)
85 comb
+= self
.o
.srr0
.eq(self
.i
.cia
)
88 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)
89 comb
+= self
.o
.should_trap
.eq(should_trap
)