I am creating an emulator for the MOS6502 processor, and at the moment I am trying to simulate a stack in code, but I really don’t understand how the stack works in the context of 6502.
One of the features of the 6502 stack’s structure is that when the stack pointer reaches the end of the stack, it will wrap up, but I don’t understand how this function even works.
Say we have a stack of 64 maximum values, if we insert the values x, yand zthe stack, we now have the following structure. With a stack pointer pointing to an address 0x62because this last value was pushed onto the stack.
+-------+
| x | 0x64
+-------+
| y | 0x63
+-------+
| z | 0x62 <-SP
+-------+
| | ...
+-------+
All is well and good. But now, if we push these three values from the stack, we now have an empty stack, the stack pointer points to the value0x64
+-------+
| | 0x64 <-SP
+-------+
| | 0x63
+-------+
| | 0x62
+-------+
| | ...
+-------+
, , 0x00, , 0x00 ? , ????
, , , . popping.
- , .
user9436253