summaryrefslogtreecommitdiff
path: root/apps/plugins/xworld/vm.c
blob: 10c3957ad7b8b87f57ea2572253ae045238aa40b (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
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
785
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 Franklin Wei, Benjamin Brown
 * Copyright (C) 2004 Gregory Montoir
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include "plugin.h"
#include "vm.h"
#include "mixer.h"
#include "resource.h"
#include "video.h"
#include "serializer.h"
#include "sfxplayer.h"
#include "sys.h"
#include "parts.h"
#include "file.h"

static const uint16_t vm_frequenceTable[] = {
    0x0CFF, 0x0DC3, 0x0E91, 0x0F6F, 0x1056, 0x114E, 0x1259, 0x136C,
    0x149F, 0x15D9, 0x1726, 0x1888, 0x19FD, 0x1B86, 0x1D21, 0x1EDE,
    0x20AB, 0x229C, 0x24B3, 0x26D7, 0x293F, 0x2BB2, 0x2E4C, 0x3110,
    0x33FB, 0x370D, 0x3A43, 0x3DDF, 0x4157, 0x4538, 0x4998, 0x4DAE,
    0x5240, 0x5764, 0x5C9A, 0x61C8, 0x6793, 0x6E19, 0x7485, 0x7BBD
};

void vm_create(struct VirtualMachine* m, struct Mixer *mix,  struct Resource* res, struct SfxPlayer *ply, struct Video *vid, struct System *stub)
{
    m->res = res;
    m->video = vid;
    m->sys = stub;
    m->mixer = mix;
    m->player = ply;
}

void vm_init(struct VirtualMachine* m) {

    rb->memset(m->vmVariables, 0, sizeof(m->vmVariables));
    m->vmVariables[0x54] = 0x81;
    m->vmVariables[VM_VARIABLE_RANDOM_SEED] = *rb->current_tick % 0x10000;

    /* rawgl has these, but they don't seem to do anything */
    //m->vmVariables[0xBC] = 0x10;
    //m->vmVariables[0xC6] = 0x80;
    //m->vmVariables[0xF2] = 4000;
    //m->vmVariables[0xDC] = 33;

    m->_fastMode = false;
    m->player->_markVar = &m->vmVariables[VM_VARIABLE_MUS_MARK];
}

void vm_op_movConst(struct VirtualMachine* m) {
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    int16_t value = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_movConst(0x%02X, %d)", variableId, value);
    m->vmVariables[variableId] = value;
}

void vm_op_mov(struct VirtualMachine* m) {
    uint8_t dstVariableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t srcVariableId = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_mov(0x%02X, 0x%02X)", dstVariableId, srcVariableId);
    m->vmVariables[dstVariableId] = m->vmVariables[srcVariableId];
}

void vm_op_add(struct VirtualMachine* m) {
    uint8_t dstVariableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t srcVariableId = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_add(0x%02X, 0x%02X)", dstVariableId, srcVariableId);
    m->vmVariables[dstVariableId] += m->vmVariables[srcVariableId];
}

void vm_op_addConst(struct VirtualMachine* m) {
    if (m->res->currentPartId == 0x3E86 && m->_scriptPtr.pc == m->res->segBytecode + 0x6D48) {
        //warning("vm_op_addConst() hack for non-stop looping gun sound bug");
        // the script 0x27 slot 0x17 doesn't stop the gun sound from looping, I
        // don't really know why ; for now, let's play the 'stopping sound' like
        // the other scripts do
        //  (0x6D43) jmp(0x6CE5)
        //  (0x6D46) break
        //  (0x6D47) VAR(6) += -50
        vm_snd_playSound(m, 0x5B, 1, 64, 1);
    }
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    int16_t value = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_addConst(0x%02X, %d)", variableId, value);
    m->vmVariables[variableId] += value;
}

void vm_op_call(struct VirtualMachine* m) {

    uint16_t offset = scriptPtr_fetchWord(&m->_scriptPtr);
    uint8_t sp = m->_stackPtr;

    debug(DBG_VM, "vm_op_call(0x%X)", offset);
    m->_scriptStackCalls[sp] = m->_scriptPtr.pc - m->res->segBytecode;
    if (m->_stackPtr == 0xFF) {
        error("vm_op_call() ec=0x%X stack overflow", 0x8F);
    }
    ++m->_stackPtr;
    m->_scriptPtr.pc = m->res->segBytecode + offset ;
}

void vm_op_ret(struct VirtualMachine* m) {
    debug(DBG_VM, "vm_op_ret()");
    if (m->_stackPtr == 0) {
        error("vm_op_ret() ec=0x%X stack underflow", 0x8F);
    }
    --m->_stackPtr;
    uint8_t sp = m->_stackPtr;
    m->_scriptPtr.pc = m->res->segBytecode + m->_scriptStackCalls[sp];
}

void vm_op_pauseThread(struct VirtualMachine* m) {
    debug(DBG_VM, "vm_op_pauseThread()");
    m->gotoNextThread = true;
}

void vm_op_jmp(struct VirtualMachine* m) {
    uint16_t pcOffset = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_jmp(0x%02X)", pcOffset);
    m->_scriptPtr.pc = m->res->segBytecode + pcOffset;
}

void vm_op_setSetVect(struct VirtualMachine* m) {
    uint8_t threadId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t pcOffsetRequested = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_setSetVect(0x%X, 0x%X)", threadId, pcOffsetRequested);
    m->threadsData[REQUESTED_PC_OFFSET][threadId] = pcOffsetRequested;
}

void vm_op_jnz(struct VirtualMachine* m) {
    uint8_t i = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_jnz(0x%02X)", i);
    --m->vmVariables[i];
    if (m->vmVariables[i] != 0) {
        vm_op_jmp(m);
    } else {
        scriptPtr_fetchWord(&m->_scriptPtr);
    }
}

#define BYPASS_PROTECTION
void vm_op_condJmp(struct VirtualMachine* m) {

    //debug(DBG_VM, "Jump : %X \n",m->_scriptPtr.pc-m->res->segBytecode);
//FCS Whoever wrote this is patching the bytecode on the fly. This is ballzy !!
#if 0
    if (m->res->currentPartId == GAME_PART_FIRST && m->_scriptPtr.pc == m->res->segBytecode + 0xCB9) {

        // (0x0CB8) condJmp(0x80, VAR(41), VAR(30), 0xCD3)
        *(m->_scriptPtr.pc + 0x00) = 0x81;
        *(m->_scriptPtr.pc + 0x03) = 0x0D;
        *(m->_scriptPtr.pc + 0x04) = 0x24;
        // (0x0D4E) condJmp(0x4, VAR(50), 6, 0xDBC)
        *(m->_scriptPtr.pc + 0x99) = 0x0D;
        *(m->_scriptPtr.pc + 0x9A) = 0x5A;
        debug(DBG_VM, "vm_op_condJmp() bypassing protection");
        debug(DBG_VM, "bytecode has been patched");

        //warning("bypassing protection");

        //vm_bypassProtection(m);
    }


#endif

    uint8_t opcode = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t var = scriptPtr_fetchByte(&m->_scriptPtr);
    int16_t b = m->vmVariables[var];
    uint8_t c = scriptPtr_fetchByte(&m->_scriptPtr);
    int16_t a;

    if (opcode & 0x80) {
        a = m->vmVariables[c];
    } else if (opcode & 0x40) {
        a = c * 256 + scriptPtr_fetchByte(&m->_scriptPtr);
    } else {
        a = c;
    }
    debug(DBG_VM, "vm_op_condJmp(%d, 0x%02X, 0x%02X)", opcode, b, a);

    // Check if the conditional value is met.
    bool expr = false;
    switch (opcode & 7) {
    case 0:     // jz
        expr = (b == a);

#ifdef BYPASS_PROTECTION
        /* always succeed in code wheel verification */
        if (m->res->currentPartId == GAME_PART_FIRST && var == 0x29 && (opcode & 0x80) != 0) {

            m->vmVariables[0x29] = m->vmVariables[0x1E];
            m->vmVariables[0x2A] = m->vmVariables[0x1F];
            m->vmVariables[0x2B] = m->vmVariables[0x20];
            m->vmVariables[0x2C] = m->vmVariables[0x21];
            // counters
            m->vmVariables[0x32] = 6;
            m->vmVariables[0x64] = 20;
            expr = true;
            //warning("Script::op_condJmp() bypassing protection");
        }
#endif
        break;
    case 1: // jnz
        expr = (b != a);
        break;
    case 2: // jg
        expr = (b > a);
        break;
    case 3: // jge
        expr = (b >= a);
        break;
    case 4: // jl
        expr = (b < a);
        break;
    case 5: // jle
        expr = (b <= a);
        break;
    default:
        warning("vm_op_condJmp() invalid condition %d", (opcode & 7));
        break;
    }

    if (expr) {
        vm_op_jmp(m);
    } else {
        scriptPtr_fetchWord(&m->_scriptPtr);
    }

}

void vm_op_setPalette(struct VirtualMachine* m) {
    uint16_t paletteId = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_changePalette(%d)", paletteId);
    m->video->paletteIdRequested = paletteId >> 8;
}

void vm_op_resetThread(struct VirtualMachine* m) {

    uint8_t threadId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t i =        scriptPtr_fetchByte(&m->_scriptPtr);

    // FCS: WTF, this is cryptic as hell !!
    // int8_t n = (i & 0x3F) - threadId;  //0x3F = 0011 1111
    // The following is so much clearer

    //Make sure i is within [0-VM_NUM_THREADS-1]
    i = i & (VM_NUM_THREADS - 1) ;
    int8_t n = i - threadId;

    if (n < 0) {
        warning("vm_op_m->resetThread() ec=0x%X (n < 0)", 0x880);
        return;
    }
    ++n;
    uint8_t a = scriptPtr_fetchByte(&m->_scriptPtr);

    debug(DBG_VM, "vm_op_m->resetThread(%d, %d, %d)", threadId, i, a);

    if (a == 2) {
        uint16_t *p = &m->threadsData[REQUESTED_PC_OFFSET][threadId];
        while (n--) {
            *p++ = 0xFFFE;
        }
    } else if (a < 2) {
        uint8_t *p = &m->vmIsChannelActive[REQUESTED_STATE][threadId];
        while (n--) {
            *p++ = a;
        }
    }
}

void vm_op_selectVideoPage(struct VirtualMachine* m) {
    uint8_t frameBufferId = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_selectVideoPage(%d)", frameBufferId);
    video_changePagePtr1(m->video, frameBufferId);
}

void vm_op_fillVideoPage(struct VirtualMachine* m) {
    uint8_t pageId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t color = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_fillVideoPage(%d, %d)", pageId, color);
    video_fillPage(m->video, pageId, color);
}

void vm_op_copyVideoPage(struct VirtualMachine* m) {
    uint8_t srcPageId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t dstPageId = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_copyVideoPage(%d, %d)", srcPageId, dstPageId);
    video_copyPage(m->video, srcPageId, dstPageId, m->vmVariables[VM_VARIABLE_SCROLL_Y]);
}


static uint32_t lastTimeStamp = 0;
void vm_op_blitFramebuffer(struct VirtualMachine* m) {

    uint8_t pageId = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_blitFramebuffer(%d)", pageId);
    vm_inp_handleSpecialKeys(m);

    /* Nasty hack....was this present in the original assembly  ??!! */
    if (m->res->currentPartId == GAME_PART_FIRST && m->vmVariables[0x67] == 1)
        m->vmVariables[0xDC] = 0x21;

    if (!m->_fastMode) {

        int32_t delay = sys_getTimeStamp(m->sys) - lastTimeStamp;
        int32_t timeToSleep = m->vmVariables[VM_VARIABLE_PAUSE_SLICES] * 20 - delay;

        /* The bytecode will set m->vmVariables[VM_VARIABLE_PAUSE_SLICES] from 1 to 5 */
        /* The virtual machine hence indicates how long the image should be displayed. */

        if (timeToSleep > 0)
        {
            sys_sleep(m->sys, timeToSleep);
        }

        lastTimeStamp = sys_getTimeStamp(m->sys);
    }

    /* WTF ? */
    m->vmVariables[0xF7] = 0;

    video_updateDisplay(m->video, pageId);
}

void vm_op_killThread(struct VirtualMachine* m) {
    debug(DBG_VM, "vm_op_killThread()");
    m->_scriptPtr.pc = m->res->segBytecode + 0xFFFF;
    m->gotoNextThread = true;
}

void vm_op_drawString(struct VirtualMachine* m) {
    uint16_t stringId = scriptPtr_fetchWord(&m->_scriptPtr);
    uint16_t x = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t y = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t color = scriptPtr_fetchByte(&m->_scriptPtr);

    debug(DBG_VM, "vm_op_drawString(0x%03X, %d, %d, %d)", stringId, x, y, color);

    video_drawString(m->video, color, x, y, stringId);
}

void vm_op_sub(struct VirtualMachine* m) {
    uint8_t i = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t j = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_sub(0x%02X, 0x%02X)", i, j);
    m->vmVariables[i] -= m->vmVariables[j];
}

void vm_op_and(struct VirtualMachine* m) {
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t n = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_and(0x%02X, %d)", variableId, n);
    m->vmVariables[variableId] = (uint16_t)m->vmVariables[variableId] & n;
}

void vm_op_or(struct VirtualMachine* m) {
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t value = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_or(0x%02X, %d)", variableId, value);
    m->vmVariables[variableId] = (uint16_t)m->vmVariables[variableId] | value;
}

void vm_op_shl(struct VirtualMachine* m) {
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t leftShiftValue = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_shl(0x%02X, %d)", variableId, leftShiftValue);
    m->vmVariables[variableId] = (uint16_t)m->vmVariables[variableId] << leftShiftValue;
}

void vm_op_shr(struct VirtualMachine* m) {
    uint8_t variableId = scriptPtr_fetchByte(&m->_scriptPtr);
    uint16_t rightShiftValue = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_shr(0x%02X, %d)", variableId, rightShiftValue);
    m->vmVariables[variableId] = (uint16_t)m->vmVariables[variableId] >> rightShiftValue;
}

void vm_op_playSound(struct VirtualMachine* m) {
    uint16_t resourceId = scriptPtr_fetchWord(&m->_scriptPtr);
    uint8_t freq = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t vol = scriptPtr_fetchByte(&m->_scriptPtr);
    uint8_t channel = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_playSound(0x%X, %d, %d, %d)", resourceId, freq, vol, channel);
    vm_snd_playSound(m, resourceId, freq, vol, channel);
}

void vm_op_updateMemList(struct VirtualMachine* m) {

    uint16_t resourceId = scriptPtr_fetchWord(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_updateMemList(%d)", resourceId);

    if (resourceId == 0) {
        player_stop(m->player);
        mixer_stopAll(m->mixer);
        res_invalidateRes(m->res);
    } else {
        res_loadPartsOrMemoryEntry(m->res, resourceId);
    }
}

void vm_op_playMusic(struct VirtualMachine* m) {
    uint16_t resNum = scriptPtr_fetchWord(&m->_scriptPtr);
    uint16_t delay = scriptPtr_fetchWord(&m->_scriptPtr);
    uint8_t pos = scriptPtr_fetchByte(&m->_scriptPtr);
    debug(DBG_VM, "vm_op_playMusic(0x%X, %d, %d)", resNum, delay, pos);
    vm_snd_playMusic(m, resNum, delay, pos);
}

void vm_initForPart(struct VirtualMachine* m, uint16_t partId) {

    player_stop(m->player);
    mixer_stopAll(m->mixer);

    /* WTF is that ? */
    m->vmVariables[0xE4] = 0x14;

    res_setupPart(m->res, partId);

    /* Set all thread to inactive (pc at 0xFFFF or 0xFFFE ) */
    rb->memset((uint8_t *)m->threadsData, 0xFF, sizeof(m->threadsData));

    rb->memset((uint8_t *)m->vmIsChannelActive, 0, sizeof(m->vmIsChannelActive));

    int firstThreadId = 0;
    m->threadsData[PC_OFFSET][firstThreadId] = 0;
}

/*
  This is called every frames in the infinite loop.
*/
void vm_checkThreadRequests(struct VirtualMachine* m) {

    /* Check if a part switch has been requested. */
    if (m->res->requestedNextPart != 0) {
        vm_initForPart(m, m->res->requestedNextPart);
        m->res->requestedNextPart = 0;
    }


    /* Check if a state update has been requested for any thread during the previous VM execution: */
    /*      - Pause */
    /*      - Jump */

    /* JUMP: */
    /* Note: If a jump has been requested, the jump destination is stored */
    /* in m->threadsData[REQUESTED_PC_OFFSET]. Otherwise m->threadsData[REQUESTED_PC_OFFSET] == 0xFFFF */

    /* PAUSE: */
    /* Note: If a pause has been requested it is stored in  m->vmIsChannelActive[REQUESTED_STATE][i] */

    for (int threadId = 0; threadId < VM_NUM_THREADS; threadId++) {

        m->vmIsChannelActive[CURR_STATE][threadId] = m->vmIsChannelActive[REQUESTED_STATE][threadId];

        uint16_t n = m->threadsData[REQUESTED_PC_OFFSET][threadId];

        if (n != VM_NO_SETVEC_REQUESTED) {

            m->threadsData[PC_OFFSET][threadId] = (n == 0xFFFE) ? VM_INACTIVE_THREAD : n;
            m->threadsData[REQUESTED_PC_OFFSET][threadId] = VM_NO_SETVEC_REQUESTED;
        }
    }
}

void vm_hostFrame(struct VirtualMachine* m) {

    /* Run the Virtual Machine for every active threads (one vm frame). */
    /* Inactive threads are marked with a thread instruction pointer set to 0xFFFF (VM_INACTIVE_THREAD). */
    /* A thread must feature a break opcode so the interpreter can move to the next thread. */

    for (int threadId = 0; threadId < VM_NUM_THREADS; threadId++) {

        if (m->vmIsChannelActive[CURR_STATE][threadId])
            continue;

        uint16_t n = m->threadsData[PC_OFFSET][threadId];

        if (n != VM_INACTIVE_THREAD) {

            /* Set the script pointer to the right location. */
            /* script pc is used in executeThread in order */
            /* to get the next opcode. */
            m->_scriptPtr.pc = m->res->segBytecode + n;
            m->_stackPtr = 0;

            m->gotoNextThread = false;
            debug(DBG_VM, "vm_hostFrame() i=0x%02X n=0x%02X *p=0x%02X", threadId, n, *m->_scriptPtr.pc);
            vm_executeThread(m);

            /* Since .pc is going to be modified by this next loop iteration, we need to save it. */
            m->threadsData[PC_OFFSET][threadId] = m->_scriptPtr.pc - m->res->segBytecode;


            debug(DBG_VM, "vm_hostFrame() i=0x%02X pos=0x%X", threadId, m->threadsData[PC_OFFSET][threadId]);
            if (m->sys->input.quit) {
                break;
            }
        }
    }
}

#define COLOR_BLACK 0xFF
#define DEFAULT_ZOOM 0x40


void vm_executeThread(struct VirtualMachine* m) {

    while (!m->gotoNextThread) {
        uint8_t opcode = scriptPtr_fetchByte(&m->_scriptPtr);

        /* 1000 0000 is set */
        if (opcode & 0x80)
        {
            uint16_t off = ((opcode << 8) | scriptPtr_fetchByte(&m->_scriptPtr)) * 2;
            m->res->_useSegVideo2 = false;
            int16_t x = scriptPtr_fetchByte(&m->_scriptPtr);
            int16_t y = scriptPtr_fetchByte(&m->_scriptPtr);
            int16_t h = y - 199;
            if (h > 0) {
                y = 199;
                x += h;
            }
            debug(DBG_VIDEO, "vid_opcd_0x80 : opcode=0x%X off=0x%X x=%d y=%d", opcode, off, x, y);

            /* This switch the polygon database to "cinematic" and probably draws a black polygon */
            /* over all the screen. */
            video_setDataBuffer(m->video, m->res->segCinematic, off);
            struct Point temp;
            temp.x = x;
            temp.y = y;
            video_readAndDrawPolygon(m->video, COLOR_BLACK, DEFAULT_ZOOM, &temp);

            continue;
        }

        /* 0100 0000 is set */
        if (opcode & 0x40)
        {
            int16_t x, y;
            uint16_t off = scriptPtr_fetchWord(&m->_scriptPtr) * 2;
            x = scriptPtr_fetchByte(&m->_scriptPtr);

            m->res->_useSegVideo2 = false;

            if (!(opcode & 0x20))
            {
                if (!(opcode & 0x10))  /* 0001 0000 is set */
                {
                    x = (x << 8) | scriptPtr_fetchByte(&m->_scriptPtr);
                } else {
                    x = m->vmVariables[x];
                }
            }
            else
            {
                if (opcode & 0x10) { /* 0001 0000 is set */
                    x += 0x100;
                }
            }

            y = scriptPtr_fetchByte(&m->_scriptPtr);

            if (!(opcode & 8))  /* 0000 1000 is set */
            {
                if (!(opcode & 4)) { /* 0000 0100 is set */
                    y = (y << 8) | scriptPtr_fetchByte(&m->_scriptPtr);
                } else {
                    y = m->vmVariables[y];
                }
            }

            uint16_t zoom = scriptPtr_fetchByte(&m->_scriptPtr);

            if (!(opcode & 2))  /* 0000 0010 is set */
            {
                if (!(opcode & 1)) /* 0000 0001 is set */
                {
                    --m->_scriptPtr.pc;
                    zoom = 0x40;
                }
                else
                {
                    zoom = m->vmVariables[zoom];
                }
            }
            else
            {

                if (opcode & 1) { /* 0000 0001 is set */
                    m->res->_useSegVideo2 = true;
                    --m->_scriptPtr.pc;
                    zoom = 0x40;
                }
            }
            debug(DBG_VIDEO, "vid_opcd_0x40 : off=0x%X x=%d y=%d", off, x, y);
            video_setDataBuffer(m->video, m->res->_useSegVideo2 ? m->res->_segVideo2 : m->res->segCinematic, off);
            struct Point temp;
            temp.x = x;
            temp.y = y;
            video_readAndDrawPolygon(m->video, 0xFF, zoom, &temp);

            continue;
        }


        if (opcode > 0x1A)
        {
            error("vm_executeThread() ec=0x%X invalid opcode=0x%X", 0xFFF, opcode);
        }
        else
        {
            (vm_opcodeTable[opcode])(m);
        }
    }
}

void vm_inp_updatePlayer(struct VirtualMachine* m) {

    sys_processEvents(m->sys);

    if (m->res->currentPartId == 0x3E89) {
        char c = m->sys->input.lastChar;
        if (c == 8 || /*c == 0xD |*/ c == 0 || (c >= 'a' && c <= 'z')) {
            m->vmVariables[VM_VARIABLE_LAST_KEYCHAR] = c & ~0x20;
            m->sys->input.lastChar = 0;
        }
    }

    int16_t lr   = 0;
    int16_t mask = 0;
    int16_t ud   = 0;

    if (m->sys->input.dirMask & DIR_RIGHT) {
        lr = 1;
        mask |= 1;
    }
    if (m->sys->input.dirMask & DIR_LEFT) {
        lr = -1;
        mask |= 2;
    }
    if (m->sys->input.dirMask & DIR_DOWN) {
        ud = 1;
        mask |= 4;
    }

    m->vmVariables[VM_VARIABLE_HERO_POS_UP_DOWN] = ud;

    if (m->sys->input.dirMask & DIR_UP) {
        m->vmVariables[VM_VARIABLE_HERO_POS_UP_DOWN] = -1;
    }

    if (m->sys->input.dirMask & DIR_UP) { /* inpJump */
        ud = -1;
        mask |= 8;
    }

    m->vmVariables[VM_VARIABLE_HERO_POS_JUMP_DOWN] = ud;
    m->vmVariables[VM_VARIABLE_HERO_POS_LEFT_RIGHT] = lr;
    m->vmVariables[VM_VARIABLE_HERO_POS_MASK] = mask;
    int16_t button = 0;

    if (m->sys->input.button) {
        button = 1;
        mask |= 0x80;
    }

    m->vmVariables[VM_VARIABLE_HERO_ACTION] = button;
    m->vmVariables[VM_VARIABLE_HERO_ACTION_POS_MASK] = mask;
}

void vm_inp_handleSpecialKeys(struct VirtualMachine* m) {

    if (m->sys->input.pause) {

        if (m->res->currentPartId != GAME_PART1 && m->res->currentPartId != GAME_PART2) {
            m->sys->input.pause = false;
            while (!m->sys->input.pause) {
                sys_processEvents(m->sys);
                sys_sleep(m->sys, 200);
            }
        }
        m->sys->input.pause = false;
    }

    if (m->sys->input.code) {
        m->sys->input.code = false;
        if (m->res->currentPartId != GAME_PART_LAST && m->res->currentPartId != GAME_PART_FIRST) {
            m->res->requestedNextPart = GAME_PART_LAST;
        }
    }

    /* User has inputted a bad code, the "ERROR" screen is showing */
    if (m->vmVariables[0xC9] == 1) {
        debug(DBG_VM, "vm_inp_handleSpecialKeys() unhandled case (m->vmVariables[0xC9] == 1)");
    }

}

void vm_snd_playSound(struct VirtualMachine* m, uint16_t resNum, uint8_t freq, uint8_t vol, uint8_t channel) {

    debug(DBG_SND, "snd_playSound(0x%X, %d, %d, %d)", resNum, freq, vol, channel);

    struct MemEntry *me = &m->res->_memList[resNum];

    if (me->state != MEMENTRY_STATE_LOADED)
        return;


    if (vol == 0) {
        mixer_stopChannel(m->mixer, channel);
    } else {
        struct MixerChunk mc;
        rb->memset(&mc, 0, sizeof(mc));
        mc.data = me->bufPtr + 8; /* skip header */
        mc.len = READ_BE_UINT16(me->bufPtr) * 2;
        mc.loopLen = READ_BE_UINT16(me->bufPtr + 2) * 2;
        if (mc.loopLen != 0) {
            mc.loopPos = mc.len;
        }
        assert(freq < 40);
        mixer_playChannel(m->mixer, channel & 3, &mc, vm_frequenceTable[freq], MIN(vol, 0x3F));
    }

}

void vm_snd_playMusic(struct VirtualMachine* m, uint16_t resNum, uint16_t delay, uint8_t pos) {

    debug(DBG_SND, "snd_playMusic(0x%X, %d, %d)", resNum, delay, pos);

    if (resNum != 0) {
        player_loadSfxModule(m->player, resNum, delay, pos);
        player_start(m->player);
    } else if (delay != 0) {
        player_setEventsDelay(m->player, delay);
    } else {
        player_stop(m->player);
    }
}

void vm_saveOrLoad(struct VirtualMachine* m, struct Serializer *ser) {
    struct Entry entries[] = {
        SE_ARRAY(m->vmVariables, 0x100, SES_INT16, VER(1)),
        SE_ARRAY(m->_scriptStackCalls, 0x100, SES_INT16, VER(1)),
        SE_ARRAY(m->threadsData, 0x40 * 2, SES_INT16, VER(1)),
        SE_ARRAY(m->vmIsChannelActive, 0x40 * 2, SES_INT8, VER(1)),
        SE_END()
    };
    ser_saveOrLoadEntries(ser, entries);
}

void vm_bypassProtection(struct VirtualMachine* m)
{
    File f;
    file_create(&f, true);
    if (!file_open(&f, "bank0e", res_getDataDir(m->res), "rb")) {
        warning("Unable to bypass protection: add bank0e file to datadir");
    } else {
        struct Serializer s;
        ser_create(&s, &f, SM_LOAD, m->res->_memPtrStart, 2);
        vm_saveOrLoad(m, &s);
        res_saveOrLoad(m->res, &s);
        video_saveOrLoad(m->video, &s);
        player_saveOrLoad(m->player, &s);
        mixer_saveOrLoad(m->mixer, &s);
    }
    file_close(&f);
}