home *** CD-ROM | disk | FTP | other *** search
- COREWARS Version 3.0
- --------------------
-
- Copyright (c) 1989, 1990 by Daniel Sharpe ccghpds@prism.gatech.edu
- Dept. Math/CSC
- West Georgia College
- Carrollton, GA 30118
-
- This program is copyrighted; it may be legally used without fee only
- for personal and educational uses -- commercial use of this program is
- specifically prohibited.
-
- Source file: CW30.PAS
- Object file: CW30.EXE
-
- Produced from CW21B.PAS, July 1990, in Turbo Pascal 5.5
- Will not compile in any version of Turbo Pascal less than 4.0
-
- This program implements the game of Core War, as described by A.K. Dewdney
- in his book: "The Armchair Universe: An Exploration of Computer Worlds",
- W.H. Freeman and Company, New York, 1988.
-
- A brief description of the game:
-
- The memory of a hypothetical computer is inhabited by several programs
- that fight against each other. A program wins the fight by making all
- the other programs crash or halt. This is accomplished by overwriting
- the other programs with values that will cause them to die. A typical
- value to use is 0, which makes the enemy program lose when it tries to
- execute that 0.
-
- The programs are loaded at random places in memory so they don't have
- advance knowledge of where their enemies are; they may search for the
- enemy or they may shoot blindly in the hopes of hitting another program.
- Programs may also move around in memory by copying themselves to other
- places.
-
- The programs take turns running; each program gets to execute one
- instruction in turn.
-
- The programs are written in an assembly/machine language called RedCode.
- The hypothetical computer is called MARS (Memory Array Redcode Simulator).
-
- In Official Computer Science Jargon, this program simulates a simple com-
- puter running a simple operating system that uses round robin scheduling
- with a quantum size of one instruction. The computer being simulated has
- a two-address architecture -- no registers. It also does not have any
- absolute addressing modes; all operands are either immediate or relative/
- indirect.
-
- No I/O instructions are included, since the architecture is oriented
- towards programs interacting with each other rather than with the outside
- world. And, naturally, there is no protection included; each program is
- responsible for defending itself from attack.
-
- The game has been modified somewhat, as follows:
-
- * The simulated memory is 4000 32-bit words long.
-
- * Up to seven programs may be loaded into memory initially. These can
- spawn children, for a total of up to 36 processes in memory at once.
-
- The simulator stops the game and declares a winner when there is only
- one process left alive. If there is no winner after 32000 rounds then
- the game is declared a draw.
-
- * The system records which program wrote to each memory location last,
- so when a program loses you can tell who it lost to.
-
- * The Protect instruction (PCT) was implemented, but it has been removed
- since it produced too many ties. The Split instruction (SPL) is imple-
- mented. Two additional instructions have been implemented: Program
- Counter Increment (PCI), and Random (RND). These are described later.
-
- * Dewdney is inconsistent as to whether the instruction set has Decrement
- and Jump is Zero (DJZ) or Decrement and Jump if Not Zero (DJN). I have
- included both.
-
- * Operands embedded in the instructions are only 12 bits long, meaning
- a program can access only those memory locations less than 2047 away.
- Pointers used in indirect addressing are full 32-bit integers.
-
- * The auto-increment and auto-decrement addressing modes have not been
- implemented. These are in the queue for possible future versions.
-
- System Requirements:
- -------------------
-
- As of version 2.1b, color VGA is required. The bottleneck on the number
- processes is the amount of screen space required to display their statuses.
- So I made it use 50-line VGA mode. It also uses color to help the user
- distinguish friend from foe more easily.
-
- Modification History:
- --------------------
-
- Changes for version 2.0 include:
-
- * Changed the memory location representation from a record with separate
- fields for opcode, etc., to a full 32-bit
-
- * Changed DAT from an instruction to a pseudo-op.
-
- Changes for version 2.1 include:
-
- * Instructions in Redcode object files no longer have to specify all the
- operands, only those that are used by the instruction. This affects the
- DAT, JMP, HLT, SPL, and PCT instructions. Object files using these
- instructions are not compatible with earlier versions of the loader.
-
- * Provided a means to disassemble memory locations.
-
- Changes for version 2.1a include:
-
- * Fixed some bugs.
-
- * Ported to Turbo Pascal 5.0.
-
- Changes for version 2.1b include:
-
- * Disabled the PCT instruction.
- It was too easy to write a program that was well nigh impossible to kill,
- by using PCT and SPL together.
-
- * Changed the maximum game length to 32000.
-
- * Added a single-step mode.
- Now instead of RUN you can type SS (or S) to go into single-step mode.
- The computer will disassemble and display each instruction as it is
- executed. Press return to go on to the next instruction; press escape
- to switch to run mode.
-
- * Added an instruction to generate a random number.
- The RND instruction has opcode 13. Its first operand gives the upper
- bound for the range of the random number generated; the second operand
- tells where to store the number.
-
- * Changed it to use 50-line VGA text mode.
-
- * Increased the number of processes that can be in the system. The
- number is limited only by the amount of screen space available, so
- using 50-line VGA allows more processes. There can now be up to 36
- processes running at a time. You can load up to 6 programs into memory
- initially.
-
- * It now automatically figures out which process(es) won the fight.
-
- * It now displays each process using a different foreground/background
- color combination; this makes it easier to see which memory location
- belongs to which process.
-
- * It can now dump a specified range of memory to a file.
-
- Changes for version 3.0 include:
-
- * You can now dump memory without having to leave single-step mode first.
-
- * You can now enable and disable the PCT and PCI instructions via commands
- from the keyboard.
-
- * You can use a command-line parameter (CGA) to make it use 25-line CGA
- mode.
-
- Modification Future:
- -------------------
-
- I hope to add the following features in the future:
-
- * Auto-increment and/or -decrement addressing modes.
-
- * An assembler.
-
- * A way to zoom in and out to watch individual skirmishes or see the whole
- battle.
-
- * Pull-down menus and associated User-Friendliness Doodads.
-
- * Better detection and reporting of the outcome. Currently it reports the
- outcome using the process ID codes. It should report the names of the
- ancestor programs that were originally loaded in and spawned the surviving
- processes.
-
- * Revise the color scheme slightly to allow the user to distinguish between
- their program's code and the bombs it has shot at the enemy.
-
- Assorted Formats:
- ----------------
-
- Use the command CW30 CGA to invoke the program in CGA mode. To invoke
- it in VGA mode just use the command CW30. Once in the program the mode
- can't be changed.
-
- All memory data values are 32-bit twos-complement integers.
-
- All instructions have the following format:
- _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
- | | | | | |
- | opcode| m1| operand1 | m2| operand2 |
- |_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|
- 31 28 25 14 11 0
-
- The data file has the following format:
-
- Length EntryPoint Comment
- OpCode0 SrcAdrMode SrcOprnd DstAdrMode DstOprnd Comment
- OpCode1 SrcAdrMode SrcOprnd DstAdrMode DstOprnd Comment
- OpCode2 SrcAdrMode SrcOprnd DstAdrMode DstOprnd Comment
- .
- . (Put each instruction on a separate line.)
- .
-
- Length - The number of lines in the program.
- EntryPoint - The first instruction to be executed. The first line is
- number 0.
- Opcode - The opcode.
- SrcAdrMode - Addressing mode for operand 1 (usually the source).
- SrcOprnd - The value of operand 1.
- DstAdrMode - Addressing mode for operand 2 ( usually the destination).
- DstOprnd - The value of operand 2.
- Comment - Each line can have a comment after the field values.
- Comments can also come after the program.
-
- The screen displays the entire contents of memory; each screen position
- corresponds to two memory locations. It also displays the name of each
- file and where it was loaded in memory, and for each process it displays
- the process ID, its parent's ID, the current program counter, how many
- steps total it has executed, and its status (Running, Killed, Lost, etc.)
-
- The user interface is mostly menu driven. Usually, the Escape key will
- stop the program in whatever it is doing (but not always). There are four
- commands that are not in the menus: PCI ON, PCI OFF, PCT ON, and PCT OFF;
- these enable/disable the PCT and PCI instructions. You can enter these
- commands when the program is prompting for a RedCode file name.
-
- There are three addressing modes:
-
- Mode Definition Source code Object code
- ---- ---------- ----------- -----------
- Immediate operand #operand 0
- PC-relative Mem[PC+operand] operand 1
- Indirect MEM[PC+operand+Mem[PC+operand]] @operand 2
-
- There are, currently, 15 instructions (including the DAT pseudo-op):
-
- Mnemonic Definition Opcode
- -------- ---------- ------
- DAT Store value into memory location at load 0
- time. Value is sign-extended to 32 bits.
-
- MOV Copy source operand to destination. 1
-
- ADD Replace destination with dest + source. 2
-
- SUB Replace destination with dest - source. 3
-
- JMP Jump to the instruction specified by the 4
- first operand.
-
- JMZ If second operand is zero then jump to the 5
- location specified by the first operand.
-
- JMG If second operand > 0 then jump to first. 6
-
- DJZ Decrement second operand, then if result is 7
- zero jump to first.
-
- CMP If the operands are not equal then skip the 8
- next instruction.
-
- HLT Halt the program and lose the game. This 9
- instruction is redundant.
-
- SPL Split into two clones. One continues 10
- executing at the next location; the other
- continues at the location specified by the
- first operand. If a program tries to split
- but the process table is full, it is killed.
-
- PCT The specified location can not be modified 11
- until after the next time it is executed.
-
- DJN Decrement second opernad, then if result is 12
- not zero jump to the first operand.
-
- RND Generate a random number in the range 13
- 0..first operand; store it in the second
- operand.
-
- PCI Change the increment that's added to the 14
- program counter after each instruction.
-
- Example Redcode Programs:
- ------------------------
-
- 1. The Imp:
-
- 1 0 <--- First line of the file.
- 1 1 0 1 1 mov 0,1
-
- *** Imp *** <--- Last line of the file.
-
- The Imp is one instruction long. This instruction copies itself to
- the next memory location, which is the next one to be executed.
-
- 2. The Dwarf (subspecies 5):
-
- 4 1 <--- First line of the file.
- 0 -1 DAT -1
- 2 0 5 1 -1 ADD #5,-1
- 1 0 0 2 -2 MOV #0,@-2
- 4 1 -2 JMP -2
-
- *** Dwarf5 *** <--- Last line of the file.
-
- This Dwarf stores a 0 in every fifth memory location, starting with
- the one immediately following the program.
-
- 3. The Backwards-Moving Imp:
-
- 2 1 <--- First line.
- 1 1 0 1 -1 MOV 0,-1
- 14 0 -1 PCI #-1
-
- ***** BIMP -- the backwards moving imp ***** <--- Last line.
-
- The Bimp is just like the Imp, except it runs backwards through memory,
- instead of forwards.
-
- 4. The Imp Gun:
-
- 2 0 <--- First line of the file.
- 10 1 0 SPL 0
- 1 1 0 1 1 MOV 0,1
-
- ***** Imp Gun ***** <--- Last line of the file.
-
- This Imp Gun spawns as many children as it can. Each child is an imp.
-
- 5. The Imp Pit:
-
- 2 0 <--- First line.
- 1 0 0 1 -1 MOV #0,-1
- 4 1 -1 JMP -1
-
- ***** Imp Pit ***** <--- Last line.
-
- This Imp Pit has 50% chance of swallowing up any Imp that falls into it.