| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- `timescale 1ns/1ps
- // Shared iterative half-up percent → steps planner (no DSP).
- // result = clamp(round_half_up(travel * percent / 100), 1, travel-1)
- module door_percent_planner (
- input wire clk,
- input wire rst_n,
- input wire start,
- input wire [1:0] axis_id,
- input wire [7:0] percent,
- input wire [12:0] travel,
- output reg busy,
- output reg done,
- output reg [12:0] result,
- output reg [1:0] done_axis_id,
- output reg [7:0] done_percent
- );
- reg [7:0] acc;
- reg [12:0] remaining;
- reg [12:0] accum_result;
- reg [12:0] travel_latched;
- reg [7:0] percent_latched;
- reg [1:0] axis_latched;
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- busy <= 1'b0;
- done <= 1'b0;
- result <= 13'd0;
- done_axis_id <= 2'd0;
- done_percent <= 8'd0;
- acc <= 8'd0;
- remaining <= 13'd0;
- accum_result <= 13'd0;
- travel_latched <= 13'd0;
- percent_latched <= 8'd0;
- axis_latched <= 2'd0;
- end else begin
- done <= 1'b0;
- if (start && !busy) begin
- busy <= 1'b1;
- accum_result <= 13'd0;
- acc <= 8'd50;
- remaining <= travel;
- travel_latched <= travel;
- percent_latched <= percent;
- axis_latched <= axis_id;
- // Degenerate travel: finish immediately with clamped mid (1 if travel>=2 else 0).
- if (travel == 13'd0) begin
- busy <= 1'b0;
- done <= 1'b1;
- result <= 13'd0;
- done_axis_id <= axis_id;
- done_percent <= percent;
- end
- end else if (busy) begin
- if (remaining != 13'd0) begin
- if ((acc + percent_latched) >= 8'd100) begin
- acc <= acc + percent_latched - 8'd100;
- accum_result <= accum_result + 13'd1;
- end else begin
- acc <= acc + percent_latched;
- end
- remaining <= remaining - 13'd1;
- end else begin
- // Clamp mid targets to 1 .. travel-1
- if (travel_latched <= 13'd1) begin
- result <= 13'd0;
- end else if (accum_result < 13'd1) begin
- result <= 13'd1;
- end else if (accum_result >= travel_latched) begin
- result <= travel_latched - 13'd1;
- end else begin
- result <= accum_result;
- end
- done_axis_id <= axis_latched;
- done_percent <= percent_latched;
- busy <= 1'b0;
- done <= 1'b1;
- end
- end
- end
- end
- endmodule
|