MSX Programming – Graham Bland
Total Page:16
File Type:pdf, Size:1020Kb
MSX PrograDlDling Graham Bland Pitman PITMAN PUBLISHING LIMITED 128 Long Acre, London WC2E 9AN A Longman Group Company © Graham Bland 1986 First published 1986 British Library Cataloguing in Publication Data Bland, Graham MSX programming. 1. MSX microcomputers - Programming 2. MSX BASIC (Computer program language) I. Title 001.64'24 QA76.8.M8 ISBN 0-273-02302-0 All rights reserved. No part ofthis publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording and/or otherwise, without the prior written permission of the publishers. This book may not be lent, resold, hired out or otherwise disposed of by way of trade in any form of binding or cover other than that in which it is published, without the prior consent of the publishers. Printed in Great Britain at the Bath Press, Avon Contents Preface v Acknowledgements vi Syntax notes vii 1 First principles 1 (The central processing unit (CPU), Memory, Input and output devices, Bits and bytes, Memory addressing, The MSX-BASIC interpreter, Programs, PRINT, Stopping programs, Displaying programs, Deleting program lines, Renumbering programs, Automatic line numbering, Altering the screen display, The SCREEN command, Altering colour, The function keys, Saving and loading programs, Summary) 2 Elementary MSX-BASIC 12 (Data and data types, Naming variables, The MSX-BASIC operators, Variable assignment, The GOTO statement, Conditional statements and branching, Loops, Variable housekeeping, Global variable typing, Summary) 3 Functions and subroutines 34 (Functions, User-defined functions, Subroutines, Stepwise refinement, Creating subroutine libraries, Summary) 4 Loops, interrupts and event monitoring 48 (The FOR ... NEXT loop, Monitoring devices, Monitoring the keyboard, Error conditions, Timer interrupts, The trigger button interrupt, Monitoring the function keys, CTRL-STOP monitoring, Points to note, Summary) 5 Input, output and string handling 64 (The MSX-BASIC character set, String manipulation, Variations on INPUT, Cursor positioning, Miscellaneous string functions, Formatted output, Using a printer for output, Summary) 6 Data structures 88 (Data lists, Array processing examples, Secondary indexing, Tables, Array housekeeping and general details, Use of files, Summary) iii 7 MSX sound features 103 (The use of sound, The PLAY command, Multiple voices and buffering, The SOUND command, SOUND programs, Summary) 8 Introduction to graphics 124 (The MSX-graphics screens, The use of colour, Painting shapes, Determining pixel colour, Mixing text and pictures, Input while using graphics, The DRAW command, Programming applications, Summary) 9 Advanced graphics 161 (Animation, Properties of sprites, Programmed screen design, Frame-by frame animation, The video random access memory (VRAM), BASE addressing, Additional uses for VRAM, Summary) Appendices 196 (The remaining functions, Error codes, Additional reserved words, Logic tables, Frequency tables, Memory map and the USR function Index 207 iv Preface This book is intended to teach the fundamentals of MSX-BASIC programming. It is assumed that the reader has some degree of familiarity with his computer - using the various functions of the keyboard, for example. The program examples are included to demonstrate features of the language as they are introduced. Wherever possible, these examples are made to be useful in their own right. For example, Chapter 5 introduces a function and shows how it may be used to centre text on a display. Readers may note a distinct bias towards graphics, with two major chapters devoted to the topic. Graphics are possibly the most interest ing and dynamic feature of home or any computers, and are extremely well supported by MSX-BASIC. The advanced graphics chapter, in particular, was included to correct the MSX manufacturer's poor, or simply non-existent documentation relating to the use of Video Mem ory and the Video Display Processor. No attempt has been made to broach the subject of machine code programming. Given the scope of this topic and the fact that this is mainly a book about BASIC programming, I feel that this omission is justified. However, the mechanism for including machine code routines in MSX-BASIC programs is discussed in the Appendices. MSX-BASIC is an ideal introduction to the BASIC language on a number of computers. A glance at BASICA of the IBM Personal Computer shows that MSX-BASIC is virtually identical to the language of the 16-bit machine. A sound knowledge of MSX-BASIC will allow the interested programmer to move on to programming GW-BASIC 2.0, which is offered by a number of 16-bit computer manufacturers including: Compaq, Digital Equipment Corporation, Hitachi, Olivetti, Sanyo, Wang, and a host of others worldwide. Graham Bland October 1985 v Acknovv ledgeIllents In the preparation of this book, I am indebted to Janet Morrison, who provided useful criticism and technical advice. This book is dedicated to young Emma Louise Fenwick, who is as yet blissfully unaware of the existence of computers and, with any luck, should remain that way for a few years yet. Graham Bland October 1985 vi Syntax notes The following notation is used to represent MSX-BASIC commands and functions throughout the book: Keywords are indicated in upper case and must be supplied. Items shown between the symbols ( ) must be supplied. Items shown in square brackets [ ] are optional. Items separated by: symbols require only one item of the list to be supplied. Repeated items are shown by ... All other punctuation must be included where shown. vii 1 First principles A computer is simply a versatile piece of electronics designed to solve problems. These problems can be as diverse as the production of a game and advanced calculus. The computer can accomplish such a wide range of tasks by virtue of its ability to recognize a set of instructions and carry them out. A list of computer instructions is termed a program. Contrary to popular belief, computers are not super-intelligent, nor are they stupid. They are simply machines which carry out instructions to the letter. The supplier of the instructions, the programmer, has to communicate the means of performing a given task clearly and concise ly. The MSX-BASIC language has been designed to ease the task of communication between man and machine. This chapter introduces MSX-BASIC and the elements of an MSX Computer. To begin with, there is an overview ofthe main components of the computer system. The central processing unit (CPU) The central processing unit is the keystone of any computer system. It can read a list of instructions and carry them out. The instructions it obeys are a series of special numbers which comprise machine code. The CPU is responsible for all calculations and the control and monitoring of most of the elements of the computer. In all MSX computers, the CPU is a microprocessor called the Zilog Z80. Memory Memory is used to store information. This information may be a list of instructions that constitute a program, or information that is to be processed by programs. There are two main types of memory that need to be considered. 2 MSX Programming 1 Random Access Memory (RAM) is termed 'volatile', as it may only retain information when supplied with power. 2 Read Only Memory (ROM) may only be read and not written to. It stores information (generally programs) which have previously been 'burned' into it. Information is retained with or without the presence of a power source. Input and output devices Input devices are used to supply the CPU with information. The most important of these is undoubtedly the keyboard. Joysticks, lightpens and touch pads are further examples of this type of device. For output, a monitor is virtually essential. This may be a domestic television set, or a custom-built dedicated device. A printer is another form of output device. Some pieces of equipment may act as both source and recipient of information. The cheapest and most popular input/output device is the cassette recorder. A faster, but costlier alternative is a disk storage unit. Bits and bytes The most basic element of storage in a digital computer is a bit, which is an abbreviation for the phrase binary digit. A bit may have one of two values: either '0' or '1'. A group of eight bits, called a byte, is used to represent information. The Z80 is termed an 8-bit processor as it may only read or write values in groups of eight bits at a time. Using a byte, up to 256 distinct numbers (from 0-255) may be represented. This is achieved using the binary number system. Unlike our more familiar decimal system, binary represents values in base 2. Instead of having units, tens, hundreds, etc., binary has units, twos, fours, eights, sixteens, etc. Here are a few examples of binary values: 100 4 (decimal) 11110001 = 241 (decimal) 1101 = 13 (decimal) A byte is not the only collective name for a group of bits. The following names are also commonly used for different groups: Nibble 4 bits Byte 8 bits Word 16 bits Double word 32 bits First principles 3 Memory addressing Your computer will be advertised as having 64K, 48K, 32K or perhaps 16K of memory. The 'K' represents the number 211il which is 1024. So if you have a 64K computer, the amount of memory will be: 1024 x 64 = 65536 bytes. Each byte of memory is regarded as a unique entity which may be referenced by name. This 'name' takes the form of a number. The Nth memory location is referred to by the number N-1. So the 65536th memory location is given by the number 65535. The MSX-BASIC interpreter Every MSX computer contains 32K of ROM. This stores a special machine code program called the MSX Interpreter. As soon as the computer is switched on, the CPU reads and executes (carries out) the instructions of the interpreter.