Skip to content
This repository
Browse code

99 almost work

  • Loading branch information...
commit dfa8894ee90e632f41187389706b309baefea26d 0 parents
zed_0xff authored April 25, 2010

Showing 1 changed file with 198 additions and 0 deletions. Show diff stats Hide diff stats

  1. 198  disasm.rb
198  disasm.rb
... ...
@@ -0,0 +1,198 @@
  1
+#!/usr/bin/ruby
  2
+
  3
+STDOUT.sync = true
  4
+
  5
+fname = ARGV.first
  6
+
  7
+@prg = File.read(fname)
  8
+@pos = 0
  9
+
  10
+def prg
  11
+  @prg
  12
+end
  13
+
  14
+def arg id=0
  15
+  (@prg[@pos+1+id*2]<<8) + @prg[@pos+id*2]
  16
+end
  17
+
  18
+def do_run
  19
+  ARGV[1] == 'run'
  20
+end
  21
+
  22
+def stop!
  23
+  @stop = true
  24
+end
  25
+
  26
+def mem
  27
+  @prg
  28
+end
  29
+
  30
+def push word
  31
+  @prg.store_word(@sp, word)
  32
+  @sp -= 2
  33
+end
  34
+
  35
+def pop
  36
+  r = @prg.get_word(@sp)
  37
+  @sp += 2
  38
+  r
  39
+end
  40
+
  41
+@prg << 0 while @prg.size < 0x12004
  42
+
  43
+@sp = @bp = 0x12000
  44
+
  45
+def @prg.store_word(ptr, word)
  46
+  self[ptr]   = word & 0xff
  47
+  self[ptr+1] = word >> 8
  48
+  word
  49
+end
  50
+
  51
+def @prg.get_word(ptr)
  52
+  self[ptr] + (self[ptr+1] << 8)
  53
+end
  54
+
  55
+output = ""
  56
+@stop = false
  57
+
  58
+begin
  59
+
  60
+while !@stop
  61
+  raise "no prg" if !@prg || @prg.size == 0
  62
+
  63
+  b = @prg[@pos]
  64
+  printf "pos=%04x sp=%05x b=%02x\t",@pos,@sp,b
  65
+  case b
  66
+    when 0:
  67
+      @pos += 1
  68
+      printf "mov [%04x], %04x (%4d)",arg,arg(1),arg(1)
  69
+      mem.store_word(arg,arg(1))
  70
+      @pos += 4
  71
+    when 1:
  72
+      @pos += 1
  73
+      printf "mov [%04x], [%04x]",arg,arg(1)
  74
+      mem.store_word(arg, mem.get_word(arg(1)))
  75
+      #mem[arg] = mem[arg(1)]
  76
+      @pos += 4
  77
+    when 2:
  78
+      @pos += 1
  79
+      printf "add [%04x], [%04x]",arg,arg(1)
  80
+      mem.store_word(arg, mem.get_word(arg) + mem.get_word(arg(1)))
  81
+      @pos += 4
  82
+    when 3:
  83
+      @pos += 1
  84
+      printf "[%04x] -= [%04x]", arg, arg(1)
  85
+      mem.store_word(arg, mem.get_word(arg) - mem.get_word(arg(1)))
  86
+      @pos += 4
  87
+    when 4:
  88
+      @pos += 1
  89
+      printf "[%04x] *= [%04x]", arg, arg(1)
  90
+      mem.store_word(arg, mem.get_word(arg) * mem.get_word(arg(1)))
  91
+      @pos += 4
  92
+    when 5:
  93
+      @pos += 1
  94
+      printf "goto %04x",arg
  95
+      if do_run
  96
+        @pos = arg
  97
+      else
  98
+        @pos += 2
  99
+      end
  100
+    when 6:
  101
+      @pos += 1
  102
+      printf "goto %04x if [%04x] == [%04x]",arg,arg(1),arg(2)
  103
+      if mem.get_word(arg(1)) == mem.get_word(arg(2)) && do_run
  104
+        @pos = arg 
  105
+      else
  106
+        @pos += 6
  107
+      end
  108
+    when 7:
  109
+      @pos += 1
  110
+      printf "goto %04x if [%04x] > [%04x]",arg,arg(1),arg(2)
  111
+      if mem.get_word(arg(1)) > mem.get_word(arg(2)) && do_run
  112
+        @pos = arg 
  113
+      else
  114
+        @pos += 6
  115
+      end
  116
+    when 9:
  117
+      @pos += 1
  118
+      printf "call %04x",arg
  119
+      if do_run
  120
+        push(@pos+2)
  121
+        @pos = arg
  122
+      else
  123
+        @pos += 2
  124
+      end
  125
+    when 0x0a
  126
+      @pos += 1
  127
+      printf "ret"
  128
+      if do_run
  129
+        @pos = pop
  130
+      end
  131
+    when 0x0b:
  132
+      @pos += 1
  133
+      printf "[%04x] (%4d) /= [%04x] (%4d)", arg, mem.get_word(arg), arg(1), mem.get_word(arg(1))
  134
+      #mem[arg] /= mem[arg(1)]
  135
+      mem.store_word(arg, mem.get_word(arg) / mem.get_word(arg(1)))
  136
+      @pos += 4
  137
+    when 0x11:
  138
+      @pos += 1
  139
+      printf "putchar from [%04x] to outfile",arg
  140
+      @pos += 2
  141
+    when 0x12:
  142
+      @pos += 1
  143
+      printf "putchar from [%04x]: '%c' (%02x)",arg,prg[mem[arg]],prg[mem[arg]]
  144
+      output += sprintf("%c",prg[mem[arg]])
  145
+      @pos += 2
  146
+    when 0x20:
  147
+      @pos += 1
  148
+      printf "push %04x",arg
  149
+      push arg
  150
+      @pos += 2
  151
+    when 0x22:
  152
+      @pos += 1
  153
+      printf "t1=bp; t2=sp; bp=sp; sp -= %04x; push t2,t1",arg
  154
+      t1 = @bp
  155
+      t2 = @sp
  156
+      @bp = @sp
  157
+      @sp -= arg
  158
+      push t2
  159
+      push t1
  160
+      @pos += 2
  161
+    when 0x23:
  162
+      @pos += 1
  163
+      bp = pop
  164
+      sp = pop
  165
+      printf "pop bp,sp"
  166
+    when 0x24:
  167
+      @pos += 1
  168
+      printf "[%04x] = bp",arg
  169
+      @pos += 2
  170
+    when 0x25:
  171
+      @pos += 1
  172
+      printf "?mov [%04x], [[%04x]] (%5d)",arg,arg(1), prg[arg(1)]
  173
+      mem.store_word(arg, mem.get_word(arg(1)))
  174
+      @pos += 4
  175
+    when 0x30:
  176
+      puts "[*] EXIT"
  177
+      stop!
  178
+    when 0x42:
  179
+      @pos += 1
  180
+      printf "printf([%04x]): \"%d\"", arg, prg[arg]
  181
+      output += prg[arg].to_s
  182
+      @pos += 2
  183
+    else
  184
+      puts "[!] unknown bytecode #{b.to_s(16)} at pos #{@pos.to_s(16)}"
  185
+      stop!
  186
+  end
  187
+  puts
  188
+end
  189
+
  190
+ensure
  191
+
  192
+puts
  193
+puts
  194
+(0x9000...0x10000).each do |addr|
  195
+  printf("mem[%04x] = %04x (%4d)\n", addr,mem[addr],mem[addr]) if mem[addr] != 0
  196
+end
  197
+print "out = #{output.inspect}"
  198
+end

0 notes on commit dfa8894

Please sign in to comment.
Something went wrong with that request. Please try again.