musix 0.3.5

Music player library for esoteric audio formats (music from C64,Amiga etc)
Documentation
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
/*
 * @file    emu68.c
 * @brief   emu68 main API
 * @author  http://sourceforge.net/users/benjihan
 *
 * Copyright (C) 1998-2011 Benjamin Gerard
 *
 * Time-stamp: <2011-10-28 22:45:13 ben>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

/* generated config include */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "struct68.h"
#include "srdef68.h"
#include "excep68.h"
#include "assert68.h"

#include "emu68.h"
#include "ioplug68.h"

#include "macro68.h"

#include "inl68_exception.h"
#include <string.h>

EMU68_EXTERN linefunc68_t *line_func[1024];

/* ,-----------------------------------------------------------------.
 * |                     Internal struct access                      |
 * `-----------------------------------------------------------------'
 */

/*  Set new interrupt IO
 *  return previous pointer
 */
io68_t * emu68_set_interrupt_io(emu68_t * emu68, io68_t * io)
{
  io68_t * oldio = 0;
  if (emu68) {
    oldio = emu68->interrupt_io;
    emu68->interrupt_io = io;
  }
  return oldio;
}

void emu68_set_registers(emu68_t * emu68, const reg68_t * r, int mask)
{
  if (emu68 && r) {
    int i;
    if ( mask & ( 1 << REG68_US_IDX ) ) REG68.usp = r->usp;
    if ( mask & ( 1 << REG68_PC_IDX ) ) REG68.pc  = r->pc;
    if ( mask & (1  << REG68_SR_IDX ) ) REG68.sr  = r->sr;
    for ( i=0; i<8; i++ )
      if ( mask & ( 1 << ( REG68_D0_IDX + i ) ) ) REG68.d[i] = r->d[i];
    for ( i=0; i<8; i++)
      if ( mask & ( 1 << ( REG68_A0_IDX + i ) ) ) REG68.a[i] = r->a[i];
  }
}

void emu68_get_registers(const emu68_t * const emu68, reg68_t * r, int mask)
{
  if (emu68 && r) {
    int i;
    if ( mask & ( 1 << REG68_US_IDX ) ) r->usp = REG68.usp;
    if ( mask & ( 1 << REG68_PC_IDX ) ) r->pc  = REG68.pc;
    if ( mask & ( 1 << REG68_SR_IDX ) ) r->sr  = REG68.sr;
    for ( i=0; i<8; i++ )
      if ( mask & ( 1 << ( REG68_D0_IDX + i ) ) ) r->d[i] = REG68.d[i];
    for ( i=0; i<8; i++ )
      if ( mask & ( 1 << ( REG68_A0_IDX + i ) ) ) r->a[i] = REG68.a[i];
  }
}

void emu68_set_cycle(emu68_t * const emu68, cycle68_t cycle)
{
  emu68->cycle = cycle;
}

cycle68_t emu68_get_cycle(emu68_t * const emu68)
{
  return emu68->cycle;
}

void * emu68_set_cookie(emu68_t * const emu68, void * cookie)
{
  void * old = 0;
  if (emu68) {
    old = emu68->cookie;
    emu68->cookie = cookie;
  }
  return old;
}

void * emu68_get_cookie(emu68_t * const emu68)
{
  return emu68
    ? emu68->cookie
    : 0
    ;
}

emu68_handler_t emu68_set_handler(emu68_t * const emu68, emu68_handler_t hdl)
{
  emu68_handler_t old = 0;
  if (emu68) {
    old = emu68->handler;
    emu68->handler = hdl;
  }
  return old;
}

emu68_handler_t emu68_get_handler(emu68_t * const emu68)
{
  return emu68
    ? emu68->handler
    : 0
    ;
}

const char * emu68_exception_name(unsigned int vector)
{
  const char * ret = 0;

  static const char * xtra_names[] = {
    "hw-brkp", "hw-trace", "hw-halt"
  };
  static const char * xcpt_names[] = {
    "reset", "reset_pc", "bus-error", "addr-error",
    "illegal", "0-divide", "chk", "trapv", "privv",
    "trace", "linea", "linef"
  };
  static const char * trap_names[] = {
    "trap#0", "trap#1", "trap#2", "trap#3",
    "trap#4", "trap#5", "trap#6", "trap#7",
    "trap#8", "trap#9", "trap#A", "trap#B",
    "trap#C", "trap#D", "trap#E", "trap#F",
  };
  if (vector >= 0x100) {
    vector -= 0x100;
    if (vector < sizeof(xtra_names)/sizeof(*xtra_names))
      ret = xtra_names[vector];
  } else if (vector < sizeof(xcpt_names)/sizeof(*xcpt_names)) {
    ret = xcpt_names[vector];
  } else if (vector >= TRAP_VECTOR(0) && vector <= TRAP_VECTOR(15)) {
    ret = trap_names[vector-TRAP_VECTOR_0];
  }
  return ret;
}

/* ,-----------------------------------------------------------------.
 * |                      Onboard memory access                      |
 * `-----------------------------------------------------------------'
 */

u8 * emu68_memptr(emu68_t * const emu68, addr68_t dst, uint68_t sz)
{
  u8 * ptr = 0;
  if (emu68) {
    addr68_t end = (dst+sz)&MEMMSK68;
    if (sz > MEMMSK68+1) {
      emu68_error_add(emu68,
                      "Not enough 68K memory ($%X>=$%X)",sz,emu68->memmsk);
    } else if (end < dst) {
      emu68_error_add(emu68,
                      "68K memory overflow :($%X-%X,$%X)",dst,dst+sz,sz);
    } else {
      ptr = emu68->mem + dst;
    }
  }
  return ptr;
}

u8 * emu68_chkptr(emu68_t * const emu68, addr68_t dst, uint68_t sz)
{
  u8 * ptr = emu68_memptr(emu68,dst,sz);
  if (ptr && emu68->chk) {
    ptr = emu68->chk + (ptr - emu68->mem);
  }
  return ptr;
}

/* Peek & Poke */
int emu68_peek(emu68_t * const emu68, addr68_t addr)
{
  return emu68
    ? emu68->mem[addr&MEMMSK68]
    : -1
    ;
}

int emu68_chkpeek(emu68_t * const emu68, addr68_t addr)
{
  return (emu68 && emu68->chk)
    ? emu68->chk[addr&MEMMSK68]
    : -1
    ;
}

int emu68_poke(emu68_t * const emu68, addr68_t addr, int68_t v)
{
  return emu68
    ? (emu68->mem[addr & MEMMSK68] = v)
    : -1
    ;
}

int emu68_chkpoke(emu68_t * const emu68, addr68_t addr, int68_t v)
{
  return (emu68 && emu68->chk)
    ? (emu68->chk[addr & MEMMSK68] = v)
    : -1
    ;
}

/*  Write memory block to 68K on board memory
 */
int emu68_memput(emu68_t * const emu68, addr68_t dst, u8 *src, uint68_t sz)
{
  u8 * ptr = emu68_memptr(emu68,dst,sz);
  if (ptr) {
    memcpy(ptr,src,sz);
    return 0;
  }
  return -1;
}

/*  Read memory block from 68K on board memory
 */
int emu68_memget(emu68_t * const emu68, u8 *dst, addr68_t src, uint68_t sz)
{
  u8 * ptr = emu68_memptr(emu68,src,sz);
  if (ptr) {
    memcpy(dst,ptr,sz);
  }
  return -!ptr;
}

int emu68_memset(emu68_t * const emu68, addr68_t dst, u8 val, uint68_t sz)
{
  u8 * ptr;

  if (!emu68) {
    ptr = 0;
  } else {
    if (!sz) {
      sz = emu68->memmsk + 1 - dst;
    }
    ptr = emu68_memptr(emu68, dst, sz);
    if (ptr) {
      while (sz--) *ptr++ = val;
    }
  }
  return -!ptr;
}

int emu68_chkset(emu68_t * const emu68, addr68_t dst, u8 val, uint68_t sz)
{
  u8 * ptr;

  if (!emu68) {
    ptr = 0;
  } else {
    if (!sz) {
      sz = emu68->memmsk+1-dst;
    }
    ptr = emu68_chkptr(emu68, dst, sz);
    if (ptr) {
      while (sz--) *ptr++ = val;
    }
  }
  return -!ptr;
}

#include "crc32.inc"

uint68_t emu68_crc32(emu68_t * const emu68)
{
  uint68_t crc = 0;
  if ( emu68 ) {
    int i;
    u8 tmp [ 18 * 4 + 2 ];              /* serialize registers */
    s32 * reg32 = emu68->reg.d;
    for ( i = 0; i < 18*4; i += 4 ) {
      unsigned int v = reg32[i>>2];
      tmp[i+0] = v >> 24;
      tmp[i+1] = v >> 16;
      tmp[i+2] = v >> 8;
      tmp[i+3] = v;
    }
    tmp[i++] = emu68->reg.sr >> 8;
    tmp[i++] = emu68->reg.sr;
    assert ( i == sizeof(tmp) );

    crc = crc32(crc, tmp, i);
    crc = crc32(crc, emu68->mem, emu68->memmsk+1);
  }
  return crc;
}

/* Process a single instruction emulation. */
static inline void step68(emu68_t * const emu68)
{
  int line,opw,reg9;
  u8 * mem;

  assert( emu68->status == EMU68_NRM );

  /* TODO: check address valid */
  mem = emu68->mem + (REG68.pc & (MEMMSK68 & ~1));
  REG68.pc += 2;
  opw  = (mem[0]<<8) | mem[1];

 /* 68000 OP-WORD format :
  *  1111 0000 0000 0000 ( LINE  )
  *  0000 0001 1111 1000 ( OP    )
  *  0000 0000 0000 0111 ( REG 0 )
  *  0000 1110 0000 0000 ( REG 9 )
  */

  /*                       LINE RG9 OPCODE  RG0 */
  /*                       -------------------- */
  line = opw & 0170000; /* 1111 000 000-000 000 */
  opw -= line;          /* 0000 111 111-111 111 */
  reg9 = opw &   07000; /* 0000 111 000-000 000 */
  opw -= reg9;          /* 0000 000 111-111 111 */
  reg9 >>= 9;           /* reg9 */
  line |= opw<<3;
  line >>= 6;
  opw  &=  7;           /* 0000 000 000-000 111 */
  (line_func[line])(emu68, reg9, opw);
}

/* $$$ evil duplicated code from mem68.c */
static void chkframe(emu68_t * const emu68, addr68_t addr, const int flags)
{
  int oldchk;

  assert ( ! (addr & 0x800000) );
  addr &= MEMMSK68;
  oldchk = emu68->chk[addr];
  if ( ( oldchk & flags ) != flags ) {
    emu68->framechk |= flags;
    emu68->chk[addr] = oldchk|flags;
  }
}

/* Run a single step emulation with all pogramm controls. */
static inline int controlled_step68(emu68_t * const emu68)
{
  assert( emu68->status == EMU68_NRM );

  /* Running in debug mode. */
  if (emu68->chk) {
     /* HardWare TRACE exception */
    exception68(emu68, HWTRACE_VECTOR, -1);
    if (emu68->status != EMU68_NRM)
      return emu68->status;
     /* HardWare BREAKPOINT exception */
    if (emu68->chk[ REG68.pc & MEMMSK68 ] & EMU68_B) {
      exception68(emu68, HWBREAK_VECTOR, -1);
      if (emu68->status != EMU68_NRM)
        return emu68->status;
    }
    chkframe(emu68, REG68.pc, EMU68_X);
  }

  /* Execute 68K instruction. */
  step68(emu68);

  /* Instruction countdown */
  if ( emu68->instructions && !--emu68->instructions )
    emu68->status = EMU68_BRK;

  return emu68->status;
}

static void loop68(emu68_t * const emu68)
{
  assert( ! (emu68->finish_sp & 1 ) );
  while ( controlled_step68(emu68) == EMU68_NRM &&
          emu68->finish_sp >= (addr68_t) REG68.a[7] )
    ;
}

const char * emu68_status_name(enum emu68_status_e status)
{
  switch (status) {
  case EMU68_ERR: return "error";
  case EMU68_NRM: return "ok";
  case EMU68_STP: return "halt";
  case EMU68_BRK: return "break";
  case EMU68_XCT: return "exception";
  }
  return "unknown";
}

/* Execute one instruction. */
int emu68_step(emu68_t * const emu68)
{
  int rc = EMU68_ERR;
  if (emu68) {
    switch (emu68->status) {
    case EMU68_NRM:
      emu68->framechk = 0;
    case EMU68_BRK:
      emu68->status = EMU68_NRM;
      /* loop68(emu68); */
      controlled_step68(emu68);
    case EMU68_STP:
      rc = emu68->status;
      break;
    default:
      assert(!"unexpected value for emu68_t::status");
      break;
    }
  }
  return rc;
}

/* Continue after a break. */
int emu68_continue(emu68_t * const emu68)
{
  int rc = EMU68_ERR;

  if (emu68) {
    switch (emu68->status) {

    case EMU68_NRM:
      /* Nothing to continue: error */
      break;

    case EMU68_BRK:
      emu68->status = EMU68_NRM;
      controlled_step68(emu68);
    case EMU68_STP:
      rc = emu68->status;
      break;

    default:
      assert(!"unexpected value for emu68_t::status");
      break;
    }
  }
  return rc;
}

int emu68_finish(emu68_t * const emu68, uint68_t instructions)
{
  io68_t *io;

  if (!emu68) {
    return EMU68_ERR;
  }

  emu68->finish_sp    = REG68.a[7];
  emu68->framechk     = 0;
  emu68->instructions = instructions;

  for (io=emu68->iohead; io; io=io->next) {
    io->adjust_cycle(io,emu68->cycle);
  }
  emu68->cycle = 0;

  loop68(emu68);

  return emu68->status;
}

/* Execute one instruction. */
int emu68_interrupt(emu68_t * const emu68, cycle68_t cycleperpass)
{

  if (!emu68) {
    return EMU68_ERR;
  }

  assert ( emu68->status == EMU68_NRM || emu68->status == EMU68_STP );

  /* Get interrupt IO (MFP) if any */
  if (emu68->interrupt_io) {
    for ( ;; ) {
      const int ipl = ( REG68.sr >> SR_I_BIT ) & 7;
      interrupt68_t * t =
        emu68->interrupt_io->interrupt(emu68->interrupt_io, cycleperpass);
      if (!t)
        break;
      emu68->cycle = t->cycle;
      if (t->level > ipl) {
        inl_exception68(emu68, t->vector, t->level);
        emu68->finish_sp = (addr68_t) REG68.a[7];
        loop68(emu68);
      }
    }
  }
  emu68->cycle = cycleperpass;

  return emu68->status;
}

#if 0
/* Run until RTS level0 occurs
 * and play Interruption for 1 pass.
 * ( Specific SC68 !!! )
 */
void emu68_level_and_interrupt(emu68_t * const emu68,
                               cycle68_t cycleperpass)
{
  addr68_t stack, pc;
  cycle68_t cycle;
  io68_t *io;

  if (!emu68) {
    return;
  }
  stack = REG68.a[7];
  pc = REG68.pc;
  REG68.a[7] = stack - 4;

  /* Clear ORed memory access flags ... */
  emu68->framechk = 0;

  /* Adjust internal IO cycle counter */
  for (io=emu68->iohead; io; io=io->next) {
    (io->adjust_cycle)(io,emu68->cycle);
  }
  emu68->cycle = 0;

  /* Do until RTS */
  do {
    step68(emu68);
  } while (stack>(addr68_t)REG68.a[7]);

  cycle=0;

  /* Get interrupt IO (MFP) if any */
  if (emu68->interrupt_io) {
    interrupt68_t * t;
    int ipl = (REG68.sr & (7<<SR_I_BIT)) >> SR_I_BIT;
#ifdef _DEBUG
    int ipl_lost = 0;
    int irq_pass = 0;
#endif
    /* $$$ HACK: mfp interupt at level 5 */
    /* $$$ TEMP: FOR DEBUGIN A MUSIC !! */
    if ( /*1 ||*/ 6 > ipl ) {
      do {

        /* Spool interrupt */
        /* Execute mfp interrupt emulation */
        t = emu68->interrupt_io->interrupt(emu68->interrupt_io, cycleperpass);
        if (t) {
#ifdef DEBUG
          if (t->cycle >= cycleperpass) {
            *(int *)0 = 0xDEADBEEF;
          }
#endif


          emu68->cycle=t->cycle;
          REG68.a[7] = stack; /* $$$ Safety net */
          REG68.pc = 0x12345678;
          inl_exception68(emu68,t->vector,t->level);
          poll_rte(emu68, stack);
#ifdef _DEBUG
          if (REG68.pc != 0x12345678) {
            BREAKPOINT68;
          }
          if (REG68.a[7] != stack) {
            BREAKPOINT68;
          }
          if (!(REG68.sr & 0x2000)) {
            BREAKPOINT68;
          }
#endif
        } else {
          //          BREAKPOINT68;
          break; //$$$ ???
        }
      } while (t);
    } else {
      // ipl masks irq
#ifdef _DEBUG
      // This happens sometime
      //BREAKPOINT68;
      ipl_lost++;
#endif
    }
  }

  /* Restore A7, PC, and new cycle counter */
  REG68.a[7]  = stack;
  REG68.pc    = pc;
  emu68->cycle = cycleperpass;
}

/* Run like a JSR at pc, exec interrupt if cycleperpass > 0.
 */
void emu68_run_rts(emu68_t * const emu68,
                   addr68_t pc, cycle68_t cycleperpass)
{
  addr68_t stack;

  if (!emu68) {
    return;
  }

  /* Push current pc .*/
  stack = REG68.a[7];
  pushl(REG68.pc);
  REG68.pc = pc;

  /* Clear ORed memory access flags ... */
  emu68->framechk = 0;

  /* Adjust internal IO cycle counter & find MFP */
  if (emu68->cycle) {
    io68_t *io;
    for (io=emu68->iohead; io; io=io->next) {
      (io->adjust_cycle)(io, emu68->cycle);
    }
    emu68->cycle = 0;
  }

  /* Do until RTS */
  do {
    step68(emu68);
  } while (stack > (addr68_t)REG68.a[7]);

  /* Get interrupt IO (MFP) if any */
  if (cycleperpass && emu68->interrupt_io) {
    interrupt68_t *t;
    int ipl = (REG68.sr & (7<<SR_I_BIT)) >> SR_I_BIT;
    cycle68_t cycle = 0;

    if (6 > ipl ) {
      cycle68_t fd;

      /* Spool interrupt */
      while (fd =
             emu68->interrupt_io->next_interrupt(emu68->interrupt_io,cycle),
             fd != IO68_NO_INT) {
        /* ... This must not happen ...  */
        if( (int)fd < 0 ) {
          fd = 0;
        }

        /* ... */

        /* TIME travel to next interruption */
        cycle += fd;

        if(cycle >= cycleperpass) {
          /* interrupt after this pass */
          break;
        }

        /* Execute mfp interrupt emulation */
        t = emu68->interrupt_io->interrupt(emu68->interrupt_io,cycle);
        if (t) {
          emu68->cycle=cycle;
          REG68.a[7] = stack; /* $$$ Safety net */
          inl_exception68(emu68,t->vector,t->level);
          poll_rte(emu68,stack);
        }
      }
    }
  }
  emu68->cycle = cycleperpass;
}


/* Run emulation for given number of cycle
 */
void emu68_cycle(emu68_t * const emu68, cycle68_t cycleperpass)
{
  cycle68_t cycle;
  io68_t *io;

  /* Clear ORed memory access flags ... */
  REG68.framechk = 0;

  /* Adjust internal IO cycle counter */
  for (io=emu68->iohead; io; io=io->next) {
    (io->adjust_cycle)(io, emu68->cycle);
  }

  /* Do until RTS */
  cycle = emu68->cycle = 0;
  do
  {
    /* If not stopped, execute an instruction */
    if (1/* !REG68.status */) {
      step68(emu68);
      cycle += 4;
    }
    emu68->cycle = cycle;

    if (emu68->interrupt_io) {
      interrupt68_t *t;
      int ipl;

      ipl = (REG68.sr & (7<<SR_I_BIT)) >> SR_I_BIT;

      /* $$$ HACK : mfp interupt at level 5 */
      if (6 > ipl) {
        t = emu68->interrupt_io->interrupt(emu68->interrupt_io,cycle);
        if (t) {
          inl_exception68(emu68,t->vector,t->level);
          /* $$$ extra cycle should be added here for exception handle */
        }
      }
    }
  } while(cycle < cycleperpass);

  // $$$ This is hack : should be "cycle" and handle later
  cycle = cycleperpass;
  emu68->cycle = cycle;
}

#endif

/* Get debug mode.  */
int emu68_debugmode(emu68_t * const emu68)
{
  return emu68
    ? !!emu68->chk
    : -1
    ;
}

/* ,-----------------------------------------------------------------.
 * |                           Breakpoints                           |
 * `-----------------------------------------------------------------'
 */

static void bp_reset(emu68_t * const emu68)
{
  int id;
  for ( id=0; id<16; ++id ) {
    emu68->breakpoints[id].addr  = 0;
    emu68->breakpoints[id].count = 0;
    emu68->breakpoints[id].reset = 0;
  }
}

void emu68_bp_delall(emu68_t * const emu68)
{
  int id;
  for ( id=0; id<16; ++id ) {
    emu68_bp_del(emu68, id);
  }
}

void emu68_bp_del(emu68_t * const emu68, int id)
{
  if (emu68 && (unsigned int)id<16u) {
    if (emu68->chk && emu68->breakpoints[id].count ) {
      const addr68_t addr = emu68->breakpoints[id].addr & MEMMSK68;
      emu68->chk[addr] &= ~(EMU68_B|EMU68_M);
    }
    emu68->breakpoints[id].addr  = 0;
    emu68->breakpoints[id].count = 0;
    emu68->breakpoints[id].reset = 0;
  }
}

int emu68_bp_set(emu68_t * const emu68, int id,
                 addr68_t addr, uint68_t count, uint68_t reset)
{
  if (!emu68) {
    id = -1;
  } else {
    if ( id == -1 )
      for ( id=0; id<16 && emu68->breakpoints[id].count; ++id)
        ;
    if ( (unsigned int) id < 16u) {
      emu68->breakpoints[id].addr  = addr &= MEMMSK68;
      emu68->breakpoints[id].count = count;
      emu68->breakpoints[id].reset = reset;
      if (emu68->chk)
        emu68->chk[addr] = (emu68->chk[addr] & ~EMU68_M) | EMU68_B | (id<<4);
    } else {
      id = -1;
    }
  }
  return id;
}

int emu68_bp_find(emu68_t * const emu68, addr68_t addr)
{
  int id = -1;
  if (emu68) {
    int i;
    for ( i=0; i<16; ++i ) {
      if (emu68->breakpoints[i].count &&
          !( (emu68->breakpoints[i].addr ^ addr) & MEMMSK68 ) ) {
        id = i;
        break;
      }
    }
  }
  return id;
}


/* ,-----------------------------------------------------------------.
 * |                     Emulator init functions                     |
 * `-----------------------------------------------------------------'
 */

static emu68_parms_t def_parms;

emu68_t * emu68_create(emu68_parms_t * const parms)
{
  emu68_t * emu68 = 0;
  int memsize, membyte;
  emu68_parms_t * const p = parms ? parms : &def_parms;

  if (!p->log2mem) {
    p->log2mem = def_parms.log2mem;
  }
  if (p->log2mem < 16 || p->log2mem > 24) {
    emu68_error_add(emu68,
                    "Invalid requested amount of memory -- 2^%d", p->log2mem);
    goto error;
  }

  if (!p->clock) {
    p->clock = def_parms.clock;
  }
  if (p->clock<500000u || p->clock>60000000u) {
    emu68_error_add(emu68,
                    "Invalid clock frequency -- %u", p->clock);
    goto error;
  }

  memsize = 1 << p->log2mem;
  membyte = sizeof(emu68_t) + (memsize << !!p->debug);
  emu68   = emu68_alloc(membyte);
  if (!emu68) {
    goto error;
  }
  memset(emu68,0,membyte);
  strncpy(emu68->name,p->name?p->name:"emu68",sizeof(emu68->name)-1);
  emu68->clock = p->clock;

  emu68->log2mem = p->log2mem;
  emu68->memmsk  = memsize-1;
  if (p->debug) {
    emu68->chk = emu68->mem + memsize+8;
  }

  emu68_mem_init(emu68);
  emu68_reset(emu68);

error:
  return emu68;
}

emu68_t * emu68_duplicate(emu68_t * emu68src, const char * dupname)
{
  emu68_parms_t parms;
  emu68_t * emu68 = 0;

  if (!emu68src)
    goto error;

  /* Create an instance with the same parameters */
  parms.name    = dupname;
  parms.log2mem = emu68->log2mem;
  parms.clock   = emu68->clock;
  parms.debug   = !!emu68->chk;
  emu68 = emu68_create(&parms);
  if (!emu68)
    goto error;

  /* Copy registers and status */
  emu68->reg          = emu68src->reg;
  emu68->cycle        = emu68src->cycle;
  emu68->framechk     = emu68src->framechk;
  emu68->instructions = emu68src->instructions;
  emu68->finish_sp    = emu68src->finish_sp;
  emu68->status       = emu68src->status;

  /* Copy memory */
  memcpy(emu68->mem, emu68src->mem, 1<<emu68->log2mem);
  if (emu68->chk)
    memcpy(emu68->chk, emu68src->chk, 1<<emu68->log2mem);

  /* Copy breakpoints */
  memcpy(emu68->breakpoints, emu68src->breakpoints, sizeof(emu68->breakpoints));

error:
  return emu68;
}

/* Destroy emulator instance. */
void emu68_destroy(emu68_t * const emu68)
{
  if (emu68) {
    emu68_ioplug_destroy_all(emu68);
    emu68_free(emu68);
  }
}

/* 68000 Hardware Reset
 * - PC = 0
 * - SR = 2700
 * - A7 = end of mem - 4
 * - All registers cleared
 * - All IO reseted
 */
void emu68_reset(emu68_t * const emu68)
{
  if (emu68) {
    io68_t * io;
    for (io=emu68->iohead; io; io=io->next) {
      (io->reset)(io);
    }
    bp_reset(emu68);

    memset(REG68.d,0,sizeof(REG68.d)*2);
    REG68.pc     = 0;
    REG68.sr     = 0x2700;
    REG68.a[7]   = MEMMSK68+1-4;
    REG68.usp    = REG68.a[7];

    emu68->cycle        = 0;
    emu68->framechk     = 0;
    emu68->instructions = 0;
    emu68->finish_sp    = -1;
    emu68->status       = EMU68_NRM;
  }
}

/* ,-----------------------------------------------------------------.
 * |                    Library init functions                       |
 * `-----------------------------------------------------------------'
 */

/* Library initialize. */
int emu68_init(int * argc, char ** argv)
{
  def_parms.name    = 0;
  def_parms.log2mem = 19;            /* log2mem: 2^19 => 512 kB */
  def_parms.clock   = EMU68_ATARIST_CLOCK;
  def_parms.debug   = 0;

  /* $$$ TODO: parse argument */
  return 0;
}

/* Library shutdown. */
void emu68_shutdown(void)
{
}