|
|
The following files are included. The same code works for both SVR5 DDI drivers and SCO OpenServer 5 ODDI drivers.
Illustrates how to determine the system endianness in code. It compares the top and bottom values of the byte to determine endianness.
Macros that swap bytes per word. The driver writer must provide such macros when the endianness of the card does not match the endianness of the bus.
Both code fragments can be included as in your driver's header file, or can be used as models for functions you code yourself.
All device accesses must be bracketed by macros to encapsulate the endianness handling.
The driver typically knows the endianness of the target device. In this example, the card_is_little_endian variable is set by the programmer, based on the capabilities of the card. This fragment shows complete host endian-independence.
#define WRITE_BOARD_32(a,d) a = must_swap ? swap_32(d) : d #define WRITE_BOARD_16(a,d) a = must_swap ? swap_16(d) : d #define READ_BOARD_32(d) must_swap ? swap_32(d) : d #define READ_BOARD_16(d) must_swap ? swap_16(d) : dIf you know the endianness of your host at compilation time, you can reduce the runtime cost with code such as the following:int must_swap = is_little_endian() ^ card_is_little_endian;
#if YOU_KNOW_HOST_ENDIANNESS != DEVICE_ENDIANNESS # define WRITE_BOARD_32(a,d) a = swap_32(d) # define WRITE_BOARD_16(a,d) a = swap_16(d) # define READ_BOARD_32(d) swap_32(d) # define READ_BOARD_16(d) swap_16(d)#else # define WRITE_BOARD_32(a,d) a = d # define WRITE_BOARD_16(a,d) a = d # define READ_BOARD_32(d) d # define READ_BOARD_16(d) d #endif