[Avida-SVN] r1060 - in development/source: cpu main

barrick at myxo.css.msu.edu barrick at myxo.css.msu.edu
Tue Oct 24 09:45:51 PDT 2006


Author: barrick
Date: 2006-10-24 12:45:50 -0400 (Tue, 24 Oct 2006)
New Revision: 1060

Modified:
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/main/cPhenotype.h
Log:
* Added instruction: put-cost  

  Right now it arbitrarily halves an organism's current bonus.

* Added instructions: throw, throwif=0, throwif!=0, catch 

  Each throw variant looks at register BX and takes a label.  
  The IP moves to the first catch instruction with the 
  complementary label in the genome after it, and wrapping 
  around.  If no suitable catch is found, execution
  continues from this point.
 
  Preliminary tests show incredibly complex (and useful) 
  execution flows are generated with these instructions!



Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-10-23 13:05:21 UTC (rev 1059)
+++ development/source/cpu/cHardwareCPU.cc	2006-10-24 16:45:50 UTC (rev 1060)
@@ -117,6 +117,11 @@
     cInstEntryCPU("jump-b",    &cHardwareCPU::Inst_JumpB),
     cInstEntryCPU("call",      &cHardwareCPU::Inst_Call),
     cInstEntryCPU("return",    &cHardwareCPU::Inst_Return),
+
+    cInstEntryCPU("throw",     &cHardwareCPU::Inst_Throw),
+    cInstEntryCPU("throwif=0", &cHardwareCPU::Inst_ThrowIf0),    
+    cInstEntryCPU("throwif!=0",&cHardwareCPU::Inst_ThrowIfNot0),
+    cInstEntryCPU("catch",     &cHardwareCPU::Inst_Catch),
     
     cInstEntryCPU("pop",       &cHardwareCPU::Inst_Pop, true,
                   "Remove top number from stack and place into ?BX?"),
@@ -207,6 +212,7 @@
     cInstEntryCPU("stk-get",   &cHardwareCPU::Inst_TaskStackGet),
     cInstEntryCPU("stk-load",  &cHardwareCPU::Inst_TaskStackLoad),
     cInstEntryCPU("put",       &cHardwareCPU::Inst_TaskPut),
+    cInstEntryCPU("put-cost", &cHardwareCPU::Inst_TaskPutCost),
     cInstEntryCPU("IO",        &cHardwareCPU::Inst_TaskIO, true,
                   "Output ?BX?, and input new number back into ?BX?"),
     cInstEntryCPU("IO-Feedback",        &cHardwareCPU::Inst_TaskIO_Feedback, true,\
@@ -1731,6 +1737,65 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_Throw(cAvidaContext& ctx)
+{
+  // Only initialize this once to save some time...
+  static cInstruction catch_inst = GetInstLib()->GetInst(cStringUtil::Stringf("catch"));
+
+  //What label complement are we looking for?
+  //If the size is zero then we just find the first example of a catch
+  ReadLabel();
+  GetLabel().Rotate(1, NUM_NOPS);
+  
+  cHeadCPU search_head(IP());
+  int start_pos = search_head.GetPosition();
+  search_head++;
+  
+  while (start_pos != search_head.GetPosition()) {
+    
+    // If we find a catch instruction, compare the NOPs following it
+    if (search_head.GetInst() == catch_inst)
+    {
+      int catch_pos = search_head.GetPosition();
+      search_head++;
+      int size_matched = 0;
+      while ( GetLabel().GetSize() > size_matched )
+      {
+        if ( !m_inst_set->IsNop( search_head.GetInst()) ) break;
+        if ( GetLabel()[size_matched] != m_inst_set->GetNopMod( search_head.GetInst()) ) break;
+        search_head++;
+        size_matched++;
+      }
+      
+      // We found a matching catch instruction
+      // set the catch to execute 
+      if (GetLabel().GetSize() == size_matched)
+      {
+        IP().Set(catch_pos);
+        m_advance_ip = false; // Don't automatically move the IP
+                              // so we mark the catch as executed.
+        return true;
+      }
+    }
+    search_head.Advance();
+  }
+
+  return false;
+}
+
+
+bool cHardwareCPU::Inst_ThrowIfNot0(cAvidaContext& ctx)
+{
+  if (GetRegister(REG_BX) == 0) return false;
+  return Inst_Throw(ctx);
+}
+
+bool cHardwareCPU::Inst_ThrowIf0(cAvidaContext& ctx)
+{
+  if (GetRegister(REG_BX) != 0) return false;
+  return Inst_Throw(ctx);
+}
+
 bool cHardwareCPU::Inst_Pop(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);
@@ -2634,6 +2699,26 @@
   return true;
 }
 
+bool cHardwareCPU::Inst_TaskPutCost(cAvidaContext& ctx)
+{
+  const int reg_used = FindModifiedRegister(REG_BX);
+  const int value = GetRegister(reg_used);
+  GetRegister(reg_used) = 0;
+/*
+  double before_bonus = organism->GetPhenotype().GetCurBonus();
+  organism->DoOutput(ctx, value);
+
+  if (organism->GetPhenotype().GetCurBonus() == before_bonus)
+  {
+    organism->GetPhenotype().SetCurBonus(before_bonus * 0.5); 
+  }
+*/  
+
+  organism->DoOutput(ctx, value);
+  organism->GetPhenotype().SetCurBonus(organism->GetPhenotype().GetCurBonus() * 0.5);
+  return true;
+}
+
 bool cHardwareCPU::Inst_TaskIO(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(REG_BX);
@@ -2741,7 +2826,7 @@
   const tArray<double> & res_count = organism->GetOrgInterface().GetResources();
 
   // Arbitrarily set to BX since the conditionals use this directly.
-  int reg_to_set = cHardwareCPU::REG_BX;
+  int reg_to_set = REG_BX;
 
   // There are no resources, return
   if (res_count.GetSize() == 0) return false;

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2006-10-23 13:05:21 UTC (rev 1059)
+++ development/source/cpu/cHardwareCPU.h	2006-10-24 16:45:50 UTC (rev 1060)
@@ -288,7 +288,12 @@
   bool Inst_JumpB(cAvidaContext& ctx);
   bool Inst_Call(cAvidaContext& ctx);
   bool Inst_Return(cAvidaContext& ctx);
-
+  
+  bool Inst_Throw(cAvidaContext& ctx);
+  bool Inst_ThrowIf0(cAvidaContext& ctx);
+  bool Inst_ThrowIfNot0(cAvidaContext& ctx);  
+  bool Inst_Catch(cAvidaContext& ctx) { return true; };
+  
   // Stack and Register Operations
   bool Inst_Pop(cAvidaContext& ctx);
   bool Inst_Push(cAvidaContext& ctx);
@@ -378,6 +383,7 @@
   bool Inst_TaskStackGet(cAvidaContext& ctx);
   bool Inst_TaskStackLoad(cAvidaContext& ctx);
   bool Inst_TaskPut(cAvidaContext& ctx);
+  bool Inst_TaskPutCost(cAvidaContext& ctx);  
   bool Inst_TaskIO(cAvidaContext& ctx);
   bool Inst_TaskIO_Feedback(cAvidaContext& ctx);
   bool Inst_MatchStrings(cAvidaContext& ctx);

Modified: development/source/main/cPhenotype.h
===================================================================
--- development/source/main/cPhenotype.h	2006-10-23 13:05:21 UTC (rev 1059)
+++ development/source/main/cPhenotype.h	2006-10-24 16:45:50 UTC (rev 1060)
@@ -263,6 +263,7 @@
   void SetIsDonorCur() { is_donor_cur = true; } 
   void SetIsReceiver() { is_receiver = true; } 
   
+  void SetCurBonus(double _bonus) { cur_bonus = _bonus; }
 
   void IncCurInstCount(int _inst_num)  { assert(initialized == true); cur_inst_count[_inst_num]++; } 
   void DecCurInstCount(int _inst_num)  { assert(initialized == true); cur_inst_count[_inst_num]--; } 




More information about the Avida-cvs mailing list