From 97df569747670a240dc8b0c05c6aa11cb7bd0c0a Mon Sep 17 00:00:00 2001 From: xinyangli Date: Sat, 23 Dec 2023 20:23:18 +0800 Subject: [PATCH] create Makefile for example in verilator manual --- npc/Makefile | 31 +++++++++++++++++++++++++++---- npc/csrc/main.cpp | 34 ++++++++++++++++++++++++++++++---- npc/vsrc/example.v | 9 +++++++-- 3 files changed, 64 insertions(+), 10 deletions(-) diff --git a/npc/Makefile b/npc/Makefile index 46469fa..4910019 100644 --- a/npc/Makefile +++ b/npc/Makefile @@ -1,8 +1,31 @@ -all: - @echo "Write this Makefile by your self." +VSRC := $(wildcard vsrc/*.v) +CPPSRC := $(addprefix $(PWD)/,$(wildcard csrc/*.cpp)) +PREFIX ?= build +OBJDIR := $(PREFIX)/obj +SUBMAKE := $(OBJDIR)/Vexample.mk +VERILATOR_FLAGS := --cc --exe -sim: +all: sim + +sim: VERILATOR_FLAGS += --trace +sim: $(VSRC) $(CPPSRC) $(OBJDIR)/Vexample $(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!! - @echo "Write this Makefile by your self." + @echo "Running" $(OBJDIR)/Vexample "..." + @echo "================================" + @$(OBJDIR)/Vexample + +$(OBJDIR)/Vexample: $(SUBMAKE) + $(MAKE) -C $(OBJDIR) -f $(notdir $(SUBMAKE)) Vexample + +$(SUBMAKE): $(VSRC) $(CPPSRC) $(OBJDIR) + verilator $(VERILATOR_FLAGS) --Mdir $(PWD)/$(OBJDIR) $(VSRC) $(CPPSRC) + +$(OBJDIR): + mkdir -p $(OBJDIR) include ../Makefile + +.PHONY: clean + +clean: + $(RM) -r $(OBJDIR) diff --git a/npc/csrc/main.cpp b/npc/csrc/main.cpp index 5e95ddc..70e48e4 100644 --- a/npc/csrc/main.cpp +++ b/npc/csrc/main.cpp @@ -1,6 +1,32 @@ -#include +#include +#include +#include +#include +#include +#include "Vexample.h" -int main() { - printf("Hello, ysyx!\n"); - return 0; +#define MAX_SIM_TIME 100 + +int main(int argc, char **argv, char **env) { + int sim_time = 0; + Verilated::commandArgs(argc, argv); + Vexample *top = new Vexample; + + Verilated::traceEverOn(true); + VerilatedVcdC *m_trace = new VerilatedVcdC; + top->trace(m_trace, 5); + m_trace->open("waveform.vcd"); + for (sim_time = 0; sim_time < MAX_SIM_TIME; sim_time++) { + int a = rand() & 1; + int b = rand() & 1; + top->a = a; + top->b = b; + top->eval(); + printf("a = %d, b = %d, f = %d\n", a, b, top->f); + assert(top->f == (a ^ b)); + m_trace->dump(sim_time); + } + m_trace->close(); + delete top; + exit(EXIT_SUCCESS); } diff --git a/npc/vsrc/example.v b/npc/vsrc/example.v index 536579a..38b2b39 100644 --- a/npc/vsrc/example.v +++ b/npc/vsrc/example.v @@ -1,2 +1,7 @@ -module example(); -endmodule +module top( + input a, + input b, + output f +); + assign f = a ^ b; +endmodule \ No newline at end of file