Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Simulide 1.0.0, AVR core incorrectly executes instruction CP

2 posters

Go down  Message [Page 1 of 1]

Serg77m



Hello.

Simulide version 1.0.0, the AVR kernel incorrectly executes the CP instruction, namely, the "S" flag is incorrectly set, which is why the signed comparison does not work correctly. Version 0.4.15-SR10 works correctly. Here is the test (arduino):
Code:
void setup() {
  Serial.begin(115200);
  int8_t a = 64, b = -64;
  Serial.print(a);
  Serial.print(' ');
  Serial.print(b);
  Serial.print(' ');
  asm volatile(
    "  cp %0, %1\n"
    "  brge m1\n"
    "  ldi %0,0\n"
    "m1:\n"
    "  in %1,__SREG__\n"
    : "+r" (a), "+r" (b)
  );
  Serial.print(a);
  Serial.print(' ');
  Serial.println(b, HEX);
}
void loop()
{
}
The sketch performs a signed comparison of "a" and "b", and if "a" is less than "b, then "a" is zeroed. In "b" writes the state of the register SREG.
Simulide 1.0.0 writes: 64 -64 0 FFFFFF9D
Simulide 0.4.15-SR10 writes: 64 -64 64 FFFFFF8D
Real Arduino writes: 64 -64 64 FFFFFF8D

The bug in the file src/mcusim/cores/avr/avrcore.cpp, line 54:
Code:
write_S_Bit( S_S, sn ^ STATUS(S_V) );
This XOR instruction does not work correctly, "sn" contains flag "N" in bit 7, but "STATUS(S_V)" returns state of flag "V" in the bit 3. This can be rewritten, for example, like this:
Code:
write_S_Bit( S_S, static_cast<bool>(sn) ^ static_cast<bool>(STATUS(S_V)) );
But maybe you will find a better solution.



Last edited by arcachofo on Tue Oct 11, 2022 9:09 pm; edited 1 time in total (Reason for editing : Mark as solved (green color))

arcachofo

arcachofo

Thanks, I think it is solved at Rev 1131.

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum