Compucolor.org – Virtual Media

Listing of file='LIFE.SRC;01' on disk='vmedia/life-sector.ccvf'


;--------------------------------------------------------
;                  GAME OF LIFE
;                    FOR  CCII
;            WRITTEN BY B. BARLOW  11-80
;
;  THIS LIFE PROGRAM USES THE SUITS CYCLINDRICAL WORLD,
;  AND THE "LET ME BE YOUR NEIGHBOR" ALGORITHM.
;  THE PROGRAM FEATURES:
;    .SPEED (ASSEMBLY LANGUAGE)
;    .EASY STARTING DEFINITIONS WITH THE CURSOR PAD
;    .STOPS ON DEAD OR STABLE WORLDS
;    .PAUSES ON COMMAND
;
;--------------------------------------------------------
;
;        MAJOR PROGRAM BLOCKS (THERE ARE 6) ARE DELINEATED:
;
;===========================================================
;
;           SET UP
;
;=========================================================
;
CURSLEFT EQU 26
CURSRGHT EQU 25
CURSUP   EQU 28
CURSDOWN EQU 10
HOME     EQU 8
;
BEGSCRN  EQU 24576
BEGWRLD  EQU 24704
ENDWRLD  EQU 28443
ENDSCRN  EQU 28671
CENTSCRN EQU 26686
;
KBDFL    EQU 81DFH
INPVECT  EQU 81C5H
;
CURSCCI EQU 8
NORMCCI EQU 2
CR       EQU 13
INDIV    EQU 42          ; INDIVIDUAL IN WORLD
BLANK    EQU 32
;
;        SET VECTOR TABLE FOR EITHER CCII VERSION OF SOFTWARE
;
         ORG  8200H
START:   LXI   H,0
        DAD   SP
        LXI   SP,STACK
        PUSH  H            ;SAVE CALLERS STACK IN OURS
        PUSH  PSW
        PUSH  D
        PUSH  B
        LDA   0002H
        CPI   37H          ;37H IS OLD (6/78) ROM
        JZ    STARTLIFE
        LXI   H,JTAB
        LXI   D,NEWVEC
        LXI   B,LENTAB
OLAY:   LDAX   D
        MOV   M,A
        INX   H
        INX   D
        DCX   B
        MOV   A,B
        ORA   C
        JNZ   OLAY
        JMP   STARTLIFE
JTAB:
OS:     JMP   33F4H
MOVDH:  JMP   344BH
MOVHD:  JMP   3444H
CMPHD:  JMP   344DH
CMPDH:  JMP   3453H
SUBHD:  JMP   3459H
ADHLA:  JMP   3518H
ANHD:   JMP   351DH
NEGH:   JMP   3524H
MULHD:  JMP   3562H
DIVHD:  JMP   3581H
XORHD:  JMP   3533H
LENTAB  EQU   $-JTAB
NEWVEC:
 JMP 182AH
 JMP 1871H
 JMP 187AH
 JMP 1883H
 JMP 1889H
 JMP 188FH
 JMP 194EH
 JMP 1953H
 JMP 195AH
 JMP 1998H
 JMP 19B7H
 JMP 1969H
; END OF VERSION VECTOR SET UP
;
STARTLIFE: LXI   H,CLRSCRN
        CALL  OS
        LDA   JTAB
        STA   INPVECT
        LXI   H,INPCHAR
        SHLD  INPVECT+1       ;SET UP INPUT CHAR LINKAGE
        MVI   A,31
        STA   KBDFL       ;POINT TO OUR ROUTINE
;
;===========================================================
;
;           PRINT DIRECTIONS
;
;===========================================================
;
        LXI   H,D1
        CALL  OS
        LXI   H,D2
        CALL  OS
        LXI  H,D3
        CALL OS
        LXI   H,D4
        CALL  OS
        LXI   H,D5
        CALL OS
        LXI   H,D6
        CALL  OS
        LXI   H,D7
        CALL  OS
        LXI   H,D8
        CALL  OS
        LXI   H,D9
        CALL  OS
        LXI   H,D10
        CALL  OS
WAITFORKEY:
        XRA   A
        STA   READY
        CALL  GETCH        ;GET A CHARACTER
;
;=========================================================
;
;          DEFINE INITIAL POPULATION
;
;=========================================================
;
DEFPOP:
        LXI   H,CLRSCRN
        CALL  OS
        LXI   H,TOP1
        CALL  OS
        LXI   H,BOTTOM1
        CALL  OS            ;SET UP INITIAL POPSCREEN
        LXI   H,CENTSCRN
        CALL  CURSOR        ;PUT CURSOR IN CENTER OF SCREEN
DIPCHAR:  CALL  GETCH
        LDA  KBCHAR
        CPI   82
        JZ    ENDDIP        ;IF USER TYPED "R"(UN)
        CPI   CURSUP
        JNZ   CH1
        LXI   D,-128
        CALL  LIMIT         ;HANDLE CURSOR UP KEY
        CNC   CURSOR
        JMP   CH99
CH1:    CPI   CURSLEFT
        JNZ   CH2
        LXI   D,-2
        CALL  LIMIT
        CNC   CURSOR
        JMP   CH99
CH2:    CPI   CURSRGHT
        JNZ   CH3
        LXI   D,2
        CALL  LIMIT
        CNC   CURSOR
        JMP   CH99
CH3:    CPI   CURSDOWN
        JNZ   CH4
        LXI   D,128
        CALL  LIMIT
        CNC   CURSOR
        JMP   CH99
CH4:    CPI   HOME
        JNZ   CH5
        LXI   H,CENTSCRN
        CALL  CURSOR
        JMP   CH99
CH5:    CPI   CR
        JNZ   CH99
        MOV   A,M
        CPI   INDIV
        JZ    CH6
        MVI   M,INDIV        ;IF NOT ONE BEFORE, MAKE IT ONE NOW
        JMP   CH99
CH6:    MVI   M,BLANK        ;IF IT WAS ONE, ERASE IT
CH99:
        JMP   DIPCHAR        ;DO IT AGAIN
ENDDIP:
;;==========================================================
;
;      DEVELOP AND DISPLAY NEXT GENERATION
;
;===========================================================
        LXI   H,GENERLINE
        CALL  OS
        LXI   H,BOTTOM2
        CALL  OS
        LHLD  CURSLOC      ;CLEAR CURSOR CCI
        INX   H
        MVI   M,NORMCCI
;
;CLEAR NEIGHBOR COUNT SPACE
GEN:    DI
        LXI   H,0
        DAD   SP
        SHLD  SAVSTACK
        MVI   A,128
        LXI   D,0
        LXI   SP,ENDNCS
CLRNCS: PUSH  D
        PUSH  D
        PUSH  D
        PUSH  D
        PUSH  D
        PUSH  D
        PUSH  D
        PUSH  D
        DCR   A
        JNZ   CLRNCS
        LHLD  SAVSTACK
        SPHL
        EI
;
;    MAIN LIFE ALGORITHM
;
        MVI   A,1
        STA   DEAD
        LXI   D,BEGWRLDNCS
        LXI   H,BEGWRLD
LLOOP:  MOV   A,M
        CPI   INDIV        ;INDIVID. PRESENT?
        JNZ   NOINDIV
        XRA   A
        STA   DEAD           ;LIFE PRESENT IN GENERATION
        XCHG


        LXI   B,-65
        DAD   B
        INR   M
        INX   H
        INR   M
        INX   H
        INR   M
        LXI   B,126
        DAD   B
        INR   M
        INX   H
        INR   M
        INX   H
        INR   M
        LXI   B,-64
        DAD   B
        INR   M
        DCX   H
        DCX   H
        INR   M
        INX   H      ;SET BACK TO ORIG
        XCHG                  ;ALL NEIGHBORS FOR THIS SPACE INCREMENTED
NOINDIV: INX  H
        INX   H               ;NEXT SCREEN LOC.
        INX   D               ;NEXT NCS LOC.
        MOV   A,H
        CPI   70H             ;CHK FOR END OF SCREEN
        JNZ   LLOOP
;
;  CHECK FOR STABLE UNIVERSE
;
        LDA   SCHK    ;DO WE WANT TO DO STABLE CHECK?
        JNZ   LIVEDIE  ;NO
        LXI   H,BEGWRLDNCS
        LXI   D,STABLECHECK
        LXI   B,ENDWRLDNCS-BEGWRLDNCS
SCLOOP: MOV   A,M
        XCHG
        CMP   M       ;SAME?
        JNZ   MVSTAB   ;NO.
        INX   H
        XCHG
        INX   H
        DCX   B
        MOV   A,C
        ORA   B
        JNZ   SCLOOP
        MVI   A,2
        STA   DEAD        ;INDICATE STABLE WORLD
;  SAVE COUNT SPACE FOR NEXT STABILITY CHECK
MVSTAB:
        LXI    H,BEGWRLDNCS
        LXI   D,STABLECHECK
        LXI   B,ENDWRLDNCS-BEGWRLDNCS
MVSLOOP: MOV  A,M
        XCHG
        MOV   M,A
        INX   H
        XCHG
        INX   H
        DCX   B
        MOV   A,C
        ORA   B
        JNZ   MVSLOOP

;
;  DETERMINE WHO LIVES AND WHO DIES
;
LIVEDIE:
        LXI   H,BEGWRLDNCS
        LXI   B,ENDWRLDNCS-BEGWRLDNCS
        LXI   D,BEGWRLD
DLOOP:  MOV   A,M               ;CHECK NEIGHBOR COUNT
        CPI   3
        JNZ   NOT3
        XCHG                 ;THIS SPACE LIVES
        MVI   M,INDIV
        XCHG
        JMP   EGEN
NOT3:   CPI   2
        JZ    EGEN          ;THIS SPACE DIES
        XCHG
        MVI   M,BLANK
        XCHG
EGEN:   INX   H       ;NEXT NCS SPACE
        INX   D            ;NEXT SCREEN SPACE
        INX   D
        DCX   B
        MOV   A,C
        ORA   B            ;ZERO YET?
        JNZ   DLOOP        ;NO.
;
;   INCREMENT GENERATION NO.
;
        LXI   H,LOWORD
NXTDIG: MOV   A,M
        ANI   15         ;CLEAR ZONE
        ORI   30H        ;MAKE ASCII
        INR   A            ;INCREMENT LOW ORDER DIGIT
        CPI   3AH
        JNZ   ENDI         ;NO OVERFLOW
        MVI   A,30H
        MOV   M,A
        DCX   H
        DCX   H            ;IN SCREEN MEMORY
        JMP   NXTDIG
ENDI:   MOV   M,A
;==========================================================
;
;        END?
;
        LDA   DEAD
        CPI   0           ;CHECK FOR DEAD OR STABLE WORLDS
        JZ    CHKEYS     ;NO.
        CPI   1
        JNZ   ND
        LXI   H,DEADMSG  ;DEAD.
        CALL  OS
        JMP   ANOTHER
ND:     CPI   2
        JNZ   CHKEYS
        LXI   H,STABLEMSG  ;STABLE
        CALL  OS
        JMP   ANOTHER
CHKEYS: LDA   KBCHAR
        CPI   81      ;   'Q'
        JNZ   NQ
        LXI   H,QUITMSG
        CALL  OS
        JMP   EXIT
NQ:     CPI   83      ;   'S'
        JNZ   NS
        LXI   H,STOPMSG
        CALL  OS
        JMP   ANOTHER
NS:     CPI   80      ;    'P'
        JZ    CHKEYS
        CPI   79    ;'O'
        JNZ   NOTO
        XRA   A
        STA   READY
        CALL  GETCH    ;WAIT FOR NEXT CHARACTER
        JMP   GEN      ;DO NXT GENERATION
NOTO:
        CPI   65   ;  'A'
        JNZ   NOTA
        MVI   A,0   ;DO STABILITY CHK
        STA   SCHK
        JMP   NOTB
NOTA:   CPI   66   ;  'B'
        JNZ   NOTB
        MVI   A,255
        STA   SCHK   ;DO NOT DO STABILITY CHECK
NOTB:
        JMP   GEN        ;NEXT GENERATION
;===========================================================
;
;         ANOTHER?
;
;===========================================================
;
ANOTHER:        LXI   H,ANOTHERMSG
        CALL  OS
        CALL  GETCH
        LDA   KBCHAR
        CPI   89      ;    'Y'
        JZ    DEFPOP
        CPI   78      ;    'N'
        JZ    EXIT
        JMP   ANOTHER

;**********************************************************
;
;        EXIT/LEAVE
;
;**********************************************************
;
EXIT:   LXI   H,CLRSCRN
        CALL  OS
        POP   B
        POP   D
        POP   PSW
        POP   H
        SPHL              ;RESTORE FCS STACK
        LXI   B,0         ;SIGNAL NO ERRORS
        RET
;
;---------------------SUBROUTINE--------------------------
;
;   CURSOR
;
;    H,L --> DESIRED CURSOR POS
;    CURSLOC --> OLD POS
;
;    CURSLOC --> NEW POS
;
CURSOR: INX   H
        MVI   M,CURSCCI     ;SET BKG/FGD COLOR FOR CURSOR
        DCX   H
        PUSH  H
        LHLD  CURSLOC
        INX   H
        MVI   M,NORMCCI     ;SET OLD SPOT TO NORMAL COLORS
        POP   H
        SHLD  CURSLOC
        RET

;---------------------SUBROUTINE--------------------------
;
;   LIMIT
;
;    H,L CONTAIN CURRENT CURSOR LOC
;    D,E CONTAIN INCREMENT
;
;    CARRY FLAG 0 - OK, WITHIN LIMITS
;                   H,L -->DESIRED CURSOR POS
;               1 - NOT OK. H,L CHANGED, BUT NOT VALID.
;
LIMIT:  DAD   D
        STC          ;TEMPORARILY EVERYTHING'S OK
        CMC
        RET

;----------------------SUBROUTINE--------------------------
;
;   GETCH
;
GETCH:  LDA   READY
        CPI   80H
        JNZ   GETCH               ;IF 80H, A CHAR IS READY
        XRA   A
        STA READY
        RET
;------------------------SUBROUTINE-----------------------
;
;   INPCHAR
;
;    INTERRUPT ROUTINE GIVEN CONTROL ON KEYBOARD INPUT
;
INPCHAR:
        PUSH  PSW
        MOV   A,E        ;CH IN E
        STA   KBCHAR
        MVI   A,80H
        STA   READY
        POP   PSW
        EI
        RET
;
;------------------------DATA-------------------------------
        DS    100
STACK:
CURSLOC: DB   0,0
CLRSCRN: DB   12,29,18,30,16,15,27,24,239
TOP1: DB  8,6,39
      DB   '                    '
      DB   'PRESS RETURN TO GENERATE AN INDIVIDUAL      '
      DB   6,NORMCCI,239
BOTTOM1: DB  3,0,31,6,39
        DB    '                    '
        DB    'PRESS R TO RUN      '
        DB    '                        ',6,NORMCCI,3,64,0,239
D1:   DB   6,5,3,20,1,'COMPUCOLOR ',6,1,'LIFE',6,NORMCCI,239
D2:   DB   3,4,3,'WELCOME TO THE WORLD OF COMPUCOLOR LIFE. IT IS A CYLINDRICAL ',239
D3:   DB   10,13,'    WORLD; WHAT GOES OFF ONE SIDE COMES BACK ON THE OTHER.'
         DB    10,13,'    ',239
D4:    DB   'DEFINE THE INITIAL POPULATION BY POSITIONING THE CURSOR AND'
      DB   10,13,'    ',239
D5:    DB   'PRESSING RETURN. ERASE THE SAME WAY. (THE CURSOR IS RED.)'
      DB   10,13,10,13,'    ',239
D6:
    DB   'PRESS R TO START THE GENERATIONS. PRESS P TO PAUSE.'
      DB    10,13,10,13,'    ',239
D7:    DB   'LIFE WILL STOP WHEN THE POPULATION STABILIZES OR DIES.'
      DB   10,13,10,10,'    ',239
D8:    DB   'PRESS ANY KEY TO BEGIN THE DEFINITION STEP...'
       DB    239
D9: DB ' '
D10: DB ' '
         DB    3,64,0
        DB    239
;
;      MESSAGES
;
DEADMSG: DB  3,31,31,11,3,31,31,6,1,'  WORLD DIED  ',239
STABLEMSG: DB  3,31,31,11,3,31,31,6,4,'  WORLD STABILIZED  ',239
ANOTHERMSG: DB 3,0,31,6,2,'ANOTHER ROUND?  ',3,64,0,239
GENERLINE: DB 3,0,0,6,39
        DB  '                    GENERATION          0                      '
        DB  239
BOTTOM2:  DB   3,0,31,6,39
        DB    ' COMMAMDS:   P(AUSE)   O(NE STEP)   S(TOP)   Q(UIT)            '
        DB    3,64,0,239
LOWORD  EQU  BEGSCRN+80
QUITMSG:   DB   3,31,31,11,3,31,31,6,1,'QUITTING AS REQUESTED!!!!',239
STOPMSG:   DB    3,31,31,11,3,31,31,6,1,'STOPPED PER REQUEST.  ',239
DEAD:      DB    0
;     KEYBOARD INTERACTION
KBCHAR:   DB   0
READY:    DB   0
;
;      DATA
;
SCHK:    DB   0     ;STABILITY CHECK FLAG
SAVSTACK:  DW 0
NCS:   DS    2048
ENDNCS:  DS    1
STABLECHECK: DS  2048     ;SPACE TO HOLD COUNT FOR STABILITY CHK
BEGWRLDNCS EQU NCS+64
ENDWRLDNCS  EQU  NCS+2048-64

;
    END START