Motorola 68000 Series Hardware Assembly Tutorial Part 1 (Revised)
Welcome!
(This is a repost of my original part 1 with errors corrected. Steemit will not allow edits to old posts for some reason. I will link to this revised post for all future lessons. I apologise for my clumsy errors).
Hello and welcome to the first part of my series on how to program for the (in my opinion) most fun processor of all, the Motorola 68000 series. These processors were used in such systems as the Commodore Amiga, Classic Macintosh, Sega MegaDrive/Genesis and Atari ST. This series is going to be super fun and engaging for newcomers and masters alike!
Once this series is over we will have a deep dive into putting this programming into practice on the Amiga platform to make fun games and trippy demos! Impress all your friends! We will start from the very beginning. Zero programming or computer knowledge is required. Let's get started!
What is a CPU?
A CPU, which stands for Central Processing Unit, is the "brain" of any computer. A computer wouldn't be a computer without one. It executes instructions one by one in a sequential order performing such instructions as number arithmetic and moving blocks of memory around. Each instruction takes a few cycles to complete, with more complicated instructions taking more cycles to complete than simple ones. The rate at which a CPU runs is measured in clock cycles per second, mostly termed "hz". For example, the stock M68000 CPU runs at 7Mhz, that is, 7 million cycles per second, which is super fast if you think about it (although modern processors can go up to 4Ghz or 4 BILLION cycles per second. Wowza!
What is the Motorola 68000 series of CPUs?
These CPUs, as mentioned earlier run on such systems as the Amiga. The Amiga 500 comes with a M68000 CPU at 7Mhz but the Amiga 1200 comes with an M68020 at 14Mhz. Accelerator boards are available to provide better models of the CPUs such as 68030, 68040 and 60860. What happened to the 68050? Apparently Motorola forgot how to count.
What? There are more than one in the series? How can I learn to program for all of them?
Fear not! If you write 68000 code, it is almost guaranteed to work perfectly for the later models. Where there is an incompatibility, I will be sure to point it out. The later models contain new features and instructions which are not backwards compatible so if you want to use these newer features you have to make sure you are not using a machine using an earlier model of the 680x0 series.
I don't own a system with these processors! Do I need to buy one?
Absolutely not, although nothing beats programming on the real thing. For people that do not own an Amiga (which is what I will be using to code) you can download an appropriate emulator here. The best one for Windows comes in a package called Amiga Forever. This is not free but it is super easy to set up and contains everything you need to get started. A free alternative for Windows is WinUAE and a great one available on Mac is FS-UAE. Please note the last 2 free emulators do not include the required "Kickstart" file needed to run because the copyright of this file is still owned, however you can use the power of the Internet to obtain it.
However, this generic 68000 programming series will start with a non Amiga specific emulator called Easy68k which is free, simple and will get you through the generic 68000 material of this course until later on in the line where we actually program something only Amiga can run. You can get it here: http://www.easy68k.com However if you want to go straight into using your favourite Amiga assembler then go right ahead!
RAM and why every computer needs it
RAM stands for Random Access Memory. Why random? Because that is exactly what it is. You can access any part of the memory in any random location you wish. The RAM is where your computer stores all its data such as images or storing the number of biscuits you ate this morning. It also stores the code which your computer will execute. The CPU has a counter inside of it counting down your code's instructions in memory one by one. It's sort of like your CPU is reading your code like a script obeying it without question. The CPU can only read data and code that is stored in RAM, and not on your hard drive. If the required data is on hard drive, it must be transferred to RAM first. When you double click on a program, your computer transfers the program's code from your hard drive into RAM where the CPU can read it and it then starts executing at the top of your code.
How is memory stored?
Think of your RAM as a bunch of pigeonholes where a block of information can be put into. You can store all your information inside any pigeonhole you wish, and as long as you remember where you put it, you can access that information again or even take it out and put something else there instead. Sometimes the information is not large enough to fit inside one pigeonhole so you put it all in sequential pigeonholes which the CPU can take out all at once. One pigeonhole (from now on we will call this a memory address or location) can store exactly ONE byte of data, no more and no less.
Wait just a cotton picking minute here, what is a byte?
Well my dear friend, (I am mighty glad you asked), a byte is a sequence of 8 bits. A bit is the tiniest piece of information a computer can deal with. A bit is like a light switch, it can either be on or off. This is represented by a 0 for off and a 1 for on. This numbering system is known as binary. Since a byte is a series of 8 bits, this is an example of a byte:
00111001
One of these bytes can fit into a memory address. If we want to store for example 9 bits in memory, it would not fit in one memory address. What we have to do is use 2 bytes, which is actually 16 bits, but we can instruct the CPU to ignore the 7 redundant bits.
The following groupings of bits is important:
- Byte - 8 bits
- Word - 16 bits (2 bytes)
- Long Word - 32 bits (4 bytes)
The reason we deal with words and long words is as a 32 bit processor, the 68000 can deal with 32 bits at once, so it can deal with long words with a single instruction (talk about efficiency!).
Binary numbers
Since bits can either be a 0 or a 1, in order to count with these digits we can't use our familiar decimal numbering system. Our decimal system uses 10 digits (0 though to 9) to represent numbers. The number of digits to represent numbers is known as the base of the number. Just like every column in a decimal (base 10) number is multiplied by 10 each time you go one digit to the left, in binary (base 2) it is multiplied by 2.
For example in decimal:
The number 1234.
We start from the very right and call this the 1's column. We see a 4 here so this is 4 x 1 = 4. Moving left we get to the 10's column. We see a 3 so 3 x 10 = 30. Left again is a 2 so 2 x 100 = 200. Finally one more to the left we get 1 x 1000 = 1000.
1000 + 200 + 30 + 4 = 1234
And an example in binary:
The number 1101.
We start from the very right and call this the 1's column. We see a 1 here so this is 1 x 1 = 1. Moving left we get to the 2's column (remember in base 2 we multiply by 2 each time). We see a 0 so 0 x 2 = 0. Left again is a 1 so 1 x 4 = 4. Finally one more to the left we get 1 x 8 = 8.
1 + 0 + 4 + 8 = 13
So, the number 13 represented in binary is 1101. The CPU can't understand 13 but it can understand 1101.
Let's count to 15 in binary:
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = 10
1011 = 11
1100 = 12
1101 = 13
1110 = 14
1111 = 15
These are all the possible combinations you can make with 4 bits. Looking at all these binary numbers is tiresome, isn't there a simpler way of representing them? Yep! So let's move next onto my favourite base of all, base 16.
All Your Base are Belong to Us, um... I mean this section is titled Hexadecimal
A number system that makes programmers' lives easier is known as hexadecimal which is a base 16 number system. It uses 16 digits (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) to count with and moving to a column to the left is multiplying by 16 this time. To convert these letters into decimal you can just add 1 each time. A = 10, B = 11 .... F = 15.
For example the number:
A4F5
We start from the very right and call this the 1's column. We see a 5 here so this is 5 x 1 = 5. Moving left we get to the 16's column (remember in base 16 we multiply by 16 each time). We see an F so 15 x16 = 240. Left again is a 4 so 4 x (16^2) = 1024. Finally one more to the left we get 10 x (16^3) = 40960.
5 + 240 + 1024 + 40960 = 42229.
This seems complicated, why does this make things easier? Well remember the 4 bit counting table above? Each combination can be represented by one hex digit.
0000 = 0
0001 = 1
0010 = 2
0011 = 3
0100 = 4
0101 = 5
0110 = 6
0111 = 7
1000 = 8
1001 = 9
1010 = A
1011 = B
1100 = C
1101 = D
1110 = E
1111 = F
Therefore if we want to type out a long binary number, we can simplify it by representing it in hex. All you do is break the number up into 4-bit chunks then use that table to convert that chunk into hex. Memorising this table will save you a lot of time in the future. Let's give it a shot!
01110010100011100111000101111010
That's a binary long word. Let's make this a lot simpler to read. We start by breaking it into 4-bit chunks known as nybbles.
0111 0010 1000 1110 0111 0001 0111 1010
Let's use the lookup table to convert each nybble into its hex representation.
7 2 8 E 7 1 7 A
Isn't 728E717A a lot easier on the eyes and brain than 01110010100011100111000101111010? Yeah, I thought so.
Now that the fundamentals are over, we are ready to move onto actual programming in the next instalment of the series! I will see you all then. Please feel free to ask questions in the comments and follow and share!