This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to enable heap in simulator?

Hello,

 

Is there any way to enable heap in simulator?

I would like to read some data from disk by using <stdio.h>'s file IO function. In the emulator I have succeeded, for example: 

FILE *fp;
fp=fopen("test.dat","r");

fp returns a valid pointer and all subsequent operations are smooth.

 

But once I switched back to simulator, fopen() seems to defunct and it always return 0x00000000 which is a NULL pointer.

I have found a number of posts in E2E discussing fopen() issue and this one seems to suggest that for file IO operations to work heap must be enabled.

I am using linker.cmd rather than DSP/BIOS.

linker.cmd beginning said:

-stack          0x00000800      /* Stack Size */
-heap           0x00000800      /* Heap Size */

MEMORY
{
 L2RAM:      o = 0x10810000  l = 0x00020000
  DDR2:       o = 0x80000000  l = 0x1000000
}
SECTIONS
{   
   .bss        >   DDR2
    .cinit      >   DDR2
    .cio        >   DDR2
    .const      >   DDR2
    .data       >   DDR2
    .far        >   DDR2
    .stack      >   DDR2
    .switch     >   DDR2
    .sysmem     >   DDR2  /*where heap and stack are placed*/
    .text       >   DDR2 

In EVM I specified heap size like this and all fopen()/fread()/fwrite() work successfully. With all files and settings intact, once I switched back to simulator, fopen() begins to return NULL.

Does simulator allow heap? How does simulator treat linker.cmd file? Does it ignore that? Or if .cmd file cannot be used, is there anyway to enable heap so that these file IO operations can be available?

 

 

Thanks,

Jim

 

  • Jim,

    What version of CCS are you using and which simulator? The console I/O routine you mentioned uses a software breakpoint to properly transfer the data to/from the host PC and I wonder if this is not properly working. If you have a short test case that can reproduce this error would you mind sending it?

    Just FYI, heap does not need to be enabled in the simulator, since it is entirely defined by the code generation tools and physically it is common memory.

    Regards,

    Rafael

  • Dear Rafael,

    My CCS version is 4.1.2.00027 and I am using 6437 little endian simulator.

    reading file said:

    #include <stdio.h>

    void main()
    {
     FILE *fp;
     
     fp=fopen("doc.txt","r");
     
     
    }

    fp returns NULL every time.

     

    Could you please check this attached project?

    5531.test_file_IO.rar

     

    Jim

     

     

  • Jim,

    I noticed the project you sent did not have any linker command file and therefore I modified it (check attached). Also, the executable (.out) runs in the subdirectory Debug, but the file <dir.txt> is in the project directory (one level above), therefore I pointed the parameter of fopen() to the parent directory.

    The code below works for me in both the simulator and the emulator in CCSv4.1.3.00038 (the version I have here with me). Can you test it with your version of CCS?

    test.c said:

    #include <errno.h>
    #include <stdio.h>
    #include <string.h>

    void main()
    {
     FILE *fp;
     int return_char;

     fp = fopen("..\\doc.txt","r");
     if(fp == NULL)
      perror("Error in fopen\n");
     else
     {
      while ((return_char = fgetc(fp)) != EOF)
       putchar(return_char);
     }
    }

    Hope this helps,

    Rafael

    test_file_IO.zip
  • Dear Desouza,

    They worked well both in simulator and emulator, thanks very much for checking and modifying them for me.

     


    Jim

  • Dear Rafael,

    Rafael said:

    heap does not need to be enabled in the simulator, since it is entirely defined by the code generation tools and physically it is common memory.


    I would like to ask some questions:


    1. The purpose of heap is for dynamic allocation, for example, used by malloc().
    2. That you said heap is "not needed" is simulator, is because the simulator, and in a deeper layer the operating system, has already allocated and is managing a reserved heap for us.
    3. It is because of the lack of software environments in 2, so that we need to manually specify heap size (-heap size) and location in linker.cmd.

    Could you verify each of the points for me?


    Another question is why does file accessing function such as fopen()/fread() require heap? Could you explain this to me?



    Jim

  • Jimmy,

    Jimmy Song said:

    I would like to ask some questions:

    1. The purpose of heap is for dynamic allocation, for example, used by malloc().
    2. That you said heap is "not needed" is simulator, is because the simulator, and in a deeper layer the operating system, has already allocated and is managing a reserved heap for us.
    3. It is because of the lack of software environments in 2, so that we need to manually specify heap size (-heap size) and location in linker.cmd.

     

    1. Yes

    2. No. The heap parameters (size and initial address) are entirely configured by the linker command file (the size can also be configured using the linker option -heap). Physically the heap does not differ from any other memory used in a regular program (code, data, stack, etc.). The device (simulator or hardware) just sees it as plain memory. When the executable file is loaded and executed on the device, the heap is allocated in the device memory before the code reaches main().

    If by operating system you mean Windows (or Linux, etc.), they do not influence the heap allocation in the TI device (simulator or hardware). If you mean an operating system in the device (DSP/BIOS, for example), then it is highly likely that it configures the heap for you.

    Check the references below that contain additional details and information.

    http://processors.wiki.ti.com/index.php/Code_Generation_Tools_FAQ#Q:_What.27s_the_difference_between_HEAP_and_STACK.3F

    http://processors.wiki.ti.com/index.php/Stack_and_Heap_size_requirements

    http://en.wikipedia.org/wiki/Dynamic_memory_allocation

    3. See above.

    Jimmy Song said:

    Another question is why does file accessing function such as fopen()/fread() require heap? Could you explain this to me?

    Please check

    http://processors.wiki.ti.com/index.php/Tips_for_using_printf#Heap_Size

    Hope this helps,

    Rafael

     

  • Anonymous
    0 Anonymous in reply to desouza

    Dear Rafael,


    Thanks very much for the explanation.

                

                        

    I was able to fread() and then fopen() a file, with neither

    1. linker.cmd, not to mention its heap specification
    2. Project->Properties->C/C++ Build->Linker->Basic Options->--heap_size: leaving blank

    Why the file operation can still success? The link you provided
    http://processors.wiki.ti.com/index.php/Tips_for_using_printf#Heap_Size

    explains why heap is needed for file IO. So does my situation (1 & 2) even I am leaving --heap_size blank, CCS still defauls a certain value, which is not shown here?


    And when options are specified both in linker.cmd and Linker->Basic Options, which value is actually used? Does the value in anyone overrides the value in another?

     

              

    Sincerely,
    Zheng

  • Zheng Zhao said:

    I was able to fread() and then fopen() a file, with neither

    1. linker.cmd, not to mention its heap specification
    2. Project->Properties->C/C++ Build->Linker->Basic Options->--heap_size: leaving blank

    If you don't specify anything, then the linker will use default settings (0x400 each for heap and stack)

    Zheng Zhao said:

    And when options are specified both in linker.cmd and Linker->Basic Options, which value is actually used? Does the value in anyone overrides the value in another?

    The linker *.cmd file will be used in this case.

  • Zheng,

    Just adding to Ki's response, to verify how much heap and stack are being actually configured (among other things) please check the linker map file <test_file_IO.map> in the output directory.

    Regards,

    Rafael

  • Anonymous
    0 Anonymous in reply to desouza

    Dear Rafael,

    test_file_IO.map said:

    .stack     0    000034e0    00000400     UNINITIALIZED
                      000034e0    00000008     rts6200e.lib : boot.obj (.stack)
                      000034e8    000003f8     --HOLE--

    .sysmem    0    000038e0    00000400     UNINITIALIZED
                      000038e0    00000008     rts6200e.lib : memory.obj (.sysmem)
                      000038e8    000003f8     --HOLE-- 

    ......

    00000400   __STACK_SIZE
    00000400   __SYSMEM_SIZE

    I could find .stack, but not .heap, nor does searching for "heap" in this .map file return anything. Is heap and system the same thing? Or if not, does this file really contain heap size information?

            

          

    Zheng

     

  • Anonymous
    0 Anonymous in reply to Ki

    Dear Ki-Soo,

    Thanks very much, this resolved many of my doubts.

     

    Zheng

  • Zheng Zhao said:
    I could find .stack, but not .heap, nor does searching for "heap" in this .map file return anything. Is heap and system the same thing? Or if not, does this file really contain heap size information?

    Yes. See Section 7.1.3 of the compiler user's guide: http://focus.ti.com/lit/ug/spru187r/spru187r.pdf

    Thanks

    ki

     

  • Anonymous
    0 Anonymous in reply to Ki

    Dear Ki-Soo,

    Got it, thanks very much.

    Still another question: What is the difference between an updated document and an older one?

    For hardware modules, once the chip is released, I think it is not going to be changed (is it the fact?). Therefore, are updated hardware documents merely errata? What else can such documents update? Does TI really update their released type of hardware, like DM6437 1.0, DM6437 1.1, DM6437 1.2... during the years when the product is still active (being sold on the market)? That sounds unlikely to me, since if a hardware is enhacned it should then get a new model name, for example, from DM6437 to DM6438, like IPhone 1, 2, 3G, 4, etc.

    Another possibility is that the new document version does not correspond to any update in the hardware, but just revision and correction on the previous released same document, containing less mistake (if the previous version had any mistake) than the previous version. Is this correct?

                      

    For new versions of software documents, the difference between a newer and an older one like:

    1. TMS320C6000 Optimizing Compiler v 7.2 Beta SPRU187R, October 2010, which you just gave the link to me
    2. and TMS320C6000 Optimizing Compiler v 6.0 Beta SPRU187N, July 2005

    Since compiler is always included as part of the newly released CCS version, then does new version of a document reflect change in the new CCS? This question also applies documents of software other than the compiler.

     

    Sincerely,

    Zheng

     

  • ideally, you'll want to use the document that applies to your compiler version. The link I gave you was the latest version so if you are using older versions, Not all content may apply. However the info on the heap has not changed so I knew that content was ok.

    This is a useful page on links to the user guides (including for C6000 6.0, 6.1)

    http://processors.wiki.ti.com/index.php/TI_Compiler_Information#Compiler_Manuals

    Otherwise I don't know too much about documentation versioning number and what exactly they mean. Sorry.

  • Also, it is good practice to ask new questions in a separate thread if the are unrelated to the existing thread. It helps people who search the forums for answers.

    Thanks!

    ki

  • Anonymous
    0 Anonymous in reply to Ki

    Dear Ki-Soo,

    Thanks for reminding. I will follow this in the future.

     

    Zheng