commit 983ea1e289d9c7106a90b8ce4734aa7c1ab243d3
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed May 20 15:47:59 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Wed May 20 15:47:59 2020 -0600
Add basic (no audio) switch version
---
.gitignore | 9 +-
platform/switch/Makefile | 193 +++++++++++++++++++++
platform/switch/source/SwitchHost.cpp | 307 ++++++++++++++++++++++++++++++++++
source/host.h | 1 +
4 files changed, 509 insertions(+), 1 deletion(-)
diff --git a/.gitignore b/.gitignore
index 7257fd2..dc76693 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,13 @@
*.3dsx
*.elf
*.smdh
+
+*.nacp
+*.nro
+*.nso
+*.pfs0
+
bin/
.vscode/
-build/
\ No newline at end of file
+build/
+
diff --git a/platform/switch/Makefile b/platform/switch/Makefile
new file mode 100644
index 0000000..f522cf1
--- /dev/null
+++ b/platform/switch/Makefile
@@ -0,0 +1,193 @@
+#---------------------------------------------------------------------------------
+.SUFFIXES:
+#---------------------------------------------------------------------------------
+
+ifeq ($(strip $(DEVKITPRO)),)
+$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
+endif
+
+TOPDIR ?= $(CURDIR)
+include $(DEVKITPRO)/libnx/switch_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
+# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm".
+# ROMFS is the directory containing data to be added to RomFS, relative to the Makefile (Optional)
+#
+# NO_ICON: if set to anything, do not use icon.
+# NO_NACP: if set to anything, no .nacp file is generated.
+# APP_TITLE is the name of the app stored in the .nacp file (Optional)
+# APP_AUTHOR is the author of the app stored in the .nacp file (Optional)
+# APP_VERSION is the version of the app stored in the .nacp file (Optional)
+# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional)
+# ICON is the filename of the icon (.jpg), relative to the project folder.
+# If not set, it attempts to use one of the following (in this order):
+# - <Project name>.jpg
+# - icon.jpg
+# - <libnx folder>/default_icon.jpg
+#---------------------------------------------------------------------------------
+TARGET := FAKE-08
+BUILD := build
+SOURCES := ${SOURCES} source
+DATA := data
+INCLUDES := ${INCLUDES}
+EXEFS_SRC := exefs_src
+#ROMFS := romfs
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
+
+CFLAGS := -g -Wall -O2 -ffunction-sections \
+ $(ARCH) $(DEFINES)
+
+CFLAGS += $(INCLUDE) -D__SWITCH__
+
+CXXFLAGS := $(CFLAGS) -fno-rtti -fexceptions -std=gnu++17
+
+ASFLAGS := -g $(ARCH)
+LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
+
+LIBS := -lnx
+
+#---------------------------------------------------------------------------------
+# list of directories containing libraries, this must be the top level containing
+# include and lib
+#---------------------------------------------------------------------------------
+LIBDIRS := $(PORTLIBS) $(LIBNX)
+
+
+#---------------------------------------------------------------------------------
+# 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,$(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)))
+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_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
+export OFILES := $(OFILES_BIN) $(OFILES_SRC)
+export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
+
+export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I$(CURDIR)/$(BUILD)
+
+export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
+
+export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)
+
+ifeq ($(strip $(ICON)),)
+ icons := $(wildcard *.jpg)
+ ifneq (,$(findstring $(TARGET).jpg,$(icons)))
+ export APP_ICON := $(TOPDIR)/$(TARGET).jpg
+ else
+ ifneq (,$(findstring icon.jpg,$(icons)))
+ export APP_ICON := $(TOPDIR)/icon.jpg
+ endif
+ endif
+else
+ export APP_ICON := $(TOPDIR)/$(ICON)
+endif
+
+ifeq ($(strip $(NO_ICON)),)
+ export NROFLAGS += --icon=$(APP_ICON)
+endif
+
+ifeq ($(strip $(NO_NACP)),)
+ export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp
+endif
+
+ifneq ($(APP_TITLEID),)
+ export NACPFLAGS += --titleid=$(APP_TITLEID)
+endif
+
+ifneq ($(ROMFS),)
+ export NROFLAGS += --romfsdir=$(CURDIR)/$(ROMFS)
+endif
+
+.PHONY: $(BUILD) clean all
+
+#---------------------------------------------------------------------------------
+all: $(BUILD)
+
+$(BUILD):
+ @[ -d $@ ] || mkdir -p $@
+ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf
+
+
+#---------------------------------------------------------------------------------
+else
+.PHONY: all
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+all : $(OUTPUT).pfs0 $(OUTPUT).nro
+
+$(OUTPUT).pfs0 : $(OUTPUT).nso
+
+$(OUTPUT).nso : $(OUTPUT).elf
+
+ifeq ($(strip $(NO_NACP)),)
+$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp
+else
+$(OUTPUT).nro : $(OUTPUT).elf
+endif
+
+$(OUTPUT).elf : $(OFILES)
+
+$(OFILES_SRC) : $(HFILES_BIN)
+
+#---------------------------------------------------------------------------------
+# you need a rule like this for each extension you use as binary data
+#---------------------------------------------------------------------------------
+%.bin.o %_bin.h : %.bin
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ @$(bin2o)
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------------
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
new file mode 100644
index 0000000..6f4f469
--- /dev/null
+++ b/platform/switch/source/SwitchHost.cpp
@@ -0,0 +1,307 @@
+#include <switch.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "../../../source/host.h"
+#include "../../../source/hostVmShared.h"
+
+#define FB_WIDTH 1280
+#define FB_HEIGHT 720
+
+#define SAMPLERATE 22050
+#define SAMPLESPERBUF (SAMPLERATE / 30)
+#define NUM_BUFFERS 2
+
+const int __screenWidth = FB_WIDTH;
+const int __screenHeight = FB_HEIGHT;
+
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+
+StretchOption stretch;
+u64 last_time;
+u64 now_time;
+u64 frame_time;
+double targetFrameTimeMs;
+
+u32 currKDown;
+u32 currKHeld;
+
+Framebuffer fb;
+
+uint8_t ConvertInputToP8(u32 input){
+ uint8_t result = 0;
+ if (input & KEY_LEFT){
+ result |= P8_KEY_LEFT;
+ }
+
+ if (input & KEY_RIGHT){
+ result |= P8_KEY_RIGHT;
+ }
+
+ if (input & KEY_UP){
+ result |= P8_KEY_UP;
+ }
+
+ if (input & KEY_DOWN){
+ result |= P8_KEY_DOWN;
+ }
+
+ if (input & KEY_B){
+ result |= P8_KEY_O;
+ }
+
+ if (input & KEY_A){
+ result |= P8_KEY_X;
+ }
+
+ if (input & KEY_PLUS){
+ result |= P8_KEY_PAUSE;
+ }
+
+ if (input & KEY_MINUS){
+ result |= P8_KEY_7;
+ }
+
+ return result;
+}
+
+void postFlipFunction(){
+ //flush switch frame buffers
+ // We're done rendering, so we end the frame here.
+ framebufferEnd(&fb);
+}
+
+
+void init_fill_buffer(void *audioBuffer,size_t offset, size_t size) {
+
+ u32 *dest = (u32*)audioBuffer;
+
+ for (size_t i=0; i<size; i++) {
+ dest[i] = 0;
+ }
+
+ //DSP_FlushDataCache(audioBuffer,size);
+
+}
+
+bool audioInitialized = false;
+u32 *audioBuffer;
+u32 audioBufferSize;
+//ndspWaveBuf waveBuf[2];
+bool fillBlock = false;
+u32 currPos;
+
+
+void audioCleanup(){
+ audioInitialized = false;
+
+ //ndspExit();
+
+ if(audioBuffer != nullptr) {
+ //linearFree(audioBuffer);
+ audioBuffer = nullptr;
+ }
+}
+
+void audioSetup(){
+ /*
+ if(R_FAILED(ndspInit())) {
+ return;
+ }
+
+ //audio setup
+ audioBufferSize = SAMPLESPERBUF * NUM_BUFFERS * sizeof(u32);
+ audioBuffer = (u32*)linearAlloc(audioBufferSize);
+ if(audioBuffer == nullptr) {
+ audioCleanup();
+ return;
+ }
+
+
+ ndspSetOutputMode(NDSP_OUTPUT_STEREO);
+
+ ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
+ ndspChnSetRate(0, SAMPLERATE);
+ ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16);
+
+ float mix[12];
+ memset(mix, 0, sizeof(mix));
+ mix[0] = 1.0;
+ mix[1] = 1.0;
+ ndspChnSetMix(0, mix);
+
+ memset(waveBuf,0,sizeof(waveBuf));
+ waveBuf[0].data_vaddr = &audioBuffer[0];
+ waveBuf[0].nsamples = SAMPLESPERBUF;
+ waveBuf[1].data_vaddr = &audioBuffer[SAMPLESPERBUF];
+ waveBuf[1].nsamples = SAMPLESPERBUF;
+
+
+ size_t stream_offset = 0;
+
+ //not sure if this is necessary? if it is, memset might be better?
+ init_fill_buffer(audioBuffer,stream_offset, SAMPLESPERBUF * 2);
+
+ stream_offset += SAMPLESPERBUF;
+
+ ndspChnWaveBufAdd(0, &waveBuf[0]);
+ ndspChnWaveBufAdd(0, &waveBuf[1]);
+
+ audioInitialized = true;
+ */
+}
+
+
+
+Host::Host() { }
+
+
+void Host::oneTimeSetup(){
+
+ audioSetup();
+
+ NWindow* win = nwindowGetDefault();
+
+ framebufferCreate(&fb, win, FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
+ framebufferMakeLinear(&fb);
+
+ last_time = 0;
+ now_time = 0;
+ frame_time = 0;
+ targetFrameTimeMs = 0;
+}
+
+void Host::oneTimeCleanup(){
+ audioCleanup();
+
+ framebufferClose(&fb);
+}
+
+void Host::setTargetFps(uint8_t targetFps){
+ targetFrameTimeMs = 1000.0 / (double)targetFps;
+}
+
+void Host::changeStretch(){
+ if (currKDown & KEY_R) {
+ if (stretch == PixelPerfect) {
+ stretch = PixelPerfectStretch;
+ }
+ else if (stretch == PixelPerfectStretch) {
+ stretch = PixelPerfect;
+ }
+ }
+}
+
+void Host::scanInput(){
+ hidScanInput();
+
+ currKDown = hidKeysDown(CONTROLLER_P1_AUTO);
+ currKHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
+}
+
+uint8_t Host::getKeysDown(){
+ return ConvertInputToP8(currKDown);
+}
+
+uint8_t Host::getKeysHeld(){
+ return ConvertInputToP8(currKHeld);
+}
+
+
+bool Host::shouldQuit() {
+ bool lpressed = currKHeld & KEY_L;
+ bool rpressed = currKDown & KEY_R;
+
+ return lpressed && rpressed;
+}
+
+
+void Host::waitForTargetFps(){
+ now_time = svcGetSystemTick();
+ frame_time = now_time - last_time;
+ last_time = now_time;
+
+ //not sure how to do this on switch?
+ /*
+ double frameTimeMs = frame_time / CPU_TICKS_PER_MSEC;
+
+ //sleep for remainder of time
+ if (frameTimeMs < targetFrameTimeMs) {
+ double msToSleep = targetFrameTimeMs - frameTimeMs;
+
+ svcSleepThread(msToSleep * 1000 * 1000);
+
+ last_time += CPU_TICKS_PER_MSEC * msToSleep;
+ }
+ */
+}
+
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors){
+ u32 stride;
+ u32* framebuf = (u32*) framebufferBegin(&fb, &stride);
+
+ //clear frame buf
+ memset(framebuf, 0, __screenHeight*__screenWidth*4);
+
+
+ u32 renderWidth = PicoScreenWidth;
+ u32 renderHeight = PicoScreenHeight;
+ u32 ratio = 1;
+
+ if (stretch == PixelPerfectStretch){
+ ratio = FB_HEIGHT / PicoScreenHeight; //should be 5
+ renderWidth = PicoScreenWidth * ratio;
+ renderHeight = PicoScreenHeight * ratio;
+ }
+
+ u32 xOffset = (FB_WIDTH - renderWidth) / 2;
+ u32 yOffset = (FB_HEIGHT - renderHeight) / 2;
+
+ for (u32 y = 0; y < renderHeight; y ++)
+ {
+ for (u32 x = 0; x < renderWidth; x ++)
+ {
+ int picoX = (int)(x / ratio);
+ int picoY = (int)(y / ratio);
+ uint8_t c = picoFb[picoX*128 + picoY];
+ //uint8_t c = picoFb[x*128 + y];
+ Color col = paletteColors[screenPaletteMap[c]];
+
+ u32 pos = (yOffset + y) * stride / sizeof(u32) + (xOffset + x);
+ framebuf[pos] = RGBA8_MAXALPHA(col.Red, col.Green, col.Blue);
+
+ }
+ }
+
+ postFlipFunction();
+}
+
+bool Host::shouldFillAudioBuff(){
+ //return waveBuf[fillBlock].status == NDSP_WBUF_DONE;
+ return false;
+}
+
+void* Host::getAudioBufferPointer(){
+ //return waveBuf[fillBlock].data_pcm16;
+ return nullptr;
+}
+
+size_t Host::getAudioBufferSize(){
+ //return waveBuf[fillBlock].nsamples;
+ return 0;
+}
+
+void Host::playFilledAudioBuffer(){
+ //DSP_FlushDataCache(waveBuf[fillBlock].data_pcm16, waveBuf[fillBlock].nsamples);
+
+ //ndspChnWaveBufAdd(0, &waveBuf[fillBlock]);
+
+ fillBlock = !fillBlock;
+}
+
+bool Host::mainLoop(){
+ return appletMainLoop();
+}
\ No newline at end of file
diff --git a/source/host.h b/source/host.h
index aecd53d..545be13 100644
--- a/source/host.h
+++ b/source/host.h
@@ -5,6 +5,7 @@
enum StretchOption {
PixelPerfect,
+ PixelPerfectStretch,
StretchToFit,
StretchAndOverflow
};