diff --git a/helper_scripts/generate_EmulatorConstants.lua b/helper_scripts/generate_EmulatorConstants.lua index 8868d31..05c1ea1 100755 --- a/helper_scripts/generate_EmulatorConstants.lua +++ b/helper_scripts/generate_EmulatorConstants.lua @@ -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') @@ -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() diff --git a/src/Buffer.sol b/src/Buffer.sol index adeb73e..154d6c1 100644 --- a/src/Buffer.sol +++ b/src/Buffer.sol @@ -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 = @@ -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; } diff --git a/src/EmulatorCompat.sol b/src/EmulatorCompat.sol index 4063af0..93c7282 100644 --- a/src/EmulatorCompat.sol +++ b/src/EmulatorCompat.sol @@ -154,6 +154,8 @@ library EmulatorCompat { ); } + // Conversions and arithmetic functions + function int8ToUint64(int8 val) internal pure returns (uint64) { return uint64(int64(val)); } @@ -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; } } diff --git a/src/EmulatorConstants.sol b/src/EmulatorConstants.sol index 08b8e3e..452e8cf 100644 --- a/src/EmulatorConstants.sol +++ b/src/EmulatorConstants.sol @@ -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; diff --git a/src/SendCmioResponse.sol b/src/SendCmioResponse.sol index 3e390b5..2f965db 100644 --- a/src/SendCmioResponse.sol +++ b/src/SendCmioResponse.sol @@ -17,7 +17,7 @@ /// @title SendCmioResponse /// @notice Sends a CMIO response //:#include macro.pp -/// DEV_COMMENT(templates/UArchReset.sol.template) +/// DEV_COMMENT(templates/SendCmioResponse.sol.template) pragma solidity ^0.8.0; diff --git a/templates/SendCmioResponse.sol.template b/templates/SendCmioResponse.sol.template index 8bfeb6e..5804043 100644 --- a/templates/SendCmioResponse.sol.template +++ b/templates/SendCmioResponse.sol.template @@ -15,9 +15,9 @@ // /// @title SendCmioResponse -/// @notice Reset microarchitecture to pristine state +/// @notice Sends a CMIO response //:#include macro.pp -/// DEV_COMMENT(templates/UArchReset.sol.template) +/// DEV_COMMENT(templates/SendCmioResponse.sol.template) pragma solidity ^0.8.0;