Getting to Know the Z80
The Z80 System
Standard Architecture
The Stack
Instructions and the PC
Internal Structure


So what is this stack thing we were hinting at in the last section? To start out nice and brief, the stack is just a section of memory where registers can be stored temporarily. Just think of it as a temporary storage space for data. So why is it even there? Is there a diagram for it? No, not really, because it's more of an abstract idea for how binary is moving around the CPU. But, so you don't get tired of just looking at text, here is a nice representation of what a stack would 'look' like:



Before explaining why it's important, this shows you how it is set up. First, remind yourself that all those little spaces you see represent memory. When data is added to the stack, it is done in what is called a LIFO structure. This stands for 'last-in, first-out.' Since programs function in sequence, whatever data is first placed on the stack comes out last, because all new information is placed on top. Think about an elevator. Imagine that everyone gets out in succession. This means, whoever gets on first, comes out last. Those who get on last, come out first, because they're in the front. Thus, whatever data is placed onto the stack first becomes the last to go out because it's on the bottom. The one that's most recent is on the top, and is thus the first to be utilized. So this is how information is stored in the stack.

But why is it important? Well, there are three reasons for its existence. First, the stack is required to implement subroutines. Subroutines are smaller parts with an independent function that are part of a larger program. Simply put, they're programs-within-programs. They are accessed when specific variables come into play, and then the data flow returns to the main program. So, imagine that the main program is your car in R.C. Grand Prix moving around. There is a specific subroutine called into play when cars collide. When this happens, this mini-program is called up (the opcode you may be familiar with is CALL, ie call CollisionDetection or whatever) to calculate what happens and after this variables are reached that end the subroutine and return to the main program, ie your car moving normally.

The second reason for the importance of the stack is that it is required to implement interrupts. Interrupts are signals to alert the program that some sort of event has occurred. They are essentially forms of subroutines and used quite frequently in the case where you have a periphreal, such as the Control Pad. The Master System, during a game like Cyborg Hunter let's say, will be constantly checking for when you press Button 1 or 2, which will cause the main program to branch into an interrupt causing you to jump or fire. The same thing applies to other items like the Light Phaser or Sports Pad; the Master System checks to see if you are calling an interrupt by pulling the trigger or moving around the track ball and once you do, this calls the interrupt into play and the program branches into a specific action based on it, ie shooting a bullet in E-SWAT.

The third and final feature of the stack is temporary data storage. This helps programs run faster because data is quickly accessed and saved at high speeds, which is what we want. It's very important, for example, in the application of subroutines as we said, where, in order for the data flow to return to the original program after what may sometimes be a number of subroutines, the original address needs to be stored somewhere that enables fast return (the program opcode for this is RETN, by the way). The image way below shows you essentially how this works, but read the next two paragraphs first.

Before we finish up the stack, there's one more important element to mention. This goes along with the LIFO issue. The stack can be accessed through two important instructions, push or pop (the latter is also called pull in some cases, but we'll be using pop). Push deposits data on top of the stack, thus incrementing the SP. What does this mean? Recall what the SP does, it points to the top of the stack. So, when you push, this puts data on top of what was already at the top of the stack, thus incrementing the top up by one. It just means it's position has changed.

As for pop, as you may already have figured out, it's just the opposite. Instead of adding data, you remove data from the top of the stack, which decrements the SP. So, since the SP points to the top, when you take off data, the top moves down, decrementing the SP. Let's check out this image I have for you below to see how this works in addition to displaying the idea of utilizing the stack as a temporary memory location.



What you see here is an abstracted idea of the stack and subroutines. You see stack indicated at the bottom there in each block, followed by some hexadecimal notation I made up on top of them. The one on the first column is the address of the original program. Assume at this point that a subroutine has been implemented, so the CPU needs to mark where the original program is located to go back to it. After this, you see the same hex code on the bottom of another. Now we're pretending another subroutine has been implemented, so you need to store the address of the first subroutine to go back to it before returning to the original program (assuming both need to finish). After this, you see the second address disappear, meaning that the second subroutine ended, returning to the first subroutine, which then ends and returns to the original program based on the code that is popped into the PC after the initial subroutine was completed, leaving the stack ready for its next function.

So that's it. Now, as you realize, these concepts have been thrown out in a very abstract fashion to some extent, you're not seeing how to work with them yet, but they need explained so you understand why they work. Keep in mind that the reason behind this part of the tutorial is to make you see how the Z80 works and what its components are, not what to do with them. That comes when we learn programming. Next, we're going to learn how the PC performs instructions, and after that get deep into the inside of the Z80.

Next Page

Main Page
 
Copyright © 2021 Sega8bit. All Rights Reserved.
Contact Us