There may be a terminology problem here. I think what Flash Gordon is
saying is that there does not appear to be any reason why the code
could not be written is such a way as to entirely portable.
I.e. there would be no need to change the source when moving from a
big- to little-endian machine. Let me illustrate where the problem
might lie.
Again, that is appsolutely true if all you want is the arithmatic values.
That would work sufficiently if you just want a hex printout of the
arithmatic result values for analysis. This might also work for simple
functional testing a DSP without actually doing a full hardware simulation
to get an idea of what a waveform should look like if the DSP software
is properly implemented.
It may not, however, be a close enough for the emulation required to
develope and debug software designed to run on the DSP/MCU/MPU. That would
require not only the arithmatic value and printed representation of that
value; but, would require a true to form binary equivilant of how the value
is actually stored inside of the emulated hardware.
I work with PIC and AVR 8bit (actually 12-16 bits internally) MCUs. I have
to test and debug software that I write for them using an emulator. In
doing so, I need to know that exact binary representation of values within
the MCU's memory. If I copy the result of an arithmatic operation
(say -64) to a pin register, I need to know which pins are on and off:
7,6 for 2's compliment, 7,6 sign and magnitude, or 7,5,4,3,2,1 for 1's
compliment. The end results could be very different. The OP may be
making emulation software for such purposes.
If I choose to represent 9-bit signed values as long ints in the range
-256 to 255. Lets also assume the simulation requores that these
9-bit int look like 2's compliment numbers (no matter how they may
actually be held in the simulation). I can dump a single memory
location (x) like this:
printf("%d%02x", x < 0, (0x100 + x) % 0x100);
Once again, this is great for printed representation; but, lacks the fine
grainded understanding of the actual binary representation on the emulated
hardware. It basically just ignores it entirely. Dumping like this also
creates a problem with streams. The actual returned value is still more
then the nine bits that should actually be stored in the stream (whether
using int or long) thereby synthetically padding the stream with extra
bits. Analysis of this stream would therefore yield gibberish.
For example, if I am trying to debug a software involving serial
communications I might run the software and dump the generated output
from the serial pin to a file for validation and analysis. Dumping the
following three words:
100100110
001101011
101010111
the actual meaning for this example is irrelavant and for this example I
just added 1s and 0s. Whats is important is that the actual dump should
be:
100100110001101011101010111
The extra bits that I stored in the host computer which only works with
byte sized divisions must be removed or the end result would be something
like (again not calculated to be exact):
000000010010011000000000011010110000000101010111
which is gibberish in regards program running on the simulated hardware.
Yes, all of this detail may not be needed depending on what the OP is
trying to do. If it is needed, then I don't see any way around binary
manipulation.
Unless there is some devious problem to do with the OP's question, all
assumptions about the hosting computer can be avoided.
The OP didn't state his exact intentions or the hardware that is working
with. It may very well be that he only needs to the arithmatic values. If
so, great. Not knowing, I prefer to be safe and assume that he might need
a binary accurate model.
I can't see a compelling reason for them *not* to be the same. Of
course a bad choice of "virtual format" might give you no end of
trouble, but if you get that right, I can't see why the code could not
be portable.
I will define "internal format" as the normal C types which are actually
used for calculations, printed representations, etc. Everything done at
this level is perfectly valid, portable C. No implementation differences
as we are merely dealing with arithmatic values which will add, subtract,
divide, modulo, etc more or less the same no matter how they are
represented.
I will define "virtual format" as the format actually apparent to
the software running on the virtual machine; and, which for simulation
purposes should have a binary representation and sequential storage that
is close as possible to the actual emulated hardware. These values are
only used when importing data from I/O sources as the emulated hardware
would receive it at which time they would be converted to internal format
for host storage; when viewing the internal binary representation of the
emulated machines memory at which time they would be converted from the
internal format, when dealing with instructions of the simulated hardware
that are binary in nature (instructions for flipping single bits and
shifting registers are very common operations for MCU's and) and must act
on the data as apperant to the simulated hardare in which case they are
converted for the internal format and then recoverted back to the internal
format after the operation is completed, or when determining the pin
outputs of the MCU at which would again be determined from a conversion of
the internal format stored by the host computer.
Only the conversion routines ever know about the actual virtual format and
its differences from the host hardware. Everything else is portable. The
conversion routines must know how to convert the internal representation to
the virtual representation and vise versa. As the binary representation of
internal format data on the host may differ from the binary format of the
same value on the simulated machine, these routines may need to do binary
manipulation to achieve a proper conversion. This may be simple and
portable recasting and calculating rollover/clamps, it may be removing
buffer and combing entries of the internal format to make virtual formats
to make streams which do not align on even byte boundries, or it may mean
converting one sign format to another. Depending on the host machine and
its similarity to the simulated machine this may be possible with portable
code. But, other machines will require binary manipulation to meet these
goals making it implemenation dependant as C does not provide binary
standards for its numeric types.
The end result is that if one is conserned about a highly realistic
emulation of the simulated hardware and of the about portability of the
emulator, one should consider keeping the internal format and virtual
format separated. They may then place any implementation dependant
code in two conversion functions which allows the rest of the program
to ignore implementation specific details. If you don't need one or the
other, then my comments need not apply.