Base 100

From NESdev Wiki
Jump to navigationJump to search

Unlike the regular 6502, the 2A03 does not have decimal mode. One workaround for this is to keep numbers in binary, and use a BCD conversion routine to convert to binary-coded decimal as needed. Base 100 is another workaround that simplifies displaying the numbers onscreen.

In base 100, a number consists of a series of bytes that range from 0-99 (or $00-$63 in hexadecimal). Unlike in regular 8-bit math, numbers wrap around at 100 instead of at 256, so $0063 + $0001 is $0100 instead of $0064. Given a base 100 number, you can use a 100-byte table to convert each byte to BCD, which is easy to display.

base_100_to_bcd:
  .byte $00, $01, $02, $03, $04, $05, $06, $07, $08, $09, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19
  .byte $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39
  .byte $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $50, $51, $52, $53, $54, $55, $56, $57, $58, $59
  .byte $60, $61, $62, $63, $64, $65, $66, $67, $68, $69, $70, $71, $72, $73, $74, $75, $76, $77, $78, $79
  .byte $80, $81, $82, $83, $84, $85, $86, $87, $88, $89, $90, $91, $92, $93, $94, $95, $96, $97, $98, $99

Alternatively for even more speed you can have two tables that separately provide the ones digit and the tens digit of the resulting BCD number, letting you display each byte with something as simple as:

  ldx base_100_number
  lda base_100_tens,x
  sta $2007
  lda base_100_ones,x
  sta $2007

Base 100 is good for numbers that you want to display and do addition and subtraction on, but don't need for more complicated math. It can be good for things like a score, an amount of currency, or a timer or countdown.

Code examples

16-bit increment

This increments a 16-bit base 100 number by 1, while preventing it from going over 9999:

AddOneCoin:
  lda #99
  cmp Money+1
  bne :+
  cmp Money+0
  bne :+
    rts
  :

  inc Money
  lda Money
  cmp #100
  bne :+
    lda #0
    sta Money
    inc Money+1
  :
  rts

16-bit subtraction

Here's an example that demonstrates base 100 subtraction, using a pair of 16-bit numbers:

  lda Money
  sec
  sbc Price
  bpl :+
    ; Carry is clear if the code ends up in here
    adc #100
    clc
  :
  sta Temp
  ; Carry
  lda Money+1
  sbc Price+1
  bmi NotEnoughFunds
  ; Write the new amount
  sta Money+1
  lda Temp
  sta Money