simple_motor_temperature_contract_formal.v 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. module simple_motor_temperature_contract_formal (
  2. input wire clk,
  3. input wire rst_n,
  4. input wire start,
  5. input wire [1:0] active_mask,
  6. input wire [1:0] dir_mask,
  7. input wire tick_500us,
  8. input wire abort
  9. );
  10. // This target proves parameterized motor invariants. Production 450-step
  11. // timing is covered by tests/sim/door_motor_engine_tb.v.
  12. localparam integer FORMAL_FIXED_STEPS = 4;
  13. localparam integer FORMAL_DIR_SETUP_TICKS = 2;
  14. wire busy;
  15. wire done;
  16. wire [1:0] step;
  17. wire [1:0] dir;
  18. wire dut_rst_n = rst_n;
  19. reg [1:0] expected_active;
  20. reg [1:0] expected_dir;
  21. reg [1:0] previous_step;
  22. reg [31:0] rising_edges;
  23. reg [1:0] reset_cycles;
  24. reg start_seen;
  25. reg run_started;
  26. door_motor_engine #(
  27. .FIXED_STEPS(FORMAL_FIXED_STEPS),
  28. .DIR_SETUP_TICKS(FORMAL_DIR_SETUP_TICKS)
  29. ) motor (
  30. .clk(clk), .rst_n(dut_rst_n), .start(start), .active_mask(active_mask), .dir_mask(dir_mask),
  31. .tick_500us(tick_500us), .abort(abort), .busy(busy), .done(done), .step(step), .dir(dir)
  32. );
  33. initial begin
  34. expected_active = 2'b00;
  35. expected_dir = 2'b00;
  36. previous_step = 2'b00;
  37. rising_edges = 32'd0;
  38. reset_cycles = 2'd0;
  39. start_seen = 1'b0;
  40. run_started = 1'b0;
  41. end
  42. always @(posedge clk) begin
  43. assume (rst_n == 1'b0 || rst_n == 1'b1);
  44. assume (start == 1'b0 || start == 1'b1);
  45. assume ((^active_mask) == 1'b0 || (^active_mask) == 1'b1);
  46. assume ((^dir_mask) == 1'b0 || (^dir_mask) == 1'b1);
  47. assume (tick_500us == 1'b0 || tick_500us == 1'b1);
  48. assume (abort == 1'b0 || abort == 1'b1);
  49. assume (!start_seen || !start);
  50. assume (!busy || !start);
  51. if ($initstate || reset_cycles < 2) begin
  52. assume (!dut_rst_n);
  53. reset_cycles <= reset_cycles + 1'b1;
  54. end else begin
  55. assume (dut_rst_n);
  56. end
  57. if (!dut_rst_n) begin
  58. expected_active <= 2'b00;
  59. expected_dir <= 2'b00;
  60. previous_step <= 2'b00;
  61. rising_edges <= 32'd0;
  62. start_seen <= 1'b0;
  63. run_started <= 1'b0;
  64. end else begin
  65. if (!busy && start) begin
  66. expected_active <= active_mask;
  67. expected_dir <= dir_mask;
  68. rising_edges <= 32'd0;
  69. run_started <= (active_mask != 2'b00);
  70. end
  71. if (start) start_seen <= 1'b1;
  72. if (!busy) assert (step == 2'b00);
  73. assert ((step & ~expected_active) == 2'b00);
  74. if (busy) assert (dir == expected_dir);
  75. if (step[0]) assert (expected_active[0]);
  76. if (step[1]) assert (expected_active[1]);
  77. if (previous_step != 2'b00)
  78. assert (step == previous_step || step == 2'b00);
  79. if (step != 2'b00 && previous_step == 2'b00) begin
  80. // Every STEP rising edge is produced by a 500 us tick.
  81. assert (busy);
  82. assert ($past(tick_500us));
  83. rising_edges <= rising_edges + 1'b1;
  84. end
  85. assert (rising_edges <= FORMAL_FIXED_STEPS);
  86. if (done) begin
  87. assert (step == 2'b00);
  88. if (run_started) assert (rising_edges == FORMAL_FIXED_STEPS);
  89. run_started <= 1'b0;
  90. end
  91. if (abort) run_started <= 1'b0;
  92. if ($past(abort)) begin
  93. // Abort takes effect on the next system-clock edge.
  94. assert (!busy);
  95. assert (step == 2'b00);
  96. end
  97. previous_step <= step;
  98. end
  99. end
  100. always @(posedge clk) begin
  101. cover (dut_rst_n && start && active_mask == 2'b11);
  102. cover (dut_rst_n && busy && step == 2'b11);
  103. cover (dut_rst_n && run_started && done);
  104. cover (dut_rst_n && abort && busy);
  105. end
  106. endmodule