8x16 and whatever else unreg wants to know

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

unregistered wrote:When you say level's index that could be 1 for level1 and 2 for level2 right?
Yup. Or 0 for level 1, 1 for level 2, and so on... just make sure that everything lines up correctly.
MetatileCollision isnt set up for an address of 2 bytes... :?
In that example, that variable is a pointer. A pointer is a variable that contains the address of something (i.e. it "points" to that thing). Since addresses have 16 bits on the 6502, the variable must be 2 bytes long.
I'm attempting to use this, for the first time, it's kind of confusing and kind of something to learn from. Hope it becomes less of the former and more of the latter; it will. :)
Just keep in mind that pointers are addresses that point to something else, and by manipulating them you can have the same variable point to different places in the ROM. The main advantage of using pointers is that the same code can access different data all across the ROM, you just have to "point" to the data you want to use at any given time. This is how you can make the same code (your game logic) access the data of the first level as well as the data of the second level, and the third, and so on.
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

tokumaru wrote:
MetatileCollision isnt set up for an address of 2 bytes... :?
In that example, that variable is a pointer. A pointer is a variable that contains the address of something (i.e. it "points" to that thing). Since addresses have 16 bits on the 6502, the variable must be 2 bytes long.
So each pointer variable must be 2 bytes long? I'm guessing you have already said yes to this, but I'd like confirmation... please. :)
tokumaru wrote:
I'm attempting to use this, for the first time, it's kind of confusing and kind of something to learn from. Hope it becomes less of the former and more of the latter; it will. :)
Just keep in mind that pointers are addresses that point to something else, and by manipulating them you can have the same variable point to different places in the ROM. The main advantage of using pointers is that the same code can access different data all across the ROM, you just have to "point" to the data you want to use at any given time. This is how you can make the same code (your game logic) access the data of the first level as well as the data of the second level, and the third, and so on.
If I have a variable called rowPointer that's a size of 2 bytes, then do I have to use the indirect index addressing to read rowPointer as a pointer? Is indirect index addressing the only way to use pointers in assembly language?

I remember I could put a dot in front of a pointer's name to use it... maybe... in a language like c++? I just want to say "Go to rowPointer's row." How can I say that in assembly 6502.
User avatar
Kasumi
Posts: 1293
Joined: Wed Apr 02, 2008 2:09 pm

Post by Kasumi »

So each pointer variable must be 2 bytes long? I'm guessing you have already said yes to this, but I'd like confirmation... please. :)
It says exactly this in the quote. He said it must be, and he said why.
If I have a variable called rowPointer that's a size of 2 bytes, then do I have to use the indirect index addressing to read rowPointer as a pointer? Is indirect index addressing the only way to use pointers in assembly language?
Edit: On second thought, It is more or less the only way to read from rowPointer as a pointer. You will want to use indirect indexed for what you're trying now.

For question two: This is not the only way to use pointers in 6502. There's also indexed indirect, but people don't generally use that because it is a very indirect way to get data from an address. As well, you can jmp to a pointer which is just called indirect to my knowledge, and you can do creative things with the stack and RTS. But none of that matters for what you're doing right now.
I remember I could put a dot in front of a pointer's name to use it... maybe... in a language like c++?
6502 assembly isn't "type safe" like C is. What this means is your program/assembler doesn't know or care what you are using your variables for. C will actually fix how operations work on a variable based on what you're using that variable for. For instance, pointer++; could increase the address the pointer is holding by 1, 2 or any other number depending on how large the type of data is that the pointer is supposed to point to. Assembly doesn't care, so it's up to you to keep track.
I just want to say "Go to rowPointer's row." How can I say that in assembly 6502.


Edit 2: After writing this post, I realize I may have misunderstood your question. Assuming rowPointer and rowPointer+1 already contain the low and high bytes of the address you want to point to, it's as easy as

Code: Select all

lda (rowPointer),y
where y contains how many places to add to that address to get the byte you want.

The below examples may still be helpful, though.

Code: Select all

row:
    .db $whatever, $whatever, $whatever; i.e. your data.


    lda #low(row);Syntax for how to get the high and low part of an address may vary based on your assembler
    sta $00;Store in a RAM location. 
    lda #high(row)
    sta $01;Store in a RAM location.

    lda ($00),y;This now gets the same result in A that lda row,y would.
    $00, and $01 are your pointer variables.    

;Note that the variable that holds the LOW BYTE of the pointer's address goes in between the paranthesis.

;Note: To use this addressing mode, the low byte of the address MUST BE stored in a RAM location that is EXACTLY one byte before the RAM location where the high byte is stored. 

;$00 = low
;$01 = high; is fine.

;$01 = low
;$02 = high; is fine.

;F1 = low
;F2 = high; is fine.

;$01 = low
;$00 = high; is NOT fine.
Naturally the above code is pretty useless. You could use lda rowPointer,y instead. So here's something useful.

Code: Select all

Row0:
   .db $00, $00;Your data
Row1:
   .db $FF, $FE; $50;Your data
Row2:
   .db $05;You get the point.
;etc

;Now you keep two separate lists of the high and low byte for this data.

RowLow:
   .db low(Row0);As said before, syntax for this may vary
   .db low(Row1)
   .db low(Row2);etc
RowHigh:
   .db high(Row0)
   .db high(Row1)
   .db high(Row2)

   ldy rownumber

   lda RowLow,y
   sta $00
   lda RowHigh,y
   sta $01

   ldy #$00
   lda ($00),y;Depending on what "rownumber" is you will load from a different address.

;if rownumber was 0:
;You load #$00 which is the first byte of data after the Row0 label.
;if rownumber was 1:
;You load #$FF which is the first byte of data after the Row1 label.
;if rownumber was 2:
;You load #$05 which is the first byte of data after the Row2 label.
That's how you do it. (Provided I didn't make a stupid mistake like saying $2000 instead of $2001.)
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

Are there any rules to be aware of when using the stack? Thought I would push processor status on stack 4 times so that I could use the plp same settings for each of the four times I read a bit from the 4bit metatile part inside my loops.

By "rules to be aware of" I mean is it possible that other things will use the stack? At any or at certain times?
Kasumi wrote:That's how you do it. (Provided I didn't make a stupid mistake like saying $2000 instead of $2001.)
Ah, it's ok to make a mistake. That's what we learn from. :)
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

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.
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

tokumaru, thank you so much!!! :D 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. :mrgreen: 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. :)
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

unregistered wrote:I would be excited to buy your NES assembly-guide-book-type-thing.
Hell no, I'm a terrible teacher! :roll:
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

If I have an accumulator value id like to keep... for a little while; would it be dangerous to leave it in the accumulator for about 10-12 cycles? Is it safe, like the stack is, from interrupts and subroutines? This might be a sub-par question. :oops:
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples »

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".
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

Wow that's really good. :D Thanks tepples!
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

Could my code here be done in an easier way?

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
There must be an easier way to transfer a bit to the accumulator... please tell me that. :)
Shiru
Posts: 1161
Joined: Sat Jan 23, 2010 11:41 pm

Post by Shiru »

rol a:rol a:and #1 ?
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

aaaaaaah!! Thank you Shiru! :D ...AND #1!!!!!!!!!!!!!!!!!!!!!!!!!! YEAY!!
unregistered
Posts: 1318
Joined: Thu Apr 23, 2009 11:21 pm
Location: cypress, texas

Post by unregistered »

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. :oops: 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.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Post by tokumaru »

No matter how many times I read I really can't understand the question... for what purpose would you be using these oddRow and evenRow variables you speak of?
Post Reply