Skip to content

Commit

Permalink
Merge pull request #60 from xyzz/fix-gp
Browse files Browse the repository at this point in the history
PsxLoader: fix calculation of $gp with signed addition
  • Loading branch information
lab313ru authored Jul 29, 2022
2 parents b9ffc67 + 39a52c1 commit f17fea5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/main/java/psx/PsxLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,18 @@ protected void load(ByteProvider provider, LoadSpec loadSpec, List<Option> optio
program.getMemory().getBytes(gpSet, gp1);
program.getMemory().getBytes(gpSet.add(4), gp2);

psxExe.setInitGp(((gp1[1] & 0xFF) << 24) | ((gp1[0] & 0xFF) << 16) | ((gp2[1] & 0xFF) << 8) | ((gp2[0] & 0xFF) << 0));
// Grab top bits from the lui instruction
long gp = ((((long)gp1[1]) & 0xFF) << 24) | ((((long)gp1[0]) & 0xFF) << 16);

// Load and sign-extend the addui operand
long add_op = ((((long)gp2[1]) & 0xFF) << 8) | ((((long)gp2[0]) & 0xFF) << 0);
if (add_op >= 0x8000)
add_op = 0xFFFF0000L | add_op;

// Calculate final gp value, truncating to 32 bits
gp = (gp + add_op) & 0xFFFFFFFFL;

psxExe.setInitGp(gp);
} catch (MemoryAccessException e) {
e.printStackTrace();
log.appendException(e);
Expand Down

0 comments on commit f17fea5

Please sign in to comment.