| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- `timescale 1ns/1ps
- module prod2u_p4_production_limit_mapping_tb;
- localparam [2:0] AXIS_CLOSED = 3'd1;
- reg clk = 1'b0;
- reg host_uart_rx = 1'b1;
- reg x_limit_raw = 1'b0;
- reg y_limit_raw = 1'b0;
- wire host_uart_tx;
- wire [23:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire solenoid_out;
- wire motor_en_n;
- wire x_step;
- wire x_dir;
- wire y_step;
- wire y_dir;
- wire z_step;
- wire z_dir;
- integer guard;
- integer x_step_rises = 0;
- integer y_step_rises = 0;
- integer x_rises_after_close;
- reg previous_x_step = 1'b0;
- reg previous_y_step = 1'b0;
- always #5 clk = ~clk;
- always @(posedge clk) begin
- if (!previous_x_step && x_step) x_step_rises <= x_step_rises + 1;
- if (!previous_y_step && y_step) y_step_rises <= y_step_rises + 1;
- previous_x_step <= x_step;
- previous_y_step <= y_step;
- end
- prod2u_p4_normal_v3_top #(
- .CLK_HZ(1000000),
- .HOST_UART_CLKS_PER_BIT(4),
- .POR_CYCLES(4),
- .INTER_BYTE_TIMEOUT_MS(2),
- .PTC_STEP_CLKS(1),
- .FAN_STEP_CLKS(1),
- .SOLENOID_ACTIVE_HIGH(1),
- .BUILD_ID(32'h1234abcd)
- ) dut (
- .sys_clk(clk),
- .host_uart_rx(host_uart_rx),
- .host_uart_tx(host_uart_tx),
- .ptc_pwm_out(ptc_pwm_out),
- .fan_pwm_out(fan_pwm_out),
- .solenoid_out(solenoid_out),
- .motor_en_n(motor_en_n),
- .x_step(x_step),
- .x_dir(x_dir),
- .y_step(y_step),
- .y_dir(y_dir),
- .z_step(z_step),
- .z_dir(z_dir),
- .x_limit_raw(x_limit_raw),
- .y_limit_raw(y_limit_raw)
- );
- task issue_jog;
- input [1:0] axis;
- input direction;
- reg [63:0] payload;
- begin
- payload = {16'd0, 16'd100, 8'd3, {7'd0, direction},
- {6'd0, axis}, 8'h35};
- guard = 0;
- while (!dut.request_ready && guard < 100000) begin
- @(posedge clk);
- guard = guard + 1;
- end
- if (guard >= 100000) begin
- $display("FAIL: core request path never became ready axis=%0d", axis);
- $fatal(1);
- end
- @(negedge clk);
- force dut.request_type = 8'h11;
- force dut.request_length = 6'd8;
- force dut.request_payload = payload;
- force dut.request_crc16 = {8'h5a, 6'd0, axis};
- force dut.request_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- release dut.request_valid;
- release dut.request_type;
- release dut.request_length;
- release dut.request_payload;
- release dut.request_crc16;
- end
- endtask
- task wait_initial_limits_low;
- begin
- guard = 0;
- while (!(dut.x_limit_ready && dut.y_limit_ready &&
- !dut.x_limit_guard && !dut.y_limit_guard &&
- !dut.x_limit_fast && !dut.y_limit_fast &&
- !dut.x_limit_active && !dut.y_limit_active) &&
- guard < 10000) begin
- @(posedge clk);
- guard = guard + 1;
- end
- if (guard >= 10000) begin
- $display("FAIL: active-high limits did not confirm raw-low inactive X=%0d/%0d/%0d/%0d Y=%0d/%0d/%0d/%0d",
- dut.x_limit_guard, dut.x_limit_fast, dut.x_limit_active,
- dut.x_limit_ready, dut.y_limit_guard, dut.y_limit_fast,
- dut.y_limit_active, dut.y_limit_ready);
- $fatal(1);
- end
- end
- endtask
- task wait_axis_step;
- input [1:0] axis;
- integer before_count;
- begin
- before_count = (axis == 0) ? x_step_rises : y_step_rises;
- guard = 0;
- while ((((axis == 0) ? x_step_rises : y_step_rises) == before_count) &&
- guard < 10000) begin
- @(posedge clk);
- guard = guard + 1;
- end
- if (guard >= 10000) begin
- $display("FAIL: axis %0d JOG emitted no STEP", axis);
- $fatal(1);
- end
- end
- endtask
- task wait_axis_closed;
- input [1:0] axis;
- begin
- guard = 0;
- while (!(((axis == 0) ? dut.x_limit_active : dut.y_limit_active) &&
- ((axis == 0) ?
- (dut.u_core.u_motors.x_axis_state == AXIS_CLOSED) :
- (dut.u_core.u_motors.y_axis_state == AXIS_CLOSED)) &&
- !dut.u_core.u_motors.axis_command_busy[axis]) &&
- guard < 50000) begin
- @(posedge clk);
- guard = guard + 1;
- end
- if (guard >= 50000) begin
- $display("FAIL: axis %0d did not close from its own active-high raw limit Xstate=%0d Ystate=%0d Xraw=%0d Yraw=%0d Xactive=%0d Yactive=%0d",
- axis, dut.u_core.u_motors.x_axis_state,
- dut.u_core.u_motors.y_axis_state, x_limit_raw, y_limit_raw,
- dut.x_limit_active, dut.y_limit_active);
- $fatal(1);
- end
- end
- endtask
- initial begin
- wait (dut.sys_rst_n === 1'b1);
- wait_initial_limits_low();
- issue_jog(2'd0, 1'b1);
- wait_axis_step(2'd0);
- if (y_step_rises != 0) begin
- $display("FAIL: Y moved during X-only JOG");
- $fatal(1);
- end
- @(negedge clk);
- x_limit_raw = 1'b1;
- wait_axis_closed(2'd0);
- if (!dut.u_core.u_motors.axis_close_dir_valid[0] ||
- dut.u_core.u_motors.axis_close_dir[0] !== 1'b1 ||
- dut.u_core.u_motors.axis_close_dir_valid[1] ||
- dut.y_limit_guard || dut.y_limit_fast || dut.y_limit_active ||
- y_step_rises != 0) begin
- $display("FAIL: X raw high affected the wrong axis");
- $fatal(1);
- end
- x_rises_after_close = x_step_rises;
- issue_jog(2'd1, 1'b0);
- wait_axis_step(2'd1);
- if (x_step_rises != x_rises_after_close) begin
- $display("FAIL: X moved during Y-only JOG");
- $fatal(1);
- end
- @(negedge clk);
- y_limit_raw = 1'b1;
- wait_axis_closed(2'd1);
- if (!dut.u_core.u_motors.axis_close_dir_valid[1] ||
- dut.u_core.u_motors.axis_close_dir[1] !== 1'b0 ||
- dut.u_core.u_motors.x_axis_state !== AXIS_CLOSED ||
- dut.u_core.u_motors.y_axis_state !== AXIS_CLOSED ||
- !dut.x_limit_guard || !dut.x_limit_fast || !dut.x_limit_active ||
- !dut.y_limit_guard || !dut.y_limit_fast || !dut.y_limit_active ||
- x_step_rises != x_rises_after_close) begin
- $display("FAIL: Y raw high affected the wrong axis or disturbed X");
- $fatal(1);
- end
- if (!motor_en_n || x_step || y_step || z_step) begin
- $display("FAIL: production limit mapping did not finish with safe motor pins");
- $fatal(1);
- end
- $display("PASS: prod2u_p4_production_limit_mapping_tb");
- $finish;
- end
- endmodule
|