⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code_instruction.html

📁 有趣的模拟进化的程序 由国外一生物学家开发 十分有趣
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><title>Instruction Implementation Checklist</title><body bgcolor="#FFFFFF" text="#000000" link="#0000AA" alink="#0000FF" vlink="#000044"><h2 align=center>Instruction Implementation Checklist</h2><p>This document discusses how to implement your own instructions.<h3>1. Build the method to be attached to the new instruction</h3><p>For this first step, you will be editing the virtual CPU definition in <tt>hardware_cpu.hh</tt> and <tt>hardware_cpu.cc</tt>, both of which arefound in the directory <tt>current/source/cpu/</tt>.  Start by going to thefinal section of the class definition in the header file (*.hh) and writingthe declaration for the new method that will be called whenever theinstruction is executed.For example, if you were going to add the instruction "<tt>minus-17</tt>"(which performs the oh-so-useful behavior of subtracting 17 from the?BX? register), you would add the line:<br><pre>   <font color="#880000">bool</font> <font color="#008800">Inst_Minus17</font>();</pre><p>If possible, place it near other instructions of the same type.  Onlya few samples were given in your handout, but there are about a hundredmethods in the actual file.  This instruction would likely fit best withthe group of instruction described as "Single-Argument Math".  That is,all those instructions that perform mathematical operation that only affect asingle register.<p>All methods associated with instructions return a<font color="#880000">bool</font> value that determines if it weresuccessfully executed, and take in no arguments.  Most instructions willalways return true since they have now way to fail.  The conventionthat we use to designate a method explicitly associated with an instructionis placing a prefix of <tt>Inst_</tt> in front of it.<p>Next, you have to write the function body in the code file(<tt>hardware_cpu.cc</tt>).  The method bodies will be listed at the endof this file in the same order that they were declared in the header.You would find the proper position, and write something to the effect of:<pre>  void <font color="#880000">cHardwareCPU</font>::<font color="#008800">Inst_Minus17</font>()  {    const <font color="#880000">int</font> <font color="#000088">reg_used</font> = <font color="#008800">FindModifiedRegister</font>(REG_BX);    <font color="#008800">Register</font>(<font color="#000088">reg_used</font>) -= 17;   <font color="#886600">    // Same as:  Register(reg_used) = Register(reg_used) - 17;</font>    return true;  }</pre><p>The first line of this method uses a helper function called<font color="#008800">FindModifiedRegister</font>() to identify the registerthat should be affected (it scans the next instruction to test if it is a<tt>nop</tt>), with a default value of <tt>REG_BX</tt> passed in.The second line then subtracts 17 from the value in that register.  Theconstant values and available helper functions will be described in moredetail below, as will a guide to accessing the components in the virtualCPU.  For the moment, you have finished implementing the method!<p>Note that this would be a good time to recompile if you want to test howwell your implementation is going so far.<h3>2. Link the instruction name to its method</h3>For this step, you will need to edit the file "<tt>hardware_util.cc</tt>"in the <tt>current/source/cpu/</tt> directory.  You would go into the method<font color="#880000">cHardwareUtil</font>::<font color="#008800">LoadInstLibCPU</font>()and add in the line<pre>  <font color="#000088">inst_dict</font>.<font color="#008800">Add</font>("minus-17", (<font color="#880000">tHardwareMethod</font>) &<font color="#880000">cHardwareCPU</font>::<font color="#008800">Inst_Minus17</font>);</pre><p>in the same order that it was defined in the class definition.<p>The instruction dictionary will be passed into the<font color="#008800">LoadInstLib</font>() method to determine whichfunctions will actually be put in the instruction library.  Here we add asingle entry to it that correlates the instruction name "<tt>minus-17</tt>"with the method that is supposed to be called when that instruction isexecuted.<p>Since we want to use a pointer to the appropriate method, that is what wemust pass into the dictionary.  To obtain said pointer, we must list the classthe function is part of (<font color="#880000">cHardwareCPU</font>) follow itby a double colon (::) and then give the method name(<font color="#008800">Inst_Minus17</font>) <i>without</i> the normalparentheses following it.  The parenthesis indicate that we should executethe method.  Without it, it is just the data that represents the method,and by preceding this whole mess with an ampersand ('&') we get the pointerto the location in memory that the method resides.  And before all of thiswe indicate that what is coming is a Hardware method to prep it for beingentered into the dictionary.<p>A type name in parenthesis means that the variable that follows should beconverted to this type.  In the case of this section of code, there can bemany different types of function pointers, so we want to make sure that wekeep them uniform for the moment.  In truth, since we only have one type ofvirtual CPU, this conversion isn't really needed yet, but will be useful inthe future when other types of hardware are possible.<p>Compile again, and you should have your instruction ready for use.<h3>3. Add the entry to your instruction set and test it!</h3><p>This last part should be the easiest.  If you want the new instruction youjust created to be loaded on startup, you must add a line in the instructionset you are using (specified in the genesis file) to indicate its inclusion:<pre>  minus-17 1</pre><p>And there you have it!  Now the real trick is to test if its working properly.  I'd recommend using as a framework the creature"<tt>organism.heads.100</tt>" and modifying some of the long series of"<tt>nop-C</tt>" instructions inside of it to perform some math using thenew instruction (only the very first <tt>nop-C</tt> cannot be changed).  Youcan then either go into zoom mode in the viewer and step through thecreature, or else use analyze mode trace its execution.  If you are goingto use zoom mode, setup your modified creature as the START_CREATURE ingenesis.  If you want to use analyze mode, put the following lines into the<tt>analyze.cfg</tt> file in your work/ directory:<pre>  LOAD_ORGANISM organism.inst_test

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -