hdmv_vm.c 26.4 KB
Newer Older
hpi1's avatar
hpi1 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*
 * This file is part of libbluray
 * Copyright (C) 2010  hpi1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include "hdmv_vm.h"

#include "mobj_parse.h"
#include "hdmv_insn.h"
#include "../register.h"

#include "util/macro.h"
#include "util/strutl.h"
#include "util/logging.h"

#include <stdlib.h>
#include <string.h>
hpi1's avatar
hpi1 committed
32
#include <sys/time.h>
hpi1's avatar
hpi1 committed
33 34


hpi1's avatar
hpi1 committed
35 36 37 38 39
typedef struct {
    struct timeval time;
    uint32_t mobj_id;
} NV_TIMER;

hpi1's avatar
hpi1 committed
40 41 42 43 44 45 46 47
struct hdmv_vm_s {
    /* state */
    uint32_t       pc;            /* program counter */
    BD_REGISTERS  *regs;          /* player registers */
    MOBJ_OBJECT   *object;        /* currently running object code */

    HDMV_EVENT     event[5];      /* pending events to return */

hpi1's avatar
hpi1 committed
48 49
    NV_TIMER       nv_timer;      /* navigation timer */

hpi1's avatar
hpi1 committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /* movie objects */
    MOBJ_OBJECTS  *movie_objects; /* disc movie objects */
    MOBJ_OBJECT   *ig_object;     /* current object from IG stream */

    /* suspended object */
    MOBJ_OBJECT *suspended_object;
    int          suspended_pc;
};

/*
 * registers: PSR and GPR access
 */

#define PSR_FLAG 0x80000000

static int _is_valid_reg(uint32_t reg)
{
    if (reg & PSR_FLAG) {
        if (reg & ~0x8000007f) {
            return 0;
        }
    }  else {
        if (reg & ~0x00000fff) {
            return 0;
        }
    }
    return 1;
}

static int _store_reg(HDMV_VM *p, uint32_t reg, uint32_t val)
{
    if (!_is_valid_reg(reg)) {
        DEBUG(DBG_HDMV, "_store_reg(): invalid register 0x%x\n", reg);
        return -1;
    }

    if (reg & PSR_FLAG) {
87 88
        DEBUG(DBG_HDMV, "_store_reg(): storing to PSR is not allowed\n");
        return -1;
hpi1's avatar
hpi1 committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    }  else {
        return bd_gpr_write(p->regs, reg, val);
    }
}

static uint32_t _read_reg(HDMV_VM *p, uint32_t reg)
{
    if (!_is_valid_reg(reg)) {
        DEBUG(DBG_HDMV, "_read_reg(): invalid register 0x%x\n", reg);
        return 0;
    }

    if (reg & PSR_FLAG) {
        return bd_psr_read(p->regs, reg & 0x7f);
    } else {
        return bd_gpr_read(p->regs, reg);
    }
}

108 109 110 111 112 113 114 115 116 117 118 119
static uint32_t _read_setstream_regs(HDMV_VM *p, uint32_t val)
{
    uint32_t flags = val & 0xf000f000;
    uint32_t reg0 = val & 0xfff;
    uint32_t reg1 = (val >> 16) & 0xfff;

    uint32_t val0 = bd_gpr_read(p->regs, reg0) & 0x0fff;
    uint32_t val1 = bd_gpr_read(p->regs, reg1) & 0x0fff;

    return flags | val0 | (val1 << 16);
}

120 121 122 123 124 125 126 127 128 129
static uint32_t _read_setbuttonpage_reg(HDMV_VM *p, uint32_t val)
{
    uint32_t flags = val & 0xc0000000;
    uint32_t reg0  = val & 0x00000fff;

    uint32_t val0  = bd_gpr_read(p->regs, reg0) & 0x3fffffff;

    return flags | val0;
}

hpi1's avatar
hpi1 committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static int _store_result(HDMV_VM *p, MOBJ_CMD *cmd, uint32_t src, uint32_t dst, uint32_t src0, uint32_t dst0)
{
    int ret = 0;

    /* store result to destination register(s) */
    if (dst != dst0) {
        if (cmd->insn.imm_op1) {
            DEBUG(DBG_HDMV|DBG_CRIT, "ERROR: storing to imm ! ");
            return -1;
        }
        ret = _store_reg(p, cmd->dst, dst);
    }

    if (src != src0) {
        if (cmd->insn.imm_op1) {
            DEBUG(DBG_HDMV|DBG_CRIT, "ERROR: storing to imm ! ");
            return -1;
        }
        ret += _store_reg(p, cmd->src, src);
    }

    return ret;
}

154
static uint32_t _fetch_operand(HDMV_VM *p, int setstream, int setbuttonpage, int imm, uint32_t value)
155
{
hpi1's avatar
hpi1 committed
156 157 158
    if (imm) {
        return value;
    }
159

hpi1's avatar
hpi1 committed
160 161
    if (setstream) {
        return _read_setstream_regs(p, value);
162

163
    } else if (setbuttonpage) {
hpi1's avatar
hpi1 committed
164
        return _read_setbuttonpage_reg(p, value);
165

166
    } else {
hpi1's avatar
hpi1 committed
167
        return _read_reg(p, value);
168 169 170 171 172 173 174 175 176 177 178
    }
}

static void _fetch_operands(HDMV_VM *p, MOBJ_CMD *cmd, uint32_t *dst, uint32_t *src)
{
    HDMV_INSN *insn = &cmd->insn;

    int setstream = (insn->grp     == INSN_GROUP_SET &&
                     insn->sub_grp == SET_SETSYSTEM  &&
                     (  insn->set_opt == INSN_SET_STREAM ||
                        insn->set_opt == INSN_SET_SEC_STREAM));
179 180 181
    int setbuttonpage = (insn->grp     == INSN_GROUP_SET &&
                         insn->sub_grp == SET_SETSYSTEM  &&
                         insn->set_opt == INSN_SET_BUTTON_PAGE);
182 183 184 185

    *dst = *src = 0;

    if (insn->op_cnt > 0) {
186
        *dst = _fetch_operand(p, setstream, setbuttonpage, insn->imm_op1, cmd->dst);
187 188 189
    }

    if (insn->op_cnt > 1) {
190
        *src = _fetch_operand(p, setstream, setbuttonpage, insn->imm_op2, cmd->src);
191 192 193
    }
}

hpi1's avatar
hpi1 committed
194 195 196 197 198 199 200 201 202 203 204 205
/*
 * event queue
 */

static int _get_event(HDMV_VM *p, HDMV_EVENT *ev)
{
    if (p->event[0].event != HDMV_EVENT_NONE) {
        *ev = p->event[0];
        memmove(p->event, p->event + 1, sizeof(p->event) - sizeof(p->event[0]));
        return 0;
    }

hpi1's avatar
hpi1 committed
206 207
    ev->event = HDMV_EVENT_NONE;

hpi1's avatar
hpi1 committed
208 209 210
    return -1;
}

hpi1's avatar
hpi1 committed
211
static int _queue_event(HDMV_VM *p, uint32_t event, uint32_t param)
hpi1's avatar
hpi1 committed
212 213 214
{
    unsigned i;
    for (i = 0; i < sizeof(p->event) / sizeof(p->event[0]) - 1; i++) {
hpi1's avatar
hpi1 committed
215
        if (p->event[i].event == HDMV_EVENT_NONE) {
hpi1's avatar
hpi1 committed
216 217
            p->event[i].event = event;
            p->event[i].param = param;
hpi1's avatar
hpi1 committed
218 219 220 221
            return 0;
        }
    }

hpi1's avatar
hpi1 committed
222
    DEBUG(DBG_HDMV|DBG_CRIT, "_queue_event(%d, %d): queue overflow !\n", event, param);
hpi1's avatar
hpi1 committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    return -1;
}

/*
 * vm init
 */

HDMV_VM *hdmv_vm_init(const char *disc_root, BD_REGISTERS *regs)
{
    HDMV_VM *p = calloc(1, sizeof(HDMV_VM));
    char *file;

    /* read movie objects */
    file = str_printf("%s/BDMV/MovieObject.bdmv", disc_root);
    p->movie_objects = mobj_parse(file);
    X_FREE(file);
    if (!p->movie_objects) {
        X_FREE(p);
        return NULL;
    }

    p->regs         = regs;

    return  p;
}

249
void hdmv_vm_free(HDMV_VM **p)
hpi1's avatar
hpi1 committed
250
{
251
    if (p && *p) {
hpi1's avatar
hpi1 committed
252

253 254 255 256 257 258
      mobj_free(&(*p)->movie_objects);

        if ((*p)->ig_object) {
            X_FREE((*p)->ig_object->cmds);
            X_FREE((*p)->ig_object);
        }
hpi1's avatar
hpi1 committed
259

260 261
        X_FREE(*p);
    }
hpi1's avatar
hpi1 committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

/*
 * suspend/resume ("function call")
 */

static void _suspend_object(HDMV_VM *p)
{
    DEBUG(DBG_HDMV, "_suspend_object()\n");

    if (p->suspended_object) {
        DEBUG(DBG_HDMV, "_suspend_object: object already suspended !\n");
        // [execute the call, discard old suspended object (10.2.4.2.2)].
    }

    bd_psr_save_state(p->regs);

    p->suspended_object = p->object;
    p->suspended_pc     = p->pc;

282
    p->object = NULL;
hpi1's avatar
hpi1 committed
283 284 285 286 287 288 289 290 291 292
}

static int _resume_object(HDMV_VM *p)
{
    if (!p->suspended_object) {
        DEBUG(DBG_HDMV|DBG_CRIT, "_resume_object: no suspended object!\n");
        return -1;
    }

    p->object = p->suspended_object;
293
    p->pc     = p->suspended_pc;
hpi1's avatar
hpi1 committed
294 295 296

    bd_psr_restore_state(p->regs);

297
    DEBUG(DBG_HDMV, "resuming object %p at %d\n", p->object, p->pc + 1);
hpi1's avatar
hpi1 committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317

    p->suspended_object = NULL;

    return 0;
}


/*
 * branching
 */

static int _jump_object(HDMV_VM *p, int object)
{
    if (object < 0 || object > p->movie_objects->num_objects) {
        DEBUG(DBG_HDMV|DBG_CRIT, "_jump_object(): invalid object %d\n", object);
        return -1;
    }

    DEBUG(DBG_HDMV, "_jump_object(): jumping to object %d\n", object);

318
    hdmv_vm_select_object(p, object);
hpi1's avatar
hpi1 committed
319 320 321 322 323 324

    return 0;
}

static int _jump_title(HDMV_VM *p, int title)
{
hpi1's avatar
hpi1 committed
325
    if (title >= 0 && title <= 0xffff) {
hpi1's avatar
hpi1 committed
326
        DEBUG(DBG_HDMV, "_jump_title(%d)\n", title);
327

hpi1's avatar
hpi1 committed
328 329 330 331 332
        if (p->suspended_object) {
            /* discard suspended object */
            p->suspended_object = NULL;
            bd_psr_restore_state(p->regs);
        }
hpi1's avatar
hpi1 committed
333

hpi1's avatar
hpi1 committed
334
        _queue_event(p, HDMV_EVENT_TITLE, title);
hpi1's avatar
hpi1 committed
335 336 337
        return 0;
    }

hpi1's avatar
hpi1 committed
338 339
    DEBUG(DBG_HDMV|DBG_CRIT, "_jump_title(%d): invalid title number\n", title);

hpi1's avatar
hpi1 committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353
    return -1;
}

static int _call_object(HDMV_VM *p, int object)
{
    DEBUG(DBG_HDMV, "_call_object(%d)\n", object);

    _suspend_object(p);

    return _jump_object(p, object);
}

static int _call_title(HDMV_VM *p, int title)
{
hpi1's avatar
hpi1 committed
354
    if (title >= 0 && title <= 0xffff) {
hpi1's avatar
hpi1 committed
355
        DEBUG(DBG_HDMV, "_call_title(%d)\n", title);
hpi1's avatar
hpi1 committed
356

hpi1's avatar
hpi1 committed
357
        _suspend_object(p);
hpi1's avatar
hpi1 committed
358

hpi1's avatar
hpi1 committed
359
        _queue_event(p, HDMV_EVENT_TITLE, title);
hpi1's avatar
hpi1 committed
360 361 362
        return 0;
    }

hpi1's avatar
hpi1 committed
363 364
    DEBUG(DBG_HDMV|DBG_CRIT, "_call_title(%d): invalid title number\n", title);

hpi1's avatar
hpi1 committed
365 366 367 368 369 370 371 372 373
    return -1;
}

/*
 * playback control
 */

static int _play_at(HDMV_VM *p, int playlist, int playitem, int playmark)
{
374 375 376 377 378 379 380 381 382 383 384 385 386 387
    if (p->ig_object && playlist >= 0) {
        DEBUG(DBG_HDMV, "play_at(list %d, item %d, mark %d): "
              "playlist change not allowed in interactive composition\n",
              playlist, playitem, playmark);
        return -1;
    }

    if (!p->ig_object && playlist < 0) {
        DEBUG(DBG_HDMV, "play_at(list %d, item %d, mark %d): "
              "playlist not given in movie object (link commands not allowed)\n",
              playlist, playitem, playmark);
        return -1;
    }

hpi1's avatar
hpi1 committed
388
    if (playlist >= 0) {
hpi1's avatar
hpi1 committed
389
        _queue_event(p, HDMV_EVENT_PLAY_PL, playlist);
hpi1's avatar
hpi1 committed
390 391 392
    }

    if (playitem >= 0) {
hpi1's avatar
hpi1 committed
393
        _queue_event(p, HDMV_EVENT_PLAY_PI, playitem);
hpi1's avatar
hpi1 committed
394 395 396
    }

    if (playmark >= 0) {
hpi1's avatar
hpi1 committed
397
        _queue_event(p, HDMV_EVENT_PLAY_PM, playmark);
hpi1's avatar
hpi1 committed
398 399
    }

400
    DEBUG(DBG_HDMV, "play_at(list %d, item %d, mark %d)\n",
hpi1's avatar
hpi1 committed
401 402 403 404 405 406 407
          playlist, playitem, playmark);

    return 0;
}

static int _play_stop(HDMV_VM *p)
{
408 409 410 411 412
    if (!p->ig_object) {
        DEBUG(DBG_HDMV, "_play_stop() not allowed in movie object\n");
        return -1;
    }

hpi1's avatar
hpi1 committed
413
    DEBUG(DBG_HDMV, "_play_stop()\n");
hpi1's avatar
hpi1 committed
414
    _queue_event(p, HDMV_EVENT_PLAY_STOP, 0);
hpi1's avatar
hpi1 committed
415 416 417
    return 0;
}

hpi1's avatar
hpi1 committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/*
 * SET/SYSTEM setstream
 */

static void _set_stream(HDMV_VM *p, uint32_t dst, uint32_t src)
{
    DEBUG(DBG_HDMV, "_set_stream(0x%x, 0x%x)\n", dst, src);

    /* primary audio stream */
    if (dst & 0x80000000) {
        bd_psr_write(p->regs, PSR_PRIMARY_AUDIO_ID, (dst >> 16) & 0xfff);
    }

    /* IG stream */
    if (src & 0x80000000) {
        bd_psr_write(p->regs, PSR_IG_STREAM_ID, (src >> 16) & 0xff);
    }

    /* angle number */
    if (src & 0x8000) {
        bd_psr_write(p->regs, PSR_ANGLE_NUMBER, src & 0xff);
    }

    /* PSR2 */

443 444
    bd_psr_lock(p->regs);

445
    uint32_t psr2 = bd_psr_read(p->regs, PSR_PG_STREAM);
hpi1's avatar
hpi1 committed
446 447 448 449 450 451 452 453 454 455 456

    /* PG TextST stream number */
    if (dst & 0x8000) {
        uint32_t text_st_num = dst & 0xfff;
        psr2 = text_st_num | (psr2 & 0xfffff000);
    }

    /* Update PG TextST stream display flag */
    uint32_t disp_s_flag = (dst & 0x4000) << 17;
    psr2 = disp_s_flag | (psr2 & 0x7fffffff);

457
    bd_psr_write(p->regs, PSR_PG_STREAM, psr2);
458 459

    bd_psr_unlock(p->regs);
hpi1's avatar
hpi1 committed
460 461 462 463 464 465 466 467 468 469 470 471
}

static void _set_sec_stream(HDMV_VM *p, uint32_t dst, uint32_t src)
{
    DEBUG(DBG_HDMV, "_set_sec_stream(0x%x, 0x%x)\n", dst, src);

    uint32_t disp_v_flag   = (dst >> 30) & 1;
    uint32_t disp_a_flag   = (src >> 30) & 1;
    uint32_t text_st_flags = (src >> 13) & 3;

    /* PSR14 */

472 473
    bd_psr_lock(p->regs);

hpi1's avatar
hpi1 committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    uint32_t psr14 = bd_psr_read(p->regs, PSR_SECONDARY_AUDIO_VIDEO);

    /* secondary video */
    if (dst & 0x80000000) {
      uint32_t sec_video = dst & 0xff;
      psr14 = (sec_video << 8) | (psr14 & 0xffff00ff);
    }

    /* secondary video size */
    if (dst & 0x00800000) {
      uint32_t video_size = (dst >> 16) & 0xf;
      psr14 = (video_size << 24) | (psr14 & 0xf0ffffff);
    }

    /* secondary audio */
    if (src & 0x80000000) {
      uint32_t sec_audio = (src >> 16) & 0xff;
      psr14 = sec_audio | (psr14 & 0xffffff00);
    }

    psr14 = (disp_v_flag << 31) | (psr14 & 0x7fffffff);
    psr14 = (disp_a_flag << 30) | (psr14 & 0xbfffffff);

    bd_psr_write(p->regs, PSR_SECONDARY_AUDIO_VIDEO, psr14);

    /* PSR2 */

501
    uint32_t psr2  = bd_psr_read(p->regs, PSR_PG_STREAM);
hpi1's avatar
hpi1 committed
502 503 504 505 506 507 508 509 510

    /* PiP PG TextST stream */
    if (src & 0x8000) {
        uint32_t stream = src & 0xfff;
        psr2 = (stream << 16) | (psr2 & 0xf000ffff);
    }

    psr2 = (text_st_flags << 30) | (psr2 & 0x3fffffff);

511
    bd_psr_write(p->regs, PSR_PG_STREAM, psr2);
512 513

    bd_psr_unlock(p->regs);
hpi1's avatar
hpi1 committed
514 515 516 517 518 519 520 521 522
}

/*
 * SET/SYSTEM navigation control
 */

static void _set_button_page(HDMV_VM *p, uint32_t dst, uint32_t src)
{
    if (p->ig_object) {
523 524 525 526 527
        uint32_t param;
        param =  (src & 0xc0000000) |        /* page and effects flags */
                ((dst & 0x80000000) >> 2) |  /* button flag */
                ((src & 0x000000ff) << 16) | /* page id */
                 (dst & 0x0000ffff);         /* button id */
hpi1's avatar
hpi1 committed
528

529
         _queue_event(p, HDMV_EVENT_SET_BUTTON_PAGE, param);
hpi1's avatar
hpi1 committed
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584

        return;
    }

    /* selected button */
    if (dst & 0x80000000) {
        bd_psr_write(p->regs, PSR_SELECTED_BUTTON_ID, dst & 0xffff);
    }

    /* active page */
    if (src & 0x80000000) {
        bd_psr_write(p->regs, PSR_MENU_PAGE_ID, src & 0xff);
    }
}

static void _enable_button(HDMV_VM *p, uint32_t dst, int enable)
{
    /* not valid in movie objects */
    if (p->ig_object) {
        if (enable) {
            _queue_event(p, HDMV_EVENT_ENABLE_BUTTON,  dst);
        } else {
            _queue_event(p, HDMV_EVENT_DISABLE_BUTTON, dst);
        }
    }
}

static void _set_still_mode(HDMV_VM *p, int enable)
{
    /* not valid in movie objects */
    if (p->ig_object) {
        _queue_event(p, HDMV_EVENT_STILL, enable);
    }
}

static void _popup_off(HDMV_VM *p)
{
    /* not valid in movie objects */
    if (p->ig_object) {
        _queue_event(p, HDMV_EVENT_POPUP_OFF, 1);
    }
}

/*
 * navigation timer
 */

static void _set_nv_timer(HDMV_VM *p, uint32_t dst, uint32_t src)
{
  uint32_t mobj_id = dst & 0xffff;
  uint32_t timeout = src & 0xffff;

  if (!timeout) {
    /* cancel timer */
    p->nv_timer.time.tv_sec = 0;
hpi1's avatar
hpi1 committed
585 586 587

    bd_psr_write(p->regs, PSR_NAV_TIMER, 0);

hpi1's avatar
hpi1 committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
    return;
  }

  /* validate params */
  if (mobj_id >= p->movie_objects->num_objects) {
      DEBUG(DBG_HDMV|DBG_CRIT, "_set_nv_timer(): invalid object id (%d) !\n", mobj_id);
      return;
  }
  if (timeout > 300) {
      DEBUG(DBG_HDMV|DBG_CRIT, "_set_nv_timer(): invalid timeout (%d) !\n", timeout);
      return;
  }

  /* set expiration time */
  gettimeofday(&p->nv_timer.time, NULL);
  p->nv_timer.time.tv_sec += timeout;

  p->nv_timer.mobj_id = mobj_id;
hpi1's avatar
hpi1 committed
606 607

  bd_psr_write(p->regs, PSR_NAV_TIMER, timeout);
hpi1's avatar
hpi1 committed
608 609
}

610 611
/* Unused function.
 * Commenting out to disable "‘_check_nv_timer’ defined but not used" warning
hpi1's avatar
hpi1 committed
612 613 614 615 616 617 618 619 620 621 622 623
static int _check_nv_timer(HDMV_VM *p)
{
    if (p->nv_timer.time.tv_sec > 0) {
        struct timeval now;
        gettimeofday(&now, NULL);

        if (now.tv_sec > p->nv_timer.time.tv_sec ||
            ( now.tv_sec  == p->nv_timer.time.tv_sec &&
              now.tv_usec >= p->nv_timer.time.tv_usec)) {

            DEBUG(DBG_HDMV, "navigation timer expired, jumping to object %d\n", p->nv_timer.mobj_id);

hpi1's avatar
hpi1 committed
624 625
            bd_psr_write(p->regs, PSR_NAV_TIMER, 0);

hpi1's avatar
hpi1 committed
626 627 628 629 630
            p->nv_timer.time.tv_sec = 0;
            _jump_object(p, p->nv_timer.mobj_id);

            return 0;
        }
hpi1's avatar
hpi1 committed
631

hpi1's avatar
hpi1 committed
632 633
        unsigned int timeout = p->nv_timer.time.tv_sec - now.tv_sec;
        if (p->nv_timer.time.tv_usec < now.tv_usec)
hpi1's avatar
hpi1 committed
634 635 636
            timeout ++;

        bd_psr_write(p->regs, PSR_NAV_TIMER, timeout);
hpi1's avatar
hpi1 committed
637 638 639 640
    }

    return -1;
}
641
*/
hpi1's avatar
hpi1 committed
642

hpi1's avatar
hpi1 committed
643 644 645 646
/*
 * trace
 */

hpi1's avatar
hpi1 committed
647
static void _hdmv_trace_cmd(int pc, MOBJ_CMD *cmd)
hpi1's avatar
hpi1 committed
648 649 650 651
{
    if (bd_get_debug_mask() & DBG_HDMV) {
        char buf[384], *dst = buf;

652
        dst += sprintf(dst, "%04d:  ", pc);
hpi1's avatar
hpi1 committed
653 654 655

        dst += mobj_sprint_cmd(dst, cmd);

hpi1's avatar
hpi1 committed
656 657 658 659 660 661 662 663
        DEBUG(DBG_HDMV, "%s\n", buf);
    }
}

static void _hdmv_trace_res(uint32_t new_src, uint32_t new_dst, uint32_t orig_src, uint32_t orig_dst)
{
    if (bd_get_debug_mask() & DBG_HDMV) {

hpi1's avatar
hpi1 committed
664
        if (new_dst != orig_dst || new_src != orig_src) {
hpi1's avatar
hpi1 committed
665 666 667
            char buf[384], *dst = buf;

            dst += sprintf(dst, "    :  [");
hpi1's avatar
hpi1 committed
668 669 670 671 672 673
            if (new_dst != orig_dst) {
                dst += sprintf(dst, " dst 0x%x <== 0x%x ", orig_dst, new_dst);
            }
            if (new_src != orig_src) {
                dst += sprintf(dst, " src 0x%x <== 0x%x ", orig_src, new_src);
            }
hpi1's avatar
hpi1 committed
674
            dst += sprintf(dst, "]");
hpi1's avatar
hpi1 committed
675

hpi1's avatar
hpi1 committed
676 677
            DEBUG(DBG_HDMV, "%s\n", buf);
        }
hpi1's avatar
hpi1 committed
678 679 680 681 682 683 684
    }
}

/*
 * interpreter
 */

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
/*
 * tools
 */

#define SWAP_u32(a, b) do { uint32_t tmp = a; a = b; b = tmp; } while(0)

static inline uint32_t RAND_u32(uint32_t range)
{
  return range > 0 ? rand() % range + 1 : 0;
}

static inline uint32_t ADD_u32(uint32_t a, uint32_t b)
{
  /* overflow -> saturate */
  uint64_t result = (uint64_t)a + b;
  return result < 0xffffffff ? result : 0xffffffff;
}

static inline uint32_t MUL_u32(uint32_t a, uint32_t b)
{
  /* overflow -> saturate */
  uint64_t result = (uint64_t)a * b;
  return result < 0xffffffff ? result : 0xffffffff;
}

hpi1's avatar
hpi1 committed
710 711 712 713 714 715 716 717 718 719
/*
 * _hdmv_step()
 *  - execute next instruction from current program
 */
static int _hdmv_step(HDMV_VM *p)
{
    MOBJ_CMD  *cmd  = &p->object->cmds[p->pc];
    HDMV_INSN *insn = &cmd->insn;
    uint32_t   src  = 0;
    uint32_t   dst  = 0;
720
    int        inc_pc = 1;
hpi1's avatar
hpi1 committed
721 722

    /* fetch operand values */
723
    _fetch_operands(p, cmd, &dst, &src);
hpi1's avatar
hpi1 committed
724

hpi1's avatar
hpi1 committed
725 726 727
    /* trace */
    _hdmv_trace_cmd(p->pc, cmd);

hpi1's avatar
hpi1 committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
    /* execute */
    switch (insn->grp) {
        case INSN_GROUP_BRANCH:
            switch (insn->sub_grp) {
                case BRANCH_GOTO:
                    if (insn->op_cnt > 1) {
                        DEBUG(DBG_HDMV|DBG_CRIT, "[too many operands in BRANCH/GOTO opcode 0x%08x] ", *(uint32_t*)insn);
                    }
                    switch (insn->branch_opt) {
                        case INSN_NOP:                      break;
                        case INSN_GOTO:  p->pc   = dst - 1; break;
                        case INSN_BREAK: p->pc   = 1 << 17; break;
                        default:
                            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown BRANCH/GOTO option in opcode 0x%08x] ", *(uint32_t*)insn);
                            break;
                    }
                    break;
                case BRANCH_JUMP:
                    if (insn->op_cnt > 1) {
                        DEBUG(DBG_HDMV|DBG_CRIT, "[too many operands in BRANCH/JUMP opcode 0x%08x] ", *(uint32_t*)insn);
                    }
                    switch (insn->branch_opt) {
750 751 752 753 754
                        case INSN_JUMP_TITLE:  _jump_title(p, dst); break;
                        case INSN_CALL_TITLE:  _call_title(p, dst); break;
                        case INSN_RESUME:      _resume_object(p);   break;
                        case INSN_JUMP_OBJECT: if (!_jump_object(p, dst)) { inc_pc = 0; } break;
                        case INSN_CALL_OBJECT: if (!_call_object(p, dst)) { inc_pc = 0; } break;
hpi1's avatar
hpi1 committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
                        default:
                            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown BRANCH/JUMP option in opcode 0x%08x] ", *(uint32_t*)insn);
                            break;
                    }
                    break;
                case BRANCH_PLAY:
                    switch (insn->branch_opt) {
                        case INSN_PLAY_PL:      _play_at(p, dst,  -1,  -1); break;
                        case INSN_PLAY_PL_PI:   _play_at(p, dst, src,  -1); break;
                        case INSN_PLAY_PL_PM:   _play_at(p, dst,  -1, src); break;
                        case INSN_TERMINATE_PL: _play_stop(p);              break;
                        case INSN_LINK_PI:      _play_at(p,  -1, dst,  -1); break;
                        case INSN_LINK_MK:      _play_at(p,  -1,  -1, dst); break;
                        default:
                            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown BRANCH/PLAY option in opcode 0x%08x] ", *(uint32_t*)insn);
                            break;
                    }
                    break;

                default:
                    DEBUG(DBG_HDMV|DBG_CRIT, "[unknown BRANCH subgroup in opcode 0x%08x] ", *(uint32_t*)insn);
                    break;
            }
            break; /* INSN_GROUP_BRANCH */

        case INSN_GROUP_CMP:
            if (insn->op_cnt < 2) {
                DEBUG(DBG_HDMV|DBG_CRIT, "missing operand in BRANCH/JUMP opcode 0x%08x] ", *(uint32_t*)insn);
            }
            switch (insn->cmp_opt) {
785
                case INSN_BC: p->pc += !(dst &  src); break;
hpi1's avatar
hpi1 committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799
                case INSN_EQ: p->pc += !(dst == src); break;
                case INSN_NE: p->pc += !(dst != src); break;
                case INSN_GE: p->pc += !(dst >= src); break;
                case INSN_GT: p->pc += !(dst >  src); break;
                case INSN_LE: p->pc += !(dst <= src); break;
                case INSN_LT: p->pc += !(dst <  src); break;
                default:
                    DEBUG(DBG_HDMV|DBG_CRIT, "[unknown COMPARE option in opcode 0x%08x] ", *(uint32_t*)insn);
                    break;
            }
            break; /* INSN_GROUP_CMP */

        case INSN_GROUP_SET:
            switch (insn->sub_grp) {
hpi1's avatar
hpi1 committed
800 801 802 803
                case SET_SET: {
                    uint32_t src0 = src;
                    uint32_t dst0 = dst;

hpi1's avatar
hpi1 committed
804 805 806 807 808
                    if (insn->op_cnt < 2) {
                        DEBUG(DBG_HDMV|DBG_CRIT, "missing operand in SET/SET opcode 0x%08x] ", *(uint32_t*)insn);
                    }
                    switch (insn->set_opt) {
                        case INSN_MOVE:   dst  = src;         break;
809 810
                        case INSN_SWAP:   SWAP_u32(src, dst);   break;
                        case INSN_SUB:    dst  = dst > src ? dst - src :          0; break;
hpi1's avatar
hpi1 committed
811 812
                        case INSN_DIV:    dst  = src > 0   ? dst / src : 0xffffffff; break;
                        case INSN_MOD:    dst  = src > 0   ? dst % src : 0xffffffff; break;
813 814 815
                        case INSN_ADD:    dst  = ADD_u32(src, dst);  break;
                        case INSN_MUL:    dst  = MUL_u32(dst, src);  break;
                        case INSN_RND:    dst  = RAND_u32(src);      break;
hpi1's avatar
hpi1 committed
816 817 818 819 820 821 822 823 824 825 826 827 828 829
                        case INSN_AND:    dst &= src;         break;
                        case INSN_OR:     dst |= src;         break;
                        case INSN_XOR:    dst ^= src;         break;
                        case INSN_BITSET: dst |=  (1 << src); break;
                        case INSN_BITCLR: dst &= ~(1 << src); break;
                        case INSN_SHL:    dst <<= src;        break;
                        case INSN_SHR:    dst >>= src;        break;
                        default:
                            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown SET option in opcode 0x%08x] ", *(uint32_t*)insn);
                            break;
                    }

                    /* store result(s) */
                    if (dst != dst0 || src != src0) {
hpi1's avatar
hpi1 committed
830 831 832

                        _hdmv_trace_res(src, dst, src0, dst0);

hpi1's avatar
hpi1 committed
833 834 835
                        _store_result(p, cmd, src, dst, src0, dst0);
                    }
                    break;
hpi1's avatar
hpi1 committed
836
                }
hpi1's avatar
hpi1 committed
837 838
                case SET_SETSYSTEM:
                    switch (insn->set_opt) {
hpi1's avatar
hpi1 committed
839 840 841 842 843 844 845 846 847
                        case INSN_SET_STREAM:      _set_stream     (p, dst, src); break;
                        case INSN_SET_SEC_STREAM:  _set_sec_stream (p, dst, src); break;
                        case INSN_SET_NV_TIMER:    _set_nv_timer   (p, dst, src); break;
                        case INSN_SET_BUTTON_PAGE: _set_button_page(p, dst, src); break;
                        case INSN_ENABLE_BUTTON:   _enable_button  (p, dst,   1); break;
                        case INSN_DISABLE_BUTTON:  _enable_button  (p, dst,   0); break;
                        case INSN_POPUP_OFF:       _popup_off      (p);           break;
                        case INSN_STILL_ON:        _set_still_mode (p,   1);      break;
                        case INSN_STILL_OFF:       _set_still_mode (p,   0);      break;
hpi1's avatar
hpi1 committed
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
                        default:
                            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown SETSYSTEM option in opcode 0x%08x] ", *(uint32_t*)insn);
                            break;
                    }
                    break;
                default:
                    DEBUG(DBG_HDMV|DBG_CRIT, "[unknown SET subgroup in opcode 0x%08x] ", *(uint32_t*)insn);
                    break;
            }
            break; /* INSN_GROUP_SET */

        default:
            DEBUG(DBG_HDMV|DBG_CRIT, "[unknown group in opcode 0x%08x] ", *(uint32_t*)insn);
            break;
    }

    /* inc program counter to next instruction */
865
    p->pc += inc_pc;
hpi1's avatar
hpi1 committed
866 867 868 869 870 871 872 873

    return 0;
}

/*
 * interface
 */

hpi1's avatar
hpi1 committed
874
int hdmv_vm_select_object(HDMV_VM *p, int object)
hpi1's avatar
hpi1 committed
875 876 877
{
    if (object >= 0) {
        if (object >= p->movie_objects->num_objects) {
hpi1's avatar
hpi1 committed
878
            DEBUG(DBG_HDMV|DBG_CRIT, "hdmv_vm_select_object(): invalid object reference (%d) !\n", object);
hpi1's avatar
hpi1 committed
879 880 881 882 883
            return -1;
        }
        p->pc     = 0;
        p->object = &p->movie_objects->objects[object];
    }
hpi1's avatar
hpi1 committed
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902

    return 0;
}

int hdmv_vm_set_object(HDMV_VM *p, int num_nav_cmds, void *nav_cmds)
{
    p->object = NULL;

    if (p->ig_object) {
        X_FREE(p->ig_object->cmds);
        X_FREE(p->ig_object);
    }

    if (nav_cmds && num_nav_cmds > 0) {
        MOBJ_OBJECT *ig_object = calloc(1, sizeof(MOBJ_OBJECT));
        ig_object->num_cmds = num_nav_cmds;
        ig_object->cmds     = calloc(num_nav_cmds, sizeof(MOBJ_CMD));
        memcpy(ig_object->cmds, nav_cmds, num_nav_cmds * sizeof(MOBJ_CMD));

hpi1's avatar
hpi1 committed
903 904 905 906 907 908 909 910 911 912 913 914 915
#if 0
        /* ??? */
        if (!p->ig_object && p->object) {
            _suspend_object(p);
        }
#endif
        p->pc        = 0;
        p->ig_object = ig_object;
        p->object    = ig_object;
    }

    return 0;
}
hpi1's avatar
hpi1 committed
916 917 918 919 920

int hdmv_vm_get_event(HDMV_VM *p, HDMV_EVENT *ev)
{
    return _get_event(p, ev);
}
hpi1's avatar
hpi1 committed
921 922 923 924 925 926 927 928 929 930 931 932 933

int hdmv_vm_running(HDMV_VM *p)
{
    return !!p->object;
}

int hdmv_vm_resume(HDMV_VM *p)
{
    return _resume_object(p);
}

int hdmv_vm_suspend(HDMV_VM *p)
{
hpi1's avatar
hpi1 committed
934 935 936
    if (p->object && !p->ig_object) {
        _suspend_object(p);
        return 0;
hpi1's avatar
hpi1 committed
937 938 939
    }
    return -1;
}
hpi1's avatar
hpi1 committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986

/* terminate program after MAX_LOOP instructions */
#define MAX_LOOP 1000000

int hdmv_vm_run(HDMV_VM *p, HDMV_EVENT *ev)
{
    int max_loop = MAX_LOOP;

    /* pending events ? */
    if (!_get_event(p, ev)) {
        return 0;
    }

    /* valid program ? */
    if (!p->object) {
        DEBUG(DBG_HDMV|DBG_CRIT, "hdmv_vm_run(): no object selected\n");
        return -1;
    }

    while (--max_loop > 0) {

        /* terminated ? */
        if (p->pc >= p->object->num_cmds) {
            DEBUG(DBG_HDMV, "terminated with PC=%d\n", p->pc);
            p->object = NULL;
            ev->event = HDMV_EVENT_END;
            return 0;
        }

        /* next instruction */
        if (_hdmv_step(p) < 0) {
            p->object = NULL;
            return -1;
        }

        /* events ? */
        if (!_get_event(p, ev)) {
            return 0;
        }
    }

    DEBUG(DBG_HDMV|DBG_CRIT, "hdmv_vm: infinite program ? terminated after %d instructions.\n", MAX_LOOP);
    p->object = NULL;
    return -1;
}