Skip to content

Commit

Permalink
fir: update, add sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Sep 21, 2020
1 parent db6378c commit a71caf9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 30 deletions.
89 changes: 63 additions & 26 deletions misoc/cores/fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,24 @@ def __init__(self, n, scale, **kwargs):
self.submodules.coeff = SRStorage(
n, (len(self.dsp.b), True), mode="circular")
self.submodules.sample = SRStorage(n, width, mode="old-first")
self.out = Endpoint([("data", (len(self.dsp.pr), True))])
self.output = Endpoint([("data", (len(self.dsp.pr), True))])
self.bias = Signal.like(self.dsp.c, reset=(1 << max(0, scale - 1)) - 1)

p_dsp = 4 # a/d/b0, ad/b1, m, p
q = Signal(p_dsp)
eop_pipe = Signal(p_dsp)
ack_dsp = Signal()
self.sync += [
If(ack_dsp,
q.eq(Cat(self.sample.out.eop, q)),
eop_pipe.eq(Cat(self.sample.out.eop, eop_pipe)),
),
]
self.comb += [
self.sample.load.eop.eq(1),

self.coeff.load.stb.eq(self.sample.load.stb), # ignore ack

ack_dsp.eq(self.sample.out.stb & (~self.out.stb | self.out.ack)),
ack_dsp.eq(self.sample.out.stb &
(~self.output.stb | self.output.ack)),

self.sample.out.ack.eq(ack_dsp),
self.coeff.out.ack.eq(ack_dsp), # ignore stb
Expand All @@ -229,16 +230,16 @@ def __init__(self, n, scale, **kwargs):
self.dsp.b.eq(self.coeff.out.data),
self.dsp.ceb0.eq(ack_dsp),
self.dsp.ceb1.eq(ack_dsp),
If(q[-1],
If(eop_pipe[-1],
self.dsp.c.eq(self.bias),
).Else(
self.dsp.c.eq(self.dsp.pr),
),
self.dsp.cem0.eq(ack_dsp),
self.dsp.cep0.eq(ack_dsp),

self.out.data.eq(self.dsp.pr >> scale),
self.out.stb.eq(q[-1]),
self.output.data.eq(self.dsp.pr >> scale),
self.output.stb.eq(eop_pipe[-1]),
]


Expand Down Expand Up @@ -300,34 +301,70 @@ def __init__(self, coeff, **kwargs):
self.coeff.sr[i].reset = c << logh

width = len(self.dsp.a)
inter_output = self.output
self.input = Endpoint([("data", (width, True))])
self.output = Endpoint([("data", (width, True))])

inter = Signal(reset=1) # interpolated sample pending
p_dsp = 4 # dsp pipeline depth
buf = [Signal.like(self.sample.sr[0])
for i in range(1 + p_dsp//n)]
# sequence (i: input, b: identity sample buffer `buf`, p: dsp p output,
# - buf_stb[-1], o: output, first column: cycle)

# n=2 p=4
# ib po
# 0a
# 1 a
# 2b
# 3 ba
# 4c -
# 5 cbai

# n=3 p=4
# ib po
# 0a
# 1 a
# 2
# 3b
# 4 ba
# 5 -
# 6c ai

# n=4 p=4
# ib po
# 0a
# 1 a
# 2
# 3
# 4b
# 5 ba
# 6 -
# 7 ai

inter = Signal() # interpolated sample pending
buf = [Signal.like(self.sample.sr[0]) for i in range(2)]
buf_stb = Signal(3)
self.comb += [
self.sample.load.data.eq(self.input.data),
self.sample.load.stb.eq(self.input.stb),
self.input.ack.eq(self.sample.load.ack),

# marks the end of an interpolation pair
self.output.eop.eq(~inter),
self.output.data.eq(Mux(
inter, self.out.data, buf[-1])),
self.output.stb.eq(self.out.stb | ~inter),
self.out.ack.eq(self.output.ack & inter),
self.sample.load.stb.eq(self.input.stb),
self.sample.load.data.eq(self.input.data),
If(inter, # interpolated from DSP
inter_output.connect(self.output),
self.output.eop.eq(1),
).Else( # identity from buffer
self.output.stb.eq(buf_stb[-1]),
self.output.data.eq(buf[1]),
)
]
self.sync += [
If(self.output.stb & self.output.ack,
inter.eq(~inter),
If(inter,
Cat(buf[1:]).eq(Cat(buf))
) if len(buf) > 1 else []
If(~inter,
buf_stb[-1].eq(0),
),
),
If(buf_stb[0] & ~buf_stb[-1],
buf_stb.eq(Cat(0, buf_stb)),
),
If(self.input.stb & self.input.ack,
# tap the center sample
buf[0].eq(self.sym.out.data)
If(self.sample.load.stb & self.sample.load.ack,
Cat(buf).eq(Cat(self.sym.out.data, buf)),
buf_stb[0].eq(1),
),
]
9 changes: 5 additions & 4 deletions misoc/test/test_fir.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_init(self):
dut = fir.MACFIR(n=10, scale=0)
self.assertEqual(len(dut.sample.load.data), 24)
self.assertEqual(len(dut.coeff.load.data), 18)
self.assertEqual(len(dut.out.data), 48)
self.assertEqual(len(dut.output.data), 48)

def setcoeff(self, c, h):
for i, bi in enumerate(h):
Expand All @@ -130,7 +130,7 @@ def test_run(self):
o = []
random.seed(42)
run_simulation(dut, [self.setcoeff(dut.coeff.sr, h[::-1]),
feed(dut.sample.load, x), retrieve(dut.out, o)])
feed(dut.sample.load, x), retrieve(dut.output, o)])
p = np.convolve(h, x)
self.assertEqual(o, list(p[:len(o)]))

Expand All @@ -141,7 +141,7 @@ def test_sym(self):
o = []
random.seed(42)
run_simulation(dut, [self.setcoeff(dut.coeff.sr, h[::-1]),
feed(dut.sample.load, x), retrieve(dut.out, o)])
feed(dut.sample.load, x), retrieve(dut.output, o)])
hh = np.r_[h, h[::-1]]
p = np.convolve(hh, x)
self.assertEqual(o, list(p[:len(o)]))
Expand All @@ -164,8 +164,9 @@ def test_run(self):
coeff, x = self.coeff(n)
with self.subTest(coeff=coeff):
self.filter(coeff, x)
print(n)
#with self.subTest(coeff=coeff, maxwait=True):
# self.filter(coeff, x, maxwait=3)
# self.filter(coeff, x, maxwait=1)

def coeff(self, n):
x = np.arange(3*n) + 1
Expand Down

0 comments on commit a71caf9

Please sign in to comment.