-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIO.cs
704 lines (544 loc) · 17 KB
/
IO.cs
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
internal class PendingInterrupt
{
public bool pending { get; set; }
public int int_vec { get; set; }
}
internal struct Timer
{
public ushort counter_cur { get; set; }
public ushort counter_prv { get; set; }
public ushort counter_ini { get; set; }
public int mode { get; set; }
public int latch_type { get; set; }
public int latch_n { get; set; }
public int latch_n_cur { get; set; }
public bool is_running { get; set; }
}
internal class i8253 : Device
{
Timer [] _timers = new Timer[3];
PendingInterrupt [] _pi = new PendingInterrupt[3];
i8237 _i8237 = null;
int clock = 0;
// using a static seed to make it behave
// the same every invocation (until threads
// and timers are introduced)
private Random _random = new Random(1);
public i8253()
{
for(int i=0; i<_timers.Length; i++)
{
_timers[i] = new Timer();
_pi[i] = new PendingInterrupt();
_pi[i].int_vec = 8;
}
}
public override String GetName()
{
return "i8253";
}
public override void RegisterDevice(Dictionary <ushort, Device> mappings)
{
mappings[0x0040] = this;
mappings[0x0041] = this;
mappings[0x0042] = this;
mappings[0x0043] = this;
}
public override List<PendingInterrupt> GetPendingInterrupts()
{
List<PendingInterrupt> rc = new();
for(int i=0; i<_timers.Length; i++)
{
if (_pi[i].pending)
rc.Add(_pi[i]);
}
if (rc.Count > 0)
return rc;
return null;
}
public override (byte, bool) IO_Read(ushort port)
{
if (port == 0x0040)
return (GetCounter(0), _pi[0].pending);
if (port == 0x0041)
return (GetCounter(1), _pi[1].pending);
if (port == 0x0042)
return (GetCounter(2), _pi[2].pending);
return (0xaa, false);
}
public override bool IO_Write(ushort port, byte value)
{
if (port == 0x0040)
LatchCounter(0, value);
else if (port == 0x0041)
LatchCounter(1, value);
else if (port == 0x0042)
LatchCounter(2, value);
else if (port == 0x0043)
Command(value);
return _pi[0].pending || _pi[1].pending || _pi[2].pending;
}
public override void SyncClock(int clock)
{
}
public override bool HasAddress(uint addr)
{
return false;
}
public override void WriteByte(uint offset, byte value)
{
}
public override byte ReadByte(uint offset)
{
return 0xee;
}
public void SetDma(i8237 dma_instance)
{
_i8237 = dma_instance;
}
private void LatchCounter(int nr, byte v)
{
#if DEBUG
Log.DoLog($"OUT 8253: timer {nr} to {v} (type {_timers[nr].latch_type}, {_timers[nr].latch_n_cur} out of {_timers[nr].latch_n})", true);
#endif
if (_timers[nr].latch_n_cur > 0)
{
if (_timers[nr].latch_type == 1)
{
_timers[nr].counter_ini &= 0xff00;
_timers[nr].counter_ini |= v;
// assert _timers[nr].latch_n_cur == 1
}
else if (_timers[nr].latch_type == 2)
{
_timers[nr].counter_ini &= 0x00ff;
_timers[nr].counter_ini |= (ushort)(v << 8);
// assert _timers[nr].latch_n_cur == 1
}
else if (_timers[nr].latch_type == 3)
{
if (_timers[nr].latch_n_cur == 2)
{
_timers[nr].counter_ini &= 0xff00;
_timers[nr].counter_ini |= v;
}
else
{
_timers[nr].counter_ini &= 0x00ff;
_timers[nr].counter_ini |= (ushort)(v << 8);
}
// assert _timers[nr].latch_n_cur >= 1
}
_timers[nr].latch_n_cur--;
if (_timers[nr].latch_n_cur == 0)
{
#if DEBUG
Log.DoLog($"OUT 8253: timer {nr} started (count start: {_timers[nr].counter_ini})", true);
#endif
_timers[nr].latch_n_cur = _timers[nr].latch_n; // restart setup
_timers[nr].counter_cur = _timers[nr].counter_ini;
_timers[nr].is_running = true;
_pi[nr].pending = false;
}
}
}
private byte AddNoiseToLSB(int nr)
{
ushort current_prv = _timers[nr].counter_prv;
_timers[nr].counter_prv = _timers[nr].counter_cur;
if (Math.Abs(_timers[nr].counter_cur - current_prv) >= 2)
return (byte)(_random.Next(2) == 1 ? _timers[nr].counter_cur ^ 1 : _timers[nr].counter_cur);
return (byte)_timers[nr].counter_cur;
}
private byte GetCounter(int nr)
{
#if DEBUG
Log.DoLog($"OUT 8253: GetCounter {nr}: {(byte)_timers[nr].counter_cur} ({_timers[nr].latch_type}|{_timers[nr].latch_n_cur}/{_timers[nr].latch_n})", true);
#endif
byte rc = 0;
if (_timers[nr].latch_type == 1)
rc = AddNoiseToLSB(nr);
else if (_timers[nr].latch_type == 2)
rc = (byte)(_timers[nr].counter_cur >> 8);
else if (_timers[nr].latch_type == 3)
{
if (_timers[nr].latch_n_cur == 2)
rc = AddNoiseToLSB(nr);
else
rc = (byte)(_timers[nr].counter_cur >> 8);
}
_timers[nr].latch_n_cur--;
if (_timers[nr].latch_n_cur == 0)
_timers[nr].latch_n_cur = _timers[nr].latch_n;
return rc;
}
private void Command(byte v)
{
int nr = v >> 6;
int latch = (v >> 4) & 3;
int mode = (v >> 1) & 7;
int type = v & 1;
if (latch != 0)
{
#if DEBUG
Log.DoLog($"OUT 8253: command timer {nr}, latch {latch}, mode {mode}, type {type}", true);
#endif
_timers[nr].mode = mode;
_timers[nr].latch_type = latch;
_timers[nr].is_running = false;
_timers[nr].counter_ini = 0;
if (_timers[nr].latch_type == 1 || _timers[nr].latch_type == 2)
_timers[nr].latch_n = 1;
else if (_timers[nr].latch_type == 3)
_timers[nr].latch_n = 2;
_timers[nr].latch_n_cur = _timers[nr].latch_n;
}
else
{
#if DEBUG
Log.DoLog($"OUT 8253: query timer {nr} (reset value: {_timers[nr].counter_ini}, current value: {_timers[nr].counter_cur})", true);
#endif
}
}
public override bool Tick(int ticks)
{
clock += ticks;
#if DEBUG
// Log.DoLog($"i8253: {clock} cycles, {ticks} added", true);
#endif
bool interrupt = false;
while (clock >= 4)
{
for(int i=0; i<3; i++)
{
if (_timers[i].is_running == false)
continue;
_timers[i].counter_cur--;
#if DEBUG
// Log.DoLog($"i8253: timer {i} is now {_timers[i].counter_cur}", true);
#endif
if (_timers[i].counter_cur == 0)
{
// Log.DoLog($"i8253 reset counter", true);
// timer 1 is RAM refresh counter
if (i == 1)
_i8237.TickChannel0();
_timers[i].counter_cur = _timers[i].counter_ini;
// mode 0 generates an interrupt
if (_timers[i].mode == 0 || _timers[i].mode == 3)
{
_pi[i].pending = true;
interrupt = true;
#if DEBUG
Log.DoLog($"i8253: interrupt for timer {i} fires ({_timers[i].counter_ini})", true);
#endif
}
}
}
clock -= 4;
}
return interrupt;
}
}
internal class FlipFlop
{
bool state = false;
public bool get_state()
{
bool rc = state;
state = !state;
return rc;
}
public void reset()
{
state = false;
}
}
internal class b16buffer
{
ushort _value;
FlipFlop _f;
public b16buffer(FlipFlop f)
{
_f = f;
}
public void Put(byte v)
{
bool low_high = _f.get_state();
if (low_high)
{
_value &= 0xff;
_value |= (ushort)(v << 8);
}
else
{
_value &= 0xff00;
_value |= v;
}
}
public ushort GetValue()
{
return _value;
}
public void SetValue(ushort v)
{
_value = v;
}
public byte Get()
{
bool low_high = _f.get_state();
if (low_high)
return (byte)(_value >> 8);
return (byte)(_value & 0xff);
}
}
internal class i8237
{
byte [] _channel_page = new byte[4];
b16buffer [] _channel_address_register = new b16buffer[4];
b16buffer [] _channel_word_count = new b16buffer[4];
byte _command;
bool [] _channel_mask = new bool[4];
bool [] _reached_tc = new bool[4];
byte [] _channel_mode = new byte[4];
FlipFlop _ff = new();
bool _dma_enabled = true;
Bus _b;
public i8237(Bus b)
{
for(int i=0; i<4; i++) {
_channel_address_register[i] = new b16buffer(_ff);
_channel_word_count[i] = new b16buffer(_ff);
}
_b = b;
}
public bool Tick(int ticks)
{
return false;
}
public void TickChannel0()
{
// RAM refresh
_channel_address_register[0].SetValue((ushort)(_channel_address_register[0].GetValue() + 1));
ushort count = _channel_word_count[0].GetValue();
count--;
#if DEBUG
// Log.DoLog($"8237_TickChannel0, mask: {_channel_mask[0]}, tc: {_reached_tc[0]}, mode: {_channel_mode[0]}, dma enabled: {_dma_enabled}, {count}", true);
#endif
_channel_word_count[0].SetValue(count);
if (count == 0xffff)
_reached_tc[0] = true;
}
public (byte, bool) In(ushort addr)
{
byte v = 0;
if (addr == 0 || addr == 2 || addr == 4 || addr == 6)
{
v = _channel_address_register[addr / 2].Get();
}
else if (addr == 1 || addr == 3 || addr == 5 || addr == 7)
{
v = _channel_word_count[addr / 2].Get();
}
else if (addr == 8) // status register
{
for(int i=0; i<4; i++)
{
if (_reached_tc[i])
{
_reached_tc[i] = false;
v |= (byte)(1 << i);
}
}
}
// Log.DoLog($"8237_IN: {addr:X4} {v:X2}", true);
return (v, false);
}
void reset_masks(bool state)
{
for(int i=0; i<4; i++)
_channel_mask[i] = state;
}
public bool Out(ushort addr, byte value)
{
// Log.DoLog($"8237_OUT: {addr:X4} {value:X2}", true);
if (addr == 0 || addr == 2 || addr == 4 || addr == 6)
{
_channel_address_register[addr / 2].Put(value);
Log.DoLog($"8237 set channel {addr / 2} to address {value}", true);
}
else if (addr == 1 || addr == 3 || addr == 5 || addr == 7)
{
_channel_word_count[addr / 2].Put(value);
Log.DoLog($"8237 set channel {addr / 2} to count {value}", true);
}
else if (addr == 8)
{
_command = value;
_dma_enabled = (_command & 4) == 0;
}
else if (addr == 0x0a) // mask
_channel_mask[value & 3] = (value & 4) == 4; // dreq enable/disable
else if (addr == 0x0b) // mode register
_channel_mode[value & 3] = value;
else if (addr == 0x0c) // reset flipflop
_ff.reset();
else if (addr == 0x0d) // master reset
{
reset_masks(true);
_ff.reset();
// TODO: clear status
}
else if (addr == 0x0e) // reset masks
{
reset_masks(false);
}
else if (addr == 0x0f) // multiple mask
{
for(int i=0; i<4; i++)
_channel_mask[i] = (value & (1 << i)) != 0;
}
else if (addr == 0x87)
{
_channel_page[0] = (byte)(value & 0x0f);
}
else if (addr == 0x83)
{
_channel_page[1] = (byte)(value & 0x0f);
}
else if (addr == 0x81)
{
_channel_page[2] = (byte)(value & 0x0f);
}
else if (addr == 0x82)
{
_channel_page[3] = (byte)(value & 0x0f);
}
return false;
}
// used by devices, e.g. floppy
public bool SendToChannel(int channel, byte value)
{
if (_dma_enabled == false)
return false;
if (_channel_mask[channel])
return false;
if (_reached_tc[channel])
return false;
ushort addr = _channel_address_register[channel].GetValue();
uint full_addr = (uint)((_channel_page[channel] << 16) | addr);
_b.WriteByte(full_addr, value);
addr++;
_channel_address_register[channel].SetValue(addr);
ushort count = _channel_word_count[channel].GetValue();
count--;
if (count == 0xffff)
_reached_tc[channel] = true;
_channel_word_count[channel].SetValue(count);
return true;
}
}
class IO
{
private pic8259 _pic;
private i8237 _i8237;
private Bus _b;
private Dictionary <ushort, byte> _values = new Dictionary <ushort, byte>();
private Dictionary <ushort, Device> _io_map = new Dictionary <ushort, Device>();
private List<Device> _devices;
private int _clock;
public IO(Bus b, ref List<Device> devices)
{
_b = b;
_pic = new();
_i8237 = new(_b);
foreach(var device in devices)
{
device.RegisterDevice(_io_map);
if (device is i8253)
((i8253)device).SetDma(_i8237);
if (device is FloppyDisk)
((FloppyDisk)device).SetDma(_i8237);
device.SetPic(_pic);
}
_devices = devices;
}
public void ClearPendingInterrupt(int nr)
{
_pic.ClearPendingInterrupt(nr);
}
public byte GetInterruptMask()
{
return _pic.GetInterruptMask();
}
public (byte, bool) In(ushort addr)
{
// Log.DoLog($"IN: {addr:X4}", true);
foreach(var device in _devices)
device.SyncClock(_clock);
if (addr <= 0x000f || addr == 0x81 || addr == 0x82 || addr == 0x83 || addr == 0xc2)
return _i8237.In(addr);
if (addr == 0x0008) // DMA status register
return (0x0f, false); // 'transfer complete'
if (addr == 0x0020 || addr == 0x0021) // PIC
return _pic.In(addr);
if (addr == 0x0062) // PPI (XT only)
{
byte mode = 0;
if (_values.ContainsKey(0x61))
mode = _values[0x61];
byte switches = 0b00110011; // 1 floppy, MDA, 256kB, nocopro/noloop
if ((mode & 8) == 0)
return ((byte)(switches & 0x0f), false);
return ((byte)(switches >> 4), false);
}
if (addr == 0x0210) // verify expansion bus data
return (0xa5, false);
if (_io_map.ContainsKey(addr))
return _io_map[addr].IO_Read(addr);
#if DEBUG
Log.DoLog($"IN: I/O port {addr:X4} not implemented", true);
#endif
if (_values.ContainsKey(addr))
return (_values[addr], false);
return (0, false);
}
public bool Tick(int ticks, int clock)
{
bool rc = false;
foreach(var device in _devices)
rc |= device.Tick(ticks);
_i8237.Tick(ticks);
_clock = clock;
return rc;
}
public bool Out(ushort addr, ushort value)
{
// Log.DoLog($"OUT: I/O port {addr:X4} ({value:X2})", true);
if (addr <= 0x000f || addr == 0x81 || addr == 0x82 || addr == 0x83 || addr == 0xc2) // 8237
return _i8237.Out(addr, (byte)value);
else if (addr == 0x0020 || addr == 0x0021) // PIC
return _pic.Out(addr, (byte)value);
else if (addr == 0x0080)
Log.DoLog($"Manufacturer systems checkpoint {value:X2}", true);
else if (addr == 0x0322)
{
int harddisk_interrupt_nr = _pic.GetInterruptOffset() + 14;
//FIXME if (scheduled_interrupts.ContainsKey(harddisk_interrupt_nr) == false)
//FIXME scheduled_interrupts[harddisk_interrupt_nr] = 31; // generate (XT disk-)controller select pulse (IRQ 5)
#if DEBUG
Log.DoLog($"OUT: I/O port {addr:X4} ({value:X2}) generate controller select pulse");
#endif
}
else
{
if (_io_map.ContainsKey(addr))
return _io_map[addr].IO_Write(addr, (byte)value);
#if DEBUG
// Log.DoLog($"OUT: I/O port {addr:X4} ({value:X2}) not implemented", true);
#endif
}
_values[addr] = (byte)value;
return false;
}
}