Previous 15994 Revisions Next

r8886 Tuesday 24th August, 2010 at 14:37:17 UTC by Miodrag Milanović
Sync with MAME
[src/emu/machine]6522via.c 6522via.h 6532riot.c 6532riot.h 6821pia.c 6821pia.h 74123.c 74123.h 7474.c 7474.h

src/emu/machine/74123.c
r8885r8886
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/74123.h
r8885r8886
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)
r8885r8886
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
r8885r8886
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
r8885r8886
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);
13885
139   /* clear structure */
140   memset(p, 0, sizeof(*p));
86}
14187
88
89
90//**************************************************************************
91//  LIVE DEVICE
92//**************************************************************************
93
94//-------------------------------------------------
95//  pia6821_device - constructor
96//-------------------------------------------------
97
98pia6821_device::pia6821_device(running_machine &_machine, const pia6821_device_config &config)
99    : device_t(_machine, config),
100      m_config(config)
101{
102
103}
104
105//-------------------------------------------------
106//  device_start - device-specific startup
107//-------------------------------------------------
108
109void pia6821_device::device_start()
110{
142111   /* 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);
112    devcb_resolve_read8(&m_in_a_func, &m_config.m_in_a_func, this);
113    devcb_resolve_read8(&m_in_b_func, &m_config.m_in_b_func, this);
114    devcb_resolve_read_line(&m_in_ca1_func, &m_config.m_in_ca1_func, this);
115    devcb_resolve_read_line(&m_in_cb1_func, &m_config.m_in_cb1_func, this);
116    devcb_resolve_read_line(&m_in_ca2_func, &m_config.m_in_ca2_func, this);
117    devcb_resolve_read_line(&m_in_cb2_func, &m_config.m_in_cb2_func, this);
118    devcb_resolve_write8(&m_out_a_func, &m_config.m_out_a_func, this);
119    devcb_resolve_write8(&m_out_b_func, &m_config.m_out_b_func, this);
120    devcb_resolve_write_line(&m_out_ca2_func, &m_config.m_out_ca2_func, this);
121    devcb_resolve_write_line(&m_out_cb2_func, &m_config.m_out_cb2_func, this);
122    devcb_resolve_write_line(&m_irq_a_func, &m_config.m_irq_a_func, this);
123    devcb_resolve_write_line(&m_irq_b_func, &m_config.m_irq_b_func, this);
155124
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);
125    state_save_register_device_item(this, 0, m_in_a);
126    state_save_register_device_item(this, 0, m_in_ca1);
127    state_save_register_device_item(this, 0, m_in_ca2);
128    state_save_register_device_item(this, 0, m_out_a);
129    state_save_register_device_item(this, 0, m_out_ca2);
130    state_save_register_device_item(this, 0, m_port_a_z_mask);
131    state_save_register_device_item(this, 0, m_ddr_a);
132    state_save_register_device_item(this, 0, m_ctl_a);
133    state_save_register_device_item(this, 0, m_irq_a1);
134    state_save_register_device_item(this, 0, m_irq_a2);
135    state_save_register_device_item(this, 0, m_irq_a_state);
136    state_save_register_device_item(this, 0, m_in_b);
137    state_save_register_device_item(this, 0, m_in_cb1);
138    state_save_register_device_item(this, 0, m_in_cb2);
139    state_save_register_device_item(this, 0, m_out_b);
140    state_save_register_device_item(this, 0, m_out_cb2);
141    state_save_register_device_item(this, 0, m_last_out_cb2_z);
142    state_save_register_device_item(this, 0, m_ddr_b);
143    state_save_register_device_item(this, 0, m_ctl_b);
144    state_save_register_device_item(this, 0, m_irq_b1);
145    state_save_register_device_item(this, 0, m_irq_b2);
146    state_save_register_device_item(this, 0, m_irq_b_state);
147    state_save_register_device_item(this, 0, m_in_a_pushed);
148    state_save_register_device_item(this, 0, m_out_a_needs_pulled);
149    state_save_register_device_item(this, 0, m_in_ca1_pushed);
150    state_save_register_device_item(this, 0, m_in_ca2_pushed);
151    state_save_register_device_item(this, 0, m_out_ca2_needs_pulled);
152    state_save_register_device_item(this, 0, m_in_b_pushed);
153    state_save_register_device_item(this, 0, m_out_b_needs_pulled);
154    state_save_register_device_item(this, 0, m_in_cb1_pushed);
155    state_save_register_device_item(this, 0, m_in_cb2_pushed);
156    state_save_register_device_item(this, 0, m_out_cb2_needs_pulled);
188157}
189158
190159
191/*-------------------------------------------------
192    DEVICE_RESET( pia6821 )
193-------------------------------------------------*/
160//-------------------------------------------------
161//  device_reset - device-specific reset
162//-------------------------------------------------
194163
195static DEVICE_RESET( pia6821 )
164void pia6821_device::device_reset()
196165{
197   pia6821_state *p = get_token(device);
198
199166   /*
200167     * set default read values.
201168     *
202169     * ports A,CA1,CA2 default to 1
203170     * ports B,CB1,CB2 are three-state and undefined (set to 0)
204171     */
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;
172   m_in_a = 0xff;
173   m_in_ca1 = TRUE;
174   m_in_ca2 = TRUE;
175   m_out_a = 0;
176   m_out_ca2 = 0;
177   m_port_a_z_mask = 0;
178   m_ddr_a = 0;
179   m_ctl_a = 0;
180   m_irq_a1 = 0;
181   m_irq_a2 = 0;
182   m_irq_a_state = 0;
183   m_in_b = 0;
184   m_in_cb1 = 0;
185   m_in_cb2 = 0;
186   m_out_b = 0;
187   m_out_cb2 = 0;
188   m_last_out_cb2_z = 0;
189   m_ddr_b = 0;
190   m_ctl_b = 0;
191   m_irq_b1 = 0;
192   m_irq_b2 = 0;
193   m_irq_b_state = 0;
194   m_in_a_pushed = 0;
195   m_out_a_needs_pulled = 0;
196   m_in_ca1_pushed = 0;
197   m_in_ca2_pushed = 0;
198   m_out_ca2_needs_pulled = 0;
199   m_in_b_pushed = 0;
200   m_out_b_needs_pulled = 0;
201   m_in_cb1_pushed = 0;
202   m_in_cb2_pushed = 0;
203   m_out_cb2_needs_pulled = 0;
204   m_logged_port_a_not_connected = 0;
205   m_logged_port_b_not_connected = 0;
206   m_logged_ca1_not_connected = 0;
207   m_logged_ca2_not_connected = 0;
208   m_logged_cb1_not_connected = 0;
209   m_logged_cb2_not_connected = 0;
243210
244211
245212   /* clear the IRQs */
246   devcb_call_write_line(&p->irq_a_func, FALSE);
247   devcb_call_write_line(&p->irq_b_func, FALSE);
213   devcb_call_write_line(&m_irq_a_func, FALSE);
214   devcb_call_write_line(&m_irq_b_func, FALSE);
248215}
249216
250217
r8885r8886
252219    update_interrupts
253220-------------------------------------------------*/
254221
255static void update_interrupts(running_device *device)
222void pia6821_device::update_interrupts()
256223{
257   pia6821_state *p = get_token(device);
258   int new_state;
224    /* start with IRQ A */
225   int new_state = (m_irq_a1 && IRQ1_ENABLED(m_ctl_a)) || (m_irq_a2 && IRQ2_ENABLED(m_ctl_a));
259226
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)
227   if (new_state != m_irq_a_state)
264228   {
265      p->irq_a_state = new_state;
266      devcb_call_write_line(&p->irq_a_func, p->irq_a_state);
229      m_irq_a_state = new_state;
230      devcb_call_write_line(&m_irq_a_func, m_irq_a_state);
267231   }
268232
269233   /* then do IRQ B */
270   new_state = (p->irq_b1 && IRQ1_ENABLED(p->ctl_b)) || (p->irq_b2 && IRQ2_ENABLED(p->ctl_b));
234   new_state = (m_irq_b1 && IRQ1_ENABLED(m_ctl_b)) || (m_irq_b2 && IRQ2_ENABLED(m_ctl_b));
271235
272   if (new_state != p->irq_b_state)
236   if (new_state != m_irq_b_state)
273237   {
274      p->irq_b_state = new_state;
275      devcb_call_write_line(&p->irq_b_func, p->irq_b_state);
238      m_irq_b_state = new_state;
239      devcb_call_write_line(&m_irq_b_func, m_irq_b_state);
276240   }
277241}
278242
r8885r8886
281245    get_in_a_value
282246-------------------------------------------------*/
283247
284static UINT8 get_in_a_value(running_device *device)
248UINT8 pia6821_device::get_in_a_value()
285249{
286   pia6821_state *p = get_token(device);
287250   UINT8 port_a_data = 0;
288251   UINT8 ret;
289252
290253   /* update the input */
291   if (p->in_a_func.read != NULL)
292      port_a_data = devcb_call_read8(&p->in_a_func, 0);
254   if (m_in_a_func.read != NULL)
255    {
256      port_a_data = devcb_call_read8(&m_in_a_func, 0);
257    }
293258   else
294259   {
295      if (p->in_a_pushed)
296         port_a_data = p->in_a;
260      if (m_in_a_pushed)
261        {
262         port_a_data = m_in_a;
263        }
297264      else
298265      {
299266         /* mark all pins disconnected */
300         p->port_a_z_mask = 0xff;
267         m_port_a_z_mask = 0xff;
301268
302         if (!p->logged_port_a_not_connected && (p->ddr_a != 0xff))
269         if (!m_logged_port_a_not_connected && (m_ddr_a != 0xff))
303270         {
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;
271            logerror("PIA #%s: Warning! No port A read handler. Assuming pins 0x%02X not connected\n", tag(), m_ddr_a ^ 0xff);
272            m_logged_port_a_not_connected = TRUE;
306273         }
307274      }
308275   }
r8885r8886
310277   /* - connected pins are always read
311278       - disconnected pins read the output buffer in output mode
312279       - 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);
280   ret = (~m_port_a_z_mask             & port_a_data) |
281         ( m_port_a_z_mask &  m_ddr_a & m_out_a) |
282         ( m_port_a_z_mask & ~m_ddr_a);
316283
317284   return ret;
318285}
r8885r8886
322289    get_in_b_value
323290-------------------------------------------------*/
324291
325static UINT8 get_in_b_value(running_device *device)
292UINT8 pia6821_device::get_in_b_value()
326293{
327   pia6821_state *p = get_token(device);
328294   UINT8 ret;
329295
330   if (p->ddr_b == 0xff)
296   if (m_ddr_b == 0xff)
297    {
331298      /* all output, just return buffer */
332      ret = p->out_b;
299      ret = m_out_b;
300    }
333301   else
334302   {
335303      UINT8 port_b_data;
336304
337305      /* update the input */
338      if (p->in_b_func.read != NULL)
339         port_b_data = devcb_call_read8(&p->in_b_func, 0);
306      if (m_in_b_func.read != NULL)
307        {
308         port_b_data = devcb_call_read8(&m_in_b_func, 0);
309        }
340310      else
341311      {
342         if (p->in_b_pushed)
343            port_b_data = p->in_b;
312         if (m_in_b_pushed)
313            {
314            port_b_data = m_in_b;
315            }
344316         else
345317         {
346            if (!p->logged_port_b_not_connected && (p->ddr_b != 0xff))
318            if (!m_logged_port_b_not_connected && (m_ddr_b != 0xff))
347319            {
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;
320               logerror("PIA #%s: Error! No port B read handler. Three-state pins 0x%02X are undefined\n", tag(), m_ddr_b ^ 0xff);
321               m_logged_port_b_not_connected = TRUE;
350322            }
351323
352324            /* undefined -- need to return something */
r8885r8886
355327      }
356328
357329      /* 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);
330      ret = (m_out_b & m_ddr_b) | (port_b_data & ~m_ddr_b);
359331   }
360332
361333   return ret;
r8885r8886
366338    get_out_a_value
367339-------------------------------------------------*/
368340
369static UINT8 get_out_a_value(running_device *device)
341UINT8 pia6821_device::get_out_a_value()
370342{
371   pia6821_state *p = get_token(device);
372343   UINT8 ret;
373344
374   if (p->ddr_a == 0xff)
345   if (m_ddr_a == 0xff)
346    {
375347      /* all output */
376      ret = p->out_a;
348      ret = m_out_a;
349    }
377350   else
351    {
378352      /* input pins don't change */
379      ret = (p->out_a & p->ddr_a) | (get_in_a_value(device) & ~p->ddr_a);
353      ret = (m_out_a & m_ddr_a) | (get_in_a_value() & ~m_ddr_a);
354    }
380355
381356   return ret;
382357}
r8885r8886
386361    get_out_b_value
387362-------------------------------------------------*/
388363
389static UINT8 get_out_b_value(running_device *device)
364UINT8 pia6821_device::get_out_b_value()
390365{
391   pia6821_state *p = get_token(device);
392
393366   /* input pins are high-impedance - we just send them as zeros for backwards compatibility */
394   return p->out_b & p->ddr_b;
367   return m_out_b & m_ddr_b;
395368}
396369
397370
r8885r8886
399372    set_out_ca2
400373-------------------------------------------------*/
401374
402static void set_out_ca2(running_device *device, int data)
375void pia6821_device::set_out_ca2(int data)
403376{
404   pia6821_state *p = get_token(device);
405
406   if (data != p->out_ca2)
377   if (data != m_out_ca2)
407378   {
408      p->out_ca2 = data;
379      m_out_ca2 = data;
409380
410381      /* send to output function */
411      if (p->out_ca2_func.write)
412         devcb_call_write_line(&p->out_ca2_func, p->out_ca2);
382      if (m_out_ca2_func.write)
383        {
384         devcb_call_write_line(&m_out_ca2_func, m_out_ca2);
385        }
413386      else
414387      {
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());
388         if (m_out_ca2_needs_pulled)
389            {
390            logerror("PIA #%s: Warning! No port CA2 write handler. Previous value has been lost!\n", tag());
391            }
417392
418         p->out_ca2_needs_pulled = TRUE;
393         m_out_ca2_needs_pulled = TRUE;
419394      }
420395   }
421396}
r8885r8886
425400    set_out_cb2
426401-------------------------------------------------*/
427402
428static void set_out_cb2(running_device *device, int data)
403void pia6821_device::set_out_cb2(int data)
429404{
430   pia6821_state *p = get_token(device);
405   int z = pia6821_get_output_cb2_z(this);
431406
432   int z = pia6821_get_output_cb2_z(device);
433
434   if ((data != p->out_cb2) || (z != p->last_out_cb2_z))
407   if ((data != m_out_cb2) || (z != m_last_out_cb2_z))
435408   {
436      p->out_cb2 = data;
437      p->last_out_cb2_z = z;
409      m_out_cb2 = data;
410      m_last_out_cb2_z = z;
438411
439412      /* send to output function */
440      if (p->out_cb2_func.write)
441         devcb_call_write_line(&p->out_cb2_func, p->out_cb2);
413      if (m_out_cb2_func.write)
414        {
415         devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
416        }
442417      else
443418      {
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());
419         if (m_out_cb2_needs_pulled)
420            {
421            logerror("PIA #%s: Warning! No port CB2 write handler. Previous value has been lost!\n", tag());
422            }
446423
447         p->out_cb2_needs_pulled = TRUE;
424         m_out_cb2_needs_pulled = TRUE;
448425      }
449426   }
450427}
r8885r8886
454431    port_a_r
455432-------------------------------------------------*/
456433
457static UINT8 port_a_r(running_device *device)
434UINT8 pia6821_device::port_a_r()
458435{
459   pia6821_state *p = get_token(device);
436   UINT8 ret = get_in_a_value();
460437
461   UINT8 ret = get_in_a_value(device);
462
463438   /* IRQ flags implicitly cleared by a read */
464   p->irq_a1 = FALSE;
465   p->irq_a2 = FALSE;
466   update_interrupts(device);
439   m_irq_a1 = FALSE;
440   m_irq_a2 = FALSE;
441   update_interrupts();
467442
468443   /* CA2 is configured as output and in read strobe mode */
469   if (C2_OUTPUT(p->ctl_a) && C2_STROBE_MODE(p->ctl_a))
444   if(C2_OUTPUT(m_ctl_a) && C2_STROBE_MODE(m_ctl_a))
470445   {
471446      /* this will cause a transition low */
472      set_out_ca2(device, FALSE);
447      set_out_ca2(FALSE);
473448
474449      /* 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);
450      if(STROBE_E_RESET(m_ctl_a))
451        {
452         set_out_ca2(TRUE);
453        }
477454   }
478455
479   LOG(("PIA #%s: port A read = %02X\n", device->tag(), ret));
456   LOG(("PIA #%s: port A read = %02X\n", tag(), ret));
480457
481458   return ret;
482459}
r8885r8886
486463    ddr_a_r
487464-------------------------------------------------*/
488465
489static UINT8 ddr_a_r(running_device *device)
466UINT8 pia6821_device::ddr_a_r()
490467{
491   pia6821_state *p = get_token(device);
468   UINT8 ret = m_ddr_a;
492469
493   UINT8 ret = p->ddr_a;
470   LOG(("PIA #%s: DDR A read = %02X\n", tag(), ret));
494471
495   LOG(("PIA #%s: DDR A read = %02X\n", device->tag(), ret));
496
497472   return ret;
498473}
499474
r8885r8886
502477    port_b_r
503478-------------------------------------------------*/
504479
505static UINT8 port_b_r(running_device *device)
480UINT8 pia6821_device::port_b_r()
506481{
507   pia6821_state *p = get_token(device);
482   UINT8 ret = get_in_b_value();
508483
509   UINT8 ret = get_in_b_value(device);
510
511484   /* This read will implicitly clear the IRQ B1 flag.  If CB2 is in write-strobe
512485       mode with CB1 restore, and a CB1 active transition set the flag,
513486       clearing it will cause CB2 to go high again.  Note that this is different
514487       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);
488   if(m_irq_b1 && C2_STROBE_MODE(m_ctl_b) && STROBE_C1_RESET(m_ctl_b))
489    {
490      set_out_cb2(TRUE);
491    }
517492
518493   /* IRQ flags implicitly cleared by a read */
519   p->irq_b1 = FALSE;
520   p->irq_b2 = FALSE;
521   update_interrupts(device);
494   m_irq_b1 = FALSE;
495   m_irq_b2 = FALSE;
496   update_interrupts();
522497
523   LOG(("PIA #%s: port B read = %02X\n", device->tag(), ret));
498   LOG(("PIA #%s: port B read = %02X\n", tag(), ret));
524499
525500   return ret;
526501}
r8885r8886
530505    ddr_b_r
531506-------------------------------------------------*/
532507
533static UINT8 ddr_b_r(running_device *device)
508UINT8 pia6821_device::ddr_b_r()
534509{
535   pia6821_state *p = get_token(device);
510   UINT8 ret = m_ddr_b;
536511
537   UINT8 ret = p->ddr_b;
512   LOG(("PIA #%s: DDR B read = %02X\n", tag(), ret));
538513
539   LOG(("PIA #%s: DDR B read = %02X\n", device->tag(), ret));
540
541514   return ret;
542515}
543516
r8885r8886
546519    control_a_r
547520-------------------------------------------------*/
548521
549static UINT8 control_a_r(running_device *device)
522UINT8 pia6821_device::control_a_r()
550523{
551   pia6821_state *p = get_token(device);
552524   UINT8 ret;
553525
554526   /* 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))
527   if (m_in_ca1_func.read != NULL)
528    {
529      ca1_w(devcb_call_read_line(&m_in_ca1_func));
530    }
531   else if(!m_logged_ca1_not_connected && (!m_in_ca1_pushed))
558532   {
559      logerror("PIA #%s: Warning! No CA1 read handler. Assuming pin not connected\n", device->tag());
560      p->logged_ca1_not_connected = TRUE;
533      logerror("PIA #%s: Warning! No CA1 read handler. Assuming pin not connected\n", tag());
534      m_logged_ca1_not_connected = TRUE;
561535   }
562536
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)
537   if (m_in_ca2_func.read != NULL)
538    {
539      ca2_w(devcb_call_read_line(&m_in_ca2_func));
540    }
541   else if ( !m_logged_ca2_not_connected && C2_INPUT(m_ctl_a) && !m_in_ca2_pushed)
566542   {
567      logerror("PIA #%s: Warning! No CA2 read handler. Assuming pin not connected\n", device->tag());
568      p->logged_ca2_not_connected = TRUE;
543      logerror("PIA #%s: Warning! No CA2 read handler. Assuming pin not connected\n", tag());
544      m_logged_ca2_not_connected = TRUE;
569545   }
570546
571547   /* read control register */
572   ret = p->ctl_a;
548   ret = m_ctl_a;
573549
574550   /* set the IRQ flags if we have pending IRQs */
575   if (p->irq_a1)
551   if(m_irq_a1)
552    {
576553      ret |= PIA_IRQ1;
554    }
577555
578   if (p->irq_a2 && C2_INPUT(p->ctl_a))
556   if(m_irq_a2 && C2_INPUT(m_ctl_a))
557    {
579558      ret |= PIA_IRQ2;
559    }
580560
581   LOG(("PIA #%s: control A read = %02X\n", device->tag(), ret));
561   LOG(("PIA #%s: control A read = %02X\n", tag(), ret));
582562
583563   return ret;
584564}
r8885r8886
588568    control_b_r
589569-------------------------------------------------*/
590570
591static UINT8 control_b_r(running_device *device)
571UINT8 pia6821_device::control_b_r()
592572{
593   pia6821_state *p = get_token(device);
594573   UINT8 ret;
595574
596575   /* 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)
576   if(m_in_cb1_func.read != NULL)
577    {
578      cb1_w(devcb_call_read_line(&m_in_cb1_func));
579    }
580   else if(!m_logged_cb1_not_connected && !m_in_cb1_pushed)
600581   {
601      logerror("PIA #%s: Error! no CB1 read handler. Three-state pin is undefined\n", device->tag());
602      p->logged_cb1_not_connected = TRUE;
582      logerror("PIA #%s: Error! no CB1 read handler. Three-state pin is undefined\n", tag());
583      m_logged_cb1_not_connected = TRUE;
603584   }
604585
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)
586   if(m_in_cb2_func.read != NULL)
587    {
588      cb2_w(devcb_call_read_line(&m_in_cb2_func));
589    }
590   else if(!m_logged_cb2_not_connected && C2_INPUT(m_ctl_b) && !m_in_cb2_pushed)
608591   {
609      logerror("PIA #%s: Error! No CB2 read handler. Three-state pin is undefined\n", device->tag());
610      p->logged_cb2_not_connected = TRUE;
592      logerror("PIA #%s: Error! No CB2 read handler. Three-state pin is undefined\n", tag());
593      m_logged_cb2_not_connected = TRUE;
611594   }
612595
613596   /* read control register */
614   ret = p->ctl_b;
597   ret = m_ctl_b;
615598
616599   /* set the IRQ flags if we have pending IRQs */
617   if (p->irq_b1)
600   if(m_irq_b1)
601    {
618602      ret |= PIA_IRQ1;
603    }
619604
620   if (p->irq_b2 && C2_INPUT(p->ctl_b))
605   if(m_irq_b2 && C2_INPUT(m_ctl_b))
606    {
621607      ret |= PIA_IRQ2;
608    }
622609
623   LOG(("PIA #%s: control B read = %02X\n", device->tag(), ret));
610   LOG(("PIA #%s: control B read = %02X\n", tag(), ret));
624611
625612   return ret;
626613}
r8885r8886
632619
633620READ8_DEVICE_HANDLER( pia6821_r )
634621{
635   pia6821_state *p = get_token(device);
622    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
623    return dev->reg_r(offset);
624}
625
626UINT8 pia6821_device::reg_r(UINT8 offset)
627{
636628   UINT8 ret;
637629
638630   switch (offset & 0x03)
639631   {
640632      default: /* impossible */
641633      case 0x00:
642         if (OUTPUT_SELECTED(p->ctl_a))
643            ret = port_a_r(device);
634         if (OUTPUT_SELECTED(m_ctl_a))
635            {
636            ret = port_a_r();
637            }
644638         else
645            ret = ddr_a_r(device);
639            {
640            ret = ddr_a_r();
641            }
646642         break;
647643
648644      case 0x01:
649         ret = control_a_r(device);
645         ret = control_a_r();
650646         break;
651647
652648      case 0x02:
653         if (OUTPUT_SELECTED(p->ctl_b))
654            ret = port_b_r(device);
649         if (OUTPUT_SELECTED(m_ctl_b))
650            {
651            ret = port_b_r();
652            }
655653         else
656            ret = ddr_b_r(device);
654            {
655            ret = ddr_b_r();
656            }
657657         break;
658658
659659      case 0x03:
660         ret = control_b_r(device);
660         ret = control_b_r();
661661         break;
662662   }
663663
r8885r8886
671671
672672READ8_DEVICE_HANDLER( pia6821_alt_r )
673673{
674   return pia6821_r(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01));
674    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
675    return dev->reg_r(((offset << 1) & 0x02) | ((offset >> 1) & 0x01));
675676}
676677
677678
679
678680/*-------------------------------------------------
679681    pia6821_get_port_b_z_mask
680682-------------------------------------------------*/
681683
682684UINT8 pia6821_get_port_b_z_mask(running_device *device)
683685{
684   pia6821_state *p = get_token(device);
685   return ~p->ddr_b;
686    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
687    return dev->get_port_b_z_mask();
686688}
687689
690UINT8 pia6821_device::get_port_b_z_mask()
691{
692   return ~m_ddr_b;
693}
688694
695
689696/*-------------------------------------------------
690697    send_to_out_a_func
691698-------------------------------------------------*/
692699
693static void send_to_out_a_func(running_device *device, const char* message)
700void pia6821_device::send_to_out_a_func(const char* message)
694701{
695   pia6821_state *p = get_token(device);
696
697702   /* input pins are pulled high */
698   UINT8 data = get_out_a_value(device);
703   UINT8 data = get_out_a_value();
699704
700   LOG(("PIA #%s: %s = %02X\n", device->tag(), message, data));
705   LOG(("PIA #%s: %s = %02X\n", tag(), message, data));
701706
702   if (p->out_a_func.write != NULL)
703      devcb_call_write8(&p->out_a_func, 0, data);
707   if(m_out_a_func.write != NULL)
708    {
709      devcb_call_write8(&m_out_a_func, 0, data);
710    }
704711   else
705712   {
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());
713      if(m_out_a_needs_pulled)
714        {
715         logerror("PIA #%s: Warning! No port A write handler. Previous value has been lost!\n", tag());
716        }
708717
709      p->out_a_needs_pulled = TRUE;
718      m_out_a_needs_pulled = TRUE;
710719   }
711720}
712721
r8885r8886
715724    send_to_out_b_func
716725-------------------------------------------------*/
717726
718static void send_to_out_b_func(running_device *device, const char* message)
727void pia6821_device::send_to_out_b_func(const char* message)
719728{
720   pia6821_state *p = get_token(device);
721
722729   /* input pins are high-impedance - we just send them as zeros for backwards compatibility */
723   UINT8 data = get_out_b_value(device);
730   UINT8 data = get_out_b_value();
724731
725   LOG(("PIA #%s: %s = %02X\n", device->tag(), message, data));
732   LOG(("PIA #%s: %s = %02X\n", tag(), message, data));
726733
727   if (p->out_b_func.write != NULL)
728      devcb_call_write8(&p->out_b_func, 0, data);
734   if(m_out_b_func.write != NULL)
735    {
736      devcb_call_write8(&m_out_b_func, 0, data);
737    }
729738   else
730739   {
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());
740      if(m_out_b_needs_pulled)
741        {
742         logerror("PIA #%s: Warning! No port B write handler. Previous value has been lost!\n", tag());
743        }
733744
734      p->out_b_needs_pulled = TRUE;
745      m_out_b_needs_pulled = TRUE;
735746   }
736747}
737748
r8885r8886
740751    port_a_w
741752-------------------------------------------------*/
742753
743static void port_a_w(running_device *device, UINT8 data)
754void pia6821_device::port_a_w(UINT8 data)
744755{
745   pia6821_state *p = get_token(device);
746
747756   /* buffer the output value */
748   p->out_a = data;
757   m_out_a = data;
749758
750   send_to_out_a_func(device, "port A write");
759   send_to_out_a_func("port A write");
751760}
752761
753762
r8885r8886
755764    ddr_a_w
756765-------------------------------------------------*/
757766
758static void ddr_a_w(running_device *device, UINT8 data)
767void pia6821_device::ddr_a_w(UINT8 data)
759768{
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));
769   if(data == 0x00)
770    {
771      LOG(("PIA #%s: DDR A write = %02X (input mode)\n", tag(), data));
772    }
773   else if(data == 0xff)
774    {
775      LOG(("PIA #%s: DDR A write = %02X (output mode)\n", tag(), data));
776    }
766777   else
767      LOG(("PIA #%s: DDR A write = %02X (mixed mode)\n", device->tag(), data));
778    {
779      LOG(("PIA #%s: DDR A write = %02X (mixed mode)\n", tag(), data));
780    }
768781
769   if (p->ddr_a != data)
782   if(m_ddr_a != data)
770783   {
771784      /* 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");
785      m_ddr_a = data;
786      m_logged_port_a_not_connected = FALSE;
787      send_to_out_a_func("port A write due to DDR change");
775788   }
776789}
777790
r8885r8886
780793    port_b_w
781794-------------------------------------------------*/
782795
783static void port_b_w(running_device *device, UINT8 data)
796void pia6821_device::port_b_w(UINT8 data)
784797{
785   pia6821_state *p = get_token(device);
786
787798   /* buffer the output value */
788   p->out_b = data;
799   m_out_b = data;
789800
790   send_to_out_b_func(device, "port B write");
801   send_to_out_b_func("port B write");
791802
792803   /* CB2 in write strobe mode */
793   if (C2_STROBE_MODE(p->ctl_b))
804   if(C2_STROBE_MODE(m_ctl_b))
794805   {
795806      /* this will cause a transition low */
796      set_out_cb2(device, FALSE);
807      set_out_cb2(FALSE);
797808
798809      /* 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);
810      if(STROBE_E_RESET(m_ctl_b))
811        {
812         set_out_cb2(TRUE);
813        }
801814   }
802815}
803816
r8885r8886
806819    ddr_b_w
807820-------------------------------------------------*/
808821
809static void ddr_b_w(running_device *device, UINT8 data)
822void pia6821_device::ddr_b_w(UINT8 data)
810823{
811   pia6821_state *p = get_token(device);
812
813824   if (data == 0x00)
814      LOG(("PIA #%s: DDR B write = %02X (input mode)\n", device->tag(), data));
825    {
826      LOG(("PIA #%s: DDR B write = %02X (input mode)\n", tag(), data));
827    }
815828   else if (data == 0xff)
816      LOG(("PIA #%s: DDR B write = %02X (output mode)\n", device->tag(), data));
829    {
830      LOG(("PIA #%s: DDR B write = %02X (output mode)\n", tag(), data));
831    }
817832   else
818      LOG(("PIA #%s: DDR B write = %02X (mixed mode)\n", device->tag(), data));
833    {
834      LOG(("PIA #%s: DDR B write = %02X (mixed mode)\n", tag(), data));
835    }
819836
820   if (p->ddr_b != data)
837   if(m_ddr_b != data)
821838   {
822839      /* 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");
840      m_ddr_b = data;
841      m_logged_port_b_not_connected = FALSE;
842      send_to_out_b_func("port B write due to DDR change");
826843   }
827844}
828845
r8885r8886
831848    control_a_w
832849-------------------------------------------------*/
833850
834static void control_a_w(running_device *device, UINT8 data)
851void pia6821_device::control_a_w(UINT8 data)
835852{
836   pia6821_state *p = get_token(device);
837
838853   /* bit 7 and 6 are read only */
839854   data &= 0x3f;
840855
841   LOG(("PIA #%s: control A write = %02X\n", device->tag(), data));
856   LOG(("PIA #%s: control A write = %02X\n", tag(), data));
842857
843858   /* update the control register */
844   p->ctl_a = data;
859   m_ctl_a = data;
845860
846861   /* CA2 is configured as output */
847   if (C2_OUTPUT(p->ctl_a))
862   if(C2_OUTPUT(m_ctl_a))
848863   {
849864      int temp;
850865
851      if (C2_SET_MODE(p->ctl_a))
866      if(C2_SET_MODE(m_ctl_a))
867        {
852868         /* set/reset mode - bit value determines the new output */
853         temp = C2_SET(p->ctl_a);
869         temp = C2_SET(m_ctl_a);
870        }
854871      else
872        {
855873         /* strobe mode - output is always high unless strobed */
856874         temp = TRUE;
875        }
857876
858      set_out_ca2(device, temp);
877      set_out_ca2(temp);
859878   }
860879
861880   /* update externals */
862   update_interrupts(device);
881   update_interrupts();
863882}
864883
865884
r8885r8886
867886    control_b_w
868887-------------------------------------------------*/
869888
870static void control_b_w(running_device *device, UINT8 data)
889void pia6821_device::control_b_w(UINT8 data)
871890{
872   pia6821_state *p = get_token(device);
873891   int temp;
874892
875893   /* bit 7 and 6 are read only */
876894   data &= 0x3f;
877895
878   LOG(("PIA #%s: control B write = %02X\n", device->tag(), data));
896   LOG(("PIA #%s: control B write = %02X\n", tag(), data));
879897
880898   /* update the control register */
881   p->ctl_b = data;
899   m_ctl_b = data;
882900
883   if (C2_SET_MODE(p->ctl_b))
901   if (C2_SET_MODE(m_ctl_b))
902    {
884903      /* set/reset mode - bit value determines the new output */
885      temp = C2_SET(p->ctl_b);
904      temp = C2_SET(m_ctl_b);
905    }
886906   else
907    {
887908      /* strobe mode - output is always high unless strobed */
888909      temp = TRUE;
910    }
889911
890   set_out_cb2(device, temp);
912   set_out_cb2(temp);
891913
892914   /* update externals */
893   update_interrupts(device);
915   update_interrupts();
894916}
895917
896918
r8885r8886
900922
901923WRITE8_DEVICE_HANDLER( pia6821_w )
902924{
903   pia6821_state *p = get_token(device);
925    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
926    dev->reg_w(offset, data);
927}
904928
929void pia6821_device::reg_w(UINT8 offset, UINT8 data)
930{
905931   switch (offset & 0x03)
906932   {
907933      default: /* impossible */
908934      case 0x00:
909         if (OUTPUT_SELECTED(p->ctl_a))
910            port_a_w(device, data);
935         if (OUTPUT_SELECTED(m_ctl_a))
936            {
937            port_a_w(data);
938            }
911939         else
912            ddr_a_w(device, data);
940            {
941            ddr_a_w(data);
942            }
913943         break;
914944
915945      case 0x01:
916         control_a_w(device, data);
946         control_a_w( data);
917947         break;
918948
919949      case 0x02:
920         if (OUTPUT_SELECTED(p->ctl_b))
921            port_b_w(device, data);
950         if(OUTPUT_SELECTED(m_ctl_b))
951            {
952            port_b_w(data);
953            }
922954         else
923            ddr_b_w(device, data);
955            {
956            ddr_b_w(data);
957            }
924958         break;
925959
926960      case 0x03:
927         control_b_w(device, data);
961         control_b_w(data);
928962         break;
929963   }
930964}
r8885r8886
936970
937971WRITE8_DEVICE_HANDLER( pia6821_alt_w )
938972{
939   pia6821_w(device, ((offset << 1) & 0x02) | ((offset >> 1) & 0x01), data);
973    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
974    dev->reg_w(((offset << 1) & 0x02) | ((offset >> 1) & 0x01), data);
940975}
941976
942977
r8885r8886
946981
947982void pia6821_set_port_a_z_mask(running_device *device, UINT8 data)
948983{
949   pia6821_state *p = get_token(device);
984    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
985    dev->set_port_a_z_mask(data);
986}
950987
951   p->port_a_z_mask = data;
988void pia6821_device::set_port_a_z_mask(UINT8 data)
989{
990   m_port_a_z_mask = data;
952991}
953992
954993
r8885r8886
958997
959998READ8_DEVICE_HANDLER( pia6821_porta_r )
960999{
961   pia6821_state *p = get_token(device);
1000    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1001    return dev->porta_r();
1002}
9621003
963   return p->in_a;
1004UINT8 pia6821_device::porta_r()
1005{
1006   return m_in_a;
9641007}
9651008
9661009
r8885r8886
9701013
9711014void pia6821_set_input_a(running_device *device, UINT8 data, UINT8 z_mask)
9721015{
973   pia6821_state *p = get_token(device);
1016    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1017    dev->set_input_a(data, z_mask);
1018}
9741019
975   assert_always(p->in_a_func.read == NULL, "pia6821_porta_w() called when in_a_func implemented");
1020void pia6821_device::set_input_a(UINT8 data, UINT8 z_mask)
1021{
1022   assert_always(m_in_a_func.read == NULL, "pia6821_porta_w() called when in_a_func implemented");
9761023
977   LOG(("PIA #%s: set input port A = %02X\n", device->tag(), data));
1024   LOG(("PIA #%s: set input port A = %02X\n", tag(), data));
9781025
979   p->in_a = data;
980   p->port_a_z_mask = z_mask;
981   p->in_a_pushed = TRUE;
1026   m_in_a = data;
1027   m_port_a_z_mask = z_mask;
1028   m_in_a_pushed = TRUE;
9821029}
9831030
9841031
r8885r8886
9881035
9891036WRITE8_DEVICE_HANDLER( pia6821_porta_w )
9901037{
991   pia6821_set_input_a(device, data, 0);
1038    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1039    dev->porta_w(data);
9921040}
9931041
1042void pia6821_device::porta_w(UINT8 data)
1043{
1044   pia6821_set_input_a(this, data, 0);
1045}
9941046
1047
9951048/*-------------------------------------------------
9961049    pia6821_get_output_a
9971050-------------------------------------------------*/
9981051
9991052UINT8 pia6821_get_output_a(running_device *device)
10001053{
1001   pia6821_state *p = get_token(device);
1054    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1055    return dev->get_output_a();
1056}
10021057
1003   p->out_a_needs_pulled = FALSE;
1058UINT8 pia6821_device::get_output_a()
1059{
1060   m_out_a_needs_pulled = FALSE;
10041061
1005   return get_out_a_value(device);
1062   return get_out_a_value();
10061063}
10071064
10081065
r8885r8886
10121069
10131070READ_LINE_DEVICE_HANDLER( pia6821_ca1_r )
10141071{
1015   pia6821_state *p = get_token(device);
1072    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1073    return dev->ca1_r();
1074}
10161075
1017   return p->in_ca1;
1076UINT8 pia6821_device::ca1_r()
1077{
1078   return m_in_ca1;
10181079}
10191080
10201081
r8885r8886
10241085
10251086WRITE_LINE_DEVICE_HANDLER( pia6821_ca1_w )
10261087{
1027   pia6821_state *p = get_token(device);
1088    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1089    dev->ca1_w(state);
1090}
10281091
1029   LOG(("PIA #%s: set input CA1 = %d\n", device->tag(), state));
1092void pia6821_device::ca1_w(UINT8 state)
1093{
1094   LOG(("PIA #%s: set input CA1 = %d\n", tag(), state));
10301095
10311096   /* 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))))
1097   if((m_in_ca1 != state) && ((state && C1_LOW_TO_HIGH(m_ctl_a)) || (!state && C1_HIGH_TO_LOW(m_ctl_a))))
10341098   {
1035      LOG(("PIA #%s: CA1 triggering\n", device->tag()));
1099      LOG(("PIA #%s: CA1 triggering\n", tag()));
10361100
10371101      /* mark the IRQ */
1038      p->irq_a1 = TRUE;
1102      m_irq_a1 = TRUE;
10391103
10401104      /* update externals */
1041      update_interrupts(device);
1105      update_interrupts();
10421106
10431107      /* 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);
1108      if(C2_OUTPUT(m_ctl_a) && C2_STROBE_MODE(m_ctl_a) && STROBE_C1_RESET(m_ctl_a))
1109        {
1110         set_out_ca2(TRUE);
1111        }
10461112   }
10471113
10481114   /* set the new value for CA1 */
1049   p->in_ca1 = state;
1050   p->in_ca1_pushed = TRUE;
1115   m_in_ca1 = state;
1116   m_in_ca1_pushed = TRUE;
10511117}
10521118
10531119
r8885r8886
10571123
10581124READ_LINE_DEVICE_HANDLER( pia6821_ca2_r )
10591125{
1060   pia6821_state *p = get_token(device);
1126    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1127    return dev->ca2_r();
1128}
10611129
1062   return p->in_ca2;
1130UINT8 pia6821_device::ca2_r()
1131{
1132   return m_in_ca2;
10631133}
10641134
10651135
r8885r8886
10691139
10701140WRITE_LINE_DEVICE_HANDLER( pia6821_ca2_w )
10711141{
1072   pia6821_state *p = get_token(device);
1142    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1143    dev->ca2_w(state);
1144}
10731145
1074   LOG(("PIA #%s: set input CA2 = %d\n", device->tag(), state));
1146void pia6821_device::ca2_w(UINT8 state)
1147{
1148   LOG(("PIA #%s: set input CA2 = %d\n", tag(), state));
10751149
10761150   /* 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))))
1151   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))))
10801152   {
1081      LOG(("PIA #%s: CA2 triggering\n", device->tag()));
1153      LOG(("PIA #%s: CA2 triggering\n", tag()));
10821154
10831155      /* mark the IRQ */
1084      p->irq_a2 = TRUE;
1156      m_irq_a2 = TRUE;
10851157
10861158      /* update externals */
1087      update_interrupts(device);
1159      update_interrupts();
10881160   }
10891161
10901162   /* set the new value for CA2 */
1091   p->in_ca2 = state;
1092   p->in_ca2_pushed = TRUE;
1163   m_in_ca2 = state;
1164   m_in_ca2_pushed = TRUE;
10931165}
10941166
10951167
r8885r8886
10991171
11001172int pia6821_get_output_ca2(running_device *device)
11011173{
1102   pia6821_state *p = get_token(device);
1174    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1175    return dev->get_output_ca2();
1176}
11031177
1104   p->out_ca2_needs_pulled = FALSE;
1178int pia6821_device::get_output_ca2()
1179{
1180   m_out_ca2_needs_pulled = FALSE;
11051181
1106   return p->out_ca2;
1182   return m_out_ca2;
11071183}
11081184
11091185
r8885r8886
11151191
11161192int pia6821_get_output_ca2_z(running_device *device)
11171193{
1118   pia6821_state *p = get_token(device);
1194    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1195    return dev->get_output_ca2_z();
1196}
11191197
1120   p->out_ca2_needs_pulled = FALSE;
1198int pia6821_device::get_output_ca2_z()
1199{
1200   m_out_ca2_needs_pulled = FALSE;
11211201
11221202   // If it's an output, output the bit, if it's an input, it's
11231203   // pulled up
1124   return p->out_ca2 |
1125          C2_INPUT(p->ctl_a);
1204   return m_out_ca2 | C2_INPUT(m_ctl_a);
11261205}
11271206
11281207
r8885r8886
11321211
11331212READ8_DEVICE_HANDLER( pia6821_portb_r )
11341213{
1135   pia6821_state *p = get_token(device);
1214    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1215    return dev->portb_r();
1216}
11361217
1137   return p->in_b;
1218UINT8 pia6821_device::portb_r()
1219{
1220   return m_in_b;
11381221}
11391222
11401223
r8885r8886
11441227
11451228WRITE8_DEVICE_HANDLER( pia6821_portb_w )
11461229{
1147   pia6821_state *p = get_token(device);
1230    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1231    dev->portb_w(data);
1232}
11481233
1149   assert_always(p->in_b_func.read == NULL, "pia_set_input_b() called when in_b_func implemented");
1234void pia6821_device::portb_w(UINT8 data)
1235{
1236   assert_always(m_in_b_func.read == NULL, "pia_set_input_b() called when in_b_func implemented");
11501237
1151   LOG(("PIA #%s: set input port B = %02X\n", device->tag(), data));
1238   LOG(("PIA #%s: set input port B = %02X\n", tag(), data));
11521239
1153   p->in_b = data;
1154   p->in_b_pushed = TRUE;
1240   m_in_b = data;
1241   m_in_b_pushed = TRUE;
11551242}
11561243
11571244
r8885r8886
11611248
11621249UINT8 pia6821_get_output_b(running_device *device)
11631250{
1164   pia6821_state *p = get_token(device);
1251    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1252    return dev->get_output_b();
1253}
11651254
1166   p->out_b_needs_pulled = FALSE;
1255UINT8 pia6821_device::get_output_b()
1256{
1257   m_out_b_needs_pulled = FALSE;
11671258
1168   return get_out_b_value(device);
1259   return get_out_b_value();
11691260}
11701261
11711262
r8885r8886
11751266
11761267READ_LINE_DEVICE_HANDLER( pia6821_cb1_r )
11771268{
1178   pia6821_state *p = get_token(device);
1269    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1270    return dev->cb1_r();
1271}
11791272
1180   return p->in_cb1;
1273UINT8 pia6821_device::cb1_r()
1274{
1275   return m_in_cb1;
11811276}
11821277
11831278
r8885r8886
11871282
11881283WRITE_LINE_DEVICE_HANDLER( pia6821_cb1_w )
11891284{
1190   pia6821_state *p = get_token(device);
1285    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1286    dev->cb1_w(state);
1287}
11911288
1192   LOG(("PIA #%s: set input CB1 = %d\n", device->tag(), state));
1289void pia6821_device::cb1_w(UINT8 state)
1290{
1291   LOG(("PIA #%s: set input CB1 = %d\n", tag(), state));
11931292
11941293   /* 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))))
1294   if((m_in_cb1 != state) && ((state && C1_LOW_TO_HIGH(m_ctl_b)) || (!state && C1_HIGH_TO_LOW(m_ctl_b))))
11971295   {
1198      LOG(("PIA #%s: CB1 triggering\n", device->tag()));
1296      LOG(("PIA #%s: CB1 triggering\n", tag()));
11991297
12001298      /* mark the IRQ */
1201      p->irq_b1 = 1;
1299      m_irq_b1 = 1;
12021300
12031301      /* update externals */
1204      update_interrupts(device);
1302      update_interrupts();
12051303
12061304      /* If CB2 is configured as a write-strobe output which is reset by a CB1
12071305           transition, this reset will only happen when a read from port B implicitly
r8885r8886
12101308   }
12111309
12121310   /* set the new value for CB1 */
1213   p->in_cb1 = state;
1214   p->in_cb1_pushed = TRUE;
1311   m_in_cb1 = state;
1312   m_in_cb1_pushed = TRUE;
12151313}
12161314
12171315
r8885r8886
12211319
12221320READ_LINE_DEVICE_HANDLER( pia6821_cb2_r )
12231321{
1224   pia6821_state *p = get_token(device);
1322    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1323    return dev->cb2_r();
1324}
12251325
1226   return p->in_cb2;
1326UINT8 pia6821_device::cb2_r()
1327{
1328   return m_in_cb2;
12271329}
12281330
12291331
r8885r8886
12331335
12341336WRITE_LINE_DEVICE_HANDLER( pia6821_cb2_w )
12351337{
1236   pia6821_state *p = get_token(device);
1338    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1339    dev->cb2_w(state);
1340}
12371341
1238   LOG(("PIA #%s: set input CB2 = %d\n", device->tag(), state));
1342void pia6821_device::cb2_w(UINT8 state)
1343{
1344   LOG(("PIA #%s: set input CB2 = %d\n", tag(), state));
12391345
12401346   /* 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))))
1347   if (C2_INPUT(m_ctl_b) &&
1348      (m_in_cb2 != state) &&
1349      ((state && C2_LOW_TO_HIGH(m_ctl_b)) || (!state && C2_HIGH_TO_LOW(m_ctl_b))))
12441350   {
1245      LOG(("PIA #%s: CB2 triggering\n", device->tag()));
1351      LOG(("PIA #%s: CB2 triggering\n", tag()));
12461352
12471353      /* mark the IRQ */
1248      p->irq_b2 = 1;
1354      m_irq_b2 = 1;
12491355
12501356      /* update externals */
1251      update_interrupts(device);
1357      update_interrupts();
12521358   }
12531359
12541360   /* set the new value for CA2 */
1255   p->in_cb2 = state;
1256   p->in_cb2_pushed = TRUE;
1361   m_in_cb2 = state;
1362   m_in_cb2_pushed = TRUE;
12571363}
12581364
12591365
r8885r8886
12631369
12641370int pia6821_get_output_cb2(running_device *device)
12651371{
1266   pia6821_state *p = get_token(device);
1372    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1373    return dev->get_output_cb2();
1374}
12671375
1268   p->out_cb2_needs_pulled = FALSE;
1376int pia6821_device::get_output_cb2()
1377{
1378   m_out_cb2_needs_pulled = FALSE;
12691379
1270   return p->out_cb2;
1380   return m_out_cb2;
12711381}
12721382
12731383
r8885r8886
12771387
12781388int pia6821_get_output_cb2_z(running_device *device)
12791389{
1280   pia6821_state *p = get_token(device);
1390    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1391    return dev->get_output_cb2_z();
1392}
12811393
1282   return !C2_OUTPUT(p->ctl_b);
1394int pia6821_device::get_output_cb2_z()
1395{
1396   return !C2_OUTPUT(m_ctl_b);
12831397}
12841398
12851399
r8885r8886
12891403
12901404int pia6821_get_irq_a(running_device *device)
12911405{
1292   pia6821_state *p = get_token(device);
1406    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1407    return dev->get_irq_a();
1408}
12931409
1294   return p->irq_a_state;
1410int pia6821_device::get_irq_a()
1411{
1412   return m_irq_a_state;
12951413}
12961414
12971415
r8885r8886
13011419
13021420int pia6821_get_irq_b(running_device *device)
13031421{
1304   pia6821_state *p = get_token(device);
1422    pia6821_device *dev = reinterpret_cast<pia6821_device *>(device);
1423    return dev->get_irq_b();
1424}
13051425
1306   return p->irq_b_state;
1426int pia6821_device::get_irq_b()
1427{
1428   return m_irq_b_state;
13071429}
13081430
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);
1431const device_type PIA6821 = pia6821_device_config::static_alloc_device_config;
src/emu/machine/6532riot.c
r8885r8886
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
r8885r8886
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
r8885r8886
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
r8885r8886
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
r8885r8886
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 */
r8885r8886
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
r8885r8886
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);
r8885r8886
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
r8885r8886
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
r8885r8886
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
208152}
209153
210154
211INLINE UINT16 v_get_counter1_value(running_device *device)
155
156/***************************************************************************
157    INLINE FUNCTIONS
158***************************************************************************/
159
160attotime via6522_device::cycles_to_time(int c)
212161{
213   via6522_t *v = get_token(device);
162   return attotime_mul(ATTOTIME_IN_HZ(clock()), c);
163}
164
165
166UINT32 via6522_device::time_to_cycles(attotime t)
167{
168   return attotime_to_double(attotime_mul(t, clock()));
169}
170
171
172UINT16 via6522_device::get_counter1_value()
173{
214174   UINT16 val;
215175
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));
176    if(m_t1_active)
177    {
178        val = time_to_cycles(timer_timeleft(m_t1)) - IFR_DELAY;
220179   }
180    else
181    {
182        val = 0xffff - time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time1));
183   }
184
221185   return val;
222186}
223187
224188
225/***************************************************************************
226    IMPLEMENTATION
227***************************************************************************/
189//**************************************************************************
190//  LIVE DEVICE
191//**************************************************************************
228192
229/*-------------------------------------------------
230    DEVICE_START( via6522 )
231-------------------------------------------------*/
193//-------------------------------------------------
194//  via6522_device - constructor
195//-------------------------------------------------
232196
233static DEVICE_START( via6522 )
197via6522_device::via6522_device(running_machine &_machine, const via6522_device_config &config)
198    : device_t(_machine, config),
199      m_config(config)
234200{
235   via6522_t *v = get_token(device);
236   const via6522_interface *intf = get_interface(device);
237201
238   memset(v, 0, sizeof(*v));
202}
239203
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);
204//-------------------------------------------------
205//  device_start - device-specific startup
206//-------------------------------------------------
253207
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);
208void via6522_device::device_start()
209{
210    devcb_resolve_read8(&m_in_a_func, &m_config.m_in_a_func, this);
211    devcb_resolve_read8(&m_in_b_func, &m_config.m_in_b_func, this);
212    devcb_resolve_read_line(&m_in_ca1_func, &m_config.m_in_ca1_func, this);
213    devcb_resolve_read_line(&m_in_cb1_func, &m_config.m_in_cb1_func, this);
214    devcb_resolve_read_line(&m_in_ca2_func, &m_config.m_in_ca2_func, this);
215    devcb_resolve_read_line(&m_in_cb2_func, &m_config.m_in_cb2_func, this);
216    devcb_resolve_write8(&m_out_a_func, &m_config.m_out_a_func, this);
217    devcb_resolve_write8(&m_out_b_func, &m_config.m_out_b_func, this);
218    devcb_resolve_write_line(&m_out_ca1_func, &m_config.m_out_ca1_func, this);
219    devcb_resolve_write_line(&m_out_cb1_func, &m_config.m_out_cb1_func, this);
220    devcb_resolve_write_line(&m_out_ca2_func, &m_config.m_out_ca2_func, this);
221    devcb_resolve_write_line(&m_out_cb2_func, &m_config.m_out_cb2_func, this);
222    devcb_resolve_write_line(&m_irq_func, &m_config.m_irq_func, this);
262223
224    m_t1ll = 0xf3; /* via at 0x9110 in vic20 show these values */
225    m_t1lh = 0xb5; /* ports are not written by kernel! */
226    m_t2ll = 0xff; /* taken from vice */
227    m_t2lh = 0xff;
228    m_time2 = m_time1 = timer_get_time(&m_machine);
229    m_t1 = timer_alloc(&m_machine, t1_timeout_callback, 0);
230    m_t2 = timer_alloc(&m_machine, t2_timeout_callback, 0);
231    m_shift_timer = timer_alloc(&m_machine, shift_callback, 0);
232
263233   /* Default clock is from CPU1 */
264   if (device->clock() == 0)
265      device->set_unscaled_clock(device->machine->firstcpu->clock());
234   if (clock() == 0)
235    {
236      set_unscaled_clock(m_machine.firstcpu->clock());
237    }
266238
267239   /* 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);
240    state_save_register_device_item(this, 0, m_in_a);
241    state_save_register_device_item(this, 0, m_in_ca1);
242    state_save_register_device_item(this, 0, m_in_ca2);
243    state_save_register_device_item(this, 0, m_out_a);
244    state_save_register_device_item(this, 0, m_out_ca2);
245    state_save_register_device_item(this, 0, m_ddr_a);
246    state_save_register_device_item(this, 0, m_in_b);
247    state_save_register_device_item(this, 0, m_in_cb1);
248    state_save_register_device_item(this, 0, m_in_cb2);
249    state_save_register_device_item(this, 0, m_out_b);
250    state_save_register_device_item(this, 0, m_out_cb2);
251    state_save_register_device_item(this, 0, m_ddr_b);
252    state_save_register_device_item(this, 0, m_t1cl);
253    state_save_register_device_item(this, 0, m_t1ch);
254    state_save_register_device_item(this, 0, m_t1ll);
255    state_save_register_device_item(this, 0, m_t1lh);
256    state_save_register_device_item(this, 0, m_t2cl);
257    state_save_register_device_item(this, 0, m_t2ch);
258    state_save_register_device_item(this, 0, m_t2ll);
259    state_save_register_device_item(this, 0, m_t2lh);
260    state_save_register_device_item(this, 0, m_sr);
261    state_save_register_device_item(this, 0, m_pcr);
262    state_save_register_device_item(this, 0, m_acr);
263    state_save_register_device_item(this, 0, m_ier);
264    state_save_register_device_item(this, 0, m_ifr);
265    state_save_register_device_item(this, 0, m_t1_active);
266    state_save_register_device_item(this, 0, m_t2_active);
267    state_save_register_device_item(this, 0, m_shift_counter);
296268}
297269
298270
r8885r8886
300272    via_set_int - external interrupt check
301273-------------------------------------------------*/
302274
303static void via_set_int (running_device *device, int data)
275void via6522_device::set_int(int data)
304276{
305   via6522_t *v = get_token(device);
306
307   v->ifr |= data;
277   m_ifr |= data;
308278   if (TRACE_VIA)
309      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), v->ifr);
279    {
280      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(&m_machine), tag(), m_ifr);
281    }
310282
311   if (v->ier & v->ifr)
283   if (m_ier & m_ifr)
312284    {
313      v->ifr |= INT_ANY;
314      devcb_call_write_line(&v->irq_func, ASSERT_LINE);
285      m_ifr |= INT_ANY;
286      devcb_call_write_line(&m_irq_func, ASSERT_LINE);
315287    }
316288}
317289
r8885r8886
320292    via_clear_int - external interrupt check
321293-------------------------------------------------*/
322294
323static void via_clear_int (running_device *device, int data)
295void via6522_device::clear_int(int data)
324296{
325   via6522_t *v = get_token(device);
297   m_ifr = (m_ifr & ~data) & 0x7f;
326298
327   v->ifr = (v->ifr & ~data) & 0x7f;
328
329299   if (TRACE_VIA)
330      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), v->ifr);
300    {
301      logerror("%s:6522VIA chip %s: IFR = %02X\n", cpuexec_describe_context(&m_machine), tag(), m_ifr);
302    }
331303
332   if (v->ifr & v->ier)
333      v->ifr |= INT_ANY;
304   if (m_ifr & m_ier)
305    {
306      m_ifr |= INT_ANY;
307    }
334308   else
335309   {
336      devcb_call_write_line(&v->irq_func, CLEAR_LINE);
310      devcb_call_write_line(&m_irq_func, CLEAR_LINE);
337311   }
338312}
339313
r8885r8886
342316    via_shift
343317-------------------------------------------------*/
344318
345static void via_shift(running_device *device)
319void via6522_device::shift()
346320{
347   via6522_t *v = get_token(device);
348
349   if (SO_O2_CONTROL(v->acr))
321   if (SO_O2_CONTROL(m_acr))
350322   {
351      v->out_cb2 = (v->sr >> 7) & 1;
352      v->sr =  (v->sr << 1) | v->out_cb2;
323      m_out_cb2 = (m_sr >> 7) & 1;
324      m_sr =  (m_sr << 1) | m_out_cb2;
353325
354      devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
326      devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
355327
356      v->in_cb1=1;
328      m_in_cb1=1;
357329
358330      /* 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);
331      devcb_call_write_line(&m_out_cb1_func, 0);
332      devcb_call_write_line(&m_out_cb1_func, 1);
361333
362      v->shift_counter = (v->shift_counter + 1) % 8;
334      m_shift_counter = (m_shift_counter + 1) % 8;
363335
364      if (v->shift_counter)
365         timer_adjust_oneshot(v->shift_timer, v_cycles_to_time(device, 2), 0);
336      if (m_shift_counter)
337        {
338         timer_adjust_oneshot(m_shift_timer, cycles_to_time(2), 0);
339        }
366340      else
367341      {
368         if (!(v->ifr & INT_SR))
369            via_set_int(device, INT_SR);
342         if (!(m_ifr & INT_SR))
343            {
344            set_int(INT_SR);
345            }
370346      }
371347   }
372   if (SO_EXT_CONTROL(v->acr))
348   if (SO_EXT_CONTROL(m_acr))
373349   {
374      v->out_cb2 = (v->sr >> 7) & 1;
375      v->sr =  (v->sr << 1) | v->out_cb2;
350        m_out_cb2 = (m_sr >> 7) & 1;
351        m_sr =  (m_sr << 1) | m_out_cb2;
376352
377      devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
353        devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
378354
379      v->shift_counter = (v->shift_counter + 1) % 8;
355        m_shift_counter = (m_shift_counter + 1) % 8;
380356
381      if (v->shift_counter == 0)
357        if (m_shift_counter == 0)
382358      {
383         if (!(v->ifr & INT_SR))
384            via_set_int(device, INT_SR);
359            if (!(m_ifr & INT_SR))
360            {
361            set_int(INT_SR);
362            }
385363      }
386364   }
387   if (SI_EXT_CONTROL(v->acr))
365    if (SI_EXT_CONTROL(m_acr))
388366   {
389      if (v->in_cb2_func.read != NULL)
390         v->in_cb2 = devcb_call_read_line(&v->in_cb2_func);
367        if (m_in_cb2_func.read != NULL)
368        {
369            m_in_cb2 = devcb_call_read_line(&m_in_cb2_func);
370        }
391371
392      v->sr =  (v->sr << 1) | (v->in_cb2 & 1);
372        m_sr =  (m_sr << 1) | (m_in_cb2 & 1);
393373
394      v->shift_counter = (v->shift_counter + 1) % 8;
374        m_shift_counter = (m_shift_counter + 1) % 8;
395375
396      if (v->shift_counter == 0)
376        if (m_shift_counter == 0)
397377      {
398         if (!(v->ifr & INT_SR))
378            if (!(m_ifr & INT_SR))
399379         {
400            via_set_int(device, INT_SR);
380            set_int(INT_SR);
401381         }
402382      }
403383   }
r8885r8886
408388    TIMER_CALLBACK( via_shift_callback )
409389-------------------------------------------------*/
410390
411static TIMER_CALLBACK( via_shift_callback )
391TIMER_CALLBACK( via6522_device::shift_callback )
412392{
413   running_device *device = (running_device *)ptr;
414   via_shift(device);
393    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
394    via->shift();
415395}
416396
417397
r8885r8886
419399    TIMER_CALLBACK( via_t1_timeout )
420400-------------------------------------------------*/
421401
422static TIMER_CALLBACK( via_t1_timeout )
402TIMER_CALLBACK( via6522_device::t1_timeout_callback )
423403{
424   running_device *device = (running_device *)ptr;
425   via6522_t *v = get_token(device);
404    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
405    via->t1_timeout();
406}
426407
427   if (T1_CONTINUOUS (v->acr))
408void via6522_device::t1_timeout()
409{
410    if (T1_CONTINUOUS (m_acr))
428411    {
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);
412        if (T1_SET_PB7(m_acr))
413        {
414            m_out_b ^= 0x80;
415        }
416        timer_adjust_oneshot(m_t1, cycles_to_time(TIMER1_VALUE + IFR_DELAY), 0);
432417    }
433418   else
434419    {
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);
420        if (T1_SET_PB7(m_acr))
421        {
422            m_out_b |= 0x80;
423        }
424        m_t1_active = 0;
425        m_time1 = timer_get_time(&m_machine);
439426    }
440   if (v->ddr_b)
427    if (m_ddr_b)
441428   {
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);
429        UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
430        devcb_call_write8(&m_out_b_func, 0, write_data);
444431   }
445432
446   if (!(v->ifr & INT_T1))
447      via_set_int (device, INT_T1);
433    if (!(m_ifr & INT_T1))
434    {
435      set_int(INT_T1);
436    }
448437}
449438
450439
r8885r8886
452441    TIMER_CALLBACK( via_t2_timeout )
453442-------------------------------------------------*/
454443
455static TIMER_CALLBACK( via_t2_timeout )
444TIMER_CALLBACK( via6522_device::t2_timeout_callback )
456445{
457   running_device *device = (running_device *)ptr;
458   via6522_t *v = get_token(device);
446    via6522_device *via = reinterpret_cast<via6522_device *>(ptr);
447    via->t2_timeout();
448}
459449
460   v->t2_active = 0;
461   v->time2 = timer_get_time(device->machine);
450void via6522_device::t2_timeout()
451{
452    m_t2_active = 0;
453    m_time2 = timer_get_time(&m_machine);
462454
463   if (!(v->ifr & INT_T2))
464      via_set_int (device, INT_T2);
455    if (!(m_ifr & INT_T2))
456    {
457      set_int(INT_T2);
458    }
465459}
466460
467461
468/*-------------------------------------------------
469    DEVICE_RESET( via6522 )
470-------------------------------------------------*/
462//-------------------------------------------------
463//  device_reset - device-specific reset
464//-------------------------------------------------
471465
472static DEVICE_RESET( via6522 )
466void via6522_device::device_reset()
473467{
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;
468    m_in_a = 0;
469    m_in_ca1 = 0;
470    m_in_ca2 = 0;
471    m_out_a = 0;
472    m_out_ca2 = 0;
473    m_ddr_a = 0;
474    m_in_b = 0;
475    m_in_cb1 = 0;
476    m_in_cb2 = 0;
477    m_out_b = 0;
478    m_out_cb2 = 0;
479    m_ddr_b = 0;
487480
488   v->t1cl = 0;
489   v->t1ch = 0;
490   v->t2cl = 0;
491   v->t2ch = 0;
481    m_t1cl = 0;
482    m_t1ch = 0;
483    m_t2cl = 0;
484    m_t2ch = 0;
492485
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;
486    m_sr = 0;
487    m_pcr = 0;
488    m_acr = 0;
489    m_ier = 0;
490    m_ifr = 0;
491    m_t1_active = 0;
492    m_t2_active = 0;
493    m_shift_counter = 0;
501494}
502495
503496
r8885r8886
507500
508501READ8_DEVICE_HANDLER(via_r)
509502{
510   via6522_t *v = get_token(device);
503    via6522_device *via = reinterpret_cast<via6522_device *>(device);
504    return via->reg_r(offset);
505}
506
507UINT8 via6522_device::reg_r(UINT8 offset)
508{
511509   int val = 0;
512510
513511   offset &= 0xf;
r8885r8886
516514    {
517515    case VIA_PB:
518516      /* update the input */
519      if (PB_LATCH_ENABLE(v->acr) == 0)
517        if (PB_LATCH_ENABLE(m_acr) == 0)
520518      {
521         if (v->ddr_b != 0xff)
519         if (m_ddr_b != 0xff)
522520         {
523            if (v->in_b_func.read != NULL)
524               v->in_b = devcb_call_read8(&v->in_b_func, 0);
521            if (m_in_b_func.read != NULL)
522                {
523               m_in_b = devcb_call_read8(&m_in_b_func, 0);
524                }
525525            else
526               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
526                {
527               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
528                }
527529         }
528530      }
529531
530      CLR_PB_INT(device);
532      CLR_PB_INT();
531533
532534      /* 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));
535      if (T1_SET_PB7(m_acr))
536        {
537         val = (m_out_b & (m_ddr_b | 0x80)) | (m_in_b & ~(m_ddr_b | 0x80));
538        }
535539      else
536         val = (v->out_b & v->ddr_b) + (v->in_b & ~v->ddr_b);
540        {
541         val = (m_out_b & m_ddr_b) + (m_in_b & ~m_ddr_b);
542        }
537543      break;
538544
539545    case VIA_PA:
540546      /* update the input */
541      if (PA_LATCH_ENABLE(v->acr) == 0)
547      if (PA_LATCH_ENABLE(m_acr) == 0)
542548      {
543         if (v->ddr_a != 0xff)
549         if (m_ddr_a != 0xff)
544550         {
545            if (v->in_a_func.read != NULL)
546               v->in_a = devcb_call_read8(&v->in_a_func, 0);
551            if (m_in_a_func.read != NULL)
552                {
553               m_in_a = devcb_call_read8(&m_in_a_func, 0);
554                }
547555            else
548               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
556                {
557               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
558                }
549559         }
550560      }
551561
552562      /* combine input and output values */
553      val = (v->out_a & v->ddr_a) + (v->in_a & ~v->ddr_a);
563      val = (m_out_a & m_ddr_a) + (m_in_a & ~m_ddr_a);
554564
555      CLR_PA_INT(device);
565      CLR_PA_INT();
556566
557567      /* If CA2 is configured as output and in pulse or handshake mode,
558568           CA2 is set now */
559      if (CA2_AUTO_HS(v->pcr))
569      if (CA2_AUTO_HS(m_pcr))
560570      {
561         if (v->out_ca2)
571         if (m_out_ca2)
562572         {
563573            /* set CA2 */
564            v->out_ca2 = 0;
574            m_out_ca2 = 0;
565575
566576            /* call the CA2 output function */
567            devcb_call_write_line(&v->out_ca2_func, 0);
577            devcb_call_write_line(&m_out_ca2_func, 0);
568578         }
569579      }
570580
r8885r8886
572582
573583    case VIA_PANH:
574584      /* update the input */
575      if (PA_LATCH_ENABLE(v->acr) == 0)
585      if (PA_LATCH_ENABLE(m_acr) == 0)
576586      {
577         if (v->in_a_func.read != NULL)
578            v->in_a = devcb_call_read8(&v->in_a_func, 0);
587         if (m_in_a_func.read != NULL)
588            {
589            m_in_a = devcb_call_read8(&m_in_a_func, 0);
590            }
579591         else
580            logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
592            {
593            logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
594            }
581595      }
582596
583597      /* combine input and output values */
584      val = (v->out_a & v->ddr_a) + (v->in_a & ~v->ddr_a);
598      val = (m_out_a & m_ddr_a) + (m_in_a & ~m_ddr_a);
585599      break;
586600
587601    case VIA_DDRB:
588      val = v->ddr_b;
602      val = m_ddr_b;
589603      break;
590604
591605    case VIA_DDRA:
592      val = v->ddr_a;
606      val = m_ddr_a;
593607      break;
594608
595609    case VIA_T1CL:
596      via_clear_int (device, INT_T1);
597      val = v_get_counter1_value(device) & 0xFF;
610      clear_int(INT_T1);
611      val = get_counter1_value() & 0xFF;
598612      break;
599613
600614    case VIA_T1CH:
601      val = v_get_counter1_value(device) >> 8;
615      val = get_counter1_value() >> 8;
602616      break;
603617
604618    case VIA_T1LL:
605      val = v->t1ll;
619      val = m_t1ll;
606620      break;
607621
608622    case VIA_T1LH:
609      val = v->t1lh;
623      val = m_t1lh;
610624      break;
611625
612626    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;
627      clear_int(INT_T2);
628      if (m_t2_active)
629        {
630         val = time_to_cycles(timer_timeleft(m_t2)) & 0xff;
631        }
616632      else
617633      {
618         if (T2_COUNT_PB6(v->acr))
619            val = v->t2cl;
634         if (T2_COUNT_PB6(m_acr))
635            {
636            val = m_t2cl;
637            }
620638         else
621            val = (0x10000- (v_time_to_cycles(device, attotime_sub(timer_get_time(device->machine), v->time2)) & 0xffff) - 1) & 0xff;
639            {
640            val = (0x10000 - (time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time2)) & 0xffff) - 1) & 0xff;
641            }
622642      }
623643      break;
624644
625645    case VIA_T2CH:
626      if (v->t2_active)
627         val = v_time_to_cycles(device, timer_timeleft(v->t2)) >> 8;
646      if (m_t2_active)
647        {
648         val = time_to_cycles(timer_timeleft(m_t2)) >> 8;
649        }
628650      else
629651      {
630         if (T2_COUNT_PB6(v->acr))
631            val = v->t2ch;
652         if (T2_COUNT_PB6(m_acr))
653            {
654            val = m_t2ch;
655            }
632656         else
633            val = (0x10000- (v_time_to_cycles(device, attotime_sub(timer_get_time(device->machine), v->time2)) & 0xffff) - 1) >> 8;
657            {
658            val = (0x10000 - (time_to_cycles(attotime_sub(timer_get_time(&m_machine), m_time2)) & 0xffff) - 1) >> 8;
659            }
634660      }
635661      break;
636662
637663    case VIA_SR:
638      val = v->sr;
639      via_clear_int(device, INT_SR);
640      if (SO_O2_CONTROL(v->acr))
664      val = m_sr;
665      clear_int(INT_SR);
666      if (SO_O2_CONTROL(m_acr))
641667      {
642         v->shift_counter=0;
643         timer_adjust_oneshot(v->shift_timer, v_cycles_to_time(device, 2), 0);
668         m_shift_counter=0;
669         timer_adjust_oneshot(m_shift_timer, cycles_to_time(2), 0);
644670      }
645671      break;
646672
647673    case VIA_PCR:
648      val = v->pcr;
674      val = m_pcr;
649675      break;
650676
651677    case VIA_ACR:
652      val = v->acr;
678      val = m_acr;
653679      break;
654680
655681    case VIA_IER:
656      val = v->ier | 0x80;
682      val = m_ier | 0x80;
657683      break;
658684
659685    case VIA_IFR:
660      val = v->ifr;
686      val = m_ifr;
661687      break;
662688    }
663689   return val;
r8885r8886
670696
671697WRITE8_DEVICE_HANDLER(via_w)
672698{
673   via6522_t *v = get_token(device);
699    via6522_device *via = reinterpret_cast<via6522_device *>(device);
700    via->reg_w(offset, data);
701}
674702
703void via6522_device::reg_w(UINT8 offset, UINT8 data)
704{
675705   offset &=0x0f;
676706
677707   switch (offset)
678708    {
679709    case VIA_PB:
680      if (T1_SET_PB7(v->acr))
681         v->out_b = (v->out_b & 0x80) | (data  & 0x7f);
710      if (T1_SET_PB7(m_acr))
711         m_out_b = (m_out_b & 0x80) | (data  & 0x7f);
682712      else
683         v->out_b = data;
713         m_out_b = data;
684714
685      if (v->ddr_b)
715      if (m_ddr_b)
686716      {
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);
717         UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
718         devcb_call_write8(&m_out_b_func, 0, write_data);
689719      }
690720
691      CLR_PB_INT(device);
721      CLR_PB_INT();
692722
693723      /* If CB2 is configured as output and in pulse or handshake mode,
694724           CB2 is set now */
695      if (CB2_AUTO_HS(v->pcr))
725      if (CB2_AUTO_HS(m_pcr))
696726      {
697         if (v->out_cb2)
727         if (m_out_cb2)
698728         {
699729            /* set CB2 */
700            v->out_cb2 = 0;
730            m_out_cb2 = 0;
701731
702732            /* call the CB2 output function */
703            devcb_call_write_line(&v->out_cb2_func, 0);
733            devcb_call_write_line(&m_out_cb2_func, 0);
704734         }
705735      }
706736      break;
707737
708738    case VIA_PA:
709      v->out_a = data;
739      m_out_a = data;
710740
711      if (v->ddr_a)
741      if (m_ddr_a)
712742      {
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);
743         UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
744         devcb_call_write8(&m_out_a_func, 0, write_data);
715745      }
716746
717      CLR_PA_INT(device);
747      CLR_PA_INT();
718748
719749      /* If CA2 is configured as output and in pulse or handshake mode,
720750           CA2 is set now */
721      if (CA2_PULSE_OUTPUT(v->pcr))
751      if (CA2_PULSE_OUTPUT(m_pcr))
722752      {
723753         /* 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);
754         devcb_call_write_line(&m_out_ca2_func, 0);
755         devcb_call_write_line(&m_out_ca2_func, 1);
726756
727757         /* set CA2 (shouldn't be needed) */
728         v->out_ca2 = 1;
758         m_out_ca2 = 1;
729759      }
730      else if (CA2_AUTO_HS(v->pcr))
760      else if (CA2_AUTO_HS(m_pcr))
731761      {
732         if (v->out_ca2)
762         if (m_out_ca2)
733763         {
734764            /* set CA2 */
735            v->out_ca2 = 0;
765            m_out_ca2 = 0;
736766
737767            /* call the CA2 output function */
738            devcb_call_write_line(&v->out_ca2_func, 0);
768            devcb_call_write_line(&m_out_ca2_func, 0);
739769         }
740770      }
741771
742772      break;
743773
744774    case VIA_PANH:
745      v->out_a = data;
775      m_out_a = data;
746776
747      if (v->ddr_a)
777      if (m_ddr_a)
748778      {
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);
779         UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
780         devcb_call_write8(&m_out_a_func, 0, write_data);
751781      }
752782
753783      break;
754784
755785    case VIA_DDRB:
756786       /* EHC 03/04/2000 - If data direction changed, present output on the lines */
757       if ( data != v->ddr_b )
787       if ( data != m_ddr_b )
758788       {
759         v->ddr_b = data;
789         m_ddr_b = data;
760790
761         //if (v->ddr_b)
791         //if (m_ddr_b)
762792         {
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);
793            UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
794            devcb_call_write8(&m_out_b_func, 0, write_data);
765795         }
766796      }
767797      break;
768798
769799    case VIA_DDRA:
770800       /* EHC 03/04/2000 - If data direction changed, present output on the lines */
771       if ( data != v->ddr_a )
801       if ( data != m_ddr_a )
772802       {
773         v->ddr_a = data;
803         m_ddr_a = data;
774804
775         //if (v->ddr_a)
805         //if (m_ddr_a)
776806         {
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);
807            UINT8 write_data = (m_out_a & m_ddr_a) | (m_ddr_a ^ 0xff);
808            devcb_call_write8(&m_out_a_func, 0, write_data);
779809         }
780810      }
781811      break;
782812
783813    case VIA_T1CL:
784814    case VIA_T1LL:
785      v->t1ll = data;
815      m_t1ll = data;
786816      break;
787817
788818   case VIA_T1LH:
789       v->t1lh = data;
790       via_clear_int (device, INT_T1);
819       m_t1lh = data;
820       clear_int(INT_T1);
791821       break;
792822
793823    case VIA_T1CH:
794      v->t1ch = v->t1lh = data;
795      v->t1cl = v->t1ll;
824      m_t1ch = m_t1lh = data;
825      m_t1cl = m_t1ll;
796826
797      via_clear_int (device, INT_T1);
827      clear_int(INT_T1);
798828
799      if (T1_SET_PB7(v->acr))
829      if (T1_SET_PB7(m_acr))
800830      {
801         v->out_b &= 0x7f;
831         m_out_b &= 0x7f;
802832
803         //if (v->ddr_b)
833         //if (m_ddr_b)
804834         {
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);
835            UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
836            devcb_call_write8(&m_out_b_func, 0, write_data);
807837         }
808838      }
809      timer_adjust_oneshot(v->t1, v_cycles_to_time(device, TIMER1_VALUE(v) + IFR_DELAY), 0);
810      v->t1_active = 1;
839      timer_adjust_oneshot(m_t1, cycles_to_time(TIMER1_VALUE + IFR_DELAY), 0);
840      m_t1_active = 1;
811841      break;
812842
813843    case VIA_T2CL:
814      v->t2ll = data;
844      m_t2ll = data;
815845      break;
816846
817847    case VIA_T2CH:
818      v->t2ch = v->t2lh = data;
819      v->t2cl = v->t2ll;
848      m_t2ch = m_t2lh = data;
849      m_t2cl = m_t2ll;
820850
821      via_clear_int (device, INT_T2);
851      clear_int(INT_T2);
822852
823      if (!T2_COUNT_PB6(v->acr))
853      if (!T2_COUNT_PB6(m_acr))
824854      {
825         timer_adjust_oneshot(v->t2, v_cycles_to_time(device, TIMER2_VALUE(v) + IFR_DELAY), 0);
826         v->t2_active = 1;
855         timer_adjust_oneshot(m_t2, cycles_to_time(TIMER2_VALUE + IFR_DELAY), 0);
856         m_t2_active = 1;
827857      }
828858      else
829859      {
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);
860         timer_adjust_oneshot(m_t2, cycles_to_time(TIMER2_VALUE), 0);
861         m_t2_active = 1;
862         m_time2 = timer_get_time(&m_machine);
833863      }
834864      break;
835865
836866    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))
867      m_sr = data;
868      m_shift_counter=0;
869      clear_int(INT_SR);
870      if (SO_O2_CONTROL(m_acr))
841871      {
842         timer_set(device->machine, v_cycles_to_time(device, 2), (void *) device, 0, via_shift_callback);
872         timer_set(&m_machine, cycles_to_time(2), (void *)this, 0, shift_callback);
843873      }
844874      break;
845875
846876    case VIA_PCR:
847      v->pcr = data;
877      m_pcr = data;
848878
849879      if (TRACE_VIA)
850         logerror("%s:6522VIA chip %s: PCR = %02X\n", cpuexec_describe_context(device->machine), device->tag(), data);
880        {
881         logerror("%s:6522VIA chip %s: PCR = %02X\n", cpuexec_describe_context(&m_machine), tag(), data);
882        }
851883
852      if (CA2_FIX_OUTPUT(data) && CA2_OUTPUT_LEVEL(data) ^ v->out_ca2)
884      if (CA2_FIX_OUTPUT(data) && CA2_OUTPUT_LEVEL(data) ^ m_out_ca2)
853885      {
854         v->out_ca2 = CA2_OUTPUT_LEVEL(data);
855         devcb_call_write_line(&v->out_ca2_func, v->out_ca2);
886         m_out_ca2 = CA2_OUTPUT_LEVEL(data);
887         devcb_call_write_line(&m_out_ca2_func, m_out_ca2);
856888      }
857889
858      if (CB2_FIX_OUTPUT(data) && CB2_OUTPUT_LEVEL(data) ^ v->out_cb2)
890      if (CB2_FIX_OUTPUT(data) && CB2_OUTPUT_LEVEL(data) ^ m_out_cb2)
859891      {
860         v->out_cb2 = CB2_OUTPUT_LEVEL(data);
861         devcb_call_write_line(&v->out_cb2_func, v->out_cb2);
892         m_out_cb2 = CB2_OUTPUT_LEVEL(data);
893         devcb_call_write_line(&m_out_cb2_func, m_out_cb2);
862894      }
863895      break;
864896
865897    case VIA_ACR:
866898      {
867         UINT16 counter1 = v_get_counter1_value(device);
868         v->acr = data;
869         if (T1_SET_PB7(v->acr))
899         UINT16 counter1 = get_counter1_value();
900         m_acr = data;
901         if (T1_SET_PB7(m_acr))
870902         {
871            if (v->t1_active)
872               v->out_b &= ~0x80;
903            if (m_t1_active)
904                {
905               m_out_b &= ~0x80;
906                }
873907            else
874               v->out_b |= 0x80;
908                {
909               m_out_b |= 0x80;
910                }
875911
876            //if (v->ddr_b)
912            //if (m_ddr_b)
877913            {
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);
914               UINT8 write_data = (m_out_b & m_ddr_b) | (m_ddr_b ^ 0xff);
915               devcb_call_write8(&m_out_b_func, 0, write_data);
880916            }
881917         }
882918         if (T1_CONTINUOUS(data))
883919         {
884            timer_adjust_oneshot(v->t1, v_cycles_to_time(device, counter1 + IFR_DELAY), 0);
885            v->t1_active = 1;
920            timer_adjust_oneshot(m_t1, cycles_to_time(counter1 + IFR_DELAY), 0);
921            m_t1_active = 1;
886922         }
887923      }
888924      break;
889925
890926   case VIA_IER:
891927      if (data & 0x80)
892         v->ier |= data & 0x7f;
928        {
929         m_ier |= data & 0x7f;
930        }
893931      else
894         v->ier &= ~(data & 0x7f);
932        {
933         m_ier &= ~(data & 0x7f);
934        }
895935
896      if (v->ifr & INT_ANY)
936      if (m_ifr & INT_ANY)
897937      {
898         if (((v->ifr & v->ier) & 0x7f) == 0)
938         if (((m_ifr & m_ier) & 0x7f) == 0)
899939         {
900            v->ifr &= ~INT_ANY;
901            devcb_call_write_line(&v->irq_func, CLEAR_LINE);
940            m_ifr &= ~INT_ANY;
941            devcb_call_write_line(&m_irq_func, CLEAR_LINE);
902942         }
903943      }
904944      else
905945      {
906         if ((v->ier & v->ifr) & 0x7f)
946         if ((m_ier & m_ifr) & 0x7f)
907947         {
908            v->ifr |= INT_ANY;
909            devcb_call_write_line(&v->irq_func, ASSERT_LINE);
948            m_ifr |= INT_ANY;
949            devcb_call_write_line(&m_irq_func, ASSERT_LINE);
910950         }
911951      }
912952      break;
913953
914954   case VIA_IFR:
915955      if (data & INT_ANY)
956        {
916957         data = 0x7f;
917      via_clear_int (device, data);
958        }
959      clear_int(data);
918960      break;
919961    }
920962}
921963
922
923964/*-------------------------------------------------
924    via_porta_w - interface setting VIA port
925    A input
965    via_ca1_r - interface returning VIA port
966    CA1 input
926967-------------------------------------------------*/
927968
928WRITE8_DEVICE_HANDLER(via_porta_w)
969READ_LINE_DEVICE_HANDLER(via_ca1_r)
929970{
930   via6522_t *v = get_token(device);
931
932   /* set the input, what could be easier? */
933   v->in_a = data;
971    via6522_device *via = reinterpret_cast<via6522_device *>(device);
972    return via->ca1_r();
934973}
935974
936975
976
937977/*-------------------------------------------------
938    via_ca1_r - interface retrieving VIA port
939    CA1 input
978    via_ca2_r - interface returning VIA port
979    CA2 input
940980-------------------------------------------------*/
941981
942READ_LINE_DEVICE_HANDLER(via_ca1_r)
982READ_LINE_DEVICE_HANDLER(via_ca2_r)
943983{
944   via6522_t *v = get_token(device);
945   return v->in_ca1;
984    via6522_device *via = reinterpret_cast<via6522_device *>(device);
985    return via->ca2_r();
946986}
947987
948988
989
949990/*-------------------------------------------------
950991    via_ca1_w - interface setting VIA port
951992    CA1 input
r8885r8886
953994
954995WRITE_LINE_DEVICE_HANDLER(via_ca1_w)
955996{
956   via6522_t *v = get_token(device);
997    via6522_device *via = reinterpret_cast<via6522_device *>(device);
998    via->ca1_w(state);
999}
9571000
1001void via6522_device::ca1_w(UINT8 state)
1002{
9581003   /* handle the active transition */
959   if (state != v->in_ca1)
1004   if (state != m_in_ca1)
9601005    {
9611006      if (TRACE_VIA)
962         logerror("%s:6522VIA chip %s: CA1 = %02X\n", cpuexec_describe_context(device->machine), device->tag(), state);
1007         logerror("%s:6522VIA chip %s: CA1 = %02X\n", cpuexec_describe_context(&m_machine), tag(), state);
9631008
964      if ((CA1_LOW_TO_HIGH(v->pcr) && state) || (CA1_HIGH_TO_LOW(v->pcr) && !state))
1009      if ((CA1_LOW_TO_HIGH(m_pcr) && state) || (CA1_HIGH_TO_LOW(m_pcr) && !state))
9651010      {
966         if (PA_LATCH_ENABLE(v->acr))
1011         if (PA_LATCH_ENABLE(m_acr))
9671012         {
968            if (v->in_a_func.read != NULL)
969               v->in_a = devcb_call_read8(&v->in_a_func, 0);
1013            if (m_in_a_func.read != NULL)
1014                {
1015               m_in_a = devcb_call_read8(&m_in_a_func, 0);
1016                }
9701017            else
971               logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
1018                {
1019                    logerror("%s:6522VIA chip %s: Port A is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
1020                }
9721021         }
9731022
974         via_set_int (device, INT_CA1);
1023         set_int(INT_CA1);
9751024
9761025         /* CA2 is configured as output and in pulse or handshake mode,
9771026               CA2 is cleared now */
978         if (CA2_AUTO_HS(v->pcr))
1027         if (CA2_AUTO_HS(m_pcr))
9791028         {
980            if (!v->out_ca2)
1029            if (!m_out_ca2)
9811030            {
9821031               /* clear CA2 */
983               v->out_ca2 = 1;
1032               m_out_ca2 = 1;
9841033
9851034               /* call the CA2 output function */
986               devcb_call_write_line(&v->out_ca2_func, 1);
1035               devcb_call_write_line(&m_out_ca2_func, 1);
9871036            }
9881037         }
9891038      }
9901039
991      v->in_ca1 = state;
1040      m_in_ca1 = state;
9921041    }
9931042}
9941043
9951044
996/*-------------------------------------------------
997    via_ca2_r - interface retrieving VIA port
998    CA2 input
999-------------------------------------------------*/
10001045
1001READ_LINE_DEVICE_HANDLER(via_ca2_r)
1002{
1003   via6522_t *v = get_token(device);
1004   return v->in_ca2;
1005}
1006
1007
10081046/*-------------------------------------------------
10091047    via_ca2_w - interface setting VIA port
10101048    CA2 input
r8885r8886
10121050
10131051WRITE_LINE_DEVICE_HANDLER(via_ca2_w)
10141052{
1015   via6522_t *v = get_token(device);
1053    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1054    via->ca2_w(state);
1055}
10161056
1057void via6522_device::ca2_w(UINT8 state)
1058{
10171059   /* CA2 is in input mode */
1018   if (CA2_INPUT(v->pcr))
1060   if (CA2_INPUT(m_pcr))
10191061    {
10201062      /* the new state has caused a transition */
1021      if (v->in_ca2 != state)
1063      if (m_in_ca2 != state)
10221064      {
10231065         /* handle the active transition */
1024         if ((state && CA2_LOW_TO_HIGH(v->pcr)) || (!state && CA2_HIGH_TO_LOW(v->pcr)))
1066         if ((state && CA2_LOW_TO_HIGH(m_pcr)) || (!state && CA2_HIGH_TO_LOW(m_pcr)))
10251067         {
10261068            /* mark the IRQ */
1027            via_set_int (device, INT_CA2);
1069            set_int(INT_CA2);
10281070         }
10291071         /* set the new value for CA2 */
1030         v->in_ca2 = state;
1072         m_in_ca2 = state;
10311073      }
10321074    }
1075}
10331076
10341077
1035}
10361078
1037
10381079/*-------------------------------------------------
1039    via_portb_r - interface retrieving VIA port
1040    B input
1080    via_cb1_r - interface returning VIA port
1081    CB1 input
10411082-------------------------------------------------*/
10421083
1043READ8_DEVICE_HANDLER(via_portb_r)
1084READ_LINE_DEVICE_HANDLER(via_cb1_r)
10441085{
1045   via6522_t *v = get_token(device);
1046   return v->in_b;
1086    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1087    return via->cb1_r();
10471088}
10481089
10491090
1091
10501092/*-------------------------------------------------
1051    via_portb_w - interface setting VIA port
1052    B input
1093    via_cb2_r - interface returning VIA port
1094    CB2 input
10531095-------------------------------------------------*/
10541096
1055WRITE8_DEVICE_HANDLER(via_portb_w)
1097READ_LINE_DEVICE_HANDLER(via_cb2_r)
10561098{
1057   via6522_t *v = get_token(device);
1058
1059   /* set the input, what could be easier? */
1060   v->in_b = data;
1099    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1100    return via->cb2_r();
10611101}
10621102
10631103
1104
10641105/*-------------------------------------------------
1065    via_cb1_r - interface retrieving VIA port
1106    via_cb1_w - interface setting VIA port
10661107    CB1 input
10671108-------------------------------------------------*/
10681109
1069READ_LINE_DEVICE_HANDLER(via_cb1_r)
1110WRITE_LINE_DEVICE_HANDLER( via_cb1_w )
10701111{
1071   via6522_t *v = get_token(device);
1072   return v->in_cb1;
1112    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1113    via->cb1_w(state);
10731114}
10741115
1075
1076/*-------------------------------------------------
1077    via_cb1_w - interface setting VIA port
1078    CB1 input
1079-------------------------------------------------*/
1080
1081WRITE_LINE_DEVICE_HANDLER(via_cb1_w)
1116void via6522_device::cb1_w(UINT8 state)
10821117{
1083   via6522_t *v = get_token(device);
1084
10851118   /* handle the active transition */
1086   if (state != v->in_cb1)
1119   if (state != m_in_cb1)
10871120    {
1088      if ((CB1_LOW_TO_HIGH(v->pcr) && state) || (CB1_HIGH_TO_LOW(v->pcr) && !state))
1121      if ((CB1_LOW_TO_HIGH(m_pcr) && state) || (CB1_HIGH_TO_LOW(m_pcr) && !state))
10891122      {
1090         if (PB_LATCH_ENABLE(v->acr))
1123         if (PB_LATCH_ENABLE(m_acr))
10911124         {
1092            if (v->in_b_func.read != NULL)
1093               v->in_b = devcb_call_read8(&v->in_b_func, 0);
1125            if (m_in_b_func.read != NULL)
1126                {
1127               m_in_b = devcb_call_read8(&m_in_b_func, 0);
1128                }
10941129            else
1095               logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(device->machine), device->tag());
1130                {
1131                    logerror("%s:6522VIA chip %s: Port B is being read but has no handler\n", cpuexec_describe_context(&m_machine), tag());
1132                }
10961133         }
1097         if (SO_EXT_CONTROL(v->acr) || SI_EXT_CONTROL(v->acr))
1098            via_shift (device);
1134         if (SO_EXT_CONTROL(m_acr) || SI_EXT_CONTROL(m_acr))
1135            {
1136            shift();
1137            }
10991138
1100         via_set_int (device, INT_CB1);
1139         set_int(INT_CB1);
11011140
11021141         /* CB2 is configured as output and in pulse or handshake mode,
11031142               CB2 is cleared now */
1104         if (CB2_AUTO_HS(v->pcr))
1143         if (CB2_AUTO_HS(m_pcr))
11051144         {
1106            if (!v->out_cb2)
1145            if (!m_out_cb2)
11071146            {
11081147               /* clear CB2 */
1109               v->out_cb2 = 1;
1148               m_out_cb2 = 1;
11101149
11111150               /* call the CB2 output function */
1112               devcb_call_write_line(&v->out_cb2_func, 1);
1151               devcb_call_write_line(&m_out_cb2_func, 1);
11131152            }
11141153         }
11151154      }
1116      v->in_cb1 = state;
1155      m_in_cb1 = state;
11171156    }
11181157}
11191158
11201159
11211160/*-------------------------------------------------
1122    via_cb2_r - interface retrieving VIA port
1161    via_cb2_w - interface setting VIA port
11231162    CB2 input
11241163-------------------------------------------------*/
11251164
1126READ_LINE_DEVICE_HANDLER(via_cb2_r)
1165WRITE_LINE_DEVICE_HANDLER(via_cb2_w)
11271166{
1128   via6522_t *v = get_token(device);
1129   return v->in_cb2;
1167    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1168    via->cb2_w(state);
11301169}
11311170
1132
1133/*-------------------------------------------------
1134    via_cb2_w - interface setting VIA port
1135    CB2 input
1136-------------------------------------------------*/
1137
1138WRITE_LINE_DEVICE_HANDLER(via_cb2_w)
1171void via6522_device::cb2_w(UINT8 state)
11391172{
1140   via6522_t *v = get_token(device);
1141
11421173   /* CB2 is in input mode */
1143   if (CB2_INPUT(v->pcr))
1174   if (CB2_INPUT(m_pcr))
11441175    {
11451176      /* the new state has caused a transition */
1146      if (v->in_cb2 != state)
1177      if (m_in_cb2 != state)
11471178      {
11481179         /* handle the active transition */
1149         if ((state && CB2_LOW_TO_HIGH(v->pcr)) || (!state && CB2_HIGH_TO_LOW(v->pcr)))
1180         if ((state && CB2_LOW_TO_HIGH(m_pcr)) || (!state && CB2_HIGH_TO_LOW(m_pcr)))
11501181         {
11511182            /* mark the IRQ */
1152            via_set_int (device, INT_CB2);
1183            set_int(INT_CB2);
11531184         }
11541185         /* set the new value for CB2 */
1155         v->in_cb2 = state;
1186         m_in_cb2 = state;
11561187      }
11571188    }
11581189}
11591190
1191/*-------------------------------------------------
1192    via_porta_w - interface writing VIA port A
1193-------------------------------------------------*/
11601194
1195WRITE8_DEVICE_HANDLER(via_porta_w)
1196{
1197    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1198    via->porta_w(data);
1199}
1200
11611201/*-------------------------------------------------
1162    DEVICE_GET_INFO( via6522 )
1202    via_portb_w - interface writing VIA port B
11631203-------------------------------------------------*/
11641204
1165DEVICE_GET_INFO(via6522)
1205WRITE8_DEVICE_HANDLER(via_portb_w)
11661206{
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;
1207    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1208    via->portb_w(data);
1209}
11721210
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;
1211/*-------------------------------------------------
1212    via_portb_r - interface reading VIA port B
1213-------------------------------------------------*/
11771214
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   }
1215READ8_DEVICE_HANDLER(via_portb_w)
1216{
1217    via6522_device *via = reinterpret_cast<via6522_device *>(device);
1218    return via->portb_r();
11851219}
11861220
1187
1188DEFINE_LEGACY_DEVICE(VIA6522, via6522);
1221const device_type VIA6522 = via6522_device_config::static_alloc_device_config;
src/emu/machine/6821pia.h
r8885r8886
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***************************************************************************/
r8885r8886
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
r8885r8886
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 );
r8885r8886
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
r8885r8886
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
r8885r8886
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
r8885r8886
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//  LIVE DEVICE
81//**************************************************************************
82
83//-------------------------------------------------
84//  ttl7474_device - constructor
85//-------------------------------------------------
86
87ttl7474_device::ttl7474_device(running_machine &_machine, const ttl7474_device_config &config)
88    : device_t(_machine, config),
89      m_config(config)
90{
91    init();
7592}
7693
94//-------------------------------------------------
95//  device_start - device-specific startup
96//-------------------------------------------------
7797
78static void ttl7474_update(ttl7474_state *state)
98void ttl7474_device::device_start()
7999{
80   if (!state->preset && state->clear)           /* line 1 in truth table */
100    register_globals();
101
102    devcb_resolve_write_line(&m_output_cb, &m_config.m_output_cb, this);
103    devcb_resolve_write_line(&m_comp_output_cb, &m_config.m_comp_output_cb, this);
104}
105
106//-------------------------------------------------
107//  device_reset - device-specific reset
108//-------------------------------------------------
109
110void ttl7474_device::device_reset()
111{
112    init();
113}
114
115void ttl7474_device::update()
116{
117    if (!m_preset && m_clear)             /* line 1 in truth table */
81118   {
82      state->output    = 1;
83      state->output_comp = 0;
119        m_output    = 1;
120        m_output_comp = 0;
84121   }
85   else if (state->preset && !state->clear)     /* line 2 in truth table */
122    else if (m_preset && !m_clear)      /* line 2 in truth table */
86123   {
87      state->output    = 0;
88      state->output_comp = 1;
124        m_output    = 0;
125        m_output_comp = 1;
89126   }
90   else if (!state->preset && !state->clear)     /* line 3 in truth table */
127    else if (!m_preset && !m_clear)     /* line 3 in truth table */
91128   {
92      state->output    = 1;
93      state->output_comp = 1;
129        m_output    = 1;
130        m_output_comp = 1;
94131   }
95   else if (!state->last_clock && state->clock)  /* line 4 in truth table */
132    else if (!m_last_clock && m_clock)  /* line 4 in truth table */
96133   {
97      state->output    =  state->d;
98      state->output_comp = !state->d;
134        m_output    =  m_d;
135        m_output_comp = !m_d;
99136   }
100137
101   state->last_clock = state->clock;
138    m_last_clock = m_clock;
102139
103140
104141   /* call callback if any of the outputs changed */
105   if (state->output != state->last_output)
142    if (m_output != m_last_output)
106143   {
107      state->last_output = state->output;
108      if (state->output_cb.write != NULL)
109         devcb_call_write_line(&state->output_cb, state->output);
144        m_last_output = m_output;
145        if (m_output_cb.write != NULL)
146            devcb_call_write_line(&m_output_cb, m_output);
110147   }
111148   /* call callback if any of the outputs changed */
112   if (state->output_comp != state->last_output_comp)
149    if (m_output_comp != m_last_output_comp)
113150   {
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);
151        m_last_output_comp = m_output_comp;
152        if (m_comp_output_cb.write != NULL)
153            devcb_call_write_line(&m_comp_output_cb, m_output_comp);
117154   }
118155}
119156
120157
121158WRITE_LINE_DEVICE_HANDLER( ttl7474_clear_w )
122159{
123   ttl7474_state *dev_state = get_safe_token(device);
124   dev_state->clear = state & 1;
125   ttl7474_update(dev_state);
160    downcast<ttl7474_device *>(device)->clear_w(state);
126161}
127162
163void ttl7474_device::clear_w(UINT8 state)
164{
165    m_clear = state & 1;
166   update();
167}
168
128169WRITE_LINE_DEVICE_HANDLER( ttl7474_preset_w )
129170{
130   ttl7474_state *dev_state = get_safe_token(device);
131   dev_state->preset = state & 1;
132   ttl7474_update(dev_state);
171    downcast<ttl7474_device *>(device)->preset_w(state);
133172}
134173
174void ttl7474_device::preset_w(UINT8 state)
175{
176    m_preset = state & 1;
177   update();
178}
179
135180WRITE_LINE_DEVICE_HANDLER( ttl7474_clock_w )
136181{
137   ttl7474_state *dev_state = get_safe_token(device);
138   dev_state->clock = state & 1;
139   ttl7474_update(dev_state);
182    downcast<ttl7474_device *>(device)->clock_w(state);
140183}
141184
185void ttl7474_device::clock_w(UINT8 state)
186{
187    m_clock = state & 1;
188   update();
189}
190
142191WRITE_LINE_DEVICE_HANDLER( ttl7474_d_w )
143192{
144   ttl7474_state *dev_state = get_safe_token(device);
145   dev_state->d = state & 1;
146   ttl7474_update(dev_state);
193    downcast<ttl7474_device *>(device)->d_w(state);
147194}
148195
196void ttl7474_device::d_w(UINT8 state)
197{
198    m_d = state & 1;
199   update();
200}
149201
202
150203READ_LINE_DEVICE_HANDLER( ttl7474_output_r )
151204{
152   ttl7474_state *dev_state = get_safe_token(device);
153   return dev_state->output;
205    return downcast<ttl7474_device *>(device)->output_r();
154206}
155207
208UINT8 ttl7474_device::output_r()
209{
210    return m_output;
211}
212
156213READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r )
157214{
158   ttl7474_state *dev_state = get_safe_token(device);
159   return dev_state->output_comp;
215    return downcast<ttl7474_device *>(device)->output_comp_r();
160216}
161217
218UINT8 ttl7474_device::output_comp_r()
219{
220    return m_output_comp;
221}
162222
163static DEVICE_START( ttl7474 )
223void ttl7474_device::init()
164224{
165   ttl7474_config *config = (ttl7474_config *)downcast<const legacy_device_config_base &>(device->baseconfig()).inline_config();
166   ttl7474_state *state = get_safe_token(device);
225    m_clear = 1;
226    m_preset = 1;
227    m_clock = 1;
228    m_d = 1;
167229
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);
170
171   state->device = device;
172
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);
230    m_output = -1;
231    m_last_clock = 1;
232    m_last_output = -1;
182233}
183234
184
185static DEVICE_RESET( ttl7474 )
235void ttl7474_device::register_globals()
186236{
187   ttl7474_state *state = get_safe_token(device);
188
189   /* all inputs are open first */
190   state->clear = 1;
191   state->preset = 1;
192   state->clock = 1;
193   state->d = 1;
194
195   state->output = -1;
196   state->last_clock = 1;
197   state->last_output = -1;
198   state->last_output_comp = -1;
237    state_save_register_device_item(this, 0, m_clear);
238    state_save_register_device_item(this, 0, m_preset);
239    state_save_register_device_item(this, 0, m_clock);
240    state_save_register_device_item(this, 0, m_d);
241    state_save_register_device_item(this, 0, m_output);
242    state_save_register_device_item(this, 0, m_output_comp);
243    state_save_register_device_item(this, 0, m_last_clock);
244    state_save_register_device_item(this, 0, m_last_output);
245    state_save_register_device_item(this, 0, m_last_output_comp);
199246}
200247
201
202static const char DEVTEMPLATE_SOURCE[] = __FILE__;
203
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"
209
210DEFINE_LEGACY_DEVICE(TTL7474, ttl7474);
248const device_type MACHINE_TTL7474 = ttl7474_device_config::static_alloc_device_config;
src/emu/machine/7474.h
r8885r8886
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_OUTPUT_CB(_output_cb) \
56    MDRV_7474_COMP_OUTPUT_CB(_comp_output_cb)
57
58#define MDRV_7474_REPLACE(_tag, _target_tag, _output_cb, _comp_output_cb) \
59    MDRV_DEVICE_REPLACE(_tag, TTL7474, 0) \
60    MDRV_7474_OUTPUT_CB(_output_cb) \
61    MDRV_7474_COMP_OUTPUT_CB(_comp_output_cb)
62
63#define MDRV_7474_OUTPUT_CB(_cb) \
64    MDRV_DEVICE_INLINE_DATAPTR(ttl7474_device_config::INLINE_OUTPUT_CB, _cb)
65
66#define MDRV_7474_COMP_OUTPUT_CB(_cb) \
67    MDRV_DEVICE_INLINE_DATAPTR(ttl7474_device_config::INLINE_COMP_OUTPUT_CB, _cb)
68
69
70
71//**************************************************************************
72//  TYPE DEFINITIONS
73//**************************************************************************
74
75// ======================> ttl7474_device_config
76
77class ttl7474_device_config :  public device_config
4878{
49   devcb_write_line output_cb;
50   devcb_write_line comp_output_cb;
79    friend class ttl7474_device;
80
81    // construction/destruction
82    ttl7474_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
83
84public:
85    // allocators
86    static device_config *static_alloc_device_config(const machine_config &mconfig, const char *tag, const device_config *owner, UINT32 clock);
87    virtual device_t *alloc_device(running_machine &machine) const;
88
89    // inline configuration indexes go here (none yet)
90
91    // indexes to inline data
92    enum
93    {
94        INLINE_OUTPUT_CB,
95        INLINE_COMP_OUTPUT_CB
96    };
97
98protected:
99    // device_config overrides (none yet)
100
101    // internal state goes here (none yet)
102    devcb_write_line m_output_cb;
103    devcb_write_line m_comp_output_cb;
51104};
52105
53106
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)
58107
108// ======================> ttl7474_device
59109
110class ttl7474_device : public device_t
111{
112    friend class ttl7474_device_config;
113
114    // construction/destruction
115    ttl7474_device(running_machine &_machine, const ttl7474_device_config &config);
116
117public:
118    void clear_w(UINT8 state);
119    void preset_w(UINT8 state);
120    void clock_w(UINT8 state);
121    void d_w(UINT8 state);
122    UINT8 output_r();
123    UINT8 output_comp_r();    /* NOT strictly the same as !ttl7474_output_r() */
124
125protected:
126    // device-level overrides
127    virtual void device_start();
128    virtual void device_reset();
129    virtual void device_post_load() { }
130    virtual void device_clock_changed() { }
131
132    // internal state
133    const ttl7474_device_config &m_config;
134
135private:
136    /* callbacks */
137    devcb_resolved_write_line m_output_cb;
138    devcb_resolved_write_line m_comp_output_cb;
139
140    /* inputs */
141    UINT8 m_clear;              /* pin 1/13 */
142    UINT8 m_preset;             /* pin 4/10 */
143    UINT8 m_clock;              /* pin 3/11 */
144    UINT8 m_d;                  /* pin 2/12 */
145
146    /* outputs */
147    UINT8 m_output;             /* pin 5/9 */
148    UINT8 m_output_comp;        /* pin 6/8 */
149
150    /* internal */
151    UINT8 m_last_clock;
152    UINT8 m_last_output;
153    UINT8 m_last_output_comp;
154
155    void update();
156    void register_globals();
157    void init();
158};
159
160
161// device type definition
162extern const device_type MACHINE_TTL7474;
163
164
165
166//**************************************************************************
167//  READ/WRITE HANDLERS
168//**************************************************************************
169
60170WRITE_LINE_DEVICE_HANDLER( ttl7474_clear_w );
61171WRITE_LINE_DEVICE_HANDLER( ttl7474_preset_w );
62172WRITE_LINE_DEVICE_HANDLER( ttl7474_clock_w );
63173WRITE_LINE_DEVICE_HANDLER( ttl7474_d_w );
64174READ_LINE_DEVICE_HANDLER( ttl7474_output_r );
65READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r );   /* NOT strictly the same as !ttl7474_output_r() */
175READ_LINE_DEVICE_HANDLER( ttl7474_output_comp_r );    /* NOT strictly the same as !ttl7474_output_r() */
66176
67DECLARE_LEGACY_DEVICE(TTL7474, ttl7474);
68177
69#endif
178#endif /* __TTL7474_H__ */
No newline at end of file

Previous 15994 Revisions Next


© 1998-2018 The MESS Team