Kevin Veroneau avatar Kevin Veroneau committed 3660e66 Draft

Updated CPU Hook interface to make it easier to create hooks and use them in the included assembler.

Comments (0)

Files changed (2)

 
 CPU Hooks can allow custom op-codes to be added and removed like a plugin system.
 For example, say you temporarily add a virtual device attached to the CPU, then later remove it.
-The inserting/removing is controlled in Python and no by the op-codes/coder.
+The inserting/removing is controlled in Python and not by the op-codes/coder.
 
 There is a Base class which must be sub-classed to make hooks work, there is an example Hook provided.
 
 
 --------------
 class HelloWorldHook(BaseCPUHook):
+    opcode = 60
+    opname = 'hlo'
     def hook_32(self):
         print "Hello World!"
     def hook_33(self):
 --------------
 In the coder interface, you can access both functions via:
 --------------
-set 60
-set 32
-set 60
-set 33
+hlo 32
+hlo 33
+int 1
 boot 0
 --------------
-Note the manual op-codes there.  In the future, I may add the ability to enable the coder to reflect these new op-codes using a custom command to make things easier.
+With this new updated version, you can select the opname along with the opcode in the subclass, and the coder will use it.
         if not isinstance(cpu, CPU):
             raise TypeError
         self.cpu = cpu
+        for hook in self.cpu.cpu_hooks:
+            self.bc_map.update({self.cpu.cpu_hooks[hook].opname: hook})
     def parse(self, c):
         try:
             sp = c.index(' ')
         func()
 
 class HelloWorldHook(BaseCPUHook):
+    opcode = 60
+    opname = 'hlo'
     def hook_32(self):
         print "Hello World!"
     def hook_33(self):
         self.mem.ptr = 0
     def savebin(self, filename):
         open(filename, 'wb').write(self.mem.mem.getvalue())
-    def add_cpu_hook(self, klass, opcode):
+    def add_cpu_hook(self, klass):
         hook = klass(self)
-        self.cpu_hooks.update({opcode: hook})
+        self.cpu_hooks.update({hook.opcode: hook})
     def dump(self):
         self.mem.ptr = 0
         for i in range(0, (len(self.mem)/2)-1):
 if __name__ == '__main__':
     #import readline
     c = CPU('hello.bin')
-    #c.add_cpu_hook(HelloWorldHook, 60)
+    #c.add_cpu_hook(HelloWorldHook)
     c.run()
     #cli = Coder(c)
     #cli()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.