Peeko Computer ============== Peeko Computer -------------- This pack contains one cassette and an instruction manual for PeekoComputer on the BBC Microcomputer. Peeko-Computer allows you to watch, and control, a model of a microprocessor at work, it will provide invaluable group teaching material, and many useful insights for those individuals who want to find out how computers work. The Peeko-Computer simulates the operation of a simplified microcomputer in order to teach the fundamentals of machine-code programming. There are ten easily-learned instructions, and these work in conjunction with the visual analogy on the screen to demonstrate the operation of a real microcomputer. Programs can be entered, single-stepped and run with the memory and register contents being displayed at every step. To aid comprehension each instruction mnemonic is displayed as it is encountered. Included on the cassette are five demonstration programs. Instructions ------------ To load and run the Peeko-Computer type CHAIN "PEEKO" and press RETURN. A "Searching" message is displayed on the screen. Now press the PLAY button on the cassette recorder; the program will take about three minutes to load. There are five demonstration programs: COUNT COPY ADD FACTOR PRIMES To load and run these programs first load and run the Peeko-Computer itself as described above. From within the program type L0 (L zero) and enter the name of the demonstration program in response to the prompt for a filename, for example COUNT (quotation marks are not required) and press RETURN. The "Searching" message is displayed. Press the PLAY button on the cassette recorder and the program will load in a few seconds, after which the "Searching" message will disappear. If the searching message remains on the screen you may have run past the program on the tape. In this case rewind the tape and play it again without touching the computer. After three minutes or so the program will be found and loaded, and the "Searching" prompt will disappear. The program is run by typing the command G (Go). Saving And Loading Your Own Programs ------------------------------------ You can save your own programs to memory, and to cassette: Saving To Memory ---------------- To save a program to memory use S followed by a number between 0 and 9; for example S3 and then press RETURN. To load this program back again you would type L3 and press RETURN. Saving To Cassette ------------------ To save a program to cassette use the command S followed by 0 (zero): S0 and press RETURN. Type in your chosen filename, press RETURN, and press the RECORD button on the cassette recorder in response to the prompt - this RECORD instruction will be displayed until the file has been saved. 1. Introduction --------------- This BASIC simulation program is designed to give the complete beginner an idea of how a microprocessor works in terms of data processing and movement. In particular it demonstrates that any given byte of data in the processor's memory could be (a) a coded instruction, (b) part of the address of some other part of memory, or (c) some actual data which is used in the course of the program. The simulated processor has 80 bytes of memory, each of which can contain a single decimal digit. The contents of memory are constantly visible, as are the contents of the accumulator and carry. The set of instructions is limited to ten, most of which are related to 6502 instructions, but if required, the ten that are used to run any given program can be changed. The debugging of programs is greatly simplified by the ability to execute programs a step at a time, with the cursor (program counter) indicating the next instruction to be interpreted. Display ------- [] Once the Peeko-Computer and all its features have been mastered it is quite an easy step to write machine code programs in 6502 code, opening up a whole new range of fast-action applications for the BBC Microcomputer. 2. Peeko-Computer Monitor ------------------------- When Peeko is RUN the user enters the 'monitor' of the Peeko-Computer. The monitor commands are single keystrokes: 0-9 Pressing any digit key will modify the contents of the memory location indicated by the cursor; the cursor then moves on to the next location. The mnemonic of each instruction being entered is shown as the contents of memory are built up, with the format of any arguments expected. G (GO) Causes the Peeko-Computer to execute the program in its memory, starting at location zero. The program will continue to run until it meets a BRK instruction, or the user presses E (ESCAPE). As each instruction is executed its mnemonic is displayed. F (FAST) Similar to G, but the program runs faster. E (ESCAPE) In RUN mode E stops the execution of the program and returns to READY mode. SPACE Allows the program to be executed one step at a time. By repeatedly pressing 'SPACE'. successive instructions are executed, and the mnemonic for each instruction is displayed. 6 → 11 The BBC Microcomputer's usual cursor controls can be used to move the cursor around. O (ORIGIN) Moves the cursor back to memory location zero. S (SAVE) Allows programs to be saved in the BBC Microcomputer's memory and to cassette. The S is followed by a single digit, 0 - 9. 0 indicates cassette, and l - 9 identify the region of memory to which the program will be transferred. When saving to cassette, the Peeko-monitor prompts for the filename and gives directions. L (LOAD) Followed by a single digit, it allows the corresponding program to be loaded from the BBC Microcomputer's memory or cassette into the Peeko-Computer. I Allows the instruction set of the Peeko-Computer to be changed. In addition to the 10 standard instructions there is a second set of 10 instructions, any of which can be substituted for any of the standard ones. In this mode entering S will return the Peeko-Computer to its standard instruction set. P (PRINT) Dumps the program in the Peeko-Computer onto a printer. The dump will be printed in double-width characters on an Acorn GP 80. N (NEXT) Steps through the program, one instruction at a time, displaying each mnemonic. 3. An Introduction to Microprocessors ------------------------------------- 3.1. What Do Microprocessors Do? -------------------------------- 1. A microprocessor uses a number of memory 'locations' each of which contains a 'byte'. In the Peeko-Computer each byte is a single decimal digit. 2. Each memory location is described by a unique 'address', which on the Peeko-Computer consists of two decimal digits; once the Peeko-Computer has been loaded you will see that, reading left to right, row by row, the addresses are 00, 01, 02, ... 78, 79. 3. The byte at each location can represent either: (a) an 'instruction' - on the Peeko-Computer there are ten different instructions, 0 to 9. (b) part of an address - two bytes are required to specify an address. (c) some 'data' which the processor can use and/or modify during the course of a program. 4. The processor itself contains 'registers' which it may use to perform calculations during the operation of a program. The Peeko-Computer has only one - the 'accumulator', and it is here that bytes can be deposited and stored, and then used in any calculations that may occur during the program. 3.2. Instructions ----------------- Each instruction is represented by a mnemonic of three letters. For example, LDA represents the instruction 'LOAD Accumulator'. Different instructions require different information following them: (a) Some require two bytes to specify the address of the memory location to which the instruction refers; for example the 'LOAD accumulator' instruction mentioned above is followed by the address whose contents are to be LOADed. Similarly, the 'store accumulator! instruction, STA, is followed by the address where the byte in the accumulator is to be stored: [] It is important to note that, as illustrated above, in the Peeko-Computer the low byte of the address must be specified FIRST, although instruction notation shows them in the usual order. This is the case with many microprocessors, including the 6502. (b) Some instructions are 'immediately' followed by a single byte which is the data to be used by the instruction. For example, the 'load accumulator immediate' instruction, LDA #, is followed by the data to be loaded: [] (c) Some instructions do not require any data. For example, the 'clear carry' instruction, CLC. When adding two numbers there will never be any carry to begin with, since nothing has been added previously. Hence the 'Clear carry' instruction precedes any addition and simply clears the carry flag to zero. (cf. the 'Add with carry' (ADC) instruction given in the case of second and subsequent numbers.) [] 3.3. Instruction Set -------------------- The standard Peeko-Computer instruction set is as follows: Code Mnemonic Description 0 BRK Break from program to monitor 1 LDA xx Load accumulator from location xx 2 STA xx Store accumulator to location xx 3 CLC Clear carry to zero 4 ADC xx Add the contents of location xx to the accumulator with carry 5 DEC Decrement the contents of location xx 6 INC xx Increment the contents of location xx 7 LDA #x Load accumulator immediately with x 8 JMP xx Jump to xx 9 JNE xx Jump to xx if previous result not equal to zero: zero flag clear 3.4. Programs ------------- A program consists of a sequence of instructions which the processor executes, instruction by instruction, starting from the lowest address until it reaches a BRK instruction. The 'jump' instructions are designed to make the processor leave its normal sequence of execution, and fetch its next instruction from some other specified address. 4. Programming the Peeko-Computer --------------------------------- The following section illustrates the use of the Peeko-Computer's instructions. Most of the instructions will be introduced by means of a demonstration program. Each program will be listed as a series of mnemonics with their assembled code, and in most cases a printout of the screen will show the program listed in the uppermost locations, and the data, or user INPUT in the lower part of the screen. Example 1: Adding Two Numbers [] Loc. Mnemonic Code 00 CLC 3 01 LDA 70 1 0 7 04 ADC 71 4 1 7 07 STA 72 2 2 7 10 BRK 0 First, type in the program starting with the 'Clear carry' code at location 00. Notice how each instruction is built-up in the centre of the Peeko-Computer header. Next input the two digits to be added at locations 70 and 71 - use the cursor controls to get the cursor there. To run the program type space to enter single-step mode. Pressing space a second time executes the first instruction, CLC, which clears the carry flag to zero. The next instruction, LDA 70, loads the contents of location 70 into the accumulator. The ADC adds the contents of 71 to the accumulator with the zero carry and puts the result in the accumulator. Next the STA instruction will store the result in location 72. Finally BRK causes the program to stop. If the numbers chosen added up to 10 or more it will be noticed that 72 only contains the last digit of the result, and that the carry is set to 1. The next example uses the carry flag to add two two-digit numbers. By typing G or F from the monitor the program can be run without single stepping. Example 2: Adding Two Two-digit Numbers --------------------------------------- This adds the numbers in 70,71 and 72,73 and puts the result in 74, 75, 76. Loc. Mnemonic Code 00 CLC 3 01 LDA 71 1 1 7 04 ADC 73 4 3 7 07 STA 76 2 6 7 10 LDA 70 1 0 7 13 ADC 72 4 2 7 16 STA 75 2 5 7 19 LDA #0 7 0 21 ADC 20 4 0 2 24 STA 74 2 4 7 27 DRK 0 [] This program uses one new instruction: this is LDA # which loads the accumulator immediately with the next byte in the program. The function of the instructions at locations 19, 21, and 24 is to set the third digit of the result to 0 or 1 depending on the state of the carry after the addition of the second digits. The 'LOAD Accumulator Immediate' instruction at location 19 loads a value of 0, which, added to the carry set by the previous 'Add with carry' instruction, yields the correct result. Problem 1 --------- Using LDA, STA, ADC and CLC write a program that will add the numbers in locations 70, 71 and 72 storing the two digit result in 73 and 74. Example 3: Countdown Program ---------------------------- The following program will subtract one from location 70 each time it goes round a continuous loop (note: 0-1 = 9): Loc. Mnemonic Code 00 DEC 70 5 0 7 03 JMP 00 8 0 0 This program introduces two new instructions. DEC subtracts one from the specified memory location - in this case 70 - and JMP causes the program to go to the location specified by the next two bytes - like GOTO in BASIC. As this program is a continuous loop, the E key will have to be typed to leave RUN mode. Typing For G instead of space causes the program to run without stopping after each instruction. Example 4: Countdown From 99 ---------------------------- This program is supplied on cassette as the file COUNT. It counts down the two digits in 70 and 71 to ten. [] Loc. Mnemonic Code 00 DEC 71 5 1 7 03 JNE 00 9 0 0 06 DEC 70 5 0 7 09 JNE 00 9 0 0 12 BRK 0 70 9 71 9 By single stepping through this it will be noticed that when location 71 gets to 0 the zero flag changes from 0 to 1. The zero flag is set whenever an arithmetic instruction gives a zero result, and cleared otherwise. The JNE instruction is similar to JMP except that the jump only occurs if the previous result was not equal to zero. This causes the program to count down to zero. The INC instruction is similar to DEC except that it adds one to the following address. It also will set the zero flag if the result is zero. By replacing DEC (code 5) by INC (code 6) in the previous program and setting 70 and 71 to 0 the program can be made to count up to (1)00. Problem 2 --------- Write a program that will count from 99 to 0 and back up to (1)00 in a continuous loop. Alternative Instructions ------------------------ The instructions discussed so far form the standard instruction set. In addition to these there are 10 alternative instructions which can replace any of the standard instructions. Entering the I command in the Peeko-monitor will produce a menu which allows you to select the number of the instruction to be replaced and the number of the instruction that will take its place. This ability to change the instruction set makes the Peeko-Computer very adaptable and able to be programmed for many tasks despite its limited program size. Most of this secondary set can be explained very easily as they are similar to those in the standard set. Alternative instruction set: Mnemonic Description SEC Set carry to 1 SBC xx Subtract contents of xx from the accumulator with carry CMP # x Compare the accumulator immediately with x. If they are equal set zero flag, otherwise clear zero flag JCC xx Jump to xx if carry is clear JCS xx Jump to xx if carry is set JEQ xx Jump to xx if zero flag is set DEC A Subtract one from the accumulator INC A Add one to the accumulator LDA (xx) Loads the accumulator with the contents of the location pointed to by xx and xx+1. STA (xx) works in a similar manner to LDA (xx) storing the contents of the accumulator at the location pointed to by xx and xx+1 The last two extra instructions are an indirect load and store. These are more complex than the others so will be dealt with in greater depth in Example 6. The next five programs use some of these features. Before they are typed in the instruction set should be changed to the required set as shown in each example. Example 5: Subtracting Two Two-digit Numbers -------------------------------------------- Instruction set changes: 3 SEC 4 SBC [] Loc. Mnemonic Code 00 SEC 3 01 LDA 71 1 1 7 04 SBC 73 4 3 7 07 STA 75 2 5 7 10 LDA 70 1 0 7 13 SBC 72 4 2 7 16 STA 74 2 4 7 19 BRK 0 Calculates (70,71) - (72,73) with the result in 74,75. The carry has been set, and if it remains set then this indicates no overflow. On the other hand if the carry is 0 at the end of the calculation then 72,73 was greater than 70,71 and hence there is a negative result. Problem 3 --------- Write a program to multiply two single-digit numbers by successive addition. Example 6: Indirect Addressing ------------------------------ Instruction set changes: 1 LDA ( 2 STA This program uses indirect addressing. If locations 70 and 71 were: [] and location 50 was: [] then LDA ( 70 ) would load the accumulator from location 50 (as 70,71 contains 50 ) and would therefore be loaded with 8. Loc. Mnemonic Code 00 LDA (70) 1 0 7 03 STA (72) 2 2 7 06 BRK 70 0 71 6 72 0 73 5 By running this short program you will be able to see how indirect addressing can be used to move the contents of one address to another address, in this case from 60 to 50. Example 7: Copying a 10-digit Table ----------------------------------- The program for this example is supplied on cassette, as the file COPY, and can be loaded into the Peeko-Computer using the 'LO' Command. Instruction set changes: 1 LDA ( 2 STA ( [] Loc. Mnemonic Code 00 LDA ( 70 ) 1 0 7 03 STA ( 72 ) 2 2 7 06 INC 70 6 0 7 09 INC 72 6 2 7 12 JNE 00 9 0 0 15 BRK 0 70 0 71 5 72 0 73 6 This example shows how indirect addressing can easily be used to access a table of data. The program will copy a line of data, starting at the location pointed to by 70,71, to the location pointed to by 72,73. Locations 50 to 59 should contain the data to be copied to 60 to 69. Example 8: Adding Two Lines of Data ----------------------------------- This program is supplied on cassette as the file ADD. Instruction set changes: 1 LDA ( 2 STA ( 7 STA The program will add two lines of data in the memory together, and store the result. [] Loc. Mnemonic Code 00 CLC 3 01 LDA ( 78 ) 1 8 7 04 STA 73 7 3 7 07 LDA ( 76 ) 1 6 7 10 ADC 73 4 3 7 13 STA ( 74 ) 2 4 7 16 DEC 78 5 8 7 19 DEC 76 5 6 7 22 DEC 74 5 4 7 25 JNE 01 9 1 0 28 BRK 0 74 9 75 6 76 9 77 5 78 9 79 4 This program again uses indirect addressing extensively. Example 9: Finding a Factor of a Number --------------------------------------- This program is supplied on cassette as the file FACTOR. Instruction set changes: 3 SEC 4 SBC 8 JCC The program finds the highest factor less than 10 of a 2 digit number in 78,79. [] Loc. Mnemonic Code 00 LDA 78 1 8 7 03 STA 28 2 8 2 06 LDA 79 1 9 7 09 STA 20 2 0 2 12 DEC 77 5 7 7 15 SEC 3 16 JCC 00 8 0 0 19 LDA # 0 7 0 21 SBC 77 4 7 7 24 STA 20 2 0 2 27 LDA # 0 7 0 29 SBC 76 4 6 7 32 STA 28 2 8 2 35 JNE 16 9 6 1 38 LDA 20 1 0 2 41 JNE 16 9 6 1 44 BRK 0 76 0 77 0 The number to be factorised should be entered in 78,79. Problem 4 --------- Program the Peeko-Computer to divide a two-digit number by a single digit. This will certainly need SBC and SEC and could also need some other instructions. Take the factor program as an example. Example 10: Generating Prime Numbers ------------------------------------ This program is supplied on cassette as the file PRIMES. Instruction set changes: 3 SEC 4 SBC 5 JCS 8 JEQ [] Loc. Mnemonic Code 00 LDA #0 7 0 02 STA 74 2 4 7 05 INC 79 6 9 7 08 JNE 14 9 4 1 11 INC 78 6 8 7 14 LDA #2 7 2 16 STA 75 2 5 7 19 LDA 79 1 9 7 22 STA 77 2 7 7 25 LDA 78 1 8 7 28 STA 76 2 6 7 31 SEC 3 32 LDA 77 1 7 7 35 SBC 75 4 5 7 38 STA 77 2 7 7 41 LDA 76 1 6 7 44 SBC 74 4 4 7 47 STA 76 2 6 7 50 JNE 59 9 9 5 53 LDA 77 1 7 7 56 JEQ 05 8 5 0 59 JCS 32 5 2 3 62 INC 75 6 5 7 65 JNE 19 9 9 1 68 BRK 0 78 0 79 9 5. Answers to Problems ---------------------- Problem 1 --------- Loc Mnemonic Code 00 CLC 3 01 LDA 70 1 0 7 04 ADC 71 4 1 7 07 STA 74 2 4 7 10 LDA #0 7 0 12 ADC 11 4 1 1 15 STA 73 2 3 7 18 LDA 74 1 4 7 21 ADC 72 4 2 7 24 STA 74 2 4 7 27 LDA 73 1 3 7 30 ADC 11 4 1 1 33 STA 73 2 3 7 36 BRK 0 Problem 2 Loc Mnemonic Code 00 DEC 71 5 1 7 03 JNE 00 9 0 0 06 DEC 70 5 0 7 09 JNE 00 9 0 0 12 INC 71 6 1 7 15 JNE 12 9 2 1 18 INC 70 6 0 7 21 JNE 12 9 2 1 24 JMP 00 8 0 0 This program will count up and down locations 70 and 71. You will probably notice that it does not stop at 00 when counting down, but when it decrements from ten (i.e. 12 11 10 00 01 02 ... ). You should be able to correct this when you have found out about the carry flag and its related conditional jumps later. Problem 3 --------- Loc Mnemonic Code 00 LDA 71 1 1 7 03 STA 74 2 4 7 06 CLC 3 07 LDA 70 1 0 7 10 ADC 73 4 3 7 13 STA 73 2 3 7 16 LDA #0 7 0 18 ADC 72 4 2 7 21 STA 72 2 2 7 24 DEC 74 5 4 7 27 JNE 06 9 6 0 30 BRK 0 This multiplies the contents of 70 and 71 putting the result in 72 and 73. Problem 4 --------- Instruction set changes: 3 SEC 4 SBC 9 JCS Loc Mnemonic Code 00 LDA 74 1 4 7 03 STA 78 2 8 7 06 LDA 75 1 5 7 09 STA 79 2 9 7 12 INC 77 6 7 7 15 LDA 79 1 9 7 18 SEC 3 19 SEC 76 4 6 7 22 STA 79 2 9 7 25 LDA 78 1 8 7 28 SBC 70 4 0 7 31 STA 78 2 8 7 34 JCS 12 9 2 1 37 DEC 77 5 7 7 This divides the contents of 74 and 75 by the contents of 76, leaving the result in 77. 78 and 79 are used in the calculation. Peeko-Monitor Commands ---------------------- 0-9 Enter byte into memory SPACE Single-step through program E Escape from program run F Fast execution of program G Go - execute program I Instruction set Load program from memory N Next instruction O Origin - cursor to 00 P Print screen S Save program to memory Instruction set --------------- 00 BRK Break to monitor 01 LDA xx Load accumulator 02 STA xx Store accumulator 03 CLC Clear carry 04 ADC xx Add with carry 05 DEC xx Decrement memory 06 INC xx Increment memory 07 LDA # x Load accumulator immediate 08 JMP xx Jump always 09 JNE xx Jump if not equal 10 SEC Set carry 11 SBC xx Subtract with carry 12 CMP # x Compare immediate 13 JCC xx Jump if carry clear 14 JCS xx Jump if carry set 15 JEQ xx Jump if equal 16 DEC A Decrement accumulator 17 INC A Increment accumulator 18 LDA (xx) Load accumulator indirect 19 STA (xx) Store accumulator indirect Contents -------- PEEKO Five demonstration programs: COUNT COPY ADD FACTOR PRIMES Loading ------- To load and run the Peeko-Computer place the cassette in the cassette recorder, type: CHAIN "PEEKO" and press RETURN, the "Searching" message should appear on the screen as you do this. Now press the PLAY button on the cassette recorder and wait for the program to load. The title page will load in less than one minute, and the main program will take a further three minutes to load, The program will run as soon as loading is complete. Refer to the accompanying booklet for full instructions about the PeekoComputer and the five demonstration programs. Game Credits ------------ Author: Tim Dobson, from an original program by Paul Beverley Acornsoft Limited, 4a Market Hill, Cambridge CB2 3NJ, England. Telephone (0223) 316039 Copyright (c) Acornsoft Limited 1982 You are reminded that the programs on this cassette are subject to copyright, and that it is illegal to reproduce them in any form. Peeko-Computer is a Trademark of Acornsoft Limited. SBE02