Saturday, August 31, 2013

Switching to 6502

Mineshaft was written mainly in C. I was comfortable with C since before, and getting from zero to Mineshaft took about a week and a half. Shiru, a member of the nesdev community, has a great library available free for anyone to download on his page.

On my next game, I also started in C. As you may or may not know, C is what is called a high level language. You write code that is easy to understand for a human which then gets translated to machine code by a compiler. This conversion produces code that works, but the code can get bigger and slower than you might want. On a modern platform such as a normal Intel/AMD x86, the compilers will most of the time be able to produce better code than a human. But on such a limited CPU as the 6502, a C compiler will have a bit more trouble. Writing assembly code can save both CPU cycles and code size even if you are not a master sorcerer of assembly.

The result of writing the new game in C was that I spent more time optimizing for speed than I spent on developing game features. I had set a goal for myself that I wanted 6 enemies plus 2 players moving about on a level in my game, while still maintaining 60 fps. When I had the game running with all zombies moving about with simple AI, music playing and collisions happening, I was using almost the full frame time for calculations. There was no more time left to do anything else. It would either have to be a very simple game in terms of mechanics, or I would have to sacrifice the 60 fps. All the opimizations also made the code a mess of hacks and it was becoming hard to read.

When starting over this time around, I had started looking at writing the game in 6502 assembly directly. It seemed like a massive undertaking at first to learn a whole new way of programming. I had never really done any assembly programming before, except going through the excellent Nerdy Nights tutorials over at Nintendoage.com, and making a few small additions to the scrolling in Mineshaft. Would I be able to learn 6502 assembly well enough to make a game in a reasonable time frame?

Arnold can LDA and and stuff

I started messing about and doing some tests of stuff like moving and animating metasprites. After a few days of trinkering I began to get an OK understanding of the way things worked, even though I still messed up simple stuff and spent hours debugging stupid newbie mistakes. Pretty soon I felt comfortable that I would be able to write the game, or at least the low level engine, in assembly.

The results so far are encouraging. I am still very much a newbie at assembly, and the code I produce is most likely shit compared to the greats, but so far things are looking good on the performance side. I won't give away too many details, but 6 enemies are now not an issue.

No comments:

Post a Comment