This is mostly for fun/sharing. I have come up with this method of calculating the nametable address, curious as to other's methods or any criticism:
nametableaddress = y * 32 + x + $2000
Observation:
Shift y value left one bit (x2) and the value in the high nibble should be added to the high(ppu address).
The value in the low nibble should be added to the low(ppu address), but to the high nibble (x16)
; start calculate nametable address
tya ; reg x has nametable x coord, reg y has nametable y coord
asl ; mult by 2....now:
; high nibble holds amount to add to (high byte, low nibble) ppu address,
; low nibble to (low byte, high nibble) ppu address
asl
asl
asl
asl ; shift low nibble to high (x16), in effect x32 with first shift
stx nametableaddress ; don't have to do anything special with x coord
clc
adc nametableaddress
sta nametableaddress ; ppu address low byte is done
tya ; restore a with y coord
lsr ; only three shifts since we restored a from before the first asl command
lsr
lsr
ora #$20 ; make it the first nametable - (code is hardcoded for 1st nametable)
sta nametableaddress+1
; done nametable address
nametableaddress = y * 32 + x + $2000
Observation:
Shift y value left one bit (x2) and the value in the high nibble should be added to the high(ppu address).
The value in the low nibble should be added to the low(ppu address), but to the high nibble (x16)
Code:
; start calculate nametable address
tya ; reg x has nametable x coord, reg y has nametable y coord
asl ; mult by 2....now:
; high nibble holds amount to add to (high byte, low nibble) ppu address,
; low nibble to (low byte, high nibble) ppu address
asl
asl
asl
asl ; shift low nibble to high (x16), in effect x32 with first shift
stx nametableaddress ; don't have to do anything special with x coord
clc
adc nametableaddress
sta nametableaddress ; ppu address low byte is done
tya ; restore a with y coord
lsr ; only three shifts since we restored a from before the first asl command
lsr
lsr
ora #$20 ; make it the first nametable - (code is hardcoded for 1st nametable)
sta nametableaddress+1
; done nametable address