DeepSeek AI for coding with LibNX

  • Thread starter Thread starter littlesmurf
  • Start date Start date
  • Views Views 10,864
  • Replies Replies 73
  • Likes Likes 18

littlesmurf

Well-Known Member
Member
Joined
Dec 5, 2024
Messages
121
Reaction score
154
Trophies
0
Age
53
XP
281
Country
United States
For anyone wanting to write code/cross compile stuff for Nintedo Switch, DeepSeek AI just made the job about 1000 times easier. An example it gave me.

I asked it how to write a program to scan pc directories using mpt via usb from the switch to the pc. It wrote all the code and then told me how to cross compile mtp for use with libnx, here's the code it wrote for the cross compiling part:

Code:
Cross-compiling libmtp to work on the Nintendo Switch with libnx is a challenging task because libmtp is not natively supported for the Switch's architecture and operating system. However, with some effort, you can attempt to port it. Below is a step-by-step guide to cross-compile libmtp for the Nintendo Switch using the libnx environment.

Prerequisites:
DevkitPro: The development environment for Nintendo Switch homebrew.

Install DevkitPro by following the official guide: DevkitPro Installation.

libmtp Source Code: Download the source code for libmtp from its official repository: libmtp GitHub.

Basic Knowledge of Cross-Compilation: Familiarity with cross-compiling libraries for ARM architectures.

Steps to Cross-Compile libmtp for Nintendo Switch:
1. Set Up the Environment
Ensure that DevkitPro is properly installed and that the environment variables are set up. Add the following to your shell configuration file (e.g., .bashrc, .zshrc):

bash
Copy
export DEVKITPRO=/opt/devkitpro
export DEVKITARM=${DEVKITPRO}/devkitARM
export DEVKITPPC=${DEVKITPRO}/devkitPPC
export PATH=${DEVKITPRO}/tools/bin:$PATH
Reload your shell configuration:

bash
Copy
source ~/.bashrc  # or source ~/.zshrc
2. Download libmtp Source Code
Clone the libmtp repository:

bash
Copy
git clone https://github.com/libmtp/libmtp.git
cd libmtp
3. Configure the Build for Nintendo Switch
libmtp uses the GNU Autotools build system. You need to configure it for cross-compilation targeting the Nintendo Switch.

Create a toolchain.cmake file to specify the cross-compilation toolchain:

cmake
Copy
# toolchain.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER ${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc)
set(CMAKE_CXX_COMPILER ${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++)

set(CMAKE_FIND_ROOT_PATH ${DEVKITPRO}/devkitA64 ${DEVKITPRO}/portlibs/switch)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
4. Configure libmtp for Cross-Compilation
Run the following commands to configure libmtp for cross-compilation:

bash
Copy
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake \
      -DCMAKE_INSTALL_PREFIX=${DEVKITPRO}/portlibs/switch \
      -DBUILD_SHARED_LIBS=OFF \
      ..
5. Build and Install libmtp
Compile and install libmtp:

bash
Copy
make -j$(nproc)
make install
This will install libmtp into the DevkitPro portlibs directory for the Nintendo Switch.

6. Use libmtp in Your Nintendo Switch Homebrew
Once libmtp is compiled and installed, you can link it in your Nintendo Switch homebrew project. Modify your Makefile to include libmtp:

makefile
Copy
# Makefile for Nintendo Switch homebrew using libnx and libmtp

TARGET = mtp_client
BUILD = build
SOURCES = source
INCLUDES = include

# Path to libnx and libmtp
LIBNX = $(DEVKITPRO)/libnx
LIBMTP = $(DEVKITPRO)/portlibs/switch

# Compiler flags
ARCH = -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS = -g -Wall -O2 -ffunction-sections $(ARCH) -D__SWITCH__
CXXFLAGS = $(CFLAGS) -std=gnu++17
ASFLAGS = -g $(ARCH)
LDFLAGS = -specs=$(LIBNX)/switch.specs -g $(ARCH) -Wl,-Map,$(TARGET).map

# Libraries
LIBS = -lmtp -lnx

# Source files
CFILES = $(SOURCES)/main.c
OFILES = $(CFILES:.c=.o)

# Rules
all: $(TARGET).nro

$(TARGET).nro: $(TARGET).elf
    elf2nro $< $@ --icon=$(SOURCES)/icon.jpg

$(TARGET).elf: $(OFILES)
    $(CC) $(LDFLAGS) $(OFILES) $(LIBS) -o $@

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

clean:
    rm -f $(OFILES) $(TARGET).elf $(TARGET).nro $(TARGET).map
7. Write Your Nintendo Switch Homebrew Program
Use the libmtp API in your Nintendo Switch homebrew program to connect to a PC via MTP and list files/directories. Refer to the libmtp documentation for API usage: libmtp Documentation.

Challenges:
USB Stack Limitations:

The Nintendo Switch's USB stack may not fully support MTP. You may need to implement custom USB communication.

Dependencies:

libmtp depends on other libraries like libusb. You may need to cross-compile and link these dependencies as well.

Testing:

Testing MTP functionality on the Switch requires a working USB connection and a PC configured for MTP.

Alternative Approach:
If cross-compiling libmtp proves too difficult, consider using a simpler protocol like FTP or SFTP for file transfer. Libraries like libcurl are easier to work with on the Switch.

Conclusion:
Cross-compiling libmtp for the Nintendo Switch is a complex but achievable task. Follow the steps above to set up the build environment, configure libmtp for cross-compilation, and integrate it into your homebrew project. Be prepared to troubleshoot issues related to USB communication and dependencies.

This AI should greatly help anyone wanting to write homebrew for the switch.
 
It wrote all the code
It didn't.

and then told me how to cross compile mtp for use with libnx
Yeah, and did you try to do it? No? I thought so. It accurately pointed out challenges that make it impossible to work without modifying libmtp (more accurately custom USB driver), and there is nothing in that output helpful to resolve that issue

AI Bros can only write prompts, doing anything above that is too hard for them. This doesn't stop them from hyping it.

Go back to reddit or X with this bullshit. HOS is a completely different beast than well-documented and very popular big three.
 
Last edited by masagrator,
For anyone wanting to write code/cross compile stuff for Nintedo Switch, DeepSeek AI just made the job about 1000 times easier. An example it gave me.

I asked it how to write a program to scan pc directories using mpt via usb from the switch to the pc. It wrote all the code and then told me how to cross compile mtp for use with libnx, here's the code it wrote for the cross compiling part:

Code:
Cross-compiling libmtp to work on the Nintendo Switch with libnx is a challenging task because libmtp is not natively supported for the Switch's architecture and operating system. However, with some effort, you can attempt to port it. Below is a step-by-step guide to cross-compile libmtp for the Nintendo Switch using the libnx environment.

Prerequisites:
DevkitPro: The development environment for Nintendo Switch homebrew.

Install DevkitPro by following the official guide: DevkitPro Installation.

libmtp Source Code: Download the source code for libmtp from its official repository: libmtp GitHub.

Basic Knowledge of Cross-Compilation: Familiarity with cross-compiling libraries for ARM architectures.

Steps to Cross-Compile libmtp for Nintendo Switch:
1. Set Up the Environment
Ensure that DevkitPro is properly installed and that the environment variables are set up. Add the following to your shell configuration file (e.g., .bashrc, .zshrc):

bash
Copy
export DEVKITPRO=/opt/devkitpro
export DEVKITARM=${DEVKITPRO}/devkitARM
export DEVKITPPC=${DEVKITPRO}/devkitPPC
export PATH=${DEVKITPRO}/tools/bin:$PATH
Reload your shell configuration:

bash
Copy
source ~/.bashrc  # or source ~/.zshrc
2. Download libmtp Source Code
Clone the libmtp repository:

bash
Copy
git clone https://github.com/libmtp/libmtp.git
cd libmtp
3. Configure the Build for Nintendo Switch
libmtp uses the GNU Autotools build system. You need to configure it for cross-compilation targeting the Nintendo Switch.

Create a toolchain.cmake file to specify the cross-compilation toolchain:

cmake
Copy
# toolchain.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER ${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-gcc)
set(CMAKE_CXX_COMPILER ${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++)

set(CMAKE_FIND_ROOT_PATH ${DEVKITPRO}/devkitA64 ${DEVKITPRO}/portlibs/switch)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
4. Configure libmtp for Cross-Compilation
Run the following commands to configure libmtp for cross-compilation:

bash
Copy
mkdir build
cd build
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake \
      -DCMAKE_INSTALL_PREFIX=${DEVKITPRO}/portlibs/switch \
      -DBUILD_SHARED_LIBS=OFF \
      ..
5. Build and Install libmtp
Compile and install libmtp:

bash
Copy
make -j$(nproc)
make install
This will install libmtp into the DevkitPro portlibs directory for the Nintendo Switch.

6. Use libmtp in Your Nintendo Switch Homebrew
Once libmtp is compiled and installed, you can link it in your Nintendo Switch homebrew project. Modify your Makefile to include libmtp:

makefile
Copy
# Makefile for Nintendo Switch homebrew using libnx and libmtp

TARGET = mtp_client
BUILD = build
SOURCES = source
INCLUDES = include

# Path to libnx and libmtp
LIBNX = $(DEVKITPRO)/libnx
LIBMTP = $(DEVKITPRO)/portlibs/switch

# Compiler flags
ARCH = -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE
CFLAGS = -g -Wall -O2 -ffunction-sections $(ARCH) -D__SWITCH__
CXXFLAGS = $(CFLAGS) -std=gnu++17
ASFLAGS = -g $(ARCH)
LDFLAGS = -specs=$(LIBNX)/switch.specs -g $(ARCH) -Wl,-Map,$(TARGET).map

# Libraries
LIBS = -lmtp -lnx

# Source files
CFILES = $(SOURCES)/main.c
OFILES = $(CFILES:.c=.o)

# Rules
all: $(TARGET).nro

$(TARGET).nro: $(TARGET).elf
    elf2nro $< $@ --icon=$(SOURCES)/icon.jpg

$(TARGET).elf: $(OFILES)
    $(CC) $(LDFLAGS) $(OFILES) $(LIBS) -o $@

%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

clean:
    rm -f $(OFILES) $(TARGET).elf $(TARGET).nro $(TARGET).map
7. Write Your Nintendo Switch Homebrew Program
Use the libmtp API in your Nintendo Switch homebrew program to connect to a PC via MTP and list files/directories. Refer to the libmtp documentation for API usage: libmtp Documentation.

Challenges:
USB Stack Limitations:

The Nintendo Switch's USB stack may not fully support MTP. You may need to implement custom USB communication.

Dependencies:

libmtp depends on other libraries like libusb. You may need to cross-compile and link these dependencies as well.

Testing:

Testing MTP functionality on the Switch requires a working USB connection and a PC configured for MTP.

Alternative Approach:
If cross-compiling libmtp proves too difficult, consider using a simpler protocol like FTP or SFTP for file transfer. Libraries like libcurl are easier to work with on the Switch.

Conclusion:
Cross-compiling libmtp for the Nintendo Switch is a complex but achievable task. Follow the steps above to set up the build environment, configure libmtp for cross-compilation, and integrate it into your homebrew project. Be prepared to troubleshoot issues related to USB communication and dependencies.

This AI should greatly help anyone wanting to write homebrew for the switch.
Did you even try it? Yes it’s possible but it’s not that easy

Using rules files I was able to make quite a few homebrew apps using cursor and borealis to handle the fancy UI

That being said it took quite a lot to even get working and if I didn’t know anything before hand it wouldn’t be possible since I had to do a lot of manual corrections and constantly adjust the rules
 
  • Like
Reactions: Axido and n3rdin
ChatGPT kind of give me the same things. Not sure why to use a censored AI over ChatGPT
 
ChatGPT kind of give me the same things. Not sure why to use a censored AI over ChatGPT
Some AIs are better at this task than others

I can usually finish a project with claudes sonnet much faster and accurately then ChatGPT

But chatgpt is better at more generic questions and pointing me in the right direction. Like finding a better Python library or browsing a github (using uithub” just go to any repository and change the g to a u)

My entire pokerogue iOS app was made using this method. All automated with a single Python file as well that modifies the various files and setups my Xcode project and even self signs)
 
After reading that whole code box... I am genuinely confused. I mean, I'm no smarty-circuits AI or anything, but how does that make libmtp work with libnx and Switch at all? All I see is it telling you to port it over yourself and spitting out some cmake and makefile. Am I missing something?
 
After reading that whole code box... I am genuinely confused. I mean, I'm no smarty-circuits AI or anything, but how does that make libmtp work with libnx and Switch at all? All I see is it telling you to port it over yourself and spitting out some cmake and makefile. Am I missing something?
Yes, I asked it to code some stuff before that and it spat out how to do it and the code and how the code worked. It totaly aids in making a program even if you do need to make some slight mods, it can save you a lot of time and point you in the correct direction to go about making something.
 
A LLM (*) will only ever produce output that looks right according to the data it was trained on. Even if that data is absolutely correct (odds are it isn't), the output is still cobbled-together gibberish heavily weighted to look right, not to be correct.

These tools can be useful, and they do have their place, but they're being sold to everyone as a magical solution that can do anything and solve everything, and they're far from it.

(*) I dislike the term "AI" to refer to these, as I believe it has become tainted by misleading marketing, abused to the point it is now so vague it can mean anything.

Blindly trusting "AI" to write working code for you, or answer questions you don't already know the answers to, is naive at best.

And when your prompt makes wrong assumptions to begin with (hint: MTP servers and clients are very different things), then it becomes a fool's errand.
 
Yes, I asked it to code some stuff before that and it spat out how to do it and the code and how the code worked. It totaly aids in making a program even if you do need to make some slight mods, it can save you a lot of time and point you in the correct direction to go about making something.
Huh. I'll believe it when I actually see it. Every time I've asked AI for help with anything it usually results in it being very confidently wrong, or in my favorite cases, copying and pasting my own code back to me with some random unneeded comments thrown in and claiming it fixed something and made it more efficient. I guess the difference here is that I actually know what I'm looking at and why it's wrong.
 
Huh. I'll believe it when I actually see it. Every time I've asked AI for help with anything it usually results in it being very confidently wrong, or in my favorite cases, copying and pasting my own code back to me with some random unneeded comments thrown in and claiming it fixed something and made it more efficient. I guess the difference here is that I actually know what I'm looking at and why it's wrong.
Well I don't know what AI programs you have been using, I just know that from the ones that I've used DeepSeek has been the best so far for writing code.

Example question to deepseek:
Code a program for nintendo switch libnx to connect to a ftp server and then scan all directories on the ftp server

Answer:
Code:
To create a program for the Nintendo Switch using libnx that connects to an FTP server and scans all directories, we can use libcurl to handle the FTP connection and directory listing. Below is an example program that connects to an FTP server, lists the contents of the root directory, and recursively scans all subdirectories.

[HEADING=2]Prerequisites:[/HEADING]
[LIST=1]
[*][B]libnx[/B]: The Nintendo Switch homebrew SDK.
[*][B]libcurl[/B]: A library for transferring data with URLs.
[*][B]DevkitPro[/B]: The development environment for Nintendo Switch homebrew.
[/LIST]
[HEADING=2]Example Code:[/HEADING]
c
Copy
#include <switch.h>
#include <curl/curl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// Function to handle FTP directory listing
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userdata) {
// This function is called by libcurl to process the received data
// We simply print the directory listing to the console
consoleUpdate(NULL);
printf("%.*s", (int)(size * nmemb), (char*)ptr);
return size * nmemb;
}

// Function to recursively scan directories
void scan_ftp_directory(CURL *curl, const char *path) {
char url[256];
snprintf(url, sizeof(url), "ftp://%s", path);

// Set the URL for the FTP directory
curl_easy_setopt(curl, CURLOPT_URL, url);

// Set the write callback to handle directory listing
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

// Perform the FTP directory listing
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
printf("curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
 return;
 }

// Recursively scan subdirectories (this is a basic example and may need improvement)
// For a full implementation, you would need to parse the directory listing and check for subdirectories
// This example assumes that subdirectories end with a '/'
// You can use a more sophisticated approach to parse the directory listing
printf("\nScanning subdirectories in %s...\n", path);
// Note: This is a placeholder for actual directory parsing logic
}

// Function to connect to the FTP server and start scanning
void connect_and_scan_ftp(const char* ftp_url, const char* userpwd) {
CURL *curl;
CURLcode res;

curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();

if (curl) {
// Set the username and password for the FTP server
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd);

// Start scanning from the root directory
scan_ftp_directory(curl, ftp_url);

// Cleanup
curl_easy_cleanup(curl);
} else {
printf("Failed to initialize libcurl.\n");
 }

curl_global_cleanup();
}

int main(int argc, char **argv)
{
// Initialize the Switch console
consoleInit(NULL);

// Set up the FTP server URL and credentials
const char* ftp_url = "example.com"; // Replace with your FTP server URL
const char* userpwd = "username:password"; // Replace with your FTP username and password

// Connect to the FTP server and scan directories
connect_and_scan_ftp(ftp_url, userpwd);

// Main loop
while (appletMainLoop())
{
// Scan the gamepad for input
hidScanInput();

// Check if the user pressed the + button to exit
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
 break;

// Update the console, sending a new frame to the display
consoleUpdate(NULL);
 }

// Clean up and exit
consoleExit(NULL);
return 0;
}
[HEADING=2]Explanation:[/HEADING]
[LIST=1]
[*][B]libcurl Initialization[/B]: The curl_global_init and curl_easy_init functions initialize the libcurl library and create a CURL handle.
[*][B]FTP Directory Listing[/B]: The write_callback function processes the directory listing received from the FTP server and prints it to the console.
[*][B]Recursive Directory Scanning[/B]: The scan_ftp_directory function lists the contents of the current directory and recursively scans subdirectories. This example assumes subdirectories end with a /.
[*][B]Main Loop[/B]: The program enters a loop where it waits for the user to press the + button to exit.
[/LIST]
[HEADING=2]Compilation:[/HEADING]
Use the same Makefile provided in the previous example to compile this program.

[HEADING=2]Running the Program:[/HEADING]
[LIST=1]
[*]Compile the program using the Makefile.
[*]Transfer the resulting .nro file to your Nintendo Switch.
[*]Run the homebrew using a homebrew launcher like hbmenu.
[/LIST]
[HEADING=2]Notes:[/HEADING]
[LIST]
[*]Replace "example.com" and "username:password" with your actual FTP server details.
[*]This example provides a basic directory listing and scanning mechanism. For a full implementation, you would need to parse the directory listing to identify subdirectories and files accurately.
[*]Ensure that libcurl is properly set up for the Nintendo Switch environment.
[/LIST]
This program provides a starting point for connecting to an FTP server and scanning directories on the Nintendo Switch using libnx and libcurl. You can extend it further to handle file uploads, downloads, or more complex directory parsing.
[code]

That seems like a pretty good answer from an Ai, sure you might need to tweak it hear an there, but it does point you in the proper direction and make life easier.

You may not like the answer or even care to use AI, but at some point everyone else will be using it and the time to get stuff done will be drastically reduced and time = money or time to do something else.
 
This code is a bullshit. It misses the main part: actual directory scanning.
It seems you are missing the entire point of Ai. It's just a tool to help you along the way and point you in the right direction, it's not meant to do "everything" for you, it's just an aid to help you.
 
This code is a bullshit. It misses the main part: actual directory scanning.
Agreed. AI is very good at making things that look good but fail miserably. I toyed with it a bit and spent more time debugging the output code than I would have just writing it myself.
Post automatically merged:

It seems you are missing the entire point of Ai. It's just a tool to help you along the way and point you in the right direction, it's not meant to do "everything" for you, it's just an aid to help you.
So code it up and tell us how much time it took to actually make it work.
 
  • Like
Reactions: Axido
It seems you are missing the entire point of Ai. It's just a tool to help you along the way and point you in the right direction, it's not meant to do "everything" for you, it's just an aid to help you.
Feed it some really complex C++ and ask it to help you debug it. See how much it really helps you along the way and points you in the right direction.
 
  • Like
Reactions: Blythe93
Feed it some really complex C++ and ask it to help you debug it. See how much it really helps you along the way and points you in the right direction.
Why would I do that? It's just a tool in a vast array of tools.

I have a 15mm rounded spanner, sure it's good for taking 15mm nuts and bolts off in some scenarios, other times I need to use a 15mm socket instead along with a breaker bar, or sometimes I need to use an open ended spanner to do the same job when the rounded spanner is not able to help. The point is that I'd rather have more tools than less tools. Deepseek is a free tool that helps in certain scenarios and I will continue to use it and also the other tools and resources available to get the job done. Whether you like this approach or not is of off no consequence to me, you have your own way of doing things and I have mine. I am happy that this AI is open and free to use and it can help a great deal in certain case scenarios.
 
Last edited by littlesmurf,
I just love all the techbros that think that "AI" is the solution to everything.

A LLM cannot figure out any easier way to do anything. All it can ever output is stuff that has already been inputted as training data.

Next will be a thread coming up with a "softmod exploit for patched Switch units" based on some BS a LLM outputs for the prompt "patched switch exploit pls".
 
I just love all the techbros that think that "AI" is the solution to everything.

A LLM cannot figure out any easier way to do anything. All it can ever output is stuff that has already been inputted as training data.

Next will be a thread coming up with a "softmod exploit for patched Switch units" based on some BS a LLM outputs for the prompt "patched switch exploit pls".
I just did this:

Question: write an old school scrolling text demo for libnx and sdl2 for nintendo switch

Next I asked it this:
Question: write and old school 3d spinning cube in an infinate loop that bounces around the screen demo for nintendo switch using libnx and sdl

Here's the resulting source. I modded the outputted makefile to add the switch paths in my install but the source is what it gave me and it compiles and runs just fine.

For an inexperienced switch coder this tool can help very much and makes life easier for many.
 

Attachments

Last edited by littlesmurf,

Site & Scene News

Popular threads in this forum