1
votes

We working with STM32 and TI controllers. I want know in controllers how program counter works, as i know it will point the next instruction need execute. If so, when we dump the code, it will stores in the Flash memory after reset program will start from the reset vector table then move to the main code.

  1. If program start from the reset vector table then PC will point the Flash address?
  2. At the running of the main application PC is point RAM address. How?
  3. When PC move to the RAM location?
  4. Code is stored in flash location and PC pointing RAM location.... how is this happening? When code is move to the RAM location?
  5. If it move to RAM, where it will stores? on which section(segmentation)?
  6. Who will decide that?

Please help to understand above doubts.

3
I do not understand you. What PC? Do you ask about uC or PC computers. - 0___________
@PeterJ I believe he is talking about Program Counter (PC) register. - rkrahl

3 Answers

2
votes

In general, the program-counter is loaded with the reset vector or is loaded with a hard-wired start address depending on the architecture. For ARM Cortex-M devices such as STM32, it is the former. For most instructions the program-counter is automatically incremented to the next instruction, branch and jump instructions modify the program-counter directly to the specific target address (either conditionally or unconditionally).

If program start from the reset vector table then PC will point the Flash address?

On STM32 at least that depends on the boot mode set by the BOOT0/BOOT1 pins; it can boot from the on-chip mask-ROM boot-loader, the on-chip Flash memory, or the on-chip SRAM - read the user manual. With respect to "TI controllers", TI manufacture many controllers with different architectures and you have been non-specific - bootstrapping is likely different for each one - again read the user manual.

At the running of the main application PC is point RAM address. How? When PC move to the RAM location?

That would only be true is your application were located in RAM where either the reset vector would point to RAM or boot-loader or run time start-up code has branched or jumped to a RAM address. STM32 code typically runs from flash rather than RAM; it runs faster that way because the flash and RAM are on different busses (Harvard architecture) allowing simultaneous data and instruction fetch. Running code from RAM will slow execution.

Code is stored in flash location and PC pointing RAM location.... how is this happening? When code is move to the RAM location?

That is not necessarily true and in the case of STM32, it is seldom true - code can be stored and executed from flash. Controllers that execute from RAM have bootstrap code in ROM that copies (relocates) the executable code to RAM and then jumps to it.

If it move to RAM, where it will stores? on which section(segmentation)?

Who will decide that?

The linker determines that and in turn it is directed by the linker script. For GNU tools, this typically has a .ld file extension, other tool chains differ.

1
votes

Firstly, please take a look at this documentation:

https://en.wikipedia.org/wiki/Program_counter

Program counter is implemented in hardware and stored as microcontroller register. Where it points at is a matter of how your software is organized. If you put your code to flash, it'll point to flash, if you put it to RAM it will point to RAM. You can control where your program is stored with linker script.

Current position of PC is controlled by program flow, that means - last instruction executed by microcontroller. It can be also changed by interrupts.

If your program is stored in flash and PC points to RAM then maybe some part of your code is copied to RAM and executed from there. It's hard to tell without looking at real code.

1
votes

PC is a register and is not part of the flash or RAM. The initialization of the PC can differ from the microcontroller. For example in documentation of the tm4c129encpdt (page 97) you can find:

The Program Counter (PC) is register R15, and it contains the current program address. On reset, the processor loads the PC with the value of the reset vector, which is at address 0x0000.0004. Bit 0 of the reset vector is loaded into the THUMB bit of the EPSR at reset and must be 1. The PC register can be accessed in either privileged or unprivileged mode.

Typically executed code is in flash. But there is no problem to place it in RAM and execute it from there.

If it move to RAM, where it will stores? on which section(segmentation)?

PC stores the address of the instruction. It depends from you where this code will be placed.

Who will decide that?

In simple applications it happens automatically and you don't need to think about this. Anyway it's easy to imagine a simple case when we want to execute code from RAM: suppose you got an update procedure and you don't want to care if the old update function will update old software to the new one correctly. You can just send an update function via UART / SPI etc. and execute it from RAM.