summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/output.cpp
blob: 25d37dd4c43c04cc8f9f69aa444a0c908b82e0ba (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
// license:BSD-3-Clause
// copyright-holders:Nicola Salmoria, Aaron Giles
/***************************************************************************

    output.c

    General purpose output routines.
***************************************************************************/

#include "emu.h"
#include "coreutil.h"

//**************************************************************************
//  OUTPUT MANAGER
//**************************************************************************

//-------------------------------------------------
//  output_manager - constructor
//-------------------------------------------------

output_manager::output_manager(running_machine &machine)
	: m_machine(machine),
		m_uniqueid(12345)
{
	/* add pause callback */
	machine.add_notifier(MACHINE_NOTIFY_PAUSE, machine_notify_delegate(FUNC(output_manager::pause), this));
	machine.add_notifier(MACHINE_NOTIFY_RESUME, machine_notify_delegate(FUNC(output_manager::resume), this));
}

/*-------------------------------------------------
    find_item - find an item based on a string
-------------------------------------------------*/

output_manager::output_item* output_manager::find_item(const char *string)
{
	/* use the hash as a starting point and find an entry */
	for (auto &item : m_itemtable)
		if (strcmp(string, item.second.name.c_str()) == 0)
			return &item.second;

	return nullptr;
}


/*-------------------------------------------------
    create_new_item - create a new item
-------------------------------------------------*/

output_manager::output_item *output_manager::create_new_item(const char *outname, INT32 value)
{
	output_item item;

	/* fill in the data */
	item.name = outname;
	item.id = m_uniqueid++;
	item.value = value;

	/* add us to the hash table */
	m_itemtable.insert(std::pair<std::string, output_item>(outname, item));
	return &m_itemtable.find(outname)->second;
}

/*-------------------------------------------------
    output_pause - send pause message
-------------------------------------------------*/

void output_manager::pause()
{
	set_value("pause", 1);
}

void output_manager::resume()
{
	set_value("pause", 0);
}


/*-------------------------------------------------
    output_set_value - set the value of an output
-------------------------------------------------*/

void output_manager::set_value(const char *outname, INT32 value)
{
	output_item *item = find_item(outname);
	INT32 oldval;

	/* if no item of that name, create a new one and send the item's state */
	if (item == nullptr)
	{
		item = create_new_item(outname, value);
		oldval = value + 1;
	}

	else
	{
		/* set the new value */
		oldval = item->value;
		item->value = value;
	}

	/* if the value is different, signal the notifier */
	if (oldval != value)
	{
		/* call the local notifiers first */
		for (auto notify : item->notifylist)
			(*notify.m_notifier)(outname, value, notify.m_param);

		/* call the global notifiers next */
		for (auto notify : m_global_notifylist)
			(*notify.m_notifier)(outname, value, notify.m_param);
	}
}


/*-------------------------------------------------
    output_set_indexed_value - set the value of an
    indexed output
-------------------------------------------------*/

void output_manager::set_indexed_value(const char *basename, int index, int value)
{
	char buffer[100];
	char *dest = buffer;

	/* copy the string */
	while (*basename != 0)
		*dest++ = *basename++;

	/* append the index */
	if (index >= 1000) *dest++ = '0' + ((index / 1000) % 10);
	if (index >= 100) *dest++ = '0' + ((index / 100) % 10);
	if (index >= 10) *dest++ = '0' + ((index / 10) % 10);
	*dest++ = '0' + (index % 10);
	*dest++ = 0;

	/* set the value */
	set_value(buffer, value);
}


/*-------------------------------------------------
    output_get_value - return the value of an
    output
-------------------------------------------------*/

INT32 output_manager::get_value(const char *outname)
{
	output_item *item = find_item(outname);

	/* if no item, value is 0 */
	if (item == nullptr)
		return 0;
	return item->value;
}


/*-------------------------------------------------
    output_get_indexed_value - get the value of an
    indexed output
-------------------------------------------------*/

INT32 output_manager::get_indexed_value(const char *basename, int index)
{
	char buffer[100];
	char *dest = buffer;

	/* copy the string */
	while (*basename != 0)
		*dest++ = *basename++;

	/* append the index */
	if (index >= 1000) *dest++ = '0' + ((index / 1000) % 10);
	if (index >= 100) *dest++ = '0' + ((index / 100) % 10);
	if (index >= 10) *dest++ = '0' + ((index / 10) % 10);
	*dest++ = '0' + (index % 10);
	*dest++ = 0;

	/* set the value */
	return get_value(buffer);
}


/*-------------------------------------------------
    output_set_notifier - sets a notifier callback
    for a particular output, or for all outputs
    if NULL is specified
-------------------------------------------------*/

void output_manager::set_notifier(const char *outname, output_notifier_func callback, void *param)
{
	output_notify notify(callback, param);
	/* if an item is specified, find it */
	if (outname != nullptr)
	{
		output_item *item = find_item(outname);

		/* if no item of that name, create a new one */
		if (item == nullptr)
			item = create_new_item(outname, 0);

		item->notifylist.push_back(notify);
	}
	else
		m_global_notifylist.push_back(notify);
}


/*-------------------------------------------------
    output_notify_all - immediately call the given
    notifier for all outputs
-------------------------------------------------*/

void output_manager::notify_all(output_notifier_func callback, void *param)
{
	for (auto &item : m_itemtable)
		(*callback)(item.second.name.c_str(), item.second.value, param);
}


/*-------------------------------------------------
    output_name_to_id - returns a unique ID for
    a given name
-------------------------------------------------*/

UINT32 output_manager::name_to_id(const char *outname)
{
	output_item *item = find_item(outname);

	/* if no item, ID is 0 */
	if (item == nullptr)
		return 0;
	return item->id;
}


/*-------------------------------------------------
    output_id_to_name - returns a name that maps
    to a given unique ID
-------------------------------------------------*/

const char *output_manager::id_to_name(UINT32 id)
{
	for (auto &item : m_itemtable)
		if (item.second.id == id)
			return item.second.name.c_str();

	/* nothing found, return nullptr */
	return nullptr;
}
ers.noreply.github.com> 2011-05-06 17:13:14 +0000 committer Angelo Salese <angelosa@users.noreply.github.com> 2011-05-06 17:13:14 +0000 More porting ...' href='/mame/commit/src/emu/machine/6525tpi.c?id=18f84bcc0cc4de0e580df60f651ed1b0e31746f1'>18f84bcc0cc
6f4d8b8423f
18f84bcc0cc
177b6f63ae7

18f84bcc0cc
6f4d8b8423f
18f84bcc0cc




6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc
18f84bcc0cc




6f4d8b8423f
18f84bcc0cc
177b6f63ae7

18f84bcc0cc
6f4d8b8423f
18f84bcc0cc



6f4d8b8423f
18f84bcc0cc
177b6f63ae7

18f84bcc0cc
6f4d8b8423f
18f84bcc0cc







6f4d8b8423f







18f84bcc0cc


6f4d8b8423f
18f84bcc0cc
177b6f63ae7

18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

1f90ceab075
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc




6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc



6f4d8b8423f

18f84bcc0cc

6f4d8b8423f
18f84bcc0cc



1f90ceab075
18f84bcc0cc




6f4d8b8423f
18f84bcc0cc
1f90ceab075
18f84bcc0cc



6f4d8b8423f
177b6f63ae7
18f84bcc0cc


6f4d8b8423f
177b6f63ae7
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc

177b6f63ae7
18f84bcc0cc


6f4d8b8423f
177b6f63ae7
18f84bcc0cc


6f4d8b8423f
177b6f63ae7
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc

177b6f63ae7
18f84bcc0cc


6f4d8b8423f
18f84bcc0cc




6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
177b6f63ae7
18f84bcc0cc



6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
177b6f63ae7
18f84bcc0cc






6f4d8b8423f
18f84bcc0cc



640354ad9ef





18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
18f84bcc0cc
6f4d8b8423f
18f84bcc0cc

6f4d8b8423f
b711b1a007c
6f4d8b8423f
b711b1a007c
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

                                  





























































































                                                                                                    







                                               

 
                                                            
 
                                                                                                             
                                                                                         








                            













                             
 
                                       
         
                         

         
 
                                                   

                                                   
 
                                   
 
                            








                                    

                                       














                                           

 


                                                   
 
                                   
 
                                       


                      

 
 



                                                                            
                                    
 
                                               
         
                                      
 
                                                                                        
 
                                                



         
                                      
 
                                              
         
                                      
 
                                                                                          
 
                                                



         
                                         
 
                                                        
         
                                       
 
                                                                  
                 

                                        




                 
                                         
 
                                                        
         
                                       
 
                                                                  
                 

                                        




                 
                                         
 
                                                        
         
                                       
 
                                                                  
                 

                                        




                 
                                         
 
                                                        
         
                                       


                                                                     
                                                         
                 

                                        




                 
                                         
 
                                                         
         
                                       


                                                                  
                                                               
                 

                                        



                 
                                    
 
                            
 

                                          
 
                                                        




                    
                                     
 
                      


 
                                    
 
                            
 

                                          
 
                                                        




                    
                                     
 
                      


 
                                    
 
                            
 

                                           
 
                                                        




                    
                                     
 
                      


 
                                    
 




                           
                              
 

                                              
 
                                                                



                      
                              
 

                                              
 
                                                                







                                   







                                                             


                    
                                      
 

                                                      
 
                                                                        

                 
                                                                                                       


                      
                               


                      
                               


                      
                               


                      
                            




                                         
                                         

                                            
                                               
                         
                                           

                                         
                                            
                         
                                           

                                         
                                            
                         
                                           

                                         
                                            
                         
                                           

                                         
                                            



                         

                                     

                 
                                  



                      
                                                                                               




                    
                                      
 
                                                                                                



                           
                                
                                                                                


                      
                                
                                                                                


                      
                                

                                    
                                                                                        


                      
                               
                                                                                


                      
                               
                                                                                


                      
                               

                                    
                                                                                        


                      
                            




                                          
                                                                  
                                 
                                                                     
                                                                



                                          
                                                                  
                                 
                                                                     
                                                                






                                 
                                   



                      





                                                                  

                                                               
                                 
 
                       

 
                                 
 
                       

 
                                 
 
                       
 
// license:BSD-3-Clause
// copyright-holders:Peter Trauner
/***************************************************************************
    mos tri port interface 6525
    mos triple interface adapter 6523

    peter.trauner@jk.uni-linz.ac.at

    used in commodore b series
    used in commodore c1551 floppy disk drive
***************************************************************************/

/*
 mos tpi 6525
 40 pin package
 3 8 bit ports (pa, pb, pc)
 8 registers to pc
 0 port a 0 in low
 1 port a data direction register 1 output
 2 port b
 3 port b ddr
 4 port c
  handshaking, interrupt mode
  0 interrupt 0 input, 1 interrupt enabled
   interrupt set on falling edge
  1 interrupt 1 input
  2 interrupt 2 input
  3 interrupt 3 input
  4 interrupt 4 input
  5 irq output
  6 ca handshake line (read handshake answer on I3 preferred)
  7 cb handshake line (write handshake clear on I4 preferred)
 5 port c ddr

 6 cr configuration register
  0 mc
    0 port c normal input output mode like port a and b
    1 port c used for handshaking and interrupt input
  1 1 interrupt prioritized
  2 i3 configure edge
    1 interrupt set on positive edge
  3 i4 configure edge
  5,4 ca handshake
   00 on read
      rising edge of i3 sets ca high
      read a data from computers sets ca low
   01 pulse output
      1 microsecond low after read a operation
   10 manual output low
   11 manual output high
  7,6 cb handshake
   00 handshake on write
      write b data from computer sets cb 0
      rising edge of i4 sets cb high
   01 pulse output
      1 microsecond low after write b operation
   10 manual output low
   11 manual output high
 7 air active interrupt register
   0 I0 occurred
   1 I1 occurred
   2 I2 occurred
   3 I3 occurred
   4 I4 occurred
   read clears interrupt

 non prioritized interrupts
  interrupt is set when occurred
  read clears all interrupts

 prioritized interrupts
  I4>I3>I2>I1>I0
  highest interrupt can be found in air register
  read clears highest interrupt
*/

#include "emu.h"
#include "6525tpi.h"


/***************************************************************************
    CONSTANTS
***************************************************************************/

#define VERBOSE_LEVEL 0
#define DBG_LOG( MACHINE, N, M, A ) \
	do { \
		if(VERBOSE_LEVEL >= N) \
		{ \
			if( M ) \
				logerror("%11.6f: %-24s", MACHINE.time().as_double(), (char*) M ); \
			logerror A; \
		} \
	} while (0)


#define INTERRUPT_MODE (m_cr & 1)
#define PRIORIZED_INTERRUPTS (m_cr & 2)
#define INTERRUPT3_RISING_EDGE (m_cr & 4)
#define INTERRUPT4_RISING_EDGE (m_cr & 8)
#define CA_MANUAL_OUT (m_cr & 0x20)
#define CA_MANUAL_LEVEL ((m_cr & 0x10) ? 1 : 0)
#define CB_MANUAL_OUT (m_cr & 0x80)
#define CB_MANUAL_LEVEL ((m_cr & 0x40) ? 1 : 0)


const device_type TPI6525 = &device_creator<tpi6525_device>;

tpi6525_device::tpi6525_device(const machine_config &mconfig, std::string tag, device_t *owner, UINT32 clock)
	: device_t(mconfig, TPI6525, "6525 TPI", tag, owner, clock, "tpi6525", __FILE__),
	m_out_irq_cb(*this),
	m_in_pa_cb(*this),
	m_out_pa_cb(*this),
	m_in_pb_cb(*this),
	m_out_pb_cb(*this),
	m_in_pc_cb(*this),
	m_out_pc_cb(*this),
	m_out_ca_cb(*this),
	m_out_cb_cb(*this),
	m_port_a(0),
	m_ddr_a(0),
	m_in_a(0),
	m_port_b(0),
	m_ddr_b(0),
	m_in_b(0),
	m_port_c(0),
	m_ddr_c(0),
	m_in_c(0),
	m_ca_level(0),
	m_cb_level(0),
	m_interrupt_level(0),
	m_cr(0),
	m_air(0)
{
	for (auto & elem : m_irq_level)
	{
		elem = 0;
	}
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void tpi6525_device::device_start()
{
	// resolve callbacks
	m_out_irq_cb.resolve_safe();
	m_in_pa_cb.resolve();
	m_out_pa_cb.resolve_safe();
	m_in_pb_cb.resolve();
	m_out_pb_cb.resolve_safe();
	m_in_pc_cb.resolve();
	m_out_pc_cb.resolve_safe();
	m_out_ca_cb.resolve_safe();
	m_out_cb_cb.resolve_safe();

	/* register for state saving */
	save_item(NAME(m_port_a));
	save_item(NAME(m_ddr_a));
	save_item(NAME(m_in_a));
	save_item(NAME(m_port_b));
	save_item(NAME(m_ddr_b));
	save_item(NAME(m_in_b));
	save_item(NAME(m_port_c));
	save_item(NAME(m_ddr_c));
	save_item(NAME(m_in_c));
	save_item(NAME(m_ca_level));
	save_item(NAME(m_cb_level));
	save_item(NAME(m_interrupt_level));
	save_item(NAME(m_cr));
	save_item(NAME(m_air));
	save_item(NAME(m_irq_level));
}

//-------------------------------------------------
//  device_reset - device-specific reset
//-------------------------------------------------

void tpi6525_device::device_reset()
{
	/* setup some initial values */
	m_in_a = 0xff;
	m_in_b = 0xff;
	m_in_c = 0xff;
}


/***************************************************************************
    IMPLEMENTATION
***************************************************************************/

void tpi6525_device::set_interrupt()
{
	if (!m_interrupt_level && (m_air != 0))
	{
		m_interrupt_level = 1;

		DBG_LOG(machine(), 3, "tpi6525", ("%s set interrupt\n", tag().c_str()));

		m_out_irq_cb(m_interrupt_level);
	}
}


void tpi6525_device::clear_interrupt()
{
	if (m_interrupt_level && (m_air == 0))
	{
		m_interrupt_level = 0;

		DBG_LOG(machine(), 3, "tpi6525", ("%s clear interrupt\n", tag().c_str()));

		m_out_irq_cb(m_interrupt_level);
	}
}


WRITE_LINE_MEMBER( tpi6525_device::i0_w )
{
	if (INTERRUPT_MODE && (state != m_irq_level[0]))
	{
		m_irq_level[0] = state;

		if ((state == 0) && !(m_air & 1) && (m_ddr_c & 1))
		{
			m_air |= 1;
			set_interrupt();
		}
	}
}


WRITE_LINE_MEMBER( tpi6525_device::i1_w )
{
	if (INTERRUPT_MODE && (state != m_irq_level[1]))
	{
		m_irq_level[1] = state;

		if ((state == 0) && !(m_air & 2) && (m_ddr_c & 2))
		{
			m_air |= 2;
			set_interrupt();
		}
	}
}


WRITE_LINE_MEMBER( tpi6525_device::i2_w )
{
	if (INTERRUPT_MODE && (state != m_irq_level[2]))
	{
		m_irq_level[2] = state;

		if ((state == 0) && !(m_air & 4) && (m_ddr_c & 4))
		{
			m_air |= 4;
			set_interrupt();
		}
	}
}


WRITE_LINE_MEMBER( tpi6525_device::i3_w )
{
	if (INTERRUPT_MODE && (state != m_irq_level[3]))
	{
		m_irq_level[3] = state;

		if (((INTERRUPT3_RISING_EDGE && (state == 1))
			|| (!INTERRUPT3_RISING_EDGE && (state == 0)))
			&& !(m_air & 8) && (m_ddr_c & 8))
		{
			m_air |= 8;
			set_interrupt();
		}
	}
}


WRITE_LINE_MEMBER( tpi6525_device::i4_w )
{
	if (INTERRUPT_MODE && (state != m_irq_level[4]) )
	{
		m_irq_level[4] = state;

		if (((INTERRUPT4_RISING_EDGE && (state == 1))
			||(!INTERRUPT4_RISING_EDGE&&(state == 0)))
			&& !(m_air & 0x10) && (m_ddr_c & 0x10))
		{
			m_air |= 0x10;
			set_interrupt();
		}
	}
}

READ8_MEMBER( tpi6525_device::pa_r )
{
	UINT8 data = m_in_a;

	if (!m_in_pa_cb.isnull())
		data = m_in_pa_cb(offset);

	data = (data & ~m_ddr_a) | (m_ddr_a & m_port_a);

	return data;
}


WRITE8_MEMBER( tpi6525_device::pa_w )
{
	m_in_a = data;
}


READ8_MEMBER( tpi6525_device::pb_r )
{
	UINT8 data = m_in_b;

	if (!m_in_pb_cb.isnull())
		data = m_in_pb_cb(offset);

	data = (data & ~m_ddr_b) | (m_ddr_b & m_port_b);

	return data;
}


WRITE8_MEMBER( tpi6525_device::pb_w )
{
	m_in_b = data;
}


READ8_MEMBER( tpi6525_device::pc_r )
{
	UINT8 data = m_in_c;

	if (!m_in_pc_cb.isnull())
		data &= m_in_pc_cb(offset);

	data = (data & ~m_ddr_c) | (m_ddr_c & m_port_c);

	return data;
}


WRITE8_MEMBER( tpi6525_device::pc_w )
{
	m_in_c = data;
}


READ8_MEMBER( tpi6525_device::read )
{
	UINT8 data = 0xff;

	switch (offset & 7)
	{
	case 0:
		data = m_in_a;

		if (!m_in_pa_cb.isnull())
			data &= m_in_pa_cb(0);

		data = (data & ~m_ddr_a) | (m_ddr_a & m_port_a);

		break;

	case 1:
		data = m_in_b;

		if (!m_in_pb_cb.isnull())
			data &= m_in_pb_cb(0);

		data = (data & ~m_ddr_b) | (m_ddr_b & m_port_b);

		break;

	case 2:
		if (INTERRUPT_MODE)
		{
			data = 0;

			if (m_irq_level[0]) data |= 0x01;
			if (m_irq_level[1]) data |= 0x02;
			if (m_irq_level[2]) data |= 0x04;
			if (m_irq_level[3]) data |= 0x08;
			if (m_irq_level[4]) data |= 0x10;
			if (!m_interrupt_level) data |= 0x20;
			if (m_ca_level) data |= 0x40;
			if (m_cb_level) data |= 0x80;
		}
		else
		{
			data = m_in_c;

			if (!m_in_pc_cb.isnull())
				data &= m_in_pc_cb(0);

			data = (data & ~m_ddr_c) | (m_ddr_c & m_port_c);
		}

		DBG_LOG(machine(), 2, "tpi6525", ("%s read %.2x %.2x\n", tag().c_str(), offset, data));
		break;

	case 3:
		data = m_ddr_a;
		break;

	case 4:
		data = m_ddr_b;
		break;

	case 5:
		data = m_ddr_c;
		break;

	case 6:
		data = m_cr;
		break;

	case 7: /* air */
		if (PRIORIZED_INTERRUPTS)
		{
			if (m_air & 0x10)
			{
				data = 0x10;
				m_air &= ~0x10;
			}
			else if (m_air & 8)
			{
				data = 8;
				m_air &= ~8;
			}
			else if (m_air & 4)
			{
				data = 4;
				m_air &= ~4;
			}
			else if (m_air & 2)
			{
				data = 2;
				m_air &= ~2;
			}
			else if (m_air & 1)
			{
				data = 1;
				m_air &= ~1;
			}
		}
		else
		{
			data = m_air;
			m_air = 0;
		}

		clear_interrupt();
		break;

	}

	DBG_LOG(machine(), 3, "tpi6525", ("%s read %.2x %.2x\n", tag().c_str(), offset, data));

	return data;
}


WRITE8_MEMBER( tpi6525_device::write )
{
	DBG_LOG(machine(), 2, "tpi6525", ("%s write %.2x %.2x\n", tag().c_str(), offset, data));

	switch (offset & 7)
	{
	case 0:
		m_port_a = data;
		m_out_pa_cb((offs_t)0, (m_port_a & m_ddr_a) | (m_ddr_a ^ 0xff));
		break;

	case 1:
		m_port_b = data;
		m_out_pb_cb((offs_t)0, (m_port_b & m_ddr_b) | (m_ddr_b ^ 0xff));
		break;

	case 2:
		m_port_c = data;

		if (!INTERRUPT_MODE)
			m_out_pc_cb((offs_t)0, (m_port_c & m_ddr_c) | (m_ddr_c ^ 0xff));
		break;

	case 3:
		m_ddr_a = data;
		m_out_pa_cb((offs_t)0, (m_port_a & m_ddr_a) | (m_ddr_a ^ 0xff));
		break;

	case 4:
		m_ddr_b = data;
		m_out_pb_cb((offs_t)0, (m_port_b & m_ddr_b) | (m_ddr_b ^ 0xff));
		break;

	case 5:
		m_ddr_c = data;

		if (!INTERRUPT_MODE)
			m_out_pc_cb((offs_t)0, (m_port_c & m_ddr_c) | (m_ddr_c ^ 0xff));
		break;

	case 6:
		m_cr = data;

		if (INTERRUPT_MODE)
		{
			if (CA_MANUAL_OUT)
			{
				if (m_ca_level != CA_MANUAL_LEVEL)
				{
					m_ca_level = CA_MANUAL_LEVEL;
					m_out_ca_cb(m_ca_level);
				}
			}
			if (CB_MANUAL_OUT)
			{
				if (m_cb_level != CB_MANUAL_LEVEL)
				{
					m_cb_level = CB_MANUAL_LEVEL;
					m_out_cb_cb(m_cb_level);
				}
			}
		}

		break;

	case 7:
		/* m_air = data; */
		break;
	}
}

void tpi6525_device::port_line_w(UINT8 &port, int line, int state)
{
	port &= ~(1 << line);
	port |= state << line;
}

/* this should probably be done better, needed for amigacd.c */

UINT8 tpi6525_device::get_ddr_a()
{
	return m_ddr_a;
}

UINT8 tpi6525_device::get_ddr_b()
{
	return m_ddr_b;
}

UINT8 tpi6525_device::get_ddr_c()
{
	return m_ddr_c;
}