| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- `timescale 1ns/1ps
- module prod2u_p4_motor_status_payload_tb;
- localparam integer STANDARD_STEPS = 8;
- reg clk = 1'b0;
- reg rst_n = 1'b0;
- reg force_stop = 1'b0;
- reg disabled = 1'b0;
- reg watchdog_timeout = 1'b0;
- reg x_limit_raw = 1'b0;
- reg y_limit_raw = 1'b0;
- reg snapshot_valid = 1'b0;
- reg [23:0] snapshot_targets = 24'd0;
- reg [2:0] snapshot_busy_at_accept = 3'd0;
- reg jog_valid = 1'b0;
- reg [1:0] jog_axis = 2'd0;
- reg jog_dir = 1'b0;
- reg [15:0] jog_steps = 16'd1;
- wire [2:0] axis_active_mask;
- wire [2:0] axis_busy_mask;
- wire [2:0] step_out;
- wire en_n;
- wire [79:0] status_payload;
- integer failures = 0;
- integer guard;
- always #5 clk = ~clk;
- prod2u_p4_motor_hub #(
- .STANDARD_STEPS(STANDARD_STEPS),
- .LIMIT_CONFIRM_US(8),
- .DIR_SETUP_US(3),
- .SPEED0_HALF_US(2),
- .SPEED1_HALF_US(2),
- .SPEED2_HALF_US(2),
- .SPEED3_HALF_US(2)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .tick_1us(1'b1),
- .force_stop(force_stop),
- .disabled(disabled),
- .watchdog_timeout(watchdog_timeout),
- .x_limit_raw(x_limit_raw),
- .y_limit_raw(y_limit_raw),
- .snapshot_valid(snapshot_valid),
- .snapshot_targets(snapshot_targets),
- .snapshot_busy_at_accept(snapshot_busy_at_accept),
- .snapshot_result_valid(),
- .snapshot_busy_mask(),
- .snapshot_invalid_mask(),
- .snapshot_accepted_mask(),
- .jog_valid(jog_valid),
- .jog_axis(jog_axis),
- .jog_dir(jog_dir),
- .jog_speed_code(2'd3),
- .jog_steps(jog_steps),
- .jog_result_valid(),
- .jog_accepted(),
- .axis_active_mask(axis_active_mask),
- .axis_busy_mask(axis_busy_mask),
- .step_out(step_out),
- .dir_out(),
- .en_n(en_n),
- .status_payload(status_payload)
- );
- task fail;
- input [8*96-1:0] message;
- begin
- failures = failures + 1;
- $display("MOTION_STATUS_FAIL: %0s payload=%020h", message,
- status_payload);
- end
- endtask
- task expect_status;
- input [7:0] flags;
- input [7:0] valid_mask;
- input [7:0] busy_mask;
- input [7:0] x_current;
- input [7:0] y_current;
- input [7:0] z_current;
- input [7:0] x_target;
- input [7:0] y_target;
- input [7:0] z_target;
- input [7:0] fault_mask;
- input [8*72-1:0] label_text;
- reg [79:0] expected;
- begin
- expected = {fault_mask, z_target, y_target, x_target,
- z_current, y_current, x_current,
- busy_mask, valid_mask, flags};
- #1;
- if (status_payload !== expected) begin
- $display("STATUS_MISMATCH label=%0s actual=%020h expected=%020h",
- label_text, status_payload, expected);
- fail(label_text);
- end
- end
- endtask
- task reset_hub;
- begin
- rst_n = 1'b0;
- force_stop = 1'b0;
- disabled = 1'b0;
- watchdog_timeout = 1'b0;
- x_limit_raw = 1'b0;
- y_limit_raw = 1'b0;
- snapshot_valid = 1'b0;
- snapshot_busy_at_accept = 3'd0;
- jog_valid = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- repeat (3) @(negedge clk);
- end
- endtask
- task start_x_standard;
- input [7:0] target;
- begin
- @(negedge clk);
- snapshot_targets = {8'd50, 8'd50, target};
- snapshot_busy_at_accept = 3'd0;
- snapshot_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- snapshot_valid = 1'b0;
- end
- endtask
- task start_x_jog;
- input direction;
- input [15:0] steps;
- begin
- @(negedge clk);
- jog_axis = 2'd0;
- jog_dir = direction;
- jog_steps = steps;
- jog_valid = 1'b1;
- @(posedge clk);
- #1;
- @(negedge clk);
- jog_valid = 1'b0;
- end
- endtask
- task wait_idle;
- begin
- guard = 0;
- while ((axis_active_mask != 3'b000) && guard < 2048) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (axis_active_mask != 3'b000 || axis_busy_mask != 3'b000) begin
- fail("axis did not retire");
- end
- end
- endtask
- task wait_x_step;
- begin
- guard = 0;
- while (!step_out[0] && guard < 256) begin
- @(negedge clk);
- guard = guard + 1;
- end
- if (!step_out[0]) fail("X did not emit first STEP");
- end
- endtask
- initial begin
- reset_hub();
- expect_status(8'h00, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "reset unknown status");
- start_x_standard(8'd100);
- expect_status(8'h02, 8'h00, 8'h00,
- 8'hfe, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
- "standard OPEN setup status");
- wait_x_step();
- expect_status(8'h03, 8'h00, 8'h01,
- 8'hfe, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
- "standard OPEN first-STEP busy status");
- wait_idle();
- expect_status(8'h00, 8'h01, 8'h00,
- 8'd100, 8'hff, 8'hff, 8'd100, 8'hff, 8'hff, 8'h00,
- "standard OPEN completion status");
- start_x_standard(8'd0);
- expect_status(8'h02, 8'h00, 8'h00,
- 8'hfe, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
- "standard CLOSE setup status");
- wait_x_step();
- expect_status(8'h03, 8'h00, 8'h01,
- 8'hfe, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
- "standard CLOSE first-STEP busy status");
- wait_idle();
- expect_status(8'h00, 8'h01, 8'h00,
- 8'd0, 8'hff, 8'hff, 8'd0, 8'hff, 8'hff, 8'h00,
- "standard CLOSE completion status");
- start_x_jog(1'b0, 16'd4);
- expect_status(8'h02, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "JOG setup unknown status");
- wait_x_step();
- expect_status(8'h03, 8'h00, 8'h01,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "JOG first-STEP busy unknown status");
- wait_idle();
- expect_status(8'h00, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "JOG completion unknown status");
- start_x_jog(1'b1, 16'd8);
- wait_x_step();
- x_limit_raw = 1'b1;
- wait_idle();
- expect_status(8'h00, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "limit-ignored JOG completion status");
- reset_hub();
- start_x_jog(1'b0, 16'd32);
- disabled = 1'b1;
- #1;
- if (step_out != 3'b000 || !en_n) fail("DISABLE did not gate pins");
- @(posedge clk);
- #1;
- expect_status(8'h04, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "DISABLE status");
- reset_hub();
- start_x_jog(1'b0, 16'd32);
- disabled = 1'b1;
- watchdog_timeout = 1'b1;
- #1;
- if (step_out != 3'b000 || !en_n) fail("watchdog did not gate pins");
- @(posedge clk);
- #1;
- expect_status(8'h0c, 8'h00, 8'h00,
- 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'hff, 8'h00,
- "watchdog timeout status");
- if (failures != 0) begin
- $fatal(1, "prod2u_p4_motor_status_payload_tb failures=%0d",
- failures);
- end
- $display("PASS: prod2u_p4_motor_status_payload_tb");
- $finish;
- end
- endmodule
|