## Code for rendering numbers and text into nametable?

**Moderator:** Moderators

### Code for rendering numbers and text into nametable?

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)

Assuming obviously font tiles are uppercase only and ordered in a sane way (0-9, A-Z, punctuation)

### Re: Code for rendering numbers and text into nametable?

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.

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.

What have you tried? I'm sure you can figure this outtext into nametable?

### Re: Code for rendering numbers and text into nametable?

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

### Re: Code for rendering numbers and text into nametable?

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.

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.

### Re: Code for rendering numbers and text into nametable?

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?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.

### Re: Code for rendering numbers and text into nametable?

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!

### Re: Code for rendering numbers and text into nametable?

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.

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

### Re: Code for rendering numbers and text into nametable?

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?

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?

### Re: Code for rendering numbers and text into nametable?

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

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

### Re: Code for rendering numbers and text into nametable?

Perhaps you should consider C at your level.

### Re: Code for rendering numbers and text into nametable?

Double dabble algorithm

Each decimal digit is stored in a nibble.

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

Each decimal digit is stored in a nibble.

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

...