Skip to content

Commit

Permalink
Merge pull request #74 from MathiasElgaard/master
Browse files Browse the repository at this point in the history
Compiler and decompiler bug fixes
  • Loading branch information
tge-was-taken authored Apr 16, 2024
2 parents 17239e9 + 09c0fcf commit 5aafe25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ private void WriteParameters( List<Parameter> parameters )
// Integer literal
private void WriteIntegerLiteral( IntLiteral intLiteral )
{
if ( IsPowerOfTwo( intLiteral.Value ) && intLiteral.Value >= 16 )
if ( IsPowerOfTwo( intLiteral.Value ) && ( intLiteral.Value & 0xF ) == 0 )
{
WriteHexIntegerLiteral( intLiteral.Value );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ namespace AtlusScriptLibrary.MessageScriptLanguage.IO
public static class RelocationTableEncoding
{
private const byte ADDRESS_SIZE = sizeof( int );
private const byte SEQ_LOOP = 0xF8;
private const byte SEQ_BASE = 0x07;
private const byte SEQ_BASE_NUM_LOOP = 2;
private const byte SEQ_FLAG_ODD = 1 << 3;
private const byte SEQ_MAX_NUM_LOOP = 33;

public static int[] Decode( byte[] relocationTable, int addressBaseOffset )
{
Expand All @@ -24,18 +25,11 @@ public static int[] Decode( byte[] relocationTable, int addressBaseOffset )
// Check if the value indicates a sequence run of addresses
if ( ( reloc & SEQ_BASE ) == SEQ_BASE )
{
// Get the base loop multiplier
int baseLoopMult = ( reloc & 0xF0 ) >> 4;
// Get the encoded loop number
int loop = ( reloc & SEQ_LOOP ) >> 3;

// Get the number of loops, base loop number is 2
int numLoop = SEQ_BASE_NUM_LOOP + ( baseLoopMult * SEQ_BASE_NUM_LOOP );

// Check if the number of loops is odd
if ( ( reloc & SEQ_FLAG_ODD ) == SEQ_FLAG_ODD )
{
// If so then add an extra loop cycle.
numLoop += 1;
}
int numLoop = SEQ_BASE_NUM_LOOP + loop;

for ( int j = 0; j < numLoop; j++ )
{
Expand Down Expand Up @@ -95,21 +89,28 @@ public static byte[] Encode( IList<int> addressLocations, int addressBaseOffset
// Subtract one because the first entry is used to locate to the start of the sequence
int numberOfAddressesInSequence = sequences[seqIdx].SequenceAddressCount - 1;

int baseLoopMult = ( numberOfAddressesInSequence - SEQ_BASE_NUM_LOOP ) / SEQ_BASE_NUM_LOOP;
bool isOdd = ( numberOfAddressesInSequence % 2 ) == 1;
// Loop until we have added the full sequence
while ( numberOfAddressesInSequence != 0 )
{
int numberOfAddressesToAdd = numberOfAddressesInSequence;
if ( numberOfAddressesToAdd > SEQ_MAX_NUM_LOOP )
{
numberOfAddressesToAdd = SEQ_MAX_NUM_LOOP;
}

// Get the loop number to encode, base loop number is 2
int loop = numberOfAddressesToAdd - SEQ_BASE_NUM_LOOP;

reloc = SEQ_BASE;
reloc |= baseLoopMult << 4;
reloc = ( loop << 3 ) | SEQ_BASE;

if ( isOdd )
{
reloc |= SEQ_FLAG_ODD;
}
relocationTable.Add( (byte)reloc );

relocationTable.Add( ( byte )reloc );
addressLocationIndex += numberOfAddressesToAdd;
prevRelocSum += numberOfAddressesToAdd * ADDRESS_SIZE;

addressLocationIndex += numberOfAddressesInSequence;
prevRelocSum += numberOfAddressesInSequence * ADDRESS_SIZE;
// Decrease the number of addresses remaining
numberOfAddressesInSequence -= numberOfAddressesToAdd;
}
}
}

Expand Down

0 comments on commit 5aafe25

Please sign in to comment.