Hi,
I'm writing code for an embedded system. In the system a
Timer has 4 memory mapped registers of 32-bit lengths
in contiguous locations:
Timer 0:
0x1000 Configuration register
0x1004 Control register
0x1008 Input register
0x100C Output register.
My objective is to declare a type for the timer,
so I can write the following:
Timer * pTimers = (Timer *) 0x1000;
pTimers[1]->Config_Reg = 0x24;
/* or */
pTimer++; /* points to next timer */
My choices are to declare a typedef of an array of
4 32-bit {unsigned} integers or a structure with
4 32-bit {unsigned} integers.
Th issue with a structure is that the compiler
*may* insert padding bytes between members, which
would mess up the mapping (of members to physical
registers).
However, an array doesn't have field members, so
one would have to declare named constants for the
indices:
#define Config_Reg 0
#define Control_Reg 1
#define Input_Reg 2
#define Output_Reg 3
Is there any preference between an array typedef
and a structure for readability?
I know if I use a structure, I can always say
something like:
assert(sizeof(struct Timer) == 4 * sizeof(/* 32-bit integer */));
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book