Skip to content

Commit

Permalink
added Lualib example
Browse files Browse the repository at this point in the history
  • Loading branch information
shazz committed May 28, 2022
1 parent d36f144 commit c535264
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 0 deletions.
134 changes: 134 additions & 0 deletions libogc2/lualib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=<path to>devkitPPC")
endif

include $(DEVKITPRO)/libogc2/gamecube_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := lua
INCLUDES := include

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------

CFLAGS = -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS = $(CFLAGS)

LDFLAGS = -g $(MACHDEP) -Wl,-Map,$(notdir $@).map

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lgctools -lbba
LIBS += -lluajit -logc -lm

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS)

#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT := $(CURDIR)/$(TARGET)

export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR := $(CURDIR)/$(BUILD)

#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
export LD := $(CC)
else
export LD := $(CXX)
endif

export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(sFILES:.s=.o) $(SFILES:.S=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)

export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))

#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) \
-I$(LIBOGC_INC)

#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS := -L$(LIBOGC_LIB) $(foreach dir,$(LIBDIRS),-L$(dir)/lib)

export OUTPUT := $(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol

#---------------------------------------------------------------------------------
else

DEPENDS := $(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

$(OFILES_SOURCES) : $(HFILES)

#---------------------------------------------------------------------------------
# This rule links in binary data with the .lua extension
#---------------------------------------------------------------------------------
%.lua.o : %.lua
#---------------------------------------------------------------------------------
@echo $(notdir $<)
$(bin2o)

-include $(DEPENDS)

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
10 changes: 10 additions & 0 deletions libogc2/lualib/lua/script.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- script.lua
-- Receives a table, returns the sum of its components.
io.write("The table the script received has:\n");
x = 0
for i = 1, #foo do
print(i, foo[i])
x = x + foo[i]
end
io.write("Returning data back to C\n");
return x
137 changes: 137 additions & 0 deletions libogc2/lualib/source/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include <gccore.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <ogcsys.h>
#include <time.h>
#include <sys/time.h>

#include <debug.h>
#include <math.h>

#include <libgctools.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

static void *xfb = NULL;

// #define BBA_DEBUG
#define TRACE_PORT 10000
#define TRACE_IP "192.168.1.53"

extern const uint8_t script_lua[];

// Callbacks
static void return_to_loader (void) {
printf("Return to loader\n");
close_bba_logging();

void (*reload)() = (void(*)()) 0x80001800;
reload ();
}

static void reset_cb(u32 irq, void* ctx) {
printf("Reset button pushed with IRQ %d\n!", irq);
return_to_loader();
}

lua_State * LUA_Init() {

lua_State * L;
L = luaL_newstate();

// Init Lua state with default libraries
luaL_openlibs(L);

return L;
}

int main(int argc, char *argv[]) {

int is_connected = 0;
lua_State * L;
GXRModeObj *rmode;

VIDEO_Init();
rmode = VIDEO_GetPreferredMode(NULL);

PAD_Init();

L = LUA_Init();

// be sure we're going back to the gclink loader
SYS_SetResetCallback(reset_cb);

xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
VIDEO_Configure(rmode);
VIDEO_SetNextFramebuffer(xfb);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
VIDEO_WaitVSync();

if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

console_init(xfb, 20, 20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*2);
printf("\n\nTesting Lua JIT API\n");

#ifdef BBA_DEBUG
if(argc != 3) {
printf("No IP configuration received as arg, using DHCP");
argv = NULL;
}

is_connected = setup_bba_logging(TRACE_PORT, TRACE_IP, KPRINTF, true, argv);
if(is_connected)
kprintf("BBA Traces enabled\n");
#endif

while(1) {

VIDEO_WaitVSync();
PAD_ScanPads();

int buttonsDown = PAD_ButtonsDown(0);

if (buttonsDown & PAD_BUTTON_A) {

double sum;

// load script from buffer
int status = luaL_loadstring(L, (const char *)script_lua);
if(status) {
kprintf("Cannot load script from buffer: %s\n", lua_tostring(L, -1));
break;
}

lua_newtable(L);
for (int i = 1; i <= 5; i++) {
lua_pushnumber(L, i); /* Push the table index */
lua_pushnumber(L, i*2); /* Push the cell value */
lua_rawset(L, -3); /* Stores the pair in the table */
}
lua_setglobal(L, "foo");

int result = lua_pcall(L, 0, LUA_MULTRET, 0);
if (result) {
kprintf("Failed to run script: %s\n", lua_tostring(L, -1));
break;
}

sum = lua_tonumber(L, -1);
printf("Script returned: %.0f\n", sum);

lua_pop(L, 1); /* Take the returned value out of the stack */
}

if (buttonsDown & PAD_BUTTON_START) {
lua_close(L); /* Cya, Lua */
exit(0);
}

}
printf("Bye bye!)\n");

return 0;
}

0 comments on commit c535264

Please sign in to comment.