home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / cm2 / cmcllmrg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-04  |  5.5 KB  |  220 lines

  1. /*
  2.  * File: cm_cellmargolus.c
  3.  *   By: Dave Hiebeler
  4.  *       June 1989
  5.  *
  6.  * Cellsim, cellular automata simulator
  7.  *
  8.  * Routines for running margolus rules
  9.  *
  10.  */
  11.  
  12.  
  13. #include "cm_cell.h"
  14.  
  15. /*
  16.  *
  17.  * Cellsim copyright 1989, 1990 by Chris Langton and Dave Hiebeler
  18.  * (cgl@lanl.gov, hiebeler@heretic.lanl.gov)
  19.  *
  20.  * This package may be freely distributed, as long as you don't:
  21.  * - remove this notice
  22.  * - try to make money by doing so
  23.  * - prevent others from copying it freely
  24.  * - distribute modified versions without clearly documenting your changes
  25.  *   and notifying us
  26.  *
  27.  * Please contact either of the authors listed above if you have questions
  28.  * or feel an exception to any of the above restrictions is in order.
  29.  *
  30.  * If you make changes to the code, or have suggestions for changes,
  31.  * let us know!  If we use your suggestion, you will receive full credit
  32.  * of course.
  33.  */
  34.  
  35. /*****
  36.  * Cellsim history:
  37.  *
  38.  * Cellsim was originally written on Apollo workstations by Chris Langton.
  39.  *
  40.  * Sun versions:
  41.  *
  42.  * - version 1.0
  43.  *   by C. Ferenbaugh and C. Langton
  44.  *   released 09/02/88
  45.  *
  46.  * - version 1.5
  47.  *   by Dave Hiebeler and C. Langton  May - June 1989
  48.  *   released 07/03/89
  49.  *
  50.  * - version 2.0
  51.  *   by Dave Hiebeler and C. Langton  July - August 1989
  52.  *   never officially released (unofficially released 09/08/89)
  53.  *
  54.  * - version 2.5
  55.  *   by Dave Hiebeler and C. Langton  September '89 - February 1990
  56.  *   released 02/26/90
  57.  *****/
  58.  
  59.  
  60.  
  61. int auto_marg();
  62. void probe_marg();
  63.  
  64.  
  65. /* set up nhood-dependent stuff */
  66. void
  67. set_params_marg()
  68. {
  69.     N = 5;
  70.     auto_step = auto_marg;
  71.     probe_func = probe_marg;
  72. }
  73.  
  74.  
  75. /*
  76.  * Look up margolus neighbors.  This is probably grossly inefficient,
  77.  * I haven't put any time into thinking about a clever way to do this
  78.  * yet.  If you have suggestions, let me know.
  79.  *  - Dave Hiebeler,  hiebeler@turing.cs.rpi.edu
  80.  */
  81. void
  82. Marg_get_nbors()
  83. {
  84.     if (phase%2) {
  85.     CM_u_move_1L(MargCenter, cell, L);
  86.     CM_u_move_constant_1L(MargPhase, phase, L);
  87.     CM_load_context(UL);
  88.     CM_get_from_news_1L(MargCcw, cell, North, L);
  89.     CM_get_from_news_1L(MargCw, cell, West, L);
  90.     CM_load_context(UR);
  91.     CM_get_from_news_1L(MargCcw, cell, East, L);
  92.     CM_get_from_news_1L(MargCw, cell, North, L);
  93.     CM_load_context(LR);
  94.     CM_get_from_news_1L(MargCcw, cell, South, L);
  95.     CM_get_from_news_1L(MargCw, cell, East, L);
  96.     CM_load_context(LL);
  97.     CM_get_from_news_1L(MargCcw, cell, West, L);
  98.     CM_get_from_news_1L(MargCw, cell, South, L);
  99.     CM_load_context(UL);
  100.     CM_get_from_news_1L(MargOpp, MargCcw, North, L);
  101.     CM_load_context(UR);
  102.     CM_get_from_news_1L(MargOpp, MargCcw, East, L);
  103.     CM_load_context(LR);
  104.     CM_get_from_news_1L(MargOpp, MargCcw, South, L);
  105.     CM_load_context(LL);
  106.     CM_get_from_news_1L(MargOpp, MargCcw, West, L);
  107.     CM_set_context();
  108.     }
  109.     else {
  110.     CM_u_move_1L(MargCenter, cell, L);
  111.     CM_u_move_constant_1L(MargPhase, phase, L);
  112.     CM_load_context(UL);
  113.     CM_get_from_news_1L(MargCcw, cell, South, L);
  114.     CM_get_from_news_1L(MargCw, cell, East, L);
  115.     CM_load_context(UR);
  116.     CM_get_from_news_1L(MargCcw, cell, West, L);
  117.     CM_get_from_news_1L(MargCw, cell, South, L);
  118.     CM_load_context(LR);
  119.     CM_get_from_news_1L(MargCcw, cell, North, L);
  120.     CM_get_from_news_1L(MargCw, cell, West, L);
  121.     CM_load_context(LL);
  122.     CM_get_from_news_1L(MargCcw, cell, East, L);
  123.     CM_get_from_news_1L(MargCw, cell, North, L);
  124.     CM_load_context(UL);
  125.     CM_get_from_news_1L(MargOpp, MargCcw, South, L);
  126.     CM_load_context(UR);
  127.     CM_get_from_news_1L(MargOpp, MargCcw, West, L);
  128.     CM_load_context(LR);
  129.     CM_get_from_news_1L(MargOpp, MargCcw, North, L);
  130.     CM_load_context(LL);
  131.     CM_get_from_news_1L(MargOpp, MargCcw, East, L);
  132.     CM_set_context();
  133.     }
  134. }
  135.  
  136.  
  137. /*
  138.  *run for "steps" time-steps; if steps is -1, then keep running until
  139.  * the client says to stop
  140.  */
  141. auto_marg(steps)
  142. int steps;
  143. {
  144.     int i, nsteps;
  145.     unsigned char buf[4];
  146.  
  147.     if (steps == -1) {
  148.     nsteps = 0;
  149.     while (1) {
  150.         /* do a non-blocking read to see if client said to stop */
  151.         if (read_from_sock(ns, buf, 1, ON, debug2) == 1)
  152.         return nsteps;
  153.         usleep(CM_delay);
  154.         CM_set_context();
  155.         Marg_get_nbors();
  156.         if (function) {
  157.         update_function();
  158.         }
  159.         else {
  160.         CM_aref32_shared_2L(temp_value,trans,
  161.                     nbor_values+2,32,(L*N)-2,max_index);
  162.         /* Ok, temp_value now has 4 8-bit numbers in it.
  163.          * One of them is the L-bit # we want.  We now get it.
  164.          */
  165.         CM_array_ref(cell,temp_value,nbor_values,L,2,4,8);
  166.         }
  167.         if (!(nsteps%CM_disp_interval)) {
  168.         if (FB_attach_ret_val && use_FB)
  169.             CMFB_display_image();
  170.         }
  171.         phase = (phase+1)%S;
  172.         nsteps++;
  173.     }
  174.     }
  175.     else {    
  176.     for (i=0; i<steps; i++) {
  177.         usleep(CM_delay);
  178.         CM_set_context();
  179.         Marg_get_nbors();
  180.         if (function) {
  181.         update_function();
  182.         }
  183.         else {
  184.         CM_aref32_shared_2L(temp_value,trans,
  185.                     nbor_values+2,32,(L*N)-2,max_index);
  186.         /* Ok, temp_value now has 4 8-bit numbers in it.
  187.          * One of them is the L-bit # we want.  We now get it.
  188.          */
  189.         CM_array_ref(cell,temp_value,nbor_values,L,2,4,8);
  190.         }
  191.         if (!(i%CM_disp_interval)) {
  192.         if (FB_attach_ret_val && use_FB)
  193.             CMFB_display_image();
  194.         }
  195.         phase = (phase+1)%S;
  196.         if (SOCKET_INUSE)
  197.         count_states();
  198.     }
  199.     }
  200.     CM_set_context();
  201. }
  202.  
  203.  
  204. /*
  205.  * Look up a transition in the LT on the CM
  206.  */
  207. void
  208. probe_marg(buf)
  209. unsigned char buf[];
  210. {
  211.     unsigned int j;
  212.  
  213.     j = buf[3] + buf[2]*256 + buf[1]*256*256 + buf[0]*256*256*256;
  214.     CM_u_move_constant_1L(nbor_values, j, L*N);
  215.     CM_aref32_shared_2L(temp_value, trans,
  216.             nbor_values+2, 32, (L*N)-2, max_index);
  217.     CM_array_ref(table_index, temp_value, nbor_values, L, 2, 4, 8);
  218.     buf[0] = CM_u_read_from_processor_1L(0, table_index, 8);
  219. }
  220.