home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TIERRA40.ZIP / TIERRA / TIERRA.H < prev    next >
C/C++ Source or Header  |  1992-09-09  |  15KB  |  407 lines

  1. /* tierra.h  9-9-92 */
  2. /* type, structure and function definitions for the Tierra Simulator */
  3. /* Tierra Simulator V4.0: Copyright (c) 1991, 1992 Tom Ray & Virtual Life */
  4.  
  5. /*
  6.  * tierra_h_sccsid: @(#)tierra.h    1.5    7/21/92
  7.  */
  8.  
  9. #include "license.h"
  10.  
  11. #ifndef LTIERRA_H
  12. #define LTIERRA_H
  13.  
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <errno.h>
  19. #include "configur.h"
  20. #include "portable.h"
  21. #ifndef ANSI
  22. #include <varargs.h>
  23. #else /* ANSI */
  24. #include <stdarg.h>
  25. #endif /* ANSI */
  26.  
  27. #ifdef ALCOMM
  28. #include <mlayer.h>
  29. #endif
  30.  
  31. #ifdef unix
  32. #if FRONTEND == BASIC
  33. #include <curses.h>
  34. #endif /*FRONTEND == BASIC */
  35. #define KEYHIT() ( TC_Menu )
  36. #endif /* unix */
  37.  
  38. #ifdef __TURBOC__
  39. #define KEYHIT() ( kbhit() )
  40. #endif /* TURBOC */
  41.  
  42. #define ONE (I32u) 1
  43.  
  44. #define tulrand() ((I32u) (tdrand() * (double) ULONG_MAX))
  45. /* returns random 32 bit positive signed integer */
  46. #define tlrand() ((I32s) (tdrand() * (double) LONG_MAX))
  47. /* returns random 32 bit positive signed integer */
  48. #define tirand() ((I16s) (tdrand() * (double) INT_MAX))
  49. /* returns random 16 bit positive signed integer */
  50. #define tuirand() ((I16u) (tdrand() * (double) UINT_MAX))
  51. /* returns random 16 bit unsigned integer */
  52. #define tcrand() ((I8s) (tdrand() * (double) CHAR_MAX))
  53. /* returns random 8 bit positive signed integer */
  54. #define tucrand() ((I8u) (tdrand() * (double) UCHAR_MAX))
  55. /* returns random 8 bit unsigned integer */
  56.  
  57. #define SetFlag(A)  (A->c.fl = 1)
  58. #define IsBit(seed, bit) ((((I32u) seed) & (ONE << ((I32u) bit))) ? 1 : 0)
  59. #define ad(A) ((A) >=0 ? ((A)%SoupSize) \
  60.                : ((SoupSize-(-(A)%SoupSize))%SoupSize))
  61. #define mo(A,B) ((A) >=0 ? ((A)%(B)) : (((B)-(-(A)%(B)))%(B)))
  62.  
  63. #define WRITE 1
  64. #define NOWRITE 0
  65. #define EXIT 1
  66. #define NOEXIT 0
  67. #define PLN_STATS 1
  68. #define SIZ_HIST 2
  69. #define SIZM_HIST 3
  70. #define GEN_HIST 4
  71.  
  72. #if INSTBITNUM == 5
  73.  
  74. #ifdef __TURBOC__
  75.  
  76. typedef struct {
  77.     unsigned int inst:5;
  78.     unsigned int exec:1;  /* 0 = unprotected, 1 = protected */
  79.     unsigned int write:1; /* 0 = unprotected, 1 = protected */
  80.     unsigned int read:1;  /* 0 = unprotected, 1 = protected */
  81. } Inst;
  82.  
  83. #else /* __TURBOC__ */
  84. #ifdef __TRS6000__
  85. typedef struct {
  86.     unsigned int inst:5;
  87.     unsigned int exec:1;  /* 0 = unprotected, 1 = protected */
  88.     unsigned int write:1; /* 0 = unprotected, 1 = protected */
  89.     unsigned int read:1;  /* 0 = unprotected, 1 = protected */
  90. } Inst;
  91.  
  92. #else /* __TURBOC__ */
  93.  
  94. typedef struct {
  95.     unsigned char inst:5;
  96.     unsigned char exec:1;  /* 0 = unprotected, 1 = protected */
  97.     unsigned char write:1; /* 0 = unprotected, 1 = protected */
  98.     unsigned char read:1;  /* 0 = unprotected, 1 = protected */
  99. } Inst;
  100.  
  101. #endif /* __TRS6000__ */
  102. #endif /* __TURBOC__ */
  103.  
  104. #endif /* INSTBITNUM == 5 */
  105.  
  106.  
  107. #if PLOIDY == 1
  108.  
  109. typedef Inst Instruction;
  110.  
  111. #else 
  112.  
  113. typedef Inst Instruction[PLOIDY];
  114.  
  115. #endif /* PLOIDY == 1 */
  116.  
  117. typedef Instruction Hp HpInst;
  118. typedef Instruction Fp FpInst;
  119. typedef Instruction Np NpInst;
  120. typedef Inst Fp FpIns;
  121.  
  122. #if PLOIDY == 1
  123. typedef I8s GenBits;
  124. #else /* PLOIDY > 1 */
  125. typedef I8s GenBits[PLOIDY];
  126. #endif /* PLOIDY > 1 */
  127.  
  128. typedef GenBits Fp FpGenB;
  129.  
  130. typedef struct cell       Cell;
  131. typedef struct cell Fp    Pcells;
  132. typedef struct g_list     GList;
  133. typedef struct g_list Fp  Pgl;
  134.  
  135. typedef I32s  Reg;   /* type for use in CPU registers */
  136. typedef Reg   *Preg; /* pointer to register */
  137.  
  138. typedef struct { /* struct for passing arguments from parse to execute */
  139.  
  140.     Preg   sreg; /* pointer to source register */
  141.     FpIns  sins; /* pointer to source instruction */
  142.     I32s   sval; /* source value */
  143.     I8s    stra; /* source track */
  144.  
  145.     Preg   dreg;  /* pointer to destination register */
  146.     I32s   dval;  /* original destination value */
  147.     FpIns  dins;  /* pointer to destination instruction */
  148.     I8s    dtra;  /* destination track */
  149.     I32s   dmod;  /* destination modulused positive this size */
  150.     I32s   dran;  /* destination kept in signed range of this value */
  151.     Pcells dcel;  /* destination cell */
  152.  
  153.     Preg   sreg2; /* pointer to 2nd source register */
  154.     FpIns  sins2; /* pointer to 2nd source instruction */
  155.     I32s   sval2; /* 2nd source value */
  156.     I8s    stra2; /* 2nd source track */
  157.  
  158.     Preg   dreg2; /* pointer to 2nd destination register */
  159.     I32s   dval2; /* original destination value */
  160.     FpIns  dins2; /* pointer to 2nd destination instruction */
  161.     I8s    dtra2; /* 2nd destination track */
  162.     I32s   dmod2; /* 2nd dest modulused positive this size */
  163.     I32s   dran2; /* 2nd dest kept in signed range of this value */
  164.  
  165.     I32s   sval3; /* 3rd source value */
  166.     I32s   dval3; /* original destination value */
  167.  
  168.     Preg   dreg3; /* pointer to 3rd destination register */
  169.     I32s   dmod3; /* 3rd dest modulused positive this size */
  170.     I32s   dran3; /* 3rd dest kept in signed range of this value */
  171.  
  172.     I8s    mode;  /* mode of instruction */
  173.     I8s    mode2; /* 2nd mode of instruction */
  174.     I8s    mode3; /* 3rd mode of instruction */
  175.  
  176.     I8s    expr;  /* execute protection 0 = no protection, 1 = protected */
  177.     I32s   oip;   /* address of instruction being executed: ce->c.ip */
  178.     FpIns  eins;  /* pointer to instruction being executed */
  179.     I8s    iip;   /* amount to increment instruction pointer */
  180.     I8s    dib;   /* amount to decrement instruction bank */
  181.     I16s   ts;    /* size of time slice, used to control central loop */
  182. } PInst;
  183.  
  184. typedef struct { /* structure for instruction set definitions */
  185.     I8s op;                      /* op code */
  186.     I8s mn[9];                   /* assembler mnemonic */
  187.     void (*execute) P_((Cell  *));  /* pointer to execute function */
  188.     void (*parse) P_((Cell  *));    /* pointer to parse function */
  189. } InstDef;
  190.  
  191. typedef struct {  /* structure for time measured in executed instructions */
  192.     I32s m;     /* count of millions of instructions */
  193.     I32s i;     /* count of instructions */
  194. } Event;
  195.  
  196. typedef struct {
  197.     I32s size;        /* size class (~species) */
  198.     I8s label[4];     /* genotype label */
  199. } Genotype;
  200.  
  201. typedef struct {
  202.     I32s si;           /* size index */
  203.     I16s gi;           /* genotype index */
  204. } GlIndex;
  205.  
  206. typedef struct {  /* structure for metabolic data */
  207.     I32s  inst;      /* count of instructions executed */
  208.     I32s  flags;     /* count of flags (error conditions) set */
  209.     I32s  mov_daught;/* count of number of instructions moved to daughter */
  210.     I8s   BreedTrue; /* 0=not, 1 = this daughter genetically same as parent */
  211. } Metabolism;
  212.  
  213. typedef struct { /* structure for indexing cells in the cells arrays */
  214.     I16u  a; /* which array */
  215.     I16u  i; /* which element of the array */
  216. } CellInd;
  217.  
  218. typedef struct { /* structure for demographic data */
  219.     Genotype gen;   /* size and genotype name of cell */
  220.     I16s gi;         /* index to this genotype in the gl array */
  221.     I32s hash;       /* hash value for this genotype */
  222.     I32s fecundity;  /* count of number of daughter cells produced */
  223.     I32s flags;      /* count of flags (error conditions) set */
  224.     I32s mov_daught; /* count of number of instructions moved to daughter */
  225.     I32s inst;       /* count of instructions executed */
  226.     I32s mut;        /* 0 = no somatic mutations, >= 1 = somatic mutations */
  227.     I32s flaw;       /* 0 = no flaws, >= 1 = flaws */
  228.     Metabolism d1;   /* metabolic data for first daughter */
  229.     Genotype parent; /* size and genotype name of parent */
  230.     I16s ib;         /* instruction bank */
  231.     CellInd  ne;     /* address of daughter cell */
  232.     I8s is;        /* 1 = this cpu is active, in the slicer; 0 = not active */
  233.     I8s tr;             /* which track currently being executed */
  234.     I8s dm;        /* 0 = mother, 1 = daughter */
  235.     I8s ploidy;    /* how many tracks */
  236.     FpInst genome; /* pointer to genome itself */
  237. #ifdef HSEX
  238.     I32s mate_addr;   /* soup address of mate */
  239.     I16s x_over_addr; /* # bytes into gen for Xover, - first half, + second */
  240. #endif /* HSEX */
  241. } Dem;
  242.  
  243. typedef struct { /* pointers to this, previous and next cells in queues */
  244.     CellInd  this;   /* index of this cell */
  245.     CellInd  n_time;   /* index to next cell in slicer queue */
  246.     CellInd  p_time;   /* index to previous cell in slicer queue */
  247.     CellInd  n_reap;   /* index to next cell in reaper queue */
  248.     CellInd  p_reap;   /* index to previous cell in reaper queue */
  249. } Que;
  250.  
  251. typedef struct {   /* structure for allocated memory block of cell */
  252.     I32s p;     /* location of start of cell memory */
  253.     I32s s;    /* size of cell memory */
  254. } Mem;
  255.  
  256. #if INST == 1
  257.  
  258. typedef struct {        /* structure for registers of virtual cpu */
  259.     Reg re[ALOC_REG];   /* array of registers */
  260.     Reg sp;             /* stack pointer */
  261.     Reg st[STACK_SIZE]; /* stack */
  262.     Reg ip;             /* instruction pointer */
  263.     I8s fl;             /* flag */
  264. } Cpu;
  265.  
  266. #else /* INST == 1 */
  267.  
  268. typedef struct {         /* structure for registers of virtual cpu */
  269.     Reg re[ALOC_REG];    /* array of registers */
  270.     Reg sp;              /* stack pointer */
  271.     Reg st[STACK_SIZE];  /* stack */
  272.     Reg gb[GETBUFSIZ+3]; /* input buffer */
  273.     Reg pb[PUTBUFSIZ+3]; /* output buffer */
  274.     Reg ip;              /* instruction pointer */
  275.     I8s fl;              /* flag */
  276. } Cpu;
  277.  
  278. #endif /* INST == 1 */
  279.  
  280. struct cell {   /* structure for cell of organisms */
  281.     Dem d;  /* fecundity and times and dates of birth and death */
  282.     Que q;  /* pointers to previous and next cells in queues */
  283.     Mem mm; /* main cell memory */
  284.     Mem md; /* daughter cell memory */
  285.     Cpu c;  /* virtual cpu */
  286.     I8s ld; /* 0 = dead, 1 = alive */
  287. } ;     /* sizeof(struct cell) = XX */
  288.  
  289. /* Structures for new soup allocator. CJS, July 1992. */
  290.  
  291. typedef struct {      /* Describes a free area of the soup */ 
  292.   I32s    l,          /* Index of left son in cartesian tree; 
  293.                          or index of 1st recycled node (>0);
  294.                          or -(number of untouched nodes)   */
  295.           r,          /* Index of rt son in cartesian tree */
  296.           p,          /* Soup addr of this unoccupied area */
  297.           s;          /* Size of the area (in instr slots) */
  298. } MemFr ;
  299.  
  300. typedef MemFr Fp  Pmf;
  301.  
  302. struct g_list { /* structure for genotype list */
  303.     I32s pop;   /* current number of adults of this genotype in soup */
  304.     Genotype gen;   /* genotype of creature */
  305.     Genotype parent;/* genotype of parent genotype (ancestor) */
  306.     I32s hash;      /* hash number for genome identification */
  307.     I32u bits;             /* see below */
  308.     Metabolism d1;  /* metabolic data for first daughter */
  309.     Metabolism d2;  /* metabolic data for second daughter */
  310.     Event originI;  /* time of origin, in instruction time */
  311.     I32s originC;      /* time of first origin of genotype, in clock time */
  312.     float MaxPropPop;  /* max. propor. of NumCells reached by this gen. */
  313.     float MaxPropInst; /* max. propor. of SoupSize reached by this gen. */
  314.     Event mpp_time;    /* most recent time of MaxPropPop update */
  315.     I8s ploidy;        /* how many tracks */
  316.     I8s track;         /* which track are we on now ... */
  317.     FpInst genome;     /* pointer to genome itself */
  318.     FpGenB gbits;      /* pointer to genome bit field */
  319.     struct g_list  *b;  /* next (below) in queue */
  320.     struct g_list  *a;  /* previous (above) in queue */
  321. } ;
  322.  
  323. typedef struct {    /* structure for size list */
  324.     I32s num_c;    /* # adult creatures of this size in soup */
  325.     I16s num_g;    /* # genotypes of this size extant in soup */
  326.     I16s a_num;        /* allocated size of *g array */
  327.     GList **g; /* array of GList structures */
  328. } SList;
  329.  
  330. typedef struct {
  331.   I16s  size ;
  332.   I16s  lbl;
  333.   I32s  count;
  334.   I8u   nstar;
  335.   } HistType;
  336.  
  337.  
  338. /* definitions of bits:
  339.     bit  0  permanent genotype name, saved in .gen file
  340.     bit  1  swapped out to disk from the rambank, saved in .mem file
  341.     bit  2  EXs = executes own instructions (self)
  342.     bit  3  EXd = executes daughter's instructions
  343.     bit  4  EXo = executes other cell's instructions
  344.     bit  5  EXf = executes instructions in free memory
  345.     bit  6  EXh = own instructions are executed by other creature (host)
  346.     bit  7  TCs = matches template complement of self
  347.     bit  8  TCd = matches template complement of daughter
  348.     bit  9  TCo = matches template complement of other
  349.     bit 10  TCf = matches template complement of free memory
  350.     bit 11  TCh = own template complement is matched by other creature (host)
  351.     bit 12  TPs = uses template pattern of self
  352.     bit 13  TPd = uses template pattern of daughter
  353.     bit 14  TPo = uses template pattern of other
  354.     bit 15  TPf = uses template pattern of free memory
  355.     bit 16  TPh = own template pattern is used by other creature (host)
  356.     bit 17  MFs = moves instruction from self
  357.     bit 18  MFd = moves instruction from daughter
  358.     bit 19  MFo = moves instruction from other cell
  359.     bit 20  MFf = moves instruction from free memory
  360.     bit 21  MFh = own instructions are moved by other creature (host)
  361.     bit 22  MTs = moves instruction to self
  362.     bit 23  MTd = moves instruction to daughter
  363.     bit 24  MTo = moves instruction to other cell
  364.     bit 25  MTf = moves instruction to free memory
  365.     bit 26  MTh = is written on by another creature (host)
  366.     bit 27  MBs = executing other creatures code, moves inst from self
  367.     bit 28  MBd = executing other creatures code, moves inst from daughter
  368.     bit 29  MBo = executing other creatures code, moves inst from other cell
  369.     bit 30  MBf = executing other creatures code, moves inst from free memory
  370.     bit 31  MBh = other creature uses another cpu to move your instructions
  371. */
  372.  
  373. typedef struct {    /* record of last data output to disk */
  374.     I32s  time;     /* elapsed time */
  375.     I32s  ctime;    /* millions of instructions */
  376.     I8s   bd;       /* b = birth, d = death */
  377.     I32s  size;     /* size of creature */
  378.     I8s   label[4]; /* genotype name of creature, e.g., aaa */
  379. } LastOut;
  380.  
  381. typedef struct {
  382.     I8s   magic[4];
  383.     I32s  g_off;    /* ofsett where genomes begin */
  384.     I16s  size;     /* size of genome */
  385.     I16s  n;        /* number of genomes in bank */
  386.     I16s  n_alloc;  /* allocated size of bank */
  387. } head_t;
  388.  
  389. typedef struct {
  390.     I8s   gen[3];
  391.     I8s   pgen[3];
  392.     I16s  psize;
  393.     I32s  hash;
  394.     I32u  bits;
  395.     I32s  originC;
  396.     I16s  mpp, mpi;
  397.     Event mppT;         /* last time of mpp update */
  398.     I32s  ptr;           /* reserved for future versions */
  399.     Event originI;
  400.     Metabolism d1, d2;
  401.     I8s   pt;           /* ploidy and track */
  402. } indx_t;
  403.  
  404. #include "prototyp.h"
  405.  
  406. #endif /* LTIERRA_H */
  407.