ebook img

Advanced 6502 Programming PDF

301 Pages·1982·3.528 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Advanced 6502 Programming

ADVANCED 6502 PROGRAMMING RODNAYZAKS BERKELEY • PARIS • DDSSELDORF SYM is a trademark of Synertek Systems, Inc. KIM is a trademark of MOS Technology, Inc. AIM65 is a trademark of Rockwell International, Inc. "COMPUTEACHER" and "GAMES BOARD" are trademarks of Sybex, Inc. Cover design by Daniel Le Noury Technical illustrations by Guy S. Orcutt and j. trujillo smith Every effort has been made to supply complete and accurate information. However, Sybex assumes no responsibility for its use, nor for any infringements of patents or other rights of third parties which would result. No license is granted by the equipment manufacturers under any patent or patent rights. Manufacturers reserve the right to change circuitry at any time without notice. © 1982 SYBEXInc., 2344Sixth Street, Berkeley, CA94710. World rights reserved. No part of this publication may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic or other record, without the prior agreement and written permission of the publisher. Based on 6502 Games by Rodnay Zaks, © 1980 SYBEX Inc. Library of Congress Card Number: 82-160235 ISBN 0-89588-089-X Printed in the United States of America 10987654321 Contents Preface vii 1 Introduction 1 Optional Hardware Support 2 Connecting the System 4 Ga.mes Bol\r.SJPtei;couuect ,,,..,..., 7 fr,~;}(~1~~~~~ 13 2 Generating Square Waves (Music Player) 20 Introduction 20 The Rules 20 A Typical Game 22 The Connections 22 The Algorithm 22 The Program 23 3 PseudoRandomNumberGenerator (Translate) 41 Introduction 41 The Rules 41 A Typical Game 42 The Algorithm 43 The Program 43 4 Hardware Random Number Generator (Hexguess) 59 Introduction 59 The Rules 59 A Typical Game 59 The Algorithm 60 The Program 60 5 Simultaneous Input/Output (Magic Square) 73 Introduction 73 The Rules 73 A Typical Game 76 The Algorithm 78 The Program 80 6 Simple Real Time Simulation (Spinner) 87 Introduction 87 The Rules 87 The Algorithm 88 The Program 89 V 7 Real Time Simulation (Slot Machine) 99 Introduction 99 The Rules 99 A Typical Game 100 The Algorithm 101 The Program 112 8 Real Time Strategies (Echo) 137 Introduction 137 The Rules 137 A Typical Game 139 The Algorithm 141 The Program 144 9 Using Interrupts (Mindbender) 162 Introduction 162 The Rules 162 A Typical Game 162 The Algorithm 165 The Program 167 10 Complex Evaluation Technique (Blackjack) 189 Introduction 189 The Rules 189 A Typical Game 190 The Program 194 11 Artificial Intelligence (Tic-Tac-Toe)) 218 Introduction 218 The Rules 218 A Typical Game 218 The Algorithm 224 The Program 247 Appendices 287 A. 6502 Instructions-Alphabetic 287 B. 6502 Instruction Set-Hex and Timing 288 Index 290 vi Preface This book has been designed to teach you advanced programming techniques for the 6502 microprocessor in a systematic and progressive way. Developing a program involves devising a suitable algorithm and appropriate data structures, and then coding the algorithm. In the case of a microprocessor such as the 6502, the design of the algorithm and the data structures is generally constrained by three conditions: 1. The amount of memory available is often limited or must be minimized; i.e., the program must be terse. 2. The highest possible execution speed may be required. Efficient coding of the program into assembly level language instructions then becomes an essential consideration. In particular, the use of registers must be optimized. 3. The specific input/o utput design requires an understanding of the input and output chips and their programming. Thus, when evaluating designs for an algorithm and data structures, the programmer must weigh the merits of the various techniques in terms of his skill, the memory limitations, the requirec-1. speed of execution, and the overall probability of success. Advanced programming for the 6502, therefore, involves knowledge of all the chips that may be affected by the program, in addition to the usual programming skills concerned with the algorithm, the data structures, and the efficient use of internal instructions and registers. This book provides a comprehensive and complete overview of all the important techniques required to program a 6502 system efficiently. The book has been designed as an educational text. Each chapter introduces new con cepts, chips, or techniques in turn. In the final chapters more complex algorithms are presented, which integrate the techniques presented throughout the book. For clarity and consistency, this book uses a specific 6502-based system on which all the programs will run. The details are presented in Chapter 1. However, the programs and techniques presented here are applicable to all 6502-based systems. Similarly, all the programs studied in this book are presented in the form of realistic games involving success ively all the techniques described. They cover most types of applications ranging from simple input/ output techniques to sophisticated real-time simulations, including the handling of interrupts and the design of com plex data structures. vii ADVANCED 6502 PROGRAMMING A case study approach is used, and each chapter contains the following: 1. A description of the concepts and techniques to be studied 2. The specifications of the program's behavior and a typical session with the program, i.e., the problem to be solved 3. The algorithm(s): theory of operation, design, and trade-offs 4. The actual program: data structures, programming techniques, specific subroutines, merits of alternative techniques, and a com plete program listing. Variations and exercises are also proposed in each chapter. Thus, you will first study the definition of the problem, then observe the expected program behavior, and then learn how to devise a possible solution (algorithm plus data structures). Finally, you will design a complete program for this algorithm in 6502 assembly level language, paying specific attention to the required data structures, the efficient use of registers, the input/o utput chips, and the techniques used for efficient programming. You will sharpen your skills at using input/o utput techniques including timers and interrupts. But most importantly, you will be consistently reminded of the trade-offs between ease in programming, use of memory, efficiency of execution, and algorithmic improvements by use of specialized hardware or software techniques. In order to learn the advanced programming techniques presented in this book, it is not necessary to build any actual hardware. However, it is necessary to write programs on your own along the ten chapters of this book. By showing you and explaining in detail the design of many actual programs, the author hopes to facilitate your next step: actual programming. viii Acknowledgments The author would like to acknowledge the contributions of Chris Williams and Eric Novikoff, who thoroughly checked all of the games programs and con tributed numerous ideas for improvements. The author is particularly indebted to Eric Novikoff for his valuable assistance throughout all phases of the manuscript's production, and for his meticulous supervision of the final text. The author would also like to express his appreciation to Rockwell International and in particular, to Scotty Maxwell, who made available to him one of the very first system 65 development systems. The availability of this powerful develop ment tool, at the time the first version of this book was being written, was a major help for the accurate and efficient check-out of all the programs. 1. Introduction In order to learn the techniques and study the program examples presented in this book, no specific equipment is required. However, the availability of a 6502-based system is a major advantage to develop and test 6502 programs on your own. Bear in mind that each 6502-based system will have a somewhat different input/ output configuration. The techniques presented in this book are applicable to all, and the programs can be easily adapted once you understand input/ output operations. To read this book, you should be familiar with the 6502 instruction set and basic programming techniques on the level of Programming the 6502. A basic knowledge of input/o utput techniques is also recommended. (This topic is covered in 6502 Applications.) The programs presented in Chapters 2 through 11 range from simple to complex. In order to implement these programs, algorithms will be devised and data structures will be designed. This is the process any disciplined computer programmer must go through when designing a 1

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.