Adding a New Instruction to Avida and Modifying avida.cfg
Start out in the outermost directory where you have the Avida source code. Your first step is to edit the virtual hardware object and add in the new command. For this example, we will create a command called "load-val" which will load in a value that we specify in avida.cfg into the ?BX? register.
To start, lets make this command load in the value "42" whenever its run.
To do this:
Edit the file "source/cpu/cHardwareCPU.h". Here we must declare our new function the will perform this task.
Do as search for "Instruction Library" to find where all of the other instructions are declared. Find an appropriate place to add your new instruction (perhaps in the section on Stack and Register Operations)
Add in a new line:
bool Inst_LoadVal(cAvidaContext& ctx);
Next, edit the file "source/cpu/cHardwareCPU.cc" to write the body of this new function.
The register and stack instruction begin on line 1836. Go there and find the proper placement for the implementation of your new instruction. You should keep these instructions in the same order in which they were declared.
Add the code for the new instruction.
bool cHardwareCPU::Inst_LoadVal(cAvidaContext& ctx)
{
const int reg_used = FindModifiedRegister(REG_BX);
GetRegister(reg_used) = 42;
return true;
}
The command "FindModifiedRegister" will automatically look at the next instruction in the genome to see if its a nop. If it is, the corresponding register will be used. If not, the default register (passed in as an argument) will be used.
You have one place left to modify; you need to let Avida know about this new instruction.
Go to line 114 in this same file, and find the proper position around there to register the new instruction. When you find it, add the line:
tInstLibEntry("load-val", &cHardwareCPU::Inst_LoadVal),
And you're done with the implementation!
To compile, go to your "build/" directory, type "make", and wait.
When testing this, make sure to add the new instruction to your inst-set.cfg file.
Adding an entry to avida.cfg
Often you will need to add a new entry to the avida configuration file. In the case of our instruction above, we might want to tell it what value to load. To do this, we're going to add in the setting "INST_LOAD_VALUE".
Edit the file "source/main/cAvidaConfig.h"
The code for the settings starts on line 118 of this file. Go there and find a good place for your setting (the avida.cfg file should be in the same order as this one).
Add the line:
CONFIG_ADD_VAR(INST_LOAD_VALUE, int, 42, "Value used by load_val instruction");
And thats it! Avida now knows to look for this value in its configuration file (as well as its type, its default value, and its description).
In the avida.cfg file, make sure to add this value
...so that there is some non-default value used.
Now we need to hook it into our instruction. Lets change the code the instruction uses (back in source/cpu/cHardwareCPU.cc) to:
bool cHardwareCPU::Inst_LoadVal(cAvidaContext& ctx)
{
const int reg_used = FindModifiedRegister(REG_BX);
GetRegister(reg_used) = m_world->GetConfig().INST_LOAD_VALUE.Get();
return true;
}
Finally, compile it and test it to make sure it works!
Comments (0)
You don't have permission to comment on this page.