-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathiocore.c
641 lines (518 loc) · 11.1 KB
/
iocore.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
#include "compiler.h"
#include "cpucore.h"
#include "pccore.h"
#include "iocore.h"
#include "cbuscore.h"
#include "sound.h"
#include "fmboard.h"
#include "ideio.h"
#include "cs4231io.h"
#include "iocore16.tbl"
_ARTIC artic;
_CGROM cgrom;
_CGWINDOW cgwindow;
_CRTC crtc;
_DMAC dmac;
_EGC egc;
_EPSONIO epsonio;
_EMSIO emsio;
_FDC fdc;
_GDC gdc;
_GDCS gdcs;
_GRCG grcg;
_KEYBRD keybrd;
_MOUSEIF mouseif;
_NECIO necio;
_NMIIO nmiio;
_NP2SYSP np2sysp;
_PIC pic;
_PIT pit;
_RS232C rs232c;
_SYSPORT sysport;
_UPD4990 uPD4990;
#if defined(SUPPORT_PC9821)
_PCIDEV pcidev;
#endif
// ----
enum {
IOFUNC_SYS = 0x01,
IOFUNC_SND = 0x02,
IOFUNC_EXT = 0x04
};
typedef struct {
IOOUT ioout[256];
IOINP ioinp[256];
UINT type;
UINT port;
} _IOFUNC, *IOFUNC;
typedef struct {
IOFUNC base[256];
UINT busclock;
LISTARRAY iotbl;
} _IOCORE, *IOCORE;
static _IOCORE iocore;
static UINT8 ioterminate[0x100];
// ----
static void IOOUTCALL defout8(UINT port, REG8 dat) {
#if !defined(DISABLE_SOUND)
UINT tmp;
tmp = port - cs4231.port[0];
if (tmp < 8) {
cs4231io0_w8(port, dat);
return;
}
tmp = port - cs4231.port[5];
if (tmp < 2) {
cs4231io5_w8(port, dat);
return;
}
#endif
if ((port & 0xf0ff) == 0x801e) {
dipsw_w8(port, dat);
return;
}
// TRACEOUT(("defout8 - %x %x %.4x %.4x", port, dat, CPU_CS, CPU_IP));
}
static REG8 IOINPCALL definp8(UINT port) {
#if !defined(DISABLE_SOUND)
UINT tmp;
tmp = port - cs4231.port[0];
if (tmp < 8) {
return(cs4231io0_r8(port));
}
tmp = port - cs4231.port[5];
if (tmp < 2) {
return(cs4231io5_r8(port));
}
#endif
if ((port & 0xf0ff) == 0x801e) {
return(dipsw_r8(port));
}
// TRACEOUT(("definp8 - %x %.4x %.4x", port, CPU_CS, CPU_IP));
return(0xff);
}
static void attachout(IOFUNC iof, UINT port, IOOUT func) {
if (func) {
iof->ioout[port] = func;
}
}
static void attachinp(IOFUNC iof, UINT port, IOINP func) {
if (func) {
iof->ioinp[port] = func;
}
}
// ---- out
typedef struct {
UINT port;
UINT mask;
const IOOUT *func;
UINT funcs;
} _ATTOUT, *ATTOUT;
static void attachoutex(IOFUNC iof, ATTOUT attout) {
UINT port;
UINT mask;
UINT num;
UINT i;
port = attout->port & 0xff;
mask = attout->mask & 0xff;
num = 0;
for (i=0; i<0x100; i++) {
if ((i & mask) == port) {
attachout(iof, i, attout->func[num]);
num = (num + 1) & (attout->funcs - 1);
}
}
}
static BOOL attachcmnout(void *iotbl, void *attout) {
attachoutex((IOFUNC)iotbl, (ATTOUT)attout);
return(FALSE);
}
void iocore_attachcmnoutex(UINT port, UINT mask,
const IOOUT *func, UINT funcs) {
_ATTOUT ao;
ao.port = port;
ao.mask = mask;
ao.func = func;
ao.funcs = funcs;
listarray_enum(iocore.iotbl, attachcmnout, &ao);
}
static BOOL attachsysout(void *iotbl, void *attout) {
if ((((IOFUNC)iotbl)->type) & IOFUNC_SYS) {
attachoutex((IOFUNC)iotbl, (ATTOUT)attout);
}
return(FALSE);
}
void iocore_attachsysoutex(UINT port, UINT mask,
const IOOUT *func, UINT funcs) {
_ATTOUT ao;
ao.port = port;
ao.mask = mask;
ao.func = func;
ao.funcs = funcs;
listarray_enum(iocore.iotbl, attachsysout, &ao);
}
// ---- inp
typedef struct {
UINT port;
UINT mask;
const IOINP *func;
UINT funcs;
} _ATTINP, *ATTINP;
static void attachinpex(IOFUNC iof, ATTINP attinp) {
UINT port;
UINT mask;
UINT num;
UINT i;
port = attinp->port & 0xff;
mask = attinp->mask & 0xff;
num = 0;
for (i=0; i<0x100; i++) {
if ((i & mask) == port) {
attachinp(iof, i, attinp->func[num]);
num = (num + 1) & (attinp->funcs - 1);
}
}
}
static BOOL attachcmninp(void *iotbl, void *attinp) {
attachinpex((IOFUNC)iotbl, (ATTINP)attinp);
return(FALSE);
}
void iocore_attachcmninpex(UINT port, UINT mask,
const IOINP *func, UINT funcs) {
_ATTINP ai;
ai.port = port;
ai.mask = mask;
ai.func = func;
ai.funcs = funcs;
listarray_enum(iocore.iotbl, attachcmninp, &ai);
}
static BOOL attachsysinp(void *iotbl, void *attinp) {
if ((((IOFUNC)iotbl)->type) & IOFUNC_SYS) {
attachinpex((IOFUNC)iotbl, (ATTINP)attinp);
}
return(FALSE);
}
void iocore_attachsysinpex(UINT port, UINT mask,
const IOINP *func, UINT funcs) {
_ATTINP ai;
ai.port = port;
ai.mask = mask;
ai.func = func;
ai.funcs = funcs;
listarray_enum(iocore.iotbl, attachsysinp, &ai);
}
// ----
static BRESULT makesndiofunc(UINT port) {
IOFUNC tbl[2];
UINT num;
IOFUNC iof;
UINT type;
IOFUNC set;
ZeroMemory(tbl, sizeof(tbl));
num = (port >> 8) & 15;
do {
iof = iocore.base[num];
type = iof->type;
if (!(type & (IOFUNC_SND | IOFUNC_EXT))) {
set = tbl[type & IOFUNC_SYS];
if (set == NULL) {
set = (IOFUNC)listarray_append(iocore.iotbl, iof);
if (set == NULL) {
return(FAILURE);
}
set->type |= IOFUNC_SND;
set->port = port & 0x0f00;
}
iocore.base[num] = set;
}
num += 0x10;
} while(num < 0x100);
return(SUCCESS);
}
BRESULT iocore_attachsndout(UINT port, IOOUT func) {
BRESULT r;
UINT num;
r = makesndiofunc(port);
if (r == SUCCESS) {
num = (port >> 8) & 15;
do {
attachout(iocore.base[num], port & 0xff, func);
num += 0x10;
} while(num < 0x100);
}
return(r);
}
BRESULT iocore_attachsndinp(UINT port, IOINP func) {
BRESULT r;
UINT num;
r = makesndiofunc(port);
if (r == SUCCESS) {
num = (port >> 8) & 15;
do {
attachinp(iocore.base[num], port & 0xff, func);
num += 0x10;
} while(num < 0x100);
}
return(r);
}
// ----
static IOFUNC getextiofunc(UINT port) {
IOFUNC iof;
iof = iocore.base[(port >> 8) & 0xff];
if (!(iof->type & IOFUNC_EXT)) {
iof = (IOFUNC)listarray_append(iocore.iotbl, iof);
if (iof != NULL) {
iocore.base[(port >> 8) & 0xff] = iof;
iof->type |= IOFUNC_EXT;
iof->port = port & 0xff00;
}
}
return(iof);
}
BRESULT iocore_attachout(UINT port, IOOUT func) {
IOFUNC iof;
iof = getextiofunc(port);
if (iof) {
attachout(iof, port & 0xff, func);
return(SUCCESS);
}
else {
return(FAILURE);
}
}
BRESULT iocore_attachinp(UINT port, IOINP func) {
IOFUNC iof;
iof = getextiofunc(port);
if (iof) {
attachinp(iof, port & 0xff, func);
return(SUCCESS);
}
else {
return(FAILURE);
}
}
// ----
void iocore_create(void) {
UINT i;
const UINT8 *p;
UINT r;
ZeroMemory(&iocore, sizeof(iocore));
ZeroMemory(ioterminate, sizeof(ioterminate));
for (i=0; i<NELEMENTS(termtbl); i++) {
p = termtbl[i].item;
r = termtbl[i].items;
do {
ioterminate[*p++] = (UINT8)(i + 1);
} while(--r);
}
}
void iocore_destroy(void) {
IOCORE ioc;
ioc = &iocore;
listarray_destroy(ioc->iotbl);
ioc->iotbl = NULL;
}
BRESULT iocore_build(void) {
IOCORE ioc;
IOFUNC cmn;
IOFUNC sys;
int i;
LISTARRAY iotbl;
ioc = &iocore;
listarray_destroy(ioc->iotbl);
iotbl = listarray_new(sizeof(_IOFUNC), 32);
ioc->iotbl = iotbl;
if (iotbl == NULL) {
goto icbld_err;
}
cmn = (IOFUNC)listarray_append(iotbl, NULL);
if (cmn == NULL) {
goto icbld_err;
}
for (i=0; i<256; i++) {
cmn->ioout[i] = defout8;
cmn->ioinp[i] = definp8;
}
sys = (IOFUNC)listarray_append(iotbl, cmn);
if (sys == NULL) {
goto icbld_err;
}
sys->type = IOFUNC_SYS;
for (i=0; i<256; i++) {
if (!(i & 0x0c)) {
ioc->base[i] = sys;
}
else {
ioc->base[i] = cmn;
}
}
return(SUCCESS);
icbld_err:
return(FAILURE);
}
// ----
static const FNIORESET resetfn[] =
{
// PC-9801 System...
cgrom_reset, crtc_reset,
dmac_reset, gdc_reset, fdc_reset,
keyboard_reset, nmiio_reset, pic_reset,
printif_reset, rs232c_reset, systemport_reset,
uPD4990_reset, fdd320_reset,
// sys+extend
itimer_reset, mouseif_reset,
// extend
artic_reset, egc_reset, np2sysp_reset,
necio_reset, epsonio_reset, emsio_reset,
#if defined(SUPPORT_PC9821)
pcidev_reset,
#endif
};
static const FNIOBIND bindfn[] =
{
// PC-9801 System...
cgrom_bind, cpuio_bind, crtc_bind,
dmac_bind, gdc_bind, fdc_bind,
keyboard_bind, nmiio_bind, pic_bind,
printif_bind, rs232c_bind, systemport_bind,
uPD4990_bind, fdd320_bind,
// sys+extend
itimer_bind, mouseif_bind,
// extend
artic_bind, egc_bind, np2sysp_bind,
necio_bind, epsonio_bind, emsio_bind,
#if defined(SUPPORT_PC9821)
pcidev_bind,
#endif
};
void iocore_cbreset(const FNIORESET *pfn, UINT uCount, const NP2CFG *pConfig)
{
while(uCount--)
{
(*pfn)(pConfig);
pfn++;
}
}
void iocore_cbbind(const FNIOBIND *pfn, UINT uCount)
{
while(uCount--)
{
(*pfn)();
pfn++;
}
}
void iocore_reset(const NP2CFG *pConfig)
{
iocore_cbreset(resetfn, NELEMENTS(resetfn), pConfig);
}
void iocore_bind(void)
{
iocore.busclock = pccore.multiple;
iocore_cbbind(bindfn, NELEMENTS(bindfn));
}
void IOOUTCALL iocore_out8(UINT port, REG8 dat) {
IOFUNC iof;
// TRACEOUT(("iocore_out8(%.2x, %.2x)", port, dat));
CPU_REMCLOCK -= iocore.busclock;
iof = iocore.base[(port >> 8) & 0xff];
iof->ioout[port & 0xff](port, dat);
}
REG8 IOINPCALL iocore_inp8(UINT port) {
IOFUNC iof;
REG8 ret;
CPU_REMCLOCK -= iocore.busclock;
iof = iocore.base[(port >> 8) & 0xff];
ret = iof->ioinp[port & 0xff](port);
// TRACEOUT(("iocore_inp8(%.2x) -> %.2x", port, ret));
return(ret);
}
void IOOUTCALL iocore_out16(UINT port, REG16 dat) {
IOFUNC iof;
// TRACEOUT(("iocore_out16(%.4x, %.4x)", port, dat));
CPU_REMCLOCK -= iocore.busclock;
#if defined(SUPPORT_IDEIO)
if (port == 0x0640) {
ideio_w16(port, dat);
return;
}
#endif
if ((port & 0xfff1) == 0x04a0) {
egc_w16(port, dat);
return;
}
if (!(port & 0x0c00)) {
switch(ioterminate[port & 0xff]) {
case TERM_WORD:
return;
case TERM_ACTIVE:
case TERM_PLUS:
case TERM_MINUS:
case TERM_EXT08:
iof = iocore.base[(port >> 8) & 0xff];
iof->ioout[port & 0xff](port, (UINT8)dat);
return;
}
}
iof = iocore.base[(port >> 8) & 0xff];
iof->ioout[port & 0xff](port, (UINT8)dat);
port++;
iof = iocore.base[(port >> 8) & 0xff];
iof->ioout[port & 0xff](port, (UINT8)(dat >> 8));
}
REG16 IOINPCALL iocore_inp16(UINT port) {
IOFUNC iof;
REG8 ret;
CPU_REMCLOCK -= iocore.busclock;
#if defined(SUPPORT_IDEIO)
if (port == 0x0640) {
return(ideio_r16(port));
}
#endif
if ((port & 0xfffc) == 0x005c) {
return(artic_r16(port));
}
iof = iocore.base[(port >> 8) & 0xff];
if (!(port & 0x0c00)) {
switch(ioterminate[port & 0xff]) {
case TERM_WORD:
return(WORD_TERMINATE);
case TERM_ACTIVE:
ret = iof->ioinp[port & 0xff](port);
return((CPU_AX & 0xff00) + ret);
case TERM_PLUS:
ret = iof->ioinp[port & 0xff](port);
return(0xff00 + ret);
case TERM_MINUS:
return(iof->ioinp[port & 0xff](port));
case TERM_EXT08:
ret = iof->ioinp[port & 0xff](port);
return(0x0800 + ret);
}
}
ret = iof->ioinp[port & 0xff](port);
port++;
iof = iocore.base[(port >> 8) & 0xff];
return((UINT16)((iof->ioinp[port & 0xff](port) << 8) + ret));
}
void IOOUTCALL iocore_out32(UINT port, UINT32 dat) {
CPU_REMCLOCK -= iocore.busclock;
#if defined(SUPPORT_PC9821)
if ((port & 0xfffb) == 0x0cf8) {
pcidev_w32(port, dat);
return;
}
#endif
iocore_out16(port, (UINT16)dat);
iocore_out16(port+2, (UINT16)(dat >> 16));
}
UINT32 IOINPCALL iocore_inp32(UINT port) {
UINT32 ret;
CPU_REMCLOCK -= iocore.busclock;
#if defined(SUPPORT_PC9821)
if ((port & 0xfffb) == 0x0cf8) {
return(pcidev_r32(port));
}
#endif
ret = iocore_inp16(port);
return(ret + (iocore_inp16(port+2) << 16));
}