Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

difference between main() and alt_main() ?

My customer NIOS2 config is : cyclone 1C20, SDRAM, EPCS4. And SDRAM memory base is locked at 0x0. Reset address and Exception address is set to SDRAM.

When I debug hello_led.c in my board, program can break at the entry and every is OK. But if I debug hello_world.c in my board, program can't stop at the entry and I can't continue run the program. The entry of hello_led.c is alt_main(), and heloo_world.c is main(), What is difference between alt_main() and main()? and What is wrong with my board?

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I've got a new clew. When I check "Reduced device drivers" in syslib, everything goes well.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    In short the difference is that main() does initialization for you then calls alt_main() (if you have the compiler spit out an object dump file you'll see both if you use main).

    So if you're confident enought to do any initialization yourself then you can speed up your time to live of the NIOS II by using alt_main(). I think alt_main() is a unix thing if I'm not mistaken (I'm not used to them cause I'm just a windows programmer).

    Cheers
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Actually write you're incorrect, alt_main is a pre-main entry point for those who wish to initialise their system themselves. If you look at Page 4-28 of the Nios II Software Developer's Handbook this is coverred in detail.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I haven't looked at those two sample codes, but the led one probably uses alt_main since it's pretty simple to write to leds, hello world would use main since it need to set up a lot more (Jtag UART).

    If you have ever done assembly programming in NIOS I think of alt_main as the assembly version you would do without all the extra overhead involved, and the main version as what the C compiler would spit out (with all kinds of overhead). If you need a fast start out of reset and want to save memory then alt_main is the way to go (if you know what you are doing with the setup of other devices attached to the NIOS II)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Please read the manual to clear up the confusion.

    Below is a paste from the manual which may clear this up.

    "The ANSI C standard defines a hosted application as one that calls

    main() to begin execution. At the start of main(), a hosted application

    presumes the runtime environment and all system services are initialized

    and ready to use. This presumption is the case with the HAL system

    library. In fact, the hosted environment is one of the HAL’s greatest

    benefits to new Nios II programmers, because you don’t have to consider

    what devices exist in the system or how to initialize each one; the HAL

    automatically initializes the whole system.

    The ANSI C standard also provides for an alternate entry point that

    avoids automatic initialization, and assumes that the Nios II programmer

    manually initializes any hardware that is used. The alt_main()

    function provides a free-standing environment, giving you complete

    control over the initialization of the system. The free-standing

    environment places upon the programmer the burden of manually

    initializing any system feature used in the program. For example, calls to

    printf() do not function correctly in the free-standing environment,

    unless alt_main() first instantiates a character-mode device driver, and

    redirects stdout to the device."

    In summary if you don't understand what alt_main is you probably should not be using it.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ooops make that should not. If you don't understand alt_main you probably should not be using it