<|start_filename|>sim-old/xenowing_driver.cpp<|end_filename|>
#include "../obj_dir/Vxenowing.h"
#include "verilated_vcd_c.h"

#include <cinttypes>
#include <iostream>
using namespace std;

#ifdef _WIN32
#include <Windows.h>
#else
#include <dlfcn.h>
#endif

typedef struct
{
    uint32_t (*get_reset_n)();
    void (*set_reset_n)(uint32_t value);
    uint32_t (*get_clk)();
    void (*set_clk)(uint32_t value);

    uint32_t (*get_program_rom_addr)();
    void (*set_program_rom_addr)(uint32_t value);
    uint32_t (*get_program_rom_q)();
    void (*set_program_rom_q)(uint32_t value);

    uint32_t (*get_leds)();
    void (*set_leds)(uint32_t value);

    uint32_t (*get_display_pixel_clk)();
    void (*set_display_pixel_clk)(uint32_t value);
    uint32_t (*get_display_vsync)();
    void (*set_display_vsync)(uint32_t value);
    uint32_t (*get_display_hsync)();
    void (*set_display_hsync)(uint32_t value);
    uint32_t (*get_display_data_enable)();
    void (*set_display_data_enable)(uint32_t value);
    uint32_t (*get_display_pixel_data)();
    void (*set_display_pixel_data)(uint32_t value);

    uint32_t (*get_avl_ready)();
    void (*set_avl_ready)(uint32_t value);
    uint32_t (*get_avl_burstbegin)();
    void (*set_avl_burstbegin)(uint32_t value);
    uint32_t (*get_avl_addr)();
    void (*set_avl_addr)(uint32_t value);
    uint32_t (*get_avl_rdata_valid)();
    void (*set_avl_rdata_valid)(uint32_t value);
    uint64_t (*get_avl_rdata)();
    void (*set_avl_rdata)(uint64_t value);
    uint64_t (*get_avl_wdata)();
    void (*set_avl_wdata)(uint64_t value);
    uint32_t (*get_avl_be)();
    void (*set_avl_be)(uint32_t value);
    uint32_t (*get_avl_read_req)();
    void (*set_avl_read_req)(uint32_t value);
    uint32_t (*get_avl_write_req)();
    void (*set_avl_write_req)(uint32_t value);
    uint32_t (*get_avl_size)();
    void (*set_avl_size)(uint32_t value);

    void (*eval)();
    void (*final)();
    void (*trace_dump)(uint64_t time);
} Env;

static Vxenowing *top = nullptr;
static VerilatedVcdC *trace = nullptr;

uint32_t get_reset_n()
{
    return top->reset_n;
}

void set_reset_n(uint32_t value)
{
    top->reset_n = value;
}

uint32_t get_clk()
{
    return top->clk;
}

void set_clk(uint32_t value)
{
    top->clk = value;
}

uint32_t get_program_rom_addr()
{
    return top->program_rom_addr;
}

void set_program_rom_addr(uint32_t value)
{
    top->program_rom_addr = value;
}

uint32_t get_program_rom_q()
{
    return top->program_rom_q;
}

void set_program_rom_q(uint32_t value)
{
    top->program_rom_q = value;
}

uint32_t get_leds()
{
    return top->leds;
}

void set_leds(uint32_t value)
{
    top->leds = value;
}

uint32_t get_display_pixel_clk()
{
    return top->display_pixel_clk;
}

void set_display_pixel_clk(uint32_t value)
{
    top->display_pixel_clk = value;
}

uint32_t get_display_vsync()
{
    return top->display_vsync;
}

void set_display_vsync(uint32_t value)
{
    top->display_vsync = value;
}

uint32_t get_display_hsync()
{
    return top->display_hsync;
}

void set_display_hsync(uint32_t value)
{
    top->display_hsync = value;
}

uint32_t get_display_data_enable()
{
    return top->display_data_enable;
}

void set_display_data_enable(uint32_t value)
{
    top->display_data_enable = value;
}

uint32_t get_display_pixel_data()
{
    return top->display_pixel_data;
}

void set_display_pixel_data(uint32_t value)
{
    top->display_pixel_data = value;
}

uint32_t get_avl_ready()
{
    return top->avl_ready;
}

void set_avl_ready(uint32_t value)
{
    top->avl_ready = value;
}

uint32_t get_avl_burstbegin()
{
    return top->avl_burstbegin;
}

void set_avl_burstbegin(uint32_t value)
{
    top->avl_burstbegin = value;
}

uint32_t get_avl_addr()
{
    return top->avl_addr;
}

void set_avl_addr(uint32_t value)
{
    top->avl_addr = value;
}

uint32_t get_avl_rdata_valid()
{
    return top->avl_rdata_valid;
}

void set_avl_rdata_valid(uint32_t value)
{
    top->avl_rdata_valid = value;
}

uint64_t get_avl_rdata()
{
    return top->avl_rdata;
}

void set_avl_rdata(uint64_t value)
{
    top->avl_rdata = value;
}

uint64_t get_avl_wdata()
{
    return top->avl_wdata;
}

void set_avl_wdata(uint64_t value)
{
    top->avl_wdata = value;
}

uint32_t get_avl_be()
{
    return top->avl_be;
}

void set_avl_be(uint32_t value)
{
    top->avl_be = value;
}

uint32_t get_avl_read_req()
{
    return top->avl_read_req;
}

void set_avl_read_req(uint32_t value)
{
    top->avl_read_req = value;
}

uint32_t get_avl_write_req()
{
    return top->avl_write_req;
}

void set_avl_write_req(uint32_t value)
{
    top->avl_write_req = value;
}

uint32_t get_avl_size()
{
    return top->avl_size;
}

void set_avl_size(uint32_t value)
{
    top->avl_size = value;
}

void eval()
{
    top->eval();
}

void final()
{
    top->final();
}

void trace_dump(uint64_t time)
{
    if (trace)
        trace->dump((vluint64_t)time);
}

int main(int argc, char **argv)
{
    if (argc != 2 && argc != 3)
    {
        cout << "Invalid number of arguments" << endl;
        exit(1);
    }

    auto libName = argv[1];
    auto lib =
#ifdef _WIN32
        LoadLibrary(libName);
#else
        dlopen(libName, RTLD_LAZY);
#endif
    if (!lib)
    {
        cout << "Failed to load library: " << libName << endl;
        exit(1);
    }
    auto run = (int32_t (*)(Env *))
#ifdef _WIN32
        GetProcAddress
#else
        dlsym
#endif
        (lib, "run");
    if (!run)
    {
        cout << "Failed to get run proc address" << endl;
        exit(1);
    }

    top = new Vxenowing();

    if (argc == 3)
    {
        Verilated::traceEverOn(true);
        trace = new VerilatedVcdC();
        top->trace(trace, 99); // TODO: What is this param?
        trace->open(argv[2]);
    }

    Env env =
    {
        .get_reset_n = get_reset_n,
        .set_reset_n = set_reset_n,
        .get_clk = get_clk,
        .set_clk = set_clk,

        .get_program_rom_addr = get_program_rom_addr,
        .set_program_rom_addr = set_program_rom_addr,
        .get_program_rom_q = get_program_rom_q,
        .set_program_rom_q = set_program_rom_q,

        .get_leds = get_leds,
        .set_leds = set_leds,

        .get_display_pixel_clk = get_display_pixel_clk,
        .set_display_pixel_clk = set_display_pixel_clk,
        .get_display_vsync = get_display_vsync,
        .set_display_vsync = set_display_vsync,
        .get_display_hsync = get_display_hsync,
        .set_display_hsync = set_display_hsync,
        .get_display_data_enable = get_display_data_enable,
        .set_display_data_enable = set_display_data_enable,
        .get_display_pixel_data = get_display_pixel_data,
        .set_display_pixel_data = set_display_pixel_data,

        .get_avl_ready = get_avl_ready,
        .set_avl_ready = set_avl_ready,
        .get_avl_burstbegin = get_avl_burstbegin,
        .set_avl_burstbegin = set_avl_burstbegin,
        .get_avl_addr = get_avl_addr,
        .set_avl_addr = set_avl_addr,
        .get_avl_rdata_valid = get_avl_rdata_valid,
        .set_avl_rdata_valid = set_avl_rdata_valid,
        .get_avl_rdata = get_avl_rdata,
        .set_avl_rdata = set_avl_rdata,
        .get_avl_wdata = get_avl_wdata,
        .set_avl_wdata = set_avl_wdata,
        .get_avl_be = get_avl_be,
        .set_avl_be = set_avl_be,
        .get_avl_read_req = get_avl_read_req,
        .set_avl_read_req = set_avl_read_req,
        .get_avl_write_req = get_avl_write_req,
        .set_avl_write_req = set_avl_write_req,
        .get_avl_size = get_avl_size,
        .set_avl_size = set_avl_size,

        .eval = eval,
        .final = final,
        .trace_dump = trace_dump,
    };

    auto ret = run(&env);

    if (trace)
    {
        trace->close();
        delete trace;
    }

    delete top;

    return ret;
}


<|start_filename|>Makefile<|end_filename|>
# TODO: Consider using cargo-binutils or similar
TARGET_PREFIX=riscv64-unknown-elf-

RM=rm
RM_FLAGS=-rf

.PHONY: all
all: boot-rom program rtl generated-rtl sim

.PHONY: clean
clean: boot-rom-clean program-clean rtl-clean generated-rtl-clean sim-clean test-clean

# Boot ROM

BOOT_ROM_RUST_DIR=sw/boot-rom
BOOT_ROM_TARGET_DIR=$(BOOT_ROM_RUST_DIR)/target
BOOT_ROM_BIN=$(BOOT_ROM_TARGET_DIR)/boot-rom.bin
BOOT_ROM_ELF=$(BOOT_ROM_TARGET_DIR)/riscv32i-unknown-none-elf/release/boot-rom

.PHONY: boot-rom
boot-rom: $(BOOT_ROM_BIN) boot-rom-rust

.PHONY: boot-rom-clean
boot-rom-clean: boot-rom-rust-clean
	$(RM) $(RM_FLAGS) $(BOOT_ROM_BIN)

$(BOOT_ROM_BIN): boot-rom-rust
	$(TARGET_PREFIX)objcopy -O binary $(BOOT_ROM_ELF) $@

.PHONY: boot-rom-rust
boot-rom-rust:
	cd $(BOOT_ROM_RUST_DIR) && cargo build --release

.PHONY: boot-rom-rust-clean
boot-rom-rust-clean:
	cd $(BOOT_ROM_RUST_DIR) && cargo clean

# Program

PROGRAM_RUST_DIR=sw/program
PROGRAM_TARGET_DIR=$(PROGRAM_RUST_DIR)/target
PROGRAM_BIN=$(PROGRAM_TARGET_DIR)/program.bin
PROGRAM_ELF=$(PROGRAM_TARGET_DIR)/riscv32i-unknown-none-elf/release/program

.PHONY: program
program: $(PROGRAM_BIN) program-rust

.PHONY: program-clean
program-clean: program-rust-clean
	$(RM) $(RM_FLAGS) $(PROGRAM_BIN)

$(PROGRAM_BIN): program-rust
	$(TARGET_PREFIX)objcopy -O binary $(PROGRAM_ELF) $@

.PHONY: program-rust
program-rust:
	cd $(PROGRAM_RUST_DIR) && cargo build --release

.PHONY: program-rust-clean
program-rust-clean:
	cd $(PROGRAM_RUST_DIR) && cargo clean

# RTL

RTL_DIR=rtl

.PHONY: rtl
rtl: boot-rom
	cd $(RTL_DIR) && cargo build --release

.PHONY: rtl-clean
rtl-clean:
	cd $(RTL_DIR) && cargo clean

# Generated RTL

GENERATED_RTL_NAME=_generated.v
GENERATED_RTL=$(RTL_DIR)/$(GENERATED_RTL_NAME)

.PHONY: generated-rtl
generated-rtl: $(GENERATED_RTL)

$(GENERATED_RTL): rtl
	cd $(RTL_DIR) && cargo run --release > $(GENERATED_RTL_NAME)

.PHONY: generated-rtl-clean
generated-rtl-clean:
	$(RM) $(RM_FLAGS) $(GENERATED_RTL)

# Sim

SIM_DIR=sim
APPROX_RECIPROCAL_DIR=$(SIM_DIR)/approx-reciprocal
BUSTER_DIR=$(SIM_DIR)/buster
FIFO_DIR=$(SIM_DIR)/fifo
FLOW_CONTROLLED_PIPE_DIR=$(SIM_DIR)/flow-controlled-pipe
MARV_DIR=$(SIM_DIR)/marv
PEEK_BUFFER_DIR=$(SIM_DIR)/peek-buffer
READ_CACHE_DIR=$(SIM_DIR)/read-cache

.PHONY: sim
sim: approx-reciprocal buster fifo flow-controlled-pipe marv peek-buffer read-cache

.PHONY: approx-reciprocal
approx-reciprocal:
	cd $(APPROX_RECIPROCAL_DIR) && cargo build --release

.PHONY: buster
buster:
	cd $(BUSTER_DIR) && cargo build --release

.PHONY: fifo
fifo:
	cd $(FIFO_DIR) && cargo build --release

.PHONY: flow-controlled-pipe
flow-controlled-pipe:
	cd $(FLOW_CONTROLLED_PIPE_DIR) && cargo build --release

.PHONY: marv
marv:
	cd $(MARV_DIR) && cargo build --release

.PHONY: peek-buffer
peek-buffer:
	cd $(PEEK_BUFFER_DIR) && cargo build --release

.PHONY: read-cache
read-cache:
	cd $(READ_CACHE_DIR) && cargo build --release

.PHONY: sim-clean
sim-clean: approx-reciprocal-clean buster-clean fifo-clean flow-controlled-pipe-clean marv-clean peek-buffer-clean read-cache-clean

.PHONY: approx-reciprocal-clean
approx-reciprocal-clean:
	cd $(APPROX_RECIPROCAL_DIR) && cargo clean

.PHONY: buster-clean
buster-clean:
	cd $(BUSTER_DIR) && cargo clean

.PHONY: fifo-clean
fifo-clean:
	cd $(FIFO_DIR) && cargo clean

.PHONY: flow-controlled-pipe-clean
flow-controlled-pipe-clean:
	cd $(FLOW_CONTROLLED_PIPE_DIR) && cargo clean

.PHONY: marv-clean
marv-clean:
	cd $(MARV_DIR) && cargo clean

.PHONY: peek-buffer-clean
peek-buffer-clean:
	cd $(PEEK_BUFFER_DIR) && cargo clean

.PHONY: read-cache-clean
read-cache-clean:
	cd $(READ_CACHE_DIR) && cargo clean

# Test

TEST_DIR=test

.PHONY: test
test: approx-reciprocal-test buster-test compliance-test fifo-test flow-controlled-pipe-test peek-buffer-test read-cache-test rtl-test

.PHONY: approx-reciprocal-test
approx-reciprocal-test: approx-reciprocal
	cd $(APPROX_RECIPROCAL_DIR) && cargo test --release

.PHONY: buster-test
buster-test: buster
	cd $(BUSTER_DIR) && cargo test --release

.PHONY: compliance-test
compliance-test: marv
	make -C $(TEST_DIR)/riscv-compliance

.PHONY: fifo-test
fifo-test: fifo
	cd $(FIFO_DIR) && cargo test --release && cargo run --release -- 10 10000000

.PHONY: flow-controlled-pipe-test
flow-controlled-pipe-test: flow-controlled-pipe
	cd $(FLOW_CONTROLLED_PIPE_DIR) && cargo test --release && cargo run --release -- 10 1000

.PHONY: peek-buffer-test
peek-buffer-test: peek-buffer
	cd $(PEEK_BUFFER_DIR) && cargo test --release && cargo run --release -- 10 1000

.PHONY: read-cache-test
read-cache-test: read-cache
	cd $(READ_CACHE_DIR) && cargo test --release && cargo run --release -- 10 2000

.PHONY: rtl-test
rtl-test: rtl
	cd $(RTL_DIR) && cargo test --release

.PHONY: test-clean
test-clean: compliance-test-clean

.PHONY: compliance-test-clean
compliance-test-clean:
	make clean -C $(TEST_DIR)/riscv-compliance


<|start_filename|>sim-old/ddr3_test_driver.cpp<|end_filename|>
#include "../obj_dir/Vtest.h"
#include "verilated_vcd_c.h"

#include <cinttypes>
#include <iostream>
using namespace std;

#ifdef _WIN32
#include <Windows.h>
#else
#include <dlfcn.h>
#endif

typedef struct
{
    uint32_t (*get_reset_n)();
    void (*set_reset_n)(uint32_t value);
    uint32_t (*get_clk)();
    void (*set_clk)(uint32_t value);

    uint32_t (*get_avl_ready)();
    void (*set_avl_ready)(uint32_t value);
    uint32_t (*get_avl_burstbegin)();
    void (*set_avl_burstbegin)(uint32_t value);
    uint32_t (*get_avl_addr)();
    void (*set_avl_addr)(uint32_t value);
    uint32_t (*get_avl_rdata_valid)();
    void (*set_avl_rdata_valid)(uint32_t value);
    uint64_t (*get_avl_rdata)();
    void (*set_avl_rdata)(uint64_t value);
    uint64_t (*get_avl_wdata)();
    void (*set_avl_wdata)(uint64_t value);
    uint32_t (*get_avl_be)();
    void (*set_avl_be)(uint32_t value);
    uint32_t (*get_avl_read_req)();
    void (*set_avl_read_req)(uint32_t value);
    uint32_t (*get_avl_write_req)();
    void (*set_avl_write_req)(uint32_t value);
    uint32_t (*get_avl_size)();
    void (*set_avl_size)(uint32_t value);

    uint32_t (*get_ddr3_init_done)();
    void (*set_ddr3_init_done)(uint32_t value);
    uint32_t (*get_ddr3_cal_success)();
    void (*set_ddr3_cal_success)(uint32_t value);
    uint32_t (*get_ddr3_cal_fail)();
    void (*set_ddr3_cal_fail)(uint32_t value);

    uint32_t (*get_is_finished)();
    void (*set_is_finished)(uint32_t value);
    uint32_t (*get_pass)();
    void (*set_pass)(uint32_t value);
    uint32_t (*get_fail)();
    void (*set_fail)(uint32_t value);

    void (*eval)();
    void (*final)();
    void (*trace_dump)(uint64_t time);
} Env;

static Vtest *top = nullptr;
static VerilatedVcdC *trace = nullptr;

uint32_t get_reset_n()
{
    return top->reset_n;
}

void set_reset_n(uint32_t value)
{
    top->reset_n = value;
}

uint32_t get_clk()
{
    return top->clk;
}

void set_clk(uint32_t value)
{
    top->clk = value;
}

uint32_t get_avl_ready()
{
    return top->avl_ready;
}

void set_avl_ready(uint32_t value)
{
    top->avl_ready = value;
}

uint32_t get_avl_burstbegin()
{
    return top->avl_burstbegin;
}

void set_avl_burstbegin(uint32_t value)
{
    top->avl_burstbegin = value;
}

uint32_t get_avl_addr()
{
    return top->avl_addr;
}

void set_avl_addr(uint32_t value)
{
    top->avl_addr = value;
}

uint32_t get_avl_rdata_valid()
{
    return top->avl_rdata_valid;
}

void set_avl_rdata_valid(uint32_t value)
{
    top->avl_rdata_valid = value;
}

uint64_t get_avl_rdata()
{
    return top->avl_rdata;
}

void set_avl_rdata(uint64_t value)
{
    top->avl_rdata = value;
}

uint64_t get_avl_wdata()
{
    return top->avl_wdata;
}

void set_avl_wdata(uint64_t value)
{
    top->avl_wdata = value;
}

uint32_t get_avl_be()
{
    return top->avl_be;
}

void set_avl_be(uint32_t value)
{
    top->avl_be = value;
}

uint32_t get_avl_read_req()
{
    return top->avl_read_req;
}

void set_avl_read_req(uint32_t value)
{
    top->avl_read_req = value;
}

uint32_t get_avl_write_req()
{
    return top->avl_write_req;
}

void set_avl_write_req(uint32_t value)
{
    top->avl_write_req = value;
}

uint32_t get_avl_size()
{
    return top->avl_size;
}

void set_avl_size(uint32_t value)
{
    top->avl_size = value;
}

uint32_t get_ddr3_init_done()
{
    return top->ddr3_init_done;
}

void set_ddr3_init_done(uint32_t value)
{
    top->ddr3_init_done = value;
}

uint32_t get_ddr3_cal_success()
{
    return top->ddr3_cal_success;
}

void set_ddr3_cal_success(uint32_t value)
{
    top->ddr3_cal_success = value;
}

uint32_t get_ddr3_cal_fail()
{
    return top->ddr3_cal_fail;
}

void set_ddr3_cal_fail(uint32_t value)
{
    top->ddr3_cal_fail = value;
}


uint32_t get_is_finished()
{
    return top->is_finished;
}

void set_is_finished(uint32_t value)
{
    top->is_finished = value;
}

uint32_t get_pass()
{
    return top->pass;
}

void set_pass(uint32_t value)
{
    top->pass = value;
}

uint32_t get_fail()
{
    return top->fail;
}

void set_fail(uint32_t value)
{
    top->fail = value;
}

void eval()
{
    top->eval();
}

void final()
{
    top->final();
}

void trace_dump(uint64_t time)
{
    if (trace)
        trace->dump((vluint64_t)time);
}

int main(int argc, char **argv)
{
    if (argc != 2 && argc != 3)
    {
        cout << "Invalid number of arguments" << endl;
        exit(1);
    }

    auto libName = argv[1];
    auto lib =
#ifdef _WIN32
        LoadLibrary(libName);
#else
        dlopen(libName, RTLD_LAZY);
#endif
    if (!lib)
    {
        cout << "Failed to load library: " << libName << endl;
        exit(1);
    }
    auto run = (int32_t (*)(Env *))
#ifdef _WIN32
        GetProcAddress
#else
        dlsym
#endif
        (lib, "run");
    if (!run)
    {
        cout << "Failed to get run proc address" << endl;
        exit(1);
    }

    top = new Vtest();

    if (argc == 3)
    {
        Verilated::traceEverOn(true);
        trace = new VerilatedVcdC();
        top->trace(trace, 99); // TODO: What is this param?
        trace->open(argv[2]);
    }

    Env env =
    {
        .get_reset_n = get_reset_n,
        .set_reset_n = set_reset_n,
        .get_clk = get_clk,
        .set_clk = set_clk,

        .get_avl_ready = get_avl_ready,
        .set_avl_ready = set_avl_ready,
        .get_avl_burstbegin = get_avl_burstbegin,
        .set_avl_burstbegin = set_avl_burstbegin,
        .get_avl_addr = get_avl_addr,
        .set_avl_addr = set_avl_addr,
        .get_avl_rdata_valid = get_avl_rdata_valid,
        .set_avl_rdata_valid = set_avl_rdata_valid,
        .get_avl_rdata = get_avl_rdata,
        .set_avl_rdata = set_avl_rdata,
        .get_avl_wdata = get_avl_wdata,
        .set_avl_wdata = set_avl_wdata,
        .get_avl_be = get_avl_be,
        .set_avl_be = set_avl_be,
        .get_avl_read_req = get_avl_read_req,
        .set_avl_read_req = set_avl_read_req,
        .get_avl_write_req = get_avl_write_req,
        .set_avl_write_req = set_avl_write_req,
        .get_avl_size = get_avl_size,
        .set_avl_size = set_avl_size,

        .get_ddr3_init_done = get_ddr3_init_done,
        .set_ddr3_init_done = set_ddr3_init_done,
        .get_ddr3_cal_success = get_ddr3_cal_success,
        .set_ddr3_cal_success = set_ddr3_cal_success,
        .get_ddr3_cal_fail = get_ddr3_cal_fail,
        .set_ddr3_cal_fail = set_ddr3_cal_fail,

        .get_is_finished = get_is_finished,
        .set_is_finished = set_is_finished,
        .get_pass = get_pass,
        .set_pass = set_pass,
        .get_fail = get_fail,
        .set_fail = set_fail,

        .eval = eval,
        .final = final,
        .trace_dump = trace_dump,
    };

    auto ret = run(&env);

    if (trace)
    {
        trace->close();
        delete trace;
    }

    delete top;

    return ret;
}
