Guest User

Untitled

a guest
Nov 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. OPCODE_ARG_COUNTS = {
  2. [0x02,0x00] => 1
  3. }
  4.  
  5. class Vm
  6. attr_accessor :memory # ""
  7.  
  8. attr_accessor :pc # 0
  9. attr_accessor :stack # []
  10.  
  11. def self.load_scm(scm = "main")
  12. new( File.read("#{`pwd`.strip}/#{scm}.scm") )
  13. end
  14.  
  15. def initialize(script_binary)
  16. self.memory = script_binary.bytes.to_a
  17. self.pc = 0
  18. self.stack = []
  19.  
  20. tick!
  21. end
  22.  
  23. def dump_memory_at(address,size = 16)
  24. start_offset = address
  25. end_offset = address + size
  26. puts "#{address} - #{hex self.memory[start_offset..end_offset]}"
  27. end
  28.  
  29. def hex(array_of_bytes)
  30. array_of_bytes.map{|m| m.to_s(16).rjust(2,"0") }.join(" ")
  31. end
  32.  
  33. def tick!
  34. dump_memory_at(pc)
  35.  
  36. opcode = read(2)
  37. args = []
  38.  
  39. OPCODE_ARG_COUNTS[opcode].times do
  40. arg_type = read(1)[0]
  41. arg = [arg_type]
  42. case arg_type
  43. when 1 # immediate 32 bit signed int
  44. arg << read(4)
  45. end
  46. args << arg
  47. end
  48.  
  49. puts " opcode #{hex(opcode)} args: #{args.inspect}"
  50.  
  51. end
  52.  
  53. def inspect
  54. "pc: #{pc}"
  55. end
  56.  
  57. protected
  58.  
  59. def read(bytes = 1)
  60. read = self.memory[(self.pc)...(self.pc+bytes)]
  61. self.pc += bytes
  62. read
  63. end
  64. end
  65.  
  66. # 0 - 02 00 01 20 ab 00 00 73 00 00 00 00 00 00 00 00 00
  67. # opcode 02 00 args: [[1, [32, 171, 0, 0]]]
Add Comment
Please, Sign In to add comment