Load and run external program - Page 1
Forum section: electronics / general

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 18, 2019, 01:28:03 pm
content: Hi, i have a question out of curiousity :What special option do i need in a MCU to load a program from USB stick ?I am intrested in how a game computer works.Suppose there is USB or cartridge support for loading a game + LCD or HDMI outWhat is the option needed to play the game ?btw : i am into PIC chips if that is possible.thanks

author: rstofer
author_rank: Super Contributor
timestamp: August 18, 2019, 02:03:20 pm
content: You need an MCU with USB Host capability and you need some boot loader code to a) attach a Thumb Drive as a Mass Storage Device (MSD) and b) read the MSD and (usually) program the MCU flash. Some MCUs will run the code out of RAM. This is good and bad: You don't have to program a flash but you also lose the program on power failure. If the Thumb Drive is not attached, the MCU won't reboot.It would be FAR easier to write the boot code for an SD card or Compact Flash card. I prefer the Compact Flash because for reasons of simplicity (it looks like a HDD) and speed (parallel data transfers, DMA capability, etc).

author: ledtester
author_rank: Super Contributor
timestamp: August 18, 2019, 02:08:39 pm
content: Interacting with a USB stick probably will be a challenge for a simple microcontroller mainly due to the complexity of the USB protocol. However, reading and writing to and from an SD card is very simple as it can be done via SPI.Here's a thread which discusses suitable PICs and libraries to use with an SD card:https://www.microchip.com/forums/m980804.aspxAs for as running loaded code, from this thread it appears that you'll have to use a PIC32 if you want to run the code from RAM:https://electronics.stackexchange.com/questions/5386/execute-instructions-from-ram-in-a-hobbyist-friendy-microcontroller/Otherwise you can re-program your flash area on the fly as this answer suggests:https://electronics.stackexchange.com/a/5408/95488Note that flash memory has a limited number of write/erase cycles which may or may not be a problem depending on how often you load code.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 18, 2019, 02:28:19 pm
content: You'd need an MCU with USB host capabilities, usually if it has USB OTG, that's doable. Select one with existing/demo code for handling USB mass storage, because implementing that from scratch is no picnic.As for running an external "program", you have to define exactly what you want to do. Do you want to be able to directly execute machine code? In that case, you need an MCU which can run code from RAM (I really wouldn't suggest the approach in which you would program it in Flash first - especially if it's going to happen often - MCU Flash has limited programming cycles and it would also take a while depending on code size...) The number of programming cycles may not be a huge issue depending on your use cases (usually ~10000 cycles), but the time it would take AND the risk of fucking something up while flashing may not be worth it if it's done as part of regular functioning and not just for updating reasons...One approach if the "external programs" don't have to be machine code directly: use some kind of interpreted language. Embed the interpreter in the MCU Flash. Load external "programs" in RAM, and run the interpreter. The interpreter could also be some kind of emulator instead (from your description, looks like it could be that), with the same approach: load the programs in RAM and run the emulator. Typically, you could well write/use an emulator of some known gaming platform (as long as it fits in your MCU).As to running code directly from RAM, AFAIK, you can also do this on ARM Cortex. You can take a look at this for instance:https://github.com/libopencm3/libopencm3/wiki/Run-From-RAM

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 18, 2019, 04:46:45 pm
content: Yes i dont want to reprogram the chip flash every time a game is swapped, as SiliconWizard say it will break some time.Having to reload the game on power loss is normal.So i am going for the "run the code out of RAM"Thanks for the SD vs USB hint, will go for SD card, this ILI screen i got has SD card in it.@ ledtester : I have a PIC32 chip the PIC32MX170F256B, what exact do i search the manual for ?

author: ledtester
author_rank: Super Contributor
timestamp: August 18, 2019, 10:18:42 pm
content: Quote from: Jan Audio on August 18, 2019, 04:46:45 pmWhat exactly are you asking about?If it's about reading/writing SD cards, then note you will need a library to 1) interact with the SD card and 2) implement a file system such as FAT16 or FAT32)Have a look at this thread for pointers to well-established libraries:https://www.microchip.com/forums/FindPost/772526If it's about executing code out of RAM, have a look at this thread:https://www.microchip.com/forums/m575216.aspxIt looks possible but might be a little more difficult than you think!Alternatively, considering writing you app in FORTH. You'll get the equivalent of a compiler/linker/loader; an interactive development environment; ability to program in a high-level language. Challenges will be: getting your dev environment set up; learning FORTH; interfacing with existing C and ASM libraries; much smaller user and support community.https://groups.google.com/forum/?nomobile=true#!topic/comp.lang.forth/P-h_5LocaLI

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 19, 2019, 01:21:56 pm
content: Hi, i am not planning to learn a new language, i do C only ( or C++ ).How do i program a game to load in the MCU ?,Or maybe it is easyer to play a game ROM ?, from old nintendo or sega.Is it something like loading a DLL for a windows program ?, load some functions.

author: rstofer
author_rank: Super Contributor
timestamp: August 19, 2019, 02:26:34 pm
content: Read the User Manual for device programming.This is where the subject gets real and hard... Can code run out of RAM? Beats me! It's in the 346 page datasheet somewhere:http://ww1.microchip.com/downloads/en/DeviceDoc/PIC32MX1XX2XX-28-36-44-PIN-DS60001168K.pdfHow does the device get programmed? ICSP (In Circuit Serial Programming) is one method but there is mention of an In Application Programming method as well. Can either method load code into RAM?Sometimes code for multiple programs is stored in a large flash device and a second MCU selects which code to run and actually performs the ICSP for the main MCU. Still, we haven't proven that code runs out of RAM and that's a fundamental question.A related question is whether there is enough RAM for the code and data space. It's not unusual to have an MCU with gobs of flash but not so much RAM. One version of the PIC32MX device has just 256 KB of Flash and only 64KB of RAM (from the datasheet linked above). That doesn't seem like a lot...Have you got a working game ready to run on the PIC32MX? Will the entire project fit in 64KB? You can have some game independent code in the flash, of course, but even that doesn't seem like a lot of space.It's one thing to pick a neat chip like the PIC32MX devices, it's another to pick one that will actually work in the application. You definitely want to run code out of RAM, make sure you have that capability before you lock in on a device.

author: Jan Audio
author_rank: Frequent Contributor
timestamp: August 19, 2019, 02:55:59 pm
content: No i have no working game ready, i dont know how to compile anything, still working on that.I am a bit nintendo fan, i dont know how much those games will take in memory.I seen youtube video that it should work :