With the demise and continued aging of MPLab (in favor of X), I am considering porting some of my .asm libraries to XC8. But I've got a dilemma:
Each of my libraries has a set of library-dependent global flags (boolean bits) that are packed into flag bytes. In assembly, I do this in a .inc file. The register location and position of the bits are irrelevant, and tracked by the label. For example:
I do this manually, and pack all of the required bits into the fewest number of registers as possible.
I could likely do something similar in C:
But I would need to define my globals on a project (as opposed to library) basis -- depending upon the libraries I wanted to use -- and pack the bits manually into one struct per flag register, with each flag register possibly spanning multiple libraries. This is sub-optimal.
Is there a way to define global bits in XC8 in the library header files so that they are automatically packed into the smallest possible number of compiler-allocated global bytes?
Interestingly, this is easy to do in Pascal (using sets). Can C do it?
Each of my libraries has a set of library-dependent global flags (boolean bits) that are packed into flag bytes. In assembly, I do this in a .inc file. The register location and position of the bits are irrelevant, and tracked by the label. For example:
Code:
#define pwrup flag0,0,0 ;0=normal/1=powering up
#define minboot flag0,1,0 ;0=normal/1=minboot
#define shtdwn flag0,2,0 ;1=Shutdown system
#define fsleep flag0,3,0 ;0=Normal Operation/1=go to sleep
#define update flag0,4,0 ;1=update display
#define fbcolon flag0,5,0 ;1=blink colon when clock runs
#define fcold flag0,6,0 ;1=temp too cold
#define fhot flag0,7,0 ;1=temp too hot
#define bdead flag1,0,0 ;1=Battery dead
#define _adrdy flag1,1,0 ;1=On-chip A/D conversion ready
#define run flag1,2,0 ;0=warmup/1=run
#define lbpdf flag1,3,0 ;low battery power down flag
#define _spst flag1,5,0 ;0=speaker lo, 1=speaker hi
#define ftest flag1,6,0 ;0=normal, 1=test mode
#define fsecond flag1,7,0 ;1=1 second elapsed
I could likely do something similar in C:
C:
struct {
unsigned pwrup :1; //0=normal/1=powering up
unsigned minboot :1; //0=normal/1=minboot
unsigned shtdwn :1; //1=Shutdown system
unsigned fsleep :1; //0=Normal Operation/1=go to sleep
unsigned update :1; //1=update display
unsigned fbcolon :1; //1=blink colon when clock runs
unsigned fcold :1; //1=temp too cold
unsigned fhot :1; //1=temp too hot
} flag0;
Is there a way to define global bits in XC8 in the library header files so that they are automatically packed into the smallest possible number of compiler-allocated global bytes?
Interestingly, this is easy to do in Pascal (using sets). Can C do it?
Last edited: