b5dc80eef6aa4be023e6e772fe766ea7a01d9163
1 # Manual translation and adaptation of rotator.vhdl from microwatt into nmigen
4 from nmigen
import (Elaboratable
, Signal
, Module
, Const
, Cat
, Repl
,
6 from soc
.fu
.shift_rot
.rotl
import ROTL
7 from nmutil
.extend
import exts
10 # note BE bit numbering
11 def right_mask(m
, mask_begin
):
12 ret
= Signal(64, name
="right_mask", reset_less
=True)
13 with m
.If(mask_begin
<= 64):
14 m
.d
.comb
+= ret
.eq((1<<(64-mask_begin
)) - 1)
17 def left_mask(m
, mask_end
):
18 ret
= Signal(64, name
="left_mask", reset_less
=True)
19 m
.d
.comb
+= ret
.eq(~
((1<<(63-mask_end
)) - 1))
23 class Rotator(Elaboratable
):
24 """Rotator: covers multiple POWER9 rotate functions
29 * rlw*, rldic, rldicr, rldimi
36 * arith = 1 when is_signed
37 * right_shift = 1 when insn_type is OP_SHR
38 * clear_left = 1 when insn_type is OP_RLC or OP_RLCL
39 * clear_right = 1 when insn_type is OP_RLC or OP_RLCR
43 self
.me
= Signal(5, reset_less
=True) # ME field
44 self
.mb
= Signal(5, reset_less
=True) # MB field
45 self
.mb_extra
= Signal(1, reset_less
=True) # extra bit of mb in MD-form
46 self
.ra
= Signal(64, reset_less
=True) # RA
47 self
.rs
= Signal(64, reset_less
=True) # RS
48 self
.shift
= Signal(7, reset_less
=True) # RB[0:7]
49 self
.is_32bit
= Signal(reset_less
=True)
50 self
.right_shift
= Signal(reset_less
=True)
51 self
.arith
= Signal(reset_less
=True)
52 self
.clear_left
= Signal(reset_less
=True)
53 self
.clear_right
= Signal(reset_less
=True)
54 self
.sign_ext_rs
= Signal(reset_less
=True)
56 self
.result_o
= Signal(64, reset_less
=True)
57 self
.carry_out_o
= Signal(reset_less
=True)
59 def elaborate(self
, platform
):
62 ra
, rs
= self
.ra
, self
.rs
65 rot_count
= Signal(6, reset_less
=True)
66 rot
= Signal(64, reset_less
=True)
67 sh
= Signal(7, reset_less
=True)
68 mb
= Signal(7, reset_less
=True)
69 me
= Signal(7, reset_less
=True)
70 mr
= Signal(64, reset_less
=True)
71 ml
= Signal(64, reset_less
=True)
72 output_mode
= Signal(2, reset_less
=True)
73 hi32
= Signal(32, reset_less
=True)
74 repl32
= Signal(64, reset_less
=True)
76 # First replicate bottom 32 bits to both halves if 32-bit
77 with m
.If(self
.is_32bit
):
78 comb
+= hi32
.eq(rs
[0:32])
79 with m
.Elif(self
.sign_ext_rs
):
80 # sign-extend bottom 32 bits
81 comb
+= hi32
.eq(Repl(rs
[31], 32))
83 comb
+= hi32
.eq(rs
[32:64])
84 comb
+= repl32
.eq(Cat(rs
[0:32], hi32
))
86 shift_signed
= Signal(signed(6))
87 comb
+= shift_signed
.eq(self
.shift
[0:6])
89 # Negate shift count for right shifts
90 with m
.If(self
.right_shift
):
91 comb
+= rot_count
.eq(-shift_signed
)
93 comb
+= rot_count
.eq(self
.shift
[0:6])
96 m
.submodules
.rotl
= rotl
= ROTL(64)
97 comb
+= rotl
.a
.eq(repl32
)
98 comb
+= rotl
.b
.eq(rot_count
)
99 comb
+= rot
.eq(rotl
.o
)
101 # Trim shift count to 6 bits for 32-bit shifts
102 comb
+= sh
.eq(Cat(self
.shift
[0:6], self
.shift
[6] & ~self
.is_32bit
))
104 # XXX errr... we should already have these, in Fields? oh well
105 # Work out mask begin/end indexes (caution, big-endian bit numbering)
108 with m
.If(self
.clear_left
):
109 comb
+= mb
.eq(self
.mb
)
110 with m
.If(self
.is_32bit
):
111 comb
+= mb
[5:7].eq(Const(0b01, 2))
113 comb
+= mb
[5:7].eq(Cat(self
.mb_extra
, Const(0b0, 1)))
114 with m
.Elif(self
.right_shift
):
115 # this is basically mb = sh + (is_32bit? 32: 0);
117 with m
.If(self
.is_32bit
):
118 comb
+= mb
[5:7].eq(Cat(~sh
[5], sh
[5]))
120 comb
+= mb
.eq(Cat(Const(0b0, 5), self
.is_32bit
, Const(0b0, 1)))
123 with m
.If(self
.clear_right
& self
.is_32bit
):
124 # TODO: track down where this is. have to use fields.
125 comb
+= me
.eq(Cat(self
.me
, Const(0b01, 2)))
126 with m
.Elif(self
.clear_right
& ~self
.clear_left
):
127 # this is me, have to use fields
128 comb
+= me
.eq(Cat(self
.mb
, self
.mb_extra
, Const(0b0, 1)))
130 # effectively, 63 - sh
131 comb
+= me
.eq(Cat(~sh
[0:6], sh
[6]))
133 # Calculate left and right masks
134 comb
+= mr
.eq(right_mask(m
, mb
))
135 comb
+= ml
.eq(left_mask(m
, me
))
137 # Work out output mode
139 # 0w for rlw*, rldic, rldicr, rldimi, where w = 1 iff mb > me
140 # 10 for rldicl, sr[wd]
141 # 1z for sra[wd][i], z = 1 if rs is negative
142 with m
.If((self
.clear_left
& ~self
.clear_right
) | self
.right_shift
):
143 comb
+= output_mode
.eq(Cat(self
.arith
& repl32
[63], Const(1, 1)))
145 mbgt
= self
.clear_right
& (mb
[0:6] > me
[0:6])
146 comb
+= output_mode
.eq(Cat(mbgt
, Const(0, 1)))
148 # Generate output from rotated input and masks
149 with m
.Switch(output_mode
):
151 comb
+= self
.result_o
.eq((rot
& (mr
& ml
)) |
(ra
& ~
(mr
& ml
)))
153 comb
+= self
.result_o
.eq((rot
& (mr | ml
)) |
(ra
& ~
(mr | ml
)))
155 comb
+= self
.result_o
.eq(rot
& mr
)
157 comb
+= self
.result_o
.eq(rot | ~mr
)
158 # Generate carry output for arithmetic shift right of -ve value
159 comb
+= self
.carry_out_o
.eq((rs
& ~ml
).bool() & rs
[0])