summaryrefslogtreecommitdiffstats
path: root/apps/codecs/spc/spc_cpu.c
blob: 3b7c129ad8dfc7c27d333c2feaeae4dea2c3599b (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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006-2007 Adam Gashlin (hcs)
 * Copyright (C) 2004-2007 Shay Green (blargg)
 * Copyright (C) 2002 Brad Martin
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
  
/* The CPU portion (shock!) */
#include "codec.h"
#include "codecs.h"
#include "spc_codec.h"
#include "spc_profiler.h"

#undef check
#define check assert

#define READ( addr )            (SPC_read( this, addr, spc_time_ ))
#define WRITE( addr, value )    (SPC_write( this, addr, value, spc_time_ ))

#define READ_DP( addr )         READ( (addr) + dp )
#define WRITE_DP( addr, value ) WRITE( (addr) + dp, value )

#define READ_PROG16( addr )     GET_LE16( RAM + (addr) )

#define READ_PC( pc )           (*(pc))
#define READ_PC16( pc )         GET_LE16( pc )

#define SET_PC( n )             (pc = RAM + (n))
#define GET_PC()                (pc - RAM)

static unsigned char const cycle_table [0x100] = {
    2,8,4,5,3,4,3,6,2,6,5,4,5,4,6,8, /* 0 */
    2,8,4,5,4,5,5,6,5,5,6,5,2,2,4,6, /* 1 */
    2,8,4,5,3,4,3,6,2,6,5,4,5,4,5,4, /* 2 */
    2,8,4,5,4,5,5,6,5,5,6,5,2,2,3,8, /* 3 */
    2,8,4,5,3,4,3,6,2,6,4,4,5,4,6,6, /* 4 */
    2,8,4,5,4,5,5,6,5,5,4,5,2,2,4,3, /* 5 */
    2,8,4,5,3,4,3,6,2,6,4,4,5,4,5,5, /* 6 */
    2,8,4,5,4,5,5,6,5,5,5,5,2,2,3,6, /* 7 */
    2,8,4,5,3,4,3,6,2,6,5,4,5,2,4,5, /* 8 */
    2,8,4,5,4,5,5,6,5,5,5,5,2,2,12,5,/* 9 */
    3,8,4,5,3,4,3,6,2,6,4,4,5,2,4,4, /* A */
    2,8,4,5,4,5,5,6,5,5,5,5,2,2,3,4, /* B */
    3,8,4,5,4,5,4,7,2,5,6,4,5,2,4,9, /* C */
    2,8,4,5,5,6,6,7,4,5,4,5,2,2,6,3, /* D */
    2,8,4,5,3,4,3,6,2,4,5,3,4,3,4,3, /* E */
    2,8,4,5,4,5,5,6,3,4,5,4,2,2,4,3  /* F */
};

#define MEM_BIT() CPU_mem_bit( this, pc, spc_time_ )
    
static unsigned CPU_mem_bit( THIS, uint8_t const* pc, long const spc_time_ )
    ICODE_ATTR;
    
static unsigned CPU_mem_bit( THIS, uint8_t const* pc, long const spc_time_ )
{
    unsigned addr = READ_PC16( pc );
    unsigned t = READ( addr & 0x1FFF ) >> (addr >> 13);
    return (t << 8) & 0x100;
}

/* status flags */
enum { st_n = 0x80 };
enum { st_v = 0x40 };
enum { st_p = 0x20 };
enum { st_b = 0x10 };
enum { st_h = 0x08 };
enum { st_i = 0x04 };
enum { st_z = 0x02 };
enum { st_c = 0x01 };
    
#define IS_NEG (nz & 0x880)

#define CALC_STATUS( out )\
{\
    out = status & ~(st_n | st_z | st_c);\
    out |= (c >> 8) & st_c;\
    out |= (dp >> 3) & st_p;\
    if ( IS_NEG ) out |= st_n;\
    if ( !(nz & 0xFF) ) out |= st_z;\
}

#define SET_STATUS( in )\
{\
    status = in & ~(st_n | st_z | st_c | st_p);\
    c = in << 8;\
    nz = ((in << 4) & 0x800) | (~in & st_z);\
    dp = (in << 3) & 0x100;\
}


/* stack */
#define PUSH( v )       (*--sp = (uint8_t) (v))
#define PUSH16( v )     (sp -= 2, SET_LE16( sp, v ))
#define POP()           (*sp++)
#define SET_SP( v )     (sp = RAM + 0x101 + (v))
#define GET_SP()        (sp - 0x101 - RAM)

long CPU_run( THIS, long start_time )
{
#if 0
    ENTER_TIMER(cpu);
#endif
    
    register long spc_time_ = start_time;
    
#ifdef CPU_ARM
    uint8_t* const ram_ = ram.ram;
    #undef RAM
    #define RAM ram_
#endif
    
    int a = this->r.a;
    int x = this->r.x;
    int y = this->r.y;
    
    uint8_t const* pc;
    SET_PC( this->r.pc );
    
    uint8_t* sp;
    SET_SP( this->r.sp );
    
    int status;
    int c;
    int nz;
    unsigned dp;
    {
        int temp = this->r.status;
        SET_STATUS( temp );
    }

    goto loop;
    
    /* main loop */
cbranch_taken_loop:
    pc += *(int8_t const*) pc;
    spc_time_ += 2;
inc_pc_loop:
    pc++;
loop:
    check( (unsigned) GET_PC() < 0x10000 );
    check( (unsigned) GET_SP() < 0x100 );
    check( (unsigned) a < 0x100 );
    check( (unsigned) x < 0x100 );
    check( (unsigned) y < 0x100 );
    
    unsigned opcode = *pc;
    int cycles = this->cycle_table [opcode];
    unsigned data = *++pc;
    if ( (spc_time_ += cycles) > 0 )
        goto out_of_time;
    switch ( opcode )
    {
    
/* Common instructions */

#define BRANCH( cond )\
{\
    pc++;\
    int offset = (int8_t) data;\
    if ( cond ) {\
        pc += offset;\
        spc_time_ += 2;\
    }\
    goto loop;\
}
    
    case 0xF0: /* BEQ (most common) */
        BRANCH( !(uint8_t) nz )
    
    case 0xD0: /* BNE */
        BRANCH( (uint8_t) nz )
    
    case 0x3F: /* CALL */
        PUSH16( GET_PC() + 2 );
        SET_PC( READ_PC16( pc ) );
        goto loop;
    
    case 0x6F: /* RET */
        SET_PC( POP() );
        pc += POP() * 0x100;
        goto loop;

#define CASE( n )   case n:

/* Define common address modes based on opcode for immediate mode. Execution
   ends with data set to the address of the operand. */
#define ADDR_MODES( op )\
    CASE( op - 0x02 ) /* (X) */\
        data = x + dp;\
        pc--;\
        goto end_##op;\
    CASE( op + 0x0F ) /* (dp)+Y */\
        data = READ_PROG16( data + dp ) + y;\
        goto end_##op;\
    CASE( op - 0x01 ) /* (dp+X) */\
        data = READ_PROG16( ((uint8_t) (data + x)) + dp );\
        goto end_##op;\
    CASE( op + 0x0E ) /* abs+Y */\
        data += y;\
        goto abs_##op;\
    CASE( op + 0x0D ) /* abs+X */\
        data += x;\
    CASE( op - 0x03 ) /* abs */\
    abs_##op:\
        data += 0x100 * READ_PC( ++pc );\
        goto end_##op;\
    CASE( op + 0x0C ) /* dp+X */\
        data = (uint8_t) (data + x);\
    CASE( op - 0x04 ) /* dp */\
        data += dp;\
    end_##op:

/* 1. 8-bit Data Transmission Commands. Group I */

    ADDR_MODES( 0xE8 ) /* MOV A,addr */
    /*case 0xE4:*/ /* MOV a,dp (most common) */
    mov_a_addr:
        a = nz = READ( data );
        goto inc_pc_loop;
    case 0xBF: /* MOV A,(X)+ */
        data = x + dp;
        x = (uint8_t) (x + 1);
        pc--;
        goto mov_a_addr;
    
    case 0xE8: /* MOV A,imm */
        a = data;
        nz = data;
        goto inc_pc_loop;
    
    case 0xF9: /* MOV X,dp+Y */
        data = (uint8_t) (data + y);
    case 0xF8: /* MOV X,dp */
        data += dp;
        goto mov_x_addr;
    case 0xE9: /* MOV X,abs */
        data = READ_PC16( pc );
        pc++;
    mov_x_addr:
        data = READ( data );
    case 0xCD: /* MOV X,imm */
        x = data;
        nz = data;
        goto inc_pc_loop;
    
    case 0xFB: /* MOV Y,dp+X */
        data = (uint8_t) (data + x);
    case 0xEB: /* MOV Y,dp */
        data += dp;
        goto mov_y_addr;
    case 0xEC: /* MOV Y,abs */
        data = READ_PC16( pc );
        pc++;
    mov_y_addr:
        data = READ( data );
    case 0x8D: /* MOV Y,imm */
        y = data;
        nz = data;
        goto inc_pc_loop;

/* 2. 8-BIT DATA TRANSMISSION COMMANDS, GROUP 2 */

    ADDR_MODES( 0xC8 ) /* MOV addr,A */
        WRITE( data, a );
        goto inc_pc_loop;
    
    {
        int temp;
    case 0xCC: /* MOV abs,Y */
        temp = y;
        goto mov_abs_temp;
    case 0xC9: /* MOV abs,X */
        temp = x;
    mov_abs_temp:
        WRITE( READ_PC16( pc ), temp );
        pc += 2;
        goto loop;
    }
    
    case 0xD9: /* MOV dp+Y,X */
        data = (uint8_t) (data + y);
    case 0xD8: /* MOV dp,X */
        WRITE( data + dp, x );
        goto inc_pc_loop;
    
    case 0xDB: /* MOV dp+X,Y */
        data = (uint8_t) (data + x);
    case 0xCB: /* MOV dp,Y */
        WRITE( data + dp, y );
        goto inc_pc_loop;

    case 0xFA: /* MOV dp,dp */
        data = READ( data + dp );
    case 0x8F: /* MOV dp,#imm */
        WRITE_DP( READ_PC( ++pc ), data );
        goto inc_pc_loop;
    
/* 3. 8-BIT DATA TRANSMISSIN COMMANDS, GROUP 3. */
    
    case 0x7D: /* MOV A,X */
        a = x;
        nz = x;
        goto loop;
    
    case 0xDD: /* MOV A,Y */
        a = y;
        nz = y;
        goto loop;
    
    case 0x5D: /* MOV X,A */
        x = a;
        nz = a;
        goto loop;
    
    case 0xFD: /* MOV Y,A */
        y = a;
        nz = a;
        goto loop;
    
    case 0x9D: /* MOV X,SP */
        x = nz = GET_SP();
        goto loop;
    
    case 0xBD: /* MOV SP,X */
        SET_SP( x );
        goto loop;
    
    /*case 0xC6:*/ /* MOV (X),A (handled by MOV addr,A in group 2) */
    
    case 0xAF: /* MOV (X)+,A */
        WRITE_DP( x, a );
        x++;
        goto loop;
    
/* 5. 8-BIT LOGIC OPERATION COMMANDS */
    
#define LOGICAL_OP( op, func )\
    ADDR_MODES( op ) /* addr */\
        data = READ( data );\
    case op: /* imm */\
        nz = a func##= data;\
        goto inc_pc_loop;\
    {   unsigned addr;\
    case op + 0x11: /* X,Y */\
        data = READ_DP( y );\
        addr = x + dp;\
        pc--;\
        goto addr_##op;\
    case op + 0x01: /* dp,dp */\
        data = READ_DP( data );\
    case op + 0x10: /*dp,imm*/\
        addr = READ_PC( ++pc ) + dp;\
    addr_##op:\
        nz = data func READ( addr );\
        WRITE( addr, nz );\
        goto inc_pc_loop;\
    }
    
    LOGICAL_OP( 0x28, & ); /* AND */
    
    LOGICAL_OP( 0x08, | ); /* OR */
    
    LOGICAL_OP( 0x48, ^ ); /* EOR */
    
/* 4. 8-BIT ARITHMETIC OPERATION COMMANDS */

    ADDR_MODES( 0x68 ) /* CMP addr */
        data = READ( data );
    case 0x68: /* CMP imm */
        nz = a - data;
        c = ~nz;
        nz &= 0xFF;
        goto inc_pc_loop;
    
    case 0x79: /* CMP (X),(Y) */
        data = READ_DP( x );
        nz = data - READ_DP( y );
        c = ~nz;
        nz &= 0xFF;
        goto loop;
    
    case 0x69: /* CMP (dp),(dp) */
        data = READ_DP( data );
    case 0x78: /* CMP dp,imm */
        nz = READ_DP( READ_PC( ++pc ) ) - data;
        c = ~nz;
        nz &= 0xFF;
        goto inc_pc_loop;
    
    case 0x3E: /* CMP X,dp */
        data += dp;
        goto cmp_x_addr;
    case 0x1E: /* CMP X,abs */
        data = READ_PC16( pc );
        pc++;
    cmp_x_addr:
        data = READ( data );
    case 0xC8: /* CMP X,imm */
        nz = x - data;
        c = ~nz;
        nz &= 0xFF;
        goto inc_pc_loop;
    
    case 0x7E: /* CMP Y,dp */
        data += dp;
        goto cmp_y_addr;
    case 0x5E: /* CMP Y,abs */
        data = READ_PC16( pc );
        pc++;
    cmp_y_addr:
        data = READ( data );
    case 0xAD: /* CMP Y,imm */
        nz = y - data;
        c = ~nz;
        nz &= 0xFF;
        goto inc_pc_loop;
    
    {
        int addr;
    case 0xB9: /* SBC (x),(y) */
    case 0x99: /* ADC (x),(y) */
        pc--; /* compensate for inc later */
        data = READ_DP( x );
        addr = y + dp;
        goto adc_addr;
    case 0xA9: /* SBC dp,dp */
    case 0x89: /* ADC dp,dp */
        data = READ_DP( data );
    case 0xB8: /* SBC dp,imm */
    case 0x98: /* ADC dp,imm */
        addr = READ_PC( ++pc ) + dp;
    adc_addr:
        nz = READ( addr );
        goto adc_data;
        
/* catch ADC and SBC together, then decode later based on operand */
#undef CASE
#define CASE( n ) case n: case (n) + 0x20:
    ADDR_MODES( 0x88 ) /* ADC/SBC addr */
        data = READ( data );
    case 0xA8: /* SBC imm */
    case 0x88: /* ADC imm */
        addr = -1; /* A */
        nz = a;
    adc_data: {
        if ( opcode & 0x20 )
            data ^= 0xFF; /* SBC */
        int carry = (c >> 8) & 1;
        int ov = (nz ^ 0x80) + carry + (int8_t) data; /* sign-extend */
        int hc = (nz & 15) + carry;
        c = nz += data + carry;
        hc = (nz & 15) - hc;
        status = (status & ~(st_v | st_h)) | ((ov >> 2) & st_v) |
            ((hc >> 1) & st_h);
        if ( addr < 0 ) {
            a = (uint8_t) nz;
            goto inc_pc_loop;
        }
        WRITE( addr, (uint8_t) nz );
        goto inc_pc_loop;
    }
    
    }
    
/* 6. ADDITION & SUBTRACTION COMMANDS */

#define INC_DEC_REG( reg, n )\
        nz = reg + n;\
        reg = (uint8_t) nz;\
        goto loop;

    case 0xBC: INC_DEC_REG( a, 1 )  /* INC A */
    case 0x3D: INC_DEC_REG( x, 1 )  /* INC X */
    case 0xFC: INC_DEC_REG( y, 1 )  /* INC Y */
    
    case 0x9C: INC_DEC_REG( a, -1 ) /* DEC A */
    case 0x1D: INC_DEC_REG( x, -1 ) /* DEC X */
    case 0xDC: INC_DEC_REG( y, -1 ) /* DEC Y */

    case 0x9B: /* DEC dp+X */
    case 0xBB: /* INC dp+X */
        data = (uint8_t) (data + x);
    case 0x8B: /* DEC dp */
    case 0xAB: /* INC dp */
        data += dp;
        goto inc_abs;
    case 0x8C: /* DEC abs */
    case 0xAC: /* INC abs */
        data = READ_PC16( pc );
        pc++;
    inc_abs:
        nz = ((opcode >> 4) & 2) - 1;
        nz += READ( data );
        WRITE( data, (uint8_t) nz );
        goto inc_pc_loop;
    
/* 7. SHIFT, ROTATION COMMANDS */

    case 0x5C: /* LSR A */
        c = 0;
    case 0x7C:{/* ROR A */
        nz = ((c >> 1) & 0x80) | (a >> 1);
        c = a << 8;
        a = nz;
        goto loop;
    }
    
    case 0x1C: /* ASL A */
        c = 0;
    case 0x3C:{/* ROL A */
        int temp = (c >> 8) & 1;
        c = a << 1;
        nz = c | temp;
        a = (uint8_t) nz;
        goto loop;
    }
    
    case 0x0B: /* ASL dp */
        c = 0;
        data += dp;
        goto rol_mem;
    case 0x1B: /* ASL dp+X */
        c = 0;
    case 0x3B: /* ROL dp+X */
        data = (uint8_t) (data + x);
    case 0x2B: /* ROL dp */
        data += dp;
        goto rol_mem;
    case 0x0C: /* ASL abs */
        c = 0;
    case 0x2C: /* ROL abs */
        data = READ_PC16( pc );
        pc++;
    rol_mem:
        nz = (c >> 8) & 1;
        nz |= (c = READ( data ) << 1);
        WRITE( data, (uint8_t) nz );
        goto inc_pc_loop;
    
    case 0x4B: /* LSR dp */
        c = 0;
        data += dp;
        goto ror_mem;
    case 0x5B: /* LSR dp+X */
        c = 0;
    case 0x7B: /* ROR dp+X */
        data = (uint8_t) (data + x);
    case 0x6B: /* ROR dp */
        data += dp;
        goto ror_mem;
    case 0x4C: /* LSR abs */
        c = 0;
    case 0x6C: /* ROR abs */
        data = READ_PC16( pc );
        pc++;
    ror_mem: {
        int temp = READ( data );
        nz = ((c >> 1) & 0x80) | (temp >> 1);
        c = temp << 8;
        WRITE( data, nz );
        goto inc_pc_loop;
    }

    case 0x9F: /* XCN */
        nz = a = (a >> 4) | (uint8_t) (a << 4);
        goto loop;

/* 8. 16-BIT TRANSMISION COMMANDS */

    case 0xBA: /* MOVW YA,dp */
        a = READ_DP( data );
        nz = (a & 0x7F) | (a >> 1);
        y = READ_DP( (uint8_t) (data + 1) );
        nz |= y;
        goto inc_pc_loop;
    
    case 0xDA: /* MOVW dp,YA */
        WRITE_DP( data, a );
        WRITE_DP( (uint8_t) (data + 1), y );
        goto inc_pc_loop;
    
/* 9. 16-BIT OPERATION COMMANDS */

    case 0x3A: /* INCW dp */
    case 0x1A:{/* DECW dp */
        data += dp;
        
        /* low byte */
        int temp = READ( data );
        temp += ((opcode >> 4) & 2) - 1; /* +1 for INCW, -1 for DECW */
        nz = ((temp >> 1) | temp) & 0x7F;
        WRITE( data, (uint8_t) temp );
        
        /* high byte */
        data = ((uint8_t) (data + 1)) + dp;
        temp >>= 8;
        temp = (uint8_t) (temp + READ( data ));
        nz |= temp;
        WRITE( data, temp );
        
        goto inc_pc_loop;
    }
        
    case 0x9A: /* SUBW YA,dp */
    case 0x7A: /* ADDW YA,dp */
    {
        /* read 16-bit addend */
        int temp = READ_DP( data );
        int sign = READ_DP( (uint8_t) (data + 1) );
        temp += 0x100 * sign;
        status &= ~(st_v | st_h);
        
        /* to do: fix half-carry for SUBW (it's probably wrong) */
        
        /* for SUBW, negate and truncate to 16 bits */
        if ( opcode & 0x80 ) {
            temp = (temp ^ 0xFFFF) + 1;
            sign = temp >> 8;
        }
        
        /* add low byte (A) */
        temp += a;
        a = (uint8_t) temp;
        nz = (temp | (temp >> 1)) & 0x7F;
        
        /* add high byte (Y) */
        temp >>= 8;
        c = y + temp;
        nz = (nz | c) & 0xFF;
        
        /* half-carry (temporary avoids CodeWarrior optimizer bug) */
        unsigned hc = (c & 15) - (y & 15);
        status |= (hc >> 4) & st_h;
        
        /* overflow if sign of YA changed when previous sign
           and addend sign were same */
        status |= (((c ^ y) & ~(y ^ sign)) >> 1) & st_v;
        
        y = (uint8_t) c;
        
        goto inc_pc_loop;
    }
    
    case 0x5A: { /* CMPW YA,dp */
        int temp = a - READ_DP( data );
        nz = ((temp >> 1) | temp) & 0x7F;
        temp = y + (temp >> 8);
        temp -= READ_DP( (uint8_t) (data + 1) );
        nz |= temp;
        c = ~temp;
        nz &= 0xFF;
        goto inc_pc_loop;
    }
    
/* 10. MULTIPLICATION & DIVISON COMMANDS */

    case 0xCF: { /* MUL YA */
        unsigned temp = y * a;
        a = (uint8_t) temp;
        nz = ((temp >> 1) | temp) & 0x7F;
        y = temp >> 8;
        nz |= y;
        goto loop;
    }
    
    case 0x9E: /* DIV YA,X */
    {
        /* behavior based on SPC CPU tests */
        
        status &= ~(st_h | st_v);
        
        if ( (y & 15) >= (x & 15) )
            status |= st_h;
        
        if ( y >= x )
            status |= st_v;
        
        unsigned ya = y * 0x100 + a;
        if ( y < x * 2 )
        {
            a = ya / x;
            y = ya - a * x;
        }
        else
        {
            a = 255 - (ya - x * 0x200) / (256 - x);
            y = x   + (ya - x * 0x200) % (256 - x);
        }
        
        nz = (uint8_t) a;
        a = (uint8_t) a;
        
        goto loop;
    }
    
/* 11. DECIMAL COMPENSATION COMMANDS */
    
    /* seem unused */
    /* case 0xDF: */ /* DAA */
    /* case 0xBE: */ /* DAS */
    
/* 12. BRANCHING COMMANDS */

    case 0x2F: /* BRA rel */
        pc += (int8_t) data;
        goto inc_pc_loop;
    
    case 0x30: /* BMI */
        BRANCH( IS_NEG )
    
    case 0x10: /* BPL */
        BRANCH( !IS_NEG )
    
    case 0xB0: /* BCS */
        BRANCH( c & 0x100 )
    
    case 0x90: /* BCC */
        BRANCH( !(c & 0x100) )
    
    case 0x70: /* BVS */
        BRANCH( status & st_v )
    
    case 0x50: /* BVC */
        BRANCH( !(status & st_v) )
    
    case 0x03: /* BBS dp.bit,rel */
    case 0x23:
    case 0x43:
    case 0x63:
    case 0x83:
    case 0xA3:
    case 0xC3:
    case 0xE3:
        pc++;
        if ( (READ_DP( data ) >> (opcode >> 5)) & 1 )
            goto cbranch_taken_loop;
        goto inc_pc_loop;
    
    case 0x13: /* BBC dp.bit,rel */
    case 0x33:
    case 0x53:
    case 0x73:
    case 0x93:
    case 0xB3:
    case 0xD3:
    case 0xF3:
        pc++;
        if ( !((READ_DP( data ) >> (opcode >> 5)) & 1) )
            goto cbranch_taken_loop;
        goto inc_pc_loop;
    
    case 0xDE: /* CBNE dp+X,rel */
        data = (uint8_t) (data + x);
        /* fall through */
    case 0x2E: /* CBNE dp,rel */
        pc++;
        if ( READ_DP( data ) != a )
            goto cbranch_taken_loop;
        goto inc_pc_loop;
    
    case 0xFE: /* DBNZ Y,rel */
        y = (uint8_t) (y - 1);
        BRANCH( y )
    
    case 0x6E: { /* DBNZ dp,rel */
        pc++;
        unsigned temp = READ_DP( data ) - 1;
        WRITE_DP( (uint8_t) data, (uint8_t) temp );
        if ( temp )
            goto cbranch_taken_loop;
        goto inc_pc_loop;
    }
    
    case 0x1F: /* JMP (abs+X) */
        SET_PC( READ_PC16( pc ) + x );
        /* fall through */
    case 0x5F: /* JMP abs */
        SET_PC( READ_PC16( pc ) );
        goto loop;
    
/* 13. SUB-ROUTINE CALL RETURN COMMANDS */
    
    case 0x0F:{/* BRK */
        check( 0 ); /* untested */
        PUSH16( GET_PC() + 1 );
        SET_PC( READ_PROG16( 0xFFDE ) ); /* vector address verified */
        int temp;
        CALC_STATUS( temp );
        PUSH( temp );
        status = (status | st_b) & ~st_i;
        goto loop;
    }
    
    case 0x4F: /* PCALL offset */
        PUSH16( GET_PC() + 1 );
        SET_PC( 0xFF00 + data );
        goto loop;
    
    case 0x01: /* TCALL n */
    case 0x11:
    case 0x21:
    case 0x31:
    case 0x41:
    case 0x51:
    case 0x61:
    case 0x71:
    case 0x81:
    case 0x91:
    case 0xA1:
    case 0xB1:
    case 0xC1:
    case 0xD1:
    case 0xE1:
    case 0xF1:
        PUSH16( GET_PC() );
        SET_PC( READ_PROG16( 0xFFDE - (opcode >> 3) ) );
        goto loop;
    
/* 14. STACK OPERATION COMMANDS */

    {
        int temp;
    case 0x7F: /* RET1 */
        temp = POP();
        SET_PC( POP() );
        pc += POP() << 8;
        goto set_status;
    case 0x8E: /* POP PSW */
        temp = POP();
    set_status:
        SET_STATUS( temp );
        goto loop;
    }
    
    case 0x0D: { /* PUSH PSW */
        int temp;
        CALC_STATUS( temp );
        PUSH( temp );
        goto loop;
    }

    case 0x2D: /* PUSH A */
        PUSH( a );
        goto loop;
    
    case 0x4D: /* PUSH X */
        PUSH( x );
        goto loop;
    
    case 0x6D: /* PUSH Y */
        PUSH( y );
        goto loop;
    
    case 0xAE: /* POP A */
        a = POP();
        goto loop;
    
    case 0xCE: /* POP X */
        x = POP();
        goto loop;
    
    case 0xEE: /* POP Y */
        y = POP();
        goto loop;
    
/* 15. BIT OPERATION COMMANDS */

    case 0x02: /* SET1 */
    case 0x22:
    case 0x42:
    case 0x62:
    case 0x82:
    case 0xA2:
    case 0xC2:
    case 0xE2:
    case 0x12: /* CLR1 */
    case 0x32:
    case 0x52:
    case 0x72:
    case 0x92:
    case 0xB2:
    case 0xD2:
    case 0xF2: {
        data += dp;
        int bit = 1 << (opcode >> 5);
        int mask = ~bit;
        if ( opcode & 0x10 )
            bit = 0;
        WRITE( data, (READ( data ) & mask) | bit );
        goto inc_pc_loop;
    }
        
    case 0x0E: /* TSET1 abs */
    case 0x4E:{/* TCLR1 abs */
        data = READ_PC16( pc );
        pc += 2;
        unsigned temp = READ( data );
        nz = temp & a;
        temp &= ~a;
        if ( !(opcode & 0x40) )
            temp |= a;
        WRITE( data, temp );
        goto loop;
    }
    
    case 0x4A: /* AND1 C,mem.bit */
        c &= MEM_BIT();
        pc += 2;
        goto loop;
    
    case 0x6A: /* AND1 C,/mem.bit */
        check( 0 ); /* untested */
        c &= ~MEM_BIT();
        pc += 2;
        goto loop;
    
    case 0x0A: /* OR1 C,mem.bit */
        check( 0 ); /* untested */
        c |= MEM_BIT();
        pc += 2;
        goto loop;
    
    case 0x2A: /* OR1 C,/mem.bit */
        check( 0 ); /* untested */
        c |= ~MEM_BIT();
        pc += 2;
        goto loop;
    
    case 0x8A: /* EOR1 C,mem.bit */
        c ^= MEM_BIT();
        pc += 2;
        goto loop;
    
    case 0xEA: { /* NOT1 mem.bit */
        data = READ_PC16( pc );
        pc += 2;
        unsigned temp = READ( data & 0x1FFF );
        temp ^= 1 << (data >> 13);
        WRITE( data & 0x1FFF, temp );
        goto loop;
    }
    
    case 0xCA: { /* MOV1 mem.bit,C */
        data = READ_PC16( pc );
        pc += 2;
        unsigned temp = READ( data & 0x1FFF );
        unsigned bit = data >> 13;
        temp = (temp & ~(1 << bit)) | (((c >> 8) & 1) << bit);
        WRITE( data & 0x1FFF, temp );
        goto loop;
    }
    
    case 0xAA: /* MOV1 C,mem.bit */
        c = MEM_BIT();
        pc += 2;
        goto loop;
    
/* 16. PROGRAM STATUS FLAG OPERATION COMMANDS */

    case 0x60: /* CLRC */
        c = 0;
        goto loop;
        
    case 0x80: /* SETC */
        c = ~0;
        goto loop;
    
    case 0xED: /* NOTC */
        c ^= 0x100;
        goto loop;
        
    case 0xE0: /* CLRV */
        status &= ~(st_v | st_h);
        goto loop;
    
    case 0x20: /* CLRP */
        dp = 0;
        goto loop;
    
    case 0x40: /* SETP */
        dp = 0x100;
        goto loop;
    
    case 0xA0: /* EI */
        check( 0 ); /* untested */
        status |= st_i;
        goto loop;
    
    case 0xC0: /* DI */
        check( 0 ); /* untested */
        status &= ~st_i;
        goto loop;
    
/* 17. OTHER COMMANDS */

    case 0x00: /* NOP */
        goto loop;
    
    /*case 0xEF:*/ /* SLEEP */
    /*case 0xFF:*/ /* STOP */
    case 0xFF:
        c |= 1; /* force switch table to have 256 entries,
                   hopefully helping optimizer */
    } /* switch */
    
    /* unhandled instructions fall out of switch so emulator can catch them */
    
out_of_time:
    /* undo partial execution of opcode */
    spc_time_ -= this->cycle_table [*--pc]; 
    {
        int temp;
        CALC_STATUS( temp );
        this->r.status = (uint8_t) temp;
    }
    
    this->r.pc = GET_PC();
    this->r.sp = (uint8_t) GET_SP();
    this->r.a  = (uint8_t) a;
    this->r.x  = (uint8_t) x;
    this->r.y  = (uint8_t) y;
 
#if 0
    EXIT_TIMER(cpu);
#endif
    return spc_time_;
}

void CPU_Init( THIS )
{
    ci->memcpy( this->cycle_table, cycle_table, sizeof cycle_table );
}