RAM

There are elements of the 6502 that I wish to make use of in my programs but cannot without any RAM (Ramdon Acces Memory)! The stack, interrupts and subroutines all require RAM. As seen previously, it is possible to build a 6502-based computer without RAM but these programs did not make use of these elements!

More specifically, the RAM is needed for what is known as page 0 (often called page zero, zero page and ZP) and page 1.

Page 0 is used to describe the address space from $0000 – $00FF, which is 256 bytes. Most programs will use some RAM in this space. There are a number of occasions (subroutines, interrupts) where RAM in page 0 is needed.

Looking at the addressing modes of the 6502 datasheet (figure 1), it is stated that the stack may use memory in the address space from $0100 – $01FF, which is 256 bytes. Therefore, when writing programs, when the stack is used, this is the area in RAM where the hardware stack will be called upon. This address range is known as page 1.

stack memory range 6502 62256 page 0 page 1 page zero zp
Figure 1. Stack memory range

Stack

The stack is an area of memory in the 6502 for temporary data storage. A user can directly place data onto the stack and the 6502 can use the stack during a subroutine and when interrupts are called. One of the five registers is named the stack register, which points to the next available location on the stack (figure 2). The stack is known as a push down stack. This means that as the stack grows (by pushing onto the stack), the address goes down. Conversely, when pulling from the stack, the address goes up. Therefore it is common to write a routine that sets the stack to 01FF and as you push values onto the stack, you move towards 0100. The user accesses values by using commands that push onto the stack and pull from the stack. When you pull a value from the stack, you always remove the last item that was added, this is known as FIFO (First In First Out).

6502 pushing and pulling the stack
Figure 2: Pushing and pulling the stack

Allocating for page 0 and page 1, we can see that already, we need at least 512 bytes of RAM.

stack overflow

When the stack is growing, if you write a program that causes the stack pointer to go further down that 0100, everything will still work. The stack wraps around and then starts at 01FF. This is not a problem if there is nothing in that location. However, if you are in locations where there is data, it will be overwritten! This is called a stack overlow! The stack is 256 bytes in size and therefore you can never have more than 256 bytes of data on the stack!

Subroutine

Often in programming, there are occasions when you repeat what you have written – an alternative is to write a subroutine. When the subroutine is required, a jump to subroutine instruction is issued. At the end of the subroutine, a return from subroutine instruction is issued. A subroutine can be called as many times and from as many locations as needed.

There are two instructions, JSR and RTS, which are used when writing a subroutine.

JSR means jump to subroutine. This pushes the return address onto the stack and changes the program counter to the specified address.

RTS means return from subroutine. The return address is taken from the top of the stack and placed into the program counter.

GOSUB

If you know BASIC, then this is similar to writing a GOSUB

Interrupts

Interrupts allow the 6502 to stop what it is doing and attend to something that is deemed more important, such as a keyboard press. There are two types of interrupts: maskable and non-maskable. The address space for IRQ is $FFFE – $FFFF and $FFFA – $FFFB for NMI.

Which RAM IC?

There is a plethora of RAM ICs available to choose from. I will be using the 62256. The 62256 is a 32K RAM IC. There are different types of RAM, the 62256 is a static RAM IC. Static RAM means that while it is still powered, it retains its data. This is the opposite of dynamic RAM, which needs to have its data constantly refreshed!

65256 pinout
Figure 3: 62256 pinout

Why are there 15 address lines?

There is a reason why there are 15 address lines! It’s because it has space for 32K. The two numbers are linked! 2^15 = 32768. Note that the answer is not 32000! Humans like nice round numbers!

Figure 4 shows the access times of the variations of the 62256 that are available. You could use any of these but if you plan on pushing the speed, then you want the ones with the fastest access time. As mentioned previously, regardless of the clock speed, there are components that will affect the maximum speed that is attainable. The RAM is one of these and the access time will place a limit on the maximum speed. As highlighted, the two fastest have an access time of 85ns.

62256 access time
Figure 4. 62256 access time

Now I can write programs for my computer that use the stack and therefore subroutines and interrupts. I have decided to add 32K of RAM to my system but I could add up to 64K, or more specifically 65,536B! This upper limit is due to the 6502 having 16-bit addresses: 2 ^ 16 = 65,536.