Twisted 2D semiconductors with built-in memory functions