summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/sc61860/scops.c
blob: 7ae3b5e12374bf121521acc793cbf4e7f7b88dc1 (plain) (blame)
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
/*****************************************************************************
 *
 *   scops.c
 *   portable sharp 61860 emulator interface
 *   (sharp pocket computers)
 *
 *   Copyright Peter Trauner, all rights reserved.
 *
 *   - This source code is released as freeware for non-commercial purposes.
 *   - You are free to use and redistribute this code in modified or
 *     unmodified form, provided you list me in the credits.
 *   - If you modify this source code, you must add a notice to each modified
 *     source file that it has been changed.  If you're a nice person, you
 *     will clearly mark each change too.  :)
 *   - If you wish to use this for commercial purposes, please contact me at
 *     peter.trauner@jk.uni-linz.ac.at
 *   - The author of this copywritten work reserves the right to change the
 *     terms of its usage and license at any time, including retroactively
 *   - This entire notice must remain in the source code.
 *
 * History of changes:
 * 21.07.2001 Several changes listed below were made by Mario Konegger
 *            (konegger@itp.tu-graz.ac.at)
 *        replaced buggy BCD-commands add_bcd, sub_bcd, add_bcd_a,
 *            sub_bcd_a and changed out_c, to implement HLT-mode of the CPU.
 *
 *****************************************************************************/

INLINE UINT8 READ_OP(void)
{
	return cpu_readop(sc61860.pc++);
}

INLINE UINT8 READ_OP_ARG(void)
{
	return cpu_readop_arg(sc61860.pc++);
}

INLINE UINT16 READ_OP_ARG_WORD(void)
{
	UINT16 t=cpu_readop(sc61860.pc++)<<8;
	t|=cpu_readop(sc61860.pc++);
	return t;
}

INLINE UINT8 READ_BYTE(UINT16 adr)
{
	return program_read_byte(adr);
}

INLINE void WRITE_BYTE(UINT16 a,UINT8 v)
{
	program_write_byte(a,v);
}

#define PUSH(v) sc61860.ram[--sc61860.r]=v
#define POP() sc61860.ram[sc61860.r++]

INLINE void sc61860_load_imm(int r, UINT8 v)
{
	sc61860.ram[r]=v;
}

INLINE void sc61860_load(void)
{
	sc61860.ram[A]=sc61860.ram[sc61860.p];
}

INLINE void sc61860_load_imm_p(UINT8 v)
{
	sc61860.p=v&0x7f;
}

INLINE void sc61860_load_imm_q(UINT8 v)
{
	sc61860.q=v&0x7f;
}

INLINE void sc61860_load_r(void)
{
	sc61860.r=sc61860.ram[A]&0x7f;
}

INLINE void sc61860_load_ext(int r)
{
	sc61860.ram[r]=READ_BYTE(sc61860.dp);
}

INLINE void sc61860_load_dp(void)
{
	sc61860.dp=READ_OP_ARG_WORD();
}

INLINE void sc61860_load_dl(void)
{
	sc61860.dp=(sc61860.dp&~0xff)|READ_OP_ARG();
}

INLINE void sc61860_store_p(void)
{
	sc61860.ram[A]=sc61860.p;
}

INLINE void sc61860_store_q(void)
{
	sc61860.ram[A]=sc61860.q;
}

INLINE void sc61860_store_r(void)
{
	sc61860.ram[A]=sc61860.r;
}

INLINE void sc61860_store_ext(int r)
{
	WRITE_BYTE(sc61860.dp, sc61860.ram[r]);
}

INLINE void sc61860_exam(int a, int b)
{
	UINT8 t=sc61860.ram[a];
	sc61860.ram[a]=sc61860.ram[b];
	sc61860.ram[b]=t;
}

INLINE void sc61860_test(int reg, UINT8 value)
{
	sc61860.zero=(sc61860.ram[reg]&value)==0;
}

INLINE void sc61860_test_ext(void)
{
	sc61860.zero=(READ_BYTE(sc61860.dp)&READ_OP_ARG())==0;
}

INLINE void sc61860_and(int reg, UINT8 value)
{
	sc61860.zero=(sc61860.ram[reg]&=value)==0;
}

INLINE void sc61860_and_ext(void)
{
	UINT8 t=READ_BYTE(sc61860.dp)&READ_OP_ARG();
	sc61860.zero=t==0;
    WRITE_BYTE(sc61860.dp,t);
}

INLINE void sc61860_or(int reg, UINT8 value)
{
	sc61860.zero=(sc61860.ram[reg]|=value)==0;
}

INLINE void sc61860_or_ext(void)
{
	UINT8 t=READ_BYTE(sc61860.dp)|READ_OP_ARG();
	sc61860.zero=t==0;
    WRITE_BYTE(sc61860.dp,t);
}

INLINE void sc61860_rotate_right(void)
{
	int t=sc61860.ram[A];
	if (sc61860.carry) t|=0x100;
	sc61860.carry=t&1;
	sc61860.ram[A]=t>>1;
}

INLINE void sc61860_rotate_left(void)
{
	int t=sc61860.ram[A]<<1;
	if (sc61860.carry) t|=1;
	sc61860.carry=t&0x100;
	sc61860.ram[A]=t;
}

INLINE void sc61860_swap(void)
{
	int t=sc61860.ram[A];
	sc61860.ram[A]=(t<<4)|((t>>4)&0xf);
}

// q=reg sideeffect
INLINE void sc61860_inc(int reg)
{
	sc61860.q=reg;
	sc61860.ram[reg]++;
	sc61860.zero=sc61860.carry=sc61860.ram[reg]==0;
}

INLINE void sc61860_inc_p(void)
{
	sc61860.p++;
}

// q=reg sideeffect
INLINE void sc61860_dec(int reg)
{
	sc61860.q=reg;
	sc61860.ram[reg]--;
	sc61860.zero=sc61860.ram[reg]==0;
	sc61860.carry=sc61860.ram[reg]==0xff;
}

INLINE void sc61860_dec_p(void)
{
	sc61860.p--;
}

INLINE void sc61860_add(int reg, UINT8 value)
{
	int t=sc61860.ram[reg]+value;
	sc61860.zero=(sc61860.ram[reg]=t)==0;
	sc61860.carry=t>=0x100;
}

INLINE void sc61860_add_carry(void)
{
	int t=sc61860.ram[sc61860.p]+sc61860.ram[A];
	if (sc61860.carry) t++;
	sc61860.zero=(sc61860.ram[sc61860.p]=t)==0;
	sc61860.carry=t>=0x100;
}

// p++ sideeffect
INLINE void sc61860_add_word(void)
{
	int t=sc61860.ram[sc61860.p]+sc61860.ram[A],t2;
	sc61860.ram[sc61860.p]=t;
	sc61860.p++;
	t2=sc61860.ram[sc61860.p]+sc61860.ram[B];
	if (t>=0x100) t2++;
	sc61860.ram[sc61860.p]=t2;
	sc61860.zero=(t2&0xff)==0 &&(t&0xff)==0;
	sc61860.carry=t2>=0x100;
}


INLINE void sc61860_sub(int reg, UINT8 value)
{
	int t=sc61860.ram[reg]-value;
	sc61860.zero=(sc61860.ram[reg]=t)==0;
	sc61860.carry=t<0;
}

INLINE void sc61860_sub_carry(void)
{
	int t=sc61860.ram[sc61860.p]-sc61860.ram[A];
	if (sc61860.carry) t--;
	sc61860.zero=(sc61860.ram[sc61860.p]=t)==0;
	sc61860.carry=t<0;
}


// p++ sideeffect
INLINE void sc61860_sub_word(void)
{
	int t=sc61860.ram[sc61860.p]-sc61860.ram[A],t2;
	sc61860.ram[sc61860.p]=t;
	sc61860.p++;
	t2=sc61860.ram[sc61860.p]-sc61860.ram[B];
	if (t<0) t2--;
	sc61860.ram[sc61860.p]=t2;
	sc61860.zero=(t2&0xff)==0 && (t&0xff)==0;
	sc61860.carry=t2<0;
}

INLINE void sc61860_cmp(int reg, UINT8 value)
{
	int t=sc61860.ram[reg]-value;
	sc61860.zero=t==0;
	sc61860.carry=t<0;
}

INLINE void sc61860_pop(void)
{
	sc61860.ram[A]=POP();
}

INLINE void sc61860_push(void)
{
	PUSH(sc61860.ram[A]);
}

INLINE void sc61860_prepare_table_call(void)
{
	int adr;
	sc61860.h=READ_OP();
	adr=READ_OP_ARG_WORD();
	PUSH(adr>>8);
	PUSH(adr&0xff);
}

INLINE void sc61860_execute_table_call(void)
{
	int i, v, adr;
	for (i=0; i<sc61860.h; i++) {
		v=READ_OP();
		adr=READ_OP_ARG_WORD();
		sc61860.zero=v==sc61860.ram[A];
		if (sc61860.zero) {
			sc61860.pc=adr;
			change_pc(sc61860.pc);
			return;
		}
	}
	sc61860.pc=READ_OP_ARG_WORD();
	change_pc(sc61860.pc);
}


INLINE void sc61860_call(UINT16 adr)
{
	PUSH(sc61860.pc>>8);
	PUSH(sc61860.pc&0xff);
	sc61860.pc=adr;
	change_pc(sc61860.pc);
}

INLINE void sc61860_return(void)
{
	UINT16 t=POP();
	t|=POP()<<8;
	sc61860.pc=t;
	change_pc(sc61860.pc);
}

INLINE void sc61860_jump(int yes)
{
	UINT16 adr=READ_OP_ARG_WORD();
	if (yes) {
		sc61860.pc=adr;
		change_pc(sc61860.pc);
	}
}

INLINE void sc61860_jump_rel_plus(int yes)
{
	UINT16 adr=sc61860.pc;
	adr+=READ_OP_ARG();
	if (yes) {
		sc61860.pc=adr;
		change_pc(sc61860.pc);
		sc61860_ICount-=3;
	}
}

INLINE void sc61860_jump_rel_minus(int yes)
{
	UINT16 adr=sc61860.pc;
	adr-=READ_OP_ARG();
	if (yes) {
		sc61860.pc=adr;
		change_pc(sc61860.pc);
		sc61860_ICount-=3;
	}
}

INLINE void sc61860_loop(void)
{
	UINT16 adr=sc61860.pc;
	adr-=READ_OP_ARG();
	sc61860.ram[sc61860.r]--;
	sc61860.zero=sc61860.ram[sc61860.r]==0;
	sc61860.carry=sc61860.ram[sc61860.r]==0xff;
	if (!sc61860.carry) {
		sc61860.pc=adr;
		adr=POP();
		change_pc(sc61860.pc);
		sc61860_ICount-=3;
	}
}

INLINE void sc61860_leave(void)
{
	sc61860.ram[sc61860.r]=0;
}

INLINE void sc61860_wait(void)
{
	int t=READ_OP();
	sc61860_ICount-=t;
	sc61860_ICount-=t;
	sc61860_ICount-=3;
}

INLINE void sc61860_set_carry(void)
{
	sc61860.carry=1;
	sc61860.zero=1;
}

INLINE void sc61860_reset_carry(void)
{
	sc61860.carry=0;
	sc61860.zero=1;
}

INLINE void sc61860_out_a(void)
{
	sc61860.q=IA;
	if (sc61860.config&&sc61860.config->outa)
	    sc61860.config->outa(sc61860.ram[IA]);
}

INLINE void sc61860_out_b(void)
{
	sc61860.q=IB;
	if (sc61860.config&&sc61860.config->outb)
	    sc61860.config->outb(sc61860.ram[IB]);
}

INLINE void sc61860_out_f(void)
{
	sc61860.q=F0;
	/*sc61860.ram[F0]; */
}


/*   c0 display on
   c1 counter reset
   c2 cpu halt
   c3 computer off
   c4 beeper frequency (1 4khz, 0 2khz), or (c5=0) membran pos1/pos2
   c5 beeper on
   c6 beeper steuerung*/
INLINE void sc61860_out_c(void)
{
    sc61860.q=C;
    if (sc61860.config&&sc61860.config->outc)
	sc61860.config->outc(sc61860.ram[C]);
    sc61860.c=sc61860.ram[C];
}

INLINE void sc61860_in_a(void)
{
	int data=0;
	if (sc61860.config&&sc61860.config->ina) data=sc61860.config->ina();
	sc61860.ram[A]=data;
	sc61860.zero=data==0;
}

INLINE void sc61860_in_b(void)
{
	int data=0;
	if (sc61860.config&&sc61860.config->inb) data=sc61860.config->inb();
	sc61860.ram[A]=data;
	sc61860.zero=data==0;
}

/* 0 systemclock 512ms
   1 systemclock 2ms
   2 ?
   3 brk/on key
   4 ?
   5 ?
   6 reset
   7 cassette input */
INLINE void sc61860_test_special(void)
{
	int t=0;
	if (sc61860.timer.t512ms) t|=1;
	if (sc61860.timer.t2ms) t|=2;
	if (sc61860.config&&sc61860.config->brk&&sc61860.config->brk()) t|=8;
	if (sc61860.config&&sc61860.config->reset&&sc61860.config->reset()) t|=0x40;
	if (sc61860.config&&sc61860.config->x&&sc61860.config->x()) t|=0x80;

	sc61860.zero=(t&READ_OP())==0;
}

/************************************************************************************
 "string" operations
***********************************************************************************/

// p-=I+1 sideeffect
INLINE void sc61860_add_bcd_a(void)
{
 UINT8 help = sc61860.ram[A];
 int i, hlp, hlp1 = 0; sc61860.zero=1;
 for ( i=0; i <= sc61860.ram[I]; i++)
 {
  hlp1 = (sc61860.ram[sc61860.p] & 0x0f) + (help & 0x0f ) + hlp1;
  if (hlp1 > 9) { hlp = hlp1 - 0x0a; hlp1 = 0x10; }
  else {hlp = hlp1; hlp1 = 0x00;}
  hlp1 = (sc61860.ram[sc61860.p] & 0xf0) + (help & 0xf0) + hlp1;
  if (hlp1 > 0x90) { sc61860.ram[sc61860.p] = hlp1 - 0xa0 + hlp; hlp1 = 1; }
  else {sc61860.ram[sc61860.p] = hlp1 + hlp; hlp1 = 0;}
  if ( sc61860.ram[sc61860.p--] != 0 ) sc61860.zero = 0;
  help = 0;
 }
 sc61860.carry= ( hlp1 ) ? 1 : 0;
 sc61860_ICount-=3*(sc61860.ram[I]+1);
}


// p-=I+1, q-=I+2 sideeffect
INLINE void sc61860_add_bcd(void)
{
 int i, hlp, hlp1 = 0; sc61860.zero=1;
 for ( i=0; i <= sc61860.ram[I]; i++)
 {
  hlp1 = (sc61860.ram[sc61860.p] & 0x0f) + (sc61860.ram[sc61860.q] & 0x0f ) +
hlp1;
  if (hlp1 > 9) { hlp = hlp1 - 0x0a; hlp1 = 0x10; }
  else {hlp = hlp1; hlp1 = 0x00;}
  hlp1 = (sc61860.ram[sc61860.p] & 0xf0) + (sc61860.ram[sc61860.q--] & 0xf0) +
hlp1;
  if (hlp1 > 0x90) { sc61860.ram[sc61860.p] = hlp1 - 0xa0 + hlp; hlp1 = 1; }
  else {sc61860.ram[sc61860.p] = hlp1 + hlp; hlp1 = 0;}
  if ( sc61860.ram[sc61860.p--] != 0 ) sc61860.zero = 0;
 }
 sc61860.carry= ( hlp1 ) ? 1 : 0;
 sc61860_ICount-=3*(sc61860.ram[I]+1);
 sc61860.q--;
}


// p-=I+1 sideeffect
INLINE void sc61860_sub_bcd_a(void)
{
 UINT8 help = sc61860.ram[A];
 int i, hlp, hlp1 = 0; sc61860.zero=1;
 for ( i=0; i <= sc61860.ram[I]; i++)
 {
  hlp1 = (sc61860.ram[sc61860.p]&0x0f) - (help&0x0f) - hlp1;
  if ( hlp1 < 0 ) { hlp = hlp1 + 0x0a; hlp1 = 0x10;}
  else { hlp = hlp1; hlp1 = 0x00;}
  hlp1 = (sc61860.ram[sc61860.p]&0xf0) - (help&0xf0) - hlp1;
  if ( hlp1 < 0 ) { sc61860.ram[sc61860.p] = hlp1 + 0xa0 + hlp; hlp1 = 1;}
  else {sc61860.ram[sc61860.p] = hlp1 + hlp; hlp1 = 0;}
  if ( sc61860.ram[sc61860.p--] != 0 ) sc61860.zero = 0;
  help = 0;
 }
 sc61860.carry= ( hlp1 ) ? 1 : 0;
 sc61860_ICount-=3*(sc61860.ram[I]+1);
}


// p-=I+1, q-=I+2 sideeffect
INLINE void sc61860_sub_bcd(void)
{
 int i, hlp, hlp1 = 0; sc61860.zero=1;
 for ( i=0; i <= sc61860.ram[I]; i++)
 {
  hlp1 = (sc61860.ram[sc61860.p]&0x0f) - (sc61860.ram[sc61860.q]&0x0f) - hlp1;
  if ( hlp1 < 0 ) { hlp = hlp1 + 0x0a; hlp1 = 0x10;}
  else { hlp = hlp1; hlp1 = 0x00;}
  hlp1 = (sc61860.ram[sc61860.p]&0xf0) - (sc61860.ram[sc61860.q--]&0xf0) -
hlp1;
  if ( hlp1 < 0 ) { sc61860.ram[sc61860.p] = hlp1 + 0xa0 + hlp; hlp1 = 1;}
  else {sc61860.ram[sc61860.p] = hlp1 + hlp; hlp1 = 0;}
  if ( sc61860.ram[sc61860.p--] != 0 ) sc61860.zero = 0;
 }
 sc61860.carry= ( hlp1 ) ? 1 : 0;
 sc61860_ICount-=3*(sc61860.ram[I]+1);
 sc61860.q--;
}

/* side effect p-i-1 -> p correct! */
INLINE void sc61860_shift_left_nibble(void)
{
	int i,t=0;
	for (i=0; i<=sc61860.ram[I]; i++) {
		t|=sc61860.ram[sc61860.p]<<4;
		sc61860.ram[sc61860.p--]=t;
		t>>=8;
		sc61860_ICount--;
	}
}

/* side effect p+i+1 -> p correct! */
INLINE void sc61860_shift_right_nibble(void)
{
	int i,t=0;
	for (i=0; i<=sc61860.ram[I]; i++) {
		t|=sc61860.ram[sc61860.p];
		sc61860.ram[sc61860.p++]=t>>4;
		t=(t<<8)&0xf00;
		sc61860_ICount--;
	}
}

// q=reg+1 sideeffect
INLINE void sc61860_inc_load_dp(int reg)
{
    if (++sc61860.ram[reg]==0) sc61860.ram[reg+1]++;
    sc61860.dp=sc61860.ram[reg]|(sc61860.ram[reg+1]<<8);
    sc61860.q=reg+1;
}

// q=reg+1 sideeffect
INLINE void sc61860_dec_load_dp(int reg)
{
    if (--sc61860.ram[reg]==0xff) sc61860.ram[reg+1]--;
    sc61860.dp=sc61860.ram[reg]|(sc61860.ram[reg+1]<<8);
    sc61860.q=reg+1;
}

// q=XH sideeffect
INLINE void sc61860_inc_load_dp_load(void)
{
    if (++sc61860.ram[XL]==0) sc61860.ram[XH]++;
    sc61860.dp=sc61860.ram[XL]|(sc61860.ram[XH]<<8);
    sc61860.q=XH; // hopefully correct before real read
    sc61860.ram[A]=READ_BYTE(sc61860.dp);
}

// q=XH sideeffect
INLINE void sc61860_dec_load_dp_load(void)
{
    if (--sc61860.ram[XL]==0xff) sc61860.ram[XH]--;
    sc61860.dp=sc61860.ram[XL]|(sc61860.ram[XH]<<8);
    sc61860.q=XH; // hopefully correct before real read
    sc61860.ram[A]=READ_BYTE(sc61860.dp);
}

// q=YH sideeffect
INLINE void sc61860_inc_load_dp_store(void)
{
    if (++sc61860.ram[YL]==0) sc61860.ram[YH]++;
    sc61860.dp=sc61860.ram[YL]|(sc61860.ram[YH]<<8);
    sc61860.q=YH; // hopefully correct before real write!
    WRITE_BYTE(sc61860.dp,sc61860.ram[A]);
}

// q=YH sideeffect
INLINE void sc61860_dec_load_dp_store(void)
{
    if (--sc61860.ram[YL]==0xff) sc61860.ram[YH]--;
    sc61860.dp=sc61860.ram[YL]|(sc61860.ram[YH]<<8);
    sc61860.q=XH; // hopefully correct before real write!
    WRITE_BYTE(sc61860.dp,sc61860.ram[A]);
}

INLINE void sc61860_fill(void)
{
	int i;
	for (i=0;i<=sc61860.ram[I];i++) {
		sc61860.ram[sc61860.p++]=sc61860.ram[A]; /* could be overwritten? */
		sc61860_ICount--;
	}
}

INLINE void sc61860_fill_ext(void)
{
	int i;
	for (i=0;i<=sc61860.ram[I];i++) {
		WRITE_BYTE(sc61860.dp, sc61860.ram[A]);
		if (i!=sc61860.ram[I]) sc61860.dp++;
		sc61860_ICount-=3;
	}
}

// p+=count+1, q+=count+1 sideeffects
INLINE void sc61860_copy(int count)
{
	int i;
	for (i=0; i<=count; i++) {
		sc61860.ram[sc61860.p++]=sc61860.ram[sc61860.q++];
		sc61860_ICount-=2;
	}

}

// p+=count+1, dp+=count sideeffects
INLINE void sc61860_copy_ext(int count)
{
	int i;
	for (i=0; i<=count; i++) {
		sc61860.ram[sc61860.p++]=READ_BYTE(sc61860.dp);
		if (i!=count) sc61860.dp++;
		sc61860_ICount-=4;
	}
}

INLINE void sc61860_copy_int(int count)
{
	int i;
	for (i=0; i<=count; i++) {
		sc61860.ram[sc61860.p++]=
			READ_BYTE((sc61860.ram[A]|(sc61860.ram[B]<<8)) ); /* internal rom! */
		if (i!=count) {
			if (++sc61860.ram[A]==0) sc61860.ram[B]++;
		}
		sc61860_ICount-=4;
	}
}

INLINE void sc61860_exchange(int count)
{
	int i;
	UINT8 t;
	for (i=0; i<=count; i++) {
		t=sc61860.ram[sc61860.p];
		sc61860.ram[sc61860.p++]=sc61860.ram[sc61860.q];
		sc61860.ram[sc61860.q++]=t;
		sc61860_ICount-=3;
	}
}

INLINE void sc61860_exchange_ext(int count)
{
	int i;
	UINT8 t;
	for (i=0; i<=count; i++) {
		t=sc61860.ram[sc61860.p];
		sc61860.ram[sc61860.p++]=READ_BYTE(sc61860.dp);
		WRITE_BYTE(sc61860.dp, t);
		if (i!=count) sc61860.dp++;
		sc61860_ICount-=6;
	}
}

// undocumented
// only 1 opcode working in pc1403
// both opcodes working in pc1350
INLINE void sc61860_wait_x(int level)
{
    int c;
    sc61860.zero=level;

    if (sc61860.config&&sc61860.config->x) {
	for (c=sc61860.ram[I]; c>=0; c--) {
//      sc61860.ram[sc61860.p]=(sc61860.ram[sc61860.p]+1)%0x60;
	    sc61860.ram[sc61860.p]=(sc61860.ram[sc61860.p]+1)&0x7f;
	    sc61860.zero=sc61860.config->x();
	    sc61860_ICount-=4;
	    if ( level != sc61860.zero) break;
	}
    }
}