Merge multiple versions of get_read_address() into one

This commit is contained in:
Ilari Liusvaara 2014-04-21 15:54:36 +03:00
parent acb0c481a5
commit ceed4f622f
4 changed files with 35 additions and 74 deletions

View file

@ -36,5 +36,7 @@ extern uint64_t lua_timer_hook_time;
extern void* synchronous_paint_ctx; extern void* synchronous_paint_ctx;
void lua_renderq_run(lua_render_context* ctx, void* synchronous_paint_ctx); void lua_renderq_run(lua_render_context* ctx, void* synchronous_paint_ctx);
uint64_t lua_get_vmabase(const std::string& vma);
uint64_t lua_get_read_address(lua::parameters& P);
#endif #endif

View file

@ -5,22 +5,12 @@
namespace 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;
throw std::runtime_error("No such VMA");
}
template<bool create> template<bool create>
int dump_sprite(lua::state& L, lua::parameters& P) int dump_sprite(lua::state& L, lua::parameters& P)
{ {
std::string vma;
lua_bitmap* b; lua_bitmap* b;
uint64_t addr; uint64_t addr;
uint32_t width, height; uint32_t width, height;
uint64_t vmabase = 0;
size_t stride1 = 32; size_t stride1 = 32;
size_t stride2; size_t stride2;
@ -31,15 +21,10 @@ namespace
if((width | height) & 8) if((width | height) & 8)
throw std::runtime_error("The image size must be multiple of 8x8"); throw std::runtime_error("The image size must be multiple of 8x8");
} }
if(P.is_string()) { addr = lua_get_read_address(P);
P(vma);
vmabase = get_vmabase(L, vma);
}
P(addr);
if(create) if(create)
P(width, height); P(width, height);
P(P.optional(stride2, 512)); P(P.optional(stride2, 512));
addr += vmabase;
if(create) if(create)
b = lua::_class<lua_bitmap>::create(L, width * 8, height * 8); b = lua::_class<lua_bitmap>::create(L, width * 8, height * 8);
@ -101,10 +86,8 @@ namespace
template<bool create> template<bool create>
int dump_palette(lua::state& L, lua::parameters& P) int dump_palette(lua::state& L, lua::parameters& P)
{ {
std::string vma;
uint64_t addr; uint64_t addr;
bool full, ftrans; bool full, ftrans;
uint64_t vmabase = 0;
lua_palette* p; lua_palette* p;
if(!create) { if(!create) {
@ -114,15 +97,10 @@ namespace
throw std::runtime_error("Palette to read must be 16 or 256 colors"); throw std::runtime_error("Palette to read must be 16 or 256 colors");
full = (ccount == 256); full = (ccount == 256);
} }
if(P.is_string()) { addr = lua_get_read_address(P);
P(vma);
vmabase = get_vmabase(L, vma);
}
P(addr);
if(create) if(create)
P(full); P(full);
P(ftrans); P(ftrans);
addr += vmabase;
size_t ps = full ? 256 : 16; size_t ps = full ? 256 : 16;
if(create) { if(create) {

View file

@ -4,23 +4,6 @@
namespace namespace
{ {
uint64_t get_vmabase(const std::string& vma)
{
for(auto i : lsnes_memory.get_regions())
if(i->name == vma)
return i->base;
throw std::runtime_error("No such VMA");
}
uint64_t get_read_address(lua::parameters& P)
{
uint64_t vmabase = 0;
if(P.is_string())
vmabase = get_vmabase(P.arg<std::string>());
auto addr = P.arg<uint64_t>();
return addr + vmabase;
}
class compare_obj class compare_obj
{ {
public: public:
@ -81,7 +64,7 @@ namespace
uint64_t addr, size; uint64_t addr, size;
uint64_t stride = 0, rows = 1; uint64_t stride = 0, rows = 1;
addr = get_read_address(P); addr = lua_get_read_address(P);
P(size, P.optional(rows, 1)); P(size, P.optional(rows, 1));
if(rows > 1) if(rows > 1)
P(stride); P(stride);

View file

@ -13,25 +13,25 @@
#include "library/hex.hpp" #include "library/hex.hpp"
#include "library/int24.hpp" #include "library/int24.hpp"
uint64_t lua_get_vmabase(const std::string& vma)
{
for(auto i : lsnes_memory.get_regions())
if(i->name == vma)
return i->base;
throw std::runtime_error("No such VMA");
}
uint64_t lua_get_read_address(lua::parameters& P)
{
uint64_t vmabase = 0;
if(P.is_string())
vmabase = lua_get_vmabase(P.arg<std::string>());
auto addr = P.arg<uint64_t>();
return addr + vmabase;
}
namespace namespace
{ {
uint64_t get_vmabase(const std::string& vma)
{
for(auto i : lsnes_memory.get_regions())
if(i->name == vma)
return i->base;
throw std::runtime_error("No such VMA");
}
uint64_t get_read_address(lua::parameters& P)
{
uint64_t vmabase = 0;
if(P.is_string())
vmabase = get_vmabase(P.arg<std::string>());
auto addr = P.arg<uint64_t>();
return addr + vmabase;
}
template<typename T, T (memory_space::*rfun)(uint64_t addr), template<typename T, T (memory_space::*rfun)(uint64_t addr),
bool (memory_space::*wfun)(uint64_t addr, T value)> bool (memory_space::*wfun)(uint64_t addr, T value)>
void do_rw(lua::state& L, uint64_t addr, bool wrflag) void do_rw(lua::state& L, uint64_t addr, bool wrflag)
@ -46,7 +46,7 @@ namespace
template<typename T, T (memory_space::*rfun)(uint64_t addr)> template<typename T, T (memory_space::*rfun)(uint64_t addr)>
int lua_read_memory(lua::state& L, lua::parameters& P) int lua_read_memory(lua::state& L, lua::parameters& P)
{ {
auto addr = get_read_address(P); auto addr = lua_get_read_address(P);
L.pushnumber(static_cast<T>((lsnes_memory.*rfun)(addr))); L.pushnumber(static_cast<T>((lsnes_memory.*rfun)(addr)));
return 1; return 1;
} }
@ -54,7 +54,7 @@ namespace
template<typename T, bool (memory_space::*wfun)(uint64_t addr, T value)> template<typename T, bool (memory_space::*wfun)(uint64_t addr, T value)>
int lua_write_memory(lua::state& L, lua::parameters& P) int lua_write_memory(lua::state& L, lua::parameters& P)
{ {
auto addr = get_read_address(P); auto addr = lua_get_read_address(P);
T value = P.arg<T>(); T value = P.arg<T>();
(lsnes_memory.*wfun)(addr, value); (lsnes_memory.*wfun)(addr, value);
return 0; return 0;
@ -343,7 +343,7 @@ namespace
addr = 0xFFFFFFFFFFFFFFFFULL; addr = 0xFFFFFFFFFFFFFFFFULL;
P.skip(); P.skip();
} else if(type != DEBUG_TRACE) } else if(type != DEBUG_TRACE)
addr = get_read_address(P); addr = lua_get_read_address(P);
else else
P(addr); P(addr);
P(P.function(lfn)); P(P.function(lfn));
@ -375,7 +375,7 @@ namespace
aperture_make_fun<T, rfun, wfun>(L.get_master(), 0, 0xFFFFFFFFFFFFFFFFULL); aperture_make_fun<T, rfun, wfun>(L.get_master(), 0, 0xFFFFFFFFFFFFFFFFULL);
return 1; return 1;
} }
auto addr = get_read_address(P); auto addr = lua_get_read_address(P);
auto size = P.arg<uint64_t>(); auto size = P.arg<uint64_t>();
if(!size) if(!size)
throw std::runtime_error("Aperture with zero size is not valid"); throw std::runtime_error("Aperture with zero size is not valid");
@ -428,7 +428,7 @@ namespace
else else
addr--; addr--;
} else if(P.is_string()) { } else if(P.is_string()) {
vmabase = get_vmabase(P.arg<std::string>()); vmabase = lua_get_vmabase(P.arg<std::string>());
continue; continue;
} else } else
addr = P.arg<uint64_t>(); addr = P.arg<uint64_t>();
@ -458,7 +458,7 @@ namespace
{ {
uint64_t addr, value; uint64_t addr, value;
addr = get_read_address(P); addr = lua_get_read_address(P);
if(P.is_novalue()) { if(P.is_novalue()) {
debug_clear_cheat(addr); debug_clear_cheat(addr);
@ -522,7 +522,7 @@ namespace
bool mappable = true; bool mappable = true;
char buffer[BLOCKSIZE]; char buffer[BLOCKSIZE];
addr = get_read_address(P); addr = lua_get_read_address(P);
P(size); P(size);
if(extra) { if(extra) {
P(P.optional(rows, 1)); P(P.optional(rows, 1));
@ -604,7 +604,7 @@ namespace
uint64_t stride = 0, rows = 1; uint64_t stride = 0, rows = 1;
bool equals = true, mappable = true; bool equals = true, mappable = true;
addr = get_read_address(P); addr = lua_get_read_address(P);
P(daddr, size, P.optional(rows, 1)); P(daddr, size, P.optional(rows, 1));
if(rows > 1) if(rows > 1)
P(stride); P(stride);
@ -658,7 +658,7 @@ namespace
{ {
uint64_t addr, size; uint64_t addr, size;
addr = get_read_address(P); addr = lua_get_read_address(P);
P(size); P(size);
L.newtable(); L.newtable();
@ -683,7 +683,7 @@ namespace
uint64_t addr, size; uint64_t addr, size;
int ltbl; int ltbl;
addr = get_read_address(P); addr = lua_get_read_address(P);
P(size, P.table(ltbl)); P(size, P.table(ltbl));
char buffer[BLOCKSIZE]; char buffer[BLOCKSIZE];
@ -777,13 +777,11 @@ namespace
int lua_mmap_struct::map(lua::state& L, lua::parameters& P) int lua_mmap_struct::map(lua::state& L, lua::parameters& P)
{ {
std::string name, type; std::string name, type;
uint64_t vmabase = 0, addr; uint64_t addr;
P(P.skipped(), name); P(P.skipped(), name);
if(P.is_string()) addr = lua_get_read_address(P);
vmabase = get_vmabase(P.arg<std::string>()); P(type);
P(addr, type);
addr += vmabase;
if(type == "byte") if(type == "byte")
mappings[name] = mapping(addr, do_rw<uint8_t, &memory_space::read<uint8_t>, mappings[name] = mapping(addr, do_rw<uint8_t, &memory_space::read<uint8_t>,