sysclock.sv 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // sysclock.sv
  3. //
  4. // Very simple unit that keeps track of time in "human" format based
  5. // on 32.768 kHz signal from the RTC. The registers have to be set from
  6. // software, presumably from reading the RTC.
  7. //
  8. // Register 0 contains the 2 s granular date and time in FAT filesystem format.
  9. // Register 1 contains two copies of a 16-bit 32 kHz counter:
  10. // the upper half contains the current counter, and the lower half is
  11. // a holding register updated when register 0 is read, and, if written
  12. // to in adcance, will write the counter when register 0 is written.
  13. module sysclock (
  14. input rst_n,
  15. input sys_clk,
  16. input rtc_clk,
  17. input valid,
  18. input addr,
  19. output reg [31:0] rdata,
  20. input [31:0] wdata,
  21. input [3:0] wstrb,
  22. output periodic
  23. );
  24. parameter PERIODIC_HZ_LG2 = 5;
  25. wire rtc_clk_sync;
  26. reg rtc_clk_q;
  27. reg rtc_clk_stb;
  28. synchronizer rtc_sync (
  29. .rst_n ( 1'b1 ),
  30. .clk ( sys_clk ),
  31. .d ( rtc_clk ),
  32. .q ( rtc_clk_sync )
  33. );
  34. always @(posedge sys_clk)
  35. begin
  36. rtc_clk_q <= rtc_clk_sync;
  37. rtc_clk_stb <= rtc_clk_sync & ~rtc_clk_q;
  38. end
  39. function logic [4:0] maxday(input [3:0] mon,
  40. input [6:0] year);
  41. case (mon)
  42. 4'd4, // April
  43. 4'd6, // June
  44. 4'd9, // September
  45. 4'd11: begin // November
  46. maxday = 5'd30;
  47. end
  48. 4'd2: begin // February
  49. if ((|year[1:0]) | (year == (2100 - 1980)))
  50. maxday = 5'd28;
  51. else
  52. maxday = 5'd29;
  53. end
  54. default: begin
  55. maxday = 5'd31;
  56. end
  57. endcase // case (mon)
  58. endfunction // mdays
  59. function logic [7:0] tick(input [7:0] me,
  60. input [7:0] start,
  61. input wrap_pre,
  62. input wrap_me);
  63. tick = wrap_me ? start : me + wrap_pre;
  64. endfunction // tick
  65. // Counter read/writes holding register
  66. reg [15:0] tm_hold;
  67. reg [ 1:0] tm_whold; // Byte enables for hold register
  68. reg [15:0] tm_tick;
  69. reg [31:0] tm_dt; // Day and time in FAT filesystem format
  70. wire [4:0] tm_2sec = tm_dt[4:0];
  71. wire [5:0] tm_min = tm_dt[10:5];
  72. wire [4:0] tm_hour = tm_dt[15:11];
  73. wire [4:0] tm_mday = tm_dt[20:16];
  74. wire [3:0] tm_mon = tm_dt[24:21];
  75. wire [6:0] tm_year = tm_dt[31:25];
  76. wire wrap_tick = rtc_clk_stb & |tm_tick;
  77. wire wrap_sec = wrap_tick & (tm_2sec >= 5'd29);
  78. wire wrap_min = wrap_sec & (tm_min >= 6'd59);
  79. wire wrap_hour = wrap_min & (tm_hour >= 5'd23);
  80. wire wrap_mday = wrap_hour & (tm_mday >= maxday(tm_mon, tm_year));
  81. wire wrap_mon = wrap_mday & (tm_mon >= 4'd12);
  82. // Yes, it may jump if the counter is written...
  83. assign periodic = tm_tick[14 - PERIODIC_HZ_LG2];
  84. always @(posedge sys_clk)
  85. begin
  86. tm_tick <= tm_tick + rtc_clk_stb;
  87. tm_dt[4:0] <= tick(tm_2sec, 5'd0, wrap_tick, wrap_sec);
  88. tm_dt[10:5] <= tick(tm_min, 6'd0, wrap_sec, wrap_min);
  89. tm_dt[15:11] <= tick(tm_hour, 4'd0, wrap_min, wrap_hour);
  90. tm_dt[20:16] <= tick(tm_mday, 5'd1, wrap_hour, wrap_mday);
  91. tm_dt[24:21] <= tick(tm_mon, 4'd1, wrap_mday, wrap_mon);
  92. tm_dt[31:25] <= tick(tm_year, 7'hxx, wrap_mon, 1'b0);
  93. if (~rst_n)
  94. begin
  95. tm_hold <= tm_tick;
  96. tm_whold <= 2'b00;
  97. end
  98. else if (valid)
  99. case (addr)
  100. 1'b0: begin
  101. // Datetime register
  102. if (wstrb[0]) tm_dt[7:0] <= wdata[7:0];
  103. if (wstrb[1]) tm_dt[15:8] <= wdata[15:8];
  104. if (wstrb[2]) tm_dt[23:16] <= wdata[23:16];
  105. if (wstrb[3]) tm_dt[31:24] <= wdata[31:24];
  106. if (tm_whold[0]) tm_tick[7:0] <= tm_hold[7:0];
  107. if (tm_whold[1]) tm_tick[15:8] <= tm_hold[15:8];
  108. tm_hold <= tm_tick;
  109. tm_whold <= 2'b00;
  110. end // case: 1'b0
  111. 1'b1: begin
  112. // Tick register
  113. if (wstrb[0]) tm_hold[7:0] <= wdata[7:0];
  114. if (wstrb[1]) tm_hold[15:8] <= wdata[15:8];
  115. if (wstrb[2]) tm_tick[7:0] <= wdata[23:16];
  116. if (wstrb[3]) tm_tick[15:8] <= wdata[31:24];
  117. tm_whold <= tm_whold | wstrb[1:0];
  118. end
  119. endcase // case (addr)
  120. end // always @ (posedge sys_clk)
  121. // Read data MUX
  122. always @(*)
  123. case (addr)
  124. 1'b0: rdata = tm_dt;
  125. 1'b1: rdata = { tm_tick, tm_hold };
  126. endcase // case (addr)
  127. endmodule // sysclock