Ndless features and limitations

From Hackspire
Revision as of 07:01, 14 March 2012 by Compu (talk | contribs) (Fixed typo)
Jump to navigation Jump to search

Ndless combines an executable loader and utilities to open the TI-Nspire to third-party C and assembly development.

This article covers advanced features of Ndless for developers. If you want to try C and assembly development on the TI-Nspire, start with the tutorial.

Some features described here require Ndless v1.7 or higher.

nspire-tools utility

Since v3.1 r541, nspire-tools can be used to execute various actions on your build environment.

  • nspire-tools new <program>: create in the current directory a Makefile to build a program called <program>.tns

Entry point

You can optionally define the standard argc and argv parameters in your main function definition to get the current program path in argv[0]:

int main(int argc, char *argv[]) {
  printf("Run from '%s'\n", argv[0]);
}

Global variables and initialization

Global variables are generally defined in the BSS section of the executable format.

BSS sections are currently not allocated dynamically by the program loader of Ndless: global variables will make your programs bigger so use them with care, switch to malloc() when necessary.

Global variables with initialization data, or variables with constant initializers which requires relocation (for example an array of function pointers, or a structure containing static strings) must be relocated manually with the non standard nl_relocdata() because of the limited features of the lightweight program loader.

int global_int;
int* global_array[] = {&global_int};
nl_relocdata((unsigned*)global_array, sizeof(global_array)/sizeof(global_array[0]));
printf("global_int=%i\n", *global_array[0]);

Syscalls

Syscalls are OS functions exposed by Ndless to C and assembly programs. Some syscalls are part of standard libraries and may be used to port programs to the TI-Nspire more easily.

To call a syscall in assembly, use syscall(<symbol name>).

If you have found an interesting syscall not defined in the latest version of Ndless, you can temporary define it locally to your program with SYSCALL_CUSTOM(). For example to call the syscall int internal_func(char *s) you have found, define in one of your program's header files:

static const unsigned internal_func_addrs[] = {0x10123456, 0x10654321, 0x10234567, 0x11234567}; // non-CAS 3.1, CAS 3.1, non-CAS CX 3.1, CAS CX 3.1 addresses
#define internal_func SYSCALL_CUSTOM(internal_func_addrs, int, char *s)

This is a temporary workaround which will force a re-build of the program each time a new OS version supported by Ndless comes out. You should suggest the syscall to the Ndless development team to make it standard, publicly available and maintained.

Functions alternatives

Some standard functions cannot or aren't yet provided by Ndless. Here are possible alternatives for these functions.

OS variables

Ndless also abstracts access to some OS variables.

To read from or write to these variables in assembly, use osvar(<symbol name>). The address of the variable is returned in r0.

Assembly source code

Pure-assembly programs must define the global symbol "main" as their entry point. See src/samples/hella/hella.S.

Always make sure that the assembly files extensions are in uppercase (.S) to let them be preprocessed by the C preprocessor on which Ndless include files depend.

Thumb state

The TI-Nspire ARM9 processor features a Thumb instruction set state which improves compiled code density with 16-bit instructions instead of 32-bit.

Only ARM state main() entry points is currently supported, but other source files may be built in Thumb state. Switching from one state to the other is transparent to C programs as long as the -mthumb-interwork GCC switch is used. To build a .c file in Thumb state, use a GCC command similar to:

test_thumb.o: test_thumb.c
 $(GCC) $(GCCFLAGS) --mthumb-interwork -mthumb -c $< -o $@

Calling syscalls in thumb state is supported by Ndless.

Assembly source files must declare the instruction set to use with the .arm and .thumb directives (ARM is the default). Switching from one mode to the other requires specific tricks.

Newlib

Newlib is an implementation of the C library intended for use on embedded systems, installed with the YAGARTO GNU ARM Toolchain.

Compatibility of Ndless with Newlib has not been tested. You may get definition conflicts and crashes due to Newlib running without the required relocation that Ndless doesn't provide. You should always build your programs with the nspire-ld flag -nostdlib, except if you need the single precision floating-point helper functions internally called by GCC when required (__aeabi_fadd, ...).

File association

File association is configured through the configuration file ndless.cfg.tns (see the User Guide). Once the file extensions supported by your program are declared in the file, your program is run with the argv[1] parameter set to the full path of the file open from the OS document browser. Check that argc >= 2 to know if the program is being run through file association. Always check the format of file open this way.

You may ask Ndless's maintainers to add your file assocation to the default ndless.cfg.tns file, if the extensions aren't already defined.

Resident programs

You can ask Ndless not to free the program's memory block after it exits by calling void nl_set_resident(void). This can be use for resident programs such as OS patches.

Builtin functions

Ndless exposes internal features and states throw the nl_*() functions.

  • BOOL nl_isstartup(void): (since v3.1 r540) returns TRUE if the program is currently being run at OS startup. See the User Guide.
  • void nl_relocdatab(unsigned *dataptr, unsigned size, void *base): see Gloval variables
  • void nl_set_resident(void): (since v3.1 r553) see Resident programs