Jump to content
IGNORED

Nybble Swapping Cleverness


Just Jeff

Recommended Posts

Good Morning,

  I need to swap nybbles between two different bytes and have been trying to figure out some clever way to do it.  Everything I try seems awkward.  Does anyone have something slicker?

 So: 

 

These for two bytes
.byte $AB    ;Two sound bytes. Left ($A) cannot play, so it needs to move.
.byte $CD    ;Game data

 

needs to be something like:

.byte $CB
.byte $DA    ;So that B plays, then A plays.  C and D can be any arrangement (re-assembled before use)

 

Here's what I've come up with:

RAM1        ;Swap space, no permanent game data
RAM2        ;Game data

.loop
    lda #$F0    ;2 2 Load a mask
    and RAM2,x  ;4 5 Take the left nybble of the game data
    sta RAM1,x  ;4 9 and store it in the swap space

    lda #$0F    ;2 11 Load a mask
    and (ROM),y ;5+ 16 Take the right nybble of the sound data
    ora RAM1,x  ;4 20 Add it to the left nybble of game data
    sta RAM1,x  ;4 24 This byte is done.  Game data in left nybble, sound in right.

    lda (ROM),y ;5+ 29 Load sound data
    asl         ;2 31 Move D7 into carry
    rol RAM2,x  ;6 37 and then into D0 or game data
    asl         ;2 39 and repeat...
    rol RAM2,x  ;6 45
    asl         ;2 47
    rol RAM2,x  ;6 53
    asl         ;2 55
    rol RAM2,x  ;6 61 Now the right nybble of game data is in left nybble and 
                ;left nybble of sound data is in right.
    dex         ;2 63
    dex         ;2 65
    dey         ;2 67
    bpl .loop   ;2/3 69/70

 

 

Additionally, it doesn't matter if C and D swap positions, or if their bits are reversed. They will be re-assembled before use.

Also, the sound data ($AB) originates in ROM, so it cannot be manipulated until loaded. 
The Game data is only in RAM

 

 

 

 

 

 

Edited by Just Jeff
Added cycles count
Link to comment
Share on other sites

Thanks!

 

It will save 2 cycles with direct replacement.  It does require using a byte of temp RAM but fortunately, that made me realize I could use that elsewhere for another 7 cycles saved.  I could save another two cycles by introducing a 2nd temp byte to replace RAM1,x on the 3rd and 7th lines if I choose.  Any other ideas anyone?

 


RAM1        ;Swap space, no permanent game data
RAM2        ;Game data

.loop
    lda #$F0    ;2 2 Load a mask
    and RAM2,x  ;4 5 Take the left nybble of the game data
    sta RAM1,x  ;4 9 and store it in the swap space

    lda (ROM),y ;5+ 14 Load the right nybble of the sound data
    and #$0F    ;2 16 apply mask
    sta Temp    ;3 19
    ora RAM1,x  ;4 23 Add it to the left nybble of game data
    sta RAM1,x  ;4 27 This byte is done.  Game data in left nybble, sound in right.

    ;lda (ROM),y ;5+ removed
    ;and #$0F    ;2 removed
    ;sta Temp    ;3 removed
    lda RAM2,x  ;4 31
    and #$F0    ;2 33
    ora Temp    ;3 36
    asl         ;2 38
    adc #$80    ;2 40
    rol         ;2 42
    asl         ;2 44
    adc #$80    ;2 46
    rol         ;2 48
    sta RAM2,x  ;4 52
                ;left nybble of sound data is in right.
    dex         ;2 54
    dex         ;2 56
    dey         ;2 58
    bpl .loop   ;2/3 60/61

 

Edited by Just Jeff
Link to comment
Share on other sites

I once made a 256 byte lookup table for flipping byte patterns backwards. It's advantage was speed and needing no RAM, it's disadvantage was the high ROM usage. Not sure if it's applicable to this scenario of nybble patterns, but could be useful if ROM is plentiful. 

Link to comment
Share on other sites

I thought about something like that but I'd have to put it in all the banks where all the data is.  Additionally, I'm not only flipping one byte, There are two bytes involved where they are flipped and switched out with the other byte- so it gets me almost nowhere..  I tried using a 16 byte table to at least flip the right nybble to the left nybble of the other byte but it didn't really help me.

 

Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...