workaround for a pointer to bit

  • 1
  • 2
  • 3
  • Page 1 of 3
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

workaround for a pointer to bit

 · 
Posted: 20.06.2014 - 02:53  ·  #1
Compiler does not allow me to have bit type in a record, or a pointer to bit type. Looking at bit type of DI_1 described in compiler generated U file I have found that this:
Code
DI_1 [@PORT_STABLE1, 0]: bit;

is translated to a compiler friendly form like this:
Code
VAR       DI_1, BIT8, RAM, $F0000;55;

As expected, it seams that it is not a simple pointer but a record of a byte pointer and a bit number in that byte. I am trying to trick compiler to accept this bit (or bit8) type in a array of records (as const or structconst), but without any success at all. I could store this bit8 type in a record as word and byte but I do not know how to cast to/from it.

I would like to have something like this:
Code
type
  PBit = pointer to bit;
  p = pointer;
  TTagStruct = record
    No : word;
    Tag: PBit;
  end;

var
{$PDATA}
  DI_1[@PinA, 0]: bit;
  DI_2[@PinA, 1]: bit;

const // or structconst
  MX: array[1..2] of TTagStruct = ((No: 8; Tag: @DI_1),
                                   (No: 23; Tag: @DI_2));

and then later in code to use it for read/write from/to that bit:
Code
  p := pointer(MX[1].Tag); // ptr to bit type in ram
  if p^ then
    WriteLn(Serout, 'that bit is on');
  else
    WriteLn(Serout, 'that bit is off');
  endif;

I already have this working with a pointer to a byte type in ram and eeprom, but bit type is giving me a headache.

If anyone has any idea I am all ears... :argue:
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 20.06.2014 - 13:04  ·  #2
Hi Avra.

The processor is a byte based machine so doesn't really handle anything smaller than a byte natively without operations like and or xor. The compiler handles this for you but it means that within a record the field must be a byte. I tend to use bitsets in this situation, so

Code
if MyBit in Rec[n].Bitfield then...

would be the logical test, or

Code
if Bit(Rec[n].Bitfield, n) then...

You can put this inside a function to make it more readable, like this

Code
function IsBit( n ) : boolean;
begin
  return( MyBit in Rec[n].BitField );
end;

then

Code
if IsBit(n) then...


but I find the 'in' method quite readable.

But I agree, something like

Code
TMyRec = record
  Field1 : byte; 
  MyBit[@Field1, 0] : bit;
end;

would be nice.

Own Opinions Only
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 20.06.2014 - 15:31  ·  #3
Hello Merlin,

I am aware of bitsets but they are not applicable in my case. I am building a communication protocol where some of the supported commands read and write digital and analog tags (real IO signals and runtime variables), and some settings stored in eeprom. I have already built supporting structures and everything works well for byte and higher types. For example I have a const array of records which holds implemented tag number, it's storage type (ram, eep), and a pointer to a tag in ram (like I[@PinA]) or to a tag in eep. Now for reading and writing them I simply look up for a tag number, and if found I get/set it's value based on tag storage type (ram/eep). Whole list of implemented tags is stored in a single const array, and both getting and setting functions use the same list. Unfortunately with bits, if I do not find a solution I will have to avoid this elegant const array solution and I will have to hard code separate case.. endcase for gettings bit tags, and separate case..endcase for setting tags, and to maintain the same list at two places in code. Very ugly, so I am all ears for any helpful workaround suggestion or AvrCo improvement in this field.
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 20.06.2014 - 17:15  ·  #4
Hi Avra.

Nice technique, but clearly impossible for bits. The '@' pseudo operator is a word (in smaller processors such as a mega128) but you need at least 3 extra bits to define the address of a bit, so the address of a bit cannot be stored in a constant in this way. The compiler handles bit fields in a special way, even though the syntax looks similar to overlaying it is handled quite differently.
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 20.06.2014 - 21:57  ·  #5
Hello Merlin,

I still think that it could be done. If we look at a bit type as a structure of one word (for address) and one byte (for a bit number), then pointer to a bit type would be a pointer to a structure that is always the same for all bit types, so dereferencing it should be possible. In my imaginary example shown above DI_1 already exists and is declared, and I want to store a pointer to DI_1 into const array element. That could be done if a pointer to bit or bit8 type would be possible as in my example. Then in runtime I just dereference the pointer to bit/bit8 type to get or set value of this bit. Simple and clean.
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 23.06.2014 - 14:49  ·  #6
Hi Avra.

Not sure that I am quite clear on what you are saying. If you are saying that you provide the bit number as an extra field in the structure that you maintain yourself, then yes, it can be done fairly easily using built in functions bit and setbit. However, if you are saying that the compiler should do it for you, then I have to say it seems a lot of work for Rolf for very little gain.

If you want some detailed help on how to do it, using the current compiler, drop me a pm.

OOO - Own opinions only
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 23.06.2014 - 20:40  ·  #7
Quote by Merlin
However, if you are saying that the compiler should do it for you, then I have to say it seems a lot of work for Rolf for very little gain.

I was kind of hoping for some quick hokus-pokus on the Rolf side and enabling what seamed to already exist in the compiler but not yet available for general use, but if it's too much work I will maintain the bits different then other types and code each bit manually with case..endcase. Having bit type in a record was nice idea, but sometimes you just can't have it all.

Thanks!
Merlin
Administrator
Avatar
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 23.06.2014 - 21:25  ·  #8
Hi Avra.

I don't think you need case statements.

Code

Type
  tAddress = record
    Tag: byte;
    Location : Word;
    Bit : byte;
  end;

...

var
  x : tAddress;

  SetBit( x.Location, x.Bit, Value );
  
  • 1
  • 2
  • 3
  • Page 1 of 3
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   121   135 · Page-Gen-Time: 0.034166s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI