Skip to content

The off-chain implementation of the Cartesi Machine

License

Notifications You must be signed in to change notification settings

neumagbutorcapri/machine-emulator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cartesi Machine Emulator

The Cartesi Machine Emulator is the reference off-chain implementation of the Cartesi Machine Specification. It's written in C/C++ with POSIX dependencies restricted to the terminal, process, and memory-mapping facilities. It is distributed as a library and scriptable in the Lua programming language.

The emulator implements RISC-V's RV64IMASU ISA. The letters after RV specify the extension set. This selection corresponds to a 64-bit machine, Integer arithmetic with Multiplication and division, Atomic operations, as well as the optional Supervisor and User privilege levels. In addition, Cartesi Machines support the Sv48 mode of address translation and memory protection.

Getting Started

Run make help for a list of target options. Here are some of them:

Cleaning targets:
  clean                      - clean the src/ artifacts
  depclean                   - clean + dependencies
  distclean                  - depclean + profile information and downloads
Docker targets:
  build-debian-image         - Build the machine-emulator debian based docker image

Requirements

  • C++ Compiler with support for C++17 (tested with GCC >= 8+ and Clang >= 8.x).
  • GNU Make >= 3.81
  • GRPC >= 1.45.0
  • Lua >= 5.4.4
  • Boost >= 1.81

Obs: Please note that Apple Clang Version number does not follow upstream LLVM/Clang.

Debian Bookworm

sudo apt-get install build-essential wget git clang-tidy-15 clang-format-15 \
        libboost1.81-dev libssl-dev \
        ca-certificates pkg-config lua5.4 liblua5.4-dev \
        libgrpc++-dev libprotobuf-dev protobuf-compiler-grpc \
        luarocks

sudo luarocks install --lua-version=5.4 lpeg
sudo luarocks install --lua-version=5.4 dkjson
sudo luarocks install --lua-version=5.4 luasocket
sudo luarocks install --lua-version=5.4 luasec
sudo luarocks install --lua-version=5.4 luaposix

MacOS

MacPorts
sudo port install clang-15 boost libtool wget pkgconfig grpc openssl lua lua-luarocks

sudo luarocks install --lua-version=5.4 lpeg
sudo luarocks install --lua-version=5.4 dkjson
sudo luarocks install --lua-version=5.4 luasocket
sudo luarocks install --lua-version=5.4 luasec
sudo luarocks install --lua-version=5.4 luaposix
Homebrew
brew install llvm@15 boost wget pkg-config grpc openssl lua luarocks
luarocks --lua-dir=$(brew --prefix)/opt/lua install lpeg
luarocks --lua-dir=$(brew --prefix)/opt/lua install dkjson
luarocks --lua-dir=$(brew --prefix)/opt/lua install luasocket
luarocks --lua-dir=$(brew --prefix)/opt/lua install luasec
luarocks --lua-dir=$(brew --prefix)/opt/lua install luaposix

For emulator scripts to work it is expected that lua5.4 binary is available in the system PATH. If operating system/package manager that you are using provides only lua or lua binary named in a different way (e.g. on Homebrew), please create symbolic link or alias lua5.4.

Build

make submodules
make downloads
make dep
make

Cleaning:

make depclean
make clean

Microarchitecture:

If you want to use a pre-built uarch RAM image instead of building one, use the variable UARCH_RAM_IMAGE to specify the path to the desired image file.

$ make UARCH_RAM_IMAGE=<path-to-your-uarch-ram.bin>

Install

sudo make install PREFIX=/usr/local

Build C libraries in standalone

Both libcartesi and libcartes_jsonrpc C libraries can be compiled in standalone, either as static or shared library:

make submodules
make downloads
make dep
make bundle-boost
make -C src release=yes libcartesi.a libcartesi_jsonrpc.a libcartesi.so libcartesi_jsonrpc.so

The .a and .so files will be available in src directory, you can use any of them to link your application.

You can even use other toolchains to cross compile targeting other platforms:

# Target WASM with Emscripten toolchain
make -C src release=yes \
  CC=emcc CXX=em++ AR="emar rcs" \
  libcartesi.a

# Target WASM with WASI SDK toolchain
make -C src release=yes \
  CC=/opt/wasi-sdk/bin/clang CXX=/opt/wasi-sdk/bin/clang++ AR="/opt/wasi-sdk/bin/llvm-ar rcs" \
  libcartesi.a

# Target Windows with mingw-w64 toolchain
make -C src release=yes \
  CC=x86_64-w64-mingw32-gcc \
  CXX=x86_64-w64-mingw32-g++ \
  AR="x86_64-w64-mingw32-ar rcs" \
  libcartesi.a

Running Tests

Copy the tests binaries to a directory called tests and run: (Eg.: )

make test

The default search path for binaries is machine-emulator/tests. Alternatively you can specify the binaries path using the CARTESI_TESTS_PATH variable as in:

make test CARTESI_TESTS_PATH=/full/path/to/test/binaries

Linter

We use clang-tidy 14 as the linter.

Install

Debian Bookworm

You need to install the package clang-tidy-15 and set it as the default executable with update-alternatives.

apt install clang-tidy-15
update-alternatives --install /usr/bin/clang-tidy clang-tidy /usr/bin/clang-tidy-15 120

Running Lint

make lint -j$(nproc)

Code format

We use clang-format to format the code base.

Install

Debian Bookworm

You need to install the package clang-format-15 and set is as the default executable with update-alternatives.

apt install clang-format-15
update-alternatives --install /usr/bin/clang-format clang-format /usr/bin/clang-format-15 120

Formatting code

make format

Checking whether the code is formatted

make check-format

Coverage

Dependencies

Debian Bookworm

If you want to run the GCC-based coverage, you should install the lcov package with the following command.

sudo apt install lcov

If you want to run the clang-based coverage, you should install the clang package with the following command.

sudo apt install clang llvm

Compilation

Before running the coverage, you should build the emulator with the flag coverage-toolchain=gcc or coverage-toolchain=clang. Make sure you run make clean to clean up any previous compilation. For GCC-based coverage run the following command.

make coverage-toolchain=gcc -j$(nproc)

For clang-based coverage run the following command.

make coverage-toolchain=clang -j$(nproc)

Running coverage

After building the emulator with coverage enable, you should run the following command. You need to specify the binaries test path using the CARTESI_TESTS_PATH variable. You also need to specify the directory containing the kernel and rootfs with the CARTESI_IMAGES_PATH variable. For instance:

make coverage=yes test-all coverage-report \
    CARTESI_TESTS_PATH=$(realpath ../tests/build) \
    CARTESI_IMAGES_PATH=$(realpath ./src)

This command will generate a coverage report in the src directory. For clang coverage, repeat the same command but with the flag coverage-toolchain=clang.

Contributing

Thank you for your interest in Cartesi! Head over to our Contributing Guidelines for instructions on how to sign our Contributors Agreement and get started with Cartesi!

Please note we have a Code of Conduct, please follow it in all your interactions with the project.

License

The machine-emulator repository and all contributions are licensed under LGPL 3.0. Please review our COPYING file.

About

The off-chain implementation of the Cartesi Machine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 82.9%
  • Lua 11.6%
  • C 3.2%
  • Makefile 1.9%
  • Shell 0.3%
  • Dockerfile 0.1%