Ciro Santilli OurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
verilog/common.hpp
#ifndef COMMON_H
#define COMMON_H

#include <cstdint>
#include <string>

#include "verilated_vcd_c.h"

template<class Vdut>
class TestCase {
    protected:
        Vdut *dut;
        std::string vcdPath;
        uint64_t clock;
        uint64_t time;
    public:
        TestCase(std::string vcdPath) {
            this->dut = new Vdut;
            this->vcdPath = vcdPath;
            this->clock = 0;
            this->time = 0;
        }
        ~TestCase() {
            this->dut->final();
            delete this->dut;
        }
        virtual bool check() { return true; }
        virtual void step(bool& finish) = 0;
        bool run() {
            bool finish, pass;
            Verilated::traceEverOn(true);
            VerilatedVcdC *vcd = new VerilatedVcdC;
            this->dut->trace(vcd, 99);
            vcd->open(vcdPath.c_str());
            pass = true;
            do {
                this->step(finish);
                this->dut->eval();
                vcd->dump(this->time);
                pass = this->check();
                this->time++;
                this->clock = !this->clock;
                if (!pass || Verilated::gotFinish()) {
                    finish = true;
                }
            } while(!finish);
            vcd->dump(this->time);
            vcd->close();
            delete vcd;
            return pass;
        }
};

#endif