Page 1 of 1

Cross Compiling Endian Issues - How To Avoid

Posted: Wed May 16, 2012 11:27 pm
by Fred
Spudmn, you did some cross compiling of the firmware recently, and noted endian-ness issues. How can we avoid these? Are they a product of masks? What if we used bit fields instead? Educate me :-) I'm curious, this is your thread, go! :-)

Re: Cross Compiling Endian Issues - How To Avoid

Posted: Thu May 31, 2012 1:28 am
by Spudmn
The only endian problem I have found in FreeEMS so far is with the “typedef union”

Code: Select all

typedef struct {
        unsigned thisPair: 4;
        unsigned lastPair: 4;
} twoPairs;
 
typedef union {
        twoPairs pairs;
        unsigned char pattern;
} match;
 

When matches.pattern is used on a PC (Unit Testing) the answer is nibble swapped.

The hack I used to get around this was to do this.

Code: Select all

#ifndef UNIT_TEST
typedef struct {
                unsigned lastPair: 4;
                unsigned thisPair: 4;
} twoPairs;
 
typedef union {
                twoPairs pairs;
                unsigned char pattern;
} match;
#else
 
typedef struct {
        unsigned thisPair: 4;
        unsigned lastPair: 4;
} twoPairs;
 
typedef union {
        twoPairs pairs;
        unsigned char pattern;
} match;
 
#endif

There is another option that uses macros to swap the byte order if required. This could be done so that there are no extra instructions for code compiled for the HC12

Code: Select all

#if(__BYTE_ORDER__)&&(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
// PC Unit Testing
#elif (MC6811)
#define BIG_ENDIAN
#else
#error Unknown Byte Order
#endif

#if BIG_ENDIAN
   #define READ_UNION_8(x) (x)
#else
  #define READ_UNION_8(x) ((((x) & 0xF0) >> 4) | (((x) & 0x0F) << 4))
#endif

//Example of use

if(READ_UNION_8(matches.pattern) == MatchedPairMatchedPair){      //         | small | small | small | - All periods match, could be anywhere, unless...
....
}
I have updated the unit test example to use the macro above.

https://github.com/Spudmn/Missing_teeth_Unit_Test

Re: Cross Compiling Endian Issues - How To Avoid

Posted: Thu May 31, 2012 1:38 am
by Fred
I just went through your repo....

Not happy!

.....

We need to get this integrated into the main repo :-)

I LOVE it! Genius work, and clean! 10/10!

OT: Talk folder structure with me. How to integrate it into the existing setup with its own make file etc? Lead the way!

<3

Fred.

Re: Cross Compiling Endian Issues - How To Avoid

Posted: Thu May 31, 2012 1:41 am
by Fred
Forked, cloned:

Feature request: Make file for CLI build please! :-)

make
make test
<results>

? You said you were busy, so no rush, but I'm keen to play!

Fred.

Re: Cross Compiling Endian Issues - How To Avoid

Posted: Thu Jun 14, 2012 9:44 pm
by Fred
Keen to setup a framework for decoder testing with CSV data input from saleae dump (or similar) such that we can run it through in a more bulk format. For example, I have some saleae logs of a 30-2 setup that I couldn't get a clean sync on. Peter has some logs of a bad pattern that he shouldn't get sync on, and so on. If we could extend that to all decoders in some low effort way, that'd be great. It'd also rule out the need for a JimStim/FredStim like device.

Disclaimer: I've had a couple of ciders.

Fred.