Log in

View Full Version : Weird Stuff in ifi_startup.c


ScottWolchok
26-10-2003, 16:21
I was poking through the default code in an effort to determine if we could program the Master PIC as well as the User PIC, and I found this odd stuff thither.
void
_entry (void)
{
_asm goto _startup _endasm

}
#pragma code _startup_scn
void
_startup (void)
{
_asm
// Initialize the stack pointer
lfsr 1, _stack lfsr 2, _stack clrf TBLPTRU, 0 // 1st silicon doesn't do this o
n POR
bcf FPFLAGS,RND,0 // Initialize rounding flag for floating point libs

// initialize the flash memory access configuration. this is harmless
// for non-flash devices, so we do it on all parts.
bsf 0xa6, 7, 0
bcf 0xa6, 6, 0
_endasm

loop:

Clear_Memory();
_do_cinit ();
// Call the user's main routine
main ();

goto loop;
} /* end _startup() */
Mmkay, so I look at this and I'm really lost. First of all, what does the underscore at the start of all these functions signify? Second, isn't assembler in C done like a function call? Thirdly, is that entry function even necessary? And last, I thought C programs started with the main function, but this code calls it instead... @_@

FotoPlasma
26-10-2003, 17:15
First of all, "thither", in modern English, translates to "to there".

Sorry, I just like to nitpick English. More to the point...

In Microchip's PIC C, assembly language programming is done using "_asm" as a starting point, and "_endasm" as an endpoint. Anything between these two points is interpreted as assembly language programming. As for the functionality of the code you're referencing, I have no idea. A cursory inspection leads me to believe, as you do, that the _entry function isn't necessary, but I didn't write the stuff. In the _startup function, the lines between the "_asm" and "_endasm" lines start with the PIC instruction mnemonics, and are followed by arguments. BCF and BSF are "bit clear following" and "bit set following", respectively, and LFSR is "Move literal (12-bit) 2nd word to FSRx1st word". The entire instruction set is well-documented in the PIC18F8520 datasheet.

I'm sorry that I don't know the specifics, and can't be very helpful.

Paladino
27-10-2003, 12:42
You are correct that in c most programs start with a main(), but that assumes that the Operating systems is present. In embedded systems with no OS _entry is the address that the CPU jumps to at power-up. The entry point usually contains processor initialization code. Since this code must run without the CPU being fully setup the developer usually will use ASM as the c runtime libraries are not available yet. You will notice that this code sets up the stack pointer which points to the memory location that stores function arguments and local variables, initializes floating point, configures memory and then calls _do_cinit () which initializes the c runtime so that c code can be used. The jump to main() is where the c code starts.


Rich

Dave Scheck
28-10-2003, 14:47
Originally posted by ScottWolchok
First of all, what does the underscore at the start of all these functions signify?

Scott

The underscore in the function name is usually used to signify an internal library function. This is a design decision that provides a few benefits to the end programmer.

The first is that it is easy to identify the functions that are provided.

The second is that the library authors are able to use meaningful names within their functions, yet they don't take valid function names away. For example, in your example above, if the library used entry() instead of _entry(), the end programmer may not be allowed to write a function named entry.

Hope this makes sense :)

Tom Saxton
22-08-2004, 15:21
Mmkay, so I look at this and I'm really lost. First of all, what does the underscore at the start of all these functions signify? Second, isn't assembler in C done like a function call? Thirdly, is that entry function even necessary? And last, I thought C programs started with the main function, but this code calls it instead... @_@

Although I like to avoid broad generalizations, I believe *every* C program begins with code that runs before "main". There's a bunch of stuff that has to happen before "main" gets called. For example, globals variables have to be initialized (either with explicit values, or with zeros if none are specified) which may involve copying values from ROM (or an executable file) to RAM. Things are even more complex if running under a full OS, where the addresses of functions in external libraries have to be found and written into the program's RAM image or in-memory tables.

Doing this work is very dependent on the architecture, so special code has to be written for each platform (Macintosh, Linux, Windows, Edu RC, etc), generally in assembly. This is done by some combination of the OS folks and the compiler support programmers since some is specific to the OS and some is specific to the language being used.

Thankfully, C programmers generally don't need to know anything about that preamble code except in very unusual circumstances.