home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / EDUCATIO / CW30.ZIP / CW30.DOC < prev    next >
Encoding:
Text File  |  1990-07-19  |  14.0 KB  |  347 lines

  1. COREWARS Version 3.0
  2. --------------------
  3.  
  4.    Copyright (c) 1989, 1990 by Daniel Sharpe        ccghpds@prism.gatech.edu
  5.                                Dept. Math/CSC
  6.                                West Georgia College
  7.                                Carrollton, GA  30118
  8.  
  9.    This program is copyrighted; it may be legally used without fee only
  10.    for personal and educational uses -- commercial use of this program is
  11.    specifically prohibited.
  12.  
  13.    Source file: CW30.PAS
  14.    Object file: CW30.EXE
  15.  
  16.    Produced from CW21B.PAS, July 1990, in Turbo Pascal 5.5
  17.    Will not compile in any version of Turbo Pascal less than 4.0
  18.  
  19.    This program implements the game of Core War, as described by A.K. Dewdney
  20.    in his book: "The Armchair Universe: An Exploration of Computer Worlds",
  21.    W.H. Freeman and Company, New York, 1988.
  22.  
  23. A brief description of the game:
  24.  
  25.    The memory of a hypothetical computer is inhabited by several programs
  26.    that fight against each other.  A program wins the fight by making all
  27.    the other programs crash or halt.  This is accomplished by overwriting
  28.    the other programs with values that will cause them to die.  A typical
  29.    value to use is 0, which makes the enemy program lose when it tries to
  30.    execute that 0.
  31.  
  32.    The programs are loaded at random places in memory so they don't have
  33.    advance knowledge of where their enemies are; they may search for the
  34.    enemy or they may shoot blindly in the hopes of hitting another program.
  35.    Programs may also move around in memory by copying themselves to other
  36.    places.
  37.  
  38.    The programs take turns running; each program gets to execute one
  39.    instruction in turn.
  40.  
  41.    The programs are written in an assembly/machine language called RedCode.
  42.    The hypothetical computer is called MARS (Memory Array Redcode Simulator).
  43.  
  44. In Official Computer Science Jargon, this program simulates a simple com-
  45. puter running a simple operating system that uses round robin scheduling
  46. with a quantum size of one instruction.  The computer being simulated has
  47. a two-address architecture -- no registers.  It also does not have any
  48. absolute addressing modes; all operands are either immediate or relative/
  49. indirect.
  50.  
  51. No I/O instructions are included, since the architecture is oriented
  52. towards programs interacting with each other rather than with the outside
  53. world.  And, naturally, there is no protection included; each program is
  54. responsible for defending itself from attack.
  55.  
  56. The game has been modified somewhat, as follows:
  57.  
  58.    * The simulated memory is 4000 32-bit words long.
  59.  
  60.    * Up to seven programs may be loaded into memory initially.  These can
  61.      spawn children, for a total of up to 36 processes in memory at once.
  62.  
  63.      The simulator stops the game and declares a winner when there is only
  64.      one process left alive.  If there is no winner after 32000 rounds then
  65.      the game is declared a draw.
  66.  
  67.    * The system records which program wrote to each memory location last,
  68.      so when a program loses you can tell who it lost to.
  69.  
  70.    * The Protect instruction (PCT) was implemented, but it has been removed
  71.      since it produced too many ties.  The Split instruction (SPL) is imple-
  72.      mented.  Two additional instructions have been implemented: Program
  73.      Counter Increment (PCI), and Random (RND).  These are described later.
  74.  
  75.    * Dewdney is inconsistent as to whether the instruction set has Decrement
  76.      and Jump is Zero (DJZ) or Decrement and Jump if Not Zero (DJN).  I have
  77.      included both.
  78.  
  79.    * Operands embedded in the instructions are only 12 bits long, meaning
  80.      a program can access only those memory locations less than 2047 away.
  81.      Pointers used in indirect addressing are full 32-bit integers.
  82.  
  83.    * The auto-increment and auto-decrement addressing modes have not been
  84.      implemented.  These are in the queue for possible future versions.
  85.  
  86. System Requirements:
  87. -------------------
  88.  
  89.    As of version 2.1b, color VGA is required.  The bottleneck on the number
  90.    processes is the amount of screen space required to display their statuses.
  91.    So I made it use 50-line VGA mode.  It also uses color to help the user
  92.    distinguish friend from foe more easily.
  93.  
  94. Modification History:
  95. --------------------
  96.  
  97.    Changes for version 2.0 include:
  98.  
  99.    * Changed the memory location representation from a record with separate
  100.      fields for opcode, etc., to a full 32-bit
  101.  
  102.    * Changed DAT from an instruction to a pseudo-op.
  103.  
  104.    Changes for version 2.1 include:
  105.  
  106.    * Instructions in Redcode object files no longer have to specify all the
  107.      operands, only those that are used by the instruction.  This affects the
  108.      DAT, JMP, HLT, SPL, and PCT instructions.  Object files using these
  109.      instructions are not compatible with earlier versions of the loader.
  110.  
  111.    * Provided a means to disassemble memory locations.
  112.  
  113.    Changes for version 2.1a include:
  114.  
  115.    * Fixed some bugs.
  116.  
  117.    * Ported to Turbo Pascal 5.0.
  118.  
  119.    Changes for version 2.1b include:
  120.  
  121.    * Disabled the PCT instruction.
  122.      It was too easy to write a program that was well nigh impossible to kill,
  123.      by using PCT and SPL together.
  124.  
  125.    * Changed the maximum game length to 32000.
  126.  
  127.    * Added a single-step mode.
  128.      Now instead of RUN you can type SS (or S) to go into single-step mode.
  129.      The computer will disassemble and display each instruction as it is
  130.      executed.  Press return to go on to the next instruction; press escape
  131.      to switch to run mode.
  132.  
  133.    * Added an instruction to generate a random number.
  134.      The RND instruction has opcode 13.  Its first operand gives the upper
  135.      bound for the range of the random number generated; the second operand
  136.      tells where to store the number.
  137.  
  138.    * Changed it to use 50-line VGA text mode.
  139.  
  140.    * Increased the number of processes that can be in the system.  The
  141.      number is limited only by the amount of screen space available, so
  142.      using 50-line VGA allows more processes.  There can now be up to 36
  143.      processes running at a time.  You can load up to 6 programs into memory
  144.      initially.
  145.  
  146.    * It now automatically figures out which process(es) won the fight.
  147.  
  148.    * It now displays each process using a different foreground/background
  149.      color combination; this makes it easier to see which memory location
  150.      belongs to which process.
  151.  
  152.    * It can now dump a specified range of memory to a file.
  153.  
  154.    Changes for version 3.0 include:
  155.  
  156.    * You can now dump memory without having to leave single-step mode first.
  157.  
  158.    * You can now enable and disable the PCT and PCI instructions via commands
  159.      from the keyboard.
  160.  
  161.    * You can use a command-line parameter (CGA) to make it use 25-line CGA
  162.      mode.
  163.  
  164. Modification Future:
  165. -------------------
  166.  
  167.    I hope to add the following features in the future:
  168.  
  169.    * Auto-increment and/or -decrement addressing modes.
  170.  
  171.    * An assembler.
  172.  
  173.    * A way to zoom in and out to watch individual skirmishes or see the whole
  174.      battle.
  175.  
  176.    * Pull-down menus and associated User-Friendliness Doodads.
  177.  
  178.    * Better detection and reporting of the outcome.  Currently it reports the
  179.      outcome using the process ID codes.  It should report the names of the
  180.      ancestor programs that were originally loaded in and spawned the surviving
  181.      processes.
  182.  
  183.    * Revise the color scheme slightly to allow the user to distinguish between
  184.      their program's code and the bombs it has shot at the enemy.
  185.  
  186. Assorted Formats:
  187. ----------------
  188.  
  189.    Use the command CW30 CGA to invoke the program in CGA mode.  To invoke
  190.    it in VGA mode just use the command CW30.  Once in the program the mode
  191.    can't be changed.
  192.  
  193.    All memory data values are 32-bit twos-complement integers.
  194.  
  195.    All instructions have the following format:
  196.        _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
  197.       |       |   |                       |   |                       |
  198.       | opcode| m1|        operand1       | m2|        operand2       |
  199.       |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
  200.       31    28     25                   14     11                    0
  201.  
  202.    The data file has the following format:
  203.  
  204.       Length   EntryPoint                                      Comment
  205.       OpCode0  SrcAdrMode   SrcOprnd   DstAdrMode   DstOprnd   Comment
  206.       OpCode1  SrcAdrMode   SrcOprnd   DstAdrMode   DstOprnd   Comment
  207.       OpCode2  SrcAdrMode   SrcOprnd   DstAdrMode   DstOprnd   Comment
  208.          .
  209.          .   (Put each instruction on a separate line.)
  210.          .
  211.  
  212.       Length     - The number of lines in the program.
  213.       EntryPoint - The first instruction to be executed.  The first line is
  214.                    number 0.
  215.       Opcode     - The opcode.
  216.       SrcAdrMode - Addressing mode for operand 1 (usually the source).
  217.       SrcOprnd   - The value of operand 1.
  218.       DstAdrMode - Addressing mode for operand 2 ( usually the destination).
  219.       DstOprnd   - The value of operand 2.
  220.       Comment    - Each line can have a comment after the field values.
  221.                    Comments can also come after the program.
  222.  
  223.    The screen displays the entire contents of memory; each screen position
  224.    corresponds to two memory locations.  It also displays the name of each
  225.    file and where it was loaded in memory, and for each process it displays
  226.    the process ID, its parent's ID, the current program counter, how many
  227.    steps total it has executed, and its status (Running, Killed, Lost, etc.)
  228.  
  229.    The user interface is mostly menu driven.  Usually, the Escape key will
  230.    stop the program in whatever it is doing (but not always).  There are four
  231.    commands that are not in the menus: PCI ON, PCI OFF, PCT ON, and PCT OFF;
  232.    these enable/disable the PCT and PCI instructions.  You can enter these
  233.    commands when the program is prompting for a RedCode file name.
  234.  
  235. There are three addressing modes:
  236.  
  237.      Mode        Definition                        Source code  Object code
  238.      ----        ----------                        -----------  -----------
  239.    Immediate     operand                             #operand        0
  240.    PC-relative   Mem[PC+operand]                      operand        1
  241.    Indirect      MEM[PC+operand+Mem[PC+operand]]     @operand        2
  242.  
  243. There are, currently, 15 instructions (including the DAT pseudo-op):
  244.  
  245.     Mnemonic      Definition                                    Opcode
  246.     --------      ----------                                    ------
  247.       DAT         Store value into memory location at load         0
  248.                   time.  Value is sign-extended to 32 bits.
  249.  
  250.       MOV         Copy source operand to destination.              1
  251.  
  252.       ADD         Replace destination with dest + source.          2
  253.  
  254.       SUB         Replace destination with dest - source.          3
  255.  
  256.       JMP         Jump to the instruction specified by the         4
  257.                   first operand.
  258.  
  259.       JMZ         If second operand is zero then jump to the       5
  260.                   location specified by the first operand.
  261.  
  262.       JMG         If second operand > 0 then jump to first.        6
  263.  
  264.       DJZ         Decrement second operand, then if result is      7
  265.                   zero jump to first.
  266.  
  267.       CMP         If the operands are not equal then skip the      8
  268.                   next instruction.
  269.  
  270.       HLT         Halt the program and lose the game.  This        9
  271.                   instruction is redundant.
  272.  
  273.       SPL         Split into two clones.  One continues           10
  274.                   executing at the next location; the other
  275.                   continues at the location specified by the
  276.                   first operand.  If a program tries to split
  277.                   but the process table is full, it is killed.
  278.  
  279.       PCT         The specified location can not be modified      11
  280.                   until after the next time it is executed.
  281.  
  282.       DJN         Decrement second opernad, then if result is     12
  283.                   not zero jump to the first operand.
  284.  
  285.       RND         Generate a random number in the range           13
  286.                   0..first operand; store it in the second
  287.                   operand.
  288.  
  289.       PCI         Change the increment that's added to the        14
  290.                   program counter after each instruction.
  291.  
  292. Example Redcode Programs:
  293. ------------------------
  294.  
  295.    1. The Imp:
  296.  
  297.          1  0                            <--- First line of the file.
  298.          1  1  0  1  1  mov 0,1
  299.  
  300.         *** Imp ***                      <--- Last line of the file.
  301.  
  302.       The Imp is one instruction long.  This instruction copies itself to
  303.       the next memory location, which is the next one to be executed.
  304.  
  305.    2. The Dwarf (subspecies 5):
  306.  
  307.          4  1                            <--- First line of the file.
  308.          0  -1           DAT  -1
  309.          2  0  5  1 -1   ADD  #5,-1
  310.          1  0  0  2 -2   MOV  #0,@-2
  311.          4  1 -2         JMP  -2
  312.  
  313.         *** Dwarf5 ***                   <--- Last line of the file.
  314.  
  315.       This Dwarf stores a 0 in every fifth memory location, starting with
  316.       the one immediately following the program.
  317.  
  318.    3. The Backwards-Moving Imp:
  319.  
  320.          2  1                                          <--- First line.
  321.          1  1  0  1 -1     MOV 0,-1
  322.         14  0 -1           PCI #-1
  323.  
  324.         ***** BIMP -- the backwards moving imp *****   <--- Last line.
  325.  
  326.       The Bimp is just like the Imp, except it runs backwards through memory,
  327.       instead of forwards.
  328.  
  329.    4. The Imp Gun:
  330.  
  331.          2  0                            <--- First line of the file.
  332.         10  1  0           SPL  0
  333.          1  1  0  1  1     MOV  0,1
  334.  
  335.         ***** Imp Gun *****              <--- Last line of the file.
  336.  
  337.       This Imp Gun spawns as many children as it can.  Each child is an imp.
  338.  
  339.    5. The Imp Pit:
  340.  
  341.          2  0                                          <--- First line.
  342.          1  0  0  1 -1         MOV  #0,-1
  343.          4  1 -1               JMP  -1
  344.  
  345.         ***** Imp Pit *****                            <--- Last line.
  346.  
  347.       This Imp Pit has 50% chance of swallowing up any Imp that falls into it.