Previous 15994 Revisions Next

r7537 Monday 8th March, 2010 at 16:50:19 UTC by Curt Coder
Moved Z80 DART and Z80 STI to MAME.
[src/emu]emu.mak
[src/emu/machine]z80dart.c* z80dart.h* z80sti.c* z80sti.h*
[src/mess]mess.mak
[src/mess/machine]z80dart.c z80dart.h z80sti.c z80sti.h

src/emu/emu.mak
r7536r7537
179179   $(EMUMACHINE)/x76f041.o \
180180   $(EMUMACHINE)/x76f100.o \
181181   $(EMUMACHINE)/z80ctc.o \
182   $(EMUMACHINE)/z80dart.o \
182183   $(EMUMACHINE)/z80dma.o \
183184   $(EMUMACHINE)/z80pio.o \
184185   $(EMUMACHINE)/z80sio.o \
186   $(EMUMACHINE)/z80sti.o \
185187
186188EMUVIDEOOBJS = \
187189   $(EMUVIDEO)/generic.o \
src/emu/machine/z80sti.c
r0r7537
1/***************************************************************************
2
3    Mostek MK3801 Serial Timer Interrupt Controller (Z80-STI) emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10/*
11
12    TODO:
13
14    - timers (other than delay mode)
15    - serial I/O
16    - reset behavior
17
18*/
19
20#include "emu.h"
21#include "z80sti.h"
22#include "cpu/z80/z80.h"
23#include "cpu/z80/z80daisy.h"
24
25/***************************************************************************
26    PARAMETERS
27***************************************************************************/
28
29#define VERBOSE 0
30
31#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
32
33/* registers */
34enum
35{
36   Z80STI_REGISTER_IR = 0,
37   Z80STI_REGISTER_GPIP,
38   Z80STI_REGISTER_IPRB,
39   Z80STI_REGISTER_IPRA,
40   Z80STI_REGISTER_ISRB,
41   Z80STI_REGISTER_ISRA,
42   Z80STI_REGISTER_IMRB,
43   Z80STI_REGISTER_IMRA,
44   Z80STI_REGISTER_PVR,
45   Z80STI_REGISTER_TABC,
46   Z80STI_REGISTER_TBDR,
47   Z80STI_REGISTER_TADR,
48   Z80STI_REGISTER_UCR,
49   Z80STI_REGISTER_RSR,
50   Z80STI_REGISTER_TSR,
51   Z80STI_REGISTER_UDR
52};
53
54/* variable registers */
55enum
56{
57   Z80STI_REGISTER_IR_SCR = 0,
58   Z80STI_REGISTER_IR_TDDR,
59   Z80STI_REGISTER_IR_TCDR,
60   Z80STI_REGISTER_IR_AER,
61   Z80STI_REGISTER_IR_IERB,
62   Z80STI_REGISTER_IR_IERA,
63   Z80STI_REGISTER_IR_DDR,
64   Z80STI_REGISTER_IR_TCDC
65};
66
67/* timers */
68enum
69{
70   TIMER_A = 0,
71   TIMER_B,
72   TIMER_C,
73   TIMER_D,
74   TIMER_COUNT
75};
76
77/* interrupt levels */
78enum
79{
80   Z80STI_IR_P0 = 0,
81   Z80STI_IR_P1,
82   Z80STI_IR_P2,
83   Z80STI_IR_P3,
84   Z80STI_IR_TD,
85   Z80STI_IR_TC,
86   Z80STI_IR_P4,
87   Z80STI_IR_P5,
88   Z80STI_IR_TB,
89   Z80STI_IR_XE,
90   Z80STI_IR_XB,
91   Z80STI_IR_RE,
92   Z80STI_IR_RB,
93   Z80STI_IR_TA,
94   Z80STI_IR_P6,
95   Z80STI_IR_P7
96};
97
98/* timer C/D control register */
99#define Z80STI_TCDC_TARS   0x80
100#define Z80STI_TCDC_TBRS   0x08
101
102/* interrupt vector register */
103#define Z80STI_PVR_ISE      0x08
104#define Z80STI_PVR_VR4      0x10
105
106/* general purpose I/O interrupt levels */
107static const int INT_LEVEL_GPIP[] =
108{
109   Z80STI_IR_P0, Z80STI_IR_P1, Z80STI_IR_P2, Z80STI_IR_P3,   Z80STI_IR_P4, Z80STI_IR_P5, Z80STI_IR_P6, Z80STI_IR_P7
110};
111
112/* timer interrupt levels */
113static const int INT_LEVEL_TIMER[] =
114{
115   Z80STI_IR_TA, Z80STI_IR_TB, Z80STI_IR_TC, Z80STI_IR_TD
116};
117
118/* interrupt vectors */
119static const UINT8 INT_VECTOR[] =
120{
121   0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
122   0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e
123};
124
125/* timer prescaler divisors */
126static const int PRESCALER[] = { 0, 4, 10, 16, 50, 64, 100, 200 };
127
128/***************************************************************************
129    TYPE DEFINITIONS
130***************************************************************************/
131
132typedef struct _z80sti_t z80sti_t;
133struct _z80sti_t
134{
135   /* device callbacks */
136   devcb_resolved_read8            in_gpio_func;
137   devcb_resolved_write8            out_gpio_func;
138   devcb_resolved_read_line         in_si_func;
139   devcb_resolved_write_line         out_so_func;
140   devcb_resolved_write_line         out_tao_func;
141   devcb_resolved_write_line         out_tbo_func;
142   devcb_resolved_write_line         out_tco_func;
143   devcb_resolved_write_line         out_tdo_func;
144   devcb_resolved_write_line         out_int_func;
145
146   /* I/O state */
147   UINT8 gpip;                     /* general purpose I/O register */
148   UINT8 aer;                     /* active edge register */
149   UINT8 ddr;                     /* data direction register */
150
151   /* interrupt state */
152   UINT16 ier;                     /* interrupt enable register */
153   UINT16 ipr;                     /* interrupt pending register */
154   UINT16 isr;                     /* interrupt in-service register */
155   UINT16 imr;                     /* interrupt mask register */
156   UINT8 pvr;                     /* interrupt vector register */
157   int int_state[16];               /* interrupt state */
158
159   /* timer state */
160   UINT8 tabc;                     /* timer A/B control register */
161   UINT8 tcdc;                     /* timer C/D control register */
162   UINT8 tdr[TIMER_COUNT];            /* timer data registers */
163   UINT8 tmc[TIMER_COUNT];            /* timer main counters */
164   int to[TIMER_COUNT];            /* timer out latch */
165
166   /* serial state */
167   UINT8 scr;                     /* synchronous character register */
168   UINT8 ucr;                     /* USART control register */
169   UINT8 tsr;                     /* transmitter status register */
170   UINT8 rsr;                     /* receiver status register */
171   UINT8 udr;                     /* USART data register */
172
173   /* timers */
174   emu_timer *timer[TIMER_COUNT];      /* counter timers */
175   emu_timer *rx_timer;            /* serial receive timer */
176   emu_timer *tx_timer;            /* serial transmit timer */
177};
178
179/***************************************************************************
180    FUNCTION PROTOTYPES
181***************************************************************************/
182
183static int z80sti_irq_state(running_device *device);
184static void z80sti_irq_reti(running_device *device);
185
186/***************************************************************************
187    INLINE FUNCTIONS
188***************************************************************************/
189
190INLINE z80sti_t *get_safe_token(running_device *device)
191{
192   assert(device != NULL);
193   assert(device->token != NULL);
194   assert(device->type == Z80STI);
195   return (z80sti_t *)device->token;
196}
197
198INLINE const z80sti_interface *get_interface(running_device *device)
199{
200   assert(device != NULL);
201   assert((device->type == Z80STI));
202   return (const z80sti_interface *) device->baseconfig().static_config;
203}
204
205/***************************************************************************
206    IMPLEMENTATION
207***************************************************************************/
208
209/*-------------------------------------------------
210    check_interrupts - set the interrupt request
211    line state
212-------------------------------------------------*/
213
214static void check_interrupts(z80sti_t *z80sti)
215{
216   if (z80sti->ipr & z80sti->imr)
217   {
218      devcb_call_write_line(&z80sti->out_int_func, ASSERT_LINE);
219   }
220   else
221   {
222      devcb_call_write_line(&z80sti->out_int_func, CLEAR_LINE);
223   }
224}
225
226/*-------------------------------------------------
227    take_interrupt - mark an interrupt pending
228-------------------------------------------------*/
229
230static void take_interrupt(z80sti_t *z80sti, int level)
231{
232   /* set interrupt pending register bit */
233   z80sti->ipr |= 1 << level;
234
235   /* trigger interrupt */
236   z80sti->int_state[level] |= Z80_DAISY_INT;
237
238   check_interrupts(z80sti);
239}
240
241/*-------------------------------------------------
242    serial_receive - receive serial bit
243-------------------------------------------------*/
244
245static void serial_receive(z80sti_t *z80sti)
246{
247}
248
249/*-------------------------------------------------
250    TIMER_CALLBACK( rx_tick )
251-------------------------------------------------*/
252
253static TIMER_CALLBACK( rx_tick )
254{
255   running_device *device = (running_device *)ptr;
256   z80sti_t *z80sti = get_safe_token(device);
257
258   serial_receive(z80sti);
259}
260
261/*-------------------------------------------------
262    serial_transmit - transmit serial bit
263-------------------------------------------------*/
264
265static void serial_transmit(z80sti_t *z80sti)
266{
267}
268
269/*-------------------------------------------------
270    TIMER_CALLBACK( tx_tick )
271-------------------------------------------------*/
272
273static TIMER_CALLBACK( tx_tick )
274{
275   running_device *device = (running_device *)ptr;
276   z80sti_t *z80sti = get_safe_token(device);
277
278   serial_transmit(z80sti);
279}
280
281/*-------------------------------------------------
282    z80sti_r - register read
283-------------------------------------------------*/
284
285READ8_DEVICE_HANDLER( z80sti_r )
286{
287   z80sti_t *z80sti = get_safe_token(device);
288
289   switch (offset & 0x0f)
290   {
291   case Z80STI_REGISTER_IR:
292      switch (z80sti->pvr & 0x07)
293      {
294      case Z80STI_REGISTER_IR_SCR:   return z80sti->scr;
295      case Z80STI_REGISTER_IR_TDDR:   return z80sti->tmc[TIMER_D];
296      case Z80STI_REGISTER_IR_TCDR:   return z80sti->tmc[TIMER_C];
297      case Z80STI_REGISTER_IR_AER:   return z80sti->aer;
298      case Z80STI_REGISTER_IR_IERB:   return z80sti->ier & 0xff;
299      case Z80STI_REGISTER_IR_IERA:   return z80sti->ier >> 8;
300      case Z80STI_REGISTER_IR_DDR:   return z80sti->ddr;
301      case Z80STI_REGISTER_IR_TCDC:   return z80sti->tcdc;
302      }
303      break;
304
305   case Z80STI_REGISTER_GPIP:   z80sti->gpip = (devcb_call_read8(&z80sti->in_gpio_func, 0) & ~z80sti->ddr) | (z80sti->gpip & z80sti->ddr); return z80sti->gpip;
306   case Z80STI_REGISTER_IPRB:   return z80sti->ipr & 0xff;
307   case Z80STI_REGISTER_IPRA:   return z80sti->ipr >> 8;
308   case Z80STI_REGISTER_ISRB:   return z80sti->isr & 0xff;
309   case Z80STI_REGISTER_ISRA:   return z80sti->isr >> 8;
310   case Z80STI_REGISTER_IMRB:   return z80sti->imr & 0xff;
311   case Z80STI_REGISTER_IMRA:   return z80sti->imr >> 8;
312   case Z80STI_REGISTER_PVR:   return z80sti->pvr;
313   case Z80STI_REGISTER_TABC:   return z80sti->tabc;
314   case Z80STI_REGISTER_TBDR:   return z80sti->tmc[TIMER_B];
315   case Z80STI_REGISTER_TADR:   return z80sti->tmc[TIMER_A];
316   case Z80STI_REGISTER_UCR:   return z80sti->ucr;
317   case Z80STI_REGISTER_RSR:   return z80sti->rsr;
318   case Z80STI_REGISTER_TSR:   return z80sti->tsr;
319   case Z80STI_REGISTER_UDR:   return z80sti->udr;
320   }
321
322   return 0;
323}
324
325/*-------------------------------------------------
326    z80sti_w - register write
327-------------------------------------------------*/
328
329WRITE8_DEVICE_HANDLER( z80sti_w )
330{
331   z80sti_t *z80sti = get_safe_token(device);
332
333   switch (offset & 0x0f)
334   {
335   case Z80STI_REGISTER_IR:
336      switch (z80sti->pvr & 0x07)
337      {
338      case Z80STI_REGISTER_IR_SCR:
339         LOG(("Z80STI '%s' Sync Character Register: %x\n", device->tag.cstr(), data));
340         z80sti->scr = data;
341         break;
342
343      case Z80STI_REGISTER_IR_TDDR:
344         LOG(("Z80STI '%s' Timer D Data Register: %x\n", device->tag.cstr(), data));
345         z80sti->tdr[TIMER_D] = data;
346         break;
347
348      case Z80STI_REGISTER_IR_TCDR:
349         LOG(("Z80STI '%s' Timer C Data Register: %x\n", device->tag.cstr(), data));
350         z80sti->tdr[TIMER_C] = data;
351         break;
352
353      case Z80STI_REGISTER_IR_AER:
354         LOG(("Z80STI '%s' Active Edge Register: %x\n", device->tag.cstr(), data));
355         z80sti->aer = data;
356         break;
357
358      case Z80STI_REGISTER_IR_IERB:
359         LOG(("Z80STI '%s' Interrupt Enable Register B: %x\n", device->tag.cstr(), data));
360         z80sti->ier = (z80sti->ier & 0xff00) | data;
361         check_interrupts(z80sti);
362         break;
363
364      case Z80STI_REGISTER_IR_IERA:
365         LOG(("Z80STI '%s' Interrupt Enable Register A: %x\n", device->tag.cstr(), data));
366         z80sti->ier = (data << 8) | (z80sti->ier & 0xff);
367         check_interrupts(z80sti);
368         break;
369
370      case Z80STI_REGISTER_IR_DDR:
371         LOG(("Z80STI '%s' Data Direction Register: %x\n", device->tag.cstr(), data));
372         z80sti->ddr = data;
373         break;
374
375      case Z80STI_REGISTER_IR_TCDC:
376         {
377         int tcc = PRESCALER[(data >> 4) & 0x07];
378         int tdc = PRESCALER[data & 0x07];
379
380         z80sti->tcdc = data;
381
382         LOG(("Z80STI '%s' Timer C Prescaler: %u\n", device->tag.cstr(), tcc));
383         LOG(("Z80STI '%s' Timer D Prescaler: %u\n", device->tag.cstr(), tdc));
384
385         if (tcc)
386            timer_adjust_periodic(z80sti->timer[TIMER_C], ATTOTIME_IN_HZ(device->clock / tcc), 0, ATTOTIME_IN_HZ(device->clock / tcc));
387         else
388            timer_enable(z80sti->timer[TIMER_C], 0);
389
390         if (tdc)
391            timer_adjust_periodic(z80sti->timer[TIMER_D], ATTOTIME_IN_HZ(device->clock / tdc), 0, ATTOTIME_IN_HZ(device->clock / tdc));
392         else
393            timer_enable(z80sti->timer[TIMER_D], 0);
394
395         if (BIT(data, 7))
396         {
397            LOG(("Z80STI '%s' Timer A Reset\n", device->tag.cstr()));
398            z80sti->to[TIMER_A] = 0;
399
400            devcb_call_write_line(&z80sti->out_tao_func, z80sti->to[TIMER_A]);
401         }
402
403         if (BIT(data, 3))
404         {
405            LOG(("Z80STI '%s' Timer B Reset\n", device->tag.cstr()));
406            z80sti->to[TIMER_B] = 0;
407
408            devcb_call_write_line(&z80sti->out_tbo_func, z80sti->to[TIMER_B]);
409         }
410         }
411         break;
412      }
413      break;
414
415   case Z80STI_REGISTER_GPIP:
416      LOG(("Z80STI '%s' General Purpose I/O Register: %x\n", device->tag.cstr(), data));
417      z80sti->gpip = data & z80sti->ddr;
418      devcb_call_write8(&z80sti->out_gpio_func, 0, z80sti->gpip);
419      break;
420
421   case Z80STI_REGISTER_IPRB:
422      {
423      int i;
424      LOG(("Z80STI '%s' Interrupt Pending Register B: %x\n", device->tag.cstr(), data));
425      z80sti->ipr &= (z80sti->ipr & 0xff00) | data;
426
427      for (i = 0; i < 16; i++)
428      {
429         if (!BIT(z80sti->ipr, i) && (z80sti->int_state[i] == Z80_DAISY_INT)) z80sti->int_state[i] = 0;
430      }
431
432      check_interrupts(z80sti);
433      }
434      break;
435
436   case Z80STI_REGISTER_IPRA:
437      {
438      int i;
439      LOG(("Z80STI '%s' Interrupt Pending Register A: %x\n", device->tag.cstr(), data));
440      z80sti->ipr &= (data << 8) | (z80sti->ipr & 0xff);
441
442      for (i = 0; i < 16; i++)
443      {
444         if (!BIT(z80sti->ipr, i) && (z80sti->int_state[i] == Z80_DAISY_INT)) z80sti->int_state[i] = 0;
445      }
446
447      check_interrupts(z80sti);
448      }
449      break;
450
451   case Z80STI_REGISTER_ISRB:
452      LOG(("Z80STI '%s' Interrupt In-Service Register B: %x\n", device->tag.cstr(), data));
453      z80sti->isr &= (z80sti->isr & 0xff00) | data;
454      break;
455
456   case Z80STI_REGISTER_ISRA:
457      LOG(("Z80STI '%s' Interrupt In-Service Register A: %x\n", device->tag.cstr(), data));
458      z80sti->isr &= (data << 8) | (z80sti->isr & 0xff);
459      break;
460
461   case Z80STI_REGISTER_IMRB:
462      LOG(("Z80STI '%s' Interrupt Mask Register B: %x\n", device->tag.cstr(), data));
463      z80sti->imr = (z80sti->imr & 0xff00) | data;
464      z80sti->isr &= z80sti->imr;
465      check_interrupts(z80sti);
466      break;
467
468   case Z80STI_REGISTER_IMRA:
469      LOG(("Z80STI '%s' Interrupt Mask Register A: %x\n", device->tag.cstr(), data));
470      z80sti->imr = (data << 8) | (z80sti->imr & 0xff);
471      z80sti->isr &= z80sti->imr;
472      check_interrupts(z80sti);
473      break;
474
475   case Z80STI_REGISTER_PVR:
476      LOG(("Z80STI '%s' Interrupt Vector: %02x\n", device->tag.cstr(), data & 0xe0));
477      LOG(("Z80STI '%s' IR Address: %01x\n", device->tag.cstr(), data & 0x07));
478      z80sti->pvr = data;
479      break;
480
481   case Z80STI_REGISTER_TABC:
482      {
483      int tac = PRESCALER[(data >> 4) & 0x07];
484      int tbc = PRESCALER[data & 0x07];
485
486      z80sti->tabc = data;
487
488      LOG(("Z80STI '%s' Timer A Prescaler: %u\n", device->tag.cstr(), tac));
489      LOG(("Z80STI '%s' Timer B Prescaler: %u\n", device->tag.cstr(), tbc));
490
491      if (tac)
492         timer_adjust_periodic(z80sti->timer[TIMER_A], ATTOTIME_IN_HZ(device->clock / tac), 0, ATTOTIME_IN_HZ(device->clock / tac));
493      else
494         timer_enable(z80sti->timer[TIMER_A], 0);
495
496      if (tbc)
497         timer_adjust_periodic(z80sti->timer[TIMER_B], ATTOTIME_IN_HZ(device->clock / tbc), 0, ATTOTIME_IN_HZ(device->clock / tbc));
498      else
499         timer_enable(z80sti->timer[TIMER_B], 0);
500      }
501      break;
502
503   case Z80STI_REGISTER_TBDR:
504      LOG(("Z80STI '%s' Timer B Data Register: %x\n", device->tag.cstr(), data));
505      z80sti->tdr[TIMER_B] = data;
506      break;
507
508   case Z80STI_REGISTER_TADR:
509      LOG(("Z80STI '%s' Timer A Data Register: %x\n", device->tag.cstr(), data));
510      z80sti->tdr[TIMER_A] = data;
511      break;
512#if 0
513    case Z80STI_REGISTER_UCR:
514        z80sti->ucr = data;
515        break;
516
517    case Z80STI_REGISTER_RSR:
518        z80sti->rsr = data;
519        break;
520
521    case Z80STI_REGISTER_TSR:
522        z80sti->tsr = data;
523        break;
524
525    case Z80STI_REGISTER_UDR:
526        z80sti->udr = data;
527        break;
528#endif
529   default:
530      LOG(("Z80STI '%s' Unsupported Register %x\n", device->tag.cstr(), offset & 0x0f));
531   }
532}
533
534/*-------------------------------------------------
535    z80sti_rc_w - receiver clock write
536-------------------------------------------------*/
537
538WRITE_LINE_DEVICE_HANDLER( z80sti_rc_w )
539{
540   z80sti_t *z80sti = get_safe_token(device);
541
542   if (state)
543   {
544      serial_receive(z80sti);
545   }
546}
547
548/*-------------------------------------------------
549    z80sti_tc_w - transmitter clock write
550-------------------------------------------------*/
551
552WRITE_LINE_DEVICE_HANDLER( z80sti_tc_w )
553{
554   z80sti_t *z80sti = get_safe_token(device);
555
556   if (state)
557   {
558      serial_transmit(z80sti);
559   }
560}
561
562/*-------------------------------------------------
563    timer_count - timer count down
564-------------------------------------------------*/
565
566static void timer_count(running_device *device, int index)
567{
568   z80sti_t *z80sti = get_safe_token(device);
569
570   if (z80sti->tmc[index] == 0x01)
571   {
572      //LOG(("Z80STI '%s' Timer %c Expired\n", device->tag, 'A' + index));
573
574      /* toggle timer output signal */
575      z80sti->to[index] = !z80sti->to[index];
576
577      switch (index)
578      {
579      case TIMER_A: devcb_call_write_line(&z80sti->out_tao_func, z80sti->to[index]); break;
580      case TIMER_B: devcb_call_write_line(&z80sti->out_tbo_func, z80sti->to[index]); break;
581      case TIMER_C: devcb_call_write_line(&z80sti->out_tco_func, z80sti->to[index]); break;
582      case TIMER_D: devcb_call_write_line(&z80sti->out_tdo_func, z80sti->to[index]); break;
583      }
584
585      if (z80sti->ier & (1 << INT_LEVEL_TIMER[index]))
586      {
587         LOG(("Z80STI '%s' Interrupt Pending for Timer %c\n", device->tag.cstr(), 'A' + index));
588
589         /* signal timer elapsed interrupt */
590         take_interrupt(z80sti, INT_LEVEL_TIMER[index]);
591      }
592
593      /* load timer main counter */
594      z80sti->tmc[index] = z80sti->tdr[index];
595   }
596   else
597   {
598      /* count down */
599      z80sti->tmc[index]--;
600   }
601}
602
603/*-------------------------------------------------
604    TIMER_CALLBACK( timer_# )
605-------------------------------------------------*/
606
607static TIMER_CALLBACK( timer_a ) { timer_count((running_device *)ptr, TIMER_A); }
608static TIMER_CALLBACK( timer_b ) { timer_count((running_device *)ptr, TIMER_B); }
609static TIMER_CALLBACK( timer_c ) { timer_count((running_device *)ptr, TIMER_C); }
610static TIMER_CALLBACK( timer_d ) { timer_count((running_device *)ptr, TIMER_D); }
611
612/*-------------------------------------------------
613    gpip_input - GPIP input line write
614-------------------------------------------------*/
615
616static void gpip_input(running_device *device, int bit, int state)
617{
618   z80sti_t *z80sti = get_safe_token(device);
619
620   int aer = BIT(z80sti->aer, bit);
621   int old_state = BIT(z80sti->gpip, bit);
622
623   if ((old_state ^ aer) && !(state ^ aer))
624   {
625      LOG(("Z80STI '%s' Edge Transition Detected on Bit: %u\n", device->tag.cstr(), bit));
626
627      if (z80sti->ier & (1 << INT_LEVEL_GPIP[bit]))
628      {
629         LOG(("Z80STI '%s' Interrupt Pending for P%u\n", device->tag.cstr(), bit));
630
631         take_interrupt(z80sti, INT_LEVEL_GPIP[bit]);
632      }
633   }
634
635   z80sti->gpip = (z80sti->gpip & ~(1 << bit)) | (state << bit);
636}
637
638/*-------------------------------------------------
639    z80sti_i#_w - GPIP input line # write
640-------------------------------------------------*/
641
642WRITE_LINE_DEVICE_HANDLER( z80sti_i0_w ) { gpip_input(device, 0, state); }
643WRITE_LINE_DEVICE_HANDLER( z80sti_i1_w ) { gpip_input(device, 1, state); }
644WRITE_LINE_DEVICE_HANDLER( z80sti_i2_w ) { gpip_input(device, 2, state); }
645WRITE_LINE_DEVICE_HANDLER( z80sti_i3_w ) { gpip_input(device, 3, state); }
646WRITE_LINE_DEVICE_HANDLER( z80sti_i4_w ) { gpip_input(device, 4, state); }
647WRITE_LINE_DEVICE_HANDLER( z80sti_i5_w ) { gpip_input(device, 5, state); }
648WRITE_LINE_DEVICE_HANDLER( z80sti_i6_w ) { gpip_input(device, 6, state); }
649WRITE_LINE_DEVICE_HANDLER( z80sti_i7_w ) { gpip_input(device, 7, state); }
650
651/*-------------------------------------------------
652    z80sti_irq_state - get interrupt status
653-------------------------------------------------*/
654
655static int z80sti_irq_state(running_device *device)
656{
657   z80sti_t *z80sti = get_safe_token(device);
658   int state = 0, i;
659
660   /* loop over all interrupt sources */
661   for (i = 15; i >= 0; i--)
662   {
663      /* if we're servicing a request, don't indicate more interrupts */
664      if (z80sti->int_state[i] & Z80_DAISY_IEO)
665      {
666         state |= Z80_DAISY_IEO;
667         break;
668      }
669
670      if (BIT(z80sti->imr, i))
671      {
672         state |= z80sti->int_state[i];
673      }
674   }
675
676   LOG(("Z80STI '%s' Interrupt State: %u\n", device->tag.cstr(), state));
677
678   return state;
679}
680
681/*-------------------------------------------------
682    z80sti_irq_ack - interrupt acknowledge
683-------------------------------------------------*/
684
685static int z80sti_irq_ack(running_device *device)
686{
687   z80sti_t *z80sti = get_safe_token(device);
688   int i;
689
690   /* loop over all interrupt sources */
691   for (i = 15; i >= 0; i--)
692   {
693      /* find the first channel with an interrupt requested */
694      if (z80sti->int_state[i] & Z80_DAISY_INT)
695      {
696         UINT8 vector = (z80sti->pvr & 0xe0) | INT_VECTOR[i];
697
698         /* clear interrupt, switch to the IEO state, and update the IRQs */
699         z80sti->int_state[i] = Z80_DAISY_IEO;
700
701         /* clear interrupt pending register bit */
702         z80sti->ipr &= ~(1 << i);
703
704         /* set interrupt in-service register bit */
705         z80sti->isr |= (1 << i);
706
707         check_interrupts(z80sti);
708
709         LOG(("Z80STI '%s' Interrupt Acknowledge Vector: %02x\n", device->tag.cstr(), vector));
710
711         return vector;
712      }
713   }
714
715   logerror("z80sti_irq_ack: failed to find an interrupt to ack!\n");
716
717   return 0;
718}
719
720/*-------------------------------------------------
721    z80sti_irq_reti - return from interrupt
722-------------------------------------------------*/
723
724static void z80sti_irq_reti(running_device *device)
725{
726   z80sti_t *z80sti = get_safe_token(device);
727   int i;
728
729   LOG(("Z80STI '%s' Return from Interrupt\n", device->tag.cstr()));
730
731   /* loop over all interrupt sources */
732   for (i = 15; i >= 0; i--)
733   {
734      /* find the first channel with an IEO pending */
735      if (z80sti->int_state[i] & Z80_DAISY_IEO)
736      {
737         /* clear the IEO state and update the IRQs */
738         z80sti->int_state[i] &= ~Z80_DAISY_IEO;
739
740         /* clear interrupt in-service register bit */
741         z80sti->isr &= ~(1 << i);
742
743         check_interrupts(z80sti);
744         return;
745      }
746   }
747
748   logerror("z80sti_irq_reti: failed to find an interrupt to clear IEO on!\n");
749}
750
751/*-------------------------------------------------
752    DEVICE_START( z80sti )
753-------------------------------------------------*/
754
755static DEVICE_START( z80sti )
756{
757   z80sti_t *z80sti = get_safe_token(device);
758   const z80sti_interface *intf = (const z80sti_interface *)device->baseconfig().static_config;
759
760   /* resolve callbacks */
761   devcb_resolve_read8(&z80sti->in_gpio_func, &intf->in_gpio_func, device);
762   devcb_resolve_write8(&z80sti->out_gpio_func, &intf->out_gpio_func, device);
763   devcb_resolve_read_line(&z80sti->in_si_func, &intf->in_si_func, device);
764   devcb_resolve_write_line(&z80sti->out_so_func, &intf->out_so_func, device);
765   devcb_resolve_write_line(&z80sti->out_tao_func, &intf->out_tao_func, device);
766   devcb_resolve_write_line(&z80sti->out_tbo_func, &intf->out_tbo_func, device);
767   devcb_resolve_write_line(&z80sti->out_tco_func, &intf->out_tco_func, device);
768   devcb_resolve_write_line(&z80sti->out_tdo_func, &intf->out_tdo_func, device);
769   devcb_resolve_write_line(&z80sti->out_int_func, &intf->out_int_func, device);
770
771   /* create the counter timers */
772   z80sti->timer[TIMER_A] = timer_alloc(device->machine, timer_a, (void *)device);
773   z80sti->timer[TIMER_B] = timer_alloc(device->machine, timer_b, (void *)device);
774   z80sti->timer[TIMER_C] = timer_alloc(device->machine, timer_c, (void *)device);
775   z80sti->timer[TIMER_D] = timer_alloc(device->machine, timer_d, (void *)device);
776
777   /* create serial receive clock timer */
778   if (intf->rx_clock > 0)
779   {
780      z80sti->rx_timer = timer_alloc(device->machine, rx_tick, (void *)device);
781      timer_adjust_periodic(z80sti->rx_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_clock));
782   }
783
784   /* create serial transmit clock timer */
785   if (intf->tx_clock > 0)
786   {
787      z80sti->tx_timer = timer_alloc(device->machine, tx_tick, (void *)device);
788      timer_adjust_periodic(z80sti->tx_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->tx_clock));
789   }
790
791   /* register for state saving */
792   state_save_register_device_item(device, 0, z80sti->gpip);
793   state_save_register_device_item(device, 0, z80sti->aer);
794   state_save_register_device_item(device, 0, z80sti->ddr);
795   state_save_register_device_item(device, 0, z80sti->ier);
796   state_save_register_device_item(device, 0, z80sti->ipr);
797   state_save_register_device_item(device, 0, z80sti->isr);
798   state_save_register_device_item(device, 0, z80sti->imr);
799   state_save_register_device_item(device, 0, z80sti->pvr);
800   state_save_register_device_item_array(device, 0, z80sti->int_state);
801   state_save_register_device_item(device, 0, z80sti->tabc);
802   state_save_register_device_item(device, 0, z80sti->tcdc);
803   state_save_register_device_item_array(device, 0, z80sti->tdr);
804   state_save_register_device_item_array(device, 0, z80sti->tmc);
805   state_save_register_device_item_array(device, 0, z80sti->to);
806   state_save_register_device_item(device, 0, z80sti->scr);
807   state_save_register_device_item(device, 0, z80sti->ucr);
808   state_save_register_device_item(device, 0, z80sti->rsr);
809   state_save_register_device_item(device, 0, z80sti->tsr);
810   state_save_register_device_item(device, 0, z80sti->udr);
811}
812
813/*-------------------------------------------------
814    DEVICE_RESET( z80sti )
815-------------------------------------------------*/
816
817static DEVICE_RESET( z80sti )
818{
819}
820
821/*-------------------------------------------------
822    DEVICE_GET_INFO( z80sti )
823-------------------------------------------------*/
824
825DEVICE_GET_INFO( z80sti )
826{
827   switch (state)
828   {
829      /* --- the following bits of info are returned as 64-bit signed integers --- */
830      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(z80sti_t);                  break;
831      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                           break;
832      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;            break;
833
834      /* --- the following bits of info are returned as pointers to data or functions --- */
835      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(z80sti);      break;
836      case DEVINFO_FCT_STOP:                     /* Nothing */                           break;
837      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(z80sti);      break;
838      case DEVINFO_FCT_IRQ_STATE:                  info->f = (genf *)z80sti_irq_state;            break;
839      case DEVINFO_FCT_IRQ_ACK:                  info->f = (genf *)z80sti_irq_ack;            break;
840      case DEVINFO_FCT_IRQ_RETI:                  info->f = (genf *)z80sti_irq_reti;            break;
841
842      /* --- the following bits of info are returned as NULL-terminated strings --- */
843      case DEVINFO_STR_NAME:                     strcpy(info->s, "Mostek MK3801");            break;
844      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Z80-STI");                  break;
845      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
846      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                  break;
847      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");      break;
848   }
849}
src/emu/machine/z80sti.h
r0r7537
1/**********************************************************************
2
3    Mostek MK3801 Serial Timer Interrupt Controller (Z80-STI) emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************
9                            _____   _____
10                   TAO   1 |*    \_/     | 40  Vcc
11                   TBO   2 |             | 39  RC
12                   TCO   3 |             | 38  SI
13                   TDO   4 |             | 37  SO
14                   TCK   5 |             | 36  TC
15                   _M1   6 |             | 35  A0
16                  _RES   7 |             | 34  A1
17                    I0   8 |             | 33  A2
18                    I1   9 |             | 32  A3
19                    I2  10 |   MK3801    | 31  _WR
20                    I3  11 |   Z80-STI   | 30  _CE
21                    I4  12 |             | 29  _RD
22                    I5  13 |             | 28  D7
23                    I6  14 |             | 27  D6
24                    I7  15 |             | 26  D5
25                   IEI  16 |             | 25  D4
26                  _INT  17 |             | 24  D3
27                   IEO  18 |             | 23  D2
28                 _IORQ  19 |             | 22  D1
29                   Vss  20 |_____________| 21  D0
30
31**********************************************************************/
32
33#ifndef __Z80STI__
34#define __Z80STI__
35
36#include "emu.h"
37#include "devcb.h"
38
39/***************************************************************************
40    MACROS / CONSTANTS
41***************************************************************************/
42
43#define Z80STI DEVICE_GET_INFO_NAME( z80sti )
44
45#define MDRV_Z80STI_ADD(_tag, _clock, _config) \
46   MDRV_DEVICE_ADD((_tag), Z80STI, _clock)   \
47   MDRV_DEVICE_CONFIG(_config)
48
49#define Z80STI_INTERFACE(name) const z80sti_interface (name) =
50
51/***************************************************************************
52    TYPE DEFINITIONS
53***************************************************************************/
54
55typedef struct _z80sti_interface z80sti_interface;
56struct _z80sti_interface
57{
58   int   rx_clock;         /* serial receive clock */
59   int   tx_clock;         /* serial transmit clock */
60
61   /* this gets called on each change of the _INT pin (pin 17) */
62   devcb_write_line      out_int_func;
63
64   /* this is called on each read of the GPIO pins */
65   devcb_read8            in_gpio_func;
66
67   /* this is called on each write of the GPIO pins */
68   devcb_write8         out_gpio_func;
69
70   /* this gets called for each read of the SI pin (pin 38) */
71   devcb_read_line         in_si_func;
72
73   /* this gets called for each change of the SO pin (pin 37) */
74   devcb_write_line      out_so_func;
75
76   /* this gets called for each change of the TAO pin (pin 1) */
77   devcb_write_line      out_tao_func;
78
79   /* this gets called for each change of the TBO pin (pin 2) */
80   devcb_write_line      out_tbo_func;
81
82   /* this gets called for each change of the TCO pin (pin 3) */
83   devcb_write_line      out_tco_func;
84
85   /* this gets called for each change of the TDO pin (pin 4) */
86   devcb_write_line      out_tdo_func;
87};
88
89/***************************************************************************
90    PROTOTYPES
91***************************************************************************/
92
93/* device interface */
94DEVICE_GET_INFO( z80sti );
95
96/* register access */
97READ8_DEVICE_HANDLER( z80sti_r );
98WRITE8_DEVICE_HANDLER( z80sti_w );
99
100/* receive clock */
101WRITE_LINE_DEVICE_HANDLER( z80sti_rc_w );
102
103/* transmit clock */
104WRITE_LINE_DEVICE_HANDLER( z80sti_tc_w );
105
106/* GPIP input lines */
107WRITE_LINE_DEVICE_HANDLER( z80sti_i0_w );
108WRITE_LINE_DEVICE_HANDLER( z80sti_i1_w );
109WRITE_LINE_DEVICE_HANDLER( z80sti_i2_w );
110WRITE_LINE_DEVICE_HANDLER( z80sti_i3_w );
111WRITE_LINE_DEVICE_HANDLER( z80sti_i4_w );
112WRITE_LINE_DEVICE_HANDLER( z80sti_i5_w );
113WRITE_LINE_DEVICE_HANDLER( z80sti_i6_w );
114WRITE_LINE_DEVICE_HANDLER( z80sti_i7_w );
115
116#endif
src/emu/machine/z80dart.c
r0r7537
1/***************************************************************************
2
3    Z80 DART Dual Asynchronous Receiver/Transmitter emulation
4
5    Copyright (c) 2008, The MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10/*
11
12    TODO:
13
14    - break detection
15    - wr0 reset tx interrupt pending
16    - wait/ready
17    - 1.5 stop bits
18
19*/
20
21#include "emu.h"
22#include "z80dart.h"
23#include "cpu/z80/z80.h"
24#include "cpu/z80/z80daisy.h"
25
26/***************************************************************************
27    PARAMETERS
28***************************************************************************/
29
30#define VERBOSE 0
31
32#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
33
34enum
35{
36   STATE_START = 0,
37   STATE_DATA,
38   STATE_PARITY,
39   STATE_STOP,
40   STATE_STOP2
41};
42
43enum
44{
45   INT_TRANSMIT = 0,
46   INT_EXTERNAL,
47   INT_RECEIVE,
48   INT_SPECIAL
49};
50
51#define Z80DART_RR0_RX_CHAR_AVAILABLE   0x01
52#define Z80DART_RR0_INTERRUPT_PENDING   0x02
53#define Z80DART_RR0_TX_BUFFER_EMPTY      0x04
54#define Z80DART_RR0_DCD               0x08
55#define Z80DART_RR0_RI               0x10
56#define Z80DART_RR0_CTS               0x20
57#define Z80DART_RR0_BREAK            0x80 /* not supported */
58
59#define Z80DART_RR1_ALL_SENT         0x01
60#define Z80DART_RR1_PARITY_ERROR      0x10
61#define Z80DART_RR1_RX_OVERRUN_ERROR   0x20
62#define Z80DART_RR1_FRAMING_ERROR      0x40
63
64#define Z80DART_WR0_REGISTER_MASK      0x07
65#define Z80DART_WR0_COMMAND_MASK      0x38
66#define Z80DART_WR0_NULL_CODE         0x00
67#define Z80DART_WR0_RESET_EXT_STATUS   0x10
68#define Z80DART_WR0_CHANNEL_RESET      0x18
69#define Z80DART_WR0_ENABLE_INT_NEXT_RX   0x20
70#define Z80DART_WR0_RESET_TX_INT      0x28 /* not supported */
71#define Z80DART_WR0_ERROR_RESET         0x30
72#define Z80DART_WR0_RETURN_FROM_INT      0x38 /* not supported */
73
74#define Z80DART_WR1_EXT_INT_ENABLE      0x01
75#define Z80DART_WR1_TX_INT_ENABLE      0x02
76#define Z80DART_WR1_STATUS_VECTOR      0x04
77#define Z80DART_WR1_RX_INT_ENABLE_MASK   0x18
78#define Z80DART_WR1_RX_INT_DISABLE      0x00
79#define Z80DART_WR1_RX_INT_FIRST      0x08
80#define Z80DART_WR1_RX_INT_ALL_PARITY   0x10 /* not supported */
81#define Z80DART_WR1_RX_INT_ALL         0x18
82#define Z80DART_WR1_WRDY_ON_RX_TX      0x20 /* not supported */
83#define Z80DART_WR1_WRDY_FUNCTION      0x40 /* not supported */
84#define Z80DART_WR1_WRDY_ENABLE         0x80 /* not supported */
85
86#define Z80DART_WR3_RX_ENABLE         0x01
87#define Z80DART_WR3_AUTO_ENABLES      0x20
88#define Z80DART_WR3_RX_WORD_LENGTH_MASK   0xc0
89#define Z80DART_WR3_RX_WORD_LENGTH_5   0x00
90#define Z80DART_WR3_RX_WORD_LENGTH_7   0x40
91#define Z80DART_WR3_RX_WORD_LENGTH_6   0x80
92#define Z80DART_WR3_RX_WORD_LENGTH_8   0xc0
93
94#define Z80DART_WR4_PARITY_ENABLE      0x01 /* not supported */
95#define Z80DART_WR4_PARITY_EVEN         0x02 /* not supported */
96#define Z80DART_WR4_STOP_BITS_MASK      0x0c
97#define Z80DART_WR4_STOP_BITS_1         0x04
98#define Z80DART_WR4_STOP_BITS_1_5      0x08 /* not supported */
99#define Z80DART_WR4_STOP_BITS_2         0x0c
100#define Z80DART_WR4_CLOCK_MODE_MASK      0xc0
101#define Z80DART_WR4_CLOCK_MODE_X1      0x00
102#define Z80DART_WR4_CLOCK_MODE_X16      0x40
103#define Z80DART_WR4_CLOCK_MODE_X32      0x80
104#define Z80DART_WR4_CLOCK_MODE_X64      0xc0
105
106#define Z80DART_WR5_RTS               0x02
107#define Z80DART_WR5_TX_ENABLE         0x08
108#define Z80DART_WR5_SEND_BREAK         0x10
109#define Z80DART_WR5_TX_WORD_LENGTH_MASK   0xc0
110#define Z80DART_WR5_TX_WORD_LENGTH_5   0x00
111#define Z80DART_WR5_TX_WORD_LENGTH_7   0x40
112#define Z80DART_WR5_TX_WORD_LENGTH_6   0x80
113#define Z80DART_WR5_TX_WORD_LENGTH_8   0xc0
114#define Z80DART_WR5_DTR               0x80
115
116/***************************************************************************
117    TYPE DEFINITIONS
118***************************************************************************/
119
120typedef struct _dart_channel dart_channel;
121struct _dart_channel
122{
123   devcb_resolved_read_line   in_rxd_func;
124   devcb_resolved_write_line   out_txd_func;
125   devcb_resolved_write_line   out_dtr_func;
126   devcb_resolved_write_line   out_rts_func;
127   devcb_resolved_write_line   out_wrdy_func;
128
129   /* register state */
130   UINT8 rr[3];         /* read register */
131   UINT8 wr[6];         /* write register */
132
133   /* receiver state */
134   UINT8 rx_data_fifo[3];   /* receive data FIFO */
135   UINT8 rx_error_fifo[3];   /* receive error FIFO */
136   UINT8 rx_shift;         /* 8-bit receive shift register */
137   UINT8 rx_error;         /* current receive error */
138   int rx_fifo;         /* receive FIFO pointer */
139
140   int rx_clock;         /* receive clock pulse count */
141   int rx_state;         /* receive state */
142   int rx_bits;         /* bits received */
143   int rx_first;         /* first character received */
144   int rx_parity;         /* received data parity */
145   int rx_break;         /* receive break condition */
146   UINT8 rx_rr0_latch;      /* read register 0 latched */
147
148   int ri;               /* ring indicator latch */
149   int cts;            /* clear to send latch */
150   int dcd;            /* data carrier detect latch */
151
152   /* transmitter state */
153   UINT8 tx_data;         /* transmit data register */
154   UINT8 tx_shift;         /* transmit shift register */
155
156   int tx_clock;         /* transmit clock pulse count */
157   int tx_state;         /* transmit state */
158   int tx_bits;         /* bits transmitted */
159   int tx_parity;         /* transmitted data parity */
160
161   int dtr;            /* data terminal ready */
162   int rts;            /* request to send */
163};
164
165typedef struct _z80dart_t z80dart_t;
166struct _z80dart_t
167{
168   devcb_resolved_write_line   out_int_func;
169
170   dart_channel channel[2];      /* channels */
171
172   int int_state[8];            /* interrupt state */
173
174   /* timers */
175   emu_timer *rxca_timer;
176   emu_timer *txca_timer;
177   emu_timer *rxtxcb_timer;
178};
179
180/***************************************************************************
181    FUNCTION PROTOTYPES
182***************************************************************************/
183
184static int z80dart_irq_state(running_device *device);
185static void z80dart_irq_reti(running_device *device);
186
187/***************************************************************************
188    INLINE FUNCTIONS
189***************************************************************************/
190
191INLINE z80dart_t *get_safe_token(running_device *device)
192{
193   assert(device != NULL);
194   assert(device->token != NULL);
195   assert(device->type == Z80DART);
196   return (z80dart_t *)device->token;
197}
198
199INLINE const z80dart_interface *get_interface(running_device *device)
200{
201   assert(device != NULL);
202   assert((device->type == Z80DART));
203   return (const z80dart_interface *) device->baseconfig().static_config;
204}
205
206/***************************************************************************
207    IMPLEMENTATION
208***************************************************************************/
209
210#define RXD \
211   devcb_call_read_line(&ch->in_rxd_func)
212
213#define TXD(_state) \
214   devcb_call_write_line(&ch->out_txd_func, _state)
215
216#define RTS(_state) \
217   devcb_call_write_line(&ch->out_rts_func, _state)
218
219#define DTR(_state) \
220   devcb_call_write_line(&ch->out_dtr_func, _state)
221
222/*-------------------------------------------------
223    check_interrupts - control interrupt line
224-------------------------------------------------*/
225
226static void check_interrupts(running_device *device)
227{
228   z80dart_t *z80dart = get_safe_token(device);
229   int state = (z80dart_irq_state(device) & Z80_DAISY_INT) ? ASSERT_LINE : CLEAR_LINE;
230
231   devcb_call_write_line(&z80dart->out_int_func, state);
232}
233
234/*-------------------------------------------------
235    take_interrupt - trigger interrupt
236-------------------------------------------------*/
237
238static void take_interrupt(running_device *device, int channel, int level)
239{
240   z80dart_t *z80dart = get_safe_token(device);
241   dart_channel *ch = &z80dart->channel[channel];
242   UINT8 vector = ch->wr[2];
243   int priority = (channel << 2) | level;
244
245   LOG(("Z80DART \"%s\" Channel %c : Interrupt Request %u\n", device->tag.cstr(), 'A' + channel, level));
246
247   if ((channel == Z80DART_CH_B) && (ch->wr[1] & Z80DART_WR1_STATUS_VECTOR))
248   {
249      /* status affects vector */
250      vector = (ch->wr[2] & 0xf1) | (!channel << 3) | (level << 1);
251   }
252
253   /* update vector register */
254   ch->rr[2] = vector;
255
256   /* trigger interrupt */
257   z80dart->int_state[priority] |= Z80_DAISY_INT;
258   z80dart->channel[Z80DART_CH_A].rr[0] |= Z80DART_RR0_INTERRUPT_PENDING;
259
260   /* check for interrupt */
261   check_interrupts(device);
262}
263
264/*-------------------------------------------------
265    get_clock_mode - get clock divisor
266-------------------------------------------------*/
267
268static int get_clock_mode(dart_channel *ch)
269{
270   int clocks = 1;
271
272   switch (ch->wr[4] & Z80DART_WR4_CLOCK_MODE_MASK)
273   {
274   case Z80DART_WR4_CLOCK_MODE_X1:      clocks = 1;      break;
275   case Z80DART_WR4_CLOCK_MODE_X16:   clocks = 16;   break;
276   case Z80DART_WR4_CLOCK_MODE_X32:   clocks = 32;   break;
277   case Z80DART_WR4_CLOCK_MODE_X64:   clocks = 64;   break;
278   }
279
280   return clocks;
281}
282
283/*-------------------------------------------------
284    get_stop_bits - get number of stop bits
285-------------------------------------------------*/
286
287static float get_stop_bits(dart_channel *ch)
288{
289   float bits = 1;
290
291   switch (ch->wr[4] & Z80DART_WR4_STOP_BITS_MASK)
292   {
293   case Z80DART_WR4_STOP_BITS_1:      bits = 1;      break;
294   case Z80DART_WR4_STOP_BITS_1_5:      bits = 1.5;      break;
295   case Z80DART_WR4_STOP_BITS_2:      bits = 2;      break;
296   }
297
298   return bits;
299}
300
301/*-------------------------------------------------
302    get_rx_word_length - get receive word length
303-------------------------------------------------*/
304
305static int get_rx_word_length(dart_channel *ch)
306{
307   int bits = 5;
308
309   switch (ch->wr[3] & Z80DART_WR3_RX_WORD_LENGTH_MASK)
310   {
311   case Z80DART_WR3_RX_WORD_LENGTH_5:   bits = 5;      break;
312   case Z80DART_WR3_RX_WORD_LENGTH_6:   bits = 6;      break;
313   case Z80DART_WR3_RX_WORD_LENGTH_7:   bits = 7;      break;
314   case Z80DART_WR3_RX_WORD_LENGTH_8:   bits = 8;      break;
315   }
316
317   return bits;
318}
319
320/*-------------------------------------------------
321    get_rx_word_length - get transmit word length
322-------------------------------------------------*/
323
324static int get_tx_word_length(dart_channel *ch)
325{
326   int bits = 5;
327
328   switch (ch->wr[5] & Z80DART_WR5_TX_WORD_LENGTH_MASK)
329   {
330   case Z80DART_WR5_TX_WORD_LENGTH_5:   bits = 5;   break;
331   case Z80DART_WR5_TX_WORD_LENGTH_6:   bits = 6;   break;
332   case Z80DART_WR5_TX_WORD_LENGTH_7:   bits = 7;   break;
333   case Z80DART_WR5_TX_WORD_LENGTH_8:   bits = 8;   break;
334   }
335
336   return bits;
337}
338
339/*-------------------------------------------------
340    reset_channel - reset channel status
341-------------------------------------------------*/
342
343static void reset_channel(running_device *device, int channel)
344{
345   z80dart_t *z80dart = get_safe_token(device);
346   dart_channel *ch = &z80dart->channel[channel];
347
348   /* disable receiver */
349   ch->wr[3] &= ~Z80DART_WR3_RX_ENABLE;
350   ch->rx_state = STATE_START;
351
352   /* disable transmitter */
353   ch->wr[5] &= ~Z80DART_WR5_TX_ENABLE;
354   ch->tx_state = STATE_START;
355
356   /* reset external lines */
357   RTS(1);
358   DTR(1);
359
360   if (channel == Z80DART_CH_A)
361   {
362      /* reset interrupt logic */
363      int i;
364
365      for (i = 0; i < 8; i++)
366      {
367         z80dart->int_state[i] = 0;
368      }
369
370      check_interrupts(device);
371   }
372}
373
374/*-------------------------------------------------
375    detect_start_bit - detect start bit
376-------------------------------------------------*/
377
378static int detect_start_bit(dart_channel *ch)
379{
380   if (!(ch->wr[3] & Z80DART_WR3_RX_ENABLE)) return 0;
381
382   return !RXD;
383}
384
385/*-------------------------------------------------
386    shift_data_in - shift in serial data
387-------------------------------------------------*/
388
389static void shift_data_in(dart_channel *ch)
390{
391   if (ch->rx_bits < 8)
392   {
393      int rxd = RXD;
394
395      ch->rx_shift >>= 1;
396      ch->rx_shift = (rxd << 7) | (ch->rx_shift & 0x7f);
397      ch->rx_parity ^= rxd;
398      ch->rx_bits++;
399   }
400}
401
402/*-------------------------------------------------
403    character_completed - check if complete
404    data word has been transferred
405-------------------------------------------------*/
406
407static int character_completed(dart_channel *ch)
408{
409   return ch->rx_bits == get_rx_word_length(ch);
410}
411
412/*-------------------------------------------------
413    detect_parity_error - detect parity error
414-------------------------------------------------*/
415
416static void detect_parity_error(running_device *device, int channel)
417{
418   z80dart_t *z80dart = get_safe_token(device);
419   dart_channel *ch = &z80dart->channel[channel];
420
421   int parity = (ch->wr[1] & Z80DART_WR4_PARITY_EVEN) ? 1 : 0;
422
423   if (RXD != (ch->rx_parity ^ parity))
424   {
425      /* parity error detected */
426      ch->rx_error |= Z80DART_RR1_PARITY_ERROR;
427
428      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
429      {
430      case Z80DART_WR1_RX_INT_FIRST:
431         if (!ch->rx_first)
432         {
433            take_interrupt(device, channel, INT_SPECIAL);
434         }
435         break;
436
437      case Z80DART_WR1_RX_INT_ALL_PARITY:
438         take_interrupt(device, channel, INT_SPECIAL);
439         break;
440
441      case Z80DART_WR1_RX_INT_ALL:
442         take_interrupt(device, channel, INT_RECEIVE);
443         break;
444      }
445   }
446}
447
448/*-------------------------------------------------
449    detect_framing_error - detect framing error
450-------------------------------------------------*/
451
452static void detect_framing_error(running_device *device, int channel)
453{
454   z80dart_t *z80dart = get_safe_token(device);
455   dart_channel *ch = &z80dart->channel[channel];
456
457   if (!RXD)
458   {
459      /* framing error detected */
460      ch->rx_error |= Z80DART_RR1_FRAMING_ERROR;
461
462      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
463      {
464      case Z80DART_WR1_RX_INT_FIRST:
465         if (!ch->rx_first)
466         {
467            take_interrupt(device, channel, INT_SPECIAL);
468         }
469         break;
470
471      case Z80DART_WR1_RX_INT_ALL_PARITY:
472      case Z80DART_WR1_RX_INT_ALL:
473         take_interrupt(device, channel, INT_SPECIAL);
474         break;
475      }
476   }
477}
478
479/*-------------------------------------------------
480    receive - receive serial data
481-------------------------------------------------*/
482
483static void receive(running_device *device, int channel)
484{
485   z80dart_t *z80dart = get_safe_token(device);
486   dart_channel *ch = &z80dart->channel[channel];
487
488   float stop_bits = get_stop_bits(ch);
489
490   switch (ch->rx_state)
491   {
492   case STATE_START:
493      /* check for start bit */
494      if (detect_start_bit(ch))
495      {
496         /* start bit detected */
497         ch->rx_shift = 0;
498         ch->rx_error = 0;
499         ch->rx_bits = 0;
500         ch->rx_parity = 0;
501
502         /* next bit is a data bit */
503         ch->rx_state = STATE_DATA;
504      }
505      break;
506
507   case STATE_DATA:
508      /* shift bit into shift register */
509      shift_data_in(ch);
510
511      if (character_completed(ch))
512      {
513         /* all data bits received */
514         if (ch->wr[4] & Z80DART_WR4_PARITY_ENABLE)
515         {
516            /* next bit is the parity bit */
517            ch->rx_state = STATE_PARITY;
518         }
519         else
520         {
521            /* next bit is a STOP bit */
522            if (stop_bits == 1)
523               ch->rx_state = STATE_STOP2;
524            else
525               ch->rx_state = STATE_STOP;
526         }
527      }
528      break;
529
530   case STATE_PARITY:
531      /* shift bit into shift register */
532      shift_data_in(ch);
533
534      /* check for parity error */
535      detect_parity_error(device, channel);
536
537      /* next bit is a STOP bit */
538      if (stop_bits == 1)
539         ch->rx_state = STATE_STOP2;
540      else
541         ch->rx_state = STATE_STOP;
542      break;
543
544   case STATE_STOP:
545      /* shift bit into shift register */
546      shift_data_in(ch);
547
548      /* check for framing error */
549      detect_framing_error(device, channel);
550
551      /* next bit is the second STOP bit */
552      ch->rx_state = STATE_STOP2;
553      break;
554
555   case STATE_STOP2:
556      /* shift bit into shift register */
557      shift_data_in(ch);
558
559      /* check for framing error */
560      detect_framing_error(device, channel);
561
562      /* store data into FIFO */
563      z80dart_receive_data(device, channel, ch->rx_shift);
564
565      /* next bit is the START bit */
566      ch->rx_state = STATE_START;
567      break;
568   }
569}
570
571/*-------------------------------------------------
572    transmit - transmit serial data
573-------------------------------------------------*/
574
575static void transmit(running_device *device, int channel)
576{
577   z80dart_t *z80dart = get_safe_token(device);
578   dart_channel *ch = &z80dart->channel[channel];
579
580   int word_length = get_tx_word_length(ch);
581   float stop_bits = get_stop_bits(ch);
582
583   switch (ch->tx_state)
584   {
585   case STATE_START:
586      if ((ch->wr[5] & Z80DART_WR5_TX_ENABLE) && !(ch->rr[0] & Z80DART_RR0_TX_BUFFER_EMPTY))
587      {
588         /* transmit start bit */
589         TXD(0);
590
591         ch->tx_bits = 0;
592         ch->tx_shift = ch->tx_data;
593
594         /* empty transmit buffer */
595         ch->rr[0] |= Z80DART_RR0_TX_BUFFER_EMPTY;
596
597         if (ch->wr[1] & Z80DART_WR1_TX_INT_ENABLE)
598            take_interrupt(device, channel, INT_TRANSMIT);
599
600         ch->tx_state = STATE_DATA;
601      }
602      else if (ch->wr[5] & Z80DART_WR5_SEND_BREAK)
603      {
604         /* transmit break */
605         TXD(0);
606      }
607      else
608      {
609         /* transmit marking line */
610         TXD(1);
611      }
612
613      break;
614
615   case STATE_DATA:
616      /* transmit data bit */
617      TXD(BIT(ch->tx_shift, 0));
618
619      /* shift data */
620      ch->tx_shift >>= 1;
621      ch->tx_bits++;
622
623      if (ch->rx_bits == word_length)
624      {
625         if (ch->wr[4] & Z80DART_WR4_PARITY_ENABLE)
626            ch->tx_state = STATE_PARITY;
627         else
628         {
629            if (stop_bits == 1)
630               ch->tx_state = STATE_STOP2;
631            else
632               ch->tx_state = STATE_STOP;
633         }
634      }
635      break;
636
637   case STATE_PARITY:
638      // TODO: calculate parity
639      if (stop_bits == 1)
640         ch->tx_state = STATE_STOP2;
641      else
642         ch->tx_state = STATE_STOP;
643      break;
644
645   case STATE_STOP:
646      /* transmit stop bit */
647      TXD(1);
648
649      ch->tx_state = STATE_STOP2;
650      break;
651
652   case STATE_STOP2:
653      /* transmit stop bit */
654      TXD(1);
655
656      /* if transmit buffer is empty */
657      if (ch->rr[0] & Z80DART_RR0_TX_BUFFER_EMPTY)
658      {
659         /* then all characters have been sent */
660         ch->rr[1] |= Z80DART_RR1_ALL_SENT;
661
662         /* when the RTS bit is reset, the _RTS output goes high after the transmitter empties */
663         if (!ch->rts)
664            RTS(1);
665      }
666
667      ch->tx_state = STATE_START;
668      break;
669   }
670}
671
672/*-------------------------------------------------
673    z80dart_c_r - read control register
674-------------------------------------------------*/
675
676READ8_DEVICE_HANDLER( z80dart_c_r )
677{
678   z80dart_t *z80dart = get_safe_token(device);
679   int channel = offset & 0x01;
680   dart_channel *ch = &z80dart->channel[channel];
681   UINT8 data = 0;
682
683   int reg = ch->wr[0] & Z80DART_WR0_REGISTER_MASK;
684
685   switch (reg)
686   {
687   case 0:
688   case 1:
689      data = ch->rr[reg];
690      break;
691
692   case 2:
693      /* channel B only */
694      if (channel == Z80DART_CH_B)
695         data = ch->rr[reg];
696      break;
697   }
698
699   LOG(("Z80DART \"%s\" Channel %c : Control Register Read '%02x'\n", device->tag.cstr(), 'A' + channel, data));
700
701   return data;
702}
703
704/*-------------------------------------------------
705    z80dart_c_w - write control register
706-------------------------------------------------*/
707
708WRITE8_DEVICE_HANDLER( z80dart_c_w )
709{
710   z80dart_t *z80dart = get_safe_token(device);
711   int channel = offset & 0x01;
712   dart_channel *ch = &z80dart->channel[channel];
713
714   int reg = ch->wr[0] & Z80DART_WR0_REGISTER_MASK;
715
716   LOG(("Z80DART \"%s\" Channel %c : Control Register Write '%02x'\n", device->tag.cstr(), 'A' + channel, data));
717
718   /* write data to selected register */
719   ch->wr[reg] = data;
720
721   if (reg != 0)
722   {
723      /* mask out register index */
724      ch->wr[0] &= ~Z80DART_WR0_REGISTER_MASK;
725   }
726
727   switch (reg)
728   {
729   case 0:
730      switch (data & Z80DART_WR0_COMMAND_MASK)
731      {
732      case Z80DART_WR0_RESET_EXT_STATUS:
733         /* reset external/status interrupt */
734         ch->rr[0] &= ~(Z80DART_RR0_DCD | Z80DART_RR0_RI | Z80DART_RR0_CTS | Z80DART_RR0_BREAK);
735
736         if (!ch->dcd) ch->rr[0] |= Z80DART_RR0_DCD;
737         if (ch->ri) ch->rr[0] |= Z80DART_RR0_RI;
738         if (ch->cts) ch->rr[0] |= Z80DART_RR0_CTS;
739
740         ch->rx_rr0_latch = 0;
741
742         LOG(("Z80DART \"%s\" Channel %c : Reset External/Status Interrupt\n", device->tag.cstr(), 'A' + channel));
743         break;
744
745      case Z80DART_WR0_CHANNEL_RESET:
746         /* channel reset */
747         LOG(("Z80DART \"%s\" Channel %c : Channel Reset\n", device->tag.cstr(), 'A' + channel));
748         reset_channel(device, channel);
749         break;
750
751      case Z80DART_WR0_ENABLE_INT_NEXT_RX:
752         /* enable interrupt on next receive character */
753         LOG(("Z80DART \"%s\" Channel %c : Enable Interrupt on Next Received Character\n", device->tag.cstr(), 'A' + channel));
754         ch->rx_first = 1;
755         break;
756
757      case Z80DART_WR0_RESET_TX_INT:
758         /* reset transmitter interrupt pending */
759         LOG(("Z80DART \"%s\" Channel %c : Reset Transmitter Interrupt Pending\n", device->tag.cstr(), 'A' + channel));
760         break;
761
762      case Z80DART_WR0_ERROR_RESET:
763         /* error reset */
764         LOG(("Z80DART \"%s\" Channel %c : Error Reset\n", device->tag.cstr(), 'A' + channel));
765         ch->rr[1] &= ~(Z80DART_RR1_FRAMING_ERROR | Z80DART_RR1_RX_OVERRUN_ERROR | Z80DART_RR1_PARITY_ERROR);
766         break;
767
768      case Z80DART_WR0_RETURN_FROM_INT:
769         /* return from interrupt */
770         LOG(("Z80DART \"%s\" Channel %c : Return from Interrupt\n", device->tag.cstr(), 'A' + channel));
771         z80dart_irq_reti(device);
772         break;
773      }
774      break;
775
776   case 1:
777      LOG(("Z80DART \"%s\" Channel %c : External Interrupt Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_EXT_INT_ENABLE) ? 1 : 0));
778      LOG(("Z80DART \"%s\" Channel %c : Transmit Interrupt Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_TX_INT_ENABLE) ? 1 : 0));
779      LOG(("Z80DART \"%s\" Channel %c : Status Affects Vector %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_STATUS_VECTOR) ? 1 : 0));
780      LOG(("Z80DART \"%s\" Channel %c : Wait/Ready Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_WRDY_ENABLE) ? 1 : 0));
781
782      switch (data & Z80DART_WR1_RX_INT_ENABLE_MASK)
783      {
784      case Z80DART_WR1_RX_INT_DISABLE:
785         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt Disabled\n", device->tag.cstr(), 'A' + channel));
786         break;
787
788      case Z80DART_WR1_RX_INT_FIRST:
789         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on First Character\n", device->tag.cstr(), 'A' + channel));
790         break;
791
792      case Z80DART_WR1_RX_INT_ALL_PARITY:
793         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on All Characters, Parity Affects Vector\n", device->tag.cstr(), 'A' + channel));
794         break;
795
796      case Z80DART_WR1_RX_INT_ALL:
797         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on All Characters\n", device->tag.cstr(), 'A' + channel));
798         break;
799      }
800
801      check_interrupts(device);
802      break;
803
804   case 2:
805      /* interrupt vector */
806      check_interrupts(device);
807      LOG(("Z80DART \"%s\" Channel %c : Interrupt Vector %02x\n", device->tag.cstr(), 'A' + channel, data));
808      break;
809
810   case 3:
811      LOG(("Z80DART \"%s\" Channel %c : Receiver Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR3_RX_ENABLE) ? 1 : 0));
812      LOG(("Z80DART \"%s\" Channel %c : Auto Enables %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR3_AUTO_ENABLES) ? 1 : 0));
813      LOG(("Z80DART \"%s\" Channel %c : Receiver Bits/Character %u\n", device->tag.cstr(), 'A' + channel, get_rx_word_length(ch)));
814      break;
815
816   case 4:
817      LOG(("Z80DART \"%s\" Channel %c : Parity Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR4_PARITY_ENABLE) ? 1 : 0));
818      LOG(("Z80DART \"%s\" Channel %c : Parity %s\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR4_PARITY_EVEN) ? "Even" : "Odd"));
819      LOG(("Z80DART \"%s\" Channel %c : Stop Bits %f\n", device->tag.cstr(), 'A' + channel, get_stop_bits(ch)));
820      LOG(("Z80DART \"%s\" Channel %c : Clock Mode %uX\n", device->tag.cstr(), 'A' + channel, get_clock_mode(ch)));
821      break;
822
823   case 5:
824      LOG(("Z80DART \"%s\" Channel %c : Transmitter Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_TX_ENABLE) ? 1 : 0));
825      LOG(("Z80DART \"%s\" Channel %c : Transmitter Bits/Character %u\n", device->tag.cstr(), 'A' + channel, get_tx_word_length(ch)));
826      LOG(("Z80DART \"%s\" Channel %c : Send Break %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_SEND_BREAK) ? 1 : 0));
827      LOG(("Z80DART \"%s\" Channel %c : Request to Send %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_RTS) ? 1 : 0));
828      LOG(("Z80DART \"%s\" Channel %c : Data Terminal Ready %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_DTR) ? 1 : 0));
829
830      if (data & Z80DART_WR5_RTS)
831      {
832         /* when the RTS bit is set, the _RTS output goes low */
833         RTS(0);
834
835         ch->rts = 1;
836      }
837      else
838      {
839         /* when the RTS bit is reset, the _RTS output goes high after the transmitter empties */
840         ch->rts = 0;
841      }
842
843      /* data terminal ready output follows the state programmed into the DTR bit*/
844      ch->dtr = (data & Z80DART_WR5_DTR) ? 0 : 1;
845      DTR(ch->dtr);
846      break;
847   }
848}
849
850/*-------------------------------------------------
851    z80dart_d_r - read data register
852-------------------------------------------------*/
853
854READ8_DEVICE_HANDLER( z80dart_d_r )
855{
856   z80dart_t *z80dart = get_safe_token(device);
857   int channel = offset & 0x01;
858   dart_channel *ch = &z80dart->channel[channel];
859   UINT8 data = 0;
860
861   if (ch->rx_fifo >= 0)
862   {
863      /* load data from the FIFO */
864      data = ch->rx_data_fifo[ch->rx_fifo];
865
866      /* load error status from the FIFO, retain overrun and parity errors */
867      ch->rr[1] = (ch->rr[1] & (Z80DART_RR1_RX_OVERRUN_ERROR | Z80DART_RR1_PARITY_ERROR)) | ch->rx_error_fifo[ch->rx_fifo];
868
869      /* decrease FIFO pointer */
870      ch->rx_fifo--;
871
872      if (ch->rx_fifo < 0)
873      {
874         /* no more characters available in the FIFO */
875         ch->rr[0] &= ~ Z80DART_RR0_RX_CHAR_AVAILABLE;
876      }
877   }
878
879   LOG(("Z80DART \"%s\" Channel %c : Data Register Read '%02x'\n", device->tag.cstr(), 'A' + channel, data));
880
881   return data;
882}
883
884/*-------------------------------------------------
885    z80dart_d_w - write data register
886-------------------------------------------------*/
887
888WRITE8_DEVICE_HANDLER( z80dart_d_w )
889{
890   z80dart_t *z80dart = get_safe_token(device);
891   int channel = offset & 0x01;
892   dart_channel *ch = &z80dart->channel[channel];
893
894   ch->tx_data = data;
895
896   ch->rr[0] &= ~Z80DART_RR0_TX_BUFFER_EMPTY;
897   ch->rr[1] &= ~Z80DART_RR1_ALL_SENT;
898
899   LOG(("Z80DART \"%s\" Channel %c : Data Register Write '%02x'\n", device->tag.cstr(), 'A' + channel, data));
900}
901
902/*-------------------------------------------------
903    z80dart_receive_data - receive data word
904-------------------------------------------------*/
905
906void z80dart_receive_data(running_device *device, int channel, UINT8 data)
907{
908   z80dart_t *z80dart = get_safe_token(device);
909   dart_channel *ch = &z80dart->channel[channel];
910
911   LOG(("Z80DART \"%s\" Channel %c : Receive Data Byte '%02x'\n", device->tag.cstr(), 'A' + channel, data));
912
913   if (ch->rx_fifo == 2)
914   {
915      /* receive overrun error detected */
916      ch->rx_error |= Z80DART_RR1_RX_OVERRUN_ERROR;
917
918      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
919      {
920      case Z80DART_WR1_RX_INT_FIRST:
921         if (!ch->rx_first)
922         {
923            take_interrupt(device, channel, INT_SPECIAL);
924         }
925         break;
926
927      case Z80DART_WR1_RX_INT_ALL_PARITY:
928      case Z80DART_WR1_RX_INT_ALL:
929         take_interrupt(device, channel, INT_SPECIAL);
930         break;
931      }
932   }
933   else
934   {
935      ch->rx_fifo++;
936   }
937
938   /* store received character and error status into FIFO */
939   ch->rx_data_fifo[ch->rx_fifo] = data;
940   ch->rx_error_fifo[ch->rx_fifo] = ch->rx_error;
941
942   ch->rr[0] |= Z80DART_RR0_RX_CHAR_AVAILABLE;
943
944   /* receive interrupt */
945   switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
946   {
947   case Z80DART_WR1_RX_INT_FIRST:
948      if (ch->rx_first)
949      {
950         take_interrupt(device, channel, INT_RECEIVE);
951
952         ch->rx_first = 0;
953      }
954      break;
955
956   case Z80DART_WR1_RX_INT_ALL_PARITY:
957   case Z80DART_WR1_RX_INT_ALL:
958      take_interrupt(device, channel, INT_RECEIVE);
959      break;
960   }
961}
962
963/*-------------------------------------------------
964    cts_w - clear to send handler
965-------------------------------------------------*/
966
967static void cts_w(running_device *device, int channel, int state)
968{
969   z80dart_t *z80dart = get_safe_token(device);
970   dart_channel *ch = &z80dart->channel[channel];
971
972   LOG(("Z80DART \"%s\" Channel %c : CTS %u\n", device->tag.cstr(), 'A' + channel, state));
973
974   if (ch->cts != state)
975   {
976      /* enable transmitter if in auto enables mode */
977      if (!state)
978         if (ch->wr[3] & Z80DART_WR3_AUTO_ENABLES)
979            ch->wr[5] |= Z80DART_WR5_TX_ENABLE;
980
981      /* set clear to send */
982      ch->cts = state;
983
984      if (!ch->rx_rr0_latch)
985      {
986         if (!ch->cts)
987            ch->rr[0] |= Z80DART_RR0_CTS;
988         else
989            ch->rr[0] &= ~Z80DART_RR0_CTS;
990
991         /* trigger interrupt */
992         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
993         {
994            /* trigger interrupt */
995            take_interrupt(device, channel, INT_EXTERNAL);
996
997            /* latch read register 0 */
998            ch->rx_rr0_latch = 1;
999         }
1000      }
1001   }
1002}
1003
1004/*-------------------------------------------------
1005    z80dart_ctsa_w - clear to send (channel A)
1006-------------------------------------------------*/
1007
1008WRITE_LINE_DEVICE_HANDLER( z80dart_ctsa_w )
1009{
1010   cts_w(device, Z80DART_CH_A, state);
1011}
1012
1013/*-------------------------------------------------
1014    z80dart_ctsb_w - clear to send (channel B)
1015-------------------------------------------------*/
1016
1017WRITE_LINE_DEVICE_HANDLER( z80dart_ctsb_w )
1018{
1019   cts_w(device, Z80DART_CH_B, state);
1020}
1021
1022/*-------------------------------------------------
1023    dcd_w - data carrier detected handler
1024-------------------------------------------------*/
1025
1026static void dcd_w(running_device *device, int channel, int state)
1027{
1028   z80dart_t *z80dart = get_safe_token(device);
1029   dart_channel *ch = &z80dart->channel[channel];
1030
1031   LOG(("Z80DART \"%s\" Channel %c : DCD %u\n", device->tag.cstr(), 'A' + channel, state));
1032
1033   if (ch->dcd != state)
1034   {
1035      /* enable receiver if in auto enables mode */
1036      if (!state)
1037         if (ch->wr[3] & Z80DART_WR3_AUTO_ENABLES)
1038            ch->wr[3] |= Z80DART_WR3_RX_ENABLE;
1039
1040      /* set data carrier detect */
1041      ch->dcd = state;
1042
1043      if (!ch->rx_rr0_latch)
1044      {
1045         if (ch->dcd)
1046            ch->rr[0] |= Z80DART_RR0_DCD;
1047         else
1048            ch->rr[0] &= ~Z80DART_RR0_DCD;
1049
1050         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
1051         {
1052            /* trigger interrupt */
1053            take_interrupt(device, channel, INT_EXTERNAL);
1054
1055            /* latch read register 0 */
1056            ch->rx_rr0_latch = 1;
1057         }
1058      }
1059   }
1060}
1061
1062/*-------------------------------------------------
1063    z80dart_dcda_w - data carrier detected
1064    (channel A)
1065-------------------------------------------------*/
1066
1067WRITE_LINE_DEVICE_HANDLER( z80dart_dcda_w )
1068{
1069   dcd_w(device, Z80DART_CH_A, state);
1070}
1071
1072/*-------------------------------------------------
1073    z80dart_dcdb_w - data carrier detected
1074    (channel B)
1075-------------------------------------------------*/
1076
1077WRITE_LINE_DEVICE_HANDLER( z80dart_dcdb_w )
1078{
1079   dcd_w(device, Z80DART_CH_B, state);
1080}
1081
1082/*-------------------------------------------------
1083    ri_w - ring indicator handler
1084-------------------------------------------------*/
1085
1086static void ri_w(running_device *device, int channel, int state)
1087{
1088   z80dart_t *z80dart = get_safe_token(device);
1089   dart_channel *ch = &z80dart->channel[channel];
1090
1091   LOG(("Z80DART \"%s\" Channel %c : RI %u\n", device->tag.cstr(), 'A' + channel, state));
1092
1093   if (ch->ri != state)
1094   {
1095      /* set ring indicator state */
1096      ch->ri = state;
1097
1098      if (!ch->rx_rr0_latch)
1099      {
1100         if (ch->ri)
1101            ch->rr[0] |= Z80DART_RR0_RI;
1102         else
1103            ch->rr[0] &= ~Z80DART_RR0_RI;
1104
1105         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
1106         {
1107            /* trigger interrupt */
1108            take_interrupt(device, channel, INT_EXTERNAL);
1109
1110            /* latch read register 0 */
1111            ch->rx_rr0_latch = 1;
1112         }
1113      }
1114   }
1115}
1116
1117/*-------------------------------------------------
1118    z80dart_ria_w - ring indicator (channel A)
1119-------------------------------------------------*/
1120
1121WRITE_LINE_DEVICE_HANDLER( z80dart_ria_w )
1122{
1123   ri_w(device, Z80DART_CH_A, state);
1124}
1125
1126/*-------------------------------------------------
1127    z80dart_rib_w - ring indicator (channel B)
1128-------------------------------------------------*/
1129
1130WRITE_LINE_DEVICE_HANDLER( z80dart_rib_w )
1131{
1132   ri_w(device, Z80DART_CH_B, state);
1133}
1134
1135/*-------------------------------------------------
1136    z80dart_rxca_w - receive clock (channel A)
1137-------------------------------------------------*/
1138
1139WRITE_LINE_DEVICE_HANDLER( z80dart_rxca_w )
1140{
1141   z80dart_t *z80dart = get_safe_token(device);
1142   dart_channel *ch = &z80dart->channel[Z80DART_CH_A];
1143
1144   int clocks = get_clock_mode(ch);
1145
1146   if (!state) return;
1147
1148   LOG(("Z80DART \"%s\" Channel A : Receiver Clock Pulse\n", device->tag.cstr()));
1149
1150   ch->rx_clock++;
1151
1152   if (ch->rx_clock == clocks)
1153   {
1154      ch->rx_clock = 0;
1155
1156      /* receive data */
1157      receive(device, Z80DART_CH_A);
1158   }
1159}
1160
1161/*-------------------------------------------------
1162    z80dart_txca_w - transmit clock (channel A)
1163-------------------------------------------------*/
1164
1165WRITE_LINE_DEVICE_HANDLER( z80dart_txca_w )
1166{
1167   z80dart_t *z80dart = get_safe_token(device);
1168   dart_channel *ch = &z80dart->channel[Z80DART_CH_A];
1169
1170   int clocks = get_clock_mode(ch);
1171
1172   if (!state) return;
1173
1174   LOG(("Z80DART \"%s\" Channel A : Transmitter Clock Pulse\n", device->tag.cstr()));
1175
1176   ch->tx_clock++;
1177
1178   if (ch->tx_clock == clocks)
1179   {
1180      ch->tx_clock = 0;
1181
1182      /* transmit data */
1183      transmit(device, Z80DART_CH_A);
1184   }
1185}
1186
1187/*-------------------------------------------------
1188    z80dart_rxtxcb_w - receive/transmit clock
1189    (channel B)
1190-------------------------------------------------*/
1191
1192WRITE_LINE_DEVICE_HANDLER( z80dart_rxtxcb_w )
1193{
1194   z80dart_t *z80dart = get_safe_token(device);
1195   dart_channel *ch = &z80dart->channel[Z80DART_CH_B];
1196
1197   int clocks = get_clock_mode(ch);
1198
1199   if (!state) return;
1200
1201   LOG(("Z80DART \"%s\" Channel A : Receiver/Transmitter Clock Pulse\n", device->tag.cstr()));
1202
1203   ch->rx_clock++;
1204   ch->tx_clock++;
1205
1206   if (ch->rx_clock == clocks)
1207   {
1208      ch->rx_clock = 0;
1209      ch->tx_clock = 0;
1210
1211      /* receive and transmit data */
1212      receive(device, Z80DART_CH_B);
1213      transmit(device, Z80DART_CH_B);
1214   }
1215}
1216
1217/*-------------------------------------------------
1218    TIMER_CALLBACK( rxca_tick )
1219-------------------------------------------------*/
1220
1221static TIMER_CALLBACK( rxca_tick )
1222{
1223   z80dart_rxca_w((running_device *)ptr, 1);
1224}
1225
1226/*-------------------------------------------------
1227    TIMER_CALLBACK( txca_tick )
1228-------------------------------------------------*/
1229
1230static TIMER_CALLBACK( txca_tick )
1231{
1232   z80dart_txca_w((running_device *)ptr, 1);
1233}
1234
1235/*-------------------------------------------------
1236    TIMER_CALLBACK( rxtxcb_tick )
1237-------------------------------------------------*/
1238
1239static TIMER_CALLBACK( rxtxcb_tick )
1240{
1241   z80dart_rxtxcb_w((running_device *)ptr, 1);
1242}
1243
1244/*-------------------------------------------------
1245    z80dart_irq_state - get interrupt status
1246-------------------------------------------------*/
1247
1248static int z80dart_irq_state(running_device *device)
1249{
1250   z80dart_t *z80dart = get_safe_token( device );
1251   int state = 0;
1252   int i;
1253
1254   LOG(("Z80DART \"%s\" : Interrupt State B:%d%d%d%d A:%d%d%d%d\n", device->tag.cstr(),
1255            z80dart->int_state[0], z80dart->int_state[1], z80dart->int_state[2], z80dart->int_state[3],
1256            z80dart->int_state[4], z80dart->int_state[5], z80dart->int_state[6], z80dart->int_state[7]));
1257
1258   /* loop over all interrupt sources */
1259   for (i = 0; i < 8; i++)
1260   {
1261      /* if we're servicing a request, don't indicate more interrupts */
1262      if (z80dart->int_state[i] & Z80_DAISY_IEO)
1263      {
1264         state |= Z80_DAISY_IEO;
1265         break;
1266      }
1267      state |= z80dart->int_state[i];
1268   }
1269
1270   LOG(("Z80DART \"%s\" : Interrupt State %u\n", device->tag.cstr(), state));
1271
1272   return state;
1273}
1274
1275/*-------------------------------------------------
1276    z80dart_irq_ack - interrupt acknowledge
1277-------------------------------------------------*/
1278
1279static int z80dart_irq_ack(running_device *device)
1280{
1281   z80dart_t *z80dart = get_safe_token( device );
1282   int i;
1283
1284   LOG(("Z80DART \"%s\" Interrupt Acknowledge\n", device->tag.cstr()));
1285
1286   /* loop over all interrupt sources */
1287   for (i = 0; i < 8; i++)
1288   {
1289      /* find the first channel with an interrupt requested */
1290      if (z80dart->int_state[i] & Z80_DAISY_INT)
1291      {
1292         /* clear interrupt, switch to the IEO state, and update the IRQs */
1293         z80dart->int_state[i] = Z80_DAISY_IEO;
1294         z80dart->channel[Z80DART_CH_A].rr[0] &= ~Z80DART_RR0_INTERRUPT_PENDING;
1295         check_interrupts(device);
1296
1297         LOG(("Z80DART \"%s\" : Interrupt Acknowledge Vector %02x\n", device->tag.cstr(), z80dart->channel[Z80DART_CH_B].rr[2]));
1298
1299         return z80dart->channel[Z80DART_CH_B].rr[2];
1300      }
1301   }
1302
1303   logerror("z80dart_irq_ack: failed to find an interrupt to ack!\n");
1304
1305   return z80dart->channel[Z80DART_CH_B].rr[2];
1306}
1307
1308/*-------------------------------------------------
1309    z80dart_irq_reti - return from interrupt
1310-------------------------------------------------*/
1311
1312static void z80dart_irq_reti(running_device *device)
1313{
1314   z80dart_t *z80dart = get_safe_token( device );
1315   int i;
1316
1317   LOG(("Z80DART \"%s\" Return from Interrupt\n", device->tag.cstr()));
1318
1319   /* loop over all interrupt sources */
1320   for (i = 0; i < 8; i++)
1321   {
1322      /* find the first channel with an IEO pending */
1323      if (z80dart->int_state[i] & Z80_DAISY_IEO)
1324      {
1325         /* clear the IEO state and update the IRQs */
1326         z80dart->int_state[i] &= ~Z80_DAISY_IEO;
1327         check_interrupts(device);
1328         return;
1329      }
1330   }
1331
1332   logerror("z80dart_irq_reti: failed to find an interrupt to clear IEO on!\n");
1333}
1334
1335/*-------------------------------------------------
1336    z80dart_cd_ba_r - register read
1337-------------------------------------------------*/
1338
1339READ8_DEVICE_HANDLER( z80dart_cd_ba_r )
1340{
1341   return (offset & 2) ? z80dart_c_r(device, offset & 1) : z80dart_d_r(device, offset & 1);
1342}
1343
1344/*-------------------------------------------------
1345    z80dart_cd_ba_w - register write
1346-------------------------------------------------*/
1347
1348WRITE8_DEVICE_HANDLER( z80dart_cd_ba_w )
1349{
1350   if (offset & 2)
1351      z80dart_c_w(device, offset & 1, data);
1352   else
1353      z80dart_d_w(device, offset & 1, data);
1354}
1355
1356/*-------------------------------------------------
1357    z80dart_ba_cd_r - register read
1358-------------------------------------------------*/
1359
1360READ8_DEVICE_HANDLER( z80dart_ba_cd_r )
1361{
1362   int channel = BIT(offset, 1);
1363
1364   return (offset & 1) ? z80dart_c_r(device, channel) : z80dart_d_r(device, channel);
1365}
1366
1367/*-------------------------------------------------
1368    z80dart_ba_cd_w - register write
1369-------------------------------------------------*/
1370
1371WRITE8_DEVICE_HANDLER( z80dart_ba_cd_w )
1372{
1373   int channel = BIT(offset, 1);
1374
1375   if (offset & 1)
1376      z80dart_c_w(device, channel, data);
1377   else
1378      z80dart_d_w(device, channel, data);
1379}
1380
1381/*-------------------------------------------------
1382    DEVICE_START( z80dart )
1383-------------------------------------------------*/
1384
1385static DEVICE_START( z80dart )
1386{
1387   z80dart_t *z80dart = get_safe_token(device);
1388   const z80dart_interface *intf = (const z80dart_interface *)device->baseconfig().static_config;
1389   int channel;
1390
1391   /* resolve callbacks */
1392   devcb_resolve_read_line(&z80dart->channel[Z80DART_CH_A].in_rxd_func, &intf->in_rxda_func, device);
1393   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_txd_func, &intf->out_txda_func, device);
1394   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_dtr_func, &intf->out_dtra_func, device);
1395   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_rts_func, &intf->out_rtsa_func, device);
1396   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_wrdy_func, &intf->out_wrdya_func, device);
1397   devcb_resolve_read_line(&z80dart->channel[Z80DART_CH_B].in_rxd_func, &intf->in_rxdb_func, device);
1398   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_txd_func, &intf->out_txdb_func, device);
1399   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_dtr_func, &intf->out_dtrb_func, device);
1400   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_rts_func, &intf->out_rtsb_func, device);
1401   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_wrdy_func, &intf->out_wrdyb_func, device);
1402   devcb_resolve_write_line(&z80dart->out_int_func, &intf->out_int_func, device);
1403
1404   if (intf->rx_clock_a)
1405   {
1406      /* allocate channel A receive timer */
1407      z80dart->rxca_timer = timer_alloc(device->machine, rxca_tick, (void *)device);
1408      timer_adjust_periodic(z80dart->rxca_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_clock_a));
1409   }
1410
1411   if (intf->tx_clock_a)
1412   {
1413      /* allocate channel A transmit timer */
1414      z80dart->txca_timer = timer_alloc(device->machine, txca_tick, (void *)device);
1415      timer_adjust_periodic(z80dart->txca_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->tx_clock_a));
1416   }
1417
1418   if (intf->rx_tx_clock_b)
1419   {
1420      /* allocate channel B receive/transmit timer */
1421      z80dart->rxtxcb_timer = timer_alloc(device->machine, rxtxcb_tick, (void *)device);
1422      timer_adjust_periodic(z80dart->rxtxcb_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_tx_clock_b));
1423   }
1424
1425   /* register for state saving */
1426   for (channel = Z80DART_CH_A; channel <= Z80DART_CH_B; channel++)
1427   {
1428      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rr);
1429      state_save_register_device_item_array(device, channel, z80dart->channel[channel].wr);
1430      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rx_data_fifo);
1431      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rx_error_fifo);
1432      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_shift);
1433      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_error);
1434      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_fifo);
1435      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_clock);
1436      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_state);
1437      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_bits);
1438      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_first);
1439      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_parity);
1440      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_break);
1441      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_rr0_latch);
1442      state_save_register_device_item(device, channel, z80dart->channel[channel].ri);
1443      state_save_register_device_item(device, channel, z80dart->channel[channel].cts);
1444      state_save_register_device_item(device, channel, z80dart->channel[channel].dcd);
1445      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_data);
1446      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_shift);
1447      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_clock);
1448      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_state);
1449      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_bits);
1450      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_parity);
1451      state_save_register_device_item(device, channel, z80dart->channel[channel].dtr);
1452      state_save_register_device_item(device, channel, z80dart->channel[channel].rts);
1453   }
1454
1455   state_save_register_device_item_array(device, 0, z80dart->int_state);
1456}
1457
1458/*-------------------------------------------------
1459    DEVICE_RESET( z80dart )
1460-------------------------------------------------*/
1461
1462static DEVICE_RESET( z80dart )
1463{
1464   int channel;
1465
1466   LOG(("Z80DART \"%s\" Reset\n", device->tag.cstr()));
1467
1468   for (channel = Z80DART_CH_A; channel <= Z80DART_CH_B; channel++)
1469   {
1470      reset_channel(device, channel);
1471   }
1472
1473   check_interrupts(device);
1474}
1475
1476/*-------------------------------------------------
1477    DEVICE_GET_INFO( z80dart )
1478-------------------------------------------------*/
1479
1480DEVICE_GET_INFO( z80dart )
1481{
1482   switch (state)
1483   {
1484      /* --- the following bits of info are returned as 64-bit signed integers --- */
1485      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(z80dart_t);               break;
1486      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                           break;
1487      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;            break;
1488
1489      /* --- the following bits of info are returned as pointers to data or functions --- */
1490      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(z80dart);      break;
1491      case DEVINFO_FCT_STOP:                     /* Nothing */                           break;
1492      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(z80dart);      break;
1493      case DEVINFO_FCT_IRQ_STATE:                  info->f = (genf *)z80dart_irq_state;         break;
1494      case DEVINFO_FCT_IRQ_ACK:                  info->f = (genf *)z80dart_irq_ack;            break;
1495      case DEVINFO_FCT_IRQ_RETI:                  info->f = (genf *)z80dart_irq_reti;            break;
1496
1497      /* --- the following bits of info are returned as NULL-terminated strings --- */
1498      case DEVINFO_STR_NAME:                     strcpy(info->s, "Zilog Z80 DART");            break;
1499      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Z80");                     break;
1500      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
1501      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                  break;
1502      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");      break;
1503   }
1504}
src/emu/machine/z80dart.h
r0r7537
1/***************************************************************************
2
3    Z80 DART Dual Asynchronous Receiver/Transmitter implementation
4
5    Copyright (c) 2008, The MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8****************************************************************************
9                            _____   _____
10                    D1   1 |*    \_/     | 40  D0
11                    D3   2 |             | 39  D2
12                    D5   3 |             | 38  D4
13                    D7   4 |             | 37  D6
14                  _INT   5 |             | 36  _IORQ
15                   IEI   6 |             | 35  _CE
16                   IEO   7 |             | 34  B/_A
17                   _M1   8 |             | 33  C/_D
18                   Vdd   9 |             | 32  _RD
19               _W/RDYA  10 |  Z80-DART   | 31  GND
20                  _RIA  11 |             | 30  _W/RDYB
21                  RxDA  12 |             | 29  _RIB
22                 _RxCA  13 |             | 28  RxDB
23                 _TxCA  14 |             | 27  _RxTxCB
24                  TxDA  15 |             | 26  TxDB
25                 _DTRA  16 |             | 25  _DTRB
26                 _RTSA  17 |             | 24  _RTSB
27                 _CTSA  18 |             | 23  _CTSB
28                 _DCDA  19 |             | 22  _DCDB
29                   CLK  20 |_____________| 21  _RESET
30
31***************************************************************************/
32
33#ifndef __Z80DART_H_
34#define __Z80DART_H_
35
36#include "devcb.h"
37
38/***************************************************************************
39    MACROS / CONSTANTS
40***************************************************************************/
41
42#define Z80DART   DEVICE_GET_INFO_NAME(z80dart)
43/*
44#define Z8470   DEVICE_GET_INFO_NAME(z8470)
45#define LH0081  DEVICE_GET_INFO_NAME(lh0088)
46#define MK3881  DEVICE_GET_INFO_NAME(mk3888)
47*/
48
49#define MDRV_Z80DART_ADD(_tag, _clock, _config) \
50   MDRV_DEVICE_ADD(_tag, Z80DART, _clock) \
51   MDRV_DEVICE_CONFIG(_config)
52
53#define MDRV_Z80DART_REMOVE(_tag) \
54   MDRV_DEVICE_REMOVE(_tag)
55
56#define Z80DART_INTERFACE(_name) \
57   const z80dart_interface (_name) =
58
59/***************************************************************************
60    TYPE DEFINITIONS
61***************************************************************************/
62
63enum
64{
65   Z80DART_CH_A = 0,
66   Z80DART_CH_B
67};
68
69typedef struct _z80dart_interface z80dart_interface;
70struct _z80dart_interface
71{
72   int rx_clock_a;         /* channel A receive clock */
73   int tx_clock_a;         /* channel A transmit clock */
74   int rx_tx_clock_b;      /* channel B receive/transmit clock */
75
76   devcb_read_line      in_rxda_func;
77   devcb_write_line   out_txda_func;
78   devcb_write_line   out_dtra_func;
79   devcb_write_line   out_rtsa_func;
80   devcb_write_line   out_wrdya_func;
81
82   devcb_read_line      in_rxdb_func;
83   devcb_write_line   out_txdb_func;
84   devcb_write_line   out_dtrb_func;
85   devcb_write_line   out_rtsb_func;
86   devcb_write_line   out_wrdyb_func;
87
88   devcb_write_line   out_int_func;
89};
90
91/***************************************************************************
92    PROTOTYPES
93***************************************************************************/
94
95/* register access */
96READ8_DEVICE_HANDLER( z80dart_cd_ba_r );
97WRITE8_DEVICE_HANDLER( z80dart_cd_ba_w );
98
99READ8_DEVICE_HANDLER( z80dart_ba_cd_r );
100WRITE8_DEVICE_HANDLER( z80dart_ba_cd_w );
101
102/* control register access */
103WRITE8_DEVICE_HANDLER( z80dart_c_w );
104READ8_DEVICE_HANDLER( z80dart_c_r );
105
106/* data register access */
107WRITE8_DEVICE_HANDLER( z80dart_d_w );
108READ8_DEVICE_HANDLER( z80dart_d_r );
109
110/* serial clocks */
111WRITE_LINE_DEVICE_HANDLER( z80dart_rxca_w );
112WRITE_LINE_DEVICE_HANDLER( z80dart_txca_w );
113WRITE_LINE_DEVICE_HANDLER( z80dart_rxtxcb_w );
114
115/* ring indicator */
116WRITE_LINE_DEVICE_HANDLER( z80dart_ria_w );
117WRITE_LINE_DEVICE_HANDLER( z80dart_rib_w );
118
119/* data carrier detected */
120WRITE_LINE_DEVICE_HANDLER( z80dart_dcda_w );
121WRITE_LINE_DEVICE_HANDLER( z80dart_dcdb_w );
122
123/* clear to send */
124WRITE_LINE_DEVICE_HANDLER( z80dart_ctsa_w );
125WRITE_LINE_DEVICE_HANDLER( z80dart_ctsb_w );
126
127/* receive data byte HACK */
128void z80dart_receive_data(running_device *device, int channel, UINT8 data);
129
130DEVICE_GET_INFO( z80dart );
131
132#endif
src/mess/machine/z80sti.c
r7536r7537
1/***************************************************************************
2
3    Mostek MK3801 Serial Timer Interrupt Controller (Z80-STI) emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10/*
11
12    TODO:
13
14    - timers (other than delay mode)
15    - serial I/O
16    - reset behavior
17
18*/
19
20#include "emu.h"
21#include "z80sti.h"
22#include "cpu/z80/z80.h"
23#include "cpu/z80/z80daisy.h"
24
25/***************************************************************************
26    PARAMETERS
27***************************************************************************/
28
29#define VERBOSE 0
30
31#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
32
33/* registers */
34enum
35{
36   Z80STI_REGISTER_IR = 0,
37   Z80STI_REGISTER_GPIP,
38   Z80STI_REGISTER_IPRB,
39   Z80STI_REGISTER_IPRA,
40   Z80STI_REGISTER_ISRB,
41   Z80STI_REGISTER_ISRA,
42   Z80STI_REGISTER_IMRB,
43   Z80STI_REGISTER_IMRA,
44   Z80STI_REGISTER_PVR,
45   Z80STI_REGISTER_TABC,
46   Z80STI_REGISTER_TBDR,
47   Z80STI_REGISTER_TADR,
48   Z80STI_REGISTER_UCR,
49   Z80STI_REGISTER_RSR,
50   Z80STI_REGISTER_TSR,
51   Z80STI_REGISTER_UDR
52};
53
54/* variable registers */
55enum
56{
57   Z80STI_REGISTER_IR_SCR = 0,
58   Z80STI_REGISTER_IR_TDDR,
59   Z80STI_REGISTER_IR_TCDR,
60   Z80STI_REGISTER_IR_AER,
61   Z80STI_REGISTER_IR_IERB,
62   Z80STI_REGISTER_IR_IERA,
63   Z80STI_REGISTER_IR_DDR,
64   Z80STI_REGISTER_IR_TCDC
65};
66
67/* timers */
68enum
69{
70   TIMER_A = 0,
71   TIMER_B,
72   TIMER_C,
73   TIMER_D,
74   TIMER_COUNT
75};
76
77/* interrupt levels */
78enum
79{
80   Z80STI_IR_P0 = 0,
81   Z80STI_IR_P1,
82   Z80STI_IR_P2,
83   Z80STI_IR_P3,
84   Z80STI_IR_TD,
85   Z80STI_IR_TC,
86   Z80STI_IR_P4,
87   Z80STI_IR_P5,
88   Z80STI_IR_TB,
89   Z80STI_IR_XE,
90   Z80STI_IR_XB,
91   Z80STI_IR_RE,
92   Z80STI_IR_RB,
93   Z80STI_IR_TA,
94   Z80STI_IR_P6,
95   Z80STI_IR_P7
96};
97
98/* timer C/D control register */
99#define Z80STI_TCDC_TARS   0x80
100#define Z80STI_TCDC_TBRS   0x08
101
102/* interrupt vector register */
103#define Z80STI_PVR_ISE      0x08
104#define Z80STI_PVR_VR4      0x10
105
106/* general purpose I/O interrupt levels */
107static const int INT_LEVEL_GPIP[] =
108{
109   Z80STI_IR_P0, Z80STI_IR_P1, Z80STI_IR_P2, Z80STI_IR_P3,   Z80STI_IR_P4, Z80STI_IR_P5, Z80STI_IR_P6, Z80STI_IR_P7
110};
111
112/* timer interrupt levels */
113static const int INT_LEVEL_TIMER[] =
114{
115   Z80STI_IR_TA, Z80STI_IR_TB, Z80STI_IR_TC, Z80STI_IR_TD
116};
117
118/* interrupt vectors */
119static const UINT8 INT_VECTOR[] =
120{
121   0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c, 0x0e,
122   0x10, 0x12, 0x14, 0x16, 0x18, 0x1a, 0x1c, 0x1e
123};
124
125/* timer prescaler divisors */
126static const int PRESCALER[] = { 0, 4, 10, 16, 50, 64, 100, 200 };
127
128/***************************************************************************
129    TYPE DEFINITIONS
130***************************************************************************/
131
132typedef struct _z80sti_t z80sti_t;
133struct _z80sti_t
134{
135   /* device callbacks */
136   devcb_resolved_read8            in_gpio_func;
137   devcb_resolved_write8            out_gpio_func;
138   devcb_resolved_read_line         in_si_func;
139   devcb_resolved_write_line         out_so_func;
140   devcb_resolved_write_line         out_tao_func;
141   devcb_resolved_write_line         out_tbo_func;
142   devcb_resolved_write_line         out_tco_func;
143   devcb_resolved_write_line         out_tdo_func;
144   devcb_resolved_write_line         out_int_func;
145
146   /* I/O state */
147   UINT8 gpip;                     /* general purpose I/O register */
148   UINT8 aer;                     /* active edge register */
149   UINT8 ddr;                     /* data direction register */
150
151   /* interrupt state */
152   UINT16 ier;                     /* interrupt enable register */
153   UINT16 ipr;                     /* interrupt pending register */
154   UINT16 isr;                     /* interrupt in-service register */
155   UINT16 imr;                     /* interrupt mask register */
156   UINT8 pvr;                     /* interrupt vector register */
157   int int_state[16];               /* interrupt state */
158
159   /* timer state */
160   UINT8 tabc;                     /* timer A/B control register */
161   UINT8 tcdc;                     /* timer C/D control register */
162   UINT8 tdr[TIMER_COUNT];            /* timer data registers */
163   UINT8 tmc[TIMER_COUNT];            /* timer main counters */
164   int to[TIMER_COUNT];            /* timer out latch */
165
166   /* serial state */
167   UINT8 scr;                     /* synchronous character register */
168   UINT8 ucr;                     /* USART control register */
169   UINT8 tsr;                     /* transmitter status register */
170   UINT8 rsr;                     /* receiver status register */
171   UINT8 udr;                     /* USART data register */
172
173   /* timers */
174   emu_timer *timer[TIMER_COUNT];      /* counter timers */
175   emu_timer *rx_timer;            /* serial receive timer */
176   emu_timer *tx_timer;            /* serial transmit timer */
177};
178
179/***************************************************************************
180    FUNCTION PROTOTYPES
181***************************************************************************/
182
183static int z80sti_irq_state(running_device *device);
184static void z80sti_irq_reti(running_device *device);
185
186/***************************************************************************
187    INLINE FUNCTIONS
188***************************************************************************/
189
190INLINE z80sti_t *get_safe_token(running_device *device)
191{
192   assert(device != NULL);
193   assert(device->token != NULL);
194   assert(device->type == Z80STI);
195   return (z80sti_t *)device->token;
196}
197
198INLINE const z80sti_interface *get_interface(running_device *device)
199{
200   assert(device != NULL);
201   assert((device->type == Z80STI));
202   return (const z80sti_interface *) device->baseconfig().static_config;
203}
204
205/***************************************************************************
206    IMPLEMENTATION
207***************************************************************************/
208
209/*-------------------------------------------------
210    check_interrupts - set the interrupt request
211    line state
212-------------------------------------------------*/
213
214static void check_interrupts(z80sti_t *z80sti)
215{
216   if (z80sti->ipr & z80sti->imr)
217   {
218      devcb_call_write_line(&z80sti->out_int_func, ASSERT_LINE);
219   }
220   else
221   {
222      devcb_call_write_line(&z80sti->out_int_func, CLEAR_LINE);
223   }
224}
225
226/*-------------------------------------------------
227    take_interrupt - mark an interrupt pending
228-------------------------------------------------*/
229
230static void take_interrupt(z80sti_t *z80sti, int level)
231{
232   /* set interrupt pending register bit */
233   z80sti->ipr |= 1 << level;
234
235   /* trigger interrupt */
236   z80sti->int_state[level] |= Z80_DAISY_INT;
237
238   check_interrupts(z80sti);
239}
240
241/*-------------------------------------------------
242    serial_receive - receive serial bit
243-------------------------------------------------*/
244
245static void serial_receive(z80sti_t *z80sti)
246{
247}
248
249/*-------------------------------------------------
250    TIMER_CALLBACK( rx_tick )
251-------------------------------------------------*/
252
253static TIMER_CALLBACK( rx_tick )
254{
255   running_device *device = (running_device *)ptr;
256   z80sti_t *z80sti = get_safe_token(device);
257
258   serial_receive(z80sti);
259}
260
261/*-------------------------------------------------
262    serial_transmit - transmit serial bit
263-------------------------------------------------*/
264
265static void serial_transmit(z80sti_t *z80sti)
266{
267}
268
269/*-------------------------------------------------
270    TIMER_CALLBACK( tx_tick )
271-------------------------------------------------*/
272
273static TIMER_CALLBACK( tx_tick )
274{
275   running_device *device = (running_device *)ptr;
276   z80sti_t *z80sti = get_safe_token(device);
277
278   serial_transmit(z80sti);
279}
280
281/*-------------------------------------------------
282    z80sti_r - register read
283-------------------------------------------------*/
284
285READ8_DEVICE_HANDLER( z80sti_r )
286{
287   z80sti_t *z80sti = get_safe_token(device);
288
289   switch (offset & 0x0f)
290   {
291   case Z80STI_REGISTER_IR:
292      switch (z80sti->pvr & 0x07)
293      {
294      case Z80STI_REGISTER_IR_SCR:   return z80sti->scr;
295      case Z80STI_REGISTER_IR_TDDR:   return z80sti->tmc[TIMER_D];
296      case Z80STI_REGISTER_IR_TCDR:   return z80sti->tmc[TIMER_C];
297      case Z80STI_REGISTER_IR_AER:   return z80sti->aer;
298      case Z80STI_REGISTER_IR_IERB:   return z80sti->ier & 0xff;
299      case Z80STI_REGISTER_IR_IERA:   return z80sti->ier >> 8;
300      case Z80STI_REGISTER_IR_DDR:   return z80sti->ddr;
301      case Z80STI_REGISTER_IR_TCDC:   return z80sti->tcdc;
302      }
303      break;
304
305   case Z80STI_REGISTER_GPIP:   z80sti->gpip = (devcb_call_read8(&z80sti->in_gpio_func, 0) & ~z80sti->ddr) | (z80sti->gpip & z80sti->ddr); return z80sti->gpip;
306   case Z80STI_REGISTER_IPRB:   return z80sti->ipr & 0xff;
307   case Z80STI_REGISTER_IPRA:   return z80sti->ipr >> 8;
308   case Z80STI_REGISTER_ISRB:   return z80sti->isr & 0xff;
309   case Z80STI_REGISTER_ISRA:   return z80sti->isr >> 8;
310   case Z80STI_REGISTER_IMRB:   return z80sti->imr & 0xff;
311   case Z80STI_REGISTER_IMRA:   return z80sti->imr >> 8;
312   case Z80STI_REGISTER_PVR:   return z80sti->pvr;
313   case Z80STI_REGISTER_TABC:   return z80sti->tabc;
314   case Z80STI_REGISTER_TBDR:   return z80sti->tmc[TIMER_B];
315   case Z80STI_REGISTER_TADR:   return z80sti->tmc[TIMER_A];
316   case Z80STI_REGISTER_UCR:   return z80sti->ucr;
317   case Z80STI_REGISTER_RSR:   return z80sti->rsr;
318   case Z80STI_REGISTER_TSR:   return z80sti->tsr;
319   case Z80STI_REGISTER_UDR:   return z80sti->udr;
320   }
321
322   return 0;
323}
324
325/*-------------------------------------------------
326    z80sti_w - register write
327-------------------------------------------------*/
328
329WRITE8_DEVICE_HANDLER( z80sti_w )
330{
331   z80sti_t *z80sti = get_safe_token(device);
332
333   switch (offset & 0x0f)
334   {
335   case Z80STI_REGISTER_IR:
336      switch (z80sti->pvr & 0x07)
337      {
338      case Z80STI_REGISTER_IR_SCR:
339         LOG(("Z80STI '%s' Sync Character Register: %x\n", device->tag.cstr(), data));
340         z80sti->scr = data;
341         break;
342
343      case Z80STI_REGISTER_IR_TDDR:
344         LOG(("Z80STI '%s' Timer D Data Register: %x\n", device->tag.cstr(), data));
345         z80sti->tdr[TIMER_D] = data;
346         break;
347
348      case Z80STI_REGISTER_IR_TCDR:
349         LOG(("Z80STI '%s' Timer C Data Register: %x\n", device->tag.cstr(), data));
350         z80sti->tdr[TIMER_C] = data;
351         break;
352
353      case Z80STI_REGISTER_IR_AER:
354         LOG(("Z80STI '%s' Active Edge Register: %x\n", device->tag.cstr(), data));
355         z80sti->aer = data;
356         break;
357
358      case Z80STI_REGISTER_IR_IERB:
359         LOG(("Z80STI '%s' Interrupt Enable Register B: %x\n", device->tag.cstr(), data));
360         z80sti->ier = (z80sti->ier & 0xff00) | data;
361         check_interrupts(z80sti);
362         break;
363
364      case Z80STI_REGISTER_IR_IERA:
365         LOG(("Z80STI '%s' Interrupt Enable Register A: %x\n", device->tag.cstr(), data));
366         z80sti->ier = (data << 8) | (z80sti->ier & 0xff);
367         check_interrupts(z80sti);
368         break;
369
370      case Z80STI_REGISTER_IR_DDR:
371         LOG(("Z80STI '%s' Data Direction Register: %x\n", device->tag.cstr(), data));
372         z80sti->ddr = data;
373         break;
374
375      case Z80STI_REGISTER_IR_TCDC:
376         {
377         int tcc = PRESCALER[(data >> 4) & 0x07];
378         int tdc = PRESCALER[data & 0x07];
379
380         z80sti->tcdc = data;
381
382         LOG(("Z80STI '%s' Timer C Prescaler: %u\n", device->tag.cstr(), tcc));
383         LOG(("Z80STI '%s' Timer D Prescaler: %u\n", device->tag.cstr(), tdc));
384
385         if (tcc)
386            timer_adjust_periodic(z80sti->timer[TIMER_C], ATTOTIME_IN_HZ(device->clock / tcc), 0, ATTOTIME_IN_HZ(device->clock / tcc));
387         else
388            timer_enable(z80sti->timer[TIMER_C], 0);
389
390         if (tdc)
391            timer_adjust_periodic(z80sti->timer[TIMER_D], ATTOTIME_IN_HZ(device->clock / tdc), 0, ATTOTIME_IN_HZ(device->clock / tdc));
392         else
393            timer_enable(z80sti->timer[TIMER_D], 0);
394
395         if (BIT(data, 7))
396         {
397            LOG(("Z80STI '%s' Timer A Reset\n", device->tag.cstr()));
398            z80sti->to[TIMER_A] = 0;
399
400            devcb_call_write_line(&z80sti->out_tao_func, z80sti->to[TIMER_A]);
401         }
402
403         if (BIT(data, 3))
404         {
405            LOG(("Z80STI '%s' Timer B Reset\n", device->tag.cstr()));
406            z80sti->to[TIMER_B] = 0;
407
408            devcb_call_write_line(&z80sti->out_tbo_func, z80sti->to[TIMER_B]);
409         }
410         }
411         break;
412      }
413      break;
414
415   case Z80STI_REGISTER_GPIP:
416      LOG(("Z80STI '%s' General Purpose I/O Register: %x\n", device->tag.cstr(), data));
417      z80sti->gpip = data & z80sti->ddr;
418      devcb_call_write8(&z80sti->out_gpio_func, 0, z80sti->gpip);
419      break;
420
421   case Z80STI_REGISTER_IPRB:
422      {
423      int i;
424      LOG(("Z80STI '%s' Interrupt Pending Register B: %x\n", device->tag.cstr(), data));
425      z80sti->ipr &= (z80sti->ipr & 0xff00) | data;
426
427      for (i = 0; i < 16; i++)
428      {
429         if (!BIT(z80sti->ipr, i) && (z80sti->int_state[i] == Z80_DAISY_INT)) z80sti->int_state[i] = 0;
430      }
431
432      check_interrupts(z80sti);
433      }
434      break;
435
436   case Z80STI_REGISTER_IPRA:
437      {
438      int i;
439      LOG(("Z80STI '%s' Interrupt Pending Register A: %x\n", device->tag.cstr(), data));
440      z80sti->ipr &= (data << 8) | (z80sti->ipr & 0xff);
441
442      for (i = 0; i < 16; i++)
443      {
444         if (!BIT(z80sti->ipr, i) && (z80sti->int_state[i] == Z80_DAISY_INT)) z80sti->int_state[i] = 0;
445      }
446
447      check_interrupts(z80sti);
448      }
449      break;
450
451   case Z80STI_REGISTER_ISRB:
452      LOG(("Z80STI '%s' Interrupt In-Service Register B: %x\n", device->tag.cstr(), data));
453      z80sti->isr &= (z80sti->isr & 0xff00) | data;
454      break;
455
456   case Z80STI_REGISTER_ISRA:
457      LOG(("Z80STI '%s' Interrupt In-Service Register A: %x\n", device->tag.cstr(), data));
458      z80sti->isr &= (data << 8) | (z80sti->isr & 0xff);
459      break;
460
461   case Z80STI_REGISTER_IMRB:
462      LOG(("Z80STI '%s' Interrupt Mask Register B: %x\n", device->tag.cstr(), data));
463      z80sti->imr = (z80sti->imr & 0xff00) | data;
464      z80sti->isr &= z80sti->imr;
465      check_interrupts(z80sti);
466      break;
467
468   case Z80STI_REGISTER_IMRA:
469      LOG(("Z80STI '%s' Interrupt Mask Register A: %x\n", device->tag.cstr(), data));
470      z80sti->imr = (data << 8) | (z80sti->imr & 0xff);
471      z80sti->isr &= z80sti->imr;
472      check_interrupts(z80sti);
473      break;
474
475   case Z80STI_REGISTER_PVR:
476      LOG(("Z80STI '%s' Interrupt Vector: %02x\n", device->tag.cstr(), data & 0xe0));
477      LOG(("Z80STI '%s' IR Address: %01x\n", device->tag.cstr(), data & 0x07));
478      z80sti->pvr = data;
479      break;
480
481   case Z80STI_REGISTER_TABC:
482      {
483      int tac = PRESCALER[(data >> 4) & 0x07];
484      int tbc = PRESCALER[data & 0x07];
485
486      z80sti->tabc = data;
487
488      LOG(("Z80STI '%s' Timer A Prescaler: %u\n", device->tag.cstr(), tac));
489      LOG(("Z80STI '%s' Timer B Prescaler: %u\n", device->tag.cstr(), tbc));
490
491      if (tac)
492         timer_adjust_periodic(z80sti->timer[TIMER_A], ATTOTIME_IN_HZ(device->clock / tac), 0, ATTOTIME_IN_HZ(device->clock / tac));
493      else
494         timer_enable(z80sti->timer[TIMER_A], 0);
495
496      if (tbc)
497         timer_adjust_periodic(z80sti->timer[TIMER_B], ATTOTIME_IN_HZ(device->clock / tbc), 0, ATTOTIME_IN_HZ(device->clock / tbc));
498      else
499         timer_enable(z80sti->timer[TIMER_B], 0);
500      }
501      break;
502
503   case Z80STI_REGISTER_TBDR:
504      LOG(("Z80STI '%s' Timer B Data Register: %x\n", device->tag.cstr(), data));
505      z80sti->tdr[TIMER_B] = data;
506      break;
507
508   case Z80STI_REGISTER_TADR:
509      LOG(("Z80STI '%s' Timer A Data Register: %x\n", device->tag.cstr(), data));
510      z80sti->tdr[TIMER_A] = data;
511      break;
512#if 0
513    case Z80STI_REGISTER_UCR:
514        z80sti->ucr = data;
515        break;
516
517    case Z80STI_REGISTER_RSR:
518        z80sti->rsr = data;
519        break;
520
521    case Z80STI_REGISTER_TSR:
522        z80sti->tsr = data;
523        break;
524
525    case Z80STI_REGISTER_UDR:
526        z80sti->udr = data;
527        break;
528#endif
529   default:
530      LOG(("Z80STI '%s' Unsupported Register %x\n", device->tag.cstr(), offset & 0x0f));
531   }
532}
533
534/*-------------------------------------------------
535    z80sti_rc_w - receiver clock write
536-------------------------------------------------*/
537
538WRITE_LINE_DEVICE_HANDLER( z80sti_rc_w )
539{
540   z80sti_t *z80sti = get_safe_token(device);
541
542   if (state)
543   {
544      serial_receive(z80sti);
545   }
546}
547
548/*-------------------------------------------------
549    z80sti_tc_w - transmitter clock write
550-------------------------------------------------*/
551
552WRITE_LINE_DEVICE_HANDLER( z80sti_tc_w )
553{
554   z80sti_t *z80sti = get_safe_token(device);
555
556   if (state)
557   {
558      serial_transmit(z80sti);
559   }
560}
561
562/*-------------------------------------------------
563    timer_count - timer count down
564-------------------------------------------------*/
565
566static void timer_count(running_device *device, int index)
567{
568   z80sti_t *z80sti = get_safe_token(device);
569
570   if (z80sti->tmc[index] == 0x01)
571   {
572      //LOG(("Z80STI '%s' Timer %c Expired\n", device->tag, 'A' + index));
573
574      /* toggle timer output signal */
575      z80sti->to[index] = !z80sti->to[index];
576
577      switch (index)
578      {
579      case TIMER_A: devcb_call_write_line(&z80sti->out_tao_func, z80sti->to[index]); break;
580      case TIMER_B: devcb_call_write_line(&z80sti->out_tbo_func, z80sti->to[index]); break;
581      case TIMER_C: devcb_call_write_line(&z80sti->out_tco_func, z80sti->to[index]); break;
582      case TIMER_D: devcb_call_write_line(&z80sti->out_tdo_func, z80sti->to[index]); break;
583      }
584
585      if (z80sti->ier & (1 << INT_LEVEL_TIMER[index]))
586      {
587         LOG(("Z80STI '%s' Interrupt Pending for Timer %c\n", device->tag.cstr(), 'A' + index));
588
589         /* signal timer elapsed interrupt */
590         take_interrupt(z80sti, INT_LEVEL_TIMER[index]);
591      }
592
593      /* load timer main counter */
594      z80sti->tmc[index] = z80sti->tdr[index];
595   }
596   else
597   {
598      /* count down */
599      z80sti->tmc[index]--;
600   }
601}
602
603/*-------------------------------------------------
604    TIMER_CALLBACK( timer_# )
605-------------------------------------------------*/
606
607static TIMER_CALLBACK( timer_a ) { timer_count((running_device *)ptr, TIMER_A); }
608static TIMER_CALLBACK( timer_b ) { timer_count((running_device *)ptr, TIMER_B); }
609static TIMER_CALLBACK( timer_c ) { timer_count((running_device *)ptr, TIMER_C); }
610static TIMER_CALLBACK( timer_d ) { timer_count((running_device *)ptr, TIMER_D); }
611
612/*-------------------------------------------------
613    gpip_input - GPIP input line write
614-------------------------------------------------*/
615
616static void gpip_input(running_device *device, int bit, int state)
617{
618   z80sti_t *z80sti = get_safe_token(device);
619
620   int aer = BIT(z80sti->aer, bit);
621   int old_state = BIT(z80sti->gpip, bit);
622
623   if ((old_state ^ aer) && !(state ^ aer))
624   {
625      LOG(("Z80STI '%s' Edge Transition Detected on Bit: %u\n", device->tag.cstr(), bit));
626
627      if (z80sti->ier & (1 << INT_LEVEL_GPIP[bit]))
628      {
629         LOG(("Z80STI '%s' Interrupt Pending for P%u\n", device->tag.cstr(), bit));
630
631         take_interrupt(z80sti, INT_LEVEL_GPIP[bit]);
632      }
633   }
634
635   z80sti->gpip = (z80sti->gpip & ~(1 << bit)) | (state << bit);
636}
637
638/*-------------------------------------------------
639    z80sti_i#_w - GPIP input line # write
640-------------------------------------------------*/
641
642WRITE_LINE_DEVICE_HANDLER( z80sti_i0_w ) { gpip_input(device, 0, state); }
643WRITE_LINE_DEVICE_HANDLER( z80sti_i1_w ) { gpip_input(device, 1, state); }
644WRITE_LINE_DEVICE_HANDLER( z80sti_i2_w ) { gpip_input(device, 2, state); }
645WRITE_LINE_DEVICE_HANDLER( z80sti_i3_w ) { gpip_input(device, 3, state); }
646WRITE_LINE_DEVICE_HANDLER( z80sti_i4_w ) { gpip_input(device, 4, state); }
647WRITE_LINE_DEVICE_HANDLER( z80sti_i5_w ) { gpip_input(device, 5, state); }
648WRITE_LINE_DEVICE_HANDLER( z80sti_i6_w ) { gpip_input(device, 6, state); }
649WRITE_LINE_DEVICE_HANDLER( z80sti_i7_w ) { gpip_input(device, 7, state); }
650
651/*-------------------------------------------------
652    z80sti_irq_state - get interrupt status
653-------------------------------------------------*/
654
655static int z80sti_irq_state(running_device *device)
656{
657   z80sti_t *z80sti = get_safe_token(device);
658   int state = 0, i;
659
660   /* loop over all interrupt sources */
661   for (i = 15; i >= 0; i--)
662   {
663      /* if we're servicing a request, don't indicate more interrupts */
664      if (z80sti->int_state[i] & Z80_DAISY_IEO)
665      {
666         state |= Z80_DAISY_IEO;
667         break;
668      }
669
670      if (BIT(z80sti->imr, i))
671      {
672         state |= z80sti->int_state[i];
673      }
674   }
675
676   LOG(("Z80STI '%s' Interrupt State: %u\n", device->tag.cstr(), state));
677
678   return state;
679}
680
681/*-------------------------------------------------
682    z80sti_irq_ack - interrupt acknowledge
683-------------------------------------------------*/
684
685static int z80sti_irq_ack(running_device *device)
686{
687   z80sti_t *z80sti = get_safe_token(device);
688   int i;
689
690   /* loop over all interrupt sources */
691   for (i = 15; i >= 0; i--)
692   {
693      /* find the first channel with an interrupt requested */
694      if (z80sti->int_state[i] & Z80_DAISY_INT)
695      {
696         UINT8 vector = (z80sti->pvr & 0xe0) | INT_VECTOR[i];
697
698         /* clear interrupt, switch to the IEO state, and update the IRQs */
699         z80sti->int_state[i] = Z80_DAISY_IEO;
700
701         /* clear interrupt pending register bit */
702         z80sti->ipr &= ~(1 << i);
703
704         /* set interrupt in-service register bit */
705         z80sti->isr |= (1 << i);
706
707         check_interrupts(z80sti);
708
709         LOG(("Z80STI '%s' Interrupt Acknowledge Vector: %02x\n", device->tag.cstr(), vector));
710
711         return vector;
712      }
713   }
714
715   logerror("z80sti_irq_ack: failed to find an interrupt to ack!\n");
716
717   return 0;
718}
719
720/*-------------------------------------------------
721    z80sti_irq_reti - return from interrupt
722-------------------------------------------------*/
723
724static void z80sti_irq_reti(running_device *device)
725{
726   z80sti_t *z80sti = get_safe_token(device);
727   int i;
728
729   LOG(("Z80STI '%s' Return from Interrupt\n", device->tag.cstr()));
730
731   /* loop over all interrupt sources */
732   for (i = 15; i >= 0; i--)
733   {
734      /* find the first channel with an IEO pending */
735      if (z80sti->int_state[i] & Z80_DAISY_IEO)
736      {
737         /* clear the IEO state and update the IRQs */
738         z80sti->int_state[i] &= ~Z80_DAISY_IEO;
739
740         /* clear interrupt in-service register bit */
741         z80sti->isr &= ~(1 << i);
742
743         check_interrupts(z80sti);
744         return;
745      }
746   }
747
748   logerror("z80sti_irq_reti: failed to find an interrupt to clear IEO on!\n");
749}
750
751/*-------------------------------------------------
752    DEVICE_START( z80sti )
753-------------------------------------------------*/
754
755static DEVICE_START( z80sti )
756{
757   z80sti_t *z80sti = get_safe_token(device);
758   const z80sti_interface *intf = (const z80sti_interface *)device->baseconfig().static_config;
759
760   /* resolve callbacks */
761   devcb_resolve_read8(&z80sti->in_gpio_func, &intf->in_gpio_func, device);
762   devcb_resolve_write8(&z80sti->out_gpio_func, &intf->out_gpio_func, device);
763   devcb_resolve_read_line(&z80sti->in_si_func, &intf->in_si_func, device);
764   devcb_resolve_write_line(&z80sti->out_so_func, &intf->out_so_func, device);
765   devcb_resolve_write_line(&z80sti->out_tao_func, &intf->out_tao_func, device);
766   devcb_resolve_write_line(&z80sti->out_tbo_func, &intf->out_tbo_func, device);
767   devcb_resolve_write_line(&z80sti->out_tco_func, &intf->out_tco_func, device);
768   devcb_resolve_write_line(&z80sti->out_tdo_func, &intf->out_tdo_func, device);
769   devcb_resolve_write_line(&z80sti->out_int_func, &intf->out_int_func, device);
770
771   /* create the counter timers */
772   z80sti->timer[TIMER_A] = timer_alloc(device->machine, timer_a, (void *)device);
773   z80sti->timer[TIMER_B] = timer_alloc(device->machine, timer_b, (void *)device);
774   z80sti->timer[TIMER_C] = timer_alloc(device->machine, timer_c, (void *)device);
775   z80sti->timer[TIMER_D] = timer_alloc(device->machine, timer_d, (void *)device);
776
777   /* create serial receive clock timer */
778   if (intf->rx_clock > 0)
779   {
780      z80sti->rx_timer = timer_alloc(device->machine, rx_tick, (void *)device);
781      timer_adjust_periodic(z80sti->rx_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_clock));
782   }
783
784   /* create serial transmit clock timer */
785   if (intf->tx_clock > 0)
786   {
787      z80sti->tx_timer = timer_alloc(device->machine, tx_tick, (void *)device);
788      timer_adjust_periodic(z80sti->tx_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->tx_clock));
789   }
790
791   /* register for state saving */
792   state_save_register_device_item(device, 0, z80sti->gpip);
793   state_save_register_device_item(device, 0, z80sti->aer);
794   state_save_register_device_item(device, 0, z80sti->ddr);
795   state_save_register_device_item(device, 0, z80sti->ier);
796   state_save_register_device_item(device, 0, z80sti->ipr);
797   state_save_register_device_item(device, 0, z80sti->isr);
798   state_save_register_device_item(device, 0, z80sti->imr);
799   state_save_register_device_item(device, 0, z80sti->pvr);
800   state_save_register_device_item_array(device, 0, z80sti->int_state);
801   state_save_register_device_item(device, 0, z80sti->tabc);
802   state_save_register_device_item(device, 0, z80sti->tcdc);
803   state_save_register_device_item_array(device, 0, z80sti->tdr);
804   state_save_register_device_item_array(device, 0, z80sti->tmc);
805   state_save_register_device_item_array(device, 0, z80sti->to);
806   state_save_register_device_item(device, 0, z80sti->scr);
807   state_save_register_device_item(device, 0, z80sti->ucr);
808   state_save_register_device_item(device, 0, z80sti->rsr);
809   state_save_register_device_item(device, 0, z80sti->tsr);
810   state_save_register_device_item(device, 0, z80sti->udr);
811}
812
813/*-------------------------------------------------
814    DEVICE_RESET( z80sti )
815-------------------------------------------------*/
816
817static DEVICE_RESET( z80sti )
818{
819}
820
821/*-------------------------------------------------
822    DEVICE_GET_INFO( z80sti )
823-------------------------------------------------*/
824
825DEVICE_GET_INFO( z80sti )
826{
827   switch (state)
828   {
829      /* --- the following bits of info are returned as 64-bit signed integers --- */
830      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(z80sti_t);                  break;
831      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                           break;
832      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;            break;
833
834      /* --- the following bits of info are returned as pointers to data or functions --- */
835      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(z80sti);      break;
836      case DEVINFO_FCT_STOP:                     /* Nothing */                           break;
837      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(z80sti);      break;
838      case DEVINFO_FCT_IRQ_STATE:                  info->f = (genf *)z80sti_irq_state;            break;
839      case DEVINFO_FCT_IRQ_ACK:                  info->f = (genf *)z80sti_irq_ack;            break;
840      case DEVINFO_FCT_IRQ_RETI:                  info->f = (genf *)z80sti_irq_reti;            break;
841
842      /* --- the following bits of info are returned as NULL-terminated strings --- */
843      case DEVINFO_STR_NAME:                     strcpy(info->s, "Mostek MK3801");            break;
844      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Z80-STI");                  break;
845      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
846      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                  break;
847      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");      break;
848   }
849}
src/mess/machine/z80sti.h
r7536r7537
1/**********************************************************************
2
3    Mostek MK3801 Serial Timer Interrupt Controller (Z80-STI) emulation
4
5    Copyright MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8**********************************************************************
9                            _____   _____
10                   TAO   1 |*    \_/     | 40  Vcc
11                   TBO   2 |             | 39  RC
12                   TCO   3 |             | 38  SI
13                   TDO   4 |             | 37  SO
14                   TCK   5 |             | 36  TC
15                   _M1   6 |             | 35  A0
16                  _RES   7 |             | 34  A1
17                    I0   8 |             | 33  A2
18                    I1   9 |             | 32  A3
19                    I2  10 |   MK3801    | 31  _WR
20                    I3  11 |   Z80-STI   | 30  _CE
21                    I4  12 |             | 29  _RD
22                    I5  13 |             | 28  D7
23                    I6  14 |             | 27  D6
24                    I7  15 |             | 26  D5
25                   IEI  16 |             | 25  D4
26                  _INT  17 |             | 24  D3
27                   IEO  18 |             | 23  D2
28                 _IORQ  19 |             | 22  D1
29                   Vss  20 |_____________| 21  D0
30
31**********************************************************************/
32
33#ifndef __Z80STI__
34#define __Z80STI__
35
36#include "emu.h"
37#include "devcb.h"
38
39/***************************************************************************
40    MACROS / CONSTANTS
41***************************************************************************/
42
43#define Z80STI DEVICE_GET_INFO_NAME( z80sti )
44
45#define MDRV_Z80STI_ADD(_tag, _clock, _config) \
46   MDRV_DEVICE_ADD((_tag), Z80STI, _clock)   \
47   MDRV_DEVICE_CONFIG(_config)
48
49#define Z80STI_INTERFACE(name) const z80sti_interface (name) =
50
51/***************************************************************************
52    TYPE DEFINITIONS
53***************************************************************************/
54
55typedef struct _z80sti_interface z80sti_interface;
56struct _z80sti_interface
57{
58   int   rx_clock;         /* serial receive clock */
59   int   tx_clock;         /* serial transmit clock */
60
61   /* this gets called on each change of the _INT pin (pin 17) */
62   devcb_write_line      out_int_func;
63
64   /* this is called on each read of the GPIO pins */
65   devcb_read8            in_gpio_func;
66
67   /* this is called on each write of the GPIO pins */
68   devcb_write8         out_gpio_func;
69
70   /* this gets called for each read of the SI pin (pin 38) */
71   devcb_read_line         in_si_func;
72
73   /* this gets called for each change of the SO pin (pin 37) */
74   devcb_write_line      out_so_func;
75
76   /* this gets called for each change of the TAO pin (pin 1) */
77   devcb_write_line      out_tao_func;
78
79   /* this gets called for each change of the TBO pin (pin 2) */
80   devcb_write_line      out_tbo_func;
81
82   /* this gets called for each change of the TCO pin (pin 3) */
83   devcb_write_line      out_tco_func;
84
85   /* this gets called for each change of the TDO pin (pin 4) */
86   devcb_write_line      out_tdo_func;
87};
88
89/***************************************************************************
90    PROTOTYPES
91***************************************************************************/
92
93/* device interface */
94DEVICE_GET_INFO( z80sti );
95
96/* register access */
97READ8_DEVICE_HANDLER( z80sti_r );
98WRITE8_DEVICE_HANDLER( z80sti_w );
99
100/* receive clock */
101WRITE_LINE_DEVICE_HANDLER( z80sti_rc_w );
102
103/* transmit clock */
104WRITE_LINE_DEVICE_HANDLER( z80sti_tc_w );
105
106/* GPIP input lines */
107WRITE_LINE_DEVICE_HANDLER( z80sti_i0_w );
108WRITE_LINE_DEVICE_HANDLER( z80sti_i1_w );
109WRITE_LINE_DEVICE_HANDLER( z80sti_i2_w );
110WRITE_LINE_DEVICE_HANDLER( z80sti_i3_w );
111WRITE_LINE_DEVICE_HANDLER( z80sti_i4_w );
112WRITE_LINE_DEVICE_HANDLER( z80sti_i5_w );
113WRITE_LINE_DEVICE_HANDLER( z80sti_i6_w );
114WRITE_LINE_DEVICE_HANDLER( z80sti_i7_w );
115
116#endif
src/mess/machine/z80dart.c
r7536r7537
1/***************************************************************************
2
3    Z80 DART Dual Asynchronous Receiver/Transmitter emulation
4
5    Copyright (c) 2008, The MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8***************************************************************************/
9
10/*
11
12    TODO:
13
14    - break detection
15    - wr0 reset tx interrupt pending
16    - wait/ready
17    - 1.5 stop bits
18
19*/
20
21#include "emu.h"
22#include "z80dart.h"
23#include "cpu/z80/z80.h"
24#include "cpu/z80/z80daisy.h"
25
26/***************************************************************************
27    PARAMETERS
28***************************************************************************/
29
30#define VERBOSE 0
31
32#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
33
34enum
35{
36   STATE_START = 0,
37   STATE_DATA,
38   STATE_PARITY,
39   STATE_STOP,
40   STATE_STOP2
41};
42
43enum
44{
45   INT_TRANSMIT = 0,
46   INT_EXTERNAL,
47   INT_RECEIVE,
48   INT_SPECIAL
49};
50
51#define Z80DART_RR0_RX_CHAR_AVAILABLE   0x01
52#define Z80DART_RR0_INTERRUPT_PENDING   0x02
53#define Z80DART_RR0_TX_BUFFER_EMPTY      0x04
54#define Z80DART_RR0_DCD               0x08
55#define Z80DART_RR0_RI               0x10
56#define Z80DART_RR0_CTS               0x20
57#define Z80DART_RR0_BREAK            0x80 /* not supported */
58
59#define Z80DART_RR1_ALL_SENT         0x01
60#define Z80DART_RR1_PARITY_ERROR      0x10
61#define Z80DART_RR1_RX_OVERRUN_ERROR   0x20
62#define Z80DART_RR1_FRAMING_ERROR      0x40
63
64#define Z80DART_WR0_REGISTER_MASK      0x07
65#define Z80DART_WR0_COMMAND_MASK      0x38
66#define Z80DART_WR0_NULL_CODE         0x00
67#define Z80DART_WR0_RESET_EXT_STATUS   0x10
68#define Z80DART_WR0_CHANNEL_RESET      0x18
69#define Z80DART_WR0_ENABLE_INT_NEXT_RX   0x20
70#define Z80DART_WR0_RESET_TX_INT      0x28 /* not supported */
71#define Z80DART_WR0_ERROR_RESET         0x30
72#define Z80DART_WR0_RETURN_FROM_INT      0x38 /* not supported */
73
74#define Z80DART_WR1_EXT_INT_ENABLE      0x01
75#define Z80DART_WR1_TX_INT_ENABLE      0x02
76#define Z80DART_WR1_STATUS_VECTOR      0x04
77#define Z80DART_WR1_RX_INT_ENABLE_MASK   0x18
78#define Z80DART_WR1_RX_INT_DISABLE      0x00
79#define Z80DART_WR1_RX_INT_FIRST      0x08
80#define Z80DART_WR1_RX_INT_ALL_PARITY   0x10 /* not supported */
81#define Z80DART_WR1_RX_INT_ALL         0x18
82#define Z80DART_WR1_WRDY_ON_RX_TX      0x20 /* not supported */
83#define Z80DART_WR1_WRDY_FUNCTION      0x40 /* not supported */
84#define Z80DART_WR1_WRDY_ENABLE         0x80 /* not supported */
85
86#define Z80DART_WR3_RX_ENABLE         0x01
87#define Z80DART_WR3_AUTO_ENABLES      0x20
88#define Z80DART_WR3_RX_WORD_LENGTH_MASK   0xc0
89#define Z80DART_WR3_RX_WORD_LENGTH_5   0x00
90#define Z80DART_WR3_RX_WORD_LENGTH_7   0x40
91#define Z80DART_WR3_RX_WORD_LENGTH_6   0x80
92#define Z80DART_WR3_RX_WORD_LENGTH_8   0xc0
93
94#define Z80DART_WR4_PARITY_ENABLE      0x01 /* not supported */
95#define Z80DART_WR4_PARITY_EVEN         0x02 /* not supported */
96#define Z80DART_WR4_STOP_BITS_MASK      0x0c
97#define Z80DART_WR4_STOP_BITS_1         0x04
98#define Z80DART_WR4_STOP_BITS_1_5      0x08 /* not supported */
99#define Z80DART_WR4_STOP_BITS_2         0x0c
100#define Z80DART_WR4_CLOCK_MODE_MASK      0xc0
101#define Z80DART_WR4_CLOCK_MODE_X1      0x00
102#define Z80DART_WR4_CLOCK_MODE_X16      0x40
103#define Z80DART_WR4_CLOCK_MODE_X32      0x80
104#define Z80DART_WR4_CLOCK_MODE_X64      0xc0
105
106#define Z80DART_WR5_RTS               0x02
107#define Z80DART_WR5_TX_ENABLE         0x08
108#define Z80DART_WR5_SEND_BREAK         0x10
109#define Z80DART_WR5_TX_WORD_LENGTH_MASK   0xc0
110#define Z80DART_WR5_TX_WORD_LENGTH_5   0x00
111#define Z80DART_WR5_TX_WORD_LENGTH_7   0x40
112#define Z80DART_WR5_TX_WORD_LENGTH_6   0x80
113#define Z80DART_WR5_TX_WORD_LENGTH_8   0xc0
114#define Z80DART_WR5_DTR               0x80
115
116/***************************************************************************
117    TYPE DEFINITIONS
118***************************************************************************/
119
120typedef struct _dart_channel dart_channel;
121struct _dart_channel
122{
123   devcb_resolved_read_line   in_rxd_func;
124   devcb_resolved_write_line   out_txd_func;
125   devcb_resolved_write_line   out_dtr_func;
126   devcb_resolved_write_line   out_rts_func;
127   devcb_resolved_write_line   out_wrdy_func;
128
129   /* register state */
130   UINT8 rr[3];         /* read register */
131   UINT8 wr[6];         /* write register */
132
133   /* receiver state */
134   UINT8 rx_data_fifo[3];   /* receive data FIFO */
135   UINT8 rx_error_fifo[3];   /* receive error FIFO */
136   UINT8 rx_shift;         /* 8-bit receive shift register */
137   UINT8 rx_error;         /* current receive error */
138   int rx_fifo;         /* receive FIFO pointer */
139
140   int rx_clock;         /* receive clock pulse count */
141   int rx_state;         /* receive state */
142   int rx_bits;         /* bits received */
143   int rx_first;         /* first character received */
144   int rx_parity;         /* received data parity */
145   int rx_break;         /* receive break condition */
146   UINT8 rx_rr0_latch;      /* read register 0 latched */
147
148   int ri;               /* ring indicator latch */
149   int cts;            /* clear to send latch */
150   int dcd;            /* data carrier detect latch */
151
152   /* transmitter state */
153   UINT8 tx_data;         /* transmit data register */
154   UINT8 tx_shift;         /* transmit shift register */
155
156   int tx_clock;         /* transmit clock pulse count */
157   int tx_state;         /* transmit state */
158   int tx_bits;         /* bits transmitted */
159   int tx_parity;         /* transmitted data parity */
160
161   int dtr;            /* data terminal ready */
162   int rts;            /* request to send */
163};
164
165typedef struct _z80dart_t z80dart_t;
166struct _z80dart_t
167{
168   devcb_resolved_write_line   out_int_func;
169
170   dart_channel channel[2];      /* channels */
171
172   int int_state[8];            /* interrupt state */
173
174   /* timers */
175   emu_timer *rxca_timer;
176   emu_timer *txca_timer;
177   emu_timer *rxtxcb_timer;
178};
179
180/***************************************************************************
181    FUNCTION PROTOTYPES
182***************************************************************************/
183
184static int z80dart_irq_state(running_device *device);
185static void z80dart_irq_reti(running_device *device);
186
187/***************************************************************************
188    INLINE FUNCTIONS
189***************************************************************************/
190
191INLINE z80dart_t *get_safe_token(running_device *device)
192{
193   assert(device != NULL);
194   assert(device->token != NULL);
195   assert(device->type == Z80DART);
196   return (z80dart_t *)device->token;
197}
198
199INLINE const z80dart_interface *get_interface(running_device *device)
200{
201   assert(device != NULL);
202   assert((device->type == Z80DART));
203   return (const z80dart_interface *) device->baseconfig().static_config;
204}
205
206/***************************************************************************
207    IMPLEMENTATION
208***************************************************************************/
209
210#define RXD \
211   devcb_call_read_line(&ch->in_rxd_func)
212
213#define TXD(_state) \
214   devcb_call_write_line(&ch->out_txd_func, _state)
215
216#define RTS(_state) \
217   devcb_call_write_line(&ch->out_rts_func, _state)
218
219#define DTR(_state) \
220   devcb_call_write_line(&ch->out_dtr_func, _state)
221
222/*-------------------------------------------------
223    check_interrupts - control interrupt line
224-------------------------------------------------*/
225
226static void check_interrupts(running_device *device)
227{
228   z80dart_t *z80dart = get_safe_token(device);
229   int state = (z80dart_irq_state(device) & Z80_DAISY_INT) ? ASSERT_LINE : CLEAR_LINE;
230
231   devcb_call_write_line(&z80dart->out_int_func, state);
232}
233
234/*-------------------------------------------------
235    take_interrupt - trigger interrupt
236-------------------------------------------------*/
237
238static void take_interrupt(running_device *device, int channel, int level)
239{
240   z80dart_t *z80dart = get_safe_token(device);
241   dart_channel *ch = &z80dart->channel[channel];
242   UINT8 vector = ch->wr[2];
243   int priority = (channel << 2) | level;
244
245   LOG(("Z80DART \"%s\" Channel %c : Interrupt Request %u\n", device->tag.cstr(), 'A' + channel, level));
246
247   if ((channel == Z80DART_CH_B) && (ch->wr[1] & Z80DART_WR1_STATUS_VECTOR))
248   {
249      /* status affects vector */
250      vector = (ch->wr[2] & 0xf1) | (!channel << 3) | (level << 1);
251   }
252
253   /* update vector register */
254   ch->rr[2] = vector;
255
256   /* trigger interrupt */
257   z80dart->int_state[priority] |= Z80_DAISY_INT;
258   z80dart->channel[Z80DART_CH_A].rr[0] |= Z80DART_RR0_INTERRUPT_PENDING;
259
260   /* check for interrupt */
261   check_interrupts(device);
262}
263
264/*-------------------------------------------------
265    get_clock_mode - get clock divisor
266-------------------------------------------------*/
267
268static int get_clock_mode(dart_channel *ch)
269{
270   int clocks = 1;
271
272   switch (ch->wr[4] & Z80DART_WR4_CLOCK_MODE_MASK)
273   {
274   case Z80DART_WR4_CLOCK_MODE_X1:      clocks = 1;      break;
275   case Z80DART_WR4_CLOCK_MODE_X16:   clocks = 16;   break;
276   case Z80DART_WR4_CLOCK_MODE_X32:   clocks = 32;   break;
277   case Z80DART_WR4_CLOCK_MODE_X64:   clocks = 64;   break;
278   }
279
280   return clocks;
281}
282
283/*-------------------------------------------------
284    get_stop_bits - get number of stop bits
285-------------------------------------------------*/
286
287static float get_stop_bits(dart_channel *ch)
288{
289   float bits = 1;
290
291   switch (ch->wr[4] & Z80DART_WR4_STOP_BITS_MASK)
292   {
293   case Z80DART_WR4_STOP_BITS_1:      bits = 1;      break;
294   case Z80DART_WR4_STOP_BITS_1_5:      bits = 1.5;      break;
295   case Z80DART_WR4_STOP_BITS_2:      bits = 2;      break;
296   }
297
298   return bits;
299}
300
301/*-------------------------------------------------
302    get_rx_word_length - get receive word length
303-------------------------------------------------*/
304
305static int get_rx_word_length(dart_channel *ch)
306{
307   int bits = 5;
308
309   switch (ch->wr[3] & Z80DART_WR3_RX_WORD_LENGTH_MASK)
310   {
311   case Z80DART_WR3_RX_WORD_LENGTH_5:   bits = 5;      break;
312   case Z80DART_WR3_RX_WORD_LENGTH_6:   bits = 6;      break;
313   case Z80DART_WR3_RX_WORD_LENGTH_7:   bits = 7;      break;
314   case Z80DART_WR3_RX_WORD_LENGTH_8:   bits = 8;      break;
315   }
316
317   return bits;
318}
319
320/*-------------------------------------------------
321    get_rx_word_length - get transmit word length
322-------------------------------------------------*/
323
324static int get_tx_word_length(dart_channel *ch)
325{
326   int bits = 5;
327
328   switch (ch->wr[5] & Z80DART_WR5_TX_WORD_LENGTH_MASK)
329   {
330   case Z80DART_WR5_TX_WORD_LENGTH_5:   bits = 5;   break;
331   case Z80DART_WR5_TX_WORD_LENGTH_6:   bits = 6;   break;
332   case Z80DART_WR5_TX_WORD_LENGTH_7:   bits = 7;   break;
333   case Z80DART_WR5_TX_WORD_LENGTH_8:   bits = 8;   break;
334   }
335
336   return bits;
337}
338
339/*-------------------------------------------------
340    reset_channel - reset channel status
341-------------------------------------------------*/
342
343static void reset_channel(running_device *device, int channel)
344{
345   z80dart_t *z80dart = get_safe_token(device);
346   dart_channel *ch = &z80dart->channel[channel];
347
348   /* disable receiver */
349   ch->wr[3] &= ~Z80DART_WR3_RX_ENABLE;
350   ch->rx_state = STATE_START;
351
352   /* disable transmitter */
353   ch->wr[5] &= ~Z80DART_WR5_TX_ENABLE;
354   ch->tx_state = STATE_START;
355
356   /* reset external lines */
357   RTS(1);
358   DTR(1);
359
360   if (channel == Z80DART_CH_A)
361   {
362      /* reset interrupt logic */
363      int i;
364
365      for (i = 0; i < 8; i++)
366      {
367         z80dart->int_state[i] = 0;
368      }
369
370      check_interrupts(device);
371   }
372}
373
374/*-------------------------------------------------
375    detect_start_bit - detect start bit
376-------------------------------------------------*/
377
378static int detect_start_bit(dart_channel *ch)
379{
380   if (!(ch->wr[3] & Z80DART_WR3_RX_ENABLE)) return 0;
381
382   return !RXD;
383}
384
385/*-------------------------------------------------
386    shift_data_in - shift in serial data
387-------------------------------------------------*/
388
389static void shift_data_in(dart_channel *ch)
390{
391   if (ch->rx_bits < 8)
392   {
393      int rxd = RXD;
394
395      ch->rx_shift >>= 1;
396      ch->rx_shift = (rxd << 7) | (ch->rx_shift & 0x7f);
397      ch->rx_parity ^= rxd;
398      ch->rx_bits++;
399   }
400}
401
402/*-------------------------------------------------
403    character_completed - check if complete
404    data word has been transferred
405-------------------------------------------------*/
406
407static int character_completed(dart_channel *ch)
408{
409   return ch->rx_bits == get_rx_word_length(ch);
410}
411
412/*-------------------------------------------------
413    detect_parity_error - detect parity error
414-------------------------------------------------*/
415
416static void detect_parity_error(running_device *device, int channel)
417{
418   z80dart_t *z80dart = get_safe_token(device);
419   dart_channel *ch = &z80dart->channel[channel];
420
421   int parity = (ch->wr[1] & Z80DART_WR4_PARITY_EVEN) ? 1 : 0;
422
423   if (RXD != (ch->rx_parity ^ parity))
424   {
425      /* parity error detected */
426      ch->rx_error |= Z80DART_RR1_PARITY_ERROR;
427
428      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
429      {
430      case Z80DART_WR1_RX_INT_FIRST:
431         if (!ch->rx_first)
432         {
433            take_interrupt(device, channel, INT_SPECIAL);
434         }
435         break;
436
437      case Z80DART_WR1_RX_INT_ALL_PARITY:
438         take_interrupt(device, channel, INT_SPECIAL);
439         break;
440
441      case Z80DART_WR1_RX_INT_ALL:
442         take_interrupt(device, channel, INT_RECEIVE);
443         break;
444      }
445   }
446}
447
448/*-------------------------------------------------
449    detect_framing_error - detect framing error
450-------------------------------------------------*/
451
452static void detect_framing_error(running_device *device, int channel)
453{
454   z80dart_t *z80dart = get_safe_token(device);
455   dart_channel *ch = &z80dart->channel[channel];
456
457   if (!RXD)
458   {
459      /* framing error detected */
460      ch->rx_error |= Z80DART_RR1_FRAMING_ERROR;
461
462      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
463      {
464      case Z80DART_WR1_RX_INT_FIRST:
465         if (!ch->rx_first)
466         {
467            take_interrupt(device, channel, INT_SPECIAL);
468         }
469         break;
470
471      case Z80DART_WR1_RX_INT_ALL_PARITY:
472      case Z80DART_WR1_RX_INT_ALL:
473         take_interrupt(device, channel, INT_SPECIAL);
474         break;
475      }
476   }
477}
478
479/*-------------------------------------------------
480    receive - receive serial data
481-------------------------------------------------*/
482
483static void receive(running_device *device, int channel)
484{
485   z80dart_t *z80dart = get_safe_token(device);
486   dart_channel *ch = &z80dart->channel[channel];
487
488   float stop_bits = get_stop_bits(ch);
489
490   switch (ch->rx_state)
491   {
492   case STATE_START:
493      /* check for start bit */
494      if (detect_start_bit(ch))
495      {
496         /* start bit detected */
497         ch->rx_shift = 0;
498         ch->rx_error = 0;
499         ch->rx_bits = 0;
500         ch->rx_parity = 0;
501
502         /* next bit is a data bit */
503         ch->rx_state = STATE_DATA;
504      }
505      break;
506
507   case STATE_DATA:
508      /* shift bit into shift register */
509      shift_data_in(ch);
510
511      if (character_completed(ch))
512      {
513         /* all data bits received */
514         if (ch->wr[4] & Z80DART_WR4_PARITY_ENABLE)
515         {
516            /* next bit is the parity bit */
517            ch->rx_state = STATE_PARITY;
518         }
519         else
520         {
521            /* next bit is a STOP bit */
522            if (stop_bits == 1)
523               ch->rx_state = STATE_STOP2;
524            else
525               ch->rx_state = STATE_STOP;
526         }
527      }
528      break;
529
530   case STATE_PARITY:
531      /* shift bit into shift register */
532      shift_data_in(ch);
533
534      /* check for parity error */
535      detect_parity_error(device, channel);
536
537      /* next bit is a STOP bit */
538      if (stop_bits == 1)
539         ch->rx_state = STATE_STOP2;
540      else
541         ch->rx_state = STATE_STOP;
542      break;
543
544   case STATE_STOP:
545      /* shift bit into shift register */
546      shift_data_in(ch);
547
548      /* check for framing error */
549      detect_framing_error(device, channel);
550
551      /* next bit is the second STOP bit */
552      ch->rx_state = STATE_STOP2;
553      break;
554
555   case STATE_STOP2:
556      /* shift bit into shift register */
557      shift_data_in(ch);
558
559      /* check for framing error */
560      detect_framing_error(device, channel);
561
562      /* store data into FIFO */
563      z80dart_receive_data(device, channel, ch->rx_shift);
564
565      /* next bit is the START bit */
566      ch->rx_state = STATE_START;
567      break;
568   }
569}
570
571/*-------------------------------------------------
572    transmit - transmit serial data
573-------------------------------------------------*/
574
575static void transmit(running_device *device, int channel)
576{
577   z80dart_t *z80dart = get_safe_token(device);
578   dart_channel *ch = &z80dart->channel[channel];
579
580   int word_length = get_tx_word_length(ch);
581   float stop_bits = get_stop_bits(ch);
582
583   switch (ch->tx_state)
584   {
585   case STATE_START:
586      if ((ch->wr[5] & Z80DART_WR5_TX_ENABLE) && !(ch->rr[0] & Z80DART_RR0_TX_BUFFER_EMPTY))
587      {
588         /* transmit start bit */
589         TXD(0);
590
591         ch->tx_bits = 0;
592         ch->tx_shift = ch->tx_data;
593
594         /* empty transmit buffer */
595         ch->rr[0] |= Z80DART_RR0_TX_BUFFER_EMPTY;
596
597         if (ch->wr[1] & Z80DART_WR1_TX_INT_ENABLE)
598            take_interrupt(device, channel, INT_TRANSMIT);
599
600         ch->tx_state = STATE_DATA;
601      }
602      else if (ch->wr[5] & Z80DART_WR5_SEND_BREAK)
603      {
604         /* transmit break */
605         TXD(0);
606      }
607      else
608      {
609         /* transmit marking line */
610         TXD(1);
611      }
612
613      break;
614
615   case STATE_DATA:
616      /* transmit data bit */
617      TXD(BIT(ch->tx_shift, 0));
618
619      /* shift data */
620      ch->tx_shift >>= 1;
621      ch->tx_bits++;
622
623      if (ch->rx_bits == word_length)
624      {
625         if (ch->wr[4] & Z80DART_WR4_PARITY_ENABLE)
626            ch->tx_state = STATE_PARITY;
627         else
628         {
629            if (stop_bits == 1)
630               ch->tx_state = STATE_STOP2;
631            else
632               ch->tx_state = STATE_STOP;
633         }
634      }
635      break;
636
637   case STATE_PARITY:
638      // TODO: calculate parity
639      if (stop_bits == 1)
640         ch->tx_state = STATE_STOP2;
641      else
642         ch->tx_state = STATE_STOP;
643      break;
644
645   case STATE_STOP:
646      /* transmit stop bit */
647      TXD(1);
648
649      ch->tx_state = STATE_STOP2;
650      break;
651
652   case STATE_STOP2:
653      /* transmit stop bit */
654      TXD(1);
655
656      /* if transmit buffer is empty */
657      if (ch->rr[0] & Z80DART_RR0_TX_BUFFER_EMPTY)
658      {
659         /* then all characters have been sent */
660         ch->rr[1] |= Z80DART_RR1_ALL_SENT;
661
662         /* when the RTS bit is reset, the _RTS output goes high after the transmitter empties */
663         if (!ch->rts)
664            RTS(1);
665      }
666
667      ch->tx_state = STATE_START;
668      break;
669   }
670}
671
672/*-------------------------------------------------
673    z80dart_c_r - read control register
674-------------------------------------------------*/
675
676READ8_DEVICE_HANDLER( z80dart_c_r )
677{
678   z80dart_t *z80dart = get_safe_token(device);
679   int channel = offset & 0x01;
680   dart_channel *ch = &z80dart->channel[channel];
681   UINT8 data = 0;
682
683   int reg = ch->wr[0] & Z80DART_WR0_REGISTER_MASK;
684
685   switch (reg)
686   {
687   case 0:
688   case 1:
689      data = ch->rr[reg];
690      break;
691
692   case 2:
693      /* channel B only */
694      if (channel == Z80DART_CH_B)
695         data = ch->rr[reg];
696      break;
697   }
698
699   LOG(("Z80DART \"%s\" Channel %c : Control Register Read '%02x'\n", device->tag.cstr(), 'A' + channel, data));
700
701   return data;
702}
703
704/*-------------------------------------------------
705    z80dart_c_w - write control register
706-------------------------------------------------*/
707
708WRITE8_DEVICE_HANDLER( z80dart_c_w )
709{
710   z80dart_t *z80dart = get_safe_token(device);
711   int channel = offset & 0x01;
712   dart_channel *ch = &z80dart->channel[channel];
713
714   int reg = ch->wr[0] & Z80DART_WR0_REGISTER_MASK;
715
716   LOG(("Z80DART \"%s\" Channel %c : Control Register Write '%02x'\n", device->tag.cstr(), 'A' + channel, data));
717
718   /* write data to selected register */
719   ch->wr[reg] = data;
720
721   if (reg != 0)
722   {
723      /* mask out register index */
724      ch->wr[0] &= ~Z80DART_WR0_REGISTER_MASK;
725   }
726
727   switch (reg)
728   {
729   case 0:
730      switch (data & Z80DART_WR0_COMMAND_MASK)
731      {
732      case Z80DART_WR0_RESET_EXT_STATUS:
733         /* reset external/status interrupt */
734         ch->rr[0] &= ~(Z80DART_RR0_DCD | Z80DART_RR0_RI | Z80DART_RR0_CTS | Z80DART_RR0_BREAK);
735
736         if (!ch->dcd) ch->rr[0] |= Z80DART_RR0_DCD;
737         if (ch->ri) ch->rr[0] |= Z80DART_RR0_RI;
738         if (ch->cts) ch->rr[0] |= Z80DART_RR0_CTS;
739
740         ch->rx_rr0_latch = 0;
741
742         LOG(("Z80DART \"%s\" Channel %c : Reset External/Status Interrupt\n", device->tag.cstr(), 'A' + channel));
743         break;
744
745      case Z80DART_WR0_CHANNEL_RESET:
746         /* channel reset */
747         LOG(("Z80DART \"%s\" Channel %c : Channel Reset\n", device->tag.cstr(), 'A' + channel));
748         reset_channel(device, channel);
749         break;
750
751      case Z80DART_WR0_ENABLE_INT_NEXT_RX:
752         /* enable interrupt on next receive character */
753         LOG(("Z80DART \"%s\" Channel %c : Enable Interrupt on Next Received Character\n", device->tag.cstr(), 'A' + channel));
754         ch->rx_first = 1;
755         break;
756
757      case Z80DART_WR0_RESET_TX_INT:
758         /* reset transmitter interrupt pending */
759         LOG(("Z80DART \"%s\" Channel %c : Reset Transmitter Interrupt Pending\n", device->tag.cstr(), 'A' + channel));
760         break;
761
762      case Z80DART_WR0_ERROR_RESET:
763         /* error reset */
764         LOG(("Z80DART \"%s\" Channel %c : Error Reset\n", device->tag.cstr(), 'A' + channel));
765         ch->rr[1] &= ~(Z80DART_RR1_FRAMING_ERROR | Z80DART_RR1_RX_OVERRUN_ERROR | Z80DART_RR1_PARITY_ERROR);
766         break;
767
768      case Z80DART_WR0_RETURN_FROM_INT:
769         /* return from interrupt */
770         LOG(("Z80DART \"%s\" Channel %c : Return from Interrupt\n", device->tag.cstr(), 'A' + channel));
771         z80dart_irq_reti(device);
772         break;
773      }
774      break;
775
776   case 1:
777      LOG(("Z80DART \"%s\" Channel %c : External Interrupt Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_EXT_INT_ENABLE) ? 1 : 0));
778      LOG(("Z80DART \"%s\" Channel %c : Transmit Interrupt Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_TX_INT_ENABLE) ? 1 : 0));
779      LOG(("Z80DART \"%s\" Channel %c : Status Affects Vector %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_STATUS_VECTOR) ? 1 : 0));
780      LOG(("Z80DART \"%s\" Channel %c : Wait/Ready Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR1_WRDY_ENABLE) ? 1 : 0));
781
782      switch (data & Z80DART_WR1_RX_INT_ENABLE_MASK)
783      {
784      case Z80DART_WR1_RX_INT_DISABLE:
785         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt Disabled\n", device->tag.cstr(), 'A' + channel));
786         break;
787
788      case Z80DART_WR1_RX_INT_FIRST:
789         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on First Character\n", device->tag.cstr(), 'A' + channel));
790         break;
791
792      case Z80DART_WR1_RX_INT_ALL_PARITY:
793         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on All Characters, Parity Affects Vector\n", device->tag.cstr(), 'A' + channel));
794         break;
795
796      case Z80DART_WR1_RX_INT_ALL:
797         LOG(("Z80DART \"%s\" Channel %c : Receiver Interrupt on All Characters\n", device->tag.cstr(), 'A' + channel));
798         break;
799      }
800
801      check_interrupts(device);
802      break;
803
804   case 2:
805      /* interrupt vector */
806      check_interrupts(device);
807      LOG(("Z80DART \"%s\" Channel %c : Interrupt Vector %02x\n", device->tag.cstr(), 'A' + channel, data));
808      break;
809
810   case 3:
811      LOG(("Z80DART \"%s\" Channel %c : Receiver Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR3_RX_ENABLE) ? 1 : 0));
812      LOG(("Z80DART \"%s\" Channel %c : Auto Enables %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR3_AUTO_ENABLES) ? 1 : 0));
813      LOG(("Z80DART \"%s\" Channel %c : Receiver Bits/Character %u\n", device->tag.cstr(), 'A' + channel, get_rx_word_length(ch)));
814      break;
815
816   case 4:
817      LOG(("Z80DART \"%s\" Channel %c : Parity Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR4_PARITY_ENABLE) ? 1 : 0));
818      LOG(("Z80DART \"%s\" Channel %c : Parity %s\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR4_PARITY_EVEN) ? "Even" : "Odd"));
819      LOG(("Z80DART \"%s\" Channel %c : Stop Bits %f\n", device->tag.cstr(), 'A' + channel, get_stop_bits(ch)));
820      LOG(("Z80DART \"%s\" Channel %c : Clock Mode %uX\n", device->tag.cstr(), 'A' + channel, get_clock_mode(ch)));
821      break;
822
823   case 5:
824      LOG(("Z80DART \"%s\" Channel %c : Transmitter Enable %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_TX_ENABLE) ? 1 : 0));
825      LOG(("Z80DART \"%s\" Channel %c : Transmitter Bits/Character %u\n", device->tag.cstr(), 'A' + channel, get_tx_word_length(ch)));
826      LOG(("Z80DART \"%s\" Channel %c : Send Break %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_SEND_BREAK) ? 1 : 0));
827      LOG(("Z80DART \"%s\" Channel %c : Request to Send %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_RTS) ? 1 : 0));
828      LOG(("Z80DART \"%s\" Channel %c : Data Terminal Ready %u\n", device->tag.cstr(), 'A' + channel, (data & Z80DART_WR5_DTR) ? 1 : 0));
829
830      if (data & Z80DART_WR5_RTS)
831      {
832         /* when the RTS bit is set, the _RTS output goes low */
833         RTS(0);
834
835         ch->rts = 1;
836      }
837      else
838      {
839         /* when the RTS bit is reset, the _RTS output goes high after the transmitter empties */
840         ch->rts = 0;
841      }
842
843      /* data terminal ready output follows the state programmed into the DTR bit*/
844      ch->dtr = (data & Z80DART_WR5_DTR) ? 0 : 1;
845      DTR(ch->dtr);
846      break;
847   }
848}
849
850/*-------------------------------------------------
851    z80dart_d_r - read data register
852-------------------------------------------------*/
853
854READ8_DEVICE_HANDLER( z80dart_d_r )
855{
856   z80dart_t *z80dart = get_safe_token(device);
857   int channel = offset & 0x01;
858   dart_channel *ch = &z80dart->channel[channel];
859   UINT8 data = 0;
860
861   if (ch->rx_fifo >= 0)
862   {
863      /* load data from the FIFO */
864      data = ch->rx_data_fifo[ch->rx_fifo];
865
866      /* load error status from the FIFO, retain overrun and parity errors */
867      ch->rr[1] = (ch->rr[1] & (Z80DART_RR1_RX_OVERRUN_ERROR | Z80DART_RR1_PARITY_ERROR)) | ch->rx_error_fifo[ch->rx_fifo];
868
869      /* decrease FIFO pointer */
870      ch->rx_fifo--;
871
872      if (ch->rx_fifo < 0)
873      {
874         /* no more characters available in the FIFO */
875         ch->rr[0] &= ~ Z80DART_RR0_RX_CHAR_AVAILABLE;
876      }
877   }
878
879   LOG(("Z80DART \"%s\" Channel %c : Data Register Read '%02x'\n", device->tag.cstr(), 'A' + channel, data));
880
881   return data;
882}
883
884/*-------------------------------------------------
885    z80dart_d_w - write data register
886-------------------------------------------------*/
887
888WRITE8_DEVICE_HANDLER( z80dart_d_w )
889{
890   z80dart_t *z80dart = get_safe_token(device);
891   int channel = offset & 0x01;
892   dart_channel *ch = &z80dart->channel[channel];
893
894   ch->tx_data = data;
895
896   ch->rr[0] &= ~Z80DART_RR0_TX_BUFFER_EMPTY;
897   ch->rr[1] &= ~Z80DART_RR1_ALL_SENT;
898
899   LOG(("Z80DART \"%s\" Channel %c : Data Register Write '%02x'\n", device->tag.cstr(), 'A' + channel, data));
900}
901
902/*-------------------------------------------------
903    z80dart_receive_data - receive data word
904-------------------------------------------------*/
905
906void z80dart_receive_data(running_device *device, int channel, UINT8 data)
907{
908   z80dart_t *z80dart = get_safe_token(device);
909   dart_channel *ch = &z80dart->channel[channel];
910
911   LOG(("Z80DART \"%s\" Channel %c : Receive Data Byte '%02x'\n", device->tag.cstr(), 'A' + channel, data));
912
913   if (ch->rx_fifo == 2)
914   {
915      /* receive overrun error detected */
916      ch->rx_error |= Z80DART_RR1_RX_OVERRUN_ERROR;
917
918      switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
919      {
920      case Z80DART_WR1_RX_INT_FIRST:
921         if (!ch->rx_first)
922         {
923            take_interrupt(device, channel, INT_SPECIAL);
924         }
925         break;
926
927      case Z80DART_WR1_RX_INT_ALL_PARITY:
928      case Z80DART_WR1_RX_INT_ALL:
929         take_interrupt(device, channel, INT_SPECIAL);
930         break;
931      }
932   }
933   else
934   {
935      ch->rx_fifo++;
936   }
937
938   /* store received character and error status into FIFO */
939   ch->rx_data_fifo[ch->rx_fifo] = data;
940   ch->rx_error_fifo[ch->rx_fifo] = ch->rx_error;
941
942   ch->rr[0] |= Z80DART_RR0_RX_CHAR_AVAILABLE;
943
944   /* receive interrupt */
945   switch (ch->wr[1] & Z80DART_WR1_RX_INT_ENABLE_MASK)
946   {
947   case Z80DART_WR1_RX_INT_FIRST:
948      if (ch->rx_first)
949      {
950         take_interrupt(device, channel, INT_RECEIVE);
951
952         ch->rx_first = 0;
953      }
954      break;
955
956   case Z80DART_WR1_RX_INT_ALL_PARITY:
957   case Z80DART_WR1_RX_INT_ALL:
958      take_interrupt(device, channel, INT_RECEIVE);
959      break;
960   }
961}
962
963/*-------------------------------------------------
964    cts_w - clear to send handler
965-------------------------------------------------*/
966
967static void cts_w(running_device *device, int channel, int state)
968{
969   z80dart_t *z80dart = get_safe_token(device);
970   dart_channel *ch = &z80dart->channel[channel];
971
972   LOG(("Z80DART \"%s\" Channel %c : CTS %u\n", device->tag.cstr(), 'A' + channel, state));
973
974   if (ch->cts != state)
975   {
976      /* enable transmitter if in auto enables mode */
977      if (!state)
978         if (ch->wr[3] & Z80DART_WR3_AUTO_ENABLES)
979            ch->wr[5] |= Z80DART_WR5_TX_ENABLE;
980
981      /* set clear to send */
982      ch->cts = state;
983
984      if (!ch->rx_rr0_latch)
985      {
986         if (!ch->cts)
987            ch->rr[0] |= Z80DART_RR0_CTS;
988         else
989            ch->rr[0] &= ~Z80DART_RR0_CTS;
990
991         /* trigger interrupt */
992         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
993         {
994            /* trigger interrupt */
995            take_interrupt(device, channel, INT_EXTERNAL);
996
997            /* latch read register 0 */
998            ch->rx_rr0_latch = 1;
999         }
1000      }
1001   }
1002}
1003
1004/*-------------------------------------------------
1005    z80dart_ctsa_w - clear to send (channel A)
1006-------------------------------------------------*/
1007
1008WRITE_LINE_DEVICE_HANDLER( z80dart_ctsa_w )
1009{
1010   cts_w(device, Z80DART_CH_A, state);
1011}
1012
1013/*-------------------------------------------------
1014    z80dart_ctsb_w - clear to send (channel B)
1015-------------------------------------------------*/
1016
1017WRITE_LINE_DEVICE_HANDLER( z80dart_ctsb_w )
1018{
1019   cts_w(device, Z80DART_CH_B, state);
1020}
1021
1022/*-------------------------------------------------
1023    dcd_w - data carrier detected handler
1024-------------------------------------------------*/
1025
1026static void dcd_w(running_device *device, int channel, int state)
1027{
1028   z80dart_t *z80dart = get_safe_token(device);
1029   dart_channel *ch = &z80dart->channel[channel];
1030
1031   LOG(("Z80DART \"%s\" Channel %c : DCD %u\n", device->tag.cstr(), 'A' + channel, state));
1032
1033   if (ch->dcd != state)
1034   {
1035      /* enable receiver if in auto enables mode */
1036      if (!state)
1037         if (ch->wr[3] & Z80DART_WR3_AUTO_ENABLES)
1038            ch->wr[3] |= Z80DART_WR3_RX_ENABLE;
1039
1040      /* set data carrier detect */
1041      ch->dcd = state;
1042
1043      if (!ch->rx_rr0_latch)
1044      {
1045         if (ch->dcd)
1046            ch->rr[0] |= Z80DART_RR0_DCD;
1047         else
1048            ch->rr[0] &= ~Z80DART_RR0_DCD;
1049
1050         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
1051         {
1052            /* trigger interrupt */
1053            take_interrupt(device, channel, INT_EXTERNAL);
1054
1055            /* latch read register 0 */
1056            ch->rx_rr0_latch = 1;
1057         }
1058      }
1059   }
1060}
1061
1062/*-------------------------------------------------
1063    z80dart_dcda_w - data carrier detected
1064    (channel A)
1065-------------------------------------------------*/
1066
1067WRITE_LINE_DEVICE_HANDLER( z80dart_dcda_w )
1068{
1069   dcd_w(device, Z80DART_CH_A, state);
1070}
1071
1072/*-------------------------------------------------
1073    z80dart_dcdb_w - data carrier detected
1074    (channel B)
1075-------------------------------------------------*/
1076
1077WRITE_LINE_DEVICE_HANDLER( z80dart_dcdb_w )
1078{
1079   dcd_w(device, Z80DART_CH_B, state);
1080}
1081
1082/*-------------------------------------------------
1083    ri_w - ring indicator handler
1084-------------------------------------------------*/
1085
1086static void ri_w(running_device *device, int channel, int state)
1087{
1088   z80dart_t *z80dart = get_safe_token(device);
1089   dart_channel *ch = &z80dart->channel[channel];
1090
1091   LOG(("Z80DART \"%s\" Channel %c : RI %u\n", device->tag.cstr(), 'A' + channel, state));
1092
1093   if (ch->ri != state)
1094   {
1095      /* set ring indicator state */
1096      ch->ri = state;
1097
1098      if (!ch->rx_rr0_latch)
1099      {
1100         if (ch->ri)
1101            ch->rr[0] |= Z80DART_RR0_RI;
1102         else
1103            ch->rr[0] &= ~Z80DART_RR0_RI;
1104
1105         if (ch->wr[1] & Z80DART_WR1_EXT_INT_ENABLE)
1106         {
1107            /* trigger interrupt */
1108            take_interrupt(device, channel, INT_EXTERNAL);
1109
1110            /* latch read register 0 */
1111            ch->rx_rr0_latch = 1;
1112         }
1113      }
1114   }
1115}
1116
1117/*-------------------------------------------------
1118    z80dart_ria_w - ring indicator (channel A)
1119-------------------------------------------------*/
1120
1121WRITE_LINE_DEVICE_HANDLER( z80dart_ria_w )
1122{
1123   ri_w(device, Z80DART_CH_A, state);
1124}
1125
1126/*-------------------------------------------------
1127    z80dart_rib_w - ring indicator (channel B)
1128-------------------------------------------------*/
1129
1130WRITE_LINE_DEVICE_HANDLER( z80dart_rib_w )
1131{
1132   ri_w(device, Z80DART_CH_B, state);
1133}
1134
1135/*-------------------------------------------------
1136    z80dart_rxca_w - receive clock (channel A)
1137-------------------------------------------------*/
1138
1139WRITE_LINE_DEVICE_HANDLER( z80dart_rxca_w )
1140{
1141   z80dart_t *z80dart = get_safe_token(device);
1142   dart_channel *ch = &z80dart->channel[Z80DART_CH_A];
1143
1144   int clocks = get_clock_mode(ch);
1145
1146   if (!state) return;
1147
1148   LOG(("Z80DART \"%s\" Channel A : Receiver Clock Pulse\n", device->tag.cstr()));
1149
1150   ch->rx_clock++;
1151
1152   if (ch->rx_clock == clocks)
1153   {
1154      ch->rx_clock = 0;
1155
1156      /* receive data */
1157      receive(device, Z80DART_CH_A);
1158   }
1159}
1160
1161/*-------------------------------------------------
1162    z80dart_txca_w - transmit clock (channel A)
1163-------------------------------------------------*/
1164
1165WRITE_LINE_DEVICE_HANDLER( z80dart_txca_w )
1166{
1167   z80dart_t *z80dart = get_safe_token(device);
1168   dart_channel *ch = &z80dart->channel[Z80DART_CH_A];
1169
1170   int clocks = get_clock_mode(ch);
1171
1172   if (!state) return;
1173
1174   LOG(("Z80DART \"%s\" Channel A : Transmitter Clock Pulse\n", device->tag.cstr()));
1175
1176   ch->tx_clock++;
1177
1178   if (ch->tx_clock == clocks)
1179   {
1180      ch->tx_clock = 0;
1181
1182      /* transmit data */
1183      transmit(device, Z80DART_CH_A);
1184   }
1185}
1186
1187/*-------------------------------------------------
1188    z80dart_rxtxcb_w - receive/transmit clock
1189    (channel B)
1190-------------------------------------------------*/
1191
1192WRITE_LINE_DEVICE_HANDLER( z80dart_rxtxcb_w )
1193{
1194   z80dart_t *z80dart = get_safe_token(device);
1195   dart_channel *ch = &z80dart->channel[Z80DART_CH_B];
1196
1197   int clocks = get_clock_mode(ch);
1198
1199   if (!state) return;
1200
1201   LOG(("Z80DART \"%s\" Channel A : Receiver/Transmitter Clock Pulse\n", device->tag.cstr()));
1202
1203   ch->rx_clock++;
1204   ch->tx_clock++;
1205
1206   if (ch->rx_clock == clocks)
1207   {
1208      ch->rx_clock = 0;
1209      ch->tx_clock = 0;
1210
1211      /* receive and transmit data */
1212      receive(device, Z80DART_CH_B);
1213      transmit(device, Z80DART_CH_B);
1214   }
1215}
1216
1217/*-------------------------------------------------
1218    TIMER_CALLBACK( rxca_tick )
1219-------------------------------------------------*/
1220
1221static TIMER_CALLBACK( rxca_tick )
1222{
1223   z80dart_rxca_w((running_device *)ptr, 1);
1224}
1225
1226/*-------------------------------------------------
1227    TIMER_CALLBACK( txca_tick )
1228-------------------------------------------------*/
1229
1230static TIMER_CALLBACK( txca_tick )
1231{
1232   z80dart_txca_w((running_device *)ptr, 1);
1233}
1234
1235/*-------------------------------------------------
1236    TIMER_CALLBACK( rxtxcb_tick )
1237-------------------------------------------------*/
1238
1239static TIMER_CALLBACK( rxtxcb_tick )
1240{
1241   z80dart_rxtxcb_w((running_device *)ptr, 1);
1242}
1243
1244/*-------------------------------------------------
1245    z80dart_irq_state - get interrupt status
1246-------------------------------------------------*/
1247
1248static int z80dart_irq_state(running_device *device)
1249{
1250   z80dart_t *z80dart = get_safe_token( device );
1251   int state = 0;
1252   int i;
1253
1254   LOG(("Z80DART \"%s\" : Interrupt State B:%d%d%d%d A:%d%d%d%d\n", device->tag.cstr(),
1255            z80dart->int_state[0], z80dart->int_state[1], z80dart->int_state[2], z80dart->int_state[3],
1256            z80dart->int_state[4], z80dart->int_state[5], z80dart->int_state[6], z80dart->int_state[7]));
1257
1258   /* loop over all interrupt sources */
1259   for (i = 0; i < 8; i++)
1260   {
1261      /* if we're servicing a request, don't indicate more interrupts */
1262      if (z80dart->int_state[i] & Z80_DAISY_IEO)
1263      {
1264         state |= Z80_DAISY_IEO;
1265         break;
1266      }
1267      state |= z80dart->int_state[i];
1268   }
1269
1270   LOG(("Z80DART \"%s\" : Interrupt State %u\n", device->tag.cstr(), state));
1271
1272   return state;
1273}
1274
1275/*-------------------------------------------------
1276    z80dart_irq_ack - interrupt acknowledge
1277-------------------------------------------------*/
1278
1279static int z80dart_irq_ack(running_device *device)
1280{
1281   z80dart_t *z80dart = get_safe_token( device );
1282   int i;
1283
1284   LOG(("Z80DART \"%s\" Interrupt Acknowledge\n", device->tag.cstr()));
1285
1286   /* loop over all interrupt sources */
1287   for (i = 0; i < 8; i++)
1288   {
1289      /* find the first channel with an interrupt requested */
1290      if (z80dart->int_state[i] & Z80_DAISY_INT)
1291      {
1292         /* clear interrupt, switch to the IEO state, and update the IRQs */
1293         z80dart->int_state[i] = Z80_DAISY_IEO;
1294         z80dart->channel[Z80DART_CH_A].rr[0] &= ~Z80DART_RR0_INTERRUPT_PENDING;
1295         check_interrupts(device);
1296
1297         LOG(("Z80DART \"%s\" : Interrupt Acknowledge Vector %02x\n", device->tag.cstr(), z80dart->channel[Z80DART_CH_B].rr[2]));
1298
1299         return z80dart->channel[Z80DART_CH_B].rr[2];
1300      }
1301   }
1302
1303   logerror("z80dart_irq_ack: failed to find an interrupt to ack!\n");
1304
1305   return z80dart->channel[Z80DART_CH_B].rr[2];
1306}
1307
1308/*-------------------------------------------------
1309    z80dart_irq_reti - return from interrupt
1310-------------------------------------------------*/
1311
1312static void z80dart_irq_reti(running_device *device)
1313{
1314   z80dart_t *z80dart = get_safe_token( device );
1315   int i;
1316
1317   LOG(("Z80DART \"%s\" Return from Interrupt\n", device->tag.cstr()));
1318
1319   /* loop over all interrupt sources */
1320   for (i = 0; i < 8; i++)
1321   {
1322      /* find the first channel with an IEO pending */
1323      if (z80dart->int_state[i] & Z80_DAISY_IEO)
1324      {
1325         /* clear the IEO state and update the IRQs */
1326         z80dart->int_state[i] &= ~Z80_DAISY_IEO;
1327         check_interrupts(device);
1328         return;
1329      }
1330   }
1331
1332   logerror("z80dart_irq_reti: failed to find an interrupt to clear IEO on!\n");
1333}
1334
1335/*-------------------------------------------------
1336    z80dart_cd_ba_r - register read
1337-------------------------------------------------*/
1338
1339READ8_DEVICE_HANDLER( z80dart_cd_ba_r )
1340{
1341   return (offset & 2) ? z80dart_c_r(device, offset & 1) : z80dart_d_r(device, offset & 1);
1342}
1343
1344/*-------------------------------------------------
1345    z80dart_cd_ba_w - register write
1346-------------------------------------------------*/
1347
1348WRITE8_DEVICE_HANDLER( z80dart_cd_ba_w )
1349{
1350   if (offset & 2)
1351      z80dart_c_w(device, offset & 1, data);
1352   else
1353      z80dart_d_w(device, offset & 1, data);
1354}
1355
1356/*-------------------------------------------------
1357    z80dart_ba_cd_r - register read
1358-------------------------------------------------*/
1359
1360READ8_DEVICE_HANDLER( z80dart_ba_cd_r )
1361{
1362   int channel = BIT(offset, 1);
1363
1364   return (offset & 1) ? z80dart_c_r(device, channel) : z80dart_d_r(device, channel);
1365}
1366
1367/*-------------------------------------------------
1368    z80dart_ba_cd_w - register write
1369-------------------------------------------------*/
1370
1371WRITE8_DEVICE_HANDLER( z80dart_ba_cd_w )
1372{
1373   int channel = BIT(offset, 1);
1374
1375   if (offset & 1)
1376      z80dart_c_w(device, channel, data);
1377   else
1378      z80dart_d_w(device, channel, data);
1379}
1380
1381/*-------------------------------------------------
1382    DEVICE_START( z80dart )
1383-------------------------------------------------*/
1384
1385static DEVICE_START( z80dart )
1386{
1387   z80dart_t *z80dart = get_safe_token(device);
1388   const z80dart_interface *intf = (const z80dart_interface *)device->baseconfig().static_config;
1389   int channel;
1390
1391   /* resolve callbacks */
1392   devcb_resolve_read_line(&z80dart->channel[Z80DART_CH_A].in_rxd_func, &intf->in_rxda_func, device);
1393   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_txd_func, &intf->out_txda_func, device);
1394   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_dtr_func, &intf->out_dtra_func, device);
1395   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_rts_func, &intf->out_rtsa_func, device);
1396   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_A].out_wrdy_func, &intf->out_wrdya_func, device);
1397   devcb_resolve_read_line(&z80dart->channel[Z80DART_CH_B].in_rxd_func, &intf->in_rxdb_func, device);
1398   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_txd_func, &intf->out_txdb_func, device);
1399   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_dtr_func, &intf->out_dtrb_func, device);
1400   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_rts_func, &intf->out_rtsb_func, device);
1401   devcb_resolve_write_line(&z80dart->channel[Z80DART_CH_B].out_wrdy_func, &intf->out_wrdyb_func, device);
1402   devcb_resolve_write_line(&z80dart->out_int_func, &intf->out_int_func, device);
1403
1404   if (intf->rx_clock_a)
1405   {
1406      /* allocate channel A receive timer */
1407      z80dart->rxca_timer = timer_alloc(device->machine, rxca_tick, (void *)device);
1408      timer_adjust_periodic(z80dart->rxca_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_clock_a));
1409   }
1410
1411   if (intf->tx_clock_a)
1412   {
1413      /* allocate channel A transmit timer */
1414      z80dart->txca_timer = timer_alloc(device->machine, txca_tick, (void *)device);
1415      timer_adjust_periodic(z80dart->txca_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->tx_clock_a));
1416   }
1417
1418   if (intf->rx_tx_clock_b)
1419   {
1420      /* allocate channel B receive/transmit timer */
1421      z80dart->rxtxcb_timer = timer_alloc(device->machine, rxtxcb_tick, (void *)device);
1422      timer_adjust_periodic(z80dart->rxtxcb_timer, attotime_zero, 0, ATTOTIME_IN_HZ(intf->rx_tx_clock_b));
1423   }
1424
1425   /* register for state saving */
1426   for (channel = Z80DART_CH_A; channel <= Z80DART_CH_B; channel++)
1427   {
1428      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rr);
1429      state_save_register_device_item_array(device, channel, z80dart->channel[channel].wr);
1430      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rx_data_fifo);
1431      state_save_register_device_item_array(device, channel, z80dart->channel[channel].rx_error_fifo);
1432      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_shift);
1433      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_error);
1434      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_fifo);
1435      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_clock);
1436      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_state);
1437      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_bits);
1438      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_first);
1439      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_parity);
1440      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_break);
1441      state_save_register_device_item(device, channel, z80dart->channel[channel].rx_rr0_latch);
1442      state_save_register_device_item(device, channel, z80dart->channel[channel].ri);
1443      state_save_register_device_item(device, channel, z80dart->channel[channel].cts);
1444      state_save_register_device_item(device, channel, z80dart->channel[channel].dcd);
1445      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_data);
1446      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_shift);
1447      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_clock);
1448      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_state);
1449      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_bits);
1450      state_save_register_device_item(device, channel, z80dart->channel[channel].tx_parity);
1451      state_save_register_device_item(device, channel, z80dart->channel[channel].dtr);
1452      state_save_register_device_item(device, channel, z80dart->channel[channel].rts);
1453   }
1454
1455   state_save_register_device_item_array(device, 0, z80dart->int_state);
1456}
1457
1458/*-------------------------------------------------
1459    DEVICE_RESET( z80dart )
1460-------------------------------------------------*/
1461
1462static DEVICE_RESET( z80dart )
1463{
1464   int channel;
1465
1466   LOG(("Z80DART \"%s\" Reset\n", device->tag.cstr()));
1467
1468   for (channel = Z80DART_CH_A; channel <= Z80DART_CH_B; channel++)
1469   {
1470      reset_channel(device, channel);
1471   }
1472
1473   check_interrupts(device);
1474}
1475
1476/*-------------------------------------------------
1477    DEVICE_GET_INFO( z80dart )
1478-------------------------------------------------*/
1479
1480DEVICE_GET_INFO( z80dart )
1481{
1482   switch (state)
1483   {
1484      /* --- the following bits of info are returned as 64-bit signed integers --- */
1485      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(z80dart_t);               break;
1486      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                           break;
1487      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;            break;
1488
1489      /* --- the following bits of info are returned as pointers to data or functions --- */
1490      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(z80dart);      break;
1491      case DEVINFO_FCT_STOP:                     /* Nothing */                           break;
1492      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(z80dart);      break;
1493      case DEVINFO_FCT_IRQ_STATE:                  info->f = (genf *)z80dart_irq_state;         break;
1494      case DEVINFO_FCT_IRQ_ACK:                  info->f = (genf *)z80dart_irq_ack;            break;
1495      case DEVINFO_FCT_IRQ_RETI:                  info->f = (genf *)z80dart_irq_reti;            break;
1496
1497      /* --- the following bits of info are returned as NULL-terminated strings --- */
1498      case DEVINFO_STR_NAME:                     strcpy(info->s, "Zilog Z80 DART");            break;
1499      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "Z80");                     break;
1500      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                     break;
1501      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                  break;
1502      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");      break;
1503   }
1504}
src/mess/machine/z80dart.h
r7536r7537
1/***************************************************************************
2
3    Z80 DART Dual Asynchronous Receiver/Transmitter implementation
4
5    Copyright (c) 2008, The MESS Team.
6    Visit http://mamedev.org for licensing and usage restrictions.
7
8****************************************************************************
9                            _____   _____
10                    D1   1 |*    \_/     | 40  D0
11                    D3   2 |             | 39  D2
12                    D5   3 |             | 38  D4
13                    D7   4 |             | 37  D6
14                  _INT   5 |             | 36  _IORQ
15                   IEI   6 |             | 35  _CE
16                   IEO   7 |             | 34  B/_A
17                   _M1   8 |             | 33  C/_D
18                   Vdd   9 |             | 32  _RD
19               _W/RDYA  10 |  Z80-DART   | 31  GND
20                  _RIA  11 |             | 30  _W/RDYB
21                  RxDA  12 |             | 29  _RIB
22                 _RxCA  13 |             | 28  RxDB
23                 _TxCA  14 |             | 27  _RxTxCB
24                  TxDA  15 |             | 26  TxDB
25                 _DTRA  16 |             | 25  _DTRB
26                 _RTSA  17 |             | 24  _RTSB
27                 _CTSA  18 |             | 23  _CTSB
28                 _DCDA  19 |             | 22  _DCDB
29                   CLK  20 |_____________| 21  _RESET
30
31***************************************************************************/
32
33#ifndef __Z80DART_H_
34#define __Z80DART_H_
35
36#include "devcb.h"
37
38/***************************************************************************
39    MACROS / CONSTANTS
40***************************************************************************/
41
42#define Z80DART   DEVICE_GET_INFO_NAME(z80dart)
43/*
44#define Z8470   DEVICE_GET_INFO_NAME(z8470)
45#define LH0081  DEVICE_GET_INFO_NAME(lh0088)
46#define MK3881  DEVICE_GET_INFO_NAME(mk3888)
47*/
48
49#define MDRV_Z80DART_ADD(_tag, _clock, _config) \
50   MDRV_DEVICE_ADD(_tag, Z80DART, _clock) \
51   MDRV_DEVICE_CONFIG(_config)
52
53#define MDRV_Z80DART_REMOVE(_tag) \
54   MDRV_DEVICE_REMOVE(_tag)
55
56#define Z80DART_INTERFACE(_name) \
57   const z80dart_interface (_name) =
58
59/***************************************************************************
60    TYPE DEFINITIONS
61***************************************************************************/
62
63enum
64{
65   Z80DART_CH_A = 0,
66   Z80DART_CH_B
67};
68
69typedef struct _z80dart_interface z80dart_interface;
70struct _z80dart_interface
71{
72   int rx_clock_a;         /* channel A receive clock */
73   int tx_clock_a;         /* channel A transmit clock */
74   int rx_tx_clock_b;      /* channel B receive/transmit clock */
75
76   devcb_read_line      in_rxda_func;
77   devcb_write_line   out_txda_func;
78   devcb_write_line   out_dtra_func;
79   devcb_write_line   out_rtsa_func;
80   devcb_write_line   out_wrdya_func;
81
82   devcb_read_line      in_rxdb_func;
83   devcb_write_line   out_txdb_func;
84   devcb_write_line   out_dtrb_func;
85   devcb_write_line   out_rtsb_func;
86   devcb_write_line   out_wrdyb_func;
87
88   devcb_write_line   out_int_func;
89};
90
91/***************************************************************************
92    PROTOTYPES
93***************************************************************************/
94
95/* register access */
96READ8_DEVICE_HANDLER( z80dart_cd_ba_r );
97WRITE8_DEVICE_HANDLER( z80dart_cd_ba_w );
98
99READ8_DEVICE_HANDLER( z80dart_ba_cd_r );
100WRITE8_DEVICE_HANDLER( z80dart_ba_cd_w );
101
102/* control register access */
103WRITE8_DEVICE_HANDLER( z80dart_c_w );
104READ8_DEVICE_HANDLER( z80dart_c_r );
105
106/* data register access */
107WRITE8_DEVICE_HANDLER( z80dart_d_w );
108READ8_DEVICE_HANDLER( z80dart_d_r );
109
110/* serial clocks */
111WRITE_LINE_DEVICE_HANDLER( z80dart_rxca_w );
112WRITE_LINE_DEVICE_HANDLER( z80dart_txca_w );
113WRITE_LINE_DEVICE_HANDLER( z80dart_rxtxcb_w );
114
115/* ring indicator */
116WRITE_LINE_DEVICE_HANDLER( z80dart_ria_w );
117WRITE_LINE_DEVICE_HANDLER( z80dart_rib_w );
118
119/* data carrier detected */
120WRITE_LINE_DEVICE_HANDLER( z80dart_dcda_w );
121WRITE_LINE_DEVICE_HANDLER( z80dart_dcdb_w );
122
123/* clear to send */
124WRITE_LINE_DEVICE_HANDLER( z80dart_ctsa_w );
125WRITE_LINE_DEVICE_HANDLER( z80dart_ctsb_w );
126
127/* receive data byte HACK */
128void z80dart_receive_data(running_device *device, int channel, UINT8 data);
129
130DEVICE_GET_INFO( z80dart );
131
132#endif
src/mess/mess.mak
r7536r7537
490490   $(MESS_MACHINE)/upd7002.o   \
491491   $(MESS_MACHINE)/upd7201.o   \
492492   $(MESS_MACHINE)/wd17xx.o   \
493   $(MESS_MACHINE)/z80dart.o   \
494   $(MESS_MACHINE)/z80sti.o   \
495493   $(MESS_VIDEO)/cdp1861.o      \
496494   $(MESS_VIDEO)/cdp1862.o      \
497495   $(MESS_VIDEO)/crt9007.o      \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team