The Azure Heights Forum


Post New Topic  New Poll  Post A Reply
my profile | search | faq | forum home
  next oldest topic   next newest topic
» The Azure Heights Forum   » Welcome Center   » Information Booth   » GameBoy Mathmatics

UBBFriend: Email this page to someone!    
Author Topic: GameBoy Mathmatics
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-16-2002 12:07 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
Quoting text from Sharkery Overview
quote:

The Game Boy likes to store values in two digit chunks, which is why 255 is such an important number in Game Boy mathematics.

Please note that it is not a matter of the GameBoy's personal preference that numbers used are usualy less than 255 (FFh). The GameBoy has small chips of memory called registers which are used by the processor to store numbers to process. Registers are used instead of RAM because registers are much faster than RAM. Registers in the GameBoy are 8 bit, which means they cannot hold a number that requires more than 8 bits (or 1 byte, 8 bits=1 byte). In binary, numbers are read from right to left. The number that a binary string stands for depends on the location of each 1. You add the place value of each one together to get the number that the binary string represents. The place value for each binary digit is always double the place value for the previous digit. So the first place value is 1, the second is 2, the third is 4 and so on. With that, you can determine that the decimal number 10 (0x0A) is stored as 00001010. You can see that there is a 1 in the 2nd digit, and a 1 in the 4th digit. You know that the place value of the 2nd digit is 2, and the place value of the 4th digit is 8, and 8+2=10. Since the GameBoy registers can only hold 8 bits, the maximum number that can be stored in a register is 11111111, or 255. (128+64+32+16+8+4+2+1=255). In order to have a larger number, you need to add another byte to it, making it a Word (1 word is equivilent to 2 bytes). For extra large numbers, you can have 4 bytes, or 1 Long. The gameboy can store these large types of numbers in the RAM, but it cannot process them as they are because to process them it needs to first store them in it's registers, and the registers can only hold 8 bits.
To calculate a hit or miss, the GameBoy picks a random number between 1 and 255 inclusively. This means when it picks a random number, it includes the numbers 1 and 255 as possible outcomes. The accuracy number of each move is stored as a number between 1 and 255, 255 being 100%. The game finds the accuracy number of the move used, if the number is less than the random number picked, it's a hit. Take the scenario of a Swift move. Swift is stored in the game as having 100% accuracy, or the accuracy number 255. If the random number chosen is equivilent to 255, the check results in false because 255 is not less than 255, making the move a miss. So actualy Swift is supposed to have 100% accuracy, but actualy only has 254/255 because of an error in the game. (Not 255/256 as stated here and here.)

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
MewtwoSama
Asshole
Member # 12

Member Rated:
posted 05-16-2002 01:10 PM      Profile for MewtwoSama   Author's Homepage   Email MewtwoSama   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
Originally posted by Jason:
To calculate a hit or miss, the GameBoy picks a random number between 1 and 255 inclusively.

I refuse to believe it excludes 0.

I also refuse to believe the world is round, for the bible says otherwise.

- - - - -
Hade ni ikuze!

From: Abyss of Evil | Registered: Feb 2000  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-16-2002 03:16 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
Now that I think about it, the random number probobly does include 0. Which also means that the likelyhood of a hit actualy is 255/256. Somehow it seemed to me when I was writing that post that it would be inconvenient to include 0 as a possible value, as 0 would mean nothing. I mean, if the random number generator fails then the resulting number is always 0. Ok so I was wrong.

Anyway, How sure are you that the Bible is telling you that the world is flat?

quote:
Originally posted by MewtwoSama:
quote:
Originally posted by Jason:
To calculate a hit or miss, the GameBoy picks a random number between 1 and 255 inclusively.

I refuse to believe it excludes 0.

I also refuse to believe the world is round, for the bible says otherwise.



- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Rei the Giant Koffing
Farting Nudist
Member # 1909

Member Rated:
posted 05-17-2002 12:27 AM      Profile for Rei the Giant Koffing   Author's Homepage     Send New Private Message      Edit/Delete Post  Reply With Quote 
Mathematics.
From: Americas b0rked penis | Registered: Jun 2001  |  IP: Logged
Mr. K
Racist
Member # 2

Member Rated:
posted 05-17-2002 03:27 AM      Profile for Mr. K   Author's Homepage   Email Mr. K   Send New Private Message      Edit/Delete Post  Reply With Quote 
Jason: Somehow it seemed to me when I was writing that post that it would be inconvenient to include 0 as a possible value, as 0 would mean nothing.

Or, it could mean "miss".

Ok so I was wrong.

Well, I don't think anything was really proved or disproved by this discussion. To know for sure, you'd have to look at the game code.

I think people have done that already, come to think of it.

Hmmm...I just went and read cfalcon's stuff and he says 0 is basically just like any of the other numbers under 255 (ie, a hit). I'd trust what he says.

[ 05-17-2002, 03:29 AM: Message edited by: Mr. K ]

From: Cinnabar Island | Registered: Feb 2000  |  IP: Logged
Coxy
Hulkamania has run wild over me.
Member # 2297

Member Rated:
posted 05-17-2002 09:09 AM      Profile for Coxy   Email Coxy   Send New Private Message      Edit/Delete Post  Reply With Quote 
That's because a 1 byte register can store 256 values from 0 to 255 inclusive. It's a standard 1 byte short integer data type!

- - - - -
I love nintendolover, but only in a strictly platonic sense.

From: Australia | Registered: Oct 2001  |  IP: Logged
Rolken
Vulcan
Member # 7

Member Rated:
posted 05-17-2002 09:29 AM      Profile for Rolken   Author's Homepage   Email Rolken   Send New Private Message      Edit/Delete Post  Reply With Quote 
I think it's safe to say that most people here (who care) also have a healthy understanding of binary and computers in general; you don't have to baby us along.

- - - - -
[insert sig here]

From: Provo, UT | Registered: Feb 2000  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 10:14 AM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
You people have missed the whole point of that post! The whole hit-miss thing was just a sidenote, a misleading and unthought out one at that! I was trying to say that there is no 'two digit rule' as Azure Heights suggests, but the reson for the numbers being no higher than 0xFF is that most of the GBs registers are incapable of storing any higher number! The exceptions are the PC (Program Counter) and the SP (Stack Pointer). If there is any attempt to bring the number over 0xFF on an 8 bit register the number is wraped around.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Mr. K
Racist
Member # 2

Member Rated:
posted 05-17-2002 12:40 PM      Profile for Mr. K   Author's Homepage   Email Mr. K   Send New Private Message      Edit/Delete Post  Reply With Quote 
Hmmm.

Well, I don't think cfalcon suggests a 2-digit rule of any sort...

Anyway, there is certainly a lot of weirdness with regards to the way certain things are calculated. Your theory works well with the Rest bug, but I'm not sure how it figures into all the strangeness with Stat Mods, which roll over at 1023.

It does seem to be true that direct calculations of numbers higher than 255 are possible. Other than the obvious addition and subtraction of, say, Chansey hit points, there's the Battle Damage Formula, which can include very high Attacks and what not.

I believe most (if not all) of the stored Pokemon data is in numbers less than 256 (to which multipliers are applied later), but since other calculations are possible, it's probably just matter of convenience and storage space.

From: Cinnabar Island | Registered: Feb 2000  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 01:04 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
Numbers that are larger than 255 are stored in register pairs. Two 8 bit registers are put together and used as one 16 bit register. The largest number that can be stored in a 16 bit number is 1023, hence that is where it is rolled over. I didn't mean to imply that numbers larger than 255 can't be used, just that it would be inconvenient for the programmer in many cases. It's kind of hard to make a game like that of any consequence without larger numbers wouldn't you say? By storing a large number in two 8 bit registers (often considered 1 16 bit register, however incorrect that may be...) the processor can calculate large numbers. So where 8 bit registers are denoted by a letter, a register pair is denoted by the two appropriate letters. This is not theory, this is fact.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 01:22 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
The largest number that can be stored in a 16 bit number is 1023
Did I just say that!? What in the world? Too much caffein and too little sleep makes for unclear thoughts! The largest number that can be stored in 16 bits in 65535. The reason it wraps around at 1024 is that 4 8 bit registers are used to store the number, 256*4=1024. Register pairs are used though. lol... As a trainer said to me
quote:

I was a fluke!



- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 01:37 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
The largest number that can be stored in 4 8 bit registers is 255*4=1020. Oh no how many fingers do I have again!? J/k I'm not suited for thinking today. The reason for the 1023 wrapover is that the number is stored in a 12 bit sector of memory, where the last 2 bits must not change from 0, effectively making only 10 bits of use. 512+256+128+64+32+16+8+4+2+1=1023. Ah, that feels better.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
mleo2003
Farting Nudist
Member # 1618

Member Rated:
posted 05-17-2002 02:38 PM      Profile for mleo2003   Author's Homepage     Send New Private Message      Edit/Delete Post  Reply With Quote 
I have been reading the posts above, and found all of this quite interesting. However, some of us forget what system we are talking about: Gameboy. I believe that Gameboy Assembly can not use two bytes to calculate something with a formula. My reason for thinking of this is the Stat Reset in the BDF. The one I am talking about is the one that says "If either the Attacking or Defending Stat, whether Physical or Special, is above 255 (i.e. 256 or up), divide it by 4. This is weird, because doing this doesn't have any effect on the outcome of the damage. If you calculate the Damage this way, then without the Divide by 4 rule, you should come up with the same numbers. Why is this divide by 4 rule there? Simple. I used to work a lot with the evil Fish Device, and have discovered that a Pokemon's stats are changed with 2 codes individually, meaning it is stored as 2 bytes. Like I said earlier, I think 2 byte variables don't work out in the Gameboy hardware. That is why we divide by four. The programmers thought that this would be high enough to prevent the Chopping off effect, which is itself a by product of the 2 byte to 1 byte conversion . Basically, if the game tries to send 2 bytes of info to 1 byte, it will only send the lower byte info, therefore chopping off the results. That is why 1024 resets to 0 (which is risen to 1 to prevent Dividing or Multiplying by 0 at an early stage). 1024 / 4 = 256, which would be 01 00 in Hex. As it can only send the lower half, the damage formula can only use the 00 part. This is why most of the Stat Mods come into play before the 999 cap. The Item Boosters and The moves Light Screen and Reflect apply directly to the variables in the Damage Formula.

Also, the Stat Reset with 1024 doesn't occur in N64 Stadium Games, which furthers my thinking, as the N64 uses larger Storage units than the Gameboy does.

Note: None of this has been proven by me by going into the game code and seeing, these are just my observations and beliefs. Please take all the above with a grain of salt. ^_^

From: Forest, Mississippi | Registered: Mar 2001  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 03:15 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
Originally posted by mleo2003:
I believe that Gameboy Assembly can not use two bytes to calculate something with a formula.

Say what!? The GameBoy processor (which is a subset of the Z80 processor) can and does process Word numbers (2 byte numbers). I know this from personal experience. With some manipulation, you can even calculate floating point numbers on GameBoy.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
mleo2003
Farting Nudist
Member # 1618

Member Rated:
posted 05-17-2002 08:12 PM      Profile for mleo2003   Author's Homepage     Send New Private Message      Edit/Delete Post  Reply With Quote 
Well, then, the only explanation for the Stat Reset with the 4 is the Laziness of the programmers. If Word Calculations are Possible, why didn't they use one in the Game? Well, like mamma always says,"You learn something new everyday."

- - - - -
When life gives you lemons...

Throw them back!!!

From: Forest, Mississippi | Registered: Mar 2001  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-17-2002 08:54 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
Originally posted by mleo2003:
[QB]Well, then, the only explanation for the Stat Reset with the 4 is the Laziness of the programmers. If Word Calculations are Possible, why didn't they use one in the Game?

Possibly. Maybe they were just concerned with available memory, you need less memory to calculate a byte than a Word, obviously.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Rolken
Vulcan
Member # 7

Member Rated:
posted 05-19-2002 08:35 PM      Profile for Rolken   Author's Homepage   Email Rolken   Send New Private Message      Edit/Delete Post  Reply With Quote 
The GameBoy processor (which is a subset of the Z80 processor) can and does process Word numbers (2 byte numbers).

I can assure you that the GB is not a 16 bit system.

Are you aware of how full of yourself you sound?
Are you aware of how old the people you are talking to are?

I'm guessing most of the other posters are likely thinking the same thing, but not stating it out of modesty or apathy or desire to stay on topic. Believe it or not, there are people who know as much as you do, if not more.

- - - - -
[insert sig here]

From: Provo, UT | Registered: Feb 2000  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-19-2002 09:52 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
I know the Gameboy processor is 8 bit. If I am anything, full of myself is not one of them. Did you see I posted 3 times in a row? I could have just died of embarrasment. I'm just trying to reveal the truth, for of it I am ignorant. When I post something, I'm attempting to make use of what I know or have recently learned. I know that the more I talk about it, the more I am forced to think of it, and the better I know it later on for further conversation. When I mess up like I did back there, it only serves to further my understanding of the subject. What may be quite obvious to you may not be so apparent to me. That's why I often state and restate things, even though you may consider it common knowledge. It has yet to be so for me. So if I sound pompous to you, it is because I doubt my own knowledge of what I'm talking about. Anyway, even though the Gameboy processor is 8 bit, somehow it is able to process extreemly large numbers. I just know it can! A GB program called "GB Basic" calculates floating point numbers acurately!

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Coxy
Hulkamania has run wild over me.
Member # 2297

Member Rated:
posted 05-19-2002 11:24 PM      Profile for Coxy   Email Coxy   Send New Private Message      Edit/Delete Post  Reply With Quote 
Floating point? Accurately? Ha ha.

- - - - -
I love nintendolover, but only in a strictly platonic sense.

From: Australia | Registered: Oct 2001  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-20-2002 08:53 AM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
Originally posted by Coxy:
Floating point? Accurately? Ha ha.

That's right. What you don't believe me? The math package from GB Basic can be found at
http://www.devrs.com/gb/files/math.zip
It compiles with TASM, try it out.

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Coxy
Hulkamania has run wild over me.
Member # 2297

Member Rated:
posted 05-21-2002 01:04 AM      Profile for Coxy   Email Coxy   Send New Private Message      Edit/Delete Post  Reply With Quote 
I was laughing sarcastically about floating point numbers being accurate, mainly becuase you missed out a 'relatively' [Wink]

- - - - -
I love nintendolover, but only in a strictly platonic sense.

From: Australia | Registered: Oct 2001  |  IP: Logged
Mr. K
Racist
Member # 2

Member Rated:
posted 05-21-2002 04:05 AM      Profile for Mr. K   Author's Homepage   Email Mr. K   Send New Private Message      Edit/Delete Post  Reply With Quote 
This is semi-interesting in all, but not much is really being said here with regards to how it affects Pokemon.

Basically, it is more difficult, but not impossible to handle large-ish numbers on a GB.

We know the bugs are a result of sloppiness anyway.

So, really, all we're saying here is that there will be bugs of differents sorts, and some of them were more convenient for the programmers than others.

Not much to get all riled up about or take sides over.

From: Cinnabar Island | Registered: Feb 2000  |  IP: Logged
Rolken
Vulcan
Member # 7

Member Rated:
posted 05-21-2002 10:56 PM      Profile for Rolken   Author's Homepage   Email Rolken   Send New Private Message      Edit/Delete Post  Reply With Quote 
It's the principle of it!
From: Provo, UT | Registered: Feb 2000  |  IP: Logged
Blade Sabre
Farting Nudist
Member # 2858

Member Rated:
posted 05-29-2002 08:20 AM      Profile for Blade Sabre     Send New Private Message      Edit/Delete Post  Reply With Quote 
It is easier for the GB to do calculations on one-byte numbers, but it's possible to use larger ones. A Pokémon's EXP is stored in 3 bytes, and new EXP is added to it after each battle.

The BBC B computer is also 8-bit. Using its high-level BASIC language, it can process numbers much larger than 255 with no problems. The BASIC interpreter creates the machine code to deal with the carries. However, using assembly code, special programming techniques are needed to do this.

It's rather difficult for a compiled program to be as stingy on memory as Pokémon is, so it may well have been written slowly and painfully in assembly code. If the GB language is high-level it will have to have special features for fitting the data into as little memory as possible.

- - - - -
runescaped@yahoo.co.uk

From: UK | Registered: May 2002  |  IP: Logged
Blade Sabre
Farting Nudist
Member # 2858

Member Rated:
posted 05-29-2002 08:34 AM      Profile for Blade Sabre     Send New Private Message      Edit/Delete Post  Reply With Quote 
Also, some of the limits in Pokémon are artificially enforced by the programming code, and not by the size of the variables.

A Pokémon's level normally cannot go above 100, even though the variable can hold numbers up to 255. The total EXP behaves similarly.

If some variables stop at 1023, it's because the programmers made it that way. The remaining unused bits are not used for anything else.

- - - - -
runescaped@yahoo.co.uk

From: UK | Registered: May 2002  |  IP: Logged
Atma
Farting Nudist
Member # 689

Member Rated:
posted 05-29-2002 04:54 PM      Profile for Atma   Author's Homepage     Send New Private Message      Edit/Delete Post  Reply With Quote 
Actually, if attack or defense (or, SA or SD for an elemental attack) is over 256, the game divides them both by four. 1023/4 = 255.75, truncates to 255, while 1024/4 = 256 = 0 (at least to the GB). The actual stat registers actually go from 0 to 65535, but (1) boxing would reduce them to normal and (2) they'd get truncated to 999 when the game checks if they're above 999.

- - - - -
"My name is Atma...
I am pure energy... and as ancient as the cosmos.
Forgotten in the river of time...
I've had an eternity to ponder the meaning of things...
And now I have an answer..."

From: Cinnabar Isle, Long Island, NY | Registered: Jul 2000  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-29-2002 10:36 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
Ah, that means that the statistical values are actualy everyday average 16 bit values. That clears up quite a bit of confusion on my part. Thanks Atma!

quote:
Originally posted by Atma:
Actually, if attack or defense (or, SA or SD for an elemental attack) is over 256, the game divides them both by four. 1023/4 = 255.75, truncates to 255, while 1024/4 = 256 = 0 (at least to the GB). The actual stat registers actually go from 0 to 65535, but (1) boxing would reduce them to normal and (2) they'd get truncated to 999 when the game checks if they're above 999.



- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged
Jason
Farting Nudist
Member # 2843

Member Rated:
posted 05-29-2002 10:43 PM      Profile for Jason   Author's Homepage   Email Jason   Send New Private Message      Edit/Delete Post  Reply With Quote 
quote:
Originally posted by Blade Sabre:
If the GB language is high-level it will have to have special features for fitting the data into as little memory as possible.

There are high level language compilers for Gameboy, but they aren't commonly used. If you are writing for something as limited as a Gameboy, you generaly want to watch every cycle. In fact, there is even a BASIC interpreter you can get for Gameboy, and with some adaptations you can plug a keyboard into it and type away at a BASIC program on your Gameboy!

- - - - -
Jason Smestad

From: Williston, ND | Registered: May 2002  |  IP: Logged


All times are ET (US)  
Post New Topic  New Poll  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
Printer-friendly view of this topic
Hop To:


Contact Us | Azure Heights Pokémon Laboratory

© 2000-2016, Maximum Penetration Industries.

Karpe Diem


The views and opinions expressed on this page are strictly those of the author(s). The contents and links have not been reviewed or approved by the University of Miami.