Author Topic: Load and run external program  (Read 830 times)

0 Members and 1 Guest are viewing this topic.

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Load and run external program
« on: August 18, 2019, 01:28:03 pm »
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 out
What is the option needed to play the game ?

btw : i am into PIC chips if that is possible.

thanks
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: Load and run external program
« Reply #1 on: August 18, 2019, 02:03:20 pm »
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).
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Load and run external program
« Reply #2 on: August 18, 2019, 02:08:39 pm »
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.aspx

As 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/95488

Note 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.

« Last Edit: August 18, 2019, 02:11:16 pm by ledtester »
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14445
  • Country: fr
Re: Load and run external program
« Reply #3 on: August 18, 2019, 02:28:19 pm »
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
 

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Re: Load and run external program
« Reply #4 on: August 18, 2019, 04:46:45 pm »
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 ?
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Load and run external program
« Reply #5 on: August 18, 2019, 10:18:42 pm »
ledtester : I have a PIC32 chip the PIC32MX170F256B, what exact do i search the manual for ?

What 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/772526

If it's about executing code out of RAM, have a look at this thread:

https://www.microchip.com/forums/m575216.aspx

It 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
 

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Re: Load and run external program
« Reply #6 on: August 19, 2019, 01:21:56 pm »
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.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: us
Re: Load and run external program
« Reply #7 on: August 19, 2019, 02:26:34 pm »
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.pdf

How 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.
« Last Edit: August 19, 2019, 02:29:04 pm by rstofer »
 

Offline Jan AudioTopic starter

  • Frequent Contributor
  • **
  • Posts: 820
  • Country: nl
Re: Load and run external program
« Reply #8 on: August 19, 2019, 02:55:59 pm »
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 :

« Last Edit: August 19, 2019, 02:57:52 pm by Jan Audio »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf