ugtrain: improve checks, dynval and mem r/w

It is possible to improve the error flow in these functions. For
dynamic value handling it is better to use a switch/case. And for
memory r/w it is always the same error message. So put it into
inline functions and improve the code parts.

Functionality is the same. Just improves readability and cleans
things up.