door_shared_dda_scheduler_a1.v 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-4): phase always wraps when active && sum>=thr;
  7. // toggle fires on wrap without allow_toggle (axis helper gates rises; high drain
  8. // must still receive pulses). service_pending sticky + overrun when busy.
  9. module door_shared_dda_scheduler #(
  10. parameter integer TRAVEL_WIDTH = 13,
  11. parameter integer PHASE_WIDTH = 26,
  12. parameter integer RATE_WIDTH = 21,
  13. parameter integer THRESH_WIDTH = 25,
  14. parameter integer CAL_PHASE_THRESHOLD = 5120
  15. ) (
  16. input wire clk,
  17. input wire rst_n,
  18. input wire service_ce,
  19. input wire [2:0] active,
  20. input wire [2:0] allow_toggle,
  21. input wire [2:0] cal_mode,
  22. input wire [RATE_WIDTH-1:0] rate0,
  23. input wire [RATE_WIDTH-1:0] rate1,
  24. input wire [RATE_WIDTH-1:0] rate2,
  25. input wire [THRESH_WIDTH-1:0] phase_threshold,
  26. output reg [2:0] toggle_pulse,
  27. output reg busy,
  28. output wire service_overrun
  29. );
  30. localparam [1:0] ST_IDLE = 2'd0,
  31. ST_X = 2'd1,
  32. ST_Y = 2'd2,
  33. ST_Z = 2'd3;
  34. reg [1:0] state;
  35. reg [PHASE_WIDTH-1:0] phase0, phase1, phase2;
  36. reg service_pending;
  37. reg overrun_r;
  38. wire _unused_allow = |allow_toggle;
  39. assign service_overrun = overrun_r;
  40. wire [PHASE_WIDTH-1:0] thr_run =
  41. {{(PHASE_WIDTH-THRESH_WIDTH){1'b0}}, phase_threshold};
  42. wire [PHASE_WIDTH-1:0] thr_cal = CAL_PHASE_THRESHOLD[PHASE_WIDTH-1:0];
  43. // Phase always advances/wraps when enabled; allow only gates fire.
  44. function automatic [PHASE_WIDTH-1:0] f_next;
  45. input [PHASE_WIDTH-1:0] ph;
  46. input [RATE_WIDTH-1:0] rate;
  47. input [PHASE_WIDTH-1:0] thr;
  48. input en;
  49. reg [PHASE_WIDTH-1:0] sum;
  50. begin
  51. if (!en) begin
  52. f_next = {PHASE_WIDTH{1'b0}};
  53. end else begin
  54. sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate};
  55. if (sum >= thr)
  56. f_next = sum - thr;
  57. else
  58. f_next = sum;
  59. end
  60. end
  61. endfunction
  62. // Fire on wrap whenever active. Axis helper gates rises via allow_rise and
  63. // always accepts a pulse to drain STEP high (!allow must not stick high).
  64. function automatic f_fire;
  65. input [PHASE_WIDTH-1:0] ph;
  66. input [RATE_WIDTH-1:0] rate;
  67. input [PHASE_WIDTH-1:0] thr;
  68. input en;
  69. reg [PHASE_WIDTH-1:0] sum;
  70. begin
  71. sum = ph + {{(PHASE_WIDTH-RATE_WIDTH){1'b0}}, rate};
  72. f_fire = en && (sum >= thr);
  73. end
  74. endfunction
  75. always @(posedge clk or negedge rst_n) begin
  76. if (!rst_n) begin
  77. state <= ST_IDLE;
  78. phase0 <= {PHASE_WIDTH{1'b0}};
  79. phase1 <= {PHASE_WIDTH{1'b0}};
  80. phase2 <= {PHASE_WIDTH{1'b0}};
  81. toggle_pulse <= 3'b000;
  82. busy <= 1'b0;
  83. service_pending <= 1'b0;
  84. overrun_r <= 1'b0;
  85. end else begin
  86. toggle_pulse <= 3'b000;
  87. // Always latch the request. Short SERVICE_CLKS TBs (e.g. 4) would
  88. // otherwise drop pulses while the 3-cycle X→Y→Z walk is busy.
  89. if (service_ce) begin
  90. if (busy)
  91. overrun_r <= 1'b1;
  92. service_pending <= 1'b1;
  93. end
  94. case (state)
  95. ST_IDLE: begin
  96. busy <= 1'b0;
  97. if (service_pending) begin
  98. service_pending <= 1'b0;
  99. busy <= 1'b1;
  100. state <= ST_X;
  101. end
  102. end
  103. ST_X: begin
  104. begin : ax0
  105. reg [PHASE_WIDTH-1:0] thr;
  106. thr = cal_mode[0] ? thr_cal : thr_run;
  107. toggle_pulse[0] <= f_fire(phase0, rate0, thr, active[0]);
  108. phase0 <= f_next(phase0, rate0, thr, active[0]);
  109. end
  110. state <= ST_Y;
  111. end
  112. ST_Y: begin
  113. begin : ax1
  114. reg [PHASE_WIDTH-1:0] thr;
  115. thr = cal_mode[1] ? thr_cal : thr_run;
  116. toggle_pulse[1] <= f_fire(phase1, rate1, thr, active[1]);
  117. phase1 <= f_next(phase1, rate1, thr, active[1]);
  118. end
  119. state <= ST_Z;
  120. end
  121. default: begin
  122. begin : ax2
  123. reg [PHASE_WIDTH-1:0] thr;
  124. thr = cal_mode[2] ? thr_cal : thr_run;
  125. toggle_pulse[2] <= f_fire(phase2, rate2, thr, active[2]);
  126. phase2 <= f_next(phase2, rate2, thr, active[2]);
  127. end
  128. if (service_pending) begin
  129. service_pending <= 1'b0;
  130. busy <= 1'b1;
  131. state <= ST_X;
  132. end else begin
  133. busy <= 1'b0;
  134. state <= ST_IDLE;
  135. end
  136. end
  137. endcase
  138. end
  139. end
  140. endmodule