forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
luascript.cpp
224 lines (196 loc) · 4.63 KB
/
luascript.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* UAE - The Un*x Amiga Emulator
*
* LUA Scripting Layer
*
* Copyright 2013 Frode SOlheim
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <lualib.h>
#include "options.h"
#include "savestate.h"
#include "memory.h"
#include "debug.h"
#include "identify.h"
#include "luascript.h"
#include "uae.h"
#include "zfile.h"
#include "threaddep/thread.h"
#ifdef WITH_LUA
static int g_num_states;
static lua_State *g_states[MAX_LUA_STATES];
static uae_sem_t lua_sem;
static int l_uae_read_u8(lua_State *L)
{
int addr = luaL_checkint(L, 1);
int value = debug_read_memory_8(addr);
lua_pushinteger(L, value);
return value >= 0 ? 1 : 0;
}
static int l_uae_write_u8(lua_State *L)
{
int addr = luaL_checkint(L, 1);
uint8_t value = luaL_checkint(L, 2);
debug_write_memory_8(addr, value);
return 0;
}
static int l_uae_write_u16(lua_State *L)
{
int addr = luaL_checkint(L, 1);
uint16_t value = luaL_checkint(L, 2);
debug_write_memory_16(addr, value);
return 0;
}
static int l_uae_read_u16(lua_State *L)
{
int addr = luaL_checkint(L, 1);
int value = debug_read_memory_16(addr);
lua_pushinteger(L, value);
return value >= 0 ? 1 : 0;
}
/* peek = read without any side-effects */
static int l_uae_peek_u16(lua_State *L)
{
int result = 0;
uint16_t value;
int addr = luaL_checkint(L, 1);
value = debug_peek_memory_16 (addr);
if (value >= 0) {
lua_pushinteger(L, value);
result = 1;
}
return result;
}
static int l_uae_read_config(lua_State *L)
{
int result = 0;
const char *s = luaL_checkstring(L, 1);
TCHAR *ts = au(s);
TCHAR out[MAX_DPATH];
if (cfgfile_searchconfig(ts, -1, out, sizeof out / sizeof(TCHAR)) == -1) {
char *c = ua(out);
lua_pushstring(L, c);
xfree(c);
result = 1;
}
xfree(ts);
return result;
}
static int l_uae_write_config(lua_State *L)
{
const char *s = luaL_checkstring(L, 1);
TCHAR *ts = au(s);
TCHAR out[MAX_DPATH];
cfgfile_modify(-1, ts, _tcslen(ts), out, sizeof out / sizeof(TCHAR));
char *c = ua(out);
lua_pushstring(L, c);
xfree(c);
return 1;
}
static int l_uae_log(lua_State *L)
{
const char *s = luaL_checkstring(L, 1);
write_log("%s", s);
//printf("%s", s);
return 0;
}
void uae_lua_log_error(lua_State *L, const char *msg)
{
write_log("%s: %s\n", msg, lua_tostring(L, -1));
//printf("%s: %s\n", msg, lua_tostring(L, -1));
}
void uae_lua_aquire_lock()
{
uae_sem_wait (&lua_sem);
}
void uae_lua_release_lock()
{
uae_sem_post (&lua_sem);
}
void uae_lua_run_handler(const char *name)
{
lua_State **L = g_states;
while(*L) {
uae_lua_aquire_lock();
lua_getglobal(*L, name);
if (lua_isnil(*L, -1)) {
//lua_pop(*L, 1);
}
else if (lua_pcall(*L, 0, 0, 0) != 0) {
uae_lua_log_error(*L, name);
//lua_pop(*L, 1);
}
lua_settop(*L, 0);
uae_lua_release_lock();
L++;
}
}
void uae_lua_load(const TCHAR *filename)
{
char *fn;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
fn = ua (filename);
int err = luaL_loadfilex(L, fn, NULL);
if (!err) {
err = lua_pcall(L, 0, LUA_MULTRET, 0);
if (!err) {
write_log (_T("'%s' loaded\n"), filename);
uae_lua_init_state (L);
}
}
if (err)
write_log (_T("'%s' initialization failed: %d\n"), err);
xfree (fn);
}
void uae_lua_loadall(void)
{
TCHAR tmp[MAX_DPATH];
fetch_luapath (tmp, sizeof tmp / sizeof (TCHAR));
_tcscat (tmp, _T("default.lua"));
if (zfile_exists (tmp))
uae_lua_load(tmp);
for (int i = 0; i < MAX_LUA_STATES; i++) {
if (currprefs.luafiles[i][0]) {
uae_lua_load(currprefs.luafiles[i]);
}
}
}
void uae_lua_init_state(lua_State *L)
{
write_log(_T("uae_lua_init_state %p\n"), L);
if (g_num_states == MAX_LUA_STATES) {
write_log(_T("WARNING: too many lua states (ignored this one)\n"));
return;
}
g_states[g_num_states] = L;
g_num_states++;
lua_register(L, "uae_log", l_uae_log);
lua_register(L, "uae_read_u8", l_uae_read_u8);
lua_register(L, "uae_read_u16", l_uae_read_u16);
lua_register(L, "uae_peek_u16", l_uae_peek_u16);
lua_register(L, "uae_write_u8", l_uae_write_u8);
lua_register(L, "uae_write_u16", l_uae_write_u16);
lua_register(L, "uae_read_config", l_uae_read_config);
lua_register(L, "uae_write_config", l_uae_write_config);
for (int i = 0; custd[i].name; i++) {
char *s = ua(custd[i].name);
lua_pushinteger(L, custd[i].adr);
lua_setglobal(L, s);
xfree(s);
}
}
void uae_lua_init(void)
{
uae_sem_init (&lua_sem, 0, 1);
}
void uae_lua_free(void)
{
for (int i = 0; i < g_num_states; i++) {
lua_close(g_states[i]);
}
g_num_states = 0;
uae_sem_destroy(&lua_sem);
}
#endif