8x16 and whatever else unreg wants to know
Moderator: Moderators
You have to pay attention not to push more bytes than you have allocated for the stack, but that's basically it. Most people just use the whole page ($0100-$01FF) for it, and filling 256 bytes with temporary data isn't easy, so you're probably safe.
The only events that use the stack non-explicitly are interrupts and jumps to subroutines. When you JSR to a subroutine, the current PC (the program counter, an internal register that points to the instruction being executed) is backed up to the stack, so that you can return to that point of the program with RTS. Interrupts also save the PC, as well as the processor status, so that the CPU can resume its work later (after an RTI), like the interrupt never happened.
Those things don't get in the way of using the stack normally (PHA, PLA, PHP and PLP), since even if an interrupt happens in the middle of you using the stack, whatever is put there will be removed when the returning from the interrupt, so the stack will be exactly like it was before the interrupt.
It's also pretty safe to manipulate the middle of the stack using the X register, since only the top of it will be modified in case of interrupts.
What can cause problems is an interrupt firing when you are directly manipulating the stack pointer (through TSX and TXS)... If S is not pointing to the top of the stack, parts of it might get corrupted. You probably want to avoid this kind of stack manipulation.
What you want to do (push 4 values for using later) is perfectly safe, you just have to remember to remove everything you put on the stack. I for example have made the mistake of putting some values in the stack, and then some logic decisions made the program not need those values anymore, and I just forgot to take them off the stack. Over time, the forgotten values accumulated and caused a stack overflow and the program crashed.
The only events that use the stack non-explicitly are interrupts and jumps to subroutines. When you JSR to a subroutine, the current PC (the program counter, an internal register that points to the instruction being executed) is backed up to the stack, so that you can return to that point of the program with RTS. Interrupts also save the PC, as well as the processor status, so that the CPU can resume its work later (after an RTI), like the interrupt never happened.
Those things don't get in the way of using the stack normally (PHA, PLA, PHP and PLP), since even if an interrupt happens in the middle of you using the stack, whatever is put there will be removed when the returning from the interrupt, so the stack will be exactly like it was before the interrupt.
It's also pretty safe to manipulate the middle of the stack using the X register, since only the top of it will be modified in case of interrupts.
What can cause problems is an interrupt firing when you are directly manipulating the stack pointer (through TSX and TXS)... If S is not pointing to the top of the stack, parts of it might get corrupted. You probably want to avoid this kind of stack manipulation.
What you want to do (push 4 values for using later) is perfectly safe, you just have to remember to remove everything you put on the stack. I for example have made the mistake of putting some values in the stack, and then some logic decisions made the program not need those values anymore, and I just forgot to take them off the stack. Over time, the forgotten values accumulated and caused a stack overflow and the program crashed.
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
tokumaru, thank you so much!!! Your stack explanation here brought back the feeling that you could make a great author! I would be excited to buy your NES assembly-guide-book-type-thing. And it would be nice to share with others.
Kasumi, I owe you a response... and it is coming, I promise. Hope you are doing well.
Kasumi, I owe you a response... and it is coming, I promise. Hope you are doing well.
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
What is in the accumulator will stay in the accumulator until power-off unless another instruction changes it. Interrupt handlers are supposed to always save and restore all registers that they use, but any other subroutine might preserve the accumulator, or it might overwrite it. When you make your subroutine, be sure to add a comment stating what registers it overwrites. For example, a controller reading subroutine might have a comment at the top that it "trashes A and X; preserves Y".
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
Could my code here be done in an easier way?
There must be an easier way to transfer a bit to the accumulator... please tell me that.
Code: Select all
asl a ;checks and saves upper left tile
bmi +
lda #$00
jmp ++n
+ lda #$01
++n sta upleft
asl a ;checks and saves upper right tile
bmi +
lda #$00
jmp ++n
+ lda #$01
++n sta upright
asl a ;checks and saves lower left tile
bmi +
lda #$00
jmp ++n
+ lda #$01
++n sta loleft
asl a ;checks and saves lower right tile
bmi +
lda #$00
jmp ++n
+ lda #$01
++n sta loright
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
Now it has come to using oddRow and evenRow... I have to ask this question... someone will have an answer. The first row of tiles of the screen is not the first scanline, but the first eight scanlines and so this does not concern scanlines. ...I think. Should I start creating the screen with oddRow or evenRow? Is that top line up there line 0 - even - OR line 1 - odd?
Last edited by unregistered on Fri Mar 09, 2012 7:55 pm, edited 1 time in total.
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
In my code idea there are two rows each 32 bytes. Then there is the screenArray; 240 bytes. I'm trying to fill both rows with values for collision... then add them both to screenArray... then clear oddRow and evenRow and fill them both again with new values for collision... then add them both to screenArray...etc.tokumaru wrote:... for what purpose would you be using these oddRow and evenRow variables you speak of?
edit: ok, I found the first mention of my plan on page 29. That was in December but I'm still learning and building; getting better.
I'm still confused.
oddRow and evenRow have to be RAM, otherwise you wouldn't be mentioning putting new values in them.
This being true, why do you want to put anything in them only to move them someplace else? Just put the values you will load (inevitably from ROM) directly into ScreenArray. Is there any reason for that first step of putting the values into oddRow and evenRow? What do you plan to do with ScreenArray? You're putting things here to do what with them? Load a value from them to check collision, or store them to the PPU? I've only got part of your plan.
The response I made to the post you linked made the assumption that the row labels were data in ROM. Your plan seems to be different, so help me understand all the steps.
oddRow and evenRow have to be RAM, otherwise you wouldn't be mentioning putting new values in them.
This being true, why do you want to put anything in them only to move them someplace else? Just put the values you will load (inevitably from ROM) directly into ScreenArray. Is there any reason for that first step of putting the values into oddRow and evenRow? What do you plan to do with ScreenArray? You're putting things here to do what with them? Load a value from them to check collision, or store them to the PPU? I've only got part of your plan.
The response I made to the post you linked made the assumption that the row labels were data in ROM. Your plan seems to be different, so help me understand all the steps.
I don't know. Isn't it your data format? You define what's stored, where it's stored, how it's stored, what it's called and what it does.Should I start creating the screen with oddRow or evenRow? Is that top line up there line 0 - even - OR line 1 - odd?
I'm as confused as Kasumi is...
The way you are asking makes it seem like there's only 1 way to do things, and that this way always uses these variables you mentioned. But actually, collision detection can be done in a thousand different ways, and we have given you some ideas to help shape the solution you'll be using in your game.
But since it's your game, everything is ultimately your choice and your design. Particularly, I don't remember everything we talked here, and I don't know exactly what solution you used, so if you want very specific help with your implementation you'll need to give us more details about how you're doing things.
All this data moving seems unnecessary to me as well, there must be a way to decode the maps from ROM into ScreenArray directly. For us to discuss that, you have to tell us how the maps and collision info are stored in the ROM, and what you want ScreenArray to contain.
The way you are asking makes it seem like there's only 1 way to do things, and that this way always uses these variables you mentioned. But actually, collision detection can be done in a thousand different ways, and we have given you some ideas to help shape the solution you'll be using in your game.
But since it's your game, everything is ultimately your choice and your design. Particularly, I don't remember everything we talked here, and I don't know exactly what solution you used, so if you want very specific help with your implementation you'll need to give us more details about how you're doing things.
All this data moving seems unnecessary to me as well, there must be a way to decode the maps from ROM into ScreenArray directly. For us to discuss that, you have to tell us how the maps and collision info are stored in the ROM, and what you want ScreenArray to contain.
-
- Posts: 1318
- Joined: Thu Apr 23, 2009 11:21 pm
- Location: cypress, texas
Kasumi wrote:why do you want to put anything in them only to move them someplace else? Just put the values you will load (inevitably from ROM) directly into ScreenArray. Is there any reason for that first step of putting the values into oddRow and evenRow?
I thought loading two rows of stuff would be hard, but still possible for me to do. And I thought that once I completed that it would be easy to make a loop to run it again. Now, I see your point! It would be much faster if I wrote to ScreenArray directly.
I plan to use ScreenArray to check collision and to set the PPU. I'm going to try writing the values directly into ScreenArray.Kasumi wrote:What do you plan to do with ScreenArray? You're putting things here to do what with them? Load a value from them to check collision, or store them to the PPU? I've only got part of your plan.
Yes, ok, then I need to create my way... just one of the thousand.tokumaru wrote:The way you are asking makes it seem like there's only 1 way to do things, and that this way always uses these variables you mentioned. But actually, collision detection can be done in a thousand different ways, and we have given you some ideas to help shape the solution you'll be using in your game.
Thanks Kasumi and tokumaru!