Skip to content

Commit 828a146

Browse files
committed
fix: cppEmitter do not add additional code when waveform is disabled
1 parent be8a7d1 commit 828a146

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Makefile

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ mainargs ?= ready-to-run/bin/linux.bin
88
# uncomment this line to let this file be part of dependency of each .o file
99
THIS_MAKEFILE = Makefile
1010

11+
WAVEFORM ?= 0
12+
WAVEFORM_PATH ?= $(WORK_DIR)/waveform.fst
13+
1114
ifeq ($(dutName),ysyx3)
1215
NAME ?= newtop
1316
TEST_FILE = $(NAME)-ysyx3
@@ -59,6 +62,12 @@ include mk/toolchain.mk
5962
SHELL := /bin/bash
6063
TIME = /usr/bin/time
6164
LOG_FILE = $(WORK_DIR)/$(dutName).log
65+
WAVE_LOG_FILE = $(WORK_DIR)/$(dutName)-wave.log
66+
EMU_RUN_ENV :=
67+
ifeq ($(WAVEFORM),1)
68+
GSIM_FLAGS_EXTRA += --trace-fst
69+
EMU_RUN_ENV += GSIM_ENABLE_WAVEFORM=1 GSIM_WAVEFORM_PATH=$(WAVEFORM_PATH)
70+
endif
6271

6372
CFLAGS_DUT = -DDUT_NAME=S$(NAME) -DDUT_HEADER=\"$(NAME).h\" -D__DUT_$(shell echo $(dutName) | tr - _)__
6473

@@ -214,11 +223,13 @@ else
214223
EMU_MAIN_SRCS = emu/emu.cpp
215224
endif
216225
EMU_GEN_SRCS = $(shell find $(GEN_CPP_DIR) -name "*.cpp" 2> /dev/null)
226+
EMU_SRCS += emu/gsim_fst_impl.cpp
217227
EMU_SRCS += $(EMU_MAIN_SRCS) $(EMU_GEN_SRCS)
218228

219-
EMU_CFLAGS := -O1 -MMD $(addprefix -I, $(abspath $(GEN_CPP_DIR))) $(EMU_CFLAGS) # allow to overwrite optimization level
220-
EMU_CFLAGS += $(MODE_FLAGS) $(CFLAGS_DUT) -Wno-parentheses-equality
229+
EMU_CFLAGS := -O1 -MMD $(addprefix -I, $(abspath $(GEN_CPP_DIR))) -I$(abspath include) $(EMU_CFLAGS) # allow to overwrite optimization level
230+
EMU_CFLAGS += $(MODE_FLAGS) $(CFLAGS_DUT) -Wno-parentheses-equality -pthread
221231
EMU_CFLAGS += -fbracket-depth=2048
232+
EMU_LDFLAGS += -lz
222233
#EMU_CFLAGS += -fsanitize=address -fsanitize-address-use-after-scope
223234
#EMU_CFLAGS += -fsanitize=undefined -fsanitize=pointer-compare -fsanitize=pointer-subtract
224235
#EMU_CFLAGS += -pg -ggdb
@@ -237,7 +248,7 @@ run-emu-simpoint: $(EMU_BIN)
237248
@echo 'Please run "$^ <gcpt> <checkpoint>" manually'
238249

239250
run-emu: $(EMU_BIN)
240-
$(TIME) taskset 0x1 $^ $(mainargs)
251+
env $(EMU_RUN_ENV) $(TIME) taskset 0x1 $^ $(mainargs)
241252

242253
clean-emu:
243254
-rm -rf $(EMU_BUILD_DIR) $(EMU_BIN)
@@ -376,6 +387,9 @@ run:
376387
mkdir -p $(dir $(LOG_FILE))
377388
set -o pipefail && $(TIME) $(MAKE) run-internal 2>&1 | tee $(LOG_FILE)
378389

390+
run-waveform:
391+
$(MAKE) run WAVEFORM=1 LOG_FILE=$(WAVE_LOG_FILE) WAVE_LOG_FILE=$(WAVE_LOG_FILE) WAVEFORM_PATH=$(WAVEFORM_PATH)
392+
379393
diff:
380394
mkdir -p $(dir $(LOG_FILE))
381395
set -o pipefail && $(TIME) $(MAKE) diff-internal 2>&1 | tee $(LOG_FILE)

emu/emu.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <fstream>
99
#include <csignal>
1010
#include <chrono>
11+
#include <cstdlib>
12+
#include <string>
1113
#include <sys/mman.h>
1214
#include <sys/stat.h>
1315
#include <fcntl.h>
@@ -97,6 +99,8 @@ extern "C" void sd_read(int* data){
9799
#if defined(GSIM)
98100
#include DUT_HEADER
99101
static DUT_NAME* dut;
102+
static bool waveform_enabled = false;
103+
static std::string waveform_path;
100104

101105
void dut_init(DUT_NAME *dut) {
102106
#if defined(__DUT_NutShell__)
@@ -126,6 +130,24 @@ void dut_hook(DUT_NAME *dut) {
126130
}
127131
#endif
128132
}
133+
134+
static void maybe_enable_waveform(DUT_NAME *dut) {
135+
const char* env_enable = std::getenv("GSIM_ENABLE_WAVEFORM");
136+
if (env_enable == nullptr || env_enable[0] == '0') return;
137+
const char* env_path = std::getenv("GSIM_WAVEFORM_PATH");
138+
waveform_path = env_path ? std::string(env_path) : std::string("waveform.fst");
139+
dut->setWaveformPath(waveform_path);
140+
dut->enableWaveform();
141+
waveform_enabled = true;
142+
}
143+
144+
static void flush_waveform_if_needed() {
145+
if (waveform_enabled && dut) {
146+
dut->flushWaveform();
147+
}
148+
}
149+
#else
150+
static inline void flush_waveform_if_needed() {}
129151
#endif
130152

131153
#if defined(VERILATOR)
@@ -253,6 +275,7 @@ int main(int argc, char** argv) {
253275
dut = new DUT_NAME();
254276
memcpy(&dut->DUT_MEMORY, program, program_sz);
255277
dut_init(dut);
278+
maybe_enable_waveform(dut);
256279
dut_reset();
257280
#endif
258281
#ifdef VERILATOR
@@ -298,6 +321,7 @@ int main(int argc, char** argv) {
298321
printf("ALL diffs: dut -- ref\n");
299322
printf("Failed after %ld cycles\n", cycles);
300323
checkSignals(false);
324+
flush_waveform_if_needed();
301325
return -1;
302326
}
303327
#endif
@@ -335,9 +359,17 @@ int main(int argc, char** argv) {
335359
}
336360
#endif
337361
#if defined(PERF) || defined(PERF_CYCLE)
338-
if (cycles >= CYCLE_MAX_PERF) return 0;
362+
if (cycles >= CYCLE_MAX_PERF) {
363+
flush_waveform_if_needed();
364+
return 0;
365+
}
339366
#endif
340-
if (cycles == CYCLE_MAX_SIM) return 0;
367+
if (cycles == CYCLE_MAX_SIM) {
368+
flush_waveform_if_needed();
369+
return 0;
370+
}
341371
}
342372
}
373+
flush_waveform_if_needed();
374+
return 0;
343375
}

emu/gsim_fst_impl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define GSIM_FST_IMPL 1
2+
#include "gsimFst.h"

src/cppEmitter.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,12 +600,13 @@ int graph::translateInst(InstInfo inst, int indent, std::string flagName) {
600600
if (inst.node->isArray() || inst.node->type == NODE_WRITER) activateUncondNext(inst.node, inst.node->nextActiveId, false, flagName, indent);
601601
else activateNext(inst.node, inst.node->nextActiveId, oldName(inst.node), false, flagName, indent);
602602
}
603-
if (fstWaveNodeSet.count(inst.node)) {
603+
if (globalConfig.TraceFst && fstWaveNodeSet.count(inst.node)) {
604+
emitBodyLock(indent, "if (dumpWaveformFlag && fstCtx) {\n");
604605
std::vector<int> dims = nodeArrayDims(inst.node);
605606
if (dims.empty()) {
606-
emitBodyLock(indent, "updateFstSignal(\"%s\", &%s, %d);\n", inst.node->name.c_str(), inst.node->name.c_str(), inst.node->width);
607+
emitBodyLock(indent + 1, "updateFstSignal(\"%s\", &%s, %d);\n", inst.node->name.c_str(), inst.node->name.c_str(), inst.node->width);
607608
} else {
608-
int curIndent = indent;
609+
int curIndent = indent + 1;
609610
for (size_t i = 0; i < dims.size(); i ++) {
610611
emitBodyLock(curIndent ++, "for (int i%zu = 0; i%zu < %d; i%zu ++) {\n", i, i, dims[i], i);
611612
}
@@ -620,6 +621,7 @@ int graph::translateInst(InstInfo inst, int indent, std::string flagName) {
620621
emitBodyLock(-- curIndent, "}\n");
621622
}
622623
}
624+
emitBodyLock(indent, "}\n");
623625
}
624626
if (skipActivate) break;
625627
}

0 commit comments

Comments
 (0)