Menu

Help understanding memory usage on MCS51

Help
2016-12-10
2016-12-12
  • Juan Castillo

    Juan Castillo - 2016-12-10

    Hello all,

    I'm having some trouble understanding how SDCC allocates memory on MCS51. I am using the large memory model. No variables should be placed on internal memory, except for spilled locations. Everything ok here. The troube is that sometimes I get the error "Could not get xx consecutive bytes in internal RAM for area DSEG". This should indicate that I run out of internal memory (bit surprising since I am using the large memory model, but ok). If I check the memory report I get something like this:

    Internal RAM layout:
          0 1 2 3 4 5 6 7 8 9 A B C D E F
    0x00:|0|0|0|0|0|0|0|0| | | | | | | | |
    0x10:| | | | | | | | |c|c|c|c|c|c|Q| |
    0x20:|B|B|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
    0x30:|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|a|
    0x40:|a|a|a|b|b|b|b|b|b|b|b|b|b|b|b|b|
    0x50:|b|b|b|d|d|d|d|d|d|d|d|d|d|d|d|d|
    0x60:|d|d|d|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0x70:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0x80:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0x90:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xa0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xb0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xc0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xd0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xe0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0xf0:|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|S|
    0-3:Reg Banks, T:Bit regs, a-z:Data, B:Bits, Q:Overlay, I:iData, S:Stack, A:Absolute
    
    ERROR: Couldn't get 37 bytes allocated in internal RAM for area DSEG.
    Stack starts at: 0x63 (sp set to 0x62) with 157 bytes available.
    
    Other memory:
       Name             Start    End      Size     Max     
       ---------------- -------- -------- -------- --------
       PAGED EXT. RAM                         0      256   
       EXTERNAL RAM     0x0000   0x0a29    2602    65536   
       ROM/EPROM/FLASH  0x0000   0x914f   37200    65536   
    

    From the SDCC user manual: "By default the 8051 linker will place the stack after the last byte of (i)data variables". So if I understood correctly, in this case SDCC has enough internal memory to allocate those 37 bytes, it just needs to reduce the stack (which is 157 bytes size, so plenty of space). Why is SDCC not doing that? Am I missing something?

    Thanks in advance.

     
  • Maarten Brock

    Maarten Brock - 2016-12-11

    The linker needs 37 bytes between 0x63 and 0x7F (the last possible DATA address) and that obviously doesn't fit. The stack is allocated afterwards in the remaining DATA+IDATA space and thus uses the thereby unused space from 0x63 up.

     
  • Juan Castillo

    Juan Castillo - 2016-12-12

    Hello Maarten,

    Thanks for the clarification. I have just read somewhere that spilled locations cannot be allocated in IDATA, that is the part I was missing. Just out of curiosity, is it an SDCC restriction or a limitation of the 8051 architecture? How could I reduce the number of spilled locations?

     
  • Maarten Brock

    Maarten Brock - 2016-12-12

    Switch to small memory model? Or maybe medium? Or use stack-auto which places them on stack and thus can use all of IDATA.

    Since large model uses XDATA and thus always needs the one and only DPTR to access everything, SDCC likes to spill a lot to improve the code.

    The best code is usually generated when only large arrays and structures are placed in xdata explicitly and rest is automatically placed in data by using the small model.

     

Log in to post a comment.

Auth0 Logo