<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://switchbrew.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rodrigo</id>
	<title>Nintendo Switch Brew - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://switchbrew.org/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Rodrigo"/>
	<link rel="alternate" type="text/html" href="https://switchbrew.org/wiki/Special:Contributions/Rodrigo"/>
	<updated>2026-04-19T13:39:39Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=Sockets_services&amp;diff=9686</id>
		<title>Sockets services</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=Sockets_services&amp;diff=9686"/>
		<updated>2020-07-03T05:26:57Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: /* Initalize */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= bsd:u, bsd:s =&lt;br /&gt;
This is &amp;quot;nn::socket::sf::IClient&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
All the services commands but the first two return -1 on failure and set errno when that happens. Although Nintendo has the FreeBSD kernel&#039;s to socket stack, &#039;&#039;&#039;the errno macro definitions being in use actually come from Linux (and not from FreeBSD as one would expect!)&#039;&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
These services have max_sessions 0x40. There&#039;s 22 IPC handler threads for bsd:u, and 11 for bsd:s.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 0  || RegisterClient ([[#Initialize]])&lt;br /&gt;
|-&lt;br /&gt;
| 1  || StartMonitoring&lt;br /&gt;
|-&lt;br /&gt;
| 2  || [[#Socket]]&lt;br /&gt;
|-&lt;br /&gt;
| 3  || [[#Socket|#SocketExempt]]&lt;br /&gt;
|-&lt;br /&gt;
| 4  || [[#Open]]&lt;br /&gt;
|-&lt;br /&gt;
| 5  || Select&lt;br /&gt;
|-&lt;br /&gt;
| 6  || Poll&lt;br /&gt;
|-&lt;br /&gt;
| 7  || [[#Sysctl]]&lt;br /&gt;
|-&lt;br /&gt;
| 8  || Recv&lt;br /&gt;
|-&lt;br /&gt;
| 9  || RecvFrom&lt;br /&gt;
|-&lt;br /&gt;
| 10 || Send&lt;br /&gt;
|-&lt;br /&gt;
| 11 || SendTo&lt;br /&gt;
|-&lt;br /&gt;
| 12 || Accept&lt;br /&gt;
|-&lt;br /&gt;
| 13 || Bind&lt;br /&gt;
|-&lt;br /&gt;
| 14 || Connect&lt;br /&gt;
|-&lt;br /&gt;
| 15 || GetPeerName&lt;br /&gt;
|-&lt;br /&gt;
| 16 || GetSockName&lt;br /&gt;
|-&lt;br /&gt;
| 17 || GetSockOpt&lt;br /&gt;
|-&lt;br /&gt;
| 18 || Listen&lt;br /&gt;
|-&lt;br /&gt;
| 19 || [[#Ioctl]]&lt;br /&gt;
|-&lt;br /&gt;
| 20 || [[#Fcntl]]&lt;br /&gt;
|-&lt;br /&gt;
| 21 || SetSockOpt&lt;br /&gt;
|-&lt;br /&gt;
| 22 || Shutdown&lt;br /&gt;
|-&lt;br /&gt;
| 23 || ShutdownAllSockets&lt;br /&gt;
|-&lt;br /&gt;
| 24 || Write&lt;br /&gt;
|-&lt;br /&gt;
| 25 || Read&lt;br /&gt;
|-&lt;br /&gt;
| 26 || Close&lt;br /&gt;
|-&lt;br /&gt;
| 27 || [[#DuplicateSocket]]&lt;br /&gt;
|-&lt;br /&gt;
| 28 || [[#GetResourceStatistics]]&lt;br /&gt;
|-&lt;br /&gt;
| 29 || [3.0.0+] [[#RecvMMsg]]&lt;br /&gt;
|-&lt;br /&gt;
| 30 || [3.0.0+] [[#SendMMsg]]&lt;br /&gt;
|-&lt;br /&gt;
| 31 || [7.0.0+] EventFd&lt;br /&gt;
|-&lt;br /&gt;
| 32 || [7.0.0+] [[#RegisterResourceStatisticsName]]&lt;br /&gt;
|-&lt;br /&gt;
| 33 || [10.0.0+] [[#Initialize2]]&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Initialize ==&lt;br /&gt;
Takes a [[#BsdBufferConfig]] (made-up name), the PID, the size of the transfer memory and a copy-handle of the latter.&lt;br /&gt;
&lt;br /&gt;
=== BsdBufferConfig ===&lt;br /&gt;
 /// Configuration structure for bsdInitalize&lt;br /&gt;
 typedef struct  {&lt;br /&gt;
     u32 version;                ///&amp;lt; Observed 1 on 2.0 LibAppletWeb, 2 on 3.0.&lt;br /&gt;
 &lt;br /&gt;
     u32 tcp_tx_buf_size;        ///&amp;lt; Size of the TCP transfer (send) buffer (initial or fixed).&lt;br /&gt;
     u32 tcp_rx_buf_size;        ///&amp;lt; Size of the TCP recieve buffer (initial or fixed).&lt;br /&gt;
     u32 tcp_tx_buf_max_size;    ///&amp;lt; Maximum size of the TCP transfer (send) buffer. If it is 0, the size of the buffer is fixed to its initial value.&lt;br /&gt;
     u32 tcp_rx_buf_max_size;    ///&amp;lt; Maximum size of the TCP receive buffer. If it is 0, the size of the buffer is fixed to its initial value.&lt;br /&gt;
 &lt;br /&gt;
     u32 udp_tx_buf_size;        ///&amp;lt; Size of the UDP transfer (send) buffer (typically 0x2400 bytes).&lt;br /&gt;
     u32 udp_rx_buf_size;        ///&amp;lt; Size of the UDP receive buffer (typically 0xA500 bytes).&lt;br /&gt;
 &lt;br /&gt;
     u32 sb_efficiency;          ///&amp;lt; Number of buffers for each socket (standard values range from 1 to 8).&lt;br /&gt;
 } BsdBufferConfig;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The transfer memory must be larger than a the computed size below. Should the transfer memory be smaller than that, the BSD sockets service would only send ZeroWindow packets (for TCP), resulting in a transfer rate not exceeding 1 byte/s.&lt;br /&gt;
&lt;br /&gt;
 static size_t _bsdGetTransferMemSizeForBufferConfig(const BsdBufferConfig *config)&lt;br /&gt;
 {&lt;br /&gt;
     u32 tcp_tx_buf_max_size = config-&amp;gt;tcp_tx_buf_max_size != 0 ? config-&amp;gt;tcp_tx_buf_max_size : config-&amp;gt;tcp_tx_buf_size;&lt;br /&gt;
     u32 tcp_rx_buf_max_size = config-&amp;gt;tcp_rx_buf_max_size != 0 ? config-&amp;gt;tcp_rx_buf_max_size : config-&amp;gt;tcp_rx_buf_size;&lt;br /&gt;
     u32 sum = tcp_tx_buf_max_size + tcp_rx_buf_max_size + config-&amp;gt;udp_tx_buf_size + config-&amp;gt;udp_rx_buf_size;&lt;br /&gt;
 &lt;br /&gt;
     sum = ((sum + 0xFFF) &amp;gt;&amp;gt; 12) &amp;lt;&amp;lt; 12; // page round-up&lt;br /&gt;
     return (size_t)(config-&amp;gt;sb_efficiency * sum);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== Socket ==&lt;br /&gt;
FreeBSD&#039;s &amp;lt;code&amp;gt;socket&amp;lt;/code&amp;gt; command.&lt;br /&gt;
&amp;lt;code&amp;gt;bsd:u&amp;lt;/code&amp;gt; disallows the usage of the &amp;lt;code&amp;gt;SOCK_SEQPACKET&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SOCK_RAW&amp;lt;/code&amp;gt; types, with the exception of &amp;lt;code&amp;gt;(AF_INET, SOCK_RAW, IPPROTO_ICMP)&amp;lt;/code&amp;gt; (IPv4 &amp;lt;code&amp;gt;ping&amp;lt;/code&amp;gt;), &amp;lt;code&amp;gt;bsd:s&amp;lt;/code&amp;gt; needs to be used for those.&lt;br /&gt;
&lt;br /&gt;
The only registered domains are &amp;lt;code&amp;gt;AF_INET&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;AF_ROUTE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
SocketExempt: same as socket but the socket is immediately shutdown (disconnected) on creation.&lt;br /&gt;
&lt;br /&gt;
== Open ==&lt;br /&gt;
FreeBSD&#039;s &amp;lt;code&amp;gt;open&amp;lt;/code&amp;gt; command, limited to opening &amp;lt;code&amp;gt;/dev/bpf&amp;lt;/code&amp;gt;. This can be used, for example, to enable promiscuous mode, see FreeBSD&#039;s &amp;lt;code&amp;gt;/dev/bpf&amp;lt;/code&amp;gt; for more details.&lt;br /&gt;
&lt;br /&gt;
== Sysctl ==&lt;br /&gt;
FreeBSD&#039;s &amp;lt;code&amp;gt;sysctl&amp;lt;/code&amp;gt; command. Privileged operations are reserved for &amp;lt;code&amp;gt;bsd:s&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;code&amp;gt;CTL_NET&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CTL_VM&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CTL_KERN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;CTL_DEBUG&amp;lt;/code&amp;gt; commands are implemented (?).&lt;br /&gt;
&lt;br /&gt;
== Ioctl ==&lt;br /&gt;
FreeBSD&#039;s &amp;lt;code&amp;gt;ioctl&amp;lt;/code&amp;gt; function. The following ioctls are whitelisted, refer to FreeBSD&#039;s headers for more details: SIOCATMARK, BIOCGBLEN, BIOCSETF BIOCIMMEDIATE, BIOCSETIF, BIOCVERSION, FIONSPACE, FIONWRITE, FIONREAD, SIOCGETSGCNT, SIOCGIFMETRIC, SIOCSIFMETRIC, SIOCDIFADDR, SIOCGIFINDEX, SIOCGIFADDR, SIOCGIFCONF, SIOCGIFNETMASK, SIOCAIFADDR, SIOCGIFMTU, SIOCSIFMTU, SIOCGIFMEDIA, SIOCSIFLLADDR and SIOCGIFXMEDIA.&lt;br /&gt;
&lt;br /&gt;
Nintendo use the following definition (hence changing all ioctls using this structure):&lt;br /&gt;
 struct bpf_program {&lt;br /&gt;
 	u_int bf_len;&lt;br /&gt;
 	struct bpf_insn bf_insns[BPF_MAXINSNS]; // [512]. This is a pointer in the official structure&lt;br /&gt;
 };&lt;br /&gt;
&lt;br /&gt;
== Fcntl ==&lt;br /&gt;
FreeBSD&#039;s &amp;lt;code&amp;gt;fcntl&amp;lt;/code&amp;gt;, limited to &amp;lt;code&amp;gt;F_GETFL&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;F_SETFL&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;O_NONBLOCK&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== DuplicateSocket ==&lt;br /&gt;
Takes a socket file descriptor and an unused u64. Duplicates the socket (FreeBSD&#039;s &amp;lt;code&amp;gt;dup&amp;lt;/code&amp;gt;). Reserved to &amp;lt;code&amp;gt;bsd:s&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== GetResourceStatistics ==&lt;br /&gt;
Takes a total of 8-bytes of input, a PID, a type-0x22 output buffer, and returns a total of 8-bytes of output.&lt;br /&gt;
&lt;br /&gt;
[4.0.0+] Now takes an additional 8-bytes of input.&lt;br /&gt;
&lt;br /&gt;
[7.0.0+] Now takes an additional type-0x21 input buffer.&lt;br /&gt;
&lt;br /&gt;
== RecvMMsg ==&lt;br /&gt;
Takes a total of 0x20-bytes of input, a type-0x22 output buffer, and returns a total of 8-bytes of output.&lt;br /&gt;
&lt;br /&gt;
[7.0.0+] The buffer was replaced with a type-0x6 output buffer.&lt;br /&gt;
&lt;br /&gt;
== SendMMsg ==&lt;br /&gt;
Takes a total of 0xC-bytes of input, two type-0x21 input buffers, and returns a total of 8-bytes of output.&lt;br /&gt;
&lt;br /&gt;
[7.0.0+] The buffers were replaced with a type-0x6 output buffer.&lt;br /&gt;
&lt;br /&gt;
== RegisterResourceStatisticsName ==&lt;br /&gt;
With [10.0.0+] this now takes an additional 8-bytes of input.&lt;br /&gt;
&lt;br /&gt;
== Initialize2 ==&lt;br /&gt;
Same input/output as [[#Initalize]] except this doesn&#039;t take an input handle.&lt;br /&gt;
&lt;br /&gt;
The work-buffer (size is still specified with this cmd) is backed by memory in the sysmodule, instead of TransferMemory. The size (after alignment) must be &amp;gt;=0x1000 and &amp;lt;=0x13F000.&lt;br /&gt;
&lt;br /&gt;
sdknso will only use this cmd when two flags in the input config are set: the first one being set indicates that the bsd:s service is used, while the second flag enables using this cmd. An error is thrown if the work-buffer size is &amp;lt;0x1000. Otherwise when these flags aren&#039;t set, [[#Initialize]] is used as usual.&lt;br /&gt;
&lt;br /&gt;
= bsdcfg =&lt;br /&gt;
This is &amp;quot;nn::bsdsocket::cfg::ServerInterface&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 0  || [[#SetIfUp]]&lt;br /&gt;
|-&lt;br /&gt;
| 1  || [[#SetIfUpWithEvent]]&lt;br /&gt;
|-&lt;br /&gt;
| 2  || CancelIf&lt;br /&gt;
|-&lt;br /&gt;
| 3  || SetIfDown&lt;br /&gt;
|-&lt;br /&gt;
| 4  || GetIfState&lt;br /&gt;
|-&lt;br /&gt;
| 5  || DhcpRenew&lt;br /&gt;
|-&lt;br /&gt;
| 6  || AddStaticArpEntry&lt;br /&gt;
|-&lt;br /&gt;
| 7  || RemoveArpEntry&lt;br /&gt;
|-&lt;br /&gt;
| 8  || LookupArpEntry&lt;br /&gt;
|-&lt;br /&gt;
| 9  || LookupArpEntry2&lt;br /&gt;
|-&lt;br /&gt;
| 10 || ClearArpEntries&lt;br /&gt;
|-&lt;br /&gt;
| 11 || ClearArpEntries2&lt;br /&gt;
|-&lt;br /&gt;
| 12 || PrintArpEntries&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== SetIfUp ==&lt;br /&gt;
Takes a total of 0x28-bytes of input and a type-0x5 input buffer, no output.&lt;br /&gt;
&lt;br /&gt;
[3.0.0+] Takes an additional 4-bytes of input.&lt;br /&gt;
&lt;br /&gt;
== SetIfUpWithEvent ==&lt;br /&gt;
Takes a total of 0x28-bytes of input and a type-0x5 input buffer, returns an output handle.&lt;br /&gt;
&lt;br /&gt;
[3.0.0+] Takes an additional 4-bytes of input.&lt;br /&gt;
&lt;br /&gt;
= ethc:c =&lt;br /&gt;
This is &amp;quot;nn::eth::sf::IEthInterface&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 0  || Initialize&lt;br /&gt;
|-&lt;br /&gt;
| 1  || Cancel&lt;br /&gt;
|-&lt;br /&gt;
| 2  || GetResult&lt;br /&gt;
|-&lt;br /&gt;
| 3  || GetMediaList&lt;br /&gt;
|-&lt;br /&gt;
| 4  || SetMediaType&lt;br /&gt;
|-&lt;br /&gt;
| 5  || GetMediaType&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= ethc:i =&lt;br /&gt;
This is &amp;quot;nn::eth::sf::IEthInterfaceGroup&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 0  || GetReadableHandle&lt;br /&gt;
|-&lt;br /&gt;
| 1  || Cancel&lt;br /&gt;
|-&lt;br /&gt;
| 2  || GetResult&lt;br /&gt;
|-&lt;br /&gt;
| 3  || GetInterfaceList&lt;br /&gt;
|-&lt;br /&gt;
| 4  || GetInterfaceCount&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
= sfdnsres =&lt;br /&gt;
This is &amp;quot;nn::socket::resolver::IResolver&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
This service uses &amp;lt;code&amp;gt;bionic/libc/dns&amp;lt;/code&amp;gt; to perform its tasks.&lt;br /&gt;
&lt;br /&gt;
This has max_sessions 8 and 8 IPC handler threads.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 0 || SetDnsAddressesPrivateRequest (stubbed, returns 0x7FE03)&lt;br /&gt;
|-&lt;br /&gt;
| 1 || GetDnsAddressPrivateRequest (stubbed, returns 0x7FE03)&lt;br /&gt;
|-&lt;br /&gt;
| 2 || GetHostByNameRequest&lt;br /&gt;
|-&lt;br /&gt;
| 3 || GetHostByAddrRequest&lt;br /&gt;
|-&lt;br /&gt;
| 4 || GetHostStringErrorRequest&lt;br /&gt;
|-&lt;br /&gt;
| 5 || GetGaiStringErrorRequest&lt;br /&gt;
|-&lt;br /&gt;
| 6 || [[#GetAddrInfoRequest]]&lt;br /&gt;
|-&lt;br /&gt;
| 7 || GetNameInfoRequest&lt;br /&gt;
|-&lt;br /&gt;
| 8 || GetCancelHandleRequest&lt;br /&gt;
|-&lt;br /&gt;
| 9 || CancelRequest&lt;br /&gt;
|-&lt;br /&gt;
| 10 || [5.0.0+] GetHostByNameRequestWithOptions&lt;br /&gt;
|-&lt;br /&gt;
| 11 || [5.0.0+] GetHostByAddrRequestWithOptions&lt;br /&gt;
|-&lt;br /&gt;
| 12 || [5.0.0+] GetAddrInfoRequestWithOptions&lt;br /&gt;
|-&lt;br /&gt;
| 13 || [5.0.0+] GetNameInfoRequestWithOptions&lt;br /&gt;
|-&lt;br /&gt;
| 14 || [5.0.0+] ResolverSetOptionRequest&lt;br /&gt;
|-&lt;br /&gt;
| 15 || [5.0.0+] ResolverGetOptionRequest&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== GetAddrInfoRequest ==&lt;br /&gt;
Takes three type 5 buffers (host, port, and hints), and a type 6 buffer (the output addrinfos). Also takes a u8 (padded to 4 bytes so the next raw parameter can align), a u32, and a u64. The u8 is a boolean for whether to enable &amp;quot;nsd resolve&amp;quot; (1) or not (0). Not sure what the u32 is. It seems to either come from a parameter to &amp;lt;tt&amp;gt;GetAddrInfo&amp;lt;/tt&amp;gt; or be zero. The u64 is most likely a placeholder for the server to copy the PID into and should be zero. Both hints and the output buffer contain serialized addrinfo chains. The hints buffer is sized 0x400 bytes long by default, and the output buffer 0x1000 bytes.&lt;br /&gt;
&lt;br /&gt;
=== Addrinfo Serialization Format ===&lt;br /&gt;
Each struct addrinfo in the linked list is serialized according to this format and then written to the buffer. All numbers are in network byte order.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Size (bytes) || Name || Notes&lt;br /&gt;
|-&lt;br /&gt;
| 4 || Magic || Needs to be 0xBEEFCAFE. Seriously.&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_flags ||&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_family ||&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_socktype ||&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_protocol ||&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_addrlen ||&lt;br /&gt;
|-&lt;br /&gt;
| ai_addrlen ? ai_addrlen : 4 || ai_addr ||&lt;br /&gt;
|-&lt;br /&gt;
| null-terminated string || ai_canonname ||&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;ai_addrlen&amp;lt;/tt&amp;gt; is zero, &amp;lt;tt&amp;gt;ai_addr&amp;lt;/tt&amp;gt; will occupy 4 bytes.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Size (bytes) || Name || Notes&lt;br /&gt;
|-&lt;br /&gt;
| 4 || ai_addr&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If &amp;lt;tt&amp;gt;ai_family&amp;lt;/tt&amp;gt; is recognized as AF_INET6 (28) or AF_INET (2), &amp;lt;tt&amp;gt;ai_addr&amp;lt;/tt&amp;gt; is read as &amp;lt;tt&amp;gt;struct sockaddr_in&amp;lt;/tt&amp;gt; or &amp;lt;tt&amp;gt;struct sockaddr_in6&amp;lt;/tt&amp;gt;. Otherwise, it&#039;s just read as &amp;lt;tt&amp;gt;u8[ai_addrlen]&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The list should be terminated with a sentinel four-byte zero value.&lt;br /&gt;
&lt;br /&gt;
= nsd:u, nsd:a =&lt;br /&gt;
This is &amp;quot;nn::nsd::detail::IManager&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; border=&amp;quot;1&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Cmd || Name&lt;br /&gt;
|-&lt;br /&gt;
| 10 || GetSettingName&lt;br /&gt;
|-&lt;br /&gt;
| 11 || [[#GetEnvironmentIdentifier]]&lt;br /&gt;
|-&lt;br /&gt;
| 12 || GetDeviceId&lt;br /&gt;
|-&lt;br /&gt;
| 13 || DeleteSettings&lt;br /&gt;
|-&lt;br /&gt;
| 14 || ImportSettings&lt;br /&gt;
|-&lt;br /&gt;
| 15 || [4.0.0+] SetChangeEnvironmentIdentifierDisabled&lt;br /&gt;
|-&lt;br /&gt;
| 20 || Resolve&lt;br /&gt;
|-&lt;br /&gt;
| 21 || ResolveEx&lt;br /&gt;
|-&lt;br /&gt;
| 30 || GetNasServiceSetting&lt;br /&gt;
|-&lt;br /&gt;
| 31 || GetNasServiceSettingEx&lt;br /&gt;
|-&lt;br /&gt;
| 40 || GetNasRequestFqdn&lt;br /&gt;
|-&lt;br /&gt;
| 41 || GetNasRequestFqdnEx&lt;br /&gt;
|-&lt;br /&gt;
| 42 || GetNasApiFqdn&lt;br /&gt;
|-&lt;br /&gt;
| 43 || GetNasApiFqdnEx&lt;br /&gt;
|-&lt;br /&gt;
| 50 || GetCurrentSetting&lt;br /&gt;
|-&lt;br /&gt;
| 51 || [9.0.0+] WriteTestParameter&lt;br /&gt;
|-&lt;br /&gt;
| 52 || [9.0.0+] ReadTestParameter&lt;br /&gt;
|-&lt;br /&gt;
| 60 || [[#ReadSaveDataFromFsForTest]]&lt;br /&gt;
|-&lt;br /&gt;
| 61 || [[#WriteSaveDataToFsForTest]]&lt;br /&gt;
|-&lt;br /&gt;
| 62 || [[#DeleteSaveDataOfFsForTest]]&lt;br /&gt;
|-&lt;br /&gt;
| 63 || [4.0.0+] IsChangeEnvironmentIdentifierDisabled&lt;br /&gt;
|-&lt;br /&gt;
| 64 || [10.0.0+] SetWithoutDomainExchangeFqdns&lt;br /&gt;
|-&lt;br /&gt;
| 100 || [10.0.0+] GetApplicationServerEnvironmentType&lt;br /&gt;
|-&lt;br /&gt;
| 101 || [10.0.0+] SetApplicationServerEnvironmentType&lt;br /&gt;
|-&lt;br /&gt;
| 102 || [10.0.0+] DeleteApplicationServerEnvironmentType&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== GetEnvironmentIdentifier ==&lt;br /&gt;
Takes a type-0x16 buffer with size 8. Returns a string.&lt;br /&gt;
&lt;br /&gt;
The output string is used by [[NIM_services|NIM]] for the &amp;quot;eid:%s&amp;quot; in the User-Agent strings.&lt;br /&gt;
&lt;br /&gt;
This is the &amp;quot;lp1&amp;quot; string also used in domains.&lt;br /&gt;
&lt;br /&gt;
== ReadSaveDataFromFsForTest ==&lt;br /&gt;
Requires the &amp;lt;code&amp;gt;nsd!test_mode&amp;lt;/code&amp;gt; setting to be equal to 1.&lt;br /&gt;
&lt;br /&gt;
Mounts the system save data for bsdsockets as &amp;lt;code&amp;gt;nsdsave&amp;lt;/code&amp;gt; and reads from &amp;lt;code&amp;gt;nsd:/file&amp;lt;/code&amp;gt; to the specified buffer, at the specified size and offset with no checks whatsoever. &amp;lt;code&amp;gt;nsdsave&amp;lt;/code&amp;gt; is then unmounted.&lt;br /&gt;
&lt;br /&gt;
== WriteSaveDataToFsForTest ==&lt;br /&gt;
Requires the &amp;lt;code&amp;gt;nsd!test_mode&amp;lt;/code&amp;gt; setting to be equal to 1.&lt;br /&gt;
&lt;br /&gt;
Mounts the system save data for bsdsockets as &amp;lt;code&amp;gt;nsdsave&amp;lt;/code&amp;gt; and writes to &amp;lt;code&amp;gt;nsd:/file&amp;lt;/code&amp;gt; (appending is allowed) using the specified buffer, at the specified size and offset, with no checks whatsoever. &amp;lt;code&amp;gt;nsdsave&amp;lt;/code&amp;gt; is then commited and unmounted.&lt;br /&gt;
&lt;br /&gt;
== DeleteSaveDataOfFsForTest ==&lt;br /&gt;
Requires the &amp;lt;code&amp;gt;nsd!test_mode&amp;lt;/code&amp;gt; setting to be equal to 1.&lt;br /&gt;
&lt;br /&gt;
Deletes the system save data for bsdsockets.&lt;br /&gt;
&lt;br /&gt;
[[Category:Services]]&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6887</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6887"/>
		<updated>2019-05-14T12:46:19Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Replace italics with &amp;quot; for consistency&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 255 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 values are emulated using uint32 instructions extending their domain through condition codes; when an instruction has to read an uint64 value it reads two subsequent registers.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 6 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There is an extra predicate that always evaluates as true (and false when negated), writing here is a no-op unless there are side-effects. It is shown as &amp;quot;PT&amp;quot; in &#039;&#039;nvdisasm&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6886</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6886"/>
		<updated>2019-05-14T12:33:36Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Fix predicates count and remove &amp;quot;always false&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 255 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 values are emulated using uint32 instructions extending their domain through condition codes; when an instruction has to read an uint64 value it reads two subsequent registers.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 6 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There is an extra predicate that always evaluates as true (and false when negated), writing here is a no-op unless there are side-effects. It is shown as &#039;&#039;PT&#039;&#039; in &#039;&#039;nvdisasm&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6883</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6883"/>
		<updated>2019-05-14T12:17:17Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Fix up registers count&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 255 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 values are emulated using uint32 instructions extending their domain through condition codes; when an instruction has to read an uint64 value it reads two subsequent registers.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 5 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There are 2 more predicates which evaluate as always true and always false.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6881</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6881"/>
		<updated>2019-05-14T12:01:28Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Rephrase uint64&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 254 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 values are emulated using uint32 instructions extending their domain through condition codes; when an instruction has to read an uint64 value it reads two subsequent registers.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 5 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There are 2 more predicates which evaluate as always true and always false.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6879</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6879"/>
		<updated>2019-05-14T11:57:24Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Fix up grammar&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 254 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 instructions are read two subsequent registers but operate on individual uint32 values extending their domain through the carry flag.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 5 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There are 2 more predicates which evaluate as always true and always false.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
	<entry>
		<id>https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6876</id>
		<title>GPU Shaders</title>
		<link rel="alternate" type="text/html" href="https://switchbrew.org/w/index.php?title=GPU_Shaders&amp;diff=6876"/>
		<updated>2019-05-14T06:51:51Z</updated>

		<summary type="html">&lt;p&gt;Rodrigo: Created page with &amp;quot;= Overview = Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs hav...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Overview =&lt;br /&gt;
Like most D3D11-era GPUs the Tegra X1 has 5 pipeline shader stages: Vertex, tessellation control, tessellation evaluation, geometry and fragment. Maxwell GPUs have two vertex shader stages: A and B, using both is not mandatory or common. There are also compute shaders or kernels which run on a separate channel.&lt;br /&gt;
&lt;br /&gt;
== Header ==&lt;br /&gt;
Pipeline stage shaders unlike compute kernels have a header documented by Nvidia[https://download.nvidia.com/open-gpu-doc/Shader-Program-Header/1/Shader-Program-Header.html]. It is a 0x50 byte structure that describes shader&#039;s behavior and enabled features that don&#039;t belong to the code itself (e.g. local memory size, whether it writes global memory). Some of these entries are not mandatory and are not emitted by Nvidia&#039;s compiler.&lt;br /&gt;
&lt;br /&gt;
It is located at the beginning of the program address. Program code starts right after it (at address 0x50, address 0 on compute kernels).&lt;br /&gt;
&lt;br /&gt;
== Text ==&lt;br /&gt;
Every 4 instructions starting from the first one there&#039;s a scheduler instruction. All instructions are 64 bits long, including scheduler instructions.&lt;br /&gt;
&lt;br /&gt;
Shaders emitted by Nvidia&#039;s compiler include a branching instruction pointing to itself after the final unconditional exit. The intention of this &amp;quot;sign&amp;quot; is unknown.&lt;br /&gt;
&lt;br /&gt;
== Registers ==&lt;br /&gt;
Maxwell GPUs have 254 type-less general purpose registers and one special register with id 255, &#039;&#039;nvdisasm&#039;&#039; shows it as RZ and &#039;&#039;envydis&#039;&#039; as 0x0. Writing here is a no-op unless there are side effects. Reading from RZ is returns zero. The fewer registers a shader uses, the more it can be parallelized.&lt;br /&gt;
&lt;br /&gt;
General purpose registers or GPRs are 32 bits long and are the same for all operations, these are given meaning on the instructions. Half float instructions are SIMD and operate on 16 bit pairs, meanwhile double instructions take two registers to operate. uint64 instructions are read two subsequent registers but operate on individual uint32 values extending their domain through the carry flag.&lt;br /&gt;
&lt;br /&gt;
It is a common technique to read or write subsequent registers with a single instruction. For example TEXS (used to sample a texture) reads the texture coordinates from Ra and Ra+1, although it gets more complex with other layouts like 3D textures (it&#039;s not Ra+2). The result of the sample is given in an Rd and their subsequent registers.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Predicates ==&lt;br /&gt;
There are 5 general purpose &amp;quot;1-bit&amp;quot; predicates. These can be used to conditionally execute a given instructions. There are 2 more predicates which evaluate as always true and always false.&lt;br /&gt;
&lt;br /&gt;
Most of the time predicates can be negated.&lt;br /&gt;
&lt;br /&gt;
TODO Add nvdisasm example&lt;br /&gt;
&lt;br /&gt;
== Condition codes ==&lt;br /&gt;
These mostly match x86 condition codes. They are explicitly set by instructions with a &amp;quot;.CC&amp;quot; flag or by special instructions like R2P.&lt;br /&gt;
&lt;br /&gt;
These can be analyzed with the P2R instruction (which moves predicates or condition codes to a register).&lt;br /&gt;
&lt;br /&gt;
= Analysis Tools =&lt;br /&gt;
&lt;br /&gt;
== Disassembling ==&lt;br /&gt;
To disassemble a Maxwell shader or compute kernel there are two applications:&lt;br /&gt;
* nvdisasm[https://developer.nvidia.com/cuda-toolkit]&lt;br /&gt;
* envydis[https://github.com/envytools/envytools/tree/master/envydis]&lt;br /&gt;
&lt;br /&gt;
=== nvdisasm ===&lt;br /&gt;
Nvidia&#039;s disassembler. Since it&#039;s made by Nvidia it usually knows more instruction variants. It tends to print the intention of the instructions rather than their internal encoding, helping visual inspection. It can show the binary representation with an option.&lt;br /&gt;
&lt;br /&gt;
One of its major problems is that the input file has to be perfectly aligned with a stride of 0x20 bytes or it will otherwise fail. Appending zeroes to the file will make it happy since those are NOP instructions. Invalid instructions will also abort the disassemble.&lt;br /&gt;
&lt;br /&gt;
To disassemble a Maxwell shader or kernel in a file this command can be used:&lt;br /&gt;
&lt;br /&gt;
$ nvdisasm -b SM53 &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information about this program it&#039;s recommended to read its help entry and CUDA Toolkit Documentation[https://docs.nvidia.com/cuda/cuda-binary-utilities/].&lt;br /&gt;
&lt;br /&gt;
=== envydis ===&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; is nouveau&#039;s assembler and disassembler. Unlike Nvidia&#039;s disassembler this one doesn&#039;t care about alignment or invalid instructions, so it is useful to analyze a raw memory dump while searching for a shader or kernel. Its declarative-style C file[https://github.com/envytools/envytools/blob/master/envydis/gm107.c] describes instructions internal encoding.&lt;br /&gt;
&lt;br /&gt;
To disassemble with it:&lt;br /&gt;
&lt;br /&gt;
$ envydis -m gm107 -i &amp;lt;file&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;envydis&#039;&#039; also has an assembler variant: &#039;&#039;envyas&#039;&#039;. To edit existing shaders or write shaders from scratch this is easier than assembling the bits manually.&lt;br /&gt;
&lt;br /&gt;
Since it&#039;s the product of reverse engineering the GPU and &#039;&#039;nvdisasm&#039;&#039; itself, it might contain errors.&lt;br /&gt;
&lt;br /&gt;
== Dumping existing shaders ==&lt;br /&gt;
One method to dump shaders from commercial games and homebrew applications is running it on an emulator.&lt;br /&gt;
&lt;br /&gt;
Ryujinx[https://ryujinx.org/#/] offers in its configuration file an entry to dump the encountered shaders in two directories. &#039;&#039;Code&#039;&#039; contains shader dumps that are compatible with &#039;nvdisasm&#039; and &#039;envydis&#039; as they are. &#039;&#039;Full&#039;&#039; is the same as &#039;&#039;Code&#039;&#039; but it includes the header, it&#039;s intended to be decompiled with &#039;&#039;Ryujinx.ShaderTools&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
yuzu[https://yuzu-emu.org/] can dump shaders using its disk-based shader cache with an external tool. &#039;&#039;maxwell-dump&#039;&#039;[https://gist.github.com/ReinUsesLisp/7ba72d3162e60cab283194fcca3474b2] is a small C application for this, it follows the same convention as Ryujinx for &#039;&#039;Code&#039;&#039; and &#039;&#039;Full&#039;&#039;. To find the shader file dumped by the emulator, right click the game from the Qt front-end and select the option to view the transferable cache file. It&#039;s important to highlight that the file format might change in the future leaving this tool unable to extract the binaries until it&#039;s updated.&lt;br /&gt;
&lt;br /&gt;
= Maxwell Instruction Set Architecture =&lt;br /&gt;
TODO Define instructions&lt;/div&gt;</summary>
		<author><name>Rodrigo</name></author>
	</entry>
</feed>