Debian Codegraph @

src/vm/vmcmd.c

raw
1 /*
2 * Copyright (C) 2000, 2001 Martin Norbäck, Håkan Hjort
3 * 2002-2004 the dvdnav project
4 *
5 * This file is part of libdvdnav, a DVD navigation library. It is modified
6 * from a file originally part of the Ogle DVD player.
7 *
8 * libdvdnav is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * libdvdnav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with libdvdnav; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <sys/time.h>
32
33 #include "dvdnav/dvdnav.h"
34 #include <dvdread/nav_types.h>
35 #include "decoder.h"
36 #include "vm.h"
37 #include "vmcmd.h"
38 #include "dvdnav_internal.h"
39
40 /* freebsd compatibility */
41 #ifndef PRIu8
42 #define PRIu8 "d"
43 #endif
44
45 /* freebsd compatibility */
46 #ifndef PRIu16
47 #define PRIu16 "d"
48 #endif
49
50 static const char cmp_op_table[][4] = {
51 "", "&", "==", "!=", ">=", ">", "<=", "<"
52 };
53 static const char set_op_table[][4] = {
54 "", "=", "<->", "+=", "-=", "*=", "/=", "%=", "rnd", "&=", "|=", "^="
55 };
56
57 static const char link_table[][16] = {
58 "LinkNoLink", "LinkTopC", "LinkNextC", "LinkPrevC",
59 "", "LinkTopPG", "LinkNextPG", "LinkPrevPG",
60 "", "LinkTopPGC", "LinkNextPGC", "LinkPrevPGC",
61 "LinkGoUpPGC", "LinkTailPGC", "", "",
62 "RSM"
63 };
64
65 static const char *const system_reg_table[] = {
66 "Menu Description Language Code",
67 "Audio Stream Number",
68 "Sub-picture Stream Number",
69 "Angle Number",
70 "Title Track Number",
71 "VTS Title Track Number",
72 "VTS PGC Number",
73 "PTT Number for One_Sequential_PGC_Title",
74 "Highlighted Button Number",
75 "Navigation Timer",
76 "Title PGC Number for Navigation Timer",
77 "Audio Mixing Mode for Karaoke",
78 "Country Code for Parental Management",
79 "Parental Level",
80 "Player Configurations for Video",
81 "Player Configurations for Audio",
82 "Initial Language Code for Audio",
83 "Initial Language Code Extension for Audio",
84 "Initial Language Code for Sub-picture",
85 "Initial Language Code Extension for Sub-picture",
86 "Player Regional Code",
87 "Reserved 21",
88 "Reserved 22",
89 "Reserved 23"
90 };
91
92 static const char system_reg_abbr_table[][8] = {
93 "",
94 "ASTN",
95 "SPSTN",
96 "AGLN",
97 "TTN",
98 "VTS_TTN",
99 "TT_PGCN",
100 "PTTN",
101 "HL_BTNN",
102 "NVTMR",
103 "NV_PGCN",
104 "",
105 "CC_PLT",
106 "PLT",
107 "",
108 "",
109 "",
110 "",
111 "",
112 "",
113 "",
114 "",
115 "",
116 "",
117 };
118
119 static void print_system_reg(uint16_t reg) {
120 if(reg < sizeof(system_reg_abbr_table) / sizeof(system_reg_abbr_table[0]))
121 fprintf(MSG_OUT, "%s (SRPM:%d)", system_reg_table[reg], reg);
122 else
123 fprintf(MSG_OUT, " WARNING: Unknown system register ( reg=%d ) ", reg);
124 }
125
126 static void print_g_reg(uint8_t reg) {
127 if(reg < 16)
128 fprintf(MSG_OUT, "g[%" PRIu8 "]", reg);
129 else
130 fprintf(MSG_OUT, " WARNING: Unknown general register ");
131 }
132
133 static void print_reg(uint8_t reg) {
134 if(reg & 0x80)
135 print_system_reg(reg & 0x7f);
136 else
137 print_g_reg(reg & 0x7f);
138 }
139
140 static void print_cmp_op(uint8_t op) {
141 if(op < sizeof(cmp_op_table) / sizeof(cmp_op_table[0]))
142 fprintf(MSG_OUT, " %s ", cmp_op_table[op]);
143 else
144 fprintf(MSG_OUT, " WARNING: Unknown compare op ");
145 }
146
147 static void print_set_op(uint8_t op) {
148 if(op < sizeof(set_op_table) / sizeof(cmp_op_table[0]))
149 fprintf(MSG_OUT, " %s ", set_op_table[op]);
150 else
151 fprintf(MSG_OUT, " WARNING: Unknown set op ");
152 }
153
154 static void print_reg_or_data(command_t* command, int immediate, int start) {
155 if(immediate) {
156 uint32_t i = vm_getbits(command, start, 16);
157
158 fprintf(MSG_OUT, "0x%x", i);
159 if(isprint(i & 0xff) && isprint((i>>8) & 0xff))
160 fprintf(MSG_OUT, " (\"%c%c\")", (char)((i>>8) & 0xff), (char)(i & 0xff));
161 } else {
162 print_reg(vm_getbits(command, start - 8, 8));
163 }
164 }
165
166 static void print_reg_or_data_2(command_t* command, int immediate, int start) {
167 if(immediate)
168 fprintf(MSG_OUT, "0x%x", vm_getbits(command, start - 1, 7));
169 else
170 fprintf(MSG_OUT, "g[%" PRIu8 "]", vm_getbits(command, start - 4, 4));
171 }
172
173 static void print_reg_or_data_3(command_t* command, int immediate, int start) {
174 if(immediate) {
175 uint32_t i = vm_getbits(command, start, 16);
176
177 fprintf(MSG_OUT, "0x%x", i);
178 if(isprint(i & 0xff) && isprint((i>>8) & 0xff))
179 fprintf(MSG_OUT, " (\"%c%c\")", (char)((i>>8) & 0xff), (char)(i & 0xff));
180 } else {
181 print_reg(vm_getbits(command, start, 8));
182 }
183 }
184
185
186 static void print_if_version_1(command_t* command) {
187 uint8_t op = vm_getbits(command, 54, 3);
188
189 if(op) {
190 fprintf(MSG_OUT, "if (");
191 print_g_reg(vm_getbits(command,39,8));
192 print_cmp_op(op);
193 print_reg_or_data(command, vm_getbits(command, 55,1), 31);
194 fprintf(MSG_OUT, ") ");
195 }
196 }
197
198 static void print_if_version_2(command_t* command) {
199 uint8_t op = vm_getbits(command, 54, 3);
200
201 if(op) {
202 fprintf(MSG_OUT, "if (");
203 print_reg(vm_getbits(command, 15, 8));
204 print_cmp_op(op);
205 print_reg(vm_getbits(command, 7, 8));
206 fprintf(MSG_OUT, ") ");
207 }
208 }
209
210 static void print_if_version_3(command_t* command) {
211 uint8_t op = vm_getbits(command, 54, 3);
212
213 if(op) {
214 fprintf(MSG_OUT, "if (");
215 print_g_reg(vm_getbits(command, 43, 4));
216 print_cmp_op(op);
217 print_reg_or_data(command, vm_getbits(command, 55, 1), 15);
218 fprintf(MSG_OUT, ") ");
219 }
220 }
221
222 static void print_if_version_4(command_t* command) {
223 uint8_t op = vm_getbits(command, 54, 3);
224
225 if(op) {
226 fprintf(MSG_OUT, "if (");
227 print_g_reg(vm_getbits(command, 51, 4));
228 print_cmp_op(op);
229 print_reg_or_data(command, vm_getbits(command, 55, 1), 31);
230 fprintf(MSG_OUT, ") ");
231 }
232 }
233
234 static void print_if_version_5(command_t* command) {
235 uint8_t op = vm_getbits(command, 54, 3);
236 int set_immediate = vm_getbits(command, 60, 1);
237
238 if(op) {
239 if (set_immediate) {
240 fprintf(MSG_OUT, "if (");
241 print_g_reg(vm_getbits(command, 31, 8));
242 print_cmp_op(op);
243 print_reg(vm_getbits(command, 23, 8));
244 fprintf(MSG_OUT, ") ");
245 } else {
246 fprintf(MSG_OUT, "if (");
247 print_g_reg(vm_getbits(command, 39, 8));
248 print_cmp_op(op);
249 print_reg_or_data(command, vm_getbits(command, 55, 1), 31);
250 fprintf(MSG_OUT, ") ");
251 }
252 }
253 }
254
255 static void print_special_instruction(command_t* command) {
256 uint8_t op = vm_getbits(command, 51, 4);
257
258 switch(op) {
259 case 0: /* NOP */
260 fprintf(MSG_OUT, "Nop");
261 break;
262 case 1: /* Goto line */
263 fprintf(MSG_OUT, "Goto %" PRIu8, vm_getbits(command, 7, 8));
264 break;
265 case 2: /* Break */
266 fprintf(MSG_OUT, "Break");
267 break;
268 case 3: /* Parental level */
269 fprintf(MSG_OUT, "SetTmpPML %" PRIu8 ", Goto %" PRIu8,
270 vm_getbits(command, 11, 4), vm_getbits(command, 7, 8));
271 break;
272 default:
273 fprintf(MSG_OUT, "WARNING: Unknown special instruction (%i)",
274 vm_getbits(command, 51, 4));
275 }
276 }
277
278 static void print_linksub_instruction(command_t* command) {
279 uint32_t linkop = vm_getbits(command, 7, 8);
280 uint32_t button = vm_getbits(command, 15, 6);
281
282 if(linkop < sizeof(link_table)/sizeof(link_table[0]))
283 fprintf(MSG_OUT, "%s (button %" PRIu8 ")", link_table[linkop], button);
284 else
285 fprintf(MSG_OUT, "WARNING: Unknown linksub instruction (%i)", linkop);
286 }
287
288 static void print_link_instruction(command_t* command, int optional) {
289 uint8_t op = vm_getbits(command, 51, 4);
290
291 if(optional && op)
292 fprintf(MSG_OUT, ", ");
293
294 switch(op) {
295 case 0:
296 if(!optional)
297 fprintf(MSG_OUT, "WARNING: NOP (link)!");
298 break;
299 case 1:
300 print_linksub_instruction(command);
301 break;
302 case 4:
303 fprintf(MSG_OUT, "LinkPGCN %" PRIu16, vm_getbits(command, 14, 15));
304 break;
305 case 5:
306 fprintf(MSG_OUT, "LinkPTT %" PRIu16 " (button %" PRIu8 ")",
307 vm_getbits(command, 9, 10), vm_getbits(command, 15, 6));
308 break;
309 case 6:
310 fprintf(MSG_OUT, "LinkPGN %" PRIu8 " (button %" PRIu8 ")",
311 vm_getbits(command, 6, 7), vm_getbits(command, 15, 6));
312 break;
313 case 7:
314 fprintf(MSG_OUT, "LinkCN %" PRIu8 " (button %" PRIu8 ")",
315 vm_getbits(command, 7, 8), vm_getbits(command, 15, 6));
316 break;
317 default:
318 fprintf(MSG_OUT, "WARNING: Unknown link instruction");
319 }
320 }
321
322 static void print_jump_instruction(command_t* command) {
323 switch(vm_getbits(command, 51, 4)) {
324 case 1:
325 fprintf(MSG_OUT, "Exit");
326 break;
327 case 2:
328 fprintf(MSG_OUT, "JumpTT %" PRIu8, vm_getbits(command, 22, 7));
329 break;
330 case 3:
331 fprintf(MSG_OUT, "JumpVTS_TT %" PRIu8, vm_getbits(command, 22, 7));
332 break;
333 case 5:
334 fprintf(MSG_OUT, "JumpVTS_PTT %" PRIu8 ":%" PRIu16,
335 vm_getbits(command, 22, 7), vm_getbits(command, 41, 10));
336 break;
337 case 6:
338 switch(vm_getbits(command, 23, 2)) {
339 case 0:
340 fprintf(MSG_OUT, "JumpSS FP");
341 break;
342 case 1:
343 fprintf(MSG_OUT, "JumpSS VMGM (menu %" PRIu8 ")", vm_getbits(command, 19, 4));
344 break;
345 case 2:
346 fprintf(MSG_OUT, "JumpSS VTSM (vts %" PRIu8 ", title %" PRIu8
347 ", menu %" PRIu8 ")", vm_getbits(command, 30, 7), vm_getbits(command, 38, 7), vm_getbits(command, 19, 4));
348 break;
349 case 3:
350 fprintf(MSG_OUT, "JumpSS VMGM (pgc %" PRIu8 ")", vm_getbits(command, 46, 15));
351 break;
352 }
353 break;
354 case 8:
355 switch(vm_getbits(command, 23, 2)) {
356 case 0:
357 fprintf(MSG_OUT, "CallSS FP (rsm_cell %" PRIu8 ")",
358 vm_getbits(command, 31, 8));
359 break;
360 case 1:
361 fprintf(MSG_OUT, "CallSS VMGM (menu %" PRIu8
362 ", rsm_cell %" PRIu8 ")", vm_getbits(command, 19, 4), vm_getbits(command, 31, 8));
363 break;
364 case 2:
365 fprintf(MSG_OUT, "CallSS VTSM (menu %" PRIu8
366 ", rsm_cell %" PRIu8 ")", vm_getbits(command, 19, 4), vm_getbits(command, 31, 8));
367 break;
368 case 3:
369 fprintf(MSG_OUT, "CallSS VMGM (pgc %" PRIu8 ", rsm_cell %" PRIu8 ")",
370 vm_getbits(command, 46, 15), vm_getbits(command, 31, 8));
371 break;
372 }
373 break;
374 default:
375 fprintf(MSG_OUT, "WARNING: Unknown Jump/Call instruction");
376 }
377 }
378
379 static void print_system_set(command_t* command) {
380 int i;
381 /* FIXME: What about SPRM11 ? Karaoke */
382 /* Surely there must be some system set command for that ? */
383
384 switch(vm_getbits(command, 59, 4)) {
385 case 1: /* Set system reg 1 &| 2 &| 3 (Audio, Subp. Angle) */
386 for(i = 1; i <= 3; i++) {
387 if(vm_getbits(command, 47 - (i*8), 1)) {
388 print_system_reg(i);
389 fprintf(MSG_OUT, " = ");
390 print_reg_or_data_2(command, vm_getbits(command, 60, 1), 47 - (i*8) );
391 fprintf(MSG_OUT, " ");
392 }
393 }
394 break;
395 case 2: /* Set system reg 9 & 10 (Navigation timer, Title PGC number) */
396 print_system_reg(9);
397 fprintf(MSG_OUT, " = ");
398 print_reg_or_data(command, vm_getbits(command, 60, 1), 47);
399 fprintf(MSG_OUT, " ");
400 print_system_reg(10);
401 fprintf(MSG_OUT, " = %" PRIu16, vm_getbits(command, 30, 15)); /* ?? */
402 break;
403 case 3: /* Mode: Counter / Register + Set */
404 fprintf(MSG_OUT, "SetMode ");
405 if(vm_getbits(command, 23, 1))
406 fprintf(MSG_OUT, "Counter ");
407 else
408 fprintf(MSG_OUT, "Register ");
409 print_g_reg(vm_getbits(command, 19, 4));
410 print_set_op(0x1); /* '=' */
411 print_reg_or_data(command, vm_getbits(command, 60, 1), 47);
412 break;
413 case 6: /* Set system reg 8 (Highlighted button) */
414 print_system_reg(8);
415 if(vm_getbits(command, 60, 1)) /* immediate */
416 fprintf(MSG_OUT, " = 0x%x (button no %d)", vm_getbits(command, 31, 16), vm_getbits(command, 31, 6));
417 else
418 fprintf(MSG_OUT, " = g[%" PRIu8 "]", vm_getbits(command, 19, 4));
419 break;
420 default:
421 fprintf(MSG_OUT, "WARNING: Unknown system set instruction (%i)",
422 vm_getbits(command, 59, 4));
423 }
424 }
425
426 static void print_set_version_1(command_t* command) {
427 uint8_t set_op = vm_getbits(command, 59, 4);
428
429 if(set_op) {
430 print_g_reg(vm_getbits(command, 35, 4));
431 print_set_op(set_op);
432 print_reg_or_data(command, vm_getbits(command, 60, 1), 31);
433 } else {
434 fprintf(MSG_OUT, "NOP");
435 }
436 }
437
438 static void print_set_version_2(command_t* command) {
439 uint8_t set_op = vm_getbits(command, 59, 4);
440
441 if(set_op) {
442 print_g_reg(vm_getbits(command, 51, 4));
443 print_set_op(set_op);
444 print_reg_or_data(command, vm_getbits(command, 60, 1), 47);
445 } else {
446 fprintf(MSG_OUT, "NOP");
447 }
448 }
449
450 static void print_set_version_3(command_t* command) {
451 uint8_t set_op = vm_getbits(command, 59, 4);
452
453 if(set_op) {
454 print_g_reg(vm_getbits(command, 51, 4));
455 print_set_op(set_op);
456 print_reg_or_data_3(command, vm_getbits(command, 60, 1), 47);
457 } else {
458 fprintf(MSG_OUT, "NOP");
459 }
460 }
461
462
463 void vm_print_mnemonic(vm_cmd_t *vm_command) {
464 command_t command;
465 command.instruction =( (uint64_t) vm_command->bytes[0] << 56 ) |
466 ( (uint64_t) vm_command->bytes[1] << 48 ) |
467 ( (uint64_t) vm_command->bytes[2] << 40 ) |
468 ( (uint64_t) vm_command->bytes[3] << 32 ) |
469 ( (uint64_t) vm_command->bytes[4] << 24 ) |
470 ( (uint64_t) vm_command->bytes[5] << 16 ) |
471 ( (uint64_t) vm_command->bytes[6] << 8 ) |
472 (uint64_t) vm_command->bytes[7] ;
473 command.examined = 0;
474
475 switch(vm_getbits(&command,63,3)) { /* three first bits */
476 case 0: /* Special instructions */
477 print_if_version_1(&command);
478 print_special_instruction(&command);
479 break;
480 case 1: /* Jump/Call or Link instructions */
481 if(vm_getbits(&command,60,1)) {
482 print_if_version_2(&command);
483 print_jump_instruction(&command);
484 } else {
485 print_if_version_1(&command);
486 print_link_instruction(&command, 0); /* must be present */
487 }
488 break;
489 case 2: /* Set System Parameters instructions */
490 print_if_version_2(&command);
491 print_system_set(&command);
492 print_link_instruction(&command, 1); /* either 'if' or 'link' */
493 break;
494 case 3: /* Set General Parameters instructions */
495 print_if_version_3(&command);
496 print_set_version_1(&command);
497 print_link_instruction(&command, 1); /* either 'if' or 'link' */
498 break;
499 case 4: /* Set, Compare -> LinkSub instructions */
500 print_set_version_2(&command);
501 fprintf(MSG_OUT, ", ");
502 print_if_version_4(&command);
503 print_linksub_instruction(&command);
504 break;
505 case 5: /* Compare -> (Set and LinkSub) instructions */
506 print_if_version_5(&command);
507 fprintf(MSG_OUT, "{ ");
508 print_set_version_3(&command);
509 fprintf(MSG_OUT, ", ");
510 print_linksub_instruction(&command);
511 fprintf(MSG_OUT, " }");
512 break;
513 case 6: /* Compare -> Set, always LinkSub instructions */
514 print_if_version_5(&command);
515 fprintf(MSG_OUT, "{ ");
516 print_set_version_3(&command);
517 fprintf(MSG_OUT, " } ");
518 print_linksub_instruction(&command);
519 break;
520 default:
521 fprintf(MSG_OUT, "WARNING: Unknown instruction type (%i)", vm_getbits(&command, 63, 3));
522 }
523 /* Check if there still are bits set that were not examined */
524
525 if(command.instruction & ~ command.examined) {
526 fprintf(MSG_OUT, " libdvdnav: vmcmd.c: [WARNING, unknown bits:");
527 fprintf(MSG_OUT, " %08"PRIx64, (command.instruction & ~ command.examined) );
528 fprintf(MSG_OUT, "]");
529 }
530 }
531
532 void vm_print_cmd(int row, vm_cmd_t *vm_command) {
533 int i;
534
535 fprintf(MSG_OUT, "(%03d) ", row + 1);
536 for(i = 0; i < 8; i++)
537 fprintf(MSG_OUT, "%02x ", vm_command->bytes[i]);
538 fprintf(MSG_OUT, "| ");
539
540 vm_print_mnemonic(vm_command);
541 fprintf(MSG_OUT, "\n");
542 }
543