Looks like I'll need help. atleast some.

Started by Stephane Richard, January 04, 2007, 11:26 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Stephane Richard

I've been reading everything that came with the CC65 distro, I think I'm basically missing the obvious here.  Just for the sake of conversation (and my own remaining sanity lol).

Could someone that knows give me the steps to making a program (a simple hello world program would probably do) in CC65 and the steps to compiling it and executing it?  Remember I use WinVICE :-).

Seems I'm lost somewhere between the coding and the prg execution ;-).  And I can't seem to find anything to make smoke signals lol.
When God created light, so too was born, the first Shadow!

MystikShadows

nikoniko

First of all, I'll assume you've downloaded the C128 specific files and extracted them to wherever you installed cc65 (probably in Program Files\cc65 if you accepted the defaults). If you haven't, then you'll need to do that before going any further. And if you haven't rebooted at all since installing cc65, go ahead and do so, because that will add cc65 binaries to your path and set some important environment variables to make things easier.

Your directory structure should look like this:

asminc
bin
doc
emd
include
joy
lib
mou
ser
tgi

Go ahead and make a source or examples directory somewhere on your harddrive. It doesn't need to be under the cc65 since cc65 will be in your path and accessible from anywhere.

Now, go ahead and make your test program. Assuming you'll start with the traditional Hello World, try this:

hello.c

#include

int main (void)
{
     puts ("Hello World!");
}

Now, in the same directory as hello.c, type the following at the Command prompt:

cc65 -O -t c128 hello.c

If you get an error, you probably don't have path and the include directory set in your environment variables. It should be there, though, if you rebooted after installing cc65. If not, you can set the include directory manually like this:

\path\to\cc65\bin\cc65 -O -I \path\to\cc65\include -t c128 hello.c

Replacing \path\to with the correct location of the cc65 folder on your system. The -O switch, by the way, optimizes for size by default. If you'd rather have it optimize for speed, use -Oi.

Now you should have a file called hello.s, which is the assembly language source code created by the C compiler. To assemble it to object code, do the following:

ca65 hello.s

Easy step, eh?

Now, the final step is linking the object code into an executable program for the C128:

ld65 -t c128 -o hello hello.o c128.o c128.lib

If it can't find c128.o or c128.lib, again you have a problem with your environment variables, so you'd need to do:

ld65 -t c128 -o hello hello.o \path\to\cc65\lib\c128.o \path\to\cc65\lib\c128.lib

At last, you've got a 128 program! Copy hello over to your WinVICE directory, then do run "hello". If everything went okay, you should see now see "Hello World!" on the screen. Cc65's text output routines automatically switch the screen to mixed upper/lower letters so that you can see it properly.

Hope that helps, but if you still can't get things working, post here again and we'll go from there.

Cheers,
nikoniko

P.S. If you're thinking of using cc65 to write an entry for the game competition, don't. As you can see, our simple Hello World program compiles to almost 1K... and if you were to use printf rather than puts in order to do more complex output, that jumps to 2.5K. (And that's when optimizing for size! :()

Stephane Richard

nikoniko, thanks a whole bunch, looks like that's exactly the kick I needed...I think I can follow that without a problem.  If something happens, i'll let you know here.
When God created light, so too was born, the first Shadow!

MystikShadows

nikoniko

I see I declared main as returning an int on exit but didn't bother returning an exit code. It is possible to tell cc65 to return a value from main, but I don't think we can access that value when our program exits back to BASIC.

Mark Smith

Quote from: nikonikoI see I declared main as returning an int on exit but didn't bother returning an exit code. It is possible to tell cc65 to return a value from main, but I don't think we can access that value when our program exits back to BASIC.
I was going to reply mentioning that .. but thought I'd come across as nitpicking, but hey it was your post so you can nitpick yourself :-)

See! I can remember some of what they taught me at uni ....
------------------------------------------------------------------------------------------------------------------

Commodore 128, 512K 1750 REU, 1581, 1571, 1541-II, MMC64 + MP3@64, Retro-Replay + RR-Net and a 1541 Ultimate with 16MB REU, IDE64 v4.1 + 4GB CF :-)

nikoniko

No need to hold your tongue for my sake... It's already clear my C skills are in sorry shape when I can mess up Hello World. Kernighan and Ritchie would be ashamed of me.

Actually, I started off by returning an exit code at the end, using the predefined EXIT_SUCCESS from stdlib.h. Then for simplicity's sake got rid of it, but in doing so, I forgot to fix my declaration.

Ah well, at least I'm pretty sure I didn't mess up my instructions for compiling, assembling and linking my sloppy code, so I'll keep my fingers crossed that it's good enough to get mystikshadows going.

hannenz

Quote from: Michael Hart on January 08, 2007, 11:16 AM
I see I declared main as returning an int on exit but didn't bother returning an exit code. It is possible to tell cc65 to return a value from main, but I don't think we can access that value when our program exits back to BASIC.
the return value is stored somewhere where you can read it from BASIC, was it in the ST status variable...?! can't remember but it must be in the cc65 docs since i read about it there...
btw: you can also pass parameters to main() like this:
RUN: REM 1,2,3,4....

nikoniko

Thanks for reminding me about this topic. Yeah, the return value is passed back to BASIC in ST. I forgot to post an update here after I became more familiar with cc65's calling and return conventions.