From my research, and this may have changed a bit since I researched it, here is the basic info I got pertaining to replacing engines.
Terminology (VERY important to keep track of!):
Engine replacement: Replacing a worn out engine with the exact same engine.
Engine change: Replacing an engine with another engine offered from the factory for that vehicle. An example would be replacing a worn out 305 in a GM truck with a 350 or 454.
Engine swap: Replacing an engine with one from a different make or model.
General guidelines are that an engine can be swapped if either of two things are met: The engine has to be from the same year, or newer, vehicle, and retains all emissions equipment from either the vehicle or the replacement engine. OR an older engine can be used, but all emissions equipment from the vehicle must be used. An example of this is if the 305 in a 1985 Chevy truck was replaced with a 1980 350. The 350 needs to have all of the emissions equipment that was on the 305.
Diesels fall into a different category, and as far as I know (last time I checked) it was illegal to swap a gas engine with a diesel. (That is, replacing a gas engine with a diesel in a vehicle that was never offered with a diesel). This came about because so many people were using the old laws (all diesel being exempt from emissions testing) to swap diesels into just about anything. I believe in CA it would be allowed to install a Cummins into a GM truck that was originally offered with a diesel, but it would have to meet any emissions standards of the diesel truck it was going into.
Tale all of this with a grain of salt. Much of my research was sketchy, since nobody was able to give clear answers when I investigated it a few years back, including emissions testing officials. Its pretty hard to follow the law when nobody knows the law, including the people who are supposed to enforce it.
Jim