; ***************************************************************************
; * CP/M BIOS Loader Definitions for ZX Spectrum Next                       *
; ***************************************************************************


; ***************************************************************************
; * XDPB layout                                                             *
; ***************************************************************************

dpb_spt                 equ     $00
dpb_bsh                 equ     $02
dpb_blm                 equ     $03
dpb_exm                 equ     $04
dpb_dsm                 equ     $05
dpb_drm                 equ     $07
dpb_al0                 equ     $09
dpb_al1                 equ     $0a
dpb_cks                 equ     $0b
dpb_off                 equ     $0d
dpb_psh                 equ     $0f
dpb_phm                 equ     $10

DPB_SIZE                equ     $11

xdpb_flags1             equ     $11
xdpb_tks                equ     $12
xdpb_secs               equ     $13
xdpb_sec1               equ     $14
xdpb_secsize            equ     $15
xdpb_gaprw              equ     $17
xdpb_gapformat          equ     $18
xdpb_mfm                equ     $19
xdpb_freeze             equ     $1a
xdpb_flags2             equ     $1b
xdpb_letter             equ     $1c
xdpb_unit               equ     $1d
xdpb_access             equ     $1e
xdpb_nfiles             equ     $21
xdpb_ndirs              equ     $22
xdpb_dirlast            equ     $24
xdpb_cksvec             equ     $26
xdpb_alvec              equ     $28
xdpb_login              equ     $2a
xdpb_readsec            equ     $2c
xdpb_writesec           equ     $2e


; ***************************************************************************
; * SCB Layout                                                              *
; ***************************************************************************

SCB_LOW_OFFSET  equ     $9c             ; offset of SCB in SCB page

o_dateflag      equ     $0c             ; date format flag (byte)
o_curdrv        equ     $13             ; current drive (byte)
o_conwidth      equ     $1a             ; console width (byte)
o_conpage       equ     $1c             ; console length (byte)
o_civec         equ     $22             ; con in redirection vector (word)
o_covec         equ     $24             ; con out redirection vector (word)
o_aivec         equ     $26             ; aux in redirection vector (word)
o_aovec         equ     $28             ; aux out redirection vector (word)
o_lovec         equ     $2a             ; lst out redirection vector (word)
o_ctlh_act      equ     $2e             ; backspace action (byte)
o_rubout_act    equ     $2f             ; rubout action (byte)
o_bnkbf         equ     $35             ; add of 128-byte banked buf (word,r/o)
o_crdma         equ     $3c             ; current dma addr (word,r/o)
o_crdsk         equ     $3e             ; current disk (byte,r/o)
o_vinfo         equ     $3f             ; BDOS variable "INFO" (word,r/o)
o_resel         equ     $41             ; FCB flag (byte,r/o)
o_fx            equ     $43             ; BDOS fn for error msgs (byte,r/o)
o_usrcd         equ     $44             ; current user code (byte,r/o)
o_mltio         equ     $4a             ; current multisector cnt (byte,r/w)
o_ermde         equ     $4b             ; BDOS error mode (byte,r/o)
o_erdsk         equ     $51             ; BDOS error disk (byte,r/o)
o_media         equ     $54             ; set by BIOS indicate open door (byte,r/w)
o_bflgs         equ     $57             ; BDOS msg size flag (byte,r/o)
                                        ; bit 7=long errors (if set)
                                        ; bit 6=single alloc vectors (if set)
o_date          equ     $58             ; date in days since 1/1/78 (word,r/w)
o_hour          equ     $5a             ; hour in BCD (byte,r/w)
o_min           equ     $5b             ; minute in BCD (byte,r/w)
o_sec           equ     $5c             ; second in BCD (byte,r/w)
o_bnktop        equ     $5e             ; high byte of commonbase
o_erjmp         equ     $5f             ; BDOS err msg jump (3 bytes,r/w)
o_mxtpa         equ     $62             ; top of TPA (addr at 6/7) (word,r/o)

; ***************************************************************************
; * RAM page allocations                                                    *
; ***************************************************************************
; CP/M bank 0 uses the +3 All-RAM configuration: 4,5,6,3
; CP/M bank 1 uses the +3 All-RAM configuration: 0,1,2,3

BANK0LOW_RAMPAGE        equ     4
BANK0MID_RAMPAGE        equ     5
BANK0TOP_RAMPAGE        equ     6

BANK1LOW_RAMPAGE        equ     0
BANK1MID_RAMPAGE        equ     1
BANK1TOP_RAMPAGE        equ     2

COMMON_RAMPAGE          equ     3

PAGEZERO_BANKL          equ     BANK1LOW_RAMPAGE*2+0
PAGEZERO_BANKU          equ     BANK1LOW_RAMPAGE*2+1

COMMON_BANKL            equ     COMMON_RAMPAGE*2+0
COMMON_BANKU            equ     COMMON_RAMPAGE*2+1
;
; RAM 7 is reserved for +3DOS use

P3DOS_RAMPAGE           equ     7
P3DOS_BANKL             equ     P3DOS_RAMPAGE*2+0
P3DOS_BANKU             equ     P3DOS_RAMPAGE*2+1

; RAM 8 is reserved for NextZXOS use, so 9 is the next available
; (normally used by layer2 which CP/M doesn't require).

CCP_RAMPAGE             equ     9
CCP_BANKL               equ     CCP_RAMPAGE*2+0
CCP_BANKU               equ     CCP_RAMPAGE*2+1

; Standard bindings when not in All-RAM configuration
MMU0_DEFAULT            equ     $ff
MMU1_DEFAULT            equ     $ff
MMU2_DEFAULT            equ     5*2+0
MMU3_DEFAULT            equ     5*2+1
MMU4_DEFAULT            equ     2*2+0
MMU5_DEFAULT            equ     2*2+1
MMU6_DEFAULT            equ     0*2+0
MMU7_DEFAULT            equ     0*2+1


; ***************************************************************************
; * Notes on allocations                                                    *
; ***************************************************************************
; All standard +3 .DSK files have a max of 64 directory entries, and less than
; 200 blocks. Non-standard .DSK files are unlikely to exceed 64 directory
; entries due to a bug in +3 BASIC and cannot exceed 2048 blocks (max possible
; using 0.5K blocks on a 1MB floppy disk).
;
; .P3D files are always created with 512 directory entries, and up to 2048
; blocks (ensuring that 1-bit allocation vectors never exceed 256 bytes for
; +3DOS).

; Therefore, the Next CP/M BIOS assumes that the maximum values are
; 512 directory entries and 2048 blocks, and this is enforced by the
; .CPM dot command loader.

MAX_BLOCKS              equ     2048
MAX_DIRENTS             equ     512

SECTOR_SIZE             equ     512
DPH_SIZE                equ     25

NUM_DRIVES              equ     16
NUM_DTABUFS             equ     2


; ***************************************************************************
; * Common memory layout                                                    *
; ***************************************************************************
; Common memory is located at the top of the memory space, in RAM3
; (since either the 4,5,6,3 or 0,1,2,3 All-RAM configurations are in use).

DTABUF_BASE             equ     $ffff-(NUM_DTABUFS*SECTOR_SIZE)+1

DPB_BASE                equ     DTABUF_BASE-(NUM_DRIVES*DPB_SIZE)
DRVTBL_BASE             equ     DPB_BASE-(NUM_DRIVES*2)
XDPBTBL_BASE            equ     DRVTBL_BASE-(NUM_DRIVES*2)
MAPTBL_BASE             equ     XDPBTBL_BASE-NUM_DRIVES
DOSTBL_BASE             equ     MAPTBL_BASE-NUM_DRIVES

BIOS_TOP                equ     DOSTBL_BASE&$ff00

; Starting page of common memory
COMMON_PAGE             equ     $c0


; ***************************************************************************
; * Address of MF3 exit routine in page zero                                *
; ***************************************************************************

CPM_MF3_EXIT            equ     $0042


; ***************************************************************************
; * RAM 5 BIOS Extension Header                                             *
; ***************************************************************************

MAX_BIOS_EXT_SIZE       equ     $1100   ; BIOSHDR_SIZE will not exceed this

BIOSHDR_LENGTH          equ     32

BIOSHDR_SIGNATURE       equ     $0000   ; "BX"
BIOSHDR_SIZE            equ     $0002   ; size of BIOS extension
BIOSHDR_ORG             equ     $0004   ; address to load BIOS extension
BIOSHDR_DPHS            equ     $0006   ; address of DPHs
BIOSHDR_FONTADDR_NORM   equ     $0008   ; address to load normal font
BIOSHDR_VTERM_INIT      equ     $000a   ; VTERM initialisation routine
BIOSHDR_VTERM_OUT       equ     $000c   ; VTERM output routine
BIOSHDR_SP              equ     $000e   ; suitable stack pointer
BIOSHDR_COPYRIGHT       equ     $0010   ; copyright signon message
BIOSHDR_LOADERVER       equ     $0012   ; min loader version required
BIOSHDR_FONTADDR_UNDR   equ     $0014   ; address to load underline font
BIOSHDR_FONTADDR_ITAL   equ     $0016   ; address to load italic font
BIOSHDR_FONTADDR_ITUN   equ     $0018   ; address to load italic underline font
