aboutsummaryrefslogtreecommitdiff
path: root/saga/sdebug.cpp
blob: 20645337587e8f1c738c49c5ec71f20e6b413503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// Scripting module simple thread debugging support
#include "saga/saga.h"

#include "saga/gfx.h"
#include "saga/text.h"
#include "saga/scene.h"
#include "saga/font.h"

#include "saga/script.h"

namespace Saga {

#define SD_DISPLAY_LEN 128
#define SD_ADDTXT( x ) strncat( disp_buf, x, SD_DISPLAY_LEN );

int Script::SDebugPrintInstr(SCRIPT_THREAD *thread) {
	TEXTLIST_ENTRY tl_e;
	char tmp_buf[80] = { 0 };
	static char disp_buf[SD_DISPLAY_LEN] = { 0 };
	int in_char;
//	int op_offset;
	int n_switch;
	int i;
	SCENE_INFO si;

	_vm->_scene->getInfo(&si);

	disp_buf[0] = 0;

	if (_dbg_txtentry != NULL) {
		_vm->textDeleteEntry(si.text_list, _dbg_txtentry);
		_dbg_txtentry = NULL;
	}

	tl_e.color = 1;
	tl_e.effect_color = 0;
	tl_e.text_x = 2;
	tl_e.text_y = 20;
	tl_e.font_id = SMALL_FONT_ID;
	tl_e.flags = FONT_OUTLINE;
	tl_e.string = disp_buf;
	tl_e.display = 1;

	MemoryReadStream readS(currentScript()->bytecode->bytecode_p 
							 + thread->i_offset, 
							 currentScript()->bytecode->bytecode_len 
							 - thread->i_offset);
	in_char = readS.readByte();
	sprintf(tmp_buf, "%04lX | %02X | ", thread->i_offset, in_char);
	strncat(disp_buf, tmp_buf, SD_DISPLAY_LEN);

	switch (in_char) {
		// Align
	case 0x01:
		SD_ADDTXT("ALGN |");
		break;
		// Push nothing
	case 0x02:
		SD_ADDTXT("PSHN |");
		break;
		// Pop nothing
	case 0x03:
		SD_ADDTXT("POPN |");
		break;
		// Push false (0)
	case 0x04:
		SD_ADDTXT("PSHF |");
		break;
		// Push true (1)
	case 0x05:
		SD_ADDTXT("PSHT |");
		break;
		// Push word (dialogue string index)
	case 0x08:
		{
			int param;

			SD_ADDTXT("PSHD | ");
			param = readS.readUint16LE();
			sprintf(tmp_buf, "%02X", param);
			SD_ADDTXT(tmp_buf);
/*
			if(( param >= 0 ) && ( param < diag_list->n_dialogue )) {
				debug(2, " ; \"%.*s\"", SCRIPT_STRINGLIMIT, diag_list->str[param] );
			}
			else {
				debug(2, " ; Invalid dialogue string.\n" );
			}
*/
		}
		break;
		// Push word
	case 0x06:
		{
			int param;

			SD_ADDTXT("PUSH | ");
			param = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Test flag
	case 0x0B:
		{
			int param1;
			int param2;

			SD_ADDTXT("TSTF | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Get word
	case 0x0C:
		{
			int param1;
			int param2;

			SD_ADDTXT("GETW | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Modify flag
	case 0x0F:
		{
			int param1;
			int param2;

			SD_ADDTXT("MODF | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Put word
	case 0x10:
		{
			int param1;
			int param2;

			SD_ADDTXT("PUTW | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Modify flag and pop
	case 0x13:
		{
			int param1;
			int param2;

			SD_ADDTXT("MDFP | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Put word and pop
	case 0x14:
		{
			int param1;
			int param2;

			SD_ADDTXT("PTWP | ");
			param1 = readS.readByte();
			param2 = readS.readUint16LE();

			sprintf(tmp_buf, "%02X %04X", param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Call subscript ?
	case 0x17:
		{
			int param1;
			int param2;
			int param3;

			SD_ADDTXT("GOSB | ");
			param1 = readS.readByte();
			param2 = readS.readByte();
			param3 = readS.readUint16LE();
			sprintf(tmp_buf, "%02X %02X %04X", param1, param2, param3);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Call function
	case 0x19:
	case 0x18:
		{
			int func_num;
			int param;

			SD_ADDTXT("CALL | ");
			func_num = readS.readByte();
			sprintf(tmp_buf, "%02X ", func_num);
			SD_ADDTXT(tmp_buf);
			param = readS.readUint16LE();
			sprintf(tmp_buf, "%04X ", param);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Begin subscript
	case 0x1A:
		{
			int param;

			SD_ADDTXT("ENTR | ");
			param = readS.readUint16LE();
			sprintf(tmp_buf, "%04X ", param);
			SD_ADDTXT(tmp_buf);
/*
			for( i = 0 ; i < script_list->n_scripts ; i++ ) {
				if( op_offset == script_list->scripts[i].offset ) {
					debug(2, "; Entrypoint \"%s\".", script_list->scriptl_p + script_list->scripts[i].name_offset );
					break;
				}
			}
*/
		}
		break;
	case 0x1B:
		SD_ADDTXT("??? ");
		break;
		// End subscript
	case 0x1C:
		SD_ADDTXT("EXIT |");
		break;
		// Unconditional jump
	case 0x1D:
		{
			int param1;

			SD_ADDTXT("JMP  | ");
			param1 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param1);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Jump if nonzero + POP
	case 0x1E:
		{
			int param1;

			SD_ADDTXT("JNZP | ");
			param1 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param1);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Jump if zero + POP
	case 0x1F:
		{
			int param1;

			SD_ADDTXT("JZP  | ");
			param1 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param1);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Jump if nonzero
	case 0x20:
		{
			int param1;
			SD_ADDTXT("JNZ  | ");
			param1 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param1);
			SD_ADDTXT(tmp_buf);
		}
		break;

		// Jump if zero
	case 0x21:
		{
			int param1;

			SD_ADDTXT("JZ   | ");
			param1 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", param1);
			SD_ADDTXT(tmp_buf);
		}
		break;
		// Switch
	case 0x22:
		{
			int switch_num;
			int switch_jmp;
			int default_jmp;

			SD_ADDTXT("SWCH | ");
			n_switch = readS.readUint16LE();
			sprintf(tmp_buf, "%02X\n", n_switch);
			SD_ADDTXT(tmp_buf);
			for (i = 0; i < n_switch; i++) {
				switch_num = readS.readUint16LE();
				switch_jmp = readS.readUint16LE();
				// printf( TAB "CASE %04X, %04X\n", switch_num, switch_jmp);
			}
			default_jmp = readS.readUint16LE();
			//printf( TAB "DEF %04X", default_jmp);
		}
		break;
		// Random branch
	case 0x24:
		{
			int n_switch2;
			int switch_num;
			int switch_jmp;

			SD_ADDTXT("RJMP | ");
			// Ignored?
			readS.readUint16LE();
			n_switch2 = readS.readUint16LE();
			sprintf(tmp_buf, "%04X", n_switch2);
			SD_ADDTXT(tmp_buf);
			for (i = 0; i < n_switch2; i++) {
				switch_num = readS.readUint16LE();
				switch_jmp = readS.readUint16LE();
				//debug(2, "WEIGHT %04X, %04X", switch_num, switch_jmp);
			}
		}
		break;
	case 0x25:
		SD_ADDTXT("NEG  |");
		break;
	case 0x26:
		SD_ADDTXT("TSTZ |");
		break;
	case 0x27:
		SD_ADDTXT("NOT  |");
		break;
	case 0x28:
		SD_ADDTXT("??? ");
		readS.readByte();
		readS.readUint16LE();
		break;
	case 0x29:
		SD_ADDTXT("??? ");
		readS.readByte();
		readS.readUint16LE();
		break;
	case 0x2A:
		SD_ADDTXT("??? ");
		readS.readByte();
		readS.readUint16LE();
		break;
	case 0x2B:
		SD_ADDTXT("??? ");
		readS.readByte();
		readS.readUint16LE();
		break;
		// Addition
	case 0x2C:
		SD_ADDTXT("ADD  |");
		break;
		// Subtraction
	case 0x2D:
		SD_ADDTXT("SUB  |");
		break;
		// Integer multiplication
	case 0x2E:
		SD_ADDTXT("MULT |");
		break;
		// Integer division
	case 0x2F:
		SD_ADDTXT("DIV |");
		break;
		// Modulus
	case 0x30:
		SD_ADDTXT("MOD  |");
		break;
		// Test equality
	case 0x33:
		SD_ADDTXT("EQU  |");
		break;
		// Test inequality
	case 0x34:
		SD_ADDTXT("NEQU |");
		break;
		// Test Greater-than
	case 0x35:
		SD_ADDTXT("GRT  |");
		break;
		// Test Less-than
	case 0x36:
		SD_ADDTXT("LST  |");
		break;
		// Test Greater-than or Equal to
	case 0x37:
		SD_ADDTXT("GRTE |");
		break;
		// Test Less-than or Equal to
	case 0x38:
		SD_ADDTXT("LSTE |");
		break;
	case 0x3F:
		SD_ADDTXT("SHR  |");
		break;
	case 0x40:
		SD_ADDTXT("SHL  |");
		break;
	case 0x41:
		SD_ADDTXT("AND  |");
		break;
	case 0x42:
		SD_ADDTXT("OR   |");
		break;
	case 0x43:
		SD_ADDTXT("XOR  |");
		break;
	case 0x44:
		SD_ADDTXT("LAND |");
		break;
	case 0x45:
		SD_ADDTXT("LOR  |");
		break;
	case 0x46:
		SD_ADDTXT("LXOR |");
		break;
	case 0x53:
		{
			int n_voices;
			int param1;
			int param2;

			SD_ADDTXT("DLGP | ");
			n_voices = readS.readByte();
			param1 = readS.readUint16LE();
			param2 = readS.readByte();
			// ignored ?
			readS.readUint16LE();
			sprintf(tmp_buf, "%02X %04X %02X", n_voices, param1, param2);
			SD_ADDTXT(tmp_buf);
		}
		break;
	case 0x54:
		SD_ADDTXT("DLGS |");
		break;
	case 0x55:
		SD_ADDTXT("DLGX |");
		break;
	case 0x56:
		{
			int param1;
			int param2;
			int param3;

			SD_ADDTXT("DLGO | ");
			param1 = readS.readByte();
			param2 = readS.readByte();
			sprintf(tmp_buf, "%02X %02X ", param1, param2);
			SD_ADDTXT(tmp_buf);
			if (param2 > 0) {
				param3 = readS.readUint16LE();
				sprintf(tmp_buf, "%04X", param3);
				SD_ADDTXT(tmp_buf);
			}
		}
		break;
	case 0x57:
		{
			int param1;
			int param2;
			int param3;

			SD_ADDTXT("JMPS | ");
			param1 = readS.readUint16LE();
			param2 = readS.readUint16LE();
			param3 = readS.readByte();
			sprintf(tmp_buf, "%04X %04X %02X", param1, param2, param3);
			SD_ADDTXT(tmp_buf);
		}
		break;
	default:
		sprintf(tmp_buf, "Invalid opcode.\n");
		SD_ADDTXT(tmp_buf);
		break;
	}

	_dbg_txtentry = _vm->textAddEntry(si.text_list, &tl_e);
	_vm->textSetDisplay(_dbg_txtentry, 1);

	return SUCCESS;
}

} // End of namespace Saga