Archive for the ‘Software dev’ Category

WDC Classic C Libs

Wednesday, May 14th, 2008

I recently got a comment through the comments of my latest post. Gato seems to have problems using C libs provided with the WDC compiler. What are really those C libs and what are the functionnality they provide? There is plenty of website providing more info on those libs. Don’t hesitate to use Google for more info about them.

I will now show only a part of the C standart librairy :  stdlib.h and string.h.

Stdlib.h – Commonly used Library Functions. Some example of functions :

int abs(int _j);
	- absolute value of integer
int atoi(const char *_nptr);
	- convert a string to an integer
long int atol(const char *_nptr);
	- convert a string to a long integer
void exit(int _status);
	- normal exit & close files
void free(void *_ptr);
	- causes the space pointed to by ptr to be deallocated.
void ftoa(double _val, char *_buf, int, int);
	- convert floating point to ascii
void *malloc(size_t _size);
	- allocate with block size
int rand(void);
	- integer random numbers (0 – 32565)
void *realloc(void *_ptr, size_t _size);
	- expand memory block
void srand(unsigned int _seed);
	- seed integer random number generator

String.h – String conversion Functions and Memory Functions. Some example of functions :

void *memchr(const void *_s, int _c, size_t _n);
	- search memory for character
int memcmp(const void *_s1, const void *_s2, size_t _n);
	- compare memory
void *memcpy(void *_dst, const void *_src, size_t _n);
	- copy memory, byte access, allows overlap
void *memmove(void *_dst,const void *_src, size_t _n);
	- move memory, byte access, allows overlap
void *memset(void *_s, int _c, size_t _n);
	- fill a block of memory with a character
char *strcat(char *_dst, const char *_src);
	- string concatenate
int strcmp(const char *_s1, const char *_s2);
	- compare strings
char *strcpy(char *_dst, const char *_src);
	- copy string
char *strstr(const char *_s1, const char *_s2);
	- search for one string in another
char *strtok(char *_s1, const char *_s2);
	- split string into tokens

You should use those carefuylly since they may not be perfectely optimised and then take a large space into your final ROM. So now ? Maybe a small example to illustrate all this. 3 simples steps : code , compiling and finally linking.

The code – main.c

#include <stdlib.h>
#include <string.h>

void main(void) {
}

void preInit(void) {
	char string[10] = "snes";
	char test[10];

	// fill with space
	memset(test, 32, 10); 	 // screen A
	// fill half with dots
	memset(test, 46, 5);	 // screen B
	// copy with string
	memcpy(test, string, 4); // screen C
	while(1 == 1) {
	}
}

void IRQHandler(void) {
}

Compiling

Two lines for compiling, please refer to the doc for the purpose of teh differnet parameters. The -MS is for specifying the model, it is important to specify when later you need to specify the lib model.

wdc816cc -wl -wp -sop -MS main.c

Linking

wdcln -HB -M21 -V -T -Pff -C008000,0000
-U0000,0000 -Avectors=FFE4,7FE4 -Aregistration_data=FFB0,7FB0
-N startupSnes.obj main.obj -LC:\65xx_FreeSDK\lib\cs -O test.smc

The cs lib is for the Model Small that we specified while compiling.

Some function don’t seems to work properly. By example, the rand function comming from stdlib.h always return the same value. The tested string function are working perfect and are not too badly optimised. Here are some screenshot from my debugger during the 3 step of execution (Screen A, B and C), click to enlarge :

WDC C lib proof of concept

As usual you can get the full archive to download here.

See ya Lint,

PS : shoot your comments as usual !

Major WDC linker issues

Wednesday, May 7th, 2008

Once again an other issue. This time it’s about the WDC linker that seems not to be able link against C library. I already have sent a few email and ticket support to the wdc team. Still no response since my first enquiry 2 months ago. It’s been a while now that the direct to the freesdk have been removed.

So here is the source code that is having the problem and need to be linked with c.lib or cs.lib (small model) :

main.c

typedef    unsigned char    byte;
typedef    unsigned short    word;

typedef struct padStatus{
    byte right:1;
    byte left:1;
    byte down:1;
    byte up:1;   
    byte start:1;    // Enter
    byte select:1;    // Space
    byte Y:1;    // X
    byte B:1;    // C
    //--------------------------------
    byte Dummy:4;
    byte R:1;    // Z
    byte L:1;    // A
    byte X:1;     // S
    byte A:1;    // D
} padStatus;

padStatus readPad(byte padNumber) {
    word test;
    // TODO fix usage of padStatus structure
    padStatus *status;

    // TODO move ot dedicated function
    // enable pad ready
    *(byte*)0x4200 = 0x01;

    // check if pad is READY !!!
    padNumber = padNumber << 1;
    test = (word) *(byte*)0x4218+padNumber << 8;
    test |= (word) *(byte*)0x4219+padNumber;

    status = (padStatus *) &test;

    return *status;
}

void main(void) {
    padStatus pad1;

    // Infinite loop
    while(1) {
        pad1 = readPad((byte) 0);

        if(pad1.up == 1) {
            *(byte*) 0x2100 = 0x00; // disable background
        } else {
                   *(byte*) 0x2100 = 0x0f; // enable background
        }
    }
}

This can be compiled with this command line :

wdc816cc -wl -wp -MS -MO -SOP main.c

The -MS is for specifying the Small code layout. So it should be linked with the “cs.lib”. But the here is the output of the linker:

wdcln -V -W -LC:\65xx_FreeSDK\lib\cs  main.obj -O test.smc
WDC 65C816 Linker Version EVALCOPY Jan 18 2006 17:36:51
   Copyright (C) 1992-2006 The Western Design Center, Inc.
Undefined symbol: __~mov

The __~mov symbol cannot be resolved but it is in the cs.lib. So I think that the linked dont link correctly. If anyone got any any idea on why. Feel free to leave a comment.

++ Lint

PS : I’m still working on the two effect and debug text output, expact an update in a few days.

FIXED !!! Look at the comments !!!

Battle of the loRom layout

Wednesday, April 30th, 2008

It’s now been a few days that I’m in a battle with the ROM layout. I’m starting to have huge ressource, a bit less than 32kb. So I tried to put that in an other bank. That was monday. Since it does’t working until this morning, after reading many documents and analysing other roms.

Now everything goes well except thatmy favoriteemu/debugger the so called “Snes9X1.43.ep9r8.exe” isn’t working with it. It’s mixing the bank access and copies the actual code into VRAM and not the data coming from from bank 1. Now all the other emu are working great (was not the case of bsnes before).

I provide you the .smc format rom. If you have any idea why it’s not working don’t hesitate to contact me ASAP via the comments on this post.

Here are some info about the rom layout from the linking process :

Section: ORG:    ROM ORG:  SIZE:
CODE     008000  000000      4CCH (  1228)
UDATA    000000  ------       10H (    16)
vectors  00FFE4  007FE4       1CH (    28)
registra 00FFB0  007FB0       2FH (    47)
bank4    018000  008000     7F40H ( 32576)
Total                       8467H ( 33895)

The huge data is in bank4 section. Address is set to 01:8000 and in the rom it’s at 8000. Main code is at address 00:8000 and rom is 0000. The registration data of the cart is set to mode 20 (lorom)

If you have any other question -> comments.

Here is the Rom for you testing and analysing.

See ya, Lint

PS : Easter egg in the rom (tested working on bsnes and znes) … Hint … play with your pad (i said too much, I know !!!)

FIXED !!! Look at the comments !!! 

WDC C Compiler for 65816 in action [PART II]

Friday, April 25th, 2008

Hey all…

I managed to do a real quick update today. I’m back with both pad reading and background scrolling as promised. Scrolling was all easy to implement. Pad reading function was more difficult to get working. It’s mainly due to a bug or a bad usage from myself when linking. It don’t like return the padStatus struct. So i needed to modify my function to make it return a word and then cast. Here is code in details :

typedef struct padStatus{
	byte right:1;
	byte left:1;
	byte down:1;
	byte up:1;
	byte start:1;	// Enter
	byte select:1;	// Space
	byte Y:1;	// X
	byte B:1;	// C
	//--------------------------------
	byte Dummy:4;
	byte R:1;	// Z
	byte L:1;	// A
	byte X:1; 	// S
	byte A:1;	// D
} padStatus;
word readPad(byte padNumber) {
    word test;

    // Enable pad reading
    *(byte*)0x4200 = 0x01;

    padNumber = padNumber << 1;
    test = (word) *(byte*)0x4218+padNumber << 8;
    test |= (word) *(byte*)0x4219+padNumber;

    return test;
}

This allow to easily read the pad and then doing stuff like : if(pad1->left) … wich is quite nice.

I have contact WDC about the bug and i will keep you updated.

For you information here is the scrool code :

word padValue1;
padStatus *pad1;
byte HScroll = 0;
// Pointer hacker for typecasting padStatus
pad1 = (padStatus *) &padValue1;
...
// Loop forever
while(1) {
	waitForVBlank();

	// Read pad1 value
	padValue1 = readPad((byte) 0);

	if(pad1->left) {
		HScroll++;
	} else if(pad1->right) {
		HScroll--;
	}

	// Plane 0 scroll register
	*(byte*) 0x210d = HScroll;
}

Full archive is available here as usual.

For any question, problem getting the code to compile, … please don’t hesitate to post a comment. I will then be glad to answer you.

So whats next ? What about a two simple effects and debug function to print text on screen ? Sounds nice …

++ Lint

WDC C Compiler for 65816 in action [PART I]

Wednesday, April 23rd, 2008

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

 (more…)

First contact with WDC 65816 C Compiler in action [Teaser]

Wednesday, April 23rd, 2008

It’s been 24 hours that I’m extensively playing with the WDC C Compiler for SNES. And I finally managed to get a first piece of code working with it. Not that much at the moment, just a writing to VRAM with DMA and from there displaying a screen in MODE 1.

Kung Fu master Teaser

 I will come back as soon as posible with source code and comments.

++ Lint

[REQUEST] Tweety still picture source code

Monday, April 21st, 2008

I’m once getting ‘uber’ nostalgic.

A about 15 years I started to code on the SNES. The first piece of code that I assembled was this demo : “tweety still picture“. At the time the demo was distributed with source code. Can you please help me found the source code?

A link, an email.. everything is welcome. I really want to see that source code once again.

Thanks in advance,

See ya, Lint

WikiBooks – Super NES Programming

Friday, April 11th, 2008

Here is the first website I’m going to present to you :

http://en.wikibooks.org/wiki/Super_NES_Programming

This site is just wonderfull when you start coding the SNES. The basic stuff is really well explained. It’s a must read for any beginner. This wiki is updated on a regular basis but not that much neither, so dont except to find a new tutorial every week.  Now you know it’s a wiki, so we are free to update it as well.

See ya, Lint

Such a small scene …

Tuesday, April 8th, 2008

This weekend I haven’t worked much on my projects. But I have looking around for retro gaming website, and mostly the one talking about new custom hardware. Custom hardware on SNES is mostly reserved to backup units or cart modding. Real homebrew hardware is quite impossible to find, believing it don’t exists. The only piece of hardware that have read about recently is the ULTRA16 mod by D4S.

No SNES but a load of Commodore 64 stuff and a bunch of other stuff for old computers. Many, many, many stuff going from custom cables to harddrives, card reader and so on. I know that the Commodore 64 is the computer that selled the best ever. Almost everybody who is 35 years old got one when they were younger. But so many custom hardware seems crazy to me. Someone had even created a SID port of Guitar Hero that play with PS2 guitar on the commodore 64. How crazy !!! Don’t expect to see that coming soon on the SNES.

I talked a few months ago with a friend that is part of the Megadrive scene. He told me that there is maybe 20 good regular coder left on Megadrive. 20 might seems a lot .. but in a worldwide view it’s like nothing. One of the irc channel that i idle on is #snesdev on efnet networks, about 7 users …. and a few words typed in everyweek. Sometimes not a single word is said in weeks. I dunno how many regular coders there is for the SNES maybe 20 also… not more…

The website talking about SNES development seems to start dying since year 2000. More and more broken links, server shutdown. Only a few continue the archiving of the docs, many thanks for them. The only community that seems really alive are the ROM translators. And they do very good work…

One other thing is that I think that more people use emulator when it comes to console than computer. That’s maybe also why no custom hardware is develloped.

Anyway, if you are SNES active developper, spread the information, show what you are doing, blog every day (even if you are a beginner). Myself I will try to get people to be interested more deeply into SNES software and hardware. I believe there is a sleeping scene… We need just need to make it true once more …

I will start referencing more SNES related website soon so new comers landing on this will ahve stuff to see and for already SNES coder there is may be a few site missing. Don’t hesitate to send me more links, it’s more likely that I’m going to miss some of them.

Now I’m maybe totally wrong about all this. If it’s the case don’t hesitate to get back to me trough the comments on this posts.

++ Lint

Already soldout

Monday, April 7th, 2008

Noooo … i’s already soldout !!! A few months ago I talked with”D4S” about his ultra16 mod for the SNES. He told it will go soon in production and be a limited number of 50 pieces. I discovered today that the sale had started and that the 50 pieces went soldout in 4 days. I guess that some of those units will reappear soon on Ebay but not a at good price…

Ultra16 pcb

Damn …

Here is the link for your information. It’s worth a read to see all the feture of this unversal ‘switchless” SNES. D4S really did a wonderfull job. He’s probably the most active coder on the really small SNES scene. Good job man !

See ya, Lint