Making 8-Bit Arcade Games in C
Total Page:16
File Type:pdf, Size:1020Kb
MAKING 8-BIT ARCADE GAMES IN C An 8bitworkshop Book by Steven Hugg Making 8-Bit Arcade Games in C Copyright ©2017 by Steven Hugg All rights reserved. No part of this book may be reproduced without written permission from the author. First printing: April 2017 Disclaimer Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, nor is any liability assumed for damages resulting from the use of the information contained herein. No warranties of any kind are expressed or implied. Making 8-Bit Arcade Games in C is an independent publication and has not been authorized, sponsored, or otherwise approved by any third party. Trademarks Brands and product names mentioned in this work are trademarks or service marks of their respective companies. Use of a term in this work should not be regarded as affecting the validity of any trademark or service mark. Inquiries Please refer all inquiries to [email protected]. Contents Preface . ix Why read this book? . ix Why focus on arcade games? . ix Which hardware was most influential? . x Why use C on an 8-bit platform? . xi 1 CPU Basics . 1 1.1 Bits, Bytes, and Binary . 1 1.2 Hexadecimal Notation . 2 1.3 Signed vs. Unsigned Bytes . 3 1.4 Integer Overflow and Arithmetic . 4 1.5 The CPU and the Bus . 4 1.6 CPU Instructions . 6 2 The Z80 CPU . 8 2.1 Registers . 9 2.2 Reading and Writing Memory . 11 2.3 Flags and Branching . 12 2.4 Stack . 15 2.5 Logical Operations . 16 3 Hello 8-bit C World . 18 3.1 Preprocessor and Compiler . 18 3.2 Assembler . 21 3.3 Linker . 22 3.4 ROM Image . 24 4 Some C Examples . 25 iii 5 The 8bitworkshop C IDE . 30 5.1 Debug Window . 33 6 The VIC Dual Hardware . 34 6.1 History . 34 6.2 Memory Map . 35 6.3 Video . 35 6.4 Colors . 38 6.5 Typedefs . 40 6.6 A Minimal Main Function . 41 6.7 C Routines . 43 6.8 Controllers . 44 6.9 Z80 Entry Routine . 46 7 Siege Game . 47 7.1 Data Structures . 48 7.2 Main Function . 49 8 Shift and Rotate . 57 8.1 Shift Operations . 57 9 The Programmable Sound Generator . 60 9.1 The AY-3-8910 PSG . 61 9.2 Inline Functions . 65 10 Binary-Coded Decimal . 66 11 Pointers . 68 11.1 A Pointer is a Typed Memory Address . 68 11.2 A Pointer is a Reference To Another Variable . 69 11.3 A Pointer is a String . 69 11.4 A Pointer is an Array ... Sort Of . 70 11.5 A Pointer is an Extra Return Value . 71 12 The Midway 8080 Hardware . 72 12.1 History . 72 12.2 Memory Map . 73 12.3 Graphics . 73 12.4 Draw vs. Erase vs. XOR . 75 12.5 Drawing Text . 77 12.6 Pixel-Accurate Sprite Shifting . 78 12.7 Watchdog Timer . 80 13 Game: Cosmic Impalas . 81 13.1 Game Data . 81 13.2 The Game Loop . 83 13.3 Collision Detection . 86 13.4 Animating an Explosion . 89 14 Interrupts . 91 14.1 Interrupts . 91 15 Randomness . 95 15.1 Galois LFSR . 96 15.2 Entropy . 97 16 Initializing Memory . 98 16.1 Initialized vs. Uninitialized vs. Const . 98 16.2 Initializing Memory . 100 16.3 Initializing Strings vs. Arrays . 101 17 The Galaxian Hardware . 102 17.1 History . 102 17.2 Memory Map . 104 17.3 Other Special Addresses . 108 17.4 Graphics ROMs . 109 17.5 Sound . 109 17.6 When Not to Use memset() . 110 18 Game: Solarian . 111 18.1 Drawing Tiles vs. Sprites . 113 18.2 Attack! . 115 18.3 Directions and Trig . 117 18.4 Alien Behavior . 119 18.5 Returning To Formation . 120 18.6 Missiles . 121 18.7 Blowing Up Aliens . 122 18.8 Game Sound . 123 v 18.9 Keeping in Sync with Interrupts . 124 19 Making Music . 126 19.1 Hitting the Right Note . 126 19.2 Laying out the Score . 127 19.3 Swinging with the Tempo . 128 19.4 Composing the Music . 130 19.5 Need Some MIDI Files? . 130 19.6 Scramble/Frogger Sound Board . 131 20 Integers and You . 132 20.1 Powers of Two . 132 20.2 Unsigned Comparisons . 134 21 The Atari Vector Hardware . 135 21.1 History . 135 21.2 How Vector Monitors Work . 136 21.3 Analog Vector Generator (AVG) . 137 21.4 Z80 Memory Map . 140 21.5 AVG C Routines . 141 22 3-D Vectors . 144 22.1 3D Types . 144 22.2 Sine and Cosine . 146 22.3 Wireframe Models . 148 22.4 Transformation . 150 22.5 The Mult Box . 150 23 Game: Crowded Orbit . 152 23.1 Shape Prerendering . 152 23.2 Actors . 154 23.3 Drawing Actors . 155 23.4 Moving Actors . 156 23.5 Allocating Memory with malloc() . 157 23.6 Linked Lists . 158 23.7 Function Pointers . 160 23.8 Deleting Objects . 161 23.9 Collision Detection . 162 23.10The Main Loop . 163 24 The POKEY Sound Chip . 166 24.1 Memory Map . 167 24.2 AUDCTL Bit Map . 167 24.3 AUDCx Bit Map . 167 24.4 Setting AUDF Frequency . 168 24.5 POKEY Music . 169 25 Miscellaneous C Topics . 170 25.1 Unions . 170 25.2 Big Numbers . 171 25.3 Advanced Loop Handling . 171 25.4 Operator Precedence . 172 26 Williams Hardware . 173 26.1 History . 173 26.2 Memory Map . 174 26.3 Bankswitched RAM/ROM . 175 26.4 Video Frame Buffer . 175 26.5 Palette . 176 26.6 Bit Blitter . 177 26.7 Blitting to Pixel Boundaries . 182 26.8 Other Hardware Locations . 182 26.9 When Not to Use memset() . 183 27 Williams Sprites . 184 27.1 Keeping Track of Objects . 184 27.2 Getting Rid of Flicker . 185 27.3 Collision Detection . 187 27.4 Clipping . ..