My only guess is that with all the damage the ECU has gotten wet. Its not common, but it can happen. Ive had it happen to me a few times at a carwash and once in heavy rain. The car basically shuts down and goes nuts and the throttle quits responding and the guages go nuts. Too much water and its fried for good. If it does it without getting wet, Id say that there is already enough damage that the ECU needs to be replaced. If the engine still runs normally when it freaks out, then it might not be the ECU but some other problem; most likely a short.
And an M3 engine can be fit into a 318 for the cost of 5-10k installed. Generally, the tranny, drive shaft, and diif also need to be replaced becuase the gearing is not appropriate for increase in power.
<span style='color:gray'><span style='font-family:Courier'>1995 e36 3 Series RIP
1999 e46 3 Series BURN IN HELL
2000 MR-S Roadster= Current</span></span>