Programming The Advance: The Unofficial Guide

Programming The Nintendo : The Unofficial Guide I Copyright ()2003 by Jonathan S. Harbour -- http://www.jharbour.com Programming The Nintendo Game Boy Advance: The Unofficial Guide

Jonathan S. Harbour

Programming The Nintendo Game Boy Advance: The Unofficial Guide II Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com © 2003 by Jonathan S. Harbour. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system w ithout w ritten permission from Jonathan S. Harbour, except for the inclusion of brief quotations in a review . The electronic edition of this book may not be re-distributed or re-printed in any form or by any means, and may only be obtained from http://w w w .jharbour.com.

Project Editor: Estelle M anticas

Copy Editor: Laura G abler

Technical Review er: André LaM othe

Program m ing Review er: Emanuel Schleussinger

Softw are Review er: Peter Schraut

Nintendo, G ame Boy, G ame Boy Advance, Super Nintendo, Nintendo 64, M ario Bros., Zelda, and/or other Nintendo products referenced herein are either registered trademarks or trademarks of Nintendo of America Inc. in the U .S. and/or other countries. M icrosoft, W indow s, DirectX, DirectDraw , DirectM usic, DirectPlay, DirectSound, Visual C++, Xbox, and/or other M icrosoft products referenced herein are either registered trademarks or trademarks of M icrosoft Corporation in the U .S. and/or other countries. All other trademarks are the property of their respective ow ners.

The author has attempted throughout this book to distinguish proprietary trademarks from descriptive terms by follow ing the capitalization style used by the trademark ow ner.

Information contained in this book has been obtained by the author from sources believed to be reliable. How ever, because of the possibility of human or mechanical error by our sources, the author does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such information. Readers should be particularly aw are of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book w ent to press.

ISBN: 1-59200-009-6

Printed in the U nited States of America

03 04 05 06 07 BH 10 9 8 7 6 5 4 3 2 1

Programming The Nintendo Game Boy Advance: The Unofficial Guide III Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com For selfless support and love, for accepting

the inequity of m y preoccupation w ith gam es

and fully supporting it, this book is dedicated

to m y w onderful w ife,

Jennifer Rebecca H arbour

Programming The Nintendo Game Boy Advance: The Unofficial Guide IV Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Acknow ledgements

I am thankful for the encouragement of my w ife, Jennifer, w ho has been w onderfully supportive of my w riting career. I love our life together, and our kids, Jeremiah and Kayleigh, bring me more joy than I thought possible. I also ow e a thank you to tw o pair of friends w ho have greatly encouraged me on a personal level: Justin and Kim G allow ay, and M ilford “W ade” and Lindsay Eutsey. I thank G od for the blessing of your friendship. I am grateful for a supportive family: my parents, Ed & Vicki; my sister, Joy; my niece, April; in- law s, Dave & Barb Yoder; David; Steve, Andrea, and Stephen; Jason, Autumn, and Tater; and Barbara Jean. O n another level, I thank the Lord for giving me the drive to do this sort of w ork, w hich is both enjoyable and insanely difficult at the same time. I am thankful for having learned many life lessons from Focus O n The Family and Dr. James Dobson, as w ell as from the extraordinary Left Behind series by Tim LaHaye and Jerry Jenkins.

This w as perhaps the most enjoyable book I have w ritten, not solely due to the content (w hich is utterly compelling!), but also due to tw o events that took place w hile this book w as being w ritten. The first experience w as during the G ame Developer's Conference 2003, w here I met several editors from Premier Press face-to-face for the first time. After the G ame Developer's Choice Aw ards, presented by the International G ame Developer's Association (IG DA), Premier Press held a private dinner at a nice restaurant in dow ntow n San Jose, w hich w as a fun meet-and-greet w ith André LaM othe, Heather Hurley, M itzi Koontz, Heather Talbot, Todd Jensen, as w ell as other fellow authors. Thanks to all of you for doing such a great job w ith this series; I have enjoyed w orking w ith all of you. The second experience that made this book memorable w as a celebration a few days later on the launch of DarkBASIC w ith fellow author Joshua Smith, our families, and my tw o favorite freelancers, Cathleen Snyer and Estelle M anticas. Thank you for doing such great w ork, it's been a great pleasure w orking w ith you.

I w ould like to thank one of the greatest game designers of all time, John Romero, for not only being such a great encouragement and role model for aspiring game developers, but also for agreeing to w rite the forew ord. As Jacopo says, "I'm your man!" Thanks to my good friend, Joshua R. Smith, for loaning me the Flash Advance Linker.

G reets go out to tw o guys w ho w ere invaluable w hile w riting this book, for they provided up-to-the-minute updates to the development tools. Emanuel Schleussinger, for the excellent G BA SDK used in this book, called H AM , and to Peter Schraut for the excellent IDE

Programming The Nintendo Game Boy Advance: The Unofficial Guide V Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com called Visual H AM . These tools saved me literally hundreds of hours of w ork. Not only did they provide superior development tools to me and the G BA community at no charge, these tw o w ere invaluable as proofreaders of the manuscript. Thank you both for getting me out of many dead ends and helping resolve coding problems along the w ay. This book reflects your efforts.

A book of any size and on any subject involves much w ork even after a manuscript is done. It takes a w hile just to read through a programming book once, so you can imagine how much is involved in reading through it several times, making changes and notes along the w ay, refining, correcting, perfecting. I am indebted to the hard w ork of the editors, artists, layout specialists w ho do such a fine job. Really, as far as total hours go, the author's w ork is only perhaps 40% (or less) of the total time spent on getting a book into print. Thank you especially to Laura G abler for copy editing the manuscript, to André LaM othe for his technical expertise, and to Estelle M anticas for managing the project.

Programming The Nintendo Game Boy Advance: The Unofficial Guide VI Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com About The Author

Jonathan S. Harbour has been programming games for 15 years, first w ith M icrosoft G W - BASIC and Turbo Pascal, then on to Turbo C, Borland C++, W atcom C++, and 80386 assembler. After finally bridging the gap to W indow s programming, he has spent time w ith Borland Delphi, Visual Basic, Visual C++, and more recently, Visual Studio .NET. Jonathan graduated from DeVry Institute of Technology in 1997 w ith a B.S. degree in Computer Information Systems, and has since w orked for cellular, aerospace, pharmaceutical, education, medical research, healthcare, and game companies. In his spare time, Jonathan enjoys spending time w ith his family, reading fiction, playing console video games, w atching movies, and w orking on his classic M ustangs.

Programming The Nintendo Game Boy Advance: The Unofficial Guide VII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Contents

Forew ord

Introduction

Part I The Zen of G etting Started

Chapter 1 W elcome To The Console W orld

Video G ames!

G etting into the Nintendo Thing

Console Contemplation

Definition of a Video G ame

A Brief History of Nintendo

Shigeru M iyamoto

Home Consoles

The 16-Bit Era

Success and Failure

A Detailed Comparison

W hat Happened to the Atari Jaguar?

The Importance of G oals

U se Your Imagination

Programming The Nintendo Game Boy Advance: The Unofficial Guide VIII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Build a Franchise

G enre- and Character-Based Franchises

Strike a Balance: Level Count vs. Difficulty

Surprise the Critics

Summary

Chapter Q uiz

Chapter 2 G ame Boy Architecture In A Nutshell

G ame Boy Handheld Systems

G ame Boy, 1989

G ame Boy Pocket, 1996

G ame Boy Color, 1998

G ame Boy Advance, 2001

G ame Boy Advance SP, 2003

Direct Hardw are Access

M emory Architecture

Internal W orking RAM

External W orking RAM

Cartridge M emory

G ame RO M

G ame Save M emory

Programming The Nintendo Game Boy Advance: The Unofficial Guide IX Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com G raphics M emory

Video M emory

Palette M emory

O bject Attribute M emory

The Central Processor

Tw o Instruction Sets

CPU Registers

The G raphics System

Tile-Based M odes (0[en]2)

M ode 0

M ode 1

M ode 2

Bitmap-Based M odes (3[en]5)

M ode 3

M ode 4

M ode 5

The Sound System

Summary

Chapter Q uiz

Chapter 3 G ame Boy Development Tools

G ame Boy Advance Development

Programming The Nintendo Game Boy Advance: The Unofficial Guide X Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Deconstructing ARM 7

Emulation: The Key to Reverse Engineering

U nfortunate Side Effects

W hat About Public Domain Development Tools?

Visual HAM and the HAM SDK

W hat Is HAM All About?

HAM on the W eb

The Visual HAM Environment

Installing the Development Tools

Installing HAM

Installing Visual HAM

Configuring Visual HAM

Running G ame Boy Programs

G ame Boy Emulation

Running Programs on Your G BA

U sing a M ultiboot Cable

U sing a Flash Advance Linker

Summary

Chapter Q uiz

Chapter 4 Starting W ith The Basics

The Basics of a G ame Boy Program

Programming The Nintendo Game Boy Advance: The Unofficial Guide XI Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Codito Ergo Sum

W hat M akes It Tick?

A Friendly G reeting

Creating a New Project

W riting the G reeting Program Source Code

Compiling the G reeting Program

Testing the G reeting Program in the

Draw ing Pixels

W riting the Draw Pixel Program Source Code

Compiling the Draw Pixel Program

Testing the Draw Pixel Program in the Emulator

Filling the Screen

W riting the FillScreen Program Source Code

Compiling the FillScreen Program

Testing the FillScreen Program in the Emulator

Detecting Button Presses

W riting the ButtonTest Program Source Code

Compiling the ButtonTest Program

Testing the ButtonTest Program in the Emulator

Running Programs Directly on the G BA

The M ultiboot Cable

The Flash Advance Linker

Programming The Nintendo Game Boy Advance: The Unofficial Guide XII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Summary

Challenges

Chapter Q uiz

Part II Being O ne W ith The Pixel

Chapter 5 Bitmap-Based Video M odes

Introduction to Bitmapped G raphics

Selecting The Ideal Video M ode

Hardw are Double Buffer

Horizontal and Vertical Retrace

W orking W ith M ode 3

Draw ing Basics

Draw ing Pixels

Draw ing Lines

Draw ing Circles

Draw ing Filled Boxes

Draw ing Bitmaps

Converting 8-Bit Bitmaps to 15-Bit Images

Converting Bitmap Images to G ame Boy Format

Draw ing Converted Bitmaps

Programming The Nintendo Game Boy Advance: The Unofficial Guide XIII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com W orking W ith M ode 4

Dealing W ith Palettes

Draw ing Pixels

Draw ing Bitmaps

How To U se Page Flipping

W orking W ith M ode 5

Draw ing Pixels

Testing M ode 5

Printing Text O n The Screen

The Hard-Coded Font

The Draw Text Program

Summary

Challenges

Chapter Q uiz

Chapter 6 Tile-Based Video M odes

Introduction To Tile-Based Video M odes

Backgrounds

Background Scrolling

Tiles and Sprites

The Tile Data and Tile M ap

Programming The Nintendo Game Boy Advance: The Unofficial Guide XIV Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Creating A Scrolling Background

Converting The G raphics

Fast Blitting W ith DM A

TileM ode0 Source Code

Creating A Rotating Background

Converting The Tile Image

Creating The Tile M ap

RotateM ode2 Source Code

Summary

Challenges

Chapter Q uiz

Chapter 7 Rounding U p Sprites

Let's G et Serious: Programming Sprites

M oving Images the Simple W ay

Creating Sprite G raphics

Tile G raphics Format

Creating Tiles

Converting Tiles

U sing Sprites

Larger Sprites

Programming The Nintendo Game Boy Advance: The Unofficial Guide XV Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Linear Tile Layouts

Draw ing A Single Sprite

Converting The Sprite

The SimpleSprite Source Code

Creating A Sprite Handler

W hat Does The Sprite Handler Do?

The BounceSprite Source Code

The Header File

The M ain Source File

Resizing The Ball Sprite

Sprite Special Effects

Implementing Alpha Blending

Blitting Transparent Sprites

The TransSprite Header File

The TransSprite Source File

Rotation and Scaling

The RotateSprite Program

The RotateSprite Header File

The RotateSprite Source File

Summary

Challenges

Chapter Q uiz

Programming The Nintendo Game Boy Advance: The Unofficial Guide XVI Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Part III M editating O n The Hardw are

Chapter 8 U sing Interrupts and Timers

U sing Interrupts

The InterruptTest Program

The InterruptTest Header File

The InterruptTest Source File

U sing Timers

The TimerTest Program

The TimerTest Header

The TimerTest Source Code

The Framerate Program

The Framerate Header

The Framerate Source

Summary

Challenges

Chapter Q uiz

Chapter 9 The Sound System

Programming The Nintendo Game Boy Advance: The Unofficial Guide XVII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Introduction To Sound Programming

G BA Sound Hardw are

FM Synthesis Support

U sing Direct Sound for Digital Playback

Sound M ixing

Playing Digital Sound Files

Playing Digital Sounds

The SoundTest Program

Converting The Sound File

The SoundTest Header File

The SoundTest Source File

The PlaySamples Program

Tracking Sample Playback

The PlaySound Function

Keeping Track of Sounds

The PlaySamples Header File

The PlaySamples Source File

Summary

Challenges

Chapter Q uiz

Programming The Nintendo Game Boy Advance: The Unofficial Guide XVIII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Chapter 10 Interfacing W ith The Buttons

The Button Architecture

Detecting Button Input

Searching For Buttons

The ScanButtons Program

M aking Sense of Button Values

Correctly Identifying Buttons

Displaying Button "Scan Codes"

G etting The Hang O f Button Input

Creating A Button Handler

Handling M ultiple Buttons

The ButtonHandler Program

Detecting Button Combos

Summary

Challenges

Chapter Q uiz

Chapter 11 ARM 7 Assembly Language Primer

Introduction To Command-Line Compiling

Compiling From The Command Line

Creating A Compile Batch File

Programming The Nintendo Game Boy Advance: The Unofficial Guide XIX Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Creating A Path to The Compiler Chain

Assembling From The Command Line

Creating An Assemble Batch File

Linking From The Command Line

Creating A Linker Batch File

(Very) Basic ARM 7 Assembly Language

A Simple Assembly Program

The FirstAsm Program

Calling Assembly Functions From C

M aking The Function Call

The Draw Pixel32 Assembly Code

Compiling The ExternAsm Program

Summary

Challenges

Chapter Q uiz

Part IV The M other of A ll A ppendixes

Appendix A ASCII Chart

Appendix B Recommended Books and W eb Sites

Programming The Nintendo Game Boy Advance: The Unofficial Guide XX Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Recommended Books

Recommended W eb Sites

Appendix C G ame Boy Advance Hardw are Reference

M ultiboot

Bit Values

Typedefs

Buttons

Sprites

Backgrounds

Video

DM A

Interrupts

M iscellaneous Registers

Timers

Appendix D Answ ers To The Chapter Q uizzes

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXI Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Appendix E U sing The CD-RO M

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Forew ord

For over 10 years, programming the G ame Boy has been a mystery. U nless you are an approved Nintendo developer, the only w ay to learn how to develop on the G ame Boy family has been to piece together a million scraps of information off the internet, play around w ith secret register addresses you heard w hispered somew here, and basically just shove lots of crazy values in the register lists you've pieced together to see just w hat w ould happen.

But now , that era has passed. Jonathan Harbour has tackled the huge task of compiling all this esoteric and cryptic information into the most useful G ame Boy programming book you'll find. Even Nintendo's documentation doesn't read this good (and w e're Approved Nintendo Developers, so w e know ) and the example code and explanations are now understandable to English-reading lifeforms.

You'll find out soon that programming the G ame Boy is really not that difficult if you're familiar w ith C programming and using pointers. W e designed our game, H yperspace Delivery Boy!, originally for the Pocket PC computer. W hen w e ported the game to the G ame Boy Advance, w e could still retain our original cross-platform code architecture and just rew rite the areas of the game that dealt w ith graphics, sound, and file access (since it's now all just memory in RO M ). It's alw ays a great idea to w rite your game for your target platform and take advantage of its strengths, but coding the G ame Boy Advance doesn't totally require you to do that and still create a successful game.

W hat are you w aiting for? Turn the page and get ready to make your ow n game on a cartridge!

John Romero

G ame Designer

http://w w w .monkeystone.com

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXIII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Introduction

The G ame Boy Advance is a sophisticated handheld video game machine, w ith a pow erful 32-bit microprocessor, 16-bit graphics, stereo digital sound, and yet small enough to fit in your pocket. M illions of G ame Boy Advance units have been sold already, w ith upw ards of 100,000 units a w eek shipping around the w orld during peak months, making this the highest selling video game system in history, w ith over 140 million units sold in all. The affordable price of the G ame Boys, large library of games, relatively low price of games and accessories, plus portability make this a very compelling video game system. Investing in development personnel and tools is more likely on a prolific system like G ame Boy Advance, w hich has a long life span (like earlier G ame Boy models). 14 years, 4 models, and 600 games later (at the time of this w riting), G ame Boy is the most successful video game franchise in history, outselling all others by a w ide margin. Adding icing to the cake, the latest G ame Boy fully supports all the game cartridges from the earlier models!

G ame publishing companies and development studios like the G ame Boy for its relatively low cost of entry and high potential margins. W hile a G ame Boy cartridge might cost more to produce than a CD-RO M or DVD game disc, there are many factors that amount to strong profits nonetheless. O ne factor is that G ame Boy is almost universally considered an accessory device, not a primary video game system. As such, families w ith one or more children w ill often have a console (such as the Nintendo G ameCube) in the living room, as w ell as a G ame Boy. Families w ith tw o or more children are also likely to buy a G ame Boy for each child. Although the demographic for G ame Boy users is primarily young children, a large percentage of G ame Boy ow ners are young adults (18-24) and older.

Game Boy Development U ntil recently, it w as nearly impossible for a hobby or student programmer to get involved in the console market. The problem is this: in order to get hired, console game developers require some level of experience w ith the hardw are; how ever, one can't gain that experience w ithout first being hired. The key w ord here is hardw are. Anyone can gain experience w riting games for a PC, because the development machine is equivalent to the end-user's machine. But this is not the case w ith consoles such as the G ame Boy Advance, w hich requires a custom hardw are interface, special development tools (the compiler, linker, emulator, link cables, etc). These tools are very expensive, and require a special license w ith the video game manufacturer (Nintendo, in this case), w hich also requires a

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXIV Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com non-disclosure agreement (NDA). This agreement says that you are not to share the secrets of the console or development tools w ith anyone w ho is not an officially licensed developer. In other w ords, console games are developed safely behind closed doors.

O ne solution for the aspiring programmer w as usually to find employment w ith a game developer as a game tester (also called Q A, or quality assurance person), and then gradually learn the ropes by w orking w ith the programmers w hile testing games, w ith the hope of being promoted to a development position. Another possibility is to find employment w ith a PC game studio, and gain experience w orking on PC games before moving on to consoles. Experienced developers are usually easy to train on console hardw are, so they are often considered for employment. The PC market is much easier to break into than the console market. Either of these solutions requires patience, determination, drive, and passion for w riting games and learning the tricks of the trade, and no one is guaranteed to succeed. M ost find enough satisfaction out of hobby programming that they are content in that respect. But the video game industry (like the computer game industry) is grow ing by leaps and bounds, so the need for highly motivated and talented programmers is greater today than at any time before.

So You Want To Be A Console Programmer? There is now a solution to the circular problem of finding a job as a console game developer. The G ame Boy Advance uses a popular microprocessor (the ARM 7 CPU ) for w hich there are softw are tools available, because this chip is used in hundreds of consumer electronics products: VCRs, DVD players, TVs, satellite receivers, M P3 players, DSL and cable modems, Pocket PCs, G PS transceivers, in addition to the Sega Dreamcast and Nintendo G ame Boy Advance. In case you are w ondering, the Dreamcast featured a pow erful 3D chip in addition to its ARM CPU , and the Dreamcast CPU and G ame Boy Advance CPU are similar (though the Dreamcast is faster).

The result is that public domain assemblers and compilers have been w ritten for ARM processors, and the ARM Corporation itself has released tools into the public domain for use w ith these products. These tools w ere quickly adapted for G ame Boy Advance, and talented programmers w ere creating and w riting G ame Boy Advance programs before the handheld w as even officially released by Nintendo. (The same w as true for the Dreamcast as w ell).

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXV Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com Software Development Kits You are probably w ondering at this point: how does one w rite G ame Boy programs, let alone compile them and run them on an actual G ame Boy? The development tools have matured in the tw o years (at the time of this w riting) since the G ame Boy Advance w as released. There are now numerous public domain (or freew are) compilers, assemblers, and emulators vying for market share in the G ame Boy development community.

The softw are development kit I selected for this book is a complete integrated development environment (IDE) for w riting, compiling, and running G ame Boy Advance programs, right on the W indow s or desktop. That package is called H AM , and w as developed by Emanuel Schleussinger. The IDE is called Visual H AM , and w as created by Peter Schraut. In order to test the programs, HAM comes w ith VisualBoyAdvance, a top- notch emulation program that runs compiled G ame Boy Advance RO M images in W indow s (or Linux). That's it! HAM is all you need to learn the art and science of programming the G ame Boy Advance, and this w onderful development tool package is free.

Although there are tools available for Linux, I have not included any information in this book about using the Linux version of HAM . Since the same G ame Boy Advance code w ill compile under W indow s or Linux, you may w ish to dow nload the Linux version of HAM from Emanuel's W eb site at http://w w w .ngine.de. The same RO M images that you w ill compile in each chapter of this book w ill run just as w ell in Linux as in W indow s, because the same emulator is available for Linux.

Now , w hat happens w hen you have w ritten a really cool game or demo and w ant to run it on a real G ame Boy Advance unit? W ell, there are basically tw o options. First, there is a multi- boot cable device that w ill dow nload and run compiled RO M binaries on the G ame Boy Advance, using the link cable port. Second, there is a more elaborate option. You can w rite to your ow n flash cartridge. This might sound difficult and expensive, but it is neither. A flash linker is a small device that plugs into your PC's parallel or U SB port and reads/w rites the flash cartridge. The process is similar to using a reader for SmartM edia, CompactFlash, and M M C/SD cards, w ith w hich you might be familiar if you have a digital camera. O nce w ritten, you can plug the flash cartridge into the G ame Boy Advance just like any other game and pow er it up.

O bviously this is not a quick process, and requires a minute or tw o at least, and adds w ear and tear to the G ame Boy Advance and the cartridge over time. W hile it is fun to show your games and demos to friends, the multi-link cable is much faster, because it doesn't require

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXVI Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com a cartridge. In fact, you leave the cartridge slot on the G ame Boy Advance empty, plug in the multi-link cable, pow er up the G ame Boy Advance, and the compiled program is dow nloaded and run directly in the little machine. Visual HAM and the HAM development kit comes w ith the emulator and the various dow nload programs, ready to go. You are literally able to compile and run your ow n code on your G ame Boy Advance. The multi-link cable and flash linker are covered in Chapter 4, "Starting W ith The Basics," along w ith details on w here you can get one. O ne of the most popular W eb sites for ordering these hardw are items, as w ell as aftermarket G ame Boy Advance accessories, is http://w w w .lik-sang.com.

Is This Book For You? M y goal w ith this book is to get you a job as a G ame Boy Advance programmer. Although you may be a hobby programmer, or perhaps a professional video game developer already, my basis and assumption for this book is that you are an aspiring game programmer. The result is that I don't w aste any time talking about subjects that w on't help you in that respect. This is a very focused book that stays close to the subject matter, and as a result it is not as large as some books. How ever, most game programming books try to cover as much as possible. Those are books about w riting PC games. This is a console programming book!

Some things are similar betw een consoles and PCs, but consoles are at a far low er level than PCs w hen it comes to game development. For one thing, at least on the W indow s platform, all commercial games use the DirectX library, a monstrously large runtime for interfacing w ith computer hardw are. In a sense, DirectX turns the hordes of different PCs into a single console platform. W hile console programmers are guaranteed that the machine w ill run exactly the same for every person, that is not the case w ith PCs. DirectX helps to homogenize all the PCs so programmers don't go insane trying to support all the different brands, w ith different CPU s, graphics chips, and so on (w hich is how it used to be back in the M S-DO S era).

You w ill need to be proficient in the C language in order to follow along in this book. Remember the assumption! If you w ant to be a game programmer, you must know C already. If you don't know C at all, you w ill definitely need a primer before getting into the later chapters of this book. There are many beginner books on C available, such as C Program m ing for the Absolute Beginner by M ichael Vine. You needn't even buy a recent book on C, because G ame Boy Advance programs follow the ANSI C standard, w hich has been around for decades. For example, one of my textbooks in college w as C: An Introduction to Program m ing by Jim Keogh, et al, and I used this book as a reference w hile

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXVII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com w riting G ame Boy Advance code. C is an easy language to learn, but very difficult to master: that is part of the mystery of this language, and the source of it's w idespread use. C is a very low -level language that is only a few degrees above assembly language.

I don't cover C++ because it is overkill for G ame Boy Advance development. M ost of the important aspects of programming the G ame Boy Advance (such as sprites) are handled by the built-in hardw are routines, and don't require extensive programmer intervention. For example, a sprite blitter is a given on the G ame Boy Advance, so you w ill definitely not have to w rite one yourself! It's built into the hardw are (w hich means, that functionality is provided by the manufacturer in the design of the system). How ever, I'm not going to argue the pros and cons, because I personally love the C++ language. C is simply easier to understand w hile learning the basics This book does not cover C++, but you m ay still use of G ame Boy Advance programming. C++ for w riting G am e Boy program s if you like, If you w ish to use C++ yourself, you since the H AM SDK includes a C++ com piler. may do so, because the HAM toolkit does support C++.

System Requirements The development tools required to w rite G ame Boy Advance programs have very low system requirements, and w ill probably run just fine on any W indow s-based PC. Even the low liest old Pentium 133 w ill probably w ork, as long as DirectX is installed (for the emulator). How ever, here is a realistic minimum PC:

* W indow s 95, NT, or later * Pentium II 300 M Hz * 128 M B memory * 200 M B hard drive space * 8 M B standard video card

Book Summary This book is divided into four parts:

Part I: The Zen of G etting Started

This first section of the book includes the introductory information you w ill need to get started in G ame Boy Advance development. Included is an overview of the console industry,

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXVIII Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com the G ame Boy Advance hardw are, and how to install and use the HAM SDK (including Visual HAM and the VisualBoyAdvance emulator).

Part II: Being O ne W ith The Pixel

This section is dedicated to getting pixels on the G ame Boy Advance screen, including chapters that cover all six video modes (three bitmap-based, and three tile-based), coverage of sprites, and also w orking w ith backgrounds, including special effects like panning, rotating, zooming, and alpha blending.

Part III: M editating O n The Hardw are

This section focuses on the hardw are aspects of the G ame Boy Advance other than the video system, such as using interrupts and timers to retain a consistent frame rate, converting and playing sound files, checking for button presses, and using low -level assembly language to speed up code.

Part IV: The M other of A ll A ppendixes

This final section of the book includes the appendices, such as the ASCII chart, book and W eb site listing, a G ame Boy Advance hardw are reference, answ ers to the chapter quizzes, and instructions for using the CD-RO M .

Programming The Nintendo Game Boy Advance: The Unofficial Guide XXIX Copyright (c)2003 by Jonathan S. Harbour -- http://www.jharbour.com