Simulating decimal mode

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

Moderator: Moderators

User avatar
TmEE
Posts: 753
Joined: Wed Feb 13, 2008 9:10 am
Location: Estonia, Rapla city (50 and 60Hz compatible :P)
Contact:

Re: Simulating decimal mode

Post by TmEE » Mon Feb 04, 2013 1:10 am

In my case summing scores and stuff in binary and then converting to decimal once a frame for showing takes a lot less time than trying to calculate stuff in BCD so in the ond you could show it in an easier manner...

User avatar
Omegamatrix
Posts: 35
Joined: Tue Jun 10, 2014 8:15 pm
Location: Canada

Re: Simulating decimal mode

Post by Omegamatrix » Tue Jun 10, 2014 9:07 pm

Bregalad wrote:True, my binary -> decimal routines takes a total of 23 bytes, and I don't think it's possible to shrink it further (it could be simplified if 0-9 digits were 0-9 in the pattern table instead of $d0-$d9 but I don't want to do that).

Code: Select all

	ldy #$00
-	cmp #10
	bcc +
	sbc #10
	iny				;Y count tens
	bne -
+	ora #$d0

	sta StringBuffer+1.w,X	;The units is written 1 tile forwad
	tya
	ora #$d0				;Convert into tile #
	sta StringBuffer.w,X	;Write the tens
	rts
Hello Bregalad,

I was looking at your code, and I believe you can reduce it further like this:

Code: Select all

    ldy    #$D0-1
    sec
.loopDivTen:
    iny
    sbc    #10
    bcs    .loopDivTen
    adc    #$DA
    sta    StringBuffer+1,X
    sty    StringBuffer,X
    rts

I am new to the NES, but no stranger to 6502 assembly. I've programmed a lot of 2600 code... not too sure if there is anyone else on these boards from AtariAge, but they may recognize my username from there. :)

I have always been interested in math routines. I have wrote a lot of unsigned integer division routines, and lately have become more interested in hex to decimal routines. All of these routines are posted in my blog here:

http://atariage.com/forums/blog/563-omegamatrixs-blog/

Early in this thread it was mentioned limiting each byte to 0-63 Hex, and then convert each byte as need be. This seems like a reasonable approach to me. Is this what most NES programmers do? It would keep the addition pretty simple. In my blog I have tackled a hex to decimal (0-65535) conversion, and while my routine does the job in 156-168 cycles, it still requires 263 bytes.

User avatar
Kasumi
Posts: 1292
Joined: Wed Apr 02, 2008 2:09 pm

Re: Simulating decimal mode

Post by Kasumi » Tue Jun 10, 2014 9:32 pm

Oh! Hey, Omegamatrix. I do recognize the name from this thread: http://atariage.com/forums/topic/113254 ... ven/page-5

Off topic: I'd planned to ask a few people eventually, but I've generally operated under the assumption routines posted in threads like that are public domain. Is this true for yours? I'm currently using your divide by 5/10 routine. More on topic, how about this decimal routine that probably beats whatever I'm using now? Was gonna PM you on atariage to ask how/if you'd like credit, but now you're here.

User avatar
Omegamatrix
Posts: 35
Joined: Tue Jun 10, 2014 8:15 pm
Location: Canada

Re: Simulating decimal mode

Post by Omegamatrix » Tue Jun 10, 2014 9:54 pm

Kasumi wrote:Oh! Hey, Omegamatrix. I do recognize the name from this thread: http://atariage.com/forums/topic/113254 ... ven/page-5

Off topic: I'd planned to ask a few people eventually, but I've generally operated under the assumption routines posted in threads like that are public domain. Is this true for yours? I'm currently using your divide by 5/10 routine. More on topic, how about this decimal routine that probably beats whatever I'm using now? Was gonna PM you on atariage to ask how/if you'd like credit, but now you're here.
Hello Kasumi,

All of the math routines I post are public domain. I want people to use them. :) Credit is always nice if you re-post them later.


If you are using the divide by 5 and divide by 10 routines, then be sure to check my blog for the latest and greatest as I didn't post all of the updated routines in the thread you linked to.


By the way, here is the divide by 5:

Code: Select all

  sta  temp
  lsr
  adc  #13
  adc  temp
  ror
  lsr
  lsr
  adc  temp
  ror
  adc  temp
  ror
  lsr
  lsr
If you are doing a smaller range of 0-129, then you can omit the ADC #13 to save 2 cycles and two bytes. Likewise you would add one more LSR to the end of the routine to have a limited (0-129 start value) unsigned integer divide by 10.

User avatar
Omegamatrix
Posts: 35
Joined: Tue Jun 10, 2014 8:15 pm
Location: Canada

Re: Simulating decimal mode

Post by Omegamatrix » Wed Jun 11, 2014 11:12 pm

I just realized that you are storing Absolute,X and not Zeropage,X.

In this case a TYA has to be added back in since there is no STY Absolute,X instruction. It now uses 18 bytes which is still not too bad.

Code: Select all

    ldy    #$D0-1
    sec
.loopDivTen:
    iny
    sbc    #10
    bcs    .loopDivTen
    adc    #$DA
    sta    StringBuffer+1,X
    tya
    sta    StringBuffer,X
    rts

Post Reply