8051 bit number

pfeuh
2006-11-07
2013-03-12
  • pfeuh

    pfeuh - 2006-11-07

    Hello,

    Is it a way to know the number of a bit? I haven't find anything in the sdcc manual.... :o( For instance, I declare

    bit dummy;

    It it possible to know the address (or number) of dummy? It's really easy to do it parsing the .map file, but I have to do it during the execution of the program.

    Regards,

    Pfeuh

     
    • Frieder Ferlemann

      like this?

      ----8<--------------------------
      bit another_bit,my_bit;

      unsigned char f(void) __naked
      {
      __asm
              mov dpl,#_my_bit
              ret
      __endasm;
      }

       
    • pfeuh

      pfeuh - 2006-11-07

      Hello,

      Thats a good technique, I discover it, thanks. I would prefer something which takes the bit name as input and returns the bit number, like the other types of vars for instance:

      unsigned char toto, titi, tata, myvar ;
      unsigned int var_addr;
      var_addr = (unsigned int)&myvar;

      Regards,

      Pfeuh

       
      • Frieder Ferlemann

        Could you explain in which context you'd want the address?

        I'm not aware of any way to do this within SDCC without
        using inline assembly but I fear this one feels a little
        too much like hacking too:
        -----------8<-----------------------------------------------

        bit another_bit, my_bit;
        int __data b,a;

        #define PUT_ADDR_OF_BIT_VAR_INTO_DATA_VAR(dst, src) __asm mov _##dst, _##src __endasm;

        void f(void)
        {
          PUT_ADDR_OF_BIT_VAR_INTO_DATA_VAR (a, my_bit);
        }

         
    • pfeuh

      pfeuh - 2006-11-07

      Hello,

      A macro, thanks, you're too strong! :o) The context is quite simple, get or set some vars on a remoted 8051,  commands are sended by a PC on a COM port. I assume that with the bit number, I will have to get or set the bit in asm by all the byte. Thanks a lot!

      Regards,

      Pfeuh

       
    • Maarten Brock

      Maarten Brock - 2006-11-08

      All this work for nothing? The 8051 cannot easily get/set bits indirectly. The best you could do I guess is to calculate the byte address in which this bit resides and its bitmask and use that to get or set the bit.

       
    • pfeuh

      pfeuh - 2006-11-08

      Hello,

      That's how I will do it... There is perhaps a langage problem, I'm french and not so familiar with english langage. I'll post the code in a few time.

      Regards

      Pfeuh

       
    • pfeuh

      pfeuh - 2006-11-13

      Hello,

      >The best you could do I guess is to calculate the byte address

      Before calculating the byte address, I have to know the bit address. That was the subject of this thread. The solution Mr Frieder Ferlemann gave me works, I thank him, but it means that I have to manage a sort of dictionnary which I have to update each time I declare a bit. It's too heavy to manage.

      Regards,

      Pfeuh

       
      • Raphael Neider

        Raphael Neider - 2006-11-13

        If you use more than one bit, maybe a bitfield (in __data, __xdata, or wherever) helps?

        struct {
          char foo:1;
          char bar:1;
          char baz:1;
          char foobar:1;
          char reserved:4; // ;-)
        } mybits;

        &mybits would yield the byte address, the bit offset within would be known by design.
        For all I know of 8051 (which is near nothing), these "bits" cannot be passed as "bit", though.

        This approach may or may not be appropriate for you, I just thought one should mention it...

        Raphael

         
      • Maarten Brock

        Maarten Brock - 2006-11-13

        Maybe you should first tell us what you're trying to achieve. Why do you need to create a dictionary of all bit variables?

         
    • pfeuh

      pfeuh - 2006-11-15

      Hello,

      >Why do you need to create a dictionary of all bit variables?

      It is for get or set some vars on a remoted 8051-like, commands are sended by a PC on a COM port. dictionnary contents:
      varname
      vartype
      varaddr
      varsize

      It works perfectly well for all the datatypes except bits... It seems that '&bit' is not implemented in SDCC... The but of the game is to write a full transparent code, except putting the variables which will be shared with the PC in the dicco.

      Regards,

      Pfeuh

       
      • Maarten Brock

        Maarten Brock - 2006-11-15

        As you already found out the 8051 cannot access bit variables indirectly. That is also why SDCC does not support taking the address of a bit variable.

        You say it works perfectly well for other variables but how do you put them in the dictionary? Doesn't that require manual updates for every new variable too?

        I think you best not use bit variables at all. They bring more overhead than they take away. Either use bitfields like Raphael suggested or use char instead of bit.

        Maarten

         
    • pfeuh

      pfeuh - 2006-11-15

      Hello,

      >how do you put them in the dictionary?
      typedef unsigned char UByte;
      typedef const struct{
      UByte Name[20];
      void * Addr;
      UByte Segment;
      UByte Attrib;
      UByte Size;
      }VariableStruct;

      #define VARLIST_SIZE 19
      static  VariableStruct VarList[] = {
      {"id",&PROGRAM_NAME,SEGCODE,TYPE_STRING|ATTRIB_READ,0}
      ,{"var01",&var01,SEGRAM,TYPE_UCHAR|ATTRIB_READ|ATTRIB_WRITE,0}
      .../...

      There is of course a peace of code to communicate with the master.

      >Doesn't that require manual updates for every new variable too?
      Not exactly, you just have to put in VarList the vars you want to share.

      >8051 cannot access bit variables indirectly.
      8051 can access bit indirectly, not natively but with a few code. For the remoted function that I need, it's enough. SDCC can't. But I agree, I like to work with SDCC, so I give up to work with bits.

      Regards,

      Pfeuh

       
    • Maarten Brock

      Maarten Brock - 2006-11-15

      If you really want you can make two small changes to the SDCC sources to get this working.

      In support/Util/SDCCerr.c line 121 you can change the severity of error E_ILLEGAL_ADDR from ERROR_LEVEL_ERROR into ERROR_LEVEL_WARNING.

      And in src/SDCCast.c line 2878 you can remove the "goto errorTreeReturn;" after the warning was thrown.

      After that the following compiles:

      bit b;
      int a = (int)&b;

      into:

      mov    _a,#_b
      mov    (_a + 1),#0x00

      Before we might change this in the source tree you should file a feature request for it and we must decide if we really want to implement this.

       
    • pfeuh

      pfeuh - 2006-11-16

      Hello,

      >you should file a feature request for it
      It's done.

      >If you really want you can make two small changes to the SDCC sources
      I Thank you, but I haven't the competence to compile SDCC... Specially on Windows! I'll wait until the good update.

      Regards,

      Pfeuh

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks