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 != "AVM1") 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 writeln("code: %d bytes, %d public labels, %d relocations, %d externs".format(csize, lcnt, rcnt, elcnt));
105 code = fl.readBuf(csize);
106 for (local f = 0; f < code.length; ++f) code[f] ^= 0x2a;
110 define findLabel (val, type) {
112 foreach (local l with list.items()) {
113 if (l.type == type && l.value == val) return l.name;
121 while (pc < code.length) {
122 local opcode = code[pc++], argc, brn, name;
124 argc = (opcode>>6)&0x03;
126 if (opcode >= opnames.length) {
127 write("%04x: BAD".format(pc-1));
129 } else if (opnames[opcode] == "jmp" && argc == 0) {
130 write("%04x: %s".format(pc-1, "ret"));
131 name = opnames[opcode];
133 write("%04x: %s".format(pc-1, opnames[opcode]));
134 name = opnames[opcode];
136 brn = isBranch(name);
138 for (local f = 0; f < argc; ++f) {
139 local vn = code[pc++], val, var = 0, name;
141 if (f != 0) write(", "); else write(" ");
145 val |= (code[pc++]<<8);
146 if ((f != 00 || !brn) && val >= 32768) val -= 65536;
148 name = findLabel(val, LB_CODE);
149 if (name) write(name); else write("0x%04x".format(val));
151 //name = findLabel(val, LB_CONST);
153 if (name) write(name); else write(val);
155 } else if (vn == 127) {
158 val |= (code[pc++]<<8);
159 if (val >= 32768) val -= 65536;
160 write("[.", val, "]");
167 name = findLabel(val, LB_GVAR);
168 if (name) write("[", name, "]"); else write("[@", val, "]");
173 name = findLabel(val, LB_TVAR);
174 if (name) write("[", name, "]"); else write("[", val, "]");