We will see in Mitzvah #613 the general obligation for a person to write a Torah scroll. The king had an obligation to write one Torah as an individual and a separate obligation to write a second Torah as king. The king had this scroll with him almost all the time. (Way back in Mitzvah #2, we cited Talmud Menachos 43b on how King David was pained when he visited the bath house because he was bereft of mitzvos there. Aside from his tallis and tefillin, David would have been carrying a Torah scroll.)
The reason for this mitzvah is because of the great authority wielded by the king. Nobody tells him what to do, so he's in even greater danger than most people of forgetting that he's not the one who's ultimately in charge. His personal sefer Torah was to serve as a constant reminder that he answers to God. In this way, the king would always keep God in mind when making judgments and decisions.
This mitzvah applies when the Jews have a king. It is discussed in the Talmud in tractate Sanhedrin, page 21b. It is codified in the Mishneh Torah in the third chapter of Hilchos Melachim. This mitzvah and is #17 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.