1. ABOUT
   WStech doc v 1.1 made by Judge and Dox (dox@space.pl).

2. CPU 
  80186 (possibly a NEC V20/V30, but without NEC-specific opcodes (prefix 0xFA)) running at 3.072 MHz. 
  Also timing of cpu is different than the one in 80186.

3. MEMORY

   20 bit addressing space = 1 Megabyte. Memory is split into 64KB blocks (segments/banks).

   BANK 0 - 64 KB internal RAM (see below)
        1 - SRAM/EEPROM (cart)
        2 - Switchable ROM Bank (initial bank = last - 13)
        3 - Switchable ROM Bank (last - 12)
        4 - Static ROM Bank (last - 11)
        5 - Static ROM Bank (last - 10)
        6 - Static ROM Bank (last - 9)
        7 - Static ROM Bank (last - 8)
        8 - Static ROM Bank (last - 7)
        9 - Static ROM Bank (last - 6)
        A - Static ROM Bank (last - 5)
        B - Static ROM Bank (last - 4)
        C - Static ROM Bank (last - 3)
        D - Static ROM Bank (last - 2)
        E - Static ROM Bank (last - 1)
        F - Static ROM Bank (last)

  RAM Map :
      $0000 - $3FFF      
      $4000 - $7FFF   Tiles Bank 0 ($4000 - $5FFF in WS)
      $8000 - $BFFF   Tiles Bank 1
      $C000 - $FDFF
      $FE00 - $FFFF   Palettes (WSC)

4. VIDEO
   Screen size     - 224 x 144 pixels (28 x 18 tiles)
   Tile size       - 8 x 8 dots , 16 bytes/tile (4 col modes) or 32 bytes/tile (16 col modes)
   Map size        - 256 x 256 pixels (32 x 32 tiles)
   Layers          - Two layers - Background and Foreground (top layer)
   Maps locations  - Selectable using port $07
   Tiles locations - Fixed, two banks - one at $4000 , second at $8000
   Map format      - Each position in the map is defined by one word:
 		     bits
                     0 - 8   - Tile number (0-511)
                     9 - 12  - Palette number (0-15)
                     13      - Tile bank
                     14      - Horizontal flip 
                     15      - Vertical flip
   Tile formats    - Depends on video mode (port $60)
   Sprites         - Max 128 sprites , limited to max 32 on scanline
                     sprite format:
                     byte 0,1 - bits
			        0 - 8   - Tile number (0-511)
			        9 - 11  - Palette number (0-7) + 8 -> (8-15)
				12      - Sprite window clipping on/off
                                13      - Priority with respect to the layers
                     		          0 - appear between the 2 background and foreground layers
                         		  1 - appear on top of both layers
				14      - Horizontal flip
				15      - Vertical flip
                     byte 2 - Y position on the screen
                     byte 3 - X position on the screen

   Colors          - Wonderswan (Mono) is capable of showing 16 shades of gray(only 8 can be selected at any one time)
                     These 8 shades form a pool from which the palette definition can select shades. There are 16 palettes.
                     All 16 palettes are used by BG and FG layers , the last 8  used by sprites.   
                     Which 8 colors are used for palette generation is defined by ports 1C and 1E- port 1C
                     defines palette colors 0 - 3, port 1E defines 4 - 7. Each palette selection is 4 bits in      
                     size:
                       1C : 11110000
	               1D : 33332222
	               1E : 55554444
	               1F : 77776666
                      
                      (where color 15 is the darkest one)

                      Ports 20 - 3E are used to define the palettes themselves.
                	20 : x111x000 - palette #0
                 	21 : x333x222
        	     
                     In color video modes each color is defined using one word,
                        where bits:
                              0 -  3  Blue
                              4 -  7  Green
                              8 - 11  Red 
                             12 - 14  unused
                     Color palettes are stored in the RAM , at address $FE00

   Scrolling       - Each of layers can be scrolled horizontal or vertical using ports $10 - $13

   Transparency    - Wonderswan       - if bit 3 on palette number is set - color 0 of that palette is transparent 
                     Wonderswan color - color 0 of each palette is transparent
   Windows         - There are two windows - rectangular areas for disabling /enabling FG layer (FG window) or sprites(Sprite window) 


5. ROM HEADER

 Header taking last 10 bytes of each ROM file.
 Bytes  :
 0   - Developer ID
 1   - Minimum support system 
       00 - WS Mono
       01 - WS Color (!!Important for WSC programs!!)
 2   - Cart ID number for developer defined at byte 0
 3   - ??
 4   - ROM Size
       01 - ?
       02 - 4Mbit
       03 - 8Mbit
       04 - 16Mbit
       05 - ?
       06 - 32Mbit
       07 - ?
       08 - ?
       09 - 128Mbit
 5   - SRAM/EEPROM Size
       00 - 0k
       01 - 64k SRAM
       02 - 256k SRAM
       10 - 1k EEPROM
       20 - 16k EEPROM
 6   - Additional capabilities(?)
       04 - ?? game played in "horizontal" position (most roms)
       05 - ?? game played in "vertical" position
       10 - ?? 
 7   - Always 0
 8,9 - Checksum = sum of all ROM bytes except two last ones ( where checksum is stored)

6. INTERRUPTS 
   The Wonderswan CPU recognizes 7 interrupts from the hardware, these are:
    7 - HBlank
    6 - VBlank begin
    5 - VBlank end
    4 - Drawing line detection
    3 - Serial recieve
    2 - RTC Alarm
    1 - Key press
    0 - Serial transmit
  Whether the CPU should indeed take action when one of these interrupts come in
  is determined by port B2. The above mentioned interrupts correspond with the bit
  numbers of port B2. When an interrupt occurs the corresponding bit of port B6 gets
  set to 1 and, if enabled, an interrupt to the CPU is generated. This bit of port B6
  will have to be cleared through code when the interrupt has been handled.

  Example:
   The Wonderswan is set to react to VBlank begin interrupts. Then bit 7 of B6 is set high
   and keeps the interrupt line high until the CPU is able to take action upon this interrupt.
   A typical VBlank interrupt routine is as follows:
	<push registers>
	<do some useful work>
	out B6,40
	<pop registers>
	iret

   The mentioned interrupts do not correspond with the same interrupt numbers for the vectors
   in the vector table. The base for the actual interrupt numbers is set through port B0. If B0
   is set to 20h then a VBlank begin interrupt routine must be set at vector 26h. (Base is 20h
   and VBlank begin interrupt is 6)
 
7. CONTROLS  -  It's easy to check buttons status reading/writing port $B5(see below).
     There's required some delay between writing and reading  port $B5 ( few NOP-s)           

8. I/O PORTS

 - $00      - DSPCTL - Display control
              bit 0   - background layer on/off
              bit 1   - foreground layer on/off
              bit 2   - sprites on/off
              bit 3   - sprite window on/off (window coords defined in ports $0C - $0F)
              bit 4,5 - fg win inside on/off (window coords defined in ports $08 - $0B)
              Meaning of bits 4 and 5 :
              5 4
              ---
              0 0  FG layer is displayed inside and outside FG window area
              0 1  ?? Unknown
              1 0  FG layer is displayed only inside window
              1 1  FG layer is displayed outside window
 - $01      - BAKCOL -Determines the background color
              bit 0-3 - background color
              bit 4-7 - background palette (WSC only)
 - $02      - Current Raster Line (0 - 159) [0 - 158 ??]
 - $03      - Line compare (for drawning line detection interrupt)
 - $04      - SPRTAB - Determines the base address for the sprite table.
              To get the address of the table, shift this value left 9 times.
              x xxxxxxx0 00000000
 - $05      - SPRFST Determines the number of the sprite to start drawing with
 - $06      - SPRLST Determines the number of the sprite to stop drawing.
 - $07      - SCRLOC - Determines the location of the fore- and back-ground screens in RAM.
               Format: bits 7-0 : ffffbbbb
               bit 7-4 - Determines foreground location (address is 0ffff000 00000000)
              bit 3-0 - Determines background location (address is 0bbbb000 00000000)
 - $08      - x0 of FG window (x0,y0) = top left corner,  (x1,y1) = bottom right corner
 - $09      - y0 of FG window
 - $0A      - x1 of FG window
 - $0B      - y1 of FG window
 - $0C      - x0 of SPR window
 - $0D      - y0 of SPR window
 - $0E      - x1 of SPR window
 - $0F      - y1 of SPR window
 - $10      - BCKSCRX -	Background layer X scroll register
 - $11      - BCKSCRY -	Background layer Y scroll register
 - $12      - FORSCRX - Foreground layer X scroll register
 - $13      - FORSCRY -	Foreground layer Y scroll register
 - $14      - LCDCTL
              bit 0 - 1 - LCD on
                      0 - LCD off
 - $15      - ??
 - $16      - ??
 - $17      - ??
 - $18      - ??
 - $19      - ??
 - $1A      - ??
 - $1B      - ??
 - $1C      - PALCOL10
 - $1D      - PALCOL32
 - $1E      - PALCOL54
 - $1F      - PALCOL76
 - $20      - PAL00
 - $21      - PAL01
 - $22      - PAL10
 - $23      - PAL11
 - $24      - PAL20
 - $25      - PAL21
 - $26      - PAL30
 - $27      - PAL31
 - $28      - PAL40
 - $29      - PAL41
 - $2A      - PAL50
 - $2B      - PAL51
 - $2C      - PAL60
 - $2D      - PAL61
 - $2E      - PAL70
 - $2F      - PAL70
 - $30      - PAL00
 - $31      - PAL01
 - $32      - PAL10
 - $33      - PAL11
 - $34      - PAL20
 - $35      - PAL21
 - $36      - PAL30
 - $37      - PAL31
 - $38      - PAL40
 - $39      - PAL41
 - $3A      - PAL50
 - $3B      - PAL51
 - $3C      - PAL60
 - $3D      - PAL61
 - $3E      - PAL70
 - $3F      - PAL70
 - $40      - DMA (?) copy source address
 - $41      -  ^^^
 - $42      - copy source bank
 - $43      - copy destination bank
 - $44      - copy destination address
 - $45      - ^^^
 - $46      - size of copied datas (in bytes)
 - $47      - ^^^
 - $48      - bit  7 = 1  -> copy start
              (bit 7=0 when data transfer is finished)
              DMA(?) isn't immediate and not stopping
              the main cpu operations (like gbc GDMA)
              ports $40-$48 are updated durning copy process
 - $49      - ??
 - $4A      - ??
 - $4B      - ??
 - $4C      - ??
 - $4D      - ??
 - $4E      - ??
 - $4F      - ??
 - $50      - ??
 - $51      - ??
 - $52      - ??
 - $53      - ??
 - $54      - ??
 - $55      - ??
 - $56      - ??
 - $57      - ??
 - $58      - ??
 - $59      - ??
 - $5A      - ??
 - $5B      - ??
 - $5C      - ??
 - $5D      - ??
 - $5E      - ??
 - $5F      - ??
 - $60      - VMODE - video mode
              Meaning of bits 5-7:
              765
              ---
              111   16 col/tile 'packed' mode   -  tiles like in Genesis, 16 col/tile
              110   16 col/tile 'layered' mode  -  tiles like in GameGear, 16 col/tile
              010    4 col/tile                 -  the same as mono (below) but using color palettes, 4 cols/tile, one tile = 16 bytes, WSC only
              000    4 col/tile mono            -  tiles like in GameBoy,

              [bit 7 = 16/4 color/tile , bit 6 - color/mono mode, bit 4 - 'packed' mode on/off]

 - $61      - ??
 - $62      - ??
 - $63      - ??
 - $64      - ??
 - $65      - ??
 - $66      - ??
 - $67      - ??
 - $68      - ??
 - $69      - ??
 - $6A      - ??
 - $6B      - ??
 - $6C      - ??
 - $6D      - ??
 - $6E      - ??
 - $6F      - ??
 - $70      - ??
 - $71      - ??
 - $72      - ??
 - $73      - ??
 - $74      - ??
 - $75      - ??
 - $76      - ??
 - $77      - ??
 - $78      - ??
 - $79      - ??
 - $7A      - ??
 - $7B      - ??
 - $7C      - ??
 - $7D      - ??
 - $7E      - ??
 - $7F      - ??
 - $80      - ??
 - $81      - ??
 - $82      - ??
 - $83      - ??
 - $84      - ??
 - $85      - ??
 - $86      - ??
 - $87      - ??
 - $88      - ??
 - $89      - ??
 - $8A      - ??
 - $8B      - ??
 - $8C      - ??
 - $8D      - ??
 - $8E      - ??
 - $8F      - ??
 - $90      - ??
 - $91      - ??
 - $92      - ??
 - $93      - ??
 - $94      - ??
 - $95      - ??
 - $96      - ??
 - $97      - ??
 - $98      - ??
 - $99      - ??
 - $9A      - ??
 - $9B      - ??
 - $9C      - ??
 - $9D      - ??
 - $9E      - ??
 - $9F      - ??
 - $A0      - Hardware type
              bit 1 - 1 - color
                      0 - mono
 - $A1      - ??
 - $A2      - ??
 - $A3      - ??
 - $A4      - HBL  0 = no HBLANK Interrupt
                   n = HBLANK Interrupt every n-th line (???)
 - $A5      - HBL counter(?) (for $A4)
 - $A6      - ??
 - $A7      - ??
 - $A8      - ??
 - $A9      - ??
 - $AA      - ??
 - $AB      - ??
 - $AC      - ??
 - $AD      - ??
 - $AE      - ??
 - $AF      - ??
 - $B0      - IRQBASE -	Interrupt Base
 - $B1      - Communication byte
 - $B2      - IRQENA - Interrupt enable
              bit 7 - HBlank
              bit 6 - VBlank begin
              bit 5 - VBlank end
              bit 4 - Drawing line detection
              bit 3 - Serial receive
              bit 2 - RTC Alarm
              bit 1 - Key press
              bit 0 - Serial transmit
 - $B3      - Communication direction
              bit 7 - recieve data interrupt generation
              bit 6 - ?
              bit 5 - send data interrupt generation
              bit 4 - ?
              bit 3 - ?
              bit 2 - ?
              bit 1 - send/recieve complete??
              bit 0 - something happened??
              [HACK - bit 1=1 , bit 2=1 ]
 - $B4      - ??
 - $B5      - Controls
              bit 4-7 : read/write - Select line of inputs to read
               0001 - read vertical cursors
               0010 - read hozizontal cursors
               0100 - read buttons
              bit 0-3 : read only - Read the current state of the input lines (positive logic) after
               having written 10h,20h, or 40h.
              Meaning of the bits when reading cursors:
               bit 0 - cursor up
               bit 1 - cursor right
               bit 2 - cursor down
               bit 3 - cursor left
              Meaning of the bits when reading buttons:
               bit 0 - ?
               bit 1 - START
               bit 2 - A
               bit 3 - B
 - $B6      - IRQACK - Interrupt Acknowledge
               bit 7 - HBlank
               bit 6 - VBlank begin
               bit 5 - VBlank end
               bit 4 - Drawing line detection
               bit 3 - Serial receive
               bit 2 - RTC Alarm
               bit 1 - Key press
               bit 0 - Serial transmit
 - $B7      - ??
 - $B8      - ??
 - $B9      - ??
 - $BA      - ??
 - $BB      - ??
 - $BC      - ??
 - $BD      - ??
 - $BE      - ?? [HACK random value($00-$FF)]
 - $BF      - ??
 - $C0      - ??
 - $C1      - SRAM/EEPROM selector
               $00 - enables & selects bank 00
               $FF - disables SRAM/EEPROM
 - $C2      - BNK2SLCT - ROM Bank selector for segment 2
 - $C3      - BNK3SLCT - ROM Bank selector for segment 3
 - $C4      - ??
 - $C5      - ??
 - $C6      - ?? [HACK random value($00-$07)]
 - $C7      - ??
 - $C8      - ?? [HACK random value of bit 1]
 - $C9      - ??
 - $CA      - ?? [HACK bit 7=1]
 - $CB      - ?? [HACK bit 7=1]
 - $CC      - ??
 - $CD      - ??
 - $CE      - ??
 - $CF      - ??
 - $D0      - ??
 - $D1      - ??
 - $D2      - ??
 - $D3      - ??
 - $D4      - ??
 - $D5      - ??
 - $D6      - ??
 - $D7      - ??
 - $D8      - ??
 - $D9      - ??
 - $DA      - ??
 - $DB      - ??
 - $DC      - ??
 - $DD      - ??
 - $DE      - ??
 - $DF      - ??
 - $E0      - ??
 - $E1      - ??
 - $E2      - ??
 - $E3      - ??
 - $E4      - ??
 - $E5      - ??
 - $E6      - ??
 - $E7      - ??
 - $E8      - ??
 - $E9      - ??
 - $EA      - ??
 - $EB      - ??
 - $EC      - ??
 - $ED      - ??
 - $EE      - ??
 - $EF      - ??
 - $F0      - ??
 - $F1      - ??
 - $F2      - ??
 - $F3      - ??
 - $F4      - ??
 - $F5      - ??
 - $F6      - ??
 - $F7      - ??
 - $F8      - ??
 - $F9      - ??
 - $FA      - ??
 - $FB      - ??
 - $FC      - ??
 - $FD      - ??
 - $FE      - ??
 - $FF      - ??
