ssr_time_proportion_tb.v 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. `timescale 1ns/1ps
  2. module ssr_time_proportion_tb;
  3. reg clk = 0, rst_n = 0;
  4. always #20 clk = ~clk;
  5. reg stage_we = 0, commit = 0, invalidate = 0;
  6. reg [5:0] stage_addr = 0;
  7. reg [7:0] stage_data = 0;
  8. reg [6:0] ext_phase = 0;
  9. wire [2:0] ssr_on;
  10. integer i, h0, h1, h2;
  11. reg measuring = 0;
  12. // Production 25 MHz clock and default 20 Hz PWM (1,250,000 clocks/period); no timer override.
  13. ssr_time_proportion dut (.*);
  14. always @(negedge clk) begin
  15. if (measuring) begin
  16. if (^ssr_on === 1'bx) $fatal(1,"undefined SSR outputs");
  17. h0 <= h0 + ssr_on[0];
  18. h1 <= h1 + ssr_on[1];
  19. h2 <= h2 + ssr_on[2];
  20. end
  21. end
  22. task stage;
  23. input [5:0] addr;
  24. input [7:0] value;
  25. begin
  26. @(negedge clk); stage_we = 1; stage_addr = addr; stage_data = value;
  27. @(negedge clk); stage_we = 0;
  28. end
  29. endtask
  30. task apply;
  31. begin
  32. @(negedge clk); commit = 1;
  33. @(negedge clk); commit = 0;
  34. end
  35. endtask
  36. task measure;
  37. input integer v0, v1, v2;
  38. begin
  39. @(negedge clk);
  40. h0=0; h1=0; h2=0;
  41. measuring = 1;
  42. #(1250000 * 40);
  43. measuring = 0;
  44. if(h0!=v0*12500 || h1!=v1*12500 || h2!=v2*12500)
  45. $fatal(1,"SSR duty mismatch %0d,%0d,%0d",h0,h1,h2);
  46. end
  47. endtask
  48. initial begin
  49. repeat(5) @(negedge clk); rst_n=1;
  50. measure(0,0,0);
  51. stage(32,1);stage(33,99);stage(34,100);
  52. measure(0,0,0); // staging alone has no output effect
  53. apply; measure(1,99,100);
  54. stage(32,50);stage(33,0);stage(34,20);
  55. measure(1,99,100); // partial/new inactive bank must not leak
  56. apply; measure(50,0,20);
  57. @(negedge clk); invalidate=1;
  58. @(negedge clk); invalidate=0;
  59. if(ssr_on !== 0) $fatal(1,"timeout must immediately disable all SSR");
  60. measure(0,0,0);
  61. apply; measure(50,0,20); // direct same-value recovery
  62. stage(32,100);stage(33,1);stage(34,0);
  63. apply; measure(100,1,0);
  64. $display("ssr_time_proportion_tb PASS: zero boot, staging isolation, 0/1/20/50/99/100 duty, shared phase, timeout and direct recovery");
  65. $finish;
  66. end
  67. initial begin #1000000000; $fatal(1,"SSR timeout"); end
  68. endmodule