2

I writes some C-program code for Altera/Nios II microprocessor (uP). This code will be different with Altera Arm 9 microprocessor. So I need to write 2 different code pieces for different uP-s. How can I check in execution time which uP is present. Or more simple, current uP is Nios or not.

Yakov
  • 187
  • 1
  • 11

3 Answers3

1

As the two processors are from different architectures, you will not be able to check which processor is running at run-time. You could do it at compile time, as you will have a specific define flag set by your toolchain (see https://sourceforge.net/p/predef/wiki/Architectures/). For Arm it should be __arm__ or similar, depending on the toolchain you are using for the HPS.

#ifdef __arm__
    <specific code for HPS>
#else
    <specific code for NIOS>
#endif /* __arm__ */

You can also look at the toolchain's defines using the c pre-processor command (cpp):

<toolchain>-cpp -dM /dev/null

Note: for Arm processor, the MIDR register could be used to know which type you are running and this one could be accessed at runtime. But when building for NIOS II, you would have a compilation error. So you need to use the preprocessor to call specific Arm register name and to remove the code when building for NiosII.

Stoogy
  • 1,307
  • 3
  • 16
  • 34
  • 1
    I suppose that **altera_bsp/system.h** file that is HW/FW interface for Nios should have some definition of processor type. This is that I look for, and this I can test in run time. – Yakov Mar 19 '19 at 15:16
0

Presumably it will be compiled with a different compiler? These compilers will (very likely) have a #define of some sort which you can use to build different code for each one.

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
-1

You can make the compiler dump all its default preprocessor defines using:

echo | ./nios2-elf-gcc.exe -dM -E -

This will in particular emit:

#define nios2 1