Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SendCmioResponse #74

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Added SendCmioResponse

## [0.12.1] - 2024-08-12
### Changed
Expand Down
40 changes: 22 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ DEPDIRS = $(TESTS_DATA_DIR) $(LOG_TEST_DIR)

help:
@echo 'Cleaning targets:'
@echo ' clean - clean the cache artifacts and generated files'
@echo ' clean - clean the cache artifacts and generated files'
@echo 'Generic targets:'
@echo '* all - build solidity code. To build from a clean clone, run: make submodules all'
@echo ' build - build solidity code'
@echo ' generate-step - generate solidity-step code from cpp'
@echo ' generate-reset - generate solidity-reset code from cpp'
@echo ' generate-constants - generate solidity-constants code by querying the cartesi machine'
@echo ' generate-mock - generate mock library code'
@echo ' generate-prod - generate production library code'
@echo ' generate-replay - generate replay tests'
@echo ' pretest - download necessary files for tests'
@echo ' test-all - test all'
@echo ' test-mock - test binary files with mock library'
@echo ' test-prod - test production code'
@echo ' test-replay - test log files'
@echo '* all - build solidity code. To build from a clean clone, run: make submodules all'
@echo ' build - build solidity code'
@echo ' generate-step - generate solidity-step code from cpp'
@echo ' generate-reset - generate solidity-reset code from cpp'
@echo ' generate-send-cmio-response - generate solidity-send-cmio-response code from cpp'
@echo ' generate-constants - generate solidity-constants code by querying the cartesi machine'
@echo ' generate-mock - generate mock library code'
@echo ' generate-prod - generate production library code'
@echo ' generate-replay - generate replay tests'
@echo ' pretest - download necessary files for tests'
@echo ' test-all - test all'
@echo ' test-mock - test binary files with mock library'
@echo ' test-prod - test production code'
@echo ' test-replay - test log files'


all: build test-all

build: generate-step generate-reset generate-constants generate-prod
build: generate-step generate-reset generate-send-cmio-response generate-constants generate-prod
forge build --use 0.8.21

clean:
Expand All @@ -64,7 +65,7 @@ test-prod: dep
test-replay: dep
$(MAKE) generate-prod
$(MAKE) generate-replay
forge test --use 0.8.21 -vv --match-contract "UArchReplay|UArchReset"
forge test --use 0.8.21 -vv --match-contract "UArchReset|SendCmioResponse|UArchReplay"

generate-mock:
./helper_scripts/generate_AccessLogs.sh mock
Expand All @@ -79,14 +80,17 @@ generate-replay:
$(MAKE) fmt

generate-constants: $(EMULATOR_DIR)
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchConstants.sh
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_EmulatorConstants.sh

generate-step: $(EMULATOR_DIR)/src/uarch-step.h $(EMULATOR_DIR)/src/uarch-step.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchStep.sh

generate-reset: $(EMULATOR_DIR)/src/uarch-reset-state.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_UArchReset.sh

generate-send-cmio-response: $(EMULATOR_DIR)/src/uarch-reset-state.cpp
EMULATOR_DIR=$(EMULATOR_DIR) ./helper_scripts/generate_SendCmioResponse.sh

fmt:
forge fmt src test

Expand Down Expand Up @@ -119,4 +123,4 @@ $(LOG_TEST_DIR): | download
submodules:
git submodule update --init --recursive

.PHONY: help all build clean checksum-download shasum-download fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay generate-constants generate-reset
.PHONY: help all build clean checksum-download shasum-download fmt generate-mock generate-prod generate-replay generate-step pretest submodules test-all test-mock test-prod test-replay generate-constants generate-reset gemerate=semd-cmio-response
mpernambuco marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The `UArchExecuteInsn` contract consists of the machine instruction logic, such

## Compatibility Layer

The `UArchCompat` contract is taking care of all the differences in the two implementations. Ranging from programming languages (Cpp versus Solidity) to architectural differences (RISC-V versus EVM).
The `EmulatorCompat` contract is taking care of all the differences in the two implementations. Ranging from programming languages (Cpp versus Solidity) to architectural differences (RISC-V versus EVM).

## Getting Started

Expand Down
43 changes: 43 additions & 0 deletions helper_scripts/generate_EmulatorConstants.lua
mpernambuco marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/lua5.4

-- This scripts generates a snippet of solidity code to be inserted
-- in the EmulatorhConstants.sol file.

local cartesi = require("cartesi")

local function hex(n)
return string.format("%x", n)
end

local function hexstring(hash)
return (string.gsub(hash, ".", function(c) return string.format("%02x", string.byte(c)) end))
end

local out = io.stdout

out:write(' uint64 constant UARCH_CYCLE_ADDRESS = 0x' .. hex(cartesi.machine.get_csr_address("uarch_cycle")) .. ';\n')
out:write(' uint64 constant UARCH_HALT_FLAG_ADDRESS = 0x' ..
hex(cartesi.machine.get_csr_address("uarch_halt_flag")) .. ';\n')
out:write(' uint64 constant UARCH_PC_ADDRESS = 0x' .. hex(cartesi.machine.get_csr_address("uarch_pc")) .. ';\n')
out:write(' uint64 constant UARCH_X0_ADDRESS = 0x' .. hex(cartesi.machine.get_uarch_x_address(0)) .. ';\n')
out:write(' uint64 constant UARCH_SHADOW_START_ADDRESS = 0x' .. hex(cartesi.UARCH_SHADOW_START_ADDRESS) .. ';\n')
out:write(' uint64 constant UARCH_SHADOW_LENGTH = 0x' .. hex(cartesi.UARCH_SHADOW_LENGTH) .. ';\n')
out:write(' uint64 constant UARCH_RAM_START_ADDRESS = 0x' .. hex(cartesi.UARCH_RAM_START_ADDRESS) .. ';\n')
out:write(' uint64 constant UARCH_RAM_LENGTH = 0x' .. hex(cartesi.UARCH_RAM_LENGTH) .. ';\n')
out:write(' uint64 constant UARCH_STATE_START_ADDRESS = 0x' .. hex(cartesi.UARCH_STATE_START_ADDRESS) .. ';\n')
out:write(' uint8 constant UARCH_STATE_LOG2_SIZE = ' .. cartesi.UARCH_STATE_LOG2_SIZE .. ';\n')
out:write(' bytes32 constant UARCH_PRISTINE_STATE_HASH = 0x' .. hexstring(cartesi.UARCH_PRISTINE_STATE_HASH) .. ';\n')
out:write(' uint64 constant UARCH_ECALL_FN_HALT = ' .. cartesi.UARCH_ECALL_FN_HALT .. ';\n')
out:write(' uint64 constant UARCH_ECALL_FN_PUTCHAR = ' .. cartesi.UARCH_ECALL_FN_PUTCHAR .. ';\n')
out:write(' uint64 constant IFLAGS_ADDRESS = 0x' .. hex(cartesi.machine.get_csr_address("iflags")) .. ';\n')
out:write(' uint64 constant HTIF_FROMHOST_ADDRESS = 0x' ..
hex(cartesi.machine.get_csr_address("htif_fromhost")) .. ';\n')
out:write(' uint8 constant HTIF_YIELD_REASON_ADVANCE_STATE = 0x' ..
hex(cartesi.machine.HTIF_YIELD_REASON_ADVANCE_STATE) .. ';\n')
out:write(' uint32 constant TREE_LOG2_WORD_SIZE = 0x' .. hex(cartesi.TREE_LOG2_WORD_SIZE) .. ';\n')
out:write(' uint32 constant TREE_WORD_SIZE = uint32(1) << TREE_LOG2_WORD_SIZE;\n')
out:write(' uint64 constant PMA_CMIO_RX_BUFFER_START = 0x' .. hex(cartesi.PMA_CMIO_RX_BUFFER_START) .. ';\n')
out:write(' uint8 constant PMA_CMIO_RX_BUFFER_LOG2_SIZE = 0x' .. hex(cartesi.PMA_CMIO_RX_BUFFER_LOG2_SIZE) .. ';\n')
out:close()

out:close()
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -e

EMULATOR_DIR=${EMULATOR_DIR:-"../emulator"}

TEMPLATE_FILE="./templates/UArchConstants.sol.template"
TARGET_FILE="src/UArchConstants.sol"
TEMPLATE_FILE="./templates/EmulatorConstants.sol.template"
TARGET_FILE="src/EmulatorConstants.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

Expand All @@ -28,7 +28,7 @@ constants=$(docker run --rm \
-v`pwd`:/opt/cartesi/machine-solidity-step \
-w /opt/cartesi/machine-solidity-step \
cartesi/machine-emulator:devel \
/opt/cartesi/machine-solidity-step/helper_scripts/generate_UArchConstants.lua)
/opt/cartesi/machine-solidity-step/helper_scripts/generate_EmulatorConstants.lua)

# compose the solidity file from all components
echo -e "$h" "\n\n$constants" > $TARGET_FILE
Expand Down
53 changes: 53 additions & 0 deletions helper_scripts/generate_SendCmioResponse.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

set -e

SED=${SED:-"sed"}
EMULATOR_DIR=${EMULATOR_DIR:-"../emulator"}
CPP_RESET_PATH=${EMULATOR_DIR}"/src/send-cmio-response.cpp"

TEMPLATE_FILE="./templates/SendCmioResponse.sol.template"
TARGET_FILE="src/SendCmioResponse.sol"
COMPAT_FILE="src/EmulatorCompat.sol"
CONSTANTS_FILE="src/EmulatorConstants.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

# get function names from EmulatorCompat.sol
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

# get constant names from EmulatorConstants.sol
CONSTANTS=`cat $CONSTANTS_FILE | grep -E -o 'constant\s+[^ ]*' | $SED -E "s/constant//g; s/ //g" | tr '\n' '|' | sed "s/.$//"`

# grab head and tail of the template
start=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_START" -n | grep -Eo "[0-9]*"`
end=`cat "$TEMPLATE_FILE" | grep "$KEYWORD_END" -n | grep -Eo "[0-9]*"`
total=`wc -l "$TEMPLATE_FILE" | grep -Eo "[0-9]*"`
let last=total-end+1

h=`head -n $start $TEMPLATE_FILE`
t=`tail -n -$last $TEMPLATE_FILE`

cpp_src=`cat "$CPP_RESET_PATH"`
pattern="namespace cartesi \{(.*)\}"
[[ $cpp_src =~ $pattern ]]

# replace cpp specific syntaxes with solidity ones
cpp_src=`echo "${BASH_REMATCH[1]}" \
| $SED "/Explicit instantiatio/d" \
| $SED "/template/d" \
| $SED "s/machine_merkle_tree::get_log2_word_size()/TREE_LOG2_WORD_SIZE/g" \
| $SED -E "s/($COMPAT_FNS)/EmulatorCompat.\1/g" \
| $SED "s/writeMemoryWithPadding(a, PMA_CMIO_RX_BUFFER_START, data, dataLength, writeLengthLog2Size);/a.writeRegion(Memory.regionFromPhysicalAddress(PMA_CMIO_RX_BUFFER_START.toPhysicalAddress(),Memory.alignedSizeFromLog2(uint8(writeLengthLog2Size - TREE_LOG2_WORD_SIZE))),dataHash);"/g \
| $SED -E "s/($CONSTANTS)([^a-zA-Z])/EmulatorConstants.\1\2/g" \
| $SED "s/void send_cmio_response(STATE_ACCESS &a, uint16 reason, bytes data, uint32 dataLength) {/function sendCmioResponse(AccessLogs.Context memory a, uint16 reason, bytes32 dataHash, uint32 dataLength) internal pure {/" \
| $SED "s/const uint64/uint64/g" \
| $SED "s/const uint32/uint32/g" \
| $SED "/^$/N;/^\n$/D"
`

# compose the solidity file from all components
echo -e "$h" "\n\n$h_src" > $TARGET_FILE
echo "$cpp_src" >> $TARGET_FILE
echo -e "\n$t" >> $TARGET_FILE
32 changes: 0 additions & 32 deletions helper_scripts/generate_UArchConstants.lua

This file was deleted.

6 changes: 3 additions & 3 deletions helper_scripts/generate_UArchReset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ CPP_RESET_PATH=${EMULATOR_DIR}"/src/uarch-reset-state.cpp"

TEMPLATE_FILE="./templates/UArchReset.sol.template"
TARGET_FILE="src/UArchReset.sol"
COMPAT_FILE="src/UArchCompat.sol"
COMPAT_FILE="src/EmulatorCompat.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

# get function names from UArchCompat.sol
# get function names from EmulatorCompat.sol
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

Expand All @@ -33,7 +33,7 @@ pattern="namespace cartesi \{(.*)\}"
cpp_src=`echo "${BASH_REMATCH[1]}" \
| $SED "/Explicit instantiatio/d" \
| $SED "/template/d" \
| $SED -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| $SED -E "s/($COMPAT_FNS)/EmulatorCompat.\1/g" \
| $SED "s/void uarch_reset_state(UarchState &a) {/function reset(AccessLogs.Context memory a) internal pure {/"`

# compose the solidity file from all components
Expand Down
12 changes: 6 additions & 6 deletions helper_scripts/generate_UArchStep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CPP_STEP_H_PATH=${EMULATOR_DIR}"/src/uarch-step.h"

TEMPLATE_FILE="./templates/UArchStep.sol.template"
TARGET_FILE="src/UArchStep.sol"
COMPAT_FILE="src/UArchCompat.sol"
CONSTANTS_FILE="src/UArchConstants.sol"
COMPAT_FILE="src/EmulatorCompat.sol"
CONSTANTS_FILE="src/EmulatorConstants.sol"
KEYWORD_START="START OF AUTO-GENERATED CODE"
KEYWORD_END="END OF AUTO-GENERATED CODE"

Expand All @@ -32,11 +32,11 @@ pattern="enum class (.*) : int \{(.*)\};"
# retrieve enum type from cpp header
h_src=`echo "enum ${BASH_REMATCH[1]} {${BASH_REMATCH[2]}}"`

# get function names from UArchCompat.sol
# get function names from EmulatorCompat.sol
COMPAT_FNS=`cat $COMPAT_FILE | grep -o "function [^(]*(" | $SED "s/function//g" | $SED "s/(//g"`
COMPAT_FNS=`echo $COMPAT_FNS | $SED -E "s/( |\n)/|/g"`

# get constant names from UArchConstants.sol
# get constant names from EmulatorConstants.sol
CONSTANTS=`cat $CONSTANTS_FILE | grep -E -o 'constant\s+[^ ]*' | $SED -E "s/constant//g; s/ //g" | tr '\n' '|' | sed "s/.$//"`

cpp_src=`cat "$CPP_STEP_PATH"`
Expand All @@ -54,8 +54,8 @@ cpp_src=`echo "${BASH_REMATCH[1]}" \
| $SED -E "s/UArchStepStatus uarch_step/static inline UArchStepStatus step/g" \
| $SED -E "s/static inline (\w+) ($INTERNAL_FN)\(([^\n]*)\) \{/function \2\(\3\) internal pure returns \(\1\)\{/g" \
| $SED -E "s/static inline (\w+) (\w+)\(([^\n]*)\) \{/function \2\(\3\) private pure returns \(\1\)\{/g" \
| $SED -E "s/($COMPAT_FNS)/UArchCompat.\1/g" \
| $SED -E "s/([^a-zA-Z])($CONSTANTS)([^a-zA-Z])/UArchConstants.\1\2\3/g" \
| $SED -E "s/($COMPAT_FNS)/EmulatorCompat.\1/g" \
| $SED -E "s/([^a-zA-Z])($CONSTANTS)([^a-zA-Z])/EmulatorConstants.\1\2\3/g" \
| $SED "s/ returns (void)//g"`

# compose the solidity file from all components
Expand Down
4 changes: 2 additions & 2 deletions shasum-mock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ c114d9e59014339c29374f0085a266fb820449f123007e7c89f2f9ec680e5de0 src/AccessLogs
b0bb91ae378db1c938e73eccde20c539da01b8556165835839fd1794ca573404 src/Buffer.sol
c2e9abd5557092fb2c7a3b6eb3d55933613c202b8ce54bba2e8cbd2d276278a1 src/Memory.sol
06df1c5a0a6094fdc3f55240a42b1991290eb672fae6833e9f2b4d4bfe2a5b88 src/MetaStep.sol
d858085617f1e8037e06420d9569e2196b72a8bd0dd217aefbb8bb738b44a8fd src/UArchCompat.sol
1152e2e0f1fdc8befe075251f5e69dcc55ec2666a287fc5002bfa021b2aaea98 src/UArchConstants.sol
d858085617f1e8037e06420d9569e2196b72a8bd0dd217aefbb8bb738b44a8fd src/EmulatorCompat.sol
1152e2e0f1fdc8befe075251f5e69dcc55ec2666a287fc5002bfa021b2aaea98 src/EmulatorConstants.sol
c250ea17a7a471716148a60528b2cd01bd82dffd1c3c57aa0a7e99527fe44527 src/UArchReset.sol
40ab03ff44235b8ee5490314db112120e1ff99b347a5faf6a00f812e2497568d src/UArchStep.sol
4 changes: 2 additions & 2 deletions shasum-prod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
b0bb91ae378db1c938e73eccde20c539da01b8556165835839fd1794ca573404 src/Buffer.sol
d5de348b450c7cb5237a836d8e6e22cbfb4649f5892fac30f054efabdd7fc0a5 src/Memory.sol
06df1c5a0a6094fdc3f55240a42b1991290eb672fae6833e9f2b4d4bfe2a5b88 src/MetaStep.sol
0cdad8286752fbecf920db8095a764802ed88236efe7d179acdd88e018291b33 src/UArchCompat.sol
60ab17c719b0cb58047bfa5a83e390b30b32f5be00243890f2d321635bc5359a src/UArchConstants.sol
0cdad8286752fbecf920db8095a764802ed88236efe7d179acdd88e018291b33 src/EmulatorCompat.sol
60ab17c719b0cb58047bfa5a83e390b30b32f5be00243890f2d321635bc5359a src/EmulatorConstants.sol
c250ea17a7a471716148a60528b2cd01bd82dffd1c3c57aa0a7e99527fe44527 src/UArchReset.sol
40ab03ff44235b8ee5490314db112120e1ff99b347a5faf6a00f812e2497568d src/UArchStep.sol
2 changes: 1 addition & 1 deletion src/AccessLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
pragma solidity ^0.8.0;

import "./Buffer.sol";
import "./UArchConstants.sol";
import "./EmulatorConstants.sol";

library AccessLogs {
using Buffer for Buffer.Context;
Expand Down
7 changes: 4 additions & 3 deletions src/Buffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ library Buffer {
uint8 nodesCount = Memory.LOG2_MAX_SIZE - logOfSize;

for (uint64 i = 0; i < nodesCount; i++) {
Buffer.Context memory siblings =
Buffer.Context(buffer.data, buffer.offset + (i << 5));
Buffer.Context memory siblings = Buffer.Context(
buffer.data, buffer.offset + (i << Memory.LOG2_LEAF)
);

if (isEven(stride >> i)) {
drive =
Expand All @@ -111,7 +112,7 @@ library Buffer {
bytes32 drive
) internal pure returns (bytes32) {
(bytes32 root, uint8 nodesCount) = buffer.peekRoot(region, drive);
buffer.offset += uint128(nodesCount) << 5;
buffer.offset += uint128(nodesCount) << Memory.LOG2_LEAF;

return root;
}
Expand Down
Loading
Loading