I'll define $f(x)$ to be the output of your function given $x.$
That is,
$$\begin{eqnarray}
f(1) &=& 1, \\
f(2) &=& 2, \\
f(255) &=& 255, \\
f(256) &=& 254, \\
\end{eqnarray}$$
and so forth.
I also assume you meant to have $f(0) = 0$ if $0$ is in the domain of your function.
If you continue stepping down from the output $254$ (given $256$) without skipping
any numbers, and you allow the output to go all the way down to $0,$
then adding $1$ to $x$ reduces $f(x)$ by $1,$
adding $2$ to $x$ reduces $f(x)$ by $2,$
and adding $253$ to $x$ reduces $f(x)$ by $253.$
That is,
$$\begin{eqnarray}
f(257) &=& 253, \\
f(258) &=& 252, \\
f(509) &=& 1, \\
f(510) &=& 0, \\
f(511) &=& 1, \\
f(512) &=& 2, \\
\end{eqnarray}$$
and so forth, since you want to start counting upward again once you reach $0.$
It should be clear from this that the period of your function is exactly $510.$
That is, once you reach the output $0,$ it will take $510$ additional steps (incrementing
the given value by $1$ each time) to reach the next output of $0.$
The following function will do the job:
$$f(x) = |((x + 254) \mod 510) - 254|$$
You can test it with the following PHP code:
<?
for($x = 0; $x <= 1025; $x++) {
echo($x . " -> " . abs((($x + 254) % 510) - 254)) . "\n";
}
?>
Look particularly at the output for inputs near $255,$ near $510,$ near $765,$
and near $1020.$