tokumaru wrote: ↑Wed Jun 10, 2020 5:26 pm
DocWaluigean wrote: ↑Wed Jun 10, 2020 4:52 pmTXY Transfer numbers from Register X into Register Y. ??? I do not see this opcode in the Documnentations.
Yeah, it doesn't exist. You can do this through a temporary variable, though:
Or trough the accumulator, if you're not using it for anything else:
ADC Adds number into Register A. If overflow, Carry Flag turns on and adds 1 more extra into Register A.
The way you're wording this still sounds confusing to me, like the "add one extra" happens right away, which's not the case.
BCS Jump to specific label or Address ONLY IF the Carry Flag is 1 or on. [The question is, how does Carry Flag turn on? Does it turn on if overflow in Register? Or overflow in Address? or overflow in ANYTHING??]
It turns on in various situations (read the documentation to see which instructions affect the carry flag and how), but as far as BCS is concerned, it doesn't matter at all. If C is set, the branch will happen, regardless of what caused C to be set.
BEQ Jumps to specific label or Address ONLY IF the Zero Flag is 1 or on.
BNE Jumps to specific label or Address ONLY IF the Zero Flag is 0 or off.
[Another question is. What type of actions need. If the Zero Flag turns on? or if the Zero Flag IS on while the code reaches this destination?]
Branch instructions test the flags BEFORE jumping, in order to decide whether to jump or not. They don't change the state of the flags though, so if a flag is on BEFORE the jump, it will remain on AFTER the jump.
SBC Subtracts number from Register A. The polar opposite of ADC.
[This one is different. If it goes below the 0 and Carry Flag is set to zero, does it turn on Negative Flag?
It does set the N flag, but this is a bit tricky. For example: $02 - $85 = $7D. You'd think that $85 (decimal 133) is larger than $02 (decimal 2), causing the result to be negative. However, the N flag is supposed to be used in the context of signed numbers, and if you interpret $85 as signed, it actually means decimal -123, instead of 133. As we learned in school, two minus signs become a plus, so 2 - (-123) equals 125, which's a positive number, so the N flag is in fact clear after this operation. This whole unsigned vs. signed thing can be a bit complicated, so I wouldn't worry too much about it at this point. You can code entire games for the NES without ever using signed numbers, so really, don't waste any time focusing on that for now.
FOR ALL THE FLAGS, would the opcode triggers IF the flag turns on/off or WHILE the flag is on/off? I'm guessing that it's an "Is, and while" the flag is on/off, the opcode executes.
Branch instructions simply check the current state of the flags at the moment when they execute. It doesn't matter what happened before or what will happen after, only the current state of the flags matters.
The way you're wording this still sounds confusing to me, like the "add one extra" happens right away, which's not the case.
I mean like, If the Carry Flag is on because of overflow, it will always add 1 extra to the A Register.
C = 0
A = #$50
ADC #$20
=
A = #$70
And this one with Flag
C = 1
A = #$25
ADC #20
=
[EITHER]
A = #$46
[OR/AND]
$#### = #$46
---
"It turns on in various situations (read the documentation to see which instructions affect the carry flag and how), but as far as BCS is concerned, it doesn't matter at all. If C is set, the branch will happen, regardless of what caused C to be set."
Oooh, so it's a WHILE C = 0/1 stuff.
---
Branch instructions test the flags BEFORE jumping, in order to decide whether to jump or not. They don't change the state of the flags though, so if a flag is on BEFORE the jump, it will remain on AFTER the jum
p.
So it's a WHILE mode, not IF mode. And the flag stays on/off like that.
---
It does set the N flag, but this is a bit tricky. For example: $02 - $85 = $7D. You'd think that $85 (decimal 133) is larger than $02 (decimal 2), causing the result to be negative. However, the N flag is supposed to be used in the context of signed numbers, and if you interpret $85 as signed, it actually means decimal -123, instead of 133. As we learned in school, two minus signs become a plus, so 2 - (-123) equals 125, which's a positive number, so the N flag is in fact clear after this operation. This whole unsigned vs. signed thing can be a bit complicated, so I wouldn't worry too much about it at this point. You can code entire games for the NES without ever using signed numbers, so really, don't waste any time focusing on that for now.
That's some what good to know not to worry about it. But I want to learn EVERYTHING and missing out details like this could mean lost opportunity to do something so amazing, and loosing opportunity to teach NES stuff that even Kindergarten would understand so greatly about N Flag...
Branch instructions simply check the current state of the flags at the moment when they execute. It doesn't matter what happened before or what will happen after, only the current state of the flags matters.
I understand, and it's a WHILE mode. "While the Flag is On..." "While the Flag is Off.." stuff. Alongside, I'm guessing the Flag on/off is permanent!
================================================================================================================
Programs do execute from top to bottom, yes, but I don't know what you mean by "calculates the target address".
About what you said about this?
"we don't have to concern ourselves with how the CPU calculates the target address."
Even though what you wrote is in fact what's happening, it looks wrong to say that CMP is comparing A to X, because that's not really what CMP does. To avoid this kind of confusion, you can simply change the way you're writing the comment, so that the comment explains and entire block of instructions instead of each individual instruction:
CODE: SELECT ALL
;compare A to X
stx temp
cmp temp
Then it doesn't look wrong anymore, since that's in fact what the 2 instructions TOGETHER are doing. Most programmers don't comment every single line of code anyway... comments explaining the overall purpose of groups of instructions are much more useful, and you can still use line by line comments if you feel like specific steps in the sequence need clarification. Don't waste time writing comments like CLC ;clear the carry flag though... this is obvious from the documentation, there's no point in repeating that over and over in your own program.
This one seems challenging.
I'm going to write the CMP part.
BNE Label - If the Zero Flag is set to Zero, then jump to Label.
Correct, only the wording is weird. since "set" is synonymous with "1", the expression "set to zero" can sound a bit confusing. I'd avoid writing it like that.
It's beautiful to know that if I'm right about this, I'm finally learning something. Also, it's a curse of my vocabulary. I can't explain well or beautifully or stuff like that.
Sometimes "set" could mean a lot of words like "Setting up the PC" this and "Ready, Set, Go!" that.
=================================================================================================================
Anyway, writing this after I saw those new comments:
So I'm guessing CMP is one of the
MOST trickiest opcodes out there?
The cmp instruction sets up the flags such that the following instructions (which replace b?? above) do the following:
For bcc (branch if carry clear): Jump if the value at address firstVar is less than the value at secondVar as an unsigned number
For bcs (branch if carry set): Jump if the value at address firstVar is greater than or equal to the value at secondVar as an unsigned number
For bne (branch if not equal): Jump if the value at address firstVar is not equal to the value at secondVar
For beq (branch if not equal): Jump if the value at address firstVar is not equal to the value at secondVar
For bpl (branch if plus): Jump if the value at address firstVar is greater than or equal to the value at secondVar as a signed number
For bmi (branch if minus): Jump if the value at address firstVar is less than the value at secondVar as a signed number
So if CMP is used, all 6 of these opcodes we know temporary transformed into these kind for one code after CMP???????
Re-reading your words, CMP is can only be used by A-Register FIRST before X/Y
/S?