-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIO.cs
More file actions
124 lines (98 loc) · 3.19 KB
/
IO.cs
File metadata and controls
124 lines (98 loc) · 3.19 KB
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
class IO
{
private i8259 _pic;
private i8237 _i8237;
private Bus _b;
private bool _test_mode = false;
private Dictionary <ushort, Device> _io_map = new Dictionary <ushort, Device>();
private List<Device> _devices;
private List<Device> _tick_devices = new();
public IO(Bus b, List<Device> devices, bool test_mode)
{
_b = b;
_pic = new();
_i8237 = new(_b);
foreach(var device in devices)
{
device.SetDma(_i8237);
device.SetPic(_pic);
device.SetBus(b);
if (device.Ticks())
_tick_devices.Add(device);
}
devices.Add(_i8237);
devices.Add(_pic);
foreach(var device in devices)
device.RegisterDevice(_io_map);
_devices = devices;
_test_mode = test_mode;
Log.DoLog($"IO: got {_tick_devices.Count()} clock-dependent devices", LogLevel.DEBUG);
}
public i8259 GetPIC()
{
return _pic;
}
public ushort In(ushort addr, bool b16)
{
if (_test_mode)
return 65535;
if (_io_map.ContainsKey(addr))
{
ushort rc = _io_map[addr].IO_Read(addr);
if (b16)
{
ushort next_port = (ushort)(addr + 1);
if (_io_map.ContainsKey(next_port))
{
ushort temp = _io_map[next_port].IO_Read(next_port);
rc |= (ushort)(temp << 8);
}
else
{
Log.DoLog($"IN: confused: 'next port' ({next_port:X04}) for a 16 bit read is not mapped", LogLevel.WARNING);
}
}
Log.DoLog($"IN: read {rc:X} from device on I/O port {addr:X4} (16 bit: {b16})", LogLevel.TRACE);
return rc;
}
if (addr == 0x0210) // verify expansion bus data
{
Tools.Assert(b16 == false, "expansion bus data");
return 0xa5;
}
Log.DoLog($"IN: I/O port {addr:X4} not implemented", LogLevel.WARNING);
return (ushort)(b16 ? 0xffff : 0xff);
}
public bool Tick(int ticks, long clock)
{
bool rc = false;
foreach(var device in _tick_devices)
rc |= device.Tick(ticks, clock);
return rc;
}
public bool Out(ushort addr, ushort value, bool b16)
{
if (_test_mode)
return false;
bool rc = false;
if (_io_map.ContainsKey(addr))
{
rc |= _io_map[addr].IO_Write(addr, (byte)(value & 255));
if (b16)
{
ushort next_port = (ushort)(addr + 1);
if (_io_map.ContainsKey(next_port))
rc |= _io_map[next_port].IO_Write(next_port, (byte)(value >> 8));
else
Log.DoLog($"OUT: confused: 'next port' ({next_port:X04}) for a 16 bit write is not mapped", LogLevel.WARNING);
}
else
{
Tools.Assert(value < 256, "b8");
}
return rc;
}
Log.DoLog($"OUT: I/O port {addr:X4} ({value:X2}) not implemented", LogLevel.WARNING);
return false;
}
}