2
0

rng.sv 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Simple hardware random number generator
  3. //
  4. module rng
  5. #(parameter
  6. nclocks = 1, // Asynchronous clocks
  7. width = 32 // Output bus width
  8. )
  9. (
  10. // System clock
  11. input sys_clk,
  12. // Output data
  13. output [width-1:0] q,
  14. // Randomness inputs
  15. input [nclocks-1:0] clocks, // Random input synchronized to sys_clk
  16. (* keep = 1 *) inout [2:0] rngio // Unconnected pins with pullups
  17. );
  18. wire int_clock; // Internal oscillator clock
  19. // Internal on-chip oscillator
  20. int_osc int_osc
  21. (
  22. .clkout ( int_clock ),
  23. .oscena ( 1'b1 )
  24. );
  25. // Ring oscillator using the rngio pins
  26. assign rngio[0] = ~rngio[2];
  27. assign rngio[1] = ~rngio[0];
  28. assign rngio[2] = ~rngio[1];
  29. wire [1:0] sclocks; // Internally generated clocks
  30. synchronizer #(.width(2)) synchro
  31. (
  32. .rst_n ( 1'b1 ),
  33. .clk ( sys_clk ),
  34. .d ( { rngio[0], int_clock } ),
  35. .q ( sclocks )
  36. );
  37. // LFSR randomness accumulator
  38. // See http://users.ece.cmu.edu/~koopman/crc/crc36.html for
  39. // choice of polynomial. The x^poly_width term in the polynomial
  40. // is implicit.
  41. localparam poly_width = 33;
  42. localparam [poly_width-1:0] poly = 33'h0_0000_009d;
  43. localparam lsfr_max = width > poly_width ? width-1 : poly_width-1;
  44. reg [lsfr_max:0] lsfr;
  45. always @(posedge sys_clk)
  46. lsfr <= {lsfr[lsfr_max-1:0], ^{sclocks, clocks}} ^
  47. {{(lsfr_max+1){lsfr[poly_width-1]}} & poly};
  48. assign q = lsfr[width-1:0];
  49. endmodule // rng