| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- module pwm_scheduler_contract_formal (
- input wire clk,
- input wire rst_n
- );
- // The proof uses small values so the scheduler invariant is reachable.
- // Production 100 phases and 1042 clocks are covered by simulation.
- localparam integer FORMAL_CHANNEL_COUNT = 3;
- localparam integer FORMAL_PWM_PHASE_COUNT = 4;
- localparam integer FORMAL_PWM_SLOT_CLKS = 2;
- wire [1:0] target_rd_addr;
- wire [2:0] logical_pwm_on;
- reg [7:0] duty_read_data;
- wire dut_rst_n = rst_n;
- reg [1:0] reset_cycles;
- reg [1:0] model_channel;
- reg [1:0] model_phase;
- reg model_slot_counter;
- reg [2:0] expected_pwm;
- reg [1:0] previous_rd_addr;
- reg rd_valid;
- function [7:0] model_ram_value;
- input [1:0] address;
- begin
- case (address)
- 2'd0: model_ram_value = 8'd0;
- 2'd1: model_ram_value = 8'd1;
- default: model_ram_value = 8'd2;
- endcase
- end
- endfunction
- // Explicit one-cycle synchronous-read model. Distinct duties make an
- // address/channel slip observable; a constant duty would hide it.
- always @(posedge clk) begin
- if (!dut_rst_n)
- duty_read_data <= 8'd0;
- else
- duty_read_data <= model_ram_value(target_rd_addr);
- end
- pwm_scheduler #(
- .CHANNEL_COUNT(FORMAL_CHANNEL_COUNT),
- .PWM_PHASE_COUNT(FORMAL_PWM_PHASE_COUNT),
- .PWM_SLOT_CLKS(FORMAL_PWM_SLOT_CLKS)
- ) dut (
- .clk(clk), .rst_n(dut_rst_n), .target_rd_addr(target_rd_addr),
- .target_rd_data(duty_read_data), .logical_pwm_on(logical_pwm_on)
- );
- // Synchronization note: product fix 72600bc removed the DUT's stale
- // data_channel pipeline. The former model_data_channel shadow delayed
- // expected_pwm by one channel and caused the old assertion to fail at
- // step 7; the synchronous RAM read-data contract itself remains one cycle.
- initial begin
- reset_cycles = 2'd0;
- model_channel = 2'd0;
- model_phase = 2'd0;
- model_slot_counter = 1'b0;
- expected_pwm = 3'd0;
- previous_rd_addr = 2'd0;
- rd_valid = 1'b0;
- end
- always @(posedge clk) begin
- assume (rst_n == 1'b0 || rst_n == 1'b1);
- if ($initstate || reset_cycles < 2) begin
- assume (!dut_rst_n);
- reset_cycles <= reset_cycles + 1'b1;
- end else begin
- assume (dut_rst_n);
- end
- if (!dut_rst_n) begin
- assert (logical_pwm_on == 3'b0);
- model_channel <= 2'd0;
- model_phase <= 2'd0;
- model_slot_counter <= 1'b0;
- expected_pwm <= 3'd0;
- previous_rd_addr <= 2'd0;
- rd_valid <= 1'b0;
- end else begin
- assert (target_rd_addr < FORMAL_CHANNEL_COUNT);
- assert (target_rd_addr == model_channel);
- assert (model_phase < FORMAL_PWM_PHASE_COUNT);
- assert (model_slot_counter < FORMAL_PWM_SLOT_CLKS);
- assert (logical_pwm_on == expected_pwm);
- if (rd_valid)
- assert (duty_read_data == model_ram_value(previous_rd_addr));
- previous_rd_addr <= target_rd_addr;
- rd_valid <= 1'b1;
- if (model_slot_counter == FORMAL_PWM_SLOT_CLKS - 1) begin
- expected_pwm[model_channel] <=
- (duty_read_data[6:0] > model_phase);
- model_slot_counter <= 1'b0;
- if (model_channel == FORMAL_CHANNEL_COUNT - 1) begin
- model_channel <= 2'd0;
- if (model_phase == FORMAL_PWM_PHASE_COUNT - 1)
- model_phase <= 2'd0;
- else
- model_phase <= model_phase + 1'b1;
- end else begin
- model_channel <= model_channel + 1'b1;
- end
- end else begin
- model_slot_counter <= 1'b1;
- end
- end
- end
- always @(posedge clk) begin
- cover (dut_rst_n && model_phase == FORMAL_PWM_PHASE_COUNT - 1);
- cover (dut_rst_n && rd_valid && target_rd_addr != previous_rd_addr);
- end
- endmodule
|