Loading a new code from within the code

I remember my first computer, a commodor64, this was no more powerful then a Sanguino.

Back then, we were loading the code from a audio tape, remember? Well here is my main question. We were able to star the tape reading with a command call "cload". This command was integrated in the basic language that came with the C64.

So at some point in your programs, you knew that you were gonna run out of space and you could tel the user to put a specific tape in the deck at a specific time, on the tape, and press a key to start loading new coding from the tape.

Now, what I want to know is, can we do the same on the Arduino? Can we, at some point, from within the Arduino programming tel the board to flush the current program and load new coding from outside?

In theory it's as simple as reading your executable code, then addressing the instruction pointer to the beginning of your binary resource and off it goes...

It sounds very reasonable to do though..

Well I liked that aspect of the C64. The only limit to your program size was how many tape you had available. For sure these day's there are many more medium faster and more interesting to use then an audio tape, but the main idea is that. We would not be limited to the 8, 16, 32, 64, 128 or 256 kB of programming space that way.

So cjdelphi, do you have any link of programming example that would lead me to learn to do this? I have no idea where to start.

Frederic_Plante:
Now, what I want to know is, can we do the same on the Arduino? Can we, at some point, from within the Arduino programming tel the board to flush the current program and load new coding from outside?

Can you tell Windows to format all your hard drives and then reload itself from the Net?
That's what you appear to be trying to do.
If you flush the current program, how does it know that it should load a new program? The code that tells it to do that (and how to do that) has been flushed!

Mk, so then if it's impossible, how did the C64 did it? :wink: It's already been done, it's just old knowledge lost in the dust.

It has to be possible, you and I just don't know how to do it.

It's probably something in the bootloader that check if there is something to run, and if there is none, request some command. I don't know. but what i do know, is that from my program on the C64, I was starting the cassette and loading a new set of command line and at the moment that the cassette was stopping from reading, the old program was not in the memory anymore, only the new one from the tape.,.

It was technology, not magic. lol

When I was a kid, thats how I was programming very long text base adventure games on BBS. Any ways has to be possible. cause it’s been done. It’s old stuff.

It's been done - on a computer that stores its program in RAM.

The Arduino stores its program in flash memory. Not the same.

You are probably better off going to a larger chip (644, 1280, 2560). By the time you've got your peripheral installed, and annoy the user getting them to load the next part of the program, you may as well spend $5 and get a larger chip.

:slight_smile: I was hopping you would get involved mister Gammon, I'm happy. :slight_smile:

I agree for the larger chip. But there would be no challenge and nothing to learn. The easy way is often the best to get result, but not to get knowledge.

Frederic_Plante:
When I was a kid, thats how I was programming very long text base adventure games on BBS. Any ways has to be possible. cause it's been done. It's old stuff.

There's probably a way to do it by rewriting the bootloader, but it would be painful and unless you have some really extraordinary circumstances, not worth it. What you're asking is akin to looking for a way to put spark advance and retard controls on a modern car - just doesn't make sense. In the example you're using, you could now hold all your text adventure data on an SD card: the program using it would not need to change. Even as an academic exercise, I can see little benefit coming from it - rejoice in the fact that you can cheaply purchase a chip with sufficient memory for your needs.

cjdelphi:
In theory it's as simple as reading your executable code, then addressing the instruction pointer to the beginning of your binary resource and off it goes...

I just have to point out this isn't correct. You can't point the instruction pointer to some "binary resource", whatever that means.

Processors like the Arduino use the Harvard architecture.

You cannot execute instructions from RAM. Wanting to won't change that basic fact. Nor will moving the instruction pointer.

Think there is a way to accomplish this loading if working together with a PC application.

The Arduino can send a signal to the PC over Serial e.g. “cload part2” and after confirmation the Arduino waits …
The PC application closes the serial connection, compiles part2.ino and uploads it with avrdude to the Arduino.
The Arduino can now start part 2.
To make it more usable, the Arduino could first store its relevant vars to a SD card and reload them after part2 is loaded.

In theory a second Arduino could do the programming (SPI?) and by having multiple .hex files on a SD card it can go on
“forever” as 1 GB SD card means >20.000 sketches :slight_smile:

a bit like - http://arduino.cc/en/Tutorial/ArduinoISP - ???

would be a nice student project to investigate and make a library out of it if possible

update another helpful link - Gammon Forum : Electronics : Microprocessors : Atmega bootloader programmer - + - http://forum.arduino.cc/index.php?topic=125248.0 -

I was thinking windows at the time, i've actually downloaded an entire exe into memory and then executed it!

but obviously there are restrictions i have no clue about... In my head I was thinking he meant something like a library (eg send over serial 50 bytes of executable instructions) then push pop whatever onto the stack and mess with the instruction pointer ..

But, if you have access to the instruction pointer why not be able to store that into ram and execute it?...

The AVR is incapable of executing anything in RAM, no matter how much you fiddle with the program counter.

that's a real shame really, it's not like anyone's going to attempt to write any kind of infection based code and a virus that spreads, the ability to send say something like a small DLL (obviously not a DLL) 10 bytes or so and have a library of routines you could send down the serial line to it, have it execute etc....

not needed i guess....

It could be done on an Attiny. They are capable of self programming their entire flash (they have no bootloader section).

You could potentially have a loading routing which fetches machine code from an external source (SD card, serial port, etc.).
Then have a section of the flash which is executed, then at the end jumps into the loader routines. The loader then erases that section of flash a programs in a next chunk.
The loader once programming is complete jumps back to the start of that section of the flash and the program continues.

You could do it with an ATmega and have the loader code inside the bootloader region, that way allowing the same process to work.

One thing you would have to be careful of is to either disable interrupts in the loader, or ensure that interrupt service routines, along with the vector table at the beginning are never erased, otherwise you could have an interrupt occur and try to run and erased section of code.
The other thing you need to be careful of is not to do the process too often. The flash has a limited erase/program cycle count. It is also a time and power consuming process.

that's a real shame really

Welcome to the real world.

This project is a over weird version of what I was thinking about. http://amigojapan.github.io/Arduino-Retro-Computer-TV/

Using 2 Arduino is, I believe, unnecessary, but hey they have an idea. I think that with a ATMega256, a few spiRam chip a few made port, a SD card, It would be possible to build a command interpreter with what left of the flash.

So then it would all be about movin stuff from the SDcard to the SPI ram and the made up command interpreter run it from RAM.

It would not be Arduino code has such, of coarse, I agree. But still, Arduino would be able to run much more code then what is inside the flash.

Thanx Tom,

I did not see that there where a second page, before writing my last comment.

Yeap, that solution seam to be the best. Using a chunk of the flash, has the bootloader does, that never get touch, seem to be a reasonable solution.

Any way. this discussion seem to bring interest, i’m happy about that :slight_smile: