nesdev.comhttps://forums.nesdev.com/ Nametable => Attribute table address conversion?https://forums.nesdev.com/viewtopic.php?f=10&t=12398 Page 1 of 1

 Author: lidnariq [ Tue Feb 17, 2015 5:52 pm ] Post subject: Re: Nametable => Attribute table address conversion? Only thought: for speed, at least, think about restructuring things to avoid the RMW-on-memory instructions; they're enough slower that you may find the overhead of temporarily loading and storing A to be preferable.

 Author: tokumaru [ Tue Feb 17, 2015 7:10 pm ] Post subject: Re: Nametable => Attribute table address conversion? I just don't do this conversion, ever. What I do is convert a set of coordinates (9-bit X, 9-bit Y) into NT and AT addresses, but I really don't see the point in converting one address to another.

 Author: rainwarrior [ Tue Feb 17, 2015 8:33 pm ] Post subject: Re: Nametable => Attribute table address conversion? I don't have 6502 code to convert, but a bitwise conversion is outlined at the skinny scrolling wiki article.Code: tile address      = 0x2000 | (v & 0x0FFF) attribute address = 0x23C0 | (v & 0x0C00) | ((v >> 4) & 0x38) | ((v >> 2) & 0x07)

 Author: rainwarrior [ Tue Feb 17, 2015 8:49 pm ] Post subject: Re: Nametable => Attribute table address conversion? So... this would be my naive approach:Code:; tile address in:; v0 = H7 H6 H5 H4 H3 H2 H1 H0; v1 = L7 L6 L5 L4 L3 L2 L1 L0; attribute address out:; a0 = 0  0  1  0  H3 H2 1  1   (\$23 | calculations); a1 = 1  1  H1 H0 L7 L4 L3 L2  (\$C0 | calculations)lda v0rorrorrorrorrorand #\$30sta a1     ; a1 = 0  0  H1 H0 0  0  0  0lda v1lsrlsrphaand #\$07ora a1sta a1     ; a1 = 0  0  H1 H0 0  L4 L3 L2plalsrlsrand #\$08ora a1     ; A  = 0  0  H1 H0 L7 L4 L3 L2ora #\$C0sta a1     ; a1 = 1  1  H1 H0 L7 L4 L3 L2lda v0and #\$0Cora #\$23sta a0     ; a0 = 0  0  1  0  H3 H2 1  1  61 cycles.

 Author: rainwarrior [ Tue Feb 17, 2015 9:12 pm ] Post subject: Re: Nametable => Attribute table address conversion? Or, yeah, if I just modify the address in-place, I think the following would be 53 cycles.Code:; v0:v1 = address in; t     = temporarylda v0    ; A:v1 = vlsrror v1lsrror v1    ; A:v1 = v >> 2lda v1and #\$07sta t     ; t  = (v >> 2) & \$07lda v1    ; v1 = (v >> 2) & \$FFlsrlsrand #\$38  ; A  = (v >> 4) & \$38ora tora #\$C0sta v1    ; v1 = ((v >> 2) & \$07) | ((v >> 4) & \$38) | #\$C0lda v0and #\$0Cora #\$23sta v0    ; v0 = ((v & \$0C00) | \$2300) >> 8Do you need to do this more than once per frame? What is use case that your original post wasn't efficient enough for?Another alternative: (49 cycles)Code:lda v1rol       ; C = L7lda v0phaand #\$0Cora #\$23sta v0    ; A = 0  0  1  0  H3 H2 1  1  plaror       ; A = L7 .  .  .  .  .  .  H1, C = H0rorror       ; A = H1 H0 L7 .  .  .  .  .rorrorand #\$38sta t     ; t = .  .  H1 H0 L7 .  .  .lda v1lsrlsrand #\$07  ; A = .  .  .  .  .  H4 H3 H2ora tsta v1Pardon me coding out loud. I'm just enjoying the exercise.

 Author: rainwarrior [ Tue Feb 17, 2015 9:51 pm ] Post subject: Re: Nametable => Attribute table address conversion? Oh, sorry, I totally missed that you needed the palette index too. Modified that last routine, which is now 63 cycles.Code:lda v1rolrol       ; A = .  .  .  .  .  .  .  L7, C = L6rol p     ; p = 0  0  0  0  0  P1 P0 L6ror       ; C = L7lda v0phaand #\$0Cora #\$23sta v0    ; A = 0  0  1  0  H3 H2 1  1plaror       ; A = L7 .  .  .  .  .  .  H1, C = H0rorror       ; A = H1 H0 L7 .  .  .  .  .rorrorand #\$38sta t     ; t = .  .  H1 H0 L7 .  .  .lda v1lsr       ; C = L0rol p     ; p = 0  0  0  0  P1 P0 L6 L0lsrand #\$07  ; A = .  .  .  .  .  H4 H3 H2ora tsta v1I think your original was 68 cycles, so this isn't much of an improvement anyway. Sorry.

 Author: rainwarrior [ Tue Feb 17, 2015 10:20 pm ] Post subject: Re: Nametable => Attribute table address conversion? As for your palette lookup conversion, comparing size and speed for what you originally posted:inline version: +12 bytescycles added: -4, 5, 15, 17 (8.25 average case)lookup version: +19 bytes (16 byte table, LDA ABS X)cycles added: 4This is presuming LDA ZP (if inline) vs LDX ZP, LDA ABS X (lookup) when you go to use the thing.If you were using my last version of the code, the inline version is incompatible, I think.However, if your goal is to do this many times per frame, optimizing these routines probably doesn't help much unless you need kinda random access to the tiles. If you're trying to update a contiguous row of tiles, you only need to do the calculation once at the start of the row, and then the thing to optimize is probably the continuation loop afterwards?

 Author: rainwarrior [ Tue Feb 17, 2015 10:25 pm ] Post subject: Re: Nametable => Attribute table address conversion? Oops! Yes, I misread what was going on with the palette bits. A quick revision, no change in speed.Code:lda v1rolphprol p     ; p = 0  0  0  0  0  P1 P0 L7plp       ; C = L7lda v0phaand #\$0Cora #\$23sta v0    ; A = 0  0  1  0  H3 H2 1  1plaror       ; A = L7 .  .  .  .  .  .  H1, C = H0rorror       ; A = H1 H0 L7 .  .  .  .  .rorrorand #\$38sta t     ; t = .  .  H1 H0 L7 .  .  .lda v1lsrlsr       ; C = L1rol p     ; p = 0  0  0  0  P1 P0 L7 L1and #\$07  ; A = .  .  .  .  .  H4 H3 H2ora tsta v1