Debian Codegraph @

src/vm/decoder.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 <stdlib.h>
29 #include <inttypes.h>
30 #include <limits.h>
31 #include <string.h> /* For memset */
32 #include <sys/time.h>
33 #include <assert.h>
34
35 #include <dvdread/nav_types.h>
36
37 #include "dvdnav/dvdnav.h"
38 #include "decoder.h"
39 #include "vm.h"
40 #include "vmcmd.h"
41 #include "dvdnav_internal.h"
42
43 uint32_t vm_getbits(command_t *command, int32_t start, int32_t count) {
44 uint64_t result = 0;
45 uint64_t bit_mask = 0;
46 uint64_t examining = 0;
47 int32_t bits;
48
49 if (count == 0) return 0;
50
51 if ( ((start - count) < -1) ||
52 (count > 32) ||
53 (start > 63) ||
54 (count < 0) ||
55 (start < 0) ) {
56 fprintf(MSG_OUT, "libdvdnav: Bad call to vm_getbits. Parameter out of range\n");
57 abort();
58 }
59 /* all ones, please */
60 bit_mask = ~bit_mask;
61 bit_mask >>= 63 - start;
62 bits = start + 1 - count;
63 examining = ((bit_mask >> bits) << bits );
64 command->examined |= examining;
65 result = (command->instruction & bit_mask) >> bits;
66 return (uint32_t) result;
67 }
68
69 static uint16_t get_GPRM(registers_t* registers, uint8_t reg) {
70 if (registers->GPRM_mode[reg] & 0x01) {
71 struct timeval current_time, time_offset;
72 uint16_t result;
73 /* Counter mode */
74 /* fprintf(MSG_OUT, "libdvdnav: Getting counter %d\n",reg);*/
75 gettimeofday(&current_time, NULL);
76 time_offset.tv_sec = current_time.tv_sec - registers->GPRM_time[reg].tv_sec;
77 time_offset.tv_usec = current_time.tv_usec - registers->GPRM_time[reg].tv_usec;
78 if (time_offset.tv_usec < 0) {
79 time_offset.tv_sec--;
80 time_offset.tv_usec += 1000000;
81 }
82 result = (uint16_t) (time_offset.tv_sec & 0xffff);
83 registers->GPRM[reg]=result;
84 return result;
85
86 } else {
87 /* Register mode */
88 return registers->GPRM[reg];
89 }
90
91 }
92
93 static void set_GPRM(registers_t* registers, uint8_t reg, uint16_t value) {
94 if (registers->GPRM_mode[reg] & 0x01) {
95 struct timeval current_time;
96 /* Counter mode */
97 /* fprintf(MSG_OUT, "libdvdnav: Setting counter %d\n",reg); */
98 gettimeofday(&current_time, NULL);
99 registers->GPRM_time[reg] = current_time;
100 registers->GPRM_time[reg].tv_sec -= value;
101 }
102 registers->GPRM[reg] = value;
103 }
104
105 /* Eval register code, can either be system or general register.
106 SXXX_XXXX, where S is 1 if it is system register. */
107 static uint16_t eval_reg(command_t* command, uint8_t reg) {
108 if(reg & 0x80) {
109 if ((reg & 0x1f) == 20) {
110 fprintf(MSG_OUT, "libdvdnav: Suspected RCE Region Protection!!!\n");
111 }
112 return command->registers->SPRM[reg & 0x1f]; /* FIXME max 24 not 32 */
113 } else {
114 return get_GPRM(command->registers, reg & 0x0f) ;
115 }
116 }
117
118 #ifdef TRACE
119
120 static char *linkcmd2str(link_cmd_t cmd) {
121 switch(cmd) {
122 case LinkNoLink:
123 return "LinkNoLink";
124 case LinkTopC:
125 return "LinkTopC";
126 case LinkNextC:
127 return "LinkNextC";
128 case LinkPrevC:
129 return "LinkPrevC";
130 case LinkTopPG:
131 return "LinkTopPG";
132 case LinkNextPG:
133 return "LinkNextPG";
134 case LinkPrevPG:
135 return "LinkPrevPG";
136 case LinkTopPGC:
137 return "LinkTopPGC";
138 case LinkNextPGC:
139 return "LinkNextPGC";
140 case LinkPrevPGC:
141 return "LinkPrevPGC";
142 case LinkGoUpPGC:
143 return "LinkGoUpPGC";
144 case LinkTailPGC:
145 return "LinkTailPGC";
146 case LinkRSM:
147 return "LinkRSM";
148 case LinkPGCN:
149 return "LinkPGCN";
150 case LinkPTTN:
151 return "LinkPTTN";
152 case LinkPGN:
153 return "LinkPGN";
154 case LinkCN:
155 return "LinkCN";
156 case Exit:
157 return "Exit";
158 case JumpTT:
159 return "JumpTT";
160 case JumpVTS_TT:
161 return "JumpVTS_TT";
162 case JumpVTS_PTT:
163 return "JumpVTS_PTT";
164 case JumpSS_FP:
165 return "JumpSS_FP";
166 case JumpSS_VMGM_MENU:
167 return "JumpSS_VMGM_MENU";
168 case JumpSS_VTSM:
169 return "JumpSS_VTSM";
170 case JumpSS_VMGM_PGC:
171 return "JumpSS_VMGM_PGC";
172 case CallSS_FP:
173 return "CallSS_FP";
174 case CallSS_VMGM_MENU:
175 return "CallSS_VMGM_MENU";
176 case CallSS_VTSM:
177 return "CallSS_VTSM";
178 case CallSS_VMGM_PGC:
179 return "CallSS_VMGM_PGC";
180 case PlayThis:
181 return "PlayThis";
182 }
183 return "*** (bug)";
184 }
185
186 void vm_print_link(link_t value) {
187 char *cmd = linkcmd2str(value.command);
188
189 switch(value.command) {
190 case LinkNoLink:
191 case LinkTopC:
192 case LinkNextC:
193 case LinkPrevC:
194 case LinkTopPG:
195 case LinkNextPG:
196 case LinkPrevPG:
197 case LinkTopPGC:
198 case LinkNextPGC:
199 case LinkPrevPGC:
200 case LinkGoUpPGC:
201 case LinkTailPGC:
202 case LinkRSM:
203 fprintf(MSG_OUT, "libdvdnav: %s (button %d)\n", cmd, value.data1);
204 break;
205 case LinkPGCN:
206 case JumpTT:
207 case JumpVTS_TT:
208 case JumpSS_VMGM_MENU: /* == 2 -> Title Menu */
209 case JumpSS_VMGM_PGC:
210 fprintf(MSG_OUT, "libdvdnav: %s %d\n", cmd, value.data1);
211 break;
212 case LinkPTTN:
213 case LinkPGN:
214 case LinkCN:
215 fprintf(MSG_OUT, "libdvdnav: %s %d (button %d)\n", cmd, value.data1, value.data2);
216 break;
217 case Exit:
218 case JumpSS_FP:
219 case PlayThis: /* Humm.. should we have this at all.. */
220 fprintf(MSG_OUT, "libdvdnav: %s\n", cmd);
221 break;
222 case JumpVTS_PTT:
223 fprintf(MSG_OUT, "libdvdnav: %s %d:%d\n", cmd, value.data1, value.data2);
224 break;
225 case JumpSS_VTSM:
226 fprintf(MSG_OUT, "libdvdnav: %s vts %d title %d menu %d\n",
227 cmd, value.data1, value.data2, value.data3);
228 break;
229 case CallSS_FP:
230 fprintf(MSG_OUT, "libdvdnav: %s resume cell %d\n", cmd, value.data1);
231 break;
232 case CallSS_VMGM_MENU: /* == 2 -> Title Menu */
233 case CallSS_VTSM:
234 fprintf(MSG_OUT, "libdvdnav: %s %d resume cell %d\n", cmd, value.data1, value.data2);
235 break;
236 case CallSS_VMGM_PGC:
237 fprintf(MSG_OUT, "libdvdnav: %s %d resume cell %d\n", cmd, value.data1, value.data2);
238 break;
239 }
240 }
241
242 void vm_print_registers( registers_t *registers ) {
243 int32_t i;
244 fprintf(MSG_OUT, "libdvdnav: # ");
245 for(i = 0; i < 24; i++)
246 fprintf(MSG_OUT, " %2d |", i);
247 fprintf(MSG_OUT, "\nlibdvdnav: SRPMS: ");
248 for(i = 0; i < 24; i++)
249 fprintf(MSG_OUT, "%04x|", registers->SPRM[i]);
250 fprintf(MSG_OUT, "\nlibdvdnav: GRPMS: ");
251 for(i = 0; i < 16; i++)
252 fprintf(MSG_OUT, "%04x|", get_GPRM(registers, i) );
253 fprintf(MSG_OUT, "\nlibdvdnav: Gmode: ");
254 for(i = 0; i < 16; i++)
255 fprintf(MSG_OUT, "%04x|", registers->GPRM_mode[i]);
256 fprintf(MSG_OUT, "\nlibdvdnav: Gtime: ");
257 for(i = 0; i < 16; i++)
258 fprintf(MSG_OUT, "%04lx|", registers->GPRM_time[i].tv_sec & 0xffff);
259 fprintf(MSG_OUT, "\n");
260 }
261
262 #endif
263
264 /* Eval register or immediate data.
265 AAAA_AAAA BBBB_BBBB, if immediate use all 16 bits for data else use
266 lower eight bits for the system or general purpose register. */
267 static uint16_t eval_reg_or_data(command_t* command, int32_t imm, int32_t start) {
268 if(imm) { /* immediate */
269 return vm_getbits(command, start, 16);
270 } else {
271 return eval_reg(command, vm_getbits(command, (start - 8), 8));
272 }
273 }
274
275 /* Eval register or immediate data.
276 xBBB_BBBB, if immediate use all 7 bits for data else use
277 lower four bits for the general purpose register number. */
278 /* Evaluates gprm or data depending on bit, data is in byte n */
279 static uint16_t eval_reg_or_data_2(command_t* command,
280 int32_t imm, int32_t start) {
281 if(imm) /* immediate */
282 return vm_getbits(command, (start - 1), 7);
283 else
284 return get_GPRM(command->registers, (vm_getbits(command, (start - 4), 4)) );
285 }
286
287
288 /* Compare data using operation, return result from comparison.
289 Helper function for the different if functions. */
290 static int32_t eval_compare(uint8_t operation, uint16_t data1, uint16_t data2) {
291 switch(operation) {
292 case 1:
293 return data1 & data2;
294 case 2:
295 return data1 == data2;
296 case 3:
297 return data1 != data2;
298 case 4:
299 return data1 >= data2;
300 case 5:
301 return data1 > data2;
302 case 6:
303 return data1 <= data2;
304 case 7:
305 return data1 < data2;
306 }
307 fprintf(MSG_OUT, "libdvdnav: eval_compare: Invalid comparison code\n");
308 return 0;
309 }
310
311
312 /* Evaluate if version 1.
313 Has comparison data in byte 3 and 4-5 (immediate or register) */
314 static int32_t eval_if_version_1(command_t* command) {
315 uint8_t op = vm_getbits(command, 54, 3);
316 if(op) {
317 return eval_compare(op, eval_reg(command, vm_getbits(command, 39, 8)),
318 eval_reg_or_data(command, vm_getbits(command, 55, 1), 31));
319 }
320 return 1;
321 }
322
323 /* Evaluate if version 2.
324 This version only compares register which are in byte 6 and 7 */
325 static int32_t eval_if_version_2(command_t* command) {
326 uint8_t op = vm_getbits(command, 54, 3);
327 if(op) {
328 return eval_compare(op, eval_reg(command, vm_getbits(command, 15, 8)),
329 eval_reg(command, vm_getbits(command, 7, 8)));
330 }
331 return 1;
332 }
333
334 /* Evaluate if version 3.
335 Has comparison data in byte 2 and 6-7 (immediate or register) */
336 static int32_t eval_if_version_3(command_t* command) {
337 uint8_t op = vm_getbits(command, 54, 3);
338 if(op) {
339 return eval_compare(op, eval_reg(command, vm_getbits(command, 47, 8)),
340 eval_reg_or_data(command, vm_getbits(command, 55, 1), 15));
341 }
342 return 1;
343 }
344
345 /* Evaluate if version 4.
346 Has comparison data in byte 1 and 4-5 (immediate or register)
347 The register in byte 1 is only the lowe nibble (4 bits) */
348 static int32_t eval_if_version_4(command_t* command) {
349 uint8_t op = vm_getbits(command, 54, 3);
350 if(op) {
351 return eval_compare(op, eval_reg(command, vm_getbits(command, 51, 4)),
352 eval_reg_or_data(command, vm_getbits(command, 55, 1), 31));
353 }
354 return 1;
355 }
356
357 /* Evaluate special instruction.... returns the new row/line number,
358 0 if no new row and 256 if Break. */
359 static int32_t eval_special_instruction(command_t* command, int32_t cond) {
360 int32_t line, level;
361
362 switch(vm_getbits(command, 51, 4)) {
363 case 0: /* NOP */
364 line = 0;
365 return cond ? line : 0;
366 case 1: /* Goto line */
367 line = vm_getbits(command, 7, 8);
368 return cond ? line : 0;
369 case 2: /* Break */
370 /* max number of rows < 256, so we will end this set */
371 line = 256;
372 return cond ? 256 : 0;
373 case 3: /* Set temporary parental level and goto */
374 line = vm_getbits(command, 7, 8);
375 level = vm_getbits(command, 11, 4);
376 if(cond) {
377 /* This always succeeds now, if we want real parental protection */
378 /* we need to ask the user and have passwords and stuff. */
379 command->registers->SPRM[13] = level;
380 }
381 return cond ? line : 0;
382 }
383 return 0;
384 }
385
386 /* Evaluate link by subinstruction.
387 Return 1 if link, or 0 if no link
388 Actual link instruction is in return_values parameter */
389 static int32_t eval_link_subins(command_t* command, int32_t cond, link_t *return_values) {
390 uint16_t button = vm_getbits(command, 15, 6);
391 uint8_t linkop = vm_getbits(command, 4, 5);
392
393 if(linkop > 0x10)
394 return 0; /* Unknown Link by Sub-Instruction command */
395
396 /* Assumes that the link_cmd_t enum has the same values as the LinkSIns codes */
397 return_values->command = linkop;
398 return_values->data1 = button;
399 return cond;
400 }
401
402
403 /* Evaluate link instruction.
404 Return 1 if link, or 0 if no link
405 Actual link instruction is in return_values parameter */
406 static int32_t eval_link_instruction(command_t* command, int32_t cond, link_t *return_values) {
407 uint8_t op = vm_getbits(command, 51, 4);
408
409 switch(op) {
410 case 1:
411 return eval_link_subins(command, cond, return_values);
412 case 4:
413 return_values->command = LinkPGCN;
414 return_values->data1 = vm_getbits(command, 14, 15);
415 return cond;
416 case 5:
417 return_values->command = LinkPTTN;
418 return_values->data1 = vm_getbits(command, 9, 10);
419 return_values->data2 = vm_getbits(command, 15, 6);
420 return cond;
421 case 6:
422 return_values->command = LinkPGN;
423 return_values->data1 = vm_getbits(command, 6, 7);
424 return_values->data2 = vm_getbits(command, 15, 6);
425 return cond;
426 case 7:
427 return_values->command = LinkCN;
428 return_values->data1 = vm_getbits(command, 7, 8);
429 return_values->data2 = vm_getbits(command, 15, 6);
430 return cond;
431 }
432 return 0;
433 }
434
435
436 /* Evaluate a jump instruction.
437 returns 1 if jump or 0 if no jump
438 actual jump instruction is in return_values parameter */
439 static int32_t eval_jump_instruction(command_t* command, int32_t cond, link_t *return_values) {
440
441 switch(vm_getbits(command, 51, 4)) {
442 case 1:
443 return_values->command = Exit;
444 return cond;
445 case 2:
446 return_values->command = JumpTT;
447 return_values->data1 = vm_getbits(command, 22, 7);
448 return cond;
449 case 3:
450 return_values->command = JumpVTS_TT;
451 return_values->data1 = vm_getbits(command, 22, 7);
452 return cond;
453 case 5:
454 return_values->command = JumpVTS_PTT;
455 return_values->data1 = vm_getbits(command, 22, 7);
456 return_values->data2 = vm_getbits(command, 41, 10);
457 return cond;
458 case 6:
459 switch(vm_getbits(command, 23, 2)) {
460 case 0:
461 return_values->command = JumpSS_FP;
462 return cond;
463 case 1:
464 return_values->command = JumpSS_VMGM_MENU;
465 return_values->data1 = vm_getbits(command, 19, 4);
466 return cond;
467 case 2:
468 return_values->command = JumpSS_VTSM;
469 return_values->data1 = vm_getbits(command, 31, 8);
470 return_values->data2 = vm_getbits(command, 39, 8);
471 return_values->data3 = vm_getbits(command, 19, 4);
472 return cond;
473 case 3:
474 return_values->command = JumpSS_VMGM_PGC;
475 return_values->data1 = vm_getbits(command, 46, 15);
476 return cond;
477 }
478 break;
479 case 8:
480 switch(vm_getbits(command, 23, 2)) {
481 case 0:
482 return_values->command = CallSS_FP;
483 return_values->data1 = vm_getbits(command, 31, 8);
484 return cond;
485 case 1:
486 return_values->command = CallSS_VMGM_MENU;
487 return_values->data1 = vm_getbits(command, 19, 4);
488 return_values->data2 = vm_getbits(command, 31, 8);
489 return cond;
490 case 2:
491 return_values->command = CallSS_VTSM;
492 return_values->data1 = vm_getbits(command, 19, 4);
493 return_values->data2 = vm_getbits(command, 31, 8);
494 return cond;
495 case 3:
496 return_values->command = CallSS_VMGM_PGC;
497 return_values->data1 = vm_getbits(command, 46, 15);
498 return_values->data2 = vm_getbits(command, 31, 8);
499 return cond;
500 }
501 break;
502 }
503 return 0;
504 }
505
506 /* Evaluate a set system register instruction
507 May contain a link so return the same as eval_link */
508 static int32_t eval_system_set(command_t* command, int32_t cond, link_t *return_values) {
509 int32_t i;
510 uint16_t data, data2;
511
512 switch(vm_getbits(command, 59, 4)) {
513 case 1: /* Set system reg 1 &| 2 &| 3 (Audio, Subp. Angle) */
514 for(i = 1; i <= 3; i++) {
515 if(vm_getbits(command, 63 - ((2 + i)*8), 1)) {
516 data = eval_reg_or_data_2(command, vm_getbits(command, 60, 1), (47 - (i*8)));
517 if(cond) {
518 command->registers->SPRM[i] = data;
519 }
520 }
521 }
522 break;
523 case 2: /* Set system reg 9 & 10 (Navigation timer, Title PGC number) */
524 data = eval_reg_or_data(command, vm_getbits(command, 60, 1), 47);
525 data2 = vm_getbits(command, 23, 8); /* ?? size */
526 if(cond) {
527 command->registers->SPRM[9] = data; /* time */
528 command->registers->SPRM[10] = data2; /* pgcN */
529 }
530 break;
531 case 3: /* Mode: Counter / Register + Set */
532 data = eval_reg_or_data(command, vm_getbits(command, 60, 1), 47);
533 data2 = vm_getbits(command, 19, 4);
534 if(vm_getbits(command, 23, 1)) {
535 command->registers->GPRM_mode[data2] |= 1; /* Set bit 0 */
536 } else {
537 command->registers->GPRM_mode[data2] &= ~ 0x01; /* Reset bit 0 */
538 }
539 if(cond) {
540 set_GPRM(command->registers, data2, data);
541 }
542 break;
543 case 6: /* Set system reg 8 (Highlighted button) */
544 data = eval_reg_or_data(command, vm_getbits(command, 60, 1), 31); /* Not system reg!! */
545 if(cond) {
546 command->registers->SPRM[8] = data;
547 }
548 break;
549 }
550 if(vm_getbits(command, 51, 4)) {
551 return eval_link_instruction(command, cond, return_values);
552 }
553 return 0;
554 }
555
556
557 /* Evaluate set operation
558 Sets the register given to the value indicated by op and data.
559 For the swap case the contents of reg is stored in reg2.
560 */
561 static void eval_set_op(command_t* command, int32_t op, int32_t reg, int32_t reg2, int32_t data) {
562 static const int32_t shortmax = 0xffff;
563 int32_t tmp;
564 switch(op) {
565 case 1:
566 set_GPRM(command->registers, reg, data);
567 break;
568 case 2: /* SPECIAL CASE - SWAP! */
569 set_GPRM(command->registers, reg2, get_GPRM(command->registers, reg));
570 set_GPRM(command->registers, reg, data);
571 break;
572 case 3:
573 tmp = get_GPRM(command->registers, reg) + data;
574 if(tmp > shortmax) tmp = shortmax;
575 set_GPRM(command->registers, reg, (uint16_t)tmp);
576 break;
577 case 4:
578 tmp = get_GPRM(command->registers, reg) - data;
579 if(tmp < 0) tmp = 0;
580 set_GPRM(command->registers, reg, (uint16_t)tmp);
581 break;
582 case 5:
583 tmp = get_GPRM(command->registers, reg) * data;
584 if(tmp > shortmax) tmp = shortmax;
585 set_GPRM(command->registers, reg, (uint16_t)tmp);
586 break;
587 case 6:
588 if (data != 0) {
589 set_GPRM(command->registers, reg, (get_GPRM(command->registers, reg) / data) );
590 } else {
591 set_GPRM(command->registers, reg, 0xffff); /* Avoid that divide by zero! */
592 }
593 break;
594 case 7:
595 if (data != 0) {
596 set_GPRM(command->registers, reg, (get_GPRM(command->registers, reg) % data) );
597 } else {
598 set_GPRM(command->registers, reg, 0xffff); /* Avoid that divide by zero! */
599 }
600 break;
601 case 8: /* SPECIAL CASE - RND! Return numbers between 1 and data. */
602 set_GPRM(command->registers, reg, 1 + ((uint16_t) ((float) data * rand()/(RAND_MAX+1.0))) );
603 break;
604 case 9:
605 set_GPRM(command->registers, reg, (get_GPRM(command->registers, reg) & data) );
606 break;
607 case 10:
608 set_GPRM(command->registers, reg, (get_GPRM(command->registers, reg) | data) );
609 break;
610 case 11:
611 set_GPRM(command->registers, reg, (get_GPRM(command->registers, reg) ^ data) );
612 break;
613 }
614 }
615
616 /* Evaluate set instruction, combined with either Link or Compare. */
617 static void eval_set_version_1(command_t* command, int32_t cond) {
618 uint8_t op = vm_getbits(command, 59, 4);
619 uint8_t reg = vm_getbits(command, 35, 4); /* FIXME: This is different from vmcmd.c!!! */
620 uint8_t reg2 = vm_getbits(command, 19, 4);
621 uint16_t data = eval_reg_or_data(command, vm_getbits(command, 60, 1), 31);
622
623 if(cond) {
624 eval_set_op(command, op, reg, reg2, data);
625 }
626 }
627
628
629 /* Evaluate set instruction, combined with both Link and Compare. */
630 static void eval_set_version_2(command_t* command, int32_t cond) {
631 uint8_t op = vm_getbits(command, 59, 4);
632 uint8_t reg = vm_getbits(command, 51, 4);
633 uint8_t reg2 = vm_getbits(command, 35, 4); /* FIXME: This is different from vmcmd.c!!! */
634 uint16_t data = eval_reg_or_data(command, vm_getbits(command, 60, 1), 47);
635
636 if(cond) {
637 eval_set_op(command, op, reg, reg2, data);
638 }
639 }
640
641
642 /* Evaluate a command
643 returns row number of goto, 0 if no goto, -1 if link.
644 Link command in return_values */
645 static int32_t eval_command(const uint8_t *bytes, registers_t* registers, link_t *return_values) {
646 int32_t cond, res = 0;
647 command_t command;
648 command.instruction =( (uint64_t) bytes[0] << 56 ) |
649 ( (uint64_t) bytes[1] << 48 ) |
650 ( (uint64_t) bytes[2] << 40 ) |
651 ( (uint64_t) bytes[3] << 32 ) |
652 ( (uint64_t) bytes[4] << 24 ) |
653 ( (uint64_t) bytes[5] << 16 ) |
654 ( (uint64_t) bytes[6] << 8 ) |
655 (uint64_t) bytes[7] ;
656 command.examined = 0;
657 command.registers = registers;
658 memset(return_values, 0, sizeof(link_t));
659
660 switch(vm_getbits(&command, 63, 3)) { /* three first old_bits */
661 case 0: /* Special instructions */
662 cond = eval_if_version_1(&command);
663 res = eval_special_instruction(&command, cond);
664 if(res == -1) {
665 fprintf(MSG_OUT, "libdvdnav: Unknown Instruction!\n");
666 assert(0);
667 }
668 break;
669 case 1: /* Link/jump instructions */
670 if(vm_getbits(&command, 60, 1)) {
671 cond = eval_if_version_2(&command);
672 res = eval_jump_instruction(&command, cond, return_values);
673 } else {
674 cond = eval_if_version_1(&command);
675 res = eval_link_instruction(&command, cond, return_values);
676 }
677 if(res)
678 res = -1;
679 break;
680 case 2: /* System set instructions */
681 cond = eval_if_version_2(&command);
682 res = eval_system_set(&command, cond, return_values);
683 if(res)
684 res = -1;
685 break;
686 case 3: /* Set instructions, either Compare or Link may be used */
687 cond = eval_if_version_3(&command);
688 eval_set_version_1(&command, cond);
689 if(vm_getbits(&command, 51, 4)) {
690 res = eval_link_instruction(&command, cond, return_values);
691 }
692 if(res)
693 res = -1;
694 break;
695 case 4: /* Set, Compare -> Link Sub-Instruction */
696 eval_set_version_2(&command, /*True*/ 1);
697 cond = eval_if_version_4(&command);
698 res = eval_link_subins(&command, cond, return_values);
699 if(res)
700 res = -1;
701 break;
702 case 5: /* Compare -> (Set and Link Sub-Instruction) */
703 /* FIXME: These are wrong. Need to be updated from vmcmd.c */
704 cond = eval_if_version_4(&command);
705 eval_set_version_2(&command, cond);
706 res = eval_link_subins(&command, cond, return_values);
707 if(res)
708 res = -1;
709 break;
710 case 6: /* Compare -> Set, always Link Sub-Instruction */
711 /* FIXME: These are wrong. Need to be updated from vmcmd.c */
712 cond = eval_if_version_4(&command);
713 eval_set_version_2(&command, cond);
714 res = eval_link_subins(&command, /*True*/ 1, return_values);
715 if(res)
716 res = -1;
717 break;
718 default: /* Unknown command type */
719 fprintf(MSG_OUT, "libdvdnav: WARNING: Unknown Command Type=%x\n", vm_getbits(&command, 63, 3));
720 }
721 /* Check if there are bits not yet examined */
722
723 if(command.instruction & ~ command.examined) {
724 fprintf(MSG_OUT, "libdvdnav: decoder.c: [WARNING, unknown bits:");
725 fprintf(MSG_OUT, " %08"PRIx64, (command.instruction & ~ command.examined) );
726 fprintf(MSG_OUT, "]\n");
727 }
728
729 return res;
730 }
731
732 /* Evaluate a set of commands in the given register set (which is modified) */
733 int32_t vmEval_CMD(const vm_cmd_t commands[], int32_t num_commands,
734 registers_t *registers, link_t *return_values) {
735 int32_t i = 0;
736 int32_t total = 0;
737
738 #ifdef TRACE
739 /* DEBUG */
740 fprintf(MSG_OUT, "libdvdnav: Registers before transaction\n");
741 vm_print_registers( registers );
742 fprintf(MSG_OUT, "libdvdnav: Full list of commands to execute\n");
743 for(i = 0; i < num_commands; i++)
744 vm_print_cmd(i, &commands[i]);
745 fprintf(MSG_OUT, "libdvdnav: --------------------------------------------\n");
746 fprintf(MSG_OUT, "libdvdnav: Single stepping commands\n");
747 #endif
748
749 i = 0;
750 while(i < num_commands && total < 100000) {
751 int32_t line;
752
753 #ifdef TRACE
754 vm_print_cmd(i, &commands[i]);
755 #endif
756
757 line = eval_command(&commands[i].bytes[0], registers, return_values);
758
759 if (line < 0) { /* Link command */
760 #ifdef TRACE
761 fprintf(MSG_OUT, "libdvdnav: Registers after transaction\n");
762 vm_print_registers( registers );
763 fprintf(MSG_OUT, "libdvdnav: eval: Doing Link/Jump/Call\n");
764 #endif
765 return 1;
766 }
767
768 if (line > 0) /* Goto command */
769 i = line - 1;
770 else /* Just continue on the next line */
771 i++;
772
773 total++;
774 }
775
776 memset(return_values, 0, sizeof(link_t));
777 #ifdef TRACE
778 fprintf(MSG_OUT, "libdvdnav: Registers after transaction\n");
779 vm_print_registers( registers );
780 #endif
781 return 0;
782 }
783