WiiRd forum

Wii & Gamecube Hacking => Wii Game hacking help => Topic started by: Bully@Wiiplaza on December 07, 2012, 04:16:34 PM

Title: Writing Nibbles
Post by: Bully@Wiiplaza on December 07, 2012, 04:16:34 PM
I was wondering if or how we could do 4 bit writes.

The codetypes don´t seem to consist anything, but I´m pretty sure there´s some assembly...
what´s the best way to overwrite one digit from a value without touching the others?
Title: Re: Writing Nibbles
Post by: biolizard89 on December 07, 2012, 05:02:59 PM
Quote from: Bully@Wiiplaza on December 07, 2012, 04:16:34 PM
I was wondering if or how we could do 4 bit writes.

The codetypes don´t seem to consist anything, but I´m pretty sure there´s some assembly...
what´s the best way to overwrite one digit from a value without touching the others?
Maybe Gecko Registers with bitmasks?
Title: Re: Writing Nibbles
Post by: Bully@Wiiplaza on December 07, 2012, 08:21:13 PM
How?
If the value is 0xHHHXHHHH and I want to write to X any amount between 0 and F...
"and" and "or" don´t work for all values... it must be reliable at all times :S
Title: Re: Writing Nibbles
Post by: James0x57 on December 07, 2012, 09:25:45 PM
If your Nibble to write is on the "right side" of the byte (_Y):

first OR the byte in a register with 0F,
then AND that with your (Nibble + F0).

[address] = ([address] OR 0F) AND FY




If your Nibble to write is on the "left side" of the byte (Y_):

first OR the byte in a register with F0,
then AND that with your (Nibble + 0F).

[address] = ([address] OR F0) AND YF

Title: Re: Writing Nibbles
Post by: Bully@Wiiplaza on December 08, 2012, 09:05:08 AM
Awesome, that´s what I was looking for. ;D
I almost had your solution though :-[
Title: Re: Writing Nibbles
Post by: dcx2 on March 23, 2013, 08:05:58 PM
You could also use the rlwimi assembly instruction to replace arbitrary contiguous bits.   I think this will work (I always have trouble with rlwi* instructions).  Assuming r3 has the nibble you want in bits 28-31 (i.e. the last four bits, 0x0000000Y) and you're putting it in r4

rlwimi r4, r3, 0, 28, 31

For the other nibble, 0x000000Y0, you'd use

rlwimi r4, r3, 0, 24, 27

This instruction is very versatile.  First, it rotates rS (r3 in this case) by sh (0 in this case).  Then it generates a mask of 1 bits, stretching from mb to me (28 to 31 in the first case).  The rotated rS is then masked by the mask bits and directly inserted into rA.  So for example, let's say r3 had 0xY, but you wanted it to replace r4's 0xY0 bits.

rlwimi r4, r3, 4, 24, 27

If r3 had 0xY0 and you wanted it to replace r4's 0xY (recall that a left shift of 28 is equivalent to a right shift of 4)

rlwimi r4, r3, 28, 28, 31