You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You have Cartesi Machine installed globally in your system.
You have linux.bin and rootfs.ext2 images available locally (you could copy them from your installation).
You have a Lua 5.4 installed.
The LUA_CPATH_5_4 is able to find cartesi.so (default for global installations).
You are using Cartesi Machine 0.20.x
Hello example
The following example boots a machine and prints a hello message:
localcartesi=require'cartesi'-- Set machine configurationlocalconfig= {
dtb= {
entrypoint=[[echo Hello from inside!]]
},
flash_drive= {
{backing_store= {data_filename="rootfs.ext2"}},
},
ram= {
backing_store= {data_filename="linux.bin"},
length=0x8000000,
},
}
-- Create a new machinelocalmachine<close>=cartesi.machine(config)
-- Run the machinelocalbreak_reason=machine:run()
-- Print reason for run interruptionprint(({
[cartesi.BREAK_REASON_HALTED] ='Halted',
[cartesi.BREAK_REASON_YIELDED_MANUALLY] ='Yielded manually\n',
[cartesi.BREAK_REASON_YIELDED_AUTOMATICALLY] ='Yielded automatically\n',
[cartesi.BREAK_REASON_YIELDED_SOFTLY] ='Yielded softly\n',
[cartesi.BREAK_REASON_REACHED_TARGET_MCYCLE] ='Reached target machine cycle\n',
[cartesi.BREAK_REASON_FAILED] ='Interpreter failed\n',
})[break_reason])
-- Read and print machine cyclesprint('Cycles: '..machine:read_reg('mcycle'))
Compile and run with:
lua5.4 example.lua
You should get the following output:
Hello from inside!
Halted
Cycles: 48985837
IO example
To perform input/output operations from inside a machine to the outside you can use the CMIO (Cartesi Machine IO) with its GIO (generic IO) interface, the following example demonstrates this:
localcartesi=require'cartesi'-- Set machine configurationlocalconfig= {
dtb= {
entrypoint=[[echo '{"domain":16,"id":"'$(echo -n Hello from inside! | hex --encode)'"}' | rollup gio | grep -Eo '0x[0-9a-f]+' | tr -d '\n' | hex --decode; echo]]
},
flash_drive= {
{backing_store= {data_filename="rootfs.ext2"}},
},
ram= {
backing_store= {data_filename="linux.bin"},
length=0x8000000,
},
}
-- Create a new machinelocalmachine<close>=cartesi.machine(config)
-- Run the machinemachine:run()
-- Receive GIO requestlocalcmd, domain, data=machine:receive_cmio_request()
assert(cmd==cartesi.CMIO_YIELD_COMMAND_MANUALanddomain==16, 'unexpected CMIO request')
print(data)
-- Send GIO responsemachine:send_cmio_response(domain, 'Hello from outside!')
-- Resume the machinelocalbreak_reason=machine:run()
-- Print reason for run interruptionprint(({
[cartesi.BREAK_REASON_HALTED] ='Halted',
[cartesi.BREAK_REASON_YIELDED_MANUALLY] ='Yielded manually\n',
[cartesi.BREAK_REASON_YIELDED_AUTOMATICALLY] ='Yielded automatically\n',
[cartesi.BREAK_REASON_YIELDED_SOFTLY] ='Yielded softly\n',
[cartesi.BREAK_REASON_REACHED_TARGET_MCYCLE] ='Reached target machine cycle\n',
[cartesi.BREAK_REASON_FAILED] ='Interpreter failed\n',
})[break_reason])
-- Read and print machine cyclesprint('Cycles: '..machine:read_reg('mcycle'))
Compile and run with:
lua5.4 example-gio.lua
You should get the following output:
Hello from inside!
Hello from outside!
Halted
Cycles: 61800308
More information
For more information on how to use API please read the C API header, every function there is documented.