[comp.lang.c] Two variables for the price of one

cchase@ee.cornell.edu (Craig M. Chase) (08/26/90)

Hi, I'm working on a a project involving the direct manipulation of 
some hardware registers in some custom circuitry.  For reasons that 
I won't go into here, we've got a bunch of registers that work as
pairs.  What I mean is that (unless something goes drastically 
wrong) these registers will always have the save value.  When I 
write to one, I really want to write to both.

So... my question is, does anybody know any neat tricks in the 
C preprocessor to define a single name that will work with both
registers.  As a seed suggestion, let me tell you what I tried:

#define fancy_reg reg1 = reg2

So, if I do
   fancy_reg = 0xFFFF;


the preprocessor generates:
   reg1 = reg2 = 0xFFFF;

which is great.  

Problem is, if I try:
   if (fancy_reg == 0xFFFF)
I get:
   if (reg1 = reg2 == 0xFFFF)
and that doesn't do the right thing (== binds tighter than =, right?)

The only solution to this that I've seen is to always use parenthesis
around fancy_reg when I want to read the register pair, and avoid
the parenthesis when I want to write them.  This solution is hardly
preferable to manipulating the two registers independently.

Any suggestions?

Craig Chase

-- 
----------------------------------------------------------------------
Craig Chase            |         Cornell University
cchase@ee.cornell.edu  | Department of Electrical Engineering
----------------------------------------------------------------------

880716a@aucs.uucp (Dave Astels) (08/28/90)

In article <1990Aug26.144403.17638@batcomputer.tn.cornell.edu> cchase@ee.cornell.edu (Craig M. Chase) writes:
>So... my question is, does anybody know any neat tricks in the 
>C preprocessor to define a single name that will work with both
>registers.
>
>Any suggestions?

Yes.  Switch to C++ and encapsulate it in a class.

class FancyReg {
private:
    RegType rega, regb;
public:
    // standard ctors & dtors ...

    RegType operator = (RegType new_val) { return rega = regb = newval; }
    RegType operator () () { return rega; }
}

... or something like that ...

-- 
"I liked him better before he died" - McCoy, ST V
===============================================================================
Dave Astels            |  Internet: 880716a@AcadiaU.CA
PO Box 835, Wolfville, |  Bitnet:   880716a@Acadia