It is currently Wed May 22, 2019 10:51 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 10 posts ]
 Print view Previous topic | Next topic
Author Message
 Posted: Tue Oct 16, 2018 11:48 am

Joined: Wed May 19, 2010 6:12 pm
Posts: 2851
I was trying to make a Super Mario World optimization patch (again), and the HEX to DEC conversion code was one of the routines I reprogrammed. Super Mario World uses a 6 digit score values (7 including the lowest digit that is always 0) and the score in binary/hex takes up 3 bytes. The routine I wrote, divided the value by 100 twice, to get out 3 bytes storing 2 decimal digits each, then used a LUT to split the 2 digit byte value into 2 decimal digits.

It made me wonder what kind of fancy math was used in NES games or could be used in an NES game. Every byte in a score counter effects every digit.

Top

 Posted: Tue Oct 16, 2018 12:01 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21392
Location: NE Indiana, USA (NTSC)
Some NES games such as RHDE store large values as binary and convert upon display. There are efficient methods for this, such as these by Omegamatrix, though it's not something you want to do several times every frame.
Some NES games such as Super Mario Bros. store large values with one byte per digit.
Some NES games store large values as BCD, where \$33 means thirty-three, not fifty-one, and implement BCD addition in software. This is easier on Super NES because decimal mode wasn't cut out of the S-CPU.
Some NES games such as Thwaite store large values as base 100, where \$0C \$22 means 1234 because the bytes mean twelve and thirty-four respectively, and convert each byte upon display. This is faster than converting an entire 16- or 24-bit number, as it can be done with a reasonably sized lookup table or even without a table in 80 cycles per byte.

If I were doing something with large floating point values, such as an idle game, I'd probably use base 100.

_________________
Pin Eight | Twitter | GitHub | Patreon

Top

 Posted: Tue Oct 16, 2018 1:29 pm

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 135
Location: Southern California

_________________
http://WilsonMinesCo.com/ lots of 6502 resources

Top

 Posted: Tue Oct 16, 2018 7:18 pm

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21392
Location: NE Indiana, USA (NTSC)
Of the 3 snippets on the first page, 2 will not work in an NES game. Likewise both of the snippets on the second page. Why? Because I sed so.

(Why for real? The sed instruction on a 2A03 does not do what those snippets expect it to do.)

_________________
Pin Eight | Twitter | GitHub | Patreon

Top

 Posted: Tue Oct 16, 2018 11:13 pm

Joined: Tue Feb 07, 2017 2:03 am
Posts: 711
try http://codebase64.org/doku.php?id=base:6502_6510_maths

However are you optimizing for speed or RAM? Not really sure the 65816 has anything to offer over a 6502..

Top

 Posted: Thu Oct 18, 2018 7:06 pm

Joined: Mon Feb 07, 2011 12:46 pm
Posts: 1040
There is also my method (which is used for 16-bit numbers). You need the first twenty patterns in the pattern table to be numbers 0 to 9 twice, and you also need lookup tables. And then, each digit (from right to left) will do like:
Code:
LDA lo_table,X
CMP #10
The ADC of each digit is then may add one due to the CMP from the previous digit.

_________________
.

Top

 Posted: Sat Oct 20, 2018 7:04 am

Joined: Wed Aug 16, 2017 12:15 am
Posts: 50
zzo38 wrote:
Code:
LDA lo_table,X
CMP #10

What's in lo_table, hi_table, X and Y?

Top

 Posted: Sat Oct 20, 2018 10:01 am

Joined: Mon Feb 07, 2011 12:46 pm
Posts: 1040
qalle wrote:
zzo38 wrote:
Code:
LDA lo_table,X
CMP #10

What's in lo_table, hi_table, X and Y?

In X is the low byte, Y is the high byte, lo_table and hi_table are tables of digits. Different tables are used for each digit position. For example the ones digit (rightmost digit) will have lo_table repeating 0 to 9, while hi_table for the ones digit will be repeating 0 6 2 8 4 (since it represents the digits of the index multiplied by 256).

You will have to replace lo_table and hi_table with the correct tables for each digit. Note also the pattern tables must be set up as I specified, in order for this to work.

_________________
.

Top

 Posted: Mon Oct 29, 2018 9:56 am

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 21392
Location: NE Indiana, USA (NTSC)
Oziphantom wrote:
Not really sure the 65816 has anything to offer over a 6502..

True, but for this specific purpose, both the 65816 and an authentic 6502 have something to offer over the slightly cut-down 6502 in the 2A03: the ALU reacts to sed.

_________________
Pin Eight | Twitter | GitHub | Patreon

Top

 Posted: Mon Oct 29, 2018 12:01 pm

Joined: Wed Nov 30, 2016 4:45 pm
Posts: 135
Location: Southern California
tepples wrote:
Oziphantom wrote:
Not really sure the 65816 has anything to offer over a 6502..

True, but for this specific purpose, both the 65816 and an authentic 6502 have something to offer over the slightly cut-down 6502 in the 2A03: the ALU reacts to sed.

I didn't comment on this earlier because I figured it was regarding the specific piece of code. However, my 65816 Forth runs two to three times as fast as my 65c02 Forth at a given clock speed, and the code is more compact. Further, when you're always dealing with 16-bit quantities, I find the '816 to be much easier to program than the '02 is. The '816 can also do things the '02 is either clumsy at or downright incapable of.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 10 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 13 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki