Skip to content

Commit

Permalink
[nx] added basic fs browser and debug monitor
Browse files Browse the repository at this point in the history
see #70
  • Loading branch information
ITotalJustice committed Apr 29, 2022
1 parent dbe5384 commit 6ebf96d
Show file tree
Hide file tree
Showing 17 changed files with 478 additions and 57 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,5 @@ gba emulator witten in c++23.
- ocornut for imgui_club <https://github.com/ocornut/imgui_club>
- everyone that has contributed to the bios decomp <https://github.com/Gericom/gba_bios>
- [ftpd](https://github.com/mtheall/ftpd) and [nxshell](https://github.com/joel16/NX-Shell) for the deko3d backend for switch.
- <a target="_blank" href="https://icons8.com/icon/38359/visual-game-boy">Visual Game Boy</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>
- <a target="_blank" href="https://icons8.com/icon/71cUHRMvCNMk/mac-folder">Mac Folder</a> icon by <a target="_blank" href="https://icons8.com">Icons8</a>
File renamed without changes
Binary file added assets/icons/icons8-mac-folder-64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/icons8-visual-game-boy-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ list(APPEND gcc_flags
-Wmissing-requires
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
# for switch build, always enable full optimisations, even in debug
if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND NOT NINTENDO_SWITCH)
list(APPEND gcc_flags -Og) # always enable otherwise debug builds would be too slow
else()
list(APPEND gcc_flags -fno-rtti -Ofast)
Expand Down
24 changes: 21 additions & 3 deletions src/frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ FetchContent_Declare(minizip
BUILD_COMMAND ""
)

FetchContent_Declare(stb
GIT_REPOSITORY https://github.com/nothings/stb.git
GIT_TAG af1a5bc352164740c1cc1354942b1c6b72eacb8a
GIT_PROGRESS TRUE
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
)

FetchContent_MakeAvailable(imgui)
FetchContent_MakeAvailable(imgui_club)
FetchContent_MakeAvailable(stb)

add_library(imgui
${imgui_SOURCE_DIR}/imgui.cpp
Expand All @@ -45,9 +54,12 @@ target_include_directories(imgui PUBLIC ${imgui_SOURCE_DIR})

# memory editor
add_library(imgui_club INTERFACE)
target_include_directories(imgui INTERFACE ${imgui_club_SOURCE_DIR}/imgui_memory_editor)
target_include_directories(imgui_club INTERFACE ${imgui_club_SOURCE_DIR}/imgui_memory_editor)
# fetch imgui is done!

add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${stb_SOURCE_DIR})

# always include backend after fetching imgui
add_subdirectory(backend)

Expand Down Expand Up @@ -134,9 +146,14 @@ target_compile_definitions(main PRIVATE
)

if (NINTENDO_SWITCH)
set(ASSETS ${CMAKE_SOURCE_DIR}/assets)

# create romfs folder
dkp_add_asset_target(main_romfs ${CMAKE_CURRENT_BINARY_DIR}/romfs)

configure_file(${ASSETS}/icons/icons8-mac-folder-64.png ${CMAKE_CURRENT_BINARY_DIR}/romfs/icons/icons8-mac-folder-64.png COPYONLY)
configure_file(${ASSETS}/icons/icons8-visual-game-boy-48.png ${CMAKE_CURRENT_BINARY_DIR}/romfs/icons/icons8-visual-game-boy-48.png COPYONLY)

# setup nacp
nx_generate_nacp(main.nacp
NAME "Notorious BEEG"
Expand All @@ -146,13 +163,13 @@ if (NINTENDO_SWITCH)

# create nro (final binary)
nx_create_nro(main
ICON ${CMAKE_SOURCE_DIR}/src/frontend/backend/nx/icon.jpg
ICON ${ASSETS}/icons/icon.jpg
NACP main.nacp
ROMFS main_romfs
)

# compile and add shaders to romfs
set(SHADER_FOLDER ${CMAKE_SOURCE_DIR}/src/frontend/backend/nx/shaders)
set(SHADER_FOLDER ${ASSETS}/shaders/nx)

nx_add_shader_program(imgui_fsh ${SHADER_FOLDER}/imgui_fsh.glsl frag)
nx_add_shader_program(imgui_vsh ${SHADER_FOLDER}/imgui_vsh.glsl vert)
Expand All @@ -163,4 +180,5 @@ if (NINTENDO_SWITCH)
imgui_fsh
imgui_vsh
)

endif()
4 changes: 4 additions & 0 deletions src/frontend/backend/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ namespace sys::backend {
auto quit() -> void;

auto poll_events() -> void;
// used for setup
auto render_begin() -> void;
// render anything specific to the backend
auto render() -> void;
// flip the screen
auto render_end() -> void;

auto get_texture(TextureID id) -> void*;
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/backend/nx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ add_library(backend
ftpd_imgui/imgui_nx.cpp

audio/audio.cpp
fs.cpp
)

target_link_libraries(backend PRIVATE imgui)
target_link_libraries(backend PRIVATE GBA)
target_link_libraries(backend PRIVATE deko3dd)
target_link_libraries(backend PRIVATE deko3d)
target_link_libraries(backend PRIVATE stb)

# why is this even needed???
target_include_directories(backend PRIVATE ${DEVKITPRO}/portlibs/switch/include)
Expand Down
32 changes: 16 additions & 16 deletions src/frontend/backend/nx/audio/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@

#include "../../../system.hpp"
#include <switch.h>
#include <switch/result.h>
#include <switch/services/audren.h>
#include <cstddef>
#include <cstdint>
#include <thread>
#include <mutex>
#include <array>
#include <vector>
#include <algorithm>
#include <ranges>
Expand Down Expand Up @@ -74,6 +73,7 @@ constexpr uint8_t sink_channels[channels]{ 0, 1 };
int sink_id;
int mem_pool_id;

// mempool that is aligned for audio
std::vector<std::uint8_t, PoolAllocator<std::uint8_t>> mem_pool;
std::size_t spec_size;

Expand All @@ -82,7 +82,7 @@ std::vector<std::int16_t> temp_buf;
std::size_t temp_buffer_index;

// this is what we copy the temp_buf into per audio frame
std::vector<AudioDriverWaveBuf> wave_buffers;
std::array<AudioDriverWaveBuf, 2> wave_buffers;
std::size_t wave_buffer_index;

std::jthread thread;
Expand All @@ -103,14 +103,8 @@ auto audio_callback(void* user, int16_t left, int16_t right) -> void

auto audio_thread(std::stop_token token) -> void
{
for (;;)
while (!token.stop_requested())
{
if (token.stop_requested())
{
printf("[INFO] stop token requested in loop!\n");
return;
}

auto& buffer = wave_buffers[wave_buffer_index];
if (buffer.state == AudioDriverWaveBufState_Free || buffer.state == AudioDriverWaveBufState_Done)
{
Expand All @@ -125,9 +119,10 @@ auto audio_thread(std::stop_token token) -> void
// stretch last sample
if (temp_buffer_index >= 2 && temp_buffer_index < temp_buf.size())
{
for (size_t i = temp_buffer_index; i < temp_buf.size(); i++)
for (size_t i = temp_buffer_index; i < temp_buf.size(); i += 2)
{
data16[i] = temp_buf[temp_buffer_index - 2];
data16[i+0] = temp_buf[temp_buffer_index - 2]; // left
data16[i+1] = temp_buf[temp_buffer_index - 1]; // right
}
}

Expand Down Expand Up @@ -165,8 +160,6 @@ namespace nx::audio {

auto init() -> bool
{
wave_buffers.resize(2);

if (auto r = audrenInitialize(&cfg); R_FAILED(r))
{
printf("failed to init audren\n");
Expand Down Expand Up @@ -216,9 +209,9 @@ auto init() -> bool
spec_size = sizeof(std::int16_t) * channels * samples;
const auto mem_pool_size = ((spec_size * wave_buffers.size()) + (AUDREN_MEMPOOL_ALIGNMENT - 1)) &~ (AUDREN_MEMPOOL_ALIGNMENT - 1);
mem_pool.resize(mem_pool_size);
// LOG("unaliged size 0x%lX aligned size: 0x%lX vector size: 0x%lX\n", spec_size * wave_buffers.size(), ((spec_size * wave_buffers.size()) + (AUDREN_MEMPOOL_ALIGNMENT - 1)) &~ (AUDREN_MEMPOOL_ALIGNMENT - 1), mem_pool.size());

for (std::size_t i = 0; i < wave_buffers.size(); ++i) {
for (std::size_t i = 0; i < wave_buffers.size(); ++i)
{
wave_buffers[i].data_adpcm = mem_pool.data();
wave_buffers[i].size = mem_pool.size();
wave_buffers[i].start_sample_offset = i * samples;
Expand All @@ -235,8 +228,15 @@ auto init() -> bool
}

wave_buffer_index = 0;

// set the buffer size
temp_buf.resize(spec_size / 2); // this is s16
std::ranges::fill(temp_buf, 0);

// start audio thread
thread = std::jthread(audio_thread);

// set callback for emu
sys::System::gameboy_advance.set_audio_callback(audio_callback);

return true;
Expand Down
Loading

0 comments on commit 6ebf96d

Please sign in to comment.