aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/arm/skyeye_common/vfp/vfp.cpp
blob: 5f3dd4b478a1fad43e76570c300ca7cc0e60cedd (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
/*
    armvfp.c - ARM VFPv3 emulation unit
    Copyright (C) 2003 Skyeye Develop Group
    for help please send mail to <skyeye-developer@lists.gro.clinux.org>

    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 program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Note: this file handles interface with arm core and vfp registers */

#include "common/common.h"

#include "core/arm/skyeye_common/armdefs.h"
#include "core/arm/skyeye_common/vfp/vfp.h"

//ARMul_State* persistent_state; /* function calls from SoftFloat lib don't have an access to ARMul_state. */

unsigned VFPInit(ARMul_State* state)
{
    state->VFP[VFP_OFFSET(VFP_FPSID)] = VFP_FPSID_IMPLMEN<<24 | VFP_FPSID_SW<<23 | VFP_FPSID_SUBARCH<<16 |
                                        VFP_FPSID_PARTNUM<<8 | VFP_FPSID_VARIANT<<4 | VFP_FPSID_REVISION;
    state->VFP[VFP_OFFSET(VFP_FPEXC)] = 0;
    state->VFP[VFP_OFFSET(VFP_FPSCR)] = 0;

    //persistent_state = state;
    /* Reset only specify VFP_FPEXC_EN = '0' */

    return 0;
}

unsigned VFPMRC(ARMul_State* state, unsigned type, u32 instr, u32* value)
{
    /* MRC<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int OPC_1 = BITS(instr, 21, 23);
    int Rt = BITS(instr, 12, 15);
    int CRn = BITS(instr, 16, 19);
    int CRm = BITS(instr, 0, 3);
    int OPC_2 = BITS(instr, 5, 7);

    /* TODO check access permission */

    /* CRn/opc1 CRm/opc2 */

    if (CoProc == 10 || CoProc == 11)
    {
        if (OPC_1 == 0x0 && CRm == 0 && (OPC_2 & 0x3) == 0)
        {
            /* VMOV r to s */
            /* Transfering Rt is not mandatory, as the value of interest is pointed by value */
            VMOVBRS(state, BIT(instr, 20), Rt, BIT(instr, 7)|CRn<<1, value);
            return ARMul_DONE;
        }

        if (OPC_1 == 0x7 && CRm == 0 && OPC_2 == 0)
        {
            VMRS(state, CRn, Rt, value);
            return ARMul_DONE;
        }
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
          instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);

    return ARMul_CANT;
}

unsigned VFPMCR(ARMul_State* state, unsigned type, u32 instr, u32 value)
{
    /* MCR<c> <coproc>,<opc1>,<Rt>,<CRn>,<CRm>{,<opc2>} */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int OPC_1 = BITS(instr, 21, 23);
    int Rt = BITS(instr, 12, 15);
    int CRn = BITS(instr, 16, 19);
    int CRm = BITS(instr, 0, 3);
    int OPC_2 = BITS(instr, 5, 7);

    /* TODO check access permission */

    /* CRn/opc1 CRm/opc2 */
    if (CoProc == 10 || CoProc == 11)
    {
        if (OPC_1 == 0x0 && CRm == 0 && (OPC_2 & 0x3) == 0)
        {
            /* VMOV s to r */
            /* Transfering Rt is not mandatory, as the value of interest is pointed by value */
            VMOVBRS(state, BIT(instr, 20), Rt, BIT(instr, 7)|CRn<<1, &value);
            return ARMul_DONE;
        }

        if (OPC_1 == 0x7 && CRm == 0 && OPC_2 == 0)
        {
            VMSR(state, CRn, Rt);
            return ARMul_DONE;
        }

        if ((OPC_1 & 0x4) == 0 && CoProc == 11 && CRm == 0)
        {
            VFP_DEBUG_UNIMPLEMENTED(VMOVBRC);
            return ARMul_DONE;
        }

        if (CoProc == 11 && CRm == 0)
        {
            VFP_DEBUG_UNIMPLEMENTED(VMOVBCR);
            return ARMul_DONE;
        }
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, CRn %x, CRm %x, OPC_2 %x\n",
          instr, CoProc, OPC_1, Rt, CRn, CRm, OPC_2);

    return ARMul_CANT;
}

unsigned VFPMRRC(ARMul_State* state, unsigned type, u32 instr, u32* value1, u32* value2)
{
    /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int OPC_1 = BITS(instr, 4, 7);
    int Rt = BITS(instr, 12, 15);
    int Rt2 = BITS(instr, 16, 19);
    int CRm = BITS(instr, 0, 3);

    if (CoProc == 10 || CoProc == 11)
    {
        if (CoProc == 10 && (OPC_1 & 0xD) == 1)
        {
            VMOVBRRSS(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, value1, value2);
            return ARMul_DONE;
        }

        if (CoProc == 11 && (OPC_1 & 0xD) == 1)
        {
            /* Transfering Rt and Rt2 is not mandatory, as the value of interest is pointed by value1 and value2 */
            VMOVBRRD(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, value1, value2);
            return ARMul_DONE;
        }
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
          instr, CoProc, OPC_1, Rt, Rt2, CRm);

    return ARMul_CANT;
}

unsigned VFPMCRR(ARMul_State* state, unsigned type, u32 instr, u32 value1, u32 value2)
{
    /* MCRR<c> <coproc>,<opc1>,<Rt>,<Rt2>,<CRm> */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int OPC_1 = BITS(instr, 4, 7);
    int Rt = BITS(instr, 12, 15);
    int Rt2 = BITS(instr, 16, 19);
    int CRm = BITS(instr, 0, 3);

    /* TODO check access permission */

    /* CRn/opc1 CRm/opc2 */

    if (CoProc == 11 || CoProc == 10)
    {
        if (CoProc == 10 && (OPC_1 & 0xD) == 1)
        {
            VMOVBRRSS(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, &value1, &value2);
            return ARMul_DONE;
        }

        if (CoProc == 11 && (OPC_1 & 0xD) == 1)
        {
            /* Transfering Rt and Rt2 is not mandatory, as the value of interest is pointed by value1 and value2 */
            VMOVBRRD(state, BIT(instr, 20), Rt, Rt2, BIT(instr, 5)<<4|CRm, &value1, &value2);
            return ARMul_DONE;
        }
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, OPC_1 %x, Rt %x, Rt2 %x, CRm %x\n",
          instr, CoProc, OPC_1, Rt, Rt2, CRm);

    return ARMul_CANT;
}

unsigned VFPSTC(ARMul_State* state, unsigned type, u32 instr, u32 * value)
{
    /* STC{L}<c> <coproc>,<CRd>,[<Rn>],<option> */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int CRd = BITS(instr, 12, 15);
    int Rn = BITS(instr, 16, 19);
    int imm8 = BITS(instr, 0, 7);
    int P = BIT(instr, 24);
    int U = BIT(instr, 23);
    int D = BIT(instr, 22);
    int W = BIT(instr, 21);

    /* TODO check access permission */

    /* VSTM */
    if ( (P|U|D|W) == 0 ) {
        LOG_ERROR(Core_ARM11, "In %s, UNDEFINED\n", __FUNCTION__);
        exit(-1);
    }
    if (CoProc == 10 || CoProc == 11) {
#if 1
        if (P == 0 && U == 0 && W == 0) {
            LOG_ERROR(Core_ARM11, "VSTM Related encodings\n");
            exit(-1);
        }
        if (P == U && W == 1) {
            LOG_ERROR(Core_ARM11, "UNDEFINED\n");
            exit(-1);
        }
#endif

        if (P == 1 && W == 0)
        {
            return VSTR(state, type, instr, value);
        }

        if (P == 1 && U == 0 && W == 1 && Rn == 0xD)
        {
            return VPUSH(state, type, instr, value);
        }

        return VSTM(state, type, instr, value);
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
          instr, CoProc, CRd, Rn, imm8, P, U, D, W);

    return ARMul_CANT;
}

unsigned VFPLDC(ARMul_State* state, unsigned type, u32 instr, u32 value)
{
    /* LDC{L}<c> <coproc>,<CRd>,[<Rn>] */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int CRd = BITS(instr, 12, 15);
    int Rn = BITS(instr, 16, 19);
    int imm8 = BITS(instr, 0, 7);
    int P = BIT(instr, 24);
    int U = BIT(instr, 23);
    int D = BIT(instr, 22);
    int W = BIT(instr, 21);

    /* TODO check access permission */

    if ( (P|U|D|W) == 0 ) {
        LOG_ERROR(Core_ARM11, "In %s, UNDEFINED\n", __FUNCTION__);
        exit(-1);
    }
    if (CoProc == 10 || CoProc == 11)
    {
        if (P == 1 && W == 0)
        {
            return VLDR(state, type, instr, value);
        }

        if (P == 0 && U == 1 && W == 1 && Rn == 0xD)
        {
            return VPOP(state, type, instr, value);
        }

        return VLDM(state, type, instr, value);
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x, CoProc %x, CRd %x, Rn %x, imm8 %x, P %x, U %x, D %x, W %x\n",
          instr, CoProc, CRd, Rn, imm8, P, U, D, W);

    return ARMul_CANT;
}

unsigned VFPCDP(ARMul_State* state, unsigned type, u32 instr)
{
    /* CDP<c> <coproc>,<opc1>,<CRd>,<CRn>,<CRm>,<opc2> */
    int CoProc = BITS(instr, 8, 11); /* 10 or 11 */
    int OPC_1 = BITS(instr, 20, 23);
    int CRd = BITS(instr, 12, 15);
    int CRn = BITS(instr, 16, 19);
    int CRm = BITS(instr, 0, 3);
    int OPC_2 = BITS(instr, 5, 7);

    /* TODO check access permission */

    /* CRn/opc1 CRm/opc2 */

    if (CoProc == 10 || CoProc == 11)
    {
        if ((OPC_1 & 0xB) == 0xB && BITS(instr, 4, 7) == 0)
        {
            unsigned int single = BIT(instr, 8) == 0;
            unsigned int d      = (single ? BITS(instr, 12,15)<<1 | BIT(instr, 22) : BITS(instr, 12,15) | BIT(instr, 22)<<4);
            unsigned int imm;
            instr = BITS(instr, 16, 19) << 4 | BITS(instr, 0, 3); // FIXME dirty workaround to get a correct imm

            if (single)
                imm = BIT(instr, 7)<<31 | (BIT(instr, 6)==0)<<30 | (BIT(instr, 6) ? 0x1f : 0)<<25 | BITS(instr, 0, 5)<<19;
            else
                imm = BIT(instr, 7)<<31 | (BIT(instr, 6)==0)<<30 | (BIT(instr, 6) ? 0xff : 0)<<22 | BITS(instr, 0, 5)<<16;

            VMOVI(state, single, d, imm);
            return ARMul_DONE;
        }

        if ((OPC_1 & 0xB) == 0xB && CRn == 0 && (OPC_2 & 0x6) == 0x2)
        {
            unsigned int single = BIT(instr, 8) == 0;
            unsigned int d      = (single ? BITS(instr, 12,15)<<1 | BIT(instr, 22) : BITS(instr, 12,15) | BIT(instr, 22)<<4);
            unsigned int m      = (single ? BITS(instr,  0, 3)<<1 | BIT(instr,  5) : BITS(instr,  0, 3) | BIT(instr, 5)<<4);
            VMOVR(state, single, d, m);
            return ARMul_DONE;
        }

        int exceptions = 0;
        if (CoProc == 10)
            exceptions = vfp_single_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);
        else
            exceptions = vfp_double_cpdo(state, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);

        vfp_raise_exceptions(state, exceptions, instr, state->VFP[VFP_OFFSET(VFP_FPSCR)]);

        return ARMul_DONE;
    }
    LOG_WARNING(Core_ARM11, "Can't identify %x\n", instr);
    return ARMul_CANT;
}

/* ----------- MRC ------------ */
void VMOVBRS(ARMul_State* state, ARMword to_arm, ARMword t, ARMword n, ARMword* value)
{
    if (to_arm)
    {
        *value = state->ExtReg[n];
    }
    else
    {
        state->ExtReg[n] = *value;
    }
}
void VMRS(ARMul_State* state, ARMword reg, ARMword Rt, ARMword* value)
{
    if (reg == 1)
    {
        if (Rt != 15)
        {
            *value = state->VFP[VFP_OFFSET(VFP_FPSCR)];
        }
        else
        {
            *value = state->VFP[VFP_OFFSET(VFP_FPSCR)] ;
        }
    }
    else
    {
        switch (reg)
        {
            case 0:
                *value = state->VFP[VFP_OFFSET(VFP_FPSID)];
                break;
            case 6:
                /* MVFR1, VFPv3 only ? */
                LOG_TRACE(Core_ARM11, "\tr%d <= MVFR1 unimplemented\n", Rt);
                break;
            case 7:
                /* MVFR0, VFPv3 only? */
                LOG_TRACE(Core_ARM11, "\tr%d <= MVFR0 unimplemented\n", Rt);
                break;
            case 8:
                *value = state->VFP[VFP_OFFSET(VFP_FPEXC)];
                break;
            default:
                LOG_TRACE(Core_ARM11, "\tSUBARCHITECTURE DEFINED\n");
                break;
        }
    }
}
void VMOVBRRD(ARMul_State* state, ARMword to_arm, ARMword t, ARMword t2, ARMword n, ARMword* value1, ARMword* value2)
{
    if (to_arm)
    {
        *value2 = state->ExtReg[n*2+1];
        *value1 = state->ExtReg[n*2];
    }
    else
    {
        state->ExtReg[n*2+1] = *value2;
        state->ExtReg[n*2] = *value1;
    }
}
void VMOVBRRSS(ARMul_State* state, ARMword to_arm, ARMword t, ARMword t2, ARMword n, ARMword* value1, ARMword* value2)
{
    if (to_arm)
    {
        *value1 = state->ExtReg[n+0];
        *value2 = state->ExtReg[n+1];
    }
    else
    {
        state->ExtReg[n+0] = *value1;
        state->ExtReg[n+1] = *value2;
    }
}

/* ----------- MCR ------------ */
void VMSR(ARMul_State* state, ARMword reg, ARMword Rt)
{
    if (reg == 1)
    {
        state->VFP[VFP_OFFSET(VFP_FPSCR)] = state->Reg[Rt];
    }
    else if (reg == 8)
    {
        state->VFP[VFP_OFFSET(VFP_FPEXC)] = state->Reg[Rt];
    }
}

/* Memory operation are not inlined, as old Interpreter and Fast interpreter
   don't have the same memory operation interface.
   Old interpreter framework does one access to coprocessor per data, and
   handles already data write, as well as address computation,
   which is not the case for Fast interpreter. Therefore, implementation
   of vfp instructions in old interpreter and fast interpreter are separate. */

/* ----------- STC ------------ */
int VSTR(ARMul_State* state, int type, ARMword instr, ARMword* value)
{
    static int i = 0;
    static int single_reg, add, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_reg = BIT(instr, 8) == 0; // Double precision
        add = BIT(instr, 23);
        imm32 = BITS(instr, 0,7)<<2; // may not be used
        d = single_reg ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); /* Base register */
        n = BITS(instr, 16, 19); // destination register

        i = 0;
        regs = 1;

        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_reg)
        {
            *value = state->ExtReg[d+i];
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            *value = state->ExtReg[d*2+i];
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}
int VPUSH(ARMul_State* state, int type, ARMword instr, ARMword* value)
{
    static int i = 0;
    static int single_regs, add, wback, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_regs = BIT(instr, 8) == 0; // Single precision
        d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
        imm32 = BITS(instr, 0,7)<<2; // may not be used
        regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 1, 7); // FSTMX if regs is odd

        state->Reg[R13] = state->Reg[R13] - imm32;

        i = 0;

        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_regs)
        {
            *value = state->ExtReg[d + i];
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            *value = state->ExtReg[d*2 + i];
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}
int VSTM(ARMul_State* state, int type, ARMword instr, ARMword* value)
{
    static int i = 0;
    static int single_regs, add, wback, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_regs = BIT(instr, 8) == 0; // Single precision
        add = BIT(instr, 23);
        wback = BIT(instr, 21); // write-back
        d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
        n = BITS(instr, 16, 19); // destination register
        imm32 = BITS(instr, 0,7) * 4; // may not be used
        regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 0, 7)>>1; // FSTMX if regs is odd

        if (wback) {
            state->Reg[n] = (add ? state->Reg[n] + imm32 : state->Reg[n] - imm32);
        }

        i = 0;

        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_regs)
        {
            *value = state->ExtReg[d + i];
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            *value = state->ExtReg[d*2 + i];
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}

/* ----------- LDC ------------ */
int VPOP(ARMul_State* state, int type, ARMword instr, ARMword value)
{
    static int i = 0;
    static int single_regs, add, wback, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_regs = BIT(instr, 8) == 0; // Single precision
        d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
        imm32 = BITS(instr, 0, 7)<<2; // may not be used
        regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 1, 7); // FLDMX if regs is odd

        state->Reg[R13] = state->Reg[R13] + imm32;

        i = 0;

        return ARMul_DONE;
    }
    else if (type == ARMul_TRANSFER)
    {
        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_regs)
        {
            state->ExtReg[d + i] = value;
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            state->ExtReg[d*2 + i] = value;
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}
int VLDR(ARMul_State* state, int type, ARMword instr, ARMword value)
{
    static int i = 0;
    static int single_reg, add, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_reg = BIT(instr, 8) == 0; // Double precision
        add = BIT(instr, 23);
        imm32 = BITS(instr, 0, 7)<<2; // may not be used
        d = single_reg ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
        n = BITS(instr, 16, 19); // destination register

        i = 0;
        regs = 1;

        return ARMul_DONE;
    }
    else if (type == ARMul_TRANSFER)
    {
        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_reg)
        {
            state->ExtReg[d+i] = value;
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            state->ExtReg[d*2+i] = value;
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}
int VLDM(ARMul_State* state, int type, ARMword instr, ARMword value)
{
    static int i = 0;
    static int single_regs, add, wback, d, n, imm32, regs;
    if (type == ARMul_FIRST)
    {
        single_regs = BIT(instr, 8) == 0; // Single precision
        add = BIT(instr, 23);
        wback = BIT(instr, 21); // write-back
        d = single_regs ? BITS(instr, 12, 15)<<1|BIT(instr, 22) : BIT(instr, 22)<<4|BITS(instr, 12, 15); // Base register
        n = BITS(instr, 16, 19); // destination register
        imm32 = BITS(instr, 0, 7) * 4; // may not be used
        regs = single_regs ? BITS(instr, 0, 7) : BITS(instr, 0, 7)>>1; // FLDMX if regs is odd

        if (wback) {
            state->Reg[n] = (add ? state->Reg[n] + imm32 : state->Reg[n] - imm32);
        }

        i = 0;

        return ARMul_DONE;
    }
    else if (type == ARMul_DATA)
    {
        if (single_regs)
        {
            state->ExtReg[d + i] = value;
            i++;
            if (i < regs)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
        else
        {
            /* FIXME Careful of endianness, may need to rework this */
            state->ExtReg[d*2 + i] = value;
            i++;
            if (i < regs*2)
                return ARMul_INC;
            else
                return ARMul_DONE;
        }
    }

    return -1;
}

/* ----------- CDP ------------ */
void VMOVI(ARMul_State* state, ARMword single, ARMword d, ARMword imm)
{
    if (single)
    {
        state->ExtReg[d] = imm;
    }
    else
    {
        /* Check endian please */
        state->ExtReg[d*2+1] = imm;
        state->ExtReg[d*2] = 0;
    }
}
void VMOVR(ARMul_State* state, ARMword single, ARMword d, ARMword m)
{
    if (single)
    {
        state->ExtReg[d] = state->ExtReg[m];
    }
    else
    {
        /* Check endian please */
        state->ExtReg[d*2+1] = state->ExtReg[m*2+1];
        state->ExtReg[d*2] = state->ExtReg[m*2];
    }
}

/* Miscellaneous functions */
int32_t vfp_get_float(arm_core_t* state, unsigned int reg)
{
    LOG_TRACE(Core_ARM11, "VFP get float: s%d=[%08x]\n", reg, state->ExtReg[reg]);
    return state->ExtReg[reg];
}

void vfp_put_float(arm_core_t* state, int32_t val, unsigned int reg)
{
    LOG_TRACE(Core_ARM11, "VFP put float: s%d <= [%08x]\n", reg, val);
    state->ExtReg[reg] = val;
}

uint64_t vfp_get_double(arm_core_t* state, unsigned int reg)
{
    uint64_t result = ((uint64_t) state->ExtReg[reg*2+1])<<32 | state->ExtReg[reg*2];
    LOG_TRACE(Core_ARM11, "VFP get double: s[%d-%d]=[%016llx]\n", reg * 2 + 1, reg * 2, result);
    return result;
}

void vfp_put_double(arm_core_t* state, uint64_t val, unsigned int reg)
{
    LOG_TRACE(Core_ARM11, "VFP put double: s[%d-%d] <= [%08x-%08x]\n", reg * 2 + 1, reg * 2, (uint32_t)(val >> 32), (uint32_t)(val & 0xffffffff));
    state->ExtReg[reg*2] = (uint32_t) (val & 0xffffffff);
    state->ExtReg[reg*2+1] = (uint32_t) (val>>32);
}

/*
 * Process bitmask of exception conditions. (from vfpmodule.c)
 */
void vfp_raise_exceptions(ARMul_State* state, u32 exceptions, u32 inst, u32 fpscr)
{
    int si_code = 0;

    LOG_TRACE(Core_ARM11, "VFP: raising exceptions %08x\n", exceptions);

    if (exceptions == VFP_EXCEPTION_ERROR) {
        LOG_TRACE(Core_ARM11, "unhandled bounce %x\n", inst);
        exit(-1);
        return;
    }

    /*
     * If any of the status flags are set, update the FPSCR.
     * Comparison instructions always return at least one of
     * these flags set.
     */
    if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
        fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);

    fpscr |= exceptions;

    state->VFP[VFP_OFFSET(VFP_FPSCR)] = fpscr;
}