fhdl/bitcontainer: remove fslice and freversed
[litex.git] / migen / fhdl / bitcontainer.py
1 from migen.fhdl import structure as f
2
3
4 __all__ = ["log2_int", "bits_for", "flen", "fiter"]
5
6
7 def log2_int(n, need_pow2=True):
8 l = 1
9 r = 0
10 while l < n:
11 l *= 2
12 r += 1
13 if need_pow2 and l != n:
14 raise ValueError("Not a power of 2")
15 return r
16
17
18 def bits_for(n, require_sign_bit=False):
19 if n > 0:
20 r = log2_int(n + 1, False)
21 else:
22 require_sign_bit = True
23 r = log2_int(-n, False)
24 if require_sign_bit:
25 r += 1
26 return r
27
28
29 def value_bits_sign(v):
30 if isinstance(v, (f.Constant, f.Signal)):
31 return v.nbits, v.signed
32 elif isinstance(v, (f.ClockSignal, f.ResetSignal)):
33 return 1, False
34 elif isinstance(v, f._Operator):
35 obs = list(map(value_bits_sign, v.operands))
36 if v.op == "+" or v.op == "-":
37 if not obs[0][1] and not obs[1][1]:
38 # both operands unsigned
39 return max(obs[0][0], obs[1][0]) + 1, False
40 elif obs[0][1] and obs[1][1]:
41 # both operands signed
42 return max(obs[0][0], obs[1][0]) + 1, True
43 elif not obs[0][1] and obs[1][1]:
44 # first operand unsigned (add sign bit), second operand signed
45 return max(obs[0][0] + 1, obs[1][0]) + 1, True
46 else:
47 # first signed, second operand unsigned (add sign bit)
48 return max(obs[0][0], obs[1][0] + 1) + 1, True
49 elif v.op == "*":
50 if not obs[0][1] and not obs[1][1]:
51 # both operands unsigned
52 return obs[0][0] + obs[1][0], False
53 elif obs[0][1] and obs[1][1]:
54 # both operands signed
55 return obs[0][0] + obs[1][0] - 1, True
56 else:
57 # one operand signed, the other unsigned (add sign bit)
58 return obs[0][0] + obs[1][0] + 1 - 1, True
59 elif v.op == "<<<":
60 if obs[1][1]:
61 extra = 2**(obs[1][0] - 1) - 1
62 else:
63 extra = 2**obs[1][0] - 1
64 return obs[0][0] + extra, obs[0][1]
65 elif v.op == ">>>":
66 if obs[1][1]:
67 extra = 2**(obs[1][0] - 1)
68 else:
69 extra = 0
70 return obs[0][0] + extra, obs[0][1]
71 elif v.op == "&" or v.op == "^" or v.op == "|":
72 if not obs[0][1] and not obs[1][1]:
73 # both operands unsigned
74 return max(obs[0][0], obs[1][0]), False
75 elif obs[0][1] and obs[1][1]:
76 # both operands signed
77 return max(obs[0][0], obs[1][0]), True
78 elif not obs[0][1] and obs[1][1]:
79 # first operand unsigned (add sign bit), second operand signed
80 return max(obs[0][0] + 1, obs[1][0]), True
81 else:
82 # first signed, second operand unsigned (add sign bit)
83 return max(obs[0][0], obs[1][0] + 1), True
84 elif v.op == "<" or v.op == "<=" or v.op == "==" or v.op == "!=" \
85 or v.op == ">" or v.op == ">=":
86 return 1, False
87 elif v.op == "~":
88 return obs[0]
89 else:
90 raise TypeError
91 elif isinstance(v, f._Slice):
92 return v.stop - v.start, value_bits_sign(v.value)[1]
93 elif isinstance(v, f.Cat):
94 return sum(value_bits_sign(sv)[0] for sv in v.l), False
95 elif isinstance(v, f.Replicate):
96 return (value_bits_sign(v.v)[0])*v.n, False
97 elif isinstance(v, f._ArrayProxy):
98 bsc = list(map(value_bits_sign, v.choices))
99 return max(bs[0] for bs in bsc), any(bs[1] for bs in bsc)
100 else:
101 raise TypeError("Can not calculate bit length of {} {}".format(
102 type(v), v))
103
104
105 def flen(v):
106 """Bit length of an expression
107
108 Parameters
109 ----------
110 v : int, bool or Value
111
112 Returns
113 -------
114 int
115 Number of bits required to store `v` or available in `v`
116
117 Examples
118 --------
119 >>> flen(f.Signal(8))
120 8
121 >>> flen(0xaa)
122 8
123 """
124 return value_bits_sign(v)[0]
125
126
127 def fiter(v):
128 """Bit iterator
129
130 Parameters
131 ----------
132 v : int, bool or Value
133
134 Returns
135 -------
136 iter
137 Iterator over the bits in `v`
138
139 Examples
140 --------
141 >>> list(fiter(f.Signal(2))) #doctest: +ELLIPSIS
142 [<migen.fhdl.structure._Slice object at 0x...>, <migen.fhdl.structure._Slice object at 0x...>]
143 >>> list(fiter(4))
144 [0, 0, 1]
145 """
146 if isinstance(v, (bool, int)):
147 return ((v >> i) & 1 for i in range(bits_for(v)))
148 elif isinstance(v, f.Value):
149 return (v[i] for i in range(flen(v)))
150 else:
151 raise TypeError("Can not bit-iterate {} {}".format(type(v), v))