#include "Vcounter.h"
#include "verilated.h"
#include "verilated_vcd_c.h"

#include <cstdint>

static vluint64_t sim_time = 0;

double sc_time_stamp() {
    return static_cast<double>(sim_time);
}

static void tick(Vcounter& top, VerilatedVcdC& trace) {
    top.clk = 0;
    top.eval();
    trace.dump(sim_time++);

    top.clk = 1;
    top.eval();
    trace.dump(sim_time++);
}

int main(int argc, char** argv) {
    Verilated::commandArgs(argc, argv);
    Verilated::traceEverOn(true);

    Vcounter top;
    VerilatedVcdC trace;
    top.trace(&trace, 99);
    trace.open("counter_cpp.vcd");

    top.rst = 1;
    top.en = 0;
    tick(top, trace);
    tick(top, trace);

    top.rst = 0;
    top.en = 1;
    for (uint32_t expected = 1; expected <= 5; ++expected) {
        tick(top, trace);
        if (top.count != expected) {
            trace.close();
            return 1;
        }
    }

    top.en = 0;
    const uint32_t held = top.count;
    tick(top, trace);
    tick(top, trace);

    trace.close();
    return top.count == held ? 0 : 1;
}
