Advertisement
Guest User

SNES regs.txt

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