[Avida-SVN] r3465 - development/source/cpu

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Mon Oct 12 07:55:45 PDT 2009


Author: brysonda
Date: 2009-10-12 10:55:44 -0400 (Mon, 12 Oct 2009)
New Revision: 3465

Modified:
   development/source/cpu/cHardwareExperimental.cc
   development/source/cpu/cHardwareExperimental.h
Log:
Add state grid instructions to cHardwareExperimental.

Modified: development/source/cpu/cHardwareExperimental.cc
===================================================================
--- development/source/cpu/cHardwareExperimental.cc	2009-10-10 18:41:38 UTC (rev 3464)
+++ development/source/cpu/cHardwareExperimental.cc	2009-10-12 14:55:44 UTC (rev 3465)
@@ -39,6 +39,7 @@
 #include "nMutation.h"
 #include "cOrganism.h"
 #include "cPhenotype.h"
+#include "cStateGrid.h"
 #include "cStringUtil.h"
 #include "cTestCPU.h"
 #include "cWorldDriver.h"
@@ -191,7 +192,14 @@
     tInstLibEntry<tMethod>("execurate", &cHardwareExperimental::Inst_Execurate),
     tInstLibEntry<tMethod>("execurate-24", &cHardwareExperimental::Inst_Execurate24),
 
-  
+    
+    // State Grid instructions
+    tInstLibEntry<tMethod>("sg-move", &cHardwareExperimental::Inst_SGMove),
+    tInstLibEntry<tMethod>("sg-rotate-l", &cHardwareExperimental::Inst_SGRotateL),
+    tInstLibEntry<tMethod>("sg-rotate-r", &cHardwareExperimental::Inst_SGRotateR),
+    tInstLibEntry<tMethod>("sg-sense", &cHardwareExperimental::Inst_SGSense),
+
+    
     // DEPRECATED Instructions
     tInstLibEntry<tMethod>("set-flow", &cHardwareExperimental::Inst_SetFlow, 0, "Set flow-head to position in ?CX?")
   };
@@ -2072,3 +2080,88 @@
 }
 
 
+bool cHardwareExperimental::Inst_SGMove(cAvidaContext& ctx)
+{
+  assert(m_ext_mem.GetSize() > 3);
+  
+  const cStateGrid& sg = m_organism->GetStateGrid();
+  
+  int& x = m_ext_mem[0];
+  int& y = m_ext_mem[1];
+  
+  const int facing = m_ext_mem[2];
+  
+  // State grid is treated as a 2-dimensional toroidal grid with size [0, width) and [0, height)
+  switch (facing) {
+    case 0: // N
+      if (++y == sg.GetHeight()) y = 0;
+      break;
+      
+    case 1: // NE
+      if (++x == sg.GetWidth()) x = 0;
+      if (++y == sg.GetHeight()) y = 0;
+      break;
+      
+    case 2: // E
+      if (++x == sg.GetWidth()) x = 0;
+      break;
+      
+    case 3: // SE
+      if (++x == sg.GetWidth()) x = 0;
+      if (--y == -1) y = sg.GetHeight() - 1;
+      break;
+      
+    case 4: // S
+      if (--y == -1) y = sg.GetHeight() - 1;
+      break;
+      
+    case 5: // SW
+      if (--x == -1) x = sg.GetWidth() - 1;
+      if (--y == -1) y = sg.GetHeight() - 1;
+      break;
+      
+    case 6: // W
+      if (--x == -1) x = sg.GetWidth() - 1;
+      break;
+      
+    case 7: // NW
+      if (--x == -1) x = sg.GetWidth() - 1;
+      if (++y == sg.GetHeight()) y = 0;
+      break;
+      
+    default:
+      assert(facing >= 0 && facing <= 7);
+  }
+  
+  // Increment state observed count
+  m_ext_mem[3 + sg.GetStateAt(x, y)]++;
+  
+  // Save this location in the movement history
+  m_ext_mem.Push(sg.GetIDFor(x, y));
+  return true;
+}
+
+bool cHardwareExperimental::Inst_SGRotateL(cAvidaContext& ctx)
+{
+  assert(m_ext_mem.GetSize() > 3);
+  if (--m_ext_mem[2] < 0) m_ext_mem[2] = 7;
+  return true;
+}
+
+bool cHardwareExperimental::Inst_SGRotateR(cAvidaContext& ctx)
+{
+  assert(m_ext_mem.GetSize() > 3);
+  if (++m_ext_mem[2] > 7) m_ext_mem[2] = 0;
+  return true;
+}
+
+bool cHardwareExperimental::Inst_SGSense(cAvidaContext& ctx)
+{
+  const cStateGrid& sg = m_organism->GetStateGrid();
+  const int reg_used = FindModifiedRegister(REG_BX);
+  setInternalValue(m_threads[m_cur_thread].reg[reg_used], sg.SenseStateAt(m_ext_mem[0], m_ext_mem[1]));
+  return true; 
+}
+
+
+

Modified: development/source/cpu/cHardwareExperimental.h
===================================================================
--- development/source/cpu/cHardwareExperimental.h	2009-10-10 18:41:38 UTC (rev 3464)
+++ development/source/cpu/cHardwareExperimental.h	2009-10-12 14:55:44 UTC (rev 3465)
@@ -474,6 +474,12 @@
   
   // Replication
   bool Inst_Repro(cAvidaContext& ctx);
+  
+  // State Grid Navigation
+  bool Inst_SGMove(cAvidaContext& ctx);
+  bool Inst_SGRotateL(cAvidaContext& ctx);
+  bool Inst_SGRotateR(cAvidaContext& ctx);
+  bool Inst_SGSense(cAvidaContext& ctx);  
 };
 
 




More information about the Avida-cvs mailing list