split emulator code roughly into a separate file.