; Critter - For the Bally Astrocade
; File Name: CRITTERP.ASM
; Version 1.3 - February 12, 2002
;
; This source is as it appears in the PEEK N' POKE
; manual.  It has only been changed so that it
; assembles under Zmac; the Z80 code it generates is
; EXACTLY the same.
; When assembled, this will NOT run as a cartridge!
; Critter does not run without the first version of
; Bally BASIC; Critter does NOT run under Astro BASIC
; (it needs vector modifications).
;
; Compile with:
; zmac -i -m -o critterp.bin -x critterp.lst critterp.asm

MCALL   EQU  $06
MRET    EQU  $08
VECT    EQU  $3E

        ORG  $4C80
        DI
        EXX
        LD   A,$4C
        LD   I,A   ;Load I with page of interrupt vector
        LD   A,$E0
        OUT  ($0D),A    ; Load custom chips with line of
        EXX             ; interrupt vector
        EI
        RET

        ORG  $4CE0
        DW   $4CE3      ; Points to interrupt routine
        
        ORG  $4CE3
        CALL $20B0      ; Call Bally's interrupt routine
        DI
        LD   ($4C70),SP ; Save SP
        LD   SP,$4C70   ; Move SP
        PUSH AF
        PUSH BC
        PUSH DE
        PUSH HL
        PUSH IX
        PUSH IY
        IN   A,($1C)    ; Get KN(1) value
        LD   ($4D3A),A  ; Place in vector block
        RST  $38        ; On-board call
        DB   $00      ;Routine 01 - Start Multiple Calls
        DB   MCALL+1    ; Call V Write Routine
        DW   VWRITE
        DB   VECT+1     ; Move vector (see ROM manual)
        DW   VBLOCK     ; Vector Block Address
        DW   LIMITS     ; Limit Table
        DB   MCALL+1    ; Call V Write Routine
        DW   VWRITE
        DB   $02       ; Routine 02 - End Multiple Calls
        POP  IY
        POP  IX
        POP  HL
        POP  DE
        POP  BC
        POP  AF
        LD   SP,($4C70) ; Return SP
        EI
        RET

        ORG  $4D18
VWRITE: DB   $1F        ; Routine 30 - VWRITR
        DW   $4D38      ; Vector
        DW   $4D24      ; Pattern
        DB   MRET

        ORG  $4D20
; Limit Table
; Note: This part of the source doesn't match the ML
;       listing.  I had to fudge it to make it match;
;       because of this, I have typed, but commented
;       out, the four lines of 'weird' ML listing.
;       Notice that the assembly hex codes are reverse
;       ordered; this is how the manual lists the lines.
; 00 98      DW   152    ; X boundaries
; 00 40      DW   64     ; Y boundaries
; 00 00      DW   0      ; (0,0) Position
; 02 08      DW   520    ; 2 byte, 8 line pattern size

LIMITS: DB   00D,152D      ; X boundaries
        DB   00D,64D       ; Y boundaries
        DB   00D,00D       ; (0,0) Position
        DB   $02,$08       ; 2 byte, 8 line pattern size

; Critter pattern
        DW   $A00A      
        DW   $8822
        DW   $AAAA
        DW   $A82A
        DW   $2008
        DW   $0820
        DW   $2008
        DW   $0000

; VECTOR BLOCK (See 'Nutting' ROM Manual, page 39-41)
VBLOCK: DB     $20     ; Magic Register value
        DB     $80     ; Vector Status
        DB     $00     ; Time Base - Holds KN(1) value
        DW     $0005   ; Delta X
        DW     $0000   ; X Position
        DB     $03     ; X Checks Mask -Bounce off walls
        DW     $0005   ; Delta Y
        DW     $0000   ; Y Position
        DB     $03     ; Y Checks Mask -Bounce off walls
