common code for emulation and device