;  Title - Astro BASIC 4x2 Multicart Loader
;  By -    Michael D. White - Summer 2001
;
;         1.4 - December 6, 2016
;             - Fixed to work with recent versions of MESS/MAME Emulator.
;               Changed Interrupt Mode from $18 to $08 (screen
;               interrupts only).
;         1.3 - November 30, 2003
;         1.2 - February 6, 2002
;         1.0 - January 26, 2002
;             - ZMAC conversion and some comments added by Adam Trionfo 
; 
;     Mike... thanks for this great program!  I don't think
;  the multicart users know you did this...
;  
;  About this Program:
;
;     Michael wrote this program on paper, then used the
;  'Blue Ram Utility' to enter it into the Astrocade- in
;  other words this program was assembled by-hand.  As ref-
;  erence he used Rodney Zaks HOW TO PROGRAM THE Z80 and
;  "The Z80 Minicourse," both of which he highly recommends
;  for any Astrocade programmer.
;
;     This program must be changed slightly for each BASIC
;  program that is going to be on a cartridge.  Michael
;  notes these cartridge ROM addresses:
;     $2001        Next Menu Link
;     $2007-$2FA6  Basic Text
;     $2FA7
;     $2FB8
;     $2FBC        &(10) [Vertical Blanking Line]
;     $2FBD        &(9) [Left/Right Color Boundary]
;     $2FC9
;     $2FCD
;     $2FD5
;     $2FFF        Color Table, Program 1
;     $3003-$3006  Colors for registers 4-7, Program 1
;     $3007-$3FA6  BASIC Text
;     $3EA7
;     $3FAB        &(10) [Vertical Blanking Line]
;     $3FAC        &(9) [Left/Right Color Boundary]
;     $3FBB        Next Menu Link
;     $3FBD        Title Link
;     $3FC1        Color table, Program 2
;     $3FC5-$3FC8  Colors for registers 4-7, Program 2
;     $3FC9        Beginning of 'switch' string
;     $3FE2        Title, Program 1
;     $????        Title, Program 2
;  
;
;     This file will assemble with ZMAC 1.3 (a little known,
;  freely distributable Z-80 assembler (with C source), that
;  has a 25-year history.  ZMAC can be compiled under just
;  about any O.S. in existence, so try it out. 
;  
; To assemble Z-80 source code using ZMAC:
;  
; zmac -d -i -m -o <outfile> -x <listfile> <filename>
;  
; For example, to assemble this Astrocade Z-80 program:
; 
; zmac -i -m -o multcart.bin -x multcart.lst multcart.asm 
;
INCLUDE "HVGLIB.H"

        ORG    FIRSTC   ; First byte of Cartridge
        DB     "U"      ; User Cartridge Sentinel

  ; Menu Selection Choice #1
        DW     $3FBB    ; Next Menu Link
        DW     $3FE2    ; Address of Title for Prg. #1
        DW     $2FB8    ; Jump Here if Prg. #1 is selected
 
  ; BASIC TEXT: $2007 - $2FA6

        ORG    $2FA7
  ; Cartridge Swap Loop
SWAP:   IN     A,($14)    ; Keypad Column #4, "="
        OR     A
        JR     Z,SWAP

        EI   
        CALL   $2D06
        LD     DE,$4EBD   ; Line Buffer
        PUSH   DE
        DEC    DE
        JP     $2563      ; Jump to Astro BASIC

        DI
        SYSTEM (INTPC)

        DO     (SETOUT)
        DB     $B0        ; Vertical Blanking Line 
        DB     $2C        ; Left/Right Color Boundary 
        DB     00001000B  ; Set Bit 3 of INterrupt MODe (Screen Interrupts Only)

        DO     (COLSET)
        DW     $2FFF      ; Color Table #1

        DO     (MOVE)
        DW     $4000      ; Destination
        DW     $0FA0      ; Bytes to move
        DW     $2007      ; Source Address
        EXIT

        EI
        SYSSUK (PAWS)
        DB     $7F        ; Number of Interrupts to Wait
        DI

  ; Clear screen 
        LD     SP,$4FEA   ; Set stack for Astro BASIC
        LD     HL,$4000
CLEAR:  LD     A,$55      ; Mask - '01010101'
        AND    (HL)
        LD     (HL),A
        INC    HL
        LD     A,$4E      ; Trip Out
        CP     H
        JR     NZ,CLEAR

        SYSSUK (STRDIS)
        DB     $14        ; X position
        DB     $20        ; Y position
        DB     $28        ; Magic Byte
        DW     $3FC9      ; Address of String

        IM     2          ; Set Interrupt Control
        LD     A,$20      ; Vector set
        LD     I,A        ; Set MSB
        LD     A,$22      ; Set LSB
        OUT    ($0D),A    ; Interrupt Feedback
        LD     A,$C8      ; Bottom line
        OUT    ($0F),A    ; Interrupt Line

        SYSSUK (MOVE)
        DW     $4FA0      ; Destination Address
        DW     $0011      ; Number of Bytes to Transfer
        DW     $2FA7      ; Source Address

        JP     $4FA0      ; Swap Loop

  ; Color Table #1
        DB     $00        ; Color 0 Right
        DB     $00        ; Color 1 Right 
        DB     $07        ; Color 2 Right 
        DB     $07        ; Color 3 Right 
        DB     $5A        ; Color 0 Left
        DB     $84        ; Color 1 Left 
        DB     $06        ; Color 2 Left 
        DB     $00        ; Color 3 Left 

  ; BASIC TEXT: $3007 - $3FA6

        ORG    $3FA7
        DI
        SYSTEM (INTPC)     ; Init Multiple Subroutine Calls

        DO     (SETOUT)
        DB     $B0        ; Vertical Blanking Line 
        DB     $2C        ; Left/Right Color Boundary 
        DB     00001000B  ; Set Bit 3 of INterrupt MODe (Screen Interrupts Only)

        DO     (COLSET)
        DW     $3FC1      ; Color Table #2

        DO     (MOVE)
        DW     $4000      ; Destination Address
        DW     $0FA0      ; Number of Bytes to Transfer
        DW     $3007      ; Source Address

        DO     (MJUMP)
        DW     $2FC9      ; Address of Jump (exit)

  ; Menu Selection Choice #2
        DW     MENUST     ; Next Menu Link
        DW     $3FF0      ; Address of Title for Prg. #2
        DW     $3FA7      ; Jump Here if Prg. #2 is selected

  ; Color Table #2
        DB     $00        ; Color 0 Right 
        DB     $00        ; Color 1 Right
        DB     $07        ; Color 2 Right 
        DB     $07        ; Color 3 Right 
        DB     $5A        ; Color 0 Left
        DB     $84        ; Color 1 Left 
        DB     $06        ; Color 2 Left 
        DB     $00        ; Color 3 Left 

        DB     'S','W','I','T','C','H'
        DB     ' ','T','O',' ','"','A','B','"'
        DB     $6B        ; Change
        DB     $2D        ; X position
        DB     $30        ; Y position
        DB     'P','R','E','S','S',' ','='
        DB     $00        ; End

        ORG    $3FE2      ; Title, Program 1
        DB     'T','I','T','L','E',' ','O','N','E'
        DB     $00        ; End 

        ORG    $3FF0      ; Title, Program 2
        DB     'T','I','T','L','E',' ','T','W','O'
        DB     $00        ; End 

        ORG    $3FFF
        DB     $00        ; End - Last Byte
