2 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3 * Understanding is not required. Only obedience.
5 * This program is free software. It comes without any warranty, to
6 * the extent permitted by applicable law. You can redistribute it
7 * and/or modify it under the terms of the Do What The Fuck You Want
8 * To Public License, Version 2, as published by Sam Hocevar. See
9 * http://sam.zoy.org/wtfpl/COPYING for more details. */
13 writeln("ERROR: ", msg);
80 define isBranch (mnemo) {
96 fl = new File(args.length<1?"awish.vmd":args[0], "rb");
97 if (!fl) abort("no code file");
99 if (sign != "AVM2") abort("invalid code file");
100 csize = fl.readWord()^0x2a2a; // code size
101 rcnt = fl.readWord()^0x2a2a; // number of fixups
102 elcnt = fl.readWord()^0x2a2a; // number of extern labels
103 lcnt = fl.readWord()^0x2a2a; // number of labels
104 gvmax = fl.readWord()^0x2a2a; // number of global vars
105 tvmax = fl.readWord()^0x2a2a; // number of thread local vars
106 writeln("code: %d bytes, %d public labels, %d relocations, %d externs".format(csize, lcnt, rcnt, elcnt));
107 code = fl.readBuf(csize);
108 for (local f = 0; f < code.length; ++f) code[f] ^= 0x2a;
110 for (local f = 0; f < rcnt; ++f) {
111 local pc = fl.readWord()^0x2a2a;
112 writeln("0x%04x".format(pc));
118 define findLabel (val, type) {
120 foreach (local l with list.items()) {
121 if (l.type == type && l.value == val) return l.name;
129 while (pc < code.length) {
130 local opcode = code[pc++], argc, brn, name;
132 argc = (opcode>>6)&0x03;
134 if (opcode >= opnames.length) {
135 write("%04x: BAD".format(pc-1));
137 } else if (opnames[opcode] == "jmp" && argc == 0) {
138 write("%04x: %s".format(pc-1, "ret"));
139 name = opnames[opcode];
141 write("%04x: %s".format(pc-1, opnames[opcode]));
142 name = opnames[opcode];
144 brn = isBranch(name);
146 for (local f = 0; f < argc; ++f) {
147 local vn = code[pc++], val, var = 0, name;
149 if (f != 0) write(", "); else write(" ");
153 val |= (code[pc++]<<8);
154 if ((f != 00 || !brn) && val >= 32768) val -= 65536;
156 name = findLabel(val, LB_CODE);
157 if (name) write(name); else write("0x%04x".format(val));
159 //name = findLabel(val, LB_CONST);
161 if (name) write(name); else write(val);
163 } else if (vn == 127) {
166 val |= (code[pc++]<<8);
167 if (val >= 32768) val -= 65536;
168 write("[.", val, "]");
175 name = findLabel(val, LB_GVAR);
176 if (name) write("[", name, "]"); else write("[@", val, "]");
181 name = findLabel(val, LB_TVAR);
182 if (name) write("[", name, "]"); else write("[", val, "]");