you probably have battery acid damage on the wiring itself, the range switch is on the transmission right under the battery tray - the VSS is on the transmission too and reads the counter shaft, check its output - dont rely on speedometer as it takes signal from ABS,
the ECU however takes reading from the gauges (speedometer via bus signal) and the VSS (counter shaft sensor) to determine transmission output speed.
Lets hope no one has monkeyed with the system too much by installing wrong combination of scrapped parts.