1 /* ScummVM Tools
2  *
3  * ScummVM Tools is the legal property of its developers, whose
4  * names are too numerous to list here. Please refer to the
5  * COPYRIGHT file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 /* Scumm Script Disassembler */
23 
24 #ifndef DESCUMM_H
25 #define DESCUMM_H
26 
27 #include <assert.h>
28 
29 #include "common/scummsys.h"
30 
31 typedef unsigned int uint;
32 
33 /**
34  * Extremly simple fixed size stack class.
35  */
36 template <class T, int MAX_SIZE = 10>
37 class FixedStack {
38 protected:
39 	T	_stack[MAX_SIZE];
40 	int	_size;
41 public:
42 	FixedStack<T, MAX_SIZE>() : _size(0) {}
43 
empty()44 	bool empty() const {
45 		return _size <= 0;
46 	}
clear()47 	void clear() {
48 		_size = 0;
49 	}
push(const T & x)50 	void push(const T& x) {
51 		assert(_size < MAX_SIZE);
52 		_stack[_size++] = x;
53 	}
top()54 	const T& top() const {
55 		assert(_size > 0);
56 		return _stack[_size - 1];
57 	}
top()58 	T& top() {
59 		assert(_size > 0);
60 		return _stack[_size - 1];
61 	}
pop()62 	T pop() {
63 		T tmp = top();
64 		--_size;
65 		return tmp;
66 	}
size()67 	int size() const {
68 		return _size;
69 	}
70 	T& operator [](int i) {
71 		assert(0 <= i && i < MAX_SIZE);
72 		return _stack[i];
73 	}
74 	const T& operator [](int i) const {
75 		assert(0 <= i && i < MAX_SIZE);
76 		return _stack[i];
77 	}
78 };
79 
80 //
81 // The block stack records jump instructions
82 //
83 struct Block {
84 	uint from;	// From which offset...
85 	uint to;		// ...to which offset
86 	bool isWhile;			// Set to true if we think this jump is part of a while loop
87 };
88 
89 typedef FixedStack<Block, 512> BlockStack;
90 
91 extern BlockStack g_blockStack;
92 
93 
94 //
95 // Jump decoding auxiliaries (used by the code which tries to translate jumps
96 // back into if / else / while / etc. constructs).
97 //
98 extern bool pendingElse, haveElse;
99 extern int pendingElseTo;
100 extern int pendingElseOffs;
101 extern int pendingElseOpcode;
102 extern int pendingElseIndent;
103 
104 //
105 // The opcode of an unconditional jump instruction.
106 //
107 extern int g_jump_opcode;
108 
109 //
110 // Command line gptions
111 //
112 struct Options {
113 	bool alwaysShowOffs;
114 	bool dontOutputIfs;
115 	bool dontOutputElse;
116 	bool dontOutputElseif;
117 	bool dontOutputWhile;
118 	bool dontOutputBreaks;
119 	bool dontShowOpcode;
120 	bool dontShowOffsets;
121 	bool haltOnError;
122 
123 	bool ZakFlag;
124 	bool IndyFlag;
125 	bool GF_UNBLOCKED;
126 
127 	//
128 	// The SCUMM version used for the script we are descumming.
129 	//
130 	byte scriptVersion;
131 	byte heVersion;
132 };
133 
134 
135 extern Options g_options;
136 
137 //
138 // Start and length of the script code (w/o header)
139 //
140 extern byte *g_scriptStart;
141 extern uint g_scriptSize;
142 
143 //
144 // Pointer to the current byte, i.e. the byte to be
145 // read next.
146 //
147 extern byte *g_scriptCurPos;
148 
149 
150 // The variable currentOpcodeBlockStart indicates the offset associated to
151 // the next line to be printed; in other words, it is the offset of
152 // the first bytecode op which is part of the current line (recall
153 // that a single line can correspond to multiple ops, e.g. several
154 // push-ops plus one op using all those pushed values).
155 extern int currentOpcodeBlockStart;
156 
157 //
158 // Common
159 //
160 
161 extern void outputLine(const char *buf, int curoffs, int opcode, int indent);
162 
163 extern char *put_ascii(char *buf, int i);
164 extern char *get_string(char *buf);
165 
166 extern char *strecpy(char *buf, const char *src);
167 extern int get_curoffs();
168 extern int get_byte();
169 extern int get_word();
170 extern int get_dword();
171 
172 extern bool maybeAddIf(uint cur, uint to);
173 extern bool maybeAddElse(uint cur, uint to);
174 extern bool maybeAddElseIf(uint cur, uint elseto, uint to);
175 extern bool maybeAddBreak(uint cur, uint to);
176 extern void writePendingElse();
177 
178 //
179 // Entry points for the descumming
180 //
181 extern void next_line_V0(char *buf);	// For V0
182 extern void next_line_V12(char *buf);	// For V1 and V2
183 extern void next_line_V345(char *buf);	// For V3, V4, V5
184 extern void next_line_V67(char *buf);
185 extern void next_line_V8(char *buf);
186 extern void next_line_HE_V72(char *buf);
187 extern void next_line_HE_V100(char *buf);
188 
189 
190 
191 #endif
192