Jump to content
IGNORED

Skip Draw


Recommended Posts

Hello all,

 

I'm finally starting my 2600 game after 2 years of research and I came across this code on Kirk Israel's Cookbook page:

 

ScanLoop

;skipDraw

; draw player sprite 0:

lda #C_P0_HEIGHT-1 ; 2

dcp P0_Y ; 5 (DEC and CMP)

bcs .doDraw0 ; 2/3

lda #0 ; 2

.byte $2c ;-1 (BIT ABS to skip next 2 bytes)

.doDraw0:

lda (P0_Ptr),y ; 5

sta GRP0 ; 3 = 18 cycles (constant, if drawing or not!)

 

sta WSYNC

 

dey

bne ScanLoop

 

Very interesting to say the least. I believe this is the famous SkipDraw routine made famous by Thomas J. Anyway, I'd like to use it in this game I'm making but I'd like to fully understand what it's doing first.

 

Q: Exactly what does "dcp" do? Does it decrement the value at P0_Y and also do a CMP? In which case is the carry set so that the bcs in the next line evaluates to true?

 

Q: What is the .byte $2c doing? I can see that it's putting inserting the opcode for the BIT instruction... why is it "skipping" the next two bytes? Is this because the assembler will read the next two bytes as the address and perform the BIT operation? I assume that we don't care what happens in this operation, but the Z, N and V flags will be affected. Is this true? Is this a *very* slick way of branching using the least amount of cycles???

 

Man - this stuff is great. I was counting cycles last night in my sleep... :)

 

Thanks for any help,

Link to comment
Share on other sites

Q: Exactly what does "dcp" do? Does it decrement the value at P0_Y and also do a CMP? In which case is the carry set so that the bcs in the next line evaluates to true?

First read the

[stella] skipDraw explained (I hope) (was: bipolar joustpong)

skipdraw from Eric Ball WAS: [stella] Finally underway: 2600 Cookbook threads on [stella]. This should explain skipDraw.

 

Q: What is the .byte $2c doing? I can see that it's putting inserting the opcode for the BIT instruction... why is it "skipping" the next two bytes? Is this because the assembler will read the next two bytes as the address and perform the BIT operation? I assume that we don't care what happens in this operation' date=' but the Z' date=' N and V flags will be affected. Is this true? Is this a *very* slick way of branching using the least amount of cycles???[/quote'']

This actually saves you a byte because you don't have to do beq $$. The negative number used in the cycle count is just to help when you're adding your cycle counts together.

 

You're right that the bit instruction will affect the status flags but in this case it doesn't matter. If you do care about the status flags then you can't use $2C.

 

I hope this helps.

Link to comment
Share on other sites

Man - this stuff is great. I was counting cycles last night in my sleep... :)

Why would you need to count cycles in Sleep ? You just tell it how many cycles to sleep for and ......

Oh I get it. Your at the stage where you still actually goto sleep at night . ;)

Link to comment
Share on other sites

Dennis,

 

Thanks for the info. I'm still a bit confused on how the dcp/bcs combo will get the sprite to draw correctly:

 

ScanLoop

;skipDraw

; draw player sprite 0:

lda #C_P0_HEIGHT-1 ; 2

dcp P0_Y ; 5 (DEC and CMP)

bcs .doDraw0 ; 2/3

lda #0 ; 2

.byte $2c ;-1 (BIT ABS to skip next 2 bytes)

.doDraw0:

lda (P0_Ptr),y ; 5

sta GRP0 ; 3 = 18 cycles (constant, if drawing or not!)

 

sta WSYNC

 

dey

bne ScanLoop

 

When is the carry set on a compare? I know it's doing an A-M, so it would make sense that the N flag is set when M>A, and the Z flag will be set when A=M, but when is C set or reset? It looks like A is initialized with the height of the sprite (minus 1), so I would guess the carry would be set when the value of M is between 0 and the height of the sprite, but why?

 

I hope this makes sense. I'll do some more reading on these instructions to get a better understanding, but this one has me stumped.

 

Thanks for your help!!

 

PS Great job on Climber 5!! I was able to play it at PC5 and was really impressed!!

Link to comment
Share on other sites

You have to understand the flags correctly.

The N flag is set if the result of a subtraction (or a compare) is negative. This is not M>A, because we are comparing two signed 8 bit integers here.

 

E.g. A=10, M=200: A-M = -190 = +66 = $42

 

So M>A is true, but the result is positive (bit7 = 0). N=0!

 

The carry flag works different. It is cleared if the subtraction result is below zero (or the addition result is >255). So in the example above the result that determines the carry flag is -190. C=0!

Link to comment
Share on other sites

An even easier explanation of the N flag is that it is set equal to bit 7 of the result. It doesn't matter whether you consider the original numbers signed or not. If you did, then the N flag would indeed mean that the second number was greater. But really it's just copying the high bit (which is always bit 7 because the 6502 doesn't have 16-bit math instructions like the 6800/6809 do.) ISTR also that the 6502 likes to copy bit 6 into the V flag in some operations, but I could be wrong.

Link to comment
Share on other sites

ISTR also that the 6502 likes to copy bit 6 into the V flag in some operations' date=' but I could be wrong.[/quote']

:idea: The overflow flag is set when an operation with two signed bytes causes an overflow (inversed sign).

 

Examples:

80 + 100 = +180 (but -76 as signed byte: V=1)

-80 - 100 = -180 (but +76 as signed byte: V=1)

but (V=0):

30 + 50 = +80

-30 - 50 = -80

-30 + 50 = +20

50 - 30 = +20

30 - 50 = -20

Link to comment
Share on other sites

Thank you all for the information. Is there a recommended resource that can explain all of this stuff - particularly addition and subtraction, signed numbers, flags affected, etc? Most of the resources I find on the net only give syntax information and what flags are affected, but don't explicitly say how or give good examples.

 

Any help would be greatly appreciated.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...