Skip to content

Commit

Permalink
feat: resolve PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenctw committed Oct 28, 2024
1 parent 274aba0 commit c6543dc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 19 deletions.
12 changes: 8 additions & 4 deletions helper_scripts/generate_EmulatorConstants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ 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_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')
Expand All @@ -29,11 +30,14 @@ out:write(' bytes32 constant UARCH_PRISTINE_STATE_HASH = 0x' .. hexstring(car
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(' 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: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()
1 change: 0 additions & 1 deletion helper_scripts/generate_SendCmioResponse.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ cpp_src=`echo "${BASH_REMATCH[1]}" \
| $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/writeMemoryWithPadding(a, PMA_CMIO_RX_BUFFER_START, data, dataLength, writeLengthLog2Size);/a.writeRegion(Memory.regionFromPhysicalAddress(PMA_CMIO_RX_BUFFER_START.toPhysicalAddress(),Memory.alignedSizeFromLog2(writeLengthLog2Size)),dataHash);"/g \
| $SED "s/const uint64/uint64/g" \
| $SED "s/const uint32/uint32/g" \
| $SED "/^$/N;/^\n$/D"
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
32 changes: 27 additions & 5 deletions src/EmulatorCompat.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ library EmulatorCompat {
);
}

// Conversions and arithmetic functions

function int8ToUint64(int8 val) internal pure returns (uint64) {
return uint64(int64(val));
}
Expand Down Expand Up @@ -285,11 +287,31 @@ library EmulatorCompat {

function uint32Log2(uint32 value) external pure returns (uint32) {
require(value > 0, "EmulatorCompat: log2(0) is undefined");
uint32 result = 0;
while (value > 1) {
value >>= 1;
result++;
return 31 - clz(value);
}

function clz(uint32 x) internal pure returns (uint32) {
uint32 n = 0;
if (x & 0xFFFF0000 == 0) {
n = n + 16;
x = x << 16;
}
if (x & 0xFF000000 == 0) {
n = n + 8;
x = x << 8;
}
return result;
if (x & 0xF0000000 == 0) {
n = n + 4;
x = x << 4;
}
if (x & 0xC0000000 == 0) {
n = n + 2;
x = x << 2;
}
if (x & 0x80000000 == 0) {
n = n + 1;
}

return n;
}
}
3 changes: 2 additions & 1 deletion src/EmulatorConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//

/// @title EmulatorConstants
/// @notice Contains constants for micro-architecture
/// @notice Contains constants from the Cartesi Machine emulator
//:#include macro.pp
/// DEV_COMMENT(templates/EmulatorConstants.sol.template)

Expand All @@ -40,6 +40,7 @@ library EmulatorConstants {
uint64 constant UARCH_ECALL_FN_PUTCHAR = 2;
uint64 constant IFLAGS_ADDRESS = 0x2e8;
uint64 constant HTIF_FROMHOST_ADDRESS = 0x318;
uint8 constant HTIF_YIELD_REASON_ADVANCE_STATE = 0x0;
uint32 constant TREE_LOG2_WORD_SIZE = 0x5;
uint32 constant TREE_WORD_SIZE = uint32(1) << TREE_LOG2_WORD_SIZE;
uint64 constant PMA_CMIO_RX_BUFFER_START = 0x60000000;
Expand Down
4 changes: 2 additions & 2 deletions src/SendCmioResponse.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//

/// @title SendCmioResponse
/// @notice Reset microarchitecture to pristine state
/// @notice Send CMIO response
//:#include macro.pp
/// DEV_COMMENT(templates/UArchReset.sol.template)
/// DEV_COMMENT(templates/SendCmioResponse.sol.template)

pragma solidity ^0.8.0;

Expand Down
2 changes: 1 addition & 1 deletion templates/EmulatorConstants.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//

/// @title EmulatorConstants
/// @notice Contains constants for micro-architecture
/// @notice Contains constants from the Cartesi Machine emulator
//:#include macro.pp
/// DEV_COMMENT(templates/EmulatorConstants.sol.template)

Expand Down
4 changes: 2 additions & 2 deletions templates/SendCmioResponse.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
//

/// @title SendCmioResponse
/// @notice Reset microarchitecture to pristine state
/// @notice Send CMIO response
//:#include macro.pp
/// DEV_COMMENT(templates/UArchReset.sol.template)
/// DEV_COMMENT(templates/SendCmioResponse.sol.template)

pragma solidity ^0.8.0;

Expand Down

0 comments on commit c6543dc

Please sign in to comment.