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