| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- `timescale 1ns/1ps
- module prod2u_p4_axis_executor #(
- parameter integer DIR_SETUP_CYCLES = 25000,
- parameter integer STEP_HALF_CYCLES = 12500
- ) (
- input wire clk,
- input wire rst_n,
- input wire command_start,
- input wire command_dir,
- input wire [15:0] command_steps,
- output wire step_out,
- output wire dir_out,
- output reg started_pulse
- `ifdef FORMAL
- , output wire [1:0] formal_motion_phase
- , output wire [31:0] formal_phase_counter
- , output wire [15:0] formal_remaining_steps
- `endif
- );
- localparam [1:0] PHASE_IDLE = 2'd0;
- localparam [1:0] PHASE_DIR_SETUP = 2'd1;
- localparam [1:0] PHASE_STEP_HIGH = 2'd2;
- localparam [1:0] PHASE_STEP_LOW = 2'd3;
- localparam integer MAX_PHASE_CYCLES =
- (DIR_SETUP_CYCLES > STEP_HALF_CYCLES) ?
- DIR_SETUP_CYCLES : STEP_HALF_CYCLES;
- localparam integer PHASE_COUNTER_BITS =
- (MAX_PHASE_CYCLES <= 1) ? 1 : $clog2(MAX_PHASE_CYCLES);
- localparam [31:0] DIR_SETUP_LAST_32 = DIR_SETUP_CYCLES - 1;
- localparam [31:0] STEP_HALF_LAST_32 = STEP_HALF_CYCLES - 1;
- localparam [PHASE_COUNTER_BITS-1:0] DIR_SETUP_LAST =
- DIR_SETUP_LAST_32[PHASE_COUNTER_BITS-1:0];
- localparam [PHASE_COUNTER_BITS-1:0] STEP_HALF_LAST =
- STEP_HALF_LAST_32[PHASE_COUNTER_BITS-1:0];
- (* keep = "true", syn_encoding = "sequential", fsm_encoding = "none" *)
- reg [1:0] motion_phase;
- reg dir_reg;
- reg [15:0] remaining_steps;
- reg [PHASE_COUNTER_BITS-1:0] phase_counter;
- assign step_out = (motion_phase == PHASE_STEP_HIGH);
- assign dir_out = dir_reg;
- `ifdef FORMAL
- assign formal_motion_phase = motion_phase;
- assign formal_phase_counter = {{(32-PHASE_COUNTER_BITS){1'b0}}, phase_counter};
- assign formal_remaining_steps = remaining_steps;
- `endif
- generate
- if ((DIR_SETUP_CYCLES < 1) || (STEP_HALF_CYCLES < 1)) begin : g_invalid_motion_period
- prod2u_p4_axis_motion_period_must_be_positive u_motion_period_error();
- end
- endgenerate
- always @(posedge clk or negedge rst_n) begin
- if (!rst_n) begin
- motion_phase <= PHASE_IDLE;
- dir_reg <= 1'b0;
- remaining_steps <= 16'd0;
- phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
- started_pulse <= 1'b0;
- end else begin
- started_pulse <= 1'b0;
- // A new command always wins. This intentionally lowers STEP on
- // the acceptance edge, discards the old distance, and restarts
- // the complete DIR setup interval.
- if (command_start) begin
- dir_reg <= command_dir;
- remaining_steps <= command_steps;
- motion_phase <= PHASE_DIR_SETUP;
- phase_counter <= DIR_SETUP_LAST;
- end else begin
- case (motion_phase)
- PHASE_IDLE: begin
- remaining_steps <= 16'd0;
- phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
- end
- PHASE_DIR_SETUP: begin
- if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
- phase_counter <= phase_counter - 1'b1;
- end else begin
- motion_phase <= PHASE_STEP_HIGH;
- phase_counter <= STEP_HALF_LAST;
- started_pulse <= 1'b1;
- end
- end
- PHASE_STEP_HIGH: begin
- if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
- phase_counter <= phase_counter - 1'b1;
- end else begin
- motion_phase <= PHASE_STEP_LOW;
- phase_counter <= STEP_HALF_LAST;
- end
- end
- PHASE_STEP_LOW: begin
- if (phase_counter != {PHASE_COUNTER_BITS{1'b0}}) begin
- phase_counter <= phase_counter - 1'b1;
- end else if (remaining_steps == 16'd1) begin
- motion_phase <= PHASE_IDLE;
- remaining_steps <= 16'd0;
- phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
- end else begin
- motion_phase <= PHASE_STEP_HIGH;
- remaining_steps <= remaining_steps - 16'd1;
- phase_counter <= STEP_HALF_LAST;
- end
- end
- default: begin
- motion_phase <= PHASE_IDLE;
- remaining_steps <= 16'd0;
- phase_counter <= {PHASE_COUNTER_BITS{1'b0}};
- end
- endcase
- end
- end
- end
- endmodule
|