1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details. */
13 writeln("ERROR: ", msg);
73 define isBranch (mnemo) {
99 fl = new File("awish.vmd", "rb");
100 if (!fl) abort("no code file");
101 sign = fl.readBuf(4);
102 if (sign != "AVM0") abort("invalid code file");
104 code = fl.readBuf(csz);
105 lcnt = fl.readWord();
107 for (local f = 0; f < lcnt; ++f) {
108 local type = fl.getc(), value, name, nlen, vname, v;
110 if (type == LB_GVAR || type == LB_TVAR) {
112 } else if (type == LB_CODE) {
113 value = fl.readWord();
114 } else if (type == LB_CONST) {
115 value = fl.readWord();
116 if (value >= 32768) value -= 65536;
118 abort("invalid code file");
121 name = fl.readBuf(nlen);
122 for (local f = 0; f < name.length; ++f) name[f] ^= 0xa5;
123 v = \{type:type, name:name, value:value};
130 define findLabel (val, type) {
131 foreach (local l with list.items()) {
132 if (l.type == type && l.value == val) return l.name;
138 for (local f = 0; f < code.length; ++f) code[f] ^= 42;
142 while (pc < code.length) {
143 local opcode = code[pc++], argc, brn, name;
145 argc = (opcode>>6)&0x03;
147 if (opcode >= opnames.length) {
148 write("%04x: BAD".format(pc-1));
150 } else if (opnames[opcode] == "jmp" && argc == 0) {
151 write("%04x: %s".format(pc-1, "ret"));
152 name = opnames[opcode];
154 write("%04x: %s".format(pc-1, opnames[opcode]));
155 name = opnames[opcode];
157 brn = isBranch(name);
159 for (local f = 0; f < argc; ++f) {
160 local vn = code[pc++], val, var = 0, name;
162 if (f != 0) write(", "); else write(" ");
166 val |= (code[pc++]<<8);
167 if ((f != 00 || !brn) && val >= 32768) val -= 65536;
169 name = findLabel(val, LB_CODE);
170 if (name) write(name); else write("0x%04x".format(val));
172 //name = findLabel(val, LB_CONST);
174 if (name) write(name); else write(val);
176 } else if (vn == 127) {
179 val |= (code[pc++]<<8);
180 if (val >= 32768) val -= 65536;
181 write("[.", val, "]");
188 name = findLabel(val, LB_GVAR);
189 if (name) write("[", name, "]"); else write("[@", val, "]");
194 name = findLabel(val, LB_TVAR);
195 if (name) write("[", name, "]"); else write("[", val, "]");