1
0
Fork 0
forked from len0rd/rockbox

hwstub: expose read/write functions

Previously only atomic read/write 8/16/32 were exposed. But it is useful to
be able to read a whole buffer at once, this is more efficient than N times
read8.

Change-Id: I06e331641e1ab1f74c0e16e8c432eafb398e8e6d
This commit is contained in:
Amaury Pouly 2017-11-12 14:13:21 +01:00
parent df0edba18e
commit dd6b8427f9

View file

@ -253,6 +253,21 @@ soc_word_t hw_read32(lua_State *state, soc_addr_t addr)
return u; return u;
} }
uint8_t *hw_read(lua_State *state, soc_addr_t addr, soc_word_t size)
{
uint8_t *buf = new uint8_t[size];
size_t sz = size;
error ret = g_hwdev->read(addr, buf, sz, false);
if(ret != error::SUCCESS || sz != size)
{
delete[] buf;
luaL_error(state, "fail to read<%d> @ %p: %d", size, addr, ret);
}
if(g_print_mem_rw)
printf("[read<%lu> @ %#lx = ...]\n", (unsigned long)size, (unsigned long)addr);
return buf;
}
void hw_write8(lua_State *state, soc_addr_t addr, soc_word_t val) void hw_write8(lua_State *state, soc_addr_t addr, soc_word_t val)
{ {
uint8_t u = val; uint8_t u = val;
@ -296,6 +311,19 @@ int my_lua_readn(lua_State *state)
return 1; return 1;
} }
int my_lua_read(lua_State *state)
{
int n = lua_gettop(state);
if(n != 2)
luaL_error(state, "read takes a two arguments: address, length");
unsigned length = mylua_checkunsigned(state, 2);
uint8_t *buf = hw_read(state, mylua_checkunsigned(state, 1), length);
/* lua strings are really byte buffers, they can contain arbitrary bytes including zeroes */
lua_pushlstring(state, (const char *)buf, length);
delete[] buf;
return 1;
}
int my_lua_writen(lua_State *state) int my_lua_writen(lua_State *state)
{ {
hw_writen_fn_t fn = (hw_writen_fn_t)lua_touserdata(state, lua_upvalueindex(1)); hw_writen_fn_t fn = (hw_writen_fn_t)lua_touserdata(state, lua_upvalueindex(1));
@ -797,6 +825,8 @@ bool my_lua_import_hwstub()
lua_pushlightuserdata(g_lua, (void *)&hw_read32); lua_pushlightuserdata(g_lua, (void *)&hw_read32);
lua_pushcclosure(g_lua, my_lua_readn, 1); lua_pushcclosure(g_lua, my_lua_readn, 1);
lua_setfield(g_lua, -2, "read32"); lua_setfield(g_lua, -2, "read32");
lua_pushcclosure(g_lua, my_lua_read, 0);
lua_setfield(g_lua, -2, "read");
lua_pushlightuserdata(g_lua, (void *)&hw_write8); lua_pushlightuserdata(g_lua, (void *)&hw_write8);
lua_pushcclosure(g_lua, my_lua_writen, 1); lua_pushcclosure(g_lua, my_lua_writen, 1);