+johnnywc Posted May 4, 2004 Share Posted May 4, 2004 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, Quote Link to comment Share on other sites More sharing options...
DEBRO Posted May 4, 2004 Share Posted May 4, 2004 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. Quote Link to comment Share on other sites More sharing options...
Happy_Dude Posted May 4, 2004 Share Posted May 4, 2004 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 . Quote Link to comment Share on other sites More sharing options...
+johnnywc Posted May 4, 2004 Author Share Posted May 4, 2004 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!! Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted May 4, 2004 Share Posted May 4, 2004 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! Quote Link to comment Share on other sites More sharing options...
Bruce Tomlin Posted May 5, 2004 Share Posted May 5, 2004 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. Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted May 5, 2004 Share Posted May 5, 2004 ISTR also that the 6502 likes to copy bit 6 into the V flag in some operations' date=' but I could be wrong.[/quote'] 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 Quote Link to comment Share on other sites More sharing options...
Bruce Tomlin Posted May 5, 2004 Share Posted May 5, 2004 I said SOME operations. As in AND and OR. In add/subtract operations it is indeed a normal overflow flag. Quote Link to comment Share on other sites More sharing options...
Cybergoth Posted May 5, 2004 Share Posted May 5, 2004 Hi there! I said SOME operations. As in AND and OR. In add/subtract operations it is indeed a normal overflow flag. Neither AND nor ORA influence the V Flag. Greetings, Manuel Quote Link to comment Share on other sites More sharing options...
Tom Posted May 5, 2004 Share Posted May 5, 2004 BIT is the only logical instruction that affects the V flag: It copies bit 6 of the operand to the V flag. Ok, depends on wether one sees BIT as a logical op or not. I do. Quote Link to comment Share on other sites More sharing options...
+johnnywc Posted May 5, 2004 Author Share Posted May 5, 2004 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. Quote Link to comment Share on other sites More sharing options...
Tom Posted May 5, 2004 Share Posted May 5, 2004 http://www.6502.org in particular, have a look at http://www.6502.org/tutorials Quote Link to comment Share on other sites More sharing options...
+johnnywc Posted May 5, 2004 Author Share Posted May 5, 2004 Thanks Tom - great resource!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.