~ubuntu-branches/debian/sid/mame/sid

« back to all changes in this revision

Viewing changes to mess/src/mess/machine/tms5501.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach, Jordi Mallach, Emmanuel Kasper
  • Date: 2011-12-19 22:56:27 UTC
  • mfrom: (0.1.2)
  • Revision ID: package-import@ubuntu.com-20111219225627-ub5oga1oys4ogqzm
Tags: 0.144-1
[ Jordi Mallach ]
* Fix syntax errors in DEP5 copyright file (lintian).
* Use a versioned copyright Format specification field.
* Update Vcs-* URLs.
* Move transitional packages to the new metapackages section, and make
  them priority extra.
* Remove references to GNU/Linux and MESS sources from copyright.
* Add build variables for s390x.
* Use .xz tarballs as it cuts 4MB for the upstream sources.
* Add nplayers.ini as a patch. Update copyright file to add CC-BY-SA-3.0.

[ Emmanuel Kasper ]
* New upstream release. Closes: #651538.
* Add Free Desktop compliant png icons of various sizes taken from
  the hydroxygen iconset
* Mess is now built from a new source package, to avoid possible source
  incompatibilities between mame and the mess overlay.
* Mame-tools are not built from the mame source package anymore, but
  from the mess source package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*********************************************************************
2
 
 
3
 
    tms5501.c
4
 
 
5
 
    TMS5501 input/output controller
6
 
 
7
 
    Krzysztof Strzecha, Nathan Woods, 2003
8
 
    Based on TMS9901 emulator by Raphael Nabet
9
 
 
10
 
    21-May-2004 -   Fixed interrupt queue overflow bug (not really fixed
11
 
            previously).
12
 
    06-Mar-2004 -   Fixed bug in sensor input.
13
 
    01-Mar-2004 -   Interrupt queue overrun problem fixed.
14
 
    19-Oct-2003 -   Status register added. Reset fixed. Some cleanups.
15
 
            INTA enable/disable.
16
 
 
17
 
    TODO:
18
 
    - SIO
19
 
 
20
 
*********************************************************************/
21
 
 
22
 
#include "emu.h"
23
 
#include "tms5501.h"
24
 
 
25
 
 
26
 
/***************************************************************************
27
 
    PARAMETERS/MACROS
28
 
***************************************************************************/
29
 
 
30
 
#define DEBUG_TMS5501   0
31
 
 
32
 
#define LOG_TMS5501(device, message, data) do { if (DEBUG_TMS5501) logerror ("\nTMS5501 %s: %s %02x", device->tag(), message, data); } while (0)
33
 
 
34
 
/* status register */
35
 
#define TMS5501_FRAME_ERROR             0x01
36
 
#define TMS5501_OVERRUN_ERROR           0x02
37
 
#define TMS5501_SERIAL_RCVD             0x04
38
 
#define TMS5501_RCV_BUFFER_LOADED       0x08
39
 
#define TMS5501_XMIT_BUFFER_EMPTY       0x10
40
 
#define TMS5501_INTERRUPT_PENDING       0x20
41
 
#define TMS5501_FULL_BIT_DETECT         0x40
42
 
#define TMS5501_START_BIT_DETECT        0x80
43
 
 
44
 
/* command */
45
 
#define TMS5501_RESET                   0x01
46
 
#define TMS5501_BREAK                   0x02
47
 
#define TMS5501_INT_7_SELECT            0x04
48
 
#define TMS5501_INT_ACK_ENABLE          0x08
49
 
#define TMS5501_TEST_BIT_1              0x10
50
 
#define TMS5501_TEST_BIT_2              0x20
51
 
#define TMS5501_COMMAND_LATCHED_BITS    0x3e
52
 
 
53
 
/* interrupt mask register */
54
 
#define TMS5501_TIMER_0_INT             0x01
55
 
#define TMS5501_TIMER_1_INT             0x02
56
 
#define TMS5501_SENSOR_INT              0x04
57
 
#define TMS5501_TIMER_2_INT             0x08
58
 
#define TMS5501_SERIAL_RCV_LOADED_INT   0x10
59
 
#define TMS5501_SERIAL_XMIT_EMPTY_INT   0x20
60
 
#define TMS5501_TIMER_3_INT             0x40
61
 
#define TMS5501_TIMER_4_INT             0x80
62
 
#define TMS5501_INT_7_INT               0x80
63
 
 
64
 
#define TMS5501_PIO_INT_7               0x80
65
 
 
66
 
 
67
 
/***************************************************************************
68
 
    TYPE DEFINITIONS
69
 
***************************************************************************/
70
 
 
71
 
typedef struct _tms5501_t tms5501_t;
72
 
struct _tms5501_t
73
 
{
74
 
        /* internal registers */
75
 
        UINT8 status;                   /* status register */
76
 
        UINT8 command;                  /* command register, bits 1-5 are latched */
77
 
 
78
 
        UINT8 sio_rate;                 /* SIO configuration register */
79
 
        UINT8 sio_input_buffer;         /* SIO input buffer */
80
 
        UINT8 sio_output_buffer;        /* SIO output buffer */
81
 
 
82
 
        UINT8 pio_input_buffer;         /* PIO input buffer */
83
 
        UINT8 pio_output_buffer;        /* PIO output buffer */
84
 
 
85
 
        UINT8 interrupt_mask;           /* interrupt mask register */
86
 
        UINT8 pending_interrupts;       /* pending interrupts register */
87
 
        UINT8 interrupt_address;        /* interrupt vector register */
88
 
 
89
 
        UINT8 sensor;                   /* sensor input */
90
 
 
91
 
        /* internal timers */
92
 
        UINT8 timer_counter[5];
93
 
        emu_timer * timer[5];
94
 
};
95
 
 
96
 
 
97
 
 
98
 
/***************************************************************************
99
 
    INLINE FUNCTIONS
100
 
***************************************************************************/
101
 
 
102
 
INLINE tms5501_t *get_token(device_t *device)
103
 
{
104
 
        assert(device != NULL);
105
 
        assert(device->type() == TMS5501);
106
 
        return (tms5501_t *) downcast<legacy_device_base *>(device)->token();
107
 
}
108
 
 
109
 
 
110
 
INLINE const tms5501_interface *get_interface(device_t *device)
111
 
{
112
 
        assert(device != NULL);
113
 
        assert(device->type() == TMS5501);
114
 
        return (const tms5501_interface *) device->static_config();
115
 
}
116
 
 
117
 
static const UINT8 timer_name[] = { TMS5501_TIMER_0_INT, TMS5501_TIMER_1_INT, TMS5501_TIMER_2_INT, TMS5501_TIMER_3_INT, TMS5501_TIMER_4_INT };
118
 
 
119
 
/***************************************************************************
120
 
    IMPLEMENTATION
121
 
***************************************************************************/
122
 
 
123
 
/*-------------------------------------------------
124
 
    find_first_bit
125
 
-------------------------------------------------*/
126
 
 
127
 
static int find_first_bit(int value)
128
 
{
129
 
        int bit = 0;
130
 
 
131
 
        if (! value)
132
 
                return -1;
133
 
 
134
 
        while (! (value & 1))
135
 
        {
136
 
                value >>= 1;    /* try next bit */
137
 
                bit++;
138
 
        }
139
 
        return bit;
140
 
}
141
 
 
142
 
 
143
 
 
144
 
/*-------------------------------------------------
145
 
    tms5501_field_interrupts
146
 
-------------------------------------------------*/
147
 
 
148
 
static void tms5501_field_interrupts(device_t *device)
149
 
{
150
 
        static const UINT8 int_vectors[] = { 0xc7, 0xcf, 0xd7, 0xdf, 0xe7, 0xef, 0xf7, 0xff };
151
 
 
152
 
        tms5501_t *tms = get_token(device);
153
 
        const tms5501_interface *intf = get_interface(device);
154
 
        UINT8 current_ints = tms->pending_interrupts;
155
 
 
156
 
        /* disabling masked interrupts */
157
 
        current_ints &= tms->interrupt_mask;
158
 
 
159
 
        LOG_TMS5501(device, "Pending interrupts", tms->pending_interrupts);
160
 
        LOG_TMS5501(device, "Interrupt mask", tms->interrupt_mask);
161
 
        LOG_TMS5501(device, "Current interrupts", current_ints);
162
 
 
163
 
        if (current_ints)
164
 
        {
165
 
                /* selecting interrupt with highest priority */
166
 
                int level = find_first_bit(current_ints);
167
 
                LOG_TMS5501(device, "Interrupt level", level);
168
 
 
169
 
                /* resetting proper bit in pending interrupts register */
170
 
                tms->pending_interrupts &= ~(1<<level);
171
 
 
172
 
                /* selecting  interrupt vector */
173
 
                tms->interrupt_address = int_vectors[level];
174
 
                LOG_TMS5501(device, "Interrupt vector", int_vectors[level]);
175
 
 
176
 
                if ((tms->command & TMS5501_INT_ACK_ENABLE))
177
 
                {
178
 
                        if (intf->interrupt_callback)
179
 
                                (*intf->interrupt_callback)(device, 1, int_vectors[level]);
180
 
                }
181
 
                else
182
 
                        tms->status |= TMS5501_INTERRUPT_PENDING;
183
 
        }
184
 
        else
185
 
        {
186
 
                if ((tms->command & TMS5501_INT_ACK_ENABLE))
187
 
                {
188
 
                        if (intf->interrupt_callback)
189
 
                                (*intf->interrupt_callback)(device, 0, 0);
190
 
                }
191
 
                else
192
 
                        tms->status &= ~TMS5501_INTERRUPT_PENDING;
193
 
        }
194
 
}
195
 
 
196
 
 
197
 
/*-------------------------------------------------
198
 
    tms5501_timer_decrementer
199
 
-------------------------------------------------*/
200
 
 
201
 
static void tms5501_timer_decrementer(device_t *device, UINT8 mask)
202
 
{
203
 
        tms5501_t *tms = get_token(device);
204
 
 
205
 
        if ((mask != TMS5501_TIMER_4_INT) || ((mask == TMS5501_TIMER_4_INT) && (!(tms->command & TMS5501_INT_7_SELECT))))
206
 
                tms->pending_interrupts |= mask;
207
 
 
208
 
        tms5501_field_interrupts(device);
209
 
}
210
 
 
211
 
 
212
 
/*-------------------------------------------------
213
 
    TIMER_CALLBACK(tms5501_timer_decrementer_callback)
214
 
-------------------------------------------------*/
215
 
 
216
 
static TIMER_CALLBACK(tms5501_timer_decrementer_callback)
217
 
{
218
 
        device_t *device = (device_t *) ptr;
219
 
        UINT8 mask = param;
220
 
 
221
 
        tms5501_timer_decrementer(device, mask);
222
 
}
223
 
 
224
 
 
225
 
/*-------------------------------------------------
226
 
    tms5501_timer_reload
227
 
-------------------------------------------------*/
228
 
 
229
 
static void tms5501_timer_reload(device_t *device, int timer)
230
 
{
231
 
        tms5501_t *tms = get_token(device);
232
 
        const tms5501_interface *intf = get_interface(device);
233
 
 
234
 
        if (tms->timer_counter[timer])
235
 
        {       /* reset clock interval */
236
 
                tms->timer[timer]->adjust(attotime::from_double((double) tms->timer_counter[0] / (intf->clock_rate / 128.)), timer_name[timer], attotime::from_double((double) tms->timer_counter[timer] / (intf->clock_rate / 128.)));
237
 
        }
238
 
        else
239
 
        {       /* clock interval == 0 -> no timer */
240
 
                switch (timer)
241
 
                {
242
 
                        case 0: tms5501_timer_decrementer(device, TMS5501_TIMER_0_INT); break;
243
 
                        case 1: tms5501_timer_decrementer(device, TMS5501_TIMER_1_INT); break;
244
 
                        case 2: tms5501_timer_decrementer(device, TMS5501_TIMER_2_INT); break;
245
 
                        case 3: tms5501_timer_decrementer(device, TMS5501_TIMER_3_INT); break;
246
 
                        case 4: tms5501_timer_decrementer(device, TMS5501_TIMER_4_INT); break;
247
 
                }
248
 
                tms->timer[timer]->enable(0);
249
 
        }
250
 
}
251
 
 
252
 
 
253
 
/*-------------------------------------------------
254
 
    DEVICE_RESET( tms5501 )
255
 
-------------------------------------------------*/
256
 
 
257
 
static DEVICE_RESET( tms5501 )
258
 
{
259
 
        tms5501_t *tms = get_token(device);
260
 
        int i;
261
 
 
262
 
        tms->status &= ~(TMS5501_RCV_BUFFER_LOADED|TMS5501_FULL_BIT_DETECT|TMS5501_START_BIT_DETECT|TMS5501_OVERRUN_ERROR);
263
 
        tms->status |= TMS5501_XMIT_BUFFER_EMPTY|TMS5501_SERIAL_RCVD;
264
 
 
265
 
        tms->pending_interrupts = TMS5501_SERIAL_XMIT_EMPTY_INT;
266
 
 
267
 
        for (i=0; i<5; i++)
268
 
        {
269
 
                tms->timer_counter[i] = 0;
270
 
                tms->timer[i]->enable(0);
271
 
        }
272
 
 
273
 
        LOG_TMS5501(device, "Reset", 0);
274
 
}
275
 
 
276
 
 
277
 
/*-------------------------------------------------
278
 
    DEVICE_START( tms5501 )
279
 
-------------------------------------------------*/
280
 
 
281
 
static DEVICE_START( tms5501 )
282
 
{
283
 
        int i;
284
 
        tms5501_t *tms = get_token(device);
285
 
 
286
 
        for (i = 0; i < 5; i++)
287
 
        {
288
 
                tms->timer[i] = device->machine().scheduler().timer_alloc(FUNC(tms5501_timer_decrementer_callback), (void *) device);
289
 
                tms->timer[i]->set_param(i);
290
 
        }
291
 
 
292
 
        tms->interrupt_mask = 0;
293
 
        tms->interrupt_address = 0;
294
 
 
295
 
        tms->sensor = 0;
296
 
        tms->sio_rate = 0;
297
 
        tms->sio_input_buffer = 0;
298
 
        tms->sio_output_buffer = 0;
299
 
        tms->pio_input_buffer = 0;
300
 
        tms->pio_output_buffer = 0;
301
 
 
302
 
        tms->command = 0;
303
 
        LOG_TMS5501(device, "Init", 0);
304
 
}
305
 
 
306
 
 
307
 
/*-------------------------------------------------
308
 
    tms5501_set_pio_bit_7
309
 
-------------------------------------------------*/
310
 
 
311
 
void tms5501_set_pio_bit_7 (device_t *device, UINT8 data)
312
 
{
313
 
        tms5501_t *tms = get_token(device);
314
 
 
315
 
        if (tms->command & TMS5501_INT_7_SELECT)
316
 
        {
317
 
                if (!(tms->pio_input_buffer & TMS5501_PIO_INT_7) && data)
318
 
                        tms->pending_interrupts |= TMS5501_INT_7_INT;
319
 
                else
320
 
                        tms->pending_interrupts &= ~TMS5501_INT_7_INT;
321
 
        }
322
 
 
323
 
        tms->pio_input_buffer &= ~TMS5501_PIO_INT_7;
324
 
        if (data)
325
 
                tms->pio_input_buffer |= TMS5501_PIO_INT_7;
326
 
 
327
 
        if (tms->pending_interrupts & TMS5501_INT_7_INT)
328
 
                tms5501_field_interrupts(device);
329
 
}
330
 
 
331
 
 
332
 
/*-------------------------------------------------
333
 
    tms5501_sensor
334
 
-------------------------------------------------*/
335
 
 
336
 
void tms5501_sensor (device_t *device, UINT8 data)
337
 
{
338
 
        tms5501_t *tms = get_token(device);
339
 
 
340
 
        if (!(tms->sensor) && data)
341
 
                tms->pending_interrupts |= TMS5501_SENSOR_INT;
342
 
        else
343
 
                tms->pending_interrupts &= ~TMS5501_SENSOR_INT;
344
 
 
345
 
        tms->sensor = data;
346
 
 
347
 
        if (tms->pending_interrupts &= TMS5501_SENSOR_INT)
348
 
                tms5501_field_interrupts(device);
349
 
}
350
 
 
351
 
 
352
 
/*-------------------------------------------------
353
 
    READ8_DEVICE_HANDLER( tms5501_r )
354
 
-------------------------------------------------*/
355
 
 
356
 
READ8_DEVICE_HANDLER( tms5501_r )
357
 
{
358
 
        tms5501_t *tms = get_token(device);
359
 
        const tms5501_interface *intf = get_interface(device);
360
 
 
361
 
        UINT8 data = 0x00;
362
 
        offset &= 0x0f;
363
 
 
364
 
        switch (offset)
365
 
        {
366
 
                case 0x00:      /* Serial input buffer */
367
 
                        data = tms->sio_input_buffer;
368
 
                        tms->status &= ~TMS5501_RCV_BUFFER_LOADED;
369
 
                        LOG_TMS5501(device, "Reading from serial input buffer", data);
370
 
                        break;
371
 
                case 0x01:      /* PIO input port */
372
 
                        if (intf->pio_read_callback)
373
 
                                data = (*intf->pio_read_callback)(device);
374
 
                        LOG_TMS5501(device, "Reading from PIO", data);
375
 
                        break;
376
 
                case 0x02:      /* Interrupt address register */
377
 
                        data = tms->interrupt_address;
378
 
                        tms->status &= ~TMS5501_INTERRUPT_PENDING;
379
 
                        break;
380
 
                case 0x03:      /* Status register */
381
 
                        data = tms->status;
382
 
                        break;
383
 
                case 0x04:      /* Command register */
384
 
                        data = tms->command;
385
 
                        LOG_TMS5501(device, "Command register read", data);
386
 
                        break;
387
 
                case 0x05:      /* Serial rate register */
388
 
                        data = tms->sio_rate;
389
 
                        LOG_TMS5501(device, "Serial rate read", data);
390
 
                        break;
391
 
                case 0x06:      /* Serial output buffer */
392
 
                case 0x07:      /* PIO output */
393
 
                        break;
394
 
                case 0x08:      /* Interrupt mask register */
395
 
                        data = tms->interrupt_mask;
396
 
                        LOG_TMS5501(device, "Interrupt mask read", data);
397
 
                        break;
398
 
                case 0x09:      /* Timer 0 address */
399
 
                case 0x0a:      /* Timer 1 address */
400
 
                case 0x0b:      /* Timer 2 address */
401
 
                case 0x0c:      /* Timer 3 address */
402
 
                case 0x0d:      /* Timer 4 address */
403
 
                        break;
404
 
        }
405
 
        return data;
406
 
}
407
 
 
408
 
 
409
 
/*-------------------------------------------------
410
 
    WRITE8_DEVICE_HANDLER( tms5501_w )
411
 
-------------------------------------------------*/
412
 
 
413
 
WRITE8_DEVICE_HANDLER( tms5501_w )
414
 
{
415
 
        tms5501_t *tms = get_token(device);
416
 
        const tms5501_interface *intf = get_interface(device);
417
 
        offset &= 0x0f;
418
 
 
419
 
        switch (offset)
420
 
        {
421
 
                case 0x00:      /* Serial input buffer */
422
 
                case 0x01:      /* Keyboard input port, Page blanking signal */
423
 
                case 0x02:      /* Interrupt address register */
424
 
                case 0x03:      /* Status register */
425
 
                        LOG_TMS5501(device, "Writing to read only port", offset&0x000f);
426
 
                        LOG_TMS5501(device, "Data", data);
427
 
                        break;
428
 
                case 0x04:
429
 
                        /* Command register
430
 
                bit 0: reset
431
 
                bit 1: send break, '1' - serial output is high impedance
432
 
                bit 2: int 7 select: '0' - timer 5, '1' - IN7 of the DCE-bus
433
 
                bit 3: int ack enable, '0' - disabled, '1' - enabled
434
 
                bits 4-5: test bits, normally '0'
435
 
                bits 6-7: not used, normally '0'
436
 
               bits 1-5 are latched */
437
 
 
438
 
                        tms->command = data & TMS5501_COMMAND_LATCHED_BITS;
439
 
                        LOG_TMS5501(device, "Command register write", data);
440
 
 
441
 
                        if (data & TMS5501_RESET)
442
 
                                device->reset();
443
 
                        break;
444
 
                case 0x05:
445
 
                        /* Serial rate register
446
 
                bit 0: 110 baud
447
 
                bit 1: 150 baud
448
 
                bit 2: 300 baud
449
 
                bit 3: 1200 baud
450
 
                bit 4: 2400 baud
451
 
                bit 5: 4800 baud
452
 
                bit 6: 9600 baud
453
 
                bit 7: '0' - two stop bits, '1' - one stop bit */
454
 
 
455
 
                        tms->sio_rate = data;
456
 
                        LOG_TMS5501(device, "Serial rate write", data);
457
 
                        break;
458
 
                case 0x06:      /* Serial output buffer */
459
 
                        tms->sio_output_buffer = data;
460
 
                        LOG_TMS5501(device, "Serial output data", data);
461
 
                        break;
462
 
                case 0x07:      /* PIO output */
463
 
                        tms->pio_output_buffer = data;
464
 
                        if (intf->pio_write_callback)
465
 
                                (*intf->pio_write_callback)(device, tms->pio_output_buffer);
466
 
                        LOG_TMS5501(device, "Writing to PIO", data);
467
 
                        break;
468
 
                case 0x08:
469
 
                        /* Interrupt mask register
470
 
                bit 0: Timer 1 has expired (UTIM)
471
 
                bit 1: Timer 2 has expired
472
 
                bit 2: External interrupt (STKIM)
473
 
                bit 3: Timer 3 has expired (SNDIM)
474
 
                bit 4: Serial receiver loaded
475
 
                bit 5: Serial transmitter empty
476
 
                bit 6: Timer 4 has expired (KBIM)
477
 
                bit 7: Timer 5 has expired or IN7 (CLKIM) */
478
 
 
479
 
                        tms->interrupt_mask = data;
480
 
                        LOG_TMS5501(device, "Interrupt mask write", data);
481
 
                        break;
482
 
                case 0x09:      /* Timer 0 counter */
483
 
                case 0x0a:      /* Timer 1 counter */
484
 
                case 0x0b:      /* Timer 2 counter */
485
 
                case 0x0c:      /* Timer 3 counter */
486
 
                case 0x0d:      /* Timer 4 counter */
487
 
                        offset -= 9;
488
 
                        tms->timer_counter[offset] = data;
489
 
                        tms5501_timer_reload(device, offset);
490
 
                        LOG_TMS5501(device, "Write timer", offset);
491
 
                        LOG_TMS5501(device, "Timer counter set", data);
492
 
                        break;
493
 
        }
494
 
}
495
 
 
496
 
 
497
 
/*-------------------------------------------------
498
 
    DEVICE_GET_INFO( tms5501 )
499
 
-------------------------------------------------*/
500
 
 
501
 
DEVICE_GET_INFO( tms5501 )
502
 
{
503
 
        switch (state)
504
 
        {
505
 
                /* --- the following bits of info are returned as 64-bit signed integers --- */
506
 
                case DEVINFO_INT_TOKEN_BYTES:                                   info->i = sizeof(tms5501_t);                            break;
507
 
                case DEVINFO_INT_INLINE_CONFIG_BYTES:                   info->i = 0;                                                            break;
508
 
 
509
 
                /* --- the following bits of info are returned as pointers to data or functions --- */
510
 
                case DEVINFO_FCT_START:                                                 info->start = DEVICE_START_NAME(tms5501);       break;
511
 
                case DEVINFO_FCT_STOP:                                                  /* Nothing */                                                           break;
512
 
                case DEVINFO_FCT_RESET:                                                 info->reset = DEVICE_RESET_NAME(tms5501);       break;
513
 
 
514
 
                /* --- the following bits of info are returned as NULL-terminated strings --- */
515
 
                case DEVINFO_STR_NAME:                                                  strcpy(info->s, "TMS5501");                                     break;
516
 
                case DEVINFO_STR_FAMILY:                                                strcpy(info->s, "TMS5501");                                     break;
517
 
                case DEVINFO_STR_VERSION:                                               strcpy(info->s, "1.0");                                         break;
518
 
                case DEVINFO_STR_SOURCE_FILE:                                   strcpy(info->s, __FILE__);                                      break;
519
 
                case DEVINFO_STR_CREDITS:                                               /* Nothing */                                                           break;
520
 
        }
521
 
}
522
 
 
523
 
DEFINE_LEGACY_DEVICE(TMS5501, tms5501);