Advertisement
Guest User

Untitled

a guest
Jul 31st, 2020
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. # cic_slice.py - CIC decimator logic. needs external rate enable
  2. # 2020-07-31 E. Brombaugh
  3.  
  4. from nmigen import *
  5. import math as math
  6. import numpy as np
  7.  
  8. class cic_slice(Elaboratable):
  9. def __init__(self, nStages = 4, gsz = 8, isz = 10):
  10. # save params
  11. self.nStages = nStages # number of stages
  12. self.gsz = gsz # bit growth per stage
  13. self.isz = isz # input word size
  14. self.asz = isz + nStages * gsz # Integrator/Adder word size
  15. self.lsz = 23 # Integrator low section size
  16. self.hsz = self.asz - self.lsz # Integrator high section size
  17. self.osz = self.asz # Output word size
  18.  
  19. # ports
  20. self.input = Signal(signed(self.isz))
  21. self.ena_out = Signal()
  22. self.output = Signal(signed(self.osz))
  23. self.valid = Signal()
  24.  
  25. def elaborate(self, platform):
  26. m = Module()
  27.  
  28. # integrators
  29. integrator = Array([Signal(signed(self.asz)) for _ in range(self.nStages)])
  30. for i in range(self.nStages):
  31. if(i==0):
  32. m.d.sync += integrator[i].eq(integrator[i] + self.input)
  33. else:
  34. m.d.sync += integrator[i].eq(integrator[i] + integrator[i-1])
  35. integrator_out = Signal(signed(self.asz))
  36. m.d.comb += integrator_out.eq(integrator[-1])
  37.  
  38. # decimation and combs
  39. comb_pipe = Signal(self.nStages+1)
  40. m.d.sync += comb_pipe.eq(Cat(self.ena_out, comb_pipe[:(self.nStages+1)]))
  41. comb_ena = Signal(self.nStages+2)
  42. m.d.comb += comb_ena.eq(Cat(self.ena_out, comb_pipe))
  43. comb_diff = Array([Signal(signed(self.osz)) for _ in range(self.nStages+1)])
  44. comb_dly = Array([Signal(signed(self.osz)) for _ in range(self.nStages+1)])
  45. for i in range(self.nStages+1):
  46. with m.If(comb_ena[i]):
  47. if(i==0):
  48. m.d.sync += comb_diff[i].eq(integrator_out>>(self.asz-self.osz))
  49. else:
  50. m.d.sync += comb_diff[i].eq(comb_diff[i-1] - comb_dly[i-1])
  51. m.d.sync += comb_dly[i].eq(comb_diff[i])
  52.  
  53. # hook up outputs
  54. m.d.sync += self.output.eq(comb_diff[-1])
  55. m.d.sync += self.valid.eq(comb_ena[-1])
  56.  
  57. return m
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement