Hot!lookups on a const LUT gives 0 with XC8 v1.44

Author
Columba
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2017/11/13 13:25:54
  • Location: 0
  • Status: offline
2017/11/14 12:41:57 (permalink)
0

lookups on a const LUT gives 0 with XC8 v1.44

Compiler is XC8 v1.44 and my target is PIC18F26K80
I would like to store some LUTs in ROM.
When I store a LUT (~250 items) in RAM like this:
 
  uint16_t pwr3[] = {
    /*0000*/ 0,
    /*0001*/ 1,
    /*0002*/ 8,
    /*0003*/ 27
    //...
  }
 
The builded code works as expected, when x=3; pwr3[x] gives 27.
The moment I've added the const qualifier like this:
 
  const uint16_t pwr3[] = {
    /*0000*/ 0,
    /*0001*/ 1,
    /*0002*/ 8,
    /*0003*/ 27
      //...
  }
 
all the lookups result zero.
I've just upgraded from XC8 v1.41 to v1.44 but the issue remains.
Any kind of datatypes stored in a const LUT seems to be affected like described; uint8_t, char, uint32_t, function pointers,...
 
Thanks a lot,
#1

8 Replies Related Threads

    DarioG
    leaving this planet
    • Total Posts : 53071
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/14 15:25:00 (permalink)
    +2 (2)
    This could be due to the NVMREG errata - search the forum for it

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 14959
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/14 15:45:03 (permalink)
    +1 (1)
    It sounds like it, but is not listed:
    http://ww1.microchip.com/...ta%20-%2080000519N.pdf
    #3
    jtemples
    Super Member
    • Total Posts : 10278
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/14 15:52:41 (permalink)
    +1 (1)
    The K80 family is old.  Only the K40 family is affected by the NVM issue.
    #4
    davekw7x
    Entropy++
    • Total Posts : 1237
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/14 17:25:29 (permalink)
    +3 (3)
    Columba
    ...
    The moment I've added the const qualifier like this:
     
      const uint16_t pwr3[] = {
        /*0000*/ 0,
        /*0001*/ 1,
        /*0002*/ 8,
        /*0003*/ 27
          //...
      }
     
    all the lookups result zero.

     
    That's the way to do it.  How did you test?
     
    Here's an abstract from a test on my PIC18F26K80:
    void init_system(void);

    const uint16_t pwr3[] = {
            0, /*  0 */
            1, /*  1 */
            8, /*  2 */
           27, /*  3 */
           64, /*  4 */
          125, /*  5 */
          216, /*  6 */
          343, /*  7 */
          512, /*  8 */
          729, /*  9 */
         1000, /* 10 */
        //... Other entries for 11**3 through 40**3

    };

    void main()
    {
        // Initialize clock, timers, uart, etc...
        init_system();
        printf("\nCompiled on %s at %s UTC with XC8 version %u\n", __DATE__, __TIME__, __XC8_VERSION);
        int i;
        for (i = 0; i < 11; i++) {
            printf("pwr3[%2d] = %5u\n", i, pwr3[i]);
        }
        // Other stuff

     
    Output:
     
    Compiled on Nov 15 2017 at 00:16:25 UTC with XC8 version 1440
    pwr3[ 0] =     0
    pwr3[ 1] =     1
    pwr3[ 2] =     8
    pwr3[ 3] =    27
    pwr3[ 4] =    64
    pwr3[ 5] =   125
    pwr3[ 6] =   216
    pwr3[ 7] =   343
    pwr3[ 8] =   512
    pwr3[ 9] =   729
    pwr3[10] =  1000


    My conclusion:
     
    Either...
        Something else in your program is screwing the pooch.  (What we don't see is more important than what we see from your post.)
     
    Or...
        Your test plan is not adequate.  (What you see is not actually going on inside the program.)
     
     
    Regards,
     
    Dave
     

    Sometimes I just can't help myself...
    #5
    qhb
    Superb Member
    • Total Posts : 6255
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/14 18:05:22 (permalink)
    +3 (3)
    davekw7x
    ...
    Either...
        Something else in your program is screwing the pooch.  (What we don't see is more important than what we see from your post.)
     
    Or...
        Your test plan is not adequate.  (What you see is not actually going on inside the program.)

    +1
    The problem is in the code that the OP did not post.
    Possibly the table is being accessed via a pointer that XC8 thinks points to RAM.
     
    #6
    Columba
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2017/11/13 13:25:54
    • Location: 0
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/17 03:29:06 (permalink)
    +2 (2)
    Hi all,
     
    first things first, thanks for being very responsive and pushing me.
    It took me some time to understand what was going on.
    I've tried similar code as davekw7x proposed in a new project and indeed (of course) it is ok.
     
    But the moment I've added a lookup on a const LUT in my project it failed. Which confirms qhb's post :-)
    To make a very long story short... one of the first things I do in my project after initializing the clock is retrieving the profile and source address of my device by reading the userid. At the moment I wrote it there was no "real support" for this, so it was written in assembly.
     
    And now it comes: I manipulated the TBLPTR to access the userid but I didn't restore it! Which affects the lookups on the const LUTs.
     
    Now I do (restore the TBLPTR) and the lookups on the const LUTs are working like a charm.
     
    Let's have a coffee,
     
    Cheers,
    #7
    mlp
    boots too small
    • Total Posts : 513
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: Microchip XC8 and XCLM team
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/17 08:32:18 (permalink)
    +4 (6)
    (To the OP: not a specific criticism of you, but a general lesson.)
    Columba
    And now it comes: I manipulated the TBLPTR to access the userid but I didn't restore it!

    This is why it's best to show a complete program which demonstrates the problem.
    When you can't find the cause of a problem, you are by definition not best placed to assess which parts of your code are relevant to the solution.
     

    Please, before you post, read the Forum Guidelines
    To get a useful answer, always state which PIC you are using!
     
    Mark (not paid to state the opinions of Microchip Technology Inc.)


    #8
    DarioG
    leaving this planet
    • Total Posts : 53071
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: lookups on a const LUT gives 0 with XC8 v1.44 2017/11/17 08:37:11 (permalink)
    0
    Oh yep, it happened to me too, long ago Smile

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #9
    Jump to:
    © 2017 APG vNext Commercial Version 4.5