5.15 __lat Intrinsic Function

The IAR __lat intrinsic function provides access to the lat instruction, available on AVRxm devices. The lat (Load And Toggle) instruction loads the memory contents at the address held by the Z register into the Rd register specified while simultaneously toggling those bits at that same address held by the Z register that were set in the Rd register.

Suggested Replacement

There is no MPLAB XC8 equivalent built-in function; however, the instruction can be inserted explicitly using in-line assembly code.

Use in-line assembly to directly write an lat instruction.

Caveats

None

Examples

Consider migrating IAR code such as:
unsigned char loadAndToggle(unsigned char regD, unsigned char * Zptr) {
    unsigned char toggled = __lat(regD, Zptr);
    return toggled;
}
to MPLAB XC8 code similar to:
#include <xc.h>

unsigned char lat(unsigned char v, unsigned char * addr) {
    __asm__("movw r30, %[ADDR] \n\t"
            "lat Z, %[Rd] \n\t"
            : [Rd] "+r" (v), "+m" (*addr)
            : [ADDR] "r" ((unsigned int)addr)
            : "r30", "r31");
    return v;
}

unsigned char loadAndClear(unsigned char regD, unsigned char * Zptr) {
    unsigned char toggled = lat(regD, Zptr);
    return toggled;
}

Further Information

See the In-line Assembly section in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on adding in-line assembly.