Teensy 3.1 Running a Program off a SD card

Status
Not open for further replies.

thallazar

New member
Hey.
I want to make a few old arcade games using an lcd screen and teensy. I've already worked one project remaking scorched earth and have plans on making snake and a few others. Scorched earth was already taking up quite a large chunk of the memory so ideally I thought, I'd want to be able to run the game off some removable memory like SD card or flash drive. Is it possible to run a hex file off an sd card and if so, where would I go about looking at how to do something like that?
Thanks for any help you guys can provide.
 
You can also try to load any big pieces of data on the SD card. So arrays, etc, that don't need to be accessed as fast.
 
Can instructions execute from memory? Or do instructions execute from flash? I would think Arm can execute from memory. Android phones do it all the time.
 
Can instructions execute from memory? Or do instructions execute from flash? I would think Arm can execute from memory. Android phones do it all the time.

Do you mean can you run code that's in RAM? If so, then yes you can do that. I believe that's essentially how the Teensy 3.x bootloader works.

Based on what I know of the assumptions the default compiler uses, my guess is you won't be able to use any of Teensyduino or the Arduino libraries without modification, and I doubt you'd be able to use the Arduino IDE at all.

But still yes, entirely possible.
 
Yes, Potatotron is correct, the processor can execute code from RAM.

In fact, from normal Arduino usage, you can add "FASTRUN" in front of a function and it will be loaded in RAM, which (sometimes) runs faster than having the same code in flash.

To load code from media like a SD card, you'd need to go to quite a lot of trouble to create that code, since the normal build process creates code for the flash. You'd need to edit the linker file, so the compiler creates the code for the RAM address, rather than flash. You code will presumably need both static and stack based variables. You could create your linker script to just put your static variables right after your code, in the same block of RAM. You could even simplify the startup routines at the beginning of your code, since it wouldn't need to copy static initialization from flash to ram. Presumably, you'd use the already-established stack, set up by the code running from flash.

You'll probably need to edit the normal linker script too, to make a big chunk of the RAM with a fixed starting address available. Then you could have the flash-based code simply copy the binary data from a file on the SD card to the big RAM buffer.

Actually calling the RAM-based code from the flash-based code requires some tricky C syntax. Here's an example from eeprom.c, which calls a function at the address "do_flash_cmd" and passes a pointer to it.

Code:
                // do_flash_cmd() must execute from RAM.  Luckily the C syntax is simple...
                (*((void (*)(volatile uint8_t *))((uint32_t)do_flash_cmd | 1)))(&FTFL_FSTAT);

Those are the easy parts.

Achieving any sort of integration between the RAM-based code and flash based code will make the arcane syntax of linker scripts and C function pointer type casting look like a walk in the park!

Using interrupts from the RAM-based code will be the first huge challenge. Probably the simplest way would be to just create a new vector table in the RAM image and use it.

But if you want to actually use any of the code in flash, you're going to have to figure out some way for your RAM-based program to know where the flash-based code allocated stuff in the _other_ RAM that's not dedicated to your program. I can think of some slow schemes, like creating tables of pointers, where the linker script places the table at a fixed location. But that's slow and wasteful. How to achieve any sort of good integration, where the RAM-based code can simply use the resource from the flash-based code is beyond my technical skills.
 
Really, I'm wondering what "Scorched earth was already taking up quite a large chunk of the memory" really means? How much memory?
 
@PaulStoffregen,
It sounds like you could have a flash based "OS" or BIOS all written in regular Arduino IDE. Then you could have "programs" that are launched after loaded into RAM by the OS. So it should be possible to have a game engine like OS on the Teensy 3.1 and load in games from SD card. However, the games would need to be compiled differently, and they would need to be able to find OS functions if code was shared with the OS. Or the OS could be more like a BIOS and just loads a selected game and basically the game takes over the hardware more like a console game does.

Although, wouldn't it just be more efficient to use something like the Raspberry Pi for this? It already is designed for this sort of application.

It would be a cool project to see someone build anyway. Could make for some interesting projects besides games. Updating code could be as simple as swapping an SD card.
 
Status
Not open for further replies.
Back
Top