Skip to content

Commit

Permalink
refactor!: rename CSR to REG in C API
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Oct 3, 2024
1 parent e126205 commit 9de486a
Show file tree
Hide file tree
Showing 44 changed files with 2,000 additions and 1,996 deletions.
2 changes: 1 addition & 1 deletion src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2019,7 +2019,7 @@ while math.ult(machine:read_mcycle(), max_mcycle) do
end
-- deal with halt
if machine:read_iflags_H() then
exit_code = machine:read_csr("htif_tohost_data") >> 1
exit_code = machine:read_reg("htif_tohost_data") >> 1
if exit_code ~= 0 then
stderr("\nHalted with payload: %u\n", exit_code)
else
Expand Down
34 changes: 17 additions & 17 deletions src/cartesi/gdbstub.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ function GDBStub:_handle_query(_, query)
elseif payload == "cycles" then -- print current cycle
self:_send_rcmd_reply(string.format("%u\n", self.machine:read_mcycle()))
return self:_send_ok()
elseif payload:find("^csr [%w_]+$") then -- read machine CSRs
local csr_name = payload:match("^csr ([%w_]+)$")
local read_method_name = "read_" .. csr_name
elseif payload:find("^reg [%w_]+$") then -- read machine registers
local reg_name = payload:match("^reg ([%w_]+)$")
local read_method_name = "read_" .. reg_name
local read_method = self.machine[read_method_name]
if not read_method then return self:_send_unsupported() end
local ok, res = pcall(read_method, self.machine)
Expand All @@ -214,25 +214,25 @@ function GDBStub:_handle_query(_, query)
self:_send_rcmd_reply(tostring(res) .. "\n")
end
return self:_send_ok()
elseif payload:find("^csr [%w_]+%=.*$") then -- write machine CSRs
local csr_name, val = payload:match("^csr ([%w_]+)%=(.*)$")
local write_method_name = "write_" .. csr_name
local read_method_name = "read_" .. csr_name
elseif payload:find("^reg [%w_]+%=.*$") then -- write machine registers
local reg_name, val = payload:match("^reg ([%w_]+)%=(.*)$")
local write_method_name = "write_" .. reg_name
local read_method_name = "read_" .. reg_name
local write_method = self.machine[write_method_name]
local read_method = self.machine[read_method_name]
if not write_method or not read_method then return self:_send_unsupported() end
val = tonumber(val)
if not val or math.type(val) ~= "integer" then
self:_send_rcmd_reply("ERROR: malformed CSR integer\n")
self:_send_rcmd_reply("ERROR: malformed register integer\n")
return self:_send_ok()
end
local write_ok = pcall(write_method, self.machine, val)
if not write_ok then return self:_send_unsupported() end
-- print the new CSR value
-- print the new register value
local ok, res = pcall(read_method, self.machine)
if ok and res ~= nil then
if math.type(res) == "integer" then
self:_send_rcmd_reply(string.format("%s = 0x%x (%d)\n", csr_name, res, res))
self:_send_rcmd_reply(string.format("%s = 0x%x (%d)\n", reg_name, res, res))
else
self:_send_rcmd_reply(tostring(res) .. "\n")
end
Expand Down Expand Up @@ -324,10 +324,10 @@ function GDBStub:_handle_write_reg(payload)
if not (reg and val) then return end
reg, val = hex2int(reg), hex2reg(val)
if reg > 0 and reg < 32 then -- machine registers
self.machine:write_csr("x" .. reg, val)
self.machine:write_reg("x" .. reg, val)
return self:_send_ok()
elseif reg == 32 then -- machine program counter
self.machine:write_csr("pc", val)
self.machine:write_reg("pc", val)
return self:_send_ok()
end
end
Expand All @@ -337,10 +337,10 @@ function GDBStub:_handle_read_all_regs()
local res = {}
-- read general purposes registers
for i = 0, 31 do
table.insert(res, reg2hex(self.machine:read_csr("x" .. i)))
table.insert(res, reg2hex(self.machine:read_reg("x" .. i)))
end
-- read program counter
table.insert(res, reg2hex(self.machine:read_csr("pc")))
table.insert(res, reg2hex(self.machine:read_reg("pc")))
res = table.concat(res)
return self:_send(res)
end
Expand All @@ -360,10 +360,10 @@ function GDBStub:_handle_write_all_regs(payload)
end
-- write general purposes registers
for i = 1, 31 do
self.machine:write_csr("x" .. i, regs[i])
self.machine:write_reg("x" .. i, regs[i])
end
-- write program counter
self.machine:write_csr("pc", regs[32])
self.machine:write_reg("pc", regs[32])
return self:_send_ok()
end

Expand Down Expand Up @@ -407,7 +407,7 @@ function GDBStub:_handle_continue()
-- need to run cycle by cycle, while checking breakpoints
while ult(mcycle, mcycle_end) do
machine:run(mcycle + 1)
if breakpoints[machine:read_csr("pc")] then -- breakpoint reached
if breakpoints[machine:read_reg("pc")] then -- breakpoint reached
return self:_send_signal(signals.SIGTRAP)
elseif machine:read_iflags_H() then -- machined halted
return self:_send_signal(signals.SIGTERM)
Expand Down
2 changes: 1 addition & 1 deletion src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ CM_API int luaopen_cartesi(lua_State *L) {
clua_setintegerfield(L, CM_VERSION_MINOR, "VERSION_MINOR", -1);
clua_setintegerfield(L, CM_VERSION_PATCH, "VERSION_PATCH", -1);
clua_setintegerfield(L, CM_HASH_SIZE, "HASH_SIZE", -1);
clua_setintegerfield(L, CM_CSR_COUNT, "CSR_COUNT", -1);
clua_setintegerfield(L, CM_REG_COUNT, "REG_COUNT", -1);
clua_setintegerfield(L, CM_TREE_LOG2_WORD_SIZE, "TREE_LOG2_WORD_SIZE", -1);
clua_setintegerfield(L, CM_TREE_LOG2_PAGE_SIZE, "TREE_LOG2_PAGE_SIZE", -1);
clua_setintegerfield(L, CM_TREE_LOG2_ROOT_SIZE, "TREE_LOG2_ROOT_SIZE", -1);
Expand Down
16 changes: 8 additions & 8 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ static int machine_obj_index_read_uarch_cycle(lua_State *L) {
return 1;
}

/// \brief This is the machine:read_csr() method implementation.
/// \brief This is the machine:read_reg() method implementation.
/// \param L Lua state.
static int machine_obj_index_read_csr(lua_State *L) {
static int machine_obj_index_read_reg(lua_State *L) {
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
uint64_t val{};
if (cm_read_csr(m.get(), clua_check_cm_proc_csr(L, 2), &val) != 0) {
if (cm_read_reg(m.get(), clua_check_cm_proc_reg(L, 2), &val) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushinteger(L, static_cast<lua_Integer>(val));
Expand Down Expand Up @@ -336,11 +336,11 @@ static int machine_obj_index_verify_merkle_tree(lua_State *L) {
return 1;
}

/// \brief This is the machine:write_csr() method implementation.
/// \brief This is the machine:write_reg() method implementation.
/// \param L Lua state.
static int machine_obj_index_write_csr(lua_State *L) {
static int machine_obj_index_write_reg(lua_State *L) {
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
if (cm_write_csr(m.get(), clua_check_cm_proc_csr(L, 2), luaL_checkinteger(L, 3)) != 0) {
if (cm_write_reg(m.get(), clua_check_cm_proc_reg(L, 2), luaL_checkinteger(L, 3)) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
return 0;
Expand Down Expand Up @@ -505,7 +505,7 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"get_proof", machine_obj_index_get_proof},
{"get_initial_config", machine_obj_index_get_initial_config},
{"get_root_hash", machine_obj_index_get_root_hash},
{"read_csr", machine_obj_index_read_csr},
{"read_reg", machine_obj_index_read_reg},
{"read_uarch_cycle", machine_obj_index_read_uarch_cycle},
{"read_iflags_H", machine_obj_index_read_iflags_H},
{"read_iflags_Y", machine_obj_index_read_iflags_Y},
Expand All @@ -522,7 +522,7 @@ static const auto machine_obj_index = cartesi::clua_make_luaL_Reg_array({
{"store", machine_obj_index_store},
{"verify_dirty_page_maps", machine_obj_index_verify_dirty_page_maps},
{"verify_merkle_tree", machine_obj_index_verify_merkle_tree},
{"write_csr", machine_obj_index_write_csr},
{"write_reg", machine_obj_index_write_reg},
{"write_memory", machine_obj_index_write_memory},
{"write_virtual_memory", machine_obj_index_write_virtual_memory},
{"translate_virtual_address", machine_obj_index_translate_virtual_address},
Expand Down
14 changes: 7 additions & 7 deletions src/clua-jsonrpc-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ static int jsonrpc_machine_class_get_default_config(lua_State *L) {
return 1;
}

/// \brief This is the machine.get_csr_address() method implementation.
static int jsonrpc_machine_class_get_csr_address(lua_State *L) {
/// \brief This is the machine.get_reg_address() method implementation.
static int jsonrpc_machine_class_get_reg_address(lua_State *L) {
auto &managed_jsonrpc_mgr =
clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, lua_upvalueindex(1), lua_upvalueindex(2));
uint64_t csr_address{};
const cm_csr csr = clua_check_cm_proc_csr(L, 1);
if (cm_jsonrpc_get_csr_address(managed_jsonrpc_mgr.get(), csr, &csr_address) != 0) {
uint64_t reg_address{};
const cm_reg reg = clua_check_cm_proc_reg(L, 1);
if (cm_jsonrpc_get_reg_address(managed_jsonrpc_mgr.get(), reg, &reg_address) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
lua_pushinteger(L, static_cast<lua_Integer>(csr_address));
lua_pushinteger(L, static_cast<lua_Integer>(reg_address));
return 1;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ static int jsonrpc_machine_class_verify_send_cmio_response(lua_State *L) {
/// \brief Contents of the machine class metatable __index table.
static const auto jsonrpc_machine_static_methods = cartesi::clua_make_luaL_Reg_array({
{"get_default_config", jsonrpc_machine_class_get_default_config},
{"get_csr_address", jsonrpc_machine_class_get_csr_address},
{"get_reg_address", jsonrpc_machine_class_get_reg_address},
{"verify_step_uarch", jsonrpc_machine_class_verify_step_uarch},
{"verify_reset_uarch", jsonrpc_machine_class_verify_reset_uarch},
{"verify_send_cmio_response", jsonrpc_machine_class_verify_send_cmio_response},
Expand Down
Loading

0 comments on commit 9de486a

Please sign in to comment.