To be rude and maybe get through to you:
IT'S THE ENGINE!!!
You got broke, INOP, or otherwise parts that are pissing the ECM off. The ECM is doing all it can to save the engine and transmission at this point. The trans is shifting due to lack of engine power and engine not running proper prevents lockup. Damage control, eh?
You want to swap the engine out, fine, DO SO and quit Fing around with this one! No harm in wanting another engine. Otherwise you already have your answer of engine CPS and related wire/sensors on page1. Keep pushing it and it (a bad running engine) will eventually take the transmission out.