[Avida-cvs] [avida-svn] r521 - in development: Avida.xcodeproj source/analyze source/classification source/cpu source/drivers source/event source/main source/tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Tue Mar 14 20:46:30 PST 2006


Author: brysonda
Date: 2006-03-14 23:45:51 -0500 (Tue, 14 Mar 2006)
New Revision: 521

Added:
   development/source/main/cAvidaContext.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/analyze/cAnalyze.cc
   development/source/analyze/cAnalyze.h
   development/source/analyze/cAnalyzeGenotype.cc
   development/source/analyze/cAnalyzeGenotype.h
   development/source/analyze/cAnalyzeUtil.cc
   development/source/analyze/tAnalyzeJob.h
   development/source/classification/cGenotype.cc
   development/source/classification/cGenotype.h
   development/source/classification/cSpecies.cc
   development/source/cpu/cHardware4Stack.cc
   development/source/cpu/cHardware4Stack.h
   development/source/cpu/cHardwareBase.cc
   development/source/cpu/cHardwareBase.h
   development/source/cpu/cHardwareCPU.cc
   development/source/cpu/cHardwareCPU.h
   development/source/cpu/cHardwareSMT.cc
   development/source/cpu/cHardwareSMT.h
   development/source/cpu/cTestCPU.cc
   development/source/cpu/cTestCPU.h
   development/source/cpu/cTestUtil.cc
   development/source/drivers/cDefaultRunDriver.cc
   development/source/event/cEventManager.cc
   development/source/main/cFitnessMatrix.cc
   development/source/main/cInstSet.cc
   development/source/main/cInstSet.h
   development/source/main/cInstUtil.cc
   development/source/main/cInstUtil.h
   development/source/main/cLandscape.cc
   development/source/main/cLandscape.h
   development/source/main/cMxCodeArray.cc
   development/source/main/cMxCodeArray.h
   development/source/main/cOrganism.cc
   development/source/main/cOrganism.h
   development/source/main/cPopulation.cc
   development/source/main/cPopulation.h
   development/source/tools/cProbSchedule.cc
   development/source/tools/cProbSchedule.h
Log:
Begin passing around cAvidaContext references among various execution functions.  This process is by no means complete.  There are numerous out-of-band instantiations of cAvidaContext.  Also, nothing is actually using the context yet.  Initial motivation/usage is to pass a cRandom reference/identfier to facilitate higher performance in multithreaded analyze operation.

Instances where lazy evaluation is employed on certain values (CalcKnockouts, for example) deserve some thought.  It doesn't seem to make sense to require a context to read the value, yet one may required to perform the lazy evaluation.



Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2006-03-14 07:41:46 UTC (rev 520)
+++ development/Avida.xcodeproj/project.pbxproj	2006-03-15 04:45:51 UTC (rev 521)
@@ -36,6 +36,9 @@
 		7013846409028B3E0087ED2E /* cAvidaConfig.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7013846009028B3E0087ED2E /* cAvidaConfig.cc */; };
 		7013852D0902E36B0087ED2E /* avida.cfg in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7013852C0902E36B0087ED2E /* avida.cfg */; };
 		7013852E0902E36B0087ED2E /* avida.cfg in CopyFiles */ = {isa = PBXBuildFile; fileRef = 7013852C0902E36B0087ED2E /* avida.cfg */; };
+		701D51CD09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 701D51CB09C645F50009B4F8 /* cAvidaContext.h */; };
+		701D51CF09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 701D51CB09C645F50009B4F8 /* cAvidaContext.h */; };
+		701D51D109C645F50009B4F8 /* cAvidaContext.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 701D51CB09C645F50009B4F8 /* cAvidaContext.h */; };
 		701D915D094B864A008B845F /* cFallbackWorldDriver.cc in Sources */ = {isa = PBXBuildFile; fileRef = 701D915C094B864A008B845F /* cFallbackWorldDriver.cc */; };
 		701D915E094B864A008B845F /* cFallbackWorldDriver.cc in Sources */ = {isa = PBXBuildFile; fileRef = 701D915C094B864A008B845F /* cFallbackWorldDriver.cc */; };
 		701D92E4094C9E6F008B845F /* cDriverManager.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 701D92E0094C9E6F008B845F /* cDriverManager.h */; };
@@ -471,6 +474,7 @@
 				7054A1B709A810CB00038658 /* cAnalyzeJobWorker.h in CopyFiles */,
 				7005A70509BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
 				7005A70B09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */,
+				701D51D109C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -496,6 +500,7 @@
 				7013852D0902E36B0087ED2E /* avida.cfg in CopyFiles */,
 				7054A12409A7BEFC00038658 /* cThread.h in CopyFiles */,
 				7005A70709BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
+				701D51CD09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -525,6 +530,7 @@
 				7054A1B509A810CB00038658 /* cAnalyzeJobWorker.h in CopyFiles */,
 				7005A70309BA0FA90007E16E /* cTestCPUInterface.h in CopyFiles */,
 				7005A70A09BA0FBE0007E16E /* cOrgInterface.h in CopyFiles */,
+				701D51CF09C645F50009B4F8 /* cAvidaContext.h in CopyFiles */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -541,6 +547,7 @@
 		7013846009028B3E0087ED2E /* cAvidaConfig.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cAvidaConfig.cc; sourceTree = "<group>"; };
 		701384A10902A16F0087ED2E /* defs.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = defs.h; sourceTree = "<group>"; };
 		7013852C0902E36B0087ED2E /* avida.cfg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = avida.cfg; sourceTree = "<group>"; };
+		701D51CB09C645F50009B4F8 /* cAvidaContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cAvidaContext.h; sourceTree = "<group>"; };
 		701D9116094B773E008B845F /* cWorldDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cWorldDriver.h; sourceTree = "<group>"; };
 		701D912B094B7AC1008B845F /* cAvidaDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cAvidaDriver.h; sourceTree = "<group>"; };
 		701D9151094B8483008B845F /* cFallbackWorldDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cFallbackWorldDriver.h; sourceTree = "<group>"; };
@@ -1362,6 +1369,7 @@
 				70B0875C08F5ECBC00FC65FE /* nReaction.h */,
 				DCC310A90762539E008F7A48 /* CMakeLists.txt */,
 				7005A70909BA0FBE0007E16E /* cOrgInterface.h */,
+				701D51CB09C645F50009B4F8 /* cAvidaContext.h */,
 			);
 			path = main;
 			sourceTree = "<group>";

Modified: development/source/analyze/cAnalyze.cc
===================================================================
--- development/source/analyze/cAnalyze.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/cAnalyze.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -25,6 +25,7 @@
 #include "cAnalyzeGenotype.h"
 #include "cAnalyzeJobQueue.h"
 #include "tAnalyzeJob.h"
+#include "cAvidaContext.h"
 #include "cDataFile.h"
 #include "cEnvironment.h"
 #include "cFitnessMatrix.h"
@@ -65,6 +66,7 @@
 : cur_batch(0)
 , m_world(world)
 , inst_set(world->GetHardwareManager().GetInstSet())
+, m_ctx(0)
 , verbose(nAnalyze::VERBOSE_QUIET)
 , interactive_depth(0)
 {
@@ -482,7 +484,7 @@
   double entropy = 0.0;
   
   // If the fitness is 0, the entropy is the length of genotype ...
-  genotype->Recalculate(m_testcpu);
+  genotype->Recalculate(m_ctx, m_testcpu);
   if (genotype->GetFitness() == 0) {
     return genotype->GetLength();
   }
@@ -504,7 +506,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -562,7 +564,7 @@
   
   double entropy = 0.0;
 
-  genotype->Recalculate(m_testcpu);
+  genotype->Recalculate(m_ctx, m_testcpu);
 
    // Calculate the stats for the genotype we're working with ...
   const int num_insts = inst_set.GetSize();
@@ -605,7 +607,7 @@
 	  mod_genome[line_1].SetOp(mod_inst_1);
 	  mod_genome[line_2].SetOp(mod_inst_2);
 	  cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-	  test_genotype.Recalculate(m_testcpu);
+	  test_genotype.Recalculate(m_ctx, m_testcpu);
 	  // Adjust fitness ...
 	  if (test_genotype.GetFitness() <= base_fitness) {
 	    test_fitness[mod_inst_1][mod_inst_2] = test_genotype.GetFitness();
@@ -684,7 +686,7 @@
   double entropy = 0.0;
   
   // Calculate the stats for the genotype we're working with ...
-  genotype->Recalculate(m_testcpu);
+  genotype->Recalculate(m_ctx, m_testcpu);
   const int num_insts = inst_set.GetSize();
   const int num_lines = genotype->GetLength();
   const cGenome & base_genome = genotype->GetGenome();
@@ -702,7 +704,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       test_fitness[mod_inst] = test_genotype.GetFitness();
     }
     
@@ -777,7 +779,7 @@
     exit(1);
   }
   
-  genotype1->Recalculate(m_testcpu);
+  genotype1->Recalculate(m_ctx, m_testcpu);
   if (genotype1->GetFitness() == 0) {
     return 0.0;
   }
@@ -799,7 +801,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       genotype1_mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, genotype1_mod_genome, inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= genotype1_base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -848,7 +850,7 @@
     genotype1_mod_genome[line_no].SetOp(cur_inst);
   }
   
-  genotype2->Recalculate(m_testcpu);
+  genotype2->Recalculate(m_ctx, m_testcpu);
   if (genotype2->GetFitness() == 0) {
     for (int line_no = 0; line_no < num_lines; ++ line_no) {
       increased_info += genotype1_info[line_no];
@@ -868,7 +870,7 @@
     for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
       genotype2_mod_genome[line_no].SetOp(mod_inst);
       cAnalyzeGenotype test_genotype(m_world, genotype2_mod_genome, inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       // Ajust fitness ...
       if (test_genotype.GetFitness() <= genotype2_base_fitness) {
         test_fitness[mod_inst] = test_genotype.GetFitness();
@@ -1625,7 +1627,7 @@
     test_info.TestThreads();
     test_info.SetTraceExecution(&trace_printer);
     
-    testcpu->TestGenome(test_info, genotype->GetGenome());
+    testcpu->TestGenome(m_ctx, test_info, genotype->GetGenome());
     
     if (verbose >= nAnalyze::VERBOSE_ON) cout << "  Tracing: " << filename << endl;
     m_world->GetDataFileManager().Remove(filename);
@@ -2749,7 +2751,7 @@
     genotype = sorted_community[i];
     
     // Skip the dead organisms
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     if (genotype->GetFitness() == 0) {
       continue;
     }
@@ -2853,7 +2855,7 @@
         mod_genome = base_genome;
         mod_genome[line].SetOp(given_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(testcpu);
+        test_genotype.Recalculate(m_ctx, testcpu);
         
         // Only when given inst make the genotype alive
         if (test_genotype.GetFitness() > 0) {
@@ -2876,7 +2878,7 @@
     vector< vector<bool> > neutral_mut(length_genome, one_line_neutral);
     vector< vector<bool> > alive_mut(length_genome, one_line_neutral);
     
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     double base_fitness = genotype->GetFitness();
     cout << base_fitness << endl;
     const cGenome & base_genome = genotype->GetGenome();
@@ -2888,7 +2890,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; ++ mod_inst) {
         mod_genome[line].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(testcpu);
+        test_genotype.Recalculate(m_ctx, testcpu);
         if (test_genotype.GetFitness() >= base_fitness) {
           neutral_mut[line][mod_inst] = true;
         } 
@@ -3116,7 +3118,7 @@
       m_world->GetDataFileManager().Remove(filename);
       return;
     }
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     int num_tasks = genotype->GetNumTasks();
     vector< vector<cAnalyzeGenotype *> > genotype_class(num_tasks);
     do {
@@ -3174,7 +3176,7 @@
     cout << "Test point mutation for genotype " << genotype->GetID() << endl;
     tMatrix<double> prob(length_genome, num_insts);
     
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     double base_fitness = genotype->GetFitness();
     const cGenome & base_genome = genotype->GetGenome();
     cGenome mod_genome(base_genome);
@@ -3186,7 +3188,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; ++ mod_inst) {
         mod_genome[line].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(testcpu);
+        test_genotype.Recalculate(m_ctx, testcpu);
         if (test_genotype.GetFitness() >= base_fitness) {
           prob[line][mod_inst] = 1.0;
           num_neutral ++;
@@ -3245,7 +3247,7 @@
   int num_cpus = genotype->GetNumCPUs();
   total_cpus += num_cpus;
   cpx_fp << num_cpus << " " << total_cpus << "   ";
-  genotype->Recalculate(testcpu);
+  genotype->Recalculate(m_ctx, testcpu);
   genotype->PrintTasks(cpx_fp, 0, -1);
   cpx_fp << endl;
   
@@ -3260,7 +3262,7 @@
     }
     
     // Skip the dead organisms
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     cout << genotype->GetID() << " " << genotype->GetFitness() << endl;
     if (genotype->GetFitness() == 0) {
       continue;
@@ -3399,8 +3401,8 @@
   cAnalyzeGenotype * genotype = NULL;
   while ((genotype = batch_it.Next()) != NULL) {
     cLandscape landscape(m_world, genotype->GetGenome(), inst_set);
-    if (num_test == 0) landscape.Process(dist);
-    else landscape.RandomProcess(num_test,dist,num_test,num_test*2);
+    if (num_test == 0) landscape.Process(m_ctx, dist);
+    else landscape.RandomProcess(m_ctx, num_test,dist,num_test,num_test*2);
     landscape.PrintStats(fp);
   }
 }
@@ -3506,14 +3508,14 @@
   
   // If we're given a file, write to it.
   ofstream& fp = m_world->GetDataFileOFStream(filename);
-  
+
   // Loop through all of the genotypes in this batch...
   tListIterator<cAnalyzeGenotype> batch_it(batch[cur_batch].List());
   cAnalyzeGenotype * genotype = NULL;
   while ((genotype = batch_it.Next()) != NULL) {
     cLandscape landscape(m_world, genotype->GetGenome(), inst_set);
-    if (test_num == 1)  landscape.TestAllPairs(fp);
-    else 		landscape.TestPairs(test_num,fp); 
+    if (test_num == 1)  landscape.TestAllPairs(m_ctx, fp);
+    else 		landscape.TestPairs(m_ctx, test_num,fp); 
     landscape.PrintStats(fp);
   }
 }
@@ -3649,7 +3651,7 @@
     cCPUTestInfo test_info;
     
     // Run each side, and determine viability...
-    testcpu->TestGenome(test_info, test_genome0);
+    testcpu->TestGenome(m_ctx, test_info, test_genome0);
     if( test_info.IsViable() == false ) {
       fail_count++;
       if (same_mate_id == true) match_fail_count++;
@@ -3796,7 +3798,7 @@
       if (copy_mut_prob > 0.0) {
         for (int i = 0; i < mod_genome.GetSize(); i++) {
           if (m_world->GetRandom().P(copy_mut_prob)) {
-            mod_genome[i] = inst_set.GetRandomInst();
+            mod_genome[i] = inst_set.GetRandomInst(m_ctx);
             num_mutations++;
           }
         }
@@ -3805,7 +3807,7 @@
       // Perform an Insertion if it has one.
       if (m_world->GetRandom().P(ins_mut_prob)) {
         ins_line = m_world->GetRandom().GetInt(mod_genome.GetSize() + 1);
-        mod_genome.Insert(ins_line, inst_set.GetRandomInst());
+        mod_genome.Insert(ins_line, inst_set.GetRandomInst(m_ctx));
         num_mutations++;
       }
       
@@ -3818,7 +3820,7 @@
     }
     
     // Collect basic state before and after the mutations...
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
     double start_complexity = genotype->GetKO_Complexity();
     double start_fitness = genotype->GetFitness();
     int start_length = genotype->GetLength();
@@ -3827,7 +3829,7 @@
     const tArray< tArray<int> >& start_KO_task_counts = genotype->GetKO_TaskCounts();
     
     cAnalyzeGenotype new_genotype(m_world, mod_genome, inst_set);
-    new_genotype.Recalculate(m_testcpu);
+    new_genotype.Recalculate(m_ctx, m_testcpu);
     double end_complexity = new_genotype.GetKO_Complexity();
     double end_fitness = new_genotype.GetFitness();
     int end_length = new_genotype.GetLength();
@@ -4021,7 +4023,7 @@
     if (verbose >= nAnalyze::VERBOSE_ON) cout << "  Knockout: " << genotype->GetName() << endl;
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
     const double base_fitness = genotype->GetFitness();
     
     const int max_line = genotype->GetLength();
@@ -4040,7 +4042,7 @@
       int cur_inst = base_genome[line_num].GetOp();
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-      ko_genotype.Recalculate(m_testcpu);
+      ko_genotype.Recalculate(m_ctx, m_testcpu);
       
       double ko_fitness = ko_genotype.GetFitness();
       if (ko_fitness == 0.0) {
@@ -4072,7 +4074,7 @@
           mod_genome[line1] = null_inst;
           mod_genome[line2] = null_inst;
           cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-          ko_genotype.Recalculate(m_testcpu);
+          ko_genotype.Recalculate(m_ctx, m_testcpu);
           
           double ko_fitness = ko_genotype.GetFitness();
           
@@ -4272,7 +4274,7 @@
     }
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
     
     // Headers...
     if (file_type == FILE_TYPE_TEXT) {
@@ -4391,7 +4393,7 @@
       
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       
       if (file_type == FILE_TYPE_HTML) fp << "<tr><td align=right>";
       fp << (line_num + 1) << " ";
@@ -4577,7 +4579,7 @@
     if (verbose >= nAnalyze::VERBOSE_ON) cout << "  Mapping " << genotype->GetName() << endl;
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
     
     // Check if the organism does any tasks. 
     int does_tasks = 0;
@@ -4648,7 +4650,7 @@
           mod_genome[line_num] = null_inst;
           cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
           cAnalyzeGenotype old_genotype(m_world, base_genome, map_inst_set);
-          test_genotype.Recalculate(m_testcpu);
+          test_genotype.Recalculate(m_ctx, m_testcpu);
           
           // Print the individual columns...
           output_it.Reset();
@@ -4864,7 +4866,7 @@
     const int base_length = genotype->GetLength();
     const cGenome & base_genome = genotype->GetGenome();
     cGenome mod_genome(base_genome);
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
 
     tMatrix<bool> task_matrix(num_traits, base_length);
     tArray<int> num_inst(num_traits);  // Number of instructions for each task
@@ -4880,7 +4882,7 @@
       // Determine what happens to this genotype when this line is knocked out
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
 
       // Loop through the individual traits
       output_it.Reset();
@@ -5024,7 +5026,7 @@
     ofstream& fp = m_world->GetDataFileOFStream(filename);
     
     // Calculate the stats for the genotype we're working with...
-    genotype->Recalculate(m_testcpu);
+    genotype->Recalculate(m_ctx, m_testcpu);
     const double base_fitness = genotype->GetFitness();
     const int num_insts = inst_set.GetSize();
     
@@ -5137,7 +5139,7 @@
         else {
           mod_genome[line_num].SetOp(mod_inst);
           cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-          test_genotype.Recalculate(m_testcpu);
+          test_genotype.Recalculate(m_ctx, m_testcpu);
           const double test_fitness = test_genotype.GetFitness() / base_fitness;
           row_fitness += test_fitness;
           total_fitness += test_fitness;
@@ -5173,7 +5175,7 @@
       // Column: Knockout
       mod_genome[line_num] = null_inst;
       cAnalyzeGenotype test_genotype(m_world, mod_genome, map_inst_set);
-      test_genotype.Recalculate(m_testcpu);
+      test_genotype.Recalculate(m_ctx, m_testcpu);
       const double test_fitness = test_genotype.GetFitness() / base_fitness;
       col_fitness[num_insts] += test_fitness;
       
@@ -5512,10 +5514,10 @@
           
           // Run each side, and determine viability...
           cCPUTestInfo test_info;
-          testcpu->TestGenome(test_info, test_genome0);
+          testcpu->TestGenome(m_ctx, test_info, test_genome0);
           cross1_viable = test_info.IsViable();
           
-          testcpu->TestGenome(test_info, test_genome1);
+          testcpu->TestGenome(m_ctx, test_info, test_genome1);
           cross2_viable = test_info.IsViable();
           
           if (cross1_viable == false) fail_count++;   
@@ -6138,11 +6140,11 @@
       cGenome test_genome(test_sequence);
       cCPUTestInfo test_info;
       test_info.TestThreads();
-      testcpu->TestGenome(test_info, test_genome);
+      testcpu->TestGenome(m_ctx, test_info, test_genome);
       const double fitness = test_info.GetGenotypeFitness();
       
       //cAnalyzeGenotype test_genotype(test_sequence);
-      //test_genotype.Recalculate(testcpu);
+      //test_genotype.Recalculate(m_ctx, testcpu);
       //const double fitness = test_genotype.GetFitness();
       
       total_fitness[diff_count] += fitness;
@@ -6163,7 +6165,7 @@
     
     for (int i = 0; i <= total_diffs; i++) {
       cAnalyzeGenotype max_genotype(m_world, max_sequence[i], inst_set);
-      max_genotype.Recalculate(testcpu);
+      max_genotype.Recalculate(m_ctx, testcpu);
       fp << i                                         << " "  //  1
         << test_count[i]                             << " "  //  2
         << total_fitness[i] / (double) test_count[i] << " "  //  3
@@ -6479,14 +6481,14 @@
     
     cGenome test_genome = genotype->GetGenome();
     cCPUTestInfo test_info;
-    testcpu->TestGenome(test_info, test_genome);
+    testcpu->TestGenome(m_ctx, test_info, test_genome);
     const double base_fitness = test_info.GetGenotypeFitness();
     
     for (int i = 0; i < size; i++) {
       if (prev_inst[i] == -1) num_static++;
       else {
         test_genome[i].SetOp(prev_inst[i]);
-        testcpu->TestGenome(test_info, test_genome);
+        testcpu->TestGenome(m_ctx, test_info, test_genome);
         const double cur_fitness = test_info.GetGenotypeFitness();
         if (cur_fitness > base_fitness) num_detrimental++;
         else if (cur_fitness < base_fitness) num_beneficial++;
@@ -6586,7 +6588,7 @@
     }
     
     // Calculate the stats for the genotype we're working with ...
-    genotype->Recalculate(testcpu);
+    genotype->Recalculate(m_ctx, testcpu);
     cout << genotype->GetFitness() << endl;
     const int num_insts = inst_set.GetSize();
     const int max_line = genotype->GetLength();
@@ -6607,7 +6609,7 @@
       for (int mod_inst = 0; mod_inst < num_insts; mod_inst++) {
         mod_genome[line_num].SetOp(mod_inst);
         cAnalyzeGenotype test_genotype(m_world, mod_genome, inst_set);
-        test_genotype.Recalculate(testcpu);
+        test_genotype.Recalculate(m_ctx, testcpu);
         test_fitness[mod_inst] = test_genotype.GetFitness();
       }
       
@@ -6934,9 +6936,9 @@
     // If the previous genotype was the parent of this one, pass in a pointer
     // to it for improved recalculate (such as distance to parent, etc.)
     if (last_genotype != NULL && genotype->GetParentID() == last_genotype->GetID()) {
-      genotype->Recalculate(testcpu, last_genotype);
+      genotype->Recalculate(m_ctx, testcpu, last_genotype);
     } else {
-      genotype->Recalculate(testcpu);
+      genotype->Recalculate(m_ctx, testcpu);
     }
     last_genotype = genotype;
   }

Modified: development/source/analyze/cAnalyze.h
===================================================================
--- development/source/analyze/cAnalyze.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/cAnalyze.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -13,6 +13,9 @@
 
 #include <vector>
 
+#ifndef cAvidaContex_h
+#include "cAvidaContext.h"
+#endif
 #ifndef cGenotypeBatch_h
 #include "cGenotypeBatch.h"
 #endif
@@ -76,7 +79,9 @@
   cWorld* m_world;
   cInstSet& inst_set;
   cTestCPU* m_testcpu;
+  cAvidaContext m_ctx;
 
+
   // This is the storage for the resource information from resource.dat.  It 
   // is a pair of the update and a vector of the resource concentrations
   std::vector<std::pair<int, std::vector<double> > > resources;

Modified: development/source/analyze/cAnalyzeGenotype.cc
===================================================================
--- development/source/analyze/cAnalyzeGenotype.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/cAnalyzeGenotype.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cAnalyzeGenotype.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "cHardwareManager.h"
 #include "cInstSet.h"
@@ -55,7 +56,12 @@
   , ancestor_dist(0)
   , parent_muts("")
   , knockout_stats(NULL)
-  , landscape_stats(NULL)
+  , land_frac_dead(0.0)
+  , land_frac_neg(0.0)
+  , land_frac_neut(0.0)
+  , land_frac_pos(0.0)
+  , land_complexity(0.0)
+  , land_ave_fitness(0.0)
 {
   // Make sure that the sequences jive with the inst_set
   for (int i = 0; i < genome.GetSize(); i++) {
@@ -100,7 +106,12 @@
   , ancestor_dist(0)
   , parent_muts("")
   , knockout_stats(NULL)
-  , landscape_stats(NULL)
+  , land_frac_dead(0.0)
+  , land_frac_neg(0.0)
+  , land_frac_neut(0.0)
+  , land_frac_pos(0.0)
+  , land_complexity(0.0)
+  , land_ave_fitness(0.0)
 {
 }
 
@@ -135,22 +146,22 @@
   , ancestor_dist(_gen.ancestor_dist)
   , parent_muts(_gen.parent_muts)
   , knockout_stats(NULL)
-  , landscape_stats(NULL)
+  , land_frac_dead(_gen.land_frac_dead)
+  , land_frac_neg(_gen.land_frac_neg)
+  , land_frac_neut(_gen.land_frac_neut)
+  , land_frac_pos(_gen.land_frac_pos)
+  , land_complexity(_gen.land_complexity)
+  , land_ave_fitness(_gen.land_ave_fitness)
 {
   if (_gen.knockout_stats != NULL) {
     knockout_stats = new cAnalyzeKnockouts;
     *knockout_stats = *(_gen.knockout_stats);
   }
-  if (_gen.landscape_stats != NULL) {
-    landscape_stats = new cAnalyzeLandscape;
-    *landscape_stats = *(_gen.landscape_stats);
-  }
 }
 
 cAnalyzeGenotype::~cAnalyzeGenotype()
 {
   if (knockout_stats != NULL) delete knockout_stats;
-  if (landscape_stats != NULL) delete landscape_stats;
 }
 
 
@@ -179,12 +190,15 @@
   }
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
   
   // Calculate the base fitness for the genotype we're working with...
   // (This may not have been run already, and cost negligiably more time
   // considering the number of knockouts we need to do.
   cAnalyzeGenotype base_genotype(m_world, genome, inst_set);
-  base_genotype.Recalculate(testcpu);
+  base_genotype.Recalculate(ctx, testcpu);
   double base_fitness = base_genotype.GetFitness();
   const tArray<int> base_task_counts( base_genotype.GetTaskCounts() );
   
@@ -225,7 +239,7 @@
     int cur_inst = mod_genome[line_num].GetOp();
     mod_genome[line_num] = null_inst;
     cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-    ko_genotype.Recalculate(testcpu);
+    ko_genotype.Recalculate(ctx, testcpu);
     if (check_chart == true) {
       const tArray<int> ko_task_counts( ko_genotype.GetTaskCounts() );
       knockout_stats->task_counts[line_num] = ko_task_counts;
@@ -285,7 +299,7 @@
       mod_genome[line1] = null_inst;
       mod_genome[line2] = null_inst;
       cAnalyzeGenotype ko_genotype(m_world, mod_genome, ko_inst_set);
-      ko_genotype.Recalculate(testcpu);
+      ko_genotype.Recalculate(ctx, testcpu);
       
       double ko_fitness = ko_genotype.GetFitness();
       
@@ -324,22 +338,19 @@
   delete testcpu;
 }
 
-void cAnalyzeGenotype::CalcLandscape() const
+void cAnalyzeGenotype::CalcLandscape(cAvidaContext& ctx)
 {
-  if (landscape_stats != NULL) return;
-
-  landscape_stats = new cAnalyzeLandscape;
   cLandscape landscape(m_world, genome, inst_set);
-  landscape.Process(1);
-  landscape_stats->frac_dead = landscape.GetProbDead();
-  landscape_stats->frac_neg  = landscape.GetProbNeg();
-  landscape_stats->frac_neut = landscape.GetProbNeut();
-  landscape_stats->frac_pos  = landscape.GetProbPos();
-  landscape_stats->complexity = landscape.GetComplexity();
-  landscape_stats->ave_fitness = landscape.GetAveFitness();
+  landscape.Process(ctx, 1);
+  land_frac_dead = landscape.GetProbDead();
+  land_frac_neg = landscape.GetProbNeg();
+  land_frac_neut = landscape.GetProbNeut();
+  land_frac_pos = landscape.GetProbPos();
+  land_complexity = landscape.GetComplexity();
+  land_ave_fitness = landscape.GetAveFitness();
 }
 
-void cAnalyzeGenotype::Recalculate(cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype)
+void cAnalyzeGenotype::Recalculate(cAvidaContext& ctx, cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype)
 {
     // Build the test info for printing.
   cCPUTestInfo test_info;
@@ -354,7 +365,7 @@
   cInstSet env_inst_set_backup = m_world->GetHardwareManager().GetInstSet();
   m_world->GetHardwareManager().GetInstSet() = inst_set;
 
-  testcpu->TestGenome(test_info, genome);
+  testcpu->TestGenome(ctx, test_info, genome);
   
   // Restore the instruction set
   m_world->GetHardwareManager().GetInstSet() = env_inst_set_backup;
@@ -468,43 +479,6 @@
   return knockout_stats->task_counts;
 }
 
-
-double cAnalyzeGenotype::GetFracDead() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->frac_dead;
-}
-
-double cAnalyzeGenotype::GetFracNeg() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->frac_neg;
-}
-
-double cAnalyzeGenotype::GetFracNeut() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->frac_neut;
-}
-
-double cAnalyzeGenotype::GetFracPos() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->frac_pos;
-}
-
-double cAnalyzeGenotype::GetComplexity() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->complexity;
-}
-
-double cAnalyzeGenotype::GetLandscapeFitness() const
-{
-  CalcLandscape();  // Make sure the landscape is calculated...
-  return landscape_stats->ave_fitness;
-}
-
 cString cAnalyzeGenotype::GetTaskList() const
 {
   const int num_tasks = task_counts.GetSize();

Modified: development/source/analyze/cAnalyzeGenotype.h
===================================================================
--- development/source/analyze/cAnalyzeGenotype.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/cAnalyzeGenotype.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -34,6 +34,7 @@
 
 // cAnalyzeGenotype    : Collection of information about loaded genotypes
 
+class cAvidaContext;
 class cInstSet;
 class cTestCPU;
 class cWorld;
@@ -120,18 +121,12 @@
   };
   mutable cAnalyzeKnockouts * knockout_stats;
 
-  class cAnalyzeLandscape {
-  public:
-    double frac_dead;
-    double frac_neg;
-    double frac_neut;
-    double frac_pos;
-    double complexity;
-    double ave_fitness;
-    cAnalyzeLandscape() : frac_dead(0.0), frac_neg(0.0),
-			  frac_neut(0.0), frac_pos(0.0), complexity(0.0), ave_fitness(0.0) { ; }
-  };
-  mutable cAnalyzeLandscape * landscape_stats;
+  double land_frac_dead;
+  double land_frac_neg;
+  double land_frac_neut;
+  double land_frac_pos;
+  double land_complexity;
+  double land_ave_fitness;
 
   // Group 5 : More complex stats (obtained indvidually, through tests)
   cString task_order;
@@ -148,7 +143,7 @@
   }
 
   int CalcMaxGestation() const;
-  void CalcKnockouts(bool check_pairs=false, bool check_chart=false) const;
+  void CalcKnockouts(bool check_pairs = false, bool check_chart = false) const;
 public:
   cAnalyzeGenotype(cWorld* world, cString symbol_string, cInstSet & in_inst_set);
   cAnalyzeGenotype(cWorld* world, const cGenome & _genome, cInstSet & in_inst_set);
@@ -158,9 +153,9 @@
   const cStringList & GetSpecialArgs() { return special_args; }
   void SetSpecialArgs(const cStringList & _args) { special_args = _args; }
 
-  void Recalculate(cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype = NULL);
+  void Recalculate(cAvidaContext& ctx, cTestCPU* testcpu, cAnalyzeGenotype* parent_genotype = NULL);
   void PrintTasks(std::ofstream& fp, int min_task = 0, int max_task = -1);
-  void CalcLandscape() const;
+  void CalcLandscape(cAvidaContext& ctx);
 
   // Set...
   void SetSequence(cString _sequence);
@@ -248,12 +243,12 @@
   const tArray< tArray<int> > & GetKO_TaskCounts() const;
   
   // Landscape accessors
-  double GetFracDead() const;
-  double GetFracNeg() const;
-  double GetFracNeut() const;
-  double GetFracPos() const;
-  double GetComplexity() const;
-  double GetLandscapeFitness() const;
+  double GetFracDead() const  { return land_frac_dead; }
+  double GetFracNeg() const { return land_frac_neg; }
+  double GetFracNeut() const { return land_frac_neut; }
+  double GetFracPos() const { return land_frac_pos; }
+  double GetComplexity() const { return land_complexity; }
+  double GetLandscapeFitness() const { return land_ave_fitness; }
 
   double GetFitnessRatio() const { return fitness_ratio; }
   double GetEfficiencyRatio() const { return efficiency_ratio; }

Modified: development/source/analyze/cAnalyzeUtil.cc
===================================================================
--- development/source/analyze/cAnalyzeUtil.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/cAnalyzeUtil.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cAnalyzeUtil.h"
 
+#include "cAvidaContext.h"
 #include "defs.h"
 #include "cEnvironment.h"
 #include "cClassificationManager.h"
@@ -42,8 +43,12 @@
                               ofstream& fp, int update)
 {
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+  
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+  
   cCPUTestInfo test_info;
-  testcpu->TestGenome(test_info, genome);
+  testcpu->TestGenome(ctx, test_info, genome);
   delete testcpu;
   
   cPhenotype &colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
@@ -64,6 +69,10 @@
                                                int num_trials, int update)
 {
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+  
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cCPUTestInfo test_info;
   const cInstruction inst_none = inst_set.GetInst("instruction_none");
   
@@ -86,7 +95,7 @@
     for (int j = 0; j < num_nops; j++)  genome.Insert(ins_pos, inst_none);
     
     // Test the genome and output stats
-    if (testcpu->TestGenome(test_info, genome)){ // Daughter viable...
+    if (testcpu->TestGenome(ctx, test_info, genome)){ // Daughter viable...
       num_viable++;
       const double child_size =
         test_info.GetColonyOrganism()->GetGenome().GetSize();
@@ -111,8 +120,11 @@
 cGenome cAnalyzeUtil::CalcLandscape(cWorld* world, int dist, const cGenome & genome,
                                     cInstSet & inst_set)
 {
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cLandscape landscape(world, genome, inst_set);
-  landscape.Process(dist);
+  landscape.Process(ctx, dist);
   double peak_fitness = landscape.GetPeakFitness();
   cGenome peak_genome = landscape.GetPeakGenome();
   
@@ -123,7 +135,7 @@
   
   // Repeat for Insertions...
   landscape.Reset(genome);
-  landscape.ProcessInsert();
+  landscape.ProcessInsert(ctx);
   landscape.PrintStats(world->GetDataFileOFStream("landscape-ins.dat"));
   landscape.PrintSiteCount(world->GetDataFileOFStream("land-ins-sitecount.dat"));
   if (landscape.GetPeakFitness() > peak_fitness) {
@@ -133,7 +145,7 @@
   
   // And Deletions...
   landscape.Reset(genome);
-  landscape.ProcessDelete();
+  landscape.ProcessDelete(ctx);
   landscape.PrintStats(world->GetDataFileOFStream("landscape-del.dat"));
   landscape.PrintSiteCount(world->GetDataFileOFStream("land-del-sitecount.dat"));
   if (landscape.GetPeakFitness() > peak_fitness) {
@@ -148,6 +160,9 @@
 void cAnalyzeUtil::AnalyzeLandscape(cWorld* world, const cGenome & genome, cInstSet &inst_set,
                                     int sample_size, int min_found, int max_sample_size, int update)
 {
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cLandscape landscape(world, genome, inst_set);
   ofstream& fp = world->GetDataFileOFStream("land_analyze.dat");
   
@@ -155,10 +170,10 @@
   for (int dist = 1; dist <= 10; dist++) {
     landscape.Reset(genome);
     if (dist == 1) {
-      landscape.Process(dist);
+      landscape.Process(ctx, dist);
       num_found = genome.GetSize() * (inst_set.GetSize() - 1);
     } else {
-      num_found = landscape.RandomProcess(sample_size, dist, min_found, max_sample_size);
+      num_found = landscape.RandomProcess(ctx, sample_size, dist, min_found, max_sample_size);
     }
     
     fp << update                       << " "  // 1
@@ -181,14 +196,17 @@
 void cAnalyzeUtil::PairTestLandscape(cWorld* world, const cGenome &genome, cInstSet &inst_set,
                                      int sample_size, int update)
 {
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cLandscape landscape(world, genome, inst_set);
   
   cString filename;
   filename.Set("pairtest.%d.dat", update);
   ofstream& fp = world->GetDataFileOFStream(filename);
   
-  if (sample_size != 0) landscape.TestPairs(sample_size, fp);
-  else landscape.TestAllPairs(fp);
+  if (sample_size != 0) landscape.TestPairs(ctx, sample_size, fp);
+  else landscape.TestAllPairs(ctx, fp);
   
   world->GetDataFileManager().Remove(filename);
 }
@@ -324,8 +342,12 @@
   }
   else {
     cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+    
     cCPUTestInfo test_info;
-    testcpu->TestGenome(test_info, con_genome);
+    testcpu->TestGenome(ctx, test_info, con_genome);
     delete testcpu;
     
     cPhenotype& colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
@@ -384,6 +406,9 @@
   cPopulation* pop = &world->GetPopulation();
   fp << "# (1) cell number (2) genotype name (3) length (4) fitness [test-cpu] (5) fitness (actual) (6) merit (7) no of breed trues occurred (8) lineage label (9) neutral metric (10) -... landscape data" << endl;
   
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   const double skip_prob = 1.0 - sample_prob;
   for (int i = 0; i < pop->GetSize(); i++) {
     if (pop->GetCell(i).IsOccupied() == false) continue;  // No organism...
@@ -411,7 +436,7 @@
     // create landscape object for this creature
     if (landscape &&  genotype->GetTestFitness() > 0) {
       cLandscape landscape(world, genome, world->GetHardwareManager().GetInstSet());
-      landscape.Process(1);
+      landscape.Process(ctx, 1);
       landscape.PrintStats(fp);
     }
     else fp << endl;
@@ -477,6 +502,9 @@
   
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
 
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   for (int i = 0; i < pop->GetSize(); i++) {
     if (pop->GetCell(i).IsOccupied() == false) continue;  // One use organisms.
     
@@ -484,7 +512,7 @@
     cGenotype * genotype = organism->GetGenotype();
     
     cCPUTestInfo test_info;
-    testcpu->TestGenome( test_info, genotype->GetGenome() );
+    testcpu->TestGenome(ctx, test_info, genotype->GetGenome());
     // We calculate the fitness based on the current merit,
     // but with the true gestation time. Also, we set the fitness
     // to zero if the creature is not viable.
@@ -697,13 +725,16 @@
   
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
 
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   for (int i = 0; i < pop->GetSize(); i++) {
     if (pop->GetCell(i).IsOccupied() == false) continue;
     cOrganism * organism = pop->GetCell(i).GetOrganism();
     
     // create a test-cpu for the current creature
     cCPUTestInfo test_info;
-    testcpu->TestGenome( test_info, organism->GetGenome() );
+    testcpu->TestGenome(ctx, test_info, organism->GetGenome());
     cPhenotype & test_phenotype = test_info.GetTestOrganism()->GetPhenotype();
     cPhenotype & phenotype = organism->GetPhenotype();
     
@@ -753,6 +784,9 @@
   cPopulation* pop = &world->GetPopulation();
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
 
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   for (int i = 0; i < pop->GetWorldX(); i++) {
     for (int j = 0; j < pop->GetWorldY(); j++) {
       int task_sum = 0;
@@ -760,7 +794,7 @@
       if (pop->GetCell(cell_num).IsOccupied() == true) {
         cOrganism * organism = pop->GetCell(cell_num).GetOrganism();
         cCPUTestInfo test_info;
-        testcpu->TestGenome( test_info, organism->GetGenome() );
+        testcpu->TestGenome(ctx, test_info, organism->GetGenome());
         cPhenotype & test_phenotype = test_info.GetTestOrganism()->GetPhenotype();
         int num_tasks = world->GetEnvironment().GetTaskLib().GetSize();   
         for (int k = 0; k < num_tasks; k++) {

Modified: development/source/analyze/tAnalyzeJob.h
===================================================================
--- development/source/analyze/tAnalyzeJob.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/analyze/tAnalyzeJob.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -13,17 +13,24 @@
 #ifndef cAnalyzeJob_h
 #include "cAnalyzeJob.h"
 #endif
+#ifndef cAvidaContext_h
+#include "cAvidaContext.h"
+#endif
 
 template <class T> class tAnalyzeJob : public cAnalyzeJob
 {
 protected:
   T* m_target;
-  void (T::*JobTask)() const;
+  void (T::*JobTask)(cAvidaContext&);
 
 public:
-  tAnalyzeJob(T* target, void (T::*funJ)() const) : cAnalyzeJob(), m_target(target), JobTask(funJ) { ; }
+  tAnalyzeJob(T* target, void (T::*funJ)(cAvidaContext&)) : cAnalyzeJob(), m_target(target), JobTask(funJ) { ; }
   
-  void Run() { (m_target->*JobTask)(); }
+  void Run()
+  {
+    cAvidaContext ctx(0);
+    (m_target->*JobTask)(ctx);
+  }
 };
 
 #endif

Modified: development/source/classification/cGenotype.cc
===================================================================
--- development/source/classification/cGenotype.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/classification/cGenotype.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cGenotype.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "cGenomeUtil.h"
 #include "cHardwareManager.h"
@@ -160,10 +161,8 @@
   int i;
 
   for (i = 0; i < genome.GetSize(); i++) {
-    if (true) { // m_world->GetRandom().GetUInt()) {     //@CAO always true!
-      genome[i].SetOp(m_world->GetRandom().GetUInt(m_world->GetNumInstructions()));
-      // Flag command as having been mutated? @CAO
-    }
+    genome[i].SetOp(m_world->GetRandom().GetUInt(m_world->GetNumInstructions()));
+    // Flag command as having been mutated? @CAO
   }
 }
 
@@ -200,8 +199,12 @@
 void cGenotype::CalcTestStats() const
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+  
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cCPUTestInfo test_info;
-  testcpu->TestGenome(test_info, genome);
+  testcpu->TestGenome(ctx, test_info, genome);
   test_data.is_viable = test_info.IsViable();
   delete testcpu;
 

Modified: development/source/classification/cGenotype.h
===================================================================
--- development/source/classification/cGenotype.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/classification/cGenotype.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -185,8 +185,8 @@
 
 
   // For tracking the genotype line back to the ancestor...
-  cGenotype * GetParentGenotype() { return birth_data.parent_genotype; }
-  cGenotype * GetParent2Genotype() { return birth_data.parent2_genotype; }
+  cGenotype* GetParentGenotype() { return birth_data.parent_genotype; }
+  cGenotype* GetParent2Genotype() { return birth_data.parent2_genotype; }
   int GetNumOffspringGenotypes() const
     { return birth_data.num_offspring_genotypes; }
   void AddOffspringGenotype() { birth_data.num_offspring_genotypes++; }

Modified: development/source/classification/cSpecies.cc
===================================================================
--- development/source/classification/cSpecies.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/classification/cSpecies.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cSpecies.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "functions.h"
 #include "cGenotype.h"
@@ -77,12 +78,16 @@
 int cSpecies::Compare(const cGenome & test_genome, int max_fail_count)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
+  
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cCPUTestInfo test_info;
 
   // First, make some phenotypic comparisons between organisms.
   // For now, just check that they both copy-true.
 
-  testcpu->TestGenome(test_info, test_genome);
+  testcpu->TestGenome(ctx, test_info, test_genome);
 
   // If the organisms aren't viable, return a -1...
   if (test_info.IsViable() == false) {
@@ -116,10 +121,10 @@
       cross_genome2[i] = genome[i];
    
       // Run each side, and determine viability...
-      testcpu->TestGenome(test_info, cross_genome1);
+      testcpu->TestGenome(ctx, test_info, cross_genome1);
       cross1_viable = test_info.IsViable();
 
-      testcpu->TestGenome(test_info, cross_genome2);
+      testcpu->TestGenome(ctx, test_info, cross_genome2);
       cross2_viable = test_info.IsViable();
     }
 

Modified: development/source/cpu/cHardware4Stack.cc
===================================================================
--- development/source/cpu/cHardware4Stack.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardware4Stack.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cHardware4Stack.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "functions.h"
 #include "cGenomeUtil.h"
@@ -310,7 +311,7 @@
 // This function processes the very next command in the genome, and is made
 // to be as optimized as possible.  This is the heart of avida.
 
-void cHardware4Stack::SingleProcess()
+void cHardware4Stack::SingleProcess(cAvidaContext& ctx)
 {
   // Mark this organism as running...
   organism->SetRunning(true);
@@ -343,14 +344,14 @@
     }
     
     // Find the instruction to be executed
-    const cInstruction & cur_inst = IP().GetInst();
+    const cInstruction& cur_inst = IP().GetInst();
     
     // Test if costs have been paid and it is okay to execute this now...
     const bool exec = SingleProcess_PayCosts(cur_inst);
     
     // Now execute the instruction...
     if (exec == true) {
-      SingleProcess_ExecuteInst(cur_inst);
+      SingleProcess_ExecuteInst(ctx, cur_inst);
       
       // Some instruction (such as jump) may turn advance_ip off.  Ususally
       // we now want to move to the next instruction in the memory.
@@ -403,14 +404,14 @@
 
 // This method will handle the actuall execution of an instruction
 // within single process, once that function has been finalized.
-bool cHardware4Stack::SingleProcess_ExecuteInst(const cInstruction & cur_inst) 
+bool cHardware4Stack::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction & cur_inst) 
 {
   // Copy Instruction locally to handle stochastic effects
   cInstruction actual_inst = cur_inst;
   
 #ifdef EXECUTION_ERRORS
   // If there is an execution error, execute a random instruction.
-  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst();
+  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst(ctx);
 #endif /* EXECUTION_ERRORS */
   
   // Get a pointer to the corrisponding method...
@@ -426,7 +427,7 @@
 #endif
 	
   // And execute it.
-  const bool exec_success = (this->*(m_functions[inst_idx]))();
+  const bool exec_success = (this->*(m_functions[inst_idx]))(ctx);
 	
 #ifdef INSTRUCTION_COUNT
   // decremenet if the instruction was not executed successfully
@@ -439,7 +440,7 @@
 }
 
 
-void cHardware4Stack::ProcessBonusInst(const cInstruction & inst)
+void cHardware4Stack::ProcessBonusInst(cAvidaContext& ctx, const cInstruction & inst)
 {
   // Mark this organism as running...
   bool prev_run_state = organism->GetIsRunning();
@@ -455,7 +456,7 @@
     }
   }
   
-  SingleProcess_ExecuteInst(inst);
+  SingleProcess_ExecuteInst(ctx, inst);
   
   organism->SetRunning(prev_run_state);
 }
@@ -835,7 +836,7 @@
 }
 
 // This is the code run by the INFECTED organism.  Its function is to SPREAD infection.
-bool cHardware4Stack::InjectParasite(double mut_multiplier)
+bool cHardware4Stack::InjectParasite(cAvidaContext& ctx, double mut_multiplier)
 {
   const int end_pos = GetHead(nHardware::HEAD_WRITE).GetPosition();
   const int mem_space_used = GetHead(nHardware::HEAD_WRITE).GetMemSpace();
@@ -857,7 +858,7 @@
   
   cCPUMemory injected_code = GetMemory(mem_space_used);
   
-  Inject_DoMutations(mut_multiplier, injected_code);
+  Inject_DoMutations(ctx, mut_multiplier, injected_code);
   
   int inject_signal = false;
   
@@ -962,26 +963,26 @@
   return true; // (inject succeeds!)
 }
 
-void cHardware4Stack::Mutate(int mut_point)
+void cHardware4Stack::Mutate(cAvidaContext& ctx, int mut_point)
 {
   // Test if trying to mutate outside of genome...
   assert(mut_point >= 0 && mut_point < GetMemory(0).GetSize());
   
-  GetMemory(0)[mut_point] = m_inst_set->GetRandomInst();
+  GetMemory(0)[mut_point] = m_inst_set->GetRandomInst(ctx);
   GetMemory(0).SetFlagMutated(mut_point);
   GetMemory(0).SetFlagPointMut(mut_point);
   //organism->GetPhenotype().IsMutated() = true;
   organism->CPUStats().mut_stats.point_mut_count++;
 }
 
-int cHardware4Stack::PointMutate(const double mut_rate)
+int cHardware4Stack::PointMutate(cAvidaContext& ctx, const double mut_rate)
 {
   const int num_muts =
   m_world->GetRandom().GetRandBinomial(GetMemory(0).GetSize(), mut_rate);
   
   for (int i = 0; i < num_muts; i++) {
     const int pos = m_world->GetRandom().GetUInt(GetMemory(0).GetSize());
-    Mutate(pos);
+    Mutate(ctx, pos);
   }
   
   return num_muts;
@@ -991,7 +992,7 @@
 // Trigger mutations of a specific type.  Outside triggers cannot specify
 // a head since hardware types are not known.
 
-bool cHardware4Stack::TriggerMutations(int trigger)
+bool cHardware4Stack::TriggerMutations(cAvidaContext& ctx, int trigger)
 {
   // Only update triggers should happen from the outside!
   assert(trigger == nMutation::TRIGGER_UPDATE);
@@ -999,10 +1000,10 @@
   // Assume instruction pointer is the intended target (if one is even
   // needed!
   
-  return TriggerMutations(trigger, IP());
+  return TriggerMutations(ctx, trigger, IP());
 }
 
-bool cHardware4Stack::TriggerMutations(int trigger, cHeadCPU& cur_head)
+bool cHardware4Stack::TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head)
 {
   // Collect information about mutations from the organism.
   cLocalMutations & mut_info = organism->GetLocalMutations();
@@ -1027,14 +1028,14 @@
     const double rate = mut_info.GetRate(mut_id);
     switch (scope) {
       case nMutation::SCOPE_GENOME:
-        if (TriggerMutations_ScopeGenome(cur_mut, target_mem, cur_head, rate)) {
+        if (TriggerMutations_ScopeGenome(ctx, cur_mut, target_mem, cur_head, rate)) {
           has_mutation = true;
           mut_info.IncCount(mut_id);
         }
         break;
       case nMutation::SCOPE_LOCAL:
       case nMutation::SCOPE_PROP:
-        if (TriggerMutations_ScopeLocal(cur_mut, target_mem, cur_head, rate)) {
+        if (TriggerMutations_ScopeLocal(ctx, cur_mut, target_mem, cur_head, rate)) {
           has_mutation = true;
           mut_info.IncCount(mut_id);
         }
@@ -1042,7 +1043,7 @@
       case nMutation::SCOPE_GLOBAL:
       case nMutation::SCOPE_SPREAD:
         int num_muts =
-        TriggerMutations_ScopeGlobal(cur_mut, target_mem, cur_head, rate);
+        TriggerMutations_ScopeGlobal(ctx, cur_mut, target_mem, cur_head, rate);
         if (num_muts > 0) {
           has_mutation = true;
           mut_info.IncCount(mut_id, num_muts);
@@ -1054,7 +1055,7 @@
   return has_mutation;
 }
 
-bool cHardware4Stack::TriggerMutations_ScopeGenome(const cMutation* cur_mut,
+bool cHardware4Stack::TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut,
                                                    cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored indicates the probability that a single
@@ -1065,26 +1066,26 @@
     // position in the genome to be mutated.
     cHeadCPU tmp_head(cur_head);
     tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     return true;
   }
   return false;
 }
 
-bool cHardware4Stack::TriggerMutations_ScopeLocal(const cMutation* cur_mut,
+bool cHardware4Stack::TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut,
                                                   cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored is the probability for a mutation at this single
   // position in the genome.
   
   if (m_world->GetRandom().P(rate) == true) {
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, cur_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, cur_head);
     return true;
   }
   return false;
 }
 
-int cHardware4Stack::TriggerMutations_ScopeGlobal(const cMutation* cur_mut,
+int cHardware4Stack::TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut,
                                                   cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The probability we have stored is per-site, so we can pull a random
@@ -1098,20 +1099,21 @@
     for (int i = 0; i < num_mut; i++) {
       cHeadCPU tmp_head(cur_head);
       tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-      TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+      TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     }
   }
   
   return num_mut;
 }
 
-void cHardware4Stack::TriggerMutations_Body(int type, cCPUMemory & target_memory, cHeadCPU& cur_head)
+void cHardware4Stack::TriggerMutations_Body(cAvidaContext& ctx, int type,
+                                            cCPUMemory& target_memory, cHeadCPU& cur_head)
 {
   const int pos = cur_head.GetPosition();
   
   switch (type) {
     case nMutation::TYPE_POINT:
-      target_memory[pos] = m_inst_set->GetRandomInst();
+      target_memory[pos] = m_inst_set->GetRandomInst(ctx);
       target_memory.SetFlagMutated(pos);
       break;
     case nMutation::TYPE_INSERT:
@@ -1258,7 +1260,7 @@
   organism->Fault(fault_loc, fault_type, fault_desc);
 }
 
-bool cHardware4Stack::Divide_CheckViable(const int parent_size,
+bool cHardware4Stack::Divide_CheckViable(cAvidaContext& ctx, const int parent_size,
                                          const int child_size, const int mem_space)
 {
   // Make sure the organism is okay with dividing now...
@@ -1316,7 +1318,7 @@
   return true; // (divide succeeds!)
 }
 
-void cHardware4Stack::Divide_DoMutations(double mut_multiplier)
+void cHardware4Stack::Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier)
 {
   sCPUStats & cpu_stats = organism->CPUStats();
   cCPUMemory & child_genome = organism->ChildGenome();
@@ -1326,14 +1328,14 @@
   // Divide Mutations
   if (organism->TestDivideMut()) {
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize());
-    child_genome[mut_line] = m_inst_set->GetRandomInst();
+    child_genome[mut_line] = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.divide_mut_count++;
   }
   
   // Divide Insertions
   if (organism->TestDivideIns() && child_genome.GetSize() < MAX_CREATURE_SIZE){
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize() + 1);
-    child_genome.Insert(mut_line, m_inst_set->GetRandomInst());
+    child_genome.Insert(mut_line, m_inst_set->GetRandomInst(ctx));
     cpu_stats.mut_stats.divide_insert_mut_count++;
   }
   
@@ -1352,7 +1354,7 @@
     if( num_mut > 0 ){
       for (int i = 0; i < num_mut; i++) {
         int site = m_world->GetRandom().GetUInt(child_genome.GetSize());
-        child_genome[site] = m_inst_set->GetRandomInst();
+        child_genome[site] = m_inst_set->GetRandomInst(ctx);
         cpu_stats.mut_stats.div_mut_count++;
       }
     }
@@ -1378,7 +1380,7 @@
       qsort( (void*)mut_sites, num_mut, sizeof(int), &IntCompareFunction );
       // Actually do the mutations (in reverse sort order)
       for(int i = num_mut-1; i >= 0; i--) {
-        child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst());
+        child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx));
         cpu_stats.mut_stats.insert_mut_count++;
       }
     }
@@ -1406,7 +1408,7 @@
   if (organism->GetParentMutProb() > 0) {
     for (int i = 0; i < GetMemory(0).GetSize(); i++) {
       if (organism->TestParentMut()) {
-        GetMemory(0)[i] = m_inst_set->GetRandomInst();
+        GetMemory(0)[i] = m_inst_set->GetRandomInst(ctx);
         cpu_stats.mut_stats.parent_mut_line_count++;
       }
     }
@@ -1426,7 +1428,7 @@
   }
 }
 
-void cHardware4Stack::Inject_DoMutations(double mut_multiplier, cCPUMemory & injected_code)
+void cHardware4Stack::Inject_DoMutations(cAvidaContext& ctx, double mut_multiplier, cCPUMemory & injected_code)
 {
   //sCPUStats & cpu_stats = organism->CPUStats();
   //cCPUMemory & child_genome = organism->ChildGenome();
@@ -1436,14 +1438,14 @@
   // Divide Mutations
   if (organism->TestDivideMut()) {
     const unsigned int mut_line = m_world->GetRandom().GetUInt(injected_code.GetSize());
-    injected_code[mut_line] = m_inst_set->GetRandomInst();
+    injected_code[mut_line] = m_inst_set->GetRandomInst(ctx);
     //cpu_stats.mut_stats.divide_mut_count++;
   }
   
   // Divide Insertions
   if (organism->TestDivideIns() && injected_code.GetSize() < MAX_CREATURE_SIZE){
     const unsigned int mut_line = m_world->GetRandom().GetUInt(injected_code.GetSize() + 1);
-    injected_code.Insert(mut_line, m_inst_set->GetRandomInst());
+    injected_code.Insert(mut_line, m_inst_set->GetRandomInst(ctx));
     //cpu_stats.mut_stats.divide_insert_mut_count++;
   }
   
@@ -1462,7 +1464,7 @@
     if( num_mut > 0 ){
       for (int i = 0; i < num_mut; i++) {
         int site = m_world->GetRandom().GetUInt(injected_code.GetSize());
-        injected_code[site] = m_inst_set->GetRandomInst();
+        injected_code[site] = m_inst_set->GetRandomInst(ctx);
         //cpu_stats.mut_stats.div_mut_count++;
       }
     }
@@ -1488,7 +1490,7 @@
       qsort( (void*)mut_sites, num_mut, sizeof(int), &IntCompareFunction );
       // Actually do the mutations (in reverse sort order)
       for(int i = num_mut-1; i >= 0; i--) {
-        injected_code.Insert(mut_sites[i], m_inst_set->GetRandomInst());
+        injected_code.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx));
         //cpu_stats.mut_stats.insert_mut_count++;
       }
     }
@@ -1516,7 +1518,7 @@
   if (organism->GetParentMutProb() > 0) {
     for (int i = 0; i < GetMemory(0).GetSize(); i++) {
       if (organism->TestParentMut()) {
-        GetMemory(0)[i] = m_inst_set->GetRandomInst();
+        GetMemory(0)[i] = m_inst_set->GetRandomInst(ctx);
         //cpu_stats.mut_stats.parent_mut_line_count++;
       }
     }
@@ -1526,7 +1528,7 @@
 
 
 // test whether the offspring creature contains an advantageous mutation.
-void cHardware4Stack::Divide_TestFitnessMeasures()
+void cHardware4Stack::Divide_TestFitnessMeasures(cAvidaContext& ctx)
 {
   cPhenotype & phenotype = organism->GetPhenotype();
   phenotype.CopyTrue() = ( organism->ChildGenome() == organism->GetGenome() );
@@ -1548,7 +1550,7 @@
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   cCPUTestInfo test_info;
   test_info.UseRandomInputs();
-  testcpu->TestGenome(test_info, organism->ChildGenome());
+  testcpu->TestGenome(ctx, test_info, organism->ChildGenome());
   const double child_fitness = test_info.GetGenotypeFitness();
   delete testcpu;
   
@@ -1587,7 +1589,7 @@
 }
 
 
-bool cHardware4Stack::Divide_Main(int mem_space_used, double mut_multiplier)
+bool cHardware4Stack::Divide_Main(cAvidaContext& ctx, int mem_space_used, double mut_multiplier)
 {
   int write_head_pos = GetHead(nHardware::HEAD_WRITE).GetPosition();
   
@@ -1597,7 +1599,7 @@
     return false;
   
   // Make sure this divide will produce a viable offspring.
-  if(!Divide_CheckViable(GetMemory(IP().GetMemSpace()).GetSize(), 
+  if(!Divide_CheckViable(ctx, GetMemory(IP().GetMemSpace()).GetSize(), 
                          write_head_pos, mem_space_used)) 
     return false;
   
@@ -1608,12 +1610,12 @@
   child_genome = GetMemory(mem_space_used);
   
   // Handle Divide Mutations...
-  Divide_DoMutations(mut_multiplier);
+  Divide_DoMutations(ctx, mut_multiplier);
   
   // Many tests will require us to run the offspring through a test CPU;
   // this is, for example, to see if mutations need to be reverted or if
   // lineages need to be updated.
-  Divide_TestFitnessMeasures();
+  Divide_TestFitnessMeasures(ctx);
   
 #ifdef INSTRUCTION_COSTS
   // reset first time instruction costs
@@ -1682,7 +1684,7 @@
 
 
 //6
-bool cHardware4Stack::Inst_ShiftR()
+bool cHardware4Stack::Inst_ShiftR(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int value = Stack(stack_used).Pop();
@@ -1692,7 +1694,7 @@
 }
 
 //7
-bool cHardware4Stack::Inst_ShiftL()
+bool cHardware4Stack::Inst_ShiftL(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int value = Stack(stack_used).Pop();
@@ -1702,7 +1704,7 @@
 }
 
 //8
-bool cHardware4Stack::Inst_Val_Nand()
+bool cHardware4Stack::Inst_Val_Nand(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Push(~(Stack(nHardware4Stack::STACK_BX).Top() & Stack(nHardware4Stack::STACK_CX).Top()));
@@ -1710,7 +1712,7 @@
 }
 
 //9
-bool cHardware4Stack::Inst_Val_Add()
+bool cHardware4Stack::Inst_Val_Add(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Push(Stack(nHardware4Stack::STACK_BX).Top() + Stack(nHardware4Stack::STACK_CX).Top());
@@ -1718,7 +1720,7 @@
 }
 
 //10
-bool cHardware4Stack::Inst_Val_Sub()
+bool cHardware4Stack::Inst_Val_Sub(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Push(Stack(nHardware4Stack::STACK_BX).Top() - Stack(nHardware4Stack::STACK_CX).Top());
@@ -1726,7 +1728,7 @@
 }
 
 //11
-bool cHardware4Stack::Inst_Val_Mult()
+bool cHardware4Stack::Inst_Val_Mult(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Push(Stack(nHardware4Stack::STACK_BX).Top() * Stack(nHardware4Stack::STACK_CX).Top());
@@ -1734,7 +1736,7 @@
 }
 
 //12
-bool cHardware4Stack::Inst_Val_Div()
+bool cHardware4Stack::Inst_Val_Div(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   if (Stack(nHardware4Stack::STACK_CX).Top() != 0) {
@@ -1750,7 +1752,7 @@
 }
 
 //13 
-bool cHardware4Stack::Inst_SetMemory()   // Allocate maximal more
+bool cHardware4Stack::Inst_SetMemory(cAvidaContext& ctx)
 {
   int mem_space_used = FindModifiedStack(-1);
   
@@ -1765,22 +1767,16 @@
 }
 
 //14
-bool cHardware4Stack::Inst_Divide()
+bool cHardware4Stack::Inst_Divide(cAvidaContext& ctx)
 {
   int mem_space_used = GetHead(nHardware::HEAD_WRITE).GetMemSpace();
   int mut_multiplier = 1;
   
-  return Divide_Main(mem_space_used, mut_multiplier);
+  return Divide_Main(ctx, mem_space_used, mut_multiplier);
 }
 
-bool cHardware4Stack::Inst_HeadDivideMut(double mut_multiplier)
-{
-  // Unused for the moment...
-  return true;
-}
-
 //15
-bool cHardware4Stack::Inst_HeadRead()
+bool cHardware4Stack::Inst_HeadRead(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_READ);
   GetHead(head_id).Adjust();
@@ -1789,7 +1785,7 @@
   // Mutations only occur on the read, for the moment.
   int read_inst = 0;
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst().GetOp();
+    read_inst = m_inst_set->GetRandomInst(ctx).GetOp();
     cpu_stats.mut_stats.copy_mut_count++;  // @CAO, hope this is good!
   } else {
     read_inst = GetHead(head_id).GetInst().GetOp();
@@ -1803,7 +1799,7 @@
 }
 
 //16
-bool cHardware4Stack::Inst_HeadWrite()
+bool cHardware4Stack::Inst_HeadWrite(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_WRITE);
   cHeadMultiMem & active_head = GetHead(head_id);
@@ -1830,7 +1826,7 @@
 }
 
 //??
-bool cHardware4Stack::Inst_HeadCopy()
+bool cHardware4Stack::Inst_HeadCopy(cAvidaContext& ctx)
 {
   // For the moment, this cannot be nop-modified.
   cHeadMultiMem & read_head = GetHead(nHardware::HEAD_READ);
@@ -1845,7 +1841,7 @@
   // Do mutations.
   cInstruction read_inst = read_head.GetInst();
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst();
+    read_inst = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.copy_mut_count++; 
     write_head.SetFlagMutated();
     write_head.SetFlagCopyMut();
@@ -1866,7 +1862,7 @@
 }
 
 //17
-bool cHardware4Stack::Inst_IfEqual()      // Execute next if bx == ?cx?
+bool cHardware4Stack::Inst_IfEqual(cAvidaContext& ctx)      // Execute next if bx == ?cx?
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_AX);
   const int stack_used2 = (stack_used+1)%nHardware4Stack::NUM_STACKS;
@@ -1875,7 +1871,7 @@
 }
 
 //18
-bool cHardware4Stack::Inst_IfNotEqual()     // Execute next if bx != ?cx?
+bool cHardware4Stack::Inst_IfNotEqual(cAvidaContext& ctx)     // Execute next if bx != ?cx?
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_AX);
   const int stack_used2 = (stack_used+1)%nHardware4Stack::NUM_STACKS;
@@ -1884,7 +1880,7 @@
 }
 
 //19
-bool cHardware4Stack::Inst_IfLess()       // Execute next if ?bx? < ?cx?
+bool cHardware4Stack::Inst_IfLess(cAvidaContext& ctx)       // Execute next if ?bx? < ?cx?
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_AX);
   const int stack_used2 = (stack_used+1)%nHardware4Stack::NUM_STACKS;
@@ -1893,7 +1889,7 @@
 }
 
 //20
-bool cHardware4Stack::Inst_IfGreater()       // Execute next if bx > ?cx?
+bool cHardware4Stack::Inst_IfGreater(cAvidaContext& ctx)       // Execute next if bx > ?cx?
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_AX);
   const int stack_used2 = (stack_used+1)%nHardware4Stack::NUM_STACKS;
@@ -1902,7 +1898,7 @@
 }
 
 //21
-bool cHardware4Stack::Inst_HeadPush()
+bool cHardware4Stack::Inst_HeadPush(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   Stack(nHardware4Stack::STACK_BX).Push(GetHead(head_used).GetPosition());
@@ -1914,7 +1910,7 @@
 }
 
 //22
-bool cHardware4Stack::Inst_HeadPop()
+bool cHardware4Stack::Inst_HeadPop(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetHead(head_used).Set(Stack(nHardware4Stack::STACK_BX).Pop(), 
@@ -1923,7 +1919,7 @@
 }
 
 //23 
-bool cHardware4Stack::Inst_HeadMove()
+bool cHardware4Stack::Inst_HeadMove(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   if(head_used != nHardware::HEAD_FLOW)
@@ -1939,7 +1935,7 @@
 }
 
 //24
-bool cHardware4Stack::Inst_Search()
+bool cHardware4Stack::Inst_Search(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(2, nHardware4Stack::NUM_NOPS);
@@ -1964,7 +1960,7 @@
 }
 
 //25
-bool cHardware4Stack::Inst_PushNext() 
+bool cHardware4Stack::Inst_PushNext(cAvidaContext& ctx) 
 {
   int stack_used = FindModifiedStack(nHardware4Stack::STACK_AX);
   int successor = (stack_used+1)%nHardware4Stack::NUM_STACKS;
@@ -1973,7 +1969,7 @@
 }
 
 //26
-bool cHardware4Stack::Inst_PushPrevious() 
+bool cHardware4Stack::Inst_PushPrevious(cAvidaContext& ctx) 
 {
   int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int predecessor = (stack_used+nHardware4Stack::NUM_STACKS-1)%nHardware4Stack::NUM_STACKS;
@@ -1982,7 +1978,7 @@
 }
 
 //27
-bool cHardware4Stack::Inst_PushComplement() 
+bool cHardware4Stack::Inst_PushComplement(cAvidaContext& ctx) 
 {
   int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int complement = FindComplementStack(stack_used);
@@ -1991,7 +1987,7 @@
 }
 
 //28
-bool cHardware4Stack::Inst_ValDelete()
+bool cHardware4Stack::Inst_ValDelete(cAvidaContext& ctx)
 {
   int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Pop();
@@ -1999,7 +1995,7 @@
 }
 
 //29
-bool cHardware4Stack::Inst_ValCopy()
+bool cHardware4Stack::Inst_ValCopy(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   Stack(stack_used).Push(Stack(stack_used).Top());
@@ -2007,7 +2003,7 @@
 }
 
 //30
-bool cHardware4Stack::Inst_ForkThread()
+bool cHardware4Stack::Inst_ForkThread(cAvidaContext& ctx)
 {
   if (!ForkThread()) 
     Fault(FAULT_LOC_THREAD_FORK, FAULT_TYPE_FORK_TH);
@@ -2017,7 +2013,7 @@
 }
 
 //31
-bool cHardware4Stack::Inst_IfLabel()
+bool cHardware4Stack::Inst_IfLabel(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(2, nHardware4Stack::NUM_NOPS);
@@ -2026,7 +2022,7 @@
 }
 
 //32
-bool cHardware4Stack::Inst_Increment()
+bool cHardware4Stack::Inst_Increment(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int value = Stack(stack_used).Pop();
@@ -2035,7 +2031,7 @@
 }
 
 //33
-bool cHardware4Stack::Inst_Decrement()
+bool cHardware4Stack::Inst_Decrement(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   int value = Stack(stack_used).Pop();
@@ -2044,7 +2040,7 @@
 }
 
 //34
-bool cHardware4Stack::Inst_Mod()
+bool cHardware4Stack::Inst_Mod(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   if (Stack(nHardware4Stack::STACK_CX).Top() != 0) {
@@ -2060,7 +2056,7 @@
 }
 
 //35
-bool cHardware4Stack::Inst_KillThread()
+bool cHardware4Stack::Inst_KillThread(cAvidaContext& ctx)
 {
   if (!KillThread()) Fault(FAULT_LOC_THREAD_KILL, FAULT_TYPE_KILL_TH);
   else AdvanceIP() = false;
@@ -2068,13 +2064,13 @@
 }
 
 //36
-bool cHardware4Stack::Inst_IO()
+bool cHardware4Stack::Inst_IO(cAvidaContext& ctx)
 {
   const int stack_used = FindModifiedStack(nHardware4Stack::STACK_BX);
   
   // Do the "put" component
   const int value_out = Stack(stack_used).Top();
-  organism->DoOutput(value_out);  // Check for tasks compleated.
+  organism->DoOutput(ctx, value_out);  // Check for tasks compleated.
   
   // Do the "get" component
   const int value_in = organism->GetNextInput();
@@ -2131,11 +2127,11 @@
 // It will then look at the template that follows the command and inject it
 // into the complement template found in a neighboring organism.
 
-bool cHardware4Stack::Inst_Inject()
+bool cHardware4Stack::Inst_Inject(cAvidaContext& ctx)
 {
   double mut_multiplier = 1;
   
-  return InjectParasite(mut_multiplier);
+  return InjectParasite(ctx, mut_multiplier);
 }
 
 

Modified: development/source/cpu/cHardware4Stack.h
===================================================================
--- development/source/cpu/cHardware4Stack.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardware4Stack.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -41,6 +41,7 @@
 #include "tArray.h"
 #endif
 
+class cAvidaContext;
 class cOrganism;
 class cMutation;
 class cInjectGenotype;
@@ -65,7 +66,7 @@
 class cHardware4Stack : public cHardwareBase
 {
 public:
-  typedef bool (cHardware4Stack::*tHardware4StackMethod)();
+  typedef bool (cHardware4Stack::*tHardware4StackMethod)(cAvidaContext& ctx);
 private:
   static cInstLib4Stack* s_inst_slib;
   static cInstLib4Stack* initInstLib(void);
@@ -97,7 +98,7 @@
 
   
   bool SingleProcess_PayCosts(const cInstruction & cur_inst);
-  bool SingleProcess_ExecuteInst(const cInstruction & cur_inst);
+  bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction & cur_inst);
   
   
   // --------  Stack Manipulation...  --------
@@ -123,13 +124,13 @@
   cCodeLabel & GetReadLabel() { return threads[cur_thread].read_label; }
   
 
-  bool TriggerMutations_ScopeGenome(const cMutation* cur_mut, cCPUMemory& target_memory,
+  bool TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut, cCPUMemory& target_memory,
                                     cHeadCPU& cur_head, const double rate);
-  bool TriggerMutations_ScopeLocal(const cMutation* cur_mut, cCPUMemory& target_memory,
+  bool TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut, cCPUMemory& target_memory,
                                    cHeadCPU& cur_head, const double rate);
-  int TriggerMutations_ScopeGlobal(const cMutation* cur_mut, cCPUMemory& target_memory,
+  int TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut, cCPUMemory& target_memory,
                                    cHeadCPU& cur_head, const double rate);
-  void TriggerMutations_Body(int type, cCPUMemory & target_memory, cHeadCPU& cur_head);
+  void TriggerMutations_Body(cAvidaContext& ctx, int type, cCPUMemory & target_memory, cHeadCPU& cur_head);
 
   // ---------- Instruction Helpers -----------
   int FindModifiedStack(int default_stack);
@@ -142,17 +143,16 @@
   bool Allocate_Default(const int new_size);
   bool Allocate_Main(const int allocated_size);
   
-  bool Divide_Main(const int mem_space_used, double mut_multiplier=1);
-  bool Divide_CheckViable(const int parent_size, const int child_size, const int mem_space);
-  void Divide_DoMutations(double mut_multiplier=1);
-  void Inject_DoMutations(double mut_multiplier, cCPUMemory & injected_code);
-  void Divide_TestFitnessMeasures();
-  void Mutate(const int mut_point);
+  bool Divide_Main(cAvidaContext& ctx, const int mem_space_used, double mut_multiplier=1);
+  bool Divide_CheckViable(cAvidaContext& ctx, const int parent_size, const int child_size, const int mem_space);
+  void Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier = 1);
+  void Inject_DoMutations(cAvidaContext& ctx, double mut_multiplier, cCPUMemory & injected_code);
+  void Divide_TestFitnessMeasures(cAvidaContext& ctx);
+  void Mutate(cAvidaContext& ctx, const int mut_point);
   
   bool HeadCopy_ErrorCorrect(double reduction);
-  bool Inst_HeadDivideMut(double mut_multiplier=1);
 
-  bool InjectParasite(double mut_multiplier);
+  bool InjectParasite(cAvidaContext& ctx, double mut_multiplier);
 
   void ReadInst(const int in_inst);
   
@@ -172,8 +172,8 @@
   static cString GetDefaultInstFilename() { return "inst_lib.4stack"; }
 
   void Reset();
-  void SingleProcess();
-  void ProcessBonusInst(const cInstruction & inst);
+  void SingleProcess(cAvidaContext& ctx);
+  void ProcessBonusInst(cAvidaContext& ctx, const cInstruction & inst);
 
   // --------  Helper methods  --------
   int GetType() const { return HARDWARE_TYPE_CPU_4STACK; }
@@ -242,46 +242,46 @@
 	
   
   // --------  Mutation  --------
-  int PointMutate(const double mut_rate);  
-  bool TriggerMutations(int trigger);
-  bool TriggerMutations(int trigger, cHeadCPU& cur_head);
+  int PointMutate(cAvidaContext& ctx, const double mut_rate);  
+  bool TriggerMutations(cAvidaContext& ctx, int trigger);
+  bool TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head);
   
 
 private:
   // ---------- Instruction Library -----------
-  bool Inst_ShiftR();
-  bool Inst_ShiftL();
-  bool Inst_Val_Nand();
-  bool Inst_Val_Add();
-  bool Inst_Val_Sub();
-  bool Inst_Val_Mult();
-  bool Inst_Val_Div();
-  bool Inst_SetMemory();
-  bool Inst_Divide();
-  bool Inst_HeadRead();
-  bool Inst_HeadWrite();
-  bool Inst_HeadCopy();
-  bool Inst_IfEqual();
-  bool Inst_IfNotEqual();
-  bool Inst_IfLess();
-  bool Inst_IfGreater();
-  bool Inst_HeadPush();
-  bool Inst_HeadPop();
-  bool Inst_HeadMove();
-  bool Inst_Search();
-  bool Inst_PushNext();
-  bool Inst_PushPrevious();
-  bool Inst_PushComplement();
-  bool Inst_ValDelete();
-  bool Inst_ValCopy();
-  bool Inst_ForkThread();
-  bool Inst_IfLabel();
-  bool Inst_Increment();
-  bool Inst_Decrement();
-  bool Inst_Mod();
-  bool Inst_KillThread();
-  bool Inst_IO();
-  bool Inst_Inject();  
+  bool Inst_ShiftR(cAvidaContext& ctx);
+  bool Inst_ShiftL(cAvidaContext& ctx);
+  bool Inst_Val_Nand(cAvidaContext& ctx);
+  bool Inst_Val_Add(cAvidaContext& ctx);
+  bool Inst_Val_Sub(cAvidaContext& ctx);
+  bool Inst_Val_Mult(cAvidaContext& ctx);
+  bool Inst_Val_Div(cAvidaContext& ctx);
+  bool Inst_SetMemory(cAvidaContext& ctx);
+  bool Inst_Divide(cAvidaContext& ctx);
+  bool Inst_HeadRead(cAvidaContext& ctx);
+  bool Inst_HeadWrite(cAvidaContext& ctx);
+  bool Inst_HeadCopy(cAvidaContext& ctx);
+  bool Inst_IfEqual(cAvidaContext& ctx);
+  bool Inst_IfNotEqual(cAvidaContext& ctx);
+  bool Inst_IfLess(cAvidaContext& ctx);
+  bool Inst_IfGreater(cAvidaContext& ctx);
+  bool Inst_HeadPush(cAvidaContext& ctx);
+  bool Inst_HeadPop(cAvidaContext& ctx);
+  bool Inst_HeadMove(cAvidaContext& ctx);
+  bool Inst_Search(cAvidaContext& ctx);
+  bool Inst_PushNext(cAvidaContext& ctx);
+  bool Inst_PushPrevious(cAvidaContext& ctx);
+  bool Inst_PushComplement(cAvidaContext& ctx);
+  bool Inst_ValDelete(cAvidaContext& ctx);
+  bool Inst_ValCopy(cAvidaContext& ctx);
+  bool Inst_ForkThread(cAvidaContext& ctx);
+  bool Inst_IfLabel(cAvidaContext& ctx);
+  bool Inst_Increment(cAvidaContext& ctx);
+  bool Inst_Decrement(cAvidaContext& ctx);
+  bool Inst_Mod(cAvidaContext& ctx);
+  bool Inst_KillThread(cAvidaContext& ctx);
+  bool Inst_IO(cAvidaContext& ctx);
+  bool Inst_Inject(cAvidaContext& ctx);  
 };
 
 

Modified: development/source/cpu/cHardwareBase.cc
===================================================================
--- development/source/cpu/cHardwareBase.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareBase.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,7 +10,7 @@
 
 #include "cHardwareBase.h"
 
-bool cHardwareBase::Inst_Nop()          // Do Nothing.
+bool cHardwareBase::Inst_Nop(cAvidaContext& ctx)          // Do Nothing.
 {
   return true;
 }

Modified: development/source/cpu/cHardwareBase.h
===================================================================
--- development/source/cpu/cHardwareBase.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareBase.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -15,6 +15,7 @@
 
 using namespace std;
 
+class cAvidaContext;
 class cCodeLabel;
 class cCPUMemory;
 class cGenome;
@@ -55,8 +56,8 @@
   
   // --------  Core Functionality  --------
   virtual void Reset() = 0;
-  virtual void SingleProcess() = 0;
-  virtual void ProcessBonusInst(const cInstruction& inst) = 0;
+  virtual void SingleProcess(cAvidaContext& ctx) = 0;
+  virtual void ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst) = 0;
   
   
   // --------  Helper methods  --------
@@ -124,14 +125,14 @@
   
   
   // --------  Mutation  --------
-  virtual int PointMutate(const double mut_rate) = 0;
-  virtual bool TriggerMutations(int trigger) = 0;
-  virtual bool TriggerMutations(int trigger, cHeadCPU& cur_head) = 0;
+  virtual int PointMutate(cAvidaContext& ctx, const double mut_rate) = 0;
+  virtual bool TriggerMutations(cAvidaContext& ctx, int trigger) = 0;
+  virtual bool TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head) = 0;
   
   
 protected:
   // --------  No-Operation Instruction --------
-  bool Inst_Nop();  // A no-operation instruction that does nothing! 
+  bool Inst_Nop(cAvidaContext& ctx);  // A no-operation instruction that does nothing! 
 };
 
 #endif

Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareCPU.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -433,7 +433,7 @@
 // This function processes the very next command in the genome, and is made
 // to be as optimized as possible.  This is the heart of avida.
 
-void cHardwareCPU::SingleProcess()
+void cHardwareCPU::SingleProcess(cAvidaContext& ctx)
 {
   // Mark this organism as running...
   organism->SetRunning(true);
@@ -477,7 +477,7 @@
     
     // Now execute the instruction...
     if (exec == true) {
-      SingleProcess_ExecuteInst(cur_inst);
+      SingleProcess_ExecuteInst(ctx, cur_inst);
       
       // Some instruction (such as jump) may turn advance_ip off.  Ususally
       // we now want to move to the next instruction in the memory.
@@ -531,14 +531,14 @@
 
 // This method will handle the actuall execution of an instruction
 // within single process, once that function has been finalized.
-bool cHardwareCPU::SingleProcess_ExecuteInst(const cInstruction & cur_inst) 
+bool cHardwareCPU::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction & cur_inst) 
 {
   // Copy Instruction locally to handle stochastic effects
   cInstruction actual_inst = cur_inst;
   
 #ifdef EXECUTION_ERRORS
   // If there is an execution error, execute a random instruction.
-  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst();
+  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst(ctx);
 #endif /* EXECUTION_ERRORS */
   
   // Get a pointer to the corrisponding method...
@@ -554,7 +554,7 @@
 #endif
 	
   // And execute it.
-  const bool exec_success = (this->*(m_functions[inst_idx]))();
+  const bool exec_success = (this->*(m_functions[inst_idx]))(ctx);
 	
 #ifdef INSTRUCTION_COUNT
   // decremenet if the instruction was not executed successfully
@@ -567,7 +567,7 @@
 }
 
 
-void cHardwareCPU::ProcessBonusInst(const cInstruction & inst)
+void cHardwareCPU::ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst)
 {
   // Mark this organism as running...
   bool prev_run_state = organism->GetIsRunning();
@@ -583,7 +583,7 @@
     }
   }
   
-  SingleProcess_ExecuteInst(inst);
+  SingleProcess_ExecuteInst(ctx, inst);
   
   organism->SetRunning(prev_run_state);
 }
@@ -1047,26 +1047,26 @@
   
 }
 
-void cHardwareCPU::Mutate(int mut_point)
+void cHardwareCPU::Mutate(cAvidaContext& ctx, int mut_point)
 {
   // Test if trying to mutate outside of genome...
   assert(mut_point >= 0 && mut_point < GetMemory().GetSize());
   
-  GetMemory()[mut_point] = m_inst_set->GetRandomInst();
+  GetMemory()[mut_point] = m_inst_set->GetRandomInst(ctx);
   GetMemory().SetFlagMutated(mut_point);
   GetMemory().SetFlagPointMut(mut_point);
   //organism->GetPhenotype().IsMutated() = true;
   organism->CPUStats().mut_stats.point_mut_count++;
 }
 
-int cHardwareCPU::PointMutate(const double mut_rate)
+int cHardwareCPU::PointMutate(cAvidaContext& ctx, const double mut_rate)
 {
   const int num_muts =
   m_world->GetRandom().GetRandBinomial(GetMemory().GetSize(), mut_rate);
   
   for (int i = 0; i < num_muts; i++) {
     const int pos = m_world->GetRandom().GetUInt(GetMemory().GetSize());
-    Mutate(pos);
+    Mutate(ctx, pos);
   }
   
   return num_muts;
@@ -1076,7 +1076,7 @@
 // Trigger mutations of a specific type.  Outside triggers cannot specify
 // a head since hardware types are not known.
 
-bool cHardwareCPU::TriggerMutations(int trigger)
+bool cHardwareCPU::TriggerMutations(cAvidaContext& ctx, int trigger)
 {
   // Only update triggers should happen from the outside!
   assert(trigger == nMutation::TRIGGER_UPDATE);
@@ -1084,10 +1084,10 @@
   // Assume instruction pointer is the intended target (if one is even
   // needed!
   
-  return TriggerMutations(trigger, IP());
+  return TriggerMutations(ctx, trigger, IP());
 }
 
-bool cHardwareCPU::TriggerMutations(int trigger, cHeadCPU & cur_head)
+bool cHardwareCPU::TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU & cur_head)
 {
   // Collect information about mutations from the organism.
   cLocalMutations & mut_info = organism->GetLocalMutations();
@@ -1112,14 +1112,14 @@
     const double rate = mut_info.GetRate(mut_id);
     switch (scope) {
       case nMutation::SCOPE_GENOME:
-        if (TriggerMutations_ScopeGenome(cur_mut, target_mem, cur_head, rate)) {
+        if (TriggerMutations_ScopeGenome(ctx, cur_mut, target_mem, cur_head, rate)) {
           has_mutation = true;
           mut_info.IncCount(mut_id);
         }
         break;
       case nMutation::SCOPE_LOCAL:
       case nMutation::SCOPE_PROP:
-        if (TriggerMutations_ScopeLocal(cur_mut, target_mem, cur_head, rate)) {
+        if (TriggerMutations_ScopeLocal(ctx, cur_mut, target_mem, cur_head, rate)) {
           has_mutation = true;
           mut_info.IncCount(mut_id);
         }
@@ -1127,7 +1127,7 @@
       case nMutation::SCOPE_GLOBAL:
       case nMutation::SCOPE_SPREAD:
         int num_muts =
-        TriggerMutations_ScopeGlobal(cur_mut, target_mem, cur_head, rate);
+        TriggerMutations_ScopeGlobal(ctx, cur_mut, target_mem, cur_head, rate);
         if (num_muts > 0) {
           has_mutation = true;
           mut_info.IncCount(mut_id, num_muts);
@@ -1139,8 +1139,8 @@
   return has_mutation;
 }
 
-bool cHardwareCPU::TriggerMutations_ScopeGenome(const cMutation * cur_mut,
-                                                cCPUMemory & target_memory, cHeadCPU & cur_head, const double rate)
+bool cHardwareCPU::TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut,
+                                                cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored indicates the probability that a single
   // mutation will occur anywhere in the genome.
@@ -1150,27 +1150,27 @@
     // position in the genome to be mutated.
     cHeadCPU tmp_head(cur_head);
     tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     return true;
   }
   return false;
 }
 
-bool cHardwareCPU::TriggerMutations_ScopeLocal(const cMutation * cur_mut,
-                                               cCPUMemory & target_memory, cHeadCPU & cur_head, const double rate)
+bool cHardwareCPU::TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut,
+                                               cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored is the probability for a mutation at this single
   // position in the genome.
   
   if (m_world->GetRandom().P(rate) == true) {
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, cur_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, cur_head);
     return true;
   }
   return false;
 }
 
-int cHardwareCPU::TriggerMutations_ScopeGlobal(const cMutation * cur_mut,
-                                               cCPUMemory & target_memory, cHeadCPU & cur_head, const double rate)
+int cHardwareCPU::TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut,
+                                               cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The probability we have stored is per-site, so we can pull a random
   // number from a binomial distribution to determine the number of mutations
@@ -1183,21 +1183,21 @@
     for (int i = 0; i < num_mut; i++) {
       cHeadCPU tmp_head(cur_head);
       tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-      TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+      TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     }
   }
   
   return num_mut;
 }
 
-void cHardwareCPU::TriggerMutations_Body(int type, cCPUMemory & target_memory,
+void cHardwareCPU::TriggerMutations_Body(cAvidaContext& ctx, int type, cCPUMemory & target_memory,
                                          cHeadCPU & cur_head)
 {
   const int pos = cur_head.GetPosition();
   
   switch (type) {
     case nMutation::TYPE_POINT:
-      target_memory[pos] = m_inst_set->GetRandomInst();
+      target_memory[pos] = m_inst_set->GetRandomInst(ctx);
       target_memory.SetFlagMutated(pos);
       break;
     case nMutation::TYPE_INSERT:
@@ -1361,12 +1361,12 @@
   return true;
 }
 
-bool cHardwareCPU::Allocate_Random(const int old_size, const int new_size)
+bool cHardwareCPU::Allocate_Random(cAvidaContext& ctx, const int old_size, const int new_size)
 {
   GetMemory().Resize(new_size);
   
   for (int i = old_size; i < new_size; i++) {
-    GetMemory()[i] = m_inst_set->GetRandomInst();
+    GetMemory()[i] = m_inst_set->GetRandomInst(ctx);
   }
   return true;
 }
@@ -1380,7 +1380,7 @@
   return true;
 }
 
-bool cHardwareCPU::Allocate_Main(const int allocated_size)
+bool cHardwareCPU::Allocate_Main(cAvidaContext& ctx, const int allocated_size)
 {
   // must do divide before second allocate & must allocate positive amount...
   if (m_world->GetConfig().REQUIRE_ALLOCATE.Get() && mal_active == true) {
@@ -1426,7 +1426,7 @@
       // Only break if this succeeds -- otherwise just do random.
       if (Allocate_Necro(new_size) == true) break;
     case ALLOC_METHOD_RANDOM:
-      Allocate_Random(old_size, new_size);
+      Allocate_Random(ctx, old_size, new_size);
       break;
     case ALLOC_METHOD_DEFAULT:
       Allocate_Default(new_size);
@@ -1439,7 +1439,7 @@
 }
 
 
-bool cHardwareCPU::Divide_CheckViable(const int child_size,
+bool cHardwareCPU::Divide_CheckViable(cAvidaContext& ctx, const int child_size,
                                       const int parent_size)
 {
   // Make sure the organism is okay with dividing now...
@@ -1539,7 +1539,7 @@
 }
 
 
-void cHardwareCPU::Divide_DoMutations(double mut_multiplier)
+void cHardwareCPU::Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier)
 {
   sCPUStats & cpu_stats = organism->CPUStats();
   cCPUMemory & child_genome = organism->ChildGenome();
@@ -1549,14 +1549,14 @@
   // Divide Mutations
   if (organism->TestDivideMut()) {
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize());
-    child_genome[mut_line] = m_inst_set->GetRandomInst();
+    child_genome[mut_line] = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.divide_mut_count++;
   }
   
   // Divide Insertions
   if (organism->TestDivideIns() && child_genome.GetSize() < MAX_CREATURE_SIZE){
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize() + 1);
-    child_genome.Insert(mut_line, m_inst_set->GetRandomInst());
+    child_genome.Insert(mut_line, m_inst_set->GetRandomInst(ctx));
     cpu_stats.mut_stats.divide_insert_mut_count++;
   }
   
@@ -1575,7 +1575,7 @@
     if( num_mut > 0 ){
       for (int i = 0; i < num_mut; i++) {
         int site = m_world->GetRandom().GetUInt(child_genome.GetSize());
-        child_genome[site] = m_inst_set->GetRandomInst();
+        child_genome[site] = m_inst_set->GetRandomInst(ctx);
         cpu_stats.mut_stats.div_mut_count++;
       }
     }
@@ -1601,7 +1601,7 @@
       qsort( (void*)mut_sites, num_mut, sizeof(int), &IntCompareFunction );
       // Actually do the mutations (in reverse sort order)
       for(int i = num_mut-1; i >= 0; i--) {
-        child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst());
+        child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx));
         cpu_stats.mut_stats.insert_mut_count++;
       }
     }
@@ -1629,7 +1629,7 @@
   if (organism->GetParentMutProb() > 0) {
     for (int i = 0; i < GetMemory().GetSize(); i++) {
       if (organism->TestParentMut()) {
-        GetMemory()[i] = m_inst_set->GetRandomInst();
+        GetMemory()[i] = m_inst_set->GetRandomInst(ctx);
         cpu_stats.mut_stats.parent_mut_line_count++;
       }
     }
@@ -1651,7 +1651,7 @@
 
 
 // test whether the offspring creature contains an advantageous mutation.
-void cHardwareCPU::Divide_TestFitnessMeasures()
+void cHardwareCPU::Divide_TestFitnessMeasures(cAvidaContext& ctx)
 {
   cPhenotype & phenotype = organism->GetPhenotype();
   phenotype.CopyTrue() = ( organism->ChildGenome() == organism->GetGenome() );
@@ -1673,7 +1673,7 @@
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   cCPUTestInfo test_info;
   test_info.UseRandomInputs();
-  testcpu->TestGenome(test_info, organism->ChildGenome());
+  testcpu->TestGenome(ctx, test_info, organism->ChildGenome());
   const double child_fitness = test_info.GetGenotypeFitness();
   delete testcpu;
   
@@ -1712,12 +1712,13 @@
 }
 
 
-bool cHardwareCPU::Divide_Main(const int div_point, const int extra_lines, double mut_multiplier)
+bool cHardwareCPU::Divide_Main(cAvidaContext& ctx, const int div_point,
+                               const int extra_lines, double mut_multiplier)
 {
   const int child_size = GetMemory().GetSize() - div_point - extra_lines;
   
   // Make sure this divide will produce a viable offspring.
-  const bool viable = Divide_CheckViable(child_size, div_point);
+  const bool viable = Divide_CheckViable(ctx, child_size, div_point);
   if (viable == false) return false;
   
   // Since the divide will now succeed, set up the information to be sent
@@ -1729,12 +1730,12 @@
   GetMemory().Resize(div_point);
   
   // Handle Divide Mutations...
-  Divide_DoMutations(mut_multiplier);
+  Divide_DoMutations(ctx, mut_multiplier);
   
   // Many tests will require us to run the offspring through a test CPU;
   // this is, for example, to see if mutations need to be reverted or if
   // lineages need to be updated.
-  Divide_TestFitnessMeasures();
+  Divide_TestFitnessMeasures(ctx);
   
 #ifdef INSTRUCTION_COSTS
   // reset first time instruction costs
@@ -1763,21 +1764,21 @@
 // And the instructions...
 //////////////////////////
 
-bool cHardwareCPU::Inst_If0()          // Execute next if ?bx? ==0.
+bool cHardwareCPU::Inst_If0(cAvidaContext& ctx)          // Execute next if ?bx? ==0.
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) != 0)  IP().Advance();
   return true; 
 }
 
-bool cHardwareCPU::Inst_IfNot0()       // Execute next if ?bx? != 0.
+bool cHardwareCPU::Inst_IfNot0(cAvidaContext& ctx)       // Execute next if ?bx? != 0.
 { 
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) == 0)  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfEqu()      // Execute next if bx == ?cx?
+bool cHardwareCPU::Inst_IfEqu(cAvidaContext& ctx)      // Execute next if bx == ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1785,7 +1786,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfNEqu()     // Execute next if bx != ?cx?
+bool cHardwareCPU::Inst_IfNEqu(cAvidaContext& ctx)     // Execute next if bx != ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1793,14 +1794,14 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfGr0()       // Execute next if ?bx? ! < 0.
+bool cHardwareCPU::Inst_IfGr0(cAvidaContext& ctx)       // Execute next if ?bx? ! < 0.
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) <= 0)  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfGr()       // Execute next if bx > ?cx?
+bool cHardwareCPU::Inst_IfGr(cAvidaContext& ctx)       // Execute next if bx > ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1808,14 +1809,14 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfGrEqu0()       // Execute next if ?bx? != 0.
+bool cHardwareCPU::Inst_IfGrEqu0(cAvidaContext& ctx)       // Execute next if ?bx? != 0.
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) < 0)  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfGrEqu()       // Execute next if bx > ?cx?
+bool cHardwareCPU::Inst_IfGrEqu(cAvidaContext& ctx)       // Execute next if bx > ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1823,14 +1824,14 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfLess0()       // Execute next if ?bx? != 0.
+bool cHardwareCPU::Inst_IfLess0(cAvidaContext& ctx)       // Execute next if ?bx? != 0.
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) >= 0)  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfLess()       // Execute next if ?bx? < ?cx?
+bool cHardwareCPU::Inst_IfLess(cAvidaContext& ctx)       // Execute next if ?bx? < ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1838,14 +1839,14 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfLsEqu0()       // Execute next if ?bx? != 0.
+bool cHardwareCPU::Inst_IfLsEqu0(cAvidaContext& ctx)       // Execute next if ?bx? != 0.
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(reg_used) > 0) IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfLsEqu()       // Execute next if bx > ?cx?
+bool cHardwareCPU::Inst_IfLsEqu(cAvidaContext& ctx)       // Execute next if bx > ?cx?
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int reg_used2 = FindComplementRegister(reg_used);
@@ -1853,32 +1854,32 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfBit1()
+bool cHardwareCPU::Inst_IfBit1(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if ((GetRegister(reg_used) & 1) == 0)  IP().Advance();
 return true;
 }
 
-bool cHardwareCPU::Inst_IfANotEqB()     // Execute next if AX != BX
+bool cHardwareCPU::Inst_IfANotEqB(cAvidaContext& ctx)     // Execute next if AX != BX
 {
   if (GetRegister(nHardwareCPU::REG_AX) == GetRegister(nHardwareCPU::REG_BX) )  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfBNotEqC()     // Execute next if BX != CX
+bool cHardwareCPU::Inst_IfBNotEqC(cAvidaContext& ctx)     // Execute next if BX != CX
 {
   if (GetRegister(nHardwareCPU::REG_BX) == GetRegister(nHardwareCPU::REG_CX) )  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfANotEqC()     // Execute next if AX != BX
+bool cHardwareCPU::Inst_IfANotEqC(cAvidaContext& ctx)     // Execute next if AX != BX
 {
   if (GetRegister(nHardwareCPU::REG_AX) == GetRegister(nHardwareCPU::REG_CX) )  IP().Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_JumpF()
+bool cHardwareCPU::Inst_JumpF(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -1903,7 +1904,7 @@
 }
 
 
-bool cHardwareCPU::Inst_JumpB()
+bool cHardwareCPU::Inst_JumpB(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -1927,7 +1928,7 @@
   return false;
 }
 
-bool cHardwareCPU::Inst_JumpP()
+bool cHardwareCPU::Inst_JumpP(cAvidaContext& ctx)
 {
   cOrganism * other_organism = organism->GetNeighbor();
   
@@ -1972,7 +1973,7 @@
   return false;
 }
 
-bool cHardwareCPU::Inst_JumpSelf()
+bool cHardwareCPU::Inst_JumpSelf(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -1996,7 +1997,7 @@
   return false;
 }
 
-bool cHardwareCPU::Inst_Call()
+bool cHardwareCPU::Inst_Call(cAvidaContext& ctx)
 {
   // Put the starting location onto the stack
   const int location = IP().GetPosition();
@@ -2023,34 +2024,34 @@
   return false;
 }
 
-bool cHardwareCPU::Inst_Return()
+bool cHardwareCPU::Inst_Return(cAvidaContext& ctx)
 {
   IP().Set(StackPop());
   return true;
 }
 
-bool cHardwareCPU::Inst_Pop()
+bool cHardwareCPU::Inst_Pop(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = StackPop();
   return true;
 }
 
-bool cHardwareCPU::Inst_Push()
+bool cHardwareCPU::Inst_Push(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   StackPush(GetRegister(reg_used));
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadPop()
+bool cHardwareCPU::Inst_HeadPop(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetHead(head_used).Set(StackPop(), this);
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadPush()
+bool cHardwareCPU::Inst_HeadPush(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   StackPush(GetHead(head_used).GetPosition());
@@ -2062,18 +2063,18 @@
 }
 
 
-bool cHardwareCPU::Inst_PopA() { GetRegister(nHardwareCPU::REG_AX) = StackPop(); return true;}
-bool cHardwareCPU::Inst_PopB() { GetRegister(nHardwareCPU::REG_BX) = StackPop(); return true;}
-bool cHardwareCPU::Inst_PopC() { GetRegister(nHardwareCPU::REG_CX) = StackPop(); return true;}
+bool cHardwareCPU::Inst_PopA(cAvidaContext& ctx) { GetRegister(nHardwareCPU::REG_AX) = StackPop(); return true;}
+bool cHardwareCPU::Inst_PopB(cAvidaContext& ctx) { GetRegister(nHardwareCPU::REG_BX) = StackPop(); return true;}
+bool cHardwareCPU::Inst_PopC(cAvidaContext& ctx) { GetRegister(nHardwareCPU::REG_CX) = StackPop(); return true;}
 
-bool cHardwareCPU::Inst_PushA() { StackPush(GetRegister(nHardwareCPU::REG_AX)); return true;}
-bool cHardwareCPU::Inst_PushB() { StackPush(GetRegister(nHardwareCPU::REG_BX)); return true;}
-bool cHardwareCPU::Inst_PushC() { StackPush(GetRegister(nHardwareCPU::REG_CX)); return true;}
+bool cHardwareCPU::Inst_PushA(cAvidaContext& ctx) { StackPush(GetRegister(nHardwareCPU::REG_AX)); return true;}
+bool cHardwareCPU::Inst_PushB(cAvidaContext& ctx) { StackPush(GetRegister(nHardwareCPU::REG_BX)); return true;}
+bool cHardwareCPU::Inst_PushC(cAvidaContext& ctx) { StackPush(GetRegister(nHardwareCPU::REG_CX)); return true;}
 
-bool cHardwareCPU::Inst_SwitchStack() { SwitchStack(); return true;}
-bool cHardwareCPU::Inst_FlipStack()   { StackFlip(); return true;}
+bool cHardwareCPU::Inst_SwitchStack(cAvidaContext& ctx) { SwitchStack(); return true;}
+bool cHardwareCPU::Inst_FlipStack(cAvidaContext& ctx)   { StackFlip(); return true;}
 
-bool cHardwareCPU::Inst_Swap()
+bool cHardwareCPU::Inst_Swap(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int other_reg = FindComplementRegister(reg_used);
@@ -2081,11 +2082,20 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_SwapAB() { nFunctions::Swap(GetRegister(nHardwareCPU::REG_AX), GetRegister(nHardwareCPU::REG_BX)); return true; }
-bool cHardwareCPU::Inst_SwapBC() { nFunctions::Swap(GetRegister(nHardwareCPU::REG_BX), GetRegister(nHardwareCPU::REG_CX)); return true; }
-bool cHardwareCPU::Inst_SwapAC() { nFunctions::Swap(GetRegister(nHardwareCPU::REG_AX), GetRegister(nHardwareCPU::REG_CX)); return true; }
+bool cHardwareCPU::Inst_SwapAB(cAvidaContext& ctx)\
+{
+  nFunctions::Swap(GetRegister(nHardwareCPU::REG_AX), GetRegister(nHardwareCPU::REG_BX)); return true;
+}
+bool cHardwareCPU::Inst_SwapBC(cAvidaContext& ctx)
+{
+  nFunctions::Swap(GetRegister(nHardwareCPU::REG_BX), GetRegister(nHardwareCPU::REG_CX)); return true;
+}
+bool cHardwareCPU::Inst_SwapAC(cAvidaContext& ctx)
+{
+  nFunctions::Swap(GetRegister(nHardwareCPU::REG_AX), GetRegister(nHardwareCPU::REG_CX)); return true;
+}
 
-bool cHardwareCPU::Inst_CopyReg()
+bool cHardwareCPU::Inst_CopyReg(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int other_reg = FindComplementRegister(reg_used);
@@ -2093,20 +2103,32 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_CopyRegAB() {GetRegister(nHardwareCPU::REG_AX) = GetRegister(nHardwareCPU::REG_BX);   return true;
+bool cHardwareCPU::Inst_CopyRegAB(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_AX) = GetRegister(nHardwareCPU::REG_BX);   return true;
 }
-bool cHardwareCPU::Inst_CopyRegAC() {GetRegister(nHardwareCPU::REG_AX) = GetRegister(nHardwareCPU::REG_CX);   return true;
+bool cHardwareCPU::Inst_CopyRegAC(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_AX) = GetRegister(nHardwareCPU::REG_CX);   return true;
 }
-bool cHardwareCPU::Inst_CopyRegBA() {GetRegister(nHardwareCPU::REG_BX) = GetRegister(nHardwareCPU::REG_AX);   return true;
+bool cHardwareCPU::Inst_CopyRegBA(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_BX) = GetRegister(nHardwareCPU::REG_AX);   return true;
 }
-bool cHardwareCPU::Inst_CopyRegBC() {GetRegister(nHardwareCPU::REG_BX) = GetRegister(nHardwareCPU::REG_CX);   return true;
+bool cHardwareCPU::Inst_CopyRegBC(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_BX) = GetRegister(nHardwareCPU::REG_CX);   return true;
 }
-bool cHardwareCPU::Inst_CopyRegCA() {GetRegister(nHardwareCPU::REG_CX) = GetRegister(nHardwareCPU::REG_AX);   return true;
+bool cHardwareCPU::Inst_CopyRegCA(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_CX) = GetRegister(nHardwareCPU::REG_AX);   return true;
 }
-bool cHardwareCPU::Inst_CopyRegCB() {GetRegister(nHardwareCPU::REG_CX) = GetRegister(nHardwareCPU::REG_BX);   return true;
+bool cHardwareCPU::Inst_CopyRegCB(cAvidaContext& ctx)
+{
+  GetRegister(nHardwareCPU::REG_CX) = GetRegister(nHardwareCPU::REG_BX);   return true;
 }
 
-bool cHardwareCPU::Inst_Reset()
+bool cHardwareCPU::Inst_Reset(cAvidaContext& ctx)
 {
   GetRegister(nHardwareCPU::REG_AX) = 0;
   GetRegister(nHardwareCPU::REG_BX) = 0;
@@ -2115,100 +2137,100 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_ShiftR()
+bool cHardwareCPU::Inst_ShiftR(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) >>= 1;
   return true;
 }
 
-bool cHardwareCPU::Inst_ShiftL()
+bool cHardwareCPU::Inst_ShiftL(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) <<= 1;
   return true;
 }
 
-bool cHardwareCPU::Inst_Bit1()
+bool cHardwareCPU::Inst_Bit1(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) |=  1;
   return true;
 }
 
-bool cHardwareCPU::Inst_SetNum()
+bool cHardwareCPU::Inst_SetNum(cAvidaContext& ctx)
 {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsInt(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_ValGrey(void) {
+bool cHardwareCPU::Inst_ValGrey(cAvidaContext& ctx) {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsIntGreyCode(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_ValDir(void) {
+bool cHardwareCPU::Inst_ValDir(cAvidaContext& ctx) {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsIntDirect(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_ValAddP(void) {
+bool cHardwareCPU::Inst_ValAddP(cAvidaContext& ctx) {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsIntAdditivePolynomial(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_ValFib(void) {
+bool cHardwareCPU::Inst_ValFib(cAvidaContext& ctx) {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsIntFib(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_ValPolyC(void) {
+bool cHardwareCPU::Inst_ValPolyC(cAvidaContext& ctx) {
   ReadLabel();
   GetRegister(nHardwareCPU::REG_BX) = GetLabel().AsIntPolynomialCoefficent(nHardwareCPU::NUM_NOPS);
   return true;
 }
 
-bool cHardwareCPU::Inst_Inc()
+bool cHardwareCPU::Inst_Inc(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) += 1;
   return true;
 }
 
-bool cHardwareCPU::Inst_Dec()
+bool cHardwareCPU::Inst_Dec(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) -= 1;
   return true;
 }
 
-bool cHardwareCPU::Inst_Zero()
+bool cHardwareCPU::Inst_Zero(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = 0;
   return true;
 }
 
-bool cHardwareCPU::Inst_Neg()
+bool cHardwareCPU::Inst_Neg(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = 0 - GetRegister(reg_used);
   return true;
 }
 
-bool cHardwareCPU::Inst_Square()
+bool cHardwareCPU::Inst_Square(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetRegister(reg_used) * GetRegister(reg_used);
   return true;
 }
 
-bool cHardwareCPU::Inst_Sqrt()
+bool cHardwareCPU::Inst_Sqrt(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int value = GetRegister(reg_used);
@@ -2220,7 +2242,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Log()
+bool cHardwareCPU::Inst_Log(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int value = GetRegister(reg_used);
@@ -2232,7 +2254,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Log10()
+bool cHardwareCPU::Inst_Log10(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int value = GetRegister(reg_used);
@@ -2244,35 +2266,35 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Minus17()
+bool cHardwareCPU::Inst_Minus17(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) -= 17;
   return true;
 }
 
-bool cHardwareCPU::Inst_Add()
+bool cHardwareCPU::Inst_Add(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetRegister(nHardwareCPU::REG_BX) + GetRegister(nHardwareCPU::REG_CX);
   return true;
 }
 
-bool cHardwareCPU::Inst_Sub()
+bool cHardwareCPU::Inst_Sub(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetRegister(nHardwareCPU::REG_BX) - GetRegister(nHardwareCPU::REG_CX);
   return true;
 }
 
-bool cHardwareCPU::Inst_Mult()
+bool cHardwareCPU::Inst_Mult(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetRegister(nHardwareCPU::REG_BX) * GetRegister(nHardwareCPU::REG_CX);
   return true;
 }
 
-bool cHardwareCPU::Inst_Div()
+bool cHardwareCPU::Inst_Div(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(nHardwareCPU::REG_CX) != 0) {
@@ -2287,7 +2309,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Mod()
+bool cHardwareCPU::Inst_Mod(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   if (GetRegister(nHardwareCPU::REG_CX) != 0) {
@@ -2300,57 +2322,57 @@
 }
 
 
-bool cHardwareCPU::Inst_Nand()
+bool cHardwareCPU::Inst_Nand(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = ~(GetRegister(nHardwareCPU::REG_BX) & GetRegister(nHardwareCPU::REG_CX));
   return true;
 }
 
-bool cHardwareCPU::Inst_Nor()
+bool cHardwareCPU::Inst_Nor(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = ~(GetRegister(nHardwareCPU::REG_BX) | GetRegister(nHardwareCPU::REG_CX));
   return true;
 }
 
-bool cHardwareCPU::Inst_And()
+bool cHardwareCPU::Inst_And(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = (GetRegister(nHardwareCPU::REG_BX) & GetRegister(nHardwareCPU::REG_CX));
   return true;
 }
 
-bool cHardwareCPU::Inst_Not()
+bool cHardwareCPU::Inst_Not(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = ~(GetRegister(reg_used));
   return true;
 }
 
-bool cHardwareCPU::Inst_Order()
+bool cHardwareCPU::Inst_Order(cAvidaContext& ctx)
 {
   if (GetRegister(nHardwareCPU::REG_BX) > GetRegister(nHardwareCPU::REG_CX)) {
     nFunctions::Swap(GetRegister(nHardwareCPU::REG_BX), GetRegister(nHardwareCPU::REG_CX));
   }
-return true;
+  return true;
 }
 
-bool cHardwareCPU::Inst_Xor()
+bool cHardwareCPU::Inst_Xor(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetRegister(nHardwareCPU::REG_BX) ^ GetRegister(nHardwareCPU::REG_CX);
   return true;
 }
 
-bool cHardwareCPU::Inst_Copy()
+bool cHardwareCPU::Inst_Copy(cAvidaContext& ctx)
 {
   const cHeadCPU from(this, GetRegister(nHardwareCPU::REG_BX));
   cHeadCPU to(this, GetRegister(nHardwareCPU::REG_AX) + GetRegister(nHardwareCPU::REG_BX));
   sCPUStats & cpu_stats = organism->CPUStats();
   
   if (organism->TestCopyMut()) {
-    to.SetInst(m_inst_set->GetRandomInst());
+    to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();  // Mark this instruction as mutated...
     to.SetFlagCopyMut();  // Mark this instruction as copy mut...
                               //organism->GetPhenotype().IsMutated() = true;
@@ -2366,7 +2388,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_ReadInst()
+bool cHardwareCPU::Inst_ReadInst(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   const cHeadCPU from(this,GetRegister(nHardwareCPU::REG_BX));
@@ -2377,32 +2399,32 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_WriteInst()
+bool cHardwareCPU::Inst_WriteInst(cAvidaContext& ctx)
 {
   cHeadCPU to(this, GetRegister(nHardwareCPU::REG_AX) + GetRegister(nHardwareCPU::REG_BX));
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   const int value = Mod(GetRegister(reg_used), m_inst_set->GetSize());
-sCPUStats & cpu_stats = organism->CPUStats();
+  sCPUStats & cpu_stats = organism->CPUStats();
 
-// Change value on a mutation...
-if (organism->TestCopyMut()) {
-  to.SetInst(m_inst_set->GetRandomInst());
-  to.SetFlagMutated();      // Mark this instruction as mutated...
-  to.SetFlagCopyMut();      // Mark this instruction as copy mut...
-                                //organism->GetPhenotype().IsMutated() = true;
-  cpu_stats.mut_stats.copy_mut_count++;
-} else {
-  to.SetInst(cInstruction(value));
-  to.ClearFlagMutated();     // UnMark
-  to.ClearFlagCopyMut();     // UnMark
-}
+  // Change value on a mutation...
+  if (organism->TestCopyMut()) {
+    to.SetInst(m_inst_set->GetRandomInst(ctx));
+    to.SetFlagMutated();      // Mark this instruction as mutated...
+    to.SetFlagCopyMut();      // Mark this instruction as copy mut...
+                                  //organism->GetPhenotype().IsMutated() = true;
+    cpu_stats.mut_stats.copy_mut_count++;
+  } else {
+    to.SetInst(cInstruction(value));
+    to.ClearFlagMutated();     // UnMark
+    to.ClearFlagCopyMut();     // UnMark
+  }
 
-to.SetFlagCopied();  // Set the copied flag.
-cpu_stats.mut_stats.copies_exec++;
-return true;
+  to.SetFlagCopied();  // Set the copied flag.
+  cpu_stats.mut_stats.copies_exec++;
+  return true;
 }
 
-bool cHardwareCPU::Inst_StackReadInst()
+bool cHardwareCPU::Inst_StackReadInst(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   cHeadCPU from(this, GetRegister(reg_used));
@@ -2410,7 +2432,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_StackWriteInst()
+bool cHardwareCPU::Inst_StackWriteInst(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   cHeadCPU to(this, GetRegister(nHardwareCPU::REG_AX) + GetRegister(reg_used));
@@ -2419,7 +2441,7 @@
   
   // Change value on a mutation...
   if (organism->TestCopyMut()) {
-    to.SetInst(m_inst_set->GetRandomInst());
+    to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();      // Mark this instruction as mutated...
     to.SetFlagCopyMut();      // Mark this instruction as copy mut...
                                   //organism->GetPhenotype().IsMutated() = true;
@@ -2435,7 +2457,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Compare()
+bool cHardwareCPU::Inst_Compare(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   cHeadCPU from(this, GetRegister(nHardwareCPU::REG_BX));
@@ -2443,7 +2465,7 @@
   
   // Compare is dangerous -- it can cause mutations!
   if (organism->TestCopyMut()) {
-    to.SetInst(m_inst_set->GetRandomInst());
+    to.SetInst(m_inst_set->GetRandomInst(ctx));
     to.SetFlagMutated();      // Mark this instruction as mutated...
     to.SetFlagCopyMut();      // Mark this instruction as copy mut...
                                   //organism->GetPhenotype().IsMutated() = true;
@@ -2454,7 +2476,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_IfNCpy()
+bool cHardwareCPU::Inst_IfNCpy(cAvidaContext& ctx)
 {
   const cHeadCPU from(this, GetRegister(nHardwareCPU::REG_BX));
   const cHeadCPU to(this, GetRegister(nHardwareCPU::REG_AX) + GetRegister(nHardwareCPU::REG_BX));
@@ -2468,43 +2490,43 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Allocate()   // Allocate bx more space...
+bool cHardwareCPU::Inst_Allocate(cAvidaContext& ctx)   // Allocate bx more space...
 {
   const int size = GetMemory().GetSize();
-  if( Allocate_Main(GetRegister(nHardwareCPU::REG_BX)) ) {
+  if( Allocate_Main(ctx, GetRegister(nHardwareCPU::REG_BX)) ) {
   GetRegister(nHardwareCPU::REG_AX) = size;
   return true;
 } else return false;
 }
 
-bool cHardwareCPU::Inst_Divide()  
+bool cHardwareCPU::Inst_Divide(cAvidaContext& ctx)  
 { 
-  return Divide_Main(GetRegister(nHardwareCPU::REG_AX));    
+  return Divide_Main(ctx, GetRegister(nHardwareCPU::REG_AX));    
 }
 
-bool cHardwareCPU::Inst_CDivide() 
+bool cHardwareCPU::Inst_CDivide(cAvidaContext& ctx) 
 { 
-  return Divide_Main(GetMemory().GetSize() / 2);   
+  return Divide_Main(ctx, GetMemory().GetSize() / 2);   
 }
 
-bool cHardwareCPU::Inst_CAlloc()  
+bool cHardwareCPU::Inst_CAlloc(cAvidaContext& ctx)  
 { 
-  return Allocate_Main(GetMemory().GetSize());   
+  return Allocate_Main(ctx, GetMemory().GetSize());   
 }
 
-bool cHardwareCPU::Inst_MaxAlloc()   // Allocate maximal more
+bool cHardwareCPU::Inst_MaxAlloc(cAvidaContext& ctx)   // Allocate maximal more
 {
   const int cur_size = GetMemory().GetSize();
   const int alloc_size = Min((int) (m_world->GetConfig().CHILD_SIZE_RANGE.Get() * cur_size),
                              MAX_CREATURE_SIZE - cur_size);
-  if( Allocate_Main(alloc_size) ) {
+  if( Allocate_Main(ctx, alloc_size) ) {
     GetRegister(nHardwareCPU::REG_AX) = cur_size;
     return true;
   } else return false;
 }
 
 
-bool cHardwareCPU::Inst_Repro()
+bool cHardwareCPU::Inst_Repro(cAvidaContext& ctx)
 {
   // Setup child
   cCPUMemory & child_genome = organism->ChildGenome();
@@ -2521,17 +2543,17 @@
   if (organism->GetCopyMutProb() > 0) { // Skip this if no mutations....
     for (int i = 0; i < GetMemory().GetSize(); i++) {
       if (organism->TestCopyMut()) {
-        child_genome[i] = m_inst_set->GetRandomInst();
+        child_genome[i] = m_inst_set->GetRandomInst(ctx);
         //organism->GetPhenotype().IsMutated() = true;
       }
     }
   }
-  Divide_DoMutations();
+  Divide_DoMutations(ctx);
   
   // Many tests will require us to run the offspring through a test CPU;
   // this is, for example, to see if mutations need to be reverted or if
   // lineages need to be updated.
-  Divide_TestFitnessMeasures();
+  Divide_TestFitnessMeasures(ctx);
   
 #ifdef INSTRUCTION_COSTS
   // reset first time instruction costs
@@ -2548,7 +2570,7 @@
 }
 
 
-bool cHardwareCPU::Inst_Kazi()
+bool cHardwareCPU::Inst_Kazi(cAvidaContext& ctx)
 {
 	const int reg_used = FindModifiedRegister(nHardwareCPU::REG_AX);
 	int percentProb = GetRegister(reg_used) % 100;
@@ -2564,7 +2586,7 @@
 	}
 }
 
-bool cHardwareCPU::Inst_Die()
+bool cHardwareCPU::Inst_Die(cAvidaContext& ctx)
 {
   const double die_prob = m_world->GetConfig().DIE_PROB.Get();
   if(m_world->GetRandom().GetDouble() < die_prob) { organism->Die(); }
@@ -2579,7 +2601,7 @@
 // It will then look at the template that follows the command and inject it
 // into the complement template found in a neighboring organism.
 
-bool cHardwareCPU::Inst_Inject()
+bool cHardwareCPU::Inst_Inject(cAvidaContext& ctx)
 {
   AdjustHeads();
   const int start_pos = GetHead(nHardware::HEAD_READ).GetPosition();
@@ -2629,12 +2651,12 @@
 }
 
 
-bool cHardwareCPU::Inst_InjectRand()
+bool cHardwareCPU::Inst_InjectRand(cAvidaContext& ctx)
 {
   // Rotate to a random facing and then run the normal inject instruction
   const int num_neighbors = organism->GetNeighborhoodSize();
   organism->Rotate(m_world->GetRandom().GetUInt(num_neighbors));
-  Inst_Inject();
+  Inst_Inject(ctx);
   return true;
 }
 
@@ -2646,7 +2668,7 @@
 // It will then look at the template that follows the command and inject it
 // into the complement template found in a neighboring organism.
 
-bool cHardwareCPU::Inst_InjectThread()
+bool cHardwareCPU::Inst_InjectThread(cAvidaContext& ctx)
 {
   AdjustHeads();
   const int start_pos = GetHead(nHardware::HEAD_READ).GetPosition();
@@ -2700,7 +2722,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_TaskGet()
+bool cHardwareCPU::Inst_TaskGet(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   const int value = organism->GetNextInput();
@@ -2709,7 +2731,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_TaskStackGet()
+bool cHardwareCPU::Inst_TaskStackGet(cAvidaContext& ctx)
 {
   const int value = organism->GetNextInput();
   StackPush(value);
@@ -2717,29 +2739,29 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_TaskStackLoad()
+bool cHardwareCPU::Inst_TaskStackLoad(cAvidaContext& ctx)
 {
   for (int i = 0; i < nHardware::IO_SIZE; i++) 
     StackPush( organism->GetNextInput() );
   return true;
 }
 
-bool cHardwareCPU::Inst_TaskPut()
+bool cHardwareCPU::Inst_TaskPut(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int value = GetRegister(reg_used);
   GetRegister(reg_used) = 0;
-  organism->DoOutput(value);
+  organism->DoOutput(ctx, value);
   return true;
 }
 
-bool cHardwareCPU::Inst_TaskIO()
+bool cHardwareCPU::Inst_TaskIO(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   
   // Do the "put" component
   const int value_out = GetRegister(reg_used);
-  organism->DoOutput(value_out);  // Check for tasks compleated.
+  organism->DoOutput(ctx, value_out);  // Check for tasks compleated.
   
   // Do the "get" component
   const int value_in = organism->GetNextInput();
@@ -2748,7 +2770,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_Send()
+bool cHardwareCPU::Inst_Send(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   organism->SendValue(GetRegister(reg_used));
@@ -2756,14 +2778,14 @@
 return true;
 }
 
-bool cHardwareCPU::Inst_Receive()
+bool cHardwareCPU::Inst_Receive(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = organism->ReceiveValue();
   return true;
 }
 
-bool cHardwareCPU::Inst_Sense()
+bool cHardwareCPU::Inst_Sense(cAvidaContext& ctx)
 {
   const tArray<double> & res_count = organism->GetOrgInterface().GetResources();
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
@@ -2800,7 +2822,7 @@
   to_org->UpdateMerit(other_merit);
 }
 
-bool cHardwareCPU::Inst_DonateRandom()
+bool cHardwareCPU::Inst_DonateRandom(cAvidaContext& ctx)
 {
   organism->GetPhenotype().IncDonates();
   if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
@@ -2820,7 +2842,7 @@
 }
 
 
-bool cHardwareCPU::Inst_DonateKin()
+bool cHardwareCPU::Inst_DonateKin(cAvidaContext& ctx)
 {
   organism->GetPhenotype().IncDonates();
   if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
@@ -2863,7 +2885,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_DonateEditDist()
+bool cHardwareCPU::Inst_DonateEditDist(cAvidaContext& ctx)
 {
   organism->GetPhenotype().IncDonates();
   if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
@@ -2910,7 +2932,7 @@
 }
 
 
-bool cHardwareCPU::Inst_DonateNULL()
+bool cHardwareCPU::Inst_DonateNULL(cAvidaContext& ctx)
 {
   organism->GetPhenotype().IncDonates();
   if (organism->GetPhenotype().GetCurNumDonates() > m_world->GetConfig().MAX_DONATES.Get()) {
@@ -2931,7 +2953,7 @@
 }
 
 
-bool cHardwareCPU::Inst_SearchF()
+bool cHardwareCPU::Inst_SearchF(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -2941,7 +2963,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_SearchB()
+bool cHardwareCPU::Inst_SearchB(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -2951,14 +2973,14 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_MemSize()
+bool cHardwareCPU::Inst_MemSize(cAvidaContext& ctx)
 {
   GetRegister(FindModifiedRegister(nHardwareCPU::REG_BX)) = GetMemory().GetSize();
   return true;
 }
 
 
-bool cHardwareCPU::Inst_RotateL()
+bool cHardwareCPU::Inst_RotateL(cAvidaContext& ctx)
 {
   const int num_neighbors = organism->GetNeighborhoodSize();
   
@@ -2994,7 +3016,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_RotateR()
+bool cHardwareCPU::Inst_RotateR(cAvidaContext& ctx)
 {
   const int num_neighbors = organism->GetNeighborhoodSize();
   
@@ -3030,7 +3052,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_SetCopyMut()
+bool cHardwareCPU::Inst_SetCopyMut(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const int new_mut_rate = Max(GetRegister(reg_used), 1 );
@@ -3038,7 +3060,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_ModCopyMut()
+bool cHardwareCPU::Inst_ModCopyMut(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   const double new_mut_rate = organism->GetCopyMutProb() +
@@ -3050,21 +3072,21 @@
 
 // Multi-threading.
 
-bool cHardwareCPU::Inst_ForkThread()
+bool cHardwareCPU::Inst_ForkThread(cAvidaContext& ctx)
 {
   IP().Advance();
   if (!ForkThread()) Fault(FAULT_LOC_THREAD_FORK, FAULT_TYPE_FORK_TH);
   return true;
 }
 
-bool cHardwareCPU::Inst_KillThread()
+bool cHardwareCPU::Inst_KillThread(cAvidaContext& ctx)
 {
   if (!KillThread()) Fault(FAULT_LOC_THREAD_KILL, FAULT_TYPE_KILL_TH);
   else advance_ip = false;
   return true;
 }
 
-bool cHardwareCPU::Inst_ThreadID()
+bool cHardwareCPU::Inst_ThreadID(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_BX);
   GetRegister(reg_used) = GetCurThreadID();
@@ -3074,21 +3096,21 @@
 
 // Head-based instructions
 
-bool cHardwareCPU::Inst_SetHead()
+bool cHardwareCPU::Inst_SetHead(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   threads[cur_thread].cur_head = (unsigned char) head_used;
   return true;
 }
 
-bool cHardwareCPU::Inst_AdvanceHead()
+bool cHardwareCPU::Inst_AdvanceHead(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_WRITE);
   GetHead(head_used).Advance();
   return true;
 }
 
-bool cHardwareCPU::Inst_MoveHead()
+bool cHardwareCPU::Inst_MoveHead(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetHead(head_used).Set(GetHead(nHardware::HEAD_FLOW));
@@ -3096,21 +3118,21 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_JumpHead()
+bool cHardwareCPU::Inst_JumpHead(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetHead(head_used).Jump(GetRegister(nHardwareCPU::REG_CX) );
   return true;
 }
 
-bool cHardwareCPU::Inst_GetHead()
+bool cHardwareCPU::Inst_GetHead(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   GetRegister(nHardwareCPU::REG_CX) = GetHead(head_used).GetPosition();
   return true;
 }
 
-bool cHardwareCPU::Inst_IfLabel()
+bool cHardwareCPU::Inst_IfLabel(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -3120,7 +3142,7 @@
 
 // This is a variation on IfLabel that will skip the next command if the "if"
 // is false, but it will also skip all nops following that command.
-bool cHardwareCPU::Inst_IfLabel2()
+bool cHardwareCPU::Inst_IfLabel2(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -3131,46 +3153,46 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadDivideMut(double mut_multiplier)
+bool cHardwareCPU::Inst_HeadDivideMut(cAvidaContext& ctx, double mut_multiplier)
 {
   AdjustHeads();
   const int divide_pos = GetHead(nHardware::HEAD_READ).GetPosition();
   int child_end =  GetHead(nHardware::HEAD_WRITE).GetPosition();
   if (child_end == 0) child_end = GetMemory().GetSize();
   const int extra_lines = GetMemory().GetSize() - child_end;
-  bool ret_val = Divide_Main(divide_pos, extra_lines, mut_multiplier);
+  bool ret_val = Divide_Main(ctx, divide_pos, extra_lines, mut_multiplier);
   // Re-adjust heads.
   AdjustHeads();
   return ret_val; 
 }
 
-bool cHardwareCPU::Inst_HeadDivide()
+bool cHardwareCPU::Inst_HeadDivide(cAvidaContext& ctx)
 {
-  return Inst_HeadDivideMut(1);
+  return Inst_HeadDivideMut(ctx, 1);
 }
 
-bool cHardwareCPU::Inst_HeadDivideSex()  
+bool cHardwareCPU::Inst_HeadDivideSex(cAvidaContext& ctx)  
 { 
   organism->GetPhenotype().SetDivideSex(true);
   organism->GetPhenotype().SetCrossNum(1);
-  return Inst_HeadDivide(); 
+  return Inst_HeadDivide(ctx); 
 }
 
-bool cHardwareCPU::Inst_HeadDivideAsex()  
+bool cHardwareCPU::Inst_HeadDivideAsex(cAvidaContext& ctx)  
 { 
   organism->GetPhenotype().SetDivideSex(false);
   organism->GetPhenotype().SetCrossNum(0);
-  return Inst_HeadDivide(); 
+  return Inst_HeadDivide(ctx); 
 }
 
-bool cHardwareCPU::Inst_HeadDivideAsexWait()  
+bool cHardwareCPU::Inst_HeadDivideAsexWait(cAvidaContext& ctx)  
 { 
   organism->GetPhenotype().SetDivideSex(true);
   organism->GetPhenotype().SetCrossNum(0);
-  return Inst_HeadDivide(); 
+  return Inst_HeadDivide(ctx); 
 }
 
-bool cHardwareCPU::Inst_HeadDivideMateSelect()  
+bool cHardwareCPU::Inst_HeadDivideMateSelect(cAvidaContext& ctx)  
 { 
   // Take the label that follows this divide and use it as the ID for which
   // other organisms this one is willing to mate with.
@@ -3180,35 +3202,35 @@
   // Proceed as normal with the rest of mate selection.
   organism->GetPhenotype().SetDivideSex(true);
   organism->GetPhenotype().SetCrossNum(1);
-  return Inst_HeadDivide(); 
+  return Inst_HeadDivide(ctx); 
 }
 
-bool cHardwareCPU::Inst_HeadDivide1()  { return Inst_HeadDivideMut(1); }
-bool cHardwareCPU::Inst_HeadDivide2()  { return Inst_HeadDivideMut(2); }
-bool cHardwareCPU::Inst_HeadDivide3()  { return Inst_HeadDivideMut(3); }
-bool cHardwareCPU::Inst_HeadDivide4()  { return Inst_HeadDivideMut(4); }
-bool cHardwareCPU::Inst_HeadDivide5()  { return Inst_HeadDivideMut(5); }
-bool cHardwareCPU::Inst_HeadDivide6()  { return Inst_HeadDivideMut(6); }
-bool cHardwareCPU::Inst_HeadDivide7()  { return Inst_HeadDivideMut(7); }
-bool cHardwareCPU::Inst_HeadDivide8()  { return Inst_HeadDivideMut(8); }
-bool cHardwareCPU::Inst_HeadDivide9()  { return Inst_HeadDivideMut(9); }
-bool cHardwareCPU::Inst_HeadDivide10()  { return Inst_HeadDivideMut(10); }
-bool cHardwareCPU::Inst_HeadDivide16()  { return Inst_HeadDivideMut(16); }
-bool cHardwareCPU::Inst_HeadDivide32()  { return Inst_HeadDivideMut(32); }
-bool cHardwareCPU::Inst_HeadDivide50()  { return Inst_HeadDivideMut(50); }
-bool cHardwareCPU::Inst_HeadDivide100()  { return Inst_HeadDivideMut(100); }
-bool cHardwareCPU::Inst_HeadDivide500()  { return Inst_HeadDivideMut(500); }
-bool cHardwareCPU::Inst_HeadDivide1000()  { return Inst_HeadDivideMut(1000); }
-bool cHardwareCPU::Inst_HeadDivide5000()  { return Inst_HeadDivideMut(5000); }
-bool cHardwareCPU::Inst_HeadDivide10000()  { return Inst_HeadDivideMut(10000); }
-bool cHardwareCPU::Inst_HeadDivide50000()  { return Inst_HeadDivideMut(50000); }
-bool cHardwareCPU::Inst_HeadDivide0_5()  { return Inst_HeadDivideMut(0.5); }
-bool cHardwareCPU::Inst_HeadDivide0_1()  { return Inst_HeadDivideMut(0.1); }
-bool cHardwareCPU::Inst_HeadDivide0_05()  { return Inst_HeadDivideMut(0.05); }
-bool cHardwareCPU::Inst_HeadDivide0_01()  { return Inst_HeadDivideMut(0.01); }
-bool cHardwareCPU::Inst_HeadDivide0_001()  { return Inst_HeadDivideMut(0.001); }
+bool cHardwareCPU::Inst_HeadDivide1(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 1); }
+bool cHardwareCPU::Inst_HeadDivide2(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 2); }
+bool cHardwareCPU::Inst_HeadDivide3(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 3); }
+bool cHardwareCPU::Inst_HeadDivide4(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 4); }
+bool cHardwareCPU::Inst_HeadDivide5(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 5); }
+bool cHardwareCPU::Inst_HeadDivide6(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 6); }
+bool cHardwareCPU::Inst_HeadDivide7(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 7); }
+bool cHardwareCPU::Inst_HeadDivide8(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 8); }
+bool cHardwareCPU::Inst_HeadDivide9(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 9); }
+bool cHardwareCPU::Inst_HeadDivide10(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 10); }
+bool cHardwareCPU::Inst_HeadDivide16(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 16); }
+bool cHardwareCPU::Inst_HeadDivide32(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 32); }
+bool cHardwareCPU::Inst_HeadDivide50(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 50); }
+bool cHardwareCPU::Inst_HeadDivide100(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 100); }
+bool cHardwareCPU::Inst_HeadDivide500(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 500); }
+bool cHardwareCPU::Inst_HeadDivide1000(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 1000); }
+bool cHardwareCPU::Inst_HeadDivide5000(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 5000); }
+bool cHardwareCPU::Inst_HeadDivide10000(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 10000); }
+bool cHardwareCPU::Inst_HeadDivide50000(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 50000); }
+bool cHardwareCPU::Inst_HeadDivide0_5(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 0.5); }
+bool cHardwareCPU::Inst_HeadDivide0_1(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 0.1); }
+bool cHardwareCPU::Inst_HeadDivide0_05(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 0.05); }
+bool cHardwareCPU::Inst_HeadDivide0_01(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 0.01); }
+bool cHardwareCPU::Inst_HeadDivide0_001(cAvidaContext& ctx)  { return Inst_HeadDivideMut(ctx, 0.001); }
 
-bool cHardwareCPU::Inst_HeadRead()
+bool cHardwareCPU::Inst_HeadRead(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_READ);
   GetHead(head_id).Adjust();
@@ -3217,7 +3239,7 @@
   // Mutations only occur on the read, for the moment.
   int read_inst = 0;
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst().GetOp();
+    read_inst = m_inst_set->GetRandomInst(ctx).GetOp();
     cpu_stats.mut_stats.copy_mut_count++;  // @CAO, hope this is good!
   } else {
     read_inst = GetHead(head_id).GetInst().GetOp();
@@ -3230,7 +3252,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadWrite()
+bool cHardwareCPU::Inst_HeadWrite(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_WRITE);
   cHeadCPU & active_head = GetHead(head_id);
@@ -3248,7 +3270,7 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadCopy()
+bool cHardwareCPU::Inst_HeadCopy(cAvidaContext& ctx)
 {
   // For the moment, this cannot be nop-modified.
   cHeadCPU & read_head = GetHead(nHardware::HEAD_READ);
@@ -3264,7 +3286,7 @@
   cInstruction read_inst = read_head.GetInst();
   ReadInst(read_inst.GetOp());
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst();
+    read_inst = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.copy_mut_count++; 
     write_head.SetFlagMutated();
     write_head.SetFlagCopyMut();
@@ -3283,7 +3305,7 @@
   return true;
 }
 
-bool cHardwareCPU::HeadCopy_ErrorCorrect(double reduction)
+bool cHardwareCPU::HeadCopy_ErrorCorrect(cAvidaContext& ctx, double reduction)
 {
   // For the moment, this cannot be nop-modified.
   cHeadCPU & read_head = GetHead(nHardware::HEAD_READ);
@@ -3297,7 +3319,7 @@
   cInstruction read_inst = read_head.GetInst();
   ReadInst(read_inst.GetOp());
   if ( m_world->GetRandom().P(organism->GetCopyMutProb() / reduction) ) {
-    read_inst = m_inst_set->GetRandomInst();
+    read_inst = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.copy_mut_count++; 
     write_head.SetFlagMutated();
     write_head.SetFlagCopyMut();
@@ -3314,17 +3336,17 @@
   return true;
 }
 
-bool cHardwareCPU::Inst_HeadCopy2()  { return HeadCopy_ErrorCorrect(2); }
-bool cHardwareCPU::Inst_HeadCopy3()  { return HeadCopy_ErrorCorrect(3); }
-bool cHardwareCPU::Inst_HeadCopy4()  { return HeadCopy_ErrorCorrect(4); }
-bool cHardwareCPU::Inst_HeadCopy5()  { return HeadCopy_ErrorCorrect(5); }
-bool cHardwareCPU::Inst_HeadCopy6()  { return HeadCopy_ErrorCorrect(6); }
-bool cHardwareCPU::Inst_HeadCopy7()  { return HeadCopy_ErrorCorrect(7); }
-bool cHardwareCPU::Inst_HeadCopy8()  { return HeadCopy_ErrorCorrect(8); }
-bool cHardwareCPU::Inst_HeadCopy9()  { return HeadCopy_ErrorCorrect(9); }
-bool cHardwareCPU::Inst_HeadCopy10() { return HeadCopy_ErrorCorrect(10); }
+bool cHardwareCPU::Inst_HeadCopy2(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 2); }
+bool cHardwareCPU::Inst_HeadCopy3(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 3); }
+bool cHardwareCPU::Inst_HeadCopy4(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 4); }
+bool cHardwareCPU::Inst_HeadCopy5(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 5); }
+bool cHardwareCPU::Inst_HeadCopy6(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 6); }
+bool cHardwareCPU::Inst_HeadCopy7(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 7); }
+bool cHardwareCPU::Inst_HeadCopy8(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 8); }
+bool cHardwareCPU::Inst_HeadCopy9(cAvidaContext& ctx)  { return HeadCopy_ErrorCorrect(ctx, 9); }
+bool cHardwareCPU::Inst_HeadCopy10(cAvidaContext& ctx) { return HeadCopy_ErrorCorrect(ctx, 10); }
 
-bool cHardwareCPU::Inst_HeadSearch()
+bool cHardwareCPU::Inst_HeadSearch(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(1, nHardwareCPU::NUM_NOPS);
@@ -3337,7 +3359,7 @@
   return true; 
 }
 
-bool cHardwareCPU::Inst_SetFlow()
+bool cHardwareCPU::Inst_SetFlow(cAvidaContext& ctx)
 {
   const int reg_used = FindModifiedRegister(nHardwareCPU::REG_CX);
   GetHead(nHardware::HEAD_FLOW).Set(GetRegister(reg_used), this);
@@ -3346,22 +3368,9 @@
 
 
 //// Placebo insts ////
-bool cHardwareCPU::Inst_Skip()
+bool cHardwareCPU::Inst_Skip(cAvidaContext& ctx)
 {
   IP().Advance();
   return true;
 }
 
-
-/*
- FIXME: Breakage of interface. This function returns data that is
- supposed to be private.
- 
- I need to make complete snapshots of hardware state from Python.  If I
- have the time, I'll come back and fix the breakage.
- 
- @kgn
- */
-const tArray<cHardwareCPU_Thread> &cHardwareCPU::pyGetThreads(){
-  return threads;
-}

Modified: development/source/cpu/cHardwareCPU.h
===================================================================
--- development/source/cpu/cHardwareCPU.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareCPU.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -66,7 +66,7 @@
 class cHardwareCPU : public cHardwareBase
 {
 public:
-  typedef bool (cHardwareCPU::*tHardwareCPUMethod)();
+  typedef bool (cHardwareCPU::*tHardwareCPUMethod)(cAvidaContext& ctx);
 
 private:
   static cInstLibCPU* s_inst_slib;
@@ -103,8 +103,8 @@
   static cString GetDefaultInstFilename() { return "inst_lib.default"; }
 
   void Reset();
-  void SingleProcess();
-  void ProcessBonusInst(const cInstruction& inst);
+  void SingleProcess(cAvidaContext& ctx);
+  void ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst);
 
   
   // --------  Helper methods  --------
@@ -174,9 +174,9 @@
 
 
   // --------  Mutation  --------
-  int PointMutate(const double mut_rate);  
-  bool TriggerMutations(int trigger);
-  bool TriggerMutations(int trigger, cHeadCPU& cur_head);
+  int PointMutate(cAvidaContext& ctx, const double mut_rate);  
+  bool TriggerMutations(cAvidaContext& ctx, int trigger);
+  bool TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head);
   
   
   // Non-Standard Methods
@@ -184,228 +184,212 @@
   int GetActiveStack() const { return threads[cur_thread].cur_stack; }
   bool GetMalActive() const   { return mal_active; }
   
-  
-  /*
-  FIXME: Breakage of interface. These functions return data that is
-  supposed to be private.
-
-  I need to make complete snapshots of hardware state from Python.
-  If I have the time, I'll come back and fix the breakage.
-
-  @kgn
-  */
-  cCPUStack pyGetGlobalStack() { return global_stack; }
-  int pyGetThreadTimeUsed() { return thread_time_used; }
-  const tArray<cHardwareCPU_Thread>& pyGetThreads();
-  bool pyGetAdvanceIP() { return advance_ip; }
-
-
 private:
   // ---------- Instruction Library -----------
 
   // Flow Control
-  bool Inst_If0();
-  bool Inst_IfEqu();
-  bool Inst_IfNot0();
-  bool Inst_IfNEqu();
-  bool Inst_IfGr0();
-  bool Inst_IfGr();
-  bool Inst_IfGrEqu0();
-  bool Inst_IfGrEqu();
-  bool Inst_IfLess0();
-  bool Inst_IfLess();
-  bool Inst_IfLsEqu0();
-  bool Inst_IfLsEqu();
-  bool Inst_IfBit1();
-  bool Inst_IfANotEqB();
-  bool Inst_IfBNotEqC();
-  bool Inst_IfANotEqC();
+  bool Inst_If0(cAvidaContext& ctx);
+  bool Inst_IfEqu(cAvidaContext& ctx);
+  bool Inst_IfNot0(cAvidaContext& ctx);
+  bool Inst_IfNEqu(cAvidaContext& ctx);
+  bool Inst_IfGr0(cAvidaContext& ctx);
+  bool Inst_IfGr(cAvidaContext& ctx);
+  bool Inst_IfGrEqu0(cAvidaContext& ctx);
+  bool Inst_IfGrEqu(cAvidaContext& ctx);
+  bool Inst_IfLess0(cAvidaContext& ctx);
+  bool Inst_IfLess(cAvidaContext& ctx);
+  bool Inst_IfLsEqu0(cAvidaContext& ctx);
+  bool Inst_IfLsEqu(cAvidaContext& ctx);
+  bool Inst_IfBit1(cAvidaContext& ctx);
+  bool Inst_IfANotEqB(cAvidaContext& ctx);
+  bool Inst_IfBNotEqC(cAvidaContext& ctx);
+  bool Inst_IfANotEqC(cAvidaContext& ctx);
 
-  bool Inst_JumpF();
-  bool Inst_JumpB();
-  bool Inst_JumpP();
-  bool Inst_JumpSelf();
-  bool Inst_Call();
-  bool Inst_Return();
+  bool Inst_JumpF(cAvidaContext& ctx);
+  bool Inst_JumpB(cAvidaContext& ctx);
+  bool Inst_JumpP(cAvidaContext& ctx);
+  bool Inst_JumpSelf(cAvidaContext& ctx);
+  bool Inst_Call(cAvidaContext& ctx);
+  bool Inst_Return(cAvidaContext& ctx);
 
   // Stack and Register Operations
-  bool Inst_Pop();
-  bool Inst_Push();
-  bool Inst_HeadPop();
-  bool Inst_HeadPush();
+  bool Inst_Pop(cAvidaContext& ctx);
+  bool Inst_Push(cAvidaContext& ctx);
+  bool Inst_HeadPop(cAvidaContext& ctx);
+  bool Inst_HeadPush(cAvidaContext& ctx);
 
-  bool Inst_PopA();
-  bool Inst_PopB();
-  bool Inst_PopC();
-  bool Inst_PushA();
-  bool Inst_PushB();
-  bool Inst_PushC();
+  bool Inst_PopA(cAvidaContext& ctx);
+  bool Inst_PopB(cAvidaContext& ctx);
+  bool Inst_PopC(cAvidaContext& ctx);
+  bool Inst_PushA(cAvidaContext& ctx);
+  bool Inst_PushB(cAvidaContext& ctx);
+  bool Inst_PushC(cAvidaContext& ctx);
 
-  bool Inst_SwitchStack();
-  bool Inst_FlipStack();
-  bool Inst_Swap();
-  bool Inst_SwapAB();
-  bool Inst_SwapBC();
-  bool Inst_SwapAC();
-  bool Inst_CopyReg();
-  bool Inst_CopyRegAB();
-  bool Inst_CopyRegAC();
-  bool Inst_CopyRegBA();
-  bool Inst_CopyRegBC();
-  bool Inst_CopyRegCA();
-  bool Inst_CopyRegCB();
-  bool Inst_Reset();
+  bool Inst_SwitchStack(cAvidaContext& ctx);
+  bool Inst_FlipStack(cAvidaContext& ctx);
+  bool Inst_Swap(cAvidaContext& ctx);
+  bool Inst_SwapAB(cAvidaContext& ctx);
+  bool Inst_SwapBC(cAvidaContext& ctx);
+  bool Inst_SwapAC(cAvidaContext& ctx);
+  bool Inst_CopyReg(cAvidaContext& ctx);
+  bool Inst_CopyRegAB(cAvidaContext& ctx);
+  bool Inst_CopyRegAC(cAvidaContext& ctx);
+  bool Inst_CopyRegBA(cAvidaContext& ctx);
+  bool Inst_CopyRegBC(cAvidaContext& ctx);
+  bool Inst_CopyRegCA(cAvidaContext& ctx);
+  bool Inst_CopyRegCB(cAvidaContext& ctx);
+  bool Inst_Reset(cAvidaContext& ctx);
 
   // Single-Argument Math
-  bool Inst_ShiftR();
-  bool Inst_ShiftL();
-  bool Inst_Bit1();
-  bool Inst_SetNum();
-  bool Inst_ValGrey();
-  bool Inst_ValDir();
-  bool Inst_ValAddP();
-  bool Inst_ValFib();
-  bool Inst_ValPolyC();
-  bool Inst_Inc();
-  bool Inst_Dec();
-  bool Inst_Zero();
-  bool Inst_Not();
-  bool Inst_Neg();
-  bool Inst_Square();
-  bool Inst_Sqrt();
-  bool Inst_Log();
-  bool Inst_Log10();
-  bool Inst_Minus17();
+  bool Inst_ShiftR(cAvidaContext& ctx);
+  bool Inst_ShiftL(cAvidaContext& ctx);
+  bool Inst_Bit1(cAvidaContext& ctx);
+  bool Inst_SetNum(cAvidaContext& ctx);
+  bool Inst_ValGrey(cAvidaContext& ctx);
+  bool Inst_ValDir(cAvidaContext& ctx);
+  bool Inst_ValAddP(cAvidaContext& ctx);
+  bool Inst_ValFib(cAvidaContext& ctx);
+  bool Inst_ValPolyC(cAvidaContext& ctx);
+  bool Inst_Inc(cAvidaContext& ctx);
+  bool Inst_Dec(cAvidaContext& ctx);
+  bool Inst_Zero(cAvidaContext& ctx);
+  bool Inst_Not(cAvidaContext& ctx);
+  bool Inst_Neg(cAvidaContext& ctx);
+  bool Inst_Square(cAvidaContext& ctx);
+  bool Inst_Sqrt(cAvidaContext& ctx);
+  bool Inst_Log(cAvidaContext& ctx);
+  bool Inst_Log10(cAvidaContext& ctx);
+  bool Inst_Minus17(cAvidaContext& ctx);
 
   // Double Argument Math
-  bool Inst_Add();
-  bool Inst_Sub();
-  bool Inst_Mult();
-  bool Inst_Div();
-  bool Inst_Mod();
-  bool Inst_Nand();
-  bool Inst_Nor();
-  bool Inst_And();
-  bool Inst_Order();
-  bool Inst_Xor();
+  bool Inst_Add(cAvidaContext& ctx);
+  bool Inst_Sub(cAvidaContext& ctx);
+  bool Inst_Mult(cAvidaContext& ctx);
+  bool Inst_Div(cAvidaContext& ctx);
+  bool Inst_Mod(cAvidaContext& ctx);
+  bool Inst_Nand(cAvidaContext& ctx);
+  bool Inst_Nor(cAvidaContext& ctx);
+  bool Inst_And(cAvidaContext& ctx);
+  bool Inst_Order(cAvidaContext& ctx);
+  bool Inst_Xor(cAvidaContext& ctx);
 
   // Biological
-  bool Inst_Copy();
-  bool Inst_ReadInst();
-  bool Inst_WriteInst();
-  bool Inst_StackReadInst();
-  bool Inst_StackWriteInst();
-  bool Inst_Compare();
-  bool Inst_IfNCpy();
-  bool Inst_Allocate();
-  bool Inst_Divide();
-  bool Inst_CAlloc();
-  bool Inst_CDivide();
-  bool Inst_MaxAlloc();
-  bool Inst_Inject();
-  bool Inst_InjectRand();
-  bool Inst_InjectThread();
-  bool Inst_Repro();
-  bool Inst_Kazi();
-  bool Inst_Die();
+  bool Inst_Copy(cAvidaContext& ctx);
+  bool Inst_ReadInst(cAvidaContext& ctx);
+  bool Inst_WriteInst(cAvidaContext& ctx);
+  bool Inst_StackReadInst(cAvidaContext& ctx);
+  bool Inst_StackWriteInst(cAvidaContext& ctx);
+  bool Inst_Compare(cAvidaContext& ctx);
+  bool Inst_IfNCpy(cAvidaContext& ctx);
+  bool Inst_Allocate(cAvidaContext& ctx);
+  bool Inst_Divide(cAvidaContext& ctx);
+  bool Inst_CAlloc(cAvidaContext& ctx);
+  bool Inst_CDivide(cAvidaContext& ctx);
+  bool Inst_MaxAlloc(cAvidaContext& ctx);
+  bool Inst_Inject(cAvidaContext& ctx);
+  bool Inst_InjectRand(cAvidaContext& ctx);
+  bool Inst_InjectThread(cAvidaContext& ctx);
+  bool Inst_Repro(cAvidaContext& ctx);
+  bool Inst_Kazi(cAvidaContext& ctx);
+  bool Inst_Die(cAvidaContext& ctx);
 
   // I/O and Sensory
-  bool Inst_TaskGet();
-  bool Inst_TaskStackGet();
-  bool Inst_TaskStackLoad();
-  bool Inst_TaskPut();
-  bool Inst_TaskIO();
-  bool Inst_Send();
-  bool Inst_Receive();
-  bool Inst_Sense();
+  bool Inst_TaskGet(cAvidaContext& ctx);
+  bool Inst_TaskStackGet(cAvidaContext& ctx);
+  bool Inst_TaskStackLoad(cAvidaContext& ctx);
+  bool Inst_TaskPut(cAvidaContext& ctx);
+  bool Inst_TaskIO(cAvidaContext& ctx);
+  bool Inst_Send(cAvidaContext& ctx);
+  bool Inst_Receive(cAvidaContext& ctx);
+  bool Inst_Sense(cAvidaContext& ctx);
 
   void DoDonate(cOrganism * to_org);
-  bool Inst_DonateRandom();
-  bool Inst_DonateKin();
-  bool Inst_DonateEditDist();
-  bool Inst_DonateNULL();
+  bool Inst_DonateRandom(cAvidaContext& ctx);
+  bool Inst_DonateKin(cAvidaContext& ctx);
+  bool Inst_DonateEditDist(cAvidaContext& ctx);
+  bool Inst_DonateNULL(cAvidaContext& ctx);
 
-  bool Inst_SearchF();
-  bool Inst_SearchB();
-  bool Inst_MemSize();
+  bool Inst_SearchF(cAvidaContext& ctx);
+  bool Inst_SearchB(cAvidaContext& ctx);
+  bool Inst_MemSize(cAvidaContext& ctx);
 
   // Environment
 
-  bool Inst_RotateL();
-  bool Inst_RotateR();
-  bool Inst_SetCopyMut();
-  bool Inst_ModCopyMut();
+  bool Inst_RotateL(cAvidaContext& ctx);
+  bool Inst_RotateR(cAvidaContext& ctx);
+  bool Inst_SetCopyMut(cAvidaContext& ctx);
+  bool Inst_ModCopyMut(cAvidaContext& ctx);
 
   // Multi-threading...
 
-  bool Inst_ForkThread();
-  bool Inst_KillThread();
-  bool Inst_ThreadID();
+  bool Inst_ForkThread(cAvidaContext& ctx);
+  bool Inst_KillThread(cAvidaContext& ctx);
+  bool Inst_ThreadID(cAvidaContext& ctx);
 
   // Head-based instructions...
 
-  bool Inst_SetHead();
-  bool Inst_AdvanceHead();
-  bool Inst_MoveHead();
-  bool Inst_JumpHead();
-  bool Inst_GetHead();
-  bool Inst_IfLabel();
-  bool Inst_IfLabel2();
-  bool Inst_HeadDivide();
-  bool Inst_HeadRead();
-  bool Inst_HeadWrite();
-  bool Inst_HeadCopy();
-  bool Inst_HeadSearch();
-  bool Inst_SetFlow();
+  bool Inst_SetHead(cAvidaContext& ctx);
+  bool Inst_AdvanceHead(cAvidaContext& ctx);
+  bool Inst_MoveHead(cAvidaContext& ctx);
+  bool Inst_JumpHead(cAvidaContext& ctx);
+  bool Inst_GetHead(cAvidaContext& ctx);
+  bool Inst_IfLabel(cAvidaContext& ctx);
+  bool Inst_IfLabel2(cAvidaContext& ctx);
+  bool Inst_HeadDivide(cAvidaContext& ctx);
+  bool Inst_HeadRead(cAvidaContext& ctx);
+  bool Inst_HeadWrite(cAvidaContext& ctx);
+  bool Inst_HeadCopy(cAvidaContext& ctx);
+  bool Inst_HeadSearch(cAvidaContext& ctx);
+  bool Inst_SetFlow(cAvidaContext& ctx);
 
-  bool Inst_HeadCopy2();
-  bool Inst_HeadCopy3();
-  bool Inst_HeadCopy4();
-  bool Inst_HeadCopy5();
-  bool Inst_HeadCopy6();
-  bool Inst_HeadCopy7();
-  bool Inst_HeadCopy8();
-  bool Inst_HeadCopy9();
-  bool Inst_HeadCopy10();
+  bool Inst_HeadCopy2(cAvidaContext& ctx);
+  bool Inst_HeadCopy3(cAvidaContext& ctx);
+  bool Inst_HeadCopy4(cAvidaContext& ctx);
+  bool Inst_HeadCopy5(cAvidaContext& ctx);
+  bool Inst_HeadCopy6(cAvidaContext& ctx);
+  bool Inst_HeadCopy7(cAvidaContext& ctx);
+  bool Inst_HeadCopy8(cAvidaContext& ctx);
+  bool Inst_HeadCopy9(cAvidaContext& ctx);
+  bool Inst_HeadCopy10(cAvidaContext& ctx);
 
-  bool Inst_HeadDivideSex();
-  bool Inst_HeadDivideAsex();
-  bool Inst_HeadDivideAsexWait();
-  bool Inst_HeadDivideMateSelect();
+  bool Inst_HeadDivideSex(cAvidaContext& ctx);
+  bool Inst_HeadDivideAsex(cAvidaContext& ctx);
+  bool Inst_HeadDivideAsexWait(cAvidaContext& ctx);
+  bool Inst_HeadDivideMateSelect(cAvidaContext& ctx);
 
-  bool Inst_HeadDivide1();
-  bool Inst_HeadDivide2();
-  bool Inst_HeadDivide3();
-  bool Inst_HeadDivide4();
-  bool Inst_HeadDivide5();
-  bool Inst_HeadDivide6();
-  bool Inst_HeadDivide7();
-  bool Inst_HeadDivide8();
-  bool Inst_HeadDivide9();
-  bool Inst_HeadDivide10();
-  bool Inst_HeadDivide16();
-  bool Inst_HeadDivide32();
-  bool Inst_HeadDivide50();
-  bool Inst_HeadDivide100();
-  bool Inst_HeadDivide500();
-  bool Inst_HeadDivide1000();
-  bool Inst_HeadDivide5000();
-  bool Inst_HeadDivide10000();
-  bool Inst_HeadDivide50000();
-  bool Inst_HeadDivide0_5();
-  bool Inst_HeadDivide0_1();
-  bool Inst_HeadDivide0_05();
-  bool Inst_HeadDivide0_01();
-  bool Inst_HeadDivide0_001();
+  bool Inst_HeadDivide1(cAvidaContext& ctx);
+  bool Inst_HeadDivide2(cAvidaContext& ctx);
+  bool Inst_HeadDivide3(cAvidaContext& ctx);
+  bool Inst_HeadDivide4(cAvidaContext& ctx);
+  bool Inst_HeadDivide5(cAvidaContext& ctx);
+  bool Inst_HeadDivide6(cAvidaContext& ctx);
+  bool Inst_HeadDivide7(cAvidaContext& ctx);
+  bool Inst_HeadDivide8(cAvidaContext& ctx);
+  bool Inst_HeadDivide9(cAvidaContext& ctx);
+  bool Inst_HeadDivide10(cAvidaContext& ctx);
+  bool Inst_HeadDivide16(cAvidaContext& ctx);
+  bool Inst_HeadDivide32(cAvidaContext& ctx);
+  bool Inst_HeadDivide50(cAvidaContext& ctx);
+  bool Inst_HeadDivide100(cAvidaContext& ctx);
+  bool Inst_HeadDivide500(cAvidaContext& ctx);
+  bool Inst_HeadDivide1000(cAvidaContext& ctx);
+  bool Inst_HeadDivide5000(cAvidaContext& ctx);
+  bool Inst_HeadDivide10000(cAvidaContext& ctx);
+  bool Inst_HeadDivide50000(cAvidaContext& ctx);
+  bool Inst_HeadDivide0_5(cAvidaContext& ctx);
+  bool Inst_HeadDivide0_1(cAvidaContext& ctx);
+  bool Inst_HeadDivide0_05(cAvidaContext& ctx);
+  bool Inst_HeadDivide0_01(cAvidaContext& ctx);
+  bool Inst_HeadDivide0_001(cAvidaContext& ctx);
 
   //// Placebo ////
-  bool Inst_Skip();
+  bool Inst_Skip(cAvidaContext& ctx);
 
 
   // Internal Implementation
 
   bool SingleProcess_PayCosts(const cInstruction & cur_inst);
-  bool SingleProcess_ExecuteInst(const cInstruction & cur_inst);
+  bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction & cur_inst);
   
   // --------  Stack Manipulation...  --------
   inline void StackPush(int value);
@@ -431,13 +415,13 @@
   cCodeLabel& GetReadLabel() { return threads[cur_thread].read_label; }
   
   
-  bool TriggerMutations_ScopeGenome(const cMutation* cur_mut,
+  bool TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut,
                                     cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
-  bool TriggerMutations_ScopeLocal(const cMutation* cur_mut,
+  bool TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut,
                                    cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
-  int TriggerMutations_ScopeGlobal(const cMutation* cur_mut, 
+  int TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut, 
                                    cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
-  void TriggerMutations_Body(int type, cCPUMemory& target_memory, cHeadCPU& cur_head);
+  void TriggerMutations_Body(cAvidaContext& ctx, int type, cCPUMemory& target_memory, cHeadCPU& cur_head);
   
   
   // ---------- Instruction Helpers -----------
@@ -448,21 +432,21 @@
   void Fault(int fault_loc, int fault_type, cString fault_desc="");
   
   bool Allocate_Necro(const int new_size);
-  bool Allocate_Random(const int old_size, const int new_size);
+  bool Allocate_Random(cAvidaContext& ctx, const int old_size, const int new_size);
   bool Allocate_Default(const int new_size);
-  bool Allocate_Main(const int allocated_size);
+  bool Allocate_Main(cAvidaContext& ctx, const int allocated_size);
   
-  bool Divide_Main(const int divide_point, const int extra_lines=0, double mut_multiplier=1);
-  bool Divide_CheckViable(const int child_size, const int parent_size);
-  void Divide_DoMutations(double mut_multiplier=1);
-  void Divide_TestFitnessMeasures();
-  void Mutate(const int mut_point);
+  bool Divide_Main(cAvidaContext& ctx, const int divide_point, const int extra_lines=0, double mut_multiplier=1);
+  bool Divide_CheckViable(cAvidaContext& ctx, const int child_size, const int parent_size);
+  void Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier=1);
+  void Divide_TestFitnessMeasures(cAvidaContext& ctx);
+  void Mutate(cAvidaContext& ctx, const int mut_point);
   
   void InjectCode(const cGenome& injection, const int line_num);
   void InjectCodeThread(const cGenome& injection, const int line_num);
   
-  bool HeadCopy_ErrorCorrect(double reduction);
-  bool Inst_HeadDivideMut(double mut_multiplier=1);
+  bool HeadCopy_ErrorCorrect(cAvidaContext& ctx, double reduction);
+  bool Inst_HeadDivideMut(cAvidaContext& ctx, double mut_multiplier = 1);
   
   void ReadInst(const int in_inst);
 };

Modified: development/source/cpu/cHardwareSMT.cc
===================================================================
--- development/source/cpu/cHardwareSMT.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareSMT.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -9,6 +9,7 @@
 
 #include "cHardwareSMT.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "functions.h"
 #include "cGenomeUtil.h"
@@ -166,7 +167,7 @@
 // This function processes the very next command in the genome, and is made
 // to be as optimized as possible.  This is the heart of avida.
 
-void cHardwareSMT::SingleProcess()
+void cHardwareSMT::SingleProcess(cAvidaContext& ctx)
 {
   // Mark this organism as running...
   organism->SetRunning(true);
@@ -203,7 +204,7 @@
 		
     // Now execute the instruction...
     if (exec == true) {
-      SingleProcess_ExecuteInst(cur_inst);
+      SingleProcess_ExecuteInst(ctx, cur_inst);
 			
       // Some instruction (such as jump) may turn advance_ip off.  Ususally
       // we now want to move to the next instruction in the memory.
@@ -255,14 +256,14 @@
 
 // This method will handle the actual execution of an instruction
 // within single process, once that function has been finalized.
-bool cHardwareSMT::SingleProcess_ExecuteInst(const cInstruction & cur_inst) 
+bool cHardwareSMT::SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst) 
 {
   // Copy Instruction locally to handle stochastic effects
   cInstruction actual_inst = cur_inst;
   
 #ifdef EXECUTION_ERRORS
   // If there is an execution error, execute a random instruction.
-  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst();
+  if (organism->TestExeErr()) actual_inst = m_inst_set->GetRandomInst(ctx);
 #endif /* EXECUTION_ERRORS */
 	
   // Get a pointer to the corrisponding method...
@@ -278,7 +279,7 @@
 #endif
 	
   // And execute it.
-  const bool exec_success = (this->*(m_functions[inst_idx]))();
+  const bool exec_success = (this->*(m_functions[inst_idx]))(ctx);
 	
 #ifdef INSTRUCTION_COUNT
   // decremenet if the instruction was not executed successfully
@@ -291,7 +292,7 @@
 }
 
 
-void cHardwareSMT::ProcessBonusInst(const cInstruction & inst)
+void cHardwareSMT::ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst)
 {
   // Mark this organism as running...
   bool prev_run_state = organism->GetIsRunning();
@@ -304,7 +305,7 @@
     }
   }
   
-  SingleProcess_ExecuteInst(inst);
+  SingleProcess_ExecuteInst(ctx, inst);
   
   organism->SetRunning(prev_run_state);
 }
@@ -680,7 +681,7 @@
 }
 
 // This is the code run by the INFECTED organism.  Its function is to SPREAD infection.
-bool cHardwareSMT::InjectParasite(double mut_multiplier)
+bool cHardwareSMT::InjectParasite(cAvidaContext& ctx, double mut_multiplier)
 {
   const int end_pos = GetHead(nHardware::HEAD_WRITE).GetPosition();
   const int mem_space_used = GetHead(nHardware::HEAD_WRITE).GetMemSpace();
@@ -702,7 +703,7 @@
 	
   cCPUMemory injected_code = m_mem_array[mem_space_used];
 	
-  Inject_DoMutations(mut_multiplier, injected_code);
+  Inject_DoMutations(ctx, mut_multiplier, injected_code);
 	
   int inject_signal = false;
 	
@@ -749,24 +750,24 @@
   return false;
 }
 
-void cHardwareSMT::Mutate(int mut_point)
+void cHardwareSMT::Mutate(cAvidaContext& ctx, int mut_point)
 {
   // Test if trying to mutate outside of genome...
   assert(mut_point >= 0 && mut_point < m_mem_array[0].GetSize());
 	
-  m_mem_array[0][mut_point] = m_inst_set->GetRandomInst();
+  m_mem_array[0][mut_point] = m_inst_set->GetRandomInst(ctx);
   m_mem_array[0].SetFlagMutated(mut_point);
   m_mem_array[0].SetFlagPointMut(mut_point);
   organism->CPUStats().mut_stats.point_mut_count++;
 }
 
-int cHardwareSMT::PointMutate(const double mut_rate)
+int cHardwareSMT::PointMutate(cAvidaContext& ctx, const double mut_rate)
 {
   const int num_muts = m_world->GetRandom().GetRandBinomial(m_mem_array[0].GetSize(), mut_rate);
 	
   for (int i = 0; i < num_muts; i++) {
     const int pos = m_world->GetRandom().GetUInt(m_mem_array[0].GetSize());
-    Mutate(pos);
+    Mutate(ctx, pos);
   }
 	
   return num_muts;
@@ -776,7 +777,7 @@
 // Trigger mutations of a specific type.  Outside triggers cannot specify
 // a head since hardware types are not known.
 
-bool cHardwareSMT::TriggerMutations(int trigger)
+bool cHardwareSMT::TriggerMutations(cAvidaContext& ctx, int trigger)
 {
   // Only update triggers should happen from the outside!
   assert(trigger == nMutation::TRIGGER_UPDATE);
@@ -784,10 +785,10 @@
   // Assume instruction pointer is the intended target (if one is even
   // needed!
 	
-  return TriggerMutations(trigger, IP());
+  return TriggerMutations(ctx, trigger, IP());
 }
 
-bool cHardwareSMT::TriggerMutations(int trigger, cHeadCPU& cur_head)
+bool cHardwareSMT::TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head)
 {
   // Collect information about mutations from the organism.
   cLocalMutations& mut_info = organism->GetLocalMutations();
@@ -812,14 +813,14 @@
     const double rate = mut_info.GetRate(mut_id);
     switch (scope) {
 			case nMutation::SCOPE_GENOME:
-				if (TriggerMutations_ScopeGenome(cur_mut, target_mem, cur_head, rate)) {
+				if (TriggerMutations_ScopeGenome(ctx, cur_mut, target_mem, cur_head, rate)) {
 					has_mutation = true;
 					mut_info.IncCount(mut_id);
 				}
 				break;
 			case nMutation::SCOPE_LOCAL:
 			case nMutation::SCOPE_PROP:
-				if (TriggerMutations_ScopeLocal(cur_mut, target_mem, cur_head, rate)) {
+				if (TriggerMutations_ScopeLocal(ctx, cur_mut, target_mem, cur_head, rate)) {
 					has_mutation = true;
 					mut_info.IncCount(mut_id);
 				}
@@ -827,7 +828,7 @@
 			case nMutation::SCOPE_GLOBAL:
 			case nMutation::SCOPE_SPREAD:
 				int num_muts =
-				TriggerMutations_ScopeGlobal(cur_mut, target_mem, cur_head, rate);
+				TriggerMutations_ScopeGlobal(ctx, cur_mut, target_mem, cur_head, rate);
 				if (num_muts > 0) {
 					has_mutation = true;
 					mut_info.IncCount(mut_id, num_muts);
@@ -839,7 +840,7 @@
   return has_mutation;
 }
 
-bool cHardwareSMT::TriggerMutations_ScopeGenome(const cMutation* cur_mut,
+bool cHardwareSMT::TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut,
                                                 cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored indicates the probability that a single
@@ -850,26 +851,26 @@
     // position in the genome to be mutated.
     cHeadCPU tmp_head(cur_head);
     tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     return true;
   }
   return false;
 }
 
-bool cHardwareSMT::TriggerMutations_ScopeLocal(const cMutation* cur_mut,
+bool cHardwareSMT::TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut,
                                                cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The rate we have stored is the probability for a mutation at this single
   // position in the genome.
 	
   if (m_world->GetRandom().P(rate) == true) {
-    TriggerMutations_Body(cur_mut->GetType(), target_memory, cur_head);
+    TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, cur_head);
     return true;
   }
   return false;
 }
 
-int cHardwareSMT::TriggerMutations_ScopeGlobal(const cMutation * cur_mut,
+int cHardwareSMT::TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation * cur_mut,
                                                cCPUMemory & target_memory, cHeadCPU& cur_head, const double rate)
 {
   // The probability we have stored is per-site, so we can pull a random
@@ -883,20 +884,20 @@
     for (int i = 0; i < num_mut; i++) {
       cHeadCPU tmp_head(cur_head);
       tmp_head.AbsSet(m_world->GetRandom().GetUInt(target_memory.GetSize()));
-      TriggerMutations_Body(cur_mut->GetType(), target_memory, tmp_head);
+      TriggerMutations_Body(ctx, cur_mut->GetType(), target_memory, tmp_head);
     }
   }
 	
   return num_mut;
 }
 
-void cHardwareSMT::TriggerMutations_Body(int type, cCPUMemory & target_memory, cHeadCPU& cur_head)
+void cHardwareSMT::TriggerMutations_Body(cAvidaContext& ctx, int type, cCPUMemory & target_memory, cHeadCPU& cur_head)
 {
   const int pos = cur_head.GetPosition();
 	
   switch (type) {
 		case nMutation::TYPE_POINT:
-			target_memory[pos] = m_inst_set->GetRandomInst();
+			target_memory[pos] = m_inst_set->GetRandomInst(ctx);
 			target_memory.SetFlagMutated(pos);
 			break;
 		case nMutation::TYPE_INSERT:
@@ -1093,7 +1094,7 @@
   organism->Fault(fault_loc, fault_type, fault_desc);
 }
 
-bool cHardwareSMT::Divide_CheckViable(const int parent_size,
+bool cHardwareSMT::Divide_CheckViable(cAvidaContext& ctx, const int parent_size,
                                       const int child_size, const int mem_space)
 {
   // Make sure the organism is okay with dividing now...
@@ -1151,7 +1152,7 @@
   return true; // (divide succeeds!)
 }
 
-void cHardwareSMT::Divide_DoMutations(double mut_multiplier)
+void cHardwareSMT::Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier)
 {
   sCPUStats& cpu_stats = organism->CPUStats();
   cCPUMemory& child_genome = organism->ChildGenome();
@@ -1161,14 +1162,14 @@
   // Divide Mutations
   if (organism->TestDivideMut()) {
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize());
-    child_genome[mut_line] = m_inst_set->GetRandomInst();
+    child_genome[mut_line] = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.divide_mut_count++;
   }
 	
   // Divide Insertions
   if (organism->TestDivideIns() && child_genome.GetSize() < MAX_CREATURE_SIZE){
     const unsigned int mut_line = m_world->GetRandom().GetUInt(child_genome.GetSize() + 1);
-    child_genome.Insert(mut_line, m_inst_set->GetRandomInst());
+    child_genome.Insert(mut_line, m_inst_set->GetRandomInst(ctx));
     cpu_stats.mut_stats.divide_insert_mut_count++;
   }
 	
@@ -1187,7 +1188,7 @@
     if( num_mut > 0 ){
       for (int i = 0; i < num_mut; i++) {
 				int site = m_world->GetRandom().GetUInt(child_genome.GetSize());
-				child_genome[site] = m_inst_set->GetRandomInst();
+				child_genome[site] = m_inst_set->GetRandomInst(ctx);
 				cpu_stats.mut_stats.div_mut_count++;
       }
     }
@@ -1213,7 +1214,7 @@
       qsort( (void*)mut_sites, num_mut, sizeof(int), &IntCompareFunction );
       // Actually do the mutations (in reverse sort order)
       for(int i = num_mut-1; i >= 0; i--) {
-				child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst());
+				child_genome.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx));
 				cpu_stats.mut_stats.insert_mut_count++;
       }
     }
@@ -1241,7 +1242,7 @@
   if (organism->GetParentMutProb() > 0) {
     for (int i = 0; i < m_mem_array[0].GetSize(); i++) {
       if (organism->TestParentMut()) {
-				m_mem_array[0][i] = m_inst_set->GetRandomInst();
+				m_mem_array[0][i] = m_inst_set->GetRandomInst(ctx);
 				cpu_stats.mut_stats.parent_mut_line_count++;
       }
     }
@@ -1261,20 +1262,20 @@
   }
 }
 
-void cHardwareSMT::Inject_DoMutations(double mut_multiplier, cCPUMemory & injected_code)
+void cHardwareSMT::Inject_DoMutations(cAvidaContext& ctx, double mut_multiplier, cCPUMemory & injected_code)
 {
   organism->GetPhenotype().SetDivType(mut_multiplier);
 	
   // Divide Mutations
   if (organism->TestDivideMut()) {
     const unsigned int mut_line = m_world->GetRandom().GetUInt(injected_code.GetSize());
-    injected_code[mut_line] = m_inst_set->GetRandomInst();
+    injected_code[mut_line] = m_inst_set->GetRandomInst(ctx);
   }
 	
   // Divide Insertions
   if (organism->TestDivideIns() && injected_code.GetSize() < MAX_CREATURE_SIZE){
     const unsigned int mut_line = m_world->GetRandom().GetUInt(injected_code.GetSize() + 1);
-    injected_code.Insert(mut_line, m_inst_set->GetRandomInst());
+    injected_code.Insert(mut_line, m_inst_set->GetRandomInst(ctx));
   }
 	
   // Divide Deletions
@@ -1291,7 +1292,7 @@
     if( num_mut > 0 ){
       for (int i = 0; i < num_mut; i++) {
 				int site = m_world->GetRandom().GetUInt(injected_code.GetSize());
-				injected_code[site] = m_inst_set->GetRandomInst();
+				injected_code[site] = m_inst_set->GetRandomInst(ctx);
       }
     }
   }
@@ -1316,7 +1317,7 @@
       qsort( (void*)mut_sites, num_mut, sizeof(int), &IntCompareFunction );
       // Actually do the mutations (in reverse sort order)
       for(int i = num_mut-1; i >= 0; i--) {
-				injected_code.Insert(mut_sites[i], m_inst_set->GetRandomInst());
+				injected_code.Insert(mut_sites[i], m_inst_set->GetRandomInst(ctx));
       }
     }
   }
@@ -1342,7 +1343,7 @@
   if (organism->GetParentMutProb() > 0) {
     for (int i = 0; i < m_mem_array[0].GetSize(); i++) {
       if (organism->TestParentMut()) {
-				m_mem_array[0][i] = m_inst_set->GetRandomInst();
+				m_mem_array[0][i] = m_inst_set->GetRandomInst(ctx);
       }
     }
   }
@@ -1351,7 +1352,7 @@
 
 
 // test whether the offspring creature contains an advantageous mutation.
-void cHardwareSMT::Divide_TestFitnessMeasures()
+void cHardwareSMT::Divide_TestFitnessMeasures(cAvidaContext& ctx)
 {
   cPhenotype & phenotype = organism->GetPhenotype();
   phenotype.CopyTrue() = ( organism->ChildGenome() == organism->GetGenome() );
@@ -1373,7 +1374,7 @@
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   cCPUTestInfo test_info;
   test_info.UseRandomInputs();
-  testcpu->TestGenome(test_info, organism->ChildGenome());
+  testcpu->TestGenome(ctx, test_info, organism->ChildGenome());
   const double child_fitness = test_info.GetGenotypeFitness();
   delete testcpu;
   
@@ -1412,7 +1413,7 @@
 }
 
 
-bool cHardwareSMT::Divide_Main(int mem_space_used, double mut_multiplier)
+bool cHardwareSMT::Divide_Main(cAvidaContext& ctx, int mem_space_used, double mut_multiplier)
 {
   int write_head_pos = GetHead(nHardware::HEAD_WRITE).GetPosition();
   
@@ -1424,7 +1425,7 @@
   if (m_mem_array.GetSize() <= mem_space_used) return false;
 	
   // Make sure this divide will produce a viable offspring.
-  if(!Divide_CheckViable(m_mem_array[IP().GetMemSpace()].GetSize(), write_head_pos, mem_space_used)) 
+  if(!Divide_CheckViable(ctx, m_mem_array[IP().GetMemSpace()].GetSize(), write_head_pos, mem_space_used)) 
     return false;
   
   // Since the divide will now succeed, set up the information to be sent to the new organism
@@ -1432,12 +1433,12 @@
   organism->ChildGenome() = m_mem_array[mem_space_used];
 	
   // Handle Divide Mutations...
-  Divide_DoMutations(mut_multiplier);
+  Divide_DoMutations(ctx, mut_multiplier);
 	
   // Many tests will require us to run the offspring through a test CPU;
   // this is, for example, to see if mutations need to be reverted or if
   // lineages need to be updated.
-  Divide_TestFitnessMeasures();
+  Divide_TestFitnessMeasures(ctx);
 	
 #ifdef INSTRUCTION_COSTS
   // reset first time instruction costs
@@ -1491,7 +1492,7 @@
 
 
 //6
-bool cHardwareSMT::Inst_ShiftR()
+bool cHardwareSMT::Inst_ShiftR(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1506,7 +1507,7 @@
 }
 
 //7
-bool cHardwareSMT::Inst_ShiftL()
+bool cHardwareSMT::Inst_ShiftL(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1521,7 +1522,7 @@
 }
 
 //8
-bool cHardwareSMT::Inst_Val_Nand()
+bool cHardwareSMT::Inst_Val_Nand(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1536,7 +1537,7 @@
 }
 
 //9
-bool cHardwareSMT::Inst_Val_Add()
+bool cHardwareSMT::Inst_Val_Add(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1551,7 +1552,7 @@
 }
 
 //10
-bool cHardwareSMT::Inst_Val_Sub()
+bool cHardwareSMT::Inst_Val_Sub(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1566,7 +1567,7 @@
 }
 
 //11
-bool cHardwareSMT::Inst_Val_Mult()
+bool cHardwareSMT::Inst_Val_Mult(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1581,7 +1582,7 @@
 }
 
 //12
-bool cHardwareSMT::Inst_Val_Div()
+bool cHardwareSMT::Inst_Val_Div(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1604,7 +1605,7 @@
 }
 
 //32
-bool cHardwareSMT::Inst_Val_Inc()
+bool cHardwareSMT::Inst_Val_Inc(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1618,7 +1619,7 @@
 }
 
 //33
-bool cHardwareSMT::Inst_Val_Dec()
+bool cHardwareSMT::Inst_Val_Dec(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1632,7 +1633,7 @@
 }
 
 //34
-bool cHardwareSMT::Inst_Val_Mod()
+bool cHardwareSMT::Inst_Val_Mod(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1655,7 +1656,7 @@
 }
 
 //13 
-bool cHardwareSMT::Inst_SetMemory() 
+bool cHardwareSMT::Inst_SetMemory(cAvidaContext& ctx) 
 {
   ReadLabel(nHardwareSMT::MAX_MEMSPACE_LABEL);
   
@@ -1671,16 +1672,16 @@
 }
 
 //14
-bool cHardwareSMT::Inst_Divide()
+bool cHardwareSMT::Inst_Divide(cAvidaContext& ctx)
 {
   int mem_space_used = GetHead(nHardware::HEAD_WRITE).GetMemSpace();
   int mut_multiplier = 1;
 	
-  return Divide_Main(mem_space_used, mut_multiplier);
+  return Divide_Main(ctx, mem_space_used, mut_multiplier);
 }
 
 //15
-bool cHardwareSMT::Inst_HeadRead()
+bool cHardwareSMT::Inst_HeadRead(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_READ);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1695,7 +1696,7 @@
   // Mutations only occur on the read, for the moment.
   int read_inst = 0;
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst().GetOp();
+    read_inst = m_inst_set->GetRandomInst(ctx).GetOp();
     cpu_stats.mut_stats.copy_mut_count++;  // @CAO, hope this is good!
   } else {
     read_inst = GetHead(head_id).GetInst().GetOp();
@@ -1709,7 +1710,7 @@
 }
 
 //16
-bool cHardwareSMT::Inst_HeadWrite()
+bool cHardwareSMT::Inst_HeadWrite(cAvidaContext& ctx)
 {
   const int head_id = FindModifiedHead(nHardware::HEAD_WRITE);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1741,7 +1742,7 @@
 }
 
 //??
-bool cHardwareSMT::Inst_HeadCopy()
+bool cHardwareSMT::Inst_HeadCopy(cAvidaContext& ctx)
 {
   // For the moment, this cannot be nop-modified.
   cHeadMultiMem & read_head = GetHead(nHardware::HEAD_READ);
@@ -1754,7 +1755,7 @@
   // Do mutations.
   cInstruction read_inst = read_head.GetInst();
   if (organism->TestCopyMut()) {
-    read_inst = m_inst_set->GetRandomInst();
+    read_inst = m_inst_set->GetRandomInst(ctx);
     cpu_stats.mut_stats.copy_mut_count++; 
     write_head.SetFlagMutated();
     write_head.SetFlagCopyMut();
@@ -1773,7 +1774,7 @@
 }
 
 //17
-bool cHardwareSMT::Inst_IfEqual()      // Execute next if bx == ?cx?
+bool cHardwareSMT::Inst_IfEqual(cAvidaContext& ctx)      // Execute next if bx == ?cx?
 {
   const int op1 = FindModifiedStack(nHardwareSMT::STACK_AX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1786,7 +1787,7 @@
 }
 
 //18
-bool cHardwareSMT::Inst_IfNotEqual()     // Execute next if bx != ?cx?
+bool cHardwareSMT::Inst_IfNotEqual(cAvidaContext& ctx)     // Execute next if bx != ?cx?
 {
   const int op1 = FindModifiedStack(nHardwareSMT::STACK_AX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1799,7 +1800,7 @@
 }
 
 //19
-bool cHardwareSMT::Inst_IfLess()       // Execute next if ?bx? < ?cx?
+bool cHardwareSMT::Inst_IfLess(cAvidaContext& ctx)       // Execute next if ?bx? < ?cx?
 {
   const int op1 = FindModifiedStack(nHardwareSMT::STACK_AX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1812,7 +1813,7 @@
 }
 
 //20
-bool cHardwareSMT::Inst_IfGreater()       // Execute next if bx > ?cx?
+bool cHardwareSMT::Inst_IfGreater(cAvidaContext& ctx)       // Execute next if bx > ?cx?
 {
   const int op1 = FindModifiedStack(nHardwareSMT::STACK_AX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1825,7 +1826,7 @@
 }
 
 //21
-bool cHardwareSMT::Inst_HeadPush()
+bool cHardwareSMT::Inst_HeadPush(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1838,7 +1839,7 @@
 }
 
 //22
-bool cHardwareSMT::Inst_HeadPop()
+bool cHardwareSMT::Inst_HeadPop(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1851,7 +1852,7 @@
 }
 
 //23 
-bool cHardwareSMT::Inst_HeadMove()
+bool cHardwareSMT::Inst_HeadMove(cAvidaContext& ctx)
 {
   const int head_used = FindModifiedHead(nHardware::HEAD_IP);
   if(head_used != nHardware::HEAD_FLOW)
@@ -1867,7 +1868,7 @@
 }
 
 //24
-bool cHardwareSMT::Inst_Search()
+bool cHardwareSMT::Inst_Search(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(2, nHardwareSMT::NUM_NOPS);
@@ -1891,7 +1892,7 @@
 }
 
 //25
-bool cHardwareSMT::Inst_PushNext() 
+bool cHardwareSMT::Inst_PushNext(cAvidaContext& ctx) 
 {
   // @DMB - Should this allow modified next, or be eliminated in favor of just 'Push'
   const int src = FindModifiedStack(nHardwareSMT::STACK_AX);
@@ -1905,7 +1906,7 @@
 }
 
 //26
-bool cHardwareSMT::Inst_PushPrevious() 
+bool cHardwareSMT::Inst_PushPrevious(cAvidaContext& ctx) 
 {
   // @DMB - Should this allow modified previous, or be eliminated in favor of just 'Push'
   const int src = FindModifiedStack(nHardwareSMT::STACK_BX);
@@ -1919,7 +1920,7 @@
 }
 
 //27
-bool cHardwareSMT::Inst_PushComplement() 
+bool cHardwareSMT::Inst_PushComplement(cAvidaContext& ctx) 
 {
   // @DMB - Should this allow modified complement, or be eliminated in favor of just 'Push'
   int src = FindModifiedStack(nHardwareSMT::STACK_BX);
@@ -1933,7 +1934,7 @@
 }
 
 //28
-bool cHardwareSMT::Inst_ValDelete()
+bool cHardwareSMT::Inst_ValDelete(cAvidaContext& ctx)
 {
   int stack_used = FindModifiedStack(nHardwareSMT::STACK_BX);
   Stack(stack_used).Pop();
@@ -1941,7 +1942,7 @@
 }
 
 //29
-bool cHardwareSMT::Inst_ValCopy()
+bool cHardwareSMT::Inst_ValCopy(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1954,7 +1955,7 @@
 }
 
 //30
-bool cHardwareSMT::Inst_ForkThread()
+bool cHardwareSMT::Inst_ForkThread(cAvidaContext& ctx)
 {
   if (!ForkThread()) 
     Fault(FAULT_LOC_THREAD_FORK, FAULT_TYPE_FORK_TH);
@@ -1964,7 +1965,7 @@
 }
 
 //31
-bool cHardwareSMT::Inst_IfLabel()
+bool cHardwareSMT::Inst_IfLabel(cAvidaContext& ctx)
 {
   ReadLabel();
   GetLabel().Rotate(2, nHardwareSMT::NUM_NOPS);
@@ -1973,7 +1974,7 @@
 }
 
 //35
-bool cHardwareSMT::Inst_KillThread()
+bool cHardwareSMT::Inst_KillThread(cAvidaContext& ctx)
 {
   if (!KillThread()) Fault(FAULT_LOC_THREAD_KILL, FAULT_TYPE_KILL_TH);
   else AdvanceIP() = false;
@@ -1981,7 +1982,7 @@
 }
 
 //36
-bool cHardwareSMT::Inst_IO()
+bool cHardwareSMT::Inst_IO(cAvidaContext& ctx)
 {
   const int dst = FindModifiedStack(nHardwareSMT::STACK_BX);
 #ifdef SMT_FULLY_ASSOCIATIVE
@@ -1992,7 +1993,7 @@
 	
   // Do the "put" component
   const int value_out = Stack(src).Top();
-  organism->DoOutput(value_out);  // Check for tasks compleated.
+  organism->DoOutput(ctx, value_out);  // Check for tasks compleated.
 	
   // Do the "get" component
   const int value_in = organism->GetNextInput();
@@ -2010,14 +2011,14 @@
 // It will then look at the template that follows the command and inject it
 // into the complement template found in a neighboring organism.
 
-bool cHardwareSMT::Inst_Inject()
+bool cHardwareSMT::Inst_Inject(cAvidaContext& ctx)
 {
   double mut_multiplier = 1;
 	
-  return InjectParasite(mut_multiplier);
+  return InjectParasite(ctx, mut_multiplier);
 }
 
-bool cHardwareSMT::Inst_Apoptosis()
+bool cHardwareSMT::Inst_Apoptosis(cAvidaContext& ctx)
 {
   organism->Die();
   

Modified: development/source/cpu/cHardwareSMT.h
===================================================================
--- development/source/cpu/cHardwareSMT.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cHardwareSMT.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -47,6 +47,7 @@
 #endif
 
 
+class cAvidaContext;
 class cInstSet;
 class cInstLibBase;
 class cOrganism;
@@ -63,7 +64,7 @@
 class cHardwareSMT : public cHardwareBase
 {
 public:
-  typedef bool (cHardwareSMT::*tMethod)();
+  typedef bool (cHardwareSMT::*tMethod)(cAvidaContext& ctx);
 
 private:
   static tInstLib<cHardwareSMT::tMethod>* s_inst_slib;
@@ -91,7 +92,7 @@
 
   
   bool SingleProcess_PayCosts(const cInstruction& cur_inst);
-  bool SingleProcess_ExecuteInst(const cInstruction& cur_inst);
+  bool SingleProcess_ExecuteInst(cAvidaContext& ctx, const cInstruction& cur_inst);
   	
 
   // --------  Stack Manipulation...  --------
@@ -117,13 +118,13 @@
   cCodeLabel& GetReadLabel() { return m_threads[m_cur_thread].read_label; }
 
   
-  bool TriggerMutations_ScopeGenome(const cMutation * cur_mut,
-																		cCPUMemory & target_memory, cHeadCPU& cur_head, const double rate);
-  bool TriggerMutations_ScopeLocal(const cMutation * cur_mut,
-																	 cCPUMemory & target_memory, cHeadCPU& cur_head, const double rate);
-  int TriggerMutations_ScopeGlobal(const cMutation * cur_mut,
-																	 cCPUMemory & target_memory, cHeadCPU& cur_head, const double rate);
-  void TriggerMutations_Body(int type, cCPUMemory & target_memory,
+  bool TriggerMutations_ScopeGenome(cAvidaContext& ctx, const cMutation* cur_mut,
+																		cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
+  bool TriggerMutations_ScopeLocal(cAvidaContext& ctx, const cMutation* cur_mut,
+																	 cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
+  int TriggerMutations_ScopeGlobal(cAvidaContext& ctx, const cMutation* cur_mut,
+																	 cCPUMemory& target_memory, cHeadCPU& cur_head, const double rate);
+  void TriggerMutations_Body(cAvidaContext& ctx, int type, cCPUMemory& target_memory,
 														 cHeadCPU& cur_head);
 	
   // ---------- Instruction Helpers -----------
@@ -143,14 +144,14 @@
   bool Allocate_Default(const int new_size);
   bool Allocate_Main(const int allocated_size);
 	
-  bool Divide_Main(const int mem_space_used, double mut_multiplier=1);
-  bool Divide_CheckViable(const int parent_size, const int child_size, const int mem_space);
-  void Divide_DoMutations(double mut_multiplier=1);
-  void Inject_DoMutations(double mut_multiplier, cCPUMemory & injected_code);
-  void Divide_TestFitnessMeasures();
-  void Mutate(const int mut_point);
+  bool Divide_Main(cAvidaContext& ctx, const int mem_space_used, double mut_multiplier=1);
+  bool Divide_CheckViable(cAvidaContext& ctx, const int parent_size, const int child_size, const int mem_space);
+  void Divide_DoMutations(cAvidaContext& ctx, double mut_multiplier = 1);
+  void Inject_DoMutations(cAvidaContext& ctx, double mut_multiplier, cCPUMemory& injected_code);
+  void Divide_TestFitnessMeasures(cAvidaContext& ctx);
+  void Mutate(cAvidaContext& ctx, const int mut_point);
 
-  bool InjectParasite(double mut_multiplier);
+  bool InjectParasite(cAvidaContext& ctx, double mut_multiplier);
 
   bool HeadCopy_ErrorCorrect(double reduction);
   
@@ -168,8 +169,8 @@
   static cString GetDefaultInstFilename() { return "inst_lib.4stack"; }
 	
   void Reset();
-  void SingleProcess();
-  void ProcessBonusInst(const cInstruction & inst);
+  void SingleProcess(cAvidaContext& ctx);
+  void ProcessBonusInst(cAvidaContext& ctx, const cInstruction& inst);
 	
   // --------  Helper methods  --------
   int GetType() const { return HARDWARE_TYPE_CPU_SMT; }
@@ -238,47 +239,47 @@
 	
   
   // --------  Mutation  --------
-  int PointMutate(const double mut_rate);  
-  bool TriggerMutations(int trigger);
-  bool TriggerMutations(int trigger, cHeadCPU& cur_head);
+  int PointMutate(cAvidaContext& ctx, const double mut_rate);  
+  bool TriggerMutations(cAvidaContext& ctx, int trigger);
+  bool TriggerMutations(cAvidaContext& ctx, int trigger, cHeadCPU& cur_head);
 
 
 private:
   // ---------- Instruction Library -----------
-  bool Inst_ShiftR();
-  bool Inst_ShiftL();
-  bool Inst_Val_Nand();
-  bool Inst_Val_Add();
-  bool Inst_Val_Sub();
-  bool Inst_Val_Mult();
-  bool Inst_Val_Div();
-  bool Inst_Val_Mod();
-  bool Inst_Val_Inc();
-  bool Inst_Val_Dec();
-  bool Inst_SetMemory();
-  bool Inst_Divide();
-  bool Inst_HeadRead();
-  bool Inst_HeadWrite();
-  bool Inst_HeadCopy();
-  bool Inst_IfEqual();
-  bool Inst_IfNotEqual();
-  bool Inst_IfLess();
-  bool Inst_IfGreater();
-  bool Inst_HeadPush();
-  bool Inst_HeadPop();
-  bool Inst_HeadMove();
-  bool Inst_Search();
-  bool Inst_PushNext();
-  bool Inst_PushPrevious();
-  bool Inst_PushComplement();
-  bool Inst_ValDelete();
-  bool Inst_ValCopy();
-  bool Inst_ForkThread();
-  bool Inst_IfLabel();
-  bool Inst_KillThread();
-  bool Inst_IO();
-  bool Inst_Inject();
-  bool Inst_Apoptosis();
+  bool Inst_ShiftR(cAvidaContext& ctx);
+  bool Inst_ShiftL(cAvidaContext& ctx);
+  bool Inst_Val_Nand(cAvidaContext& ctx);
+  bool Inst_Val_Add(cAvidaContext& ctx);
+  bool Inst_Val_Sub(cAvidaContext& ctx);
+  bool Inst_Val_Mult(cAvidaContext& ctx);
+  bool Inst_Val_Div(cAvidaContext& ctx);
+  bool Inst_Val_Mod(cAvidaContext& ctx);
+  bool Inst_Val_Inc(cAvidaContext& ctx);
+  bool Inst_Val_Dec(cAvidaContext& ctx);
+  bool Inst_SetMemory(cAvidaContext& ctx);
+  bool Inst_Divide(cAvidaContext& ctx);
+  bool Inst_HeadRead(cAvidaContext& ctx);
+  bool Inst_HeadWrite(cAvidaContext& ctx);
+  bool Inst_HeadCopy(cAvidaContext& ctx);
+  bool Inst_IfEqual(cAvidaContext& ctx);
+  bool Inst_IfNotEqual(cAvidaContext& ctx);
+  bool Inst_IfLess(cAvidaContext& ctx);
+  bool Inst_IfGreater(cAvidaContext& ctx);
+  bool Inst_HeadPush(cAvidaContext& ctx);
+  bool Inst_HeadPop(cAvidaContext& ctx);
+  bool Inst_HeadMove(cAvidaContext& ctx);
+  bool Inst_Search(cAvidaContext& ctx);
+  bool Inst_PushNext(cAvidaContext& ctx);
+  bool Inst_PushPrevious(cAvidaContext& ctx);
+  bool Inst_PushComplement(cAvidaContext& ctx);
+  bool Inst_ValDelete(cAvidaContext& ctx);
+  bool Inst_ValCopy(cAvidaContext& ctx);
+  bool Inst_ForkThread(cAvidaContext& ctx);
+  bool Inst_IfLabel(cAvidaContext& ctx);
+  bool Inst_KillThread(cAvidaContext& ctx);
+  bool Inst_IO(cAvidaContext& ctx);
+  bool Inst_Inject(cAvidaContext& ctx);
+  bool Inst_Apoptosis(cAvidaContext& ctx);
 };
 
 

Modified: development/source/cpu/cTestCPU.cc
===================================================================
--- development/source/cpu/cTestCPU.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cTestCPU.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cTestCPU.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "cEnvironment.h"
 #include "functions.h"
@@ -87,7 +88,7 @@
 
 
 // NOTE: This method assumes that the organism is a fresh creation.
-bool cTestCPU::ProcessGestation(cCPUTestInfo & test_info, int cur_depth)
+bool cTestCPU::ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth)
 {
   assert(test_info.org_array[cur_depth] != NULL);
 
@@ -112,7 +113,7 @@
          organism.GetPhenotype().GetNumDivides() == 0)
   {
     time_used++;
-    organism.GetHardware().SingleProcess();
+    organism.GetHardware().SingleProcess(ctx);
     // @CAO Need to watch out for parasites.
   }
   organism.GetHardware().SetTrace(NULL);
@@ -135,19 +136,19 @@
 }
 
 
-bool cTestCPU::TestGenome(cCPUTestInfo & test_info, const cGenome & genome)
+bool cTestCPU::TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome)
 {
   test_info.Clear();
-  TestGenome_Body(test_info, genome, 0);
+  TestGenome_Body(ctx, test_info, genome, 0);
 
   return test_info.is_viable;
 }
 
-bool cTestCPU::TestGenome(cCPUTestInfo & test_info, const cGenome & genome,
-		       ofstream& out_fp)
+bool cTestCPU::TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome,
+                          ofstream& out_fp)
 {
   test_info.Clear();
-  TestGenome_Body(test_info, genome, 0);
+  TestGenome_Body(ctx, test_info, genome, 0);
 
   ////////////////////////////////////////////////////////////////
   // IsViable() == false
@@ -175,8 +176,8 @@
   return test_info.is_viable;
 }
 
-bool cTestCPU::TestGenome_Body(cCPUTestInfo & test_info,
-			       const cGenome & genome, int cur_depth)
+bool cTestCPU::TestGenome_Body(cAvidaContext& ctx, cCPUTestInfo& test_info,
+                               const cGenome& genome, int cur_depth)
 {
   assert(cur_depth < test_info.generation_tests);
 
@@ -213,7 +214,7 @@
   organism.GetPhenotype().SetupInject(genome.GetSize());
 
   // Run the current organism.
-  ProcessGestation(test_info, cur_depth);
+  ProcessGestation(ctx, test_info, cur_depth);
 
   // Must be able to divide twice in order to form a successful colony,
   // assuming the CPU doesn't get reset on divides.
@@ -254,7 +255,7 @@
   // If we haven't reached maximum depth yet, check out the child.
   if (cur_depth+1 < test_info.generation_tests) {
     // Run the child's genome.
-    return TestGenome_Body(test_info, organism.ChildGenome(), cur_depth+1);
+    return TestGenome_Body(ctx, test_info, organism.ChildGenome(), cur_depth+1);
   }
 
   // All options have failed; just return false.
@@ -262,20 +263,20 @@
 }
 
 
-void cTestCPU::TestThreads(const cGenome & genome)
+void cTestCPU::TestThreads(cAvidaContext& ctx, const cGenome& genome)
 {
   cCPUTestInfo test_info;
   test_info.TestThreads();
-  cTestCPU::TestGenome(test_info, genome);
+  cTestCPU::TestGenome(ctx, test_info, genome);
 }
 
 
-void cTestCPU::PrintThreads(const cGenome & genome)
+void cTestCPU::PrintThreads(cAvidaContext& ctx, const cGenome& genome)
 {
   cCPUTestInfo test_info;
   test_info.TestThreads();
   test_info.PrintThreads();
-  cTestCPU::TestGenome(test_info, genome);
+  cTestCPU::TestGenome(ctx, test_info, genome);
 }
 
 

Modified: development/source/cpu/cTestCPU.h
===================================================================
--- development/source/cpu/cTestCPU.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cTestCPU.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -23,6 +23,7 @@
 #include "cResourceCount.h"
 #endif
 
+class cAvidaContext;
 class cInstSet;
 class cResourceCount;
 class cCPUTestInfo;
@@ -54,8 +55,8 @@
   cTestResources* m_res;
   bool m_localres;
 
-  bool ProcessGestation(cCPUTestInfo & test_info, int cur_depth);
-  bool TestGenome_Body(cCPUTestInfo & test_info, const cGenome & genome, int cur_depth);
+  bool ProcessGestation(cAvidaContext& ctx, cCPUTestInfo& test_info, int cur_depth);
+  bool TestGenome_Body(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome, int cur_depth);
   void SetupResources(void);
 
   cTestCPU(); // @not_implemented
@@ -67,11 +68,11 @@
   cTestCPU(cWorld* world, cTestResources* res) : m_world(world), m_res(res), m_localres(false) { ; }
   ~cTestCPU() { if (m_localres) delete m_res; }
   
-  bool TestGenome(cCPUTestInfo & test_info, const cGenome & genome);
-  bool TestGenome(cCPUTestInfo & test_info, const cGenome & genome, std::ofstream& out_fp);
+  bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome& genome);
+  bool TestGenome(cAvidaContext& ctx, cCPUTestInfo& test_info, const cGenome & genome, std::ofstream& out_fp);
 
-  void TestThreads(const cGenome & genome);
-  void PrintThreads(const cGenome & genome);
+  void TestThreads(cAvidaContext& ctx, const cGenome& genome);
+  void PrintThreads(cAvidaContext& ctx, const cGenome& genome);
 
   // Test if a genome has any chance of being a replicator (i.e., in the
   // default set, has an allocate, a copy, and a divide).

Modified: development/source/cpu/cTestUtil.cc
===================================================================
--- development/source/cpu/cTestUtil.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/cpu/cTestUtil.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cTestUtil.h"
 
+#include "cAvidaContext.h"
 #include "cCPUTestInfo.h"
 #include "cDataFileManager.h"
 #include "cEnvironment.h"
@@ -39,9 +40,13 @@
 
   // Build the test info for printing.
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+  
   cCPUTestInfo test_info;
   test_info.TestThreads();
-  testcpu->TestGenome(test_info, genome);
+  testcpu->TestGenome(ctx, test_info, genome);
   delete testcpu;
 
   // Open the file...
@@ -141,9 +146,13 @@
 
   // Build the test info for printing.
   cTestCPU* testcpu = world->GetHardwareManager().CreateTestCPU();
+  
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   cCPUTestInfo test_info;
   test_info.TestThreads();
-  testcpu->TestGenome(test_info, genome);
+  testcpu->TestGenome(ctx, test_info, genome);
   delete testcpu;
 
   // Open the file...

Modified: development/source/drivers/cDefaultRunDriver.cc
===================================================================
--- development/source/drivers/cDefaultRunDriver.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/drivers/cDefaultRunDriver.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -9,6 +9,7 @@
 
 #include "cDefaultRunDriver.h"
 
+#include "cAvidaContext.h"
 #include "cChangeList.h"
 #include "cClassificationManager.h"
 #include "cDriverManager.h"
@@ -49,6 +50,8 @@
   
   const int ave_time_slice = m_world->GetConfig().AVE_TIME_SLICE.Get();
   const double point_mut_prob = m_world->GetConfig().POINT_MUT_PROB.Get();
+  
+  cAvidaContext ctx(0);
 
   while (!m_done) {
     if (cChangeList* change_list = population.GetChangeList()) {
@@ -84,7 +87,7 @@
         m_done = true;
         break;
       }
-      population.ProcessStep(step_size);
+      population.ProcessStep(ctx, step_size);
     }
     
 
@@ -106,7 +109,7 @@
     if (point_mut_prob > 0 ) {
       for (int i = 0; i < population.GetSize(); i++) {
         if (population.GetCell(i).IsOccupied()) {
-          population.GetCell(i).GetOrganism()->GetHardware().PointMutate(point_mut_prob);
+          population.GetCell(i).GetOrganism()->GetHardware().PointMutate(ctx, point_mut_prob);
         }
       }
     }

Modified: development/source/event/cEventManager.cc
===================================================================
--- development/source/event/cEventManager.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/event/cEventManager.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -11,6 +11,7 @@
 #include "cEventManager.h"
 
 #include "cAnalyzeUtil.h"
+#include "cAvidaContext.h"
 #include "avida.h"
 #include "cClassificationManager.h"
 #include "cEnvironment.h"
@@ -1862,8 +1863,12 @@
   ///// inject_random /////
   void Process(){
     if (cell_id == -1) cell_id = m_world->GetRandom().GetUInt(m_world->GetPopulation().GetSize());
+    
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+    
     cGenome genome =
-      cInstUtil::RandomGenome(length, m_world->GetHardwareManager().GetInstSet());
+      cInstUtil::RandomGenome(ctx, length, m_world->GetHardwareManager().GetInstSet());
     m_world->GetPopulation().Inject(genome, cell_id, merit, lineage_label, neutral_metric);
   }
 };
@@ -2282,9 +2287,12 @@
   }
   ///// predict_w_landscape /////
   void Process(){
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
     cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.PredictWProcess(m_world->GetDataFileOFStream(datafile));
+    landscape.PredictWProcess(ctx, m_world->GetDataFileOFStream(datafile));
   }
 };
 
@@ -2310,9 +2318,12 @@
   }
   ///// predict_nu_landscape /////
   void Process(){
-    cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
+    cGenome& genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.PredictNuProcess(m_world->GetDataFileOFStream(datafile));
+    landscape.PredictNuProcess(ctx, m_world->GetDataFileOFStream(datafile));
   }
 };
 
@@ -2338,10 +2349,13 @@
   }
   ///// sample_landscape /////
   void Process(){
-    cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
+    cGenome& genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
     if (sample_size == 0) sample_size = m_world->GetHardwareManager().GetInstSet().GetSize() - 1;
-    landscape.SampleProcess(sample_size);
+    landscape.SampleProcess(ctx, sample_size);
     landscape.PrintStats(m_world->GetDataFileOFStream("land-sample.dat"), m_world->GetStats().GetUpdate());
   }
 };
@@ -2376,9 +2390,12 @@
   }
   ///// random_landscape /////
   void Process(){
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
     cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.RandomProcess(sample_size, landscape_dist, min_found,
+    landscape.RandomProcess(ctx, sample_size, landscape_dist, min_found,
                             max_sample_size, print_if_found);
     landscape.PrintStats(m_world->GetDataFileOFStream("land-random.dat"), m_world->GetStats().GetUpdate());
   }
@@ -2881,9 +2898,12 @@
     m_args = in_args; }
   ///// hillclimb /////
   void Process(){
-    cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
+    cGenome& genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.HillClimb(m_world->GetDataFileOFStream("hillclimb.dat"));
+    landscape.HillClimb(ctx, m_world->GetDataFileOFStream("hillclimb.dat"));
     m_world->GetDataFileManager().Remove("hillclimb.dat");
   }
 };
@@ -2906,9 +2926,12 @@
     m_args = in_args; }
   ///// hillclimb_neut /////
   void Process(){
-    cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
+    cGenome& genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.HillClimb_Neut(m_world->GetDataFileOFStream("hillclimb.dat"));
+    landscape.HillClimb_Neut(ctx, m_world->GetDataFileOFStream("hillclimb.dat"));
     m_world->GetDataFileManager().Remove("hillclimb.dat");
   }
 };
@@ -2931,9 +2954,12 @@
     m_args = in_args; }
   ///// hillclimb_rand /////
   void Process(){
-    cGenome & genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
+    cGenome& genome = m_world->GetClassificationManager().GetBestGenotype()->GetGenome();
     cLandscape landscape(m_world, genome, m_world->GetHardwareManager().GetInstSet());
-    landscape.HillClimb_Rand(m_world->GetDataFileOFStream("hillclimb.dat"));
+    landscape.HillClimb_Rand(ctx, m_world->GetDataFileOFStream("hillclimb.dat"));
     m_world->GetDataFileManager().Remove("hillclimb.dat");
   }
 };
@@ -3133,8 +3159,11 @@
   
   ///// test_threads /////
   void Process(){
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
     cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-    testcpu->TestThreads(m_world->GetClassificationManager().GetBestGenotype()->GetGenome());
+    testcpu->TestThreads(ctx, m_world->GetClassificationManager().GetBestGenotype()->GetGenome());
     delete testcpu;
   }
 };
@@ -3158,8 +3187,11 @@
   }
   ///// print_threads /////
   void Process(){
+    // @DMB - Warning: Creating context out of band.
+    cAvidaContext ctx(0);
+
     cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
-    testcpu->PrintThreads( m_world->GetClassificationManager().GetBestGenotype()->GetGenome() );
+    testcpu->PrintThreads(ctx, m_world->GetClassificationManager().GetBestGenotype()->GetGenome() );
     delete testcpu;
   }
 };

Added: development/source/main/cAvidaContext.h
===================================================================
--- development/source/main/cAvidaContext.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cAvidaContext.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -0,0 +1,25 @@
+/*
+ *  cAvidaContext.h
+ *  Avida
+ *
+ *  Created by David on 3/13/06.
+ *  Copyright 2006 Michigan State University. All rights reserved.
+ *
+ */
+
+#ifndef cAvidaContext_h
+#define cAvidaContext_h
+
+class cAvidaContext
+{
+private:
+  int m_rng_id;
+  
+public:
+  cAvidaContext(int rng_id) : m_rng_id(rng_id) { ; }
+  ~cAvidaContext() { ; }
+  
+  int GetRandomID() { return m_rng_id; }
+};
+
+#endif

Modified: development/source/main/cFitnessMatrix.cc
===================================================================
--- development/source/main/cFitnessMatrix.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cFitnessMatrix.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cFitnessMatrix.h"
 
+#include "cAvidaContext.h"
 #include "cInstSet.h"
 #include "cWorld.h"
 
@@ -20,8 +21,11 @@
   :  m_world(world), m_start_genotype(m_world, code), m_inst_set( inst_set ), m_DFS_MaxDepth(0),
      m_DFS_NumRecorded(0)
 {
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+  
   m_start_genotype.SetNumInstructions( m_inst_set->GetSize());
-  m_start_genotype.CalcFitness();
+  m_start_genotype.CalcFitness(ctx);
   m_data_set.insert(m_start_genotype);
 }
 
@@ -121,6 +125,9 @@
 
   // MyCodeArrayLessThan myLess;
 
+  // @DMB - Warning: Creating context out of band.
+  cAvidaContext ctx(0);
+
   for (list_iter = theMutants.begin(); list_iter != theMutants.end(); list_iter++)
     {
       // check if its already in the data set
@@ -135,7 +142,7 @@
       if (data_iter == m_data_set.end())
 	{
 
-	  list_iter->CalcFitness();
+	  list_iter->CalcFitness(ctx);
 	  double fitness = list_iter->GetFitness();
 	
 	  if (fitness == 0.0)

Modified: development/source/main/cInstSet.cc
===================================================================
--- development/source/main/cInstSet.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cInstSet.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cInstSet.h"
 
+#include "cAvidaContext.h"
 #include "cStringUtil.h"
 #include "cWorld.h"
 
@@ -67,7 +68,7 @@
   return true;
 }
 
-cInstruction cInstSet::GetRandomInst() const
+cInstruction cInstSet::GetRandomInst(cAvidaContext& ctx) const
 {
   int inst_op = m_mutation_chart[m_world->GetRandom().GetUInt(m_mutation_chart.GetSize())];
   return cInstruction(inst_op);

Modified: development/source/main/cInstSet.h
===================================================================
--- development/source/main/cInstSet.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cInstSet.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -36,6 +36,7 @@
  * attach different cInstSet objects to different hardware.
  **/
 
+class cAvidaContext;
 class cWorld;
 
 class cInstSet
@@ -113,10 +114,10 @@
     return m_inst_lib->GetNopMod(nopmod);
   }
 
-  cInstruction GetRandomInst() const;
-  int GetRandFunctionIndex() const
+  cInstruction GetRandomInst(cAvidaContext& ctx) const;
+  int GetRandFunctionIndex(cAvidaContext& ctx) const
   {
-    return m_lib_name_map[ GetRandomInst().GetOp() ].lib_fun_id;
+    return m_lib_name_map[ GetRandomInst(ctx).GetOp() ].lib_fun_id;
   }
 
   int GetSize() const {

Modified: development/source/main/cInstUtil.cc
===================================================================
--- development/source/main/cInstUtil.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cInstUtil.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -24,8 +24,7 @@
 using namespace std;
 
 
-cGenome cInstUtil::LoadGenome(const cString & filename,
-			      const cInstSet & inst_set)
+cGenome cInstUtil::LoadGenome(const cString& filename, const cInstSet& inst_set)
 {
     cInitFile input_file(filename);
   if (!input_file.IsOpen()) {
@@ -59,7 +58,7 @@
   return new_genome;
 }
 
-cGenome cInstUtil::LoadInternalGenome(istream & fp, const cInstSet &inst_set)
+cGenome cInstUtil::LoadInternalGenome(istream& fp, const cInstSet& inst_set)
 {
   assert(fp.good()); // Invalid stream to load genome from!
 
@@ -85,7 +84,7 @@
   return new_genome;
 }
 
-void cInstUtil::SaveGenome(ostream& fp, const cInstSet & inst_set,
+void cInstUtil::SaveGenome(ostream& fp, const cInstSet& inst_set,
 			   const cGenome & gen)
 {
   for (int i = 0; i < gen.GetSize(); i++) {
@@ -93,7 +92,7 @@
   }
 }
 
-void cInstUtil::SaveInternalGenome(ostream& fp, const cInstSet & inst_set,
+void cInstUtil::SaveInternalGenome(ostream& fp, const cInstSet& inst_set,
 				   const cGenome & gen)
 {
   fp << gen.GetSize() << endl;
@@ -101,11 +100,11 @@
 }
 
 
-cGenome cInstUtil::RandomGenome(int length, const cInstSet & inst_set)
+cGenome cInstUtil::RandomGenome(cAvidaContext& ctx, int length, const cInstSet& inst_set)
 {
   cGenome genome(length);
   for (int i = 0; i < length; i++) {
-    genome[i] = inst_set.GetRandomInst();
+    genome[i] = inst_set.GetRandomInst(ctx);
   }
   return genome;
 }

Modified: development/source/main/cInstUtil.h
===================================================================
--- development/source/main/cInstUtil.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cInstUtil.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -17,6 +17,7 @@
 #include "cGenome.h"
 #endif
 
+class cAvidaContext;
 class cInitFile;
 class cInstSet;
 class cString;
@@ -41,7 +42,7 @@
 				 const cGenome &gen);
 
   // ========= Genome Construction =========
-  static cGenome RandomGenome(int length, const cInstSet & inst_set);
+  static cGenome RandomGenome(cAvidaContext& ctx, int length, const cInstSet & inst_set);
 };
 
 #endif

Modified: development/source/main/cLandscape.cc
===================================================================
--- development/source/main/cLandscape.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cLandscape.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -81,9 +81,9 @@
   neut_max = 0.0;
 }
 
-void cLandscape::ProcessGenome(cTestCPU* testcpu, cGenome & in_genome)
+void cLandscape::ProcessGenome(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& in_genome)
 {
-  testcpu->TestGenome(test_info, in_genome);
+  testcpu->TestGenome(ctx, test_info, in_genome);
   
   test_fitness = test_info.GetColonyFitness();
   
@@ -107,11 +107,11 @@
   }
 }
 
-void cLandscape::ProcessBase(cTestCPU* testcpu)
+void cLandscape::ProcessBase(cAvidaContext& ctx, cTestCPU* testcpu)
 {
   // Collect info on base creature.
   
-  testcpu->TestGenome(test_info, base_genome);
+  testcpu->TestGenome(ctx, test_info, base_genome);
   
   cPhenotype & phenotype = test_info.GetColonyOrganism()->GetPhenotype();
   base_fitness = test_info.GetColonyFitness();
@@ -126,17 +126,17 @@
   
 }
 
-void cLandscape::Process(int in_distance)
+void cLandscape::Process(cAvidaContext& ctx, int in_distance)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   distance = in_distance;
   
   // Get the info about the base creature.
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   
   // Now Process the new creature at the proper distance.
-  Process_Body(testcpu, base_genome, distance, 0);
+  Process_Body(ctx, testcpu, base_genome, distance, 0);
 
   delete testcpu;
   
@@ -155,7 +155,8 @@
 
 // For distances greater than one, this needs to be called recursively.
 
-void cLandscape::Process_Body(cTestCPU* testcpu, cGenome& cur_genome, int cur_distance, int start_line)
+void cLandscape::Process_Body(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& cur_genome,
+                              int cur_distance, int start_line)
 {
   const int max_line = base_genome.GetSize() - cur_distance + 1;
   const int inst_size = inst_set.GetSize();
@@ -172,10 +173,10 @@
       
       mod_genome[line_num].SetOp(inst_num);
       if (cur_distance <= 1) {
-        ProcessGenome(testcpu, mod_genome);
+        ProcessGenome(ctx, testcpu, mod_genome);
         if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
       } else {
-        Process_Body(testcpu, mod_genome, cur_distance - 1, line_num + 1);
+        Process_Body(ctx, testcpu, mod_genome, cur_distance - 1, line_num + 1);
       }
     }
     
@@ -184,12 +185,12 @@
   
 }
 
-void cLandscape::ProcessDelete()
+void cLandscape::ProcessDelete(cAvidaContext& ctx)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   // Get the info about the base creature.
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   
   const int max_line = base_genome.GetSize();
   cCPUMemory mod_genome(base_genome);
@@ -198,7 +199,7 @@
   for (int line_num = 0; line_num < max_line; line_num++) {
     int cur_inst = base_genome[line_num].GetOp();
     mod_genome.Remove(line_num);
-    ProcessGenome(testcpu, mod_genome);
+    ProcessGenome(ctx, testcpu, mod_genome);
     if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
     mod_genome.Insert(line_num, cInstruction(cur_inst));
   }
@@ -206,12 +207,12 @@
   delete testcpu;
 }
 
-void cLandscape::ProcessInsert()
+void cLandscape::ProcessInsert(cAvidaContext& ctx)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   // Get the info about the base creature.
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   
   const int max_line = base_genome.GetSize();
   const int inst_size = inst_set.GetSize();
@@ -223,7 +224,7 @@
     // Loop through all instructions...
     for (int inst_num = 0; inst_num < inst_size; inst_num++) {
       mod_genome.Insert(line_num, cInstruction(inst_num));
-      ProcessGenome(testcpu, mod_genome);
+      ProcessGenome(ctx, testcpu, mod_genome);
       if (test_info.GetColonyFitness() >= neut_min) site_count[line_num]++;
       mod_genome.Remove(line_num);
     }
@@ -233,17 +234,17 @@
 }
 
 // Prediction for a landscape where n sites are _randomized_.
-void cLandscape::PredictWProcess(ostream& fp, int update)
+void cLandscape::PredictWProcess(cAvidaContext& ctx, ostream& fp, int update)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   distance = 1;
   
   // Get the info about the base creature.
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   if (base_fitness == 0.0) return;
   
-  BuildFitnessChart(testcpu);
+  BuildFitnessChart(ctx, testcpu);
   const int genome_size = fitness_chart.GetNumRows();
   const int inst_size = fitness_chart.GetNumCols();
   const double min_neut_fitness = 0.99;
@@ -354,17 +355,17 @@
 
 
 // Prediction for a landscape where n sites are _mutated_.
-void cLandscape::PredictNuProcess(ostream& fp, int update)
+void cLandscape::PredictNuProcess(cAvidaContext& ctx, ostream& fp, int update)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   distance = 1;
   
   // Get the info about the base creature.
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   if (base_fitness == 0.0) return;
   
-  BuildFitnessChart(testcpu);
+  BuildFitnessChart(ctx, testcpu);
   const int genome_size = fitness_chart.GetNumRows();
   const int inst_size = fitness_chart.GetNumCols();
   const double min_neut_fitness = 0.99;
@@ -491,7 +492,7 @@
 }
 
 
-void cLandscape::SampleProcess(int in_trials)
+void cLandscape::SampleProcess(cAvidaContext& ctx, int in_trials)
 {
   trials = in_trials;  // Trials _per line_
   distance = 1;
@@ -501,7 +502,7 @@
 
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   
   // Loop through all the lines of genome, testing each line.
   for (int line_num = 0; line_num < genome_size; line_num++) {
@@ -509,12 +510,12 @@
     
     for (int i = 0; i < trials; i++) {
       // Choose the new instruction for that line...
-      cInstruction new_inst( inst_set.GetRandomInst() );
+      cInstruction new_inst( inst_set.GetRandomInst(ctx) );
       if (cur_inst == new_inst) { i--; continue; }
       
       // Make the change, and test it!
       mod_genome[line_num] = new_inst;
-      ProcessGenome(testcpu, mod_genome);
+      ProcessGenome(ctx, testcpu, mod_genome);
     }
     
     mod_genome[line_num] = cur_inst;
@@ -524,7 +525,7 @@
 }
 
 
-int cLandscape::RandomProcess(int in_trials, int in_distance, int min_found,
+int cLandscape::RandomProcess(cAvidaContext& ctx, int in_trials, int in_distance, int min_found,
                               int max_trials, bool print_if_found)
 {
   distance = in_distance;
@@ -534,7 +535,7 @@
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   
   int mut_num;
   tArray<int> mut_lines(distance);
@@ -556,7 +557,7 @@
     
     // Choose the new instructions for those lines...
     for (mut_num = 0; mut_num < distance; mut_num++) {
-      const cInstruction new_inst( inst_set.GetRandomInst() );
+      const cInstruction new_inst( inst_set.GetRandomInst(ctx) );
       const cInstruction & cur_inst = base_genome[ mut_lines[mut_num] ];
       if (cur_inst == new_inst) {
         mut_num--;
@@ -568,7 +569,7 @@
     
     // And test it!
     
-    ProcessGenome(testcpu, mod_genome);
+    ProcessGenome(ctx, testcpu, mod_genome);
     
     
     // And reset the genome.
@@ -584,7 +585,7 @@
   return total_found;
 }
 
-void cLandscape::BuildFitnessChart(cTestCPU* testcpu)
+void cLandscape::BuildFitnessChart(cAvidaContext& ctx, cTestCPU* testcpu)
 {
   // First, resize the fitness_chart.
   const int max_line = base_genome.GetSize();
@@ -605,7 +606,7 @@
       }
       
       mod_genome[line_num].SetOp(inst_num);
-      ProcessGenome(testcpu, mod_genome);
+      ProcessGenome(ctx, testcpu, mod_genome);
       fitness_chart(line_num, inst_num) = test_info.GetColonyFitness();
     }
     
@@ -613,16 +614,16 @@
   }
 }
 
-void cLandscape::TestPairs(int in_trials, ostream& fp)
+void cLandscape::TestPairs(cAvidaContext& ctx, int in_trials, ostream& fp)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
   trials = in_trials;
   
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   if (base_fitness == 0.0) return;
   
-  BuildFitnessChart(testcpu);
+  BuildFitnessChart(ctx, testcpu);
   
   cGenome mod_genome(base_genome);
   const int genome_size = base_genome.GetSize();
@@ -637,7 +638,7 @@
     
     // Choose the new instructions for those lines...
     for (int mut_num = 0; mut_num < 2; mut_num++) {
-      const cInstruction new_inst( inst_set.GetRandomInst() );
+      const cInstruction new_inst( inst_set.GetRandomInst(ctx) );
       const cInstruction & cur_inst = base_genome[ mut_lines[mut_num] ];
       if (cur_inst == new_inst) {
         mut_num--;
@@ -647,7 +648,7 @@
       mut_insts[mut_num] = new_inst;
     }
     
-    TestMutPair(testcpu, mod_genome, mut_lines[0], mut_lines[1], mut_insts[0],
+    TestMutPair(ctx, testcpu, mod_genome, mut_lines[0], mut_lines[1], mut_insts[0],
                 mut_insts[1], fp);
     
   }
@@ -655,14 +656,14 @@
 }
 
 
-void cLandscape::TestAllPairs(ostream& fp)
+void cLandscape::TestAllPairs(cAvidaContext& ctx, ostream& fp)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
 
-  ProcessBase(testcpu);
+  ProcessBase(ctx, testcpu);
   if (base_fitness == 0.0) return;
   
-  BuildFitnessChart(testcpu);
+  BuildFitnessChart(ctx, testcpu);
   
   const int max_line = base_genome.GetSize();
   const int inst_size = inst_set.GetSize();
@@ -680,7 +681,7 @@
         for (int inst2_num = 0; inst2_num < inst_size; inst2_num++) {
           inst2.SetOp(inst2_num);
           if (inst2 == base_genome[line2_num]) continue;
-          TestMutPair(testcpu, mod_genome, line1_num, line2_num, inst1, inst2, fp);
+          TestMutPair(ctx, testcpu, mod_genome, line1_num, line2_num, inst1, inst2, fp);
         } // inst2_num loop
       } //inst1_num loop;
       
@@ -691,16 +692,16 @@
 }
 
 
-void cLandscape::HillClimb(ofstream& fp)
+void cLandscape::HillClimb(cAvidaContext& ctx, ofstream& fp)
 {
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   cGenome cur_genome(base_genome);
   int gen = 0;
-  HillClimb_Body(testcpu, fp, cur_genome, gen);
+  HillClimb_Body(ctx, testcpu, fp, cur_genome, gen);
   delete testcpu;
 }
 
-void cLandscape::HillClimb_Body(cTestCPU* testcpu, ofstream& fp, cGenome & cur_genome,
+void cLandscape::HillClimb_Body(cAvidaContext& ctx, cTestCPU* testcpu, ofstream& fp, cGenome & cur_genome,
                                 int & gen)
 {
   cCPUMemory mod_genome(base_genome);
@@ -718,7 +719,7 @@
     const int max_line = cur_genome.GetSize();
     
     // Try all Mutations...
-    Process(1);
+    Process(ctx, 1);
     
     // Try Insertion Mutations.
     
@@ -727,7 +728,7 @@
       // Loop through all instructions...
       for (int inst_num = 0; inst_num < inst_size; inst_num++) {
         mod_genome.Insert(line_num, cInstruction(inst_num));
-        ProcessGenome(testcpu, mod_genome);
+        ProcessGenome(ctx, testcpu, mod_genome);
         mod_genome.Remove(line_num);
       }
     }
@@ -737,14 +738,14 @@
     for (int line_num = 0; line_num < max_line; line_num++) {
       int cur_inst = cur_genome[line_num].GetOp();
       mod_genome.Remove(line_num);
-      ProcessGenome(testcpu, mod_genome);
+      ProcessGenome(ctx, testcpu, mod_genome);
       mod_genome.Insert(line_num, cInstruction(cur_inst));
     }
     
     pos_frac = GetProbPos();
     
     // Print the information on the current best.
-    HillClimb_Print(testcpu, fp, cur_genome, gen);
+    HillClimb_Print(ctx, testcpu, fp, cur_genome, gen);
     
     // Move on to the peak genome found.
     cur_genome = GetPeakGenome();
@@ -752,18 +753,19 @@
   }
 }
 
-void cLandscape::HillClimb_Neut(ofstream& fp)
+void cLandscape::HillClimb_Neut(cAvidaContext& ctx, ofstream& fp)
 {
 }
 
-void cLandscape::HillClimb_Rand(ofstream& fp)
+void cLandscape::HillClimb_Rand(cAvidaContext& ctx, ofstream& fp)
 {
 }
 
-void cLandscape::HillClimb_Print(cTestCPU* testcpu, ofstream& fp, const cGenome & _genome, const int gen) const
+void cLandscape::HillClimb_Print(cAvidaContext& ctx, cTestCPU* testcpu, ofstream& fp,
+                                 const cGenome& _genome, const int gen) const
 {
   cCPUTestInfo test_info;
-  testcpu->TestGenome(test_info, _genome);
+  testcpu->TestGenome(ctx, test_info, _genome);
   cPhenotype &colony_phenotype = test_info.GetColonyOrganism()->GetPhenotype();
   fp << gen << " "
     << colony_phenotype.GetMerit().GetDouble() << " "
@@ -777,12 +779,12 @@
     << endl;
 }
 
-double cLandscape::TestMutPair(cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2,
+double cLandscape::TestMutPair(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2,
                                const cInstruction& mut1, const cInstruction& mut2, ostream& fp)
 {
   mod_genome[line1] = mut1;
   mod_genome[line2] = mut2;
-  testcpu->TestGenome(test_info, mod_genome);
+  testcpu->TestGenome(ctx, test_info, mod_genome);
   double combo_fitness = test_info.GetColonyFitness() / base_fitness;
   
   mod_genome[line1] = base_genome[line1];

Modified: development/source/main/cLandscape.h
===================================================================
--- development/source/main/cLandscape.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cLandscape.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -26,6 +26,7 @@
 
 #include <fstream>
 
+class cAvidaContext;
 class cInstSet;
 class cInstruction;
 class cWorld;
@@ -84,19 +85,20 @@
   tMatrix<double> fitness_chart; // Chart of all one-step mutations.
 
 
-  void BuildFitnessChart(cTestCPU* testcpu);
-  void ProcessGenome(cTestCPU* testcpu, cGenome& in_genome);
-  void ProcessBase(cTestCPU* testcpu);
-  void Process_Body(cTestCPU* testcpu, cGenome& cur_genome, int cur_distance, int start_line);
+  void BuildFitnessChart(cAvidaContext& ctx, cTestCPU* testcpu);
+  void ProcessGenome(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& in_genome);
+  void ProcessBase(cAvidaContext& ctx, cTestCPU* testcpu);
+  void Process_Body(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& cur_genome, int cur_distance, int start_line);
 
-  void HillClimb_Body(cTestCPU* testcpu, std::ofstream& fp, cGenome& cur_genome, int& gen);
-  void HillClimb_Print(cTestCPU* testcpu, std::ofstream& fp, const cGenome& _genome, const int gen) const;
+  void HillClimb_Body(cAvidaContext& ctx, cTestCPU* testcpu, std::ofstream& fp, cGenome& cur_genome, int& gen);
+  void HillClimb_Print(cAvidaContext& ctx, cTestCPU* testcpu, std::ofstream& fp,
+                       const cGenome& _genome, const int gen) const;
 
-  double TestMutPair(cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2, const cInstruction& mut1,
-                     const cInstruction& mut2, std::ostream& fp);
+  double TestMutPair(cAvidaContext& ctx, cTestCPU* testcpu, cGenome& mod_genome, int line1, int line2,
+                     const cInstruction& mut1, const cInstruction& mut2, std::ostream& fp);
 
   cLandscape(); // @not_implemented
-  cLandscape(const cLandscape &); // @not_implemented
+  cLandscape(const cLandscape&); // @not_implemented
   cLandscape& operator=(const cLandscape&); // @not_implemented
 
 public:
@@ -105,21 +107,22 @@
 
   void Reset(const cGenome& in_genome);
 
-  void Process(int in_distance = 1);
-  void ProcessDelete();
-  void ProcessInsert();
-  void PredictWProcess(std::ostream& fp, int update = -1);
-  void PredictNuProcess(std::ostream& fp, int update = -1);
+  void Process(cAvidaContext& ctx, int in_distance = 1);
+  void ProcessDelete(cAvidaContext& ctx);
+  void ProcessInsert(cAvidaContext& ctx);
+  void PredictWProcess(cAvidaContext& ctx, std::ostream& fp, int update = -1);
+  void PredictNuProcess(cAvidaContext& ctx, std::ostream& fp, int update = -1);
 
-  void SampleProcess(int in_trials);
-  int RandomProcess(int in_trials, int in_distance = 1, int min_found = 0, int max_trials = 0, bool print_if_found = false);
+  void SampleProcess(cAvidaContext& ctx, int in_trials);
+  int RandomProcess(cAvidaContext& ctx, int in_trials, int in_distance = 1, int min_found = 0,
+                    int max_trials = 0, bool print_if_found = false);
 
-  void TestPairs(int in_trials, std::ostream& fp);
-  void TestAllPairs(std::ostream& fp);
+  void TestPairs(cAvidaContext& ctx, int in_trials, std::ostream& fp);
+  void TestAllPairs(cAvidaContext& ctx, std::ostream& fp);
 
-  void HillClimb(std::ofstream& fp);
-  void HillClimb_Neut(std::ofstream& fp);
-  void HillClimb_Rand(std::ofstream& fp);
+  void HillClimb(cAvidaContext& ctx, std::ofstream& fp);
+  void HillClimb_Neut(cAvidaContext& ctx, std::ofstream& fp);
+  void HillClimb_Rand(cAvidaContext& ctx, std::ofstream& fp);
 
   void PrintStats(std::ofstream& fp, int update = -1);
   void PrintEntropy(std::ofstream& fp);

Modified: development/source/main/cMxCodeArray.cc
===================================================================
--- development/source/main/cMxCodeArray.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cMxCodeArray.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -449,14 +449,14 @@
 }
 
 
-void cMxCodeArray::CalcFitness()
+void cMxCodeArray::CalcFitness(cAvidaContext& ctx)
 {
   cGenome temp(1);
   
   cTestCPU* testcpu = m_world->GetHardwareManager().CreateTestCPU();
   cCPUTestInfo test_info;
   CopyDataTo(temp); 
-  testcpu->TestGenome(test_info, temp);
+  testcpu->TestGenome(ctx, test_info, temp);
   delete testcpu;
   
   if ( test_info.IsViable() )

Modified: development/source/main/cMxCodeArray.h
===================================================================
--- development/source/main/cMxCodeArray.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cMxCodeArray.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -31,6 +31,7 @@
  * Cannot be run in a simulation.
  **/
 
+class cAvidaContext;
 class cGenome;
 class MyCodeArrayLessThan;
 class cWorld;
@@ -72,7 +73,7 @@
     else return m_merit/m_gestation_time;}
   double GetMerit() const { return m_merit; }
   double GetGestationTime() const { return m_gestation_time; }
-  void CalcFitness();
+  void CalcFitness(cAvidaContext& ctx);
 
   inline const cInstruction & Get(int index) const {
     assert(index < size);

Modified: development/source/main/cOrganism.cc
===================================================================
--- development/source/main/cOrganism.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cOrganism.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -109,7 +109,7 @@
   phenotype.TestInput(input_buf, output_buf);
 }
 
-void cOrganism::DoOutput(const int value)
+void cOrganism::DoOutput(cAvidaContext& ctx, const int value)
 {
   assert(m_interface);
   const tArray<double> & resource_count = m_interface->GetResources();
@@ -152,7 +152,7 @@
 
   for (int i = 0; i < insts_triggered.GetSize(); i++) {
     const int cur_inst = insts_triggered[i];
-    hardware->ProcessBonusInst( cInstruction(cur_inst) );
+    hardware->ProcessBonusInst(ctx, cInstruction(cur_inst) );
   }
 }
 

Modified: development/source/main/cOrganism.h
===================================================================
--- development/source/main/cOrganism.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cOrganism.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -44,6 +44,7 @@
  * about a creature.  Effectively the chemistry acting on the genome.
  **/
 
+class cAvidaContext;
 class cHardwareBase;
 class cGenotype;
 class cInjectGenotype;
@@ -124,7 +125,7 @@
   
   // Input & Output Testing
   void DoInput(const int value);
-  void DoOutput(const int value);
+  void DoOutput(cAvidaContext& ctx, const int value);
 
   // Message stuff
   void SendMessage(cOrgMessage & mess);

Modified: development/source/main/cPopulation.cc
===================================================================
--- development/source/main/cPopulation.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cPopulation.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -10,6 +10,7 @@
 
 #include "cPopulation.h"
 
+#include "cAvidaContext.h"
 #include "cChangeList.h"
 #include "cClassificationManager.h"
 #include "cCodeLabel.h"
@@ -1048,7 +1049,7 @@
   return schedule->GetNextID();
 }
 
-void cPopulation::ProcessStep(double step_size, int cell_id)
+void cPopulation::ProcessStep(cAvidaContext& ctx, double step_size, int cell_id)
 {
   assert(step_size > 0.0);
   assert(cell_id < cell_array.GetSize());
@@ -1056,11 +1057,11 @@
   // If cell_id is negative, no cell could be found -- stop here.
   if (cell_id < 0) return;
   
-  cPopulationCell & cell = GetCell(cell_id);
+  cPopulationCell& cell = GetCell(cell_id);
   assert(cell.IsOccupied()); // Unoccupied cell getting processor time!
   
-  cOrganism * cur_org = cell.GetOrganism();
-  cur_org->GetHardware().SingleProcess();
+  cOrganism* cur_org = cell.GetOrganism();
+  cur_org->GetHardware().SingleProcess(ctx);
   if (cur_org->GetPhenotype().GetToDelete() == true) {
     delete cur_org;
   }
@@ -1749,7 +1750,7 @@
       schedule = new cConstSchedule(cell_array.GetSize());
       break;
     case SLICE_PROB_MERIT:
-      schedule = new cProbSchedule(m_world, cell_array.GetSize());
+      schedule = new cProbSchedule(cell_array.GetSize(), m_world->GetRandom().GetInt(1000000000));
       break;
     case SLICE_INTEGRATED_MERIT:
       schedule = new cIntegratedSchedule(cell_array.GetSize());

Modified: development/source/main/cPopulation.h
===================================================================
--- development/source/main/cPopulation.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/main/cPopulation.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -38,6 +38,7 @@
 #include "tList.h"
 #endif
 
+class cAvidaContext;
 class cSchedule;
 class cChangeList;
 class cEnvironment;
@@ -136,8 +137,8 @@
 
   // Process a single organism one instruction...
   int ScheduleOrganism();          // Determine next organism to be processed.
-  void ProcessStep(double step_size, int cell_id);
-  void ProcessStep(double step_size) { ProcessStep(step_size, ScheduleOrganism()); }
+  void ProcessStep(cAvidaContext& ctx, double step_size, int cell_id);
+  void ProcessStep(cAvidaContext& ctx, double step_size) { ProcessStep(ctx, step_size, ScheduleOrganism()); }
 
   // Calculate the statistics from the most recent update.
   void CalcUpdateStats();

Modified: development/source/tools/cProbSchedule.cc
===================================================================
--- development/source/tools/cProbSchedule.cc	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/tools/cProbSchedule.cc	2006-03-15 04:45:51 UTC (rev 521)
@@ -12,31 +12,17 @@
 
 #include "cChangeList.h"
 #include "cMerit.h"
-#include "cWorld.h"
 
-
 // The larger merits cause problems here; things need to be re-thought out.
 
-cProbSchedule::cProbSchedule(cWorld* world, int _item_count)
-  : cSchedule(_item_count)
-  , m_world(world)
-  , chart(_item_count)
-{
-}
-
-cProbSchedule::~cProbSchedule()
-{
-}
-
-
 int cProbSchedule::GetNextID()
 {
   assert(chart.GetTotalWeight() > 0);
-  const double position = m_world->GetRandom().GetDouble(chart.GetTotalWeight());
+  const double position = m_rng.GetDouble(chart.GetTotalWeight());
   return chart.FindPosition(position);
 }
 
-void cProbSchedule::Adjust(int item_id, const cMerit & item_merit)
+void cProbSchedule::Adjust(int item_id, const cMerit& item_merit)
 {
   if (cChangeList *change_list = GetChangeList()) {
     change_list->MarkChange(item_id);

Modified: development/source/tools/cProbSchedule.h
===================================================================
--- development/source/tools/cProbSchedule.h	2006-03-14 07:41:46 UTC (rev 520)
+++ development/source/tools/cProbSchedule.h	2006-03-15 04:45:51 UTC (rev 521)
@@ -11,6 +11,9 @@
 #ifndef cProbSchedule_h
 #define cProbSchedule_h
 
+#ifndef cRandom_h
+#include "cRandom.h"
+#endif
 #ifndef cSchedule_h
 #include "cSchedule.h"
 #endif
@@ -24,12 +27,11 @@
  **/
 
 class cMerit;
-class cWorld;
 
 class cProbSchedule : public cSchedule
 {
 private:
-  cWorld* m_world;
+  cRandom m_rng;
   cWeightedIndex chart;
   
   
@@ -37,10 +39,10 @@
   cProbSchedule& operator=(const cProbSchedule&); // @not_implemented
 
 public:
-  cProbSchedule(cWorld* world, int num_cells);
-  ~cProbSchedule();
+  cProbSchedule(int num_cells, int seed) : cSchedule(num_cells), m_rng(seed), chart(num_cells) { ; }
+  ~cProbSchedule() { ; }
 
-  void Adjust(int item_id, const cMerit & merit);
+  void Adjust(int item_id, const cMerit& merit);
   int GetNextID();
 };
 




More information about the Avida-cvs mailing list