Previous 15994 Revisions Next

r1072 Friday 5th October, 2007 at 13:45:01 UTC by Curt Coder
[MFP68901] Added preliminary serial I/O
[src/mess/machine]68901mfp.c 68901mfp.h

src/mess/machine/68901mfp.c
r1071r1072
22
33   TODO:
44
5   - serial I/O
65   - daisy chaining
76   - correct irq_timer period
7   
8   - divide serial clock by 16
9   - synchronous mode
10   - 1.5/2 stop bits
11   - interrupt on receiver break end
12   - interrupt on character boundaries during break transmission
13   - loopback mode
14   - transmit parity bit
815
916*/
1017
r1071r1072
3239   UINT8 ucr;
3340   UINT8 udr;
3441   UINT8 scr;
35   UINT8 tsr, rsr, next_rsr;
42   UINT8 tsr, rsr, next_rsr, rsr_read;
43   UINT8 rx_buffer, tx_buffer;
3644   int   rx_bits, tx_bits;
3745   int   rx_parity, tx_parity;
38   int rsr_read;
46   int   rx_state, tx_state, xmit_state;
47   int rxtx_word, rxtx_start, rxtx_stop;
3948   mame_timer *rx_timer, *tx_timer;
4049} mfp_68901;
4150
r1071r1072
116125   mfp68901_irq_ack(param);
117126}
118127
128/* USART */
129
130static inline void mfp68901_rx_buffer_full(int which)
131{
132   mfp_68901 *mfp_p = &mfp[which];
133
134   if (mfp_p->ier & MFP68901_IR_RCV_BUFFER_FULL)
135   {
136      mfp_p->ipr |= MFP68901_IR_RCV_BUFFER_FULL;
137   }
138}
139
140static inline void mfp68901_rx_error(int which)
141{
142   mfp_68901 *mfp_p = &mfp[which];
143
144   if (mfp_p->ier & MFP68901_IR_RCV_ERROR)
145   {
146      mfp_p->ipr |= MFP68901_IR_RCV_ERROR;
147   }
148   else
149   {
150      mfp68901_rx_buffer_full(which);
151   }
152}
153
154static inline void mfp68901_tx_buffer_empty(int which)
155{
156   mfp_68901 *mfp_p = &mfp[which];
157
158   if (mfp_p->ier & MFP68901_IR_XMIT_BUFFER_EMPTY)
159   {
160      mfp_p->ipr |= MFP68901_IR_XMIT_BUFFER_EMPTY;
161   }
162}
163
164static inline void mfp68901_tx_error(int which)
165{
166   mfp_68901 *mfp_p = &mfp[which];
167
168   if (mfp_p->ier & MFP68901_IR_XMIT_ERROR)
169   {
170      mfp_p->ipr |= MFP68901_IR_XMIT_ERROR;
171   }
172   else
173   {
174      mfp68901_tx_buffer_empty(which);
175   }
176}
177
178static inline int mfp68901_parity(UINT8 b)
179{
180   b ^= b >> 4;
181   b ^= b >> 2;
182   b ^= b >> 1;
183   return b & 1;
184}
185
186static TIMER_CALLBACK( rx_tick )
187{
188   mfp_68901 *mfp_p = &mfp[param];
189
190   if (mfp_p->rsr & MFP68901_RSR_RCV_ENABLE)
191   {
192      switch (mfp_p->rx_state)
193      {
194      case MFP68901_SERIAL_START:
195         if (mfp_p->intf->rx_pin == 0)
196         {
197            mfp_p->rsr |= MFP68901_RSR_CHAR_IN_PROGRESS;
198            mfp_p->rx_bits = 0;
199            mfp_p->rx_buffer = 0;
200            mfp_p->rx_state = MFP68901_SERIAL_DATA;
201            mfp_p->next_rsr = MFP68901_RSR_BREAK;
202         }
203         break;
204
205      case MFP68901_SERIAL_DATA:
206         if ((mfp_p->next_rsr & MFP68901_RSR_BREAK) && (*mfp_p->intf->rx_pin == 1) && mfp_p->rsr_read)
207         {
208            mfp_p->next_rsr &= ~MFP68901_RSR_BREAK;
209         }
210
211         mfp_p->rx_buffer >>= 1;
212         mfp_p->rx_buffer |= (*mfp_p->intf->rx_pin << 7);
213         mfp_p->rx_bits++;
214
215         if (mfp_p->rx_bits == mfp_p->rxtx_word)
216         {
217            if (mfp_p->rxtx_word < 8)
218            {
219               mfp_p->rx_buffer >>= (8 - mfp_p->rxtx_word);
220            }
221
222            mfp_p->rsr &= ~MFP68901_RSR_CHAR_IN_PROGRESS;
223
224            if (mfp_p->ucr & MFP68901_UCR_PARITY_ENABLED)
225            {
226               mfp_p->rx_state = MFP68901_SERIAL_PARITY;
227            }
228            else
229            {
230               mfp_p->rx_state = MFP68901_SERIAL_STOP;
231            }
232         }
233         break;
234
235      case MFP68901_SERIAL_PARITY:
236         mfp_p->rx_parity = *mfp_p->intf->rx_pin;
237
238         if (mfp_p->rx_parity != (mfp68901_parity(mfp_p->rx_buffer) ^ ((mfp_p->ucr & MFP68901_UCR_PARITY_EVEN) >> 1)))
239         {
240            mfp_p->next_rsr |= MFP68901_RSR_PARITY_ERROR;
241         }
242
243         mfp_p->rx_state = MFP68901_SERIAL_STOP;
244         break;
245
246      case MFP68901_SERIAL_STOP:
247         if (*mfp_p->intf->rx_pin == 1)
248         {
249            if (!((mfp_p->rsr & MFP68901_RSR_SYNC_STRIP_ENABLE) && (mfp_p->rx_buffer == mfp_p->scr)))
250            {
251               if (!(mfp_p->rsr & MFP68901_RSR_OVERRUN_ERROR))
252               {
253                  if (mfp_p->rsr & MFP68901_RSR_BUFFER_FULL)
254                  {
255                     // incoming word received but last word in receive buffer has not been read
256                     mfp_p->next_rsr |= MFP68901_RSR_OVERRUN_ERROR;
257                  }
258                  else
259                  {
260                     // incoming word received and receive buffer is empty
261                     mfp_p->rsr |= MFP68901_RSR_BUFFER_FULL;
262                     mfp_p->udr = mfp_p->rx_buffer;
263                     mfp68901_rx_buffer_full(param);
264                  }
265               }
266            }
267         }
268         else
269         {
270            if (mfp_p->rx_buffer)
271            {
272               // non-zero data word not followed by a stop bit
273               mfp_p->next_rsr |= MFP68901_RSR_FRAME_ERROR;
274            }
275         }
276
277         mfp_p->rx_state = MFP68901_SERIAL_START;
278         break;
279      }
280   }
281}
282
283static inline void mfp68901_check_xe(int which)
284{
285   mfp_68901 *mfp_p = &mfp[which];
286
287   if (!(mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE))
288   {
289      if (mfp_p->tsr & MFP68901_TSR_AUTO_TURNAROUND)
290      {
291         mfp_p->tsr |= MFP68901_TSR_XMIT_ENABLE;
292      }
293      else
294      {
295         mfp_p->xmit_state = MFP68901_XMIT_OFF;
296         mfp_p->tsr |= MFP68901_TSR_END_OF_XMIT;
297         mfp68901_tx_error(which);
298      }
299   }
300}
301
302static void mpf68901_transmit_disabled(int which)
303{
304   mfp_68901 *mfp_p = &mfp[which];
305
306   switch (mfp_p->tsr & MFP68901_TSR_OUTPUT_MASK)
307   {
308   case MFP68901_TSR_OUTPUT_HI_Z:
309      // indeterminate
310   case MFP68901_TSR_OUTPUT_LOW:
311      *mfp_p->intf->tx_pin = 0;
312      break;
313
314   case MFP68901_TSR_OUTPUT_HIGH:
315   case MFP68901_TSR_OUTPUT_LOOP:
316      *mfp_p->intf->tx_pin = 1;
317      break;
318   }
319}
320
321static void mpf68901_transmit(int which)
322{
323   mfp_68901 *mfp_p = &mfp[which];
324
325   switch (mfp_p->tx_state)
326   {
327   case MFP68901_SERIAL_START:
328      if (mfp_p->tsr & MFP68901_TSR_UNDERRUN_ERROR)
329      {
330         if (mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE)
331         {
332            *mfp_p->intf->tx_pin = 1;
333         }
334         else
335         {
336            mpf68901_transmit_disabled(which);
337         }
338      }
339      else
340      {
341         if (mfp_p->tsr & MFP68901_TSR_BUFFER_EMPTY)
342         {
343            mfp_p->tsr |= MFP68901_TSR_UNDERRUN_ERROR;
344
345            if (mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE)
346            {
347               *mfp_p->intf->tx_pin = 1;
348            }
349            else
350            {
351               mpf68901_transmit_disabled(which);
352            }
353         }
354         else
355         {
356            *mfp_p->intf->tx_pin = 0;
357            mfp_p->tx_buffer = mfp_p->udr;
358            mfp_p->tx_bits = 0;
359            mfp_p->tx_state = MFP68901_SERIAL_DATA;
360            mfp_p->tsr |= MFP68901_TSR_BUFFER_EMPTY;
361            mfp68901_tx_buffer_empty(which);
362         }
363      }
364      break;
365
366   case MFP68901_SERIAL_DATA:
367      *mfp_p->intf->tx_pin = mfp_p->tx_buffer & 0x01;
368      mfp_p->tx_buffer >>= 1;
369      mfp_p->tx_bits++;
370
371      if (mfp_p->tx_bits == mfp_p->rxtx_word)
372      {
373         if (mfp_p->ucr & MFP68901_UCR_PARITY_ENABLED)
374         {
375            mfp_p->tx_state = MFP68901_SERIAL_PARITY;
376         }
377         else
378         {
379            mfp_p->tx_state = MFP68901_SERIAL_STOP;
380         }
381      }
382      break;
383
384   case MFP68901_SERIAL_PARITY:
385      *mfp_p->intf->tx_pin = 0;
386      mfp_p->tx_state = MFP68901_SERIAL_STOP;
387      break;
388
389   case MFP68901_SERIAL_STOP:
390      *mfp_p->intf->tx_pin = 1;
391
392      if (mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE)
393      {
394         mfp_p->tx_state = MFP68901_SERIAL_START;
395      }
396      else
397      {
398         if (mfp_p->tsr & MFP68901_TSR_AUTO_TURNAROUND)
399         {
400            mfp_p->tsr |= MFP68901_TSR_XMIT_ENABLE;
401            mfp_p->tx_state = MFP68901_SERIAL_START;
402         }
403         else
404         {
405            mfp_p->xmit_state = MFP68901_XMIT_OFF;
406            mfp_p->tsr |= MFP68901_TSR_END_OF_XMIT;
407            mfp68901_tx_error(which);
408         }
409      }
410      break;
411   }
412}
413
414static TIMER_CALLBACK( tx_tick )
415{
416   mfp_68901 *mfp_p = &mfp[param];
417
418   switch (mfp_p->xmit_state)
419   {
420   case MFP68901_XMIT_OFF:
421      mpf68901_transmit_disabled(param);
422      break;
423
424   case MFP68901_XMIT_STARTING:
425      if (mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE)
426      {
427         *mfp_p->intf->tx_pin = 1;
428         mfp_p->xmit_state = MFP68901_XMIT_ON;
429      }
430      else
431      {
432         mfp_p->xmit_state = MFP68901_XMIT_OFF;
433         mfp_p->tsr |= MFP68901_TSR_END_OF_XMIT;
434      }
435      break;
436
437   case MFP68901_XMIT_BREAK:
438      if (mfp_p->tsr & MFP68901_TSR_XMIT_ENABLE)
439      {
440         if (mfp_p->tsr & MFP68901_TSR_BREAK)
441         {
442            *mfp_p->intf->tx_pin = 1;
443         }
444         else
445         {
446            mfp_p->xmit_state = MFP68901_XMIT_ON;
447         }
448      }
449      else
450      {
451         mfp_p->xmit_state = MFP68901_XMIT_OFF;
452         mfp_p->tsr |= MFP68901_TSR_END_OF_XMIT;
453      }
454      break;
455
456   case MFP68901_XMIT_ON:
457      mpf68901_transmit(param);
458      break;
459   }
460}
461
119462static UINT8 mfp68901_register_r(int which, int reg)
120463{
121464   mfp_68901 *mfp_p = &mfp[which];
r1071r1072
146489
147490   case MFP68901_REGISTER_SCR:   return mfp_p->scr;
148491   case MFP68901_REGISTER_UCR:   return mfp_p->ucr;
149   case MFP68901_REGISTER_RSR:   return mfp_p->rsr;
150   case MFP68901_REGISTER_TSR:   return mfp_p->tsr;
151   case MFP68901_REGISTER_UDR:   return mfp_p->udr;
492   case MFP68901_REGISTER_RSR:
493      mfp_p->rsr_read = 1;
494      return mfp_p->rsr;
152495
496   case MFP68901_REGISTER_TSR:
497      {
498         // clear UE bit (in reality, this won't be cleared until one full clock cycle of the transmitter has passed since the bit was set)
499
500         UINT8 tsr = mfp_p->tsr;
501         mfp_p->tsr &= 0xbf;
502
503         return tsr;
504      }
505
506   case MFP68901_REGISTER_UDR:
507      // load RSR with latched value
508
509      mfp_p->rsr = (mfp_p->next_rsr & 0x7c) | (mfp_p->rsr & 0x03);
510      mfp_p->next_rsr = 0;
511
512      // signal receiver error interrupt
513
514      if (mfp_p->rsr & 0x78)
515      {
516         mfp68901_rx_error(which);
517      }
518
519      return mfp_p->udr;
520
153521   default:                 return 0;
154522   }
155523}
r1071r1072
449817      }
450818      break;
451819
452   // USART not implemented
453820   case MFP68901_REGISTER_SCR:
454821      logerror("MFP68901 #%u Sync Character : %x\n", which, data);
455822
r1071r1072
472839      switch (data & 0x60)
473840      {
474841      case MFP68901_UCR_WORD_LENGTH_8:
842         mfp_p->rxtx_word = 8;
475843         logerror("MFP68901 #%u Word Length : 8 bits\n", which);
476844         break;
477845      case MFP68901_UCR_WORD_LENGTH_7:
846         mfp_p->rxtx_word = 7;
478847         logerror("MFP68901 #%u Word Length : 7 bits\n", which);
479848         break;
480849      case MFP68901_UCR_WORD_LENGTH_6:
850         mfp_p->rxtx_word = 6;
481851         logerror("MFP68901 #%u Word Length : 6 bits\n", which);
482852         break;
483853      case MFP68901_UCR_WORD_LENGTH_5:
854         mfp_p->rxtx_word = 5;
484855         logerror("MFP68901 #%u Word Length : 5 bits\n", which);
485856         break;
486857      }
r1071r1072
488859      switch (data & 0x18)
489860      {
490861      case MFP68901_UCR_START_STOP_0_0:
862         mfp_p->rxtx_start = 0;
863         mfp_p->rxtx_stop = 0;
491864         logerror("MFP68901 #%u Start Bits : 0, Stop Bits : 0, Format : synchronous\n", which);
492865         break;
493866      case MFP68901_UCR_START_STOP_1_1:
867         mfp_p->rxtx_start = 1;
868         mfp_p->rxtx_stop = 1;
494869         logerror("MFP68901 #%u Start Bits : 1, Stop Bits : 1, Format : asynchronous\n", which);
495870         break;
496871      case MFP68901_UCR_START_STOP_1_15:
872         mfp_p->rxtx_start = 1;
873         mfp_p->rxtx_stop = 1;
497874         logerror("MFP68901 #%u Start Bits : 1, Stop Bits : 1½, Format : asynchronous\n", which);
498875         break;
499876      case MFP68901_UCR_START_STOP_1_2:
877         mfp_p->rxtx_start = 1;
878         mfp_p->rxtx_stop = 2;
500879         logerror("MFP68901 #%u Start Bits : 1, Stop Bits : 2, Format : asynchronous\n", which);
501880         break;
502881      }
r1071r1072
569948            logerror("MFP68901 #%u Transmitter Auto Turnaround Disabled\n", which);
570949
571950         mfp_p->tsr = data & 0x2f;
572         mfp_p->tsr |= 0x80;  // x68000 expects the buffer to be empty, so this will do for now
951         mfp_p->tsr |= MFP68901_TSR_BUFFER_EMPTY;  // x68000 expects the buffer to be empty, so this will do for now
573952      }
574953      break;
575954
576955   case MFP68901_REGISTER_UDR:
577956      logerror("MFP68901 UDR %x\n", data);
578957      mfp_p->udr = data;
958      //mfp_p->tsr &= ~MFP68901_TSR_BUFFER_EMPTY;
579959      break;
580960   }
581961}
r1071r1072
8281208   mfp68901_timer_count_d(param);
8291209}
8301210
831static TIMER_CALLBACK( rx_tick )
832{
833   // TODO
834}
835
836static TIMER_CALLBACK( tx_tick )
837{
838   // TODO
839}
840
8411211void mfp68901_config(int which, const mfp68901_interface *intf)
8421212{
8431213   mfp_68901 *mfp_p = &mfp[which];
src/mess/machine/68901mfp.h
r1071r1072
8080   MFP68901_MAX_TIMERS
8181};
8282
83enum
84{
85   MFP68901_SERIAL_START = 0,
86   MFP68901_SERIAL_DATA,
87   MFP68901_SERIAL_PARITY,
88   MFP68901_SERIAL_STOP
89};
90
91enum
92{
93   MFP68901_XMIT_OFF = 0,
94   MFP68901_XMIT_STARTING,
95   MFP68901_XMIT_ON,
96   MFP68901_XMIT_BREAK,
97   MFP68901_XMIT_STOPPING
98};
99
83100#define MFP68901_TAO_LOOPBACK         -1
84101#define MFP68901_TBO_LOOPBACK         -2
85102#define MFP68901_TCO_LOOPBACK         -3
r1071r1072
147164
148165#define MFP68901_RSR_RCV_ENABLE         0x01
149166#define MFP68901_RSR_SYNC_STRIP_ENABLE   0x02
150#define MFP68901_RSR_MCIP            0x04
167#define MFP68901_RSR_MATCH            0x04
168#define MFP68901_RSR_CHAR_IN_PROGRESS   0x04
151169#define MFP68901_RSR_FOUND_SEARCH      0x08
170#define MFP68901_RSR_BREAK            0x08
152171#define MFP68901_RSR_FRAME_ERROR      0x10
153172#define MFP68901_RSR_PARITY_ERROR      0x20
154173#define MFP68901_RSR_OVERRUN_ERROR      0x40
r1071r1072
159178#define MFP68901_TSR_OUTPUT_LOW         0x02
160179#define MFP68901_TSR_OUTPUT_HIGH      0x04
161180#define MFP68901_TSR_OUTPUT_LOOP      0x06
181#define MFP68901_TSR_OUTPUT_MASK      0x06
162182#define MFP68901_TSR_BREAK            0x08
163183#define MFP68901_TSR_END_OF_XMIT      0x10
164184#define MFP68901_TSR_AUTO_TURNAROUND   0x20

Previous 15994 Revisions Next


© 1998-2018 The MESS Team