Skip to content

Commit

Permalink
[nix] simplify runEmu
Browse files Browse the repository at this point in the history
  • Loading branch information
FanShupei authored and Avimitin committed Jan 25, 2025
1 parent 6650fec commit e44c918
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 210 deletions.
44 changes: 0 additions & 44 deletions nix/t1/conversion/link-verilator-with-dpi.nix

This file was deleted.

155 changes: 76 additions & 79 deletions nix/t1/conversion/sv-to-vcs-simulator.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@
, enableCover ? false
, enableTrace ? false
, vcsLinkLibs ? [ ]
, rtLinkDpiLib ? null
, topModule ? null
}:

assert lib.assertMsg (builtins.typeOf vsrc == "list") "vsrc should be a list of file path";
assert lib.assertMsg (builtins.typeOf vcsLinkLibs == "list") "vcsLinkLibs should be list of strings";

stdenv.mkDerivation rec {
name = mainProgram;
inherit mainProgram;

# require license
__noChroot = true;
dontPatchELF = true;

dontUnpack = true;

let
# VCS simulation profiling
# This is a debug feature, thus intentionally not exposed
# Enable it by changing line below to 'true'
enableProfile = false;

vcsArgs = [
vcsCompileArgs = [
"-sverilog"
"-full64"
"-timescale=1ns/1ps"
Expand Down Expand Up @@ -72,81 +64,86 @@ stdenv.mkDerivation rec {
]
++ vcsLinkLibs;

buildPhase = ''
runHook preBuild
vcsRtLinkArgs = lib.optionals (rtLinkDpiLib != null) [
"-sv_root"
"${rtLinkDpiLib}/lib"
"-sv_lib"
"${rtLinkDpiLib.svLibName}"
];

# vcsRtLinkArgs is only allowed in passthru
# to enable better caching
self = stdenv.mkDerivation {
name = mainProgram;
inherit mainProgram;

# require license
__noChroot = true;
dontPatchELF = true;

fhsEnv="${snps-fhs-env}/bin/snps-fhs-env"
DWBB_DIR=$($fhsEnv -c "echo \$DWBB_DIR")
vcsArgsStr="${lib.escapeShellArgs vcsArgs}"
dontUnpack = true;

echo "[nix] running VCS with args: $fhsEnv vcs $vcsArgsStr"
"$fhsEnv" -c "vcs $vcsArgsStr -o $mainProgram"
buildPhase = ''
runHook preBuild
runHook postBuild
'';
fhsEnv="${snps-fhs-env}/bin/snps-fhs-env"
DWBB_DIR=$($fhsEnv -c "echo \$DWBB_DIR")
vcsArgsStr="${lib.escapeShellArgs vcsCompileArgs}"
passthru = rec {
inherit snps-fhs-env enableTrace enableCover;
echo "[nix] running VCS with args: $fhsEnv vcs $vcsArgsStr"
"$fhsEnv" -c "vcs $vcsArgsStr -o $mainProgram"
# DPI library should be loaded at runtime through "-sv_lib"
isRuntimeLoad = (builtins.length vcsLinkLibs == 0);
runHook postBuild
'';

emuKind = "vcs";
buildCmdArgs =
{ plusargs
, vcsExtraArgs ? [ ]
, vcsDpiLib ? null
}:
assert if isRuntimeLoad
then lib.assertMsg (vcsDpiLib != null) "vcsDpiLib must be set for rtlink emu"
else lib.assertMsg (vcsDpiLib == null) "vcsDpiLib must not be set for static-link emu";
[
passthru = {
inherit snps-fhs-env enableTrace enableCover;

emuKind = "vcs";
driverWithArgs =
[ (lib.getExe self) ]
++ vcsRtLinkArgs
++ [
"-exitstatus"
"-assert"
"global_finish_maxfail=10000"
]
++ lib.optionals (vcsDpiLib != null) [
"-sv_root"
"${vcsDpiLib}/lib"
"-sv_lib"
"${vcsDpiLib.svLibName}"
]
++ plusargs
++ vcsExtraArgs;
};

shellHook = ''
echo "[nix] entering fhs env"
${snps-fhs-env}/bin/snps-fhs-env
'';

installPhase = ''
runHook preInstall
mkdir -p $out/bin $out/lib
cp $mainProgram $out/lib
cp -r $mainProgram.daidir $out/lib
${lib.optionalString enableCover ''
cp -r ./cm.vdb $out/lib
''}
# We need to carefully handle string escape here, so don't use makeWrapper
tee $out/bin/$mainProgram <<EOF
#!${bash}/bin/bash
_argv="\$@"
${lib.optionalString enableCover ''
cp -r $out/lib/cm.vdb ./cm.vdb
chmod +w -R ./cm.vdb
''}
${snps-fhs-env}/bin/snps-fhs-env -c "$out/lib/$mainProgram ${lib.optionalString enableCover ''-cm_dir ./cm.vdb''} \$_argv"
EOF
chmod +x $out/bin/$mainProgram
runHook postInstall
'';

meta = {
inherit mainProgram;
];
};

shellHook = ''
echo "[nix] entering fhs env"
${snps-fhs-env}/bin/snps-fhs-env
'';

installPhase = ''
runHook preInstall
mkdir -p $out/bin $out/lib
cp $mainProgram $out/lib
cp -r $mainProgram.daidir $out/lib
${lib.optionalString enableCover ''
cp -r ./cm.vdb $out/lib
''}
# We need to carefully handle string escape here, so don't use makeWrapper
tee $out/bin/$mainProgram <<EOF
#!${bash}/bin/bash
_argv="\$@"
${lib.optionalString enableCover ''
cp -r $out/lib/cm.vdb ./cm.vdb
chmod +w -R ./cm.vdb
''}
${snps-fhs-env}/bin/snps-fhs-env -c "$out/lib/$mainProgram ${lib.optionalString enableCover ''-cm_dir ./cm.vdb''} \$_argv"
EOF
chmod +x $out/bin/$mainProgram
runHook postInstall
'';

meta = {
inherit mainProgram;
};
};
}
in
self
131 changes: 81 additions & 50 deletions nix/t1/conversion/sv-to-verilator-emulator.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,12 @@
, vsrc
, enableTrace ? false
, extraVerilatorArgs ? [ ]
, dpiLibs ? [ ]
}:

assert lib.assertMsg (builtins.typeOf vsrc == "list") "vsrc should be a list of file path";

stdenv.mkDerivation (rec {
name = mainProgram;
inherit mainProgram;

__noChroot = true;

dontUnpack = true;

nativeBuildInputs = [ verilator ];

# zlib is required for Rust to link against
propagatedBuildInputs = [ zlib ];

let
verilatorFilelist = "${rtl}/filelist.f";
verilatorThreads = 4;
verilatorArgs = [
Expand All @@ -54,57 +43,99 @@ stdenv.mkDerivation (rec {
"--top"
topModule
]
++ extraVerilatorArgs
++ lib.optionals enableTrace [
"+define+T1_ENABLE_TRACE"
"--trace-fst"
];
]
++ extraVerilatorArgs;

buildPhase = ''
runHook preBuild
# verilatedLib should NOT depend on dpiLibs
# to enable better caching
verilatedLib = stdenv.mkDerivation (rec {
name = mainProgram + "-vlib";

DWBB_DIR=$(${snps-fhs-env}/bin/snps-fhs-env -c "echo \$DWBB_DIR")
__noChroot = true;

verilatorPhase="verilator ${lib.escapeShellArgs verilatorArgs}"
echo "[nix] running verilator: $verilatorPhase"
$builder -c "$verilatorPhase"
dontUnpack = true;

echo "[nix] building verilated C lib"
# backup srcs
mkdir -p $out/share
cp -r obj_dir $out/share/verilated_src
nativeBuildInputs = [ verilator ];

make -j "$NIX_BUILD_CORES" -f V${topModule}.mk -C obj_dir
# zlib is required for Rust to link against
propagatedBuildInputs = [ zlib ];

runHook postBuild
'';
buildPhase = ''
runHook preBuild
passthru = {
inherit topModule;
inherit enableTrace;
DWBB_DIR=$(${snps-fhs-env}/bin/snps-fhs-env -c "echo \$DWBB_DIR")
buildCmdArgs =
{ plusargs
, verilatorExtraArgs ? [ ]
}: plusargs ++ verilatorExtraArgs;
};
verilatorPhase="verilator ${lib.escapeShellArgs verilatorArgs}"
echo "[nix] running verilator: $verilatorPhase"
$builder -c "$verilatorPhase"
echo "[nix] building verilated C lib"
# backup srcs
mkdir -p $out/share
cp -r obj_dir $out/share/verilated_src
make -j "$NIX_BUILD_CORES" -f V${topModule}.mk -C obj_dir
runHook postBuild
'';

passthru = {
inherit topModule;
inherit enableTrace;
};

installPhase = ''
runHook preInstall
installPhase = ''
runHook preInstall
mkdir -p $out/{include,lib}
cp obj_dir/*.h $out/include
cp obj_dir/*.a $out/lib
mkdir -p $out/{include,lib}
cp obj_dir/*.h $out/include
cp obj_dir/*.a $out/lib
runHook postInstall
'';
runHook postInstall
'';

meta = {
# nix fortify hardening add `-O2` gcc flag,
# we'd like verilator to controll optimization flags, so disable it.
# `-O2` will make gcc build time in verilating extremely long
hardeningDisable = [ "fortify" ];
});

verilatorLinkArgs = [
"${verilatedLib}/lib/libV${verilatedLib.topModule}.a"
]
++ dpiLibs
++ [
"${verilatedLib}/lib/libverilated.a"
"-lz"
];

self = stdenv.mkDerivation {
name = mainProgram;
inherit mainProgram;
};

# nix fortify hardening add `-O2` gcc flag,
# we'd like verilator to controll optimization flags, so disable it.
# `-O2` will make gcc build time in verilating extremely long
hardeningDisable = [ "fortify" ];
})
dontUnpack = true;

propagatedBuildInputs = [ zlib ];

buildCommand = ''
mkdir -p $out/bin
$CXX -o $out/bin/$mainProgram ${lib.escapeShellArgs verilatorLinkArgs}
'';

meta = {
inherit mainProgram;
};

passthru = {
inherit verilatedLib;
inherit enableTrace;
emuKind = "verilator";
driverWithArgs = [ (lib.getExe self) ];
};
};
in
self
Loading

0 comments on commit e44c918

Please sign in to comment.