module simple_frame_contract_formal ( input wire clk, input wire rst_n, input wire [1:0] scenario ); localparam [1:0] SCENARIO_VALID = 2'd0; localparam [1:0] SCENARIO_BAD_CRC = 2'd1; localparam [1:0] SCENARIO_WRONG_LENGTH = 2'd2; localparam integer FRAME_PAYLOAD_LENGTH = 14; localparam integer BYTE_GAP_CYCLES = 9; wire dut_rst_n = rst_n; wire frame_ready = 1'b1; wire accept_enable = dut_rst_n; reg [4:0] stream_index; reg [3:0] gap_counter; reg [1:0] reset_cycles; wire [7:0] selected_length = (scenario == SCENARIO_WRONG_LENGTH) ? 8'd15 : 8'd14; wire byte_valid = dut_rst_n && (stream_index < selected_length + 8'd6) && (gap_counter == 0); reg [7:0] byte_data; reg [7:0] crc_low; reg [7:0] crc_high; wire frame_valid; wire [7:0] frame_type; wire [7:0] frame_length; wire [7:0] payload_read_addr = 8'd0; wire [7:0] payload_read_data = 8'd0; wire payload_wr_en; wire [7:0] payload_wr_addr; wire [7:0] payload_wr_data; function [7:0] payload_value; input [7:0] index_value; begin case (index_value) 8'd0: payload_value = 8'd0; 8'd1: payload_value = 8'd1; 8'd2: payload_value = 8'd2; 8'd3: payload_value = 8'd3; 8'd4: payload_value = 8'd4; 8'd5: payload_value = 8'd5; 8'd6: payload_value = 8'd6; 8'd7: payload_value = 8'd7; 8'd8: payload_value = 8'd10; 8'd9: payload_value = 8'd20; 8'd10: payload_value = 8'd30; 8'd11: payload_value = 8'd40; 8'd12: payload_value = 8'd1; default: payload_value = 8'd2; endcase end endfunction always @* begin byte_data = 8'h00; crc_low = 8'hB2; crc_high = 8'hC5; if (scenario == SCENARIO_BAD_CRC) begin crc_low = 8'h00; crc_high = 8'h00; end if (stream_index == 0) byte_data = 8'hA5; else if (stream_index == 1) byte_data = 8'h5A; else if (stream_index == 2) byte_data = 8'h01; else if (stream_index == 3) byte_data = selected_length; else if ((stream_index >= 4) && (stream_index < selected_length + 4)) byte_data = payload_value(stream_index - 4); else if (stream_index == selected_length + 4) byte_data = crc_low; else if (stream_index == selected_length + 5) byte_data = crc_high; end frame_rx #(.MAX_PAYLOAD(FRAME_PAYLOAD_LENGTH), .INTER_BYTE_TIMEOUT_CYCLES(64)) dut ( .clk(clk), .rst_n(dut_rst_n), .byte_valid(byte_valid), .byte_data(byte_data), .accept_enable(accept_enable), .frame_valid(frame_valid), .frame_ready(frame_ready), .frame_type(frame_type), .frame_length(frame_length), .payload_read_addr(payload_read_addr), .payload_read_data(payload_read_data), .payload_wr_en(payload_wr_en), .payload_wr_addr(payload_wr_addr), .payload_wr_data(payload_wr_data) ); initial begin stream_index = 5'd0; gap_counter = 4'd0; reset_cycles = 2'd0; end always @(posedge clk) begin // Formal inputs are constrained to the intended 2-state boundary. assume (rst_n == 1'b0 || rst_n == 1'b1); assume ((^scenario) == 1'b0 || (^scenario) == 1'b1); assume (scenario <= SCENARIO_WRONG_LENGTH); if (!$initstate) assume (scenario == $past(scenario)); 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 stream_index <= 5'd0; gap_counter <= 4'd0; end else if (stream_index < selected_length + 8'd6) begin if (byte_valid) begin stream_index <= stream_index + 1'b1; gap_counter <= BYTE_GAP_CYCLES - 1; end else if (gap_counter != 0) begin gap_counter <= gap_counter - 1'b1; end end if (frame_valid) begin assert (scenario == SCENARIO_VALID); assert (frame_type == 8'h01); assert (frame_length == FRAME_PAYLOAD_LENGTH); end if (payload_wr_en) assert (payload_wr_addr < FRAME_PAYLOAD_LENGTH); if (scenario == SCENARIO_BAD_CRC || scenario == SCENARIO_WRONG_LENGTH) assert (!frame_valid); end always @(posedge clk) begin // Early non-vacuity covers: BYTE_GAP=9 makes a full frame_valid cover // need ~180 cycles, which is too heavy for the low-mem cover task. cover (scenario == SCENARIO_VALID && stream_index == 4); cover (scenario == SCENARIO_BAD_CRC && stream_index == 4); cover (scenario == SCENARIO_WRONG_LENGTH && stream_index == 4); end endmodule