From 249cf3433928cbf60e00be30b15ac6c297b559e6 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Sat, 19 Nov 2022 18:41:50 -0700 Subject: [PATCH] add bh_asserts.sv. Untested --- src/bh_assert.sv | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/bh_assert.sv diff --git a/src/bh_assert.sv b/src/bh_assert.sv new file mode 100644 index 0000000..6bdf93b --- /dev/null +++ b/src/bh_assert.sv @@ -0,0 +1,48 @@ + +int bh_assert_pass_count = 0; +int bh_assert_fail_count = 0; + +logic bh_info_enable = 0; + +function logic bh_assert_equal(int val, int expected, string description); + // display results + $timeformat(-9, 2, " ns", 20); + $display( + "%t: %s: %d %s %d - %s", + $time, + val == expected ? "PASS" : "FAIL", + val, + val == expected ? "==" : "!=", + expected, + description + ); + + // update statistics + if (val == expected) begin + bh_assert_pass_count = bh_assert_pass_count + 1; + end else begin + bh_assert_fail_count = bh_assert_fail_count + 1; + end + + return val == expected; +endfunction + +function void bh_assert_stats(); + $timeformat(-9, 2, " ns", 20); + $display("%t: DONE: "); + if (bh_assert_pass_count + bh_assert_fail_count == 0) begin + $display("ERROR: no assertions found"); + // TODO: error in a better way? + end else if (bh_assert_fail_count > 0) begin + $display("ERROR: some tests failed"); + end else begin + $display("SUCCESS: all tests passed"); + end +endfunction + +function void bh_info(string description); + if (bh_info_enable) begin + $display(description); + end +endfunction +