| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- `timescale 1ns/1ps
- // Alternate-config STARTUP behavior TB against REAL top.
- // Self-configures from dut.LIMIT_ACTIVE_HIGH / DIR_CLOSE_LEVEL / CLOSE_LIMIT_IS_LS.
- // Counts external STEP ball rising edges (not internal start/done alone).
- // Covers configured close already-hit, mid-search hit, found_close latch,
- // 5000 miss + 500 backoff, open-limit early stop, and per-axis independence.
- module prod2u_p4_alt_config_startup_tb;
- reg sys_clk = 1'b0;
- reg host_uart_rx = 1'b1;
- wire host_uart_tx;
- wire [7:0] ptc_pwm_out;
- wire [3:0] fan_pwm_out;
- wire motor_en_n;
- wire x_step, x_dir, y_step, y_dir, z_step, z_dir;
- reg x_limit_ls = 1'b0;
- reg x_limit_hs = 1'b0;
- reg y_limit_ls = 1'b0;
- reg y_limit_hs = 1'b0;
- reg z_limit_ls = 1'b0;
- reg z_limit_hs = 1'b0;
- tri temp_scl;
- tri temp_sda;
- localparam integer FAST_TICK_DIV = 10;
- localparam integer CLOSE_STEPS = 5000;
- localparam integer BACKOFF_STEPS = 500;
- localparam integer HOST_STEPS = 450;
- reg [31:0] fast_tick_div;
- reg fast_tick;
- integer x_steps, y_steps, z_steps;
- integer x_close_dir_steps, x_open_dir_steps;
- reg prev_x_step, prev_y_step, prev_z_step;
- reg counting;
- wire [2:0] AH = dut.LIMIT_ACTIVE_HIGH;
- wire [2:0] DCL = dut.DIR_CLOSE_LEVEL;
- wire [2:0] CLS = dut.CLOSE_LIMIT_IS_LS;
- always #1 sys_clk = ~sys_clk;
- prod2u_p4_normal_v3_top dut (
- .sys_clk(sys_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),
- .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_ls(x_limit_ls), .x_limit_hs(x_limit_hs),
- .y_limit_ls(y_limit_ls), .y_limit_hs(y_limit_hs),
- .z_limit_ls(z_limit_ls), .z_limit_hs(z_limit_hs),
- .temp_scl(temp_scl), .temp_sda(temp_sda)
- );
- initial begin
- fast_tick = 1'b0;
- fast_tick_div = 0;
- force dut.tick_500us = fast_tick;
- force dut.watchdog_timeout_event = 1'b0;
- end
- always @(posedge sys_clk) begin
- if (fast_tick_div + 1 >= FAST_TICK_DIV) begin
- fast_tick_div <= 0;
- fast_tick <= 1'b1;
- end else begin
- fast_tick_div <= fast_tick_div + 1'b1;
- fast_tick <= 1'b0;
- end
- end
- always @(posedge sys_clk) begin
- prev_x_step <= x_step;
- prev_y_step <= y_step;
- prev_z_step <= z_step;
- if (counting) begin
- if (!prev_x_step && x_step) begin
- x_steps <= x_steps + 1;
- if (x_dir === DCL[0])
- x_close_dir_steps <= x_close_dir_steps + 1;
- else if (x_dir === ~DCL[0])
- x_open_dir_steps <= x_open_dir_steps + 1;
- end
- if (!prev_y_step && y_step)
- y_steps <= y_steps + 1;
- if (!prev_z_step && z_step)
- z_steps <= z_steps + 1;
- end
- end
- function automatic phys_inactive;
- input ah_bit;
- begin
- phys_inactive = ~ah_bit;
- end
- endfunction
- function automatic phys_active;
- input ah_bit;
- begin
- phys_active = ah_bit;
- end
- endfunction
- task drive_inactive_all;
- begin
- x_limit_ls = phys_inactive(AH[0]);
- x_limit_hs = phys_inactive(AH[0]);
- y_limit_ls = phys_inactive(AH[1]);
- y_limit_hs = phys_inactive(AH[1]);
- z_limit_ls = phys_inactive(AH[2]);
- z_limit_hs = phys_inactive(AH[2]);
- end
- endtask
- task set_close_phys;
- input [1:0] axis;
- input active;
- reg level;
- begin
- level = active ? phys_active(AH[axis]) : phys_inactive(AH[axis]);
- if (CLS[axis]) begin
- if (axis == 2'd0) x_limit_ls = level;
- else if (axis == 2'd1) y_limit_ls = level;
- else z_limit_ls = level;
- end else begin
- if (axis == 2'd0) x_limit_hs = level;
- else if (axis == 2'd1) y_limit_hs = level;
- else z_limit_hs = level;
- end
- end
- endtask
- task set_open_phys;
- input [1:0] axis;
- input active;
- reg level;
- begin
- level = active ? phys_active(AH[axis]) : phys_inactive(AH[axis]);
- if (CLS[axis]) begin
- if (axis == 2'd0) x_limit_hs = level;
- else if (axis == 2'd1) y_limit_hs = level;
- else z_limit_hs = level;
- end else begin
- if (axis == 2'd0) x_limit_ls = level;
- else if (axis == 2'd1) y_limit_ls = level;
- else z_limit_ls = level;
- end
- end
- endtask
- task drain_limit_sync;
- begin
- repeat (4) @(posedge sys_clk);
- end
- endtask
- task clear_step_counters;
- begin
- x_steps = 0;
- y_steps = 0;
- z_steps = 0;
- x_close_dir_steps = 0;
- x_open_dir_steps = 0;
- prev_x_step = 1'b0;
- prev_y_step = 1'b0;
- prev_z_step = 1'b0;
- end
- endtask
- task wait_startup_done;
- integer guard;
- begin
- guard = 0;
- while (!dut.startup_done) begin
- @(posedge sys_clk);
- guard = guard + 1;
- if (guard > 20000000)
- $fatal(1, "timeout waiting startup_done");
- end
- end
- endtask
- task pulse_por;
- begin
- // Cold start: release forces that might linger; rely on simulator POR.
- counting = 1'b0;
- clear_step_counters();
- // Force async reset via internal rst_n if present after sync window.
- // Top uses power-on reset counter; wait it out with inactive limits.
- drain_limit_sync();
- end
- endtask
- // ---- Scenario helpers that re-elaborate via $finish/$stop not available;
- // instead we use force on u_startup_close_seek.rst path by re-running from
- // a single initial with staged cases that each begin after POR settles,
- // then force found_close/state only when needed. Prefer limit pin control. ----
- task force_soft_restart;
- begin
- // Restart POR + startup FSM via the POR flop (drives sys_rst_n).
- // Caller owns limit pins BEFORE calling this; pins are not cleared here.
- counting = 1'b0;
- force dut.u_por.por_reset_n = 1'b0;
- force dut.u_por.por_count = 0;
- repeat (8) @(posedge sys_clk);
- clear_step_counters();
- // Count from the first post-reset STEP edge so 5000/500 totals stay exact.
- counting = 1'b1;
- release dut.u_por.por_count;
- release dut.u_por.por_reset_n;
- while (!dut.sys_rst_n) @(posedge sys_clk);
- while (!dut.limit_sync_valid) @(posedge sys_clk);
- end
- endtask
- task start_host_motor;
- input [2:0] mask;
- input [2:0] dir_mask;
- begin
- @(negedge sys_clk);
- force dut.host_motor_active_mask = mask;
- force dut.host_motor_dir_mask = dir_mask;
- force dut.host_motor_start = 1'b1;
- @(posedge sys_clk);
- @(negedge sys_clk);
- force dut.host_motor_start = 1'b0;
- end
- endtask
- task release_host_motor;
- begin
- release dut.host_motor_start;
- release dut.host_motor_active_mask;
- release dut.host_motor_dir_mask;
- end
- endtask
- task case_already_on_close;
- begin
- $display("CASE already_on_close");
- drive_inactive_all();
- set_close_phys(2'd0, 1'b1);
- set_close_phys(2'd1, 1'b1);
- set_close_phys(2'd2, 1'b1);
- force_soft_restart();
- wait_startup_done();
- counting = 1'b0;
- if (x_steps !== 0 || y_steps !== 0 || z_steps !== 0)
- $fatal(1, "already-on-close should be 0 steps, got x=%0d y=%0d z=%0d",
- x_steps, y_steps, z_steps);
- if (motor_en_n !== 1'b1)
- $fatal(1, "EN must stay permanent_high");
- $display("PASS_SECTION_ALREADY_ON_CLOSE");
- end
- endtask
- task case_mid_search_hit_no_backoff;
- integer hit_at;
- begin
- $display("CASE mid_search_hit_no_backoff");
- drive_inactive_all();
- force_soft_restart();
- hit_at = 40;
- while (x_close_dir_steps < hit_at) @(posedge sys_clk);
- if (x_dir !== DCL[0])
- $fatal(1, "close search DIR mismatch: got %b expect %b", x_dir, DCL[0]);
- set_close_phys(2'd0, 1'b1);
- set_close_phys(2'd1, 1'b1);
- set_close_phys(2'd2, 1'b1);
- drain_limit_sync();
- wait_startup_done();
- counting = 1'b0;
- if (x_open_dir_steps !== 0)
- $fatal(1, "mid-hit must not backoff, open_dir_steps=%0d", x_open_dir_steps);
- if (x_close_dir_steps >= CLOSE_STEPS)
- $fatal(1, "mid-hit should stop before 5000, got %0d", x_close_dir_steps);
- if (x_close_dir_steps < hit_at)
- $fatal(1, "mid-hit never reached hit_at, got %0d", x_close_dir_steps);
- $display("PASS_SECTION_MID_HIT steps=%0d", x_close_dir_steps);
- end
- endtask
- task case_brief_hit_latches_found_close;
- integer hit_at;
- begin
- $display("CASE brief_hit_latches_found_close");
- drive_inactive_all();
- force_soft_restart();
- hit_at = 25;
- while (x_close_dir_steps < hit_at) @(posedge sys_clk);
- set_close_phys(2'd0, 1'b1);
- set_close_phys(2'd1, 1'b1);
- set_close_phys(2'd2, 1'b1);
- drain_limit_sync();
- // Release immediately after sync — found_close must stay latched.
- drive_inactive_all();
- drain_limit_sync();
- wait_startup_done();
- counting = 1'b0;
- if (x_open_dir_steps !== 0)
- $fatal(1, "brief hit released but backoff ran: open=%0d", x_open_dir_steps);
- if (dut.u_startup_close_seek.found_close !== 3'b111)
- $fatal(1, "found_close not latched after brief hit: %b",
- dut.u_startup_close_seek.found_close);
- $display("PASS_SECTION_BRIEF_HIT_LATCH");
- end
- endtask
- task case_full_miss_then_backoff;
- begin
- $display("CASE full_miss_then_backoff");
- drive_inactive_all();
- force_soft_restart();
- // Stay inactive through entire close search + backoff.
- wait_startup_done();
- counting = 1'b0;
- if (x_close_dir_steps !== CLOSE_STEPS)
- $fatal(1, "expected exactly 5000 close steps, got %0d", x_close_dir_steps);
- if (x_open_dir_steps !== BACKOFF_STEPS)
- $fatal(1, "expected exactly 500 open backoff steps, got %0d", x_open_dir_steps);
- $display("PASS_SECTION_FULL_MISS_BACKOFF close=%0d open=%0d",
- x_close_dir_steps, x_open_dir_steps);
- end
- endtask
- task case_backoff_open_limit_early_stop;
- integer seen_open_dir;
- begin
- $display("CASE backoff_open_limit_early_stop");
- drive_inactive_all();
- force_soft_restart();
- // Wait until close search finishes (5000) and backoff begins.
- while (x_close_dir_steps < CLOSE_STEPS) @(posedge sys_clk);
- // Wait for DIR to switch to logical open.
- seen_open_dir = 0;
- while (seen_open_dir == 0) begin
- @(posedge sys_clk);
- if (x_dir === ~DCL[0] && x_step)
- seen_open_dir = 1;
- if (dut.startup_done)
- $fatal(1, "startup finished before backoff open DIR observed");
- end
- while (x_open_dir_steps < 30) @(posedge sys_clk);
- set_open_phys(2'd0, 1'b1);
- set_open_phys(2'd1, 1'b1);
- set_open_phys(2'd2, 1'b1);
- drain_limit_sync();
- wait_startup_done();
- counting = 1'b0;
- if (x_close_dir_steps !== CLOSE_STEPS)
- $fatal(1, "close search not 5000 before early open stop: %0d", x_close_dir_steps);
- if (x_open_dir_steps >= BACKOFF_STEPS)
- $fatal(1, "open limit did not early-stop backoff: %0d", x_open_dir_steps);
- if (x_open_dir_steps < 30)
- $fatal(1, "backoff never reached sample point: %0d", x_open_dir_steps);
- $display("PASS_SECTION_BACKOFF_EARLY_OPEN open_steps=%0d", x_open_dir_steps);
- end
- endtask
- task case_axis_independent_profiles;
- begin
- $display("CASE axis_independent_profiles");
- // X: already on close (0 steps)
- // Y: mid-hit during close search
- // Z: full miss → 5000+500
- drive_inactive_all();
- set_close_phys(2'd0, 1'b1);
- force_soft_restart();
- // Y hits after some steps; Z never; X stays on close.
- while (y_steps < 35) @(posedge sys_clk);
- set_close_phys(2'd1, 1'b1);
- drain_limit_sync();
- wait_startup_done();
- counting = 1'b0;
- if (x_steps !== 0)
- $fatal(1, "X should be 0 steps (pre-hit), got %0d", x_steps);
- if (y_steps >= CLOSE_STEPS)
- $fatal(1, "Y should mid-stop, got %0d", y_steps);
- if (y_steps < 35)
- $fatal(1, "Y never reached mid-hit, got %0d", y_steps);
- if (z_steps !== (CLOSE_STEPS + BACKOFF_STEPS))
- $fatal(1, "Z should be 5000+500, got %0d", z_steps);
- $display("PASS_SECTION_AXIS_INDEPENDENT x=%0d y=%0d z=%0d",
- x_steps, y_steps, z_steps);
- end
- endtask
- task case_host_jog_after_startup;
- begin
- $display("CASE host_jog_after_startup");
- drive_inactive_all();
- set_close_phys(2'd0, 1'b1);
- set_close_phys(2'd1, 1'b1);
- set_close_phys(2'd2, 1'b1);
- force_soft_restart();
- wait_startup_done();
- drive_inactive_all();
- drain_limit_sync();
- clear_step_counters();
- counting = 1'b1;
- start_host_motor(3'b001, 3'b001);
- while (!dut.u_motor_engine.done) @(posedge sys_clk);
- release_host_motor();
- counting = 1'b0;
- if (x_steps !== HOST_STEPS)
- $fatal(1, "post-startup host jog expected 450, got %0d", x_steps);
- if (motor_en_n !== 1'b1)
- $fatal(1, "EN disturbed after startup");
- $display("PASS_SECTION_HOST_JOG_AFTER_STARTUP");
- end
- endtask
- initial begin
- counting = 1'b0;
- clear_step_counters();
- drive_inactive_all();
- $display("ALT_STARTUP_MASKS AH=%b DCL=%b CLS=%b", AH, DCL, CLS);
- if (`P4_STARTUP_CLOSE_STEPS !== CLOSE_STEPS)
- $fatal(1, "P4_STARTUP_CLOSE_STEPS drift");
- if (`P4_STARTUP_BACKOFF_STEPS !== BACKOFF_STEPS)
- $fatal(1, "P4_STARTUP_BACKOFF_STEPS drift");
- // Let initial POR complete once with inactive limits, then drive cases
- // via soft restart (force rst_n).
- counting = 1'b1;
- repeat (2000) @(posedge sys_clk);
- counting = 1'b0;
- case_already_on_close();
- case_mid_search_hit_no_backoff();
- case_brief_hit_latches_found_close();
- case_full_miss_then_backoff();
- case_backoff_open_limit_early_stop();
- case_axis_independent_profiles();
- case_host_jog_after_startup();
- $display("PASS prod2u_p4_alt_config_startup_tb AH=%b DCL=%b CLS=%b", AH, DCL, CLS);
- $finish;
- end
- endmodule
|