Extremely advanced timing

My corolla is a 1996 automatic 1.6L 4A-FE. The timing is about 40 degrees advanced according to a timing light and the ECU (using one of those OBDII code readers). It starts up, idles fast (around 1500rpm) and sounds terrible. It will drive for about 5 min and then it dies and won't start again till the motor's completely cooled off. It has a distributor, but it's a one bolt type (non-adjustable), so the timing I'm assuming is completely controlled by the ECU. I have no idea what could be causing this nor how to fix it. Have found nothing online or in forums. Has anybody here encountered this kind of problem before?