foxygit / fake-08-xbox360 Log in
commit 7e4c15a87552b4b91158d21d5cdcfec8c7f359f3
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Feb 25 11:55:50 2021 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Wed Mar 3 20:24:17 2021 -0700

    Add basic Wii U support

    Seems to be working, but not fully tested
    Known issues:
    No sound
    + button doesnt work, use x for pause menu, y to quit
---
 .github/workflows/build.yml       |  35 ++++
 .github/workflows/buildWiiU.sh    |   8 +
 .gitignore                        |   2 +
 Makefile                          |  12 +-
 platform/wiiu/Makefile            | 131 ++++++++++++++
 platform/wiiu/source/WiiUHost.cpp | 353 ++++++++++++++++++++++++++++++++++++++
 source/main.cpp                   |   8 +-
 7 files changed, 544 insertions(+), 5 deletions(-)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index bd109c6..e8565f3 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -20,6 +20,13 @@ jobs:
       - name: Build
         run: |
           docker run -e ENABLE_COMPATIBILITY_REPORTING -v $GITHUB_WORKSPACE:/build_dir devkitpro/devkitarm /bin/bash -ex /build_dir/.github/workflows/build3ds.sh
+
+      - uses: actions/upload-artifact@v2
+        with:
+          name: Nintendo 3DS
+          path: |
+            platform/3ds/*.elf
+            platform/3ds/*.3dsx

   second:
     name: Switch
@@ -35,7 +42,35 @@ jobs:
         run: |
           docker run -e ENABLE_COMPATIBILITY_REPORTING -v $GITHUB_WORKSPACE:/build_dir devkitpro/devkita64 /bin/bash -ex /build_dir/.github/workflows/buildSwitch.sh

+      - uses: actions/upload-artifact@v2
+        with:
+          name: Nintendo Switch
+          path: |
+            platform/switch/*.elf
+            platform/switch/*.nro
+
   third:
+    name: WiiU
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+        with:
+          submodules : recursive
+
+      - name: Build
+        run: |
+          docker run -e ENABLE_COMPATIBILITY_REPORTING -v $GITHUB_WORKSPACE:/build_dir jondbell/devkitppc-wiiu-sdl2 /bin/bash -ex /build_dir/.github/workflows/buildWiiU.sh
+
+      - uses: actions/upload-artifact@v2
+        with:
+          name: Nintendo Wii U
+          path: |
+            platform/wiiu/*.elf
+            platform/wiiu/*.rpx
+
+  fourth:
     name: Tests
     runs-on: ubuntu-latest

diff --git a/.github/workflows/buildWiiU.sh b/.github/workflows/buildWiiU.sh
new file mode 100644
index 0000000..ae40464
--- /dev/null
+++ b/.github/workflows/buildWiiU.sh
@@ -0,0 +1,8 @@
+source /etc/profile.d/devkit-env.sh
+
+cd /build_dir
+
+#Build rpx
+make wiiu
+
+#todo: copy artifacts for uploading
diff --git a/.gitignore b/.gitignore
index 8b2079b..ed028b8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,8 @@
 *.nso
 *.pfs0

+*.rpx
+
 bin/
 .vscode/
 build/
diff --git a/Makefile b/Makefile
index bcbe7c8..14c9b62 100644
--- a/Makefile
+++ b/Makefile
@@ -6,11 +6,11 @@ ROOT_INCLUDES	?= include
 export SOURCES   = ../../source ../../libs/z8lua ../../libs/utf8-util ../../libs/lodepng
 export INCLUDES  = ../../include ../../libs/z8lua ../../libs/utf8-util ../../libs/lodepng

-.PHONY: all 3ds switch sdl2 clean clean-3ds clean-switch clean-sdl2
+.PHONY: all 3ds switch wiiu sdl2 clean clean-3ds clean-switch clean-wiiu clean-sdl2

-all: 3ds switch sdl2
+all: 3ds switch wiiu sdl2

-clean: clean-tests clean-3ds clean-switch clean-sdl2
+clean: clean-tests clean-3ds clean-switch clean-wiiu clean-sdl2

 clean-3ds:
 	@$(MAKE) -C platform/3ds clean
@@ -18,6 +18,9 @@ clean-3ds:
 clean-switch:
 	@$(MAKE) -C platform/switch clean

+clean-wiiu:
+	@$(MAKE) -C platform/wiiu clean
+
 clean-sdl2:
 	@$(MAKE) -C platform/SDL2 clean

@@ -30,6 +33,9 @@ cia:
 switch:
 	@$(MAKE) -C platform/switch

+wiiu:
+	@$(MAKE) -C platform/wiiu
+
 sdl2:
 	@$(MAKE) -C platform/SDL2

diff --git a/platform/wiiu/Makefile b/platform/wiiu/Makefile
new file mode 100644
index 0000000..cdfaa01
--- /dev/null
+++ b/platform/wiiu/Makefile
@@ -0,0 +1,131 @@
+#-------------------------------------------------------------------------------
+.SUFFIXES:
+#-------------------------------------------------------------------------------
+
+ifeq ($(strip $(DEVKITPRO)),)
+$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
+endif
+
+TOPDIR ?= $(CURDIR)
+include $(DEVKITPRO)/wut/share/wut_rules
+
+#-------------------------------------------------------------------------------
+# pkg-config for libraries flags
+#-------------------------------------------------------------------------------
+PKGCONF		:=	$(PORTLIBS_PATH)/wiiu/bin/powerpc-eabi-pkg-config
+
+#-------------------------------------------------------------------------------
+# 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 header files
+#-------------------------------------------------------------------------------
+TARGET		:=	FAKE-08
+BUILD		:=	build
+SOURCES		:=	${SOURCES} source
+INCLUDES	:=	${INCLUDES}
+ROMFS		:=	romfs
+LIBRARIES	:=	SDL2_gfx SDL2_image SDL2_mixer SDL2_ttf sdl2 \
+			freetype2 zlib libpng libjpeg
+
+#-------------------------------------------------------------------------------
+# options for code generation
+#-------------------------------------------------------------------------------
+CC = $(CXX)
+
+CFLAGS		:=	-g -Wall -O2 -ffunction-sections \
+			-ffast-math \
+			$(MACHDEP)
+
+CFLAGS		+=	$(INCLUDE) -D__WIIU__ -D__WUT__
+
+CFLAGS		+=	`$(PKGCONF) --cflags $(LIBRARIES)`
+
+CXXFLAGS	:=	$(CFLAGS) -std=gnu++17
+
+ASFLAGS		:=	-g $(MACHDEP)
+LDFLAGS		:=	-g $(MACHDEP) $(RPXSPECS) -Wl,-Map,$(notdir $*.map)
+
+LIBS		:=	`$(PKGCONF) --libs $(LIBRARIES)`
+LIBS		+= -lwut
+
+#-------------------------------------------------------------------------------
+# list of directories containing libraries, this must be the top level
+# containing include and lib
+#-------------------------------------------------------------------------------
+LIBDIRS		:=	$(PORTLIBS) $(WUT_ROOT)
+
+
+#-------------------------------------------------------------------------------
+# 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))
+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)))
+
+#-------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#-------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+#-------------------------------------------------------------------------------
+	export LD	:=	$(CC)
+#-------------------------------------------------------------------------------
+else
+#-------------------------------------------------------------------------------
+	export LD	:=	$(CXX)
+#-------------------------------------------------------------------------------
+endif
+#-------------------------------------------------------------------------------
+
+export SRCFILES		:=	$(CPPFILES) $(CFILES) $(SFILES)
+export OFILES		:=	$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
+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)
+
+.PHONY: $(BUILD) clean all
+
+#-------------------------------------------------------------------------------
+all: $(BUILD)
+
+$(BUILD): $(SRCFILES)
+	@[ -d $@ ] || mkdir -p $@
+	@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#-------------------------------------------------------------------------------
+clean:
+	@echo clean ...
+	@rm -fr $(BUILD) $(TARGET).rpx $(TARGET).elf
+
+#-------------------------------------------------------------------------------
+else
+.PHONY:	all
+
+DEPENDS		:=	$(OFILES:.o=.d)
+
+#-------------------------------------------------------------------------------
+# main targets
+#-------------------------------------------------------------------------------
+all		:	$(OUTPUT).rpx
+
+$(OUTPUT).rpx	:	$(OUTPUT).elf
+
+$(OUTPUT).elf	:	$(OFILES)
+
+-include $(DEPENDS)
+
+#-------------------------------------------------------------------------------
+endif
+#-------------------------------------------------------------------------------
diff --git a/platform/wiiu/source/WiiUHost.cpp b/platform/wiiu/source/WiiUHost.cpp
new file mode 100644
index 0000000..35cdb23
--- /dev/null
+++ b/platform/wiiu/source/WiiUHost.cpp
@@ -0,0 +1,353 @@
+/*
+Wii u port todo:
+(X) exit gracefully
+(X) fix input- ish - still no start
+(X) load files from sd card
+4) sound
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <fstream>
+#include <iostream>
+using namespace std;
+
+#include "../../../source/host.h"
+#include "../../../source/hostVmShared.h"
+#include "../../../source/nibblehelpers.h"
+#include "../../../source/logger.h"
+
+// sdl
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_events.h>
+
+#define SCREEN_SIZE_X 640
+#define SCREEN_SIZE_Y 640
+
+#define WIN_WIDTH 1280
+#define WIN_HEIGHT 720
+
+
+#define SAMPLERATE 22050
+#define SAMPLESPERBUF (SAMPLERATE / 30)
+#define NUM_BUFFERS 2
+
+const int __screenWidth = SCREEN_SIZE_X;
+const int __screenHeight = SCREEN_SIZE_Y;
+
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+
+StretchOption stretch;
+uint32_t last_time;
+uint32_t now_time;
+uint32_t frame_time;
+uint32_t targetFrameTimeMs;
+
+uint8_t currKDown;
+uint8_t currKHeld;
+
+Color* _paletteColors;
+
+SDL_Window* window;
+SDL_Event event;
+SDL_Renderer *renderer;
+SDL_Texture *texture = NULL;
+int quit = 0;
+void *pixels;
+uint8_t *base;
+int pitch;
+
+SDL_Rect DestR;
+
+void postFlipFunction(){
+    //flush switch frame buffers
+    // We're done rendering, so we end the frame here.
+
+
+    SDL_UnlockTexture(texture);
+    SDL_RenderCopy(renderer, texture, NULL, &DestR);
+
+    SDL_RenderPresent(renderer);
+}
+
+
+void init_fill_buffer(void *audioBuffer,size_t offset, size_t size) {
+
+	uint32_t *dest = (uint32_t*)audioBuffer;
+
+	for (size_t i=0; i<size; i++) {
+		dest[i] = 0;
+	}
+
+	//DSP_FlushDataCache(audioBuffer,size);
+
+}
+
+bool audioInitialized = false;
+uint32_t *audioBuffer;
+uint32_t audioBufferSize;
+//ndspWaveBuf waveBuf[2];
+bool fillBlock = false;
+uint32_t currPos;
+
+
+void audioCleanup(){
+    audioInitialized = false;
+
+    //ndspExit();
+
+    if(audioBuffer != nullptr) {
+        //linearFree(audioBuffer);
+        audioBuffer = nullptr;
+    }
+}
+
+void audioSetup(){
+
+}
+
+Host::Host() { }
+
+
+void Host::oneTimeSetup(Color* paletteColors){
+
+    // ----- Initialize SDL
+    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
+    {
+        fprintf(stderr, "SDL could not initialize\n");
+        quit = 1;
+        return;
+    }
+
+    //Setup window
+	window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
+	if (!window)
+    {
+        quit = 1;
+        return;
+    }
+
+	//Setup renderer
+	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+	if (!renderer)
+    {
+        quit = 1;
+        return;
+    }
+
+	texture  = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, PicoScreenWidth, PicoScreenHeight);
+	if (texture == NULL)
+    {
+		quit = 1;
+        return;
+	}
+
+    atexit(SDL_Quit);
+
+    DestR.x = WIN_WIDTH / 2 - SCREEN_SIZE_X / 2;
+    DestR.y = WIN_HEIGHT / 2 - SCREEN_SIZE_Y / 2;
+    DestR.w = SCREEN_SIZE_X;
+    DestR.h = SCREEN_SIZE_Y;
+
+    audioSetup();
+
+    for (int i = 0; i < SDL_NumJoysticks(); i++) {
+		if (SDL_JoystickOpen(i) == NULL) {
+			printf("Failed to open joystick %d!\n", i);
+			quit = 1;
+		}
+    }
+
+
+    last_time = 0;
+    now_time = 0;
+    frame_time = 0;
+    targetFrameTimeMs = 0;
+
+    _paletteColors = paletteColors;
+}
+
+void Host::oneTimeCleanup(){
+    audioCleanup();
+
+    SDL_DestroyTexture(texture);
+    SDL_DestroyRenderer(renderer);
+    SDL_DestroyWindow(window);
+    SDL_Quit();
+}
+
+void Host::setTargetFps(int targetFps){
+    targetFrameTimeMs = 1000 / targetFps;
+}
+
+void Host::changeStretch(){
+}
+
+InputState_t Host::scanInput(){
+    currKDown = 0;
+    uint8_t kUp = 0;
+    //Logger::Write("Scan input\n");
+
+
+//check input here (call open joystick):
+//https://github.com/ulquiorra-dev/Simple_SDL_Snake_WiiU_Port/blob/master/source/main.c
+//tetris calls open joystick:
+//https://github.com/ulquiorra-dev/SDL_TETRIS_WiiU_Port/blob/master/src/main.c
+
+    while (SDL_PollEvent(&event)) {
+        switch (event.type) {
+            case SDL_JOYBUTTONDOWN :
+                switch (event.jbutton.button)
+                {
+                    case SDL_CONTROLLER_BUTTON_BACK:
+                    case SDL_CONTROLLER_BUTTON_GUIDE:
+                    case SDL_CONTROLLER_BUTTON_X:
+                    case SDL_CONTROLLER_BUTTON_START:currKDown |= P8_KEY_PAUSE; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_DOWN:  currKDown |= P8_KEY_LEFT; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: currKDown |= P8_KEY_RIGHT; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_LEFT:    currKDown |= P8_KEY_UP; break;
+                    case SDL_CONTROLLER_BUTTON_MAX:  currKDown |= P8_KEY_DOWN; break;
+                    case SDL_CONTROLLER_BUTTON_A:     currKDown |= P8_KEY_X; break;
+                    case SDL_CONTROLLER_BUTTON_B:     currKDown |= P8_KEY_O; break;
+                    case SDL_CONTROLLER_BUTTON_Y: quit = 1; break;
+                }
+                break;
+
+            case SDL_JOYBUTTONUP :
+                switch (event.jbutton.button)
+                {
+                    case SDL_CONTROLLER_BUTTON_BACK:
+                    case SDL_CONTROLLER_BUTTON_GUIDE:
+                    case SDL_CONTROLLER_BUTTON_X:
+                    case SDL_CONTROLLER_BUTTON_START:kUp |= P8_KEY_PAUSE; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_DOWN:  kUp |= P8_KEY_LEFT; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: kUp |= P8_KEY_RIGHT; break;
+                    case SDL_CONTROLLER_BUTTON_DPAD_LEFT:    kUp |= P8_KEY_UP; break;
+                    case SDL_CONTROLLER_BUTTON_MAX:  kUp |= P8_KEY_DOWN; break;
+                    case SDL_CONTROLLER_BUTTON_A:     kUp |= P8_KEY_X; break;
+                    case SDL_CONTROLLER_BUTTON_B:     kUp |= P8_KEY_O; break;
+                }
+               break;
+
+            case SDL_QUIT:
+                quit = 1;
+                break;
+        }
+    }
+
+    currKHeld |= currKDown;
+    currKHeld ^= kUp;
+
+    return InputState_t {
+        currKDown,
+        currKHeld
+    };
+
+}
+
+bool Host::shouldQuit() {
+    return quit > 0;
+}
+
+void Host::waitForTargetFps(){
+    now_time = SDL_GetTicks();
+    frame_time = now_time - last_time;
+	last_time = now_time;
+
+
+	//sleep for remainder of time
+	if (frame_time < targetFrameTimeMs) {
+		uint32_t msToSleep = targetFrameTimeMs - frame_time;
+
+        SDL_Delay(msToSleep);
+
+		last_time += msToSleep;
+	}
+}
+
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap){
+    //clear screen to all black
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
+    SDL_RenderClear(renderer);
+
+    SDL_LockTexture(texture, NULL, &pixels, &pitch);
+
+    for (int y = 0; y < PicoScreenHeight; y ++){
+        for (int x = 0; x < PicoScreenWidth; x ++){
+            uint8_t c = getPixelNibble(x, y, picoFb);
+            Color col = _paletteColors[screenPaletteMap[c]];
+
+            base = ((Uint8 *)pixels) + (4 * ( y * PicoScreenHeight + x));
+            base[0] = col.Red;
+            base[1] = col.Green;
+            base[2] = col.Blue;
+            base[3] = col.Alpha;
+        }
+    }
+
+
+    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::shouldRunMainLoop(){
+
+    //check how tetris handles update loop - quit flag
+    //https://github.com/ulquiorra-dev/SDL_TETRIS_WiiU_Port/blob/master/src/main.c
+    return !quit;
+}
+
+vector<string> Host::listcarts(){
+    vector<string> carts;
+
+
+    std::string cartDir = "p8carts";
+    std::string container = "fs:/vol/external01/";
+    std::string fullCartDir = container + cartDir;
+
+    chdir(container.c_str());
+    DIR* dir = opendir(cartDir.c_str());
+    struct dirent *ent;
+    if (dir != NULL) {
+        /* print all the files and directories within directory */
+        while ((ent = readdir (dir)) != NULL) {
+            carts.push_back(fullCartDir + "/" + ent->d_name);
+        }
+        closedir (dir);
+    } else {
+        /* could not open directory */
+        perror ("");
+    }
+
+
+    return carts;
+}
diff --git a/source/main.cpp b/source/main.cpp
index bb93bb8..2df4dc3 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -37,12 +37,16 @@ int main(int argc, char* argv[])

 	Logger_Write("Turning off vm and exiting logger\n");
 	vm->CloseCart();
+	Logger_Write("deleting vm\n");
 	delete vm;
-
-	Logger_Exit();

+	Logger_Write("calling one time cleanup\n");
 	host->oneTimeCleanup();
+	Logger_Write("deleting host\n");
 	delete host;
+
+	Logger_Write("exiting logger\n");
+	Logger_Exit();

 	return 0;
 }