1
0
Fork 0
mirror of https://gitlab.com/niansa/pilang2.git synced 2025-03-06 20:49:22 +01:00

Added 3ds target

This commit is contained in:
niansa 2020-05-27 13:20:23 +02:00
parent 8a4572991c
commit c772876a24
7 changed files with 402 additions and 32 deletions

7
.gitignore vendored
View file

@ -1,2 +1,7 @@
bin bin
obj obj-generic
obj-3ds
pilang2
*.smdh
*.elf
*.3dsx

View file

@ -1,21 +1,25 @@
CXX=g++ all: generic
CC=gcc
CXXFLAGS=-Wextra -Os -std=c++17 generic:
LDFLAGS=-Wextra @mv Makefile Makefile.bak
@mv Makefile-generic Makefile
@make | cat
@mv Makefile Makefile-generic | cat
@mv Makefile.bak Makefile | cat
all: obj pilang 3ds:
@mv Makefile Makefile.bak
obj: @mv Makefile-3ds Makefile
mkdir -p obj @make | cat
@mv Makefile Makefile-3ds | cat
pilang: obj/main.o @mv Makefile.bak Makefile | cat
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
obj/main.o: main.cpp
$(CXX) -c $^ -o $@ $(CXXFLAGS)
clean: clean:
rm -Rf ./obj/ @mv Makefile Makefile.bak
rm -f ./pilang @mv Makefile-3ds Makefile
@make clean | cat
@mv Makefile Makefile-3ds
@mv Makefile-generic Makefile
@make clean | cat
@mv Makefile Makefile-generic
@mv Makefile.bak Makefile | cat

255
Makefile-3ds Normal file
View file

@ -0,0 +1,255 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITARM)/3ds_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
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
# GRAPHICS is a list of directories containing graphics files
# GFXBUILD is the directory where converted graphics files will be placed
# If set to $(BUILD), it will statically link in the converted
# files as if they were data files.
#
# NO_SMDH: if set to anything, no SMDH file is generated.
# ROMFS is the directory which contains the RomFS, relative to the Makefile (Optional)
# APP_TITLE is the name of the app stored in the SMDH file (Optional)
# APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional)
# APP_AUTHOR is the author of the app stored in the SMDH file (Optional)
# ICON is the filename of the icon (.png), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.png
# - icon.png
# - <libctru folder>/default_icon.png
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := obj-3ds
SOURCES := source
DATA := data
INCLUDES := include
GRAPHICS := gfx
GFXBUILD := $(BUILD)
#ROMFS := romfs
#GFXBUILD := $(ROMFS)/gfx
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
CFLAGS := -g -Wall -O2 -mword-relocations \
-fomit-frame-pointer -ffunction-sections \
$(ARCH)
CFLAGS += $(INCLUDE) -DARM11 -D_3DS
CXXFLAGS := $(CFLAGS) -fno-rtti -std=c++17
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := -lctru -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(CTRULIB)
#---------------------------------------------------------------------------------
# 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 TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PICAFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.v.pica)))
SHLISTFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.shlist)))
GFXFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.t3s)))
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
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
ifeq ($(GFXBUILD),$(BUILD))
#---------------------------------------------------------------------------------
export T3XFILES := $(GFXFILES:.t3s=.t3x)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export ROMFS_T3XFILES := $(patsubst %.t3s, $(GFXBUILD)/%.t3x, $(GFXFILES))
export T3XHFILES := $(patsubst %.t3s, $(BUILD)/%.h, $(GFXFILES))
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES_BIN := $(addsuffix .o,$(BINFILES)) \
$(PICAFILES:.v.pica=.shbin.o) $(SHLISTFILES:.shlist=.shbin.o) \
$(addsuffix .o,$(T3XFILES))
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES)
export HFILES := $(PICAFILES:.v.pica=_shbin.h) $(SHLISTFILES:.shlist=_shbin.h) \
$(addsuffix .h,$(subst .,_,$(BINFILES))) \
$(GFXFILES:.t3s=.h)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) -I$(CURDIR)/wrappers/3ds
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
export _3DSXDEPS := $(if $(NO_SMDH),,$(OUTPUT).smdh)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.png)
ifneq (,$(findstring $(TARGET).png,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).png
else
ifneq (,$(findstring icon.png,$(icons)))
export APP_ICON := $(TOPDIR)/icon.png
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
ifeq ($(strip $(NO_SMDH)),)
export _3DSXFLAGS += --smdh=$(CURDIR)/$(TARGET).smdh
endif
ifneq ($(ROMFS),)
export _3DSXFLAGS += --romfs=$(CURDIR)/$(ROMFS)
endif
.PHONY: all clean
#---------------------------------------------------------------------------------
all: $(BUILD) $(GFXBUILD) $(DEPSDIR) $(ROMFS_T3XFILES) $(T3XHFILES)
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
$(BUILD):
@mkdir -p $@
ifneq ($(GFXBUILD),$(BUILD))
$(GFXBUILD):
@mkdir -p $@
endif
ifneq ($(DEPSDIR),$(BUILD))
$(DEPSDIR):
@mkdir -p $@
endif
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf $(GFXBUILD)
#---------------------------------------------------------------------------------
$(GFXBUILD)/%.t3x $(BUILD)/%.h : %.t3s
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@tex3ds -i $< -H $(BUILD)/$*.h -d $(DEPSDIR)/$*.d -o $(GFXBUILD)/$*.t3x
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).3dsx : $(OUTPUT).elf $(_3DSXDEPS)
$(OFILES_SOURCES) : $(HFILES)
$(OUTPUT).elf : $(OFILES)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
.PRECIOUS : %.t3x
#---------------------------------------------------------------------------------
%.t3x.o %_t3x.h : %.t3x
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
# rules for assembling GPU shaders
#---------------------------------------------------------------------------------
define shader-as
$(eval CURBIN := $*.shbin)
$(eval DEPSFILE := $(DEPSDIR)/$*.shbin.d)
echo "$(CURBIN).o: $< $1" > $(DEPSFILE)
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(CURBIN) | tr . _)`.h
echo "extern const u8" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(CURBIN) | tr . _)`.h
echo "extern const u32" `(echo $(CURBIN) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(CURBIN) | tr . _)`.h
picasso -o $(CURBIN) $1
bin2s $(CURBIN) | $(AS) -o $*.shbin.o
endef
%.shbin.o %_shbin.h : %.v.pica %.g.pica
@echo $(notdir $^)
@$(call shader-as,$^)
%.shbin.o %_shbin.h : %.v.pica
@echo $(notdir $<)
@$(call shader-as,$<)
%.shbin.o %_shbin.h : %.shlist
@echo $(notdir $<)
@$(call shader-as,$(foreach file,$(shell cat $<),$(dir $<)$(file)))
#---------------------------------------------------------------------------------
%.t3x %.h : %.t3s
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@tex3ds -i $< -H $*.h -d $*.d -o $*.t3x
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

20
Makefile-generic Normal file
View file

@ -0,0 +1,20 @@
CXX=g++
CC=gcc
CXXFLAGS=-Wextra -Os -std=c++17
LDFLAGS=-Wextra
all: obj-generic pilang2
obj-generic:
mkdir -p obj-generic
pilang2: obj-generic/pilang2.o
$(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS)
obj-generic/pilang2.o: source/pilang2.cpp
$(CXX) -c $^ -o $@ $(CXXFLAGS) -Iwrappers/generic
clean:
rm -Rf obj-generic
rm -f ./pilang2

View file

@ -27,10 +27,10 @@ along with pilang. If not, see <https://www.gnu.org/licenses/>.
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <csignal> #include <csignal>
#include <dlfcn.h>
#include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <wrapper.hh>
// Return type defs // Return type defs
std::string success = "Success"; std::string success = "Success";
@ -112,6 +112,7 @@ std::string get_rstring(std::string rstring) {
return rstring; return rstring;
} }
// Builtin functions // Builtin functions
std::string cmd_ret(std::vector<std::string> args) { std::string cmd_ret(std::vector<std::string> args) {
std::string retstring; std::string retstring;
@ -163,7 +164,7 @@ std::string cmd_exit(std::vector<std::string> args) {
} }
std::string exitcode = *args.begin(); std::string exitcode = *args.begin();
if (is_float(exitcode)) if (is_float(exitcode))
exit(std::stoi(exitcode)); _exit(std::stoi(exitcode));
else else
return badarguments; return badarguments;
} }
@ -198,9 +199,10 @@ std::string cmd_print(std::vector<std::string> args) {
return result; return result;
} }
std::string cmd_input(std::vector<std::string> args) { std::string cmd_input(std::vector<std::string> args) {
cmd_printnnl(args); std::string hint = cmd_ret(args);
cmd_printnnl({hint});
std::string result; std::string result;
std::getline(std::cin, result); std::getline(readkbd(hint), result);
return result; return result;
} }
std::string cmd_set(std::vector<std::string> args) { std::string cmd_set(std::vector<std::string> args) {
@ -227,7 +229,7 @@ std::string cmd_append(std::vector<std::string> args) {
auto argsit = args.begin(); auto argsit = args.begin();
std::string variablename = *argsit; std::string variablename = *argsit;
argsit++; argsit++;
for (int dummy; argsit != args.end(); argsit++) for (; argsit != args.end(); argsit++)
variables[variablename].append(*argsit); variables[variablename].append(*argsit);
return success; return success;
} }
@ -355,7 +357,7 @@ std::string cmd_addsub(bool type, std::vector<std::string> args) {
float result = std::atof((*argsit).c_str()); float result = std::atof((*argsit).c_str());
argsit++; argsit++;
// Calculaté // Calculaté
for (int dummy; argsit != args.end(); argsit++) { for (; argsit != args.end(); argsit++) {
if (!is_float(*argsit)) if (!is_float(*argsit))
return badarguments; return badarguments;
numarg = std::atof((*argsit).c_str()); numarg = std::atof((*argsit).c_str());
@ -380,8 +382,8 @@ std::string cmd_sleep(std::vector<std::string> args) {
if (!is_float(*argsit)) if (!is_float(*argsit))
return badarguments; return badarguments;
// Get argument // Get argument
float sleepnsecs = std::atof((*argsit).c_str()) * 1000000; float sleepsecs = std::atof((*argsit).c_str());
usleep(sleepnsecs); sleep(sleepsecs);
return success; return success;
} }
std::string cmd_argv(std::vector<std::string> args) { std::string cmd_argv(std::vector<std::string> args) {
@ -544,11 +546,11 @@ std::string execline(std::string commandstr) {
// Main CLI // Main CLI
int main(int argc, char *argv[]) { int _main(int argc, char *argv[]) {
using namespace std;
signal(SIGSEGV, sigsegv_handler); signal(SIGSEGV, sigsegv_handler);
std::string line; std::string line;
using namespace std; if (argc < 2) { // Start interactive CLI
if (argc == 1) { // Start interactive CLI
// Show copyright note // Show copyright note
cout << "pilang Copyright (C) 2020 niansa" << endl; cout << "pilang Copyright (C) 2020 niansa" << endl;
cout << "This program comes with ABSOLUTELY NO WARRANTY; for details type `warranty'." << endl; cout << "This program comes with ABSOLUTELY NO WARRANTY; for details type `warranty'." << endl;
@ -562,8 +564,14 @@ int main(int argc, char *argv[]) {
while (true) { while (true) {
line = ""; line = "";
cout << ">>> " << flush; cout << ">>> " << flush;
std::getline(std::cin, line); std::getline(readkbd("Type in command..."), line);
cout << execline(line) << endl; try {
cout << execline(line) << endl;
} catch (markergoto) { // Catch marker goto
cerr << "Unable to use markers in CLI mode!" << endl;
} catch (pilerror) {
cerr << "In CLI: " << lasterr << " (catched)" << endl;
}
} }
} else { } else {
// Read from argv[1] into vector // Read from argv[1] into vector
@ -603,4 +611,5 @@ int main(int argc, char *argv[]) {
linesit++; linesit++;
} }
} }
return 0;
} }

60
wrappers/3ds/wrapper.hh Normal file
View file

@ -0,0 +1,60 @@
#include <3ds.h>
int _main(int argc, char *argv[]);
class exitexc : public std::string {};
static int exitcode = 0;
void wait_for_gpkey(u32 key) {
u32 kDown;
while (true) {
hidScanInput();
kDown = hidKeysDown();
if (kDown & key) {
break;
}
}
}
int main(int argc, char *argv[]) {
using namespace std;
// Intialise stuff
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
// Run actual main
int res;
try {
res = _main(argc, argv);
} catch (exitexc) {
res = exitcode;
}
// Print message and wait for key to be pressed
clog << endl << "Interpreter returned " << res << endl;
clog << "Press START to exit" << flush;
wait_for_gpkey(KEY_START);
// Exit
cerr << endl << "Error: exit is not yet implemented; will reboot" << flush;
}
static SwkbdState swkbd;
static char swkbd_buf[2048];
std::stringstream readkbd(std::string hint) {
// Wait for 'A' key
wait_for_gpkey(KEY_A);
// Read input
memset(swkbd_buf, 0, sizeof(swkbd_buf));
swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 3, -1);
swkbdSetHintText(&swkbd, hint.c_str());
swkbdInputText(&swkbd, swkbd_buf, sizeof(swkbd_buf));
// Echo input
std::cout << swkbd_buf << std::endl;
// Write input to stringstream
std::stringstream out;
out << swkbd_buf;
return out;
}
void _exit(int code) {
exitcode = code;
throw exitexc();
}

View file

@ -0,0 +1,17 @@
int _main(int argc, char *argv[]);
int main(int argc, char *argv[]) {
return _main(argc, argv);
}
std::stringstream readkbd(std::string hint) {
std::string outstr;
std::getline(std::cin, outstr);
std::stringstream out;
out << outstr;
return out;
}
inline void _exit(int code) {
exit(code);
}