-
Notifications
You must be signed in to change notification settings - Fork 0
/
ova.c
703 lines (591 loc) · 13.5 KB
/
ova.c
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
/* ova.c
*
* Not an egg -- overflow arithmetic. 16 and 32 bit.
*
* Designed to work with both gcc and hi-tech C. This is then meant
* to allow operation of AM9511 emulation with hi-tech C natively
* on z80 platform. There are faster ways, but we want to be able
* to validate the code against a real AM9511A, as long as some of
* those chips are still operational.
*
* Because this code is meant to be (potentially) used with
* hi-tech C, and other very old environments, external names are
* unique to 5 characters (6 with prepended '_').
*/
#include <stdio.h>
#include "ova.h"
#include "types.h"
#define USE_MUL16
/* Constant 1, good for 16, 32 and 64 bit
*/
static unsigned char one[] = { 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };
/* Return overflow flag after add16().
*
* Overflow is detected by sign bits of the arguments and result.
* This can be done after the add (or subtract) is done. That is
* why add and subtract return carry, and leave overflow to a
* separate function.
*/
int oadd16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char sa, sb, sc;
int overflow = 0;
sa = pa[1] & 0x80;
sb = pb[1] & 0x80;
sc = pc[1] & 0x80;
if ((sa == 0x00) && (sb == 0x00) && (sc == 0x80))
overflow = 1;
if ((sa == 0x80) && (sb == 0x80) && (sc == 0x00))
overflow = 1;
return overflow;
}
/* Return overflow flag after add32().
*/
int oadd32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char sa, sb, sc;
int overflow = 0;
sa = pa[3] & 0x80;
sb = pb[3] & 0x80;
sc = pc[3] & 0x80;
if ((sa == 0x00) && (sb == 0x00) && (sc == 0x80))
overflow = 1;
if ((sa == 0x80) && (sb == 0x80) && (sc == 0x00))
overflow = 1;
return overflow;
}
/* Return overflow flag after sub16().
*/
int osub16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char sa, sb, sc;
int overflow = 0;
sa = pa[1] & 0x80;
sb = pb[1] & 0x80;
sc = pc[1] & 0x80;
if ((sa == 0x00) && (sb == 0x80) && (sc == 0x80))
overflow = 1;
if ((sa == 0x80) && (sb == 0x00) && (sc == 0x00))
overflow = 1;
return overflow;
}
/* Return overflow after sub32().
*/
int osub32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char sa, sb, sc;
int overflow = 0;
sa = pa[3] & 0x80;
sb = pb[3] & 0x80;
sc = pc[3] & 0x80;
if ((sa == 0x00) && (sb == 0x80) && (sc == 0x80))
overflow = 1;
if ((sa == 0x80) && (sb == 0x00) && (sc == 0x00))
overflow = 1;
return overflow;
}
/* 16 bit add, returns carry.
*
* All parameters are little endian.
*/
int add16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
uint16 a, b, c;
int carry;
a = pa[0] | (pa[1] << 8);
b = pb[0] | (pb[1] << 8);
c = a + b;
carry = (c < a) || (c < b);
pc[0] = c;
pc[1] = c >> 8;
return carry;
}
/* 32 bit add, returns carry. Uses add16().
*/
int add32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char *pah, *pbh, *pch;
int carry, c2;
pah = pa + 2;
pbh = pb + 2;
pch = pc + 2;
carry = add16(pa, pb, pc);
if (!carry) {
carry = add16(pah, pbh, pch);
return carry;
}
carry = add16(one, pbh, pch);
c2 = add16(pch, pah, pch);
return carry || c2;
}
/* 64 bit add, returns carry. Uses add32().
*
* This supports 32x32->64 bit multiply.
*/
static int add64(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char *pah, *pbh, *pch;
int carry, c2;
pah = pa + 4;
pbh = pb + 4;
pch = pc + 4;
carry = add32(pa, pb, pc);
if (!carry) {
carry = add32(pah, pbh, pch);
return carry;
}
carry = add32(one, pbh, pch);
c2 = add32(pch, pah, pch);
return carry || c2;
}
/* 16 bit 2's complement, return 1 if 0x8000
*/
int cm16(unsigned char *pa,
unsigned char *pb) {
uint16 a, b;
int r = 0;
a = pa[0] | (pa[1] << 8);
if (a == 0x8000) {
b = a;
r = 1;
} else {
b = ~a;
++b;
}
pb[0] = b;
pb[1] = b >> 8;
return r;
}
/* 32 bit 2's complement, return 1 if 0x80000000
*/
int cm32(unsigned char *pa,
unsigned char *pb) {
uint16 a, ah, b, bh;
int r = 0;
a = pa[0] | (pa[1] << 8);
ah = pa[2] | (pa[3] << 8);
if ((a == 0x0000) && (ah == 0x8000)) {
b = a;
bh = ah;
r = 1;
} else {
b = ~a;
bh = ~ah;
if (++b == 0)
++bh;
}
pb[0] = b;
pb[1] = b >> 8;
pb[2] = bh;
pb[3] = bh >> 8;
return r;
}
/* 64 bit 2's complement, return 1 if 0x8000 0000 0000 0000
*/
static int cm64(unsigned char *pa,
unsigned char *pb) {
if (pa[7] == 0x80)
if ((pa[0] | pa[1] | pa[2] | pa[3] |
pa[4] | pa[5] | pa[6]) == 0) {
pb[0] = pb[1] = pb[2] = pb[3] = pb[4] = pb[5] = pb[6] = 0;
pb[7] = 0x80;
return 1;
}
pb[0] = ~pa[0];
pb[1] = ~pa[1];
pb[2] = ~pa[2];
pb[3] = ~pa[3];
pb[4] = ~pa[4];
pb[5] = ~pa[5];
pb[6] = ~pa[6];
pb[7] = ~pa[7];
add64(pb, one, pb);
return 0;
}
/* 16 bit subtract. Return 1 if carry.
*/
int sub16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
uint16 a, b, c;
int carry;
a = pa[0] | (pa[1] << 8);
b = pb[0] | (pb[1] << 8);
c = a - b;
if (a == 0x8000)
carry = 1;
else
carry = a < b;
pc[0] = c;
pc[1] = c >> 8;
return carry;
}
/* 32 subtract. Return carry.
*/
int sub32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char *pah, *pbh, *pch;
int carry, c2;
pah = pa + 2;
pbh = pb + 2;
pch = pc + 2;
carry = sub16(pa, pb, pc);
if (!carry) {
carry = sub16(pah, pbh, pch);
return carry;
}
carry = sub16(pah, one, pch);
c2 = sub16(pch, pbh, pch);
return carry || c2;
}
/* 16x16 giving 32 bit multiplication
*
* We break it down into 8x8 giving 16 bit
*
* [mHigh mLow]
* * [nHigh nLow]
* ------------
* [mHigh * nLow] [mLow * nLow]
* + [mHigh * nHigh] [mLow * nHigh]
* --------------------------------------------
*/
#ifndef USE_MUL16
/* Multiply 8x8->16 bit result. Hopefully, we have hardware
* which does this. The Z80 does not. But most of its successors
* do, and most gcc platforms do.
*/
static uint16 mul8(unsigned char m, unsigned char n) {
return m * n;
}
#endif
/* Multiply 16x16->32
*
* r may be one or both of the operands
*/
static void mul16(unsigned char *m, unsigned char *n, unsigned char *r) {
#ifdef USE_MUL16
/* If we have 16x16->32 multiply, use it.
*/
uint32 a, b, c;
a = m[0] + (m[1] << 8);
b = n[0] + (n[1] << 8);
c = a * b;
r[0] = c & 0xff;
r[1] = (c >> 8) & 0xff;
r[2] = (c >> 16) & 0xff;
r[3] = (c >> 24) & 0xff;
#else
/* Build 16x16->32 from 8x8->16 bit multiply
*/
unsigned char mLow = m[0];
unsigned char mHigh = m[1];
unsigned char nLow = n[0];
unsigned char nHigh = n[1];
uint16 mLow_nLow = mul8(mLow, nLow);
uint16 mHigh_nLow = mul8(mHigh, nLow);
uint16 mLow_nHigh = mul8(mLow, nHigh);
uint16 mHigh_nHigh = mul8(mHigh, nHigh);
uint16 a;
int carry;
unsigned char r2[4], r3[4], r4[4];
int i;
for (i = 0; i < 4; ++i)
r2[i] = r3[i] = r[4] = 0;
/* r2:
*
* 3
* 2
* 1 | mLow_nLow
* 0 |
*/
r2[0] = mLow_nLow & 0xff;
r2[1] = (mLow_nLow >> 8) & 0xff;
/* r3:
*
* 3
* 2 | mHigh_nLow + mLow_nHigh
* 1 |
* 0
*/
carry = add16((unsigned char *)&mHigh_nLow,
(unsigned char *)&mLow_nHigh,
r3 + 1);
/* r4:
*
* 3 | mHigh_nHigh (+ carry)
* 2 |
* 1
* 0
*/
a = mHigh_nHigh + carry;
r4[2] = a & 0xff;
r4[3] = (a >> 8) & 0xff;
/* r = r2 + r3
*/
add32(r2, r3, r);
/* r = r + r4 */
add32(r, r4, r);
#endif
}
/* 16 bit multiply, lower. Uses mul16(). Returns overflow.
* (1 if high 16 is non-zero).
*/
int mull16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char r[4];
unsigned char a[2], b[2];
int s, o;
if (((pa[0] == 0x00) && (pa[1] == 0x80)) ||
((pb[0] == 0x00) && (pb[1] == 0x80))) {
pc[0] = 0x00;
pc[1] = 0x80;
return 1;
}
/* We know that neither operand is 0x8000, so change sign will
* work.
*/
s = 0;
if (pa[1] & 0x80) {
s ^= 1;
cm16(pa, a);
} else {
a[0] = pa[0];
a[1] = pa[1];
}
if (pb[1] & 0x80) {
s ^= 1;
cm16(pb, b);
} else {
b[0] = pb[0];
b[1] = pb[1];
}
mul16(a, b, r);
o = (r[2] | r[3]) != 0;
if (s)
o |= cm32(r, r);
pc[0] = r[0];
pc[1] = r[1];
return o;
}
/* 16 bit multiply, upper. Uses mul16(). returns overflow.
*/
int mulu16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char r[4];
unsigned char a[2], b[2];
int s, o;
if (((pa[0] == 0x00) && (pa[1] == 0x80)) ||
((pb[0] == 0x00) && (pb[1] == 0x80))) {
pc[0] = 0x00;
pc[1] = 0x80;
return 1;
}
s = 0;
if (pa[1] & 0x80) {
s ^= 1;
cm16(pa, a);
} else {
a[0] = pa[0];
a[1] = pa[1];
}
if (pb[1] & 0x80) {
s ^= 1;
cm16(pb, b);
} else {
b[0] = pb[0];
b[1] = pb[1];
}
mul16(a, b, r);
o = 0;
if (s)
o = cm32(r, r);
pc[0] = r[2];
pc[1] = r[3];
return o;
}
/* 16 bit division.
*
* Eventually, I will put my own code in here... but, the objective
* is to have the emulator running.
*/
int div16(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
int16 a, b, c;
int r = 0;
a = pa[0] |
(pa[1] << 8);
b = pb[0] |
(pb[1] << 8);
if (b == 0) {
c = b;
r = 1;
} else
c = a / b;
pc[0] = c & 0xff;
pc[1] = (c >> 8) & 0xff;
return r;
}
/* 32 bit division.
*
* Eventually, I will put my own code in here... but, the objective
* is to have the emulator running.
*/
int div32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
int32 a, b, c;
int r = 0;
a = pa[0] |
(pa[1] << 8) |
(pa[2] << 16) |
(pa[3] << 24);
b = pb[0] |
(pb[1] << 8) |
(pb[2] << 16) |
(pb[3] << 24);
if (b == 0) {
c = b;
r = 1;
} else
c = a / b;
pc[0] = c & 0xff;
pc[1] = (c >> 8) & 0xff;
pc[2] = (c >> 16) & 0xff;
pc[3] = (c >> 24) & 0xff;
return r;
}
/* 32x32->64 multiply
*/
static void mul32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char *pah, *pal;
unsigned char *pbh, *pbl;
unsigned char r0[8];
unsigned char r1[8];
unsigned char r2[8];
unsigned char r3[8];
int i;
for (i = 0; i < 8; ++i)
pc[i] = r0[i] = r1[i] = r2[i] = r3[i] = 0;
pal = pa;
pah = pa + 2;
pbl = pb;
pbh = pb + 2;
mul16(pbl, pal, r0);
mul16(pbl, pah, r1 + 2);
mul16(pbh, pal, r2 + 2);
mul16(pbh, pah, r3 + 4);
add64(r0, r1, pc);
add64(pc, r2, r0);
add64(r0, r3, pc);
}
int mull32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char r[8];
unsigned char a[4], b[4];
int s, o;
if (((pa[0]==0x00) && (pa[1]==0x00) && (pa[2]==0x00) && (pa[3]==0x80)) ||
((pb[0]==0x00) && (pb[1]==0x00) && (pa[2]==0x00) && (pa[3]==0x80))) {
pc[0] = 0x00;
pc[1] = 0x00;
pc[2] = 0x00;
pc[3] = 0x80;
return 1;
}
s = 0;
if (pa[3] & 0x80) {
s ^= 1;
cm32(pa, a);
} else {
a[0] = pa[0];
a[1] = pa[1];
a[2] = pa[2];
a[3] = pa[3];
}
if (pb[3] & 0x80) {
s ^= 1;
cm32(pb, b);
} else {
b[0] = pb[0];
b[1] = pb[1];
b[2] = pb[2];
b[3] = pb[3];
}
mul32(a, b, r);
o = (r[4] | r[5] | r[6] | r[7]) != 0;
if (s)
o |= cm64(r, r);
pc[0] = r[0];
pc[1] = r[1];
pc[2] = r[2];
pc[3] = r[3];
return o;
}
int mulu32(unsigned char *pa,
unsigned char *pb,
unsigned char *pc) {
unsigned char r[8];
unsigned char a[4], b[4];
int s, o;
if (((pa[0]==0x00) && (pa[1]==0x00) && (pa[2]==0x00) && (pa[3]==0x80)) ||
((pb[0]==0x00) && (pb[1]==0x00) && (pa[2]==0x00) && (pa[3]==0x80))) {
pc[0] = 0x00;
pc[1] = 0x00;
pc[2] = 0x00;
pc[3] = 0x80;
return 1;
}
s = 0;
if (pa[3] & 0x80) {
s ^= 1;
cm32(pa, a);
} else {
a[0] = pa[0];
a[1] = pa[1];
a[2] = pa[2];
a[3] = pa[3];
}
if (pb[3] & 0x80) {
s ^= 1;
cm32(pb, b);
} else {
b[0] = pb[0];
b[1] = pb[1];
b[2] = pb[2];
b[3] = pb[3];
}
mul32(a, b, r);
o = 0;
if (s)
o = cm64(r, r);
pc[0] = r[4];
pc[1] = r[5];
pc[2] = r[6];
pc[3] = r[7];
return o;
}
/*
16B,16B (32-bit multiplicand)
* 16B,16B (32-bit multiplier)
-----------------
16B,16B (32-bit partial product)
+ 16B,16B (32-bit partial product)
+ 16B,16B (32-bit partial product)
+ 16B,16B (32-bit partial product)
=================
16B,16B,16B,16B (64-bit product)
*/