wokwi / rp2040js

A Raspberry Pi Pico Emulator in JavaScript
MIT License
403 stars 46 forks source link

printf() incorrectly formats numbers (Arduino Mbed Core) #24

Closed urish closed 3 years ago

urish commented 3 years ago

To reproduce, compile the following sketch using the Arduino Mbed Core for RP2040:

void setup() {
  printf("12 is %d\n", 12);
}

void loop() {}

Then run the result in the emulator. This will print to the serial console:

12 is <

Instead of the expected result, 12 is 12.

urish commented 3 years ago

Traced the issue to our LSRS (register) implementation.

Pre-compiled HEX file (of the above) sketch that reproduces the issue: test-printf.ino.hex

Unit test case (passes on the actual silicone, fails in the emulator):

  it('should return zero for `lsrs r2, r3` with 32 bit shift', async () => {
    await cpu.setPC(0x20000000);
    await cpu.writeUint16(0x20000000, opcodeLSRSreg(r2, r3));
    await cpu.setRegisters({ r2: 10, r3: 32 });
    await cpu.singleStep();
    const registers = await cpu.readRegisters();
    expect(registers.r2).toEqual(0);
    expect(registers.Z).toEqual(true);
    expect(registers.C).toEqual(false);
  });