Is there a way for a program to know if it's executed under the simulator and not on an actual CPU?
Alright, here's my solution for LPC2xxx chips, in case anyone is interested. It is based on the fact that most flash programming tools modify the program image in such a way that the sum of all interrupt vectors in the boot block will be 0, whereas under the simulator the program is executed "as is", without any modification.
#include <stdint.h> #include <stdbool.h> bool in_simulator(void) { uint32_t sum = 0; uint32_t * p; for (p = 0; p < (uint32_t *)0x20; p++) sum += *p; return sum != 0; }
I doubt that anyone cares, but any criticism and suggestions are welcome :)
It would be simple to put a sim script in that watched for a particular variable / address to be written then modify it, invert it, or something else specific as desired. You likely would have ignore the zero init of the variable.
In the application, write this item then read it back - treating it as volatile. If it reads back different than written, it would indicate simulator.
This should be quite generic and not rely on specifics of the CPU, build configuration, or simulator flaws (features ) that do not exactly match the CPU.
Chad,
Yes, this is a good idea too. Its big advantage is that it's CPU-independent (I think you could use it with the 8051 as well), but the disadvantage is that it depends on a script that you have to create for every program that uses it.
Cheers, Sandy
View all questions in Keil forum