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