Commits

Kevin Veroneau committed 3660e66

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

  • Participants
  • Parent commits 1f36694

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()