I’m doing programming of a softcore processor, Nios II from Altera, below is the code in one of the tutorial, I manage to get the code working by testing it on the hardware (DE2 board), however, I could not understand the code.
#define Switches (volatile char *) 0x0003000
#define LEDs (char *) 0x0003010
void main()
{ while (1)
*LEDs = *Switches;
}
What I know about #define is that, it is either used to define a constant, or a macro, but
- why in the above code, there are casting like,
(char *) 0x0003010, in#define? - why the 2 constants,
SwitchesandLEDsact like a variable instead of a constant?
Preprocessor macros are textual replacements. So the code comes out as
which repeated assigns the contents of the input (switch) mapped at 0x3000 to the output (led) mapped at 0x3010.
Note that those are pointer. So they always point to the same place (which happens to be a couple of memory mapped IO pins or something similar), but there is no guarantee the the contents of those constant locations are constant, and the
*appearing before each preprocessor symbol is the pointer de-reference operator.