Page 1 of 1
a Primary Mathematics question
Posted: Thu Oct 30, 2014 7:35 pm
by Boolean
I have a question about "nes-test-roms\full_palette\full_palette.s".
Code: Select all
; Delay 4739 cycles, well into frame
ldx #4
ldy #176
: dey
bne :-
dex
bne :-
The author says
4739 cycles delayed. What I calculated is as following:
; Delay 4739 cycles, well into frame
ldx #4
ldy #176
: dey
bne :-
dex
bne :-
result =
2 + 2 +
(176 x 5 - 1) + 3 x (255 x 5 - 1) +
(4 x 5 - 1)
=
4724
Why my answer is wrong?
Re: a Primary Mathematics question
Posted: Thu Oct 30, 2014 7:56 pm
by tokumaru
I didn't do the math, but I believe your problem is in the 255, which should be 256.
If you're counting 176 iterations for a counter loaded with 176, why are you counting only 255 iterations for a counter loaded with 0? In this case, 0 works the same as 256.
Re: a Primary Mathematics question
Posted: Thu Oct 30, 2014 10:51 pm
by koitsu
tokumaru is correct -- the formula should be 2 + 2 + (176*5-1) + 3 * (256*5-1) + (4*5-1) which equals 4739.
Re: a Primary Mathematics question
Posted: Thu Oct 30, 2014 11:34 pm
by Boolean
Thank you. You two are right.
At the same time, I find
it is good to use a familiar programming language to emulate 6502 codes.
Here comes my solution.
Re: a Primary Mathematics question
Posted: Fri Oct 31, 2014 3:03 am
by nIghtorius
Why not
Code: Select all
/*
; Delay 4739 cycles, well into frame
ldx #4
ldy #176
: dey
bne :-
dex
bne :-
*/
#include <iostream>
using namespace std;
int main () {
int Cycles = 0;
int X = 4, Y = 176;
Cycles += 4;
while (X != 0) {
while (Y != 0) {
// dey
Y--;
Y &= 0xFF;
Cycles += 2;
// bne :-
if (Y != 0) Cycles++;
}
// dex
X--;
X &= 0xFF;
Cycles += 2;
if (X != 0) Cycles++;
// bne :-
}
cout << "Cycles is " << Cycles << endl;
return 0;
}
??
I find goto's in C++ weird.
Re: a Primary Mathematics question
Posted: Mon Nov 03, 2014 6:30 pm
by Boolean
nIghtorius wrote:
I find goto's in C++ weird.
Because mine is closer to original 6502 code and is more easier to understand.
Yours is more reasonable in real life, but it's farther from assembly languages.
The keyword "goto" is certainly not recommended, but it is useful in some cases.