Previous 15994 Revisions Next

r8893 Wednesday 25th August, 2010 at 06:48:22 UTC by Miodrag Milanović
Sync with MAME
[src/emu/machine]6522via.c 6522via.h 6526cia.c 6526cia.h 6532riot.c 6532riot.h 6821pia.c 6821pia.h 74123.c 74123.h 7474.c 7474.h i2cmem.c i2cmem.h

src/emu/machine/6526cia.c
r8892r8893
4141#define   CIA_CRA_SPMODE   0x40
4242#define   CIA_CRA_TODIN   0x80
4343
44/***************************************************************************
45    TYPE DEFINITIONS
46***************************************************************************/
44//**************************************************************************
45//  DEVICE CONFIGURATION
46//**************************************************************************
4747
48typedef struct _cia_timer cia_timer;
49typedef struct _cia_port cia_port;
50typedef struct _cia_state cia_state;
48//-------------------------------------------------
49//  mos6526_device_config - constructor
50//-------------------------------------------------
5151
52struct _cia_timer
52mos6526_device_config::mos6526_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
53    : device_config(mconfig, static_alloc_device_config, "MOS6526", tag, owner, clock)
5354{
54   UINT16      latch;
55   UINT16      count;
56   UINT8      mode;
57   UINT8      irq;
58   emu_timer *timer;
59   cia_state *   cia;
60};
55}
6156
62struct _cia_port
63{
64   UINT8      ddr;
65   UINT8      latch;
66   UINT8      in;
67   UINT8      out;
68   devcb_resolved_read8   read;
69   devcb_resolved_write8   write;
70   UINT8      mask_value; /* in READ operation the value can be forced by a extern electric circuit */
71};
7257
73struct _cia_state
58//-------------------------------------------------
59//  static_alloc_device_config - allocate a new
60//  configuration object
61//-------------------------------------------------
62
63device_config *mos6526_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
7464{
75   running_device *device;
76   devcb_resolved_write_line out_irq_func;
77   devcb_resolved_write_line out_pc_func;
78   devcb_resolved_write_line out_cnt_func;
79   devcb_resolved_write_line out_sp_func;
65    return global_alloc(mos6526_device_config(mconfig, tag, owner, clock));
66}
8067
81   cia_port      port[2];
82   cia_timer      timer[2];
8368
84   /* Time Of the Day clock (TOD) */
85   UINT32         tod;
86   UINT32         tod_latch;
87   UINT8         tod_latched;
88   UINT8         tod_running;
89   UINT32         alarm;
69//-------------------------------------------------
70//  alloc_device - allocate a new device object
71//-------------------------------------------------
9072
91   /* Interrupts */
92   UINT8         icr;
93   UINT8         ics;
94   UINT8         irq;
95   int            flag;
73device_t *mos6526_device_config::alloc_device(running_machine &machine) const
74{
75    return auto_alloc(&machine, mos6526_device(machine, *this));
76}
9677
97   /* Serial */
98   UINT8         loaded;
99   UINT8         sdr;
100   UINT8         sp;
101   UINT8         cnt;
102   UINT8         shift;
103   UINT8         serial;
104};
10578
106/***************************************************************************
107    PROTOTYPES
108***************************************************************************/
79//-------------------------------------------------
80//  device_config_complete - perform any
81//  operations now that the configuration is
82//  complete
83//-------------------------------------------------
10984
110static TIMER_CALLBACK( cia_timer_proc );
111static void cia_timer_underflow(running_device *device, int timer);
112static TIMER_CALLBACK( cia_clock_tod_callback );
113
114/***************************************************************************
115    INLINE FUNCTIONS
116***************************************************************************/
117
118INLINE cia_state *get_token(running_device *device)
85void mos6526_device_config::device_config_complete()
11986{
120   assert(device != NULL);
121   assert((device->type() == MOS6526R1) || (device->type() == MOS6526R2) || (device->type() == MOS8520));
122   return (cia_state *) downcast<legacy_device_base *>(device)->token();
123}
87   // inherit a copy of the static data
88   const mos6526_interface *intf = reinterpret_cast<const mos6526_interface *>(static_config());
89   if (intf != NULL)
90      *static_cast<mos6526_interface *>(this) = *intf;
12491
125INLINE const mos6526_interface *get_interface(running_device *device)
126{
127   assert(device != NULL);
128   assert((device->type() == MOS6526R1) || (device->type() == MOS6526R2) || (device->type() == MOS8520));
129   return (mos6526_interface *) device->baseconfig().static_config();
92   // or initialize to defaults if none provided
93   else
94   {
95      m_tod_clock = 0;
96       memset(&m_out_irq_func, 0, sizeof(m_out_irq_func));
97       memset(&m_out_pc_func, 0, sizeof(m_out_pc_func));
98       memset(&m_out_cnt_func, 0, sizeof(m_out_cnt_func));
99       memset(&m_out_sp_func, 0, sizeof(m_out_sp_func));
100       memset(&m_in_pa_func, 0, sizeof(m_in_pa_func));
101       memset(&m_out_pa_func, 0, sizeof(m_out_pa_func));
102       memset(&m_in_pb_func, 0, sizeof(m_in_pb_func));
103       memset(&m_out_pb_func, 0, sizeof(m_out_pb_func));
104   }
130105}
131106
132/***************************************************************************
133    IMPLEMENTATION
134***************************************************************************/
135107
136/*-------------------------------------------------
137    cia_set_port_mask_value
138-------------------------------------------------*/
139108
140void cia_set_port_mask_value(running_device *device, int port, int data)
109//**************************************************************************
110//  LIVE DEVICE
111//**************************************************************************
112
113const device_type MOS6526R1 = mos6526_device_config::static_alloc_device_config;
114const device_type MOS6526R2 = mos6526_device_config::static_alloc_device_config;
115const device_type MOS8520 = mos6526_device_config::static_alloc_device_config;
116
117//-------------------------------------------------
118//  mos6526_device - constructor
119//-------------------------------------------------
120
121mos6526_device::mos6526_device(running_machine &_machine, const mos6526_device_config &config)
122    : device_t(_machine, config),
123      m_config(config)
141124{
142   cia_state *cia = get_token(device);
143   cia->port[port].mask_value = data;
125
144126}
145127
146/*-------------------------------------------------
147    DEVICE_RESET( cia )
148-------------------------------------------------*/
149128
150static DEVICE_RESET( cia )
151{
152   int t;
153   cia_state *cia = get_token(device);
129//-------------------------------------------------
130//  device_reset - device-specific reset
131//-------------------------------------------------
154132
133void mos6526_device::device_reset()
134{
155135   /* clear things out */
156   cia->port[0].latch = 0x00;
157   cia->port[0].in = 0x00;
158   cia->port[0].out = 0x00;
159   cia->port[0].mask_value = 0xff;
160   cia->port[1].latch = 0x00;
161   cia->port[1].in = 0x00;
162   cia->port[1].out = 0x00;
163   cia->port[1].mask_value = 0xff;
164   cia->tod = 0;
165   cia->tod_latch = 0;
166   cia->alarm = 0;
167   cia->icr = 0x00;
168   cia->ics = 0x00;
169   cia->irq = 0;
170   cia->shift = 0;
171   cia->loaded = 0;
172   cia->cnt = 1;
173   cia->sp = 0;
136   m_port[0].m_latch = 0x00;
137   m_port[0].m_in = 0x00;
138   m_port[0].m_out = 0x00;
139   m_port[0].m_mask_value = 0xff;
140   m_port[1].m_latch = 0x00;
141   m_port[1].m_in = 0x00;
142   m_port[1].m_out = 0x00;
143   m_port[1].m_mask_value = 0xff;
144   m_tod = 0;
145   m_tod_latch = 0;
146   m_alarm = 0;
147   m_icr = 0x00;
148   m_ics = 0x00;
149   m_irq = 0;
150   m_shift = 0;
151   m_loaded = 0;
152   m_cnt = 1;
153   m_sp = 0;
174154
175155   /* initialize data direction registers */
176   cia->port[0].ddr = !strcmp(device->tag(), "cia_0") ? 0x03 : 0xff;
177   cia->port[1].ddr = !strcmp(device->tag(), "cia_0") ? 0x00 : 0xff;
156   m_port[0].m_ddr = !strcmp(tag(), "cia_0") ? 0x03 : 0xff;
157   m_port[1].m_ddr = !strcmp(tag(), "cia_0") ? 0x00 : 0xff;
178158
179159   /* TOD running by default */
180   cia->tod_running = TRUE;
160   m_tod_running = TRUE;
181161
182162   /* initialize timers */
183   for (t = 0; t < 2; t++)
163   for(int t = 0; t < 2; t++)
184164   {
185      cia_timer *timer = &cia->timer[t];
186
187      timer->latch = 0xffff;
188      timer->count = 0x0000;
189      timer->mode = 0x00;
165      cia_timer *timer = &m_timer[t];
166      timer->m_cia = this;
167      timer->m_clock = clock();
168      timer->m_latch = 0xffff;
169      timer->m_count = 0x0000;
170      timer->m_mode = 0x00;
190171   }
191172}
192173
193/*-------------------------------------------------
194    DEVICE_VALIDITY_CHECK( cia )
195-------------------------------------------------*/
196174
197#if 0
198static DEVICE_VALIDITY_CHECK( cia )
175//-------------------------------------------------
176//  device_start - device-specific startup
177//-------------------------------------------------
178
179void mos6526_device::device_start()
199180{
200   int error = FALSE;
181   /* clear out CIA structure, and copy the interface */
182   devcb_resolve_write_line(&m_out_irq_func, &m_config.m_out_irq_func, this);
183   devcb_resolve_write_line(&m_out_pc_func, &m_config.m_out_pc_func, this);
184   devcb_resolve_write_line(&m_out_cnt_func, &m_config.m_out_cnt_func, this);
185   devcb_resolve_write_line(&m_out_sp_func, &m_config.m_out_sp_func, this);
186   m_flag = 1;
201187
202   if (device->clock() <= 0)
188   /* setup ports */
189   devcb_resolve_read8(&m_port[0].m_read, &m_config.m_in_pa_func, this);
190   devcb_resolve_write8(&m_port[0].m_write, &m_config.m_out_pa_func, this);
191   devcb_resolve_read8(&m_port[1].m_read, &m_config.m_in_pb_func, this);
192   devcb_resolve_write8(&m_port[1].m_write, &m_config.m_out_pb_func, this);
193
194   for (int p = 0; p < (sizeof(m_port) / sizeof(m_port[0])); p++)
203195   {
204      mame_printf_error("%s: %s has a cia with an invalid clock\n", driver->source_file, driver->name);
205      error = TRUE;
196      m_port[p].m_mask_value = 0xff;
206197   }
207198
208   return error;
209}
210#endif
199   /* setup timers */
200   for (int t = 0; t < (sizeof(m_timer) / sizeof(m_timer[0])); t++)
201   {
202      cia_timer *timer = &m_timer[t];
203      timer->m_timer = timer_alloc(&m_machine, timer_proc, (void*)this);
204      timer->m_cia = this;
205      timer->m_irq = 0x01 << t;
206   }
211207
212/*-------------------------------------------------
213    cia_update_pc - pulse /pc output
214-------------------------------------------------*/
208   /* setup TOD timer, if appropriate */
209   if (m_config.m_tod_clock != 0)
210   {
211      timer_pulse(&m_machine, ATTOTIME_IN_HZ(m_config.m_tod_clock), (void *)this, 0, clock_tod_callback);
212   }
215213
216static void cia_update_pc(running_device *device)
217{
218   cia_state *cia = get_token(device);
219
220   /* this should really be one cycle long */
221   devcb_call_write_line(&cia->out_pc_func, 0);
222   devcb_call_write_line(&cia->out_pc_func, 1);
214   /* state save support */
215   state_save_register_device_item(this, 0, m_port[0].m_ddr);
216   state_save_register_device_item(this, 0, m_port[0].m_latch);
217   state_save_register_device_item(this, 0, m_port[0].m_in);
218   state_save_register_device_item(this, 0, m_port[0].m_out);
219   state_save_register_device_item(this, 0, m_port[0].m_mask_value);
220   state_save_register_device_item(this, 0, m_port[1].m_ddr);
221   state_save_register_device_item(this, 0, m_port[1].m_latch);
222   state_save_register_device_item(this, 0, m_port[1].m_in);
223   state_save_register_device_item(this, 0, m_port[1].m_out);
224   state_save_register_device_item(this, 0, m_port[1].m_mask_value);
225   state_save_register_device_item(this, 0, m_timer[0].m_latch);
226   state_save_register_device_item(this, 0, m_timer[0].m_count);
227   state_save_register_device_item(this, 0, m_timer[0].m_mode);
228   state_save_register_device_item(this, 0, m_timer[0].m_irq);
229   state_save_register_device_item(this, 0, m_timer[1].m_latch);
230   state_save_register_device_item(this, 0, m_timer[1].m_count);
231   state_save_register_device_item(this, 0, m_timer[1].m_mode);
232   state_save_register_device_item(this, 0, m_timer[1].m_irq);
233   state_save_register_device_item(this, 0, m_tod);
234   state_save_register_device_item(this, 0, m_tod_latch);
235   state_save_register_device_item(this, 0, m_tod_latched);
236   state_save_register_device_item(this, 0, m_tod_running);
237   state_save_register_device_item(this, 0, m_alarm);
238   state_save_register_device_item(this, 0, m_icr);
239   state_save_register_device_item(this, 0, m_ics);
240   state_save_register_device_item(this, 0, m_irq);
241   state_save_register_device_item(this, 0, m_flag);
242   state_save_register_device_item(this, 0, m_loaded);
243   state_save_register_device_item(this, 0, m_sdr);
244   state_save_register_device_item(this, 0, m_sp);
245   state_save_register_device_item(this, 0, m_cnt);
246   state_save_register_device_item(this, 0, m_shift);
247   state_save_register_device_item(this, 0, m_serial);
223248}
224249
250
225251/*-------------------------------------------------
226    cia_update_interrupts
252    set_port_mask_value
227253-------------------------------------------------*/
228254
229static void cia_update_interrupts(running_device *device)
255void mos6526_device::set_port_mask_value(int port, int data)
230256{
231   UINT8 new_irq;
232   cia_state *cia = get_token(device);
233
234   /* always update the high bit of ICS */
235   if (cia->ics & 0x7f)
236      cia->ics |= 0x80;
237   else
238      cia->ics &= ~0x80;
239
240   /* based on what is enabled, set/clear the IRQ via the custom chip */
241   new_irq = (cia->ics & cia->icr) ? 1 : 0;
242   if (cia->irq != new_irq)
243   {
244      cia->irq = new_irq;
245      devcb_call_write_line(&cia->out_irq_func, cia->irq);
246   }
257   m_port[port].m_mask_value = data;
247258}
248259
249260/*-------------------------------------------------
250    is_timer_active
261    update_pc - pulse /pc output
251262-------------------------------------------------*/
252263
253static int is_timer_active(emu_timer *timer)
264void mos6526_device::update_pc()
254265{
255   attotime t = timer_firetime(timer);
256   return attotime_compare(t, attotime_never) != 0;
266   /* this should really be one cycle long */
267   devcb_call_write_line(&m_out_pc_func, 0);
268   devcb_call_write_line(&m_out_pc_func, 1);
257269}
258270
259271/*-------------------------------------------------
260    cia_timer_update - updates the count and
261    emu_timer for a given CIA timer
272    update_interrupts
262273-------------------------------------------------*/
263274
264static void cia_timer_update(cia_timer *timer, INT32 new_count)
275void mos6526_device::update_interrupts()
265276{
266   int which = timer - timer->cia->timer;
277   UINT8 new_irq;
267278
268   /* sanity check arguments */
269   assert((new_count >= -1) && (new_count <= 0xffff));
270
271   /* update the timer count, if necessary */
272   if ((new_count == -1) && is_timer_active(timer->timer))
279   /* always update the high bit of ICS */
280   if (m_ics & 0x7f)
273281   {
274      UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(timer->timer), timer->cia->device->clock()));
275      timer->count = timer->count - MIN(timer->count, current_count);
282      m_ics |= 0x80;
276283   }
277
278   /* set the timer if we are instructed to */
279   if (new_count != -1)
280      timer->count = new_count;
281
282   /* now update the MAME timer */
283   if ((timer->mode & 0x01) && ((timer->mode & (which ? 0x60 : 0x20)) == 0x00))
284   else
284285   {
285      /* timer is on and is connected to clock */
286      attotime period = attotime_mul(ATTOTIME_IN_HZ(timer->cia->device->clock()), (timer->count ? timer->count : 0x10000));
287      timer_adjust_oneshot(timer->timer, period, 0);
286      m_ics &= ~0x80;
288287   }
289   else
288
289   /* based on what is enabled, set/clear the IRQ via the custom chip */
290   new_irq = (m_ics & m_icr) ? 1 : 0;
291   if (m_irq != new_irq)
290292   {
291      /* timer is off or not connected to clock */
292      timer_adjust_oneshot(timer->timer, attotime_never, 0);
293      m_irq = new_irq;
294      devcb_call_write_line(&m_out_irq_func, m_irq);
293295   }
294296}
295297
298
296299/*-------------------------------------------------
297    cia_get_timer - get the count
298    for a given CIA timer
300    timer_bump
299301-------------------------------------------------*/
300302
301static UINT16 cia_get_timer(cia_timer *timer)
303void mos6526_device::timer_bump(int timer)
302304{
303   UINT16 count;
305   m_timer[timer].update(timer, -1);
304306
305   if (is_timer_active(timer->timer))
307   if (m_timer[timer].m_count == 0x00)
306308   {
307      UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(timer->timer), timer->cia->device->clock()));
308      count = timer->count - MIN(timer->count, current_count);
309      timer_underflow(timer);
309310   }
310311   else
311      count = timer->count;
312
313   return count;
312   {
313      m_timer[timer].update(timer, m_timer[timer].m_count - 1);
314   }
314315}
315316
316317/*-------------------------------------------------
317    cia_timer_bump
318-------------------------------------------------*/
319
320static void cia_timer_bump(running_device *device, int timer)
321{
322   cia_state *cia = get_token(device);
323
324   cia_timer_update(&cia->timer[timer], -1);
325
326   if (cia->timer[timer].count == 0x00)
327      cia_timer_underflow(device, timer);
328   else
329      cia_timer_update(&cia->timer[timer], cia->timer[timer].count - 1);
330}
331
332/*-------------------------------------------------
333318    cia_timer_underflow
334319-------------------------------------------------*/
335320
336static void cia_timer_underflow(running_device *device, int timer)
321void mos6526_device::timer_underflow(int timer)
337322{
338   cia_state *cia = get_token(device);
339
340323   assert((timer == 0) || (timer == 1));
341324
342325   /* set the status and update interrupts */
343   cia->ics |= cia->timer[timer].irq;
344   cia_update_interrupts(device);
326   m_ics |= m_timer[timer].m_irq;
327   update_interrupts();
345328
346329   /* if one-shot mode, turn it off */
347   if (cia->timer[timer].mode & 0x08)
348      cia->timer[timer].mode &= 0xfe;
330   if (m_timer[timer].m_mode & 0x08)
331   {
332      m_timer[timer].m_mode &= 0xfe;
333   }
349334
350335   /* reload the timer */
351   cia_timer_update(&cia->timer[timer], cia->timer[timer].latch);
336   m_timer[timer].update(timer, m_timer[timer].m_latch);
352337
353338   /* timer A has some interesting properties */
354339   if (timer == 0)
355340   {
356341      /* such as cascading to timer B */
357      if ((cia->timer[1].mode & 0x41) == 0x41)
342      if ((m_timer[1].m_mode & 0x41) == 0x41)
358343      {
359         if (cia->cnt || !(cia->timer[1].mode & 0x20))
360            cia_timer_bump(device, 1);
344         if (m_cnt || !(m_timer[1].m_mode & 0x20))
345         {
346            timer_bump(1);
347         }
361348      }
362349
363350      /* also the serial line */
364      if ((cia->timer[timer].irq == 0x01) && (cia->timer[timer].mode & CIA_CRA_SPMODE))
351      if ((m_timer[timer].m_irq == 0x01) && (m_timer[timer].m_mode & CIA_CRA_SPMODE))
365352      {
366         if (cia->loaded || cia->shift)
353         if (m_loaded || m_shift)
367354         {
368355            /* falling edge */
369            if (cia->cnt)
356            if (m_cnt)
370357            {
371               if (cia->shift == 0)
358               if (m_shift == 0)
372359               {
373360                  /* load shift register */
374                  cia->loaded = 0;
375                  cia->serial = cia->sdr;
361                  m_loaded = 0;
362                  m_serial = m_sdr;
376363               }
377364
378365               /* transmit MSB */
379               cia->sp = BIT(cia->serial, 7);
380               devcb_call_write_line(&cia->out_sp_func, cia->sp);
366               m_sp = BIT(m_serial, 7);
367               devcb_call_write_line(&m_out_sp_func, m_sp);
381368
382369               /* toggle CNT */
383               cia->cnt = !cia->cnt;
384               devcb_call_write_line(&cia->out_cnt_func, cia->cnt);
370               m_cnt = !m_cnt;
371               devcb_call_write_line(&m_out_cnt_func, m_cnt);
385372
386373               /* shift data */
387               cia->serial <<= 1;
388               cia->shift++;
374               m_serial <<= 1;
375               m_shift++;
389376
390               if (cia->shift == 8)
377               if (m_shift == 8)
391378               {
392379                  /* signal interrupt */
393                  cia->ics |= 0x08;
394                  cia_update_interrupts(device);
380                  m_ics |= 0x08;
381                  update_interrupts();
395382               }
396383            }
397384            else
398385            {
399386               /* toggle CNT */
400               cia->cnt = !cia->cnt;
401               devcb_call_write_line(&cia->out_cnt_func, cia->cnt);
387               m_cnt = !m_cnt;
388               devcb_call_write_line(&m_out_cnt_func, m_cnt);
402389
403               if (cia->shift == 8)
390               if (m_shift == 8)
404391               {
405                  cia->shift = 0;
392                  m_shift = 0;
406393               }
407394            }
408395         }
r8892r8893
414401    TIMER_CALLBACK( cia_timer_proc )
415402-------------------------------------------------*/
416403
417static TIMER_CALLBACK( cia_timer_proc )
404TIMER_CALLBACK( mos6526_device::timer_proc )
418405{
419   cia_timer *timer = (cia_timer *)ptr;
420   cia_state *cia = timer->cia;
406    mos6526_device *cia = reinterpret_cast<mos6526_device *>(ptr);
421407
422   cia_timer_underflow(cia->device, timer - cia->timer);
408   cia->timer_underflow(param);
423409}
424410
425411/*-------------------------------------------------
r8892r8893
438424    cia6526_increment
439425-------------------------------------------------*/
440426
441static void cia6526_increment(cia_state *cia)
427void mos6526_device::increment()
442428{
443429   /* break down TOD value into components */
444   UINT8 subsecond   = (UINT8) (cia->tod >>  0);
445   UINT8 second   = (UINT8) (cia->tod >>  8);
446   UINT8 minute   = (UINT8) (cia->tod >> 16);
447   UINT8 hour      = (UINT8) (cia->tod >> 24);
430   UINT8 subsecond   = (UINT8) (m_tod >>  0);
431   UINT8 second   = (UINT8) (m_tod >>  8);
432   UINT8 minute   = (UINT8) (m_tod >> 16);
433   UINT8 hour      = (UINT8) (m_tod >> 24);
448434
449435   subsecond = bcd_increment(subsecond);
450436   if (subsecond >= 0x10)
451437   {
452438      subsecond = 0x00;
453439      second = bcd_increment(second);
454      if (second >= ((cia->timer[0].mode & 0x80) ? 0x50 : 0x60))
440      if (second >= ((m_timer[0].m_mode & 0x80) ? 0x50 : 0x60))
455441      {
456442         second = 0x00;
457443         minute = bcd_increment(minute);
r8892r8893
473459   }
474460
475461   /* update the TOD with new value */
476   cia->tod = (((UINT32) subsecond)   <<  0)
477          | (((UINT32) second)      <<  8)
478          | (((UINT32) minute)      << 16)
479          | (((UINT32) hour)         << 24);
462   m_tod = (((UINT32) subsecond)   <<  0)
463        | (((UINT32) second)      <<  8)
464        | (((UINT32) minute)      << 16)
465        | (((UINT32) hour)      << 24);
480466}
481467
482468/*-------------------------------------------------
483469    cia_clock_tod - Update TOD on CIA A
484470-------------------------------------------------*/
485471
486static void cia_clock_tod(running_device *device)
472void mos6526_device::clock_tod()
487473{
488   cia_state *cia;
489
490   cia = get_token(device);
491
492   if (cia->tod_running)
474   if (m_tod_running)
493475   {
494      if ((device->type() == MOS6526R1) || (device->type() == MOS6526R2))
476      if ((type() == MOS6526R1) || (type() == MOS6526R2))
495477      {
496478         /* The 6526 split the value into hours, minutes, seconds and
497479             * subseconds */
498         cia6526_increment(cia);
480         increment();
499481      }
500      else if (device->type() == MOS8520)
482      else if (type() == MOS8520)
501483      {
502484         /* the 8520 has a straight 24-bit counter */
503         cia->tod++;
504         cia->tod &= 0xffffff;
485         m_tod++;
486         m_tod &= 0xffffff;
505487      }
506488
507      if (cia->tod == cia->alarm)
489      if (m_tod == m_alarm)
508490      {
509         cia->ics |= 0x04;
510         cia_update_interrupts(device);
491         m_ics |= 0x04;
492         update_interrupts();
511493      }
512494   }
513495}
514496
497
515498/*-------------------------------------------------
516    cia_clock_tod_callback
499    clock_tod_callback
517500-------------------------------------------------*/
518501
519static TIMER_CALLBACK( cia_clock_tod_callback )
502TIMER_CALLBACK( mos6526_device::clock_tod_callback )
520503{
521   running_device *device = (running_device *)ptr;
522   cia_clock_tod(device);
504    mos6526_device *cia = reinterpret_cast<mos6526_device *>(ptr);
505   cia->clock_tod();
523506}
524507
525READ8_DEVICE_HANDLER( mos6526_pa_r )
526{
527   return (get_token(device)->port[0].latch | ~get_token(device)->port[0].ddr);
528}
529508
530READ8_DEVICE_HANDLER( mos6526_pb_r )
531{
532   return (get_token(device)->port[1].latch | ~get_token(device)->port[1].ddr);
533}
509/*-------------------------------------------------
510   cnt_w
511-------------------------------------------------*/
534512
535READ_LINE_DEVICE_HANDLER( mos6526_irq_r )
513void mos6526_device::cnt_w(UINT8 state)
536514{
537   cia_state *cia = get_token(device);
538
539   return cia->irq;
540}
541
542WRITE_LINE_DEVICE_HANDLER( mos6526_tod_w )
543{
544   if (state) cia_clock_tod(device);
545}
546
547READ_LINE_DEVICE_HANDLER( mos6526_cnt_r )
548{
549   cia_state *cia = get_token(device);
550
551   return cia->cnt;
552}
553
554WRITE_LINE_DEVICE_HANDLER( mos6526_cnt_w )
555{
556   cia_state *cia = get_token(device);
557
558515   /* is this a rising edge? */
559   if (!cia->cnt && state)
516   if (!m_cnt && state)
560517   {
561      if (cia->timer[0].mode & CIA_CRA_START)
518      if (m_timer[0].m_mode & CIA_CRA_START)
562519      {
563520         /* does timer #0 bump on CNT? */
564         if (cia->timer[0].mode & CIA_CRA_INMODE)
565            cia_timer_bump(device, 0);
521         if (m_timer[0].m_mode & CIA_CRA_INMODE)
522         {
523            timer_bump(0);
524         }
566525      }
567526
568527      /* if the serial port is set to input, the CNT will shift the port */
569      if (!(cia->timer[0].mode & CIA_CRA_SPMODE))
528      if (!(m_timer[0].m_mode & CIA_CRA_SPMODE))
570529      {
571         cia->serial <<= 1;
572         cia->shift++;
530         m_serial <<= 1;
531         m_shift++;
573532
574         if (cia->sp) cia->serial |= 0x01;
533         if (m_sp)
534         {
535            m_serial |= 0x01;
536         }
575537
576         if (cia->shift == 8)
538         if (m_shift == 8)
577539         {
578            cia->sdr = cia->serial;
579            cia->serial = 0;
580            cia->shift = 0;
581            cia->ics |= 0x08;
582            cia_update_interrupts(device);
540            m_sdr = m_serial;
541            m_serial = 0;
542            m_shift = 0;
543            m_ics |= 0x08;
544            update_interrupts();
583545         }
584546      }
585547
586548      /* does timer #1 bump on CNT? */
587      if ((cia->timer[1].mode & 0x61) == 0x21)
588         cia_timer_bump(device, 1);
549      if ((m_timer[1].m_mode & 0x61) == 0x21)
550      {
551         timer_bump(1);
552      }
589553   }
590554
591   cia->cnt = state;
555   m_cnt = state;
592556}
593557
594READ_LINE_DEVICE_HANDLER( mos6526_sp_r )
558void mos6526_device::flag_w(UINT8 state)
595559{
596   cia_state *cia = get_token(device);
597
598   return cia->sp;
599}
600
601WRITE_LINE_DEVICE_HANDLER( mos6526_sp_w )
602{
603   cia_state *cia = get_token(device);
604
605   cia->sp = state;
606}
607
608WRITE_LINE_DEVICE_HANDLER( mos6526_flag_w )
609{
610   cia_state *cia = get_token(device);
611
612560   /* falling edge */
613   if (cia->flag && !state)
561   if (m_flag && !state)
614562   {
615      cia->ics |= 0x10;
616      cia_update_interrupts(device);
563      m_ics |= 0x10;
564      update_interrupts();
617565   }
618566
619   cia->flag = state;
567   m_flag = state;
620568}
621569
622570/*-------------------------------------------------
623    mos6526_r
571    reg_r
624572-------------------------------------------------*/
625573
626READ8_DEVICE_HANDLER( mos6526_r )
574UINT8 mos6526_device::reg_r(UINT8 offset)
627575{
628576   cia_timer *timer;
629   cia_state *cia;
630577   cia_port *port;
631578   UINT8 data = 0x00;
632579
633   cia = get_token(device);
634580   offset &= 0x0F;
635581
636582   switch(offset)
r8892r8893
638584      /* port A/B data */
639585      case CIA_PRA:
640586      case CIA_PRB:
641         port = &cia->port[offset & 1];
642         data = devcb_call_read8(&port->read, 0);
643         data = ((data & ~port->ddr) | (port->latch & port->ddr)) & port->mask_value;
587         port = &m_port[offset & 1];
588         data = devcb_call_read8(&port->m_read, 0);
589         data = ((data & ~port->m_ddr) | (port->m_latch & port->m_ddr)) & port->m_mask_value;
644590
645         port->in = data;
591         port->m_in = data;
646592
647593         if (offset == CIA_PRB)
648594         {
649595            /* timer #0 can change PB6 */
650            if (cia->timer[0].mode & 0x02)
596            if (m_timer[0].m_mode & 0x02)
651597            {
652               cia_timer_update(&cia->timer[0], -1);
653               if (cia->timer[0].count != 0)
598               m_timer[0].update(0, -1);
599               if (m_timer[0].m_count != 0)
600               {
654601                  data |= 0x40;
602               }
655603               else
604               {
656605                  data &= ~0x40;
606               }
657607            }
658608
659609            /* timer #1 can change PB7 */
660            if (cia->timer[1].mode & 0x02)
610            if (m_timer[1].m_mode & 0x02)
661611            {
662               cia_timer_update(&cia->timer[1], -1);
663               if (cia->timer[1].count != 0)
612               m_timer[1].update(1, -1);
613               if (m_timer[1].m_count != 0)
614               {
664615                  data |= 0x80;
616               }
665617               else
618               {
666619                  data &= ~0x80;
620               }
667621            }
668622
669623            /* pulse /PC following the read */
670            cia_update_pc(device);
624            update_pc();
671625         }
672626         break;
673627
674628      /* port A/B direction */
675629      case CIA_DDRA:
676630      case CIA_DDRB:
677         port = &cia->port[offset & 1];
678         data = port->ddr;
631         port = &m_port[offset & 1];
632         data = port->m_ddr;
679633         break;
680634
681635      /* timer A/B low byte */
682636      case CIA_TALO:
683637      case CIA_TBLO:
684         timer = &cia->timer[(offset >> 1) & 1];
685         data = cia_get_timer(timer) >> 0;
638         timer = &m_timer[(offset >> 1) & 1];
639         data = timer->get_count() >> 0;
686640         break;
687641
688642      /* timer A/B high byte */
689643      case CIA_TAHI:
690644      case CIA_TBHI:
691         timer = &cia->timer[(offset >> 1) & 1];
692         data = cia_get_timer(timer) >> 8;
645         timer = &m_timer[(offset >> 1) & 1];
646         data = timer->get_count() >> 8;
693647         break;
694648
695649      /* TOD counter */
r8892r8893
697651      case CIA_TOD1:
698652      case CIA_TOD2:
699653      case CIA_TOD3:
700         if (device->type() == MOS8520)
654         if (type() == MOS8520)
701655         {
702656            if (offset == CIA_TOD2)
703657            {
704               cia->tod_latch = cia->tod;
705               cia->tod_latched = TRUE;
658               m_tod_latch = m_tod;
659               m_tod_latched = TRUE;
706660            }
707661         }
708662         else
709663         {
710664            if (offset == CIA_TOD3)
711665            {
712               cia->tod_latch = cia->tod;
713               cia->tod_latched = TRUE;
666               m_tod_latch = m_tod;
667               m_tod_latched = TRUE;
714668            }
715669         }
716670         if (offset == CIA_TOD0)
717            cia->tod_latched = FALSE;
671         {
672            m_tod_latched = FALSE;
673         }
718674
719         if (cia->tod_latched)
720            data = cia->tod_latch >> ((offset - CIA_TOD0) * 8);
675         if (m_tod_latched)
676         {
677            data = m_tod_latch >> ((offset - CIA_TOD0) * 8);
678         }
721679         else
722            data = cia->tod >> ((offset - CIA_TOD0) * 8);
680         {
681            data = m_tod >> ((offset - CIA_TOD0) * 8);
682         }
723683         break;
724684
725685      /* serial data ready */
726686      case CIA_SDR:
727         data = cia->sdr;
687         data = m_sdr;
728688         break;
729689
730690      /* interrupt status/clear */
731691      case CIA_ICR:
732         data = cia->ics;
733         cia->ics = 0; /* clear on read */
734         cia_update_interrupts(device);
692         data = m_ics;
693         m_ics = 0; /* clear on read */
694         update_interrupts();
735695         break;
736696
737697      /* timer A/B mode */
738698      case CIA_CRA:
739699      case CIA_CRB:
740         timer = &cia->timer[offset & 1];
741         data = timer->mode;
700         timer = &m_timer[offset & 1];
701         data = timer->m_mode;
742702         break;
743703   }
744704
r8892r8893
746706}
747707
748708/*-------------------------------------------------
749    mos6526_w
709    reg_w
750710-------------------------------------------------*/
751711
752WRITE8_DEVICE_HANDLER( mos6526_w )
712void mos6526_device::reg_w(UINT8 offset, UINT8 data)
753713{
754714   cia_timer *timer;
755   cia_state *cia;
756715   cia_port *port;
757716   int shift;
758717
759   cia = get_token(device);
760718   offset &= 0x0F;
761719
762720   switch(offset)
r8892r8893
764722      /* port A/B data */
765723      case CIA_PRA:
766724      case CIA_PRB:
767         port = &cia->port[offset & 1];
768         port->latch = data;
769         port->out = (data & port->ddr) | (port->in & ~port->ddr);
770         devcb_call_write8(&port->write, 0, port->out);
725         port = &m_port[offset & 1];
726         port->m_latch = data;
727         port->m_out = (data & port->m_ddr) | (port->m_in & ~port->m_ddr);
728         devcb_call_write8(&port->m_write, 0, port->m_out);
771729
772730         /* pulse /PC following the write */
773731         if (offset == CIA_PRB)
774            cia_update_pc(device);
732         {
733            update_pc();
734         }
775735
776736         break;
777737
778738      /* port A/B direction */
779739      case CIA_DDRA:
780740      case CIA_DDRB:
781         port = &cia->port[offset & 1];
782         port->ddr = data;
741         port = &m_port[offset & 1];
742         port->m_ddr = data;
783743         break;
784744
785745      /* timer A/B latch low */
786746      case CIA_TALO:
787747      case CIA_TBLO:
788         timer = &cia->timer[(offset >> 1) & 1];
789         timer->latch = (timer->latch & 0xff00) | (data << 0);
748         timer = &m_timer[(offset >> 1) & 1];
749         timer->m_latch = (timer->m_latch & 0xff00) | (data << 0);
790750         break;
791751
792752      /* timer A latch high */
793753      case CIA_TAHI:
794754      case CIA_TBHI:
795         timer = &cia->timer[(offset >> 1) & 1];
796         timer->latch = (timer->latch & 0x00ff) | (data << 8);
755         timer = &m_timer[(offset >> 1) & 1];
756         timer->m_latch = (timer->m_latch & 0x00ff) | (data << 8);
797757
798758         /* if the timer is one-shot, then force a start on it */
799         if (timer->mode & 0x08)
759         if (timer->m_mode & 0x08)
800760         {
801            timer->mode |= 1;
802            cia_timer_update(timer, timer->latch);
761            timer->m_mode |= 1;
762            timer->update((offset >> 1) & 1, timer->m_latch);
803763         }
804764         else
805765         {
806766            /* if the timer is off, update the count */
807            if (!(timer->mode & 0x01))
808               cia_timer_update(timer, timer->latch);
767            if (!(timer->m_mode & 0x01))
768            {
769               timer->update((offset >> 1) & 1, timer->m_latch);
770            }
809771         }
810772         break;
811773
r8892r8893
817779         shift = 8 * ((offset - CIA_TOD0));
818780
819781         /* alarm setting mode? */
820         if (cia->timer[1].mode & 0x80)
821            cia->alarm = (cia->alarm & ~(0xff << shift)) | (data << shift);
782         if (m_timer[1].m_mode & 0x80)
783         {
784            m_alarm = (m_alarm & ~(0xff << shift)) | (data << shift);
785         }
822786         /* counter setting mode */
823787         else
824            cia->tod = (cia->tod & ~(0xff << shift)) | (data << shift);
788         {
789            m_tod = (m_tod & ~(0xff << shift)) | (data << shift);
790         }
825791
826         if (device->type() == MOS8520)
792         if (type() == MOS8520)
827793         {
828794            if (offset == CIA_TOD2)
829               cia->tod_running = FALSE;
795            {
796               m_tod_running = FALSE;
797            }
830798         }
831799         else
832800         {
833801            if (offset == CIA_TOD3)
834               cia->tod_running = FALSE;
802            {
803               m_tod_running = FALSE;
804            }
835805         }
836806         if (offset == CIA_TOD0)
837            cia->tod_running = TRUE;
807         {
808            m_tod_running = TRUE;
809         }
838810         break;
839811
840812      /* serial data ready */
841813      case CIA_SDR:
842         cia->sdr = data;
843         if (cia->timer[0].mode & 0x40)
844            cia->loaded = 1;
814         m_sdr = data;
815         if (m_timer[0].m_mode & 0x40)
816         {
817            m_loaded = 1;
818         }
845819         break;
846820
847821      /* interrupt control register */
848822      case CIA_ICR:
849823         if (data & 0x80)
850            cia->icr |= data & 0x7f;
824         {
825            m_icr |= data & 0x7f;
826         }
851827         else
852            cia->icr &= ~(data & 0x7f);
853         cia_update_interrupts(device);
828         {
829            m_icr &= ~(data & 0x7f);
830         }
831         update_interrupts();
854832         break;
855833
856834      /* timer A/B modes */
857835      case CIA_CRA:
858836      case CIA_CRB:
859         timer = &cia->timer[offset & 1];
860         timer->mode = data & 0xef;
837         timer = &m_timer[offset & 1];
838         timer->m_mode = data & 0xef;
861839
862840         /* force load? */
863841         if (data & 0x10)
864            cia_timer_update(timer, timer->latch);
842         {
843            timer->update(offset & 1, timer->m_latch);
844         }
865845         else
866            cia_timer_update(timer, -1);
846         {
847            timer->update(offset & 1, -1);
848         }
867849         break;
868850   }
869851}
870852
871853/*-------------------------------------------------
872    DEVICE_START( cia )
854    is_timer_active
873855-------------------------------------------------*/
874856
875static DEVICE_START( cia )
857static int is_timer_active(emu_timer *timer)
876858{
877   int t, p;
878   cia_state *cia = get_token(device);
879   const mos6526_interface *intf = get_interface(device);
859   attotime t = timer_firetime(timer);
860   return attotime_compare(t, attotime_never) != 0;
861}
880862
881   /* clear out CIA structure, and copy the interface */
882   memset(cia, 0, sizeof(*cia));
883   cia->device = device;
884   devcb_resolve_write_line(&cia->out_irq_func, &intf->out_irq_func, device);
885   devcb_resolve_write_line(&cia->out_pc_func, &intf->out_pc_func, device);
886   devcb_resolve_write_line(&cia->out_cnt_func, &intf->out_cnt_func, device);
887   devcb_resolve_write_line(&cia->out_sp_func, &intf->out_sp_func, device);
888   cia->flag = 1;
863/*-------------------------------------------------
864    update - updates the count and emu_timer for
865    a given CIA timer
866-------------------------------------------------*/
889867
890   /* setup ports */
891   devcb_resolve_read8(&cia->port[0].read, &intf->in_pa_func, device);
892   devcb_resolve_write8(&cia->port[0].write, &intf->out_pa_func, device);
893   devcb_resolve_read8(&cia->port[1].read, &intf->in_pb_func, device);
894   devcb_resolve_write8(&cia->port[1].write, &intf->out_pb_func, device);
868void mos6526_device::cia_timer::update(int which, INT32 new_count)
869{
870   /* sanity check arguments */
871   assert((new_count >= -1) && (new_count <= 0xffff));
895872
896   for (p = 0; p < (sizeof(cia->port) / sizeof(cia->port[0])); p++)
873   /* update the timer count, if necessary */
874   if ((new_count == -1) && is_timer_active(m_timer))
897875   {
898      cia->port[p].mask_value = 0xff;
876      UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(m_timer), m_clock));
877      m_count = m_count - MIN(m_count, current_count);
899878   }
900879
901   /* setup timers */
902   for (t = 0; t < (sizeof(cia->timer) / sizeof(cia->timer[0])); t++)
880   /* set the timer if we are instructed to */
881   if (new_count != -1)
903882   {
904      cia_timer *timer = &cia->timer[t];
905      timer->timer = timer_alloc(device->machine, cia_timer_proc, timer);
906      timer->cia = cia;
907      timer->irq = 0x01 << t;
883      m_count = new_count;
908884   }
909885
910   /* setup TOD timer, if appropriate */
911   if (intf->tod_clock != 0)
912      timer_pulse(device->machine, ATTOTIME_IN_HZ(intf->tod_clock), (void *) device, 0, cia_clock_tod_callback);
913
914   /* state save support */
915   state_save_register_device_item(device, 0, cia->port[0].ddr);
916   state_save_register_device_item(device, 0, cia->port[0].latch);
917   state_save_register_device_item(device, 0, cia->port[0].in);
918   state_save_register_device_item(device, 0, cia->port[0].out);
919   state_save_register_device_item(device, 0, cia->port[0].mask_value);
920   state_save_register_device_item(device, 0, cia->port[1].ddr);
921   state_save_register_device_item(device, 0, cia->port[1].latch);
922   state_save_register_device_item(device, 0, cia->port[1].in);
923   state_save_register_device_item(device, 0, cia->port[1].out);
924   state_save_register_device_item(device, 0, cia->port[1].mask_value);
925   state_save_register_device_item(device, 0, cia->timer[0].latch);
926   state_save_register_device_item(device, 0, cia->timer[0].count);
927   state_save_register_device_item(device, 0, cia->timer[0].mode);
928   state_save_register_device_item(device, 0, cia->timer[0].irq);
929   state_save_register_device_item(device, 0, cia->timer[1].latch);
930   state_save_register_device_item(device, 0, cia->timer[1].count);
931   state_save_register_device_item(device, 0, cia->timer[1].mode);
932   state_save_register_device_item(device, 0, cia->timer[1].irq);
933   state_save_register_device_item(device, 0, cia->tod);
934   state_save_register_device_item(device, 0, cia->tod_latch);
935   state_save_register_device_item(device, 0, cia->tod_latched);
936   state_save_register_device_item(device, 0, cia->tod_running);
937   state_save_register_device_item(device, 0, cia->alarm);
938   state_save_register_device_item(device, 0, cia->icr);
939   state_save_register_device_item(device, 0, cia->ics);
940   state_save_register_device_item(device, 0, cia->irq);
941   state_save_register_device_item(device, 0, cia->flag);
942   state_save_register_device_item(device, 0, cia->loaded);
943   state_save_register_device_item(device, 0, cia->sdr);
944   state_save_register_device_item(device, 0, cia->sp);
945   state_save_register_device_item(device, 0, cia->cnt);
946   state_save_register_device_item(device, 0, cia->shift);
947   state_save_register_device_item(device, 0, cia->serial);
948}
949
950/*-------------------------------------------------
951    DEVICE_GET_INFO( cia6526r1 )
952-------------------------------------------------*/
953
954DEVICE_GET_INFO(cia6526r1)
955{
956   switch (state)
886   /* now update the MAME timer */
887   if ((m_mode & 0x01) && ((m_mode & (which ? 0x60 : 0x20)) == 0x00))
957888   {
958      /* --- the following bits of info are returned as 64-bit signed integers --- */
959      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(cia_state);            break;
960      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
961
962      /* --- the following bits of info are returned as pointers to data or functions --- */
963      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(cia);      break;
964      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
965      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(cia);      break;
966//      case DEVINFO_FCT_VALIDITY_CHECK:                info->validity_check = DEVICE_VALIDITY_CHECK_NAME(cia); break;
967
968      /* --- the following bits of info are returned as NULL-terminated strings --- */
969      case DEVINFO_STR_NAME:                     strcpy(info->s, "MOS6526 rev1");         break;
970      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "MOS6526");               break;
971      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                  break;
972      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
973      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
889      /* timer is on and is connected to clock */
890      attotime period = attotime_mul(ATTOTIME_IN_HZ(m_clock), (m_count ? m_count : 0x10000));
891      timer_adjust_oneshot(m_timer, period, which);
974892   }
975}
976
977/*-------------------------------------------------
978    DEVICE_GET_INFO( cia8520 )
979-------------------------------------------------*/
980
981DEVICE_GET_INFO(cia6526r2)
982{
983   switch (state)
893   else
984894   {
985      /* --- the following bits of info are returned as NULL-terminated strings --- */
986      case DEVINFO_STR_NAME:                     strcpy(info->s, "MOS6526 rev2");         break;
987      default:   DEVICE_GET_INFO_CALL(cia6526r1);   break;
895      /* timer is off or not connected to clock */
896      timer_adjust_oneshot(m_timer, attotime_never, which);
988897   }
989898}
990899
991900/*-------------------------------------------------
992    DEVICE_GET_INFO( cia8520 )
901    get_count - get the count for a given CIA
902    timer
993903-------------------------------------------------*/
994904
995DEVICE_GET_INFO(cia8520)
905UINT16 mos6526_device::cia_timer::get_count()
996906{
997   switch (state)
907   UINT16 count;
908
909   if (is_timer_active(m_timer))
998910   {
999      /* --- the following bits of info are returned as NULL-terminated strings --- */
1000      case DEVINFO_STR_NAME:                     strcpy(info->s, "MOS8520");            break;
1001      default:   DEVICE_GET_INFO_CALL(cia6526r1);   break;
911      UINT16 current_count = attotime_to_double(attotime_mul(timer_timeelapsed(m_timer), m_clock));
912      count = m_count - MIN(m_count, current_count);
1002913   }
914   else
915   {
916      count = m_count;
917   }
918
919   return count;
1003920}
1004921
1005DEFINE_LEGACY_DEVICE(MOS6526R1, cia6526r1);
1006DEFINE_LEGACY_DEVICE(MOS6526R2, cia6526r2);
1007DEFINE_LEGACY_DEVICE(MOS8520, cia8520);
922/***************************************************************************
923    TRAMPOLINES
924***************************************************************************/
925
926void cia_set_port_mask_value(running_device *device, int port, int data) { downcast<mos6526_device *>(device)->set_port_mask_value(port, data); }
927
928READ8_DEVICE_HANDLER( mos6526_r ) { return downcast<mos6526_device *>(device)->reg_r(offset); }
929WRITE8_DEVICE_HANDLER( mos6526_w ) { downcast<mos6526_device *>(device)->reg_w(offset, data); }
930
931READ8_DEVICE_HANDLER( mos6526_pa_r ) { return downcast<mos6526_device *>(device)->pa_r(offset); }
932READ8_DEVICE_HANDLER( mos6526_pb_r ) { return downcast<mos6526_device *>(device)->pb_r(offset); }
933
934READ_LINE_DEVICE_HANDLER( mos6526_irq_r ) { return downcast<mos6526_device *>(device)->irq_r(); }
935
936WRITE_LINE_DEVICE_HANDLER( mos6526_tod_w ) { downcast<mos6526_device *>(device)->tod_w(state); }
937
938READ_LINE_DEVICE_HANDLER( mos6526_cnt_r ) { return downcast<mos6526_device *>(device)->cnt_r(); }
939WRITE_LINE_DEVICE_HANDLER( mos6526_cnt_w ) { downcast<mos6526_device *>(device)->cnt_w(state); }
940
941READ_LINE_DEVICE_HANDLER( mos6526_sp_r ) { return downcast<mos6526_device *>(device)->sp_r(); }
942WRITE_LINE_DEVICE_HANDLER( mos6526_sp_w ) { downcast<mos6526_device *>(device)->sp_w(state); }
943
944WRITE_LINE_DEVICE_HANDLER( mos6526_flag_w ) { downcast<mos6526_device *>(device)->flag_w(state); }
src/emu/machine/74123.c
r8892r8893
1414#define   LOG      (0)
1515
1616
17typedef struct _ttl74123_t ttl74123_t;
17//**************************************************************************
18//  DEVICE CONFIGURATION
19//**************************************************************************
1820
19struct _ttl74123_t
21//-------------------------------------------------
22//  ttl74123_device_config - constructor
23//-------------------------------------------------
24
25ttl74123_device_config::ttl74123_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
26    : device_config(mconfig, static_alloc_device_config, "TTL74123", tag, owner, clock)
2027{
21   const ttl74123_config *intf;
28}
2229
23   UINT8 a;         /* pin 1/9 */
24   UINT8 b;         /* pin 2/10 */
25   UINT8 clear;      /* pin 3/11 */
26   emu_timer *timer;
27};
2830
29/* ----------------------------------------------------------------------- */
31//-------------------------------------------------
32//  static_alloc_device_config - allocate a new
33//  configuration object
34//-------------------------------------------------
3035
31INLINE ttl74123_t *get_safe_token(running_device *device) {
32   assert( device != NULL );
33   assert( device->type() == TTL74123 );
34   return ( ttl74123_t * ) downcast<legacy_device_base *>(device)->token();
36device_config *ttl74123_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
37{
38    return global_alloc(ttl74123_device_config(mconfig, tag, owner, clock));
3539}
3640
3741
38static attotime compute_duration(ttl74123_t *chip)
42//-------------------------------------------------
43//  alloc_device - allocate a new device object
44//-------------------------------------------------
45
46device_t *ttl74123_device_config::alloc_device(running_machine &machine) const
3947{
40   double duration;
48    return auto_alloc(&machine, ttl74123_device(machine, *this));
49}
4150
42   switch (chip->intf->connection_type)
43   {
44   case TTL74123_NOT_GROUNDED_NO_DIODE:
45      duration = 0.28 * chip->intf->res * chip->intf->cap * (1.0 + (700.0 / chip->intf->res));
46      break;
4751
48   case TTL74123_NOT_GROUNDED_DIODE:
49      duration = 0.25 * chip->intf->res * chip->intf->cap * (1.0 + (700.0 / chip->intf->res));
50      break;
52//-------------------------------------------------
53//  device_config_complete - perform any
54//  operations now that the configuration is
55//  complete
56//-------------------------------------------------
5157
52   case TTL74123_GROUNDED:
53   default:
54      if (chip->intf->cap < CAP_U(0.1))
55         /* this is really a curve - a very flat one in the 0.1uF-.01uF range */
56         duration = 0.32 * chip->intf->res * chip->intf->cap;
57      else
58         duration = 0.33 * chip->intf->res * chip->intf->cap;
59      break;
60   }
58void ttl74123_device_config::device_config_complete()
59{
6160
62   return double_to_attotime(duration);
6361}
6462
6563
66static int timer_running(ttl74123_t *chip)
67{
68   return (attotime_compare(timer_timeleft(chip->timer), attotime_zero) > 0) &&
69         (attotime_compare(timer_timeleft(chip->timer), attotime_never) != 0);
70}
7164
65//**************************************************************************
66//  LIVE DEVICE
67//**************************************************************************
7268
73static TIMER_CALLBACK( output_callback )
69//-------------------------------------------------
70//  ttl74123_device - constructor
71//-------------------------------------------------
72
73ttl74123_device::ttl74123_device(running_machine &_machine, const ttl74123_device_config &config)
74    : device_t(_machine, config),
75      m_config(config)
7476{
75   running_device *device = (running_device *)ptr;
76   ttl74123_t *chip = get_safe_token(device);
7777
78   chip->intf->output_changed_cb(device, 0, param);
7978}
8079
8180
82static void set_output(running_device *device)
81//-------------------------------------------------
82//  device_start - device-specific startup
83//-------------------------------------------------
84
85void ttl74123_device::device_start()
8386{
84   ttl74123_t *chip = get_safe_token(device);
85   int output = timer_running(chip);
87    m_timer = timer_alloc(&m_machine, clear_callback, (void *)this);
8688
87   timer_set( device->machine, attotime_zero, (void *) device, output, output_callback );
89   /* start with the defaults */
90    m_a = m_config.m_a;
91    m_b = m_config.m_b;
92    m_clear = m_config.m_clear;
8893
89   if (LOG) logerror("74123 %s:  Output: %d\n", device->tag(), output);
94   /* register for state saving */
95    state_save_register_device_item(this, 0, m_a);
96    state_save_register_device_item(this, 0, m_b);
97    state_save_register_device_item(this, 0, m_clear);
9098}
9199
92100
93static TIMER_CALLBACK( clear_callback )
101//-------------------------------------------------
102//  device_reset - device-specific reset
103//-------------------------------------------------
104
105void ttl74123_device::device_reset()
94106{
95   running_device *device = (running_device *)ptr;
96   ttl74123_t *chip = get_safe_token(device);
97   int output = timer_running(chip);
98
99   chip->intf->output_changed_cb(device, 0, output);
107   set_output();
100108}
101109
102110
103111
112/*-------------------------------------------------
113    compute_duration
114-------------------------------------------------*/
104115
105static void start_pulse(running_device *device)
116attotime ttl74123_device::compute_duration()
106117{
107   ttl74123_t *chip = get_safe_token(device);
118 double duration;
108119
109   attotime duration = compute_duration(chip);
120    switch (m_config.m_connection_type)
121    {
122    case TTL74123_NOT_GROUNDED_NO_DIODE:
123        duration = 0.28 * m_config.m_res * m_config.m_cap * (1.0 + (700.0 / m_config.m_res));
124        break;
110125
111   if (timer_running(chip))
112   {
113      /* retriggering, but not if we are called to quickly */
114      attotime delay_time = attotime_make(0, ATTOSECONDS_PER_SECOND * chip->intf->cap * 220);
126    case TTL74123_NOT_GROUNDED_DIODE:
127        duration = 0.25 * m_config.m_res * m_config.m_cap * (1.0 + (700.0 / m_config.m_res));
128        break;
115129
116      if (attotime_compare(timer_timeelapsed(chip->timer), delay_time) >= 0)
117      {
118         timer_adjust_oneshot(chip->timer, duration, 0);
130    case TTL74123_GROUNDED:
131    default:
132        if (m_config.m_cap < CAP_U(0.1))
133        {
134            /* this is really a curve - a very flat one in the 0.1uF-.01uF range */
135            duration = 0.32 * m_config.m_res * m_config.m_cap;
136        }
137        else
138        {
139            duration = 0.33 * m_config.m_res * m_config.m_cap;
140        }
141        break;
142    }
119143
120         if (LOG) logerror("74123 %s:  Retriggering pulse.  Duration: %f\n", device->tag(), attotime_to_double(duration));
121      }
122      else
123      {
124         if (LOG) logerror("74123 %s:  Retriggering failed.\n", device->tag());
125      }
126   }
127   else
128   {
129      /* starting */
130      timer_adjust_oneshot(chip->timer, duration, 0);
144    return double_to_attotime(duration);
145}
131146
132      set_output(device);
133147
134      if (LOG) logerror("74123 %s:  Starting pulse.  Duration: %f\n", device->tag(), attotime_to_double(duration));
135   }
148/*-------------------------------------------------
149    timer_running
150-------------------------------------------------*/
151
152int ttl74123_device::timer_running()
153{
154    return (attotime_compare(timer_timeleft(m_timer), attotime_zero) > 0) &&
155           (attotime_compare(timer_timeleft(m_timer), attotime_never) != 0);
136156}
137157
138158
139WRITE8_DEVICE_HANDLER( ttl74123_a_w )
159/*-------------------------------------------------
160    TIMER_CALLBACK( output_callback )
161-------------------------------------------------*/
162
163TIMER_CALLBACK( ttl74123_device::output_callback )
140164{
141   ttl74123_t *chip = get_safe_token(device);
165    ttl74123_device *dev = reinterpret_cast<ttl74123_device*>(ptr);
166    dev->output(param);
167}
142168
143   /* start/regtrigger pulse if B=HI and falling edge on A (while clear is HI) */
144   if (!data && chip->a && chip->b && chip->clear)
145      start_pulse(device);
146
147   chip->a = data;
169void ttl74123_device::output(INT32 param)
170{
171    m_config.m_output_changed_cb(this, 0, param);
148172}
149173
150174
151WRITE8_DEVICE_HANDLER( ttl74123_b_w )
175void ttl74123_device::set_output()
152176{
153   ttl74123_t *chip = get_safe_token(device);
177    int output = timer_running();
154178
155   /* start/regtrigger pulse if A=LO and rising edge on B (while clear is HI) */
156   if (data && !chip->b && !chip->a && chip->clear)
157      start_pulse(device);
179    timer_set( &m_machine, attotime_zero, (void *)this, output, output_callback );
158180
159   chip->b = data;
181    if (LOG) logerror("74123 %s:  Output: %d\n", tag(), output);
160182}
161183
162184
163WRITE8_DEVICE_HANDLER( ttl74123_clear_w )
185TIMER_CALLBACK( ttl74123_device::clear_callback )
164186{
165   ttl74123_t *chip = get_safe_token(device);
187    ttl74123_device *dev = reinterpret_cast<ttl74123_device*>(ptr);
188    dev->clear();
189}
166190
167   /* start/regtrigger pulse if B=HI and A=LO and rising edge on clear */
168   if (data && !chip->a && chip->b && !chip->clear)
169      start_pulse(device);
170   else if (!data)    /* clear the output  */
171   {
172      timer_adjust_oneshot(chip->timer, attotime_zero, 0);
191void ttl74123_device::clear()
192{
193    int output = timer_running();
173194
174      if (LOG) logerror("74123 #%s:  Cleared\n", device->tag() );
175   }
176   chip->clear = data;
195    m_config.m_output_changed_cb(this, 0, output);
177196}
178197
179WRITE8_DEVICE_HANDLER( ttl74123_reset_w )
198
199void ttl74123_device::start_pulse()
180200{
181   set_output(device);
201    attotime duration = compute_duration();
202
203    if(timer_running())
204    {
205        /* retriggering, but not if we are called to quickly */
206        attotime delay_time = attotime_make(0, ATTOSECONDS_PER_SECOND * m_config.m_cap * 220);
207
208        if(attotime_compare(timer_timeelapsed(m_timer), delay_time) >= 0)
209        {
210            timer_adjust_oneshot(m_timer, duration, 0);
211
212            if (LOG) logerror("74123 %s:  Retriggering pulse.  Duration: %f\n", tag(), attotime_to_double(duration));
213        }
214        else
215        {
216            if (LOG) logerror("74123 %s:  Retriggering failed.\n", tag());
217        }
218    }
219    else
220    {
221        /* starting */
222        timer_adjust_oneshot(m_timer, duration, 0);
223
224        set_output();
225
226        if (LOG) logerror("74123 %s:  Starting pulse.  Duration: %f\n", tag(), attotime_to_double(duration));
227    }
182228}
183229
184/* ----------------------------------------------------------------------- */
185230
186/* device interface */
231WRITE8_DEVICE_HANDLER( ttl74123_a_w )
232{
233    ttl74123_device *dev = reinterpret_cast<ttl74123_device *>(device);
234    dev->a_w(data);
235}
187236
188static DEVICE_START( ttl74123 )
237void ttl74123_device::a_w(UINT8 data)
189238{
190   ttl74123_t *chip = get_safe_token(device);
239    /* start/regtrigger pulse if B=HI and falling edge on A (while clear is HI) */
240    if (!data && m_a && m_b && m_clear)
241    {
242        start_pulse();
243    }
191244
192   /* validate arguments */
193   chip->intf = (ttl74123_config *)device->baseconfig().static_config();
245    m_a = data;
246}
194247
195   assert_always(chip->intf, "No interface specified");
196   assert_always((chip->intf->connection_type != TTL74123_GROUNDED) || (chip->intf->cap >= CAP_U(0.01)), "Only capacitors >= 0.01uF supported for GROUNDED type");
197   assert_always(chip->intf->cap >= CAP_P(1000), "Only capacitors >= 1000pF supported ");
198248
199   chip->timer = timer_alloc(device->machine, clear_callback, (void *) device);
249WRITE8_DEVICE_HANDLER( ttl74123_b_w )
250{
251    ttl74123_device *dev = reinterpret_cast<ttl74123_device *>(device);
252    dev->b_w(data);
253}
200254
201   /* start with the defaults */
202   chip->a = chip->intf->a;
203    chip->b = chip->intf->b;
204   chip->clear = chip->intf->clear;
255void ttl74123_device::b_w(UINT8 data)
256{
257    /* start/regtrigger pulse if A=LO and rising edge on B (while clear is HI) */
258    if (data && !m_b && !m_a && m_clear)
259    {
260        start_pulse();
261    }
205262
206   /* register for state saving */
207   state_save_register_device_item(device, 0, chip->a);
208   state_save_register_device_item(device, 0, chip->b);
209   state_save_register_device_item(device, 0, chip->clear);
263    m_b = data;
210264}
211265
212266
213static DEVICE_RESET( ttl74123 )
267WRITE8_DEVICE_HANDLER( ttl74123_clear_w )
214268{
215   set_output(device);
269    ttl74123_device *dev = reinterpret_cast<ttl74123_device *>(device);
270    dev->clear_w(data);
216271}
217272
218
219DEVICE_GET_INFO( ttl74123 )
273void ttl74123_device::clear_w(UINT8 data)
220274{
221   switch (state)
222   {
223      /* --- the following bits of info are returned as 64-bit signed integers --- */
224      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(ttl74123_t);                  break;
225      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                              break;
275    /* start/regtrigger pulse if B=HI and A=LO and rising edge on clear */
276    if (data && !m_a && m_b && !m_clear)
277    {
278        start_pulse();
279    }
280    else if (!data)     /* clear the output  */
281    {
282        timer_adjust_oneshot(m_timer, attotime_zero, 0);
226283
227      /* --- the following bits of info are returned as pointers to data or functions --- */
228      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(ttl74123);         break;
229      case DEVINFO_FCT_STOP:                     /* Nothing */                              break;
230      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(ttl74123);         break;
284        if (LOG) logerror("74123 #%s:  Cleared\n", tag() );
285    }
286    m_clear = data;
287}
231288
232      /* --- the following bits of info are returned as NULL-terminated strings --- */
233      case DEVINFO_STR_NAME:                     strcpy(info->s, "74123");                     break;
234      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "TTL");                        break;
235      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                        break;
236      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                     break;
237      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
238   }
289WRITE8_DEVICE_HANDLER( ttl74123_reset_w )
290{
291    ttl74123_device *dev = reinterpret_cast<ttl74123_device *>(device);
292    dev->reset_w();
239293}
240294
241DEFINE_LEGACY_DEVICE(TTL74123, ttl74123);
295void ttl74123_device::reset_w()
296{
297    set_output();
298}
299
300const device_type TTL74123 = ttl74123_device_config::static_alloc_device_config;
src/emu/machine/6526cia.h
r8892r8893
3030
3131**********************************************************************/
3232
33#pragma once
34
3335#ifndef __6526CIA_H__
3436#define __6526CIA_H__
3537
36#include "devcb.h"
38#include "emu.h"
3739
38/***************************************************************************
39    MACROS
40***************************************************************************/
4140
42DECLARE_LEGACY_DEVICE(MOS6526R1, cia6526r1);
43DECLARE_LEGACY_DEVICE(MOS6526R2, cia6526r2);
44DECLARE_LEGACY_DEVICE(MOS8520, cia8520);
4541
42   //**************************************************************************
43//  INTERFACE CONFIGURATION MACROS
44//**************************************************************************
45
4646#define MDRV_MOS6526R1_ADD(_tag, _clock, _config) \
4747   MDRV_DEVICE_ADD(_tag, MOS6526R1, _clock) \
4848   MDRV_DEVICE_CONFIG(_config)
r8892r8893
6565    TYPE DEFINITIONS
6666***************************************************************************/
6767
68typedef struct _mos6526_interface mos6526_interface;
69struct _mos6526_interface
68
69// ======================> mos6526_interface
70
71struct mos6526_interface
7072{
71   int tod_clock;
73   int m_tod_clock;
7274
73   devcb_write_line   out_irq_func;
74   devcb_write_line   out_pc_func;
75   devcb_write_line   out_cnt_func;
76   devcb_write_line   out_sp_func;
75   devcb_write_line   m_out_irq_func;
76   devcb_write_line   m_out_pc_func;
77   devcb_write_line   m_out_cnt_func;
78   devcb_write_line   m_out_sp_func;
7779
78   devcb_read8         in_pa_func;
79   devcb_write8      out_pa_func;
80   devcb_read8         m_in_pa_func;
81   devcb_write8      m_out_pa_func;
8082
81   devcb_read8         in_pb_func;
82   devcb_write8      out_pb_func;
83   devcb_read8         m_in_pb_func;
84   devcb_write8      m_out_pb_func;
8385};
8486
87
88
89// ======================> mos6526_device_config
90
91class mos6526_device_config :   public device_config,
92                                public mos6526_interface
93{
94    friend class mos6526_device;
95
96    // construction/destruction
97    mos6526_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
98
99public:
100    // allocators
101    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
102    virtual device_t *alloc_device(running_machine &machine) const;
103
104protected:
105    // device_config overrides
106    virtual void device_config_complete();
107};
108
109
110
111// ======================> mos6526_device
112
113class mos6526_device :  public device_t
114{
115    friend class mos6526_device_config;
116    friend class dart_channel;
117
118    // construction/destruction
119    mos6526_device(running_machine &_machine, const mos6526_device_config &_config);
120
121public:
122   UINT8 reg_r(UINT8 offset);
123   void reg_w(UINT8 offset, UINT8 data);
124
125   /* port access */
126   UINT8 pa_r(UINT8 offset) { return (m_port[0].m_latch | ~m_port[0].m_ddr); }
127   UINT8 pb_r(UINT8 offset) { return (m_port[1].m_latch | ~m_port[1].m_ddr); }
128
129   /* interrupt request */
130   UINT8 irq_r() { return m_irq; }
131
132   /* time of day clock */
133   void tod_w(UINT8 state) { if(state) clock_tod(); }
134
135   /* serial counter */
136   UINT8 cnt_r() { return m_cnt; }
137   void cnt_w(UINT8 state);
138
139   /* serial port */
140   UINT8 sp_r() { return m_sp; }
141   void sp_w(UINT8 state) { m_sp = state; }
142
143   /* flag */
144   void flag_w(UINT8 state);
145
146   /* port mask */
147   void set_port_mask_value(int port, int data);
148
149protected:
150    // device-level overrides
151    virtual void device_start();
152    virtual void device_reset();
153    virtual void device_post_load() { }
154    virtual void device_clock_changed() { }
155
156   static TIMER_CALLBACK( timer_proc );
157   static TIMER_CALLBACK( clock_tod_callback );
158
159private:
160
161   void update_pc();
162   void update_interrupts();
163   void timer_bump(int timer);
164   void timer_underflow(int timer);
165   void increment();
166   void clock_tod();
167
168   struct cia_timer
169   {
170   public:
171      cia_timer() { }
172
173      UINT16 get_count();
174      void update(int which, INT32 new_count);
175
176      UINT32         m_clock;
177      UINT16         m_latch;
178      UINT16         m_count;
179      UINT8         m_mode;
180      UINT8         m_irq;
181      emu_timer*      m_timer;
182      mos6526_device*   m_cia;
183   };
184
185   struct cia_port
186   {
187      UINT8      m_ddr;
188      UINT8      m_latch;
189      UINT8      m_in;
190      UINT8      m_out;
191      devcb_resolved_read8   m_read;
192      devcb_resolved_write8   m_write;
193      UINT8      m_mask_value; /* in READ operation the value can be forced by a extern electric circuit */
194   };
195
196   devcb_resolved_write_line m_out_irq_func;
197   devcb_resolved_write_line m_out_pc_func;
198   devcb_resolved_write_line m_out_cnt_func;
199   devcb_resolved_write_line m_out_sp_func;
200
201   cia_port      m_port[2];
202   cia_timer      m_timer[2];
203
204   /* Time Of the Day clock (TOD) */
205   UINT32         m_tod;
206   UINT32         m_tod_latch;
207   UINT8         m_tod_latched;
208   UINT8         m_tod_running;
209   UINT32         m_alarm;
210
211   /* Interrupts */
212   UINT8         m_icr;
213   UINT8         m_ics;
214   UINT8         m_irq;
215   int            m_flag;
216
217   /* Serial */
218   UINT8         m_loaded;
219   UINT8         m_sdr;
220   UINT8         m_sp;
221   UINT8         m_cnt;
222   UINT8         m_shift;
223   UINT8         m_serial;
224
225    const mos6526_device_config &m_config;
226};
227
228
229// device type definition
230extern const device_type MOS6526R1;
231extern const device_type MOS6526R2;
232extern const device_type MOS8520;
233
234
235
85236/***************************************************************************
86237    FUNCTION PROTOTYPES
87238***************************************************************************/
src/emu/machine/74123.h
r8892r8893
4343
4444*****************************************************************************/
4545
46#ifndef TTL74123_H
47#define TTL74123_H
46#pragma once
4847
49#include "devlegcy.h"
48#ifndef __TTL74123_H__
49#define __TTL74123_H__
5050
51DECLARE_LEGACY_DEVICE(TTL74123, ttl74123);
51#include "emu.h"
5252
53
54
55/***************************************************************************
56    DEVICE CONFIGURATION MACROS
57***************************************************************************/
58
5359#define MDRV_TTL74123_ADD(_tag, _config) \
5460   MDRV_DEVICE_ADD(_tag, TTL74123, 0) \
5561   MDRV_DEVICE_CONFIG(_config)
5662
57
5863/* constants for the different ways the cap/res can be connected.
5964   This determines the formula for calculating the pulse width */
6065#define TTL74123_NOT_GROUNDED_NO_DIODE      (1)
r8892r8893
6267#define TTL74123_GROUNDED               (3)
6368
6469
65typedef struct _ttl74123_config ttl74123_config;
66struct _ttl74123_config
70/***************************************************************************
71    TYPE DEFINITIONS
72***************************************************************************/
73
74
75// ======================> ttl74123_interface
76
77struct ttl74123_interface
6778{
68   int connection_type;   /* the hook up type - one of the constants above */
69   double res;            /* resistor connected to RCext */
70   double cap;            /* capacitor connected to Cext and RCext */
71   int a;               /* initial/constant value of the A pin */
72   int b;               /* initial/constant value of the B pin */
73   int clear;            /* initial/constant value of the Clear pin */
74   write8_device_func   output_changed_cb;
79    int m_connection_type;  /* the hook up type - one of the constants above */
80    double m_res;           /* resistor connected to RCext */
81    double m_cap;           /* capacitor connected to Cext and RCext */
82    int m_a;                /* initial/constant value of the A pin */
83    int m_b;                /* initial/constant value of the B pin */
84    int m_clear;            /* initial/constant value of the Clear pin */
85    write8_device_func  m_output_changed_cb;
7586};
7687
77/* write inputs */
7888
89
90// ======================> ttl74123_device_config
91
92class ttl74123_device_config : public device_config,
93                              public ttl74123_interface
94{
95    friend class ttl74123_device;
96
97    // construction/destruction
98    ttl74123_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
99
100public:
101    // allocators
102    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
103    virtual device_t *alloc_device(running_machine &machine) const;
104
105protected:
106    // device_config overrides
107    virtual void device_config_complete();
108};
109
110
111
112// ======================> ttl74123_device
113
114class ttl74123_device :  public device_t
115{
116    friend class ttl74123_device_config;
117
118    // construction/destruction
119    ttl74123_device(running_machine &_machine, const ttl74123_device_config &_config);
120
121public:
122
123    void a_w(UINT8 data);
124    void b_w(UINT8 data);
125    void clear_w(UINT8 data);
126    void reset_w();
127
128protected:
129    // device-level overrides
130    virtual void device_start();
131    virtual void device_reset();
132    virtual void device_post_load() { }
133    virtual void device_clock_changed() { }
134
135    static TIMER_CALLBACK( output_callback );
136    static TIMER_CALLBACK( clear_callback );
137
138private:
139
140    int timer_running();
141    void start_pulse();
142    void output(INT32 param);
143    void set_output();
144    attotime compute_duration();
145    void clear();
146
147    UINT8 m_a;            /* pin 1/9 */
148    UINT8 m_b;            /* pin 2/10 */
149    UINT8 m_clear;        /* pin 3/11 */
150    emu_timer *m_timer;
151
152    const ttl74123_device_config &m_config;
153};
154
155
156// device type definition
157extern const device_type TTL74123;
158
159
160
161/***************************************************************************
162    PROTOTYPES
163***************************************************************************/
164
79165WRITE8_DEVICE_HANDLER( ttl74123_a_w );
80166WRITE8_DEVICE_HANDLER( ttl74123_b_w );
81167WRITE8_DEVICE_HANDLER( ttl74123_clear_w );
168WRITE8_DEVICE_HANDLER( ttl74123_reset_w ); /* reset the latch */
82169
83/* reset the latch */
84
85WRITE8_DEVICE_HANDLER( ttl74123_reset_w );
86
87170#endif
src/emu/machine/6821pia.c
r8892r8893
99
1010
1111/***************************************************************************
12    TYPE DEFINITIONS
13***************************************************************************/
14
15typedef struct _pia6821_state pia6821_state;
16struct _pia6821_state
17{
18   devcb_resolved_read8 in_a_func;
19   devcb_resolved_read8 in_b_func;
20   devcb_resolved_read_line in_ca1_func;
21   devcb_resolved_read_line in_cb1_func;
22   devcb_resolved_read_line in_ca2_func;
23   devcb_resolved_read_line in_cb2_func;
24   devcb_resolved_write8 out_a_func;
25   devcb_resolved_write8 out_b_func;
26   devcb_resolved_write_line out_ca2_func;
27   devcb_resolved_write_line out_cb2_func;
28   devcb_resolved_write_line irq_a_func;
29   devcb_resolved_write_line irq_b_func;
30
31   UINT8 in_a;
32   UINT8 in_ca1;
33   UINT8 in_ca2;
34   UINT8 out_a;
35   UINT8 out_ca2;
36   UINT8 port_a_z_mask;
37   UINT8 ddr_a;
38   UINT8 ctl_a;
39   UINT8 irq_a1;
40   UINT8 irq_a2;
41   UINT8 irq_a_state;
42
43   UINT8 in_b;
44   UINT8 in_cb1;
45   UINT8 in_cb2;
46   UINT8 out_b;
47   UINT8 out_cb2;
48   UINT8 last_out_cb2_z;
49   UINT8 ddr_b;
50   UINT8 ctl_b;
51   UINT8 irq_b1;
52   UINT8 irq_b2;
53   UINT8 irq_b_state;
54
55   /* variables that indicate if access a line externally -
56       used to for logging purposes ONLY */
57   UINT8 in_a_pushed;
58   UINT8 out_a_needs_pulled;
59   UINT8 in_ca1_pushed;
60   UINT8 in_ca2_pushed;
61   UINT8 out_ca2_needs_pulled;
62   UINT8 in_b_pushed;
63   UINT8 out_b_needs_pulled;
64   UINT8 in_cb1_pushed;
65   UINT8 in_cb2_pushed;
66   UINT8 out_cb2_needs_pulled;
67   UINT8 logged_port_a_not_connected;
68   UINT8 logged_port_b_not_connected;
69   UINT8 logged_ca1_not_connected;
70   UINT8 logged_ca2_not_connected;
71   UINT8 logged_cb1_not_connected;
72   UINT8 logged_cb2_not_connected;
73};
74
75
76/***************************************************************************
7712    MACROS
7813***************************************************************************/
7914
r8892r8893
10136
10237
10338/***************************************************************************
104    PROTOTYPES
39    IMPLEMENTATION
10540***************************************************************************/
106/***************************************************************************
107    INLINE FUNCTIONS
108***************************************************************************/
10941
110INLINE pia6821_state *get_token(running_device *device)
42//**************************************************************************
43//  DEVICE CONFIGURATION
44//**************************************************************************
45
46//-------------------------------------------------
47//  pia6821_device_config - constructor
48//-------------------------------------------------
49
50pia6821_device_config::pia6821_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
51    : device_config(mconfig, static_alloc_device_config, "PIA6821", tag, owner, clock)
11152{
112   assert(device != NULL);
113   assert((device->type() == PIA6821) || (device->type() == PIA6822));
114   return (pia6821_state *) downcast<legacy_device_base *>(device)->token();
11553}
11654
11755
118INLINE const pia6821_interface *get_interface(running_device *device)
56//-------------------------------------------------
57//  static_alloc_device_config - allocate a new
58//  configuration object
59//-------------------------------------------------
60
61device_config *pia6821_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
11962{
120   assert(device != NULL);
121   assert((device->type() == PIA6821) || (device->type() == PIA6822));
122   return (const pia6821_interface *) device->baseconfig().static_config();
63    return global_alloc(pia6821_device_config(mconfig, tag, owner, clock));
12364}
12465
12566
126/***************************************************************************
127    IMPLEMENTATION
128***************************************************************************/
67//-------------------------------------------------
68//  alloc_device - allocate a new device object
69//-------------------------------------------------
12970
130/*-------------------------------------------------
131    DEVICE_START( pia6821 )
132-------------------------------------------------*/
71device_t *pia6821_device_config::alloc_device(running_machine &machine) const
72{
73    return auto_alloc(&machine, pia6821_device(machine, *this));
74}
13375
134static DEVICE_START( pia6821 )
76
77//-------------------------------------------------
78//  device_config_complete - perform any
79//  operations now that the configuration is
80//  complete
81//-------------------------------------------------
82
83void pia6821_device_config::device_config_complete()
13584{
136   pia6821_state *p = get_token(device);
137   const pia6821_interface *intf = get_interface(device);
85   // inherit a copy of the static data
86   const pia6821_interface *intf = reinterpret_cast<const pia6821_interface *>(static_config());
87   if (intf != NULL)
88      *static_cast<pia6821_interface *>(this) = *intf;
13889
139   /* clear structure */
140   memset(p, 0, sizeof(*p));
90   // or initialize to defaults if none provided
91   else
92   {
93       memset(&m_in_a_func, 0, sizeof(m_in_a_func));
94       memset(&m_in_b_func, 0, sizeof(m_in_b_func));
95       memset(&m_in_ca1_func, 0, sizeof(m_in_ca1_func));
96       memset(&m_in_cb1_func, 0, sizeof(m_in_cb1_func));
97       memset(&m_in_ca2_func, 0, sizeof(m_in_ca2_func));
98       memset(&m_in_cb2_func, 0, sizeof(m_in_cb2_func));
99       memset(&m_out_a_func, 0, sizeof(m_out_a_func));
100       memset(&m_out_b_func, 0, sizeof(m_out_b_func));
101       memset(&m_out_ca2_func, 0, sizeof(m_out_ca2_func));
102       memset(&m_out_cb2_func, 0, sizeof(m_out_cb2_func));
103       memset(&m_irq_a_func, 0, sizeof(m_irq_a_func));
104       memset(&m_irq_b_func, 0, sizeof(m_irq_b_func));
105   }
106}
141107
108
109
110//**************************************************************************
111//  LIVE DEVICE
112//**************************************************************************
113
114const device_type PIA6821 = pia6821_device_config::static_alloc_device_config;
115
116//-------------------------------------------------
117//  pia6821_device - constructor
118//-------------------------------------------------
119
120pia6821_device::pia6821_device(running_machine &_machine, const pia6821_device_config &config)
121    : device_t(_machine, config),
122      m_config(config)
123{
124
125}
126
127//-------------------------------------------------
128//  device_start - device-specific startup
129//-------------------------------------------------
130
131void pia6821_device::device_start()
132{
142133   /* resolve callbacks */
143   devcb_resolve_read8(&p->in_a_func, &intf->in_a_func, device);
144   devcb_resolve_read8(&p->in_b_func, &intf->in_b_func, device);
145   devcb_resolve_read_line(&p->in_ca1_func, &intf->in_ca1_func, device);
146   devcb_resolve_read_line(&p->in_cb1_func, &intf->in_cb1_func, device);
147   devcb_resolve_read_line(&p->in_ca2_func, &intf->in_ca2_func, device);
148   devcb_resolve_read_line(&p->in_cb2_func, &intf->in_cb2_func, device);
149   devcb_resolve_write8(&p->out_a_func, &intf->out_a_func, device);
150   devcb_resolve_write8(&p->out_b_func, &intf->out_b_func, device);
151   devcb_resolve_write_line(&p->out_ca2_func, &intf->out_ca2_func, device);
152   devcb_resolve_write_line(&p->out_cb2_func, &intf->out_cb2_func, device);
153   devcb_resolve_write_line(&p->irq_a_func, &intf->irq_a_func, device);
154   devcb_resolve_write_line(&p->irq_b_func, &intf->irq_b_func, device);
134    devcb_resolve_read8(&m_in_a_func, &m_config.m_in_a_func, this);
135    devcb_resolve_read8(&m_in_b_func, &m_config.m_in_b_func, this);
136    devcb_resolve_read_line(&m_in_ca1_func, &m_config.m_in_ca1_func, this);
137    devcb_resolve_read_line(&m_in_cb1_func, &m_config.m_in_cb1_func, this);
138    devcb_resolve_read_line(&m_in_ca2_func, &m_config.m_in_ca2_func, this);
139    devcb_resolve_read_line(&m_in_cb2_func, &m_config.m_in_cb2_func, this);
140    devcb_resolve_write8(&m_out_a_func, &m_config.m_out_a_func, this);
141    devcb_resolve_write8(&m_out_b_func, &m_config.m_out_b_func, this);
142    devcb_resolve_write_line(&m_out_ca2_func, &m_config.m_out_ca2_func, this);
143    devcb_resolve_write_line(&m_out_cb2_func, &m_config.m_out_cb2_func, this);
144    devcb_resolve_write_line(&m_irq_a_func, &m_config.m_irq_a_func, this);
145    devcb_resolve_write_line(&m_irq_b_func, &m_config.m_irq_b_func, this);
155146
156   state_save_register_device_item(device, 0, p->in_a);
157   state_save_register_device_item(device, 0, p->in_ca1);
158   state_save_register_device_item(device, 0, p->in_ca2);
159   state_save_register_device_item(device, 0, p->out_a);
160   state_save_register_device_item(device, 0, p->out_ca2);
161   state_save_register_device_item(device, 0, p->port_a_z_mask);
162   state_save_register_device_item(device, 0, p->ddr_a);
163   state_save_register_device_item(device, 0, p->ctl_a);
164   state_save_register_device_item(device, 0, p->irq_a1);
165   state_save_register_device_item(device, 0, p->irq_a2);
166   state_save_register_device_item(device, 0, p->irq_a_state);
167   state_save_register_device_item(device, 0, p->in_b);
168   state_save_register_device_item(device, 0, p->in_cb1);
169   state_save_register_device_item(device, 0, p->in_cb2);
170   state_save_register_device_item(device, 0, p->out_b);
171   state_save_register_device_item(device, 0, p->out_cb2);
172   state_save_register_device_item(device, 0, p->last_out_cb2_z);
173   state_save_register_device_item(device, 0, p->ddr_b);
174   state_save_register_device_item(device, 0, p->ctl_b);
175   state_save_register_device_item(device, 0, p->irq_b1);
176   state_save_register_device_item(device, 0, p->irq_b2);
177   state_save_register_device_item(device, 0, p->irq_b_state);
178   state_save_register_device_item(device, 0, p->in_a_pushed);
179   state_save_register_device_item(device, 0, p->out_a_needs_pulled);
180   state_save_register_device_item(device, 0, p->in_ca1_pushed);
181   state_save_register_device_item(device, 0, p->in_ca2_pushed);
182   state_save_register_device_item(device, 0, p->out_ca2_needs_pulled);
183   state_save_register_device_item(device, 0, p->in_b_pushed);
184   state_save_register_device_item(device, 0, p->out_b_needs_pulled);
185   state_save_register_device_item(device, 0, p->in_cb1_pushed);
186   state_save_register_device_item(device, 0, p->in_cb2_pushed);
187   state_save_register_device_item(device, 0, p->out_cb2_needs_pulled);
147    state_save_register_device_item(this, 0, m_in_a);
148    state_save_register_device_item(this, 0, m_in_ca1);
149    state_save_register_device_item(this, 0, m_in_ca2);
150    state_save_register_device_item(this, 0, m_out_a);
151    state_save_register_device_item(this, 0, m_out_ca2);
152    state_save_register_device_item(this, 0, m_port_a_z_mask);
153    state_save_register_device_item(this, 0, m_ddr_a);
154    state_save_register_device_item(this, 0, m_ctl_a);
155    state_save_register_device_item(this, 0, m_irq_a1);
156    state_save_register_device_item(this, 0, m_irq_a2);
157    state_save_register_device_item(this, 0, m_irq_a_state);
158    state_save_register_device_item(this, 0, m_in_b);
159    state_save_register_device_item(this, 0, m_in_cb1);
160    state_save_register_device_item(this, 0, m_in_cb2);
161    state_save_register_device_item(this, 0, m_out_b);
162    state_save_register_device_item(this, 0, m_out_cb2);
163    state_save_register_device_item(this, 0, m_last_out_cb2_z);
164    state_save_register_device_item(this, 0, m_ddr_b);
165    state_save_register_device_item(this, 0, m_ctl_b);
166    state_save_register_device_item(this, 0, m_irq_b1);
167    state_save_register_device_item(this, 0, m_irq_b2);
168    state_save_register_device_item(this, 0, m_irq_b_state);
169    state_save_register_device_item(this, 0, m_in_a_pushed);
170    state_save_register_device_item(this, 0, m_out_a_needs_pulled);
171    state_save_register_device_item(this, 0, m_in_ca1_pushed);
172    state_save_register_device_item(this, 0, m_in_ca2_pushed);
173    state_save_register_device_item(this, 0, m_out_ca2_needs_pulled);
174    state_save_register_device_item(this, 0, m_in_b_pushed);
175    state_save_register_device_item(this, 0, m_out_b_needs_pulled);
176    state_save_register_device_item(this, 0, m_in_cb1_pushed);
177    state_save_register_device_item(this, 0, m_in_cb2_pushed);
178    state_save_register_device_item(this, 0, m_out_cb2_needs_pulled);
188179}
189180
190181
191/*-------------------------------------------------
192    DEVICE_RESET( pia6821 )
193-------------------------------------------------*/
182//-------------------------------------------------
183//  device_reset - device-specific reset
184//-------------------------------------------------
194185
195static DEVICE_RESET( pia6821 )
186void pia6821_device::device_reset()
196187{
197   pia6821_state *p = get_token(device);
198
199188   /*
200189     * set default read values.
201190     *
202191     * ports A,CA1,CA2 default to 1
203192     * ports B,CB1,CB2 are three-state and undefined (set to 0)
204193     */
205   p->in_a = 0xff;
206   p->in_ca1 = TRUE;
207   p->in_ca2 = TRUE;
208   p->out_a = 0;
209   p->out_ca2 = 0;
210   p->port_a_z_mask = 0;
211   p->ddr_a = 0;
212   p->ctl_a = 0;
213   p->irq_a1 = 0;
214   p->irq_a2 = 0;
215   p->irq_a_state = 0;
216   p->in_b = 0;
217   p->in_cb1 = 0;
218   p->in_cb2 = 0;
219   p->out_b = 0;
220   p->out_cb2 = 0;
221   p->last_out_cb2_z = 0;
222   p->ddr_b = 0;
223   p->ctl_b = 0;
224   p->irq_b1 = 0;
225   p->irq_b2 = 0;
226   p->irq_b_state = 0;
227   p->in_a_pushed = 0;
228   p->out_a_needs_pulled = 0;
229   p->in_ca1_pushed = 0;
230   p->in_ca2_pushed = 0;
231   p->out_ca2_needs_pulled = 0;
232   p->in_b_pushed = 0;
233   p->out_b_needs_pulled = 0;
234   p->in_cb1_pushed = 0;
235   p->in_cb2_pushed = 0;
236   p->out_cb2_needs_pulled = 0;
237   p->logged_port_a_not_connected = 0;
238   p->logged_port_b_not_connected = 0;
239   p->logged_ca1_not_connected = 0;
240   p->logged_ca2_not_connected = 0;
241   p->logged_cb1_not_connected = 0;
242   p->logged_cb2_not_connected = 0;
194   m_in_a = 0xff;
195   m_in_ca1 = TRUE;
196   m_in_ca2 = TRUE;
197   m_out_a = 0;
198   m_out_ca2 = 0;
199   m_port_a_z_mask = 0;
200   m_ddr_a = 0;
201   m_ctl_a = 0;
202   m_irq_a1 = 0;
203   m_irq_a2 = 0;
204   m_irq_a_state = 0;
205   m_in_b = 0;
206   m_in_cb1 = 0;
207   m_in_cb2 = 0;
208   m_out_b = 0;
209   m_out_cb2 = 0;
210   m_last_out_cb2_z = 0;
211   m_ddr_b = 0;
212   m_ctl_b = 0;
213   m_irq_b1 = 0;
214   m_irq_b2 = 0;
215   m_irq_b_state = 0;
216   m_in_a_pushed = 0;
217   m_out_a_needs_pulled = 0;
218   m_in_ca1_pushed = 0;
219   m_in_ca2_pushed = 0;
220   m_out_ca2_needs_pulled = 0;
221   m_in_b_pushed = 0;
222   m_out_b_needs_pulled = 0;
223   m_in_cb1_pushed = 0;
224   m_in_cb2_pushed = 0;
225   m_out_cb2_needs_pulled = 0;
226   m_logged_port_a_not_connected = 0;
227   m_logged_port_b_not_connected = 0;
228   m_logged_ca1_not_connected = 0;
229   m_logged_ca2_not_connected = 0;
230   m_logged_cb1_not_connected = 0;
231   m_logged_cb2_not_connected = 0;
243232
244233
245234   /* clear the IRQs */
246   devcb_call_write_line(&p->irq_a_func, FALSE);
247   devcb_call_write_line(&p->irq_b_func, FALSE);
235   devcb_call_write_line(&m_irq_a_func, FALSE);
236   devcb_call_write_line(&m_irq_b_func, FALSE);
248237}
249238
250239
r8892r8893
252241    update_interrupts
253242-------------------------------------------------*/
254243
255static void update_interrupts(running_device *device)
244void pia6821_device::update_interrupts()
256245{
257   pia6821_state *p = get_token(device);
258   int new_state;
246    /* start with IRQ A */
247   int new_state = (m_irq_a1 && IRQ1_ENABLED(m_ctl_a)) || (m_irq_a2 && IRQ2_ENABLED(m_ctl_a));
259248
260   /* start with IRQ A */
261   new_state = (p->irq_a1 && IRQ1_ENABLED(p->ctl_a)) || (p->irq_a2 && IRQ2_ENABLED(p->ctl_a));
262
263   if (new_state != p->irq_a_state)
249   if (new_state != m_irq_a_state)
264250   {
265      p->irq_a_state = new_state;
266      devcb_call_write_line(&p->irq_a_func, p->irq_a_state);
251      m_irq_a_state = new_state;
252      devcb_call_write_line(&m_irq_a_func, m_irq_a_state);
267253   }
268254
269255   /* then do IRQ B */
270   new_state = (p->irq_b1 && IRQ1_ENABLED(p->ctl_b)) || (p->irq_b2 && IRQ2_ENABLED(p->ctl_b));
256   new_state = (m_irq_b1 && IRQ1_ENABLED(m_ctl_b)) || (m_irq_b2 && IRQ2_ENABLED(m_ctl_b));
271257
272   if (new_state != p->irq_b_state)
258   if (new_state != m_irq_b_state)
273259   {
274      p->irq_b_state = new_state;
275      devcb_call_write_line(&p->irq_b_func, p->irq_b_state);
260      m_irq_b_state = new_state;
261      devcb_call_write_line(&m_irq_b_func, m_irq_b_state);
276262   }
277263}
278264
r8892r8893
281267    get_in_a_value
282268-------------------------------------------------*/
283269
284static UINT8 get_in_a_value(running_device *device)
270UINT8 pia6821_device::get_in_a_value()
285271{
286   pia6821_state *p = get_token(device);
287272   UINT8 port_a_data = 0;
288273   UINT8 ret;
289274
290275   /* update the input */
291   if (p->in_a_func.read != NULL)
292      port_a_data = devcb_call_read8(&p->in_a_func, 0);
276   if (m_in_a_func.read != NULL)
277    {
278      port_a_data = devcb_call_read8(&m_in_a_func, 0);
279    }
293280   else
294281   {
295      if (p->in_a_pushed)
296         port_a_data = p->in_a;
282      if (m_in_a_pushed)
283        {
284         port_a_data = m_in_a;
285        }
297286      else
298287      {
299288         /* mark all pins disconnected */
300         p->port_a_z_mask = 0xff;
289         m_port_a_z_mask = 0xff;
301290
302         if (!p->logged_port_a_not_connected && (p->ddr_a != 0xff))
291         if (!m_logged_port_a_not_connected && (m_ddr_a != 0xff))
303292         {
304            logerror("PIA #%s: Warning! No port A read handler. Assuming pins 0x%02X not connected\n", device->tag(), p->ddr_a ^ 0xff);
305            p->logged_port_a_not_connected = TRUE;
293            logerror("PIA #%s: Warning! No port A read handler. Assuming pins 0x%02X not connected\n", tag(), m_ddr_a ^ 0xff);
294            m_logged_port_a_not_connected = TRUE;
306295         }
307296      }
308297   }
r8892r8893
310299   /* - connected pins are always read
311300       - disconnected pins read the output buffer in output mode
312301       - disconnected pins are HI in input mode */
313   ret = (~p->port_a_z_mask             & port_a_data) |
314         ( p->port_a_z_mask &  p->ddr_a & p->out_a) |
315         ( p->port_a_z_mask & ~p->ddr_a);
302   ret = (~m_port_a_z_mask             & port_a_data) |
303         ( m_port_a_z_mask &  m_ddr_a & m_out_a) |
304         ( m_port_a_z_mask & ~m_ddr_a);
316305
317306   return ret;
318307}
r8892r8893
322311    get_in_b_value
323312-------------------------------------------------*/
324313
325static UINT8 get_in_b_value(running_device *device)
314UINT8 pia6821_device::get_in_b_value()
326315{
327   pia6821_state *p = get_token(device);
328316   UINT8 ret;
329317
330   if (p->ddr_b == 0xff)
318   if (m_ddr_b == 0xff)
319    {
331320      /* all output, just return buffer */
332      ret = p->out_b;
321      ret = m_out_b;
322    }
333323   else
334324   {
335325      UINT8 port_b_data;
336326
337327      /* update the input */
338      if (p->in_b_func.read != NULL)
339         port_b_data = devcb_call_read8(&p->in_b_func, 0);
328      if (m_in_b_func.read != NULL)
329        {
330         port_b_data = devcb_call_read8(&m_in_b_func, 0);
331        }
340332      else
341333      {
342         if (p->in_b_pushed)
343            port_b_data = p->in_b;
334         if (m_in_b_pushed)
335            {
336            port_b_data = m_in_b;
337            }
344338         else
345339         {
346            if (!p->logged_port_b_not_connected && (p->ddr_b != 0xff))
340            if (!m_logged_port_b_not_connected && (m_ddr_b != 0xff))
347341            {
348               logerror("PIA #%s: Error! No port B read handler. Three-state pins 0x%02X are undefined\n", device->tag(), p->ddr_b ^ 0xff);
349               p->logged_port_b_not_connected = TRUE;
342               logerror("PIA #%s: Error! No port B read handler. Three-state pins 0x%02X are undefined\n", tag(), m_ddr_b ^ 0xff);
343               m_logged_port_b_not_connected = TRUE;
350344            }
351345
352346            /* undefined -- need to return something */
r8892r8893
355349      }
356350
357351      /* the DDR determines if the pin or the output buffer is read */
358      ret = (p->out_b & p->ddr_b) | (port_b_data & ~p->ddr_b);
352      ret = (m_out_b & m_ddr_b) | (port_b_data & ~m_ddr_b);
359353   }
360354
361355   return ret;
r8892r8893
366360    get_out_a_value
367361-------------------------------------------------*/
368362
369static UINT8 get_out_a_value(running_device *device)
363UINT8 pia6821_device::get_out_a_value()
370364{
371   pia6821_state *p = get_token(device);
372365   UINT8 ret;
373366
374   if (p->ddr_a == 0xff)
367   if (m_ddr_a == 0xff)
368    {
375369      /* all output */
376      ret = p->out_a;
370      ret = m_out_a;
371    }
377372   else
373    {
378374      /* input pins don't change */
379      ret = (p->out_a & p->ddr_a) | (get_in_a_value(device) & ~p->ddr_a);
375      ret = (m_out_a & m_ddr_a) | (get_in_a_value() & ~m_ddr_a);
376    }
380377
381378   return ret;
382379}
r8892r8893
386383    get_out_b_value
387384-------------------------------------------------*/
388385
389static UINT8 get_out_b_value(running_device *device)
386UINT8 pia6821_device::get_out_b_value()
390387{
391   pia6821_state *p = get_token(device);
392
393388   /* input pins are high-impedance - we just send them as zeros for backwards compatibility */
394   return p->out_b & p->ddr_b;
389   return m_out_b & m_ddr_b;
395390}
396391
397392
r8892r8893
399394    set_out_ca2
400395-------------------------------------------------*/
401396
402static void set_out_ca2(running_device *device, int data)
397void pia6821_device::set_out_ca2(int data)
403398{
404   pia6821_state *p = get_token(device);
405
406   if (data != p->out_ca2)
399   if (data != m_out_ca2)
407400   {
408      p->out_ca2 = data;
401      m_out_ca2 = data;
409402
410403      /* send to output function */
411      if (p->out_ca2_func.write)
412         devcb_call_write_line(&p->out_ca2_func, p->out_ca2);
404      if (m_out_ca2_func.write)
405        {
406         devcb_call_write_line(&m_out_ca2_func, m_out_ca2);
407        }
413408      else
414409      {
415         if (p->out_ca2_needs_pulled)
416            logerror("PIA #%s: Warning! No port CA2 write handler. Previous value has been lost!\n", device->tag());
410         if (m_out_ca2_needs_pulled)
411            {
412            logerror("PIA #%s: Warning! No port CA2 write handler. Previous value has been lost!\n", tag());
413            }
417414
418         p->out_ca2_needs_pulled = TRUE;
415         m_out_ca2_needs_pulled = TRUE;
419416      }
420417   }
421418}
r8892r8893
425422    set_out_cb2
426423-------------------------------------------------*/
427424
428static void set_out_cb2(running_device *device, int data)
425void pia6821_device::set_out_cb2(int data)
429426{
430   pia6821_state *p = get_token(device);
427   int z = pia6821_get_output_cb2_z(this);
431428
432   int z = pia6821_get_output_cb2_z(device);
433
434   if ((data != p->out_cb2) || (z != p->last_out_cb2_z))
429   if ((data != m_out_cb2) || (z != m_last_out_cb2_z))
435430   {
436      p->out_cb2 = data;
437      p->last_out_cb2_z = z;
431      m_out_cb2 = data;
432      m_last_out_cb2_z = z;
438433
439434      /* send to output function */
440      if (p->out_cb2_func.write)
441         devcb_call_write_line(&p->out_cb2_func, p->out_cb2);
435      if (m_out_cb2_func.write)
436        {
437         devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
438        }
442439      else
443440      {
444         if (p->out_cb2_needs_pulled)
445            logerror("PIA #%s: Warning! No port CB2 write handler. Previous value has been lost!\n", device->tag());
441         if (m_out_cb2_needs_pulled)
442            {
443            logerror("PIA #%s: Warning! No port CB2 write handler. Previous value has been lost!\n", tag());
444            }
446445
447         p->out_cb2_needs_pulled = TRUE;
446         m_out_cb2_needs_pulled = TRUE;
448447      }
449448   }
450449}
r8892r8893
454453    port_a_r
455454-------------------------------------------------*/
456455
457static UINT8 port_a_r(running_device *device)
456UINT8 pia6821_device::port_a_r()
458457{
459   pia6821_state *p = get_token(device);
458   UINT8 ret = get_in_a_value();
460459
461   UINT8 ret = get_in_a_value(device);
462
463460   /* IRQ flags implicitly cleared by a read */
464   p->irq_a1 = FALSE;
465   p->irq_a2 = FALSE;
466   update_interrupts(device);
461   m_irq_a1 = FALSE;
462   m_irq_a2 = FALSE;
463   update_interrupts();
467464
468465   /* CA2 is configured as output and in read strobe mode */
469   if (C2_OUTPUT(p->ctl_a) && C2_STROBE_MODE(p->ctl_a))
466   if(C2_OUTPUT(m_ctl_a) && C2_STROBE_MODE(m_ctl_a))
470467   {
471468      /* this will cause a transition low */
472      set_out_ca2(device, FALSE);
469      set_out_ca2(FALSE);
473470
474471      /* if the CA2 strobe is cleared by the E, reset it right away */
475      if (STROBE_E_RESET(p->ctl_a))
476         set_out_ca2(device, TRUE);
472      if(STROBE_E_RESET(m_ctl_a))
473        {
474         set_out_ca2(TRUE);
475        }
477476   }
478477
479   LOG(("PIA #%s: port A read = %02X\n", device->tag(), ret));
478   LOG(("PIA #%s: port A read = %02X\n", tag(), ret));
480479
481480   return ret;
482481}
r8892r8893
486485    ddr_a_r
487486-------------------------------------------------*/
488487
489static UINT8 ddr_a_r(running_device *device)
488UINT8 pia6821_device::ddr_a_r()
490489{
491   pia6821_state *p = get_token(device);
490   UINT8 ret = m_ddr_a;
492491
493   UINT8 ret = p->ddr_a;
492   LOG(("PIA #%s: DDR A read = %02X\n", tag(), ret));
494493
495   LOG(("PIA #%s: DDR A read = %02X\n", device->tag(), ret));
496
497494   return ret;
498495}
499496
r8892r8893
502499    port_b_r
503500-------------------------------------------------*/
504501
505static UINT8 port_b_r(running_device *device)
502UINT8 pia6821_device::port_b_r()
506503{
507   pia6821_state *p = get_token(device);
504   UINT8 ret = get_in_b_value();
508505
509   UINT8 ret = get_in_b_value(device);
510
511506   /* This read will implicitly clear the IRQ B1 flag.  If CB2 is in write-strobe
512507       mode with CB1 restore, and a CB1 active transition set the flag,
513508       clearing it will cause CB2 to go high again.  Note that this is different
514509       from what happens with port A. */
515   if (p->irq_b1 && C2_STROBE_MODE(p->ctl_b) && STROBE_C1_RESET(p->ctl_b))
516      set_out_cb2(device, TRUE);
510   if(m_irq_b1 && C2_STROBE_MODE(m_ctl_b) && STROBE_C1_RESET(m_ctl_b))
511    {
512      set_out_cb2(TRUE);
513    }
517514
518515   /* IRQ flags implicitly cleared by a read */
519   p->irq_b1 = FALSE;
520   p->irq_b2 = FALSE;
521   update_interrupts(device);
516   m_irq_b1 = FALSE;
517   m_irq_b2 = FALSE;
518   update_interrupts();
522519
523   LOG(("PIA #%s: port B read = %02X\n", device->tag(), ret));
520   LOG(("PIA #%s: port B read = %02X\n", tag(), ret));
524521
525522   return ret;
526523}
r8892r8893
530527    ddr_b_r
531528-------------------------------------------------*/
532529
533static UINT8 ddr_b_r(running_device *device)
530UINT8 pia6821_device::ddr_b_r()
534531{
535   pia6821_state *p = get_token(device);
532   UINT8 ret = m_ddr_b;
536533
537   UINT8 ret = p->ddr_b;
534   LOG(("PIA #%s: DDR B read = %02X\n", tag(), ret));
538535
539   LOG(("PIA #%s: DDR B read = %02X\n", device->tag(), ret));
540
541536   return ret;
542537}
543538
r8892r8893
546541    control_a_r
547542-------------------------------------------------*/
548543
549static UINT8 control_a_r(running_device *device)
544UINT8 pia6821_device::control_a_r()
550545{
551   pia6821_state *p = get_token(device);
552546   UINT8 ret;
553547
554548   /* update CA1 & CA2 if callback exists, these in turn may update IRQ's */
555   if (p->in_ca1_func.read != NULL)
556      pia6821_ca1_w(device, devcb_call_read_line(&p->in_ca1_func));
557   else if (!p->logged_ca1_not_connected && (!p->in_ca1_pushed))
549   if (m_in_ca1_func.read != NULL)
550    {
551      ca1_w(devcb_call_read_line(&m_in_ca1_func));
552    }
553   else if(!m_logged_ca1_not_connected && (!m_in_ca1_pushed))
558554   {
559      logerror("PIA #%s: Warning! No CA1 read handler. Assuming pin not connected\n", device->tag());
560      p->logged_ca1_not_connected = TRUE;
555      logerror("PIA #%s: Warning! No CA1 read handler. Assuming pin not connected\n", tag());
556      m_logged_ca1_not_connected = TRUE;
561557   }
562558
563   if (p->in_ca2_func.read != NULL)
564      pia6821_ca2_w(device, devcb_call_read_line(&p->in_ca2_func));
565   else if ( !p->logged_ca2_not_connected && C2_INPUT(p->ctl_a) && !p->in_ca2_pushed)
559   if (m_in_ca2_func.read != NULL)
560    {
561      ca2_w(devcb_call_read_line(&m_in_ca2_func));
562    }
563   else if ( !m_logged_ca2_not_connected && C2_INPUT(m_ctl_a) && !m_in_ca2_pushed)
566564   {
567      logerror("PIA #%s: Warning! No CA2 read handler. Assuming pin not connected\n", device->tag());
568      p->logged_ca2_not_connected = TRUE;
565      logerror("PIA #%s: Warning! No CA2 read handler. Assuming pin not connected\n", tag());
566      m_logged_ca2_not_connected = TRUE;
569567   }
570568
571569   /* read control register */
572   ret = p->ctl_a;
570   ret = m_ctl_a;
573571
574572   /* set the IRQ flags if we have pending IRQs */
575   if (p->irq_a1)
573   if(m_irq_a1)
574    {
576575      ret |= PIA_IRQ1;
576    }
577577
578   if (p->irq_a2 && C2_INPUT(p->ctl_a))
578   if(m_irq_a2 && C2_INPUT(m_ctl_a))
579    {
579580      ret |= PIA_IRQ2;
581    }
580582
581   LOG(("PIA #%s: control A read = %02X\n", device->tag(), ret));
583   LOG(("PIA #%s: control A read = %02X\n", tag(), ret));
582584
583585   return ret;
584586}
r8892r8893
588590    control_b_r
589591-------------------------------------------------*/
590592
591static UINT8 control_b_r(running_device *device)
593UINT8 pia6821_device::control_b_r()
592594{
593   pia6821_state *p = get_token(device);
594595   UINT8 ret;
595596
596597   /* update CB1 & CB2 if callback exists, these in turn may update IRQ's */
597   if (p->in_cb1_func.read != NULL)
598      pia6821_cb1_w(device, devcb_call_read_line(&p->in_cb1_func));
599   else if (!p->logged_cb1_not_connected && !p->in_cb1_pushed)
598   if(m_in_cb1_func.read != NULL)
599    {
600      cb1_w(devcb_call_read_line(&m_in_cb1_func));
601    }
602   else if(!m_logged_cb1_not_connected && !m_in_cb1_pushed)
600603   {
601      logerror("PIA #%s: Error! no CB1 read handler. Three-state pin is undefined\n", device->tag());
602      p->logged_cb1_not_connected = TRUE;
604      logerror("PIA #%s: Error! no CB1 read handler. Three-state pin is undefined\n", tag());
605      m_logged_cb1_not_connected = TRUE;
603606   }
604607
605   if (p->in_cb2_func.read != NULL)
606      pia6821_cb2_w(device, devcb_call_read_line(&p->in_cb2_func));
607   else if (!p->logged_cb2_not_connected && C2_INPUT(p->ctl_b) && !p->in_cb2_pushed)
608   if(m_in_cb2_func.read != NULL)
609    {
610      cb2_w(devcb_call_read_line(&m_in_cb2_func));
611    }
612   else if(!m_logged_cb2_not_connected && C2_INPUT(m_ctl_b) && !m_in_cb2_pushed)
608613   {
609      logerror("PIA #%s: Error! No CB2 read handler. Three-state pin is undefined\n", device->tag());
610      p->logged_cb2_not_connected = TRUE;
614      logerror("PIA #%s: Error! No CB2 read handler. Three-state pin is undefined\n", tag());
615      m_logged_cb2_not_connected = TRUE;
611616   }
612617
613618   /* read control register */
614   ret = p->ctl_b;
619   ret = m_ctl_b;
615620
616621   /* set the IRQ flags if we have pending IRQs */
617   if (p->irq_b1)
622   if(m_irq_b1)
623    {
618624      ret |= PIA_IRQ1;
625    }
619626
620   if (p->irq_b2 && C2_INPUT(p->ctl_b))
627   if(m_irq_b2 && C2_INPUT(m_ctl_b))
628    {
621629      ret |= PIA_IRQ2;
630    }
622631
623   LOG(("PIA #%s: control B read = %02X\n", device->tag(), ret));
632   LOG(("PIA #%s: control B read = %02X\n", tag(), ret));
624633
625634   return ret;
626635}
r8892r8893
632641
633642READ8_DEVICE_HANDLER( pia6821_r )
634643{
635   pia6821_state *p = get_token(device);
644    return downcast<pia6821_device *>(device)->reg_r(offset);
645}
646
647UINT8 pia6821_device::reg_r(UINT8 offset)
648{
636649   UINT8 ret;
637650
638651   switch (offset & 0x03)
639652   {
640653      default: /* impossible */
641654      case 0x00:
642         if (OUTPUT_SELECTED(p->ctl_a))
643            ret = port_a_r(device);
655         if (OUTPUT_SELECTED(m_ctl_a))
656            {
657            ret = port_a_r();
658            }
644659         else
645            ret = ddr_a_r(device);
660            {
661            ret = ddr_a_r();
662            }
646663         break;
647664
648665      case 0x01:
649         ret = control_a_r(device);
666         ret = control_a_r();
650667         break;
651668
652669      case 0x02:
653         if (OUTPUT_SELECTED(p->ctl_b))
654            ret = port_b_r(device);
670         if (OUTPUT_SELECTED(m_ctl_b))
671            {
672            ret = port_b_r();
673            }
655674         else
656            ret = ddr_b_r(device);
675            {
676            ret = ddr_b_r();
677            }
657678         break;
658679
659680      case 0x03:
660         ret = control_b_r(device);
681         ret = control_b_r();
661682         break;
662683   }
663684
r8892r8893
671692
672693READ8_DEVICE_HANDLER( pia6821_alt_r )
673694{
674   return pia6821_r(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01));
695    return downcast<pia6821_device *>(device)->reg_r(((offset << 1) & 0x02) | ((offset >> 1) & 0x01));
675696}
676697
677698
699
678700/*-------------------------------------------------
679701    pia6821_get_port_b_z_mask
680702-------------------------------------------------*/
681703
682704UINT8 pia6821_get_port_b_z_mask(running_device *device)
683705{
684   pia6821_state *p = get_token(device);
685   return ~p->ddr_b;
706    return downcast<pia6821_device *>(device)->get_port_b_z_mask();
686707}
687708
709UINT8 pia6821_device::get_port_b_z_mask()
710{
711   return ~m_ddr_b;
712}
688713
714
689715/*-------------------------------------------------
690716    send_to_out_a_func
691717-------------------------------------------------*/
692718
693static void send_to_out_a_func(running_device *device, const char* message)
719void pia6821_device::send_to_out_a_func(const char* message)
694720{
695   pia6821_state *p = get_token(device);
696
697721   /* input pins are pulled high */
698   UINT8 data = get_out_a_value(device);
722   UINT8 data = get_out_a_value();
699723
700   LOG(("PIA #%s: %s = %02X\n", device->tag(), message, data));
724   LOG(("PIA #%s: %s = %02X\n", tag(), message, data));
701725
702   if (p->out_a_func.write != NULL)
703      devcb_call_write8(&p->out_a_func, 0, data);
726   if(m_out_a_func.write != NULL)
727    {
728      devcb_call_write8(&m_out_a_func, 0, data);
729    }
704730   else
705731   {
706      if (p->out_a_needs_pulled)
707         logerror("PIA #%s: Warning! No port A write handler. Previous value has been lost!\n", device->tag());
732      if(m_out_a_needs_pulled)
733        {
734         logerror("PIA #%s: Warning! No port A write handler. Previous value has been lost!\n", tag());
735        }
708736
709      p->out_a_needs_pulled = TRUE;
737      m_out_a_needs_pulled = TRUE;
710738   }
711739}
712740
r8892r8893
715743    send_to_out_b_func
716744-------------------------------------------------*/
717745
718static void send_to_out_b_func(running_device *device, const char* message)
746void pia6821_device::send_to_out_b_func(const char* message)
719747{
720   pia6821_state *p = get_token(device);
721
722748   /* input pins are high-impedance - we just send them as zeros for backwards compatibility */
723   UINT8 data = get_out_b_value(device);
749   UINT8 data = get_out_b_value();
724750
725   LOG(("PIA #%s: %s = %02X\n", device->tag(), message, data));
751   LOG(("PIA #%s: %s = %02X\n", tag(), message, data));
726752
727   if (p->out_b_func.write != NULL)
728      devcb_call_write8(&p->out_b_func, 0, data);
753   if(m_out_b_func.write != NULL)
754    {
755      devcb_call_write8(&m_out_b_func, 0, data);
756    }
729757   else
730758   {
731      if (p->out_b_needs_pulled)
732         logerror("PIA #%s: Warning! No port B write handler. Previous value has been lost!\n", device->tag());
759      if(m_out_b_needs_pulled)
760        {
761         logerror("PIA #%s: Warning! No port B write handler. Previous value has been lost!\n", tag());
762        }
733763
734      p->out_b_needs_pulled = TRUE;
764      m_out_b_needs_pulled = TRUE;
735765   }
736766}
737767
r8892r8893
740770    port_a_w
741771-------------------------------------------------*/
742772
743static void port_a_w(running_device *device, UINT8 data)
773void pia6821_device::port_a_w(UINT8 data)
744774{
745   pia6821_state *p = get_token(device);
746
747775   /* buffer the output value */
748   p->out_a = data;
776   m_out_a = data;
749777
750   send_to_out_a_func(device, "port A write");
778   send_to_out_a_func("port A write");
751779}
752780
753781
r8892r8893
755783    ddr_a_w
756784-------------------------------------------------*/
757785
758static void ddr_a_w(running_device *device, UINT8 data)
786void pia6821_device::ddr_a_w(UINT8 data)
759787{
760   pia6821_state *p = get_token(device);
761
762   if (data == 0x00)
763      LOG(("PIA #%s: DDR A write = %02X (input mode)\n", device->tag(), data));
764   else if (data == 0xff)
765      LOG(("PIA #%s: DDR A write = %02X (output mode)\n", device->tag(), data));
788   if(data == 0x00)
789    {
790      LOG(("PIA #%s: DDR A write = %02X (input mode)\n", tag(), data));
791    }
792   else if(data == 0xff)
793    {
794      LOG(("PIA #%s: DDR A write = %02X (output mode)\n", tag(), data));
795    }
766796   else
767      LOG(("PIA #%s: DDR A write = %02X (mixed mode)\n", device->tag(), data));
797    {
798      LOG(("PIA #%s: DDR A write = %02X (mixed mode)\n", tag(), data));
799    }
768800
769   if (p->ddr_a != data)
801   if(m_ddr_a != data)
770802   {
771803      /* DDR changed, call the callback again */
772      p->ddr_a = data;
773      p->logged_port_a_not_connected = FALSE;
774      send_to_out_a_func(device, "port A write due to DDR change");
804      m_ddr_a = data;
805      m_logged_port_a_not_connected = FALSE;
806      send_to_out_a_func("port A write due to DDR change");
775807   }
776808}
777809
r8892r8893
780812    port_b_w
781813-------------------------------------------------*/
782814
783static void port_b_w(running_device *device, UINT8 data)
815void pia6821_device::port_b_w(UINT8 data)
784816{
785   pia6821_state *p = get_token(device);
786
787817   /* buffer the output value */
788   p->out_b = data;
818   m_out_b = data;
789819
790   send_to_out_b_func(device, "port B write");
820   send_to_out_b_func("port B write");
791821
792822   /* CB2 in write strobe mode */
793   if (C2_STROBE_MODE(p->ctl_b))
823   if(C2_STROBE_MODE(m_ctl_b))
794824   {
795825      /* this will cause a transition low */
796      set_out_cb2(device, FALSE);
826      set_out_cb2(FALSE);
797827
798828      /* if the CB2 strobe is cleared by the E, reset it right away */
799      if (STROBE_E_RESET(p->ctl_b))
800         set_out_cb2(device, TRUE);
829      if(STROBE_E_RESET(m_ctl_b))
830        {
831         set_out_cb2(TRUE);
832        }
801833   }
802834}
803835
r8892r8893
806838    ddr_b_w
807839-------------------------------------------------*/
808840
809static void ddr_b_w(running_device *device, UINT8 data)
841void pia6821_device::ddr_b_w(UINT8 data)
810842{
811   pia6821_state *p = get_token(device);
812
813843   if (data == 0x00)
814      LOG(("PIA #%s: DDR B write = %02X (input mode)\n", device->tag(), data));
844    {
845      LOG(("PIA #%s: DDR B write = %02X (input mode)\n", tag(), data));
846    }
815847   else if (data == 0xff)
816      LOG(("PIA #%s: DDR B write = %02X (output mode)\n", device->tag(), data));
848    {
849      LOG(("PIA #%s: DDR B write = %02X (output mode)\n", tag(), data));
850    }
817851   else
818      LOG(("PIA #%s: DDR B write = %02X (mixed mode)\n", device->tag(), data));
852    {
853      LOG(("PIA #%s: DDR B write = %02X (mixed mode)\n", tag(), data));
854    }
819855
820   if (p->ddr_b != data)
856   if(m_ddr_b != data)
821857   {
822858      /* DDR changed, call the callback again */
823      p->ddr_b = data;
824      p->logged_port_b_not_connected = FALSE;
825      send_to_out_b_func(device, "port B write due to DDR change");
859      m_ddr_b = data;
860      m_logged_port_b_not_connected = FALSE;
861      send_to_out_b_func("port B write due to DDR change");
826862   }
827863}
828864
r8892r8893
831867    control_a_w
832868-------------------------------------------------*/
833869
834static void control_a_w(running_device *device, UINT8 data)
870void pia6821_device::control_a_w(UINT8 data)
835871{
836   pia6821_state *p = get_token(device);
837
838872   /* bit 7 and 6 are read only */
839873   data &= 0x3f;
840874
841   LOG(("PIA #%s: control A write = %02X\n", device->tag(), data));
875   LOG(("PIA #%s: control A write = %02X\n", tag(), data));
842876
843877   /* update the control register */
844   p->ctl_a = data;
878   m_ctl_a = data;
845879
846880   /* CA2 is configured as output */
847   if (C2_OUTPUT(p->ctl_a))
881   if(C2_OUTPUT(m_ctl_a))
848882   {
849883      int temp;
850884
851      if (C2_SET_MODE(p->ctl_a))
885      if(C2_SET_MODE(m_ctl_a))
886        {
852887         /* set/reset mode - bit value determines the new output */
853         temp = C2_SET(p->ctl_a);
888         temp = C2_SET(m_ctl_a);
889        }
854890      else
891        {
855892         /* strobe mode - output is always high unless strobed */
856893         temp = TRUE;
894        }
857895
858      set_out_ca2(device, temp);
896      set_out_ca2(temp);
859897   }
860898
861899   /* update externals */
862   update_interrupts(device);
900   update_interrupts();
863901}
864902
865903
r8892r8893
867905    control_b_w
868906-------------------------------------------------*/
869907
870static void control_b_w(running_device *device, UINT8 data)
908void pia6821_device::control_b_w(UINT8 data)
871909{
872   pia6821_state *p = get_token(device);
873910   int temp;
874911
875912   /* bit 7 and 6 are read only */
876913   data &= 0x3f;
877914
878   LOG(("PIA #%s: control B write = %02X\n", device->tag(), data));
915   LOG(("PIA #%s: control B write = %02X\n", tag(), data));
879916
880917   /* update the control register */
881   p->ctl_b = data;
918   m_ctl_b = data;
882919
883   if (C2_SET_MODE(p->ctl_b))
920   if (C2_SET_MODE(m_ctl_b))
921    {
884922      /* set/reset mode - bit value determines the new output */
885      temp = C2_SET(p->ctl_b);
923      temp = C2_SET(m_ctl_b);
924    }
886925   else
926    {
887927      /* strobe mode - output is always high unless strobed */
888928      temp = TRUE;
929    }
889930
890   set_out_cb2(device, temp);
931   set_out_cb2(temp);
891932
892933   /* update externals */
893   update_interrupts(device);
934   update_interrupts();
894935}
895936
896937
r8892r8893
900941
901942WRITE8_DEVICE_HANDLER( pia6821_w )
902943{
903   pia6821_state *p = get_token(device);
944    downcast<pia6821_device *>(device)->reg_w(offset, data);
945}
904946
947void pia6821_device::reg_w(UINT8 offset, UINT8 data)
948{
905949   switch (offset & 0x03)
906950   {
907951      default: /* impossible */
908952      case 0x00:
909         if (OUTPUT_SELECTED(p->ctl_a))
910            port_a_w(device, data);
953         if (OUTPUT_SELECTED(m_ctl_a))
954            {
955            port_a_w(data);
956            }
911957         else
912            ddr_a_w(device, data);
958            {
959            ddr_a_w(data);
960            }
913961         break;
914962
915963      case 0x01:
916         control_a_w(device, data);
964         control_a_w( data);
917965         break;
918966
919967      case 0x02:
920         if (OUTPUT_SELECTED(p->ctl_b))
921            port_b_w(device, data);
968         if(OUTPUT_SELECTED(m_ctl_b))
969            {
970            port_b_w(data);
971            }
922972         else
923            ddr_b_w(device, data);
973            {
974            ddr_b_w(data);
975            }
924976         break;
925977
926978      case 0x03:
927         control_b_w(device, data);
979         control_b_w(data);
928980         break;
929981   }
930982}
r8892r8893
936988
937989WRITE8_DEVICE_HANDLER( pia6821_alt_w )
938990{
939   pia6821_w(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01), data);
991    downcast<pia6821_device *>(device)->reg_w(((offset << 1) & 0x02) | ((offset >> 1) & 0x01), data);
940992}
941993
942994
r8892r8893
946998
947999void pia6821_set_port_a_z_mask(running_device *device, UINT8 data)
9481000{
949   pia6821_state *p = get_token(device);
1001    downcast<pia6821_device *>(device)->set_port_a_z_mask(data);
1002}
9501003
951   p->port_a_z_mask = data;
1004void pia6821_device::set_port_a_z_mask(UINT8 data)
1005{
1006   m_port_a_z_mask = data;
9521007}
9531008
9541009
r8892r8893
9581013
9591014READ8_DEVICE_HANDLER( pia6821_porta_r )
9601015{
961   pia6821_state *p = get_token(device);
1016    return downcast<pia6821_device *>(device)->porta_r();
1017}
9621018
963   return p->in_a;
1019UINT8 pia6821_device::porta_r()
1020{
1021   return m_in_a;
9641022}
9651023
9661024
r8892r8893
9701028
9711029void pia6821_set_input_a(running_device *device, UINT8 data, UINT8 z_mask)
9721030{
973   pia6821_state *p = get_token(device);
1031    downcast<pia6821_device *>(device)->set_input_a(data, z_mask);
1032}
9741033
975   assert_always(p->in_a_func.read == NULL, "pia6821_porta_w() called when in_a_func implemented");
1034void pia6821_device::set_input_a(UINT8 data, UINT8 z_mask)
1035{
1036   assert_always(m_in_a_func.read == NULL, "pia6821_porta_w() called when in_a_func implemented");
9761037
977   LOG(("PIA #%s: set input port A = %02X\n", device->tag(), data));
1038   LOG(("PIA #%s: set input port A = %02X\n", tag(), data));
9781039
979   p->in_a = data;
980   p->port_a_z_mask = z_mask;
981   p->in_a_pushed = TRUE;
1040   m_in_a = data;
1041   m_port_a_z_mask = z_mask;
1042   m_in_a_pushed = TRUE;
9821043}
9831044
9841045
r8892r8893
9881049
9891050WRITE8_DEVICE_HANDLER( pia6821_porta_w )
9901051{
991   pia6821_set_input_a(device, data, 0);
1052    downcast<pia6821_device *>(device)->porta_w(data);
9921053}
9931054
1055void pia6821_device::porta_w(UINT8 data)
1056{
1057   pia6821_set_input_a(this, data, 0);
1058}
9941059
1060
9951061/*-------------------------------------------------
9961062    pia6821_get_output_a
9971063-------------------------------------------------*/
9981064
9991065UINT8 pia6821_get_output_a(running_device *device)
10001066{
1001   pia6821_state *p = get_token(device);
1067    return downcast<pia6821_device *>(device)->get_output_a();
1068}
10021069
1003   p->out_a_needs_pulled = FALSE;
1070UINT8 pia6821_device::get_output_a()
1071{
1072   m_out_a_needs_pulled = FALSE;
10041073
1005   return get_out_a_value(device);
1074   return get_out_a_value();
10061075}
10071076
10081077
r8892r8893
10121081
10131082READ_LINE_DEVICE_HANDLER( pia6821_ca1_r )
10141083{
1015   pia6821_state *p = get_token(device);
1084    return downcast<pia6821_device *>(device)->ca1_r();
1085}
10161086
1017   return p->in_ca1;
1087UINT8 pia6821_device::ca1_r()
1088{
1089   return m_in_ca1;
10181090}
10191091
10201092
r8892r8893
10241096
10251097WRITE_LINE_DEVICE_HANDLER( pia6821_ca1_w )
10261098{
1027   pia6821_state *p = get_token(device);
1099    downcast<pia6821_device *>(device)->ca1_w(state);
1100}
10281101
1029   LOG(("PIA #%s: set input CA1 = %d\n", device->tag(), state));
1102void pia6821_device::ca1_w(UINT8 state)
1103{
1104   LOG(("PIA #%s: set input CA1 = %d\n", tag(), state));
10301105
10311106   /* the new state has caused a transition */
1032   if ((p->in_ca1 != state) &&
1033      ((state && C1_LOW_TO_HIGH(p->ctl_a)) || (!state && C1_HIGH_TO_LOW(p->ctl_a))))
1107   if((m_in_ca1 != state) && ((state && C1_LOW_TO_HIGH(m_ctl_a)) || (!state && C1_HIGH_TO_LOW(m_ctl_a))))
10341108   {
1035      LOG(("PIA #%s: CA1 triggering\n", device->tag()));
1109      LOG(("PIA #%s: CA1 triggering\n", tag()));
10361110
10371111      /* mark the IRQ */
1038      p->irq_a1 = TRUE;
1112      m_irq_a1 = TRUE;
10391113
10401114      /* update externals */
1041      update_interrupts(device);
1115      update_interrupts();
10421116
10431117      /* CA2 is configured as output and in read strobe mode and cleared by a CA1 transition */
1044      if (C2_OUTPUT(p->ctl_a) && C2_STROBE_MODE(p->ctl_a) && STROBE_C1_RESET(p->ctl_a))
1045         set_out_ca2(device, TRUE);
1118      if(C2_OUTPUT(m_ctl_a) && C2_STROBE_MODE(m_ctl_a) && STROBE_C1_RESET(m_ctl_a))
1119        {
1120         set_out_ca2(TRUE);
1121        }
10461122   }
10471123
10481124   /* set the new value for CA1 */
1049   p->in_ca1 = state;
1050   p->in_ca1_pushed = TRUE;
1125   m_in_ca1 = state;
1126   m_in_ca1_pushed = TRUE;
10511127}
10521128
10531129
r8892r8893
10571133
10581134READ_LINE_DEVICE_HANDLER( pia6821_ca2_r )
10591135{
1060   pia6821_state *p = get_token(device);
1136    return downcast<pia6821_device *>(device)->ca2_r();
1137}
10611138
1062   return p->in_ca2;
1139UINT8 pia6821_device::ca2_r()
1140{
1141   return m_in_ca2;
10631142}
10641143
10651144
r8892r8893
10691148
10701149WRITE_LINE_DEVICE_HANDLER( pia6821_ca2_w )
10711150{
1072   pia6821_state *p = get_token(device);
1151    downcast<pia6821_device *>(device)->ca2_w(state);
1152}
10731153
1074   LOG(("PIA #%s: set input CA2 = %d\n", device->tag(), state));
1154void pia6821_device::ca2_w(UINT8 state)
1155{
1156   LOG(("PIA #%s: set input CA2 = %d\n", tag(), state));
10751157
10761158   /* if input mode and the new state has caused a transition */
1077   if (C2_INPUT(p->ctl_a) &&
1078      (p->in_ca2 != state) &&
1079      ((state && C2_LOW_TO_HIGH(p->ctl_a)) || (!state && C2_HIGH_TO_LOW(p->ctl_a))))
1159   if(C2_INPUT(m_ctl_a) && (m_in_ca2 != state) && ((state && C2_LOW_TO_HIGH(m_ctl_a)) || (!state && C2_HIGH_TO_LOW(m_ctl_a))))
10801160   {
1081      LOG(("PIA #%s: CA2 triggering\n", device->tag()));
1161      LOG(("PIA #%s: CA2 triggering\n", tag()));
10821162
10831163      /* mark the IRQ */
1084      p->irq_a2 = TRUE;
1164      m_irq_a2 = TRUE;
10851165
10861166      /* update externals */
1087      update_interrupts(device);
1167      update_interrupts();
10881168   }
10891169
10901170   /* set the new value for CA2 */
1091   p->in_ca2 = state;
1092   p->in_ca2_pushed = TRUE;
1171   m_in_ca2 = state;
1172   m_in_ca2_pushed = TRUE;
10931173}
10941174
10951175
r8892r8893
10991179
11001180int pia6821_get_output_ca2(running_device *device)
11011181{
1102   pia6821_state *p = get_token(device);
1182    return downcast<pia6821_device *>(device)->get_output_ca2();
1183}
11031184
1104   p->out_ca2_needs_pulled = FALSE;
1185int pia6821_device::get_output_ca2()
1186{
1187   m_out_ca2_needs_pulled = FALSE;
11051188
1106   return p->out_ca2;
1189   return m_out_ca2;
11071190}
11081191
11091192
r8892r8893
11151198
11161199int pia6821_get_output_ca2_z(running_device *device)
11171200{
1118   pia6821_state *p = get_token(device);
1201    return downcast<pia6821_device *>(device)->get_output_ca2_z();
1202}
11191203
1120   p->out_ca2_needs_pulled = FALSE;
1204int pia6821_device::get_output_ca2_z()
1205{
1206   m_out_ca2_needs_pulled = FALSE;
11211207
11221208   // If it's an output, output the bit, if it's an input, it's
11231209   // pulled up
1124   return p->out_ca2 |
1125          C2_INPUT(p->ctl_a);
1210   return m_out_ca2 | C2_INPUT(m_ctl_a);
11261211}
11271212
11281213
r8892r8893
11321217
11331218READ8_DEVICE_HANDLER( pia6821_portb_r )
11341219{
1135   pia6821_state *p = get_token(device);
1220    return downcast<pia6821_device *>(device)->portb_r();
1221}
11361222
1137   return p->in_b;
1223UINT8 pia6821_device::portb_r()
1224{
1225   return m_in_b;
11381226}
11391227
11401228
r8892r8893
11441232
11451233WRITE8_DEVICE_HANDLER( pia6821_portb_w )
11461234{
1147   pia6821_state *p = get_token(device);
1235    downcast<pia6821_device *>(device)->portb_w(data);
1236}
11481237
1149   assert_always(p->in_b_func.read == NULL, "pia_set_input_b() called when in_b_func implemented");
1238void pia6821_device::portb_w(UINT8 data)
1239{
1240   assert_always(m_in_b_func.read == NULL, "pia_set_input_b() called when in_b_func implemented");
11501241
1151   LOG(("PIA #%s: set input port B = %02X\n", device->tag(), data));
1242   LOG(("PIA #%s: set input port B = %02X\n", tag(), data));
11521243
1153   p->in_b = data;
1154   p->in_b_pushed = TRUE;
1244   m_in_b = data;
1245   m_in_b_pushed = TRUE;
11551246}
11561247
11571248
r8892r8893
11611252
11621253UINT8 pia6821_get_output_b(running_device *device)
11631254{
1164   pia6821_state *p = get_token(device);
1255    return downcast<pia6821_device *>(device)->get_output_b();
1256}
11651257
1166   p->out_b_needs_pulled = FALSE;
1258UINT8 pia6821_device::get_output_b()
1259{
1260   m_out_b_needs_pulled = FALSE;
11671261
1168   return get_out_b_value(device);
1262   return get_out_b_value();
11691263}
11701264
11711265
r8892r8893
11751269
11761270READ_LINE_DEVICE_HANDLER( pia6821_cb1_r )
11771271{
1178   pia6821_state *p = get_token(device);
1272    return downcast<pia6821_device *>(device)->cb1_r();
1273}
11791274
1180   return p->in_cb1;
1275UINT8 pia6821_device::cb1_r()
1276{
1277   return m_in_cb1;
11811278}
11821279
11831280
r8892r8893
11871284
11881285WRITE_LINE_DEVICE_HANDLER( pia6821_cb1_w )
11891286{
1190   pia6821_state *p = get_token(device);
1287    downcast<pia6821_device *>(device)->cb1_w(state);
1288}
11911289
1192   LOG(("PIA #%s: set input CB1 = %d\n", device->tag(), state));
1290void pia6821_device::cb1_w(UINT8 state)
1291{
1292   LOG(("PIA #%s: set input CB1 = %d\n", tag(), state));
11931293
11941294   /* the new state has caused a transition */
1195   if ((p->in_cb1 != state) &&
1196      ((state && C1_LOW_TO_HIGH(p->ctl_b)) || (!state && C1_HIGH_TO_LOW(p->ctl_b))))
1295   if((m_in_cb1 != state) && ((state && C1_LOW_TO_HIGH(m_ctl_b)) || (!state && C1_HIGH_TO_LOW(m_ctl_b))))
11971296   {
1198      LOG(("PIA #%s: CB1 triggering\n", device->tag()));
1297      LOG(("PIA #%s: CB1 triggering\n", tag()));
11991298
12001299      /* mark the IRQ */
1201      p->irq_b1 = 1;
1300      m_irq_b1 = 1;
12021301
12031302      /* update externals */
1204      update_interrupts(device);
1303      update_interrupts();
12051304
12061305      /* If CB2 is configured as a write-strobe output which is reset by a CB1
12071306           transition, this reset will only happen when a read from port B implicitly
r8892r8893
12101309   }
12111310
12121311   /* set the new value for CB1 */
1213   p->in_cb1 = state;
1214   p->in_cb1_pushed = TRUE;
1312   m_in_cb1 = state;
1313   m_in_cb1_pushed = TRUE;
12151314}
12161315
12171316
r8892r8893
12211320
12221321READ_LINE_DEVICE_HANDLER( pia6821_cb2_r )
12231322{
1224   pia6821_state *p = get_token(device);
1323    return downcast<pia6821_device *>(device)->cb2_r();
1324}
12251325
1226   return p->in_cb2;
1326UINT8 pia6821_device::cb2_r()
1327{
1328   return m_in_cb2;
12271329}
12281330
12291331
r8892r8893
12331335
12341336WRITE_LINE_DEVICE_HANDLER( pia6821_cb2_w )
12351337{
1236   pia6821_state *p = get_token(device);
1338    downcast<pia6821_device *>(device)->cb2_w(state);
1339}
12371340
1238   LOG(("PIA #%s: set input CB2 = %d\n", device->tag(), state));
1341void pia6821_device::cb2_w(UINT8 state)
1342{
1343   LOG(("PIA #%s: set input CB2 = %d\n", tag(), state));
12391344
12401345   /* if input mode and the new state has caused a transition */
1241   if (C2_INPUT(p->ctl_b) &&
1242      (p->in_cb2 != state) &&
1243      ((state && C2_LOW_TO_HIGH(p->ctl_b)) || (!state && C2_HIGH_TO_LOW(p->ctl_b))))
1346   if (C2_INPUT(m_ctl_b) &&
1347      (m_in_cb2 != state) &&
1348      ((state && C2_LOW_TO_HIGH(m_ctl_b)) || (!state && C2_HIGH_TO_LOW(m_ctl_b))))
12441349   {
1245      LOG(("PIA #%s: CB2 triggering\n", device->tag()));
1350      LOG(("PIA #%s: CB2 triggering\n", tag()));
12461351
12471352      /* mark the IRQ */
1248      p->irq_b2 = 1;
1353      m_irq_b2 = 1;
12491354
12501355      /* update externals */
1251      update_interrupts(device);
1356      update_interrupts();
12521357   }
12531358
12541359   /* set the new value for CA2 */
1255   p->in_cb2 = state;
1256   p->in_cb2_pushed = TRUE;
1360   m_in_cb2 = state;
1361   m_in_cb2_pushed = TRUE;
12571362}
12581363
12591364
r8892r8893
12631368
12641369int pia6821_get_output_cb2(running_device *device)
12651370{
1266   pia6821_state *p = get_token(device);
1371    return downcast<pia6821_device *>(device)->get_output_cb2();
1372}
12671373
1268   p->out_cb2_needs_pulled = FALSE;
1374int pia6821_device::get_output_cb2()
1375{
1376   m_out_cb2_needs_pulled = FALSE;
12691377
1270   return p->out_cb2;
1378   return m_out_cb2;
12711379}
12721380
12731381
r8892r8893
12771385
12781386int pia6821_get_output_cb2_z(running_device *device)
12791387{
1280   pia6821_state *p = get_token(device);
1388    return downcast<pia6821_device *>(device)->get_output_cb2_z();
1389}
12811390
1282   return !C2_OUTPUT(p->ctl_b);
1391int pia6821_device::get_output_cb2_z()
1392{
1393   return !C2_OUTPUT(m_ctl_b);
12831394}
12841395
12851396
r8892r8893
12891400
12901401int pia6821_get_irq_a(running_device *device)
12911402{
1292   pia6821_state *p = get_token(device);
1403    return downcast<pia6821_device *>(device)->get_irq_a();
1404}
12931405
1294   return p->irq_a_state;
1406int pia6821_device::get_irq_a()
1407{
1408   return m_irq_a_state;
12951409}
12961410
12971411
r8892r8893
13011415
13021416int pia6821_get_irq_b(running_device *device)
13031417{
1304   pia6821_state *p = get_token(device);
1418    return downcast<pia6821_device *>(device)->get_irq_b();
1419}
13051420
1306   return p->irq_b_state;
1421int pia6821_device::get_irq_b()
1422{
1423   return m_irq_b_state;
13071424}
1308
1309
1310/*-------------------------------------------------
1311    device definition
1312-------------------------------------------------*/
1313
1314static const char DEVTEMPLATE_SOURCE[] = __FILE__;
1315
1316#define DEVTEMPLATE_ID(p,s)            p##pia6821##s
1317#define DEVTEMPLATE_FEATURES         DT_HAS_START | DT_HAS_RESET
1318#define DEVTEMPLATE_NAME            "6821 PIA"
1319#define DEVTEMPLATE_FAMILY            "6821 PIA"
1320#include "devtempl.h"
1321
1322#define DEVTEMPLATE_DERIVED_ID(p,s)      p##pia6822##s
1323#define DEVTEMPLATE_DERIVED_FEATURES   0
1324#define DEVTEMPLATE_DERIVED_NAME      "6822 PIA"
1325#include "devtempl.h"
1326
1327DEFINE_LEGACY_DEVICE(PIA6821, pia6821);
1328DEFINE_LEGACY_DEVICE(PIA6822, pia6822);
src/emu/machine/6532riot.c
r8892r8893
3131
3232
3333
34/***************************************************************************
35    TYPE DEFINITIONS
36***************************************************************************/
34//**************************************************************************
35//  DEVICE CONFIGURATION
36//**************************************************************************
3737
38typedef struct _riot6532_port riot6532_port;
39struct _riot6532_port
38//-------------------------------------------------
39//  riot6532_device_config - constructor
40//-------------------------------------------------
41
42riot6532_device_config::riot6532_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
43    : device_config(mconfig, static_alloc_device_config, "RIOT6532", tag, owner, clock)
4044{
41   UINT8               in;
42   UINT8               out;
43   UINT8               ddr;
44   devcb_resolved_read8   in_func;
45   devcb_resolved_write8   out_func;
46};
45}
4746
4847
49typedef struct _riot6532_state riot6532_state;
50struct _riot6532_state
48//-------------------------------------------------
49//  static_alloc_device_config - allocate a new
50//  configuration object
51//-------------------------------------------------
52
53device_config *riot6532_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
5154{
52   running_device *device;
53   const riot6532_interface *intf;
54   int            index;
55    return global_alloc(riot6532_device_config(mconfig, tag, owner, clock));
56}
5557
56   riot6532_port   port[2];
5758
58   devcb_resolved_write_line   irq_func;
59//-------------------------------------------------
60//  alloc_device - allocate a new device object
61//-------------------------------------------------
5962
60   UINT8         irqstate;
61   UINT8         irqenable;
63device_t *riot6532_device_config::alloc_device(running_machine &machine) const
64{
65    return auto_alloc(&machine, riot6532_device(machine, *this));
66}
6267
63   UINT8         pa7dir;      /* 0x80 = high-to-low, 0x00 = low-to-high */
64   UINT8         pa7prev;
6568
66   UINT8         timershift;
67   UINT8         timerstate;
68   emu_timer *      timer;
69};
69//-------------------------------------------------
70//  device_config_complete - perform any
71//  operations now that the configuration is
72//  complete
73//-------------------------------------------------
7074
75void riot6532_device_config::device_config_complete()
76{
77    // inherit a copy of the static data
78    const riot6532_interface *intf = reinterpret_cast<const riot6532_interface *>(static_config());
79    if (intf != NULL)
80    {
81        *static_cast<riot6532_interface *>(this) = *intf;
82    }
7183
84    // or initialize to defaults if none provided
85    else
86    {
87        memset(&m_in_a_func, 0, sizeof(m_in_a_func));
88        memset(&m_in_b_func, 0, sizeof(m_in_b_func));
89        memset(&m_out_a_func, 0, sizeof(m_out_a_func));
90        memset(&m_out_b_func, 0, sizeof(m_out_b_func));
91        memset(&m_irq_func, 0, sizeof(m_irq_func));
92    }
93}
7294
95
96
7397/***************************************************************************
74    INLINE FUNCTIONS
98    INTERNAL FUNCTIONS
7599***************************************************************************/
76100
77101/*-------------------------------------------------
78    get_safe_token - convert a device's token
79    into a riot6532_state
80-------------------------------------------------*/
81
82INLINE riot6532_state *get_safe_token(running_device *device)
83{
84   assert(device != NULL);
85   assert(device->type() == RIOT6532);
86   return (riot6532_state *)downcast<legacy_device_base *>(device)->token();
87}
88
89
90/*-------------------------------------------------
91102    update_irqstate - update the IRQ state
92103    based on interrupt enables
93104-------------------------------------------------*/
94105
95INLINE void update_irqstate(running_device *device)
106void riot6532_device::update_irqstate()
96107{
97   riot6532_state *riot = get_safe_token(device);
98   int state = (riot->irqstate & riot->irqenable);
108   int state = (m_irqstate & m_irqenable);
99109
100   if (riot->irq_func.write != NULL)
101      devcb_call_write_line(&riot->irq_func, (state != 0) ? ASSERT_LINE : CLEAR_LINE);
110   if (m_irq_func.write != NULL)
111    {
112      devcb_call_write_line(&m_irq_func, (state != 0) ? ASSERT_LINE : CLEAR_LINE);
113    }
102114   else
103      logerror("%s:6532RIOT chip #%d: no irq callback function\n", cpuexec_describe_context(device->machine), riot->index);
115    {
116      logerror("%s:6532RIOT chip #%d: no irq callback function\n", cpuexec_describe_context(&m_machine), m_index);
117    }
104118}
105119
106120
r8892r8893
109123    according to the DDR
110124-------------------------------------------------*/
111125
112INLINE UINT8 apply_ddr(const riot6532_port *port)
126UINT8 riot6532_device::apply_ddr(const riot6532_port *port)
113127{
114   return (port->out & port->ddr) | (port->in & ~port->ddr);
128   return (port->m_out & port->m_ddr) | (port->m_in & ~port->m_ddr);
115129}
116130
117131
r8892r8893
120134    and signal appropriately
121135-------------------------------------------------*/
122136
123INLINE void update_pa7_state(running_device *device)
137void riot6532_device::update_pa7_state()
124138{
125   riot6532_state *riot = get_safe_token(device);
126   UINT8 data = apply_ddr(&riot->port[0]) & 0x80;
139   UINT8 data = apply_ddr(&m_port[0]) & 0x80;
127140
128141   /* if the state changed in the correct direction, set the PA7 flag and update IRQs */
129   if ((riot->pa7prev ^ data) && (riot->pa7dir ^ data) == 0)
142   if ((m_pa7prev ^ data) && (m_pa7dir ^ data) == 0)
130143   {
131      riot->irqstate |= PA7_FLAG;
132      update_irqstate(device);
144      m_irqstate |= PA7_FLAG;
145      update_irqstate();
133146   }
134   riot->pa7prev = data;
147   m_pa7prev = data;
135148}
136149
137150
r8892r8893
139152    get_timer - return the current timer value
140153-------------------------------------------------*/
141154
142INLINE UINT8 get_timer(riot6532_state *riot)
155UINT8 riot6532_device::get_timer()
143156{
144157   /* if idle, return 0 */
145   if (riot->timerstate == TIMER_IDLE)
158   if (m_timerstate == TIMER_IDLE)
159    {
146160      return 0;
161    }
147162
148163   /* if counting, return the number of ticks remaining */
149   else if (riot->timerstate == TIMER_COUNTING)
150      return attotime_to_ticks(timer_timeleft(riot->timer), riot->device->clock()) >> riot->timershift;
164   else if (m_timerstate == TIMER_COUNTING)
165    {
166      return attotime_to_ticks(timer_timeleft(m_timer), clock()) >> m_timershift;
167    }
151168
152169   /* if finishing, return the number of ticks without the shift */
153170   else
154      return attotime_to_ticks(timer_timeleft(riot->timer), riot->device->clock());
171    {
172      return attotime_to_ticks(timer_timeleft(m_timer), clock());
173    }
155174}
156175
157176
158177
159/***************************************************************************
160    INTERNAL FUNCTIONS
161***************************************************************************/
162
163178/*-------------------------------------------------
164179    timer_end_callback - callback to process the
165180    timer
166181-------------------------------------------------*/
167182
168static TIMER_CALLBACK( timer_end_callback )
183TIMER_CALLBACK( riot6532_device::timer_end_callback )
169184{
170   running_device *device = (running_device *)ptr;
171   riot6532_state *riot = get_safe_token(device);
185    riot6532_device *via = reinterpret_cast<riot6532_device *>(ptr);
186    via->timer_end();
187}
172188
173   assert(riot->timerstate != TIMER_IDLE);
189void riot6532_device::timer_end()
190{
191   assert(m_timerstate != TIMER_IDLE);
174192
175193   /* if we finished counting, switch to the finishing state */
176   if (riot->timerstate == TIMER_COUNTING)
194   if(m_timerstate == TIMER_COUNTING)
177195   {
178      riot->timerstate = TIMER_FINISHING;
179      timer_adjust_oneshot(riot->timer, ticks_to_attotime(256, device->clock()), 0);
196      m_timerstate = TIMER_FINISHING;
197      timer_adjust_oneshot(m_timer, ticks_to_attotime(256, clock()), 0);
180198
181199      /* signal timer IRQ as well */
182      riot->irqstate |= TIMER_FLAG;
183      update_irqstate(device);
200      m_irqstate |= TIMER_FLAG;
201      update_irqstate();
184202   }
185203
186204   /* if we finished finishing, keep spinning */
187   else if (riot->timerstate == TIMER_FINISHING)
205   else if (m_timerstate == TIMER_FINISHING)
188206   {
189      timer_adjust_oneshot(riot->timer, ticks_to_attotime(256, device->clock()), 0);
207      timer_adjust_oneshot(m_timer, ticks_to_attotime(256, clock()), 0);
190208   }
191209}
192210
r8892r8893
202220
203221WRITE8_DEVICE_HANDLER( riot6532_w )
204222{
205   riot6532_state *riot = get_safe_token(device);
223    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
224    via->reg_w(offset, data);
225}
206226
227void riot6532_device::reg_w(UINT8 offset, UINT8 data)
228{
207229   /* if A4 == 1 and A2 == 1, we are writing to the timer */
208230   if ((offset & 0x14) == 0x14)
209231   {
210232      static const UINT8 timershift[4] = { 0, 3, 6, 10 };
211      attotime curtime = timer_get_time(device->machine);
233      attotime curtime = timer_get_time(&m_machine);
212234      INT64 target;
213235
214236      /* A0-A1 contain the timer divisor */
215      riot->timershift = timershift[offset & 3];
237      m_timershift = timershift[offset & 3];
216238
217239      /* A3 contains the timer IRQ enable */
218240      if (offset & 8)
219         riot->irqenable |= TIMER_FLAG;
241         m_irqenable |= TIMER_FLAG;
220242      else
221         riot->irqenable &= ~TIMER_FLAG;
243         m_irqenable &= ~TIMER_FLAG;
222244
223245      /* writes here clear the timer flag */
224      if (riot->timerstate != TIMER_FINISHING || get_timer(riot) != 0xff)
225         riot->irqstate &= ~TIMER_FLAG;
226      update_irqstate(device);
246      if (m_timerstate != TIMER_FINISHING || get_timer() != 0xff)
247        {
248         m_irqstate &= ~TIMER_FLAG;
249        }
250      update_irqstate();
227251
228252      /* update the timer */
229      riot->timerstate = TIMER_COUNTING;
230      target = attotime_to_ticks(curtime, device->clock()) + 1 + (data << riot->timershift);
231      timer_adjust_oneshot(riot->timer, attotime_sub(ticks_to_attotime(target, device->clock()), curtime), 0);
253      m_timerstate = TIMER_COUNTING;
254      target = attotime_to_ticks(curtime, clock()) + 1 + (data << m_timershift);
255      timer_adjust_oneshot(m_timer, attotime_sub(ticks_to_attotime(target, clock()), curtime), 0);
232256   }
233257
234258   /* if A4 == 0 and A2 == 1, we are writing to the edge detect control */
r8892r8893
236260   {
237261      /* A1 contains the A7 IRQ enable */
238262      if (offset & 2)
239         riot->irqenable |= PA7_FLAG;
263        {
264         m_irqenable |= PA7_FLAG;
265        }
240266      else
241         riot->irqenable &= ~PA7_FLAG;
267        {
268         m_irqenable &= ~PA7_FLAG;
269        }
242270
243271      /* A0 specifies the edge detect direction: 0=negative, 1=positive */
244      riot->pa7dir = (offset & 1) << 7;
272      m_pa7dir = (offset & 1) << 7;
245273   }
246274
247275   /* if A4 == anything and A2 == 0, we are writing to the I/O section */
248276   else
249277   {
250278      /* A1 selects the port */
251      riot6532_port *port = &riot->port[(offset >> 1) & 1];
279      riot6532_port *port = &m_port[(offset >> 1) & 1];
252280
253281      /* if A0 == 1, we are writing to the port's DDR */
254282      if (offset & 1)
255         port->ddr = data;
283        {
284         port->m_ddr = data;
285        }
256286
257287      /* if A0 == 0, we are writing to the port's output */
258288      else
259289      {
260         port->out = data;
261         if (port->out_func.write != NULL)
262            devcb_call_write8(&port->out_func, 0, data);
290         port->m_out = data;
291         if (port->m_out_func.write != NULL)
292            {
293            devcb_call_write8(&port->m_out_func, 0, data);
294            }
263295         else
264            logerror("%s:6532RIOT chip %s: Port %c is being written to but has no handler. %02X\n", cpuexec_describe_context(device->machine), device->tag(), 'A' + (offset & 1), data);
296            {
297            logerror("%s:6532RIOT chip %s: Port %c is being written to but has no handler. %02X\n", cpuexec_describe_context(&m_machine), tag(), 'A' + (offset & 1), data);
298            }
265299      }
266300
267301      /* writes to port A need to update the PA7 state */
268      if (port == &riot->port[0])
269         update_pa7_state(device);
302      if (port == &m_port[0])
303        {
304         update_pa7_state();
305        }
270306   }
271307}
272308
r8892r8893
277313
278314READ8_DEVICE_HANDLER( riot6532_r )
279315{
280   riot6532_state *riot = get_safe_token(device);
316    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
317    return via->reg_r(offset);
318}
319
320UINT8 riot6532_device::reg_r(UINT8 offset)
321{
281322   UINT8 val = 0;
282323
283324   /* if A2 == 1 and A0 == 1, we are reading interrupt flags */
284325   if ((offset & 0x05) == 0x05)
285326   {
286      val = riot->irqstate;
327      val = m_irqstate;
287328
288329      /* implicitly clears the PA7 flag */
289      riot->irqstate &= ~PA7_FLAG;
290      update_irqstate(device);
330      m_irqstate &= ~PA7_FLAG;
331      update_irqstate();
291332   }
292333
293334   /* if A2 == 1 and A0 == 0, we are reading the timer */
294335   else if ((offset & 0x05) == 0x04)
295336   {
296      val = get_timer(riot);
337      val = get_timer();
297338
298339      /* A3 contains the timer IRQ enable */
299340      if (offset & 8)
300         riot->irqenable |= TIMER_FLAG;
341        {
342         m_irqenable |= TIMER_FLAG;
343        }
301344      else
302         riot->irqenable &= ~TIMER_FLAG;
345        {
346         m_irqenable &= ~TIMER_FLAG;
347        }
303348
304349      /* implicitly clears the timer flag */
305      if (riot->timerstate != TIMER_FINISHING || val != 0xff)
306         riot->irqstate &= ~TIMER_FLAG;
307      update_irqstate(device);
350      if (m_timerstate != TIMER_FINISHING || val != 0xff)
351        {
352         m_irqstate &= ~TIMER_FLAG;
353        }
354      update_irqstate();
308355   }
309356
310357   /* if A2 == 0 and A0 == anything, we are reading from ports */
311358   else
312359   {
313360      /* A1 selects the port */
314      riot6532_port *port = &riot->port[(offset >> 1) & 1];
361      riot6532_port *port = &m_port[(offset >> 1) & 1];
315362
316363      /* if A0 == 1, we are reading the port's DDR */
317364      if (offset & 1)
318         val = port->ddr;
365        {
366         val = port->m_ddr;
367        }
319368
320369      /* if A0 == 0, we are reading the port as an input */
321370      else
322371      {
323372         /* call the input callback if it exists */
324         if (port->in_func.read != NULL)
373         if (port->m_in_func.read != NULL)
325374         {
326            port->in = devcb_call_read8(&port->in_func, 0);
375            port->m_in = devcb_call_read8(&port->m_in_func, 0);
327376
328377            /* changes to port A need to update the PA7 state */
329            if (port == &riot->port[0])
330               update_pa7_state(device);
378            if (port == &m_port[0])
379                {
380               update_pa7_state();
381                }
331382         }
332383         else
333            logerror("%s:6532RIOT chip %s: Port %c is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag(), 'A' + (offset & 1));
384            {
385            logerror("%s:6532RIOT chip %s: Port %c is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag(), 'A' + (offset & 1));
386            }
334387
335388         /* apply the DDR to the result */
336389         val = apply_ddr(port);
r8892r8893
341394
342395
343396/*-------------------------------------------------
344    riot6532_porta_in_set - set port A input
345    value
397    porta_in_set - set port A input value
346398-------------------------------------------------*/
347399
348400void riot6532_porta_in_set(running_device *device, UINT8 data, UINT8 mask)
349401{
350   riot6532_state *riot = get_safe_token(device);
351   riot->port[0].in = (riot->port[0].in & ~mask) | (data & mask);
352   update_pa7_state(device);
402    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
403    via->porta_in_set(data, mask);
353404}
354405
406void riot6532_device::porta_in_set(UINT8 data, UINT8 mask)
407{
408   m_port[0].m_in = (m_port[0].m_in & ~mask) | (data & mask);
409   update_pa7_state();
410}
355411
412
356413/*-------------------------------------------------
357    riot6532_portb_in_set - set port B input
358    value
414    portb_in_set - set port B input value
359415-------------------------------------------------*/
360416
361417void riot6532_portb_in_set(running_device *device, UINT8 data, UINT8 mask)
362418{
363   riot6532_state *riot = get_safe_token(device);
364   riot->port[1].in = (riot->port[1].in & ~mask) | (data & mask);
419    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
420    via->portb_in_set(data, mask);
365421}
366422
423void riot6532_device::portb_in_set(UINT8 data, UINT8 mask)
424{
425   m_port[1].m_in = (m_port[1].m_in & ~mask) | (data & mask);
426}
367427
428
368429/*-------------------------------------------------
369    riot6532_porta_in_get - return port A input
370    value
430    porta_in_get - return port A input value
371431-------------------------------------------------*/
372432
373433UINT8 riot6532_porta_in_get(running_device *device)
374434{
375   riot6532_state *riot = get_safe_token(device);
376   return riot->port[0].in;
435    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
436    return via->porta_in_get();
377437}
378438
439UINT8 riot6532_device::porta_in_get()
440{
441   return m_port[0].m_in;
442}
379443
444
380445/*-------------------------------------------------
381    riot6532_portb_in_get - return port B input
382    value
446    portb_in_get - return port B input value
383447-------------------------------------------------*/
384448
385449UINT8 riot6532_portb_in_get(running_device *device)
386450{
387   riot6532_state *riot = get_safe_token(device);
388   return riot->port[1].in;
451    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
452    return via->portb_in_get();
389453}
390454
455UINT8 riot6532_device::portb_in_get()
456{
457   return m_port[1].m_in;
458}
391459
460
392461/*-------------------------------------------------
393    riot6532_porta_in_get - return port A output
394    value
462    porta_in_get - return port A output value
395463-------------------------------------------------*/
396464
397465UINT8 riot6532_porta_out_get(running_device *device)
398466{
399   riot6532_state *riot = get_safe_token(device);
400   return riot->port[0].out;
467    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
468    return via->porta_out_get();
401469}
402470
471UINT8 riot6532_device::porta_out_get()
472{
473   return m_port[0].m_out;
474}
403475
476
404477/*-------------------------------------------------
405    riot6532_portb_in_get - return port B output
406    value
478    portb_in_get - return port B output value
407479-------------------------------------------------*/
408480
409481UINT8 riot6532_portb_out_get(running_device *device)
410482{
411   riot6532_state *riot = get_safe_token(device);
412   return riot->port[1].out;
483    riot6532_device *via = reinterpret_cast<riot6532_device *>(device);
484    return via->portb_out_get();
413485}
414486
487UINT8 riot6532_device::portb_out_get()
488{
489   return m_port[1].m_out;
490}
415491
416/***************************************************************************
417    DEVICE INTERFACE
418***************************************************************************/
419492
493//**************************************************************************
494//  LIVE DEVICE
495//**************************************************************************
496
497//-------------------------------------------------
498//  riot6532_device - constructor
499//-------------------------------------------------
500
501riot6532_device::riot6532_device(running_machine &_machine, const riot6532_device_config &config)
502    : device_t(_machine, config),
503      m_config(config)
504{
505}
506
420507/*-------------------------------------------------
421    riot6532_portb_r - return port B output
422    value
508    device_start - device-specific startup
423509-------------------------------------------------*/
424510
425static DEVICE_START( riot6532 )
511void riot6532_device::device_start()
426512{
427   riot6532_state *riot = get_safe_token(device);
428
429513   /* validate arguments */
430   assert(device != NULL);
514   assert(this != NULL);
431515
432516   /* set static values */
433   riot->device = device;
434   riot->intf = (riot6532_interface *)device->baseconfig().static_config();
435   riot->index = device->machine->m_devicelist.index(RIOT6532, device->tag());
517   m_index = m_machine.m_devicelist.index(RIOT6532, tag());
436518
437519   /* configure the ports */
438   devcb_resolve_read8(&riot->port[0].in_func, &riot->intf->in_a_func, device);
439   devcb_resolve_write8(&riot->port[0].out_func, &riot->intf->out_a_func, device);
440   devcb_resolve_read8(&riot->port[1].in_func, &riot->intf->in_b_func, device);
441   devcb_resolve_write8(&riot->port[1].out_func, &riot->intf->out_b_func, device);
520    devcb_resolve_read8(&m_port[0].m_in_func, &m_config.m_in_a_func, this);
521    devcb_resolve_write8(&m_port[0].m_out_func, &m_config.m_out_a_func, this);
522    devcb_resolve_read8(&m_port[1].m_in_func, &m_config.m_in_b_func, this);
523    devcb_resolve_write8(&m_port[1].m_out_func, &m_config.m_out_b_func, this);
442524
443525   /* resolve irq func */
444   devcb_resolve_write_line(&riot->irq_func, &riot->intf->irq_func, device);
526    devcb_resolve_write_line(&m_irq_func, &m_config.m_irq_func, this);
445527
446528   /* allocate timers */
447   riot->timer = timer_alloc(device->machine, timer_end_callback, (void *)device);
529    m_timer = timer_alloc(&m_machine, timer_end_callback, (void *)this);
448530
449531   /* register for save states */
450   state_save_register_device_item(device, 0, riot->port[0].in);
451   state_save_register_device_item(device, 0, riot->port[0].out);
452   state_save_register_device_item(device, 0, riot->port[0].ddr);
453   state_save_register_device_item(device, 0, riot->port[1].in);
454   state_save_register_device_item(device, 0, riot->port[1].out);
455   state_save_register_device_item(device, 0, riot->port[1].ddr);
532    state_save_register_device_item(this, 0, m_port[0].m_in);
533    state_save_register_device_item(this, 0, m_port[0].m_out);
534    state_save_register_device_item(this, 0, m_port[0].m_ddr);
535    state_save_register_device_item(this, 0, m_port[1].m_in);
536    state_save_register_device_item(this, 0, m_port[1].m_out);
537    state_save_register_device_item(this, 0, m_port[1].m_ddr);
456538
457   state_save_register_device_item(device, 0, riot->irqstate);
458   state_save_register_device_item(device, 0, riot->irqenable);
539    state_save_register_device_item(this, 0, m_irqstate);
540    state_save_register_device_item(this, 0, m_irqenable);
459541
460   state_save_register_device_item(device, 0, riot->pa7dir);
461   state_save_register_device_item(device, 0, riot->pa7prev);
542    state_save_register_device_item(this, 0, m_pa7dir);
543    state_save_register_device_item(this, 0, m_pa7prev);
462544
463   state_save_register_device_item(device, 0, riot->timershift);
464   state_save_register_device_item(device, 0, riot->timerstate);
545    state_save_register_device_item(this, 0, m_timershift);
546    state_save_register_device_item(this, 0, m_timerstate);
465547}
466548
467549
468static DEVICE_RESET( riot6532 )
550
551/*-------------------------------------------------
552    device_reset - device-specific reset
553-------------------------------------------------*/
554
555void riot6532_device::device_reset()
469556{
470   riot6532_state *riot = get_safe_token(device);
471
472557   /* reset I/O states */
473   riot->port[0].out = 0;
474   riot->port[0].ddr = 0;
475   riot->port[1].out = 0;
476   riot->port[1].ddr = 0;
558   m_port[0].m_out = 0;
559   m_port[0].m_ddr = 0;
560   m_port[1].m_out = 0;
561   m_port[1].m_ddr = 0;
477562
478563   /* reset IRQ states */
479   riot->irqenable = 0;
480   riot->irqstate = 0;
481   update_irqstate(device);
564   m_irqenable = 0;
565   m_irqstate = 0;
566   update_irqstate();
482567
483568   /* reset PA7 states */
484   riot->pa7dir = 0;
485   riot->pa7prev = 0;
569   m_pa7dir = 0;
570   m_pa7prev = 0;
486571
487572   /* reset timer states */
488   riot->timershift = 0;
489   riot->timerstate = TIMER_IDLE;
490   timer_adjust_oneshot(riot->timer, attotime_never, 0);
573   m_timershift = 0;
574   m_timerstate = TIMER_IDLE;
575   timer_adjust_oneshot(m_timer, attotime_never, 0);
491576}
492577
493
494DEVICE_GET_INFO( riot6532 )
495{
496   switch (state)
497   {
498      /* --- the following bits of info are returned as 64-bit signed integers --- */
499      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(riot6532_state);      break;
500      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                     break;
501
502      /* --- the following bits of info are returned as pointers to data or functions --- */
503      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(riot6532);break;
504      case DEVINFO_FCT_STOP:                     /* Nothing */                     break;
505      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(riot6532);break;
506
507      /* --- the following bits of info are returned as NULL-terminated strings --- */
508      case DEVINFO_STR_NAME:                     strcpy(info->s, "6532 (RIOT)");         break;
509      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "I/O devices");         break;
510      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");               break;
511      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);            break;
512      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright Nicola Salmoria and the MAME Team"); break;
513   }
514}
515
516
517DEFINE_LEGACY_DEVICE(RIOT6532, riot6532);
578const device_type RIOT6532 = riot6532_device_config::static_alloc_device_config;
src/emu/machine/6522via.c
r8892r8893
3434
3535
3636/***************************************************************************
37    TYPE DEFINITIONS
38***************************************************************************/
39
40/******************* internal VIA data structure *******************/
41
42typedef struct _via6522_t via6522_t;
43struct _via6522_t
44{
45   devcb_resolved_read8 in_a_func;
46   devcb_resolved_read8 in_b_func;
47   devcb_resolved_read_line in_ca1_func;
48   devcb_resolved_read_line in_cb1_func;
49   devcb_resolved_read_line in_ca2_func;
50   devcb_resolved_read_line in_cb2_func;
51   devcb_resolved_write8 out_a_func;
52   devcb_resolved_write8 out_b_func;
53   devcb_resolved_write_line out_ca1_func;
54   devcb_resolved_write_line out_cb1_func;
55   devcb_resolved_write_line out_ca2_func;
56   devcb_resolved_write_line out_cb2_func;
57   devcb_resolved_write_line irq_func;
58
59   UINT8 in_a;
60   UINT8 in_ca1;
61   UINT8 in_ca2;
62   UINT8 out_a;
63   UINT8 out_ca2;
64   UINT8 ddr_a;
65
66   UINT8 in_b;
67   UINT8 in_cb1;
68   UINT8 in_cb2;
69   UINT8 out_b;
70   UINT8 out_cb2;
71   UINT8 ddr_b;
72
73   UINT8 t1cl;
74   UINT8 t1ch;
75   UINT8 t1ll;
76   UINT8 t1lh;
77   UINT8 t2cl;
78   UINT8 t2ch;
79   UINT8 t2ll;
80   UINT8 t2lh;
81
82   UINT8 sr;
83   UINT8 pcr;
84   UINT8 acr;
85   UINT8 ier;
86   UINT8 ifr;
87
88   emu_timer *t1;
89   attotime time1;
90   UINT8 t1_active;
91   emu_timer *t2;
92   attotime time2;
93   UINT8 t2_active;
94
95   emu_timer *shift_timer;
96   UINT8 shift_counter;
97};
98
99
100/***************************************************************************
10137    MACROS
10238***************************************************************************/
10339
r8892r8893
15995#define INT_T1   0x40
16096#define INT_ANY   0x80
16197
162#define CLR_PA_INT(device)   via_clear_int (device, INT_CA1 | ((!CA2_IND_IRQ(get_token(device)->pcr)) ? INT_CA2: 0))
163#define CLR_PB_INT(device)   via_clear_int (device, INT_CB1 | ((!CB2_IND_IRQ(get_token(device)->pcr)) ? INT_CB2: 0))
98#define CLR_PA_INT()   clear_int(INT_CA1 | ((!CA2_IND_IRQ(m_pcr)) ? INT_CA2: 0))
99#define CLR_PB_INT()   clear_int(INT_CB1 | ((!CB2_IND_IRQ(m_pcr)) ? INT_CB2: 0))
164100
165101#define IFR_DELAY 3
166102
167#define TIMER1_VALUE(v) (v->t1ll+(v->t1lh<<8))
168#define TIMER2_VALUE(v) (v->t2ll+(v->t2lh<<8))
103#define TIMER1_VALUE    (m_t1ll+(m_t1lh<<8))
104#define TIMER2_VALUE    (m_t2ll+(m_t2lh<<8))
169105
170/***************************************************************************
171    PROTOTYPES
172***************************************************************************/
173106
174static TIMER_CALLBACK( via_shift_callback );
175static TIMER_CALLBACK( via_t1_timeout );
176static TIMER_CALLBACK( via_t2_timeout );
177107
108//**************************************************************************
109//  DEVICE CONFIGURATION
110//**************************************************************************
178111
179/***************************************************************************
180    INLINE FUNCTIONS
181***************************************************************************/
112//-------------------------------------------------
113//  via6522_device_config - constructor
114//-------------------------------------------------
182115
183INLINE via6522_t *get_token(running_device *device)
116via6522_device_config::via6522_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
117    : device_config(mconfig, static_alloc_device_config, "VIA6522", tag, owner, clock)
184118{
185   assert(device != NULL);
186   assert((device->type() == VIA6522));
187   return (via6522_t *) downcast<legacy_device_base *>(device)->token();
188119}
189120
190121
191INLINE const via6522_interface *get_interface(running_device *device)
122//-------------------------------------------------
123//  static_alloc_device_config - allocate a new
124//  configuration object
125//-------------------------------------------------
126
127device_config *via6522_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
192128{
193   assert(device != NULL);
194   assert((device->type() == VIA6522));
195   return (const via6522_interface *) device->baseconfig().static_config();
129    return global_alloc(via6522_device_config(mconfig, tag, owner, clock));
196130}
197131
198132
199INLINE attotime v_cycles_to_time(running_device *device, int c)
133//-------------------------------------------------
134//  alloc_device - allocate a new device object
135//-------------------------------------------------
136
137device_t *via6522_device_config::alloc_device(running_machine &machine) const
200138{
201   return attotime_mul(ATTOTIME_IN_HZ(device->clock()), c);
139    return auto_alloc(&machine, via6522_device(machine, *this));
202140}
203141
204142
205INLINE UINT32 v_time_to_cycles(running_device *device, attotime t)
143//-------------------------------------------------
144//  device_config_complete - perform any
145//  operations now that the configuration is
146//  complete
147//-------------------------------------------------
148
149void via6522_device_config::device_config_complete()
206150{
207   return attotime_to_double(attotime_mul(t, device->clock()));
151   // inherit a copy of the static data
152   const via6522_interface *intf = reinterpret_cast<const via6522_interface *>(static_config());
153   if (intf != NULL)
154      *static_cast<via6522_interface *>(this) = *intf;
155
156   // or initialize to defaults if none provided
157   else
158   {
159       memset(&m_in_a_func, 0, sizeof(m_in_a_func));
160       memset(&m_in_b_func, 0, sizeof(m_in_b_func));
161       memset(&m_in_ca1_func, 0, sizeof(m_in_ca1_func));
162       memset(&m_in_cb1_func, 0, sizeof(m_in_cb1_func));
163       memset(&m_in_ca2_func, 0, sizeof(m_in_ca2_func));
164       memset(&m_in_cb2_func, 0, sizeof(m_in_cb2_func));
165       memset(&m_out_a_func, 0, sizeof(m_out_a_func));
166       memset(&m_out_b_func, 0, sizeof(m_out_b_func));
167       memset(&m_out_ca2_func, 0, sizeof(m_out_ca2_func));
168       memset(&m_out_cb2_func, 0, sizeof(m_out_cb2_func));
169       memset(&m_irq_func, 0, sizeof(m_irq_func));
170   }
208171}
209172
210173
211INLINE UINT16 v_get_counter1_value(running_device *device)
174
175/***************************************************************************
176    INLINE FUNCTIONS
177***************************************************************************/
178
179attotime via6522_device::cycles_to_time(int c)
212180{
213   via6522_t *v = get_token(device);
181   return attotime_mul(ATTOTIME_IN_HZ(clock()), c);
182}
183
184
185UINT32 via6522_device::time_to_cycles(attotime t)
186{
187   return attotime_to_double(attotime_mul(t, clock()));
188}
189
190
191UINT16 via6522_device::get_counter1_value()
192{
214193   UINT16 val;
215194
216   if (v->t1_active) {
217      val = v_time_to_cycles(device, timer_timeleft(v->t1)) - IFR_DELAY;
218   } else {
219      val = 0xFFFF - v_time_to_cycles(device, attotime_sub(timer_get_time(device->machine), v->time1));
195    if(m_t1_active)
196    {
197        val = time_to_cycles(timer_timeleft(m_t1)) - IFR_DELAY;
220198   }
199    else
200    {
201        val = 0xffff - time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time1));
202   }
203
221204   return val;
222205}
223206
224207
225/***************************************************************************
226    IMPLEMENTATION
227***************************************************************************/
208//**************************************************************************
209//  LIVE DEVICE
210//**************************************************************************
228211
229/*-------------------------------------------------
230    DEVICE_START( via6522 )
231-------------------------------------------------*/
212const device_type VIA6522 = via6522_device_config::static_alloc_device_config;
232213
233static DEVICE_START( via6522 )
214//-------------------------------------------------
215//  via6522_device - constructor
216//-------------------------------------------------
217
218via6522_device::via6522_device(running_machine &_machine, const via6522_device_config &config)
219    : device_t(_machine, config),
220      m_config(config)
234221{
235   via6522_t *v = get_token(device);
236   const via6522_interface *intf = get_interface(device);
237222
238   memset(v, 0, sizeof(*v));
223}
239224
240   devcb_resolve_read8(&v->in_a_func, &intf->in_a_func, device);
241   devcb_resolve_read8(&v->in_b_func, &intf->in_b_func, device);
242   devcb_resolve_read_line(&v->in_ca1_func, &intf->in_ca1_func, device);
243   devcb_resolve_read_line(&v->in_cb1_func, &intf->in_cb1_func, device);
244   devcb_resolve_read_line(&v->in_ca2_func, &intf->in_ca2_func, device);
245   devcb_resolve_read_line(&v->in_cb2_func, &intf->in_cb2_func, device);
246   devcb_resolve_write8(&v->out_a_func, &intf->out_a_func, device);
247   devcb_resolve_write8(&v->out_b_func, &intf->out_b_func, device);
248   devcb_resolve_write_line(&v->out_ca1_func, &intf->out_ca1_func, device);
249   devcb_resolve_write_line(&v->out_cb1_func, &intf->out_cb1_func, device);
250   devcb_resolve_write_line(&v->out_ca2_func, &intf->out_ca2_func, device);
251   devcb_resolve_write_line(&v->out_cb2_func, &intf->out_cb2_func, device);
252   devcb_resolve_write_line(&v->irq_func, &intf->irq_func, device);
225//-------------------------------------------------
226//  device_start - device-specific startup
227//-------------------------------------------------
253228
254   v->t1ll = 0xf3; /* via at 0x9110 in vic20 show these values */
255   v->t1lh = 0xb5; /* ports are not written by kernel! */
256   v->t2ll = 0xff; /* taken from vice */
257   v->t2lh = 0xff;
258   v->time2 = v->time1 = timer_get_time(device->machine);
259   v->t1 = timer_alloc(device->machine, via_t1_timeout, (void *) device);
260   v->t2 = timer_alloc(device->machine, via_t2_timeout, (void *) device);
261   v->shift_timer = timer_alloc(device->machine, via_shift_callback, (void *) device);
229void via6522_device::device_start()
230{
231    devcb_resolve_read8(&m_in_a_func, &m_config.m_in_a_func, this);
232    devcb_resolve_read8(&m_in_b_func, &m_config.m_in_b_func, this);
233    devcb_resolve_read_line(&m_in_ca1_func, &m_config.m_in_ca1_func, this);
234    devcb_resolve_read_line(&m_in_cb1_func, &m_config.m_in_cb1_func, this);
235    devcb_resolve_read_line(&m_in_ca2_func, &m_config.m_in_ca2_func, this);
236    devcb_resolve_read_line(&m_in_cb2_func, &m_config.m_in_cb2_func, this);
237    devcb_resolve_write8(&m_out_a_func, &m_config.m_out_a_func, this);
238    devcb_resolve_write8(&m_out_b_func, &m_config.m_out_b_func, this);
239    devcb_resolve_write_line(&m_out_ca1_func, &m_config.m_out_ca1_func, this);
240    devcb_resolve_write_line(&m_out_cb1_func, &m_config.m_out_cb1_func, this);
241    devcb_resolve_write_line(&m_out_ca2_func, &m_config.m_out_ca2_func, this);
242    devcb_resolve_write_line(&m_out_cb2_func, &m_config.m_out_cb2_func, this);
243    devcb_resolve_write_line(&m_irq_func, &m_config.m_irq_func, this);
262244
245    m_t1ll = 0xf3; /* via at 0x9110 in vic20 show these values */
246    m_t1lh = 0xb5; /* ports are not written by kernel! */
247    m_t2ll = 0xff; /* taken from vice */
248    m_t2lh = 0xff;
249    m_time2 = m_time1 = timer_get_time(&m_machine);
250    m_t1 = timer_alloc(&m_machine, t1_timeout_callback, (void *)this);
251    m_t2 = timer_alloc(&m_machine, t2_timeout_callback, (void *)this);
252    m_shift_timer = timer_alloc(&m_machine, shift_callback, (void *)this);
253
263254   /* Default clock is from CPU1 */
264   if (device->clock() == 0)
265      device->set_unscaled_clock(device->machine->firstcpu->clock());
255   if (clock() == 0)
256    {
257      set_unscaled_clock(m_machine.firstcpu->clock());
258    }
266259
267260   /* save state register */
268   state_save_register_device_item(device, 0, v->in_a);
269   state_save_register_device_item(device, 0, v->in_ca1);
270   state_save_register_device_item(device, 0, v->in_ca2);
271   state_save_register_device_item(device, 0, v->out_a);
272   state_save_register_device_item(device, 0, v->out_ca2);
273   state_save_register_device_item(device, 0, v->ddr_a);
274   state_save_register_device_item(device, 0, v->in_b);
275   state_save_register_device_item(device, 0, v->in_cb1);
276   state_save_register_device_item(device, 0, v->in_cb2);
277   state_save_register_device_item(device, 0, v->out_b);
278   state_save_register_device_item(device, 0, v->out_cb2);
279   state_save_register_device_item(device, 0, v->ddr_b);
280   state_save_register_device_item(device, 0, v->t1cl);
281   state_save_register_device_item(device, 0, v->t1ch);
282   state_save_register_device_item(device, 0, v->t1ll);
283   state_save_register_device_item(device, 0, v->t1lh);
284   state_save_register_device_item(device, 0, v->t2cl);
285   state_save_register_device_item(device, 0, v->t2ch);
286   state_save_register_device_item(device, 0, v->t2ll);
287   state_save_register_device_item(device, 0, v->t2lh);
288   state_save_register_device_item(device, 0, v->sr);
289   state_save_register_device_item(device, 0, v->pcr);
290   state_save_register_device_item(device, 0, v->acr);
291   state_save_register_device_item(device, 0, v->ier);
292   state_save_register_device_item(device, 0, v->ifr);
293   state_save_register_device_item(device, 0, v->t1_active);
294   state_save_register_device_item(device, 0, v->t2_active);
295   state_save_register_device_item(device, 0, v->shift_counter);
261    state_save_register_device_item(this, 0, m_in_a);
262    state_save_register_device_item(this, 0, m_in_ca1);
263    state_save_register_device_item(this, 0, m_in_ca2);
264    state_save_register_device_item(this, 0, m_out_a);
265    state_save_register_device_item(this, 0, m_out_ca2);
266    state_save_register_device_item(this, 0, m_ddr_a);
267    state_save_register_device_item(this, 0, m_in_b);
268    state_save_register_device_item(this, 0, m_in_cb1);
269    state_save_register_device_item(this, 0, m_in_cb2);
270    state_save_register_device_item(this, 0, m_out_b);
271    state_save_register_device_item(this, 0, m_out_cb2);
272    state_save_register_device_item(this, 0, m_ddr_b);
273    state_save_register_device_item(this, 0, m_t1cl);
274    state_save_register_device_item(this, 0, m_t1ch);
275    state_save_register_device_item(this, 0, m_t1ll);
276    state_save_register_device_item(this, 0, m_t1lh);
277    state_save_register_device_item(this, 0, m_t2cl);
278    state_save_register_device_item(this, 0, m_t2ch);
279    state_save_register_device_item(this, 0, m_t2ll);
280    state_save_register_device_item(this, 0, m_t2lh);
281    state_save_register_device_item(this, 0, m_sr);
282    state_save_register_device_item(this, 0, m_pcr);
283    state_save_register_device_item(this, 0, m_acr);
284    state_save_register_device_item(this, 0, m_ier);
285    state_save_register_device_item(this, 0, m_ifr);
286    state_save_register_device_item(this, 0, m_t1_active);
287    state_save_register_device_item(this, 0, m_t2_active);
288    state_save_register_device_item(this, 0, m_shift_counter);
296289}
297290
298291
292//-------------------------------------------------
293//  device_reset - device-specific reset
294//-------------------------------------------------
295
296void via6522_device::device_reset()
297{
298    m_in_a = 0;
299    m_in_ca1 = 0;
300    m_in_ca2 = 0;
301    m_out_a = 0;
302    m_out_ca2 = 0;
303    m_ddr_a = 0;
304    m_in_b = 0;
305    m_in_cb1 = 0;
306    m_in_cb2 = 0;
307    m_out_b = 0;
308    m_out_cb2 = 0;
309    m_ddr_b = 0;
310
311    m_t1cl = 0;
312    m_t1ch = 0;
313    m_t2cl = 0;
314    m_t2ch = 0;
315
316    m_sr = 0;
317    m_pcr = 0;
318    m_acr = 0;
319    m_ier = 0;
320    m_ifr = 0;
321    m_t1_active = 0;
322    m_t2_active = 0;
323    m_shift_counter = 0;
324}
325
326
299327/*-------------------------------------------------
300328    via_set_int - external interrupt check
301329-------------------------------------------------*/
302330
303static void via_set_int (running_device *device, int data)
331void via6522_device::set_int(int data)
304332{
305   via6522_t *v = get_token(device);
306
307   v->ifr |= data;
333   m_ifr |= data;
308334   if (TRACE_VIA)
309      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), v->ifr);
335    {
336      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(&m_machine), tag(), m_ifr);
337    }
310338
311   if (v->ier & v->ifr)
339   if (m_ier & m_ifr)
312340    {
313      v->ifr |= INT_ANY;
314      devcb_call_write_line(&v->irq_func, ASSERT_LINE);
341      m_ifr |= INT_ANY;
342      devcb_call_write_line(&m_irq_func, ASSERT_LINE);
315343    }
316344}
317345
r8892r8893
320348    via_clear_int - external interrupt check
321349-------------------------------------------------*/
322350
323static void via_clear_int (running_device *device, int data)
351void via6522_device::clear_int(int data)
324352{
325   via6522_t *v = get_token(device);
353   m_ifr = (m_ifr & ~data) & 0x7f;
326354
327   v->ifr = (v->ifr & ~data) & 0x7f;
328
329355   if (TRACE_VIA)
330      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), v->ifr);
356    {
357      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(&m_machine), tag(), m_ifr);
358    }
331359
332   if (v->ifr & v->ier)
333      v->ifr |= INT_ANY;
360   if (m_ifr & m_ier)
361    {
362      m_ifr |= INT_ANY;
363    }
334364   else
335365   {
336      devcb_call_write_line(&v->irq_func, CLEAR_LINE);
366      devcb_call_write_line(&m_irq_func, CLEAR_LINE);
337367   }
338368}
339369
r8892r8893
342372    via_shift
343373-------------------------------------------------*/
344374
345static void via_shift(running_device *device)
375void via6522_device::shift()
346376{
347   via6522_t *v = get_token(device);
348
349   if (SO_O2_CONTROL(v->acr))
377   if (SO_O2_CONTROL(m_acr))
350378   {
351      v->out_cb2 = (v->sr >> 7) & 1;
352      v->sr =  (v->sr << 1) | v->out_cb2;
379      m_out_cb2 = (m_sr >> 7) & 1;
380      m_sr =  (m_sr << 1) | m_out_cb2;
353381
354      devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
382      devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
355383
356      v->in_cb1=1;
384      m_in_cb1=1;
357385
358386      /* this should be one cycle wide */
359      devcb_call_write_line(&v->out_cb1_func, 0);
360      devcb_call_write_line(&v->out_cb1_func, 1);
387      devcb_call_write_line(&m_out_cb1_func, 0);
388      devcb_call_write_line(&m_out_cb1_func, 1);
361389
362      v->shift_counter = (v->shift_counter + 1) % 8;
390      m_shift_counter = (m_shift_counter + 1) % 8;
363391
364      if (v->shift_counter)
365         timer_adjust_oneshot(v->shift_timer, v_cycles_to_time(device, 2), 0);
392      if (m_shift_counter)
393        {
394         timer_adjust_oneshot(m_shift_timer, cycles_to_time(2), 0);
395        }
366396      else
367397      {
368         if (!(v->ifr & INT_SR))
369            via_set_int(device, INT_SR);
398         if (!(m_ifr & INT_SR))
399            {
400            set_int(INT_SR);
401            }
370402      }
371403   }
372   if (SO_EXT_CONTROL(v->acr))
404   if (SO_EXT_CONTROL(m_acr))
373405   {
374      v->out_cb2 = (v->sr >> 7) & 1;
375      v->sr =  (v->sr << 1) | v->out_cb2;
406        m_out_cb2 = (m_sr >> 7) & 1;
407        m_sr =  (m_sr << 1) | m_out_cb2;
376408
377      devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
409        devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
378410
379      v->shift_counter = (v->shift_counter + 1) % 8;
411        m_shift_counter = (m_shift_counter + 1) % 8;
380412
381      if (v->shift_counter == 0)
413        if (m_shift_counter == 0)
382414      {
383         if (!(v->ifr & INT_SR))
384            via_set_int(device, INT_SR);
415            if (!(m_ifr & INT_SR))
416            {
417            set_int(INT_SR);
418            }
385419      }
386420   }
387   if (SI_EXT_CONTROL(v->acr))
421    if (SI_EXT_CONTROL(m_acr))
388422   {
389      if (v->in_cb2_func.read != NULL)
390         v->in_cb2 = devcb_call_read_line(&v->in_cb2_func);
423        if (m_in_cb2_func.read != NULL)
424        {
425            m_in_cb2 = devcb_call_read_line(&m_in_cb2_func);
426        }
391427
392      v->sr =  (v->sr << 1) | (v->in_cb2 & 1);
428        m_sr =  (m_sr << 1) | (m_in_cb2 & 1);
393429
394      v->shift_counter = (v->shift_counter + 1) % 8;
430        m_shift_counter = (m_shift_counter + 1) % 8;
395431
396      if (v->shift_counter == 0)
432        if (m_shift_counter == 0)
397433      {
398         if (!(v->ifr & INT_SR))
434            if (!(m_ifr & INT_SR))
399435         {
400            via_set_int(device, INT_SR);
436            set_int(INT_SR);
401437         }
402438      }
403439   }
r8892r8893
408444    TIMER_CALLBACK( via_shift_callback )
409445-------------------------------------------------*/
410446
411static TIMER_CALLBACK( via_shift_callback )
447TIMER_CALLBACK( via6522_device::shift_callback )
412448{
413   running_device *device = (running_device *)ptr;
414   via_shift(device);
449    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
450    via->shift();
415451}
416452
417453
r8892r8893
419455    TIMER_CALLBACK( via_t1_timeout )
420456-------------------------------------------------*/
421457
422static TIMER_CALLBACK( via_t1_timeout )
458TIMER_CALLBACK( via6522_device::t1_timeout_callback )
423459{
424   running_device *device = (running_device *)ptr;
425   via6522_t *v = get_token(device);
460    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
461    via->t1_timeout();
462}
426463
427   if (T1_CONTINUOUS (v->acr))
464void via6522_device::t1_timeout()
465{
466    if (T1_CONTINUOUS (m_acr))
428467    {
429      if (T1_SET_PB7(v->acr))
430         v->out_b ^= 0x80;
431      timer_adjust_oneshot(v->t1, v_cycles_to_time(device, TIMER1_VALUE(v) + IFR_DELAY), 0);
468        if (T1_SET_PB7(m_acr))
469        {
470            m_out_b ^= 0x80;
471        }
472        timer_adjust_oneshot(m_t1, cycles_to_time(TIMER1_VALUE + IFR_DELAY), 0);
432473    }
433474   else
434475    {
435      if (T1_SET_PB7(v->acr))
436         v->out_b |= 0x80;
437      v->t1_active = 0;
438      v->time1 = timer_get_time(device->machine);
476        if (T1_SET_PB7(m_acr))
477        {
478            m_out_b |= 0x80;
479        }
480        m_t1_active = 0;
481        m_time1 = timer_get_time(&m_machine);
439482    }
440   if (v->ddr_b)
483    if (m_ddr_b)
441484   {
442      UINT8 write_data = (v->out_b & v->ddr_b) | (v->ddr_b ^ 0xff);
443      devcb_call_write8(&v->out_b_func, 0, write_data);
485        UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
486        devcb_call_write8(&m_out_b_func, 0, write_data);
444487   }
445488
446   if (!(v->ifr & INT_T1))
447      via_set_int (device, INT_T1);
489    if (!(m_ifr & INT_T1))
490    {
491      set_int(INT_T1);
492    }
448493}
449494
450495
r8892r8893
452497    TIMER_CALLBACK( via_t2_timeout )
453498-------------------------------------------------*/
454499
455static TIMER_CALLBACK( via_t2_timeout )
500TIMER_CALLBACK( via6522_device::t2_timeout_callback )
456501{
457   running_device *device = (running_device *)ptr;
458   via6522_t *v = get_token(device);
459
460   v->t2_active = 0;
461   v->time2 = timer_get_time(device->machine);
462
463   if (!(v->ifr & INT_T2))
464      via_set_int (device, INT_T2);
502    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
503    via->t2_timeout();
465504}
466505
467
468/*-------------------------------------------------
469    DEVICE_RESET( via6522 )
470-------------------------------------------------*/
471
472static DEVICE_RESET( via6522 )
506void via6522_device::t2_timeout()
473507{
474   via6522_t *v = get_token(device);
475   v->in_a = 0;
476   v->in_ca1 = 0;
477   v->in_ca2 = 0;
478   v->out_a = 0;
479   v->out_ca2 = 0;
480   v->ddr_a = 0;
481   v->in_b = 0;
482   v->in_cb1 = 0;
483   v->in_cb2 = 0;
484   v->out_b = 0;
485   v->out_cb2 = 0;
486   v->ddr_b = 0;
508    m_t2_active = 0;
509    m_time2 = timer_get_time(&m_machine);
487510
488   v->t1cl = 0;
489   v->t1ch = 0;
490   v->t2cl = 0;
491   v->t2ch = 0;
492
493   v->sr = 0;
494   v->pcr = 0;
495   v->acr = 0;
496   v->ier = 0;
497   v->ifr = 0;
498   v->t1_active = 0;
499   v->t2_active = 0;
500   v->shift_counter = 0;
511    if (!(m_ifr & INT_T2))
512    {
513      set_int(INT_T2);
514    }
501515}
502516
503517
r8892r8893
505519    via_r - CPU interface for VIA read
506520-------------------------------------------------*/
507521
508READ8_DEVICE_HANDLER(via_r)
522UINT8 via6522_device::reg_r(UINT8 offset)
509523{
510   via6522_t *v = get_token(device);
511524   int val = 0;
512525
513526   offset &= 0xf;
r8892r8893
516529    {
517530    case VIA_PB:
518531      /* update the input */
519      if (PB_LATCH_ENABLE(v->acr) == 0)
532        if (PB_LATCH_ENABLE(m_acr) == 0)
520533      {
521         if (v->ddr_b != 0xff)
534         if (m_ddr_b != 0xff)
522535         {
523            if (v->in_b_func.read != NULL)
524               v->in_b = devcb_call_read8(&v->in_b_func, 0);
536            if (m_in_b_func.read != NULL)
537                {
538               m_in_b = devcb_call_read8(&m_in_b_func, 0);
539                }
525540            else
526               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
541                {
542               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
543                }
527544         }
528545      }
529546
530      CLR_PB_INT(device);
547      CLR_PB_INT();
531548
532549      /* combine input and output values, hold DDRB bit 7 high if T1_SET_PB7 */
533      if (T1_SET_PB7(v->acr))
534         val = (v->out_b & (v->ddr_b | 0x80)) | (v->in_b & ~(v->ddr_b | 0x80));
550      if (T1_SET_PB7(m_acr))
551        {
552         val = (m_out_b & (m_ddr_b | 0x80)) | (m_in_b & ~(m_ddr_b | 0x80));
553        }
535554      else
536         val = (v->out_b & v->ddr_b) + (v->in_b & ~v->ddr_b);
555        {
556         val = (m_out_b & m_ddr_b) + (m_in_b & ~m_ddr_b);
557        }
537558      break;
538559
539560    case VIA_PA:
540561      /* update the input */
541      if (PA_LATCH_ENABLE(v->acr) == 0)
562      if (PA_LATCH_ENABLE(m_acr) == 0)
542563      {
543         if (v->ddr_a != 0xff)
564         if (m_ddr_a != 0xff)
544565         {
545            if (v->in_a_func.read != NULL)
546               v->in_a = devcb_call_read8(&v->in_a_func, 0);
566            if (m_in_a_func.read != NULL)
567                {
568               m_in_a = devcb_call_read8(&m_in_a_func, 0);
569                }
547570            else
548               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
571                {
572               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
573                }
549574         }
550575      }
551576
552577      /* combine input and output values */
553      val = (v->out_a & v->ddr_a) + (v->in_a & ~v->ddr_a);
578      val = (m_out_a & m_ddr_a) + (m_in_a & ~m_ddr_a);
554579
555      CLR_PA_INT(device);
580      CLR_PA_INT();
556581
557582      /* If CA2 is configured as output and in pulse or handshake mode,
558583           CA2 is set now */
559      if (CA2_AUTO_HS(v->pcr))
584      if (CA2_AUTO_HS(m_pcr))
560585      {
561         if (v->out_ca2)
586         if (m_out_ca2)
562587         {
563588            /* set CA2 */
564            v->out_ca2 = 0;
589            m_out_ca2 = 0;
565590
566591            /* call the CA2 output function */
567            devcb_call_write_line(&v->out_ca2_func, 0);
592            devcb_call_write_line(&m_out_ca2_func, 0);
568593         }
569594      }
570595
r8892r8893
572597
573598    case VIA_PANH:
574599      /* update the input */
575      if (PA_LATCH_ENABLE(v->acr) == 0)
600      if (PA_LATCH_ENABLE(m_acr) == 0)
576601      {
577         if (v->in_a_func.read != NULL)
578            v->in_a = devcb_call_read8(&v->in_a_func, 0);
602         if (m_in_a_func.read != NULL)
603            {
604            m_in_a = devcb_call_read8(&m_in_a_func, 0);
605            }
579606         else
580            logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
607            {
608            logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
609            }
581610      }
582611
583612      /* combine input and output values */
584      val = (v->out_a & v->ddr_a) + (v->in_a & ~v->ddr_a);
613      val = (m_out_a & m_ddr_a) + (m_in_a & ~m_ddr_a);
585614      break;
586615
587616    case VIA_DDRB:
588      val = v->ddr_b;
617      val = m_ddr_b;
589618      break;
590619
591620    case VIA_DDRA:
592      val = v->ddr_a;
621      val = m_ddr_a;
593622      break;
594623
595624    case VIA_T1CL:
596      via_clear_int (device, INT_T1);
597      val = v_get_counter1_value(device) & 0xFF;
625      clear_int(INT_T1);
626      val = get_counter1_value() & 0xFF;
598627      break;
599628
600629    case VIA_T1CH:
601      val = v_get_counter1_value(device) >> 8;
630      val = get_counter1_value() >> 8;
602631      break;
603632
604633    case VIA_T1LL:
605      val = v->t1ll;
634      val = m_t1ll;
606635      break;
607636
608637    case VIA_T1LH:
609      val = v->t1lh;
638      val = m_t1lh;
610639      break;
611640
612641    case VIA_T2CL:
613      via_clear_int (device, INT_T2);
614      if (v->t2_active)
615         val = v_time_to_cycles(device, timer_timeleft(v->t2)) & 0xff;
642      clear_int(INT_T2);
643      if (m_t2_active)
644        {
645         val = time_to_cycles(timer_timeleft(m_t2)) & 0xff;
646        }
616647      else
617648      {
618         if (T2_COUNT_PB6(v->acr))
619            val = v->t2cl;
649         if (T2_COUNT_PB6(m_acr))
650            {
651            val = m_t2cl;
652            }
620653         else
621            val = (0x10000- (v_time_to_cycles(device, attotime_sub(timer_get_time(device->machine), v->time2)) & 0xffff) - 1) & 0xff;
654            {
655            val = (0x10000 - (time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time2)) & 0xffff) - 1) & 0xff;
656            }
622657      }
623658      break;
624659
625660    case VIA_T2CH:
626      if (v->t2_active)
627         val = v_time_to_cycles(device, timer_timeleft(v->t2)) >> 8;
661      if (m_t2_active)
662        {
663         val = time_to_cycles(timer_timeleft(m_t2)) >> 8;
664        }
628665      else
629666      {
630         if (T2_COUNT_PB6(v->acr))
631            val = v->t2ch;
667         if (T2_COUNT_PB6(m_acr))
668            {
669            val = m_t2ch;
670            }
632671         else
633            val = (0x10000- (v_time_to_cycles(device, attotime_sub(timer_get_time(device->machine), v->time2)) & 0xffff) - 1) >> 8;
672            {
673            val = (0x10000 - (time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time2)) & 0xffff) - 1) >> 8;
674            }
634675      }
635676      break;
636677
637678    case VIA_SR:
638      val = v->sr;
639      via_clear_int(device, INT_SR);
640      if (SO_O2_CONTROL(v->acr))
679      val = m_sr;
680      clear_int(INT_SR);
681      if (SO_O2_CONTROL(m_acr))
641682      {
642         v->shift_counter=0;
643         timer_adjust_oneshot(v->shift_timer, v_cycles_to_time(device, 2), 0);
683         m_shift_counter=0;
684         timer_adjust_oneshot(m_shift_timer, cycles_to_time(2), 0);
644685      }
645686      break;
646687
647688    case VIA_PCR:
648      val = v->pcr;
689      val = m_pcr;
649690      break;
650691
651692    case VIA_ACR:
652      val = v->acr;
693      val = m_acr;
653694      break;
654695
655696    case VIA_IER:
656      val = v->ier | 0x80;
697      val = m_ier | 0x80;
657698      break;
658699
659700    case VIA_IFR:
660      val = v->ifr;
701      val = m_ifr;
661702      break;
662703    }
663704   return val;
r8892r8893
668709    via_w - CPU interface for VIA write
669710-------------------------------------------------*/
670711
671WRITE8_DEVICE_HANDLER(via_w)
712void via6522_device::reg_w(UINT8 offset, UINT8 data)
672713{
673   via6522_t *v = get_token(device);
674
675714   offset &=0x0f;
676715
677716   switch (offset)
678717    {
679718    case VIA_PB:
680      if (T1_SET_PB7(v->acr))
681         v->out_b = (v->out_b & 0x80) | (data  & 0x7f);
719      if (T1_SET_PB7(m_acr))
720         m_out_b = (m_out_b & 0x80) | (data  & 0x7f);
682721      else
683         v->out_b = data;
722         m_out_b = data;
684723
685      if (v->ddr_b)
724      if (m_ddr_b)
686725      {
687         UINT8 write_data = (v->out_b & v->ddr_b) | (v->ddr_b ^ 0xff);
688         devcb_call_write8(&v->out_b_func, 0, write_data);
726         UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
727         devcb_call_write8(&m_out_b_func, 0, write_data);
689728      }
690729
691      CLR_PB_INT(device);
730      CLR_PB_INT();
692731
693732      /* If CB2 is configured as output and in pulse or handshake mode,
694733           CB2 is set now */
695      if (CB2_AUTO_HS(v->pcr))
734      if (CB2_AUTO_HS(m_pcr))
696735      {
697         if (v->out_cb2)
736         if (m_out_cb2)
698737         {
699738            /* set CB2 */
700            v->out_cb2 = 0;
739            m_out_cb2 = 0;
701740
702741            /* call the CB2 output function */
703            devcb_call_write_line(&v->out_cb2_func, 0);
742            devcb_call_write_line(&m_out_cb2_func, 0);
704743         }
705744      }
706745      break;
707746
708747    case VIA_PA:
709      v->out_a = data;
748      m_out_a = data;
710749
711      if (v->ddr_a)
750      if (m_ddr_a)
712751      {
713         UINT8 write_data = (v->out_a & v->ddr_a) | (v->ddr_a ^ 0xff);
714         devcb_call_write8(&v->out_a_func, 0, write_data);
752         UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
753         devcb_call_write8(&m_out_a_func, 0, write_data);
715754      }
716755
717      CLR_PA_INT(device);
756      CLR_PA_INT();
718757
719758      /* If CA2 is configured as output and in pulse or handshake mode,
720759           CA2 is set now */
721      if (CA2_PULSE_OUTPUT(v->pcr))
760      if (CA2_PULSE_OUTPUT(m_pcr))
722761      {
723762         /* call the CA2 output function */
724         devcb_call_write_line(&v->out_ca2_func, 0);
725         devcb_call_write_line(&v->out_ca2_func, 1);
763         devcb_call_write_line(&m_out_ca2_func, 0);
764         devcb_call_write_line(&m_out_ca2_func, 1);
726765
727766         /* set CA2 (shouldn't be needed) */
728         v->out_ca2 = 1;
767         m_out_ca2 = 1;
729768      }
730      else if (CA2_AUTO_HS(v->pcr))
769      else if (CA2_AUTO_HS(m_pcr))
731770      {
732         if (v->out_ca2)
771         if (m_out_ca2)
733772         {
734773            /* set CA2 */
735            v->out_ca2 = 0;
774            m_out_ca2 = 0;
736775
737776            /* call the CA2 output function */
738            devcb_call_write_line(&v->out_ca2_func, 0);
777            devcb_call_write_line(&m_out_ca2_func, 0);
739778         }
740779      }
741780
742781      break;
743782
744783    case VIA_PANH:
745      v->out_a = data;
784      m_out_a = data;
746785
747      if (v->ddr_a)
786      if (m_ddr_a)
748787      {
749         UINT8 write_data = (v->out_a & v->ddr_a) | (v->ddr_a ^ 0xff);
750         devcb_call_write8(&v->out_a_func, 0, write_data);
788         UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
789         devcb_call_write8(&m_out_a_func, 0, write_data);
751790      }
752791
753792      break;
754793
755794    case VIA_DDRB:
756795       /* EHC 03/04/2000 - If data direction changed, present output on the lines */
757       if ( data != v->ddr_b )
796       if ( data != m_ddr_b )
758797       {
759         v->ddr_b = data;
798         m_ddr_b = data;
760799
761         //if (v->ddr_b)
800         //if (m_ddr_b)
762801         {
763            UINT8 write_data = (v->out_b & v->ddr_b) | (v->ddr_b ^ 0xff);
764            devcb_call_write8(&v->out_b_func, 0, write_data);
802            UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
803            devcb_call_write8(&m_out_b_func, 0, write_data);
765804         }
766805      }
767806      break;
768807
769808    case VIA_DDRA:
770809       /* EHC 03/04/2000 - If data direction changed, present output on the lines */
771       if ( data != v->ddr_a )
810       if ( data != m_ddr_a )
772811       {
773         v->ddr_a = data;
812         m_ddr_a = data;
774813
775         //if (v->ddr_a)
814         //if (m_ddr_a)
776815         {
777            UINT8 write_data = (v->out_a & v->ddr_a) | (v->ddr_a ^ 0xff);
778            devcb_call_write8(&v->out_a_func, 0, write_data);
816            UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
817            devcb_call_write8(&m_out_a_func, 0, write_data);
779818         }
780819      }
781820      break;
782821
783822    case VIA_T1CL:
784823    case VIA_T1LL:
785      v->t1ll = data;
824      m_t1ll = data;
786825      break;
787826
788827   case VIA_T1LH:
789       v->t1lh = data;
790       via_clear_int (device, INT_T1);
828       m_t1lh = data;
829       clear_int(INT_T1);
791830       break;
792831
793832    case VIA_T1CH:
794      v->t1ch = v->t1lh = data;
795      v->t1cl = v->t1ll;
833      m_t1ch = m_t1lh = data;
834      m_t1cl = m_t1ll;
796835
797      via_clear_int (device, INT_T1);
836      clear_int(INT_T1);
798837
799      if (T1_SET_PB7(v->acr))
838      if (T1_SET_PB7(m_acr))
800839      {
801         v->out_b &= 0x7f;
840         m_out_b &= 0x7f;
802841
803         //if (v->ddr_b)
842         //if (m_ddr_b)
804843         {
805            UINT8 write_data = (v->out_b & v->ddr_b) | (v->ddr_b ^ 0xff);
806            devcb_call_write8(&v->out_b_func, 0, write_data);
844            UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
845            devcb_call_write8(&m_out_b_func, 0, write_data);
807846         }
808847      }
809      timer_adjust_oneshot(v->t1, v_cycles_to_time(device, TIMER1_VALUE(v) + IFR_DELAY), 0);
810      v->t1_active = 1;
848      timer_adjust_oneshot(m_t1, cycles_to_time(TIMER1_VALUE + IFR_DELAY), 0);
849      m_t1_active = 1;
811850      break;
812851
813852    case VIA_T2CL:
814      v->t2ll = data;
853      m_t2ll = data;
815854      break;
816855
817856    case VIA_T2CH:
818      v->t2ch = v->t2lh = data;
819      v->t2cl = v->t2ll;
857      m_t2ch = m_t2lh = data;
858      m_t2cl = m_t2ll;
820859
821      via_clear_int (device, INT_T2);
860      clear_int(INT_T2);
822861
823      if (!T2_COUNT_PB6(v->acr))
862      if (!T2_COUNT_PB6(m_acr))
824863      {
825         timer_adjust_oneshot(v->t2, v_cycles_to_time(device, TIMER2_VALUE(v) + IFR_DELAY), 0);
826         v->t2_active = 1;
864         timer_adjust_oneshot(m_t2, cycles_to_time(TIMER2_VALUE + IFR_DELAY), 0);
865         m_t2_active = 1;
827866      }
828867      else
829868      {
830         timer_adjust_oneshot(v->t2, v_cycles_to_time(device, TIMER2_VALUE(v)), 0);
831         v->t2_active = 1;
832         v->time2 = timer_get_time(device->machine);
869         timer_adjust_oneshot(m_t2, cycles_to_time(TIMER2_VALUE), 0);
870         m_t2_active = 1;
871         m_time2 = timer_get_time(&m_machine);
833872      }
834873      break;
835874
836875    case VIA_SR:
837      v->sr = data;
838      v->shift_counter=0;
839      via_clear_int(device, INT_SR);
840      if (SO_O2_CONTROL(v->acr))
876      m_sr = data;
877      m_shift_counter=0;
878      clear_int(INT_SR);
879      if (SO_O2_CONTROL(m_acr))
841880      {
842         timer_set(device->machine, v_cycles_to_time(device, 2), (void *) device, 0, via_shift_callback);
881         timer_set(&m_machine, cycles_to_time(2), (void *)this, 0, shift_callback);
843882      }
844883      break;
845884
846885    case VIA_PCR:
847      v->pcr = data;
886      m_pcr = data;
848887
849888      if (TRACE_VIA)
850         logerror("%s:6522VIA chip %s: PCR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), data);
889        {
890         logerror("%s:6522VIA chip %s: PCR = %02X\n", cpuexec_describe_context(&m_machine), tag(), data);
891        }
851892
852      if (CA2_FIX_OUTPUT(data) && CA2_OUTPUT_LEVEL(data) ^ v->out_ca2)
893      if (CA2_FIX_OUTPUT(data) && CA2_OUTPUT_LEVEL(data) ^ m_out_ca2)
853894      {
854         v->out_ca2 = CA2_OUTPUT_LEVEL(data);
855         devcb_call_write_line(&v->out_ca2_func, v->out_ca2);
895         m_out_ca2 = CA2_OUTPUT_LEVEL(data);
896         devcb_call_write_line(&m_out_ca2_func, m_out_ca2);
856897      }
857898
858      if (CB2_FIX_OUTPUT(data) && CB2_OUTPUT_LEVEL(data) ^ v->out_cb2)
899      if (CB2_FIX_OUTPUT(data) && CB2_OUTPUT_LEVEL(data) ^ m_out_cb2)
859900      {
860         v->out_cb2 = CB2_OUTPUT_LEVEL(data);
861         devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
901         m_out_cb2 = CB2_OUTPUT_LEVEL(data);
902         devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
862903      }
863904      break;
864905
865906    case VIA_ACR:
866907      {
867         UINT16 counter1 = v_get_counter1_value(device);
868         v->acr = data;
869         if (T1_SET_PB7(v->acr))
908         UINT16 counter1 = get_counter1_value();
909         m_acr = data;
910         if (T1_SET_PB7(m_acr))
870911         {
871            if (v->t1_active)
872               v->out_b &= ~0x80;
912            if (m_t1_active)
913                {
914               m_out_b &= ~0x80;
915                }
873916            else
874               v->out_b |= 0x80;
917                {
918               m_out_b |= 0x80;
919                }
875920
876            //if (v->ddr_b)
921            //if (m_ddr_b)
877922            {
878               UINT8 write_data = (v->out_b & v->ddr_b) | (v->ddr_b ^ 0xff);
879               devcb_call_write8(&v->out_b_func, 0, write_data);
923               UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
924               devcb_call_write8(&m_out_b_func, 0, write_data);
880925            }
881926         }
882927         if (T1_CONTINUOUS(data))
883928         {
884            timer_adjust_oneshot(v->t1, v_cycles_to_time(device, counter1 + IFR_DELAY), 0);
885            v->t1_active = 1;
929            timer_adjust_oneshot(m_t1, cycles_to_time(counter1 + IFR_DELAY), 0);
930            m_t1_active = 1;
886931         }
887932      }
888933      break;
889934
890935   case VIA_IER:
891936      if (data & 0x80)
892         v->ier |= data & 0x7f;
937        {
938         m_ier |= data & 0x7f;
939        }
893940      else
894         v->ier &= ~(data & 0x7f);
941        {
942         m_ier &= ~(data & 0x7f);
943        }
895944
896      if (v->ifr & INT_ANY)
945      if (m_ifr & INT_ANY)
897946      {
898         if (((v->ifr & v->ier) & 0x7f) == 0)
947         if (((m_ifr & m_ier) & 0x7f) == 0)
899948         {
900            v->ifr &= ~INT_ANY;
901            devcb_call_write_line(&v->irq_func, CLEAR_LINE);
949            m_ifr &= ~INT_ANY;
950            devcb_call_write_line(&m_irq_func, CLEAR_LINE);
902951         }
903952      }
904953      else
905954      {
906         if ((v->ier & v->ifr) & 0x7f)
955         if ((m_ier & m_ifr) & 0x7f)
907956         {
908            v->ifr |= INT_ANY;
909            devcb_call_write_line(&v->irq_func, ASSERT_LINE);
957            m_ifr |= INT_ANY;
958            devcb_call_write_line(&m_irq_func, ASSERT_LINE);
910959         }
911960      }
912961      break;
913962
914963   case VIA_IFR:
915964      if (data & INT_ANY)
965        {
916966         data = 0x7f;
917      via_clear_int (device, data);
967        }
968      clear_int(data);
918969      break;
919970    }
920971}
921972
922973
923974/*-------------------------------------------------
924    via_porta_w - interface setting VIA port
925    A input
975    ca1_w - interface setting VIA port CA1 input
926976-------------------------------------------------*/
927977
928WRITE8_DEVICE_HANDLER(via_porta_w)
978void via6522_device::ca1_w(UINT8 state)
929979{
930   via6522_t *v = get_token(device);
931
932   /* set the input, what could be easier? */
933   v->in_a = data;
934}
935
936
937/*-------------------------------------------------
938    via_ca1_r - interface retrieving VIA port
939    CA1 input
940-------------------------------------------------*/
941
942READ_LINE_DEVICE_HANDLER(via_ca1_r)
943{
944   via6522_t *v = get_token(device);
945   return v->in_ca1;
946}
947
948
949/*-------------------------------------------------
950    via_ca1_w - interface setting VIA port
951    CA1 input
952-------------------------------------------------*/
953
954WRITE_LINE_DEVICE_HANDLER(via_ca1_w)
955{
956   via6522_t *v = get_token(device);
957
958980   /* handle the active transition */
959   if (state != v->in_ca1)
981   if (state != m_in_ca1)
960982    {
961983      if (TRACE_VIA)
962         logerror("%s:6522VIA chip %s: CA1 = %02X\n", cpuexec_describe_context(device->machine), device->tag(), state);
984         logerror("%s:6522VIA chip %s: CA1 = %02X\n", cpuexec_describe_context(&m_machine), tag(), state);
963985
964      if ((CA1_LOW_TO_HIGH(v->pcr) && state) || (CA1_HIGH_TO_LOW(v->pcr) && !state))
986      if ((CA1_LOW_TO_HIGH(m_pcr) && state) || (CA1_HIGH_TO_LOW(m_pcr) && !state))
965987      {
966         if (PA_LATCH_ENABLE(v->acr))
988         if (PA_LATCH_ENABLE(m_acr))
967989         {
968            if (v->in_a_func.read != NULL)
969               v->in_a = devcb_call_read8(&v->in_a_func, 0);
990            if (m_in_a_func.read != NULL)
991                {
992               m_in_a = devcb_call_read8(&m_in_a_func, 0);
993                }
970994            else
971               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
995                {
996                    logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
997                }
972998         }
973999
974         via_set_int (device, INT_CA1);
1000         set_int(INT_CA1);
9751001
9761002         /* CA2 is configured as output and in pulse or handshake mode,
9771003               CA2 is cleared now */
978         if (CA2_AUTO_HS(v->pcr))
1004         if (CA2_AUTO_HS(m_pcr))
9791005         {
980            if (!v->out_ca2)
1006            if (!m_out_ca2)
9811007            {
9821008               /* clear CA2 */
983               v->out_ca2 = 1;
1009               m_out_ca2 = 1;
9841010
9851011               /* call the CA2 output function */
986               devcb_call_write_line(&v->out_ca2_func, 1);
1012               devcb_call_write_line(&m_out_ca2_func, 1);
9871013            }
9881014         }
9891015      }
9901016
991      v->in_ca1 = state;
1017      m_in_ca1 = state;
9921018    }
9931019}
9941020
9951021
9961022/*-------------------------------------------------
997    via_ca2_r - interface retrieving VIA port
998    CA2 input
1023    ca2_w - interface setting VIA port CA2 input
9991024-------------------------------------------------*/
10001025
1001READ_LINE_DEVICE_HANDLER(via_ca2_r)
1026void via6522_device::ca2_w(UINT8 state)
10021027{
1003   via6522_t *v = get_token(device);
1004   return v->in_ca2;
1005}
1006
1007
1008/*-------------------------------------------------
1009    via_ca2_w - interface setting VIA port
1010    CA2 input
1011-------------------------------------------------*/
1012
1013WRITE_LINE_DEVICE_HANDLER(via_ca2_w)
1014{
1015   via6522_t *v = get_token(device);
1016
10171028   /* CA2 is in input mode */
1018   if (CA2_INPUT(v->pcr))
1029   if (CA2_INPUT(m_pcr))
10191030    {
10201031      /* the new state has caused a transition */
1021      if (v->in_ca2 != state)
1032      if (m_in_ca2 != state)
10221033      {
10231034         /* handle the active transition */
1024         if ((state && CA2_LOW_TO_HIGH(v->pcr)) || (!state && CA2_HIGH_TO_LOW(v->pcr)))
1035         if ((state && CA2_LOW_TO_HIGH(m_pcr)) || (!state && CA2_HIGH_TO_LOW(m_pcr)))
10251036         {
10261037            /* mark the IRQ */
1027            via_set_int (device, INT_CA2);
1038            set_int(INT_CA2);
10281039         }
10291040         /* set the new value for CA2 */
1030         v->in_ca2 = state;
1041         m_in_ca2 = state;
10311042      }
10321043    }
1033
1034
10351044}
10361045
10371046
10381047/*-------------------------------------------------
1039    via_portb_r - interface retrieving VIA port
1040    B input
1048    cb1_w - interface setting VIA port CB1 input
10411049-------------------------------------------------*/
10421050
1043READ8_DEVICE_HANDLER(via_portb_r)
1051void via6522_device::cb1_w(UINT8 state)
10441052{
1045   via6522_t *v = get_token(device);
1046   return v->in_b;
1047}
1048
1049
1050/*-------------------------------------------------
1051    via_portb_w - interface setting VIA port
1052    B input
1053-------------------------------------------------*/
1054
1055WRITE8_DEVICE_HANDLER(via_portb_w)
1056{
1057   via6522_t *v = get_token(device);
1058
1059   /* set the input, what could be easier? */
1060   v->in_b = data;
1061}
1062
1063
1064/*-------------------------------------------------
1065    via_cb1_r - interface retrieving VIA port
1066    CB1 input
1067-------------------------------------------------*/
1068
1069READ_LINE_DEVICE_HANDLER(via_cb1_r)
1070{
1071   via6522_t *v = get_token(device);
1072   return v->in_cb1;
1073}
1074
1075
1076/*-------------------------------------------------
1077    via_cb1_w - interface setting VIA port
1078    CB1 input
1079-------------------------------------------------*/
1080
1081WRITE_LINE_DEVICE_HANDLER(via_cb1_w)
1082{
1083   via6522_t *v = get_token(device);
1084
10851053   /* handle the active transition */
1086   if (state != v->in_cb1)
1054   if (state != m_in_cb1)
10871055    {
1088      if ((CB1_LOW_TO_HIGH(v->pcr) && state) || (CB1_HIGH_TO_LOW(v->pcr) && !state))
1056      if ((CB1_LOW_TO_HIGH(m_pcr) && state) || (CB1_HIGH_TO_LOW(m_pcr) && !state))
10891057      {
1090         if (PB_LATCH_ENABLE(v->acr))
1058         if (PB_LATCH_ENABLE(m_acr))
10911059         {
1092            if (v->in_b_func.read != NULL)
1093               v->in_b = devcb_call_read8(&v->in_b_func, 0);
1060            if (m_in_b_func.read != NULL)
1061                {
1062               m_in_b = devcb_call_read8(&m_in_b_func, 0);
1063                }
10941064            else
1095               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
1065                {
1066                    logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
1067                }
10961068         }
1097         if (SO_EXT_CONTROL(v->acr) || SI_EXT_CONTROL(v->acr))
1098            via_shift (device);
1069         if (SO_EXT_CONTROL(m_acr) || SI_EXT_CONTROL(m_acr))
1070            {
1071            shift();
1072            }
10991073
1100         via_set_int (device, INT_CB1);
1074         set_int(INT_CB1);
11011075
11021076         /* CB2 is configured as output and in pulse or handshake mode,
11031077               CB2 is cleared now */
1104         if (CB2_AUTO_HS(v->pcr))
1078         if (CB2_AUTO_HS(m_pcr))
11051079         {
1106            if (!v->out_cb2)
1080            if (!m_out_cb2)
11071081            {
11081082               /* clear CB2 */
1109               v->out_cb2 = 1;
1083               m_out_cb2 = 1;
11101084
11111085               /* call the CB2 output function */
1112               devcb_call_write_line(&v->out_cb2_func, 1);
1086               devcb_call_write_line(&m_out_cb2_func, 1);
11131087            }
11141088         }
11151089      }
1116      v->in_cb1 = state;
1090      m_in_cb1 = state;
11171091    }
11181092}
11191093
11201094
11211095/*-------------------------------------------------
1122    via_cb2_r - interface retrieving VIA port
1123    CB2 input
1096    cb2_w - interface setting VIA port CB2 input
11241097-------------------------------------------------*/
11251098
1126READ_LINE_DEVICE_HANDLER(via_cb2_r)
1099void via6522_device::cb2_w(UINT8 state)
11271100{
1128   via6522_t *v = get_token(device);
1129   return v->in_cb2;
1130}
1131
1132
1133/*-------------------------------------------------
1134    via_cb2_w - interface setting VIA port
1135    CB2 input
1136-------------------------------------------------*/
1137
1138WRITE_LINE_DEVICE_HANDLER(via_cb2_w)
1139{
1140   via6522_t *v = get_token(device);
1141
11421101   /* CB2 is in input mode */
1143   if (CB2_INPUT(v->pcr))
1102   if (CB2_INPUT(m_pcr))
11441103    {
11451104      /* the new state has caused a transition */
1146      if (v->in_cb2 != state)
1105      if (m_in_cb2 != state)
11471106      {
11481107         /* handle the active transition */
1149         if ((state && CB2_LOW_TO_HIGH(v->pcr)) || (!state && CB2_HIGH_TO_LOW(v->pcr)))
1108         if ((state && CB2_LOW_TO_HIGH(m_pcr)) || (!state && CB2_HIGH_TO_LOW(m_pcr)))
11501109         {
11511110            /* mark the IRQ */
1152            via_set_int (device, INT_CB2);
1111            set_int(INT_CB2);
11531112         }
11541113         /* set the new value for CB2 */
1155         v->in_cb2 = state;
1114         m_in_cb2 = state;
11561115      }
11571116    }
11581117}
11591118
11601119
1161/*-------------------------------------------------
1162    DEVICE_GET_INFO( via6522 )
1163-------------------------------------------------*/
11641120
1165DEVICE_GET_INFO(via6522)
1166{
1167   switch (state)
1168   {
1169      /* --- the following bits of info are returned as 64-bit signed integers --- */
1170      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(via6522_t);            break;
1171      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
1121/***************************************************************************
1122    TRAMPOLINES
1123***************************************************************************/
11721124
1173      /* --- the following bits of info are returned as pointers to data or functions --- */
1174      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(via6522);   break;
1175      case DEVINFO_FCT_STOP:                     /* Nothing */                        break;
1176      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(via6522);   break;
1125READ8_DEVICE_HANDLER(via_r) { return downcast<via6522_device *>(device)->reg_r(offset); }
1126WRITE8_DEVICE_HANDLER(via_w) { downcast<via6522_device *>(device)->reg_w(offset, data); }
11771127
1178      /* --- the following bits of info are returned as NULL-terminated strings --- */
1179      case DEVINFO_STR_NAME:                     strcpy(info->s, "6522 VIA");            break;
1180      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "6522 VIA");            break;
1181      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                  break;
1182      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
1183      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
1184   }
1185}
1128READ_LINE_DEVICE_HANDLER(via_ca1_r) { return downcast<via6522_device *>(device)->ca1_r(); }
1129WRITE_LINE_DEVICE_HANDLER(via_ca1_w) { downcast<via6522_device *>(device)->ca1_w(state); }
11861130
1131READ_LINE_DEVICE_HANDLER(via_ca2_r) { return downcast<via6522_device *>(device)->ca2_r(); }
1132WRITE_LINE_DEVICE_HANDLER(via_ca2_w) { downcast<via6522_device *>(device)->ca2_w(state); }
11871133
1188DEFINE_LEGACY_DEVICE(VIA6522, via6522);
1134READ_LINE_DEVICE_HANDLER(via_cb1_r) { return downcast<via6522_device *>(device)->cb1_r(); }
1135WRITE_LINE_DEVICE_HANDLER(via_cb1_w) { downcast<via6522_device *>(device)->cb1_w(state); }
1136
1137READ_LINE_DEVICE_HANDLER(via_cb2_r) { return downcast<via6522_device *>(device)->cb2_r(); }
1138WRITE_LINE_DEVICE_HANDLER(via_cb2_w) { downcast<via6522_device *>(device)->cb2_w(state); }
1139
1140WRITE8_DEVICE_HANDLER(via_porta_w) { downcast<via6522_device *>(device)->porta_w(data); }
1141
1142READ8_DEVICE_HANDLER(via_portb_r) { return downcast<via6522_device *>(device)->portb_r(); }
1143WRITE8_DEVICE_HANDLER(via_portb_w) { downcast<via6522_device *>(device)->portb_w(data); }
src/emu/machine/6821pia.h
r8892r8893
1818
1919**********************************************************************/
2020
21#pragma once
22
2123#ifndef __6821PIA_H__
2224#define __6821PIA_H__
2325
24#include "devlegcy.h"
26#include "emu.h"
2527
2628
29
2730/***************************************************************************
31    DEVICE CONFIGURATION MACROS
32***************************************************************************/
33
34#define MDRV_PIA6821_ADD(_tag, _intrf) \
35    MDRV_DEVICE_ADD(_tag, PIA6821, 0) \
36    MDRV_DEVICE_CONFIG(_intrf)
37
38#define MDRV_PIA6821_MODIFY(_tag, _intrf) \
39    MDRV_DEVICE_MODIFY(_tag) \
40    MDRV_DEVICE_CONFIG(_intrf)
41
42#define MDRV_PIA6822_ADD(_tag, _intrf) \
43    MDRV_DEVICE_ADD(_tag, PIA6822, 0) \
44    MDRV_DEVICE_CONFIG(_intrf)
45
46#define MDRV_PIA6822_MODIFY(_tag, _intrf) \
47    MDRV_DEVICE_MODIFY(_tag) \
48    MDRV_DEVICE_CONFIG(_intrf)
49
50
51
52/***************************************************************************
2853    TYPE DEFINITIONS
2954***************************************************************************/
3055
31/*------------- PIA interface structure -----------------*/
3256
33typedef struct _pia6821_interface pia6821_interface;
34struct _pia6821_interface
57// ======================> pia6821_interface
58
59struct pia6821_interface
3560{
36   devcb_read8 in_a_func;
37   devcb_read8 in_b_func;
38   devcb_read_line in_ca1_func;
39   devcb_read_line in_cb1_func;
40   devcb_read_line in_ca2_func;
41   devcb_read_line in_cb2_func;
42   devcb_write8 out_a_func;
43   devcb_write8 out_b_func;
44   devcb_write_line out_ca2_func;
45   devcb_write_line out_cb2_func;
46   devcb_write_line irq_a_func;
47   devcb_write_line irq_b_func;
61    devcb_read8 m_in_a_func;
62    devcb_read8 m_in_b_func;
63    devcb_read_line m_in_ca1_func;
64    devcb_read_line m_in_cb1_func;
65    devcb_read_line m_in_ca2_func;
66    devcb_read_line m_in_cb2_func;
67    devcb_write8 m_out_a_func;
68    devcb_write8 m_out_b_func;
69    devcb_write_line m_out_ca2_func;
70    devcb_write_line m_out_cb2_func;
71    devcb_write_line m_irq_a_func;
72    devcb_write_line m_irq_b_func;
4873};
4974
50DECLARE_LEGACY_DEVICE(PIA6821, pia6821);
51DECLARE_LEGACY_DEVICE(PIA6822, pia6822);
5275
5376
77// ======================> pia6821_device_config
5478
79class pia6821_device_config : public device_config,
80                              public pia6821_interface
81{
82    friend class pia6821_device;
83
84    // construction/destruction
85    pia6821_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
86
87public:
88    // allocators
89    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
90    virtual device_t *alloc_device(running_machine &machine) const;
91
92protected:
93    // device_config overrides
94    virtual void device_config_complete();
95};
96
97
98
99// ======================> pia6821_device
100
101class pia6821_device :  public device_t
102{
103    friend class pia6821_device_config;
104
105    // construction/destruction
106    pia6821_device(running_machine &_machine, const pia6821_device_config &_config);
107
108public:
109
110    UINT8 reg_r(UINT8 offset);
111    void reg_w(UINT8 offset, UINT8 data);
112
113    UINT8 alt_r(UINT8 offset);
114    void alt_w(UINT8 offset, UINT8 data);
115
116    UINT8 get_port_b_z_mask();          /* see first note in .c */
117    void set_port_a_z_mask(UINT8 data); /* see second note in .c */
118
119    UINT8 porta_r();
120    void porta_w(UINT8 data);
121    void set_input_a(UINT8 data, UINT8 z_mask);
122    UINT8 get_output_a();
123
124    UINT8 ca1_r();
125    void ca1_w(UINT8 state);
126
127    UINT8 ca2_r();
128    void ca2_w(UINT8 state);
129    int get_output_ca2();
130    int get_output_ca2_z();
131
132    UINT8 portb_r();
133    void portb_w(UINT8 data);
134    UINT8 get_output_b();
135
136    UINT8 cb1_r();
137    void cb1_w(UINT8 state);
138
139    UINT8 cb2_r();
140    void cb2_w(UINT8 state);
141    int get_output_cb2();
142    int get_output_cb2_z();
143
144    int get_irq_a();
145    int get_irq_b();
146
147protected:
148    // device-level overrides
149    virtual void device_start();
150    virtual void device_reset();
151    virtual void device_post_load() { }
152    virtual void device_clock_changed() { }
153
154private:
155
156    void update_interrupts();
157
158    UINT8 get_in_a_value();
159    UINT8 get_in_b_value();
160
161    UINT8 get_out_a_value();
162    UINT8 get_out_b_value();
163
164    void set_out_ca2(int data);
165    void set_out_cb2(int data);
166
167    UINT8 port_a_r();
168    UINT8 ddr_a_r();
169    UINT8 control_a_r();
170
171    UINT8 port_b_r();
172    UINT8 ddr_b_r();
173    UINT8 control_b_r();
174
175    void send_to_out_a_func(const char* message);
176    void send_to_out_b_func(const char* message);
177
178    void port_a_w(UINT8 data);
179    void ddr_a_w(UINT8 data);
180
181    void port_b_w(UINT8 data);
182    void ddr_b_w(UINT8 data);
183
184    void control_a_w(UINT8 data);
185    void control_b_w(UINT8 data);
186
187    devcb_resolved_read8 m_in_a_func;
188    devcb_resolved_read8 m_in_b_func;
189    devcb_resolved_read_line m_in_ca1_func;
190    devcb_resolved_read_line m_in_cb1_func;
191    devcb_resolved_read_line m_in_ca2_func;
192    devcb_resolved_read_line m_in_cb2_func;
193    devcb_resolved_write8 m_out_a_func;
194    devcb_resolved_write8 m_out_b_func;
195    devcb_resolved_write_line m_out_ca2_func;
196    devcb_resolved_write_line m_out_cb2_func;
197    devcb_resolved_write_line m_irq_a_func;
198    devcb_resolved_write_line m_irq_b_func;
199
200    UINT8 m_in_a;
201    UINT8 m_in_ca1;
202    UINT8 m_in_ca2;
203    UINT8 m_out_a;
204    UINT8 m_out_ca2;
205    UINT8 m_port_a_z_mask;
206    UINT8 m_ddr_a;
207    UINT8 m_ctl_a;
208    UINT8 m_irq_a1;
209    UINT8 m_irq_a2;
210    UINT8 m_irq_a_state;
211
212    UINT8 m_in_b;
213    UINT8 m_in_cb1;
214    UINT8 m_in_cb2;
215    UINT8 m_out_b;
216    UINT8 m_out_cb2;
217    UINT8 m_last_out_cb2_z;
218    UINT8 m_ddr_b;
219    UINT8 m_ctl_b;
220    UINT8 m_irq_b1;
221    UINT8 m_irq_b2;
222    UINT8 m_irq_b_state;
223
224    /* variables that indicate if access a line externally -
225       used to for logging purposes ONLY */
226    UINT8 m_in_a_pushed;
227    UINT8 m_out_a_needs_pulled;
228    UINT8 m_in_ca1_pushed;
229    UINT8 m_in_ca2_pushed;
230    UINT8 m_out_ca2_needs_pulled;
231    UINT8 m_in_b_pushed;
232    UINT8 m_out_b_needs_pulled;
233    UINT8 m_in_cb1_pushed;
234    UINT8 m_in_cb2_pushed;
235    UINT8 m_out_cb2_needs_pulled;
236    UINT8 m_logged_port_a_not_connected;
237    UINT8 m_logged_port_b_not_connected;
238    UINT8 m_logged_ca1_not_connected;
239    UINT8 m_logged_ca2_not_connected;
240    UINT8 m_logged_cb1_not_connected;
241    UINT8 m_logged_cb2_not_connected;
242
243    const pia6821_device_config &m_config;
244};
245
246
247// device type definition
248extern const device_type PIA6821;
249
250
251
55252/***************************************************************************
56253    PROTOTYPES
57254***************************************************************************/
r8892r8893
94291int pia6821_get_irq_b(running_device *device);
95292
96293
97/***************************************************************************
98    DEVICE CONFIGURATION MACROS
99***************************************************************************/
100
101#define MDRV_PIA6821_ADD(_tag, _intrf) \
102   MDRV_DEVICE_ADD(_tag, PIA6821, 0) \
103   MDRV_DEVICE_CONFIG(_intrf)
104
105#define MDRV_PIA6821_MODIFY(_tag, _intrf) \
106   MDRV_DEVICE_MODIFY(_tag) \
107   MDRV_DEVICE_CONFIG(_intrf)
108
109#define MDRV_PIA6822_ADD(_tag, _intrf) \
110   MDRV_DEVICE_ADD(_tag, PIA6822, 0) \
111   MDRV_DEVICE_CONFIG(_intrf)
112
113#define MDRV_PIA6822_MODIFY(_tag, _intrf) \
114   MDRV_DEVICE_MODIFY(_tag) \
115   MDRV_DEVICE_CONFIG(_intrf)
116
117#endif /* __6821NEW_H__ */
294#endif /* __6821PIA_H__ */
src/emu/machine/6532riot.h
r8892r8893
44
55***************************************************************************/
66
7#pragma once
8
79#ifndef __RIOT6532_H__
810#define __RIOT6532_H__
911
10#include "devlegcy.h"
12#include "emu.h"
1113
1214
15
16//**************************************************************************
17//  INTERFACE CONFIGURATION MACROS
18//**************************************************************************
19
20#define MDRV_RIOT6532_ADD(_tag, _clock, _intrf) \
21    MDRV_DEVICE_ADD(_tag, RIOT6532, _clock) \
22    MDRV_DEVICE_CONFIG(_intrf)
23
24
25
1326/***************************************************************************
1427    TYPE DEFINITIONS
1528***************************************************************************/
1629
1730
18typedef struct _riot6532_interface riot6532_interface;
19struct _riot6532_interface
31// ======================> riot6532_interface
32
33struct riot6532_interface
2034{
21   devcb_read8         in_a_func;
22   devcb_read8         in_b_func;
23   devcb_write8      out_a_func;
24   devcb_write8      out_b_func;
25   devcb_write_line   irq_func;
35    devcb_read8         m_in_a_func;
36    devcb_read8         m_in_b_func;
37    devcb_write8        m_out_a_func;
38    devcb_write8        m_out_b_func;
39    devcb_write_line    m_irq_func;
2640};
2741
2842
2943
30/***************************************************************************
31    DEVICE CONFIGURATION MACROS
32***************************************************************************/
44// ======================> riot6532_device_config
3345
34#define MDRV_RIOT6532_ADD(_tag, _clock, _intrf) \
35   MDRV_DEVICE_ADD(_tag, RIOT6532, _clock) \
36   MDRV_DEVICE_CONFIG(_intrf)
46class riot6532_device_config : public device_config,
47                               public riot6532_interface
48{
49    friend class riot6532_device;
3750
51    // construction/destruction
52    riot6532_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
3853
54public:
55    // allocators
56    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
57    virtual device_t *alloc_device(running_machine &machine) const;
3958
59protected:
60    // device_config overrides
61    virtual void device_config_complete();
62};
63
64
65
66// ======================> riot6532_device
67
68class riot6532_device :  public device_t
69{
70    friend class riot6532_device_config;
71
72    // construction/destruction
73    riot6532_device(running_machine &_machine, const riot6532_device_config &_config);
74
75public:
76    UINT8 reg_r(UINT8 offset);
77    void reg_w(UINT8 offset, UINT8 data);
78
79    void porta_in_set(UINT8 data, UINT8 mask);
80    void portb_in_set(UINT8 data, UINT8 mask);
81
82    UINT8 porta_in_get();
83    UINT8 portb_in_get();
84
85    UINT8 porta_out_get();
86    UINT8 portb_out_get();
87
88    void timer_end();
89
90protected:
91    class riot6532_port
92    {
93    public:
94        UINT8                   m_in;
95        UINT8                   m_out;
96        UINT8                   m_ddr;
97        devcb_resolved_read8    m_in_func;
98        devcb_resolved_write8   m_out_func;
99    };
100
101    // device-level overrides
102    virtual void device_start();
103    virtual void device_reset();
104    virtual void device_post_load() { }
105    virtual void device_clock_changed() { }
106
107    static TIMER_CALLBACK( timer_end_callback );
108
109private:
110    void update_irqstate();
111    UINT8 apply_ddr(const riot6532_port *port);
112    void update_pa7_state();
113    UINT8 get_timer();
114
115    int             m_index;
116
117    riot6532_port   m_port[2];
118
119    devcb_resolved_write_line   m_irq_func;
120
121    UINT8           m_irqstate;
122    UINT8           m_irqenable;
123
124    UINT8           m_pa7dir;     /* 0x80 = high-to-low, 0x00 = low-to-high */
125    UINT8           m_pa7prev;
126
127    UINT8           m_timershift;
128    UINT8           m_timerstate;
129    emu_timer *     m_timer;
130
131    const riot6532_device_config &m_config;
132};
133
134
135// device type definition
136extern const device_type RIOT6532;
137
138
139
40140/***************************************************************************
41    FUNCTION PROTOTYPES
141    PROTOTYPES
42142***************************************************************************/
43143
44144READ8_DEVICE_HANDLER( riot6532_r );
r8892r8893
53153UINT8 riot6532_porta_out_get(running_device *device);
54154UINT8 riot6532_portb_out_get(running_device *device);
55155
56
57/* ----- device interface ----- */
58
59DECLARE_LEGACY_DEVICE(RIOT6532, riot6532);
60
61156#endif
src/emu/machine/6522via.h
r8892r8893
55    This function emulates all the functionality of 6522
66    versatile interface adapters.
77
8    This is based on the M6821 emulation in MAME.
8    This is based on the pre-existing 6821 emulation.
99
1010    Written by Mathis Rosenhauer
1111
1212**********************************************************************/
1313
14#pragma once
15
1416#ifndef __6522VIA_H__
1517#define __6522VIA_H__
1618
17#include "devlegcy.h"
19#include "emu.h"
1820
1921
22
23//**************************************************************************
24//  INTERFACE CONFIGURATION MACROS
25//**************************************************************************
26
27#define MDRV_VIA6522_ADD(_tag, _clock, _intrf) \
28    MDRV_DEVICE_ADD(_tag, VIA6522, _clock) \
29    MDRV_DEVICE_CONFIG(_intrf)
30
31
32
2033/***************************************************************************
2134    MACROS / CONSTANTS
2235***************************************************************************/
2336
24DECLARE_LEGACY_DEVICE(VIA6522, via6522);
25
26#define MDRV_VIA6522_ADD(_tag, _clock, _intrf) \
27   MDRV_DEVICE_ADD(_tag, VIA6522, _clock) \
28   MDRV_DEVICE_CONFIG(_intrf)
29
3037#define   VIA_PB       0
3138#define   VIA_PA       1
3239#define   VIA_DDRB    2
r8892r8893
4956    TYPE DEFINITIONS
5057***************************************************************************/
5158
52typedef struct _via6522_interface via6522_interface;
53struct _via6522_interface
59
60// ======================> via6522_interface
61
62struct via6522_interface
5463{
55   devcb_read8 in_a_func;
56   devcb_read8 in_b_func;
57   devcb_read_line in_ca1_func;
58   devcb_read_line in_cb1_func;
59   devcb_read_line in_ca2_func;
60   devcb_read_line in_cb2_func;
61   devcb_write8 out_a_func;
62   devcb_write8 out_b_func;
63   devcb_write_line out_ca1_func;
64   devcb_write_line out_cb1_func;
65   devcb_write_line out_ca2_func;
66   devcb_write_line out_cb2_func;
67   devcb_write_line irq_func;
64    devcb_read8 m_in_a_func;
65    devcb_read8 m_in_b_func;
66    devcb_read_line m_in_ca1_func;
67    devcb_read_line m_in_cb1_func;
68    devcb_read_line m_in_ca2_func;
69    devcb_read_line m_in_cb2_func;
70    devcb_write8 m_out_a_func;
71    devcb_write8 m_out_b_func;
72    devcb_write_line m_out_ca1_func;
73    devcb_write_line m_out_cb1_func;
74    devcb_write_line m_out_ca2_func;
75    devcb_write_line m_out_cb2_func;
76    devcb_write_line m_irq_func;
6877};
6978
7079
80
81// ======================> via6522_device_config
82
83class via6522_device_config :   public device_config,
84                                public via6522_interface
85{
86    friend class via6522_device;
87
88    // construction/destruction
89    via6522_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
90
91public:
92    // allocators
93    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
94    virtual device_t *alloc_device(running_machine &machine) const;
95
96protected:
97    // device_config overrides
98    virtual void device_config_complete();
99};
100
101
102
103// ======================> via6522_device
104
105class via6522_device :  public device_t
106{
107    friend class via6522_device_config;
108    friend class dart_channel;
109
110    // construction/destruction
111    via6522_device(running_machine &_machine, const via6522_device_config &_config);
112
113public:
114    UINT8 reg_r(UINT8 offset);
115    void reg_w(UINT8 offset, UINT8 data);
116
117    void porta_w(UINT8 data) { m_in_a = data; }
118
119    UINT8 portb_r() { return m_in_b; }
120    void portb_w(UINT8 data) { m_in_b = data; }
121
122    UINT8 ca1_r() { return m_in_ca1; }
123    void ca1_w(UINT8 data);
124
125    UINT8 ca2_r() { return m_in_ca2; }
126    void ca2_w(UINT8 data);
127
128    UINT8 cb1_r() { return m_in_cb1; }
129    void cb1_w(UINT8 data);
130
131    UINT8 cb2_r() { return m_in_cb2; }
132    void cb2_w(UINT8 data);
133
134protected:
135    // device-level overrides
136    virtual void device_start();
137    virtual void device_reset();
138    virtual void device_post_load() { }
139    virtual void device_clock_changed() { }
140
141    static TIMER_CALLBACK( t1_timeout_callback );
142    static TIMER_CALLBACK( t2_timeout_callback );
143    static TIMER_CALLBACK( shift_callback );
144
145private:
146    attotime cycles_to_time(int c);
147    UINT32 time_to_cycles(attotime t);
148    UINT16 get_counter1_value();
149
150    void set_int(int data);
151    void clear_int(int data);
152    void shift();
153    void t1_timeout();
154    void t2_timeout();
155
156    devcb_resolved_read8 m_in_a_func;
157    devcb_resolved_read8 m_in_b_func;
158    devcb_resolved_read_line m_in_ca1_func;
159    devcb_resolved_read_line m_in_cb1_func;
160    devcb_resolved_read_line m_in_ca2_func;
161    devcb_resolved_read_line m_in_cb2_func;
162    devcb_resolved_write8 m_out_a_func;
163    devcb_resolved_write8 m_out_b_func;
164    devcb_resolved_write_line m_out_ca1_func;
165    devcb_resolved_write_line m_out_cb1_func;
166    devcb_resolved_write_line m_out_ca2_func;
167    devcb_resolved_write_line m_out_cb2_func;
168    devcb_resolved_write_line m_irq_func;
169
170    UINT8 m_in_a;
171    UINT8 m_in_ca1;
172    UINT8 m_in_ca2;
173    UINT8 m_out_a;
174    UINT8 m_out_ca2;
175    UINT8 m_ddr_a;
176
177    UINT8 m_in_b;
178    UINT8 m_in_cb1;
179    UINT8 m_in_cb2;
180    UINT8 m_out_b;
181    UINT8 m_out_cb2;
182    UINT8 m_ddr_b;
183
184    UINT8 m_t1cl;
185    UINT8 m_t1ch;
186    UINT8 m_t1ll;
187    UINT8 m_t1lh;
188    UINT8 m_t2cl;
189    UINT8 m_t2ch;
190    UINT8 m_t2ll;
191    UINT8 m_t2lh;
192
193    UINT8 m_sr;
194    UINT8 m_pcr;
195    UINT8 m_acr;
196    UINT8 m_ier;
197    UINT8 m_ifr;
198
199    emu_timer *m_t1;
200    attotime m_time1;
201    UINT8 m_t1_active;
202    emu_timer *m_t2;
203    attotime m_time2;
204    UINT8 m_t2_active;
205
206    emu_timer *m_shift_timer;
207    UINT8 m_shift_counter;
208
209    const via6522_device_config &m_config;
210};
211
212
213// device type definition
214extern const device_type VIA6522;
215
216
217
71218/***************************************************************************
72219    PROTOTYPES
73220***************************************************************************/
src/emu/machine/7474.c
r8892r8893
4040#include "emu.h"
4141#include "7474.h"
4242
43//**************************************************************************
44//  DEVICE CONFIGURATION
45//**************************************************************************
4346
44typedef struct _ttl7474_state ttl7474_state;
45struct _ttl7474_state
47//-------------------------------------------------
48//  ttl7474_device_config - constructor
49//-------------------------------------------------
50
51ttl7474_device_config::ttl7474_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
52    : device_config(mconfig, static_alloc_device_config, "TTL7474", tag, owner, clock)
4653{
47   /* callbacks */
48   devcb_resolved_write_line output_cb;
49   devcb_resolved_write_line comp_output_cb;
54}
5055
51   /* inputs */
52   UINT8 clear;         /* pin 1/13 */
53   UINT8 preset;         /* pin 4/10 */
54   UINT8 clock;         /* pin 3/11 */
55   UINT8 d;            /* pin 2/12 */
5656
57   /* outputs */
58   UINT8 output;         /* pin 5/9 */
59   UINT8 output_comp;   /* pin 6/8 */
57//-------------------------------------------------
58//  static_alloc_device_config - allocate a new
59//  configuration object
60//-------------------------------------------------
6061
61   /* internal */
62   UINT8 last_clock;
63   UINT8 last_output;
64   UINT8 last_output_comp;
62device_config *ttl7474_device_config::static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock)
63{
64    return global_alloc(ttl7474_device_config(mconfig, tag, owner, clock));
65}
6566
66   running_device *device;
67};
6867
69INLINE ttl7474_state *get_safe_token(running_device *device)
68//-------------------------------------------------
69//  alloc_device - allocate a new device object
70//-------------------------------------------------
71
72device_t *ttl7474_device_config::alloc_device(running_machine &machine) const
7073{
71   assert(device != NULL);
72   assert(device->type() == TTL7474);
74    return auto_alloc(&machine, ttl7474_device(machine, *this));
75}
7376
74   return (ttl7474_state *)downcast<legacy_device_base *>(device)->token();
77
78
79//-------------------------------------------------
80//  device_config_complete - perform any
81//  operations now that the configuration is
82//  complete
83//-------------------------------------------------
84
85void ttl7474_device_config::device_config_complete()
86{
87   m_target_tag = reinterpret_cast<const char *>(m_inline_data[INLINE_TARGET_TAG]);
88   m_base_output_cb = reinterpret_cast<void (*)(device_t *device, INT32)>(m_inline_data[INLINE_OUTPUT_CB]);
89   m_base_comp_output_cb = reinterpret_cast<void (*)(device_t *device, INT32)>(m_inline_data[INLINE_COMP_OUTPUT_CB]);
90
91   m_output_cb.type = DEVCB_TYPE_DEVICE;
92   m_output_cb.tag = m_target_tag;
93   m_output_cb.writeline = m_base_output_cb;
94
95   m_comp_output_cb.type = DEVCB_TYPE_DEVICE;
96   m_comp_output_cb.tag = m_target_tag;
97   m_comp_output_cb.writeline = m_base_comp_output_cb;
7598}
7699
77100
78static void ttl7474_update(ttl7474_state *state)
101
102//**************************************************************************
103//  LIVE DEVICE
104//**************************************************************************
105
106//-------------------------------------------------
107//  ttl7474_device - constructor
108//-------------------------------------------------
109
110ttl7474_device::ttl7474_device(running_machine &_machine, const ttl7474_device_config &config)
111    : device_t(_machine, config),
112      m_config(config)
79113{
80   if (!state->preset && state->clear)           /* line 1 in truth table */
114    init();
115}
116
117//-------------------------------------------------
118//  device_start - device-specific startup
119//-------------------------------------------------
120
121void ttl7474_device::device_start()
122{
123    state_save_register_device_item(this, 0, m_clear);
124    state_save_register_device_item(this, 0, m_preset);
125    state_save_register_device_item(this, 0, m_clk);
126    state_save_register_device_item(this, 0, m_d);
127    state_save_register_device_item(this, 0, m_output);
128    state_save_register_device_item(this, 0, m_output_comp);
129    state_save_register_device_item(this, 0, m_last_clock);
130    state_save_register_device_item(this, 0, m_last_output);
131    state_save_register_device_item(this, 0, m_last_output_comp);
132
133    devcb_resolve_write_line(&m_output_cb, &m_config.m_output_cb, this);
134    devcb_resolve_write_line(&m_comp_output_cb, &m_config.m_comp_output_cb, this);
135}
136
137//-------------------------------------------------
138//  device_reset - device-specific reset
139//-------------------------------------------------
140
141void ttl7474_device::device_reset()
142{
143    init();
144}
145
146
147//-------------------------------------------------
148//  update - update internal state
149//-------------------------------------------------
150
151void ttl7474_device::update()
152{
153    if (!m_preset && m_clear)             /* line 1 in truth table */
81154   {
82      state->output    = 1;
83      state->output_comp = 0;
155        m_output    = 1;
156        m_output_comp = 0;
84157   }
85   else if (state->preset && !state->clear)     /* line 2 in truth table */
158    else if (m_preset && !m_clear)      /* line 2 in truth table */
86159   {
87      state->output    = 0;
88      state->output_comp = 1;
160        m_output    = 0;
161        m_output_comp = 1;
89162   }
90   else if (!state->preset && !state->clear)     /* line 3 in truth table */
163    else if (!m_preset && !m_clear)     /* line 3 in truth table */
91164   {
92      state->output    = 1;
93      state->output_comp = 1;
165        m_output    = 1;
166        m_output_comp = 1;
94167   }
95   else if (!state->last_clock && state->clock)  /* line 4 in truth table */
168    else if (!m_last_clock && m_clk)     /* line 4 in truth table */
96169   {
97      state->output    =  state->d;
98      state->output_comp = !state->d;
170        m_output    =  m_d;
171        m_output_comp = !m_d;
99172   }
100173
101   state->last_clock = state->clock;
174    m_last_clock = m_clk;
102175
103176
104177   /* call callback if any of the outputs changed */
105   if (state->output != state->last_output)
178    if (m_output != m_last_output)
106179   {
107      state->last_output = state->output;
108      if (state->output_cb.write != NULL)
109         devcb_call_write_line(&state->output_cb, state->output);
180        m_last_output = m_output;
181      devcb_call_write_line(&m_output_cb, m_output);
110182   }
111183   /* call callback if any of the outputs changed */
112   if (state->output_comp != state->last_output_comp)
184    if (m_output_comp != m_last_output_comp)
113185   {
114      state->last_output_comp = state->output_comp;
115      if (state->comp_output_cb.write != NULL)
116         devcb_call_write_line(&state->comp_output_cb, state->output_comp);
186        m_last_output_comp = m_output_comp;
187      devcb_call_write_line(&m_comp_output_cb, m_output_comp);
117188   }
118189}
119190
120191
192//-------------------------------------------------
193//  clear_w - set the clear line state
194//-------------------------------------------------
195
121196WRITE_LINE_DEVICE_HANDLER( ttl7474_clear_w )
122197{
123   ttl7474_state *dev_state = get_safe_token(device);
124   dev_state->clear = state & 1;
125   ttl7474_update(dev_state);
198    downcast<ttl7474_device *>(device)->clear_w(state);
126199}
127200
128WRITE_LINE_DEVICE_HANDLER( ttl7474_preset_w )
201void ttl7474_device::clear_w(UINT8 state)
129202{
130   ttl7474_state *dev_state = get_safe_token(device);
131   dev_state->preset = state & 1;
132   ttl7474_update(dev_state);
203    m_clear = state & 1;
204   update();
133205}
134206
135WRITE_LINE_DEVICE_HANDLER( ttl7474_clock_w )
207
208//-------------------------------------------------
209//  clear_w - set the clear line state
210//-------------------------------------------------
211
212WRITE_LINE_DEVICE_HANDLER( ttl7474_preset_w )
136213{
137   ttl7474_state *dev_state = get_safe_token(device);
138   dev_state->clock = state & 1;
139   ttl7474_update(dev_state);
214    downcast<ttl7474_device *>(device)->preset_w(state);
140215}
141216
142WRITE_LINE_DEVICE_HANDLER( ttl7474_d_w )
217void ttl7474_device::preset_w(UINT8 state)
143218{
144   ttl7474_state *dev_state = get_safe_token(device);
145   dev_state->d = state & 1;
146   ttl7474_update(dev_state);
219    m_preset = state & 1;
220   update();
147221}
148222
149223
150READ_LINE_DEVICE_HANDLER( ttl7474_output_r )
224//-------------------------------------------------
225//  clock_w - set the clock line state
226//-------------------------------------------------
227
228WRITE_LINE_DEVICE_HANDLER( ttl7474_clock_w )
151229{
152   ttl7474_state *dev_state = get_safe_token(device);
153   return dev_state->output;
230    downcast<ttl7474_device *>(device)->clock_w(state);
154231}
155232
156READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r )
233void ttl7474_device::clock_w(UINT8 state)
157234{
158   ttl7474_state *dev_state = get_safe_token(device);
159   return dev_state->output_comp;
235    m_clk = state & 1;
236   update();
160237}
161238
162239
163static DEVICE_START( ttl7474 )
240//-------------------------------------------------
241//  d_w - set the d line state
242//-------------------------------------------------
243
244WRITE_LINE_DEVICE_HANDLER( ttl7474_d_w )
164245{
165   ttl7474_config *config = (ttl7474_config *)downcast<const legacy_device_config_base &>(device->baseconfig()).inline_config();
166   ttl7474_state *state = get_safe_token(device);
246    downcast<ttl7474_device *>(device)->d_w(state);
247}
167248
168   devcb_resolve_write_line(&state->output_cb, &config->output_cb, device);
169   devcb_resolve_write_line(&state->comp_output_cb, &config->comp_output_cb, device);
249void ttl7474_device::d_w(UINT8 state)
250{
251    m_d = state & 1;
252   update();
253}
170254
171   state->device = device;
172255
173   state_save_register_device_item(device, 0, state->clear);
174   state_save_register_device_item(device, 0, state->preset);
175   state_save_register_device_item(device, 0, state->clock);
176   state_save_register_device_item(device, 0, state->d);
177   state_save_register_device_item(device, 0, state->output);
178   state_save_register_device_item(device, 0, state->output_comp);
179   state_save_register_device_item(device, 0, state->last_clock);
180   state_save_register_device_item(device, 0, state->last_output);
181   state_save_register_device_item(device, 0, state->last_output_comp);
256//-------------------------------------------------
257//  output_r - get the output line state
258//-------------------------------------------------
259
260READ_LINE_DEVICE_HANDLER( ttl7474_output_r )
261{
262    return downcast<ttl7474_device *>(device)->output_r();
182263}
183264
184
185static DEVICE_RESET( ttl7474 )
265UINT8 ttl7474_device::output_r()
186266{
187   ttl7474_state *state = get_safe_token(device);
267    return m_output;
268}
188269
189   /* all inputs are open first */
190   state->clear = 1;
191   state->preset = 1;
192   state->clock = 1;
193   state->d = 1;
194270
195   state->output = -1;
196   state->last_clock = 1;
197   state->last_output = -1;
198   state->last_output_comp = -1;
271//-----------------------------------------------------
272//  output_comp_r - get the output-compare line state
273//-----------------------------------------------------
274
275READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r )
276{
277    return downcast<ttl7474_device *>(device)->output_comp_r();
199278}
200279
280UINT8 ttl7474_device::output_comp_r()
281{
282    return m_output_comp;
283}
201284
202static const char DEVTEMPLATE_SOURCE[] = __FILE__;
285void ttl7474_device::init()
286{
287    m_clear = 1;
288    m_preset = 1;
289    m_clk = 1;
290    m_d = 1;
203291
204#define DEVTEMPLATE_ID(p,s)      p##ttl7474##s
205#define DEVTEMPLATE_FEATURES   DT_HAS_START | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
206#define DEVTEMPLATE_NAME      "7474"
207#define DEVTEMPLATE_FAMILY      "TTL"
208#include "devtempl.h"
292    m_output = -1;
293    m_last_clock = 1;
294    m_last_output = -1;
295}
209296
210DEFINE_LEGACY_DEVICE(TTL7474, ttl7474);
297const device_type MACHINE_TTL7474 = ttl7474_device_config::static_alloc_device_config;
src/emu/machine/7474.h
r8892r8893
3737
3838*****************************************************************************/
3939
40#ifndef TTL7474_H
41#define TTL7474_H
40#pragma once
4241
43#include "devlegcy.h"
42#ifndef __TTL7474_H__
43#define __TTL7474_H__
4444
45#include "emu.h"
4546
46typedef struct _ttl7474_config ttl7474_config;
47struct _ttl7474_config
47
48
49//**************************************************************************
50//  INTERFACE CONFIGURATION MACROS
51//**************************************************************************
52
53#define MDRV_7474_ADD(_tag, _target_tag, _output_cb, _comp_output_cb) \
54    MDRV_DEVICE_ADD(_tag, MACHINE_TTL7474, 0) \
55    MDRV_7474_TARGET_TAG(_target_tag) \
56    MDRV_7474_OUTPUT_CB(_output_cb) \
57    MDRV_7474_COMP_OUTPUT_CB(_comp_output_cb)
58
59#define MDRV_7474_REPLACE(_tag, _target_tag, _output_cb, _comp_output_cb) \
60    MDRV_DEVICE_REPLACE(_tag, TTL7474, 0) \
61    MDRV_7474_TARGET_TAG(_target_tag) \
62    MDRV_7474_OUTPUT_CB(_output_cb) \
63    MDRV_7474_COMP_OUTPUT_CB(_comp_output_cb)
64
65#define MDRV_7474_TARGET_TAG(_target_tag) \
66    MDRV_DEVICE_INLINE_DATAPTR(ttl7474_device_config::INLINE_TARGET_TAG, _target_tag)
67
68#define MDRV_7474_OUTPUT_CB(_cb) \
69    MDRV_DEVICE_INLINE_DATAPTR(ttl7474_device_config::INLINE_OUTPUT_CB, _cb)
70
71#define MDRV_7474_COMP_OUTPUT_CB(_cb) \
72    MDRV_DEVICE_INLINE_DATAPTR(ttl7474_device_config::INLINE_COMP_OUTPUT_CB, _cb)
73
74
75
76//**************************************************************************
77//  TYPE DEFINITIONS
78//**************************************************************************
79
80// ======================> ttl7474_device_config
81
82class ttl7474_device_config :  public device_config
4883{
49   devcb_write_line output_cb;
50   devcb_write_line comp_output_cb;
84    friend class ttl7474_device;
85
86    // construction/destruction
87    ttl7474_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
88
89public:
90    // allocators
91    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
92    virtual device_t *alloc_device(running_machine &machine) const;
93
94    // inline configuration indexes go here
95    enum
96    {
97      INLINE_TARGET_TAG,
98        INLINE_OUTPUT_CB,
99        INLINE_COMP_OUTPUT_CB
100    };
101
102protected:
103    // device_config overrides
104    virtual void device_config_complete();
105
106    // internal state goes here
107   const char *m_target_tag;
108   void (*m_base_output_cb)(device_t *device, INT32);
109   void (*m_base_comp_output_cb)(device_t *device, INT32);
110
111    devcb_write_line m_output_cb;
112    devcb_write_line m_comp_output_cb;
51113};
52114
53115
54#define MDRV_7474_ADD(_tag, _target_tag, _output_cb, _comp_output_cb) \
55   MDRV_DEVICE_ADD(_tag, TTL7474, 0) \
56   MDRV_DEVICE_CONFIG_WRITE_LINE(ttl7474_config, output_cb, _target_tag, _output_cb) \
57   MDRV_DEVICE_CONFIG_WRITE_LINE(ttl7474_config, comp_output_cb, _target_tag, _comp_output_cb)
58116
117// ======================> ttl7474_device
59118
119class ttl7474_device : public device_t
120{
121    friend class ttl7474_device_config;
122
123    // construction/destruction
124    ttl7474_device(running_machine &_machine, const ttl7474_device_config &config);
125
126public:
127    void clear_w(UINT8 state);
128    void preset_w(UINT8 state);
129    void clock_w(UINT8 state);
130    void d_w(UINT8 state);
131    UINT8 output_r();
132    UINT8 output_comp_r();    /* NOT strictly the same as !ttl7474_output_r() */
133
134protected:
135    // device-level overrides
136    virtual void device_start();
137    virtual void device_reset();
138    virtual void device_post_load() { }
139    virtual void device_clock_changed() { }
140
141    // internal state
142    const ttl7474_device_config &m_config;
143
144private:
145    /* callbacks */
146    devcb_resolved_write_line m_output_cb;
147    devcb_resolved_write_line m_comp_output_cb;
148
149    /* inputs */
150    UINT8 m_clear;              /* pin 1/13 */
151    UINT8 m_preset;             /* pin 4/10 */
152    UINT8 m_clk;                 /* pin 3/11 */
153    UINT8 m_d;                  /* pin 2/12 */
154
155    /* outputs */
156    UINT8 m_output;             /* pin 5/9 */
157    UINT8 m_output_comp;        /* pin 6/8 */
158
159    /* internal */
160    UINT8 m_last_clock;
161    UINT8 m_last_output;
162    UINT8 m_last_output_comp;
163
164    void update();
165    void init();
166};
167
168
169// device type definition
170extern const device_type MACHINE_TTL7474;
171
172
173
174//**************************************************************************
175//  READ/WRITE HANDLERS
176//**************************************************************************
177
60178WRITE_LINE_DEVICE_HANDLER( ttl7474_clear_w );
61179WRITE_LINE_DEVICE_HANDLER( ttl7474_preset_w );
62180WRITE_LINE_DEVICE_HANDLER( ttl7474_clock_w );
63181WRITE_LINE_DEVICE_HANDLER( ttl7474_d_w );
64182READ_LINE_DEVICE_HANDLER( ttl7474_output_r );
65READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r );   /* NOT strictly the same as !ttl7474_output_r() */
183READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r );    /* NOT strictly the same as !ttl7474_output_r() */
66184
67DECLARE_LEGACY_DEVICE(TTL7474, ttl7474);
68185
69#endif
186#endif /* __TTL7474_H__ */
No newline at end of file
src/emu/machine/i2cmem.c
r8892r8893
183183   m_sdar( 1 ),
184184   m_state( STATE_IDLE )
185185{
186   if( m_page_size > 0 )
186   if( m_config.m_page_size > 0 )
187187   {
188      m_page = auto_alloc_array( machine, UINT8, m_page_size );
188      m_page = auto_alloc_array( machine, UINT8, m_config.m_page_size );
189189   }
190190}
191191
r8892r8893
208208   state_save_register_device_item( this, 0, m_shift );
209209   state_save_register_device_item( this, 0, m_devsel );
210210   state_save_register_device_item( this, 0, m_byteaddr );
211   state_save_register_device_item_pointer( this, 0, m_page, m_page_size );
211   state_save_register_device_item_pointer( this, 0, m_page, m_config.m_page_size );
212212}
213213
214214
r8892r8893
450450                     verboselog( this, 0, "write not enabled\n" );
451451                     m_state = STATE_IDLE;
452452                  }
453                  else if( m_page_size > 0 )
453                  else if( m_config.m_page_size > 0 )
454454                  {
455455                     m_page[ m_page_offset ] = m_shift;
456456                     verboselog( this, 1, "page[ %04x ] <- %02x\n", m_page_offset, m_page[ m_page_offset ] );
457457
458458                     m_page_offset++;
459                     if( m_page_offset == m_page_size )
459                     if( m_page_offset == m_config.m_page_size )
460460                     {
461                        int offset = data_offset() & ~( m_page_size - 1 );
461                        int offset = data_offset() & ~( m_config.m_page_size - 1 );
462462
463                        verboselog( this, 1, "data[ %04x to %04x ] = page\n", offset, offset + m_page_size - 1 );
463                        verboselog( this, 1, "data[ %04x to %04x ] = page\n", offset, offset + m_config.m_page_size - 1 );
464464
465                        for( int i = 0; i < m_page_size; i++ )
465                        for( int i = 0; i < m_config.m_page_size; i++ )
466466                        {
467467                           m_addrspace[ 0 ]->write_byte( offset + i, m_page[ i ] );
468468                        }
src/emu/machine/i2cmem.h
r8892r8893
135135   int m_byteaddr;
136136   UINT8 *m_page;
137137   int m_page_offset;
138   int m_page_size;
139138};
140139
141140

Previous 15994 Revisions Next


© 1998-2018 The MESS Team