To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 2260
2259  |  2261
Subject: 
Re: GNU C wizards?
Newsgroups: 
lugnet.robotics
Date: 
Mon, 4 Jan 1999 18:18:06 GMT
Original-From: 
dave madden <DHM@PARADIGM.spamcakeWEBVISION.COM>
Viewed: 
1208 times
  
On Sun, 3 Jan 1999, I wrote:
=>>
=>> Are there any GNU C/C++ wizards out there?  I'm trying to write an
=>> asm() statement to describe the H8's bclr/bset instructions.  I have a
=>> wicked sweet tooth for C++ syntactic sugar, and I'd like to be able to
=>> say (for example):
=>>
=>>     Port6.Bit3 = true;
=>>
=>> and have it turn into:
=>>
=>>     bset #3,@0xbb:8
=>>
=>> [Actually, I'd *really* like to say: "Port6[3] = true;" but after
=>> beating my head against the compiler for an hour or two, I don't think
=>> it's going to happen.]

Then "John A. Tamplin" <jat@Traveller.COM> replied:
=>
=>Actually, you can do that -- look at the STL implementation for
=>BitVector<bool>.
=>[...]
=>This is similar to the STL implementation using a helper class for the
=>assignments.  All you need to do is pass a reference to the port object
=>into the bit when you create it (in the subscript opererator), and then
=>the helper object calls members on the Port object when you access the
=>value (via cast operators) or assign the value (via the assignment
=>operator).

I'd tried doing this, but the generated code (-O6) only infrequently
reduces to "bset #n,...", and never to "bset #n,@xx:8".  (It also
seemed to allocate/construct stack space for the Bit sometimes...)  If
you need to pass Bit references around, of course, then you have to do
something like BitVector<T>, but for simple linear code, keeping the
port backreference and and mask around just make the compiler work
harder.

Anyway, I kept at it a little longer last night and got the templates
working in the "Port6.Bit3 = true; if (Port6.Bit5) do_something( );"
mode.  The DDR access is clumsier because, as you note, the register
is write-only, so you have to keep a shadow copy around.  I'll take
another shot at expressing things as arrays and post the best version
I can come up with.

=>I don't think you need to use asm - the compiler will use those
=>instructions.  Just do it in C++:
=>  value|=(1<<bit) to set
=>  value&= ~(1<<bit) to clear

I saw "bset" and "bclr" generated for these types of statements in
some places, but my test case (Port6[3] = true;) resulted in

mov.b @0xffbb,r2l ; (this is from memory...)
or.b #x08,r2l
mov.b r2l,@0xffbb

Wastes space, and worse, throws off the timing.  (Although the LCD
driver doesn't seem to be *that* sensitive to those delay loops
getting shortened.)

BTW, won't the "-r" option to gnu-ld work for your load-on-the-fly
problem?  The info page says:

`-r'
`--relocateable'
     Generate relocatable output--i.e., generate an output file that
     can in turn serve as input to `ld'.  This is often called "partial
     linking".  As a side effect, in environments that support standard
     Unix magic numbers, this option also sets the output file's magic
     number to `OMAGIC'.  If this option is not specified, an absolute
     file is produced.  When linking C++ programs, this option *will
     not* resolve references to constructors; to do that, use `-Ur'.

d.
--
Did you check the web site first?: http://www.crynwr.com/lego-robotics



Message has 1 Reply:
  Re: GNU C wizards?
 
(...) GCC has in general done a very good job removing unnecessary code from templates. You can certainly optimize the generated code further, but you are getting the facilities of a high-level language in exchange for the (usually) small losses in (...) (25 years ago, 4-Jan-99, to lugnet.robotics)

Message is in Reply To:
  Re: GNU C wizards?
 
(...) Actually, you can do that -- look at the STL implementation for BitVector<bool>. (...) This is similar to the STL implementation using a helper class for the assignments. All you need to do is pass a reference to the port object into the bit (...) (25 years ago, 4-Jan-99, to lugnet.robotics)

6 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR