Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from nmigen import *
- import math as math
- import numpy as np
- class wave(Elaboratable):
- def __init__(self, bit_width = 8, phsinc = 1):
- self.phsinc = phsinc
- if 1:
- # define the LUT contents
- sine_len = 256
- scl = (2**(bit_width-1))-1
- if 0:
- # this always works
- data = [0] * sine_len
- for i in range(sine_len):
- data[i] = math.floor(math.sin((i+0.5)*2*math.pi/(sine_len))*scl + scl + 0.5)
- else:
- # this fails for simulation but works for synthesis
- data = np.zeros(sine_len, dtype=np.int)
- #data = [0] * sine_len
- for i in np.arange(sine_len):
- data[i] = int(np.floor(np.sin((i+0.5)*2*np.pi/(sine_len))*scl + scl + 0.5))
- #print(data)
- # create the LUT
- self.LUT = Memory(width = bit_width, depth = sine_len, init = data)
- # LUT read port
- self.r = self.LUT.read_port(transparent=False)
- self.output = Signal(bit_width)
- def elaborate(self, platform):
- m = Module()
- # phase accumulator
- accum = Signal(28)
- m.d.sync += accum.eq(accum + self.phsinc)
- # waveforms
- if 0:
- # simple sawtooth from upper bits
- m.d.sync += self.output.eq(accum[-(self.output.width+1):-1])
- else:
- # LUT from upper bits
- m.submodules.r = self.r
- m.d.comb += self.r.addr.eq(accum[-(self.output.width+1):-1])
- m.d.comb += self.output.eq(self.r.data)
- return m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement