;  "Star Battle"
;       For Bally Arcade / Astrocade Game System
;       Programmed by Lew Harp
;
;  Videocade #2004 / Action/Skills Series
;
;  Released in 1979 by Bally Mfg. Corp.
;  Re-released in 1981 by Astrovision, Inc.
;
;  Cartridge for Bally Arcade / Astrocade
;
;  Note that the complete instruction for "Star Battle" are at the
;  end of this source code.  
;
;  Dissassembly by Adam Trionfo
;
;  Version Histoy:
;     .002 (Dec. 22, 2011)
;        - First public release 
;        - All graphics (ships, missiles and explosions) have been
;          "translated" so that they can be seen.  For a 2K program,
;          there are quite a lot of graphics-- though all of them
;          are mono-color.
;        - Disassembled all sound effects using Richard Degler's 
;          various Music.LUA scripts for the DZ80 disassembler.
;        - Began to add Tom Wood's April 10, 1979 comments (PDF
;          scan available online)
;        - Program is nearly 100% disassembled
;        - Added Astrovision version of manual at the end of
;          the source code.
;        - To-Do:
;             - Is the DOIT Table (at $2675) correct?  I THINK 
;               it is right, but I have to double-check it.
;             - Finish adding Tom Wood's comments
;             - Name Ports after HVGLIB.H
;             - Generally comment program 
;     .001 (Dec. 21, 2011)
;        - First Day of Disassembly
;        - Disassembled using DZ80 and Richard Degler's
;          "BallyHVG.LUA" script.  
;        - Hand disassembled the setup Menus
;        - Program mostly not commented
;        - Assembled binary output matches 100% with "Star Battle" cart
;
;  To assemble this Z-80 source code using ZMAC:
;  
;  zmac -d -o <outfile> -x <listfile> <filename>
;  
;  For example, assemble this Astrocade Z-80 ROM file:
;     
;  zmac -i -m -o stardis.bin -x stardis.lst stardis.asm


P1TBL     EQU     $4F6B         ; Player 1 Table
P2TBL     EQU     $4F91         ; Player 2 Table
L4FC8     EQU     $4FC8         ; P1/P2 Phasor Sound Adrs
L4FCC     EQU     $4FCC         ; Sound Work Area
L4FF4     EQU     $4FF4         ; Max Score
L4FF7     EQU     $4FF7         ; 0 = 2 Players, 1 = 1 Player

          INCLUDE "HVGLIB.H"    ; Home Video Game Library
     
          ORG     FIRSTC
                                
          DB      "U"           ; User Cartridge Sentinel
          
;  On-Board ROM "SELECT GAME" Menu Data Structure
;
;     1-STAR BATTLE
;     2-GUNFIGHT
;     3-CHECKMATE
;     4-CALCULATOR
;     5-SCRIBBLING 
;
          DW      MENUST        ; Link to Head of "On-Board Menu"
          DW      L265E         ; Address of "STAR BATTLE" menu text
          DW      L2027         ; Jump here if STAR BATTLE selected

;
;   RESTART VECTORS
;
          JP       RST08        ; Reset VECTor #1 (RST $08)
        
          LD       HL,$4FB9     ; Reset VECTor #2 (RST $10)
          BIT      0,(HL)
          RET

          LD      A,($4FF7)     ; Reset VECTor #4 (RST $20)
          OR      A             ; 0 = 2 Player, Not Zero = 1 Player
          RET

;  "Number of Players" Menu Data Structure
;
;     1-PLAYER 
;     2-PLAYER
;       
L2015:    DW    L2021           ; Link to "Number of Players" Menu, Choice 2
          DW    L201B           ; Address of "PLAYER" menu text
          DW    L202D           ; Jump here if 1-PLAYER selected

L201B:    DB    "PLAYER"

L2021:    DW    $0000           ; This is last link of "Number of Players" Menu
          DW    L201B           ; Address of "PLAYER" menu text
          DW    $2030           ; Jump here if 2-PLAYER selected


L2027:    PUSH    DE
          LD      HL,L2015      ; Address of Menu List
          SYSTEM  MENU          ; UPI display a MENU
          ;

;  Start of 1-PLAYER Game
L202D:    LD      A,$01         ; Start of 1-PLAYER Game
          LD      B,$AF
          
;  Start of 2-PLAYER Game
;  Note that a 2-player game seems to start in the middle of the above
;  "LD  B,$AF" instruction, but it doesn't.  Address $2030, the start of
;  a two player game is "AF," which is a valid instruction: XOR A.  After
;  the XOR A, the two programs "merge" again, thus the program from here
;  is valid for both the 1 and 2 player game.
         
          POP     DE
          PUSH    AF
          SYSSUK  GETPAR        ;  UPI GET game PARameter from user
          DW      $021E         ;  ... Prompt Address = 542
          DB      $83           ;  ... Digits = 131
          DW      $4FF4         ;  ... Parameter Address = 20468
          ;
          DI      
          POP     AF
          LD      ($4FF7),A
          LD      SP,$4F6B
          SYSTEM  INTPC         ;  UPI INTerPret with Context create
          ;
          DO      FILL          ;  UPI FILL memory with data
          DW      $4000         ;  ... Memory Address = 16384
          DW      $0E38         ;  ... Byte Count = 3640
          DB      $00           ;  ... Data = 0
          ;
          DO      COLSET        ;  UPI COLors SET
          DW      L266D         ;  ... Table Address = 9837
          ;
          DO      FILL          ;  UPI FILL memory with data
          DW      $4F6B         ;  ... Memory Address = 20331
          DW      $0061         ;  ... Byte Count = 97
          DB      $00           ;  ... Data = 0
          ;
          DO      SETOUT        ;  UPI SET some OUTput ports
          DB      $00           ;  ... VERBL*2 = 0
          DB      $2C           ;  ... HORCB/4 = 44
          DB      $08           ;  ... INMOD = 8
          ;
          DONT    XINTC         ;  UPI eXit INTerpreter with Context
          ;
          IM      2
          LD      A,$55
          LD      B,$40
          LD      DE,$2A60
          LD      HL,$0201
          CALL    L2533
          LD      A,$00
          LD      B,$40
          LD      DE,$1760
          LD      HL,$FF01
          CALL    L2533
          LD      A,$55
          LD      B,$40
          LD      DE,$2A3F
          LD      HL,$02FF
          CALL    L2533
          LD      A,$00
          LD      B,$40
          LD      DE,$173F
          LD      HL,$FFFF
          CALL    L2533
          SYSSUK  SETOUT        ;  UPI SET some OUTput ports
          DB      $B6           ;  ... VERBL*2 = 182
          DB      $6C           ;  ... HORCB/4 = 108
          DB      $08           ;  ... INMOD = 8
          ;
          LD      HL,$4FF8
          LD      (HL),$02
          CALL    L2566
          CALL    ISR5
          CALL    L2111
          CALL    L2162
          LD      HL,$20A4
          PUSH    HL
L20A8:    LD      HL,$4FBB
          LD      A,(HL)
          OR      A
          JR      Z,L20B8
          DEC     (HL)
          CALL    STIMER
          SYSSUK  DECCTS        ;  UPI DECrement CT'S under mask
          DB      $04           ;  ... Counters = 4
          ;
          JR      L20A8
L20B8:    SYSSUK  SENTRY        ;  UPI SENse TRansition
          DW      $0214         ;  ... Keymask Address = 532
          ;
          SYSSUK  DOIT          ;  UPI DOIT table, branch to translation handler
          DW      L2675         ;  Address of Player Input Transition Table
          ;
          RET     


L20C1:    RST     $20           ; P2 Joystick - User ReStarT #4 of 6
          RET     NZ
          LD      A,$01
          DB      $0E

L20C6:    XOR     A             ; P1 Joystick
          CALL    L262E
          LD      (IX+$0F),B
          RET     


L20CE:    SYSTEM  PIZBRK        ;  UPI take a PIZza BReaK
          ;
          RET     


L20D1:    XOR     A             ; P1 Trigger
          LD      HL,$2769
          JR      L20DE

L20D7:    RST     $20           ; P2 Trigger - User ReStarT #4 of 6
          RET     NZ

L20D9:    LD      HL,L2764      ; Player 2 Phasor Sound
          LD      A,$01
L20DE:    CALL    L262E
          LD      A,B
          OR      A
          RET     Z

          SET     5,(IX+$01)
          LD      ($4FC8),HL
          RET     


L20EC:    SYSSUK  DECCTS        ;  UPI DECrement CT'S under mask
          DB      $03           ;  ... Counters = 3
          ;
          RET     


L20F0:    LD      HL,$4FF8
          BIT     7,(HL)
          JR      Z,L20FD
          LD      HL,$4FD6
          LD      (HL),$05
          RET     


L20FD:    CALL    L217D
          RST     $10           ;  User ReStarT #2 of 6
          RES     0,(HL)
          SET     3,(HL)
          RET     

;  Game Over 
L2106:    DI      
          LD      A,$FB
          OUT     ($04),A
          LD      A,$2C
          OUT     ($09),A
          SYSTEM  QUIT          ;  UPI QUIT cassette execution

         
L2111:    RST     $20           ;  User ReStarT #4 of 6
          RET     Z
          ;
          SYSSUK  RANGED        ;  UPI RANGED random number
          DB      $00           ;  ... Cutoff = 0
          ;
          LD      B,A
          AND     $0C
          JR      NZ,L2126
          LD      HL,$4F71
          LD      A,($4F97)
          CP      (HL)
          LD      A,$04
          JR      C,L2128
L2126:    XOR     $0C
L2128:    LD      C,A
          LD      A,B
          AND     $03
          JR      Z,L2130
          XOR     $03
L2130:    OR      C
          LD      ($4FA0),A
          LD      A,B
          AND     $14
          INC     A
          LD      ($4FD7),A
          LD      A,B
          AND     $03
          RET     NZ
          CPL     
          LD      B,A
          JR      L20D9
L2143:    RST     $10           ;  User ReStarT #2 of 6
          BIT     1,(HL)
          RET     NZ
          LD      A,$C0
          LD      IX,$4FCC
          LD      HL,($4FC8)
          SYSTEM  BMUSIC        ;  UPI Begin playing MUSIC
          ;
          RET     


L2153:    SYSSUK  BMUSIC        ;  UPI Begin playing MUSIC
          DW      $4FCC         ;  ... Music Stack = 20428
          DB      $FC           ;  ... Voices = 11111100B (Tones A,B and C)
          DW      L2796         ;  ... Score Address = 10134
          ;
          RET     


L215B:    SYSSUK  BMUSIC        ;  UPI Begin playing MUSIC
          DW      $4FCC         ;  ... Music Stack = 20428
          DB      $FC           ;  ... Voices = 11111100B (Tones A,B and C)
          DW      L279E         ;  ... Score Address = 10142
          ;
L2162:    RST     $10           ;  User ReStarT #2 of 6
          SET     0,(HL)
          CALL    NZ,L217D
          LD      HL,$4F82
          CALL    L21C4
          LD      HL,$4FA8
          CALL    L21C4
          LD      HL,$4FD5
          LD      (HL),$04
          CALL    L217D
          RET     


L217D:    LD      HL,$4FB8
          PUSH    HL
          INC     (HL)
          LD      HL,$4F7E
          LD      DE,$001C
          CALL    L21BA
          LD      HL,$4FA4
          LD      DE,$006E
          CALL    L21BA
          SYSSUK  WRITP         ;  UPI WRITe with Pattern size lookup
          DB      $2A           ;  ... X = 42
          DB      $00           ;  ... Y = 0
          DB      $28           ;  ... Magic = 40
          DW      $272B         ;  ... Pattern Address = 10027
          ;
          SYSSUK  WRITP         ;  UPI WRITe with Pattern size lookup
          DB      $7C           ;  ... X = 124
          DB      $00           ;  ... Y = 0
          DB      $28           ;  ... Magic = 40
          DW      $26EC         ;  ... Pattern Address = 9964
          ;
          RST     $10           ;  User ReStarT #2 of 6
          BIT     3,(HL)
          JR      NZ,L21B7
          SYSSUK  DISNUM        ;  UPI DISplay NUMber
          DB      $58           ;  ... X = 88
          DB      $34           ;  ... Y = 52
          DB      $28           ;  ... Options = 40
          DB      $C1           ;  ... Extended = 193
          DW      $27D0         ;  ... Number Address = 10192
          ;
          SYSSUK  DISNUM        ;  UPI DISplay NUMber
          DB      $44           ;  ... X = 68
          DB      $11           ;  ... Y = 17
          DB      $28           ;  ... Options = 40
          DB      $C1           ;  ... Extended = 193
          DW      $2717         ;  ... Number Address = 10007
          ;
L21B7:    POP     HL
          DEC     (HL)
          RET     


L21BA:    LD      BC,$C324
          LD      IX,$020D
          SYSTEM  DISNUM        ;  UPI DISplay NUMber
          ;
          RET      


L21C4:    BIT     1,(HL)
          RET     Z

          RES     1,(HL)
          LD      DE,$FFFC
          ADD     HL,DE
          SYSTEM  INCSCR        ;  UPI INCrement SCoRe
          ;
          RET     


;  Interrupt Service Routine #3
;  ----------------------------
;
ISR3:     EX      AF,AF'
          OUT     ($04),A
          OUT     ($00),A
          LD      A,$B6          ; Setup Interrrupt Service Routine #4
          OUT     (INFBK),A      ; Load Port $D (write INterrupt FeedBacK) with $B6
          LD      A,$94
          OUT     ($0F),A
          EX      AF,AF'
          EI      
          RET     


;  Interrupt Service Routine #2
;  ----------------------------
;
ISR2:     EX      AF,AF'
          OUT     ($04),A
          OUT     ($00),A
          LD      A,$AE         ; Setup Interrrupt Service Routine #3
          OUT     (INFBK),A     ; Load Port $D (write INterrupt FeedBacK) with $AE
          LD      A,($4FBD)
          OUT     ($0F),A
          LD      A,($4FBF)
          EX      AF,AF'
          EI      
          PUSH    HL
          PUSH    AF
          PUSH    BC
          PUSH    DE
          PUSH    IX
          PUSH    IY
          XOR     A
          CALL    L2285
L21FF:    LD      HL,$4FB8
          DEC     (HL)
          POP     IY
          POP     IX
          POP     DE
          POP     BC
          POP     AF
          POP     HL
          RET     


;  Interrupt Service Routine #5
;  ----------------------------
;
ISR5:     EX      AF,AF'
          OUT     ($04),A
          OUT     ($00),A
          LD      A,$26         ; Setup Interrupt Service Routine #1
          LD      I,A           ; Load High byte of Interrupt Vector with $26
          LD      A,$B4         ; Setup Interrrupt Service Routine #1
          OUT     (INFBK),A     ; Load Port $D (write INterrupt FeedBacK) with $B4
          LD      A,$0A
          OUT     ($0F),A
          LD      A,$64
          OUT     ($05),A
          OUT     ($01),A
          LD      A,$2C
          OUT     ($09),A
          EX      AF,AF'
          EI      
          PUSH    HL
          PUSH    AF
          PUSH    BC
          PUSH    DE
          PUSH    IX
          PUSH    IY
          CALL    L2599
          LD      A,$01
          CALL    L2285
          LD      HL,$4F6D
          INC     (HL)
          LD      HL,$4F93
          INC     (HL)
          LD      HL,$4F83
          INC     (HL)
          LD      HL,$4FA9
          INC     (HL)
          LD      HL,$4FBA
          INC     (HL)
          INC     HL
          INC     (HL)
          JR      L21FF


;  Interrupt Service Routine #1
;  ----------------------------
;
ISR1:     EX      AF,AF'
          LD      A,($4FC5)
          OUT     ($05),A
          LD      A,($4FC6)
          OUT     ($01),A
          LD      A,($4FC7)
          AND     $3F
          ADD     A,$40
          OUT     ($09),A
          LD      A,$B0         ; Setup Interrrupt Service Routine #2
          OUT     (INFBK),A     ; Load Port $D (write INterrupt FeedBacK) with $B0
          LD      A,$52
          OUT     ($0F),A
          LD      A,($4FC0)
          EX      AF,AF'
          EI      
          RET     

;  Interrupt Service Routine #4
;  ----------------------------
;
ISR4:     EX      AF,AF'
          LD      A,$2C
          OUT     ($09),A
          LD      A,$B2          ; Setup Interrrupt Service Routine #5
          OUT     ($0D),A        ; Load Port $D (write INterrupt FeedBacK) with $B2
          LD      A,$D6
          OUT     ($0F),A
          LD      A,$F8
          EX      AF,AF'
          EI      
          RET     


L2285:    LD      HL,$4FB8
          LD      C,A
          LD      A,(HL)
          INC     (HL)
          OR      A
          RET     NZ
          LD      A,C
          OR      A
          LD      DE,$2900
          JR      Z,L2297
          LD      DE,$6B29
L2297:    LD      A,($4FBA)
          LD      C,A
          CPL     
          CALL    L2629
          LD      A,(IX+$0B)
          CP      D
          JR      NC,L22A8
          CP      E
          JR      NC,L22B3
L22A8:    LD      A,C
          CALL    L2629
          LD      A,(IX+$0B)
          CP      D
          RET     NC
          CP      E
          RET     C
L22B3:    PUSH    IX
          LD      HL,$4FB7
          LD      A,(HL)
          LD      B,A
          CPL     
          CALL    L262E
          LD      ($4FC1),IX
          POP     IX
          LD      A,(IX+$01)
          INC     (IX+$12)
          BIT     0,(IX+$12)
          JR      Z,L22E4
          LD      (HL),$FF
          BIT     2,(IX+$17)
          JP      NZ,L2454
          BIT     4,A
          JP      NZ,L23BF
          BIT     5,A
          JP      NZ,L2371
          LD      (HL),B
L22E4:    BIT     2,A
          JP      NZ,L2459
          LD      A,(IX+$0E)
          OR      A
          CALL    NZ,RST08      ; Restart Vector $08
          LD      B,(IX+$0F)
          LD      IY,($4FC1)
          LD      A,(IY+$0F)
          LD      H,(IX+$11)
          LD      (IX+$11),$00
          LD      L,(IY+$11)
          LD      (IY+$11),$00
          AND     $03
          LD      D,A
          LD      A,B
          AND     $03
          LD      E,A
          LD      A,B
          AND     $0C
          LD      B,A
          LD      A,E
          CP      D
          JR      NZ,L2344
          OR      A
          LD      C,$00
          JR      Z,L235C
          LD      A,H
          OR      A
          LD      E,H
          LD      (IX+$11),H
          LD      (IY+$11),L
          JR      NZ,L2354
          LD      A,(IX+$0B)
          CP      $24
          LD      E,$02
          LD      (IY+$11),$01
          LD      C,$01
          LD      (IX+$11),E
          JR      C,L2354
          LD      E,$01
          LD      (IY+$11),$02
          LD      (IX+$11),E
          JR      L2354 
L2344:    OR      A
          JR      NZ,L2350
          LD      A,D
          XOR     $03
L234A:    LD      C,$02
L234C:    OR      B
          LD      B,A
          JR      L235C
L2350:    LD      A,D
          OR      A
          JR      NZ,L2357
L2354:    LD      A,E
          JR      L234A
L2357:    LD      A,E
          LD      C,$02
          JR      L234C
L235C:    RST     $10                     ;  User ReStarT #2 of 6
          JR      Z,L2361
          LD      C,$00
L2361:    CALL    L24C3
          RST     $10                     ;  User ReStarT #2 of 6
          BIT     3,(HL)
          JR      Z,L236E
          LD      HL,$265A                ; Limit Table
          SYSTEM  VECT                    ;  UPI VECTor move coordinate pair
          ;
L236E:    JP      L244F
L2371:    RES     5,(IX+$01)
          RST     $10                     ;  User ReStarT #2 of 6
          RET     NZ
          BIT     2,A
          RET     NZ
          LD      HL,$4FDE
          SET     0,(HL)
          LD      A,(IX+$0B)
          LD      (IX+$21),A
          LD      B,$01
          CP      $24
          JR      NC,L238D
          LD      B,$02
L238D:    LD      (IX+$25),B
          LD      A,(IX+$06)
          LD      (IX+$1C),A
          SET     4,(IX+$01)
          LD      DE,$0016
          ADD     IX,DE
          LD      (IX+$00),$28
          LD      (IX+$07),$01
          LD      (IX+$0C),$01
          LD      (IX+$01),$80
          LD      (IX+$02),$05
          JR      L2432
L23B5:    LD      DE,$FFEA
          ADD     IX,DE
          RES     4,(IX+$01)
          RET     


L23BF:    LD      DE,$0016
          ADD     IX,DE
          RST     $08                     ;  User ReStarT #1 of 6
          BIT     7,(IX+$01)
          JR      Z,L23B5
          LD      HL,$4000
          ADD     HL,DE
          LD      BC,$0500
          LD      DE,$0027
L23D5:    LD      A,$AA
          AND     (HL)
          OR      C
          LD      C,A
          INC     HL
          LD      A,$80
          AND     (HL)
          OR      C
          LD      C,A
          ADD     HL,DE
          DJNZ    L23D5
          OR      A
          JR      Z,L2432
          LD      HL,$4FDE
          LD      IY,($4FC1)
          LD      A,(IY+$1C)
          ADD     A,$04
          SUB     (IX+$06)
          CP      $08
          JR      NC,L2423
          LD      A,(IY+$21)
          ADD     A,$05
          SUB     (IX+$0B)
          CP      $0A
          JR      NC,L2423
          BIT     2,(IY+$17)
          JR      NZ,L2411
          BIT     4,(IY+$01)
          JR      Z,L2423
L2411:    SET     2,(HL)
          SET     2,(IX+$01)
          RES     7,(IY+$17)
          RST     $10           ;  User ReStarT #2 of 6
          SET     1,(HL)
          CALL    L244F
          JR      L23B5
L2423:    SET     2,(IY+$01)
          SET     1,(IX+$01)
          SET     1,(HL)
          RST     $10           ;  User ReStarT #2 of 6
          SET     2,(HL)
L2430:    JR      L23B5
L2432:    LD      A,(IX+$0B)
          PUSH    AF
          LD      B,(IX+$0F)
          LD      C,$04
          CALL    L24C3
          LD      HL,$2669
          SYSTEM  VECT          ;  UPI VECTor move coordinate pair
          ;
          POP     AF
          CP      (IX+$0B)
          JR      Z,L2430
          BIT     3,(IX+$07)
          JR      NZ,L2430
L244F:    CALL    L24B0
          RST     $08           ;  User ReStarT #1 of 6
          RET     


L2454:    LD      DE,$0016
          ADD     IX,DE
L2459:    BIT     3,(IX+$01)
          JR      Z,L24A1
          LD      A,(IX+$0E)
          CP      $06
          JR      C,L246A
          RST     $10           ;  User ReStarT #2 of 6
          JR      Z,L247F
          RET     


L246A:    LD      A,(IX+$02)
          CP      $10
          RET     C
          RST     $10           ;  User ReStarT #2 of 6
          RES     2,(HL)
          RST     $08           ;  User ReStarT #1 of 6
          INC     (IX+$0E)
          LD      A,(IX+$0E)
          CP      $06
          JR      C,L24A6
          RET     


L247F:    RST     $10           ;  User ReStarT #2 of 6
          RES     1,(HL)
          RES     2,(IX+$01)
          RES     3,(IX+$01)
          LD      (IX+$0E),$00
          LD      (IX+$02),$00
          LD      A,R
          ADD     A,(IX+$06)
          CP      $9F
          JR      C,L249D
          SUB     $9F
L249D:    LD      (IX+$06),A
          RET     


L24A1:    RST     $08           ;  USER RESTART #1 OF 6
          LD      (IX+$0E),$04
L24A6:    RST     $08           ;  USER RESTART #1 OF 6
          LD      (IX+$02),$00
          SET     3,(IX+$01)
          RET     


L24B0:    LD      A,(IX+$0B)
          LD      B,$03
          CP      $1E
          JR      C,L24BF
          DEC     B
          CP      $3C
          JR      C,L24BF
          DEC     B
L24BF:    LD      (IX+$0E),B
          RET     


L24C3:    LD      A,(IX+$0B)
          ADD     A,$40
          LD      H,$00
          LD      L,A
          SLA     L
          RL      H
          CALL    L2514
          LD      (IX+$09),H
          LD      (IX+$08),L
          LD      A,(IX+$06)
          SUB     $4F
          LD      H,$00
          LD      L,A
          SLA     L
          JR      NC,L24E6
          LD      H,$FF
L24E6:    PUSH    DE
          CALL    L2514
          POP     DE
          SLA     E
          RL      D
          BIT     3,B
          JR      NZ,L2503
          BIT     2,B
          JR      Z,L2500
          LD      A,D
          CPL     
          LD      D,A
          LD      A,E
          CPL     
          LD      E,A
          INC     DE
          JR      L2503
L2500:    LD      DE,$0000
L2503:    BIT     3,(IX+$0C)
          JR      Z,L250C
          LD      HL,$0000
L250C:    ADD     HL,DE
          LD      (IX+$04),H
          LD      (IX+$03),L
          RET     


L2514:    EX      DE,HL
          LD      HL,$0000
          LD      A,C
L2519:    OR      A
          JR      Z,L2520
          ADD     HL,DE
          DEC     A
          JR      L2519


L2520:    BIT     1,B
          RET     NZ

          BIT     0,B
          JR      Z,L252F
          LD      A,L
          CPL     
          LD      L,A
          LD      A,H
          CPL     
          LD      H,A
          INC     HL
          RET     


L252F:    LD      HL,$0000
          RET     


L2533:    LD      C,$00
L2535:    PUSH    AF
          PUSH    BC
          PUSH    DE
          PUSH    HL
          LD      B,D
          LD      C,$01
          LD      D,$00
          SYSTEM  RECTAN        ;  UPI paint a RECTANgle
          ;
          POP     HL
          POP     DE
          POP     BC
          LD      A,E
          ADD     A,L
          LD      E,A
          PUSH    HL
          PUSH    DE
          LD      E,C
          LD      L,$00
          SRA     H
          RR      L
          SRA     H
          RR      L
          ADD     HL,DE
          LD      C,L
          POP     DE
          LD      D,H
          POP     HL
          POP     AF
          DJNZ    L2535
          RET     


L255C:    XOR     A
          LD      ($4FBC),A
          LD      A,$54
          LD      ($4FBD),A
          RET     


L2566:    CALL    L255C
          LD      IX,$4F6B
          LD      (IX+$00),$28
          LD      (IX+$06),$59
          LD      (IX+$0B),$2E
          LD      (IX+$0C),$01
          LD      (IX+$07),$01
          SET     7,(IX+$01)
          LD      DE,$4F91
          LD      HL,$4F6B
          LD      BC,$0016
          LDIR    
          LD      (IX+$2C),$45
          LD      (IX+$31),$1A
          RET     


L2599:    LD      A,($4FBD)
          SUB     $52
          SRL     A
          SRL     A
          ADD     A,$19
          LD      ($4FC7),A
          LD      A,($4FC0)
          LD      ($4FC5),A
          LD      A,($4FBF)
          LD      ($4FC6),A
          LD      HL,$4FBA
          BIT     0,(HL)
          CALL    NZ,L25FF
          INC     HL
          INC     HL
          LD      A,(HL)
          ADD     A,$01
          LD      (HL),A
          INC     HL
          ADD     A,(HL)
          LD      (HL),A
          CP      $94
          RET     C
          LD      (HL),$90
          XOR     A
          LD      ($4FBC),A
          INC     HL
          LD      A,(HL)
          OR      A
          INC     HL
          INC     HL
          LD      A,(HL)
          DEC     HL
          LD      (HL),A
          JR      Z,L25DA
          DEC     HL
          DEC     (HL)
          RET     


L25DA:    CALL    L255C
          INC     HL
          LD      A,($4FC3)
          INC     A
          LD      ($4FC3),A
          LD      B,A
          LD      A,($4FC4)
          ADD     A,$08
          CP      $00
          JR      NZ,L25F1
          ADD     A,$08
L25F1:    LD      ($4FC4),A
          BIT     0,B
          JR      NZ,L25F9
          INC     A
L25F9:    LD      (HL),A
          DEC     HL
          DEC     HL
          LD      (HL),$02
          RET     


L25FF:    LD      A,($4FB9)
          BIT     2,A
          JR      Z,L260C
          LD      A,$07
          OUT     ($04),A
          OUT     ($00),A
L260C:    LD      A,($4FBD)
          SUB     $52
          SRL     A
          SRL     A
          NEG     
          ADD     A,$10
          LD      ($4FC7),A
          LD      A,($4FC0)
          LD      ($4FC6),A
          LD      A,($4FBF)
          LD      ($4FC5),A
          RET     


L2629:    AND     $01
          LD      ($4FB7),A
L262E:    BIT     0,A
          LD      IX,$4F6B
          RET     Z
          LD      IX,$4F91
          RET     


;  Restart Vector $08
;
RST08:    LD      A,($4FB7)
          OR      A
          LD      HL,L26A4-2      ; X-Fighter Table
          JR      Z,L264D
          LD      HL,L269A-2      ; H-Fighter Table
          CP      $01
          JR      Z,L264D
          LD      HL,$27C2
L264D:    LD      A,(IX+$0E)
          SYSTEM  INDEXW        ;  UPI INDEX Word by A
          ;
          EX      DE,HL
          LD      A,$08
          OUT     ($19),A
          SYSTEM  VWRITR        ;  UPI Vector WRITe Relative
          ;
          RET     


;  Limit Table for VECT (2 Bytes, goes into HL)
L265A:    DB    $0B
          DB    $95
          DB    $03
          DB    $54


;  Menu Text
L265E:    DB     "STAR BATTLE",$00    
         
L266A:    DB    $9E
          DB    $00
          DB    $56

;  Color Table
L266D:    DB    $0F         ; COL3L - 
          DB    $0F         ; COL2L - 
          DB    $02         ; COL1L - 
          DB    $F8         ; COL0L - 
          DB    $0F         ; COL3R - 
          DB    $0F         ; COL2R - 
          DB    $02         ; COL1R - 
          DB    $F8         ; COL0R - 

;  DOIT Table
L2675:    DB    SF0     ; Sentry, Flag bit 0 has changed 
          DW    L2143

          DB    SF1     ; Sentry, Flag bit 1 has changed
          DW    L215B

          DB    SF2     ; Sentry, Flag bit 2 has changed      
          DW    L2153

          DB    SJ0     ; Sentry, Joystick 0 for player 1 has changed     
          DW    L20C6

          DB    SJ1     ; Sentry, Joystick 1 for player 2 has changed
          DW    L20C1

          DB    ST0     ; Sentry, Trigger 0 for player 1 has changed
          DW    L20D1

          DB    ST1     ; Sentry, Trigger 1 for player 2 has changed
          DW    L20D7

          DB    SCT0    ; Sentry, Counter-Timer 0 has counted down
          DW    L20F0

          DB    SCT1    ; Sentry, Counter-Timer 1 has counted down
          DW    L2106

          DB    SCT2    ; Sentry, Counter-Timer 2 has counted down
          DW    L2111

          DB    SSEC    ; Sentry, SEConds timer has counted down
          DW    L20EC

          DB    SKYD    ; Sentry, KeY is now Down
          DW    L20CE

          DB    ENDx

;  H-Fighter Table
L269A:    DW    L26B8     ; Large H-Fighter
          DW    L26D4     ; Medium H-Fighter
          DW    L26EA     ; Small H-Fighter
          DW    L2732     ; Large Main Explosion
          DW    L274C     ; Large Fading Explosion

;  X-Fighter Table
L26A4:    DW    L26F3     ; Large X-Fighter
          DW    L2715     ; Medium X-Fighter
          DW    L2729     ; Small X-Fighter
          DW    L2732     ; Large Main Explosion
          DW    L274C     ; Large Fading Explosion

;  Screen INTerrupt VECtor Routine Pointers
L26AE:    DW    ISR3      ; Pointer to Interrupt Service Routine #3
L26B0:    DW    ISR2      ; Pointer to Interrupt Service Routine #2
L26B2:    DW    ISR5      ; Pointer to Interrupt Service Routine #5
L26B4:    DW    ISR1      ; Pointer to Interrupt Service Routine #1
L26B6:    DW    ISR4      ; Pointer to Interrupt Service Routine #4

;  Large H-Fighter Graphic
L26B8:    DB    -7,-6     ; X,Y Displacement
          DB    2,12      ; X,Y Size
;
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .
          DB    10000011B,10000010B  ;   X . . . . . X X X . . . . . X .
          DB    11000111B,11000110B  ;   X X . . . X X X X X . . . X X .
          DB    11111111B,11111110B  ;   X X X X X X X X X X X X X X X .
          DB    11111111B,11111110B  ;   X X X X X X X X X X X X X X X .
          DB    11000111B,11000110B  ;   X X . . . X X X X X . . . X X .
          DB    10000011B,10000010B  ;   X . . . . . X X X . . . . . X .
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .
          DB    10000000B,00000010B  ;   X . . . . . . . . . . . . . X .


;  Medium H-Fighter Graphic
L26D4:    DB    -6,-4     ; X,Y Displacement
          DB    2,9       ; X,Y Size
;
          DB    10000000B,00010000B  ;   X . . . . . . . . . . X . . . .
          DB    10000000B,00010000B  ;   X . . . . . . . . . . X . . . .
          DB    10000110B,00010000B  ;   X . . . . X X . . . . X . . . .
          DB    11001111B,00110000B  ;   X X . . X X X X . . X X . . . .
          DB    11111111B,11110000B  ;   X X X X X X X X X X X X . . . .
          DB    11001111B,00110000B  ;   X X . . X X X X . . X X . . . .
          DB    10000110B,00010000B  ;   X . . . . X X . . . . X . . . .
          DB    10000000B,00010000B  ;   X . . . . . . . . . . X . . . .
          DB    10000000B,00010000B  ;   X . . . . . . . . . . X . . . .


; Small H-Fighter Graphic
L26EA:    DB    -3,-2     ; X,Y Displacement
          DB    1,5       ; X,Y Size
;
          DB    10000100B            ;   X . . . . X . .
          DB    10110100B            ;   X . X X . X . .
          DB    11111100B            ;   X X X X X X . .
          DB    10110100B            ;   X . X X . X . .
          DB    10000100B            ;   X . . . . X . .


;  Large X-Fighter Graphic
L26F3:    DB    -10,-5    ; X,Y Displacement
          DB    3,10      ; X-Y Size
;
  ;   X X . . . . . . . . . . . . . . . . . X X . . .
  ;   X X X X . . . . . . . . . . . . . X X X X . . .
  ;   . . . X X X X . . . . . . . X X X X . . . . . .
  ;   . . . . . X X X . X X X . X X X . . . . . . . .
  ;   . . . . . . . X X X X X X X . . . . . . . . . .
  ;   . . . . . . . X X X X X X X . . . . . . . . . .
  ;   . . . . . X X X . X X X . X X X . . . . . . . .
  ;   . . . X X X X . . . . . . . X X X X . . . . . .
  ;   X X X X . . . . . . . . . . . . . X X X X . . .
  ;   X X . . . . . . . . . . . . . . . . . X X . . .
;
          DB    11000000B,00000000B,00011000B
          DB    11110000B,00000000B,01111000B
          DB    00011110B,00000011B,11000000B
          DB    00000111B,01110111B,00000000B
          DB    00000001B,11111100B,00000000B
          DB    00000001B,11111100B,00000000B
          DB    00000111B,01110111B,00000000B
          DB    00011110B,00000011B,11000000B
          DB    11110000B,00000000B,01111000B
          DB    11000000B,00000000B,00011000B


;  Medium X-Fighter Graphic
L2715:    DB    -8,-4     ; X,Y Displacement
          DB    2,8       ; X,Y Size
;
          DB    11000000B,00000011B  ;   X X . . . . . . . . . . . . X X
          DB    01110000B,00001110B  ;   . X X X . . . . . . . . X X X .
          DB    00011101B,10111000B  ;   . . . X X X . X X . X X X . . .
          DB    00000111B,11100000B  ;   . . . . . X X X X X X . . . . .
          DB    00000111B,11100000B  ;   . . . . . X X X X X X . . . . .
          DB    00011101B,10111000B  ;   . . . X X X . X X . X X X . . .
          DB    01110000B,00001110B  ;   . X X X . . . . . . . . X X X .
          DB    11000000B,00000011B  ;   X X . . . . . . . . . . . . X X


;  Small X-Fighter Graphic
L2729:    DB    -4,-2     ; X,Y Displacement
          DB    1,5       ; X,Y Size
;
          DB    10000001B            ;   X . . . . . . X
          DB    01100110B            ;   . X X . . X X .
          DB    00111100B            ;   . . X X X X . .
          DB    01100110B            ;   . X X . . X X .
          DB    10000001B            ;   X . . . . . . X


; Large Main Explosion Graphic
L2732:    DB    -7,-3     ; X,Y Displacement
          DB    2,11      ; X,Y Size
;
          DB    00100000B,00000110B  ;   . . X . . . . . . . . . . X X .   
          DB    00011000B,00011000B  ;   . . . X X . . . . . . X X . . .
          DB    00001100B,00110001B  ;   . . . . X X . . . . X X . . . X
          DB    11000111B,11111110B  ;   X X . . . X X X X X X X X X X .
          DB    00111110B,01111000B  ;   . . X X X X X . . X X X X . . .
          DB    00001100B,00110000B  ;   . . . . X X . . . . X X . . . .
          DB    00111110B,01111000B  ;   . . X X X X X . . X X X X . . .
          DB    11000111B,11111110B  ;   X X . . . X X X X X X X X X X .
          DB    00001100B,00110001B  ;   . . . . X X . . . . X X . . . X
          DB    00011000B,00011000B  ;   . . . X X . . . . . . X X . . .
          DB    00100000B,00000110B  ;   . . X . . . . . . . . . . X X .


;  Large Fading Explosion Graphic
L274C:    DB    -7,-3     ; X,Y Displacement
          DB    2,10      ; X,Y Size
;
          DB    10000000B,10001000B  ;   X . . . . . . . X . . . X . . .
          DB    00101010B,00000010B  ;   . . X . X . X . . . . . . . X .
          DB    00000001B,00010000B  ;   . . . . . . . X . . . X . . . .
          DB    00001000B,00000100B  ;   . . . . X . . . . . . . . X . .
          DB    00000000B,00010000B  ;   . . . . . . . . . . . X . . . .
          DB    00100000B,00000001B  ;   . . X . . . . . . . . . . . . X
          DB    00001000B,00101000B  ;   . . . . X . . . . . X . X . . .
          DB    01000001B,00000000B  ;   . X . . . . . X . . . . . . . .
          DB    00010100B,10100100B  ;   . . . X . X . . X . X . . X . .
          DB    01000010B,00001000B  ;   . X . . . . X . . . . . X . . .


;  Player 2 Phasor Sound
L2764:    OUTPUT  $03,GS4          ; Set TONEC
          OUTPUT  $02, A5          ; Set TONEB
          CREL    4                ; Call Relative $276D

;  Player 1 Phasor Sound
L2769:    OUTPUT  $03,FS4          ; Set TONEC
L276D:    OUTPUT  $02,DS6          ; Set TONEB
          VOLUME  $77,$17          ; VA = 7, VB = 7, VC= 7, NM = 1
          LEGSTA                   ; Toggle LEGato/STAccato
          MASTER  $48
          OUTPUT  $07,$30          ; Set VOLN = 48
          NOTE1   $03, G7
          MASTER  $38
          OUTPUT  $07,$20          ; Set VOLN = 32
          NOTE1   $03, G7
          MASTER  $2A
          OUTPUT  $07,$10          ; Set VOLN = 16
          NOTE1   $03, G7
          MASTER  $38
          OUTPUT  $07,$20          ; Set VOLN = 32
          NOTE1   $03, G7
          MASTER  $4A
          OUTPUT  $07,$30          ; Set VOLN = 48
          NOTE1   $03, G7
          MASTER  $60
          OUTPUT  $07,$48          ; Set VOLN = 72
          NOTE1   $03, G7
          QUIET   

;  Sound Effect
L2796:    MASTER  $18              ; Set Master Oscillator               
          OUTPUT  7,8              ; set VOLN = 8
          VOLUME  $CC,$1C          ; VA = 12, VB = 12, VC= 12, NM = 1
          CREL    7                ; Call Relative $27A5

;  Sound Effect
L279E:    MASTER  $00              ; Set Master Oscillator           
          OUTPUT  7,8              ;  set VOLN = 8
          VOLUME  $FF,$1F          ;  VA = 15, VB = 15, VC= 15, NM = 1
L27A5:    LEGSTA                   ;  Toggle LEGato/STAccato
          NOTE3   $08,$FF,$FC,$F8
          NOTE3   $08,$FF,$FB,$F6
          VOLUME  $88,$18          ;  VA = 8, VB = 8, VC= 8, NM = 1
          NOTE3   $06,$FF,$FA,$F4
          VOLUME  $44,$14          ;  VA = 4, VB = 4, VC= 4, NM = 1
          NOTE3   $08,$FF,$F9,$F2
          VOLUME  $11,$11          ;  VA = 1, VB = 1, VC= 1, NM = 1
          NOTE3   $19,$F8,$FF,$F0
          QUIET
;
; Table
L27C4:    DW    L27CE      ;  Points to Large Missile Graphic
          DW    L27D6      ;  Points to Medium Missile Graphic 
          DW    L27DE      ;  Points to Small Missile Graphic 
          DW    L27E5      ;  Points to Small Main Explosion Graphic 
          DW    L27EF      ;  Points to Small Fading Explosion Graphic 


;  Large Missile Graphic
L27CE:    DB    0,0        ; X,Y Displacement
L27D0:    DB    1,4        ; X,Y Size                         
;
          DB    11000000B  ; X X . . . . . .
          DB    11000000B  ; X X . . . . . .
          DB    11000000B  ; X X . . . . . .
          DB    11000000B  ; X X . . . . . .


;  Medium Missile Graphic
L27D6:    DB    0,0        ; X,Y Displacement
          DB    1,4        ; X,Y Size
;
          DB    10000000B  ; X . . . . . . .
          DB    10000000B  ; X . . . . . . .
          DB    10000000B  ; X . . . . . . .
          DB    10000000B  ; X . . . . . . .


;  Small Missile Graphic
L27DE:    DB    0,0        ; X,Y Displacement
          DB    1,3        ; X,Y Size
;
          DB    10000000B  ; X . . . . . . .
          DB    10000000B  ; X . . . . . . .
          DB    10000000B  ; X . . . . . . .


;  Small Main Explosion Graphic
L27E5:    DB    -4,0       ; X,Y Displacement 
          DB    1,6        ; X,Y Size
;
          DB    01000010B  ; . X . . . . X .
          DB    00101100B  ; . . X . X X . .
          DB    00111000B  ; . . X X X . . .
          DB    01011100B  ; . X . X X X . .
          DB    00010010B  ; . . . X . . X .
          DB    00100000B  ; . . X . . . . .


;  Small Fading Explosion Graphic
L27EF:    DB    -4,0      ; X,Y Displacement  
          DB    1,7       ; X,Y Size
;
          DB    00100010B  ; . . X . . . X .
          DB    00000100B  ; . . . . . X . .
          DB    00000000B  ; . . . . . . . .
          DB    11000011B  ; X X . . . . X X
          DB    00000000B  ; . . . . . . . .
          DB    00100000B  ; . . X . . . . .
          DB    01000100B  ; . X . . . X . .

;  Six Bytes of Free ROM
          DB    $FF,$FF,$FF,$FF,$FF,$FF

END 

;  End of Assembly Code

;  +------------------------------------------------------+
;  |       Additional Comments about "Star Battle"        |
;  +------------------------------------------------------+

;   Instructions for "Star Battle"
;   ------------------------------
;  (from the Astrovision re-release of the game)
;
;     An exciting race through space in a running battle with
;  enemy star ships.  Three dimensional action looks like you're
;  plunging through a time warp on your way to hyperspace!  Fire
;  your missiles and take evasive action to avoid enemy lasers!
;  You have have control over your spacecraft's speed and
;  direction-- and you can play to any score for long or short
;  games!  1 or 2 players.
;
;  Star Battle (1 or 2 players)
;  
;     Fly your spacecraft down a 3-D tunnel while dodging enemy
;  lasers.  Score points by firing and hitting enemy star ships.
;  
;     With Star Battle, you can play to any score by choosing
;  the number of star ships that must be destroyed.  Then, it's
;  a race against time to strike the enemy ships before they
;  hit you!
;  
;  Starting The Game
;  
;     Choose Star Battle by pressing 1 on the keypad or by using
;  remote game selection (pull the trigger on hand control #1,
;  turn the knob until 1 appears on the screen, then pull the
;  trigger again).
;  
;     Select the number of players (1 or 2).  The star ship of 
;  player l is designed like an X.  The star ship of player
;  2 looks like an H.
;  
;     Enter the maximum score (1-999).
;  
;  Playing The Game
;  
;     The hand control functions for Star Battle are:
;  
;  Trigger	Launches the missiles.
;  Knob	        Has no function.
;  Joystick	Steers left or right; slows the speed of
;               the ship when pulled back; goes faster when
;               pushed forward.
;  
;     The object of the game is to hit the number of enemy star
;  ships selected before the enemy scores that number of hits.
;  
;     The star ships dart back and forth very quickly, so you 
;  must time the release of your missiles to meet the enemy
;  ship where it is going to be.
;  
;     For one player games, the Arcade automatically becomes
;  the enemy and flies the second star ship.  Since both sides
;  have an unlimited supply of ammunition, the action is
;  very fast-paced.
;  
;  Scoring
;  
;     When a missile hits a star ship, an explosion is seen and
;  heard, and the ship disappears from the screen.  Points are 
;  awarded to the player who made the hit, and another enemy
;  star ship comes into view.
;  
;     The scores of both players are displayed at the top of the 
;  screen.  Since the length of the game is determined by the
;  score which was entered, the game continues until one player
;  reaches that score.
;  
;  Strategies For Winning
;  
;      * Try to fly your star ship in, out, around as well
;        as behind the enemy ship to be hit.  As you practice
;        maneuvering with the joystick, you will increase your
;        ability to target the ship you wish to hit (and you'll
;        also be able to avoid enemy lasers!).  
;
;  Playing Again
;  
;     To play Star Rattle again, press the RESET button, and
;  follow the instructions for starting the game.
;
