# Untitled

a guest
Jul 29th, 2020
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from nmigen import *
2. import math as math
3. import numpy as np
4.
5. class wave(Elaboratable):
6. def __init__(self, bit_width = 8, phsinc = 1):
7. self.phsinc = phsinc
8.
9. if 1:
10. # define the LUT contents
11. sine_len = 256
12. scl = (2**(bit_width-1))-1
13.
14. if 0:
15. # this always works
16. data = [0] * sine_len
17. for i in range(sine_len):
18. data[i] = math.floor(math.sin((i+0.5)*2*math.pi/(sine_len))*scl + scl + 0.5)
19. else:
20. # this fails for simulation but works for synthesis
21. data = np.zeros(sine_len, dtype=np.int)
22. for i in np.arange(sine_len):
23. data[i] = np.floor(math.sin((i+0.5)*2*math.pi/(sine_len))*scl + scl + 0.5)
24.
25. #print(data)
26.
27. # create the LUT
28. self.LUT = Memory(width = bit_width, depth = sine_len, init = data)
29.
32.
33. self.output = Signal(bit_width)
34.
35. def elaborate(self, platform):
36. m = Module()
37.
38. # phase accumulator
39. accum = Signal(28)
40. m.d.sync += accum.eq(accum + self.phsinc)
41.
42. # waveforms
43. if 0:
44. # simple sawtooth from upper bits
45. m.d.sync += self.output.eq(accum[-(self.output.width+1):-1])
46. else:
47. # LUT from upper bits
48. m.submodules.r = self.r