Running code on external flash

Hi, i would like to know can i load code from an external flash and run it and if it is how, even if it is slow or for really simple code i am interested in knowing (only for learning because i think it would be almost the same as doing it on an avr32 and i want to try it before i buy a AT32AP7000) because the main feature of this would be lunching small program like a computer and the ATmega328 flash would be like the bios.

i just found code for loading a small linux kernel on some avr and i am looking at it to see if i can find how it process the byte received... i may learn something in this big code.....

what i have learn from what i have read is i need to send the compiled code in the sram before executing it....well in the code i found it seams just that easy.... any idea how i can implement this with the arduino ??

what i have learn from what i have read is i need to send the compiled code in the sram before executing it.

Wrong, for the ATmega and typical microcontrollers, which are harvard architecture (separate program and data memory).

Since the ATmega is capable of self programming, what you ask is technically possible, but not at all practical.

-j

well i hate to say this to a God member but you may not totally be right as

“The Harvard architecture is a computer architecture with physically separate storage and signal pathways for instructions and data.”

Storage separated from ram…

“In a computer with the contrasting von Neumann architecture (and no cache), the CPU can be either reading an instruction or reading/writing data from/to the memory. Both cannot occur at the same time since the instructions and data use the same bus system. In a computer using the Harvard architecture, the CPU can both read an instruction and perform a data memory access at the same time, even without a cache. A Harvard architecture computer can thus be faster for a given circuit complexity because instruction fetches and data access do not contend for a single memory pathway.”

so what i want to do CAN be done as i currently have a minimal linux kernel for the ATmega128 and i am currently trying to figure out how this work…

also it seams that i just have to make a function pointer to the address of the code i put in the sram and all should be working but i don’t understand how to convert line of code to byte to be put in the sram and how i can lock a mem space so the controller don’t try to overwrite it… for this i am reading about how malloc work and if this could co it. also i have found some way to map an external sram to the mem address and using it with malloc maybe there is a way to use it for more bigger and complex program as it seam some are doing it for bigger Atmel product.

Some interesting links

http://avr32linux.org/twiki/bin/view : AVR32 Linux project http://www.freertos.org/ : portable, open source, royalty free, mini Real Time Kernel. has a version ported for ATmega128 http://www.nongnu.org/avr-libc/user-manual/malloc.html : Memory Areas and Using malloc()

(i also got some other links but i want to post only the one i read first... at least i want to know the content of the link i am posting...)

Recent thread on avrfreaks discusses using FreeRTOS on atmega 168 or 328, and somebody succeeded running 3-4 tasks on a 168: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=602532

Nice, that you've found those links, but I think kg4wsv was right, and you are overexcited about some your own idea.

so what i want to do CAN be done

... but not on an Arduino, which is the context of this forum. The ATmega8 family in use on the arduino [u]is[/u] a harvard architecture.

-j

ya maybe.... but i am not gonna stop until i see exactly why and how this is or is not gonna work.... after that i will know the how and why..... of the result and why it is doing this.... learning here is the real goal not just doing it... so if you got some info of why i can or can't i want link and data because can't be done and because it is harvard based dont really help me understand WHY ?

Ps : Thx for helping me.

and also if i really have no way of sending compiled code in memory and lunching it then i will make the biggest array i can send my code there and build a little code that will read the array and process it as instruction... will do the job but not the way i wanted it to be done...

well it seam that i was … wrong and right… normally it cant be done but there are way of doing it…

Various “solutions” have been suggested such as:

  1. run a small Java virtual machine that executes Java bytecode from SRAM loaded from SD (or from SD direct)

  2. run a p-code interpreter that executes p-ode from the same

  3. run an AVR core emulator that has been modified to makes its opcode fetches from SRAM or SD

now i just need to do one of these…

so now i got a list of all the opcode and i'm on my way to try to read them from an ext flash or eeprom and lunch them in my code (so i will emulate what the hardware cant do).... when i think of yesterday i didn't even know what was opcode....

What you're talking about is a simulator / emulator. Not going to be easy in such a small memory; not only do you have to simulate each and every op-code's function, but also how they interact with things like the register set, particularly the status register.

On a normal non-Harvard architecture, one way of doing this is to execute each simulated instruction in memory on the actual processor trapping the execution, effectively single-stepping the simulated program. On a Harvard architecture this isn't an option. Good luck.

Is it not possible programatically load object code into program flash and then jump to that code? Upon completion the new code could return via a return address pushed onto some sort of user stack. Or is that kind of program jump only possible from the bootstrap address space?

well the ATmega series is capable of self reprogramming but it is a flash so you cant do it as fast as you want without killing it. the advantage of doing it in sram is i wont burn after like 100,000 rewrite. but you cant lunch it from there except if you have a little trick...

So i want to know guys what you think of my idea... what i want to try is

  1. provide a way to read code from an extenal device
  2. pass the read code to a sub that will call is op-code like : in flash we have a single direct register addressing (OP Rd) like DEC for decrement and is var, so in sram we will have 1001010 (5 bit number to decrement here, the actual var value) 1010

  3. after this has been executed return the result in sram.

  4. do next function.

the only problem is i still had some problem to access directly to those memory address but i am still reading

PS : can't do doesn't exist, it is only that you don't know how...... now i just need to learn to fly..... ;D

if someone has a good idea how i can make a pointer or something to access the opcode register it would be nice as i dont want to make an emulator or simulator because i dont need to i just need to pass the code from sram to the register... like when i have

B0101 & B0100

in my flash it would be written

0010 0000 0101 0100

it takes the 2 byte value and call it in the register and read the value at d

0010 00rd dddd rrrr

but i didnt find any way i can do this.... if someone has an idea i would be happy to know how i can do this...

I did mention in my previous reply avrfreaks - http://www.avrfreaks.net/. I think that, as your questions are very much AVR related and not so much Arduino, that is the place to ask. You will find some guys that have hacked the AVR hard.

thx... i will make an account there soon.... but at the same time if someone here can help it would be nice and also i am trying to make all this on the arduino ide....

So i want to know guys what you think of my idea... what i want to try is

  1. provide a way to read code from an extenal device
  2. pass the read code to a sub that will call is op-code like : in flash we have a single direct register addressing (OP Rd) like DEC for decrement and is var, so in sram we will have 1001010 (5 bit number to decrement here, the actual var value) 1010

  3. after this has been executed return the result in sram.

  4. do next function.

the only problem is i still had some problem to access directly to those memory address but i am still reading

I'm having difficulty understanding this, but what I think you're saying is that you don't want to emulate actual AVR opcodes, just some generic, invented opcodes.

Of course this is possible and really simple with an appropriately reduced and simplified instruction set. You could probably even implement a simple FORTH; such stack-based languages are ideally suited to this form of interpreter, because you don't need to worry about decoding addresses.

However, you'll still be very limited by only one or two K of RAM.

i do this for fun so ram limitation isnt a problem aslo thx for the good idea... i might give it a try.... so i would like a little more info on this thx.