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