Make all Lua memory functions optionally take VMA to use as base

This commit is contained in:
Ilari Liusvaara 2013-05-01 22:43:07 +03:00
parent 9fdb5b0deb
commit 6fe837ab48
3 changed files with 106 additions and 47 deletions

View file

@ -2825,7 +2825,7 @@ Finds the VMA containing specified address.
\end_layout
\begin_layout Subsubsection
memory.readbyte(number address)
memory.readbyte([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2833,7 +2833,7 @@ Reads the specified address as unsigned byte and returns the result.
\end_layout
\begin_layout Subsubsection
memory.readsbyte(number address)
memory.readsbyte([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2841,7 +2841,7 @@ Reads the specified address as signed byte and returns the result.
\end_layout
\begin_layout Subsubsection
memory.writebyte(number address, number value)
memory.writebyte([string vma, ]number address, number value)
\end_layout
\begin_layout Standard
@ -2850,7 +2850,7 @@ Writes the specified value (negative values undergo 2's complement) to specified
\end_layout
\begin_layout Subsubsection
memory.readword(number address)
memory.readword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2858,7 +2858,7 @@ Reads the specified address as unsigned word and returns the result.
\end_layout
\begin_layout Subsubsection
memory.readsword(number address)
memory.readsword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2866,7 +2866,7 @@ Reads the specified address as signed word and returns the result.
\end_layout
\begin_layout Subsubsection
memory.writeword(number address, number value)
memory.writeword([string vma, ]number address, number value)
\end_layout
\begin_layout Standard
@ -2875,7 +2875,7 @@ Writes the specified value (negative values undergo 2's complement) to specified
\end_layout
\begin_layout Subsubsection
memory.readdword(number address)
memory.readdword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2883,7 +2883,7 @@ Reads the specified address as unsigned doubleword and returns the result.
\end_layout
\begin_layout Subsubsection
memory.readsdword(number address)
memory.readsdword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2891,7 +2891,7 @@ Reads the specified address as signed doubleword and returns the result.
\end_layout
\begin_layout Subsubsection
memory.writedword(number address, number value)
memory.writedword([string vma, ]number address, number value)
\end_layout
\begin_layout Standard
@ -2900,7 +2900,7 @@ Writes the specified value (negative values undergo 2's complement) to specified
\end_layout
\begin_layout Subsubsection
memory.readqword(number address)
memory.readqword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2908,7 +2908,7 @@ Reads the specified address as unsigned quadword and returns the result.
\end_layout
\begin_layout Subsubsection
memory.readsqword(number address)
memory.readsqword([string vma, ]number address)
\end_layout
\begin_layout Standard
@ -2916,7 +2916,7 @@ Reads the specified address as signed quadword and returns the result.
\end_layout
\begin_layout Subsubsection
memory.writeqword(number address, number value)
memory.writeqword([string vma, ]number address, number value)
\end_layout
\begin_layout Standard
@ -2925,7 +2925,7 @@ Writes the specified value (negative values undergo 2's complement) to specified
\end_layout
\begin_layout Subsubsection
memory.hash_region(number base, number size)
memory.hash_region([string vma, ]number base, number size)
\end_layout
\begin_layout Standard
@ -2943,7 +2943,7 @@ Hash the current system state.
\end_layout
\begin_layout Subsubsection
memory.readregion(number base, number size)
memory.readregion([string vma, ]number base, number size)
\end_layout
\begin_layout Standard
@ -2955,7 +2955,7 @@ Warning: If the region crosses VMA boundary, the results are undefined.
\end_layout
\begin_layout Subsubsection
memory.map<type>([number base, number size])
memory.map<type>([[string vma, ]number base, number size])
\end_layout
\begin_layout Standard
@ -2968,7 +2968,7 @@ Type may be one of: byte, sbyte, word, sword, dword, sdword, qword or sqword.
\end_layout
\begin_layout Subsubsection
memory.writeregion(number base, number size, table data)
memory.writeregion([string vma, ]number base, number size, table data)
\end_layout
\begin_layout Standard
@ -2988,7 +2988,7 @@ Returns a new mapping structure (MMAP_STRUCT)
\end_layout
\begin_layout Subsubsection
MMAP_STRUCT(string key, number address, string type)
MMAP_STRUCT(string key, [string vma, ]number address, string type)
\end_layout
\begin_layout Standard

View file

@ -1421,67 +1421,72 @@ fields:
Finds the VMA containing specified address. Returns table in the
same format as read_vma or nil if not found.
7.10.4 memory.readbyte(number address)
7.10.4 memory.readbyte([string vma, ]number address)
Reads the specified address as unsigned byte and returns the
result.
7.10.5 memory.readsbyte(number address)
7.10.5 memory.readsbyte([string vma, ]number address)
Reads the specified address as signed byte and returns the
result.
7.10.6 memory.writebyte(number address, number value)
7.10.6 memory.writebyte([string vma, ]number address, number
value)
Writes the specified value (negative values undergo 2's
complement) to specified address (as a byte).
7.10.7 memory.readword(number address)
7.10.7 memory.readword([string vma, ]number address)
Reads the specified address as unsigned word and returns the
result.
7.10.8 memory.readsword(number address)
7.10.8 memory.readsword([string vma, ]number address)
Reads the specified address as signed word and returns the
result.
7.10.9 memory.writeword(number address, number value)
7.10.9 memory.writeword([string vma, ]number address, number
value)
Writes the specified value (negative values undergo 2's
complement) to specified address (as a word).
7.10.10 memory.readdword(number address)
7.10.10 memory.readdword([string vma, ]number address)
Reads the specified address as unsigned doubleword and returns
the result.
7.10.11 memory.readsdword(number address)
7.10.11 memory.readsdword([string vma, ]number address)
Reads the specified address as signed doubleword and returns the
result.
7.10.12 memory.writedword(number address, number value)
7.10.12 memory.writedword([string vma, ]number address, number
value)
Writes the specified value (negative values undergo 2's
complement) to specified address (as a doubleword).
7.10.13 memory.readqword(number address)
7.10.13 memory.readqword([string vma, ]number address)
Reads the specified address as unsigned quadword and returns the
result.
7.10.14 memory.readsqword(number address)
7.10.14 memory.readsqword([string vma, ]number address)
Reads the specified address as signed quadword and returns the
result.
7.10.15 memory.writeqword(number address, number value)
7.10.15 memory.writeqword([string vma, ]number address, number
value)
Writes the specified value (negative values undergo 2's
complement) to specified address (as a quadword).
7.10.16 memory.hash_region(number base, number size)
7.10.16 memory.hash_region([string vma, ]number base, number
size)
Hash specified number of bytes starting from specified address
and return the SHA-256.
@ -1491,14 +1496,15 @@ and return the SHA-256.
Hash the current system state. Mainly useful for debugging
savestates.
7.10.18 memory.readregion(number base, number size)
7.10.18 memory.readregion([string vma, ]number base, number size)
Read a region of memory.
• Warning: If the region crosses VMA boundary, the results are
undefined.
7.10.19 memory.map<type>([number base, number size])
7.10.19 memory.map<type>([[string vma, ]number base, number
size])
Returns a table mapping specified memory aperture for read/write.
If parameters are omitted, entiere map space is the aperture.
@ -1506,7 +1512,8 @@ If parameters are omitted, entiere map space is the aperture.
• Type may be one of: byte, sbyte, word, sword, dword, sdword,
qword or sqword.
7.10.20 memory.writeregion(number base, number size, table data)
7.10.20 memory.writeregion([string vma, ]number base, number
size, table data)
Write a region of memory.
@ -1517,7 +1524,8 @@ Write a region of memory.
Returns a new mapping structure (MMAP_STRUCT)
7.10.22 MMAP_STRUCT(string key, number address, string type)
7.10.22 MMAP_STRUCT(string key, [string vma, ]number address,
string type)
Bind key in mmap structure to specified address with specified
type.

View file

@ -10,6 +10,15 @@
namespace
{
uint64_t get_vmabase(lua_state& L, const std::string& vma)
{
for(auto i : lsnes_memory.get_regions())
if(i->name == vma)
return i->base;
L.pushstring("No such VMA");
L.error();
}
template<typename T, T (memory_space::*rfun)(uint64_t addr)>
class lua_read_memory : public lua_function
{
@ -17,7 +26,13 @@ namespace
lua_read_memory(const std::string& name) : lua_function(LS, name) {}
int invoke(lua_state& L)
{
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, "lua_read_memory"));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
L.pushnumber(static_cast<T>((lsnes_memory.*rfun)(addr)));
return 1;
}
@ -30,8 +45,14 @@ namespace
lua_write_memory(const std::string& name) : lua_function(LS, name) {}
int invoke(lua_state& L)
{
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
T value = L.get_numeric_argument<T>(2, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, "lua_read_memory"));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
T value = L.get_numeric_argument<T>(base + 2, fname.c_str());
(lsnes_memory.*wfun)(addr, value);
return 0;
}
@ -181,8 +202,14 @@ namespace
aperture_make_fun(L, 0, 0xFFFFFFFFFFFFFFFFULL, h);
return 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
uint64_t size = L.get_numeric_argument<uint64_t>(2, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, "lua_mmap_memory"));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
uint64_t size = L.get_numeric_argument<uint64_t>(base + 2, fname.c_str());
if(!size) {
L.pushstring("Aperture with zero size is not valid");
L.error();
@ -267,8 +294,14 @@ namespace
function_ptr_luafun hashmemory(LS, "memory.hash_region", [](lua_state& L, const std::string& fname) -> int {
std::string hash;
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
uint64_t size = L.get_numeric_argument<uint64_t>(2, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, fname.c_str()));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
uint64_t size = L.get_numeric_argument<uint64_t>(base + 2, fname.c_str());
char buffer[BLOCKSIZE];
sha256 h;
while(size > BLOCKSIZE) {
@ -288,8 +321,14 @@ namespace
function_ptr_luafun readmemoryr(LS, "memory.readregion", [](lua_state& L, const std::string& fname) -> int {
std::string hash;
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
uint64_t size = L.get_numeric_argument<uint64_t>(2, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, fname.c_str()));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
uint64_t size = L.get_numeric_argument<uint64_t>(base + 2, fname.c_str());
L.newtable();
char buffer[BLOCKSIZE];
uint64_t ctr = 0;
@ -309,8 +348,14 @@ namespace
function_ptr_luafun writememoryr(LS, "memory.writeregion", [](lua_state& L, const std::string& fname) -> int {
std::string hash;
uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
uint64_t size = L.get_numeric_argument<uint64_t>(2, fname.c_str());
int base = 0;
uint64_t vmabase = 0;
if(L.type(1) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(1, fname.c_str()));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 1, fname.c_str()) + vmabase;
uint64_t size = L.get_numeric_argument<uint64_t>(base + 2, fname.c_str());
char buffer[BLOCKSIZE];
uint64_t ctr = 0;
while(size > 0) {
@ -375,8 +420,14 @@ namespace
int lua_mmap_struct::map(lua_state& L)
{
const char* name = L.tostring(2);
uint64_t addr = L.get_numeric_argument<uint64_t>(3, "lua_mmap_struct::map");
const char* type = L.tostring(4);
int base = 0;
uint64_t vmabase = 0;
if(L.type(3) == LUA_TSTRING) {
vmabase = get_vmabase(L, L.get_string(3, "lua_mmap_struct::map"));
base = 1;
}
uint64_t addr = L.get_numeric_argument<uint64_t>(base + 3, "lua_mmap_struct::map");
const char* type = L.tostring(base + 4);
if(!name) {
L.pushstring("lua_mmap_struct::map: Bad name");
L.error();