Script for loading cartridges from usb cart dumpers

Discussion of developmental aspects of the MiSTer Project.
t9999clint
Posts: 11
Joined: Tue Sep 22, 2020 7:09 am
Has thanked: 2 times
Been thanked: 2 times

Script for loading cartridges from usb cart dumpers

Unread post by t9999clint »

I want to make a simple script to automatically detect the Retrode, dump the rom and the ram save from the physical cart, then launch the appropriate core for the type of rom that was dumped.
I will also make a matching script to later detect the retrode and save the save rom back to the cart.

I'm kind of starting from scratch here, is there any documentation on what syntax I should be using for controlling/launching cores?
Is it even possible to start a core from a .sh script?

Thanks for your help.
zakk4223
Posts: 287
Joined: Sun May 24, 2020 10:55 pm
Been thanked: 119 times

Re: Questions for script writing, Retrode 2 support

Unread post by zakk4223 »

You can load an rbf or an mra from the command line via /dev/MiSTer_cmd

echo "load_core _Console/SNES_20201106.rbf" > /dev/MiSTer_cmd will load that core.

Unfortunately you can't load a rom this way.

Honestly it doesn't look too hard to add a way to do that, but the script writer will have to know what the proper 'rom index' is.
t9999clint
Posts: 11
Joined: Tue Sep 22, 2020 7:09 am
Has thanked: 2 times
Been thanked: 2 times

Re: Questions for script writing, Retrode 2 support

Unread post by t9999clint »

ok thanks. It sucks that you can't tell it to load a core, but it'll still work ok for what I'm planning.
It won't be 100% automatic, but it'll be good as a proof of concept at least.

And once I get that working I'll try and see why the MiSTer isn't detecting the controllers on the retrode2. Hopefully it's just user error.
Yim
Posts: 126
Joined: Mon Jan 18, 2021 11:51 pm
Has thanked: 3 times
Been thanked: 87 times

Re: Questions for script writing, Retrode 2 support

Unread post by Yim »

t9999clint wrote: Tue Jan 05, 2021 5:27 pmwhy the MiSTer isn't detecting the controllers on the retrode2.
Bit of a late response here, but I was able to use a mega drive controller on MiSTer through my retrode2, so it is possible. I think I just plugged it in and used the define function in the OSD, no particular tricks to get it going.
bry111
Posts: 26
Joined: Tue Jul 21, 2020 12:31 am
Has thanked: 1 time
Been thanked: 2 times

Re: Questions for script writing, Retrode 2 support

Unread post by bry111 »

Here is an even later response. A long time ago (years) the MiSTer never worked with Retrode 2 controllers. Now, for me at least it sometimes works. So that's an improvement.

I've never gotten it to work with just an on-the-go adapter cable. I've only gotten it to work with a powered USB hub (don't have the official MiSTer hub, I use a 10-ish year old powered USB2 hub with an on-the-go adapter which hopefully isn't the source of my trouble).

When it does work, the LED on the front of the Retrode lights up solid for about 50 seconds, then blinks a bit, then dims and only lights up when a controller button is pressed and only then do controller inputs work for me. Not sure why it takes so long, maybe it's looking for something in the cartridge slot? But when the Retrode is plugged into a PC it's much quicker than 50 seconds, maybe more like 10.

When it doesn't work, generally the Retrode LED doesn't light up at all. Sometimes plugging in a different device can trigger the 50 second light up on the Retrode, I've had the most luck plugging in a PS3 controller, sometimes a Daemonbite arcade stick does it too. Does not seem to be affected by a keyboard. But maybe this suggests a problem with my hub?

In any case to use more than one controller I think the Retrode should be set to 4 joystick + mouse mode (edit RETRODE.CFG on the mass storage volume exposed by the Retrode, set HIDMode to 1, careful editing the file because most text editors try to save temporary files on write which will not work, may need to copy it elsewhere then cat back an edited copy). I haven't tried 2 joystick mode with MiSTer but on a PC it actually appears as 1 joystick with two axes and extra buttons. Keyboard mode might be fine with only one player, unless MiSTer supports sharing one keyboard with multiple players?

Also to actually use multiple controllers with MiSTer I had to edit MiSTer.ini and add the following:

Code: Select all

no_merge_vidpid=0x040397c1
Without the ini change, all 4 controllers behaved as if they were the same controller. (Unsure if the newer DragonBox-manufactured Retrodes have the same VID/PID.)

Button mapping is shared between all 4 controllers, remapping one changes the others. Which can be a bit strange with a mix of SNES and Genesis controllers; mapping the SNES controller normally will result in the Genesis controllers being mapped similarly to how Nintendo mapped Genesis 6 button controllers to a Classic Controller on the Wii VC, but in reverse: Genesis A,B,C,X,Y,Z is mapped to SNES Y,B,A,L,X,R respectively. Remapping the Genesis controller in MiSTer to anything else will make the SNES controller mapping weird.

Unsure about controller latency. It seems OK? I guess? At some point I'd like to try something more formal as per https://rpubs.com/misteraddons/inputlatency but maybe I should just build some Daemonbite adapters...

The above is with MiSTer Main 20210917, Retrode 2 firmware 0.23a.

As per the original idea in this thread, while it sounds super cool to plug in a cartridge and play it on MiSTer, the Retrode's slow cartridge read speed and general flakiness (eg. it seems I have to clean cartridges a lot more thoroughly to get a good dump than would be required for use in an original console) means it'd be pretty impractical. But if you own a cartridge ROM dumper you probably don't mind things that are impractical. :) It looks like people have figured out a few things to load ROMs programmatically, unsure how well it works but see the following links:
jayp76
Posts: 30
Joined: Mon May 25, 2020 8:30 am
Has thanked: 1 time
Been thanked: 3 times

Re: Questions for script writing, Retrode 2 support

Unread post by jayp76 »

For documentation purpose, because it is asked many times.
Sources:
http://forum.retrode.org/index.php?topic=306.0
https://www.atari-forum.com/viewtopic.p ... de#p372033

Raspberry Pi Slowness Issue solved
« on: 06/Sep/2015 11:07:17 PM »
So, while working on the RetroPi-Setup, I finally found the reason for the slowness (50kb/s) when transferring games.

It's actually a common Raspberry Pi issue: When connecting a USB 1.1 device to the 2.0 hub of the Pi, there are packet drops... which result in the slow speed.

Forcing the Raspberry Pi to run in 1.1 mode fixes the slowness issue!
Of course, 2.0 devices will also run slower then (so USB Sticks, harddisks, etc.), but the Retrode runs full speed then.

To do that, add the following kernel parameter to the boot-script:

dwc_otg.speed=1

(Add that into /boot/cmdline.txt)

That's it :)
bry111
Posts: 26
Joined: Tue Jul 21, 2020 12:31 am
Has thanked: 1 time
Been thanked: 2 times

Re: Questions for script writing, Retrode 2 support

Unread post by bry111 »

For reference, I tried adding dwc_otg.speed=1 to linux/u-boot.txt and it didn't seem to make any difference. I have not attempted the u-boot command prompt suggested by Sorgelig in the atari-forum thread from a few years ago.

Using the NES lag tester core I was able to get the following results (count/avg/max/min/stdev, all appearing as joystick ID 0403:97c1):

Single SNES controller, SNS-005:
3114/7.989/13.85/1.75/2.582

Single 3-button Genesis controller, MK-1650:
3254/7.952/14.19/2/2.6

Single 6-button Genesis controller, MK-1470 (no, not that MK-1470, this one):
3100/8.039/14.21/2.03/2.534

(I assume multiple controllers would not make it better but did not test this.)

So, about 8ms average latency, much of the time about 5.5ms-10.5ms, occasionally up to about 14ms. Could be a lot worse, but there are much better options these days, and at least for the purpose of using as a controller adapter I don't think it's worth pursuing the USB 1.1/2.0 issue further.
User avatar
amstan
Posts: 56
Joined: Sat Feb 12, 2022 8:08 am
Location: Mountain View, California
Has thanked: 5 times
Been thanked: 26 times
Contact:

Re: Questions for script writing, Retrode 2 support

Unread post by amstan »

bry111 wrote: Sun Feb 13, 2022 9:15 pm dwc_otg.speed=1 to linux/u-boot.txt
It's not that easy, the raspberry pi uses the homegrown, out of tree, dwc_otg usb driver instead of the upstream dwc2 driver found. It has extra hacks (like processing IRQs in the FIQ) to deal with the raspberrypiness of it (the low cpu power on the rpi1 comes to mind) (and now that I think about it, maybe some of those requirements would apply to our situation too).

Source: I know this because my coworker and I worked on this driver for rk3288 chromebooks (they also use this usb controller).

Mister kernel uses `CONFIG_USB_DWC2=y`, at least according to my arch config. So that's a totally different driver from the rpi. Compare our config with a rasberrypi config
jayp76 wrote: Wed Oct 20, 2021 12:15 pm It's actually a common Raspberry Pi issue: When connecting a USB 1.1 device to the 2.0 hub of the Pi, there are packet drops... which result in the slow speed.
And that's where the differences between the drivers might start. My coworker's packet scheduler (which mister's kernel runs) work might actually improve a lot for USB 1.1 devices on a 2.0 hub scenario. You might be seeing another bug unfortunatelly.
t9999clint
Posts: 11
Joined: Tue Sep 22, 2020 7:09 am
Has thanked: 2 times
Been thanked: 2 times

Re: Questions for script writing, Retrode 2 support

Unread post by t9999clint »

hmm... looks like a bunch of work will be required for the controllers too.
Also they finally started working on directly launching a rom from a command so that should make working on this script much easier.

I seen some other people working on simular stuff so the idea has some merrit. RMC did a video where he scanned a UPC code and it launched a matching rom from his collection. I should look at his code and see what tricks he did to do that.

Basic process that I have a plan for is this...

Have a Cartridge reader program either as a core option or as a script in the scripts menu (gonna do the script way first to make things more simple)
When the user selects the script it'll do the following...

1. scan for a compatible cart reader (I only have a retrode 2, but there's a USB2NES thingy that's pretty simular that should also work)
2. Mount the filesystem if it's not already mounted
3. Check if the ROM has already been dumped, if not dump it. (show progress if possible)
4. if ROM is already dumped and if save data exists, ask if should be copied over the current copy.
5. Clean up any temp files and if a filesystem was mounted in step 2, unmount it.
6. Launch apropriate core for the ROM detected. Ask for core if unsure
7. if possible, after the loaded core is done running, ask if save should be uploaded to cart. (this is probably not possible)

Also there should also be a utility to download/upload the save data to/from the cart.

I'm pretty bad at looking for this stuff, but if anyone can link to some documentation on the command syntax for launching a core with a certain rom file that would help a lot. (this alone would make this script take hours of work to finish instead of days)
*edit* Closest I could find was this, https://github.com/MiSTer-devel/Main_Mi ... -MGL-files and using those lists would be a very hacky way to get it done.
t9999clint
Posts: 11
Joined: Tue Sep 22, 2020 7:09 am
Has thanked: 2 times
Been thanked: 2 times

Re: Questions for script writing, Retrode 2 support

Unread post by t9999clint »

oh shit, I just need to cat out a mgl file and send this command to MiSTer_cmd...

Code: Select all

echo "load_core /media/fat/mygame.mgl" > /dev/MiSTer_cmd
This is actually possible now without any weird hacky workarounds. (I wan't clean understandable code)

Now that it's relatively easy to script out, I might actually start this project. (I'm not really a programmer)

I wonder what I should name it... The MisterKin 5, the PolyMiSTer? Analogue's Nighmare?

nah, I shouldn't be edgy like that. I'll probably just name it Dump n Load or something dumb like that.
grocarlito
Posts: 3
Joined: Sun Oct 08, 2023 3:56 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Script for loading cartridges from usb cart dumpers

Unread post by grocarlito »

Hi,

Is there a script that is now working ?

Any luck that it would load the game automatically like the Nfc reader does ?

I know the people are not very interested with this solution but I have a child and navigating through menus to load a game is not the experience I want her to have.

Moreover I have a set of NES cartridges and I want her to use them ^^

grocarlito
Posts: 3
Joined: Sun Oct 08, 2023 3:56 pm
Has thanked: 1 time
Been thanked: 1 time

Re: Script for loading cartridges from usb cart dumpers

Unread post by grocarlito »

Hi again,

I finally managed to automatically load a rom when a usb storage is connected.

This is a POC, and it might be tuned with parameters etc.

Here's the technique:

With

Code: Select all

usedev

, I created the following rule:
/etc/usedev/rules.d/10-cartridge-add.rule

Code: Select all

SUBSYSTEM=="block", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/media/fat/Scripts/cartridges_async.sh"

And 2 scripts comes along (Since there is no systemd available):

/media/fat/Scripts/cartridges_async.sh

Code: Select all

#!/bin/bash
/media/fat/Scripts/cartridges.sh &

/media/fat/Scripts/cartridges.sh

Code: Select all

#!/bin/bash
exec > /tmp/cartridges.log 2>&1

usb0_directory=/media/usb0

echo "Detecting nes cartridge in usb0..."

while :
do
        for entry in "$usb0_directory"/*;
        do
                if [[ $entry == rom.nes ]]
                then
                        echo "Loading nes cartridge ${entry}..."
                        echo "load_core /media/fat/cartridges/usbnes.mgl" > /dev/MiSTer_cmd
                        exit;
                else
                        echo "Ignoring file ${entry}."
                fi
        done
sleep 1
done

As you can see I iterate until I find my "rom.nes", because I don't know why but the first loop, the folder contains just a "." file, the second loop it contains the "rom.nes".

And the mgl file:
/media/fat/cartridges/usbnes.mgl

Code: Select all

<mistergamedescription>
    <rbf>_console/nes</rbf>
    <file delay="2" type="f" index="0" path="/media/usb0/rom.nes"/>
</mistergamedescription>
Post Reply