WDC C Compiler for 65816 in action [PART I]

As promised here is the release of the previous teaser that you saw earlier on this blog. A few months ago I already had talked about the WDC C Compiler. I know that SNES is used to be coded in asm directly and so not using a high level language like C. So why C ? Just because i didn’t wanted to attack a big project with only the use of asm, since it’s been about at least 10 years that I have’t touched 65816 opcodes. C allow me to code high level and to get slowly in touch with generated code to fix problem with my snes9x emu/debugger.

Where to start ? Get the source code and at the file layout of the archive :

/ressource : contains .pcx file to display + generated ressource
/tools : contains tool to convert ressource gfx files
ressource.asm : WDC asm to include binary ressource
ressource.h : header file to includes ressource
main.c : main Rom code
StartupSnes.asm : ROM layout directive and Snes init code
kungfu.smc : pre generated ROM file
make.bat : compile the code and create ROM file kungfu.smc
makeClean.bat : clean up file generated by make.bat
makeRessource.bat : create the tile, map and palette files

I’m now going to in depth for certain parts of the code.

Kung Fu Master Title Screen

We will start with StartupSnes.asm. I made that one a few ago while playing for the first time with the WDC compiler checking if it was able to get it to compile a valid ROM. This file is clearly going to evoluate soon and getting a better structure. For now all I needed was the thing to work.

What is really StartupSnes.asm doing :

  • Initialize the Snes registers (see reference code)
  • Jump to main function
  • Define IRQ handling simple method that call IRQHandler() function
  • SNES registration data (Maker code, Game title, …)
  • Define Interrupts and Reset vectors

Nothing really complicated.

Next we will analyse the ressource inclusion proces. Everything is mostly done in ressource.asm. You need to be aware of two directive that we will use wit hte WDC assembler : XDEF and INSERT. Here is an example with a apart of my ressource.asm :

    XDEF __title_pic
__title_pic:
    INSERT ressource/kungfu.pic

Xdef allow to define a symbol that will be made available in other object if needed. The INSERT directive allow to insert binary data. So we tell tha assembler that the __title_pic symbol will be made available for to other .obj at linking, then we define the symbol and insert the binary data. Once again nothing really complicated. The __ prefix is needed when you need to use symbol in a C compiled code (I will serach more info about that soon). The ressource.h is then just here to declare the ressource in C :

extern word title_pic[];

Let’s now get inside the main.c file. It contains 4 functions. Two of them need to be declared since they are declared in StartupSnes.asm : main and IRQHandler. IRHandler function isn’t doing anything at the moment. The main function is calling two other function declared here : VRAMLoad and CGRAMLoad for loading respectively data in VRAM and in CGRAM. Then it define some SNES registers to make the screen appears on screen. The VRAM and CGRAM functions are quited well commented directly in the source code.

Optimisation is required on some parts of the code. That will be done a bit later. I hope that this post will help you and and make you want to code for the Snes. More source code will come fast. Currently I hesitate between a simple scroll or a joypad handling functions. I keep you all updated.

The current full archive is available here.

See ya, Lint

Comments are closed.