From c772876a24af407f256e7d6e93d0cdc0f0ae97ba Mon Sep 17 00:00:00 2001 From: niansa Date: Wed, 27 May 2020 13:20:23 +0200 Subject: [PATCH] Added 3ds target --- .gitignore | 7 +- Makefile | 38 ++--- Makefile-3ds | 255 +++++++++++++++++++++++++++++++++ Makefile-generic | 20 +++ main.cpp => source/pilang2.cpp | 37 +++-- wrappers/3ds/wrapper.hh | 60 ++++++++ wrappers/generic/wrapper.hh | 17 +++ 7 files changed, 402 insertions(+), 32 deletions(-) create mode 100644 Makefile-3ds create mode 100644 Makefile-generic rename main.cpp => source/pilang2.cpp (95%) create mode 100644 wrappers/3ds/wrapper.hh create mode 100644 wrappers/generic/wrapper.hh diff --git a/.gitignore b/.gitignore index 8d4a6c0..c189660 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,7 @@ bin -obj \ No newline at end of file +obj-generic +obj-3ds +pilang2 +*.smdh +*.elf +*.3dsx diff --git a/Makefile b/Makefile index 50862e2..9742019 100644 --- a/Makefile +++ b/Makefile @@ -1,21 +1,25 @@ -CXX=g++ -CC=gcc +all: generic -CXXFLAGS=-Wextra -Os -std=c++17 -LDFLAGS=-Wextra +generic: + @mv Makefile Makefile.bak + @mv Makefile-generic Makefile + @make | cat + @mv Makefile Makefile-generic | cat + @mv Makefile.bak Makefile | cat -all: obj pilang - -obj: - mkdir -p obj - -pilang: obj/main.o - $(CXX) -o $@ $^ $(CXXFLAGS) $(LDFLAGS) - -obj/main.o: main.cpp - $(CXX) -c $^ -o $@ $(CXXFLAGS) +3ds: + @mv Makefile Makefile.bak + @mv Makefile-3ds Makefile + @make | cat + @mv Makefile Makefile-3ds | cat + @mv Makefile.bak Makefile | cat clean: - rm -Rf ./obj/ - rm -f ./pilang - + @mv Makefile Makefile.bak + @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 diff --git a/Makefile-3ds b/Makefile-3ds new file mode 100644 index 0000000..10e05e6 --- /dev/null +++ b/Makefile-3ds @@ -0,0 +1,255 @@ +#--------------------------------------------------------------------------------- +.SUFFIXES: +#--------------------------------------------------------------------------------- + +ifeq ($(strip $(DEVKITARM)),) +$(error "Please set DEVKITARM in your environment. export DEVKITARM=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): +# - .png +# - icon.png +# - /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 +#--------------------------------------------------------------------------------------- diff --git a/Makefile-generic b/Makefile-generic new file mode 100644 index 0000000..30c408f --- /dev/null +++ b/Makefile-generic @@ -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 diff --git a/main.cpp b/source/pilang2.cpp similarity index 95% rename from main.cpp rename to source/pilang2.cpp index 9d58a2c..d40ef87 100644 --- a/main.cpp +++ b/source/pilang2.cpp @@ -27,10 +27,10 @@ along with pilang. If not, see . #include #include #include -#include -#include #include +#include + // Return type defs std::string success = "Success"; @@ -112,6 +112,7 @@ std::string get_rstring(std::string rstring) { return rstring; } + // Builtin functions std::string cmd_ret(std::vector args) { std::string retstring; @@ -163,7 +164,7 @@ std::string cmd_exit(std::vector args) { } std::string exitcode = *args.begin(); if (is_float(exitcode)) - exit(std::stoi(exitcode)); + _exit(std::stoi(exitcode)); else return badarguments; } @@ -198,9 +199,10 @@ std::string cmd_print(std::vector args) { return result; } std::string cmd_input(std::vector args) { - cmd_printnnl(args); + std::string hint = cmd_ret(args); + cmd_printnnl({hint}); std::string result; - std::getline(std::cin, result); + std::getline(readkbd(hint), result); return result; } std::string cmd_set(std::vector args) { @@ -227,7 +229,7 @@ std::string cmd_append(std::vector args) { auto argsit = args.begin(); std::string variablename = *argsit; argsit++; - for (int dummy; argsit != args.end(); argsit++) + for (; argsit != args.end(); argsit++) variables[variablename].append(*argsit); return success; } @@ -355,7 +357,7 @@ std::string cmd_addsub(bool type, std::vector args) { float result = std::atof((*argsit).c_str()); argsit++; // Calculaté - for (int dummy; argsit != args.end(); argsit++) { + for (; argsit != args.end(); argsit++) { if (!is_float(*argsit)) return badarguments; numarg = std::atof((*argsit).c_str()); @@ -380,8 +382,8 @@ std::string cmd_sleep(std::vector args) { if (!is_float(*argsit)) return badarguments; // Get argument - float sleepnsecs = std::atof((*argsit).c_str()) * 1000000; - usleep(sleepnsecs); + float sleepsecs = std::atof((*argsit).c_str()); + sleep(sleepsecs); return success; } std::string cmd_argv(std::vector args) { @@ -544,11 +546,11 @@ std::string execline(std::string commandstr) { // Main CLI -int main(int argc, char *argv[]) { +int _main(int argc, char *argv[]) { + using namespace std; signal(SIGSEGV, sigsegv_handler); std::string line; - using namespace std; - if (argc == 1) { // Start interactive CLI + if (argc < 2) { // Start interactive CLI // Show copyright note cout << "pilang Copyright (C) 2020 niansa" << 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) { line = ""; cout << ">>> " << flush; - std::getline(std::cin, line); - cout << execline(line) << endl; + std::getline(readkbd("Type in command..."), line); + 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 { // Read from argv[1] into vector @@ -603,4 +611,5 @@ int main(int argc, char *argv[]) { linesit++; } } + return 0; } diff --git a/wrappers/3ds/wrapper.hh b/wrappers/3ds/wrapper.hh new file mode 100644 index 0000000..3a9a481 --- /dev/null +++ b/wrappers/3ds/wrapper.hh @@ -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(); +} diff --git a/wrappers/generic/wrapper.hh b/wrappers/generic/wrapper.hh new file mode 100644 index 0000000..a302ce7 --- /dev/null +++ b/wrappers/generic/wrapper.hh @@ -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); +}