test_prod2u_p4_motion_source_mutations.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. #!/usr/bin/env python3
  2. """Kill high-risk motion regressions through real Icarus simulations."""
  3. from __future__ import annotations
  4. import subprocess
  5. import sys
  6. import tempfile
  7. import textwrap
  8. import time
  9. import unittest
  10. from dataclasses import dataclass
  11. from pathlib import Path
  12. from typing import Iterable
  13. ROOT = Path(__file__).resolve().parents[2]
  14. sys.path.insert(0, str(ROOT / "tools"))
  15. import prod2u_p4_harness as harness # noqa: E402
  16. AXIS_SOURCE = ROOT / "src/rtl/prod2u_p4_axis_executor.v"
  17. HUB_SOURCE = ROOT / "src/rtl/prod2u_p4_motor_hub.v"
  18. CORE_SOURCE = ROOT / "src/rtl/prod2u_p4_normal_v3_core.v"
  19. TOP_SOURCE = ROOT / "src/top/prod2u_p4_normal_v3_top.v"
  20. AXIS_16BIT_TB = ROOT / "tests/sim/prod2u_p4_axis_jog_16bit_tb.v"
  21. FAST_JOG_CORE_TB = ROOT / "tests/sim/prod2u_p4_fast_jog_core_tb.v"
  22. TOP_TB = ROOT / "tests/sim/prod2u_p4_normal_v3_top_tb.v"
  23. @dataclass(frozen=True)
  24. class IcarusResult:
  25. compile_returncode: int
  26. run_returncode: int | None
  27. output: str
  28. elapsed_seconds: float
  29. def _replace_exact(
  30. source: str,
  31. old: str,
  32. new: str,
  33. *,
  34. label: str,
  35. expected_count: int = 1,
  36. ) -> str:
  37. actual_count = source.count(old)
  38. if actual_count != expected_count:
  39. raise AssertionError(
  40. f"{label}: expected {expected_count} mutation sites, found {actual_count}"
  41. )
  42. return source.replace(old, new)
  43. def _run_icarus(
  44. *,
  45. iverilog: Path,
  46. vvp: Path,
  47. top: str,
  48. sources: Iterable[Path],
  49. workdir: Path,
  50. timeout_seconds: float = 120.0,
  51. defines: Iterable[str] = (),
  52. ) -> IcarusResult:
  53. executable = workdir / f"{top}.vvp"
  54. started = time.perf_counter()
  55. compile_result = subprocess.run(
  56. [
  57. str(iverilog),
  58. "-g2005-sv",
  59. *(f"-D{define}" for define in defines),
  60. "-s",
  61. top,
  62. "-o",
  63. str(executable),
  64. *(str(path) for path in sources),
  65. ],
  66. cwd=workdir,
  67. env=harness.tool_env(),
  68. capture_output=True,
  69. text=True,
  70. timeout=timeout_seconds,
  71. check=False,
  72. )
  73. output = compile_result.stdout + compile_result.stderr
  74. if compile_result.returncode != 0:
  75. return IcarusResult(
  76. compile_result.returncode,
  77. None,
  78. output,
  79. time.perf_counter() - started,
  80. )
  81. run_result = subprocess.run(
  82. [str(vvp), str(executable)],
  83. cwd=workdir,
  84. env=harness.tool_env(),
  85. capture_output=True,
  86. text=True,
  87. timeout=timeout_seconds,
  88. check=False,
  89. )
  90. return IcarusResult(
  91. compile_result.returncode,
  92. run_result.returncode,
  93. output + run_result.stdout + run_result.stderr,
  94. time.perf_counter() - started,
  95. )
  96. def _mutate_axis_with_800_step_clamp(source: str) -> str:
  97. return _replace_exact(
  98. source,
  99. " remaining_steps <= command_steps;",
  100. " remaining_steps <= (command_steps > 16'd800) ? "
  101. "16'd800 : command_steps;",
  102. label="800-step clamp",
  103. )
  104. def _mutate_axis_with_speed_tiers(source: str) -> str:
  105. source = _replace_exact(
  106. source,
  107. " input wire [15:0] command_steps,\n"
  108. " output wire step_out,",
  109. " input wire [15:0] command_steps,\n"
  110. " input wire [1:0] command_speed,\n"
  111. " output wire step_out,",
  112. label="axis speed port",
  113. )
  114. source = _replace_exact(
  115. source,
  116. " localparam integer MAX_PHASE_CYCLES =\n"
  117. " (DIR_SETUP_CYCLES > STEP_HALF_CYCLES) ?\n"
  118. " DIR_SETUP_CYCLES : STEP_HALF_CYCLES;",
  119. " localparam integer MAX_STEP_HALF_CYCLES = STEP_HALF_CYCLES * 8;\n"
  120. " localparam integer MAX_PHASE_CYCLES =\n"
  121. " (DIR_SETUP_CYCLES > MAX_STEP_HALF_CYCLES) ?\n"
  122. " DIR_SETUP_CYCLES : MAX_STEP_HALF_CYCLES;",
  123. label="axis speed counter width",
  124. )
  125. source = _replace_exact(
  126. source,
  127. " reg dir_reg;\n reg [15:0] remaining_steps;",
  128. " reg dir_reg;\n"
  129. " reg [1:0] speed_reg;\n"
  130. " reg [15:0] remaining_steps;",
  131. label="axis speed register",
  132. )
  133. source = _replace_exact(
  134. source,
  135. " assign step_out = (motion_phase == PHASE_STEP_HIGH);\n"
  136. " assign dir_out = dir_reg;",
  137. " assign step_out = (motion_phase == PHASE_STEP_HIGH);\n"
  138. " assign dir_out = dir_reg;\n"
  139. " wire [PHASE_COUNTER_BITS-1:0] speed_step_half_cycles =\n"
  140. " STEP_HALF_CYCLES << (3 - speed_reg);\n"
  141. " wire [PHASE_COUNTER_BITS-1:0] speed_step_half_last =\n"
  142. " speed_step_half_cycles - 1'b1;",
  143. label="axis speed reload",
  144. )
  145. source = _replace_exact(
  146. source,
  147. " dir_reg <= 1'b0;\n"
  148. " remaining_steps <= 16'd0;",
  149. " dir_reg <= 1'b0;\n"
  150. " speed_reg <= 2'd0;\n"
  151. " remaining_steps <= 16'd0;",
  152. label="axis speed reset",
  153. )
  154. source = _replace_exact(
  155. source,
  156. " dir_reg <= command_dir;\n"
  157. " remaining_steps <= command_steps;",
  158. " dir_reg <= command_dir;\n"
  159. " speed_reg <= command_speed;\n"
  160. " remaining_steps <= command_steps;",
  161. label="axis speed capture",
  162. )
  163. return _replace_exact(
  164. source,
  165. "phase_counter <= STEP_HALF_LAST;",
  166. "phase_counter <= speed_step_half_last;",
  167. label="axis speed phase reloads",
  168. expected_count=3,
  169. )
  170. def _mutate_core_with_request_ready_gate(source: str) -> str:
  171. return _replace_exact(
  172. source,
  173. " .jog_start(fast_jog_valid),",
  174. " .jog_start(fast_jog_valid && request_ready),",
  175. label="fast JOG request_ready gate",
  176. )
  177. def _mutate_top_with_motor_safety_gates(source: str) -> str:
  178. return _replace_exact(
  179. source,
  180. " assign x_step = motor_steps[0];\n"
  181. " assign y_step = motor_steps[1];\n"
  182. " assign z_step = motor_steps[2];\n"
  183. " assign x_dir = motor_dirs[0];\n"
  184. " assign y_dir = motor_dirs[1];\n"
  185. " assign z_dir = motor_dirs[2];\n"
  186. " assign motor_en_n = 1'b0;",
  187. " wire motor_gate_open = !disabled && !watchdog_timeout;\n"
  188. " assign x_step = motor_steps[0] && motor_gate_open && !x_limit_raw;\n"
  189. " assign y_step = motor_steps[1] && motor_gate_open && !y_limit_raw;\n"
  190. " assign z_step = motor_steps[2] && motor_gate_open;\n"
  191. " assign x_dir = motor_dirs[0];\n"
  192. " assign y_dir = motor_dirs[1];\n"
  193. " assign z_dir = motor_dirs[2];\n"
  194. " assign motor_en_n = !motor_gate_open;",
  195. label="top LS/DISABLE/watchdog/EN safety gate",
  196. )
  197. def _mutate_top_with_individual_step_gate(
  198. source: str,
  199. *,
  200. gate_expression: str,
  201. label: str,
  202. ) -> str:
  203. return _replace_exact(
  204. source,
  205. " assign x_step = motor_steps[0];",
  206. f" assign x_step = motor_steps[0] && {gate_expression};",
  207. label=label,
  208. )
  209. def _top_individual_gate_tb(
  210. *,
  211. marker: str,
  212. x_limit_raw: int,
  213. disabled: int,
  214. watchdog_timeout: int,
  215. ) -> str:
  216. return textwrap.dedent(
  217. f"""
  218. `timescale 1ns/1ps
  219. module prod2u_p4_individual_safety_gate_mutation_tb;
  220. reg clk = 1'b0;
  221. reg x_limit_raw = 1'b{x_limit_raw};
  222. reg y_limit_raw = 1'b0;
  223. wire x_step;
  224. integer x_rises = 0;
  225. integer guard = 0;
  226. always #5 clk = ~clk;
  227. always @(posedge x_step) x_rises = x_rises + 1;
  228. prod2u_p4_normal_v3_top #(
  229. .CLK_HZ(4000000),
  230. .HOST_UART_CLKS_PER_BIT(4),
  231. .POR_CYCLES(4),
  232. .INTER_BYTE_TIMEOUT_MS(2),
  233. .PTC_STEP_CLKS(1),
  234. .FAN_STEP_CLKS(1),
  235. .SOLENOID_ACTIVE_HIGH(1)
  236. ) dut (
  237. .sys_clk(clk), .host_uart_rx(1'b1), .host_uart_tx(),
  238. .ptc_pwm_out(), .fan_pwm_out(), .solenoid_out(),
  239. .motor_en_n(), .x_step(x_step), .x_dir(),
  240. .y_step(), .y_dir(), .z_step(), .z_dir(),
  241. .x_limit_raw(x_limit_raw), .y_limit_raw(y_limit_raw)
  242. );
  243. initial begin
  244. wait (dut.sys_rst_n === 1'b1);
  245. repeat (4) @(negedge clk);
  246. force dut.disabled = 1'b{disabled};
  247. force dut.watchdog_timeout = 1'b{watchdog_timeout};
  248. force dut.fast_jog_axis = 2'd0;
  249. force dut.fast_jog_dir = 1'b1;
  250. force dut.fast_jog_steps = 16'd1;
  251. force dut.fast_jog_valid = 1'b1;
  252. @(negedge clk);
  253. force dut.fast_jog_valid = 1'b0;
  254. while (x_rises == 0 && guard < 60000) begin
  255. @(negedge clk);
  256. guard = guard + 1;
  257. end
  258. if (x_rises != 1) $fatal(1, "{marker}");
  259. $display("PASS: prod2u_p4_individual_safety_gate_mutation_tb");
  260. $finish;
  261. end
  262. endmodule
  263. """
  264. ).strip() + "\n"
  265. FIXED_SPEED_TB = textwrap.dedent(
  266. r"""
  267. `timescale 1ns/1ps
  268. module prod2u_p4_fixed_speed_contract_mutation_tb;
  269. reg clk = 1'b0;
  270. reg rst_n = 1'b0;
  271. reg command_start = 1'b0;
  272. reg command_dir = 1'b0;
  273. reg [1:0] command_speed = 2'd0;
  274. reg [15:0] command_steps = 16'd2;
  275. wire step_out;
  276. integer cycle_count = 0;
  277. integer interval0;
  278. integer interval1;
  279. integer interval2;
  280. integer interval3;
  281. always #5 clk = ~clk;
  282. always @(posedge clk) cycle_count = cycle_count + 1;
  283. prod2u_p4_axis_executor #(
  284. .DIR_SETUP_CYCLES(2),
  285. .STEP_HALF_CYCLES(2)
  286. ) dut (
  287. .clk(clk), .rst_n(rst_n),
  288. .command_start(command_start), .command_dir(command_dir),
  289. .command_steps(command_steps),
  290. `ifdef MUTANT_SPEED_PORT
  291. .command_speed(command_speed),
  292. `endif
  293. .step_out(step_out), .dir_out(), .started_pulse()
  294. );
  295. task reset_axis;
  296. begin
  297. rst_n = 1'b0;
  298. command_start = 1'b0;
  299. command_dir = 1'b0;
  300. command_speed = 2'd0;
  301. command_steps = 16'd2;
  302. repeat (3) @(negedge clk);
  303. rst_n = 1'b1;
  304. repeat (2) @(negedge clk);
  305. end
  306. endtask
  307. task measure_speed;
  308. input [1:0] speed_code;
  309. output integer interval;
  310. integer first_cycle;
  311. begin
  312. reset_axis();
  313. @(negedge clk);
  314. command_speed = speed_code;
  315. command_steps = 16'd2;
  316. command_start = 1'b1;
  317. @(negedge clk);
  318. command_start = 1'b0;
  319. @(posedge step_out);
  320. first_cycle = cycle_count;
  321. @(posedge step_out);
  322. interval = cycle_count - first_cycle;
  323. if (interval <= 0) $fatal(1, "SPEED_FIXED_RATE_NO_SECOND_STEP");
  324. end
  325. endtask
  326. initial begin
  327. measure_speed(2'd0, interval0);
  328. measure_speed(2'd1, interval1);
  329. measure_speed(2'd2, interval2);
  330. measure_speed(2'd3, interval3);
  331. if ((interval0 != interval1) || (interval0 != interval2) ||
  332. (interval0 != interval3)) begin
  333. $fatal(1,
  334. "SPEED_FIXED_RATE_FAIL intervals=%0d/%0d/%0d/%0d",
  335. interval0, interval1, interval2, interval3);
  336. end
  337. $display("PASS: prod2u_p4_fixed_speed_contract_mutation_tb");
  338. $finish;
  339. end
  340. endmodule
  341. """
  342. ).strip() + "\n"
  343. class MotionSourceMutationTests(unittest.TestCase):
  344. @classmethod
  345. def setUpClass(cls) -> None:
  346. cls.iverilog = harness.resolve_tool("iverilog")
  347. cls.vvp = harness.resolve_tool("vvp")
  348. if cls.iverilog is None or cls.vvp is None:
  349. raise RuntimeError("Icarus Verilog and vvp are required for mutation tests")
  350. def assert_baseline_passed(self, result: IcarusResult, marker: str) -> None:
  351. self.assertEqual(result.compile_returncode, 0, result.output)
  352. self.assertEqual(result.run_returncode, 0, result.output)
  353. self.assertIn(marker, result.output)
  354. def assert_mutant_killed(self, result: IcarusResult, marker: str) -> None:
  355. self.assertEqual(
  356. result.compile_returncode,
  357. 0,
  358. "mutant must elaborate before it can count as killed:\n" + result.output,
  359. )
  360. self.assertIsNotNone(result.run_returncode, result.output)
  361. self.assertNotEqual(result.run_returncode, 0, result.output)
  362. self.assertIn(marker, result.output)
  363. def assert_individual_top_gate_killed(
  364. self,
  365. *,
  366. gate_expression: str,
  367. label: str,
  368. marker: str,
  369. x_limit_raw: int,
  370. disabled: int,
  371. watchdog_timeout: int,
  372. ) -> None:
  373. with tempfile.TemporaryDirectory(
  374. prefix=f"p4-mutant-{label}-", dir=ROOT.parent
  375. ) as temporary:
  376. workdir = Path(temporary)
  377. testbench = workdir / "prod2u_p4_individual_safety_gate_mutation_tb.v"
  378. testbench.write_text(
  379. _top_individual_gate_tb(
  380. marker=marker,
  381. x_limit_raw=x_limit_raw,
  382. disabled=disabled,
  383. watchdog_timeout=watchdog_timeout,
  384. ),
  385. encoding="ascii",
  386. newline="\n",
  387. )
  388. production_sources = tuple(
  389. ROOT / relative for relative in harness.active_rtl_sources()
  390. )
  391. baseline = _run_icarus(
  392. iverilog=self.iverilog,
  393. vvp=self.vvp,
  394. top="prod2u_p4_individual_safety_gate_mutation_tb",
  395. sources=(*production_sources, testbench),
  396. workdir=workdir,
  397. )
  398. self.assert_baseline_passed(
  399. baseline,
  400. "PASS: prod2u_p4_individual_safety_gate_mutation_tb",
  401. )
  402. mutant_top = workdir / f"prod2u_p4_normal_v3_top_{label}.v"
  403. mutant_top.write_text(
  404. _mutate_top_with_individual_step_gate(
  405. TOP_SOURCE.read_text(encoding="ascii"),
  406. gate_expression=gate_expression,
  407. label=label,
  408. ),
  409. encoding="ascii",
  410. newline="\n",
  411. )
  412. mutant_sources = tuple(
  413. mutant_top if source == TOP_SOURCE else source
  414. for source in production_sources
  415. )
  416. mutant = _run_icarus(
  417. iverilog=self.iverilog,
  418. vvp=self.vvp,
  419. top="prod2u_p4_individual_safety_gate_mutation_tb",
  420. sources=(*mutant_sources, testbench),
  421. workdir=workdir,
  422. )
  423. self.assert_mutant_killed(mutant, marker)
  424. def test_800_step_clamp_is_killed_by_real_16bit_boundary_tb(self) -> None:
  425. with tempfile.TemporaryDirectory(
  426. prefix="p4-mutant-clamp-", dir=ROOT.parent
  427. ) as temporary:
  428. workdir = Path(temporary)
  429. baseline = _run_icarus(
  430. iverilog=self.iverilog,
  431. vvp=self.vvp,
  432. top="prod2u_p4_axis_jog_16bit_tb",
  433. sources=(AXIS_SOURCE, AXIS_16BIT_TB),
  434. workdir=workdir,
  435. )
  436. self.assert_baseline_passed(
  437. baseline, "PASS: prod2u_p4_axis_jog_16bit_tb"
  438. )
  439. mutant_axis = workdir / "prod2u_p4_axis_executor_clamped.v"
  440. mutant_axis.write_text(
  441. _mutate_axis_with_800_step_clamp(
  442. AXIS_SOURCE.read_text(encoding="ascii")
  443. ),
  444. encoding="ascii",
  445. newline="\n",
  446. )
  447. mutant = _run_icarus(
  448. iverilog=self.iverilog,
  449. vvp=self.vvp,
  450. top="prod2u_p4_axis_jog_16bit_tb",
  451. sources=(mutant_axis, AXIS_16BIT_TB),
  452. workdir=workdir,
  453. )
  454. self.assert_mutant_killed(mutant, "16-bit JOG mismatch")
  455. def test_reintroduced_speed_tiers_are_killed_by_fixed_rate_rtl(self) -> None:
  456. with tempfile.TemporaryDirectory(
  457. prefix="p4-mutant-speed-", dir=ROOT.parent
  458. ) as temporary:
  459. workdir = Path(temporary)
  460. speed_tb = workdir / "prod2u_p4_fixed_speed_contract_mutation_tb.v"
  461. speed_tb.write_text(FIXED_SPEED_TB, encoding="ascii", newline="\n")
  462. baseline = _run_icarus(
  463. iverilog=self.iverilog,
  464. vvp=self.vvp,
  465. top="prod2u_p4_fixed_speed_contract_mutation_tb",
  466. sources=(AXIS_SOURCE, speed_tb),
  467. workdir=workdir,
  468. )
  469. self.assert_baseline_passed(
  470. baseline, "PASS: prod2u_p4_fixed_speed_contract_mutation_tb"
  471. )
  472. mutant_axis = workdir / "prod2u_p4_axis_executor_speed.v"
  473. mutant_axis.write_text(
  474. _mutate_axis_with_speed_tiers(
  475. AXIS_SOURCE.read_text(encoding="ascii")
  476. ),
  477. encoding="ascii",
  478. newline="\n",
  479. )
  480. mutant = _run_icarus(
  481. iverilog=self.iverilog,
  482. vvp=self.vvp,
  483. top="prod2u_p4_fixed_speed_contract_mutation_tb",
  484. sources=(mutant_axis, speed_tb),
  485. workdir=workdir,
  486. defines=("MUTANT_SPEED_PORT",),
  487. )
  488. self.assert_mutant_killed(mutant, "SPEED_FIXED_RATE_FAIL")
  489. def test_fast_jog_request_ready_gate_is_killed_by_core_bypass_tb(self) -> None:
  490. with tempfile.TemporaryDirectory(
  491. prefix="p4-mutant-request-ready-", dir=ROOT.parent
  492. ) as temporary:
  493. workdir = Path(temporary)
  494. sources = (
  495. ROOT / "src/rtl/binary921_crc16.v",
  496. ROOT / "src/rtl/prod2u_p4_normal_v3_output_state.v",
  497. ROOT / "src/rtl/prod2u_p4_normal_v3_watchdog.v",
  498. AXIS_SOURCE,
  499. HUB_SOURCE,
  500. ROOT / "src/rtl/prod2u_p4_response_scheduler.v",
  501. )
  502. baseline = _run_icarus(
  503. iverilog=self.iverilog,
  504. vvp=self.vvp,
  505. top="prod2u_p4_fast_jog_core_tb",
  506. sources=(*sources, CORE_SOURCE, FAST_JOG_CORE_TB),
  507. workdir=workdir,
  508. )
  509. self.assert_baseline_passed(
  510. baseline, "PASS: prod2u_p4_fast_jog_core_tb"
  511. )
  512. mutant_core = workdir / "prod2u_p4_normal_v3_core_request_ready.v"
  513. mutant_core.write_text(
  514. _mutate_core_with_request_ready_gate(
  515. CORE_SOURCE.read_text(encoding="ascii")
  516. ),
  517. encoding="ascii",
  518. newline="\n",
  519. )
  520. mutant = _run_icarus(
  521. iverilog=self.iverilog,
  522. vvp=self.vvp,
  523. top="prod2u_p4_fast_jog_core_tb",
  524. sources=(*sources, mutant_core, FAST_JOG_CORE_TB),
  525. workdir=workdir,
  526. )
  527. self.assert_mutant_killed(
  528. mutant,
  529. "fast X JOG did not finish while the old response was stalled",
  530. )
  531. def test_top_motor_safety_gates_are_killed_by_real_uart_top_tb(self) -> None:
  532. with tempfile.TemporaryDirectory(
  533. prefix="p4-mutant-top-gates-", dir=ROOT.parent
  534. ) as temporary:
  535. workdir = Path(temporary)
  536. production_sources = tuple(
  537. ROOT / relative for relative in harness.active_rtl_sources()
  538. )
  539. baseline = _run_icarus(
  540. iverilog=self.iverilog,
  541. vvp=self.vvp,
  542. top="prod2u_p4_normal_v3_top_tb",
  543. sources=(*production_sources, TOP_TB),
  544. workdir=workdir,
  545. )
  546. self.assert_baseline_passed(
  547. baseline, "PASS: prod2u_p4_normal_v3_top_tb"
  548. )
  549. mutant_top = workdir / "prod2u_p4_normal_v3_top_gated.v"
  550. mutant_top.write_text(
  551. _mutate_top_with_motor_safety_gates(
  552. TOP_SOURCE.read_text(encoding="ascii")
  553. ),
  554. encoding="ascii",
  555. newline="\n",
  556. )
  557. mutant_sources = tuple(
  558. mutant_top if source == TOP_SOURCE else source
  559. for source in production_sources
  560. )
  561. mutant = _run_icarus(
  562. iverilog=self.iverilog,
  563. vvp=self.vvp,
  564. top="prod2u_p4_normal_v3_top_tb",
  565. sources=(*mutant_sources, TOP_TB),
  566. workdir=workdir,
  567. )
  568. self.assert_mutant_killed(mutant, "NO_GATES_TOP_FAIL")
  569. def test_limit_gate_is_independently_killed(self) -> None:
  570. self.assert_individual_top_gate_killed(
  571. gate_expression="!x_limit_raw",
  572. label="limit_gate",
  573. marker="LIMIT_GATE_FAIL",
  574. x_limit_raw=1,
  575. disabled=0,
  576. watchdog_timeout=0,
  577. )
  578. def test_disable_gate_is_independently_killed(self) -> None:
  579. self.assert_individual_top_gate_killed(
  580. gate_expression="!disabled",
  581. label="disable_gate",
  582. marker="DISABLE_GATE_FAIL",
  583. x_limit_raw=0,
  584. disabled=1,
  585. watchdog_timeout=0,
  586. )
  587. def test_watchdog_gate_is_independently_killed(self) -> None:
  588. self.assert_individual_top_gate_killed(
  589. gate_expression="!watchdog_timeout",
  590. label="watchdog_gate",
  591. marker="WATCHDOG_GATE_FAIL",
  592. x_limit_raw=0,
  593. disabled=0,
  594. watchdog_timeout=1,
  595. )
  596. if __name__ == "__main__":
  597. unittest.main()