[Avida-SVN] r2976 - in development/source: cpu main
brysonda at myxo.css.msu.edu
brysonda at myxo.css.msu.edu
Wed Nov 26 12:13:48 PST 2008
Author: brysonda
Date: 2008-11-26 15:13:48 -0500 (Wed, 26 Nov 2008)
New Revision: 2976
Modified:
development/source/cpu/cHardwareCPU.cc
development/source/main/cEnvironment.cc
development/source/main/cStateGrid.h
Log:
Implement the state grid sensing support. The returned values of the sense instruction can be defined in the environment specification.
Modified: development/source/cpu/cHardwareCPU.cc
===================================================================
--- development/source/cpu/cHardwareCPU.cc 2008-11-26 19:18:09 UTC (rev 2975)
+++ development/source/cpu/cHardwareCPU.cc 2008-11-26 20:13:48 UTC (rev 2976)
@@ -4542,8 +4542,10 @@
bool cHardwareCPU::Inst_SGSense(cAvidaContext& ctx)
{
- // @TODO - state grid sense instruction
- return true;
+ const cStateGrid& sg = m_organism->GetStateGrid();
+ const int reg_used = FindModifiedRegister(REG_BX);
+ GetRegister(reg_used) = sg.SenseStateAt(m_ext_mem[0], m_ext_mem[1]);
+ return true;
}
// @WRE addition for movement
Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc 2008-11-26 19:18:09 UTC (rev 2975)
+++ development/source/main/cEnvironment.cc 2008-11-26 20:13:48 UTC (rev 2976)
@@ -751,8 +751,11 @@
schema.AddEntry("states", 0, cArgSchema::SCHEMA_STRING);
schema.AddEntry("grid", 1, cArgSchema::SCHEMA_STRING);
+ // Load the Arguments
tList<cString> errors;
tAutoRelease<cArgContainer> args(cArgContainer::Load(desc, schema, &errors));
+
+ // Check for errors loading the arguments
if (args.IsNull() || errors.GetSize() > 0) {
cString* err_str;
while ((err_str = errors.Pop()) != NULL) {
@@ -762,6 +765,7 @@
return false;
}
+ // Extract and validate the arguments
int width = args->GetInt(0);
int height = args->GetInt(1);
int initx = args->GetInt(2);
@@ -774,40 +778,55 @@
}
- cString temp;
+ // Load the states
+ cString statename;
+ cString statesensestr;
tArray<cString> states;
+ tArray<int> state_sense;
cString statestr = args->GetString(0);
statestr.Trim();
while (statestr.GetSize()) {
- temp = statestr.Pop(',');
- temp.Trim();
+ statesensestr = statestr.Pop(',');
+ statename = statesensestr.Pop('=');
+ statename.Trim();
+
+ // Check for duplicate state definition
for (int i = 0; i < states.GetSize(); i++) {
- if (temp == states[i]) {
+ if (statename == states[i]) {
cerr << "error: duplicate state identifier for state grid " << name << endl;
return false;
}
}
- states.Push(temp);
+
+ // Add state to the collection
+ states.Push(statename);
+
+ // Determing the value returned when sense operations are run on this state
+ int state_sense_value = states.GetSize(); // Default value is the order in which the states are loaded
+ if (statesensestr.GetSize()) state_sense_value = statesensestr.AsInt();
+ state_sense.Push(state_sense_value);
}
if (states.GetSize() == 0) {
cerr << "error: no states defined for state grid " << name << endl;
return false;
}
+ // Load the state grid itself
tArray<int> grid(width * height);
cString gridstr = args->GetString(1);
int cell = 0;
while (gridstr.GetSize() && cell < grid.GetSize()) {
- temp = gridstr.Pop(',');
- temp.Trim();
+ statename = gridstr.Pop(',');
+ statename.Trim();
for (int i = 0; i < states.GetSize(); i++) {
- if (temp == states[i]) {
+ if (statename == states[i]) {
grid[cell++] = i;
break;
}
}
- cerr << "error: state identifier undefined for cell (" << (cell / width) << ", " << (cell % width) << ") in state grid " << name << endl;
+ cerr << "error: state identifier undefined for cell (" << (cell / width) << ", "
+ << (cell % width) << ") in state grid " << name << endl;
return false;
}
if (cell != (grid.GetSize() - 1) || gridstr.GetSize() > 0) {
@@ -815,7 +834,7 @@
return false;
}
- m_state_grids.Push(new cStateGrid(name, width, height, initx, inity, initfacing, states, grid));
+ m_state_grids.Push(new cStateGrid(name, width, height, initx, inity, initfacing, states, state_sense, grid));
return true;
}
Modified: development/source/main/cStateGrid.h
===================================================================
--- development/source/main/cStateGrid.h 2008-11-26 19:18:09 UTC (rev 2975)
+++ development/source/main/cStateGrid.h 2008-11-26 20:13:48 UTC (rev 2976)
@@ -39,6 +39,7 @@
int m_init_y;
int m_init_facing;
tArray<cString> m_states;
+ tArray<int> m_sense_values;
tArray<int> m_grid;
cStateGrid(); // @not_implemented
@@ -46,18 +47,29 @@
cStateGrid& operator=(const cStateGrid&); // @not_implemented
public:
- cStateGrid(const cString& name, int w, int h, int x, int y, int f, const tArray<cString>& states, const tArray<int>& grid)
- : m_name(name), m_w(w), m_h(h), m_init_x(x), m_init_y(y), m_init_facing(f), m_states(states), m_grid(grid) { ; }
+ inline cStateGrid(const cString& name, int w, int h, int x, int y, int f, const tArray<cString>& states,
+ const tArray<int>& sense_values, const tArray<int>& grid);
~cStateGrid() { ; }
- int GetWidth() const { return m_w; }
- int GetHeight() const { return m_h; }
- int GetInitialX() const { return m_init_x; }
- int GetInitialY() const { return m_init_y; }
- int GetInitialFacing() const { return m_init_facing; }
- int GetNumStates() const { return m_states.GetSize(); }
+ inline int GetWidth() const { return m_w; }
+ inline int GetHeight() const { return m_h; }
+ inline int GetInitialX() const { return m_init_x; }
+ inline int GetInitialY() const { return m_init_y; }
+ inline int GetInitialFacing() const { return m_init_facing; }
+ inline int GetNumStates() const { return m_states.GetSize(); }
- int GetStateAt(int x, int y) const { return m_grid[x * m_w + y]; }
+ inline int GetStateAt(int x, int y) const { return m_grid[x * m_w + y]; }
+ inline int SenseStateAt(int x, int y) const { return m_sense_values[GetStateAt(x, y)]; }
};
+
+inline cStateGrid::cStateGrid(const cString& name, int w, int h, int x, int y, int f, const tArray<cString>& states,
+ const tArray<int>& sense_values, const tArray<int>& grid)
+ : m_name(name), m_w(w), m_h(h), m_init_x(x), m_init_y(y), m_init_facing(f), m_states(states)
+ , m_sense_values(sense_values), m_grid(grid)
+{
+ assert(states.GetSize() == sense_values.GetSize());
+}
+
+
#endif
More information about the Avida-cvs
mailing list