Previous 15994 Revisions Next

r15653 Monday 23rd July, 2012 at 19:08:16 UTC by Michael Zapf
Moved TMS9901/9902 from mess/machine to emu/machine [Michael Zapf]
[src/emu]emu.mak
[src/emu/machine]tms9901.c* tms9901.h* tms9902.c* tms9902.h*
[src/mess]mess.mak
[src/mess/machine]tms9901.c tms9901.h tms9902.c tms9902.h

src/emu/emu.mak
r15652r15653
250250   $(EMUMACHINE)/timekpr.o      \
251251   $(EMUMACHINE)/tmp68301.o   \
252252   $(EMUMACHINE)/tms6100.o      \
253   $(EMUMACHINE)/tms9901.o      \
254   $(EMUMACHINE)/tms9902.o      \
253255   $(EMUMACHINE)/upd1990a.o   \
254256   $(EMUMACHINE)/upd4701.o      \
255257   $(EMUMACHINE)/upd7201.o      \
src/emu/machine/tms9901.c
r0r15653
1/****************************************************************************
2
3    TMS9901 Programmable System Interface
4
5Overview:
6    TMS9901 is a support chip for TMS9900.  It handles interrupts, provides
7    several I/O pins, and a timer (a.k.a. clock: it is merely a register which
8    decrements regularly and can generate an interrupt when it reaches 0).
9
10    It communicates with the TMS9900 with the CRU bus, and with the rest of the
11    world with a number of parallel I/O pins.
12
13    I/O and timer functions should work with any other 990/99xx/99xxx CPU.
14    On the other hand, interrupt handling was primarily designed for tms9900
15    and 99000 based systems: other CPUs can support interrupts, but not the 16
16    distinct interrupt vectors.
17
18Pins:
19    Vcc, Vss: power supply
20    Phi*: system clock (connected to TMS9900 Phi3* or TMS9980 CLKOUT*)
21    RST1*: reset input
22    CRUIN, CRUOUT, CRUCLK, CE*, S0-S4: CRU bus (CPU interface)
23    INTREQ*, IC0-IC3: interrupt bus (CPU interface)
24    INT*1-INT*6: used as interrupt/input pins.
25    P0-P6: used as input/output pins.
26    INT*7/P15-INT*15/P7: used as either interrupt/input or input/output pins.
27      Note that a pin cannot be used simultaneously as output and as interrupt.
28      (This is mostly obvious, but it implies that you cannot trigger an
29      interrupt by setting the output state of a pin, which is not SO obvious.)
30
31Interrupt handling:
32    After each clock cycle, TMS9901 latches the state of INT1*-INT15* (except
33    pins which are set as output pins).  If the clock is enabled, it replaces
34    INT3* with an internal timer interrupt flag.  Then it inverts the value and
35    performs a bit-wise AND with the interrupt mask.
36
37    If there are some unmasked interrupt bits, INTREQ* is asserted and the code
38    of the lowest active interrupt is placed on IC0-IC3.  If these pins are
39    duly connected to the tms9900 INTREQ* and IC0-IC3 pins, the result is that
40    asserting an INTn* on tms9901 will cause a level-n interrupt request on the
41    tms9900, provided that this interrupt pin is not masked in tms9901, and
42    that no unmasked higher-priority (i.e. lower-level) interrupt pin is set.
43
44    This interrupt request lasts for as long as the interrupt pin and the
45    relevant bit in the interrupt mask are set (level-triggered interrupts).
46    (The request may be shadowed by a higher-priority interrupt request, but
47    it will resume when the higher-priority request ends.)
48
49    TIMER interrupts are kind of an exception, since they are not associated
50    with an external interrupt pin.  I think there is an internal timer
51    interrupt flag that is set when the decrementer reaches 0, and is cleared
52    by a write to the 9901 int*3 enable bit ("SBO 3" in interrupt mode).
53
54TODO:
55    * Emulate the RST1* input.  Note that RST1* active (low) makes INTREQ*
56      inactive (high) with IC0-IC3 = 0.
57    * the clock read register is updated every time the timer decrements when
58      the TMS9901 is not in clock mode.  This probably implies that if the
59      clock mode is cleared and re-asserted immediately, the tms9901 may fail
60      to update the clock read register: this is not emulated.
61    * The clock mode is entered when a 1 is written to the control bit.  It is
62      exited when a 0 is written to the control bit or the a tms9901 select bit
63      greater than 15 is accessed.  According to the data sheet, "when CE* is
64      inactive (HIGH), the PSI is not disabled from seeing the select lines.
65      As the CPU is accessing memory, A10-A14 could very easily have a value of
66      15 or greater" (this is assuming that S0-S4 are connected to A10-A14,
67      which makes sense with most tms9900 family members).  There is no way
68      this "feature" (I would call it a hardware bug) can be emulated
69      efficiently, as we would need to watch every memory access.
70
71MZ: According to the description in
72       A. Osborne, G. Kane: Osborne 16-bit microprocessor handbook
73       page 3-81
74    the 9901 only temporarily leaves the timer mode as long as S0 is set to 1.
75    In the meantime the timer function continues but cannot be queried. This
76    makes it possible to continue using the chip as a timer while working with
77    its I/O pins. Thus I believe the above TODO concering the exit of the timer
78    mode is not applicable.
79    The problem is that the original 9901 specification is not clear about this.
80
81MZ: Turned to class (January 2012)
82
83TODO: Tests on a real machine
84- Set an interrupt input (e.g. keyboard for Geneve), trigger RST2*, check whether
85  interrupt mask has been reset
86- Check whether the clock_read_register is updated whenever clock mode is exited
87  (in particular when S0=1, i.e. A10=1 -> addresses xxxx xxxx xx1x xxxx
88  requires to write a program that fits into 32 bytes; workspace elsewhere)
89
90    Raphael Nabet, 2000-2004
91    Michael Zapf
92
93    February 2012: Rewritten as class
94
95*****************************************************************************/
96
97#include <math.h>
98#include "emu.h"
99
100#include "tms9901.h"
101
102#define VERBOSE 1
103#define LOG logerror
104
105/*
106    Constructor
107*/
108tms9901_device::tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
109: device_t(mconfig, TMS9901, "TMS9901 Programmable System Interface", tag, owner, clock)
110{
111}
112
113/*
114    should be called after any change to int_state or enabled_ints.
115*/
116void tms9901_device::field_interrupts(void)
117{
118   int current_ints;
119
120   /* int_state: state of lines int1-int15 */
121   current_ints = m_int_state;
122   if (m_clock_register != 0)
123   {   /* if timer is enabled, INT3 pin is overriden by timer */
124      if (m_timer_int_pending)
125      {
126         if (VERBOSE>8) LOG("tms9901: timer fires\n");
127         current_ints |= TMS9901_INT3;
128      }
129      else
130      {
131         if (VERBOSE>8) LOG("tms9901: timer clear\n");
132         current_ints &= ~TMS9901_INT3;
133      }
134   }
135
136   /* enabled_ints: enabled interrupts */
137   /* mask out all int pins currently set as output */
138   current_ints &= m_enabled_ints & (~m_pio_direction_mirror);
139
140   // Check whether we have a new state. For systems that use level-triggered
141   // interrupts it should not do any harm if the line is re-asserted
142   // but we may as well avoid this.
143   if (current_ints == m_old_int_state)
144      return;
145
146   m_old_int_state = current_ints;
147
148   if (current_ints)
149   {
150      // find which interrupt tripped us:
151      // the number of the first (i.e. least significant) non-zero bit among
152      // the 16 first bits
153      // we simply look for the first bit set to 1 in current_ints... */
154      int level = 0;
155
156      while ((current_ints & 1)==0)
157      {
158         current_ints >>= 1;   /* try next bit */
159         level++;
160      }
161      m_int_pending = true;
162      if (!m_interrupt.isnull())
163         m_interrupt(level, 1);  // the offset carries the IC0-3 level
164   }
165   else
166   {
167      m_int_pending = false;
168      if (!m_interrupt.isnull())
169         m_interrupt(0xf, 0);  //Spec: INTREQ*=1 <=> IC0,1,2,3 = 1111
170   }
171}
172
173/*
174    function which should be called by the driver when the state of an INTn*
175    pin changes (only required if the pin is set up as an interrupt pin)
176
177    state == 0: INTn* is inactive (high)
178    state != 0: INTn* is active (low)
179
180    0<=pin_number<=15
181*/
182void tms9901_device::set_single_int(int pin_number, int state)
183{
184   /* remember new state of INTn* pin state */
185   if (state==ASSERT_LINE)
186      m_int_state |= 1 << pin_number;
187   else
188      m_int_state &= ~(1 << pin_number);
189
190   /* we do not need to always call this function - time for an optimization */
191   field_interrupts();
192}
193
194/*
195    load the content of m_clock_register into the decrementer
196*/
197void tms9901_device::timer_reload(void)
198{
199   if (m_clock_register != 0)
200   {   /* reset clock interval */
201      m_decrementer_value = m_clock_register;
202      m_decrementer->enable(true);
203   }
204   else
205   {   /* clock interval == 0 -> no timer */
206      m_decrementer->enable(false);
207   }
208}
209
210/*----------------------------------------------------------------
211    TMS9901 CRU interface.
212----------------------------------------------------------------*/
213
214/*
215    Read a 8 bit chunk from tms9901.
216
217    signification:
218    bit 0: m_clock_mode
219    if (m_clock_mode == false)
220     bit 1-15: current status of the INT1*-INT15* pins
221    else
222     bit 1-14: current timer value
223     bit 15: value of the INTREQ* (interrupt request to TMS9900) pin.
224
225    bit 16-31: current status of the P0-P15 pins (quits timer mode, too...)
226*/
227READ8_MEMBER( tms9901_device::read )
228{
229   int answer = 0;
230
231   offset &= 0x003;
232
233   switch (offset)
234   {
235   case 0:
236      if (m_clock_mode)
237      {   /* clock mode */
238         answer = ((m_clock_read_register & 0x7F) << 1) | 0x01;
239      }
240      else
241      {   /* interrupt mode */
242         // m_int_state stores the INTx values, which are inverted to the pin levels (INTx*)
243         answer = ((~m_int_state) & m_supported_int_mask) & 0xFF;
244
245         if (!m_read_block.isnull())
246            answer |= m_read_block(TMS9901_CB_INT7);
247
248         answer &= ~ m_pio_direction_mirror;
249         answer |= (m_pio_output_mirror & m_pio_direction_mirror) & 0xFF;
250      }
251      break;
252   case 1:
253      if (m_clock_mode)
254      {   /* clock mode */
255         answer = (m_clock_read_register & 0x3F80) >> 7;
256         if (!m_int_pending)
257            answer |= 0x80;
258      }
259      else
260      {   /* interrupt mode */
261         answer = ((~m_int_state) & m_supported_int_mask) >> 8;
262
263         if (!m_read_block.isnull())
264            answer |= m_read_block(TMS9901_INT8_INT15);
265
266         answer &= ~ (m_pio_direction_mirror >> 8);
267         answer |= (m_pio_output_mirror & m_pio_direction_mirror) >> 8;
268      }
269      break;
270   case 2:
271      /* exit timer mode */
272      // MZ: See comments at the beginning. I'm pretty sure this is not correct.
273      // m_clock_mode = false;
274
275      if (!m_read_block.isnull())
276         answer = m_read_block(TMS9901_P0_P7);
277      else
278         answer = 0;
279
280      answer &= ~ m_pio_direction;
281      answer |= (m_pio_output & m_pio_direction) & 0xFF;
282
283      break;
284   case 3:
285      // MZ: see above
286      // m_clock_mode = false;        // exit timer mode
287      if (!m_read_block.isnull())
288         answer = m_read_block(TMS9901_P8_P15);
289      else
290         answer = 0;
291
292      answer &= ~ (m_pio_direction >> 8);
293      answer |= (m_pio_output & m_pio_direction) >> 8;
294
295      break;
296   }
297
298   return answer;
299}
300
301/*
302    Write 1 bit to tms9901.
303
304    signification:
305    bit 0: write m_clock_mode
306    if (!m_clock_mode)
307     bit 1-15: write interrupt mask register
308    else
309     bit 1-14: write timer period
310     bit 15: if written value == 0, soft reset (just resets all I/O pins as input)
311
312    bit 16-31: set output state of P0-P15 (and set them as output pin) (quit timer mode, too...)
313*/
314WRITE8_MEMBER ( tms9901_device::write )
315{
316   data &= 1;   /* clear extra bits */
317   offset &= 0x01F;
318   switch (offset)
319   {
320   case 0x00:   /* write to mode bit */
321      if (data == 0)
322      {
323         /* we are quitting clock mode */
324         m_clock_mode = false;
325         if (VERBOSE>5) LOG("tms9901: int mode\n");
326      }
327      else
328      {
329         m_clock_mode = true;
330         if (VERBOSE>5) LOG("tms9901: clock mode\n");
331         // we are switching to clock mode: latch the current value of
332         // the decrementer register
333         if (m_clock_register != 0)
334            m_clock_read_register = m_decrementer_value;
335         else
336            m_clock_read_register = 0;      /* timer inactive... */
337      }
338      break;
339   case 0x01:
340   case 0x02:
341   case 0x03:
342   case 0x04:
343   case 0x05:
344   case 0x06:
345   case 0x07:
346   case 0x08:
347   case 0x09:
348   case 0x0A:
349   case 0x0B:
350   case 0x0C:
351   case 0x0D:
352   case 0x0E:
353      // write one bit to 9901 (bits 1-14)
354      //
355      // m_clock_mode==false ?  Disable/Enable an interrupt
356      // :  Bit in clock interval
357      //
358      // offset is the index of the modified bit of register (-> interrupt number -1)
359      if (m_clock_mode)
360      {   /* modify clock interval */
361         int mask = 1 << ((offset & 0x0F) - 1);   /* corresponding mask */
362
363         if (data)
364            m_clock_register |= mask;      /* set bit */
365         else
366            m_clock_register &= ~mask;      /* clear bit */
367
368         /* reset clock timer (page 8) */
369         if (VERBOSE>6) LOG("tms9901: clock register = %04x\n", m_clock_register);
370         timer_reload();
371      }
372      else
373      {   /* modify interrupt enable mask */
374         int mask = 1 << (offset & 0x0F);   /* corresponding mask */
375
376         if (data)
377            m_enabled_ints |= mask;      /* set bit */
378         else
379            m_enabled_ints &= ~mask;      /* unset bit */
380
381         if (offset == 3)
382            m_timer_int_pending = false;   /* SBO 3 clears pending timer interrupt (??) */
383
384         if (VERBOSE>6) LOG("tms9901: interrupts = %04x\n", m_enabled_ints);
385         field_interrupts();      /* changed interrupt state */
386      }
387      break;
388   case 0x0F:
389      if (m_clock_mode)
390      {   /* in clock mode this is the soft reset bit */
391         if (!data)
392         {   // TMS9901 soft reset (RST2*)
393            // Spec: "Writing a 0 to bit 15 while in the clock mode executes a soft reset on the I/O pins.
394            // [...] RST2* will program all ports to the input mode"
395            m_pio_direction = 0;
396            m_pio_direction_mirror = 0;
397
398            // "RST1* (power-up reset) will reset all mask bits low."
399            // Spec is not clear on whether the mask bits are also reset by RST2*
400            // TODO: Check on a real machine. (I'd guess from the text they are not touched)
401            m_enabled_ints = 0;
402            if (VERBOSE>5) LOG("tms9901: Soft reset (RST2*)\n");
403         }
404      }
405      else
406      {   /* modify interrupt enable mask */
407         if (data)
408            m_enabled_ints |= 0x4000;      /* set bit */
409         else
410            m_enabled_ints &= ~0x4000;      /* unset bit */
411
412         if (VERBOSE>6) LOG("tms9901: interrupts = %04x\n", m_enabled_ints);
413         field_interrupts();      /* changed interrupt state */
414      }
415      break;
416   case 0x10:
417   case 0x11:
418   case 0x12:
419   case 0x13:
420   case 0x14:
421   case 0x15:
422   case 0x16:
423   case 0x17:
424   case 0x18:
425   case 0x19:
426   case 0x1A:
427   case 0x1B:
428   case 0x1C:
429   case 0x1D:
430   case 0x1E:
431   case 0x1F:
432      int pin = offset & 0x0F;
433      if (VERBOSE>6) LOG("tms9901: output on P%d = %d\n", pin, data);
434      int mask = (1 << pin);
435
436      // MZ: see above - I think this is wrong
437      // m_clock_mode = false; // exit timer mode
438
439      m_pio_direction |= mask;         /* set up as output pin */
440
441      if (data)
442         m_pio_output |= mask;
443      else
444         m_pio_output &= ~mask;
445
446      if (pin >= 7)
447      {   /* pins P7-P15 are mirrored as INT15*-INT7* */
448         int pin2 = 22 - pin;
449         int mask2 = (1 << pin2);
450
451         m_pio_direction_mirror |= mask2;   /* set up as output pin */
452
453         if (data)
454            m_pio_output_mirror |= mask2;
455         else
456            m_pio_output_mirror &= ~ mask2;
457      }
458
459      if (!m_write_line[pin].isnull())
460         (m_write_line[pin])(data);
461
462      break;
463   }
464}
465
466/*
467    Timer callback
468    Decrementer counts down the value set in clock mode; when it reaches 0,
469    raises an interrupt and resets to the start value
470    The decrementer works as long as the clock_register contains a non-zero value.
471*/
472void tms9901_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
473{
474   if (id==DECREMENTER) // we have only that one
475   {
476      m_decrementer_value--;
477      if (VERBOSE>6) LOG("tms9901: decrementer = %d\n", m_decrementer_value);
478      if (m_decrementer_value<=0)
479      {
480         m_timer_int_pending = true;         // decrementer interrupt requested
481         field_interrupts();
482         m_decrementer_value = m_clock_register;
483      }
484   }
485}
486
487/*-------------------------------------------------
488    DEVICE_STOP( tms9901 )
489-------------------------------------------------*/
490
491void tms9901_device::device_stop(void)
492{
493}
494
495/*-------------------------------------------------
496    DEVICE_RESET( tms9901 )
497-------------------------------------------------*/
498
499void tms9901_device::device_reset(void)
500{
501   m_timer_int_pending = false;
502   m_enabled_ints = 0;
503
504   m_pio_direction = 0;
505   m_pio_direction_mirror = 0;
506   m_pio_output = m_pio_output_mirror = 0;
507
508   m_int_state = 0;
509   m_old_int_state = -1;
510   field_interrupts();
511
512   m_clock_mode = false;
513
514   m_clock_register = 0;
515   timer_reload();
516}
517
518
519/*-------------------------------------------------
520    DEVICE_START( tms9901 )
521-------------------------------------------------*/
522
523void tms9901_device::device_start(void)
524{
525   const tms9901_interface *intf = reinterpret_cast<const tms9901_interface *>(static_config());
526   m_supported_int_mask = intf->interrupt_mask;
527
528   m_decrementer = timer_alloc(DECREMENTER);
529   m_decrementer->adjust(attotime::from_hz(clock() / 64.), 0, attotime::from_hz(clock() / 64.));
530   m_decrementer->enable(false);
531
532   m_read_block.resolve(intf->read_handler, *this);
533
534   for (int i=0; i < 16; i++)
535   {
536      m_write_line[i].resolve(intf->write_handler[i], *this);
537   }
538
539   m_interrupt.resolve(intf->interrupt_callback, *this);
540}
541
542const device_type TMS9901 = &device_creator<tms9901_device>;
src/emu/machine/tms9901.h
r0r15653
1/****************************************************************************
2
3    TMS9901 Programmable System Interface
4    See tms9901.c for documentation
5
6    Raphael Nabet
7    Michael Zapf
8
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
13#ifndef __TMS9901_H__
14#define __TMS9901_H__
15
16#include "emu.h"
17
18extern const device_type TMS9901;
19
20/***************************************************************************
21    MACROS
22***************************************************************************/
23
24/* Masks for the interrupts levels available on TMS9901 */
25#define TMS9901_INT1 0x0002
26#define TMS9901_INT2 0x0004
27#define TMS9901_INT3 0x0008      // overriden by the timer interrupt
28#define TMS9901_INT4 0x0010
29#define TMS9901_INT5 0x0020
30#define TMS9901_INT6 0x0040
31#define TMS9901_INT7 0x0080
32#define TMS9901_INT8 0x0100
33#define TMS9901_INT9 0x0200
34#define TMS9901_INTA 0x0400
35#define TMS9901_INTB 0x0800
36#define TMS9901_INTC 0x1000
37#define TMS9901_INTD 0x2000
38#define TMS9901_INTE 0x4000
39#define TMS9901_INTF 0x8000
40
41enum
42{
43   TMS9901_CB_INT7 = 0,
44   TMS9901_INT8_INT15 = 1,
45   TMS9901_P0_P7 = 2,
46   TMS9901_P8_P15 = 3
47};
48
49/***************************************************************************
50    CLASS DEFINITION
51***************************************************************************/
52
53typedef struct _tms9901_interface
54{
55   int             interrupt_mask;         // a bit for each input pin whose state is always notified to the TMS9901 core
56   devcb_read8       read_handler;         // 4*8 bits, to be selected using the offset (0-3)
57   devcb_write_line   write_handler[16];      // 16 Pn outputs
58   devcb_write8      interrupt_callback;    // called when interrupt bus state changes
59} tms9901_interface;
60
61class tms9901_device : public device_t
62{
63public:
64   tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
65
66   void set_single_int(int pin_number, int state);
67
68   DECLARE_READ8_MEMBER( read );
69   DECLARE_WRITE8_MEMBER( write );
70
71private:
72   static const device_timer_id DECREMENTER = 0;
73
74   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void timer_reload(void);
76   void field_interrupts(void);
77
78   void device_start(void);
79   void device_stop(void);
80   void device_reset(void);
81
82   /* interrupt registers */
83   // mask:  bit #n is set if pin #n is supported as an interrupt pin,
84   // i.e. the driver sends a notification whenever the pin state changes
85   // setting these bits is not required, but it saves you the trouble of
86   // saving the state of interrupt pins and feeding it to the port read
87   // handlers again
88   int m_supported_int_mask;
89   int m_int_state;         // state of the int1-int15 lines (must be inverted when queried)
90   int m_old_int_state;      // stores the previous value to avoid useless INT line assertions
91   int m_enabled_ints;         // interrupt enable mask
92
93   bool m_int_pending;         // status of the int* pin (connected to TMS9900)
94   bool m_timer_int_pending;   // timer int pending (overrides int3 pin if timer enabled)
95
96   // PIO registers
97   int m_pio_direction;      // direction register for PIO
98
99   // current PIO output (to be masked with pio_direction)
100   int m_pio_output;
101
102   // mirrors used for INT7*-INT15*
103   int m_pio_direction_mirror;
104   int m_pio_output_mirror;
105
106   // =======================================================================
107
108   // TMS9901 clock mode
109   // false = so-called interrupt mode (read interrupt state, write interrupt enable mask)
110   // true = clock mode (read/write clock interval)
111   bool m_clock_mode;
112
113   // MESS timer, used to emulate the decrementer register
114   emu_timer *m_decrementer;
115
116   // clock interval, loaded in decrementer when it reaches 0.
117   // 0 means decrementer off
118   int m_clock_register;
119
120   // Current decrementer value
121   int m_decrementer_value;
122
123   // when we go into timer mode, the decrementer is copied there to allow to read it reliably
124   int m_clock_read_register;
125
126   // =======================================================================
127
128   // Callbacks
129   devcb_resolved_read8      m_read_block;
130   devcb_resolved_write_line   m_write_line[16];
131   devcb_resolved_write8      m_interrupt;  // also delivers the interrupt level
132};
133
134/***************************************************************************
135    DEVICE CONFIGURATION MACROS
136***************************************************************************/
137
138#define MCFG_TMS9901_ADD(_tag, _intrf, _rate) \
139   MCFG_DEVICE_ADD(_tag, TMS9901, _rate) \
140   MCFG_DEVICE_CONFIG(_intrf)
141
142#endif /* __TMS9901_H__ */
src/emu/machine/tms9902.c
r0r15653
1/****************************************************************************
2
3    TMS9902 Asynchronous Communication Controller
4
5    TMS9902 is an asynchronous serial controller for use with the TI990 and
6    TMS9900 family.  It provides serial I/O, three extra I/O pins (namely RTS,
7    DSR and CTS), and a timer.  It communicates with the CPU through the CRU
8    I/O bus, and one interrupt pin.
9
10               +----+--+----+
11     <-   /INT |1   \--/  18| VCC
12     <-   XOUT |2         17| /CE     <-
13     ->    RIN |3         16| /PHI    <-
14     <-  CRUIN |4         15| CRUCLK  <-
15     <-   /RTS |5         14| S0      <-
16     ->   /CTS |6         13| S1      <-
17     ->   /DSR |7         12| S2      <-
18     -> CRUOUT |8         11| S3      <-
19           VSS |9         10| S4      <-
20               +------------+
21
22     The CRUIN line borrows its name from the connector of the connected CPU
23     where it is an input, so CRUIN is an output of this chip. The same is true
24     for CRUOUT.
25
26     /PHI is a TTL clock input with 4 MHz maximum rate.
27
28    IMPORTANT NOTE: The previous versions of TMS9902 attempted to write their
29    output to a file. This implementation is able to communicate with an external
30    UART via a socket connection and an external bridge. However, the work is
31    not done yet, and until then the file writing is disabled.
32
33    Raphael Nabet, 2003
34    Michael Zapf, 2011
35    February 2012: Rewritten as class
36
37*****************************************************************************/
38
39#include <math.h>
40#include "tms9902.h"
41
42#define VERBOSE 1
43#define LOG logerror
44
45enum
46{
47   DECTIMER,
48   RECVTIMER,
49   SENDTIMER
50};
51
52// Polling frequency. We use a much higher value to allow for line state changes
53// happening between character transmissions (which happen in parallel in real
54// communications but which must be serialized here)
55#define POLLING_FREQ 20000
56
57
58/*
59    Constructor
60*/
61tms9902_device::tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
62: device_t(mconfig, TMS9902, "TMS9902 Asynchronous Communication Controller", tag, owner, clock)
63{
64}
65
66/*
67    should be called after any change to int_state or enabled_ints.
68*/
69void tms9902_device::field_interrupts()
70{
71   bool new_int = (m_DSCH && m_DSCENB)
72                     || (m_RBRL && m_RIENB)
73                     || (m_XBRE && m_XBIENB)
74                     || (m_TIMELP && m_TIMENB);
75// if (VERBOSE>3) LOG("TMS9902: interrupt flags (DSCH = %02x, DSCENB = %02x), (RBRL = %02x, RIENB = %02x), (XBRE = %02x, XBIENB = %02x), (TIMELP = %02x, TIMENB = %02x)\n",
76//  m_DSCH, m_DSCENB, m_RBRL, m_RIENB, m_XBRE, m_XBIENB, m_TIMELP, m_TIMENB);
77   if (new_int != m_INT)
78   {
79      // Only consider edges
80      m_INT = new_int;
81      if (VERBOSE>3) LOG("TMS9902: /INT = %s\n", (m_INT)? "asserted" : "cleared");
82      int_callback(m_INT? ASSERT_LINE : CLEAR_LINE);
83   }
84}
85
86/*
87    Called whenever the incoming CTS* line changes. This should be called by
88    the device that contains the UART.
89*/
90void tms9902_device::rcv_cts(line_state state)
91{
92   bool previous = m_CTSin;
93
94   // CTSin is an internal register of the TMS9902 with positive logic
95   m_CTSin = (state==ASSERT_LINE);
96
97   if (VERBOSE>3) LOG("TMS9902: CTS* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
98
99   if (m_CTSin != previous)
100   {
101      m_DSCH = true;
102      field_interrupts();
103
104      // If CTS becomes asserted and we have been sending
105      if (state==ASSERT_LINE && m_RTSout)
106      {
107         // and if the byte buffer is empty
108         if (m_XBRE)
109         {
110            // and we want to have a BRK, send it
111            if (m_BRKON) send_break(true);
112         }
113         else
114         {
115            // Buffer is not empty, we can send it
116            // If the shift register is empty, transfer the data
117            if (m_XSRE && !m_BRKout)
118            {
119               initiate_transmit();
120            }
121         }
122      }
123   }
124   else
125   {
126      m_DSCH = false;
127      if (VERBOSE>4) LOG("TMS9902: no change in CTS line, no interrupt.");
128   }
129}
130
131void tms9902_device::set_clock(bool state)
132{
133   if (state)
134      m_recvtimer->adjust(attotime::from_msec(1), 0, attotime::from_hz(POLLING_FREQ));
135   else
136      m_recvtimer->reset();
137}
138
139/*
140    Called whenever the incoming DSR* line changes. This should be called by
141    the device that contains the UART.
142*/
143void tms9902_device::rcv_dsr(line_state state)
144{
145   bool previous = m_DSRin;
146   if (VERBOSE>3) LOG("TMS9902: DSR* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
147   m_DSRin = (state==ASSERT_LINE);
148
149   if (m_DSRin != previous)
150   {
151      m_DSCH = true;
152      field_interrupts();
153   }
154   else
155   {
156      m_DSCH = false;
157      if (VERBOSE>4) LOG("TMS9902: no change in DSR line, no interrupt.");
158   }
159}
160
161/*
162    Called whenever the incoming RIN line changes. This should be called by
163    the device that contains the UART. Unlike the real thing, we deliver
164    complete bytes in one go.
165*/
166void tms9902_device::rcv_data(UINT8 data)
167{
168   // Put the received byte into the 1-byte receive buffer
169   m_RBR = data;
170
171   // Clear last errors
172   m_RFER = false;
173   m_RPER = false;
174
175   if (!m_RBRL)
176   {
177      // Receive buffer was empty
178      m_RBRL = true;
179      m_ROVER = false;
180      if (VERBOSE>3) LOG("TMS9902: Receive buffer loaded with byte %02x\n", data);
181      field_interrupts();
182   }
183   else
184   {
185      // Receive buffer was full
186      m_ROVER = true;
187      if (VERBOSE>1) LOG("TMS9902: Receive buffer still loaded; overflow error\n");
188   }
189}
190
191//------------------------------------------------
192
193/*
194    Framing error. This can only be detected by a remotely attached real UART;
195    if we get a report on a framing error we use it to announce the framing error
196    as if it occurred here.
197    The flag is reset by the next correctly received byte.
198*/
199void tms9902_device::rcv_framing_error()
200{
201   if (VERBOSE>2) LOG("TMS9902: Detected framing error\n");
202   m_RFER = true;
203}
204
205/*
206    Parity error. This can only be detected by a remotely attached real UART;
207    if we get a report on a parity error we use it to announce the parity error
208    as if it occurred here.
209    The flag is reset by the next correctly received byte.
210*/
211void tms9902_device::rcv_parity_error()
212{
213   if (VERBOSE>2) LOG("TMS9902: Detected parity error\n");
214   m_RPER = true;
215}
216
217/*
218    Incoming BREAK condition. The TMS9902 does not show any directly visible
219    reactions on a BREAK (no interrupt, no flag set). A BREAK is a time period
220    of low level on the RIN pin which makes the chip re-synchronize on the
221    next rising edge.
222*/
223void tms9902_device::rcv_break(bool value)
224{
225   if (VERBOSE>2) LOG("TMS9902: Receive BREAK=%d (no effect)\n", value? 1:0);
226}
227
228//------------------------------------------------
229
230/*
231    Timer callback
232*/
233void tms9902_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
234{
235   switch (id)
236   {
237   // This call-back is called by the MESS timer system when the decrementer
238   // reaches 0.
239   case DECTIMER:
240      m_TIMERR = m_TIMELP;
241      break;
242
243   //  Callback for the autonomous operations of the chip. This is normally
244   //  controlled by an external clock of 3-4 MHz, internally divided by 3 or 4,
245   //  depending on CLK4M. With this timer, reception of characters becomes
246   //  possible.
247   case RECVTIMER:
248      rcv_callback(ASSERT_LINE);
249      break;
250
251   case SENDTIMER:
252      // Byte has been sent
253      m_XSRE = true;
254
255      // In the meantime, the CPU may have pushed a new byte into the XBR
256      // so we loop until all data are transferred
257      if (!m_XBRE && m_CTSin)
258      {
259         initiate_transmit();
260      }
261      break;
262   }
263}
264
265/*
266    load the content of clockinvl into the decrementer
267*/
268void tms9902_device::reload_interval_timer()
269{
270   if (m_TMR)
271   {   /* reset clock interval */
272      m_dectimer->adjust(
273                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)),
274                  0,
275                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)));
276   }
277   else
278   {   /* clock interval == 0 -> no timer */
279      m_dectimer->enable(0);
280   }
281}
282
283void tms9902_device::send_break(bool state)
284{
285   if (state != m_BRKout)
286   {
287      m_BRKout = state;
288      if (VERBOSE>2) LOG("TMS9902: Sending BREAK=%d\n", state? 1:0);
289
290      // Signal BRK (on/off) to the remote site
291      ctrl_callback((EXCEPT | BRK), state? 1:0);
292   }
293}
294
295/*
296    Baudpoll value allows the callback function to know when the next data byte shall be delivered.
297*/
298double tms9902_device::get_baudpoll()
299{
300   return m_baudpoll;
301}
302
303// ==========================================================================
304
305/*
306    Sets the data rate for the receiver part. If a remote UART is attached,
307    propagate this setting.
308    The TMS9902 calculates the baud rate from the external clock, and the result
309    does not match the known baud rates precisely (e.g. for 9600 baud the
310    closest value is 9615). Other UARTs may have a different way to set baud
311    rates. Thus we transmit the bit pattern and leave it up to the remote UART
312    to calculate its own baud rate from it. Apart from that, the callback
313    function should add information about the UART.
314
315    CLK4M RDV8 RDR9 RDR8 | RDR7 RDR6 RDR5 RDR4 | RDR3 RDR2 RDR1 RDR0
316*/
317void tms9902_device::set_receive_data_rate()
318{
319   int value = (m_CLK4M? 0x800 : 0) | (m_RDV8? 0x400 : 0) | m_RDR;
320   if (VERBOSE>3) LOG("TMS9902: receive rate = %04x\n", value);
321
322   // Calculate the ratio between receive baud rate and polling frequency
323   double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
324   double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
325
326   // We assume 10 bit per character (7 data usually add 1 parity; 1 start, 1 stop)
327   // This value represents the ratio of data inputs of one poll.
328   // Thus the callback function should add up this value on each poll
329   // and deliver a data input not before it sums up to 1.
330   m_baudpoll = (double)(baud / (10*POLLING_FREQ));
331   if (VERBOSE>3) LOG ("TMS9902: baudpoll = %lf\n", m_baudpoll);
332
333   m_last_config_value = value;
334   ctrl_callback(CONFIG, RATERECV);
335}
336
337/*
338    Sets the data rate for the sender part. If a remote UART is attached,
339    propagate this setting.
340*/
341void tms9902_device::set_transmit_data_rate()
342{
343   int value = (m_CLK4M? 0x800 : 0) | (m_XDV8? 0x400 : 0) | m_XDR;
344   if (VERBOSE>3) LOG("TMS9902: set transmit rate = %04x\n", value);
345   m_last_config_value = value;
346   ctrl_callback(CONFIG, RATEXMIT);
347}
348
349void tms9902_device::set_stop_bits()
350{
351   int value = m_STOPB;
352   if (VERBOSE>3) LOG("TMS9902: set stop bits = %02x\n", value);
353   m_last_config_value = value;
354   ctrl_callback(CONFIG, STOPBITS);
355}
356
357void tms9902_device::set_data_bits()
358{
359   int value = m_RCL;
360   if (VERBOSE>3) LOG("TMS9902: set data bits = %02x\n", value);
361   m_last_config_value = value;
362   ctrl_callback(CONFIG, DATABITS);
363}
364
365void tms9902_device::set_parity()
366{
367   int value = (m_PENB? 2:0) | (m_ODDP? 1:0);
368   if (VERBOSE>3) LOG("TMS9902: set parity = %02x\n", value);
369   m_last_config_value = value;
370   ctrl_callback(CONFIG, PARITY);
371}
372
373void tms9902_device::transmit_line_state()
374{
375   // 00ab cdef = setting line RTS=a, CTS=b, DSR=c, DCD=d, DTR=e, RI=f
376   // The 9902 only outputs RTS and BRK
377   if (VERBOSE>3) LOG("TMS9902: transmitting line state (only RTS) = %02x\n", (m_RTSout)? 1:0);
378   m_last_config_value = (m_RTSout)? RTS : 0;
379   ctrl_callback(LINES, RTS);
380}
381
382void tms9902_device::set_rts(line_state state)
383{
384   bool lstate = (state==ASSERT_LINE);
385
386   if (lstate != m_RTSout)
387   {
388      // Signal RTS to the modem
389      if (VERBOSE>3) LOG("TMS9902: Set RTS=%d\n", lstate? 1:0);
390      m_RTSout = lstate;
391      transmit_line_state();
392   }
393}
394
395int tms9902_device::get_config_value()
396{
397   return m_last_config_value;
398}
399
400// ==========================================================================
401
402void tms9902_device::initiate_transmit()
403{
404   if (m_BRKON && m_CTSin)
405      /* enter break mode */
406      send_break(true);
407   else
408   {
409      if (!m_RTSON && (!m_CTSin || (m_XBRE && !m_BRKout)))
410         /* clear RTS output */
411         set_rts(CLEAR_LINE);
412      else
413      {
414         if (VERBOSE>5) LOG("TMS9902: transferring XBR to XSR; XSRE=false, XBRE=true\n");
415         m_XSR = m_XBR;
416         m_XSRE = false;
417         m_XBRE = true;
418
419         field_interrupts();
420
421         if (VERBOSE>4) LOG("TMS9902: transmit XSR=%02x, RCL=%02x\n", m_XSR, m_RCL);
422
423         xmit_callback(0, m_XSR & (0xff >> (3-m_RCL)));
424
425         // Should store that somewhere (but the CPU is fast enough, can afford to recalc :-) )
426         double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
427         double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
428
429         // Time for transmitting 10 bit (8 bit + start + stop)
430         m_sendtimer->adjust(attotime::from_hz(baud/10.0));
431      }
432   }
433}
434
435
436
437/*----------------------------------------------------------------
438    TMS9902 CRU interface.
439----------------------------------------------------------------*/
440
441/*
442    Read a 8 bit chunk from tms9902.
443
444    signification:
445    bit 0-7: RBR0-7 Receive Buffer register
446    bit 8: not used (always 0)
447    bit 9: RCVERR Receive Error (RFER | ROVER | RPER)
448    bit 10: RPER Receive Parity Error
449    bit 11: ROVER Receive Overrun Error
450    bit 12: RFER Receive Framing Error
451    bit 13-15: not emulated, normally used for diagnostics
452    bit 16: RBINT (RBRL&RIENB)
453*/
454READ8_MEMBER( tms9902_device::cruread )
455{
456   UINT8 answer = 0;
457
458   offset &= 0x0003;
459
460   switch (offset)
461   {
462   case 3: // Bits 31-24
463      if (m_INT) answer |= 0x80;
464      if (m_LDCTRL || m_LDIR || m_LRDR || m_LXDR || m_BRKON) answer |= 0x40;
465      if (m_DSCH) answer |= 0x20;
466      if (m_CTSin) answer |= 0x10;
467      if (m_DSRin) answer |= 0x08;
468      if (m_RTSout) answer |= 0x04;
469      if (m_TIMELP)  answer |= 0x02;
470      if (m_TIMERR)  answer |= 0x01;
471      break;
472
473   case 2: // Bits 23-16
474      if (m_XSRE) answer |= 0x80;
475      if (m_XBRE) answer |= 0x40;
476      if (m_RBRL) answer |= 0x20;
477      if (m_DSCH && m_DSCENB) answer |= 0x10;
478      if (m_TIMELP && m_TIMENB) answer |= 0x08;
479      if (m_XBRE && m_XBIENB) answer |= 0x02;
480      if (m_RBRL && m_RIENB) answer |= 0x01;
481      break;
482
483   case 1: // Bits 15-8
484      if (m_RIN) answer |= 0x80;
485      if (m_RSBD) answer |= 0x40;
486      if (m_RFBD) answer |= 0x20;
487      if (m_RFER) answer |= 0x10;
488      if (m_ROVER) answer |= 0x08;
489      if (m_RPER) answer |= 0x04;
490      if (m_RPER || m_RFER || m_ROVER) answer |= 0x02;
491      break;
492
493   case 0: // Bits 7-0
494      answer = m_RBR;
495      break;
496   }
497   if (VERBOSE>7) LOG("TMS9902: Reading flag bits %d - %d = %02x\n", ((offset+1)*8-1), offset*8, answer);
498   return answer;
499}
500
501static inline void set_bits8(UINT8 *reg, UINT8 bits, bool set)
502{
503   if (set)
504      *reg |= bits;
505   else
506      *reg &= ~bits;
507}
508
509static inline void set_bits16(UINT16 *reg, UINT16 bits, bool set)
510{
511   if (set)
512      *reg |= bits;
513   else
514      *reg &= ~bits;
515}
516
517void tms9902_device::reset_uart()
518{
519   if (VERBOSE>1) LOG("TMS9902: resetting\n");
520
521   /*  disable all interrupts */
522   m_DSCENB = false;   // Data Set Change Interrupt Enable
523   m_TIMENB = false;   // Timer Interrupt Enable
524   m_XBIENB = false;   // Transmit Buffer Interrupt Enable
525   m_RIENB = false;      // Read Buffer Interrupt Enable
526
527   /* initialize transmitter */
528   m_XBRE = true;      // Transmit Buffer Register Empty
529   m_XSRE = true;      // Transmit Shift Register Empty
530
531   /* initialize receiver */
532   m_RBRL = false;      // Read Buffer Register Loaded
533
534   /* clear RTS */
535   m_RTSON = false;      // Request-to-send on (flag)
536   m_RTSout = true;      // Note we are doing this to ensure the state is sent to the interface
537   set_rts(CLEAR_LINE);
538   m_RTSout = false;   // what we actually want
539
540   /* set all register load flags to 1 */
541   m_LDCTRL = true;
542   m_LDIR = true;
543   m_LRDR = true;
544   m_LXDR = true;
545
546   /* clear break condition */
547   m_BRKON = false;
548   m_BRKout = false;
549
550   m_TMR = 0;
551   m_STOPB = 0;
552   m_RCL = 0;
553   m_XDR = 0;
554   m_RDR = 0;
555   m_RBR = 0;
556   m_XBR = 0;
557   m_XSR = 0;
558
559   field_interrupts();
560}
561
562/*
563    TMS9902 CRU write
564*/
565WRITE8_MEMBER( tms9902_device::cruwrite )
566{
567   data &= 1;   /* clear extra bits */
568
569   offset &= 0x1F;
570   if (VERBOSE>5) LOG("TMS9902: Setting bit %d = %02x\n", offset, data);
571
572   if (offset <= 10)
573   {
574      UINT16 mask = (1 << offset);
575
576      if (m_LDCTRL)
577      {   // Control Register mode. Values written to bits 0-7 are copied
578         // into the control register.
579         switch (offset)
580         {
581         case 0:
582            set_bits8(&m_RCL, 0x01, (data!=0));
583            // we assume that bits are written in increasing order
584            // so we do not transmit the data bits twice
585            // (will fail when bit 1 is written first)
586            break;
587         case 1:
588            set_bits8(&m_RCL, 0x02, (data!=0));
589            set_data_bits();
590            break;
591         case 2:
592            break;
593         case 3:
594            m_CLK4M = (data!=0);
595            break;
596         case 4:
597            m_ODDP = (data!=0);
598            // we also assume that the parity type is set before the parity enable
599            break;
600         case 5:
601            m_PENB = (data!=0);
602            set_parity();
603            break;
604         case 6:
605            set_bits8(&m_STOPB, 0x01, (data!=0));
606            break;
607         case 7:
608            set_bits8(&m_STOPB, 0x02, (data!=0));
609            // When bit 7 is written the control register mode is automatically terminated.
610            m_LDCTRL = false;
611            set_stop_bits();
612            break;
613         default:
614            if (VERBOSE>1) LOG("tms9902: Invalid control register address %d\n", offset);
615         }
616      }
617      else if (m_LDIR)
618      {   // Interval Register mode. Values written to bits 0-7 are copied
619         // into the interval register.
620         if (offset <= 7)
621         {
622            set_bits8(&m_TMR, mask, (data!=0));
623
624            if (offset == 7)
625            {
626               reload_interval_timer();
627               // When bit 7 is written the interval register mode is automatically terminated.
628               m_LDIR = false;
629            }
630         }
631      }
632      else if (m_LRDR || m_LXDR)
633      {
634         if (m_LRDR)
635         {   // Receive rate register mode. Values written to bits 0-10 are copied
636            // into the receive rate register.
637            if (offset < 10)
638            {
639               set_bits16(&m_RDR, mask, (data!=0));
640            }
641            else
642            {
643               // When bit 10 is written the receive register mode is automatically terminated.
644               m_RDV8 = (data!=0);
645               m_LRDR = false;
646               set_receive_data_rate();
647            }
648         }
649         if (m_LXDR)
650         {
651            // The transmit rate register can be set together with the receive rate register.
652            if (offset < 10)
653            {
654               set_bits16(&m_XDR, mask, (data!=0));
655            }
656            else
657            {
658               // Note that the transmit rate register is NOT terminated when
659               // writing bit 10. This must be done by unsetting bit 11.
660               m_XDV8 = (data!=0);
661               set_transmit_data_rate();
662            }
663         }
664      }
665      else
666      {   // LDCTRL=LDIR=LRDR=LXRD=0: Transmit buffer register mode. Values
667         // written to bits 0-7 are transferred into the transmit buffer register.
668         if (offset <= 7)
669         {
670            set_bits8(&m_XBR, mask, (data!=0));
671
672            if (offset == 7)
673            {   /* transmit */
674               m_XBRE = false;
675               // Spec: When the transmitter is active, the contents of the Transmit
676               // Buffer Register are transferred to the Transmit Shift Register
677               // each time the previous character has been completely transmitted
678               // We need to check XSRE=true as well, as the implementation
679               // makes use of a timed transmission, during which XSRE=false
680               if (m_XSRE && m_RTSout && m_CTSin && !m_BRKout)
681               {
682                  initiate_transmit();
683               }
684            }
685         }
686      }
687      return;
688   }
689   switch (offset)
690   {
691      case 11:
692         m_LXDR = (data!=0);
693         break;
694      case 12:
695         m_LRDR = (data!=0);
696         break;
697      case 13:
698         m_LDIR = (data!=0);
699         // Spec: Each time LDIR is reset the contents of the Interval
700         // Register are loaded into the Interval Timer, thus restarting
701         // the timer.
702         if (data==0)
703            reload_interval_timer();
704         break;
705      case 14:
706         m_LDCTRL = (data!=0);
707         break;
708      case 15:
709         m_TSTMD = (data!=0); // Test mode not implemented
710         break;
711      case 16:
712         if (data!=0)
713         {
714            m_RTSON = true;
715            set_rts(ASSERT_LINE);
716            if (m_CTSin)
717            {
718               if (m_XSRE && !m_XBRE && !m_BRKout)
719                  initiate_transmit();
720               else if (m_BRKON)
721                  send_break(true);
722            }
723         }
724         else
725         {
726            m_RTSON = false;
727            if (m_XBRE && m_XSRE && !m_BRKout)
728            {
729               set_rts(CLEAR_LINE);
730            }
731         }
732         return;
733      case 17:
734         if (VERBOSE>3) LOG("TMS9902: set BRKON=%d; BRK=%d\n", data, m_BRKout? 1:0);
735         m_BRKON = (data!=0);
736         if (m_BRKout && data==0)
737         {
738            // clear BRK
739            m_BRKout = false;
740            if ((!m_XBRE) && m_CTSin)
741            {
742               /* transmit next byte */
743               initiate_transmit();
744            }
745            else if (!m_RTSON)
746            {
747               /* clear RTS */
748               set_rts(CLEAR_LINE);
749            }
750         }
751         else if (m_XBRE && m_XSRE && m_RTSout && m_CTSin)
752         {
753            send_break(data!=0);
754         }
755         return;
756      case 18:
757         // Receiver Interrupt Enable
758         // According to spec, (re)setting this flag clears the RBRL flag
759         // (the only way to clear the flag!)
760         m_RIENB = (data!=0);
761         m_RBRL = false;
762         if (VERBOSE>4) LOG("TMS9902: set RBRL=0, set RIENB=%d\n", data);
763         field_interrupts();
764         return;
765      case 19:
766         /* Transmit Buffer Interrupt Enable */
767         m_XBIENB = (data!=0);
768         if (VERBOSE>4) LOG("TMS9902: set XBIENB=%d\n", data);
769         field_interrupts();
770         return;
771      case 20:
772         /* Timer Interrupt Enable */
773         m_TIMENB = (data!=0);
774         m_TIMELP = false;
775         m_TIMERR = false;
776         field_interrupts();
777         return;
778      case 21:
779         /* Data Set Change Interrupt Enable */
780         m_DSCENB = (data!=0);
781         m_DSCH = false;
782         if (VERBOSE>4) LOG("TMS9902: set DSCH=0, set DSCENB=%d\n", data);
783         field_interrupts();
784         return;
785      case 31:
786         /* RESET */
787         reset_uart();
788         return;
789      default:
790         if (VERBOSE>1) LOG("TMS9902: Writing to undefined flag bit position %d = %01x\n", offset, data);
791   }
792}
793
794/*-------------------------------------------------
795    DEVICE_STOP( tms9902 )
796-------------------------------------------------*/
797
798void tms9902_device::device_stop()
799{
800   if (m_dectimer)
801   {
802      m_dectimer->reset();
803      m_dectimer = NULL;
804   }
805}
806
807/*-------------------------------------------------
808    DEVICE_RESET( tms9902 )
809-------------------------------------------------*/
810
811void tms9902_device::device_reset()
812{
813   reset_uart();
814}
815
816/*-------------------------------------------------
817    DEVICE_START( tms9902 )
818-------------------------------------------------*/
819
820void tms9902_device::device_start()
821{
822   const tms9902_interface *intf = reinterpret_cast<const tms9902_interface *>(static_config());
823
824   m_clock_rate = clock();
825
826   int_callback.resolve(intf->int_callback, *this);
827   rcv_callback.resolve(intf->rcv_callback, *this);
828   xmit_callback.resolve(intf->xmit_callback, *this);
829   ctrl_callback.resolve(intf->ctrl_callback, *this);
830
831   m_dectimer = timer_alloc(DECTIMER);
832   m_recvtimer = timer_alloc(RECVTIMER);
833   m_sendtimer = timer_alloc(SENDTIMER);
834}
835
836const device_type TMS9902 = &device_creator<tms9902_device>;
837
src/emu/machine/tms9902.h
r0r15653
1/****************************************************************************
2
3    TMS9902 Asynchronous Communication Controller
4    See tms9902.c for documentation
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
12#ifndef __TMS9902_H__
13#define __TMS9902_H__
14
15#include "emu.h"
16
17// Serial control protocol values
18#define TYPE_TMS9902 0x01
19
20// Configuration (output only)
21#define CONFIG   0x80
22#define RATERECV 0x70
23#define RATEXMIT 0x60
24#define DATABITS 0x50
25#define STOPBITS 0x40
26#define PARITY   0x30
27
28// Exceptional states (BRK: both directions; FRMERR/PARERR: input only)
29#define EXCEPT 0x40
30#define BRK    0x02
31#define FRMERR 0x04
32#define PARERR 0x06
33
34// Line states (RTS, DTR: output; CTS, DSR, RI, DCD: input)
35#define LINES 0x00
36#define RTS 0x20
37#define CTS 0x10
38#define DSR 0x08
39#define DCD 0x04
40#define DTR 0x02
41#define RI  0x01
42
43extern const device_type TMS9902;
44
45typedef struct _tms9902_interface
46{
47   devcb_write_line   int_callback;
48   devcb_write_line   rcv_callback;
49   devcb_write8      xmit_callback;
50   devcb_write8      ctrl_callback;
51} tms9902_interface;
52
53class tms9902_device : public device_t
54{
55public:
56   tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
57   void   set_clock(bool state);
58
59   void   rcv_cts(line_state state);
60   void   rcv_dsr(line_state state);
61   void   rcv_data(UINT8 data);
62   void   rcv_break(bool value);
63   void   rcv_framing_error();
64   void   rcv_parity_error();
65
66   double   get_baudpoll();
67
68   int      get_config_value();
69
70   DECLARE_READ8_MEMBER( cruread );
71   DECLARE_WRITE8_MEMBER( cruwrite );
72
73protected:
74   void   device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void   device_start();
76   void   device_reset();
77   void   device_stop();
78
79private:
80   void   field_interrupts();
81   void   reload_interval_timer();
82   void   send_break(bool state);
83   void   set_receive_data_rate();
84   void   set_transmit_data_rate();
85   void   set_stop_bits();
86   void   set_data_bits();
87   void   set_parity();
88   void   transmit_line_state();
89   void   set_rts(line_state state);
90   void   initiate_transmit();
91   void   reset_uart();
92
93   devcb_resolved_write_line      int_callback;
94   devcb_resolved_write_line      rcv_callback;
95   devcb_resolved_write8         xmit_callback;
96   devcb_resolved_write8         ctrl_callback;      // needs to be used with get_config_value
97
98   // tms9902 clock rate (PHI* pin, normally connected to TMS9900 Phi3*)
99   // Official range is 2MHz-3.3MHz.  Some tms9902s were sold as "MP9214", and
100   // were tested for speeds up to 4MHz, provided the clk4m control bit is set.
101   // (warning: 3MHz on a tms9900 is equivalent to 12MHz on a tms9995 or tms99000)
102   double   m_clock_rate;
103
104   /* Modes */
105   bool   m_LDCTRL;      // Load control register
106   bool   m_LDIR;         // Load interval register
107   bool   m_LRDR;         // Load receive data register
108   bool   m_LXDR;         // Load transmit data register
109   bool   m_TSTMD;         // Test mode
110
111   /* output pin */
112   bool   m_RTSON;         // RTS-on request
113
114   /* transmitter registers */
115   bool   m_BRKON;         // BRK-on request
116   bool   m_BRKout;      // indicates the current BRK state
117
118   UINT8   m_XBR;         // transmit buffer register
119   UINT8   m_XSR;         // transmit shift register
120
121   /* receiver registers */
122   UINT8   m_RBR;         // Receive buffer register
123
124   /* Interrupt enable flags */
125   bool   m_DSCENB;      // Data set change interrupt enable
126   bool   m_RIENB;         // Receiver interrupt enable
127   bool   m_XBIENB;      // Tansmit buffer interrupt enable
128   bool   m_TIMENB;      // Timer interrupt enable
129
130   /*
131        Rate registers. The receive bit rate calculates as
132        bitrate = clock1 / (2 * (8 ^ RDV8) * RDR)
133        (similarly for transmit)
134
135        where clock1 = clock_rate / (CLK4M? 4:3)
136    */
137   UINT16   m_RDR;         // Receive data rate
138   bool   m_RDV8;         // Receive data rate divider
139   UINT16   m_XDR;         // Transmit data rate
140   bool   m_XDV8;         // Transmit data rate divider
141
142   /* Status flags */
143   bool   m_INT;         // mirrors /INT output line, inverted
144   bool   m_DSCH;         // Data set status change
145
146   bool   m_CTSin;         // Inverted /CTS input (i.e. CTS)
147   bool   m_DSRin;         // Inverted /DSR input (i.e. DSR)
148   bool   m_RTSout;      // Current inverted /RTS line state (i.e. RTS)
149
150   bool   m_TIMELP;      // Timer elapsed
151   bool   m_TIMERR;      // Timer error
152
153   bool   m_XSRE;         // Transmit shift register empty
154   bool   m_XBRE;         // Transmit buffer register empty
155   bool   m_RBRL;         // Receive buffer register loaded
156
157   bool   m_RIN;         // State of the RIN pin
158   bool   m_RSBD;         // Receive start bit detect
159   bool   m_RFBD;         // Receive full bit detect
160   bool   m_RFER;         // Receive framing error
161   bool   m_ROVER;         // Receiver overflow
162   bool   m_RPER;         // Receive parity error
163
164   UINT8   m_RCL;         // Character length
165   bool   m_ODDP;
166   bool   m_PENB;
167   UINT8   m_STOPB;
168   bool   m_CLK4M;      // /PHI input divide select
169
170   UINT8   m_TMR;      /* interval timer */
171
172   /* clock registers */
173   emu_timer *m_dectimer;         /* MESS timer, used to emulate the decrementer register */
174   emu_timer *m_recvtimer;
175   emu_timer *m_sendtimer;
176
177   // This value is the ratio of data input versus the poll rate. The
178   // data source should deliver data bytes at every 1/baudpoll call.
179   // This is to ensure that data is delivered at a rate that is expected
180   // from the emulated program.
181   double   m_baudpoll;
182
183   // Caches the last configuration setting (used with the ctrl_callback)
184   int      m_last_config_value;
185};
186
187/***************************************************************************
188    DEVICE CONFIGURATION MACROS
189***************************************************************************/
190
191#define MCFG_TMS9902_ADD(_tag, _intrf, _clock) \
192   MCFG_DEVICE_ADD(_tag, TMS9902, _clock) \
193   MCFG_DEVICE_CONFIG(_intrf)
194
195#endif /* __TMS9902_H__ */
src/mess/machine/tms9901.c
r15652r15653
1/****************************************************************************
2
3    TMS9901 Programmable System Interface
4
5Overview:
6    TMS9901 is a support chip for TMS9900.  It handles interrupts, provides
7    several I/O pins, and a timer (a.k.a. clock: it is merely a register which
8    decrements regularly and can generate an interrupt when it reaches 0).
9
10    It communicates with the TMS9900 with the CRU bus, and with the rest of the
11    world with a number of parallel I/O pins.
12
13    I/O and timer functions should work with any other 990/99xx/99xxx CPU.
14    On the other hand, interrupt handling was primarily designed for tms9900
15    and 99000 based systems: other CPUs can support interrupts, but not the 16
16    distinct interrupt vectors.
17
18Pins:
19    Vcc, Vss: power supply
20    Phi*: system clock (connected to TMS9900 Phi3* or TMS9980 CLKOUT*)
21    RST1*: reset input
22    CRUIN, CRUOUT, CRUCLK, CE*, S0-S4: CRU bus (CPU interface)
23    INTREQ*, IC0-IC3: interrupt bus (CPU interface)
24    INT*1-INT*6: used as interrupt/input pins.
25    P0-P6: used as input/output pins.
26    INT*7/P15-INT*15/P7: used as either interrupt/input or input/output pins.
27      Note that a pin cannot be used simultaneously as output and as interrupt.
28      (This is mostly obvious, but it implies that you cannot trigger an
29      interrupt by setting the output state of a pin, which is not SO obvious.)
30
31Interrupt handling:
32    After each clock cycle, TMS9901 latches the state of INT1*-INT15* (except
33    pins which are set as output pins).  If the clock is enabled, it replaces
34    INT3* with an internal timer interrupt flag.  Then it inverts the value and
35    performs a bit-wise AND with the interrupt mask.
36
37    If there are some unmasked interrupt bits, INTREQ* is asserted and the code
38    of the lowest active interrupt is placed on IC0-IC3.  If these pins are
39    duly connected to the tms9900 INTREQ* and IC0-IC3 pins, the result is that
40    asserting an INTn* on tms9901 will cause a level-n interrupt request on the
41    tms9900, provided that this interrupt pin is not masked in tms9901, and
42    that no unmasked higher-priority (i.e. lower-level) interrupt pin is set.
43
44    This interrupt request lasts for as long as the interrupt pin and the
45    relevant bit in the interrupt mask are set (level-triggered interrupts).
46    (The request may be shadowed by a higher-priority interrupt request, but
47    it will resume when the higher-priority request ends.)
48
49    TIMER interrupts are kind of an exception, since they are not associated
50    with an external interrupt pin.  I think there is an internal timer
51    interrupt flag that is set when the decrementer reaches 0, and is cleared
52    by a write to the 9901 int*3 enable bit ("SBO 3" in interrupt mode).
53
54TODO:
55    * Emulate the RST1* input.  Note that RST1* active (low) makes INTREQ*
56      inactive (high) with IC0-IC3 = 0.
57    * the clock read register is updated every time the timer decrements when
58      the TMS9901 is not in clock mode.  This probably implies that if the
59      clock mode is cleared and re-asserted immediately, the tms9901 may fail
60      to update the clock read register: this is not emulated.
61    * The clock mode is entered when a 1 is written to the control bit.  It is
62      exited when a 0 is written to the control bit or the a tms9901 select bit
63      greater than 15 is accessed.  According to the data sheet, "when CE* is
64      inactive (HIGH), the PSI is not disabled from seeing the select lines.
65      As the CPU is accessing memory, A10-A14 could very easily have a value of
66      15 or greater" (this is assuming that S0-S4 are connected to A10-A14,
67      which makes sense with most tms9900 family members).  There is no way
68      this "feature" (I would call it a hardware bug) can be emulated
69      efficiently, as we would need to watch every memory access.
70
71MZ: According to the description in
72       A. Osborne, G. Kane: Osborne 16-bit microprocessor handbook
73       page 3-81
74    the 9901 only temporarily leaves the timer mode as long as S0 is set to 1.
75    In the meantime the timer function continues but cannot be queried. This
76    makes it possible to continue using the chip as a timer while working with
77    its I/O pins. Thus I believe the above TODO concering the exit of the timer
78    mode is not applicable.
79    The problem is that the original 9901 specification is not clear about this.
80
81MZ: Turned to class (January 2012)
82
83TODO: Tests on a real machine
84- Set an interrupt input (e.g. keyboard for Geneve), trigger RST2*, check whether
85  interrupt mask has been reset
86- Check whether the clock_read_register is updated whenever clock mode is exited
87  (in particular when S0=1, i.e. A10=1 -> addresses xxxx xxxx xx1x xxxx
88  requires to write a program that fits into 32 bytes; workspace elsewhere)
89
90    Raphael Nabet, 2000-2004
91    Michael Zapf
92
93    February 2012: Rewritten as class
94
95*****************************************************************************/
96
97#include <math.h>
98#include "emu.h"
99
100#include "tms9901.h"
101
102#define VERBOSE 1
103#define LOG logerror
104
105/*
106    Constructor
107*/
108tms9901_device::tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
109: device_t(mconfig, TMS9901, "TMS9901 Programmable System Interface", tag, owner, clock)
110{
111}
112
113/*
114    should be called after any change to int_state or enabled_ints.
115*/
116void tms9901_device::field_interrupts(void)
117{
118   int current_ints;
119
120   /* int_state: state of lines int1-int15 */
121   current_ints = m_int_state;
122   if (m_clock_register != 0)
123   {   /* if timer is enabled, INT3 pin is overriden by timer */
124      if (m_timer_int_pending)
125      {
126         if (VERBOSE>8) LOG("tms9901: timer fires\n");
127         current_ints |= TMS9901_INT3;
128      }
129      else
130      {
131         if (VERBOSE>8) LOG("tms9901: timer clear\n");
132         current_ints &= ~TMS9901_INT3;
133      }
134   }
135
136   /* enabled_ints: enabled interrupts */
137   /* mask out all int pins currently set as output */
138   current_ints &= m_enabled_ints & (~m_pio_direction_mirror);
139
140   // Check whether we have a new state. For systems that use level-triggered
141   // interrupts it should not do any harm if the line is re-asserted
142   // but we may as well avoid this.
143   if (current_ints == m_old_int_state)
144      return;
145
146   m_old_int_state = current_ints;
147
148   if (current_ints)
149   {
150      // find which interrupt tripped us:
151      // the number of the first (i.e. least significant) non-zero bit among
152      // the 16 first bits
153      // we simply look for the first bit set to 1 in current_ints... */
154      int level = 0;
155
156      while ((current_ints & 1)==0)
157      {
158         current_ints >>= 1;   /* try next bit */
159         level++;
160      }
161      m_int_pending = true;
162      if (!m_interrupt.isnull())
163         m_interrupt(level, 1);  // the offset carries the IC0-3 level
164   }
165   else
166   {
167      m_int_pending = false;
168      if (!m_interrupt.isnull())
169         m_interrupt(0xf, 0);  //Spec: INTREQ*=1 <=> IC0,1,2,3 = 1111
170   }
171}
172
173/*
174    function which should be called by the driver when the state of an INTn*
175    pin changes (only required if the pin is set up as an interrupt pin)
176
177    state == 0: INTn* is inactive (high)
178    state != 0: INTn* is active (low)
179
180    0<=pin_number<=15
181*/
182void tms9901_device::set_single_int(int pin_number, int state)
183{
184   /* remember new state of INTn* pin state */
185   if (state==ASSERT_LINE)
186      m_int_state |= 1 << pin_number;
187   else
188      m_int_state &= ~(1 << pin_number);
189
190   /* we do not need to always call this function - time for an optimization */
191   field_interrupts();
192}
193
194/*
195    load the content of m_clock_register into the decrementer
196*/
197void tms9901_device::timer_reload(void)
198{
199   if (m_clock_register != 0)
200   {   /* reset clock interval */
201      m_decrementer_value = m_clock_register;
202      m_decrementer->enable(true);
203   }
204   else
205   {   /* clock interval == 0 -> no timer */
206      m_decrementer->enable(false);
207   }
208}
209
210/*----------------------------------------------------------------
211    TMS9901 CRU interface.
212----------------------------------------------------------------*/
213
214/*
215    Read a 8 bit chunk from tms9901.
216
217    signification:
218    bit 0: m_clock_mode
219    if (m_clock_mode == false)
220     bit 1-15: current status of the INT1*-INT15* pins
221    else
222     bit 1-14: current timer value
223     bit 15: value of the INTREQ* (interrupt request to TMS9900) pin.
224
225    bit 16-31: current status of the P0-P15 pins (quits timer mode, too...)
226*/
227READ8_MEMBER( tms9901_device::read )
228{
229   int answer = 0;
230
231   offset &= 0x003;
232
233   switch (offset)
234   {
235   case 0:
236      if (m_clock_mode)
237      {   /* clock mode */
238         answer = ((m_clock_read_register & 0x7F) << 1) | 0x01;
239      }
240      else
241      {   /* interrupt mode */
242         // m_int_state stores the INTx values, which are inverted to the pin levels (INTx*)
243         answer = ((~m_int_state) & m_supported_int_mask) & 0xFF;
244
245         if (!m_read_block.isnull())
246            answer |= m_read_block(TMS9901_CB_INT7);
247
248         answer &= ~ m_pio_direction_mirror;
249         answer |= (m_pio_output_mirror & m_pio_direction_mirror) & 0xFF;
250      }
251      break;
252   case 1:
253      if (m_clock_mode)
254      {   /* clock mode */
255         answer = (m_clock_read_register & 0x3F80) >> 7;
256         if (!m_int_pending)
257            answer |= 0x80;
258      }
259      else
260      {   /* interrupt mode */
261         answer = ((~m_int_state) & m_supported_int_mask) >> 8;
262
263         if (!m_read_block.isnull())
264            answer |= m_read_block(TMS9901_INT8_INT15);
265
266         answer &= ~ (m_pio_direction_mirror >> 8);
267         answer |= (m_pio_output_mirror & m_pio_direction_mirror) >> 8;
268      }
269      break;
270   case 2:
271      /* exit timer mode */
272      // MZ: See comments at the beginning. I'm pretty sure this is not correct.
273      // m_clock_mode = false;
274
275      if (!m_read_block.isnull())
276         answer = m_read_block(TMS9901_P0_P7);
277      else
278         answer = 0;
279
280      answer &= ~ m_pio_direction;
281      answer |= (m_pio_output & m_pio_direction) & 0xFF;
282
283      break;
284   case 3:
285      // MZ: see above
286      // m_clock_mode = false;        // exit timer mode
287      if (!m_read_block.isnull())
288         answer = m_read_block(TMS9901_P8_P15);
289      else
290         answer = 0;
291
292      answer &= ~ (m_pio_direction >> 8);
293      answer |= (m_pio_output & m_pio_direction) >> 8;
294
295      break;
296   }
297
298   return answer;
299}
300
301/*
302    Write 1 bit to tms9901.
303
304    signification:
305    bit 0: write m_clock_mode
306    if (!m_clock_mode)
307     bit 1-15: write interrupt mask register
308    else
309     bit 1-14: write timer period
310     bit 15: if written value == 0, soft reset (just resets all I/O pins as input)
311
312    bit 16-31: set output state of P0-P15 (and set them as output pin) (quit timer mode, too...)
313*/
314WRITE8_MEMBER ( tms9901_device::write )
315{
316   data &= 1;   /* clear extra bits */
317   offset &= 0x01F;
318   switch (offset)
319   {
320   case 0x00:   /* write to mode bit */
321      if (data == 0)
322      {
323         /* we are quitting clock mode */
324         m_clock_mode = false;
325         if (VERBOSE>5) LOG("tms9901: int mode\n");
326      }
327      else
328      {
329         m_clock_mode = true;
330         if (VERBOSE>5) LOG("tms9901: clock mode\n");
331         // we are switching to clock mode: latch the current value of
332         // the decrementer register
333         if (m_clock_register != 0)
334            m_clock_read_register = m_decrementer_value;
335         else
336            m_clock_read_register = 0;      /* timer inactive... */
337      }
338      break;
339   case 0x01:
340   case 0x02:
341   case 0x03:
342   case 0x04:
343   case 0x05:
344   case 0x06:
345   case 0x07:
346   case 0x08:
347   case 0x09:
348   case 0x0A:
349   case 0x0B:
350   case 0x0C:
351   case 0x0D:
352   case 0x0E:
353      // write one bit to 9901 (bits 1-14)
354      //
355      // m_clock_mode==false ?  Disable/Enable an interrupt
356      // :  Bit in clock interval
357      //
358      // offset is the index of the modified bit of register (-> interrupt number -1)
359      if (m_clock_mode)
360      {   /* modify clock interval */
361         int mask = 1 << ((offset & 0x0F) - 1);   /* corresponding mask */
362
363         if (data)
364            m_clock_register |= mask;      /* set bit */
365         else
366            m_clock_register &= ~mask;      /* clear bit */
367
368         /* reset clock timer (page 8) */
369         if (VERBOSE>6) LOG("tms9901: clock register = %04x\n", m_clock_register);
370         timer_reload();
371      }
372      else
373      {   /* modify interrupt enable mask */
374         int mask = 1 << (offset & 0x0F);   /* corresponding mask */
375
376         if (data)
377            m_enabled_ints |= mask;      /* set bit */
378         else
379            m_enabled_ints &= ~mask;      /* unset bit */
380
381         if (offset == 3)
382            m_timer_int_pending = false;   /* SBO 3 clears pending timer interrupt (??) */
383
384         if (VERBOSE>6) LOG("tms9901: interrupts = %04x\n", m_enabled_ints);
385         field_interrupts();      /* changed interrupt state */
386      }
387      break;
388   case 0x0F:
389      if (m_clock_mode)
390      {   /* in clock mode this is the soft reset bit */
391         if (!data)
392         {   // TMS9901 soft reset (RST2*)
393            // Spec: "Writing a 0 to bit 15 while in the clock mode executes a soft reset on the I/O pins.
394            // [...] RST2* will program all ports to the input mode"
395            m_pio_direction = 0;
396            m_pio_direction_mirror = 0;
397
398            // "RST1* (power-up reset) will reset all mask bits low."
399            // Spec is not clear on whether the mask bits are also reset by RST2*
400            // TODO: Check on a real machine. (I'd guess from the text they are not touched)
401            m_enabled_ints = 0;
402            if (VERBOSE>5) LOG("tms9901: Soft reset (RST2*)\n");
403         }
404      }
405      else
406      {   /* modify interrupt enable mask */
407         if (data)
408            m_enabled_ints |= 0x4000;      /* set bit */
409         else
410            m_enabled_ints &= ~0x4000;      /* unset bit */
411
412         if (VERBOSE>6) LOG("tms9901: interrupts = %04x\n", m_enabled_ints);
413         field_interrupts();      /* changed interrupt state */
414      }
415      break;
416   case 0x10:
417   case 0x11:
418   case 0x12:
419   case 0x13:
420   case 0x14:
421   case 0x15:
422   case 0x16:
423   case 0x17:
424   case 0x18:
425   case 0x19:
426   case 0x1A:
427   case 0x1B:
428   case 0x1C:
429   case 0x1D:
430   case 0x1E:
431   case 0x1F:
432      int pin = offset & 0x0F;
433      if (VERBOSE>6) LOG("tms9901: output on P%d = %d\n", pin, data);
434      int mask = (1 << pin);
435
436      // MZ: see above - I think this is wrong
437      // m_clock_mode = false; // exit timer mode
438
439      m_pio_direction |= mask;         /* set up as output pin */
440
441      if (data)
442         m_pio_output |= mask;
443      else
444         m_pio_output &= ~mask;
445
446      if (pin >= 7)
447      {   /* pins P7-P15 are mirrored as INT15*-INT7* */
448         int pin2 = 22 - pin;
449         int mask2 = (1 << pin2);
450
451         m_pio_direction_mirror |= mask2;   /* set up as output pin */
452
453         if (data)
454            m_pio_output_mirror |= mask2;
455         else
456            m_pio_output_mirror &= ~ mask2;
457      }
458
459      if (!m_write_line[pin].isnull())
460         (m_write_line[pin])(data);
461
462      break;
463   }
464}
465
466/*
467    Timer callback
468    Decrementer counts down the value set in clock mode; when it reaches 0,
469    raises an interrupt and resets to the start value
470    The decrementer works as long as the clock_register contains a non-zero value.
471*/
472void tms9901_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
473{
474   if (id==DECREMENTER) // we have only that one
475   {
476      m_decrementer_value--;
477      if (VERBOSE>6) LOG("tms9901: decrementer = %d\n", m_decrementer_value);
478      if (m_decrementer_value<=0)
479      {
480         m_timer_int_pending = true;         // decrementer interrupt requested
481         field_interrupts();
482         m_decrementer_value = m_clock_register;
483      }
484   }
485}
486
487/*-------------------------------------------------
488    DEVICE_STOP( tms9901 )
489-------------------------------------------------*/
490
491void tms9901_device::device_stop(void)
492{
493}
494
495/*-------------------------------------------------
496    DEVICE_RESET( tms9901 )
497-------------------------------------------------*/
498
499void tms9901_device::device_reset(void)
500{
501   m_timer_int_pending = false;
502   m_enabled_ints = 0;
503
504   m_pio_direction = 0;
505   m_pio_direction_mirror = 0;
506   m_pio_output = m_pio_output_mirror = 0;
507
508   m_int_state = 0;
509   m_old_int_state = -1;
510   field_interrupts();
511
512   m_clock_mode = false;
513
514   m_clock_register = 0;
515   timer_reload();
516}
517
518
519/*-------------------------------------------------
520    DEVICE_START( tms9901 )
521-------------------------------------------------*/
522
523void tms9901_device::device_start(void)
524{
525   const tms9901_interface *intf = reinterpret_cast<const tms9901_interface *>(static_config());
526   m_supported_int_mask = intf->interrupt_mask;
527
528   m_decrementer = timer_alloc(DECREMENTER);
529   m_decrementer->adjust(attotime::from_hz(clock() / 64.), 0, attotime::from_hz(clock() / 64.));
530   m_decrementer->enable(false);
531
532   m_read_block.resolve(intf->read_handler, *this);
533
534   for (int i=0; i < 16; i++)
535   {
536      m_write_line[i].resolve(intf->write_handler[i], *this);
537   }
538
539   m_interrupt.resolve(intf->interrupt_callback, *this);
540}
541
542const device_type TMS9901 = &device_creator<tms9901_device>;
src/mess/machine/tms9901.h
r15652r15653
1/****************************************************************************
2
3    TMS9901 Programmable System Interface
4    See tms9901.c for documentation
5
6    Raphael Nabet
7    Michael Zapf
8
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
13#ifndef __TMS9901_H__
14#define __TMS9901_H__
15
16#include "emu.h"
17
18extern const device_type TMS9901;
19
20/***************************************************************************
21    MACROS
22***************************************************************************/
23
24/* Masks for the interrupts levels available on TMS9901 */
25#define TMS9901_INT1 0x0002
26#define TMS9901_INT2 0x0004
27#define TMS9901_INT3 0x0008      // overriden by the timer interrupt
28#define TMS9901_INT4 0x0010
29#define TMS9901_INT5 0x0020
30#define TMS9901_INT6 0x0040
31#define TMS9901_INT7 0x0080
32#define TMS9901_INT8 0x0100
33#define TMS9901_INT9 0x0200
34#define TMS9901_INTA 0x0400
35#define TMS9901_INTB 0x0800
36#define TMS9901_INTC 0x1000
37#define TMS9901_INTD 0x2000
38#define TMS9901_INTE 0x4000
39#define TMS9901_INTF 0x8000
40
41enum
42{
43   TMS9901_CB_INT7 = 0,
44   TMS9901_INT8_INT15 = 1,
45   TMS9901_P0_P7 = 2,
46   TMS9901_P8_P15 = 3
47};
48
49/***************************************************************************
50    CLASS DEFINITION
51***************************************************************************/
52
53typedef struct _tms9901_interface
54{
55   int             interrupt_mask;         // a bit for each input pin whose state is always notified to the TMS9901 core
56   devcb_read8       read_handler;         // 4*8 bits, to be selected using the offset (0-3)
57   devcb_write_line   write_handler[16];      // 16 Pn outputs
58   devcb_write8      interrupt_callback;    // called when interrupt bus state changes
59} tms9901_interface;
60
61class tms9901_device : public device_t
62{
63public:
64   tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
65
66   void set_single_int(int pin_number, int state);
67
68   DECLARE_READ8_MEMBER( read );
69   DECLARE_WRITE8_MEMBER( write );
70
71private:
72   static const device_timer_id DECREMENTER = 0;
73
74   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void timer_reload(void);
76   void field_interrupts(void);
77
78   void device_start(void);
79   void device_stop(void);
80   void device_reset(void);
81
82   /* interrupt registers */
83   // mask:  bit #n is set if pin #n is supported as an interrupt pin,
84   // i.e. the driver sends a notification whenever the pin state changes
85   // setting these bits is not required, but it saves you the trouble of
86   // saving the state of interrupt pins and feeding it to the port read
87   // handlers again
88   int m_supported_int_mask;
89   int m_int_state;         // state of the int1-int15 lines (must be inverted when queried)
90   int m_old_int_state;      // stores the previous value to avoid useless INT line assertions
91   int m_enabled_ints;         // interrupt enable mask
92
93   bool m_int_pending;         // status of the int* pin (connected to TMS9900)
94   bool m_timer_int_pending;   // timer int pending (overrides int3 pin if timer enabled)
95
96   // PIO registers
97   int m_pio_direction;      // direction register for PIO
98
99   // current PIO output (to be masked with pio_direction)
100   int m_pio_output;
101
102   // mirrors used for INT7*-INT15*
103   int m_pio_direction_mirror;
104   int m_pio_output_mirror;
105
106   // =======================================================================
107
108   // TMS9901 clock mode
109   // false = so-called interrupt mode (read interrupt state, write interrupt enable mask)
110   // true = clock mode (read/write clock interval)
111   bool m_clock_mode;
112
113   // MESS timer, used to emulate the decrementer register
114   emu_timer *m_decrementer;
115
116   // clock interval, loaded in decrementer when it reaches 0.
117   // 0 means decrementer off
118   int m_clock_register;
119
120   // Current decrementer value
121   int m_decrementer_value;
122
123   // when we go into timer mode, the decrementer is copied there to allow to read it reliably
124   int m_clock_read_register;
125
126   // =======================================================================
127
128   // Callbacks
129   devcb_resolved_read8      m_read_block;
130   devcb_resolved_write_line   m_write_line[16];
131   devcb_resolved_write8      m_interrupt;  // also delivers the interrupt level
132};
133
134/***************************************************************************
135    DEVICE CONFIGURATION MACROS
136***************************************************************************/
137
138#define MCFG_TMS9901_ADD(_tag, _intrf, _rate) \
139   MCFG_DEVICE_ADD(_tag, TMS9901, _rate) \
140   MCFG_DEVICE_CONFIG(_intrf)
141
142#endif /* __TMS9901_H__ */
src/mess/machine/tms9902.c
r15652r15653
1/****************************************************************************
2
3    TMS9902 Asynchronous Communication Controller
4
5    TMS9902 is an asynchronous serial controller for use with the TI990 and
6    TMS9900 family.  It provides serial I/O, three extra I/O pins (namely RTS,
7    DSR and CTS), and a timer.  It communicates with the CPU through the CRU
8    I/O bus, and one interrupt pin.
9
10               +----+--+----+
11     <-   /INT |1   \--/  18| VCC
12     <-   XOUT |2         17| /CE     <-
13     ->    RIN |3         16| /PHI    <-
14     <-  CRUIN |4         15| CRUCLK  <-
15     <-   /RTS |5         14| S0      <-
16     ->   /CTS |6         13| S1      <-
17     ->   /DSR |7         12| S2      <-
18     -> CRUOUT |8         11| S3      <-
19           VSS |9         10| S4      <-
20               +------------+
21
22     The CRUIN line borrows its name from the connector of the connected CPU
23     where it is an input, so CRUIN is an output of this chip. The same is true
24     for CRUOUT.
25
26     /PHI is a TTL clock input with 4 MHz maximum rate.
27
28    IMPORTANT NOTE: The previous versions of TMS9902 attempted to write their
29    output to a file. This implementation is able to communicate with an external
30    UART via a socket connection and an external bridge. However, the work is
31    not done yet, and until then the file writing is disabled.
32
33    Raphael Nabet, 2003
34    Michael Zapf, 2011
35    February 2012: Rewritten as class
36
37*****************************************************************************/
38
39#include <math.h>
40#include "tms9902.h"
41
42#define VERBOSE 1
43#define LOG logerror
44
45enum
46{
47   DECTIMER,
48   RECVTIMER,
49   SENDTIMER
50};
51
52// Polling frequency. We use a much higher value to allow for line state changes
53// happening between character transmissions (which happen in parallel in real
54// communications but which must be serialized here)
55#define POLLING_FREQ 20000
56
57
58/*
59    Constructor
60*/
61tms9902_device::tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
62: device_t(mconfig, TMS9902, "TMS9902 Asynchronous Communication Controller", tag, owner, clock)
63{
64}
65
66/*
67    should be called after any change to int_state or enabled_ints.
68*/
69void tms9902_device::field_interrupts()
70{
71   bool new_int = (m_DSCH && m_DSCENB)
72                     || (m_RBRL && m_RIENB)
73                     || (m_XBRE && m_XBIENB)
74                     || (m_TIMELP && m_TIMENB);
75// if (VERBOSE>3) LOG("TMS9902: interrupt flags (DSCH = %02x, DSCENB = %02x), (RBRL = %02x, RIENB = %02x), (XBRE = %02x, XBIENB = %02x), (TIMELP = %02x, TIMENB = %02x)\n",
76//  m_DSCH, m_DSCENB, m_RBRL, m_RIENB, m_XBRE, m_XBIENB, m_TIMELP, m_TIMENB);
77   if (new_int != m_INT)
78   {
79      // Only consider edges
80      m_INT = new_int;
81      if (VERBOSE>3) LOG("TMS9902: /INT = %s\n", (m_INT)? "asserted" : "cleared");
82      int_callback(m_INT? ASSERT_LINE : CLEAR_LINE);
83   }
84}
85
86/*
87    Called whenever the incoming CTS* line changes. This should be called by
88    the device that contains the UART.
89*/
90void tms9902_device::rcv_cts(line_state state)
91{
92   bool previous = m_CTSin;
93
94   // CTSin is an internal register of the TMS9902 with positive logic
95   m_CTSin = (state==ASSERT_LINE);
96
97   if (VERBOSE>3) LOG("TMS9902: CTS* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
98
99   if (m_CTSin != previous)
100   {
101      m_DSCH = true;
102      field_interrupts();
103
104      // If CTS becomes asserted and we have been sending
105      if (state==ASSERT_LINE && m_RTSout)
106      {
107         // and if the byte buffer is empty
108         if (m_XBRE)
109         {
110            // and we want to have a BRK, send it
111            if (m_BRKON) send_break(true);
112         }
113         else
114         {
115            // Buffer is not empty, we can send it
116            // If the shift register is empty, transfer the data
117            if (m_XSRE && !m_BRKout)
118            {
119               initiate_transmit();
120            }
121         }
122      }
123   }
124   else
125   {
126      m_DSCH = false;
127      if (VERBOSE>4) LOG("TMS9902: no change in CTS line, no interrupt.");
128   }
129}
130
131void tms9902_device::set_clock(bool state)
132{
133   if (state)
134      m_recvtimer->adjust(attotime::from_msec(1), 0, attotime::from_hz(POLLING_FREQ));
135   else
136      m_recvtimer->reset();
137}
138
139/*
140    Called whenever the incoming DSR* line changes. This should be called by
141    the device that contains the UART.
142*/
143void tms9902_device::rcv_dsr(line_state state)
144{
145   bool previous = m_DSRin;
146   if (VERBOSE>3) LOG("TMS9902: DSR* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
147   m_DSRin = (state==ASSERT_LINE);
148
149   if (m_DSRin != previous)
150   {
151      m_DSCH = true;
152      field_interrupts();
153   }
154   else
155   {
156      m_DSCH = false;
157      if (VERBOSE>4) LOG("TMS9902: no change in DSR line, no interrupt.");
158   }
159}
160
161/*
162    Called whenever the incoming RIN line changes. This should be called by
163    the device that contains the UART. Unlike the real thing, we deliver
164    complete bytes in one go.
165*/
166void tms9902_device::rcv_data(UINT8 data)
167{
168   // Put the received byte into the 1-byte receive buffer
169   m_RBR = data;
170
171   // Clear last errors
172   m_RFER = false;
173   m_RPER = false;
174
175   if (!m_RBRL)
176   {
177      // Receive buffer was empty
178      m_RBRL = true;
179      m_ROVER = false;
180      if (VERBOSE>3) LOG("TMS9902: Receive buffer loaded with byte %02x\n", data);
181      field_interrupts();
182   }
183   else
184   {
185      // Receive buffer was full
186      m_ROVER = true;
187      if (VERBOSE>1) LOG("TMS9902: Receive buffer still loaded; overflow error\n");
188   }
189}
190
191//------------------------------------------------
192
193/*
194    Framing error. This can only be detected by a remotely attached real UART;
195    if we get a report on a framing error we use it to announce the framing error
196    as if it occurred here.
197    The flag is reset by the next correctly received byte.
198*/
199void tms9902_device::rcv_framing_error()
200{
201   if (VERBOSE>2) LOG("TMS9902: Detected framing error\n");
202   m_RFER = true;
203}
204
205/*
206    Parity error. This can only be detected by a remotely attached real UART;
207    if we get a report on a parity error we use it to announce the parity error
208    as if it occurred here.
209    The flag is reset by the next correctly received byte.
210*/
211void tms9902_device::rcv_parity_error()
212{
213   if (VERBOSE>2) LOG("TMS9902: Detected parity error\n");
214   m_RPER = true;
215}
216
217/*
218    Incoming BREAK condition. The TMS9902 does not show any directly visible
219    reactions on a BREAK (no interrupt, no flag set). A BREAK is a time period
220    of low level on the RIN pin which makes the chip re-synchronize on the
221    next rising edge.
222*/
223void tms9902_device::rcv_break(bool value)
224{
225   if (VERBOSE>2) LOG("TMS9902: Receive BREAK=%d (no effect)\n", value? 1:0);
226}
227
228//------------------------------------------------
229
230/*
231    Timer callback
232*/
233void tms9902_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
234{
235   switch (id)
236   {
237   // This call-back is called by the MESS timer system when the decrementer
238   // reaches 0.
239   case DECTIMER:
240      m_TIMERR = m_TIMELP;
241      break;
242
243   //  Callback for the autonomous operations of the chip. This is normally
244   //  controlled by an external clock of 3-4 MHz, internally divided by 3 or 4,
245   //  depending on CLK4M. With this timer, reception of characters becomes
246   //  possible.
247   case RECVTIMER:
248      rcv_callback(ASSERT_LINE);
249      break;
250
251   case SENDTIMER:
252      // Byte has been sent
253      m_XSRE = true;
254
255      // In the meantime, the CPU may have pushed a new byte into the XBR
256      // so we loop until all data are transferred
257      if (!m_XBRE && m_CTSin)
258      {
259         initiate_transmit();
260      }
261      break;
262   }
263}
264
265/*
266    load the content of clockinvl into the decrementer
267*/
268void tms9902_device::reload_interval_timer()
269{
270   if (m_TMR)
271   {   /* reset clock interval */
272      m_dectimer->adjust(
273                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)),
274                  0,
275                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)));
276   }
277   else
278   {   /* clock interval == 0 -> no timer */
279      m_dectimer->enable(0);
280   }
281}
282
283void tms9902_device::send_break(bool state)
284{
285   if (state != m_BRKout)
286   {
287      m_BRKout = state;
288      if (VERBOSE>2) LOG("TMS9902: Sending BREAK=%d\n", state? 1:0);
289
290      // Signal BRK (on/off) to the remote site
291      ctrl_callback((EXCEPT | BRK), state? 1:0);
292   }
293}
294
295/*
296    Baudpoll value allows the callback function to know when the next data byte shall be delivered.
297*/
298double tms9902_device::get_baudpoll()
299{
300   return m_baudpoll;
301}
302
303// ==========================================================================
304
305/*
306    Sets the data rate for the receiver part. If a remote UART is attached,
307    propagate this setting.
308    The TMS9902 calculates the baud rate from the external clock, and the result
309    does not match the known baud rates precisely (e.g. for 9600 baud the
310    closest value is 9615). Other UARTs may have a different way to set baud
311    rates. Thus we transmit the bit pattern and leave it up to the remote UART
312    to calculate its own baud rate from it. Apart from that, the callback
313    function should add information about the UART.
314
315    CLK4M RDV8 RDR9 RDR8 | RDR7 RDR6 RDR5 RDR4 | RDR3 RDR2 RDR1 RDR0
316*/
317void tms9902_device::set_receive_data_rate()
318{
319   int value = (m_CLK4M? 0x800 : 0) | (m_RDV8? 0x400 : 0) | m_RDR;
320   if (VERBOSE>3) LOG("TMS9902: receive rate = %04x\n", value);
321
322   // Calculate the ratio between receive baud rate and polling frequency
323   double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
324   double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
325
326   // We assume 10 bit per character (7 data usually add 1 parity; 1 start, 1 stop)
327   // This value represents the ratio of data inputs of one poll.
328   // Thus the callback function should add up this value on each poll
329   // and deliver a data input not before it sums up to 1.
330   m_baudpoll = (double)(baud / (10*POLLING_FREQ));
331   if (VERBOSE>3) LOG ("TMS9902: baudpoll = %lf\n", m_baudpoll);
332
333   m_last_config_value = value;
334   ctrl_callback(CONFIG, RATERECV);
335}
336
337/*
338    Sets the data rate for the sender part. If a remote UART is attached,
339    propagate this setting.
340*/
341void tms9902_device::set_transmit_data_rate()
342{
343   int value = (m_CLK4M? 0x800 : 0) | (m_XDV8? 0x400 : 0) | m_XDR;
344   if (VERBOSE>3) LOG("TMS9902: set transmit rate = %04x\n", value);
345   m_last_config_value = value;
346   ctrl_callback(CONFIG, RATEXMIT);
347}
348
349void tms9902_device::set_stop_bits()
350{
351   int value = m_STOPB;
352   if (VERBOSE>3) LOG("TMS9902: set stop bits = %02x\n", value);
353   m_last_config_value = value;
354   ctrl_callback(CONFIG, STOPBITS);
355}
356
357void tms9902_device::set_data_bits()
358{
359   int value = m_RCL;
360   if (VERBOSE>3) LOG("TMS9902: set data bits = %02x\n", value);
361   m_last_config_value = value;
362   ctrl_callback(CONFIG, DATABITS);
363}
364
365void tms9902_device::set_parity()
366{
367   int value = (m_PENB? 2:0) | (m_ODDP? 1:0);
368   if (VERBOSE>3) LOG("TMS9902: set parity = %02x\n", value);
369   m_last_config_value = value;
370   ctrl_callback(CONFIG, PARITY);
371}
372
373void tms9902_device::transmit_line_state()
374{
375   // 00ab cdef = setting line RTS=a, CTS=b, DSR=c, DCD=d, DTR=e, RI=f
376   // The 9902 only outputs RTS and BRK
377   if (VERBOSE>3) LOG("TMS9902: transmitting line state (only RTS) = %02x\n", (m_RTSout)? 1:0);
378   m_last_config_value = (m_RTSout)? RTS : 0;
379   ctrl_callback(LINES, RTS);
380}
381
382void tms9902_device::set_rts(line_state state)
383{
384   bool lstate = (state==ASSERT_LINE);
385
386   if (lstate != m_RTSout)
387   {
388      // Signal RTS to the modem
389      if (VERBOSE>3) LOG("TMS9902: Set RTS=%d\n", lstate? 1:0);
390      m_RTSout = lstate;
391      transmit_line_state();
392   }
393}
394
395int tms9902_device::get_config_value()
396{
397   return m_last_config_value;
398}
399
400// ==========================================================================
401
402void tms9902_device::initiate_transmit()
403{
404   if (m_BRKON && m_CTSin)
405      /* enter break mode */
406      send_break(true);
407   else
408   {
409      if (!m_RTSON && (!m_CTSin || (m_XBRE && !m_BRKout)))
410         /* clear RTS output */
411         set_rts(CLEAR_LINE);
412      else
413      {
414         if (VERBOSE>5) LOG("TMS9902: transferring XBR to XSR; XSRE=false, XBRE=true\n");
415         m_XSR = m_XBR;
416         m_XSRE = false;
417         m_XBRE = true;
418
419         field_interrupts();
420
421         if (VERBOSE>4) LOG("TMS9902: transmit XSR=%02x, RCL=%02x\n", m_XSR, m_RCL);
422
423         xmit_callback(0, m_XSR & (0xff >> (3-m_RCL)));
424
425         // Should store that somewhere (but the CPU is fast enough, can afford to recalc :-) )
426         double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
427         double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
428
429         // Time for transmitting 10 bit (8 bit + start + stop)
430         m_sendtimer->adjust(attotime::from_hz(baud/10.0));
431      }
432   }
433}
434
435
436
437/*----------------------------------------------------------------
438    TMS9902 CRU interface.
439----------------------------------------------------------------*/
440
441/*
442    Read a 8 bit chunk from tms9902.
443
444    signification:
445    bit 0-7: RBR0-7 Receive Buffer register
446    bit 8: not used (always 0)
447    bit 9: RCVERR Receive Error (RFER | ROVER | RPER)
448    bit 10: RPER Receive Parity Error
449    bit 11: ROVER Receive Overrun Error
450    bit 12: RFER Receive Framing Error
451    bit 13-15: not emulated, normally used for diagnostics
452    bit 16: RBINT (RBRL&RIENB)
453*/
454READ8_MEMBER( tms9902_device::cruread )
455{
456   UINT8 answer = 0;
457
458   offset &= 0x0003;
459
460   switch (offset)
461   {
462   case 3: // Bits 31-24
463      if (m_INT) answer |= 0x80;
464      if (m_LDCTRL || m_LDIR || m_LRDR || m_LXDR || m_BRKON) answer |= 0x40;
465      if (m_DSCH) answer |= 0x20;
466      if (m_CTSin) answer |= 0x10;
467      if (m_DSRin) answer |= 0x08;
468      if (m_RTSout) answer |= 0x04;
469      if (m_TIMELP)  answer |= 0x02;
470      if (m_TIMERR)  answer |= 0x01;
471      break;
472
473   case 2: // Bits 23-16
474      if (m_XSRE) answer |= 0x80;
475      if (m_XBRE) answer |= 0x40;
476      if (m_RBRL) answer |= 0x20;
477      if (m_DSCH && m_DSCENB) answer |= 0x10;
478      if (m_TIMELP && m_TIMENB) answer |= 0x08;
479      if (m_XBRE && m_XBIENB) answer |= 0x02;
480      if (m_RBRL && m_RIENB) answer |= 0x01;
481      break;
482
483   case 1: // Bits 15-8
484      if (m_RIN) answer |= 0x80;
485      if (m_RSBD) answer |= 0x40;
486      if (m_RFBD) answer |= 0x20;
487      if (m_RFER) answer |= 0x10;
488      if (m_ROVER) answer |= 0x08;
489      if (m_RPER) answer |= 0x04;
490      if (m_RPER || m_RFER || m_ROVER) answer |= 0x02;
491      break;
492
493   case 0: // Bits 7-0
494      answer = m_RBR;
495      break;
496   }
497   if (VERBOSE>7) LOG("TMS9902: Reading flag bits %d - %d = %02x\n", ((offset+1)*8-1), offset*8, answer);
498   return answer;
499}
500
501static inline void set_bits8(UINT8 *reg, UINT8 bits, bool set)
502{
503   if (set)
504      *reg |= bits;
505   else
506      *reg &= ~bits;
507}
508
509static inline void set_bits16(UINT16 *reg, UINT16 bits, bool set)
510{
511   if (set)
512      *reg |= bits;
513   else
514      *reg &= ~bits;
515}
516
517void tms9902_device::reset_uart()
518{
519   if (VERBOSE>1) LOG("TMS9902: resetting\n");
520
521   /*  disable all interrupts */
522   m_DSCENB = false;   // Data Set Change Interrupt Enable
523   m_TIMENB = false;   // Timer Interrupt Enable
524   m_XBIENB = false;   // Transmit Buffer Interrupt Enable
525   m_RIENB = false;      // Read Buffer Interrupt Enable
526
527   /* initialize transmitter */
528   m_XBRE = true;      // Transmit Buffer Register Empty
529   m_XSRE = true;      // Transmit Shift Register Empty
530
531   /* initialize receiver */
532   m_RBRL = false;      // Read Buffer Register Loaded
533
534   /* clear RTS */
535   m_RTSON = false;      // Request-to-send on (flag)
536   m_RTSout = true;      // Note we are doing this to ensure the state is sent to the interface
537   set_rts(CLEAR_LINE);
538   m_RTSout = false;   // what we actually want
539
540   /* set all register load flags to 1 */
541   m_LDCTRL = true;
542   m_LDIR = true;
543   m_LRDR = true;
544   m_LXDR = true;
545
546   /* clear break condition */
547   m_BRKON = false;
548   m_BRKout = false;
549
550   m_TMR = 0;
551   m_STOPB = 0;
552   m_RCL = 0;
553   m_XDR = 0;
554   m_RDR = 0;
555   m_RBR = 0;
556   m_XBR = 0;
557   m_XSR = 0;
558
559   field_interrupts();
560}
561
562/*
563    TMS9902 CRU write
564*/
565WRITE8_MEMBER( tms9902_device::cruwrite )
566{
567   data &= 1;   /* clear extra bits */
568
569   offset &= 0x1F;
570   if (VERBOSE>5) LOG("TMS9902: Setting bit %d = %02x\n", offset, data);
571
572   if (offset <= 10)
573   {
574      UINT16 mask = (1 << offset);
575
576      if (m_LDCTRL)
577      {   // Control Register mode. Values written to bits 0-7 are copied
578         // into the control register.
579         switch (offset)
580         {
581         case 0:
582            set_bits8(&m_RCL, 0x01, (data!=0));
583            // we assume that bits are written in increasing order
584            // so we do not transmit the data bits twice
585            // (will fail when bit 1 is written first)
586            break;
587         case 1:
588            set_bits8(&m_RCL, 0x02, (data!=0));
589            set_data_bits();
590            break;
591         case 2:
592            break;
593         case 3:
594            m_CLK4M = (data!=0);
595            break;
596         case 4:
597            m_ODDP = (data!=0);
598            // we also assume that the parity type is set before the parity enable
599            break;
600         case 5:
601            m_PENB = (data!=0);
602            set_parity();
603            break;
604         case 6:
605            set_bits8(&m_STOPB, 0x01, (data!=0));
606            break;
607         case 7:
608            set_bits8(&m_STOPB, 0x02, (data!=0));
609            // When bit 7 is written the control register mode is automatically terminated.
610            m_LDCTRL = false;
611            set_stop_bits();
612            break;
613         default:
614            if (VERBOSE>1) LOG("tms9902: Invalid control register address %d\n", offset);
615         }
616      }
617      else if (m_LDIR)
618      {   // Interval Register mode. Values written to bits 0-7 are copied
619         // into the interval register.
620         if (offset <= 7)
621         {
622            set_bits8(&m_TMR, mask, (data!=0));
623
624            if (offset == 7)
625            {
626               reload_interval_timer();
627               // When bit 7 is written the interval register mode is automatically terminated.
628               m_LDIR = false;
629            }
630         }
631      }
632      else if (m_LRDR || m_LXDR)
633      {
634         if (m_LRDR)
635         {   // Receive rate register mode. Values written to bits 0-10 are copied
636            // into the receive rate register.
637            if (offset < 10)
638            {
639               set_bits16(&m_RDR, mask, (data!=0));
640            }
641            else
642            {
643               // When bit 10 is written the receive register mode is automatically terminated.
644               m_RDV8 = (data!=0);
645               m_LRDR = false;
646               set_receive_data_rate();
647            }
648         }
649         if (m_LXDR)
650         {
651            // The transmit rate register can be set together with the receive rate register.
652            if (offset < 10)
653            {
654               set_bits16(&m_XDR, mask, (data!=0));
655            }
656            else
657            {
658               // Note that the transmit rate register is NOT terminated when
659               // writing bit 10. This must be done by unsetting bit 11.
660               m_XDV8 = (data!=0);
661               set_transmit_data_rate();
662            }
663         }
664      }
665      else
666      {   // LDCTRL=LDIR=LRDR=LXRD=0: Transmit buffer register mode. Values
667         // written to bits 0-7 are transferred into the transmit buffer register.
668         if (offset <= 7)
669         {
670            set_bits8(&m_XBR, mask, (data!=0));
671
672            if (offset == 7)
673            {   /* transmit */
674               m_XBRE = false;
675               // Spec: When the transmitter is active, the contents of the Transmit
676               // Buffer Register are transferred to the Transmit Shift Register
677               // each time the previous character has been completely transmitted
678               // We need to check XSRE=true as well, as the implementation
679               // makes use of a timed transmission, during which XSRE=false
680               if (m_XSRE && m_RTSout && m_CTSin && !m_BRKout)
681               {
682                  initiate_transmit();
683               }
684            }
685         }
686      }
687      return;
688   }
689   switch (offset)
690   {
691      case 11:
692         m_LXDR = (data!=0);
693         break;
694      case 12:
695         m_LRDR = (data!=0);
696         break;
697      case 13:
698         m_LDIR = (data!=0);
699         // Spec: Each time LDIR is reset the contents of the Interval
700         // Register are loaded into the Interval Timer, thus restarting
701         // the timer.
702         if (data==0)
703            reload_interval_timer();
704         break;
705      case 14:
706         m_LDCTRL = (data!=0);
707         break;
708      case 15:
709         m_TSTMD = (data!=0); // Test mode not implemented
710         break;
711      case 16:
712         if (data!=0)
713         {
714            m_RTSON = true;
715            set_rts(ASSERT_LINE);
716            if (m_CTSin)
717            {
718               if (m_XSRE && !m_XBRE && !m_BRKout)
719                  initiate_transmit();
720               else if (m_BRKON)
721                  send_break(true);
722            }
723         }
724         else
725         {
726            m_RTSON = false;
727            if (m_XBRE && m_XSRE && !m_BRKout)
728            {
729               set_rts(CLEAR_LINE);
730            }
731         }
732         return;
733      case 17:
734         if (VERBOSE>3) LOG("TMS9902: set BRKON=%d; BRK=%d\n", data, m_BRKout? 1:0);
735         m_BRKON = (data!=0);
736         if (m_BRKout && data==0)
737         {
738            // clear BRK
739            m_BRKout = false;
740            if ((!m_XBRE) && m_CTSin)
741            {
742               /* transmit next byte */
743               initiate_transmit();
744            }
745            else if (!m_RTSON)
746            {
747               /* clear RTS */
748               set_rts(CLEAR_LINE);
749            }
750         }
751         else if (m_XBRE && m_XSRE && m_RTSout && m_CTSin)
752         {
753            send_break(data!=0);
754         }
755         return;
756      case 18:
757         // Receiver Interrupt Enable
758         // According to spec, (re)setting this flag clears the RBRL flag
759         // (the only way to clear the flag!)
760         m_RIENB = (data!=0);
761         m_RBRL = false;
762         if (VERBOSE>4) LOG("TMS9902: set RBRL=0, set RIENB=%d\n", data);
763         field_interrupts();
764         return;
765      case 19:
766         /* Transmit Buffer Interrupt Enable */
767         m_XBIENB = (data!=0);
768         if (VERBOSE>4) LOG("TMS9902: set XBIENB=%d\n", data);
769         field_interrupts();
770         return;
771      case 20:
772         /* Timer Interrupt Enable */
773         m_TIMENB = (data!=0);
774         m_TIMELP = false;
775         m_TIMERR = false;
776         field_interrupts();
777         return;
778      case 21:
779         /* Data Set Change Interrupt Enable */
780         m_DSCENB = (data!=0);
781         m_DSCH = false;
782         if (VERBOSE>4) LOG("TMS9902: set DSCH=0, set DSCENB=%d\n", data);
783         field_interrupts();
784         return;
785      case 31:
786         /* RESET */
787         reset_uart();
788         return;
789      default:
790         if (VERBOSE>1) LOG("TMS9902: Writing to undefined flag bit position %d = %01x\n", offset, data);
791   }
792}
793
794/*-------------------------------------------------
795    DEVICE_STOP( tms9902 )
796-------------------------------------------------*/
797
798void tms9902_device::device_stop()
799{
800   if (m_dectimer)
801   {
802      m_dectimer->reset();
803      m_dectimer = NULL;
804   }
805}
806
807/*-------------------------------------------------
808    DEVICE_RESET( tms9902 )
809-------------------------------------------------*/
810
811void tms9902_device::device_reset()
812{
813   reset_uart();
814}
815
816/*-------------------------------------------------
817    DEVICE_START( tms9902 )
818-------------------------------------------------*/
819
820void tms9902_device::device_start()
821{
822   const tms9902_interface *intf = reinterpret_cast<const tms9902_interface *>(static_config());
823
824   m_clock_rate = clock();
825
826   int_callback.resolve(intf->int_callback, *this);
827   rcv_callback.resolve(intf->rcv_callback, *this);
828   xmit_callback.resolve(intf->xmit_callback, *this);
829   ctrl_callback.resolve(intf->ctrl_callback, *this);
830
831   m_dectimer = timer_alloc(DECTIMER);
832   m_recvtimer = timer_alloc(RECVTIMER);
833   m_sendtimer = timer_alloc(SENDTIMER);
834}
835
836const device_type TMS9902 = &device_creator<tms9902_device>;
837
src/mess/machine/tms9902.h
r15652r15653
1/****************************************************************************
2
3    TMS9902 Asynchronous Communication Controller
4    See tms9902.c for documentation
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
12#ifndef __TMS9902_H__
13#define __TMS9902_H__
14
15#include "emu.h"
16
17// Serial control protocol values
18#define TYPE_TMS9902 0x01
19
20// Configuration (output only)
21#define CONFIG   0x80
22#define RATERECV 0x70
23#define RATEXMIT 0x60
24#define DATABITS 0x50
25#define STOPBITS 0x40
26#define PARITY   0x30
27
28// Exceptional states (BRK: both directions; FRMERR/PARERR: input only)
29#define EXCEPT 0x40
30#define BRK    0x02
31#define FRMERR 0x04
32#define PARERR 0x06
33
34// Line states (RTS, DTR: output; CTS, DSR, RI, DCD: input)
35#define LINES 0x00
36#define RTS 0x20
37#define CTS 0x10
38#define DSR 0x08
39#define DCD 0x04
40#define DTR 0x02
41#define RI  0x01
42
43extern const device_type TMS9902;
44
45typedef struct _tms9902_interface
46{
47   devcb_write_line   int_callback;
48   devcb_write_line   rcv_callback;
49   devcb_write8      xmit_callback;
50   devcb_write8      ctrl_callback;
51} tms9902_interface;
52
53class tms9902_device : public device_t
54{
55public:
56   tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
57   void   set_clock(bool state);
58
59   void   rcv_cts(line_state state);
60   void   rcv_dsr(line_state state);
61   void   rcv_data(UINT8 data);
62   void   rcv_break(bool value);
63   void   rcv_framing_error();
64   void   rcv_parity_error();
65
66   double   get_baudpoll();
67
68   int      get_config_value();
69
70   DECLARE_READ8_MEMBER( cruread );
71   DECLARE_WRITE8_MEMBER( cruwrite );
72
73protected:
74   void   device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void   device_start();
76   void   device_reset();
77   void   device_stop();
78
79private:
80   void   field_interrupts();
81   void   reload_interval_timer();
82   void   send_break(bool state);
83   void   set_receive_data_rate();
84   void   set_transmit_data_rate();
85   void   set_stop_bits();
86   void   set_data_bits();
87   void   set_parity();
88   void   transmit_line_state();
89   void   set_rts(line_state state);
90   void   initiate_transmit();
91   void   reset_uart();
92
93   devcb_resolved_write_line      int_callback;
94   devcb_resolved_write_line      rcv_callback;
95   devcb_resolved_write8         xmit_callback;
96   devcb_resolved_write8         ctrl_callback;      // needs to be used with get_config_value
97
98   // tms9902 clock rate (PHI* pin, normally connected to TMS9900 Phi3*)
99   // Official range is 2MHz-3.3MHz.  Some tms9902s were sold as "MP9214", and
100   // were tested for speeds up to 4MHz, provided the clk4m control bit is set.
101   // (warning: 3MHz on a tms9900 is equivalent to 12MHz on a tms9995 or tms99000)
102   double   m_clock_rate;
103
104   /* Modes */
105   bool   m_LDCTRL;      // Load control register
106   bool   m_LDIR;         // Load interval register
107   bool   m_LRDR;         // Load receive data register
108   bool   m_LXDR;         // Load transmit data register
109   bool   m_TSTMD;         // Test mode
110
111   /* output pin */
112   bool   m_RTSON;         // RTS-on request
113
114   /* transmitter registers */
115   bool   m_BRKON;         // BRK-on request
116   bool   m_BRKout;      // indicates the current BRK state
117
118   UINT8   m_XBR;         // transmit buffer register
119   UINT8   m_XSR;         // transmit shift register
120
121   /* receiver registers */
122   UINT8   m_RBR;         // Receive buffer register
123
124   /* Interrupt enable flags */
125   bool   m_DSCENB;      // Data set change interrupt enable
126   bool   m_RIENB;         // Receiver interrupt enable
127   bool   m_XBIENB;      // Tansmit buffer interrupt enable
128   bool   m_TIMENB;      // Timer interrupt enable
129
130   /*
131        Rate registers. The receive bit rate calculates as
132        bitrate = clock1 / (2 * (8 ^ RDV8) * RDR)
133        (similarly for transmit)
134
135        where clock1 = clock_rate / (CLK4M? 4:3)
136    */
137   UINT16   m_RDR;         // Receive data rate
138   bool   m_RDV8;         // Receive data rate divider
139   UINT16   m_XDR;         // Transmit data rate
140   bool   m_XDV8;         // Transmit data rate divider
141
142   /* Status flags */
143   bool   m_INT;         // mirrors /INT output line, inverted
144   bool   m_DSCH;         // Data set status change
145
146   bool   m_CTSin;         // Inverted /CTS input (i.e. CTS)
147   bool   m_DSRin;         // Inverted /DSR input (i.e. DSR)
148   bool   m_RTSout;      // Current inverted /RTS line state (i.e. RTS)
149
150   bool   m_TIMELP;      // Timer elapsed
151   bool   m_TIMERR;      // Timer error
152
153   bool   m_XSRE;         // Transmit shift register empty
154   bool   m_XBRE;         // Transmit buffer register empty
155   bool   m_RBRL;         // Receive buffer register loaded
156
157   bool   m_RIN;         // State of the RIN pin
158   bool   m_RSBD;         // Receive start bit detect
159   bool   m_RFBD;         // Receive full bit detect
160   bool   m_RFER;         // Receive framing error
161   bool   m_ROVER;         // Receiver overflow
162   bool   m_RPER;         // Receive parity error
163
164   UINT8   m_RCL;         // Character length
165   bool   m_ODDP;
166   bool   m_PENB;
167   UINT8   m_STOPB;
168   bool   m_CLK4M;      // /PHI input divide select
169
170   UINT8   m_TMR;      /* interval timer */
171
172   /* clock registers */
173   emu_timer *m_dectimer;         /* MESS timer, used to emulate the decrementer register */
174   emu_timer *m_recvtimer;
175   emu_timer *m_sendtimer;
176
177   // This value is the ratio of data input versus the poll rate. The
178   // data source should deliver data bytes at every 1/baudpoll call.
179   // This is to ensure that data is delivered at a rate that is expected
180   // from the emulated program.
181   double   m_baudpoll;
182
183   // Caches the last configuration setting (used with the ctrl_callback)
184   int      m_last_config_value;
185};
186
187/***************************************************************************
188    DEVICE CONFIGURATION MACROS
189***************************************************************************/
190
191#define MCFG_TMS9902_ADD(_tag, _intrf, _clock) \
192   MCFG_DEVICE_ADD(_tag, TMS9902, _clock) \
193   MCFG_DEVICE_CONFIG(_intrf)
194
195#endif /* __TMS9902_H__ */
src/mess/mess.mak
r15652r15653
17101710   $(MESS_VIDEO)/t6a04.o      \
17111711   $(MESS_MACHINE)/ti85.o      \
17121712   $(MESS_DRIVERS)/ti89.o      \
1713   $(MESS_MACHINE)/tms9901.o   \
1714   $(MESS_MACHINE)/tms9902.o   \
17151713   $(MESS_MACHINE)/990_hd.o   \
17161714   $(MESS_MACHINE)/990_tap.o   \
17171715   $(MESS_MACHINE)/ti990.o      \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team