Skip to content

Commit 9162c5e

Browse files
authored
fix(rp2040): SUBS register N flag handling (#26)
found using gdbdiff
1 parent 6581e2d commit 9162c5e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/instructions.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,19 @@ describe('Cortex-M0+ Instruction Set', () => {
12001200
expect(registers.V).toEqual(false);
12011201
});
12021202

1203+
it('should execute a `subs r3, r3, r2` instruction', async () => {
1204+
await cpu.setPC(0x20000000);
1205+
await cpu.writeUint16(0x20000000, opcodeSUBSreg(r3, r3, r2));
1206+
await cpu.setRegisters({ r2: 8, r3: 0xffffffff });
1207+
await cpu.singleStep();
1208+
const registers = await cpu.readRegisters();
1209+
expect(registers.r3).toEqual(0xfffffff7);
1210+
expect(registers.N).toEqual(true);
1211+
expect(registers.Z).toEqual(false);
1212+
expect(registers.C).toEqual(true);
1213+
expect(registers.V).toEqual(false);
1214+
});
1215+
12031216
it('should raise an SVCALL exception when `svc` instruction runs', async () => {
12041217
const SVCALL_HANDLER = 0x20002000;
12051218
await cpu.setRegisters({ sp: 0x20004000 });

src/rp2040.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ export class RP2040 {
15591559
const rightValue = this.registers[Rm];
15601560
const result = (leftValue - rightValue) | 0;
15611561
this.registers[Rd] = result;
1562-
this.N = leftValue < rightValue;
1562+
this.N = (leftValue | 0) < (rightValue | 0);
15631563
this.Z = leftValue === rightValue;
15641564
this.C = leftValue >= rightValue;
15651565
this.V = (leftValue | 0) < 0 && rightValue > 0 && result > 0;

0 commit comments

Comments
 (0)