It was originally constructed as a small fortress in 889 and then largely ignored until its ruins were renovated and rebuilt in the mid-13th century by the Moorish emir Mohammed ben Al-Ahmar of the Emirate of Granada, who built its current palace and walls. It was converted into a royal palace in 1333