Code for rendering numbers and text into nametable?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
darkhog
Posts: 192
Joined: Tue Jun 28, 2011 2:39 pm

Code for rendering numbers and text into nametable?

Post by darkhog »

This is somewhat of a noob thread so posting here. Does anyone has code for rendering numbers and text into nametable? Numbers for stuff like score and so on (or debug info) and text, because you know, text is important in games.

Assuming obviously font tiles are uppercase only and ordered in a sane way (0-9, A-Z, punctuation)
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: Code for rendering numbers and text into nametable?

Post by pubby »

Well, to display a number you have to know all of the base-10 digits. If your number is in binary, you'd have to do a conversion to get it in base 10. Here's optimized code to do that by omegamatrix: viewtopic.php?p=130363

Alternatively you can store your numbers as one byte per base-10 digit. Instead of bytes being in the range [0, 255], you'd cap them to [0, 9]. This makes arithmetic more difficult, but it makes displaying numbers trivial. Personally, I use this technique most of the time.
text into nametable?
What have you tried? I'm sure you can figure this out :wink:
darkhog
Posts: 192
Joined: Tue Jun 28, 2011 2:39 pm

Re: Code for rendering numbers and text into nametable?

Post by darkhog »

pubby wrote:Alternatively you can store your numbers as one byte per base-10 digit. Instead of bytes being in the range [0, 255], you'd cap them to [0, 9]. This makes arithmetic more difficult
And wastes precious RAM. No, thanks. I know you're trying to help and thanks for that, but this simply won't work for me.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Code for rendering numbers and text into nametable?

Post by tokumaru »

I store things like scores and lives as individual base 10 digits myself. Addition and subtraction are easy (simpler than converting binary to decimal, IMO). It may seem like a waste of precious RAM at first, but realistically speaking, how many displayable numbers do you need? Most games only have a handful of values to display in the status bar.

If you're really hung up on converting binary to decimal though, this has been discussed countless times in the forums, so try searching for these discussions (the most recent one is just a few days old).

As for displaying text, there's really no secret behind it: each symbol you can display is a tile (you can only display symbols you've included in your CHR data, along with the other background elements of your game), so in order to display a letter you just need to write the index of its tile to VRAM, using PPU registers $2006/$2007, like you would for any other background element.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: Code for rendering numbers and text into nametable?

Post by pubby »

darkhog wrote:And wastes precious RAM. No, thanks. I know you're trying to help and thanks for that, but this simply won't work for me.
if you're new enough to be asking how to make text appear on the screen, do you really think you're at the point where memory micro-optimizations matter?

:thinking:
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: Code for rendering numbers and text into nametable?

Post by nesrocks »

There's plenty of RAM. You don't need to use zp for this. Store each digit in one byte like 99.9% of games do.
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Code for rendering numbers and text into nametable?

Post by dougeff »

Some 6502 number systems use 0-99 per RAM address. While slightly less efficient than 0-255, the code involved to translate from RAM to on-screen decimal is fairly easy.

I use 0-9 myself for all numbers that appear on screen. 6 digits for score, 2 for lives left, 2 for some other thing, is really only 10 RAM address used. Not too bad. And it's the easiest.

If your number tiles start at $f0, then you just need to add $f0 to each digit to get its tile value.
nesdoug.com -- blog/tutorial on programming for the NES
darkhog
Posts: 192
Joined: Tue Jun 28, 2011 2:39 pm

Re: Code for rendering numbers and text into nametable?

Post by darkhog »

Okay, you convinced me. How does arithmetic for 0-9 bytes work then? And how much is it slower compared to standard ADC/SBC?

Additionally what's the fastest way to convert from normal bytes to 0-9 format 3 bytes for stuff like debug values that I may want to put on screen?
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: Code for rendering numbers and text into nametable?

Post by dougeff »

Edit. I think I misunderstood your question.

Let's say you are adding 1 to a 2 digit number, max 99

LDX highDigit
LDA lowDigit
clc
adc #1
cmp #10
bcc +
inx ;low digit over 9
LDA #0
+
CPX #10
bcc +
LDA #9 ;max 99 reached
TAX
+
STA lowDigit
STX highDigit


Something like that.

Subtract 1 from a 2 digit number

LDA highDigit ;if both zero, skip
ORA lowDigit
bne +
rts
+
LDX highDigit
LDA lowDigit
Sec
SBC #1
bcs +
dex
lda #9
+
sta lowDigit
stx highDigit
nesdoug.com -- blog/tutorial on programming for the NES
calima
Posts: 1745
Joined: Tue Oct 06, 2015 10:16 am

Re: Code for rendering numbers and text into nametable?

Post by calima »

Perhaps you should consider C at your level.
User avatar
olddb
Posts: 188
Joined: Thu Oct 26, 2017 12:29 pm
Contact:

Re: Code for rendering numbers and text into nametable?

Post by olddb »

Double dabble algorithm

Each decimal digit is stored in a nibble.


https://en.m.wikipedia.org/wiki/Double_dabble
...
Post Reply