Skip to content

Commit df57f39

Browse files
committed
Add lua_setpico8memory() entry point to support operators @ @@ and $.
1 parent 116645d commit df57f39

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

lapi.c

+7
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ LUA_API lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf) {
134134
}
135135

136136

137+
LUA_API void lua_setpico8memory (lua_State *L, unsigned char const *p) {
138+
lua_lock(L);
139+
G(L)->pico8memory = p;
140+
lua_unlock(L);
141+
}
142+
143+
137144
LUA_API const lua_Number *lua_version (lua_State *L) {
138145
static const lua_Number version = LUA_VERSION_NUM;
139146
if (L == NULL) return &version;

lstate.h

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ typedef struct global_State {
140140
int gcmajorinc; /* pause between major collections (only in gen. mode) */
141141
int gcstepmul; /* GC `granularity' */
142142
lua_CFunction panic; /* to be called in unprotected errors */
143+
lu_byte const *pico8memory; /* pointer to PICO-8 RAM */
143144
struct lua_State *mainthread;
144145
const lua_Number *version; /* pointer to version number */
145146
TString *memerrmsg; /* memory-error message */

lua.h

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
252252
LUA_API int (lua_setmetatable) (lua_State *L, int objindex);
253253
LUA_API void (lua_setuservalue) (lua_State *L, int idx);
254254

255+
LUA_API void (lua_setpico8memory) (lua_State *L, unsigned char const *p);
255256

256257
/*
257258
** 'load' and 'call' functions (load and run Lua code)

luaconf.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,9 @@
583583
#define luai_numshr(L,a,b) ((a)>>int((b)))
584584
#define luai_numlshr(L,a,b) (l_mathop(lshr)((a),(b)))
585585
#define luai_numbnot(L,a) (~(a))
586-
#define luai_numpeek(L,a) ((void)(a),0)
587-
#define luai_numpeek2(L,a) ((void)(a),0)
588-
#define luai_numpeek4(L,a) ((void)(a),0)
586+
#define luai_numpeek(L,a) (lua_peek(L,a,1))
587+
#define luai_numpeek2(L,a) (lua_peek(L,a,2))
588+
#define luai_numpeek4(L,a) (lua_peek(L,a,4))
589589

590590
#define lua_number2str(s,n) [&]() { \
591591
int i = sprintf(s, "%1.4f", (double)n); \

lvm.c

+22
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,28 @@ static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
206206
}
207207

208208

209+
#define PEEK(ram, address) (ram && (address < 0x8000) ? ram[address] : 0)
210+
211+
static z8::fix32 lua_peek(struct lua_State *L, z8::fix32 a, int count)
212+
{
213+
unsigned char const *p = G(L)->pico8memory;
214+
int address = int(a) & 0x7fff;
215+
uint32_t ret = 0;
216+
switch (count) {
217+
case 4:
218+
ret |= PEEK(p, address + 1) << 8;
219+
ret |= PEEK(p, address);
220+
address += 2;
221+
case 2:
222+
ret |= PEEK(p, address + 1) << 24;
223+
case 1:
224+
ret |= PEEK(p, address) << 16;
225+
break;
226+
}
227+
return z8::fix32::frombits(ret);
228+
}
229+
230+
209231
static int l_strcmp (const TString *ls, const TString *rs) {
210232
const char *l = getstr(ls);
211233
size_t ll = ls->tsv.len;

0 commit comments

Comments
 (0)