Skip to content

Commit

Permalink
fix(rp2040): SUBS register N flag handling (#26)
Browse files Browse the repository at this point in the history
found using gdbdiff
  • Loading branch information
Turro75 authored May 14, 2021
1 parent 6581e2d commit 9162c5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/instructions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,19 @@ describe('Cortex-M0+ Instruction Set', () => {
expect(registers.V).toEqual(false);
});

it('should execute a `subs r3, r3, r2` instruction', async () => {
await cpu.setPC(0x20000000);
await cpu.writeUint16(0x20000000, opcodeSUBSreg(r3, r3, r2));
await cpu.setRegisters({ r2: 8, r3: 0xffffffff });
await cpu.singleStep();
const registers = await cpu.readRegisters();
expect(registers.r3).toEqual(0xfffffff7);
expect(registers.N).toEqual(true);
expect(registers.Z).toEqual(false);
expect(registers.C).toEqual(true);
expect(registers.V).toEqual(false);
});

it('should raise an SVCALL exception when `svc` instruction runs', async () => {
const SVCALL_HANDLER = 0x20002000;
await cpu.setRegisters({ sp: 0x20004000 });
Expand Down
2 changes: 1 addition & 1 deletion src/rp2040.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ export class RP2040 {
const rightValue = this.registers[Rm];
const result = (leftValue - rightValue) | 0;
this.registers[Rd] = result;
this.N = leftValue < rightValue;
this.N = (leftValue | 0) < (rightValue | 0);
this.Z = leftValue === rightValue;
this.C = leftValue >= rightValue;
this.V = (leftValue | 0) < 0 && rightValue > 0 && result > 0;
Expand Down

0 comments on commit 9162c5e

Please sign in to comment.