;*******************************************************************************
; ProSystem Memory Controller 1                                                *
;*******************************************************************************
; - 2MB of ROM
;   \- 16KB Paged Segment
;    - 8KB Paged Segment
;    - 8KB Fixed Segment
; - 256KB of EXRAM
;   \- Segmented 4KB + 4KB + 8KB Mode
;    - Display List Memory (DLM) Mode
; - SPI Controller
;   \- Two Slave Devices
;    - Mode 0
; - Maria Fetch Trapping
; - Raster Interrupt
; - BupChip Expansion Audio *
;-------------------------------------------------------------------------------


;-------------------------------------------------------------------------------
; Memory Map (Sally)                                                           /
;------------------------------------------------------------------------------
; Sally is presented with the following memory arrangement by the PMC1...
;
;               | $xx00        | $xx40        | $xx80        | $xxC0
;---------------------------------------------------------------------------
; $00xx - $3Fxx |...........................................................
;---------------|-----------------------------------------------------------
; $40xx - $4Fxx | EXRAM (4KB, EXRAM_V_SEL) or DLM (4KB, DSX/DSY + BGL/FGL)
; $50xx - $5Fxx | EXRAM (4KB, EXRAM_A_SEL)
; $60xx - $7Fxx | EXRAM (8KB, EXRAM_B_SEL) or SPI_DATA
;---------------|-----------------------------------------------------------
; $80xx - $BFxx | Paged ROM (16KB, BANK_B_SEL)
; $C0xx - $DFxx | Paged ROM (8KB, BANK_S_SEL)
; $E0xx - $FFxx | Fixed ROM (8KB, BANK_F_SEL / END)
;---------------------------------------------------------------------------
;
; This may be adjusted during execution through MODE_SEL and SPI_CTRL - which
; adjust the Display List Memory (DLM) feature and SPI Controller's presence.
;
;
;---------------------------------------
; Display List Memory (DLM)
;=======================================
; Setting MODE,1 (MODE_DLM) will enable the Display List Memory feature, this
; replaces the EXRAM_V segment in $4000 - $4FFF with a new memory arrangement
; to simplify the manipulation of Display List (List) structures...
;
;               | $xx00        | $xx40        | $xx80        | $xxC0
;---------------------------------------------------------------------------
; $40xx - $40xx | EXRAM (128B, DSX_SEL)       | EXRAM (128B, DSY_SEL)
; $41xx - $4Fxx | EXRAM (128B x 15, BGL_SEL)  | EXRAM (128B x 15, FGL_SEL)
;---------------|-----------------------------------------------------------
;
; Both DSL_SEL and DSY_SEL are used to select which 128 Byte strips of memory
; appear in $4000 - $407F (DSX) and $4080 - $40FF (DSY), these must reside in
; the first 32KB of EXRAM due to sizing limits. The intention here is for one
; strip to hold a Display List List (DLL) and the other used for manipulation
; of the Background Display List (BGL) and Foreground Display List (FGL).
;
; BGL_SEL and FGL_SEL are used to pick a 128 Byte wide and 15 Entry tall page
; of memory at $4100 and $4180, and are intended to hold Display Lists (DLs).
;
; These are arranged such that a Display List would start in BGL (which holds
; any background graphics) - then cross over to FGL which contains foreground
; draws such as characters or overlays. The 128 Byte "holes" between each BGL
; or FGL segment may be used to store DLLs.
;
; The state of EXRAM_V is unaffected by switching into DLM mode or writing to
; any of its pertinent registers (DSX_SEL, DSY_SEL, ...) - allowing software
; to flip between managed DLM and unmanaged DL(L)s in this segment.
;
;
;---------------------------------------
; SPI Controller
;=======================================
; The built-in SPI Controller supports two devices in Mode 0 - these could be
; large NOR Flashes containing game resources or small EEPROMs for save data.
;
; Setting SPI_CTRL,7 (SPI_VIEW) replaces the EXRAM_B segment in $6000 - $7FFF
; with SPI_DATA - a one byte register mirrored over the entire range. Reading
; yields the last byte returned over MISO while writing instigates a transfer
; and shifts the value written out over MOSI. A single exchange is eight PHI2
; clocks and full-duplex.
;
; Devices are selected using SPI_CTRL,1-0 (SPI_DEV_A/B).
;
; Disabling the SPI Controller viewport during an exchange will not interfere
; with pending data - it'll be available once the viewport is enabled again.
;
;
;---------------------------------------
; Raster Comparator
;=======================================
; Setting MODE,7 (MODE_RCI) enables the Raster Comparator Interrupt.
;
; This isn't a vanilla line counter - rather a line comparator which supports
; defining the start and size of its active region. This design is intended
; to simplify sharing of line tables between NTSC and PAL.
;
; RLC_START is used to define the first line of the RLC region, counting from
; the scanline Maria begins rendering on, RLC_SIZE then defines how large the
; region is in scanlines. Both of these should only be updated in VBL.
;
; Writing to RLC_TARGET selects which line (starting from zero) in the region
; is set to trigger the Raster Interrupt, and also acknowledges any currently
; pending raster interrupts. Setting RLC_TARGET to a line outside the defined
; comparator region will cause the interrupt to never trigger and can be used
; to "terminate" a frame.
;---------------------------------------
EXRAM_V		EQU $4000
EXRAM_V_BASE	EQU $4000
EXRAM_V_SIZE	EQU $1000
EXRAM_V_TOP	EQU (EXRAM_V_BASE + EXRAM_V_SIZE)

DLM		EQU $4000
DSX		EQU $4000
DSY		EQU $4080
BGL		EQU $4100
FGL		EQU $4180

EXRAM_A		EQU $5000
EXRAM_A_BASE	EQU $5000
EXRAM_A_SIZE	EQU $1000
EXRAM_A_TOP	EQU (EXRAM_A_BASE + EXRAM_A_SIZE)

EXRAM_B		EQU $6000
EXRAM_B_BASE	EQU $6000
EXRAM_B_SIZE	EQU $2000
EXRAM_B_TOP	EQU (EXRAM_B_BASE + EXRAM_B_SIZE)

SPI_DATA	EQU $6000


BANK_B		EQU $8000
BANK_B_BASE	EQU $8000
BANK_B_SIZE	EQU $4000
BANK_B_TOP	EQU (BANK_B_BASE + BANK_B_SIZE)

BANK_S		EQU $C000
BANK_S_BASE	EQU $C000
BANK_S_SIZE	EQU $2000
BANK_S_TOP	EQU (BANK_S_BASE + BANK_S_SIZE)

BANK_F		EQU $E000
BANK_F_BASE	EQU $E000
BANK_F_SIZE	EQU $2000
BANK_F_TOP	EQU (BANK_F_BASE + BANK_F_SIZE)


;-------------------------------------------------------------------------------
; Memory Map (Maria)                                                           /
;------------------------------------------------------------------------------
; Maria's view of memory is identical to Sally's until MODE,0 (MODE_MFT) is set,
; this enables Maria Fetch Trapping and repartitions her arrangement to...
;
;               | $xx00        | $xx40        | $xx80        | $xxC0
;---------------------------------------------------------------------------
; $00xx - $3Fxx |...........................................................
;---------------|-----------------------------------------------------------
; $40xx - $4Fxx | EXRAM (4KB, EXRAM_V_SEL) or DLM (4KB, DSX/DSY + BGL/FGL)
;---------------|-----------------------------------------------------------
; $50xx - $5Fxx | BGX_C        | BGX_D        | BGX_E        | BGX_F
;---------------|-----------------------------------------------------------
; $60xx - $6Fxx | BGX_A                       | BGX_B
;---------------|-----------------------------------------------------------
; $70xx - $7Fxx | BGX_W
;---------------|-----------------------------------------------------------
; $80xx - $8Fxx | BGR_W
;---------------|-----------------------------------------------------------
; $A0xx - $AFxx | CHX_C        | CHX_D        | CHX_E        | CHX_F
;---------------|-----------------------------------------------------------
; $C0xx - $CFxx | CHX_A                       | CHX_B
;---------------|-----------------------------------------------------------
; $E0xx - $EFxx | CHR_A                       | CHR_B
;---------------------------------------------------------------------------
;
; EXRAM_V or DLM in $4000 - $4FFF are static, see "Display List Memory (DLM)"
; for more info on this segment.
;
; The BGX and BGR segments live below Maria's Holey DMA region - intended for
; aligned graphics in EXRAM or ROM such as backgrounds or static overlays.
;
; Both CHX and CHR fall in Holey DMA regions and are ideal for unaligned draw
; items such as characters or foreground objects in EXRAM (CHX) or ROM (CHR).
;---------------------------------------
BGX_W		EQU $7000
BGX_A		EQU $6000
BGX_B		EQU $6080
BGX_C		EQU $5000
BGX_D		EQU $5040
BGX_E		EQU $5080
BGX_F		EQU $50C0

CHX_A		EQU $C000
CHX_B		EQU $C080
CHX_C		EQU $A000
CHX_D		EQU $A040
CHX_E		EQU $A080
CHX_F		EQU $A0C0

BGR_W		EQU $8000
CHR_A		EQU $E000
CHR_B		EQU $E080


;-------------------------------------------------------------------------------
; Registers                                                                    /
;------------------------------------------------------------------------------
; The PMC1's registers are accessed by writing anywhere from $8000 - $BFFF and
; repeat every page (256 Byte) boundary.
;
; Only the defined set from $8000 - $80FF should be used and undefined bits in
; all registers zeroed in order to ensure compatibility.
;---------------------------------------
BANK_B_SEL	EQU $8000
; -/W
; Paged ROM Bank Select (%xBBBBBBB)
; Select the 16KB ROM page visible in $8000 - $BFFF.


BANK_S_SEL	EQU $8001
; -/W
; Paged ROM Bank Select (%BBBBBBBB)
; Select the 8KB ROM page visible in $C000 - $DFFF.


SPI_CTRL	EQU $8002
SPI_VIEW	EQU $80
SPI_DEV_B	EQU $02
SPI_DEV_A	EQU $01
; -/W
; SPI Controller (%VxxxxxBA, '1' == Enable)
; 7 : View SPI_DATA in $6000 - $7FFF
; 6 - 2 : ???
; 1 : Device B Select
; 0 : Device A Select


MODE_SEL	EQU $8003
MODE_RCI	EQU $80
MODE_DLM	EQU $02
MODE_MFT	EQU $01
; -/W
; Operating Mode (%RxxxxxDF, '1' == Enable)
; 7 : Raster Compare Interrupt
; 6 - 2 : ???
; 1 : Display List Memory (DLM) in $4000 - $4FFF
; 0 : Maria Fetch Trapping


DSX_SEL		EQU $8004
DSY_SEL		EQU $8005
; -/W
; Display List (List) Strip Select (%BBBXYYYY)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ BBBXYYYY : DSX/DSY (!)
; ..... 01000000 !LLLLLLL : ADDRESS
; -----------------------
; 00000 0BBBYYYY XLLLLLLL : EXRAM


BGL_SEL		EQU $8006
FGL_SEL		EQU $8007
; -/W
; Background and Foreground Display List Selects (%xxxxBBBX)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ ....BBBX : BGL/FGL (!)
; ..... 0100HHHH !LLLLLLL : ADDRESS
; -----------------------
; 00000 0BBBHHHH XLLLLLLL : EXRAM


EXRAM_V_SEL	EQU $8008
; -/W
; EXRAM 'V' Segment Page Select (%xxBBBBBB)
; Select the 4KB EXRAM page visible in $4000 - $4FFF.


EXRAM_A_SEL	EQU $800A
; -/W
; EXRAM 'A' Segment Page Select (%xxBBBBBB)
; Select the 4KB EXRAM page visible in $5000 - $5FFF.


EXRAM_B_SEL	EQU $800B
; -/W
; EXRAM 'B' Segment Page Select (%xxxBBBBB)
; Select the 8KB EXRAM page visible in $6000 - $7FFF.


RLC_START	EQU $800C
; -/W
; Raster Comparator Starting Line


RLC_LENGTH	EQU $800D
; -/W
; Raster Comparator Region Size


RLC_TARGET	EQU $800E
; -/W
; Raster Comparator Target


AUDIO_CMD	EQU $800F
; -/W
; Audio Command
; Written value is sent to the BupChip.


CHR_A_SEL	EQU $8010
CHR_B_SEL	EQU $8012
; -/W
; ROM Character 'A/B' Page Selects (%BBBBBBBS)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ BBBBBBBS : CHR_A/B (!)
; ..... 111xHHHH !LLLLLLL : ADDRESS
; 00BBB BBBBHHHH SLLLLLLL = ROM


BGR_W_SEL	EQU $8014
; -/W
; ROM Background Graphic 'W' Page Select (%xBBBBBBB)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ .BBBBBBB : BGR_W
; ..... 100xHHHH LLLLLLLL : ADDRESS
; 00BBB BBBBHHHH LLLLLLLL = ROM


CHX_A_SEL	EQU $8018
CHX_B_SEL	EQU $8019
; -/W
; EXRAM Character 'A/B' Page Selects (%xBBBBBBS)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ .BBBBBBS : CHX_A/B (!)
; ..... 110xHHHH !LLLLLLL : ADDRESS
; 000BB BBBBHHHH SLLLLLLL = EXRAM


CHX_C_SEL	EQU $801C
CHX_D_SEL	EQU $801D
CHX_E_SEL	EQU $801E
CHX_F_SEL	EQU $801F
; -/W
; EXRAM Character 'C/D/E/F' Page Selects (%BBBBBBSS)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ BBBBBBSS : CHX_C/D/E/F (!!)
; ..... 101xHHHH !!LLLLLL : ADDRESS
; 000BB BBBBHHHH SSLLLLLL = EXRAM


BGX_A_SEL	EQU $8020
BGX_B_SEL	EQU $8021
; -/W
; EXRAM Background Graphic 'A/B' Page Selects (%xBBBBBBS)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ .BBBBBBS : BGX_A/B (!)
; ..... 0110HHHH !LLLLLLL : ADDRESS
; 000BB BBBBHHHH SLLLLLLL = EXRAM


BGX_C_SEL	EQU $8024
BGX_D_SEL	EQU $8025
BGX_E_SEL	EQU $8026
BGX_F_SEL	EQU $8027
; -/W
; EXRAM Background Graphic 'C/D/E/F' Page Selects (%BBBBBBSS)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ BBBBBBSS : BGX_C/D/E/F (!!)
; ..... 0101HHHH !!LLLLLL : ADDRESS
; 000BB BBBBHHHH SSLLLLLL = EXRAM


BGX_W_SEL	EQU $8028
; -/W
; EXRAM Background Graphic 'W' Page Select (%xxBBBBBB)
;
; 11111
; 43210 FEDCBA98 76543210
; ..... ........ ..BBBBBB : BGX_W
; ..... 0111HHHH LLLLLLLL : ADDRESS
; 000BB BBBBHHHH LLLLLLLL = EXRAM




FLASH_UNLOCK	EQU $8080
; -/W
; Flash Write Unlock
; Writing the ASCII sequence "WR" ($57,$52) will unlock the flash for write
; access in $E000 - $FFFF and allow selecting an 8KB ROM page with BANK_F_SEL.


FLASH_PROTECT	EQU $8081
; -/W
; Flash Write Protect
; Any value disables write access to the flash and sets BANK_F to $FF.


BANK_F_SEL	EQU $8082
; -/W
; Fixed ROM Bank Select (%BBBBBBBB)
; Select the 8KB ROM page visible in $E000 - $FFFF, this can only be altered
; after a flash unlock procedure.
