1 # This stage is intended to do most of the work of executing Logical
2 # instructions. This is OR, AND, XOR, POPCNT, PRTY, CMPB, BPERMD, CNTLZ
3 # however input and output stages also perform bit-negation on input(s)
4 # and output, as well as carry and overflow generation.
5 # This module however should not gate the carry or overflow, that's up
8 from nmigen
import (Module
, Signal
, Cat
, Repl
, Mux
, Const
, Array
)
9 from nmutil
.pipemodbase
import PipeModBase
10 from nmutil
.clz
import CLZ
11 from soc
.fu
.logical
.pipe_data
import LogicalInputData
12 from soc
.fu
.logical
.bpermd
import Bpermd
13 from soc
.fu
.logical
.popcount
import Popcount
14 from soc
.fu
.logical
.pipe_data
import LogicalOutputData
15 from ieee754
.part
.partsig
import PartitionedSignal
16 from soc
.decoder
.power_enums
import MicrOp
18 from soc
.decoder
.power_fields
import DecodeFields
19 from soc
.decoder
.power_fieldsn
import SignalBitRange
22 class LogicalMainStage(PipeModBase
):
23 def __init__(self
, pspec
):
24 super().__init
__(pspec
, "main")
25 self
.fields
= DecodeFields(SignalBitRange
, [self
.i
.ctx
.op
.insn
])
26 self
.fields
.create_specs()
29 return LogicalInputData(self
.pspec
)
32 return LogicalOutputData(self
.pspec
)
34 def elaborate(self
, platform
):
37 op
, a
, b
, o
= self
.i
.ctx
.op
, self
.i
.a
, self
.i
.b
, self
.o
.o
39 comb
+= o
.ok
.eq(1) # overridden if no op activates
41 m
.submodules
.bpermd
= bpermd
= Bpermd(64)
42 m
.submodules
.popcount
= popcount
= Popcount()
44 ##########################
45 # main switch for logic ops AND, OR and XOR, cmpb, parity, and popcount
47 with m
.Switch(op
.insn_type
):
50 ###### AND, OR, XOR v3.0B p92-95
52 with m
.Case(MicrOp
.OP_AND
):
53 comb
+= o
.data
.eq(a
& b
)
54 with m
.Case(MicrOp
.OP_OR
):
55 comb
+= o
.data
.eq(a | b
)
56 with m
.Case(MicrOp
.OP_XOR
):
57 comb
+= o
.data
.eq(a ^ b
)
62 with m
.Case(MicrOp
.OP_CMPB
):
65 slc
= slice(i
*8, (i
+1)*8)
66 l
.append(Repl(a
[slc
] == b
[slc
], 8))
67 comb
+= o
.data
.eq(Cat(*l
))
70 ###### popcount v3.0B p97, p98
72 with m
.Case(MicrOp
.OP_POPCNT
):
73 comb
+= popcount
.a
.eq(a
)
74 comb
+= popcount
.b
.eq(b
)
75 comb
+= popcount
.data_len
.eq(op
.data_len
)
76 comb
+= o
.data
.eq(popcount
.o
)
79 ###### parity v3.0B p98
81 with m
.Case(MicrOp
.OP_PRTY
):
82 # strange instruction which XORs together the LSBs of each byte
83 par0
= Signal(reset_less
=True)
84 par1
= Signal(reset_less
=True)
85 comb
+= par0
.eq(Cat(a
[0], a
[8], a
[16], a
[24]).xor())
86 comb
+= par1
.eq(Cat(a
[32], a
[40], a
[48], a
[56]).xor())
87 with m
.If(op
.data_len
[3] == 1):
88 comb
+= o
.data
.eq(par0 ^ par1
)
91 comb
+= o
[32].eq(par1
)
94 ###### cntlz v3.0B p99
96 with m
.Case(MicrOp
.OP_CNTZ
):
97 XO
= self
.fields
.FormX
.XO
[0:-1]
98 count_right
= Signal(reset_less
=True)
99 comb
+= count_right
.eq(XO
[-1])
101 cntz_i
= Signal(64, reset_less
=True)
102 a32
= Signal(32, reset_less
=True)
103 comb
+= a32
.eq(a
[0:32])
105 with m
.If(op
.is_32bit
):
106 comb
+= cntz_i
.eq(Mux(count_right
, a32
[::-1], a32
))
108 comb
+= cntz_i
.eq(Mux(count_right
, a
[::-1], a
))
110 m
.submodules
.clz
= clz
= CLZ(64)
111 comb
+= clz
.sig_in
.eq(cntz_i
)
112 comb
+= o
.data
.eq(Mux(op
.is_32bit
, clz
.lz
-32, clz
.lz
))
115 ###### bpermd v3.0B p100
117 with m
.Case(MicrOp
.OP_BPERM
):
118 comb
+= bpermd
.rs
.eq(a
)
119 comb
+= bpermd
.rb
.eq(b
)
120 comb
+= o
.data
.eq(bpermd
.ra
)
125 ###### context, pass-through #####
127 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)