I am writing simulator to microcontroller msp430. I cant understand when i should set carry bit. For example in add
instruction:
1+0x7FFF
setting carry bit or 1+0xFFFF
?
For the ADD
instruction, the carry bit is set on unsigned overflow.
You can deduce that from the examples in TI documents. In particular, the second example in the documentation of ADD
instruction (page 3-22) says that carry occurs on ADD.B
if the result is greater than 0xff
(and for ADD
and ADDA
the limits are 0xffff
and 0xfffff
respectively - 8, 16 and 20 bits):
ADD.B #10,R5 ; Add 10 to Lowbyte of R5
JC TONI ; Carry occurred, if (R5) ≥ 246 [0Ah+0F6h]
...... ; No carry
The fact that there is a NEGATIVE bit in the msp430 status register in addition to the carry bit confirms this.
There are at least a couple of existing open-source MSP430 emulators, namely mspsim
and Avrora. I suggest to use them as reference implementations.