diff --git a/firmware/rtl/testbench.hpp b/firmware/rtl/testbench.hpp index 7976390..3f011c2 100644 --- a/firmware/rtl/testbench.hpp +++ b/firmware/rtl/testbench.hpp @@ -1,6 +1,6 @@ #pragma once -#include #include +#include "util.hpp" /* https://zipcpu.com/blog/2017/06/21/looking-at-verilator.html */ template class TB { @@ -14,6 +14,7 @@ template class TB { TB(int argc, char *argv[], int _bailout = 0) : mod(), bailout(_bailout), vc() { vc.commandArgs(argc, argv); vc.traceEverOn(true); + mod.clk = 0; tick_count = 0; } @@ -22,10 +23,11 @@ template class TB { mod.final(); } - virtual void run_clock() { + void run_clock() { mod.clk = !mod.clk; mod.eval(); vc.timeInc(1); + mod.clk = !mod.clk; mod.eval(); vc.timeInc(1); @@ -35,35 +37,3 @@ template class TB { exit(1); } }; - -static inline void _assert(const char *file, int line, const char *exp, bool ev, const char *fmt, ...) { - if (!ev) { - va_list va; - va_start(va, fmt); - fprintf(stderr, "%s:%d: assertion failed: %s\n", file, line, exp); - vfprintf(stderr, fmt, va); - fprintf(stderr, "\n"); - va_end(va); - exit(1); - } -} -#define STRINGIFY(s) #s -/* ,##__VA_ARGS__ is a GNU C extension */ -#define my_assert(e, fmt, ...) _assert(__FILE__, __LINE__, STRINGIFY(e), (e), fmt ,##__VA_ARGS__) - -template -static inline V sign_extend(V x, unsigned len) { - /* if high bit is 1 */ - if (x >> (len - 1) & 1) { - V mask = (1 << len) - 1; - return ~mask | x; - } else { - return x; - } -} - -#define MASK(x,v) ((x) & ((1 << (v)) - 1)) -template -static inline V mask_extend(V x, unsigned len) { - return sign_extend(MASK(x,len), len); -} diff --git a/firmware/rtl/util.hpp b/firmware/rtl/util.hpp new file mode 100644 index 0000000..6733294 --- /dev/null +++ b/firmware/rtl/util.hpp @@ -0,0 +1,35 @@ +#pragma once +#include + +static inline void _assert(const char *file, int line, const char *exp, bool ev, const char *fmt, ...) { + if (!ev) { + va_list va; + va_start(va, fmt); + fprintf(stderr, "%s:%d: assertion failed: %s\n", file, line, exp); + vfprintf(stderr, fmt, va); + fprintf(stderr, "\n"); + va_end(va); + exit(1); + } +} + +#define STRINGIFY(s) #s +/* ,##__VA_ARGS__ is a GNU C extension */ +#define my_assert(e, fmt, ...) _assert(__FILE__, __LINE__, STRINGIFY(e), (e), fmt ,##__VA_ARGS__) + +template +static inline V sign_extend(V x, unsigned len) { + /* if high bit is 1 */ + if (x >> (len - 1) & 1) { + V mask = (1 << len) - 1; + return ~mask | x; + } else { + return x; + } +} + +#define MASK(x,v) ((x) & ((1 << (v)) - 1)) +template +static inline V mask_extend(V x, unsigned len) { + return sign_extend(MASK(x,len), len); +}