working assertion lib

This commit is contained in:
Brendan Haines 2022-12-01 01:03:23 -07:00
parent ebc3b22ac7
commit 38a91579ec
4 changed files with 38 additions and 85 deletions

View File

@ -4,5 +4,5 @@
],
"editor.formatOnSave": true,
"verilog.linting.linter": "iverilog",
"verilog.linting.iverilog.arguments": "-g2012 -i -Wall",
"verilog.linting.iverilog.arguments": "-g2012 -i -Wall -Y .sv -I lib",
}

View File

@ -4,16 +4,27 @@ package bh_assert;
int bh_assert_pass_count = 0;
int bh_assert_fail_count = 0;
int bh_assert_warn_count = 0;
logic bh_info_enable = 0;
localparam BH_ASSERT_LOG_LEVEL_FAIL = 0;
localparam BH_ASSERT_LOG_LEVEL_ASSERT = 1;
localparam BH_ASSERT_LOG_LEVEL_WARN = 2;
localparam BH_ASSERT_LOG_LEVEL_INFO = 3;
function logic bh_assert_equal(int val, int expected, string description);
logic bh_assert_log_level = BH_ASSERT_LOG_LEVEL_WARN; // 0 = errors only, 1 = all assertions, 2 = warnings, 3 = info
localparam COLOR_RED = "\033[31m";
localparam COLOR_YELLOW = "\033[33m";
localparam COLOR_GREEN = "\033[32m";
localparam COLOR_NORMAL = "\033[0;39m";
task 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 == expected ? {COLOR_GREEN, "PASS", COLOR_NORMAL} : {COLOR_RED, "FAIL", COLOR_NORMAL},
val,
val == expected ? "==" : "!=",
expected,
@ -26,27 +37,33 @@ function logic bh_assert_equal(int val, int expected, string description);
end else begin
bh_assert_fail_count = bh_assert_fail_count + 1;
end
endtask
return val == expected;
endfunction
function void bh_assert_stats();
task bh_assert_stats;
$timeformat(-9, 2, " ns", 20);
$display("%t: DONE: ");
$display("%t: DONE: %1d pass, %1d fail, %1d warn", $time, bh_assert_pass_count, bh_assert_fail_count, bh_assert_warn_count);
if (bh_assert_pass_count + bh_assert_fail_count == 0) begin
$display("ERROR: no assertions found");
$display("%sERROR%s: no assertions found", COLOR_YELLOW, COLOR_NORMAL);
// TODO: error in a better way?
end else if (bh_assert_fail_count > 0) begin
$display("ERROR: some tests failed");
$display("%sERROR%s: some tests failed", COLOR_RED, COLOR_NORMAL);
end else begin
$display("SUCCESS: all tests passed");
$display("%sSUCCESS%s: all tests passed", COLOR_GREEN, COLOR_NORMAL);
end
endfunction
endtask
function void bh_info(string description);
if (bh_info_enable) begin
$display(description);
task bh_info(string description);
if (bh_assert_log_level >= BH_ASSERT_LOG_LEVEL_INFO) begin
$display("%t: INFO: %s", $time, description);
end
endfunction
endtask
task bh_warn(string description);
if (bh_assert_log_level >= BH_ASSERT_LOG_LEVEL_WARN) begin
$display("%t: %sWARN%s: %s", $time, COLOR_YELLOW, COLOR_NORMAL, description);
end
bh_assert_warn_count = bh_assert_warn_count + 1;
endtask
endpackage

View File

@ -6,7 +6,7 @@ LOGS = $(TESTBENCH_V:.sv=.log)
# Hardware compilation
%.out: %.sv $(SOURCE_V)
iverilog -g2012 -o $@ $^
iverilog -g2012 -o $@ $< -y ../ -Y .sv -I ../
# Run test
%.vcd %.log: %.out

View File

@ -1,73 +1,11 @@
`include "bh_assert.sv"
`timescale 1ns/1ps
// import bh_assert::*;
import bh_assert::bh_assert_equal;
import bh_assert::bh_assert_stats;
module skidbuffer_tb();
int bh_assert_pass_count = 0;
int bh_assert_fail_count = 0;
int bh_assert_warn_count = 0;
localparam BH_ASSERT_LOG_LEVEL_FAIL = 0;
localparam BH_ASSERT_LOG_LEVEL_ASSERT = 1;
localparam BH_ASSERT_LOG_LEVEL_WARN = 2;
localparam BH_ASSERT_LOG_LEVEL_INFO = 3;
logic bh_assert_log_level = BH_ASSERT_LOG_LEVEL_WARN; // 0 = errors only, 1 = all assertions, 2 = warnings, 3 = info
localparam COLOR_RED = "\033[31m";
localparam COLOR_YELLOW = "\033[33m";
localparam COLOR_GREEN = "\033[32m";
localparam COLOR_NORMAL = "\033[0;39m";
task 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 ? {COLOR_GREEN, "PASS", COLOR_NORMAL} : {COLOR_RED, "FAIL", COLOR_NORMAL},
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
endtask
task bh_assert_stats;
$timeformat(-9, 2, " ns", 20);
$display("%t: DONE: %1d pass, %1d fail, %1d warn", $time, bh_assert_pass_count, bh_assert_fail_count, bh_assert_warn_count);
if (bh_assert_pass_count + bh_assert_fail_count == 0) begin
$display("%sERROR%s: no assertions found", COLOR_YELLOW, COLOR_NORMAL);
// TODO: error in a better way?
end else if (bh_assert_fail_count > 0) begin
$display("%sERROR%s: some tests failed", COLOR_RED, COLOR_NORMAL);
end else begin
$display("%sSUCCESS%s: all tests passed", COLOR_GREEN, COLOR_NORMAL);
end
endtask
task bh_info(string description);
if (bh_assert_log_level >= BH_ASSERT_LOG_LEVEL_INFO) begin
$display("%t: INFO: %s", $time, description);
end
endtask
task bh_warn(string description);
if (bh_assert_log_level >= BH_ASSERT_LOG_LEVEL_WARN) begin
$display("%t: %sWARN%s: %s", $time, COLOR_YELLOW, COLOR_NORMAL, description);
end
bh_assert_warn_count = bh_assert_warn_count + 1;
endtask
parameter WIDTH = 15;
parameter TEST_LIST_LENGTH = 256;
@ -134,8 +72,6 @@ always @(posedge clk) begin
if (reset == 0 && out_valid && out_ready) begin
bh_assert_equal(out, in_list[out_count], $sformatf("Output value [%3d]", out_count));
bh_warn("hello warning");
bh_info("hello info");
out_count <= out_count + 1;
end
end