diff --git a/.github/workflows/test-compile.yml b/.github/workflows/test-compile.yml index 089e65ca757..74c3e2639ef 100644 --- a/.github/workflows/test-compile.yml +++ b/.github/workflows/test-compile.yml @@ -32,9 +32,9 @@ jobs: # oldest supported - 'clang-14' - 'gcc-10' - # newest - - 'clang' - - 'gcc' + # newest, make sure to update maximum standard step to match + - 'clang-18' + - 'gcc-13' include: # macOS - os: macos-13 @@ -72,7 +72,7 @@ jobs: # maximum standard, only on newest compilers - name: Build C++20 - if: ${{ matrix.compiler == 'clang' || matrix.compiler == 'gcc'}} + if: ${{ matrix.compiler == 'clang-18' || matrix.compiler == 'gcc-13' }} shell: bash run: | make config-$CC_SHORT diff --git a/CHANGELOG b/CHANGELOG index 0a6aab40e7d..1ebaeb24aa5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,9 +2,36 @@ List of major changes and improvements between releases ======================================================= -Yosys 0.47 .. Yosys 0.48-dev +Yosys 0.48 .. Yosys 0.49-dev -------------------------- +Yosys 0.47 .. Yosys 0.48 +-------------------------- + * Various + - Removed "read_ilang" deprecated pass. + - Enhanced boxing features in the experimental "abc_new" command. + - Added new Tcl methods for design inspection. + - Added clock enable inference to "dfflibmap". + - Added a Han-Carlson and Sklansky option for $lcu mapping. + + * New commands and options + - Added "-nopeepopt" option to "clk2fflogic" pass. + - Added "-liberty" and "-dont_use" options to "clockgate" pass. + - Added "-ignore_buses" option to "read_liberty" pass. + - Added "-dont_map" option to "techmap" pass. + - Added "-selected" option to "write_json" pass. + - Added "wrapcell" command for creating wrapper modules + around selected cells. + - Added "portarcs" command for deriving propagation timing arcs. + - Added "setenv" command for setting environment variables. + + * Gowin support + - Added "-family" option to "synth_gowin" pass. + - Cell definitions split by family. + + * Verific support + - Improved blackbox support. + Yosys 0.46 .. Yosys 0.47 -------------------------- * Various diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000..ad29106307f --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at contact@yosyshq.com and/or +claire@clairexen.net. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/Makefile b/Makefile index 686fab8f143..1ef5bd160fd 100644 --- a/Makefile +++ b/Makefile @@ -118,6 +118,7 @@ AWK ?= awk ifeq ($(OS), Darwin) PLUGIN_LINKFLAGS += -undefined dynamic_lookup +LINKFLAGS += -rdynamic # homebrew search paths ifneq ($(shell :; command -v brew),) @@ -154,7 +155,7 @@ ifeq ($(OS), Haiku) CXXFLAGS += -D_DEFAULT_SOURCE endif -YOSYS_VER := 0.47+0 +YOSYS_VER := 0.48+0 # Note: We arrange for .gitcommit to contain the (short) commit hash in # tarballs generated with git-archive(1) using .gitattributes. The git repo @@ -170,7 +171,7 @@ endif OBJS = kernel/version_$(GIT_REV).o bumpversion: - sed -i "/^YOSYS_VER := / s/+[0-9][0-9]*$$/+`git log --oneline 647d61d.. | wc -l`/;" Makefile + sed -i "/^YOSYS_VER := / s/+[0-9][0-9]*$$/+`git log --oneline aaa5347.. | wc -l`/;" Makefile ABCMKARGS = CC="$(CXX)" CXX="$(CXX)" ABC_USE_LIBSTDCXX=1 ABC_USE_NAMESPACE=abc VERBOSE=$(Q) @@ -304,7 +305,7 @@ endif else ifeq ($(CONFIG),mxe) PKG_CONFIG = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-pkg-config CXX = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-g++ -CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) -D_POSIX_SOURCE -DYOSYS_MXE_HACKS -Wno-attributes +CXXFLAGS += -std=$(CXXSTD) $(OPT_LEVEL) -D_POSIX_SOURCE -Wno-attributes CXXFLAGS := $(filter-out -fPIC,$(CXXFLAGS)) LINKFLAGS := $(filter-out -rdynamic,$(LINKFLAGS)) -s LIBS := $(filter-out -lrt,$(LIBS)) @@ -397,10 +398,9 @@ endif else ifeq ($(ENABLE_EDITLINE),1) CXXFLAGS += -DYOSYS_ENABLE_EDITLINE -LIBS += -ledit -ltinfo -lbsd -else -ABCMKARGS += "ABC_USE_NO_READLINE=1" +LIBS += -ledit endif +ABCMKARGS += "ABC_USE_NO_READLINE=1" endif ifeq ($(DISABLE_ABC_THREADS),1) @@ -449,6 +449,9 @@ LIBS += -ltcl86 -lwsock32 -lws2_32 -lnetapi32 -lz -luserenv else CXXFLAGS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --silence-errors --cflags tcl || echo -I$(TCL_INCLUDE)) -DYOSYS_ENABLE_TCL LIBS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --silence-errors --libs tcl || echo $(TCL_LIBS)) +ifneq (,$(findstring TCL_WITH_EXTERNAL_TOMMATH,$(CXXFLAGS))) +LIBS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --silence-errors --libs libtommath || echo) +endif endif endif @@ -639,7 +642,7 @@ $(eval $(call add_include_file,frontends/blif/blifparse.h)) $(eval $(call add_include_file,backends/rtlil/rtlil_backend.h)) OBJS += kernel/driver.o kernel/register.o kernel/rtlil.o kernel/log.o kernel/calc.o kernel/yosys.o -OBJS += kernel/binding.o +OBJS += kernel/binding.o kernel/tclapi.o OBJS += kernel/cellaigs.o kernel/celledges.o kernel/cost.o kernel/satgen.o kernel/scopeinfo.o kernel/qcsat.o kernel/mem.o kernel/ffmerge.o kernel/ff.o kernel/yw.o kernel/json.o kernel/fmt.o kernel/sexpr.o OBJS += kernel/drivertools.o kernel/functional.o ifeq ($(ENABLE_ZLIB),1) @@ -996,15 +999,12 @@ docs/source/cell/word_add.rst: $(TARGETS) $(EXTRA_TARGETS) docs/source/generated/cells.json: docs/source/generated $(TARGETS) $(EXTRA_TARGETS) $(Q) ./$(PROGRAM_PREFIX)yosys -p 'help -dump-cells-json $@' -PHONY: docs/gen docs/guidelines docs/usage docs/reqs +PHONY: docs/gen docs/usage docs/reqs docs/gen: $(TARGETS) $(Q) $(MAKE) -C docs gen -DOCS_GUIDELINE_FILES := GettingStarted CodingStyle -DOCS_GUIDELINE_SOURCE := $(addprefix guidelines/,$(DOCS_GUIDELINE_FILES)) -docs/guidelines docs/source/generated: $(DOCS_GUIDELINE_SOURCE) +docs/source/generated: $(Q) mkdir -p docs/source/generated - $(Q) cp -f $(DOCS_GUIDELINE_SOURCE) docs/source/generated # some commands return an error and print the usage text to stderr define DOC_USAGE_STDERR @@ -1034,7 +1034,7 @@ docs/reqs: $(Q) $(MAKE) -C docs reqs .PHONY: docs/prep -docs/prep: docs/source/cmd/abc.rst docs/source/generated/cells.json docs/gen docs/guidelines docs/usage +docs/prep: docs/source/cmd/abc.rst docs/source/generated/cells.json docs/gen docs/usage DOC_TARGET ?= html docs: docs/prep @@ -1094,6 +1094,7 @@ vcxsrc: $(GENFILES) $(EXTRA_TARGETS) rm -rf yosys-win32-vcxsrc-$(YOSYS_VER){,.zip} set -e; for f in `ls $(filter %.cc %.cpp,$(GENFILES)) $(addsuffix .cc,$(basename $(OBJS))) $(addsuffix .cpp,$(basename $(OBJS))) 2> /dev/null`; do \ echo "Analyse: $$f" >&2; cpp -std=c++17 -MM -I. -D_YOSYS_ $$f; done | sed 's,.*:,,; s,//*,/,g; s,/[^/]*/\.\./,/,g; y, \\,\n\n,;' | grep '^[^/]' | sort -u | grep -v kernel/version_ > srcfiles.txt + echo "libs/fst/fst_win_unistd.h" >> srcfiles.txt bash misc/create_vcxsrc.sh yosys-win32-vcxsrc $(YOSYS_VER) $(GIT_REV) echo "namespace Yosys { extern const char *yosys_version_str; const char *yosys_version_str=\"Yosys (Version Information Unavailable)\"; }" > kernel/version.cc zip yosys-win32-vcxsrc-$(YOSYS_VER)/genfiles.zip $(GENFILES) kernel/version.cc diff --git a/README.md b/README.md index 3845d25029b..71d47d76c21 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,3 @@ -``` -yosys -- Yosys Open SYnthesis Suite - -Copyright (C) 2012 - 2024 Claire Xenia Wolf - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -``` - - yosys – Yosys Open SYnthesis Suite =================================== @@ -37,23 +18,21 @@ Third-party software distributed alongside this software is licensed under compatible licenses. Please refer to `abc` and `libs` subdirectories for their license terms. + Web Site and Other Resources ============================ More information and documentation can be found on the Yosys web site: - https://yosyshq.net/yosys/ -The "Documentation" page on the web site contains links to more resources, -including a manual that even describes some of the Yosys internals: -- https://yosyshq.net/yosys/documentation.html - -The directory `guidelines` contains additional information -for people interested in using the Yosys C++ APIs. +Documentation from this repository is automatically built and available on Read +the Docs: +- https://yosyshq.readthedocs.io/projects/yosys -Users interested in formal verification might want to use the formal verification -front-end for Yosys, SymbiYosys: -- https://symbiyosys.readthedocs.io/en/latest/ -- https://github.com/YosysHQ/SymbiYosys +Users interested in formal verification might want to use the formal +verification front-end for Yosys, SBY: +- https://yosyshq.readthedocs.io/projects/sby/ +- https://github.com/YosysHQ/sby Installation @@ -71,9 +50,25 @@ For more information about the difference between Tabby CAD Suite and the OSS CA Many Linux distributions also provide Yosys binaries, some more up to date than others. Check with your package manager! + Building from Source ==================== +For more details, and instructions for other platforms, check [building from +source](https://yosyshq.readthedocs.io/projects/yosys/en/latest/getting_started/installation.html#building-from-source) +on Read the Docs. + +When cloning Yosys, some required libraries are included as git submodules. Make +sure to call e.g. + + $ git clone --recurse-submodules https://github.com/YosysHQ/yosys.git + +or + + $ git clone https://github.com/YosysHQ/yosys.git + $ cd yosys + $ git submodule update --init --recursive + You need a C++ compiler with C++17 support (up-to-date CLANG or GCC is recommended) and some standard tools such as GNU Flex, GNU Bison, and GNU Make. TCL, readline and libffi are optional (see ``ENABLE_*`` settings in Makefile). @@ -87,52 +82,22 @@ prerequisites for building yosys: graphviz xdot pkg-config python3 libboost-system-dev \ libboost-python-dev libboost-filesystem-dev zlib1g-dev -Similarily, on Mac OS X Homebrew can be used to install dependencies (from within cloned yosys repository): - - $ brew tap Homebrew/bundle && brew bundle - -or MacPorts: - - $ sudo port install bison flex readline gawk libffi \ - git graphviz pkgconfig python36 boost zlib tcl - -On FreeBSD use the following command to install all prerequisites: - - # pkg install bison flex readline gawk libffi\ - git graphviz pkgconf python3 python36 tcl-wrapper boost-libs - -On FreeBSD system use gmake instead of make. To run tests use: - % MAKE=gmake CC=cc gmake test - -For Cygwin use the following command to install all prerequisites, or select these additional packages: - - setup-x86_64.exe -q --packages=bison,flex,gcc-core,gcc-g++,git,libffi-devel,libreadline-devel,make,pkg-config,python3,tcl-devel,boost-build,zlib-devel - The environment variable `CXX` can be used to control the C++ compiler used, or -run one of the following: +run one of the following to override it: $ make config-clang $ make config-gcc -Note that these will result in `make` ignoring the `CXX` environment variable, -unless `CXX` is assigned in the call to make, e.g. - - $ make CXX=$CXX - The Makefile has many variables influencing the build process. These can be -adjusted by modifying the Makefile.conf file which is created at the -`make config-...` step (see above), or they can be set by passing an option -to the make command directly. +adjusted by modifying the Makefile.conf file which is created at the `make +config-...` step (see above), or they can be set by passing an option to the +make command directly: -For example, if you have clang, and (a compatible version of) `ld.lld` -available in PATH, it's recommended to speed up incremental builds with -lld by enabling LTO: - - $ make ENABLE_LTO=1 + $ make CXX=$CXX -For other compilers and build configurations it might be -necessary to make some changes to the config section of the -Makefile. +For other compilers and build configurations it might be necessary to make some +changes to the config section of the Makefile. It's also an alternative way to +set the make variables mentioned above. $ vi Makefile # ..or.. $ vi Makefile.conf @@ -142,10 +107,9 @@ To build Yosys simply type 'make' in this directory. $ make $ sudo make install -Note that this also downloads, builds and installs ABC (using yosys-abc -as executable name). - -Tests are located in the tests subdirectory and can be executed using the test target. Note that you need gawk as well as a recent version of iverilog (i.e. build from git). Then, execute tests via: +Tests are located in the tests subdirectory and can be executed using the test +target. Note that you need gawk as well as a recent version of iverilog (i.e. +build from git). Then, execute tests via: $ make test @@ -156,6 +120,7 @@ To use a separate (out-of-tree) build directory, provide a path to the Makefile. Out-of-tree builds require a clean source tree. + Getting Started =============== @@ -260,365 +225,14 @@ The command ``prep`` provides a good default word-level synthesis script, as used in SMT-based formal verification. -Unsupported Verilog-2005 Features -================================= - -The following Verilog-2005 features are not supported by -Yosys and there are currently no plans to add support -for them: - -- Non-synthesizable language features as defined in - IEC 62142(E):2005 / IEEE Std. 1364.1(E):2002 - -- The ``tri``, ``triand`` and ``trior`` net types - -- The ``config`` and ``disable`` keywords and library map files - - -Verilog Attributes and non-standard features -============================================ - -- The ``full_case`` attribute on case statements is supported - (also the non-standard ``// synopsys full_case`` directive) - -- The ``parallel_case`` attribute on case statements is supported - (also the non-standard ``// synopsys parallel_case`` directive) - -- The ``// synopsys translate_off`` and ``// synopsys translate_on`` - directives are also supported (but the use of ``` `ifdef .. `endif ``` - is strongly recommended instead). - -- The ``nomem2reg`` attribute on modules or arrays prohibits the - automatic early conversion of arrays to separate registers. This - is potentially dangerous. Usually the front-end has good reasons - for converting an array to a list of registers. Prohibiting this - step will likely result in incorrect synthesis results. - -- The ``mem2reg`` attribute on modules or arrays forces the early - conversion of arrays to separate registers. - -- The ``nomeminit`` attribute on modules or arrays prohibits the - creation of initialized memories. This effectively puts ``mem2reg`` - on all memories that are written to in an ``initial`` block and - are not ROMs. - -- The ``nolatches`` attribute on modules or always-blocks - prohibits the generation of logic-loops for latches. Instead - all not explicitly assigned values default to x-bits. This does - not affect clocked storage elements such as flip-flops. - -- The ``nosync`` attribute on registers prohibits the generation of a - storage element. The register itself will always have all bits set - to 'x' (undefined). The variable may only be used as blocking assigned - temporary variable within an always block. This is mostly used internally - by Yosys to synthesize Verilog functions and access arrays. - -- The ``nowrshmsk`` attribute on a register prohibits the generation of - shift-and-mask type circuits for writing to bit slices of that register. - -- The ``onehot`` attribute on wires mark them as one-hot state register. This - is used for example for memory port sharing and set by the fsm_map pass. - -- The ``blackbox`` attribute on modules is used to mark empty stub modules - that have the same ports as the real thing but do not contain information - on the internal configuration. This modules are only used by the synthesis - passes to identify input and output ports of cells. The Verilog backend - also does not output blackbox modules on default. ``read_verilog``, unless - called with ``-noblackbox`` will automatically set the blackbox attribute - on any empty module it reads. - -- The ``noblackbox`` attribute set on an empty module prevents ``read_verilog`` - from automatically setting the blackbox attribute on the module. - -- The ``whitebox`` attribute on modules triggers the same behavior as - ``blackbox``, but is for whitebox modules, i.e. library modules that - contain a behavioral model of the cell type. - -- The ``lib_whitebox`` attribute overwrites ``whitebox`` when ``read_verilog`` - is run in `-lib` mode. Otherwise it's automatically removed. - -- The ``dynports`` attribute is used by the Verilog front-end to mark modules - that have ports with a width that depends on a parameter. - -- The ``hdlname`` attribute is used by some passes to document the original - (HDL) name of a module when renaming a module. It should contain a single - name, or, when describing a hierarchical name in a flattened design, multiple - names separated by a single space character. - -- The ``keep`` attribute on cells and wires is used to mark objects that should - never be removed by the optimizer. This is used for example for cells that - have hidden connections that are not part of the netlist, such as IO pads. - Setting the ``keep`` attribute on a module has the same effect as setting it - on all instances of the module. - -- The ``keep_hierarchy`` attribute on cells and modules keeps the ``flatten`` - command from flattening the indicated cells and modules. - -- The ``init`` attribute on wires is set by the frontend when a register is - initialized "FPGA-style" with ``reg foo = val``. It can be used during - synthesis to add the necessary reset logic. - -- The ``top`` attribute on a module marks this module as the top of the - design hierarchy. The ``hierarchy`` command sets this attribute when called - with ``-top``. Other commands, such as ``flatten`` and various backends - use this attribute to determine the top module. - -- The ``src`` attribute is set on cells and wires created by to the string - ``:`` by the HDL front-end and is then carried - through the synthesis. When entities are combined, a new |-separated - string is created that contains all the string from the original entities. - -- The ``defaultvalue`` attribute is used to store default values for - module inputs. The attribute is attached to the input wire by the HDL - front-end when the input is declared with a default value. - -- The ``parameter`` and ``localparam`` attributes are used to mark wires - that represent module parameters or localparams (when the HDL front-end - is run in ``-pwires`` mode). - -- Wires marked with the ``hierconn`` attribute are connected to wires with the - same name (format ``cell_name.identifier``) when they are imported from - sub-modules by ``flatten``. - -- The ``clkbuf_driver`` attribute can be set on an output port of a blackbox - module to mark it as a clock buffer output, and thus prevent ``clkbufmap`` - from inserting another clock buffer on a net driven by such output. - -- The ``clkbuf_sink`` attribute can be set on an input port of a module to - request clock buffer insertion by the ``clkbufmap`` pass. - -- The ``clkbuf_inv`` attribute can be set on an output port of a module - with the value set to the name of an input port of that module. When - the ``clkbufmap`` would otherwise insert a clock buffer on this output, - it will instead try inserting the clock buffer on the input port (this - is used to implement clock inverter cells that clock buffer insertion - will "see through"). - -- The ``clkbuf_inhibit`` is the default attribute to set on a wire to prevent - automatic clock buffer insertion by ``clkbufmap``. This behaviour can be - overridden by providing a custom selection to ``clkbufmap``. - -- The ``invertible_pin`` attribute can be set on a port to mark it as - invertible via a cell parameter. The name of the inversion parameter - is specified as the value of this attribute. The value of the inversion - parameter must be of the same width as the port, with 1 indicating - an inverted bit and 0 indicating a non-inverted bit. - -- The ``iopad_external_pin`` attribute on a blackbox module's port marks - it as the external-facing pin of an I/O pad, and prevents ``iopadmap`` - from inserting another pad cell on it. - -- The module attribute ``abc9_lut`` is an integer attribute indicating to - `abc9` that this module describes a LUT with an area cost of this value, and - propagation delays described using `specify` statements. - -- The module attribute ``abc9_box`` is a boolean specifying a black/white-box - definition, with propagation delays described using `specify` statements, for - use by `abc9`. - -- The port attribute ``abc9_carry`` marks the carry-in (if an input port) and - carry-out (if output port) ports of a box. This information is necessary for - `abc9` to preserve the integrity of carry-chains. Specifying this attribute - onto a bus port will affect only its most significant bit. - -- The module attribute ``abc9_flop`` is a boolean marking the module as a - flip-flop. This allows `abc9` to analyse its contents in order to perform - sequential synthesis. - -- The frontend sets attributes ``always_comb``, ``always_latch`` and - ``always_ff`` on processes derived from SystemVerilog style always blocks - according to the type of the always. These are checked for correctness in - ``proc_dlatch``. - -- The cell attribute ``wildcard_port_conns`` represents wildcard port - connections (SystemVerilog ``.*``). These are resolved to concrete - connections to matching wires in ``hierarchy``. - -- In addition to the ``(* ... *)`` attribute syntax, Yosys supports - the non-standard ``{* ... *}`` attribute syntax to set default attributes - for everything that comes after the ``{* ... *}`` statement. (Reset - by adding an empty ``{* *}`` statement.) - -- In module parameter and port declarations, and cell port and parameter - lists, a trailing comma is ignored. This simplifies writing Verilog code - generators a bit in some cases. - -- Modules can be declared with ``module mod_name(...);`` (with three dots - instead of a list of module ports). With this syntax it is sufficient - to simply declare a module port as 'input' or 'output' in the module - body. - -- When defining a macro with `define, all text between triple double quotes - is interpreted as macro body, even if it contains unescaped newlines. The - triple double quotes are removed from the macro body. For example: - - `define MY_MACRO(a, b) """ - assign a = 23; - assign b = 42; - """ - -- The attribute ``via_celltype`` can be used to implement a Verilog task or - function by instantiating the specified cell type. The value is the name - of the cell type to use. For functions the name of the output port can - be specified by appending it to the cell type separated by a whitespace. - The body of the task or function is unused in this case and can be used - to specify a behavioral model of the cell type for simulation. For example: - - module my_add3(A, B, C, Y); - parameter WIDTH = 8; - input [WIDTH-1:0] A, B, C; - output [WIDTH-1:0] Y; - ... - endmodule - - module top; - ... - (* via_celltype = "my_add3 Y" *) - (* via_celltype_defparam_WIDTH = 32 *) - function [31:0] add3; - input [31:0] A, B, C; - begin - add3 = A + B + C; - end - endfunction - ... - endmodule - -- The ``wiretype`` attribute is added by the verilog parser for wires of a - typedef'd type to indicate the type identifier. - -- Various ``enum_value_{value}`` attributes are added to wires of an enumerated type - to give a map of possible enum items to their values. - -- The ``enum_base_type`` attribute is added to enum items to indicate which - enum they belong to (enums -- anonymous and otherwise -- are - automatically named with an auto-incrementing counter). Note that enums - are currently not strongly typed. - -- A limited subset of DPI-C functions is supported. The plugin mechanism - (see ``help plugin``) can be used to load .so files with implementations - of DPI-C routines. As a non-standard extension it is possible to specify - a plugin alias using the ``:`` syntax. For example: - - module dpitest; - import "DPI-C" function foo:round = real my_round (real); - parameter real r = my_round(12.345); - endmodule - - $ yosys -p 'plugin -a foo -i /lib/libm.so; read_verilog dpitest.v' - -- Sized constants (the syntax ``'s?[bodh]``) support constant - expressions as ````. If the expression is not a simple identifier, it - must be put in parentheses. Examples: ``WIDTH'd42``, ``(4+2)'b101010`` - -- The system tasks ``$finish``, ``$stop`` and ``$display`` are supported in - initial blocks in an unconditional context (only if/case statements on - expressions over parameters and constant values are allowed). The intended - use for this is synthesis-time DRC. - -- There is limited support for converting ``specify`` .. ``endspecify`` - statements to special ``$specify2``, ``$specify3``, and ``$specrule`` cells, - for use in blackboxes and whiteboxes. Use ``read_verilog -specify`` to - enable this functionality. (By default these blocks are ignored.) - -- The ``reprocess_after`` internal attribute is used by the Verilog frontend to - mark cells with bindings which might depend on the specified instantiated - module. Modules with such cells will be reprocessed during the ``hierarchy`` - pass once the referenced module definition(s) become available. - -- The ``smtlib2_module`` attribute can be set on a blackbox module to specify a - formal model directly using SMT-LIB 2. For such a module, the - ``smtlib2_comb_expr`` attribute can be used on output ports to define their - value using an SMT-LIB 2 expression. For example: - - (* blackbox *) - (* smtlib2_module *) - module submod(a, b); - input [7:0] a; - (* smtlib2_comb_expr = "(bvnot a)" *) - output [7:0] b; - endmodule - -Non-standard or SystemVerilog features for formal verification -============================================================== - -- Support for ``assert``, ``assume``, ``restrict``, and ``cover`` is enabled - when ``read_verilog`` is called with ``-formal``. - -- The system task ``$initstate`` evaluates to 1 in the initial state and - to 0 otherwise. - -- The system function ``$anyconst`` evaluates to any constant value. This is - equivalent to declaring a reg as ``rand const``, but also works outside - of checkers. (Yosys also supports ``rand const`` outside checkers.) - -- The system function ``$anyseq`` evaluates to any value, possibly a different - value in each cycle. This is equivalent to declaring a reg as ``rand``, - but also works outside of checkers. (Yosys also supports ``rand`` - variables outside checkers.) - -- The system functions ``$allconst`` and ``$allseq`` can be used to construct - formal exist-forall problems. Assumptions only hold if the trace satisfies - the assumption for all ``$allconst/$allseq`` values. For assertions and cover - statements it is sufficient if just one ``$allconst/$allseq`` value triggers - the property (similar to ``$anyconst/$anyseq``). - -- Wires/registers declared using the ``anyconst/anyseq/allconst/allseq`` attribute - (for example ``(* anyconst *) reg [7:0] foobar;``) will behave as if driven - by a ``$anyconst/$anyseq/$allconst/$allseq`` function. - -- The SystemVerilog tasks ``$past``, ``$stable``, ``$rose`` and ``$fell`` are - supported in any clocked block. - -- The syntax ``@($global_clock)`` can be used to create FFs that have no - explicit clock input (``$ff`` cells). The same can be achieved by using - ``@(posedge )`` or ``@(negedge )`` when ```` - is marked with the ``(* gclk *)`` Verilog attribute. - - -Supported features from SystemVerilog -===================================== - -When ``read_verilog`` is called with ``-sv``, it accepts some language features -from SystemVerilog: - -- The ``assert`` statement from SystemVerilog is supported in its most basic - form. In module context: ``assert property ();`` and within an - always block: ``assert();``. It is transformed to an ``$assert`` cell. - -- The ``assume``, ``restrict``, and ``cover`` statements from SystemVerilog are - also supported. The same limitations as with the ``assert`` statement apply. - -- The keywords ``always_comb``, ``always_ff`` and ``always_latch``, ``logic`` - and ``bit`` are supported. - -- Declaring free variables with ``rand`` and ``rand const`` is supported. - -- Checkers without a port list that do not need to be instantiated (but instead - behave like a named block) are supported. - -- SystemVerilog packages are supported. Once a SystemVerilog file is read - into a design with ``read_verilog``, all its packages are available to - SystemVerilog files being read into the same design afterwards. - -- typedefs are supported (including inside packages) - - type casts are currently not supported - -- enums are supported (including inside packages) - - but are currently not strongly typed - -- packed structs and unions are supported - - arrays of packed structs/unions are currently not supported - - structure literals are currently not supported - -- multidimensional arrays are supported - - array assignment of unpacked arrays is currently not supported - - array literals are currently not supported - -- SystemVerilog interfaces (SVIs) are supported. Modports for specifying whether - ports are inputs or outputs are supported. +Additional information +====================== -- Assignments within expressions are supported. +The ``read_verilog`` command, used by default when calling ``read`` with Verilog +source input, does not perform syntax checking. You should instead lint your +source with another tool such as +[Verilator](https://www.veripool.org/verilator/) first, e.g. by calling +``verilator --lint-only``. Building the documentation diff --git a/backends/aiger2/aiger.cc b/backends/aiger2/aiger.cc index a230cda4f9c..c7ed3b81f0f 100644 --- a/backends/aiger2/aiger.cc +++ b/backends/aiger2/aiger.cc @@ -262,7 +262,8 @@ struct Index { if (cell->type.in(ID($gt), ID($ge))) std::swap(aport, bport); int carry = cell->type.in(ID($le), ID($ge)) ? CFALSE : CTRUE; - Lit a, b; + Lit a = Writer::EMPTY_LIT; + Lit b = Writer::EMPTY_LIT; // TODO: this might not be the most economic structure; revisit at a later date for (int i = 0; i < width; i++) { a = visit(cursor, aport[i]); @@ -664,8 +665,6 @@ struct Index { struct AigerWriter : Index { typedef unsigned int Lit; - const static Lit CONST_FALSE = 0; - const static Lit CONST_TRUE = 1; const static constexpr Lit EMPTY_LIT = std::numeric_limits::max(); static Lit negate(Lit lit) { @@ -802,8 +801,6 @@ struct AigerWriter : Index { }; struct XAigerAnalysis : Index { - const static int CONST_FALSE = 0; - const static int CONST_TRUE = 0; const static constexpr int EMPTY_LIT = -1; XAigerAnalysis() @@ -835,12 +832,8 @@ struct XAigerAnalysis : Index { return false; Cell *driver = bit.wire->driverCell(); - if (!driver->type.isPublic()) - return false; - Module *mod = design->module(driver->type); - log_assert(mod); - if (!mod->has_attribute(ID::abc9_box_id)) + if (!mod || !mod->has_attribute(ID::abc9_box_id)) return false; int max = 1; @@ -873,7 +866,7 @@ struct XAigerAnalysis : Index { HierCursor cursor; for (auto box : top_minfo->found_blackboxes) { Module *def = design->module(box->type); - if (!box->type.isPublic() || (def && !def->has_attribute(ID::abc9_box_id))) + if (!(def && def->has_attribute(ID::abc9_box_id))) for (auto &conn : box->connections_) if (box->output(conn.first)) for (auto bit : conn.second) @@ -888,7 +881,7 @@ struct XAigerAnalysis : Index { for (auto box : top_minfo->found_blackboxes) { Module *def = design->module(box->type); - if (!box->type.isPublic() || (def && !def->has_attribute(ID::abc9_box_id))) + if (!(def && def->has_attribute(ID::abc9_box_id))) for (auto &conn : box->connections_) if (box->input(conn.first)) for (auto bit : conn.second) @@ -1109,7 +1102,7 @@ struct XAigerWriter : AigerWriter { holes_module->ports.push_back(w->name); holes_pis.push_back(w); } - in_conn.append(holes_pis[i]); + in_conn.append(holes_pis[holes_pi_idx]); holes_pi_idx++; } holes_wb->setPort(port_id, in_conn); diff --git a/backends/btor/btor.cc b/backends/btor/btor.cc index c3637bc8f95..43d1896797e 100644 --- a/backends/btor/btor.cc +++ b/backends/btor/btor.cc @@ -832,7 +832,10 @@ struct BtorWorker } } - if (constword) + // If not fully defined, undef bits should be able to take a + // different value for each address so we can't initialise from + // one value (and btor2parser doesn't like it) + if (constword && firstword.is_fully_def()) { if (verbose) btorf("; initval = %s\n", log_signal(firstword)); @@ -1077,6 +1080,7 @@ struct BtorWorker btorf("%d input %d\n", nid, sid); ywmap_input(s); nid_width[nid] = GetSize(s); + add_nid_sig(nid, s); for (int j = 0; j < GetSize(s); j++) nidbits.push_back(make_pair(nid, j)); diff --git a/backends/btor/test_cells.sh b/backends/btor/test_cells.sh index f8bd797825e..fc7f5b75caa 100755 --- a/backends/btor/test_cells.sh +++ b/backends/btor/test_cells.sh @@ -10,11 +10,11 @@ cd test_cells.tmp for fn in test_*.il; do ../../../yosys -p " - read_ilang $fn + read_rtlil $fn rename gold gate synth - read_ilang $fn + read_rtlil $fn miter -equiv -make_assert -flatten gold gate main hierarchy -top main write_btor ${fn%.il}.btor diff --git a/backends/cxxrtl/cxxrtl_backend.cc b/backends/cxxrtl/cxxrtl_backend.cc index a56bfc03639..b72caf119c3 100644 --- a/backends/cxxrtl/cxxrtl_backend.cc +++ b/backends/cxxrtl/cxxrtl_backend.cc @@ -612,7 +612,7 @@ std::string escape_c_string(const std::string &input) output.push_back('"'); for (auto c : input) { if (::isprint(c)) { - if (c == '\\') + if (c == '\\' || c == '"') output.push_back('\\'); output.push_back(c); } else { @@ -2497,7 +2497,7 @@ struct CxxrtlWorker { // Alias of a member wire const RTLIL::Wire *aliasee = debug_wire_type.sig_subst.as_wire(); f << indent << "items->add(path, " << escape_cxx_string(get_hdl_name(wire)) << ", "; - dump_debug_attrs(aliasee); + dump_debug_attrs(wire); f << ", "; // If the aliasee is an outline, then the alias must be an outline, too; otherwise downstream // tooling has no way to find out about the outline. diff --git a/backends/edif/edif.cc b/backends/edif/edif.cc index c664c41eb7c..d751a5996f5 100644 --- a/backends/edif/edif.cc +++ b/backends/edif/edif.cc @@ -338,7 +338,7 @@ struct EdifBackend : public Backend { *f << stringf("\n (property %s (integer %u))", EDIF_DEF(name), val.as_int()); else { std::string hex_string = ""; - for (size_t i = 0; i < val.size(); i += 4) { + for (auto i = 0; i < val.size(); i += 4) { int digit_value = 0; if (i+0 < val.size() && val.at(i+0) == RTLIL::State::S1) digit_value |= 1; if (i+1 < val.size() && val.at(i+1) == RTLIL::State::S1) digit_value |= 2; diff --git a/backends/json/json.cc b/backends/json/json.cc index 2f442c494f7..287c01ead35 100644 --- a/backends/json/json.cc +++ b/backends/json/json.cc @@ -353,6 +353,9 @@ struct JsonBackend : public Backend { log(" emit 32-bit or smaller fully-defined parameter values directly\n"); log(" as JSON numbers (for compatibility with old parsers)\n"); log("\n"); + log(" -selected\n"); + log(" output only select module\n"); + log("\n"); log("\n"); log("The general syntax of the JSON output created by this command is as follows:\n"); log("\n"); @@ -597,6 +600,7 @@ struct JsonBackend : public Backend { { bool aig_mode = false; bool compat_int_mode = false; + bool use_selection = false; size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) @@ -609,13 +613,17 @@ struct JsonBackend : public Backend { compat_int_mode = true; continue; } + if (args[argidx] == "-selected") { + use_selection = true; + continue; + } break; } extra_args(f, filename, args, argidx); log_header(design, "Executing JSON backend.\n"); - JsonWriter json_writer(*f, false, aig_mode, compat_int_mode); + JsonWriter json_writer(*f, use_selection, aig_mode, compat_int_mode); json_writer.write_design(design); } } JsonBackend; diff --git a/backends/rtlil/rtlil_backend.cc b/backends/rtlil/rtlil_backend.cc index 462401fb69d..113f1a61513 100644 --- a/backends/rtlil/rtlil_backend.cc +++ b/backends/rtlil/rtlil_backend.cc @@ -464,21 +464,6 @@ struct RTLILBackend : public Backend { } } RTLILBackend; -struct IlangBackend : public Backend { - IlangBackend() : Backend("ilang", "(deprecated) alias of write_rtlil") { } - void help() override - { - // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| - log("\n"); - log("See `help write_rtlil`.\n"); - log("\n"); - } - void execute(std::ostream *&f, std::string filename, std::vector args, RTLIL::Design *design) override - { - RTLILBackend.execute(f, filename, args, design); - } -} IlangBackend; - struct DumpPass : public Pass { DumpPass() : Pass("dump", "print parts of the design in RTLIL format") { } void help() override diff --git a/backends/smt2/smt2.cc b/backends/smt2/smt2.cc index 55c5c03f551..a6e1965ba5c 100644 --- a/backends/smt2/smt2.cc +++ b/backends/smt2/smt2.cc @@ -329,13 +329,14 @@ struct Smt2Worker { sigmap.apply(bit); + if (bit_driver.count(bit)) { + export_cell(bit_driver.at(bit)); + sigmap.apply(bit); + } + if (bit.wire == nullptr) return bit == RTLIL::State::S1 ? "true" : "false"; - if (bit_driver.count(bit)) - export_cell(bit_driver.at(bit)); - sigmap.apply(bit); - if (fcache.count(bit) == 0) { if (verbose) log("%*s-> external bool: %s\n", 2+2*GetSize(recursive_cells), "", log_signal(bit)); diff --git a/backends/smt2/test_cells.sh b/backends/smt2/test_cells.sh index 3f84d65a21a..33c1b9989bb 100644 --- a/backends/smt2/test_cells.sh +++ b/backends/smt2/test_cells.sh @@ -21,7 +21,7 @@ EOT for x in $(set +x; ls test_*.il | sort -R); do x=${x%.il} cat > $x.ys <<- EOT - read_ilang $x.il + read_rtlil $x.il copy gold gate cd gate diff --git a/backends/smv/test_cells.sh b/backends/smv/test_cells.sh index 1347b70446b..f2c5ff09dfa 100644 --- a/backends/smv/test_cells.sh +++ b/backends/smv/test_cells.sh @@ -17,11 +17,11 @@ EOT for fn in test_*.il; do ../../../yosys -p " - read_ilang $fn + read_rtlil $fn rename gold gate synth - read_ilang $fn + read_rtlil $fn miter -equiv -flatten gold gate main hierarchy -top main write_smv -tpl template.txt ${fn#.il}.smv diff --git a/backends/verilog/verilog_backend.cc b/backends/verilog/verilog_backend.cc index 04b87b40d1d..fa9100635f5 100644 --- a/backends/verilog/verilog_backend.cc +++ b/backends/verilog/verilog_backend.cc @@ -1071,7 +1071,7 @@ bool dump_cell_expr(std::ostream &f, std::string indent, RTLIL::Cell *cell) return true; } - if (cell->type == ID($_BUF_)) { + if (cell->type.in(ID($_BUF_), ID($buf))) { f << stringf("%s" "assign ", indent.c_str()); dump_sigspec(f, cell->getPort(ID::Y)); f << stringf(" = "); @@ -2332,19 +2332,15 @@ void dump_module(std::ostream &f, std::string indent, RTLIL::Module *module) dump_attributes(f, indent, module->attributes, "\n", /*modattr=*/true); f << stringf("%s" "module %s(", indent.c_str(), id(module->name, false).c_str()); - bool keep_running = true; int cnt = 0; - for (int port_id = 1; keep_running; port_id++) { - keep_running = false; - for (auto wire : module->wires()) { - if (wire->port_id == port_id) { - if (port_id != 1) - f << stringf(", "); - f << stringf("%s", id(wire->name).c_str()); - keep_running = true; - if (cnt==20) { f << stringf("\n"); cnt = 0; } else cnt++; - continue; - } + for (auto port : module->ports) { + Wire *wire = module->wire(port); + if (wire) { + if (port != module->ports[0]) + f << stringf(", "); + f << stringf("%s", id(wire->name).c_str()); + if (cnt==20) { f << stringf("\n"); cnt = 0; } else cnt++; + continue; } } f << stringf(");\n"); diff --git a/docs/source/_images/internals/overview_flow.tex b/docs/source/_images/internals/overview_flow.tex index ac0afde5fda..b3f12626a7c 100644 --- a/docs/source/_images/internals/overview_flow.tex +++ b/docs/source/_images/internals/overview_flow.tex @@ -15,23 +15,23 @@ \tikzstyle{data} = [draw, fill=blue!10, ellipse, minimum height=3em, minimum width=7em, node distance=15em] \node[process] (vlog) {Verilog Frontend}; \node[process, dashed, fill=green!5] (vhdl) [right of=vlog] {VHDL Frontend}; - \node[process] (ilang) [right of=vhdl] {RTLIL Frontend}; + \node[process] (rtlilfe) [right of=vhdl] {RTLIL Frontend}; \node[data] (ast) [below of=vlog, node distance=5em, xshift=7.5em] {AST}; \node[process] (astfe) [below of=ast, node distance=5em] {AST Frontend}; \node[data] (rtlil) [below of=astfe, node distance=5em, xshift=7.5em] {RTLIL}; \node[process] (pass) [right of=rtlil, node distance=5em, xshift=7.5em] {Passes}; \node[process] (vlbe) [below of=rtlil, node distance=7em, xshift=-13em] {Verilog Backend}; - \node[process] (ilangbe) [below of=rtlil, node distance=7em, xshift=0em] {RTLIL Backend}; + \node[process] (rtlilbe) [below of=rtlil, node distance=7em, xshift=0em] {RTLIL Backend}; \node[process, dashed, fill=green!5] (otherbe) [below of=rtlil, node distance=7em, xshift=+13em] {Other Backends}; \draw[-latex] (vlog) -- (ast); \draw[-latex] (vhdl) -- (ast); \draw[-latex] (ast) -- (astfe); \draw[-latex] (astfe) -- (rtlil); - \draw[-latex] (ilang) -- (rtlil); + \draw[-latex] (rtlilfe) -- (rtlil); \draw[latex-latex] (rtlil) -- (pass); \draw[-latex] (rtlil) -- (vlbe); - \draw[-latex] (rtlil) -- (ilangbe); + \draw[-latex] (rtlil) -- (rtlilbe); \draw[-latex] (rtlil) -- (otherbe); \end{tikzpicture} \end{document} diff --git a/docs/source/appendix/rtlil_text.rst b/docs/source/appendix/rtlil_text.rst index 2c7a82d190c..b1bc9c5822c 100644 --- a/docs/source/appendix/rtlil_text.rst +++ b/docs/source/appendix/rtlil_text.rst @@ -242,7 +242,7 @@ Processes Declares a process, with zero or more attributes, with the given identifier in the enclosing module. The body of a process consists of zero or more -assignments, exactly one switch, and zero or more syncs. +assignments followed by zero or more switches and zero or more syncs. See :ref:`sec:rtlil_process` for an overview of processes. @@ -250,7 +250,7 @@ See :ref:`sec:rtlil_process` for an overview of processes. ::= * ::= process - ::= * ? * * + ::= * * * ::= assign ::= ::= @@ -262,8 +262,8 @@ Switches Switches test a signal for equality against a list of cases. Each case specifies a comma-separated list of signals to check against. If there are no signals in the list, then the case is the default case. The body of a case consists of zero -or more switches and assignments. Both switches and cases may have zero or more -attributes. +or more assignments followed by zero or more switches. Both switches and cases +may have zero or more attributes. .. code:: BNF @@ -272,7 +272,7 @@ attributes. ::= * ::= case ? ::= (, )* - ::= ( | )* + ::= * * ::= end Syncs diff --git a/docs/source/code_examples/fifo/fifo.v b/docs/source/code_examples/fifo/fifo.v index 769dfafd4c9..86f29240680 100644 --- a/docs/source/code_examples/fifo/fifo.v +++ b/docs/source/code_examples/fifo/fifo.v @@ -5,7 +5,7 @@ module addr_gen ) ( input en, clk, rst, output reg [AWIDTH-1:0] addr ); - initial addr <= 0; + initial addr = 0; // async reset // increment address when enabled @@ -13,7 +13,7 @@ module addr_gen if (rst) addr <= 0; else if (en) begin - if (addr == MAX_DATA-1) + if ({'0, addr} == MAX_DATA-1) addr <= 0; else addr <= addr + 1; @@ -57,7 +57,7 @@ module fifo ); // status signals - initial count <= 0; + initial count = 0; always @(posedge clk or posedge rst) begin if (rst) diff --git a/docs/source/conf.py b/docs/source/conf.py index 0de8cd44513..8c8555b715b 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -6,7 +6,7 @@ project = 'YosysHQ Yosys' author = 'YosysHQ GmbH' copyright ='2024 YosysHQ GmbH' -yosys_ver = "0.47" +yosys_ver = "0.48" # select HTML theme html_theme = 'furo-ys' @@ -56,6 +56,9 @@ else: release = yosys_ver todo_include_todos = False +elif os.getenv("YOSYS_DOCS_RELEASE") is not None: + release = yosys_ver + todo_include_todos = False else: release = yosys_ver todo_include_todos = True @@ -87,5 +90,9 @@ def setup(app: Sphinx) -> None: from util.RtlilLexer import RtlilLexer app.add_lexer("RTLIL", RtlilLexer) - from furo_ys.lexers.YoscryptLexer import YoscryptLexer - app.add_lexer("yoscrypt", YoscryptLexer) + try: + from furo_ys.lexers.YoscryptLexer import YoscryptLexer + app.add_lexer("yoscrypt", YoscryptLexer) + except ModuleNotFoundError: + from pygments.lexers.special import TextLexer + app.add_lexer("yoscrypt", TextLexer) diff --git a/docs/source/getting_started/example_synth.rst b/docs/source/getting_started/example_synth.rst index f8530b45bbf..189eaddfada 100644 --- a/docs/source/getting_started/example_synth.rst +++ b/docs/source/getting_started/example_synth.rst @@ -30,6 +30,14 @@ First, let's quickly look at the design we'll be synthesizing: .. todo:: fifo.v description +While the open source `read_verilog` frontend generally does a pretty good job +at processing valid Verilog input, it does not provide very good error handling +or reporting. Using an external tool such as `verilator`_ before running Yosys +is highly recommended. We can quickly check the Verilog syntax of our design by +calling ``verilator --lint-only fifo.v``. + +.. _verilator: https://www.veripool.org/verilator/ + Loading the design ~~~~~~~~~~~~~~~~~~ diff --git a/docs/source/getting_started/installation.rst b/docs/source/getting_started/installation.rst index 57ae5303639..4f31af96627 100644 --- a/docs/source/getting_started/installation.rst +++ b/docs/source/getting_started/installation.rst @@ -62,9 +62,25 @@ The `OSS CAD Suite`_ releases `nightly builds`_ for the following architectures: Building from source ~~~~~~~~~~~~~~~~~~~~ -Refer to the `readme`_ for the most up-to-date install instructions. +The Yosys source files can be obtained from the `YosysHQ/Yosys git repository`_. +`ABC`_ and some of the other libraries used are included as git submodules. To +clone these submodules at the same time, use e.g.: -.. _readme: https://github.com/YosysHQ/yosys#building-from-source +.. code:: console + + git clone --recurse-submodules https://github.com/YosysHQ/yosys.git # ..or.. + git clone https://github.com/YosysHQ/yosys.git + cd yosys + git submodule update --init --recursive + +.. _YosysHQ/Yosys git repository: https://github.com/yosyshq/yosys/ +.. _ABC: https://github.com/berkeley-abc/abc + +.. note:: + + As of Yosys v0.47, releases include a ``yosys.tar.gz`` file which includes + all source code and all sub-modules in a single archive. This can be used as + an alternative which does not rely on ``git``. Supported platforms ^^^^^^^^^^^^^^^^^^^ @@ -94,48 +110,136 @@ Installing all prerequisites for Ubuntu 20.04: .. code:: console - sudo sudo apt-get install build-essential clang lld bison flex \ - libreadline-dev gawk tcl-dev libffi-dev git make \ - graphviz xdot pkg-config python3 libboost-system-dev \ + sudo apt-get install gperf build-essential bison flex \ + libreadline-dev gawk tcl-dev libffi-dev git graphviz \ + xdot pkg-config python3 libboost-system-dev \ libboost-python-dev libboost-filesystem-dev zlib1g-dev -Installing all prerequisites for macOS 11 (with Homebrew): +Installing all prerequisites for macOS 13 (with Homebrew): .. code:: console - brew install bison flex gawk libffi git graphviz \ - pkg-config python3 tcl-tk xdot bash boost-python3 + brew tap Homebrew/bundle && brew bundle -Running the build system -^^^^^^^^^^^^^^^^^^^^^^^^ +or MacPorts: -From the root ``yosys`` directory, call the following commands: +.. code:: console + + sudo port install bison flex readline gawk libffi graphviz \ + pkgconfig python311 boost zlib tcl + +On FreeBSD use the following command to install all prerequisites: .. code:: console - - make - sudo make install -This will build and then install Yosys, making it available on the command line -as ``yosys``. Note that this also downloads, builds, and installs `ABC`_ (using -:program:`yosys-abc` as the executable name). + pkg install bison flex readline gawk libffi graphviz \ + pkgconf python311 tcl-wrapper boost-libs -.. _ABC: https://github.com/berkeley-abc/abc +.. note:: On FreeBSD system use gmake instead of make. To run tests use: + ``MAKE=gmake CXX=cxx CC=cc gmake test`` + +For Cygwin use the following command to install all prerequisites, or select these additional packages: + +.. code:: console + + setup-x86_64.exe -q --packages=bison,flex,gcc-core,gcc-g++,git,libffi-devel,libreadline-devel,make,pkg-config,python3,tcl-devel,boost-build,zlib-devel + +.. warning:: + + As of this writing, Cygwin only supports up to Python 3.9.16 while the + minimum required version of Python is 3.11. This means that Cygwin is not + compatible with many of the Python-based frontends. While this does not + currently prevent Yosys itself from working, no guarantees are made for + continued support. It is instead recommended to use Windows Subsystem for + Linux (WSL) and follow the instructions for Ubuntu. + +.. + For MSYS2 (MINGW64): + + .. code:: console + + pacman -S bison flex mingw-w64-x86_64-gcc git libffi-devel libreadline-devel make pkg-config python3 tcl-devel mingw-w64-x86_64-boost zlib-devel + + Not that I can get this to work; it's failing during ld with what looks like + math library issues: ``multiple definition of `tanh'`` and + ``undefined reference to `__imp_acosh'``, as well as issues in `aiger2` with + ``seekg`` et al not being available. + + .. note:: + + The ``config-msys2-64`` target uses the ``mingw-w64-x86_64-`` prefixed + compiler in order to allow compiled exe files to be run without an MSYS2 + shell. + +Build configuration +^^^^^^^^^^^^^^^^^^^ -The default compiler is ``clang``, to change between ``clang`` and ``gcc``, use -one of the following: +The Yosys build is based solely on Makefiles, and uses a number of variables +which influence the build process. The recommended method for configuring +builds is with a ``Makefile.conf`` file in the root ``yosys`` directory. The +following commands will clean the directory and provide an initial configuration +file: .. code:: console - make config-clang + make config-clang # ..or.. make config-gcc -To use a compiler different than the default, use: +Check the root Makefile to see what other configuration targets are available. +Other variables can then be added to the ``Makefile.conf`` as needed, for +example: + +.. code:: console + + echo "ENABLE_ZLIB := 0" >> Makefile.conf + +Using one of these targets will set the ``CONFIG`` variable to something other +than ``none``, and will override the environment variable for ``CXX``. To use a +different compiler than the default when building, use: .. code:: console + make CXX=$CXX # ..or.. make CXX="g++-11" +.. note:: + + Setting the compiler in this way will prevent some other options such as + ``ENABLE_CCACHE`` from working as expected. + +If you have clang, and (a compatible version of) ``ld.lld`` available in PATH, +it's recommended to speed up incremental builds with lld by enabling LTO with +``ENABLE_LTO=1``. On macOS, LTO requires using clang from homebrew rather than +clang from xcode. For example: + +.. code:: console + + make ENABLE_LTO=1 CXX=$(brew --prefix)/opt/llvm/bin/clang++ + +By default, building (and installing) yosys will build (and install) `ABC`_, +using :program:`yosys-abc` as the executable name. To use an existing ABC +executable instead, set the ``ABCEXTERNAL`` make variable to point to the +desired executable. + +Running the build system +^^^^^^^^^^^^^^^^^^^^^^^^ + +From the root ``yosys`` directory, call the following commands: + +.. code:: console + + make + sudo make install + +To use a separate (out-of-tree) build directory, provide a path to the Makefile. + +.. code:: console + + mkdir build; cd build + make -f ../Makefile + +Out-of-tree builds require a clean source tree. + .. seealso:: Refer to :doc:`/yosys_internals/extending_yosys/test_suites` for details on @@ -161,10 +265,6 @@ directories: ``frontends/`` This directory contains a subdirectory for each of the frontend modules. -``guidelines/`` - Contains developer guidelines, including the code of conduct and coding style - guide. - ``kernel/`` This directory contains all the core functionality of Yosys. This includes the functions and definitions for working with the RTLIL data structures @@ -206,9 +306,6 @@ commands. Good starting points for reading example source code to learn how to write passes are :file:`passes/opt/opt_dff.cc` and :file:`passes/opt/opt_merge.cc`. -See the top-level README file for a quick Getting Started guide and build -instructions. The Yosys build is based solely on Makefiles. - Users of the Qt Creator IDE can generate a QT Creator project file using make qtcreator. Users of the Eclipse IDE can use the "Makefile Project with Existing Code" project type in the Eclipse "New Project" dialog (only available after the diff --git a/docs/source/introduction.rst b/docs/source/introduction.rst index 7261d8edb74..376c8043b32 100644 --- a/docs/source/introduction.rst +++ b/docs/source/introduction.rst @@ -69,9 +69,14 @@ Things you can't do - Check out `nextpnr`_ for that +- Rely on built-in syntax checking + + - Use an external tool like `verilator`_ instead + .. todo:: nextpnr for FPGAs, consider mentioning openlane, vpr, coriolis .. _nextpnr: https://github.com/YosysHQ/nextpnr +.. _verilator: https://www.veripool.org/verilator/ The Yosys family ---------------- diff --git a/docs/source/yosys_internals/extending_yosys/contributing.rst b/docs/source/yosys_internals/extending_yosys/contributing.rst new file mode 100644 index 00000000000..e06de61b165 --- /dev/null +++ b/docs/source/yosys_internals/extending_yosys/contributing.rst @@ -0,0 +1,36 @@ +Contributing to Yosys +===================== + +Coding Style +------------ + +Formatting of code +~~~~~~~~~~~~~~~~~~ + +- Yosys code is using tabs for indentation. Tabs are 8 characters. + +- A continuation of a statement in the following line is indented by two + additional tabs. + +- Lines are as long as you want them to be. A good rule of thumb is to break + lines at about column 150. + +- Opening braces can be put on the same or next line as the statement opening + the block (if, switch, for, while, do). Put the opening brace on its own line + for larger blocks, especially blocks that contains blank lines. + +- Otherwise stick to the `Linux Kernel Coding Style`_. + +.. _Linux Kernel Coding Style: https://www.kernel.org/doc/Documentation/process/coding-style.rst + + +C++ Language +~~~~~~~~~~~~ + +Yosys is written in C++17. + +In general Yosys uses ``int`` instead of ``size_t``. To avoid compiler warnings +for implicit type casts, always use ``GetSize(foobar)`` instead of +``foobar.size()``. (``GetSize()`` is defined in :file:`kernel/yosys.h`) + +Use range-based for loops whenever applicable. diff --git a/docs/source/yosys_internals/extending_yosys/extensions.rst b/docs/source/yosys_internals/extending_yosys/extensions.rst index 4063641d479..b02c4cd992f 100644 --- a/docs/source/yosys_internals/extending_yosys/extensions.rst +++ b/docs/source/yosys_internals/extending_yosys/extensions.rst @@ -11,11 +11,7 @@ Writing extensions This chapter contains some bits and pieces of information about programming yosys extensions. Don't be afraid to ask questions on the YosysHQ Slack. -The :file:`guidelines/` directory of the Yosys source code contains notes on -various aspects of Yosys development. In particular, the files GettingStarted -and CodingStyle may be of interest. - -.. todo:: what's in guidelines/GettingStarted that's missing from the manual? +.. todo:: mention coding guide Quick guide ----------- diff --git a/docs/source/yosys_internals/extending_yosys/index.rst b/docs/source/yosys_internals/extending_yosys/index.rst index 88f36d5264e..4ee21517bba 100644 --- a/docs/source/yosys_internals/extending_yosys/index.rst +++ b/docs/source/yosys_internals/extending_yosys/index.rst @@ -11,5 +11,6 @@ of interest for developers looking to customise Yosys builds. extensions build_verific functional_ir + contributing test_suites diff --git a/docs/source/yosys_internals/extending_yosys/test_suites.rst b/docs/source/yosys_internals/extending_yosys/test_suites.rst index 7a6b7497774..264b97ce1db 100644 --- a/docs/source/yosys_internals/extending_yosys/test_suites.rst +++ b/docs/source/yosys_internals/extending_yosys/test_suites.rst @@ -1,7 +1,7 @@ Testing Yosys ============= -.. todo:: more about the included test suite +.. TODO:: more about the included test suite and how to add tests Automatic testing ----------------- @@ -23,3 +23,79 @@ For up to date information, including OS versions, refer to `the git actions page`_. .. _the git actions page: https://github.com/YosysHQ/yosys/actions + +.. todo:: are unit tests currently working + +.. + How to add a unit test + ---------------------- + + Unit test brings some advantages, briefly, we can list some of them (reference + [1](https://en.wikipedia.org/wiki/Unit_testing)): + + * Tests reduce bugs in new features; + * Tests reduce bugs in existing features; + * Tests are good documentation; + * Tests reduce the cost of change; + * Tests allow refactoring; + + With those advantages in mind, it was required to choose a framework which fits + well with C/C++ code. Hence, `google test`_ was chosen, because it is widely + used and it is relatively easy learn. + + .. _google test: https://github.com/google/googletest + + Install and configure google test (manually) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + In this section, you will see a brief description of how to install google test. + However, it is strongly recommended that you take a look to the official + repository (https://github.com/google/googletest) and refers to that if you have + any problem to install it. Follow the steps below: + + * Install: cmake and pthread + * Clone google test project from: https://github.com/google/googletest and enter + in the project directory + * Inside project directory, type: + + .. code-block:: console + + cmake -DBUILD_SHARED_LIBS=ON . + make + + * After compilation, copy all ``*.so`` inside directory ``googlemock`` and + ``googlemock/gtest`` to ``/usr/lib/`` + * Done! Now you can compile your tests. + + If you have any problem, go to the official repository to find help. + + Ps.: Some distros already have googletest packed. If your distro supports it, + you can use it instead of compile. + + Create a new unit test + ~~~~~~~~~~~~~~~~~~~~~~ + + If you want to add new unit tests for Yosys, just follow the steps below: + + * Go to directory :file:`test/unit/` + * In this directory you can find something similar Yosys's directory structure. + To create your unit test file you have to follow this pattern: + fileNameToImplementUnitTest + Test.cc. E.g.: if you want to implement the unit + test for ``kernel/celledges.cc``, you will need to create a file like this: + ``tests/unit/kernel/celledgesTest.cc``; + * Implement your unit test + + Run unit tests + ~~~~~~~~~~~~~~ + + To compile and run all unit tests, just go to yosys root directory and type: + + .. code-block:: console + + make unit-test + + If you want to remove all unit test files, type: + + .. code-block:: console + + make clean-unit-test diff --git a/docs/source/yosys_internals/index.rst b/docs/source/yosys_internals/index.rst index efd865623b0..9631e865331 100644 --- a/docs/source/yosys_internals/index.rst +++ b/docs/source/yosys_internals/index.rst @@ -38,3 +38,4 @@ as reference to implement a similar system in any language. formats/index extending_yosys/index techmap + verilog diff --git a/docs/source/yosys_internals/verilog.rst b/docs/source/yosys_internals/verilog.rst new file mode 100644 index 00000000000..a1941963d7e --- /dev/null +++ b/docs/source/yosys_internals/verilog.rst @@ -0,0 +1,377 @@ +Notes on Verilog support in Yosys +================================= + +.. TODO:: how much of this is specific to the read_verilog and should be in :doc:`/yosys_internals/flow/verilog_frontend`? + +Unsupported Verilog-2005 Features +--------------------------------- + +The following Verilog-2005 features are not supported by +Yosys and there are currently no plans to add support +for them: + +- Non-synthesizable language features as defined in + IEC 62142(E):2005 / IEEE Std. 1364.1(E):2002 + +- The ``tri``, ``triand`` and ``trior`` net types + +- The ``config`` and ``disable`` keywords and library map files + + +Verilog Attributes and non-standard features +-------------------------------------------- + +- The ``full_case`` attribute on case statements is supported (also the + non-standard ``// synopsys full_case`` directive) + +- The ``parallel_case`` attribute on case statements is supported (also the + non-standard ``// synopsys parallel_case`` directive) + +- The ``// synopsys translate_off`` and ``// synopsys translate_on`` directives + are also supported (but the use of ``` `ifdef .. `endif ``` is strongly + recommended instead). + +- The ``nomem2reg`` attribute on modules or arrays prohibits the automatic early + conversion of arrays to separate registers. This is potentially dangerous. + Usually the front-end has good reasons for converting an array to a list of + registers. Prohibiting this step will likely result in incorrect synthesis + results. + +- The ``mem2reg`` attribute on modules or arrays forces the early conversion of + arrays to separate registers. + +- The ``nomeminit`` attribute on modules or arrays prohibits the creation of + initialized memories. This effectively puts ``mem2reg`` on all memories that + are written to in an ``initial`` block and are not ROMs. + +- The ``nolatches`` attribute on modules or always-blocks prohibits the + generation of logic-loops for latches. Instead all not explicitly assigned + values default to x-bits. This does not affect clocked storage elements such + as flip-flops. + +- The ``nosync`` attribute on registers prohibits the generation of a storage + element. The register itself will always have all bits set to 'x' (undefined). + The variable may only be used as blocking assigned temporary variable within + an always block. This is mostly used internally by Yosys to synthesize Verilog + functions and access arrays. + +- The ``nowrshmsk`` attribute on a register prohibits the generation of + shift-and-mask type circuits for writing to bit slices of that register. + +- The ``onehot`` attribute on wires mark them as one-hot state register. This is + used for example for memory port sharing and set by the fsm_map pass. + +- The ``blackbox`` attribute on modules is used to mark empty stub modules that + have the same ports as the real thing but do not contain information on the + internal configuration. This modules are only used by the synthesis passes to + identify input and output ports of cells. The Verilog backend also does not + output blackbox modules on default. `read_verilog`, unless called with + ``-noblackbox`` will automatically set the blackbox attribute on any empty + module it reads. + +- The ``noblackbox`` attribute set on an empty module prevents `read_verilog` + from automatically setting the blackbox attribute on the module. + +- The ``whitebox`` attribute on modules triggers the same behavior as + ``blackbox``, but is for whitebox modules, i.e. library modules that contain a + behavioral model of the cell type. + +- The ``lib_whitebox`` attribute overwrites ``whitebox`` when `read_verilog` is + run in ``-lib`` mode. Otherwise it's automatically removed. + +- The ``dynports`` attribute is used by the Verilog front-end to mark modules + that have ports with a width that depends on a parameter. + +- The ``hdlname`` attribute is used by some passes to document the original + (HDL) name of a module when renaming a module. It should contain a single + name, or, when describing a hierarchical name in a flattened design, multiple + names separated by a single space character. + +- The ``keep`` attribute on cells and wires is used to mark objects that should + never be removed by the optimizer. This is used for example for cells that + have hidden connections that are not part of the netlist, such as IO pads. + Setting the ``keep`` attribute on a module has the same effect as setting it + on all instances of the module. + +- The ``keep_hierarchy`` attribute on cells and modules keeps the `flatten` + command from flattening the indicated cells and modules. + +- The `gate_cost_equivalent` attribute on a module can be used to specify + the estimated cost of the module as a number of basic gate instances. See + the help message of command `keep_hierarchy` which interprets this + attribute. + +- The ``init`` attribute on wires is set by the frontend when a register is + initialized "FPGA-style" with ``reg foo = val``. It can be used during + synthesis to add the necessary reset logic. + +- The ``top`` attribute on a module marks this module as the top of the design + hierarchy. The `hierarchy` command sets this attribute when called with + ``-top``. Other commands, such as `flatten` and various backends use this + attribute to determine the top module. + +- The ``src`` attribute is set on cells and wires created by to the string + ``:`` by the HDL front-end and is then carried + through the synthesis. When entities are combined, a new \|-separated string + is created that contains all the strings from the original entities. + +- The ``defaultvalue`` attribute is used to store default values for module + inputs. The attribute is attached to the input wire by the HDL front-end when + the input is declared with a default value. + +- The ``parameter`` and ``localparam`` attributes are used to mark wires that + represent module parameters or localparams (when the HDL front-end is run in + ``-pwires`` mode). + +- Wires marked with the ``hierconn`` attribute are connected to wires with the + same name (format ``cell_name.identifier``) when they are imported from + sub-modules by `flatten`. + +- The ``clkbuf_driver`` attribute can be set on an output port of a blackbox + module to mark it as a clock buffer output, and thus prevent `clkbufmap` from + inserting another clock buffer on a net driven by such output. + +- The ``clkbuf_sink`` attribute can be set on an input port of a module to + request clock buffer insertion by the `clkbufmap` pass. + +- The ``clkbuf_inv`` attribute can be set on an output port of a module with the + value set to the name of an input port of that module. When the `clkbufmap` + would otherwise insert a clock buffer on this output, it will instead try + inserting the clock buffer on the input port (this is used to implement clock + inverter cells that clock buffer insertion will "see through"). + +- The ``clkbuf_inhibit`` is the default attribute to set on a wire to prevent + automatic clock buffer insertion by `clkbufmap`. This behaviour can be + overridden by providing a custom selection to `clkbufmap`. + +- The ``invertible_pin`` attribute can be set on a port to mark it as invertible + via a cell parameter. The name of the inversion parameter is specified as the + value of this attribute. The value of the inversion parameter must be of the + same width as the port, with 1 indicating an inverted bit and 0 indicating a + non-inverted bit. + +- The ``iopad_external_pin`` attribute on a blackbox module's port marks it as + the external-facing pin of an I/O pad, and prevents `iopadmap` from inserting + another pad cell on it. + +- The module attribute ``abc9_lut`` is an integer attribute indicating to `abc9` + that this module describes a LUT with an area cost of this value, and + propagation delays described using ``specify`` statements. + +- The module attribute ``abc9_box`` is a boolean specifying a black/white-box + definition, with propagation delays described using ``specify`` statements, + for use by `abc9`. + +- The port attribute ``abc9_carry`` marks the carry-in (if an input port) and + carry-out (if output port) ports of a box. This information is necessary for + `abc9` to preserve the integrity of carry-chains. Specifying this attribute + onto a bus port will affect only its most significant bit. + +- The module attribute ``abc9_flop`` is a boolean marking the module as a + flip-flop. This allows `abc9` to analyse its contents in order to perform + sequential synthesis. + +- The frontend sets attributes ``always_comb``, ``always_latch`` and + ``always_ff`` on processes derived from SystemVerilog style always blocks + according to the type of the always. These are checked for correctness in + ``proc_dlatch``. + +- The cell attribute ``wildcard_port_conns`` represents wildcard port + connections (SystemVerilog ``.*``). These are resolved to concrete connections + to matching wires in `hierarchy`. + +- In addition to the ``(* ... *)`` attribute syntax, Yosys supports the + non-standard ``{* ... *}`` attribute syntax to set default attributes for + everything that comes after the ``{* ... *}`` statement. (Reset by adding an + empty ``{* *}`` statement.) + +- In module parameter and port declarations, and cell port and parameter lists, + a trailing comma is ignored. This simplifies writing Verilog code generators a + bit in some cases. + +- Modules can be declared with ``module mod_name(...);`` (with three dots + instead of a list of module ports). With this syntax it is sufficient to + simply declare a module port as 'input' or 'output' in the module body. + +- When defining a macro with ``\`define``, all text between triple double quotes + is interpreted as macro body, even if it contains unescaped newlines. The + triple double quotes are removed from the macro body. For example: + +.. code-block:: verilog + + `define MY_MACRO(a, b) """ + assign a = 23; + assign b = 42; + """ + +- The attribute ``via_celltype`` can be used to implement a Verilog task or + function by instantiating the specified cell type. The value is the name of + the cell type to use. For functions the name of the output port can be + specified by appending it to the cell type separated by a whitespace. The body + of the task or function is unused in this case and can be used to specify a + behavioral model of the cell type for simulation. For example: + +.. code-block:: verilog + + module my_add3(A, B, C, Y); + parameter WIDTH = 8; + input [WIDTH-1:0] A, B, C; + output [WIDTH-1:0] Y; + ... + endmodule + + module top; + ... + (* via_celltype = "my_add3 Y" *) + (* via_celltype_defparam_WIDTH = 32 *) + function [31:0] add3; + input [31:0] A, B, C; + begin + add3 = A + B + C; + end + endfunction + ... + endmodule + +- The ``wiretype`` attribute is added by the verilog parser for wires of a + typedef'd type to indicate the type identifier. + +- Various ``enum_value_{value}`` attributes are added to wires of an enumerated + type to give a map of possible enum items to their values. + +- The ``enum_base_type`` attribute is added to enum items to indicate which enum + they belong to (enums -- anonymous and otherwise -- are automatically named + with an auto-incrementing counter). Note that enums are currently not strongly + typed. + +- A limited subset of DPI-C functions is supported. The plugin mechanism (see + ``help plugin``) can be used to load .so files with implementations of DPI-C + routines. As a non-standard extension it is possible to specify a plugin alias + using the ``:`` syntax. For example: + +.. code-block:: verilog + + module dpitest; + import "DPI-C" function foo:round = real my_round (real); + parameter real r = my_round(12.345); + endmodule + +.. code-block:: + + $ yosys -p 'plugin -a foo -i /lib/libm.so; read_verilog dpitest.v' + +- Sized constants (the syntax ``'s?[bodh]``) support constant + expressions as ````. If the expression is not a simple identifier, it + must be put in parentheses. Examples: ``WIDTH'd42``, ``(4+2)'b101010`` + +- The system tasks ``$finish``, ``$stop`` and ``$display`` are supported in + initial blocks in an unconditional context (only if/case statements on + expressions over parameters and constant values are allowed). The intended use + for this is synthesis-time DRC. + +- There is limited support for converting ``specify`` .. ``endspecify`` + statements to special ``$specify2``, ``$specify3``, and ``$specrule`` cells, + for use in blackboxes and whiteboxes. Use ``read_verilog -specify`` to enable + this functionality. (By default these blocks are ignored.) + +- The ``reprocess_after`` internal attribute is used by the Verilog frontend to + mark cells with bindings which might depend on the specified instantiated + module. Modules with such cells will be reprocessed during the `hierarchy` + pass once the referenced module definition(s) become available. + +- The ``smtlib2_module`` attribute can be set on a blackbox module to specify a + formal model directly using SMT-LIB 2. For such a module, the + ``smtlib2_comb_expr`` attribute can be used on output ports to define their + value using an SMT-LIB 2 expression. For example: + +.. code-block:: verilog + + (* blackbox *) + (* smtlib2_module *) + module submod(a, b); + input [7:0] a; + (* smtlib2_comb_expr = "(bvnot a)" *) + output [7:0] b; + endmodule + +Non-standard or SystemVerilog features for formal verification +-------------------------------------------------------------- + +- Support for ``assert``, ``assume``, ``restrict``, and ``cover`` is enabled + when `read_verilog` is called with ``-formal``. + +- The system task ``$initstate`` evaluates to 1 in the initial state and to 0 + otherwise. + +- The system function ``$anyconst`` evaluates to any constant value. This is + equivalent to declaring a reg as ``rand const``, but also works outside of + checkers. (Yosys also supports ``rand const`` outside checkers.) + +- The system function ``$anyseq`` evaluates to any value, possibly a different + value in each cycle. This is equivalent to declaring a reg as ``rand``, but + also works outside of checkers. (Yosys also supports ``rand`` variables + outside checkers.) + +- The system functions ``$allconst`` and ``$allseq`` can be used to construct + formal exist-forall problems. Assumptions only hold if the trace satisfies the + assumption for all ``$allconst/$allseq`` values. For assertions and cover + statements it is sufficient if just one ``$allconst/$allseq`` value triggers + the property (similar to ``$anyconst/$anyseq``). + +- Wires/registers declared using the ``anyconst/anyseq/allconst/allseq`` + attribute (for example ``(* anyconst *) reg [7:0] foobar;``) will behave as if + driven by a ``$anyconst/$anyseq/$allconst/$allseq`` function. + +- The SystemVerilog tasks ``$past``, ``$stable``, ``$rose`` and ``$fell`` are + supported in any clocked block. + +- The syntax ``@($global_clock)`` can be used to create FFs that have no + explicit clock input (``$ff`` cells). The same can be achieved by using + ``@(posedge )`` or ``@(negedge )`` when ```` is + marked with the ``(* gclk *)`` Verilog attribute. + + +Supported features from SystemVerilog +------------------------------------- + +When `read_verilog` is called with ``-sv``, it accepts some language features +from SystemVerilog: + +- The ``assert`` statement from SystemVerilog is supported in its most basic + form. In module context: ``assert property ();`` and within an + always block: ``assert();``. It is transformed to an ``$assert`` + cell. + +- The ``assume``, ``restrict``, and ``cover`` statements from SystemVerilog are + also supported. The same limitations as with the ``assert`` statement apply. + +- The keywords ``always_comb``, ``always_ff`` and ``always_latch``, ``logic`` + and ``bit`` are supported. + +- Declaring free variables with ``rand`` and ``rand const`` is supported. + +- Checkers without a port list that do not need to be instantiated (but instead + behave like a named block) are supported. + +- SystemVerilog packages are supported. Once a SystemVerilog file is read into a + design with `read_verilog`, all its packages are available to SystemVerilog + files being read into the same design afterwards. + +- typedefs are supported (including inside packages) + - type casts are currently not supported + +- enums are supported (including inside packages) + - but are currently not strongly typed + +- packed structs and unions are supported + - arrays of packed structs/unions are currently not supported + - structure literals are currently not supported + +- multidimensional arrays are supported + - array assignment of unpacked arrays is currently not supported + - array literals are currently not supported + +- SystemVerilog interfaces (SVIs) are supported. Modports for specifying whether + ports are inputs or outputs are supported. + +- Assignments within expressions are supported. diff --git a/frontends/aiger2/xaiger.cc b/frontends/aiger2/xaiger.cc index 04beee3965b..616bec9e7f7 100644 --- a/frontends/aiger2/xaiger.cc +++ b/frontends/aiger2/xaiger.cc @@ -198,13 +198,11 @@ struct Xaiger2Frontend : public Frontend { int ci_counter = 0; for (uint32_t i = 0; i < no_boxes; i++) { - uint32_t box_inputs, box_outputs, box_id, box_seq; - box_inputs = read_be32(*f); - box_outputs = read_be32(*f); - box_id = read_be32(*f); - box_seq = read_be32(*f); + /* unused box_inputs = */ read_be32(*f); + YS_MAYBE_UNUSED auto box_outputs = read_be32(*f); + /* unused box_id = */ read_be32(*f); + auto box_seq = read_be32(*f); - log("box_seq=%d boxes.size=%d\n", box_seq, (int) boxes.size()); log_assert(box_seq < boxes.size()); auto [cell, def] = boxes[box_seq]; @@ -337,13 +335,11 @@ struct Xaiger2Frontend : public Frontend { len, ci_num, co_num, pi_num, po_num, no_boxes); for (uint32_t i = 0; i < no_boxes; i++) { - uint32_t box_inputs, box_outputs, box_id, box_seq; - box_inputs = read_be32(*f); - box_outputs = read_be32(*f); - box_id = read_be32(*f); - box_seq = read_be32(*f); + YS_MAYBE_UNUSED auto box_inputs = read_be32(*f); + /* unused box_outputs = */ read_be32(*f); + /* unused box_id = */ read_be32(*f); + auto box_seq = read_be32(*f); - log("box_seq=%d boxes.size=%d\n", box_seq, (int) boxes.size()); log_assert(box_seq < boxes.size()); auto [cell, def] = boxes[box_seq]; diff --git a/frontends/ast/ast.cc b/frontends/ast/ast.cc index c5bf5b4ada8..431f7b4f838 100644 --- a/frontends/ast/ast.cc +++ b/frontends/ast/ast.cc @@ -931,7 +931,7 @@ bool AstNode::bits_only_01() const RTLIL::Const AstNode::bitsAsUnsizedConst(int width) { RTLIL::State extbit = bits.back(); - while (width > int(bits.size())) + while (width > GetSize(bits)) bits.push_back(extbit); return RTLIL::Const(bits); } @@ -939,13 +939,13 @@ RTLIL::Const AstNode::bitsAsUnsizedConst(int width) RTLIL::Const AstNode::bitsAsConst(int width, bool is_signed) { std::vector bits = this->bits; - if (width >= 0 && width < int(bits.size())) + if (width >= 0 && width < GetSize(bits)) bits.resize(width); - if (width >= 0 && width > int(bits.size())) { + if (width >= 0 && width > GetSize(bits)) { RTLIL::State extbit = RTLIL::State::S0; if ((is_signed || is_unsized) && !bits.empty()) extbit = bits.back(); - while (width > int(bits.size())) + while (width > GetSize(bits)) bits.push_back(extbit); } return RTLIL::Const(bits); @@ -1029,7 +1029,7 @@ double AstNode::asReal(bool is_signed) val = const_neg(val, val, false, false, val.size()); double v = 0; - for (size_t i = 0; i < val.size(); i++) + for (auto i = 0; i < val.size(); i++) // IEEE Std 1800-2012 Par 6.12.2: Individual bits that are x or z in // the net or the variable shall be treated as zero upon conversion. if (val.at(i) == RTLIL::State::S1) diff --git a/frontends/ast/genrtlil.cc b/frontends/ast/genrtlil.cc index ea7da521cae..d3982b92bda 100644 --- a/frontends/ast/genrtlil.cc +++ b/frontends/ast/genrtlil.cc @@ -984,7 +984,7 @@ void AstNode::detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *foun // unallocated enum, ignore break; case AST_CONSTANT: - width_hint = max(width_hint, int(bits.size())); + width_hint = max(width_hint, GetSize(bits)); if (!is_signed) sign_hint = false; break; diff --git a/frontends/ast/simplify.cc b/frontends/ast/simplify.cc index 6d78369cc0a..b00cde28e2c 100644 --- a/frontends/ast/simplify.cc +++ b/frontends/ast/simplify.cc @@ -3493,7 +3493,7 @@ skip_dynamic_range_lvalue_expansion:; delete buf; uint32_t result = 0; - for (size_t i = 0; i < arg_value.size(); i++) + for (auto i = 0; i < arg_value.size(); i++) if (arg_value.at(i) == RTLIL::State::S1) result = i + 1; @@ -4339,7 +4339,7 @@ replace_fcall_later:; RTLIL::Const a = children[1]->bitsAsConst(width_hint, sign_hint); RTLIL::Const b = children[2]->bitsAsConst(width_hint, sign_hint); log_assert(a.size() == b.size()); - for (size_t i = 0; i < a.size(); i++) + for (auto i = 0; i < a.size(); i++) if (a[i] != b[i]) a.bits()[i] = RTLIL::State::Sx; newNode = mkconst_bits(a.to_bits(), sign_hint); diff --git a/frontends/liberty/liberty.cc b/frontends/liberty/liberty.cc index e183cbf1007..b29984ecd44 100644 --- a/frontends/liberty/liberty.cc +++ b/frontends/liberty/liberty.cc @@ -462,6 +462,9 @@ struct LibertyFrontend : public Frontend { log(" -ignore_miss_data_latch\n"); log(" ignore latches with missing data and/or enable pins\n"); log("\n"); + log(" -ignore_buses\n"); + log(" ignore cells with bus interfaces (wide ports)\n"); + log("\n"); log(" -setattr \n"); log(" set the specified attribute (to the value 1) on all loaded modules\n"); log("\n"); @@ -478,6 +481,7 @@ struct LibertyFrontend : public Frontend { bool flag_ignore_miss_func = false; bool flag_ignore_miss_dir = false; bool flag_ignore_miss_data_latch = false; + bool flag_ignore_buses = false; bool flag_unit_delay = false; std::vector attributes; @@ -514,6 +518,10 @@ struct LibertyFrontend : public Frontend { flag_ignore_miss_data_latch = true; continue; } + if (arg == "-ignore_buses") { + flag_ignore_buses = true; + continue; + } if (arg == "-setattr" && argidx+1 < args.size()) { attributes.push_back(RTLIL::escape_id(args[++argidx])); continue; @@ -542,27 +550,13 @@ struct LibertyFrontend : public Frontend { if (cell->id != "cell" || cell->args.size() != 1) continue; - std::string cell_name = RTLIL::escape_id(cell->args.at(0)); - - if (design->has(cell_name)) { - Module *existing_mod = design->module(cell_name); - if (!flag_nooverwrite && !flag_overwrite && !existing_mod->get_bool_attribute(ID::blackbox)) { - log_error("Re-definition of cell/module %s!\n", log_id(cell_name)); - } else if (flag_nooverwrite) { - log("Ignoring re-definition of module %s.\n", log_id(cell_name)); - continue; - } else { - log("Replacing existing%s module %s.\n", existing_mod->get_bool_attribute(ID::blackbox) ? " blackbox" : "", log_id(cell_name)); - design->remove(existing_mod); - } - } - // log("Processing cell type %s.\n", RTLIL::unescape_id(cell_name).c_str()); std::map> type_map = global_type_map; parse_type_map(type_map, cell); RTLIL::Module *module = new RTLIL::Module; + std::string cell_name = RTLIL::escape_id(cell->args.at(0)); module->name = cell_name; if (flag_lib) @@ -571,9 +565,15 @@ struct LibertyFrontend : public Frontend { if (flag_wb) module->set_bool_attribute(ID::whitebox); + const LibertyAst *area = cell->find("area"); + if (area) + module->attributes[ID::area] = area->value; + for (auto &attr : attributes) module->attributes[attr] = 1; + bool simple_comb_cell = true, has_outputs = false; + for (auto node : cell->children) { if (node->id == "pin" && node->args.size() == 1) { @@ -595,6 +595,12 @@ struct LibertyFrontend : public Frontend { if (node->id == "bus" && node->args.size() == 1) { + if (flag_ignore_buses) { + log("Ignoring cell %s with a bus interface %s.\n", log_id(module->name), node->args.at(0).c_str()); + delete module; + goto skip_cell; + } + if (!flag_lib) log_error("Error in cell %s: bus interfaces are only supported in -lib mode.\n", log_id(cell_name)); @@ -609,6 +615,8 @@ struct LibertyFrontend : public Frontend { if (!dir || (dir->value != "input" && dir->value != "output" && dir->value != "inout" && dir->value != "internal")) log_error("Missing or invalid direction for bus %s on cell %s.\n", node->args.at(0).c_str(), log_id(module->name)); + simple_comb_cell = false; + if (dir->value == "internal") continue; @@ -656,12 +664,19 @@ struct LibertyFrontend : public Frontend { { const LibertyAst *dir = node->find("direction"); + if (dir->value == "internal" || dir->value == "inout") + simple_comb_cell = false; + if (flag_lib && dir->value == "internal") continue; RTLIL::Wire *wire = module->wires_.at(RTLIL::escape_id(node->args.at(0))); log_assert(wire); + const LibertyAst *capacitance = node->find("capacitance"); + if (capacitance) + wire->attributes[ID::capacitance] = capacitance->value; + if (dir && dir->value == "inout") { wire->port_input = true; wire->port_output = true; @@ -672,8 +687,10 @@ struct LibertyFrontend : public Frontend { continue; } - if (dir && dir->value == "output") + if (dir && dir->value == "output") { + has_outputs = true; wire->port_output = true; + } if (flag_lib) continue; @@ -691,36 +708,35 @@ struct LibertyFrontend : public Frontend { goto skip_cell; } } + simple_comb_cell = false; } else { RTLIL::SigSpec out_sig = parse_func_expr(module, func->value.c_str()); const LibertyAst *three_state = node->find("three_state"); if (three_state) { out_sig = create_tristate(module, out_sig, three_state->value.c_str()); + simple_comb_cell = false; } module->connect(RTLIL::SigSig(wire, out_sig)); } + } - if (flag_unit_delay) { - pool done; - - for (auto timing : node->children) - if (timing->id == "timing" && timing->args.empty()) { - auto type = timing->find("timing_type"); - auto related_pin = timing->find("related_pin"); - if (!type || type->value != "combinational" || !related_pin) - continue; - - Wire *related = module->wire(RTLIL::escape_id(related_pin->value)); - if (!related) - log_error("Failed to find related pin %s for timing of pin %s on %s\n", - related_pin->value.c_str(), log_id(wire), log_id(module)); + if (node->id == "ff" || node->id == "ff_bank" || + node->id == "latch" || node->id == "latch_bank" || + node->id == "statetable") + simple_comb_cell = false; + } - if (done.count(related)) - continue; + if (simple_comb_cell && has_outputs) { + module->set_bool_attribute(ID::abc9_box); + if (flag_unit_delay) { + for (auto wi : module->wires()) + if (wi->port_input) { + for (auto wo : module->wires()) + if (wo->port_output) { RTLIL::Cell *spec = module->addCell(NEW_ID, ID($specify2)); - spec->setParam(ID::SRC_WIDTH, 1); - spec->setParam(ID::DST_WIDTH, 1); + spec->setParam(ID::SRC_WIDTH, wi->width); + spec->setParam(ID::DST_WIDTH, wo->width); spec->setParam(ID::T_FALL_MAX, 1000); spec->setParam(ID::T_FALL_TYP, 1000); spec->setParam(ID::T_FALL_MIN, 1000); @@ -729,16 +745,29 @@ struct LibertyFrontend : public Frontend { spec->setParam(ID::T_RISE_MIN, 1000); spec->setParam(ID::SRC_DST_POL, false); spec->setParam(ID::SRC_DST_PEN, false); - spec->setParam(ID::FULL, false); + spec->setParam(ID::FULL, true); spec->setPort(ID::EN, Const(1, 1)); - spec->setPort(ID::SRC, related); - spec->setPort(ID::DST, wire); - done.insert(related); + spec->setPort(ID::SRC, wi); + spec->setPort(ID::DST, wo); } } } } + if (design->has(cell_name)) { + Module *existing_mod = design->module(cell_name); + if (!flag_nooverwrite && !flag_overwrite && !existing_mod->get_bool_attribute(ID::blackbox)) { + log_error("Re-definition of cell/module %s!\n", log_id(cell_name)); + } else if (flag_nooverwrite) { + log("Ignoring re-definition of module %s.\n", log_id(cell_name)); + delete module; + goto skip_cell; + } else { + log("Replacing existing%s module %s.\n", existing_mod->get_bool_attribute(ID::blackbox) ? " blackbox" : "", log_id(cell_name)); + design->remove(existing_mod); + } + } + module->fixup_ports(); design->add(module); cell_count++; diff --git a/frontends/rtlil/rtlil_frontend.cc b/frontends/rtlil/rtlil_frontend.cc index 5f85ca2b88b..2c1910d1341 100644 --- a/frontends/rtlil/rtlil_frontend.cc +++ b/frontends/rtlil/rtlil_frontend.cc @@ -31,6 +31,11 @@ void rtlil_frontend_yyerror(char const *s) YOSYS_NAMESPACE_PREFIX log_error("Parser error in line %d: %s\n", rtlil_frontend_yyget_lineno(), s); } +void rtlil_frontend_yywarning(char const *s) +{ + YOSYS_NAMESPACE_PREFIX log_warning("In line %d: %s\n", rtlil_frontend_yyget_lineno(), s); +} + YOSYS_NAMESPACE_BEGIN struct RTLILFrontend : public Frontend { @@ -96,20 +101,5 @@ struct RTLILFrontend : public Frontend { } } RTLILFrontend; -struct IlangFrontend : public Frontend { - IlangFrontend() : Frontend("ilang", "(deprecated) alias of read_rtlil") { } - void help() override - { - // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| - log("\n"); - log("See `help read_rtlil`.\n"); - log("\n"); - } - void execute(std::istream *&f, std::string filename, std::vector args, RTLIL::Design *design) override - { - RTLILFrontend.execute(f, filename, args, design); - } -} IlangFrontend; - YOSYS_NAMESPACE_END diff --git a/frontends/rtlil/rtlil_frontend.h b/frontends/rtlil/rtlil_frontend.h index 18926060545..31cfb80b406 100644 --- a/frontends/rtlil/rtlil_frontend.h +++ b/frontends/rtlil/rtlil_frontend.h @@ -42,6 +42,7 @@ YOSYS_NAMESPACE_END extern int rtlil_frontend_yydebug; int rtlil_frontend_yylex(void); void rtlil_frontend_yyerror(char const *s); +void rtlil_frontend_yywarning(char const *s); void rtlil_frontend_yyrestart(FILE *f); int rtlil_frontend_yyparse(void); int rtlil_frontend_yylex_destroy(void); diff --git a/frontends/rtlil/rtlil_parser.y b/frontends/rtlil/rtlil_parser.y index deb37d9a663..fc76153648f 100644 --- a/frontends/rtlil/rtlil_parser.y +++ b/frontends/rtlil/rtlil_parser.y @@ -344,6 +344,16 @@ assign_stmt: TOK_ASSIGN sigspec sigspec EOL { if (attrbuf.size() != 0) rtlil_frontend_yyerror("dangling attribute"); + + // See https://github.com/YosysHQ/yosys/pull/4765 for discussion on this + // warning + if (!switch_stack.back()->empty()) { + rtlil_frontend_yywarning( + "case rule assign statements after switch statements may cause unexpected behaviour. " + "The assign statement is reordered to come before all switch statements." + ); + } + case_stack.back()->actions.push_back(RTLIL::SigSig(*$2, *$3)); delete $2; delete $3; diff --git a/frontends/verific/verific.cc b/frontends/verific/verific.cc index 0cdf772a8f9..08b8c505dce 100644 --- a/frontends/verific/verific.cc +++ b/frontends/verific/verific.cc @@ -1471,7 +1471,8 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma FOREACH_PARAMETER_OF_NETLIST(nl, mi, param_name, param_value) { module->avail_parameters(RTLIL::escape_id(param_name)); const TypeRange *tr = nl->GetTypeRange(param_name) ; - module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(tr->GetTypeName(), param_value, nl); + const char* type_name = (tr) ? tr->GetTypeName() : nullptr; + module->parameter_default_values[RTLIL::escape_id(param_name)] = verific_const(type_name, param_value, nl); } SetIter si; @@ -2126,12 +2127,6 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma log(" assert condition %s.\n", log_signal(cond)); Cell *cell = module->addAssert(new_verific_id(inst), cond, State::S1); - // Initialize FF feeding condition to 1, in case it is not - // used by rest of design logic, to prevent failing on - // initial uninitialized state - if (cond.is_wire() && !cond.wire->name.isPublic()) - cond.wire->attributes[ID::init] = Const(1,1); - import_attributes(cell->attributes, inst); continue; } @@ -2177,7 +2172,8 @@ void VerificImporter::import_netlist(RTLIL::Design *design, Netlist *nl, std::ma if (is_blackbox(inst->View())) { FOREACH_PARAMETER_OF_INST(inst, mi2, param_name, param_value) { const TypeRange *tr = inst->View()->GetTypeRange(param_name) ; - cell->setParam(RTLIL::escape_id(param_name), verific_const(tr->GetTypeName(), param_value, inst->View())); + const char* type_name = (tr) ? tr->GetTypeName() : nullptr; + cell->setParam(RTLIL::escape_id(param_name), verific_const(type_name, param_value, inst->View())); } } @@ -2692,11 +2688,51 @@ struct VerificExtNets } }; +#ifdef VERIFIC_SYSTEMVERILOG_SUPPORT +static msg_type_t prev_1063; +#endif +#ifdef VERIFIC_VHDL_SUPPORT +static msg_type_t prev_1240 ; +static msg_type_t prev_1241 ; +#endif +void save_blackbox_msg_state() +{ +#ifdef VERIFIC_SYSTEMVERILOG_SUPPORT + prev_1063 = Message::GetMessageType("VERI-1063") ; + Message::SetMessageType("VERI-1063", VERIFIC_INFO); +#endif +#ifdef VERIFIC_VHDL_SUPPORT + prev_1240 = Message::GetMessageType("VHDL-1240") ; + prev_1241 = Message::GetMessageType("VHDL-1241") ; + Message::SetMessageType("VHDL-1240", VERIFIC_INFO); + Message::SetMessageType("VHDL-1241", VERIFIC_INFO); +#endif +} + +void restore_blackbox_msg_state() +{ +#ifdef VERIFIC_SYSTEMVERILOG_SUPPORT + Message::ClearMessageType("VERI-1063") ; + if (Message::GetMessageType("VERI-1063")!=prev_1063) + Message::SetMessageType("VERI-1063", prev_1063); +#endif +#ifdef VERIFIC_VHDL_SUPPORT + Message::ClearMessageType("VHDL-1240") ; + Message::ClearMessageType("VHDL-1241") ; + if (Message::GetMessageType("VHDL-1240")!=prev_1240) + Message::SetMessageType("VHDL-1240", prev_1240); + if (Message::GetMessageType("VHDL-1241")!=prev_1241) + Message::SetMessageType("VHDL-1241", prev_1241); +#endif +} + void import_all(const char* work, std::map *nl_todo, Map *parameters, bool show_message, std::string ppfile YS_MAYBE_UNUSED) { #ifdef YOSYSHQ_VERIFIC_EXTENSIONS + save_blackbox_msg_state(); VerificExtensions::ElaborateAndRewrite(work, parameters); verific_error_msg.clear(); + restore_blackbox_msg_state(); #endif #ifdef VERIFIC_SYSTEMVERILOG_SUPPORT if (!ppfile.empty()) @@ -2827,8 +2863,10 @@ std::set import_tops(const char* work, std::mapSetCompileAsBlackbox(); } } - Message::ClearMessageType("VHDL-1240") ; - Message::ClearMessageType("VHDL-1241") ; - if (Message::GetMessageType("VHDL-1240")!=prev_1240) - Message::SetMessageType("VHDL-1240", prev_1240); - if (Message::GetMessageType("VHDL-1241")!=prev_1241) - Message::SetMessageType("VHDL-1241", prev_1241); - + restore_blackbox_msg_state(); } #endif - msg_type_t prev_1063; #ifdef VERIFIC_SYSTEMVERILOG_SUPPORT void add_modules_to_map(Map &map, std::string work, bool flag_lib) { @@ -3353,9 +3378,7 @@ struct VerificPass : public Pass { map.Insert(veri_module,veri_module); } } - - prev_1063 = Message::GetMessageType("VERI-1063") ; - Message::SetMessageType("VERI-1063", VERIFIC_INFO); + save_blackbox_msg_state(); } void set_modules_to_blackbox(Map &map, std::string work, bool flag_lib) @@ -3370,9 +3393,6 @@ struct VerificPass : public Pass { veri_module->SetCompileAsBlackbox(); } } - Message::ClearMessageType("VERI-1063") ; - if (Message::GetMessageType("VERI-1063")!=prev_1063) - Message::SetMessageType("VERI-1063", prev_1063); } #endif @@ -4184,7 +4204,7 @@ struct VerificPass : public Pass { } if (v[0] == '"') { std::string s = v.substr(1, GetSize(v)-2); - RuntimeFlags::SetStringVar(k.c_str(), v.c_str()); + RuntimeFlags::SetStringVar(k.c_str(), s.c_str()); goto check_error; } char *endptr; diff --git a/guidelines/Checklists b/guidelines/Checklists deleted file mode 100644 index 75af12fa9e4..00000000000 --- a/guidelines/Checklists +++ /dev/null @@ -1,116 +0,0 @@ -Checklist for adding internal cell types -======================================== - -Things to do right away: - - - Add to kernel/celltypes.h (incl. eval() handling for non-mem cells) - - Add to InternalCellChecker::check() in kernel/rtlil.cc - - Add to techlibs/common/simlib.v - - Add to techlibs/common/techmap.v - -Things to do after finalizing the cell interface: - - - Add support to kernel/satgen.h for the new cell type - - Add to docs/source/CHAPTER_CellLib.rst (or just add a fixme to the bottom) - - Maybe add support to the Verilog backend for dumping such cells as expression - - - -Checklist for creating Yosys releases -===================================== - -Update the CHANGELOG file: - - cd ~yosys - gitk & - vi CHANGELOG - - -Update and check documentation: - - cd ~yosys - make docs - - sanity check the figures in docs/images - - if there are any odd things -> investigate - - cd ~yosys - vi README guidelines/* - - is the information provided in those file still up to date - - -Then with default config setting: - - cd ~yosys - make vgtest - - cd ~yosys - ./yosys -p 'proc; show' tests/simple/fiedler-cooley.v - ./yosys -p 'proc; opt; show' tests/simple/fiedler-cooley.v - ./yosys -p 'synth; show' tests/simple/fiedler-cooley.v - ./yosys -p 'synth_xilinx -top up3down5; show' tests/simple/fiedler-cooley.v - - cd ~yosys/examples/cmos - bash testbench.sh - - cd ~yosys/examples/basys3 - bash run.sh - - -Test building plugins with various of the standard passes: - - yosys-config --build test.so equiv_simple.cc - - also check the code examples in guidelines/GettingStarted - - -And if a version of the verific library is currently available: - - cd ~yosys - cat frontends/verific/build_amd64.txt - - follow instructions - - cd frontends/verific - ../../yosys test_navre.ys - - -Finally run all tests with "make config-{clang,gcc}": - - cd ~yosys - make clean - make test - make ystests - make vloghtb - make install - - cd ~yosys-bigsim - make clean - make full - - cd ~vloghammer - make purge gen_issues gen_samples - make SYN_LIST="yosys" SIM_LIST="icarus yosim verilator" REPORT_FULL=1 world - chromium-browser report.html - - -Release: - - - set YOSYS_VER to x.y.z in Makefile - - remove "bumpversion" target from Makefile - - update version string in CHANGELOG - git commit -am "Yosys x.y.z" - - - push tag to github - - post changelog on github - - post short release note on reddit - - -Updating the website: - - cd ~yosys - make install - - cd ~yosys-web - make update_show - git commit -am update - make push - - - Read the Docs updates handled by Jenkins on source change diff --git a/guidelines/CodeOfConduct b/guidelines/CodeOfConduct deleted file mode 100644 index 92decd3b6a5..00000000000 --- a/guidelines/CodeOfConduct +++ /dev/null @@ -1,72 +0,0 @@ -Contributor Covenant Code of Conduct - -Our Pledge - -In the interest of fostering an open and welcoming environment, we as -contributors and maintainers pledge to making participation in our project and -our community a harassment-free experience for everyone, regardless of age, body -size, disability, ethnicity, gender identity and expression, level of experience, -nationality, personal appearance, race, religion, or sexual identity and -orientation. - -Our Standards - -Examples of behavior that contributes to creating a positive environment -include: - -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members - -Examples of unacceptable behavior by participants include: - -* The use of sexualized language or imagery and unwelcome sexual attention or - advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic - address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a - professional setting - -Our Responsibilities - -Project maintainers are responsible for clarifying the standards of acceptable -behavior and are expected to take appropriate and fair corrective action in -response to any instances of unacceptable behavior. - -Project maintainers have the right and responsibility to remove, edit, or -reject comments, commits, code, wiki edits, issues, and other contributions -that are not aligned to this Code of Conduct, or to ban temporarily or -permanently any contributor for other behaviors that they deem inappropriate, -threatening, offensive, or harmful. - -Scope - -This Code of Conduct applies both within project spaces and in public spaces -when an individual is representing the project or its community. Examples of -representing a project or community include using an official project e-mail -address, posting via an official social media account, or acting as an appointed -representative at an online or offline event. Representation of a project may be -further defined and clarified by project maintainers. - -Enforcement - -Instances of abusive, harassing, or otherwise unacceptable behavior may be -reported by contacting the project team at contact@yosyshq.com and/or -claire@clairexen.net. All complaints will be reviewed and investigated and -will result in a response that is deemed necessary and appropriate to the -circumstances. The project team is obligated to maintain confidentiality with -regard to the reporter of an incident. Further details of specific enforcement -policies may be posted separately. - -Project maintainers who do not follow or enforce the Code of Conduct in good -faith may face temporary or permanent repercussions as determined by other -members of the project's leadership. - -Attribution - -This Code of Conduct is adapted from the Contributor Covenant, version 1.4, -available at http://contributor-covenant.org/version/1/4/ diff --git a/guidelines/CodingStyle b/guidelines/CodingStyle deleted file mode 100644 index 491507cdd48..00000000000 --- a/guidelines/CodingStyle +++ /dev/null @@ -1,34 +0,0 @@ -Coding Style -============ - - -Formatting of code ------------------- - -- Yosys code is using tabs for indentation. Tabs are 8 characters. - -- A continuation of a statement in the following line is indented by - two additional tabs. - -- Lines are as long as you want them to be. A good rule of thumb is - to break lines at about column 150. - -- Opening braces can be put on the same or next line as the statement - opening the block (if, switch, for, while, do). Put the opening brace - on its own line for larger blocks, especially blocks that contains - blank lines. - -- Otherwise stick to the Linux Kernel Coding Style: - https://www.kernel.org/doc/Documentation/process/coding-style.rst - - -C++ Language -------------- - -Yosys is written in C++17. - -In general Yosys uses "int" instead of "size_t". To avoid compiler -warnings for implicit type casts, always use "GetSize(foobar)" instead -of "foobar.size()". (GetSize() is defined in kernel/yosys.h) - -Use range-based for loops whenever applicable. \ No newline at end of file diff --git a/guidelines/UnitTests b/guidelines/UnitTests deleted file mode 100644 index d42a63ce587..00000000000 --- a/guidelines/UnitTests +++ /dev/null @@ -1,69 +0,0 @@ -How to add unit test -==================== - -Unit test brings some advantages, briefly, we can list some of them (reference -[1](https://en.wikipedia.org/wiki/Unit_testing)): - -* Tests reduce bugs in new features; -* Tests reduce bugs in existing features; -* Tests are good documentation; -* Tests reduce the cost of change; -* Tests allow refactoring; - -With those advantages in mind, it was required to choose a framework which fits -well with C/C++ code. Hence, it was chosen (google test) -[https://github.com/google/googletest], because it is largely used and it is -relatively easy learn. - -Install and configure google test (manually) --------------------------------------------- - -In this section, you will see a brief description of how to install google -test. However, it is strongly recommended that you take a look to the official -repository (https://github.com/google/googletest) and refers to that if you -have any problem to install it. Follow the steps below: - -* Install: cmake and pthread -* Clone google test project from: https://github.com/google/googletest and - enter in the project directory -* Inside project directory, type: - -``` -cmake -DBUILD_SHARED_LIBS=ON . -make -``` - -* After compilation, copy all "*.so" inside directory "googlemock" and - "googlemock/gtest" to "/usr/lib/" -* Done! Now you can compile your tests. - -If you have any problem, go to the official repository to find help. - -Ps.: Some distros already have googletest packed. If your distro supports it, -you can use it instead of compile. - -Create new unit test --------------------- - -If you want to add new unit tests for Yosys, just follow the steps below: - -* Go to directory "yosys/test/unit/" -* In this directory you can find something similar Yosys's directory structure. - To create your unit test file you have to follow this pattern: - fileNameToImplementUnitTest + Test.cc. E.g.: if you want to implement the - unit test for kernel/celledges.cc, you will need to create a file like this: - tests/unit/kernel/celledgesTest.cc; -* Implement your unit test - -Run unit test -------------- - -To compile and run all unit tests, just go to yosys root directory and type: -``` -make unit-test -``` - -If you want to remove all unit test files, type: -``` -make clean-unit-test -``` diff --git a/guidelines/Windows b/guidelines/Windows deleted file mode 100644 index c4548c37cc7..00000000000 --- a/guidelines/Windows +++ /dev/null @@ -1,83 +0,0 @@ -Creating the Visual Studio Template Project -=========================================== - -1. Create an empty Visual C++ Win32 Console App project - - Microsoft Visual Studio Express 2013 for Windows Desktop - Open New Project Wizard (File -> New Project..) - - Project Name: YosysVS - Solution Name: YosysVS - [X] Create directory for solution - [ ] Add to source control - - [X] Console applications - [X] Empty Project - [ ] SDL checks - -2. Open YosysVS Project Properties - - Select Configuration: All Configurations - - C/C++ -> General -> Additional Include Directories - Add: ..\yosys - - C/C++ -> Preprocessor -> Preprocessor Definitions - Add: _YOSYS_;_CRT_SECURE_NO_WARNINGS - -3. Resulting file system tree: - - YosysVS/ - YosysVS/YosysVS - YosysVS/YosysVS/YosysVS.vcxproj - YosysVS/YosysVS/YosysVS.vcxproj.filters - YosysVS/YosysVS.sdf - YosysVS/YosysVS.sln - YosysVS/YosysVS.v12.suo - -4. Zip YosysVS as YosysVS-Tpl-v1.zip - -Compiling with Visual Studio -============================ - -Visual Studio builds are not directly supported by build scripts, but they are still possible. - -1. Easy way - - - Go to https://github.com/YosysHQ/yosys/actions/workflows/vs.yml?query=branch%3Amain - - Click on the most recent completed run - - In Artifacts region find vcxsrc and click on it to download - - Unpack downloaded ZIP file - - Open YosysVS.sln with Visual Studio - -2. Using WSL or MSYS2 - - - Make sure to have make, python3 and git available - - Git clone yosys repository - - Execute ```make vcxsrc YOSYS_VER=latest``` - - File yosys-win32-vcxsrc-latest.zip will be created - - Transfer that file to location visible by Windows application - - Unpack ZIP - - Open YosysVS.sln with Visual Studio - -Cross-Building for Windows with MXE -=================================== - -Check http://mxe.cc/#requirements and install all missing requirements. - -As root (or other user with write access to /usr/local/src): - - cd /usr/local/src - git clone https://github.com/mxe/mxe.git - cd mxe - - make -j$(nproc) MXE_PLUGIN_DIRS="plugins/tcl.tk" \ - MXE_TARGETS="i686-w64-mingw32.static" \ - gcc tcl readline - -Then as regular user in some directory where you build stuff: - - git clone https://github.com/YosysHQ/yosys.git yosys-win32 - cd yosys-win32 - make config-mxe - make -j$(nproc) mxebin diff --git a/kernel/calc.cc b/kernel/calc.cc index 172b6a905bf..f08c9739657 100644 --- a/kernel/calc.cc +++ b/kernel/calc.cc @@ -33,7 +33,7 @@ static void extend_u0(RTLIL::Const &arg, int width, bool is_signed) if (arg.size() > 0 && is_signed) padding = arg.back(); - while (int(arg.size()) < width) + while (GetSize(arg) < width) arg.bits().push_back(padding); arg.bits().resize(width); @@ -45,7 +45,7 @@ static BigInteger const2big(const RTLIL::Const &val, bool as_signed, int &undef_ BigInteger::Sign sign = BigInteger::positive; State inv_sign_bit = RTLIL::State::S1; - size_t num_bits = val.size(); + auto num_bits = val.size(); if (as_signed && num_bits && val[num_bits-1] == RTLIL::State::S1) { inv_sign_bit = RTLIL::State::S0; @@ -53,7 +53,7 @@ static BigInteger const2big(const RTLIL::Const &val, bool as_signed, int &undef_ num_bits--; } - for (size_t i = 0; i < num_bits; i++) + for (auto i = 0; i < num_bits; i++) if (val[i] == RTLIL::State::S0 || val[i] == RTLIL::State::S1) mag.setBit(i, val[i] == inv_sign_bit); else if (undef_bit_pos < 0) @@ -78,12 +78,12 @@ static RTLIL::Const big2const(const BigInteger &val, int result_len, int undef_b if (val.getSign() < 0) { mag--; - for (int i = 0; i < result_len; i++) + for (auto i = 0; i < result_len; i++) result.bits()[i] = mag.getBit(i) ? RTLIL::State::S0 : RTLIL::State::S1; } else { - for (int i = 0; i < result_len; i++) + for (auto i = 0; i < result_len; i++) result.bits()[i] = mag.getBit(i) ? RTLIL::State::S1 : RTLIL::State::S0; } } @@ -132,14 +132,14 @@ static RTLIL::State logic_xnor(RTLIL::State a, RTLIL::State b) RTLIL::Const RTLIL::const_not(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len) { if (result_len < 0) - result_len = arg1.size(); + result_len = GetSize(arg1); RTLIL::Const arg1_ext = arg1; extend_u0(arg1_ext, result_len, signed1); RTLIL::Const result(RTLIL::State::Sx, result_len); - for (size_t i = 0; i < size_t(result_len); i++) { - if (i >= arg1_ext.size()) + for (auto i = 0; i < result_len; i++) { + if (i >= GetSize(arg1_ext)) result.bits()[i] = RTLIL::State::S0; else if (arg1_ext.bits()[i] == RTLIL::State::S0) result.bits()[i] = RTLIL::State::S1; @@ -154,15 +154,15 @@ static RTLIL::Const logic_wrapper(RTLIL::State(*logic_func)(RTLIL::State, RTLIL: RTLIL::Const arg1, RTLIL::Const arg2, bool signed1, bool signed2, int result_len = -1) { if (result_len < 0) - result_len = max(arg1.size(), arg2.size()); + result_len = max(GetSize(arg1), GetSize(arg2)); extend_u0(arg1, result_len, signed1); extend_u0(arg2, result_len, signed2); RTLIL::Const result(RTLIL::State::Sx, result_len); - for (size_t i = 0; i < size_t(result_len); i++) { - RTLIL::State a = i < arg1.size() ? arg1.bits()[i] : RTLIL::State::S0; - RTLIL::State b = i < arg2.size() ? arg2.bits()[i] : RTLIL::State::S0; + for (auto i = 0; i < result_len; i++) { + RTLIL::State a = i < GetSize(arg1) ? arg1.bits()[i] : RTLIL::State::S0; + RTLIL::State b = i < GetSize(arg2) ? arg2.bits()[i] : RTLIL::State::S0; result.bits()[i] = logic_func(a, b); } @@ -193,11 +193,11 @@ static RTLIL::Const logic_reduce_wrapper(RTLIL::State initial, RTLIL::State(*log { RTLIL::State temp = initial; - for (size_t i = 0; i < arg1.size(); i++) + for (auto i = 0; i < arg1.size(); i++) temp = logic_func(temp, arg1[i]); RTLIL::Const result(temp); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -240,7 +240,7 @@ RTLIL::Const RTLIL::const_logic_not(const RTLIL::Const &arg1, const RTLIL::Const BigInteger a = const2big(arg1, signed1, undef_bit_pos_a); RTLIL::Const result(a.isZero() ? undef_bit_pos_a >= 0 ? RTLIL::State::Sx : RTLIL::State::S1 : RTLIL::State::S0); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -255,7 +255,7 @@ RTLIL::Const RTLIL::const_logic_and(const RTLIL::Const &arg1, const RTLIL::Const RTLIL::State bit_b = b.isZero() ? undef_bit_pos_b >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1; RTLIL::Const result(logic_and(bit_a, bit_b)); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -270,7 +270,7 @@ RTLIL::Const RTLIL::const_logic_or(const RTLIL::Const &arg1, const RTLIL::Const RTLIL::State bit_b = b.isZero() ? undef_bit_pos_b >= 0 ? RTLIL::State::Sx : RTLIL::State::S0 : RTLIL::State::S1; RTLIL::Const result(logic_or(bit_a, bit_b)); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -286,7 +286,7 @@ static RTLIL::Const const_shift_worker(const RTLIL::Const &arg1, const RTLIL::Co BigInteger offset = const2big(arg2, signed2, undef_bit_pos) * direction; if (result_len < 0) - result_len = arg1.size(); + result_len = GetSize(arg1); RTLIL::Const result(RTLIL::State::Sx, result_len); if (undef_bit_pos >= 0) @@ -296,7 +296,7 @@ static RTLIL::Const const_shift_worker(const RTLIL::Const &arg1, const RTLIL::Co BigInteger pos = BigInteger(i) + offset; if (pos < 0) result.bits()[i] = vacant_bits; - else if (pos >= BigInteger(int(arg1.size()))) + else if (pos >= BigInteger(GetSize(arg1))) result.bits()[i] = sign_ext ? arg1.back() : vacant_bits; else result.bits()[i] = arg1[pos.toInt()]; @@ -347,7 +347,7 @@ RTLIL::Const RTLIL::const_lt(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool y = const2big(arg1, signed1, undef_bit_pos) < const2big(arg2, signed2, undef_bit_pos); RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -358,7 +358,7 @@ RTLIL::Const RTLIL::const_le(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool y = const2big(arg1, signed1, undef_bit_pos) <= const2big(arg2, signed2, undef_bit_pos); RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -369,12 +369,12 @@ RTLIL::Const RTLIL::const_eq(const RTLIL::Const &arg1, const RTLIL::Const &arg2, RTLIL::Const arg2_ext = arg2; RTLIL::Const result(RTLIL::State::S0, result_len); - int width = max(arg1_ext.size(), arg2_ext.size()); + int width = max(GetSize(arg1_ext), GetSize(arg2_ext)); extend_u0(arg1_ext, width, signed1 && signed2); extend_u0(arg2_ext, width, signed1 && signed2); RTLIL::State matched_status = RTLIL::State::S1; - for (size_t i = 0; i < arg1_ext.size(); i++) { + for (auto i = 0; i < arg1_ext.size(); i++) { if (arg1_ext.at(i) == RTLIL::State::S0 && arg2_ext.at(i) == RTLIL::State::S1) return result; if (arg1_ext.at(i) == RTLIL::State::S1 && arg2_ext.at(i) == RTLIL::State::S0) @@ -403,11 +403,11 @@ RTLIL::Const RTLIL::const_eqx(const RTLIL::Const &arg1, const RTLIL::Const &arg2 RTLIL::Const arg2_ext = arg2; RTLIL::Const result(RTLIL::State::S0, result_len); - int width = max(arg1_ext.size(), arg2_ext.size()); + int width = max(GetSize(arg1_ext), GetSize(arg2_ext)); extend_u0(arg1_ext, width, signed1 && signed2); extend_u0(arg2_ext, width, signed1 && signed2); - for (size_t i = 0; i < arg1_ext.size(); i++) { + for (auto i = 0; i < arg1_ext.size(); i++) { if (arg1_ext.at(i) != arg2_ext.at(i)) return result; } @@ -432,7 +432,7 @@ RTLIL::Const RTLIL::const_ge(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool y = const2big(arg1, signed1, undef_bit_pos) >= const2big(arg2, signed2, undef_bit_pos); RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -443,7 +443,7 @@ RTLIL::Const RTLIL::const_gt(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool y = const2big(arg1, signed1, undef_bit_pos) > const2big(arg2, signed2, undef_bit_pos); RTLIL::Const result(undef_bit_pos >= 0 ? RTLIL::State::Sx : y ? RTLIL::State::S1 : RTLIL::State::S0); - while (int(result.size()) < result_len) + while (GetSize(result) < result_len) result.bits().push_back(RTLIL::State::S0); return result; } @@ -452,21 +452,21 @@ RTLIL::Const RTLIL::const_add(const RTLIL::Const &arg1, const RTLIL::Const &arg2 { int undef_bit_pos = -1; BigInteger y = const2big(arg1, signed1, undef_bit_pos) + const2big(arg2, signed2, undef_bit_pos); - return big2const(y, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), undef_bit_pos); + return big2const(y, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), undef_bit_pos); } RTLIL::Const RTLIL::const_sub(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len) { int undef_bit_pos = -1; BigInteger y = const2big(arg1, signed1, undef_bit_pos) - const2big(arg2, signed2, undef_bit_pos); - return big2const(y, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), undef_bit_pos); + return big2const(y, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), undef_bit_pos); } RTLIL::Const RTLIL::const_mul(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len) { int undef_bit_pos = -1; BigInteger y = const2big(arg1, signed1, undef_bit_pos) * const2big(arg2, signed2, undef_bit_pos); - return big2const(y, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(y, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } // truncating division @@ -480,7 +480,7 @@ RTLIL::Const RTLIL::const_div(const RTLIL::Const &arg1, const RTLIL::Const &arg2 bool result_neg = (a.getSign() == BigInteger::negative) != (b.getSign() == BigInteger::negative); a = a.getSign() == BigInteger::negative ? -a : a; b = b.getSign() == BigInteger::negative ? -b : b; - return big2const(result_neg ? -(a / b) : (a / b), result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(result_neg ? -(a / b) : (a / b), result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } // truncating modulo @@ -494,7 +494,7 @@ RTLIL::Const RTLIL::const_mod(const RTLIL::Const &arg1, const RTLIL::Const &arg2 bool result_neg = a.getSign() == BigInteger::negative; a = a.getSign() == BigInteger::negative ? -a : a; b = b.getSign() == BigInteger::negative ? -b : b; - return big2const(result_neg ? -(a % b) : (a % b), result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(result_neg ? -(a % b) : (a % b), result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } RTLIL::Const RTLIL::const_divfloor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len) @@ -516,7 +516,7 @@ RTLIL::Const RTLIL::const_divfloor(const RTLIL::Const &arg1, const RTLIL::Const // bigint division with negative numbers is wonky, make sure we only negate at the very end result = -((a + b - 1) / b); } - return big2const(result, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(result, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } RTLIL::Const RTLIL::const_modfloor(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len) @@ -539,7 +539,7 @@ RTLIL::Const RTLIL::const_modfloor(const RTLIL::Const &arg1, const RTLIL::Const } else { modulo = b_sign == BigInteger::negative ? truncated - b : truncated + b; } - return big2const(modulo, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(modulo, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } RTLIL::Const RTLIL::const_pow(const RTLIL::Const &arg1, const RTLIL::Const &arg2, bool signed1, bool signed2, int result_len) @@ -590,7 +590,7 @@ RTLIL::Const RTLIL::const_pow(const RTLIL::Const &arg1, const RTLIL::Const &arg2 y *= -1; } - return big2const(y, result_len >= 0 ? result_len : max(arg1.size(), arg2.size()), min(undef_bit_pos, 0)); + return big2const(y, result_len >= 0 ? result_len : max(GetSize(arg1), GetSize(arg2)), min(undef_bit_pos, 0)); } RTLIL::Const RTLIL::const_pos(const RTLIL::Const &arg1, const RTLIL::Const&, bool signed1, bool, int result_len) @@ -626,7 +626,7 @@ RTLIL::Const RTLIL::const_mux(const RTLIL::Const &arg1, const RTLIL::Const &arg2 return arg2; RTLIL::Const ret = arg1; - for (int i = 0; i < ret.size(); i++) + for (auto i = 0; i < ret.size(); i++) if (ret[i] != arg2[i]) ret.bits()[i] = State::Sx; return ret; @@ -640,7 +640,7 @@ RTLIL::Const RTLIL::const_pmux(const RTLIL::Const &arg1, const RTLIL::Const &arg if (!arg3.is_onehot()) return RTLIL::Const(State::Sx, arg1.size()); - for (int i = 0; i < arg3.size(); i++) + for (auto i = 0; i < arg3.size(); i++) if (arg3[i] == State::S1) return RTLIL::Const(std::vector(arg2.begin() + i*arg1.size(), arg2.begin() + (i+1)*arg1.size())); @@ -702,7 +702,7 @@ RTLIL::Const RTLIL::const_bweqx(const RTLIL::Const &arg1, const RTLIL::Const &ar { log_assert(arg2.size() == arg1.size()); RTLIL::Const result(RTLIL::State::S0, arg1.size()); - for (int i = 0; i < arg1.size(); i++) + for (auto i = 0; i < arg1.size(); i++) result.bits()[i] = arg1[i] == arg2[i] ? State::S1 : State::S0; return result; @@ -713,7 +713,7 @@ RTLIL::Const RTLIL::const_bwmux(const RTLIL::Const &arg1, const RTLIL::Const &ar log_assert(arg2.size() == arg1.size()); log_assert(arg3.size() == arg1.size()); RTLIL::Const result(RTLIL::State::Sx, arg1.size()); - for (int i = 0; i < arg1.size(); i++) { + for (auto i = 0; i < arg1.size(); i++) { if (arg3[i] != State::Sx || arg1[i] == arg2[i]) result.bits()[i] = arg3[i] == State::S1 ? arg2[i] : arg1[i]; } diff --git a/kernel/constids.inc b/kernel/constids.inc index d1bbb8edaf6..d68e2dfe646 100644 --- a/kernel/constids.inc +++ b/kernel/constids.inc @@ -274,3 +274,5 @@ X(X) X(xprop_decoder) X(Y) X(Y_WIDTH) +X(area) +X(capacitance) diff --git a/kernel/driver.cc b/kernel/driver.cc index 65f09099303..6565c472c2a 100644 --- a/kernel/driver.cc +++ b/kernel/driver.cc @@ -31,6 +31,10 @@ # include #endif +#ifdef YOSYS_ENABLE_TCL +# include +#endif + #include #include #include @@ -276,6 +280,8 @@ int main(int argc, char **argv) options.add_options("developer") ("X,trace", "enable tracing of core data structure changes. for debugging") ("M,randomize-pointers", "will slightly randomize allocated pointer addresses. for debugging") + ("autoidx", "start counting autoidx up from , similar effect to --hash-seed", + cxxopts::value(), "") ("A,abort", "will call abort() at the end of the script. for debugging") ("x,experimental", "do not print warnings for the experimental ", cxxopts::value>(), "") @@ -427,6 +433,10 @@ int main(int argc, char **argv) if (result.count("infile")) { frontend_files = result["infile"].as>(); } + if (result.count("autoidx")) { + int idx = result["autoidx"].as(); + autoidx = idx; + } if (log_errfile == NULL) { log_files.push_back(stdout); diff --git a/kernel/drivertools.h b/kernel/drivertools.h index 079701c35cc..8929c342627 100644 --- a/kernel/drivertools.h +++ b/kernel/drivertools.h @@ -364,7 +364,7 @@ struct DriveBit unsigned int hash() const { - unsigned int inner; + unsigned int inner = 0; switch (type_) { case DriveType::NONE: @@ -385,6 +385,9 @@ struct DriveBit case DriveType::MULTIPLE: inner = multiple_.hash(); break; + default: + log_abort(); + break; } return mkhash((unsigned int)type_, inner); } @@ -912,7 +915,7 @@ struct DriveChunk unsigned int hash() const { - unsigned int inner; + unsigned int inner = 0; switch (type_) { case DriveType::NONE: @@ -933,6 +936,9 @@ struct DriveChunk case DriveType::MULTIPLE: inner = multiple_.hash(); break; + default: + log_abort(); + break; } return mkhash((unsigned int)type_, inner); } diff --git a/kernel/fmt.cc b/kernel/fmt.cc index d1c6b8ac96d..6ba1150cef1 100644 --- a/kernel/fmt.cc +++ b/kernel/fmt.cc @@ -630,7 +630,7 @@ std::string escape_cxx_string(const std::string &input) std::string output = "\""; for (auto c : input) { if (::isprint(c)) { - if (c == '\\') + if (c == '\\' || c == '"') output.push_back('\\'); output.push_back(c); } else { diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index 65ae3426cd6..a62691fa139 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -137,14 +137,22 @@ void FstData::extractVarNames() if (!var.is_alias) handle_to_var[h->u.var.handle] = var; std::string clean_name; + bool has_space = false; for(size_t i=0;iu.var.name);i++) { char c = h->u.var.name[i]; - if(c==' ') break; + if(c==' ') { has_space = true; break; } clean_name += c; } if (clean_name[0]=='\\') clean_name = clean_name.substr(1); + if (!has_space) { + size_t pos = clean_name.find_last_of("["); + std::string index_or_range = clean_name.substr(pos+1); + if (index_or_range.find(":") != std::string::npos) { + clean_name = clean_name.substr(0,pos); + } + } size_t pos = clean_name.find_last_of("<"); if (pos != std::string::npos && clean_name.back() == '>') { std::string mem_cell = clean_name.substr(0, pos); @@ -258,7 +266,8 @@ void FstData::reconstructAllAtTimes(std::vector &signal, uint64_t sta std::string FstData::valueOf(fstHandle signal) { - if (past_data.find(signal) == past_data.end()) - log_error("Signal id %d not found\n", (int)signal); + if (past_data.find(signal) == past_data.end()) { + return std::string(handle_to_var[signal].width, 'x'); + } return past_data[signal]; } diff --git a/kernel/functional.cc b/kernel/functional.cc index c270a2bb631..adf7bfb0c65 100644 --- a/kernel/functional.cc +++ b/kernel/functional.cc @@ -253,7 +253,7 @@ class CellSimplifier { int y_width = parameters.at(ID(Y_WIDTH), Const(-1)).as_int(); bool a_signed = parameters.at(ID(A_SIGNED), Const(0)).as_bool(); bool b_signed = parameters.at(ID(B_SIGNED), Const(0)).as_bool(); - if(cellType.in({ID($add), ID($sub), ID($and), ID($or), ID($xor), ID($xnor), ID($mul)})){ + if(cellType.in(ID($add), ID($sub), ID($and), ID($or), ID($xor), ID($xnor), ID($mul))){ bool is_signed = a_signed && b_signed; Node a = factory.extend(inputs.at(ID(A)), y_width, is_signed); Node b = factory.extend(inputs.at(ID(B)), y_width, is_signed); @@ -273,14 +273,14 @@ class CellSimplifier { return factory.bitwise_not(factory.bitwise_xor(a, b)); else log_abort(); - }else if(cellType.in({ID($eq), ID($ne), ID($eqx), ID($nex), ID($le), ID($lt), ID($ge), ID($gt)})){ + }else if(cellType.in(ID($eq), ID($ne), ID($eqx), ID($nex), ID($le), ID($lt), ID($ge), ID($gt))){ bool is_signed = a_signed && b_signed; int width = max(a_width, b_width); Node a = factory.extend(inputs.at(ID(A)), width, is_signed); Node b = factory.extend(inputs.at(ID(B)), width, is_signed); - if(cellType.in({ID($eq), ID($eqx)})) + if(cellType.in(ID($eq), ID($eqx))) return factory.extend(factory.equal(a, b), y_width, false); - else if(cellType.in({ID($ne), ID($nex)})) + else if(cellType.in(ID($ne), ID($nex))) return factory.extend(factory.not_equal(a, b), y_width, false); else if(cellType == ID($lt)) return factory.extend(is_signed ? factory.signed_greater_than(b, a) : factory.unsigned_greater_than(b, a), y_width, false); @@ -292,7 +292,7 @@ class CellSimplifier { return factory.extend(is_signed ? factory.signed_greater_equal(a, b) : factory.unsigned_greater_equal(a, b), y_width, false); else log_abort(); - }else if(cellType.in({ID($logic_or), ID($logic_and)})){ + }else if(cellType.in(ID($logic_or), ID($logic_and))){ Node a = factory.reduce_or(inputs.at(ID(A))); Node b = factory.reduce_or(inputs.at(ID(B))); Node y = cellType == ID($logic_and) ? factory.bitwise_and(a, b) : factory.bitwise_or(a, b); @@ -309,13 +309,13 @@ class CellSimplifier { Node a = factory.reduce_or(inputs.at(ID(A))); Node y = factory.bitwise_not(a); return factory.extend(y, y_width, false); - }else if(cellType.in({ID($reduce_or), ID($reduce_bool)})){ + }else if(cellType.in(ID($reduce_or), ID($reduce_bool))){ Node a = factory.reduce_or(inputs.at(ID(A))); return factory.extend(a, y_width, false); }else if(cellType == ID($reduce_and)){ Node a = factory.reduce_and(inputs.at(ID(A))); return factory.extend(a, y_width, false); - }else if(cellType.in({ID($reduce_xor), ID($reduce_xnor)})){ + }else if(cellType.in(ID($reduce_xor), ID($reduce_xnor))){ Node a = factory.reduce_xor(inputs.at(ID(A))); Node y = cellType == ID($reduce_xnor) ? factory.bitwise_not(a) : a; return factory.extend(y, y_width, false); @@ -355,7 +355,7 @@ class CellSimplifier { int offset = parameters.at(ID(OFFSET)).as_int(); Node a = inputs.at(ID(A)); return factory.slice(a, offset, y_width); - }else if(cellType.in({ID($div), ID($mod), ID($divfloor), ID($modfloor)})) { + }else if(cellType.in(ID($div), ID($mod), ID($divfloor), ID($modfloor))) { int width = max(a_width, b_width); bool is_signed = a_signed && b_signed; Node a = factory.extend(inputs.at(ID(A)), width, is_signed); @@ -397,7 +397,7 @@ class CellSimplifier { } else log_error("unhandled cell in CellSimplifier %s\n", cellType.c_str()); } else { - if(cellType.in({ID($mod), ID($modfloor)})) + if(cellType.in(ID($mod), ID($modfloor))) return factory.extend(factory.unsigned_mod(a, b), y_width, false); else return factory.extend(factory.unsigned_div(a, b), y_width, false); @@ -439,12 +439,12 @@ class CellSimplifier { return handle_lcu(inputs.at(ID(P)), inputs.at(ID(G)), inputs.at(ID(CI))); } else if(cellType == ID($alu)) { return handle_alu(inputs.at(ID(A)), inputs.at(ID(B)), y_width, a_signed && b_signed, inputs.at(ID(CI)), inputs.at(ID(BI))); - } else if(cellType.in({ID($assert), ID($assume), ID($live), ID($fair), ID($cover)})) { + } else if(cellType.in(ID($assert), ID($assume), ID($live), ID($fair), ID($cover))) { Node a = factory.mux(factory.constant(Const(State::S1, 1)), inputs.at(ID(A)), inputs.at(ID(EN))); auto &output = factory.add_output(cellName, cellType, Sort(1)); output.set_value(a); return {}; - } else if(cellType.in({ID($anyconst), ID($allconst), ID($anyseq), ID($allseq)})) { + } else if(cellType.in(ID($anyconst), ID($allconst), ID($anyseq), ID($allseq))) { int width = parameters.at(ID(WIDTH)).as_int(); auto &input = factory.add_input(cellName, cellType, Sort(width)); return factory.value(input); diff --git a/kernel/macc.h b/kernel/macc.h index 6bcd4cc3b0f..4af08cfd8ee 100644 --- a/kernel/macc.h +++ b/kernel/macc.h @@ -228,6 +228,14 @@ struct Macc return true; } + bool is_simple_product() + { + return bit_ports.empty() && + ports.size() == 1 && + !ports[0].in_b.empty() && + !ports[0].do_subtract; + } + Macc(RTLIL::Cell *cell = nullptr) { if (cell != nullptr) diff --git a/kernel/mem.cc b/kernel/mem.cc index 5e20c83eeed..67501acfdb4 100644 --- a/kernel/mem.cc +++ b/kernel/mem.cc @@ -1680,8 +1680,6 @@ SigSpec MemWr::decompress_en(const std::vector &swizzle, SigSpec sig) { return res; } -using addr_t = MemContents::addr_t; - MemContents::MemContents(Mem *mem) : MemContents(ceil_log2(mem->size), mem->width) { @@ -1758,7 +1756,7 @@ bool MemContents::_range_overlaps(std::map::iterator it, a return !(top1 < begin_addr || top2 < _range_begin(it)); } -std::map::iterator MemContents::_range_at(addr_t addr) const { +std::map::iterator MemContents::_range_at(addr_t addr) const { // allow addr == 1<<_addr_width (which will just return end()) log_assert(addr <= (addr_t)(1<<_addr_width)); // get the first range with base > addr @@ -1786,7 +1784,7 @@ RTLIL::Const MemContents::operator[](addr_t addr) const { return _default_value; } -addr_t MemContents::count_range(addr_t begin_addr, addr_t end_addr) const { +MemContents::addr_t MemContents::count_range(addr_t begin_addr, addr_t end_addr) const { addr_t count = 0; for(auto it = _range_at(begin_addr); _range_overlaps(it, begin_addr, end_addr); it++) { auto first = std::max(_range_begin(it), begin_addr); @@ -1829,7 +1827,7 @@ void MemContents::clear_range(addr_t begin_addr, addr_t end_addr) { _values.erase(begin_it, end_it); } -std::map::iterator MemContents::_reserve_range(addr_t begin_addr, addr_t end_addr) { +std::map::iterator MemContents::_reserve_range(addr_t begin_addr, addr_t end_addr) { if(begin_addr >= end_addr) return _values.end(); // need a dummy value to return, end() is cheap // find the first range containing any addr >= begin_addr - 1 diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 180982a7cba..b279cce6e15 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -380,7 +380,7 @@ int RTLIL::Const::as_int(bool is_signed) const return ret; } -size_t RTLIL::Const::get_min_size(bool is_signed) const +int RTLIL::Const::get_min_size(bool is_signed) const { if (empty()) return 0; @@ -391,7 +391,7 @@ size_t RTLIL::Const::get_min_size(bool is_signed) const else leading_bit = RTLIL::State::S0; - size_t idx = size(); + auto idx = size(); while (idx > 0 && (*this)[idx -1] == leading_bit) { idx--; } @@ -406,22 +406,22 @@ size_t RTLIL::Const::get_min_size(bool is_signed) const void RTLIL::Const::compress(bool is_signed) { - size_t idx = get_min_size(is_signed); + auto idx = get_min_size(is_signed); bits().erase(bits().begin() + idx, bits().end()); } std::optional RTLIL::Const::as_int_compress(bool is_signed) const { - size_t size = get_min_size(is_signed); + auto size = get_min_size(is_signed); if(size == 0 || size > 32) return std::nullopt; int32_t ret = 0; - for (size_t i = 0; i < size && i < 32; i++) + for (auto i = 0; i < size && i < 32; i++) if ((*this)[i] == State::S1) ret |= 1 << i; if (is_signed && (*this)[size-1] == State::S1) - for (size_t i = size; i < 32; i++) + for (auto i = size; i < 32; i++) ret |= 1 << i; return ret; } @@ -1856,9 +1856,9 @@ namespace { param_bits(ID::RD_COLLISION_X_MASK, max(1, param(ID::RD_PORTS) * param(ID::WR_PORTS))); param_bits(ID::RD_WIDE_CONTINUATION, max(1, param(ID::RD_PORTS))); param_bits(ID::RD_CE_OVER_SRST, max(1, param(ID::RD_PORTS))); - param_bits(ID::RD_ARST_VALUE, param(ID::RD_PORTS) * param(ID::WIDTH)); - param_bits(ID::RD_SRST_VALUE, param(ID::RD_PORTS) * param(ID::WIDTH)); - param_bits(ID::RD_INIT_VALUE, param(ID::RD_PORTS) * param(ID::WIDTH)); + param_bits(ID::RD_ARST_VALUE, max(1, param(ID::RD_PORTS) * param(ID::WIDTH))); + param_bits(ID::RD_SRST_VALUE, max(1, param(ID::RD_PORTS) * param(ID::WIDTH))); + param_bits(ID::RD_INIT_VALUE, max(1, param(ID::RD_PORTS) * param(ID::WIDTH))); param_bits(ID::WR_CLK_ENABLE, max(1, param(ID::WR_PORTS))); param_bits(ID::WR_CLK_POLARITY, max(1, param(ID::WR_PORTS))); param_bits(ID::WR_WIDE_CONTINUATION, max(1, param(ID::WR_PORTS))); @@ -2147,6 +2147,21 @@ namespace { check_expected(); return; } + /* + * Checklist for adding internal cell types + * ======================================== + * Things to do right away: + * - Add to kernel/celltypes.h (incl. eval() handling for non-mem cells) + * - Add to InternalCellChecker::check() in kernel/rtlil.cc + * - Add to techlibs/common/simlib.v + * - Add to techlibs/common/techmap.v + * + * Things to do after finalizing the cell interface: + * - Add support to kernel/satgen.h for the new cell type + * - Add to docs/source/CHAPTER_CellLib.rst (or just add a fixme to the bottom) + * - Maybe add support to the Verilog backend for dumping such cells as expression + * + */ error(__LINE__); } }; diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 0f3984ab8fc..6d3558621c1 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -780,7 +780,7 @@ struct RTLIL::Const RTLIL::Const extract(int offset, int len = 1, RTLIL::State padding = RTLIL::State::S0) const; // find the MSB without redundant leading bits - size_t get_min_size(bool is_signed) const; + int get_min_size(bool is_signed) const; // compress representation to the minimum required bits void compress(bool is_signed = false); @@ -814,6 +814,7 @@ struct RTLIL::AttrObject void set_bool_attribute(const RTLIL::IdString &id, bool value=true); bool get_bool_attribute(const RTLIL::IdString &id) const; + [[deprecated("Use Module::get_blackbox_attribute() instead.")]] bool get_blackbox_attribute(bool ignore_wb=false) const { return get_bool_attribute(ID::blackbox) || (!ignore_wb && get_bool_attribute(ID::whitebox)); } @@ -1291,6 +1292,10 @@ struct RTLIL::Module : public RTLIL::AttrObject virtual void optimize(); virtual void makeblackbox(); + bool get_blackbox_attribute(bool ignore_wb=false) const { + return get_bool_attribute(ID::blackbox) || (!ignore_wb && get_bool_attribute(ID::whitebox)); + } + void connect(const RTLIL::SigSig &conn); void connect(const RTLIL::SigSpec &lhs, const RTLIL::SigSpec &rhs); void new_connections(const std::vector &new_conn); diff --git a/kernel/scopeinfo.h b/kernel/scopeinfo.h index 71af70344ce..8c6e99fc066 100644 --- a/kernel/scopeinfo.h +++ b/kernel/scopeinfo.h @@ -169,6 +169,10 @@ class IdTree return !(*this == other); } + int hash() const { + return mkhash(scope_name.hash(), hash_ptr_ops::hash(target)); + } + bool valid() const { return target != nullptr; } diff --git a/kernel/tclapi.cc b/kernel/tclapi.cc new file mode 100644 index 00000000000..be39ca4a08c --- /dev/null +++ b/kernel/tclapi.cc @@ -0,0 +1,574 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2012 Claire Xenia Wolf + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "kernel/yosys.h" +#include "kernel/rtlil.h" +#include "libs/json11/json11.hpp" + +#ifdef YOSYS_ENABLE_TCL +#include +#include +#include +#endif + +YOSYS_NAMESPACE_BEGIN + +#ifdef YOSYS_ENABLE_TCL +bool yosys_tcl_repl_active = false; + +void yosys_tcl_activate_repl() +{ + yosys_tcl_repl_active = true; +} + +static Tcl_Obj *json_to_tcl(Tcl_Interp *interp, const json11::Json &json) +{ + if (json.is_null()) + return Tcl_NewStringObj("null", 4); + else if (json.is_string()) { + auto string = json.string_value(); + return Tcl_NewStringObj(string.data(), string.size()); + } else if (json.is_number()) { + double value = json.number_value(); + double round_val = std::nearbyint(value); + if (std::isfinite(round_val) && value == round_val && value >= LONG_MIN && value < -double(LONG_MIN)) + return Tcl_NewLongObj((long)round_val); + else + return Tcl_NewDoubleObj(value); + } else if (json.is_bool()) { + return Tcl_NewBooleanObj(json.bool_value()); + } else if (json.is_array()) { + auto list = json.array_items(); + Tcl_Obj *result = Tcl_NewListObj(list.size(), nullptr); + for (auto &item : list) + Tcl_ListObjAppendElement(interp, result, json_to_tcl(interp, item)); + return result; + } else if (json.is_object()) { + auto map = json.object_items(); + Tcl_Obj *result = Tcl_NewListObj(map.size() * 2, nullptr); + for (auto &item : map) { + Tcl_ListObjAppendElement(interp, result, Tcl_NewStringObj(item.first.data(), item.first.size())); + Tcl_ListObjAppendElement(interp, result, json_to_tcl(interp, item.second)); + } + return result; + } else { + log_abort(); + } +} + +static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) +{ + std::vector args; + for (int i = 1; i < argc; i++) + args.push_back(argv[i]); + + if (args.size() >= 1 && args[0] == "-import") { + for (auto &it : pass_register) { + std::string tcl_command_name = it.first; + if (tcl_command_name == "proc") + tcl_command_name = "procs"; + else if (tcl_command_name == "rename") + tcl_command_name = "renames"; + Tcl_CmdInfo info; + if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) { + log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str()); + } else { + std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str()); + Tcl_Eval(interp, tcl_script.c_str()); + } + } + return TCL_OK; + } + + yosys_get_design()->scratchpad_unset("result.json"); + yosys_get_design()->scratchpad_unset("result.string"); + + bool in_repl = yosys_tcl_repl_active; + bool restore_log_cmd_error_throw = log_cmd_error_throw; + + log_cmd_error_throw = true; + + try { + if (args.size() == 1) { + Pass::call(yosys_get_design(), args[0]); + } else { + Pass::call(yosys_get_design(), args); + } + } catch (log_cmd_error_exception) { + if (in_repl) { + auto design = yosys_get_design(); + while (design->selection_stack.size() > 1) + design->selection_stack.pop_back(); + log_reset_stack(); + } + Tcl_SetResult(interp, (char *)"Yosys command produced an error", TCL_STATIC); + + yosys_tcl_repl_active = in_repl; + log_cmd_error_throw = restore_log_cmd_error_throw; + return TCL_ERROR; + } catch (...) { + log_error("uncaught exception during Yosys command invoked from TCL\n"); + } + + yosys_tcl_repl_active = in_repl; + log_cmd_error_throw = restore_log_cmd_error_throw; + + auto &scratchpad = yosys_get_design()->scratchpad; + auto result = scratchpad.find("result.json"); + if (result != scratchpad.end()) { + std::string err; + auto json = json11::Json::parse(result->second, err); + if (err.empty()) { + Tcl_SetObjResult(interp, json_to_tcl(interp, json)); + } else + log_warning("Ignoring result.json scratchpad value due to parse error: %s\n", err.c_str()); + } else if ((result = scratchpad.find("result.string")) != scratchpad.end()) { + Tcl_SetObjResult(interp, Tcl_NewStringObj(result->second.data(), result->second.size())); + } + + return TCL_OK; +} + +#define FLAG(name) \ + if (!strcmp(argv[i], "-" #name)) { \ + name##_flag = true; \ + continue; \ + } \ + +#define FLAG2(name) \ + if (!strcmp(Tcl_GetString(objv[i]), "-" #name)) { \ + name##_flag = true; \ + continue; \ + } \ + +#define ERROR(str) \ + { \ + Tcl_SetResult(interp, (char *)(str), TCL_STATIC); \ + return TCL_ERROR; \ + } + +bool const_to_mp_int(const Const &a, mp_int *b, bool force_signed, bool force_unsigned) +{ + if (!a.is_fully_def()) + return false; + + if (mp_init(b)) + return false; + + bool negative = ((a.flags & RTLIL::CONST_FLAG_SIGNED) || force_signed) && + !force_unsigned && + !a.empty() && (a.back() == RTLIL::S1); + + for (int i = a.size() - 1; i >= 0; i--) { + if (mp_mul_2d(b, 1, b)) { + mp_clear(b); + return false; + } + + if ((a[i] == RTLIL::S1) ^ negative) { + if (mp_add_d(b, 1, b)) { + mp_clear(b); + return false; + } + } + } + + if (negative) { + if (mp_add_d(b, 1, b) || mp_neg(b, b)) { + mp_clear(b); + return false; + } + } + + return true; +} + +bool mp_int_to_const(mp_int *a, Const &b, bool is_signed) +{ + bool negative = (mp_cmp_d(a, 0) == MP_LT); + if (negative && !is_signed) + return false; + + if (negative) { + mp_neg(a, a); + mp_sub_d(a, 1, a); + } + + std::vector buf; + buf.resize(mp_unsigned_bin_size(a)); + mp_to_unsigned_bin(a, buf.data()); + + b.bits().reserve(mp_count_bits(a) + is_signed); + for (int i = 0; i < mp_count_bits(a);) { + for (int j = 0; j < 8 && i < mp_count_bits(a); j++, i++) { + bool bv = ((buf.back() & (1 << j)) != 0) ^ negative; + b.bits().push_back(bv ? RTLIL::S1 : RTLIL::S0); + } + buf.pop_back(); + } + + if (is_signed) { + b.bits().push_back(negative ? RTLIL::S1 : RTLIL::S0); + } + + return true; +} + +static int tcl_get_attr(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) +{ + int i; + bool mod_flag = false, string_flag = false, bool_flag = false; + bool int_flag = false, sint_flag = false, uint_flag = false; + for (i = 1; i < argc; i++) { + FLAG(mod) + FLAG(string) + FLAG(int) + FLAG(sint) + FLAG(uint) + FLAG(bool) + break; + } + + if ((mod_flag && i != argc - 2) || + (!mod_flag && i != argc - 3) || + (string_flag + int_flag + sint_flag + uint_flag + bool_flag > 1)) + ERROR("bad usage: expected \"get_attr -mod [-string|-int|-sint|-uint|-bool] \"" + " or \"get_attr [-string|-int|-sint|-uint|-bool] \"") + + IdString mod_id, obj_id, attr_id; + mod_id = RTLIL::escape_id(argv[i++]); + if (!mod_flag) + obj_id = RTLIL::escape_id(argv[i++]); + attr_id = RTLIL::escape_id(argv[i++]); + + RTLIL::Module *mod = yosys_design->module(mod_id); + if (!mod) + ERROR("module not found") + + RTLIL::AttrObject *obj = nullptr; + if (mod_flag) { + obj = mod; + } else { + obj = mod->wire(obj_id); + if (!obj) + obj = mod->memories.at(obj_id, nullptr); + if (!obj) + obj = mod->cell(obj_id); + if (!obj) + obj = mod->processes.at(obj_id, nullptr); + } + + if (!obj) + ERROR("object not found") + + if (string_flag) { + Tcl_SetResult(interp, (char *) obj->get_string_attribute(attr_id).c_str(), TCL_VOLATILE); + } else if (int_flag || uint_flag || sint_flag) { + if (!obj->has_attribute(attr_id)) + ERROR("attribute missing (required for -int)"); + RTLIL::Const &value = obj->attributes.at(attr_id); + + mp_int value_mp; + if (!const_to_mp_int(value, &value_mp, sint_flag, uint_flag)) + ERROR("bignum manipulation failed"); + Tcl_SetObjResult(interp, Tcl_NewBignumObj(&value_mp)); + } else if (bool_flag) { + Tcl_SetObjResult(interp, Tcl_NewBooleanObj(obj->get_bool_attribute(attr_id))); + } else { + if (!obj->has_attribute(attr_id)) + ERROR("attribute missing (required unless -bool or -string)") + + Tcl_SetResult(interp, (char *) obj->attributes.at(attr_id).as_string().c_str(), TCL_VOLATILE); + } + + return TCL_OK; +} + +static int tcl_has_attr(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) +{ + int i; + bool mod_flag = false; + for (i = 1; i < argc; i++) { + FLAG(mod) + break; + } + + if ((mod_flag && i != argc - 2) || + (!mod_flag && i != argc - 3)) + ERROR("bad usage: expected \"has_attr -mod \"" + " or \"has_attr \"") + + IdString mod_id, obj_id, attr_id; + mod_id = RTLIL::escape_id(argv[i++]); + if (!mod_flag) + obj_id = RTLIL::escape_id(argv[i++]); + attr_id = RTLIL::escape_id(argv[i++]); + + RTLIL::Module *mod = yosys_design->module(mod_id); + if (!mod) + ERROR("module not found") + + RTLIL::AttrObject *obj = nullptr; + if (mod_flag) { + obj = mod; + } else { + obj = mod->wire(obj_id); + if (!obj) + obj = mod->memories.at(obj_id, nullptr); + if (!obj) + obj = mod->cell(obj_id); + if (!obj) + obj = mod->processes.at(obj_id, nullptr); + } + + if (!obj) + ERROR("object not found") + + Tcl_SetResult(interp, (char *) std::to_string(obj->has_attribute(attr_id)).c_str(), TCL_VOLATILE); + return TCL_OK; +} + +static int tcl_set_attr(ClientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) +{ + int i; + bool mod_flag = false, string_flag = false, bool_flag = false; + bool true_flag = false, false_flag = false, sint_flag = false, uint_flag = false; + for (i = 1; i < objc; i++) { + FLAG2(mod) + FLAG2(string) + FLAG2(true) + FLAG2(false) + FLAG2(sint) + FLAG2(uint) + FLAG2(bool) + break; + } + + if ((i != objc - (2 + !mod_flag + !(true_flag || false_flag))) || + (string_flag + sint_flag + uint_flag + bool_flag + true_flag + false_flag > 1)) + ERROR("bad usage: expected \"set_attr -mod [-string|-sint|-uint|-bool] \"" + " or \"set_attr [-string|-sint|-uint|-bool] \"" + " or \"set_attr [-true|-false] \"" + " or \"set_attr -mod [-true|-false| \"") + + IdString mod_id, obj_id, attr_id; + mod_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + if (!mod_flag) + obj_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + attr_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + + RTLIL::Module *mod = yosys_design->module(mod_id); + if (!mod) + ERROR("module not found") + + RTLIL::AttrObject *obj = nullptr; + if (mod_flag) { + obj = mod; + } else { + obj = mod->wire(obj_id); + if (!obj) + obj = mod->memories.at(obj_id, nullptr); + if (!obj) + obj = mod->cell(obj_id); + if (!obj) + obj = mod->processes.at(obj_id, nullptr); + } + + if (!obj) + ERROR("object not found") + + if (string_flag) { + obj->set_string_attribute(attr_id, Tcl_GetString(objv[i++])); + } else if (sint_flag || uint_flag) { + RTLIL::Const const_; + mp_int value_mp; + + if (Tcl_TakeBignumFromObj(interp, objv[i++], &value_mp)) + ERROR("non-integral value") + + if (!mp_int_to_const(&value_mp, const_, sint_flag)) + ERROR("bignum manipulation failed"); + + if (sint_flag) { + const_.flags |= RTLIL::CONST_FLAG_SIGNED; + if (const_.size() < 32) + const_.exts(32); + } else { + if (const_.size() < 32) + const_.extu(32); + } + + obj->attributes[attr_id] = const_; + } else if (bool_flag) { + obj->set_bool_attribute(attr_id, atoi(Tcl_GetString(objv[i++])) != 0); + } else if (true_flag) { + obj->set_bool_attribute(attr_id, true); + } else if (false_flag) { + obj->set_bool_attribute(attr_id, false); + } else { + obj->attributes[attr_id] = Const::from_string(std::string(Tcl_GetString(objv[i++]))); + } + + return TCL_OK; +} + +static int tcl_get_param(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) +{ + int i; + bool string_flag = false; + bool int_flag = false, sint_flag = false, uint_flag = false; + for (i = 1; i < argc; i++) { + FLAG(string) + FLAG(int) + FLAG(sint) + FLAG(uint) + break; + } + + if ((i != argc - 3) || + (string_flag + int_flag > 1)) + ERROR("bad usage: expected \"get_param [-string|-int|-sint|-uint] ") + + IdString mod_id, cell_id, param_id; + mod_id = RTLIL::escape_id(argv[i++]); + cell_id = RTLIL::escape_id(argv[i++]); + param_id = RTLIL::escape_id(argv[i++]); + + RTLIL::Module *mod = yosys_design->module(mod_id); + if (!mod) + ERROR("module not found") + + RTLIL::Cell *cell = mod->cell(cell_id); + if (!cell) + ERROR("object not found") + + if (!cell->hasParam(param_id)) + ERROR("parameter missing") + + const RTLIL::Const &value = cell->getParam(param_id); + + if (string_flag) { + Tcl_SetResult(interp, (char *) value.decode_string().c_str(), TCL_VOLATILE); + } else if (int_flag || uint_flag || sint_flag) { + mp_int value_mp; + if (!const_to_mp_int(value, &value_mp, sint_flag, uint_flag)) + ERROR("bignum manipulation failed"); + Tcl_SetObjResult(interp, Tcl_NewBignumObj(&value_mp)); + } else { + Tcl_SetResult(interp, (char *) value.as_string().c_str(), TCL_VOLATILE); + } + return TCL_OK; +} + +static int tcl_set_param(ClientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) +{ + int i; + bool string_flag = false, sint_flag = false, uint_flag = false; + for (i = 1; i < objc; i++) { + FLAG2(string) + FLAG2(sint) + FLAG2(uint) + break; + } + + if ((i != objc - 4) || + (string_flag + sint_flag + uint_flag > 1)) + ERROR("bad usage: expected \"set_param [-string|-sint|-uint] ") + + IdString mod_id, cell_id, param_id; + mod_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + cell_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + param_id = RTLIL::escape_id(Tcl_GetString(objv[i++])); + + RTLIL::Module *mod = yosys_design->module(mod_id); + if (!mod) + ERROR("module not found") + + RTLIL::Cell *cell = mod->cell(cell_id); + if (!cell) + ERROR("object not found") + + if (string_flag) { + cell->setParam(param_id, Const(std::string(Tcl_GetString(objv[i++])))); + } else if (sint_flag || uint_flag) { + RTLIL::Const const_; + mp_int value_mp; + + if (Tcl_TakeBignumFromObj(interp, objv[i++], &value_mp)) + ERROR("non-integral value") + + if (!mp_int_to_const(&value_mp, const_, sint_flag)) + ERROR("bignum manipulation failed"); + + if (sint_flag) { + const_.flags |= RTLIL::CONST_FLAG_SIGNED; + if (const_.size() < 32) + const_.exts(32); + } else { + if (const_.size() < 32) + const_.extu(32); + } + + cell->setParam(param_id, const_); + } else { + cell->setParam(param_id, Const::from_string(std::string(Tcl_GetString(objv[i++])))); + } + return TCL_OK; +} + +int yosys_tcl_iterp_init(Tcl_Interp *interp) +{ + if (Tcl_Init(interp)!=TCL_OK) + log_warning("Tcl_Init() call failed - %s\n",Tcl_ErrnoMsg(Tcl_GetErrno())); + Tcl_CreateCommand(interp, "yosys", tcl_yosys_cmd, NULL, NULL); + Tcl_CreateCommand(interp, "rtlil::get_attr", tcl_get_attr, NULL, NULL); + Tcl_CreateCommand(interp, "rtlil::has_attr", tcl_has_attr, NULL, NULL); + Tcl_CreateObjCommand(interp, "rtlil::set_attr", tcl_set_attr, NULL, NULL); + Tcl_CreateCommand(interp, "rtlil::get_param", tcl_get_param, NULL, NULL); + Tcl_CreateObjCommand(interp, "rtlil::set_param", tcl_set_param, NULL, NULL); + + // TODO: + // + // port_list + // wire_list + // cell_list + // wire_width + // + // add_wire + // add_cell + // rename_wire + // rename_cell + // remove + // + // SigSpec land + // + // get_conn + // set_conn + // unpack + // pack + + // Note (dev jf 24-12-02): Make log_id escape everything that’s not a valid + // verilog identifier before adding any tcl API that returns IdString values + // to avoid -option injection + + return TCL_OK ; +} +#endif + +YOSYS_NAMESPACE_END diff --git a/kernel/timinginfo.h b/kernel/timinginfo.h index e7e4eab6e2e..8eb7eb738ac 100644 --- a/kernel/timinginfo.h +++ b/kernel/timinginfo.h @@ -37,6 +37,14 @@ struct TimingInfo bool operator==(const NameBit& nb) const { return nb.name == name && nb.offset == offset; } bool operator!=(const NameBit& nb) const { return !operator==(nb); } unsigned int hash() const { return mkhash_add(name.hash(), offset); } + std::optional get_connection(RTLIL::Cell *cell) { + if (!cell->hasPort(name)) + return {}; + auto &port = cell->getPort(name); + if (offset >= port.size()) + return {}; + return port[offset]; + } }; struct BitBit { diff --git a/kernel/yosys.cc b/kernel/yosys.cc index 774c7f37dc4..bdd7303aa45 100644 --- a/kernel/yosys.cc +++ b/kernel/yosys.cc @@ -29,6 +29,10 @@ # include #endif +#ifdef YOSYS_ENABLE_TCL +# include +#endif + #ifdef YOSYS_ENABLE_PLUGINS # include #endif @@ -73,8 +77,6 @@ #include #include -#include "libs/json11/json11.hpp" - YOSYS_NAMESPACE_BEGIN int autoidx = 1; @@ -84,7 +86,6 @@ CellTypes yosys_celltypes; #ifdef YOSYS_ENABLE_TCL Tcl_Interp *yosys_tcl_interp = NULL; -bool yosys_tcl_repl_active = false; #endif std::set yosys_input_files, yosys_output_files; @@ -722,126 +723,8 @@ void rewrite_filename(std::string &filename) #ifdef YOSYS_ENABLE_TCL -static Tcl_Obj *json_to_tcl(Tcl_Interp *interp, const json11::Json &json) -{ - if (json.is_null()) - return Tcl_NewStringObj("null", 4); - else if (json.is_string()) { - auto string = json.string_value(); - return Tcl_NewStringObj(string.data(), string.size()); - } else if (json.is_number()) { - double value = json.number_value(); - double round_val = std::nearbyint(value); - if (std::isfinite(round_val) && value == round_val && value >= LONG_MIN && value < -double(LONG_MIN)) - return Tcl_NewLongObj((long)round_val); - else - return Tcl_NewDoubleObj(value); - } else if (json.is_bool()) { - return Tcl_NewBooleanObj(json.bool_value()); - } else if (json.is_array()) { - auto list = json.array_items(); - Tcl_Obj *result = Tcl_NewListObj(list.size(), nullptr); - for (auto &item : list) - Tcl_ListObjAppendElement(interp, result, json_to_tcl(interp, item)); - return result; - } else if (json.is_object()) { - auto map = json.object_items(); - Tcl_Obj *result = Tcl_NewListObj(map.size() * 2, nullptr); - for (auto &item : map) { - Tcl_ListObjAppendElement(interp, result, Tcl_NewStringObj(item.first.data(), item.first.size())); - Tcl_ListObjAppendElement(interp, result, json_to_tcl(interp, item.second)); - } - return result; - } else { - log_abort(); - } -} - -static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[]) -{ - std::vector args; - for (int i = 1; i < argc; i++) - args.push_back(argv[i]); - - if (args.size() >= 1 && args[0] == "-import") { - for (auto &it : pass_register) { - std::string tcl_command_name = it.first; - if (tcl_command_name == "proc") - tcl_command_name = "procs"; - else if (tcl_command_name == "rename") - tcl_command_name = "renames"; - Tcl_CmdInfo info; - if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) { - log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str()); - } else { - std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str()); - Tcl_Eval(interp, tcl_script.c_str()); - } - } - return TCL_OK; - } - - yosys_get_design()->scratchpad_unset("result.json"); - yosys_get_design()->scratchpad_unset("result.string"); - - bool in_repl = yosys_tcl_repl_active; - bool restore_log_cmd_error_throw = log_cmd_error_throw; - - log_cmd_error_throw = true; - - try { - if (args.size() == 1) { - Pass::call(yosys_get_design(), args[0]); - } else { - Pass::call(yosys_get_design(), args); - } - } catch (log_cmd_error_exception) { - if (in_repl) { - auto design = yosys_get_design(); - while (design->selection_stack.size() > 1) - design->selection_stack.pop_back(); - log_reset_stack(); - } - Tcl_SetResult(interp, (char *)"Yosys command produced an error", TCL_STATIC); - - yosys_tcl_repl_active = in_repl; - log_cmd_error_throw = restore_log_cmd_error_throw; - return TCL_ERROR; - } catch (...) { - log_error("uncaught exception during Yosys command invoked from TCL\n"); - } - - yosys_tcl_repl_active = in_repl; - log_cmd_error_throw = restore_log_cmd_error_throw; - - auto &scratchpad = yosys_get_design()->scratchpad; - auto result = scratchpad.find("result.json"); - if (result != scratchpad.end()) { - std::string err; - auto json = json11::Json::parse(result->second, err); - if (err.empty()) { - Tcl_SetObjResult(interp, json_to_tcl(interp, json)); - } else - log_warning("Ignoring result.json scratchpad value due to parse error: %s\n", err.c_str()); - } else if ((result = scratchpad.find("result.string")) != scratchpad.end()) { - Tcl_SetObjResult(interp, Tcl_NewStringObj(result->second.data(), result->second.size())); - } - - return TCL_OK; -} - -int yosys_tcl_iterp_init(Tcl_Interp *interp) -{ - if (Tcl_Init(interp)!=TCL_OK) - log_warning("Tcl_Init() call failed - %s\n",Tcl_ErrnoMsg(Tcl_GetErrno())); - Tcl_CreateCommand(interp, "yosys", tcl_yosys_cmd, NULL, NULL); - return TCL_OK ; -} - -void yosys_tcl_activate_repl() -{ - yosys_tcl_repl_active = true; -} +// defined in tclapi.cc +extern int yosys_tcl_iterp_init(Tcl_Interp *interp); extern Tcl_Interp *yosys_get_tcl_interp() { diff --git a/kernel/yosys.h b/kernel/yosys.h index 5922d9ab752..d0359aea8c8 100644 --- a/kernel/yosys.h +++ b/kernel/yosys.h @@ -32,8 +32,6 @@ // // This header is very boring. It just defines some general things that // belong nowhere else and includes the interesting headers. -// -// Find more information in the "guidelines/GettingStarted" file. #ifndef YOSYS_H @@ -45,6 +43,10 @@ #include "kernel/rtlil.h" #include "kernel/register.h" +#ifdef YOSYS_ENABLE_TCL +struct Tcl_Interp; +#endif + YOSYS_NAMESPACE_BEGIN void yosys_setup(); diff --git a/kernel/yosys_common.h b/kernel/yosys_common.h index b0fbc3f1969..29dcd58c55a 100644 --- a/kernel/yosys_common.h +++ b/kernel/yosys_common.h @@ -65,29 +65,6 @@ #define FRIEND_TEST(test_case_name, test_name) \ friend class test_case_name##_##test_name##_Test -#ifdef YOSYS_ENABLE_TCL -# include -# ifdef YOSYS_MXE_HACKS -extern Tcl_Command Tcl_CreateCommand(Tcl_Interp *interp, const char *cmdName, Tcl_CmdProc *proc, ClientData clientData, Tcl_CmdDeleteProc *deleteProc); -extern Tcl_Interp *Tcl_CreateInterp(void); -extern void Tcl_Preserve(ClientData data); -extern void Tcl_Release(ClientData clientData); -extern int Tcl_InterpDeleted(Tcl_Interp *interp); -extern void Tcl_DeleteInterp(Tcl_Interp *interp); -extern int Tcl_Eval(Tcl_Interp *interp, const char *script); -extern int Tcl_EvalFile(Tcl_Interp *interp, const char *fileName); -extern void Tcl_Finalize(void); -extern int Tcl_GetCommandInfo(Tcl_Interp *interp, const char *cmdName, Tcl_CmdInfo *infoPtr); -extern const char *Tcl_GetStringResult(Tcl_Interp *interp); -extern Tcl_Obj *Tcl_NewStringObj(const char *bytes, int length); -extern Tcl_Obj *Tcl_NewIntObj(int intValue); -extern Tcl_Obj *Tcl_NewListObj(int objc, Tcl_Obj *const objv[]); -extern Tcl_Obj *Tcl_ObjSetVar2(Tcl_Interp *interp, Tcl_Obj *part1Ptr, Tcl_Obj *part2Ptr, Tcl_Obj *newValuePtr, int flags); -# endif -# undef CONST -# undef INLINE -#endif - #ifdef _WIN32 # undef NOMINMAX # define NOMINMAX 1 diff --git a/libs/ezsat/demo_cmp.cc b/libs/ezsat/demo_cmp.cc index 7b927c66402..28ddd98babe 100644 --- a/libs/ezsat/demo_cmp.cc +++ b/libs/ezsat/demo_cmp.cc @@ -19,6 +19,7 @@ #include "ezminisat.h" #include +#include #define INIT_X 123456789 #define INIT_Y 362436069 @@ -143,4 +144,3 @@ int main() } return 0; } - diff --git a/libs/ezsat/demo_vec.cc b/libs/ezsat/demo_vec.cc index a13430d5570..706529a5f4e 100644 --- a/libs/ezsat/demo_vec.cc +++ b/libs/ezsat/demo_vec.cc @@ -19,6 +19,7 @@ #include "ezminisat.h" #include +#include #define INIT_X 123456789 #define INIT_Y 362436069 @@ -109,4 +110,3 @@ int main() return 0; } - diff --git a/libs/fst/00_PATCH_win_io.patch b/libs/fst/00_PATCH_win_io.patch new file mode 100644 index 00000000000..a5bc989a8a4 --- /dev/null +++ b/libs/fst/00_PATCH_win_io.patch @@ -0,0 +1,58 @@ +--- fst_win_unistd.h ++++ fst_win_unistd.h +@@ -26,7 +26,7 @@ + #define WIN_UNISTD_H + + #include +-#ifdef _WIN64 ++#if defined(_MSC_VER) + #include + #else + #include +--- fstapi.cc ++++ fstapi.cc +@@ -56,7 +56,7 @@ + #include + #endif + +-#ifdef __MINGW32__ ++#if defined __CYGWIN__ || defined __MINGW32__ || defined _MSC_VER + #define WIN32_LEAN_AND_MEAN + #include + #endif +@@ -137,7 +137,7 @@ void **JenkinsIns(void *base_i, const unsigned char *mem, uint32_t length, uint3 + #include + #endif + +-#if defined(FST_MACOSX) || defined(__MINGW32__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) ++#if defined(FST_MACOSX) || defined(__MINGW32__) || defined(_MSC_VER) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) + #define FST_UNBUFFERED_IO + #endif + +@@ -159,7 +159,7 @@ void **JenkinsIns(void *base_i, const unsigned char *mem, uint32_t length, uint3 + /*** ***/ + /***********************/ + +-#ifdef __MINGW32__ ++#if defined(__MINGW32__) || defined(_MSC_VER) + #include + #ifndef HAVE_FSEEKO + #define ftello _ftelli64 +@@ -341,7 +341,7 @@ return(NULL); + /* + * mmap compatibility + */ +-#if defined __MINGW32__ ++#if defined __MINGW32__ || defined _MSC_VER + #include + #define fstMmap(__addr,__len,__prot,__flags,__fd,__off) fstMmap2((__len), (__fd), (__off)) + #define fstMunmap(__addr,__len) UnmapViewOfFile((LPCVOID)__addr) +@@ -4638,7 +4638,7 @@ if(sectype == FST_BL_ZWRAPPER) + #endif + + fstReaderFseeko(xc, xc->f, FST_ZWRAPPER_HDR_SIZE, SEEK_SET); +-#ifndef __MINGW32__ ++#if !defined(__MINGW32__) && !defined(_MSC_VER) + fflush(xc->f); + #else + /* Windows UCRT runtime library reads one byte ahead in the file diff --git a/libs/fst/00_PATCH_win_zlib.patch b/libs/fst/00_PATCH_win_zlib.patch new file mode 100644 index 00000000000..125e7cf58f2 --- /dev/null +++ b/libs/fst/00_PATCH_win_zlib.patch @@ -0,0 +1,16 @@ +--- fstapi.h ++++ fstapi.h +@@ -33,11 +33,12 @@ extern "C" { + #include + #include + #include +-#include + #include + #if defined(_MSC_VER) ++ #include "libs/zlib/zlib.h" + #include "fst_win_unistd.h" + #else ++ #include + #include + #endif + #include diff --git a/libs/fst/00_UPDATE.sh b/libs/fst/00_UPDATE.sh new file mode 100755 index 00000000000..aef0e4fe85a --- /dev/null +++ b/libs/fst/00_UPDATE.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +mv config.h config.h.bak +rm -f *.txt *.cc *.h +git clone --depth 1 https://github.com/gtkwave/gtkwave fst_upstream +rm fst_upstream/lib/libfst/CMakeLists.txt +mv fst_upstream/lib/libfst/*.{h,c,txt} . +rm -rf fst_upstream + +for src in *.c; do + mv -- "$src" "${src%.c}.cc" +done +mv config.h.bak config.h + +sed -i -e 's,,"config.h",' *.cc *.h +sed -i -e 's,"fastlz.c","fastlz.cc",' *.cc *.h + +patch -p0 < 00_PATCH_win_zlib.patch +patch -p0 < 00_PATCH_win_io.patch diff --git a/libs/fst/fastlz.cc b/libs/fst/fastlz.cc index 68bda3346b3..3272ca7a8a4 100644 --- a/libs/fst/fastlz.cc +++ b/libs/fst/fastlz.cc @@ -36,15 +36,16 @@ */ #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)) +#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) +#define FASTLZ_EXPECT_CONDITIONAL(c) (c) +#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c) #endif /* @@ -63,7 +64,7 @@ */ #if !defined(FASTLZ_STRICT_ALIGN) #define FASTLZ_STRICT_ALIGN -#if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */ +#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 @@ -79,29 +80,24 @@ #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); +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_COPY 32 +#define MAX_LEN 264 /* 256 + 8 */ #define MAX_DISTANCE 8192 #if !defined(FASTLZ_STRICT_ALIGN) -#define FASTLZ_READU16(p) *((const flzuint16 *)(p)) +#define FASTLZ_READU16(p) *((const flzuint16*)(p)) #else -#define FASTLZ_READU16(p) ((p)[0] | (p)[1] << 8) +#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; \ - } +#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 @@ -110,8 +106,8 @@ int fastlz_decompress(const void *input, int length, void *output, int maxout); #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); +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.cc" #undef FASTLZ_LEVEL @@ -119,410 +115,435 @@ static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void #undef MAX_DISTANCE #define MAX_DISTANCE 8191 -#define MAX_FARDISTANCE (65535 + MAX_DISTANCE - 1) +#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); +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.cc" -int fastlz_compress(const void *input, int length, void *output) +int fastlz_compress(const void* input, int length, void* output) { - /* for short block, choose fastlz1 */ - if (length < 65536) - return fastlz1_compress(input, length, output); + /* for short block, choose fastlz1 */ + if(length < 65536) + return fastlz1_compress(input, length, output); - /* else... */ - return fastlz2_compress(input, length, output); + /* else... */ + return fastlz2_compress(input, length, output); } -int fastlz_decompress(const void *input, int length, void *output, int maxout) +int fastlz_decompress(const void* input, int length, void* output, int maxout) { - /* magic identifier for compression level */ - int level = ((*(const flzuint8 *)input) >> 5) + 1; + /* 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); + 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; + /* unknown level, trigger error */ + return 0; } -int fastlz_compress_level(int level, const void *input, int length, void *output) +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); + if(level == 1) + return fastlz1_compress(input, length, output); + if(level == 2) + return fastlz2_compress(input, length, output); - return 0; + return 0; } #else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ -static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void *input, int length, void *output) +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; + 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; } - - /* 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]; + /* find potential match */ + HASH_FUNCTION(hval,ip); + hslot = htab + hval; + ref = htab[hval]; - /* calculate distance to the match */ - distance = anchor - ref; + /* calculate distance to the match */ + distance = anchor - ref; - /* update hash table */ - *hslot = anchor; + /* update hash table */ + *hslot = anchor; - /* is this a match? check the first 3 bytes */ - if (distance == 0 || -#if FASTLZ_LEVEL == 1 - (distance >= MAX_DISTANCE) || + /* is this a match? check the first 3 bytes */ + if(distance==0 || +#if FASTLZ_LEVEL==1 + (distance >= MAX_DISTANCE) || #else - (distance >= MAX_FARDISTANCE) || + (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; - } + *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; + /* last matched byte */ + ip = anchor + len; - /* 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; - } - } + /* 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); - } + 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++; + /* 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; + /* assuming literal copy */ + *op++ = MAX_COPY-1; - continue; + 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; - } + *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 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); +#if FASTLZ_LEVEL==2 + /* marker for fastlz2 */ + *(flzuint8*)output |= (1 << 5); #endif - return op - (flzuint8 *)output; + return op - (flzuint8*)output; } -static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void *input, int length, void *output, int maxout) +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; + 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++; + 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; - } + 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(op + len + 3 > op_limit)) + return 0; - if (FASTLZ_UNEXPECT_CONDITIONAL(ref - 1 < (flzuint8 *)output)) - 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(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; + const flzuint16* p; + flzuint16* q; #endif - /* copy from reference */ - ref--; - *op++ = *ref++; - *op++ = *ref++; - *op++ = *ref++; + /* 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++; + /* 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++; + for(; len; --len) + *op++ = *ref++; #endif - } - } else { - ctrl++; + } + } + 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; + 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++; + *op++ = *ip++; + for(--ctrl; ctrl; ctrl--) + *op++ = *ip++; - loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); - if (loop) - ctrl = *ip++; - } - } while (FASTLZ_EXPECT_CONDITIONAL(loop)); + loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); + if(loop) + ctrl = *ip++; + } + } + while(FASTLZ_EXPECT_CONDITIONAL(loop)); - return op - (flzuint8 *)output; + return op - (flzuint8*)output; } #endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */ diff --git a/libs/fst/fst_win_unistd.h b/libs/fst/fst_win_unistd.h new file mode 100644 index 00000000000..089f0180b3b --- /dev/null +++ b/libs/fst/fst_win_unistd.h @@ -0,0 +1,52 @@ +/* + * 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 +#if defined(_MSC_VER) +#include +#else +#include +#endif + +#include + +#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 diff --git a/libs/fst/fstapi.cc b/libs/fst/fstapi.cc index bd70d725005..a4329cf322f 100644 --- a/libs/fst/fstapi.cc +++ b/libs/fst/fstapi.cc @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2018 Tony Bybell. + * Copyright (c) 2009-2023 Tony Bybell. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -34,13 +34,12 @@ * FST_DEBUG : not for production use, only enable for development * FST_REMOVE_DUPLICATE_VC : glitch removal (has writer performance impact) * HAVE_LIBPTHREAD -> FST_WRITER_PARALLEL : enables inclusion of parallel writer code - * FST_DO_MISALIGNED_OPS (defined automatically for x86 and some others) : CPU architecture can handle misaligned - * loads/stores _WAVE_HAVE_JUDY : use Judy arrays instead of Jenkins (undefine if LGPL is not acceptable) + * _WAVE_HAVE_JUDY : use Judy arrays instead of Jenkins (undefine if LGPL is not acceptable) * */ #ifndef FST_CONFIG_INCLUDE -#define FST_CONFIG_INCLUDE "config.h" +# define FST_CONFIG_INCLUDE "config.h" #endif #include FST_CONFIG_INCLUDE @@ -57,7 +56,8 @@ #include #endif -#ifdef __MINGW32__ +#if defined __CYGWIN__ || defined __MINGW32__ || defined _MSC_VER +#define WIN32_LEAN_AND_MEAN #include #endif @@ -93,51 +93,54 @@ typedef off_t fst_off_t; #include #else /* should be more than enough for fstWriterSetSourceStem() */ -#define FST_PATH_HASHMASK ((1UL << 16) - 1) +#define FST_PATH_HASHMASK ((1UL << 16) - 1) typedef const void *Pcvoid_t; typedef void *Pvoid_t; typedef void **PPvoid_t; -#define JudyHSIns(a, b, c, d) JenkinsIns((a), (b), (c), (hashmask)) -#define JudyHSFreeArray(a, b) JenkinsFree((a), (hashmask)) +#define JudyHSIns(a,b,c,d) JenkinsIns((a),(b),(c),(hashmask)) +#define JudyHSFreeArray(a,b) JenkinsFree((a),(hashmask)) void JenkinsFree(void *base_i, uint32_t hashmask); void **JenkinsIns(void *base_i, const unsigned char *mem, uint32_t length, uint32_t hashmask); #endif + #ifndef FST_WRITEX_DISABLE -#define FST_WRITEX_MAX (64 * 1024) +#define FST_WRITEX_MAX (64 * 1024) #else -#define fstWritex(a, b, c) fstFwrite((b), (c), 1, fv) +#define fstWritex(a,b,c) fstFwrite((b), (c), 1, fv) #endif + /* these defines have a large impact on writer speed when a model has a */ /* huge number of symbols. as a default, use 128MB and increment when */ /* every 1M signals are defined. */ -#define FST_BREAK_SIZE (1UL << 27) -#define FST_BREAK_ADD_SIZE (1UL << 22) -#define FST_BREAK_SIZE_MAX (1UL << 31) -#define FST_ACTIVATE_HUGE_BREAK (1000000) -#define FST_ACTIVATE_HUGE_INC (1000000) - -#define FST_WRITER_STR "fstWriter" -#define FST_ID_NAM_SIZ (512) -#define FST_ID_NAM_ATTR_SIZ (65536 + 4096) -#define FST_DOUBLE_ENDTEST (2.7182818284590452354) -#define FST_HDR_SIM_VERSION_SIZE (128) -#define FST_HDR_DATE_SIZE (119) -#define FST_HDR_FILETYPE_SIZE (1) -#define FST_HDR_TIMEZERO_SIZE (8) -#define FST_GZIO_LEN (32768) -#define FST_HDR_FOURPACK_DUO_SIZE (4 * 1024 * 1024) - -#if defined(__i386__) || defined(__x86_64__) || defined(_AIX) -#define FST_DO_MISALIGNED_OPS -#endif +#define FST_BREAK_SIZE (1UL << 27) +#define FST_BREAK_ADD_SIZE (1UL << 22) +#define FST_BREAK_SIZE_MAX (1UL << 31) +#define FST_ACTIVATE_HUGE_BREAK (1000000) +#define FST_ACTIVATE_HUGE_INC (1000000) + +#define FST_WRITER_STR "fstWriter" +#define FST_ID_NAM_SIZ (512) +#define FST_ID_NAM_ATTR_SIZ (65536+4096) +#define FST_DOUBLE_ENDTEST (2.7182818284590452354) +#define FST_HDR_SIM_VERSION_SIZE (128) +#define FST_HDR_DATE_SIZE (119) +#define FST_HDR_FILETYPE_SIZE (1) +#define FST_HDR_TIMEZERO_SIZE (8) +#define FST_GZIO_LEN (32768) +#define FST_HDR_FOURPACK_DUO_SIZE (4*1024*1024) +#define FST_ZWRAPPER_HDR_SIZE (1+8+8) #if defined(__APPLE__) && defined(__MACH__) #define FST_MACOSX #include #endif +#if defined(FST_MACOSX) || defined(__MINGW32__) || defined(_MSC_VER) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) +#define FST_UNBUFFERED_IO +#endif + #ifdef __GNUC__ /* Boolean expression more often true than false */ #define FST_LIKELY(x) __builtin_expect(!!(x), 1) @@ -172,108 +175,137 @@ void **JenkinsIns(void *base_i, const unsigned char *mem, uint32_t length, uint3 * this is currently not implemented so that the branchless decode is: * uint32_t shcnt = 2 << (vli & 1); tdelta = vli >> shcnt; */ -#define FST_RCV_X (1 | (0 << 1)) -#define FST_RCV_Z (1 | (1 << 1)) -#define FST_RCV_H (1 | (2 << 1)) -#define FST_RCV_U (1 | (3 << 1)) -#define FST_RCV_W (1 | (4 << 1)) -#define FST_RCV_L (1 | (5 << 1)) -#define FST_RCV_D (1 | (6 << 1)) -#define FST_RCV_Q (1 | (7 << 1)) +#define FST_RCV_X (1 | (0<<1)) +#define FST_RCV_Z (1 | (1<<1)) +#define FST_RCV_H (1 | (2<<1)) +#define FST_RCV_U (1 | (3<<1)) +#define FST_RCV_W (1 | (4<<1)) +#define FST_RCV_L (1 | (5<<1)) +#define FST_RCV_D (1 | (6<<1)) +#define FST_RCV_Q (1 | (7<<1)) #define FST_RCV_STR "xzhuwl-?" /* 01234567 */ + +/* + * report abort messages + */ +static void chk_report_abort(const char *s) +{ +fprintf(stderr,"Triggered %s security check, exiting.\n", s); +abort(); +} + + /* * prevent old file overwrite when currently being read */ static FILE *unlink_fopen(const char *nam, const char *mode) { - unlink(nam); - return (fopen(nam, mode)); +unlink(nam); +return(fopen(nam, mode)); } + /* * system-specific temp file handling */ #ifdef __MINGW32__ -static FILE *tmpfile_open(char **nam) +static FILE* tmpfile_open(char **nam) { - char *fname = NULL; - TCHAR szTempFileName[MAX_PATH]; - TCHAR lpTempPathBuffer[MAX_PATH]; - DWORD dwRetVal = 0; - UINT uRetVal = 0; - FILE *fh = NULL; +char *fname = NULL; +TCHAR szTempFileName[MAX_PATH]; +TCHAR lpTempPathBuffer[MAX_PATH]; +DWORD dwRetVal = 0; +UINT uRetVal = 0; +FILE *fh = NULL; - if (nam) /* cppcheck warning fix: nam is always defined, so this is not needed */ - { +if(nam) /* cppcheck warning fix: nam is always defined, so this is not needed */ + { dwRetVal = GetTempPath(MAX_PATH, lpTempPathBuffer); - if ((dwRetVal > MAX_PATH) || (dwRetVal == 0)) { - fprintf(stderr, FST_APIMESS "GetTempPath() failed in " __FILE__ " line %d, exiting.\n", __LINE__); - exit(255); - } else { - uRetVal = GetTempFileName(lpTempPathBuffer, TEXT("FSTW"), 0, szTempFileName); - if (uRetVal == 0) { - fprintf(stderr, FST_APIMESS "GetTempFileName() failed in " __FILE__ " line %d, exiting.\n", __LINE__); + if((dwRetVal > MAX_PATH) || (dwRetVal == 0)) + { + fprintf(stderr, FST_APIMESS "GetTempPath() failed in " __FILE__ " line %d, exiting.\n", __LINE__); exit(255); - } else { - fname = strdup(szTempFileName); - } - } + } + else + { + uRetVal = GetTempFileName(lpTempPathBuffer, TEXT("FSTW"), 0, szTempFileName); + if (uRetVal == 0) + { + fprintf(stderr, FST_APIMESS "GetTempFileName() failed in " __FILE__ " line %d, exiting.\n", __LINE__); + exit(255); + } + else + { + fname = strdup(szTempFileName); + } + } - if (fname) { - *nam = fname; - fh = unlink_fopen(fname, "w+b"); + if(fname) + { + *nam = fname; + fh = unlink_fopen(fname, "w+b"); + } } - } - return (fh); +return(fh); } #else -static FILE *tmpfile_open(char **nam) +static FILE* tmpfile_open(char **nam) { - FILE *f = tmpfile(); /* replace with mkstemp() + fopen(), etc if this is not good enough */ - if (nam) { - *nam = NULL; - } - return (f); +FILE *f = tmpfile(); /* replace with mkstemp() + fopen(), etc if this is not good enough */ +if(nam) { *nam = NULL; } +return(f); } #endif + static void tmpfile_close(FILE **f, char **nam) { - if (f) { - if (*f) { - fclose(*f); - *f = NULL; +if(f) + { + if(*f) { fclose(*f); *f = NULL; } } - } - if (nam) { - if (*nam) { - unlink(*nam); - free(*nam); - *nam = NULL; +if(nam) + { + if(*nam) + { + unlink(*nam); + free(*nam); + *nam = NULL; + } } - } } /*****************************************/ + /* * to remove warn_unused_result compile time messages * (in the future there needs to be results checking) */ -static size_t fstFread(void *buf, size_t siz, size_t cnt, FILE *fp) { return (fread(buf, siz, cnt, fp)); } +static size_t fstFread(void *buf, size_t siz, size_t cnt, FILE *fp) +{ +return(fread(buf, siz, cnt, fp)); +} -static size_t fstFwrite(const void *buf, size_t siz, size_t cnt, FILE *fp) { return (fwrite(buf, siz, cnt, fp)); } +static size_t fstFwrite(const void *buf, size_t siz, size_t cnt, FILE *fp) +{ +return(fwrite(buf, siz, cnt, fp)); +} + +static int fstFtruncate(int fd, fst_off_t length) +{ +return(ftruncate(fd, length)); +} -static int fstFtruncate(int fd, fst_off_t length) { return (ftruncate(fd, length)); } /* * realpath compatibility @@ -282,49 +314,50 @@ static char *fstRealpath(const char *path, char *resolved_path) { #if defined __USE_BSD || defined __USE_XOPEN_EXTENDED || defined __CYGWIN__ || defined HAVE_REALPATH #if (defined(__MACH__) && defined(__APPLE__)) - if (!resolved_path) { - resolved_path = (char *)malloc(PATH_MAX + 1); /* fixes bug on Leopard when resolved_path == NULL */ - } +if(!resolved_path) + { + resolved_path = (char *)malloc(PATH_MAX+1); /* fixes bug on Leopard when resolved_path == NULL */ + } #endif - return (realpath(path, resolved_path)); +return(realpath(path, resolved_path)); #else #ifdef __MINGW32__ - if (!resolved_path) { - resolved_path = (char *)malloc(PATH_MAX + 1); - } - return (_fullpath(resolved_path, path, PATH_MAX)); +if(!resolved_path) + { + resolved_path = (char *)malloc(PATH_MAX+1); + } +return(_fullpath(resolved_path, path, PATH_MAX)); #else - (void)path; - (void)resolved_path; - return (NULL); +(void)path; +(void)resolved_path; +return(NULL); #endif #endif } + /* * mmap compatibility */ -#if defined __CYGWIN__ || defined __MINGW32__ || defined _MSC_VER +#if defined __MINGW32__ || defined _MSC_VER #include -#define fstMmap(__addr, __len, __prot, __flags, __fd, __off) fstMmap2((__len), (__fd), (__off)) -#define fstMunmap(__addr, __len) free(__addr) +#define fstMmap(__addr,__len,__prot,__flags,__fd,__off) fstMmap2((__len), (__fd), (__off)) +#define fstMunmap(__addr,__len) UnmapViewOfFile((LPCVOID)__addr) static void *fstMmap2(size_t __len, int __fd, fst_off_t __off) { - (void)__off; - - unsigned char *pnt = (unsigned char *)malloc(__len); - fst_off_t cur_offs = lseek(__fd, 0, SEEK_CUR); - size_t i; +DWORD64 len64 = __len; /* Must be 64-bit for shift below */ +HANDLE handle = CreateFileMapping((HANDLE)_get_osfhandle(__fd), NULL, + PAGE_READWRITE, (DWORD)(len64 >> 32), + (DWORD)__len, NULL); +if (!handle) { return NULL; } - lseek(__fd, 0, SEEK_SET); - for (i = 0; i < __len; i += SSIZE_MAX) { - read(__fd, pnt + i, ((__len - i) >= SSIZE_MAX) ? SSIZE_MAX : (__len - i)); - } - lseek(__fd, cur_offs, SEEK_SET); - return (pnt); +void *ptr = MapViewOfFileEx(handle, FILE_MAP_READ | FILE_MAP_WRITE, + 0, (DWORD)__off, (SIZE_T)__len, (LPVOID)NULL); +CloseHandle(handle); +return ptr; } #else #include @@ -333,324 +366,366 @@ static void *fstMmap2(size_t __len, int __fd, fst_off_t __off) #else #define FST_CADDR_T_CAST #endif -#define fstMmap(__addr, __len, __prot, __flags, __fd, __off) \ - (void *)mmap(FST_CADDR_T_CAST(__addr), (__len), (__prot), (__flags), (__fd), (__off)) -#define fstMunmap(__addr, __len) \ - { \ - if (__addr) \ - munmap(FST_CADDR_T_CAST(__addr), (__len)); \ - } +#define fstMmap(__addr,__len,__prot,__flags,__fd,__off) (void*)mmap(FST_CADDR_T_CAST (__addr),(__len),(__prot),(__flags),(__fd),(__off)) +#define fstMunmap(__addr,__len) { if(__addr) munmap(FST_CADDR_T_CAST (__addr),(__len)); } #endif + /* * regular and variable-length integer access functions */ -#ifdef FST_DO_MISALIGNED_OPS -#define fstGetUint32(x) (*(uint32_t *)(x)) -#else -static inline uint32_t fstGetUint32(unsigned char *mem) +static uint32_t fstGetUint32(unsigned char *mem) { - union { - uint8_t u8[sizeof(uint32_t)]; - uint32_t u32; - } u; +uint32_t u32; +unsigned char *buf = (unsigned char *)(&u32); - for (size_t i=0; i < sizeof(u.u8); i++) - u.u8[i] = mem[i]; +memcpy(buf, mem, sizeof(uint32_t)); - return u.u32; +return(*(uint32_t *)buf); } -#endif + static int fstWriterUint64(FILE *handle, uint64_t v) { - unsigned char buf[8]; - int i; +unsigned char buf[8]; +int i; - for (i = 7; i >= 0; i--) { +for(i=7;i>=0;i--) + { buf[i] = v & 0xff; v >>= 8; - } + } - fstFwrite(buf, 8, 1, handle); - return (8); +fstFwrite(buf, 8, 1, handle); +return(8); } + static uint64_t fstReaderUint64(FILE *f) { - uint64_t val = 0; - unsigned char buf[sizeof(uint64_t)]; - unsigned int i; +uint64_t val = 0; +unsigned char buf[sizeof(uint64_t)]; +unsigned int i; - fstFread(buf, sizeof(uint64_t), 1, f); - for (i = 0; i < sizeof(uint64_t); i++) { +fstFread(buf, sizeof(uint64_t), 1, f); +for(i=0;i> 7)) /* determine len to avoid temp buffer copying to cut down on load-hit-store */ - { +while((nxt = nxt>>7)) /* determine len to avoid temp buffer copying to cut down on load-hit-store */ + { cnt++; - } + } - pnt -= cnt; - spnt = pnt; - cnt--; +pnt -= cnt; +spnt = pnt; +cnt--; - for (i = 0; i < cnt; i++) /* now generate left to right as normal */ - { - nxt = v >> 7; +for(i=0;i>7; *(spnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *spnt = (unsigned char)v; + } +*spnt = (unsigned char)v; - return (pnt); +return(pnt); } + static unsigned char *fstCopyVarint64ToRight(unsigned char *pnt, uint64_t v) { - uint64_t nxt; +uint64_t nxt; - while ((nxt = v >> 7)) { +while((nxt = v>>7)) + { *(pnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *(pnt++) = (unsigned char)v; + } +*(pnt++) = (unsigned char)v; - return (pnt); +return(pnt); } + static uint64_t fstGetVarint64(unsigned char *mem, int *skiplen) { - unsigned char *mem_orig = mem; - uint64_t rc = 0; - while (*mem & 0x80) { +unsigned char *mem_orig = mem; +uint64_t rc = 0; +while(*mem & 0x80) + { mem++; - } + } - *skiplen = mem - mem_orig + 1; - for (;;) { +*skiplen = mem - mem_orig + 1; +for(;;) + { rc <<= 7; rc |= (uint64_t)(*mem & 0x7f); - if (mem == mem_orig) { - break; - } + if(mem == mem_orig) + { + break; + } mem--; - } + } - return (rc); +return(rc); } + static uint32_t fstReaderVarint32(FILE *f) { - unsigned char buf[5]; - unsigned char *mem = buf; - uint32_t rc = 0; - int ch; +const int chk_len_max = 5; /* TALOS-2023-1783 */ +int chk_len = chk_len_max; +unsigned char buf[chk_len_max]; +unsigned char *mem = buf; +uint32_t rc = 0; +int ch; - do { +do + { ch = fgetc(f); *(mem++) = ch; - } while (ch & 0x80); - mem--; + } while((ch & 0x80) && (--chk_len)); + +if(ch & 0x80) chk_report_abort("TALOS-2023-1783"); +mem--; - for (;;) { +for(;;) + { rc <<= 7; rc |= (uint32_t)(*mem & 0x7f); - if (mem == buf) { - break; - } + if(mem == buf) + { + break; + } mem--; - } + } - return (rc); +return(rc); } + static uint32_t fstReaderVarint32WithSkip(FILE *f, uint32_t *skiplen) { - unsigned char buf[5]; - unsigned char *mem = buf; - uint32_t rc = 0; - int ch; +const int chk_len_max = 5; /* TALOS-2023-1783 */ +int chk_len = chk_len_max; +unsigned char buf[chk_len_max]; +unsigned char *mem = buf; +uint32_t rc = 0; +int ch; - do { +do + { ch = fgetc(f); *(mem++) = ch; - } while (ch & 0x80); - *skiplen = mem - buf; - mem--; + } while((ch & 0x80) && (--chk_len)); - for (;;) { +if(ch & 0x80) chk_report_abort("TALOS-2023-1783"); +*skiplen = mem - buf; +mem--; + +for(;;) + { rc <<= 7; rc |= (uint32_t)(*mem & 0x7f); - if (mem == buf) { - break; - } + if(mem == buf) + { + break; + } mem--; - } + } - return (rc); +return(rc); } + static uint64_t fstReaderVarint64(FILE *f) { - unsigned char buf[16]; - unsigned char *mem = buf; - uint64_t rc = 0; - int ch; +const int chk_len_max = 16; /* TALOS-2023-1783 */ +int chk_len = chk_len_max; +unsigned char buf[chk_len_max]; +unsigned char *mem = buf; +uint64_t rc = 0; +int ch; - do { +do + { ch = fgetc(f); *(mem++) = ch; - } while (ch & 0x80); - mem--; + } while((ch & 0x80) && (--chk_len)); + +if(ch & 0x80) chk_report_abort("TALOS-2023-1783"); +mem--; - for (;;) { + +for(;;) + { rc <<= 7; rc |= (uint64_t)(*mem & 0x7f); - if (mem == buf) { - break; - } + if(mem == buf) + { + break; + } mem--; - } + } - return (rc); +return(rc); } + static int fstWriterVarint(FILE *handle, uint64_t v) { - uint64_t nxt; - unsigned char buf[10]; /* ceil(64/7) = 10 */ - unsigned char *pnt = buf; - int len; +uint64_t nxt; +unsigned char buf[10]; /* ceil(64/7) = 10 */ +unsigned char *pnt = buf; +int len; - while ((nxt = v >> 7)) { +while((nxt = v>>7)) + { *(pnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *(pnt++) = (unsigned char)v; + } +*(pnt++) = (unsigned char)v; - len = pnt - buf; - fstFwrite(buf, len, 1, handle); - return (len); +len = pnt-buf; +fstFwrite(buf, len, 1, handle); +return(len); } + /* signed integer read/write routines are currently unused */ static int64_t fstGetSVarint64(unsigned char *mem, int *skiplen) { - unsigned char *mem_orig = mem; - int64_t rc = 0; - const int64_t one = 1; - const int siz = sizeof(int64_t) * 8; - int shift = 0; - unsigned char byt; +unsigned char *mem_orig = mem; +int64_t rc = 0; +const int64_t one = 1; +const int siz = sizeof(int64_t) * 8; +int shift = 0; +unsigned char byt; - do { +do { byt = *(mem++); rc |= ((int64_t)(byt & 0x7f)) << shift; shift += 7; - } while (byt & 0x80); + } while(byt & 0x80); - if ((shift < siz) && (byt & 0x40)) { +if((shift>= 7; - if (((!v) && (!(byt & 0x40))) || ((v == -1) && (byt & 0x40))) { - more = 0; - byt &= 0x7f; - } + if (((!v) && (!(byt & 0x40))) || ((v == -1) && (byt & 0x40))) + { + more = 0; + byt &= 0x7f; + } *(pnt++) = byt; - } while (more); + } while(more); - len = pnt - buf; - fstFwrite(buf, len, 1, handle); - return (len); +len = pnt-buf; +fstFwrite(buf, len, 1, handle); +return(len); } #endif + /***********************/ /*** ***/ /*** writer function ***/ @@ -662,337 +737,330 @@ static int fstWriterSVarint(FILE *handle, int64_t v) */ struct fstBlackoutChain { - struct fstBlackoutChain *next; - uint64_t tim; - unsigned active : 1; +struct fstBlackoutChain *next; +uint64_t tim; +unsigned active : 1; }; + struct fstWriterContext { - FILE *handle; - FILE *hier_handle; - FILE *geom_handle; - FILE *valpos_handle; - FILE *curval_handle; - FILE *tchn_handle; +FILE *handle; +FILE *hier_handle; +FILE *geom_handle; +FILE *valpos_handle; +FILE *curval_handle; +FILE *tchn_handle; - unsigned char *vchg_mem; +unsigned char *vchg_mem; - fst_off_t hier_file_len; +fst_off_t hier_file_len; - uint32_t *valpos_mem; - unsigned char *curval_mem; +uint32_t *valpos_mem; +unsigned char *curval_mem; - unsigned char *outval_mem; /* for two-state / Verilator-style value changes */ - uint32_t outval_alloc_siz; +unsigned char *outval_mem; /* for two-state / Verilator-style value changes */ +uint32_t outval_alloc_siz; - char *filename; +char *filename; - fstHandle maxhandle; - fstHandle numsigs; - uint32_t maxvalpos; +fstHandle maxhandle; +fstHandle numsigs; +uint32_t maxvalpos; - unsigned vc_emitted : 1; - unsigned is_initial_time : 1; - unsigned fourpack : 1; - unsigned fastpack : 1; +unsigned vc_emitted : 1; +unsigned is_initial_time : 1; +unsigned fourpack : 1; +unsigned fastpack : 1; - int64_t timezero; - fst_off_t section_header_truncpos; - uint32_t tchn_cnt, tchn_idx; - uint64_t curtime; - uint64_t firsttime; - uint32_t vchg_siz; - uint32_t vchg_alloc_siz; +int64_t timezero; +fst_off_t section_header_truncpos; +uint32_t tchn_cnt, tchn_idx; +uint64_t curtime; +uint64_t firsttime; +uint32_t vchg_siz; +uint32_t vchg_alloc_siz; - uint32_t secnum; - fst_off_t section_start; +uint32_t secnum; +fst_off_t section_start; - uint32_t numscopes; - double nan; /* nan value for uninitialized doubles */ +uint32_t numscopes; +double nan; /* nan value for uninitialized doubles */ - struct fstBlackoutChain *blackout_head; - struct fstBlackoutChain *blackout_curr; - uint32_t num_blackouts; +struct fstBlackoutChain *blackout_head; +struct fstBlackoutChain *blackout_curr; +uint32_t num_blackouts; - uint64_t dump_size_limit; +uint64_t dump_size_limit; - unsigned char filetype; /* default is 0, FST_FT_VERILOG */ +unsigned char filetype; /* default is 0, FST_FT_VERILOG */ - unsigned compress_hier : 1; - unsigned repack_on_close : 1; - unsigned skip_writing_section_hdr : 1; - unsigned size_limit_locked : 1; - unsigned section_header_only : 1; - unsigned flush_context_pending : 1; - unsigned parallel_enabled : 1; - unsigned parallel_was_enabled : 1; +unsigned compress_hier : 1; +unsigned repack_on_close : 1; +unsigned skip_writing_section_hdr : 1; +unsigned size_limit_locked : 1; +unsigned section_header_only : 1; +unsigned flush_context_pending : 1; +unsigned parallel_enabled : 1; +unsigned parallel_was_enabled : 1; - /* should really be semaphores, but are bytes to cut down on read-modify-write window size */ - unsigned char already_in_flush; /* in case control-c handlers interrupt */ - unsigned char already_in_close; /* in case control-c handlers interrupt */ +/* should really be semaphores, but are bytes to cut down on read-modify-write window size */ +unsigned char already_in_flush; /* in case control-c handlers interrupt */ +unsigned char already_in_close; /* in case control-c handlers interrupt */ #ifdef FST_WRITER_PARALLEL - pthread_mutex_t mutex; - pthread_t thread; - pthread_attr_t thread_attr; - struct fstWriterContext *xc_parent; +pthread_mutex_t mutex; +pthread_t thread; +pthread_attr_t thread_attr; +struct fstWriterContext *xc_parent; #endif - unsigned in_pthread : 1; +unsigned in_pthread : 1; - size_t fst_orig_break_size; - size_t fst_orig_break_add_size; +size_t fst_orig_break_size; +size_t fst_orig_break_add_size; - size_t fst_break_size; - size_t fst_break_add_size; +size_t fst_break_size; +size_t fst_break_add_size; - size_t fst_huge_break_size; +size_t fst_huge_break_size; - fstHandle next_huge_break; +fstHandle next_huge_break; - Pvoid_t path_array; - uint32_t path_array_count; +Pvoid_t path_array; +uint32_t path_array_count; - unsigned fseek_failed : 1; +unsigned fseek_failed : 1; - char *geom_handle_nam; - char *valpos_handle_nam; - char *curval_handle_nam; - char *tchn_handle_nam; +char *geom_handle_nam; +char *valpos_handle_nam; +char *curval_handle_nam; +char *tchn_handle_nam; - fstEnumHandle max_enumhandle; +fstEnumHandle max_enumhandle; }; + static int fstWriterFseeko(struct fstWriterContext *xc, FILE *stream, fst_off_t offset, int whence) { - int rc = fseeko(stream, offset, whence); +int rc = fseeko(stream, offset, whence); - if (rc < 0) { +if(rc<0) + { xc->fseek_failed = 1; #ifdef FST_DEBUG fprintf(stderr, FST_APIMESS "Seek to #%" PRId64 " (whence = %d) failed!\n", offset, whence); perror("Why"); #endif - } + } - return (rc); +return(rc); } -static uint32_t fstWriterUint32WithVarint32(struct fstWriterContext *xc, uint32_t *u, uint32_t v, const void *dbuf, - uint32_t siz) + +static uint32_t fstWriterUint32WithVarint32(struct fstWriterContext *xc, uint32_t *u, uint32_t v, const void *dbuf, uint32_t siz) { - unsigned char *buf = xc->vchg_mem + xc->vchg_siz; - unsigned char *pnt = buf; - uint32_t nxt; - uint32_t len; +unsigned char *buf = xc->vchg_mem + xc->vchg_siz; +unsigned char *pnt = buf; +uint32_t nxt; +uint32_t len; -#ifdef FST_DO_MISALIGNED_OPS - (*(uint32_t *)(pnt)) = (*(uint32_t *)(u)); -#else - memcpy(pnt, u, sizeof(uint32_t)); -#endif - pnt += 4; +memcpy(pnt, u, sizeof(uint32_t)); +pnt += 4; - while ((nxt = v >> 7)) { +while((nxt = v>>7)) + { *(pnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *(pnt++) = (unsigned char)v; - memcpy(pnt, dbuf, siz); + } +*(pnt++) = (unsigned char)v; +memcpy(pnt, dbuf, siz); - len = pnt - buf + siz; - return (len); +len = pnt-buf + siz; +return(len); } -static uint32_t fstWriterUint32WithVarint32AndLength(struct fstWriterContext *xc, uint32_t *u, uint32_t v, - const void *dbuf, uint32_t siz) + +static uint32_t fstWriterUint32WithVarint32AndLength(struct fstWriterContext *xc, uint32_t *u, uint32_t v, const void *dbuf, uint32_t siz) { - unsigned char *buf = xc->vchg_mem + xc->vchg_siz; - unsigned char *pnt = buf; - uint32_t nxt; - uint32_t len; +unsigned char *buf = xc->vchg_mem + xc->vchg_siz; +unsigned char *pnt = buf; +uint32_t nxt; +uint32_t len; -#ifdef FST_DO_MISALIGNED_OPS - (*(uint32_t *)(pnt)) = (*(uint32_t *)(u)); -#else - memcpy(pnt, u, sizeof(uint32_t)); -#endif - pnt += 4; +memcpy(pnt, u, sizeof(uint32_t)); +pnt += 4; - while ((nxt = v >> 7)) { +while((nxt = v>>7)) + { *(pnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *(pnt++) = (unsigned char)v; + } +*(pnt++) = (unsigned char)v; - v = siz; - while ((nxt = v >> 7)) { +v = siz; +while((nxt = v>>7)) + { *(pnt++) = ((unsigned char)v) | 0x80; v = nxt; - } - *(pnt++) = (unsigned char)v; + } +*(pnt++) = (unsigned char)v; - memcpy(pnt, dbuf, siz); +memcpy(pnt, dbuf, siz); - len = pnt - buf + siz; - return (len); +len = pnt-buf + siz; +return(len); } + /* * header bytes, write here so defines are set up before anything else * that needs to use them */ static void fstWriterEmitHdrBytes(struct fstWriterContext *xc) { - char vbuf[FST_HDR_SIM_VERSION_SIZE]; - char dbuf[FST_HDR_DATE_SIZE]; - double endtest = FST_DOUBLE_ENDTEST; - time_t walltime; +char vbuf[FST_HDR_SIM_VERSION_SIZE]; +char dbuf[FST_HDR_DATE_SIZE]; +double endtest = FST_DOUBLE_ENDTEST; +time_t walltime; -#define FST_HDR_OFFS_TAG (0) - fputc(FST_BL_HDR, xc->handle); /* +0 tag */ +#define FST_HDR_OFFS_TAG (0) +fputc(FST_BL_HDR, xc->handle); /* +0 tag */ -#define FST_HDR_OFFS_SECLEN (FST_HDR_OFFS_TAG + 1) - fstWriterUint64(xc->handle, 329); /* +1 section length */ +#define FST_HDR_OFFS_SECLEN (FST_HDR_OFFS_TAG + 1) +fstWriterUint64(xc->handle, 329); /* +1 section length */ -#define FST_HDR_OFFS_START_TIME (FST_HDR_OFFS_SECLEN + 8) - fstWriterUint64(xc->handle, 0); /* +9 start time */ +#define FST_HDR_OFFS_START_TIME (FST_HDR_OFFS_SECLEN + 8) +fstWriterUint64(xc->handle, 0); /* +9 start time */ -#define FST_HDR_OFFS_END_TIME (FST_HDR_OFFS_START_TIME + 8) - fstWriterUint64(xc->handle, 0); /* +17 end time */ +#define FST_HDR_OFFS_END_TIME (FST_HDR_OFFS_START_TIME + 8) +fstWriterUint64(xc->handle, 0); /* +17 end time */ -#define FST_HDR_OFFS_ENDIAN_TEST (FST_HDR_OFFS_END_TIME + 8) - fstFwrite(&endtest, 8, 1, xc->handle); /* +25 endian test for reals */ +#define FST_HDR_OFFS_ENDIAN_TEST (FST_HDR_OFFS_END_TIME + 8) +fstFwrite(&endtest, 8, 1, xc->handle); /* +25 endian test for reals */ -#define FST_HDR_OFFS_MEM_USED (FST_HDR_OFFS_ENDIAN_TEST + 8) - fstWriterUint64(xc->handle, xc->fst_break_size); /* +33 memory used by writer */ +#define FST_HDR_OFFS_MEM_USED (FST_HDR_OFFS_ENDIAN_TEST + 8) +fstWriterUint64(xc->handle, xc->fst_break_size);/* +33 memory used by writer */ -#define FST_HDR_OFFS_NUM_SCOPES (FST_HDR_OFFS_MEM_USED + 8) - fstWriterUint64(xc->handle, 0); /* +41 scope creation count */ +#define FST_HDR_OFFS_NUM_SCOPES (FST_HDR_OFFS_MEM_USED + 8) +fstWriterUint64(xc->handle, 0); /* +41 scope creation count */ -#define FST_HDR_OFFS_NUM_VARS (FST_HDR_OFFS_NUM_SCOPES + 8) - fstWriterUint64(xc->handle, 0); /* +49 var creation count */ +#define FST_HDR_OFFS_NUM_VARS (FST_HDR_OFFS_NUM_SCOPES + 8) +fstWriterUint64(xc->handle, 0); /* +49 var creation count */ -#define FST_HDR_OFFS_MAXHANDLE (FST_HDR_OFFS_NUM_VARS + 8) - fstWriterUint64(xc->handle, 0); /* +57 max var idcode */ +#define FST_HDR_OFFS_MAXHANDLE (FST_HDR_OFFS_NUM_VARS + 8) +fstWriterUint64(xc->handle, 0); /* +57 max var idcode */ -#define FST_HDR_OFFS_SECTION_CNT (FST_HDR_OFFS_MAXHANDLE + 8) - fstWriterUint64(xc->handle, 0); /* +65 vc section count */ +#define FST_HDR_OFFS_SECTION_CNT (FST_HDR_OFFS_MAXHANDLE + 8) +fstWriterUint64(xc->handle, 0); /* +65 vc section count */ -#define FST_HDR_OFFS_TIMESCALE (FST_HDR_OFFS_SECTION_CNT + 8) - fputc((-9) & 255, xc->handle); /* +73 timescale 1ns */ +#define FST_HDR_OFFS_TIMESCALE (FST_HDR_OFFS_SECTION_CNT + 8) +fputc((-9)&255, xc->handle); /* +73 timescale 1ns */ -#define FST_HDR_OFFS_SIM_VERSION (FST_HDR_OFFS_TIMESCALE + 1) - memset(vbuf, 0, FST_HDR_SIM_VERSION_SIZE); - strcpy(vbuf, FST_WRITER_STR); - fstFwrite(vbuf, FST_HDR_SIM_VERSION_SIZE, 1, xc->handle); /* +74 version */ +#define FST_HDR_OFFS_SIM_VERSION (FST_HDR_OFFS_TIMESCALE + 1) +memset(vbuf, 0, FST_HDR_SIM_VERSION_SIZE); +strcpy(vbuf, FST_WRITER_STR); +fstFwrite(vbuf, FST_HDR_SIM_VERSION_SIZE, 1, xc->handle); /* +74 version */ -#define FST_HDR_OFFS_DATE (FST_HDR_OFFS_SIM_VERSION + FST_HDR_SIM_VERSION_SIZE) - memset(dbuf, 0, FST_HDR_DATE_SIZE); - time(&walltime); - strcpy(dbuf, asctime(localtime(&walltime))); - fstFwrite(dbuf, FST_HDR_DATE_SIZE, 1, xc->handle); /* +202 date */ +#define FST_HDR_OFFS_DATE (FST_HDR_OFFS_SIM_VERSION + FST_HDR_SIM_VERSION_SIZE) +memset(dbuf, 0, FST_HDR_DATE_SIZE); +time(&walltime); +strcpy(dbuf, asctime(localtime(&walltime))); +fstFwrite(dbuf, FST_HDR_DATE_SIZE, 1, xc->handle); /* +202 date */ - /* date size is deliberately overspecified at 119 bytes (originally 128) in order to provide backfill for new args - */ +/* date size is deliberately overspecified at 119 bytes (originally 128) in order to provide backfill for new args */ -#define FST_HDR_OFFS_FILETYPE (FST_HDR_OFFS_DATE + FST_HDR_DATE_SIZE) - fputc(xc->filetype, xc->handle); /* +321 filetype */ +#define FST_HDR_OFFS_FILETYPE (FST_HDR_OFFS_DATE + FST_HDR_DATE_SIZE) +fputc(xc->filetype, xc->handle); /* +321 filetype */ -#define FST_HDR_OFFS_TIMEZERO (FST_HDR_OFFS_FILETYPE + FST_HDR_FILETYPE_SIZE) - fstWriterUint64(xc->handle, xc->timezero); /* +322 timezero */ +#define FST_HDR_OFFS_TIMEZERO (FST_HDR_OFFS_FILETYPE + FST_HDR_FILETYPE_SIZE) +fstWriterUint64(xc->handle, xc->timezero); /* +322 timezero */ -#define FST_HDR_LENGTH (FST_HDR_OFFS_TIMEZERO + FST_HDR_TIMEZERO_SIZE) - /* +330 next section starts here */ - fflush(xc->handle); +#define FST_HDR_LENGTH (FST_HDR_OFFS_TIMEZERO + FST_HDR_TIMEZERO_SIZE) + /* +330 next section starts here */ +fflush(xc->handle); } + /* * mmap functions */ static void fstWriterMmapSanity(void *pnt, const char *file, int line, const char *usage) { -#if !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(_MSC_VER) - if (pnt == MAP_FAILED) { - fprintf(stderr, "fstMmap() assigned to %s failed: errno: %d, file %s, line %d.\n", usage, errno, file, line); - perror("Why"); - pnt = NULL; - } +if(pnt == NULL +#ifdef MAP_FAILED + || pnt == MAP_FAILED +#endif + ) + { + fprintf(stderr, "fstMmap() assigned to %s failed: errno: %d, file %s, line %d.\n", usage, errno, file, line); +#if !defined(__MINGW32__) + perror("Why"); +#else + LPSTR mbuf = NULL; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)&mbuf, 0, NULL); + fprintf(stderr, "%s", mbuf); + LocalFree(mbuf); #endif + pnt = NULL; + } } + static void fstWriterCreateMmaps(struct fstWriterContext *xc) { - fst_off_t curpos = ftello(xc->handle); +fst_off_t curpos = ftello(xc->handle); - fflush(xc->hier_handle); +fflush(xc->hier_handle); - /* write out intermediate header */ - fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_START_TIME, SEEK_SET); - fstWriterUint64(xc->handle, xc->firsttime); - fstWriterUint64(xc->handle, xc->curtime); - fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_NUM_SCOPES, SEEK_SET); - fstWriterUint64(xc->handle, xc->numscopes); - fstWriterUint64(xc->handle, xc->numsigs); - fstWriterUint64(xc->handle, xc->maxhandle); - fstWriterUint64(xc->handle, xc->secnum); - fstWriterFseeko(xc, xc->handle, curpos, SEEK_SET); - fflush(xc->handle); +/* write out intermediate header */ +fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_START_TIME, SEEK_SET); +fstWriterUint64(xc->handle, xc->firsttime); +fstWriterUint64(xc->handle, xc->curtime); +fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_NUM_SCOPES, SEEK_SET); +fstWriterUint64(xc->handle, xc->numscopes); +fstWriterUint64(xc->handle, xc->numsigs); +fstWriterUint64(xc->handle, xc->maxhandle); +fstWriterUint64(xc->handle, xc->secnum); +fstWriterFseeko(xc, xc->handle, curpos, SEEK_SET); +fflush(xc->handle); - /* do mappings */ - if (!xc->valpos_mem) { +/* do mappings */ +if(!xc->valpos_mem) + { fflush(xc->valpos_handle); - errno = 0; - if (xc->maxhandle) { - fstWriterMmapSanity(xc->valpos_mem = (uint32_t *)fstMmap(NULL, xc->maxhandle * 4 * sizeof(uint32_t), - PROT_READ | PROT_WRITE, MAP_SHARED, - fileno(xc->valpos_handle), 0), - __FILE__, __LINE__, "xc->valpos_mem"); - } - } - if (!xc->curval_mem) { + errno = 0; + if(xc->maxhandle) + { + fstWriterMmapSanity(xc->valpos_mem = (uint32_t *)fstMmap(NULL, xc->maxhandle * 4 * sizeof(uint32_t), PROT_READ|PROT_WRITE, MAP_SHARED, fileno(xc->valpos_handle), 0), __FILE__, __LINE__, "xc->valpos_mem"); + } + } +if(!xc->curval_mem) + { fflush(xc->curval_handle); - errno = 0; - if (xc->maxvalpos) { - fstWriterMmapSanity(xc->curval_mem = (unsigned char *)fstMmap(NULL, xc->maxvalpos, PROT_READ | PROT_WRITE, - MAP_SHARED, fileno(xc->curval_handle), 0), - __FILE__, __LINE__, "xc->curval_handle"); + errno = 0; + if(xc->maxvalpos) + { + fstWriterMmapSanity(xc->curval_mem = (unsigned char *)fstMmap(NULL, xc->maxvalpos, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(xc->curval_handle), 0), __FILE__, __LINE__, "xc->curval_handle"); + } } - } } + static void fstDestroyMmaps(struct fstWriterContext *xc, int is_closing) { -#if !defined __CYGWIN__ && !defined __MINGW32__ - (void)is_closing; -#endif - - fstMunmap(xc->valpos_mem, xc->maxhandle * 4 * sizeof(uint32_t)); - xc->valpos_mem = NULL; - -#if defined __CYGWIN__ || defined __MINGW32__ - if (xc->curval_mem) { - if (!is_closing) /* need to flush out for next emulated mmap() read */ - { - unsigned char *pnt = xc->curval_mem; - int __fd = fileno(xc->curval_handle); - fst_off_t cur_offs = lseek(__fd, 0, SEEK_CUR); - size_t i; - size_t __len = xc->maxvalpos; +(void)is_closing; - lseek(__fd, 0, SEEK_SET); - for (i = 0; i < __len; i += SSIZE_MAX) { - write(__fd, pnt + i, ((__len - i) >= SSIZE_MAX) ? SSIZE_MAX : (__len - i)); - } - lseek(__fd, cur_offs, SEEK_SET); - } - } -#endif +fstMunmap(xc->valpos_mem, xc->maxhandle * 4 * sizeof(uint32_t)); +xc->valpos_mem = NULL; - fstMunmap(xc->curval_mem, xc->maxvalpos); - xc->curval_mem = NULL; +fstMunmap(xc->curval_mem, xc->maxvalpos); +xc->curval_mem = NULL; } + /* * set up large and small memory usages * crossover point in model is FST_ACTIVATE_HUGE_BREAK number of signals @@ -1000,89 +1068,105 @@ static void fstDestroyMmaps(struct fstWriterContext *xc, int is_closing) static void fstDetermineBreakSize(struct fstWriterContext *xc) { #if defined(__linux__) || defined(FST_MACOSX) - int was_set = 0; +int was_set = 0; #ifdef __linux__ - FILE *f = fopen("/proc/meminfo", "rb"); +FILE *f = fopen("/proc/meminfo", "rb"); - if (f) { +if(f) + { char buf[257]; char *s; - while (!feof(f)) { - buf[0] = 0; - s = fgets(buf, 256, f); - if (s && *s) { - if (!strncmp(s, "MemTotal:", 9)) { - size_t v = atol(s + 10); - v *= 1024; /* convert to bytes */ - v /= 8; /* chop down to 1/8 physical memory */ - if (v > FST_BREAK_SIZE) { - if (v > FST_BREAK_SIZE_MAX) { - v = FST_BREAK_SIZE_MAX; + while(!feof(f)) + { + buf[0] = 0; + s = fgets(buf, 256, f); + if(s && *s) + { + if(!strncmp(s, "MemTotal:", 9)) + { + size_t v = atol(s+10); + v *= 1024; /* convert to bytes */ + v /= 8; /* chop down to 1/8 physical memory */ + if(v > FST_BREAK_SIZE) + { + if(v > FST_BREAK_SIZE_MAX) + { + v = FST_BREAK_SIZE_MAX; + } + + xc->fst_huge_break_size = v; + was_set = 1; + break; + } + } } - - xc->fst_huge_break_size = v; - was_set = 1; - break; - } } - } - } fclose(f); - } + } - if (!was_set) { +if(!was_set) + { xc->fst_huge_break_size = FST_BREAK_SIZE; - } + } #else - int mib[2]; - int64_t v; - size_t length; - - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - length = sizeof(int64_t); - if (!sysctl(mib, 2, &v, &length, NULL, 0)) { +int mib[2]; +int64_t v; +size_t length; + +mib[0] = CTL_HW; +mib[1] = HW_MEMSIZE; +length = sizeof(int64_t); +if(!sysctl(mib, 2, &v, &length, NULL, 0)) + { v /= 8; - if (v > (int64_t)FST_BREAK_SIZE) { - if (v > (int64_t)FST_BREAK_SIZE_MAX) { - v = FST_BREAK_SIZE_MAX; - } + if(v > (int64_t)FST_BREAK_SIZE) + { + if(v > (int64_t)FST_BREAK_SIZE_MAX) + { + v = FST_BREAK_SIZE_MAX; + } - xc->fst_huge_break_size = v; - was_set = 1; + xc->fst_huge_break_size = v; + was_set = 1; + } } - } - if (!was_set) { +if(!was_set) + { xc->fst_huge_break_size = FST_BREAK_SIZE; - } + } #endif #else - xc->fst_huge_break_size = FST_BREAK_SIZE; +xc->fst_huge_break_size = FST_BREAK_SIZE; #endif - xc->fst_break_size = xc->fst_orig_break_size = FST_BREAK_SIZE; - xc->fst_break_add_size = xc->fst_orig_break_add_size = FST_BREAK_ADD_SIZE; - xc->next_huge_break = FST_ACTIVATE_HUGE_BREAK; +xc->fst_break_size = xc->fst_orig_break_size = FST_BREAK_SIZE; +xc->fst_break_add_size = xc->fst_orig_break_add_size = FST_BREAK_ADD_SIZE; +xc->next_huge_break = FST_ACTIVATE_HUGE_BREAK; } + /* * file creation and close */ void *fstWriterCreate(const char *nam, int use_compressed_hier) { - struct fstWriterContext *xc = (struct fstWriterContext *)calloc(1, sizeof(struct fstWriterContext)); +struct fstWriterContext *xc = (struct fstWriterContext *)calloc(1, sizeof(struct fstWriterContext)); - xc->compress_hier = use_compressed_hier; - fstDetermineBreakSize(xc); +xc->compress_hier = use_compressed_hier; +fstDetermineBreakSize(xc); - if ((!nam) || (!(xc->handle = unlink_fopen(nam, "w+b")))) { +if((!nam)|| + (!(xc->handle=unlink_fopen(nam, "w+b")))) + { free(xc); - xc = NULL; - } else { + xc=NULL; + } + else + { int flen = strlen(nam); char *hf = (char *)calloc(1, flen + 6); @@ -1090,96 +1174,99 @@ void *fstWriterCreate(const char *nam, int use_compressed_hier) strcpy(hf + flen, ".hier"); xc->hier_handle = unlink_fopen(hf, "w+b"); - xc->geom_handle = tmpfile_open(&xc->geom_handle_nam); /* .geom */ - xc->valpos_handle = tmpfile_open(&xc->valpos_handle_nam); /* .offs */ - xc->curval_handle = tmpfile_open(&xc->curval_handle_nam); /* .bits */ - xc->tchn_handle = tmpfile_open(&xc->tchn_handle_nam); /* .tchn */ + xc->geom_handle = tmpfile_open(&xc->geom_handle_nam); /* .geom */ + xc->valpos_handle = tmpfile_open(&xc->valpos_handle_nam); /* .offs */ + xc->curval_handle = tmpfile_open(&xc->curval_handle_nam); /* .bits */ + xc->tchn_handle = tmpfile_open(&xc->tchn_handle_nam); /* .tchn */ xc->vchg_alloc_siz = xc->fst_break_size + xc->fst_break_add_size; xc->vchg_mem = (unsigned char *)malloc(xc->vchg_alloc_siz); - if (xc->hier_handle && xc->geom_handle && xc->valpos_handle && xc->curval_handle && xc->vchg_mem && - xc->tchn_handle) { - xc->filename = strdup(nam); - xc->is_initial_time = 1; + if(xc->hier_handle && xc->geom_handle && xc->valpos_handle && xc->curval_handle && xc->vchg_mem && xc->tchn_handle) + { + xc->filename = strdup(nam); + xc->is_initial_time = 1; - fstWriterEmitHdrBytes(xc); - xc->nan = strtod("NaN", NULL); + fstWriterEmitHdrBytes(xc); + xc->nan = strtod("NaN", NULL); #ifdef FST_WRITER_PARALLEL - pthread_mutex_init(&xc->mutex, NULL); - pthread_attr_init(&xc->thread_attr); - pthread_attr_setdetachstate(&xc->thread_attr, PTHREAD_CREATE_DETACHED); + pthread_mutex_init(&xc->mutex, NULL); + pthread_attr_init(&xc->thread_attr); + pthread_attr_setdetachstate(&xc->thread_attr, PTHREAD_CREATE_DETACHED); #endif - } else { - fclose(xc->handle); - if (xc->hier_handle) { - fclose(xc->hier_handle); - unlink(hf); - } - tmpfile_close(&xc->geom_handle, &xc->geom_handle_nam); - tmpfile_close(&xc->valpos_handle, &xc->valpos_handle_nam); - tmpfile_close(&xc->curval_handle, &xc->curval_handle_nam); - tmpfile_close(&xc->tchn_handle, &xc->tchn_handle_nam); - free(xc->vchg_mem); - free(xc); - xc = NULL; - } + } + else + { + fclose(xc->handle); + if(xc->hier_handle) { fclose(xc->hier_handle); unlink(hf); } + tmpfile_close(&xc->geom_handle, &xc->geom_handle_nam); + tmpfile_close(&xc->valpos_handle, &xc->valpos_handle_nam); + tmpfile_close(&xc->curval_handle, &xc->curval_handle_nam); + tmpfile_close(&xc->tchn_handle, &xc->tchn_handle_nam); + free(xc->vchg_mem); + free(xc); + xc=NULL; + } free(hf); - } + } - return (xc); +return(xc); } + /* * generation and writing out of value change data sections */ static void fstWriterEmitSectionHeader(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { unsigned long destlen; unsigned char *dmem; int rc; destlen = xc->maxvalpos; dmem = (unsigned char *)malloc(compressBound(destlen)); - rc = compress2(dmem, &destlen, xc->curval_mem, xc->maxvalpos, - 4); /* was 9...which caused performance drag on traces with many signals */ + rc = compress2(dmem, &destlen, xc->curval_mem, xc->maxvalpos, 4); /* was 9...which caused performance drag on traces with many signals */ - fputc(FST_BL_SKIP, xc->handle); /* temporarily tag the section, use FST_BL_VCDATA on finalize */ + fputc(FST_BL_SKIP, xc->handle); /* temporarily tag the section, use FST_BL_VCDATA on finalize */ xc->section_start = ftello(xc->handle); #ifdef FST_WRITER_PARALLEL - if (xc->xc_parent) - xc->xc_parent->section_start = xc->section_start; + if(xc->xc_parent) xc->xc_parent->section_start = xc->section_start; #endif - xc->section_header_only = 1; /* indicates truncate might be needed */ - fstWriterUint64(xc->handle, 0); /* placeholder = section length */ - fstWriterUint64(xc->handle, xc->is_initial_time ? xc->firsttime : xc->curtime); /* begin time of section */ - fstWriterUint64(xc->handle, xc->curtime); /* end time of section (placeholder) */ - fstWriterUint64(xc->handle, - 0); /* placeholder = amount of buffer memory required in reader for full vc traversal */ - fstWriterVarint(xc->handle, xc->maxvalpos); /* maxvalpos = length of uncompressed data */ - - if ((rc == Z_OK) && (destlen < xc->maxvalpos)) { - fstWriterVarint(xc->handle, destlen); /* length of compressed data */ - } else { - fstWriterVarint(xc->handle, xc->maxvalpos); /* length of (unable to be) compressed data */ - } - fstWriterVarint(xc->handle, - xc->maxhandle); /* max handle associated with this data (in case of dynamic facility adds) */ + xc->section_header_only = 1; /* indicates truncate might be needed */ + fstWriterUint64(xc->handle, 0); /* placeholder = section length */ + fstWriterUint64(xc->handle, xc->is_initial_time ? xc->firsttime : xc->curtime); /* begin time of section */ + fstWriterUint64(xc->handle, xc->curtime); /* end time of section (placeholder) */ + fstWriterUint64(xc->handle, 0); /* placeholder = amount of buffer memory required in reader for full vc traversal */ + fstWriterVarint(xc->handle, xc->maxvalpos); /* maxvalpos = length of uncompressed data */ + + if((rc == Z_OK) && (destlen < xc->maxvalpos)) + { + fstWriterVarint(xc->handle, destlen); /* length of compressed data */ + } + else + { + fstWriterVarint(xc->handle, xc->maxvalpos); /* length of (unable to be) compressed data */ + } + fstWriterVarint(xc->handle, xc->maxhandle); /* max handle associated with this data (in case of dynamic facility adds) */ - if ((rc == Z_OK) && (destlen < xc->maxvalpos)) { - fstFwrite(dmem, destlen, 1, xc->handle); - } else /* comparison between compressed / decompressed len tells if compressed */ - { - fstFwrite(xc->curval_mem, xc->maxvalpos, 1, xc->handle); - } + if((rc == Z_OK) && (destlen < xc->maxvalpos)) + { + fstFwrite(dmem, destlen, 1, xc->handle); + } + else /* comparison between compressed / decompressed len tells if compressed */ + { + fstFwrite(xc->curval_mem, xc->maxvalpos, 1, xc->handle); + } free(dmem); - } + } } + /* * only to be called directly by fst code...otherwise must * be synced up with time changes @@ -1191,517 +1278,566 @@ static void fstWriterFlushContextPrivate(void *ctx) #endif { #ifdef FST_DEBUG - int cnt = 0; +int cnt = 0; #endif - unsigned int i; - unsigned char *vchg_mem; - FILE *f; - fst_off_t fpos, indxpos, endpos; - uint32_t prevpos; - int zerocnt; - unsigned char *scratchpad; - unsigned char *scratchpnt; - unsigned char *tmem; - fst_off_t tlen; - fst_off_t unc_memreq = 0; /* for reader */ - unsigned char *packmem; - unsigned int packmemlen; - uint32_t *vm4ip; - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +unsigned int i; +unsigned char *vchg_mem; +FILE *f; +fst_off_t fpos, indxpos, endpos; +uint32_t prevpos; +int zerocnt; +unsigned char *scratchpad; +unsigned char *scratchpnt; +unsigned char *tmem; +fst_off_t tlen; +fst_off_t unc_memreq = 0; /* for reader */ +unsigned char *packmem; +unsigned int packmemlen; +uint32_t *vm4ip; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; #ifdef FST_WRITER_PARALLEL - struct fstWriterContext *xc2 = xc->xc_parent; +struct fstWriterContext *xc2 = xc->xc_parent; #else - struct fstWriterContext *xc2 = xc; +struct fstWriterContext *xc2 = xc; #endif #ifndef FST_DYNAMIC_ALIAS_DISABLE - Pvoid_t PJHSArray = (Pvoid_t)NULL; +Pvoid_t PJHSArray = (Pvoid_t) NULL; #ifndef _WAVE_HAVE_JUDY - uint32_t hashmask = xc->maxhandle; - hashmask |= hashmask >> 1; - hashmask |= hashmask >> 2; - hashmask |= hashmask >> 4; - hashmask |= hashmask >> 8; - hashmask |= hashmask >> 16; +uint32_t hashmask = xc->maxhandle; +hashmask |= hashmask >> 1; +hashmask |= hashmask >> 2; +hashmask |= hashmask >> 4; +hashmask |= hashmask >> 8; +hashmask |= hashmask >> 16; #endif #endif - if ((xc->vchg_siz <= 1) || (xc->already_in_flush)) - return; - xc->already_in_flush = 1; /* should really do this with a semaphore */ +if((xc->vchg_siz <= 1)||(xc->already_in_flush)) return; +xc->already_in_flush = 1; /* should really do this with a semaphore */ - xc->section_header_only = 0; - scratchpad = (unsigned char *)malloc(xc->vchg_siz); +xc->section_header_only = 0; +scratchpad = (unsigned char *)malloc(xc->vchg_siz); - vchg_mem = xc->vchg_mem; +vchg_mem = xc->vchg_mem; - f = xc->handle; - fstWriterVarint(f, xc->maxhandle); /* emit current number of handles */ - fputc(xc->fourpack ? '4' : (xc->fastpack ? 'F' : 'Z'), f); - fpos = 1; +f = xc->handle; +fstWriterVarint(f, xc->maxhandle); /* emit current number of handles */ +fputc(xc->fourpack ? '4' : (xc->fastpack ? 'F' : 'Z'), f); +fpos = 1; - packmemlen = 1024; /* maintain a running "longest" allocation to */ - packmem = (unsigned char *)malloc(packmemlen); /* prevent continual malloc...free every loop iter */ +packmemlen = 1024; /* maintain a running "longest" allocation to */ +packmem = (unsigned char *)malloc(packmemlen); /* prevent continual malloc...free every loop iter */ - for (i = 0; i < xc->maxhandle; i++) { - vm4ip = &(xc->valpos_mem[4 * i]); +for(i=0;imaxhandle;i++) + { + vm4ip = &(xc->valpos_mem[4*i]); - if (vm4ip[2]) { - uint32_t offs = vm4ip[2]; - uint32_t next_offs; - unsigned int wrlen; + if(vm4ip[2]) + { + uint32_t offs = vm4ip[2]; + uint32_t next_offs; + unsigned int wrlen; - vm4ip[2] = fpos; + vm4ip[2] = fpos; - scratchpnt = scratchpad + xc->vchg_siz; /* build this buffer backwards */ - if (vm4ip[1] <= 1) { - if (vm4ip[1] == 1) { - wrlen = fstGetVarint32Length(vchg_mem + offs + 4); /* used to advance and determine wrlen */ + scratchpnt = scratchpad + xc->vchg_siz; /* build this buffer backwards */ + if(vm4ip[1] <= 1) + { + if(vm4ip[1] == 1) + { + wrlen = fstGetVarint32Length(vchg_mem + offs + 4); /* used to advance and determine wrlen */ #ifndef FST_REMOVE_DUPLICATE_VC - xc->curval_mem[vm4ip[0]] = vchg_mem[offs + 4 + wrlen]; /* checkpoint variable */ + xc->curval_mem[vm4ip[0]] = vchg_mem[offs + 4 + wrlen]; /* checkpoint variable */ #endif - while (offs) { - unsigned char val; - uint32_t time_delta, rcv; - next_offs = fstGetUint32(vchg_mem + offs); - offs += 4; - - time_delta = fstGetVarint32(vchg_mem + offs, (int *)&wrlen); - val = vchg_mem[offs + wrlen]; - offs = next_offs; - - switch (val) { - case '0': - case '1': - rcv = ((val & 1) << 1) | (time_delta << 2); - break; /* pack more delta bits in for 0/1 vchs */ - - case 'x': - case 'X': - rcv = FST_RCV_X | (time_delta << 4); - break; - case 'z': - case 'Z': - rcv = FST_RCV_Z | (time_delta << 4); - break; - case 'h': - case 'H': - rcv = FST_RCV_H | (time_delta << 4); - break; - case 'u': - case 'U': - rcv = FST_RCV_U | (time_delta << 4); - break; - case 'w': - case 'W': - rcv = FST_RCV_W | (time_delta << 4); - break; - case 'l': - case 'L': - rcv = FST_RCV_L | (time_delta << 4); - break; - default: - rcv = FST_RCV_D | (time_delta << 4); - break; + while(offs) + { + unsigned char val; + uint32_t time_delta, rcv; + next_offs = fstGetUint32(vchg_mem + offs); + offs += 4; + + time_delta = fstGetVarint32(vchg_mem + offs, (int *)&wrlen); + val = vchg_mem[offs+wrlen]; + offs = next_offs; + + switch(val) + { + case '0': + case '1': rcv = ((val&1)<<1) | (time_delta<<2); + break; /* pack more delta bits in for 0/1 vchs */ + + case 'x': case 'X': rcv = FST_RCV_X | (time_delta<<4); break; + case 'z': case 'Z': rcv = FST_RCV_Z | (time_delta<<4); break; + case 'h': case 'H': rcv = FST_RCV_H | (time_delta<<4); break; + case 'u': case 'U': rcv = FST_RCV_U | (time_delta<<4); break; + case 'w': case 'W': rcv = FST_RCV_W | (time_delta<<4); break; + case 'l': case 'L': rcv = FST_RCV_L | (time_delta<<4); break; + default: rcv = FST_RCV_D | (time_delta<<4); break; + } + + scratchpnt = fstCopyVarint32ToLeft(scratchpnt, rcv); + } + } + else + { + /* variable length */ + /* fstGetUint32 (next_offs) + fstGetVarint32 (time_delta) + fstGetVarint32 (len) + payload */ + unsigned char *pnt; + uint32_t record_len; + uint32_t time_delta; + + while(offs) + { + next_offs = fstGetUint32(vchg_mem + offs); + offs += 4; + pnt = vchg_mem + offs; + offs = next_offs; + time_delta = fstGetVarint32(pnt, (int *)&wrlen); + pnt += wrlen; + record_len = fstGetVarint32(pnt, (int *)&wrlen); + pnt += wrlen; + + scratchpnt -= record_len; + memcpy(scratchpnt, pnt, record_len); + + scratchpnt = fstCopyVarint32ToLeft(scratchpnt, record_len); + scratchpnt = fstCopyVarint32ToLeft(scratchpnt, (time_delta << 1)); /* reserve | 1 case for future expansion */ + } + } } - - scratchpnt = fstCopyVarint32ToLeft(scratchpnt, rcv); - } - } else { - /* variable length */ - /* fstGetUint32 (next_offs) + fstGetVarint32 (time_delta) + fstGetVarint32 (len) + payload */ - unsigned char *pnt; - uint32_t record_len; - uint32_t time_delta; - - while (offs) { - next_offs = fstGetUint32(vchg_mem + offs); - offs += 4; - pnt = vchg_mem + offs; - offs = next_offs; - time_delta = fstGetVarint32(pnt, (int *)&wrlen); - pnt += wrlen; - record_len = fstGetVarint32(pnt, (int *)&wrlen); - pnt += wrlen; - - scratchpnt -= record_len; - memcpy(scratchpnt, pnt, record_len); - - scratchpnt = fstCopyVarint32ToLeft(scratchpnt, record_len); - scratchpnt = fstCopyVarint32ToLeft( - scratchpnt, (time_delta << 1)); /* reserve | 1 case for future expansion */ - } - } - } else { - wrlen = fstGetVarint32Length(vchg_mem + offs + 4); /* used to advance and determine wrlen */ + else + { + wrlen = fstGetVarint32Length(vchg_mem + offs + 4); /* used to advance and determine wrlen */ #ifndef FST_REMOVE_DUPLICATE_VC - memcpy(xc->curval_mem + vm4ip[0], vchg_mem + offs + 4 + wrlen, vm4ip[1]); /* checkpoint variable */ + memcpy(xc->curval_mem + vm4ip[0], vchg_mem + offs + 4 + wrlen, vm4ip[1]); /* checkpoint variable */ #endif - while (offs) { - unsigned int idx; - char is_binary = 1; - unsigned char *pnt; - uint32_t time_delta; - - next_offs = fstGetUint32(vchg_mem + offs); - offs += 4; - - time_delta = fstGetVarint32(vchg_mem + offs, (int *)&wrlen); - - pnt = vchg_mem + offs + wrlen; - offs = next_offs; - - for (idx = 0; idx < vm4ip[1]; idx++) { - if ((pnt[idx] == '0') || (pnt[idx] == '1')) { - continue; - } else { - is_binary = 0; - break; - } - } - - if (is_binary) { - unsigned char acc = 0; - /* new algorithm */ - idx = ((vm4ip[1] + 7) & ~7); - switch (vm4ip[1] & 7) { - case 0: - do { - acc = (pnt[idx + 7 - 8] & 1) << 0; /* fallthrough */ - case 7: - acc |= (pnt[idx + 6 - 8] & 1) << 1; /* fallthrough */ - case 6: - acc |= (pnt[idx + 5 - 8] & 1) << 2; /* fallthrough */ - case 5: - acc |= (pnt[idx + 4 - 8] & 1) << 3; /* fallthrough */ - case 4: - acc |= (pnt[idx + 3 - 8] & 1) << 4; /* fallthrough */ - case 3: - acc |= (pnt[idx + 2 - 8] & 1) << 5; /* fallthrough */ - case 2: - acc |= (pnt[idx + 1 - 8] & 1) << 6; /* fallthrough */ - case 1: - acc |= (pnt[idx + 0 - 8] & 1) << 7; - *(--scratchpnt) = acc; - idx -= 8; - } while (idx); + while(offs) + { + unsigned int idx; + char is_binary = 1; + unsigned char *pnt; + uint32_t time_delta; + + next_offs = fstGetUint32(vchg_mem + offs); + offs += 4; + + time_delta = fstGetVarint32(vchg_mem + offs, (int *)&wrlen); + + pnt = vchg_mem+offs+wrlen; + offs = next_offs; + + for(idx=0;idxvchg_siz - scratchpnt; - unc_memreq += wrlen; - if (wrlen > 32) { - unsigned long destlen = wrlen; - unsigned char *dmem; - unsigned int rc; - - if (!xc->fastpack) { - if (wrlen <= packmemlen) { - dmem = packmem; - } else { - free(packmem); - dmem = packmem = (unsigned char *)malloc(compressBound(packmemlen = wrlen)); - } - - rc = compress2(dmem, &destlen, scratchpnt, wrlen, 4); - if (rc == Z_OK) { -#ifndef FST_DYNAMIC_ALIAS_DISABLE - PPvoid_t pv = JudyHSIns(&PJHSArray, dmem, destlen, NULL); - if (*pv) { - uint32_t pvi = (intptr_t)(*pv); - vm4ip[2] = -pvi; - } else { - *pv = (void *)(intptr_t)(i + 1); -#endif - fpos += fstWriterVarint(f, wrlen); - fpos += destlen; - fstFwrite(dmem, destlen, 1, f); + wrlen = scratchpad + xc->vchg_siz - scratchpnt; + unc_memreq += wrlen; + if(wrlen > 32) + { + unsigned long destlen = wrlen; + unsigned char *dmem; + unsigned int rc; + + if(!xc->fastpack) + { + if(wrlen <= packmemlen) + { + dmem = packmem; + } + else + { + free(packmem); + dmem = packmem = (unsigned char *)malloc(compressBound(packmemlen = wrlen)); + } + + rc = compress2(dmem, &destlen, scratchpnt, wrlen, 4); + if(rc == Z_OK) + { #ifndef FST_DYNAMIC_ALIAS_DISABLE - } + PPvoid_t pv = JudyHSIns(&PJHSArray, dmem, destlen, NULL); + if(*pv) + { + uint32_t pvi = (intptr_t)(*pv); + vm4ip[2] = -pvi; + } + else + { + *pv = (void *)(intptr_t)(i+1); #endif - } else { + fpos += fstWriterVarint(f, wrlen); + fpos += destlen; + fstFwrite(dmem, destlen, 1, f); #ifndef FST_DYNAMIC_ALIAS_DISABLE - PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); - if (*pv) { - uint32_t pvi = (intptr_t)(*pv); - vm4ip[2] = -pvi; - } else { - *pv = (void *)(intptr_t)(i + 1); + } #endif - fpos += fstWriterVarint(f, 0); - fpos += wrlen; - fstFwrite(scratchpnt, wrlen, 1, f); + } + else + { #ifndef FST_DYNAMIC_ALIAS_DISABLE - } + PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); + if(*pv) + { + uint32_t pvi = (intptr_t)(*pv); + vm4ip[2] = -pvi; + } + else + { + *pv = (void *)(intptr_t)(i+1); #endif - } - } else { - /* this is extremely conservative: fastlz needs +5% for worst case, lz4 needs siz+(siz/255)+16 */ - if (((wrlen * 2) + 2) <= packmemlen) { - dmem = packmem; - } else { - free(packmem); - dmem = packmem = (unsigned char *)malloc(packmemlen = (wrlen * 2) + 2); - } - - rc = (xc->fourpack) ? LZ4_compress((char *)scratchpnt, (char *)dmem, wrlen) - : fastlz_compress(scratchpnt, wrlen, dmem); - if (rc < destlen) { + fpos += fstWriterVarint(f, 0); + fpos += wrlen; + fstFwrite(scratchpnt, wrlen, 1, f); #ifndef FST_DYNAMIC_ALIAS_DISABLE - PPvoid_t pv = JudyHSIns(&PJHSArray, dmem, rc, NULL); - if (*pv) { - uint32_t pvi = (intptr_t)(*pv); - vm4ip[2] = -pvi; - } else { - *pv = (void *)(intptr_t)(i + 1); + } #endif - fpos += fstWriterVarint(f, wrlen); - fpos += rc; - fstFwrite(dmem, rc, 1, f); + } + } + else + { + /* this is extremely conservative: fastlz needs +5% for worst case, lz4 needs siz+(siz/255)+16 */ + if(((wrlen * 2) + 2) <= packmemlen) + { + dmem = packmem; + } + else + { + free(packmem); + dmem = packmem = (unsigned char *)malloc(packmemlen = (wrlen * 2) + 2); + } + + rc = (xc->fourpack) ? LZ4_compress_default((char *)scratchpnt, (char *)dmem, wrlen, packmemlen) : fastlz_compress(scratchpnt, wrlen, dmem); + if(rc < destlen) + { #ifndef FST_DYNAMIC_ALIAS_DISABLE - } + PPvoid_t pv = JudyHSIns(&PJHSArray, dmem, rc, NULL); + if(*pv) + { + uint32_t pvi = (intptr_t)(*pv); + vm4ip[2] = -pvi; + } + else + { + *pv = (void *)(intptr_t)(i+1); #endif - } else { + fpos += fstWriterVarint(f, wrlen); + fpos += rc; + fstFwrite(dmem, rc, 1, f); #ifndef FST_DYNAMIC_ALIAS_DISABLE - PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); - if (*pv) { - uint32_t pvi = (intptr_t)(*pv); - vm4ip[2] = -pvi; - } else { - *pv = (void *)(intptr_t)(i + 1); + } #endif - fpos += fstWriterVarint(f, 0); - fpos += wrlen; - fstFwrite(scratchpnt, wrlen, 1, f); + } + else + { #ifndef FST_DYNAMIC_ALIAS_DISABLE - } + PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); + if(*pv) + { + uint32_t pvi = (intptr_t)(*pv); + vm4ip[2] = -pvi; + } + else + { + *pv = (void *)(intptr_t)(i+1); #endif - } - } - } else { + fpos += fstWriterVarint(f, 0); + fpos += wrlen; + fstFwrite(scratchpnt, wrlen, 1, f); #ifndef FST_DYNAMIC_ALIAS_DISABLE - PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); - if (*pv) { - uint32_t pvi = (intptr_t)(*pv); - vm4ip[2] = -pvi; - } else { - *pv = (void *)(intptr_t)(i + 1); + } #endif - fpos += fstWriterVarint(f, 0); - fpos += wrlen; - fstFwrite(scratchpnt, wrlen, 1, f); -#ifndef FST_DYNAMIC_ALIAS_DISABLE - } + } + } + } + else + { +#ifndef FST_DYNAMIC_ALIAS_DISABLE + PPvoid_t pv = JudyHSIns(&PJHSArray, scratchpnt, wrlen, NULL); + if(*pv) + { + uint32_t pvi = (intptr_t)(*pv); + vm4ip[2] = -pvi; + } + else + { + *pv = (void *)(intptr_t)(i+1); #endif - } + fpos += fstWriterVarint(f, 0); + fpos += wrlen; + fstFwrite(scratchpnt, wrlen, 1, f); +#ifndef FST_DYNAMIC_ALIAS_DISABLE + } +#endif + } - /* vm4ip[3] = 0; ...redundant with clearing below */ + /* vm4ip[3] = 0; ...redundant with clearing below */ #ifdef FST_DEBUG - cnt++; + cnt++; #endif + } } - } #ifndef FST_DYNAMIC_ALIAS_DISABLE - JudyHSFreeArray(&PJHSArray, NULL); +JudyHSFreeArray(&PJHSArray, NULL); #endif - free(packmem); - packmem = NULL; /* packmemlen = 0; */ /* scan-build */ +free(packmem); packmem = NULL; /* packmemlen = 0; */ /* scan-build */ - prevpos = 0; - zerocnt = 0; - free(scratchpad); - scratchpad = NULL; +prevpos = 0; zerocnt = 0; +free(scratchpad); scratchpad = NULL; - indxpos = ftello(f); - xc->secnum++; +indxpos = ftello(f); +xc->secnum++; #ifndef FST_DYNAMIC_ALIAS2_DISABLE - if (1) { +if(1) + { uint32_t prev_alias = 0; - for (i = 0; i < xc->maxhandle; i++) { - vm4ip = &(xc->valpos_mem[4 * i]); + for(i=0;imaxhandle;i++) + { + vm4ip = &(xc->valpos_mem[4*i]); - if (vm4ip[2]) { - if (zerocnt) { - fpos += fstWriterVarint(f, (zerocnt << 1)); - zerocnt = 0; - } + if(vm4ip[2]) + { + if(zerocnt) + { + fpos += fstWriterVarint(f, (zerocnt << 1)); + zerocnt = 0; + } - if (vm4ip[2] & 0x80000000) { - if (vm4ip[2] != prev_alias) { - fpos += fstWriterSVarint(f, (((int64_t)((int32_t)(prev_alias = vm4ip[2]))) << 1) | 1); - } else { - fpos += fstWriterSVarint(f, (0 << 1) | 1); - } - } else { - fpos += fstWriterSVarint(f, ((vm4ip[2] - prevpos) << 1) | 1); - prevpos = vm4ip[2]; + if(vm4ip[2] & 0x80000000) + { + if(vm4ip[2] != prev_alias) + { + int32_t t_i32 = ((int32_t)(prev_alias = vm4ip[2])); /* vm4ip is generic unsigned data */ + int64_t t_i64 = (int64_t)t_i32; /* convert to signed */ + uint64_t t_u64 = (uint64_t)t_i64; /* sign extend through 64b */ + + fpos += fstWriterSVarint(f, (int64_t)((t_u64 << 1) | 1)); /* all in this block was: fpos += fstWriterSVarint(f, (((int64_t)((int32_t)(prev_alias = vm4ip[2]))) << 1) | 1); */ + } + else + { + fpos += fstWriterSVarint(f, (0 << 1) | 1); + } + } + else + { + fpos += fstWriterSVarint(f, ((vm4ip[2] - prevpos) << 1) | 1); + prevpos = vm4ip[2]; + } + vm4ip[2] = 0; + vm4ip[3] = 0; /* clear out tchn idx */ + } + else + { + zerocnt++; + } } - vm4ip[2] = 0; - vm4ip[3] = 0; /* clear out tchn idx */ - } else { - zerocnt++; - } } - } else + else #endif - { - for (i = 0; i < xc->maxhandle; i++) { - vm4ip = &(xc->valpos_mem[4 * i]); + { + for(i=0;imaxhandle;i++) + { + vm4ip = &(xc->valpos_mem[4*i]); - if (vm4ip[2]) { - if (zerocnt) { - fpos += fstWriterVarint(f, (zerocnt << 1)); - zerocnt = 0; - } + if(vm4ip[2]) + { + if(zerocnt) + { + fpos += fstWriterVarint(f, (zerocnt << 1)); + zerocnt = 0; + } - if (vm4ip[2] & 0x80000000) { - fpos += fstWriterVarint(f, 0); /* signal, note that using a *signed* varint would be more efficient - than this byte escape! */ - fpos += fstWriterVarint(f, (-(int32_t)vm4ip[2])); - } else { - fpos += fstWriterVarint(f, ((vm4ip[2] - prevpos) << 1) | 1); - prevpos = vm4ip[2]; + if(vm4ip[2] & 0x80000000) + { + fpos += fstWriterVarint(f, 0); /* signal, note that using a *signed* varint would be more efficient than this byte escape! */ + fpos += fstWriterVarint(f, (-(int32_t)vm4ip[2])); + } + else + { + fpos += fstWriterVarint(f, ((vm4ip[2] - prevpos) << 1) | 1); + prevpos = vm4ip[2]; + } + vm4ip[2] = 0; + vm4ip[3] = 0; /* clear out tchn idx */ + } + else + { + zerocnt++; + } } - vm4ip[2] = 0; - vm4ip[3] = 0; /* clear out tchn idx */ - } else { - zerocnt++; - } } - } - if (zerocnt) { +if(zerocnt) + { /* fpos += */ fstWriterVarint(f, (zerocnt << 1)); /* scan-build */ - } + } #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "value chains: %d\n", cnt); +fprintf(stderr, FST_APIMESS "value chains: %d\n", cnt); #endif - xc->vchg_mem[0] = '!'; - xc->vchg_siz = 1; +xc->vchg_mem[0] = '!'; +xc->vchg_siz = 1; - endpos = ftello(xc->handle); - fstWriterUint64(xc->handle, endpos - indxpos); /* write delta index position at very end of block */ +endpos = ftello(xc->handle); +fstWriterUint64(xc->handle, endpos-indxpos); /* write delta index position at very end of block */ - /*emit time changes for block */ - fflush(xc->tchn_handle); - tlen = ftello(xc->tchn_handle); - fstWriterFseeko(xc, xc->tchn_handle, 0, SEEK_SET); +/*emit time changes for block */ +fflush(xc->tchn_handle); +tlen = ftello(xc->tchn_handle); +fstWriterFseeko(xc, xc->tchn_handle, 0, SEEK_SET); - errno = 0; - fstWriterMmapSanity( - tmem = (unsigned char *)fstMmap(NULL, tlen, PROT_READ | PROT_WRITE, MAP_SHARED, fileno(xc->tchn_handle), 0), - __FILE__, __LINE__, "tmem"); - if (tmem) { +errno = 0; +fstWriterMmapSanity(tmem = (unsigned char *)fstMmap(NULL, tlen, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(xc->tchn_handle), 0), __FILE__, __LINE__, "tmem"); +if(tmem) + { unsigned long destlen = tlen; unsigned char *dmem = (unsigned char *)malloc(compressBound(destlen)); int rc = compress2(dmem, &destlen, tmem, tlen, 9); - if ((rc == Z_OK) && (((fst_off_t)destlen) < tlen)) { - fstFwrite(dmem, destlen, 1, xc->handle); - } else /* comparison between compressed / decompressed len tells if compressed */ - { - fstFwrite(tmem, tlen, 1, xc->handle); - destlen = tlen; - } + if((rc == Z_OK) && (((fst_off_t)destlen) < tlen)) + { + fstFwrite(dmem, destlen, 1, xc->handle); + } + else /* comparison between compressed / decompressed len tells if compressed */ + { + fstFwrite(tmem, tlen, 1, xc->handle); + destlen = tlen; + } free(dmem); fstMunmap(tmem, tlen); - fstWriterUint64(xc->handle, tlen); /* uncompressed */ - fstWriterUint64(xc->handle, destlen); /* compressed */ - fstWriterUint64(xc->handle, xc->tchn_cnt); /* number of time items */ - } - - xc->tchn_cnt = xc->tchn_idx = 0; - fstWriterFseeko(xc, xc->tchn_handle, 0, SEEK_SET); - fstFtruncate(fileno(xc->tchn_handle), 0); - - /* write block trailer */ - endpos = ftello(xc->handle); - fstWriterFseeko(xc, xc->handle, xc->section_start, SEEK_SET); - fstWriterUint64(xc->handle, endpos - xc->section_start); /* write block length */ - fstWriterFseeko(xc, xc->handle, 8, SEEK_CUR); /* skip begin time */ - fstWriterUint64(xc->handle, xc->curtime); /* write end time for section */ - fstWriterUint64(xc->handle, unc_memreq); /* amount of buffer memory required in reader for full traversal */ - fflush(xc->handle); - - fstWriterFseeko(xc, xc->handle, xc->section_start - 1, SEEK_SET); /* write out FST_BL_VCDATA over FST_BL_SKIP */ + fstWriterUint64(xc->handle, tlen); /* uncompressed */ + fstWriterUint64(xc->handle, destlen); /* compressed */ + fstWriterUint64(xc->handle, xc->tchn_cnt); /* number of time items */ + } + +xc->tchn_cnt = xc->tchn_idx = 0; +fstWriterFseeko(xc, xc->tchn_handle, 0, SEEK_SET); +fstFtruncate(fileno(xc->tchn_handle), 0); + +/* write block trailer */ +endpos = ftello(xc->handle); +fstWriterFseeko(xc, xc->handle, xc->section_start, SEEK_SET); +fstWriterUint64(xc->handle, endpos - xc->section_start); /* write block length */ +fstWriterFseeko(xc, xc->handle, 8, SEEK_CUR); /* skip begin time */ +fstWriterUint64(xc->handle, xc->curtime); /* write end time for section */ +fstWriterUint64(xc->handle, unc_memreq); /* amount of buffer memory required in reader for full traversal */ +fflush(xc->handle); + +fstWriterFseeko(xc, xc->handle, xc->section_start-1, SEEK_SET); /* write out FST_BL_VCDATA over FST_BL_SKIP */ #ifndef FST_DYNAMIC_ALIAS_DISABLE #ifndef FST_DYNAMIC_ALIAS2_DISABLE - fputc(FST_BL_VCDATA_DYN_ALIAS2, xc->handle); +fputc(FST_BL_VCDATA_DYN_ALIAS2, xc->handle); #else - fputc(FST_BL_VCDATA_DYN_ALIAS, xc->handle); +fputc(FST_BL_VCDATA_DYN_ALIAS, xc->handle); #endif #else - fputc(FST_BL_VCDATA, xc->handle); +fputc(FST_BL_VCDATA, xc->handle); #endif - fflush(xc->handle); +fflush(xc->handle); - fstWriterFseeko(xc, xc->handle, endpos, SEEK_SET); /* seek to end of file */ +fstWriterFseeko(xc, xc->handle, endpos, SEEK_SET); /* seek to end of file */ - xc2->section_header_truncpos = endpos; /* cache in case of need to truncate */ - if (xc->dump_size_limit) { - if (endpos >= ((fst_off_t)xc->dump_size_limit)) { - xc2->skip_writing_section_hdr = 1; - xc2->size_limit_locked = 1; - xc2->is_initial_time = 1; /* to trick emit value and emit time change */ +xc2->section_header_truncpos = endpos; /* cache in case of need to truncate */ +if(xc->dump_size_limit) + { + if(endpos >= ((fst_off_t)xc->dump_size_limit)) + { + xc2->skip_writing_section_hdr = 1; + xc2->size_limit_locked = 1; + xc2->is_initial_time = 1; /* to trick emit value and emit time change */ #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "<< dump file size limit reached, stopping dumping >>\n"); + fprintf(stderr, FST_APIMESS "<< dump file size limit reached, stopping dumping >>\n"); #endif + } } - } - if (!xc2->skip_writing_section_hdr) { - fstWriterEmitSectionHeader(xc); /* emit next section header */ - } - fflush(xc->handle); +if(!xc2->skip_writing_section_hdr) + { + fstWriterEmitSectionHeader(xc); /* emit next section header */ + } +fflush(xc->handle); - xc->already_in_flush = 0; +xc->already_in_flush = 0; } + #ifdef FST_WRITER_PARALLEL static void *fstWriterFlushContextPrivate1(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - struct fstWriterContext *xc_parent; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc_parent; - pthread_mutex_lock(&(xc->xc_parent->mutex)); - fstWriterFlushContextPrivate2(xc); +pthread_mutex_lock(&(xc->xc_parent->mutex)); +fstWriterFlushContextPrivate2(xc); #ifdef FST_REMOVE_DUPLICATE_VC - free(xc->curval_mem); +free(xc->curval_mem); #endif - free(xc->valpos_mem); - free(xc->vchg_mem); - tmpfile_close(&xc->tchn_handle, &xc->tchn_handle_nam); - xc_parent = xc->xc_parent; - free(xc); +free(xc->valpos_mem); +free(xc->vchg_mem); +tmpfile_close(&xc->tchn_handle, &xc->tchn_handle_nam); +xc_parent = xc->xc_parent; +free(xc); - xc_parent->in_pthread = 0; - pthread_mutex_unlock(&(xc_parent->mutex)); +xc_parent->in_pthread = 0; +pthread_mutex_unlock(&(xc_parent->mutex)); - return (NULL); +return(NULL); } + static void fstWriterFlushContextPrivate(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc->parallel_enabled) { +if(xc->parallel_enabled) + { struct fstWriterContext *xc2 = (struct fstWriterContext *)malloc(sizeof(struct fstWriterContext)); unsigned int i; @@ -1711,6 +1847,14 @@ static void fstWriterFlushContextPrivate(void *ctx) xc->xc_parent = xc; memcpy(xc2, xc, sizeof(struct fstWriterContext)); + if(sizeof(size_t) < sizeof(uint64_t)) + { + /* TALOS-2023-1777 for 32b overflow */ + uint64_t chk_64 = xc->maxhandle * 4 * sizeof(uint32_t); + size_t chk_32 = xc->maxhandle * 4 * sizeof(uint32_t); + if(chk_64 != chk_32) chk_report_abort("TALOS-2023-1777"); + } + xc2->valpos_mem = (uint32_t *)malloc(xc->maxhandle * 4 * sizeof(uint32_t)); memcpy(xc2->valpos_mem, xc->valpos_mem, xc->maxhandle * 4 * sizeof(uint32_t)); @@ -1724,11 +1868,12 @@ static void fstWriterFlushContextPrivate(void *ctx) xc->vchg_mem[0] = '!'; xc->vchg_siz = 1; - for (i = 0; i < xc->maxhandle; i++) { - uint32_t *vm4ip = &(xc->valpos_mem[4 * i]); - vm4ip[2] = 0; /* zero out offset val */ - vm4ip[3] = 0; /* zero out last time change val */ - } + for(i=0;imaxhandle;i++) + { + uint32_t *vm4ip = &(xc->valpos_mem[4*i]); + vm4ip[2] = 0; /* zero out offset val */ + vm4ip[3] = 0; /* zero out last time change val */ + } xc->tchn_cnt = xc->tchn_idx = 0; xc->tchn_handle = tmpfile_open(&xc->tchn_handle_nam); /* child thread will deallocate file/name */ @@ -1738,265 +1883,288 @@ static void fstWriterFlushContextPrivate(void *ctx) xc->section_header_only = 0; xc->secnum++; - while (xc->in_pthread) { - pthread_mutex_lock(&xc->mutex); - pthread_mutex_unlock(&xc->mutex); - }; + while (xc->in_pthread) + { + pthread_mutex_lock(&xc->mutex); + pthread_mutex_unlock(&xc->mutex); + }; pthread_mutex_lock(&xc->mutex); - xc->in_pthread = 1; + xc->in_pthread = 1; pthread_mutex_unlock(&xc->mutex); pthread_create(&xc->thread, &xc->thread_attr, fstWriterFlushContextPrivate1, xc2); - } else { - if (xc->parallel_was_enabled) /* conservatively block */ - { - pthread_mutex_lock(&xc->mutex); - pthread_mutex_unlock(&xc->mutex); } + else + { + if(xc->parallel_was_enabled) /* conservatively block */ + { + pthread_mutex_lock(&xc->mutex); + pthread_mutex_unlock(&xc->mutex); + } xc->xc_parent = xc; fstWriterFlushContextPrivate2(xc); - } + } } #endif + /* * queues up a flush context operation */ void fstWriterFlushContext(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { - if (xc->tchn_idx > 1) { - xc->flush_context_pending = 1; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { + if(xc->tchn_idx > 1) + { + xc->flush_context_pending = 1; + } } - } } + /* * close out FST file */ void fstWriterClose(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; #ifdef FST_WRITER_PARALLEL - if (xc) { +if(xc) + { pthread_mutex_lock(&xc->mutex); pthread_mutex_unlock(&xc->mutex); - } + } #endif - if (xc && !xc->already_in_close && !xc->already_in_flush) { +if(xc && !xc->already_in_close && !xc->already_in_flush) + { unsigned char *tmem = NULL; fst_off_t fixup_offs, tlen, hlen; xc->already_in_close = 1; /* never need to zero this out as it is freed at bottom */ - if (xc->section_header_only && xc->section_header_truncpos && (xc->vchg_siz <= 1) && (!xc->is_initial_time)) { - fstFtruncate(fileno(xc->handle), xc->section_header_truncpos); - fstWriterFseeko(xc, xc->handle, xc->section_header_truncpos, SEEK_SET); - xc->section_header_only = 0; - } else { - xc->skip_writing_section_hdr = 1; - if (!xc->size_limit_locked) { - if (FST_UNLIKELY(xc->is_initial_time)) /* simulation time never advanced so mock up the changes as time - zero ones */ - { - fstHandle dupe_idx; - - fstWriterEmitTimeChange(xc, 0); /* emit some time change just to have one */ - for (dupe_idx = 0; dupe_idx < xc->maxhandle; dupe_idx++) /* now clone the values */ - { - fstWriterEmitValueChange(xc, dupe_idx + 1, xc->curval_mem + xc->valpos_mem[4 * dupe_idx]); - } - } - fstWriterFlushContextPrivate(xc); + if(xc->section_header_only && xc->section_header_truncpos && (xc->vchg_siz <= 1) && (!xc->is_initial_time)) + { + fstFtruncate(fileno(xc->handle), xc->section_header_truncpos); + fstWriterFseeko(xc, xc->handle, xc->section_header_truncpos, SEEK_SET); + xc->section_header_only = 0; + } + else + { + xc->skip_writing_section_hdr = 1; + if(!xc->size_limit_locked) + { + if(FST_UNLIKELY(xc->is_initial_time)) /* simulation time never advanced so mock up the changes as time zero ones */ + { + fstHandle dupe_idx; + + fstWriterEmitTimeChange(xc, 0); /* emit some time change just to have one */ + for(dupe_idx = 0; dupe_idx < xc->maxhandle; dupe_idx++) /* now clone the values */ + { + fstWriterEmitValueChange(xc, dupe_idx+1, xc->curval_mem + xc->valpos_mem[4*dupe_idx]); + } + } + fstWriterFlushContextPrivate(xc); #ifdef FST_WRITER_PARALLEL - pthread_mutex_lock(&xc->mutex); - pthread_mutex_unlock(&xc->mutex); - - while (xc->in_pthread) { - pthread_mutex_lock(&xc->mutex); - pthread_mutex_unlock(&xc->mutex); - }; + pthread_mutex_lock(&xc->mutex); + pthread_mutex_unlock(&xc->mutex); + + while (xc->in_pthread) + { + pthread_mutex_lock(&xc->mutex); + pthread_mutex_unlock(&xc->mutex); + }; #endif - } - } + } + } fstDestroyMmaps(xc, 1); - if (xc->outval_mem) { - free(xc->outval_mem); - xc->outval_mem = NULL; - xc->outval_alloc_siz = 0; - } + if(xc->outval_mem) + { + free(xc->outval_mem); xc->outval_mem = NULL; + xc->outval_alloc_siz = 0; + } /* write out geom section */ fflush(xc->geom_handle); tlen = ftello(xc->geom_handle); - errno = 0; - if (tlen) { - fstWriterMmapSanity(tmem = (unsigned char *)fstMmap(NULL, tlen, PROT_READ | PROT_WRITE, MAP_SHARED, - fileno(xc->geom_handle), 0), - __FILE__, __LINE__, "tmem"); - } + errno = 0; + if(tlen) + { + fstWriterMmapSanity(tmem = (unsigned char *)fstMmap(NULL, tlen, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(xc->geom_handle), 0), __FILE__, __LINE__, "tmem"); + } - if (tmem) { - unsigned long destlen = tlen; - unsigned char *dmem = (unsigned char *)malloc(compressBound(destlen)); - int rc = compress2(dmem, &destlen, tmem, tlen, 9); - - if ((rc != Z_OK) || (((fst_off_t)destlen) > tlen)) { - destlen = tlen; - } - - fixup_offs = ftello(xc->handle); - fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ - fstWriterUint64(xc->handle, destlen + 24); /* section length */ - fstWriterUint64(xc->handle, tlen); /* uncompressed */ - /* compressed len is section length - 24 */ - fstWriterUint64(xc->handle, xc->maxhandle); /* maxhandle */ - fstFwrite((((fst_off_t)destlen) != tlen) ? dmem : tmem, destlen, 1, xc->handle); - fflush(xc->handle); - - fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); - fputc(FST_BL_GEOM, xc->handle); /* actual tag */ - - fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ - fflush(xc->handle); - - free(dmem); - fstMunmap(tmem, tlen); - } - - if (xc->num_blackouts) { - uint64_t cur_bl = 0; - fst_off_t bpos, eos; - uint32_t i; - - fixup_offs = ftello(xc->handle); - fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ - bpos = fixup_offs + 1; - fstWriterUint64(xc->handle, 0); /* section length */ - fstWriterVarint(xc->handle, xc->num_blackouts); - - for (i = 0; i < xc->num_blackouts; i++) { - fputc(xc->blackout_head->active, xc->handle); - fstWriterVarint(xc->handle, xc->blackout_head->tim - cur_bl); - cur_bl = xc->blackout_head->tim; - xc->blackout_curr = xc->blackout_head->next; - free(xc->blackout_head); - xc->blackout_head = xc->blackout_curr; - } - - eos = ftello(xc->handle); - fstWriterFseeko(xc, xc->handle, bpos, SEEK_SET); - fstWriterUint64(xc->handle, eos - bpos); - fflush(xc->handle); - - fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); - fputc(FST_BL_BLACKOUT, xc->handle); /* actual tag */ - - fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ - fflush(xc->handle); - } - - if (xc->compress_hier) { - fst_off_t hl, eos; - gzFile zhandle; - int zfd; - int fourpack_duo = 0; -#ifndef __MINGW32__ - auto fnam_size = strlen(xc->filename) + 5 + 1; - char *fnam = (char *)malloc(fnam_size); -#endif + if(tmem) + { + unsigned long destlen = tlen; + unsigned char *dmem = (unsigned char *)malloc(compressBound(destlen)); + int rc = compress2(dmem, &destlen, tmem, tlen, 9); - fixup_offs = ftello(xc->handle); - fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ - hlen = ftello(xc->handle); - fstWriterUint64(xc->handle, 0); /* section length */ - fstWriterUint64(xc->handle, xc->hier_file_len); /* uncompressed length */ + if((rc != Z_OK) || (((fst_off_t)destlen) > tlen)) + { + destlen = tlen; + } - if (!xc->fourpack) { - unsigned char *mem = (unsigned char *)malloc(FST_GZIO_LEN); - zfd = dup(fileno(xc->handle)); + fixup_offs = ftello(xc->handle); + fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ + fstWriterUint64(xc->handle, destlen + 24); /* section length */ + fstWriterUint64(xc->handle, tlen); /* uncompressed */ + /* compressed len is section length - 24 */ + fstWriterUint64(xc->handle, xc->maxhandle); /* maxhandle */ + fstFwrite((((fst_off_t)destlen) != tlen) ? dmem : tmem, destlen, 1, xc->handle); fflush(xc->handle); - zhandle = gzdopen(zfd, "wb4"); - if (zhandle) { - fstWriterFseeko(xc, xc->hier_handle, 0, SEEK_SET); - for (hl = 0; hl < xc->hier_file_len; hl += FST_GZIO_LEN) { - unsigned len = - ((xc->hier_file_len - hl) > FST_GZIO_LEN) ? FST_GZIO_LEN : (xc->hier_file_len - hl); - fstFread(mem, len, 1, xc->hier_handle); - gzwrite(zhandle, mem, len); - } - gzclose(zhandle); - } else { - close(zfd); - } - free(mem); - } else { - int lz4_maxlen; - unsigned char *mem; - unsigned char *hmem = NULL; - int packed_len; + fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); + fputc(FST_BL_GEOM, xc->handle); /* actual tag */ + + fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ fflush(xc->handle); - lz4_maxlen = LZ4_compressBound(xc->hier_file_len); - mem = (unsigned char *)malloc(lz4_maxlen); - errno = 0; - if (xc->hier_file_len) { - fstWriterMmapSanity(hmem = (unsigned char *)fstMmap(NULL, xc->hier_file_len, PROT_READ | PROT_WRITE, - MAP_SHARED, fileno(xc->hier_handle), 0), - __FILE__, __LINE__, "hmem"); + free(dmem); + fstMunmap(tmem, tlen); } - packed_len = LZ4_compress((char *)hmem, (char *)mem, xc->hier_file_len); - fstMunmap(hmem, xc->hier_file_len); - fourpack_duo = - (!xc->repack_on_close) && - (xc->hier_file_len > FST_HDR_FOURPACK_DUO_SIZE); /* double pack when hierarchy is large */ - - if (fourpack_duo) /* double packing with LZ4 is faster than gzip */ + if(xc->num_blackouts) { - unsigned char *mem_duo; - int lz4_maxlen_duo; - int packed_len_duo; + uint64_t cur_bl = 0; + fst_off_t bpos, eos; + uint32_t i; + + fixup_offs = ftello(xc->handle); + fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ + bpos = fixup_offs + 1; + fstWriterUint64(xc->handle, 0); /* section length */ + fstWriterVarint(xc->handle, xc->num_blackouts); - lz4_maxlen_duo = LZ4_compressBound(packed_len); - mem_duo = (unsigned char *)malloc(lz4_maxlen_duo); - packed_len_duo = LZ4_compress((char *)mem, (char *)mem_duo, packed_len); + for(i=0;inum_blackouts;i++) + { + fputc(xc->blackout_head->active, xc->handle); + fstWriterVarint(xc->handle, xc->blackout_head->tim - cur_bl); + cur_bl = xc->blackout_head->tim; + xc->blackout_curr = xc->blackout_head->next; + free(xc->blackout_head); + xc->blackout_head = xc->blackout_curr; + } + + eos = ftello(xc->handle); + fstWriterFseeko(xc, xc->handle, bpos, SEEK_SET); + fstWriterUint64(xc->handle, eos - bpos); + fflush(xc->handle); - fstWriterVarint(xc->handle, packed_len); /* 1st round compressed length */ - fstFwrite(mem_duo, packed_len_duo, 1, xc->handle); - free(mem_duo); - } else { - fstFwrite(mem, packed_len, 1, xc->handle); + fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); + fputc(FST_BL_BLACKOUT, xc->handle); /* actual tag */ + + fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ + fflush(xc->handle); } - free(mem); - } + if(xc->compress_hier) + { + fst_off_t hl, eos; + gzFile zhandle; + int zfd; + int fourpack_duo = 0; +#ifndef __MINGW32__ + int fnam_len = strlen(xc->filename) + 5 + 1; + char *fnam = (char *)malloc(fnam_len); +#endif + + fixup_offs = ftello(xc->handle); + fputc(FST_BL_SKIP, xc->handle); /* temporary tag */ + hlen = ftello(xc->handle); + fstWriterUint64(xc->handle, 0); /* section length */ + fstWriterUint64(xc->handle, xc->hier_file_len); /* uncompressed length */ + + if(!xc->fourpack) + { + unsigned char *mem = (unsigned char *)malloc(FST_GZIO_LEN); + zfd = dup(fileno(xc->handle)); + fflush(xc->handle); + zhandle = gzdopen(zfd, "wb4"); + if(zhandle) + { + fstWriterFseeko(xc, xc->hier_handle, 0, SEEK_SET); + for(hl = 0; hl < xc->hier_file_len; hl += FST_GZIO_LEN) + { + unsigned len = ((xc->hier_file_len - hl) > FST_GZIO_LEN) ? FST_GZIO_LEN : (xc->hier_file_len - hl); + fstFread(mem, len, 1, xc->hier_handle); + gzwrite(zhandle, mem, len); + } + gzclose(zhandle); + } + else + { + close(zfd); + } + free(mem); + } + else + { + int lz4_maxlen; + unsigned char *mem; + unsigned char *hmem = NULL; + int packed_len; + + fflush(xc->handle); + + lz4_maxlen = LZ4_compressBound(xc->hier_file_len); + mem = (unsigned char *)malloc(lz4_maxlen); + errno = 0; + if(xc->hier_file_len) + { + fstWriterMmapSanity(hmem = (unsigned char *)fstMmap(NULL, xc->hier_file_len, PROT_READ|PROT_WRITE, MAP_SHARED, fileno(xc->hier_handle), 0), __FILE__, __LINE__, "hmem"); + } + packed_len = LZ4_compress_default((char *)hmem, (char *)mem, xc->hier_file_len, lz4_maxlen); + fstMunmap(hmem, xc->hier_file_len); + + fourpack_duo = (!xc->repack_on_close) && (xc->hier_file_len > FST_HDR_FOURPACK_DUO_SIZE); /* double pack when hierarchy is large */ + + if(fourpack_duo) /* double packing with LZ4 is faster than gzip */ + { + unsigned char *mem_duo; + int lz4_maxlen_duo; + int packed_len_duo; + + lz4_maxlen_duo = LZ4_compressBound(packed_len); + mem_duo = (unsigned char *)malloc(lz4_maxlen_duo); + packed_len_duo = LZ4_compress_default((char *)mem, (char *)mem_duo, packed_len, lz4_maxlen_duo); + + fstWriterVarint(xc->handle, packed_len); /* 1st round compressed length */ + fstFwrite(mem_duo, packed_len_duo, 1, xc->handle); + free(mem_duo); + } + else + { + fstFwrite(mem, packed_len, 1, xc->handle); + } + + free(mem); + } - fstWriterFseeko(xc, xc->handle, 0, SEEK_END); - eos = ftello(xc->handle); - fstWriterFseeko(xc, xc->handle, hlen, SEEK_SET); - fstWriterUint64(xc->handle, eos - hlen); - fflush(xc->handle); + fstWriterFseeko(xc, xc->handle, 0, SEEK_END); + eos = ftello(xc->handle); + fstWriterFseeko(xc, xc->handle, hlen, SEEK_SET); + fstWriterUint64(xc->handle, eos - hlen); + fflush(xc->handle); - fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); - fputc(xc->fourpack ? (fourpack_duo ? FST_BL_HIER_LZ4DUO : FST_BL_HIER_LZ4) : FST_BL_HIER, - xc->handle); /* actual tag now also == compression type */ + fstWriterFseeko(xc, xc->handle, fixup_offs, SEEK_SET); + fputc(xc->fourpack ? + ( fourpack_duo ? FST_BL_HIER_LZ4DUO : FST_BL_HIER_LZ4) : + FST_BL_HIER, xc->handle); /* actual tag now also == compression type */ - fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ - fflush(xc->handle); + fstWriterFseeko(xc, xc->handle, 0, SEEK_END); /* move file pointer to end for any section adds */ + fflush(xc->handle); #ifndef __MINGW32__ - snprintf(fnam, fnam_size, "%s.hier", xc->filename); - unlink(fnam); - free(fnam); + snprintf(fnam, fnam_len, "%s.hier", xc->filename); + unlink(fnam); + free(fnam); #endif - } + } /* finalize out header */ fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_START_TIME, SEEK_SET); @@ -2010,87 +2178,92 @@ void fstWriterClose(void *ctx) fflush(xc->handle); tmpfile_close(&xc->tchn_handle, &xc->tchn_handle_nam); - free(xc->vchg_mem); - xc->vchg_mem = NULL; + free(xc->vchg_mem); xc->vchg_mem = NULL; tmpfile_close(&xc->curval_handle, &xc->curval_handle_nam); tmpfile_close(&xc->valpos_handle, &xc->valpos_handle_nam); tmpfile_close(&xc->geom_handle, &xc->geom_handle_nam); - if (xc->hier_handle) { - fclose(xc->hier_handle); - xc->hier_handle = NULL; - } - if (xc->handle) { - if (xc->repack_on_close) { - FILE *fp; - fst_off_t offpnt, uclen; - int flen = strlen(xc->filename); - char *hf = (char *)calloc(1, flen + 5); - - strcpy(hf, xc->filename); - strcpy(hf + flen, ".pak"); - fp = fopen(hf, "wb"); - - if (fp) { - gzFile dsth; - int zfd; - char gz_membuf[FST_GZIO_LEN]; - - fstWriterFseeko(xc, xc->handle, 0, SEEK_END); - uclen = ftello(xc->handle); - - fputc(FST_BL_ZWRAPPER, fp); - fstWriterUint64(fp, 0); - fstWriterUint64(fp, uclen); - fflush(fp); - - fstWriterFseeko(xc, xc->handle, 0, SEEK_SET); - zfd = dup(fileno(fp)); - dsth = gzdopen(zfd, "wb4"); - if (dsth) { - for (offpnt = 0; offpnt < uclen; offpnt += FST_GZIO_LEN) { - size_t this_len = ((uclen - offpnt) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - offpnt); - fstFread(gz_membuf, this_len, 1, xc->handle); - gzwrite(dsth, gz_membuf, this_len); + if(xc->hier_handle) { fclose(xc->hier_handle); xc->hier_handle = NULL; } + if(xc->handle) + { + if(xc->repack_on_close) + { + FILE *fp; + fst_off_t offpnt, uclen; + int flen = strlen(xc->filename); + char *hf = (char *)calloc(1, flen + 5); + + strcpy(hf, xc->filename); + strcpy(hf+flen, ".pak"); + fp = fopen(hf, "wb"); + + if(fp) + { + gzFile dsth; + int zfd; + char gz_membuf[FST_GZIO_LEN]; + + fstWriterFseeko(xc, xc->handle, 0, SEEK_END); + uclen = ftello(xc->handle); + + fputc(FST_BL_ZWRAPPER, fp); + fstWriterUint64(fp, 0); + fstWriterUint64(fp, uclen); + fflush(fp); + + fstWriterFseeko(xc, xc->handle, 0, SEEK_SET); + zfd = dup(fileno(fp)); + dsth = gzdopen(zfd, "wb4"); + if(dsth) + { + for(offpnt = 0; offpnt < uclen; offpnt += FST_GZIO_LEN) + { + size_t this_len = ((uclen - offpnt) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - offpnt); + fstFread(gz_membuf, this_len, 1, xc->handle); + gzwrite(dsth, gz_membuf, this_len); + } + gzclose(dsth); + } + else + { + close(zfd); + } + fstWriterFseeko(xc, fp, 0, SEEK_END); + offpnt = ftello(fp); + fstWriterFseeko(xc, fp, 1, SEEK_SET); + fstWriterUint64(fp, offpnt - 1); + fclose(fp); + fclose(xc->handle); xc->handle = NULL; + + unlink(xc->filename); + rename(hf, xc->filename); + } + else + { + xc->repack_on_close = 0; + fclose(xc->handle); xc->handle = NULL; + } + + free(hf); + } + else + { + fclose(xc->handle); xc->handle = NULL; } - gzclose(dsth); - } else { - close(zfd); - } - fstWriterFseeko(xc, fp, 0, SEEK_END); - offpnt = ftello(fp); - fstWriterFseeko(xc, fp, 1, SEEK_SET); - fstWriterUint64(fp, offpnt - 1); - fclose(fp); - fclose(xc->handle); - xc->handle = NULL; - - unlink(xc->filename); - rename(hf, xc->filename); - } else { - xc->repack_on_close = 0; - fclose(xc->handle); - xc->handle = NULL; } - free(hf); - } else { - fclose(xc->handle); - xc->handle = NULL; - } - } - #ifdef __MINGW32__ { - int flen = strlen(xc->filename); - char *hf = (char *)calloc(1, flen + 6); - strcpy(hf, xc->filename); + int flen = strlen(xc->filename); + char *hf = (char *)calloc(1, flen + 6); + strcpy(hf, xc->filename); - if (xc->compress_hier) { + if(xc->compress_hier) + { strcpy(hf + flen, ".hier"); unlink(hf); /* no longer needed as a section now exists for this */ - } + } - free(hf); + free(hf); } #endif @@ -2099,26 +2272,28 @@ void fstWriterClose(void *ctx) pthread_attr_destroy(&xc->thread_attr); #endif - if (xc->path_array) { + if(xc->path_array) + { #ifndef _WAVE_HAVE_JUDY - const uint32_t hashmask = FST_PATH_HASHMASK; + const uint32_t hashmask = FST_PATH_HASHMASK; #endif - JudyHSFreeArray(&(xc->path_array), NULL); - } + JudyHSFreeArray(&(xc->path_array), NULL); + } - free(xc->filename); - xc->filename = NULL; + free(xc->filename); xc->filename = NULL; free(xc); - } + } } + /* * functions to set miscellaneous header/block information */ void fstWriterSetDate(void *ctx, const char *dat) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { char s[FST_HDR_DATE_SIZE]; fst_off_t fpos = ftello(xc->handle); int len = strlen(dat); @@ -2129,13 +2304,15 @@ void fstWriterSetDate(void *ctx, const char *dat) fstFwrite(s, FST_HDR_DATE_SIZE, 1, xc->handle); fflush(xc->handle); fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); - } + } } + void fstWriterSetVersion(void *ctx, const char *vers) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc && vers) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc && vers) + { char s[FST_HDR_SIM_VERSION_SIZE]; fst_off_t fpos = ftello(xc->handle); int len = strlen(vers); @@ -2146,867 +2323,972 @@ void fstWriterSetVersion(void *ctx, const char *vers) fstFwrite(s, FST_HDR_SIM_VERSION_SIZE, 1, xc->handle); fflush(xc->handle); fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); - } + } } + void fstWriterSetFileType(void *ctx, enum fstFileType filetype) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { - if (/*(filetype >= FST_FT_MIN) &&*/ (filetype <= FST_FT_MAX)) { - fst_off_t fpos = ftello(xc->handle); +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { + if(/*(filetype >= FST_FT_MIN) &&*/ (filetype <= FST_FT_MAX)) + { + fst_off_t fpos = ftello(xc->handle); - xc->filetype = filetype; + xc->filetype = filetype; - fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_FILETYPE, SEEK_SET); - fputc(xc->filetype, xc->handle); - fflush(xc->handle); - fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); + fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_FILETYPE, SEEK_SET); + fputc(xc->filetype, xc->handle); + fflush(xc->handle); + fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); + } } - } } + static void fstWriterSetAttrDoubleArgGeneric(void *ctx, int typ, uint64_t arg1, uint64_t arg2) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { unsigned char buf[11]; /* ceil(64/7) = 10 + null term */ unsigned char *pnt = fstCopyVarint64ToRight(buf, arg1); - if (arg1) { - *pnt = 0; /* this converts any *nonzero* arg1 when made a varint into a null-term string */ - } + if(arg1) + { + *pnt = 0; /* this converts any *nonzero* arg1 when made a varint into a null-term string */ + } fstWriterSetAttrBegin(xc, FST_AT_MISC, typ, (char *)buf, arg2); - } + } } + static void fstWriterSetAttrGeneric(void *ctx, const char *comm, int typ, uint64_t arg) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc && comm) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc && comm) + { char *s = strdup(comm); char *sf = s; - while (*s) { - if ((*s == '\n') || (*s == '\r')) - *s = ' '; - s++; - } + while(*s) + { + if((*s == '\n') || (*s == '\r')) *s = ' '; + s++; + } fstWriterSetAttrBegin(xc, FST_AT_MISC, typ, sf, arg); free(sf); - } + } } + static void fstWriterSetSourceStem_2(void *ctx, const char *path, unsigned int line, unsigned int use_realpath, int typ) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc && path && path[0]) { +if(xc && path && path[0]) + { uint64_t sidx = 0; int slen = strlen(path); #ifndef _WAVE_HAVE_JUDY const uint32_t hashmask = FST_PATH_HASHMASK; const unsigned char *path2 = (const unsigned char *)path; - PPvoid_t pv; + PPvoid_t pv; #else char *path2 = (char *)alloca(slen + 1); /* judy lacks const qualifier in its JudyHSIns definition */ - PPvoid_t pv; + PPvoid_t pv; strcpy(path2, path); #endif pv = JudyHSIns(&(xc->path_array), path2, slen, NULL); - if (*pv) { - sidx = (intptr_t)(*pv); - } else { - char *rp = NULL; + if(*pv) + { + sidx = (intptr_t)(*pv); + } + else + { + char *rp = NULL; - sidx = ++xc->path_array_count; - *pv = (void *)(intptr_t)(xc->path_array_count); + sidx = ++xc->path_array_count; + *pv = (void *)(intptr_t)(xc->path_array_count); - if (use_realpath) { - rp = fstRealpath( + if(use_realpath) + { + rp = fstRealpath( #ifndef _WAVE_HAVE_JUDY - (const char *) + (const char *) #endif - path2, - NULL); - } + path2, NULL); + } - fstWriterSetAttrGeneric(xc, - rp ? rp : + fstWriterSetAttrGeneric(xc, rp ? rp : #ifndef _WAVE_HAVE_JUDY - (const char *) + (const char *) #endif - path2, - FST_MT_PATHNAME, sidx); + path2, FST_MT_PATHNAME, sidx); - if (rp) { - free(rp); - } - } + if(rp) + { + free(rp); + } + } fstWriterSetAttrDoubleArgGeneric(xc, typ, sidx, line); - } + } } + void fstWriterSetSourceStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath) { - fstWriterSetSourceStem_2(ctx, path, line, use_realpath, FST_MT_SOURCESTEM); +fstWriterSetSourceStem_2(ctx, path, line, use_realpath, FST_MT_SOURCESTEM); } + void fstWriterSetSourceInstantiationStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath) { - fstWriterSetSourceStem_2(ctx, path, line, use_realpath, FST_MT_SOURCEISTEM); +fstWriterSetSourceStem_2(ctx, path, line, use_realpath, FST_MT_SOURCEISTEM); } -void fstWriterSetComment(void *ctx, const char *comm) { fstWriterSetAttrGeneric(ctx, comm, FST_MT_COMMENT, 0); } -void fstWriterSetValueList(void *ctx, const char *vl) { fstWriterSetAttrGeneric(ctx, vl, FST_MT_VALUELIST, 0); } +void fstWriterSetComment(void *ctx, const char *comm) +{ +fstWriterSetAttrGeneric(ctx, comm, FST_MT_COMMENT, 0); +} + + +void fstWriterSetValueList(void *ctx, const char *vl) +{ +fstWriterSetAttrGeneric(ctx, vl, FST_MT_VALUELIST, 0); +} + + +void fstWriterSetEnvVar(void *ctx, const char *envvar) +{ +fstWriterSetAttrGeneric(ctx, envvar, FST_MT_ENVVAR, 0); +} -void fstWriterSetEnvVar(void *ctx, const char *envvar) { fstWriterSetAttrGeneric(ctx, envvar, FST_MT_ENVVAR, 0); } void fstWriterSetTimescale(void *ctx, int ts) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { fst_off_t fpos = ftello(xc->handle); fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_TIMESCALE, SEEK_SET); fputc(ts & 255, xc->handle); fflush(xc->handle); fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); - } + } } + void fstWriterSetTimescaleFromString(void *ctx, const char *s) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc && s) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc && s) + { int mat = 0; int seconds_exp = -9; int tv = atoi(s); const char *pnt = s; - while (*pnt) { - switch (*pnt) { - case 'm': - seconds_exp = -3; - mat = 1; - break; - case 'u': - seconds_exp = -6; - mat = 1; - break; - case 'n': - seconds_exp = -9; - mat = 1; - break; - case 'p': - seconds_exp = -12; - mat = 1; - break; - case 'f': - seconds_exp = -15; - mat = 1; - break; - case 'a': - seconds_exp = -18; - mat = 1; - break; - case 'z': - seconds_exp = -21; - mat = 1; - break; - case 's': - seconds_exp = 0; - mat = 1; - break; - default: - break; - } + while(*pnt) + { + switch(*pnt) + { + case 'm': seconds_exp = -3; mat = 1; break; + case 'u': seconds_exp = -6; mat = 1; break; + case 'n': seconds_exp = -9; mat = 1; break; + case 'p': seconds_exp = -12; mat = 1; break; + case 'f': seconds_exp = -15; mat = 1; break; + case 'a': seconds_exp = -18; mat = 1; break; + case 'z': seconds_exp = -21; mat = 1; break; + case 's': seconds_exp = 0; mat = 1; break; + default: break; + } - if (mat) - break; - pnt++; - } + if(mat) break; + pnt++; + } - if (tv == 10) { - seconds_exp++; - } else if (tv == 100) { - seconds_exp += 2; - } + if(tv == 10) + { + seconds_exp++; + } + else + if(tv == 100) + { + seconds_exp+=2; + } fstWriterSetTimescale(ctx, seconds_exp); - } + } } + void fstWriterSetTimezero(void *ctx, int64_t tim) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { fst_off_t fpos = ftello(xc->handle); fstWriterFseeko(xc, xc->handle, FST_HDR_OFFS_TIMEZERO, SEEK_SET); fstWriterUint64(xc->handle, (xc->timezero = tim)); fflush(xc->handle); fstWriterFseeko(xc, xc->handle, fpos, SEEK_SET); - } + } } + void fstWriterSetPackType(void *ctx, enum fstWriterPackType typ) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { - xc->fastpack = (typ != FST_WR_PT_ZLIB); - xc->fourpack = (typ == FST_WR_PT_LZ4); - } +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { + xc->fastpack = (typ != FST_WR_PT_ZLIB); + xc->fourpack = (typ == FST_WR_PT_LZ4); + } } + void fstWriterSetRepackOnClose(void *ctx, int enable) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { xc->repack_on_close = (enable != 0); - } + } } + void fstWriterSetParallelMode(void *ctx, int enable) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { xc->parallel_was_enabled |= xc->parallel_enabled; /* make sticky */ xc->parallel_enabled = (enable != 0); #ifndef FST_WRITER_PARALLEL - if (xc->parallel_enabled) { - fprintf(stderr, FST_APIMESS - "fstWriterSetParallelMode(), FST_WRITER_PARALLEL not enabled during compile, exiting.\n"); - exit(255); - } + if(xc->parallel_enabled) + { + fprintf(stderr, FST_APIMESS "fstWriterSetParallelMode(), FST_WRITER_PARALLEL not enabled during compile, exiting.\n"); + exit(255); + } #endif - } + } } + void fstWriterSetDumpSizeLimit(void *ctx, uint64_t numbytes) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { xc->dump_size_limit = numbytes; - } + } } + int fstWriterGetDumpSizeLimitReached(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { - return (xc->size_limit_locked != 0); - } +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { + return(xc->size_limit_locked != 0); + } - return (0); +return(0); } + int fstWriterGetFseekFailed(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { - return (xc->fseek_failed != 0); - } +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc) + { + return(xc->fseek_failed != 0); + } - return (0); +return(0); } + /* * writer attr/scope/var creation: * fstWriterCreateVar2() is used to dump VHDL or other languages, but the * underlying variable needs to map to Verilog/SV via the proper fstVarType vt */ -fstHandle fstWriterCreateVar2(void *ctx, enum fstVarType vt, enum fstVarDir vd, uint32_t len, const char *nam, - fstHandle aliasHandle, const char *type, enum fstSupplementalVarType svt, - enum fstSupplementalDataType sdt) +fstHandle fstWriterCreateVar2(void *ctx, enum fstVarType vt, enum fstVarDir vd, + uint32_t len, const char *nam, fstHandle aliasHandle, + const char *type, enum fstSupplementalVarType svt, enum fstSupplementalDataType sdt) { - fstWriterSetAttrGeneric(ctx, type ? type : "", FST_MT_SUPVAR, - (svt << FST_SDT_SVT_SHIFT_COUNT) | (sdt & FST_SDT_ABS_MAX)); - return (fstWriterCreateVar(ctx, vt, vd, len, nam, aliasHandle)); +fstWriterSetAttrGeneric(ctx, type ? type : "", FST_MT_SUPVAR, (svt<valpos_mem) { - fstDestroyMmaps(xc, 0); - } +fstHandle fstWriterCreateVar(void *ctx, enum fstVarType vt, enum fstVarDir vd, + uint32_t len, const char *nam, fstHandle aliasHandle) +{ +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +unsigned int i; +int nlen, is_real; + +if(xc && nam) + { + if(xc->valpos_mem) + { + fstDestroyMmaps(xc, 0); + } fputc(vt, xc->hier_handle); fputc(vd, xc->hier_handle); nlen = strlen(nam); fstFwrite(nam, nlen, 1, xc->hier_handle); fputc(0, xc->hier_handle); - xc->hier_file_len += (nlen + 3); + xc->hier_file_len += (nlen+3); - if ((vt == FST_VT_VCD_REAL) || (vt == FST_VT_VCD_REAL_PARAMETER) || (vt == FST_VT_VCD_REALTIME) || - (vt == FST_VT_SV_SHORTREAL)) { - is_real = 1; - len = 8; /* recast number of bytes to that of what a double is */ - } else { - is_real = 0; - if (vt == FST_VT_GEN_STRING) { - len = 0; - } - } + if((vt == FST_VT_VCD_REAL) || (vt == FST_VT_VCD_REAL_PARAMETER) || (vt == FST_VT_VCD_REALTIME) || (vt == FST_VT_SV_SHORTREAL)) + { + is_real = 1; + len = 8; /* recast number of bytes to that of what a double is */ + } + else + { + is_real = 0; + if(vt == FST_VT_GEN_STRING) + { + len = 0; + } + } xc->hier_file_len += fstWriterVarint(xc->hier_handle, len); - if (aliasHandle > xc->maxhandle) - aliasHandle = 0; + if(aliasHandle > xc->maxhandle) aliasHandle = 0; xc->hier_file_len += fstWriterVarint(xc->hier_handle, aliasHandle); xc->numsigs++; - if (xc->numsigs == xc->next_huge_break) { - if (xc->fst_break_size < xc->fst_huge_break_size) { - xc->next_huge_break += FST_ACTIVATE_HUGE_INC; - xc->fst_break_size += xc->fst_orig_break_size; - xc->fst_break_add_size += xc->fst_orig_break_add_size; - - xc->vchg_alloc_siz = xc->fst_break_size + xc->fst_break_add_size; - if (xc->vchg_mem) { - xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); + if(xc->numsigs == xc->next_huge_break) + { + if(xc->fst_break_size < xc->fst_huge_break_size) + { + xc->next_huge_break += FST_ACTIVATE_HUGE_INC; + xc->fst_break_size += xc->fst_orig_break_size; + xc->fst_break_add_size += xc->fst_orig_break_add_size; + + xc->vchg_alloc_siz = xc->fst_break_size + xc->fst_break_add_size; + if(xc->vchg_mem) + { + xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); + } + } } - } - } - if (!aliasHandle) { - uint32_t zero = 0; + if(!aliasHandle) + { + uint32_t zero = 0; - if (len) { - fstWriterVarint(xc->geom_handle, !is_real ? len : 0); /* geom section encodes reals as zero byte */ - } else { - fstWriterVarint(xc->geom_handle, 0xFFFFFFFF); /* geom section encodes zero len as 32b -1 */ - } + if(len) + { + fstWriterVarint(xc->geom_handle, !is_real ? len : 0); /* geom section encodes reals as zero byte */ + } + else + { + fstWriterVarint(xc->geom_handle, 0xFFFFFFFF); /* geom section encodes zero len as 32b -1 */ + } - fstFwrite(&xc->maxvalpos, sizeof(uint32_t), 1, xc->valpos_handle); - fstFwrite(&len, sizeof(uint32_t), 1, xc->valpos_handle); - fstFwrite(&zero, sizeof(uint32_t), 1, xc->valpos_handle); - fstFwrite(&zero, sizeof(uint32_t), 1, xc->valpos_handle); + fstFwrite(&xc->maxvalpos, sizeof(uint32_t), 1, xc->valpos_handle); + fstFwrite(&len, sizeof(uint32_t), 1, xc->valpos_handle); + fstFwrite(&zero, sizeof(uint32_t), 1, xc->valpos_handle); + fstFwrite(&zero, sizeof(uint32_t), 1, xc->valpos_handle); - if (!is_real) { - for (i = 0; i < len; i++) { - fputc('x', xc->curval_handle); - } - } else { - fstFwrite(&xc->nan, 8, 1, xc->curval_handle); /* initialize doubles to NaN rather than x */ - } + if(!is_real) + { + for(i=0;icurval_handle); + } + } + else + { + fstFwrite(&xc->nan, 8, 1, xc->curval_handle); /* initialize doubles to NaN rather than x */ + } - xc->maxvalpos += len; - xc->maxhandle++; - return (xc->maxhandle); - } else { - return (aliasHandle); + xc->maxvalpos+=len; + xc->maxhandle++; + return(xc->maxhandle); + } + else + { + return(aliasHandle); + } } - } - return (0); +return(0); } -void fstWriterSetScope(void *ctx, enum fstScopeType scopetype, const char *scopename, const char *scopecomp) + +void fstWriterSetScope(void *ctx, enum fstScopeType scopetype, + const char *scopename, const char *scopecomp) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { fputc(FST_ST_VCD_SCOPE, xc->hier_handle); - if (/*(scopetype < FST_ST_VCD_MODULE) ||*/ (scopetype > FST_ST_MAX)) { - scopetype = FST_ST_VCD_MODULE; - } + if(/*(scopetype < FST_ST_VCD_MODULE) ||*/ (scopetype > FST_ST_MAX)) { scopetype = FST_ST_VCD_MODULE; } fputc(scopetype, xc->hier_handle); - fprintf(xc->hier_handle, "%s%c%s%c", scopename ? scopename : "", 0, scopecomp ? scopecomp : "", 0); + fprintf(xc->hier_handle, "%s%c%s%c", + scopename ? scopename : "", 0, + scopecomp ? scopecomp : "", 0); - if (scopename) { - xc->hier_file_len += strlen(scopename); - } - if (scopecomp) { - xc->hier_file_len += strlen(scopecomp); - } + if(scopename) + { + xc->hier_file_len += strlen(scopename); + } + if(scopecomp) + { + xc->hier_file_len += strlen(scopecomp); + } xc->hier_file_len += 4; /* FST_ST_VCD_SCOPE + scopetype + two string terminating zeros */ xc->numscopes++; - } + } } + void fstWriterSetUpscope(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { fputc(FST_ST_VCD_UPSCOPE, xc->hier_handle); xc->hier_file_len++; - } + } } -void fstWriterSetAttrBegin(void *ctx, enum fstAttrType attrtype, int subtype, const char *attrname, uint64_t arg) + +void fstWriterSetAttrBegin(void *ctx, enum fstAttrType attrtype, int subtype, + const char *attrname, uint64_t arg) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { fputc(FST_ST_GEN_ATTRBEGIN, xc->hier_handle); - if (/*(attrtype < FST_AT_MISC) ||*/ (attrtype > FST_AT_MAX)) { - attrtype = FST_AT_MISC; - subtype = FST_MT_UNKNOWN; - } + if(/*(attrtype < FST_AT_MISC) ||*/ (attrtype > FST_AT_MAX)) { attrtype = FST_AT_MISC; subtype = FST_MT_UNKNOWN; } fputc(attrtype, xc->hier_handle); - switch (attrtype) { - case FST_AT_ARRAY: - if ((subtype < FST_AR_NONE) || (subtype > FST_AR_MAX)) - subtype = FST_AR_NONE; - break; - case FST_AT_ENUM: - if ((subtype < FST_EV_SV_INTEGER) || (subtype > FST_EV_MAX)) - subtype = FST_EV_SV_INTEGER; - break; - case FST_AT_PACK: - if ((subtype < FST_PT_NONE) || (subtype > FST_PT_MAX)) - subtype = FST_PT_NONE; - break; - - case FST_AT_MISC: - default: - break; - } + switch(attrtype) + { + case FST_AT_ARRAY: if((subtype < FST_AR_NONE) || (subtype > FST_AR_MAX)) subtype = FST_AR_NONE; break; + case FST_AT_ENUM: if((subtype < FST_EV_SV_INTEGER) || (subtype > FST_EV_MAX)) subtype = FST_EV_SV_INTEGER; break; + case FST_AT_PACK: if((subtype < FST_PT_NONE) || (subtype > FST_PT_MAX)) subtype = FST_PT_NONE; break; + + case FST_AT_MISC: + default: break; + } fputc(subtype, xc->hier_handle); - fprintf(xc->hier_handle, "%s%c", attrname ? attrname : "", 0); + fprintf(xc->hier_handle, "%s%c", + attrname ? attrname : "", 0); - if (attrname) { - xc->hier_file_len += strlen(attrname); - } + if(attrname) + { + xc->hier_file_len += strlen(attrname); + } xc->hier_file_len += 4; /* FST_ST_GEN_ATTRBEGIN + type + subtype + string terminating zero */ xc->hier_file_len += fstWriterVarint(xc->hier_handle, arg); - } + } } + void fstWriterSetAttrEnd(void *ctx) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { fputc(FST_ST_GEN_ATTREND, xc->hier_handle); xc->hier_file_len++; - } + } } -fstEnumHandle fstWriterCreateEnumTable(void *ctx, const char *name, uint32_t elem_count, unsigned int min_valbits, - const char **literal_arr, const char **val_arr) + +fstEnumHandle fstWriterCreateEnumTable(void *ctx, const char *name, uint32_t elem_count, unsigned int min_valbits, const char **literal_arr, const char **val_arr) { - fstEnumHandle handle = 0; - unsigned int *literal_lens = NULL; - unsigned int *val_lens = NULL; - int lit_len_tot = 0; - int val_len_tot = 0; - int name_len; - char elem_count_buf[16]; - int elem_count_len; - int total_len; - int pos = 0; - char *attr_str = NULL; +fstEnumHandle handle = 0; +unsigned int *literal_lens = NULL; +unsigned int *val_lens = NULL; +int lit_len_tot = 0; +int val_len_tot = 0; +int name_len; +char elem_count_buf[16]; +int elem_count_len; +int total_len; +int pos = 0; +char *attr_str = NULL; - if (ctx && name && literal_arr && val_arr && (elem_count != 0)) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(ctx && name && literal_arr && val_arr && (elem_count != 0)) + { + struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - uint32_t i; + uint32_t i; - name_len = strlen(name); - elem_count_len = snprintf(elem_count_buf, sizeof(elem_count_buf), "%" PRIu32, elem_count); + name_len = strlen(name); + elem_count_len = snprintf(elem_count_buf, 16, "%" PRIu32, elem_count); - literal_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int)); - val_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int)); + literal_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int)); + val_lens = (unsigned int *)calloc(elem_count, sizeof(unsigned int)); - for (i = 0; i < elem_count; i++) { - literal_lens[i] = strlen(literal_arr[i]); - lit_len_tot += fstUtilityBinToEscConvertedLen((unsigned char *)literal_arr[i], literal_lens[i]); + for(i=0;i 0) { - if (val_lens[i] < min_valbits) { - val_len_tot += (min_valbits - val_lens[i]); /* additional converted len is same for '0' character */ - } - } - } + if(min_valbits > 0) + { + if(val_lens[i] < min_valbits) + { + val_len_tot += (min_valbits - val_lens[i]); /* additional converted len is same for '0' character */ + } + } + } - total_len = name_len + 1 + elem_count_len + 1 + lit_len_tot + elem_count + val_len_tot + elem_count; + total_len = name_len + 1 + elem_count_len + 1 + lit_len_tot + elem_count + val_len_tot + elem_count; - attr_str = (char *)malloc(total_len); - pos = 0; + attr_str = (char*)malloc(total_len); + pos = 0; - memcpy(attr_str + pos, name, name_len); - pos += name_len; - attr_str[pos++] = ' '; + memcpy(attr_str+pos, name, name_len); + pos += name_len; + attr_str[pos++] = ' '; - memcpy(attr_str + pos, elem_count_buf, elem_count_len); - pos += elem_count_len; - attr_str[pos++] = ' '; + memcpy(attr_str+pos, elem_count_buf, elem_count_len); + pos += elem_count_len; + attr_str[pos++] = ' '; - for (i = 0; i < elem_count; i++) { - pos += fstUtilityBinToEsc((unsigned char *)attr_str + pos, (unsigned char *)literal_arr[i], - literal_lens[i]); - attr_str[pos++] = ' '; - } + for(i=0;i 0) { - if (val_lens[i] < min_valbits) { - memset(attr_str + pos, '0', min_valbits - val_lens[i]); - pos += (min_valbits - val_lens[i]); - } - } + for(i=0;i 0) + { + if(val_lens[i] < min_valbits) + { + memset(attr_str+pos, '0', min_valbits - val_lens[i]); + pos += (min_valbits - val_lens[i]); + } + } - pos += fstUtilityBinToEsc((unsigned char *)attr_str + pos, (unsigned char *)val_arr[i], val_lens[i]); - attr_str[pos++] = ' '; - } + pos += fstUtilityBinToEsc((unsigned char*)attr_str+pos, (unsigned char*)val_arr[i], val_lens[i]); + attr_str[pos++] = ' '; + } - attr_str[pos - 1] = 0; + attr_str[pos-1] = 0; #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "fstWriterCreateEnumTable() total_len: %d, pos: %d\n", total_len, pos); - fprintf(stderr, FST_APIMESS "*%s*\n", attr_str); + fprintf(stderr, FST_APIMESS "fstWriterCreateEnumTable() total_len: %d, pos: %d\n", total_len, pos); + fprintf(stderr, FST_APIMESS "*%s*\n", attr_str); #endif - fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, attr_str, handle = ++xc->max_enumhandle); + fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, attr_str, handle = ++xc->max_enumhandle); - free(attr_str); - free(val_lens); - free(literal_lens); - } + free(attr_str); + free(val_lens); + free(literal_lens); + } - return (handle); +return(handle); } + void fstWriterEmitEnumTableRef(void *ctx, fstEnumHandle handle) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc && handle) { - fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, NULL, handle); - } +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +if(xc && handle) + { + fstWriterSetAttrBegin(xc, FST_AT_MISC, FST_MT_ENUMTABLE, NULL, handle); + } } + /* * value and time change emission */ void fstWriterEmitValueChange(void *ctx, fstHandle handle, const void *val) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - const unsigned char *buf = (const unsigned char *)val; - uint32_t offs; - int len; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +const unsigned char *buf = (const unsigned char *)val; +uint32_t offs; +int len; - if (FST_LIKELY((xc) && (handle <= xc->maxhandle))) { +if(FST_LIKELY((xc) && (handle <= xc->maxhandle))) + { uint32_t fpos; uint32_t *vm4ip; - if (FST_UNLIKELY(!xc->valpos_mem)) { - xc->vc_emitted = 1; - fstWriterCreateMmaps(xc); - } + if(FST_UNLIKELY(!xc->valpos_mem)) + { + xc->vc_emitted = 1; + fstWriterCreateMmaps(xc); + } handle--; /* move starting at 1 index to starting at 0 */ - vm4ip = &(xc->valpos_mem[4 * handle]); + vm4ip = &(xc->valpos_mem[4*handle]); - len = vm4ip[1]; - if (FST_LIKELY(len)) /* len of zero = variable length, use fstWriterEmitVariableLengthValueChange */ - { - if (FST_LIKELY(!xc->is_initial_time)) { - fpos = xc->vchg_siz; - - if (FST_UNLIKELY((fpos + len + 10) > xc->vchg_alloc_siz)) { - xc->vchg_alloc_siz += - (xc->fst_break_add_size + - len); /* +len added in the case of extremely long vectors and small break add sizes */ - xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); - if (FST_UNLIKELY(!xc->vchg_mem)) { - fprintf(stderr, FST_APIMESS "Could not realloc() in fstWriterEmitValueChange, exiting.\n"); - exit(255); - } - } + len = vm4ip[1]; + if(FST_LIKELY(len)) /* len of zero = variable length, use fstWriterEmitVariableLengthValueChange */ + { + if(FST_LIKELY(!xc->is_initial_time)) + { + fpos = xc->vchg_siz; + + if(FST_UNLIKELY((fpos + len + 10) > xc->vchg_alloc_siz)) + { + xc->vchg_alloc_siz += (xc->fst_break_add_size + len); /* +len added in the case of extremely long vectors and small break add sizes */ + xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); + if(FST_UNLIKELY(!xc->vchg_mem)) + { + fprintf(stderr, FST_APIMESS "Could not realloc() in fstWriterEmitValueChange, exiting.\n"); + exit(255); + } + } #ifdef FST_REMOVE_DUPLICATE_VC - offs = vm4ip[0]; - - if (len != 1) { - if ((vm4ip[3] == xc->tchn_idx) && (vm4ip[2])) { - unsigned char *old_value = xc->vchg_mem + vm4ip[2] + 4; /* the +4 skips old vm4ip[2] value */ - while (*(old_value++) & 0x80) { /* skips over varint encoded "xc->tchn_idx - vm4ip[3]" */ - } - memcpy(old_value, buf, len); /* overlay new value */ + offs = vm4ip[0]; + + if(len != 1) + { + if((vm4ip[3]==xc->tchn_idx)&&(vm4ip[2])) + { + unsigned char *old_value = xc->vchg_mem + vm4ip[2] + 4; /* the +4 skips old vm4ip[2] value */ + while(*(old_value++) & 0x80) { /* skips over varint encoded "xc->tchn_idx - vm4ip[3]" */ } + memcpy(old_value, buf, len); /* overlay new value */ + + memcpy(xc->curval_mem + offs, buf, len); + return; + } + else + { + if(!memcmp(xc->curval_mem + offs, buf, len)) + { + if(!xc->curtime) + { + int i; + for(i=0;icurval_mem + offs, buf, len); - return; - } else { - if (!memcmp(xc->curval_mem + offs, buf, len)) { - if (!xc->curtime) { - int i; - for (i = 0; i < len; i++) { - if (buf[i] != 'x') - break; + memcpy(xc->curval_mem + offs, buf, len); } + else + { + if((vm4ip[3]==xc->tchn_idx)&&(vm4ip[2])) + { + unsigned char *old_value = xc->vchg_mem + vm4ip[2] + 4; /* the +4 skips old vm4ip[2] value */ + while(*(old_value++) & 0x80) { /* skips over varint encoded "xc->tchn_idx - vm4ip[3]" */ } + *old_value = *buf; /* overlay new value */ + + *(xc->curval_mem + offs) = *buf; + return; + } + else + { + if((*(xc->curval_mem + offs)) == (*buf)) + { + if(!xc->curtime) + { + if(*buf != 'x') return; + } + else + { + return; + } + } + } - if (i < len) - return; - } else { - return; - } + *(xc->curval_mem + offs) = *buf; + } +#endif + xc->vchg_siz += fstWriterUint32WithVarint32(xc, &vm4ip[2], xc->tchn_idx - vm4ip[3], buf, len); /* do one fwrite op only */ + vm4ip[3] = xc->tchn_idx; + vm4ip[2] = fpos; } - } - - memcpy(xc->curval_mem + offs, buf, len); - } else { - if ((vm4ip[3] == xc->tchn_idx) && (vm4ip[2])) { - unsigned char *old_value = xc->vchg_mem + vm4ip[2] + 4; /* the +4 skips old vm4ip[2] value */ - while (*(old_value++) & 0x80) { /* skips over varint encoded "xc->tchn_idx - vm4ip[3]" */ + else + { + offs = vm4ip[0]; + memcpy(xc->curval_mem + offs, buf, len); } - *old_value = *buf; /* overlay new value */ + } + } +} - *(xc->curval_mem + offs) = *buf; - return; - } else { - if ((*(xc->curval_mem + offs)) == (*buf)) { - if (!xc->curtime) { - if (*buf != 'x') - return; - } else { - return; - } +void fstWriterEmitValueChange32(void *ctx, fstHandle handle, + uint32_t bits, uint32_t val) { + char buf[32]; + char *s = buf; + uint32_t i; + for (i = 0; i < bits; ++i) + { + *s++ = '0' + ((val >> (bits - i - 1)) & 1); + } + fstWriterEmitValueChange(ctx, handle, buf); +} +void fstWriterEmitValueChange64(void *ctx, fstHandle handle, + uint32_t bits, uint64_t val) { + char buf[64]; + char *s = buf; + uint32_t i; + for (i = 0; i < bits; ++i) + { + *s++ = '0' + ((val >> (bits - i - 1)) & 1); + } + fstWriterEmitValueChange(ctx, handle, buf); +} +void fstWriterEmitValueChangeVec32(void *ctx, fstHandle handle, + uint32_t bits, const uint32_t *val) { + struct fstWriterContext *xc = (struct fstWriterContext *)ctx; + if (FST_UNLIKELY(bits <= 32)) + { + fstWriterEmitValueChange32(ctx, handle, bits, val[0]); + } + else if(FST_LIKELY(xc)) + { + int bq = bits / 32; + int br = bits & 31; + int i; + int w; + uint32_t v; + unsigned char* s; + if (FST_UNLIKELY(bits > xc->outval_alloc_siz)) + { + xc->outval_alloc_siz = bits*2 + 1; + xc->outval_mem = (unsigned char*)realloc(xc->outval_mem, xc->outval_alloc_siz); + if (FST_UNLIKELY(!xc->outval_mem)) + { + fprintf(stderr, + FST_APIMESS "Could not realloc() in fstWriterEmitValueChangeVec32, exiting.\n"); + exit(255); } - } - - *(xc->curval_mem + offs) = *buf; } -#endif - xc->vchg_siz += fstWriterUint32WithVarint32(xc, &vm4ip[2], xc->tchn_idx - vm4ip[3], buf, - len); /* do one fwrite op only */ - vm4ip[3] = xc->tchn_idx; - vm4ip[2] = fpos; - } else { - offs = vm4ip[0]; - memcpy(xc->curval_mem + offs, buf, len); - } - } - } -} - -void fstWriterEmitValueChange32(void *ctx, fstHandle handle, uint32_t bits, uint32_t val) -{ - char buf[32]; - char *s = buf; - uint32_t i; - for (i = 0; i < bits; ++i) { - *s++ = '0' + ((val >> (bits - i - 1)) & 1); - } - fstWriterEmitValueChange(ctx, handle, buf); -} -void fstWriterEmitValueChange64(void *ctx, fstHandle handle, uint32_t bits, uint64_t val) -{ - char buf[64]; - char *s = buf; - uint32_t i; - for (i = 0; i < bits; ++i) { - *s++ = '0' + ((val >> (bits - i - 1)) & 1); - } - fstWriterEmitValueChange(ctx, handle, buf); -} -void fstWriterEmitValueChangeVec32(void *ctx, fstHandle handle, uint32_t bits, const uint32_t *val) -{ - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (FST_UNLIKELY(bits <= 32)) { - fstWriterEmitValueChange32(ctx, handle, bits, val[0]); - } else if (FST_LIKELY(xc)) { - int bq = bits / 32; - int br = bits & 31; - int i; - int w; - uint32_t v; - unsigned char *s; - if (FST_UNLIKELY(bits > xc->outval_alloc_siz)) { - xc->outval_alloc_siz = bits * 2 + 1; - xc->outval_mem = (unsigned char *)realloc(xc->outval_mem, xc->outval_alloc_siz); - if (FST_UNLIKELY(!xc->outval_mem)) { - fprintf(stderr, FST_APIMESS "Could not realloc() in fstWriterEmitValueChangeVec32, exiting.\n"); - exit(255); - } - } - s = xc->outval_mem; - { - w = bq; - v = val[w]; - for (i = 0; i < br; ++i) { - *s++ = '0' + ((v >> (br - i - 1)) & 1); - } - } - for (w = bq - 1; w >= 0; --w) { - v = val[w]; - for (i = (32 - 4); i >= 0; i -= 4) { - s[0] = '0' + ((v >> (i + 3)) & 1); - s[1] = '0' + ((v >> (i + 2)) & 1); - s[2] = '0' + ((v >> (i + 1)) & 1); - s[3] = '0' + ((v >> (i + 0)) & 1); - s += 4; - } - } - fstWriterEmitValueChange(ctx, handle, xc->outval_mem); - } -} -void fstWriterEmitValueChangeVec64(void *ctx, fstHandle handle, uint32_t bits, const uint64_t *val) -{ - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (FST_UNLIKELY(bits <= 64)) { - fstWriterEmitValueChange64(ctx, handle, bits, val[0]); - } else if (FST_LIKELY(xc)) { - int bq = bits / 64; - int br = bits & 63; - int i; - int w; - uint32_t v; - unsigned char *s; - if (FST_UNLIKELY(bits > xc->outval_alloc_siz)) { - xc->outval_alloc_siz = bits * 2 + 1; - xc->outval_mem = (unsigned char *)realloc(xc->outval_mem, xc->outval_alloc_siz); - if (FST_UNLIKELY(!xc->outval_mem)) { - fprintf(stderr, FST_APIMESS "Could not realloc() in fstWriterEmitValueChangeVec64, exiting.\n"); - exit(255); - } + s = xc->outval_mem; + { + w = bq; + v = val[w]; + for (i = 0; i < br; ++i) + { + *s++ = '0' + ((v >> (br - i - 1)) & 1); + } + } + for (w = bq - 1; w >= 0; --w) + { + v = val[w]; + for (i = (32 - 4); i >= 0; i -= 4) { + s[0] = '0' + ((v >> (i + 3)) & 1); + s[1] = '0' + ((v >> (i + 2)) & 1); + s[2] = '0' + ((v >> (i + 1)) & 1); + s[3] = '0' + ((v >> (i + 0)) & 1); + s += 4; + } + } + fstWriterEmitValueChange(ctx, handle, xc->outval_mem); } - s = xc->outval_mem; +} +void fstWriterEmitValueChangeVec64(void *ctx, fstHandle handle, + uint32_t bits, const uint64_t *val) { + struct fstWriterContext *xc = (struct fstWriterContext *)ctx; + if (FST_UNLIKELY(bits <= 64)) { - w = bq; - v = val[w]; - for (i = 0; i < br; ++i) { - *s++ = '0' + ((v >> (br - i - 1)) & 1); - } + fstWriterEmitValueChange64(ctx, handle, bits, val[0]); } - for (w = bq - 1; w >= 0; --w) { - v = val[w]; - for (i = (64 - 4); i >= 0; i -= 4) { - s[0] = '0' + ((v >> (i + 3)) & 1); - s[1] = '0' + ((v >> (i + 2)) & 1); - s[2] = '0' + ((v >> (i + 1)) & 1); - s[3] = '0' + ((v >> (i + 0)) & 1); - s += 4; - } + else if(FST_LIKELY(xc)) + { + int bq = bits / 64; + int br = bits & 63; + int i; + int w; + uint32_t v; + unsigned char* s; + if (FST_UNLIKELY(bits > xc->outval_alloc_siz)) + { + xc->outval_alloc_siz = bits*2 + 1; + xc->outval_mem = (unsigned char*)realloc(xc->outval_mem, xc->outval_alloc_siz); + if (FST_UNLIKELY(!xc->outval_mem)) + { + fprintf(stderr, + FST_APIMESS "Could not realloc() in fstWriterEmitValueChangeVec64, exiting.\n"); + exit(255); + } + } + s = xc->outval_mem; + { + w = bq; + v = val[w]; + for (i = 0; i < br; ++i) + { + *s++ = '0' + ((v >> (br - i - 1)) & 1); + } + } + for (w = bq - 1; w >= 0; --w) { + v = val[w]; + for (i = (64 - 4); i >= 0; i -= 4) + { + s[0] = '0' + ((v >> (i + 3)) & 1); + s[1] = '0' + ((v >> (i + 2)) & 1); + s[2] = '0' + ((v >> (i + 1)) & 1); + s[3] = '0' + ((v >> (i + 0)) & 1); + s += 4; + } + } + fstWriterEmitValueChange(ctx, handle, xc->outval_mem); } - fstWriterEmitValueChange(ctx, handle, xc->outval_mem); - } } + void fstWriterEmitVariableLengthValueChange(void *ctx, fstHandle handle, const void *val, uint32_t len) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - const unsigned char *buf = (const unsigned char *)val; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +const unsigned char *buf = (const unsigned char *)val; - if (FST_LIKELY((xc) && (handle <= xc->maxhandle))) { +if(FST_LIKELY((xc) && (handle <= xc->maxhandle))) + { uint32_t fpos; uint32_t *vm4ip; - if (FST_UNLIKELY(!xc->valpos_mem)) { - xc->vc_emitted = 1; - fstWriterCreateMmaps(xc); - } + if(FST_UNLIKELY(!xc->valpos_mem)) + { + xc->vc_emitted = 1; + fstWriterCreateMmaps(xc); + } handle--; /* move starting at 1 index to starting at 0 */ - vm4ip = &(xc->valpos_mem[4 * handle]); + vm4ip = &(xc->valpos_mem[4*handle]); /* there is no initial time dump for variable length value changes */ - if (FST_LIKELY(!vm4ip[1])) /* len of zero = variable length */ - { - fpos = xc->vchg_siz; + if(FST_LIKELY(!vm4ip[1])) /* len of zero = variable length */ + { + fpos = xc->vchg_siz; - if (FST_UNLIKELY((fpos + len + 10 + 5) > xc->vchg_alloc_siz)) { - xc->vchg_alloc_siz += - (xc->fst_break_add_size + len + - 5); /* +len added in the case of extremely long vectors and small break add sizes */ - xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); - if (FST_UNLIKELY(!xc->vchg_mem)) { - fprintf(stderr, - FST_APIMESS "Could not realloc() in fstWriterEmitVariableLengthValueChange, exiting.\n"); - exit(255); - } - } + if(FST_UNLIKELY((fpos + len + 10 + 5) > xc->vchg_alloc_siz)) + { + xc->vchg_alloc_siz += (xc->fst_break_add_size + len + 5); /* +len added in the case of extremely long vectors and small break add sizes */ + xc->vchg_mem = (unsigned char *)realloc(xc->vchg_mem, xc->vchg_alloc_siz); + if(FST_UNLIKELY(!xc->vchg_mem)) + { + fprintf(stderr, FST_APIMESS "Could not realloc() in fstWriterEmitVariableLengthValueChange, exiting.\n"); + exit(255); + } + } - xc->vchg_siz += fstWriterUint32WithVarint32AndLength(xc, &vm4ip[2], xc->tchn_idx - vm4ip[3], buf, - len); /* do one fwrite op only */ - vm4ip[3] = xc->tchn_idx; - vm4ip[2] = fpos; + xc->vchg_siz += fstWriterUint32WithVarint32AndLength(xc, &vm4ip[2], xc->tchn_idx - vm4ip[3], buf, len); /* do one fwrite op only */ + vm4ip[3] = xc->tchn_idx; + vm4ip[2] = fpos; + } } - } } + void fstWriterEmitTimeChange(void *ctx, uint64_t tim) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - unsigned int i; - int skip = 0; - if (xc) { - if (FST_UNLIKELY(xc->is_initial_time)) { - if (xc->size_limit_locked) /* this resets xc->is_initial_time to one */ - { - return; - } - - if (!xc->valpos_mem) { - fstWriterCreateMmaps(xc); - } - - skip = 1; - - xc->firsttime = (xc->vc_emitted) ? 0 : tim; - xc->curtime = 0; - xc->vchg_mem[0] = '!'; - xc->vchg_siz = 1; - fstWriterEmitSectionHeader(xc); - for (i = 0; i < xc->maxhandle; i++) { - xc->valpos_mem[4 * i + 2] = 0; /* zero out offset val */ - xc->valpos_mem[4 * i + 3] = 0; /* zero out last time change val */ - } - xc->is_initial_time = 0; - } else { - if ((xc->vchg_siz >= xc->fst_break_size) || (xc->flush_context_pending)) { - xc->flush_context_pending = 0; - fstWriterFlushContextPrivate(xc); - xc->tchn_cnt++; - fstWriterVarint(xc->tchn_handle, xc->curtime); - } - } - - if (!skip) { - xc->tchn_idx++; - } +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +unsigned int i; +int skip = 0; +if(xc) + { + if(FST_UNLIKELY(xc->is_initial_time)) + { + if(xc->size_limit_locked) /* this resets xc->is_initial_time to one */ + { + return; + } + + if(!xc->valpos_mem) + { + fstWriterCreateMmaps(xc); + } + + skip = 1; + + xc->firsttime = (xc->vc_emitted) ? 0: tim; + xc->curtime = 0; + xc->vchg_mem[0] = '!'; + xc->vchg_siz = 1; + fstWriterEmitSectionHeader(xc); + for(i=0;imaxhandle;i++) + { + xc->valpos_mem[4*i+2] = 0; /* zero out offset val */ + xc->valpos_mem[4*i+3] = 0; /* zero out last time change val */ + } + xc->is_initial_time = 0; + } + else + { + if((xc->vchg_siz >= xc->fst_break_size) || (xc->flush_context_pending)) + { + xc->flush_context_pending = 0; + fstWriterFlushContextPrivate(xc); + xc->tchn_cnt++; + fstWriterVarint(xc->tchn_handle, xc->curtime); + } + } + + if(!skip) + { + xc->tchn_idx++; + } fstWriterVarint(xc->tchn_handle, tim - xc->curtime); xc->tchn_cnt++; xc->curtime = tim; - } + } } + void fstWriterEmitDumpActive(void *ctx, int enable) { - struct fstWriterContext *xc = (struct fstWriterContext *)ctx; +struct fstWriterContext *xc = (struct fstWriterContext *)ctx; - if (xc) { +if(xc) + { struct fstBlackoutChain *b = (struct fstBlackoutChain *)calloc(1, sizeof(struct fstBlackoutChain)); b->tim = xc->curtime; b->active = (enable != 0); xc->num_blackouts++; - if (xc->blackout_curr) { - xc->blackout_curr->next = b; - xc->blackout_curr = b; - } else { - xc->blackout_head = b; - xc->blackout_curr = b; + if(xc->blackout_curr) + { + xc->blackout_curr->next = b; + xc->blackout_curr = b; + } + else + { + xc->blackout_head = b; + xc->blackout_curr = b; + } } - } } + /***********************/ /*** ***/ /*** reader function ***/ @@ -3016,531 +3298,592 @@ void fstWriterEmitDumpActive(void *ctx, int enable) /* * private structs */ -static const char *vartypes[] = {"event", "integer", "parameter", "real", "real_parameter", "reg", "supply0", - "supply1", "time", "tri", "triand", "trior", "trireg", "tri0", - "tri1", "wand", "wire", "wor", "port", "sparray", "realtime", - "string", "bit", "logic", "int", "shortint", "longint", "byte", - "enum", "shortreal"}; - -static const char *modtypes[] = {"module", - "task", - "function", - "begin", - "fork", - "generate", - "struct", - "union", - "class", - "interface", - "package", - "program", - "vhdl_architecture", - "vhdl_procedure", - "vhdl_function", - "vhdl_record", - "vhdl_process", - "vhdl_block", - "vhdl_for_generate", - "vhdl_if_generate", - "vhdl_generate", - "vhdl_package"}; - -static const char *attrtypes[] = {"misc", "array", "enum", "class"}; - -static const char *arraytypes[] = {"none", "unpacked", "packed", "sparse"}; - -static const char *enumvaluetypes[] = {"integer", - "bit", - "logic", - "int", - "shortint", - "longint", - "byte", - "unsigned_integer", - "unsigned_bit", - "unsigned_logic", - "unsigned_int", - "unsigned_shortint", - "unsigned_longint", - "unsigned_byte"}; - -static const char *packtypes[] = {"none", "unpacked", "packed", "tagged_packed"}; +static const char *vartypes[] = { + "event", "integer", "parameter", "real", "real_parameter", + "reg", "supply0", "supply1", "time", "tri", + "triand", "trior", "trireg", "tri0", "tri1", + "wand", "wire", "wor", "port", "sparray", "realtime", + "string", + "bit", "logic", "int", "shortint", "longint", "byte", "enum", "shortreal" + }; + +static const char *modtypes[] = { + "module", "task", "function", "begin", "fork", "generate", "struct", "union", "class", "interface", "package", "program", + "vhdl_architecture", "vhdl_procedure", "vhdl_function", "vhdl_record", "vhdl_process", "vhdl_block", "vhdl_for_generate", "vhdl_if_generate", "vhdl_generate", "vhdl_package" + }; + +static const char *attrtypes[] = { + "misc", "array", "enum", "class" + }; + +static const char *arraytypes[] = { + "none", "unpacked", "packed", "sparse" + }; + +static const char *enumvaluetypes[] = { + "integer", "bit", "logic", "int", "shortint", "longint", "byte", + "unsigned_integer", "unsigned_bit", "unsigned_logic", "unsigned_int", "unsigned_shortint", "unsigned_longint", "unsigned_byte" + }; + +static const char *packtypes[] = { + "none", "unpacked", "packed", "tagged_packed" + }; + struct fstCurrHier { - struct fstCurrHier *prev; - void *user_info; - int len; +struct fstCurrHier *prev; +void *user_info; +int len; }; + struct fstReaderContext { - /* common entries */ - - FILE *f, *fh; - - uint64_t start_time, end_time; - uint64_t mem_used_by_writer; - uint64_t scope_count; - uint64_t var_count; - fstHandle maxhandle; - uint64_t num_alias; - uint64_t vc_section_count; - - uint32_t *signal_lens; /* maxhandle sized */ - unsigned char *signal_typs; /* maxhandle sized */ - unsigned char *process_mask; /* maxhandle-based, bitwise sized */ - uint32_t longest_signal_value_len; /* longest len value encountered */ - unsigned char *temp_signal_value_buf; /* malloced for len in longest_signal_value_len */ - - signed char timescale; - unsigned char filetype; - - unsigned use_vcd_extensions : 1; - unsigned double_endian_match : 1; - unsigned native_doubles_for_cb : 1; - unsigned contains_geom_section : 1; - unsigned contains_hier_section : 1; /* valid for hier_pos */ - unsigned contains_hier_section_lz4duo : 1; /* valid for hier_pos (contains_hier_section_lz4 always also set) */ - unsigned contains_hier_section_lz4 : 1; /* valid for hier_pos */ - unsigned limit_range_valid : 1; /* valid for limit_range_start, limit_range_end */ - - char version[FST_HDR_SIM_VERSION_SIZE + 1]; - char date[FST_HDR_DATE_SIZE + 1]; - int64_t timezero; - - char *filename, *filename_unpacked; - fst_off_t hier_pos; - - uint32_t num_blackouts; - uint64_t *blackout_times; - unsigned char *blackout_activity; - - uint64_t limit_range_start, limit_range_end; - - /* entries specific to read value at time functions */ - - unsigned rvat_data_valid : 1; - uint64_t *rvat_time_table; - uint64_t rvat_beg_tim, rvat_end_tim; - unsigned char *rvat_frame_data; - uint64_t rvat_frame_maxhandle; - fst_off_t *rvat_chain_table; - uint32_t *rvat_chain_table_lengths; - uint64_t rvat_vc_maxhandle; - fst_off_t rvat_vc_start; - uint32_t *rvat_sig_offs; - int rvat_packtype; - - uint32_t rvat_chain_len; - unsigned char *rvat_chain_mem; - fstHandle rvat_chain_facidx; - - uint32_t rvat_chain_pos_tidx; - uint32_t rvat_chain_pos_idx; - uint64_t rvat_chain_pos_time; - unsigned rvat_chain_pos_valid : 1; - - /* entries specific to hierarchy traversal */ - - struct fstHier hier; - struct fstCurrHier *curr_hier; - fstHandle current_handle; - char *curr_flat_hier_nam; - int flat_hier_alloc_len; - unsigned do_rewind : 1; - char str_scope_nam[FST_ID_NAM_SIZ + 1]; - char str_scope_comp[FST_ID_NAM_SIZ + 1]; - - unsigned fseek_failed : 1; - - /* self-buffered I/O for writes */ +/* common entries */ + +FILE *f, *fh; + +uint64_t start_time, end_time; +uint64_t mem_used_by_writer; +uint64_t scope_count; +uint64_t var_count; +fstHandle maxhandle; +uint64_t num_alias; +uint64_t vc_section_count; + +uint32_t *signal_lens; /* maxhandle sized */ +unsigned char *signal_typs; /* maxhandle sized */ +unsigned char *process_mask; /* maxhandle-based, bitwise sized */ +uint32_t longest_signal_value_len; /* longest len value encountered */ +unsigned char *temp_signal_value_buf; /* malloced for len in longest_signal_value_len */ + +signed char timescale; +unsigned char filetype; + +unsigned use_vcd_extensions : 1; +unsigned double_endian_match : 1; +unsigned native_doubles_for_cb : 1; +unsigned contains_geom_section : 1; +unsigned contains_hier_section : 1; /* valid for hier_pos */ +unsigned contains_hier_section_lz4duo : 1; /* valid for hier_pos (contains_hier_section_lz4 always also set) */ +unsigned contains_hier_section_lz4 : 1; /* valid for hier_pos */ +unsigned limit_range_valid : 1; /* valid for limit_range_start, limit_range_end */ + +char version[FST_HDR_SIM_VERSION_SIZE + 1]; +char date[FST_HDR_DATE_SIZE + 1]; +int64_t timezero; + +char *filename, *filename_unpacked; +fst_off_t hier_pos; + +uint32_t num_blackouts; +uint64_t *blackout_times; +unsigned char *blackout_activity; + +uint64_t limit_range_start, limit_range_end; + +/* entries specific to read value at time functions */ + +unsigned rvat_data_valid : 1; +uint64_t *rvat_time_table; +uint64_t rvat_beg_tim, rvat_end_tim; +unsigned char *rvat_frame_data; +uint64_t rvat_frame_maxhandle; +fst_off_t *rvat_chain_table; +uint32_t *rvat_chain_table_lengths; +uint64_t rvat_vc_maxhandle; +fst_off_t rvat_vc_start; +uint32_t *rvat_sig_offs; +int rvat_packtype; + +uint32_t rvat_chain_len; +unsigned char *rvat_chain_mem; +fstHandle rvat_chain_facidx; + +uint32_t rvat_chain_pos_tidx; +uint32_t rvat_chain_pos_idx; +uint64_t rvat_chain_pos_time; +unsigned rvat_chain_pos_valid : 1; + +/* entries specific to hierarchy traversal */ + +struct fstHier hier; +struct fstCurrHier *curr_hier; +fstHandle current_handle; +char *curr_flat_hier_nam; +int flat_hier_alloc_len; +unsigned do_rewind : 1; +char str_scope_nam[FST_ID_NAM_SIZ+1]; +char str_scope_comp[FST_ID_NAM_SIZ+1]; +char *str_scope_attr; + +unsigned fseek_failed : 1; + +/* self-buffered I/O for writes */ #ifndef FST_WRITEX_DISABLE - int writex_pos; - int writex_fd; - unsigned char writex_buf[FST_WRITEX_MAX]; +int writex_pos; +int writex_fd; +unsigned char writex_buf[FST_WRITEX_MAX]; #endif - char *f_nam; - char *fh_nam; +char *f_nam; +char *fh_nam; }; + int fstReaderFseeko(struct fstReaderContext *xc, FILE *stream, fst_off_t offset, int whence) { - int rc = fseeko(stream, offset, whence); +int rc = fseeko(stream, offset, whence); - if (rc < 0) { +if(rc<0) + { xc->fseek_failed = 1; #ifdef FST_DEBUG fprintf(stderr, FST_APIMESS "Seek to #%" PRId64 " (whence = %d) failed!\n", offset, whence); perror("Why"); #endif - } + } - return (rc); +return(rc); } + #ifndef FST_WRITEX_DISABLE -static void fstWritex(struct fstReaderContext *xc, void *v, int len) +static void fstWritex(struct fstReaderContext *xc, void *v, uint32_t len) /* TALOS-2023-1793: change len to unsigned */ { - unsigned char *s = (unsigned char *)v; +unsigned char *s = (unsigned char *)v; - if (len) { - if (len < FST_WRITEX_MAX) { - if (xc->writex_pos + len >= FST_WRITEX_MAX) { - fstWritex(xc, NULL, 0); - } +if(len) + { + if(len < FST_WRITEX_MAX) + { + if(xc->writex_pos + len >= FST_WRITEX_MAX) + { + fstWritex(xc, NULL, 0); + } - memcpy(xc->writex_buf + xc->writex_pos, s, len); - xc->writex_pos += len; - } else { - fstWritex(xc, NULL, 0); - if (write(xc->writex_fd, s, len)) { - }; + memcpy(xc->writex_buf + xc->writex_pos, s, len); + xc->writex_pos += len; + } + else + { + fstWritex(xc, NULL, 0); + if (write(xc->writex_fd, s, len)) { }; + } } - } else { - if (xc->writex_pos) { - if (write(xc->writex_fd, xc->writex_buf, xc->writex_pos)) { - }; - xc->writex_pos = 0; + else + { + if(xc->writex_pos) + { + if(write(xc->writex_fd, xc->writex_buf, xc->writex_pos)) { }; + xc->writex_pos = 0; + } } - } } #endif + /* * scope -> flat name handling */ static void fstReaderDeallocateScopeData(struct fstReaderContext *xc) { - struct fstCurrHier *chp; +struct fstCurrHier *chp; - free(xc->curr_flat_hier_nam); - xc->curr_flat_hier_nam = NULL; - while (xc->curr_hier) { +free(xc->curr_flat_hier_nam); xc->curr_flat_hier_nam = NULL; +while(xc->curr_hier) + { chp = xc->curr_hier->prev; free(xc->curr_hier); xc->curr_hier = chp; - } + } } + const char *fstReaderGetCurrentFlatScope(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - return (xc->curr_flat_hier_nam ? xc->curr_flat_hier_nam : ""); - } else { - return (NULL); - } +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { + return(xc->curr_flat_hier_nam ? xc->curr_flat_hier_nam : ""); + } + else + { + return(NULL); + } } + void *fstReaderGetCurrentScopeUserInfo(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - return (xc->curr_hier ? xc->curr_hier->user_info : NULL); - } else { - return (NULL); - } +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { + return(xc->curr_hier ? xc->curr_hier->user_info : NULL); + } + else + { + return(NULL); + } } + const char *fstReaderPopScope(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc && xc->curr_hier) { +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc && xc->curr_hier) + { struct fstCurrHier *ch = xc->curr_hier; - if (xc->curr_hier->prev) { - xc->curr_flat_hier_nam[xc->curr_hier->prev->len] = 0; - } else { - *xc->curr_flat_hier_nam = 0; - } + if(xc->curr_hier->prev) + { + xc->curr_flat_hier_nam[xc->curr_hier->prev->len] = 0; + } + else + { + *xc->curr_flat_hier_nam = 0; + } xc->curr_hier = xc->curr_hier->prev; free(ch); - return (xc->curr_flat_hier_nam ? xc->curr_flat_hier_nam : ""); - } + return(xc->curr_flat_hier_nam ? xc->curr_flat_hier_nam : ""); + } - return (NULL); +return(NULL); } + void fstReaderResetScope(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - while (fstReaderPopScope(xc)) - ; /* remove any already-built scoping info */ - } +if(xc) + { + while(fstReaderPopScope(xc)); /* remove any already-built scoping info */ + } } + const char *fstReaderPushScope(void *ctx, const char *nam, void *user_info) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { struct fstCurrHier *ch = (struct fstCurrHier *)malloc(sizeof(struct fstCurrHier)); int chl = xc->curr_hier ? xc->curr_hier->len : 0; int len = chl + 1 + strlen(nam); - if (len >= xc->flat_hier_alloc_len) { - xc->curr_flat_hier_nam = - xc->curr_flat_hier_nam ? (char *)realloc(xc->curr_flat_hier_nam, len + 1) : (char *)malloc(len + 1); - } + if(len >= xc->flat_hier_alloc_len) + { + xc->curr_flat_hier_nam = xc->curr_flat_hier_nam ? (char *)realloc(xc->curr_flat_hier_nam, len+1) : (char *)malloc(len+1); + } - if (chl) { - xc->curr_flat_hier_nam[chl] = '.'; - strcpy(xc->curr_flat_hier_nam + chl + 1, nam); - } else { - strcpy(xc->curr_flat_hier_nam, nam); - len--; - } + if(chl) + { + xc->curr_flat_hier_nam[chl] = '.'; + strcpy(xc->curr_flat_hier_nam + chl + 1, nam); + } + else + { + strcpy(xc->curr_flat_hier_nam, nam); + len--; + } ch->len = len; ch->prev = xc->curr_hier; ch->user_info = user_info; xc->curr_hier = ch; - return (xc->curr_flat_hier_nam); - } + return(xc->curr_flat_hier_nam); + } - return (NULL); +return(NULL); } + int fstReaderGetCurrentScopeLen(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc && xc->curr_hier) { - return (xc->curr_hier->len); - } +if(xc && xc->curr_hier) + { + return(xc->curr_hier->len); + } - return (0); +return(0); } + int fstReaderGetFseekFailed(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - return (xc->fseek_failed != 0); - } +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { + return(xc->fseek_failed != 0); + } - return (0); +return(0); } + /* * iter mask manipulation util functions */ int fstReaderGetFacProcessMask(void *ctx, fstHandle facidx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +if(xc) + { facidx--; - if (facidx < xc->maxhandle) { - int process_idx = facidx / 8; - int process_bit = facidx & 7; + if(facidxmaxhandle) + { + int process_idx = facidx/8; + int process_bit = facidx&7; - return ((xc->process_mask[process_idx] & (1 << process_bit)) != 0); + return( (xc->process_mask[process_idx]&(1<maxhandle) { - int idx = facidx / 8; - int bitpos = facidx & 7; + if(facidxmaxhandle) + { + int idx = facidx/8; + int bitpos = facidx&7; - xc->process_mask[idx] |= (1 << bitpos); + xc->process_mask[idx] |= (1<maxhandle) { - int idx = facidx / 8; - int bitpos = facidx & 7; + if(facidxmaxhandle) + { + int idx = facidx/8; + int bitpos = facidx&7; - xc->process_mask[idx] &= (~(1 << bitpos)); + xc->process_mask[idx] &= (~(1<process_mask, 0xff, (xc->maxhandle + 7) / 8); - } +if(xc) + { + memset(xc->process_mask, 0xff, (xc->maxhandle+7)/8); + } } + void fstReaderClrFacProcessMaskAll(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - memset(xc->process_mask, 0x00, (xc->maxhandle + 7) / 8); - } +if(xc) + { + memset(xc->process_mask, 0x00, (xc->maxhandle+7)/8); + } } + /* * various utility read/write functions */ signed char fstReaderGetTimescale(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->timescale : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->timescale : 0); } + uint64_t fstReaderGetStartTime(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->start_time : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->start_time : 0); } + uint64_t fstReaderGetEndTime(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->end_time : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->end_time : 0); } + uint64_t fstReaderGetMemoryUsedByWriter(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->mem_used_by_writer : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->mem_used_by_writer : 0); } + uint64_t fstReaderGetScopeCount(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->scope_count : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->scope_count : 0); } + uint64_t fstReaderGetVarCount(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->var_count : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->var_count : 0); } + fstHandle fstReaderGetMaxHandle(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->maxhandle : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->maxhandle : 0); } + uint64_t fstReaderGetAliasCount(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->num_alias : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->num_alias : 0); } + uint64_t fstReaderGetValueChangeSectionCount(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->vc_section_count : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->vc_section_count : 0); } + int fstReaderGetDoubleEndianMatchState(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->double_endian_match : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->double_endian_match : 0); } + const char *fstReaderGetVersionString(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->version : NULL); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->version : NULL); } + const char *fstReaderGetDateString(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->date : NULL); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->date : NULL); } + int fstReaderGetFileType(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? (int)xc->filetype : (int)FST_FT_VERILOG); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? (int)xc->filetype : (int)FST_FT_VERILOG); } + int64_t fstReaderGetTimezero(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->timezero : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->timezero : 0); } + uint32_t fstReaderGetNumberDumpActivityChanges(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - return (xc ? xc->num_blackouts : 0); +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +return(xc ? xc->num_blackouts : 0); } + uint64_t fstReaderGetDumpActivityChangeTime(void *ctx, uint32_t idx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc && (idx < xc->num_blackouts) && (xc->blackout_times)) { - return (xc->blackout_times[idx]); - } else { - return (0); - } +if(xc && (idx < xc->num_blackouts) && (xc->blackout_times)) + { + return(xc->blackout_times[idx]); + } + else + { + return(0); + } } + unsigned char fstReaderGetDumpActivityChangeValue(void *ctx, uint32_t idx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc && (idx < xc->num_blackouts) && (xc->blackout_activity)) { - return (xc->blackout_activity[idx]); - } else { - return (0); - } +if(xc && (idx < xc->num_blackouts) && (xc->blackout_activity)) + { + return(xc->blackout_activity[idx]); + } + else + { + return(0); + } } + void fstReaderSetLimitTimeRange(void *ctx, uint64_t start_time, uint64_t end_time) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +if(xc) + { xc->limit_range_valid = 1; xc->limit_range_start = start_time; xc->limit_range_end = end_time; - } + } } + void fstReaderSetUnlimitedTimeRange(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +if(xc) + { xc->limit_range_valid = 0; - } + } } + void fstReaderSetVcdExtensions(void *ctx, int enable) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +if(xc) + { xc->use_vcd_extensions = (enable != 0); - } + } } + void fstReaderIterBlocksSetNativeDoublesOnCallback(void *ctx, int enable) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { xc->native_doubles_for_cb = (enable != 0); - } + } } /* @@ -3548,40 +3891,44 @@ void fstReaderIterBlocksSetNativeDoublesOnCallback(void *ctx, int enable) */ static void fstVcdID(char *buf, unsigned int value) { - char *pnt = buf; +char *pnt = buf; - /* zero is illegal for a value...it is assumed they start at one */ - while (value) { +/* zero is illegal for a value...it is assumed they start at one */ +while (value) + { value--; *(pnt++) = (char)('!' + value % 94); value = value / 94; - } + } - *pnt = 0; +*pnt = 0; } static int fstVcdIDForFwrite(char *buf, unsigned int value) { - char *pnt = buf; +char *pnt = buf; - /* zero is illegal for a value...it is assumed they start at one */ - while (value) { +/* zero is illegal for a value...it is assumed they start at one */ +while (value) + { value--; *(pnt++) = (char)('!' + value % 94); value = value / 94; - } + } - return (pnt - buf); +return(pnt - buf); } + static int fstReaderRecreateHierFile(struct fstReaderContext *xc) { - int pass_status = 1; +int pass_status = 1; - if (!xc->fh) { +if(!xc->fh) + { fst_off_t offs_cache = ftello(xc->f); - auto fnam_size = strlen(xc->filename) + 6 + 16 + 32 + 1; - char *fnam = (char *)malloc(fnam_size); + int fnam_len = strlen(xc->filename) + 6 + 16 + 32 + 1; + char *fnam = (char *)malloc(fnam_len); unsigned char *mem = (unsigned char *)malloc(FST_GZIO_LEN); fst_off_t hl, uclen; fst_off_t clen = 0; @@ -3590,643 +3937,666 @@ static int fstReaderRecreateHierFile(struct fstReaderContext *xc) int htyp = FST_BL_SKIP; /* can't handle both set at once should never happen in a real file */ - if (!xc->contains_hier_section_lz4 && xc->contains_hier_section) { - htyp = FST_BL_HIER; - } else if (xc->contains_hier_section_lz4 && !xc->contains_hier_section) { - htyp = xc->contains_hier_section_lz4duo ? FST_BL_HIER_LZ4DUO : FST_BL_HIER_LZ4; - } + if(!xc->contains_hier_section_lz4 && xc->contains_hier_section) + { + htyp = FST_BL_HIER; + } + else + if(xc->contains_hier_section_lz4 && !xc->contains_hier_section) + { + htyp = xc->contains_hier_section_lz4duo ? FST_BL_HIER_LZ4DUO : FST_BL_HIER_LZ4; + } - snprintf(fnam, fnam_size, "%s.hier_%d_%p", xc->filename, getpid(), (void *)xc); + snprintf(fnam, fnam_len, "%s.hier_%d_%p", xc->filename, getpid(), (void *)xc); fstReaderFseeko(xc, xc->f, xc->hier_pos, SEEK_SET); uclen = fstReaderUint64(xc->f); #ifndef __MINGW32__ fflush(xc->f); #endif - if (htyp == FST_BL_HIER) { - fstReaderFseeko(xc, xc->f, xc->hier_pos, SEEK_SET); - uclen = fstReaderUint64(xc->f); + if(htyp == FST_BL_HIER) + { + fstReaderFseeko(xc, xc->f, xc->hier_pos, SEEK_SET); + uclen = fstReaderUint64(xc->f); #ifndef __MINGW32__ - fflush(xc->f); + fflush(xc->f); #endif - zfd = dup(fileno(xc->f)); - lseek(zfd, ftell(xc->f), SEEK_SET); - zhandle = gzdopen(zfd, "rb"); - if (!zhandle) { - close(zfd); - free(mem); - free(fnam); - return (0); - } - } else if ((htyp == FST_BL_HIER_LZ4) || (htyp == FST_BL_HIER_LZ4DUO)) { - fstReaderFseeko(xc, xc->f, xc->hier_pos - 8, SEEK_SET); /* get section len */ - clen = fstReaderUint64(xc->f) - 16; - uclen = fstReaderUint64(xc->f); + zfd = dup(fileno(xc->f)); + zhandle = gzdopen(zfd, "rb"); + if(!zhandle) + { + close(zfd); + free(mem); + free(fnam); + return(0); + } + } + else + if((htyp == FST_BL_HIER_LZ4) || (htyp == FST_BL_HIER_LZ4DUO)) + { + fstReaderFseeko(xc, xc->f, xc->hier_pos - 8, SEEK_SET); /* get section len */ + clen = fstReaderUint64(xc->f) - 16; + uclen = fstReaderUint64(xc->f); #ifndef __MINGW32__ - fflush(xc->f); + fflush(xc->f); #endif - } + } #ifndef __MINGW32__ xc->fh = fopen(fnam, "w+b"); - if (!xc->fh) + if(!xc->fh) #endif - { - xc->fh = tmpfile_open(&xc->fh_nam); - free(fnam); - fnam = NULL; - if (!xc->fh) { - tmpfile_close(&xc->fh, &xc->fh_nam); - free(mem); - return (0); - } - } + { + xc->fh = tmpfile_open(&xc->fh_nam); + free(fnam); fnam = NULL; + if(!xc->fh) + { + tmpfile_close(&xc->fh, &xc->fh_nam); + free(mem); + return(0); + } + } #ifndef __MINGW32__ - if (fnam) - unlink(fnam); + if(fnam) unlink(fnam); #endif - if (htyp == FST_BL_HIER) { - for (hl = 0; hl < uclen; hl += FST_GZIO_LEN) { - size_t len = ((uclen - hl) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - hl); - size_t gzreadlen = gzread(zhandle, mem, len); /* rc should equal len... */ - size_t fwlen; - - if (gzreadlen != len) { - pass_status = 0; - break; - } - - fwlen = fstFwrite(mem, len, 1, xc->fh); - if (fwlen != 1) { - pass_status = 0; - break; - } - } - gzclose(zhandle); - } else if (htyp == FST_BL_HIER_LZ4DUO) { - unsigned char *lz4_cmem = (unsigned char *)malloc(clen); - unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen); - unsigned char *lz4_ucmem2; - uint64_t uclen2; - int skiplen2 = 0; - - fstFread(lz4_cmem, clen, 1, xc->f); - - uclen2 = fstGetVarint64(lz4_cmem, &skiplen2); - lz4_ucmem2 = (unsigned char *)malloc(uclen2); - pass_status = - (uclen2 == (uint64_t)LZ4_decompress_safe_partial((char *)lz4_cmem + skiplen2, (char *)lz4_ucmem2, - clen - skiplen2, uclen2, uclen2)); - if (pass_status) { - pass_status = (uclen == LZ4_decompress_safe_partial((char *)lz4_ucmem2, (char *)lz4_ucmem, uclen2, - uclen, uclen)); - - if (fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1) { - pass_status = 0; - } - } - - free(lz4_ucmem2); - free(lz4_ucmem); - free(lz4_cmem); - } else if (htyp == FST_BL_HIER_LZ4) { - unsigned char *lz4_cmem = (unsigned char *)malloc(clen); - unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen); - - fstFread(lz4_cmem, clen, 1, xc->f); - pass_status = - (uclen == LZ4_decompress_safe_partial((char *)lz4_cmem, (char *)lz4_ucmem, clen, uclen, uclen)); - - if (fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1) { - pass_status = 0; - } + if(htyp == FST_BL_HIER) + { + for(hl = 0; hl < uclen; hl += FST_GZIO_LEN) + { + size_t len = ((uclen - hl) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - hl); + size_t gzreadlen = gzread(zhandle, mem, len); /* rc should equal len... */ + size_t fwlen; + + if(gzreadlen != len) + { + pass_status = 0; + break; + } - free(lz4_ucmem); - free(lz4_cmem); - } else /* FST_BL_SKIP */ - { - pass_status = 0; - if (xc->fh) { - fclose(xc->fh); - xc->fh = NULL; /* needed in case .hier file is missing and there are no hier sections */ - } - } + fwlen = fstFwrite(mem, len, 1, xc->fh); + if(fwlen != 1) + { + pass_status = 0; + break; + } + } + gzclose(zhandle); + } + else + if(htyp == FST_BL_HIER_LZ4DUO) + { + unsigned char *lz4_cmem = (unsigned char *)malloc(clen); + unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen); + unsigned char *lz4_ucmem2; + uint64_t uclen2; + int skiplen2 = 0; + + fstFread(lz4_cmem, clen, 1, xc->f); + + uclen2 = fstGetVarint64(lz4_cmem, &skiplen2); + lz4_ucmem2 = (unsigned char *)malloc(uclen2); + pass_status = (uclen2 == (uint64_t)LZ4_decompress_safe_partial ((char *)lz4_cmem + skiplen2, (char *)lz4_ucmem2, clen - skiplen2, uclen2, uclen2)); + if(pass_status) + { + pass_status = (uclen == LZ4_decompress_safe_partial ((char *)lz4_ucmem2, (char *)lz4_ucmem, uclen2, uclen, uclen)); + + if(fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1) + { + pass_status = 0; + } + } + + free(lz4_ucmem2); + free(lz4_ucmem); + free(lz4_cmem); + } + else + if(htyp == FST_BL_HIER_LZ4) + { + unsigned char *lz4_cmem = (unsigned char *)malloc(clen); + unsigned char *lz4_ucmem = (unsigned char *)malloc(uclen); + + fstFread(lz4_cmem, clen, 1, xc->f); + pass_status = (uclen == LZ4_decompress_safe_partial ((char *)lz4_cmem, (char *)lz4_ucmem, clen, uclen, uclen)); + + if(fstFwrite(lz4_ucmem, uclen, 1, xc->fh) != 1) + { + pass_status = 0; + } + + free(lz4_ucmem); + free(lz4_cmem); + } + else /* FST_BL_SKIP */ + { + pass_status = 0; + if(xc->fh) + { + fclose(xc->fh); xc->fh = NULL; /* needed in case .hier file is missing and there are no hier sections */ + } + } free(mem); free(fnam); fstReaderFseeko(xc, xc->f, offs_cache, SEEK_SET); - } + } - return (pass_status); +return(pass_status); } + int fstReaderIterateHierRewind(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - int pass_status = 0; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +int pass_status = 0; - if (xc) { +if(xc) + { pass_status = 1; - if (!xc->fh) { - pass_status = fstReaderRecreateHierFile(xc); - } + if(!xc->fh) + { + pass_status = fstReaderRecreateHierFile(xc); + } xc->do_rewind = 1; - } + } - return (pass_status); +return(pass_status); } + struct fstHier *fstReaderIterateHier(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - int isfeof; - fstHandle alias; - char *pnt; - int ch; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +int isfeof; +fstHandle alias; +char *pnt; +int ch; - if (!xc) - return (NULL); +if(!xc) return(NULL); - if (!xc->fh) { - if (!fstReaderRecreateHierFile(xc)) { - return (NULL); +if(!xc->fh) + { + if(!fstReaderRecreateHierFile(xc)) + { + return(NULL); + } } - } - if (xc->do_rewind) { +if(xc->do_rewind) + { xc->do_rewind = 0; xc->current_handle = 0; fstReaderFseeko(xc, xc->fh, 0, SEEK_SET); clearerr(xc->fh); - } + } - if (!(isfeof = feof(xc->fh))) { +if(!(isfeof=feof(xc->fh))) + { int tag = fgetc(xc->fh); - switch (tag) { - case FST_ST_VCD_SCOPE: - xc->hier.htyp = FST_HT_SCOPE; - xc->hier.u.scope.typ = fgetc(xc->fh); - xc->hier.u.scope.name = pnt = xc->str_scope_nam; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* scopename */ - *pnt = 0; - xc->hier.u.scope.name_length = pnt - xc->hier.u.scope.name; - - xc->hier.u.scope.component = pnt = xc->str_scope_comp; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* scopecomp */ - *pnt = 0; - xc->hier.u.scope.component_length = pnt - xc->hier.u.scope.component; - break; - - case FST_ST_VCD_UPSCOPE: - xc->hier.htyp = FST_HT_UPSCOPE; - break; - - case FST_ST_GEN_ATTRBEGIN: - xc->hier.htyp = FST_HT_ATTRBEGIN; - xc->hier.u.attr.typ = fgetc(xc->fh); - xc->hier.u.attr.subtype = fgetc(xc->fh); - xc->hier.u.attr.name = pnt = xc->str_scope_nam; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* scopename */ - *pnt = 0; - xc->hier.u.attr.name_length = pnt - xc->hier.u.scope.name; - - xc->hier.u.attr.arg = fstReaderVarint64(xc->fh); - - if (xc->hier.u.attr.typ == FST_AT_MISC) { - if ((xc->hier.u.attr.subtype == FST_MT_SOURCESTEM) || (xc->hier.u.attr.subtype == FST_MT_SOURCEISTEM)) { - int sidx_skiplen_dummy = 0; - xc->hier.u.attr.arg_from_name = - fstGetVarint64((unsigned char *)xc->str_scope_nam, &sidx_skiplen_dummy); - } - } - break; - - case FST_ST_GEN_ATTREND: - xc->hier.htyp = FST_HT_ATTREND; - break; - - case FST_VT_VCD_EVENT: - case FST_VT_VCD_INTEGER: - case FST_VT_VCD_PARAMETER: - case FST_VT_VCD_REAL: - case FST_VT_VCD_REAL_PARAMETER: - case FST_VT_VCD_REG: - case FST_VT_VCD_SUPPLY0: - case FST_VT_VCD_SUPPLY1: - case FST_VT_VCD_TIME: - case FST_VT_VCD_TRI: - case FST_VT_VCD_TRIAND: - case FST_VT_VCD_TRIOR: - case FST_VT_VCD_TRIREG: - case FST_VT_VCD_TRI0: - case FST_VT_VCD_TRI1: - case FST_VT_VCD_WAND: - case FST_VT_VCD_WIRE: - case FST_VT_VCD_WOR: - case FST_VT_VCD_PORT: - case FST_VT_VCD_SPARRAY: - case FST_VT_VCD_REALTIME: - case FST_VT_GEN_STRING: - case FST_VT_SV_BIT: - case FST_VT_SV_LOGIC: - case FST_VT_SV_INT: - case FST_VT_SV_SHORTINT: - case FST_VT_SV_LONGINT: - case FST_VT_SV_BYTE: - case FST_VT_SV_ENUM: - case FST_VT_SV_SHORTREAL: - xc->hier.htyp = FST_HT_VAR; - xc->hier.u.var.svt_workspace = FST_SVT_NONE; - xc->hier.u.var.sdt_workspace = FST_SDT_NONE; - xc->hier.u.var.sxt_workspace = 0; - xc->hier.u.var.typ = tag; - xc->hier.u.var.direction = fgetc(xc->fh); - xc->hier.u.var.name = pnt = xc->str_scope_nam; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* varname */ - *pnt = 0; - xc->hier.u.var.name_length = pnt - xc->hier.u.var.name; - xc->hier.u.var.length = fstReaderVarint32(xc->fh); - if (tag == FST_VT_VCD_PORT) { - xc->hier.u.var.length -= 2; /* removal of delimiting spaces */ - xc->hier.u.var.length /= 3; /* port -> signal size adjust */ - } - - alias = fstReaderVarint32(xc->fh); - - if (!alias) { - xc->current_handle++; - xc->hier.u.var.handle = xc->current_handle; - xc->hier.u.var.is_alias = 0; - } else { - xc->hier.u.var.handle = alias; - xc->hier.u.var.is_alias = 1; - } - - break; - - default: - isfeof = 1; - break; - } - } - - return (!isfeof ? &xc->hier : NULL); + int cl; + switch(tag) + { + case FST_ST_VCD_SCOPE: + xc->hier.htyp = FST_HT_SCOPE; + xc->hier.u.scope.typ = fgetc(xc->fh); + xc->hier.u.scope.name = pnt = xc->str_scope_nam; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_SIZ) + { + pnt[cl++] = ch; + } + }; /* scopename */ + pnt[cl] = 0; + xc->hier.u.scope.name_length = cl; + + xc->hier.u.scope.component = pnt = xc->str_scope_comp; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_SIZ) + { + pnt[cl++] = ch; + } + }; /* scopecomp */ + pnt[cl] = 0; + xc->hier.u.scope.component_length = cl; + break; + + case FST_ST_VCD_UPSCOPE: + xc->hier.htyp = FST_HT_UPSCOPE; + break; + + case FST_ST_GEN_ATTRBEGIN: + xc->hier.htyp = FST_HT_ATTRBEGIN; + xc->hier.u.attr.typ = fgetc(xc->fh); + xc->hier.u.attr.subtype = fgetc(xc->fh); + if(!xc->str_scope_attr) + { + xc->str_scope_attr = (char *)calloc(1, FST_ID_NAM_ATTR_SIZ+1); + } + xc->hier.u.attr.name = pnt = xc->str_scope_attr; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_ATTR_SIZ) + { + pnt[cl++] = ch; + } + }; /* attrname */ + pnt[cl] = 0; + xc->hier.u.attr.name_length = cl; + + xc->hier.u.attr.arg = fstReaderVarint64(xc->fh); + + if(xc->hier.u.attr.typ == FST_AT_MISC) + { + if((xc->hier.u.attr.subtype == FST_MT_SOURCESTEM)||(xc->hier.u.attr.subtype == FST_MT_SOURCEISTEM)) + { + int sidx_skiplen_dummy = 0; + xc->hier.u.attr.arg_from_name = fstGetVarint64((unsigned char *)xc->str_scope_attr, &sidx_skiplen_dummy); + } + } + break; + + case FST_ST_GEN_ATTREND: + xc->hier.htyp = FST_HT_ATTREND; + break; + + case FST_VT_VCD_EVENT: + case FST_VT_VCD_INTEGER: + case FST_VT_VCD_PARAMETER: + case FST_VT_VCD_REAL: + case FST_VT_VCD_REAL_PARAMETER: + case FST_VT_VCD_REG: + case FST_VT_VCD_SUPPLY0: + case FST_VT_VCD_SUPPLY1: + case FST_VT_VCD_TIME: + case FST_VT_VCD_TRI: + case FST_VT_VCD_TRIAND: + case FST_VT_VCD_TRIOR: + case FST_VT_VCD_TRIREG: + case FST_VT_VCD_TRI0: + case FST_VT_VCD_TRI1: + case FST_VT_VCD_WAND: + case FST_VT_VCD_WIRE: + case FST_VT_VCD_WOR: + case FST_VT_VCD_PORT: + case FST_VT_VCD_SPARRAY: + case FST_VT_VCD_REALTIME: + case FST_VT_GEN_STRING: + case FST_VT_SV_BIT: + case FST_VT_SV_LOGIC: + case FST_VT_SV_INT: + case FST_VT_SV_SHORTINT: + case FST_VT_SV_LONGINT: + case FST_VT_SV_BYTE: + case FST_VT_SV_ENUM: + case FST_VT_SV_SHORTREAL: + xc->hier.htyp = FST_HT_VAR; + xc->hier.u.var.svt_workspace = FST_SVT_NONE; + xc->hier.u.var.sdt_workspace = FST_SDT_NONE; + xc->hier.u.var.sxt_workspace = 0; + xc->hier.u.var.typ = tag; + xc->hier.u.var.direction = fgetc(xc->fh); + xc->hier.u.var.name = pnt = xc->str_scope_nam; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_SIZ) + { + pnt[cl++] = ch; + } + }; /* varname */ + pnt[cl] = 0; + xc->hier.u.var.name_length = cl; + xc->hier.u.var.length = fstReaderVarint32(xc->fh); + if(tag == FST_VT_VCD_PORT) + { + xc->hier.u.var.length -= 2; /* removal of delimiting spaces */ + xc->hier.u.var.length /= 3; /* port -> signal size adjust */ + } + + alias = fstReaderVarint32(xc->fh); + + if(!alias) + { + xc->current_handle++; + xc->hier.u.var.handle = xc->current_handle; + xc->hier.u.var.is_alias = 0; + } + else + { + xc->hier.u.var.handle = alias; + xc->hier.u.var.is_alias = 1; + } + + break; + + default: + isfeof = 1; + break; + } + } + +return(!isfeof ? &xc->hier : NULL); } + int fstReaderProcessHier(void *ctx, FILE *fv) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - char *str; - char *pnt; - int ch, scopetype; - int vartype; - uint32_t len, alias; - /* uint32_t maxvalpos=0; */ - unsigned int num_signal_dyn = 65536; - int attrtype, subtype; - uint64_t attrarg; - fstHandle maxhandle_scanbuild; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +char *str; +char *pnt; +int ch, scopetype; +int vartype; +uint32_t len, alias; +/* uint32_t maxvalpos=0; */ +unsigned int num_signal_dyn = 65536; +int attrtype, subtype; +uint64_t attrarg; +fstHandle maxhandle_scanbuild; +int cl; - if (!xc) - return (0); +if(!xc) return(0); - xc->longest_signal_value_len = 32; /* arbitrarily set at 32...this is much longer than an expanded double */ +xc->longest_signal_value_len = 32; /* arbitrarily set at 32...this is much longer than an expanded double */ - if (!xc->fh) { - if (!fstReaderRecreateHierFile(xc)) { - return (0); +if(!xc->fh) + { + if(!fstReaderRecreateHierFile(xc)) + { + return(0); + } } - } - str = (char *)malloc(FST_ID_NAM_ATTR_SIZ + 1); +str = (char *)malloc(FST_ID_NAM_ATTR_SIZ+1); - if (fv) { +if(fv) + { char time_dimension[2] = {0, 0}; int time_scale = 1; fprintf(fv, "$date\n\t%s\n$end\n", xc->date); fprintf(fv, "$version\n\t%s\n$end\n", xc->version); - if (xc->timezero) - fprintf(fv, "$timezero\n\t%" PRId64 "\n$end\n", xc->timezero); - - switch (xc->timescale) { - case 2: - time_scale = 100; - time_dimension[0] = 0; - break; - case 1: - time_scale = 10; /* fallthrough */ - case 0: - time_dimension[0] = 0; - break; - - case -1: - time_scale = 100; - time_dimension[0] = 'm'; - break; - case -2: - time_scale = 10; /* fallthrough */ - case -3: - time_dimension[0] = 'm'; - break; - - case -4: - time_scale = 100; - time_dimension[0] = 'u'; - break; - case -5: - time_scale = 10; /* fallthrough */ - case -6: - time_dimension[0] = 'u'; - break; - - case -10: - time_scale = 100; - time_dimension[0] = 'p'; - break; - case -11: - time_scale = 10; /* fallthrough */ - case -12: - time_dimension[0] = 'p'; - break; - - case -13: - time_scale = 100; - time_dimension[0] = 'f'; - break; - case -14: - time_scale = 10; /* fallthrough */ - case -15: - time_dimension[0] = 'f'; - break; - - case -16: - time_scale = 100; - time_dimension[0] = 'a'; - break; - case -17: - time_scale = 10; /* fallthrough */ - case -18: - time_dimension[0] = 'a'; - break; - - case -19: - time_scale = 100; - time_dimension[0] = 'z'; - break; - case -20: - time_scale = 10; /* fallthrough */ - case -21: - time_dimension[0] = 'z'; - break; - - case -7: - time_scale = 100; - time_dimension[0] = 'n'; - break; - case -8: - time_scale = 10; /* fallthrough */ - case -9: - default: - time_dimension[0] = 'n'; - break; - } - - if (fv) - fprintf(fv, "$timescale\n\t%d%ss\n$end\n", time_scale, time_dimension); - } - - xc->maxhandle = 0; - xc->num_alias = 0; - - free(xc->signal_lens); - xc->signal_lens = (uint32_t *)malloc(num_signal_dyn * sizeof(uint32_t)); - - free(xc->signal_typs); - xc->signal_typs = (unsigned char *)malloc(num_signal_dyn * sizeof(unsigned char)); - - fstReaderFseeko(xc, xc->fh, 0, SEEK_SET); - while (!feof(xc->fh)) { - int tag = fgetc(xc->fh); - switch (tag) { - case FST_ST_VCD_SCOPE: - scopetype = fgetc(xc->fh); - if ((scopetype < FST_ST_MIN) || (scopetype > FST_ST_MAX)) - scopetype = FST_ST_VCD_MODULE; - pnt = str; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* scopename */ - *pnt = 0; - while (fgetc(xc->fh)) { - }; /* scopecomp */ - - if (fv) - fprintf(fv, "$scope %s %s $end\n", modtypes[scopetype], str); - break; - - case FST_ST_VCD_UPSCOPE: - if (fv) - fprintf(fv, "$upscope $end\n"); - break; - - case FST_ST_GEN_ATTRBEGIN: - attrtype = fgetc(xc->fh); - subtype = fgetc(xc->fh); - pnt = str; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* attrname */ - *pnt = 0; - - if (!str[0]) { - strcpy(str, "\"\""); - } - - attrarg = fstReaderVarint64(xc->fh); - - if (fv && xc->use_vcd_extensions) { - switch (attrtype) { - case FST_AT_ARRAY: - if ((subtype < FST_AR_NONE) || (subtype > FST_AR_MAX)) - subtype = FST_AR_NONE; - fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], arraytypes[subtype], str, - attrarg); - break; - case FST_AT_ENUM: - if ((subtype < FST_EV_SV_INTEGER) || (subtype > FST_EV_MAX)) - subtype = FST_EV_SV_INTEGER; - fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], enumvaluetypes[subtype], - str, attrarg); - break; - case FST_AT_PACK: - if ((subtype < FST_PT_NONE) || (subtype > FST_PT_MAX)) - subtype = FST_PT_NONE; - fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], packtypes[subtype], str, - attrarg); - break; - case FST_AT_MISC: - default: - attrtype = FST_AT_MISC; - if (subtype == FST_MT_COMMENT) { - fprintf(fv, "$comment\n\t%s\n$end\n", str); - } else { - if ((subtype == FST_MT_SOURCESTEM) || (subtype == FST_MT_SOURCEISTEM)) { - int sidx_skiplen_dummy = 0; - uint64_t sidx = fstGetVarint64((unsigned char *)str, &sidx_skiplen_dummy); - - fprintf(fv, "$attrbegin %s %02x %" PRId64 " %" PRId64 " $end\n", attrtypes[attrtype], - subtype, sidx, attrarg); - } else { - fprintf(fv, "$attrbegin %s %02x %s %" PRId64 " $end\n", attrtypes[attrtype], subtype, str, - attrarg); - } - } - break; - } - } - break; - - case FST_ST_GEN_ATTREND: - if (fv && xc->use_vcd_extensions) - fprintf(fv, "$attrend $end\n"); - break; - - case FST_VT_VCD_EVENT: - case FST_VT_VCD_INTEGER: - case FST_VT_VCD_PARAMETER: - case FST_VT_VCD_REAL: - case FST_VT_VCD_REAL_PARAMETER: - case FST_VT_VCD_REG: - case FST_VT_VCD_SUPPLY0: - case FST_VT_VCD_SUPPLY1: - case FST_VT_VCD_TIME: - case FST_VT_VCD_TRI: - case FST_VT_VCD_TRIAND: - case FST_VT_VCD_TRIOR: - case FST_VT_VCD_TRIREG: - case FST_VT_VCD_TRI0: - case FST_VT_VCD_TRI1: - case FST_VT_VCD_WAND: - case FST_VT_VCD_WIRE: - case FST_VT_VCD_WOR: - case FST_VT_VCD_PORT: - case FST_VT_VCD_SPARRAY: - case FST_VT_VCD_REALTIME: - case FST_VT_GEN_STRING: - case FST_VT_SV_BIT: - case FST_VT_SV_LOGIC: - case FST_VT_SV_INT: - case FST_VT_SV_SHORTINT: - case FST_VT_SV_LONGINT: - case FST_VT_SV_BYTE: - case FST_VT_SV_ENUM: - case FST_VT_SV_SHORTREAL: - vartype = tag; - /* vardir = */ fgetc(xc->fh); /* unused in VCD reader, but need to advance read pointer */ - pnt = str; - while ((ch = fgetc(xc->fh))) { - *(pnt++) = ch; - }; /* varname */ - *pnt = 0; - len = fstReaderVarint32(xc->fh); - alias = fstReaderVarint32(xc->fh); - - if (!alias) { - if (xc->maxhandle == num_signal_dyn) { - num_signal_dyn *= 2; - xc->signal_lens = (uint32_t *)realloc(xc->signal_lens, num_signal_dyn * sizeof(uint32_t)); - xc->signal_typs = (unsigned char *)realloc(xc->signal_typs, num_signal_dyn * sizeof(unsigned char)); - } - xc->signal_lens[xc->maxhandle] = len; - xc->signal_typs[xc->maxhandle] = vartype; - - /* maxvalpos+=len; */ - if (len > xc->longest_signal_value_len) { - xc->longest_signal_value_len = len; - } - - if ((vartype == FST_VT_VCD_REAL) || (vartype == FST_VT_VCD_REAL_PARAMETER) || - (vartype == FST_VT_VCD_REALTIME) || (vartype == FST_VT_SV_SHORTREAL)) { - len = (vartype != FST_VT_SV_SHORTREAL) ? 64 : 32; - xc->signal_typs[xc->maxhandle] = FST_VT_VCD_REAL; - } - if (fv) { - char vcdid_buf[16]; - uint32_t modlen = (vartype != FST_VT_VCD_PORT) ? len : ((len - 2) / 3); - fstVcdID(vcdid_buf, xc->maxhandle + 1); - fprintf(fv, "$var %s %" PRIu32 " %s %s $end\n", vartypes[vartype], modlen, vcdid_buf, str); - } - xc->maxhandle++; - } else { - if ((vartype == FST_VT_VCD_REAL) || (vartype == FST_VT_VCD_REAL_PARAMETER) || - (vartype == FST_VT_VCD_REALTIME) || (vartype == FST_VT_SV_SHORTREAL)) { - len = (vartype != FST_VT_SV_SHORTREAL) ? 64 : 32; - xc->signal_typs[xc->maxhandle] = FST_VT_VCD_REAL; - } - if (fv) { - char vcdid_buf[16]; - uint32_t modlen = (vartype != FST_VT_VCD_PORT) ? len : ((len - 2) / 3); - fstVcdID(vcdid_buf, alias); - fprintf(fv, "$var %s %" PRIu32 " %s %s $end\n", vartypes[vartype], modlen, vcdid_buf, str); + if(xc->timezero) fprintf(fv, "$timezero\n\t%" PRId64 "\n$end\n", xc->timezero); + + switch(xc->timescale) + { + case 2: time_scale = 100; time_dimension[0] = 0; break; + case 1: time_scale = 10; /* fallthrough */ + case 0: time_dimension[0] = 0; break; + + case -1: time_scale = 100; time_dimension[0] = 'm'; break; + case -2: time_scale = 10; /* fallthrough */ + case -3: time_dimension[0] = 'm'; break; + + case -4: time_scale = 100; time_dimension[0] = 'u'; break; + case -5: time_scale = 10; /* fallthrough */ + case -6: time_dimension[0] = 'u'; break; + + case -10: time_scale = 100; time_dimension[0] = 'p'; break; + case -11: time_scale = 10; /* fallthrough */ + case -12: time_dimension[0] = 'p'; break; + + case -13: time_scale = 100; time_dimension[0] = 'f'; break; + case -14: time_scale = 10; /* fallthrough */ + case -15: time_dimension[0] = 'f'; break; + + case -16: time_scale = 100; time_dimension[0] = 'a'; break; + case -17: time_scale = 10; /* fallthrough */ + case -18: time_dimension[0] = 'a'; break; + + case -19: time_scale = 100; time_dimension[0] = 'z'; break; + case -20: time_scale = 10; /* fallthrough */ + case -21: time_dimension[0] = 'z'; break; + + case -7: time_scale = 100; time_dimension[0] = 'n'; break; + case -8: time_scale = 10; /* fallthrough */ + case -9: + default: time_dimension[0] = 'n'; break; } - xc->num_alias++; - } - break; + if(fv) fprintf(fv, "$timescale\n\t%d%ss\n$end\n", time_scale, time_dimension); + } + +xc->maxhandle = 0; +xc->num_alias = 0; + +free(xc->signal_lens); +xc->signal_lens = (uint32_t *)malloc(num_signal_dyn*sizeof(uint32_t)); + +free(xc->signal_typs); +xc->signal_typs = (unsigned char *)malloc(num_signal_dyn*sizeof(unsigned char)); + +fstReaderFseeko(xc, xc->fh, 0, SEEK_SET); +while(!feof(xc->fh)) + { + int tag = fgetc(xc->fh); + switch(tag) + { + case FST_ST_VCD_SCOPE: + scopetype = fgetc(xc->fh); + if((scopetype < FST_ST_MIN) || (scopetype > FST_ST_MAX)) scopetype = FST_ST_VCD_MODULE; + pnt = str; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_ATTR_SIZ) + { + pnt[cl++] = ch; + } + }; /* scopename */ + pnt[cl] = 0; + while(fgetc(xc->fh)) { }; /* scopecomp */ + + if(fv) fprintf(fv, "$scope %s %s $end\n", modtypes[scopetype], str); + break; + + case FST_ST_VCD_UPSCOPE: + if(fv) fprintf(fv, "$upscope $end\n"); + break; + + case FST_ST_GEN_ATTRBEGIN: + attrtype = fgetc(xc->fh); + subtype = fgetc(xc->fh); + pnt = str; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_ATTR_SIZ) + { + pnt[cl++] = ch; + } + }; /* attrname */ + pnt[cl] = 0; + + if(!str[0]) { strcpy(str, "\"\""); } + + attrarg = fstReaderVarint64(xc->fh); + + if(fv && xc->use_vcd_extensions) + { + switch(attrtype) + { + case FST_AT_ARRAY: if((subtype < FST_AR_NONE) || (subtype > FST_AR_MAX)) subtype = FST_AR_NONE; + fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], arraytypes[subtype], str, attrarg); + break; + case FST_AT_ENUM: if((subtype < FST_EV_SV_INTEGER) || (subtype > FST_EV_MAX)) subtype = FST_EV_SV_INTEGER; + fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], enumvaluetypes[subtype], str, attrarg); + break; + case FST_AT_PACK: if((subtype < FST_PT_NONE) || (subtype > FST_PT_MAX)) subtype = FST_PT_NONE; + fprintf(fv, "$attrbegin %s %s %s %" PRId64 " $end\n", attrtypes[attrtype], packtypes[subtype], str, attrarg); + break; + case FST_AT_MISC: + default: attrtype = FST_AT_MISC; + if(subtype == FST_MT_COMMENT) + { + fprintf(fv, "$comment\n\t%s\n$end\n", str); + } + else + { + if((subtype == FST_MT_SOURCESTEM)||(subtype == FST_MT_SOURCEISTEM)) + { + int sidx_skiplen_dummy = 0; + uint64_t sidx = fstGetVarint64((unsigned char *)str, &sidx_skiplen_dummy); + + fprintf(fv, "$attrbegin %s %02x %" PRId64 " %" PRId64 " $end\n", attrtypes[attrtype], subtype, sidx, attrarg); + } + else + { + fprintf(fv, "$attrbegin %s %02x %s %" PRId64 " $end\n", attrtypes[attrtype], subtype, str, attrarg); + } + } + break; + } + } + break; - default: - break; + case FST_ST_GEN_ATTREND: + if(fv && xc->use_vcd_extensions) fprintf(fv, "$attrend $end\n"); + break; + + case FST_VT_VCD_EVENT: + case FST_VT_VCD_INTEGER: + case FST_VT_VCD_PARAMETER: + case FST_VT_VCD_REAL: + case FST_VT_VCD_REAL_PARAMETER: + case FST_VT_VCD_REG: + case FST_VT_VCD_SUPPLY0: + case FST_VT_VCD_SUPPLY1: + case FST_VT_VCD_TIME: + case FST_VT_VCD_TRI: + case FST_VT_VCD_TRIAND: + case FST_VT_VCD_TRIOR: + case FST_VT_VCD_TRIREG: + case FST_VT_VCD_TRI0: + case FST_VT_VCD_TRI1: + case FST_VT_VCD_WAND: + case FST_VT_VCD_WIRE: + case FST_VT_VCD_WOR: + case FST_VT_VCD_PORT: + case FST_VT_VCD_SPARRAY: + case FST_VT_VCD_REALTIME: + case FST_VT_GEN_STRING: + case FST_VT_SV_BIT: + case FST_VT_SV_LOGIC: + case FST_VT_SV_INT: + case FST_VT_SV_SHORTINT: + case FST_VT_SV_LONGINT: + case FST_VT_SV_BYTE: + case FST_VT_SV_ENUM: + case FST_VT_SV_SHORTREAL: + vartype = tag; + /* vardir = */ fgetc(xc->fh); /* unused in VCD reader, but need to advance read pointer */ + pnt = str; + cl = 0; + while((ch = fgetc(xc->fh))) + { + if(cl < FST_ID_NAM_ATTR_SIZ) + { + pnt[cl++] = ch; + } + }; /* varname */ + pnt[cl] = 0; + len = fstReaderVarint32(xc->fh); + alias = fstReaderVarint32(xc->fh); + + if(!alias) + { + if(xc->maxhandle == num_signal_dyn) + { + num_signal_dyn *= 2; + xc->signal_lens = (uint32_t *)realloc(xc->signal_lens, num_signal_dyn*sizeof(uint32_t)); + xc->signal_typs = (unsigned char *)realloc(xc->signal_typs, num_signal_dyn*sizeof(unsigned char)); + } + xc->signal_lens[xc->maxhandle] = len; + xc->signal_typs[xc->maxhandle] = vartype; + + /* maxvalpos+=len; */ + if(len > xc->longest_signal_value_len) + { + xc->longest_signal_value_len = len; + } + + if((vartype == FST_VT_VCD_REAL) || (vartype == FST_VT_VCD_REAL_PARAMETER) || (vartype == FST_VT_VCD_REALTIME) || (vartype == FST_VT_SV_SHORTREAL)) + { + len = (vartype != FST_VT_SV_SHORTREAL) ? 64 : 32; + xc->signal_typs[xc->maxhandle] = FST_VT_VCD_REAL; + } + if(fv) + { + char vcdid_buf[16]; + uint32_t modlen = (vartype != FST_VT_VCD_PORT) ? len : ((len - 2) / 3); + fstVcdID(vcdid_buf, xc->maxhandle+1); + fprintf(fv, "$var %s %" PRIu32 " %s %s $end\n", vartypes[vartype], modlen, vcdid_buf, str); + } + xc->maxhandle++; + } + else + { + if((vartype == FST_VT_VCD_REAL) || (vartype == FST_VT_VCD_REAL_PARAMETER) || (vartype == FST_VT_VCD_REALTIME) || (vartype == FST_VT_SV_SHORTREAL)) + { + len = (vartype != FST_VT_SV_SHORTREAL) ? 64 : 32; + xc->signal_typs[xc->maxhandle] = FST_VT_VCD_REAL; + } + if(fv) + { + char vcdid_buf[16]; + uint32_t modlen = (vartype != FST_VT_VCD_PORT) ? len : ((len - 2) / 3); + fstVcdID(vcdid_buf, alias); + fprintf(fv, "$var %s %" PRIu32 " %s %s $end\n", vartypes[vartype], modlen, vcdid_buf, str); + } + xc->num_alias++; + } + + break; + + default: + break; + } } - } - if (fv) - fprintf(fv, "$enddefinitions $end\n"); +if(fv) fprintf(fv, "$enddefinitions $end\n"); - maxhandle_scanbuild = xc->maxhandle ? xc->maxhandle - : 1; /*scan-build warning suppression, in reality we have at least one signal */ +maxhandle_scanbuild = xc->maxhandle ? xc->maxhandle : 1; /*scan-build warning suppression, in reality we have at least one signal */ - xc->signal_lens = (uint32_t *)realloc(xc->signal_lens, maxhandle_scanbuild * sizeof(uint32_t)); - xc->signal_typs = (unsigned char *)realloc(xc->signal_typs, maxhandle_scanbuild * sizeof(unsigned char)); +xc->signal_lens = (uint32_t *)realloc(xc->signal_lens, maxhandle_scanbuild*sizeof(uint32_t)); +xc->signal_typs = (unsigned char *)realloc(xc->signal_typs, maxhandle_scanbuild*sizeof(unsigned char)); - free(xc->process_mask); - xc->process_mask = (unsigned char *)calloc(1, (maxhandle_scanbuild + 7) / 8); +free(xc->process_mask); +xc->process_mask = (unsigned char *)calloc(1, (maxhandle_scanbuild+7)/8); - free(xc->temp_signal_value_buf); - xc->temp_signal_value_buf = (unsigned char *)malloc(xc->longest_signal_value_len + 1); +free(xc->temp_signal_value_buf); +xc->temp_signal_value_buf = (unsigned char *)malloc(xc->longest_signal_value_len + 1); - xc->var_count = xc->maxhandle + xc->num_alias; +xc->var_count = xc->maxhandle + xc->num_alias; - free(str); - return (1); +free(str); +return(1); } + /* * reader file open/close functions */ int fstReaderInit(struct fstReaderContext *xc) { - fst_off_t blkpos = 0; - fst_off_t endfile; - uint64_t seclen; - int sectype; - uint64_t vc_section_count_actual = 0; - int hdr_incomplete = 0; - int hdr_seen = 0; - int gzread_pass_status = 1; - - sectype = fgetc(xc->f); - if (sectype == FST_BL_ZWRAPPER) { +fst_off_t blkpos = 0; +fst_off_t endfile; +uint64_t seclen; +int sectype; +uint64_t vc_section_count_actual = 0; +int hdr_incomplete = 0; +int hdr_seen = 0; +int gzread_pass_status = 1; + +sectype = fgetc(xc->f); +if(sectype == FST_BL_ZWRAPPER) + { FILE *fcomp; fst_off_t offpnt, uclen; char gz_membuf[FST_GZIO_LEN]; @@ -4234,292 +4604,334 @@ int fstReaderInit(struct fstReaderContext *xc) int zfd; int flen = strlen(xc->filename); char *hf; + int hf_len; seclen = fstReaderUint64(xc->f); uclen = fstReaderUint64(xc->f); - if (!seclen) - return (0); /* not finished compressing, this is a failed read */ + if(!seclen) return(0); /* not finished compressing, this is a failed read */ - size_t hf_size = flen + 16 + 32 + 1; - hf = (char *)calloc(1, hf_size); + hf_len = flen + 16 + 32 + 1; + hf = (char *)calloc(1, hf_len); - snprintf(hf, hf_size, "%s.upk_%d_%p", xc->filename, getpid(), (void *)xc); + snprintf(hf, hf_len, "%s.upk_%d_%p", xc->filename, getpid(), (void *)xc); fcomp = fopen(hf, "w+b"); - if (!fcomp) { - fcomp = tmpfile_open(&xc->f_nam); - free(hf); - hf = NULL; - if (!fcomp) { - tmpfile_close(&fcomp, &xc->f_nam); - return (0); - } - } - -#if defined(FST_MACOSX) - setvbuf(fcomp, (char *)NULL, _IONBF, 0); /* keeps gzip from acting weird in tandem with fopen */ + if(!fcomp) + { + fcomp = tmpfile_open(&xc->f_nam); + free(hf); hf = NULL; + if(!fcomp) { tmpfile_close(&fcomp, &xc->f_nam); return(0); } + } + +#if defined(FST_UNBUFFERED_IO) + setvbuf(fcomp, (char *)NULL, _IONBF, 0); /* keeps gzip from acting weird in tandem with fopen */ #endif #ifdef __MINGW32__ - setvbuf(fcomp, (char *)NULL, _IONBF, 0); /* keeps gzip from acting weird in tandem with fopen */ xc->filename_unpacked = hf; #else - if (hf) { - unlink(hf); - free(hf); - } + if(hf) + { + unlink(hf); + free(hf); + } #endif - fstReaderFseeko(xc, xc->f, 1 + 8 + 8, SEEK_SET); -#ifndef __MINGW32__ + fstReaderFseeko(xc, xc->f, FST_ZWRAPPER_HDR_SIZE, SEEK_SET); +#if !defined(__MINGW32__) && !defined(_MSC_VER) fflush(xc->f); +#else + /* Windows UCRT runtime library reads one byte ahead in the file + even with buffering disabled and does not synchronise the + file position after fseek. */ + _lseek(fileno(xc->f), FST_ZWRAPPER_HDR_SIZE, SEEK_SET); #endif zfd = dup(fileno(xc->f)); - lseek(zfd, ftell(xc->f), SEEK_SET); zhandle = gzdopen(zfd, "rb"); - if (zhandle) { - for (offpnt = 0; offpnt < uclen; offpnt += FST_GZIO_LEN) { - size_t this_len = ((uclen - offpnt) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - offpnt); - size_t gzreadlen = gzread(zhandle, gz_membuf, this_len); - size_t fwlen; - - if (gzreadlen != this_len) { - gzread_pass_status = 0; - break; - } - fwlen = fstFwrite(gz_membuf, this_len, 1, fcomp); - if (fwlen != 1) { - gzread_pass_status = 0; - break; - } - } - gzclose(zhandle); - } else { - close(zfd); - } + if(zhandle) + { + for(offpnt = 0; offpnt < uclen; offpnt += FST_GZIO_LEN) + { + size_t this_len = ((uclen - offpnt) > FST_GZIO_LEN) ? FST_GZIO_LEN : (uclen - offpnt); + size_t gzreadlen = gzread(zhandle, gz_membuf, this_len); + size_t fwlen; + + if(gzreadlen != this_len) + { + gzread_pass_status = 0; + break; + } + fwlen = fstFwrite(gz_membuf, this_len, 1, fcomp); + if(fwlen != 1) + { + gzread_pass_status = 0; + break; + } + } + gzclose(zhandle); + } + else + { + close(zfd); + } fflush(fcomp); fclose(xc->f); xc->f = fcomp; - } + } - if (gzread_pass_status) { +if(gzread_pass_status) + { fstReaderFseeko(xc, xc->f, 0, SEEK_END); endfile = ftello(xc->f); - while (blkpos < endfile) { - fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET); + while(blkpos < endfile) + { + fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET); - sectype = fgetc(xc->f); - seclen = fstReaderUint64(xc->f); + sectype = fgetc(xc->f); + seclen = fstReaderUint64(xc->f); - if (sectype == EOF) { - break; - } + if(sectype == EOF) + { + break; + } - if ((hdr_incomplete) && (!seclen)) { - break; - } + if((hdr_incomplete) && (!seclen)) + { + break; + } - if (!hdr_seen && (sectype != FST_BL_HDR)) { - break; - } + if(!hdr_seen && (sectype != FST_BL_HDR)) + { + break; + } - blkpos++; - if (sectype == FST_BL_HDR) { - if (!hdr_seen) { - int ch; - double dcheck; + blkpos++; + if(sectype == FST_BL_HDR) + { + if(!hdr_seen) + { + int ch; + double dcheck; + + xc->start_time = fstReaderUint64(xc->f); + xc->end_time = fstReaderUint64(xc->f); + + hdr_incomplete = (xc->start_time == 0) && (xc->end_time == 0); + + fstFread(&dcheck, 8, 1, xc->f); + xc->double_endian_match = (dcheck == FST_DOUBLE_ENDTEST); + if(!xc->double_endian_match) + { + union { + unsigned char rvs_buf[8]; + double d; + } vu; + + unsigned char *dcheck_alias = (unsigned char *)&dcheck; + int rvs_idx; + + for(rvs_idx=0;rvs_idx<8;rvs_idx++) + { + vu.rvs_buf[rvs_idx] = dcheck_alias[7-rvs_idx]; + } + if(vu.d != FST_DOUBLE_ENDTEST) + { + break; /* either corrupt file or wrong architecture (offset +33 also functions as matchword) */ + } + } - xc->start_time = fstReaderUint64(xc->f); - xc->end_time = fstReaderUint64(xc->f); + hdr_seen = 1; + + xc->mem_used_by_writer = fstReaderUint64(xc->f); + xc->scope_count = fstReaderUint64(xc->f); + xc->var_count = fstReaderUint64(xc->f); + xc->maxhandle = fstReaderUint64(xc->f); + xc->num_alias = xc->var_count - xc->maxhandle; + xc->vc_section_count = fstReaderUint64(xc->f); + ch = fgetc(xc->f); + xc->timescale = (signed char)ch; + fstFread(xc->version, FST_HDR_SIM_VERSION_SIZE, 1, xc->f); + xc->version[FST_HDR_SIM_VERSION_SIZE] = 0; + fstFread(xc->date, FST_HDR_DATE_SIZE, 1, xc->f); + xc->date[FST_HDR_DATE_SIZE] = 0; + ch = fgetc(xc->f); + xc->filetype = (unsigned char)ch; + xc->timezero = fstReaderUint64(xc->f); + } + } + else if((sectype == FST_BL_VCDATA) || (sectype == FST_BL_VCDATA_DYN_ALIAS) || (sectype == FST_BL_VCDATA_DYN_ALIAS2)) + { + if(hdr_incomplete) + { + uint64_t bt = fstReaderUint64(xc->f); + xc->end_time = fstReaderUint64(xc->f); - hdr_incomplete = (xc->start_time == 0) && (xc->end_time == 0); + if(!vc_section_count_actual) { xc->start_time = bt; } + } - fstFread(&dcheck, 8, 1, xc->f); - xc->double_endian_match = (dcheck == (double)FST_DOUBLE_ENDTEST); - if (!xc->double_endian_match) { - union + vc_section_count_actual++; + } + else if(sectype == FST_BL_GEOM) { - unsigned char rvs_buf[8]; - double d; - } vu; + if(!hdr_incomplete) + { + uint64_t clen = seclen - 24; + uint64_t uclen = fstReaderUint64(xc->f); + unsigned char *ucdata = (unsigned char *)malloc(uclen); + unsigned char *pnt = ucdata; + unsigned int i; + + xc->contains_geom_section = 1; + xc->maxhandle = fstReaderUint64(xc->f); + xc->longest_signal_value_len = 32; /* arbitrarily set at 32...this is much longer than an expanded double */ + + free(xc->process_mask); + xc->process_mask = (unsigned char *)calloc(1, (xc->maxhandle+7)/8); + + if(clen != uclen) + { + unsigned char *cdata = (unsigned char *)malloc(clen); + unsigned long destlen = uclen; + unsigned long sourcelen = clen; + int rc; + + fstFread(cdata, clen, 1, xc->f); + rc = uncompress(ucdata, &destlen, cdata, sourcelen); + + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderInit(), geom uncompress rc = %d, exiting.\n", rc); + exit(255); + } + + free(cdata); + } + else + { + fstFread(ucdata, uclen, 1, xc->f); + } + + free(xc->signal_lens); + xc->signal_lens = (uint32_t *)malloc(sizeof(uint32_t) * xc->maxhandle); + free(xc->signal_typs); + xc->signal_typs = (unsigned char *)malloc(sizeof(unsigned char) * xc->maxhandle); + + for(i=0;imaxhandle;i++) + { + int skiplen; + uint64_t val = fstGetVarint32(pnt, &skiplen); + + pnt += skiplen; + + if(val) + { + xc->signal_lens[i] = (val != 0xFFFFFFFF) ? val : 0; + xc->signal_typs[i] = FST_VT_VCD_WIRE; + if(xc->signal_lens[i] > xc->longest_signal_value_len) + { + xc->longest_signal_value_len = xc->signal_lens[i]; + } + } + else + { + xc->signal_lens[i] = 8; /* backpatch in real */ + xc->signal_typs[i] = FST_VT_VCD_REAL; + /* xc->longest_signal_value_len handled above by overly large init size */ + } + } - unsigned char *dcheck_alias = (unsigned char *)&dcheck; - int rvs_idx; + free(xc->temp_signal_value_buf); + xc->temp_signal_value_buf = (unsigned char *)malloc(xc->longest_signal_value_len + 1); - for (rvs_idx = 0; rvs_idx < 8; rvs_idx++) { - vu.rvs_buf[rvs_idx] = dcheck_alias[7 - rvs_idx]; + free(ucdata); + } } - if (vu.d != FST_DOUBLE_ENDTEST) { - break; /* either corrupt file or wrong architecture (offset +33 also functions as matchword) - */ + else if(sectype == FST_BL_HIER) + { + xc->contains_hier_section = 1; + xc->hier_pos = ftello(xc->f); } - } - - hdr_seen = 1; - - xc->mem_used_by_writer = fstReaderUint64(xc->f); - xc->scope_count = fstReaderUint64(xc->f); - xc->var_count = fstReaderUint64(xc->f); - xc->maxhandle = fstReaderUint64(xc->f); - xc->num_alias = xc->var_count - xc->maxhandle; - xc->vc_section_count = fstReaderUint64(xc->f); - ch = fgetc(xc->f); - xc->timescale = (signed char)ch; - fstFread(xc->version, FST_HDR_SIM_VERSION_SIZE, 1, xc->f); - xc->version[FST_HDR_SIM_VERSION_SIZE] = 0; - fstFread(xc->date, FST_HDR_DATE_SIZE, 1, xc->f); - xc->date[FST_HDR_DATE_SIZE] = 0; - ch = fgetc(xc->f); - xc->filetype = (unsigned char)ch; - xc->timezero = fstReaderUint64(xc->f); - } - } else if ((sectype == FST_BL_VCDATA) || (sectype == FST_BL_VCDATA_DYN_ALIAS) || - (sectype == FST_BL_VCDATA_DYN_ALIAS2)) { - if (hdr_incomplete) { - uint64_t bt = fstReaderUint64(xc->f); - xc->end_time = fstReaderUint64(xc->f); - - if (!vc_section_count_actual) { - xc->start_time = bt; - } - } - - vc_section_count_actual++; - } else if (sectype == FST_BL_GEOM) { - if (!hdr_incomplete) { - uint64_t clen = seclen - 24; - uint64_t uclen = fstReaderUint64(xc->f); - unsigned char *ucdata = (unsigned char *)malloc(uclen); - unsigned char *pnt = ucdata; - unsigned int i; - - xc->contains_geom_section = 1; - xc->maxhandle = fstReaderUint64(xc->f); - xc->longest_signal_value_len = - 32; /* arbitrarily set at 32...this is much longer than an expanded double */ - - free(xc->process_mask); - xc->process_mask = (unsigned char *)calloc(1, (xc->maxhandle + 7) / 8); - - if (clen != uclen) { - unsigned char *cdata = (unsigned char *)malloc(clen); - unsigned long destlen = uclen; - unsigned long sourcelen = clen; - int rc; - - fstFread(cdata, clen, 1, xc->f); - rc = uncompress(ucdata, &destlen, cdata, sourcelen); - - if (rc != Z_OK) { - fprintf(stderr, FST_APIMESS "fstReaderInit(), geom uncompress rc = %d, exiting.\n", rc); - exit(255); + else if(sectype == FST_BL_HIER_LZ4DUO) + { + xc->contains_hier_section_lz4 = 1; + xc->contains_hier_section_lz4duo = 1; + xc->hier_pos = ftello(xc->f); } - - free(cdata); - } else { - fstFread(ucdata, uclen, 1, xc->f); - } - - free(xc->signal_lens); - xc->signal_lens = (uint32_t *)malloc(sizeof(uint32_t) * xc->maxhandle); - free(xc->signal_typs); - xc->signal_typs = (unsigned char *)malloc(sizeof(unsigned char) * xc->maxhandle); - - for (i = 0; i < xc->maxhandle; i++) { - int skiplen; - uint64_t val = fstGetVarint32(pnt, &skiplen); - - pnt += skiplen; - - if (val) { - xc->signal_lens[i] = (val != 0xFFFFFFFF) ? val : 0; - xc->signal_typs[i] = FST_VT_VCD_WIRE; - if (xc->signal_lens[i] > xc->longest_signal_value_len) { - xc->longest_signal_value_len = xc->signal_lens[i]; - } - } else { - xc->signal_lens[i] = 8; /* backpatch in real */ - xc->signal_typs[i] = FST_VT_VCD_REAL; - /* xc->longest_signal_value_len handled above by overly large init size */ + else if(sectype == FST_BL_HIER_LZ4) + { + xc->contains_hier_section_lz4 = 1; + xc->hier_pos = ftello(xc->f); + } + else if(sectype == FST_BL_BLACKOUT) + { + uint32_t i; + uint64_t cur_bl = 0; + uint64_t delta; + + xc->num_blackouts = fstReaderVarint32(xc->f); + free(xc->blackout_times); + xc->blackout_times = (uint64_t *)calloc(xc->num_blackouts, sizeof(uint64_t)); + free(xc->blackout_activity); + xc->blackout_activity = (unsigned char *)calloc(xc->num_blackouts, sizeof(unsigned char)); + + for(i=0;inum_blackouts;i++) + { + xc->blackout_activity[i] = fgetc(xc->f) != 0; + delta = fstReaderVarint64(xc->f); + cur_bl += delta; + xc->blackout_times[i] = cur_bl; + } } - } - - free(xc->temp_signal_value_buf); - xc->temp_signal_value_buf = (unsigned char *)malloc(xc->longest_signal_value_len + 1); - - free(ucdata); - } - } else if (sectype == FST_BL_HIER) { - xc->contains_hier_section = 1; - xc->hier_pos = ftello(xc->f); - } else if (sectype == FST_BL_HIER_LZ4DUO) { - xc->contains_hier_section_lz4 = 1; - xc->contains_hier_section_lz4duo = 1; - xc->hier_pos = ftello(xc->f); - } else if (sectype == FST_BL_HIER_LZ4) { - xc->contains_hier_section_lz4 = 1; - xc->hier_pos = ftello(xc->f); - } else if (sectype == FST_BL_BLACKOUT) { - uint32_t i; - uint64_t cur_bl = 0; - uint64_t delta; - - xc->num_blackouts = fstReaderVarint32(xc->f); - free(xc->blackout_times); - xc->blackout_times = (uint64_t *)calloc(xc->num_blackouts, sizeof(uint64_t)); - free(xc->blackout_activity); - xc->blackout_activity = (unsigned char *)calloc(xc->num_blackouts, sizeof(unsigned char)); - for (i = 0; i < xc->num_blackouts; i++) { - xc->blackout_activity[i] = fgetc(xc->f) != 0; - delta = fstReaderVarint64(xc->f); - cur_bl += delta; - xc->blackout_times[i] = cur_bl; + blkpos += seclen; + if(!hdr_seen) break; } - } - blkpos += seclen; - if (!hdr_seen) - break; - } - - if (hdr_seen) { - if (xc->vc_section_count != vc_section_count_actual) { - xc->vc_section_count = vc_section_count_actual; - } + if(hdr_seen) + { + if(xc->vc_section_count != vc_section_count_actual) + { + xc->vc_section_count = vc_section_count_actual; + } - if (!xc->contains_geom_section) { - fstReaderProcessHier(xc, NULL); /* recreate signal_lens/signal_typs info */ - } + if(!xc->contains_geom_section) + { + fstReaderProcessHier(xc, NULL); /* recreate signal_lens/signal_typs info */ + } + } } - } - return (hdr_seen); +return(hdr_seen); } + void *fstReaderOpenForUtilitiesOnly(void) { - struct fstReaderContext *xc = (struct fstReaderContext *)calloc(1, sizeof(struct fstReaderContext)); +struct fstReaderContext *xc = (struct fstReaderContext *)calloc(1, sizeof(struct fstReaderContext)); - return (xc); +return(xc); } + void *fstReaderOpen(const char *nam) { - struct fstReaderContext *xc = (struct fstReaderContext *)calloc(1, sizeof(struct fstReaderContext)); +struct fstReaderContext *xc = (struct fstReaderContext *)calloc(1, sizeof(struct fstReaderContext)); - if ((!nam) || (!(xc->f = fopen(nam, "rb")))) { +if((!nam)||(!(xc->f=fopen(nam, "rb")))) + { free(xc); - xc = NULL; - } else { + xc=NULL; + } + else + { int flen = strlen(nam); char *hf = (char *)calloc(1, flen + 6); int rc; -#if defined(__MINGW32__) || defined(FST_MACOSX) - setvbuf(xc->f, (char *)NULL, _IONBF, 0); /* keeps gzip from acting weird in tandem with fopen */ +#if defined(FST_UNBUFFERED_IO) + setvbuf(xc->f, (char *)NULL, _IONBF, 0); /* keeps gzip from acting weird in tandem with fopen */ #endif memcpy(hf, nam, flen); @@ -4530,151 +4942,147 @@ void *fstReaderOpen(const char *nam) xc->filename = strdup(nam); rc = fstReaderInit(xc); - if ((rc) && (xc->vc_section_count) && (xc->maxhandle) && - ((xc->fh) || (xc->contains_hier_section || (xc->contains_hier_section_lz4)))) { - /* more init */ - xc->do_rewind = 1; - } else { - fstReaderClose(xc); - xc = NULL; + if((rc) && (xc->vc_section_count) && (xc->maxhandle) && ((xc->fh)||(xc->contains_hier_section||(xc->contains_hier_section_lz4)))) + { + /* more init */ + xc->do_rewind = 1; + } + else + { + fstReaderClose(xc); + xc = NULL; + } } - } - return (xc); +return(xc); } + static void fstReaderDeallocateRvatData(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { - free(xc->rvat_chain_mem); - xc->rvat_chain_mem = NULL; - free(xc->rvat_frame_data); - xc->rvat_frame_data = NULL; - free(xc->rvat_time_table); - xc->rvat_time_table = NULL; - free(xc->rvat_chain_table); - xc->rvat_chain_table = NULL; - free(xc->rvat_chain_table_lengths); - xc->rvat_chain_table_lengths = NULL; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +if(xc) + { + free(xc->rvat_chain_mem); xc->rvat_chain_mem = NULL; + free(xc->rvat_frame_data); xc->rvat_frame_data = NULL; + free(xc->rvat_time_table); xc->rvat_time_table = NULL; + free(xc->rvat_chain_table); xc->rvat_chain_table = NULL; + free(xc->rvat_chain_table_lengths); xc->rvat_chain_table_lengths = NULL; xc->rvat_data_valid = 0; - } + } } + void fstReaderClose(void *ctx) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - if (xc) { +if(xc) + { fstReaderDeallocateScopeData(xc); fstReaderDeallocateRvatData(xc); - free(xc->rvat_sig_offs); - xc->rvat_sig_offs = NULL; - - free(xc->process_mask); - xc->process_mask = NULL; - free(xc->blackout_times); - xc->blackout_times = NULL; - free(xc->blackout_activity); - xc->blackout_activity = NULL; - free(xc->temp_signal_value_buf); - xc->temp_signal_value_buf = NULL; - free(xc->signal_typs); - xc->signal_typs = NULL; - free(xc->signal_lens); - xc->signal_lens = NULL; - free(xc->filename); - xc->filename = NULL; - - if (xc->fh) { - tmpfile_close(&xc->fh, &xc->fh_nam); - } - - if (xc->f) { - tmpfile_close(&xc->f, &xc->f_nam); - if (xc->filename_unpacked) { - unlink(xc->filename_unpacked); - free(xc->filename_unpacked); - } - } + free(xc->rvat_sig_offs); xc->rvat_sig_offs = NULL; + + free(xc->process_mask); xc->process_mask = NULL; + free(xc->blackout_times); xc->blackout_times = NULL; + free(xc->blackout_activity); xc->blackout_activity = NULL; + free(xc->temp_signal_value_buf); xc->temp_signal_value_buf = NULL; + free(xc->signal_typs); xc->signal_typs = NULL; + free(xc->signal_lens); xc->signal_lens = NULL; + free(xc->filename); xc->filename = NULL; + free(xc->str_scope_attr); xc->str_scope_attr = NULL; + + if(xc->fh) + { + tmpfile_close(&xc->fh, &xc->fh_nam); + } + + if(xc->f) + { + tmpfile_close(&xc->f, &xc->f_nam); + if(xc->filename_unpacked) + { + unlink(xc->filename_unpacked); + free(xc->filename_unpacked); + } + } free(xc); - } + } } + /* * read processing */ /* normal read which re-interleaves the value change data */ int fstReaderIterBlocks(void *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 *fv) + void (*value_change_callback)(void *user_callback_data_pointer, uint64_t time, fstHandle facidx, const unsigned char *value), + void *user_callback_data_pointer, FILE *fv) { - return (fstReaderIterBlocks2(ctx, value_change_callback, NULL, user_callback_data_pointer, fv)); +return(fstReaderIterBlocks2(ctx, value_change_callback, NULL, user_callback_data_pointer, fv)); } + int fstReaderIterBlocks2(void *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 *fv) -{ - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - - uint64_t previous_time = UINT64_MAX; - uint64_t *time_table = NULL; - uint64_t tsec_nitems; - unsigned int secnum = 0; - int blocks_skipped = 0; - fst_off_t blkpos = 0; - uint64_t seclen, beg_tim; - uint64_t end_tim; - uint64_t frame_uclen, frame_clen, frame_maxhandle, vc_maxhandle; - fst_off_t vc_start; - fst_off_t indx_pntr, indx_pos; - fst_off_t *chain_table = NULL; - uint32_t *chain_table_lengths = NULL; - unsigned char *chain_cmem; - unsigned char *pnt; - long chain_clen; - fstHandle idx, pidx = 0, i; - uint64_t pval; - uint64_t vc_maxhandle_largest = 0; - uint64_t tsec_uclen = 0, tsec_clen = 0; - int sectype; - uint64_t mem_required_for_traversal; - unsigned char *mem_for_traversal = NULL; - uint32_t traversal_mem_offs; - uint32_t *scatterptr, *headptr, *length_remaining; - uint32_t cur_blackout = 0; - int packtype; - unsigned char *mc_mem = NULL; - uint32_t mc_mem_len; /* corresponds to largest value encountered in chain_table_lengths[i] */ - int dumpvars_state = 0; - - if (!xc) - return (0); - - scatterptr = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); - headptr = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); - length_remaining = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); - - if (fv) { + 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 *fv) +{ +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; + +uint64_t previous_time = UINT64_MAX; +uint64_t *time_table = NULL; +uint64_t tsec_nitems; +unsigned int secnum = 0; +int blocks_skipped = 0; +fst_off_t blkpos = 0; +uint64_t seclen, beg_tim; +uint64_t end_tim; +uint64_t frame_uclen, frame_clen, frame_maxhandle, vc_maxhandle; +fst_off_t vc_start; +fst_off_t indx_pntr, indx_pos; +fst_off_t *chain_table = NULL; +uint32_t *chain_table_lengths = NULL; +unsigned char *chain_cmem; +unsigned char *pnt; +long chain_clen; +fstHandle idx, pidx=0, i; +uint64_t pval; +uint64_t vc_maxhandle_largest = 0; +uint64_t tsec_uclen = 0, tsec_clen = 0; +int sectype; +uint64_t mem_required_for_traversal; +unsigned char *mem_for_traversal = NULL; +uint32_t traversal_mem_offs; +uint32_t *scatterptr, *headptr, *length_remaining; +uint32_t cur_blackout = 0; +int packtype; +unsigned char *mc_mem = NULL; +uint32_t mc_mem_len; /* corresponds to largest value encountered in chain_table_lengths[i] */ +int dumpvars_state = 0; + +if(!xc) return(0); + +scatterptr = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); +headptr = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); +length_remaining = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); + +if(fv) + { #ifndef FST_WRITEX_DISABLE fflush(fv); - setvbuf(fv, (char *)NULL, _IONBF, - 0); /* even buffered IO is slow so disable it and use our own routines that don't need seeking */ + setvbuf(fv, (char *) NULL, _IONBF, 0); /* even buffered IO is slow so disable it and use our own routines that don't need seeking */ xc->writex_fd = fileno(fv); #endif - } + } - for (;;) { +for(;;) + { uint32_t *tc_head = NULL; + uint32_t tc_head_items = 0; traversal_mem_offs = 0; fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET); @@ -4682,292 +5090,372 @@ int fstReaderIterBlocks2(void *ctx, sectype = fgetc(xc->f); seclen = fstReaderUint64(xc->f); - if ((sectype == EOF) || (sectype == FST_BL_SKIP)) { + if((sectype == EOF) || (sectype == FST_BL_SKIP)) + { #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "<< EOF >>\n"); + fprintf(stderr, FST_APIMESS "<< EOF >>\n"); #endif - break; - } + break; + } blkpos++; - if ((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) && - (sectype != FST_BL_VCDATA_DYN_ALIAS2)) { - blkpos += seclen; - continue; - } + if((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) && (sectype != FST_BL_VCDATA_DYN_ALIAS2)) + { + blkpos += seclen; + continue; + } - if (!seclen) - break; + if(!seclen) break; beg_tim = fstReaderUint64(xc->f); end_tim = fstReaderUint64(xc->f); - if (xc->limit_range_valid) { - if (end_tim < xc->limit_range_start) { - blocks_skipped++; - blkpos += seclen; - continue; - } + if(xc->limit_range_valid) + { + if(end_tim < xc->limit_range_start) + { + blocks_skipped++; + blkpos += seclen; + continue; + } + + if(beg_tim > xc->limit_range_end) /* likely the compare in for(i=0;i - xc->limit_range_end) /* likely the compare in for(i=0;if); - mem_for_traversal = - (unsigned char *)malloc(mem_required_for_traversal + 66); /* add in potential fastlz overhead */ + mem_required_for_traversal = fstReaderUint64(xc->f) + 66; /* add in potential fastlz overhead */ + mem_for_traversal = (unsigned char *)malloc(mem_required_for_traversal); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "sec: %u seclen: %d begtim: %d endtim: %d\n", secnum, (int)seclen, (int)beg_tim, - (int)end_tim); - fprintf(stderr, FST_APIMESS "mem_required_for_traversal: %d\n", (int)mem_required_for_traversal); + fprintf(stderr, FST_APIMESS "sec: %u seclen: %d begtim: %d endtim: %d\n", + secnum, (int)seclen, (int)beg_tim, (int)end_tim); + fprintf(stderr, FST_APIMESS "mem_required_for_traversal: %d\n", (int)mem_required_for_traversal-66); #endif /* process time block */ { - unsigned char *ucdata; - unsigned char *cdata; - unsigned long destlen /* = tsec_uclen */; /* scan-build */ - unsigned long sourcelen /*= tsec_clen */; /* scan-build */ - int rc; - unsigned char *tpnt; - uint64_t tpval; - unsigned int ti; + unsigned char *ucdata; + unsigned char *cdata; + unsigned long destlen /* = tsec_uclen */; /* scan-build */ + unsigned long sourcelen /*= tsec_clen */; /* scan-build */ + int rc; + unsigned char *tpnt; + uint64_t tpval; + unsigned int ti; - if (fstReaderFseeko(xc, xc->f, blkpos + seclen - 24, SEEK_SET) != 0) - break; - tsec_uclen = fstReaderUint64(xc->f); - tsec_clen = fstReaderUint64(xc->f); - tsec_nitems = fstReaderUint64(xc->f); + if(fstReaderFseeko(xc, xc->f, blkpos + seclen - 24, SEEK_SET) != 0) break; + tsec_uclen = fstReaderUint64(xc->f); + tsec_clen = fstReaderUint64(xc->f); + tsec_nitems = fstReaderUint64(xc->f); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "time section unc: %d, com: %d (%d items)\n", (int)tsec_uclen, (int)tsec_clen, - (int)tsec_nitems); + fprintf(stderr, FST_APIMESS "time section unc: %d, com: %d (%d items)\n", + (int)tsec_uclen, (int)tsec_clen, (int)tsec_nitems); #endif - if (tsec_clen > seclen) - break; /* corrupted tsec_clen: by definition it can't be larger than size of section */ - ucdata = (unsigned char *)malloc(tsec_uclen); - if (!ucdata) - break; /* malloc fail as tsec_uclen out of range from corrupted file */ - destlen = tsec_uclen; - sourcelen = tsec_clen; + if(tsec_clen > seclen) break; /* corrupted tsec_clen: by definition it can't be larger than size of section */ + ucdata = (unsigned char *)malloc(tsec_uclen); + if(!ucdata) break; /* malloc fail as tsec_uclen out of range from corrupted file */ + destlen = tsec_uclen; + sourcelen = tsec_clen; - fstReaderFseeko(xc, xc->f, -24 - ((fst_off_t)tsec_clen), SEEK_CUR); + fstReaderFseeko(xc, xc->f, -24 - ((fst_off_t)tsec_clen), SEEK_CUR); - if (tsec_uclen != tsec_clen) { + if(tsec_uclen != tsec_clen) + { cdata = (unsigned char *)malloc(tsec_clen); fstFread(cdata, tsec_clen, 1, xc->f); rc = uncompress(ucdata, &destlen, cdata, sourcelen); - if (rc != Z_OK) { - fprintf(stderr, FST_APIMESS "fstReaderIterBlocks2(), tsec uncompress rc = %d, exiting.\n", rc); - exit(255); - } + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderIterBlocks2(), tsec uncompress rc = %d, exiting.\n", rc); + exit(255); + } free(cdata); - } else { + } + else + { fstFread(ucdata, tsec_uclen, 1, xc->f); - } + } - free(time_table); - time_table = (uint64_t *)calloc(tsec_nitems, sizeof(uint64_t)); - tpnt = ucdata; - tpval = 0; - for (ti = 0; ti < tsec_nitems; ti++) { + free(time_table); + + if(sizeof(size_t) < sizeof(uint64_t)) + { + /* TALOS-2023-1792 for 32b overflow */ + uint64_t chk_64 = tsec_nitems * sizeof(uint64_t); + size_t chk_32 = ((size_t)tsec_nitems) * sizeof(uint64_t); + if(chk_64 != chk_32) chk_report_abort("TALOS-2023-1792"); + } + else + { + uint64_t chk_64 = tsec_nitems * sizeof(uint64_t); + if((chk_64/sizeof(uint64_t)) != tsec_nitems) + { + chk_report_abort("TALOS-2023-1792"); + } + } + time_table = (uint64_t *)calloc(tsec_nitems, sizeof(uint64_t)); + tpnt = ucdata; + tpval = 0; + for(ti=0;tif, blkpos + 32, SEEK_SET); + fstReaderFseeko(xc, xc->f, blkpos+32, SEEK_SET); frame_uclen = fstReaderVarint64(xc->f); frame_clen = fstReaderVarint64(xc->f); frame_maxhandle = fstReaderVarint64(xc->f); - if (secnum == 0) { - if ((beg_tim != time_table[0]) || (blocks_skipped)) { - unsigned char *mu = (unsigned char *)malloc(frame_uclen); - uint32_t sig_offs = 0; - - if (fv) { - char wx_buf[32]; - int wx_len; - - if (beg_tim) { - if (dumpvars_state == 1) { - wx_len = snprintf(wx_buf, 6, "$end\n"); - fstWritex(xc, wx_buf, wx_len); - dumpvars_state = 2; - } - wx_len = snprintf(wx_buf, 20, "#%" PRIu64 "\n", beg_tim); - fstWritex(xc, wx_buf, wx_len); - if (!dumpvars_state) { - wx_len = snprintf(wx_buf, 11, "$dumpvars\n"); - fstWritex(xc, wx_buf, wx_len); - dumpvars_state = 1; - } - } - if ((xc->num_blackouts) && (cur_blackout != xc->num_blackouts)) { - if (beg_tim == xc->blackout_times[cur_blackout]) { - wx_len = snprintf(wx_buf, 16, "$dump%s $end\n", - (xc->blackout_activity[cur_blackout++]) ? "on" : "off"); - fstWritex(xc, wx_buf, wx_len); - } - } - } - - if (frame_uclen == frame_clen) { - fstFread(mu, frame_uclen, 1, xc->f); - } else { - unsigned char *mc = (unsigned char *)malloc(frame_clen); - int rc; - - unsigned long destlen = frame_uclen; - unsigned long sourcelen = frame_clen; + if(secnum == 0) + { + if((beg_tim != time_table[0]) || (blocks_skipped)) + { + unsigned char *mu = (unsigned char *)malloc(frame_uclen); + uint32_t sig_offs = 0; - fstFread(mc, sourcelen, 1, xc->f); - rc = uncompress(mu, &destlen, mc, sourcelen); - if (rc != Z_OK) { - fprintf(stderr, FST_APIMESS "fstReaderIterBlocks2(), frame uncompress rc: %d, exiting.\n", rc); - exit(255); - } - free(mc); - } - - for (idx = 0; idx < frame_maxhandle; idx++) { - int process_idx = idx / 8; - int process_bit = idx & 7; - - if (xc->process_mask[process_idx] & (1 << process_bit)) { - if (xc->signal_lens[idx] <= 1) { - if (xc->signal_lens[idx] == 1) { - unsigned char val = mu[sig_offs]; - if (value_change_callback) { - xc->temp_signal_value_buf[0] = val; - xc->temp_signal_value_buf[1] = 0; - value_change_callback(user_callback_data_pointer, beg_tim, idx + 1, - xc->temp_signal_value_buf); - } else { - if (fv) { - char vcd_id[16]; + if(fv) + { + char wx_buf[32]; + int wx_len; - int vcdid_len = fstVcdIDForFwrite(vcd_id + 1, idx + 1); - vcd_id[0] = val; /* collapse 3 writes into one I/O call */ - vcd_id[vcdid_len + 1] = '\n'; - fstWritex(xc, vcd_id, vcdid_len + 2); - } + if(beg_tim) + { + if(dumpvars_state == 1) { wx_len = snprintf(wx_buf, 32, "$end\n"); fstWritex(xc, wx_buf, wx_len); dumpvars_state = 2; } + wx_len = snprintf(wx_buf, 32, "#%" PRIu64 "\n", beg_tim); + fstWritex(xc, wx_buf, wx_len); + if(!dumpvars_state) { wx_len = snprintf(wx_buf, 32, "$dumpvars\n"); fstWritex(xc, wx_buf, wx_len); dumpvars_state = 1; } + } + if((xc->num_blackouts)&&(cur_blackout != xc->num_blackouts)) + { + if(beg_tim == xc->blackout_times[cur_blackout]) + { + wx_len = snprintf(wx_buf, 32, "$dump%s $end\n", (xc->blackout_activity[cur_blackout++]) ? "on" : "off"); + fstWritex(xc, wx_buf, wx_len); + } + } } - } else { - /* variable-length ("0" length) records have no initial state */ - } - } else { - if (xc->signal_typs[idx] != FST_VT_VCD_REAL) { - if (value_change_callback) { - memcpy(xc->temp_signal_value_buf, mu + sig_offs, xc->signal_lens[idx]); - xc->temp_signal_value_buf[xc->signal_lens[idx]] = 0; - value_change_callback(user_callback_data_pointer, beg_tim, idx + 1, - xc->temp_signal_value_buf); - } else { - if (fv) { - char vcd_id[16]; - int vcdid_len = fstVcdIDForFwrite(vcd_id + 1, idx + 1); - vcd_id[0] = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; - fstWritex(xc, vcd_id, 1); - fstWritex(xc, mu + sig_offs, xc->signal_lens[idx]); - - vcd_id[0] = ' '; /* collapse 3 writes into one I/O call */ - vcd_id[vcdid_len + 1] = '\n'; - fstWritex(xc, vcd_id, vcdid_len + 2); - } + if(frame_uclen == frame_clen) + { + fstFread(mu, frame_uclen, 1, xc->f); } - } else { - double d; - unsigned char *clone_d; - unsigned char *srcdata = mu + sig_offs; - - if (value_change_callback) { - if (xc->native_doubles_for_cb) { - if (xc->double_endian_match) { - clone_d = srcdata; - } else { - int j; - - clone_d = (unsigned char *)&d; - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } - } - value_change_callback(user_callback_data_pointer, beg_tim, idx + 1, clone_d); - } else { - clone_d = (unsigned char *)&d; - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; - - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } + else + { + unsigned char *mc = (unsigned char *)malloc(frame_clen); + int rc; + + unsigned long destlen = frame_uclen; + unsigned long sourcelen = frame_clen; + + fstFread(mc, sourcelen, 1, xc->f); + rc = uncompress(mu, &destlen, mc, sourcelen); + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderIterBlocks2(), frame uncompress rc: %d, exiting.\n", rc); + exit(255); } - snprintf((char *)xc->temp_signal_value_buf, 32, "%.16g", d); - value_change_callback(user_callback_data_pointer, beg_tim, idx + 1, - xc->temp_signal_value_buf); - } - } else { - if (fv) { - char vcdid_buf[16]; - char wx_buf[64]; - int wx_len; - - clone_d = (unsigned char *)&d; - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; - - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } + free(mc); + } + + + for(idx=0;idxprocess_mask[process_idx]&(1<signal_lens[idx] <= 1) + { + if(xc->signal_lens[idx] == 1) + { + unsigned char val = mu[sig_offs]; + if(value_change_callback) + { + xc->temp_signal_value_buf[0] = val; + xc->temp_signal_value_buf[1] = 0; + value_change_callback(user_callback_data_pointer, beg_tim, idx+1, xc->temp_signal_value_buf); + } + else + { + if(fv) + { + char vcd_id[16]; + + int vcdid_len = fstVcdIDForFwrite(vcd_id+1, idx+1); + vcd_id[0] = val; /* collapse 3 writes into one I/O call */ + vcd_id[vcdid_len + 1] = '\n'; + fstWritex(xc, vcd_id, vcdid_len + 2); + } + } + } + else + { + /* variable-length ("0" length) records have no initial state */ + } + } + else + { + if(xc->signal_typs[idx] != FST_VT_VCD_REAL) + { + if(value_change_callback) + { + if(xc->signal_lens[idx] > xc->longest_signal_value_len) + { + chk_report_abort("TALOS-2023-1797"); + } + memcpy(xc->temp_signal_value_buf, mu+sig_offs, xc->signal_lens[idx]); + xc->temp_signal_value_buf[xc->signal_lens[idx]] = 0; + value_change_callback(user_callback_data_pointer, beg_tim, idx+1, xc->temp_signal_value_buf); + } + else + { + if(fv) + { + char vcd_id[16]; + int vcdid_len = fstVcdIDForFwrite(vcd_id+1, idx+1); + + vcd_id[0] = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; + fstWritex(xc, vcd_id, 1); + if((sig_offs + xc->signal_lens[idx]) > frame_uclen) + { + chk_report_abort("TALOS-2023-1793"); + } + fstWritex(xc,mu+sig_offs, xc->signal_lens[idx]); + + vcd_id[0] = ' '; /* collapse 3 writes into one I/O call */ + vcd_id[vcdid_len + 1] = '\n'; + fstWritex(xc, vcd_id, vcdid_len + 2); + } + } + } + else + { + double d; + unsigned char *clone_d; + unsigned char *srcdata = mu+sig_offs; + + if(value_change_callback) + { + if(xc->native_doubles_for_cb) + { + if(xc->double_endian_match) + { + clone_d = srcdata; + } + else + { + int j; + + clone_d = (unsigned char *)&d; + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + value_change_callback(user_callback_data_pointer, beg_tim, idx+1, clone_d); + } + else + { + clone_d = (unsigned char *)&d; + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; + + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + snprintf((char *)xc->temp_signal_value_buf, xc->longest_signal_value_len + 1, "%.16g", d); + value_change_callback(user_callback_data_pointer, beg_tim, idx+1, xc->temp_signal_value_buf); + } + } + else + { + if(fv) + { + char vcdid_buf[16]; + char wx_buf[64]; + int wx_len; + + clone_d = (unsigned char *)&d; + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; + + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + + fstVcdID(vcdid_buf, idx+1); + wx_len = snprintf(wx_buf, 64, "r%.16g %s\n", d, vcdid_buf); + fstWritex(xc, wx_buf, wx_len); + } + } + } + } } - fstVcdID(vcdid_buf, idx + 1); - wx_len = snprintf(wx_buf, sizeof(wx_buf), "r%.16g %s\n", d, vcdid_buf); - fstWritex(xc, wx_buf, wx_len); - } + sig_offs += xc->signal_lens[idx]; } - } - } - } - sig_offs += xc->signal_lens[idx]; + free(mu); + fstReaderFseeko(xc, xc->f, -((fst_off_t)frame_clen), SEEK_CUR); + } } - free(mu); - fstReaderFseeko(xc, xc->f, -((fst_off_t)frame_clen), SEEK_CUR); - } - } - fstReaderFseeko(xc, xc->f, (fst_off_t)frame_clen, SEEK_CUR); /* skip past compressed data */ vc_maxhandle = fstReaderVarint64(xc->f); - vc_start = ftello(xc->f); /* points to '!' character */ + vc_start = ftello(xc->f); /* points to '!' character */ packtype = fgetc(xc->f); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "frame_uclen: %d, frame_clen: %d, frame_maxhandle: %d\n", (int)frame_uclen, - (int)frame_clen, (int)frame_maxhandle); + fprintf(stderr, FST_APIMESS "frame_uclen: %d, frame_clen: %d, frame_maxhandle: %d\n", + (int)frame_uclen, (int)frame_clen, (int)frame_maxhandle); fprintf(stderr, FST_APIMESS "vc_maxhandle: %d, packtype: %c\n", (int)vc_maxhandle, packtype); #endif - indx_pntr = blkpos + seclen - 24 - tsec_clen - 8; + indx_pntr = blkpos + seclen - 24 -tsec_clen -8; fstReaderFseeko(xc, xc->f, indx_pntr, SEEK_SET); chain_clen = fstReaderUint64(xc->f); indx_pos = indx_pntr - chain_clen; @@ -4975,106 +5463,168 @@ int fstReaderIterBlocks2(void *ctx, fprintf(stderr, FST_APIMESS "indx_pos: %d (%d bytes)\n", (int)indx_pos, (int)chain_clen); #endif chain_cmem = (unsigned char *)malloc(chain_clen); - if (!chain_cmem) - goto block_err; + if(!chain_cmem) goto block_err; fstReaderFseeko(xc, xc->f, indx_pos, SEEK_SET); fstFread(chain_cmem, chain_clen, 1, xc->f); - if (vc_maxhandle > vc_maxhandle_largest) { - free(chain_table); - free(chain_table_lengths); - - vc_maxhandle_largest = vc_maxhandle; - chain_table = (fst_off_t *)calloc((vc_maxhandle + 1), sizeof(fst_off_t)); - chain_table_lengths = (uint32_t *)calloc((vc_maxhandle + 1), sizeof(uint32_t)); - } + if(vc_maxhandle > vc_maxhandle_largest) + { + free(chain_table); + free(chain_table_lengths); + + vc_maxhandle_largest = vc_maxhandle; + + if(!(vc_maxhandle+1)) + { + chk_report_abort("TALOS-2023-1798"); + } + + if(sizeof(size_t) < sizeof(uint64_t)) + { + /* TALOS-2023-1798 for 32b overflow */ + uint64_t chk_64 = (vc_maxhandle+1) * sizeof(fst_off_t); + size_t chk_32 = ((size_t)(vc_maxhandle+1)) * sizeof(fst_off_t); + if(chk_64 != chk_32) chk_report_abort("TALOS-2023-1798"); + } + else + { + uint64_t chk_64 = (vc_maxhandle+1) * sizeof(fst_off_t); + if((chk_64/sizeof(fst_off_t)) != (vc_maxhandle+1)) + { + chk_report_abort("TALOS-2023-1798"); + } + } + chain_table = (fst_off_t *)calloc((vc_maxhandle+1), sizeof(fst_off_t)); + + if(sizeof(size_t) < sizeof(uint64_t)) + { + /* TALOS-2023-1798 for 32b overflow */ + uint64_t chk_64 = (vc_maxhandle+1) * sizeof(uint32_t); + size_t chk_32 = ((size_t)(vc_maxhandle+1)) * sizeof(uint32_t); + if(chk_64 != chk_32) chk_report_abort("TALOS-2023-1798"); + } + else + { + uint64_t chk_64 = (vc_maxhandle+1) * sizeof(uint32_t); + if((chk_64/sizeof(uint32_t)) != (vc_maxhandle+1)) + { + chk_report_abort("TALOS-2023-1798"); + } + } + chain_table_lengths = (uint32_t *)calloc((vc_maxhandle+1), sizeof(uint32_t)); + } - if (!chain_table || !chain_table_lengths) - goto block_err; + if(!chain_table || !chain_table_lengths) goto block_err; pnt = chain_cmem; idx = 0; pval = 0; - if (sectype == FST_BL_VCDATA_DYN_ALIAS2) { - uint32_t prev_alias = 0; + if(sectype == FST_BL_VCDATA_DYN_ALIAS2) + { + uint32_t prev_alias = 0; - do { - int skiplen; + do { + int skiplen; - if (*pnt & 0x01) { - int64_t shval = fstGetSVarint64(pnt, &skiplen) >> 1; - if (shval > 0) { - pval = chain_table[idx] = pval + shval; - if (idx) { - chain_table_lengths[pidx] = pval - chain_table[pidx]; - } - pidx = idx++; - } else if (shval < 0) { - chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ - chain_table_lengths[idx] = prev_alias = - shval; /* because during this loop iter would give stale data! */ - idx++; - } else { - chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ - chain_table_lengths[idx] = - prev_alias; /* because during this loop iter would give stale data! */ - idx++; - } - } else { - uint64_t val = fstGetVarint32(pnt, &skiplen); - - fstHandle loopcnt = val >> 1; - for (i = 0; i < loopcnt; i++) { - chain_table[idx++] = 0; - } - } + if(*pnt & 0x01) + { + int64_t shval = fstGetSVarint64(pnt, &skiplen) >> 1; + if(shval > 0) + { + pval = chain_table[idx] = pval + shval; + if(idx) { chain_table_lengths[pidx] = pval - chain_table[pidx]; } + pidx = idx++; + } + else if(shval < 0) + { + chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ + chain_table_lengths[idx] = prev_alias = shval; /* because during this loop iter would give stale data! */ + idx++; + } + else + { + chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ + chain_table_lengths[idx] = prev_alias; /* because during this loop iter would give stale data! */ + idx++; + } + } + else + { + uint64_t val = fstGetVarint32(pnt, &skiplen); + + fstHandle loopcnt = val >> 1; + if((idx+loopcnt-1) > vc_maxhandle) /* TALOS-2023-1789 */ + { + chk_report_abort("TALOS-2023-1789"); + } + + for(i=0;i> 1); - if (idx) { - chain_table_lengths[pidx] = pval - chain_table[pidx]; - } - pidx = idx++; - } else { - fstHandle loopcnt = val >> 1; - for (i = 0; i < loopcnt; i++) { - chain_table[idx++] = 0; - } + pnt += skiplen; + } while (pnt != (chain_cmem + chain_clen)); } + else + { + do { + int skiplen; + uint64_t val = fstGetVarint32(pnt, &skiplen); - pnt += skiplen; - } while (pnt != (chain_cmem + chain_clen)); - } + if(!val) + { + pnt += skiplen; + val = fstGetVarint32(pnt, &skiplen); + chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ + chain_table_lengths[idx] = -val; /* because during this loop iter would give stale data! */ + idx++; + } + else + if(val&1) + { + pval = chain_table[idx] = pval + (val >> 1); + if(idx) { chain_table_lengths[pidx] = pval - chain_table[pidx]; } + pidx = idx++; + } + else + { + fstHandle loopcnt = val >> 1; + + if((idx+loopcnt-1) > vc_maxhandle) /* TALOS-2023-1789 */ + { + chk_report_abort("TALOS-2023-1789"); + } + + for(i=0;i xc->maxhandle) - idx = xc->maxhandle; - for (i = 0; i < idx; i++) { - if (chain_table[i]) { - int process_idx = i / 8; - int process_bit = i & 7; - - if (xc->process_mask[process_idx] & (1 << process_bit)) { - int rc = Z_OK; - uint32_t val; - uint32_t skiplen; - uint32_t tdelta; - - fstReaderFseeko(xc, xc->f, vc_start + chain_table[i], SEEK_SET); - val = fstReaderVarint32WithSkip(xc->f, &skiplen); - if (val) { - unsigned char *mu = mem_for_traversal + traversal_mem_offs; /* uncomp: dst */ - unsigned char *mc; /* comp: src */ - unsigned long destlen = val; - unsigned long sourcelen = chain_table_lengths[i]; - - if (mc_mem_len < chain_table_lengths[i]) { - free(mc_mem); - mc_mem = (unsigned char *)malloc(mc_mem_len = chain_table_lengths[i]); - } - mc = mc_mem; - - fstFread(mc, chain_table_lengths[i], 1, xc->f); - - switch (packtype) { - case '4': - rc = (destlen == (unsigned long)LZ4_decompress_safe_partial((char *)mc, (char *)mu, - sourcelen, destlen, destlen)) - ? Z_OK - : Z_DATA_ERROR; - break; - case 'F': - fastlz_decompress(mc, sourcelen, mu, destlen); /* rc appears unreliable */ - break; - default: - rc = uncompress(mu, &destlen, mc, sourcelen); - break; - } - - /* data to process is for(j=0;jf); - /* data to process is for(j=0;jsignal_lens[i] == 1) { - uint32_t vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[i]); - uint32_t shcnt = 2 << (vli & 1); - tdelta = vli >> shcnt; - } else { - uint32_t vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[i]); - tdelta = vli >> 1; - } - - scatterptr[i] = tc_head[tdelta]; - tc_head[tdelta] = i + 1; - } - } - } - - free(mc_mem); /* there is no usage below for this, no real need to clear out mc_mem or mc_mem_len */ - - for (i = 0; i < tsec_nitems; i++) { - uint32_t tdelta; - int skiplen, skiplen2; - uint32_t vli; - - if (fv) { - char wx_buf[32]; - int wx_len; - - if (time_table[i] != previous_time) { - if (xc->limit_range_valid) { - if (time_table[i] > xc->limit_range_end) { - break; - } - } - - if (dumpvars_state == 1) { - wx_len = snprintf(wx_buf, 6, "$end\n"); - fstWritex(xc, wx_buf, wx_len); - dumpvars_state = 2; - } - wx_len = snprintf(wx_buf, 20, "#%" PRIu64 "\n", time_table[i]); - fstWritex(xc, wx_buf, wx_len); - if (!dumpvars_state) { - wx_len = snprintf(wx_buf, 11, "$dumpvars\n"); - fstWritex(xc, wx_buf, wx_len); - dumpvars_state = 1; - } - - if ((xc->num_blackouts) && (cur_blackout != xc->num_blackouts)) { - if (time_table[i] == xc->blackout_times[cur_blackout]) { - wx_len = snprintf(wx_buf, 16, "$dump%s $end\n", - (xc->blackout_activity[cur_blackout++]) ? "on" : "off"); - fstWritex(xc, wx_buf, wx_len); - } - } - previous_time = time_table[i]; - } - } else { - if (time_table[i] != previous_time) { - if (xc->limit_range_valid) { - if (time_table[i] > xc->limit_range_end) { - break; - } - } - previous_time = time_table[i]; - } - } - - while (tc_head[i]) { - idx = tc_head[i] - 1; - vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); - - if (xc->signal_lens[idx] <= 1) { - if (xc->signal_lens[idx] == 1) { - unsigned char val; - if (!(vli & 1)) { - /* tdelta = vli >> 2; */ /* scan-build */ - val = ((vli >> 1) & 1) | '0'; - } else { - /* tdelta = vli >> 4; */ /* scan-build */ - val = FST_RCV_STR[((vli >> 1) & 7)]; - } - - if (value_change_callback) { - xc->temp_signal_value_buf[0] = val; - xc->temp_signal_value_buf[1] = 0; - value_change_callback(user_callback_data_pointer, time_table[i], idx + 1, - xc->temp_signal_value_buf); - } else { - if (fv) { - char vcd_id[16]; - int vcdid_len = fstVcdIDForFwrite(vcd_id + 1, idx + 1); - - vcd_id[0] = val; - vcd_id[vcdid_len + 1] = '\n'; - fstWritex(xc, vcd_id, vcdid_len + 2); - } - } - headptr[idx] += skiplen; - length_remaining[idx] -= skiplen; + if(idx > xc->maxhandle) idx = xc->maxhandle; + for(i=0;iprocess_mask[process_idx]&(1<f, vc_start + chain_table[i], SEEK_SET); + val = fstReaderVarint32WithSkip(xc->f, &skiplen); + if(val) + { + unsigned char *mu = mem_for_traversal + traversal_mem_offs; /* uncomp: dst */ + unsigned char *mc; /* comp: src */ + unsigned long destlen = val; + unsigned long sourcelen = chain_table_lengths[i]; + + if(traversal_mem_offs >= mem_required_for_traversal) + { + chk_report_abort("TALOS-2023-1785"); + } + + if(mc_mem_len < chain_table_lengths[i]) + { + free(mc_mem); + mc_mem = (unsigned char *)malloc(mc_mem_len = chain_table_lengths[i]); + } + mc = mc_mem; + + fstFread(mc, chain_table_lengths[i], 1, xc->f); + + switch(packtype) + { + case '4': rc = (destlen == (unsigned long)LZ4_decompress_safe_partial((char *)mc, (char *)mu, sourcelen, destlen, destlen)) ? Z_OK : Z_DATA_ERROR; + break; + case 'F': fastlz_decompress(mc, sourcelen, mu, destlen); /* rc appears unreliable */ + break; + default: rc = uncompress(mu, &destlen, mc, sourcelen); + break; + } + + /* data to process is for(j=0;j= mem_required_for_traversal) + { + chk_report_abort("TALOS-2023-1785"); + } + + fstFread(mu, destlen, 1, xc->f); + /* data to process is for(j=0;j> shamt; + if(xc->signal_lens[i] == 1) + { + uint32_t vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[i]); + uint32_t shcnt = 2 << (vli & 1); + tdelta = vli >> shcnt; + } + else + { + uint32_t vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[i]); + tdelta = vli >> 1; + } - scatterptr[idx] = tc_head[i + tdelta]; - tc_head[i + tdelta] = idx + 1; - } - } else { - unsigned char *vdata; - uint32_t len; + if(tdelta >= tc_head_items) + { + chk_report_abort("TALOS-2023-1791"); + } - vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); - len = fstGetVarint32(mem_for_traversal + headptr[idx] + skiplen, &skiplen2); - /* tdelta = vli >> 1; */ /* scan-build */ - skiplen += skiplen2; - vdata = mem_for_traversal + headptr[idx] + skiplen; - - if (!(vli & 1)) { - if (value_change_callback_varlen) { - value_change_callback_varlen(user_callback_data_pointer, time_table[i], idx + 1, vdata, - len); - } else { - if (fv) { - char vcd_id[16]; - int vcdid_len; - - vcd_id[0] = 's'; - fstWritex(xc, vcd_id, 1); - - vcdid_len = fstVcdIDForFwrite(vcd_id + 1, idx + 1); - { - unsigned char *vesc = (unsigned char *)malloc(len * 4 + 1); - int vlen = fstUtilityBinToEsc(vesc, vdata, len); - fstWritex(xc, vesc, vlen); - free(vesc); - } - - vcd_id[0] = ' '; - vcd_id[vcdid_len + 1] = '\n'; - fstWritex(xc, vcd_id, vcdid_len + 2); + scatterptr[i] = tc_head[tdelta]; + tc_head[tdelta] = i+1; } - } } + } - skiplen += len; - headptr[idx] += skiplen; - length_remaining[idx] -= skiplen; + free(mc_mem); /* there is no usage below for this, no real need to clear out mc_mem or mc_mem_len */ - tc_head[i] = scatterptr[idx]; - scatterptr[idx] = 0; + for(i=0;i> 1; + if(fv) + { + char wx_buf[32]; + int wx_len; + + if(time_table[i] != previous_time) + { + if(xc->limit_range_valid) + { + if(time_table[i] > xc->limit_range_end) + { + break; + } + } - scatterptr[idx] = tc_head[i + tdelta]; - tc_head[i + tdelta] = idx + 1; - } - } - } else { - uint32_t len = xc->signal_lens[idx]; - unsigned char *vdata; - - vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); - /* tdelta = vli >> 1; */ /* scan-build */ - vdata = mem_for_traversal + headptr[idx] + skiplen; - - if (xc->signal_typs[idx] != FST_VT_VCD_REAL) { - if (!(vli & 1)) { - int byte = 0; - int bit; - unsigned int j; - - for (j = 0; j < len; j++) { - unsigned char ch; - byte = j / 8; - bit = 7 - (j & 7); - ch = ((vdata[byte] >> bit) & 1) | '0'; - xc->temp_signal_value_buf[j] = ch; - } - xc->temp_signal_value_buf[j] = 0; - - if (value_change_callback) { - value_change_callback(user_callback_data_pointer, time_table[i], idx + 1, - xc->temp_signal_value_buf); - } else { - if (fv) { - unsigned char ch_bp = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; - - fstWritex(xc, &ch_bp, 1); - fstWritex(xc, xc->temp_signal_value_buf, len); - } - } - - len = byte + 1; - } else { - if (value_change_callback) { - memcpy(xc->temp_signal_value_buf, vdata, len); - xc->temp_signal_value_buf[len] = 0; - value_change_callback(user_callback_data_pointer, time_table[i], idx + 1, - xc->temp_signal_value_buf); - } else { - if (fv) { - unsigned char ch_bp = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; - - fstWritex(xc, &ch_bp, 1); - fstWritex(xc, vdata, len); + if(dumpvars_state == 1) { wx_len = snprintf(wx_buf, 32, "$end\n"); fstWritex(xc, wx_buf, wx_len); dumpvars_state = 2; } + wx_len = snprintf(wx_buf, 32, "#%" PRIu64 "\n", time_table[i]); + fstWritex(xc, wx_buf, wx_len); + if(!dumpvars_state) { wx_len = snprintf(wx_buf, 32, "$dumpvars\n"); fstWritex(xc, wx_buf, wx_len); dumpvars_state = 1; } + + if((xc->num_blackouts)&&(cur_blackout != xc->num_blackouts)) + { + if(time_table[i] == xc->blackout_times[cur_blackout]) + { + wx_len = snprintf(wx_buf, 32, "$dump%s $end\n", (xc->blackout_activity[cur_blackout++]) ? "on" : "off"); + fstWritex(xc, wx_buf, wx_len); + } + } + previous_time = time_table[i]; } - } } - } else { - double d; - unsigned char *clone_d /*= (unsigned char *)&d */; /* scan-build */ - unsigned char buf[8]; - unsigned char *srcdata; - if (!(vli & 1)) /* very rare case, but possible */ + while(tc_head[i]) { - int bit; - int j; - - for (j = 0; j < 8; j++) { - unsigned char ch; - bit = 7 - (j & 7); - ch = ((vdata[0] >> bit) & 1) | '0'; - buf[j] = ch; - } - - len = 1; - srcdata = buf; - } else { - srcdata = vdata; - } - - if (value_change_callback) { - if (xc->native_doubles_for_cb) { - if (xc->double_endian_match) { - clone_d = srcdata; - } else { - int j; - - clone_d = (unsigned char *)&d; - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } - } - value_change_callback(user_callback_data_pointer, time_table[i], idx + 1, clone_d); - } else { - clone_d = (unsigned char *)&d; - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; - - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } - } - snprintf((char *)xc->temp_signal_value_buf, 32, "%.16g", d); - value_change_callback(user_callback_data_pointer, time_table[i], idx + 1, - xc->temp_signal_value_buf); - } - } else { - if (fv) { - char wx_buf[32]; - int wx_len; - - clone_d = (unsigned char *)&d; - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; + idx = tc_head[i] - 1; + vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } + if(xc->signal_lens[idx] <= 1) + { + if(xc->signal_lens[idx] == 1) + { + unsigned char val; + if(!(vli & 1)) + { + /* tdelta = vli >> 2; */ /* scan-build */ + val = ((vli >> 1) & 1) | '0'; + } + else + { + /* tdelta = vli >> 4; */ /* scan-build */ + val = FST_RCV_STR[((vli >> 1) & 7)]; + } + + if(value_change_callback) + { + xc->temp_signal_value_buf[0] = val; + xc->temp_signal_value_buf[1] = 0; + value_change_callback(user_callback_data_pointer, time_table[i], idx+1, xc->temp_signal_value_buf); + } + else + { + if(fv) + { + char vcd_id[16]; + int vcdid_len = fstVcdIDForFwrite(vcd_id+1, idx+1); + + vcd_id[0] = val; + vcd_id[vcdid_len+1] = '\n'; + fstWritex(xc, vcd_id, vcdid_len+2); + } + } + headptr[idx] += skiplen; + length_remaining[idx] -= skiplen; + + tc_head[i] = scatterptr[idx]; + scatterptr[idx] = 0; + + if(length_remaining[idx]) + { + int shamt; + vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[idx]); + shamt = 2 << (vli & 1); + tdelta = vli >> shamt; + + if((tdelta+i) >= tc_head_items) + { + chk_report_abort("TALOS-2023-1791"); + } + + scatterptr[idx] = tc_head[i+tdelta]; + tc_head[i+tdelta] = idx+1; + } + } + else + { + unsigned char *vdata; + uint32_t len; + + vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); + len = fstGetVarint32(mem_for_traversal + headptr[idx] + skiplen, &skiplen2); + /* tdelta = vli >> 1; */ /* scan-build */ + skiplen += skiplen2; + vdata = mem_for_traversal + headptr[idx] + skiplen; + + if(!(vli & 1)) + { + if(value_change_callback_varlen) + { + value_change_callback_varlen(user_callback_data_pointer, time_table[i], idx+1, vdata, len); + } + else + { + if(fv) + { + char vcd_id[16]; + int vcdid_len; + + vcd_id[0] = 's'; + fstWritex(xc, vcd_id, 1); + + vcdid_len = fstVcdIDForFwrite(vcd_id+1, idx+1); + { + if(sizeof(size_t) < sizeof(uint64_t)) + { + /* TALOS-2023-1790 for 32b overflow */ + uint64_t chk_64 = len*4 + 1; + size_t chk_32 = len*4 + 1; + if(chk_64 != chk_32) chk_report_abort("TALOS-2023-1790"); + } + + unsigned char *vesc = (unsigned char *)malloc(len*4 + 1); + int vlen = fstUtilityBinToEsc(vesc, vdata, len); + fstWritex(xc, vesc, vlen); + free(vesc); + } + + vcd_id[0] = ' '; + vcd_id[vcdid_len + 1] = '\n'; + fstWritex(xc, vcd_id, vcdid_len+2); + } + } + } + + skiplen += len; + headptr[idx] += skiplen; + length_remaining[idx] -= skiplen; + + tc_head[i] = scatterptr[idx]; + scatterptr[idx] = 0; + + if(length_remaining[idx]) + { + vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[idx]); + tdelta = vli >> 1; + + if((tdelta+i) >= tc_head_items) + { + chk_report_abort("TALOS-2023-1791"); + } + + scatterptr[idx] = tc_head[i+tdelta]; + tc_head[i+tdelta] = idx+1; + } + } } + else + { + uint32_t len = xc->signal_lens[idx]; + unsigned char *vdata; + + vli = fstGetVarint32(mem_for_traversal + headptr[idx], &skiplen); + /* tdelta = vli >> 1; */ /* scan-build */ + vdata = mem_for_traversal + headptr[idx] + skiplen; + + if(xc->signal_typs[idx] != FST_VT_VCD_REAL) + { + if(len > xc->longest_signal_value_len) + { + chk_report_abort("TALOS-2023-1797"); + } + + if(!(vli & 1)) + { + int byte = 0; + int bit; + unsigned int j; + + for(j=0;j> bit) & 1) | '0'; + xc->temp_signal_value_buf[j] = ch; + } + xc->temp_signal_value_buf[j] = 0; + + if(value_change_callback) + { + value_change_callback(user_callback_data_pointer, time_table[i], idx+1, xc->temp_signal_value_buf); + } + else + { + if(fv) { + unsigned char ch_bp = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; + + fstWritex(xc, &ch_bp, 1); + fstWritex(xc, xc->temp_signal_value_buf, len); + } + } + + len = byte+1; + } + else + { + if(value_change_callback) + { + memcpy(xc->temp_signal_value_buf, vdata, len); + xc->temp_signal_value_buf[len] = 0; + value_change_callback(user_callback_data_pointer, time_table[i], idx+1, xc->temp_signal_value_buf); + } + else + { + if(fv) + { + unsigned char ch_bp = (xc->signal_typs[idx] != FST_VT_VCD_PORT) ? 'b' : 'p'; + uint64_t mem_required_for_traversal_chk = vdata - mem_for_traversal + len; + + fstWritex(xc, &ch_bp, 1); + if(mem_required_for_traversal_chk > mem_required_for_traversal) + { + chk_report_abort("TALOS-2023-1793"); + } + fstWritex(xc, vdata, len); + } + } + } + } + else + { + double d; + unsigned char *clone_d /*= (unsigned char *)&d */; /* scan-build */ + unsigned char buf[8]; + unsigned char *srcdata; + + if(!(vli & 1)) /* very rare case, but possible */ + { + int bit; + int j; + + for(j=0;j<8;j++) + { + unsigned char ch; + bit = 7 - (j & 7); + ch = ((vdata[0] >> bit) & 1) | '0'; + buf[j] = ch; + } + + len = 1; + srcdata = buf; + } + else + { + srcdata = vdata; + } + + if(value_change_callback) + { + if(xc->native_doubles_for_cb) + { + if(xc->double_endian_match) + { + clone_d = srcdata; + } + else + { + int j; + + clone_d = (unsigned char *)&d; + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + value_change_callback(user_callback_data_pointer, time_table[i], idx+1, clone_d); + } + else + { + clone_d = (unsigned char *)&d; + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; + + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + snprintf((char *)xc->temp_signal_value_buf, xc->longest_signal_value_len + 1, "%.16g", d); + value_change_callback(user_callback_data_pointer, time_table[i], idx+1, xc->temp_signal_value_buf); + } + } + else + { + if(fv) + { + char wx_buf[32]; + int wx_len; + + clone_d = (unsigned char *)&d; + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; + + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } + + wx_len = snprintf(wx_buf, 32, "r%.16g", d); + fstWritex(xc, wx_buf, wx_len); + } + } + } - wx_len = snprintf(wx_buf, sizeof(wx_buf), "r%.16g", d); - fstWritex(xc, wx_buf, wx_len); - } - } - } + if(fv) + { + char vcd_id[16]; + int vcdid_len = fstVcdIDForFwrite(vcd_id+1, idx+1); + vcd_id[0] = ' '; + vcd_id[vcdid_len+1] = '\n'; + fstWritex(xc, vcd_id, vcdid_len+2); + } - if (fv) { - char vcd_id[16]; - int vcdid_len = fstVcdIDForFwrite(vcd_id + 1, idx + 1); - vcd_id[0] = ' '; - vcd_id[vcdid_len + 1] = '\n'; - fstWritex(xc, vcd_id, vcdid_len + 2); - } + skiplen += len; + headptr[idx] += skiplen; + length_remaining[idx] -= skiplen; - skiplen += len; - headptr[idx] += skiplen; - length_remaining[idx] -= skiplen; + tc_head[i] = scatterptr[idx]; + scatterptr[idx] = 0; - tc_head[i] = scatterptr[idx]; - scatterptr[idx] = 0; + if(length_remaining[idx]) + { + vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[idx]); + tdelta = vli >> 1; - if (length_remaining[idx]) { - vli = fstGetVarint32NoSkip(mem_for_traversal + headptr[idx]); - tdelta = vli >> 1; + if((tdelta+i) >= tc_head_items) + { + chk_report_abort("TALOS-2023-1791"); + } - scatterptr[idx] = tc_head[i + tdelta]; - tc_head[i + tdelta] = idx + 1; - } + scatterptr[idx] = tc_head[i+tdelta]; + tc_head[i+tdelta] = idx+1; + } + } + } } - } - } - block_err: +block_err: free(tc_head); free(chain_cmem); - free(mem_for_traversal); - mem_for_traversal = NULL; + free(mem_for_traversal); mem_for_traversal = NULL; secnum++; - if (secnum == xc->vc_section_count) - break; /* in case file is growing, keep with original block count */ + if(secnum == xc->vc_section_count) break; /* in case file is growing, keep with original block count */ blkpos += seclen; - } + } - if (mem_for_traversal) - free(mem_for_traversal); /* scan-build */ - free(length_remaining); - free(headptr); - free(scatterptr); +if(mem_for_traversal) free(mem_for_traversal); /* scan-build */ +free(length_remaining); +free(headptr); +free(scatterptr); - if (chain_table) - free(chain_table); - if (chain_table_lengths) - free(chain_table_lengths); +if(chain_table) free(chain_table); +if(chain_table_lengths) free(chain_table_lengths); - free(time_table); +free(time_table); #ifndef FST_WRITEX_DISABLE - if (fv) { +if(fv) + { fstWritex(xc, NULL, 0); - } + } #endif - return (1); +return(1); } + /* rvat functions */ static char *fstExtractRvatDataFromFrame(struct fstReaderContext *xc, fstHandle facidx, char *buf) { - if (facidx >= xc->rvat_frame_maxhandle) { - return (NULL); - } +if(facidx >= xc->rvat_frame_maxhandle) + { + return(NULL); + } - if (xc->signal_lens[facidx] == 1) { +if(xc->signal_lens[facidx] == 1) + { buf[0] = (char)xc->rvat_frame_data[xc->rvat_sig_offs[facidx]]; buf[1] = 0; - } else { - if (xc->signal_typs[facidx] != FST_VT_VCD_REAL) { - memcpy(buf, xc->rvat_frame_data + xc->rvat_sig_offs[facidx], xc->signal_lens[facidx]); - buf[xc->signal_lens[facidx]] = 0; - } else { - double d; - unsigned char *clone_d = (unsigned char *)&d; - unsigned char *srcdata = xc->rvat_frame_data + xc->rvat_sig_offs[facidx]; + } + else + { + if(xc->signal_typs[facidx] != FST_VT_VCD_REAL) + { + memcpy(buf, xc->rvat_frame_data + xc->rvat_sig_offs[facidx], xc->signal_lens[facidx]); + buf[xc->signal_lens[facidx]] = 0; + } + else + { + double d; + unsigned char *clone_d = (unsigned char *)&d; + unsigned char *srcdata = xc->rvat_frame_data + xc->rvat_sig_offs[facidx]; + + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; + snprintf((char *)buf, 32, "%.16g", d); /* this will write 18 bytes */ } - } - - snprintf((char *)buf, 32, "%.16g", d); } - } - return (buf); +return(buf); } + char *fstReaderGetValueFromHandleAtTime(void *ctx, uint64_t tim, fstHandle facidx, char *buf) { - struct fstReaderContext *xc = (struct fstReaderContext *)ctx; - fst_off_t blkpos = 0, prev_blkpos; - uint64_t beg_tim, end_tim, beg_tim2, end_tim2; - int sectype; +struct fstReaderContext *xc = (struct fstReaderContext *)ctx; +fst_off_t blkpos = 0, prev_blkpos; +uint64_t beg_tim, end_tim, beg_tim2, end_tim2; +int sectype; #ifdef FST_DEBUG - unsigned int secnum = 0; +unsigned int secnum = 0; #endif - uint64_t seclen; - uint64_t tsec_uclen = 0, tsec_clen = 0; - uint64_t tsec_nitems; - uint64_t frame_uclen, frame_clen; +uint64_t seclen; +uint64_t tsec_uclen = 0, tsec_clen = 0; +uint64_t tsec_nitems; +uint64_t frame_uclen, frame_clen; #ifdef FST_DEBUG - uint64_t mem_required_for_traversal; +uint64_t mem_required_for_traversal; #endif - fst_off_t indx_pntr, indx_pos; - long chain_clen; - unsigned char *chain_cmem; - unsigned char *pnt; - fstHandle idx, pidx = 0, i; - uint64_t pval; - - if ((!xc) || (!facidx) || (facidx > xc->maxhandle) || (!buf) || (!xc->signal_lens[facidx - 1])) { - return (NULL); - } - - if (!xc->rvat_sig_offs) { +fst_off_t indx_pntr, indx_pos; +long chain_clen; +unsigned char *chain_cmem; +unsigned char *pnt; +fstHandle idx, pidx=0, i; +uint64_t pval; + +if((!xc) || (!facidx) || (facidx > xc->maxhandle) || (!buf) || (!xc->signal_lens[facidx-1])) + { + return(NULL); + } + +if(!xc->rvat_sig_offs) + { uint32_t cur_offs = 0; xc->rvat_sig_offs = (uint32_t *)calloc(xc->maxhandle, sizeof(uint32_t)); - for (i = 0; i < xc->maxhandle; i++) { - xc->rvat_sig_offs[i] = cur_offs; - cur_offs += xc->signal_lens[i]; + for(i=0;imaxhandle;i++) + { + xc->rvat_sig_offs[i] = cur_offs; + cur_offs += xc->signal_lens[i]; + } } - } - if (xc->rvat_data_valid) { - if ((xc->rvat_beg_tim <= tim) && (tim <= xc->rvat_end_tim)) { - goto process_value; - } +if(xc->rvat_data_valid) + { + if((xc->rvat_beg_tim <= tim) && (tim <= xc->rvat_end_tim)) + { + goto process_value; + } fstReaderDeallocateRvatData(xc); - } + } - xc->rvat_chain_pos_valid = 0; +xc->rvat_chain_pos_valid = 0; - for (;;) { +for(;;) + { fstReaderFseeko(xc, xc->f, (prev_blkpos = blkpos), SEEK_SET); sectype = fgetc(xc->f); seclen = fstReaderUint64(xc->f); - if ((sectype == EOF) || (sectype == FST_BL_SKIP) || (!seclen)) { - return (NULL); /* if this loop exits on break, it's successful */ - } + if((sectype == EOF) || (sectype == FST_BL_SKIP) || (!seclen)) + { + return(NULL); /* if this loop exits on break, it's successful */ + } blkpos++; - if ((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) && - (sectype != FST_BL_VCDATA_DYN_ALIAS2)) { - blkpos += seclen; - continue; - } + if((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) && (sectype != FST_BL_VCDATA_DYN_ALIAS2)) + { + blkpos += seclen; + continue; + } beg_tim = fstReaderUint64(xc->f); end_tim = fstReaderUint64(xc->f); - if ((beg_tim <= tim) && (tim <= end_tim)) { - if ((tim == end_tim) && (tim != xc->end_time)) { - fst_off_t cached_pos = ftello(xc->f); - fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET); + if((beg_tim <= tim) && (tim <= end_tim)) + { + if((tim == end_tim) && (tim != xc->end_time)) + { + fst_off_t cached_pos = ftello(xc->f); + fstReaderFseeko(xc, xc->f, blkpos, SEEK_SET); - sectype = fgetc(xc->f); - seclen = fstReaderUint64(xc->f); + sectype = fgetc(xc->f); + seclen = fstReaderUint64(xc->f); - beg_tim2 = fstReaderUint64(xc->f); - end_tim2 = fstReaderUint64(xc->f); + beg_tim2 = fstReaderUint64(xc->f); + end_tim2 = fstReaderUint64(xc->f); - if (((sectype != FST_BL_VCDATA) && (sectype != FST_BL_VCDATA_DYN_ALIAS) && - (sectype != FST_BL_VCDATA_DYN_ALIAS2)) || - (!seclen) || (beg_tim2 != tim)) { - blkpos = prev_blkpos; - break; + if(((sectype != FST_BL_VCDATA)&&(sectype != FST_BL_VCDATA_DYN_ALIAS)&&(sectype != FST_BL_VCDATA_DYN_ALIAS2)) || (!seclen) || (beg_tim2 != tim)) + { + blkpos = prev_blkpos; + break; + } + beg_tim = beg_tim2; + end_tim = end_tim2; + fstReaderFseeko(xc, xc->f, cached_pos, SEEK_SET); + } + break; } - beg_tim = beg_tim2; - end_tim = end_tim2; - fstReaderFseeko(xc, xc->f, cached_pos, SEEK_SET); - } - break; - } blkpos += seclen; #ifdef FST_DEBUG secnum++; #endif - } + } - xc->rvat_beg_tim = beg_tim; - xc->rvat_end_tim = end_tim; +xc->rvat_beg_tim = beg_tim; +xc->rvat_end_tim = end_tim; #ifdef FST_DEBUG - mem_required_for_traversal = +mem_required_for_traversal = #endif - fstReaderUint64(xc->f); + fstReaderUint64(xc->f); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "rvat sec: %u seclen: %d begtim: %d endtim: %d\n", secnum, (int)seclen, (int)beg_tim, - (int)end_tim); - fprintf(stderr, FST_APIMESS "mem_required_for_traversal: %d\n", (int)mem_required_for_traversal); +fprintf(stderr, FST_APIMESS "rvat sec: %u seclen: %d begtim: %d endtim: %d\n", + secnum, (int)seclen, (int)beg_tim, (int)end_tim); +fprintf(stderr, FST_APIMESS "mem_required_for_traversal: %d\n", (int)mem_required_for_traversal); #endif - /* process time block */ - { - unsigned char *ucdata; - unsigned char *cdata; - unsigned long destlen /* = tsec_uclen */; /* scan-build */ - unsigned long sourcelen /* = tsec_clen */; /* scan-build */ - int rc; - unsigned char *tpnt; - uint64_t tpval; - unsigned int ti; - - fstReaderFseeko(xc, xc->f, blkpos + seclen - 24, SEEK_SET); - tsec_uclen = fstReaderUint64(xc->f); - tsec_clen = fstReaderUint64(xc->f); - tsec_nitems = fstReaderUint64(xc->f); +/* process time block */ +{ +unsigned char *ucdata; +unsigned char *cdata; +unsigned long destlen /* = tsec_uclen */; /* scan-build */ +unsigned long sourcelen /* = tsec_clen */; /* scan-build */ +int rc; +unsigned char *tpnt; +uint64_t tpval; +unsigned int ti; + +fstReaderFseeko(xc, xc->f, blkpos + seclen - 24, SEEK_SET); +tsec_uclen = fstReaderUint64(xc->f); +tsec_clen = fstReaderUint64(xc->f); +tsec_nitems = fstReaderUint64(xc->f); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "time section unc: %d, com: %d (%d items)\n", (int)tsec_uclen, (int)tsec_clen, - (int)tsec_nitems); +fprintf(stderr, FST_APIMESS "time section unc: %d, com: %d (%d items)\n", + (int)tsec_uclen, (int)tsec_clen, (int)tsec_nitems); #endif - ucdata = (unsigned char *)malloc(tsec_uclen); - destlen = tsec_uclen; - sourcelen = tsec_clen; +ucdata = (unsigned char *)malloc(tsec_uclen); +destlen = tsec_uclen; +sourcelen = tsec_clen; - fstReaderFseeko(xc, xc->f, -24 - ((fst_off_t)tsec_clen), SEEK_CUR); - if (tsec_uclen != tsec_clen) { - cdata = (unsigned char *)malloc(tsec_clen); - fstFread(cdata, tsec_clen, 1, xc->f); +fstReaderFseeko(xc, xc->f, -24 - ((fst_off_t)tsec_clen), SEEK_CUR); +if(tsec_uclen != tsec_clen) + { + cdata = (unsigned char *)malloc(tsec_clen); + fstFread(cdata, tsec_clen, 1, xc->f); - rc = uncompress(ucdata, &destlen, cdata, sourcelen); + rc = uncompress(ucdata, &destlen, cdata, sourcelen); - if (rc != Z_OK) { - fprintf(stderr, FST_APIMESS "fstReaderGetValueFromHandleAtTime(), tsec uncompress rc = %d, exiting.\n", - rc); + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderGetValueFromHandleAtTime(), tsec uncompress rc = %d, exiting.\n", rc); exit(255); - } + } - free(cdata); - } else { - fstFread(ucdata, tsec_uclen, 1, xc->f); + free(cdata); + } + else + { + fstFread(ucdata, tsec_uclen, 1, xc->f); } - xc->rvat_time_table = (uint64_t *)calloc(tsec_nitems, sizeof(uint64_t)); - tpnt = ucdata; - tpval = 0; - for (ti = 0; ti < tsec_nitems; ti++) { - int skiplen; - uint64_t val = fstGetVarint64(tpnt, &skiplen); - tpval = xc->rvat_time_table[ti] = tpval + val; - tpnt += skiplen; +xc->rvat_time_table = (uint64_t *)calloc(tsec_nitems, sizeof(uint64_t)); +tpnt = ucdata; +tpval = 0; +for(ti=0;tirvat_time_table[ti] = tpval + val; + tpnt += skiplen; } - free(ucdata); - } +free(ucdata); +} - fstReaderFseeko(xc, xc->f, blkpos + 32, SEEK_SET); +fstReaderFseeko(xc, xc->f, blkpos+32, SEEK_SET); - frame_uclen = fstReaderVarint64(xc->f); - frame_clen = fstReaderVarint64(xc->f); - xc->rvat_frame_maxhandle = fstReaderVarint64(xc->f); - xc->rvat_frame_data = (unsigned char *)malloc(frame_uclen); +frame_uclen = fstReaderVarint64(xc->f); +frame_clen = fstReaderVarint64(xc->f); +xc->rvat_frame_maxhandle = fstReaderVarint64(xc->f); +xc->rvat_frame_data = (unsigned char *)malloc(frame_uclen); - if (frame_uclen == frame_clen) { +if(frame_uclen == frame_clen) + { fstFread(xc->rvat_frame_data, frame_uclen, 1, xc->f); - } else { + } + else + { unsigned char *mc = (unsigned char *)malloc(frame_clen); int rc; @@ -5716,277 +6386,321 @@ char *fstReaderGetValueFromHandleAtTime(void *ctx, uint64_t tim, fstHandle facid fstFread(mc, sourcelen, 1, xc->f); rc = uncompress(xc->rvat_frame_data, &destlen, mc, sourcelen); - if (rc != Z_OK) { - fprintf(stderr, FST_APIMESS "fstReaderGetValueFromHandleAtTime(), frame decompress rc: %d, exiting.\n", rc); - exit(255); - } + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderGetValueFromHandleAtTime(), frame decompress rc: %d, exiting.\n", rc); + exit(255); + } free(mc); - } + } - xc->rvat_vc_maxhandle = fstReaderVarint64(xc->f); - xc->rvat_vc_start = ftello(xc->f); /* points to '!' character */ - xc->rvat_packtype = fgetc(xc->f); +xc->rvat_vc_maxhandle = fstReaderVarint64(xc->f); +xc->rvat_vc_start = ftello(xc->f); /* points to '!' character */ +xc->rvat_packtype = fgetc(xc->f); #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "frame_uclen: %d, frame_clen: %d, frame_maxhandle: %d\n", (int)frame_uclen, - (int)frame_clen, (int)xc->rvat_frame_maxhandle); - fprintf(stderr, FST_APIMESS "vc_maxhandle: %d\n", (int)xc->rvat_vc_maxhandle); +fprintf(stderr, FST_APIMESS "frame_uclen: %d, frame_clen: %d, frame_maxhandle: %d\n", + (int)frame_uclen, (int)frame_clen, (int)xc->rvat_frame_maxhandle); +fprintf(stderr, FST_APIMESS "vc_maxhandle: %d\n", (int)xc->rvat_vc_maxhandle); #endif - indx_pntr = blkpos + seclen - 24 - tsec_clen - 8; - fstReaderFseeko(xc, xc->f, indx_pntr, SEEK_SET); - chain_clen = fstReaderUint64(xc->f); - indx_pos = indx_pntr - chain_clen; +indx_pntr = blkpos + seclen - 24 -tsec_clen -8; +fstReaderFseeko(xc, xc->f, indx_pntr, SEEK_SET); +chain_clen = fstReaderUint64(xc->f); +indx_pos = indx_pntr - chain_clen; #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "indx_pos: %d (%d bytes)\n", (int)indx_pos, (int)chain_clen); +fprintf(stderr, FST_APIMESS "indx_pos: %d (%d bytes)\n", (int)indx_pos, (int)chain_clen); #endif - chain_cmem = (unsigned char *)malloc(chain_clen); - fstReaderFseeko(xc, xc->f, indx_pos, SEEK_SET); - fstFread(chain_cmem, chain_clen, 1, xc->f); +chain_cmem = (unsigned char *)malloc(chain_clen); +fstReaderFseeko(xc, xc->f, indx_pos, SEEK_SET); +fstFread(chain_cmem, chain_clen, 1, xc->f); - xc->rvat_chain_table = (fst_off_t *)calloc((xc->rvat_vc_maxhandle + 1), sizeof(fst_off_t)); - xc->rvat_chain_table_lengths = (uint32_t *)calloc((xc->rvat_vc_maxhandle + 1), sizeof(uint32_t)); +xc->rvat_chain_table = (fst_off_t *)calloc((xc->rvat_vc_maxhandle+1), sizeof(fst_off_t)); +xc->rvat_chain_table_lengths = (uint32_t *)calloc((xc->rvat_vc_maxhandle+1), sizeof(uint32_t)); - pnt = chain_cmem; - idx = 0; - pval = 0; +pnt = chain_cmem; +idx = 0; +pval = 0; - if (sectype == FST_BL_VCDATA_DYN_ALIAS2) { +if(sectype == FST_BL_VCDATA_DYN_ALIAS2) + { uint32_t prev_alias = 0; - do { - int skiplen; - - if (*pnt & 0x01) { - int64_t shval = fstGetSVarint64(pnt, &skiplen) >> 1; - if (shval > 0) { - pval = xc->rvat_chain_table[idx] = pval + shval; - if (idx) { - xc->rvat_chain_table_lengths[pidx] = pval - xc->rvat_chain_table[pidx]; - } - pidx = idx++; - } else if (shval < 0) { - xc->rvat_chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ - xc->rvat_chain_table_lengths[idx] = prev_alias = - shval; /* because during this loop iter would give stale data! */ - idx++; - } else { - xc->rvat_chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ - xc->rvat_chain_table_lengths[idx] = - prev_alias; /* because during this loop iter would give stale data! */ - idx++; - } - } else { - uint64_t val = fstGetVarint32(pnt, &skiplen); - - fstHandle loopcnt = val >> 1; - for (i = 0; i < loopcnt; i++) { - xc->rvat_chain_table[idx++] = 0; - } - } - - pnt += skiplen; - } while (pnt != (chain_cmem + chain_clen)); - } else { - do { - int skiplen; - uint64_t val = fstGetVarint32(pnt, &skiplen); - - if (!val) { + do { + int skiplen; + + if(*pnt & 0x01) + { + int64_t shval = fstGetSVarint64(pnt, &skiplen) >> 1; + if(shval > 0) + { + pval = xc->rvat_chain_table[idx] = pval + shval; + if(idx) { xc->rvat_chain_table_lengths[pidx] = pval - xc->rvat_chain_table[pidx]; } + pidx = idx++; + } + else if(shval < 0) + { + xc->rvat_chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ + xc->rvat_chain_table_lengths[idx] = prev_alias = shval; /* because during this loop iter would give stale data! */ + idx++; + } + else + { + xc->rvat_chain_table[idx] = 0; /* need to explicitly zero as calloc above might not run */ + xc->rvat_chain_table_lengths[idx] = prev_alias; /* because during this loop iter would give stale data! */ + idx++; + } + } + else + { + uint64_t val = fstGetVarint32(pnt, &skiplen); + + fstHandle loopcnt = val >> 1; + for(i=0;irvat_chain_table[idx++] = 0; + } + } + pnt += skiplen; - val = fstGetVarint32(pnt, &skiplen); - xc->rvat_chain_table[idx] = 0; - xc->rvat_chain_table_lengths[idx] = -val; - idx++; - } else if (val & 1) { - pval = xc->rvat_chain_table[idx] = pval + (val >> 1); - if (idx) { - xc->rvat_chain_table_lengths[pidx] = pval - xc->rvat_chain_table[pidx]; - } - pidx = idx++; - } else { - fstHandle loopcnt = val >> 1; - for (i = 0; i < loopcnt; i++) { - xc->rvat_chain_table[idx++] = 0; - } - } - - pnt += skiplen; - } while (pnt != (chain_cmem + chain_clen)); - } - - free(chain_cmem); - xc->rvat_chain_table[idx] = indx_pos - xc->rvat_vc_start; - xc->rvat_chain_table_lengths[pidx] = xc->rvat_chain_table[idx] - xc->rvat_chain_table[pidx]; - - for (i = 0; i < idx; i++) { + } while (pnt != (chain_cmem + chain_clen)); + } + else + { + do + { + int skiplen; + uint64_t val = fstGetVarint32(pnt, &skiplen); + + if(!val) + { + pnt += skiplen; + val = fstGetVarint32(pnt, &skiplen); + xc->rvat_chain_table[idx] = 0; + xc->rvat_chain_table_lengths[idx] = -val; + idx++; + } + else + if(val&1) + { + pval = xc->rvat_chain_table[idx] = pval + (val >> 1); + if(idx) { xc->rvat_chain_table_lengths[pidx] = pval - xc->rvat_chain_table[pidx]; } + pidx = idx++; + } + else + { + fstHandle loopcnt = val >> 1; + for(i=0;irvat_chain_table[idx++] = 0; + } + } + + pnt += skiplen; + } while (pnt != (chain_cmem + chain_clen)); + } + +free(chain_cmem); +xc->rvat_chain_table[idx] = indx_pos - xc->rvat_vc_start; +xc->rvat_chain_table_lengths[pidx] = xc->rvat_chain_table[idx] - xc->rvat_chain_table[pidx]; + +for(i=0;irvat_chain_table_lengths[i]; - if ((v32 < 0) && (!xc->rvat_chain_table[i])) { - v32 = -v32; - v32--; - if (((uint32_t)v32) < i) /* sanity check */ - { - xc->rvat_chain_table[i] = xc->rvat_chain_table[v32]; - xc->rvat_chain_table_lengths[i] = xc->rvat_chain_table_lengths[v32]; - } + if((v32 < 0) && (!xc->rvat_chain_table[i])) + { + v32 = -v32; + v32--; + if(((uint32_t)v32) < i) /* sanity check */ + { + xc->rvat_chain_table[i] = xc->rvat_chain_table[v32]; + xc->rvat_chain_table_lengths[i] = xc->rvat_chain_table_lengths[v32]; + } + } } - } #ifdef FST_DEBUG - fprintf(stderr, FST_APIMESS "decompressed chain idx len: %" PRIu32 "\n", idx); +fprintf(stderr, FST_APIMESS "decompressed chain idx len: %" PRIu32 "\n", idx); #endif - xc->rvat_data_valid = 1; +xc->rvat_data_valid = 1; /* all data at this point is loaded or resident in fst cache, process and return appropriate value */ process_value: - if (facidx > xc->rvat_vc_maxhandle) { - return (NULL); - } +if(facidx > xc->rvat_vc_maxhandle) + { + return(NULL); + } + +facidx--; /* scale down for array which starts at zero */ - facidx--; /* scale down for array which starts at zero */ - if (((tim == xc->rvat_beg_tim) && (!xc->rvat_chain_table[facidx])) || (!xc->rvat_chain_table[facidx])) { - return (fstExtractRvatDataFromFrame(xc, facidx, buf)); - } +if(((tim == xc->rvat_beg_tim)&&(!xc->rvat_chain_table[facidx])) || (!xc->rvat_chain_table[facidx])) + { + return(fstExtractRvatDataFromFrame(xc, facidx, buf)); + } - if (facidx != xc->rvat_chain_facidx) { - if (xc->rvat_chain_mem) { - free(xc->rvat_chain_mem); - xc->rvat_chain_mem = NULL; +if(facidx != xc->rvat_chain_facidx) + { + if(xc->rvat_chain_mem) + { + free(xc->rvat_chain_mem); + xc->rvat_chain_mem = NULL; - xc->rvat_chain_pos_valid = 0; + xc->rvat_chain_pos_valid = 0; + } } - } - if (!xc->rvat_chain_mem) { +if(!xc->rvat_chain_mem) + { uint32_t skiplen; fstReaderFseeko(xc, xc->f, xc->rvat_vc_start + xc->rvat_chain_table[facidx], SEEK_SET); xc->rvat_chain_len = fstReaderVarint32WithSkip(xc->f, &skiplen); - if (xc->rvat_chain_len) { - unsigned char *mu = (unsigned char *)malloc(xc->rvat_chain_len); - unsigned char *mc = (unsigned char *)malloc(xc->rvat_chain_table_lengths[facidx]); - unsigned long destlen = xc->rvat_chain_len; - unsigned long sourcelen = xc->rvat_chain_table_lengths[facidx]; - int rc = Z_OK; - - fstFread(mc, xc->rvat_chain_table_lengths[facidx], 1, xc->f); - - switch (xc->rvat_packtype) { - case '4': - rc = (destlen == - (unsigned long)LZ4_decompress_safe_partial((char *)mc, (char *)mu, sourcelen, destlen, destlen)) - ? Z_OK - : Z_DATA_ERROR; - break; - case 'F': - fastlz_decompress(mc, sourcelen, mu, destlen); /* rc appears unreliable */ - break; - default: - rc = uncompress(mu, &destlen, mc, sourcelen); - break; - } + if(xc->rvat_chain_len) + { + unsigned char *mu = (unsigned char *)malloc(xc->rvat_chain_len); + unsigned char *mc = (unsigned char *)malloc(xc->rvat_chain_table_lengths[facidx]); + unsigned long destlen = xc->rvat_chain_len; + unsigned long sourcelen = xc->rvat_chain_table_lengths[facidx]; + int rc = Z_OK; + + fstFread(mc, xc->rvat_chain_table_lengths[facidx], 1, xc->f); + + switch(xc->rvat_packtype) + { + case '4': rc = (destlen == (unsigned long)LZ4_decompress_safe_partial((char *)mc, (char *)mu, sourcelen, destlen, destlen)) ? Z_OK : Z_DATA_ERROR; + break; + case 'F': fastlz_decompress(mc, sourcelen, mu, destlen); /* rc appears unreliable */ + break; + default: rc = uncompress(mu, &destlen, mc, sourcelen); + break; + } - free(mc); + free(mc); - if (rc != Z_OK) { - fprintf(stderr, - FST_APIMESS "fstReaderGetValueFromHandleAtTime(), rvat decompress clen: %d (rc=%d), exiting.\n", - (int)xc->rvat_chain_len, rc); - exit(255); - } + if(rc != Z_OK) + { + fprintf(stderr, FST_APIMESS "fstReaderGetValueFromHandleAtTime(), rvat decompress clen: %d (rc=%d), exiting.\n", (int)xc->rvat_chain_len, rc); + exit(255); + } - /* data to process is for(j=0;jrvat_chain_mem = mu; - } else { - int destlen = xc->rvat_chain_table_lengths[facidx] - skiplen; - unsigned char *mu = (unsigned char *)malloc(xc->rvat_chain_len = destlen); - fstFread(mu, destlen, 1, xc->f); - /* data to process is for(j=0;jrvat_chain_mem = mu; - } + /* data to process is for(j=0;jrvat_chain_mem = mu; + } + else + { + int destlen = xc->rvat_chain_table_lengths[facidx] - skiplen; + unsigned char *mu = (unsigned char *)malloc(xc->rvat_chain_len = destlen); + fstFread(mu, destlen, 1, xc->f); + /* data to process is for(j=0;jrvat_chain_mem = mu; + } xc->rvat_chain_facidx = facidx; - } + } - /* process value chain here */ +/* process value chain here */ - { - uint32_t tidx = 0, ptidx = 0; - uint32_t tdelta; - int skiplen; - unsigned int iprev = xc->rvat_chain_len; - uint32_t pvli = 0; - int pskip = 0; +{ +uint32_t tidx = 0, ptidx = 0; +uint32_t tdelta; +int skiplen; +unsigned int iprev = xc->rvat_chain_len; +uint32_t pvli = 0; +int pskip = 0; - if ((xc->rvat_chain_pos_valid) && (tim >= xc->rvat_chain_pos_time)) { - i = xc->rvat_chain_pos_idx; - tidx = xc->rvat_chain_pos_tidx; - } else { - i = 0; - tidx = 0; - xc->rvat_chain_pos_time = xc->rvat_beg_tim; +if((xc->rvat_chain_pos_valid)&&(tim >= xc->rvat_chain_pos_time)) + { + i = xc->rvat_chain_pos_idx; + tidx = xc->rvat_chain_pos_tidx; + } + else + { + i = 0; + tidx = 0; + xc->rvat_chain_pos_time = xc->rvat_beg_tim; } - if (xc->signal_lens[facidx] == 1) { - while (i < xc->rvat_chain_len) { +if(xc->signal_lens[facidx] == 1) + { + while(irvat_chain_len) + { uint32_t vli = fstGetVarint32(xc->rvat_chain_mem + i, &skiplen); uint32_t shcnt = 2 << (vli & 1); tdelta = vli >> shcnt; - if (xc->rvat_time_table[tidx + tdelta] <= tim) { - iprev = i; - pvli = vli; - ptidx = tidx; - /* pskip = skiplen; */ /* scan-build */ + if(xc->rvat_time_table[tidx + tdelta] <= tim) + { + iprev = i; + pvli = vli; + ptidx = tidx; + /* pskip = skiplen; */ /* scan-build */ - tidx += tdelta; - i += skiplen; - } else { - break; + tidx += tdelta; + i+=skiplen; + } + else + { + break; + } } - } - if (iprev != xc->rvat_chain_len) { + if(iprev != xc->rvat_chain_len) + { xc->rvat_chain_pos_tidx = ptidx; xc->rvat_chain_pos_idx = iprev; xc->rvat_chain_pos_time = tim; xc->rvat_chain_pos_valid = 1; - if (!(pvli & 1)) { - buf[0] = ((pvli >> 1) & 1) | '0'; - } else { - buf[0] = FST_RCV_STR[((pvli >> 1) & 7)]; - } + if(!(pvli & 1)) + { + buf[0] = ((pvli >> 1) & 1) | '0'; + } + else + { + buf[0] = FST_RCV_STR[((pvli >> 1) & 7)]; + } buf[1] = 0; - return (buf); - } else { - return (fstExtractRvatDataFromFrame(xc, facidx, buf)); - } - } else { - while (i < xc->rvat_chain_len) { + return(buf); + } + else + { + return(fstExtractRvatDataFromFrame(xc, facidx, buf)); + } + } + else + { + while(irvat_chain_len) + { uint32_t vli = fstGetVarint32(xc->rvat_chain_mem + i, &skiplen); tdelta = vli >> 1; - if (xc->rvat_time_table[tidx + tdelta] <= tim) { - iprev = i; - pvli = vli; - ptidx = tidx; - pskip = skiplen; + if(xc->rvat_time_table[tidx + tdelta] <= tim) + { + iprev = i; + pvli = vli; + ptidx = tidx; + pskip = skiplen; - tidx += tdelta; - i += skiplen; + tidx += tdelta; + i+=skiplen; - if (!(pvli & 1)) { - i += ((xc->signal_lens[facidx] + 7) / 8); - } else { - i += xc->signal_lens[facidx]; - } - } else { - break; + if(!(pvli & 1)) + { + i+=((xc->signal_lens[facidx]+7)/8); + } + else + { + i+=xc->signal_lens[facidx]; + } + } + else + { + break; + } } - } - if (iprev != xc->rvat_chain_len) { + if(iprev != xc->rvat_chain_len) + { unsigned char *vdata = xc->rvat_chain_mem + iprev + pskip; xc->rvat_chain_pos_tidx = ptidx; @@ -5994,72 +6708,90 @@ char *fstReaderGetValueFromHandleAtTime(void *ctx, uint64_t tim, fstHandle facid xc->rvat_chain_pos_time = tim; xc->rvat_chain_pos_valid = 1; - if (xc->signal_typs[facidx] != FST_VT_VCD_REAL) { - if (!(pvli & 1)) { - int byte = 0; - int bit; - unsigned int j; - - for (j = 0; j < xc->signal_lens[facidx]; j++) { - unsigned char ch; - byte = j / 8; - bit = 7 - (j & 7); - ch = ((vdata[byte] >> bit) & 1) | '0'; - buf[j] = ch; - } - buf[j] = 0; - - return (buf); - } else { - memcpy(buf, vdata, xc->signal_lens[facidx]); - buf[xc->signal_lens[facidx]] = 0; - return (buf); - } - } else { - double d; - unsigned char *clone_d = (unsigned char *)&d; - unsigned char bufd[8]; - unsigned char *srcdata; - - if (!(pvli & 1)) /* very rare case, but possible */ - { - int bit; - int j; + if(xc->signal_typs[facidx] != FST_VT_VCD_REAL) + { + if(!(pvli & 1)) + { + int byte = 0; + int bit; + unsigned int j; + + for(j=0;jsignal_lens[facidx];j++) + { + unsigned char ch; + byte = j/8; + bit = 7 - (j & 7); + ch = ((vdata[byte] >> bit) & 1) | '0'; + buf[j] = ch; + } + buf[j] = 0; - for (j = 0; j < 8; j++) { - unsigned char ch; - bit = 7 - (j & 7); - ch = ((vdata[0] >> bit) & 1) | '0'; - bufd[j] = ch; + return(buf); + } + else + { + memcpy(buf, vdata, xc->signal_lens[facidx]); + buf[xc->signal_lens[facidx]] = 0; + return(buf); + } } + else + { + double d; + unsigned char *clone_d = (unsigned char *)&d; + unsigned char bufd[8]; + unsigned char *srcdata; - srcdata = bufd; - } else { - srcdata = vdata; - } + if(!(pvli & 1)) /* very rare case, but possible */ + { + int bit; + int j; + + for(j=0;j<8;j++) + { + unsigned char ch; + bit = 7 - (j & 7); + ch = ((vdata[0] >> bit) & 1) | '0'; + bufd[j] = ch; + } - if (xc->double_endian_match) { - memcpy(clone_d, srcdata, 8); - } else { - int j; + srcdata = bufd; + } + else + { + srcdata = vdata; + } - for (j = 0; j < 8; j++) { - clone_d[j] = srcdata[7 - j]; - } - } + if(xc->double_endian_match) + { + memcpy(clone_d, srcdata, 8); + } + else + { + int j; + + for(j=0;j<8;j++) + { + clone_d[j] = srcdata[7-j]; + } + } - snprintf(buf, 32, "r%.16g", d); - return (buf); + snprintf(buf, 32, "r%.16g", d); /* this will write 19 bytes */ + return(buf); + } + } + else + { + return(fstExtractRvatDataFromFrame(xc, facidx, buf)); } - } else { - return (fstExtractRvatDataFromFrame(xc, facidx, buf)); - } } - } +} - /* return(NULL); */ +/* return(NULL); */ } + + /**********************************************************************/ #ifndef _WAVE_HAVE_JUDY @@ -6095,36 +6827,18 @@ mix() was built out of 36 single-cycle latency instructions in a to choose from. I only looked at a billion or so. -------------------------------------------------------------------- */ -#define mix(a, b, c) \ - { \ - a -= b; \ - a -= c; \ - a ^= (c >> 13); \ - b -= c; \ - b -= a; \ - b ^= (a << 8); \ - c -= a; \ - c -= b; \ - c ^= (b >> 13); \ - a -= b; \ - a -= c; \ - a ^= (c >> 12); \ - b -= c; \ - b -= a; \ - b ^= (a << 16); \ - c -= a; \ - c -= b; \ - c ^= (b >> 5); \ - a -= b; \ - a -= c; \ - a ^= (c >> 3); \ - b -= c; \ - b -= a; \ - b ^= (a << 10); \ - c -= a; \ - c -= b; \ - c ^= (b >> 15); \ - } +#define mix(a,b,c) \ +{ \ + a -= b; a -= c; a ^= (c>>13); \ + b -= c; b -= a; b ^= (a<<8); \ + c -= a; c -= b; c ^= (b>>13); \ + a -= b; a -= c; a ^= (c>>12); \ + b -= c; b -= a; b ^= (a<<16); \ + c -= a; c -= b; c ^= (b>>5); \ + a -= b; a -= c; a ^= (c>>3); \ + b -= c; b -= a; b ^= (a<<10); \ + c -= a; c -= b; c ^= (b>>15); \ +} /* -------------------------------------------------------------------- @@ -6156,55 +6870,44 @@ acceptable. Do NOT use for cryptographic purposes. static uint32_t j_hash(const uint8_t *k, uint32_t length, uint32_t initval) { - uint32_t a, b, c, len; - - /* Set up the internal state */ - len = length; - a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ - c = initval; /* the previous hash value */ - - /*---------------------------------------- handle most of the key */ - while (len >= 12) { - a += (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((uint32_t)k[3] << 24)); - b += (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6] << 16) + ((uint32_t)k[7] << 24)); - c += (k[8] + ((uint32_t)k[9] << 8) + ((uint32_t)k[10] << 16) + ((uint32_t)k[11] << 24)); - mix(a, b, c); - k += 12; - len -= 12; - } - - /*------------------------------------- handle the last 11 bytes */ - c += length; - switch (len) /* all the case statements fall through */ - { - case 11: - c += ((uint32_t)k[10] << 24); /* fallthrough */ - case 10: - c += ((uint32_t)k[9] << 16); /* fallthrough */ - case 9: - c += ((uint32_t)k[8] << 8); /* fallthrough */ - /* the first byte of c is reserved for the length */ - case 8: - b += ((uint32_t)k[7] << 24); /* fallthrough */ - case 7: - b += ((uint32_t)k[6] << 16); /* fallthrough */ - case 6: - b += ((uint32_t)k[5] << 8); /* fallthrough */ - case 5: - b += k[4]; /* fallthrough */ - case 4: - a += ((uint32_t)k[3] << 24); /* fallthrough */ - case 3: - a += ((uint32_t)k[2] << 16); /* fallthrough */ - case 2: - a += ((uint32_t)k[1] << 8); /* fallthrough */ - case 1: - a += k[0]; - /* case 0: nothing left to add */ - } - mix(a, b, c); - /*-------------------------------------------- report the result */ - return (c); + uint32_t a,b,c,len; + + /* Set up the internal state */ + len = length; + a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */ + c = initval; /* the previous hash value */ + + /*---------------------------------------- handle most of the key */ + while (len >= 12) + { + a += (k[0] +((uint32_t)k[1]<<8) +((uint32_t)k[2]<<16) +((uint32_t)k[3]<<24)); + b += (k[4] +((uint32_t)k[5]<<8) +((uint32_t)k[6]<<16) +((uint32_t)k[7]<<24)); + c += (k[8] +((uint32_t)k[9]<<8) +((uint32_t)k[10]<<16)+((uint32_t)k[11]<<24)); + mix(a,b,c); + k += 12; len -= 12; + } + + /*------------------------------------- handle the last 11 bytes */ + c += length; + switch(len) /* all the case statements fall through */ + { + case 11: c+=((uint32_t)k[10]<<24); /* fallthrough */ + case 10: c+=((uint32_t)k[9]<<16); /* fallthrough */ + case 9 : c+=((uint32_t)k[8]<<8); /* fallthrough */ + /* the first byte of c is reserved for the length */ + case 8 : b+=((uint32_t)k[7]<<24); /* fallthrough */ + case 7 : b+=((uint32_t)k[6]<<16); /* fallthrough */ + case 6 : b+=((uint32_t)k[5]<<8); /* fallthrough */ + case 5 : b+=k[4]; /* fallthrough */ + case 4 : a+=((uint32_t)k[3]<<24); /* fallthrough */ + case 3 : a+=((uint32_t)k[2]<<16); /* fallthrough */ + case 2 : a+=((uint32_t)k[1]<<8); /* fallthrough */ + case 1 : a+=k[0]; + /* case 0: nothing left to add */ + } + mix(a,b,c); + /*-------------------------------------------- report the result */ + return(c); } /********************************************************************/ @@ -6217,71 +6920,79 @@ static uint32_t j_hash(const uint8_t *k, uint32_t length, uint32_t initval) struct collchain_t { - struct collchain_t *next; - void *payload; - uint32_t fullhash, length; - unsigned char mem[1]; +struct collchain_t *next; +void *payload; +uint32_t fullhash, length; +unsigned char mem[1]; }; + void **JenkinsIns(void *base_i, const unsigned char *mem, uint32_t length, uint32_t hashmask) { - struct collchain_t ***base = (struct collchain_t ***)base_i; - uint32_t hf, h; - struct collchain_t **ar; - struct collchain_t *chain, *pchain; +struct collchain_t ***base = (struct collchain_t ***)base_i; +uint32_t hf, h; +struct collchain_t **ar; +struct collchain_t *chain, *pchain; - if (!*base) { +if(!*base) + { *base = (struct collchain_t **)calloc(1, (hashmask + 1) * sizeof(void *)); - } - ar = *base; - - h = (hf = j_hash(mem, length, length)) & hashmask; - pchain = chain = ar[h]; - while (chain) { - if ((chain->fullhash == hf) && (chain->length == length) && !memcmp(chain->mem, mem, length)) { - if (pchain != chain) /* move hit to front */ - { - pchain->next = chain->next; - chain->next = ar[h]; - ar[h] = chain; - } - return (&(chain->payload)); } +ar = *base; + +h = (hf = j_hash(mem, length, length)) & hashmask; +pchain = chain = ar[h]; +while(chain) + { + if((chain->fullhash == hf) && (chain->length == length) && !memcmp(chain->mem, mem, length)) + { + if(pchain != chain) /* move hit to front */ + { + pchain->next = chain->next; + chain->next = ar[h]; + ar[h] = chain; + } + return(&(chain->payload)); + } pchain = chain; chain = chain->next; - } + } - chain = (struct collchain_t *)calloc(1, sizeof(struct collchain_t) + length - 1); - memcpy(chain->mem, mem, length); - chain->fullhash = hf; - chain->length = length; - chain->next = ar[h]; - ar[h] = chain; - return (&(chain->payload)); +chain = (struct collchain_t *)calloc(1, sizeof(struct collchain_t) + length - 1); +memcpy(chain->mem, mem, length); +chain->fullhash = hf; +chain->length = length; +chain->next = ar[h]; +ar[h] = chain; +return(&(chain->payload)); } + void JenkinsFree(void *base_i, uint32_t hashmask) { - struct collchain_t ***base = (struct collchain_t ***)base_i; - uint32_t h; - struct collchain_t **ar; - struct collchain_t *chain, *chain_next; +struct collchain_t ***base = (struct collchain_t ***)base_i; +uint32_t h; +struct collchain_t **ar; +struct collchain_t *chain, *chain_next; - if (base && *base) { +if(base && *base) + { ar = *base; - for (h = 0; h <= hashmask; h++) { - chain = ar[h]; - while (chain) { - chain_next = chain->next; - free(chain); - chain = chain_next; - } - } + for(h=0;h<=hashmask;h++) + { + chain = ar[h]; + while(chain) + { + chain_next = chain->next; + free(chain); + chain = chain_next; + } + } free(*base); *base = NULL; - } + } } #endif @@ -6296,260 +7007,212 @@ void JenkinsFree(void *base_i, uint32_t hashmask) int fstUtilityBinToEscConvertedLen(const unsigned char *s, int len) { - const unsigned char *src = s; - int dlen = 0; - int i; - - for (i = 0; i < len; i++) { - switch (src[i]) { - case '\a': /* fallthrough */ - case '\b': /* fallthrough */ - case '\f': /* fallthrough */ - case '\n': /* fallthrough */ - case '\r': /* fallthrough */ - case '\t': /* fallthrough */ - case '\v': /* fallthrough */ - case '\'': /* fallthrough */ - case '\"': /* fallthrough */ - case '\\': /* fallthrough */ - case '\?': - dlen += 2; - break; - default: - if ((src[i] > ' ') && (src[i] <= '~')) /* no white spaces in output */ - { - dlen++; - } else { - dlen += 4; - } - break; - } - } - - return (dlen); +const unsigned char *src = s; +int dlen = 0; +int i; + +for(i=0;i ' ') && (src[i] <= '~')) /* no white spaces in output */ + { + dlen++; + } + else + { + dlen += 4; + } + break; + } + } + +return(dlen); } + int fstUtilityBinToEsc(unsigned char *d, const unsigned char *s, int len) { - const unsigned char *src = s; - unsigned char *dst = d; - unsigned char val; - int i; - - for (i = 0; i < len; i++) { - switch (src[i]) { - case '\a': - *(dst++) = '\\'; - *(dst++) = 'a'; - break; - case '\b': - *(dst++) = '\\'; - *(dst++) = 'b'; - break; - case '\f': - *(dst++) = '\\'; - *(dst++) = 'f'; - break; - case '\n': - *(dst++) = '\\'; - *(dst++) = 'n'; - break; - case '\r': - *(dst++) = '\\'; - *(dst++) = 'r'; - break; - case '\t': - *(dst++) = '\\'; - *(dst++) = 't'; - break; - case '\v': - *(dst++) = '\\'; - *(dst++) = 'v'; - break; - case '\'': - *(dst++) = '\\'; - *(dst++) = '\''; - break; - case '\"': - *(dst++) = '\\'; - *(dst++) = '\"'; - break; - case '\\': - *(dst++) = '\\'; - *(dst++) = '\\'; - break; - case '\?': - *(dst++) = '\\'; - *(dst++) = '\?'; - break; - default: - if ((src[i] > ' ') && (src[i] <= '~')) /* no white spaces in output */ - { - *(dst++) = src[i]; - } else { - val = src[i]; - *(dst++) = '\\'; - *(dst++) = (val / 64) + '0'; - val = val & 63; - *(dst++) = (val / 8) + '0'; - val = val & 7; - *(dst++) = (val) + '0'; - } - break; +const unsigned char *src = s; +unsigned char *dst = d; +unsigned char val; +int i; + +for(i=0;i ' ') && (src[i] <= '~')) /* no white spaces in output */ + { + *(dst++) = src[i]; + } + else + { + val = src[i]; + *(dst++) = '\\'; + *(dst++) = (val/64) + '0'; val = val & 63; + *(dst++) = (val/8) + '0'; val = val & 7; + *(dst++) = (val) + '0'; + } + break; + } } - } - return (dst - d); +return(dst - d); } + /* * this overwrites the original string if the destination pointer is NULL */ int fstUtilityEscToBin(unsigned char *d, unsigned char *s, int len) { - unsigned char *src = s; - unsigned char *dst = (!d) ? s : (s = d); - unsigned char val[3]; - int i; - - for (i = 0; i < len; i++) { - if (src[i] != '\\') { - *(dst++) = src[i]; - } else { - switch (src[++i]) { - case 'a': - *(dst++) = '\a'; - break; - case 'b': - *(dst++) = '\b'; - break; - case 'f': - *(dst++) = '\f'; - break; - case 'n': - *(dst++) = '\n'; - break; - case 'r': - *(dst++) = '\r'; - break; - case 't': - *(dst++) = '\t'; - break; - case 'v': - *(dst++) = '\v'; - break; - case '\'': - *(dst++) = '\''; - break; - case '\"': - *(dst++) = '\"'; - break; - case '\\': - *(dst++) = '\\'; - break; - case '\?': - *(dst++) = '\?'; - break; +unsigned char *src = s; +unsigned char *dst = (!d) ? s : (s = d); +unsigned char val[3]; +int i; - case 'x': - val[0] = toupper(src[++i]); - val[1] = toupper(src[++i]); - val[0] = ((val[0] >= 'A') && (val[0] <= 'F')) ? (val[0] - 'A' + 10) : (val[0] - '0'); - val[1] = ((val[1] >= 'A') && (val[1] <= 'F')) ? (val[1] - 'A' + 10) : (val[1] - '0'); - *(dst++) = val[0] * 16 + val[1]; - break; +for(i=0;i='A')&&(val[0]<='F')) ? (val[0] - 'A' + 10) : (val[0] - '0'); + val[1] = ((val[1]>='A')&&(val[1]<='F')) ? (val[1] - 'A' + 10) : (val[1] - '0'); + *(dst++) = val[0] * 16 + val[1]; + break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - val[0] = src[i] - '0'; - val[1] = src[++i] - '0'; - val[2] = src[++i] - '0'; - *(dst++) = val[0] * 64 + val[1] * 8 + val[2]; - break; + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': val[0] = src[ i] - '0'; + val[1] = src[++i] - '0'; + val[2] = src[++i] - '0'; + *(dst++) = val[0] * 64 + val[1] * 8 + val[2]; + break; - default: - *(dst++) = src[i]; - break; - } + default: *(dst++) = src[i]; break; + } + } } - } - return (dst - s); +return(dst - s); } + struct fstETab *fstUtilityExtractEnumTableFromString(const char *s) { - struct fstETab *et = NULL; - int num_spaces = 0; - int i; - int newlen; +struct fstETab *et = NULL; +int num_spaces = 0; +int i; +int newlen; - if (s) { - const char *csp = strchr(s, ' '); - int cnt = atoi(csp + 1); +if(s) + { + const char *csp = strchr(s, ' '); + int cnt = atoi(csp+1); - for (;;) { - csp = strchr(csp + 1, ' '); - if (csp) { - num_spaces++; - } else { - break; - } - } + for(;;) + { + csp = strchr(csp+1, ' '); + if(csp) { num_spaces++; } else { break; } + } - if (num_spaces == (2 * cnt)) { - char *sp, *sp2; + if(num_spaces == (2*cnt)) + { + char *sp, *sp2; - et = (struct fstETab *)calloc(1, sizeof(struct fstETab)); - et->elem_count = cnt; - et->name = strdup(s); - et->literal_arr = (char **)calloc(cnt, sizeof(char *)); - et->val_arr = (char **)calloc(cnt, sizeof(char *)); + et = (struct fstETab*)calloc(1, sizeof(struct fstETab)); + et->elem_count = cnt; + et->name = strdup(s); + et->literal_arr = (char**)calloc(cnt, sizeof(char *)); + et->val_arr = (char**)calloc(cnt, sizeof(char *)); - sp = strchr(et->name, ' '); - *sp = 0; + sp = strchr(et->name, ' '); + *sp = 0; - sp = strchr(sp + 1, ' '); + sp = strchr(sp+1, ' '); - for (i = 0; i < cnt; i++) { - sp2 = strchr(sp + 1, ' '); - *(char *)sp2 = 0; - et->literal_arr[i] = sp + 1; - sp = sp2; + for(i=0;iliteral_arr[i] = sp+1; + sp = sp2; - newlen = fstUtilityEscToBin(NULL, (unsigned char *)et->literal_arr[i], strlen(et->literal_arr[i])); - et->literal_arr[i][newlen] = 0; - } + newlen = fstUtilityEscToBin(NULL, (unsigned char*)et->literal_arr[i], strlen(et->literal_arr[i])); + et->literal_arr[i][newlen] = 0; + } - for (i = 0; i < cnt; i++) { - sp2 = strchr(sp + 1, ' '); - if (sp2) { - *sp2 = 0; - } - et->val_arr[i] = sp + 1; - sp = sp2; + for(i=0;ival_arr[i] = sp+1; + sp = sp2; - newlen = fstUtilityEscToBin(NULL, (unsigned char *)et->val_arr[i], strlen(et->val_arr[i])); - et->val_arr[i][newlen] = 0; - } - } - } + newlen = fstUtilityEscToBin(NULL, (unsigned char*)et->val_arr[i], strlen(et->val_arr[i])); + et->val_arr[i][newlen] = 0; + } + } + } - return (et); +return(et); } + void fstUtilityFreeEnumTable(struct fstETab *etab) { - if (etab) { - free(etab->literal_arr); - free(etab->val_arr); - free(etab->name); - free(etab); - } +if(etab) + { + free(etab->literal_arr); + free(etab->val_arr); + free(etab->name); + free(etab); + } } diff --git a/libs/fst/fstapi.h b/libs/fst/fstapi.h index a5e0971a16b..ba58c2017cc 100644 --- a/libs/fst/fstapi.h +++ b/libs/fst/fstapi.h @@ -35,28 +35,11 @@ extern "C" { #include #include #if defined(_MSC_VER) -#include "libs/zlib/zlib.h" -#include - -#include - -#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" + #include "libs/zlib/zlib.h" + #include "fst_win_unistd.h" #else -#include -#include + #include + #include #endif #include @@ -65,433 +48,417 @@ extern "C" { 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 fstWriterPackType { + FST_WR_PT_ZLIB = 0, + FST_WR_PT_FASTLZ = 1, + FST_WR_PT_LZ4 = 2 }; -enum fstFileType -{ - FST_FT_MIN = 0, +enum fstFileType { + FST_FT_MIN = 0, - FST_FT_VERILOG = 0, - FST_FT_VHDL = 1, - FST_FT_VERILOG_VHDL = 2, + FST_FT_VERILOG = 0, + FST_FT_VHDL = 1, + FST_FT_VERILOG_VHDL = 2, - FST_FT_MAX = 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 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_MAX = 21, - - FST_ST_GEN_ATTRBEGIN = 252, - FST_ST_GEN_ATTREND = 253, - - FST_ST_VCD_SCOPE = 254, - FST_ST_VCD_UPSCOPE = 255 +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_MAX = 21, + + 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 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, +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_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 + FST_VD_MAX = 5 }; -enum fstHierType -{ - FST_HT_MIN = 0, +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_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_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 + FST_HT_MAX = 6 }; -enum fstAttrType -{ - FST_AT_MIN = 0, +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_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 + 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 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, +enum fstArrayType { + FST_AR_MIN = 0, - FST_AR_NONE = 0, - FST_AR_UNPACKED = 1, - FST_AR_PACKED = 2, - FST_AR_SPARSE = 3, + FST_AR_NONE = 0, + FST_AR_UNPACKED = 1, + FST_AR_PACKED = 2, + FST_AR_SPARSE = 3, - FST_AR_MAX = 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, +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_SV_UNSIGNED_LONGINT = 12, + FST_EV_SV_UNSIGNED_BYTE = 13, - FST_EV_REG = 14, - FST_EV_TIME = 15, + FST_EV_REG = 14, + FST_EV_TIME = 15, - FST_EV_MAX = 15 + FST_EV_MAX = 15 }; -enum fstPackType -{ - FST_PT_NONE = 0, - FST_PT_UNPACKED = 1, - FST_PT_PACKED = 2, +enum fstPackType { + FST_PT_NONE = 0, + FST_PT_UNPACKED = 1, + FST_PT_PACKED = 2, FST_PT_TAGGED_PACKED = 3, - FST_PT_MAX = 3 + FST_PT_MAX = 3 }; -enum fstSupplementalVarType -{ - FST_SVT_MIN = 0, +enum fstSupplementalVarType { + FST_SVT_MIN = 0, - FST_SVT_NONE = 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_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 + FST_SVT_MAX = 5 }; -enum fstSupplementalDataType -{ - FST_SDT_MIN = 0, +enum fstSupplementalDataType { + FST_SDT_MIN = 0, - FST_SDT_NONE = 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_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) + 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; +unsigned char htyp; - union - { +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; + 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; + 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 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; +char *name; +uint32_t elem_count; +char **literal_arr; +char **val_arr; }; + /* * writer functions */ -void fstWriterClose(void *ctx); -void *fstWriterCreate(const char *nam, int use_compressed_hier); -fstEnumHandle fstWriterCreateEnumTable(void *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(void *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(void *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(void *ctx, int enable); -void fstWriterEmitEnumTableRef(void *ctx, fstEnumHandle handle); -void fstWriterEmitValueChange(void *ctx, fstHandle handle, const void *val); -void fstWriterEmitValueChange32(void *ctx, fstHandle handle, uint32_t bits, uint32_t val); -void fstWriterEmitValueChange64(void *ctx, fstHandle handle, uint32_t bits, uint64_t val); -void fstWriterEmitValueChangeVec32(void *ctx, fstHandle handle, uint32_t bits, const uint32_t *val); -void fstWriterEmitValueChangeVec64(void *ctx, fstHandle handle, uint32_t bits, const uint64_t *val); -void fstWriterEmitVariableLengthValueChange(void *ctx, fstHandle handle, const void *val, uint32_t len); -void fstWriterEmitTimeChange(void *ctx, uint64_t tim); -void fstWriterFlushContext(void *ctx); -int fstWriterGetDumpSizeLimitReached(void *ctx); -int fstWriterGetFseekFailed(void *ctx); -void fstWriterSetAttrBegin(void *ctx, enum fstAttrType attrtype, int subtype, const char *attrname, uint64_t arg); -void fstWriterSetAttrEnd(void *ctx); -void fstWriterSetComment(void *ctx, const char *comm); -void fstWriterSetDate(void *ctx, const char *dat); -void fstWriterSetDumpSizeLimit(void *ctx, uint64_t numbytes); -void fstWriterSetEnvVar(void *ctx, const char *envvar); -void fstWriterSetFileType(void *ctx, enum fstFileType filetype); -void fstWriterSetPackType(void *ctx, enum fstWriterPackType typ); -void fstWriterSetParallelMode(void *ctx, int enable); -void fstWriterSetRepackOnClose(void *ctx, int enable); /* type = 0 (none), 1 (libz) */ -void fstWriterSetScope(void *ctx, enum fstScopeType scopetype, const char *scopename, const char *scopecomp); -void fstWriterSetSourceInstantiationStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath); -void fstWriterSetSourceStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath); -void fstWriterSetTimescale(void *ctx, int ts); -void fstWriterSetTimescaleFromString(void *ctx, const char *s); -void fstWriterSetTimezero(void *ctx, int64_t tim); -void fstWriterSetUpscope(void *ctx); -void fstWriterSetValueList(void *ctx, const char *vl); -void fstWriterSetVersion(void *ctx, const char *vers); +void fstWriterClose(void *ctx); +void * fstWriterCreate(const char *nam, int use_compressed_hier); +fstEnumHandle fstWriterCreateEnumTable(void *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(void *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(void *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(void *ctx, int enable); +void fstWriterEmitEnumTableRef(void *ctx, fstEnumHandle handle); +void fstWriterEmitValueChange(void *ctx, fstHandle handle, const void *val); +void fstWriterEmitValueChange32(void *ctx, fstHandle handle, + uint32_t bits, uint32_t val); +void fstWriterEmitValueChange64(void *ctx, fstHandle handle, + uint32_t bits, uint64_t val); +void fstWriterEmitValueChangeVec32(void *ctx, fstHandle handle, + uint32_t bits, const uint32_t *val); +void fstWriterEmitValueChangeVec64(void *ctx, fstHandle handle, + uint32_t bits, const uint64_t *val); +void fstWriterEmitVariableLengthValueChange(void *ctx, fstHandle handle, const void *val, uint32_t len); +void fstWriterEmitTimeChange(void *ctx, uint64_t tim); +void fstWriterFlushContext(void *ctx); +int fstWriterGetDumpSizeLimitReached(void *ctx); +int fstWriterGetFseekFailed(void *ctx); +void fstWriterSetAttrBegin(void *ctx, enum fstAttrType attrtype, int subtype, + const char *attrname, uint64_t arg); +void fstWriterSetAttrEnd(void *ctx); +void fstWriterSetComment(void *ctx, const char *comm); +void fstWriterSetDate(void *ctx, const char *dat); +void fstWriterSetDumpSizeLimit(void *ctx, uint64_t numbytes); +void fstWriterSetEnvVar(void *ctx, const char *envvar); +void fstWriterSetFileType(void *ctx, enum fstFileType filetype); +void fstWriterSetPackType(void *ctx, enum fstWriterPackType typ); +void fstWriterSetParallelMode(void *ctx, int enable); +void fstWriterSetRepackOnClose(void *ctx, int enable); /* type = 0 (none), 1 (libz) */ +void fstWriterSetScope(void *ctx, enum fstScopeType scopetype, + const char *scopename, const char *scopecomp); +void fstWriterSetSourceInstantiationStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath); +void fstWriterSetSourceStem(void *ctx, const char *path, unsigned int line, unsigned int use_realpath); +void fstWriterSetTimescale(void *ctx, int ts); +void fstWriterSetTimescaleFromString(void *ctx, const char *s); +void fstWriterSetTimezero(void *ctx, int64_t tim); +void fstWriterSetUpscope(void *ctx); +void fstWriterSetValueList(void *ctx, const char *vl); +void fstWriterSetVersion(void *ctx, const char *vers); + /* * reader functions */ -void fstReaderClose(void *ctx); -void fstReaderClrFacProcessMask(void *ctx, fstHandle facidx); -void fstReaderClrFacProcessMaskAll(void *ctx); -uint64_t fstReaderGetAliasCount(void *ctx); -const char *fstReaderGetCurrentFlatScope(void *ctx); -void *fstReaderGetCurrentScopeUserInfo(void *ctx); -int fstReaderGetCurrentScopeLen(void *ctx); -const char *fstReaderGetDateString(void *ctx); -int fstReaderGetDoubleEndianMatchState(void *ctx); -uint64_t fstReaderGetDumpActivityChangeTime(void *ctx, uint32_t idx); -unsigned char fstReaderGetDumpActivityChangeValue(void *ctx, uint32_t idx); -uint64_t fstReaderGetEndTime(void *ctx); -int fstReaderGetFacProcessMask(void *ctx, fstHandle facidx); -int fstReaderGetFileType(void *ctx); -int fstReaderGetFseekFailed(void *ctx); -fstHandle fstReaderGetMaxHandle(void *ctx); -uint64_t fstReaderGetMemoryUsedByWriter(void *ctx); -uint32_t fstReaderGetNumberDumpActivityChanges(void *ctx); -uint64_t fstReaderGetScopeCount(void *ctx); -uint64_t fstReaderGetStartTime(void *ctx); -signed char fstReaderGetTimescale(void *ctx); -int64_t fstReaderGetTimezero(void *ctx); -uint64_t fstReaderGetValueChangeSectionCount(void *ctx); -char *fstReaderGetValueFromHandleAtTime(void *ctx, uint64_t tim, fstHandle facidx, char *buf); -uint64_t fstReaderGetVarCount(void *ctx); -const char *fstReaderGetVersionString(void *ctx); +void fstReaderClose(void *ctx); +void fstReaderClrFacProcessMask(void *ctx, fstHandle facidx); +void fstReaderClrFacProcessMaskAll(void *ctx); +uint64_t fstReaderGetAliasCount(void *ctx); +const char * fstReaderGetCurrentFlatScope(void *ctx); +void * fstReaderGetCurrentScopeUserInfo(void *ctx); +int fstReaderGetCurrentScopeLen(void *ctx); +const char * fstReaderGetDateString(void *ctx); +int fstReaderGetDoubleEndianMatchState(void *ctx); +uint64_t fstReaderGetDumpActivityChangeTime(void *ctx, uint32_t idx); +unsigned char fstReaderGetDumpActivityChangeValue(void *ctx, uint32_t idx); +uint64_t fstReaderGetEndTime(void *ctx); +int fstReaderGetFacProcessMask(void *ctx, fstHandle facidx); +int fstReaderGetFileType(void *ctx); +int fstReaderGetFseekFailed(void *ctx); +fstHandle fstReaderGetMaxHandle(void *ctx); +uint64_t fstReaderGetMemoryUsedByWriter(void *ctx); +uint32_t fstReaderGetNumberDumpActivityChanges(void *ctx); +uint64_t fstReaderGetScopeCount(void *ctx); +uint64_t fstReaderGetStartTime(void *ctx); +signed char fstReaderGetTimescale(void *ctx); +int64_t fstReaderGetTimezero(void *ctx); +uint64_t fstReaderGetValueChangeSectionCount(void *ctx); +char * fstReaderGetValueFromHandleAtTime(void *ctx, uint64_t tim, fstHandle facidx, char *buf); +uint64_t fstReaderGetVarCount(void *ctx); +const char * fstReaderGetVersionString(void *ctx); struct fstHier *fstReaderIterateHier(void *ctx); -int fstReaderIterateHierRewind(void *ctx); -int fstReaderIterBlocks(void *ctx, - void (*value_change_callback)(void *user_callback_data_pointer, uint64_t time, fstHandle facidx, - const unsigned char *value), +int fstReaderIterateHierRewind(void *ctx); +int fstReaderIterBlocks(void *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(void *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(void *ctx, int enable); -void *fstReaderOpen(const char *nam); -void *fstReaderOpenForUtilitiesOnly(void); -const char *fstReaderPopScope(void *ctx); -int fstReaderProcessHier(void *ctx, FILE *vcdhandle); -const char *fstReaderPushScope(void *ctx, const char *nam, void *user_info); -void fstReaderResetScope(void *ctx); -void fstReaderSetFacProcessMask(void *ctx, fstHandle facidx); -void fstReaderSetFacProcessMaskAll(void *ctx); -void fstReaderSetLimitTimeRange(void *ctx, uint64_t start_time, uint64_t end_time); -void fstReaderSetUnlimitedTimeRange(void *ctx); -void fstReaderSetVcdExtensions(void *ctx, int enable); +int fstReaderIterBlocks2(void *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(void *ctx, int enable); +void * fstReaderOpen(const char *nam); +void * fstReaderOpenForUtilitiesOnly(void); +const char * fstReaderPopScope(void *ctx); +int fstReaderProcessHier(void *ctx, FILE *vcdhandle); +const char * fstReaderPushScope(void *ctx, const char *nam, void *user_info); +void fstReaderResetScope(void *ctx); +void fstReaderSetFacProcessMask(void *ctx, fstHandle facidx); +void fstReaderSetFacProcessMaskAll(void *ctx); +void fstReaderSetLimitTimeRange(void *ctx, uint64_t start_time, uint64_t end_time); +void fstReaderSetUnlimitedTimeRange(void *ctx); +void fstReaderSetVcdExtensions(void *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); +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 */ +void fstUtilityFreeEnumTable(struct fstETab *etab); /* must use to free fstETab properly */ + #ifdef __cplusplus } diff --git a/libs/fst/lz4.cc b/libs/fst/lz4.cc index 7e94f2492f4..0a727596b85 100644 --- a/libs/fst/lz4.cc +++ b/libs/fst/lz4.cc @@ -1,6 +1,6 @@ /* LZ4 - Fast LZ compression algorithm - Copyright (C) 2011-2015, Yann Collet. + Copyright (C) 2011-2023, Yann Collet. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) @@ -27,1396 +27,2559 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - SPDX-License-Identifier: BSD-2-Clause - You can contact the author at : - - LZ4 source repository : https://github.com/Cyan4973/lz4 - - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c + - LZ4 homepage : http://www.lz4.org + - LZ4 source repository : https://github.com/lz4/lz4 */ -/************************************** - * Tuning parameters - **************************************/ +/*-************************************ +* Tuning parameters +**************************************/ /* - * HEAPMODE : - * Select how default compression functions will allocate memory for their hash table, + * LZ4_HEAPMODE : + * Select how stateless compression functions like `LZ4_compress_default()` + * allocate memory for their hash table, * in memory stack (0:default, fastest), or in memory heap (1:requires malloc()). */ -#define HEAPMODE 0 +#ifndef LZ4_HEAPMODE +# define LZ4_HEAPMODE 0 +#endif /* - * ACCELERATION_DEFAULT : + * LZ4_ACCELERATION_DEFAULT : * Select "acceleration" for LZ4_compress_fast() when parameter value <= 0 */ -#define ACCELERATION_DEFAULT 1 +#define LZ4_ACCELERATION_DEFAULT 1 +/* + * LZ4_ACCELERATION_MAX : + * Any "acceleration" value higher than this threshold + * get treated as LZ4_ACCELERATION_MAX instead (fix #876) + */ +#define LZ4_ACCELERATION_MAX 65537 + + +/*-************************************ +* CPU Feature Detection +**************************************/ +/* LZ4_FORCE_MEMORY_ACCESS + * By default, access to unaligned memory is controlled by `memcpy()`, which is safe and portable. + * Unfortunately, on some target/compiler combinations, the generated assembly is sub-optimal. + * The below switch allow to select different access method for improved performance. + * Method 0 (default) : use `memcpy()`. Safe and portable. + * Method 1 : `__packed` statement. It depends on compiler extension (ie, not portable). + * This method is safe if your compiler supports it, and *generally* as fast or faster than `memcpy`. + * Method 2 : direct access. This method is portable but violate C standard. + * It can generate buggy code on targets which assembly generation depends on alignment. + * But in some circumstances, it's the only known way to get the most performance (ie GCC + ARMv6) + * See https://fastcompression.blogspot.fr/2015/08/accessing-unaligned-memory.html for details. + * Prefer these methods in priority order (0 > 1 > 2) + */ +#ifndef LZ4_FORCE_MEMORY_ACCESS /* can be defined externally */ +# if defined(__GNUC__) && \ + ( defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) \ + || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6T2__) ) +# define LZ4_FORCE_MEMORY_ACCESS 2 +# elif (defined(__INTEL_COMPILER) && !defined(_WIN32)) || defined(__GNUC__) || defined(_MSC_VER) +# define LZ4_FORCE_MEMORY_ACCESS 1 +# endif +#endif -/************************************** - * CPU Feature Detection - **************************************/ /* * LZ4_FORCE_SW_BITCOUNT * Define this parameter if your target system or compiler does not support hardware bit count */ -#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */ -#define LZ4_FORCE_SW_BITCOUNT +#if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for WinCE doesn't support Hardware bit count */ +# undef LZ4_FORCE_SW_BITCOUNT /* avoid double def */ +# define LZ4_FORCE_SW_BITCOUNT #endif -/************************************** - * Includes - **************************************/ + + +/*-************************************ +* Dependency +**************************************/ +/* + * LZ4_SRC_INCLUDED: + * Amalgamation flag, whether lz4.c is included + */ +#ifndef LZ4_SRC_INCLUDED +# define LZ4_SRC_INCLUDED 1 +#endif + +#ifndef LZ4_DISABLE_DEPRECATE_WARNINGS +# define LZ4_DISABLE_DEPRECATE_WARNINGS /* due to LZ4_decompress_safe_withPrefix64k */ +#endif + +#ifndef LZ4_STATIC_LINKING_ONLY +# define LZ4_STATIC_LINKING_ONLY +#endif #include "lz4.h" +/* see also "memory routines" below */ + + +/*-************************************ +* Compiler Options +**************************************/ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) /* Visual Studio 2005+ */ +# include /* only present in VS2005+ */ +# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ +# pragma warning(disable : 6237) /* disable: C6237: conditional expression is always 0 */ +#endif /* _MSC_VER */ + +#ifndef LZ4_FORCE_INLINE +# ifdef _MSC_VER /* Visual Studio */ +# define LZ4_FORCE_INLINE static __forceinline +# else +# if defined (__cplusplus) || defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L /* C99 */ +# ifdef __GNUC__ +# define LZ4_FORCE_INLINE static inline __attribute__((always_inline)) +# else +# define LZ4_FORCE_INLINE static inline +# endif +# else +# define LZ4_FORCE_INLINE static +# endif /* __STDC_VERSION__ */ +# endif /* _MSC_VER */ +#endif /* LZ4_FORCE_INLINE */ + +/* LZ4_FORCE_O2 and LZ4_FORCE_INLINE + * gcc on ppc64le generates an unrolled SIMDized loop for LZ4_wildCopy8, + * together with a simple 8-byte copy loop as a fall-back path. + * However, this optimization hurts the decompression speed by >30%, + * because the execution does not go to the optimized loop + * for typical compressible data, and all of the preamble checks + * before going to the fall-back path become useless overhead. + * This optimization happens only with the -O3 flag, and -O2 generates + * a simple 8-byte copy loop. + * With gcc on ppc64le, all of the LZ4_decompress_* and LZ4_wildCopy8 + * functions are annotated with __attribute__((optimize("O2"))), + * and also LZ4_wildCopy8 is forcibly inlined, so that the O2 attribute + * of LZ4_wildCopy8 does not affect the compression speed. + */ +#if defined(__PPC64__) && defined(__LITTLE_ENDIAN__) && defined(__GNUC__) && !defined(__clang__) +# define LZ4_FORCE_O2 __attribute__((optimize("O2"))) +# undef LZ4_FORCE_INLINE +# define LZ4_FORCE_INLINE static __inline __attribute__((optimize("O2"),always_inline)) +#else +# define LZ4_FORCE_O2 +#endif -/************************************** - * Compiler Options - **************************************/ -#ifdef _MSC_VER /* Visual Studio */ -#define FORCE_INLINE static __forceinline -#include -#pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */ +#if (defined(__GNUC__) && (__GNUC__ >= 3)) || (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 800)) || defined(__clang__) +# define expect(expr,value) (__builtin_expect ((expr),(value)) ) #else -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ -#if defined(__GNUC__) || defined(__clang__) -#define FORCE_INLINE static inline __attribute__((always_inline)) +# define expect(expr,value) (expr) +#endif + +#ifndef likely +#define likely(expr) expect((expr) != 0, 1) +#endif +#ifndef unlikely +#define unlikely(expr) expect((expr) != 0, 0) +#endif + +/* Should the alignment test prove unreliable, for some reason, + * it can be disabled by setting LZ4_ALIGN_TEST to 0 */ +#ifndef LZ4_ALIGN_TEST /* can be externally provided */ +# define LZ4_ALIGN_TEST 1 +#endif + + +/*-************************************ +* Memory routines +**************************************/ + +/*! LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION : + * Disable relatively high-level LZ4/HC functions that use dynamic memory + * allocation functions (malloc(), calloc(), free()). + * + * Note that this is a compile-time switch. And since it disables + * public/stable LZ4 v1 API functions, we don't recommend using this + * symbol to generate a library for distribution. + * + * The following public functions are removed when this symbol is defined. + * - lz4 : LZ4_createStream, LZ4_freeStream, + * LZ4_createStreamDecode, LZ4_freeStreamDecode, LZ4_create (deprecated) + * - lz4hc : LZ4_createStreamHC, LZ4_freeStreamHC, + * LZ4_createHC (deprecated), LZ4_freeHC (deprecated) + * - lz4frame, lz4file : All LZ4F_* functions + */ +#if defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +# define ALLOC(s) lz4_error_memory_allocation_is_disabled +# define ALLOC_AND_ZERO(s) lz4_error_memory_allocation_is_disabled +# define FREEMEM(p) lz4_error_memory_allocation_is_disabled +#elif defined(LZ4_USER_MEMORY_FUNCTIONS) +/* memory management functions can be customized by user project. + * Below functions must exist somewhere in the Project + * and be available at link time */ +void* LZ4_malloc(size_t s); +void* LZ4_calloc(size_t n, size_t s); +void LZ4_free(void* p); +# define ALLOC(s) LZ4_malloc(s) +# define ALLOC_AND_ZERO(s) LZ4_calloc(1,s) +# define FREEMEM(p) LZ4_free(p) +#else +# include /* malloc, calloc, free */ +# define ALLOC(s) malloc(s) +# define ALLOC_AND_ZERO(s) calloc(1,s) +# define FREEMEM(p) free(p) +#endif + +#if ! LZ4_FREESTANDING +# include /* memset, memcpy */ +#endif +#if !defined(LZ4_memset) +# define LZ4_memset(p,v,s) memset((p),(v),(s)) +#endif +#define MEM_INIT(p,v,s) LZ4_memset((p),(v),(s)) + + +/*-************************************ +* Common Constants +**************************************/ +#define MINMATCH 4 + +#define WILDCOPYLENGTH 8 +#define LASTLITERALS 5 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ +#define MFLIMIT 12 /* see ../doc/lz4_Block_format.md#parsing-restrictions */ +#define MATCH_SAFEGUARD_DISTANCE ((2*WILDCOPYLENGTH) - MINMATCH) /* ensure it's possible to write 2 x wildcopyLength without overflowing output buffer */ +#define FASTLOOP_SAFE_DISTANCE 64 +static const int LZ4_minLength = (MFLIMIT+1); + +#define KB *(1 <<10) +#define MB *(1 <<20) +#define GB *(1U<<30) + +#define LZ4_DISTANCE_ABSOLUTE_MAX 65535 +#if (LZ4_DISTANCE_MAX > LZ4_DISTANCE_ABSOLUTE_MAX) /* max supported by LZ4 format */ +# error "LZ4_DISTANCE_MAX is too big : must be <= 65535" +#endif + +#define ML_BITS 4 +#define ML_MASK ((1U<=1) +# include #else -#define FORCE_INLINE static inline +# ifndef assert +# define assert(condition) ((void)0) +# endif #endif + +#define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use after variable declarations */ + +#if defined(LZ4_DEBUG) && (LZ4_DEBUG>=2) +# include + static int g_debuglog_enable = 1; +# define DEBUGLOG(l, ...) { \ + if ((g_debuglog_enable) && (l<=LZ4_DEBUG)) { \ + fprintf(stderr, __FILE__ " %i: ", __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, " \n"); \ + } } #else -#define FORCE_INLINE static -#endif /* __STDC_VERSION__ */ -#endif /* _MSC_VER */ +# define DEBUGLOG(l, ...) {} /* disabled */ +#endif + +static int LZ4_isAligned(const void* ptr, size_t alignment) +{ + return ((size_t)ptr & (alignment -1)) == 0; +} -/* LZ4_GCC_VERSION is defined into lz4.h */ -#if (LZ4_GCC_VERSION >= 302) || (__INTEL_COMPILER >= 800) || defined(__clang__) -#define expect(expr, value) (__builtin_expect((expr), (value))) + +/*-************************************ +* Types +**************************************/ +#include +#if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +# include + typedef uint8_t BYTE; + typedef uint16_t U16; + typedef uint32_t U32; + typedef int32_t S32; + typedef uint64_t U64; + typedef uintptr_t uptrval; #else -#define expect(expr, value) (expr) +# if UINT_MAX != 4294967295UL +# error "LZ4 code (when not C++ or C99) assumes that sizeof(int) == 4" +# endif + typedef unsigned char BYTE; + typedef unsigned short U16; + typedef unsigned int U32; + typedef signed int S32; + typedef unsigned long long U64; + typedef size_t uptrval; /* generally true, except OpenVMS-64 */ #endif -#define likely(expr) expect((expr) != 0, 1) -#define unlikely(expr) expect((expr) != 0, 0) - -/************************************** - * Memory routines - **************************************/ -#include /* malloc, calloc, free */ -#define ALLOCATOR(n, s) calloc(n, s) -#define FREEMEM free -#include /* memset, memcpy */ -#define MEM_INIT memset - -/************************************** - * Basic Types - **************************************/ -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */ -#include -typedef uint8_t BYTE; -typedef uint16_t U16; -typedef uint32_t U32; -typedef int32_t S32; -typedef uint64_t U64; +#if defined(__x86_64__) + typedef U64 reg_t; /* 64-bits in x32 mode */ #else -typedef unsigned char BYTE; -typedef unsigned short U16; -typedef unsigned int U32; -typedef signed int S32; -typedef unsigned long long U64; + typedef size_t reg_t; /* 32-bits in x32 mode */ #endif -/************************************** - * Reading and writing into memory - **************************************/ -#define STEPSIZE sizeof(size_t) +typedef enum { + notLimited = 0, + limitedOutput = 1, + fillOutput = 2 +} limitedOutput_directive; -static unsigned LZ4_64bits(void) { return sizeof(void *) == 8; } + +/*-************************************ +* Reading and writing into memory +**************************************/ + +/** + * LZ4 relies on memcpy with a constant size being inlined. In freestanding + * environments, the compiler can't assume the implementation of memcpy() is + * standard compliant, so it can't apply its specialized memcpy() inlining + * logic. When possible, use __builtin_memcpy() to tell the compiler to analyze + * memcpy() as if it were standard compliant, so it can inline it in freestanding + * environments. This is needed when decompressing the Linux Kernel, for example. + */ +#if !defined(LZ4_memcpy) +# if defined(__GNUC__) && (__GNUC__ >= 4) +# define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size) +# else +# define LZ4_memcpy(dst, src, size) memcpy(dst, src, size) +# endif +#endif + +#if !defined(LZ4_memmove) +# if defined(__GNUC__) && (__GNUC__ >= 4) +# define LZ4_memmove __builtin_memmove +# else +# define LZ4_memmove memmove +# endif +#endif static unsigned LZ4_isLittleEndian(void) { - const union - { - U32 i; - BYTE c[4]; - } one = {1}; /* don't use static : performance detrimental */ + const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */ return one.c[0]; } -static U16 LZ4_read16(const void *memPtr) +#if defined(__GNUC__) || defined(__INTEL_COMPILER) +#define LZ4_PACK( __Declaration__ ) __Declaration__ __attribute__((__packed__)) +#elif defined(_MSC_VER) +#define LZ4_PACK( __Declaration__ ) __pragma( pack(push, 1) ) __Declaration__ __pragma( pack(pop)) +#endif + +#if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2) +/* lie to the compiler about data alignment; use with caution */ + +static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; } +static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; } +static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; } + +static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; } +static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; } + +#elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1) + +/* __pack instructions are safer, but compiler specific, hence potentially problematic for some compilers */ +/* currently only defined for gcc and icc */ +LZ4_PACK(typedef struct { U16 u16; }) LZ4_unalign16; +LZ4_PACK(typedef struct { U32 u32; }) LZ4_unalign32; +LZ4_PACK(typedef struct { reg_t uArch; }) LZ4_unalignST; + +static U16 LZ4_read16(const void* ptr) { return ((const LZ4_unalign16*)ptr)->u16; } +static U32 LZ4_read32(const void* ptr) { return ((const LZ4_unalign32*)ptr)->u32; } +static reg_t LZ4_read_ARCH(const void* ptr) { return ((const LZ4_unalignST*)ptr)->uArch; } + +static void LZ4_write16(void* memPtr, U16 value) { ((LZ4_unalign16*)memPtr)->u16 = value; } +static void LZ4_write32(void* memPtr, U32 value) { ((LZ4_unalign32*)memPtr)->u32 = value; } + +#else /* safe and portable access using memcpy() */ + +static U16 LZ4_read16(const void* memPtr) +{ + U16 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; +} + +static U32 LZ4_read32(const void* memPtr) +{ + U32 val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; +} + +static reg_t LZ4_read_ARCH(const void* memPtr) +{ + reg_t val; LZ4_memcpy(&val, memPtr, sizeof(val)); return val; +} + +static void LZ4_write16(void* memPtr, U16 value) +{ + LZ4_memcpy(memPtr, &value, sizeof(value)); +} + +static void LZ4_write32(void* memPtr, U32 value) { - U16 val16; - memcpy(&val16, memPtr, 2); - return val16; + LZ4_memcpy(memPtr, &value, sizeof(value)); } -static U16 LZ4_readLE16(const void *memPtr) +#endif /* LZ4_FORCE_MEMORY_ACCESS */ + + +static U16 LZ4_readLE16(const void* memPtr) { if (LZ4_isLittleEndian()) { return LZ4_read16(memPtr); } else { - const BYTE *p = (const BYTE *)memPtr; - return (U16)((U16)p[0] + (p[1] << 8)); + const BYTE* p = (const BYTE*)memPtr; + return (U16)((U16)p[0] + (p[1]<<8)); } } -static void LZ4_writeLE16(void *memPtr, U16 value) +#ifdef LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT +static U32 LZ4_readLE32(const void* memPtr) { if (LZ4_isLittleEndian()) { - memcpy(memPtr, &value, 2); + return LZ4_read32(memPtr); } else { - BYTE *p = (BYTE *)memPtr; - p[0] = (BYTE)value; - p[1] = (BYTE)(value >> 8); + const BYTE* p = (const BYTE*)memPtr; + return (U32)p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24); } } +#endif -static U32 LZ4_read32(const void *memPtr) +static void LZ4_writeLE16(void* memPtr, U16 value) { - U32 val32; - memcpy(&val32, memPtr, 4); - return val32; + if (LZ4_isLittleEndian()) { + LZ4_write16(memPtr, value); + } else { + BYTE* p = (BYTE*)memPtr; + p[0] = (BYTE) value; + p[1] = (BYTE)(value>>8); + } } -static U64 LZ4_read64(const void *memPtr) +/* customized variant of memcpy, which can overwrite up to 8 bytes beyond dstEnd */ +LZ4_FORCE_INLINE +void LZ4_wildCopy8(void* dstPtr, const void* srcPtr, void* dstEnd) { - U64 val64; - memcpy(&val64, memPtr, 8); - return val64; -} + BYTE* d = (BYTE*)dstPtr; + const BYTE* s = (const BYTE*)srcPtr; + BYTE* const e = (BYTE*)dstEnd; -static size_t LZ4_read_ARCH(const void *p) -{ - if (LZ4_64bits()) - return (size_t)LZ4_read64(p); - else - return (size_t)LZ4_read32(p); + do { LZ4_memcpy(d,s,8); d+=8; s+=8; } while (d= 16. */ +LZ4_FORCE_INLINE void +LZ4_wildCopy32(void* dstPtr, const void* srcPtr, void* dstEnd) { - BYTE *d = (BYTE *)dstPtr; - const BYTE *s = (const BYTE *)srcPtr; - BYTE *e = (BYTE *)dstEnd; - do { - LZ4_copy8(d, s); - d += 8; - s += 8; - } while (d < e); + BYTE* d = (BYTE*)dstPtr; + const BYTE* s = (const BYTE*)srcPtr; + BYTE* const e = (BYTE*)dstEnd; + + do { LZ4_memcpy(d,s,16); LZ4_memcpy(d+16,s+16,16); d+=32; s+=32; } while (d= dstPtr + MINMATCH + * - there is at least 8 bytes available to write after dstEnd */ +LZ4_FORCE_INLINE void +LZ4_memcpy_using_offset(BYTE* dstPtr, const BYTE* srcPtr, BYTE* dstEnd, const size_t offset) +{ + BYTE v[8]; -#define COPYLENGTH 8 -#define LASTLITERALS 5 -#define MFLIMIT (COPYLENGTH + MINMATCH) -static const int LZ4_minLength = (MFLIMIT + 1); + assert(dstEnd >= dstPtr + MINMATCH); -#define KB *(1 << 10) -#define MB *(1 << 20) -#define GB *(1U << 30) + switch(offset) { + case 1: + MEM_INIT(v, *srcPtr, 8); + break; + case 2: + LZ4_memcpy(v, srcPtr, 2); + LZ4_memcpy(&v[2], srcPtr, 2); +#if defined(_MSC_VER) && (_MSC_VER <= 1937) /* MSVC 2022 ver 17.7 or earlier */ +# pragma warning(push) +# pragma warning(disable : 6385) /* warning C6385: Reading invalid data from 'v'. */ +#endif + LZ4_memcpy(&v[4], v, 4); +#if defined(_MSC_VER) && (_MSC_VER <= 1937) /* MSVC 2022 ver 17.7 or earlier */ +# pragma warning(pop) +#endif + break; + case 4: + LZ4_memcpy(v, srcPtr, 4); + LZ4_memcpy(&v[4], srcPtr, 4); + break; + default: + LZ4_memcpy_using_offset_base(dstPtr, srcPtr, dstEnd, offset); + return; + } -#define MAXD_LOG 16 -#ifdef MAX_DISTANCE -#undef MAX_DISTANCE + LZ4_memcpy(dstPtr, v, 8); + dstPtr += 8; + while (dstPtr < dstEnd) { + LZ4_memcpy(dstPtr, v, 8); + dstPtr += 8; + } +} #endif -#define MAX_DISTANCE ((1 << MAXD_LOG) - 1) - -#define ML_BITS 4 -#define ML_MASK ((1U << ML_BITS) - 1) -#define RUN_BITS (8 - ML_BITS) -#define RUN_MASK ((1U << RUN_BITS) - 1) - -/************************************** - * Common Utils - **************************************/ -#define LZ4_STATIC_ASSERT(c) \ - { \ - enum \ - { \ - LZ4_static_assert = 1 / (int)(!!(c)) \ - }; \ - } /* use only *after* variable declarations */ - -/************************************** - * Common functions - **************************************/ -static unsigned LZ4_NbCommonBytes(size_t val) + + +/*-************************************ +* Common functions +**************************************/ +static unsigned LZ4_NbCommonBytes (reg_t val) { + assert(val != 0); if (LZ4_isLittleEndian()) { - if (LZ4_64bits()) { -#if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) + if (sizeof(val) == 8) { +# if defined(_MSC_VER) && (_MSC_VER >= 1800) && (defined(_M_AMD64) && !defined(_M_ARM64EC)) && !defined(LZ4_FORCE_SW_BITCOUNT) +/*-************************************************************************************************* +* ARM64EC is a Microsoft-designed ARM64 ABI compatible with AMD64 applications on ARM64 Windows 11. +* The ARM64EC ABI does not support AVX/AVX2/AVX512 instructions, nor their relevant intrinsics +* including _tzcnt_u64. Therefore, we need to neuter the _tzcnt_u64 code path for ARM64EC. +****************************************************************************************************/ +# if defined(__clang__) && (__clang_major__ < 10) + /* Avoid undefined clang-cl intrinsics issue. + * See https://github.com/lz4/lz4/pull/1017 for details. */ + return (unsigned)__builtin_ia32_tzcnt_u64(val) >> 3; +# else + /* x64 CPUS without BMI support interpret `TZCNT` as `REP BSF` */ + return (unsigned)_tzcnt_u64(val) >> 3; +# endif +# elif defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r = 0; _BitScanForward64(&r, (U64)val); - return (int)(r >> 3); -#elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (__builtin_ctzll((U64)val) >> 3); -#else - static const int DeBruijnBytePos[64] = {0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, - 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, - 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7}; - return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; -#endif - } else /* 32 bits */ - { -#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (unsigned)r >> 3; +# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) + return (unsigned)__builtin_ctzll((U64)val) >> 3; +# else + const U64 m = 0x0101010101010101ULL; + val ^= val - 1; + return (unsigned)(((U64)((val & (m - 1)) * m)) >> 56); +# endif + } else /* 32 bits */ { +# if defined(_MSC_VER) && (_MSC_VER >= 1400) && !defined(LZ4_FORCE_SW_BITCOUNT) unsigned long r; _BitScanForward(&r, (U32)val); - return (int)(r >> 3); -#elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (__builtin_ctz((U32)val) >> 3); -#else - static const int DeBruijnBytePos[32] = {0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, - 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1}; - return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; -#endif + return (unsigned)r >> 3; +# elif (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (unsigned)__builtin_ctz((U32)val) >> 3; +# else + const U32 m = 0x01010101; + return (unsigned)((((val - 1) ^ val) & (m - 1)) * m) >> 24; +# endif } - } else /* Big Endian CPU */ - { - if (LZ4_64bits()) { -#if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r = 0; - _BitScanReverse64(&r, val); - return (unsigned)(r >> 3); -#elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (__builtin_clzll((U64)val) >> 3); + } else /* Big Endian CPU */ { + if (sizeof(val)==8) { +# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(__TINYC__) && !defined(LZ4_FORCE_SW_BITCOUNT) + return (unsigned)__builtin_clzll((U64)val) >> 3; +# else +#if 1 + /* this method is probably faster, + * but adds a 128 bytes lookup table */ + static const unsigned char ctz7_tab[128] = { + 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, + }; + U64 const mask = 0x0101010101010101ULL; + U64 const t = (((val >> 8) - mask) | val) & mask; + return ctz7_tab[(t * 0x0080402010080402ULL) >> 57]; #else + /* this method doesn't consume memory space like the previous one, + * but it contains several branches, + * that may end up slowing execution */ + static const U32 by32 = sizeof(val)*4; /* 32 on 64 bits (goal), 16 on 32 bits. + Just to avoid some static analyzer complaining about shift by 32 on 32-bits target. + Note that this code path is never triggered in 32-bits mode. */ unsigned r; - if (!(val >> 32)) { - r = 4; - } else { - r = 0; - val >>= 32; - } - if (!(val >> 16)) { - r += 2; - val >>= 8; - } else { - val >>= 24; - } - r += (!val); - return r; -#endif - } else /* 32 bits */ - { -#if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT) - unsigned long r = 0; - _BitScanReverse(&r, (unsigned long)val); - return (unsigned)(r >> 3); -#elif (defined(__clang__) || (LZ4_GCC_VERSION >= 304)) && !defined(LZ4_FORCE_SW_BITCOUNT) - return (__builtin_clz((U32)val) >> 3); -#else - unsigned r; - if (!(val >> 16)) { - r = 2; - val >>= 8; - } else { - r = 0; - val >>= 24; - } + if (!(val>>by32)) { r=4; } else { r=0; val>>=by32; } + if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } r += (!val); return r; #endif +# endif + } else /* 32 bits */ { +# if (defined(__clang__) || (defined(__GNUC__) && ((__GNUC__ > 3) || \ + ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))))) && \ + !defined(LZ4_FORCE_SW_BITCOUNT) + return (unsigned)__builtin_clz((U32)val) >> 3; +# else + val >>= 8; + val = ((((val + 0x00FFFF00) | 0x00FFFFFF) + val) | + (val + 0x00FF0000)) >> 24; + return (unsigned)val ^ 3; +# endif } } } -static unsigned LZ4_count(const BYTE *pIn, const BYTE *pMatch, const BYTE *pInLimit) + +#define STEPSIZE sizeof(reg_t) +LZ4_FORCE_INLINE +unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) { - const BYTE *const pStart = pIn; + const BYTE* const pStart = pIn; - while (likely(pIn < pInLimit - (STEPSIZE - 1))) { - size_t diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); + if (likely(pIn < pInLimit-(STEPSIZE-1))) { + reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); if (!diff) { - pIn += STEPSIZE; - pMatch += STEPSIZE; - continue; - } + pIn+=STEPSIZE; pMatch+=STEPSIZE; + } else { + return LZ4_NbCommonBytes(diff); + } } + + while (likely(pIn < pInLimit-(STEPSIZE-1))) { + reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn); + if (!diff) { pIn+=STEPSIZE; pMatch+=STEPSIZE; continue; } pIn += LZ4_NbCommonBytes(diff); return (unsigned)(pIn - pStart); } - if (LZ4_64bits()) - if ((pIn < (pInLimit - 3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { - pIn += 4; - pMatch += 4; - } - if ((pIn < (pInLimit - 1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { - pIn += 2; - pMatch += 2; - } - if ((pIn < pInLimit) && (*pMatch == *pIn)) - pIn++; + if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) { pIn+=4; pMatch+=4; } + if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) { pIn+=2; pMatch+=2; } + if ((pIn compression run slower on incompressible data */ - -/************************************** - * Local Structures and types - **************************************/ -typedef struct -{ - U32 hashTable[HASH_SIZE_U32]; - U32 currentOffset; - U32 initCheck; - const BYTE *dictionary; - BYTE *bufferStart; /* obsolete, used for slideInputBuffer */ - U32 dictSize; -} LZ4_stream_t_internal; - -typedef enum -{ - notLimited = 0, - limitedOutput = 1 -} limitedOutput_directive; -typedef enum -{ - byPtr, - byU32, - byU16 -} tableType_t; +/*-************************************ +* Local Constants +**************************************/ +static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1)); +static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */ + + +/*-************************************ +* Local Structures and types +**************************************/ +typedef enum { clearedTable = 0, byPtr, byU32, byU16 } tableType_t; + +/** + * This enum distinguishes several different modes of accessing previous + * content in the stream. + * + * - noDict : There is no preceding content. + * - withPrefix64k : Table entries up to ctx->dictSize before the current blob + * blob being compressed are valid and refer to the preceding + * content (of length ctx->dictSize), which is available + * contiguously preceding in memory the content currently + * being compressed. + * - usingExtDict : Like withPrefix64k, but the preceding content is somewhere + * else in memory, starting at ctx->dictionary with length + * ctx->dictSize. + * - usingDictCtx : Everything concerning the preceding content is + * in a separate context, pointed to by ctx->dictCtx. + * ctx->dictionary, ctx->dictSize, and table entries + * in the current context that refer to positions + * preceding the beginning of the current compression are + * ignored. Instead, ctx->dictCtx->dictionary and ctx->dictCtx + * ->dictSize describe the location and size of the preceding + * content, and matches are found by looking in the ctx + * ->dictCtx->hashTable. + */ +typedef enum { noDict = 0, withPrefix64k, usingExtDict, usingDictCtx } dict_directive; +typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive; -typedef enum -{ - noDict = 0, - withPrefix64k, - usingExtDict -} dict_directive; -typedef enum -{ - noDictIssue = 0, - dictSmall -} dictIssue_directive; -typedef enum -{ - endOnOutputSize = 0, - endOnInputSize = 1 -} endCondition_directive; -typedef enum -{ - full = 0, - partial = 1 -} earlyEnd_directive; +/*-************************************ +* Local Utils +**************************************/ +int LZ4_versionNumber (void) { return LZ4_VERSION_NUMBER; } +const char* LZ4_versionString(void) { return LZ4_VERSION_STRING; } +int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } +int LZ4_sizeofState(void) { return sizeof(LZ4_stream_t); } -/************************************** - * Local Utils - **************************************/ -int LZ4_versionNumber(void) { return LZ4_VERSION_NUMBER; } -int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); } -int LZ4_sizeofState() { return LZ4_STREAMSIZE; } -/******************************** - * Compression functions - ********************************/ +/*-**************************************** +* Internal Definitions, used only in Tests +*******************************************/ +#if defined (__cplusplus) +extern "C" { +#endif + +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize); -static U32 LZ4_hashSequence(U32 sequence, tableType_t const tableType) +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, + int compressedSize, int maxOutputSize, + const void* dictStart, size_t dictSize); +int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest, + int compressedSize, int targetOutputSize, int dstCapacity, + const void* dictStart, size_t dictSize); +#if defined (__cplusplus) +} +#endif + +/*-****************************** +* Compression functions +********************************/ +LZ4_FORCE_INLINE U32 LZ4_hash4(U32 sequence, tableType_t const tableType) { if (tableType == byU16) - return (((sequence)*2654435761U) >> ((MINMATCH * 8) - (LZ4_HASHLOG + 1))); + return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1))); else - return (((sequence)*2654435761U) >> ((MINMATCH * 8) - LZ4_HASHLOG)); + return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG)); } -static const U64 prime5bytes = 889523592379ULL; -static U32 LZ4_hashSequence64(size_t sequence, tableType_t const tableType) +LZ4_FORCE_INLINE U32 LZ4_hash5(U64 sequence, tableType_t const tableType) { - const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG + 1 : LZ4_HASHLOG; - const U32 hashMask = (1 << hashLog) - 1; - return ((sequence * prime5bytes) >> (40 - hashLog)) & hashMask; + const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG; + if (LZ4_isLittleEndian()) { + const U64 prime5bytes = 889523592379ULL; + return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog)); + } else { + const U64 prime8bytes = 11400714785074694791ULL; + return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog)); + } } -static U32 LZ4_hashSequenceT(size_t sequence, tableType_t const tableType) +LZ4_FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType) { - if (LZ4_64bits()) - return LZ4_hashSequence64(sequence, tableType); - return LZ4_hashSequence((U32)sequence, tableType); + if ((sizeof(reg_t)==8) && (tableType != byU16)) return LZ4_hash5(LZ4_read_ARCH(p), tableType); + +#ifdef LZ4_STATIC_LINKING_ONLY_ENDIANNESS_INDEPENDENT_OUTPUT + return LZ4_hash4(LZ4_readLE32(p), tableType); +#else + return LZ4_hash4(LZ4_read32(p), tableType); +#endif } -static U32 LZ4_hashPosition(const void *p, tableType_t tableType) +LZ4_FORCE_INLINE void LZ4_clearHash(U32 h, void* tableBase, tableType_t const tableType) { - return LZ4_hashSequenceT(LZ4_read_ARCH(p), tableType); + switch (tableType) + { + default: /* fallthrough */ + case clearedTable: { /* illegal! */ assert(0); return; } + case byPtr: { const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = NULL; return; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = 0; return; } + case byU16: { U16* hashTable = (U16*) tableBase; hashTable[h] = 0; return; } + } } -static void LZ4_putPositionOnHash(const BYTE *p, U32 h, void *tableBase, tableType_t const tableType, - const BYTE *srcBase) +LZ4_FORCE_INLINE void LZ4_putIndexOnHash(U32 idx, U32 h, void* tableBase, tableType_t const tableType) { - switch (tableType) { - case byPtr: { - const BYTE **hashTable = (const BYTE **)tableBase; - hashTable[h] = p; - return; - } - case byU32: { - U32 *hashTable = (U32 *)tableBase; - hashTable[h] = (U32)(p - srcBase); - return; - } - case byU16: { - U16 *hashTable = (U16 *)tableBase; - hashTable[h] = (U16)(p - srcBase); - return; - } + switch (tableType) + { + default: /* fallthrough */ + case clearedTable: /* fallthrough */ + case byPtr: { /* illegal! */ assert(0); return; } + case byU32: { U32* hashTable = (U32*) tableBase; hashTable[h] = idx; return; } + case byU16: { U16* hashTable = (U16*) tableBase; assert(idx < 65536); hashTable[h] = (U16)idx; return; } } } -static void LZ4_putPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase) +/* LZ4_putPosition*() : only used in byPtr mode */ +LZ4_FORCE_INLINE void LZ4_putPositionOnHash(const BYTE* p, U32 h, + void* tableBase, tableType_t const tableType) { - U32 h = LZ4_hashPosition(p, tableType); - LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase); + const BYTE** const hashTable = (const BYTE**)tableBase; + assert(tableType == byPtr); (void)tableType; + hashTable[h] = p; } -static const BYTE *LZ4_getPositionOnHash(U32 h, void *tableBase, tableType_t tableType, const BYTE *srcBase) +LZ4_FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType) { - if (tableType == byPtr) { - const BYTE **hashTable = (const BYTE **)tableBase; + U32 const h = LZ4_hashPosition(p, tableType); + LZ4_putPositionOnHash(p, h, tableBase, tableType); +} + +/* LZ4_getIndexOnHash() : + * Index of match position registered in hash table. + * hash position must be calculated by using base+index, or dictBase+index. + * Assumption 1 : only valid if tableType == byU32 or byU16. + * Assumption 2 : h is presumed valid (within limits of hash table) + */ +LZ4_FORCE_INLINE U32 LZ4_getIndexOnHash(U32 h, const void* tableBase, tableType_t tableType) +{ + LZ4_STATIC_ASSERT(LZ4_MEMORY_USAGE > 2); + if (tableType == byU32) { + const U32* const hashTable = (const U32*) tableBase; + assert(h < (1U << (LZ4_MEMORY_USAGE-2))); return hashTable[h]; } - if (tableType == byU32) { - U32 *hashTable = (U32 *)tableBase; - return hashTable[h] + srcBase; + if (tableType == byU16) { + const U16* const hashTable = (const U16*) tableBase; + assert(h < (1U << (LZ4_MEMORY_USAGE-1))); + return hashTable[h]; } - { - U16 *hashTable = (U16 *)tableBase; - return hashTable[h] + srcBase; - } /* default, to ensure a return */ + assert(0); return 0; /* forbidden case */ } -static const BYTE *LZ4_getPosition(const BYTE *p, void *tableBase, tableType_t tableType, const BYTE *srcBase) +static const BYTE* LZ4_getPositionOnHash(U32 h, const void* tableBase, tableType_t tableType) { - U32 h = LZ4_hashPosition(p, tableType); - return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase); + assert(tableType == byPtr); (void)tableType; + { const BYTE* const* hashTable = (const BYTE* const*) tableBase; return hashTable[h]; } } -FORCE_INLINE int LZ4_compress_generic(void *const ctx, const char *const source, char *const dest, const int inputSize, - const int maxOutputSize, const limitedOutput_directive outputLimited, - const tableType_t tableType, const dict_directive dict, - const dictIssue_directive dictIssue, const U32 acceleration) +LZ4_FORCE_INLINE const BYTE* +LZ4_getPosition(const BYTE* p, + const void* tableBase, tableType_t tableType) { - LZ4_stream_t_internal *const dictPtr = (LZ4_stream_t_internal *)ctx; + U32 const h = LZ4_hashPosition(p, tableType); + return LZ4_getPositionOnHash(h, tableBase, tableType); +} + +LZ4_FORCE_INLINE void +LZ4_prepareTable(LZ4_stream_t_internal* const cctx, + const int inputSize, + const tableType_t tableType) { + /* If the table hasn't been used, it's guaranteed to be zeroed out, and is + * therefore safe to use no matter what mode we're in. Otherwise, we figure + * out if it's safe to leave as is or whether it needs to be reset. + */ + if ((tableType_t)cctx->tableType != clearedTable) { + assert(inputSize >= 0); + if ((tableType_t)cctx->tableType != tableType + || ((tableType == byU16) && cctx->currentOffset + (unsigned)inputSize >= 0xFFFFU) + || ((tableType == byU32) && cctx->currentOffset > 1 GB) + || tableType == byPtr + || inputSize >= 4 KB) + { + DEBUGLOG(4, "LZ4_prepareTable: Resetting table in %p", cctx); + MEM_INIT(cctx->hashTable, 0, LZ4_HASHTABLESIZE); + cctx->currentOffset = 0; + cctx->tableType = (U32)clearedTable; + } else { + DEBUGLOG(4, "LZ4_prepareTable: Re-use hash table (no reset)"); + } + } - const BYTE *ip = (const BYTE *)source; - const BYTE *base; - const BYTE *lowLimit; - const BYTE *const lowRefLimit = ip - dictPtr->dictSize; - const BYTE *const dictionary = dictPtr->dictionary; - const BYTE *const dictEnd = dictionary + dictPtr->dictSize; - const size_t dictDelta = dictEnd - (const BYTE *)source; - const BYTE *anchor = (const BYTE *)source; - const BYTE *const iend = ip + inputSize; - const BYTE *const mflimit = iend - MFLIMIT; - const BYTE *const matchlimit = iend - LASTLITERALS; + /* Adding a gap, so all previous entries are > LZ4_DISTANCE_MAX back, + * is faster than compressing without a gap. + * However, compressing with currentOffset == 0 is faster still, + * so we preserve that case. + */ + if (cctx->currentOffset != 0 && tableType == byU32) { + DEBUGLOG(5, "LZ4_prepareTable: adding 64KB to currentOffset"); + cctx->currentOffset += 64 KB; + } - BYTE *op = (BYTE *)dest; - BYTE *const olimit = op + maxOutputSize; + /* Finally, clear history */ + cctx->dictCtx = NULL; + cctx->dictionary = NULL; + cctx->dictSize = 0; +} +/** LZ4_compress_generic_validated() : + * inlined, to ensure branches are decided at compilation time. + * The following conditions are presumed already validated: + * - source != NULL + * - inputSize > 0 + */ +LZ4_FORCE_INLINE int LZ4_compress_generic_validated( + LZ4_stream_t_internal* const cctx, + const char* const source, + char* const dest, + const int inputSize, + int* inputConsumed, /* only written when outputDirective == fillOutput */ + const int maxOutputSize, + const limitedOutput_directive outputDirective, + const tableType_t tableType, + const dict_directive dictDirective, + const dictIssue_directive dictIssue, + const int acceleration) +{ + int result; + const BYTE* ip = (const BYTE*)source; + + U32 const startIndex = cctx->currentOffset; + const BYTE* base = (const BYTE*)source - startIndex; + const BYTE* lowLimit; + + const LZ4_stream_t_internal* dictCtx = (const LZ4_stream_t_internal*) cctx->dictCtx; + const BYTE* const dictionary = + dictDirective == usingDictCtx ? dictCtx->dictionary : cctx->dictionary; + const U32 dictSize = + dictDirective == usingDictCtx ? dictCtx->dictSize : cctx->dictSize; + const U32 dictDelta = + (dictDirective == usingDictCtx) ? startIndex - dictCtx->currentOffset : 0; /* make indexes in dictCtx comparable with indexes in current context */ + + int const maybe_extMem = (dictDirective == usingExtDict) || (dictDirective == usingDictCtx); + U32 const prefixIdxLimit = startIndex - dictSize; /* used when dictDirective == dictSmall */ + const BYTE* const dictEnd = dictionary ? dictionary + dictSize : dictionary; + const BYTE* anchor = (const BYTE*) source; + const BYTE* const iend = ip + inputSize; + const BYTE* const mflimitPlusOne = iend - MFLIMIT + 1; + const BYTE* const matchlimit = iend - LASTLITERALS; + + /* the dictCtx currentOffset is indexed on the start of the dictionary, + * while a dictionary in the current context precedes the currentOffset */ + const BYTE* dictBase = (dictionary == NULL) ? NULL : + (dictDirective == usingDictCtx) ? + dictionary + dictSize - dictCtx->currentOffset : + dictionary + dictSize - startIndex; + + BYTE* op = (BYTE*) dest; + BYTE* const olimit = op + maxOutputSize; + + U32 offset = 0; U32 forwardH; - size_t refDelta = 0; - /* Init conditions */ - if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) - return 0; /* Unsupported input size, too large (or negative) */ - switch (dict) { - case noDict: - default: - base = (const BYTE *)source; - lowLimit = (const BYTE *)source; - break; - case withPrefix64k: - base = (const BYTE *)source - dictPtr->currentOffset; - lowLimit = (const BYTE *)source - dictPtr->dictSize; - break; - case usingExtDict: - base = (const BYTE *)source - dictPtr->currentOffset; - lowLimit = (const BYTE *)source; - break; + DEBUGLOG(5, "LZ4_compress_generic_validated: srcSize=%i, tableType=%u", inputSize, tableType); + assert(ip != NULL); + if (tableType == byU16) assert(inputSize= 1); + + lowLimit = (const BYTE*)source - (dictDirective == withPrefix64k ? dictSize : 0); + + /* Update context state */ + if (dictDirective == usingDictCtx) { + /* Subsequent linked blocks can't use the dictionary. */ + /* Instead, they use the block we just compressed. */ + cctx->dictCtx = NULL; + cctx->dictSize = (U32)inputSize; + } else { + cctx->dictSize += (U32)inputSize; } - if ((tableType == byU16) && (inputSize >= LZ4_64Klimit)) - return 0; /* Size too large (not within 64K limit) */ - if (inputSize < LZ4_minLength) - goto _last_literals; /* Input too small, no compression (all literals) */ + cctx->currentOffset += (U32)inputSize; + cctx->tableType = (U32)tableType; + + if (inputSizehashTable, byPtr); + } else { + LZ4_putIndexOnHash(startIndex, h, cctx->hashTable, tableType); + } } + ip++; forwardH = LZ4_hashPosition(ip, tableType); /* Main Loop */ - for (;;) { - const BYTE *match; - BYTE *token; - { - const BYTE *forwardIp = ip; - unsigned step = 1; - unsigned searchMatchNb = acceleration << LZ4_skipTrigger; + for ( ; ; ) { + const BYTE* match; + BYTE* token; + const BYTE* filledIp; + + /* Find a match */ + if (tableType == byPtr) { + const BYTE* forwardIp = ip; + int step = 1; + int searchMatchNb = acceleration << LZ4_skipTrigger; + do { + U32 const h = forwardH; + ip = forwardIp; + forwardIp += step; + step = (searchMatchNb++ >> LZ4_skipTrigger); + + if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; + assert(ip < mflimitPlusOne); + + match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType); + forwardH = LZ4_hashPosition(forwardIp, tableType); + LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType); + + } while ( (match+LZ4_DISTANCE_MAX < ip) + || (LZ4_read32(match) != LZ4_read32(ip)) ); + + } else { /* byU32, byU16 */ - /* Find a match */ + const BYTE* forwardIp = ip; + int step = 1; + int searchMatchNb = acceleration << LZ4_skipTrigger; do { - U32 h = forwardH; + U32 const h = forwardH; + U32 const current = (U32)(forwardIp - base); + U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); + assert(matchIndex <= current); + assert(forwardIp - base < (ptrdiff_t)(2 GB - 1)); ip = forwardIp; forwardIp += step; step = (searchMatchNb++ >> LZ4_skipTrigger); - if (unlikely(forwardIp > mflimit)) - goto _last_literals; + if (unlikely(forwardIp > mflimitPlusOne)) goto _last_literals; + assert(ip < mflimitPlusOne); - match = LZ4_getPositionOnHash(h, ctx, tableType, base); - if (dict == usingExtDict) { - if (match < (const BYTE *)source) { - refDelta = dictDelta; + if (dictDirective == usingDictCtx) { + if (matchIndex < startIndex) { + /* there was no match, try the dictionary */ + assert(tableType == byU32); + matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); + match = dictBase + matchIndex; + matchIndex += dictDelta; /* make dictCtx index comparable with current context */ lowLimit = dictionary; } else { - refDelta = 0; - lowLimit = (const BYTE *)source; + match = base + matchIndex; + lowLimit = (const BYTE*)source; } + } else if (dictDirective == usingExtDict) { + if (matchIndex < startIndex) { + DEBUGLOG(7, "extDict candidate: matchIndex=%5u < startIndex=%5u", matchIndex, startIndex); + assert(startIndex - matchIndex >= MINMATCH); + assert(dictBase); + match = dictBase + matchIndex; + lowLimit = dictionary; + } else { + match = base + matchIndex; + lowLimit = (const BYTE*)source; + } + } else { /* single continuous memory segment */ + match = base + matchIndex; } forwardH = LZ4_hashPosition(forwardIp, tableType); - LZ4_putPositionOnHash(ip, h, ctx, tableType, base); + LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); + + DEBUGLOG(7, "candidate at pos=%u (offset=%u \n", matchIndex, current - matchIndex); + if ((dictIssue == dictSmall) && (matchIndex < prefixIdxLimit)) { continue; } /* match outside of valid area */ + assert(matchIndex < current); + if ( ((tableType != byU16) || (LZ4_DISTANCE_MAX < LZ4_DISTANCE_ABSOLUTE_MAX)) + && (matchIndex+LZ4_DISTANCE_MAX < current)) { + continue; + } /* too far */ + assert((current - matchIndex) <= LZ4_DISTANCE_MAX); /* match now expected within distance */ + + if (LZ4_read32(match) == LZ4_read32(ip)) { + if (maybe_extMem) offset = current - matchIndex; + break; /* match found */ + } - } while (((dictIssue == dictSmall) ? (match < lowRefLimit) : 0) || - ((tableType == byU16) ? 0 : (match + MAX_DISTANCE < ip)) || - (LZ4_read32(match + refDelta) != LZ4_read32(ip))); + } while(1); } /* Catch up */ - while ((ip > anchor) && (match + refDelta > lowLimit) && (unlikely(ip[-1] == match[refDelta - 1]))) { - ip--; - match--; + filledIp = ip; + assert(ip > anchor); /* this is always true as ip has been advanced before entering the main loop */ + if ((match > lowLimit) && unlikely(ip[-1] == match[-1])) { + do { ip--; match--; } while (((ip > anchor) & (match > lowLimit)) && (unlikely(ip[-1] == match[-1]))); } - { - /* Encode Literal length */ - unsigned litLength = (unsigned)(ip - anchor); + /* Encode Literals */ + { unsigned const litLength = (unsigned)(ip - anchor); token = op++; - if ((outputLimited) && (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength / 255) > olimit))) - return 0; /* Check output limit */ + if ((outputDirective == limitedOutput) && /* Check output buffer overflow */ + (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit)) ) { + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ + } + if ((outputDirective == fillOutput) && + (unlikely(op + (litLength+240)/255 /* litlen */ + litLength /* literals */ + 2 /* offset */ + 1 /* token */ + MFLIMIT - MINMATCH /* min last literals so last match is <= end - MFLIMIT */ > olimit))) { + op--; + goto _last_literals; + } if (litLength >= RUN_MASK) { - int len = (int)litLength - RUN_MASK; - *token = (RUN_MASK << ML_BITS); - for (; len >= 255; len -= 255) - *op++ = 255; + int len = (int)(litLength - RUN_MASK); + *token = (RUN_MASK<= 255 ; len-=255) *op++ = 255; *op++ = (BYTE)len; - } else - *token = (BYTE)(litLength << ML_BITS); + } + else *token = (BYTE)(litLength< olimit)) { + /* the match was too close to the end, rewind and go to last literals */ + op = token; + goto _last_literals; } - _next_match: /* Encode Offset */ - LZ4_writeLE16(op, (U16)(ip - match)); - op += 2; + if (maybe_extMem) { /* static test */ + DEBUGLOG(6, " with offset=%u (ext if > %i)", offset, (int)(ip - (const BYTE*)source)); + assert(offset <= LZ4_DISTANCE_MAX && offset > 0); + LZ4_writeLE16(op, (U16)offset); op+=2; + } else { + DEBUGLOG(6, " with offset=%u (same segment)", (U32)(ip - match)); + assert(ip-match <= LZ4_DISTANCE_MAX); + LZ4_writeLE16(op, (U16)(ip - match)); op+=2; + } /* Encode MatchLength */ - { - unsigned matchLength; - - if ((dict == usingExtDict) && (lowLimit == dictionary)) { - const BYTE *limit; - match += refDelta; - limit = ip + (dictEnd - match); - if (limit > matchlimit) - limit = matchlimit; - matchLength = LZ4_count(ip + MINMATCH, match + MINMATCH, limit); - ip += MINMATCH + matchLength; - if (ip == limit) { - unsigned more = LZ4_count(ip, (const BYTE *)source, matchlimit); - matchLength += more; + { unsigned matchCode; + + if ( (dictDirective==usingExtDict || dictDirective==usingDictCtx) + && (lowLimit==dictionary) /* match within extDict */ ) { + const BYTE* limit = ip + (dictEnd-match); + assert(dictEnd > match); + if (limit > matchlimit) limit = matchlimit; + matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit); + ip += (size_t)matchCode + MINMATCH; + if (ip==limit) { + unsigned const more = LZ4_count(limit, (const BYTE*)source, matchlimit); + matchCode += more; ip += more; } + DEBUGLOG(6, " with matchLength=%u starting in extDict", matchCode+MINMATCH); } else { - matchLength = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit); - ip += MINMATCH + matchLength; + matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit); + ip += (size_t)matchCode + MINMATCH; + DEBUGLOG(6, " with matchLength=%u", matchCode+MINMATCH); } - if ((outputLimited) && (unlikely(op + (1 + LASTLITERALS) + (matchLength >> 8) > olimit))) - return 0; /* Check output limit */ - if (matchLength >= ML_MASK) { - *token += ML_MASK; - matchLength -= ML_MASK; - for (; matchLength >= 510; matchLength -= 510) { - *op++ = 255; - *op++ = 255; + if ((outputDirective) && /* Check output buffer overflow */ + (unlikely(op + (1 + LASTLITERALS) + (matchCode+240)/255 > olimit)) ) { + if (outputDirective == fillOutput) { + /* Match description too long : reduce it */ + U32 newMatchCode = 15 /* in token */ - 1 /* to avoid needing a zero byte */ + ((U32)(olimit - op) - 1 - LASTLITERALS) * 255; + ip -= matchCode - newMatchCode; + assert(newMatchCode < matchCode); + matchCode = newMatchCode; + if (unlikely(ip <= filledIp)) { + /* We have already filled up to filledIp so if ip ends up less than filledIp + * we have positions in the hash table beyond the current position. This is + * a problem if we reuse the hash table. So we have to remove these positions + * from the hash table. + */ + const BYTE* ptr; + DEBUGLOG(5, "Clearing %u positions", (U32)(filledIp - ip)); + for (ptr = ip; ptr <= filledIp; ++ptr) { + U32 const h = LZ4_hashPosition(ptr, tableType); + LZ4_clearHash(h, cctx->hashTable, tableType); + } + } + } else { + assert(outputDirective == limitedOutput); + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ } - if (matchLength >= 255) { - matchLength -= 255; - *op++ = 255; + } + if (matchCode >= ML_MASK) { + *token += ML_MASK; + matchCode -= ML_MASK; + LZ4_write32(op, 0xFFFFFFFF); + while (matchCode >= 4*255) { + op+=4; + LZ4_write32(op, 0xFFFFFFFF); + matchCode -= 4*255; } - *op++ = (BYTE)matchLength; + op += matchCode / 255; + *op++ = (BYTE)(matchCode % 255); } else - *token += (BYTE)(matchLength); + *token += (BYTE)(matchCode); } + /* Ensure we have enough space for the last literals. */ + assert(!(outputDirective == fillOutput && op + 1 + LASTLITERALS > olimit)); anchor = ip; /* Test end of chunk */ - if (ip > mflimit) - break; + if (ip >= mflimitPlusOne) break; /* Fill table */ - LZ4_putPosition(ip - 2, ctx, tableType, base); + { U32 const h = LZ4_hashPosition(ip-2, tableType); + if (tableType == byPtr) { + LZ4_putPositionOnHash(ip-2, h, cctx->hashTable, byPtr); + } else { + U32 const idx = (U32)((ip-2) - base); + LZ4_putIndexOnHash(idx, h, cctx->hashTable, tableType); + } } /* Test next position */ - match = LZ4_getPosition(ip, ctx, tableType, base); - if (dict == usingExtDict) { - if (match < (const BYTE *)source) { - refDelta = dictDelta; - lowLimit = dictionary; - } else { - refDelta = 0; - lowLimit = (const BYTE *)source; + if (tableType == byPtr) { + + match = LZ4_getPosition(ip, cctx->hashTable, tableType); + LZ4_putPosition(ip, cctx->hashTable, tableType); + if ( (match+LZ4_DISTANCE_MAX >= ip) + && (LZ4_read32(match) == LZ4_read32(ip)) ) + { token=op++; *token=0; goto _next_match; } + + } else { /* byU32, byU16 */ + + U32 const h = LZ4_hashPosition(ip, tableType); + U32 const current = (U32)(ip-base); + U32 matchIndex = LZ4_getIndexOnHash(h, cctx->hashTable, tableType); + assert(matchIndex < current); + if (dictDirective == usingDictCtx) { + if (matchIndex < startIndex) { + /* there was no match, try the dictionary */ + assert(tableType == byU32); + matchIndex = LZ4_getIndexOnHash(h, dictCtx->hashTable, byU32); + match = dictBase + matchIndex; + lowLimit = dictionary; /* required for match length counter */ + matchIndex += dictDelta; + } else { + match = base + matchIndex; + lowLimit = (const BYTE*)source; /* required for match length counter */ + } + } else if (dictDirective==usingExtDict) { + if (matchIndex < startIndex) { + assert(dictBase); + match = dictBase + matchIndex; + lowLimit = dictionary; /* required for match length counter */ + } else { + match = base + matchIndex; + lowLimit = (const BYTE*)source; /* required for match length counter */ + } + } else { /* single memory segment */ + match = base + matchIndex; + } + LZ4_putIndexOnHash(current, h, cctx->hashTable, tableType); + assert(matchIndex < current); + if ( ((dictIssue==dictSmall) ? (matchIndex >= prefixIdxLimit) : 1) + && (((tableType==byU16) && (LZ4_DISTANCE_MAX == LZ4_DISTANCE_ABSOLUTE_MAX)) ? 1 : (matchIndex+LZ4_DISTANCE_MAX >= current)) + && (LZ4_read32(match) == LZ4_read32(ip)) ) { + token=op++; + *token=0; + if (maybe_extMem) offset = current - matchIndex; + DEBUGLOG(6, "seq.start:%i, literals=%u, match.start:%i", + (int)(anchor-(const BYTE*)source), 0, (int)(ip-(const BYTE*)source)); + goto _next_match; } - } - LZ4_putPosition(ip, ctx, tableType, base); - if (((dictIssue == dictSmall) ? (match >= lowRefLimit) : 1) && (match + MAX_DISTANCE >= ip) && - (LZ4_read32(match + refDelta) == LZ4_read32(ip))) { - token = op++; - *token = 0; - goto _next_match; } /* Prepare next loop */ forwardH = LZ4_hashPosition(++ip, tableType); + } _last_literals: /* Encode Last Literals */ - { - const size_t lastRun = (size_t)(iend - anchor); - if ((outputLimited) && - ((op - (BYTE *)dest) + lastRun + 1 + ((lastRun + 255 - RUN_MASK) / 255) > (U32)maxOutputSize)) - return 0; /* Check output limit */ + { size_t lastRun = (size_t)(iend - anchor); + if ( (outputDirective) && /* Check output buffer overflow */ + (op + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > olimit)) { + if (outputDirective == fillOutput) { + /* adapt lastRun to fill 'dst' */ + assert(olimit >= op); + lastRun = (size_t)(olimit-op) - 1/*token*/; + lastRun -= (lastRun + 256 - RUN_MASK) / 256; /*additional length tokens*/ + } else { + assert(outputDirective == limitedOutput); + return 0; /* cannot compress within `dst` budget. Stored indexes in hash table are nonetheless fine */ + } + } + DEBUGLOG(6, "Final literal run : %i literals", (int)lastRun); if (lastRun >= RUN_MASK) { size_t accumulator = lastRun - RUN_MASK; *op++ = RUN_MASK << ML_BITS; - for (; accumulator >= 255; accumulator -= 255) - *op++ = 255; - *op++ = (BYTE)accumulator; + for(; accumulator >= 255 ; accumulator-=255) *op++ = 255; + *op++ = (BYTE) accumulator; } else { - *op++ = (BYTE)(lastRun << ML_BITS); + *op++ = (BYTE)(lastRun<= LZ4_compressBound(inputSize)) { - if (inputSize < LZ4_64Klimit) - return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, - acceleration); - else - return LZ4_compress_generic(state, source, dest, inputSize, 0, notLimited, LZ4_64bits() ? byU32 : byPtr, - noDict, noDictIssue, acceleration); - } else { - if (inputSize < LZ4_64Klimit) - return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, - noDictIssue, acceleration); - else - return LZ4_compress_generic(state, source, dest, inputSize, maxOutputSize, limitedOutput, - LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); + if (outputDirective == fillOutput) { + *inputConsumed = (int) (((const char*)ip)-source); } -} - -int LZ4_compress_fast(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration) -{ -#if (HEAPMODE) - void *ctxPtr = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ -#else - LZ4_stream_t ctx; - void *ctxPtr = &ctx; -#endif - - int result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration); - -#if (HEAPMODE) - FREEMEM(ctxPtr); -#endif + result = (int)(((char*)op) - dest); + assert(result > 0); + DEBUGLOG(5, "LZ4_compress_generic: compressed %i bytes into %i bytes", inputSize, result); return result; } -int LZ4_compress_default(const char *source, char *dest, int inputSize, int maxOutputSize) -{ - return LZ4_compress_fast(source, dest, inputSize, maxOutputSize, 1); -} - -/* hidden debug function */ -/* strangely enough, gcc generates faster code when this function is uncommented, even if unused */ -int LZ4_compress_fast_force(const char *source, char *dest, int inputSize, int maxOutputSize, int acceleration) -{ - LZ4_stream_t ctx; - - LZ4_resetStream(&ctx); +/** LZ4_compress_generic() : + * inlined, to ensure branches are decided at compilation time; + * takes care of src == (NULL, 0) + * and forward the rest to LZ4_compress_generic_validated */ +LZ4_FORCE_INLINE int LZ4_compress_generic( + LZ4_stream_t_internal* const cctx, + const char* const src, + char* const dst, + const int srcSize, + int *inputConsumed, /* only written when outputDirective == fillOutput */ + const int dstCapacity, + const limitedOutput_directive outputDirective, + const tableType_t tableType, + const dict_directive dictDirective, + const dictIssue_directive dictIssue, + const int acceleration) +{ + DEBUGLOG(5, "LZ4_compress_generic: srcSize=%i, dstCapacity=%i", + srcSize, dstCapacity); + + if ((U32)srcSize > (U32)LZ4_MAX_INPUT_SIZE) { return 0; } /* Unsupported srcSize, too large (or negative) */ + if (srcSize == 0) { /* src == NULL supported if srcSize == 0 */ + if (outputDirective != notLimited && dstCapacity <= 0) return 0; /* no output, can't write anything */ + DEBUGLOG(5, "Generating an empty block"); + assert(outputDirective == notLimited || dstCapacity >= 1); + assert(dst != NULL); + dst[0] = 0; + if (outputDirective == fillOutput) { + assert (inputConsumed != NULL); + *inputConsumed = 0; + } + return 1; + } + assert(src != NULL); - if (inputSize < LZ4_64Klimit) - return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, - noDictIssue, acceleration); - else - return LZ4_compress_generic(&ctx, source, dest, inputSize, maxOutputSize, limitedOutput, - LZ4_64bits() ? byU32 : byPtr, noDict, noDictIssue, acceleration); + return LZ4_compress_generic_validated(cctx, src, dst, srcSize, + inputConsumed, /* only written into if outputDirective == fillOutput */ + dstCapacity, outputDirective, + tableType, dictDirective, dictIssue, acceleration); } -/******************************** - * destSize variant - ********************************/ -static int LZ4_compress_destSize_generic(void *const ctx, const char *const src, char *const dst, int *const srcSizePtr, - const int targetDstSize, const tableType_t tableType) +int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration) { - const BYTE *ip = (const BYTE *)src; - const BYTE *base = (const BYTE *)src; - const BYTE *lowLimit = (const BYTE *)src; - const BYTE *anchor = ip; - const BYTE *const iend = ip + *srcSizePtr; - const BYTE *const mflimit = iend - MFLIMIT; - const BYTE *const matchlimit = iend - LASTLITERALS; - - BYTE *op = (BYTE *)dst; - BYTE *const oend = op + targetDstSize; - BYTE *const oMaxLit = op + targetDstSize - 2 /* offset */ - 8 /* because 8+MINMATCH==MFLIMIT */ - 1 /* token */; - BYTE *const oMaxMatch = op + targetDstSize - (LASTLITERALS + 1 /* token */); - BYTE *const oMaxSeq = oMaxLit - 1 /* token */; - - U32 forwardH; - - /* Init conditions */ - if (targetDstSize < 1) - return 0; /* Impossible to store anything */ - if ((U32)*srcSizePtr > (U32)LZ4_MAX_INPUT_SIZE) - return 0; /* Unsupported input size, too large (or negative) */ - if ((tableType == byU16) && (*srcSizePtr >= LZ4_64Klimit)) - return 0; /* Size too large (not within 64K limit) */ - if (*srcSizePtr < LZ4_minLength) - goto _last_literals; /* Input too small, no compression (all literals) */ - - /* First Byte */ - *srcSizePtr = 0; - LZ4_putPosition(ip, ctx, tableType, base); - ip++; - forwardH = LZ4_hashPosition(ip, tableType); - - /* Main Loop */ - for (;;) { - const BYTE *match; - BYTE *token; - { - const BYTE *forwardIp = ip; - unsigned step = 1; - unsigned searchMatchNb = 1 << LZ4_skipTrigger; - - /* Find a match */ - do { - U32 h = forwardH; - ip = forwardIp; - forwardIp += step; - step = (searchMatchNb++ >> LZ4_skipTrigger); - - if (unlikely(forwardIp > mflimit)) - goto _last_literals; - - match = LZ4_getPositionOnHash(h, ctx, tableType, base); - forwardH = LZ4_hashPosition(forwardIp, tableType); - LZ4_putPositionOnHash(ip, h, ctx, tableType, base); - - } while (((tableType == byU16) ? 0 : (match + MAX_DISTANCE < ip)) || (LZ4_read32(match) != LZ4_read32(ip))); - } - - /* Catch up */ - while ((ip > anchor) && (match > lowLimit) && (unlikely(ip[-1] == match[-1]))) { - ip--; - match--; - } - - { - /* Encode Literal length */ - unsigned litLength = (unsigned)(ip - anchor); - token = op++; - if (op + ((litLength + 240) / 255) + litLength > oMaxLit) { - /* Not enough space for a last match */ - op--; - goto _last_literals; - } - if (litLength >= RUN_MASK) { - unsigned len = litLength - RUN_MASK; - *token = (RUN_MASK << ML_BITS); - for (; len >= 255; len -= 255) - *op++ = 255; - *op++ = (BYTE)len; - } else - *token = (BYTE)(litLength << ML_BITS); - - /* Copy Literals */ - LZ4_wildCopy(op, anchor, op + litLength); - op += litLength; - } - - _next_match: - /* Encode Offset */ - LZ4_writeLE16(op, (U16)(ip - match)); - op += 2; - - /* Encode MatchLength */ - { - size_t matchLength; - - matchLength = LZ4_count(ip + MINMATCH, match + MINMATCH, matchlimit); - - if (op + ((matchLength + 240) / 255) > oMaxMatch) { - /* Match description too long : reduce it */ - matchLength = (15 - 1) + (oMaxMatch - op) * 255; - } - /*printf("offset %5i, matchLength%5i \n", (int)(ip-match), matchLength + MINMATCH);*/ - ip += MINMATCH + matchLength; - - if (matchLength >= ML_MASK) { - *token += ML_MASK; - matchLength -= ML_MASK; - while (matchLength >= 255) { - matchLength -= 255; - *op++ = 255; - } - *op++ = (BYTE)matchLength; - } else - *token += (BYTE)(matchLength); + LZ4_stream_t_internal* const ctx = & LZ4_initStream(state, sizeof(LZ4_stream_t)) -> internal_donotuse; + assert(ctx != NULL); + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; + if (maxOutputSize >= LZ4_compressBound(inputSize)) { + if (inputSize < LZ4_64Klimit) { + return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, byU16, noDict, noDictIssue, acceleration); + } else { + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; + return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } - - anchor = ip; - - /* Test end of block */ - if (ip > mflimit) - break; - if (op > oMaxSeq) - break; - - /* Fill table */ - LZ4_putPosition(ip - 2, ctx, tableType, base); - - /* Test next position */ - match = LZ4_getPosition(ip, ctx, tableType, base); - LZ4_putPosition(ip, ctx, tableType, base); - if ((match + MAX_DISTANCE >= ip) && (LZ4_read32(match) == LZ4_read32(ip))) { - token = op++; - *token = 0; - goto _next_match; + } else { + if (inputSize < LZ4_64Klimit) { + return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration); + } else { + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)source > LZ4_DISTANCE_MAX)) ? byPtr : byU32; + return LZ4_compress_generic(ctx, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, noDict, noDictIssue, acceleration); } - - /* Prepare next loop */ - forwardH = LZ4_hashPosition(++ip, tableType); } +} -_last_literals: - /* Encode Last Literals */ - { - size_t lastRunSize = (size_t)(iend - anchor); - if (op + 1 /* token */ + ((lastRunSize + 240) / 255) /* litLength */ + lastRunSize /* literals */ > oend) { - /* adapt lastRunSize to fill 'dst' */ - lastRunSize = (oend - op) - 1; - lastRunSize -= (lastRunSize + 240) / 255; +/** + * 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 comment in lz4.h on LZ4_resetStream_fast() for a definition of + * "correctly initialized"). + */ +int LZ4_compress_fast_extState_fastReset(void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration) +{ + LZ4_stream_t_internal* const ctx = &((LZ4_stream_t*)state)->internal_donotuse; + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; + assert(ctx != NULL); + + if (dstCapacity >= LZ4_compressBound(srcSize)) { + if (srcSize < LZ4_64Klimit) { + const tableType_t tableType = byU16; + LZ4_prepareTable(ctx, srcSize, tableType); + if (ctx->currentOffset) { + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, dictSmall, acceleration); + } else { + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); + } + } else { + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; + LZ4_prepareTable(ctx, srcSize, tableType); + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, 0, notLimited, tableType, noDict, noDictIssue, acceleration); } - ip = anchor + lastRunSize; - - if (lastRunSize >= RUN_MASK) { - size_t accumulator = lastRunSize - RUN_MASK; - *op++ = RUN_MASK << ML_BITS; - for (; accumulator >= 255; accumulator -= 255) - *op++ = 255; - *op++ = (BYTE)accumulator; + } else { + if (srcSize < LZ4_64Klimit) { + const tableType_t tableType = byU16; + LZ4_prepareTable(ctx, srcSize, tableType); + if (ctx->currentOffset) { + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, dictSmall, acceleration); + } else { + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); + } } else { - *op++ = (BYTE)(lastRunSize << ML_BITS); + const tableType_t tableType = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; + LZ4_prepareTable(ctx, srcSize, tableType); + return LZ4_compress_generic(ctx, src, dst, srcSize, NULL, dstCapacity, limitedOutput, tableType, noDict, noDictIssue, acceleration); } - memcpy(op, anchor, lastRunSize); - op += lastRunSize; } +} + + +int LZ4_compress_fast(const char* src, char* dest, int srcSize, int dstCapacity, int acceleration) +{ + int result; +#if (LZ4_HEAPMODE) + LZ4_stream_t* const ctxPtr = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ + if (ctxPtr == NULL) return 0; +#else + LZ4_stream_t ctx; + LZ4_stream_t* const ctxPtr = &ctx; +#endif + result = LZ4_compress_fast_extState(ctxPtr, src, dest, srcSize, dstCapacity, acceleration); + +#if (LZ4_HEAPMODE) + FREEMEM(ctxPtr); +#endif + return result; +} - /* End */ - *srcSizePtr = (int)(((const char *)ip) - src); - return (int)(((char *)op) - dst); + +int LZ4_compress_default(const char* src, char* dst, int srcSize, int dstCapacity) +{ + return LZ4_compress_fast(src, dst, srcSize, dstCapacity, 1); } -static int LZ4_compress_destSize_extState(void *state, const char *src, char *dst, int *srcSizePtr, int targetDstSize) + +/* Note!: This function leaves the stream in an unclean/broken state! + * It is not safe to subsequently use the same state with a _fastReset() or + * _continue() call without resetting it. */ +static int LZ4_compress_destSize_extState_internal(LZ4_stream_t* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration) { - LZ4_resetStream((LZ4_stream_t *)state); + void* const s = LZ4_initStream(state, sizeof (*state)); + assert(s != NULL); (void)s; - if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) /* compression success is guaranteed */ - { - return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, 1); + if (targetDstSize >= LZ4_compressBound(*srcSizePtr)) { /* compression success is guaranteed */ + return LZ4_compress_fast_extState(state, src, dst, *srcSizePtr, targetDstSize, acceleration); } else { - if (*srcSizePtr < LZ4_64Klimit) - return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, byU16); - else - return LZ4_compress_destSize_generic(state, src, dst, srcSizePtr, targetDstSize, - LZ4_64bits() ? byU32 : byPtr); - } + if (*srcSizePtr < LZ4_64Klimit) { + return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, byU16, noDict, noDictIssue, acceleration); + } else { + tableType_t const addrMode = ((sizeof(void*)==4) && ((uptrval)src > LZ4_DISTANCE_MAX)) ? byPtr : byU32; + return LZ4_compress_generic(&state->internal_donotuse, src, dst, *srcSizePtr, srcSizePtr, targetDstSize, fillOutput, addrMode, noDict, noDictIssue, acceleration); + } } +} + +int LZ4_compress_destSize_extState(void* state, const char* src, char* dst, int* srcSizePtr, int targetDstSize, int acceleration) +{ + int const r = LZ4_compress_destSize_extState_internal((LZ4_stream_t*)state, src, dst, srcSizePtr, targetDstSize, acceleration); + /* clean the state on exit */ + LZ4_initStream(state, sizeof (LZ4_stream_t)); + return r; } -int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize) + +int LZ4_compress_destSize(const char* src, char* dst, int* srcSizePtr, int targetDstSize) { -#if (HEAPMODE) - void *ctx = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ +#if (LZ4_HEAPMODE) + LZ4_stream_t* const ctx = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */ + if (ctx == NULL) return 0; #else LZ4_stream_t ctxBody; - void *ctx = &ctxBody; + LZ4_stream_t* const ctx = &ctxBody; #endif - int result = LZ4_compress_destSize_extState(ctx, src, dst, srcSizePtr, targetDstSize); + int result = LZ4_compress_destSize_extState_internal(ctx, src, dst, srcSizePtr, targetDstSize, 1); -#if (HEAPMODE) +#if (LZ4_HEAPMODE) FREEMEM(ctx); #endif return result; } -/******************************** - * Streaming functions - ********************************/ -LZ4_stream_t *LZ4_createStream(void) + +/*-****************************** +* Streaming functions +********************************/ + +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +LZ4_stream_t* LZ4_createStream(void) { - LZ4_stream_t *lz4s = (LZ4_stream_t *)ALLOCATOR(8, LZ4_STREAMSIZE_U64); - LZ4_STATIC_ASSERT( - LZ4_STREAMSIZE >= - sizeof(LZ4_stream_t_internal)); /* A compilation error here means LZ4_STREAMSIZE is not large enough */ - LZ4_resetStream(lz4s); + LZ4_stream_t* const lz4s = (LZ4_stream_t*)ALLOC(sizeof(LZ4_stream_t)); + LZ4_STATIC_ASSERT(sizeof(LZ4_stream_t) >= sizeof(LZ4_stream_t_internal)); + DEBUGLOG(4, "LZ4_createStream %p", lz4s); + if (lz4s == NULL) return NULL; + LZ4_initStream(lz4s, sizeof(*lz4s)); return lz4s; } +#endif + +static size_t LZ4_stream_t_alignment(void) +{ +#if LZ4_ALIGN_TEST + typedef struct { char c; LZ4_stream_t t; } t_a; + return sizeof(t_a) - sizeof(LZ4_stream_t); +#else + return 1; /* effectively disabled */ +#endif +} + +LZ4_stream_t* LZ4_initStream (void* buffer, size_t size) +{ + DEBUGLOG(5, "LZ4_initStream"); + if (buffer == NULL) { return NULL; } + if (size < sizeof(LZ4_stream_t)) { return NULL; } + if (!LZ4_isAligned(buffer, LZ4_stream_t_alignment())) return NULL; + MEM_INIT(buffer, 0, sizeof(LZ4_stream_t_internal)); + return (LZ4_stream_t*)buffer; +} + +/* resetStream is now deprecated, + * prefer initStream() which is more general */ +void LZ4_resetStream (LZ4_stream_t* LZ4_stream) +{ + DEBUGLOG(5, "LZ4_resetStream (ctx:%p)", LZ4_stream); + MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t_internal)); +} -void LZ4_resetStream(LZ4_stream_t *LZ4_stream) { MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t)); } +void LZ4_resetStream_fast(LZ4_stream_t* ctx) { + LZ4_prepareTable(&(ctx->internal_donotuse), 0, byU32); +} -int LZ4_freeStream(LZ4_stream_t *LZ4_stream) +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +int LZ4_freeStream (LZ4_stream_t* LZ4_stream) { + if (!LZ4_stream) return 0; /* support free on NULL */ + DEBUGLOG(5, "LZ4_freeStream %p", LZ4_stream); FREEMEM(LZ4_stream); return (0); } +#endif -#define HASH_UNIT sizeof(size_t) -int LZ4_loadDict(LZ4_stream_t *LZ4_dict, const char *dictionary, int dictSize) + +#define HASH_UNIT sizeof(reg_t) +int LZ4_loadDict (LZ4_stream_t* LZ4_dict, const char* dictionary, int dictSize) { - LZ4_stream_t_internal *dict = (LZ4_stream_t_internal *)LZ4_dict; - const BYTE *p = (const BYTE *)dictionary; - const BYTE *const dictEnd = p + dictSize; - const BYTE *base; + LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; + const tableType_t tableType = byU32; + const BYTE* p = (const BYTE*)dictionary; + const BYTE* const dictEnd = p + dictSize; + U32 idx32; + + DEBUGLOG(4, "LZ4_loadDict (%i bytes from %p into %p)", dictSize, dictionary, LZ4_dict); - if ((dict->initCheck) || (dict->currentOffset > 1 GB)) /* Uninitialized structure, or reuse overflow */ - LZ4_resetStream(LZ4_dict); + /* It's necessary to reset the context, + * and not just continue it with prepareTable() + * to avoid any risk of generating overflowing matchIndex + * when compressing using this dictionary */ + LZ4_resetStream(LZ4_dict); + + /* We always increment the offset by 64 KB, since, if the dict is longer, + * we truncate it to the last 64k, and if it's shorter, we still want to + * advance by a whole window length so we can provide the guarantee that + * there are only valid offsets in the window, which allows an optimization + * in LZ4_compress_fast_continue() where it uses noDictIssue even when the + * dictionary isn't a full 64k. */ + dict->currentOffset += 64 KB; if (dictSize < (int)HASH_UNIT) { - dict->dictionary = NULL; - dict->dictSize = 0; return 0; } - if ((dictEnd - p) > 64 KB) - p = dictEnd - 64 KB; - dict->currentOffset += 64 KB; - base = p - dict->currentOffset; + if ((dictEnd - p) > 64 KB) p = dictEnd - 64 KB; dict->dictionary = p; dict->dictSize = (U32)(dictEnd - p); - dict->currentOffset += dict->dictSize; + dict->tableType = (U32)tableType; + idx32 = dict->currentOffset - dict->dictSize; - while (p <= dictEnd - HASH_UNIT) { - LZ4_putPosition(p, dict->hashTable, byU32, base); - p += 3; + while (p <= dictEnd-HASH_UNIT) { + U32 const h = LZ4_hashPosition(p, tableType); + LZ4_putIndexOnHash(idx32, h, dict->hashTable, tableType); + p+=3; idx32+=3; } - return dict->dictSize; + return (int)dict->dictSize; } -static void LZ4_renormDictT(LZ4_stream_t_internal *LZ4_dict, const BYTE *src) +void LZ4_attach_dictionary(LZ4_stream_t* workingStream, const LZ4_stream_t* dictionaryStream) { - if ((LZ4_dict->currentOffset > 0x80000000) || - ((size_t)LZ4_dict->currentOffset > (size_t)src)) /* address space overflow */ - { + const LZ4_stream_t_internal* dictCtx = (dictionaryStream == NULL) ? NULL : + &(dictionaryStream->internal_donotuse); + + DEBUGLOG(4, "LZ4_attach_dictionary (%p, %p, size %u)", + workingStream, dictionaryStream, + dictCtx != NULL ? dictCtx->dictSize : 0); + + if (dictCtx != NULL) { + /* If the current offset is zero, we will never look in the + * external dictionary context, since there is no value a table + * entry can take that indicate a miss. In that case, we need + * to bump the offset to something non-zero. + */ + if (workingStream->internal_donotuse.currentOffset == 0) { + workingStream->internal_donotuse.currentOffset = 64 KB; + } + + /* Don't actually attach an empty dictionary. + */ + if (dictCtx->dictSize == 0) { + dictCtx = NULL; + } + } + workingStream->internal_donotuse.dictCtx = dictCtx; +} + + +static void LZ4_renormDictT(LZ4_stream_t_internal* LZ4_dict, int nextSize) +{ + assert(nextSize >= 0); + if (LZ4_dict->currentOffset + (unsigned)nextSize > 0x80000000) { /* potential ptrdiff_t overflow (32-bits mode) */ /* rescale hash table */ - U32 delta = LZ4_dict->currentOffset - 64 KB; - const BYTE *dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; + U32 const delta = LZ4_dict->currentOffset - 64 KB; + const BYTE* dictEnd = LZ4_dict->dictionary + LZ4_dict->dictSize; int i; - for (i = 0; i < HASH_SIZE_U32; i++) { - if (LZ4_dict->hashTable[i] < delta) - LZ4_dict->hashTable[i] = 0; - else - LZ4_dict->hashTable[i] -= delta; + DEBUGLOG(4, "LZ4_renormDictT"); + for (i=0; ihashTable[i] < delta) LZ4_dict->hashTable[i]=0; + else LZ4_dict->hashTable[i] -= delta; } LZ4_dict->currentOffset = 64 KB; - if (LZ4_dict->dictSize > 64 KB) - LZ4_dict->dictSize = 64 KB; + if (LZ4_dict->dictSize > 64 KB) LZ4_dict->dictSize = 64 KB; LZ4_dict->dictionary = dictEnd - LZ4_dict->dictSize; } } -int LZ4_compress_fast_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize, - int maxOutputSize, int acceleration) -{ - LZ4_stream_t_internal *streamPtr = (LZ4_stream_t_internal *)LZ4_stream; - const BYTE *const dictEnd = streamPtr->dictionary + streamPtr->dictSize; - const BYTE *smallest = (const BYTE *)source; - if (streamPtr->initCheck) - return 0; /* Uninitialized structure detected */ - if ((streamPtr->dictSize > 0) && (smallest > dictEnd)) - smallest = dictEnd; - LZ4_renormDictT(streamPtr, smallest); - if (acceleration < 1) - acceleration = ACCELERATION_DEFAULT; +int LZ4_compress_fast_continue (LZ4_stream_t* LZ4_stream, + const char* source, char* dest, + int inputSize, int maxOutputSize, + int acceleration) +{ + const tableType_t tableType = byU32; + LZ4_stream_t_internal* const streamPtr = &LZ4_stream->internal_donotuse; + const char* dictEnd = streamPtr->dictSize ? (const char*)streamPtr->dictionary + streamPtr->dictSize : NULL; + + DEBUGLOG(5, "LZ4_compress_fast_continue (inputSize=%i, dictSize=%u)", inputSize, streamPtr->dictSize); + + LZ4_renormDictT(streamPtr, inputSize); /* fix index overflow */ + if (acceleration < 1) acceleration = LZ4_ACCELERATION_DEFAULT; + if (acceleration > LZ4_ACCELERATION_MAX) acceleration = LZ4_ACCELERATION_MAX; + + /* invalidate tiny dictionaries */ + if ( (streamPtr->dictSize < 4) /* tiny dictionary : not enough for a hash */ + && (dictEnd != source) /* prefix mode */ + && (inputSize > 0) /* tolerance : don't lose history, in case next invocation would use prefix mode */ + && (streamPtr->dictCtx == NULL) /* usingDictCtx */ + ) { + DEBUGLOG(5, "LZ4_compress_fast_continue: dictSize(%u) at addr:%p is too small", streamPtr->dictSize, streamPtr->dictionary); + /* remove dictionary existence from history, to employ faster prefix mode */ + streamPtr->dictSize = 0; + streamPtr->dictionary = (const BYTE*)source; + dictEnd = source; + } /* Check overlapping input/dictionary space */ - { - const BYTE *sourceEnd = (const BYTE *)source + inputSize; - if ((sourceEnd > streamPtr->dictionary) && (sourceEnd < dictEnd)) { + { const char* const sourceEnd = source + inputSize; + if ((sourceEnd > (const char*)streamPtr->dictionary) && (sourceEnd < dictEnd)) { streamPtr->dictSize = (U32)(dictEnd - sourceEnd); - if (streamPtr->dictSize > 64 KB) - streamPtr->dictSize = 64 KB; - if (streamPtr->dictSize < 4) - streamPtr->dictSize = 0; - streamPtr->dictionary = dictEnd - streamPtr->dictSize; + if (streamPtr->dictSize > 64 KB) streamPtr->dictSize = 64 KB; + if (streamPtr->dictSize < 4) streamPtr->dictSize = 0; + streamPtr->dictionary = (const BYTE*)dictEnd - streamPtr->dictSize; } } /* prefix mode : source data follows dictionary */ - if (dictEnd == (const BYTE *)source) { - int result; + if (dictEnd == source) { if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) - result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, - withPrefix64k, dictSmall, acceleration); + return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, dictSmall, acceleration); else - result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, - withPrefix64k, noDictIssue, acceleration); - streamPtr->dictSize += (U32)inputSize; - streamPtr->currentOffset += (U32)inputSize; - return result; + return LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, withPrefix64k, noDictIssue, acceleration); } /* external dictionary mode */ - { - int result; - if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) - result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, - usingExtDict, dictSmall, acceleration); - else - result = LZ4_compress_generic(LZ4_stream, source, dest, inputSize, maxOutputSize, limitedOutput, byU32, - usingExtDict, noDictIssue, acceleration); - streamPtr->dictionary = (const BYTE *)source; + { int result; + if (streamPtr->dictCtx) { + /* We depend here on the fact that dictCtx'es (produced by + * LZ4_loadDict) guarantee that their tables contain no references + * to offsets between dictCtx->currentOffset - 64 KB and + * dictCtx->currentOffset - dictCtx->dictSize. This makes it safe + * to use noDictIssue even when the dict isn't a full 64 KB. + */ + if (inputSize > 4 KB) { + /* For compressing large blobs, it is faster to pay the setup + * cost to copy the dictionary's tables into the active context, + * so that the compression loop is only looking into one table. + */ + LZ4_memcpy(streamPtr, streamPtr->dictCtx, sizeof(*streamPtr)); + result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); + } else { + result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingDictCtx, noDictIssue, acceleration); + } + } else { /* small data <= 4 KB */ + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { + result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, dictSmall, acceleration); + } else { + result = LZ4_compress_generic(streamPtr, source, dest, inputSize, NULL, maxOutputSize, limitedOutput, tableType, usingExtDict, noDictIssue, acceleration); + } + } + streamPtr->dictionary = (const BYTE*)source; streamPtr->dictSize = (U32)inputSize; - streamPtr->currentOffset += (U32)inputSize; return result; } } -/* Hidden debug function, to force external dictionary mode */ -int LZ4_compress_forceExtDict(LZ4_stream_t *LZ4_dict, const char *source, char *dest, int inputSize) + +/* Hidden debug function, to force-test external dictionary mode */ +int LZ4_compress_forceExtDict (LZ4_stream_t* LZ4_dict, const char* source, char* dest, int srcSize) { - LZ4_stream_t_internal *streamPtr = (LZ4_stream_t_internal *)LZ4_dict; + LZ4_stream_t_internal* const streamPtr = &LZ4_dict->internal_donotuse; int result; - const BYTE *const dictEnd = streamPtr->dictionary + streamPtr->dictSize; - const BYTE *smallest = dictEnd; - if (smallest > (const BYTE *)source) - smallest = (const BYTE *)source; - LZ4_renormDictT((LZ4_stream_t_internal *)LZ4_dict, smallest); + LZ4_renormDictT(streamPtr, srcSize); - result = - LZ4_compress_generic(LZ4_dict, source, dest, inputSize, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); + if ((streamPtr->dictSize < 64 KB) && (streamPtr->dictSize < streamPtr->currentOffset)) { + result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, dictSmall, 1); + } else { + result = LZ4_compress_generic(streamPtr, source, dest, srcSize, NULL, 0, notLimited, byU32, usingExtDict, noDictIssue, 1); + } - streamPtr->dictionary = (const BYTE *)source; - streamPtr->dictSize = (U32)inputSize; - streamPtr->currentOffset += (U32)inputSize; + streamPtr->dictionary = (const BYTE*)source; + streamPtr->dictSize = (U32)srcSize; return result; } -int LZ4_saveDict(LZ4_stream_t *LZ4_dict, char *safeBuffer, int dictSize) + +/*! LZ4_saveDict() : + * If previously compressed data block is not guaranteed to remain available at its memory location, + * save it into a safer place (char* safeBuffer). + * Note : no need to call LZ4_loadDict() afterwards, dictionary is immediately usable, + * one can therefore call LZ4_compress_fast_continue() right after. + * @return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error. + */ +int LZ4_saveDict (LZ4_stream_t* LZ4_dict, char* safeBuffer, int dictSize) { - LZ4_stream_t_internal *dict = (LZ4_stream_t_internal *)LZ4_dict; - const BYTE *previousDictEnd = dict->dictionary + dict->dictSize; + LZ4_stream_t_internal* const dict = &LZ4_dict->internal_donotuse; + + DEBUGLOG(5, "LZ4_saveDict : dictSize=%i, safeBuffer=%p", dictSize, safeBuffer); - if ((U32)dictSize > 64 KB) - dictSize = 64 KB; /* useless to define a dictionary > 64 KB */ - if ((U32)dictSize > dict->dictSize) - dictSize = dict->dictSize; + if ((U32)dictSize > 64 KB) { dictSize = 64 KB; } /* useless to define a dictionary > 64 KB */ + if ((U32)dictSize > dict->dictSize) { dictSize = (int)dict->dictSize; } - memmove(safeBuffer, previousDictEnd - dictSize, dictSize); + if (safeBuffer == NULL) assert(dictSize == 0); + if (dictSize > 0) { + const BYTE* const previousDictEnd = dict->dictionary + dict->dictSize; + assert(dict->dictionary); + LZ4_memmove(safeBuffer, previousDictEnd - dictSize, (size_t)dictSize); + } - dict->dictionary = (const BYTE *)safeBuffer; + dict->dictionary = (const BYTE*)safeBuffer; dict->dictSize = (U32)dictSize; return dictSize; } -/******************************* + + +/*-******************************* * Decompression functions - *******************************/ -/* - * This generic decompression function cover all use cases. - * It shall be instantiated several times, using different sets of directives - * Note that it is essential this generic function is really inlined, - * in order to remove useless branches during compilation optimization. - */ -FORCE_INLINE int -LZ4_decompress_generic(const char *const source, char *const dest, int inputSize, - int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */ - - int endOnInput, /* endOnOutputSize, endOnInputSize */ - int partialDecoding, /* full, partial */ - int targetOutputSize, /* only used if partialDecoding==partial */ - int dict, /* noDict, withPrefix64k, usingExtDict */ - const BYTE *const lowPrefix, /* == dest if dict == noDict */ - const BYTE *const dictStart, /* only if dict==usingExtDict */ - const size_t dictSize /* note : = 0 if noDict */ -) -{ - /* Local Variables */ - const BYTE *ip = (const BYTE *)source; - const BYTE *const iend = ip + inputSize; - - BYTE *op = (BYTE *)dest; - BYTE *const oend = op + outputSize; - BYTE *cpy; - BYTE *oexit = op + targetOutputSize; - const BYTE *const lowLimit = lowPrefix - dictSize; - - const BYTE *const dictEnd = (const BYTE *)dictStart + dictSize; - const size_t dec32table[] = {4, 1, 2, 1, 4, 4, 4, 4}; - const size_t dec64table[] = {0, 0, 0, (size_t)-1, 0, 1, 2, 3}; - - const int safeDecode = (endOnInput == endOnInputSize); - const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB))); - - /* Special cases */ - if ((partialDecoding) && (oexit > oend - MFLIMIT)) - oexit = oend - MFLIMIT; /* targetOutputSize too high => decode everything */ - if ((endOnInput) && (unlikely(outputSize == 0))) - return ((inputSize == 1) && (*ip == 0)) ? 0 : -1; /* Empty output buffer */ - if ((!endOnInput) && (unlikely(outputSize == 0))) - return (*ip == 0 ? 1 : -1); + ********************************/ + +typedef enum { decode_full_block = 0, partial_decode = 1 } earlyEnd_directive; + +#undef MIN +#define MIN(a,b) ( (a) < (b) ? (a) : (b) ) + + +/* variant for decompress_unsafe() + * does not know end of input + * presumes input is well formed + * note : will consume at least one byte */ +static size_t read_long_length_no_check(const BYTE** pp) +{ + size_t b, l = 0; + do { b = **pp; (*pp)++; l += b; } while (b==255); + DEBUGLOG(6, "read_long_length_no_check: +length=%zu using %zu input bytes", l, l/255 + 1) + return l; +} + +/* core decoder variant for LZ4_decompress_fast*() + * for legacy support only : these entry points are deprecated. + * - Presumes input is correctly formed (no defense vs malformed inputs) + * - Does not know input size (presume input buffer is "large enough") + * - Decompress a full block (only) + * @return : nb of bytes read from input. + * Note : this variant is not optimized for speed, just for maintenance. + * the goal is to remove support of decompress_fast*() variants by v2.0 +**/ +LZ4_FORCE_INLINE int +LZ4_decompress_unsafe_generic( + const BYTE* const istart, + BYTE* const ostart, + int decompressedSize, + + size_t prefixSize, + const BYTE* const dictStart, /* only if dict==usingExtDict */ + const size_t dictSize /* note: =0 if dictStart==NULL */ + ) +{ + const BYTE* ip = istart; + BYTE* op = (BYTE*)ostart; + BYTE* const oend = ostart + decompressedSize; + const BYTE* const prefixStart = ostart - prefixSize; + + DEBUGLOG(5, "LZ4_decompress_unsafe_generic"); + if (dictStart == NULL) assert(dictSize == 0); - /* Main Loop */ while (1) { + /* start new sequence */ + unsigned token = *ip++; + + /* literals */ + { size_t ll = token >> ML_BITS; + if (ll==15) { + /* long literal length */ + ll += read_long_length_no_check(&ip); + } + if ((size_t)(oend-op) < ll) return -1; /* output buffer overflow */ + LZ4_memmove(op, ip, ll); /* support in-place decompression */ + op += ll; + ip += ll; + if ((size_t)(oend-op) < MFLIMIT) { + if (op==oend) break; /* end of block */ + DEBUGLOG(5, "invalid: literals end at distance %zi from end of block", oend-op); + /* incorrect end of block : + * last match must start at least MFLIMIT==12 bytes before end of output block */ + return -1; + } } + + /* match */ + { size_t ml = token & 15; + size_t const offset = LZ4_readLE16(ip); + ip+=2; + + if (ml==15) { + /* long literal length */ + ml += read_long_length_no_check(&ip); + } + ml += MINMATCH; + + if ((size_t)(oend-op) < ml) return -1; /* output buffer overflow */ + + { const BYTE* match = op - offset; + + /* out of range */ + if (offset > (size_t)(op - prefixStart) + dictSize) { + DEBUGLOG(6, "offset out of range"); + return -1; + } + + /* check special case : extDict */ + if (offset > (size_t)(op - prefixStart)) { + /* extDict scenario */ + const BYTE* const dictEnd = dictStart + dictSize; + const BYTE* extMatch = dictEnd - (offset - (size_t)(op-prefixStart)); + size_t const extml = (size_t)(dictEnd - extMatch); + if (extml > ml) { + /* match entirely within extDict */ + LZ4_memmove(op, extMatch, ml); + op += ml; + ml = 0; + } else { + /* match split between extDict & prefix */ + LZ4_memmove(op, extMatch, extml); + op += extml; + ml -= extml; + } + match = prefixStart; + } + + /* match copy - slow variant, supporting overlap copy */ + { size_t u; + for (u=0; u= ipmax before start of loop. Returns initial_error if so. + * @error (output) - error code. Must be set to 0 before call. +**/ +typedef size_t Rvl_t; +static const Rvl_t rvl_error = (Rvl_t)(-1); +LZ4_FORCE_INLINE Rvl_t +read_variable_length(const BYTE** ip, const BYTE* ilimit, + int initial_check) +{ + Rvl_t s, length = 0; + assert(ip != NULL); + assert(*ip != NULL); + assert(ilimit != NULL); + if (initial_check && unlikely((*ip) >= ilimit)) { /* read limit reached */ + return rvl_error; + } + s = **ip; + (*ip)++; + length += s; + if (unlikely((*ip) > ilimit)) { /* read limit reached */ + return rvl_error; + } + /* accumulator overflow detection (32-bit mode only) */ + if ((sizeof(length) < 8) && unlikely(length > ((Rvl_t)(-1)/2)) ) { + return rvl_error; + } + if (likely(s != 255)) return length; + do { + s = **ip; + (*ip)++; + length += s; + if (unlikely((*ip) > ilimit)) { /* read limit reached */ + return rvl_error; + } + /* accumulator overflow detection (32-bit mode only) */ + if ((sizeof(length) < 8) && unlikely(length > ((Rvl_t)(-1)/2)) ) { + return rvl_error; + } + } while (s == 255); + + return length; +} + +/*! LZ4_decompress_generic() : + * This generic decompression function covers all use cases. + * It shall be instantiated several times, using different sets of directives. + * Note that it is important for performance that this function really get inlined, + * in order to remove useless branches during compilation optimization. + */ +LZ4_FORCE_INLINE int +LZ4_decompress_generic( + const char* const src, + char* const dst, + int srcSize, + int outputSize, /* If endOnInput==endOnInputSize, this value is `dstCapacity` */ + + earlyEnd_directive partialDecoding, /* full, partial */ + dict_directive dict, /* noDict, withPrefix64k, usingExtDict */ + const BYTE* const lowPrefix, /* always <= dst, == dst when no prefix */ + const BYTE* const dictStart, /* only if dict==usingExtDict */ + const size_t dictSize /* note : = 0 if noDict */ + ) +{ + if ((src == NULL) || (outputSize < 0)) { return -1; } + + { const BYTE* ip = (const BYTE*) src; + const BYTE* const iend = ip + srcSize; + + BYTE* op = (BYTE*) dst; + BYTE* const oend = op + outputSize; + BYTE* cpy; + + const BYTE* const dictEnd = (dictStart == NULL) ? NULL : dictStart + dictSize; + + const int checkOffset = (dictSize < (int)(64 KB)); + + + /* Set up the "end" pointers for the shortcut. */ + const BYTE* const shortiend = iend - 14 /*maxLL*/ - 2 /*offset*/; + const BYTE* const shortoend = oend - 14 /*maxLL*/ - 18 /*maxML*/; + + const BYTE* match; + size_t offset; unsigned token; size_t length; - const BYTE *match; - /* get literal length */ - token = *ip++; - if ((length = (token >> ML_BITS)) == RUN_MASK) { - unsigned s; - do { - s = *ip++; - length += s; - } while (likely((endOnInput) ? ip < iend - RUN_MASK : 1) && (s == 255)); - if ((safeDecode) && unlikely((size_t)(op + length) < (size_t)(op))) - goto _output_error; /* overflow detection */ - if ((safeDecode) && unlikely((size_t)(ip + length) < (size_t)(ip))) - goto _output_error; /* overflow detection */ + + DEBUGLOG(5, "LZ4_decompress_generic (srcSize:%i, dstSize:%i)", srcSize, outputSize); + + /* Special cases */ + assert(lowPrefix <= op); + if (unlikely(outputSize==0)) { + /* Empty output buffer */ + if (partialDecoding) return 0; + return ((srcSize==1) && (*ip==0)) ? 0 : -1; + } + if (unlikely(srcSize==0)) { return -1; } + + /* LZ4_FAST_DEC_LOOP: + * designed for modern OoO performance cpus, + * where copying reliably 32-bytes is preferable to an unpredictable branch. + * note : fast loop may show a regression for some client arm chips. */ +#if LZ4_FAST_DEC_LOOP + if ((oend - op) < FASTLOOP_SAFE_DISTANCE) { + DEBUGLOG(6, "skip fast decode loop"); + goto safe_decode; } - /* copy literals */ - cpy = op + length; - if (((endOnInput) && - ((cpy > (partialDecoding ? oexit : oend - MFLIMIT)) || (ip + length > iend - (2 + 1 + LASTLITERALS)))) || - ((!endOnInput) && (cpy > oend - COPYLENGTH))) { - if (partialDecoding) { - if (cpy > oend) - goto _output_error; /* Error : write attempt beyond end of output buffer */ - if ((endOnInput) && (ip + length > iend)) - goto _output_error; /* Error : read attempt beyond end of input buffer */ + /* Fast loop : decode sequences as long as output < oend-FASTLOOP_SAFE_DISTANCE */ + DEBUGLOG(6, "using fast decode loop"); + while (1) { + /* Main fastloop assertion: We can always wildcopy FASTLOOP_SAFE_DISTANCE */ + assert(oend - op >= FASTLOOP_SAFE_DISTANCE); + assert(ip < iend); + token = *ip++; + length = token >> ML_BITS; /* literal length */ + + /* decode literal length */ + if (length == RUN_MASK) { + size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); + if (addl == rvl_error) { + DEBUGLOG(6, "error reading long literal length"); + goto _output_error; + } + length += addl; + if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ + + /* copy literals */ + LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); + if ((op+length>oend-32) || (ip+length>iend-32)) { goto safe_literal_copy; } + LZ4_wildCopy32(op, ip, op+length); + ip += length; op += length; + } else if (ip <= iend-(16 + 1/*max lit + offset + nextToken*/)) { + /* We don't need to check oend, since we check it once for each loop below */ + DEBUGLOG(7, "copy %u bytes in a 16-bytes stripe", (unsigned)length); + /* Literals can only be <= 14, but hope compilers optimize better when copy by a register size */ + LZ4_memcpy(op, ip, 16); + ip += length; op += length; } else { - if ((!endOnInput) && (cpy != oend)) - goto _output_error; /* Error : block decoding must stop exactly there */ - if ((endOnInput) && ((ip + length != iend) || (cpy > oend))) - goto _output_error; /* Error : input must be consumed */ + goto safe_literal_copy; } - memcpy(op, ip, length); - ip += length; - op += length; - break; /* Necessarily EOF, due to parsing restrictions */ - } - LZ4_wildCopy(op, ip, cpy); - ip += length; - op = cpy; - - /* get offset */ - match = cpy - LZ4_readLE16(ip); - ip += 2; - if ((checkOffset) && (unlikely(match < lowLimit))) - goto _output_error; /* Error : offset outside destination buffer */ - - /* get matchlength */ - length = token & ML_MASK; - if (length == ML_MASK) { - unsigned s; - do { - if ((endOnInput) && (ip > iend - LASTLITERALS)) + + /* get offset */ + offset = LZ4_readLE16(ip); ip+=2; + DEBUGLOG(6, " offset = %zu", offset); + match = op - offset; + assert(match <= op); /* overflow check */ + + /* get matchlength */ + length = token & ML_MASK; + + if (length == ML_MASK) { + size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); + if (addl == rvl_error) { + DEBUGLOG(6, "error reading long match length"); goto _output_error; - s = *ip++; - length += s; - } while (s == 255); - if ((safeDecode) && unlikely((size_t)(op + length) < (size_t)op)) - goto _output_error; /* overflow detection */ + } + length += addl; + length += MINMATCH; + if (unlikely((uptrval)(op)+length<(uptrval)op)) { goto _output_error; } /* overflow detection */ + if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { + goto safe_match_copy; + } + } else { + length += MINMATCH; + if (op + length >= oend - FASTLOOP_SAFE_DISTANCE) { + goto safe_match_copy; + } + + /* Fastpath check: skip LZ4_wildCopy32 when true */ + if ((dict == withPrefix64k) || (match >= lowPrefix)) { + if (offset >= 8) { + assert(match >= lowPrefix); + assert(match <= op); + assert(op + 18 <= oend); + + LZ4_memcpy(op, match, 8); + LZ4_memcpy(op+8, match+8, 8); + LZ4_memcpy(op+16, match+16, 2); + op += length; + continue; + } } } + + if ( checkOffset && (unlikely(match + dictSize < lowPrefix)) ) { + DEBUGLOG(6, "Error : pos=%zi, offset=%zi => outside buffers", op-lowPrefix, op-match); + goto _output_error; + } + /* match starting within external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) { + assert(dictEnd != NULL); + if (unlikely(op+length > oend-LASTLITERALS)) { + if (partialDecoding) { + DEBUGLOG(7, "partialDecoding: dictionary match, close to dstEnd"); + length = MIN(length, (size_t)(oend-op)); + } else { + DEBUGLOG(6, "end-of-block condition violated") + goto _output_error; + } } + + if (length <= (size_t)(lowPrefix-match)) { + /* match fits entirely within external dictionary : just copy */ + LZ4_memmove(op, dictEnd - (lowPrefix-match), length); + op += length; + } else { + /* match stretches into both external dictionary and current block */ + size_t const copySize = (size_t)(lowPrefix - match); + size_t const restSize = length - copySize; + LZ4_memcpy(op, dictEnd - copySize, copySize); + op += copySize; + if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ + BYTE* const endOfMatch = op + restSize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) { *op++ = *copyFrom++; } + } else { + LZ4_memcpy(op, lowPrefix, restSize); + op += restSize; + } } + continue; + } + + /* copy match within block */ + cpy = op + length; + + assert((op <= oend) && (oend-op >= 32)); + if (unlikely(offset<16)) { + LZ4_memcpy_using_offset(op, match, cpy, offset); + } else { + LZ4_wildCopy32(op, match, cpy); + } + + op = cpy; /* wildcopy correction */ } - length += MINMATCH; + safe_decode: +#endif + + /* Main Loop : decode remaining sequences where output < FASTLOOP_SAFE_DISTANCE */ + DEBUGLOG(6, "using safe decode loop"); + while (1) { + assert(ip < iend); + token = *ip++; + length = token >> ML_BITS; /* literal length */ + + /* A two-stage shortcut for the most common case: + * 1) If the literal length is 0..14, and there is enough space, + * enter the shortcut and copy 16 bytes on behalf of the literals + * (in the fast mode, only 8 bytes can be safely copied this way). + * 2) Further if the match length is 4..18, copy 18 bytes in a similar + * manner; but we ensure that there's enough space in the output for + * those 18 bytes earlier, upon entering the shortcut (in other words, + * there is a combined check for both stages). + */ + if ( (length != RUN_MASK) + /* strictly "less than" on input, to re-enter the loop with at least one byte */ + && likely((ip < shortiend) & (op <= shortoend)) ) { + /* Copy the literals */ + LZ4_memcpy(op, ip, 16); + op += length; ip += length; + + /* The second stage: prepare for match copying, decode full info. + * If it doesn't work out, the info won't be wasted. */ + length = token & ML_MASK; /* match length */ + offset = LZ4_readLE16(ip); ip += 2; + match = op - offset; + assert(match <= op); /* check overflow */ + + /* Do not deal with overlapping matches. */ + if ( (length != ML_MASK) + && (offset >= 8) + && (dict==withPrefix64k || match >= lowPrefix) ) { + /* Copy the match. */ + LZ4_memcpy(op + 0, match + 0, 8); + LZ4_memcpy(op + 8, match + 8, 8); + LZ4_memcpy(op +16, match +16, 2); + op += length + MINMATCH; + /* Both stages worked, load the next token. */ + continue; + } + + /* The second stage didn't work out, but the info is ready. + * Propel it right to the point of match copying. */ + goto _copy_match; + } - /* check external dictionary */ - if ((dict == usingExtDict) && (match < lowPrefix)) { - if (unlikely(op + length > oend - LASTLITERALS)) - goto _output_error; /* doesn't respect parsing restriction */ + /* decode literal length */ + if (length == RUN_MASK) { + size_t const addl = read_variable_length(&ip, iend-RUN_MASK, 1); + if (addl == rvl_error) { goto _output_error; } + length += addl; + if (unlikely((uptrval)(op)+length<(uptrval)(op))) { goto _output_error; } /* overflow detection */ + if (unlikely((uptrval)(ip)+length<(uptrval)(ip))) { goto _output_error; } /* overflow detection */ + } - if (length <= (size_t)(lowPrefix - match)) { - /* match can be copied as a single segment from external dictionary */ - match = dictEnd - (lowPrefix - match); - memmove(op, match, length); +#if LZ4_FAST_DEC_LOOP + safe_literal_copy: +#endif + /* copy literals */ + cpy = op+length; + + LZ4_STATIC_ASSERT(MFLIMIT >= WILDCOPYLENGTH); + if ((cpy>oend-MFLIMIT) || (ip+length>iend-(2+1+LASTLITERALS))) { + /* We've either hit the input parsing restriction or the output parsing restriction. + * In the normal scenario, decoding a full block, it must be the last sequence, + * otherwise it's an error (invalid input or dimensions). + * In partialDecoding scenario, it's necessary to ensure there is no buffer overflow. + */ + if (partialDecoding) { + /* Since we are partial decoding we may be in this block because of the output parsing + * restriction, which is not valid since the output buffer is allowed to be undersized. + */ + DEBUGLOG(7, "partialDecoding: copying literals, close to input or output end") + DEBUGLOG(7, "partialDecoding: literal length = %u", (unsigned)length); + DEBUGLOG(7, "partialDecoding: remaining space in dstBuffer : %i", (int)(oend - op)); + DEBUGLOG(7, "partialDecoding: remaining space in srcBuffer : %i", (int)(iend - ip)); + /* Finishing in the middle of a literals segment, + * due to lack of input. + */ + if (ip+length > iend) { + length = (size_t)(iend-ip); + cpy = op + length; + } + /* Finishing in the middle of a literals segment, + * due to lack of output space. + */ + if (cpy > oend) { + cpy = oend; + assert(op<=oend); + length = (size_t)(oend-op); + } + } else { + /* We must be on the last sequence (or invalid) because of the parsing limitations + * so check that we exactly consume the input and don't overrun the output buffer. + */ + if ((ip+length != iend) || (cpy > oend)) { + DEBUGLOG(6, "should have been last run of literals") + DEBUGLOG(6, "ip(%p) + length(%i) = %p != iend (%p)", ip, (int)length, ip+length, iend); + DEBUGLOG(6, "or cpy(%p) > oend(%p)", cpy, oend); + goto _output_error; + } + } + LZ4_memmove(op, ip, length); /* supports overlapping memory regions, for in-place decompression scenarios */ + ip += length; op += length; + /* Necessarily EOF when !partialDecoding. + * When partialDecoding, it is EOF if we've either + * filled the output buffer or + * can't proceed with reading an offset for following match. + */ + if (!partialDecoding || (cpy == oend) || (ip >= (iend-2))) { + break; + } } else { - /* match encompass external dictionary and current segment */ - size_t copySize = (size_t)(lowPrefix - match); - memcpy(op, dictEnd - copySize, copySize); - op += copySize; - copySize = length - copySize; - if (copySize > (size_t)(op - lowPrefix)) /* overlap within current segment */ - { - BYTE *const endOfMatch = op + copySize; - const BYTE *copyFrom = lowPrefix; - while (op < endOfMatch) - *op++ = *copyFrom++; + LZ4_wildCopy8(op, ip, cpy); /* can overwrite up to 8 bytes beyond cpy */ + ip += length; op = cpy; + } + + /* get offset */ + offset = LZ4_readLE16(ip); ip+=2; + match = op - offset; + + /* get matchlength */ + length = token & ML_MASK; + + _copy_match: + if (length == ML_MASK) { + size_t const addl = read_variable_length(&ip, iend - LASTLITERALS + 1, 0); + if (addl == rvl_error) { goto _output_error; } + length += addl; + if (unlikely((uptrval)(op)+length<(uptrval)op)) goto _output_error; /* overflow detection */ + } + length += MINMATCH; + +#if LZ4_FAST_DEC_LOOP + safe_match_copy: +#endif + if ((checkOffset) && (unlikely(match + dictSize < lowPrefix))) goto _output_error; /* Error : offset outside buffers */ + /* match starting within external dictionary */ + if ((dict==usingExtDict) && (match < lowPrefix)) { + assert(dictEnd != NULL); + if (unlikely(op+length > oend-LASTLITERALS)) { + if (partialDecoding) length = MIN(length, (size_t)(oend-op)); + else goto _output_error; /* doesn't respect parsing restriction */ + } + + if (length <= (size_t)(lowPrefix-match)) { + /* match fits entirely within external dictionary : just copy */ + LZ4_memmove(op, dictEnd - (lowPrefix-match), length); + op += length; } else { - memcpy(op, lowPrefix, copySize); + /* match stretches into both external dictionary and current block */ + size_t const copySize = (size_t)(lowPrefix - match); + size_t const restSize = length - copySize; + LZ4_memcpy(op, dictEnd - copySize, copySize); op += copySize; + if (restSize > (size_t)(op - lowPrefix)) { /* overlap copy */ + BYTE* const endOfMatch = op + restSize; + const BYTE* copyFrom = lowPrefix; + while (op < endOfMatch) *op++ = *copyFrom++; + } else { + LZ4_memcpy(op, lowPrefix, restSize); + op += restSize; + } } + continue; + } + assert(match >= lowPrefix); + + /* copy match within block */ + cpy = op + length; + + /* partialDecoding : may end anywhere within the block */ + assert(op<=oend); + if (partialDecoding && (cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + size_t const mlen = MIN(length, (size_t)(oend-op)); + const BYTE* const matchEnd = match + mlen; + BYTE* const copyEnd = op + mlen; + if (matchEnd > op) { /* overlap copy */ + while (op < copyEnd) { *op++ = *match++; } + } else { + LZ4_memcpy(op, match, mlen); } + op = copyEnd; + if (op == oend) { break; } + continue; } - continue; - } - /* copy repeated sequence */ - cpy = op + length; - if (unlikely((op - match) < 8)) { - const size_t dec64 = dec64table[op - match]; - op[0] = match[0]; - op[1] = match[1]; - op[2] = match[2]; - op[3] = match[3]; - match += dec32table[op - match]; - LZ4_copy4(op + 4, match); - op += 8; - match -= dec64; - } else { - LZ4_copy8(op, match); + if (unlikely(offset<8)) { + LZ4_write32(op, 0); /* silence msan warning when offset==0 */ + op[0] = match[0]; + op[1] = match[1]; + op[2] = match[2]; + op[3] = match[3]; + match += inc32table[offset]; + LZ4_memcpy(op+4, match, 4); + match -= dec64table[offset]; + } else { + LZ4_memcpy(op, match, 8); + match += 8; + } op += 8; - match += 8; - } - if (unlikely(cpy > oend - 12)) { - if (cpy > oend - LASTLITERALS) - goto _output_error; /* Error : last LASTLITERALS bytes must be literals */ - if (op < oend - 8) { - LZ4_wildCopy(op, match, oend - 8); - match += (oend - 8) - op; - op = oend - 8; + if (unlikely(cpy > oend-MATCH_SAFEGUARD_DISTANCE)) { + BYTE* const oCopyLimit = oend - (WILDCOPYLENGTH-1); + if (cpy > oend-LASTLITERALS) { goto _output_error; } /* Error : last LASTLITERALS bytes must be literals (uncompressed) */ + if (op < oCopyLimit) { + LZ4_wildCopy8(op, match, oCopyLimit); + match += oCopyLimit - op; + op = oCopyLimit; + } + while (op < cpy) { *op++ = *match++; } + } else { + LZ4_memcpy(op, match, 8); + if (length > 16) { LZ4_wildCopy8(op+8, match+8, cpy); } } - while (op < cpy) - *op++ = *match++; - } else - LZ4_wildCopy(op, match, cpy); - op = cpy; /* correction */ + op = cpy; /* wildcopy correction */ + } + + /* end of decoding */ + DEBUGLOG(5, "decoded %i bytes", (int) (((char*)op)-dst)); + return (int) (((char*)op)-dst); /* Nb of output bytes decoded */ + + /* Overflow error detected */ + _output_error: + return (int) (-(((const char*)ip)-src))-1; } +} - /* end of decoding */ - if (endOnInput) - return (int)(((char *)op) - dest); /* Nb of output bytes decoded */ - else - return (int)(((const char *)ip) - source); /* Nb of input bytes read */ - /* Overflow error detected */ -_output_error: - return (int)(-(((const char *)ip) - source)) - 1; +/*===== Instantiate the API decoding functions. =====*/ + +LZ4_FORCE_O2 +int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, + decode_full_block, noDict, + (BYTE*)dest, NULL, 0); } -int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize) +LZ4_FORCE_O2 +int LZ4_decompress_safe_partial(const char* src, char* dst, int compressedSize, int targetOutputSize, int dstCapacity) { - return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, full, 0, noDict, - (BYTE *)dest, NULL, 0); + dstCapacity = MIN(targetOutputSize, dstCapacity); + return LZ4_decompress_generic(src, dst, compressedSize, dstCapacity, + partial_decode, + noDict, (BYTE*)dst, NULL, 0); } -int LZ4_decompress_safe_partial(const char *source, char *dest, int compressedSize, int targetOutputSize, - int maxDecompressedSize) +LZ4_FORCE_O2 +int LZ4_decompress_fast(const char* source, char* dest, int originalSize) { - return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, partial, - targetOutputSize, noDict, (BYTE *)dest, NULL, 0); + DEBUGLOG(5, "LZ4_decompress_fast"); + return LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + 0, NULL, 0); } -int LZ4_decompress_fast(const char *source, char *dest, int originalSize) +/*===== Instantiate a few more decoding cases, used more than once. =====*/ + +LZ4_FORCE_O2 /* Exported, an obsolete API function. */ +int LZ4_decompress_safe_withPrefix64k(const char* source, char* dest, int compressedSize, int maxOutputSize) { - return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, - (BYTE *)(dest - 64 KB), NULL, 64 KB); + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + decode_full_block, withPrefix64k, + (BYTE*)dest - 64 KB, NULL, 0); } -/* streaming decompression functions */ +LZ4_FORCE_O2 +static int LZ4_decompress_safe_partial_withPrefix64k(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity) +{ + dstCapacity = MIN(targetOutputSize, dstCapacity); + return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, + partial_decode, withPrefix64k, + (BYTE*)dest - 64 KB, NULL, 0); +} -typedef struct +/* Another obsolete API function, paired with the previous one. */ +int LZ4_decompress_fast_withPrefix64k(const char* source, char* dest, int originalSize) { - const BYTE *externalDict; - size_t extDictSize; - const BYTE *prefixEnd; - size_t prefixSize; -} LZ4_streamDecode_t_internal; + return LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + 64 KB, NULL, 0); +} -/* - * If you prefer dynamic allocation methods, - * LZ4_createStreamDecode() - * provides a pointer (void*) towards an initialized LZ4_streamDecode_t structure. +LZ4_FORCE_O2 +static int LZ4_decompress_safe_withSmallPrefix(const char* source, char* dest, int compressedSize, int maxOutputSize, + size_t prefixSize) +{ + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + decode_full_block, noDict, + (BYTE*)dest-prefixSize, NULL, 0); +} + +LZ4_FORCE_O2 +static int LZ4_decompress_safe_partial_withSmallPrefix(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, + size_t prefixSize) +{ + dstCapacity = MIN(targetOutputSize, dstCapacity); + return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, + partial_decode, noDict, + (BYTE*)dest-prefixSize, NULL, 0); +} + +LZ4_FORCE_O2 +int LZ4_decompress_safe_forceExtDict(const char* source, char* dest, + int compressedSize, int maxOutputSize, + const void* dictStart, size_t dictSize) +{ + DEBUGLOG(5, "LZ4_decompress_safe_forceExtDict"); + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + decode_full_block, usingExtDict, + (BYTE*)dest, (const BYTE*)dictStart, dictSize); +} + +LZ4_FORCE_O2 +int LZ4_decompress_safe_partial_forceExtDict(const char* source, char* dest, + int compressedSize, int targetOutputSize, int dstCapacity, + const void* dictStart, size_t dictSize) +{ + dstCapacity = MIN(targetOutputSize, dstCapacity); + return LZ4_decompress_generic(source, dest, compressedSize, dstCapacity, + partial_decode, usingExtDict, + (BYTE*)dest, (const BYTE*)dictStart, dictSize); +} + +LZ4_FORCE_O2 +static int LZ4_decompress_fast_extDict(const char* source, char* dest, int originalSize, + const void* dictStart, size_t dictSize) +{ + return LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + 0, (const BYTE*)dictStart, dictSize); +} + +/* The "double dictionary" mode, for use with e.g. ring buffers: the first part + * of the dictionary is passed as prefix, and the second via dictStart + dictSize. + * These routines are used only once, in LZ4_decompress_*_continue(). */ -LZ4_streamDecode_t *LZ4_createStreamDecode(void) +LZ4_FORCE_INLINE +int LZ4_decompress_safe_doubleDict(const char* source, char* dest, int compressedSize, int maxOutputSize, + size_t prefixSize, const void* dictStart, size_t dictSize) { - LZ4_streamDecode_t *lz4s = (LZ4_streamDecode_t *)ALLOCATOR(1, sizeof(LZ4_streamDecode_t)); - return lz4s; + return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, + decode_full_block, usingExtDict, + (BYTE*)dest-prefixSize, (const BYTE*)dictStart, dictSize); } -int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream) +/*===== streaming decompression functions =====*/ + +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +LZ4_streamDecode_t* LZ4_createStreamDecode(void) +{ + LZ4_STATIC_ASSERT(sizeof(LZ4_streamDecode_t) >= sizeof(LZ4_streamDecode_t_internal)); + return (LZ4_streamDecode_t*) ALLOC_AND_ZERO(sizeof(LZ4_streamDecode_t)); +} + +int LZ4_freeStreamDecode (LZ4_streamDecode_t* LZ4_stream) { + if (LZ4_stream == NULL) { return 0; } /* support free on NULL */ FREEMEM(LZ4_stream); return 0; } +#endif -/* - * LZ4_setStreamDecode - * Use this function to instruct where to find the dictionary - * This function is not necessary if previous data is still available where it was decoded. - * Loading a size of 0 is allowed (same effect as no dictionary). - * Return : 1 if OK, 0 if error +/*! LZ4_setStreamDecode() : + * Use this function to instruct where to find the dictionary. + * This function is not necessary if previous data is still available where it was decoded. + * Loading a size of 0 is allowed (same effect as no dictionary). + * @return : 1 if OK, 0 if error */ -int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize) +int LZ4_setStreamDecode (LZ4_streamDecode_t* LZ4_streamDecode, const char* dictionary, int dictSize) { - LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *)LZ4_streamDecode; + LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; lz4sd->prefixSize = (size_t)dictSize; - lz4sd->prefixEnd = (const BYTE *)dictionary + dictSize; + if (dictSize) { + assert(dictionary != NULL); + lz4sd->prefixEnd = (const BYTE*) dictionary + dictSize; + } else { + lz4sd->prefixEnd = (const BYTE*) dictionary; + } lz4sd->externalDict = NULL; - lz4sd->extDictSize = 0; + lz4sd->extDictSize = 0; return 1; } +/*! LZ4_decoderRingBufferSize() : + * when setting a ring buffer for streaming decompression (optional scenario), + * provides the minimum size of this ring buffer + * to be compatible with any source respecting maxBlockSize condition. + * Note : in a ring buffer scenario, + * blocks are presumed decompressed next to each other. + * When not enough space remains for next block (remainingSize < maxBlockSize), + * decoding resumes from beginning of ring buffer. + * @return : minimum ring buffer size, + * or 0 if there is an error (invalid maxBlockSize). + */ +int LZ4_decoderRingBufferSize(int maxBlockSize) +{ + if (maxBlockSize < 0) return 0; + if (maxBlockSize > LZ4_MAX_INPUT_SIZE) return 0; + if (maxBlockSize < 16) maxBlockSize = 16; + return LZ4_DECODER_RING_BUFFER_SIZE(maxBlockSize); +} + /* *_continue() : These decoding functions allow decompression of multiple blocks in "streaming" mode. @@ -1424,60 +2587,88 @@ int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictio If it's not possible, save the relevant part of decoded data into a safe buffer, and indicate where it stands using LZ4_setStreamDecode() */ -int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, - int compressedSize, int maxOutputSize) +LZ4_FORCE_O2 +int LZ4_decompress_safe_continue (LZ4_streamDecode_t* LZ4_streamDecode, const char* source, char* dest, int compressedSize, int maxOutputSize) { - LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *)LZ4_streamDecode; + LZ4_streamDecode_t_internal* lz4sd = &LZ4_streamDecode->internal_donotuse; int result; - if (lz4sd->prefixEnd == (BYTE *)dest) { - result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, - usingExtDict, lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, - lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize += result; - lz4sd->prefixEnd += result; + if (lz4sd->prefixSize == 0) { + /* The first call, no dictionary yet. */ + assert(lz4sd->extDictSize == 0); + result = LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); + if (result <= 0) return result; + lz4sd->prefixSize = (size_t)result; + lz4sd->prefixEnd = (BYTE*)dest + result; + } else if (lz4sd->prefixEnd == (BYTE*)dest) { + /* They're rolling the current segment. */ + if (lz4sd->prefixSize >= 64 KB - 1) + result = LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); + else if (lz4sd->extDictSize == 0) + result = LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, + lz4sd->prefixSize); + else + result = LZ4_decompress_safe_doubleDict(source, dest, compressedSize, maxOutputSize, + lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += (size_t)result; + lz4sd->prefixEnd += result; } else { + /* The buffer wraps around, or they're switching to another buffer. */ lz4sd->extDictSize = lz4sd->prefixSize; lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; - result = LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, - usingExtDict, (BYTE *)dest, lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize = result; - lz4sd->prefixEnd = (BYTE *)dest + result; + result = LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, + lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = (size_t)result; + lz4sd->prefixEnd = (BYTE*)dest + result; } return result; } -int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, int originalSize) +LZ4_FORCE_O2 int +LZ4_decompress_fast_continue (LZ4_streamDecode_t* LZ4_streamDecode, + const char* source, char* dest, int originalSize) { - LZ4_streamDecode_t_internal *lz4sd = (LZ4_streamDecode_t_internal *)LZ4_streamDecode; + LZ4_streamDecode_t_internal* const lz4sd = + (assert(LZ4_streamDecode!=NULL), &LZ4_streamDecode->internal_donotuse); int result; - if (lz4sd->prefixEnd == (BYTE *)dest) { - result = LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, usingExtDict, - lz4sd->prefixEnd - lz4sd->prefixSize, lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize += originalSize; - lz4sd->prefixEnd += originalSize; + DEBUGLOG(5, "LZ4_decompress_fast_continue (toDecodeSize=%i)", originalSize); + assert(originalSize >= 0); + + if (lz4sd->prefixSize == 0) { + DEBUGLOG(5, "first invocation : no prefix nor extDict"); + assert(lz4sd->extDictSize == 0); + result = LZ4_decompress_fast(source, dest, originalSize); + if (result <= 0) return result; + lz4sd->prefixSize = (size_t)originalSize; + lz4sd->prefixEnd = (BYTE*)dest + originalSize; + } else if (lz4sd->prefixEnd == (BYTE*)dest) { + DEBUGLOG(5, "continue using existing prefix"); + result = LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + lz4sd->prefixSize, + lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize += (size_t)originalSize; + lz4sd->prefixEnd += originalSize; } else { + DEBUGLOG(5, "prefix becomes extDict"); lz4sd->extDictSize = lz4sd->prefixSize; - lz4sd->externalDict = (BYTE *)dest - lz4sd->extDictSize; - result = LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, usingExtDict, - (BYTE *)dest, lz4sd->externalDict, lz4sd->extDictSize); - if (result <= 0) - return result; - lz4sd->prefixSize = originalSize; - lz4sd->prefixEnd = (BYTE *)dest + originalSize; + lz4sd->externalDict = lz4sd->prefixEnd - lz4sd->extDictSize; + result = LZ4_decompress_fast_extDict(source, dest, originalSize, + lz4sd->externalDict, lz4sd->extDictSize); + if (result <= 0) return result; + lz4sd->prefixSize = (size_t)originalSize; + lz4sd->prefixEnd = (BYTE*)dest + originalSize; } return result; } + /* Advanced decoding functions : *_usingDict() : @@ -1485,131 +2676,114 @@ Advanced decoding functions : the dictionary must be explicitly provided within parameters */ -FORCE_INLINE int LZ4_decompress_usingDict_generic(const char *source, char *dest, int compressedSize, int maxOutputSize, - int safe, const char *dictStart, int dictSize) -{ - if (dictSize == 0) - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, (BYTE *)dest, - NULL, 0); - if (dictStart + dictSize == dest) { - if (dictSize >= (int)(64 KB - 1)) - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, withPrefix64k, - (BYTE *)dest - 64 KB, NULL, 0); - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, noDict, - (BYTE *)dest - dictSize, NULL, 0); +int LZ4_decompress_safe_usingDict(const char* source, char* dest, int compressedSize, int maxOutputSize, const char* dictStart, int dictSize) +{ + if (dictSize==0) + return LZ4_decompress_safe(source, dest, compressedSize, maxOutputSize); + if (dictStart+dictSize == dest) { + if (dictSize >= 64 KB - 1) { + return LZ4_decompress_safe_withPrefix64k(source, dest, compressedSize, maxOutputSize); + } + assert(dictSize >= 0); + return LZ4_decompress_safe_withSmallPrefix(source, dest, compressedSize, maxOutputSize, (size_t)dictSize); } - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, safe, full, 0, usingExtDict, - (BYTE *)dest, (const BYTE *)dictStart, dictSize); + assert(dictSize >= 0); + return LZ4_decompress_safe_forceExtDict(source, dest, compressedSize, maxOutputSize, dictStart, (size_t)dictSize); } -int LZ4_decompress_safe_usingDict(const char *source, char *dest, int compressedSize, int maxOutputSize, - const char *dictStart, int dictSize) +int LZ4_decompress_safe_partial_usingDict(const char* source, char* dest, int compressedSize, int targetOutputSize, int dstCapacity, const char* dictStart, int dictSize) { - return LZ4_decompress_usingDict_generic(source, dest, compressedSize, maxOutputSize, 1, dictStart, dictSize); + if (dictSize==0) + return LZ4_decompress_safe_partial(source, dest, compressedSize, targetOutputSize, dstCapacity); + if (dictStart+dictSize == dest) { + if (dictSize >= 64 KB - 1) { + return LZ4_decompress_safe_partial_withPrefix64k(source, dest, compressedSize, targetOutputSize, dstCapacity); + } + assert(dictSize >= 0); + return LZ4_decompress_safe_partial_withSmallPrefix(source, dest, compressedSize, targetOutputSize, dstCapacity, (size_t)dictSize); + } + assert(dictSize >= 0); + return LZ4_decompress_safe_partial_forceExtDict(source, dest, compressedSize, targetOutputSize, dstCapacity, dictStart, (size_t)dictSize); } -int LZ4_decompress_fast_usingDict(const char *source, char *dest, int originalSize, const char *dictStart, int dictSize) +int LZ4_decompress_fast_usingDict(const char* source, char* dest, int originalSize, const char* dictStart, int dictSize) { - return LZ4_decompress_usingDict_generic(source, dest, 0, originalSize, 0, dictStart, dictSize); + if (dictSize==0 || dictStart+dictSize == dest) + return LZ4_decompress_unsafe_generic( + (const BYTE*)source, (BYTE*)dest, originalSize, + (size_t)dictSize, NULL, 0); + assert(dictSize >= 0); + return LZ4_decompress_fast_extDict(source, dest, originalSize, dictStart, (size_t)dictSize); } -/* debug function */ -int LZ4_decompress_safe_forceExtDict(const char *source, char *dest, int compressedSize, int maxOutputSize, - const char *dictStart, int dictSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, usingExtDict, - (BYTE *)dest, (const BYTE *)dictStart, dictSize); -} -/*************************************************** - * Obsolete Functions - ***************************************************/ +/*=************************************************* +* Obsolete Functions +***************************************************/ /* obsolete compression functions */ -int LZ4_compress_limitedOutput(const char *source, char *dest, int inputSize, int maxOutputSize) +int LZ4_compress_limitedOutput(const char* source, char* dest, int inputSize, int maxOutputSize) { return LZ4_compress_default(source, dest, inputSize, maxOutputSize); } -int LZ4_compress(const char *source, char *dest, int inputSize) +int LZ4_compress(const char* src, char* dest, int srcSize) { - return LZ4_compress_default(source, dest, inputSize, LZ4_compressBound(inputSize)); + return LZ4_compress_default(src, dest, srcSize, LZ4_compressBound(srcSize)); } -int LZ4_compress_limitedOutput_withState(void *state, const char *src, char *dst, int srcSize, int dstSize) +int LZ4_compress_limitedOutput_withState (void* state, const char* src, char* dst, int srcSize, int dstSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, dstSize, 1); } -int LZ4_compress_withState(void *state, const char *src, char *dst, int srcSize) +int LZ4_compress_withState (void* state, const char* src, char* dst, int srcSize) { return LZ4_compress_fast_extState(state, src, dst, srcSize, LZ4_compressBound(srcSize), 1); } -int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_stream, const char *src, char *dst, int srcSize, - int maxDstSize) +int LZ4_compress_limitedOutput_continue (LZ4_stream_t* LZ4_stream, const char* src, char* dst, int srcSize, int dstCapacity) { - return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, maxDstSize, 1); + return LZ4_compress_fast_continue(LZ4_stream, src, dst, srcSize, dstCapacity, 1); } -int LZ4_compress_continue(LZ4_stream_t *LZ4_stream, const char *source, char *dest, int inputSize) +int LZ4_compress_continue (LZ4_stream_t* LZ4_stream, const char* source, char* dest, int inputSize) { return LZ4_compress_fast_continue(LZ4_stream, source, dest, inputSize, LZ4_compressBound(inputSize), 1); } /* -These function names are deprecated and should no longer be used. +These decompression functions are deprecated and should no longer be used. They are only provided here for compatibility with older user programs. - LZ4_uncompress is totally equivalent to LZ4_decompress_fast - LZ4_uncompress_unknownOutputSize is totally equivalent to LZ4_decompress_safe */ -int LZ4_uncompress(const char *source, char *dest, int outputSize) +int LZ4_uncompress (const char* source, char* dest, int outputSize) { return LZ4_decompress_fast(source, dest, outputSize); } -int LZ4_uncompress_unknownOutputSize(const char *source, char *dest, int isize, int maxOutputSize) +int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize) { return LZ4_decompress_safe(source, dest, isize, maxOutputSize); } /* Obsolete Streaming functions */ -int LZ4_sizeofStreamState() { return LZ4_STREAMSIZE; } - -static void LZ4_init(LZ4_stream_t_internal *lz4ds, BYTE *base) -{ - MEM_INIT(lz4ds, 0, LZ4_STREAMSIZE); - lz4ds->bufferStart = base; -} +int LZ4_sizeofStreamState(void) { return sizeof(LZ4_stream_t); } -int LZ4_resetStreamState(void *state, char *inputBuffer) +int LZ4_resetStreamState(void* state, char* inputBuffer) { - if ((((size_t)state) & 3) != 0) - return 1; /* Error : pointer is not aligned on 4-bytes boundary */ - LZ4_init((LZ4_stream_t_internal *)state, (BYTE *)inputBuffer); + (void)inputBuffer; + LZ4_resetStream((LZ4_stream_t*)state); return 0; } -void *LZ4_create(char *inputBuffer) +#if !defined(LZ4_STATIC_LINKING_ONLY_DISABLE_MEMORY_ALLOCATION) +void* LZ4_create (char* inputBuffer) { - void *lz4ds = ALLOCATOR(8, LZ4_STREAMSIZE_U64); - LZ4_init((LZ4_stream_t_internal *)lz4ds, (BYTE *)inputBuffer); - return lz4ds; -} - -char *LZ4_slideInputBuffer(void *LZ4_Data) -{ - LZ4_stream_t_internal *ctx = (LZ4_stream_t_internal *)LZ4_Data; - int dictSize = LZ4_saveDict((LZ4_stream_t *)LZ4_Data, (char *)ctx->bufferStart, 64 KB); - return (char *)(ctx->bufferStart + dictSize); -} - -/* Obsolete streaming decompression functions */ - -int LZ4_decompress_safe_withPrefix64k(const char *source, char *dest, int compressedSize, int maxOutputSize) -{ - return LZ4_decompress_generic(source, dest, compressedSize, maxOutputSize, endOnInputSize, full, 0, withPrefix64k, - (BYTE *)dest - 64 KB, NULL, 64 KB); + (void)inputBuffer; + return LZ4_createStream(); } +#endif -int LZ4_decompress_fast_withPrefix64k(const char *source, char *dest, int originalSize) +char* LZ4_slideInputBuffer (void* state) { - return LZ4_decompress_generic(source, dest, 0, originalSize, endOnOutputSize, full, 0, withPrefix64k, - (BYTE *)dest - 64 KB, NULL, 64 KB); + /* avoid const char * -> char * conversion warning */ + return (char *)(uptrval)((LZ4_stream_t*)state)->internal_donotuse.dictionary; } -#endif /* LZ4_COMMONDEFS_ONLY */ +#endif /* LZ4_COMMONDEFS_ONLY */ diff --git a/libs/fst/lz4.h b/libs/fst/lz4.h index 929cf02cac7..7a2dbfd4bc4 100644 --- a/libs/fst/lz4.h +++ b/libs/fst/lz4.h @@ -1,7 +1,7 @@ /* - LZ4 - Fast LZ compression algorithm - Header File - Copyright (C) 2011-2015, Yann Collet. + * 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) @@ -28,340 +28,841 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - SPDX-License-Identifier: BSD-2-Clause - You can contact the author at : - - LZ4 source repository : https://github.com/Cyan4973/lz4 - - LZ4 public forum : https://groups.google.com/forum/#!forum/lz4c + - LZ4 homepage : http://www.lz4.org + - LZ4 source repository : https://github.com/lz4/lz4 */ -#pragma once - -#if defined(__cplusplus) +#if defined (__cplusplus) extern "C" { #endif +#ifndef LZ4_H_2983827168210 +#define LZ4_H_2983827168210 + +/* --- Dependency --- */ +#include /* 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.h provides block compression functions, and gives full buffer control to programmer. - * If you need to generate inter-operable compressed data (respecting LZ4 frame specification), - * and can let the library handle its own memory, please use lz4frame.h instead. +* 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 7 /* for new (non-breaking) interface capabilities */ -#define LZ4_VERSION_RELEASE 1 /* for tweaks, bug-fixes, or development */ -#define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR * 100 * 100 + LZ4_VERSION_MINOR * 100 + LZ4_VERSION_RELEASE) -int LZ4_versionNumber(void); - -/************************************** - * Tuning parameter - **************************************/ -/* + +/*------ 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; etc.) - * Increasing memory usage improves compression ratio - * Reduced memory usage can improve speed, due to cache effect - * Default value is 14, for 16KB, which nicely fits into Intel x86 L1 cache + * 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. */ -#define LZ4_MEMORY_USAGE 14 +#ifndef LZ4_MEMORY_USAGE +# define LZ4_MEMORY_USAGE LZ4_MEMORY_USAGE_DEFAULT +#endif -/************************************** - * Simple Functions - **************************************/ +#define LZ4_MEMORY_USAGE_MIN 10 +#define LZ4_MEMORY_USAGE_DEFAULT 14 +#define LZ4_MEMORY_USAGE_MAX 20 -int LZ4_compress_default(const char *source, char *dest, int sourceSize, int maxDestSize); -int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize); +#if (LZ4_MEMORY_USAGE < LZ4_MEMORY_USAGE_MIN) +# error "LZ4_MEMORY_USAGE is too small !" +#endif -/* -LZ4_compress_default() : - Compresses 'sourceSize' bytes from buffer 'source' - into already allocated 'dest' buffer of size 'maxDestSize'. - Compression is guaranteed to succeed if 'maxDestSize' >= LZ4_compressBound(sourceSize). - It also runs faster, so it's a recommended setting. - If the function cannot compress 'source' into a more limited 'dest' budget, - compression stops *immediately*, and the function result is zero. - As a consequence, 'dest' content is not valid. - This function never writes outside 'dest' buffer, nor read outside 'source' buffer. - sourceSize : Max supported value is LZ4_MAX_INPUT_VALUE - maxDestSize : full or partial size of buffer 'dest' (which must be already allocated) - return : the number of bytes written into buffer 'dest' (necessarily <= maxOutputSize) - or 0 if compression fails - -LZ4_decompress_safe() : - compressedSize : is the precise full size of the compressed block. - maxDecompressedSize : is the size of destination buffer, which must be already allocated. - return : the number of bytes decompressed into destination buffer (necessarily <= maxDecompressedSize) - If destination buffer is not large enough, decoding will stop and output an error code (<0). - If the source stream is detected malformed, the function will stop decoding and return a negative result. - This function is protected against buffer overflow exploits, including malicious data packets. - It never writes outside output buffer, nor reads outside input buffer. -*/ +#if (LZ4_MEMORY_USAGE > LZ4_MEMORY_USAGE_MAX) +# error "LZ4_MEMORY_USAGE is too large !" +#endif -/************************************** - * 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) +/*-************************************ +* 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); -/* -LZ4_compressBound() : + +/*-************************************ +* 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() compress faster when dest buffer size is >= LZ4_compressBound(srcSize) + 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 too large ( > LZ4_MAX_INPUT_SIZE) + or 0, if input size is incorrect (too large or negative) */ -int LZ4_compressBound(int inputSize); +LZ4LIB_API int LZ4_compressBound(int inputSize); -/* -LZ4_compress_fast() : - Same as LZ4_compress_default(), but allows to select an "acceleration" factor. +/*! 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 ACCELERATION_DEFAULT (see lz4.c), which is 1. + 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). */ -int LZ4_compress_fast(const char *source, char *dest, int sourceSize, int maxDestSize, int acceleration); +LZ4LIB_API int LZ4_compress_fast (const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); -/* -LZ4_compress_fast_extState() : - Same compression function, just using an externally allocated memory space to store compression state. - Use LZ4_sizeofState() to know how much memory must be allocated, - and allocate it on 8-bytes boundaries (using malloc() typically). - Then, provide it as 'void* state' to compression function. -*/ -int LZ4_sizeofState(void); -int LZ4_compress_fast_extState(void *state, const char *source, char *dest, int inputSize, int maxDestSize, - int acceleration); -/* -LZ4_compress_destSize() : - Reverse the logic, by compressing as much data as possible from 'source' buffer - into already allocated buffer 'dest' of size 'targetDestSize'. - This function either compresses the entire 'source' content into 'dest' if it's large enough, - or fill 'dest' buffer completely with as much data as possible from 'source'. - *sourceSizePtr : will be modified to indicate how many bytes where read from 'source' to fill 'dest'. - New value is necessarily <= old value. - return : Nb bytes written into 'dest' (necessarily <= targetDestSize) - or 0 if compression fails -*/ -int LZ4_compress_destSize(const char *source, char *dest, int *sourceSizePtr, int targetDestSize); +/*! 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); -/* -LZ4_decompress_fast() : - originalSize : is the original and therefore uncompressed size - return : the number of bytes read from the source buffer (in other words, the compressed size) - If the source stream is detected malformed, the function will stop decoding and return a negative result. - Destination buffer must be already allocated. Its size must be a minimum of 'originalSize' bytes. - note : This function fully respect memory boundaries for properly formed compressed data. - It is a bit faster than LZ4_decompress_safe(). - However, it does not provide any protection against intentionally modified data stream (malicious input). - Use this function in trusted environment only (data to decode comes from a trusted source). -*/ -int LZ4_decompress_fast(const char *source, char *dest, int originalSize); -/* -LZ4_decompress_safe_partial() : - This function decompress a compressed block of size 'compressedSize' at position 'source' - into destination buffer 'dest' of size 'maxDecompressedSize'. - The function tries to stop decompressing operation as soon as 'targetOutputSize' has been reached, - reducing decompression time. - return : the number of bytes decoded in the destination buffer (necessarily <= maxDecompressedSize) - Note : this number can be < 'targetOutputSize' should the compressed block to decode be smaller. - Always control how many bytes were decoded. - If the source stream is detected malformed, the function will stop decoding and return a negative result. - This function never writes outside of output buffer, and never reads outside of input buffer. It is -therefore protected against malicious data packets +/*-********************************************* +* 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". */ -int LZ4_decompress_safe_partial(const char *source, char *dest, int compressedSize, int targetOutputSize, - int maxDecompressedSize); - -/*********************************************** - * Streaming Compression Functions - ***********************************************/ -#define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE - 3)) + 4) -#define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(long long)) -/* - * LZ4_stream_t - * information structure to track an LZ4 stream. - * important : init this structure content before first use ! - * note : only allocated directly the structure if you are statically linking LZ4 - * If you are using liblz4 as a DLL, please use below construction methods instead. +#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. */ -typedef struct -{ - long long table[LZ4_STREAMSIZE_U64]; -} LZ4_stream_t; +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_resetStream - * Use this function to init an allocated LZ4_stream_t structure +/*! 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. */ -void LZ4_resetStream(LZ4_stream_t *streamPtr); +LZ4LIB_API int LZ4_saveDict (LZ4_stream_t* streamPtr, char* safeBuffer, int maxDictSize); -/* - * LZ4_createStream will allocate and initialize an LZ4_stream_t structure - * LZ4_freeStream releases its memory. - * In the context of a DLL (liblz4), please use these methods rather than the static struct. - * They are more future proof, in case of a change of LZ4_stream_t size. + +/*-********************************************** +* 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. */ -LZ4_stream_t *LZ4_createStream(void); -int LZ4_freeStream(LZ4_stream_t *streamPtr); +#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_loadDict - * Use this function to load a static dictionary into LZ4_stream. - * Any previous data will be forgotten, only 'dictionary' will remain in memory. - * Loading a size of 0 is allowed. - * Return : dictionary size, in bytes (necessarily <= 64 KB) +/*! 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 */ -int LZ4_loadDict(LZ4_stream_t *streamPtr, const char *dictionary, int dictSize); +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); -/* - * LZ4_compress_fast_continue - * Compress buffer content 'src', using data from previously compressed blocks as dictionary to improve compression - * ratio. Important : Previous data blocks are assumed to still be present and unmodified ! 'dst' buffer must be already - * allocated. If maxDstSize >= LZ4_compressBound(srcSize), compression is guaranteed to succeed, and runs faster. If - * not, and if compressed data cannot fit into 'dst' buffer size, compression stops, and function returns a zero. +#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(). */ -int LZ4_compress_fast_continue(LZ4_stream_t *streamPtr, const char *src, char *dst, int srcSize, int maxDstSize, - int acceleration); +LZ4LIB_STATIC_API int LZ4_compress_fast_extState_fastReset (void* state, const char* src, char* dst, int srcSize, int dstCapacity, int acceleration); -/* - * LZ4_saveDict - * If previously compressed data block is not guaranteed to remain available at its memory location - * save it into a safer place (char* safeBuffer) - * Note : you don't need to call LZ4_loadDict() afterwards, - * dictionary is immediately usable, you can therefore call LZ4_compress_fast_continue() - * Return : saved dictionary size in bytes (necessarily <= dictSize), or 0 if error +/*! LZ4_compress_destSize_extState() : + * Same as LZ4_compress_destSize(), but using an externally allocated state. + * Also: exposes @acceleration */ -int LZ4_saveDict(LZ4_stream_t *streamPtr, char *safeBuffer, int dictSize); - -/************************************************ - * Streaming Decompression Functions - ************************************************/ - -#define LZ4_STREAMDECODESIZE_U64 4 -#define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long)) -typedef struct -{ - unsigned long long table[LZ4_STREAMDECODESIZE_U64]; -} LZ4_streamDecode_t; -/* - * LZ4_streamDecode_t - * information structure to track an LZ4 stream. - * init this structure content using LZ4_setStreamDecode or memset() before first use ! - * - * In the context of a DLL (liblz4) please prefer usage of construction methods below. - * They are more future proof, in case of a change of LZ4_streamDecode_t size in the future. - * LZ4_createStreamDecode will allocate and initialize an LZ4_streamDecode_t structure - * LZ4_freeStreamDecode releases its memory. +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. */ -LZ4_streamDecode_t *LZ4_createStreamDecode(void); -int LZ4_freeStreamDecode(LZ4_streamDecode_t *LZ4_stream); +LZ4LIB_STATIC_API void +LZ4_attach_dictionary(LZ4_stream_t* workingStream, + const LZ4_stream_t* dictionaryStream); -/* - * LZ4_setStreamDecode - * Use this function to instruct where to find the dictionary. - * Setting a size of 0 is allowed (same effect as reset). - * Return : 1 if OK, 0 if error + +/*! 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. */ -int LZ4_setStreamDecode(LZ4_streamDecode_t *LZ4_streamDecode, const char *dictionary, int dictSize); -/* -*_continue() : - These decoding functions allow decompression of multiple blocks in "streaming" mode. - Previously decoded blocks *must* remain available at the memory position where they were decoded (up to 64 KB) - In the case of a ring buffers, decoding buffer must be either : - - Exactly same size as encoding buffer, with same update rule (block boundaries at same positions) - In which case, the decoding & encoding ring buffer can have any size, including very small ones ( < 64 KB). - - Larger than encoding buffer, by a minimum of maxBlockSize more bytes. - maxBlockSize is implementation dependent. It's the maximum size you intend to compress into a single block. - 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). - - _At least_ 64 KB + 8 bytes + maxBlockSize. - In which case, encoding and decoding buffers do not need to be synchronized, - and encoding ring buffer can have any size, including larger than decoding buffer. - Whenever these conditions are not possible, save the last 64KB of decoded data into a safe buffer, - and indicate where it is saved using LZ4_setStreamDecode() -*/ -int LZ4_decompress_safe_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, - int compressedSize, int maxDecompressedSize); -int LZ4_decompress_fast_continue(LZ4_streamDecode_t *LZ4_streamDecode, const char *source, char *dest, - int originalSize); +#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 */ -/* -Advanced decoding functions : -*_usingDict() : - These decoding functions work the same as - a combination of LZ4_setStreamDecode() followed by LZ4_decompress_x_continue() - They are stand-alone. They don't need nor update an LZ4_streamDecode_t structure. -*/ -int LZ4_decompress_safe_usingDict(const char *source, char *dest, int compressedSize, int maxDecompressedSize, - const char *dictStart, int dictSize); -int LZ4_decompress_fast_usingDict(const char *source, char *dest, int originalSize, const char *dictStart, - int dictSize); - -/************************************** - * Obsolete Functions - **************************************/ -/* Deprecate Warnings */ -/* Should these warnings messages be a problem, - it is generally possible to disable them, - with -Wno-deprecated-declarations for gcc - or _CRT_SECURE_NO_WARNINGS in Visual for example. - You can also define LZ4_DEPRECATE_WARNING_DEFBLOCK. */ -#ifndef LZ4_DEPRECATE_WARNING_DEFBLOCK -#define LZ4_DEPRECATE_WARNING_DEFBLOCK -#define LZ4_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) -#if (LZ4_GCC_VERSION >= 405) || defined(__clang__) -#define LZ4_DEPRECATED(message) __attribute__((deprecated(message))) -#elif (LZ4_GCC_VERSION >= 301) -#define LZ4_DEPRECATED(message) __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define LZ4_DEPRECATED(message) __declspec(deprecated(message)) +#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 + typedef int8_t LZ4_i8; + typedef uint8_t LZ4_byte; + typedef uint16_t LZ4_u16; + typedef uint32_t LZ4_u32; #else -#pragma message("WARNING: You need to implement LZ4_DEPRECATED for this compiler") -#define LZ4_DEPRECATED(message) + typedef signed char LZ4_i8; + typedef unsigned char LZ4_byte; + typedef unsigned short LZ4_u16; + typedef unsigned int LZ4_u32; #endif -#endif /* LZ4_DEPRECATE_WARNING_DEFBLOCK */ - -/* Obsolete compression functions */ -/* These functions are planned to start generate warnings by r131 approximately */ -int LZ4_compress(const char *source, char *dest, int sourceSize); -int LZ4_compress_limitedOutput(const char *source, char *dest, int sourceSize, int maxOutputSize); -int LZ4_compress_withState(void *state, const char *source, char *dest, int inputSize); -int LZ4_compress_limitedOutput_withState(void *state, const char *source, char *dest, int inputSize, int maxOutputSize); -int LZ4_compress_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize); -int LZ4_compress_limitedOutput_continue(LZ4_stream_t *LZ4_streamPtr, const char *source, char *dest, int inputSize, - int maxOutputSize); - -/* Obsolete decompression functions */ -/* These function names are completely deprecated and must no longer be used. - They are only provided here for compatibility with older programs. - - LZ4_uncompress is the same as LZ4_decompress_fast - - LZ4_uncompress_unknownOutputSize is the same as LZ4_decompress_safe - These function prototypes are now disabled; uncomment them only if you really need them. - It is highly recommended to stop using these prototypes and migrate to maintained ones */ -/* int LZ4_uncompress (const char* source, char* dest, int outputSize); */ -/* int LZ4_uncompress_unknownOutputSize (const char* source, char* dest, int isize, int maxOutputSize); */ - -/* Obsolete streaming functions; use new streaming interface whenever possible */ -LZ4_DEPRECATED("use LZ4_createStream() instead") void *LZ4_create(char *inputBuffer); -LZ4_DEPRECATED("use LZ4_createStream() instead") int LZ4_sizeofStreamState(void); -LZ4_DEPRECATED("use LZ4_resetStream() instead") int LZ4_resetStreamState(void *state, char *inputBuffer); -LZ4_DEPRECATED("use LZ4_saveDict() instead") char *LZ4_slideInputBuffer(void *state); - -/* Obsolete streaming decoding functions */ -LZ4_DEPRECATED("use LZ4_decompress_safe_usingDict() instead") -int LZ4_decompress_safe_withPrefix64k(const char *src, char *dst, int compressedSize, int maxDstSize); -LZ4_DEPRECATED("use LZ4_decompress_fast_usingDict() instead") -int LZ4_decompress_fast_withPrefix64k(const char *src, char *dst, int originalSize); - -#if defined(__cplusplus) + +/*! 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 diff --git a/libs/subcircuit/subcircuit.cc b/libs/subcircuit/subcircuit.cc index f38da3fcc57..60f27fd55e2 100644 --- a/libs/subcircuit/subcircuit.cc +++ b/libs/subcircuit/subcircuit.cc @@ -21,6 +21,7 @@ #include "subcircuit.h" #include +#include #include #include #include diff --git a/misc/py_wrap_generator.py b/misc/py_wrap_generator.py index 91a82081ae4..c62e624d2d2 100644 --- a/misc/py_wrap_generator.py +++ b/misc/py_wrap_generator.py @@ -375,6 +375,10 @@ class PoolTranslator(PythonListTranslator): insert_name = ".insert" orig_name = "pool" +#Sub-type for ObjRange +class ObjRangeTranslator(PythonListTranslator): + orig_name = "RTLIL::ObjRange" + #Translates dict-types (dict, std::map), that only differ in their name and #the name of the insertion function class PythonDictTranslator(Translator): @@ -536,13 +540,14 @@ def translate_cpp(c, varname, types, prefix, ref): #Associate the Translators with their c++ type known_containers = { - "std::set" : SetTranslator, - "std::vector" : VectorTranslator, - "pool" : PoolTranslator, - "idict" : IDictTranslator, - "dict" : DictTranslator, - "std::pair" : TupleTranslator, - "std::map" : MapTranslator + "std::set" : SetTranslator, + "std::vector" : VectorTranslator, + "pool" : PoolTranslator, + "idict" : IDictTranslator, + "dict" : DictTranslator, + "std::pair" : TupleTranslator, + "std::map" : MapTranslator, + "RTLIL::ObjRange" : ObjRangeTranslator } class Attribute: @@ -1268,6 +1273,11 @@ def from_string(str_def, containing_file, class_, line_number, namespace): func.duplicate = False func.namespace = namespace str_def = str_def.replace("operator ","operator") + + # remove attributes from the start + if str.startswith(str_def, "[[") and "]]" in str_def: + str_def = str_def[str_def.find("]]")+2:] + if str.startswith(str_def, "static "): func.is_static = True str_def = str_def[7:] @@ -1569,10 +1579,15 @@ def gen_default_impl(self): return_stmt = "return " if self.ret_type.name != "void" else "" text += ")\n\t\t{" - text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\"))" - text += f"\n\t\t\t\t{return_stmt}" + call_string - text += "\n\t\t\telse" + text += "\n\t\t\tif (boost::python::override py_" + self.alias + " = this->get_override(\"py_" + self.alias + "\")) {" + text += "\n\t\t\t\ttry {" + text += f"\n\t\t\t\t\t{return_stmt}" + call_string + text += "\n\t\t\t\t} catch (boost::python::error_already_set &) {" + text += "\n\t\t\t\t\tlog_python_exception_as_error();" + text += "\n\t\t\t\t}" + text += "\n\t\t\t} else {" text += f"\n\t\t\t\t{return_stmt}" + self.member_of.name + "::" + call_string + text += "\n\t\t\t}" text += "\n\t\t}" text += "\n\n\t\t" + self.ret_type.gen_text() + " default_py_" + self.alias + "(" @@ -2325,6 +2340,11 @@ def gen_wrappers(filename, debug_level_ = 0): namespace YOSYS_PYTHON { + [[noreturn]] static void log_python_exception_as_error() { + PyErr_Print(); + log_error("Python interpreter encountered an exception.\\n"); + } + struct YosysStatics{}; """) diff --git a/passes/cmds/Makefile.inc b/passes/cmds/Makefile.inc index af5451b8961..5a2af4df507 100644 --- a/passes/cmds/Makefile.inc +++ b/passes/cmds/Makefile.inc @@ -50,3 +50,6 @@ OBJS += passes/cmds/dft_tag.o OBJS += passes/cmds/future.o OBJS += passes/cmds/box_derive.o OBJS += passes/cmds/example_dt.o +OBJS += passes/cmds/portarcs.o +OBJS += passes/cmds/wrapcell.o +OBJS += passes/cmds/setenv.o diff --git a/passes/cmds/glift.cc b/passes/cmds/glift.cc index 8553b02a541..6a7d070d7de 100644 --- a/passes/cmds/glift.cc +++ b/passes/cmds/glift.cc @@ -184,7 +184,7 @@ struct GliftWorker { std::vector connections(module->connections()); for(auto &cell : module->cells().to_vector()) { - if (!cell->type.in({ID($_AND_), ID($_NAND_), ID($_OR_), ID($_NOR_), ID($_XOR_), ID($_XNOR_), ID($_MUX_), ID($_NMUX_), ID($_NOT_), ID($anyconst), ID($allconst), ID($assume), ID($assert)}) && module->design->module(cell->type) == nullptr) { + if (!cell->type.in(ID($_AND_), ID($_NAND_), ID($_OR_), ID($_NOR_), ID($_XOR_), ID($_XNOR_), ID($_MUX_), ID($_NMUX_), ID($_NOT_), ID($anyconst), ID($allconst), ID($assume), ID($assert)) && module->design->module(cell->type) == nullptr) { log_cmd_error("Unsupported cell type \"%s\" found. Run `techmap` first.\n", cell->type.c_str()); } if (cell->type.in(ID($_AND_), ID($_NAND_), ID($_OR_), ID($_NOR_))) { diff --git a/passes/cmds/portarcs.cc b/passes/cmds/portarcs.cc new file mode 100644 index 00000000000..a6ed75de3bd --- /dev/null +++ b/passes/cmds/portarcs.cc @@ -0,0 +1,312 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 Martin Povišer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + + +#include "kernel/timinginfo.h" +#include "kernel/rtlil.h" +#include "kernel/utils.h" +#include "kernel/celltypes.h" + +PRIVATE_NAMESPACE_BEGIN +USING_YOSYS_NAMESPACE + +static RTLIL::SigBit canonical_bit(RTLIL::SigBit bit) +{ + RTLIL::Wire *w; + while ((w = bit.wire) != NULL && !w->port_input && + w->driverCell()->type.in(ID($buf), ID($_BUF_))) { + bit = w->driverCell()->getPort(ID::A)[bit.offset]; + } + return bit; +} + +struct PortarcsPass : Pass { + PortarcsPass() : Pass("portarcs", "derive port arcs for propagation delay") {} + + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" portarcs [options] [selection]\n"); + log("\n"); + log("This command characterizes the combinational content of selected modules and\n"); + log("derives timing arcs going from module inputs to module outputs representing the\n"); + log("propagation delay of the module.\n"); + log("\n"); + log(" -draw\n"); + log(" plot the computed delay table to the terminal\n"); + log("\n"); + log(" -icells\n"); + log(" assign unit delay to gates from the internal Yosys cell library\n"); + log("\n"); + log(" -write\n"); + log(" write the computed arcs back into the module as $specify2 instances\n"); + log("\n"); + } + + void execute(std::vector args, RTLIL::Design *d) override + { + log_header(d, "Executing PORTARCS pass. (derive propagation arcs)\n"); + + size_t argidx; + bool icells_mode = false, write_mode = false, draw_mode = false; + for (argidx = 1; argidx < args.size(); argidx++) { + if (args[argidx] == "-icells") + icells_mode = true; + else if (args[argidx] == "-write") + write_mode = true; + else if (args[argidx] == "-draw") + draw_mode = true; + else + break; + } + extra_args(args, argidx, d); + + d->bufNormalize(true); + TimingInfo tinfo(d); + + if (icells_mode) { + CellTypes ct; + ct.setup_stdcells_eval(); + for (auto [id, type] : ct.cell_types) { + auto &tdata = tinfo.data[id]; + tdata.has_inputs = true; + for (auto inp : type.inputs) + for (auto out : type.outputs) + tdata.comb[TimingInfo::BitBit({inp, 0}, {out, 0})] = 1000; + } + } + + for (auto m : d->selected_whole_modules_warn()) { + bool ambiguous_ports = false; + SigSpec inputs, outputs; + for (auto port : m->ports) { + Wire *w = m->wire(port); + log_assert(w->port_input || w->port_output); + if (w->port_input && w->port_output) { + log_warning("Module '%s' with ambiguous direction on port %s ignored.\n", + log_id(m), log_id(w)); + ambiguous_ports = true; + break; + } + if (w->port_input) + inputs.append(w); + else + outputs.append(w); + } + if (ambiguous_ports) + continue; + + SigSpec ordering; + { + TopoSort sort; + + for (auto cell : m->cells()) + if (cell->type != ID($buf)) { + auto tdata = tinfo.find(cell->type); + if (tdata == tinfo.end()) + log_cmd_error("Missing timing data for module '%s'.\n", log_id(cell->type)); + for (auto [edge, delay] : tdata->second.comb) { + auto from = edge.first.get_connection(cell); + auto to = edge.second.get_connection(cell); + if (from && to) { + auto from_c = canonical_bit(from.value()); + if (from_c.wire) + sort.edge(from_c, to.value()); + } + } + } + + if (!sort.sort()) + log_error("Failed to sort instances in module %s.\n", log_id(m)); + + ordering = sort.sorted; + } + + dict annotations; + std::vector> allocated; + std::vector recycling; + + auto alloc_for_bit = [&](SigBit bit) { + if (!recycling.empty()) { + annotations[bit] = recycling.back(); + recycling.pop_back(); + } else { + int *p = new int[std::max(1, inputs.size())]; + allocated.emplace_back(p); + annotations[bit] = p; + } + }; + + for (auto bit : outputs) { + SigBit bit_c = canonical_bit(bit); + alloc_for_bit(bit_c); + + // consistency check + annotations.at(bit_c)[0] = (intptr_t) bit_c.wire; + } + + for (int i = ordering.size() - 1; i >= 0; i--) { + SigBit bit = ordering[i]; + + if (!bit.wire->port_input) { + auto cell = bit.wire->driverCell(); + auto tdata = tinfo.find(cell->type); + log_assert(tdata != tinfo.end()); + for (auto [edge, delay] : tdata->second.comb) { + auto from = edge.first.get_connection(cell); + auto to = edge.second.get_connection(cell); + if (from && to && to.value() == bit) { + auto from_c = canonical_bit(from.value()); + if (from_c.wire) { + if (!annotations.count(from_c)) { + alloc_for_bit(from_c); + + // consistency check + annotations.at(from_c)[0] = (intptr_t) from_c.wire; + } else { + // consistency check + log_assert(annotations.at(from_c)[0] == ((int) (intptr_t) from_c.wire)); + } + } + } + } + } + + if (annotations.count(bit)) { + // consistency check + log_assert(annotations.at(bit)[0] == ((int) (intptr_t) bit.wire)); + } else { + alloc_for_bit(bit); + } + + recycling.push_back(annotations.at(ordering[i])); + } + log_debug("Allocated %lux%d\n", allocated.size(), inputs.size()); + + for (auto bit : outputs) { + int *p = annotations.at(canonical_bit(bit)); + for (int i = 0; i < inputs.size(); i++) + p[i] = -1; + } + + for (int i = 0; i < ordering.size(); i++) { + SigBit bit = ordering[i]; + int *p = annotations.at(bit); + if (bit.wire->port_input) { + for (int j = 0; j < inputs.size(); j++) + p[j] = (j == i) ? 0 : -1; + } else { + for (int j = 0; j < inputs.size(); j++) + p[j] = -1; + + auto cell = ordering[i].wire->driverCell(); + auto tdata = tinfo.find(cell->type); + log_assert(tdata != tinfo.end()); + for (auto [edge, delay] : tdata->second.comb) { + auto from = edge.first.get_connection(cell); + auto to = edge.second.get_connection(cell); + if (from && to && to.value() == ordering[i]) { + auto from_c = canonical_bit(from.value()); + if (from_c.wire) { + int *q = annotations.at(from_c); + for (int j = 0; j < inputs.size(); j++) + if (q[j] >= 0) + p[j] = std::max(p[j], q[j] + delay); + } + } + } + } + } + + if (draw_mode) { + auto bit_str = [](SigBit bit) { + return stringf("%s%d", RTLIL::unescape_id(bit.wire->name.str()).c_str(), bit.offset); + }; + + std::vector headings; + int top_length = 0; + for (auto bit : inputs) { + headings.push_back(bit_str(bit)); + top_length = std::max(top_length, (int) headings.back().size()); + } + + int max_delay = 0; + for (auto bit : outputs) { + int *p = annotations.at(canonical_bit(bit)); + for (auto i = 0; i < inputs.size(); i++) + if (p[i] > max_delay) + max_delay = p[i]; + } + + log("Delay legend:\n\n"); + log(" "); + for (int i = 0; i < 24; i++) + log("\033[48;5;%dm ", 232+i); + log("\033[0m\n"); + log(" |%22s|\n", ""); + log(" 0%22s%d\n", "", max_delay); + log("\n"); + for (int k = top_length - 1; k >= 0; k--) { + log(" %10s ", ""); + for (auto &h : headings) + log("%c", (k < (int) h.size()) ? h[k] : ' '); + log("\n"); + } + log("\n"); + + for (auto bit : outputs) { + log(" %10s ", bit_str(bit).c_str()); + int *p = annotations.at(canonical_bit(bit)); + for (auto i = 0; i < inputs.size(); i++) + log("\033[48;5;%dm ", 232 + ((std::max(p[i], 0) * 24) - 1) / max_delay); + log("\033[0m\n"); + } + } + + if (write_mode) { + for (auto bit : outputs) { + int *p = annotations.at(canonical_bit(bit)); + for (auto i = 0; i < inputs.size(); i++) { + if (p[i] >= 0) { + Cell *spec = m->addCell(NEW_ID, ID($specify2)); + spec->setParam(ID::SRC_WIDTH, 1); + spec->setParam(ID::DST_WIDTH, 1); + spec->setParam(ID::T_FALL_MAX, p[i]); + spec->setParam(ID::T_FALL_TYP, p[i]); + spec->setParam(ID::T_FALL_MIN, p[i]); + spec->setParam(ID::T_RISE_MAX, p[i]); + spec->setParam(ID::T_RISE_TYP, p[i]); + spec->setParam(ID::T_RISE_MIN, p[i]); + spec->setParam(ID::SRC_DST_POL, false); + spec->setParam(ID::SRC_DST_PEN, false); + spec->setParam(ID::FULL, false); + spec->setPort(ID::EN, Const(1, 1)); + spec->setPort(ID::SRC, inputs[i]); + spec->setPort(ID::DST, bit); + } + } + } + } + } + d->bufNormalize(false); + } +} PortarcsPass; + +PRIVATE_NAMESPACE_END diff --git a/passes/cmds/setenv.cc b/passes/cmds/setenv.cc new file mode 100644 index 00000000000..27f2eea2894 --- /dev/null +++ b/passes/cmds/setenv.cc @@ -0,0 +1,58 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 N. Engelhardt + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ + +#include "kernel/register.h" +#include "kernel/rtlil.h" +#include "kernel/log.h" +#include + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN +struct SetenvPass : public Pass { + SetenvPass() : Pass("setenv", "set an environment variable") { } + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" setenv name value\n"); + log("\n"); + log("Set the given environment variable on the current process. Values containing\n"); + log("whitespace must be passed in double quotes (\").\n"); + log("\n"); + } + void execute(std::vector args, [[maybe_unused]] RTLIL::Design *design) override + { + if(args.size() != 3) + log_cmd_error("Wrong number of arguments given.\n"); + + std::string name = args[1]; + std::string value = args[2]; + if (value.front() == '\"' && value.back() == '\"') value = value.substr(1, value.size() - 2); + +#if defined(_WIN32) + _putenv_s(name.c_str(), value.c_str()); +#else + if (setenv(name.c_str(), value.c_str(), 1)) + log_cmd_error("Invalid name \"%s\".\n", name.c_str()); +#endif + + } +} SetenvPass; + +PRIVATE_NAMESPACE_END diff --git a/passes/cmds/wrapcell.cc b/passes/cmds/wrapcell.cc new file mode 100644 index 00000000000..0c15848e429 --- /dev/null +++ b/passes/cmds/wrapcell.cc @@ -0,0 +1,302 @@ +/* + * yosys -- Yosys Open SYnthesis Suite + * + * Copyright (C) 2024 Martin Povišer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + */ +#include "kernel/yosys.h" +#include "kernel/celltypes.h" +#include "kernel/sigtools.h" +#include "backends/rtlil/rtlil_backend.h" + +USING_YOSYS_NAMESPACE +PRIVATE_NAMESPACE_BEGIN + +bool has_fmt_field(std::string fmt, std::string field_name) +{ + auto it = fmt.begin(); + while (it != fmt.end()) { + if (*it == '{') { + it++; + auto beg = it; + while (it != fmt.end() && *it != '}') it++; + if (it == fmt.end()) + return false; + + if (std::string(beg, it) == field_name) + return true; + } + it++; + } + return false; +} + +struct ContextData { + std::string unused_outputs; +}; + +std::optional format(std::string fmt, const dict ¶meters, + const ContextData &context) +{ + std::stringstream result; + + auto it = fmt.begin(); + while (it != fmt.end()) { + if (*it == '{') { + it++; + auto beg = it; + while (it != fmt.end() && *it != '}') it++; + if (it == fmt.end()) { + log("Unclosed curly brackets in format string '%s'\n", fmt.c_str()); + return {}; + } + + std::string param_name = {beg, it}; + + if (param_name == "%unused") { + result << context.unused_outputs; + } else { + auto id = RTLIL::escape_id(std::string(beg, it)); + if (!parameters.count(id)) { + log("Parameter %s referenced in format string '%s' not found\n", log_id(id), fmt.c_str()); + return {}; + } + + RTLIL_BACKEND::dump_const(result, parameters.at(id)); + } + } else { + result << *it; + } + it++; + } + + return {result.str()}; +} + +struct Chunk { + IdString port; + int base, len; + + Chunk(IdString id, int base, int len) + : port(id), base(base), len(len) {} + + IdString format(Cell *cell) + { + if (len == cell->getPort(port).size()) + return port; + else if (len == 1) + return stringf("%s[%d]", port.c_str(), base); + else + return stringf("%s[%d:%d]", port.c_str(), base + len - 1, base); + } + + SigSpec sample(Cell *cell) + { + return cell->getPort(port).extract(base, len); + } +}; + +// Joins contiguous runs of bits into a 'Chunk' +std::vector collect_chunks(std::vector> bits) +{ + std::vector ret; + std::sort(bits.begin(), bits.end()); + for (auto it = bits.begin(); it != bits.end();) { + auto sep = it + 1; + for (; sep != bits.end() && + sep->first == it->first && + sep->second == (sep - 1)->second + 1; sep++); + ret.emplace_back(it->first, it->second, sep - it); + it = sep; + } + return ret; +} + +struct WrapcellPass : Pass { + WrapcellPass() : Pass("wrapcell", "wrap individual cells into new modules") {} + + void help() override + { + // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| + log("\n"); + log(" wrapcell -name [selection]\n"); + log("\n"); + log("This command wraps the selected cells individually into modules. The name for\n"); + log("each wrapper module is derived from the template by substituting\n"); + log("parameter values as specified in curly brackets. If the named module already\n"); + log("exists, it is reused.\n"); + log("\n"); + log("If the template contains the special string '{%%unused}', the command tracks\n"); + log("unused output ports -- specialized wrapper modules will be generated per every\n"); + log("distinct set of unused port bits as appearing on any selected cell.\n"); + log("\n"); + log(" -setattr \n"); + log(" set the given boolean attribute on each created wrapper module\n"); + log("\n"); + log(" -formatattr \n"); + log(" set a string attribute on the created wrapper module by substituting\n"); + log(" parameter values into \n"); + log("\n"); + log("Currently this command only supports wrapping internal cell types.\n"); + log("\n"); + } + + void execute(std::vector args, Design *d) override + { + log_header(d, "Executing WRAPCELL pass. (wrap selected cells)\n"); + + struct AttrRule { + IdString name; + std::string value_fmt; + + AttrRule(IdString name, std::string value_fmt) + : name(name), value_fmt(value_fmt) {} + }; + std::vector attributes; + std::string name_fmt; + + size_t argidx; + for (argidx = 1; argidx < args.size(); argidx++) { + if (args[argidx] == "-setattr" && argidx+1 < args.size()) { + attributes.emplace_back(RTLIL::escape_id(args[++argidx]), ""); + } else if (args[argidx] == "-formatattr" && argidx+2 < args.size()) { + IdString id = RTLIL::escape_id(args[++argidx]); + attributes.emplace_back(id, args[++argidx]); + } else if (args[argidx] == "-name" && argidx+1 < args.size()) { + name_fmt = args[++argidx]; + } else { + break; + } + } + extra_args(args, argidx, d); + + if (name_fmt.empty()) + log_cmd_error("Argument -name required"); + + CellTypes ct; + ct.setup(); + + bool tracking_unused = has_fmt_field(name_fmt, "%unused"); + + for (auto module : d->selected_modules()) { + SigPool unused; + + for (auto wire : module->wires()) + if (wire->has_attribute(ID::unused_bits)) { + std::string str = wire->get_string_attribute(ID::unused_bits); + for (auto it = str.begin(); it != str.end();) { + auto sep = it; + for (; sep != str.end() && *sep != ' '; sep++); + unused.add(SigBit(wire, std::stoi(std::string(it, sep)))); + for (it = sep; it != str.end() && *it == ' '; it++); + } + } + + for (auto cell : module->selected_cells()) { + Module *subm; + Cell *subcell; + + if (!ct.cell_known(cell->type)) + log_error("Non-internal cell type '%s' on cell '%s' in module '%s' unsupported\n", + log_id(cell->type), log_id(cell), log_id(module)); + + std::vector> unused_outputs, used_outputs; + for (auto conn : cell->connections()) { + if (ct.cell_output(cell->type, conn.first)) + for (int i = 0; i < conn.second.size(); i++) { + if (tracking_unused && unused.check(conn.second[i])) + unused_outputs.emplace_back(conn.first, i); + else + used_outputs.emplace_back(conn.first, i); + } + } + + ContextData context; + if (!unused_outputs.empty()) { + context.unused_outputs += "_unused"; + for (auto chunk : collect_chunks(unused_outputs)) + context.unused_outputs += "_" + RTLIL::unescape_id(chunk.format(cell)); + } + + std::optional unescaped_name = format(name_fmt, cell->parameters, context); + if (!unescaped_name) + log_error("Formatting error when processing cell '%s' in module '%s'\n", + log_id(cell), log_id(module)); + + IdString name = RTLIL::escape_id(unescaped_name.value()); + if (d->module(name)) + goto replace_cell; + + subm = d->addModule(name); + subcell = subm->addCell("$1", cell->type); + for (auto conn : cell->connections()) { + if (ct.cell_output(cell->type, conn.first)) { + // Insert marker bits as placehodlers which need to be replaced + subcell->setPort(conn.first, SigSpec(RTLIL::Sm, conn.second.size())); + } else { + Wire *w = subm->addWire(conn.first, conn.second.size()); + w->port_input = true; + subcell->setPort(conn.first, w); + } + } + + for (auto chunk : collect_chunks(used_outputs)) { + Wire *w = subm->addWire(chunk.format(cell), chunk.len); + w->port_output = true; + subcell->connections_[chunk.port].replace(chunk.base, w); + } + + for (auto chunk : collect_chunks(unused_outputs)) { + Wire *w = subm->addWire(chunk.format(cell), chunk.len); + subcell->connections_[chunk.port].replace(chunk.base, w); + } + + subcell->parameters = cell->parameters; + subm->fixup_ports(); + + for (auto rule : attributes) { + if (rule.value_fmt.empty()) { + subm->set_bool_attribute(rule.name); + } else { + std::optional value = format(rule.value_fmt, cell->parameters, context); + + if (!value) + log_error("Formatting error when processing cell '%s' in module '%s'\n", + log_id(cell), log_id(module)); + + subm->set_string_attribute(rule.name, value.value()); + } + } + + replace_cell: + cell->parameters.clear(); + + dict new_connections; + + for (auto conn : cell->connections()) + if (!ct.cell_output(cell->type, conn.first)) + new_connections[conn.first] = conn.second; + + for (auto chunk : collect_chunks(used_outputs)) + new_connections[chunk.format(cell)] = chunk.sample(cell); + + cell->type = name; + cell->connections_ = new_connections; + } + } + } +} WrapcellPass; + +PRIVATE_NAMESPACE_END diff --git a/passes/fsm/fsm_map.cc b/passes/fsm/fsm_map.cc index 02ce7f4964b..711a81db21c 100644 --- a/passes/fsm/fsm_map.cc +++ b/passes/fsm/fsm_map.cc @@ -54,7 +54,7 @@ static void implement_pattern_cache(RTLIL::Module *module, std::map= 0) diff --git a/passes/hierarchy/hierarchy.cc b/passes/hierarchy/hierarchy.cc index a8d7d5a53ef..8372c0339c1 100644 --- a/passes/hierarchy/hierarchy.cc +++ b/passes/hierarchy/hierarchy.cc @@ -1003,8 +1003,10 @@ struct HierarchyPass : public Pass { if (top_mod == nullptr) for (auto mod : design->modules()) - if (mod->get_bool_attribute(ID::top)) + if (mod->get_bool_attribute(ID::top)) { + log("Attribute `top' found on module `%s'. Setting top module to %s.\n", log_id(mod), log_id(mod)); top_mod = mod; + } if (top_mod == nullptr) { diff --git a/passes/hierarchy/keep_hierarchy.cc b/passes/hierarchy/keep_hierarchy.cc index 6b947e67370..bfae9fa3831 100644 --- a/passes/hierarchy/keep_hierarchy.cc +++ b/passes/hierarchy/keep_hierarchy.cc @@ -23,20 +23,80 @@ USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN +struct ThresholdHierarchyKeeping { + Design *design; + CellCosts costs; + dict done; + pool in_progress; + uint64_t threshold; + + ThresholdHierarchyKeeping(Design *design, uint64_t threshold) + : design(design), costs(design), threshold(threshold) {} + + uint64_t visit(RTLIL::Module *module) { + if (module->has_attribute(ID(gate_cost_equivalent))) + return module->attributes[ID(gate_cost_equivalent)].as_int(); + + if (module->has_attribute(ID(keep_hierarchy))) + return 0; + + if (module->get_blackbox_attribute()) + log_error("Missing cost information on instanced blackbox %s\n", log_id(module)); + + if (done.count(module)) + return done.at(module); + + if (in_progress.count(module)) + log_error("Circular hierarchy\n"); + in_progress.insert(module); + + uint64_t size = 0; + module->has_processes_warn(); + + for (auto cell : module->cells()) { + if (!cell->type.isPublic()) { + size += costs.get(cell); + } else { + RTLIL::Module *submodule = design->module(cell->type); + if (!submodule) + log_error("Hierarchy contains unknown module '%s' (instanced as %s in %s)\n", + log_id(cell->type), log_id(cell), log_id(module)); + size += visit(submodule); + } + } + + if (size > threshold) { + log("Keeping %s (estimated size above threshold: %llu > %llu).\n", log_id(module), size, threshold); + module->set_bool_attribute(ID::keep_hierarchy); + size = 0; + } + + in_progress.erase(module); + done[module] = size; + return size; + } +}; + struct KeepHierarchyPass : public Pass { - KeepHierarchyPass() : Pass("keep_hierarchy", "add the keep_hierarchy attribute") {} + KeepHierarchyPass() : Pass("keep_hierarchy", "selectively add the keep_hierarchy attribute") {} void help() override { // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---| log("\n"); - log(" keep_hierarchy [options]\n"); + log(" keep_hierarchy [options] [selection]\n"); log("\n"); log("Add the keep_hierarchy attribute.\n"); log("\n"); log(" -min_cost \n"); - log(" only add the attribute to modules estimated to have more\n"); - log(" than gates after simple techmapping. Intended\n"); - log(" for tuning trade-offs between quality and yosys runtime.\n"); + log(" only add the attribute to modules estimated to have more than \n"); + log(" gates after simple techmapping. Intended for tuning trade-offs between\n"); + log(" quality and yosys runtime.\n"); + log("\n"); + log(" When evaluating a module's cost, gates which are within a submodule\n"); + log(" which is marked with the keep_hierarchy attribute are not counted\n"); + log(" towards the upper module's cost. This applies to both when the attribute\n"); + log(" was added by this command or was pre-existing.\n"); + log("\n"); } void execute(std::vector args, RTLIL::Design *design) override { @@ -54,16 +114,15 @@ struct KeepHierarchyPass : public Pass { } extra_args(args, argidx, design); - CellCosts costs(design); + if (min_cost) { + RTLIL::Module *top = design->top_module(); + if (!top) + log_cmd_error("'-min_cost' mode requires a single top module in the design\n"); - for (auto module : design->selected_modules()) { - if (min_cost) { - unsigned int cost = costs.get(module); - if (cost > min_cost) { - log("Marking %s (module too big: %d > %d).\n", log_id(module), cost, min_cost); - module->set_bool_attribute(ID::keep_hierarchy); - } - } else { + ThresholdHierarchyKeeping worker(design, min_cost); + worker.visit(top); + } else { + for (auto module : design->selected_modules()) { log("Marking %s.\n", log_id(module)); module->set_bool_attribute(ID::keep_hierarchy); } diff --git a/passes/opt/opt_demorgan.cc b/passes/opt/opt_demorgan.cc index 4db3a810105..1a2c1fe8265 100644 --- a/passes/opt/opt_demorgan.cc +++ b/passes/opt/opt_demorgan.cc @@ -39,6 +39,10 @@ void demorgan_worker( return; auto insig = sigmap(cell->getPort(ID::A)); + + if (GetSize(insig) < 1) + return; + log("Inspecting %s cell %s (%d inputs)\n", log_id(cell->type), log_id(cell->name), GetSize(insig)); int num_inverted = 0; for(int i=0; itype == ID($reduce_or)) ? State::S0 : State::S1; + if (new_sig_a != sig_a || sig_a.size() != cell->getPort(ID::A).size()) { log(" New input vector for %s cell %s: %s\n", cell->type.c_str(), cell->name.c_str(), log_signal(new_sig_a)); did_something = true; diff --git a/passes/pmgen/Makefile.inc b/passes/pmgen/Makefile.inc index 84d9ebaf121..6fa7d1fd740 100644 --- a/passes/pmgen/Makefile.inc +++ b/passes/pmgen/Makefile.inc @@ -57,6 +57,7 @@ PEEPOPT_PATTERN = passes/pmgen/peepopt_shiftmul_right.pmg PEEPOPT_PATTERN += passes/pmgen/peepopt_shiftmul_left.pmg PEEPOPT_PATTERN += passes/pmgen/peepopt_shiftadd.pmg PEEPOPT_PATTERN += passes/pmgen/peepopt_muldiv.pmg +PEEPOPT_PATTERN += passes/pmgen/peepopt_formal_clockgateff.pmg passes/pmgen/peepopt_pm.h: passes/pmgen/pmgen.py $(PEEPOPT_PATTERN) $(P) mkdir -p passes/pmgen && $(PYTHON_EXECUTABLE) $< -o $@ -p peepopt $(filter-out $<,$^) diff --git a/passes/pmgen/peepopt.cc b/passes/pmgen/peepopt.cc index 5638ec3c29c..5b678ee556d 100644 --- a/passes/pmgen/peepopt.cc +++ b/passes/pmgen/peepopt.cc @@ -40,7 +40,7 @@ struct PeepoptPass : public Pass { log("\n"); log("This pass applies a collection of peephole optimizers to the current design.\n"); log("\n"); - log("This pass employs the following rules:\n"); + log("This pass employs the following rules by default:\n"); log("\n"); log(" * muldiv - Replace (A*B)/B with A\n"); log("\n"); @@ -57,14 +57,26 @@ struct PeepoptPass : public Pass { log(" limits the amount of padding to a multiple of the data, \n"); log(" to avoid high resource usage from large temporary MUX trees.\n"); log("\n"); + log("If -formalclk is specified it instead employs the following rules:\n"); + log("\n"); + log(" * clockgateff - Replace latch based clock gating patterns with a flip-flop\n"); + log(" based pattern to prevent combinational paths from the\n"); + log(" output to the enable input after running clk2fflogic.\n"); + log("\n"); } void execute(std::vector args, RTLIL::Design *design) override { log_header(design, "Executing PEEPOPT pass (run peephole optimizers).\n"); + bool formalclk = false; + size_t argidx; for (argidx = 1; argidx < args.size(); argidx++) { + if (args[argidx] == "-formalclk") { + formalclk = true; + continue; + } break; } extra_args(args, argidx, design); @@ -86,10 +98,14 @@ struct PeepoptPass : public Pass { pm.setup(module->selected_cells()); - pm.run_shiftadd(); - pm.run_shiftmul_right(); - pm.run_shiftmul_left(); - pm.run_muldiv(); + if (formalclk) { + pm.run_formal_clockgateff(); + } else { + pm.run_shiftadd(); + pm.run_shiftmul_right(); + pm.run_shiftmul_left(); + pm.run_muldiv(); + } } } } diff --git a/passes/pmgen/peepopt_formal_clockgateff.pmg b/passes/pmgen/peepopt_formal_clockgateff.pmg new file mode 100644 index 00000000000..835f68bd86d --- /dev/null +++ b/passes/pmgen/peepopt_formal_clockgateff.pmg @@ -0,0 +1,59 @@ +pattern formal_clockgateff + +// Detects the most common clock gating pattern using a latch and replaces it +// with a functionally equivalent pattern based on a flip-flop. The latch +// based pattern has a combinational path from the enable input to output after +// clk2fflogic, but this is a stable loop and the flip-flop based pattern does +// not exhibit this. +// +// This optimization is suitable for formal to prevent false comb loops, but +// should not be used for synthesis where the latch is an intentional choice +// +// Latch style: +// always @* if (!clk_i) latched_en = en; +// assign gated_clk_o = latched_en & clk_i; +// +// Flip-flop style: +// always @(posedge clk) flopped_en <= en; +// assign gated_clk_o = flopped_en & clk_i; + +state clk en latched_en gated_clk +state latched_en_port_name + +match latch + select latch->type == $dlatch + select param(latch, \WIDTH) == 1 + select param(latch, \EN_POLARITY).as_bool() == false + set clk port(latch, \EN) + set en port(latch, \D) + set latched_en port(latch, \Q) +endmatch + +match and_gate + select and_gate->type.in($and, $logic_and) + select param(and_gate, \A_WIDTH) == 1 + select param(and_gate, \B_WIDTH) == 1 + select param(and_gate, \Y_WIDTH) == 1 + choice clk_port {\A, \B} + define latch_port {clk_port == \A ? \B : \A} + index port(and_gate, clk_port) === clk + index port(and_gate, latch_port) === latched_en + set gated_clk port(and_gate, \Y) + set latched_en_port_name latch_port +endmatch + +code + log("replacing clock gate pattern in %s with ff: latch=%s, and=%s\n", + log_id(module), log_id(latch), log_id(and_gate)); + + // Add a flip-flop and rewire the AND gate to use the output of this flop + // instead of the latch. We don't delete the latch in case its output is + // used to drive other nodes. If it isn't, it will be trivially removed by + // clean + SigSpec flopped_en = module->addWire(NEW_ID); + module->addDff(NEW_ID, clk, en, flopped_en, true, latch->get_src_attribute()); + and_gate->setPort(latched_en_port_name, flopped_en); + did_something = true; + + accept; +endcode diff --git a/passes/pmgen/peepopt_shiftadd.pmg b/passes/pmgen/peepopt_shiftadd.pmg index ecd43355fe6..58dbefc1260 100644 --- a/passes/pmgen/peepopt_shiftadd.pmg +++ b/passes/pmgen/peepopt_shiftadd.pmg @@ -53,6 +53,11 @@ match add select port(add, constport).is_fully_const() define varport (constport == \A ? \B : \A) + // only optimize for constants up to a fixed width. this prevents cases + // with a blowup in internal term size and prevents larger constants being + // casted to int incorrectly + select (GetSize(port(add, constport)) <= 24) + // if a value of var is able to wrap the output, the transformation might give wrong results // an addition/substraction can at most flip one more bit than the largest operand (the carry bit) // as long as the output can show this bit, no wrap should occur (assuming all signed-ness make sense) diff --git a/passes/proc/proc_dff.cc b/passes/proc/proc_dff.cc index 3aad9ac1c6e..d094abf1bd0 100644 --- a/passes/proc/proc_dff.cc +++ b/passes/proc/proc_dff.cc @@ -262,7 +262,7 @@ void proc_dff(RTLIL::Module *mod, RTLIL::Process *proc, ConstEval &ce) { log_warning("Complex async reset for dff `%s'.\n", log_signal(sig)); gen_dffsr_complex(mod, insig, sig, sync_edge->signal, sync_edge->type == RTLIL::SyncType::STp, async_rules, proc); - return; + continue; } // If there is a reset condition in the async rules, use it @@ -277,7 +277,7 @@ void proc_dff(RTLIL::Module *mod, RTLIL::Process *proc, ConstEval &ce) sync_edge->type == RTLIL::SyncType::STp, sync_level && sync_level->type == RTLIL::SyncType::ST1, sync_edge->signal, sync_level->signal, proc); - return; + continue; } gen_dff(mod, insig, rstval.as_const(), sig_q, diff --git a/passes/sat/clk2fflogic.cc b/passes/sat/clk2fflogic.cc index bcefa7d8f92..348bab72721 100644 --- a/passes/sat/clk2fflogic.cc +++ b/passes/sat/clk2fflogic.cc @@ -51,6 +51,10 @@ struct Clk2fflogicPass : public Pass { log(" -nolower\n"); log(" Do not automatically run 'chformal -lower' to lower $check cells.\n"); log("\n"); + log(" -nopeepopt\n"); + log(" Do not automatically run 'peepopt -formalclk' to rewrite clock patterns\n"); + log(" to more formal friendly forms.\n"); + log("\n"); } // Active-high sampled and current value of a level-triggered control signal. Initial sampled values is low/non-asserted. SampledSig sample_control(Module *module, SigSpec sig, bool polarity, bool is_fine) { @@ -121,6 +125,7 @@ struct Clk2fflogicPass : public Pass { void execute(std::vector args, RTLIL::Design *design) override { bool flag_nolower = false; + bool flag_nopeepopt = false; log_header(design, "Executing CLK2FFLOGIC pass (convert clocked FFs to generic $ff cells).\n"); @@ -131,10 +136,20 @@ struct Clk2fflogicPass : public Pass { flag_nolower = true; continue; } + if (args[argidx] == "-nopeepopt") { + flag_nopeepopt = true; + continue; + } break; } extra_args(args, argidx, design); + if (!flag_nopeepopt) { + log_push(); + Pass::call(design, "peepopt -formalclk"); + log_pop(); + } + bool have_check_cells = false; for (auto module : design->selected_modules()) diff --git a/passes/techmap/abc9_ops.cc b/passes/techmap/abc9_ops.cc index 378f29042b3..4c7667a435f 100644 --- a/passes/techmap/abc9_ops.cc +++ b/passes/techmap/abc9_ops.cc @@ -969,13 +969,10 @@ void prep_box(RTLIL::Design *design) if (it == module->attributes.end()) continue; bool box = it->second.as_bool(); - module->attributes.erase(it); if (!box) continue; auto r = module->attributes.insert(ID::abc9_box_id); - if (!r.second) - continue; r.first->second = abc9_box_id++; if (module->get_bool_attribute(ID::abc9_flop)) { @@ -1078,7 +1075,8 @@ void prep_box(RTLIL::Design *design) } ss << log_id(module) << " " << module->attributes.at(ID::abc9_box_id).as_int(); - ss << " " << (module->get_bool_attribute(ID::whitebox) ? "1" : "0"); + bool has_model = module->get_bool_attribute(ID::whitebox) || !module->get_bool_attribute(ID::blackbox); + ss << " " << (has_model ? "1" : "0"); ss << " " << GetSize(inputs) << " " << GetSize(outputs) << std::endl; bool first = true; @@ -1096,8 +1094,9 @@ void prep_box(RTLIL::Design *design) ss << std::endl; auto &t = timing.setup_module(module); - if (t.comb.empty()) + if (t.comb.empty() && !outputs.empty() && !inputs.empty()) { log_error("Module '%s' with (* abc9_box *) has no timing (and thus no connectivity) information.\n", log_id(module)); + } for (const auto &o : outputs) { first = true; diff --git a/passes/techmap/abc_new.cc b/passes/techmap/abc_new.cc index eefe34f84c7..5be8239163e 100644 --- a/passes/techmap/abc_new.cc +++ b/passes/techmap/abc_new.cc @@ -19,10 +19,29 @@ #include "kernel/register.h" #include "kernel/rtlil.h" +#include "kernel/utils.h" USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN +std::vector order_modules(Design *design, std::vector modules) +{ + std::set modules_set(modules.begin(), modules.end()); + TopoSort sort; + + for (auto m : modules) { + sort.node(m); + + for (auto cell : m->cells()) { + Module *submodule = design->module(cell->type); + if (modules_set.count(submodule)) + sort.edge(submodule, m); + } + } + log_assert(sort.sort()); + return sort.sorted; +} + struct AbcNewPass : public ScriptPass { AbcNewPass() : ScriptPass("abc_new", "(experimental) use ABC for SC technology mapping (new)") { @@ -101,6 +120,15 @@ struct AbcNewPass : public ScriptPass { } if (check_label("prep_boxes")) { + if (!help_mode) { + for (auto mod : active_design->selected_whole_modules_warn()) { + if (mod->get_bool_attribute(ID::abc9_box)) { + mod->set_bool_attribute(ID::abc9_box, false); + mod->set_bool_attribute(ID(abc9_deferred_box), true); + } + } + } + run("box_derive"); run("abc9_ops -prep_box"); } @@ -109,7 +137,8 @@ struct AbcNewPass : public ScriptPass { std::vector selected_modules; if (!help_mode) { - selected_modules = active_design->selected_whole_modules_warn(); + selected_modules = order_modules(active_design, + active_design->selected_whole_modules_warn()); active_design->selection_stack.emplace_back(false); } else { selected_modules = {nullptr}; @@ -131,15 +160,36 @@ struct AbcNewPass : public ScriptPass { active_design->selection().select(mod); } + std::string script_save; + if (!help_mode && mod->has_attribute(ID(abc9_script))) { + script_save = active_design->scratchpad_get_string("abc9.script"); + active_design->scratchpad_set_string("abc9.script", + mod->get_string_attribute(ID(abc9_script))); + } + run(stringf(" abc9_ops -write_box %s/input.box", tmpdir.c_str())); run(stringf(" write_xaiger2 -mapping_prep -map2 %s/input.map2 %s/input.xaig", tmpdir.c_str(), tmpdir.c_str())); run(stringf(" abc9_exe %s -cwd %s -box %s/input.box", exe_options.c_str(), tmpdir.c_str(), tmpdir.c_str())); run(stringf(" read_xaiger2 -sc_mapping -module_name %s -map2 %s/input.map2 %s/output.aig", modname.c_str(), tmpdir.c_str(), tmpdir.c_str())); + if (!help_mode && mod->has_attribute(ID(abc9_script))) { + if (script_save.empty()) + active_design->scratchpad_unset("abc9.script"); + else + active_design->scratchpad_set_string("abc9.script", script_save); + } + if (!help_mode) { active_design->selection().selected_modules.clear(); log_pop(); + + if (mod->get_bool_attribute(ID(abc9_deferred_box))) { + mod->set_bool_attribute(ID(abc9_deferred_box), false); + mod->set_bool_attribute(ID::abc9_box, true); + Pass::call_on_module(active_design, mod, "portarcs -draw -write"); + run("abc9_ops -prep_box"); + } } } diff --git a/passes/techmap/booth.cc b/passes/techmap/booth.cc index 09c20b50712..dce7da486f5 100644 --- a/passes/techmap/booth.cc +++ b/passes/techmap/booth.cc @@ -57,6 +57,7 @@ synth -top my_design -booth #include "kernel/sigtools.h" #include "kernel/yosys.h" +#include "kernel/macc.h" USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN @@ -207,12 +208,33 @@ struct BoothPassWorker { void run() { for (auto cell : module->selected_cells()) { - if (cell->type != ID($mul)) + SigSpec A, B, Y; + bool is_signed; + + if (cell->type == ID($mul)) { + A = cell->getPort(ID::A); + B = cell->getPort(ID::B); + Y = cell->getPort(ID::Y); + + log_assert(cell->getParam(ID::A_SIGNED).as_bool() == cell->getParam(ID::B_SIGNED).as_bool()); + is_signed = cell->getParam(ID::A_SIGNED).as_bool(); + } else if (cell->type == ID($macc)) { + Macc macc; + macc.from_cell(cell); + + if (!macc.is_simple_product()) { + log_debug("Not mapping cell %s: not a simple macc cell\n", log_id(cell)); + continue; + } + + A = macc.ports[0].in_a; + B = macc.ports[0].in_b; + is_signed = macc.ports[0].is_signed; + Y = cell->getPort(ID::Y); + } else { continue; + } - SigSpec A = cell->getPort(ID::A); - SigSpec B = cell->getPort(ID::B); - SigSpec Y = cell->getPort(ID::Y); int x_sz = GetSize(A), y_sz = GetSize(B), z_sz = GetSize(Y); if (x_sz < 4 || y_sz < 4 || z_sz < 8) { @@ -221,9 +243,6 @@ struct BoothPassWorker { continue; } - log_assert(cell->getParam(ID::A_SIGNED).as_bool() == cell->getParam(ID::B_SIGNED).as_bool()); - bool is_signed = cell->getParam(ID::A_SIGNED).as_bool(); - log("Mapping cell %s to %s Booth multiplier\n", log_id(cell), is_signed ? "signed" : "unsigned"); // To simplify the generator size the arguments diff --git a/passes/techmap/bufnorm.cc b/passes/techmap/bufnorm.cc index 3c2cd923027..a4552c71bd4 100644 --- a/passes/techmap/bufnorm.cc +++ b/passes/techmap/bufnorm.cc @@ -404,6 +404,17 @@ struct BufnormPass : public Pass { pool added_buffers; + const auto lookup_mapping = [&mapped_bits](const SigBit bit, bool default_sx = false) + { + if (!bit.is_wire()) + return bit; + + if (default_sx) + return mapped_bits.at(bit, State::Sx); + + return mapped_bits.at(bit); + }; + auto make_buffer_f = [&](const IdString &type, const SigSpec &src, const SigSpec &dst) { auto it = old_buffers.find(pair(type, dst)); @@ -439,7 +450,8 @@ struct BufnormPass : public Pass { SigSpec keysig = sigmap(wire), insig = wire, outsig = wire; for (int i = 0; i < GetSize(insig); i++) - insig[i] = mapped_bits.at(keysig[i], State::Sx); + insig[i] = lookup_mapping(keysig[i], true); + if (chain_this_wire) { for (int i = 0; i < GetSize(outsig); i++) mapped_bits[keysig[i]] = outsig[i]; @@ -486,7 +498,7 @@ struct BufnormPass : public Pass { SigSpec newsig = conn.second; for (auto &bit : newsig) - bit = mapped_bits[sigmap(bit)]; + bit = lookup_mapping(sigmap(bit)); if (conn.second != newsig) { log(" fixing input signal on cell %s port %s: %s\n", diff --git a/passes/techmap/clockgate.cc b/passes/techmap/clockgate.cc index bf53b02bbc8..44fdc7e47aa 100644 --- a/passes/techmap/clockgate.cc +++ b/passes/techmap/clockgate.cc @@ -1,5 +1,6 @@ #include "kernel/yosys.h" #include "kernel/ff.h" +#include "libparse.h" #include USING_YOSYS_NAMESPACE @@ -10,6 +11,7 @@ struct ClockGateCell { IdString ce_pin; IdString clk_in_pin; IdString clk_out_pin; + std::vector tie_lo_pins; }; ClockGateCell icg_from_arg(std::string& name, std::string& str) { @@ -37,6 +39,152 @@ ClockGateCell icg_from_arg(std::string& name, std::string& str) { return c; } +static std::pair, std::optional> + find_icgs(std::vector cells, std::vector const& dont_use_cells) { + // We will pick the most suitable ICG absed on tie_lo count and area + struct ICGRankable : public ClockGateCell { double area; }; + std::optional best_pos; + std::optional best_neg; + + // This is a lot of boilerplate, isn't it? + for (auto cell : cells) + { + const LibertyAst *dn = cell->find("dont_use"); + if (dn != nullptr && dn->value == "true") + continue; + + bool dont_use = false; + for (auto dont_use_cell : dont_use_cells) + { + if (patmatch(dont_use_cell.c_str(), cell->args[0].c_str())) + { + dont_use = true; + break; + } + } + if (dont_use) + continue; + + const LibertyAst *icg_kind_ast = cell->find("clock_gating_integrated_cell"); + if (icg_kind_ast == nullptr) + continue; + + auto cell_name = cell->args[0]; + auto icg_kind = icg_kind_ast->value; + auto starts_with = [&](std::string prefix) { + return icg_kind.compare(0, prefix.size(), prefix) == 0; + }; + bool clk_pol; + if (icg_kind == "latch_posedge" || starts_with("latch_posedge_")) { + clk_pol = true; + } else if (icg_kind == "latch_negedge" || starts_with("latch_negedge_")) { + clk_pol = false; + } else { + log("Ignoring ICG primitive %s of kind '%s'\n", cell_name.c_str(), icg_kind.c_str()); + continue; + } + + log_debug("maybe valid icg: %s\n", cell_name.c_str()); + ClockGateCell icg_interface; + icg_interface.name = RTLIL::escape_id(cell_name); + + for (auto pin : cell->children) { + if (pin->id != "pin" || pin->args.size() != 1) + continue; + + if (pin->find("clock_gate_clock_pin")) { + if (!icg_interface.clk_in_pin.empty()) { + log_warning("Malformed liberty file - multiple clock_gate_clock_pin in cell %s\n", + cell_name.c_str()); + continue; + } else + icg_interface.clk_in_pin = RTLIL::escape_id(pin->args[0]); + } else if (pin->find("clock_gate_out_pin")) { + if (!icg_interface.clk_out_pin.empty()) { + log_warning("Malformed liberty file - multiple clock_gate_out_pin in cell %s\n", + cell_name.c_str()); + continue; + } else + icg_interface.clk_out_pin = RTLIL::escape_id(pin->args[0]); + } else if (pin->find("clock_gate_enable_pin")) { + if (!icg_interface.ce_pin.empty()) { + log_warning("Malformed liberty file - multiple clock_gate_enable_pin in cell %s\n", + cell_name.c_str()); + continue; + } else + icg_interface.ce_pin = RTLIL::escape_id(pin->args[0]); + } else if (pin->find("clock_gate_test_pin")) { + icg_interface.tie_lo_pins.push_back(RTLIL::escape_id(pin->args[0])); + } else { + const LibertyAst *dir = pin->find("direction"); + if (dir->value == "internal") + continue; + + log_warning("Malformed liberty file - extra pin %s in cell %s\n", + pin->args[0].c_str(), cell_name.c_str()); + continue; + } + } + + if (icg_interface.clk_in_pin.empty()) { + log_warning("Malformed liberty file - missing clock_gate_clock_pin in cell %s", + cell_name.c_str()); + continue; + } + if (icg_interface.clk_out_pin.empty()) { + log_warning("Malformed liberty file - missing clock_gate_out_pin in cell %s", + cell_name.c_str()); + continue; + } + if (icg_interface.ce_pin.empty()) { + log_warning("Malformed liberty file - missing clock_gate_enable_pin in cell %s", + cell_name.c_str()); + continue; + } + + double area = 0; + const LibertyAst *ar = cell->find("area"); + if (ar != nullptr && !ar->value.empty()) + area = atof(ar->value.c_str()); + + std::optional& icg_to_beat = clk_pol ? best_pos : best_neg; + + bool winning = false; + if (icg_to_beat) { + log_debug("ties: %zu ? %zu\n", icg_to_beat->tie_lo_pins.size(), + icg_interface.tie_lo_pins.size()); + log_debug("area: %f ? %f\n", icg_to_beat->area, area); + + // Prefer fewer test enables over area reduction (unlikely to matter) + auto goal = std::make_pair(icg_to_beat->tie_lo_pins.size(), icg_to_beat->area); + auto cost = std::make_pair(icg_interface.tie_lo_pins.size(), area); + winning = cost < goal; + + if (winning) + log_debug("%s beats %s\n", icg_interface.name.c_str(), icg_to_beat->name.c_str()); + } else { + log_debug("%s is the first of its polarity\n", icg_interface.name.c_str()); + winning = true; + } + if (winning) { + ICGRankable new_icg {icg_interface, area}; + icg_to_beat.emplace(new_icg); + } + } + + std::optional pos; + std::optional neg; + if (best_pos) { + log("Selected rising edge ICG %s from Liberty file\n", best_pos->name.c_str()); + pos.emplace(*best_pos); + } + if (best_neg) { + log("Selected falling edge ICG %s from Liberty file\n", best_neg->name.c_str()); + neg.emplace(*best_neg); + } + return std::make_pair(pos, neg); +} + struct ClockgatePass : public Pass { ClockgatePass() : Pass("clockgate", "extract clock gating out of flip flops") { } void help() override { @@ -60,12 +208,20 @@ struct ClockgatePass : public Pass { log(" user-specified ICG (integrated clock gating)\n"); log(" cell with ports named , , .\n"); log(" The ICG's clock enable pin must be active high.\n"); + log(" -liberty \n"); + log(" If specified, ICGs will be selected from the liberty files\n"); + log(" if available. Priority is given to cells with fewer tie_lo\n"); + log(" inputs and smaller size. This removes the need to manually\n"); + log(" specify -pos or -neg and -tie_lo.\n"); + log(" -dont_use \n"); + log(" Cells won't be considered when searching for ICGs\n"); + log(" in the liberty file specified by -liberty.\n"); log(" -tie_lo \n"); log(" Port of the ICG will be tied to zero.\n"); log(" Intended for DFT scan-enable pins.\n"); log(" -min_net_size \n"); log(" Only transform sets of at least eligible FFs.\n"); - // log(" \n"); + log(" \n"); } // One ICG will be generated per ClkNetInfo @@ -110,7 +266,9 @@ struct ClockgatePass : public Pass { std::optional pos_icg_desc; std::optional neg_icg_desc; - std::vector tie_lo_ports; + std::vector tie_lo_pins; + std::vector liberty_files; + std::vector dont_use_cells; int min_net_size = 0; size_t argidx; @@ -119,17 +277,54 @@ struct ClockgatePass : public Pass { auto name = args[++argidx]; auto rest = args[++argidx]; pos_icg_desc = icg_from_arg(name, rest); + continue; } if (args[argidx] == "-neg" && argidx+2 < args.size()) { auto name = args[++argidx]; auto rest = args[++argidx]; neg_icg_desc = icg_from_arg(name, rest); + continue; } if (args[argidx] == "-tie_lo" && argidx+1 < args.size()) { - tie_lo_ports.push_back(RTLIL::escape_id(args[++argidx])); + tie_lo_pins.push_back(RTLIL::escape_id(args[++argidx])); + continue; + } + if (args[argidx] == "-liberty" && argidx+1 < args.size()) { + std::string liberty_file = args[++argidx]; + rewrite_filename(liberty_file); + liberty_files.push_back(liberty_file); + continue; + } + if (args[argidx] == "-dont_use" && argidx+1 < args.size()) { + dont_use_cells.push_back(args[++argidx]); + continue; } if (args[argidx] == "-min_net_size" && argidx+1 < args.size()) { min_net_size = atoi(args[++argidx].c_str()); + continue; + } + break; + } + + if (!liberty_files.empty()) { + LibertyMergedCells merged; + for (auto path : liberty_files) { + std::ifstream f; + f.open(path.c_str()); + if (f.fail()) + log_cmd_error("Can't open liberty file `%s': %s\n", path.c_str(), strerror(errno)); + LibertyParser p(f); + merged.merge(p); + f.close(); + } + std::tie(pos_icg_desc, neg_icg_desc) = + find_icgs(merged.cells, dont_use_cells); + } else { + for (auto pin : tie_lo_pins) { + if (pos_icg_desc) + pos_icg_desc->tie_lo_pins.push_back(pin); + if (neg_icg_desc) + neg_icg_desc->tie_lo_pins.push_back(pin); } } @@ -185,7 +380,7 @@ struct ClockgatePass : public Pass { gclk.new_net = module->addWire(NEW_ID); icg->setPort(matching_icg_desc->clk_out_pin, gclk.new_net); // Tie low DFT ports like scan chain enable - for (auto port : tie_lo_ports) + for (auto port : matching_icg_desc->tie_lo_pins) icg->setPort(port, Const(0, 1)); // Fix CE polarity if needed if (!clk.pol_ce) { diff --git a/passes/techmap/dfflibmap.cc b/passes/techmap/dfflibmap.cc index 43eef1ac04a..b475cdd2d1a 100644 --- a/passes/techmap/dfflibmap.cc +++ b/passes/techmap/dfflibmap.cc @@ -66,6 +66,11 @@ static void logmap_all() logmap(ID($_DFF_PP0_)); logmap(ID($_DFF_PP1_)); + logmap(ID($_DFFE_NN_)); + logmap(ID($_DFFE_NP_)); + logmap(ID($_DFFE_PN_)); + logmap(ID($_DFFE_PP_)); + logmap(ID($_DFFSR_NNN_)); logmap(ID($_DFFSR_NNP_)); logmap(ID($_DFFSR_NPN_)); @@ -76,6 +81,115 @@ static void logmap_all() logmap(ID($_DFFSR_PPP_)); } +static bool parse_next_state(const LibertyAst *cell, const LibertyAst *attr, std::string &data_name, bool &data_not_inverted, std::string &enable_name, bool &enable_not_inverted) +{ + static pool warned_cells{}; + + if (cell == nullptr || attr == nullptr || attr->value.empty()) + return false; + + auto expr = attr->value; + auto cell_name = cell->args[0]; + + for (size_t pos = expr.find_first_of("\" \t"); pos != std::string::npos; pos = expr.find_first_of("\" \t")) + expr.erase(pos, 1); + + // if this isn't an enable flop, the next_state variable is usually just the input pin name. + if (expr[expr.size()-1] == '\'') { + data_name = expr.substr(0, expr.size()-1); + data_not_inverted = false; + } else if (expr[0] == '!') { + data_name = expr.substr(1, expr.size()-1); + data_not_inverted = false; + } else { + data_name = expr; + data_not_inverted = true; + } + + for (auto child : cell->children) + if (child->id == "pin" && child->args.size() == 1 && child->args[0] == data_name) + return true; + + // the next_state variable isn't just a pin name; perhaps this is an enable? + auto helper = LibertyExpression::Lexer(expr); + auto tree = LibertyExpression::parse(helper); + + if (tree.kind == LibertyExpression::Kind::EMPTY) { + if (!warned_cells.count(cell_name)) { + log_debug("Invalid expression '%s' in next_state attribute of cell '%s' - skipping.\n", expr.c_str(), cell_name.c_str()); + warned_cells.insert(cell_name); + } + return false; + } + + auto pin_names = pool{}; + tree.get_pin_names(pin_names); + + // from the `ff` block, we know the flop output signal name for loopback. + auto ff = cell->find("ff"); + if (ff == nullptr || ff->args.size() != 2) + return false; + auto ff_output = ff->args.at(0); + + // This test is redundant with the one in enable_pin, but we're in a + // position that gives better diagnostics here. + if (!pin_names.count(ff_output)) { + if (!warned_cells.count(cell_name)) { + log_debug("Inference failed on expression '%s' in next_state attribute of cell '%s' because it does not contain ff output '%s' - skipping.\n", expr.c_str(), cell_name.c_str(), ff_output.c_str()); + warned_cells.insert(cell_name); + } + return false; + } + + data_not_inverted = true; + data_name = ""; + enable_not_inverted = true; + enable_name = ""; + + if (pin_names.size() == 3 && pin_names.count(ff_output)) { + pin_names.erase(ff_output); + auto pins = std::vector(pin_names.begin(), pin_names.end()); + int lut = 0; + for (int n = 0; n < 8; n++) { + auto values = dict{}; + values.insert(std::make_pair(pins[0], (n & 1) == 1)); + values.insert(std::make_pair(pins[1], (n & 2) == 2)); + values.insert(std::make_pair(ff_output, (n & 4) == 4)); + if (tree.eval(values)) + lut |= 1 << n; + } + // the ff output Q is in a known bit location, so we now just have to compare the LUT mask to known values to find the enable pin and polarity. + if (lut == 0xD8) { + data_name = pins[1]; + enable_name = pins[0]; + return true; + } + if (lut == 0xB8) { + data_name = pins[0]; + enable_name = pins[1]; + return true; + } + enable_not_inverted = false; + if (lut == 0xE4) { + data_name = pins[1]; + enable_name = pins[0]; + return true; + } + if (lut == 0xE2) { + data_name = pins[0]; + enable_name = pins[1]; + return true; + } + // this does not match an enable flop. + } + + if (!warned_cells.count(cell_name)) { + log_debug("Inference failed on expression '%s' in next_state attribute of cell '%s' because it does not evaluate to an enable flop - skipping.\n", expr.c_str(), cell_name.c_str()); + warned_cells.insert(cell_name); + } + return false; +} + static bool parse_pin(const LibertyAst *cell, const LibertyAst *attr, std::string &pin_name, bool &pin_pol) { if (cell == nullptr || attr == nullptr || attr->value.empty()) @@ -106,16 +220,16 @@ static bool parse_pin(const LibertyAst *cell, const LibertyAst *attr, std::strin For now, we'll simply produce a warning to let the user know something is up. */ if (pin_name.find_first_of("^*|&") == std::string::npos) { - log_warning("Malformed liberty file - cannot find pin '%s' in cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str()); + log_debug("Malformed liberty file - cannot find pin '%s' in cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str()); } else { - log_warning("Found unsupported expression '%s' in pin attribute of cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str()); + log_debug("Found unsupported expression '%s' in pin attribute of cell '%s' - skipping.\n", pin_name.c_str(), cell->args[0].c_str()); } return false; } -static void find_cell(const LibertyAst *ast, IdString cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval, std::vector &dont_use_cells) +static void find_cell(std::vector cells, IdString cell_type, bool clkpol, bool has_reset, bool rstpol, bool rstval, bool has_enable, bool enapol, std::vector &dont_use_cells) { const LibertyAst *best_cell = nullptr; std::map best_cell_ports; @@ -123,14 +237,8 @@ static void find_cell(const LibertyAst *ast, IdString cell_type, bool clkpol, bo bool best_cell_noninv = false; double best_cell_area = 0; - if (ast->id != "library") - log_error("Format error in liberty file.\n"); - - for (auto cell : ast->children) + for (auto cell : cells) { - if (cell->id != "cell" || cell->args.size() != 1) - continue; - const LibertyAst *dn = cell->find("dont_use"); if (dn != nullptr && dn->value == "true") continue; @@ -151,12 +259,12 @@ static void find_cell(const LibertyAst *ast, IdString cell_type, bool clkpol, bo if (ff == nullptr) continue; - std::string cell_clk_pin, cell_rst_pin, cell_next_pin; - bool cell_clk_pol, cell_rst_pol, cell_next_pol; + std::string cell_clk_pin, cell_rst_pin, cell_next_pin, cell_enable_pin; + bool cell_clk_pol, cell_rst_pol, cell_next_pol, cell_enable_pol; if (!parse_pin(cell, ff->find("clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol) continue; - if (!parse_pin(cell, ff->find("next_state"), cell_next_pin, cell_next_pol)) + if (!parse_next_state(cell, ff->find("next_state"), cell_next_pin, cell_next_pol, cell_enable_pin, cell_enable_pol) || (has_enable && (cell_enable_pin.empty() || cell_enable_pol != enapol))) continue; if (has_reset && rstval == false) { if (!parse_pin(cell, ff->find("clear"), cell_rst_pin, cell_rst_pol) || cell_rst_pol != rstpol) @@ -171,6 +279,8 @@ static void find_cell(const LibertyAst *ast, IdString cell_type, bool clkpol, bo this_cell_ports[cell_clk_pin] = 'C'; if (has_reset) this_cell_ports[cell_rst_pin] = 'R'; + if (has_enable) + this_cell_ports[cell_enable_pin] = 'E'; this_cell_ports[cell_next_pin] = 'D'; double area = 0; @@ -239,7 +349,7 @@ static void find_cell(const LibertyAst *ast, IdString cell_type, bool clkpol, bo } } -static void find_cell_sr(const LibertyAst *ast, IdString cell_type, bool clkpol, bool setpol, bool clrpol, std::vector &dont_use_cells) +static void find_cell_sr(std::vector cells, IdString cell_type, bool clkpol, bool setpol, bool clrpol, bool has_enable, bool enapol, std::vector &dont_use_cells) { const LibertyAst *best_cell = nullptr; std::map best_cell_ports; @@ -247,14 +357,10 @@ static void find_cell_sr(const LibertyAst *ast, IdString cell_type, bool clkpol, bool best_cell_noninv = false; double best_cell_area = 0; - if (ast->id != "library") - log_error("Format error in liberty file.\n"); + log_assert(!enapol && "set/reset cell with enable is unimplemented due to lack of cells for testing"); - for (auto cell : ast->children) + for (auto cell : cells) { - if (cell->id != "cell" || cell->args.size() != 1) - continue; - const LibertyAst *dn = cell->find("dont_use"); if (dn != nullptr && dn->value == "true") continue; @@ -275,12 +381,12 @@ static void find_cell_sr(const LibertyAst *ast, IdString cell_type, bool clkpol, if (ff == nullptr) continue; - std::string cell_clk_pin, cell_set_pin, cell_clr_pin, cell_next_pin; - bool cell_clk_pol, cell_set_pol, cell_clr_pol, cell_next_pol; + std::string cell_clk_pin, cell_set_pin, cell_clr_pin, cell_next_pin, cell_enable_pin; + bool cell_clk_pol, cell_set_pol, cell_clr_pol, cell_next_pol, cell_enable_pol; if (!parse_pin(cell, ff->find("clocked_on"), cell_clk_pin, cell_clk_pol) || cell_clk_pol != clkpol) continue; - if (!parse_pin(cell, ff->find("next_state"), cell_next_pin, cell_next_pol)) + if (!parse_next_state(cell, ff->find("next_state"), cell_next_pin, cell_next_pol, cell_enable_pin, cell_enable_pol)) continue; if (!parse_pin(cell, ff->find("preset"), cell_set_pin, cell_set_pol) || cell_set_pol != setpol) continue; @@ -291,6 +397,8 @@ static void find_cell_sr(const LibertyAst *ast, IdString cell_type, bool clkpol, this_cell_ports[cell_clk_pin] = 'C'; this_cell_ports[cell_set_pin] = 'S'; this_cell_ports[cell_clr_pin] = 'R'; + if (has_enable) + this_cell_ports[cell_enable_pin] = 'E'; this_cell_ports[cell_next_pin] = 'D'; double area = 0; @@ -441,7 +549,7 @@ struct DfflibmapPass : public Pass { log(" dfflibmap [-prepare] [-map-only] [-info] [-dont_use ] -liberty [selection]\n"); log("\n"); log("Map internal flip-flop cells to the flip-flop cells in the technology\n"); - log("library specified in the given liberty file.\n"); + log("library specified in the given liberty files.\n"); log("\n"); log("This pass may add inverters as needed. Therefore it is recommended to\n"); log("first run this pass and then map the logic paths to the target technology.\n"); @@ -470,11 +578,11 @@ struct DfflibmapPass : public Pass { log_header(design, "Executing DFFLIBMAP pass (mapping DFF cells to sequential cells from liberty file).\n"); log_push(); - std::string liberty_file; bool prepare_mode = false; bool map_only_mode = false; bool info_mode = false; + std::vector liberty_files; std::vector dont_use_cells; size_t argidx; @@ -482,8 +590,9 @@ struct DfflibmapPass : public Pass { { std::string arg = args[argidx]; if (arg == "-liberty" && argidx+1 < args.size()) { - liberty_file = args[++argidx]; + std::string liberty_file = args[++argidx]; rewrite_filename(liberty_file); + liberty_files.push_back(liberty_file); continue; } if (arg == "-prepare") { @@ -516,36 +625,45 @@ struct DfflibmapPass : public Pass { if (modes > 1) log_cmd_error("Only one of -prepare, -map-only, or -info options should be given!\n"); - if (liberty_file.empty()) + if (liberty_files.empty()) log_cmd_error("Missing `-liberty liberty_file' option!\n"); - std::ifstream f; - f.open(liberty_file.c_str()); - if (f.fail()) - log_cmd_error("Can't open liberty file `%s': %s\n", liberty_file.c_str(), strerror(errno)); - LibertyParser libparser(f); - f.close(); - - find_cell(libparser.ast, ID($_DFF_N_), false, false, false, false, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_P_), true, false, false, false, dont_use_cells); - - find_cell(libparser.ast, ID($_DFF_NN0_), false, true, false, false, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_NN1_), false, true, false, true, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_NP0_), false, true, true, false, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_NP1_), false, true, true, true, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_PN0_), true, true, false, false, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_PN1_), true, true, false, true, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_PP0_), true, true, true, false, dont_use_cells); - find_cell(libparser.ast, ID($_DFF_PP1_), true, true, true, true, dont_use_cells); - - find_cell_sr(libparser.ast, ID($_DFFSR_NNN_), false, false, false, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_NNP_), false, false, true, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_NPN_), false, true, false, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_NPP_), false, true, true, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_PNN_), true, false, false, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_PNP_), true, false, true, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_PPN_), true, true, false, dont_use_cells); - find_cell_sr(libparser.ast, ID($_DFFSR_PPP_), true, true, true, dont_use_cells); + LibertyMergedCells merged; + for (auto path : liberty_files) { + std::ifstream f; + f.open(path.c_str()); + if (f.fail()) + log_cmd_error("Can't open liberty file `%s': %s\n", path.c_str(), strerror(errno)); + LibertyParser p(f); + merged.merge(p); + f.close(); + } + + find_cell(merged.cells, ID($_DFF_N_), false, false, false, false, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_P_), true, false, false, false, false, false, dont_use_cells); + + find_cell(merged.cells, ID($_DFF_NN0_), false, true, false, false, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_NN1_), false, true, false, true, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_NP0_), false, true, true, false, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_NP1_), false, true, true, true, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_PN0_), true, true, false, false, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_PN1_), true, true, false, true, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_PP0_), true, true, true, false, false, false, dont_use_cells); + find_cell(merged.cells, ID($_DFF_PP1_), true, true, true, true, false, false, dont_use_cells); + + find_cell(merged.cells, ID($_DFFE_NN_), false, false, false, false, true, false, dont_use_cells); + find_cell(merged.cells, ID($_DFFE_NP_), false, false, false, false, true, true, dont_use_cells); + find_cell(merged.cells, ID($_DFFE_PN_), true, false, false, false, true, false, dont_use_cells); + find_cell(merged.cells, ID($_DFFE_PP_), true, false, false, false, true, true, dont_use_cells); + + find_cell_sr(merged.cells, ID($_DFFSR_NNN_), false, false, false, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_NNP_), false, false, true, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_NPN_), false, true, false, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_NPP_), false, true, true, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_PNN_), true, false, false, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_PNP_), true, false, true, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_PPN_), true, true, false, false, false, dont_use_cells); + find_cell_sr(merged.cells, ID($_DFFSR_PPP_), true, true, true, false, false, dont_use_cells); log(" final dff cell mappings:\n"); logmap_all(); diff --git a/passes/techmap/libparse.cc b/passes/techmap/libparse.cc index ebfb325fcd5..06dd6288e33 100644 --- a/passes/techmap/libparse.cc +++ b/passes/techmap/libparse.cc @@ -80,6 +80,152 @@ void LibertyAst::dump(FILE *f, sieve &blacklist, sieve &whitelist, std::string i fprintf(f, " ;\n"); } +#ifndef FILTERLIB +// https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html +LibertyExpression LibertyExpression::parse(Lexer &s, int min_prio) { + if (s.empty()) + return LibertyExpression{}; + + char c = s.peek(); + auto lhs = LibertyExpression{}; + + while (isspace(c)) { + if (s.empty()) + return lhs; + s.next(); + c = s.peek(); + } + + if (isalpha(c)) { // pin + lhs.kind = Kind::PIN; + lhs.name = s.pin(); + } else if (c == '(') { // parens + s.next(); + lhs = parse(s); + if (s.peek() != ')') { + log_warning("expected ')' instead of '%c' while parsing Liberty expression '%s'\n", s.peek(), s.full_expr().c_str()); + return lhs; + } + s.next(); + } else if (c == '!') { // prefix NOT + s.next(); + lhs.kind = Kind::NOT; + lhs.children.push_back(parse(s, 7)); + } else { + log_warning("unrecognised character '%c' while parsing Liberty expression '%s'\n", c, s.full_expr().c_str()); + return lhs; + } + + while (true) { + if (s.empty()) + break; + + c = s.peek(); + + while (isspace(c)) { + if (s.empty()) + return lhs; + s.next(); + c = s.peek(); + } + + if (c == '\'') { // postfix NOT + if (min_prio > 7) + break; + s.next(); + + auto n = LibertyExpression{}; + n.kind = Kind::NOT; + n.children.push_back(lhs); + lhs = std::move(n); + + continue; + } else if (c == '^') { // infix XOR + if (min_prio > 5) + break; + s.next(); + + auto rhs = parse(s, 6); + auto n = LibertyExpression{}; + n.kind = Kind::XOR; + n.children.push_back(lhs); + n.children.push_back(rhs); + lhs = std::move(n); + + continue; + } else if (c == '&' || c == '*') { // infix AND + // technically space should be considered infix AND. it seems rare in practice. + if (min_prio > 3) + break; + s.next(); + + auto rhs = parse(s, 4); + auto n = LibertyExpression{}; + n.kind = Kind::AND; + n.children.push_back(lhs); + n.children.push_back(rhs); + lhs = std::move(n); + + continue; + } else if (c == '+' || c == '|') { // infix OR + if (min_prio > 1) + break; + s.next(); + + auto rhs = parse(s, 2); + auto n = LibertyExpression{}; + n.kind = Kind::OR; + n.children.push_back(lhs); + n.children.push_back(rhs); + lhs = std::move(n); + + continue; + } + break; + } + + return lhs; +} + +void LibertyExpression::get_pin_names(pool& names) { + if (kind == Kind::PIN) { + names.insert(name); + } else { + for (auto& child : children) + child.get_pin_names(names); + } +} + +bool LibertyExpression::eval(dict& values) { + bool result = false; + switch (kind) { + case Kind::AND: + result = true; + for (auto& child : children) + result &= child.eval(values); + return result; + case Kind::OR: + result = false; + for (auto& child : children) + result |= child.eval(values); + return result; + case Kind::NOT: + log_assert(children.size() == 1); + return !children[0].eval(values); + case Kind::XOR: + result = false; + for (auto& child : children) + result ^= child.eval(values); + return result; + case Kind::PIN: + return values.at(name); + case Kind::EMPTY: + log_assert(false); + } + return false; +} +#endif + int LibertyParser::lexer(std::string &str) { int c; @@ -115,12 +261,19 @@ int LibertyParser::lexer(std::string &str) // maybe it's a string? if (c == '"') { str = ""; +#ifdef FILTERLIB + str += c; +#endif while (1) { c = f.get(); if (c == '\n') line++; - if (c == '"') + if (c == '"') { +#ifdef FILTERLIB + str += c; +#endif break; + } str += c; } // fprintf(stderr, "LEX: string >>%s<<\n", str.c_str()); @@ -350,12 +503,12 @@ LibertyAst *LibertyParser::parse() #ifndef FILTERLIB -void LibertyParser::error() +void LibertyParser::error() const { log_error("Syntax error in liberty file on line %d.\n", line); } -void LibertyParser::error(const std::string &str) +void LibertyParser::error(const std::string &str) const { std::stringstream ss; ss << "Syntax error in liberty file on line " << line << ".\n"; @@ -365,13 +518,13 @@ void LibertyParser::error(const std::string &str) #else -void LibertyParser::error() +void LibertyParser::error() const { fprintf(stderr, "Syntax error in liberty file on line %d.\n", line); exit(1); } -void LibertyParser::error(const std::string &str) +void LibertyParser::error(const std::string &str) const { std::stringstream ss; ss << "Syntax error in liberty file on line " << line << ".\n"; diff --git a/passes/techmap/libparse.h b/passes/techmap/libparse.h index ad38ae5f8e0..fef569927d7 100644 --- a/passes/techmap/libparse.h +++ b/passes/techmap/libparse.h @@ -20,6 +20,7 @@ #ifndef LIBPARSE_H #define LIBPARSE_H +#include "kernel/yosys.h" #include #include #include @@ -39,8 +40,56 @@ namespace Yosys void dump(FILE *f, sieve &blacklist, sieve &whitelist, std::string indent = "", std::string path = "", bool path_ok = false) const; }; + struct LibertyExpression + { + struct Lexer { + std::string s, expr; + + Lexer(std::string s) : s{s}, expr{s} {} + + bool empty() { return s.empty();} + char peek() { return s[0]; } + std::string full_expr() { return expr; } + + char next() { + char c = s[0]; + s = s.substr(1, s.size()); + return c; + } + + std::string pin() { + auto length = s.find_first_of("\t()'!^*& +|"); + auto pin = s.substr(0, length); + s = s.substr(length, s.size()); + return pin; + } + }; + + enum Kind { + AND, + OR, + NOT, + XOR, + // the standard specifies constants, but they're probably rare in practice. + PIN, + EMPTY + }; + + Kind kind; + std::string name; + std::vector children; + + LibertyExpression() : kind(Kind::EMPTY) {} + + static LibertyExpression parse(Lexer &s, int min_prio = 0); + void get_pin_names(pool& names); + bool eval(dict& values); + }; + + class LibertyMergedCells; class LibertyParser { + friend class LibertyMergedCells; private: std::istream &f; int line; @@ -51,10 +100,10 @@ namespace Yosys anything else is a single character. */ int lexer(std::string &str); - + LibertyAst *parse(); - void error(); - void error(const std::string &str); + void error() const; + void error(const std::string &str) const; public: const LibertyAst *ast; @@ -62,6 +111,35 @@ namespace Yosys LibertyParser(std::istream &f) : f(f), line(1), ast(parse()) {} ~LibertyParser() { if (ast) delete ast; } }; + + class LibertyMergedCells + { + std::vector asts; + + public: + std::vector cells; + void merge(LibertyParser &parser) + { + if (parser.ast) { + const LibertyAst *ast = parser.ast; + asts.push_back(ast); + // The parser no longer owns its top level ast, but we do. + // sketchy zone + parser.ast = nullptr; + if (ast->id != "library") + parser.error("Top level entity isn't \"library\".\n"); + for (const LibertyAst *cell : ast->children) + if (cell->id == "cell" && cell->args.size() == 1) + cells.push_back(cell); + } + } + ~LibertyMergedCells() + { + for (auto ast : asts) + delete ast; + } + }; + } #endif diff --git a/passes/techmap/techmap.cc b/passes/techmap/techmap.cc index f3927a48661..b77bab69eb0 100644 --- a/passes/techmap/techmap.cc +++ b/passes/techmap/techmap.cc @@ -1028,6 +1028,9 @@ struct TechmapPass : public Pass { log(" map file. Note that the Verilog frontend is also called with the\n"); log(" '-nooverwrite' option set.\n"); log("\n"); + log(" -dont_map \n"); + log(" leave the given cell type unmapped by ignoring any mapping rules for it\n"); + log("\n"); log("When a module in the map file has the 'techmap_celltype' attribute set, it will\n"); log("match cells with a type that match the text value of this attribute. Otherwise\n"); log("the module name will be used to match the cell. Multiple space-separated cell\n"); @@ -1159,6 +1162,7 @@ struct TechmapPass : public Pass { simplemap_get_mappers(worker.simplemap_mappers); std::vector map_files; + std::vector dont_map; std::string verilog_frontend = "verilog -nooverwrite -noblackbox"; int max_iter = -1; @@ -1200,6 +1204,10 @@ struct TechmapPass : public Pass { worker.ignore_wb = true; continue; } + if (args[argidx] == "-dont_map" && argidx+1 < args.size()) { + dont_map.push_back(RTLIL::escape_id(args[++argidx])); + continue; + } break; } extra_args(args, argidx, design); @@ -1256,6 +1264,11 @@ struct TechmapPass : public Pass { celltypeMap[module_name].insert(module->name); } } + + // Erase any rules disabled with a -dont_map argument + for (auto type : dont_map) + celltypeMap.erase(type); + log_debug("Cell type mappings to use:\n"); for (auto &i : celltypeMap) { i.second.sort(RTLIL::sort_by_id_str()); diff --git a/techlibs/common/Makefile.inc b/techlibs/common/Makefile.inc index fa5c2a825de..e76d70a1a8e 100644 --- a/techlibs/common/Makefile.inc +++ b/techlibs/common/Makefile.inc @@ -36,3 +36,5 @@ $(eval $(call add_share_file,share,techlibs/common/abc9_unmap.v)) $(eval $(call add_share_file,share,techlibs/common/cmp2lcu.v)) $(eval $(call add_share_file,share,techlibs/common/cmp2softlogic.v)) $(eval $(call add_share_file,share/choices,techlibs/common/choices/kogge-stone.v)) +$(eval $(call add_share_file,share/choices,techlibs/common/choices/han-carlson.v)) +$(eval $(call add_share_file,share/choices,techlibs/common/choices/sklansky.v)) diff --git a/techlibs/common/choices/han-carlson.v b/techlibs/common/choices/han-carlson.v new file mode 100644 index 00000000000..0ce8ee2071c --- /dev/null +++ b/techlibs/common/choices/han-carlson.v @@ -0,0 +1,59 @@ +(* techmap_celltype = "$lcu" *) +module _80_lcu_han_carlson (P, G, CI, CO); + parameter WIDTH = 2; + + (* force_downto *) + input [WIDTH-1:0] P, G; + input CI; + + (* force_downto *) + output [WIDTH-1:0] CO; + + integer i, j; + (* force_downto *) + reg [WIDTH-1:0] p, g; + + wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast"; + + always @* begin + i = 0; + p = P; + g = G; + + // in almost all cases CI will be constant zero + g[0] = g[0] | (p[0] & CI); + if (i < $clog2(WIDTH)) begin + + // First layer: BK + for (j = WIDTH - 1; j >= 0; j = j - 1) begin + if (j % 2 == 1) begin + g[j] = g[j] | p[j] & g[j - 1]; + p[j] = p[j] & p[j - 1]; + end + end + + // Inner (log(WIDTH) - 1) layers: KS + for (i = 1; i < $clog2(WIDTH); i = i + 1) begin + for (j = WIDTH - 1; j >= 2**i; j = j - 1) begin + if (j % 2 == 1) begin + g[j] = g[j] | p[j] & g[j - 2**i]; + p[j] = p[j] & p[j - 2**i]; + end + end + end + + // Last layer: BK + if (i < ($clog2(WIDTH) + 1)) begin + for (j = WIDTH - 1; j >= 0; j = j - 1) begin + if ((j % 2 == 0) && (j > 0)) begin + g[j] = g[j] | p[j] & g[j - 1]; + p[j] = p[j] & p[j - 1]; + end + end + end + + end + end + + assign CO = g; +endmodule diff --git a/techlibs/common/choices/sklansky.v b/techlibs/common/choices/sklansky.v new file mode 100644 index 00000000000..2a79e76a51c --- /dev/null +++ b/techlibs/common/choices/sklansky.v @@ -0,0 +1,37 @@ +(* techmap_celltype = "$lcu" *) +module _80_lcu_sklansky (P, G, CI, CO); + parameter WIDTH = 2; + + (* force_downto *) + input [WIDTH-1:0] P, G; + input CI; + + (* force_downto *) + output [WIDTH-1:0] CO; + + integer i, j; + (* force_downto *) + reg [WIDTH-1:0] p, g; + + wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast"; + + always @* begin + p = P; + g = G; + + // in almost all cases CI will be constant zero + g[0] = g[0] | (p[0] & CI); + + for (i = 0; i < $clog2(WIDTH); i = i + 1) begin + // iterate in reverse so we don't confuse a result from this stage and the previous + for (j = WIDTH - 1; j >= 0; j = j - 1) begin + if (j & 2**i) begin + g[j] = g[j] | p[j] & g[(j & ~(2**i - 1)) - 1]; + p[j] = p[j] & p[(j & ~(2**i - 1)) - 1]; + end + end + end + end + + assign CO = g; +endmodule diff --git a/techlibs/gatemate/synth_gatemate.cc b/techlibs/gatemate/synth_gatemate.cc index 1d46d79295c..fa36252f542 100644 --- a/techlibs/gatemate/synth_gatemate.cc +++ b/techlibs/gatemate/synth_gatemate.cc @@ -301,6 +301,7 @@ struct SynthGateMatePass : public ScriptPass } run("muxcover " + muxcover_args); run("opt -full"); + run("simplemap"); run("techmap -map +/gatemate/mux_map.v"); } diff --git a/techlibs/gowin/Makefile.inc b/techlibs/gowin/Makefile.inc index 0e24b91e5ff..9ec7dce4d11 100644 --- a/techlibs/gowin/Makefile.inc +++ b/techlibs/gowin/Makefile.inc @@ -3,7 +3,9 @@ OBJS += techlibs/gowin/synth_gowin.o $(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_map.v)) $(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_sim.v)) -$(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_xtra.v)) +$(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_xtra_gw1n.v)) +$(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_xtra_gw2a.v)) +$(eval $(call add_share_file,share/gowin,techlibs/gowin/cells_xtra_gw5a.v)) $(eval $(call add_share_file,share/gowin,techlibs/gowin/arith_map.v)) $(eval $(call add_share_file,share/gowin,techlibs/gowin/brams_map.v)) $(eval $(call add_share_file,share/gowin,techlibs/gowin/brams.txt)) diff --git a/techlibs/gowin/cells_xtra.py b/techlibs/gowin/cells_xtra.py index 3f0047ecbff..418accdfeb4 100644 --- a/techlibs/gowin/cells_xtra.py +++ b/techlibs/gowin/cells_xtra.py @@ -63,14 +63,17 @@ def xtract_cells_decl(dir, fout): parser.add_argument('gowin_dir', nargs='?', default='/opt/gowin/') args = parser.parse_args() - dirs = [ - os.path.join(args.gowin_dir, 'IDE/simlib/gw1n/'), - ] + families = { + 'gw1n': os.path.join(args.gowin_dir, 'IDE/simlib/gw1n/'), + 'gw2a': os.path.join(args.gowin_dir, 'IDE/simlib/gw2a/'), + 'gw5a': os.path.join(args.gowin_dir, 'IDE/simlib/gw5a/'), + } - with open('cells_xtra.v', 'w') as fout: - fout.write('// Created by cells_xtra.py\n') - fout.write('\n') - for dir in dirs: - if not os.path.isdir(dir): - print(f'{dir} is not a directory') - xtract_cells_decl(dir, fout) + for family, dir in families.items(): + if not os.path.isdir(dir): + print(f'{dir} is not a directory') + else: + with open(f'cells_xtra_{family}.v', 'w') as fout: + fout.write('// Created by cells_xtra.py\n') + fout.write('\n') + xtract_cells_decl(dir, fout) diff --git a/techlibs/gowin/cells_xtra.v b/techlibs/gowin/cells_xtra_gw1n.v similarity index 97% rename from techlibs/gowin/cells_xtra.v rename to techlibs/gowin/cells_xtra_gw1n.v index 4cb4bab2f8c..ca6afd2e233 100644 --- a/techlibs/gowin/cells_xtra.v +++ b/techlibs/gowin/cells_xtra_gw1n.v @@ -1465,12 +1465,22 @@ output O, OB; input I, IB, IL, MODESEL; endmodule +module ELVDS_IBUF_MIPI (...); +output OH, OL; +input I, IB; +endmodule + module I3C_IOBUF (...); output O; inout IO; input I, MODESEL; endmodule +module TLVDS_OEN_BK (...); +input OEN; +parameter OEN_BANK = "0"; +endmodule + module CLKDIV (...); input HCLKIN; input RESETN; @@ -1570,117 +1580,6 @@ input CE; output CLKOUT; endmodule -module FLASH128K (...); -input [31:0] DIN; -input [14:0] ADDR; -input CS,AE,OE; -input PCLK; -input PROG, SERA, MASE; -input NVSTR; -input IFREN; -input RESETN; -output [31:0] DOUT; -output TBIT; -parameter IDLE = 4'd0, - READ_S1 = 4'd1, - READ_S2 = 4'd2, - PROG_S1 = 4'd3, - PROG_S2 = 4'd4, - PROG_S3 = 4'd5, - PROG_S4 = 4'd6, - SERA_S1 = 4'd7, - SERA_S2 = 4'd8, - SERA_S3 = 4'd9, - SERA_S4 = 4'd10, - MASE_S1 = 4'd11, - MASE_S2 = 4'd12, - MASE_S3 = 4'd13, - MASE_S4 = 4'd14; -endmodule - -module MCU (...); -endmodule - -module USB20_PHY (...); -parameter DATABUS16_8 = 1'b0; -parameter ADP_PRBEN = 1'b0; -parameter TEST_MODE = 5'b00000; -parameter HSDRV1 = 1'b0; -parameter HSDRV0 = 1'b0; -parameter CLK_SEL = 1'b0; -parameter M = 4'b0000; -parameter N = 6'b101000; -parameter C = 2'b01; -parameter FOC_LOCK = 1'b0; -input [15:0] DATAIN; -input TXVLD; -input TXVLDH; -input RESET; -input SUSPENDM; -input [1:0] XCVRSEL; -input TERMSEL; -input [1:0] OPMODE; -output [15:0] DATAOUT; -output TXREADY; -output RXACTIVE; -output RXVLD; -output RXVLDH; -output CLK; -output RXERROR; -inout DP; -inout DM; -output [1:0] LINESTATE; -input IDPULLUP; -input DPPD; -input DMPD; -input CHARGVBUS; -input DISCHARGVBUS; -input TXBITSTUFFEN; -input TXBITSTUFFENH; -input TXENN; -input TXDAT; -input TXSE0; -input FSLSSERIAL; -output HOSTDIS; -output IDDIG; -output ADPPRB; -output ADPSNS; -output SESSVLD; -output VBUSVLD; -output RXDP; -output RXDM; -output RXRCV; -output LBKERR; -output CLKRDY; -input INTCLK; -inout ID; -inout VBUS; -inout REXT; -input XIN; -inout XOUT; -input TEST; -output CLK480PAD; -input SCANCLK; -input SCANEN; -input SCANMODE; -input TRESETN; -input SCANIN1; -output SCANOUT1; -input SCANIN2; -output SCANOUT2; -input SCANIN3; -output SCANOUT3; -input SCANIN4; -output SCANOUT4; -input SCANIN5; -output SCANOUT5; -input SCANIN6; -output SCANOUT6; -endmodule - -module ADC (...); -endmodule - module CLKDIV2 (...); parameter GSREN = "false"; input HCLKIN, RESETN; @@ -1977,7 +1876,7 @@ parameter MIPI_LANE1_EN = 1'b0; parameter MIPI_LANE2_EN = 1'b0; parameter MIPI_LANE3_EN = 1'b0; parameter MIPI_CK_EN = 1'b1; -parameter SYNC_CLK_SEL = 1'b1; +parameter SYNC_CLK_SEL = 1'b0; endmodule module CLKDIVG (...); @@ -1988,3 +1887,7 @@ output CLKOUT; parameter DIV_MODE = "2"; parameter GSREN = "false"; endmodule + +module PWRGRD (...); +input PDEN; +endmodule diff --git a/techlibs/gowin/cells_xtra_gw2a.v b/techlibs/gowin/cells_xtra_gw2a.v new file mode 100644 index 00000000000..84c08c6cb7e --- /dev/null +++ b/techlibs/gowin/cells_xtra_gw2a.v @@ -0,0 +1,1724 @@ +// Created by cells_xtra.py + + +module MUX2_MUX8 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX2_MUX16 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX2_MUX32 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX4 (...); +input I0, I1, I2, I3; +input S0, S1; +output O; +endmodule + + +module MUX8 (...); +input I0, I1, I2, I3, I4, I5, I6, I7; +input S0, S1, S2; +output O; +endmodule + + +module MUX16 (...); +input I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15; +input S0, S1, S2, S3; +output O; +endmodule + +module MUX32 (...); +input I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31; +input S0, S1, S2, S3, S4; +output O; +endmodule + +module LUT5 (...); +parameter INIT = 32'h00000000; +input I0, I1, I2, I3, I4; +output F; +endmodule + + +module LUT6 (...); +parameter INIT = 64'h0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5; +output F; +endmodule + + +module LUT7 (...); +parameter INIT = 128'h0000_0000_0000_0000_0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5, I6; +output F; +endmodule + + +module LUT8 (...); +parameter INIT = 256'h0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5, I6, I7; +output F; +endmodule + + +module DL (...); +input D, G; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLE (...); +input D, G, CE; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLC (...); +input D, G, CLEAR; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLCE (...); +input D, G, CLEAR, CE; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLP (...); +input D, G, PRESET; +output Q; +parameter INIT = 1'b1; +endmodule + + +module DLPE (...); +input D, G, PRESET, CE; +output Q; +parameter INIT = 1'b1; +endmodule + + +module DLN (...); +input D, G; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLNE (...); +input D, G, CE; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLNC (...); +input D, G, CLEAR; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLNCE (...); +input D, G, CLEAR, CE; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLNP (...); +input D, G, PRESET; +output Q; +parameter INIT = 1'b1; +endmodule + + +module DLNPE (...); +input D, G, PRESET, CE; +output Q; +parameter INIT = 1'b1; +endmodule + + +module INV (...); +input I; +output O; +endmodule + + +module IDDR_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +input D, ICLK, PCLK; +input [2:0] WADDR; +input [2:0] RADDR; +input RESET; +output Q0,Q1; +endmodule + + +module ODDR_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1; +input TX, PCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module IDES4_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +input D, ICLK, FCLK, PCLK; +input [2:0] WADDR; +input [2:0] RADDR; +input RESET,CALIB; +output Q0,Q1,Q2,Q3; +endmodule + + +module IDES8_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +input D, ICLK, FCLK, PCLK; +input [2:0] WADDR; +input [2:0] RADDR; +input RESET,CALIB; +output Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7; +endmodule + + +module OSER4_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +parameter HWL = "false"; +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1, D2, D3; +input TX0, TX1; +input PCLK, FCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module OSER8_MEM (...); +parameter GSREN = "false"; +parameter LSREN = "true"; +parameter HWL = "false"; +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1, D2, D3, D4, D5, D6, D7; +input TX0, TX1, TX2, TX3; +input PCLK, FCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module IODELAY (...); +parameter C_STATIC_DLY = 0; +input DI; +input SDTAP; +input SETN; +input VALUE; +output DF; +output DO; +endmodule + + +module IEM (...); +parameter WINSIZE = "SMALL"; +parameter GSREN = "false"; +parameter LSREN = "true"; +input D, CLK, RESET, MCLK; +output LAG, LEAD; +endmodule + + +module ROM16 (...); +parameter INIT_0 = 16'h0000; +input [3:0] AD; +output DO; +endmodule + + +module ROM (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 32; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input WRE; +input [13:0] AD; +input [2:0] BLKSEL; +output [31:0] DO; +endmodule + + +module ROMX9 (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 36; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input WRE; +input [13:0] AD; +input [2:0] BLKSEL; +output [35:0] DO; +endmodule + + +module rSDP (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 32; +parameter BIT_WIDTH_1 = 32; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESETA, RESETB; +input [13:0] ADA, ADB; +input [31:0] DI; +input [2:0] BLKSEL; +output [31:0] DO; +endmodule + + +module rSDPX9 (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 36; +parameter BIT_WIDTH_1 = 36; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESETA, RESETB; +input [13:0] ADA, ADB; +input [2:0] BLKSEL; +input [35:0] DI; +output [35:0] DO; +endmodule + + +module rROM (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 32; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +input [2:0] BLKSEL; +output [31:0] DO; +endmodule + + +module rROMX9 (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 36; +parameter BLK_SEL = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +input [2:0] BLKSEL; +output [35:0] DO; +endmodule + + +module pROM (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 32; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +output [31:0] DO; +endmodule + + +module pROMX9 (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 36; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +output [35:0] DO; +endmodule + + +module SDPB (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 32; +parameter BIT_WIDTH_1 = 32; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESETA, RESETB; +input [13:0] ADA, ADB; +input [31:0] DI; +input [2:0] BLKSELA, BLKSELB; +output [31:0] DO; +endmodule + + +module SDPX9B (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 36; +parameter BIT_WIDTH_1 = 36; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESETA, RESETB; +input [13:0] ADA, ADB; +input [2:0] BLKSELA, BLKSELB; +input [35:0] DI; +output [35:0] DO; +endmodule + + +module DPB (...); +parameter READ_MODE0 = 1'b0; +parameter READ_MODE1 = 1'b0; +parameter WRITE_MODE0 = 2'b00; +parameter WRITE_MODE1 = 2'b00; +parameter BIT_WIDTH_0 = 16; +parameter BIT_WIDTH_1 = 16; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCEA, OCEB; +input RESETA, RESETB; +input WREA, WREB; +input [13:0] ADA, ADB; +input [2:0] BLKSELA, BLKSELB; +input [15:0] DIA, DIB; +output [15:0] DOA, DOB; +endmodule + + +module DPX9B (...); +parameter READ_MODE0 = 1'b0; +parameter READ_MODE1 = 1'b0; +parameter WRITE_MODE0 = 2'b00; +parameter WRITE_MODE1 = 2'b00; +parameter BIT_WIDTH_0 = 18; +parameter BIT_WIDTH_1 = 18; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCEA, OCEB; +input RESETA, RESETB; +input WREA, WREB; +input [13:0] ADA, ADB; +input [17:0] DIA, DIB; +input [2:0] BLKSELA, BLKSELB; +output [17:0] DOA, DOB; +endmodule + + +module PADD18 (...); +input [17:0] A; +input [17:0] B; +input ASEL; +input CE,CLK,RESET; +input [17:0] SI,SBI; +output [17:0] SO,SBO; +output [17:0] DOUT; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter ADD_SUB = 1'b0; +parameter PADD_RESET_MODE = "SYNC"; +parameter BSEL_MODE = 1'b1; +parameter SOREG = 1'b0; +endmodule + +module PADD9 (...); +input [8:0] A; +input [8:0] B; +input ASEL; +input CE,CLK,RESET; +input [8:0] SI,SBI; +output [8:0] SO,SBO; +output [8:0] DOUT; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter ADD_SUB = 1'b0; +parameter PADD_RESET_MODE = "SYNC"; +parameter BSEL_MODE = 1'b1; +parameter SOREG = 1'b0; +endmodule + +module MULT9X9 (...); +input [8:0] A,SIA; +input [8:0] B,SIB; +input ASIGN,BSIGN; +input ASEL,BSEL; +input CE; +input CLK; +input RESET; +output [17:0] DOUT; +output [8:0] SOA,SOB; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter OUT_REG = 1'b0; +parameter PIPE_REG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter SOA_REG = 1'b0; +parameter MULT_RESET_MODE = "SYNC"; +endmodule + +module MULT18X18 (...); +input [17:0] A,SIA; +input [17:0] B,SIB; +input ASIGN,BSIGN; +input ASEL,BSEL; +input CE; +input CLK; +input RESET; +output [35:0] DOUT; +output [17:0] SOA,SOB; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter OUT_REG = 1'b0; +parameter PIPE_REG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter SOA_REG = 1'b0; +parameter MULT_RESET_MODE = "SYNC"; +endmodule + +module MULT36X36 (...); +input [35:0] A; +input [35:0] B; +input ASIGN,BSIGN; +input CE; +input CLK; +input RESET; +output [71:0] DOUT; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter OUT0_REG = 1'b0; +parameter OUT1_REG = 1'b0; +parameter PIPE_REG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter MULT_RESET_MODE = "SYNC"; +endmodule + +module MULTALU36X18 (...); +input [17:0] A; +input [35:0] B; +input [53:0] C; +input ASIGN,BSIGN,ACCLOAD; +input CE; +input CLK; +input RESET; +input [54:0] CASI; +output [53:0] DOUT; +output [54:0] CASO; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter CREG = 1'b0; +parameter OUT_REG = 1'b0; +parameter PIPE_REG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter ACCLOAD_REG0 = 1'b0; +parameter ACCLOAD_REG1 = 1'b0; +parameter MULT_RESET_MODE = "SYNC"; +parameter MULTALU36X18_MODE = 0; +parameter C_ADD_SUB = 1'b0; +endmodule + +module MULTADDALU18X18 (...); +input [17:0] A0; +input [17:0] B0; +input [17:0] A1; +input [17:0] B1; +input [53:0] C; +input [17:0] SIA, SIB; +input [1:0] ASIGN, BSIGN; +input [1:0] ASEL, BSEL; +input [54:0] CASI; +input CE; +input CLK; +input RESET; +input ACCLOAD; +output [53:0] DOUT; +output [54:0] CASO; +output [17:0] SOA, SOB; +parameter A0REG = 1'b0; +parameter A1REG = 1'b0; +parameter B0REG = 1'b0; +parameter B1REG = 1'b0; +parameter CREG = 1'b0; +parameter PIPE0_REG = 1'b0; +parameter PIPE1_REG = 1'b0; +parameter OUT_REG = 1'b0; +parameter ASIGN0_REG = 1'b0; +parameter ASIGN1_REG = 1'b0; +parameter ACCLOAD_REG0 = 1'b0; +parameter ACCLOAD_REG1 = 1'b0; +parameter BSIGN0_REG = 1'b0; +parameter BSIGN1_REG = 1'b0; +parameter SOA_REG = 1'b0; +parameter B_ADD_SUB = 1'b0; +parameter C_ADD_SUB = 1'b0; +parameter MULTADDALU18X18_MODE = 0; +parameter MULT_RESET_MODE = "SYNC"; +endmodule + +module MULTALU18X18 (...); +input [17:0] A, B; +input CLK,CE,RESET; +input ASIGN, BSIGN; +input ACCLOAD,DSIGN; +input [53:0] C,D; +input [54:0] CASI; +output [53:0] DOUT; +output [54:0] CASO; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter CREG = 1'b0; +parameter DREG = 1'b0; +parameter DSIGN_REG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter ACCLOAD_REG0 = 1'b0; +parameter ACCLOAD_REG1 = 1'b0; +parameter MULT_RESET_MODE = "SYNC"; +parameter PIPE_REG = 1'b0; +parameter OUT_REG = 1'b0; +parameter B_ADD_SUB = 1'b0; +parameter C_ADD_SUB = 1'b0; +parameter MULTALU18X18_MODE = 0; +endmodule + +module ALU54D (...); +input [53:0] A, B; +input ASIGN,BSIGN; +input ACCLOAD; +input [54:0] CASI; +input CLK, CE, RESET; +output [53:0] DOUT; +output [54:0] CASO; +parameter AREG = 1'b0; +parameter BREG = 1'b0; +parameter ASIGN_REG = 1'b0; +parameter BSIGN_REG = 1'b0; +parameter ACCLOAD_REG = 1'b0; +parameter OUT_REG = 1'b0; +parameter B_ADD_SUB = 1'b0; +parameter C_ADD_SUB = 1'b0; +parameter ALUD_MODE = 0; +parameter ALU_RESET_MODE = "SYNC"; +endmodule + +module BUFG (...); +output O; +input I; +endmodule + + +module BUFS (...); +output O; +input I; +endmodule + + +module PLL (...); +input CLKIN; +input CLKFB; +input RESET; +input RESET_P; +input RESET_I; +input RESET_S; +input [5:0] FBDSEL; +input [5:0] IDSEL; +input [5:0] ODSEL; +input [3:0] PSDA,FDLY; +input [3:0] DUTYDA; +output CLKOUT; +output LOCK; +output CLKOUTP; +output CLKOUTD; +output CLKOUTD3; +parameter FCLKIN = "100.0"; +parameter DYN_IDIV_SEL= "false"; +parameter IDIV_SEL = 0; +parameter DYN_FBDIV_SEL= "false"; +parameter FBDIV_SEL = 0; +parameter DYN_ODIV_SEL= "false"; +parameter ODIV_SEL = 8; +parameter PSDA_SEL= "0000"; +parameter DYN_DA_EN = "false"; +parameter DUTYDA_SEL= "1000"; +parameter CLKOUT_FT_DIR = 1'b1; +parameter CLKOUTP_FT_DIR = 1'b1; +parameter CLKOUT_DLY_STEP = 0; +parameter CLKOUTP_DLY_STEP = 0; +parameter CLKFB_SEL = "internal"; +parameter CLKOUT_BYPASS = "false"; +parameter CLKOUTP_BYPASS = "false"; +parameter CLKOUTD_BYPASS = "false"; +parameter DYN_SDIV_SEL = 2; +parameter CLKOUTD_SRC = "CLKOUT"; +parameter CLKOUTD3_SRC = "CLKOUT"; +parameter DEVICE = "GW2A-18"; +endmodule + +module TLVDS_IBUF (...); +output O; +input I, IB; +endmodule + +module TLVDS_TBUF (...); +output O, OB; +input I, OEN; +endmodule + +module TLVDS_IOBUF (...); +output O; +inout IO, IOB; +input I, OEN; +endmodule + +module ELVDS_IBUF (...); +output O; +input I, IB; +endmodule + +module ELVDS_TBUF (...); +output O, OB; +input I, OEN; +endmodule + +module ELVDS_IOBUF (...); +output O; +inout IO, IOB; +input I, OEN; +endmodule + +module DLL (...); +input CLKIN; +input STOP; +input UPDNCNTL; +input RESET; +output [7:0]STEP; +output LOCK; +parameter DLL_FORCE = 0; +parameter CODESCAL="000"; +parameter SCAL_EN="true"; +parameter DIV_SEL = 1'b0; +endmodule + +module CLKDIV (...); +input HCLKIN; +input RESETN; +input CALIB; +output CLKOUT; +parameter DIV_MODE = "2"; +parameter GSREN = "false"; +endmodule + +module DHCEN (...); +input CLKIN,CE; +output CLKOUT; +endmodule + +module DQS (...); +input DQSIN,PCLK,FCLK,RESET; +input [3:0] READ; +input [2:0] RCLKSEL; +input [7:0] DLLSTEP; +input [7:0] WSTEP; +input RLOADN, RMOVE, RDIR, WLOADN, WMOVE, WDIR, HOLD; +output DQSR90, DQSW0, DQSW270; +output [2:0] RPOINT, WPOINT; +output RVALID,RBURST, RFLAG, WFLAG; +endmodule + +module DLLDLY (...); +input CLKIN; +input [7:0] DLLSTEP; +input DIR,LOADN,MOVE; +output CLKOUT; +output FLAG; +parameter DLL_INSEL = 1'b1; +parameter DLY_SIGN = 1'b0; +parameter DLY_ADJ = 0; +endmodule + +module DQCE (...); +input CLKIN; +input CE; +output CLKOUT; +endmodule + +module CLKDIV2 (...); +parameter GSREN = "false"; +input HCLKIN, RESETN; +output CLKOUT; +endmodule + +module IODELAYA (...); +parameter C_STATIC_DLY = 0; +input DI; +input SDTAP; +input SETN; +input VALUE; +output DF; +output DO; +endmodule + +module IBUF_R (...); +input I; +input RTEN; +output O; +endmodule + +module IOBUF_R (...); +input I,OEN; +input RTEN; +output O; +inout IO; +endmodule + +module ELVDS_IBUF_R (...); +output O; +input I, IB; +input RTEN; +endmodule + +module ELVDS_IOBUF_R (...); +output O; +inout IO, IOB; +input I, OEN; +input RTEN; +endmodule + +module OTP (...); +input CSB, SCLK; +output DOUT; +endmodule + +module SAMB (...); +input [23:0] SPIAD; +input LOADN_SPIAD; +endmodule + +module CLKDIVG (...); +input CLKIN; +input RESETN; +input CALIB; +output CLKOUT; +parameter DIV_MODE = "2"; +parameter GSREN = "false"; +endmodule + +module PLLO (...); +input CLKIN; +input CLKFB; +input RESET; +input RESET_P; +input RESET_I; +input RESET_S; +input [5:0] FBDSEL; +input [5:0] IDSEL; +input [6:0] ODSELA; +input [6:0] ODSELB; +input [6:0] ODSELC; +input [6:0] ODSELD; +input [3:0] DTA; +input [3:0] DTB; +input [4:0] ICPSEL; +input [2:0] LPFRES; +input [1:0] PSSEL; +input PSDIR; +input PSPULSE; +input ENCLKA; +input ENCLKB; +input ENCLKC; +input ENCLKD; +output LOCK; +output CLKOUTA; +output CLKOUTB; +output CLKOUTC; +output CLKOUTD; +parameter FCLKIN = "100.0"; +parameter DYN_IDIV_SEL= "FALSE"; +parameter IDIV_SEL = 0; +parameter DYN_FBDIV_SEL= "FALSE"; +parameter FBDIV_SEL = 0; +parameter DYN_ODIVA_SEL= "FALSE"; +parameter ODIVA_SEL = 6; +parameter DYN_ODIVB_SEL= "FALSE"; +parameter ODIVB_SEL = 6; +parameter DYN_ODIVC_SEL= "FALSE"; +parameter ODIVC_SEL = 6; +parameter DYN_ODIVD_SEL= "FALSE"; +parameter ODIVD_SEL = 6; +parameter CLKOUTA_EN = "TRUE"; +parameter CLKOUTB_EN = "TRUE"; +parameter CLKOUTC_EN = "TRUE"; +parameter CLKOUTD_EN = "TRUE"; +parameter DYN_DTA_SEL = "FALSE"; +parameter DYN_DTB_SEL = "FALSE"; +parameter CLKOUTA_DT_DIR = 1'b1; +parameter CLKOUTB_DT_DIR = 1'b1; +parameter CLKOUTA_DT_STEP = 0; +parameter CLKOUTB_DT_STEP = 0; +parameter CLKA_IN_SEL = 2'b00; +parameter CLKA_OUT_SEL = 1'b0; +parameter CLKB_IN_SEL = 2'b00; +parameter CLKB_OUT_SEL = 1'b0; +parameter CLKC_IN_SEL = 2'b00; +parameter CLKC_OUT_SEL = 1'b0; +parameter CLKD_IN_SEL = 2'b00; +parameter CLKD_OUT_SEL = 1'b0; +parameter CLKFB_SEL = "INTERNAL"; +parameter DYN_DPA_EN = "FALSE"; +parameter DYN_PSB_SEL = "FALSE"; +parameter DYN_PSC_SEL = "FALSE"; +parameter DYN_PSD_SEL = "FALSE"; +parameter PSB_COARSE = 1; +parameter PSB_FINE = 0; +parameter PSC_COARSE = 1; +parameter PSC_FINE = 0; +parameter PSD_COARSE = 1; +parameter PSD_FINE = 0; +parameter DTMS_ENB = "FALSE"; +parameter DTMS_ENC = "FALSE"; +parameter DTMS_END = "FALSE"; +parameter RESET_I_EN = "FALSE"; +parameter RESET_S_EN = "FALSE"; +parameter DYN_ICP_SEL= "FALSE"; +parameter ICP_SEL = 5'bXXXXX; +parameter DYN_RES_SEL= "FALSE"; +parameter LPR_REF = 7'bXXXXXXX; +endmodule + +module ELVDS_IBUF_MIPI (...); +output OH, OL; +input I, IB; +endmodule + +module MIPI_IBUF (...); +output OH, OL, OB; +inout IO, IOB; +input I, IB; +input OEN, OENB; +input HSREN; +endmodule + +module I3C_IOBUF (...); +output O; +inout IO; +input I, MODESEL; +endmodule diff --git a/techlibs/gowin/cells_xtra_gw5a.v b/techlibs/gowin/cells_xtra_gw5a.v new file mode 100644 index 00000000000..3a9766a939f --- /dev/null +++ b/techlibs/gowin/cells_xtra_gw5a.v @@ -0,0 +1,2680 @@ +// Created by cells_xtra.py + + +module MUX2_MUX8 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX2_MUX16 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX2_MUX32 (...); +input I0,I1; +input S0; +output O; +endmodule + + +module MUX4 (...); +input I0, I1, I2, I3; +input S0, S1; +output O; +endmodule + + +module MUX8 (...); +input I0, I1, I2, I3, I4, I5, I6, I7; +input S0, S1, S2; +output O; +endmodule + + +module MUX16 (...); +input I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15; +input S0, S1, S2, S3; +output O; +endmodule + +module MUX32 (...); +input I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31; +input S0, S1, S2, S3, S4; +output O; +endmodule + +module LUT5 (...); +parameter INIT = 32'h00000000; +input I0, I1, I2, I3, I4; +output F; +endmodule + + +module LUT6 (...); +parameter INIT = 64'h0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5; +output F; +endmodule + + +module LUT7 (...); +parameter INIT = 128'h0000_0000_0000_0000_0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5, I6; +output F; +endmodule + + +module LUT8 (...); +parameter INIT = 256'h0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000; +input I0, I1, I2, I3, I4, I5, I6, I7; +output F; +endmodule + + +module DLCE (...); +input D, G, CLEAR, GE; +output Q; +parameter INIT = 1'b0; +endmodule + + +module DLPE (...); +input D, G, PRESET, GE; +output Q; +parameter INIT = 1'b1; +endmodule + + +module ROM16 (...); +parameter INIT_0 = 16'h0000; +input [3:0] AD; +output DO; +endmodule + + +module INV (...); +input I; +output O; +endmodule + + +module TLVDS_IBUF (...); +output O; +input I, IB; +endmodule + +module TLVDS_TBUF (...); +output O, OB; +input I, OEN; +endmodule + +module TLVDS_IOBUF (...); +output O; +inout IO, IOB; +input I, OEN; +endmodule + +module ELVDS_TBUF (...); +output O, OB; +input I, OEN; +endmodule + +module ELVDS_IOBUF (...); +output O; +inout IO, IOB; +input I, OEN; +endmodule + +module MIPI_IBUF (...); +output OH, OL, OB; +inout IO, IOB; +input I, IB; +input OEN, OENB; +input HSEN, HSREN; +endmodule + +module MIPI_OBUF_A (...); +output O, OB; +input I, IB, IL, MODESEL; +inout IO, IOB; +input OEN, OENB; +endmodule + +module ELVDS_IOBUF_R (...); +output O; +inout IO, IOB; +input I, OEN; +input RTEN; +endmodule + +module I3C_IOBUF (...); +output O; +inout IO; +input I, MODESEL; +endmodule + +module TLVDS_IBUF_ADC (...); +input I, IB; +input ADCEN; +endmodule + +module MIPI_CPHY_IBUF (...); +output OH0, OL0, OB0, OH1, OL1, OB1, OH2, OL2, OB2; +inout IO0, IOB0, IO1, IOB1, IO2, IOB2; +input I0, IB0, I1, IB1, I2, IB2; +input OEN, OENB; +input HSEN; +endmodule + +module MIPI_CPHY_OBUF (...); +output O0, OB0, O1, OB1, O2, OB2; +input I0, IB0, IL0, I1, IB1, IL1, I2, IB2, IL2; +inout IO0, IOB0, IO1, IOB1, IO2, IOB2; +input OEN, OENB, MODESEL, VCOME; +endmodule + +module SDPB (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 32; +parameter BIT_WIDTH_1 = 32; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESET; +input [13:0] ADA, ADB; +input [31:0] DI; +input [2:0] BLKSELA, BLKSELB; +output [31:0] DO; +endmodule + + +module SDPX9B (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH_0 = 36; +parameter BIT_WIDTH_1 = 36; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESET; +input [13:0] ADA, ADB; +input [2:0] BLKSELA, BLKSELB; +input [35:0] DI; +output [35:0] DO; +endmodule + + +module DPB (...); +parameter READ_MODE0 = 1'b0; +parameter READ_MODE1 = 1'b0; +parameter WRITE_MODE0 = 2'b00; +parameter WRITE_MODE1 = 2'b00; +parameter BIT_WIDTH_0 = 16; +parameter BIT_WIDTH_1 = 16; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCEA, OCEB; +input RESETA, RESETB; +input WREA, WREB; +input [13:0] ADA, ADB; +input [2:0] BLKSELA, BLKSELB; +input [15:0] DIA, DIB; +output [15:0] DOA, DOB; +endmodule + + +module DPX9B (...); +parameter READ_MODE0 = 1'b0; +parameter READ_MODE1 = 1'b0; +parameter WRITE_MODE0 = 2'b00; +parameter WRITE_MODE1 = 2'b00; +parameter BIT_WIDTH_0 = 18; +parameter BIT_WIDTH_1 = 18; +parameter BLK_SEL_0 = 3'b000; +parameter BLK_SEL_1 = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCEA, OCEB; +input RESETA, RESETB; +input WREA, WREB; +input [13:0] ADA, ADB; +input [17:0] DIA, DIB; +input [2:0] BLKSELA, BLKSELB; +output [17:0] DOA, DOB; +endmodule + + +module pROM (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 32; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +output [31:0] DO; +endmodule + + +module pROMX9 (...); +parameter READ_MODE = 1'b0; +parameter BIT_WIDTH = 36; +parameter RESET_MODE = "SYNC"; +parameter INIT_RAM_00 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 288'h000000000000000000000000000000000000000000000000000000000000000000000000; +input CLK, CE; +input OCE; +input RESET; +input [13:0] AD; +output [35:0] DO; +endmodule + + +module SDP36KE (...); +parameter ECC_WRITE_EN="TRUE"; +parameter ECC_READ_EN="TRUE"; +parameter READ_MODE = 1'b0; +parameter BLK_SEL_A = 3'b000; +parameter BLK_SEL_B = 3'b000; +parameter RESET_MODE = "SYNC"; +parameter INIT_FILE = "NONE"; +parameter INIT_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_10 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_11 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_12 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_13 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_14 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_15 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_16 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_17 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_18 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_19 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_1F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_20 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_21 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_22 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_23 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_24 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_25 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_26 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_27 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_28 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_29 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_2F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_30 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_31 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_32 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_33 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_34 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_35 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_36 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_37 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_38 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_39 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_3F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_40 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_41 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_42 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_43 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_44 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_45 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_46 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_47 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_48 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_49 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_4F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_50 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_51 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_52 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_53 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_54 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_55 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_56 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_57 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_58 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_59 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_5F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_60 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_61 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_62 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_63 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_64 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_65 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_66 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_67 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_68 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_69 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_6F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_70 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_71 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_72 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_73 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_74 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_75 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_76 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_77 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_78 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_79 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INIT_RAM_7F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_00 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_01 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_02 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_03 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_04 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_05 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_06 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_07 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_08 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_09 = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0A = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0B = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0C = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0D = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0E = 256'h0000000000000000000000000000000000000000000000000000000000000000; +parameter INITP_RAM_0F = 256'h0000000000000000000000000000000000000000000000000000000000000000; +input CLKA, CEA, CLKB, CEB; +input OCE; +input RESET; +input [8:0] ADA, ADB; +input [63:0] DI; +input [7:0] DIP; +input [2:0] BLKSELA, BLKSELB; +input DECCI, SECCI; +output [63:0] DO; +output [7:0] DOP; +output DECCO, SECCO; +output [7:0] ECCP; +endmodule + + +module SDP136K (...); +input CLKA, CLKB; +input WE, RE; +input [10:0] ADA, ADB; +input [67:0] DI; +output [67:0] DO; +endmodule + +module MULTADDALU12X12 (...); +parameter A0REG_CLK = "BYPASS"; +parameter A0REG_CE = "CE0"; +parameter A0REG_RESET = "RESET0"; +parameter A1REG_CLK = "BYPASS"; +parameter A1REG_CE = "CE0"; +parameter A1REG_RESET = "RESET0"; +parameter B0REG_CLK = "BYPASS"; +parameter B0REG_CE = "CE0"; +parameter B0REG_RESET = "RESET0"; +parameter B1REG_CLK = "BYPASS"; +parameter B1REG_CE = "CE0"; +parameter B1REG_RESET = "RESET0"; +parameter ACCSEL_IREG_CLK = "BYPASS"; +parameter ACCSEL_IREG_CE = "CE0"; +parameter ACCSEL_IREG_RESET = "RESET0"; +parameter CASISEL_IREG_CLK = "BYPASS"; +parameter CASISEL_IREG_CE = "CE0"; +parameter CASISEL_IREG_RESET = "RESET0"; +parameter ADDSUB0_IREG_CLK = "BYPASS"; +parameter ADDSUB0_IREG_CE = "CE0"; +parameter ADDSUB0_IREG_RESET = "RESET0"; +parameter ADDSUB1_IREG_CLK = "BYPASS"; +parameter ADDSUB1_IREG_CE = "CE0"; +parameter ADDSUB1_IREG_RESET = "RESET0"; +parameter PREG0_CLK = "BYPASS"; +parameter PREG0_CE = "CE0"; +parameter PREG0_RESET = "RESET0"; +parameter PREG1_CLK = "BYPASS"; +parameter PREG1_CE = "CE0"; +parameter PREG1_RESET = "RESET0"; +parameter FB_PREG_EN = "FALSE"; +parameter ACCSEL_PREG_CLK = "BYPASS"; +parameter ACCSEL_PREG_CE = "CE0"; +parameter ACCSEL_PREG_RESET = "RESET0"; +parameter CASISEL_PREG_CLK = "BYPASS"; +parameter CASISEL_PREG_CE = "CE0"; +parameter CASISEL_PREG_RESET = "RESET0"; +parameter ADDSUB0_PREG_CLK = "BYPASS"; +parameter ADDSUB0_PREG_CE = "CE0"; +parameter ADDSUB0_PREG_RESET = "RESET0"; +parameter ADDSUB1_PREG_CLK = "BYPASS"; +parameter ADDSUB1_PREG_CE = "CE0"; +parameter ADDSUB1_PREG_RESET = "RESET0"; +parameter OREG_CLK = "BYPASS"; +parameter OREG_CE = "CE0"; +parameter OREG_RESET = "RESET0"; +parameter MULT_RESET_MODE = "SYNC"; +parameter PRE_LOAD = 48'h000000000000; +parameter DYN_ADD_SUB_0 = "FALSE"; +parameter ADD_SUB_0 = 1'b0; +parameter DYN_ADD_SUB_1 = "FALSE"; +parameter ADD_SUB_1 = 1'b0; +parameter DYN_CASI_SEL = "FALSE"; +parameter CASI_SEL = 1'b0; +parameter DYN_ACC_SEL = "FALSE"; +parameter ACC_SEL = 1'b0; +output [47:0] DOUT, CASO; +input [11:0] A0, B0, A1, B1; +input [47:0] CASI; +input ACCSEL; +input CASISEL; +input [1:0] ADDSUB; +input [1:0] CLK, CE, RESET; +endmodule + +module MULTALU27X18 (...); +parameter AREG_CLK = "BYPASS"; +parameter AREG_CE = "CE0"; +parameter AREG_RESET = "RESET0"; +parameter BREG_CLK = "BYPASS"; +parameter BREG_CE = "CE0"; +parameter BREG_RESET = "RESET0"; +parameter DREG_CLK = "BYPASS"; +parameter DREG_CE = "CE0"; +parameter DREG_RESET = "RESET0"; +parameter C_IREG_CLK = "BYPASS"; +parameter C_IREG_CE = "CE0"; +parameter C_IREG_RESET = "RESET0"; +parameter PSEL_IREG_CLK = "BYPASS"; +parameter PSEL_IREG_CE = "CE0"; +parameter PSEL_IREG_RESET = "RESET0"; +parameter PADDSUB_IREG_CLK = "BYPASS"; +parameter PADDSUB_IREG_CE = "CE0"; +parameter PADDSUB_IREG_RESET = "RESET0"; +parameter ADDSUB0_IREG_CLK = "BYPASS"; +parameter ADDSUB0_IREG_CE = "CE0"; +parameter ADDSUB0_IREG_RESET = "RESET0"; +parameter ADDSUB1_IREG_CLK = "BYPASS"; +parameter ADDSUB1_IREG_CE = "CE0"; +parameter ADDSUB1_IREG_RESET = "RESET0"; +parameter CSEL_IREG_CLK = "BYPASS"; +parameter CSEL_IREG_CE = "CE0"; +parameter CSEL_IREG_RESET = "RESET0"; +parameter CASISEL_IREG_CLK = "BYPASS"; +parameter CASISEL_IREG_CE = "CE0"; +parameter CASISEL_IREG_RESET = "RESET0"; +parameter ACCSEL_IREG_CLK = "BYPASS"; +parameter ACCSEL_IREG_CE = "CE0"; +parameter ACCSEL_IREG_RESET = "RESET0"; +parameter PREG_CLK = "BYPASS"; +parameter PREG_CE = "CE0"; +parameter PREG_RESET = "RESET0"; +parameter ADDSUB0_PREG_CLK = "BYPASS"; +parameter ADDSUB0_PREG_CE = "CE0"; +parameter ADDSUB0_PREG_RESET = "RESET0"; +parameter ADDSUB1_PREG_CLK = "BYPASS"; +parameter ADDSUB1_PREG_CE = "CE0"; +parameter ADDSUB1_PREG_RESET = "RESET0"; +parameter CSEL_PREG_CLK = "BYPASS"; +parameter CSEL_PREG_CE = "CE0"; +parameter CSEL_PREG_RESET = "RESET0"; +parameter CASISEL_PREG_CLK = "BYPASS"; +parameter CASISEL_PREG_CE = "CE0"; +parameter CASISEL_PREG_RESET = "RESET0"; +parameter ACCSEL_PREG_CLK = "BYPASS"; +parameter ACCSEL_PREG_CE = "CE0"; +parameter ACCSEL_PREG_RESET = "RESET0"; +parameter C_PREG_CLK = "BYPASS"; +parameter C_PREG_CE = "CE0"; +parameter C_PREG_RESET = "RESET0"; +parameter FB_PREG_EN = "FALSE"; +parameter SOA_PREG_EN = "FALSE"; +parameter OREG_CLK = "BYPASS"; +parameter OREG_CE = "CE0"; +parameter OREG_RESET = "RESET0"; +parameter MULT_RESET_MODE = "SYNC"; +parameter PRE_LOAD = 48'h000000000000; +parameter DYN_P_SEL = "FALSE"; +parameter P_SEL = 1'b0; +parameter DYN_P_ADDSUB = "FALSE"; +parameter P_ADDSUB = 1'b0; +parameter DYN_A_SEL = "FALSE"; +parameter A_SEL = 1'b0; +parameter DYN_ADD_SUB_0 = "FALSE"; +parameter ADD_SUB_0 = 1'b0; +parameter DYN_ADD_SUB_1 = "FALSE"; +parameter ADD_SUB_1 = 1'b0; +parameter DYN_C_SEL = "FALSE"; +parameter C_SEL = 1'b1; +parameter DYN_CASI_SEL = "FALSE"; +parameter CASI_SEL = 1'b0; +parameter DYN_ACC_SEL = "FALSE"; +parameter ACC_SEL = 1'b0; +parameter MULT12X12_EN = "FALSE"; +output [47:0] DOUT, CASO; +output [26:0] SOA; +input [26:0] A, SIA; +input [17:0] B; +input [47:0] C; +input [25:0] D; +input [47:0] CASI; +input ACCSEL; +input PSEL; +input ASEL; +input PADDSUB; +input CSEL, CASISEL; +input [1:0] ADDSUB; +input [1:0] CLK, CE, RESET; +endmodule + +module MULT12X12 (...); +parameter AREG_CLK = "BYPASS"; +parameter AREG_CE = "CE0"; +parameter AREG_RESET = "RESET0"; +parameter BREG_CLK = "BYPASS"; +parameter BREG_CE = "CE0"; +parameter BREG_RESET = "RESET0"; +parameter PREG_CLK = "BYPASS"; +parameter PREG_CE = "CE0"; +parameter PREG_RESET = "RESET0"; +parameter OREG_CLK = "BYPASS"; +parameter OREG_CE = "CE0"; +parameter OREG_RESET = "RESET0"; +parameter MULT_RESET_MODE = "SYNC"; +output [23:0] DOUT; +input [11:0] A, B; +input [1:0] CLK, CE, RESET; +endmodule + +module MULT27X36 (...); +parameter AREG_CLK = "BYPASS"; +parameter AREG_CE = "CE0"; +parameter AREG_RESET = "RESET0"; +parameter BREG_CLK = "BYPASS"; +parameter BREG_CE = "CE0"; +parameter BREG_RESET = "RESET0"; +parameter DREG_CLK = "BYPASS"; +parameter DREG_CE = "CE0"; +parameter DREG_RESET = "RESET0"; +parameter PADDSUB_IREG_CLK = "BYPASS"; +parameter PADDSUB_IREG_CE = "CE0"; +parameter PADDSUB_IREG_RESET = "RESET0"; +parameter PREG_CLK = "BYPASS"; +parameter PREG_CE = "CE0"; +parameter PREG_RESET = "RESET0"; +parameter PSEL_IREG_CLK = "BYPASS"; +parameter PSEL_IREG_CE = "CE0"; +parameter PSEL_IREG_RESET = "RESET0"; +parameter OREG_CLK = "BYPASS"; +parameter OREG_CE = "CE0"; +parameter OREG_RESET = "RESET0"; +parameter MULT_RESET_MODE = "SYNC"; +parameter DYN_P_SEL = "FALSE"; +parameter P_SEL = 1'b0; +parameter DYN_P_ADDSUB = "FALSE"; +parameter P_ADDSUB = 1'b0; +output [62:0] DOUT; +input [26:0] A; +input [35:0] B; +input [25:0] D; +input [1:0] CLK, CE, RESET; +input PSEL; +input PADDSUB; +endmodule + +module MULTACC (...); +output [23:0] DATAO, CASO; +input CE, CLK; +input [5:0] COFFIN0, COFFIN1, COFFIN2; +input [9:0] DATAIN0, DATAIN1; +input [9:0] DATAIN2; +input RSTN; +input [23:0] CASI; +parameter COFFIN_WIDTH = 4; +parameter DATAIN_WIDTH = 8; +parameter IREG = 1'b0; +parameter OREG = 1'b0; +parameter PREG = 1'b0; +parameter ACC_EN = "FALSE"; +parameter CASI_EN = "FALSE"; +parameter CASO_EN = "FALSE"; +endmodule + +module IDDR_MEM (...); +input D, ICLK, PCLK; +input [2:0] WADDR; +input [2:0] RADDR; +input RESET; +output Q0,Q1; +endmodule + + +module ODDR_MEM (...); +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1; +input TX, PCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module IDES4_MEM (...); +input PCLK, D, ICLK, FCLK, RESET, CALIB; +input [2:0] WADDR; +input [2:0] RADDR; +output Q0,Q1,Q2,Q3; +endmodule + + +module IDES8_MEM (...); +input PCLK, D, ICLK, FCLK, RESET, CALIB; +input [2:0] WADDR; +input [2:0] RADDR; +output Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7; +endmodule + + +module IDES14 (...); +input D, FCLK, PCLK, CALIB,RESET; +output Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12, Q13; +endmodule + + +module IDES32 (...); +input D, FCLK, PCLK, CALIB,RESET; +output Q0, Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q10, Q11, Q12, Q13, Q14, Q15, Q16, Q17, Q18, Q19, Q20, Q21, Q22, Q23, Q24, Q25, Q26, Q27, Q28, Q29, Q30, Q31; +endmodule + + +module OSER4_MEM (...); +parameter HWL = "false"; +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1, D2, D3; +input TX0, TX1; +input PCLK, FCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module OSER8_MEM (...); +parameter HWL = "false"; +parameter TCLK_SOURCE = "DQSW"; +parameter TXCLK_POL = 1'b0; +input D0, D1, D2, D3, D4, D5, D6, D7; +input TX0, TX1, TX2, TX3; +input PCLK, FCLK, TCLK, RESET; +output Q0, Q1; +endmodule + + +module IODELAY (...); +parameter C_STATIC_DLY = 0; +parameter DYN_DLY_EN = "FALSE"; +parameter ADAPT_EN = "FALSE"; +input DI; +input SDTAP; +input VALUE; +input [7:0] DLYSTEP; +output DF; +output DO; +endmodule + + +module OSIDES32 (...); +output [31:0] Q; +input D; +input PCLK, FCLKP, FCLKN, FCLKQP, FCLKQN; +input RESET; +output DF0, DF1; +input SDTAP0, SDTAP1; +input VALUE0,VALUE1; +input [7:0] DLYSTEP0,DLYSTEP1; +parameter C_STATIC_DLY_0 = 0; +parameter DYN_DLY_EN_0 = "FALSE"; +parameter ADAPT_EN_0 = "FALSE"; +parameter C_STATIC_DLY_1 = 0; +parameter DYN_DLY_EN_1 = "FALSE"; +parameter ADAPT_EN_1 = "FALSE"; +endmodule + +module OSIDES64 (...); +output [63:0] Q; +input D; +input PCLK, FCLKP, FCLKN, FCLKQP, FCLKQN; +input RESET; +output DF0, DF1, DF2, DF3; +input SDTAP0, SDTAP1, SDTAP2, SDTAP3; +input VALUE0, VALUE1, VALUE2, VALUE3; +input [7:0] DLYSTEP0, DLYSTEP1, DLYSTEP2, DLYSTEP3; +parameter C_STATIC_DLY_0 = 0; +parameter DYN_DLY_EN_0 = "FALSE"; +parameter ADAPT_EN_0 = "FALSE"; +parameter C_STATIC_DLY_1 = 0; +parameter DYN_DLY_EN_1 = "FALSE"; +parameter ADAPT_EN_1 = "FALSE"; +parameter C_STATIC_DLY_2 = 0; +parameter DYN_DLY_EN_2 = "FALSE"; +parameter ADAPT_EN_2 = "FALSE"; +parameter C_STATIC_DLY_3 = 0; +parameter DYN_DLY_EN_3 = "FALSE"; +parameter ADAPT_EN_3 = "FALSE"; +endmodule + +module DCE (...); +input CLKIN; +input CE; +output CLKOUT; +endmodule + +module DDRDLL (...); +input CLKIN; +input STOP; +input UPDNCNTL; +input RESET; +output [7:0]STEP; +output LOCK; +parameter DLL_FORCE = "FALSE"; +parameter CODESCAL = "000"; +parameter SCAL_EN = "TRUE"; +parameter DIV_SEL = 1'b0; +endmodule + +module DLLDLY (...); +input CLKIN; +input [7:0] DLLSTEP, CSTEP; +input LOADN,MOVE; +output CLKOUT; +output FLAG; +parameter DLY_SIGN = 1'b0; +parameter DLY_ADJ = 0; +parameter DYN_DLY_EN = "FALSE"; +parameter ADAPT_EN = "FALSE"; +parameter STEP_SEL = 1'b0; +endmodule + +module CLKDIV (...); +input HCLKIN; +input RESETN; +input CALIB; +output CLKOUT; +parameter DIV_MODE = "2"; +endmodule + +module CLKDIV2 (...); +input HCLKIN, RESETN; +output CLKOUT; +endmodule + +module DHCE (...); +input CLKIN; +input CEN; +output CLKOUT; +endmodule + +module OSCA (...); +parameter FREQ_DIV = 100; +output OSCOUT; +input OSCEN; +endmodule + +module OSCB (...); +parameter FREQ_MODE = "25"; +parameter FREQ_DIV = 10; +parameter DYN_TRIM_EN = "FALSE"; +output OSCOUT; +output OSCREF; +input OSCEN, FMODE; +input [7:0] RTRIM; +input [5:0] RTCTRIM; +endmodule + +module PLL (...); +input CLKIN; +input CLKFB; +input RESET; +input PLLPWD; +input RESET_I; +input RESET_O; +input [5:0] FBDSEL; +input [5:0] IDSEL; +input [6:0] MDSEL; +input [2:0] MDSEL_FRAC; +input [6:0] ODSEL0; +input [2:0] ODSEL0_FRAC; +input [6:0] ODSEL1; +input [6:0] ODSEL2; +input [6:0] ODSEL3; +input [6:0] ODSEL4; +input [6:0] ODSEL5; +input [6:0] ODSEL6; +input [3:0] DT0,DT1,DT2,DT3; +input [5:0] ICPSEL; +input [2:0] LPFRES; +input [1:0] LPFCAP; +input [2:0] PSSEL; +input PSDIR; +input PSPULSE; +input ENCLK0; +input ENCLK1; +input ENCLK2; +input ENCLK3; +input ENCLK4; +input ENCLK5; +input ENCLK6; +input SSCPOL; +input SSCON; +input [6:0] SSCMDSEL; +input [2:0] SSCMDSEL_FRAC; +output LOCK; +output CLKOUT0; +output CLKOUT1; +output CLKOUT2; +output CLKOUT3; +output CLKOUT4; +output CLKOUT5; +output CLKOUT6; +output CLKFBOUT; +parameter FCLKIN = "100.0"; +parameter DYN_IDIV_SEL= "FALSE"; +parameter IDIV_SEL = 1; +parameter DYN_FBDIV_SEL= "FALSE"; +parameter FBDIV_SEL = 1; +parameter DYN_ODIV0_SEL= "FALSE"; +parameter ODIV0_SEL = 8; +parameter DYN_ODIV1_SEL= "FALSE"; +parameter ODIV1_SEL = 8; +parameter DYN_ODIV2_SEL= "FALSE"; +parameter ODIV2_SEL = 8; +parameter DYN_ODIV3_SEL= "FALSE"; +parameter ODIV3_SEL = 8; +parameter DYN_ODIV4_SEL= "FALSE"; +parameter ODIV4_SEL = 8; +parameter DYN_ODIV5_SEL= "FALSE"; +parameter ODIV5_SEL = 8; +parameter DYN_ODIV6_SEL= "FALSE"; +parameter ODIV6_SEL = 8; +parameter DYN_MDIV_SEL= "FALSE"; +parameter MDIV_SEL = 8; +parameter MDIV_FRAC_SEL = 0; +parameter ODIV0_FRAC_SEL = 0; +parameter CLKOUT0_EN = "TRUE"; +parameter CLKOUT1_EN = "FALSE"; +parameter CLKOUT2_EN = "FALSE"; +parameter CLKOUT3_EN = "FALSE"; +parameter CLKOUT4_EN = "FALSE"; +parameter CLKOUT5_EN = "FALSE"; +parameter CLKOUT6_EN = "FALSE"; +parameter CLKFB_SEL = "INTERNAL"; +parameter DYN_DT0_SEL = "FALSE"; +parameter DYN_DT1_SEL = "FALSE"; +parameter DYN_DT2_SEL = "FALSE"; +parameter DYN_DT3_SEL = "FALSE"; +parameter CLKOUT0_DT_DIR = 1'b1; +parameter CLKOUT1_DT_DIR = 1'b1; +parameter CLKOUT2_DT_DIR = 1'b1; +parameter CLKOUT3_DT_DIR = 1'b1; +parameter CLKOUT0_DT_STEP = 0; +parameter CLKOUT1_DT_STEP = 0; +parameter CLKOUT2_DT_STEP = 0; +parameter CLKOUT3_DT_STEP = 0; +parameter CLK0_IN_SEL = 1'b0; +parameter CLK0_OUT_SEL = 1'b0; +parameter CLK1_IN_SEL = 1'b0; +parameter CLK1_OUT_SEL = 1'b0; +parameter CLK2_IN_SEL = 1'b0; +parameter CLK2_OUT_SEL = 1'b0; +parameter CLK3_IN_SEL = 1'b0; +parameter CLK3_OUT_SEL = 1'b0; +parameter CLK4_IN_SEL = 2'b00; +parameter CLK4_OUT_SEL = 1'b0; +parameter CLK5_IN_SEL = 1'b0; +parameter CLK5_OUT_SEL = 1'b0; +parameter CLK6_IN_SEL = 1'b0; +parameter CLK6_OUT_SEL = 1'b0; +parameter DYN_DPA_EN = "FALSE"; +parameter CLKOUT0_PE_COARSE = 0; +parameter CLKOUT0_PE_FINE = 0; +parameter CLKOUT1_PE_COARSE = 0; +parameter CLKOUT1_PE_FINE = 0; +parameter CLKOUT2_PE_COARSE = 0; +parameter CLKOUT2_PE_FINE = 0; +parameter CLKOUT3_PE_COARSE = 0; +parameter CLKOUT3_PE_FINE = 0; +parameter CLKOUT4_PE_COARSE = 0; +parameter CLKOUT4_PE_FINE = 0; +parameter CLKOUT5_PE_COARSE = 0; +parameter CLKOUT5_PE_FINE = 0; +parameter CLKOUT6_PE_COARSE = 0; +parameter CLKOUT6_PE_FINE = 0; +parameter DYN_PE0_SEL = "FALSE"; +parameter DYN_PE1_SEL = "FALSE"; +parameter DYN_PE2_SEL = "FALSE"; +parameter DYN_PE3_SEL = "FALSE"; +parameter DYN_PE4_SEL = "FALSE"; +parameter DYN_PE5_SEL = "FALSE"; +parameter DYN_PE6_SEL = "FALSE"; +parameter DE0_EN = "FALSE"; +parameter DE1_EN = "FALSE"; +parameter DE2_EN = "FALSE"; +parameter DE3_EN = "FALSE"; +parameter DE4_EN = "FALSE"; +parameter DE5_EN = "FALSE"; +parameter DE6_EN = "FALSE"; +parameter RESET_I_EN = "FALSE"; +parameter RESET_O_EN = "FALSE"; +parameter DYN_ICP_SEL= "FALSE"; +parameter ICP_SEL = 6'bXXXXXX; +parameter DYN_LPF_SEL= "FALSE"; +parameter LPF_RES = 3'bXXX; +parameter LPF_CAP = 2'b00; +parameter SSC_EN = "FALSE"; +endmodule + +module PLLA (...); +input CLKIN; +input CLKFB; +input RESET; +input PLLPWD; +input RESET_I; +input RESET_O; +input [2:0] PSSEL; +input PSDIR; +input PSPULSE; +input SSCPOL; +input SSCON; +input [6:0] SSCMDSEL; +input [2:0] SSCMDSEL_FRAC; +input MDCLK; +input [1:0] MDOPC; +input MDAINC; +input [7:0] MDWDI; +output [7:0] MDRDO; +output LOCK; +output CLKOUT0; +output CLKOUT1; +output CLKOUT2; +output CLKOUT3; +output CLKOUT4; +output CLKOUT5; +output CLKOUT6; +output CLKFBOUT; +parameter FCLKIN = "100.0"; +parameter IDIV_SEL = 1; +parameter FBDIV_SEL = 1; +parameter ODIV0_SEL = 8; +parameter ODIV1_SEL = 8; +parameter ODIV2_SEL = 8; +parameter ODIV3_SEL = 8; +parameter ODIV4_SEL = 8; +parameter ODIV5_SEL = 8; +parameter ODIV6_SEL = 8; +parameter MDIV_SEL = 8; +parameter MDIV_FRAC_SEL = 0; +parameter ODIV0_FRAC_SEL = 0; +parameter CLKOUT0_EN = "TRUE"; +parameter CLKOUT1_EN = "FALSE"; +parameter CLKOUT2_EN = "FALSE"; +parameter CLKOUT3_EN = "FALSE"; +parameter CLKOUT4_EN = "FALSE"; +parameter CLKOUT5_EN = "FALSE"; +parameter CLKOUT6_EN = "FALSE"; +parameter CLKFB_SEL = "INTERNAL"; +parameter CLKOUT0_DT_DIR = 1'b1; +parameter CLKOUT1_DT_DIR = 1'b1; +parameter CLKOUT2_DT_DIR = 1'b1; +parameter CLKOUT3_DT_DIR = 1'b1; +parameter CLKOUT0_DT_STEP = 0; +parameter CLKOUT1_DT_STEP = 0; +parameter CLKOUT2_DT_STEP = 0; +parameter CLKOUT3_DT_STEP = 0; +parameter CLK0_IN_SEL = 1'b0; +parameter CLK0_OUT_SEL = 1'b0; +parameter CLK1_IN_SEL = 1'b0; +parameter CLK1_OUT_SEL = 1'b0; +parameter CLK2_IN_SEL = 1'b0; +parameter CLK2_OUT_SEL = 1'b0; +parameter CLK3_IN_SEL = 1'b0; +parameter CLK3_OUT_SEL = 1'b0; +parameter CLK4_IN_SEL = 2'b00; +parameter CLK4_OUT_SEL = 1'b0; +parameter CLK5_IN_SEL = 1'b0; +parameter CLK5_OUT_SEL = 1'b0; +parameter CLK6_IN_SEL = 1'b0; +parameter CLK6_OUT_SEL = 1'b0; +parameter DYN_DPA_EN = "FALSE"; +parameter CLKOUT0_PE_COARSE = 0; +parameter CLKOUT0_PE_FINE = 0; +parameter CLKOUT1_PE_COARSE = 0; +parameter CLKOUT1_PE_FINE = 0; +parameter CLKOUT2_PE_COARSE = 0; +parameter CLKOUT2_PE_FINE = 0; +parameter CLKOUT3_PE_COARSE = 0; +parameter CLKOUT3_PE_FINE = 0; +parameter CLKOUT4_PE_COARSE = 0; +parameter CLKOUT4_PE_FINE = 0; +parameter CLKOUT5_PE_COARSE = 0; +parameter CLKOUT5_PE_FINE = 0; +parameter CLKOUT6_PE_COARSE = 0; +parameter CLKOUT6_PE_FINE = 0; +parameter DYN_PE0_SEL = "FALSE"; +parameter DYN_PE1_SEL = "FALSE"; +parameter DYN_PE2_SEL = "FALSE"; +parameter DYN_PE3_SEL = "FALSE"; +parameter DYN_PE4_SEL = "FALSE"; +parameter DYN_PE5_SEL = "FALSE"; +parameter DYN_PE6_SEL = "FALSE"; +parameter DE0_EN = "FALSE"; +parameter DE1_EN = "FALSE"; +parameter DE2_EN = "FALSE"; +parameter DE3_EN = "FALSE"; +parameter DE4_EN = "FALSE"; +parameter DE5_EN = "FALSE"; +parameter DE6_EN = "FALSE"; +parameter RESET_I_EN = "FALSE"; +parameter RESET_O_EN = "FALSE"; +parameter ICP_SEL = 6'bXXXXXX; +parameter LPF_RES = 3'bXXX; +parameter LPF_CAP = 2'b00; +parameter SSC_EN = "FALSE"; +endmodule + +module AE350_SOC (...); +input POR_N; +input HW_RSTN; +input CORE_CLK; +input DDR_CLK; +input AHB_CLK; +input APB_CLK; +input DBG_TCK; +input RTC_CLK; +input CORE_CE; +input AXI_CE; +input DDR_CE; +input AHB_CE; +input [7:0] APB_CE; +input APB2AHB_CE; +input SCAN_TEST; +input SCAN_EN; +output PRESETN; +output HRESETN; +output DDR_RSTN; +input [15:0] GP_INT; +input [ 7:0] DMA_REQ; +output [ 7:0] DMA_ACK; +output CORE0_WFI_MODE; +input WAKEUP_IN; +output RTC_WAKEUP; +input TEST_CLK; +input TEST_MODE; +input TEST_RSTN; +output [31:0] ROM_HADDR; +input [31:0] ROM_HRDATA; +input ROM_HREADY; +input ROM_HRESP; +output [1:0] ROM_HTRANS; +output ROM_HWRITE; +output [31:0] APB_PADDR; +output APB_PENABLE; +input [31:0] APB_PRDATA; +input APB_PREADY; +output APB_PSEL; +output [31:0] APB_PWDATA; +output APB_PWRITE; +input APB_PSLVERR; +output [2:0] APB_PPROT; +output [3:0] APB_PSTRB; +input [31:0] EXTS_HRDATA; +input EXTS_HREADYIN; +input EXTS_HRESP; +output [31:0] EXTS_HADDR; +output [2:0] EXTS_HBURST; +output [3:0] EXTS_HPROT; +output EXTS_HSEL; +output [2:0] EXTS_HSIZE; +output [1:0] EXTS_HTRANS; +output [31:0] EXTS_HWDATA; +output EXTS_HWRITE; +input [31:0] EXTM_HADDR; +input [2:0] EXTM_HBURST; +input [3:0] EXTM_HPROT; +output [63:0] EXTM_HRDATA; +input EXTM_HREADY; +output EXTM_HREADYOUT; +output EXTM_HRESP; +input EXTM_HSEL; +input [2:0] EXTM_HSIZE; +input [1:0] EXTM_HTRANS; +input [63:0] EXTM_HWDATA; +input EXTM_HWRITE; +output [31:0] DDR_HADDR; +output [2:0] DDR_HBURST; +output [3:0] DDR_HPROT; +input [63:0] DDR_HRDATA; +input DDR_HREADY; +input DDR_HRESP; +output [2:0] DDR_HSIZE; +output [1:0] DDR_HTRANS; +output [63:0] DDR_HWDATA; +output DDR_HWRITE; +input TMS_IN; +input TRST_IN; +input TDI_IN; +output TDO_OUT; +output TDO_OE; +input SPI2_HOLDN_IN; +input SPI2_WPN_IN; +input SPI2_CLK_IN; +input SPI2_CSN_IN; +input SPI2_MISO_IN; +input SPI2_MOSI_IN; +output SPI2_HOLDN_OUT; +output SPI2_HOLDN_OE; +output SPI2_WPN_OUT; +output SPI2_WPN_OE; +output SPI2_CLK_OUT; +output SPI2_CLK_OE; +output SPI2_CSN_OUT; +output SPI2_CSN_OE; +output SPI2_MISO_OUT; +output SPI2_MISO_OE; +output SPI2_MOSI_OUT; +output SPI2_MOSI_OE; +input I2C_SCL_IN; +input I2C_SDA_IN; +output I2C_SCL; +output I2C_SDA; +output UART1_TXD; +output UART1_RTSN; +input UART1_RXD; +input UART1_CTSN; +input UART1_DSRN; +input UART1_DCDN; +input UART1_RIN; +output UART1_DTRN; +output UART1_OUT1N; +output UART1_OUT2N; +output UART2_TXD; +output UART2_RTSN; +input UART2_RXD; +input UART2_CTSN; +input UART2_DCDN; +input UART2_DSRN; +input UART2_RIN; +output UART2_DTRN; +output UART2_OUT1N; +output UART2_OUT2N; +output CH0_PWM; +output CH0_PWMOE; +output CH1_PWM; +output CH1_PWMOE; +output CH2_PWM; +output CH2_PWMOE; +output CH3_PWM; +output CH3_PWMOE; +input [31:0] GPIO_IN; +output [31:0] GPIO_OE; +output [31:0] GPIO_OUT; +input [19:0] SCAN_IN; +input INTEG_TCK; +input INTEG_TDI; +input INTEG_TMS; +input INTEG_TRST; +output INTEG_TDO; +output [19:0] SCAN_OUT; +input PGEN_CHAIN_I; +output PRDYN_CHAIN_O; +input [2:0] EMA; +input [1:0] EMAW; +input EMAS; +input RET1N; +input RET2N; +endmodule + +module AE350_RAM (...); +input POR_N; +input HW_RSTN; +input CORE_CLK; +input AHB_CLK; +input APB_CLK; +input RTC_CLK; +input CORE_CE; +input AXI_CE; +input AHB_CE; +input [31:0] EXTM_HADDR; +input [2:0] EXTM_HBURST; +input [3:0] EXTM_HPROT; +output [63:0] EXTM_HRDATA; +input EXTM_HREADY; +output EXTM_HREADYOUT; +output EXTM_HRESP; +input EXTM_HSEL; +input [2:0] EXTM_HSIZE; +input [1:0] EXTM_HTRANS; +input [63:0] EXTM_HWDATA; +input EXTM_HWRITE; +input [2:0] EMA; +input [1:0] EMAW; +input EMAS; +input RET1N; +input RET2N; +endmodule + +module SAMB (...); +parameter MODE = 2'b00; +input [23:0] SPIAD; +input LOAD; +input ADWSEL; +endmodule + +module OTP (...); +parameter MODE = 2'b01; +input CLK, READ, SHIFT; +output DOUT; +endmodule + +module CMSER (...); +output RUNNING; +output CRCERR; +output CRCDONE; +output ECCCORR; +output ECCUNCORR; +output [27:0] ERRLOC; +output ECCDEC; +output DSRRD; +output DSRWR; +output ASRRESET; +output ASRINC; +output REFCLK; +input CLK; +input [2:0] SEREN; +input ERRINJECT; +input [6:0] ERRINJLOC; +endmodule + +module CMSERA (...); +output RUNNING; +output CRCERR; +output CRCDONE; +output ECCCORR; +output ECCUNCORR; +output [26:0] ERR0LOC; +output [26:0] ERR1LOC; +output ECCDEC; +output DSRRD; +output DSRWR; +output ASRRESET; +output ASRINC; +output REFCLK; +input CLK; +input [2:0] SEREN; +input ERR0INJECT,ERR1INJECT; +input [6:0] ERRINJ0LOC,ERRINJ1LOC; +endmodule + +module CMSERB (...); +output RUNNING; +output CRCERR; +output CRCDONE; +output ECCCORR; +output ECCUNCORR; +output [12:0] ERRLOC; +output ECCDEC; +output DSRRD; +output DSRWR; +output ASRRESET; +output ASRINC; +output REFCLK; +input CLK; +input [2:0] SEREN; +input ERR0INJECT,ERR1INJECT; +input [6:0] ERRINJ0LOC,ERRINJ1LOC; +endmodule + +module SAMBA (...); +parameter MODE = 2'b00; +input SPIAD; +input LOAD; +endmodule + +module ADCLRC (...); +endmodule + +module ADCULC (...); +endmodule + +module ADC (...); +endmodule + +module MIPI_DPHY_RX (...); +output D0LN_DESKEW_DONE,D1LN_DESKEW_DONE,D2LN_DESKEW_DONE,D3LN_DESKEW_DONE; +output [15:0] D0LN_HSRXD, D1LN_HSRXD, D2LN_HSRXD, D3LN_HSRXD; +output D0LN_HSRXD_VLD,D1LN_HSRXD_VLD,D2LN_HSRXD_VLD,D3LN_HSRXD_VLD; +output DI_LPRX0_N, DI_LPRX0_P, DI_LPRX1_N, DI_LPRX1_P, DI_LPRX2_N, DI_LPRX2_P, DI_LPRX3_N, DI_LPRX3_P; +output DI_LPRXCK_N, DI_LPRXCK_P; +output RX_CLK_O; +output DESKEW_ERROR; +inout CK_N, CK_P, RX0_N, RX0_P, RX1_N, RX1_P, RX2_N, RX2_P, RX3_N, RX3_P; +input BYTE_LENDIAN; +input [2:0] FIFO_RD_STD; +input HSRX_STOP; +input PWRON; +input RESET; +input D0LN_HSRX_DREN, D1LN_HSRX_DREN, D2LN_HSRX_DREN, D3LN_HSRX_DREN; +input DESKEW_BY,DESKEW_EN_OEDGE; +input [5:0] DESKEW_HALF_OPENING; +input [2:0] DESKEW_LNSEL; +input [1:0] DESKEW_LSB_MODE; +input [2:0] DESKEW_M; +input [12:0] DESKEW_MTH; +input [6:0] DESKEW_MSET; +input DESKEW_OCLKEDG_EN; +input [6:0] DESKEW_OWVAL; +input DESKEW_REQ; +input DO_LPTX0_N, DO_LPTX0_P, DO_LPTX1_N, DO_LPTX1_P, DO_LPTX2_N, DO_LPTX2_P, DO_LPTX3_N, DO_LPTX3_P; +input DO_LPTXCK_N, DO_LPTXCK_P; +input DRST_N; +input [2:0] EQCS_LANE0,EQCS_LANE1,EQCS_LANE2,EQCS_LANE3,EQCS_CK; +input [2:0] EQRS_LANE0,EQRS_LANE1,EQRS_LANE2,EQRS_LANE3,EQRS_CK; +input HS_8BIT_MODE; +input HSRX_DLYDIR_LANE0, HSRX_DLYDIR_LANE1,HSRX_DLYDIR_LANE2,HSRX_DLYDIR_LANE3,HSRX_DLYDIR_CK; +input HSRX_DLYLDN_LANE0, HSRX_DLYLDN_LANE1,HSRX_DLYLDN_LANE2,HSRX_DLYLDN_LANE3,HSRX_DLYLDN_CK; +input HSRX_DLYMV_LANE0, HSRX_DLYMV_LANE1,HSRX_DLYMV_LANE2,HSRX_DLYMV_LANE3,HSRX_DLYMV_CK; +input HSRX_EN_CK; +input HSRX_ODTEN_CK, HSRX_ODTEN_D0, HSRX_ODTEN_D1, HSRX_ODTEN_D2, HSRX_ODTEN_D3; +input LALIGN_EN; +input LPRX_EN_CK, LPRX_EN_D0, LPRX_EN_D1, LPRX_EN_D2, LPRX_EN_D3; +input LPTX_EN_CK, LPTX_EN_D0, LPTX_EN_D1, LPTX_EN_D2, LPTX_EN_D3; +input ONE_BYTE0_MATCH; +input RX_CLK_1X; +input RX_INVERT; +input WALIGN_BY; +input WALIGN_DVLD; +input WORD_LENDIAN; +parameter ALIGN_BYTE = 8'b10111000; +parameter EN_CLKB1X = 1'b1; +parameter EQ_ADPSEL_LANE0 = 1'b0; +parameter EQ_ADPSEL_LANE1 = 1'b0; +parameter EQ_ADPSEL_LANE2 = 1'b0; +parameter EQ_ADPSEL_LANE3 = 1'b0; +parameter EQ_ADPSEL_CK = 1'b0; +parameter EQ_CS_LANE0 = 3'b100; +parameter EQ_CS_LANE1 = 3'b100; +parameter EQ_CS_LANE2 = 3'b100; +parameter EQ_CS_LANE3 = 3'b100; +parameter EQ_CS_CK = 3'b100; +parameter EQ_PBIAS_LANE0 = 4'b0100; +parameter EQ_PBIAS_LANE1 = 4'b0100; +parameter EQ_PBIAS_LANE2 = 4'b0100; +parameter EQ_PBIAS_LANE3 = 4'b0100; +parameter EQ_PBIAS_CK = 4'b0100; +parameter EQ_RS_LANE0 = 3'b100; +parameter EQ_RS_LANE1 = 3'b100; +parameter EQ_RS_LANE2 = 3'b100; +parameter EQ_RS_LANE3 = 3'b100; +parameter EQ_RS_CK = 3'b100; +parameter EQ_ZLD_LANE0 = 4'b1000; +parameter EQ_ZLD_LANE1 = 4'b1000; +parameter EQ_ZLD_LANE2 = 4'b1000; +parameter EQ_ZLD_LANE3 = 4'b1000; +parameter EQ_ZLD_CK = 4'b1000; +parameter HIGH_BW_LANE0 = 1'b1; +parameter HIGH_BW_LANE1 = 1'b1; +parameter HIGH_BW_LANE2 = 1'b1; +parameter HIGH_BW_LANE3 = 1'b1; +parameter HIGH_BW_CK = 1'b1; +parameter HSRX_DLYCTL_CK = 7'b0000000; +parameter HSRX_DLYCTL_LANE0 = 7'b0000000; +parameter HSRX_DLYCTL_LANE1 = 7'b0000000; +parameter HSRX_DLYCTL_LANE2 = 7'b0000000; +parameter HSRX_DLYCTL_LANE3 = 7'b0000000; +parameter HSRX_DLY_SEL = 1'b0; +parameter HSRX_DUTY_LANE0 = 4'b1000; +parameter HSRX_DUTY_LANE1 = 4'b1000; +parameter HSRX_DUTY_LANE2 = 4'b1000; +parameter HSRX_DUTY_LANE3 = 4'b1000; +parameter HSRX_DUTY_CK = 4'b1000; +parameter HSRX_EN = 1'b1; +parameter HSRX_EQ_EN_LANE0 = 1'b1; +parameter HSRX_EQ_EN_LANE1 = 1'b1; +parameter HSRX_EQ_EN_LANE2 = 1'b1; +parameter HSRX_EQ_EN_LANE3 = 1'b1; +parameter HSRX_EQ_EN_CK = 1'b1; +parameter HSRX_IBIAS = 4'b0011; +parameter HSRX_IMARG_EN = 1'b1; +parameter MIPI_LANE0_EN = 1'b0; +parameter MIPI_LANE1_EN = 1'b0; +parameter MIPI_LANE2_EN = 1'b0; +parameter MIPI_LANE3_EN = 1'b0; +parameter MIPI_CK_EN = 1'b1; +parameter HSRX_ODT_EN = 1'b1; +parameter HSRX_ODT_TST = 4'b0000; +parameter HSRX_ODT_TST_CK = 1'b0; +parameter HSRX_STOP_EN = 1'b0; +parameter HSRX_TST = 4'b0000; +parameter HSRX_TST_CK = 1'b0; +parameter HSRX_WAIT4EDGE = 1'b0; +parameter HYST_NCTL = 2'b01; +parameter HYST_PCTL = 2'b01; +parameter LOW_LPRX_VTH = 1'b0; +parameter LPRX_EN = 1'b1; +parameter LPRX_TST = 4'b0000; +parameter LPRX_TST_CK = 1'b0; +parameter LPTX_EN = 1'b1; +parameter LPTX_SW_LANE0 = 3'b100; +parameter LPTX_SW_LANE1 = 3'b100; +parameter LPTX_SW_LANE2 = 3'b100; +parameter LPTX_SW_LANE3 = 3'b100; +parameter LPTX_SW_CK = 3'b100; +parameter LPTX_TST = 4'b0000; +parameter LPTX_TST_CK = 1'b0; +parameter MIPI_DIS_N = 1'b1; +parameter PGA_BIAS_LANE0 = 4'b1000; +parameter PGA_BIAS_LANE1 = 4'b1000; +parameter PGA_BIAS_LANE2 = 4'b1000; +parameter PGA_BIAS_LANE3 = 4'b1000; +parameter PGA_BIAS_CK = 4'b1000; +parameter PGA_GAIN_LANE0 = 4'b1000; +parameter PGA_GAIN_LANE1 = 4'b1000; +parameter PGA_GAIN_LANE2 = 4'b1000; +parameter PGA_GAIN_LANE3 = 4'b1000; +parameter PGA_GAIN_CK = 4'b1000; +parameter RX_CLK1X_SYNC_SEL = 1'b0; +parameter RX_ODT_TRIM_LANE0 = 4'b0111; +parameter RX_ODT_TRIM_LANE1 = 4'b0111; +parameter RX_ODT_TRIM_LANE2 = 4'b0111; +parameter RX_ODT_TRIM_LANE3 = 4'b0111; +parameter RX_ODT_TRIM_CK = 4'b0111; +parameter STP_UNIT = 2'b00; +parameter SYNC_CLK_SEL = 1'b1; +parameter WALIGN_DVLD_SRC_SEL = 1'b0; +endmodule + +module MIPI_DPHY (...); +output RX_CLK_O, TX_CLK_O; +output [15:0] D0LN_HSRXD, D1LN_HSRXD, D2LN_HSRXD, D3LN_HSRXD; +output D0LN_HSRXD_VLD,D1LN_HSRXD_VLD,D2LN_HSRXD_VLD,D3LN_HSRXD_VLD; +input D0LN_HSRX_DREN, D1LN_HSRX_DREN, D2LN_HSRX_DREN, D3LN_HSRX_DREN; +output DI_LPRX0_N, DI_LPRX0_P, DI_LPRX1_N, DI_LPRX1_P, DI_LPRX2_N, DI_LPRX2_P, DI_LPRX3_N, DI_LPRX3_P, DI_LPRXCK_N, DI_LPRXCK_P; +inout CK_N, CK_P, D0_N, D0_P, D1_N, D1_P, D2_N, D2_P, D3_N, D3_P; +input HSRX_STOP, HSTXEN_LN0, HSTXEN_LN1, HSTXEN_LN2, HSTXEN_LN3, HSTXEN_LNCK; +input PWRON_RX, PWRON_TX, RESET, RX_CLK_1X, TX_CLK_1X; +input TXDPEN_LN0, TXDPEN_LN1, TXDPEN_LN2, TXDPEN_LN3, TXDPEN_LNCK, TXHCLK_EN; +input [15:0] CKLN_HSTXD,D0LN_HSTXD,D1LN_HSTXD,D2LN_HSTXD,D3LN_HSTXD; +input HSTXD_VLD; +input CK0, CK90, CK180, CK270; +input DO_LPTX0_N, DO_LPTX1_N, DO_LPTX2_N, DO_LPTX3_N, DO_LPTXCK_N, DO_LPTX0_P, DO_LPTX1_P, DO_LPTX2_P, DO_LPTX3_P, DO_LPTXCK_P; +input HSRX_EN_CK, HSRX_EN_D0, HSRX_EN_D1, HSRX_EN_D2, HSRX_EN_D3, HSRX_ODTEN_CK; +input RX_DRST_N, TX_DRST_N, WALIGN_DVLD; +output [7:0] MRDATA; +input MA_INC, MCLK; +input [1:0] MOPCODE; +input [7:0] MWDATA; +output ALPEDO_LANE0, ALPEDO_LANE1, ALPEDO_LANE2, ALPEDO_LANE3, ALPEDO_LANECK; +output D1LN_DESKEW_DONE,D2LN_DESKEW_DONE,D3LN_DESKEW_DONE,D0LN_DESKEW_DONE; +output D1LN_DESKEW_ERROR, D2LN_DESKEW_ERROR, D3LN_DESKEW_ERROR, D0LN_DESKEW_ERROR; +input D0LN_DESKEW_REQ, D1LN_DESKEW_REQ, D2LN_DESKEW_REQ, D3LN_DESKEW_REQ; +input HSRX_DLYDIR_LANE0, HSRX_DLYDIR_LANE1, HSRX_DLYDIR_LANE2, HSRX_DLYDIR_LANE3, HSRX_DLYDIR_LANECK; +input HSRX_DLYLDN_LANE0, HSRX_DLYLDN_LANE1, HSRX_DLYLDN_LANE2, HSRX_DLYLDN_LANE3, HSRX_DLYLDN_LANECK; +input HSRX_DLYMV_LANE0, HSRX_DLYMV_LANE1, HSRX_DLYMV_LANE2, HSRX_DLYMV_LANE3, HSRX_DLYMV_LANECK; +input ALP_EDEN_LANE0, ALP_EDEN_LANE1, ALP_EDEN_LANE2, ALP_EDEN_LANE3, ALP_EDEN_LANECK, ALPEN_LN0, ALPEN_LN1, ALPEN_LN2, ALPEN_LN3, ALPEN_LNCK; +parameter TX_PLLCLK = "NONE"; +parameter RX_ALIGN_BYTE = 8'b10111000 ; +parameter RX_HS_8BIT_MODE = 1'b0 ; +parameter RX_LANE_ALIGN_EN = 1'b0 ; +parameter TX_HS_8BIT_MODE = 1'b0 ; +parameter HSREG_EN_LN0 = 1'b0; +parameter HSREG_EN_LN1 = 1'b0; +parameter HSREG_EN_LN2 = 1'b0; +parameter HSREG_EN_LN3 = 1'b0; +parameter HSREG_EN_LNCK = 1'b0; +parameter LANE_DIV_SEL = 2'b00; +parameter HSRX_EN = 1'b1 ; +parameter HSRX_LANESEL = 4'b1111 ; +parameter HSRX_LANESEL_CK = 1'b1 ; +parameter HSTX_EN_LN0 = 1'b0 ; +parameter HSTX_EN_LN1 = 1'b0 ; +parameter HSTX_EN_LN2 = 1'b0 ; +parameter HSTX_EN_LN3 = 1'b0 ; +parameter HSTX_EN_LNCK = 1'b0 ; +parameter LPTX_EN_LN0 = 1'b1 ; +parameter LPTX_EN_LN1 = 1'b1 ; +parameter LPTX_EN_LN2 = 1'b1 ; +parameter LPTX_EN_LN3 = 1'b1 ; +parameter LPTX_EN_LNCK = 1'b1 ; +parameter TXDP_EN_LN0 = 1'b0 ; +parameter TXDP_EN_LN1 = 1'b0 ; +parameter TXDP_EN_LN2 = 1'b0 ; +parameter TXDP_EN_LN3 = 1'b0 ; +parameter TXDP_EN_LNCK = 1'b0 ; +parameter CKLN_DELAY_EN = 1'b0; +parameter CKLN_DELAY_OVR_VAL = 7'b0000000; +parameter D0LN_DELAY_EN = 1'b0; +parameter D0LN_DELAY_OVR_VAL = 7'b0000000; +parameter D0LN_DESKEW_BYPASS = 1'b0; +parameter D1LN_DELAY_EN = 1'b0; +parameter D1LN_DELAY_OVR_VAL = 7'b0000000; +parameter D1LN_DESKEW_BYPASS = 1'b0; +parameter D2LN_DELAY_EN = 1'b0; +parameter D2LN_DELAY_OVR_VAL = 7'b0000000; +parameter D2LN_DESKEW_BYPASS = 1'b0; +parameter D3LN_DELAY_EN = 1'b0; +parameter D3LN_DELAY_OVR_VAL = 7'b0000000; +parameter D3LN_DESKEW_BYPASS = 1'b0; +parameter DESKEW_EN_LOW_DELAY = 1'b0; +parameter DESKEW_EN_ONE_EDGE = 1'b0; +parameter DESKEW_FAST_LOOP_TIME = 4'b0000; +parameter DESKEW_FAST_MODE = 1'b0; +parameter DESKEW_HALF_OPENING = 6'b010110; +parameter DESKEW_LSB_MODE = 2'b00; +parameter DESKEW_M = 3'b011; +parameter DESKEW_M_TH = 13'b0000110100110; +parameter DESKEW_MAX_SETTING = 7'b0100001; +parameter DESKEW_ONE_CLK_EDGE_EN = 1'b0 ; +parameter DESKEW_RST_BYPASS = 1'b0 ; +parameter RX_BYTE_LITTLE_ENDIAN = 1'b1 ; +parameter RX_CLK_1X_SYNC_SEL = 1'b0 ; +parameter RX_INVERT = 1'b0 ; +parameter RX_ONE_BYTE0_MATCH = 1'b0 ; +parameter RX_RD_START_DEPTH = 5'b00001; +parameter RX_SYNC_MODE = 1'b0 ; +parameter RX_WORD_ALIGN_BYPASS = 1'b0 ; +parameter RX_WORD_ALIGN_DATA_VLD_SRC_SEL = 1'b0 ; +parameter RX_WORD_LITTLE_ENDIAN = 1'b1 ; +parameter TX_BYPASS_MODE = 1'b0 ; +parameter TX_BYTECLK_SYNC_MODE = 1'b0 ; +parameter TX_OCLK_USE_CIBCLK = 1'b0 ; +parameter TX_RD_START_DEPTH = 5'b00001; +parameter TX_SYNC_MODE = 1'b0 ; +parameter TX_WORD_LITTLE_ENDIAN = 1'b1 ; +parameter EQ_CS_LANE0 = 3'b100; +parameter EQ_CS_LANE1 = 3'b100; +parameter EQ_CS_LANE2 = 3'b100; +parameter EQ_CS_LANE3 = 3'b100; +parameter EQ_CS_LANECK = 3'b100; +parameter EQ_RS_LANE0 = 3'b100; +parameter EQ_RS_LANE1 = 3'b100; +parameter EQ_RS_LANE2 = 3'b100; +parameter EQ_RS_LANE3 = 3'b100; +parameter EQ_RS_LANECK = 3'b100; +parameter HSCLK_LANE_LN0 = 1'b0; +parameter HSCLK_LANE_LN1 = 1'b0; +parameter HSCLK_LANE_LN2 = 1'b0; +parameter HSCLK_LANE_LN3 = 1'b0; +parameter HSCLK_LANE_LNCK = 1'b1; +parameter ALP_ED_EN_LANE0 = 1'b1 ; +parameter ALP_ED_EN_LANE1 = 1'b1 ; +parameter ALP_ED_EN_LANE2 = 1'b1 ; +parameter ALP_ED_EN_LANE3 = 1'b1 ; +parameter ALP_ED_EN_LANECK = 1'b1 ; +parameter ALP_ED_TST_LANE0 = 1'b0 ; +parameter ALP_ED_TST_LANE1 = 1'b0 ; +parameter ALP_ED_TST_LANE2 = 1'b0 ; +parameter ALP_ED_TST_LANE3 = 1'b0 ; +parameter ALP_ED_TST_LANECK = 1'b0 ; +parameter ALP_EN_LN0 = 1'b0 ; +parameter ALP_EN_LN1 = 1'b0 ; +parameter ALP_EN_LN2 = 1'b0 ; +parameter ALP_EN_LN3 = 1'b0 ; +parameter ALP_EN_LNCK = 1'b0 ; +parameter ALP_HYS_EN_LANE0 = 1'b1 ; +parameter ALP_HYS_EN_LANE1 = 1'b1 ; +parameter ALP_HYS_EN_LANE2 = 1'b1 ; +parameter ALP_HYS_EN_LANE3 = 1'b1 ; +parameter ALP_HYS_EN_LANECK = 1'b1 ; +parameter ALP_TH_LANE0 = 4'b1000 ; +parameter ALP_TH_LANE1 = 4'b1000 ; +parameter ALP_TH_LANE2 = 4'b1000 ; +parameter ALP_TH_LANE3 = 4'b1000 ; +parameter ALP_TH_LANECK = 4'b1000 ; +parameter ANA_BYTECLK_PH = 2'b00 ; +parameter BIT_REVERSE_LN0 = 1'b0 ; +parameter BIT_REVERSE_LN1 = 1'b0 ; +parameter BIT_REVERSE_LN2 = 1'b0 ; +parameter BIT_REVERSE_LN3 = 1'b0 ; +parameter BIT_REVERSE_LNCK = 1'b0 ; +parameter BYPASS_TXHCLKEN = 1'b1 ; +parameter BYPASS_TXHCLKEN_SYNC = 1'b0 ; +parameter BYTE_CLK_POLAR = 1'b0 ; +parameter BYTE_REVERSE_LN0 = 1'b0 ; +parameter BYTE_REVERSE_LN1 = 1'b0 ; +parameter BYTE_REVERSE_LN2 = 1'b0 ; +parameter BYTE_REVERSE_LN3 = 1'b0 ; +parameter BYTE_REVERSE_LNCK = 1'b0 ; +parameter EN_CLKB1X = 1'b1 ; +parameter EQ_PBIAS_LANE0 = 4'b1000 ; +parameter EQ_PBIAS_LANE1 = 4'b1000 ; +parameter EQ_PBIAS_LANE2 = 4'b1000 ; +parameter EQ_PBIAS_LANE3 = 4'b1000 ; +parameter EQ_PBIAS_LANECK = 4'b1000 ; +parameter EQ_ZLD_LANE0 = 4'b1000 ; +parameter EQ_ZLD_LANE1 = 4'b1000 ; +parameter EQ_ZLD_LANE2 = 4'b1000 ; +parameter EQ_ZLD_LANE3 = 4'b1000 ; +parameter EQ_ZLD_LANECK = 4'b1000 ; +parameter HIGH_BW_LANE0 = 1'b1 ; +parameter HIGH_BW_LANE1 = 1'b1 ; +parameter HIGH_BW_LANE2 = 1'b1 ; +parameter HIGH_BW_LANE3 = 1'b1 ; +parameter HIGH_BW_LANECK = 1'b1 ; +parameter HSREG_VREF_CTL = 3'b100 ; +parameter HSREG_VREF_EN = 1'b1 ; +parameter HSRX_DLY_CTL_CK = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE0 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE1 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE2 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE3 = 7'b0000000 ; +parameter HSRX_DLY_SEL_LANE0 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE1 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE2 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE3 = 1'b0 ; +parameter HSRX_DLY_SEL_LANECK = 1'b0 ; +parameter HSRX_DUTY_LANE0 = 4'b1000 ; +parameter HSRX_DUTY_LANE1 = 4'b1000 ; +parameter HSRX_DUTY_LANE2 = 4'b1000 ; +parameter HSRX_DUTY_LANE3 = 4'b1000 ; +parameter HSRX_DUTY_LANECK = 4'b1000 ; +parameter HSRX_EQ_EN_LANE0 = 1'b1 ; +parameter HSRX_EQ_EN_LANE1 = 1'b1 ; +parameter HSRX_EQ_EN_LANE2 = 1'b1 ; +parameter HSRX_EQ_EN_LANE3 = 1'b1 ; +parameter HSRX_EQ_EN_LANECK = 1'b1 ; +parameter HSRX_IBIAS = 4'b0011 ; +parameter HSRX_IBIAS_TEST_EN = 1'b0 ; +parameter HSRX_IMARG_EN = 1'b0 ; +parameter HSRX_ODT_EN = 1'b1 ; +parameter HSRX_ODT_TST = 4'b0000 ; +parameter HSRX_ODT_TST_CK = 1'b0 ; +parameter HSRX_SEL = 4'b0000 ; +parameter HSRX_STOP_EN = 1'b0 ; +parameter HSRX_TST = 4'b0000 ; +parameter HSRX_TST_CK = 1'b0 ; +parameter HSRX_WAIT4EDGE = 1'b1 ; +parameter HYST_NCTL = 2'b01 ; +parameter HYST_PCTL = 2'b01 ; +parameter IBIAS_TEST_EN = 1'b0 ; +parameter LB_CH_SEL = 1'b0 ; +parameter LB_EN_LN0 = 1'b0 ; +parameter LB_EN_LN1 = 1'b0 ; +parameter LB_EN_LN2 = 1'b0 ; +parameter LB_EN_LN3 = 1'b0 ; +parameter LB_EN_LNCK = 1'b0 ; +parameter LB_POLAR_LN0 = 1'b0 ; +parameter LB_POLAR_LN1 = 1'b0 ; +parameter LB_POLAR_LN2 = 1'b0 ; +parameter LB_POLAR_LN3 = 1'b0 ; +parameter LB_POLAR_LNCK = 1'b0 ; +parameter LOW_LPRX_VTH = 1'b0 ; +parameter LPBK_DATA2TO1 = 4'b0000; +parameter LPBK_DATA2TO1_CK = 1'b0 ; +parameter LPBK_EN = 1'b0 ; +parameter LPBK_SEL = 4'b0000; +parameter LPBKTST_EN = 4'b0000; +parameter LPBKTST_EN_CK = 1'b0 ; +parameter LPRX_EN = 1'b1 ; +parameter LPRX_TST = 4'b0000; +parameter LPRX_TST_CK = 1'b0 ; +parameter LPTX_DAT_POLAR_LN0 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN1 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN2 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN3 = 1'b0 ; +parameter LPTX_DAT_POLAR_LNCK = 1'b0 ; +parameter LPTX_NIMP_LN0 = 3'b100 ; +parameter LPTX_NIMP_LN1 = 3'b100 ; +parameter LPTX_NIMP_LN2 = 3'b100 ; +parameter LPTX_NIMP_LN3 = 3'b100 ; +parameter LPTX_NIMP_LNCK = 3'b100 ; +parameter LPTX_PIMP_LN0 = 3'b100 ; +parameter LPTX_PIMP_LN1 = 3'b100 ; +parameter LPTX_PIMP_LN2 = 3'b100 ; +parameter LPTX_PIMP_LN3 = 3'b100 ; +parameter LPTX_PIMP_LNCK = 3'b100 ; +parameter MIPI_PMA_DIS_N = 1'b1 ; +parameter PGA_BIAS_LANE0 = 4'b1000 ; +parameter PGA_BIAS_LANE1 = 4'b1000 ; +parameter PGA_BIAS_LANE2 = 4'b1000 ; +parameter PGA_BIAS_LANE3 = 4'b1000 ; +parameter PGA_BIAS_LANECK = 4'b1000 ; +parameter PGA_GAIN_LANE0 = 4'b1000 ; +parameter PGA_GAIN_LANE1 = 4'b1000 ; +parameter PGA_GAIN_LANE2 = 4'b1000 ; +parameter PGA_GAIN_LANE3 = 4'b1000 ; +parameter PGA_GAIN_LANECK = 4'b1000 ; +parameter RX_ODT_TRIM_LANE0 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE1 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE2 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE3 = 4'b1000 ; +parameter RX_ODT_TRIM_LANECK = 4'b1000 ; +parameter SLEWN_CTL_LN0 = 4'b1111 ; +parameter SLEWN_CTL_LN1 = 4'b1111 ; +parameter SLEWN_CTL_LN2 = 4'b1111 ; +parameter SLEWN_CTL_LN3 = 4'b1111 ; +parameter SLEWN_CTL_LNCK = 4'b1111 ; +parameter SLEWP_CTL_LN0 = 4'b1111 ; +parameter SLEWP_CTL_LN1 = 4'b1111 ; +parameter SLEWP_CTL_LN2 = 4'b1111 ; +parameter SLEWP_CTL_LN3 = 4'b1111 ; +parameter SLEWP_CTL_LNCK = 4'b1111 ; +parameter STP_UNIT = 2'b01 ; +parameter TERMN_CTL_LN0 = 4'b1000 ; +parameter TERMN_CTL_LN1 = 4'b1000 ; +parameter TERMN_CTL_LN2 = 4'b1000 ; +parameter TERMN_CTL_LN3 = 4'b1000 ; +parameter TERMN_CTL_LNCK = 4'b1000 ; +parameter TERMP_CTL_LN0 = 4'b1000 ; +parameter TERMP_CTL_LN1 = 4'b1000 ; +parameter TERMP_CTL_LN2 = 4'b1000 ; +parameter TERMP_CTL_LN3 = 4'b1000 ; +parameter TERMP_CTL_LNCK = 4'b1000 ; +parameter TEST_EN_LN0 = 1'b0 ; +parameter TEST_EN_LN1 = 1'b0 ; +parameter TEST_EN_LN2 = 1'b0 ; +parameter TEST_EN_LN3 = 1'b0 ; +parameter TEST_EN_LNCK = 1'b0 ; +parameter TEST_N_IMP_LN0 = 1'b0 ; +parameter TEST_N_IMP_LN1 = 1'b0 ; +parameter TEST_N_IMP_LN2 = 1'b0 ; +parameter TEST_N_IMP_LN3 = 1'b0 ; +parameter TEST_N_IMP_LNCK = 1'b0 ; +parameter TEST_P_IMP_LN0 = 1'b0 ; +parameter TEST_P_IMP_LN1 = 1'b0 ; +parameter TEST_P_IMP_LN2 = 1'b0 ; +parameter TEST_P_IMP_LN3 = 1'b0 ; +parameter TEST_P_IMP_LNCK = 1'b0 ; +endmodule + +module MIPI_DPHYA (...); +output RX_CLK_O, TX_CLK_O; +output [15:0] D0LN_HSRXD, D1LN_HSRXD, D2LN_HSRXD, D3LN_HSRXD; +output D0LN_HSRXD_VLD,D1LN_HSRXD_VLD,D2LN_HSRXD_VLD,D3LN_HSRXD_VLD; +input D0LN_HSRX_DREN, D1LN_HSRX_DREN, D2LN_HSRX_DREN, D3LN_HSRX_DREN; +output DI_LPRX0_N, DI_LPRX0_P, DI_LPRX1_N, DI_LPRX1_P, DI_LPRX2_N, DI_LPRX2_P, DI_LPRX3_N, DI_LPRX3_P, DI_LPRXCK_N, DI_LPRXCK_P; +inout CK_N, CK_P, D0_N, D0_P, D1_N, D1_P, D2_N, D2_P, D3_N, D3_P; +input HSRX_STOP, HSTXEN_LN0, HSTXEN_LN1, HSTXEN_LN2, HSTXEN_LN3, HSTXEN_LNCK; +input PWRON_RX, PWRON_TX, RESET, RX_CLK_1X, TX_CLK_1X; +input TXDPEN_LN0, TXDPEN_LN1, TXDPEN_LN2, TXDPEN_LN3, TXDPEN_LNCK, TXHCLK_EN; +input [15:0] CKLN_HSTXD,D0LN_HSTXD,D1LN_HSTXD,D2LN_HSTXD,D3LN_HSTXD; +input HSTXD_VLD; +input CK0, CK90, CK180, CK270; +input DO_LPTX0_N, DO_LPTX1_N, DO_LPTX2_N, DO_LPTX3_N, DO_LPTXCK_N, DO_LPTX0_P, DO_LPTX1_P, DO_LPTX2_P, DO_LPTX3_P, DO_LPTXCK_P; +input HSRX_EN_CK, HSRX_EN_D0, HSRX_EN_D1, HSRX_EN_D2, HSRX_EN_D3, HSRX_ODTEN_CK; +input RX_DRST_N, TX_DRST_N, WALIGN_DVLD; +output [7:0] MRDATA; +input MA_INC, MCLK; +input [1:0] MOPCODE; +input [7:0] MWDATA; +input SPLL_CKN, SPLL_CKP; +output ALPEDO_LANE0, ALPEDO_LANE1, ALPEDO_LANE2, ALPEDO_LANE3, ALPEDO_LANECK; +output D1LN_DESKEW_DONE,D2LN_DESKEW_DONE,D3LN_DESKEW_DONE,D0LN_DESKEW_DONE; +output D1LN_DESKEW_ERROR, D2LN_DESKEW_ERROR, D3LN_DESKEW_ERROR, D0LN_DESKEW_ERROR; +input D0LN_DESKEW_REQ, D1LN_DESKEW_REQ, D2LN_DESKEW_REQ, D3LN_DESKEW_REQ; +input HSRX_DLYDIR_LANE0, HSRX_DLYDIR_LANE1, HSRX_DLYDIR_LANE2, HSRX_DLYDIR_LANE3, HSRX_DLYDIR_LANECK; +input HSRX_DLYLDN_LANE0, HSRX_DLYLDN_LANE1, HSRX_DLYLDN_LANE2, HSRX_DLYLDN_LANE3, HSRX_DLYLDN_LANECK; +input HSRX_DLYMV_LANE0, HSRX_DLYMV_LANE1, HSRX_DLYMV_LANE2, HSRX_DLYMV_LANE3, HSRX_DLYMV_LANECK; +input ALP_EDEN_LANE0, ALP_EDEN_LANE1, ALP_EDEN_LANE2, ALP_EDEN_LANE3, ALP_EDEN_LANECK, ALPEN_LN0, ALPEN_LN1, ALPEN_LN2, ALPEN_LN3, ALPEN_LNCK; +parameter TX_PLLCLK = "NONE"; +parameter RX_ALIGN_BYTE = 8'b10111000 ; +parameter RX_HS_8BIT_MODE = 1'b0 ; +parameter RX_LANE_ALIGN_EN = 1'b0 ; +parameter TX_HS_8BIT_MODE = 1'b0 ; +parameter HSREG_EN_LN0 = 1'b0; +parameter HSREG_EN_LN1 = 1'b0; +parameter HSREG_EN_LN2 = 1'b0; +parameter HSREG_EN_LN3 = 1'b0; +parameter HSREG_EN_LNCK = 1'b0; +parameter LANE_DIV_SEL = 2'b00; +parameter HSRX_EN = 1'b1 ; +parameter HSRX_LANESEL = 4'b1111 ; +parameter HSRX_LANESEL_CK = 1'b1 ; +parameter HSTX_EN_LN0 = 1'b0 ; +parameter HSTX_EN_LN1 = 1'b0 ; +parameter HSTX_EN_LN2 = 1'b0 ; +parameter HSTX_EN_LN3 = 1'b0 ; +parameter HSTX_EN_LNCK = 1'b0 ; +parameter LPTX_EN_LN0 = 1'b1 ; +parameter LPTX_EN_LN1 = 1'b1 ; +parameter LPTX_EN_LN2 = 1'b1 ; +parameter LPTX_EN_LN3 = 1'b1 ; +parameter LPTX_EN_LNCK = 1'b1 ; +parameter TXDP_EN_LN0 = 1'b0 ; +parameter TXDP_EN_LN1 = 1'b0 ; +parameter TXDP_EN_LN2 = 1'b0 ; +parameter TXDP_EN_LN3 = 1'b0 ; +parameter TXDP_EN_LNCK = 1'b0 ; +parameter SPLL_DIV_SEL = 2'b00; +parameter DPHY_CK_SEL = 2'b01; +parameter CKLN_DELAY_EN = 1'b0; +parameter CKLN_DELAY_OVR_VAL = 7'b0000000; +parameter D0LN_DELAY_EN = 1'b0; +parameter D0LN_DELAY_OVR_VAL = 7'b0000000; +parameter D0LN_DESKEW_BYPASS = 1'b0; +parameter D1LN_DELAY_EN = 1'b0; +parameter D1LN_DELAY_OVR_VAL = 7'b0000000; +parameter D1LN_DESKEW_BYPASS = 1'b0; +parameter D2LN_DELAY_EN = 1'b0; +parameter D2LN_DELAY_OVR_VAL = 7'b0000000; +parameter D2LN_DESKEW_BYPASS = 1'b0; +parameter D3LN_DELAY_EN = 1'b0; +parameter D3LN_DELAY_OVR_VAL = 7'b0000000; +parameter D3LN_DESKEW_BYPASS = 1'b0; +parameter DESKEW_EN_LOW_DELAY = 1'b0; +parameter DESKEW_EN_ONE_EDGE = 1'b0; +parameter DESKEW_FAST_LOOP_TIME = 4'b0000; +parameter DESKEW_FAST_MODE = 1'b0; +parameter DESKEW_HALF_OPENING = 6'b010110; +parameter DESKEW_LSB_MODE = 2'b00; +parameter DESKEW_M = 3'b011; +parameter DESKEW_M_TH = 13'b0000110100110; +parameter DESKEW_MAX_SETTING = 7'b0100001; +parameter DESKEW_ONE_CLK_EDGE_EN = 1'b0 ; +parameter DESKEW_RST_BYPASS = 1'b0 ; +parameter RX_BYTE_LITTLE_ENDIAN = 1'b1 ; +parameter RX_CLK_1X_SYNC_SEL = 1'b0 ; +parameter RX_INVERT = 1'b0 ; +parameter RX_ONE_BYTE0_MATCH = 1'b0 ; +parameter RX_RD_START_DEPTH = 5'b00001; +parameter RX_SYNC_MODE = 1'b0 ; +parameter RX_WORD_ALIGN_BYPASS = 1'b0 ; +parameter RX_WORD_ALIGN_DATA_VLD_SRC_SEL = 1'b0 ; +parameter RX_WORD_LITTLE_ENDIAN = 1'b1 ; +parameter TX_BYPASS_MODE = 1'b0 ; +parameter TX_BYTECLK_SYNC_MODE = 1'b0 ; +parameter TX_OCLK_USE_CIBCLK = 1'b0 ; +parameter TX_RD_START_DEPTH = 5'b00001; +parameter TX_SYNC_MODE = 1'b0 ; +parameter TX_WORD_LITTLE_ENDIAN = 1'b1 ; +parameter EQ_CS_LANE0 = 3'b100; +parameter EQ_CS_LANE1 = 3'b100; +parameter EQ_CS_LANE2 = 3'b100; +parameter EQ_CS_LANE3 = 3'b100; +parameter EQ_CS_LANECK = 3'b100; +parameter EQ_RS_LANE0 = 3'b100; +parameter EQ_RS_LANE1 = 3'b100; +parameter EQ_RS_LANE2 = 3'b100; +parameter EQ_RS_LANE3 = 3'b100; +parameter EQ_RS_LANECK = 3'b100; +parameter HSCLK_LANE_LN0 = 1'b0; +parameter HSCLK_LANE_LN1 = 1'b0; +parameter HSCLK_LANE_LN2 = 1'b0; +parameter HSCLK_LANE_LN3 = 1'b0; +parameter HSCLK_LANE_LNCK = 1'b1; +parameter ALP_ED_EN_LANE0 = 1'b1 ; +parameter ALP_ED_EN_LANE1 = 1'b1 ; +parameter ALP_ED_EN_LANE2 = 1'b1 ; +parameter ALP_ED_EN_LANE3 = 1'b1 ; +parameter ALP_ED_EN_LANECK = 1'b1 ; +parameter ALP_ED_TST_LANE0 = 1'b0 ; +parameter ALP_ED_TST_LANE1 = 1'b0 ; +parameter ALP_ED_TST_LANE2 = 1'b0 ; +parameter ALP_ED_TST_LANE3 = 1'b0 ; +parameter ALP_ED_TST_LANECK = 1'b0 ; +parameter ALP_EN_LN0 = 1'b0 ; +parameter ALP_EN_LN1 = 1'b0 ; +parameter ALP_EN_LN2 = 1'b0 ; +parameter ALP_EN_LN3 = 1'b0 ; +parameter ALP_EN_LNCK = 1'b0 ; +parameter ALP_HYS_EN_LANE0 = 1'b1 ; +parameter ALP_HYS_EN_LANE1 = 1'b1 ; +parameter ALP_HYS_EN_LANE2 = 1'b1 ; +parameter ALP_HYS_EN_LANE3 = 1'b1 ; +parameter ALP_HYS_EN_LANECK = 1'b1 ; +parameter ALP_TH_LANE0 = 4'b1000 ; +parameter ALP_TH_LANE1 = 4'b1000 ; +parameter ALP_TH_LANE2 = 4'b1000 ; +parameter ALP_TH_LANE3 = 4'b1000 ; +parameter ALP_TH_LANECK = 4'b1000 ; +parameter ANA_BYTECLK_PH = 2'b00 ; +parameter BIT_REVERSE_LN0 = 1'b0 ; +parameter BIT_REVERSE_LN1 = 1'b0 ; +parameter BIT_REVERSE_LN2 = 1'b0 ; +parameter BIT_REVERSE_LN3 = 1'b0 ; +parameter BIT_REVERSE_LNCK = 1'b0 ; +parameter BYPASS_TXHCLKEN = 1'b1 ; +parameter BYPASS_TXHCLKEN_SYNC = 1'b0 ; +parameter BYTE_CLK_POLAR = 1'b0 ; +parameter BYTE_REVERSE_LN0 = 1'b0 ; +parameter BYTE_REVERSE_LN1 = 1'b0 ; +parameter BYTE_REVERSE_LN2 = 1'b0 ; +parameter BYTE_REVERSE_LN3 = 1'b0 ; +parameter BYTE_REVERSE_LNCK = 1'b0 ; +parameter EN_CLKB1X = 1'b1 ; +parameter EQ_PBIAS_LANE0 = 4'b1000 ; +parameter EQ_PBIAS_LANE1 = 4'b1000 ; +parameter EQ_PBIAS_LANE2 = 4'b1000 ; +parameter EQ_PBIAS_LANE3 = 4'b1000 ; +parameter EQ_PBIAS_LANECK = 4'b1000 ; +parameter EQ_ZLD_LANE0 = 4'b1000 ; +parameter EQ_ZLD_LANE1 = 4'b1000 ; +parameter EQ_ZLD_LANE2 = 4'b1000 ; +parameter EQ_ZLD_LANE3 = 4'b1000 ; +parameter EQ_ZLD_LANECK = 4'b1000 ; +parameter HIGH_BW_LANE0 = 1'b1 ; +parameter HIGH_BW_LANE1 = 1'b1 ; +parameter HIGH_BW_LANE2 = 1'b1 ; +parameter HIGH_BW_LANE3 = 1'b1 ; +parameter HIGH_BW_LANECK = 1'b1 ; +parameter HSREG_VREF_CTL = 3'b100 ; +parameter HSREG_VREF_EN = 1'b1 ; +parameter HSRX_DLY_CTL_CK = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE0 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE1 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE2 = 7'b0000000 ; +parameter HSRX_DLY_CTL_LANE3 = 7'b0000000 ; +parameter HSRX_DLY_SEL_LANE0 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE1 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE2 = 1'b0 ; +parameter HSRX_DLY_SEL_LANE3 = 1'b0 ; +parameter HSRX_DLY_SEL_LANECK = 1'b0 ; +parameter HSRX_DUTY_LANE0 = 4'b1000 ; +parameter HSRX_DUTY_LANE1 = 4'b1000 ; +parameter HSRX_DUTY_LANE2 = 4'b1000 ; +parameter HSRX_DUTY_LANE3 = 4'b1000 ; +parameter HSRX_DUTY_LANECK = 4'b1000 ; +parameter HSRX_EQ_EN_LANE0 = 1'b1 ; +parameter HSRX_EQ_EN_LANE1 = 1'b1 ; +parameter HSRX_EQ_EN_LANE2 = 1'b1 ; +parameter HSRX_EQ_EN_LANE3 = 1'b1 ; +parameter HSRX_EQ_EN_LANECK = 1'b1 ; +parameter HSRX_IBIAS = 4'b0011 ; +parameter HSRX_IBIAS_TEST_EN = 1'b0 ; +parameter HSRX_IMARG_EN = 1'b0 ; +parameter HSRX_ODT_EN = 1'b1 ; +parameter HSRX_ODT_TST = 4'b0000 ; +parameter HSRX_ODT_TST_CK = 1'b0 ; +parameter HSRX_SEL = 4'b0000 ; +parameter HSRX_STOP_EN = 1'b0 ; +parameter HSRX_TST = 4'b0000 ; +parameter HSRX_TST_CK = 1'b0 ; +parameter HSRX_WAIT4EDGE = 1'b1 ; +parameter HYST_NCTL = 2'b01 ; +parameter HYST_PCTL = 2'b01 ; +parameter IBIAS_TEST_EN = 1'b0 ; +parameter LB_CH_SEL = 1'b0 ; +parameter LB_EN_LN0 = 1'b0 ; +parameter LB_EN_LN1 = 1'b0 ; +parameter LB_EN_LN2 = 1'b0 ; +parameter LB_EN_LN3 = 1'b0 ; +parameter LB_EN_LNCK = 1'b0 ; +parameter LB_POLAR_LN0 = 1'b0 ; +parameter LB_POLAR_LN1 = 1'b0 ; +parameter LB_POLAR_LN2 = 1'b0 ; +parameter LB_POLAR_LN3 = 1'b0 ; +parameter LB_POLAR_LNCK = 1'b0 ; +parameter LOW_LPRX_VTH = 1'b0 ; +parameter LPBK_DATA2TO1 = 4'b0000; +parameter LPBK_DATA2TO1_CK = 1'b0 ; +parameter LPBK_EN = 1'b0 ; +parameter LPBK_SEL = 4'b0000; +parameter LPBKTST_EN = 4'b0000; +parameter LPBKTST_EN_CK = 1'b0 ; +parameter LPRX_EN = 1'b1 ; +parameter LPRX_TST = 4'b0000; +parameter LPRX_TST_CK = 1'b0 ; +parameter LPTX_DAT_POLAR_LN0 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN1 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN2 = 1'b0 ; +parameter LPTX_DAT_POLAR_LN3 = 1'b0 ; +parameter LPTX_DAT_POLAR_LNCK = 1'b0 ; +parameter LPTX_NIMP_LN0 = 3'b100 ; +parameter LPTX_NIMP_LN1 = 3'b100 ; +parameter LPTX_NIMP_LN2 = 3'b100 ; +parameter LPTX_NIMP_LN3 = 3'b100 ; +parameter LPTX_NIMP_LNCK = 3'b100 ; +parameter LPTX_PIMP_LN0 = 3'b100 ; +parameter LPTX_PIMP_LN1 = 3'b100 ; +parameter LPTX_PIMP_LN2 = 3'b100 ; +parameter LPTX_PIMP_LN3 = 3'b100 ; +parameter LPTX_PIMP_LNCK = 3'b100 ; +parameter MIPI_PMA_DIS_N = 1'b1 ; +parameter PGA_BIAS_LANE0 = 4'b1000 ; +parameter PGA_BIAS_LANE1 = 4'b1000 ; +parameter PGA_BIAS_LANE2 = 4'b1000 ; +parameter PGA_BIAS_LANE3 = 4'b1000 ; +parameter PGA_BIAS_LANECK = 4'b1000 ; +parameter PGA_GAIN_LANE0 = 4'b1000 ; +parameter PGA_GAIN_LANE1 = 4'b1000 ; +parameter PGA_GAIN_LANE2 = 4'b1000 ; +parameter PGA_GAIN_LANE3 = 4'b1000 ; +parameter PGA_GAIN_LANECK = 4'b1000 ; +parameter RX_ODT_TRIM_LANE0 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE1 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE2 = 4'b1000 ; +parameter RX_ODT_TRIM_LANE3 = 4'b1000 ; +parameter RX_ODT_TRIM_LANECK = 4'b1000 ; +parameter SLEWN_CTL_LN0 = 4'b1111 ; +parameter SLEWN_CTL_LN1 = 4'b1111 ; +parameter SLEWN_CTL_LN2 = 4'b1111 ; +parameter SLEWN_CTL_LN3 = 4'b1111 ; +parameter SLEWN_CTL_LNCK = 4'b1111 ; +parameter SLEWP_CTL_LN0 = 4'b1111 ; +parameter SLEWP_CTL_LN1 = 4'b1111 ; +parameter SLEWP_CTL_LN2 = 4'b1111 ; +parameter SLEWP_CTL_LN3 = 4'b1111 ; +parameter SLEWP_CTL_LNCK = 4'b1111 ; +parameter STP_UNIT = 2'b01 ; +parameter TERMN_CTL_LN0 = 4'b1000 ; +parameter TERMN_CTL_LN1 = 4'b1000 ; +parameter TERMN_CTL_LN2 = 4'b1000 ; +parameter TERMN_CTL_LN3 = 4'b1000 ; +parameter TERMN_CTL_LNCK = 4'b1000 ; +parameter TERMP_CTL_LN0 = 4'b1000 ; +parameter TERMP_CTL_LN1 = 4'b1000 ; +parameter TERMP_CTL_LN2 = 4'b1000 ; +parameter TERMP_CTL_LN3 = 4'b1000 ; +parameter TERMP_CTL_LNCK = 4'b1000 ; +parameter TEST_EN_LN0 = 1'b0 ; +parameter TEST_EN_LN1 = 1'b0 ; +parameter TEST_EN_LN2 = 1'b0 ; +parameter TEST_EN_LN3 = 1'b0 ; +parameter TEST_EN_LNCK = 1'b0 ; +parameter TEST_N_IMP_LN0 = 1'b0 ; +parameter TEST_N_IMP_LN1 = 1'b0 ; +parameter TEST_N_IMP_LN2 = 1'b0 ; +parameter TEST_N_IMP_LN3 = 1'b0 ; +parameter TEST_N_IMP_LNCK = 1'b0 ; +parameter TEST_P_IMP_LN0 = 1'b0 ; +parameter TEST_P_IMP_LN1 = 1'b0 ; +parameter TEST_P_IMP_LN2 = 1'b0 ; +parameter TEST_P_IMP_LN3 = 1'b0 ; +parameter TEST_P_IMP_LNCK = 1'b0 ; +endmodule + +module MIPI_CPHY (...); +output [41:0] D0LN_HSRXD, D1LN_HSRXD, D2LN_HSRXD; +output D0LN_HSRXD_VLD, D1LN_HSRXD_VLD, D2LN_HSRXD_VLD; +output [1:0] D0LN_HSRX_DEMAP_INVLD, D1LN_HSRX_DEMAP_INVLD, D2LN_HSRX_DEMAP_INVLD; +output D0LN_HSRX_FIFO_RDE_ERR, D0LN_HSRX_FIFO_WRF_ERR, D1LN_HSRX_FIFO_RDE_ERR, D1LN_HSRX_FIFO_WRF_ERR, D2LN_HSRX_FIFO_RDE_ERR, D2LN_HSRX_FIFO_WRF_ERR; +output [1:0] D0LN_HSRX_WA, D1LN_HSRX_WA, D2LN_HSRX_WA; +output D0LN_RX_CLK_1X_O, D1LN_RX_CLK_1X_O, D2LN_RX_CLK_1X_O; +output HSTX_FIFO_AE, HSTX_FIFO_AF; +output HSTX_FIFO_RDE_ERR, HSTX_FIFO_WRF_ERR; +output RX_CLK_MUXED; +output TX_CLK_1X_O; +output DI_LPRX0_A, DI_LPRX0_B, DI_LPRX0_C, DI_LPRX1_A, DI_LPRX1_B, DI_LPRX1_C, DI_LPRX2_A, DI_LPRX2_B, DI_LPRX2_C; +output [7:0] MDRP_RDATA; +inout D0A, D0B, D0C, D1A, D1B, D1C, D2A, D2B, D2C; +input D0LN_HSRX_EN, D0LN_HSTX_EN, D1LN_HSRX_EN, D1LN_HSTX_EN, D2LN_HSRX_EN, D2LN_HSTX_EN; +input [41:0] D0LN_HSTX_DATA,D1LN_HSTX_DATA, D2LN_HSTX_DATA; +input D0LN_HSTX_DATA_VLD, D1LN_HSTX_DATA_VLD, D2LN_HSTX_DATA_VLD; +input [1:0] D0LN_HSTX_MAP_DIS, D1LN_HSTX_MAP_DIS, D2LN_HSTX_MAP_DIS; +input D0LN_RX_CLK_1X_I,D1LN_RX_CLK_1X_I, D2LN_RX_CLK_1X_I; +input D0LN_RX_DRST_N, D0LN_TX_DRST_N, D1LN_RX_DRST_N, D1LN_TX_DRST_N, D2LN_RX_DRST_N, D2LN_TX_DRST_N; +input HSTX_ENLN0, HSTX_ENLN1, HSTX_ENLN2, LPTX_ENLN0, LPTX_ENLN1, LPTX_ENLN2; +input [7:0] MDRP_A_D_I; +input MDRP_A_INC_I; +input MDRP_CLK_I; +input [1:0] MDRP_OPCODE_I; +input PWRON_RX_LN0, PWRON_RX_LN1, PWRON_RX_LN2, PWRON_TX; +input ARST_RXLN0, ARST_RXLN1, ARST_RXLN2; +input ARSTN_TX; +input RX_CLK_EN_LN0, RX_CLK_EN_LN1, RX_CLK_EN_LN2; +input TX_CLK_1X_I; +input TXDP_ENLN0, TXDP_ENLN1, TXDP_ENLN2; +input TXHCLK_EN; +input DO_LPTX_A_LN0, DO_LPTX_A_LN1, DO_LPTX_A_LN2, DO_LPTX_B_LN0, DO_LPTX_B_LN1, DO_LPTX_B_LN2, DO_LPTX_C_LN0, DO_LPTX_C_LN1, DO_LPTX_C_LN2; +input GPLL_CK0,GPLL_CK90, GPLL_CK180, GPLL_CK270; +input HSRX_EN_D0, HSRX_EN_D1, HSRX_EN_D2; +input HSRX_ODT_EN_D0, HSRX_ODT_EN_D1, HSRX_ODT_EN_D2; +input LPRX_EN_D0, LPRX_EN_D1, LPRX_EN_D2; +input SPLL0_CKN, SPLL0_CKP, SPLL1_CKN, SPLL1_CKP; +parameter TX_PLLCLK = "NONE"; +parameter D0LN_HS_TX_EN = 1'b1; +parameter D1LN_HS_TX_EN = 1'b1; +parameter D2LN_HS_TX_EN = 1'b1; +parameter D0LN_HS_RX_EN = 1'b1; +parameter D1LN_HS_RX_EN = 1'b1; +parameter D2LN_HS_RX_EN = 1'b1; +parameter TX_HS_21BIT_MODE = 1'b0; +parameter RX_OUTCLK_SEL = 2'b00; +parameter TX_W_LENDIAN = 1'b1; +parameter CLK_SEL = 2'b00; +parameter LNDIV_RATIO = 4'b0000; +parameter LNDIV_EN = 1'b0; +parameter D0LN_TX_REASGN_A = 2'b00; +parameter D0LN_TX_REASGN_B = 2'b01; +parameter D0LN_TX_REASGN_C = 2'b10; +parameter D0LN_RX_HS_21BIT_MODE = 1'b0; +parameter D0LN_RX_WA_SYNC_PAT0_EN = 1'b1; +parameter D0LN_RX_WA_SYNC_PAT0_H = 7'b1001001; +parameter D0LN_RX_WA_SYNC_PAT0_L = 8'b00100100; +parameter D0LN_RX_WA_SYNC_PAT1_EN = 1'b1; +parameter D0LN_RX_WA_SYNC_PAT1_H = 7'b0101001; +parameter D0LN_RX_WA_SYNC_PAT1_L = 8'b00100100; +parameter D0LN_RX_WA_SYNC_PAT2_EN = 1'b1; +parameter D0LN_RX_WA_SYNC_PAT2_H = 7'b0011001; +parameter D0LN_RX_WA_SYNC_PAT2_L = 8'b00100100; +parameter D0LN_RX_WA_SYNC_PAT3_EN = 1'b0; +parameter D0LN_RX_WA_SYNC_PAT3_H = 7'b0001001; +parameter D0LN_RX_WA_SYNC_PAT3_L = 8'b00100100; +parameter D0LN_RX_W_LENDIAN = 1'b1; +parameter D0LN_RX_REASGN_A = 2'b00; +parameter D0LN_RX_REASGN_B = 2'b01; +parameter D0LN_RX_REASGN_C = 2'b10; +parameter HSRX_LNSEL = 3'b111; +parameter EQ_RS_LN0 = 3'b001; +parameter EQ_CS_LN0 = 3'b101; +parameter PGA_GAIN_LN0 = 4'b0110; +parameter PGA_BIAS_LN0 = 4'b1000; +parameter EQ_PBIAS_LN0 = 4'b0100; +parameter EQ_ZLD_LN0 = 4'b1000; +parameter D1LN_TX_REASGN_A = 2'b00; +parameter D1LN_TX_REASGN_B = 2'b01; +parameter D1LN_TX_REASGN_C = 2'b10; +parameter D1LN_RX_HS_21BIT_MODE = 1'b0; +parameter D1LN_RX_WA_SYNC_PAT0_EN = 1'b1; +parameter D1LN_RX_WA_SYNC_PAT0_H = 7'b1001001; +parameter D1LN_RX_WA_SYNC_PAT0_L = 8'b00100100; +parameter D1LN_RX_WA_SYNC_PAT1_EN = 1'b1; +parameter D1LN_RX_WA_SYNC_PAT1_H = 7'b0101001; +parameter D1LN_RX_WA_SYNC_PAT1_L = 8'b00100100; +parameter D1LN_RX_WA_SYNC_PAT2_EN = 1'b1; +parameter D1LN_RX_WA_SYNC_PAT2_H = 7'b0011001; +parameter D1LN_RX_WA_SYNC_PAT2_L = 8'b00100100; +parameter D1LN_RX_WA_SYNC_PAT3_EN = 1'b0; +parameter D1LN_RX_WA_SYNC_PAT3_H = 7'b0001001; +parameter D1LN_RX_WA_SYNC_PAT3_L = 8'b00100100; +parameter D1LN_RX_W_LENDIAN = 1'b1; +parameter D1LN_RX_REASGN_A = 2'b00; +parameter D1LN_RX_REASGN_B = 2'b01; +parameter D1LN_RX_REASGN_C = 2'b10; +parameter EQ_RS_LN1 = 3'b001; +parameter EQ_CS_LN1 = 3'b101; +parameter PGA_GAIN_LN1 = 4'b0110; +parameter PGA_BIAS_LN1 = 4'b1000; +parameter EQ_PBIAS_LN1 = 4'b0100; +parameter EQ_ZLD_LN1 = 4'b1000; +parameter D2LN_TX_REASGN_A = 2'b00; +parameter D2LN_TX_REASGN_B = 2'b01; +parameter D2LN_TX_REASGN_C = 2'b10; +parameter D2LN_RX_HS_21BIT_MODE = 1'b0; +parameter D2LN_RX_WA_SYNC_PAT0_EN = 1'b1; +parameter D2LN_RX_WA_SYNC_PAT0_H = 7'b1001001; +parameter D2LN_RX_WA_SYNC_PAT0_L = 8'b00100100; +parameter D2LN_RX_WA_SYNC_PAT1_EN = 1'b1; +parameter D2LN_RX_WA_SYNC_PAT1_H = 7'b0101001; +parameter D2LN_RX_WA_SYNC_PAT1_L = 8'b00100100; +parameter D2LN_RX_WA_SYNC_PAT2_EN = 1'b1; +parameter D2LN_RX_WA_SYNC_PAT2_H = 7'b0011001; +parameter D2LN_RX_WA_SYNC_PAT2_L = 8'b00100100; +parameter D2LN_RX_WA_SYNC_PAT3_EN = 1'b0; +parameter D2LN_RX_WA_SYNC_PAT3_H = 7'b0001001; +parameter D2LN_RX_WA_SYNC_PAT3_L = 8'b00100100; +parameter D2LN_RX_W_LENDIAN = 1'b1; +parameter D2LN_RX_REASGN_A = 2'b00; +parameter D2LN_RX_REASGN_B = 2'b01; +parameter D2LN_RX_REASGN_C = 2'b10; +parameter EQ_RS_LN2 = 3'b001; +parameter EQ_CS_LN2 = 3'b101; +parameter PGA_GAIN_LN2 = 4'b0110; +parameter PGA_BIAS_LN2 = 4'b1000; +parameter EQ_PBIAS_LN2 = 4'b0100; +parameter EQ_ZLD_LN2 = 4'b1000; +endmodule + +module GTR12_QUAD (...); +endmodule + +module GTR12_UPAR (...); +endmodule + +module GTR12_PMAC (...); +endmodule + +module GTR12_QUADA (...); +endmodule + +module GTR12_UPARA (...); +endmodule + +module GTR12_PMACA (...); +endmodule + +module GTR12_QUADB (...); +endmodule + +module DQS (...); +input DQSIN,PCLK,FCLK,RESET; +input [3:0] READ; +input [2:0] RCLKSEL; +input [7:0] DLLSTEP; +input [7:0] WSTEP; +input RLOADN, RMOVE, RDIR, WLOADN, WMOVE, WDIR, HOLD; +output DQSR90, DQSW0, DQSW270; +output [2:0] RPOINT, WPOINT; +output RVALID,RBURST, RFLAG, WFLAG; +parameter FIFO_MODE_SEL = 1'b0; +parameter RD_PNTR = 3'b000; +parameter DQS_MODE = "X1"; +parameter HWL = "false"; +endmodule diff --git a/techlibs/gowin/synth_gowin.cc b/techlibs/gowin/synth_gowin.cc index 48b7563b1ec..484b88b943c 100644 --- a/techlibs/gowin/synth_gowin.cc +++ b/techlibs/gowin/synth_gowin.cc @@ -86,17 +86,22 @@ struct SynthGowinPass : public ScriptPass log(" read/write collision\" (same result as setting the no_rw_check\n"); log(" attribute on all memories).\n"); log("\n"); + log(" -family \n"); + log(" sets the gowin family to the specified value. The default is 'gw1n'.\n"); + log(" The following families are supported:\n"); + log(" 'gw1n', 'gw2a', 'gw5a'.\n"); log("\n"); log("The following commands are executed by this synthesis command:\n"); help_script(); log("\n"); } - string top_opt, vout_file, json_file; + string top_opt, vout_file, json_file, family; bool retime, nobram, nolutram, flatten, nodffe, nowidelut, abc9, noiopads, noalu, no_rw_check; void clear_flags() override { + family = "gw1n"; top_opt = "-auto-top"; vout_file = ""; json_file = ""; @@ -132,6 +137,10 @@ struct SynthGowinPass : public ScriptPass json_file = args[++argidx]; continue; } + if (args[argidx] == "-family" && argidx+1 < args.size()) { + family = args[++argidx]; + continue; + } if (args[argidx] == "-run" && argidx+1 < args.size()) { size_t pos = args[argidx+1].find(':'); if (pos == std::string::npos) @@ -210,7 +219,7 @@ struct SynthGowinPass : public ScriptPass if (check_label("begin")) { run("read_verilog -specify -lib +/gowin/cells_sim.v"); - run("read_verilog -specify -lib +/gowin/cells_xtra.v"); + run(stringf("read_verilog -specify -lib +/gowin/cells_xtra_%s.v", help_mode ? "" : family.c_str())); run(stringf("hierarchy -check %s", help_mode ? "-top " : top_opt.c_str())); } diff --git a/techlibs/quicklogic/ql_dsp_simd.cc b/techlibs/quicklogic/ql_dsp_simd.cc index 47b343dadc4..b42823d8b10 100644 --- a/techlibs/quicklogic/ql_dsp_simd.cc +++ b/techlibs/quicklogic/ql_dsp_simd.cc @@ -60,7 +60,7 @@ struct QlDspSimdPass : public Pass { // .......................................... - const size_t m_ModeBitsSize = 80; + const int m_ModeBitsSize = 80; // DSP parameters const std::vector m_DspParams = {"COEFF_3", "COEFF_2", "COEFF_1", "COEFF_0"}; diff --git a/tests/arch/common/blockrom.v b/tests/arch/common/blockrom.v index 93f5c9ddffa..eda57a3ea85 100644 --- a/tests/arch/common/blockrom.v +++ b/tests/arch/common/blockrom.v @@ -10,8 +10,11 @@ module sync_rom #(parameter DATA_WIDTH=8, ADDRESS_WIDTH=10) reg [WORD:0] data_out_r; reg [WORD:0] memory [0:DEPTH]; - integer i,j = 64'hF4B1CA8127865242; - initial + integer i,j; + // Initialize in initial block as a workaround for + // https://github.com/YosysHQ/yosys/issues/4792 + initial begin + j = 64'hF4B1CA8127865242; for (i = 0; i <= DEPTH; i++) begin // In case this ROM will be implemented in fabric: fill the memory with some data // uncorrelated with the address, or Yosys might see through the ruse and e.g. not @@ -21,6 +24,7 @@ module sync_rom #(parameter DATA_WIDTH=8, ADDRESS_WIDTH=10) j = j ^ (j << 25); j = j ^ (j >> 27); end + end always @(posedge clk) begin data_out_r <= memory[address_in]; diff --git a/tests/arch/ecp5/bug1630.ys b/tests/arch/ecp5/bug1630.ys index 63df1ad5b61..651e7c635ad 100644 --- a/tests/arch/ecp5/bug1630.ys +++ b/tests/arch/ecp5/bug1630.ys @@ -1,2 +1,2 @@ -read_ilang bug1630.il.gz +read_rtlil bug1630.il.gz abc9 -lut 4 diff --git a/tests/arch/ecp5/opt_lut_ins.ys b/tests/arch/ecp5/opt_lut_ins.ys index 2bc5469120d..622b5406c1b 100644 --- a/tests/arch/ecp5/opt_lut_ins.ys +++ b/tests/arch/ecp5/opt_lut_ins.ys @@ -1,4 +1,4 @@ -read_ilang << EOF +read_rtlil << EOF module \top diff --git a/tests/arch/ice40/bug1626.ys b/tests/arch/ice40/bug1626.ys index 27b6fb5e850..16f8283a03a 100644 --- a/tests/arch/ice40/bug1626.ys +++ b/tests/arch/ice40/bug1626.ys @@ -1,4 +1,4 @@ -read_ilang <&2 "Error: No file types selected" exit 1 fi @@ -72,6 +84,11 @@ generate_tests() { generate_ys_test "$x" "$yosys_args" done fi; + if [[ $do_tcl = true ]]; then + for x in *.tcl; do + generate_tcl_test "$x" "$yosys_args" + done + fi; if [[ $do_sv = true ]]; then for x in *.sv; do if [ ! -f "${x%.sv}.ys" ]; then diff --git a/tests/liberty/.gitignore b/tests/liberty/.gitignore index 8763aaac515..2ee56e9d156 100644 --- a/tests/liberty/.gitignore +++ b/tests/liberty/.gitignore @@ -1,3 +1,3 @@ *.log -*.filtered +/*.filtered *.verilogsim diff --git a/tests/liberty/foundry_data/.gitignore b/tests/liberty/foundry_data/.gitignore new file mode 100644 index 00000000000..3b97f2a3e74 --- /dev/null +++ b/tests/liberty/foundry_data/.gitignore @@ -0,0 +1,2 @@ +*.lib +*.lib.filtered diff --git a/tests/liberty/foundry_data/RM_IHPSG13_1P_64x64_c2_bm_bist_typ_1p20V_25C.lib.filtered.gz b/tests/liberty/foundry_data/RM_IHPSG13_1P_64x64_c2_bm_bist_typ_1p20V_25C.lib.filtered.gz new file mode 100644 index 00000000000..826d77060a5 Binary files /dev/null and b/tests/liberty/foundry_data/RM_IHPSG13_1P_64x64_c2_bm_bist_typ_1p20V_25C.lib.filtered.gz differ diff --git a/tests/liberty/foundry_data/rules.txt b/tests/liberty/foundry_data/rules.txt new file mode 100644 index 00000000000..1e739f552b9 --- /dev/null +++ b/tests/liberty/foundry_data/rules.txt @@ -0,0 +1,16 @@ +-wire_load +-wire_load_selection +-default_wire_load +-default_wire_load_area +-default_wire_load_capacitance +-default_wire_load_mode +-default_wire_load_resistance +-default_cell_leakage_power +-default_wire_load_selection +-default_leakage_power_density +-lu_table_template +-power_lut_template +-leakage_power +-cell_leakage_power +-leakage_power +-internal_power diff --git a/tests/liberty/foundry_data/sg13g2_stdcell_typ_1p20V_25C.lib.filtered.gz b/tests/liberty/foundry_data/sg13g2_stdcell_typ_1p20V_25C.lib.filtered.gz new file mode 100644 index 00000000000..81c100e4359 Binary files /dev/null and b/tests/liberty/foundry_data/sg13g2_stdcell_typ_1p20V_25C.lib.filtered.gz differ diff --git a/tests/liberty/options_test.ys b/tests/liberty/options_test.ys new file mode 100644 index 00000000000..54957950e4f --- /dev/null +++ b/tests/liberty/options_test.ys @@ -0,0 +1,23 @@ +# Test memory macro gets ignored due to -ignore_buses +read_verilog -noblackbox <Y arc on nand2_1 exists +select -assert-any =sg13g2_nand2_1/i:A %co1:+$specify2[SRC] =sg13g2_nand2_1/o:Y %co1:+$specify2[DST] %i + +# D->Q arc on sdfbbp_1 doesn't +select -assert-none =sg13g2_sdfbbp_1/i:D %co1:+$specify2[SRC] =sg13g2_nand2_1/o:Q %co1:+$specify2[DST] %i diff --git a/tests/liberty/unit_delay.ys b/tests/liberty/unit_delay.ys deleted file mode 100644 index 8dd4091835a..00000000000 --- a/tests/liberty/unit_delay.ys +++ /dev/null @@ -1,3 +0,0 @@ -# Nothing gets imported: the file lacks timing data -read_liberty -wb -unit_delay normal.lib -select -assert-none =*/t:$specify* diff --git a/tests/memories/nordports.ys b/tests/memories/nordports.ys new file mode 100644 index 00000000000..d3694e64a3b --- /dev/null +++ b/tests/memories/nordports.ys @@ -0,0 +1,9 @@ +# check memory_collect doesn't produce invalid RTLIL on a memory w/o read ports +read_rtlil < $1.ys <