Previous 15994 Revisions Next

r5572 Friday 4th September, 2009 at 02:45:52 UTC by Jonathan Gevaryahu
Add loopback mode and counter/timer modes 3 and 6 to 68681 DUART, self-tests now pass in dectalk [Lord Nightmare]
[src/emu/machine]68681.c

src/emu/machine/68681.c
r5571r5572
22    68681 DUART
33
44    Written by Mariusz Wojcieszek
5    Updated by Jonathan Gevaryahu AKA Lord Nightmare
56*/
67
78#include "driver.h"
r5571r5572
3839#define STATUS_FIFO_FULL         0x02
3940#define STATUS_RECEIVER_READY      0x01
4041
41#define MODE_RX_INT_SELECT_BIT      6
42#define MODE_RX_INT_SELECT_BIT      0x40
4243
4344#define RX_FIFO_SIZE            3
4445
r5571r5572
109110
110111static void duart68681_update_interrupts(duart68681_state *duart68681)
111112{
113   /* update SR state and update interrupt ISR state for the following bits:
114   SRn: bits 7-4: handled elsewhere.
115   SRn: bit 3 (TxEMTn) (we can assume since we're not actually emulating the delay/timing of sending bits, that as long as TxRDYn is set, TxEMTn is also set since the transmit byte has 'already happened', therefore TxEMTn is always 1 assuming tx is enabled on channel n and the MSR2n mode is 0 or 2; in mode 1 it is explicitly zeroed, and mode 3 is undefined)
116   SRn: bit 2 (TxRDYn) (we COULD assume since we're not emulating delay and timing output, that as long as tx is enabled on channel n, TxRDY is 1 for channel n and the MSR2n mode is 0 or 2; in mode 1 it is explicitly zeroed, and mode 3 is undefined; however, tx_ready is already nicely handled for us elsewhere, so we can use that instead for now, though we may need to retool that code as well)
117   SRn: bit 1 (FFULLn) (this bit we actually emulate; if the recieve fifo for channel n is full, this bit is 1, otherwise it is 0. the recieve fifo should be three words long.)
118   SRn: bit 0 (RxRDYn) (this bit we also emulate; the bit is always asserted if the recieve fifo is not empty)
119   ISR: bit 7: Input Port change; this should be handled elsewhere, on the input port handler
120   ISR: bit 6: Delta Break B; this should be handled elsewhere, on the data recieve handler
121   ISR: bit 5: RxRDYB/FFULLB: this is handled here; depending on whether MSR1B bit 6 is 0 or 1, this bit holds the state of SRB bit 0 or bit 1 respectively
122   ISR: bit 4: TxRDYB: this is handled here; it mirrors SRB bit 2
123   ISR: bit 3: Counter ready; this should be handled by the timer generator
124   ISR: bit 2: Delta Break A; this should be handled elsewhere, on the data recieve handler
125   ISR: bit 1: RxRDYA/FFULLA: this is handled here; depending on whether MSR1A bit 6 is 0 or 1, this bit holds the state of SRA bit 0 or bit 1 respectively
126   ISR: bit 0: TxRDYA: this is handled here; it mirrors SRA bit 2
127   */
128   UINT8 ch = 0;
129   //logerror("DEBUG: 68681 int check: upon func call, SRA is %02X, SRB is %02X, ISR is %02X\n", duart68681->channel[0].SR, duart68681->channel[1].SR, duart68681->ISR);
130   for (ch = 0; ch < 2; ch++)
131   {
132      //if ( duart68681->channel[ch].rx_enabled )
133      //{
134         if ( duart68681->channel[ch].rx_fifo_num > 0 )
135         {
136            duart68681->channel[ch].SR |= STATUS_RECEIVER_READY;
137         }
138         else
139         {
140            duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY;
141         }
142         if ( duart68681->channel[ch].rx_fifo_num == RX_FIFO_SIZE )
143         {
144            duart68681->channel[ch].SR |= STATUS_FIFO_FULL;
145         }
146         else
147         {
148            duart68681->channel[ch].SR &= ~STATUS_FIFO_FULL;
149         }
150      //}
151      //else
152      //{
153      //duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY;
154      //duart68681->channel[ch].SR &= ~STATUS_FIFO_FULL;
155      //}
156      // Handle the TxEMT and TxRDY bits based on mode
157      switch( duart68681->channel[ch].MR2&0xC0) // what mode are we in?
158         {
159         case 0x00: // normal mode
160            if ( duart68681->channel[ch].tx_enabled )
161            {
162               duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY;
163            }
164            else
165            {
166               duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY;
167            }
168         break;
169         case 0x40: // automatic echo mode
170            duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY;
171            duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY;
172         break;
173         case 0x80: // local loopback mode
174            if ( duart68681->channel[ch].tx_enabled )
175            {
176               duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY;
177            }
178            else
179            {
180               duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY;
181            }
182         break;
183         case 0xC0: // remote loopback mode
184            // write me, what the txrdy/txemt regs do for remote loopback mode is undocumented afaik, for now just clear both
185            duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY;
186            duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY;
187         break;
188         }
189      // now handle the ISR bits
190      if ( duart68681->channel[ch].SR & STATUS_TRANSMITTER_READY )
191      {
192         if (ch == 0)
193            duart68681->ISR |= INT_TXRDYA;
194         else
195            duart68681->ISR |= INT_TXRDYB;
196      }
197      else
198      {
199         if (ch == 0)
200            duart68681->ISR &= ~INT_TXRDYA;
201         else
202            duart68681->ISR &= ~INT_TXRDYB;
203      }
204      //logerror("DEBUG: 68681 int check: before reciever test, SR%c is %02X, ISR is %02X\n", (ch+0x41), duart68681->channel[ch].SR, duart68681->ISR);
205      if ( duart68681->channel[ch].MR1 & MODE_RX_INT_SELECT_BIT )
206      {
207         if ( duart68681->channel[ch].SR & STATUS_FIFO_FULL )
208         {
209            duart68681->ISR |= ((ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB);
210         }
211         else
212         {
213            duart68681->ISR &= ((ch == 0) ? ~INT_RXRDY_FFULLA : ~INT_RXRDY_FFULLB);
214         }
215      }
216      else
217      {
218         if ( duart68681->channel[ch].SR & STATUS_RECEIVER_READY )
219         {
220            duart68681->ISR |= ((ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB);
221         }
222         else
223         {
224            duart68681->ISR &= ((ch == 0) ? ~INT_RXRDY_FFULLA : ~INT_RXRDY_FFULLB);
225         }
226      }
227      //logerror("DEBUG: 68681 int check: after reciever test, SR%c is %02X, ISR is %02X\n", (ch+0x41), duart68681->channel[ch].SR, duart68681->ISR);
228   }
112229   if ( (duart68681->ISR & duart68681->IMR) != 0 )
113230   {
114231      if ( duart68681->duart_config->irq_handler )
115232      {
233         LOG(( "68681: Interrupt line active (IMR & ISR = %02X)\n", (duart68681->ISR & duart68681->IMR) ));
116234         duart68681->duart_config->irq_handler( duart68681->device, duart68681->IVR );
117235      }
118236   }
r5571r5572
138256   {
139257      duart68681->channel[ch].MR2 = data;
140258   }
259   duart68681_update_interrupts(duart68681);
141260};
142261
143262static void duart68681_write_CSR(duart68681_state *duart68681, int ch, UINT8 data, UINT8 ACR)
r5571r5572
171290   if ( BIT(data,1) )
172291   {
173292      duart68681->channel[ch].rx_enabled = 0;
174      duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY;
293      duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY; // datasheet explicitly says disabling rx does not affect the reciever ready line, which just indicates if the fifo has anything in it at all.
175294   }
176295
177296   if ( BIT(data,2) )
r5571r5572
179298      duart68681->channel[ch].tx_enabled = 1;
180299      duart68681->channel[ch].tx_ready = 1;
181300      duart68681->channel[ch].SR |= STATUS_TRANSMITTER_READY;
301      duart68681->channel[ch].SR |= STATUS_TRANSMITTER_EMPTY;
182302   }
183303   if ( BIT(data,3) )
184304   {
185305      duart68681->channel[ch].tx_enabled = 0;
186306      duart68681->channel[ch].tx_ready = 0;
187307      duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_READY;
308      duart68681->channel[ch].SR &= ~STATUS_TRANSMITTER_EMPTY;
188309   }
189310
190311   switch( (data >> 4) & 0x07 )
r5571r5572
197318      case 2: /* Reset channel A receiver (disable receiver and flush fifo) */
198319         duart68681->channel[ch].rx_enabled = 0;
199320         duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY;
321         duart68681->channel[ch].SR &= ~STATUS_OVERRUN_ERROR; // is this correct?
200322         duart68681->channel[ch].rx_fifo_read_ptr = 0;
201323         duart68681->channel[ch].rx_fifo_write_ptr = 0;
202324         duart68681->channel[ch].rx_fifo_num = 0;
r5571r5572
218340         {
219341            duart68681->ISR &= ~INT_DELTA_BREAK_B;
220342         }
221         duart68681_update_interrupts(duart68681);
222343         break;
344      /* TODO: case 6 and case 7 are start break and stop break respectively, which start or stop holding the TxDA or TxDB line low (space) after whatever data is in the buffer finishes transmitting (following the stop bit?), or after two bit-times if no data is being transmitted  */
223345      default:
224346         LOG(( "68681: Unhandled command (%x) in CR%d\n", (data >> 4) & 0x07, ch ));
225347         break;
226348   }
349   duart68681_update_interrupts(duart68681);
227350};
228351
229352static UINT8 duart68681_read_rx_fifo(duart68681_state *duart68681, int ch)
r5571r5572
243366   }
244367
245368   duart68681->channel[ch].rx_fifo_num--;
246
247   if ( duart68681->channel[ch].rx_fifo_num > 0 )
248   {
249      duart68681->channel[ch].SR |= STATUS_RECEIVER_READY;
250      if ( ch == 0 )
251      {
252         duart68681->ISR |= INT_RXRDY_FFULLA;
253      }
254      else
255      {
256         duart68681->ISR |= INT_RXRDY_FFULLB;
257      }
258   }
259   else
260   {
261      duart68681->channel[ch].SR &= ~STATUS_RECEIVER_READY;
262      if ( ch == 0 )
263      {
264         duart68681->ISR &= ~INT_RXRDY_FFULLA;
265      }
266      else
267      {
268         duart68681->ISR &= ~INT_RXRDY_FFULLB;
269      }
270   }
271369   duart68681_update_interrupts(duart68681);
272370
273371   return r;
r5571r5572
312410
313411   period = ATTOTIME_IN_HZ(duart68681->channel[ch].baud_rate / 10 );
314412   timer_adjust_oneshot(duart68681->channel[ch].tx_timer, period, ch);
413
414   // if local loopback is on, write the transmitted data as if a byte had been recieved
415   if ((duart68681->channel[ch].MR2&0xC0) == 0x80)
416   {
417      if ( duart68681->channel[ch].rx_fifo_num >= RX_FIFO_SIZE )
418      {
419         LOG(( "68681: FIFO overflow\n" ));
420         duart68681->channel[ch].SR |= STATUS_OVERRUN_ERROR;
421         return;
422      }
423      duart68681->channel[ch].rx_fifo[duart68681->channel[ch].rx_fifo_write_ptr++] = data;
424      if ( duart68681->channel[ch].rx_fifo_write_ptr == RX_FIFO_SIZE )
425      {
426         duart68681->channel[ch].rx_fifo_write_ptr = 0;
427      }
428      duart68681->channel[ch].rx_fifo_num++;
429      duart68681_update_interrupts(duart68681);
430   }
315431};
316432
317433READ8_DEVICE_HANDLER(duart68681_r)
r5571r5572
397513      case 0x0e: /* Start counter command */
398514         switch( (duart68681->ACR >> 4) & 0x07 )
399515         {
516            /* TODO: implement modes 0,1,2,4,5 */
517            case 0x03: /* Counter, CLK/16 */
518               {
519                  attotime rate = ATTOTIME_IN_HZ(2*device->clock/(2*16*16*duart68681->CTR.w.l));
520                  timer_adjust_periodic(duart68681->duart_timer, rate, 0, rate);
521               }
522               break;
523            case 0x06: /* Timer, CLK/1 */
524               {
525                  attotime rate = ATTOTIME_IN_HZ(2*device->clock/(2*16*duart68681->CTR.w.l));
526                  timer_adjust_periodic(duart68681->duart_timer, rate, 0, rate);
527               }
528               break;
400529            case 0x07: /* Timer, CLK/16 */
401530               {
402531                  //double hz;
r5571r5572
410539         break;
411540      case 0x0f: /* Stop counter command */
412541         duart68681->ISR &= ~INT_COUNTER_READY;
542         if (((duart68681->ACR >>4)& 0x07) < 4) // if in counter mode...
543         timer_adjust_oneshot(duart68681->duart_timer, attotime_never, 0); // shut down timer
413544         duart68681_update_interrupts(duart68681);
414545         break;
415546      default:
r5571r5572
446577         duart68681->ACR = data;
447578         //       bits 6-4: Counter/Timer Mode And Clock Source Select
448579         //       bits 3-0: IP3-0 Change-Of-State Interrupt Enable
449         if (((data >> 4) & 0x07) != 0x07)
580         switch ((data >> 4) & 0x07)
581         {
582            case 0: case 1: case 2: case 4: case 5: // TODO: handle these cases!
450583            logerror( "68681 (%s): Unhandled timer/counter mode %d\n", device->tag, (data >> 4) & 0x07);
451
584            break;
585            case 3: case 6: case 7:
586            break;
587         }
452588         duart68681_write_CSR(duart68681, 0, duart68681->channel[0].CSR, data);
453589         duart68681_write_CSR(duart68681, 1, duart68681->channel[1].CSR, data);
590         duart68681_update_interrupts(duart68681); // need to add ACR checking for IP delta ints
454591         break;
455592      case 0x05: /* IMR */
456593         duart68681->IMR = data;
r5571r5572
504641      if ( duart68681->channel[ch].rx_fifo_num >= RX_FIFO_SIZE )
505642      {
506643         LOG(( "68681: FIFO overflow\n" ));
644         duart68681->channel[ch].SR |= STATUS_OVERRUN_ERROR;
507645         return;
508646      }
509647      duart68681->channel[ch].rx_fifo[duart68681->channel[ch].rx_fifo_write_ptr++] = data;
r5571r5572
512650         duart68681->channel[ch].rx_fifo_write_ptr = 0;
513651      }
514652      duart68681->channel[ch].rx_fifo_num++;
515
516      duart68681->channel[ch].SR |= STATUS_RECEIVER_READY;
517      if ( BIT(duart68681->channel[ch].MR1,MODE_RX_INT_SELECT_BIT) == 0 )
518      {
519         duart68681->ISR |= (ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB;
520      }
521      if ( duart68681->channel[ch].rx_fifo_num == RX_FIFO_SIZE )
522      {
523         duart68681->channel[ch].SR |= STATUS_FIFO_FULL;
524         if ( BIT(duart68681->channel[ch].MR1,MODE_RX_INT_SELECT_BIT) == 1 )
525         {
526            duart68681->ISR |= (ch == 0) ? INT_RXRDY_FFULLA : INT_RXRDY_FFULLB;
527         }
528      }
529653      duart68681_update_interrupts(duart68681);
530654   }
531655};
r5571r5572
594718   memset(duart68681, 0, sizeof(duart68681_state));
595719   duart68681->duart_config = (const duart68681_config *)device->static_config;
596720   duart68681->device = device;
597   duart68681->IVR = 0x0f;
721   duart68681->IVR = 0x0f;  /* Interrupt Vector Register */
722   // "reset clears internal registers (SRA, SRB, IMR, ISR, OPR, OPCR) puts OP0-7 in the high state, stops the counter/timer, and puts channels a/b in the inactive state"
723   duart68681->channel[0].SR = 0;
724   duart68681->channel[1].SR = 0;
725   duart68681->IMR = 0;  /* Interrupt Mask Register */
726   duart68681->ISR = 0;  /* Interrupt Status Register */
727   duart68681->OPCR = 0; /* Output Port Conf. Register */
728   duart68681->OPR = 0;  /* Output Port Register */
729   if (duart68681->duart_config->output_port_write)
730   duart68681->duart_config->output_port_write(duart68681->device, duart68681->OPR ^ 0xff);
598731
599732   // allocate timers
600733   duart68681->channel[0].tx_timer = timer_alloc(device->machine, tx_timer_callback, (void*)device);

Previous 15994 Revisions Next


© 1998-2018 The MESS Team