| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- `timescale 1ns/1ps
- module prod2u_p4_axis_force_jog_v1_tb;
- localparam integer DIR_SETUP_CYCLES = 3;
- localparam integer STEP_HALF_CYCLES = 2;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg command_start = 1'b0;
- reg command_dir = 1'b0;
- reg [15:0] command_steps = 16'd1;
- wire step_out;
- wire dir_out;
- wire started_pulse;
- reg prod_command_start = 1'b0;
- reg prod_command_dir = 1'b0;
- reg [15:0] prod_command_steps = 16'd2;
- wire prod_step_out;
- wire prod_dir_out;
- wire prod_started_pulse;
- integer failures = 0;
- integer cycle_index = 0;
- integer rise_count = 0;
- integer fall_count = 0;
- integer started_count = 0;
- integer last_rise_cycle = -1;
- integer last_fall_cycle = -1;
- integer command_cycle;
- integer guard;
- reg previous_step = 1'b0;
- reg check_widths = 1'b1;
- time prod_command_time;
- time prod_first_rise_time;
- time prod_first_fall_time;
- time prod_second_rise_time;
- time prod_second_fall_time;
- always #5 clk = ~clk;
- prod2u_p4_axis_executor #(
- .DIR_SETUP_CYCLES(DIR_SETUP_CYCLES),
- .STEP_HALF_CYCLES(STEP_HALF_CYCLES)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .command_start(command_start),
- .command_dir(command_dir),
- .command_steps(command_steps),
- .step_out(step_out),
- .dir_out(dir_out),
- .started_pulse(started_pulse)
- );
- prod2u_p4_axis_executor prod_dut (
- .clk(clk),
- .rst_n(rst_n),
- .command_start(prod_command_start),
- .command_dir(prod_command_dir),
- .command_steps(prod_command_steps),
- .step_out(prod_step_out),
- .dir_out(prod_dir_out),
- .started_pulse(prod_started_pulse)
- );
- task fail;
- input [8*120-1:0] message;
- begin
- failures = failures + 1;
- $display("AXIS_FIXED_FAIL: %0s", message);
- end
- endtask
- always @(posedge clk) begin
- #1;
- cycle_index = cycle_index + 1;
- if (!rst_n) begin
- previous_step = 1'b0;
- end else begin
- if (!previous_step && step_out) begin
- rise_count = rise_count + 1;
- last_rise_cycle = cycle_index;
- if (!started_pulse && (started_count == 0)) begin
- fail("first STEP rise did not assert started_pulse");
- end
- if (check_widths && (last_fall_cycle >= 0) &&
- ((cycle_index - last_fall_cycle) != STEP_HALF_CYCLES)) begin
- fail("inter-step low phase had the wrong width");
- end
- end
- if (previous_step && !step_out) begin
- fall_count = fall_count + 1;
- if (check_widths &&
- ((cycle_index - last_rise_cycle) != STEP_HALF_CYCLES)) begin
- fail("STEP high phase had the wrong width");
- end
- last_fall_cycle = cycle_index;
- end
- if (started_pulse) begin
- started_count = started_count + 1;
- if (!step_out) begin
- fail("started_pulse asserted without STEP high");
- end
- if ((rise_count != 1) && check_widths) begin
- fail("started_pulse was not limited to the first STEP rise");
- end
- end
- end
- previous_step = step_out;
- end
- task reset_axis;
- begin
- rst_n = 1'b0;
- command_start = 1'b0;
- command_dir = 1'b0;
- command_steps = 16'd1;
- rise_count = 0;
- fall_count = 0;
- started_count = 0;
- last_rise_cycle = -1;
- last_fall_cycle = -1;
- previous_step = 1'b0;
- check_widths = 1'b1;
- repeat (3) @(negedge clk);
- rst_n = 1'b1;
- repeat (2) @(negedge clk);
- if (step_out || started_pulse) begin
- fail("reset did not leave STEP and started_pulse low");
- end
- end
- endtask
- task issue_command;
- input direction;
- input [15:0] steps;
- begin
- @(negedge clk);
- command_dir = direction;
- command_steps = steps;
- command_start = 1'b1;
- @(posedge clk);
- #1;
- command_cycle = cycle_index;
- if (step_out || started_pulse || (dir_out !== direction)) begin
- fail("command acceptance did not force STEP low and latch DIR");
- end
- @(negedge clk);
- command_start = 1'b0;
- end
- endtask
- task wait_for_rises;
- input integer expected;
- input integer maximum_cycles;
- begin
- guard = 0;
- while ((rise_count < expected) && (guard < maximum_cycles)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (rise_count != expected) begin
- fail("command did not produce the requested STEP rises");
- end
- end
- endtask
- task run_exact_count;
- input direction;
- input [15:0] steps;
- integer first_rise_cycle;
- begin
- reset_axis();
- issue_command(direction, steps);
- wait_for_rises(steps, (steps * STEP_HALF_CYCLES * 2) +
- DIR_SETUP_CYCLES + 32);
- first_rise_cycle = command_cycle + DIR_SETUP_CYCLES;
- if ((last_rise_cycle < first_rise_cycle) ||
- (started_count != 1) || (dir_out !== direction)) begin
- fail("exact-count command violated setup, started, or DIR contract");
- end
- guard = 0;
- while ((fall_count < steps) && (guard < STEP_HALF_CYCLES + 8)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- repeat (STEP_HALF_CYCLES + 2) @(negedge clk);
- if ((rise_count != steps) || (fall_count != steps) || step_out ||
- started_pulse || (started_count != 1)) begin
- fail("axis did not retire after the exact raw 16-bit step count");
- end
- end
- endtask
- initial begin
- // Values above the former 800-step cap must execute in full.
- run_exact_count(1'b0, 16'd1);
- run_exact_count(1'b1, 16'd799);
- run_exact_count(1'b0, 16'd800);
- run_exact_count(1'b1, 16'd801);
- run_exact_count(1'b0, 16'd8191);
- run_exact_count(1'b1, 16'hffff);
- // A setup-phase replacement restarts setup and suppresses the old move.
- reset_axis();
- issue_command(1'b0, 16'd7);
- @(negedge clk);
- issue_command(1'b1, 16'd2);
- wait_for_rises(2, 64);
- repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
- if ((rise_count != 2) || (fall_count != 2) ||
- (started_count != 1) || !dir_out) begin
- fail("DIR_SETUP replacement did not fully supersede the old command");
- end
- // A high-phase replacement has absolute priority: STEP goes low on the
- // acceptance edge, then the replacement waits a fresh setup interval.
- reset_axis();
- issue_command(1'b0, 16'd7);
- wait_for_rises(1, 32);
- check_widths = 1'b0;
- issue_command(1'b1, 16'd3);
- if (step_out || started_pulse || !dir_out) begin
- fail("STEP_HIGH replacement did not immediately force STEP low");
- end
- rise_count = 0;
- fall_count = 0;
- started_count = 0;
- previous_step = 1'b0;
- last_rise_cycle = -1;
- last_fall_cycle = -1;
- check_widths = 1'b1;
- wait_for_rises(3, 64);
- repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
- if ((rise_count != 3) || (fall_count != 3) ||
- (started_count != 1) || !dir_out) begin
- fail("STEP_HIGH replacement did not execute only the new command");
- end
- // A low-phase replacement also starts a fresh setup and discards tail.
- reset_axis();
- issue_command(1'b0, 16'd7);
- wait_for_rises(1, 32);
- while (step_out) @(negedge clk);
- issue_command(1'b1, 16'd2);
- rise_count = 0;
- fall_count = 0;
- started_count = 0;
- previous_step = 1'b0;
- last_rise_cycle = -1;
- last_fall_cycle = -1;
- wait_for_rises(2, 64);
- repeat (STEP_HALF_CYCLES * 2 + 4) @(negedge clk);
- if ((rise_count != 2) || (fall_count != 2) ||
- (started_count != 1) || !dir_out) begin
- fail("STEP_LOW replacement did not execute only the new command");
- end
- // Default production parameters at 25 MHz: 25,000 cycles of setup,
- // followed by 12,500 high and 12,500 low cycles per STEP.
- reset_axis();
- @(negedge clk);
- prod_command_dir = 1'b1;
- prod_command_steps = 16'd2;
- prod_command_start = 1'b1;
- @(posedge clk);
- prod_command_time = $time;
- #1;
- if (prod_step_out || prod_started_pulse || !prod_dir_out) begin
- fail("production command did not latch with STEP low");
- end
- @(negedge clk);
- prod_command_start = 1'b0;
- @(posedge prod_step_out);
- prod_first_rise_time = $time;
- #1;
- if (!prod_started_pulse) begin
- fail("production first STEP rise lacked started_pulse");
- end
- @(negedge prod_step_out);
- prod_first_fall_time = $time;
- @(posedge prod_step_out);
- prod_second_rise_time = $time;
- #1;
- if (prod_started_pulse) begin
- fail("production second STEP rise repeated started_pulse");
- end
- @(negedge prod_step_out);
- prod_second_fall_time = $time;
- if ((prod_first_rise_time - prod_command_time) != 250000ns) begin
- fail("production DIR setup was not exactly 25,000 clocks");
- end
- if ((prod_first_fall_time - prod_first_rise_time) != 125000ns) begin
- fail("production STEP high phase was not exactly 12,500 clocks");
- end
- if ((prod_second_rise_time - prod_first_fall_time) != 125000ns) begin
- fail("production STEP low phase was not exactly 12,500 clocks");
- end
- if ((prod_second_fall_time - prod_second_rise_time) != 125000ns) begin
- fail("production second STEP high phase was not exactly 12,500 clocks");
- end
- if (failures != 0) begin
- $fatal(1, "prod2u_p4_axis_force_jog_v1_tb failures=%0d", failures);
- end
- $display("PASS: prod2u_p4_axis_force_jog_v1_tb");
- $finish;
- end
- endmodule
|