door_shared_dda_scheduler_a1.v 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. `timescale 1ns/1ps
  2. // RECONSTRUCTED Scheme A1 sequential shared DDA (pending-queue + drain parity).
  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. // Always latch the request. Short SERVICE_CLKS TBs (e.g. 4) would
  85. // otherwise drop pulses while the 3-cycle X→Y→Z walk is busy.
  86. if (service_ce) begin
  87. if (busy)
  88. overrun_r <= 1'b1;
  89. service_pending <= 1'b1;
  90. end
  91. case (state)
  92. ST_IDLE: begin
  93. busy <= 1'b0;
  94. if (service_pending) begin
  95. service_pending <= 1'b0;
  96. busy <= 1'b1;
  97. state <= ST_X;
  98. end
  99. end
  100. ST_X: begin
  101. begin : ax0
  102. reg [PHASE_WIDTH-1:0] thr;
  103. thr = cal_mode[0] ? thr_cal : thr_run;
  104. toggle_pulse[0] <= f_fire(phase0, rate0, thr, active[0], allow_toggle[0]);
  105. phase0 <= f_next(phase0, rate0, thr, active[0]);
  106. end
  107. state <= ST_Y;
  108. end
  109. ST_Y: begin
  110. begin : ax1
  111. reg [PHASE_WIDTH-1:0] thr;
  112. thr = cal_mode[1] ? thr_cal : thr_run;
  113. toggle_pulse[1] <= f_fire(phase1, rate1, thr, active[1], allow_toggle[1]);
  114. phase1 <= f_next(phase1, rate1, thr, active[1]);
  115. end
  116. state <= ST_Z;
  117. end
  118. default: begin
  119. begin : ax2
  120. reg [PHASE_WIDTH-1:0] thr;
  121. thr = cal_mode[2] ? thr_cal : thr_run;
  122. toggle_pulse[2] <= f_fire(phase2, rate2, thr, active[2], allow_toggle[2]);
  123. phase2 <= f_next(phase2, rate2, thr, active[2]);
  124. end
  125. if (service_pending) begin
  126. service_pending <= 1'b0;
  127. busy <= 1'b1;
  128. state <= ST_X;
  129. end else begin
  130. busy <= 1'b0;
  131. state <= ST_IDLE;
  132. end
  133. end
  134. endcase
  135. end
  136. end
  137. endmodule