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