Guest User

SNES regs.txt

a guest
May 20th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 106.37 KB | None | 0 0
  1. =============================================================================
  2. Anomie's Register Doc
  3. $Revision: 1157 $
  4. $Date: 2007-07-12 16:39:41 -0400 (Thu, 12 Jul 2007) $
  5. =============================================================================
  6.  
  7.  
  8. REGISTERS
  9. =========
  10.  
  11. Addr rw?fvha Name
  12. bits
  13.  
  14. Explanation
  15.  
  16. "Addr" is the address this register is mapped into the SNES memory space.
  17. "Name" is the official and unofficial name of the register
  18. "bits" is either 8 or 16 characters explicating the bitfields in this register.
  19.  
  20. The flags are:
  21. rw?fvha
  22. ||||||+--> '+' if it can be read/written at any time, '-' otherwise
  23. |||||+---> '+' if it can be read/written during H-Blank
  24. ||||+----> '+' if it can be read/written during V-Blank
  25. |||+-----> '+' if it can be read/written during force-blank
  26. ||+------> Read/Write style: 'b' => byte
  27. || 'h'/'l' => read/write high/low byte of a word
  28. || 'w' => word read/write twice low then high
  29. |+-------> 'w' if the register is writable for an effect
  30. +--------> 'r' if the register is readable for a value or effect (i.e. not
  31. open bus).
  32.  
  33. To find the entry for a particular register, search for the register number
  34. (i.e. '2100') at the very beginning of the line. Note that the DMA registers
  35. are combined, so e.g. to find $4300, $4310, $4320, $4330, $4340, $4350, $4360,
  36. or $4370 you'd search for '43x0'.
  37.  
  38. For most registers (and most undefined bits of readable registers), the
  39. returned value is Open Bus, that is the last value read over the main bus from
  40. the ROM (typically part of the opcode arguments or the indirect base address).
  41.  
  42. Registers matching $21x4-6 or $21x8-A (where x is 0-2) return the last value
  43. read from any of the PPU1 registers $2134-6, $2138-A, or $213E. This is known
  44. as PPU1 Open Bus. Similarly, PPU2 Open Bus involves reading registers $213B-D
  45. or $213F (NOT $21xB-D though).
  46.  
  47. Note that it may be possible to write registers anytime even if marked '-', but
  48. until we have proof '-' is a better guess.
  49.  
  50. --------
  51.  
  52. 2100 wb++++ INIDISP - Screen Display
  53. x---bbbb
  54.  
  55. x = Force blank on when set.
  56. bbbb = Screen brightness, F=max, 0="off".
  57.  
  58. Note that force blank CAN be disabled mid-scanline. However, this can
  59. result in glitched graphics on that scanline, as the internal rendering
  60. buffers will not have been updated during force blank. Current theory
  61. is that BGs will be glitched for a few tiles (depending on how far in
  62. advance the PPU operates), and OBJ will be glitched for the entire
  63. scanline.
  64.  
  65. Also, writing this register on the first line of V-Blank (225 or 240,
  66. depending on overscan) when force blank is currently active causes the
  67. OAM Address Reset to occur.
  68.  
  69.  
  70. 2101 wb++?- OBSEL - Object Size and Chr Address
  71. sssnnbbb
  72.  
  73. sss = Object size:
  74. 000 = 8x8 and 16x16 sprites
  75. 001 = 8x8 and 32x32 sprites
  76. 010 = 8x8 and 64x64 sprites
  77. 011 = 16x16 and 32x32 sprites
  78. 100 = 16x16 and 64x64 sprites
  79. 101 = 32x32 and 64x64 sprites
  80. 110 = 16x32 and 32x64 sprites ('undocumented')
  81. 111 = 16x32 and 32x32 sprites ('undocumented')
  82.  
  83. nn = Name Select
  84. bbb = Name Base Select (Addr>>14)
  85. See the section "SPRITES" below for details.
  86.  
  87.  
  88. 2102 wl++?- OAMADDL - OAM Address low byte
  89. 2103 wh++?- OAMADDH - OAM Address high bit and Obj Priority
  90. p------b aaaaaaaa
  91.  
  92. p = Obj Priority activation bit
  93. When this bit is set, an Obj other than Sprite 0 may be given
  94. priority. See the section "SPRITES" below for details.
  95.  
  96. b aaaaaaaa = OAM address
  97. This can be thought of in two ways, depending on your conception of
  98. OAM. If you consider OAM as a 544-byte table, baaaaaaaa is the word
  99. address into that table. If you consider OAM to be a 512-byte table
  100. and a 32-byte table, b is the table selector and aaaaaaaa is the
  101. word address in the table. See the section "SPRITES" below for
  102. details.
  103.  
  104. The internal OAM address is invalidated when scanlines are being
  105. rendered. This invalidation is deterministic, but we do not know how
  106. it is determined. Thus, the last value written to these registers is
  107. reloaded into the internal OAM address at the beginning of V-Blank if
  108. that occurs outside of a force-blank period. This is known as 'OAM
  109. reset'. 'OAM reset' also occurs on certain writes to $2100.
  110.  
  111. Writing to either $2102 or $2103 resets the entire internal OAM Address
  112. to the values last written to this register. E.g., if you set $104 to
  113. this register, write 4 bytes, then write $1 to $2103, the internal OAM
  114. address will point to word 4, not word 6.
  115.  
  116.  
  117. 2104 wb++-- OAMDATA - Data for OAM write
  118. dddddddd
  119.  
  120. Note that OAM writes are done in an odd manner, in particular
  121. the low table of OAM is not affected until the high byte of a
  122. word is written (however, the high table is affected
  123. immediately). Thus, if you set the address, then alternate writes and
  124. reads, OAM will never be affected until you reach the high table!
  125.  
  126. Similarly, if you set the address to 0, then write 1, 2, read, then
  127. write 3, OAM will end up as "01 02 01 03", rather than "01 02 xx 03" as
  128. you might expect.
  129.  
  130. Technically, this register CAN be written during H-blank (and probably
  131. mid-scanline as well). However, due to OAM address invalidation the
  132. actual OAM byte written will probably not be what you expect. Note that
  133. writing during force-blank will only work as expected if that
  134. force-blank was begun during V-Blank, or (probably) if $2102/3 have
  135. been reset during that force-blank period.
  136.  
  137. See the section "SPRITES" below for details.
  138.  
  139.  
  140. 2105 wb+++- BGMODE - BG Mode and Character Size
  141. DCBAemmm
  142.  
  143. A/B/C/D = BG character size for BG1/BG2/BG3/BG4
  144. If the bit is set, then the BG is made of 16x16 tiles. Otherwise,
  145. 8x8 tiles are used. However, note that Modes 5 and 6 always use
  146. 16-pixel wide tiles, and Mode 7 always uses 8x8 tiles. See the
  147. section "BACKGROUNDS" below for details.
  148.  
  149. mmm = BG Mode
  150. e = Mode 1 BG3 priority bit
  151. Mode BG depth OPT Priorities
  152. 1 2 3 4 Front -> Back
  153. -=-------=-=-=-=----=---============---
  154. 0 2 2 2 2 n 3AB2ab1CD0cd
  155. 1 4 4 2 n 3AB2ab1C 0c
  156. * if e set: C3AB2ab1 0c
  157. 2 4 4 y 3A 2B 1a 0b
  158. 3 8 4 n 3A 2B 1a 0b
  159. 4 8 2 y 3A 2B 1a 0b
  160. 5 4 2 n 3A 2B 1a 0b
  161. 6 4 y 3A 2 1a 0
  162. 7 8 n 3 2 1a 0
  163. 7+EXTBG 8 7 n 3 2B 1a 0b
  164.  
  165. "OPT" means "Offset-per-tile mode". For the priorities, numbers
  166. mean sprites with that priority. Letters correspond to BGs (A=1,
  167. B=2, etc), with upper/lower case indicating tile priority 1/0. See
  168. the section "BACKGROUNDS" below for details.
  169.  
  170. Mode 7's EXTBG mode allows you to enable BG2, which uses the same
  171. tilemap and character data as BG1 but interprets bit 7 of the pixel
  172. data as a priority bit. BG2 also has some oddness to do with some
  173. of the per-BG registers below. See the Mode 7 section under
  174. BACKGROUNDS for details.
  175.  
  176.  
  177. 2106 wb+++- MOSAIC - Screen Pixelation
  178. xxxxDCBA
  179.  
  180. A/B/C/D = Affect BG1/BG2/BG3/BG4
  181.  
  182. xxxx = pixel size, 0=1x1, F=16x16
  183. The mosaic filter goes over the BG and covers each x-by-x square
  184. with the upper-left pixel of that square, with the top of the first
  185. row of squares on the 'starting scanline'. If this register is set
  186. during the frame, the 'starting scanline' is the current scanline,
  187. otherwise it is the first visible scanline of the frame. I.e. if
  188. even scanlines are completely red and odd scanlines are completely
  189. blue, setting the xxxx=1 mid-frame will make the rest of the screen
  190. either completely red or completely blue depending on whether you
  191. set xxxx on an even or an odd scanline.
  192.  
  193. XXX: It seems that writing the same value to this register does not
  194. reset the 'starting scanline', but which changes do reset it?
  195.  
  196. Note that mosaic is applied after scrolling, but before any clip
  197. windows, color windows, or math. So the XxX block can be partially
  198. clipped, and it can be mathed as normal with a non-mosaiced BG. But
  199. scrolling can't make it partially one color and partially another.
  200.  
  201. Modes 5-6 should 'double' the expansion factor to expand
  202. half-pixels. This actually makes xxxx=0 have a visible effect,
  203. since the even half-pixels (usually on the subscreen) hide the odd
  204. half-pixels. The same thing happens vertically with interlace mode.
  205.  
  206. Mode 7, of course, is weird. BG1 mosaics about like normal, as long
  207. as you remember that the Mode 7 transformations have no effect on
  208. the XxX blocks. BG2 uses bit A to control 'vertical mosaic' and bit
  209. B to control 'horizontal mosaic', so you could be expanding over
  210. 1xX, Xx1, or XxX blocks. This can get really interesting as BG1
  211. still uses bit A as normal, so you could have the BG1 pixels
  212. expanded XxX with high-priority BG2 pixels expanded 1xX on top of
  213. them.
  214.  
  215. See the section "BACKGROUNDS" below for details.
  216.  
  217.  
  218. 2107 wb++?- BG1SC - BG1 Tilemap Address and Size
  219. 2108 wb++?- BG2SC - BG2 Tilemap Address and Size
  220. 2109 wb++?- BG3SC - BG3 Tilemap Address and Size
  221. 210a wb++?- BG4SC - BG4 Tilemap Address and Size
  222. aaaaaayx
  223.  
  224. aaaaaa = Tilemap address in VRAM (Addr>>10)
  225. x = Tilemap horizontal mirroring
  226. y = Tilemap veritcal mirroring
  227. All tilemaps are 32x32 tiles. If x and y are both unset, there is
  228. one tilemap at Addr. If x is set, a second tilemap follows the
  229. first that should be considered "to the right of" the first. If y
  230. is set, a second tilemap follows the first that should be
  231. considered "below" the first. If both are set, then a second
  232. follows "to the right", then a third "below", and a fourth "below
  233. and to the right".
  234.  
  235. See the section "BACKGROUNDS" below for more details.
  236.  
  237.  
  238. 210b wb++?- BG12NBA - BG1 and 2 Chr Address
  239. 210c wb++?- BG34NBA - BG3 and 4 Chr Address
  240. bbbbaaaa
  241.  
  242. aaaa = Base address for BG1/3 (Addr>>13)
  243. bbbb = Base address for BG2/4 (Addr>>13)
  244. See the section "BACKGROUNDS" below for details.
  245.  
  246.  
  247. 210d ww+++- BG1HOFS - BG1 Horizontal Scroll
  248. ww+++- M7HOFS - Mode 7 BG Horizontal Scroll
  249. 210e ww+++- BG1VOFS - BG1 Vertical Scroll
  250. ww+++- M7VOFS - Mode 7 BG Vertical Scroll
  251. ------xx xxxxxxxx
  252. ---mmmmm mmmmmmmm
  253.  
  254. x = The BG offset, 10 bits.
  255. m = The Mode 7 BG offset, 13 bits two's-complement signed.
  256.  
  257. These are actually two registers in one (or would that be "4 registers
  258. in 2"?). Anyway, writing $210d will write both BG1HOFS which works
  259. exactly like the rest of the BGnxOFS registers below ($210f-$2114), and
  260. M7HOFS which works with the M7* registers ($211b-$2120) instead.
  261.  
  262. Modes 0-6 use BG1xOFS and ignore M7xOFS, while Mode 7 uses M7xOFS and
  263. ignores BG1HOFS. See the appropriate sections below for details, and
  264. note the different formulas for BG1HOFS versus M7HOFS.
  265.  
  266.  
  267. 210f ww+++- BG2HOFS - BG2 Horizontal Scroll
  268. 2110 ww+++- BG2VOFS - BG2 Vertical Scroll
  269. 2111 ww+++- BG3HOFS - BG3 Horizontal Scroll
  270. 2112 ww+++- BG3VOFS - BG3 Vertical Scroll
  271. 2113 ww+++- BG4HOFS - BG4 Horizontal Scroll
  272. 2114 ww+++- BG4VOFS - BG4 Vertical Scroll
  273. ------xx xxxxxxxx
  274.  
  275. Note that these are "write twice" registers, first the low byte is
  276. written then the high. Current theory is that writes to the register
  277. work like this:
  278. BGnHOFS = (Current<<8) | (Prev&~7) | ((Reg>>8)&7);
  279. Prev = Current;
  280. or
  281. BGnVOFS = (Current<<8) | Prev;
  282. Prev = Current;
  283.  
  284. Note that there is only one Prev shared by all the BGnxOFS registers.
  285. This is NOT shared with the M7* registers (not even M7xOFS and
  286. BG1xOFS).
  287.  
  288. x = The BG offset, at most 10 bits (some modes effectively use as few
  289. as 8).
  290.  
  291. Note that all BGs wrap if you try to go past their edges. Thus, the
  292. maximum offset value in BG Modes 0-6 is 1023, since you have at most 64
  293. tiles (if x/y of BGnSC is set) of 16 pixels each (if the appropriate
  294. bit of BGMODE is set).
  295.  
  296. Horizontal scrolling scrolls in units of full pixels no matter if we're
  297. rendering a 256-pixel wide screen or a 512-half-pixel wide screen.
  298. However, vertical scrolling will move in half-line increments if
  299. interlace mode is active.
  300.  
  301. See the section "BACKGROUNDS" below for details.
  302.  
  303.  
  304. 2115 wb++?- VMAIN - Video Port Control
  305. i---mmii
  306.  
  307. i = Address increment mode:
  308. 0 => increment after writing $2118/reading $2139
  309. 1 => increment after writing $2119/reading $213a
  310. Note that a word write stores low first, then high. Thus, if you're
  311. storing a word value to $2118/9, you'll probably want to set 1
  312. here.
  313.  
  314. ii = Address increment amount
  315. 00 = Normal increment by 1
  316. 01 = Increment by 32
  317. 10 = Increment by 128
  318. 11 = Increment by 128
  319.  
  320. mm = Address remapping
  321. 00 = No remapping
  322. 01 = Remap addressing aaaaaaaaBBBccccc => aaaaaaaacccccBBB
  323. 10 = Remap addressing aaaaaaaBBBcccccc => aaaaaaaccccccBBB
  324. 11 = Remap addressing aaaaaaBBBccccccc => aaaaaacccccccBBB
  325.  
  326. The "remap" modes basically implement address translation. If
  327. $2116/7 are set to #$0003, then word address #$0018 will be written
  328. instead, and $2116/7 will be incremented to $0004.
  329.  
  330.  
  331. 2116 wl++?- VMADDL - VRAM Address low byte
  332. 2117 wh++?- VMADDH - VRAM Address high byte
  333. aaaaaaaa aaaaaaaa
  334.  
  335. This sets the address for $2118/9 and $2139/a. Note that this is a word
  336. address, not a byte address!
  337.  
  338. See the sections "BACKGROUNDS" and "SPRITES" below for details.
  339.  
  340.  
  341. 2118 wl++-- VMDATAL - VRAM Data Write low byte
  342. 2119 wh++-- VMDATAH - VRAM Data Write high byte
  343. xxxxxxxx xxxxxxxx
  344.  
  345. This writes data to VRAM. The writes take effect immediately(?), even
  346. if no increment is performed. The address is incremented when one of
  347. the two bytes is written; which one depends on the setting of bit 7 of
  348. register $2115. Keep in mind the address translation bits of $2115 as
  349. well.
  350.  
  351. The interaction between these registers and $2139/a is unknown.
  352.  
  353. See the sections "BACKGROUNDS" and "SPRITES" below for details.
  354.  
  355.  
  356. 211a wb++?- M7SEL - Mode 7 Settings
  357. rc----yx
  358.  
  359. r = Playing field size: When clear, the playing field is 1024x1024
  360. pixels (so the tilemap completely fills it). When set, the playing
  361. field is much larger, and the 'empty space' fill is controlled by
  362. bit 6.
  363.  
  364. c = Empty space fill, when bit 7 is set:
  365. 0 = Transparent.
  366. 1 = Fill with character 0. Note that the fill is matrix
  367. transformed like all other Mode 7 tiles.
  368.  
  369. x/y = Horizontal/Veritcal mirroring. If the bit is set, flip the
  370. 256x256 pixel 'screen' in that direction.
  371.  
  372. See the section "BACKGROUNDS" below for details.
  373.  
  374.  
  375. 211b ww+++- M7A - Mode 7 Matrix A (also used with $2134/6)
  376. 211c ww+++- M7B - Mode 7 Matrix B (also used with $2134/6)
  377. 211d ww+++- M7C - Mode 7 Matrix C
  378. 211e ww+++- M7D - Mode 7 Matrix D
  379. aaaaaaaa aaaaaaaa
  380.  
  381. Note that these are "write twice" registers, first the low byte is
  382. written then the high. Current theory is that writes to the register
  383. work like this:
  384. Reg = (Current<<8) | Prev;
  385. Prev = Current;
  386.  
  387. Note that there is only one Prev shared by all these registers. This
  388. Prev is NOT shared with the BGnxOFS registers, but it IS shared with
  389. the M7xOFS registers.
  390.  
  391. These set the matrix parameters for Mode 7. The values are an 8-bit
  392. fixed point, i.e. the value should be divided by 256.0 when used in
  393. calculations. See below for more explanation.
  394.  
  395. The product A*(B>>8) may be read from registers $2134/6. There is
  396. supposedly no important delay. It may not be operative during Mode 7
  397. rendering.
  398.  
  399. See the section "BACKGROUNDS" below for details.
  400.  
  401.  
  402. 211f ww+++- M7X - Mode 7 Center X
  403. 2120 ww+++- M7Y - Mode 7 Center Y
  404. ---xxxxx xxxxxxxx
  405.  
  406. Note that these are "write twice" registers, like the other M7*
  407. registers. See above for the write semantics. The value is 13 bit
  408. two's-complement signed.
  409.  
  410. The matrix transformation formula is:
  411.  
  412. [ X ] [ A B ] [ SX + M7HOFS - CX ] [ CX ]
  413. [ ] = [ ] * [ ] + [ ]
  414. [ Y ] [ C D ] [ SY + M7VOFS - CY ] [ CY ]
  415.  
  416. Note: SX/SY are screen coordinates. X/Y are coordinates in the playing
  417. field from which the pixel is taken. If $211a bit 7 is clear, the
  418. result is then restricted to 0<=X<=1023 and 0<=Y<=1023. If $211a bits 6
  419. and 7 are both set and X or Y is less than 0 or greater than 1023, use
  420. the low 3 bits of each to choose the pixel from character 0.
  421.  
  422. The bit-accurate formula seems to be something along the lines of:
  423. #define CLIP(a) (((a)&0x2000)?((a)|~0x3ff):((a)&0x3ff))
  424.  
  425. X[0,y] = ((A*CLIP(HOFS-CX))&~63)
  426. + ((B*y)&~63) + ((B*CLIP(VOFS-CY))&~63)
  427. + (CX<<8)
  428. Y[0,y] = ((C*CLIP(HOFS-CX))&~63)
  429. + ((D*y)&~63) + ((D*CLIP(VOFS-CY))&~63)
  430. + (CY<<8)
  431.  
  432. X[x,y] = X[x-1,y] + A
  433. Y[x,y] = Y[x-1,y] + C
  434.  
  435. (In all cases, X[] and Y[] are fixed point with 8 bits of fraction)
  436.  
  437. See the section "BACKGROUNDS" below for details.
  438.  
  439.  
  440. 2121 wb+++- CGADD - CGRAM Address
  441. cccccccc
  442.  
  443. This sets the word address (i.e. color) which will be affected by $2122
  444. and $213b.
  445.  
  446.  
  447. 2122 ww+++- CGDATA - CGRAM Data write
  448. -bbbbbgg gggrrrrr
  449.  
  450. This writes to CGRAM, effectively setting the palette colors.
  451.  
  452. Accesses to CGRAM are handled just like accesses to the low table of
  453. OAM, see $2104 for details.
  454.  
  455. Note that the color values are stored in BGR order.
  456.  
  457.  
  458. 2123 wb+++- W12SEL - Window Mask Settings for BG1 and BG2
  459. 2124 wb+++- W34SEL - Window Mask Settings for BG3 and BG4
  460. 2125 wb+++- WOBJSEL - Window Mask Settings for OBJ and Color Window
  461. ABCDabcd
  462.  
  463. c = Enable window 1 for BG1/BG3/OBJ
  464. a = Enable window 2 for BG1/BG3/OBJ
  465. C/A = Enable window 1/2 for BG2/BG4/Color
  466. When the bit is set, the corresponding window will affect the
  467. corresponding background (subject to the settings of $212e/f).
  468.  
  469. d = Window 1 Inversion for BG1/BG3/OBJ
  470. b = Window 2 Inversion for BG1/BG3/OBJ
  471. D/B = Window 1/2 Inversion for BG2/BG4/Color
  472. When the bit is set, "W" should be replaced by "~W" (not-W) in the
  473. window combination formulae below.
  474.  
  475. See the section "WINDOWS" below for more details.
  476.  
  477.  
  478. 2126 wb+++- WH0 - Window 1 Left Position
  479. 2127 wb+++- WH1 - Window 1 Right Position
  480. 2128 wb+++- WH2 - Window 2 Left Position
  481. 2129 wb+++- WH3 - Window 2 Right Position
  482. xxxxxxxx
  483.  
  484. These set the offset of the appropriate edge of the appropriate window.
  485. Note that if the left edge is greater than the right edge, the window
  486. is considered to have no range at all (and thus "W" always is false).
  487. See the section "WINDOWS" below for more details.
  488.  
  489.  
  490. 212a wb+++- WBGLOG - Window mask logic for BGs
  491. 44332211
  492. 212b wb+++- WOBJLOG - Window mask logic for OBJs and Color Window
  493. ----ccoo
  494.  
  495. 44/33/22/11/oo/cc = Mask logic for BG1/BG2/BG3/BG4/OBJ/Color
  496. This specified the window combination method, using standard
  497. boolean operators:
  498. 00 = OR
  499. 01 = AND
  500. 10 = XOR
  501. 11 = XNOR
  502.  
  503. Consider two variables, W1 and W2, which are true for pixels
  504. between the appropriate left and right bounds as set in
  505. $2126-$2129 and false otherwise. Then, you have the following
  506. possibilities: (replace "W#" with "~W#", depending on the Inversion
  507. settings of $2123-$2125)
  508. Neither window enabled => nothing masked.
  509. One window enabled => Either W1 or W2, as appropriate.
  510. Both windows enabled => W1 op W2, where "op" is as above.
  511. Where the function is true, the BG will be masked.
  512.  
  513. See the section "WINDOWS" below for more details.
  514.  
  515.  
  516. 212c wb+++- TM - Main Screen Designation
  517. 212d wb+++- TS - Subscreen Designation
  518. ---o4321
  519.  
  520. 1/2/3/4/o = Enable BG1/BG2/BG3/BG4/OBJ for display
  521. on the main (or sub) screen.
  522.  
  523. See the section "BACKGROUNDS" below for details.
  524.  
  525.  
  526. 212e wb+++- TMW - Window Mask Designation for the Main Screen
  527. 212f wb+++- TSW - Window Mask Designation for the Subscreen
  528. ---o4321
  529.  
  530. 1/2/3/4/o = Enable window masking for BG1/BG2/BG3/BG4/OBJ on the
  531. main (or sub) screen.
  532.  
  533. See the section "BACKGROUNDS" below for details.
  534.  
  535.  
  536. 2130 wb+++- CGWSEL - Color Addition Select
  537. ccmm--sd
  538.  
  539. cc = Clip colors to black before math
  540. 00 => Never
  541. 01 => Outside Color Window only
  542. 10 => Inside Color Window only
  543. 11 => Always
  544.  
  545. mm = Prevent color math
  546. 00 => Never
  547. 01 => Outside Color Window only
  548. 10 => Inside Color Window only
  549. 11 => Always
  550.  
  551. s = Add subscreen (instead of fixed color)
  552.  
  553. d = Direct color mode for 256-color BGs
  554.  
  555. See the sections "BACKGROUNDS", "WINDOWS", and "RENDERING THE
  556. SCREEN" below for details.
  557.  
  558.  
  559. 2131 wb+++- CGADSUB - Color math designation
  560. shbo4321
  561.  
  562. s = Add/subtract select
  563. 0 => Add the colors
  564. 1 => Subtract the colors
  565.  
  566. h = Half color math. When set, the result of the color math is
  567. divided by 2 (except when $2130 bit 1 is set and the fixed color is
  568. used, or when color is cliped).
  569.  
  570. 4/3/2/1/o/b = Enable color math on BG1/BG2/BG3/BG4/OBJ/Backdrop
  571.  
  572. See the sections "BACKGROUNDS", "WINDOWS", and "RENDERING THE
  573. SCREEN" below for details.
  574.  
  575.  
  576. 2132 wb+++- COLDATA - Fixed Color Data
  577. bgrccccc
  578.  
  579. b/g/r = Which color plane(s) to set the intensity for.
  580. ccccc = Color intensity.
  581.  
  582. So basically, to set an orange you'd do something along the lines of:
  583. LDA #$3f
  584. STA $2132
  585. LDA #$4f
  586. STA $2132
  587. LDA #$80
  588. STA $2132
  589.  
  590. See the sections "BACKGROUNDS" and "WINDOWS" below for details.
  591.  
  592.  
  593. 2133 wb+++- SETINI - Screen Mode/Video Select
  594. se--poIi
  595.  
  596. s = "External Sync". Used for superimposing "sfx" graphics, whatever
  597. that means. Usually 0. Not much is known about this bit.
  598. Interestingly, the SPPU1 chip has a pin named "EXTSYNC" (or
  599. not-EXTSYNC, since it has a bar over it) which is tied to Vcc.
  600.  
  601. e = Mode 7 EXTBG ("Extra BG"). When this bit is set, you may enable
  602. BG2 on Mode 7. BG2 uses the same tile and character data as BG1,
  603. but interprets the high bit of the color data as a priority for the
  604. pixel.
  605.  
  606. Various sources report additional effects for this bit, possibly
  607. related to bit 7. For example, "Enable the Data Supplied From the
  608. External Lsi.", whatever that means. Of course, maybe that's a
  609. typo and it's supposed to apply to bit 7 instead.
  610.  
  611. p = Enable pseudo-hires mode. This creates a 512-pixel horizontal
  612. resolution by taking pixels from the subscreen for the
  613. even-numbered pixels (zero based) and from the main screen for the
  614. odd-numbered pixels. Color math behaves just as with Mode 5/6
  615. hires. The interlace bit still has no effect. Mosaic operates as
  616. normal (not like Mode 5/6). The 'subscreen' pixel is clipped (by
  617. windows) when the main-screen pixel to the LEFT is clipped, not
  618. when the one to the RIGHT is clipped as you'd expect. What happens
  619. with pixel column 0 is unknown.
  620.  
  621. Enabling this bit in Modes 5 or 6 has no effect.
  622.  
  623. o = Overscan mode. When set, 239 lines will be displayed instead of
  624. the normal 224. This also means V-Blank will occur that
  625. much later, and be shorter. All that happens is that extra lines
  626. get added to the display, and it seems the TV will like to move
  627. the display up 8 pixels. See below for more details.
  628.  
  629. I = OBJ Interlace. When set regardless of BG mode, the OBJ will be
  630. interlaced (see bit 0 below), and thus will appear half-height.
  631.  
  632. Note that this only controls whether obj are drawn as normal or
  633. not; the interlace signal is only output to the TV based on bit 0
  634. below.
  635.  
  636. i = Screen interlace. When set in BG mode 5 (and probably 6), the
  637. effective screen height will be 448 (or 478) pixles, rather than
  638. 224 (or 239). When set in any other mode, the screen will just get
  639. a bit jumpy. However, toggling the tilemap each field would
  640. simulate the increased screen height (much like pseudo-hires
  641. simulares hires).
  642.  
  643. In hardware, setting this bit makes the SNES output a normal
  644. interlace signal rather than always forcing one frame.
  645.  
  646. See the sections "BACKGROUNDS" and "SPRITES" below for details.
  647.  
  648. Overscan: The bit only matters at the very end of the frame, if you
  649. change the setting on line 0xE0 before the normal NMI trigger point
  650. then it's the same as if you had it on all frame. Note that this
  651. affects both the NMI trigger point and when HDMA stops for the
  652. frame.
  653.  
  654. If you turn the bit off at the very beginning of scanline X (for
  655. 0xE1<=X<=0xF0), NMI will occur on line X and the last HDMA transfer
  656. will occur on line X-1. However, on my TV at least, the display will
  657. remain in the normal no-overscan position for lines E1-EC, it will
  658. move up only one pixel for line ED, and it will lose vertical sync
  659. for lines EF-F4!
  660.  
  661. Turning the bit on, only line E1 gives any effect: NMI will occur on
  662. line E2, although the last HDMA will still occur on line E0.
  663. Anything else acts like you left the bit off the whole time. Note,
  664. however, that if you wait too long after the beginning of the
  665. scanline then you will get no effect.
  666.  
  667. Even if there is no visible effect, the overscan setting still
  668. affects VRAM writes. In particular, executing "LDA #'-' / STA $2118
  669. / LDA r2133 / STA $2133 / LDA #'+' / STA $2118" during the E1-F0
  670. period will write only + or only - to VRAM, depending on whether the
  671. overscan bit was set to 0 or 1.
  672.  
  673.  
  674. 2134 r l+++? MPYL - Multiplication Result low byte
  675. 2135 r m+++? MPYM - Multiplication Result middle byte
  676. 2136 r h+++? MPYH - Multiplication Result high byte
  677. xxxxxxxx xxxxxxxx xxxxxxxx
  678.  
  679. This is the 2's compliment product of the 16-bit value written to $211b
  680. and the 8-bit value most recently written to $211c. There is supposedly
  681. no important delay. It may not be operative during Mode 7 rendering.
  682.  
  683.  
  684. 2137 b++++ SLHV - Software Latch for H/V Counter
  685. --------
  686.  
  687. When read, the H/V counter (as read from $213c and $213d) will be
  688. latched to the current X and Y position if bit 7 of $4201 is set. The
  689. data actually read is open bus.
  690.  
  691.  
  692. 2138 r w++?- OAMDATAREAD* - Data for OAM read
  693. xxxxxxxx
  694.  
  695. OAM reads are straightforward: the current byte as set in $2102/3 and
  696. incremented by reads from this register and writes to $2104 will be
  697. returned. Note that writes to the lower table are not affected so
  698. logically. See register $2104 and the section "SPRITES" below for
  699. details.
  700.  
  701. Also, note that OAM address invalidation probably affects the address
  702. read by this register as well.
  703.  
  704.  
  705. 2139 r l++?- VMDATALREAD* - VRAM Data Read low byte
  706. 213a r h++?- VMDATAHREAD* - VRAM Data Read high byte
  707. xxxxxxxx xxxxxxxx
  708.  
  709. Simply, this reads data from VRAM. The address is incremented when
  710. either $2139 or $213a is read, depending on the setting of bit 7 of
  711. $2115.
  712.  
  713. Actually, the reading is more complex. When either of these registers
  714. is read, the appropriate byte from a word-sized buffer is returned. A
  715. word from VRAM is loaded into this buffer just *before* the VRAM
  716. address is incremented. The actual data read and the amount of the
  717. increment depend on the low 4 bits of $2115. The effect of this is
  718. that a 'dummy read' is required after setting $2116-7 before you start
  719. getting the actual data.
  720.  
  721. The interaction between these registers and $2118/9 is unknown.
  722.  
  723. See the sections "BACKGROUNDS" and "SPRITES" below for details.
  724.  
  725.  
  726. 213b r w++?- CGDATAREAD* - CGRAM Data read
  727. -bbbbbgg gggrrrrr
  728.  
  729. This reads from CGRAM.
  730.  
  731. Accesses to CGRAM are handled just like accesses to the low table of
  732. OAM, see $2138 for details.
  733.  
  734. Note that the color values are stored in BGR order. The '-' bit is PPU2
  735. Open Bus.
  736.  
  737.  
  738. 213c r w++++ OPHCT - Horizontal Scanline Location
  739. 213d r w++++ OPVCT - Vertical Scanline Location
  740. -------x xxxxxxxx
  741.  
  742. These values are latched by reading $2137 when bit 7 of $4201 is set,
  743. or by clearing-and-setting bit 7 of $4201 either by writing $4201 or by
  744. pin 6 of Controller Port 2 (the latch occurs on the 1->0 transition).
  745.  
  746. Note that the value read is only 9 bits: bits 1-7 of the high byte are
  747. PPU2 Open Bus. Each register keeps seperate track of whether to
  748. return the low or high byte. The high/low selector is reset to 'low'
  749. when $213f is read (the selector is NOT reset when the counter is
  750. latched).
  751.  
  752. H Counter values range from 0 to 339, with 22-277 being visible on the
  753. screen. V Counter values range from 0 to 261 in NTSC mode (262 is
  754. possible every other frame when interlace is active) and 0 to 311 in
  755. PAL mode (312 in interlace?), with 1-224 (or 1-239(?) if overscan is
  756. enabled) visible on the screen.
  757.  
  758.  
  759. 213e r b++++ STAT77 - PPU Status Flag and Version
  760. trm-vvvv
  761.  
  762. t = Time Over Flag. If more than 34 sprite-tiles (e.g. a 16x16
  763. sprite has 2 sprite-tiles) were encountered on a single line, this
  764. flag will be set. The flag is reset at the end of V-Blank. See the
  765. section "SPRITES" below for details.
  766.  
  767. r = Range Over Flag. If more than 32 sprites were encountered on a
  768. single line, this flag will be set. The flag is reset at the end of
  769. V-Blank. See the section "SPRITES" below for details.
  770.  
  771. Note that the above two flags are set whether or not OBJ are
  772. actually enabled at the time.
  773.  
  774. m = "Master/slave mode select". Little is known about this bit.
  775. Current theory is that it indicates the status of the "MASTER" pin
  776. on the S-PPU1 chip, which in the normal SNES is always Gnd. We
  777. always seem to read back 0 here.
  778.  
  779. vvvv = 5c77 chip version number. So far, we've only encountered version
  780. 1.
  781.  
  782. The '-' bit is PPU1 Open Bus.
  783.  
  784.  
  785. 213f r b++++ STAT78 - PPU Status Flag and Version
  786. fl-pvvvv
  787.  
  788. f = Interlace Field. This will toggle every V-Blank.
  789.  
  790. l = External latch flag. When the PPU counters are latched, this
  791. flag gets set. The flag is reset on read, but only when $4201 bit 7
  792. is set.
  793.  
  794. p = NTSC/Pal Mode. If this is a PAL SNES, this bit will be set,
  795. otherwise it will be clear.
  796.  
  797. vvvv = 5C78 chip version number. So far, we've encountered at least 2
  798. and 3. Possibly 1 as well.
  799.  
  800. The '-' bit is PPU2 Open Bus.
  801.  
  802. Note: as a side effect of reading this register, the high/low byte
  803. selector for $213c/d is reset to 'low'.
  804.  
  805.  
  806. 2140 rwb++++ APUIO0 - APU I/O register 0
  807. 2141 rwb++++ APUIO1 - APU I/O register 1
  808. 2142 rwb++++ APUIO2 - APU I/O register 2
  809. 2143 rwb++++ APUIO3 - APU I/O register 3
  810. xxxxxxxx
  811.  
  812. These registers are used in communication with the SPC700. Note that
  813. the value written here is not the value read back. Rather, the value
  814. written shows up in the SPC700's registers $f4-7, and the values
  815. written to those registers by the SPC700 are what you read here.
  816.  
  817. If the SPC700 writes the register during a read, the value read will
  818. be the logical OR of the old and new values. The exact cycles during
  819. which the 'read' actually occurs is not known, although a good guess
  820. would be some portion of the final 3 master cycles of the 6-cycle
  821. memory access.
  822.  
  823. Note that these registers are mirrored throughout the range
  824. $2140-$217f.
  825.  
  826.  
  827. 2180 rwb++++ WMDATA - WRAM Data read/write
  828. xxxxxxxx
  829.  
  830. This register reads to or writes from the WRAM address set in $2181-3.
  831. The address is then incremented. The effect of mixed reads and writes
  832. is unknown, but it is suspected that they are handled logically.
  833.  
  834. Note that attempting a DMA from WRAM to this register will not work,
  835. WRAM will not be written. Attempting a DMA from this register to
  836. WRAM will similarly not work, the value written is (initially) the Open
  837. Bus value. In either case, the address in $2181-3 is not incremented.
  838.  
  839.  
  840. 2181 wl++++ WMADDL - WRAM Address low byte
  841. 2182 wm++++ WMADDM - WRAM Address middle byte
  842. 2183 wh++++ WMADDH - WRAM Address high bit
  843. -------x xxxxxxxx xxxxxxxx
  844.  
  845. This is the address that will be read or written by accesses to $2180.
  846. Note that WRAM is also mapped in the SNES memory space from $7E:0000 to
  847. $7F:FFFF, and from $0000 to $1FFF in banks $00 through $3F and $80
  848. through $BF.
  849.  
  850. Verious docs indicate that these registers may be read as well as
  851. written. However, they are wrong. These registers are open bus.
  852.  
  853. DMA from WRAM to these registers has no effect. Otherwise, however, DMA
  854. writes them as normal. This means you could use DMA mode 4 to $2180 and
  855. a table in ROM to write any sequence of RAM addresses.
  856.  
  857. The value does not wrap at page boundaries on increment.
  858.  
  859.  
  860. 4016 rwb++++ JOYSER0 - NES-style Joypad Access Port 1
  861. Rd: ------ca
  862. Wr: -------l
  863. 4017 r?b++++ JOYSER1 - NES-style Joypad Access Port 2
  864. ---111db
  865.  
  866. These registers basically have a direct connection to the controller
  867. ports on the front of the SNES.
  868.  
  869. l = Writing this bit controlls the Latch line of both controller
  870. ports. When 1 is set, the Latch goes high (or is it low? At any
  871. rate, whichever one makes the pads latch their state). When
  872. cleared, the Latch goes the other way.
  873.  
  874. a/b = These bits return the state of the Data1 line.
  875. c/d = These bits return the state of the Data2 line.
  876. Reading $4016 drives the Clock line of Controller Port 1 low.
  877. The SNES then reads the Data1 and Data2 lines, and Clock is set
  878. back to high. $4017 does the same for Port 2.
  879.  
  880. Note the 1-bits in $4017: the CPU chip has pins for these bits, but
  881. these pins are tied to Gnd and thus always 1.
  882.  
  883. Data for normal joypads is returned in the order: B, Y, Select,
  884. Start, Up, Down, Left, Right, A, X, L, R, 0, 0, 0, 0, then ones
  885. until latched again.
  886.  
  887. Note that Auto-Joypad Read (see register $4200) will effectively write
  888. 1 then 0 to bit 'l', then read 16 times from both $4016 and $4017. The
  889. 'a' bits will end up in $4218/9, with the first bit read (e.g. the B
  890. button) in bit 15 of the word. Similarly, the 'b' bits end up in
  891. $421a/b, the 'c' bits in $42c/d, and the 'd' bits in $421e/f. Any
  892. further bits the device may return may be read from $4016/$4017 as
  893. normal.
  894.  
  895. The effect of reading these during auto-joypad read is unknown.
  896.  
  897. See the section "CONTROLLERS" below for details.
  898.  
  899.  
  900. 4200 wb+++? NMITIMEN - Interrupt Enable Flags
  901. n-yx---a
  902.  
  903. n = Enable NMI. If clear, NMI will not occur. If set, NMI will fire
  904. just after the start of V-Blank.
  905.  
  906. NMI fires shortly after the V Counter reaches $E1 (or presumably
  907. $F0 if overscan is enabled, see register $2133).
  908.  
  909. x/y = IRQ enable.
  910. 0/0 => No IRQ will occur
  911. 0/1 => An IRQ will occur sometime just after the V Counter reaches
  912. the value set in $4209/a.
  913. 1/0 => An IRQ will occur sometime just after the H Counter reaches
  914. the value set in $4207/8.
  915. 1/1 => An IRQ will occur sometime just after the H Counter reaches
  916. the value set in $4207/8 when V Counter equals the value set
  917. in $4209/a.
  918.  
  919. a = Auto-Joypad Read Enable. When set, the registers $4218-$421f
  920. will be updated at about V Counter = $E3 (or presumably $F2).
  921.  
  922. Some games try to read this register. However, they work only because
  923. open bus behavior gives them values they expect.
  924.  
  925. This register is initialized to $00 on power on or reset.
  926.  
  927.  
  928. 4201 wb++++ WRIO - Programmable I/O port (out-port)
  929. abxxxxxx
  930.  
  931. This is basically just an 8-bit I/O Port. 'b' is connected to pin 6 of
  932. Controller Port 1. 'a' is connected to pin 6 of Controller Port 2, and
  933. to the PPU Latch line. Thus, writing a 0 then a 1 to bit 'a' will latch
  934. the H and V Counters much like reading $2137 (the latch happens on the
  935. transition to 0). When bit 'a' is 0, no latching can occur.
  936.  
  937. Any other effects of this register are unknown. See $4213 for the
  938. I half of the I/O Port.
  939.  
  940. Note that the IO Port is initialized as if this register were written
  941. with all 1-bits at power up, unchanged on reset(?).
  942.  
  943.  
  944. 4202 wb++++ WRMPYA - Multiplicand A
  945. 4203 wb++++ WRMPYB - Multiplicand B
  946. mmmmmmmm
  947.  
  948. Write $4202, then $4203. 8 "machine cycles" (probably 48 master cycles)
  949. after $4203 is set, the product may be read from $4216/7. $4202 will
  950. not be altered by this process, thus a new value may be written to
  951. $4203 to perform another multiplication without resetting $4202.
  952.  
  953. The multiplication is unsigned.
  954.  
  955. $4202 holds the value $ff on power on and is unchanged on reset.
  956.  
  957.  
  958. 4204 wl++++ WRDIVL - Dividend C low byte
  959. 4205 wh++++ WRDIVH - Dividend C high byte
  960. dddddddd dddddddd
  961. 4206 wb++++ WRDIVB - Divisor B
  962. bbbbbbbb
  963.  
  964. Write $4204/5, then $4206. 16 "machine cycles" (probably 96 master
  965. cycles) after $4206 is set, the quotient may be read from $4214/5, and
  966. the remainder from $4216/7. Presumably, $4204/5 are not altered by this
  967. process, much like $4202.
  968.  
  969. The division is unsigned. Division by 0 gives a quotient of $FFFF and a
  970. remainder of C.
  971.  
  972. WRDIV holds the value $ffff on power on and is unchanged on reset.
  973.  
  974.  
  975. 4207 wl++++ HTIMEL - H Timer low byte
  976. 4208 wh++++ HTIMEH - H Timer high byte
  977. -------h hhhhhhhh
  978.  
  979. If bit 4 of $4200 is set and bit 5 is clear, an IRQ will fire every
  980. scanline when the H Counter reaches the value set here. If bits 4 and 5
  981. are both set, the IRQ will fire only when the V Counter equals the
  982. value set in $4209/a.
  983.  
  984. Note that the H Counter ranges from 0 to 339, thus greater values will
  985. result in no IRQ firing.
  986.  
  987. HTIME is initialized to $1ff on power on, unchanged on reset.
  988.  
  989.  
  990. 4209 wl++++ VTIMEL - V Timer low byte
  991. 420a wh++++ VTIMEH - V Timer high byte
  992. -------v vvvvvvvv
  993.  
  994. If bit 5 of $4200 is set and bit 4 is clear, an IRQ will fire just
  995. after the V Counter reaches the value set here. If bits 4 and 5 are
  996. both set, the IRQ will fire instead when the V Counter equals the value
  997. set here and the H Counter reaches the value set in $4207/8.
  998.  
  999. Note that the V Counter ranges from 0 to 261 in NTSC mode (262 is
  1000. possible every other frame whan interlace is active) and 0 to 311 in
  1001. PAL mode (312 in interlace?), thus greater values will result in no IRQ
  1002. firing.
  1003.  
  1004. VTIME is initialized to $1ff on power on, unchanged on reset.
  1005.  
  1006.  
  1007. 420b wb++++ MDMAEN - DMA Enable
  1008. 76543210
  1009.  
  1010. 7/6/5/4/3/2/1/0 = Enable the selected DMA channels. The CPU will be
  1011. paused until all DMAs complete. DMAs will be executed in order from
  1012. 0 to 7 (?).
  1013.  
  1014. See registers $43x0-$43xA for more details.
  1015.  
  1016. If HDMA (init or transfer) occurs while a DMA is in progress, the DMA
  1017. will be paused for the duration. If the HDMA happens to involve the
  1018. current DMA channel, the DMA will be immediately terminated and the
  1019. HDMA will progress using the then-current values of the registers.
  1020. Other DMA channels will be unaffected.
  1021.  
  1022. This register is initialized to $00 on power on or reset.
  1023.  
  1024. See the section "DMA AND HDMA" below for more information.
  1025.  
  1026.  
  1027. 420c wb++++ HDMAEN - HDMA Enable
  1028. 76543210
  1029.  
  1030. 7/6/5/4/3/2/1/0 = Enable the selected HDMA channels. HDMAs will be
  1031. executed in order from 0 to 7 (?).
  1032.  
  1033. See registers $43x0-$43xA for more details.
  1034.  
  1035. If HDMA (init or transfer) occurs while a DMA is in progress, the DMA
  1036. will be paused for the duration. If the HDMA happens to involve the
  1037. current DMA channel, the DMA will be immediately terminated and the
  1038. HDMA will progress using the then-current values of the registers.
  1039. Other DMA channels will be unaffected.
  1040.  
  1041. Note that enabling a channel mid-frame will begin HDMA at the next HDMA
  1042. point. However, the HDMA register initialization only occurs before the
  1043. HDMA point on scanline 0, so those registers will have to be
  1044. initialized by hand before enabling HDMA. A channel that has already
  1045. terminated for the frame cannot be restarted in this manner.
  1046.  
  1047. Writing 0 to a bit will pause an ongoing HDMA; the transfer may be
  1048. continued by writing 1 to the bit.
  1049.  
  1050. This register is initialized to $00 on power on or reset.
  1051.  
  1052. See the section "DMA AND HDMA" below for more information.
  1053.  
  1054.  
  1055. 420d wb++++ MEMSEL - ROM Access Speed
  1056. -------f
  1057.  
  1058. f = FastROM select. The SNES uses a master clock running at
  1059. about 21.477 MHz (current theory is 1.89e9/88 Hz). By default, the
  1060. SNES takes 8 master cycles for each ROM access. If this bit is set
  1061. and ROM is accessed via banks $80-$FF, only 6 master cycles will be
  1062. used.
  1063.  
  1064. This register is initialized to $00 on power on (or reset?).
  1065.  
  1066. See my memory map and timing doc (memmap.txt) for more details.
  1067.  
  1068.  
  1069. 4210 r b++++ RDNMI - NMI Flag and 5A22 Version
  1070. n---vvvv
  1071.  
  1072. n = NMI Flag. This bit is set at the start of V-Blank (at the
  1073. moment, we suspect when H-Counter is somewhere between $28 and
  1074. $4E), and cleared on read or at the end of V-Blank. Supposedly, it
  1075. is required that this register be read during NMI.
  1076.  
  1077. Note that this bit is not affected by bit 7 of $4200.
  1078.  
  1079. vvvv = 5A22 chip version number. So far, we've encountered at least 2,
  1080. maybe 1 as well.
  1081.  
  1082. NMI is cleared on power on or reset.
  1083.  
  1084. The '-' bits are open bus.
  1085.  
  1086.  
  1087. 4211 r b++++ TIMEUP - IRQ Flag
  1088. i-------
  1089.  
  1090. i = IRQ Flag. This bit is set just after an IRQ fires (at the
  1091. moment, it seems to have the same delay as the NMI Flag of $4210
  1092. has following NMI), and is cleared on read or write. Supposedly, it
  1093. is required that this register be read during the IRQ handler. If
  1094. this really is the case, then I suspect that that read is what
  1095. actually clears the CPU's IRQ line.
  1096.  
  1097. This register is marked read/write in another doc, with no explanation.
  1098.  
  1099. IRQ is cleared on power on or reset.
  1100.  
  1101. The '-' bits are open bus.
  1102.  
  1103.  
  1104. 4212 r b++++ HVBJOY - PPU Status
  1105. vh-----a
  1106.  
  1107. v = V-Blank Flag. If we're currently in V-Blank, this flag is set,
  1108. otherwise it is clear. The setting seems to occur at H Counter
  1109. about $16-$17 when V Counter is $E1, and the clearing at about $1E
  1110. with V Counter 0.
  1111.  
  1112. h = H-Blank Flag. If we're currently in H-Blank, this flag is set,
  1113. otherwise it is clear. The setting seems to occur at H Counter
  1114. about $121-$122, and the clearing at about $12-$18.
  1115.  
  1116. a = Auto-Joypad Status. This is set while Auto-Joypad Read is in
  1117. progress, and cleared when complete. It typically turns on at
  1118. the start of V-Blank, and completes 3 scanlines later.
  1119.  
  1120. This register is marked read/write in another doc, with no explanation.
  1121.  
  1122.  
  1123. 4213 r b++++ RDIO - Programmable I/O port (in-port)
  1124. abxxxxxx
  1125.  
  1126. Reading this register reads data from the I/O Port. The way the
  1127. I/O Port works, any bit set to 0 in $4201 will be 0 here. Any bit
  1128. set to 1 in $4201 may be 1 or 0 here, depending on whether any other
  1129. device connected to the I/O Port has set a 0 to that bit.
  1130.  
  1131. Bit 'b' is connected to pin 6 of Controller Port 1. Bit 'a' is
  1132. connected to pin 6 of Controller Port 2, and to the PPU Latch line.
  1133.  
  1134. See register $4201 for the O side of the I/O Port.
  1135.  
  1136.  
  1137. 4214 r l++++ RDDIVL - Quotient of Divide Result low byte
  1138. 4215 r h++++ RDDIVH - Quotient of Divide Result high byte
  1139. qqqqqqqq qqqqqqqq
  1140.  
  1141. Write $4204/5, then $4206. 16 "machine cycles" (probably 96 master
  1142. cycles) after $4206 is set, the quotient may be read from these
  1143. registers, and the remainder from $4216/7.
  1144.  
  1145. The division is unsigned.
  1146.  
  1147.  
  1148. 4216 r l++++ RDMPYL - Multiplication Product or Divide Remainder low byte
  1149. 4217 r h++++ RDMPYH - Multiplication Product or Divide Remainder high byte
  1150. xxxxxxxx xxxxxxxx
  1151.  
  1152. Write $4202, then $4203. 8 "machine cycles" (probably 48 master cycles)
  1153. after $4203 is set, the product may be read from these registers.
  1154.  
  1155. Write $4204/5, then $4206. 16 "machine cycles" (probably 96 master
  1156. cycles) after $4206 is set, the quotient may be read from $4214/5, and
  1157. the remainder from these registers.
  1158.  
  1159. The multiplication and division are both unsigned.
  1160.  
  1161.  
  1162. 4218 r l++++ JOY1L - Controller Port 1 Data1 Register low byte
  1163. 4219 r h++++ JOY1H - Controller Port 1 Data1 Register high byte
  1164. 421a r l++++ JOY2L - Controller Port 2 Data1 Register low byte
  1165. 421b r h++++ JOY2H - Controller Port 2 Data1 Register high byte
  1166. 421c r l++++ JOY3L - Controller Port 1 Data2 Register low byte
  1167. 421d r h++++ JOY3H - Controller Port 1 Data2 Register high byte
  1168. 421e r l++++ JOY4L - Controller Port 2 Data2 Register low byte
  1169. 421f r h++++ JOY4H - Controller Port 2 Data2 Register high byte
  1170. byetUDLR axlr0000
  1171.  
  1172. The bitmap above only applies for joypads, obviously. More
  1173. generically, Auto Joypad Read effectively sets 1 then 0 to $4016,
  1174. then reads $4016/7 16 times to get the bits for these registers.
  1175.  
  1176. a/b/x/y/l/r/e/t = A/B/X/Y/L/R/Select/Start button status.
  1177.  
  1178. U/D/L/R = Up/Down/Left/Right control pad status. Note that only one of
  1179. L/R and only one of U/D may be set, due to the pad hardware.
  1180.  
  1181. These registers are only updated when the Auto-Joypad Read bit (bit 0)
  1182. of $4200 is set. They are being updated while the Auto-Joypad Status
  1183. bit (bit 0) of $4212 is set. Reading during this time will return
  1184. incorrect values.
  1185.  
  1186. See the section "CONTROLLERS" below for details.
  1187.  
  1188.  
  1189. 43x0 rwb++++ DMAPx - DMA Control for Channel x (x=0-7)
  1190. da-ifttt
  1191.  
  1192. d = Transfer Direction. When clear, data will be read from the CPU
  1193. memory and written to the PPU register. When set, vice versa.
  1194.  
  1195. Contrary to previous belief, this bit DOES affect HDMA! Indirect
  1196. mode is more useful, it will read the table as normal and write
  1197. from Bus B to the Bus A address specified. Direct mode will work as
  1198. expected though, it will read counts from the table and try to
  1199. write the data values into the table.
  1200.  
  1201. a = HDMA Addressing Mode. When clear, the HDMA table contains the
  1202. data to transfar. When set, the HDMA table contains pointers to the
  1203. data. This bit does not affect DMA.
  1204.  
  1205. i = DMA Address Increment. When clear, the DMA address will be
  1206. incremented for each byte. When set, the DMA address will be
  1207. decremented. This bit does not affect HDMA.
  1208.  
  1209. f = DMA Fixed Transfer. When set, the DMA address will not be
  1210. adjusted. When clear, the address will be adjusted as specified by
  1211. bit 4. This bit does not affect HDMA.
  1212.  
  1213. ttt = Transfer Mode.
  1214. 000 => 1 register write once (1 byte: p )
  1215. 001 => 2 registers write once (2 bytes: p, p+1 )
  1216. 010 => 1 register write twice (2 bytes: p, p )
  1217. 011 => 2 registers write twice each (4 bytes: p, p, p+1, p+1)
  1218. 100 => 4 registers write once (4 bytes: p, p+1, p+2, p+3)
  1219. 101 => 2 registers write twice alternate (4 bytes: p, p+1, p, p+1)
  1220. 110 => 1 register write twice (2 bytes: p, p )
  1221. 111 => 2 registers write twice each (4 bytes: p, p, p+1, p+1)
  1222.  
  1223. The effect of writing this register during HDMA to the associated
  1224. channel is unknown. Most likely, the change takes effect for the
  1225. next HDMA transfer.
  1226.  
  1227. This register is set to $ff on power on, and is unchanged on reset.
  1228.  
  1229. See the section "DMA AND HDMA" below for more information.
  1230.  
  1231.  
  1232. 43x1 rwb++++ BBADx - DMA Destination Register for Channel x (x=0-7)
  1233. pppppppp
  1234.  
  1235. This specifies the Bus B address to access. Considering the standard
  1236. CPU memory space, this specifies which address $00:2100-$00:21ff to
  1237. access, with two- and four-register modes wrapping $21ff->$2100, not
  1238. $2200.
  1239.  
  1240. The effect of writing this register during HDMA to the associated
  1241. channel is unknown. Most likely, the change takes effect for the
  1242. next transfer.
  1243.  
  1244. This register is set to $ff on power on, and is unchanged on reset.
  1245.  
  1246. See the section "DMA AND HDMA" below for more information.
  1247.  
  1248.  
  1249. 43x2 rwl++++ A1TxL - DMA Source Address for Channel x low byte (x=0-7)
  1250. 43x3 rwh++++ A1TxH - DMA Source Address for Channel x high byte (x=0-7)
  1251. 43x4 rwb++++ A1Bx - DMA Source Address for Channel x bank byte (x=0-7)
  1252. bbbbbbbb hhhhhhhh llllllll
  1253.  
  1254. This specifies the starting Address Bus A address for the DMA transfer,
  1255. or the beginning of the HDMA table for HDMA transfers. Note that Bus A
  1256. does not access the Bus B registers, so pointing this address at say
  1257. $00:2100 results in open bus.
  1258.  
  1259. The effect of writing this register during HDMA to the associated
  1260. channel is unknown. However, current theory is that only $43x4 will
  1261. affect the transfer. The changes will take effect at the next HDMA
  1262. init.
  1263.  
  1264. During DMA, $43x2/3 will be incremented or decremented as specified by
  1265. $43x0. However $43x4 will NOT be adjusted. These registers will not be
  1266. affected by HDMA.
  1267.  
  1268. This register is set to $ff on power on, and is unchanged on reset.
  1269.  
  1270. See the section "DMA AND HDMA" below for more information.
  1271.  
  1272.  
  1273. 43x5 rwl++++ DASxL - DMA Size/HDMA Indirect Address low byte (x=0-7)
  1274. 43x6 rwh++++ DASxH - DMA Size/HDMA Indirect Address high byte (x=0-7)
  1275. 43x7 rwb++++ DASBx - HDMA Indirect Address bank byte (x=0-7)
  1276. bbbbbbbb hhhhhhhh llllllll
  1277.  
  1278. For DMA, $43x5/6 indicate the number of bytes to transfer. Note that
  1279. this is a strict limit: if this is set to 1 then only 1 byte will be
  1280. written, even if the transfer mode specifies 2 or 4 registers (and if
  1281. this is 5, all 4 registers would be written once, then the first only
  1282. would be written a second time). Note, however, that writing $0000 to
  1283. this register actually results in a transfer of $10000 bytes, not 0.
  1284.  
  1285. $43x5/6 are decremented during DMA, and thus typically end up set to 0
  1286. when DMA is complete.
  1287.  
  1288. For HDMA, $43x7 specifies the bank for indirect addressing mode. The
  1289. indirect address is copied into $43x5/6 and incremented appropriately.
  1290. For direct HDMA, these registers are not used or altered.
  1291.  
  1292. Writes to $43x7 during indirect HDMA will take effect for the next
  1293. transfer. Writes to $43x5/6 during indirect HDMA will also take effect
  1294. for the next HDMA transfer, however this is only noticable during
  1295. repeat mode (for normal mode, a new indirect address will be read from
  1296. the table before the transfer). For a direct transfer, presumably
  1297. nothing will happen.
  1298.  
  1299. This register is set to $ff on power on, and is unchanged on reset.
  1300.  
  1301. See the section "DMA AND HDMA" below for more information.
  1302.  
  1303.  
  1304. 43x8 rwl++++ A2AxL - HDMA Table Address low byte (x=0-7)
  1305. 43x9 rwh++++ A2AxH - HDMA Table Address high byte (x=0-7)
  1306. aaaaaaaa aaaaaaaa
  1307.  
  1308. At the beginning of the frame $43x2/3 are copied into this register for
  1309. all active HDMA channels, and then this register is updated as the
  1310. table is read. Thus, if a game wishes to start HDMA mid-frame (or
  1311. change tables mid-frame), this register must be written. Writing this
  1312. register mid-frame changes the table address for the next scanline.
  1313.  
  1314. This register is not used for DMA.
  1315.  
  1316. This register is set to $ff on power on, and is unchanged on reset.
  1317.  
  1318. See the section "DMA AND HDMA" below for more information.
  1319.  
  1320.  
  1321. 43xa rwb++++ NLTRx - HDMA Line Counter (x=0-7)
  1322. rccccccc
  1323.  
  1324. r = Repeat Select. When set, the HDMA transfer will be performed
  1325. every line, rather than only when this register is loaded from the
  1326. table. However, this byte (and the indirect HDMA address) will only
  1327. be reloaded from the table when the counter reaches 0.
  1328.  
  1329. ccccccc = Line count. This is decremented every scanline. When it
  1330. reaches 0, a byte is read from the HDMA table into this register
  1331. (and the indirect HDMA address is read into $43x5/6 if applicable).
  1332.  
  1333. One oddity: the register is decremeted before being checked for r
  1334. status or c==0. Thus, setting a value of $80 is really "128 lines with
  1335. no repeat" rather than "0 lines with repeat". Similarly, a value of $00
  1336. will be "128 lines with repeat" when it doesn't mean "terminate the
  1337. channel".
  1338.  
  1339. This register is initialized at the end of V-Blank for every active
  1340. HDMA channel. Note that if a game wishes to begin HDMA during the
  1341. frame, it will most likely have to initalize this register. Writing
  1342. this mid-transfer will similarly change the count and repeat to take
  1343. effect next scanline. Remember though that 'repeat' won't take effect
  1344. until after the next transfer period.
  1345.  
  1346. This register is set to $ff on power on, and is unchanged on reset.
  1347.  
  1348. See the section "DMA AND HDMA" below for more information.
  1349.  
  1350. 43xb rwb++++ ????x - Unknown (x=0-7)
  1351. 43xf rwb++++ ????x - Unknown (x=0-7)
  1352. ????????
  1353.  
  1354. The effects of these registers (if any) are unknown. $43xf and $43xb
  1355. are really aliases for the same register.
  1356.  
  1357. This register is set to $ff on power on, and is unchanged on reset.
  1358.  
  1359.  
  1360.  
  1361. SPRITES
  1362. =======
  1363.  
  1364. The SNES has 128 independant sprites. The sprite definitions are stored in
  1365. Object Attribute Memory, or OAM.
  1366.  
  1367. OAM
  1368. ---
  1369.  
  1370. OAM consists of 544 bytes, organized into a low table of 512 bytes and a high
  1371. table of 32 bytes. Both tables are made up of 128 records. OAM is accessed by
  1372. setting the word address in register $2102, the "table select" in bit 0 of
  1373. $2103, then writing to $2104 or reading from $2138. Since the high table is
  1374. only 32 bytes long, only the low 4 bits of $2102 are significant for indexing
  1375. this table.
  1376.  
  1377. The internal OAM address is invalidated during the rendering of a scanline;
  1378. this invalidation is deterministic, but we do not know how or when the value is
  1379. determined. Current theory is that it is invalidated more-or-less continuously
  1380. and has something to do with the current OAM address and possibly which sprites
  1381. are on the current scanline. The internal OAM address is reloaded from $2102/3
  1382. at the beginning of V-Blank, if this occurs outside of a force-blank period.
  1383. The reload also occurs on a 1->0 transition of $2100.7.
  1384.  
  1385. Each read/write increments the address by one byte (the internal address has 10
  1386. bits, with bit 9 selecting the table and bits 0-8 indexing). Reads simply read
  1387. the current byte. Writes to the low table go into a word-sized buffer, which is
  1388. written to the appropriate word of OAM when the high byte of the word is
  1389. written. Thus, if alternating reads and writes occur such that the high byte of
  1390. the word is always read instead of written, none of the writes will actually
  1391. affect OAM. If the alternation happens such that the writes always occur to the
  1392. high byte, not only the high bytes but whatever garbage is left in the low byte
  1393. will be written as well!
  1394.  
  1395. Pictorally: Start OAM filled with all zeros. Write 1, read, read, Write 2,
  1396. read, write 3 => OAM is 00 00 01 02 01 03, rather than 01 00 00 02 00 03 as
  1397. you might expect.
  1398.  
  1399. Writes to the high table, on the other hand, work exactly as expected.
  1400.  
  1401. The record format for the low table is 4 bytes:
  1402. byte OBJ*4+0: xxxxxxxx
  1403. byte OBJ*4+1: yyyyyyyy
  1404. byte OBJ*4+2: cccccccc
  1405. byte OBJ*4+3: vhoopppN
  1406.  
  1407. The record format for the high table is 2 bits:
  1408. bit 0/2/4/6 of byte OBJ/4: X
  1409. bit 1/3/5/7 of byte OBJ/4: s
  1410.  
  1411. The values are:
  1412. Xxxxxxxxx = X position of the sprite. Basically, consider this signed but see
  1413. below.
  1414. yyyyyyyy = Y position of the sprite. Values 0-239 are on-screen. -63 through
  1415. -1 are "off the top", so the bottom part of the sprite comes in at the
  1416. top of the screen. Note that this implies a really big sprite can go off
  1417. the bottom and come back in the top.
  1418. cccccccc = First tile of the sprite. See below for the calculation of the
  1419. VRAM address. Note that this could also be considered as 'rrrrcccc'
  1420. specifying the row and column of the tile in the 16x16 character table.
  1421. N = Name table of the sprite. See below for the calculation of the
  1422. VRAM address.
  1423. ppp = Palette of the sprite. The first palette index is 128+ppp*16.
  1424. oo = Sprite priority. See below for details.
  1425. h/v = Horizontal/Veritcal flip flags. Note this flips the whole sprite,
  1426. not just the individual tiles. However, the rectangular sprites are
  1427. flipped vertically as if they were two square sprites (i.e. rows
  1428. "01234567" flip to "32107654", not "76543210").
  1429. s = Sprite size flag. See below for details.
  1430.  
  1431. The sprite size is controlled by bits 5-7 of $2101, and the Size bit of OAM.
  1432. $2101 determines the two possible sizes for all sprites. If the OAM Size flag
  1433. is 0, the sprite uses the smaller size, otherwise it uses the larger size.
  1434.  
  1435.  
  1436. Palettes
  1437. --------
  1438.  
  1439. There are 8 16-color palettes available to sprites, starting at CGRAM index
  1440. 128. Thus, the palette number 'ppp' in OAM indicates that colors 128+ppp*16
  1441. through 128+ppp*16+15 are available to this sprite. However, the first of these
  1442. is always considered transparent, to allow for non-rectangular shaped sprites.
  1443.  
  1444. Only sprites with palettes 4-7 participate in color math.
  1445.  
  1446.  
  1447. Character table in VRAM
  1448. -----------------------
  1449.  
  1450. Sprites have two 16x16 tile character tables in VRAM. Wrapping on these works
  1451. much like for BG tilemaps: tile 0 is to the right of tile $0F and below tile
  1452. $F0, tile $10 is below tile 0 and to the right of tile $1F, tile $FF is to the
  1453. left of tile $F0 and above tile $0F, and so on. Which character table a sprite
  1454. uses is determined by the N bit in OAM. So if you specify Tile=$ff, your 16x16
  1455. sprite is made of tiles $ff, $f0, $0f, and $00.
  1456.  
  1457. The first table is at the address specified by the Name Base bits of $2101, and
  1458. the offset of the second is determined by the Name bits of $2101. The word
  1459. address in VRAM of a sprite's first tile may be calculated as:
  1460. ((Base<<13) + (cccccccc<<4) + (N ? ((Name+1)<<12) : 0)) & 0x7fff
  1461.  
  1462. See the section "BACKGROUNDS" below for details on the format of the character
  1463. data.
  1464.  
  1465.  
  1466. Sprite Priority
  1467. ---------------
  1468.  
  1469. There are two 'priority' concepts applicalbe to sprites. First, there are
  1470. the priority bits in OAM, which control the priority of the sprites relative
  1471. to the BGs. See the section "BACKGROUNDS" for more details on this.
  1472.  
  1473. The second is the priority with relation to the other sprites. This is
  1474. completely controlled by the sprite's index and the priority rotation
  1475. setting.
  1476.  
  1477. Priority rotation is set by bit 7 of $2103. If the bit is unset, Sprite 0 is
  1478. always the first sprite. Otherwise, take the current internal OAM word
  1479. address (not affected by OAM Address Invalidation) and give priority to the
  1480. sprite number (OAMAddr&0xFE)>>1. So if you set $2102/3 to $104, then write 4
  1481. bytes, sprite 3 will have priority for the next frame. However, OAM Address
  1482. Reset will reset the internal OAM address to word $104, so sprite 2 will
  1483. have priority for subsequent frames.
  1484.  
  1485. There is one major oddity: if you set $2102/3=A, then write 4n+2*(A&1)+1
  1486. bytes (e.g. so the next byte written would go to the last byte in the 4-byte
  1487. sprite record), sprite ((OAMAddr>>1)+Y)&0x7F has priority (where Y is the
  1488. current line as addressed by sprites). Thus, if you put all 128 8x8 sprites
  1489. at Y=63, write $8000 to $2102/3, then read 3 bytes from $2138, you will see
  1490. sprites 63-70 having priority on successive scanlines.
  1491.  
  1492. FirstSprite ends up on top of all other sprites, regardless of the priority
  1493. bits in OAM. FirstSprite+1 is on top of FirstSprite+2 is on top of
  1494. FirstSprite+3 and so on until FirstSprite+127 (wrapping of course from sprite
  1495. 127 to sprite 0). Note that only the priority of the topmost sprite is
  1496. considered relative to the backgrounds. Thus, if FirstSprite+3 and
  1497. FirstSprite+4 are identical except FirstSprite+3 has priority 0 and
  1498. FirstSprite+4 has priority 3, they will both be hidden by any backgrounds that
  1499. hide priority 0 sprites. This may seem counterintuitive, since FirstSprite+4
  1500. would normally go in front of these BGs, but many games depend on this
  1501. behavior.
  1502.  
  1503.  
  1504. Drawing the Sprites
  1505. -------------------
  1506.  
  1507. As with everything else on the SNES, sprites are drawn per-scanline. The
  1508. process is basically as follows:
  1509.  
  1510. 0) If any OBJ is at X=256 (or X=-256, same difference), consider it as being
  1511. at X=0 when considering Range and Time. Note that this doesn't mean you
  1512. actually draw it at X=0.
  1513.  
  1514. 1) Range: Starting with the FirstSprite, determine the first 32 sprites on
  1515. this scanline. Only those sprites with -size < X < 256 are considered in
  1516. Range. If there are more than 32 sprites on the scanline, set bit 6 of
  1517. register $213e.
  1518.  
  1519. 2) Time: Starting with the last sprite in Range, load up to 34 8x8 tiles (from
  1520. left-to-right, after flipping). If there are more than 34 tiles in Range,
  1521. set bit 7 of $213e. Only those tiles with -8 < X < 256 are counted.
  1522.  
  1523. 3) Associate with each tile in Range and Time its true X position (256/-256
  1524. should not be set to 0), palette, and priority for drawing.
  1525.  
  1526. See the section "RENDERING THE SCREEN" below for details.
  1527.  
  1528.  
  1529. BACKGROUNDS
  1530. ===========
  1531.  
  1532. BG Modes
  1533. --------
  1534.  
  1535. The SNES has 7 background modes, two of which have major variations. The modes
  1536. are selected by bits 0-2 of register $2105. The variation of Mode 1 is selected
  1537. by bit 3 of $2105, and the variation of Mode 7 is selected by bit 6 of $2133.
  1538.  
  1539. Mode # Colors for BG
  1540. 1 2 3 4
  1541. ======---=---=---=---=
  1542. 0 4 4 4 4
  1543. 1 16 16 4 -
  1544. 2 16 16 - -
  1545. 3 256 16 - -
  1546. 4 256 4 - -
  1547. 5 16 4 - -
  1548. 6 16 - - -
  1549. 7 256 - - -
  1550. 7EXTBG 256 128 - -
  1551.  
  1552. In all modes and for all BGs, color 0 in any palette is considered transparent.
  1553.  
  1554.  
  1555. Tile Maps and Character Maps
  1556. ----------------------------
  1557.  
  1558. Each BG has two regions of VRAM associated with it: one for the tilemap, and
  1559. one for the character data.
  1560.  
  1561. The tilemap address is selected by bits 2-7 of registers $2107-a, and the
  1562. tilemap size is selected by bits 0-1 of that same register. All tilemaps are
  1563. 32x32, bits 0-1 simply select the number of 32x32 tilemaps and how they're
  1564. layed out in memory:
  1565. 00 32x32 AA
  1566. AA
  1567. 01 64x32 AB
  1568. AB
  1569. 10 32x64 AA
  1570. BB
  1571. 11 64x64 AB
  1572. CD
  1573. Starting at the tilemap address, the first $800 bytes are for tilemap A. Then
  1574. come the $800 bytes for B, then C then D. Of course, if only A is required
  1575. something else could be stuck in the empty space.
  1576.  
  1577. Each entry in the tilemap is 2 bytes, formatted as (high low):
  1578. vhopppcc cccccccc
  1579.  
  1580. v/h = Vertical/Horizontal flip this tile.
  1581. o = Tile priority.
  1582. ppp = Tile palette. The number of entries in the palette depends on the Mode
  1583. and the BG.
  1584. cccccccccc = Tile number.
  1585.  
  1586. To find the tilemap word address for a particular tile (X and Y), you'd use a
  1587. formula something like this:
  1588. (Addr<<9) + ((Y&0x1f)<<5) + (X&0x1f) +
  1589. (SY ? ((Y&0x20)<<(SX ? 6 : 5)) : 0) + (SX ? ((X&0x20)<<5) : 0)
  1590.  
  1591. The tile character data is stored at the address pointed to by registers
  1592. $210b-c, starting at byte address:
  1593. (Base<<13) + (TileNumber * 8*NumBitplanes)
  1594. Each tile is (normally) 8x8 pixels. The data is stored in bitplanes.
  1595. Each row of the tile fills 1 byte, with the leftmost pixel being in bit 7. For
  1596. 4-color tiles, bitplanes 0 and 1 are stored in the low and high bytes of a
  1597. word, with 8 words making up the tile. For a 16-color tile, bitplanes 0 and 1
  1598. are stored as for a 4-color tile, followed by bitplanes 2 and 3 in the same
  1599. format. A 256-color tile is stored in the same way as 2 4-color tiles.
  1600.  
  1601. If the appropriate bit of $2105 is set, each "tile" of the tilemap actually
  1602. corresponds to a 16x16 pixel block consisting of Tile, Tile+1, Tile+16, and
  1603. Tile+17. In this case, the 32x32 tile tilemap codes for a 512x512 pixel screen
  1604. rather than a 256x256 pixel screen as normal. Thus, using both 16x16 tiles and
  1605. the 64x64 tilemap each BG can be up to 1024x1024 pixels. There is no wrapping
  1606. like there is for 16x16 sprites: if you specify Tile=$2ff, you'll get $2ff,
  1607. $300, $30f, and $310 (as opposed to $2ff, $2f0, $20f, and $200 you might
  1608. otherwise expect). $3ff goes to $000, of course. Flipping in this mode flips th
  1609. whole 16x16 tile, not just the individual 8x8 tiles.
  1610.  
  1611.  
  1612. BG Scrolling
  1613. ------------
  1614.  
  1615. Of course, depending on the BG mode and the interlace setting, Modes 0-6 have
  1616. an actual display of 256x224 or 256x239 pixels. The BG scroll registers
  1617. $210d-$2114 control the offset of the displayed area within that possible
  1618. 256x256 to 1024x1024 pixel BG.
  1619.  
  1620. The display can never fall outside the BG: if that would seem to be the case,
  1621. simply wrap around back to 0 (or 'tile' the BG to fill the full 1024x1024,
  1622. however you like to think of it).
  1623.  
  1624. The registers $210d-$2114 are all write-twice to set the 16-bit value. The way
  1625. this works, the last write to any of these registers is stored in a buffer.
  1626. When a new byte is written to any register, the current register value, the
  1627. previous byte written to any of the 6 registers, and the new byte written
  1628. are combined as follows:
  1629. For BGnHOFS: (NewByte<<8) | (PrevByte&~7) | ((CurrentValue>>8)&7)
  1630. For BGnVOFS: (NewByte<<8) | PrevByte
  1631. For the most part, the details don't really matter as most games always write
  1632. two bytes to one of these registers. However, some games write only one byte,
  1633. or they do other odd things.
  1634.  
  1635. Thus, the tilemap entry for a particular X and Y position on the screen may be
  1636. calculated as follows:
  1637. Size = 8 or 16 depending on the appropriate bit of $2105
  1638. TileX = (X + BGnHOFS)/Size
  1639. TileY = (Y + BGnVOFS)/Size
  1640. Look up the tile at TileX and TileY as described above.
  1641.  
  1642. Note that many games will set their vertical scroll values to -1 rather than 0.
  1643. This is bacause the SNES loads OBJ data for each scanline during the previous
  1644. scanline. The very first line, though, wouldn't have any OBJ data loaded! So
  1645. the SNES doesn't actually output scanline 0, although it does everything to
  1646. render it. These games want the first line of their tilemap to be the first
  1647. line output, so they set their VOFS registers in this manner. Note that an
  1648. interlace screen needs -2 rather than -1 to properly correct for the missing
  1649. line 0 (and an emulator would need to add 2 instead of 1 to account for this).
  1650.  
  1651.  
  1652. Direct Color Mode
  1653. -----------------
  1654.  
  1655. For the 256-color BGs of Modes 3, 4, and 7, $2130 bit 0 when set enables direct
  1656. color mode. In this mode, instead of ignoring ppp and using the character data
  1657. as the palette index, you treat the character data as expressing a color
  1658. BBGGGRRR, and use the 3 bits of ppp as bgr to make the color
  1659. Red=RRRr0, Green=GGGg0, Blue=BBb00
  1660.  
  1661. In direct color mode you cannot have a black pixel, since any pixel with
  1662. character data = 0 is still considered transparent. Use one of the almost-black
  1663. colors instead (01, 08 or 09 are good choices).
  1664.  
  1665.  
  1666. Mode 0
  1667. ------
  1668.  
  1669. In Mode 0, you have 4 BGs of 4 colors each. To calculate the starting palette
  1670. entry for a particular tile, you calculate:
  1671. ppp*4 + (BG#-1)*32
  1672.  
  1673. The background priority is (from 'front' to 'back'):
  1674. Sprites with priority 3
  1675. BG1 tiles with priority 1
  1676. BG2 tiles with priority 1
  1677. Sprites with priority 2
  1678. BG1 tiles with priority 0
  1679. BG2 tiles with priority 0
  1680. Sprites with priority 1
  1681. BG3 tiles with priority 1
  1682. BG4 tiles with priority 1
  1683. Sprites with priority 0
  1684. BG3 tiles with priority 0
  1685. BG4 tiles with priority 0
  1686.  
  1687.  
  1688. Mode 1
  1689. ------
  1690.  
  1691. In Mode 1, you have 2 BGs of 16 colors and 1 BG of 4 colors. To calculate the
  1692. starting palette entry, calculate:
  1693. ppp*ncolors
  1694.  
  1695. The background priority varies depending on the setting of bit 3 of $2105.
  1696. The priority is (from 'front' to 'back'):
  1697. BG3 tiles with priority 1 if bit 3 of $2105 is set
  1698. Sprites with priority 3
  1699. BG1 tiles with priority 1
  1700. BG2 tiles with priority 1
  1701. Sprites with priority 2
  1702. BG1 tiles with priority 0
  1703. BG2 tiles with priority 0
  1704. Sprites with priority 1
  1705. BG3 tiles with priority 1 if bit 3 of $2105 is clear
  1706. Sprites with priority 0
  1707. BG3 tiles with priority 0
  1708.  
  1709.  
  1710. Mode 2
  1711. ------
  1712.  
  1713. In Mode 2, you have 2 BGs of 16 colors each. To calculate the starting palette
  1714. index, calculate:
  1715. ppp*16
  1716.  
  1717. The priority is (from 'front' to 'back'):
  1718. Sprites with priority 3
  1719. BG1 tiles with priority 1
  1720. Sprites with priority 2
  1721. BG2 tiles with priority 1
  1722. Sprites with priority 1
  1723. BG1 tiles with priority 0
  1724. Sprites with priority 0
  1725. BG2 tiles with priority 0
  1726.  
  1727. Note the change from Modes 0 and 1.
  1728.  
  1729. Mode 2 is the first of the Offset-Per-Tile Modes. In this mode, the 'tile
  1730. data' for BG3 actually encodes a (possible) replacement HOffset and/or
  1731. VOffset value for each tile of BG1 and/or BG2.
  1732.  
  1733. Consider a visible scanline. Normally, you'd get the pixels something like
  1734. this:
  1735.  
  1736. HOFS = X + BGnHOFS
  1737. VOFS = Y + BGnVOFS
  1738. Pixel[X,Y] = GetPixel(GetTile(BGn, HOFS, VOFS), HOFS, VOFS)
  1739.  
  1740. With offset-per-tile, the formula is a little more complicated:
  1741.  
  1742. HOFS = X + BGnHOFS
  1743. VOFS = Y + BGnVOFS
  1744. ValidBit = 0x2000 for BG1, or 0x4000 for BG2
  1745. if (!IsFirst8x8Tile(BGn, HOFS)) {
  1746. /* Hopefully these calculations are right... */
  1747. Hval = GetTile(BG3, (HOFS&7)|(((X-8)&~7)+(BG3HOFS&~7)), BG3VOFS)
  1748. Vval = GetTile(BG3, (HOFS&7)|(((X-8)&~7)+(BG3HOFS&~7)), BG3VOFS + 8)
  1749. if (Hval&ValidBit) HOFS = (HOFS&7) | ((X&~7) + (Hval&~7))
  1750. if (Vval&ValidBit) VOFS = Y + Vval
  1751. }
  1752. Pixel[X,Y] = GetPixel(Get8x8Tile(BGn, HOFS, VOFS), HOFS, VOFS)
  1753.  
  1754. In other words, number the visible tiles in BGn from 0-32, and the 'visible'
  1755. tiles in BG3 the same way. BGn tile 0 is offset as normal, then for 1<=T<33
  1756. BGn tile T gets the offset data from BG3 tile T-1. It doesn't matter whether
  1757. or not the tiles actually align in any way.
  1758.  
  1759. Note that the leftmost visible tile is done as normal in all cases (although
  1760. as little as 1 pixel may be visible, and if that still bothers you then use
  1761. a clip window to hide it), and the next tile uses the tilemap entry for what
  1762. would be BG3's leftmost tile. Note also that the 'new' offset completely
  1763. overrides the BGnVOFS register, but the lower 3 bits of the BGnHOFS offset
  1764. are still used. And note that the current Y position on the screen does not
  1765. affect which row of the BG3 tilemap to reference, it's as if Y were always 0.
  1766.  
  1767. On the other hand, note that even if BGn is 16x16 tiles, BG3 can specify the
  1768. offset for each 8x8 subtile. And if BG3 is 16x16, the offsets will apply to
  1769. all the corresponding 8x8 subtiles on BGn. Also note that if BG3 is 16x16, we
  1770. may end up using the same tile for Hval and Vval.
  1771.  
  1772.  
  1773. Mode 3
  1774. ------
  1775.  
  1776. In Mode 3, you have one 256-color BG and one 16-color BG. To calculate the
  1777. starting palette index, calculate:
  1778. BG1: 0
  1779. BG2: ppp*16
  1780.  
  1781. The priority is (from 'front' to 'back'):
  1782. Sprites with priority 3
  1783. BG1 tiles with priority 1
  1784. Sprites with priority 2
  1785. BG2 tiles with priority 1
  1786. Sprites with priority 1
  1787. BG1 tiles with priority 0
  1788. Sprites with priority 0
  1789. BG2 tiles with priority 0
  1790.  
  1791. Note that register $2130 may enable Direct Color Mode on BG1.
  1792.  
  1793.  
  1794. Mode 4
  1795. ------
  1796.  
  1797. In Mode 4, you have one 256-color BG and one 4-color BG. To calculate the
  1798. starting palette index, calculate:
  1799. BG1: 0
  1800. BG2: ppp*4
  1801.  
  1802. The priority is (from 'front' to 'back'):
  1803. Sprites with priority 3
  1804. BG1 tiles with priority 1
  1805. Sprites with priority 2
  1806. BG2 tiles with priority 1
  1807. Sprites with priority 1
  1808. BG1 tiles with priority 0
  1809. Sprites with priority 0
  1810. BG2 tiles with priority 0
  1811.  
  1812. Note that register $2130 may enable Direct Color Mode on BG1.
  1813.  
  1814. Mode 4 is the second of the Offset-Per-Tile Modes. It operates much like
  1815. Mode 2, however the SNES doesn't have time to load two offset values.
  1816. Instead, it does this:
  1817. Val = GetTile(BG3, ...)
  1818. if (Val&0x8000) {
  1819. Hval = 0
  1820. Vval = Val
  1821. } else {
  1822. Hval = Val
  1823. Vval = 0
  1824. }
  1825.  
  1826.  
  1827. Mode 5
  1828. ------
  1829.  
  1830. In Mode 5, you have one 16-color BG and one 4-color BG. To calculate the
  1831. starting palette index, calculate:
  1832. ppp*ncolors
  1833.  
  1834. The priority is (from 'front' to 'back'):
  1835. Sprites with priority 3
  1836. BG1 tiles with priority 1
  1837. Sprites with priority 2
  1838. BG2 tiles with priority 1
  1839. Sprites with priority 1
  1840. BG1 tiles with priority 0
  1841. Sprites with priority 0
  1842. BG2 tiles with priority 0
  1843.  
  1844. Mode 5 is rather different from the previous modes. Instead of using an 8/16
  1845. pixel wide tile as normal, it always takes a 16 pixel wide tile (the height
  1846. may still be 8 or 16) and only uses half the pixels (zero-based, the even
  1847. pixels for subscreen tiles and the odd pixels for mainscreen tiles). Then it
  1848. forces pseudo-hires on to render a 512-pixel wide scanline. Also, if
  1849. Interlace mode is on (see bit 0 of $2133), the screen is 448 or 478
  1850. half-lines high instead of 224 or 239. Either the odd half-lines or the even
  1851. half-lines are drawn each frame, as indicated by bit 7 of $213f.
  1852.  
  1853. Note that this means you must set $212c and $212d to the same value to get the
  1854. 'expected' display.
  1855.  
  1856.  
  1857. Mode 6
  1858. ------
  1859.  
  1860. In Mode 6, you have only one 16-color BG. To calculate the starting palette
  1861. index, calculate:
  1862. ppp*ncolors
  1863.  
  1864. The priority is (from 'front' to 'back'):
  1865. Sprites with priority 3
  1866. BG1 tiles with priority 1
  1867. Sprites with priority 2
  1868. Sprites with priority 1
  1869. BG1 tiles with priority 0
  1870. Sprites with priority 0
  1871.  
  1872. Mode 6 has the same oddities as Mode 5. In addition, it is an offset per tile
  1873. mode! That part works just like as Mode 2. However, remember that Mode 6 always
  1874. uses 8 pixel (16 half-pixel) wide tiles, this applies to BG3 as well as BG1.
  1875. You can't apply the offset to an 8-half-pixel tile nor to a 16-pixel wide area
  1876. (except by using two offset values for the two 8-pixel areas).
  1877.  
  1878.  
  1879. Mode 7
  1880. ------
  1881.  
  1882. Mode 7 is extremely different from all the modes before. You have one BG of 256
  1883. colors. However, the tilemap and character map are laid out completely
  1884. differently.
  1885.  
  1886. The tilemap and charactermap are interleaved, with the character data being in
  1887. the high byte of each word and the tilemap data being in the low byte (note
  1888. that in hardware, VRAM is set up such that odd bytes are in one RAM chip and
  1889. even in another, and each RAM chip has a separate address bus. The Mode 7
  1890. renderer probably accesses the two chips independantly). The tilemap is 128x128
  1891. entries of one byte each, with that one byte being simply a character map
  1892. index. The character data is stored packed pixel rather than bitplaned, with
  1893. one pixel per byte. Thus, to calculate the tilemap entry byte address for an X
  1894. and Y position in the playing field, you'd calculate: (((Y&~7)<<4) + (X>>3))<<1
  1895.  
  1896. To find the byte address of the pixel, you'd calculate:
  1897. (((TileData<<6) + ((Y&7)<<3) + (X&7))<<1) + 1
  1898.  
  1899. Note that bits 4-7 of $2105 are ignored, as are $2107-$210c. They can be
  1900. considered to be always 0.
  1901.  
  1902. The next odd thing about Mode 7 is that you have full matrix transformation
  1903. abilities. With creative use of HDMA, you can even change the matrix per
  1904. scanline. See registers $211b-$2120 for details on the matrix transformation
  1905. formula. The entire screen can be flipped with bits 0-1 of $211a.
  1906.  
  1907. And finally, the playing field can actually be made larger than the tilemap. If
  1908. bit 7 of $211a is set, bit 6 of $211a controls what is seen filling the space
  1909. surrounding the map.
  1910.  
  1911. The background priorities are:
  1912. Sprites with priority 3
  1913. Sprites with priority 2
  1914. Sprites with priority 1
  1915. BG1
  1916. Sprites with priority 0
  1917.  
  1918. When bit 6 of $2133 is set, you get a related mode known as Mode 7 EXTBG. In
  1919. this mode, you get a BG2 with 128 colors, which uses the same tilemap and
  1920. character data as BG1 but interprets the high bit of the pixel as a priority
  1921. bit. The priority map is:
  1922. Sprites with priority 3
  1923. Sprites with priority 2
  1924. BG2 pixels with priority 1
  1925. Sprites with priority 1
  1926. BG1
  1927. Sprites with priority 0
  1928. BG2 pixels with priority 0
  1929.  
  1930. Note that the BG1 pixels (if BG1 is enabled) will usually completely obscure
  1931. the low-priority BG2 pixels.
  1932.  
  1933. BG2 uses the Mode 7 scrolling registers ($210d-e) rather than the 'normal' BG2
  1934. ones ($210f-10). Subscreen, pseudo-hires, math, and clip windows work as
  1935. normal; keep in mind OBJ and that you can do things like enable BG1 on main and
  1936. BG2 on sub if you so desire. Mosaic is somewhat weird, see the section on
  1937. Mosaic below.
  1938.  
  1939. Note that BG1, being a 256-color BG, can do Direct Color mode (in this case, of
  1940. course, there is no palette value so you're limited to 256 colors instead of
  1941. 2048). BG2 does not do direct color mode, since it is only 7-bit.
  1942.  
  1943.  
  1944. Rendering the BGs
  1945. -----------------
  1946.  
  1947. Rendering a BG is simple.
  1948.  
  1949. 1) Get your H and V offsets (either by reading the appropriate registers or by
  1950. doing the offset-per-tile calculation).
  1951. 2) Use those to translate the screen X and Y into playing field X and Y
  1952. - Note this is rather complicated for Mode 7
  1953. 3) Look up the tilemap for those coordinates
  1954. 4) Use that to find the character data
  1955. 5) If necessary, de-bitplane it and stick it in a buffer.
  1956.  
  1957. See the section "RENDERING THE SCREEN" below for more details.
  1958.  
  1959. Unresolved Issues
  1960. -----------------
  1961.  
  1962. 1) What happens to the very first pixel on the scanline in Hires Math?
  1963. 2) Various registers still need to know when writing to them is effective.
  1964.  
  1965.  
  1966. WINDOWS
  1967. =======
  1968.  
  1969. The masking windows are pretty simple. The windows can be used to mask off a
  1970. portion of any BG on the scanline. With HDMA, they can be adjusted per
  1971. scanline. They can be combined in various ways, per BG. Each can be used to
  1972. select either the region of the BG to keep, or the region of the BG to hide,
  1973. per BG. All that's left is to see the registers above and the section
  1974. "RENDERING THE SCREEN" below for details.
  1975.  
  1976. The Color Window
  1977. ----------------
  1978.  
  1979. The color window is rather different. The color window itself can be set
  1980. to clip the colors of pixels to black (before math, so it's almost the same
  1981. effect you'd get by setting all entries in the palette to black, then fixing
  1982. them before you do subscreen addition--the only difference is that half math
  1983. will not occur), and to prevent all color math effects from occurring. These
  1984. can be applied never, always, inside the "clip" windows specified for the color
  1985. window, or outside the "clip" window.
  1986.  
  1987. Bits 6-7 of register $2130 controls whether the pixel colors (and half-math)
  1988. will be clipped inside the window, outside the window, never, or always. Bits
  1989. 4-5 do the same for preventing color math.
  1990.  
  1991. Consider the main screen set up so BGs 1 and 2 are visible in an 8x8
  1992. checkerboard pattern, with all the BG1 pixels red and all the BG2 pixels blue.
  1993. The subscreen is filled with a green BG, and color math is enabled on BG 1
  1994. only. You'll end up with a yellow and blue checkerboard. Turn on the color
  1995. window to clip colors, and you'll get a green and black checkerboard since
  1996. the subscreen is only added (to a black pixel) where BG1 would be visible. If
  1997. you clip math instead, you'll get the same display you'd get with color math
  1998. disabled on all BGs.
  1999.  
  2000. In hires modes, we use the previous main-screen pixel to determine whether the
  2001. color window effect should be applied to a subscreen pixel. See "Color Math"
  2002. below for details.
  2003.  
  2004.  
  2005. RENDERING THE SCREEN
  2006. ====================
  2007.  
  2008. Mosaic
  2009. ------
  2010.  
  2011. The mosaic filter is applied after the BG is rendered and scrolled but before
  2012. it is clipped, combined with other BGs, pseudo-hiresed, or mathed. Each XxX
  2013. block of pixels is replaced with the upper-leftmost pixel of the block. The
  2014. 'blocks' are such that the upper-leftmost block is at the left edge of the
  2015. screen at the scanline where $2106 was written (or the first visible scanline
  2016. if it was not written this frame).
  2017.  
  2018. Modes 5/6 Hires work slightly differently: they use a 2XxX block of
  2019. half-pixels. Similarly, Modes 5/6 interlaced use a 2Xx2X block of half-pixels.
  2020. So if you set $2106 to $0F ("1x1" blocks), the even half-pixels will be
  2021. expanded to cover the odd half-pixels. $1F would cover the next even-and-odd
  2022. pixel over as well. An example: put a single red pixel at line #1 pixel #0 of
  2023. Mode 5 BG1, and a single blue pixel in the same place on BG2. Enable BG1 on
  2024. main and BG2 on sub, you'll see the blue pixel only. Set $2106=$03, and you'll
  2025. suddenly see both the blue and red pixels. Set $2106=$13, and you'll see
  2026. "BRBR" on two lines.
  2027.  
  2028. Mode 7's matrix transformations do not affect the mosaic block positions, so
  2029. BG1 can be mosaiced about as normal. BG2 in EXTBG mode is weird, though: it
  2030. uses bit 0 of $2106 to control "vertical mosaic" and bit 1 to control
  2031. "horizontal mosaic". So if $2106 is $F1, BG2 will expand with 1x16 blocks. $F2
  2032. will give 16x1 blocks, and only $F3 will give the expected 16x16 blocks. Note
  2033. that BG1 still uses bit 0 as usual, so you can have BG1 expanded with 16x16
  2034. blocks and the high-priority BG2 pixels expanded with 1x16 blocks on top of it.
  2035. Or you could have BG1 rendered as normal, but with the high-priority pixels
  2036. from BG2 expanded 16x1 on top of it.
  2037.  
  2038.  
  2039. Color Math
  2040. ----------
  2041.  
  2042. Each main-screen BG (and the color-0 backdrop, and the sprites (although
  2043. sprites with palettes 0-3 never participate)) may be marked in register $2131
  2044. to participate in color math. If the visible pixel is from a layer/OBJ
  2045. participating in color math, we perform one of 8 operations on the pixel,
  2046. depending on $2130 bit 1 and $2131 bits 6-7.
  2047.  
  2048. 0 00: Add the fixed color. R, G, and B are added separately, and clipped to
  2049. the max.
  2050. 0 01: Add the fixed color, and divide the result by 2 before clipping (unless
  2051. the Color Window is clipping colors here).
  2052. 0 10: Subtract the fixed color from the pixel. For example, if the pixel is
  2053. (31,31,0) and the fixed color is (0,16,16), the result is (31,15,0).
  2054. 0 11: Subtract the fixed color, and divide the result by 2 (unless CW etc).
  2055. 1 00: Add the corresopnding subscreen pixel, or the fixed color if it's the
  2056. subscreen backdrop.
  2057. 1 01: Add the subscreen pixel and divide by 2 (unless CW etc), or add the
  2058. fixed color with no division.
  2059. 1 10: Subtract the subscreen pixel/fixed color.
  2060. 1 11: Subtract the subscreen pixel and divide by 2 (unless CW etc), or sub
  2061. the fixed color with no division.
  2062.  
  2063. In hires modes, color math is applied to the visible subscreen pixels as well.
  2064. Choosing the math operation is simple: look at the previous main-screen pixel
  2065. (i.e. if we're at pixel #6 on the 512-pixel screen (which is taken from pixel
  2066. #4 on the subscreen), we look at pixel #5 (#3 on the main screen)). If no math
  2067. was applied to that pixel, don't math this subscreen pixel either. If the
  2068. fixed color was added/subtracted, add/subtract the fixed color. And if a pixel
  2069. from the subscreen was added/subtracted, add/subtract that main-screen pixel
  2070. (the original value before math). What happens to the subscreen pixel at the
  2071. left edge of the screen is unknown.
  2072.  
  2073. This is really important with color subtraction: normally, if you have
  2074. a block of cyan (#00ffff) on main and a block of magenta (#ff00ff) on sub,
  2075. subtraction would give a block of green. Hires math will give you a block of
  2076. alternating green and red, which will probably appear yellow on your TV. If
  2077. you've set $2131 bit 6 and this block is sitting alone in the middle of the
  2078. backdrop, you'll have a bright line at the left edge where the fixed color was
  2079. subtracted from the subscreen pixel and no 1/2 was applied (because the
  2080. previous main pixel had the fixed color subtracted and no 1/2 applied).
  2081.  
  2082.  
  2083. Rendering the Screen
  2084. --------------------
  2085.  
  2086. Note that this may be inaccurate.
  2087.  
  2088. 1) Go down the priority list to find the first BG/OBJ layer that is enabled on
  2089. main, not clipped, and has a non-transparent pixel here. You'll always
  2090. bottom out on the backdrop (color 0) if not before.
  2091. 2) If the color window clips colors here, set the color of that pixel to 0.
  2092. 3) If color math is applicable and the color window doesn't clip math here, do
  2093. math.
  2094.  
  2095. Hires modes (BG modes 5 and 6 or any mode 0-4 with bit 3 of $2133 set) should
  2096. process the visible subscreen pixels as described above.
  2097.  
  2098.  
  2099.  
  2100. CONTROLLERS
  2101. ===========
  2102.  
  2103. The SNES has 2 controller ports on the front of the unit, and an "expansion
  2104. port" on the bottom (which AFAIK was only used by a few things released only
  2105. in Japan). Little is known about the expansion port.
  2106.  
  2107. A number of peripherals could be plugged into the controller ports:
  2108. * Joypads
  2109. * The Multitap (aka MP5), into which up to 4 joypads may be plugged.
  2110. * A Mouse, with 2 buttons.
  2111. * The SuperScope, a bazooka-like light gun.
  2112. * The Konami Justifiers, a normal style gun into which a second gun could
  2113. be plugged.
  2114.  
  2115. There are probably others, these are just the ones I know anything about.
  2116.  
  2117.  
  2118. Generic
  2119. -------
  2120.  
  2121. The controller ports of the SNES has 7 pins, laid out something like this:
  2122. _________________ ____________
  2123. | | \
  2124. | (1) (2) (3) (4) | (5) (6) (7) |
  2125. |_________________|____________/
  2126.  
  2127. The pins are:
  2128. 1: +5v (power)
  2129. 2: Clock
  2130. 3: Latch
  2131. 4: Data1
  2132. 5: Data2
  2133. 6: IOBit
  2134. 7: Ground
  2135.  
  2136. Latch is written trhough bit 0 of register $4016. Writing 1 to this bit
  2137. results in Latch going to whatever state means 'latch' to a joypad.
  2138.  
  2139. Clock of Port 1 is connected to the 'read' signal of $4016, in that reading
  2140. $4016 causes Clock to transition. Data1 and Data2 are then read, and Clock
  2141. transitions back (at this point, the pad is expected to stick its next bits
  2142. of data on Data1 and Data2). Clock of Port 2 is connected to $4017.
  2143.  
  2144. Data1 and Data2 are read through bits 0 and 1 (respectively) of $4016 and
  2145. $4017 (for Ports 1 and 2, respectively). Thus, you must read both bits at
  2146. once, you can't choose to read only Data1 and leave Data2 for later.
  2147.  
  2148. IOBit is connected to the I/O Port (which is accessed through registers
  2149. $4201 and $4213). Port 1's IOBit is connected to bit 6 of the I/O Port, and
  2150. Port 2's IOBit is connected to bit 7. Note that, since bit 7 of the I/O Port
  2151. is connected to the PPU Counter Latch, anything plugged into Port 2 may
  2152. latch the H and V Counters by setting IOBit to 0.
  2153.  
  2154. Auto Joypad Read, when enabled by bit 0 of $4200, effectively does the
  2155. following (in pseudo-ASM):
  2156. LDA $4212
  2157. ORA #$01
  2158. STA $4212 ; pretend it's writable
  2159.  
  2160. LDA #$01
  2161. STA $4016
  2162. ; There may be a delay here
  2163. STZ $4016
  2164.  
  2165. LDX #$0010
  2166. loop:
  2167. LDA $4016
  2168. REP #$20
  2169. LSR
  2170. ROL $4218
  2171. LSR
  2172. ROL $421C
  2173. SEP #$20
  2174.  
  2175. LDA $4017
  2176. REP #$20
  2177. LSR
  2178. ROL $421A
  2179. LSR
  2180. ROL $421E
  2181. SEP #$20
  2182.  
  2183. DEX
  2184. BNE loop
  2185.  
  2186. LDA $4212
  2187. AND #$7E
  2188. STA $4212 ; pretend it's writable again
  2189.  
  2190.  
  2191. "Open Port"
  2192. -----------
  2193.  
  2194. If nothing is plugged into a port (or the thing plugged in doesn't connect to
  2195. the pin), the SNES will read zeros from Data1 and Data2.
  2196.  
  2197.  
  2198. Joypads
  2199. -------
  2200.  
  2201. The joypads return 16 bits of data out Data1, then one bits until
  2202. latched again. The data is:
  2203. byetUDLRaxlr0000
  2204.  
  2205. b/y/a/x/l/r are the similarly named buttons. 'e' is select. 't' is start.
  2206. U/D/L/R are the pad directions. Note that the standard joypad can only
  2207. return either U or D set, and either L or R set. Some games will crash or
  2208. exhibit other odd behavior if both U and D and/or both L and R are set.
  2209.  
  2210. Data2 is not even connected, nor is IOBit.
  2211.  
  2212.  
  2213. Mouse
  2214. -----
  2215.  
  2216. The mouse returns 32 bits of data out Data1, and 1 bits thereafter. The data
  2217. is:
  2218. 00000000rlss0001 YyyyyyyyXxxxxxxx
  2219.  
  2220. l/r are the two mouse buttons. 'ss' are the "speed bits", which are
  2221. incremented mod 3 if Clock cycles while Latch is active. Y/X are the
  2222. direction bits (set is up/left), and yyyyyyy/xxxxxxx are the distance
  2223. traveled in the appropriate direction.
  2224.  
  2225. Supposedly, the 'speed bits' may not match the internal speed setting when
  2226. the mouse first receives power. The speed setting controls the delta curve
  2227. of the mouse, with 0 giving a flat curve and 2 giving the greatest delta
  2228. response.
  2229.  
  2230. Data2 and IOBit are presumably not connected, but this is not known for
  2231. sure.
  2232.  
  2233.  
  2234. SuperScope
  2235. ----------
  2236.  
  2237. The SuperScope returns 8 bits of data out Data1, and 1 bits thereafter. The
  2238. data is:
  2239. fctp00on
  2240.  
  2241. 'f' is Fire, 'c' is Cursor, 't' is Turbo, 'p' is Pause, 'o' is Offscreen,
  2242. and 'n' is Noise.
  2243.  
  2244. The SuperScope has two modes of operation: normal mode and turbo mode. The
  2245. current mode is controlled by a switch on the unit, and is indicated by the
  2246. 't' bit. Note however that the 't' bit is only updated when the Fire button
  2247. is pressed (i.e. the 'f' bit is set). Thus, when you turn turbo on the 't'
  2248. bit remains clear until you shoot, and similarly when turbo is deactivated
  2249. the bit remains set until you fire.
  2250.  
  2251. In either mode, the Pause bit will be set for the first strobe after the
  2252. pause button is pressed, and then will be clear for subsequent strobes until
  2253. the button is pressed again. However, the pause button is ignored if either
  2254. cursor or fire are down(?).
  2255.  
  2256. In either mode, the Cursor bit will be set while the Cursor button is pressed.
  2257.  
  2258. In normal mode, the Fire bit operates like Pause: it is on for only one strobe.
  2259. In turbo mode, it remains set as long as the button is held down.
  2260.  
  2261. When Fire/Cursor are set, Offscreen will be set if the gun did not latch during
  2262. the previous strobe and cleared otherwise (Offscreen is not altered when
  2263. Fire/Cursor are both clear).
  2264.  
  2265. Noise is set if there is interference in the infrared transmission from the
  2266. Scope to the receiver.
  2267.  
  2268. If the Fire button is being held when turbo mode is activated, the gun sets the
  2269. Fire bit and begins latching. If the Fire button is being held when turbo mode
  2270. is deactivated, the next poll will have Fire clear but the Turbo bit will not
  2271. be updated until the next fire (i.e. FcTp => turbo off => fcTp, not fctp).
  2272.  
  2273. The PPU latch operates as follows: When Fire or Cursor is set, IOBit is set
  2274. to 0 when the gun sees the TV's electron gun, and left a 1 otherwise. Thus,
  2275. if the SNES also leaves it one (bit 7 of $4201), the PPU Counters will be
  2276. latched at that point. This would also imply that bit 7 of $4213 will be 0
  2277. at the moment the SuperScope sees the electron gun.
  2278.  
  2279. Since the gun depends on the latching behavior of IOBit, it will only
  2280. function properly when plugged into Port 2. If plugged into Port 1 instead,
  2281. everything will work except that there will be no way to tell where on the
  2282. screen the gun is pointing.
  2283.  
  2284. When creating graphics for the SuperScope, note that the color red is not
  2285. detected. For best results, use colors with the blue component over 75% and/or
  2286. the green component over 50%.
  2287.  
  2288. Data2 is presumably not connected, but this is not known for sure.
  2289.  
  2290.  
  2291. Justifiers
  2292. ----------
  2293.  
  2294. The Justifier returns 48 bits of data out Data1. Presumably it returns
  2295. one bits after (if so, it really only returns 32 bits), but this is not
  2296. known. The data is:
  2297. 0000000000001110 01010101TtSsl000 1111111111111111
  2298.  
  2299. T/t are the trigger states for guns 1 and 2. S/s are the start button states
  2300. for guns 1 and 2. 'l' indicates which gun was connected to IOBit: 1 means
  2301. gun 1, 0 means gun 2. Note that 'l' toggles even when gun 2 is not connected.
  2302.  
  2303. IOBit is used just like for the SuperScope. However, since two guns may be
  2304. plugged into one port, which gun is actually connected to IOBit changes each
  2305. time Latch cycles. Also note, the Justifier does not wait for the trigger to
  2306. be pulled before attempting to latch, it will latch every time it sees the
  2307. electron gun. Bit 6 of $213F may be used to determine if the Justifier was
  2308. pointed at the screen or not.
  2309.  
  2310. Data2 is presumably not connected, but this is not known for sure.
  2311.  
  2312.  
  2313. MP5
  2314. ---
  2315.  
  2316. The MP5 plugs into one Controller Port on the SNES (typically Port 2), and
  2317. has 4 ports for controllers to be plugged into it (labeled 2 through 5). It
  2318. also has an override switch which makes it pass through Pad 2 and ignore
  2319. everything else.
  2320.  
  2321. If IOBit is 1, Clock is passed through to Pad 2 and Pad 3, Data1 is
  2322. connected to Data1 on Pad 2, and Data2 is connected to Data1 on Pad 3. If
  2323. IOBit is 0, Pads 4 and 5 are used instead of 2 and 3, respectively. In
  2324. either case, Latch is passed through to all pads, and IOBit is presumably
  2325. not passed through at all.
  2326.  
  2327. Note that Clock is only passed through to the pads that are actually being
  2328. passed through. Thus, you can read the first two pads (or let Auto-Joypad
  2329. Read do it), then toggle IOBit and read the other two pads manually. Most
  2330. games requiring more than 3 players do exactly this.
  2331.  
  2332. Also note that there is nothing preventing the MP5 from functioning
  2333. perfectly when plugged in to Port 1, except that the game must use bit 6 of
  2334. $4201 instead of bit 7 to set IOBit and must use the Port 1 registers
  2335. instead of the Port 2 registers. With 2 MP5 units, one could actually create
  2336. an 8-player game!
  2337.  
  2338. When Latch is active, 1s will be read from Data2 and 0s from Data1. This is
  2339. sometmies used to detect the presence of an MP5 unit. The override switch
  2340. disables this behavior.
  2341.  
  2342. There are reports that the MP5 does not react immediately when IOBit is
  2343. transitioned from 0 to 1. Thus, reading 2&3 then 4&5 will probably work
  2344. better than vice versa.
  2345.  
  2346.  
  2347. DMA AND HDMA
  2348. ============
  2349.  
  2350. DMA, or "direct memory access" is found in a number of computer systems, not
  2351. just the Super Nintendo. It's basically a way for a peripheral or
  2352. coprocessor to read data directly from memory, instead of requiring the main
  2353. CPU to do a number of reads and writes. This is typically faster, if only
  2354. because it lets the system skip the opcode fetch-and-decode. In the SNES, the
  2355. CPU is paused during DMA since the address busses are in use for the transfer.
  2356.  
  2357. HDMA is similar in concept, though rather different in execution: instead of
  2358. transferring a block of memory all at once, it transfers a few bytes during
  2359. the H-Blank period of each scanline. This is extremely helpful, as most PPU
  2360. registers may only be changed during a frame (at least without glitching)
  2361. during this narrow window.
  2362.  
  2363. The SNES has 8 channels (numbered 0-7) that can be used for either DMA or HDMA.
  2364. HDMA takes priority over DMA if both are to occur at once, pausing all DMA and
  2365. terminating a conflicting DMA immediately. Lower-numbered channels take
  2366. priority over higher-numbered channels.
  2367.  
  2368.  
  2369. DMA
  2370. ---
  2371.  
  2372. A DMA transfer has three main variables, and a number of setting bits. These
  2373. are: (those marked '*' must be set up before starting DMA)
  2374. * Direction (bit 7 of $43x0): Read from PPU or write to PPU?
  2375. * Fixed (bit 3 of $43x0): Adjust Address?
  2376. * Increment (bit 4 of $43x0): Direction to adjust Address?
  2377. * Mode (bits 0-2 of $43x0): See below...
  2378. * Port (register $43x1): If this is 'xx', the register accessed will be $21xx.
  2379. * AAddress (registers $43x2-4): Any CPU address, just like you'd use with
  2380. the Absolute Long addressing mode.
  2381. * Count (registers $43x5-6): The number of bytes to transfer.
  2382.  
  2383. See register $43x0 for the correspondance between the Mode bits and the
  2384. transfer mode. Note that One Register Write Once and One Register Write
  2385. Twice end up being the exact same thing, and Two Registers Write Once
  2386. and Two Registers Write Twice Alternale are the same, but that Two Registers
  2387. Write Once and Two Registers Write Twice Each are different.
  2388.  
  2389. DMA transfers take 8 master cycles per byte transferred, no matter the
  2390. FastROM setting. There is also an overhead of 8 master cycles per channel, and
  2391. an overhead of 12-24 cycles for the whole transfer.
  2392.  
  2393. The basic process seems to be:
  2394. 1. Get byte and write it to the destination.
  2395. - The DMA seems to take advantage of the SNES's two address busses with one
  2396. shared data bus. AAddress is pushed out Bus A, Port is pushed out bus B,
  2397. and the read/write signals are sent according to Direction. The bus
  2398. marked read obligingly put data on the bus, while the bus marked write
  2399. obligingly writes that value.
  2400. - Thus, since the PPU/APU/WRAM registers are only accessible via Bus B,
  2401. attempts to access them via AAddress will result in Open Bus accesses.
  2402. - Attempts to access WRAM via both Bus A and Bus B (registers 2180-3) will
  2403. fail, with the 2180-3 access being Open Bussed.
  2404. - Also, DMA cannot access the $4300-$437f registers nor $420b nor $420c.
  2405. Writes will have no effect, and reads will return Open Bus.
  2406. 2. Adjust AAddress.
  2407. - If Fixed is set, do nothing. Else if Increment is set, subtract one,
  2408. else add one.
  2409. - Note that the bank byte is not modified.
  2410. 3. Decrement Count. If count is not zero, then go to step 1.
  2411. - Thus, if Count is initially zero, it wraps to 65535 before being
  2412. tested. So you end up transferring 65536 bytes.
  2413.  
  2414. Note that Count ($43x5-6) ends up always 0, unless a conflicting HDMA
  2415. terminates the transfer early.
  2416.  
  2417.  
  2418. HDMA
  2419. ----
  2420.  
  2421. HDMA has 4 flags and 5 variables. Again, those marked '*' are required
  2422. before starting HDMA. In addition, those marked '+' are required if HDMA is
  2423. to be started mid-frame.
  2424. * Addressing Mode (bit 6 of $43x0): If clear, Direct, else Indirect.
  2425. * Transfer Mode (bits 0-2 of $43x0): See below...
  2426. * Port ($43x1): As for DMA.
  2427. * AAddress ($43x2-4): Pointer to the HDMA Table. Not really 'required' for
  2428. starting mid-frame, but unless you're going to stop it before the next
  2429. init...
  2430. - Indirect Address ($43x5-6): Used with Indirect Bank. See below...
  2431. * Indirect Bank ($43x7): Used with Indirect Address. See below...
  2432. + Address ($43x8-9): See below...
  2433. + Repeat (bit 7 of $43xA): Whether to write every scanline or not
  2434. + Line Counter (bits 0-6 of $43xA): See below...
  2435. - DoTransfer: Used internally.
  2436.  
  2437. Modes are the same as for DMA. However, note that only one cycle through the
  2438. mode is done per scanline, so One Register Write Once will write 1 byte per
  2439. scanline, while One Register Write Twice will write two.
  2440.  
  2441. For each scanline during which HDMA is active (i.e. at least one channel is not
  2442. paused and has not terminated yet for the frame), there are ~18 master cycles
  2443. overhead. Each active channel incurs another 8 master cycles overhead (during
  2444. which time $42xA is presumably loaded if necessary) for every scanline, whether
  2445. or not a transfer actually occurs. If a new indirect address is required, 16
  2446. master cycles are taken to load it. Then 8 cycles per byte transferred are
  2447. used. Thus, HDMA takes a maximum of 466 master cycles per scanline (if all 8
  2448. channels are active, require an indirect address load, and transfer 4 bytes).
  2449.  
  2450. The basic process has two sections. First, at the beginning of the frame (V=0
  2451. H=approx 6), for all active HDMA channels (see register $420c):
  2452. 1. Copy AAddress into Address.
  2453. 2. Load $43xA (Line Counter and Repeat) from the table. I believe $00 will
  2454. terminate this channel immediately.
  2455. 3. Load Indirect Address, if necessary.
  2456. 4. Set DoTransfer to true.
  2457.  
  2458. The CPU is paused during this time. Overhead is ~18 master cycles, plus 8
  2459. master cycles for each channel set for direct HDMA and 24 master cycles for
  2460. each channel set for indirect HDMA.
  2461.  
  2462. If you are starting HDMA mid-frame, you must basically do the init process
  2463. manually by setting $43x8-A, and $43x5-6 for indirect channels. Note though
  2464. that there is no way to perform step 4, so no transfer will be done the first
  2465. transfer period. Also, note that a channel that has already terminated for the
  2466. frame cannot be restarted.
  2467. XXX: Or does it automatically do Step 4 when you enable the channel?
  2468.  
  2469. Then, for each scanline from V=0 to V=$e0 (or V=$ef is overscan is enabled) at
  2470. about H=$116:
  2471. 1. If DoTransfer is false, skip to step 3.
  2472. 2. For the number of bytes (1, 2, or 4) required for this Transfer Mode...
  2473. a. Read a byte from Address or Indirect Address, and increment.
  2474. b. Write the byte to Port, Port+1, Port+2, or Port+3, depending on the
  2475. Transfer Mode and which byte we're on.
  2476. - The same notes regarding DMA from PPU to PPU or RAM to RAM via $2180
  2477. apply here as well.
  2478. 3. Decrement $43xA.
  2479. 4. Set DoTransfer to the value of Repeat.
  2480. 5. If Line Counter is zero...
  2481. a. Read the next byte from Address into $43xA (thus, into both Line
  2482. Counter and Repeat).
  2483. b. If Addressing Mode is Indirect, read two bytes from Address into
  2484. Indirect Address (and increment Address by two bytes).
  2485. - One oddity: if $43xA is 0 and this is the last active HDMA channel for
  2486. this scanline, only load one byte for Address, and use the
  2487. $00 for the low byte. So Address ends up incremented one less than
  2488. otherwise expected, and one less CPU Cycle is used.
  2489. c. If $43xA is zero, terminate this HDMA channel for this frame. The bit in
  2490. $420c is not cleared, though, so it may be automatically restarted next
  2491. frame.
  2492. d. Set DoTransfer to true.
  2493. 6. Continue with Step 1 next scanline.
  2494.  
  2495. HDMA does not occur during V-Blank, as any writes it might perform are
  2496. likely have no visible effect anyway. The start-of-frame processing then resets
  2497. all active channels at the end of V-Blank. This allows updating of the HDMA
  2498. registers during V-Blank without worrying about the transfer beginning
  2499. immediately and scribbling on the PPU state.
  2500.  
  2501. Note how the above implicitly defines the format of the HDMA table.
  2502. Explicitly, the format is a series of entries. Each entry begins with a line
  2503. count and repeat flag. If repeat is false, there is one scanline worth of
  2504. data following and the count is the number of scanlines to wait before
  2505. processing the next entry. If it's true, the line count is the number of
  2506. scanlines worth of data following. The data following is either a pointer to
  2507. the data (for Indirect HDMA), or the data itself (for Direct HDMA).
  2508.  
  2509. Looking at the above, it's clear why Address, and Repeat/Line Counter must
  2510. be initialized by hand when starting HDMA mid-frame: they're only
  2511. automatically initialized at the start of the frame. Note how AAddress is
  2512. not affected by HDMA, though Address and Repeat/Line Counter are.
  2513.  
  2514.  
  2515. HISTORY
  2516. =======
  2517.  
  2518. In the beginning... Well, ok, somewhere in the middle is where I came in. In
  2519. my beginning, there was Yoshi's register doc (the one with bananas) and
  2520. snesmap.txt (the one missing all those appendices). Both good register docs,
  2521. complete as far as bits go, but sorely lacking in the explanations. And there
  2522. was the snes9x source, with more clear semantics but many errors. I began
  2523. writing test ROMs for others to run, and discover the real behavior of the
  2524. SNES. Soon, someone lent me a device to test the ROMs myself.
  2525.  
  2526. I discovered many things, and lamented the errors in the documentation
  2527. available. Finally one day I decided to sit down and write out everything I
  2528. had discovered. An early version of this was the result. Along the way, and
  2529. continuing since, I've revised this document with new findings. I believe this
  2530. is the most accurate SNES PPU/graphics document available today. Enjoy!
Advertisement
Add Comment
Please, Sign In to add comment