Skip to content

Commit 3508dbc

Browse files
adder: Remove unnecessary xor of lowest bit
Mikey noticed the lowest bit of the adder has an xor with 0. To fix this, we have to move the flattening of the array of signals later. Signed-off-by: Anton Blanchard <anton@ozlabs.org>
1 parent be18a29 commit 3508dbc

1 file changed

Lines changed: 10 additions & 12 deletions

File tree

vlsiffra/adder.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import math
22

3-
from amaranth import Elaboratable, Module, Signal, Const
3+
from amaranth import Elaboratable, Module, Signal, Cat
44

55

66
class AdderFramework(Elaboratable):
@@ -68,32 +68,30 @@ def elaborate(self, platform):
6868
m.d.comb += p_tmp[i].eq(self._p[i])
6969

7070
self._calculate_pg()
71+
o = [Signal() for i in range(self._bits)]
7172

7273
# g is the carry out signal. We need to shift it left one bit then
73-
# xor it with the sum (ie p_tmp). Since we have a list of 1 bit
74-
# signals, just insert a constant zero signal at the head of of the
75-
# list to shift g.
76-
self._g.insert(0, Const(0))
74+
# xor it with the sum (ie p_tmp). This means bit 0 is just p.
75+
m.d.comb += o[0].eq(p_tmp[0])
7776

78-
o = Signal(self._bits)
79-
for i in range(self._bits):
80-
# This also flattens the list of bits when writing to o
81-
self._generate_xor(p_tmp[i], self._g[i], o[i])
77+
for i in range(1, self._bits):
78+
self._generate_xor(p_tmp[i], self._g[i - 1], o[i])
8279

8380
if self._carry_out:
8481
carry_out = Signal()
85-
m.d.comb += carry_out.eq(self._g[self._bits])
82+
m.d.comb += carry_out.eq(self._g[self._bits - 1])
8683

8784
if self._register_output:
8885
m.d.sync += self.carry_out.eq(carry_out)
8986
else:
9087
m.d.comb += self.carry_out.eq(carry_out)
9188

9289
o2 = Signal(self._bits, reset_less=True)
90+
# This also flattens the list of bits when writing to o2
9391
if self._register_output:
94-
m.d.sync += o2.eq(o)
92+
m.d.sync += o2.eq(Cat(o[n] for n in range(len(o))))
9593
else:
96-
m.d.comb += o2.eq(o)
94+
m.d.comb += o2.eq(Cat(o[n] for n in range(len(o))))
9795

9896
m.d.comb += self.o.eq(o2)
9997
return m

0 commit comments

Comments
 (0)