Search code examples
embeddedstm32controllersflash-memory

How Program counter increment


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.


Solution

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