                              _Bonkers_

Level #01
                         (c) Curtis White 

FREEWARE: NO SELL OF THIS MAGAZINE IS ALLOWED WITHOUT PRIOR WRITTEN 
                            PERMISSION.

Use the force!

This is the first issue of the coding magazine Bonkers. Our mission is 
to provide the reader with a strong background in 65XX machine language. 
This magazine is intended for someone with little or no experience in 
coding.  
 
Before we get started, I'd like to thank Coolhand and Dokken for their 
help in making this the best magazine possible, thanks! 


Why learn 65xx machine and assembly language? Well, for several reasons. 
It will allow you to code demos, games, applications, and even entire 
operating systems. But more then that, many people find it to be the 
most fun and rewarding thing about computers. And what you learn here 
will be of benefit if you decide to code on other computers.

We hope you will enjoy reading and following each issue of Bonkers.	
					
                                    Signed, The Bonkers Staff

Editor: Coolhand                  < Send your opinions, thanks,
Technical Director: Dokken        < and gripes to these clowns!
Author: Light                     < 


Stage #01 - I turn the light off, I turn the light on.

These are the only two states the computer knows, on and off. Every 
digital computer only knows these two properties. Why is that? Because 
the computer uses a series of electrical pulses, and each 'location' in 
the computer either has a charge or doesn't have a charge. It is easy to 
represent off as the number 0 and on as the number 1. In fact, there is 
a number base specifically for this; that system is binary. You may not 
know that us humans use the number base 10, or the decimal base. We call 
a single binary digit (recall this is either on or off) a bit. As you 
would expect, a bit can only be 0 or 1. Now that is not very powerful, 
until you realize that you can string these together to command the 
computer to do anything you wish.

%110111 - this is 55 in binary. Note the % tells us that 
          the number does not equal 110,111.

Your saying, 'duh duh duh' about now, right? Well, just keep reading! I 
promise it gets better then this.

Each bit in a binary number can be given a weight so we can more easily 
understand it.

     Weight: 128:64:32:16:8:4:2:1
Binary Code:   0: 0: 0: 0:0:1:0:0 

Each one in the binary code is given the weight corresponding to it.

So the binary code of 00000100 = 4. Yeah, it isn't easy for us, but it's 
a snap for the computer.

Here are some more examples:

     Weight:128:64:32:16:8:4:2:1
Binary Code:  1: 1: 1: 1:1:1:1:1 = 255 Decimal

     Weight:128:64:32:16:8:4:2:1
Binary Code:  1: 0: 0: 0:0:0:0:1 = 129 Decimal

     Weight:128:64:32:16:8:4:2:1
Binary Code:  0: 1: 1: 0:1:0:1:0 = 106 Decimal

Have you noticed how each bit to the left has a weight of double the 
previous amount? Yep, that's important. Let's try 16 binary bits now! 
You can understand why coders are always hungry...

The weight:
32768:16384:8192:4096:2048:1024:512:256:128:64:32:16:8:4:2:1
    1:    0:   1:   1:   0:   1:  0:  0:  1: 0: 0: 0:0:1:0:1

= 46212 Decimal!

Oh no! I can't even verify that on my TI-68 calculator. Guess you'd 
better learn now. No fret, you will not be doing much in binary. You 
just need to know that it's there.

Eight bits = 1 BYTE = 255 DECIMAL = 11111111 BINARY

Okay.. so we know a bit about binary, bah. Now, lets get into 
hexadecimal, another number system, base 16.  When you represent a 
binary number in hexadecimal you use a group of 4 bits or a nybble.

0000 - 0   0100-4   1000-8   1100-C
0001 - 1   0101-5   1001-9   1101-D
0010 - 2   0110-6   1010-A   1110-E
0011 - 3   0111-7   1011-B   1111-F

Here are some examples:

	0001 1111 0100:Binary
	   1    F    4:Hex
                   500:Decimal
	              
	0001 0100 0110:Binary
           1    4    6:Hex	
                   326:Decimal

When counting in hex, it goes like this:

HEX\/\/\/
1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 16 17 18 19 

Here is a table to help you when your practicing:

$10 - 16d   $1b - 27d  $26 - 38d
$11 - 17d   $1c - 28d  $27 - 39d
$12 - 18d   $1d - 29d  $28 - 40d
$13 - 19d   $1e - 30d  $29 - 41d
$14 - 20d   $1f - 31d  $2a - 42d
$15 - 21d   $20 - 32d  $2b - 43d
$16 - 22d   $21 - 33d  $2c - 44d
$17 - 23d   $22 - 34d  $2d - 45d
$18 - 24d   $23 - 35d  $2e - 46d
$19 - 25d   $24 - 36d  $2f - 47d
$1a - 26d   $25 - 37d  $30 - 48d
	
Useful Notation:  $ for hex numbers
                  % for binary numbers
                  d for decimal numbers


___ Power-up Time____

Questions //\\//\\//

1. 	What number system do computers use?
2. 	What number system do people generally use?
3. 	What number system is a go-between decimal and binary?
4. 	Convert %100001001 TO HEX
5. 	Convert %111100001 TO DECIMAL
6. 	Convert FF to Decimal
7. 	Convert FA to Decimal
8. 	Convert FB to Binary
9. 	Convert FC to Binary

___Stage Boss___

Finish the chart up to 255 decimal! :D

Stage #01 Completed_

______Things You've Learned_____

You learned about binary, decimal, and hexadecimal. You learned how to 
convert between these three popular number systems. You've gained an 
understanding of the internal workings of the computer.


Stage #02 - Get Ready!


Here are some things you may already know about your computer.. but just 
in case.

RAM: Random Access Memory. You can read and write to it.
ROM: Read Only Memory. You can only read from it, i.e.,
     the basic program that the computer runs when it boots.	
 IA: Interface Adapter. These is includes your SID, VIC,     
     PIA.

Inside the CPU.

I'm sure you've heard a lot about the CPU especially with all the new 
chips out.  Let's see how it works for real.

Virtual View Of 65XX chip:

         PC(16 bits): Program Counter
Accumulator (8 bits): What type of name is that?!  Consider it 
                      the heart of the cpu, just as the cpu 
                      is the heart of the computer.
 X Register (8 bits): An indexing register, you will learn  
                      how this works soon.
 Y Register (8 bits): Another indexing register, similar
                      in function to the X Register.	
     Status Register: This will be important when we start	
                      doing compares.	

An important aspect of the computer is the layout of memory. We will not
go into much detail on this, but we need to cover it. Every location or
address in your computer contains a value of 0 to 255 or a byte. So the
location 49152 might contain the byte 155.  When we read the location
49152, we 'copy'  this value into the accumulator or another machine
language register. We might want to do this to 'paste' it somewhere
else. Can you think of some reasons we would want to do this? Well, we
might do this in a scroll or moving a stick man across the screen. Other
examples include reading the joystick, accepting input in a word
processing program, and much more!

We are almost ready to code, so we will go over two very useful machine 
language instructions. LDA which means 'load accumulator', one of the 
most used instructions. What is this load accumulator? Well, it 'loads' 
a value from memory, like when you load a program. Hmm.. consider it a 
grabber. You can reach in and grab memory from anywhere in your 
computer. You do this with LDA. Look at this:

BYTE OF MEMORY=5         ACCUMULATOR=0    - before LDA
LDA Byte OF MEMORY       ACCUMULATOR=5    - after LDA

The other instruction we will look at is STA (Store Accumulator To 
Address), like this:

BYTE OF MEMORY=5         ACCUMULATOR=25    - before STA
STA Byte Of Memory       Byte Of Memory=25 - after STA

If we look at LDA as the copy instruction, then STA is the paste (or 
grab and drop). The accumulator serves as the clip board of sorts. 

Time to jam, time to code. First, you will need a machine language 
monitor. I will be using Gnome Kit 64. Since monitors are usually not 
exactly the same, you will have to see what does what in your monitor 
(or better use Gnome Kit 64).

.49152 LDA 49159 - Tag this, we may have to change it.
.49155 STA 49160 - Tag this..
.49158 RTS       - Returns us to Basic.	              

When you type the first line in, your monitor should give you the .491XX 
on the next line, and of course don't type anything after the - (those 
are remarks which the monitor will not understand). If you get a '?', 
then that usually means your typing something in that is wrong; enter 
the line again. Notice the 'tag this'. We wanted to load in the byte 
right after the end of our code and store it to the next location; so if 
we had guessed where our code would end wrong, we would have had to 
change it. Before you execute this, do a:

.49159 B - (we must verify what is at this location)

It will give you a list of numbers. The first number will be what's 
stored at 49159; the next number will be 49160. We are moving 49159 to 
49160, so make sure they are different. If not, then type over the 
number that is there and press return. Now, we will 'run' the code with 
an sys 49152. After doing this, you should have your cursor back with no 
apparent change. Now type .49159 b. Did the location of 49160 change? It 
should have. If it didn't, then check your typing. 

Let's try something else as well. We are now going to disassemble what 
we just now coded! Type.. .49152 b. You should get something like:

.49152 b 173 7 192 141 8

The 173 is the op-code for LDA absolute (we will learn 'immediate', and 
other modes later on). 7 is the low byte of 49152, and 192 is the high 
byte of 49152. Notice how the low byte is first and the high byte is 
second. This is called low byte, high byte order. You should remember 
this, as it's an important feature of the 65xx architecture. Wait, your 
saying how in the world does 7, 192 = 49152 (multiply 192*256 and add 7, 
te da!).

Make sure your understanding everything as we are going over it, because 
we are walking now - soon we will be flying. 

The task...

Suppose we were walking down the street, and someone came up and said "I 
can't type. When I type, I get so excited that I always type the words 
in backwards; here's a million dollars, fix this!". Well, we won't code 
a general application (yet), but we can code a specific example.
 
First, a quick diversion. We need to know how screen memory works. 
Briefly, screen memory starts at 1024 and goes through 2023.  By placing 
any of the screen codes in these locations, we can form letters and, in 
turn, make entire words. It's arranged in a matrix of 40 columns by 25 
rows. Reading any of these locations tells us the character at that 
particular location; writing any of these locations will place the 
character at that location. For reference, 1024 is the first location 
which is located in the upper left hand corner of the screen. This would 
be column 0 and row 0. Column 1 and row 0 would place you at location 
1025. Row 1, Column 1 would be location 1065. The character placed there 
will be dependent on how the value correlates to the screen codes. 

If your still not sure how screen memory works, consult the screen 
location table in any of several popular c64 programming books. Okay, 
now back to our million dollar program.

.49152 LDA 1024 - Grab value at 1024
.49155 LDX 1025 - Grab value into 1025
.49158 LDY 1026 - Grab value at 1026
.49161 STA 1026 - Drop value into 1026
.49164 STX 1025 - Drop value into 1025, no change
.49167 STY 1024 - Drop value into 1024
.49170 RTS

;type 'rac' in the upper left hand corner of the screen.

When you execute the code, you should get 'car'. Notice the four new 
instructions, LDY, STY, LDX, STX. Can you guess what these do? Well, if 
you said LDY (load memory into y register), STY (store y register to 
memory), LDX (load memory into x register), and STX (store x register to 
memory), then you are precisely right. These four new instructions work 
just as LDA and STA do. In fact, just as there is an accumulator, there 
are X and Y registers. For now, we can consider each of these having the 
same properties, but we will learn the unique properties of each of 
these various registers in the coming Levels.

___ Power-up Time ____

Questions //\\//\\//

1. 	What instructions can we use to 'grab' or load values  
	from memory into our registers?
2. 	What instructions can we use to 'drop' or store 
	information to memory?
3. 	Write a machine language program to copy your name from 
	somewhere on the screen to another place on the screen. 
4. 	Write a machine language program to read in a word or 
	words and paste them vertically down the screen.

___Stage Boss___

Code a machine language program to read in your name (from screen memory 
or otherwise) and copy it to the screen. Try at least four ways. If you 
really feel up to it, do six!

Like geometric shapes, circles, boxes, lines, - oops, no more ideas. 
Hint, you may need to look at the layout of screen memory.

Stage #02 Completed_

______Things You've Learned_____

Well, you have learned a lot in this stage. You worked with the monitor. 
You learned about loading (lda,ldy,ldx) and storing to memory 
(sta,sty,stx). You created a million dollar program and experimented 
with screen memory.  
 
_____Level #01 Completed_____

That's right, it's the end for this first issue of Bonkers. We will try 
to release on a very regular basis, hopefully, every week or at least 
every other week. And since we are a current magazine, if you have any 
questions, then you can always drop us an email or check us on IRC, 
channel #c-64. Where can you get the latest issue of Bonkers? Well, from 
any of the staff, or from Bonkers web page at 
http://soho.ios.com/~coolhnd/bonkers/planet.htm.

We hope you've enjoyed this issue. I know I've enjoyed writing it. Now 
go code! And grab the next issue of Bonkers; it'll be here before your 
socks start stinking, I promise. 

                                           Bonkers Staff
