I could be wrong but it sounds like your air flow meter is going bad or a wire is loose. I had a friend with a similar problem with his 200sx would not rev past 2500. He changed the air flow meter(lucky he found one at junkyard) and the problem went away.