summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/pic8259.c
blob: 7630404491a65b2b9753e5d54239fb4bfbe6b4df (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
/**********************************************************************

    8259 PIC interface and emulation

    The 8259 is a programmable interrupt controller used to multiplex
    interrupts for x86 and other computers.  The chip is set up by
    writing a series of Initialization Command Words (ICWs) after which
    the chip is operational and capable of dispatching interrupts.  After
    this, Operation Command Words (OCWs) can be written to control further
    behavior.

**********************************************************************/

#include "driver.h"
#include "memconv.h"
#include "machine/pic8259.h"

#define IRQ_COUNT	8

#define LOG_ICW		0
#define LOG_OCW		0
#define LOG_GENERAL	0

typedef enum
{
	STATE_ICW1,
	STATE_ICW2,
	STATE_ICW3,
	STATE_ICW4,
	STATE_READY
} pic8259_state_t;

struct pic8259
{
	emu_timer *timer;
	void (*set_int_line)(int which, int interrupt);

	pic8259_state_t state;

	UINT8 irq_lines;
	UINT8 in_service;
	UINT8 pending;
	UINT8 prio;
	UINT8 interrupt_mask;

	UINT8 input;
	UINT32 special : 1;

	/* ICW1 state */
	UINT32 level_trig_mode : 1;
	UINT32 vector_size : 1;
	UINT32 cascade : 1;
	UINT32 icw4_needed : 1;

	/* ICW2 state */
	UINT8 base;

	/* ICW3 state */
	UINT8 slave;

	/* ICW4 state */
	UINT32 nested : 1;
	UINT32 mode : 2;
	UINT32 auto_eoi : 1;
	UINT32 is_x86 : 1;
};

static struct pic8259 *pic;

static TIMER_CALLBACK( pic8259_timerproc );


/* initializer */
int pic8259_init(int count, void (*set_int_line)(int which, int interrupt))
{
	int i;

	/* allocate pic structures */
	pic = auto_malloc(count * sizeof(struct pic8259));
	memset(pic, 0, count * sizeof(struct pic8259));

	for (i = 0; i < count; i++)
	{
		pic[i].timer = timer_alloc(pic8259_timerproc);
		pic[i].set_int_line = set_int_line;
	}

	return 0;
}



static TIMER_CALLBACK( pic8259_timerproc )
{
	int which = param;
	struct pic8259 *p = &pic[which];
	int irq;
	UINT8 mask;

	/* check the various IRQs */
	for (irq = 0; irq < IRQ_COUNT; irq++)
	{
		mask = 1 << irq;

		/* is this IRQ in service? */
		if (p->in_service & mask)
		{
			if (LOG_GENERAL)
				logerror("pic8259_timerproc(): PIC #%d IRQ #%d still in service\n", which, irq);
			return;
		}

		/* is this IRQ pending and enabled? */
		if ((p->state == STATE_READY) && (p->pending & mask) && !(p->interrupt_mask & mask))
		{
			if (LOG_GENERAL)
				logerror("pic8259_timerproc(): PIC #%d triggering IRQ #%d\n", which, irq);
			if (p->set_int_line)
				p->set_int_line(which, 1);
			return;
		}
	}
	if (p->set_int_line)
		p->set_int_line(which, 0);
}



static void pic8259_set_timer(int which)
{
	timer_adjust(pic[which].timer, attotime_zero, which, attotime_zero);
}



void pic8259_set_irq_line(int which, int irq, int state)
{
	if (state)
	{
		/* setting IRQ line */
		if (!(pic[which].irq_lines & (1 << irq)))
		{
			if (LOG_GENERAL)
				logerror("pic8259_set_irq_line(): PIC #%d set IRQ line #%d\n", which, irq);

			pic[which].irq_lines |= 1 << irq;
			pic[which].pending |= 1 << irq;
			pic8259_set_timer(which);
		}
	}
	else
	{
		/* clearing IRQ line */
		if (pic[which].irq_lines & (1 << irq))
		{
			if (LOG_GENERAL)
				logerror("pic8259_set_irq_line(): PIC #%d cleared IRQ line #%d\n", which, irq);

			pic[which].irq_lines &= ~(1 << irq);
			pic8259_set_timer(which);
		}
	}
}



int pic8259_acknowledge(int which)
{
	struct pic8259 *p = &pic[which];
	UINT8 mask;
	int irq;

	for (irq = 0; irq < IRQ_COUNT; irq++)
	{
		mask = 1 << irq;

		/* is this IRQ pending and enabled? */
		if ((p->pending & mask) && !(p->interrupt_mask & mask))
		{
			if (LOG_GENERAL)
				logerror("pic8259_acknowledge(): PIC #%d acknowledge IRQ #%d\n", which, irq);

			p->pending &= ~mask;
			if (!p->auto_eoi)
				p->in_service |= mask;

			return irq + p->base;
		}
	}
	return 0;
}



static UINT8 pic8259_read(int which, offs_t offset)
{
	struct pic8259 *p = &pic[which];

	/* NPW 18-May-2003 - Changing 0xFF to 0x00 as per Ruslan */
	UINT8 data = 0x00;

	switch(offset)
	{
		case 0: /* PIC acknowledge IRQ */
			if (p->special)
			{
				p->special = 0;
				data = p->input;
			}
			break;

		case 1: /* PIC mask register */
			data = p->interrupt_mask;
			break;
	}
	return data;
}



static void pic8259_write(int which, offs_t offset, UINT8 data )
{
	struct pic8259 *p = &pic[which];

	switch(offset)
	{
		case 0:    /* PIC acknowledge IRQ */
			if (data & 0x10)
			{
				/* write ICW1 - this pretty much resets the chip */
				if (LOG_ICW)
					logerror("pic8259_write(): ICW1; which=%d data=0x%02X\n", which, data);

				p->interrupt_mask	= 0x00;
				p->level_trig_mode	= (data & 0x08) ? 1 : 0;
				p->vector_size		= (data & 0x04) ? 1 : 0;
				p->cascade			= (data & 0x02) ? 0 : 1;
				p->icw4_needed		= (data & 0x01) ? 1 : 0;
				p->state			= STATE_ICW2;
			}
			else if (p->state == STATE_READY)
			{
				if ((data & 0x98) == 0x08)
				{
					/* write OCW3 */
					if (LOG_OCW)
						logerror("pic8259_write(): OCW3; which=%d data=0x%02X\n", which, data);

					switch (data & 0x03)
					{
						case 0x02:
							p->special = 1;
							p->input = p->pending;
							break;
						case 0x03:
							p->special = 1;
							p->input = p->in_service & ~p->interrupt_mask;
							break;
					}
				}
				else if ((data & 0x18) == 0x00)
				{
					int n = data & 7;
					UINT8 mask = 1 << n;

					/* write OCW2 */
					if (LOG_OCW)
						logerror("pic8259_write(): OCW2; which=%d data=0x%02X\n", which, data);

					switch (data & 0xe0)
					{
						case 0x00:
							p->prio = 0;
							break;
						case 0x20:
							for (n = 0, mask = 1<<p->prio; n < 8; n++, mask = (mask<<1) | (mask>>7))
							{
								if (p->in_service & mask)
								{
									p->in_service &= ~mask;
									break;
								}
							}
							break;
						case 0x40:
							break;
						case 0x60:
							if( p->in_service & mask )
							{
								p->in_service &= ~mask;
							}
							break;
						case 0x80:
							p->prio = ++p->prio & 7;
							break;
						case 0xa0:
							for (n = 0, mask = 1<<p->prio; n < 8; n++, mask = (mask<<1) | (mask>>7))
							{
								if( p->in_service & mask )
								{
									p->in_service &= ~mask;
									p->prio = ++p->prio & 7;
									break;
								}
							}
							break;
						case 0xc0:
							p->prio = n & 7;
							break;
						case 0xe0:
							if( p->in_service & mask )
							{
								p->in_service &= ~mask;
								p->pending &= ~mask;
								p->prio = ++p->prio & 7;
							}
							break;
					}
				}
			}
			break;

		case 1:
			switch(p->state)
			{
				case STATE_ICW1:
					break;

				case STATE_ICW2:
					/* write ICW2 */
					if (LOG_ICW)
						logerror("pic8259_write(): ICW2; which=%d data=0x%02X\n", which, data);

					p->base = data & 0xf8;
					if (p->cascade)
						p->state = STATE_ICW3;
					else
						p->state = p->icw4_needed ? STATE_ICW4 : STATE_READY;
					break;

				case STATE_ICW3:
					/* write ICW3 */
					if (LOG_ICW)
						logerror("pic8259_write(): ICW3; which=%d data=0x%02X\n", which, data);

					p->slave = data;
					p->state = p->icw4_needed ? STATE_ICW4 : STATE_READY;
					break;

				case STATE_ICW4:
					/* write ICW4 */
					if (LOG_ICW)
						logerror("pic8259_write(): ICW4; which=%d data=0x%02X\n", which, data);

					p->nested	= (data & 0x10) ? 1 : 0;
					p->mode = (data >> 2) & 3;
					p->auto_eoi = (data & 0x02) ? 1 : 0;
					p->is_x86 = (data & 0x01) ? 1 : 0;
					p->state = STATE_READY;
					break;

				case STATE_READY:
					/* write OCW1 - set interrupt mask register */
					if (LOG_OCW)
						logerror("pic8259_write(): OCW1; which=%d data=0x%02X\n", which, data);

					p->interrupt_mask = data;
					break;
			}
			break;
    }
	pic8259_set_timer(which);
}



/* ----------------------------------------------------------------------- */

READ8_HANDLER ( pic8259_0_r )	{ return pic8259_read(0, offset); }
READ8_HANDLER ( pic8259_1_r )	{ return pic8259_read(1, offset); }
WRITE8_HANDLER ( pic8259_0_w )	{ pic8259_write(0, offset, data); }
WRITE8_HANDLER ( pic8259_1_w )	{ pic8259_write(1, offset, data); }

READ16_HANDLER ( pic8259_16le_0_r ) { return read16le_with_read8_handler(pic8259_0_r, offset, mem_mask); }
READ16_HANDLER ( pic8259_16le_1_r ) { return read16le_with_read8_handler(pic8259_1_r, offset, mem_mask); }
WRITE16_HANDLER ( pic8259_16le_0_w ) { write16le_with_write8_handler(pic8259_0_w, offset, data, mem_mask); }
WRITE16_HANDLER ( pic8259_16le_1_w ) { write16le_with_write8_handler(pic8259_1_w, offset, data, mem_mask); }

READ32_HANDLER ( pic8259_32le_0_r ) { return read32le_with_read8_handler(pic8259_0_r, offset, mem_mask); }
READ32_HANDLER ( pic8259_32le_1_r ) { return read32le_with_read8_handler(pic8259_1_r, offset, mem_mask); }
WRITE32_HANDLER ( pic8259_32le_0_w ) { write32le_with_write8_handler(pic8259_0_w, offset, data, mem_mask); }
WRITE32_HANDLER ( pic8259_32le_1_w ) { write32le_with_write8_handler(pic8259_1_w, offset, data, mem_mask); }

READ64_HANDLER ( pic8259_64be_0_r ) { return read64be_with_read8_handler(pic8259_0_r, offset, mem_mask); }
READ64_HANDLER ( pic8259_64be_1_r ) { return read64be_with_read8_handler(pic8259_1_r, offset, mem_mask); }
WRITE64_HANDLER ( pic8259_64be_0_w ) { write64be_with_write8_handler(pic8259_0_w, offset, data, mem_mask); }
WRITE64_HANDLER ( pic8259_64be_1_w ) { write64be_with_write8_handler(pic8259_1_w, offset, data, mem_mask); }