door_shared_dda_scheduler_a2_active_a978ff6.v 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. `timescale 1ns/1ps
  2. // Scheme A1 (RECONSTRUCTED candidate for fair full-chip TD) sequential shared DDA (not exact byte-archive).
  3. // Provenance: recovered from agent transcript 7c830c69 (Round-2 A1 implementation)
  4. // matching yosys-a1-dda.stat signals (state/service_pending/f_fire/f_next).
  5. // Label: reconstructed — must pass same functional tests as A2 before fair TD.
  6. // Drain semantics (Round-3): phase always wraps when active && sum>=thr;
  7. // allow_toggle only gates the STEP toggle pulse (high-pulse drain).
  8. module door_shared_dda_scheduler #(
  9. parameter integer TRAVEL_WIDTH = 13,
  10. parameter integer PHASE_WIDTH = 26,
  11. parameter integer RATE_WIDTH = 21,
  12. parameter integer THRESH_WIDTH = 25,
  13. parameter integer CAL_PHASE_THRESHOLD = 5120
  14. ) (
  15. input wire clk,
  16. input wire rst_n,
  17. input wire service_ce,
  18. input wire [2:0] active,
  19. input wire [2:0] allow_toggle,
  20. input wire [2:0] cal_mode,
  21. input wire [RATE_WIDTH-1:0] rate0,
  22. input wire [RATE_WIDTH-1:0] rate1,
  23. input wire [RATE_WIDTH-1:0] rate2,
  24. input wire [THRESH_WIDTH-1:0] phase_threshold,
  25. output reg [2:0] toggle_pulse,
  26. output reg busy,
  27. output wire service_overrun
  28. );
  29. localparam [1:0] ST_IDLE = 2'd0,
  30. ST_X = 2'd1,
  31. ST_Y = 2'd2,
  32. ST_Z = 2'd3;
  33. reg [1:0] state;
  34. reg [PHASE_WIDTH-1:0] phase0, phase1, phase2;
  35. reg service_pending;
  36. reg overrun_r;
  37. assign service_overrun = overrun_r;
  38. wire [PHASE_WIDTH-1:0] thr_run =
  39. {{(PHASE_WIDTH-THRESH_WIDTH){1'b0}}, phase_threshold};
  40. wire [PHASE_WIDTH-1:0] thr_cal = CAL_PHASE_THRESHOLD[PHASE_WIDTH-1:0];
  41. // Phase always advances/wraps when enabled; allow only gates fire.
  42. function automatic [PHASE_WIDTH-1:0] f_next;
  43. input [PHASE_WIDTH-1:0] ph;
  44. input [RATE_WIDTH-1:0] rate;
  45. input [PHASE_WIDTH-1:0] thr;
  46. input en;
  47. reg [PHASE_WIDTH-1:0] sum;
  48. begin
  49. if (!en) begin
  50. f_next = {PHASE_WIDTH{1'b0}};
  51. end else begin
  52. sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate};
  53. if (sum >= thr)
  54. f_next = sum - thr;
  55. else
  56. f_next = sum;
  57. end
  58. end
  59. endfunction
  60. function automatic f_fire;
  61. input [PHASE_WIDTH-1:0] ph;
  62. input [RATE_WIDTH-1:0] rate;
  63. input [PHASE_WIDTH-1:0] thr;
  64. input en;
  65. input allow;
  66. reg [PHASE_WIDTH-1:0] sum;
  67. begin
  68. sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate};
  69. f_fire = en && allow && (sum >= thr);
  70. end
  71. endfunction
  72. always @(posedge clk or negedge rst_n) begin
  73. if (!rst_n) begin
  74. state <= ST_IDLE;
  75. phase0 <= {PHASE_WIDTH{1'b0}};
  76. phase1 <= {PHASE_WIDTH{1'b0}};
  77. phase2 <= {PHASE_WIDTH{1'b0}};
  78. toggle_pulse <= 3'b000;
  79. busy <= 1'b0;
  80. service_pending <= 1'b0;
  81. overrun_r <= 1'b0;
  82. end else begin
  83. toggle_pulse <= 3'b000;
  84. if (service_ce) begin
  85. if (busy)
  86. overrun_r <= 1'b1;
  87. else
  88. service_pending <= 1'b1;
  89. end
  90. case (state)
  91. ST_IDLE: begin
  92. busy <= 1'b0;
  93. if (service_pending) begin
  94. service_pending <= 1'b0;
  95. busy <= 1'b1;
  96. state <= ST_X;
  97. end
  98. end
  99. ST_X: begin
  100. begin : ax0
  101. reg [PHASE_WIDTH-1:0] thr;
  102. thr = cal_mode[0] ? thr_cal : thr_run;
  103. toggle_pulse[0] <= f_fire(phase0, rate0, thr, active[0], allow_toggle[0]);
  104. phase0 <= f_next(phase0, rate0, thr, active[0]);
  105. end
  106. state <= ST_Y;
  107. end
  108. ST_Y: begin
  109. begin : ax1
  110. reg [PHASE_WIDTH-1:0] thr;
  111. thr = cal_mode[1] ? thr_cal : thr_run;
  112. toggle_pulse[1] <= f_fire(phase1, rate1, thr, active[1], allow_toggle[1]);
  113. phase1 <= f_next(phase1, rate1, thr, active[1]);
  114. end
  115. state <= ST_Z;
  116. end
  117. default: begin
  118. begin : ax2
  119. reg [PHASE_WIDTH-1:0] thr;
  120. thr = cal_mode[2] ? thr_cal : thr_run;
  121. toggle_pulse[2] <= f_fire(phase2, rate2, thr, active[2], allow_toggle[2]);
  122. phase2 <= f_next(phase2, rate2, thr, active[2]);
  123. end
  124. busy <= 1'b0;
  125. state <= ST_IDLE;
  126. end
  127. endcase
  128. end
  129. end
  130. endmodule