Byteswapping In The RISC System/6000
Paul Chamberlain
This is supposed to be a 10-minute, everything you need to know about
byteswapping document. There's not much fluff, so read it carefully.
Origin
Byteswapping came about because microprocessors built by
Intel and
Motorola
interpret a sequence of bytes differently.
Sometimes it is helpful to remember that there are
24 possible (and entirely feasible) orderings of a four
byte value, the two discussed here are the only popular ones.
Given four consecutive bytes of
memory containg 0x41, 0x42, 0x43, and 0x44, an Intel
processor will obtain the 32-bit binary value of 0x44434241,
while a Motorola processor will read a value of 0x41424344.
The popular term for the Motorola style is Big-Endian, and for
the Intel style is Little-Endian. These are references to
the egg-eating styles in
Gulliver's Travels.
When is byteswapping necessary
It's important to note that no matter where the data resides,
the string ``Hello World'' is always stored sequentially with
the ``H'' in the first byte (the lowest address). Therefore,
someone has to know that 0x41424344 is intended to be used as
a binary value and not as the string ``ABCD'' to know
that it needs to be byteswapped when moved from a Big-Endian
CPU to a Little-Endian CPU or it will come out wrong.
The Microchannel bus
When data is transferred over the Microchannel, the first byte
is always on data lines that are called D7-D0 (The Least
Significant Bit is called D0), and the next three bytes
are on D15-D8, D23-D16, and D31-D24. Note that the meaning
of the first byte depends on what processor wrote the value
and wether or not they byteswapped it. Architecturally speaking
it should be the Least Significant Byte since the bus is based
on the Intel processors.
Confusing notation
To further complicate this subject, a diagram of memory
is biased by the author. Big-Endian authors draw memory
left-to-right while Little-Endian authors draw it
right-to-left. The above diagram does it the right way.
In fact, what makes the diagram in the Hardware Technical Reference
-- General Information so difficult to understand is that
the top half of the diagram is drawn Big-Endian style and the
bottom half is drawn Little-Endian style. It appears that the
IOCC is doing byte swapping but it is really just a notation change
in the diagram.
Who does byteswapping in the RISC System/6000
In general, the device driver knows whether a value is data
and should be written as is or if it is a 32-bit binary value
and needs to be byteswapped. In some cases, the driver is
merely passing data and cannot determine if it needs to be
swapped and leaves this responsibility to higher level software.
An important example is network data; TCP/IP, the driver, and
even the adapter preserve the data order and depend on the
applications on either end to agree on the representation
of binary values. See the
man page for htonl() for information about "Network Order."