How to get SH4-GCC to work with C++ code  
 

What is this guide about?

This guide is intended for people who want to compile C++ code for their Dreamcast systems, but can't get the compiler working. This guide relies haevily on existing information, namely by Groepaz/Hitmen and Marcus Comstedt. If you find wrong information on this page, please write me a mail and I will fix it.

UPDATES

What configurations are covered?

  • Linux 2.2
  • GCC 2.95.2 compiled for SH4 code generation
  • Newlib 1.8.2 compiled for SH4
  • Binutils 2.10 compiled for SH4

1. Setting up Cygwin + GCC + the cable

First of all, you need the serial cable that Marcus Comstedt designed. A complete documentation on how to build it is located here (local mirror here).

If you have a serial cable, get the Serial Slave software which is available at Fulgs site (local mirror here). If you hav troubles burning a working image using Discjuggler/Nero/others (i had), download the CDRecord tools for Win32 (Cygwin) and follow the instructions

If you intend to send large projects to your DC, you are a lot better off using the DCLoad system. It basically is the same thing but supports binary compressed transfers. No more SRECS =). Since the Cygwin tools precompiled by Florian Proff Schulze only work with the 115k DCLoad CD, you can find a version I compiled with 56k here.

Done? When you now start your DC with the serial slave inserted, you should get the Sega logo with the blue borders. At this time, it is a good idea to test if your serial cable works. Open your Hyperterm, and load this configuration file. You should now, upon pressing return, see a ">" sign, which indicates the serial slave being ready. Cheer, your serial cable seems to work!

Next you will need GCC. Hitmen have provided a great setup guide (local mirror here) for GCC, which you can use to get your environment going. There is only one difference you need to be aware of:

the line in building GCC on the Hitmen site says:

				$ make LANGUAGES="c" all install
				
change that to:
   			
				$ make LANGUAGES="c c++" all install
				
This did work compiling using Cygwin, but I already had the C compiler for SH4 up and running when I compiled C++ support. I verified on my Linux box the correct order to compile things is:
  • Binutils
  • GCC (LANGUAGES="c")
  • Newlib
  • GCC (LANGUAGES="c++")
This works much more reliable across platforms.

2. Adding required routines not included with SH4-GCC

Now, there is one little problem. Even though you C++ compiler works (the exe is called sh-elf-g++), it will produce errors when trying to initialize a class. This is due to the fact that we are missing target platform specific routines to allocate memory the c++ way. the missing routines all start with "__builtin_". The good news is, all you have to do is paste this on the top of your code:

               extern "C" {
                 void *__builtin_new(int size)
                 {
                    return malloc(size);
                 }
 
                 void __builtin_delete(void *ptr)
                 {
                    free(ptr);
                 }

                 void *__builtin_vec_new (int size)
                 {
                   return __builtin_new (size);
                 }
   
               } // end extern "C"
			   
You might also wish to add __builtin_vec_delete, but it is not needed for compiling a standard C++ executable, so i left it out.

HELP! malloc does not work!
Your instant cure is
here. Just copy this crt0 file over the one you currently use when linking your project. Kudos to Groepaz/HITMEN on this one.

3. Modifying your Makefile

Fortunately, the differences in the Makefiles are trivial, the only thing you have to do is to add a new build rule for cpp files. this is as easy as adding:

			   # skip these if they are already in your Makefile
               TARGET=sh-elf
               DCBASE=/usr/local
               # this is the c++ compiler
               CCC=$(DCBASE)/bin/$(TARGET)-g++ -ml -O -m4-single-only -Wall			   
               ...
			   ...
			   # this is the new target for cpp files, put this below your c file target
               %.o: %.cpp
	                 $(CCC) $(INCS) $(FLAGS) -c $< -o $@
			   
(do not forget that the last line here MUST only contain a TAB before $(CCC)

If you are using the modified crt0.s, to prevent linking errors, you should use the following line in your Makefile to link your code:

                $(BIN).elf: $(OBJS)
				    $(CC) -Wl,--script,sh-ld.x -o $(BIN).elf $(OBJS) $(LIBS)
				
sh-ld.x is a linker script by groepaz you can download here

When all runs smooth, you should now be able to compile C++ sources without major troubles. I know, this part of the guide is to be improved, mail me with any suggestions, please.

4. Sending your code to Dreamcast

A lot of free Makefiles can be found around the net, the best tutorial sources to compile are propably at Dans site. Just take one of the dreamlib examples, and look at the Makefile. You will notice that the last step performed in the Makefile is a conversion of the SH4 binary to SREC format. What this means is that the binary is broken down into little descriptive chunks that can be received by the serial slave, and placed from there into system memory. The final SREC command starts the execution of the code.

have a look at your .srec file in a text editor, and do a "select all" and copy it. Then, boot the Dreamcast using Serial Slave, start up your Hyperterm with the config file provided above, and click "Edit - Paste to Host". Depending on the size of your file, this will take a few minutes =). After the SREC has been sent, the program will run on your Dreamcast. Ta - Da.

5. Did all that, works. Now what do I do.

I cannot help you any more, but certainly, go and play with Liddream, and KOS now, both available at Dans site. Hope you found this guide helpful rather than "too buggy". Again, if you find errors in this guide, let me know.

In no way am I connected to any companies mentioned on this page. Further- more, all the software and hardware solutions offered on these pages are provided "AS IS", meaning, I cannot be held reliable if it fries your hardware, software, or soul. Ah, and Sega is a trademark.

Maintained by tubooboo@ngine.de
all content © ngine.de

counter