<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>http://wiki.specnext.dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Intrepidis</id>
	<title>SpecNext Wiki - User contributions [en-gb]</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.specnext.dev/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Intrepidis"/>
	<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/Special:Contributions/Intrepidis"/>
	<updated>2026-04-09T07:52:00Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=DMA&amp;diff=11948</id>
		<title>DMA</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=DMA&amp;diff=11948"/>
		<updated>2024-01-06T15:56:25Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Layer2 auto-slowdown was lifted in Core 3.0&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Overview ==&lt;br /&gt;
The ZX Spectrum Next DMA (zxnDMA) is a single channel DMA device that implements a subset of the Z80 DMA functionality. The subset is large enough to be compatible with common uses of the similar Datagear interface available for standard ZX Spectrum computers and compatibles. It also adds a burst mode capability that can deliver audio at programmable sample rates to the DAC device.&lt;br /&gt;
&lt;br /&gt;
== Accessing the zxnDMA ==&lt;br /&gt;
&amp;lt;del&amp;gt;The zxnDMA is mapped to a single Read/Write IO Port 0x6B which is the same one used by the Datagear but unlike the Datagear it doesn&#039;t also map itself to a second port 0x0B similar to the MB-02 interface.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Since core 3.1.2 the zxnDMA is mapped to {{PortNo|$xx6B}}, and Zilog-DMA mode is mapped to {{PortNo|$xx0B}}.&lt;br /&gt;
&lt;br /&gt;
== Description  ==&lt;br /&gt;
The normal Z80 DMA (Z8410) chip is a pipelined device and because of that it has numerous off-by-one idiosyncrasies and requirements on the order that certain commands should be carried out. These issues are not duplicated in the zxnDMA. You can continue to program the zxnDMA as if it is were a Z8410 DMA device but it can also be programmed in a simpler manner.&lt;br /&gt;
&lt;br /&gt;
The single channel of the zxnDMA chip consists of two ports named A and B. Transfers can occur in either direction between ports A and B, each port can describe a target in memory or IO, and each can be configured to autoincrement, autodecrement or stay fixed after a byte is transferred.&lt;br /&gt;
&lt;br /&gt;
A special feature of the zxnDMA can force each byte transfer to take a fixed amount of time so that the zxnDMA can be used to deliver sampled audio.&lt;br /&gt;
&lt;br /&gt;
== Modes of Operation ==&lt;br /&gt;
The zxnDMA can operate in a z80-DMA compatibility mode.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;REMOVED in core 3.1.2:&#039;&#039;&#039; &amp;lt;del&amp;gt;The z80-DMA compatibility mode is selected by setting bit 6 of nextreg 0x06. In this mode, all transfers involve length+1 bytes which is the same behaviour as the z80-DMA chip. In zxn-DMA mode, the transfer length is exactly the number of bytes programmed. This mode is mainly present to accommodate existing spectrum software that uses the z80-DMA and for cp/m programs that may have a z80-DMA option.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Since core 3.1.2:&#039;&#039;&#039; the DMA mode is selected by port number, the {{PortNo|$xx6B}} works in zxnDMA mode, {{PortNo|$xx0B}} works in Zilog mode. The bit 6 in {{NextRegNo|$06}} is not DMA related any more and will be reused for something different.&lt;br /&gt;
&lt;br /&gt;
The zxnDMA can also operate in either burst or continuous modes.&lt;br /&gt;
&lt;br /&gt;
Continuous mode means the DMA chip runs to completion without allowing the CPU to run. When the CPU starts the DMA, the DMA operation will complete before the CPU executes its next instruction.&lt;br /&gt;
&lt;br /&gt;
Burst mode nominally means the DMA lets the CPU run if either port is not ready. This condition can&#039;t happen in the zxnDMA chip except when operated in the special fixed time transfer mode. In this mode, the zxnDMA will let the CPU run while it waits for the fixed time to expire between bytes transferred.&lt;br /&gt;
&lt;br /&gt;
Note that there is no byte transfer mode as in the Z80 DMA.&lt;br /&gt;
&lt;br /&gt;
== Programming the zxnDMA ==&lt;br /&gt;
Like the Z80 DMA chip, the zxnDMA has seven write registers named WR0-WR6 that control the device. Each register WR0-WR6 can have zero or more parameters associated with it.&lt;br /&gt;
&lt;br /&gt;
In a first write to the zxnDMA port, the write value is compared against a bitmask to determine which of the WR0-WR6 is the target. Remaining bits in the written value can contain data as well as a list of associated parameter bits. The parameter bits determine if further writes are expected to deliver parameter values. If there are multiple parameter bits set, the expected order of parameter values written is determined by parameter bit position from right to left (bit 0 through bit 7). Once all parameters are written, the zxnDMA again expects a regular register write selecting WR0-WR6.&lt;br /&gt;
&lt;br /&gt;
The table below describes the registers and the bitmask required to select them on the zxnDMA.&lt;br /&gt;
&lt;br /&gt;
{| &lt;br /&gt;
! Register Group&lt;br /&gt;
! Register Function Description&lt;br /&gt;
! Bitmask&lt;br /&gt;
! Notes&lt;br /&gt;
|- &lt;br /&gt;
| WR0&lt;br /&gt;
| Direction, Operation and Port A configuration&lt;br /&gt;
| &amp;lt;pre&amp;gt;0XXXXXAA&amp;lt;/pre&amp;gt;&lt;br /&gt;
| AA must NOT be 00&lt;br /&gt;
|- &lt;br /&gt;
| WR1&lt;br /&gt;
| Port A configuration&lt;br /&gt;
| &amp;lt;pre&amp;gt;0XXXX100&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| WR2&lt;br /&gt;
| Port B configuration&lt;br /&gt;
| &amp;lt;pre&amp;gt;0XXXX000&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| WR3&lt;br /&gt;
| Activation&lt;br /&gt;
| &amp;lt;pre&amp;gt;1XXXXX00&amp;lt;/pre&amp;gt;&lt;br /&gt;
| It’s best to use WR6&lt;br /&gt;
|- &lt;br /&gt;
| WR4&lt;br /&gt;
| Port B, Timing and Interrupt configuration&lt;br /&gt;
| &amp;lt;pre&amp;gt;1XXXXX01&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| WR5&lt;br /&gt;
| Ready and Stop configuration&lt;br /&gt;
| &amp;lt;pre&amp;gt;10XXX010&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &lt;br /&gt;
|- &lt;br /&gt;
| WR6&lt;br /&gt;
| Command Register&lt;br /&gt;
| &amp;lt;pre&amp;gt;1XXXXX11&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== zxnDMA Registers ==&lt;br /&gt;
These are described below following the same convention used by Zilog for its DMA chip:&lt;br /&gt;
&lt;br /&gt;
=== WR0 – Write Register Group 0 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 0   |   |   |   |   |   |   |&lt;br /&gt;
     |   |   |   |   |   0   0  Do not use&lt;br /&gt;
     |   |   |   |   |   0   1  Transfer (Prefer this for Z80 DMA compatibility)&lt;br /&gt;
     |   |   |   |   |   1   0  Do not use (Behaves like Transfer, Search on Z80 DMA)&lt;br /&gt;
     |   |   |   |   |                       &lt;br /&gt;
     |   |   |   |   |   1   1  Do not use (Behaves like Transfer, Search/Transfer on Z80 DMA)&lt;br /&gt;
     |   |   |   |   |                      &lt;br /&gt;
     |   |   |   |   0 = Port B -&amp;amp;gt; Port A (Byte transfer direction)&lt;br /&gt;
     |   |   |   |   1 = Port A -&amp;amp;gt; Port B&lt;br /&gt;
     |   |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT A STARTING ADDRESS (LOW BYTE)&lt;br /&gt;
     |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT A STARTING ADDRESS (HIGH BYTE)&lt;br /&gt;
     |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  BLOCK LENGTH (LOW BYTE)&lt;br /&gt;
     V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  BLOCK LENGTH (HIGH BYTE)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Several registers are accessible from WR0. The first write to WR0 is to the base register byte. Bits D6:D3 are optionally set to indicate that associated registers in this group will be written next. The order the writes come in are from D3 to D6 (right to left). For example, if bits D6 and D3 are set, the next two writes will be directed to PORT A STARTING ADDRESS LOW followed by BLOCK LENGTH HIGH.&lt;br /&gt;
&lt;br /&gt;
=== WR1 – Write Register Group 1 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 0   |   |   |   |   1   0   0&lt;br /&gt;
     |   |   |   |&lt;br /&gt;
     |   |   |   0 = Port A is memory&lt;br /&gt;
     |   |   |   1 = Port A is IO&lt;br /&gt;
     |   |   |&lt;br /&gt;
     |   0   0 = Port A address decrements&lt;br /&gt;
     |   0   1 = Port A address increments&lt;br /&gt;
     |   1   0 = Port A address is fixed&lt;br /&gt;
     |   1   1 = Port A address is fixed&lt;br /&gt;
     |&lt;br /&gt;
     V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT A VARIABLE TIMING BYTE&lt;br /&gt;
 0   0   0   0   0   0   |   |&lt;br /&gt;
                         0   0 = Cycle Length = 4&lt;br /&gt;
                         0   1 = Cycle Length = 3&lt;br /&gt;
                         1   0 = Cycle Length = 2&lt;br /&gt;
                         1   1 = Do not use&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The cycle length is the number of cycles used in a read or write operation. The first cycle asserts signals and the last cycle releases them. There is no half cycle timing for the control signals.&lt;br /&gt;
&lt;br /&gt;
=== WR2 – Write Register Group 2 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 0   |   |   |   |   0   0   0&lt;br /&gt;
     |   |   |   |&lt;br /&gt;
     |   |   |   0 = Port B is memory&lt;br /&gt;
     |   |   |   1 = Port B is IO&lt;br /&gt;
     |   |   |&lt;br /&gt;
     |   0   0 = Port B address decrements&lt;br /&gt;
     |   0   1 = Port B address increments&lt;br /&gt;
     |   1   0 = Port B address is fixed&lt;br /&gt;
     |   1   1 = Port B address is fixed&lt;br /&gt;
     |&lt;br /&gt;
     V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT B VARIABLE TIMING BYTE&lt;br /&gt;
 0   0   |   0   0   0   |   |&lt;br /&gt;
         |               0   0 = Cycle Length = 4&lt;br /&gt;
         |               0   1 = Cycle Length = 3&lt;br /&gt;
         |               1   0 = Cycle Length = 2&lt;br /&gt;
         |               1   1 = Do not use&lt;br /&gt;
         |&lt;br /&gt;
         V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  ZXN PRESCALAR (FIXED TIME TRANSFER)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The ZXN PRESCALAR is a feature of the zxnDMA implementation. If non-zero, a delay will be inserted after each byte is transferred such that the total time needed for each transfer is determined by the prescalar. This works in both the continuous mode and the burst mode. If the DMA is operated in burst mode, the DMA will give up any waiting time to the CPU so that the CPU can run while the DMA is idle.&lt;br /&gt;
&lt;br /&gt;
The rate of transfer is given by the formula “Frate = 875kHz / prescalar” or, rearranged, “prescalar = 875kHz / Frate”. The formula is framed in terms of a sample rate (Frate) but Frate can be inverted to set a transfer time for each byte instead. The 875kHz constant is a nominal value assuming a 28MHz system clock; the system clock actually varies from this depending on the video timing selected by the user (HDMI, VGA0-6) so for complete accuracy the constant should be prorated according to documentation for nextreg 0x11.&lt;br /&gt;
&lt;br /&gt;
In a DMA audio setting, selecting a sample rate of 16kHz would mean setting the prescalar value to 55. This sample period is constant across changes in CPU speed.&lt;br /&gt;
&lt;br /&gt;
=== WR3 – Write Register Group 3 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 1   |   0   0   0   0   0   0&lt;br /&gt;
     |&lt;br /&gt;
     1 = DMA Enable&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Z80 DMA defines more fields but they are ignored by the zxnDMA. The two other registers defined by the Z80 DMA in this group on D4 and D3 are implemented by the zxnDMA but they do nothing.&lt;br /&gt;
&lt;br /&gt;
It is preferred to start the DMA by writing an &#039;Enable DMA&#039; command to WR6.&lt;br /&gt;
&lt;br /&gt;
=== WR4 – Write Register Group 4 ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 1   |   |   0   |   |   0   1&lt;br /&gt;
     |   |       |   |&lt;br /&gt;
     0   0 = Do not use (Behaves like Continuous mode, Byte mode on Z80 DMA)&lt;br /&gt;
     0   1 = Continuous mode&lt;br /&gt;
     1   0 = Burst mode&lt;br /&gt;
     1   1 = Do not use&lt;br /&gt;
                 |   |&lt;br /&gt;
                 |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT B STARTING ADDRESS (LOW BYTE)&lt;br /&gt;
                 |&lt;br /&gt;
                 V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  PORT B STARTING ADDRESS (HIGH BYTE)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The Z80 DMA defines three more registers in this group through D4 that define interrupt behaviour. Interrups and pulse generation are not implemented in the zxnDMA nor are these registers available for writing.&lt;br /&gt;
&lt;br /&gt;
=== WR5 – Write Register Group 5 ===&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 1   0   |   |   0   0   1   0&lt;br /&gt;
         |   |&lt;br /&gt;
         |   0 = /ce only&lt;br /&gt;
         |   1 = /ce &amp;amp; /wait multiplexed&lt;br /&gt;
         |&lt;br /&gt;
         0 = Stop on end of block&lt;br /&gt;
         1 = Auto restart on end of block&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
The /ce &amp;amp; /wait mode is implemented in the zxnDMA but is not currently used. This mode has an external device using the DMA&#039;s /ce pin to insert wait states during the DMA&#039;s transfer.&lt;br /&gt;
&lt;br /&gt;
The auto restart feature causes the DMA to automatically reload its source and destination addresses and reset its byte counter to zero to repeat the last transfer when a previous one is finished.&lt;br /&gt;
&lt;br /&gt;
=== WR6 – Command Register ===&lt;br /&gt;
&amp;lt;pre&amp;gt;D7  D6  D5  D4  D3  D2  D1  D0  BASE REGISTER BYTE&lt;br /&gt;
 1   ?   ?   ?   ?   ?   1   1&lt;br /&gt;
     |   |   |   |   |&lt;br /&gt;
     1   0   0   0   0 = 0xC3 = Reset&lt;br /&gt;
     1   0   0   0   1 = 0xC7 = Reset Port A Timing&lt;br /&gt;
     1   0   0   1   0 = 0xCB = Reset Port B Timing&lt;br /&gt;
     0   1   1   0   0 = 0xB3 = Force Ready (irrelevant for zxnDMA)&lt;br /&gt;
     0   1   1   1   1 = 0xBF = Read Status Byte&lt;br /&gt;
     0   0   0   1   0 = 0x8B = Reinitialize Status Byte&lt;br /&gt;
     0   1   0   0   1 = 0xA7 = Initialize Read Sequence&lt;br /&gt;
     1   0   0   1   1 = 0xCF = Load&lt;br /&gt;
     1   0   1   0   0 = 0xD3 = Continue&lt;br /&gt;
     0   0   0   0   1 = 0x87 = Enable DMA&lt;br /&gt;
     0   0   0   0   0 = 0x83 = Disable DMA&lt;br /&gt;
 +-- 0   1   1   1   0 = 0xBB = Read Mask Follows&lt;br /&gt;
 |&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  READ MASK&lt;br /&gt;
 0   |   |   |   |   |   |   |&lt;br /&gt;
     |   |   |   |   |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Status Byte&lt;br /&gt;
     |   |   |   |   |   |&lt;br /&gt;
     |   |   |   |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Byte Counter Low (&amp;quot;High&amp;quot; with core 3.0.5 = bug in core)&lt;br /&gt;
     |   |   |   |   |&lt;br /&gt;
     |   |   |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Byte Counter High (&amp;quot;Low&amp;quot; with core 3.0.5 = bug in core)&lt;br /&gt;
     |   |   |   |&lt;br /&gt;
     |   |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Port A Address Low&lt;br /&gt;
     |   |   |&lt;br /&gt;
     |   |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Port A Address High&lt;br /&gt;
     |   |&lt;br /&gt;
     |   V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Port B Address Low&lt;br /&gt;
     |&lt;br /&gt;
     V&lt;br /&gt;
D7  D6  D5  D4  D3  D2  D1  D0  Port B Address High&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Unimplemented Z80 DMA commands are ignored.&lt;br /&gt;
&lt;br /&gt;
Prior to starting the DMA, a LOAD command must be issued to copy the Port A and Port B addresses into the DMA&#039;s internal pointers. Then an &#039;Enable DMA&#039; command is issued to start the DMA.&lt;br /&gt;
&lt;br /&gt;
The &#039;Continue&#039; command resets the DMA&#039;s byte counter so that a following &#039;Enable DMA&#039; allows the DMA to repeat the last transfer but using the current internal address pointers. I.e. it continues from where the last copy operation left off.&lt;br /&gt;
&lt;br /&gt;
Registers can be read via an IO read from the DMA port after setting the read mask. (At power up the read mask is set to 0x7f). Register values are the current internal DMA counter values. So &#039;Port Address A Low&#039; is the lower 8-bits of Port A’s next transfer address. Once the end of the read mask is reached, further reads loop around to the first one.&lt;br /&gt;
&lt;br /&gt;
The format of the DMA status byte is as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;00E1101T&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
E is set to 0 if the total block length has been transferred at least once.&lt;br /&gt;
&lt;br /&gt;
T is set to 1 if at least one byte has been transferred.&lt;br /&gt;
&lt;br /&gt;
== Operating speed ==&lt;br /&gt;
The zxnDMA operates at the same speed as the CPU, that is 3.5MHz, 7MHz, 14MHz or 28Mhz. This is a contended clock that is modified by the ULA and the auto-slowdown by [[Layer 2|Layer2]] (which only occurred in Next core&#039;s 1 and 2, the limitation was lifted in core 3.0).&lt;br /&gt;
&lt;br /&gt;
The (pre-core 3.0) auto-slowdown occurs without user intervention if speed exceeds 7Mhz and the active Layer2 display is being generated (higher speed operation resumes when the active Layer2 display is not generated). Programmers do NOT need to account for speed differences regarding DMA transfers as this happens automatically.&lt;br /&gt;
&lt;br /&gt;
Because of this, the cycle lengths for Ports A and B can be set to their minimum values without ill effects. The cycle lengths specified for Ports A and B are intended to selectively slow down read or write cycles for hardware that cannot operate at the DMA&#039;s full speed.&lt;br /&gt;
&lt;br /&gt;
== The DMA and Interrupts ==&lt;br /&gt;
The zxnDMA cannot currently generate [[Interrupts|interrupts]].&lt;br /&gt;
&lt;br /&gt;
The other side of this is that while the DMA controls the bus, the Z80 cannot respond to interrupts. On the Z80, the NMI interrupt is edge triggered so if an NMI occurs the fact that it occurred is stored internally in the Z80 so that it will respond when it is woken up. On the other hand, maskable interrupts are level triggered. That is, the Z80 must be active to regularly sample the /INT line to determine if a maskable interrupt is occurring. On the Spectrum and the ZX Next, the ULA (and line interrupt) are only asserted for a fixed amount of time ~30 cycles at 3.5MHz. If the DMA is executing a transfer while the interrupt is asserted, the CPU will not be able to see this and it will most likely miss the interrupt. In burst mode, with large-enough prescalar value, the CPU will never miss these interrupts, although this may change if multiple channels are implemented.&lt;br /&gt;
&lt;br /&gt;
== Programming examples ==&lt;br /&gt;
A simple way to program the DMA is to walk down the list of registers WR0-WR5, sending desired settings to each. Then start the DMA by sending a LOAD command followed by an ENABLE_DMA command to WR6. Once more familiar with the DMA, you will discover that the amount of information sent can be reduced to what changes between transfers.&lt;br /&gt;
&lt;br /&gt;
=== Assembly ===&lt;br /&gt;
Short example program to DMA memory to the screen, then DMA a sprite image from memory to sprite RAM, and then showing said sprite scrolling across the screen.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;;------------------------------------------------------------------------------&lt;br /&gt;
    ; sjasmplus extra options to enable Z80N, stricter syntax and Next device&lt;br /&gt;
    opt --zxnext --syntax=abf : device zxspectrumnext&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
;     DEFINE testing        ; uncomment to produce NEX file (instead of DOT)&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
; DMA (Register 6)&lt;br /&gt;
;&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
;zxnDMA programming example&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
;(c) Jim Bagley&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
DMA_RESET                      equ $c3&lt;br /&gt;
DMA_RESET_PORT_A_TIMING        equ $c7&lt;br /&gt;
DMA_RESET_PORT_B_TIMING        equ $cb&lt;br /&gt;
DMA_LOAD                       equ $cf ; %11001111&lt;br /&gt;
DMA_CONTINUE                   equ $d3&lt;br /&gt;
DMA_DISABLE_INTERUPTS          equ $af&lt;br /&gt;
DMA_ENABLE_INTERUPTS           equ $ab&lt;br /&gt;
DMA_RESET_DISABLE_INTERUPTS    equ $a3&lt;br /&gt;
DMA_ENABLE_AFTER_RETI          equ $b7&lt;br /&gt;
DMA_READ_STATUS_BYTE           equ $bf&lt;br /&gt;
DMA_REINIT_STATUS_BYTE         equ $8b&lt;br /&gt;
DMA_START_READ_SEQUENCE        equ $a7&lt;br /&gt;
DMA_FORCE_READY                equ $b3&lt;br /&gt;
DMA_DISABLE                    equ $83&lt;br /&gt;
DMA_ENABLE                     equ $87&lt;br /&gt;
DMA_WRITE_REGISTER_COMMAND     equ $bb&lt;br /&gt;
DMA_BURST                      equ %11001101&lt;br /&gt;
DMA_CONTINUOUS                 equ %10101101&lt;br /&gt;
ZXN_DMA_PORT                   equ $6b&lt;br /&gt;
SPRITE_STATUS_SLOT_SELECT      equ $303B&lt;br /&gt;
SPRITE_IMAGE_PORT              equ $5b&lt;br /&gt;
SPRITE_INFO_PORT               equ $57&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
    IFDEF testing&lt;br /&gt;
        org $5800&lt;br /&gt;
        block 32*24, $38              ; default ULA attributes&lt;br /&gt;
        org $6000&lt;br /&gt;
    ELSE&lt;br /&gt;
        org $2000&lt;br /&gt;
    ENDIF&lt;br /&gt;
&lt;br /&gt;
start&lt;br /&gt;
    ld   hl,$0000&lt;br /&gt;
    ld   de,$4000&lt;br /&gt;
    ld   bc,$800&lt;br /&gt;
    call TransferDMA                  ; copy some random data to the screen pointing&lt;br /&gt;
                                      ; to ROM for now, for the purpose of showing &lt;br /&gt;
                                      ; how to do a DMA copy.&lt;br /&gt;
    ld   a,0                          ; sprite image number we want to update&lt;br /&gt;
    ld   bc,SPRITE_STATUS_SLOT_SELECT&lt;br /&gt;
    out  (c),a                        ; set the sprite image number&lt;br /&gt;
    ld   bc,1*256                     ; number to transfer (1)&lt;br /&gt;
    ld   hl,testsprite                ; from &lt;br /&gt;
    call TransferDMASprite            ; transfer to sprite ram&lt;br /&gt;
&lt;br /&gt;
    nextreg 21,1                      ; turn sprite on. for more info on this check &lt;br /&gt;
                                      ; out https://www.specnext.com/tbblue-io-port-system/&lt;br /&gt;
    ld   de,0&lt;br /&gt;
    ld   (xpos),de                    ; set initial X position ( doesn&#039;t need it for&lt;br /&gt;
                                      ; this demo, but if you run the .loop again it&lt;br /&gt;
                                      ; will continue from where it was&lt;br /&gt;
    ld   a,$20&lt;br /&gt;
    ld   (ypos),a                     ; set initial Y position&lt;br /&gt;
&lt;br /&gt;
.loop&lt;br /&gt;
    ld   a,0                          ; sprite number we want to position&lt;br /&gt;
    ld   bc,SPRITE_STATUS_SLOT_SELECT&lt;br /&gt;
    out  (c),a&lt;br /&gt;
    ld   de,(xpos)&lt;br /&gt;
    ld   hl,(ypos)                    ; ignores H so doing this rather than &lt;br /&gt;
                                      ; ld a,(ypos):ld l,a&lt;br /&gt;
    ld   bc,(image)                   ; not flipped or palette shifted&lt;br /&gt;
    call SetSprite&lt;br /&gt;
&lt;br /&gt;
    halt&lt;br /&gt;
&lt;br /&gt;
    ld   de,(xpos)&lt;br /&gt;
    inc  de&lt;br /&gt;
    ld   (xpos),de&lt;br /&gt;
    ld   a,d&lt;br /&gt;
    cp   $01&lt;br /&gt;
    jr   nz,.loop                     ; if high byte of xpos is not 1 (right of &lt;br /&gt;
                                      ; screen )&lt;br /&gt;
    ld   a,e&lt;br /&gt;
    cp   $20+1&lt;br /&gt;
    jr   nz,.loop                     ; if low byte is not $21 just off the right of&lt;br /&gt;
                                      ; the screen, $20 is off screen but as the &lt;br /&gt;
                                      ; INC DE is just above and not updated sprite&lt;br /&gt;
                                      ; after it, it needs to be $21&lt;br /&gt;
    xor  a&lt;br /&gt;
    ret                               ; return back to basic with OK&lt;br /&gt;
&lt;br /&gt;
xpos dw 0                             ; x position&lt;br /&gt;
ypos db 0                             ; y position&lt;br /&gt;
                                      ; these next two BITS and IMAGE are swapped &lt;br /&gt;
                                      ; as bits needs to go into B register&lt;br /&gt;
image db 0+$80                        ; use image 0 (for the image we transfered)&lt;br /&gt;
                                      ; +$80 to set the sprite to active&lt;br /&gt;
bits db 0                             ; not flipped or palette shifted&lt;br /&gt;
&lt;br /&gt;
c1 = %11100000&lt;br /&gt;
c2 = %11000000&lt;br /&gt;
c3 = %10100000&lt;br /&gt;
c4 = %10000000&lt;br /&gt;
c5 = %01100000&lt;br /&gt;
c6 = %01000000&lt;br /&gt;
c7 = %00100000&lt;br /&gt;
c8 = %00000000&lt;br /&gt;
&lt;br /&gt;
testsprite&lt;br /&gt;
    db c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1&lt;br /&gt;
    db c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c1&lt;br /&gt;
    db c1,c2,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c4,c4,c4,c4,c4,c4,c4,c4,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c5,c5,c5,c5,c5,c5,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c6,c6,c6,c6,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c7,c7,c7,c7,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c7,c8,c8,c7,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c7,c8,c8,c7,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c7,c7,c7,c7,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c6,c6,c6,c6,c6,c6,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c5,c5,c5,c5,c5,c5,c5,c5,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c4,c4,c4,c4,c4,c4,c4,c4,c4,c4,c3,c2,c1&lt;br /&gt;
    db c1,c2,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c3,c2,c1&lt;br /&gt;
    db c1,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c2,c1&lt;br /&gt;
    db c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1,c1&lt;br /&gt;
&lt;br /&gt;
;-------------------------------------------------&lt;br /&gt;
; de = X&lt;br /&gt;
; l = Y&lt;br /&gt;
; b = bits&lt;br /&gt;
; c = sprite image&lt;br /&gt;
SetSprite&lt;br /&gt;
    push bc&lt;br /&gt;
    ld bc,SPRITE_INFO_PORT&lt;br /&gt;
    out (c),e ; Xpos&lt;br /&gt;
    out (c),l ; Ypos&lt;br /&gt;
    pop hl&lt;br /&gt;
    ld a,d&lt;br /&gt;
    and 1&lt;br /&gt;
    or h&lt;br /&gt;
    out (c),a&lt;br /&gt;
    ld a,l:or $80&lt;br /&gt;
    out (c),a ; image&lt;br /&gt;
    ret&lt;br /&gt;
&lt;br /&gt;
;--------------------------------&lt;br /&gt;
; hl = source&lt;br /&gt;
; de = destination&lt;br /&gt;
; bc = length&lt;br /&gt;
;--------------------------------&lt;br /&gt;
TransferDMA&lt;br /&gt;
    di&lt;br /&gt;
    ld (DMASource),hl&lt;br /&gt;
    ld (DMADest),de&lt;br /&gt;
    ld (DMALength),bc&lt;br /&gt;
    ld hl,DMACode&lt;br /&gt;
    ld b,DMACode_Len&lt;br /&gt;
    ld c,ZXN_DMA_PORT&lt;br /&gt;
    otir&lt;br /&gt;
    ei&lt;br /&gt;
    ret&lt;br /&gt;
&lt;br /&gt;
DMACode db DMA_DISABLE&lt;br /&gt;
        db %01111101                  ; R0-Transfer mode, A -&amp;gt; B, write adress &lt;br /&gt;
                                      ; + block length&lt;br /&gt;
DMASource dw 0                        ; R0-Port A, Start address &lt;br /&gt;
                                      ; (source address)&lt;br /&gt;
DMALength dw 0                        ; R0-Block length (length in bytes)&lt;br /&gt;
        db %01010100                  ; R1-write A time byte, increment, to &lt;br /&gt;
                                      ; memory, bitmask&lt;br /&gt;
        db %00000010                  ; 2t&lt;br /&gt;
        db %01010000                  ; R2-write B time byte, increment, to &lt;br /&gt;
                                      ; memory, bitmask&lt;br /&gt;
        db %00000010                  ; R2-Cycle length port B&lt;br /&gt;
        db DMA_CONTINUOUS             ; R4-Continuous mode (use this for block &lt;br /&gt;
                                      ; transfer), write dest adress&lt;br /&gt;
DMADest dw 0                          ; R4-Dest address (destination address)&lt;br /&gt;
        db %10000010                  ; R5-Restart on end of block, RDY active &lt;br /&gt;
                                      ; LOW&lt;br /&gt;
        db DMA_LOAD                   ; R6-Load&lt;br /&gt;
        db DMA_ENABLE                 ; R6-Enable DMA&lt;br /&gt;
        &lt;br /&gt;
DMACode_Len                    equ $-DMACode&lt;br /&gt;
&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
; hl = source&lt;br /&gt;
; bc = length&lt;br /&gt;
; set port to write to with TBBLUE_REGISTER_SELECT&lt;br /&gt;
; prior to call&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
TransferDMAPort&lt;br /&gt;
    di&lt;br /&gt;
    ld (DMASourceP),hl&lt;br /&gt;
    ld (DMALengthP),bc&lt;br /&gt;
    ld hl,DMACodeP&lt;br /&gt;
    ld b,DMACode_LenP&lt;br /&gt;
    ld c,ZXN_DMA_PORT&lt;br /&gt;
    otir&lt;br /&gt;
    ei&lt;br /&gt;
    ret&lt;br /&gt;
&lt;br /&gt;
DMACodeP db DMA_DISABLE&lt;br /&gt;
        db %01111101                  ; R0-Transfer mode, A -&amp;gt; B, write adress &lt;br /&gt;
                                      ; + block length&lt;br /&gt;
DMASourceP dw 0                       ; R0-Port A, Start address (source address)&lt;br /&gt;
DMALengthP dw 0                       ; R0-Block length (length in bytes)&lt;br /&gt;
        db %01010100                  ; R1-read A time byte, increment, to &lt;br /&gt;
                                      ; memory, bitmask&lt;br /&gt;
        db %00000010                  ; R1-Cycle length port A&lt;br /&gt;
        db %01101000                  ; R2-write B time byte, increment, to &lt;br /&gt;
                                      ; memory, bitmask&lt;br /&gt;
        db %00000010                  ; R2-Cycle length port B&lt;br /&gt;
        db %10101101                  ; R4-Continuous mode (use this for block &lt;br /&gt;
                                      ; transfer), write dest adress&lt;br /&gt;
        dw $253b                      ; R4-Dest address (destination address)&lt;br /&gt;
        db %10000010                  ; R5-Restart on end of block, RDY active&lt;br /&gt;
                                      ; LOW&lt;br /&gt;
        db DMA_LOAD                   ; R6-Load&lt;br /&gt;
        db DMA_ENABLE                 ; R6-Enable DMA&lt;br /&gt;
        &lt;br /&gt;
DMACode_LenP                   equ $-DMACodeP&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
; hl = source&lt;br /&gt;
; bc = length&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
TransferDMASprite&lt;br /&gt;
    di&lt;br /&gt;
    ld (DMASourceS),hl&lt;br /&gt;
    ld (DMALengthS),bc&lt;br /&gt;
    ld hl,DMACodeS&lt;br /&gt;
    ld b,DMACode_LenS&lt;br /&gt;
    ld c,ZXN_DMA_PORT&lt;br /&gt;
    otir&lt;br /&gt;
    ei&lt;br /&gt;
    ret&lt;br /&gt;
&lt;br /&gt;
DMACodeS db DMA_DISABLE&lt;br /&gt;
        db %01111101                   ; R0-Transfer mode, A -&amp;gt; B, write adress &lt;br /&gt;
                                       ; + block length&lt;br /&gt;
DMASourceS dw 0                        ; R0-Port A, Start address (source address)&lt;br /&gt;
DMALengthS dw 0                        ; R0-Block length (length in bytes)&lt;br /&gt;
        db %01010100                   ; R1-read A time byte, increment, to &lt;br /&gt;
                                       ; memory, bitmask&lt;br /&gt;
        db %00000010                   ; R1-Cycle length port A&lt;br /&gt;
        db %01101000                   ; R2-write B time byte, increment, to &lt;br /&gt;
                                       ; memory, bitmask&lt;br /&gt;
        db %00000010                   ; R2-Cycle length port B&lt;br /&gt;
        db %10101101                   ; R4-Continuous mode (use this for block&lt;br /&gt;
                                       ; transfer), write dest adress&lt;br /&gt;
        dw SPRITE_IMAGE_PORT           ; R4-Dest address (destination address)&lt;br /&gt;
        db %10000010                   ; R5-Restart on end of block, RDY active&lt;br /&gt;
                                       ; LOW&lt;br /&gt;
        db DMA_LOAD                    ; R6-Load&lt;br /&gt;
        db DMA_ENABLE                  ; R6-Enable DMA&lt;br /&gt;
DMACode_LenS                   equ $-DMACodeS&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
; de = dest, a = fill value, bc = lenth&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
DMAFill&lt;br /&gt;
    di&lt;br /&gt;
    ld (FillValue),a&lt;br /&gt;
    ld (DMACDest),de&lt;br /&gt;
    ld (DMACLength),bc&lt;br /&gt;
    ld hl,DMACCode&lt;br /&gt;
    ld b,DMACCode_Len&lt;br /&gt;
    ld c,ZXN_DMA_PORT&lt;br /&gt;
    otir&lt;br /&gt;
    ei&lt;br /&gt;
    ret&lt;br /&gt;
&lt;br /&gt;
FillValue db 22&lt;br /&gt;
DMACCode db DMA_DISABLE&lt;br /&gt;
        db %01111101&lt;br /&gt;
DMACSource dw FillValue&lt;br /&gt;
DMACLength dw 0&lt;br /&gt;
        db %00100100,%00010000,%10101101&lt;br /&gt;
DMACDest dw 0&lt;br /&gt;
        db DMA_LOAD,DMA_ENABLE&lt;br /&gt;
DMACCode_Len equ $-DMACCode&lt;br /&gt;
&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
; End of file&lt;br /&gt;
;------------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
    IFDEF testing&lt;br /&gt;
        savenex open &amp;quot;DMAtest.nex&amp;quot;, start, $FF00&lt;br /&gt;
        savenex bank 5&lt;br /&gt;
    ELSE&lt;br /&gt;
fin&lt;br /&gt;
        savebin &amp;quot;DMATEST&amp;quot;,start,fin-start&lt;br /&gt;
    ENDIF&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Based on original text by: Allen Albright &amp;amp; Mike Dailly with input by Jim Bagley, Lyndon J Sharp and Phoebus R. Dokos&lt;br /&gt;
&lt;br /&gt;
== Technical details (core 3.1.3+)  ==&lt;br /&gt;
&lt;br /&gt;
The Zilog/zxnDMA mode is now selected by using the particular I/O port number ({{PortNo|$xx6B}} for zxnDMA mode, {{PortNo|$xx0B}} for Zilog mode). The bit 6 in {{NextRegNo|$06}} is not DMA related any more and will be reused for something different.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;counter&amp;quot; RR1-RR2 read back after transfer has correct byte order since core 3.1.4.&lt;br /&gt;
&lt;br /&gt;
Other differences described below in &amp;quot;3.0.5&amp;quot; remains (but from practical point of view the Zilog DMA emulation in 3.1.4 is near-perfect, all the remaining differences are very minor).&lt;br /&gt;
&lt;br /&gt;
== Technical details (core 3.0.5)  ==&lt;br /&gt;
&lt;br /&gt;
=== Zilog DMA compatibility mode ===&lt;br /&gt;
In Zilog DMA compatibility mode (bit 6 of {{NextRegNo|$06}}) the zxnDMA will mostly work as expected, but there are few differences in behaviour which may eventually throw off some rare SW, here is the list of the known differences (most of them describe also how the zxnDMA mode works):&lt;br /&gt;
&lt;br /&gt;
The LOAD command must be issued with correct transfer direction, loading addresses in opposite direction and flipping direction afterward will mismatch the source/destination address data (Zilog/UA858D DMA chips are also sensitive to direction flip after LOAD, but the resulting transfer quirks in different way, reading source data byte after write, offsetting whole transfer by one and damaging start/end of sequence).&lt;br /&gt;
(does apply also to zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
The LOAD command will NOT destroy the already issued &amp;quot;Initialize Read Sequence&amp;quot; - this is how even the original Zilog documentation describes the DMA chip operation, but the real Zilog DMA and UA858D (clone chip) both destroy read sequence upon LOAD command (zxnDMA is better).&lt;br /&gt;
(does apply also to zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
The content of registers read back after finished transfer differs: the counter has swapped LSB with MSB byte, and both addresses will be adjusted length+1 times (Zilog/UA858D will return destination address adjusted only length-many times).&lt;br /&gt;
(does apply also to zxnDMA mode, except addresses are adjusted only &amp;quot;length&amp;quot; times of course, counter has still swapped bytes)&lt;br /&gt;
&lt;br /&gt;
Any read of zxnDMA port without pending read request (commands &amp;quot;Read Status Byte&amp;quot; or &amp;quot;Initialize Read Sequence&amp;quot;) will return status byte (Zilog will return random value vaguely similar to status byte, but incorrect, UA858D will return zero).&lt;br /&gt;
(does apply also to zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
Status byte doesn&#039;t have bit 0 set (the &amp;quot;T&amp;quot; bit in description above).&lt;br /&gt;
(does apply also to zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
Be aware that both custom timing cycles count, and prescalar values are preserved in zxnDMA even when future write to WR1/WR2 does skip these particular bytes. To reset prescalar or cycles timing, write explicitly zero into prescalar register or use commands reset/reset-port-timing.&lt;br /&gt;
(does apply also to zxnDMA mode, the prescalar works only in zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
The destination port address is LOAD-ed even when it is &amp;quot;fixed&amp;quot; type (Contrary to Zilog DMA, which requires you to load such port as &amp;quot;source&amp;quot;, flip the direction after and re-LOAD again with correct direction. UA858D chip does also load destination port address in any case, just like zxnDMA).&lt;br /&gt;
(does apply also to zxnDMA mode)&lt;br /&gt;
&lt;br /&gt;
=== zxnDMA mode vs Zilog mode ===&lt;br /&gt;
&lt;br /&gt;
In zxnDMA mode length of transfer is equal to the length written to WR0 register, port addresses are adjusted also only length-times.&lt;br /&gt;
&lt;br /&gt;
Prescalar value will affect speed of transfer (use zero to switch prescalar off) (in burst mode during the extra idle time the CPU receives control back and can execute further instructions, in continuous mode the transfer will keep blocking CPU even when &amp;quot;slow&amp;quot; transfer is being done).&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1306</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1306"/>
		<updated>2019-05-05T10:46:43Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Fix formatting in the Global Control of Sprites section.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
P = 4-bit Palette Offset&amp;lt;br /&amp;gt;&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&amp;lt;br /&amp;gt;&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&amp;lt;br /&amp;gt;&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&amp;lt;br /&amp;gt;&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&amp;lt;br /&amp;gt;&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_flags.png|frame|center|All possibilities of Rotate, Mirror X and Mirror Y flags.]]&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&amp;lt;br /&amp;gt;&lt;br /&gt;
E = 1 to enable attribute byte 4&amp;lt;br /&amp;gt;&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
* Anchor.visible&lt;br /&gt;
* Anchor.X&lt;br /&gt;
* Anchor.Y&lt;br /&gt;
* Anchor.palette_offset&lt;br /&gt;
* Anchor.N (pattern number)&lt;br /&gt;
* Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
* Anchor.rotate&lt;br /&gt;
* Anchor.xmirror&lt;br /&gt;
* Anchor.ymirror&lt;br /&gt;
* Anchor.xscale&lt;br /&gt;
* Anchor.yscale&lt;br /&gt;
&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&amp;lt;br /&amp;gt;&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&amp;lt;br /&amp;gt;&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&amp;lt;br /&amp;gt;&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&amp;lt;br /&amp;gt;&lt;br /&gt;
Otherwise&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = Ignored&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-0 = Sprite number 0-127&amp;lt;br /&amp;gt;&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&amp;lt;br /&amp;gt;&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 3 = X mirror&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 2 = Y mirror&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 1 = Rotate&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
4-bit Sprites&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&amp;lt;br /&amp;gt;&lt;br /&gt;
Scaling&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = MSB of Y coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&amp;lt;br /&amp;gt;&lt;br /&gt;
00 = scanlines off&amp;lt;br /&amp;gt;&lt;br /&gt;
01 = scanlines 75%&amp;lt;br /&amp;gt;&lt;br /&gt;
10 = scanlines 50%&amp;lt;br /&amp;gt;&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 4-2 = set layers priorities:&amp;lt;br /&amp;gt;&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&amp;lt;br /&amp;gt;&lt;br /&gt;
000 – S L U&amp;lt;br /&amp;gt;&lt;br /&gt;
001 – L S U&amp;lt;br /&amp;gt;&lt;br /&gt;
010 – S U L&amp;lt;br /&amp;gt;&lt;br /&gt;
011 – L U S&amp;lt;br /&amp;gt;&lt;br /&gt;
100 – U S L&amp;lt;br /&amp;gt;&lt;br /&gt;
101 – U L S&amp;lt;br /&amp;gt;&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&amp;lt;br /&amp;gt;&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = Cood. of the clip window&amp;lt;br /&amp;gt;&lt;br /&gt;
1st write – X1 position&amp;lt;br /&amp;gt;&lt;br /&gt;
2nd write – X2 position&amp;lt;br /&amp;gt;&lt;br /&gt;
3rd write – Y1 position&amp;lt;br /&amp;gt;&lt;br /&gt;
4rd write – Y2 position&amp;lt;br /&amp;gt;&lt;br /&gt;
The values are 0,255,0,191 after a Reset&amp;lt;br /&amp;gt;&lt;br /&gt;
Reads do not advance the clip position&amp;lt;br /&amp;gt;&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode, the X coords are internally doubled and the clip window origin is moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-4 = Reserved, must be 0&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 3 – reset the tilemap clip index&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 1 – reset the sprite clip index.&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&amp;lt;br /&amp;gt;&lt;br /&gt;
000 = ULA first palette&amp;lt;br /&amp;gt;&lt;br /&gt;
100 = ULA second palette&amp;lt;br /&amp;gt;&lt;br /&gt;
001 = Layer 2 first palette&amp;lt;br /&amp;gt;&lt;br /&gt;
101 = Layer 2 second palette&amp;lt;br /&amp;gt;&lt;br /&gt;
010 = Sprites first palette&amp;lt;br /&amp;gt;&lt;br /&gt;
110 = Sprites second palette&amp;lt;br /&amp;gt;&lt;br /&gt;
011 = Tilemap first palette&amp;lt;br /&amp;gt;&lt;br /&gt;
111 = Tilemap second palette&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = Enable ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&amp;lt;br /&amp;gt;&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15, PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&amp;lt;br /&amp;gt;&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&amp;lt;br /&amp;gt;&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&amp;lt;br /&amp;gt;&lt;br /&gt;
1st write:&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = RRRGGGBB&amp;lt;br /&amp;gt;&lt;br /&gt;
2nd write.&amp;lt;br /&amp;gt;&lt;br /&gt;
If writing a L2 palette&amp;lt;br /&amp;gt;&lt;br /&gt;
———————————————————————–&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&amp;lt;br /&amp;gt;&lt;br /&gt;
Priority colour will always be on top even on an SLU priority arrangement. If you need the exact same colour on priority and non priority locations you will need to program the same colour twice changing bit 7 to 0 for the second colour&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-1 = Reserved, must be 0&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&amp;lt;br /&amp;gt;&lt;br /&gt;
———————————————————————–&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-1 = Reserved, must be 0&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&amp;lt;br /&amp;gt;&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1305</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1305"/>
		<updated>2019-05-05T10:39:05Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Fix formatting in the Programming Sprites section.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
P = 4-bit Palette Offset&amp;lt;br /&amp;gt;&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&amp;lt;br /&amp;gt;&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&amp;lt;br /&amp;gt;&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&amp;lt;br /&amp;gt;&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&amp;lt;br /&amp;gt;&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_flags.png|frame|center|All possibilities of Rotate, Mirror X and Mirror Y flags.]]&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&amp;lt;br /&amp;gt;&lt;br /&gt;
E = 1 to enable attribute byte 4&amp;lt;br /&amp;gt;&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
* Anchor.visible&lt;br /&gt;
* Anchor.X&lt;br /&gt;
* Anchor.Y&lt;br /&gt;
* Anchor.palette_offset&lt;br /&gt;
* Anchor.N (pattern number)&lt;br /&gt;
* Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
* Anchor.rotate&lt;br /&gt;
* Anchor.xmirror&lt;br /&gt;
* Anchor.ymirror&lt;br /&gt;
* Anchor.xscale&lt;br /&gt;
* Anchor.yscale&lt;br /&gt;
&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&amp;lt;br /&amp;gt;&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&amp;lt;br /&amp;gt;&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&amp;lt;br /&amp;gt;&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&amp;lt;br /&amp;gt;&lt;br /&gt;
Otherwise&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = Ignored&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 6-0 = Sprite number 0-127&amp;lt;br /&amp;gt;&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&amp;lt;br /&amp;gt;&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 3 = X mirror&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 2 = Y mirror&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 1 = Rotate&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&amp;lt;br /&amp;gt;&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&amp;lt;br /&amp;gt;&lt;br /&gt;
4-bit Sprites&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&amp;lt;br /&amp;gt;&lt;br /&gt;
Scaling&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
bit 0 = MSB of Y coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1304</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1304"/>
		<updated>2019-05-05T10:33:31Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Add bullet points to Composite section.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
P = 4-bit Palette Offset&amp;lt;br /&amp;gt;&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&amp;lt;br /&amp;gt;&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&amp;lt;br /&amp;gt;&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&amp;lt;br /&amp;gt;&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&amp;lt;br /&amp;gt;&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_flags.png|frame|center|All possibilities of Rotate, Mirror X and Mirror Y flags.]]&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&amp;lt;br /&amp;gt;&lt;br /&gt;
E = 1 to enable attribute byte 4&amp;lt;br /&amp;gt;&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
* Anchor.visible&lt;br /&gt;
* Anchor.X&lt;br /&gt;
* Anchor.Y&lt;br /&gt;
* Anchor.palette_offset&lt;br /&gt;
* Anchor.N (pattern number)&lt;br /&gt;
* Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
* Anchor.rotate&lt;br /&gt;
* Anchor.xmirror&lt;br /&gt;
* Anchor.ymirror&lt;br /&gt;
* Anchor.xscale&lt;br /&gt;
* Anchor.yscale&lt;br /&gt;
&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1303</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1303"/>
		<updated>2019-05-05T10:29:59Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Add better formatting to the attribute section.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
P = 4-bit Palette Offset&amp;lt;br /&amp;gt;&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&amp;lt;br /&amp;gt;&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&amp;lt;br /&amp;gt;&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&amp;lt;br /&amp;gt;&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&amp;lt;br /&amp;gt;&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_flags.png|frame|center|All possibilities of Rotate, Mirror X and Mirror Y flags.]]&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&amp;lt;br /&amp;gt;&lt;br /&gt;
E = 1 to enable attribute byte 4&amp;lt;br /&amp;gt;&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&amp;lt;br /&amp;gt;&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
Anchor.visible&lt;br /&gt;
Anchor.X&lt;br /&gt;
Anchor.Y&lt;br /&gt;
Anchor.palette_offset&lt;br /&gt;
Anchor.N (pattern number)&lt;br /&gt;
Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
Anchor.rotate&lt;br /&gt;
Anchor.xmirror&lt;br /&gt;
Anchor.ymirror&lt;br /&gt;
Anchor.xscale&lt;br /&gt;
Anchor.yscale&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=File:Sprite_flags.png&amp;diff=1302</id>
		<title>File:Sprite flags.png</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=File:Sprite_flags.png&amp;diff=1302"/>
		<updated>2019-05-05T10:25:29Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Sprite flags.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Summary ==&lt;br /&gt;
Sprite flags.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1301</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1301"/>
		<updated>2019-05-05T10:24:46Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Add the sprite flags image link.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
P = 4-bit Palette Offset&amp;lt;br /&amp;gt;&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&amp;lt;br /&amp;gt;&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&amp;lt;br /&amp;gt;&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&amp;lt;br /&amp;gt;&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&amp;lt;br /&amp;gt;&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&amp;lt;br /&amp;gt;&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_flags.png|frame|center|All possibilities of Rotate, Mirror X and Mirror Y flags.]]&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&lt;br /&gt;
E = 1 to enable attribute byte 4&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
Anchor.visible&lt;br /&gt;
Anchor.X&lt;br /&gt;
Anchor.Y&lt;br /&gt;
Anchor.palette_offset&lt;br /&gt;
Anchor.N (pattern number)&lt;br /&gt;
Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
Anchor.rotate&lt;br /&gt;
Anchor.xmirror&lt;br /&gt;
Anchor.ymirror&lt;br /&gt;
Anchor.xscale&lt;br /&gt;
Anchor.yscale&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1300</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1300"/>
		<updated>2019-05-05T10:19:17Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Format the code sections.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;X X X X X X X X&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;Y Y Y Y Y Y Y Y&amp;lt;/pre&amp;gt;&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;P P P P XM YM R X8/PR&amp;lt;/pre&amp;gt;&lt;br /&gt;
P = 4-bit Palette Offset&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All possibilities of Rotate, Mirror X and Mirror Y flags.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
&amp;lt;pre&amp;gt;V E N5 N4 N3 N2 N1 N0&amp;lt;/pre&amp;gt;&lt;br /&gt;
V = 1 to make the sprite visible&lt;br /&gt;
E = 1 to enable attribute byte 4&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
&amp;lt;pre&amp;gt;H N6 T X X Y Y Y8&amp;lt;/pre&amp;gt;&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 X X Y Y PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
&amp;lt;pre&amp;gt;0 1 N6 0 0 0 0 PO&amp;lt;/pre&amp;gt;&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
Anchor.visible&lt;br /&gt;
Anchor.X&lt;br /&gt;
Anchor.Y&lt;br /&gt;
Anchor.palette_offset&lt;br /&gt;
Anchor.N (pattern number)&lt;br /&gt;
Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
Anchor.rotate&lt;br /&gt;
Anchor.xmirror&lt;br /&gt;
Anchor.ymirror&lt;br /&gt;
Anchor.xscale&lt;br /&gt;
Anchor.yscale&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;0 0 0 0 0 0 M C&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1299</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1299"/>
		<updated>2019-05-05T10:14:03Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Fix image caption.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png|frame|center|Pattern example]]&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
X X X X X X X X&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
Y Y Y Y Y Y Y Y&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
P P P P XM YM R X8/PR&lt;br /&gt;
P = 4-bit Palette Offset&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All possibilities of Rotate, Mirror X and Mirror Y flags.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
V E N5 N4 N3 N2 N1 N0&lt;br /&gt;
V = 1 to make the sprite visible&lt;br /&gt;
E = 1 to enable attribute byte 4&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
H N6 T X X Y Y Y8&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
0 1 N6 X X Y Y PO&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
0 1 N6 0 0 0 0 PO&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
Anchor.visible&lt;br /&gt;
Anchor.X&lt;br /&gt;
Anchor.Y&lt;br /&gt;
Anchor.palette_offset&lt;br /&gt;
Anchor.N (pattern number)&lt;br /&gt;
Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
Anchor.rotate&lt;br /&gt;
Anchor.xmirror&lt;br /&gt;
Anchor.ymirror&lt;br /&gt;
Anchor.xscale&lt;br /&gt;
Anchor.yscale&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
0 0 0 0 0 0 M C&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
	<entry>
		<id>http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1298</id>
		<title>Sprites</title>
		<link rel="alternate" type="text/html" href="http://wiki.specnext.dev/index.php?title=Sprites&amp;diff=1298"/>
		<updated>2019-05-04T11:52:47Z</updated>

		<summary type="html">&lt;p&gt;Intrepidis: Add formatting to headers.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The Spectrum Next has a hardware sprite system with the following characteristics:&lt;br /&gt;
&lt;br /&gt;
* Total of 128 sprites&lt;br /&gt;
* Display surface is 320×256 overlapping the ULA screen by 32 pixels on each side&lt;br /&gt;
* Minimum of 100 sprites per scanline*&lt;br /&gt;
* Choice of 512 colours for each pixel&lt;br /&gt;
* Site of each sprite is 16×16 pixels but sprites can be magnified 2x, 4x or 8x horizontally and vertically&lt;br /&gt;
* Sprites can be mirrored and rotated&lt;br /&gt;
* Sprites can be grouped together to form larger sprites under the control of a single anchor&lt;br /&gt;
* A 16K pattern memory can contain 64 8-bit sprite images or 128 4-bit sprite images and combinations in-between&lt;br /&gt;
* A per sprite palette offset allows sprites to share images but colour them differently&lt;br /&gt;
* A nextreg interface allows the copper to move sprites during the video frame&lt;br /&gt;
&lt;br /&gt;
&amp;lt;nowiki&amp;gt;*&amp;lt;/nowiki&amp;gt;A minimum of 100 16×16 sprites is guaranteed to be displayed in any scanline. Any additional sprites will not be displayed with the hardware ensuring sprites are not partially plotted.&lt;br /&gt;
&lt;br /&gt;
The actual limit is determined by how many 28MHz clock cycles there are in a scanline. The sprite hardware is able to plot one pixel cycle and uses one cycle to qualify each sprite. Since the number of cycles there are in a scanline varies with video timing (HDMI, VGA), the number of pixels that can be plotted also varies but the minimum will be 1600 pixels per line including overhead cycles needed to qualify 100 sprites. Since sprites magified horizontally involve plotting more pixels, x2 x4 x8 sprites will take more cycles to plot and the presence of these sprites in a line will reduce the total number of sprites that can be plotted.&lt;br /&gt;
&lt;br /&gt;
== Sprite Patterns ==&lt;br /&gt;
Sprite patterns are the images that each sprite can take on. The images are stored in a 16K memory internal to the FPGA and are identified by pattern number. A particular sprite chooses a pattern by storing a pattern number in its attributes.&lt;br /&gt;
&lt;br /&gt;
All sprites are 16×16 pixels in size but the come in two flavours: 4-bit and 8-bit. The bit width describes how many bits are used to code the colour of each pixel.&lt;br /&gt;
&lt;br /&gt;
An 8-bit sprite uses a full byte to colour each of its pixels so that each pixel can be one of 256 colours. In this case, a 16×16 sprite requires 256 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
A 4-bit sprite uses a nibble to colour each of its pixels so that each pixel can be one of 16 colours. In this case, a 16×16 sprite requires just 128 bytes of pattern memory to store its image.&lt;br /&gt;
&lt;br /&gt;
The 16K pattern memory can contain any combination of these images, whether they are 128 bytes or 256 bytes and their locations in the pattern memory are described by a pattern number. This pattern number is 7 bits with bits named as follows:&lt;br /&gt;
&lt;br /&gt;
=== Pattern Number ===&lt;br /&gt;
&amp;lt;pre&amp;gt;N5 N4 N3 N2 N1 N0 N6&lt;br /&gt;
N6, despite the name, is the least significant bit.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This 7-bit pattern number can identify 128 patterns in the 16k pattern memory, each of which are 128 bytes in size. The full 7-bits are therefore used for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
For 8-bit sprites, N6=0 always. The remaining 6 bits can identify 64 patterns, each of which is 256 bytes in size.&lt;br /&gt;
&lt;br /&gt;
The N5:N0,N6 bits are stored in a particular sprite’s attributes to identify which image a sprite uses.&lt;br /&gt;
&lt;br /&gt;
=== 8-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 8-bits for each pixel so that each pixel can be one of 256 colours. One colour indicates transparency and this is programmed into the Sprite Transparency Index register (nextreg 0x4B). By default the transparent value is 0xE3.&lt;br /&gt;
&lt;br /&gt;
As an example of an 8-bit sprite, let’s have a look at the sprite below:&lt;br /&gt;
&lt;br /&gt;
[[File:Sprite_1.png]]&lt;br /&gt;
&lt;br /&gt;
===== Pattern example =====&lt;br /&gt;
&lt;br /&gt;
Using the default palette, which is initialised with RGB332 colours from 0-255, the hexadecimal values for this pattern arranged in a 16×16 array are shown below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;04040404040404E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFFFFFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBFBFBFF04E3E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5F5FBFF04E3E3E3E3E3E3E3E3&lt;br /&gt;
04FFFBF5A8A8FBFF04E3E3E3E3E3E3E3&lt;br /&gt;
04FFFFFBA844A8FBFF04E3E3E3E3E3E3&lt;br /&gt;
040404FFFBA844A8FBFF04E3E3E3E3E3&lt;br /&gt;
E3E3E304FFFBA84444FBFF04E304E3E3&lt;br /&gt;
E3E3E3E304FFFB444444FBFF044D04E3&lt;br /&gt;
E3E3E3E3E304FFFB44444444FA4D04E3&lt;br /&gt;
E3E3E3E3E3E304FFFB44FFF54404E3E3&lt;br /&gt;
E3E3E3E3E3E3E304FF44F5A804E3E3E3&lt;br /&gt;
E3E3E3E3E3E3E3E304FA4404A804E3E3&lt;br /&gt;
E3E3E3E3E3E3E3044D4D04E304F504E3&lt;br /&gt;
E3E3E3E3E3E3E3E30404E3E3E304FA04&lt;br /&gt;
E3E3E3E3E3E3E3E3E3E3E3E3E3E30404&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here 0xE3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 256 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
=== 4-Bit Sprite Patterns ===&lt;br /&gt;
The 16×16 pixel image uses 4-bits for each pixel so that each pixel can be one of 16 colours. One colour indicates transparency and this is programmed into the lower 4-bits of the Sprite Transparency Index register (nextreg 0x4B). By default the transparency value is 0x3. Note that the same register is shared with 8-bit patterns to identify the transparent index.&lt;br /&gt;
&lt;br /&gt;
Since each pixel only occupies 4-bits, two pixels are stored in each byte. The leftmost pixel is stored in the upper 4-bits and the rightmost pixel is stored in the lower 4-bits.&lt;br /&gt;
&lt;br /&gt;
As an example we will use the same sprite image as was given in the 8-bit pattern example. Here only the lower 4 bits of each pixel is retained to confine each pixel’s color to 4-bits:&lt;br /&gt;
&lt;br /&gt;
4444444333333333&lt;br /&gt;
4FFFFF4333333333&lt;br /&gt;
4FBBBF4333333333&lt;br /&gt;
4FB55BF433333333&lt;br /&gt;
4FB588BF43333333&lt;br /&gt;
4FFB848BF4333333&lt;br /&gt;
444FB848BF433333&lt;br /&gt;
3334FB844BF43433&lt;br /&gt;
33334FB444BF4D43&lt;br /&gt;
333334FB4444AD43&lt;br /&gt;
3333334FB4F54433&lt;br /&gt;
33333334F4584333&lt;br /&gt;
333333334A448433&lt;br /&gt;
33333334DD434543&lt;br /&gt;
33333333443334A4&lt;br /&gt;
3333333333333344&lt;br /&gt;
0x3 is used as the transparent index.&lt;br /&gt;
&lt;br /&gt;
These 128 bytes would be stored in pattern memory in left to right, top to bottom order.&lt;br /&gt;
&lt;br /&gt;
The actual colour that will appear on screen will depend on the palette, described below. The default palette will not likely generate suitable colours for 4-bit sprites.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Palette ===&lt;br /&gt;
Each pixel of a sprite image is 8-bit for 8-bit patterns or 4-bit for 4-bit patterns. The pixel value is known as a pixel colour index. This colour index is combined with the sprite’s palette offset. The palette offset is a 4-bit value added to the top 4-bits of the pixel colour index. The purpose of the palette offset is to allow a sprite to change the colour of an image.&lt;br /&gt;
&lt;br /&gt;
The final sprite colour index generated by the sprite hardware is then the sum of the pixel index and the 4-bit palette offset. In pictures using binary math:&lt;br /&gt;
&lt;br /&gt;
8-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ IIIIIIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS&lt;br /&gt;
&lt;br /&gt;
4-bit Sprite&lt;br /&gt;
PPPP0000&lt;br /&gt;
+ 0000IIII&lt;br /&gt;
----------&lt;br /&gt;
SSSSSSSS = PPPPIIII&lt;br /&gt;
Where “PPPP” is the 4-bit palette offset from the sprite’s attributes and the “I”s represent the pixel value from the sprite pattern. The final sprite index is represented by the 8-bit value “SSSSSSSS”.&lt;br /&gt;
&lt;br /&gt;
For 4-bit sprites the palette offset can be thought of as selecting one of 16 different 16-colour palettes.&lt;br /&gt;
&lt;br /&gt;
This final 8-bit sprite index is then passed through the sprite palette which acts like a lookup table that returns the 9-bit RGB333 colour associated with the sprite index.&lt;br /&gt;
&lt;br /&gt;
At power up, the sprite palette is initialized such that the sprite index passes through unchanged and is therefore interpretted as an RGB332 colour. The missing third blue bit is generated as the logical OR of the two other blue bits. In short, for 8-bit sprites, the sprite index also acts like the colour when using the default palette.&lt;br /&gt;
&lt;br /&gt;
=== Sprite Attributes ===&lt;br /&gt;
A sprite’s attributes is a list of properties that determine how and where the sprite is drawn.&lt;br /&gt;
&lt;br /&gt;
Each sprite is described by either 4 or 5 attribute bytes listed below:&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 0 ====&lt;br /&gt;
X X X X X X X X&lt;br /&gt;
The least significant eight bits of the sprite’s X coordinate. The ninth bit is found in sprite attribute 2.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 1 ====&lt;br /&gt;
Y Y Y Y Y Y Y Y&lt;br /&gt;
The least significant eight bits of the sprite’s Y coordinate. The ninth bit is optional and is found in attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 2 ====&lt;br /&gt;
P P P P XM YM R X8/PR&lt;br /&gt;
P = 4-bit Palette Offset&lt;br /&gt;
XM = 1 to mirror the sprite image horizontally&lt;br /&gt;
YM = 1 to mirror the sprite image vertically&lt;br /&gt;
R = 1 to rotate the sprite image 90 degrees clockwise&lt;br /&gt;
X8 = Ninth bit of the sprite’s X coordinate&lt;br /&gt;
PR = 1 to indicate P is relative to the anchor’s palette offset (relative sprites only)&lt;br /&gt;
&lt;br /&gt;
Rotation is applied before mirroring.&lt;br /&gt;
Relative sprites, described below, replace X8 with PR.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
All possibilities of Rotate, Mirror X and Mirror Y flags.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 3 ====&lt;br /&gt;
V E N5 N4 N3 N2 N1 N0&lt;br /&gt;
V = 1 to make the sprite visible&lt;br /&gt;
E = 1 to enable attribute byte 4&lt;br /&gt;
N = Sprite pattern to use 0-63&lt;br /&gt;
&lt;br /&gt;
If E=0, the sprite is fully described by sprite attributes 0-3. The sprite pattern is an 8-bit one identified by pattern N=0-63. The sprite is an anchor and cannot be made relative. The sprite is displayed as if sprite attribute 4 is zero.&lt;br /&gt;
&lt;br /&gt;
If E=1, the sprite is further described by sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== Sprite Attribute 4 ====&lt;br /&gt;
===== A. Extended Anchor Sprite =====&lt;br /&gt;
H N6 T X X Y Y Y8&lt;br /&gt;
H = 1 if the sprite pattern is 4-bit&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
T = 0 if relative sprites are composite type else 1 for unified type&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
Y8 = Ninth bit of the sprite’s Y coordinate&lt;br /&gt;
&lt;br /&gt;
{H,N6} must not equal {0,1} as this combination is used to indicate a relative sprite.&lt;br /&gt;
&lt;br /&gt;
===== B. Relative Sprite, Composite Type =====&lt;br /&gt;
0 1 N6 X X Y Y PO&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
XX = Magnification in the X direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
YY = Magnification in the Y direction (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
===== C. Relative Sprite, Unified Type =====&lt;br /&gt;
0 1 N6 0 0 0 0 PO&lt;br /&gt;
N6 = 7th pattern bit if the sprite pattern is 4-bit&lt;br /&gt;
PO = 1 to indicate the sprite pattern number is relative to the anchor’s&lt;br /&gt;
&lt;br /&gt;
The display surface for sprites is 320×256. The X coordinate of the sprite is nine bits, ranging over 0-511, and the Y coordinate is optionally nine bits again ranging over 0-511 or is eight bits ranging over 0-255. The full extent 0-511 wraps on both axes, meaning a sprite 16 pixels wide plotted at X coordinate 511 would see its first pixel not displayed (coordinate 511) and the following pixels displayed in coordinates 0-14.&lt;br /&gt;
&lt;br /&gt;
The full display area is visible in VGA. However, the HDMI display is vertically shorter so the top eight pixel rows (Y = 0-7) and the bottom eight pixel rows (Y = 248-255) will not be visible on an HDMI display.&lt;br /&gt;
&lt;br /&gt;
Sprites can be fully described by sprite attributes 0-3 if the E bit in sprite attribute 3 is zero. These sprites are compatible with the original sprite module from core versions prior to 2.00.26.&lt;br /&gt;
&lt;br /&gt;
If the E bit is set then a fifth sprite attribute, sprite attribute 4, becomes active. This attribute introduces scaling, 4-bit patterns, and relative sprites. Scaling is self-explanatory and 4-bit patterns were described in the last section. Relative sprites are described in the next section.&lt;br /&gt;
&lt;br /&gt;
=== Relative Sprites ===&lt;br /&gt;
Normal sprites (sprites that are not relative) are known as anchor sprites. As the sprite module draws sprites in the order 0-127 (there are 128 sprites), it internally stores characteristics of the last anchor sprite seen. If following sprites are relative, they inherit some of these characteristics, which allows relative sprites to have, among other things, coordinates relative to the anchor. This means moving the anchor sprite also causes its relatives to move with it.&lt;br /&gt;
&lt;br /&gt;
There are two types of relative sprites supported known as “Composite Sprites” and “Unified Sprites”. The type is determined by the anchor in the T bit of sprite attribute 4.&lt;br /&gt;
&lt;br /&gt;
==== A. Composite Sprites ====&lt;br /&gt;
The sprite module records the following information from the anchor:&lt;br /&gt;
&lt;br /&gt;
Anchor.visible&lt;br /&gt;
Anchor.X&lt;br /&gt;
Anchor.Y&lt;br /&gt;
Anchor.palette_offset&lt;br /&gt;
Anchor.N (pattern number)&lt;br /&gt;
Anchor.H (indicates if the sprite uses 4-bit patterns)&lt;br /&gt;
These recorded items are not used by composite sprites:&lt;br /&gt;
&lt;br /&gt;
Anchor.rotate&lt;br /&gt;
Anchor.xmirror&lt;br /&gt;
Anchor.ymirror&lt;br /&gt;
Anchor.xscale&lt;br /&gt;
Anchor.yscale&lt;br /&gt;
The anchor determines if all its relative sprites use 4-bit patterns or not.&lt;br /&gt;
&lt;br /&gt;
The visibility of a particular relative sprite is the result of ANDing the anchor’s visibility with the relative sprite’s visibility. In other words, if the anchor is invisible then so are all its relatives.&lt;br /&gt;
&lt;br /&gt;
Relative sprites only have 8-bit X and Y coordinates (the ninth bits are taken for other purposes). These are signed offsets from the anchor’s X,Y coordinate. Moving the anchor moves all its relatives along with it.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PR bit set in sprite attribute 2, then the anchor’s palette offset is added to the relative sprite’s to determine the active palette offset for the relative sprite. Otherwise the relative sprite uses its own palette offset as usual.&lt;br /&gt;
&lt;br /&gt;
If the relative sprite has its PO bit set in sprite attribute 4, then the anchor’s pattern number is added to the relative sprite’s to determine the pattern used for display. Otherwise the relative sprite uses its own pattern number as usual. The intention is to supply a method to easily animate a large sprite by manipulating the pattern number in the anchor.&lt;br /&gt;
&lt;br /&gt;
A composite sprite is like a collection of independent sprites tied to an anchor.&lt;br /&gt;
&lt;br /&gt;
==== B. Unified Sprites ====&lt;br /&gt;
Unified sprites are a further extension of the composite type. The same information is recorded from the anchor and the same behaviour as described under composite sprites applies.&lt;br /&gt;
&lt;br /&gt;
The difference is the collection of anchor and relatives is treated as if it were a single 16×16 sprite. The anchor’s rotation, mirror, and scaling bits apply to all its relatives. Rotating the anchor causes all the relatives to rotate around the anchor. Mirroring the anchor causes the relatives to mirror around the anchor. The sprite hardware will automatically adjust X,Y coords and rotation, scaling and mirror bits of all relatives according to settings in the anchor.&lt;br /&gt;
&lt;br /&gt;
Unified sprites should be defined as if all its parts are 16×16 in size with the anchor controlling the look of the whole.&lt;br /&gt;
&lt;br /&gt;
A unified sprite is like a big version of an individual 16×16 sprite controlled by the anchor.&lt;br /&gt;
&lt;br /&gt;
=== Programming Sprites ===&lt;br /&gt;
Sprites are created via three io registers and a nextreg interface.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (W)&lt;br /&gt;
&lt;br /&gt;
X S S S S S S S&lt;br /&gt;
N6 X N N N N N N&lt;br /&gt;
A write to this port has two effects.&lt;br /&gt;
&lt;br /&gt;
One is it selects one of 128 sprites for writing sprite attributes via port 0x57.&lt;br /&gt;
&lt;br /&gt;
The other is it selects one of 128 4-bit patterns in pattern memory for writing sprite patterns via port 0x5B. The N6 bit shown is the least significant in the 7-bit pattern number and should always be zero when selecting one of 64 8-bit patterns indicated by N.&lt;br /&gt;
&lt;br /&gt;
Port 0x57 (W)&lt;br /&gt;
&lt;br /&gt;
Once a sprite is selected via port 0x303B, its attributes can be written to this port one byte after another. Sprites can have either four or five attribute bytes and the internal attribute pointer will move onto the next sprite after those four or five attribute bytes are written. This means you can select a sprite via port 0x303B and write attributes for as many sequential sprites as desired. The attribute pointer will roll over from sprite 127 to sprite 0.&lt;br /&gt;
&lt;br /&gt;
Port 0x5B (W)&lt;br /&gt;
&lt;br /&gt;
Once a pattern number is selected via port 0x303B, the 256-byte or 128-byte pattern can be written to this port. The internal pattern pointer auto-increments after each write so as many sequential patterns as desired can be written. The internal pattern pointer will roll over from pattern 127 to pattern 0 (4-bit patterns) or from pattern 63 to pattern 0 (8-bit patterns) automatically.&lt;br /&gt;
&lt;br /&gt;
Port 0x303B (R)&lt;br /&gt;
&lt;br /&gt;
0 0 0 0 0 0 M C&lt;br /&gt;
M = 1 if the maximum number of sprites per line was exceeded&lt;br /&gt;
C = 1 if any two displayed sprites collide on screen&lt;br /&gt;
&lt;br /&gt;
Reading this port automatically resets the M and C bits.&lt;br /&gt;
&lt;br /&gt;
Besides the i/o interface, there is a nextreg interface to sprite attributes. The nextreg interface allows the copper to manipulate sprites and grants the program random access to a sprite’s individual attribute bytes.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x34 (52) =&amp;gt; Sprite Number&lt;br /&gt;
If the sprite number is in lockstep with io port 0x303B (nextreg 0x09 bit 4 is set)&lt;br /&gt;
bits 7 = Pattern address offset (Add 128 to pattern address)&lt;br /&gt;
bits 6-0 = Sprite number 0-127, Pattern number 0-63&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Effectively performs an out to port 0x303B with the same value&lt;br /&gt;
Otherwise&lt;br /&gt;
bit 7 = Ignored&lt;br /&gt;
bits 6-0 = Sprite number 0-127&lt;br /&gt;
Selects which sprite has its attributes connected to the following registers.&lt;br /&gt;
Bit 7 always reads back as zero.&lt;br /&gt;
&lt;br /&gt;
This nextreg can operate in two modes.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is set, then this register is kept in lockstep with i/o port 0x303B. A write to this nextreg is equivalent to a write to port 0x303B and vice versa. In this mode, the i/o interface and nextreg interface are exactly equivalent.&lt;br /&gt;
&lt;br /&gt;
If nextreg 0x09 bit 4 is reset, then the nextreg interface is decoupled from i/o port 0x303B. This nextreg is used to select a particular sprite 0-127 and this is completely independent from the sprite selected for the i/o interface. This independence allows the copper, for example, to manipulate different sprites than the cpu using the i/o interface.&lt;br /&gt;
&lt;br /&gt;
(W) 0x35 (53) =&amp;gt; Sprite Attribute 0&lt;br /&gt;
(W) 0x75 (117) =&amp;gt; Sprite Attribute 0 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x75 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x36 (54) =&amp;gt; Sprite Attribute 1&lt;br /&gt;
(W) 0x76 (118) =&amp;gt; Sprite Attribute 1 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-0 = LSB of Y coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x76 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x37 (55) =&amp;gt; Sprite Attribute 2&lt;br /&gt;
(W) 0x77 (119) =&amp;gt; Sprite Attribute 2 with automatic post increment of Sprite Number&lt;br /&gt;
bits 7-4 = Palette offset added to top 4 bits of sprite colour index&lt;br /&gt;
bit 3 = X mirror&lt;br /&gt;
bit 2 = Y mirror&lt;br /&gt;
bit 1 = Rotate&lt;br /&gt;
bit 0 = MSB of X coordinate&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x77 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x38 (56) =&amp;gt; Sprite Attribute 3&lt;br /&gt;
(W) 0x78 (120) =&amp;gt; Sprite Attribute 3 with automatic post increment of Sprite Number&lt;br /&gt;
bit 7 = Visible flag (1 = displayed)&lt;br /&gt;
bit 6 = Extended attribute (1 = Sprite Attribute 4 is active)&lt;br /&gt;
bits 5-0 = Pattern used by sprite (0-63)&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x78 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
(W) 0x39 (57) =&amp;gt; Sprite Attribute 4&lt;br /&gt;
(W) 0x79 (121) =&amp;gt; Sprite Attribute 4 with automatic post increment of Sprite Number&lt;br /&gt;
4-bit Sprites&lt;br /&gt;
bit 7 = H (1 = sprite uses 4-bit patterns)&lt;br /&gt;
bit 6 = N6 (0 = use the first 128 bytes of the pattern else use the last 128 bytes)&lt;br /&gt;
bit 5 = 1 if relative sprites are composite, 0 if relative sprites are unified&lt;br /&gt;
Scaling&lt;br /&gt;
bits 4-3 = X scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bits 2-1 = Y scaling (00 = 1x, 01 = 2x, 10 = 4x, 11 = 8x)&lt;br /&gt;
bit 0 = MSB of Y coordinate&lt;br /&gt;
A relative mode is enabled if H,N6 = 01. The byte format for relative sprites is described above.&lt;br /&gt;
&lt;br /&gt;
A write to nextreg 0x79 also increases the selected sprite in nextreg 0x34.&lt;br /&gt;
&lt;br /&gt;
=== Global Control of Sprites ===&lt;br /&gt;
The following nextreg are also of interest for sprites.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x09 (09) =&amp;gt; Peripheral 4 setting:&lt;br /&gt;
bit 7 = Mono setting for AY 2 (1 = mono, 0 default)&lt;br /&gt;
bit 6 = Mono setting for AY 1 (1 = mono, 0 default)&lt;br /&gt;
bit 5 = Mono setting for AY 0 (1 = mono, 0 default)&lt;br /&gt;
bit 4 = Sprite id lockstep (1 = Nextreg 0x34 and IO Port 0x303B are in lockstep, 0 default)&lt;br /&gt;
bit 3 = Disables Kempston port ($DF) if set&lt;br /&gt;
bit 2 = Disables divMMC ports ($E3, $E7, $EB) if set&lt;br /&gt;
bits 1-0 = scanlines (0 after a PoR or Hard-reset)&lt;br /&gt;
00 = scanlines off&lt;br /&gt;
01 = scanlines 75%&lt;br /&gt;
10 = scanlines 50%&lt;br /&gt;
11 = scanlines 25%&lt;br /&gt;
&lt;br /&gt;
Bit 4 determines if the i/o interface and nextreg interface operate in lockstep.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x15 (21) =&amp;gt; Sprite and Layers system&lt;br /&gt;
bit 7 = LoRes mode, 128 x 96 x 256 colours (1 = enabled)&lt;br /&gt;
bit 6 = Sprite priority (1 = sprite 0 on top, 0 = sprite 127 on top)&lt;br /&gt;
bit 5 = Enable sprite clipping in over border mode (1 = enabled)&lt;br /&gt;
bits 4-2 = set layers priorities:&lt;br /&gt;
Reset default is 000, sprites over the Layer 2, over the ULA graphics&lt;br /&gt;
000 – S L U&lt;br /&gt;
001 – L S U&lt;br /&gt;
010 – S U L&lt;br /&gt;
011 – L U S&lt;br /&gt;
100 – U S L&lt;br /&gt;
101 – U L S&lt;br /&gt;
110 – S(U+L) ULA and Layer 2 combined, colours clamped to 7&lt;br /&gt;
111 – S(U+L-5) ULA and Layer 2 combined, colours clamped to [0,7]&lt;br /&gt;
bit 1 = Over border (1 = yes)(Back to 0 after a reset)&lt;br /&gt;
bit 0 = Sprites visible (1 = visible)(Back to 0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Bit 0 must be set for sprites to be visible.&lt;br /&gt;
&lt;br /&gt;
Bit 1 set allows sprites to be visible in the border area. When this bit is reset, sprites will not display outside the 256×192 area of the ULA display.&lt;br /&gt;
&lt;br /&gt;
Bit 5 set enables clipping when sprites are visible in the border area. If reset, no clipping is applied and sprites will be visible in the full 320×256 space.&lt;br /&gt;
&lt;br /&gt;
The sprite module draws sprites in the order 0-127 in each scanline. Bit 6 determines whether sprite 0 is topmost or sprite 127 is topmost.&lt;br /&gt;
&lt;br /&gt;
Bits 4:2 determine layer priority and how sprites overlay or are obscured by other layers.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x19 (25) =&amp;gt; Clip Window Sprites&lt;br /&gt;
bits 7-0 = Cood. of the clip window&lt;br /&gt;
1st write – X1 position&lt;br /&gt;
2nd write – X2 position&lt;br /&gt;
3rd write – Y1 position&lt;br /&gt;
4rd write – Y2 position&lt;br /&gt;
The values are 0,255,0,191 after a Reset&lt;br /&gt;
Reads do not advance the clip position&lt;br /&gt;
When the clip window is enabled for sprites in “over border” mode,&lt;br /&gt;
the X coords are internally doubled and the clip window origin is&lt;br /&gt;
moved to the sprite origin inside the border.&lt;br /&gt;
&lt;br /&gt;
Sprites will only be visible inside the clipping window. When not in over-border mode (bit 1 of nextreg 0x15) the clipping window is given in ULA screen coordinates with 0,0 correspoding to the top left corner of the ULA screen. In over-border mode, the clipping window’s origin is moved to the sprite coordinate origin 32 pixels to the left and 32 pixels above the ULA screen origin.&lt;br /&gt;
&lt;br /&gt;
Regardless, sprite position is always in sprite coordinates with 32,32 corresponding to the top left corner of the ULA screen.&lt;br /&gt;
&lt;br /&gt;
(W) 0x1C (28) =&amp;gt; Clip Window control&lt;br /&gt;
bits 7-4 = Reserved, must be 0&lt;br /&gt;
bit 3 – reset the tilemap clip index&lt;br /&gt;
bit 2 – reset the ULA/LoRes clip index.&lt;br /&gt;
bit 1 – reset the sprite clip index.&lt;br /&gt;
bit 0 – reset the Layer 2 clip index.&lt;br /&gt;
&lt;br /&gt;
Can be used to reset nextreg 0x19.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x43 (67) =&amp;gt; Palette Control&lt;br /&gt;
bit 7 = ‘1’ to disable palette write auto-increment.&lt;br /&gt;
bits 6-4 = Select palette for reading or writing:&lt;br /&gt;
000 = ULA first palette&lt;br /&gt;
100 = ULA second palette&lt;br /&gt;
001 = Layer 2 first palette&lt;br /&gt;
101 = Layer 2 second palette&lt;br /&gt;
010 = Sprites first palette&lt;br /&gt;
110 = Sprites second palette&lt;br /&gt;
011 = Tilemap first palette&lt;br /&gt;
111 = Tilemap second palette&lt;br /&gt;
bit 3 = Select Sprites palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 2 = Select Layer 2 palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 1 = Select ULA palette (0 = first palette, 1 = second palette)&lt;br /&gt;
bit 0 = Enabe ULANext mode if 1. (0 after a reset)&lt;br /&gt;
&lt;br /&gt;
Sprites have two associated palettes which can be selected in this nextreg.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x40 (64) =&amp;gt; Palette Index&lt;br /&gt;
bits 7-0 = Select the palette index to change the associated colour.&lt;br /&gt;
For the ULA only, INKs are mapped to indices 0-7, Bright INKS to indices 8-15,&lt;br /&gt;
PAPERs to indices 16-23 and Bright PAPERs to indices 24-31.&lt;br /&gt;
In ULANext mode, INKs come from a subset of indices 0-127 and PAPERs come from&lt;br /&gt;
a subset of indices 128-255. The number of active indices depends on the number&lt;br /&gt;
of attribute bits assigned to INK and PAPER out of the attribute byte.&lt;br /&gt;
The ULA always takes border colour from paper.&lt;br /&gt;
&lt;br /&gt;
Select the starting palette index if writing the sprite palette.&lt;br /&gt;
&lt;br /&gt;
Palette values can be written in either 8-bit or 9-bit form:&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x41 (65) =&amp;gt; Palette Value (8 bit colour)&lt;br /&gt;
bits 7-0 = Colour for the palette index selected by the register 0x40.&lt;br /&gt;
(Format is RRRGGGBB – the lower blue bit of the 9-bit colour will&lt;br /&gt;
be a logical OR of blue bits 1 and 0 of this 8-bit value.)&lt;br /&gt;
After the write, the palette index is auto-incremented to the next index&lt;br /&gt;
if the auto-increment is enabled at reg 0x43. Reads do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x44 (68) =&amp;gt; Palette Value (9 bit colour)&lt;br /&gt;
Two consecutive writes are needed to write the 9 bit colour&lt;br /&gt;
1st write:&lt;br /&gt;
bits 7-0 = RRRGGGBB&lt;br /&gt;
2nd write.&lt;br /&gt;
If writing a L2 palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bit 7 = 1 for L2 priority colour, 0 for normal&lt;br /&gt;
Priority colour will always be on top even on an SLU priori-&lt;br /&gt;
ty arrangement. If you need the exact same colour on priori-&lt;br /&gt;
ty and non priority locations you will need to program the&lt;br /&gt;
same colour twice changing bit 7 to 0 for the second colour&lt;br /&gt;
bits 6-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
If writing another palette&lt;br /&gt;
———————————————————————–&lt;br /&gt;
bits 7-1 = Reserved, must be 0&lt;br /&gt;
bit 0 = lsb B&lt;br /&gt;
&lt;br /&gt;
After the two consecutives writes the palette index is&lt;br /&gt;
auto-incremented if the auto-increment is enabled by reg 0x43.&lt;br /&gt;
&lt;br /&gt;
Reads only return the 2nd byte and do not auto-increment.&lt;br /&gt;
&lt;br /&gt;
(R/W) 0x4B (75) =&amp;gt; Transparency index for sprites&lt;br /&gt;
bits 7-0 = Set the index value (0xE3 after reset)&lt;br /&gt;
For 4-bit sprites only the bottom 4-bits are relevant.&lt;br /&gt;
&lt;br /&gt;
Determines the transparent colour index used for sprites.&lt;/div&gt;</summary>
		<author><name>Intrepidis</name></author>
	</entry>
</feed>