pwm_scheduler_contract_formal.v 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. module pwm_scheduler_contract_formal (
  2. input wire clk,
  3. input wire rst_n
  4. );
  5. // The proof uses small values so the scheduler invariant is reachable.
  6. // Production 100 phases and 1042 clocks are covered by simulation.
  7. localparam integer FORMAL_CHANNEL_COUNT = 3;
  8. localparam integer FORMAL_PWM_PHASE_COUNT = 4;
  9. localparam integer FORMAL_PWM_SLOT_CLKS = 2;
  10. wire [1:0] target_rd_addr;
  11. wire [2:0] logical_pwm_on;
  12. reg [7:0] duty_read_data;
  13. wire dut_rst_n = rst_n;
  14. reg [1:0] reset_cycles;
  15. reg [1:0] model_channel;
  16. reg [1:0] model_phase;
  17. reg model_slot_counter;
  18. reg [2:0] expected_pwm;
  19. reg [1:0] previous_rd_addr;
  20. reg rd_valid;
  21. function [7:0] model_ram_value;
  22. input [1:0] address;
  23. begin
  24. case (address)
  25. 2'd0: model_ram_value = 8'd0;
  26. 2'd1: model_ram_value = 8'd1;
  27. default: model_ram_value = 8'd2;
  28. endcase
  29. end
  30. endfunction
  31. // Explicit one-cycle synchronous-read model. Distinct duties make an
  32. // address/channel slip observable; a constant duty would hide it.
  33. always @(posedge clk) begin
  34. if (!dut_rst_n)
  35. duty_read_data <= 8'd0;
  36. else
  37. duty_read_data <= model_ram_value(target_rd_addr);
  38. end
  39. pwm_scheduler #(
  40. .CHANNEL_COUNT(FORMAL_CHANNEL_COUNT),
  41. .PWM_PHASE_COUNT(FORMAL_PWM_PHASE_COUNT),
  42. .PWM_SLOT_CLKS(FORMAL_PWM_SLOT_CLKS)
  43. ) dut (
  44. .clk(clk), .rst_n(dut_rst_n), .target_rd_addr(target_rd_addr),
  45. .target_rd_data(duty_read_data), .logical_pwm_on(logical_pwm_on)
  46. );
  47. // Synchronization note: product fix 72600bc removed the DUT's stale
  48. // data_channel pipeline. The former model_data_channel shadow delayed
  49. // expected_pwm by one channel and caused the old assertion to fail at
  50. // step 7; the synchronous RAM read-data contract itself remains one cycle.
  51. initial begin
  52. reset_cycles = 2'd0;
  53. model_channel = 2'd0;
  54. model_phase = 2'd0;
  55. model_slot_counter = 1'b0;
  56. expected_pwm = 3'd0;
  57. previous_rd_addr = 2'd0;
  58. rd_valid = 1'b0;
  59. end
  60. always @(posedge clk) begin
  61. assume (rst_n == 1'b0 || rst_n == 1'b1);
  62. if ($initstate || reset_cycles < 2) begin
  63. assume (!dut_rst_n);
  64. reset_cycles <= reset_cycles + 1'b1;
  65. end else begin
  66. assume (dut_rst_n);
  67. end
  68. if (!dut_rst_n) begin
  69. assert (logical_pwm_on == 3'b0);
  70. model_channel <= 2'd0;
  71. model_phase <= 2'd0;
  72. model_slot_counter <= 1'b0;
  73. expected_pwm <= 3'd0;
  74. previous_rd_addr <= 2'd0;
  75. rd_valid <= 1'b0;
  76. end else begin
  77. assert (target_rd_addr < FORMAL_CHANNEL_COUNT);
  78. assert (target_rd_addr == model_channel);
  79. assert (model_phase < FORMAL_PWM_PHASE_COUNT);
  80. assert (model_slot_counter < FORMAL_PWM_SLOT_CLKS);
  81. assert (logical_pwm_on == expected_pwm);
  82. if (rd_valid)
  83. assert (duty_read_data == model_ram_value(previous_rd_addr));
  84. previous_rd_addr <= target_rd_addr;
  85. rd_valid <= 1'b1;
  86. if (model_slot_counter == FORMAL_PWM_SLOT_CLKS - 1) begin
  87. expected_pwm[model_channel] <=
  88. (duty_read_data[6:0] > model_phase);
  89. model_slot_counter <= 1'b0;
  90. if (model_channel == FORMAL_CHANNEL_COUNT - 1) begin
  91. model_channel <= 2'd0;
  92. if (model_phase == FORMAL_PWM_PHASE_COUNT - 1)
  93. model_phase <= 2'd0;
  94. else
  95. model_phase <= model_phase + 1'b1;
  96. end else begin
  97. model_channel <= model_channel + 1'b1;
  98. end
  99. end else begin
  100. model_slot_counter <= 1'b1;
  101. end
  102. end
  103. end
  104. always @(posedge clk) begin
  105. cover (dut_rst_n && model_phase == FORMAL_PWM_PHASE_COUNT - 1);
  106. cover (dut_rst_n && rd_valid && target_rd_addr != previous_rd_addr);
  107. end
  108. endmodule