Author Topic: Running a blank microcontroller - what's actually executing?  (Read 6213 times)

0 Members and 1 Guest are viewing this topic.

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1480
  • Country: gb
As I was lying in bed this morning, a curious thought suddenly popped inside my head... (What do you mean, thinking about microcontrollers during your Sunday lie-in isn't normal? ;D)

Take a fresh-out-of-the-bag microcontroller - for sake of discussion, say an AVR - that has never been used before, and is blank/empty, with nothing programmed on it. You hook it up to power in an appropriate manner, and turn it on. What will it actually be doing?

What I mean by that is, from an external viewpoint it may be sitting there doing nothing, but what's actually executing on the CPU?

Would it simply depend on what the default contents of the flash memory is? Is the flash memory just pre-filled with null bytes? That would make sense for an AVR, as 0x0000 is the NOP instruction. But then how would it keep looping so forever? Would it keep executing instructions until it reaches end of program memory, then wrap around to the beginning?

Or, is there some other behaviour? Perhaps some kind of special value pre-programmed in that tells the CPU to halt or sleep straight away?
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2086
Re: Running a blank microcontroller - what's actually executing?
« Reply #1 on: July 23, 2017, 11:34:25 pm »
I suspect that the programme memory will default to either all ones or all zeros. The next time you take a microcontroller fresh-out-of-the-bag you could try reading its contents before you overwrite them. It shouldn't take too long to figure-out what the op-codes are doing.
 

Offline george.b

  • Frequent Contributor
  • **
  • Posts: 383
  • Country: br
Re: Running a blank microcontroller - what's actually executing?
« Reply #2 on: July 23, 2017, 11:37:11 pm »
A fresh-out-of-the-bag microcontroller would (should) be blank. A blank EEPROM is actually full of 1s, not null bytes. Your hypothetical blank AVR would be executing 0xFFFF. I don't know what it does on an AVR, but 0x3FFF on a PIC (which has a 14-bit word, hence 0x3FFF) is ADDLW (Add Literal to W register) 0xFF... which, externally, would be unnoticeable.
I'm not sure what happens when the program counter overflows. I suppose it wraps around.
 
The following users thanked this post: KL27x

Offline sleemanj

  • Super Contributor
  • ***
  • Posts: 3025
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Running a blank microcontroller - what's actually executing?
« Reply #3 on: July 23, 2017, 11:40:57 pm »
1. NOP
2. NOP
3. NOP
... [ a lot more of the same ] ...
n. NOP
GOTO 1

To add to this, on the AVR 0xFFFF is debatable I think, some will say it is an invalid instruction and becomes a NOP, other say that it's actually behaving as a skip instruction (skip the next, execute the one after), end of the day it serves to move the program counter forwards until it wraps.

« Last Edit: July 23, 2017, 11:51:30 pm by sleemanj »
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline schmitt trigger

  • Super Contributor
  • ***
  • Posts: 2223
  • Country: mx
Re: Running a blank microcontroller - what's actually executing?
« Reply #4 on: July 24, 2017, 12:43:32 am »
As I was lying in bed this morning, a curious thought suddenly popped inside my head... (What do you mean, thinking about microcontrollers during your Sunday lie-in isn't normal? ;D)


Sorry to break the news to you my friend, but you are now addicted.

Don't attempt a withdrawal cold turkey; it is painful.
My advice to you, find a proper 13 step program in your city and register immediately!
 

Offline laneboysrc

  • Contributor
  • Posts: 28
  • Country: sg
    • LANE Boys RC - radio control related electronics
Re: Running a blank microcontroller - what's actually executing?
« Reply #5 on: July 24, 2017, 03:14:12 am »
On the NXP LPC812 (and other) the MCU has a tiny ROM that runs after reset. It checks whether the flash contains a program through checking a magic value at a certain flash location, and if that is not found it stays in a ISP (in-circuit serial programming) function.

Quite useful feature to bring up new boards.
 

Offline krho

  • Regular Contributor
  • *
  • Posts: 223
  • Country: si
Re: Running a blank microcontroller - what's actually executing?
« Reply #6 on: July 24, 2017, 05:15:29 am »
A fresh-out-of-the-bag microcontroller would (should) be blank. A blank EEPROM is actually full of 1s, not null bytes.

Usually it's 1s, but I had a stm32L0 on the desk the other day and blank was full of 0s.
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11653
  • Country: my
  • reassessing directives...
Re: Running a blank microcontroller - what's actually executing?
« Reply #7 on: July 24, 2017, 05:25:27 am »
Code: [Select]
goto (PC)everywhere, no i'm not sure its none of my business...
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Running a blank microcontroller - what's actually executing?
« Reply #8 on: July 24, 2017, 05:37:24 am »
Erased flash condition is 0xFF, if they perform production test on flash, they most likely erase it again, expect 0xFF.

From the ARM docs it looks like it will continuously run the SWI instruction with an invalid condition field.
But, it will most likely encounter an error before even starting anything since your reset vector will be 0xFFFFFFFF. I don't know precisely to be honest.

For NXP chips the bootrom actually inhibits the user code, perhaps more chips do this.
« Last Edit: July 24, 2017, 09:40:31 am by Jeroen3 »
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: Running a blank microcontroller - what's actually executing?
« Reply #9 on: July 24, 2017, 06:04:16 am »
But, it will most likely encounter an error before even starting anything since your reset vector will be 0xFFFFFFFF.

why would it? Isnt' the reset vector just a memory location? it could be a goto or it could be anything (like a NOP) then at the end of the program space it could either wrap around or generate and address error trap and reset
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 3725
  • Country: us
Re: Running a blank microcontroller - what's actually executing?
« Reply #10 on: July 24, 2017, 06:06:09 am »
What happens is going to depend on the MCU in question.

ARM cortex-M processors start by loading the stack pointer and PC from the values at address 0x0 and 0x4.  This is generally the first 8 bytes of internal flash.  If those are 0xFFFFFFFF it will presumably generate a hard fault trying to execute at an invalid address.  What happens then I have no idea -- it might trigger a reset for a fault happening during PC initialization, or it might try to execute the hard fault hander stored in early flash -- which will also be invalid.  It might give up and shut down, or just keep reseting itself.  All of that is assuming that the MCU even starts up -- there could be vendor specific  rules that fail before the ARM even initializes, such as configuration bits that set the initial clock source or that control memory mapping -- for instance to selectively map internal or external flash at address 0x0.

Some MCUs come pre-flashed with a bootloader, or have a ROM with a bootloader.  In that case, they probably detect the lack of a valid program, and sit waiting to be programmed.
 

Offline C

  • Super Contributor
  • ***
  • Posts: 1346
  • Country: us
Re: Running a blank microcontroller - what's actually executing?
« Reply #11 on: July 24, 2017, 06:17:00 am »
Way back in the TTL logic day, An output low could supply more current then and output high. You also had TTL open collector logic which used a pull-up resistor which you could create a wired-OR.
This leads to Pull-up resistors on tri-state signals if you want a known condition.
Pull-up resistors on Inputs to have a known state when nothing is connected.

For a Z80, 8085, 8080
00H is a NOP. Address bus counts 0H to FFFFH and pc wraps to 0 and does it again again
FFH is a RST7, a one byte call to an address. Calling PC address is push pushed on stack, but stack register is unknown at power up.
Address bus shows a fixed address followed by decreasing by 2 address that wraps.



For 68000
68000 reads 4 bytes starting at 0H to PC, Next 4 bytes to SP and jumps to PC address.
16 bit insturctions
0000H ORI   Inclusive-OR of data register 0 , the data is the low-order byte of the immediate word  So internal address bus counts 0 to FFFFFFFEh by 2 bytes with only A1-A23 on pins a counter that wraps if allowed to execute.
FFFFH is a coprocessor instruction if valid.
Low address area is mot used for instructions but software traps & hardware interrupt vectors  ( pointers to software). Trying to run Instructions in this area  this area could be a software trap
So what happens is a question, for someone that cares enough.

Have no
     
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12864
Re: Running a blank microcontroller - what's actually executing?
« Reply #12 on: July 24, 2017, 08:37:41 am »
There's also the matter of the default non-programmed settings of non-volatile configuration registers (aka 'Fuses').   E.g. many Microchip PICs and (ex-Atmel) AVRs wont run unprogrammed in many typical application circuits because their default clock mode isn't compatible with the clock circuit components present.
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Running a blank microcontroller - what's actually executing?
« Reply #13 on: July 24, 2017, 10:01:20 am »
halt
and
get
fire

 :D
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Running a blank microcontroller - what's actually executing?
« Reply #14 on: July 24, 2017, 10:11:32 am »
FFFFH is a coprocessor instruction if valid.
Low address area is mot used for instructions but software traps & hardware interrupt vectors  ( pointers to software). Trying to run Instructions in this area  this area could be a software trap
So what happens is a question, for someone that cares enough.

Tried on a CPU32 system which I happen to have attached to a LA: I haven't put to much attention on LA, what I see from the outside, well it fetches NOPs until it resets, and then it begins the same in circle  :-//

Oh well, it doesn't explode, at least  :D
 

Offline @rt

  • Super Contributor
  • ***
  • Posts: 1059
Re: Running a blank microcontroller - what's actually executing?
« Reply #15 on: July 24, 2017, 12:57:16 pm »
For a given micro you might find out by writing instructions to every location other than the reset vector that
set an output bit for a digital pin perhaps, assuming you may not have to set port direction.
Or maybe see if the watchdog resets that sort of program if it doesn’t run,
or allow execution to fall off the end of an otherwise valid asm program into the blank memory space,
and check the reset register to find out why the previous reset occurred (if it did reset).

« Last Edit: July 24, 2017, 12:58:57 pm by @rt »
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Running a blank microcontroller - what's actually executing?
« Reply #16 on: July 24, 2017, 02:20:09 pm »
on the AVR 0xFFFF is debatable I think, some will say it is an invalid instruction and becomes a NOP, other say that it's actually behaving as a skip instruction
0xFFFF is supposed to be an invalid opcode, but has the same effect as SBRS 31, 7 (skip if bit 7 set in register 31) and so in this situation is effectively a NOP.

http://www.avrfreaks.net/comment/83257#comment-83257
Quote from: pepsi
So what happens when an AVR tries to execute 0xFFFF? If you look in a recent instruction set, you will find that no instruction corresponds to 0xFFFF. However, in early databooks (May 1997), SBRS is given the opcode of 1111 111r rrrr Xbbb, X meaning dont-care. This means that 0xFFFF corresponds to SBRS r31, 7!
« Last Edit: July 25, 2017, 06:06:37 am by Bruce Abbott »
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: Running a blank microcontroller - what's actually executing?
« Reply #17 on: July 24, 2017, 03:06:09 pm »
Take a fresh-out-of-the-bag microcontroller - for sake of discussion, say an AVR - that has never been used before, and is blank/empty, with nothing programmed on it. You hook it up to power in an appropriate manner, and turn it on. What will it actually be doing?
It is watching you, wondering what you have planned for it.

Seriously, I think this is a great question (maybe I have the addiction already).  The initial register and pin states is normally well documented.  I would imagine that anything that changed those states would be potentially hazardous (assuming you are building an ACME GunTurrentMaxoMatic).  I'm surprised it isn't explictly stated in the datasheet.
« Last Edit: July 24, 2017, 03:37:40 pm by NivagSwerdna »
 

Online Sal Ammoniac

  • Super Contributor
  • ***
  • Posts: 1674
  • Country: us
Re: Running a blank microcontroller - what's actually executing?
« Reply #18 on: July 24, 2017, 04:15:39 pm »
ARM cortex-M processors start by loading the stack pointer and PC from the values at address 0x0 and 0x4.  This is generally the first 8 bytes of internal flash.  If those are 0xFFFFFFFF it will presumably generate a hard fault trying to execute at an invalid address.  What happens then I have no idea -- it might trigger a reset for a fault happening during PC initialization, or it might try to execute the hard fault hander stored in early flash -- which will also be invalid.

A fault while running the hard fault handler results in a CPU lock-up.
Complexity is the number-one enemy of high-quality code.
 

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1480
  • Country: gb
Re: Running a blank microcontroller - what's actually executing?
« Reply #19 on: July 24, 2017, 09:49:48 pm »
Performed an experiment: took an unused Atmel ATtiny13A that was still sealed in it's anti-static bag, popped it in the breadboard and hooked up my AVR ISP MkII, and read the flash memory from it.

The entirety of the flash memory defaults to 0xFF, as you can see from the dump:

Code: [Select]
:10000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00
:10001000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0
:10002000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0
:10003000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFD0
:10004000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC0
... etc ...
:1003B000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4D
:1003C000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF3D
:1003D000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2D
:1003E000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF1D
:1003F000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0D
:00000001FF

I also managed to find a reference in the ATmega328P datasheet about what happens when the chip is erased. In the section on Serial Programming, it says:

Quote
The Chip Erase operation turns the content of every memory location in both the Program and EEPROM arrays into 0xFF.

It seems both Jeroen3 and Bruce Abbott were right on the money. :-+

So if 0xFFFF is interpreted as SBRS 31,7 - which by the way turns out is 'skip if bit in register is set', not 'set bit in register' - then that means it will always be testing for a bit that will never be set (assuming R31 defaults to zero); at every step it will simply skip to the next instruction, forever and ever. Effectively equivalent to a NOP, really.

Interesting to also hear that more sophisticated microcontrollers with integrated bootloaders and such actually check to see if they have a program.
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Running a blank microcontroller - what's actually executing?
« Reply #20 on: July 25, 2017, 05:32:07 am »
From the LPC1788 user manual:
Quote
37.3.1.1 Criterion for Valid User Code
The reserved Cortex-M3 exception vector location 7 (offset 0x001C in the vector table)
should contain the 2’s complement of the check-sum of table entries 0 through 6. This
causes the checksum of the first 8 table entries to be 0. The boot loader code checksums
the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is
transferred to the user code.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: Running a blank microcontroller - what's actually executing?
« Reply #21 on: July 25, 2017, 06:20:26 am »
Here is what I have observed from a STM32F407ZG after a fresh erase: the processor tries to jump to address 0xFFFFFFFF, hits a hard fault and loads the hard fault handler, which is once again 0xFFFFFFFF, and it just loops there.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3147
  • Country: ca
Re: Running a blank microcontroller - what's actually executing?
« Reply #22 on: July 25, 2017, 12:51:47 pm »
From the LPC1788 user manual:
Quote
37.3.1.1 Criterion for Valid User Code
The reserved Cortex-M3 exception vector location 7 (offset 0x001C in the vector table)
should contain the 2’s complement of the check-sum of table entries 0 through 6. This
causes the checksum of the first 8 table entries to be 0. The boot loader code checksums
the first 8 locations in sector 0 of the flash. If the result is 0, then execution control is
transferred to the user code.

PIC24 won't run neither. It requires a "goto" instruction at the reset (0x0) location. If there's anything else it won't run.
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 828
Re: Running a blank microcontroller - what's actually executing?
« Reply #23 on: July 25, 2017, 02:15:26 pm »
Quote
assuming R31 defaults to zero
Wrong assumption. R0-R31 are not initialized by hardware, and could be any value when powered on.

A more interesting question for an AVR, is what happens when a push/pop is executed when the stack pointer points to an address outside of the ram addresses (either above or below). Also what happens when an XYZ pointer is used which points to a location above ram.

I do have the answer.
 

Online HwAoRrDkTopic starter

  • Super Contributor
  • ***
  • Posts: 1480
  • Country: gb
Re: Running a blank microcontroller - what's actually executing?
« Reply #24 on: July 25, 2017, 03:59:50 pm »
Quote
assuming R31 defaults to zero
Wrong assumption. R0-R31 are not initialized by hardware, and could be any value when powered on.

I thought that might be a possibility, hence that parenthetical remark.

If the bit tested happens to be 1, no real difference I suppose - it'll just do the jump and carry on to yet another SBRS instruction. :)
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Running a blank microcontroller - what's actually executing?
« Reply #25 on: July 26, 2017, 08:02:07 pm »
Well, on my HDL-softcore (fpga), I have designed the register file to have a full initialization to zero on every hw-reset event. Thus, I am sure that all registers are actually zero  :D

 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: Running a blank microcontroller - what's actually executing?
« Reply #26 on: July 27, 2017, 08:56:34 pm »
Well, on my HDL-softcore (fpga), I have designed the register file to have a full initialization to zero on every hw-reset event. Thus, I am sure that all registers are actually zero  :D
I wonder if that kind of rigidity is necessary. Maybe you can design most the register file to be uninitialized.

AFAIK for AVR only the program counter (default to either zero or the beginning of the bootloader area, determined by a fuse) and the stack pointer (default to the end of RAM) have defined reset values. For ARM Cortex-M there is no initial value for all registers and the only guaranteed reset state is that the pipeline resets into the state as if loaded the instruction “ldm 0, {sp, pc}”
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Running a blank microcontroller - what's actually executing?
« Reply #27 on: July 27, 2017, 10:03:39 pm »
I wonder if that kind of rigidity is necessary

Good practice on HDL-design.
 

Offline technix

  • Super Contributor
  • ***
  • Posts: 3507
  • Country: cn
  • From Shanghai With Love
    • My Untitled Blog
Re: Running a blank microcontroller - what's actually executing?
« Reply #28 on: July 27, 2017, 11:54:23 pm »
I wonder if that kind of rigidity is necessary

Good practice on HDL-design.
I would consider those additional rigidity a lower priority component, which can be axed when yield is dipping or cost is getting a bit too high.
 

Offline eugenenine

  • Frequent Contributor
  • **
  • Posts: 865
  • Country: us
Re: Running a blank microcontroller - what's actually executing?
« Reply #29 on: July 28, 2017, 02:21:51 am »
We always said it was executing go jump in the lake instructions after having an early college professor who explained that microprocessors are stupid, if you tell them to go jump in the lake they will.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf