This commit is contained in:
Yu-Sheng Lin 2026-03-30 16:19:55 -07:00 committed by GitHub
commit d2e7a841b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
27 changed files with 3016 additions and 12053 deletions

View File

@ -182,6 +182,6 @@ install(
PATTERN "include/*.cpp"
PATTERN "include/*.vlt"
PATTERN "include/*.sv"
PATTERN "include/gtkwave/*.[chv]*"
PATTERN "include/fstcpp/*.[chv]*"
PATTERN "include/vltstd/*.[chv]*"
)

View File

@ -260,7 +260,7 @@ VL_INST_INC_SRCDIR_FILES = \
include/*.[chv]* \
include/*.vlt \
include/*.sv \
include/gtkwave/*.[chv]* \
include/fstcpp/*.[chv]* \
include/vltstd/*.[chv]* \
VL_INST_DATA_SRCDIR_FILES = \
@ -311,7 +311,7 @@ installman: $(VL_INST_MAN_FILES)
done
installdata:
$(MKINSTALLDIRS) $(DESTDIR)$(pkgdatadir)/include/gtkwave
$(MKINSTALLDIRS) $(DESTDIR)$(pkgdatadir)/include/fstcpp
$(MKINSTALLDIRS) $(DESTDIR)$(pkgdatadir)/include/vltstd
for p in $(VL_INST_INC_BLDDIR_FILES) ; do \
$(INSTALL_DATA) $$p $(DESTDIR)$(pkgdatadir)/$$p; \
@ -356,7 +356,7 @@ uninstall:
-rm $(DESTDIR)$(pkgdatadir)/verilator-config.cmake
-rm $(DESTDIR)$(pkgdatadir)/verilator-config-version.cmake
-rmdir $(DESTDIR)$(pkgdatadir)/bin
-rmdir $(DESTDIR)$(pkgdatadir)/include/gtkwave
-rmdir $(DESTDIR)$(pkgdatadir)/include/fstcpp
-rmdir $(DESTDIR)$(pkgdatadir)/include/vltstd
-rmdir $(DESTDIR)$(pkgdatadir)/include
-rmdir $(DESTDIR)$(pkgdatadir)/examples/make_hello_binary
@ -417,7 +417,7 @@ CPPCHECK_FLAGS += --cppcheck-build-dir=$(CPPCHECK_CACHE)
CPPCHECK_FLAGS += -DVL_DEBUG=1 -DVL_CPPCHECK=1 -DINFILTER_PIPE=1 -D__GNUC__=1
CPPCHECK_FLAGS += -j$(CPPCHECK_JOBS)
CPPCHECK_INC = -I$(srcdir)/include
CPPCHECK_INC += -I$(srcdir)/include/gtkwave
CPPCHECK_INC += -I$(srcdir)/include/fstcpp
CPPCHECK_INC += -I$(srcdir)/include/vltstd
CPPCHECK_INC += -I$(srcdir)/src/obj_dbg
CPPCHECK_INC += -I$(srcdir)/src
@ -684,7 +684,7 @@ FASTCOV_OPT += --dump-statistic
FASTCOV_OPT += --exclude-glob
FASTCOV_OPT += '/usr/*'
FASTCOV_OPT += '*examples/*'
FASTCOV_OPT += '*include/gtkwave/*'
FASTCOV_OPT += '*include/fstcpp/*'
FASTCOV_OPT += '*src/obj_dbg/*'
FASTCOV_OPT += '*src/obj_opt/*.yy.cpp'
FASTCOV_OPT += '*src/obj_opt/V3Ast*'

View File

@ -64,7 +64,7 @@ In brief, to install from git:
#sudo apt-get install libgoogle-perftools-dev libjemalloc-dev numactl perl-doc
#sudo apt-get install libfl2 # Ubuntu only (ignore if gives error)
#sudo apt-get install libfl-dev # Ubuntu only (ignore if gives error)
#sudo apt-get install zlibc zlib1g zlib1g-dev # Ubuntu only (ignore if gives error)
#sudo apt-get install zlibc zlib1g zlib1g-dev liblz4 liblz4-dev # Ubuntu only (ignore if gives error)
git clone https://github.com/verilator/verilator # Only first time
@ -116,7 +116,7 @@ To build or run Verilator, you need these standard packages:
sudo apt-get install libgz # Non-Ubuntu (ignore if gives error)
sudo apt-get install libfl2 # Ubuntu only (ignore if gives error)
sudo apt-get install libfl-dev # Ubuntu only (ignore if gives error)
sudo apt-get install zlibc zlib1g zlib1g-dev # Ubuntu only (ignore if gives error)
sudo apt-get install zlibc zlib1g zlib1g-dev liblz4 liblz4-dev # Ubuntu only (ignore if gives error)
For SystemC:

View File

@ -0,0 +1,22 @@
---
Language: Cpp
BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: BlockIndent
AlignEscapedNewlines: Left
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: Inline
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Attach
ColumnLimit: 100
ContinuationIndentWidth: 4
DerivePointerAlignment: false
IncludeBlocks: Preserve
IndentCaseLabels: false
IndentPPDirectives: AfterHash
IndentWidth: 4
PointerAlignment: Right
TabWidth: 4
UseTab: ForContinuationAndIndentation

260
include/fstcpp/fstcpp.h Normal file
View File

@ -0,0 +1,260 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
// C system headers
// C++ standard library headers
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <string>
// Other libraries' .h files.
// Your project's .h files.
#if defined(MSC_VER_) || defined(FORCE_MSC_VER_)
# define USE_GCC_INTRINSIC 0
// Note: we do not support MSVC intrinsic for now
# define USE_MSVC_INTRINSIC 0
#elif defined(__GNUC__) || defined(__clang__)
# define USE_GCC_INTRINSIC 1
# define USE_MSVC_INTRINSIC 0
#else
# define USE_GCC_INTRINSIC 0
# define USE_MSVC_INTRINSIC 0
#endif
// Remove these when we upgrade to C++20
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wc++17-attribute-extensions"
#pragma GCC diagnostic ignored "-Wc++20-attribute-extensions"
namespace fst {
typedef uint32_t Handle;
typedef uint32_t EnumHandle;
struct string_view_pair {
const char *m_data = nullptr;
size_t m_size = 0;
// implicit conversion from const char*, std::string, std::string_view
string_view_pair(const char *data)
: m_data{data}, m_size{data == nullptr ? 0 : std::strlen(data)} {}
string_view_pair(const char *data, size_t size) : m_data{data}, m_size{size} {}
string_view_pair(const std::string &s) : m_data{s.c_str()}, m_size{s.size()} {}
#if __cplusplus >= 201703L
string_view_pair(std::string_view s) : m_data{s.data()}, m_size{s.size()} {}
#endif
};
[[maybe_unused]]
static inline string_view_pair make_string_view_pair(const char *data) {
if (!data) {
return {nullptr, 0};
}
return {data, std::strlen(data)};
}
[[maybe_unused]]
static inline string_view_pair make_string_view_pair(const char *data, std::size_t size) {
return {data, size};
}
enum class WriterPackType : uint8_t {
ZLIB = 0, // not supported
FASTLZ = 1, // not supported
LZ4 = 2,
// usually for testing, you should use eLz4
// This will turn off compression for geometry/hierarchy/wave data
NO_COMPRESSION = 3,
};
enum class FileType : uint8_t {
VERILOG = 0,
VHDL,
VERILOG_VHDL,
};
enum class EncodingType : uint8_t {
BINARY = 0, // 1 bit per bit to represent 0,1
VERILOG = 1, // 2 bits per bit to represent X,Z
VHDL = 2, // 4 bits per bit to represent H,U,W,L,-,?
};
[[maybe_unused]]
static inline constexpr unsigned bitPerEncodedBit(EncodingType type) {
return 1 << static_cast<uint8_t>(type);
}
[[maybe_unused]]
static const char* kEncodedBitToCharTable = (
"01" // Binary
"xzhu" // Verilog
"wl-? " // Vhdl (padded with ' ')
);
struct Hierarchy {
enum class ScopeType : uint8_t {
VCD_MODULE = 0,
VCD_TASK = 1,
VCD_FUNCTION = 2,
VCD_BEGIN = 3,
VCD_FORK = 4,
VCD_GENERATE = 5,
VCD_STRUCT = 6,
VCD_UNION = 7,
VCD_CLASS = 8,
VCD_INTERFACE = 9,
VCD_PACKAGE = 10,
VCD_PROGRAM = 11,
VHDL_ARCHITECTURE = 12,
VHDL_PROCEDURE = 13,
VHDL_FUNCTION = 14,
VHDL_RECORD = 15,
VHDL_PROCESS = 16,
VHDL_BLOCK = 17,
VHDL_FORGENERATE = 18,
VHDL_IFGENERATE = 19,
VHDL_GENERATE = 20,
VHDL_PACKAGE = 21,
SV_ARRAY = 22,
};
enum class ScopeControlType : uint8_t {
GEN_ATTR_BEGIN = 252,
GEN_ATTR_END = 253,
VCD_SCOPE = 254,
VCD_UPSCOPE = 255,
};
enum class VarType : uint8_t {
VCD_EVENT = 0,
VCD_INTEGER = 1,
VCD_PARAMETER = 2,
VCD_REAL = 3,
VCD_REAL_PARAMETER = 4,
VCD_REG = 5,
VCD_SUPPLY0 = 6,
VCD_SUPPLY1 = 7,
VCD_TIME = 8,
VCD_TRI = 9,
VCD_TRIAND = 10,
VCD_TRIOR = 11,
VCD_TRIREG = 12,
VCD_TRI0 = 13,
VCD_TRI1 = 14,
VCD_WAND = 15,
VCD_WIRE = 16,
VCD_WOR = 17,
VCD_PORT = 18,
VCD_SPARRAY = 19,
VCD_REALTIME = 20,
GEN_STRING = 21,
SV_BIT = 22,
SV_LOGIC = 23,
SV_INT = 24,
SV_SHORTINT = 25,
SV_LONGINT = 26,
SV_BYTE = 27,
SV_ENUM = 28,
SV_SHORTREAL = 29,
};
enum class VarDirection : uint8_t {
MIN = 0,
IMPLICIT = 0,
INPUT = 1,
OUTPUT = 2,
INOUT = 3,
BUFFER = 4,
LINKAGE = 5,
MAX = 5,
};
enum class AttrType : uint8_t {
MIN = 0,
MISC = 0,
ARRAY = 1,
ENUM = 2,
PACK = 3,
MAX = 3,
};
enum class AttrSubType : uint8_t {
// For AttrType::eMisc
MISC_MIN = 0,
MISC_COMMENT = 0,
MISC_ENVVAR = 1,
MISC_SUPVAR = 2,
MISC_PATHNAME = 3,
MISC_SOURCESTEM = 4,
MISC_SOURCEISTEM = 5,
MISC_VALUELIST = 6,
MISC_ENUMTABLE = 7,
MISC_UNKNOWN = 8,
MISC_MAX = 8,
// For AttrType::eArray
ARRAY_MIN = 0,
ARRAY_NONE = 0,
ARRAY_UNPACKED = 1,
ARRAY_PACKED = 2,
ARRAY_SPARSE = 3,
ARRAY_MAX = 3,
// For AttrType::eEnum
ENUM_MIN = 0,
ENUM_SV_INTEGER = 0,
ENUM_SV_BIT = 1,
ENUM_SV_LOGIC = 2,
ENUM_SV_INT = 3,
ENUM_SV_SHORTINT = 4,
ENUM_SV_LONGINT = 5,
ENUM_SV_BYTE = 6,
ENUM_SV_UNSIGNED_INTEGER = 7,
ENUM_SV_UNSIGNED_BIT = 8,
ENUM_SV_UNSIGNED_LOGIC = 9,
ENUM_SV_UNSIGNED_INT = 10,
ENUM_SV_UNSIGNED_SHORTINT = 11,
ENUM_SV_UNSIGNED_LONGINT = 12,
ENUM_SV_UNSIGNED_BYTE = 13,
ENUM_REG = 14,
ENUM_TIME = 15,
ENUM_MAX = 15,
// For AttrType::ePack
PACK_MIN = 0,
PACK_NONE = 0,
PACK_UNPACKED = 1,
PACK_PACKED = 2,
PACK_SPARSE = 3,
PACK_MAX = 3,
};
enum class SupplementalVarType : uint8_t {};
enum class SupplementalDataType : uint8_t {};
};
struct Header {
uint64_t m_start_time{uint64_t(-1)};
uint64_t m_end_time{0};
int64_t m_timezero{0};
// Match the original fstapi.c. Just for information, not used in FST.
uint64_t m_writer_memory_use{1ull << 27};
uint64_t m_num_scopes{0};
uint64_t m_num_vars{0}; // #CreateVar calls, including aliases
uint64_t m_num_handles{0}; // #unique handles, excluding aliases, shall be <= m_num_vars
uint64_t m_num_value_change_data_blocks{0};
char m_writer[128]{};
char m_date[26]{};
FileType m_filetype{FileType::VERILOG};
int8_t m_timescale{-9};
};
static constexpr uint64_t kInvalidTime = uint64_t(-1);
} // namespace fst

View File

@ -0,0 +1,113 @@
// SPDX-FileCopyrightText: 2025 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
// C system headers
// C++ standard library headers
#include <cstdlib>
#include <iostream>
#include <sstream>
// Other libraries' .h files.
// Your project's .h files.
#define FST_CHECK(a) \
if (!(a)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK failed: " #a; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_EQ(a, b) \
if ((a) != (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_EQ failed: " #a " != " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_NE(a, b) \
if ((a) == (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_NE failed: " #a " == " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_GT(a, b) \
if ((a) <= (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_GT failed: " #a " <= " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_GE(a, b) \
if ((a) < (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_GE failed: " #a " < " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_LT(a, b) \
if ((a) >= (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_LT failed: " #a " >= " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
#define FST_CHECK_LE(a, b) \
if ((a) > (b)) [[unlikely]] { \
std::ostringstream oss; \
oss << "FST_CHECK_LE failed: " #a " > " #b; \
oss << " (" << (a) << " vs. " << (b) << ")"; \
const auto e = oss.str(); \
std::cerr << e << std::endl; \
std::abort(); \
}
// We turn on all DCHECKs to CHECKs temporarily for better safety.
#if 1
# define FST_DCHECK(a) FST_CHECK(a)
# define FST_DCHECK_EQ(a, b) FST_CHECK_EQ(a, b)
# define FST_DCHECK_NE(a, b) FST_CHECK_NE(a, b)
# define FST_DCHECK_GT(a, b) FST_CHECK_GT(a, b)
# define FST_DCHECK_GE(a, b) FST_CHECK_GE(a, b)
# define FST_DCHECK_LT(a, b) FST_CHECK_LT(a, b)
# define FST_DCHECK_LE(a, b) FST_CHECK_LE(a, b)
#else
# define FST_DCHECK(a)
# define FST_DCHECK_EQ(a, b)
# define FST_DCHECK_NE(a, b)
# define FST_DCHECK_GT(a, b)
# define FST_DCHECK_GE(a, b)
# define FST_DCHECK_LT(a, b)
# define FST_DCHECK_LE(a, b)
#endif
// Compatibility layer for unreachable code hint
#if defined(__cplusplus) && __cplusplus >= 202302L
# include <utility>
# define FST_UNREACHABLE std::unreachable()
#elif USE_GCC_INTRINSIC
# define FST_UNREACHABLE __builtin_unreachable()
// TODO: implement MSVC version
// #elif USE_MSVC_INTRINSIC
#else
# define FST_UNREACHABLE std::abort()
#endif

View File

@ -0,0 +1,83 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
// C system headers
// C++ standard library headers
#include <cstdint>
// Other libraries' .h files.
// Your project's .h files.
namespace fst {
// Original block types from fstapi.h
// FST_BL_HDR = 0,
// FST_BL_VCDATA = 1,
// FST_BL_BLACKOUT = 2,
// FST_BL_GEOM = 3,
// FST_BL_HIER = 4,
// FST_BL_VCDATA_DYN_ALIAS = 5,
// FST_BL_HIER_LZ4 = 6,
// FST_BL_HIER_LZ4DUO = 7,
// FST_BL_VCDATA_DYN_ALIAS2 = 8,
// FST_BL_ZWRAPPER = 254,
// FST_BL_SKIP = 255
enum class BlockType : uint8_t {
HEADER = 0,
WAVE_DATA_VERSION1 = 1, // not implemented
BLACKOUT = 2,
GEOMETRY = 3,
HIERARCHY_GZ_COMPRESSED = 4, // not implemented
WAVE_DATA_VERSION2 = 5, // not implemented
HIERARCHY_LZ4_COMPRESSED = 6,
HIERARCHY_LZ4_COMPRESSED_TWICE = 7, // not implemented
WAVE_DATA_VERSION3 = 8,
ZWRAPPER = 254, // not implemented
SKIP = 255 // not implemented
};
constexpr unsigned kSharedBlockHeaderSize = 1 /* BlockType */ + 8 /* size (u64) */;
struct HeaderInfo {
struct Size {
static constexpr unsigned start_time = 0;
static constexpr unsigned end_time = 8;
static constexpr unsigned real_endianness = 8;
static constexpr unsigned writer_memory_use = 8;
static constexpr unsigned num_scopes = 8;
static constexpr unsigned num_vars = 8;
static constexpr unsigned num_handles = 8;
static constexpr unsigned num_wave_data_blocks = 8;
static constexpr unsigned timescale = 1;
static constexpr unsigned writer = 128;
static constexpr unsigned date = 26;
static constexpr unsigned reserved = 93;
static constexpr unsigned filetype = 1;
static constexpr unsigned timezero = 8;
};
struct Offset {
static constexpr unsigned start_time = 0;
static constexpr unsigned end_time = start_time + Size::end_time;
static constexpr unsigned real_endianness = end_time + Size::real_endianness;
static constexpr unsigned writer_memory_use = real_endianness + Size::writer_memory_use;
static constexpr unsigned num_scopes = writer_memory_use + Size::num_scopes;
static constexpr unsigned num_vars = num_scopes + Size::num_vars;
static constexpr unsigned num_handles = num_vars + Size::num_vars;
static constexpr unsigned num_wave_data_blocks = num_handles + Size::num_handles;
static constexpr unsigned timescale = num_wave_data_blocks + Size::num_wave_data_blocks;
static constexpr unsigned writer = timescale + Size::timescale;
static constexpr unsigned date = writer + Size::writer;
static constexpr unsigned reserved = date + Size::date;
static constexpr unsigned filetype = reserved + Size::reserved;
static constexpr unsigned timezero = filetype + Size::filetype;
};
static constexpr unsigned total_size = Offset::timezero + Size::timezero;
static constexpr double kEndianessMagicIdentifier = 2.7182818284590452354;
static_assert(total_size == 321, "Total size of HeaderInfo must be 321 bytes");
};
} // namespace fst

View File

@ -0,0 +1,388 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
// C system headers
// C++ standard library headers
#if defined(__cplusplus) && __cplusplus >= 202302L
# include <bit>
#endif
#include <cstdint>
#include <cstring>
#include <vector>
// Other libraries' .h files.
// Your project's .h files.
#include "fstcpp/fstcpp.h"
#include "fstcpp/fstcpp_file.h"
namespace fst {
namespace platform {
// For C++14
// Can remove once C++23 is required
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
// clang-format off
template <typename U> U to_big_endian(U u) { return u; }
#else
#if defined(__cplusplus) && __cplusplus >= 202302L
template <typename U, size_t S>
U to_big_endian(U u, std::integral_constant<size_t, S>) {
return std::byteswap(u);
}
#elif USE_GCC_INTRINSIC
template<typename U> U to_big_endian(U u, std::integral_constant<size_t, 1>) { return u; }
template<typename U> U to_big_endian(U u, std::integral_constant<size_t, 2>) { return __builtin_bswap16(u); }
template<typename U> U to_big_endian(U u, std::integral_constant<size_t, 4>) { return __builtin_bswap32(u); }
template<typename U> U to_big_endian(U u, std::integral_constant<size_t, 8>) { return __builtin_bswap64(u); }
// TODO: implement MSVC version
// #elif USE_MSVC_INTRINSIC
#else
template <typename U, size_t S>
U to_big_endian(U u, std::integral_constant<size_t, S>) {
U ret{0};
for (size_t i = 0; i < S; ++i) {
ret = (ret << 8) | (u & 0xff);
u >>= 8;
}
return ret;
}
#endif
// clang-format on
template <typename U>
U to_big_endian(U u) {
return platform::to_big_endian(u, std::integral_constant<size_t, sizeof(U)>());
}
#endif
} // namespace platform
struct StreamWriteHelper {
std::ostream *m_os{nullptr};
StreamWriteHelper(std::ostream &os_) : m_os{&os_} {}
StreamWriteHelper(std::ostream *os_) : m_os{os_} {}
// Write the entire uint, big-endian
// We do not provide little-endian version since FST only uses big-endian
template <typename U>
StreamWriteHelper &writeUInt(U u) {
u = platform::to_big_endian(u);
m_os->write(reinterpret_cast<const char *>(&u), sizeof(u));
return *this;
}
// Write the uint, big-endian, left-aligned but only (bitwidth+7)/8 bytes
// This is a very special case for value changes
// For example, if the value is 10-bits (e.g. logic [9:0] in Verilog),
// then the first byte will be [9-:8], then {[1:0], 6'b0}.
template <typename U>
StreamWriteHelper &writeUIntPartialForValueChange(U u, size_t bitwidth) {
// Shift left to align the MSB to the MSB of the uint
u <<= sizeof(u) * 8 - bitwidth;
// Write the first (bitwidth+7)/8 bytes
u = platform::to_big_endian(u);
m_os->write(reinterpret_cast<const char *>(&u), (bitwidth + 7) / 8);
return *this;
}
StreamWriteHelper &writeLEB128(uint64_t v) {
// Just reuse the logic from fstapi.c, is there a better way?
uint64_t nxt{0};
unsigned char buf[10]{}; /* ceil(64/7) = 10 */
unsigned char *pnt{buf};
int len{0};
while ((nxt = v >> 7)) {
*(pnt++) = ((unsigned char)v) | 0x80;
v = nxt;
}
*(pnt++) = (unsigned char)v;
len = static_cast<int>(pnt - buf);
m_os->write(reinterpret_cast<const char *>(buf), len);
return *this;
}
StreamWriteHelper &writeLEB128Signed(int64_t v) {
// Just reuse the logic from fstapi.c, is there a better way?
unsigned char buf[15]{}; /* ceil(64/7) = 10 + sign byte padded way up */
unsigned char byt{0};
unsigned char *pnt{buf};
int more{1};
int len{0};
do {
byt = static_cast<unsigned char>(v | 0x80);
v >>= 7;
if (((!v) && (!(byt & 0x40))) || ((v == -1) && (byt & 0x40))) {
more = 0;
byt &= 0x7f;
}
*(pnt++) = byt;
} while (more);
len = static_cast<int>(pnt - buf);
m_os->write(reinterpret_cast<const char *>(buf), len);
return *this;
}
template <typename F>
StreamWriteHelper &writeFloat(F f) {
// Always write in native endianness
m_os->write(reinterpret_cast<const char *>(&f), sizeof(f));
return *this;
}
StreamWriteHelper &writeBlockHeader(fst::BlockType block_type, uint64_t block_length) {
return (
this //
->writeUInt(static_cast<uint8_t>(block_type))
.writeUInt(
block_length + 8
) // The 8 is required by FST, which is the size of this uint64_t
);
}
// Write the string, non-null-terminated
StreamWriteHelper &writeString(const fst::string_view_pair str) {
m_os->write(str.m_data, str.m_size);
return *this;
}
// Write the string, null-terminated
StreamWriteHelper &writeString0(const fst::string_view_pair str) {
m_os->write(str.m_data, str.m_size).put('\0');
return *this;
}
StreamWriteHelper &writeString(const std::string &str) {
return writeString0(fst::make_string_view_pair(str.c_str(), str.size()));
}
StreamWriteHelper &writeString(const char *str) {
return writeString0(fst::make_string_view_pair(str));
}
StreamWriteHelper &write(const char *ptr, size_t size) {
m_os->write(ptr, size);
return *this;
}
StreamWriteHelper &write(const uint8_t *ptr, size_t size) {
m_os->write(reinterpret_cast<const char *>(ptr), size);
return *this;
}
StreamWriteHelper &seek(std::streamoff pos, std::ios_base::seekdir dir) {
m_os->seekp(pos, dir);
return *this;
}
StreamWriteHelper &fill(char fill_char, size_t size) {
if (size > 32) {
// optimize large fills
constexpr unsigned s_kChunkSize = 16;
char buf[s_kChunkSize]{};
std::memset(buf, fill_char, s_kChunkSize);
for (size_t i{0}; i < size / s_kChunkSize; ++i) {
m_os->write(buf, s_kChunkSize);
}
size %= s_kChunkSize;
}
for (size_t i{0}; i < size; ++i) {
m_os->put(fill_char);
}
return *this;
}
// Handy functions for writing variable length data, you can
// cascade multiple write() calls after RecordOffset(), then
// call DiffOffset() to get the total number of bytes written.
// (1)
// std::streamoff diff;
// h
// .beginOffset(diff)
// .write(...)
// ... do other stuff ...
// .endOffset(&diff); <-- diff will be set to the number of bytes written
// (2)
// std::streamoff pos, diff;
// h
// .beginOffset(pos)
// .write(...)
// ... do other stuff ...
// .endOffset(&diff, pos); <-- diff will be set to the number of bytes written
// The API uses pointer on purpose to prevent you pass (pos, diff) as arguments
// to endOffset(), which is a common mistake.
StreamWriteHelper &beginOffset(std::streamoff &pos) {
pos = m_os->tellp();
return *this;
}
StreamWriteHelper &endOffset(std::streamoff *diff) {
// diff shall store previous position before calling this function
*diff = m_os->tellp() - *diff;
return *this;
}
StreamWriteHelper &endOffset(std::streamoff *diff, std::streamoff pos) {
*diff = m_os->tellp() - pos;
return *this;
}
};
struct StreamVectorWriteHelper {
std::vector<uint8_t> &m_vec;
StreamVectorWriteHelper(std::vector<uint8_t> &vec_) : m_vec{vec_} {}
template <typename T>
StreamVectorWriteHelper &write(T u) {
const size_t s = sizeof(u);
m_vec.resize(m_vec.size() + s);
std::memcpy(m_vec.data() + m_vec.size() - s, &u, s);
return *this;
}
template <typename T>
StreamVectorWriteHelper &fill(T u, size_t count) {
const size_t s = sizeof(u) * count;
m_vec.resize(m_vec.size() + s);
for (size_t i{0}; i < count; ++i) {
std::memcpy(m_vec.data() + m_vec.size() - s + i * sizeof(u), &u, sizeof(u));
}
return *this;
}
template <typename T>
StreamVectorWriteHelper &write(T *u, size_t size) {
const size_t s = sizeof(u) * size;
m_vec.resize(m_vec.size() + s);
std::memcpy(m_vec.data() + m_vec.size() - s, u, s);
return *this;
}
template <typename E>
StreamVectorWriteHelper &writeU8Enum(E e) {
m_vec.push_back(static_cast<uint8_t>(e));
return *this;
}
// Write the entire uint, big-endian
// We do not provide little-endian version since FST only uses big-endian
template <typename U>
StreamVectorWriteHelper &writeUIntBE(U u) {
u = platform::to_big_endian(u);
const size_t s = sizeof(u);
m_vec.resize(m_vec.size() + s);
std::memcpy(m_vec.data() + m_vec.size() - s, &u, s);
return *this;
}
// Write the uint, big-endian, left-aligned but only (bitwidth+7)/8 bytes
// This is a very special case for value changes
// For example, if the value is 10-bits (e.g. logic [9:0] in Verilog),
// then the first byte will be [9-:8], then {[1:0], 6'b0}.
template <typename U>
StreamVectorWriteHelper &writeUIntPartialForValueChange(U u, size_t bitwidth) {
// Shift left to align the MSB to the MSB of the uint
u <<= sizeof(u) * 8 - bitwidth;
// Write the first (bitwidth+7)/8 bytes
u = platform::to_big_endian(u);
const size_t s = (bitwidth + 7) / 8;
m_vec.resize(m_vec.size() + s);
std::memcpy(m_vec.data() + m_vec.size() - s, &u, s);
return *this;
}
StreamVectorWriteHelper &writeLEB128(uint64_t v) {
// Just reuse the logic from fstapi.c, is there a better way?
uint64_t nxt{0};
unsigned char buf[10]{}; /* ceil(64/7) = 10 */
unsigned char *pnt{buf};
int len{0};
while ((nxt = v >> 7)) {
*(pnt++) = ((unsigned char)v) | 0x80;
v = nxt;
}
*(pnt++) = (unsigned char)v;
len = static_cast<int>(pnt - buf);
const size_t cur = m_vec.size();
m_vec.resize(cur + len);
std::memcpy(m_vec.data() + cur, buf, len);
return *this;
}
StreamVectorWriteHelper &writeLEB128Signed(int64_t v) {
// Just reuse the logic from fstapi.c, is there a better way?
unsigned char buf[15]{}; /* ceil(64/7) = 10 + sign byte padded way up */
unsigned char byt{0};
unsigned char *pnt{buf};
int more{1};
int len{0};
do {
byt = static_cast<unsigned char>(v | 0x80);
v >>= 7;
if (((!v) && (!(byt & 0x40))) || ((v == -1) && (byt & 0x40))) {
more = 0;
byt &= 0x7f;
}
*(pnt++) = byt;
} while (more);
len = static_cast<int>(pnt - buf);
const size_t cur = m_vec.size();
m_vec.resize(cur + len);
std::memcpy(m_vec.data() + cur, buf, len);
return *this;
}
StreamVectorWriteHelper &writeBlockHeader(fst::BlockType block_type, uint64_t block_length) {
return (
this //
->writeUIntBE(static_cast<uint8_t>(block_type))
.writeUIntBE(
block_length + 8
) // The 8 is required by FST, which is the size of this uint64_t
);
}
// Write the string, non-null-terminated
StreamVectorWriteHelper &writeString(const fst::string_view_pair str) {
if (str.m_size != 0) {
const size_t len = str.m_size;
const size_t cur = m_vec.size();
m_vec.resize(cur + len);
std::memcpy(m_vec.data() + cur, str.m_data, len);
}
return *this;
}
// Write the string, null-terminated
StreamVectorWriteHelper &writeString0(const fst::string_view_pair str) {
if (str.m_size != 0) {
const size_t len = str.m_size;
const size_t cur = m_vec.size();
m_vec.resize(cur + len + 1);
std::memcpy(m_vec.data() + cur, str.m_data, len);
m_vec[cur + len] = '\0';
} else {
m_vec.push_back('\0');
}
return *this;
}
StreamVectorWriteHelper &writeString(const std::string &str) {
return writeString0(fst::make_string_view_pair(str.c_str(), str.size()));
}
StreamVectorWriteHelper &writeString(const char *str) {
return writeString0(fst::make_string_view_pair(str));
}
};
} // namespace fst

View File

@ -0,0 +1,39 @@
// SPDX-FileCopyrightText: 2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
// direct include
#include "fstcpp/fstcpp_variable_info.h"
// C system headers
// C++ standard library headers
#include <algorithm>
// Other libraries' .h files.
// Your project's .h files.
namespace fst {
// I don't know why I need to define them here, but StackOverflow says it
constexpr uint64_t VariableInfo::kCapacityBaseShift;
constexpr uint64_t VariableInfo::kCapacityBase;
void VariableInfo::reallocate(uint64_t new_size) {
// Allocate new memory
const uint32_t new_capacity_log2{
std::max(
static_cast<uint32_t>(platform::clog2(new_size)),
static_cast<uint32_t>(kCapacityBaseShift)
) -
static_cast<uint32_t>(kCapacityBaseShift)
};
uint8_t *new_data{new uint8_t[kCapacityBase << new_capacity_log2]};
// Copy old data to new memory
if (m_data != nullptr) {
const uint64_t old_size{size()};
std::copy_n(m_data, old_size, new_data);
delete[] m_data;
}
m_data = new_data;
capacity_log2(new_capacity_log2);
}
} // namespace fst

View File

@ -0,0 +1,830 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
#include "fstcpp/fstcpp.h"
// C system headers
// C++ standard library headers
#if defined(__cplusplus) && __cplusplus >= 202002L
# include <bit>
#endif
#include <algorithm>
#include <cstdint>
#include <limits>
#include <vector>
// Other libraries' .h files.
// Your project's .h files.
#include "fstcpp/fstcpp_assertion.h"
#include "fstcpp/fstcpp_stream_write_helper.h"
namespace fst {
namespace platform {
// Can be replaced with std::bit_width when C++20 is available
inline uint64_t clog2(uint64_t x) {
if (x <= 1) return 0;
#if defined(__cplusplus) && __cplusplus >= 202002L
return std::bit_width(x - 1);
#elif USE_GCC_INTRINSIC
return 64 - __builtin_clzll(x - 1);
// TODO: implement MSVC version
// #elif USE_MSVC_INTRINSIC
#else
uint64_t r = 0;
x -= 1;
auto CheckAndShift = [&](uint64_t shift) {
if (x >> shift) {
r += shift;
x >>= shift;
}
};
CheckAndShift(32);
CheckAndShift(16);
CheckAndShift(8);
CheckAndShift(4);
CheckAndShift(2);
CheckAndShift(1);
r += x;
return r;
#endif
}
inline constexpr uint32_t gen_mask_safe(unsigned width) {
// works even when width == 32
return ((uint32_t(1) << (width - 1)) << 1) - 1;
}
inline uint32_t read_field(const uint32_t src, unsigned width, unsigned offset) {
const uint32_t mask = gen_mask_safe(width);
return (src >> offset) & mask;
}
inline void write_field(uint32_t &dst, const uint32_t src, unsigned width, unsigned offset) {
const uint32_t mask = gen_mask_safe(width) << offset;
dst = (dst & ~mask) | ((src << offset) & mask);
}
} // namespace platform
class VariableInfo final {
public:
static constexpr uint32_t kMaxSupportedBitwidth = 0x7fffff;
private:
static constexpr uint64_t kCapacityBaseShift = 5;
static constexpr uint64_t kCapacityBase = 1 << kCapacityBaseShift;
// To maximize cache efficiency, we compact the data members into 16 bytes.
// We make use of bitfields to store multiple pieces of information in a single integer.
// But standard does not guarantee the layout of bitfields (the `int x : N;` syntax),
// so we use helper functions to access bitfields.
// begin of data members
// 1. 8B pointer (assume 64-bit architecture), its size can be:
// - 0 if m_data is nullptr
// - `kCapacityBase * pow(2, m_capacity_log2)` if m_data is not nullptr
// - If we want more bits, we can use the `kCapacityBaseShift` LSB for other purposes.
uint8_t *m_data{nullptr};
// 2. 4B size. The same as vector.size(), but we only need 32b.
uint32_t m_size{0};
// 3. 4B misc. Highly compacted information for max cache efficiency.
// - 6b capacity_log2
// - 2b last_encoding_type
// - 23b bitwidth
// - 1b is_real
uint32_t m_misc{0};
// end of data members
// Note: optimization possibility (not implemented)
// - real is always 64-bit double, so we can use 24 bits to encode
// is_real and bitwidth together, and bitwidth = (1<<24-1) is a special
// value to indicate that the variable is a real.
// - Currently bitwidth is whatever you pass to Writer::createVar.
// - Not implemented since nobody needs 16M-bit over 8M-bit bitwidth IMO.
static constexpr uint32_t kIsRealWidth = 1;
static constexpr uint32_t kBitwidthWidth = 23;
static constexpr uint32_t kLastEncodingTypeWidth = 2;
static constexpr uint32_t kCapacityLog2Width = 6;
static constexpr uint32_t kIsRealOffset = 0;
static constexpr uint32_t kBitwidthOffset = kIsRealOffset + kIsRealWidth;
static constexpr uint32_t kLastEncodingTypeOffset = kBitwidthOffset + kBitwidthWidth;
static constexpr uint32_t kCapacityLog2Offset =
kLastEncodingTypeOffset + kLastEncodingTypeWidth;
void capacity_log2(uint32_t capacity_log2_) {
platform::write_field(m_misc, capacity_log2_, kCapacityLog2Width, kCapacityLog2Offset);
}
uint32_t capacity() const {
if (m_data == nullptr) {
return 0;
}
return kCapacityBase << platform::read_field(
m_misc, kCapacityLog2Width, kCapacityLog2Offset
);
}
bool need_reallocate(uint64_t new_size) const { return capacity() < new_size; }
// This function is cold, so we don't inline it
void reallocate(uint64_t new_size);
void size(uint64_t s) { m_size = static_cast<uint32_t>(s); }
public:
uint64_t size() const { return m_size; }
uint32_t bitwidth() const {
return platform::read_field(m_misc, kBitwidthWidth, kBitwidthOffset);
}
bool is_real() const { return bool(platform::read_field(m_misc, kIsRealWidth, kIsRealOffset)); }
void last_written_encode_type(EncodingType encoding_) {
platform::write_field(
m_misc,
static_cast<uint32_t>(encoding_),
kLastEncodingTypeWidth,
kLastEncodingTypeOffset
);
}
EncodingType last_written_encode_type() const {
return static_cast<EncodingType>(
platform::read_field(m_misc, kLastEncodingTypeWidth, kLastEncodingTypeOffset)
);
}
uint64_t last_written_bytes() const;
template <typename Callable, typename... Args>
auto dispatchHelper(Callable &&callable, Args &&...args) const;
VariableInfo(uint32_t bitwidth_, bool is_real_ = false);
~VariableInfo() {
if (data_ptr() != nullptr) {
// don't delete data directly for better abstraction
// we might use the LSB of data in the future as LSB is
// always aligned to kCapacityBase
delete[] data_ptr();
}
}
VariableInfo(VariableInfo &&rhs) {
m_data = rhs.m_data;
rhs.m_data = nullptr;
m_misc = rhs.m_misc;
m_size = rhs.m_size;
}
uint32_t emitValueChange(uint64_t current_time_index, const uint64_t val);
uint32_t emitValueChange(
uint64_t current_time_index, const uint32_t *val, EncodingType encoding
);
uint32_t emitValueChange(
uint64_t current_time_index, const uint64_t *val, EncodingType encoding
);
void keepOnlyTheLatestValue() {
const uint64_t last_written_bytes_ = last_written_bytes();
uint8_t *data_ptr_ = data_ptr();
std::copy_n(data_ptr_ + size() - last_written_bytes_, last_written_bytes_, data_ptr_);
size(last_written_bytes_);
}
void dumpInitialBits(std::vector<uint8_t> &buf) const;
void dumpValueChanges(std::vector<uint8_t> &buf) const;
// We only need to make this class compatible with vector
// delete copy constructor and assignment operator
VariableInfo(const VariableInfo &) = delete;
VariableInfo &operator=(const VariableInfo &) = delete;
VariableInfo &operator=(VariableInfo &&) = delete;
void resize(size_t new_size) {
if (need_reallocate(new_size)) {
reallocate(new_size);
}
size(new_size);
}
void add_size(size_t added_size) { resize(size() + added_size); }
uint8_t *data_ptr() { return m_data; }
};
static_assert(
sizeof(VariableInfo) != 12,
"We don't support 32-bit architecture, comment out the assertions and take the risk"
);
static_assert(sizeof(VariableInfo) == 16, "VariableInfo should be small");
namespace detail {
constexpr size_t kEmitTimeIndexAndEncodingSize = sizeof(uint64_t) + sizeof(fst::EncodingType);
// EmitReaderHelper and EmitWriterHelper are very optimized for emit functions
// User must ensure the pointer points to the valid memory region
struct EmitReaderHelper {
const uint8_t *ptr;
EmitReaderHelper(const uint8_t *ptr_) : ptr(ptr_) {}
std::pair<uint64_t, fst::EncodingType> readTimeIndexAndEncoding() {
const auto time_index = read<uint64_t>();
const auto encoding = read<fst::EncodingType>();
return std::make_pair(time_index, encoding);
}
template <typename T>
T read() {
const size_t s = sizeof(T);
T u;
std::memcpy(&u, ptr, s);
ptr += s;
return u;
}
void skip(size_t count) { ptr += count; }
template <typename T>
T peek(size_t i = 0) {
const size_t s = sizeof(T);
T u;
std::memcpy(&u, ptr + i * s, s);
return u;
}
};
struct EmitWriterHelper {
uint8_t *ptr;
EmitWriterHelper(uint8_t *ptr_) : ptr(ptr_) {}
EmitWriterHelper &writeTimeIndexAndEncoding(uint64_t time_index, fst::EncodingType encoding) {
write(time_index);
write(encoding);
return *this;
}
template <typename T>
EmitWriterHelper &write(T u) {
const size_t s = sizeof(u);
std::memcpy(ptr, &u, s);
ptr += s;
return *this;
}
template <typename T>
EmitWriterHelper &fill(T u, size_t count) {
for (size_t i = 0; i < count; ++i) {
std::memcpy(ptr, &u, sizeof(u));
ptr += sizeof(u);
}
return *this;
}
template <typename T>
EmitWriterHelper &write(T *u, size_t size) {
for (size_t i = 0; i < size; ++i) {
std::memcpy(ptr, u + i, sizeof(T));
ptr += sizeof(T);
}
return *this;
}
};
class VariableInfoDouble {
VariableInfo &info;
public:
VariableInfoDouble(VariableInfo &info_) : info(info_) {}
public:
inline size_t computeBytesNeeded(EncodingType encoding) const {
(void)encoding;
return kEmitTimeIndexAndEncodingSize + sizeof(double);
}
inline EmitWriterHelper emitValueChangeCommonPart(
uint64_t current_time_index, EncodingType encoding
) {
if (current_time_index + 1 == 0) {
info.resize(0);
}
// For Double, value is always 8 bytes (sizeof(double) or uint64_t)
const size_t added_size = computeBytesNeeded(encoding);
const size_t old_size = info.size();
info.add_size(added_size);
EmitWriterHelper wh(info.data_ptr() + old_size);
wh.writeTimeIndexAndEncoding(current_time_index, encoding);
return wh;
}
public:
void construct() {
const size_t needed = computeBytesNeeded(EncodingType::BINARY);
info.resize(needed);
EmitWriterHelper wh(info.data_ptr());
const double nan_val = std::numeric_limits<double>::quiet_NaN();
uint64_t nan_val_u64;
std::memcpy(&nan_val_u64, &nan_val, sizeof(nan_val_u64));
wh.writeTimeIndexAndEncoding(0, EncodingType::BINARY).write<uint64_t>(nan_val_u64);
}
void emitValueChange(uint64_t current_time_index, const uint64_t val) {
auto wh = emitValueChangeCommonPart(current_time_index, EncodingType::BINARY);
// Note, do not use write<double> here since the uint64_t is
// already bit_cast'ed from double
wh.write<uint64_t>(val);
}
// Double variables should not use these array-based emitValueChange overloads.
// We implement them to satisfy the VairableInfo::dispatchHelper template instantiation.
void emitValueChange(uint64_t, const uint32_t *, EncodingType) {
throw std::runtime_error("emitValueChange(uint32_t*) not supported for Double");
}
void emitValueChange(uint64_t, const uint64_t *, EncodingType) {
throw std::runtime_error("emitValueChange(uint64_t*) not supported for Double");
}
void dumpInitialBits(std::vector<uint8_t> &buf) const {
FST_DCHECK_GT(info.size(), kEmitTimeIndexAndEncodingSize);
EmitReaderHelper rh(info.data_ptr());
StreamVectorWriteHelper wh(buf);
(void)rh.readTimeIndexAndEncoding();
auto v = rh.read<double>();
wh.write<double>(v);
}
void dumpValueChanges(std::vector<uint8_t> &buf) const {
StreamVectorWriteHelper wh(buf);
EmitReaderHelper rh(info.data_ptr());
const uint8_t *tail = info.data_ptr() + info.size();
bool first = true;
uint64_t prev_time_index = 0;
while (true) {
if (rh.ptr == tail) break;
FST_CHECK_GT(tail, rh.ptr);
const auto time_index = rh.read<uint64_t>();
const auto enc = rh.read<EncodingType>();
const auto num_byte = sizeof(double);
if (first) {
// Note: [0] is initial value, which is already dumped in dumpInitialBits()
first = false;
} else {
FST_CHECK(enc == EncodingType::BINARY);
const uint64_t delta_time_index = time_index - prev_time_index;
prev_time_index = time_index;
// Double shall be treated as non-binary
const bool has_non_binary = true;
wh //
.writeLEB128((delta_time_index << 1) | has_non_binary)
.write<double>(rh.peek<double>());
}
rh.skip(num_byte);
}
}
};
template <typename T>
class VariableInfoScalarInt {
VariableInfo &info;
public:
VariableInfoScalarInt(VariableInfo &info_) : info(info_) {}
public:
size_t computeBytesNeeded(EncodingType encoding) const {
return kEmitTimeIndexAndEncodingSize + sizeof(T) * bitPerEncodedBit(encoding);
}
// The returning address points to the first byte of the value
EmitWriterHelper emitValueChangeCommonPart(uint64_t current_time_index, EncodingType encoding) {
if (current_time_index + 1 == 0) {
// This is the first value change, we need to remove everything
// and then add the new value
info.resize(0);
}
const size_t added_size = computeBytesNeeded(encoding);
const size_t old_size = info.size();
info.add_size(added_size);
EmitWriterHelper wh(info.data_ptr() + old_size);
wh.writeTimeIndexAndEncoding(current_time_index, encoding);
return wh;
}
public:
void construct() {
info.resize(computeBytesNeeded(EncodingType::VERILOG));
EmitWriterHelper wh(info.data_ptr());
wh.writeTimeIndexAndEncoding(0, EncodingType::VERILOG).write(T(0)).write(T(-1));
}
void emitValueChange(uint64_t current_time_index, const uint64_t val) {
auto wh = emitValueChangeCommonPart(current_time_index, EncodingType::BINARY);
wh.template write<T>(val);
}
void emitValueChange(uint64_t current_time_index, const uint32_t *val, EncodingType encoding) {
auto wh = emitValueChangeCommonPart(current_time_index, encoding);
for (unsigned i = 0; i < bitPerEncodedBit(encoding); ++i) {
// C++17: replace this with if constexpr
if (sizeof(T) == 8) {
uint64_t v = val[1]; // high bits
v <<= 32;
v |= val[0]; // low bits
wh.template write<uint64_t>(v);
val += 2;
} else {
wh.template write<T>(val[0]);
val += 1;
}
}
}
void emitValueChange(uint64_t current_time_index, const uint64_t *val, EncodingType encoding) {
auto wh = emitValueChangeCommonPart(current_time_index, encoding);
for (unsigned i = 0; i < bitPerEncodedBit(encoding); ++i) {
wh.template write<T>(val[i]);
}
}
void dumpInitialBits(std::vector<uint8_t> &buf) const {
// FST requires initial bits present
FST_DCHECK_GT(info.size(), kEmitTimeIndexAndEncodingSize);
EmitReaderHelper rh(info.data_ptr());
const auto time_index_enc = rh.readTimeIndexAndEncoding();
const auto enc = time_index_enc.second;
const auto bitwidth = info.bitwidth();
switch (enc) {
case EncodingType::BINARY: {
auto v0 = rh.read<T>();
for (unsigned i = bitwidth; i-- > 0;) {
const char c = ((v0 >> i) & T(1)) ? '1' : '0';
buf.push_back(c);
}
break;
}
case EncodingType::VERILOG: {
auto v0 = rh.read<T>();
auto v1 = rh.read<T>();
for (unsigned i = bitwidth; i-- > 0;) {
const T b1 = ((v1 >> i) & T(1));
const T b0 = ((v0 >> i) & T(1));
const char c = kEncodedBitToCharTable[(b1 << 1) | b0];
buf.push_back(c);
}
break;
}
// Not supporting VHDL now
// LCOV_EXCL_START
default:
case EncodingType::VHDL: {
auto v0 = rh.read<T>();
auto v1 = rh.read<T>();
auto v2 = rh.read<T>();
for (unsigned i = bitwidth; i-- > 0;) {
const T b2 = ((v2 >> i) & T(1));
const T b1 = ((v1 >> i) & T(1));
const T b0 = ((v0 >> i) & T(1));
const char c = kEncodedBitToCharTable[(b2 << 2) | (b1 << 1) | b0];
buf.push_back(c);
}
break;
}
}
// LCOV_EXCL_STOP
}
void dumpValueChanges(std::vector<uint8_t> &buf) const {
StreamVectorWriteHelper h(buf);
EmitReaderHelper rh(info.data_ptr());
const uint8_t *tail = info.data_ptr() + info.size();
const auto bitwidth = info.bitwidth();
bool first = true;
uint64_t prev_time_index = 0;
if (bitwidth == 1) {
while (true) {
if (rh.ptr == tail) {
break;
}
FST_DCHECK_GT(tail, rh.ptr);
const auto time_index = rh.read<uint64_t>();
const auto enc = rh.read<EncodingType>();
const auto num_element = bitPerEncodedBit(enc);
const auto num_byte = num_element * sizeof(T);
if (first) {
// Note: [0] is initial value, which is already dumped in dumpInitialBits()
first = false;
} else {
unsigned val = 0;
for (unsigned i = 0; i < num_element; ++i) {
val |= rh.peek<T>(i);
}
uint64_t delta_time_index = time_index - prev_time_index;
prev_time_index = time_index;
switch (val) {
// clang-format off
case 0: delta_time_index = (delta_time_index<<2) | (0<<1) | 0; break; // '0'
case 1: delta_time_index = (delta_time_index<<2) | (1<<1) | 0; break; // '1'
case 2: delta_time_index = (delta_time_index<<4) | (0<<1) | 1; break; // 'X'
case 3: delta_time_index = (delta_time_index<<4) | (1<<1) | 1; break; // 'Z'
// Not supporting VHDL now
// LCOV_EXCL_START
case 4: delta_time_index = (delta_time_index<<4) | (2<<1) | 1; break; // 'H'
case 5: delta_time_index = (delta_time_index<<4) | (3<<1) | 1; break; // 'U'
case 6: delta_time_index = (delta_time_index<<4) | (4<<1) | 1; break; // 'W'
case 7: delta_time_index = (delta_time_index<<4) | (5<<1) | 1; break; // 'L'
case 8: delta_time_index = (delta_time_index<<4) | (6<<1) | 1; break; // '-'
case 9: delta_time_index = (delta_time_index<<4) | (7<<1) | 1; break; // '?'
default: break;
// LCOV_EXCL_STOP
// clang-format on
}
h.writeLEB128(delta_time_index);
}
rh.skip(num_byte);
}
} else {
while (true) {
if (rh.ptr == tail) {
break;
}
FST_CHECK_GT(tail, rh.ptr);
const auto time_index = rh.read<uint64_t>();
const auto enc = rh.read<EncodingType>();
const auto num_element = bitPerEncodedBit(enc);
const auto num_byte = num_element * sizeof(T);
if (first) {
first = false;
} else {
FST_CHECK(enc == EncodingType::BINARY); // TODO
const bool has_non_binary = enc != EncodingType::BINARY;
const uint64_t delta_time_index = time_index - prev_time_index;
prev_time_index = time_index;
h //
.writeLEB128((delta_time_index << 1) | has_non_binary)
.writeUIntPartialForValueChange(rh.peek<T>(), bitwidth);
}
rh.skip(num_byte);
}
}
}
};
class VariableInfoLongInt {
VariableInfo &info;
unsigned num_words() const { return (info.bitwidth() + 63) / 64; }
public:
VariableInfoLongInt(VariableInfo &info_) : info(info_) {}
public:
size_t computeBytesNeeded(EncodingType encoding) const {
return (
kEmitTimeIndexAndEncodingSize +
num_words() * sizeof(uint64_t) * bitPerEncodedBit(encoding)
);
}
EmitWriterHelper emitValueChangeCommonPart(uint64_t current_time_index, EncodingType encoding) {
if (current_time_index + 1 == 0) {
info.resize(0);
}
const size_t added_size = computeBytesNeeded(encoding);
const size_t old_size = info.size();
info.add_size(added_size);
EmitWriterHelper wh(info.data_ptr() + old_size);
wh.writeTimeIndexAndEncoding(current_time_index, encoding);
return wh;
}
public:
void construct() {
const size_t nw = num_words();
info.resize(computeBytesNeeded(EncodingType::VERILOG));
EmitWriterHelper wh(info.data_ptr());
wh //
.writeTimeIndexAndEncoding(0, EncodingType::VERILOG)
.fill(uint64_t(0), nw)
.fill(uint64_t(-1), nw);
}
void emitValueChange(uint64_t current_time_index, const uint64_t val) {
const unsigned nw = num_words();
auto wh = emitValueChangeCommonPart(current_time_index, EncodingType::BINARY);
wh.write(val).fill(uint64_t(0), nw - 1);
}
void emitValueChange(uint64_t current_time_index, const uint32_t *val, EncodingType encoding) {
const unsigned nw32 = (info.bitwidth() + 31) / 32;
const unsigned bpb = bitPerEncodedBit(encoding);
auto wh = emitValueChangeCommonPart(current_time_index, encoding);
for (unsigned i = 0; i < bpb; ++i) {
for (unsigned j = 0; j < nw32 / 2; ++j) {
uint64_t v = val[1]; // high bits
v <<= 32;
v |= val[0]; // low bits
wh.write(v);
val += 2;
}
if (nw32 % 2 != 0) {
uint64_t v = val[0];
wh.write(v);
val += 1;
}
}
}
void emitValueChange(uint64_t current_time_index, const uint64_t *val, EncodingType encoding) {
const unsigned nw_encoded = num_words() * bitPerEncodedBit(encoding);
auto wh = emitValueChangeCommonPart(current_time_index, encoding);
wh.write(val, nw_encoded);
}
void dumpInitialBits(std::vector<uint8_t> &buf) const {
FST_DCHECK_GT(info.size(), kEmitTimeIndexAndEncodingSize);
EmitReaderHelper rh(info.data_ptr());
const auto time_index_enc = rh.readTimeIndexAndEncoding();
const auto enc = time_index_enc.second;
const unsigned nw = num_words();
switch (enc) {
case EncodingType::BINARY: {
for (unsigned word_index = nw; word_index-- > 0;) {
const uint64_t v0 = rh.peek<uint64_t>(word_index);
const unsigned num_bit =
(word_index * 64 + 64 > info.bitwidth()) ? (info.bitwidth() % 64) : 64;
for (unsigned bit_index = num_bit; bit_index-- > 0;) {
const char c = ((v0 >> bit_index) & uint64_t(1)) ? '1' : '0';
buf.push_back(c);
}
}
break;
}
case EncodingType::VERILOG: {
for (unsigned word_index = nw; word_index-- > 0;) {
const uint64_t v0 = rh.peek<uint64_t>(nw * 0 + word_index);
const uint64_t v1 = rh.peek<uint64_t>(nw * 1 + word_index);
const unsigned num_bit =
(word_index * 64 + 64 > info.bitwidth()) ? (info.bitwidth() % 64) : 64;
for (unsigned bit_index = num_bit; bit_index-- > 0;) {
const bool b0 = ((v0 >> bit_index) & uint64_t(1));
const bool b1 = ((v1 >> bit_index) & uint64_t(1));
const char c = kEncodedBitToCharTable[(b1 << 1) | b0];
buf.push_back(c);
}
}
break;
}
default:
case EncodingType::VHDL: {
// Not supporting VHDL now
// LCOV_EXCL_START
for (unsigned word_index = nw; word_index-- > 0;) {
const uint64_t v0 = rh.peek<uint64_t>(nw * 0 + word_index);
const uint64_t v1 = rh.peek<uint64_t>(nw * 1 + word_index);
const uint64_t v2 = rh.peek<uint64_t>(nw * 2 + word_index);
const unsigned num_bit =
(word_index * 64 + 64 > info.bitwidth()) ? (info.bitwidth() % 64) : 64;
for (unsigned bit_index = num_bit; bit_index-- > 0;) {
const bool b0 = ((v0 >> bit_index) & uint64_t(1));
const bool b1 = ((v1 >> bit_index) & uint64_t(1));
const bool b2 = ((v2 >> bit_index) & uint64_t(1));
const char c = kEncodedBitToCharTable[(b2 << 2) | (b1 << 1) | b0];
buf.push_back(c);
}
}
break;
// LCOV_EXCL_STOP
}
rh.skip(sizeof(uint64_t) * nw * bitPerEncodedBit(enc));
}
}
void dumpValueChanges(std::vector<uint8_t> &buf) const {
StreamVectorWriteHelper h(buf);
EmitReaderHelper rh(info.data_ptr());
const uint8_t *tail = info.data_ptr() + info.size();
const unsigned nw = num_words();
const unsigned bitwidth = info.bitwidth(); // Local copy for lambda capture/usage if needed
bool first = true;
uint64_t prev_time_index = 0;
while (true) {
if (rh.ptr == tail) break;
FST_DCHECK_GT(tail, rh.ptr);
const auto time_index = rh.read<uint64_t>();
const auto enc = rh.read<EncodingType>();
const auto num_element = bitPerEncodedBit(enc);
const auto num_byte = num_element * nw * sizeof(uint64_t);
if (first) {
// Note: [0] is initial value, which is already dumped in dumpInitialBits()
first = false;
} else {
FST_CHECK(enc == EncodingType::BINARY); // TODO
const bool has_non_binary = enc != EncodingType::BINARY;
const uint64_t delta_time_index = time_index - prev_time_index;
prev_time_index = time_index;
h.writeLEB128((delta_time_index << 1) | has_non_binary);
if (bitwidth % 64 != 0) {
const unsigned remaining = bitwidth % 64;
uint64_t hi64 = rh.peek<uint64_t>(nw - 1);
// write from nw-1 to 1
for (unsigned j = nw - 1; j > 0; --j) {
uint64_t lo64 = rh.peek<uint64_t>(j - 1);
h.writeUIntBE((hi64 << (64 - remaining)) | (lo64 >> remaining));
hi64 = lo64;
}
// write 0
h.writeUIntPartialForValueChange(hi64, remaining);
} else {
// write from nw-1 to 0
for (unsigned j = nw; j-- > 0;) {
h.writeUIntBE(rh.peek<uint64_t>(j));
}
}
}
rh.skip(num_byte);
}
}
};
} // namespace detail
template <typename Callable, typename... Args>
auto VariableInfo::dispatchHelper(Callable &&callable, Args &&...args) const {
const uint32_t bitwidth = this->bitwidth();
const bool is_real = this->is_real();
if (!is_real) {
// Decision: the branch miss is too expensive for large design, so we only use 3 types of
// int
if (bitwidth <= 8) {
return callable(
detail::VariableInfoScalarInt<uint8_t>(const_cast<VariableInfo &>(*this)),
std::forward<Args>(args)...
);
} else if (bitwidth <= 64) {
return callable(
detail::VariableInfoScalarInt<uint64_t>(const_cast<VariableInfo &>(*this)),
std::forward<Args>(args)...
);
} else {
return callable(
detail::VariableInfoLongInt(const_cast<VariableInfo &>(*this)),
std::forward<Args>(args)...
);
}
}
return callable(
detail::VariableInfoDouble(const_cast<VariableInfo &>(*this)), std::forward<Args>(args)...
);
}
inline VariableInfo::VariableInfo(uint32_t bitwidth_, bool is_real_) {
platform::write_field(m_misc, bitwidth_, kBitwidthWidth, kBitwidthOffset);
platform::write_field(m_misc, is_real_, kIsRealWidth, kIsRealOffset);
dispatchHelper([](auto obj) { obj.construct(); });
last_written_encode_type(EncodingType::BINARY);
}
inline uint32_t VariableInfo::emitValueChange(uint64_t current_time_index, const uint64_t val) {
const uint64_t old_size = size();
dispatchHelper([=](auto obj) { obj.emitValueChange(current_time_index, val); });
last_written_encode_type(EncodingType::BINARY);
return static_cast<uint32_t>(size() - old_size);
}
inline uint32_t VariableInfo::emitValueChange(
uint64_t current_time_index, const uint32_t *val, EncodingType encoding
) {
const uint64_t old_size = size();
dispatchHelper([=](auto obj) { obj.emitValueChange(current_time_index, val, encoding); });
last_written_encode_type(encoding);
return static_cast<uint32_t>(size() - old_size);
}
inline uint32_t VariableInfo::emitValueChange(
uint64_t current_time_index, const uint64_t *val, EncodingType encoding
) {
const uint64_t old_size = size();
dispatchHelper([=](auto obj) { obj.emitValueChange(current_time_index, val, encoding); });
last_written_encode_type(encoding);
return static_cast<uint32_t>(size() - old_size);
}
inline void VariableInfo::dumpInitialBits(std::vector<uint8_t> &buf) const {
dispatchHelper([&](auto obj) { obj.dumpInitialBits(buf); });
}
inline void VariableInfo::dumpValueChanges(std::vector<uint8_t> &buf) const {
dispatchHelper([&](auto obj) { obj.dumpValueChanges(buf); });
}
inline uint64_t VariableInfo::last_written_bytes() const {
const EncodingType encoding = last_written_encode_type();
return dispatchHelper([encoding](auto obj) { return obj.computeBytesNeeded(encoding); });
}
} // namespace fst

View File

@ -0,0 +1,891 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
// direct include
#include "fstcpp/fstcpp_writer.h"
// C system headers
// C++ standard library headers
#include <cstdio>
#include <cstring>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
// Other libraries' .h files.
#include <lz4.h>
#include <zlib.h>
// Your project's .h files.
#include "fstcpp/fstcpp.h"
#include "fstcpp/fstcpp_assertion.h"
#include "fstcpp/fstcpp_stream_write_helper.h"
#include "fstcpp/fstcpp_variable_info.h"
// AT(vec, x) is used to access vector at index x, and it will throw exception if out of bound
// in debug mode, but in release mode, it will not throw exception
// Usually you should only need AT(vec, x) only at very hot code path.
#ifndef NDEBUG
# define AT(vec, x) (vec.at(x))
#else
# define AT(vec, x) (vec[x])
#endif
namespace fst {
namespace detail {
void BlackoutData::emitDumpActive(uint64_t current_timestamp, bool enable) {
StreamVectorWriteHelper h(m_buffer);
h.writeUIntBE<uint8_t>(enable).writeLEB128(current_timestamp - m_previous_timestamp);
++m_count;
}
ValueChangeData::ValueChangeData() {
m_variable_infos.reserve(1024);
}
ValueChangeData::~ValueChangeData() = default;
void ValueChangeData::keepOnlyTheLatestValue() {
for (VariableInfo &v : m_variable_infos) {
v.keepOnlyTheLatestValue();
}
FST_CHECK(!m_timestamps.empty());
m_timestamps.front() = m_timestamps.back();
m_timestamps.resize(1);
}
} // namespace detail
void Writer::open(const string_view_pair name) {
FST_CHECK(!m_main_fst_file_.is_open());
m_main_fst_file_.open(std::string(name.m_data, name.m_size), std::ios::binary);
// reserve space for header, we will write it at Close(), append geometry and hierarchy at the
// end wave data will be flushed in between
m_main_fst_file_.seekp(kSharedBlockHeaderSize + HeaderInfo::total_size, std::ios_base::beg);
}
void Writer::close() {
if (!m_main_fst_file_.is_open()) return;
// Finalize header fields
if (m_header_.m_date[0] == '\0') {
// date is not set yet, set to the current date
setDate();
}
if (m_header_.m_start_time == kInvalidTime) {
m_header_.m_start_time = 0;
}
flushValueChangeData_(m_value_change_data_, m_main_fst_file_);
appendGeometry_(m_main_fst_file_);
appendHierarchy_(m_main_fst_file_);
appendBlackout_(m_main_fst_file_);
// Note: write header seek to 0, so we need to do
// this after all append operations
writeHeader_(m_header_, m_main_fst_file_);
m_main_fst_file_.close();
}
/////////////////////////////////////////
// Hierarchy / variable API
/////////////////////////////////////////
void Writer::setScope(
Hierarchy::ScopeType scopetype,
const string_view_pair scopename,
const string_view_pair scopecomp
) {
FST_CHECK(!m_hierarchy_finalized_);
StreamVectorWriteHelper h(m_hierarchy_buffer_);
h //
.writeU8Enum(Hierarchy::ScopeControlType::VCD_SCOPE)
.writeU8Enum(scopetype)
.writeString0(scopename)
.writeString0(scopecomp);
++m_header_.m_num_scopes;
}
void Writer::upscope() {
FST_CHECK(!m_hierarchy_finalized_);
// TODO: shall we inline it?
StreamVectorWriteHelper h(m_hierarchy_buffer_);
h.writeU8Enum(Hierarchy::ScopeControlType::VCD_UPSCOPE);
}
Handle Writer::createVar(
Hierarchy::VarType vartype,
Hierarchy::VarDirection vardir,
uint32_t bitwidth,
const string_view_pair name,
Handle alias_handle
) {
FST_CHECK(!m_hierarchy_finalized_);
FST_CHECK_LE(bitwidth, VariableInfo::kMaxSupportedBitwidth);
// write hierarchy entry: type, direction, name, length, alias
StreamVectorWriteHelper h(m_hierarchy_buffer_);
// determine real/string handling like original C implementation
bool is_real{false};
switch (vartype) {
case Hierarchy::VarType::VCD_REAL:
case Hierarchy::VarType::VCD_REAL_PARAMETER:
case Hierarchy::VarType::VCD_REALTIME:
case Hierarchy::VarType::SV_SHORTREAL:
is_real = true;
bitwidth = 8; // recast to double size
break;
case Hierarchy::VarType::GEN_STRING:
bitwidth = 0;
break;
default:
break;
}
if (alias_handle > m_header_.m_num_handles) {
// sanitize
alias_handle = 0;
}
const bool is_alias{alias_handle != 0};
// This counter is incremented whether alias || non-alias
++m_header_.m_num_vars;
if (!is_alias) {
// This counter is incremented only for non-alias variables
++m_header_.m_num_handles;
alias_handle = static_cast<uint32_t>(m_header_.m_num_handles);
}
h //
.writeU8Enum(vartype)
.writeU8Enum(vardir)
.writeString0(name)
.writeLEB128(bitwidth)
.writeLEB128(is_alias ? alias_handle : 0);
// If alias_handle == 0, we must allocate geom/valpos/curval entries and create a new handle
if (!is_alias) {
StreamVectorWriteHelper g(m_geometry_buffer_);
// I don't know why the original C implementation encode bitwidth again
const uint32_t geom_len{(bitwidth == 0 ? uint32_t(-1) : is_real ? uint32_t(0) : bitwidth)};
g.writeLEB128(geom_len);
m_value_change_data_.m_variable_infos.emplace_back(bitwidth, is_real);
}
return alias_handle;
}
// TODO
// LCOV_EXCL_START
// Handle Writer::createVar2(
// Hierarchy::VarType vartype,
// Hierarchy::VarDirection vardir,
// uint32_t bitwidth,
// const string_view_pair name,
// Handle alias_handle,
// const string_view_pair type,
// Hierarchy::SupplementalVarType svt,
// Hierarchy::SupplementalDataType sdt
// ) {
// FST_CHECK(!m_hierarchy_finalized_);
// (void)vartype;
// (void)vardir;
// (void)bitwidth;
// (void)name;
// (void)alias_handle;
// (void)type;
// (void)svt;
// (void)sdt;
// throw std::runtime_error("TODO");
// return 0;
// }
// LCOV_EXCL_STOP
/////////////////////////////////////////
// Waveform API
/////////////////////////////////////////
void Writer::emitTimeChange(uint64_t tim) {
finalizeHierarchy_();
if (m_value_change_data_usage_ > m_value_change_data_flush_threshold_ || m_flush_pending_) {
flushValueChangeData_(m_value_change_data_, m_main_fst_file_);
}
// Update header
m_header_.m_start_time = std::min(m_header_.m_start_time, tim);
m_header_.m_end_time = tim;
if (m_value_change_data_.m_timestamps.empty() ||
m_value_change_data_.m_timestamps.back() != tim) {
m_value_change_data_.m_timestamps.push_back(tim);
}
}
// TODO
// void Writer::emitDumpActive(bool enable) {
// // TODO: this API is not fully understood, need to check
// FST_CHECK(!m_value_change_data_.m_timestamps.empty());
// m_blackout_data_.emitDumpActive(m_value_change_data_.m_timestamps.back(), enable);
// }
template <typename... T>
void Writer::emitValueChangeHelper_(Handle handle, T &&...val) {
// Let data prefetch go first
VariableInfo &var_info = AT(m_value_change_data_.m_variable_infos, handle - 1);
#if defined(__GNUC__) || defined(__clang__)
__builtin_prefetch(var_info.data_ptr() + var_info.size() - 1, 1, 0);
#endif
finalizeHierarchy_();
// Original implementation: virtual, but vtable is too costly, we switch to if-else static
// dispatch
m_value_change_data_usage_ += var_info.emitValueChange(
m_value_change_data_.m_timestamps.size() - 1, std::forward<T>(val)...
);
}
void Writer::emitValueChange(Handle handle, const uint32_t *val, EncodingType encoding) {
emitValueChangeHelper_(handle, val, encoding);
}
void Writer::emitValueChange(Handle handle, const uint64_t *val, EncodingType encoding) {
emitValueChangeHelper_(handle, val, encoding);
}
void Writer::emitValueChange(Handle handle, uint64_t val) {
emitValueChangeHelper_(handle, val);
}
void Writer::emitValueChange(Handle handle, const char *val) {
finalizeHierarchy_();
VariableInfo &var_info = AT(m_value_change_data_.m_variable_infos, handle - 1);
// For double handles, const char* is interpreted as a double* (8B)
// This double shall be written out as raw IEEE 754 double
// So we just reinterpret_cast it to uint64_t and emit it
if (var_info.is_real()) {
emitValueChange(handle, *reinterpret_cast<const uint64_t *>(val));
return;
}
// For normal integer handles, const char* is "01xz..." (1B per bit)
const uint32_t bitwidth{var_info.bitwidth()};
FST_DCHECK_NE(bitwidth, 0);
val += bitwidth;
const unsigned num_words{(bitwidth + 63) / 64};
m_packed_value_buffer_.assign(num_words, 0);
for (unsigned i = 0; i < num_words; ++i) {
const char *start{val - std::min((i + 1) * 64, bitwidth)};
const char *end{val - 64 * i};
m_packed_value_buffer_[i] = 0;
for (const char *p = start; p < end; ++p) {
// No checking for invalid characters, follow original C implementation
m_packed_value_buffer_[i] <<= 1;
m_packed_value_buffer_[i] |= static_cast<uint64_t>(*p - '0');
}
}
if (bitwidth <= 64) {
emitValueChange(handle, m_packed_value_buffer_.front());
} else {
emitValueChange(handle, m_packed_value_buffer_.data(), EncodingType::BINARY);
}
}
/////////////////////////////////////////
// File flushing functions
/////////////////////////////////////////
void Writer::writeHeader_(const Header &header, std::ostream &os) {
StreamWriteHelper h(os);
static char kDefaultWriterName[sizeof(header.m_writer)] = "fstcppWriter";
const char *writer_name = header.m_writer[0] == '\0' ? kDefaultWriterName : header.m_writer;
// Actual write
h //
.seek(std::streamoff(0), std::ios_base::beg)
.writeBlockHeader(BlockType::HEADER, HeaderInfo::total_size)
.writeUInt(header.m_start_time)
.writeUInt(header.m_end_time)
.writeFloat(HeaderInfo::kEndianessMagicIdentifier)
.writeUInt(header.m_writer_memory_use)
.writeUInt(header.m_num_scopes)
.writeUInt(header.m_num_vars)
.writeUInt(header.m_num_handles)
.writeUInt(header.m_num_value_change_data_blocks)
.writeUInt(header.m_timescale)
.write(writer_name, sizeof(header.m_writer))
.write(header.m_date, sizeof(header.m_date))
.fill('\0', HeaderInfo::Size::reserved)
.writeUInt(static_cast<uint8_t>(header.m_filetype))
.writeUInt(header.m_timezero);
FST_DCHECK_EQ(os.tellp(), HeaderInfo::total_size + kSharedBlockHeaderSize);
}
namespace { // compression helpers
// These API pass compressed_data to avoid frequent reallocations
void compressUsingLz4(
const std::vector<uint8_t> &uncompressed_data, std::vector<uint8_t> &compressed_data
) {
const int uncompressed_size = uncompressed_data.size();
const int compressed_bound = LZ4_compressBound(uncompressed_size);
compressed_data.resize(compressed_bound);
const int compressed_size = LZ4_compress_default(
reinterpret_cast<const char *>(uncompressed_data.data()),
reinterpret_cast<char *>(compressed_data.data()),
uncompressed_size,
compressed_bound
);
compressed_data.resize(compressed_size);
}
void compressUsingZlib(
const std::vector<uint8_t> &uncompressed_data, std::vector<uint8_t> &compressed_data, int level
) {
// compress using zlib
const uLong uncompressed_size = uncompressed_data.size();
uLongf compressed_bound = compressBound(uncompressed_size);
compressed_data.resize(compressed_bound);
const auto z_status = compress2(
reinterpret_cast<Bytef *>(compressed_data.data()),
&compressed_bound,
reinterpret_cast<const Bytef *>(uncompressed_data.data()),
uncompressed_size,
level
);
if (z_status != Z_OK) {
throw std::runtime_error(
"Failed to compress data with zlib, error code: " + std::to_string(z_status)
);
}
compressed_data.resize(compressed_bound);
}
std::pair<const uint8_t *, size_t> selectSmaller(
const std::vector<uint8_t> &compressed_data, const std::vector<uint8_t> &uncompressed_data
) {
std::pair<const uint8_t *, size_t> ret;
if (compressed_data.size() < uncompressed_data.size()) {
ret.first = compressed_data.data();
ret.second = compressed_data.size();
} else {
ret.first = uncompressed_data.data();
ret.second = uncompressed_data.size();
}
return ret;
}
} // namespace
// AppendHierarchy_ and AppendGeometry_ shares a very similar structure
// But they are slightly different in the original C implementation...
void Writer::appendGeometry_(std::ostream &os) {
if (m_geometry_buffer_.empty()) {
// skip the geometry block if there is no data
return;
}
std::vector<uint8_t> geometry_buffer_compressed_{};
compressUsingZlib(m_geometry_buffer_, geometry_buffer_compressed_, 9);
// TODO: Replace with structured binding in C++17
const std::pair<const uint8_t *, size_t> selected_pair =
selectSmaller(geometry_buffer_compressed_, m_geometry_buffer_);
const uint8_t *selected_data = selected_pair.first;
const size_t selected_size = selected_pair.second;
StreamWriteHelper h(os);
h //
.seek(0, std::ios_base::end)
// 16 is for the uncompressed_size and header_.num_handles
.writeBlockHeader(BlockType::GEOMETRY, selected_size + 16)
.writeUInt<uint64_t>(m_geometry_buffer_.size())
// I don't know why the original C implementation write num_handles again here
// but we have to follow it
.writeUInt(m_header_.m_num_handles)
.write(selected_data, selected_size);
}
void Writer::appendHierarchy_(std::ostream &os) {
if (m_hierarchy_buffer_.empty()) {
// skip the hierarchy block if there is no data
return;
}
// compress hierarchy_buffer_ using LZ4.
const int compressed_bound{LZ4_compressBound(m_hierarchy_buffer_.size())};
std::vector<uint8_t> hierarchy_buffer_compressed_(compressed_bound);
const int compressed_size{LZ4_compress_default(
reinterpret_cast<const char *>(m_hierarchy_buffer_.data()),
reinterpret_cast<char *>(hierarchy_buffer_compressed_.data()),
m_hierarchy_buffer_.size(),
compressed_bound
)};
StreamWriteHelper h(os);
h //
.seek(0, std::ios_base::end)
// +16 is for the uncompressed_size
.writeBlockHeader(BlockType::HIERARCHY_LZ4_COMPRESSED, compressed_size + 8)
.writeUInt<uint64_t>(m_hierarchy_buffer_.size())
.write(hierarchy_buffer_compressed_.data(), compressed_size);
}
void Writer::appendBlackout_(std::ostream &os) {
if (m_blackout_data_.m_count == 0) {
// skip the blackout block if there is no data
return;
}
const std::vector<uint8_t> &blackout_data = m_blackout_data_.m_buffer;
const std::streampos begin_of_blackout_block = os.tellp();
StreamWriteHelper h(os);
h //
// skip the block header
.seek(kSharedBlockHeaderSize, std::ios_base::cur)
// Note: we cannot know the size beforehand since this length is LEB128 encoded
.writeLEB128(blackout_data.size())
.write(blackout_data.data(), blackout_data.size());
const std::streamoff size_of_blackout_block = os.tellp() - begin_of_blackout_block;
h //
// go back to the beginning of the block
.seek(begin_of_blackout_block, std::ios_base::beg)
// and write the block header
.writeBlockHeader(
BlockType::BLACKOUT,
static_cast<uint64_t>(size_of_blackout_block - kSharedBlockHeaderSize)
);
}
void detail::ValueChangeData::writeInitialBits(std::vector<uint8_t> &os) const {
// Build vc_bits_data by concatenating each variable's initial bits as documented.
// We will not compress for now; just generate the raw bytes and print summary to stdout.
for (size_t i{0}; i < m_variable_infos.size(); ++i) {
const VariableInfo &vref = m_variable_infos[i];
vref.dumpInitialBits(os);
}
}
std::vector<std::vector<uint8_t>> detail::ValueChangeData::computeWaveData() const {
const size_t N{m_variable_infos.size()};
std::vector<std::vector<uint8_t>> data(N);
for (size_t i{0}; i < N; ++i) {
m_variable_infos[i].dumpValueChanges(data[i]);
}
return data;
}
std::vector<int64_t> detail::ValueChangeData::uniquifyWaveData(
std::vector<std::vector<uint8_t>> &data
) {
// After this function, positions[i] is:
// - = 0: If data[i] is unique (first occurrence)
// - < 0: If data[i] is a duplicate, encoded as -(original_index + 1)
std::vector<int64_t> positions(data.size(), 0);
struct MyHash {
size_t operator()(const std::vector<uint8_t> *vec) const {
size_t seed = 0;
for (auto v : *vec) {
seed ^= v + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
struct MyEqual {
bool operator()(const std::vector<uint8_t> *a, const std::vector<uint8_t> *b) const {
return *a == *b;
}
};
std::unordered_map<const std::vector<uint8_t> *, int64_t, MyHash, MyEqual> data_map;
for (size_t i = 0; i < data.size(); ++i) {
if (data[i].empty()) {
continue;
}
// insert vec->i to data_map if not exists
auto p = data_map.emplace(&data[i], static_cast<int64_t>(i));
auto it = p.first;
const bool inserted{p.second};
if (!inserted) {
// duplicated wave data found
positions[i] = -(it->second + 1);
// clear data to save memory
data[i].clear();
}
}
return positions;
}
uint64_t detail::ValueChangeData::encodePositionsAndwriteUniqueWaveData(
std::ostream &os,
const std::vector<std::vector<uint8_t>> &data,
std::vector<int64_t> &positions,
WriterPackType pack_type
) {
// After this function, positions[i] is:
// - = 0: If variable i has no wave data
// - < 0: The negative value from flushValueChangeData_ValueChanges_UniquifyWaveData_,
// unchanged
// - > 0: The size (in bytes) of the wave data block for *previous* variable,
// the previous block size of the first block is 1 (required by FST spec).
StreamWriteHelper h(os);
int64_t previous_size = 1;
uint64_t written_count = 0;
std::vector<uint8_t> compressed_data;
for (size_t i = 0; i < positions.size(); ++i) {
if (positions[i] < 0) {
// duplicate (negative index), do nothing
} else if (data[i].empty()) {
// no change (empty data), positions[i] remains 0
} else {
// try to compress
const uint8_t *selected_data;
size_t selected_size;
if (pack_type == WriterPackType::NO_COMPRESSION || data[i].size() <= 32) {
selected_data = data[i].data();
selected_size = data[i].size();
} else {
compressUsingLz4(data[i], compressed_data);
const std::pair<const uint8_t *, size_t> selected_pair =
selectSmaller(compressed_data, data[i]);
selected_data = selected_pair.first;
selected_size = selected_pair.second;
}
const bool is_compressed = selected_data != data[i].data();
// non-empty unique data, write it
written_count++;
std::streamoff bytes_written;
h //
.beginOffset(bytes_written)
// FST spec: 0 means no compression, >0 for the size of the original data
.writeLEB128(is_compressed ? data[i].size() : 0)
.write(selected_data, selected_size)
.endOffset(&bytes_written);
positions[i] = previous_size;
previous_size = bytes_written;
}
}
return written_count;
}
void detail::ValueChangeData::writeEncodedPositions(
const std::vector<int64_t> &encoded_positions, std::ostream &os
) {
// Encode positions with the specified run/varint rules into a varint buffer.
StreamWriteHelper h(os);
size_t i = 0;
const size_t n = encoded_positions.size();
// arbitrary positive value for prev_negative
// so that first negative is always != prev_negative
int64_t prev_negative = 1;
// Please refer to the comments in
// flushValueChangeData_ValueChanges_EncodePositionsAndwriteWaveData_() for the encoding rules
// of positions.
while (i < n) {
if (encoded_positions[i] == 0) {
// zero: handle zero run-length
size_t run = 0;
while (i < n && encoded_positions[i] == 0) {
++run;
++i;
}
// encode as signed (run << 1) | 0 and write as signed LEB128
h.writeLEB128(run << 1);
} else {
// non-zero
int64_t value_to_encode = 0;
int64_t cur = encoded_positions[i];
if (cur < 0) {
if (cur == prev_negative) {
value_to_encode = 0;
} else {
value_to_encode = cur;
prev_negative = cur;
}
} else {
value_to_encode = cur;
}
// encode as signed (value << 1) | 1 and write as signed LEB128
h.writeLEB128Signed((value_to_encode << 1) | 1);
++i;
}
}
}
void detail::ValueChangeData::writeTimestamps(std::vector<uint8_t> &os) const {
// Build LEB128-encoded delta stream (first delta is timestamp[0] - 0)
StreamVectorWriteHelper h(os);
uint64_t prev{0};
for (size_t i{0}; i < m_timestamps.size(); ++i) {
const uint64_t cur{m_timestamps[i]};
const uint64_t delta{cur - prev};
h.writeLEB128(delta);
prev = cur;
}
}
void Writer::flushValueChangeDataConstPart_(
const detail::ValueChangeData &vcd, std::ostream &os, WriterPackType pack_type
) {
// 0. setup
StreamWriteHelper h(os);
// 1. write Block Header & Global Fields (start/end/mem_req placeholder)
// FST_BL_VCDATA_DYN_ALIAS2 (8) maps to WaveDataVersion3 in fst_file.h
// The positions we cannot fill in yet
const auto p_tmp1 = [&]() {
std::streamoff start_pos, memory_usage_pos;
h //
.beginOffset(start_pos) // record start position
.writeBlockHeader(BlockType::WAVE_DATA_VERSION3, 0 /* Length placeholder 0 */)
.writeUInt(vcd.m_timestamps.front())
.writeUInt(vcd.m_timestamps.back())
.beginOffset(memory_usage_pos) // record memory usage position
.writeUInt<uint64_t>(0); // placeholder for memory usage
return std::make_pair(start_pos, memory_usage_pos);
}();
const std::streamoff start_pos{p_tmp1.first};
const std::streamoff memory_usage_pos{p_tmp1.second};
// 2. Bits Section
{
std::vector<uint8_t> bits_data;
vcd.writeInitialBits(bits_data);
std::vector<uint8_t> bits_data_compressed;
const uint8_t *selected_data;
size_t selected_size;
if (pack_type == WriterPackType::NO_COMPRESSION || bits_data.size() < 32) {
selected_data = bits_data.data();
selected_size = bits_data.size();
} else {
compressUsingZlib(bits_data, bits_data_compressed, 4);
const std::pair<const uint8_t *, size_t> selected_pair =
selectSmaller(bits_data_compressed, bits_data);
selected_data = selected_pair.first;
selected_size = selected_pair.second;
}
h //
.writeLEB128(bits_data.size()) // uncompressed length
.writeLEB128(selected_size) // compressed length
.writeLEB128(vcd.m_variable_infos.size()) // bits count
.write(selected_data, selected_size);
}
// 3. Waves Section
// Note: We need positions for the next section
const auto p_tmp2 = [&, pack_type]() {
std::vector<std::vector<uint8_t>> wave_data{vcd.computeWaveData()};
const size_t memory_usage{std::accumulate(
wave_data.begin(),
wave_data.end(),
size_t(0),
[](size_t a, const std::vector<uint8_t> &b) { return a + b.size(); }
)};
std::vector<int64_t> positions{vcd.uniquifyWaveData(wave_data)};
h
// Note: this is not a typo, I expect we shall write count here.
// but the spec indeed write vcd.variable_infos.size(),
// which is repeated 1 times in header block, 2 times in valuechange block
.writeLEB128(vcd.m_variable_infos.size())
.writeUInt(uint8_t('4'));
const uint64_t count{detail::ValueChangeData::encodePositionsAndwriteUniqueWaveData(
os, wave_data, positions, pack_type
)};
(void)count;
return std::make_pair(positions, memory_usage);
}();
const std::vector<int64_t> positions{p_tmp2.first};
const size_t memory_usage{p_tmp2.second};
// 4. Position Section
{
const std::streampos pos_begin{os.tellp()};
vcd.writeEncodedPositions(positions, os);
const uint64_t pos_size{static_cast<uint64_t>(os.tellp() - pos_begin)};
h.writeUInt(pos_size); // Length comes AFTER data for positions
}
// 5. Time Section
{
std::vector<uint8_t> time_data;
vcd.writeTimestamps(time_data);
std::vector<uint8_t> time_data_compressed;
const uint8_t *selected_data;
size_t selected_size;
if (pack_type == WriterPackType::NO_COMPRESSION) {
selected_data = time_data.data();
selected_size = time_data.size();
} else {
compressUsingZlib(time_data, time_data_compressed, 9);
const std::pair<const uint8_t *, size_t> selected_pair =
selectSmaller(time_data_compressed, time_data);
selected_data = selected_pair.first;
selected_size = selected_pair.second;
}
h //
.write(selected_data, selected_size) // time data
.writeUInt(time_data.size()) // uncompressed len
.writeUInt(selected_size) // compressed len
.writeUInt(uint64_t(vcd.m_timestamps.size())); // count
}
// 6. Patch Block Length and Memory Required
std::streamoff end_pos{0};
h //
.beginOffset(end_pos)
// Patch Block Length (after 1 byte Type)
.seek(start_pos + std::streamoff(1), std::ios_base::beg)
.writeUInt<uint64_t>(static_cast<uint64_t>(end_pos - start_pos - 1))
// Patch Memory Required
.seek(memory_usage_pos, std::ios_base::beg)
.writeUInt<uint64_t>(static_cast<uint64_t>(memory_usage))
// Restore position to end
.seek(end_pos, std::ios_base::beg);
}
namespace { // Helper functions for createEnumTable
void appendEscToString(const string_view_pair in, std::string &out) {
for (size_t i{0}; i < in.m_size; ++i) {
const char c{in.m_data[i]};
switch (c) {
// clang-format off
case '\a': { out += "\\a"; break; }
case '\b': { out += "\\b"; break; }
case '\f': { out += "\\f"; break; }
case '\n': { out += "\\n"; break; }
case '\r': { out += "\\r"; break; }
case '\t': { out += "\\t"; break; }
case '\v': { out += "\\v"; break; }
case '\'': { out += "\\'"; break; }
case '\"': { out += "\\\""; break; }
case '\\': { out += "\\\\"; break; }
case '?': { out += "\\?"; break; }
// clang-format on
default: {
if (c > ' ' && c <= '~') {
out += c;
} else {
unsigned char val = static_cast<unsigned char>(c);
out += '\\';
out += (val / 64) + '0';
val &= 63;
out += (val / 8) + '0';
val &= 7;
out += val + '0';
}
break;
}
}
}
}
} // namespace
void Writer::setAttrBegin(
Hierarchy::AttrType attrtype,
Hierarchy::AttrSubType subtype,
const string_view_pair attrname,
uint64_t arg
) {
FST_CHECK(!m_hierarchy_finalized_);
StreamVectorWriteHelper h(m_hierarchy_buffer_);
if (attrtype > Hierarchy::AttrType::MAX) {
attrtype = Hierarchy::AttrType::MISC;
subtype = Hierarchy::AttrSubType::MISC_UNKNOWN;
}
switch (attrtype) {
// clang-format off
case Hierarchy::AttrType::ARRAY:
if (
subtype < Hierarchy::AttrSubType::ARRAY_NONE ||
subtype > Hierarchy::AttrSubType::ARRAY_SPARSE
) {
subtype = Hierarchy::AttrSubType::ARRAY_NONE;
}
break;
case Hierarchy::AttrType::ENUM:
if (
subtype < Hierarchy::AttrSubType::ENUM_SV_INTEGER ||
subtype > Hierarchy::AttrSubType::ENUM_TIME
) {
subtype = Hierarchy::AttrSubType::ENUM_SV_INTEGER;
}
break;
case Hierarchy::AttrType::PACK:
if (
subtype < Hierarchy::AttrSubType::PACK_NONE ||
subtype > Hierarchy::AttrSubType::PACK_SPARSE
) {
subtype = Hierarchy::AttrSubType::PACK_NONE;
}
break;
// clang-format on
case Hierarchy::AttrType::MISC:
default:
break;
}
h //
.writeU8Enum(Hierarchy::ScopeControlType::GEN_ATTR_BEGIN)
.writeU8Enum(attrtype)
.writeU8Enum(subtype)
.writeString0(attrname)
.writeLEB128(arg);
}
EnumHandle Writer::createEnumTable(
const string_view_pair name,
uint32_t min_valbits,
const std::vector<std::pair<string_view_pair, string_view_pair>> &literal_val_arr
) {
EnumHandle handle{0};
if (name.m_size == 0 || literal_val_arr.empty()) {
return handle;
}
std::string attr_str;
attr_str.reserve(256);
attr_str.append(name.m_data, name.m_size);
attr_str += ' ';
attr_str += std::to_string(literal_val_arr.size());
attr_str += ' ';
for (const auto &p : literal_val_arr) {
const string_view_pair literal{p.first};
// literal
appendEscToString(literal, attr_str);
attr_str += ' ';
}
for (const auto &p : literal_val_arr) {
const string_view_pair val{p.second};
// val (with padding)
if (min_valbits > 0 && val.m_size < min_valbits) {
attr_str.insert(attr_str.end(), min_valbits - val.m_size, '0');
}
appendEscToString(val, attr_str);
attr_str += ' ';
}
attr_str.pop_back(); // remove last space
handle = ++m_enum_count_;
setAttrBegin(
Hierarchy::AttrType::MISC,
Hierarchy::AttrSubType::MISC_ENUMTABLE,
make_string_view_pair(attr_str.c_str(), attr_str.size()),
handle
);
return handle;
}
} // namespace fst

View File

@ -0,0 +1,269 @@
// SPDX-FileCopyrightText: 2025-2026 Yu-Sheng Lin <johnjohnlys@gmail.com>
// SPDX-FileCopyrightText: 2025-2026 Yoda Lee <lc85301@gmail.com>
// SPDX-License-Identifier: MIT
// Project: libfstwriter
// Website: https://github.com/gtkwave/libfstwriter
#pragma once
// direct include
#include "fstcpp/fstcpp.h"
// C system headers
// C++ standard library headers
#include <algorithm>
#include <cstdint>
#include <ctime>
#include <fstream>
#include <vector>
#if __cplusplus >= 201703L
# include <string_view>
#endif
// Other libraries' .h files.
// Your project's .h files.
#include "fstcpp/fstcpp_assertion.h"
#include "fstcpp/fstcpp_variable_info.h"
namespace fst {
class Writer;
namespace detail {
// We define BlackoutData here for better code inlining, no forward declaration
// Blackout is not implemented yet
struct BlackoutData {
std::vector<uint8_t> m_buffer{};
uint64_t m_previous_timestamp{0};
uint64_t m_count{0};
void emitDumpActive(uint64_t current_timestamp, bool enable);
};
// We define ValueChangeData here for better code inlining, no forward declaration
struct ValueChangeData {
std::vector<VariableInfo> m_variable_infos{};
std::vector<uint64_t> m_timestamps{};
ValueChangeData();
~ValueChangeData();
void writeInitialBits(std::vector<uint8_t> &os) const;
std::vector<std::vector<uint8_t>> computeWaveData() const;
static std::vector<int64_t> uniquifyWaveData(std::vector<std::vector<uint8_t>> &data);
static uint64_t encodePositionsAndwriteUniqueWaveData(
std::ostream &os,
const std::vector<std::vector<uint8_t>> &unique_data,
std::vector<int64_t> &positions,
WriterPackType pack_type
);
static void writeEncodedPositions(
const std::vector<int64_t> &encoded_positions, std::ostream &os
);
void writeTimestamps(std::vector<uint8_t> &os) const;
void keepOnlyTheLatestValue();
};
} // namespace detail
class Writer {
friend class WriterTest;
private:
// File/memory buffers
// 1. For hierarchy and geometry, we do not keep the data structure, instead we just
// serialize them into buffers, and compress+write them at the end of file.
// 2. For header, we keep the data structure in memory since it is quite small
// 3. For wave data, we keep a complicated data structure in memory,
// and flush them to file when necessary
// 4. For blackout data, it is not implemented yet
std::ofstream m_main_fst_file_{};
std::vector<uint8_t> m_hierarchy_buffer_{};
std::vector<uint8_t> m_geometry_buffer_{};
// Temporary buffer for packing bit strings into words
// Only used in emitValueChange(Handle, const char*)
std::vector<uint64_t> m_packed_value_buffer_{};
Header m_header_{};
detail::BlackoutData m_blackout_data_{}; // Not implemented yet
detail::ValueChangeData m_value_change_data_{};
bool m_hierarchy_finalized_{false};
WriterPackType m_pack_type_{WriterPackType::LZ4};
uint64_t m_value_change_data_usage_{0}; // Note: this value is just an estimation
uint64_t m_value_change_data_flush_threshold_{128 << 20}; // 128MB
uint32_t m_enum_count_{0};
bool m_flush_pending_{false};
public:
Writer() {}
Writer(const string_view_pair name) {
if (name.m_size != 0) open(name);
}
~Writer() { close(); }
Writer(const Writer &) = delete;
Writer(Writer &&) = delete;
Writer &operator=(const Writer &) = delete;
Writer &operator=(Writer &&) = delete;
// File control
void open(const string_view_pair name);
void close();
//////////////////////////////
// Header manipulation API
//////////////////////////////
const Header &getHeader() const { return m_header_; }
void setTimecale(int8_t timescale) { m_header_.m_timescale = timescale; }
void setWriter(const string_view_pair writer) {
const size_t len = std::min(writer.m_size, sizeof(m_header_.m_writer));
std::copy_n(writer.m_data, len, m_header_.m_writer);
if (len != sizeof(m_header_.m_writer)) {
m_header_.m_writer[len] = '\0';
}
}
void setDate(const string_view_pair date_str) {
const size_t len = date_str.m_size;
FST_CHECK_EQ(len, sizeof(m_header_.m_date) - 1);
std::copy_n(date_str.m_data, len, m_header_.m_date);
m_header_.m_date[len] = '\0';
}
void setDate(const std::tm *d) { setDate(make_string_view_pair(std::asctime(d))); }
void setDate() {
// set date to now
std::time_t t{std::time(nullptr)};
setDate(std::localtime(&t));
}
void setTimezero(int64_t timezero) { m_header_.m_timezero = timezero; }
//////////////////////////////
// Change scope API
//////////////////////////////
void setScope(
Hierarchy::ScopeType scopetype,
const string_view_pair scopename,
const string_view_pair scopecomp
);
void upscope();
//////////////////////////////
// Attribute / Misc API
//////////////////////////////
void setAttrBegin(
Hierarchy::AttrType attrtype,
Hierarchy::AttrSubType subtype,
const string_view_pair attrname,
uint64_t arg
);
void setAttrEnd() {
m_hierarchy_buffer_.push_back(
static_cast<uint8_t>(Hierarchy::ScopeControlType::GEN_ATTR_END)
);
}
EnumHandle createEnumTable(
const string_view_pair name,
uint32_t min_valbits,
const std::vector<std::pair<string_view_pair, string_view_pair>> &literal_val_arr
);
template <typename T1, typename T2>
EnumHandle createEnumTable(
const char *name,
uint32_t min_valbits,
const std::vector<std::pair<T1, T2>> &literal_val_arr
) {
std::vector<std::pair<string_view_pair, string_view_pair>> arr{};
arr.reserve(literal_val_arr.size());
for (const auto &p : literal_val_arr) {
arr.emplace_back(make_string_view_pair(p.first), make_string_view_pair(p.second));
}
return createEnumTable(make_string_view_pair(name), min_valbits, arr);
}
void emitEnumTableRef(EnumHandle handle) {
setAttrBegin(
Hierarchy::AttrType::MISC,
Hierarchy::AttrSubType::MISC_ENUMTABLE,
make_string_view_pair(nullptr, 0),
handle
);
}
void setWriterPackType(WriterPackType pack_type) {
FST_CHECK(pack_type != WriterPackType::ZLIB && pack_type != WriterPackType::FASTLZ);
m_pack_type_ = pack_type;
}
//////////////////////////////
// Create variable API
//////////////////////////////
Handle createVar(
Hierarchy::VarType vartype,
Hierarchy::VarDirection vardir,
uint32_t bitwidth,
const string_view_pair name,
uint32_t alias_handle
);
// TODO
// Handle createVar2(
// Hierarchy::VarType vartype,
// Hierarchy::VarDirection vardir,
// uint32_t bitwidth,
// const string_view_pair name,
// uint32_t alias_handle,
// const string_view_pair type,
// Hierarchy::SupplementalVarType svt,
// Hierarchy::SupplementalDataType sdt
// );
//////////////////////////////
// Waveform API
//////////////////////////////
void emitTimeChange(uint64_t tim);
// TODO
// void emitDumpActive(bool enable);
void emitValueChange(
Handle handle, const uint32_t *val, EncodingType encoding = EncodingType::BINARY
);
void emitValueChange(
Handle handle, const uint64_t *val, EncodingType encoding = EncodingType::BINARY
);
// Pass by value for small integers
void emitValueChange(Handle handle, uint64_t val);
// Add support for C-string value changes (e.g. fst string values)
// Note: This function is mainly for GtkWave compatibility.
// It is very dirty and inefficient, users should avoid using it.
// - For double handles, const char* is interpreted as a double* (8B)
// - For normal integer handles, const char* is "01xz..." (1B per bit)
// We only ensure that this function works where Verilator use it.
void emitValueChange(Handle handle, const char *val);
// Flush value change data
void flushValueChangeData() { m_flush_pending_ = true; }
private:
// internal helpers
static void writeHeader_(const Header &header, std::ostream &os);
void appendGeometry_(std::ostream &os);
void appendHierarchy_(std::ostream &os);
void appendBlackout_(std::ostream &os); // Not implemented yet
// This function is used to flush value change data to file, and keep only the latest value in
// memory Just want to separate the const part from the non-const part for code clarity
static void flushValueChangeDataConstPart_(
const detail::ValueChangeData &vcd, std::ostream &os, WriterPackType pack_type
);
void flushValueChangeData_(detail::ValueChangeData &vcd, std::ostream &os) {
if (vcd.m_timestamps.empty()) {
return;
}
flushValueChangeDataConstPart_(vcd, os, m_pack_type_);
vcd.keepOnlyTheLatestValue();
++m_header_.m_num_value_change_data_blocks;
m_value_change_data_usage_ = 0;
m_flush_pending_ = false;
}
void finalizeHierarchy_() {
if (m_hierarchy_finalized_) return;
m_hierarchy_finalized_ = true;
// Original FST code comments: as a default, use 128MB and increment when
// every 1M signals are defined.
m_value_change_data_flush_threshold_ = (((m_header_.m_num_handles - 1) >> 20) + 1) << 27;
}
template <typename... T>
void emitValueChangeHelper_(Handle handle, T &&...val);
};
} // namespace fst

View File

@ -1,549 +0,0 @@
/*
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
SPDX-License-Identifier: MIT
*/
#include "fastlz.h"
#if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
/*
* Always check for bound when decompressing.
* Generally it is best to leave it defined.
*/
#define FASTLZ_SAFE
/*
* Give hints to the compiler for branch prediction optimization.
*/
#if defined(__GNUC__) && (__GNUC__ > 2)
#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
#else
#define FASTLZ_EXPECT_CONDITIONAL(c) (c)
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
#endif
/*
* Use inlined functions for supported systems.
*/
#if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
#define FASTLZ_INLINE inline
#elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
#define FASTLZ_INLINE __inline
#else
#define FASTLZ_INLINE
#endif
/*
* Prevent accessing more than 8-bit at once, except on x86 architectures.
*/
#if !defined(FASTLZ_STRICT_ALIGN)
#define FASTLZ_STRICT_ALIGN
#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__amd64) /* GNU C */
#undef FASTLZ_STRICT_ALIGN
#elif defined(_M_IX86) /* Intel, MSVC */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__386)
#undef FASTLZ_STRICT_ALIGN
#elif defined(_X86_) /* MinGW */
#undef FASTLZ_STRICT_ALIGN
#elif defined(__I86__) /* Digital Mars */
#undef FASTLZ_STRICT_ALIGN
#endif
#endif
/* prototypes */
int fastlz_compress(const void* input, int length, void* output);
int fastlz_compress_level(int level, const void* input, int length, void* output);
int fastlz_decompress(const void* input, int length, void* output, int maxout);
#define MAX_COPY 32
#define MAX_LEN 264 /* 256 + 8 */
#define MAX_DISTANCE 8192
#if !defined(FASTLZ_STRICT_ALIGN)
#define FASTLZ_READU16(p) *((const flzuint16*)(p))
#else
#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
#endif
#define HASH_LOG 13
#define HASH_SIZE (1<< HASH_LOG)
#define HASH_MASK (HASH_SIZE-1)
#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
#undef FASTLZ_LEVEL
#define FASTLZ_LEVEL 1
#undef FASTLZ_COMPRESSOR
#undef FASTLZ_DECOMPRESSOR
#define FASTLZ_COMPRESSOR fastlz1_compress
#define FASTLZ_DECOMPRESSOR fastlz1_decompress
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
#include "fastlz.c"
#undef FASTLZ_LEVEL
#define FASTLZ_LEVEL 2
#undef MAX_DISTANCE
#define MAX_DISTANCE 8191
#define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
#undef FASTLZ_COMPRESSOR
#undef FASTLZ_DECOMPRESSOR
#define FASTLZ_COMPRESSOR fastlz2_compress
#define FASTLZ_DECOMPRESSOR fastlz2_decompress
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
#include "fastlz.c"
int fastlz_compress(const void* input, int length, void* output)
{
/* for short block, choose fastlz1 */
if(length < 65536)
return fastlz1_compress(input, length, output);
/* else... */
return fastlz2_compress(input, length, output);
}
int fastlz_decompress(const void* input, int length, void* output, int maxout)
{
/* magic identifier for compression level */
int level = ((*(const flzuint8*)input) >> 5) + 1;
if(level == 1)
return fastlz1_decompress(input, length, output, maxout);
if(level == 2)
return fastlz2_decompress(input, length, output, maxout);
/* unknown level, trigger error */
return 0;
}
int fastlz_compress_level(int level, const void* input, int length, void* output)
{
if(level == 1)
return fastlz1_compress(input, length, output);
if(level == 2)
return fastlz2_compress(input, length, output);
return 0;
}
#else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output)
{
const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_bound = ip + length - 2;
const flzuint8* ip_limit = ip + length - 12;
flzuint8* op = (flzuint8*) output;
const flzuint8* htab[HASH_SIZE];
const flzuint8** hslot;
flzuint32 hval;
flzuint32 copy;
/* sanity check */
if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
{
if(length)
{
/* create literal copy only */
*op++ = length-1;
ip_bound++;
while(ip <= ip_bound)
*op++ = *ip++;
return length+1;
}
else
return 0;
}
/* initializes hash table */
for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
*hslot = ip;
/* we start with literal copy */
copy = 2;
*op++ = MAX_COPY-1;
*op++ = *ip++;
*op++ = *ip++;
/* main loop */
while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
{
const flzuint8* ref;
flzuint32 distance;
/* minimum match length */
flzuint32 len = 3;
/* comparison starting-point */
const flzuint8* anchor = ip;
/* check for a run */
#if FASTLZ_LEVEL==2
if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
{
distance = 1;
/* ip += 3; */ /* scan-build, never used */
ref = anchor - 1 + 3;
goto match;
}
#endif
/* find potential match */
HASH_FUNCTION(hval,ip);
hslot = htab + hval;
ref = htab[hval];
/* calculate distance to the match */
distance = anchor - ref;
/* update hash table */
*hslot = anchor;
/* is this a match? check the first 3 bytes */
if(distance==0 ||
#if FASTLZ_LEVEL==1
(distance >= MAX_DISTANCE) ||
#else
(distance >= MAX_FARDISTANCE) ||
#endif
*ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
goto literal;
#if FASTLZ_LEVEL==2
/* far, needs at least 5-byte match */
if(distance >= MAX_DISTANCE)
{
if(*ip++ != *ref++ || *ip++!= *ref++)
goto literal;
len += 2;
}
match:
#endif
/* last matched byte */
ip = anchor + len;
/* distance is biased */
distance--;
if(!distance)
{
/* zero distance means a run */
flzuint8 x = ip[-1];
while(ip < ip_bound)
if(*ref++ != x) break; else ip++;
}
else
for(;;)
{
/* safe because the outer check against ip limit */
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
if(*ref++ != *ip++) break;
while(ip < ip_bound)
if(*ref++ != *ip++) break;
break;
}
/* if we have copied something, adjust the copy count */
if(copy)
/* copy is biased, '0' means 1 byte copy */
*(op-copy-1) = copy-1;
else
/* back, to overwrite the copy count */
op--;
/* reset literal counter */
copy = 0;
/* length is biased, '1' means a match of 3 bytes */
ip -= 3;
len = ip - anchor;
/* encode the match */
#if FASTLZ_LEVEL==2
if(distance < MAX_DISTANCE)
{
if(len < 7)
{
*op++ = (len << 5) + (distance >> 8);
*op++ = (distance & 255);
}
else
{
*op++ = (7 << 5) + (distance >> 8);
for(len-=7; len >= 255; len-= 255)
*op++ = 255;
*op++ = len;
*op++ = (distance & 255);
}
}
else
{
/* far away, but not yet in the another galaxy... */
if(len < 7)
{
distance -= MAX_DISTANCE;
*op++ = (len << 5) + 31;
*op++ = 255;
*op++ = distance >> 8;
*op++ = distance & 255;
}
else
{
distance -= MAX_DISTANCE;
*op++ = (7 << 5) + 31;
for(len-=7; len >= 255; len-= 255)
*op++ = 255;
*op++ = len;
*op++ = 255;
*op++ = distance >> 8;
*op++ = distance & 255;
}
}
#else
if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
while(len > MAX_LEN-2)
{
*op++ = (7 << 5) + (distance >> 8);
*op++ = MAX_LEN - 2 - 7 -2;
*op++ = (distance & 255);
len -= MAX_LEN-2;
}
if(len < 7)
{
*op++ = (len << 5) + (distance >> 8);
*op++ = (distance & 255);
}
else
{
*op++ = (7 << 5) + (distance >> 8);
*op++ = len - 7;
*op++ = (distance & 255);
}
#endif
/* update the hash at match boundary */
HASH_FUNCTION(hval,ip);
htab[hval] = ip++;
HASH_FUNCTION(hval,ip);
htab[hval] = ip++;
/* assuming literal copy */
*op++ = MAX_COPY-1;
continue;
literal:
*op++ = *anchor++;
ip = anchor;
copy++;
if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
{
copy = 0;
*op++ = MAX_COPY-1;
}
}
/* left-over as literal copy */
ip_bound++;
while(ip <= ip_bound)
{
*op++ = *ip++;
copy++;
if(copy == MAX_COPY)
{
copy = 0;
*op++ = MAX_COPY-1;
}
}
/* if we have copied something, adjust the copy length */
if(copy)
*(op-copy-1) = copy-1;
else
op--;
#if FASTLZ_LEVEL==2
/* marker for fastlz2 */
*(flzuint8*)output |= (1 << 5);
#endif
return op - (flzuint8*)output;
}
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
{
const flzuint8* ip = (const flzuint8*) input;
const flzuint8* ip_limit = ip + length;
flzuint8* op = (flzuint8*) output;
flzuint8* op_limit = op + maxout;
flzuint32 ctrl = (*ip++) & 31;
int loop = 1;
do
{
const flzuint8* ref = op;
flzuint32 len = ctrl >> 5;
flzuint32 ofs = (ctrl & 31) << 8;
if(ctrl >= 32)
{
#if FASTLZ_LEVEL==2
flzuint8 code;
#endif
len--;
ref -= ofs;
if (len == 7-1)
#if FASTLZ_LEVEL==1
len += *ip++;
ref -= *ip++;
#else
do
{
code = *ip++;
len += code;
} while (code==255);
code = *ip++;
ref -= code;
/* match from 16-bit distance */
if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
{
ofs = (*ip++) << 8;
ofs += *ip++;
ref = op - ofs - MAX_DISTANCE;
}
#endif
#ifdef FASTLZ_SAFE
if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
return 0;
if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
return 0;
#endif
if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
ctrl = *ip++;
else
loop = 0;
if(ref == op)
{
/* optimize copy for a run */
flzuint8 b = ref[-1];
*op++ = b;
*op++ = b;
*op++ = b;
for(; len; --len)
*op++ = b;
}
else
{
#if !defined(FASTLZ_STRICT_ALIGN)
const flzuint16* p;
flzuint16* q;
#endif
/* copy from reference */
ref--;
*op++ = *ref++;
*op++ = *ref++;
*op++ = *ref++;
#if !defined(FASTLZ_STRICT_ALIGN)
/* copy a byte, so that now it's word aligned */
if(len & 1)
{
*op++ = *ref++;
len--;
}
/* copy 16-bit at once */
q = (flzuint16*) op;
op += len;
p = (const flzuint16*) ref;
for(len>>=1; len > 4; len-=4)
{
*q++ = *p++;
*q++ = *p++;
*q++ = *p++;
*q++ = *p++;
}
for(; len; --len)
*q++ = *p++;
#else
for(; len; --len)
*op++ = *ref++;
#endif
}
}
else
{
ctrl++;
#ifdef FASTLZ_SAFE
if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
return 0;
if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
return 0;
#endif
*op++ = *ip++;
for(--ctrl; ctrl; ctrl--)
*op++ = *ip++;
loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
if(loop)
ctrl = *ip++;
}
}
while(FASTLZ_EXPECT_CONDITIONAL(loop));
return op - (flzuint8*)output;
}
#endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */

View File

@ -1,109 +0,0 @@
/*
FastLZ - lightning-fast lossless compression library
Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
SPDX-License-Identifier: MIT
*/
#ifndef FASTLZ_H
#define FASTLZ_H
#include <inttypes.h>
#define flzuint8 uint8_t
#define flzuint16 uint16_t
#define flzuint32 uint32_t
#define FASTLZ_VERSION 0x000100
#define FASTLZ_VERSION_MAJOR 0
#define FASTLZ_VERSION_MINOR 0
#define FASTLZ_VERSION_REVISION 0
#define FASTLZ_VERSION_STRING "0.1.0"
#if defined (__cplusplus)
extern "C" {
#endif
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
*/
int fastlz_compress(const void* input, int length, void* output);
/**
Decompress a block of compressed data and returns the size of the
decompressed block. If error occurs, e.g. the compressed data is
corrupted or the output buffer is not large enough, then 0 (zero)
will be returned instead.
The input buffer and the output buffer can not overlap.
Decompression is memory safe and guaranteed not to write the output buffer
more than what is specified in maxout.
*/
int fastlz_decompress(const void* input, int length, void* output, int maxout);
/**
Compress a block of data in the input buffer and returns the size of
compressed block. The size of input buffer is specified by length. The
minimum input buffer size is 16.
The output buffer must be at least 5% larger than the input buffer
and can not be smaller than 66 bytes.
If the input is not compressible, the return value might be larger than
length (input buffer size).
The input buffer and the output buffer can not overlap.
Compression level can be specified in parameter level. At the moment,
only level 1 and level 2 are supported.
Level 1 is the fastest compression and generally useful for short data.
Level 2 is slightly slower but it gives better compression ratio.
Note that the compressed data, regardless of the level, can always be
decompressed using the function fastlz_decompress above.
*/
int fastlz_compress_level(int level, const void* input, int length, void* output);
#if defined (__cplusplus)
}
#endif
#endif /* FASTLZ_H */

View File

@ -1,12 +0,0 @@
// This file specifically for FST usage
// Originally generated from config.h.in by configure.
// SPDX-FileCopyrightText: 2018-2026 Wilson Snyder
// SPDX-License-Identifier: CC0-1.0
/* Define to 1 if you have <alloca.h> and it should be used (not on Ultrix). */
#if !defined(__MINGW32__) && !defined(__FreeBSD__)
# define HAVE_ALLOCA_H 1
#endif
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
#define HAVE_FSEEKO 1

View File

@ -1,52 +0,0 @@
/*
* Copyright (c) 2009-2018 Tony Bybell.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
*/
#ifndef WIN_UNISTD_H
#define WIN_UNISTD_H
#include <stdlib.h>
#ifdef _WIN64
#include <io.h>
#else
#include <sys/io.h>
#endif
#include <process.h>
#define ftruncate _chsize_s
#define unlink _unlink
#define fileno _fileno
#define lseek _lseeki64
#ifdef _WIN64
#define ssize_t __int64
#define SSIZE_MAX 9223372036854775807i64
#else
#define ssize_t long
#define SSIZE_MAX 2147483647L
#endif
#include "stdint.h"
#endif //WIN_UNISTD_H

File diff suppressed because it is too large Load Diff

View File

@ -1,548 +0,0 @@
/*
* Copyright (c) 2009-2026 Tony Bybell.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* SPDX-License-Identifier: MIT
*/
#ifndef FST_API_H
#define FST_API_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <zlib.h>
#include <inttypes.h>
#if defined(_MSC_VER)
#include "fst_win_unistd.h"
#else
#include <unistd.h>
#endif
#include <time.h>
typedef uint32_t fstHandle;
typedef uint32_t fstEnumHandle;
enum fstWriterPackType
{
FST_WR_PT_ZLIB = 0,
FST_WR_PT_FASTLZ = 1,
FST_WR_PT_LZ4 = 2
};
enum fstFileType
{
FST_FT_MIN = 0,
FST_FT_VERILOG = 0,
FST_FT_VHDL = 1,
FST_FT_VERILOG_VHDL = 2,
FST_FT_MAX = 2
};
enum fstBlockType
{
FST_BL_HDR = 0,
FST_BL_VCDATA = 1,
FST_BL_BLACKOUT = 2,
FST_BL_GEOM = 3,
FST_BL_HIER = 4,
FST_BL_VCDATA_DYN_ALIAS = 5,
FST_BL_HIER_LZ4 = 6,
FST_BL_HIER_LZ4DUO = 7,
FST_BL_VCDATA_DYN_ALIAS2 = 8,
FST_BL_ZWRAPPER = 254, /* indicates that whole trace is gz wrapped */
FST_BL_SKIP = 255 /* used while block is being written */
};
enum fstScopeType
{
FST_ST_MIN = 0,
FST_ST_VCD_MODULE = 0,
FST_ST_VCD_TASK = 1,
FST_ST_VCD_FUNCTION = 2,
FST_ST_VCD_BEGIN = 3,
FST_ST_VCD_FORK = 4,
FST_ST_VCD_GENERATE = 5,
FST_ST_VCD_STRUCT = 6,
FST_ST_VCD_UNION = 7,
FST_ST_VCD_CLASS = 8,
FST_ST_VCD_INTERFACE = 9,
FST_ST_VCD_PACKAGE = 10,
FST_ST_VCD_PROGRAM = 11,
FST_ST_VHDL_ARCHITECTURE = 12,
FST_ST_VHDL_PROCEDURE = 13,
FST_ST_VHDL_FUNCTION = 14,
FST_ST_VHDL_RECORD = 15,
FST_ST_VHDL_PROCESS = 16,
FST_ST_VHDL_BLOCK = 17,
FST_ST_VHDL_FOR_GENERATE = 18,
FST_ST_VHDL_IF_GENERATE = 19,
FST_ST_VHDL_GENERATE = 20,
FST_ST_VHDL_PACKAGE = 21,
FST_ST_SV_ARRAY = 22,
FST_ST_MAX = 22,
FST_ST_GEN_ATTRBEGIN = 252,
FST_ST_GEN_ATTREND = 253,
FST_ST_VCD_SCOPE = 254,
FST_ST_VCD_UPSCOPE = 255
};
enum fstVarType
{
FST_VT_MIN = 0, /* start of vartypes */
FST_VT_VCD_EVENT = 0,
FST_VT_VCD_INTEGER = 1,
FST_VT_VCD_PARAMETER = 2,
FST_VT_VCD_REAL = 3,
FST_VT_VCD_REAL_PARAMETER = 4,
FST_VT_VCD_REG = 5,
FST_VT_VCD_SUPPLY0 = 6,
FST_VT_VCD_SUPPLY1 = 7,
FST_VT_VCD_TIME = 8,
FST_VT_VCD_TRI = 9,
FST_VT_VCD_TRIAND = 10,
FST_VT_VCD_TRIOR = 11,
FST_VT_VCD_TRIREG = 12,
FST_VT_VCD_TRI0 = 13,
FST_VT_VCD_TRI1 = 14,
FST_VT_VCD_WAND = 15,
FST_VT_VCD_WIRE = 16,
FST_VT_VCD_WOR = 17,
FST_VT_VCD_PORT = 18,
FST_VT_VCD_SPARRAY = 19, /* used to define the rownum (index) port for a sparse array */
FST_VT_VCD_REALTIME = 20,
FST_VT_GEN_STRING = 21, /* generic string type (max len is defined dynamically via
fstWriterEmitVariableLengthValueChange) */
FST_VT_SV_BIT = 22,
FST_VT_SV_LOGIC = 23,
FST_VT_SV_INT = 24, /* declare as size = 32 */
FST_VT_SV_SHORTINT = 25, /* declare as size = 16 */
FST_VT_SV_LONGINT = 26, /* declare as size = 64 */
FST_VT_SV_BYTE = 27, /* declare as size = 8 */
FST_VT_SV_ENUM = 28, /* declare as appropriate type range */
FST_VT_SV_SHORTREAL = 29, /* declare and emit same as FST_VT_VCD_REAL (needs to be emitted
as double, not a float) */
FST_VT_MAX = 29 /* end of vartypes */
};
enum fstVarDir
{
FST_VD_MIN = 0,
FST_VD_IMPLICIT = 0,
FST_VD_INPUT = 1,
FST_VD_OUTPUT = 2,
FST_VD_INOUT = 3,
FST_VD_BUFFER = 4,
FST_VD_LINKAGE = 5,
FST_VD_MAX = 5
};
enum fstHierType
{
FST_HT_MIN = 0,
FST_HT_SCOPE = 0,
FST_HT_UPSCOPE = 1,
FST_HT_VAR = 2,
FST_HT_ATTRBEGIN = 3,
FST_HT_ATTREND = 4,
/* FST_HT_TREEBEGIN and FST_HT_TREEEND are not yet used by FST but are currently used when
fstHier bridges other formats */
FST_HT_TREEBEGIN = 5,
FST_HT_TREEEND = 6,
FST_HT_MAX = 6
};
enum fstAttrType
{
FST_AT_MIN = 0,
FST_AT_MISC = 0, /* self-contained: does not need matching FST_HT_ATTREND */
FST_AT_ARRAY = 1,
FST_AT_ENUM = 2,
FST_AT_PACK = 3,
FST_AT_MAX = 3
};
enum fstMiscType
{
FST_MT_MIN = 0,
FST_MT_COMMENT = 0, /* use fstWriterSetComment() to emit */
FST_MT_ENVVAR = 1, /* use fstWriterSetEnvVar() to emit */
FST_MT_SUPVAR = 2, /* use fstWriterCreateVar2() to emit */
FST_MT_PATHNAME = 3, /* reserved for fstWriterSetSourceStem() string -> number management */
FST_MT_SOURCESTEM = 4, /* use fstWriterSetSourceStem() to emit */
FST_MT_SOURCEISTEM = 5, /* use fstWriterSetSourceInstantiationStem() to emit */
FST_MT_VALUELIST =
6, /* use fstWriterSetValueList() to emit, followed by fstWriterCreateVar*() */
FST_MT_ENUMTABLE =
7, /* use fstWriterCreateEnumTable() and fstWriterEmitEnumTableRef() to emit */
FST_MT_UNKNOWN = 8,
FST_MT_MAX = 8
};
enum fstArrayType
{
FST_AR_MIN = 0,
FST_AR_NONE = 0,
FST_AR_UNPACKED = 1,
FST_AR_PACKED = 2,
FST_AR_SPARSE = 3,
FST_AR_MAX = 3
};
enum fstEnumValueType
{
FST_EV_SV_INTEGER = 0,
FST_EV_SV_BIT = 1,
FST_EV_SV_LOGIC = 2,
FST_EV_SV_INT = 3,
FST_EV_SV_SHORTINT = 4,
FST_EV_SV_LONGINT = 5,
FST_EV_SV_BYTE = 6,
FST_EV_SV_UNSIGNED_INTEGER = 7,
FST_EV_SV_UNSIGNED_BIT = 8,
FST_EV_SV_UNSIGNED_LOGIC = 9,
FST_EV_SV_UNSIGNED_INT = 10,
FST_EV_SV_UNSIGNED_SHORTINT = 11,
FST_EV_SV_UNSIGNED_LONGINT = 12,
FST_EV_SV_UNSIGNED_BYTE = 13,
FST_EV_REG = 14,
FST_EV_TIME = 15,
FST_EV_MAX = 15
};
enum fstPackType
{
FST_PT_NONE = 0,
FST_PT_UNPACKED = 1,
FST_PT_PACKED = 2,
FST_PT_TAGGED_PACKED = 3,
FST_PT_MAX = 3
};
enum fstSupplementalVarType
{
FST_SVT_MIN = 0,
FST_SVT_NONE = 0,
FST_SVT_VHDL_SIGNAL = 1,
FST_SVT_VHDL_VARIABLE = 2,
FST_SVT_VHDL_CONSTANT = 3,
FST_SVT_VHDL_FILE = 4,
FST_SVT_VHDL_MEMORY = 5,
FST_SVT_MAX = 5
};
enum fstSupplementalDataType
{
FST_SDT_MIN = 0,
FST_SDT_NONE = 0,
FST_SDT_VHDL_BOOLEAN = 1,
FST_SDT_VHDL_BIT = 2,
FST_SDT_VHDL_BIT_VECTOR = 3,
FST_SDT_VHDL_STD_ULOGIC = 4,
FST_SDT_VHDL_STD_ULOGIC_VECTOR = 5,
FST_SDT_VHDL_STD_LOGIC = 6,
FST_SDT_VHDL_STD_LOGIC_VECTOR = 7,
FST_SDT_VHDL_UNSIGNED = 8,
FST_SDT_VHDL_SIGNED = 9,
FST_SDT_VHDL_INTEGER = 10,
FST_SDT_VHDL_REAL = 11,
FST_SDT_VHDL_NATURAL = 12,
FST_SDT_VHDL_POSITIVE = 13,
FST_SDT_VHDL_TIME = 14,
FST_SDT_VHDL_CHARACTER = 15,
FST_SDT_VHDL_STRING = 16,
FST_SDT_MAX = 16,
FST_SDT_SVT_SHIFT_COUNT = 10, /* FST_SVT_* is ORed in by fstWriterCreateVar2() to the left
after shifting FST_SDT_SVT_SHIFT_COUNT */
FST_SDT_ABS_MAX = ((1 << (FST_SDT_SVT_SHIFT_COUNT)) - 1)
};
struct fstHier
{
unsigned char htyp;
union
{
/* if htyp == FST_HT_SCOPE */
struct fstHierScope
{
unsigned char typ; /* FST_ST_MIN ... FST_ST_MAX */
const char *name;
const char *component;
uint32_t name_length; /* strlen(u.scope.name) */
uint32_t component_length; /* strlen(u.scope.component) */
} scope;
/* if htyp == FST_HT_VAR */
struct fstHierVar
{
unsigned char typ; /* FST_VT_MIN ... FST_VT_MAX */
unsigned char direction; /* FST_VD_MIN ... FST_VD_MAX */
unsigned char svt_workspace; /* zeroed out by FST reader, for client code use */
unsigned char sdt_workspace; /* zeroed out by FST reader, for client code use */
unsigned int sxt_workspace; /* zeroed out by FST reader, for client code use */
const char *name;
uint32_t length;
fstHandle handle;
uint32_t name_length; /* strlen(u.var.name) */
unsigned is_alias : 1;
} var;
/* if htyp == FST_HT_ATTRBEGIN */
struct fstHierAttr
{
unsigned char typ; /* FST_AT_MIN ... FST_AT_MAX */
unsigned char
subtype; /* from fstMiscType, fstArrayType, fstEnumValueType, fstPackType */
const char *name;
uint64_t arg; /* number of array elements, struct members, or some other payload
(possibly ignored) */
uint64_t arg_from_name; /* for when name is overloaded as a variable-length integer
(FST_AT_MISC + FST_MT_SOURCESTEM) */
uint32_t name_length; /* strlen(u.attr.name) */
} attr;
} u;
};
struct fstETab
{
char *name;
uint32_t elem_count;
char **literal_arr;
char **val_arr;
};
/*
* writer functions
*/
typedef struct fstWriterContext fstWriterContext;
void fstWriterClose(fstWriterContext *ctx);
fstWriterContext *fstWriterCreate(const char *nam, int use_compressed_hier);
fstEnumHandle fstWriterCreateEnumTable(fstWriterContext *ctx,
const char *name,
uint32_t elem_count,
unsigned int min_valbits,
const char **literal_arr,
const char **val_arr);
/* used for Verilog/SV */
fstHandle fstWriterCreateVar(fstWriterContext *ctx,
enum fstVarType vt,
enum fstVarDir vd,
uint32_t len,
const char *nam,
fstHandle aliasHandle);
/* future expansion for VHDL and other languages. The variable type, data type, etc map onto
the current Verilog/SV one. The "type" string is optional for a more verbose or custom
description */
fstHandle fstWriterCreateVar2(fstWriterContext *ctx,
enum fstVarType vt,
enum fstVarDir vd,
uint32_t len,
const char *nam,
fstHandle aliasHandle,
const char *type,
enum fstSupplementalVarType svt,
enum fstSupplementalDataType sdt);
void fstWriterEmitDumpActive(fstWriterContext *ctx, int enable);
void fstWriterEmitEnumTableRef(fstWriterContext *ctx, fstEnumHandle handle);
void fstWriterEmitValueChange(fstWriterContext *ctx, fstHandle handle, const void *val);
void fstWriterEmitValueChange32(fstWriterContext *ctx,
fstHandle handle,
uint32_t bits,
uint32_t val);
void fstWriterEmitValueChange64(fstWriterContext *ctx,
fstHandle handle,
uint32_t bits,
uint64_t val);
void fstWriterEmitValueChangeVec32(fstWriterContext *ctx,
fstHandle handle,
uint32_t bits,
const uint32_t *val);
void fstWriterEmitValueChangeVec64(fstWriterContext *ctx,
fstHandle handle,
uint32_t bits,
const uint64_t *val);
void fstWriterEmitVariableLengthValueChange(fstWriterContext *ctx,
fstHandle handle,
const void *val,
uint32_t len);
void fstWriterEmitTimeChange(fstWriterContext *ctx, uint64_t tim);
void fstWriterFlushContext(fstWriterContext *ctx);
int fstWriterGetDumpSizeLimitReached(fstWriterContext *ctx);
int fstWriterGetFseekFailed(fstWriterContext *ctx);
int fstWriterGetFlushContextPending(fstWriterContext *ctx);
void fstWriterSetAttrBegin(fstWriterContext *ctx,
enum fstAttrType attrtype,
int subtype,
const char *attrname,
uint64_t arg);
void fstWriterSetAttrEnd(fstWriterContext *ctx);
void fstWriterSetComment(fstWriterContext *ctx, const char *comm);
void fstWriterSetDate(fstWriterContext *ctx, const char *dat);
void fstWriterSetDumpSizeLimit(fstWriterContext *ctx, uint64_t numbytes);
void fstWriterSetEnvVar(fstWriterContext *ctx, const char *envvar);
void fstWriterSetFileType(fstWriterContext *ctx, enum fstFileType filetype);
void fstWriterSetPackType(fstWriterContext *ctx, enum fstWriterPackType typ);
void fstWriterSetParallelMode(fstWriterContext *ctx, int enable);
void fstWriterSetRepackOnClose(fstWriterContext *ctx,
int enable); /* type = 0 (none), 1 (libz) */
void fstWriterSetScope(fstWriterContext *ctx,
enum fstScopeType scopetype,
const char *scopename,
const char *scopecomp);
void fstWriterSetSourceInstantiationStem(fstWriterContext *ctx,
const char *path,
unsigned int line,
unsigned int use_realpath);
void fstWriterSetSourceStem(fstWriterContext *ctx,
const char *path,
unsigned int line,
unsigned int use_realpath);
void fstWriterSetTimescale(fstWriterContext *ctx, int ts);
void fstWriterSetTimescaleFromString(fstWriterContext *ctx, const char *s);
void fstWriterSetTimezero(fstWriterContext *ctx, int64_t tim);
void fstWriterSetUpscope(fstWriterContext *ctx);
void fstWriterSetValueList(fstWriterContext *ctx, const char *vl);
void fstWriterSetVersion(fstWriterContext *ctx, const char *vers);
/*
* reader functions
*/
typedef struct fstReaderContext fstReaderContext;
void fstReaderClose(fstReaderContext *ctx);
void fstReaderClrFacProcessMask(fstReaderContext *ctx, fstHandle facidx);
void fstReaderClrFacProcessMaskAll(fstReaderContext *ctx);
uint64_t fstReaderGetAliasCount(fstReaderContext *ctx);
const char *fstReaderGetCurrentFlatScope(fstReaderContext *ctx);
void *fstReaderGetCurrentScopeUserInfo(fstReaderContext *ctx);
int fstReaderGetCurrentScopeLen(fstReaderContext *ctx);
const char *fstReaderGetDateString(fstReaderContext *ctx);
int fstReaderGetDoubleEndianMatchState(fstReaderContext *ctx);
uint64_t fstReaderGetDumpActivityChangeTime(fstReaderContext *ctx, uint32_t idx);
unsigned char fstReaderGetDumpActivityChangeValue(fstReaderContext *ctx, uint32_t idx);
uint64_t fstReaderGetEndTime(fstReaderContext *ctx);
int fstReaderGetFacProcessMask(fstReaderContext *ctx, fstHandle facidx);
int fstReaderGetFileType(fstReaderContext *ctx);
int fstReaderGetFseekFailed(fstReaderContext *ctx);
fstHandle fstReaderGetMaxHandle(fstReaderContext *ctx);
uint64_t fstReaderGetMemoryUsedByWriter(fstReaderContext *ctx);
uint32_t fstReaderGetNumberDumpActivityChanges(fstReaderContext *ctx);
uint64_t fstReaderGetScopeCount(fstReaderContext *ctx);
uint64_t fstReaderGetStartTime(fstReaderContext *ctx);
signed char fstReaderGetTimescale(fstReaderContext *ctx);
int64_t fstReaderGetTimezero(fstReaderContext *ctx);
uint64_t fstReaderGetValueChangeSectionCount(fstReaderContext *ctx);
char *fstReaderGetValueFromHandleAtTime(fstReaderContext *ctx,
uint64_t tim,
fstHandle facidx,
char *buf);
uint64_t fstReaderGetVarCount(fstReaderContext *ctx);
const char *fstReaderGetVersionString(fstReaderContext *ctx);
struct fstHier *fstReaderIterateHier(fstReaderContext *ctx);
int fstReaderIterateHierRewind(fstReaderContext *ctx);
int fstReaderIterBlocks(fstReaderContext *ctx,
void (*value_change_callback)(void *user_callback_data_pointer,
uint64_t time,
fstHandle facidx,
const unsigned char *value),
void *user_callback_data_pointer,
FILE *vcdhandle);
int fstReaderIterBlocks2(fstReaderContext *ctx,
void (*value_change_callback)(void *user_callback_data_pointer,
uint64_t time,
fstHandle facidx,
const unsigned char *value),
void (*value_change_callback_varlen)(void *user_callback_data_pointer,
uint64_t time,
fstHandle facidx,
const unsigned char *value,
uint32_t len),
void *user_callback_data_pointer,
FILE *vcdhandle);
void fstReaderIterBlocksSetNativeDoublesOnCallback(fstReaderContext *ctx, int enable);
fstReaderContext *fstReaderOpen(const char *nam);
fstReaderContext *fstReaderOpenForUtilitiesOnly(void);
const char *fstReaderPopScope(fstReaderContext *ctx);
int fstReaderProcessHier(fstReaderContext *ctx, FILE *vcdhandle);
const char *fstReaderPushScope(fstReaderContext *ctx, const char *nam, void *user_info);
void fstReaderResetScope(fstReaderContext *ctx);
void fstReaderSetFacProcessMask(fstReaderContext *ctx, fstHandle facidx);
void fstReaderSetFacProcessMaskAll(fstReaderContext *ctx);
void fstReaderSetLimitTimeRange(fstReaderContext *ctx, uint64_t start_time, uint64_t end_time);
void fstReaderSetUnlimitedTimeRange(fstReaderContext *ctx);
void fstReaderSetVcdExtensions(fstReaderContext *ctx, int enable);
/*
* utility functions
*/
int fstUtilityBinToEscConvertedLen(const unsigned char *s, int len); /* used for mallocs for fstUtilityBinToEsc() */
int fstUtilityBinToEsc(unsigned char *d, const unsigned char *s, int len);
int fstUtilityEscToBin(unsigned char *d, unsigned char *s, int len);
struct fstETab *fstUtilityExtractEnumTableFromString(const char *s);
void fstUtilityFreeEnumTable(struct fstETab *etab); /* must use to free fstETab properly */
#ifdef __cplusplus
}
#endif
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,868 +0,0 @@
/*
* LZ4 - Fast LZ compression algorithm
* Header File
* Copyright (C) 2011-2023, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You can contact the author at :
- LZ4 homepage : http://www.lz4.org
- LZ4 source repository : https://github.com/lz4/lz4
*/
#if defined (__cplusplus)
extern "C" {
#endif
#ifndef LZ4_H_2983827168210
#define LZ4_H_2983827168210
/* --- Dependency --- */
#include <stddef.h> /* size_t */
/**
Introduction
LZ4 is lossless compression algorithm, providing compression speed >500 MB/s per core,
scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
The LZ4 compression library provides in-memory compression and decompression functions.
It gives full buffer control to user.
Compression can be done in:
- a single step (described as Simple Functions)
- a single step, reusing a context (described in Advanced Functions)
- unbounded multiple steps (described as Streaming compression)
lz4.h generates and decodes LZ4-compressed blocks (doc/lz4_Block_format.md).
Decompressing such a compressed block requires additional metadata.
Exact metadata depends on exact decompression function.
For the typical case of LZ4_decompress_safe(),
metadata includes block's compressed size, and maximum bound of decompressed size.
Each application is free to encode and pass such metadata in whichever way it wants.
lz4.h only handle blocks, it can not generate Frames.
Blocks are different from Frames (doc/lz4_Frame_format.md).
Frames bundle both blocks and metadata in a specified manner.
Embedding metadata is required for compressed data to be self-contained and portable.
Frame format is delivered through a companion API, declared in lz4frame.h.
The `lz4` CLI can only manage frames.
*/
/*^***************************************************************
* Export parameters
*****************************************************************/
/*
* LZ4_DLL_EXPORT :
* Enable exporting of functions when building a Windows DLL
* LZ4LIB_VISIBILITY :
* Control library symbols visibility.
*/
#ifndef LZ4LIB_VISIBILITY
# if defined(__GNUC__) && (__GNUC__ >= 4)
# define LZ4LIB_VISIBILITY __attribute__ ((visibility ("default")))
# else
# define LZ4LIB_VISIBILITY
# endif
#endif
#if defined(LZ4_DLL_EXPORT) && (LZ4_DLL_EXPORT==1)
# define LZ4LIB_API __declspec(dllexport) LZ4LIB_VISIBILITY
#elif defined(LZ4_DLL_IMPORT) && (LZ4_DLL_IMPORT==1)
# define LZ4LIB_API __declspec(dllimport) LZ4LIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
#else
# define LZ4LIB_API LZ4LIB_VISIBILITY
#endif
/*! LZ4_FREESTANDING :
* When this macro is set to 1, it enables "freestanding mode" that is
* suitable for typical freestanding environment which doesn't support
* standard C library.
*
* - LZ4_FREESTANDING is a compile-time switch.
* - It requires the following macros to be defined:
* LZ4_memcpy, LZ4_memmove, LZ4_memset.
* - It only enables LZ4/HC functions which don't use heap.
* All LZ4F_* functions are not supported.
* - See tests/freestanding.c to check its basic setup.
*/
#if defined(LZ4_FREESTANDING) && (LZ4_FREESTANDING == 1)
# define LZ4_HEAPMODE 0
# define LZ4HC_HEAPMODE 0
# define LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION 1
# if !defined(LZ4_memcpy)
# error "LZ4_FREESTANDING requires macro 'LZ4_memcpy'."
# endif
# if !defined(LZ4_memset)
# error "LZ4_FREESTANDING requires macro 'LZ4_memset'."
# endif
# if !defined(LZ4_memmove)
# error "LZ4_FREESTANDING requires macro 'LZ4_memmove'."
# endif
#elif ! defined(LZ4_FREESTANDING)
# define LZ4_FREESTANDING 0
#endif
/*------ Version ------*/
#define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
#define LZ4_VERSION_MINOR 9 /* for new (non-breaking) interface capabilities */
#define LZ4_VERSION_RELEASE 5 /* for tweaks, bug-fixes, or development */
#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
#define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
#define LZ4_QUOTE(str) #str
#define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
#define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION) /* requires v1.7.3+ */
LZ4LIB_API int LZ4_versionNumber (void); /**< library version number; useful to check dll version; requires v1.3.0+ */
LZ4LIB_API const char* LZ4_versionString (void); /**< library version string; useful to check dll version; requires v1.7.5+ */
/*-************************************
* Tuning memory usage
**************************************/
/*!
* LZ4_MEMORY_USAGE :
* Memory usage formula : N->2^N Bytes (examples : 10 -> 1KB; 12 -> 4KB ; 16 -> 64KB; 20 -> 1MB)
* Increasing memory usage improves compression ratio, generally at the cost of speed.
* Reduced memory usage may improve speed at the cost of ratio, thanks to better cache locality.
* Default value is 14, for 16KB, which nicely fits into most L1 caches.
*/
#ifndef LZ4_MEMORY_USAGE
# define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT
#endif
#define LZ4_MEMORY_USAGE_MIN 10
#define LZ4_MEMORY_USAGE_DEFAULT 14
#define LZ4_MEMORY_USAGE_MAX 20
#if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN)
# error "LZ4_MEMORY_USAGE is too small !"
#endif
#if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX)
# error "LZ4_MEMORY_USAGE is too large !"
#endif
/*-************************************
* Simple Functions
**************************************/
/*! LZ4_compress_default() :
* Compresses 'srcSize' bytes from buffer 'src'
* into already allocated 'dst' buffer of size 'dstCapacity'.
* Compression is guaranteed to succeed if 'dstCapacity' >= LZ4_compressBound(srcSize).
* It also runs faster, so it's a recommended setting.
* If the function cannot compress 'src' into a more limited 'dst' budget,
* compression stops *immediately*, and the function result is zero.
* In which case, 'dst' content is undefined (invalid).
* srcSize : max supported value is LZ4_MAX_INPUT_SIZE.
* dstCapacity : size of buffer 'dst' (which must be already allocated)
* @return : the number of bytes written into buffer 'dst' (necessarily <= dstCapacity)
* or 0 if compression fails
* Note : This function is protected against buffer overflow scenarios (never writes outside 'dst' buffer, nor read outside 'source' buffer).
*/
LZ4LIB_API int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity);
/*! LZ4_decompress_safe() :
* @compressedSize : is the exact complete size of the compressed block.
* @dstCapacity : is the size of destination buffer (which must be already allocated),
* presumed an upper bound of decompressed size.
* @return : the number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
* If destination buffer is not large enough, decoding will stop and output an error code (negative value).
* If the source stream is detected malformed, the function will stop decoding and return a negative result.
* Note 1 : This function is protected against malicious data packets :
* it will never writes outside 'dst' buffer, nor read outside 'source' buffer,
* even if the compressed block is maliciously modified to order the decoder to do these actions.
* In such case, the decoder stops immediately, and considers the compressed block malformed.
* Note 2 : compressedSize and dstCapacity must be provided to the function, the compressed block does not contain them.
* The implementation is free to send / store / derive this information in whichever way is most beneficial.
* If there is a need for a different format which bundles together both compressed data and its metadata, consider looking at lz4frame.h instead.
*/
LZ4LIB_API int LZ4_decompress_safe (const char* src, char* dst, int compressedSize, int dstCapacity);
/*-************************************
* Advanced Functions
**************************************/
#define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
#define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
/*! LZ4_compressBound() :
Provides the maximum size that LZ4 compression may output in a "worst case" scenario (input data not compressible)
This function is primarily useful for memory allocation purposes (destination buffer size).
Macro LZ4_COMPRESSBOUND() is also provided for compilation-time evaluation (stack memory allocation for example).
Note that LZ4_compress_default() compresses faster when dstCapacity is >= LZ4_compressBound(srcSize)
inputSize : max supported value is LZ4_MAX_INPUT_SIZE
return : maximum output size in a "worst case" scenario
or 0, if input size is incorrect (too large or negative)
*/
LZ4LIB_API int LZ4_compressBound(int inputSize);
/*! LZ4_compress_fast() :
Same as LZ4_compress_default(), but allows selection of "acceleration" factor.
The larger the acceleration value, the faster the algorithm, but also the lesser the compression.
It's a trade-off. It can be fine tuned, with each successive value providing roughly +~3% to speed.
An acceleration value of "1" is the same as regular LZ4_compress_default()
Values <= 0 will be replaced by LZ4_ACCELERATION_DEFAULT (currently == 1, see lz4.c).
Values > LZ4_ACCELERATION_MAX will be replaced by LZ4_ACCELERATION_MAX (currently == 65537, see lz4.c).
*/
LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
/*! LZ4_compress_fast_extState() :
* Same as LZ4_compress_fast(), using an externally allocated memory space for its state.
* Use LZ4_sizeofState() to know how much memory must be allocated,
* and allocate it on 8-bytes boundaries (using `malloc()` typically).
* Then, provide this buffer as `void* state` to compression function.
*/
LZ4LIB_API int LZ4_sizeofState(void);
LZ4LIB_API int LZ4_compress_fast_extState (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
/*! LZ4_compress_destSize() :
* Reverse the logic : compresses as much data as possible from 'src' buffer
* into already allocated buffer 'dst', of size >= 'dstCapacity'.
* This function either compresses the entire 'src' content into 'dst' if it's large enough,
* or fill 'dst' buffer completely with as much data as possible from 'src'.
* note: acceleration parameter is fixed to "default".
*
* *srcSizePtr : in+out parameter. Initially contains size of input.
* Will be modified to indicate how many bytes where read from 'src' to fill 'dst'.
* New value is necessarily <= input value.
* @return : Nb bytes written into 'dst' (necessarily <= dstCapacity)
* or 0 if compression fails.
*
* Note : from v1.8.2 to v1.9.1, this function had a bug (fixed in v1.9.2+):
* the produced compressed content could, in specific circumstances,
* require to be decompressed into a destination buffer larger
* by at least 1 byte than the content to decompress.
* If an application uses `LZ4_compress_destSize()`,
* it's highly recommended to update liblz4 to v1.9.2 or better.
* If this can't be done or ensured,
* the receiving decompression function should provide
* a dstCapacity which is > decompressedSize, by at least 1 byte.
* See https://github.com/lz4/lz4/issues/859 for details
*/
LZ4LIB_API int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize);
/*! LZ4_decompress_safe_partial() :
* Decompress an LZ4 compressed block, of size 'srcSize' at position 'src',
* into destination buffer 'dst' of size 'dstCapacity'.
* Up to 'targetOutputSize' bytes will be decoded.
* The function stops decoding on reaching this objective.
* This can be useful to boost performance
* whenever only the beginning of a block is required.
*
* @return : the number of bytes decoded in `dst` (necessarily <= targetOutputSize)
* If source stream is detected malformed, function returns a negative result.
*
* Note 1 : @return can be < targetOutputSize, if compressed block contains less data.
*
* Note 2 : targetOutputSize must be <= dstCapacity
*
* Note 3 : this function effectively stops decoding on reaching targetOutputSize,
* so dstCapacity is kind of redundant.
* This is because in older versions of this function,
* decoding operation would still write complete sequences.
* Therefore, there was no guarantee that it would stop writing at exactly targetOutputSize,
* it could write more bytes, though only up to dstCapacity.
* Some "margin" used to be required for this operation to work properly.
* Thankfully, this is no longer necessary.
* The function nonetheless keeps the same signature, in an effort to preserve API compatibility.
*
* Note 4 : If srcSize is the exact size of the block,
* then targetOutputSize can be any value,
* including larger than the block's decompressed size.
* The function will, at most, generate block's decompressed size.
*
* Note 5 : If srcSize is _larger_ than block's compressed size,
* then targetOutputSize **MUST** be <= block's decompressed size.
* Otherwise, *silent corruption will occur*.
*/
LZ4LIB_API int LZ4_decompress_safe_partial (const char* src, char* dst, int srcSize, int targetOutputSize, int dstCapacity);
/*-*********************************************
* Streaming Compression Functions
***********************************************/
typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
/*!
Note about RC_INVOKED
- RC_INVOKED is predefined symbol of rc.exe (the resource compiler which is part of MSVC/Visual Studio).
https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros
- Since rc.exe is a legacy compiler, it truncates long symbol (> 30 chars)
and reports warning "RC4011: identifier truncated".
- To eliminate the warning, we surround long preprocessor symbol with
"#if !defined(RC_INVOKED) ... #endif" block that means
"skip this block when rc.exe is trying to read it".
*/
#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
LZ4LIB_API LZ4_stream_t* LZ4_createStream(void);
LZ4LIB_API int LZ4_freeStream (LZ4_stream_t* streamPtr);
#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */
#endif
/*! LZ4_resetStream_fast() : v1.9.0+
* Use this to prepare an LZ4_stream_t for a new chain of dependent blocks
* (e.g., LZ4_compress_fast_continue()).
*
* An LZ4_stream_t must be initialized once before usage.
* This is automatically done when created by LZ4_createStream().
* However, should the LZ4_stream_t be simply declared on stack (for example),
* it's necessary to initialize it first, using LZ4_initStream().
*
* After init, start any new stream with LZ4_resetStream_fast().
* A same LZ4_stream_t can be re-used multiple times consecutively
* and compress multiple streams,
* provided that it starts each new stream with LZ4_resetStream_fast().
*
* LZ4_resetStream_fast() is much faster than LZ4_initStream(),
* but is not compatible with memory regions containing garbage data.
*
* Note: it's only useful to call LZ4_resetStream_fast()
* in the context of streaming compression.
* The *extState* functions perform their own resets.
* Invoking LZ4_resetStream_fast() before is redundant, and even counterproductive.
*/
LZ4LIB_API void LZ4_resetStream_fast (LZ4_stream_t* streamPtr);
/*! LZ4_loadDict() :
* Use this function to reference a static dictionary into LZ4_stream_t.
* The dictionary must remain available during compression.
* LZ4_loadDict() triggers a reset, so any previous data will be forgotten.
* The same dictionary will have to be loaded on decompression side for successful decoding.
* Dictionary are useful for better compression of small data (KB range).
* While LZ4 itself accepts any input as dictionary, dictionary efficiency is also a topic.
* When in doubt, employ the Zstandard's Dictionary Builder.
* Loading a size of 0 is allowed, and is the same as reset.
* @return : loaded dictionary size, in bytes (note: only the last 64 KB are loaded)
*/
LZ4LIB_API int LZ4_loadDict (LZ4_stream_t* streamPtr, const char* dictionary, int dictSize);
/*! LZ4_compress_fast_continue() :
* Compress 'src' content using data from previously compressed blocks, for better compression ratio.
* 'dst' buffer must be already allocated.
* If dstCapacity >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster.
*
* @return : size of compressed block
* or 0 if there is an error (typically, cannot fit into 'dst').
*
* Note 1 : Each invocation to LZ4_compress_fast_continue() generates a new block.
* Each block has precise boundaries.
* Each block must be decompressed separately, calling LZ4_decompress_*() with relevant metadata.
* It's not possible to append blocks together and expect a single invocation of LZ4_decompress_*() to decompress them together.
*
* Note 2 : The previous 64KB of source data is __assumed__ to remain present, unmodified, at same address in memory !
*
* Note 3 : When input is structured as a double-buffer, each buffer can have any size, including < 64 KB.
* Make sure that buffers are separated, by at least one byte.
* This construction ensures that each block only depends on previous block.
*
* Note 4 : If input buffer is a ring-buffer, it can have any size, including < 64 KB.
*
* Note 5 : After an error, the stream status is undefined (invalid), it can only be reset or freed.
*/
LZ4LIB_API int LZ4_compress_fast_continue (LZ4_stream_t* streamPtr, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
/*! LZ4_saveDict() :
* If last 64KB data cannot be guaranteed to remain available at its current memory location,
* save it into a safer place (char* safeBuffer).
* This is schematically equivalent to a memcpy() followed by LZ4_loadDict(),
* but is much faster, because LZ4_saveDict() doesn't need to rebuild tables.
* @return : saved dictionary size in bytes (necessarily <= maxDictSize), or 0 if error.
*/
LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize);
/*-**********************************************
* Streaming Decompression Functions
* Bufferless synchronous API
************************************************/
typedef union LZ4_streamDecode_u LZ4_streamDecode_t; /* tracking context */
/*! LZ4_createStreamDecode() and LZ4_freeStreamDecode() :
* creation / destruction of streaming decompression tracking context.
* A tracking context can be re-used multiple times.
*/
#if !defined(RC_INVOKED) /* https://docs.microsoft.com/en-us/windows/win32/menurc/predefined-macros */
#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION)
LZ4LIB_API LZ4_streamDecode_t* LZ4_createStreamDecode(void);
LZ4LIB_API int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream);
#endif /* !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) */
#endif
/*! LZ4_setStreamDecode() :
* An LZ4_streamDecode_t context can be allocated once and re-used multiple times.
* Use this function to start decompression of a new stream of blocks.
* A dictionary can optionally be set. Use NULL or size 0 for a reset order.
* Dictionary is presumed stable : it must remain accessible and unmodified during next decompression.
* @return : 1 if OK, 0 if error
*/
LZ4LIB_API int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize);
/*! LZ4_decoderRingBufferSize() : v1.8.2+
* Note : in a ring buffer scenario (optional),
* blocks are presumed decompressed next to each other
* up to the moment there is not enough remaining space for next block (remainingSize < maxBlockSize),
* at which stage it resumes from beginning of ring buffer.
* When setting such a ring buffer for streaming decompression,
* provides the minimum size of this ring buffer
* to be compatible with any source respecting maxBlockSize condition.
* @return : minimum ring buffer size,
* or 0 if there is an error (invalid maxBlockSize).
*/
LZ4LIB_API int LZ4_decoderRingBufferSize(int maxBlockSize);
#define LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize) (65536 + 14 + (maxBlockSize)) /* for static allocation; maxBlockSize presumed valid */
/*! LZ4_decompress_safe_continue() :
* This decoding function allows decompression of consecutive blocks in "streaming" mode.
* The difference with the usual independent blocks is that
* new blocks are allowed to find references into former blocks.
* A block is an unsplittable entity, and must be presented entirely to the decompression function.
* LZ4_decompress_safe_continue() only accepts one block at a time.
* It's modeled after `LZ4_decompress_safe()` and behaves similarly.
*
* @LZ4_streamDecode : decompression state, tracking the position in memory of past data
* @compressedSize : exact complete size of one compressed block.
* @dstCapacity : size of destination buffer (which must be already allocated),
* must be an upper bound of decompressed size.
* @return : number of bytes decompressed into destination buffer (necessarily <= dstCapacity)
* If destination buffer is not large enough, decoding will stop and output an error code (negative value).
* If the source stream is detected malformed, the function will stop decoding and return a negative result.
*
* The last 64KB of previously decoded data *must* remain available and unmodified
* at the memory position where they were previously decoded.
* If less than 64KB of data has been decoded, all the data must be present.
*
* Special : if decompression side sets a ring buffer, it must respect one of the following conditions :
* - Decompression buffer size is _at least_ LZ4_decoderRingBufferSize(maxBlockSize).
* maxBlockSize is the maximum size of any single block. It can have any value > 16 bytes.
* In which case, encoding and decoding buffers do not need to be synchronized.
* Actually, data can be produced by any source compliant with LZ4 format specification, and respecting maxBlockSize.
* - Synchronized mode :
* Decompression buffer size is _exactly_ the same as compression buffer size,
* and follows exactly same update rule (block boundaries at same positions),
* and decoding function is provided with exact decompressed size of each block (exception for last block of the stream),
* _then_ decoding & encoding ring buffer can have any size, including small ones ( < 64 KB).
* - Decompression buffer is larger than encoding buffer, by a minimum of maxBlockSize more bytes.
* In which case, encoding and decoding buffers do not need to be synchronized,
* and encoding ring buffer can have any size, including small ones ( < 64 KB).
*
* Whenever these conditions are not possible,
* save the last 64KB of decoded data into a safe buffer where it can't be modified during decompression,
* then indicate where this data is saved using LZ4_setStreamDecode(), before decompressing next block.
*/
LZ4LIB_API int
LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode,
const char* src, char* dst,
int srcSize, int dstCapacity);
/*! LZ4_decompress_safe_usingDict() :
* Works the same as
* a combination of LZ4_setStreamDecode() followed by LZ4_decompress_safe_continue()
* However, it's stateless: it doesn't need any LZ4_streamDecode_t state.
* Dictionary is presumed stable : it must remain accessible and unmodified during decompression.
* Performance tip : Decompression speed can be substantially increased
* when dst == dictStart + dictSize.
*/
LZ4LIB_API int
LZ4_decompress_safe_usingDict(const char* src, char* dst,
int srcSize, int dstCapacity,
const char* dictStart, int dictSize);
/*! LZ4_decompress_safe_partial_usingDict() :
* Behaves the same as LZ4_decompress_safe_partial()
* with the added ability to specify a memory segment for past data.
* Performance tip : Decompression speed can be substantially increased
* when dst == dictStart + dictSize.
*/
LZ4LIB_API int
LZ4_decompress_safe_partial_usingDict(const char* src, char* dst,
int compressedSize,
int targetOutputSize, int maxOutputSize,
const char* dictStart, int dictSize);
#endif /* LZ4_H_2983827168210 */
/*^*************************************
* !!!!!! STATIC LINKING ONLY !!!!!!
***************************************/
/*-****************************************************************************
* Experimental section
*
* Symbols declared in this section must be considered unstable. Their
* signatures or semantics may change, or they may be removed altogether in the
* future. They are therefore only safe to depend on when the caller is
* statically linked against the library.
*
* To protect against unsafe usage, not only are the declarations guarded,
* the definitions are hidden by default
* when building LZ4 as a shared/dynamic library.
*
* In order to access these declarations,
* define LZ4_STATIC_LINKING_ONLY in your application
* before including LZ4's headers.
*
* In order to make their implementations accessible dynamically, you must
* define LZ4_PUBLISH_STATIC_FUNCTIONS when building the LZ4 library.
******************************************************************************/
#ifdef LZ4_STATIC_LINKING_ONLY
#ifndef LZ4_STATIC_3504398509
#define LZ4_STATIC_3504398509
#ifdef LZ4_PUBLISH_STATIC_FUNCTIONS
# define LZ4LIB_STATIC_API LZ4LIB_API
#else
# define LZ4LIB_STATIC_API
#endif
/*! LZ4_compress_fast_extState_fastReset() :
* A variant of LZ4_compress_fast_extState().
*
* Using this variant avoids an expensive initialization step.
* It is only safe to call if the state buffer is known to be correctly initialized already
* (see above comment on LZ4_resetStream_fast() for a definition of "correctly initialized").
* From a high level, the difference is that
* this function initializes the provided state with a call to something like LZ4_resetStream_fast()
* while LZ4_compress_fast_extState() starts with a call to LZ4_resetStream().
*/
LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration);
/*! LZ4_compress_destSize_extState() :
* Same as LZ4_compress_destSize(), but using an externally allocated state.
* Also: exposes @acceleration
*/
int LZ4_compress_destSize_extState(void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration);
/*! LZ4_attach_dictionary() :
* This is an experimental API that allows
* efficient use of a static dictionary many times.
*
* Rather than re-loading the dictionary buffer into a working context before
* each compression, or copying a pre-loaded dictionary's LZ4_stream_t into a
* working LZ4_stream_t, this function introduces a no-copy setup mechanism,
* in which the working stream references the dictionary stream in-place.
*
* Several assumptions are made about the state of the dictionary stream.
* Currently, only streams which have been prepared by LZ4_loadDict() should
* be expected to work.
*
* Alternatively, the provided dictionaryStream may be NULL,
* in which case any existing dictionary stream is unset.
*
* If a dictionary is provided, it replaces any pre-existing stream history.
* The dictionary contents are the only history that can be referenced and
* logically immediately precede the data compressed in the first subsequent
* compression call.
*
* The dictionary will only remain attached to the working stream through the
* first compression call, at the end of which it is cleared. The dictionary
* stream (and source buffer) must remain in-place / accessible / unchanged
* through the completion of the first compression call on the stream.
*/
LZ4LIB_STATIC_API void
LZ4_attach_dictionary(LZ4_stream_t* workingStream,
const LZ4_stream_t* dictionaryStream);
/*! In-place compression and decompression
*
* It's possible to have input and output sharing the same buffer,
* for highly constrained memory environments.
* In both cases, it requires input to lay at the end of the buffer,
* and decompression to start at beginning of the buffer.
* Buffer size must feature some margin, hence be larger than final size.
*
* |<------------------------buffer--------------------------------->|
* |<-----------compressed data--------->|
* |<-----------decompressed size------------------>|
* |<----margin---->|
*
* This technique is more useful for decompression,
* since decompressed size is typically larger,
* and margin is short.
*
* In-place decompression will work inside any buffer
* which size is >= LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize).
* This presumes that decompressedSize > compressedSize.
* Otherwise, it means compression actually expanded data,
* and it would be more efficient to store such data with a flag indicating it's not compressed.
* This can happen when data is not compressible (already compressed, or encrypted).
*
* For in-place compression, margin is larger, as it must be able to cope with both
* history preservation, requiring input data to remain unmodified up to LZ4_DISTANCE_MAX,
* and data expansion, which can happen when input is not compressible.
* As a consequence, buffer size requirements are much higher,
* and memory savings offered by in-place compression are more limited.
*
* There are ways to limit this cost for compression :
* - Reduce history size, by modifying LZ4_DISTANCE_MAX.
* Note that it is a compile-time constant, so all compressions will apply this limit.
* Lower values will reduce compression ratio, except when input_size < LZ4_DISTANCE_MAX,
* so it's a reasonable trick when inputs are known to be small.
* - Require the compressor to deliver a "maximum compressed size".
* This is the `dstCapacity` parameter in `LZ4_compress*()`.
* When this size is < LZ4_COMPRESSBOUND(inputSize), then compression can fail,
* in which case, the return code will be 0 (zero).
* The caller must be ready for these cases to happen,
* and typically design a backup scheme to send data uncompressed.
* The combination of both techniques can significantly reduce
* the amount of margin required for in-place compression.
*
* In-place compression can work in any buffer
* which size is >= (maxCompressedSize)
* with maxCompressedSize == LZ4_COMPRESSBOUND(srcSize) for guaranteed compression success.
* LZ4_COMPRESS_INPLACE_BUFFER_SIZE() depends on both maxCompressedSize and LZ4_DISTANCE_MAX,
* so it's possible to reduce memory requirements by playing with them.
*/
#define LZ4_DECOMPRESS_INPLACE_MARGIN(compressedSize) (((compressedSize) >> 8) + 32)
#define LZ4_DECOMPRESS_INPLACE_BUFFER_SIZE(decompressedSize) ((decompressedSize) + LZ4_DECOMPRESS_INPLACE_MARGIN(decompressedSize)) /**< note: presumes that compressedSize < decompressedSize. note2: margin is overestimated a bit, since it could use compressedSize instead */
#ifndef LZ4_DISTANCE_MAX /* history window size; can be user-defined at compile time */
# define LZ4_DISTANCE_MAX 65535 /* set to maximum value by default */
#endif
#define LZ4_COMPRESS_INPLACE_MARGIN (LZ4_DISTANCE_MAX + 32) /* LZ4_DISTANCE_MAX can be safely replaced by srcSize when it's smaller */
#define LZ4_COMPRESS_INPLACE_BUFFER_SIZE(maxCompressedSize) ((maxCompressedSize) + LZ4_COMPRESS_INPLACE_MARGIN) /**< maxCompressedSize is generally LZ4_COMPRESSBOUND(inputSize), but can be set to any lower value, with the risk that compression can fail (return code 0(zero)) */
#endif /* LZ4_STATIC_3504398509 */
#endif /* LZ4_STATIC_LINKING_ONLY */
#ifndef LZ4_H_98237428734687
#define LZ4_H_98237428734687
/*-************************************************************
* Private Definitions
**************************************************************
* Do not use these definitions directly.
* They are only exposed to allow static allocation of `LZ4_stream_t` and `LZ4_streamDecode_t`.
* Accessing members will expose user code to API and/or ABI break in future versions of the library.
**************************************************************/
#define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
#define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
#define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
# include <stdint.h>
typedef int8_t LZ4_i8;
typedef uint8_t LZ4_byte;
typedef uint16_t LZ4_u16;
typedef uint32_t LZ4_u32;
#else
typedef signed char LZ4_i8;
typedef unsigned char LZ4_byte;
typedef unsigned short LZ4_u16;
typedef unsigned int LZ4_u32;
#endif
/*! LZ4_stream_t :
* Never ever use below internal definitions directly !
* These definitions are not API/ABI safe, and may change in future versions.
* If you need static allocation, declare or allocate an LZ4_stream_t object.
**/
typedef struct LZ4_stream_t_internal LZ4_stream_t_internal;
struct LZ4_stream_t_internal {
LZ4_u32 hashTable[LZ4_HASH_SIZE_U32];
const LZ4_byte* dictionary;
const LZ4_stream_t_internal* dictCtx;
LZ4_u32 currentOffset;
LZ4_u32 tableType;
LZ4_u32 dictSize;
/* Implicit padding to ensure structure is aligned */
};
#define LZ4_STREAM_MINSIZE ((1UL << (LZ4_MEMORY_USAGE)) + 32) /* static size, for inter-version compatibility */
union LZ4_stream_u {
char minStateSize[LZ4_STREAM_MINSIZE];
LZ4_stream_t_internal internal_donotuse;
}; /* previously typedef'd to LZ4_stream_t */
/*! LZ4_initStream() : v1.9.0+
* An LZ4_stream_t structure must be initialized at least once.
* This is automatically done when invoking LZ4_createStream(),
* but it's not when the structure is simply declared on stack (for example).
*
* Use LZ4_initStream() to properly initialize a newly declared LZ4_stream_t.
* It can also initialize any arbitrary buffer of sufficient size,
* and will @return a pointer of proper type upon initialization.
*
* Note : initialization fails if size and alignment conditions are not respected.
* In which case, the function will @return NULL.
* Note2: An LZ4_stream_t structure guarantees correct alignment and size.
* Note3: Before v1.9.0, use LZ4_resetStream() instead
**/
LZ4LIB_API LZ4_stream_t* LZ4_initStream (void* stateBuffer, size_t size);
/*! LZ4_streamDecode_t :
* Never ever use below internal definitions directly !
* These definitions are not API/ABI safe, and may change in future versions.
* If you need static allocation, declare or allocate an LZ4_streamDecode_t object.
**/
typedef struct {
const LZ4_byte* externalDict;
const LZ4_byte* prefixEnd;
size_t extDictSize;
size_t prefixSize;
} LZ4_streamDecode_t_internal;
#define LZ4_STREAMDECODE_MINSIZE 32
union LZ4_streamDecode_u {
char minStateSize[LZ4_STREAMDECODE_MINSIZE];
LZ4_streamDecode_t_internal internal_donotuse;
} ; /* previously typedef'd to LZ4_streamDecode_t */
/*-************************************
* Obsolete Functions
**************************************/
/*! Deprecation warnings
*
* Deprecated functions make the compiler generate a warning when invoked.
* This is meant to invite users to update their source code.
* Should deprecation warnings be a problem, it is generally possible to disable them,
* typically with -Wno-deprecated-declarations for gcc
* or _CRT_SECURE_NO_WARNINGS in Visual.
*
* Another method is to define LZ4_DISABLE_DEPRECATE_WARNINGS
* before including the header file.
*/
#ifdef LZ4_DISABLE_DEPRECATE_WARNINGS
# define LZ4_DEPRECATED(message) /* disable deprecation warnings */
#else
# if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
# define LZ4_DEPRECATED(message) [[deprecated(message)]]
# elif defined(_MSC_VER)
# define LZ4_DEPRECATED(message) __declspec(deprecated(message))
# elif defined(__clang__) || (defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 45))
# define LZ4_DEPRECATED(message) __attribute__((deprecated(message)))
# elif defined(__GNUC__) && (__GNUC__ * 10 + __GNUC_MINOR__ >= 31)
# define LZ4_DEPRECATED(message) __attribute__((deprecated))
# else
# pragma message("WARNING: LZ4_DEPRECATED needs custom implementation for this compiler")
# define LZ4_DEPRECATED(message) /* disabled */
# endif
#endif /* LZ4_DISABLE_DEPRECATE_WARNINGS */
/*! Obsolete compression functions (since v1.7.3) */
LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress (const char* src, char* dest, int srcSize);
LZ4_DEPRECATED("use LZ4_compress_default() instead") LZ4LIB_API int LZ4_compress_limitedOutput (const char* src, char* dest, int srcSize, int maxOutputSize);
LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_withState (void* state, const char* source, char* dest, int inputSize);
LZ4_DEPRECATED("use LZ4_compress_fast_extState() instead") LZ4LIB_API int LZ4_compress_limitedOutput_withState (void* state, const char* source, char* dest, int inputSize, int maxOutputSize);
LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize);
LZ4_DEPRECATED("use LZ4_compress_fast_continue() instead") LZ4LIB_API int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_streamPtr, const char* source, char* dest, int inputSize, int maxOutputSize);
/*! Obsolete decompression functions (since v1.8.0) */
LZ4_DEPRECATED("use LZ4_decompress_fast() instead") LZ4LIB_API int LZ4_uncompress (const char* source, char* dest, int outputSize);
LZ4_DEPRECATED("use LZ4_decompress_safe() instead") LZ4LIB_API int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize);
/* Obsolete streaming functions (since v1.7.0)
* degraded functionality; do not use!
*
* In order to perform streaming compression, these functions depended on data
* that is no longer tracked in the state. They have been preserved as well as
* possible: using them will still produce a correct output. However, they don't
* actually retain any history between compression calls. The compression ratio
* achieved will therefore be no better than compressing each chunk
* independently.
*/
LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API void* LZ4_create (char* inputBuffer);
LZ4_DEPRECATED("Use LZ4_createStream() instead") LZ4LIB_API int LZ4_sizeofStreamState(void);
LZ4_DEPRECATED("Use LZ4_resetStream() instead") LZ4LIB_API int LZ4_resetStreamState(void* state, char* inputBuffer);
LZ4_DEPRECATED("Use LZ4_saveDict() instead") LZ4LIB_API char* LZ4_slideInputBuffer (void* state);
/*! Obsolete streaming decoding functions (since v1.7.0) */
LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") LZ4LIB_API int LZ4_decompress_safe_withPrefix64k (const char* src, char* dst, int compressedSize, int maxDstSize);
LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") LZ4LIB_API int LZ4_decompress_fast_withPrefix64k (const char* src, char* dst, int originalSize);
/*! Obsolete LZ4_decompress_fast variants (since v1.9.0) :
* These functions used to be faster than LZ4_decompress_safe(),
* but this is no longer the case. They are now slower.
* This is because LZ4_decompress_fast() doesn't know the input size,
* and therefore must progress more cautiously into the input buffer to not read beyond the end of block.
* On top of that `LZ4_decompress_fast()` is not protected vs malformed or malicious inputs, making it a security liability.
* As a consequence, LZ4_decompress_fast() is strongly discouraged, and deprecated.
*
* The last remaining LZ4_decompress_fast() specificity is that
* it can decompress a block without knowing its compressed size.
* Such functionality can be achieved in a more secure manner
* by employing LZ4_decompress_safe_partial().
*
* Parameters:
* originalSize : is the uncompressed size to regenerate.
* `dst` must be already allocated, its size must be >= 'originalSize' bytes.
* @return : number of bytes read from source buffer (== compressed size).
* The function expects to finish at block's end exactly.
* If the source stream is detected malformed, the function stops decoding and returns a negative result.
* note : LZ4_decompress_fast*() requires originalSize. Thanks to this information, it never writes past the output buffer.
* However, since it doesn't know its 'src' size, it may read an unknown amount of input, past input buffer bounds.
* Also, since match offsets are not validated, match reads from 'src' may underflow too.
* These issues never happen if input (compressed) data is correct.
* But they may happen if input data is invalid (error or intentional tampering).
* As a consequence, use these functions in trusted environments with trusted data **only**.
*/
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial() instead")
LZ4LIB_API int LZ4_decompress_fast (const char* src, char* dst, int originalSize);
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider migrating towards LZ4_decompress_safe_continue() instead. "
"Note that the contract will change (requires block's compressed size, instead of decompressed size)")
LZ4LIB_API int LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* src, char* dst, int originalSize);
LZ4_DEPRECATED("This function is deprecated and unsafe. Consider using LZ4_decompress_safe_partial_usingDict() instead")
LZ4LIB_API int LZ4_decompress_fast_usingDict (const char* src, char* dst, int originalSize, const char* dictStart, int dictSize);
/*! LZ4_resetStream() :
* An LZ4_stream_t structure must be initialized at least once.
* This is done with LZ4_initStream(), or LZ4_resetStream().
* Consider switching to LZ4_initStream(),
* invoking LZ4_resetStream() will trigger deprecation warnings in the future.
*/
LZ4LIB_API void LZ4_resetStream (LZ4_stream_t* streamPtr);
#endif /* LZ4_H_98237428734687 */
#if defined (__cplusplus)
}
#endif

View File

@ -205,6 +205,12 @@ VK_OBJS_SLOW = $(addsuffix .o, $(VM_SLOW))
VK_USER_OBJS = $(addsuffix .o, $(VM_USER_CLASSES))
ifneq ($(VM_TRACE_FST),0)
ifneq ($(VM_TRACE_FST),)
LDLIBS += -llz4 -lz
endif
endif
# Note VM_GLOBAL_FAST and VM_GLOBAL_SLOW holds the files required from the
# run-time library. In practice everything is actually in VM_GLOBAL_FAST,
# but keeping the distinction for compatibility for now.

View File

@ -26,21 +26,16 @@
#include "verilated.h"
#include "verilated_fst_c.h"
// GTKWave configuration
#define HAVE_LIBPTHREAD
#define FST_WRITER_PARALLEL
#define LZ4_DISABLE_DEPRECATE_WARNINGS
// Include the GTKWave implementation directly
#define FST_CONFIG_INCLUDE "fst_config.h"
#include "gtkwave/fastlz.c"
#include "gtkwave/fstapi.c"
#include "gtkwave/lz4.c"
// Include fstcpp cpp file directly
#include "fstcpp/fstcpp_variable_info.cpp"
#include "fstcpp/fstcpp_writer.cpp"
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <sstream>
#include <type_traits>
#include <vector>
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
# include <io.h>
@ -53,8 +48,8 @@
//=============================================================================
// Check that forward declared types matches the FST API types
static_assert(std::is_same<vlFstHandle, fstHandle>::value, "vlFstHandle mismatch");
static_assert(std::is_same<vlFstEnumHandle, fstEnumHandle>::value, "vlFstHandle mismatch");
static_assert(std::is_same<vlFstHandle, fst::Handle>::value, "vlFstHandle mismatch");
static_assert(std::is_same<vlFstEnumHandle, fst::EnumHandle>::value, "vlFstHandle mismatch");
//=============================================================================
// Specialization of the generics for this trace format
@ -71,18 +66,18 @@ static_assert(std::is_same<vlFstEnumHandle, fstEnumHandle>::value, "vlFstHandle
VerilatedFst::VerilatedFst(void* /*fst*/) {}
VerilatedFst::~VerilatedFst() {
if (m_fst) fstWriterClose(m_fst);
if (m_fst) VL_DO_CLEAR(delete m_fst, m_fst = nullptr);
if (m_symbolp) VL_DO_CLEAR(delete[] m_symbolp, m_symbolp = nullptr);
if (m_strbufp) VL_DO_CLEAR(delete[] m_strbufp, m_strbufp = nullptr);
}
void VerilatedFst::open(const char* filename) VL_MT_SAFE_EXCLUDES(m_mutex) {
const VerilatedLockGuard lock{m_mutex};
m_fst = fstWriterCreate(filename, 1);
fstWriterSetPackType(m_fst, FST_WR_PT_LZ4);
fstWriterSetTimescaleFromString(m_fst, timeResStr().c_str()); // lintok-begin-on-ref
if (m_useFstWriterThread) fstWriterSetParallelMode(m_fst, 1);
fstWriterSetVersion(m_fst, "Generated by VerilatedFst");
m_fst = new fst::Writer{filename};
m_fst->setWriterPackType(fst::WriterPackType::LZ4);
m_fst->setTimecale(int8_t(round(log10(timeRes()))));
// if (m_useFstWriterThread) fstWriterSetParallelMode(m_fst, 1);
m_fst->setWriter("Generated by VerilatedFst");
constDump(true); // First dump must contain the const signals
fullDump(true); // First dump must be full for fst
@ -90,7 +85,7 @@ void VerilatedFst::open(const char* filename) VL_MT_SAFE_EXCLUDES(m_mutex) {
// convert m_code2symbol into an array for fast lookup
if (!m_symbolp) {
m_symbolp = new fstHandle[nextCode()]{0};
m_symbolp = new fst::Handle[nextCode()]{0};
for (const auto& i : m_code2symbol) m_symbolp[i.first] = i.second;
}
m_code2symbol.clear();
@ -103,7 +98,7 @@ void VerilatedFst::close() VL_MT_SAFE_EXCLUDES(m_mutex) {
const VerilatedLockGuard lock{m_mutex};
Super::closeBase();
emitTimeChangeMaybe();
fstWriterClose(m_fst);
if (m_fst) m_fst->close();
m_fst = nullptr;
}
@ -111,18 +106,18 @@ void VerilatedFst::flush() VL_MT_SAFE_EXCLUDES(m_mutex) {
const VerilatedLockGuard lock{m_mutex};
Super::flushBase();
emitTimeChangeMaybe();
fstWriterFlushContext(m_fst);
if (m_fst) m_fst->flushValueChangeData();
}
void VerilatedFst::emitTimeChange(uint64_t timeui) {
if (!timeui) fstWriterEmitTimeChange(m_fst, timeui);
if (!timeui) m_fst->emitTimeChange(timeui);
m_timeui = timeui;
}
VL_ATTR_ALWINLINE
void VerilatedFst::emitTimeChangeMaybe() {
if (VL_UNLIKELY(m_timeui)) {
fstWriterEmitTimeChange(m_fst, m_timeui);
m_fst->emitTimeChange(m_timeui);
m_timeui = 0;
}
}
@ -133,8 +128,12 @@ void VerilatedFst::emitTimeChangeMaybe() {
void VerilatedFst::declDTypeEnum(int dtypenum, const char* name, uint32_t elements,
unsigned int minValbits, const char** itemNamesp,
const char** itemValuesp) {
const fstEnumHandle enumNum
= fstWriterCreateEnumTable(m_fst, name, elements, minValbits, itemNamesp, itemValuesp);
std::vector<std::pair<const char*, const char*>> itemNameValuesp{elements};
for (uint32_t i = 0; i < elements; ++i) {
itemNameValuesp[i].first = itemNamesp[i];
itemNameValuesp[i].second = itemValuesp[i];
}
const fst::EnumHandle enumNum = m_fst->createEnumTable(name, minValbits, itemNameValuesp);
const bool newEntry = m_local2fstdtype[initUserp()].emplace(dtypenum, enumNum).second;
assert(newEntry);
}
@ -176,30 +175,35 @@ void VerilatedFst::pushPrefix(const char* namep, VerilatedTracePrefixType type,
switch (type) {
case VerilatedTracePrefixType::SCOPE_MODULE:
fstWriterSetScope(m_fst, FST_ST_VCD_MODULE, namep, nullptr);
m_fst->setScope(fst::Hierarchy::ScopeType::VCD_MODULE, name, std::string{});
break;
case VerilatedTracePrefixType::SCOPE_INTERFACE:
fstWriterSetScope(m_fst, FST_ST_VCD_INTERFACE, namep, nullptr);
m_fst->setScope(fst::Hierarchy::ScopeType::VCD_INTERFACE, name, std::string{});
break;
case VerilatedTracePrefixType::STRUCT_PACKED:
fstWriterSetAttrBegin(m_fst, FST_AT_PACK, FST_PT_PACKED, "members", l);
fstWriterSetScope(m_fst, FST_ST_VCD_STRUCT, namep, nullptr);
m_fst->setAttrBegin(fst::Hierarchy::AttrType::PACK,
fst::Hierarchy::AttrSubType::PACK_PACKED, "members", l);
m_fst->setScope(fst::Hierarchy::ScopeType::VCD_STRUCT, name, std::string{});
break;
case VerilatedTracePrefixType::STRUCT_UNPACKED:
fstWriterSetAttrBegin(m_fst, FST_AT_PACK, FST_PT_UNPACKED, "members", l);
fstWriterSetScope(m_fst, FST_ST_VCD_STRUCT, namep, nullptr);
m_fst->setAttrBegin(fst::Hierarchy::AttrType::PACK,
fst::Hierarchy::AttrSubType::PACK_UNPACKED, "members", l);
m_fst->setScope(fst::Hierarchy::ScopeType::VCD_STRUCT, name, std::string{});
break;
case VerilatedTracePrefixType::UNION_PACKED:
fstWriterSetAttrBegin(m_fst, FST_AT_PACK, FST_PT_PACKED, "members", l);
fstWriterSetScope(m_fst, FST_ST_VCD_UNION, namep, nullptr);
m_fst->setAttrBegin(fst::Hierarchy::AttrType::PACK,
fst::Hierarchy::AttrSubType::PACK_PACKED, "members", l);
m_fst->setScope(fst::Hierarchy::ScopeType::VCD_UNION, name, std::string{});
break;
case VerilatedTracePrefixType::ARRAY_PACKED:
fstWriterSetAttrBegin(m_fst, FST_AT_ARRAY, FST_AR_PACKED, "bounds", lr);
fstWriterSetScope(m_fst, FST_ST_SV_ARRAY, namep, nullptr);
m_fst->setAttrBegin(fst::Hierarchy::AttrType::ARRAY,
fst::Hierarchy::AttrSubType::ARRAY_PACKED, "bounds", lr);
m_fst->setScope(fst::Hierarchy::ScopeType::SV_ARRAY, name, std::string{});
break;
case VerilatedTracePrefixType::ARRAY_UNPACKED:
fstWriterSetAttrBegin(m_fst, FST_AT_ARRAY, FST_AR_UNPACKED, "bounds", lr);
fstWriterSetScope(m_fst, FST_ST_SV_ARRAY, namep, nullptr);
m_fst->setAttrBegin(fst::Hierarchy::AttrType::ARRAY,
fst::Hierarchy::AttrSubType::ARRAY_UNPACKED, "bounds", lr);
m_fst->setScope(fst::Hierarchy::ScopeType::SV_ARRAY, name, std::string{});
break;
default: break;
}
@ -208,7 +212,7 @@ void VerilatedFst::pushPrefix(const char* namep, VerilatedTracePrefixType type,
void VerilatedFst::popPrefix() {
assert(!m_prefixStack.empty());
if (m_prefixStack.back().second != VerilatedTracePrefixType::ROOTIO_WRAPPER) {
fstWriterSetUpscope(m_fst);
m_fst->upscope();
}
m_prefixStack.pop_back();
assert(!m_prefixStack.empty()); // Always one left, the constructor's initial one
@ -232,57 +236,54 @@ void VerilatedFst::declare(uint32_t code, const char* name, int dtypenum,
if (bussed) name_ss << " [" << msb << ":" << lsb << "]";
const std::string name_str = name_ss.str();
if (dtypenum > 0) {
fstWriterEmitEnumTableRef(m_fst, m_local2fstdtype.at(initUserp()).at(dtypenum));
}
if (dtypenum > 0) { m_fst->emitEnumTableRef(m_local2fstdtype.at(initUserp()).at(dtypenum)); }
fstVarDir varDir = FST_VD_IMPLICIT;
fst::Hierarchy::VarDirection varDir = fst::Hierarchy::VarDirection::IMPLICIT;
switch (direction) {
case VerilatedTraceSigDirection::INOUT: varDir = FST_VD_INOUT; break;
case VerilatedTraceSigDirection::OUTPUT: varDir = FST_VD_OUTPUT; break;
case VerilatedTraceSigDirection::INPUT: varDir = FST_VD_INPUT; break;
case VerilatedTraceSigDirection::NONE: varDir = FST_VD_IMPLICIT; break;
case VerilatedTraceSigDirection::INOUT: varDir = fst::Hierarchy::VarDirection::INOUT; break;
case VerilatedTraceSigDirection::OUTPUT: varDir = fst::Hierarchy::VarDirection::OUTPUT; break;
case VerilatedTraceSigDirection::INPUT: varDir = fst::Hierarchy::VarDirection::INPUT; break;
case VerilatedTraceSigDirection::NONE: varDir = fst::Hierarchy::VarDirection::IMPLICIT; break;
}
fstVarType varType;
fst::Hierarchy::VarType varType;
// Doubles have special decoding properties, so must indicate if a double
if (type == VerilatedTraceSigType::DOUBLE) {
if (kind == VerilatedTraceSigKind::PARAMETER) {
varType = FST_VT_VCD_REAL_PARAMETER;
varType = fst::Hierarchy::VarType::VCD_REAL_PARAMETER;
} else {
varType = FST_VT_VCD_REAL;
varType = fst::Hierarchy::VarType::VCD_REAL;
}
}
// clang-format off
else if (kind == VerilatedTraceSigKind::PARAMETER) varType = FST_VT_VCD_PARAMETER;
else if (kind == VerilatedTraceSigKind::SUPPLY0) varType = FST_VT_VCD_SUPPLY0;
else if (kind == VerilatedTraceSigKind::SUPPLY1) varType = FST_VT_VCD_SUPPLY1;
else if (kind == VerilatedTraceSigKind::TRI) varType = FST_VT_VCD_TRI;
else if (kind == VerilatedTraceSigKind::TRI0) varType = FST_VT_VCD_TRI0;
else if (kind == VerilatedTraceSigKind::TRI1) varType = FST_VT_VCD_TRI1;
else if (kind == VerilatedTraceSigKind::TRIAND) varType = FST_VT_VCD_TRIAND;
else if (kind == VerilatedTraceSigKind::TRIOR) varType = FST_VT_VCD_TRIOR;
else if (kind == VerilatedTraceSigKind::TRIREG) varType = FST_VT_VCD_TRIREG;
else if (kind == VerilatedTraceSigKind::WIRE) varType = FST_VT_VCD_WIRE;
else if (kind == VerilatedTraceSigKind::PARAMETER) varType = fst::Hierarchy::VarType::VCD_PARAMETER;
else if (kind == VerilatedTraceSigKind::SUPPLY0) varType = fst::Hierarchy::VarType::VCD_SUPPLY0;
else if (kind == VerilatedTraceSigKind::SUPPLY1) varType = fst::Hierarchy::VarType::VCD_SUPPLY1;
else if (kind == VerilatedTraceSigKind::TRI) varType = fst::Hierarchy::VarType::VCD_TRI;
else if (kind == VerilatedTraceSigKind::TRI0) varType = fst::Hierarchy::VarType::VCD_TRI0;
else if (kind == VerilatedTraceSigKind::TRI1) varType = fst::Hierarchy::VarType::VCD_TRI1;
else if (kind == VerilatedTraceSigKind::TRIAND) varType = fst::Hierarchy::VarType::VCD_TRIAND;
else if (kind == VerilatedTraceSigKind::TRIOR) varType = fst::Hierarchy::VarType::VCD_TRIOR;
else if (kind == VerilatedTraceSigKind::TRIREG) varType = fst::Hierarchy::VarType::VCD_TRIREG;
else if (kind == VerilatedTraceSigKind::WIRE) varType = fst::Hierarchy::VarType::VCD_WIRE;
//
else if (type == VerilatedTraceSigType::INTEGER) varType = FST_VT_VCD_INTEGER;
else if (type == VerilatedTraceSigType::BIT) varType = FST_VT_SV_BIT;
else if (type == VerilatedTraceSigType::LOGIC) varType = FST_VT_SV_LOGIC;
else if (type == VerilatedTraceSigType::INT) varType = FST_VT_SV_INT;
else if (type == VerilatedTraceSigType::SHORTINT) varType = FST_VT_SV_SHORTINT;
else if (type == VerilatedTraceSigType::LONGINT) varType = FST_VT_SV_LONGINT;
else if (type == VerilatedTraceSigType::BYTE) varType = FST_VT_SV_BYTE;
else if (type == VerilatedTraceSigType::EVENT) varType = FST_VT_VCD_EVENT;
else if (type == VerilatedTraceSigType::TIME) varType = FST_VT_VCD_TIME;
else if (type == VerilatedTraceSigType::INTEGER) varType = fst::Hierarchy::VarType::VCD_INTEGER;
else if (type == VerilatedTraceSigType::BIT) varType = fst::Hierarchy::VarType::SV_BIT;
else if (type == VerilatedTraceSigType::LOGIC) varType = fst::Hierarchy::VarType::SV_LOGIC;
else if (type == VerilatedTraceSigType::INT) varType = fst::Hierarchy::VarType::SV_INT;
else if (type == VerilatedTraceSigType::SHORTINT) varType = fst::Hierarchy::VarType::SV_SHORTINT;
else if (type == VerilatedTraceSigType::LONGINT) varType = fst::Hierarchy::VarType::SV_LONGINT;
else if (type == VerilatedTraceSigType::BYTE) varType = fst::Hierarchy::VarType::SV_BYTE;
else if (type == VerilatedTraceSigType::EVENT) varType = fst::Hierarchy::VarType::VCD_EVENT;
else if (type == VerilatedTraceSigType::TIME) varType = fst::Hierarchy::VarType::VCD_TIME;
else { assert(0); /* Unreachable */ }
// clang-format on
const auto it = vlstd::as_const(m_code2symbol).find(code);
if (it == m_code2symbol.end()) { // New
m_code2symbol[code]
= fstWriterCreateVar(m_fst, varType, varDir, bits, name_str.c_str(), 0);
m_code2symbol[code] = m_fst->createVar(varType, varDir, bits, name_str.c_str(), 0);
} else { // Alias
fstWriterCreateVar(m_fst, varType, varDir, bits, name_str.c_str(), it->second);
m_fst->createVar(varType, varDir, bits, name_str.c_str(), it->second);
}
}
@ -391,71 +392,57 @@ VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitEvent(uint32_t code) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], "1");
m_fst->emitValueChange(m_symbolp[code], 1);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitBit(uint32_t code, CData newval) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], newval ? "1" : "0");
m_fst->emitValueChange(m_symbolp[code], uint64_t(newval));
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitCData(uint32_t code, CData newval, int bits) {
char buf[VL_BYTESIZE];
void VerilatedFstBuffer::emitCData(uint32_t code, CData newval, int) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
cvtCDataToStr(buf, newval << (VL_BYTESIZE - bits));
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
m_fst->emitValueChange(m_symbolp[code], newval);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitSData(uint32_t code, SData newval, int bits) {
char buf[VL_SHORTSIZE];
void VerilatedFstBuffer::emitSData(uint32_t code, SData newval, int) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
cvtSDataToStr(buf, newval << (VL_SHORTSIZE - bits));
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
m_fst->emitValueChange(m_symbolp[code], newval);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitIData(uint32_t code, IData newval, int bits) {
char buf[VL_IDATASIZE];
void VerilatedFstBuffer::emitIData(uint32_t code, IData newval, int) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
cvtIDataToStr(buf, newval << (VL_IDATASIZE - bits));
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
m_fst->emitValueChange(m_symbolp[code], newval);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitQData(uint32_t code, QData newval, int bits) {
char buf[VL_QUADSIZE];
void VerilatedFstBuffer::emitQData(uint32_t code, QData newval, int) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
cvtQDataToStr(buf, newval << (VL_QUADSIZE - bits));
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], buf);
m_fst->emitValueChange(m_symbolp[code], newval);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitWData(uint32_t code, const WData* newvalp, int bits) {
int words = VL_WORDS_I(bits);
char* wp = m_strbufp;
// Convert the most significant word
const int bitsInMSW = VL_BITBIT_E(bits) ? VL_BITBIT_E(bits) : VL_EDATASIZE;
cvtEDataToStr(wp, newvalp[--words] << (VL_EDATASIZE - bitsInMSW));
wp += bitsInMSW;
// Convert the remaining words
while (words > 0) {
cvtEDataToStr(wp, newvalp[--words]);
wp += VL_EDATASIZE;
}
void VerilatedFstBuffer::emitWData(uint32_t code, const WData* newvalp, int) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], m_strbufp);
// call emitValueChange(handle, uint32_t*)
m_fst->emitValueChange(m_symbolp[code], newvalp);
}
VL_ATTR_ALWINLINE
void VerilatedFstBuffer::emitDouble(uint32_t code, double newval) {
VL_DEBUG_IFDEF(assert(m_symbolp[code]););
m_owner.emitTimeChangeMaybe();
fstWriterEmitValueChange(m_fst, m_symbolp[code], &newval);
uint64_t newval_u64;
std::memcpy(&newval_u64, &newval, sizeof(newval_u64));
m_fst->emitValueChange(m_symbolp[code], newval_u64);
}

View File

@ -34,7 +34,9 @@ typedef uint32_t vlFstEnumHandle;
class VerilatedFstBuffer;
struct fstWriterContext;
namespace fst {
class Writer;
}
//=============================================================================
// VerilatedFst
@ -51,7 +53,7 @@ private:
//=========================================================================
// FST-specific internals
fstWriterContext* m_fst = nullptr;
fst::Writer* m_fst = nullptr;
std::map<uint32_t, vlFstHandle> m_code2symbol;
std::map<void*, std::map<int, vlFstEnumHandle>> m_local2fstdtype;
vlFstHandle* m_symbolp = nullptr; // same as m_code2symbol, but as an array
@ -211,7 +213,7 @@ class VerilatedFstBuffer VL_NOT_FINAL {
VerilatedFst& m_owner; // Trace file owning this buffer. Required by subclasses.
// The FST file handle
fstWriterContext* const m_fst = m_owner.m_fst;
fst::Writer* const m_fst = m_owner.m_fst;
// code to fstHande map, as an array
const vlFstHandle* const m_symbolp = m_owner.m_symbolp;
// String buffer long enough to hold maxBits() chars
@ -229,11 +231,11 @@ class VerilatedFstBuffer VL_NOT_FINAL {
// called from only one place (the full* methods), so always inline them.
VL_ATTR_ALWINLINE void emitEvent(uint32_t code);
VL_ATTR_ALWINLINE void emitBit(uint32_t code, CData newval);
VL_ATTR_ALWINLINE void emitCData(uint32_t code, CData newval, int bits);
VL_ATTR_ALWINLINE void emitSData(uint32_t code, SData newval, int bits);
VL_ATTR_ALWINLINE void emitIData(uint32_t code, IData newval, int bits);
VL_ATTR_ALWINLINE void emitQData(uint32_t code, QData newval, int bits);
VL_ATTR_ALWINLINE void emitWData(uint32_t code, const WData* newvalp, int bits);
VL_ATTR_ALWINLINE void emitCData(uint32_t code, CData newval, int);
VL_ATTR_ALWINLINE void emitSData(uint32_t code, SData newval, int);
VL_ATTR_ALWINLINE void emitIData(uint32_t code, IData newval, int);
VL_ATTR_ALWINLINE void emitQData(uint32_t code, QData newval, int);
VL_ATTR_ALWINLINE void emitWData(uint32_t code, const WData* newvalp, int);
VL_ATTR_ALWINLINE void emitDouble(uint32_t code, double newval);
};

View File

@ -32,7 +32,7 @@ EXEMPT_FILES_LIST = """
docs/gen
docs/spelling.txt
docs/verilated.dox
include/gtkwave
include/fstcpp
include/vltstd
install-sh
src/mkinstalldirs

View File

@ -57,7 +57,7 @@ for filename in sorted(files.keys()):
continue
if not re.search(r'\.(h|c|cpp)(\.in)?$', filename):
continue
if '/gtkwave/' in filename:
if '/fstcpp/' in filename:
continue
contents = test.file_contents(filename) + "\n\n"

View File

@ -11,7 +11,7 @@ import vltest_bootstrap
test.scenarios('dist')
Tabs_Exempt_Re = r'(\.out$)|(/gtkwave)|(Makefile)|(\.mk$)|(\.mk\.in$)|test_regress/t/t_preproc\.v|install-sh'
Tabs_Exempt_Re = r'(\.out$)|(/fstcpp)|(Makefile)|(\.mk$)|(\.mk\.in$)|test_regress/t/t_preproc\.v|install-sh'
Unicode_Exempt_Re = r'(Changes$|CONTRIBUTORS$|LICENSES?|contributors.rst$|spelling.txt$)'

View File

@ -743,6 +743,10 @@ function(verilate TARGET)
target_link_libraries(${TARGET} PUBLIC ${VERILATOR_MT_CFLAGS})
if(${VERILATE_PREFIX}_TRACE_FST)
target_link_libraries(${TARGET} PUBLIC -llz4 -lz)
endif()
target_compile_features(${TARGET} PRIVATE cxx_std_11)
if(${VERILATE_PREFIX}_TIMING)