IGNORED

# Z80 Assembly - Module 1 - Basic Z80 programming

## Recommended Posts

Ok, let’s start our Z80 Assembly programming course.

Here is how I plan to organize this course:

Module 1 – Basic Z80 programming

Module 2 – Advanced Z80 programming including peripheral programming

Module 3 – Introduction to game logic

Module 4 – Advanced game logic

So let’s start Module 1 with a quick historical background.

The Z80 was first released in 1976 by a company called Zilog. Its main attractive at the time was being compatible with another CPU called the i8080 made by Intel. The i8080 was probably the first important 8 bit CPU, and because of that an operational system called CP/M was developed for it. CP/M became the first the facto standard OS available for personal computers. But the Z80 wasn’t only compatible, as actually it extended the 8080 in many ways, so by the late 70s it had mostly replaced the 8080 as the CPU of choice for CP/M.

Around the same time (late 70s) the Z80 also became popular with arcade maker, especially in Japan. From 1979 to 1983 the Z80 was by far the dominant CPU in arcades. So it was natural that Coleco decided to use the Z80 in their “arcade quality” video game system.

The Z80 is an 8-bit CPU, meaning it can process 8 bits a time.

Now what is bit, you ask.

Well, you can understand bit as the smallest information unit that a CPU can store or transfer. Still too vague? Ok, a microchip is made of very small structures called transistors. You can understand transistor as a sort of switch, which can be either on or off, so two possible states. Bit is the representation of that, but instead of referring to on/off, we use “0” and “1” to represent the possible bit values. Better now?

Ok, I said the Z80 is an 8-bit CPU. You already know what a bit is. A group of 8-bits is usually referred to as a BYTE (I believe it comes from English acronym for “BinarY TErm”.

Everybody is familiar with the decimal numeral system, where we can use 10 different digits (0 to 9) to represent all sort of quantities. We can do the same with bits, and it is called the binary numeral system, since we only have two different digits to represent quantities.

So let’s see, if we have a byte, which means 8-bits, how many quantities can we represent?

First using two bits. Possible values are:

00, 01, 10, 11. So 4 possible values.

Now using three bits:

000, 001, 010, 011, 100, 101, 110, 111. So 8 possible values, which is twice as many as we could represent with 2 bits. Well, it isn’t too hard to figure out that every extra bit we add doubles the number of values we can represent, so the number of possible values is given by 2^n (2 to the n power), where “n” is the number of bits. Thus, 8-bits can give us 2^8=256 possible values.

Every number in binary system has a correspondent in the decimal system. For example, supposing the 3 bits example. 000 represents 0 in decimal, 001 is 1, 010 is 2, 011 is 3 and so on.

There is an easy way to determine the decimal equivalent of a binary number. Here is how you do that for an 8-bit number:

Starting from right to left, each bit is worth a certain decimal value:

128 | 64 | 32 | 16 | 8 | 4 | 2 | 1

The rule is to add the decimals for each bit that is “1” in the binary value. For example, the binary number 01100011.

The “1” bits correspond to the 1, 2, 32 and 64 positions, so we add those numbers. 1+2+32+64 = 99. So the binary above corresponds to 99 in decimal.

Very good. So we have just learned about bytes, bits and the binary system.

Ok, so we know the Z80 can deal with 8-bit values, hence 256 possible values.

So does that mean that the Z80 cannot deal with values greater than 255? No, it can deal with values as big as we want, however the value must be broken into smaller 8-bit segments or components.

External memory

Any CPU is usually connected to an external memory or memories. Memory is the structure where the CPU stores data or where it reads the program from. A program is a sequence of commands that says the CPU what to do. Data is stored and read by the CPU while it executes a program. For example, the graphics that are displayed on screen are a type of data.

The ColecoVision has two different types of memory: RAM and ROM. ROM is the memory present inside each cartridge. The CPU can read it, but it cannot change the data or store new data in ROM. ROM means Read Only Memory. So a cartridge usually contains the game program and all the audio and video data.

RAM is the memory inside the ColecoVision. It is used as “work” memory, a place where the CPU can temporarily store the variable data used by the game, like the position of objects on screen, the current score, etc. RAM can be read from or written to and the content is lost once the ColecoVision is turned off.

Each memory cell or address can store an 8-bit value. Now, how does the Z80 access memory?

Even though the Z80 is an 8-bit CPU, it can “address” 16-bits of external memory. The reason is simple, 8-bit would represent only 256 possible memory addresses, and since each address store 1 byte, that would mean 256 bytes maximum. 256 bytes is certainly too little.

So instead the Z80 can address 16-bits memory positions, or 65,536 possible addresses if you do the math. In computer science it is usual to represent a “kilo”(a thousand) as 1,024, not the usual 1,000. The reason is because 1,024 is the closest to 1,000 that can be represented by a power of 2 (2^10). So 65,536 is exactly 64Kbytes.

So just to sum up, the Z80 can manipulate 8-bit data, but can address 16-bits of external memory.

Ok, I think this is enough for now. Sorry for all the technical stuff, but unfortunately we need to go through it if we want to program a video game.

In the next lesson we are going to discuss the Z80 internals.

##### Share on other sites

This is the best description of binary data I've ever seen. I had never understood how to read binary numbers before, but this lesson made it quick and easy to interpret. Great job!

##### Share on other sites

Fantastic start - can't wait for Part 2

##### Share on other sites

• 8 months later...

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.