A 15-year-old boy told his headteacher “I’m not right in the head” after fatally stabbing a fellow pupil in the heart, a jury has been told.