Previous 15994 Revisions Next

r3853 Tuesday 16th December, 2008 at 21:43:00 UTC by Antoine Mine
Converting MC6846 to device
[src/mess/machine]mc6846.c mc6846.h

src/mess/machine/mc6846.c
r3852r3853
2828/******************* internal chip data structure ******************/
2929
3030
31static struct
31typedef struct
3232{
3333
3434   const mc6846_interface* iface;
r3852r3853
5959   emu_timer *interval; /* interval programmable timer */
6060   emu_timer *one_shot; /* 1-us x factor one-shot timer */
6161
62} mc6846;
62} mc6846_t;
6363
6464
6565
r3852r3853
6767
6868#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
6969
70#define PORT_SPACE                        \
71   ((mc6846.pdr & mc6846.ddr) |               \
72    ((mc6846.iface->in_port_func ? mc6846.iface->in_port_func(space, 0) : 0) & \
73     ~mc6846.ddr))
70#define PORT                        \
71   ((mc6846->pdr & mc6846->ddr) |               \
72    ((mc6846->iface->in_port_func ? mc6846->iface->in_port_func( device, 0 ) : 0) & \
73     ~mc6846->ddr))
7474
75#define PORT_MACHINE                        \
76   ((mc6846.pdr & mc6846.ddr) |               \
77    ((mc6846.iface->in_port_func ? mc6846.iface->in_port_func(cputag_get_address_space(machine,"main",ADDRESS_SPACE_PROGRAM), 0) : 0) & \
78     ~mc6846.ddr))
79
8075#define CTO                        \
81   ((MODE == 0x30 || (mc6846.tcr & 0x80)) ? mc6846.cto : 0)
76   ((MODE == 0x30 || (mc6846->tcr & 0x80)) ? mc6846->cto : 0)
8277
83#define MODE (mc6846.tcr & 0x38)
78#define MODE (mc6846->tcr & 0x38)
8479
85#define FACTOR ((mc6846.tcr & 4) ? 8 : 1)
80#define FACTOR ((mc6846->tcr & 4) ? 8 : 1)
8681
8782
8883
89INLINE UINT16 mc6846_counter ( running_machine *machine )
84INLINE mc6846_t* get_safe_token( const device_config *device )
9085{
91   if ( mc6846.timer_started )
86   assert( device != NULL );
87   assert( device->token != NULL );
88   assert( device->type == DEVICE_GET_INFO_NAME( mc6846 ) );
89   return (mc6846_t*) device->token;
90}
91
92
93INLINE UINT16 mc6846_counter( const device_config *device )
94{
95   mc6846_t* mc6846 = get_safe_token( device );
96   if ( mc6846->timer_started )
9297   {
93      attotime delay = timer_timeleft( mc6846.interval );
94      return cpu_attotime_to_clocks( machine->cpu[mc6846.iface->cpunum], delay ) / FACTOR;
98      attotime delay = timer_timeleft( mc6846->interval );
99      return attotime_to_ticks( delay, 1000000 ) / FACTOR;
95100   }
96101   else
97      return mc6846.preset;
102      return mc6846->preset;
98103}
99104
100105
101106
102INLINE void mc6846_update_irq( running_machine *machine )
107INLINE void mc6846_update_irq( const device_config *device )
103108{
109   mc6846_t* mc6846 = get_safe_token( device );
104110   static int old_cif;
105111   int cif = 0;
106112   /* composite interrupt flag */
107   if ( ( (mc6846.csr & 1) && (mc6846.tcr & 0x40) ) ||
108        ( (mc6846.csr & 2) && (mc6846.pcr & 1) ) ||
109        ( (mc6846.csr & 4) && (mc6846.pcr & 8) && ! (mc6846.pcr & 0x20) ) )
113   if ( ( (mc6846->csr & 1) && (mc6846->tcr & 0x40) ) ||
114        ( (mc6846->csr & 2) && (mc6846->pcr & 1) ) ||
115        ( (mc6846->csr & 4) && (mc6846->pcr & 8) && ! (mc6846->pcr & 0x20) ) )
110116      cif = 1;
111117   if ( old_cif != cif )
112118   {
113119      LOG (( "%f: mc6846 interrupt %i (time=%i cp1=%i cp2=%i)\n",
114             attotime_to_double(timer_get_time(machine)), cif,
115             mc6846.csr & 1, (mc6846.csr >> 1 ) & 1, (mc6846.csr >> 2 ) & 1 ));
120             attotime_to_double(timer_get_time(device->machine)), cif,
121             mc6846->csr & 1, (mc6846->csr >> 1 ) & 1, (mc6846->csr >> 2 ) & 1 ));
116122      old_cif = cif;
117123   }
118124   if ( cif )
119125   {
120      mc6846.csr |= 0x80;
121      if ( mc6846.iface->irq_func )
122         mc6846.iface->irq_func( machine, 1 );
126      mc6846->csr |= 0x80;
127      if ( mc6846->iface->irq_func )
128         mc6846->iface->irq_func( device, 1 );
123129   }
124130   else
125131   {
126      mc6846.csr &= ~0x80;
127      if ( mc6846.iface->irq_func )
128         mc6846.iface->irq_func( machine, 0 );
132      mc6846->csr &= ~0x80;
133      if ( mc6846->iface->irq_func )
134         mc6846->iface->irq_func( device, 0 );
129135   }
130136}
131137
132138
133139
134INLINE void mc6846_update_cto ( running_machine *machine )
140INLINE void mc6846_update_cto ( const device_config *device )
135141{
142   mc6846_t* mc6846 = get_safe_token( device );
136143   int cto = CTO;
137144   static int old_cto;
138145   if ( cto != old_cto )
139146   {
140      LOG (( "%f: mc6846 CTO set to %i\n", attotime_to_double(timer_get_time(machine)), cto ));
147      LOG (( "%f: mc6846 CTO set to %i\n", attotime_to_double(timer_get_time(device->machine)), cto ));
141148      old_cto = cto;
142149   }
143   if ( mc6846.iface->out_cto_func )
144      mc6846.iface->out_cto_func( cputag_get_address_space(machine,"main",ADDRESS_SPACE_PROGRAM), 0, cto );
150   if ( mc6846->iface->out_cto_func )
151      mc6846->iface->out_cto_func( device, 0, cto );
145152}
146153
147154
148155
149INLINE void mc6846_timer_launch ( running_machine *machine )
156INLINE void mc6846_timer_launch ( const device_config *device )
150157{
151//   int delay = FACTOR * (mc6846.preset+1);
152   LOG (( "%f: mc6846 timer launch called, mode=%i, preset=%i (x%i)\n", attotime_to_double(timer_get_time(machine)), MODE, mc6846.preset, FACTOR ));
158   mc6846_t* mc6846 = get_safe_token( device );
159   int delay = FACTOR * (mc6846->preset+1);
160   LOG (( "%f: mc6846 timer launch called, mode=%i, preset=%i (x%i)\n", attotime_to_double(timer_get_time(device->machine)), MODE, mc6846->preset, FACTOR ));
153161
154   if ( ! (mc6846.tcr & 2) )
162   if ( ! (mc6846->tcr & 2) )
155163   {
156164      logerror( "mc6846 external clock CTC not implemented\n" );
157165   }
r3852r3853
161169
162170   case 0x00:
163171   case 0x10: /* continuous */
164      mc6846.cto = 0;
172      mc6846->cto = 0;
165173      break;
166174
167175   case 0x20: /* single-shot */
168      mc6846.cto = 0;
169      timer_reset( mc6846.one_shot, cpu_clocks_to_attotime( machine->cpu[mc6846.iface->cpunum], FACTOR ) );
176      mc6846->cto = 0;
177      timer_reset( mc6846->one_shot, ATTOTIME_IN_USEC(FACTOR) );
170178      break;
171179
172180   case 0x30:  /* cascaded single-shot */
r3852r3853
174182
175183   default:
176184      logerror( "mc6846 timer mode %i not implemented\n", MODE );
177      timer_reset( mc6846.interval, attotime_never );
178      mc6846.timer_started = 0;
185      timer_reset( mc6846->interval, attotime_never );
186      mc6846->timer_started = 0;
179187      return;
180188   }
181189
182   timer_reset( mc6846.interval, cpu_clocks_to_attotime( machine->cpu[mc6846.iface->cpunum], FACTOR ) );
183   mc6846.timer_started = 1;
190   timer_reset( mc6846->interval, ATTOTIME_IN_USEC(delay) );
191   mc6846->timer_started = 1;
184192
185   mc6846.csr &= ~1;
186   mc6846_update_cto(machine);
187   mc6846_update_irq(machine);
193   mc6846->csr &= ~1;
194   mc6846_update_cto( device );
195   mc6846_update_irq( device );
188196}
189197
190198
191199
192200/******************* timer callbacks *********************************/
193201
194static TIMER_CALLBACK(mc6846_timer_expire)
202static TIMER_CALLBACK( mc6846_timer_expire )
195203{
196//   int delay = FACTOR * (mc6846.latch+1);
204   device_config* device = (device_config*) ptr;
205   mc6846_t* mc6846 = get_safe_token( device );
206   int delay = FACTOR * (mc6846->latch+1);
197207
198   LOG (( "%f: mc6846 timer expire called, mode=%i, latch=%i (x%i)\n", attotime_to_double(timer_get_time(machine)), MODE, mc6846.latch, FACTOR ));
208   LOG (( "%f: mc6846 timer expire called, mode=%i, latch=%i (x%i)\n", attotime_to_double(timer_get_time(device->machine)), MODE, mc6846->latch, FACTOR ));
199209
200210   /* latch => counter */
201   mc6846.preset = mc6846.latch;
211   mc6846->preset = mc6846->latch;
202212
203   if ( ! (mc6846.tcr & 2) )
213   if ( ! (mc6846->tcr & 2) )
204214      logerror( "mc6846 external clock CTC not implemented\n" );
205215
206216   switch ( MODE )
207217   {
208218   case 0x00:
209219   case 0x10: /* continuous */
210      mc6846.cto = 1 ^ mc6846.cto;
220      mc6846->cto = 1 ^ mc6846->cto;
211221      break;
212222
213223   case 0x20: /* single-shot */
214      mc6846.cto = 0;
224      mc6846->cto = 0;
215225      break;
216226
217227   case 0x30:  /* cascaded single-shot */
218      mc6846.cto = ( mc6846.tcr & 0x80 ) ? 1 : 0;
228      mc6846->cto = ( mc6846->tcr & 0x80 ) ? 1 : 0;
219229      break;
220230
221231   default:
222232      logerror( "mc6846 timer mode %i not implemented\n", MODE );
223      timer_reset( mc6846.interval, attotime_never );
224      mc6846.timer_started = 0;
233      timer_reset( mc6846->interval, attotime_never );
234      mc6846->timer_started = 0;
225235      return;
226236   }
227237
228   timer_reset( mc6846.interval, cpu_clocks_to_attotime( machine->cpu[mc6846.iface->cpunum], FACTOR ) );
238   timer_reset( mc6846->interval, ATTOTIME_IN_USEC(delay) );
229239
230   mc6846.csr |= 1;
231   mc6846_update_cto(machine);
232   mc6846_update_irq(machine);
240   mc6846->csr |= 1;
241   mc6846_update_cto( device );
242   mc6846_update_irq( device );
233243}
234244
235245
236246
237static TIMER_CALLBACK(mc6846_timer_one_shot)
247static TIMER_CALLBACK( mc6846_timer_one_shot )
238248{
239   LOG (( "%f: mc6846 timer one shot called\n", attotime_to_double(timer_get_time(machine)) ));
249   device_config* device = (device_config*) ptr;
250   mc6846_t* mc6846 = get_safe_token( device );
251   LOG (( "%f: mc6846 timer one shot called\n", attotime_to_double(timer_get_time(device->machine)) ));
240252
241253   /* 1 micro second after one-shot launch, we put cto to high */
242   mc6846.cto = 1;
243   mc6846_update_cto(machine);
254   mc6846->cto = 1;
255   mc6846_update_cto( device );
244256}
245257
246258
r3852r3853
248260/************************** CPU interface ****************************/
249261
250262
251
252READ8_HANDLER ( mc6846_r )
263READ8_DEVICE_HANDLER ( mc6846_r )
253264{
265   mc6846_t* mc6846 = get_safe_token( device );
254266   switch ( offset )
255267   {
256268   case 0:
257269   case 4:
258270      LOG (( "$%04x %f: mc6846 CSR read $%02X intr=%i (timer=%i, cp1=%i, cp2=%i)\n",
259             cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)),
260             mc6846.csr, (mc6846.csr >> 7) & 1,
261             mc6846.csr & 1, (mc6846.csr >> 1) & 1, (mc6846.csr >> 2) & 1 ));
262      mc6846.csr0_to_be_cleared = mc6846.csr & 1;
263      mc6846.csr1_to_be_cleared = mc6846.csr & 2;
264      mc6846.csr2_to_be_cleared = mc6846.csr & 4;
265      return mc6846.csr;
271             cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)),
272             mc6846->csr, (mc6846->csr >> 7) & 1,
273             mc6846->csr & 1, (mc6846->csr >> 1) & 1, (mc6846->csr >> 2) & 1 ));
274      mc6846->csr0_to_be_cleared = mc6846->csr & 1;
275      mc6846->csr1_to_be_cleared = mc6846->csr & 2;
276      mc6846->csr2_to_be_cleared = mc6846->csr & 4;
277      return mc6846->csr;
266278
267279   case 1:
268      LOG (( "$%04x %f: mc6846 PCR read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846.pcr ));
269      return mc6846.pcr;
280      LOG (( "$%04x %f: mc6846 PCR read $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846->pcr ));
281      return mc6846->pcr;
270282
271283   case 2:
272      LOG (( "$%04x %f: mc6846 DDR read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846.ddr ));
273      return mc6846.ddr;
284      LOG (( "$%04x %f: mc6846 DDR read $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846->ddr ));
285      return mc6846->ddr;
274286
275287   case 3:
276      LOG (( "$%04x %f: mc6846 PORT read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), PORT_SPACE ));
277      if ( ! (mc6846.pcr & 0x80) )
288      LOG (( "$%04x %f: mc6846 PORT read $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), PORT ));
289      if ( ! (mc6846->pcr & 0x80) )
278290      {
279         if ( mc6846.csr1_to_be_cleared )
280            mc6846.csr &= ~2;
281         if ( mc6846.csr2_to_be_cleared )
282            mc6846.csr &= ~4;
283         mc6846_update_irq(space->machine);
284         mc6846.csr1_to_be_cleared = 0;
285         mc6846.csr2_to_be_cleared = 0;
291         if ( mc6846->csr1_to_be_cleared )
292            mc6846->csr &= ~2;
293         if ( mc6846->csr2_to_be_cleared )
294            mc6846->csr &= ~4;
295         mc6846_update_irq( device );
296         mc6846->csr1_to_be_cleared = 0;
297         mc6846->csr2_to_be_cleared = 0;
286298      }
287      return PORT_SPACE;
299      return PORT;
288300
289301   case 5:
290      LOG (( "$%04x %f: mc6846 TCR read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846.tcr ));
291      return mc6846.tcr;
302      LOG (( "$%04x %f: mc6846 TCR read $%02X\n",cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846->tcr ));
303      return mc6846->tcr;
292304
293305   case 6:
294      LOG (( "$%04x %f: mc6846 COUNTER hi read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846_counter(space->machine) >> 8 ));
295      if ( mc6846.csr0_to_be_cleared )
306      LOG (( "$%04x %f: mc6846 COUNTER hi read $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846_counter( device ) >> 8 ));
307      if ( mc6846->csr0_to_be_cleared )
296308      {
297         mc6846.csr &= ~1;
298         mc6846_update_irq(space->machine);
309         mc6846->csr &= ~1;
310         mc6846_update_irq( device );
299311      }
300      mc6846.csr0_to_be_cleared = 0;
301      return mc6846_counter(space->machine) >> 8;
312      mc6846->csr0_to_be_cleared = 0;
313      return mc6846_counter( device ) >> 8;
302314
303315   case 7:
304      LOG (( "$%04x %f: mc6846 COUNTER low read $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846_counter(space->machine) & 0xff ));
305      if ( mc6846.csr0_to_be_cleared )
316      LOG (( "$%04x %f: mc6846 COUNTER low read $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846_counter( device ) & 0xff ));
317      if ( mc6846->csr0_to_be_cleared )
306318      {
307         mc6846.csr &= ~1;
308         mc6846_update_irq(space->machine);
319         mc6846->csr &= ~1;
320         mc6846_update_irq( device );
309321      }
310      mc6846.csr0_to_be_cleared = 0;
311      return mc6846_counter(space->machine) & 0xff;
322      mc6846->csr0_to_be_cleared = 0;
323      return mc6846_counter( device ) & 0xff;
312324
313325   default:
314      logerror( "$%04x mc6846 invalid read offset %i\n", cpu_get_previouspc(space->cpu), offset );
326      logerror( "$%04x mc6846 invalid read offset %i\n", cpu_get_previouspc( device->machine->cpu[0] ), offset );
315327   }
316328   return 0;
317329}
318330
319331
320332
321WRITE8_HANDLER ( mc6846_w )
333WRITE8_DEVICE_HANDLER ( mc6846_w )
322334{
335   mc6846_t* mc6846 = get_safe_token( device );
323336   switch ( offset )
324337   {
325338   case 0:
r3852r3853
340353         "latcged,pos-edge", "latcged,pos-edge,intr"
341354      };
342355      LOG (( "$%04x %f: mc6846 PCR write $%02X reset=%i cp2=%s cp1=%s\n",
343             cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), data,
356             cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), data,
344357             (data >> 7) & 1, cp2[ (data >> 3) & 7 ], cp1[ data & 7 ] ));
345358
346359   }
347   mc6846.pcr = data;
360   mc6846->pcr = data;
348361   if ( data & 0x80 )
349362   {      /* data reset */
350      mc6846.pdr = 0;
351      mc6846.ddr = 0;
352      mc6846.csr &= ~6;
353      mc6846_update_irq(space->machine);
363      mc6846->pdr = 0;
364      mc6846->ddr = 0;
365      mc6846->csr &= ~6;
366      mc6846_update_irq( device );
354367   }
355368   if ( data & 4 )
356      logerror( "$%04x mc6846 CP1 latching not implemented\n", cpu_get_previouspc(space->cpu) );
369      logerror( "$%04x mc6846 CP1 latching not implemented\n", cpu_get_previouspc( device->machine->cpu[0] ) );
357370   if (data & 0x20)
358371   {
359372      if (data & 0x10)
360373      {
361         mc6846.cp2_cpu = (data >> 3) & 1;
362         if ( mc6846.iface->out_cp2_func )
363            mc6846.iface->out_cp2_func( space, 0, mc6846.cp2_cpu );
374         mc6846->cp2_cpu = (data >> 3) & 1;
375         if ( mc6846->iface->out_cp2_func )
376            mc6846->iface->out_cp2_func( device, 0, mc6846->cp2_cpu );
364377      }
365378      else
366         logerror( "$%04x mc6846 acknowledge not implemented\n", cpu_get_previouspc(space->cpu) );
379         logerror( "$%04x mc6846 acknowledge not implemented\n", cpu_get_previouspc( device->machine->cpu[0] ) );
367380   }
368381   break;
369382
370383   case 2:
371      LOG (( "$%04x %f: mc6846 DDR write $%02X\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), data ));
372      if ( ! (mc6846.pcr & 0x80) )
384      LOG (( "$%04x %f: mc6846 DDR write $%02X\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), data ));
385      if ( ! (mc6846->pcr & 0x80) )
373386      {
374         mc6846.ddr = data;
375         if ( mc6846.iface->out_port_func )
376            mc6846.iface->out_port_func( space, 0, mc6846.pdr & mc6846.ddr );
387         mc6846->ddr = data;
388         if ( mc6846->iface->out_port_func )
389            mc6846->iface->out_port_func( device, 0, mc6846->pdr & mc6846->ddr );
377390      }
378391      break;
379392
380393   case 3:
381      LOG (( "$%04x %f: mc6846 PORT write $%02X (mask=$%02X)\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), data,mc6846.ddr ));
382      if ( ! (mc6846.pcr & 0x80) )
394      LOG (( "$%04x %f: mc6846 PORT write $%02X (mask=$%02X)\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), data,mc6846->ddr ));
395      if ( ! (mc6846->pcr & 0x80) )
383396      {
384         mc6846.pdr = data;
385         if ( mc6846.iface->out_port_func )
386            mc6846.iface->out_port_func( space, 0, mc6846.pdr & mc6846.ddr );
387         if ( mc6846.csr1_to_be_cleared && (mc6846.csr & 2) )
397         mc6846->pdr = data;
398         if ( mc6846->iface->out_port_func )
399            mc6846->iface->out_port_func( device, 0, mc6846->pdr & mc6846->ddr );
400         if ( mc6846->csr1_to_be_cleared && (mc6846->csr & 2) )
388401         {
389            mc6846.csr &= ~2;
390            LOG (( "$%04x %f: mc6846 CP1 intr reset\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)) ));
402            mc6846->csr &= ~2;
403            LOG (( "$%04x %f: mc6846 CP1 intr reset\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)) ));
391404         }
392         if ( mc6846.csr2_to_be_cleared && (mc6846.csr & 4) )
405         if ( mc6846->csr2_to_be_cleared && (mc6846->csr & 4) )
393406         {
394            mc6846.csr &= ~4;
395            LOG (( "$%04x %f: mc6846 CP2 intr reset\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)) ));
407            mc6846->csr &= ~4;
408            LOG (( "$%04x %f: mc6846 CP2 intr reset\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)) ));
396409         }
397         mc6846.csr1_to_be_cleared = 0;
398         mc6846.csr2_to_be_cleared = 0;
399         mc6846_update_irq(space->machine);
410         mc6846->csr1_to_be_cleared = 0;
411         mc6846->csr2_to_be_cleared = 0;
412         mc6846_update_irq( device );
400413      }
401414      break;
402415
r3852r3853
408421            "freq-cmp", "freq-cmp", "pulse-cmp", "pulse-cmp"
409422         };
410423      LOG (( "$%04x %f: mc6846 TCR write $%02X reset=%i clock=%s scale=%i mode=%s out=%s\n",
411             cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), data,
424             cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), data,
412425             (data >> 7) & 1, (data & 0x40) ? "extern" : "sys",
413426             (data & 0x40) ? 1 : 8, mode[ (data >> 1) & 7 ],
414427             (data & 1) ? "enabled" : "0" ));
415428
416      mc6846.tcr = data;
417      if ( mc6846.tcr & 1 )
429      mc6846->tcr = data;
430      if ( mc6846->tcr & 1 )
418431      {
419432         /* timer preset = initialization without launch */
420         mc6846.preset = mc6846.latch;
421         mc6846.csr &= ~1;
433         mc6846->preset = mc6846->latch;
434         mc6846->csr &= ~1;
422435         if ( MODE != 0x30 )
423            mc6846.cto = 0;
424         mc6846_update_cto(space->machine);
425         timer_reset( mc6846.interval, attotime_never );
426         timer_reset( mc6846.one_shot, attotime_never );
427         mc6846.timer_started = 0;
436            mc6846->cto = 0;
437         mc6846_update_cto( device );
438         timer_reset( mc6846->interval, attotime_never );
439         timer_reset( mc6846->one_shot, attotime_never );
440         mc6846->timer_started = 0;
428441      }
429442      else
430443      {
431444         /* timer launch */
432         if ( ! mc6846.timer_started )
433            mc6846_timer_launch(space->machine);
445         if ( ! mc6846->timer_started )
446            mc6846_timer_launch( device );
434447      }
435      mc6846_update_irq(space->machine);
448      mc6846_update_irq( device );
436449   }
437450   break;
438451
439452   case 6:
440      mc6846.time_MSB = data;
453      mc6846->time_MSB = data;
441454      break;
442455
443456   case 7:
444      mc6846.latch = ( ((UINT16) mc6846.time_MSB) << 8 ) + data;
445      LOG (( "$%04x %f: mc6846 COUNT write %i\n", cpu_get_previouspc(space->cpu), attotime_to_double(timer_get_time(space->machine)), mc6846.latch  ));
446      if (!(mc6846.tcr & 0x38))
457      mc6846->latch = ( ((UINT16) mc6846->time_MSB) << 8 ) + data;
458      LOG (( "$%04x %f: mc6846 COUNT write %i\n", cpu_get_previouspc( device->machine->cpu[0] ), attotime_to_double(timer_get_time(device->machine)), mc6846->latch  ));
459      if (!(mc6846->tcr & 0x38))
447460      {
448461         /* timer initialization */
449         mc6846.preset = mc6846.latch;
450         mc6846.csr &= ~1;
451         mc6846_update_irq(space->machine);
452         mc6846.cto = 0;
453         mc6846_update_cto(space->machine);
462         mc6846->preset = mc6846->latch;
463         mc6846->csr &= ~1;
464         mc6846_update_irq( device );
465         mc6846->cto = 0;
466         mc6846_update_cto( device );
454467         /* launch only if started */
455         if (!(mc6846.tcr & 1))
456            mc6846_timer_launch(space->machine);
468         if (!(mc6846->tcr & 1))
469            mc6846_timer_launch( device );
457470      }
458471      break;
459472
460473   default:
461      logerror( "$%04x mc6846 invalid write offset %i\n", cpu_get_previouspc(space->cpu), offset );
474      logerror( "$%04x mc6846 invalid write offset %i\n", cpu_get_previouspc( device->machine->cpu[0] ), offset );
462475   }
463476}
464477
r3852r3853
468481
469482
470483
471void mc6846_set_input_cp1 ( running_machine *machine, int data )
484void mc6846_set_input_cp1 ( const device_config *device, int data )
472485{
486   mc6846_t* mc6846 = get_safe_token( device );
473487   data = (data != 0 );
474   if ( data == mc6846.cp1 )
488   if ( data == mc6846->cp1 )
475489      return;
476   mc6846.cp1 = data;
477   LOG (( "%f: mc6846 input CP1 set to %i\n",  attotime_to_double(timer_get_time(machine)), data ));
478   if (( data &&  (mc6846.pcr & 2)) || (!data && !(mc6846.pcr & 2)))
490   mc6846->cp1 = data;
491   LOG (( "%f: mc6846 input CP1 set to %i\n",  attotime_to_double(timer_get_time(device->machine)), data ));
492   if (( data &&  (mc6846->pcr & 2)) || (!data && !(mc6846->pcr & 2)))
479493   {
480      mc6846.csr |= 2;
481      mc6846_update_irq(machine);
494      mc6846->csr |= 2;
495      mc6846_update_irq( device );
482496   }
483497}
484498
485void mc6846_set_input_cp2 ( running_machine *machine, int data )
499void mc6846_set_input_cp2 ( const device_config *device, int data )
486500{
501   mc6846_t* mc6846 = get_safe_token( device );
487502   data = (data != 0 );
488   if ( data == mc6846.cp2 )
503   if ( data == mc6846->cp2 )
489504      return;
490   mc6846.cp2 = data;
491   LOG (( "%f: mc6846 input CP2 set to %i\n", attotime_to_double(timer_get_time(machine)), data ));
492   if (mc6846.pcr & 0x20)
505   mc6846->cp2 = data;
506   LOG (( "%f: mc6846 input CP2 set to %i\n", attotime_to_double(timer_get_time(device->machine)), data ));
507   if (mc6846->pcr & 0x20)
493508   {
494      if (( data &&  (mc6846.pcr & 0x10)) || (!data && !(mc6846.pcr & 0x10)))
509      if (( data &&  (mc6846->pcr & 0x10)) || (!data && !(mc6846->pcr & 0x10)))
495510      {
496         mc6846.csr |= 4;
497         mc6846_update_irq(machine);
511         mc6846->csr |= 4;
512         mc6846_update_irq( device );
498513      }
499514   }
500515}
r3852r3853
505520
506521
507522
508UINT8 mc6846_get_output_port ( running_machine *machine )
523UINT8 mc6846_get_output_port ( const device_config *device )
509524{
510   return PORT_MACHINE;
525   mc6846_t* mc6846 = get_safe_token( device );
526   return PORT;
511527}
512528
513529
514530
515UINT8 mc6846_get_output_cto ( void )
531UINT8 mc6846_get_output_cto ( const device_config *device )
516532{
533   mc6846_t* mc6846 = get_safe_token( device );
517534   return CTO;
518535}
519536
520537
521538
522UINT8 mc6846_get_output_cp2 ( void )
539UINT8 mc6846_get_output_cp2 ( const device_config *device )
523540{
524   return mc6846.cp2_cpu;
541   mc6846_t* mc6846 = get_safe_token( device );
542   return mc6846->cp2_cpu;
525543}
526544
527545
528546
529UINT16 mc6846_get_preset ( void )
547UINT16 mc6846_get_preset ( const device_config *device )
530548{
531   return mc6846.preset;
549   mc6846_t* mc6846 = get_safe_token( device );
550   return mc6846->preset;
532551}
533552
534553
r3852r3853
536555/************************ reset *****************************/
537556
538557
539
540void mc6846_reset ( void )
558static DEVICE_RESET( mc6846 )
541559{
560   mc6846_t* mc6846 = get_safe_token( device );
542561   LOG (( "mc6846_reset\n" ));
543   mc6846.cto   = 0;
544   mc6846.csr   = 0;
545   mc6846.pcr   = 0x80;
546   mc6846.ddr   = 0;
547   mc6846.pdr   = 0;
548   mc6846.tcr   = 1;
549   mc6846.cp1   = 0;
550   mc6846.cp2   = 0;
551   mc6846.cp2_cpu  = 0;
552   mc6846.latch    = 0xffff;
553   mc6846.preset   = 0xffff;
554   mc6846.time_MSB = 0;
555   mc6846.csr0_to_be_cleared = 0;
556   mc6846.csr1_to_be_cleared = 0;
557   mc6846.csr2_to_be_cleared = 0;
558   mc6846.timer_started = 0;
559   timer_reset( mc6846.interval, attotime_never );
560   timer_reset( mc6846.one_shot, attotime_never );
562   mc6846->cto   = 0;
563   mc6846->csr   = 0;
564   mc6846->pcr   = 0x80;
565   mc6846->ddr   = 0;
566   mc6846->pdr   = 0;
567   mc6846->tcr   = 1;
568   mc6846->cp1   = 0;
569   mc6846->cp2   = 0;
570   mc6846->cp2_cpu  = 0;
571   mc6846->latch    = 0xffff;
572   mc6846->preset   = 0xffff;
573   mc6846->time_MSB = 0;
574   mc6846->csr0_to_be_cleared = 0;
575   mc6846->csr1_to_be_cleared = 0;
576   mc6846->csr2_to_be_cleared = 0;
577   mc6846->timer_started = 0;
578   timer_reset( mc6846->interval, attotime_never );
579   timer_reset( mc6846->one_shot, attotime_never );
561580}
562581
563582
583/************************ start *****************************/
564584
585static DEVICE_START( mc6846 )
586{
587   mc6846_t* mc6846 = get_safe_token( device );
588
589   mc6846->iface = device->static_config;
590   mc6846->interval = timer_alloc( device->machine, mc6846_timer_expire , (void*) device );
591   mc6846->one_shot = timer_alloc( device->machine, mc6846_timer_one_shot , (void*) device );
592
593   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->csr );
594   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->pcr );
595   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->ddr );
596   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->pdr );
597   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->tcr );
598   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->cp1 );
599   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->cp2 );
600   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->cp2_cpu );
601   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->cto );
602   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->time_MSB );
603   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->csr0_to_be_cleared );
604   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->csr1_to_be_cleared );
605   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->csr2_to_be_cleared );
606   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->latch );
607   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->preset );
608   state_save_register_item( device->machine, "mc6846", device->tag, 0, mc6846->timer_started );
609
610   return DEVICE_START_OK;
611}
612
613
565614/************************** configuration ****************************/
566615
567void mc6846_config ( running_machine *machine, const mc6846_interface* iface )
616
617static DEVICE_SET_INFO( mc6846 )
568618{
569   mc6846.iface = iface;
570   mc6846.interval = timer_alloc(machine,  mc6846_timer_expire , NULL);
571   mc6846.one_shot = timer_alloc(machine,  mc6846_timer_one_shot , NULL);
619   switch ( state ) {
620      /* no parameters to set */
621   }
622}
572623
573   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.csr );
574   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.pcr );
575   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.ddr );
576   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.pdr );
577   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.tcr );
578   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.cp1 );
579   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.cp2 );
580   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.cp2_cpu );
581   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.cto );
582   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.time_MSB );
583   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.csr0_to_be_cleared );
584   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.csr1_to_be_cleared );
585   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.csr2_to_be_cleared );
586   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.latch );
587   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.preset );
588   state_save_register_item(machine, "mc6846", NULL, 0, mc6846.timer_started );
589624
590   mc6846_reset();
625DEVICE_GET_INFO( mc6846 ) {
626   switch ( state ) {
627      /* --- the following bits of info are returned as 64-bit signed integers --- */
628      case DEVINFO_INT_TOKEN_BYTES:         info->i = sizeof(mc6846_t);         break;
629      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = 0;               break;
630      case DEVINFO_INT_CLASS:            info->i = DEVICE_CLASS_PERIPHERAL;      break;
631
632      /* --- the following bits of info are returned as pointers to data or functions --- */
633           case DEVINFO_FCT_SET_INFO:              info->set_info = DEVICE_SET_INFO_NAME(mc6846);  break;
634      case DEVINFO_FCT_START:            info->start = DEVICE_START_NAME(mc6846);   break;
635      case DEVINFO_FCT_STOP:            /* nothing */               break;
636      case DEVINFO_FCT_RESET:            info->reset = DEVICE_RESET_NAME(mc6846);   break;
637
638      /* --- the following bits of info are returned as NULL-terminated strings --- */
639              case DEVINFO_STR_NAME:            info->s = "Motorola MC6846 programmable timer";   break;
640      case DEVINFO_STR_FAMILY:         info->s = "MC6846";            break;
641      case DEVINFO_STR_VERSION:         info->s = "1.00";            break;
642      case DEVINFO_STR_SOURCE_FILE:         info->s = __FILE__;            break;
643      case DEVINFO_STR_CREDITS:         info->s = "Copyright the MAME and MESS Teams";  break;
644   }
591645}
src/mess/machine/mc6846.h
r3852r3853
66
77**********************************************************************/
88
9#ifndef MC6846
10#define MC6846
9#ifndef MC6846_H
10#define MC6846_H
1111
12#define MC6846 DEVICE_GET_INFO_NAME(mc6846)
1213
1314/* ---------- configuration ------------ */
1415
r3852r3853
1617struct _mc6846_interface
1718{
1819  /* CPU write to the outside through chip */
19  write8_space_func out_port_func;  /* 8-bit output */
20  write8_space_func out_cp1_func;   /* 1-bit output */
21  write8_space_func out_cp2_func;   /* 1-bit output */
20  write8_device_func out_port_func;  /* 8-bit output */
21  write8_device_func out_cp1_func;   /* 1-bit output */
22  write8_device_func out_cp2_func;   /* 1-bit output */
2223
2324  /* CPU read from the outside through chip */
24  read8_space_func in_port_func; /* 8-bit input */
25  read8_device_func in_port_func; /* 8-bit input */
2526
2627  /* asynchronous timer output to outside world */
27  write8_space_func out_cto_func; /* 1-bit output */
28  write8_device_func out_cto_func; /* 1-bit output */
2829
2930  /* timer interrupt */
30  void ( * irq_func ) ( running_machine *machine, int state );
31
32  /* CPU identifier (defines the clock rate) */
33  int cpunum;
31  void ( * irq_func ) ( const device_config *device, int state );
3432};
3533
3634
35#define MDRV_MC6846_ADD(_tag, _intrf) \
36  MDRV_DEVICE_ADD(_tag, MC6846)         \
37  MDRV_DEVICE_CONFIG(_intrf)
38
39#define MDRV_MC6846_MODIFY(_tag, _intrf) \
40  MDRV_DEVICE_MODIFY(_tag, MC6846)         \
41  MDRV_DEVICE_CONFIG(_intrf)
42
43#define MDRV_MC6846_REMOVE(_tag)      \
44  MDRV_DEVICE_REMOVE(_tag, MC6846)
45
46
3747/* ---------- functions ------------ */
3848
39extern void mc6846_config ( running_machine *machine, const mc6846_interface *func );
4049
41/* reset by external signal */
42extern void mc6846_reset ( void );
50extern DEVICE_GET_INFO(mc6846);
4351
4452/* interface to CPU via address/data bus*/
45extern READ8_HANDLER  ( mc6846_r );
46extern WRITE8_HANDLER ( mc6846_w );
53extern READ8_DEVICE_HANDLER  ( mc6846_r );
54extern WRITE8_DEVICE_HANDLER ( mc6846_w );
4755
4856/* asynchronous write from outside world into interrupt-generating pins */
49extern void mc6846_set_input_cp1 ( running_machine *machine, int data );
50extern void mc6846_set_input_cp2 ( running_machine *machine, int data );
57extern void mc6846_set_input_cp1 ( const device_config *device, int data );
58extern void mc6846_set_input_cp2 ( const device_config *device, int data );
5159
5260/* polling from outside world */
53extern UINT8  mc6846_get_output_port ( running_machine *machine );
54extern UINT8  mc6846_get_output_cto ( void );
55extern UINT8  mc6846_get_output_cp2 ( void );
61extern UINT8  mc6846_get_output_port ( const device_config *device );
62extern UINT8  mc6846_get_output_cto  ( const device_config *device );
63extern UINT8  mc6846_get_output_cp2  ( const device_config *device );
5664
5765/* partial access to internal state */
58extern UINT16 mc6846_get_preset ( void ); /* timer interval - 1 in us */
66extern UINT16 mc6846_get_preset ( const device_config *device ); /* timer interval - 1 in us */
5967
6068#endif
6169

Previous 15994 Revisions Next


© 1998-2018 The MESS Team