Just throw out a tutorial C for the ti83/84 calcs using SDCC! (Intended for linux)

1. Getting the needed tools
SDCC is a C compiler that targets many CPUs including the z80. The compiler is taking on major changes right now and fails to compile even its own libraries. So for now, I am showing how to do stuff with SDCC revision 6489. Make sure that you have gcc and boost headers installed. You can get the sources for SDCC with:

Code:

svn co http://sdcc.svn.sf.net/svnroot/sdcc/trunk/sdcc/ sdcc -r 6489

Once it grabs the source files, navigate into the sdcc/ directory created and run

if you want it to be installed to /usr/* and not /usr/local/*. Now, configure may fail if you don't have the needed dependencies. Linux distros are different, so I can't give specific help on all. Once you have configure finished, go ahead and run

Code:

make

This may take a while depending on your system. The first compile takes a lot of time as it does testing on the z80 and gameboy z80 ports. Once it finished, do

You don't need to do make. When you have hex2bin installed, you can proceed onto writing programs! (Don't get rid of your sdcc directory, you will need it)

2. Targeting and Using SDCC
SDCC is different from other C compilers. With GCC, you compile sources into binaries, usually no extension. SDCC compiles into .ihx files. These are not binary files, but can be made into them using hex2bin. It converts an .ihx to a .bin. That can be used with binpac8x or similar program to make a .8xp. Also, gcc -c will output .o files, sdcc will output .rel files.

SDCC has no concept of what it will run on. It is setup to make operating systems. How it formats programs is determined by a crt0.rel file. This file can be found in sdcc/device/lib/z80. Now, the .rel is the compiled form. The crt0, written in assembly, is stored in crt0.s. For simplicity, .s is a assembly source file.

Opening up the file, you will see that the assembly used is a different dialect. The assembler, ASxxxx (SDCC installs it as sdasz80), changes how numbers are written and a few other things. For simplicity, here is the given crt0.s file with comments:

Now, you can see that it is setup to work for an OS. Also, you see that .* statements use numbers like 0x100, but the rest use them as #*. I don't know why they are different, but you should at least know how to use numbers in assembly. #__ is for a regular number, like #10. #0x__ is for a hexidecimal number, like #0xA. #0b__ is for a binary number, and #. is for the current position. This file is compiled by using

Code:

sdasz80 -p -g -o crt0.rel crt0.s

That .rel would be used with SDCC to use as your header.

SDCC by default uses the premade header. If you have your own and made a .rel file, you can use

Code:

--no-std-crt0 crt0.rel

or whatever your .rel file is called. This is just a flag to SDCC, so lets see how to actually use SDCC

Right away, this would not compile, as we have 3 functions and a variable not known. Now, SDCC doesn't know what a B_CALL is, so you have to make a function yourself in assembly Here is how to do ClrLCDFull:

A few things to note. First, __naked means that SDCC shouldn't treat the function as C function. SDCC would normally setup IX so that it can access variables, but that isn't needed. The ret is needed because __naked really means SDCC doesn't touch it.

__asm and __endasm; are used to inline asm code. Simply that. SDCC doesn't make way for your code, so that's why I made this a function and not in the main function. SDCC backs up registers before calling functions.

Now, you can see that the first 2 functions didn't use parameters at all. PutS needs one. This is simple to do in asm, just ld hl,(text), but SDCC doesn't use registers for that, it uses the stack. This also requires you to understand how SDCC uses IX.

C uses the stack for variables. Local variables are added to the stack and calling functions pushes variables to the stack. (It isn't just SDCC, the language is that way ) In order to access variables, it uses offsets from IX to get to the very quickly. If you have a char buffer[50];, that is on the stack, being a local variable. It uses IX on that, as doing pop/push or hl (hl pointing on the stack) would be too much.

Ix is setup by SDCC by starting and ending a function like:

Code:

void function()
{
__asm
push ix
ld ix,#0
add ix,sp
_endasm;

// Code here

__asm
; Adds this on a return
; When using local variables:
ld sp,ix
; always adds this
pop ix
ret
__endasm;
}

A char as the first parameter would be, from the layout above, 4(ix). Note that _(ix) is how ASxxxx uses offsets. So, for our PutS function:

You can run this from TIOS, as from a shell, the program will return right away. Once you compile this program, you can use hex2bin to get a .bin file to use with binpac8x (from the DCS SDK, but you can use something else). This is my compile script:

4. SDCC with GlassOS
GlassOS is an OS that provides a perfect C environment. A builtin libc is provided along with hardware drivers, USB access, ion functions, and more. The OS is pending a release. Once the OS has been released, a full SDK will be made available along with code examples and explanations on already existing games and programs (calculator, grayscale game and a classic arcade game).

4.1. Writing code that uses GlassOS libraries and kernel calls
todo

4.2. Preparing a binary for sending to the calc
Unlike TIOS, GlassOS doesn't encapsulate data into special formats. For example, to send a program to the calc, you simply need to send the raw binary data over.

When compiling, you get a .ihx as the final result. Using hex2bin, you can create a .bin of the .ihx. For example, if you compile a program named pak, you can useCode:

hex2bin pak.ihx

and pak.bin will be created. That file can be sent to the calc and will be read as a program/library.

4.3. Files required to run programs on-calc
GlassOS's kernel, when starts, spawns the Launcher process. That program is in charge of displaying programs to the user in a quick and organized way. Simply putting binaries on the calc will not let them be organized on their own. Launcher uses .desktop files (limited versions) to know about programs. There are two directories that tell the OS about programs on the calc, /usr/share/applications and /usr/share/types. .../applications contains all of the .desktop files, and .../types contains files that describe filetype associations. /usr/bin contains the actual binaries. When sending a program to the calc, a program must have at minimum a binary and a .desktop file. Here is a typical layout of a package for the calc:Code:

The above would be a minimal layout of a package, which is read by the PC software and the individual files are sent to the calc using the FSA protocol.

The pak.desktop would contain:Code:

Exec=/usr/bin/pak.bin
Category=Other
Name=Pak!

This will cause Launcher to show an entry 'Pak!' under the Other category. If you want to manually send a program to GlassOS, you can use the File Browser in glassLink to manually send these two files to the calc. Once the files are on the calc, the Launcher must be restarted, and can be done by killing the Launcher process. This limitation will be addressed.

4.4. Making packages for the package manager
todo

5. More with Inline Assembly
todo

6. Accessing the Hardware Ports
SDCC lets you access hardware ports like an unsigned char. To access the ASIC version port, you can do:

Wow, thanks for posting this, this is excellent! I've long insulted SDCC for its bloated code; perhaps this will give me a chance to test out its latest versions and see how the optimization has been improved. An excellent guide indeed, and I look forward to you fleshing it out further!_________________

Yup. The only reason I say to use this revision (which is old) is because the new optimising code ... doesn't work. printf fails on "%02d" and printing floats, the port access isn't working, etc._________________RIP Monty Oum, 1981 - 2015
RoosterTeeth's Lazer Team!

From what I see, __attribute__ is gcc-specific and SDCC doesn't support it as it isn't based off of gcc. The above method is what is used by the devs in the sdcclib code. The variables being mentioned like that doesn't generate code - they are simply ignored when assembly is being generated._________________RIP Monty Oum, 1981 - 2015
RoosterTeeth's Lazer Team!

If you wait patiently, I'm sure AHelper0 will come and respond. In the meantime, have a topic to introduce yourself in: Introduce Yourself.

I duplicated that error message by having the file permissions set so that I cannot access it. (Assuming you are using *nix) chmod 775 tios_crt0.s will fix permission issues, but make sure that you can also write files in the folder. Also make sure that the file is in the same directory when you run the command.

Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.