| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- `timescale 1ns/1ps
- module prod2u_p4_motor_hub_axis_isolation_tb;
- localparam integer DIR_SETUP_CYCLES = 3;
- localparam integer STEP_HALF_CYCLES = 2;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg jog_start = 1'b0;
- reg [1:0] jog_axis = 2'd0;
- reg jog_dir = 1'b0;
- reg [15:0] jog_steps = 16'd1;
- wire [1:0] axis_started_pulse;
- wire [2:0] step_out;
- wire [2:0] dir_out;
- integer failures = 0;
- integer x_rises = 0;
- integer y_rises = 0;
- integer x_started = 0;
- integer y_started = 0;
- integer guard;
- always #5 clk = ~clk;
- always @(posedge step_out[0]) x_rises = x_rises + 1;
- always @(posedge step_out[1]) y_rises = y_rises + 1;
- always @(posedge clk) begin
- #1;
- if (axis_started_pulse[0]) x_started = x_started + 1;
- if (axis_started_pulse[1]) y_started = y_started + 1;
- if (step_out[2] || dir_out[2]) begin
- failures = failures + 1;
- $display("HUB_FAIL: reserved Z outputs were not fixed low");
- end
- if (axis_started_pulse[0] && !step_out[0]) begin
- failures = failures + 1;
- $display("HUB_FAIL: X started pulse did not coincide with X STEP high");
- end
- if (axis_started_pulse[1] && !step_out[1]) begin
- failures = failures + 1;
- $display("HUB_FAIL: Y started pulse did not coincide with Y STEP high");
- end
- end
- prod2u_p4_motor_hub #(
- .DIR_SETUP_CYCLES(DIR_SETUP_CYCLES),
- .STEP_HALF_CYCLES(STEP_HALF_CYCLES)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .jog_start(jog_start),
- .jog_axis(jog_axis),
- .jog_dir(jog_dir),
- .jog_steps(jog_steps),
- .axis_started_pulse(axis_started_pulse),
- .step_out(step_out),
- .dir_out(dir_out)
- );
- task issue_jog;
- input [1:0] axis;
- input direction;
- input [15:0] steps;
- begin
- @(negedge clk);
- jog_axis = axis;
- jog_dir = direction;
- jog_steps = steps;
- jog_start = 1'b1;
- @(posedge clk);
- #1;
- if ((axis == 2'd0) && (step_out[0] || (dir_out[0] !== direction))) begin
- failures = failures + 1;
- $display("HUB_FAIL: X command did not latch with STEP low");
- end
- if ((axis == 2'd1) && (step_out[1] || (dir_out[1] !== direction))) begin
- failures = failures + 1;
- $display("HUB_FAIL: Y command did not latch with STEP low");
- end
- @(negedge clk);
- jog_start = 1'b0;
- end
- endtask
- task wait_counts;
- input integer expected_x;
- input integer expected_y;
- begin
- guard = 0;
- while (((x_rises < expected_x) || (y_rises < expected_y)) &&
- (guard < 512)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if ((x_rises != expected_x) || (y_rises != expected_y)) begin
- failures = failures + 1;
- $display("HUB_FAIL: count mismatch x=%0d/%0d y=%0d/%0d",
- x_rises, expected_x, y_rises, expected_y);
- end
- end
- endtask
- initial begin
- repeat (3) @(negedge clk);
- rst_n = 1'b1;
- repeat (2) @(negedge clk);
- // X and Y are separate executors and must overlap without interference.
- issue_jog(2'd0, 1'b0, 16'd8);
- wait_counts(1, 0);
- issue_jog(2'd1, 1'b1, 16'd3);
- wait_counts(8, 3);
- repeat (8) @(negedge clk);
- if ((x_started != 1) || (y_started != 1) ||
- (x_rises != 8) || (y_rises != 3)) begin
- failures = failures + 1;
- $display("HUB_FAIL: independent overlap count/started mismatch");
- end
- // Replacing X while it is high must immediately lower only X; Y keeps
- // its own schedule and state.
- x_rises = 0;
- y_rises = 0;
- x_started = 0;
- y_started = 0;
- issue_jog(2'd0, 1'b0, 16'd9);
- issue_jog(2'd1, 1'b0, 16'd9);
- wait_counts(1, 1);
- issue_jog(2'd0, 1'b1, 16'd2);
- if (step_out[0] || !dir_out[0]) begin
- failures = failures + 1;
- $display("HUB_FAIL: same-axis overwrite did not force X STEP low");
- end
- guard = 0;
- while ((y_rises < 9) && (guard < 128)) begin
- @(negedge clk);
- guard = guard + 1;
- end
- repeat (16) @(negedge clk);
- if ((x_rises != 3) || (y_rises != 9) ||
- (x_started != 2) || (y_started != 1)) begin
- failures = failures + 1;
- $display("HUB_FAIL: overwrite/peer isolation x=%0d y=%0d sx=%0d sy=%0d",
- x_rises, y_rises, x_started, y_started);
- end
- // Reserved axis values are ignored and cannot disturb X/Y.
- issue_jog(2'd2, 1'b1, 16'd5);
- issue_jog(2'd3, 1'b1, 16'd5);
- repeat (16) @(negedge clk);
- if ((x_rises != 3) || (y_rises != 9) ||
- (x_started != 2) || (y_started != 1)) begin
- failures = failures + 1;
- $display("HUB_FAIL: reserved axis command changed X/Y motion");
- end
- if (failures != 0) begin
- $fatal(1, "prod2u_p4_motor_hub_axis_isolation_tb failures=%0d",
- failures);
- end
- $display("PASS: prod2u_p4_motor_hub_axis_isolation_tb");
- $finish;
- end
- endmodule
|