a Primary Mathematics question

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
Boolean
Posts: 87
Joined: Thu Jan 02, 2014 7:58 am

a Primary Mathematics question

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

Re: a Primary Mathematics question

Post 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.
User avatar
koitsu
Posts: 4203
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: a Primary Mathematics question

Post by koitsu »

tokumaru is correct -- the formula should be 2 + 2 + (176*5-1) + 3 * (256*5-1) + (4*5-1) which equals 4739.
Boolean
Posts: 87
Joined: Thu Jan 02, 2014 7:58 am

Re: a Primary Mathematics question

Post 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.
Attachments
test.cpp
(581 Bytes) Downloaded 123 times
nIghtorius
Posts: 48
Joined: Tue Apr 29, 2014 1:31 pm

Re: a Primary Mathematics question

Post 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.
Boolean
Posts: 87
Joined: Thu Jan 02, 2014 7:58 am

Re: a Primary Mathematics question

Post 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.
Post Reply