Previous 15994 Revisions Next

r14514 Saturday 18th February, 2012 at 17:39:34 UTC by Michael Zapf
ti99: The Grand TI-99 Family Rewrite, using class structures and modern
address maps [Michael Zapf]
[src/mess]mess.mak mess_dev.lst
[src/mess/drivers]geneve.c ti99_4p.c ti99_4x.c ti99_8.c tm990189.c
[src/mess/machine]at29040a.c at29040a.h smc92x4.c smc92x4.h tms9901.c tms9901.h tms9902.c tms9902.h
[src/mess/machine/ti99]bwg.c bwg.h datamux.c datamux.h evpc.c evpc.h genboard.c genboard.h grom.c grom.h gromport.c gromport.h handset.c handset.h hfdc.c hfdc.h hsgpl.c hsgpl.h mapper8.c mapper8.h mecmouse.c mecmouse.h memex.c memex.h myarcmem.c myarcmem.h p_code.c p_code.h peribox.c peribox.h samsmem.c samsmem.h sgcpu.c sgcpu.h spchsyn.c spchsyn.h speech8.c speech8.h ti32kmem.c ti32kmem.h ti99_hd.c ti99_hd.h ti99defs.h ti_fdc.c ti_fdc.h ti_rs232.c ti_rs232.h tiboard.c tiboard.h tn_ide.c tn_ide.h tn_usbsm.c tn_usbsm.h videowrp.c videowrp.h

src/mess/machine/tms9901.c
r14513r14514
1/*
1/****************************************************************************
2
23    TMS9901 Programmable System Interface
34
4    Raphael Nabet, 2000-2004
5*/
6
7#include <math.h>
8#include "emu.h"
9
10#include "tms9901.h"
11
12/*
13    TMS9901 emulation.
14
155Overview:
166    TMS9901 is a support chip for TMS9900.  It handles interrupts, provides
177    several I/O pins, and a timer (a.k.a. clock: it is merely a register which
r14513r14514
8777    its I/O pins. Thus I believe the above TODO concering the exit of the timer
8878    mode is not applicable.
8979    The problem is that the original 9901 specification is not clear about this.
90*/
9180
81MZ: Turned to class (January 2012)
9282
93/*
94    tms9901 state structure
95*/
83TODO: Tests on a real machine
84- Set an interrupt input (e.g. keyboard for Geneve), trigger RST2*, check whether
85  interrupt mask has been reset
86- Check whether the clock_read_register is updated whenever clock mode is exited
87  (in particular when S0=1, i.e. A10=1 -> addresses xxxx xxxx xx1x xxxx
88  requires to write a program that fits into 32 bytes; workspace elsewhere)
9689
90    Raphael Nabet, 2000-2004
91    Michael Zapf
9792
98/***************************************************************************
99    TYPE DEFINITIONS
100***************************************************************************/
93    February 2012: Rewritten as class
10194
102typedef struct _tms9901_t tms9901_t;
103struct _tms9901_t
104{
105   /* interrupt registers */
106   // mask:  bit #n is set if pin #n is supported as an interrupt pin,
107   // i.e. the driver sends a notification whenever the pin state changes
108   // setting these bits is not required, but it saves you the trouble of
109   // saving the state of interrupt pins and feeding it to the port read
110   // handlers again
111   int supported_int_mask;
112   int int_state;         /* state of the int1-int15 lines */
113   int old_int_state;      /* stores the previous value to avoid useless INT line assertions */
114   int timer_int_pending;   /* timer int pending (overrides int3 pin if timer enabled) */
115   int enabled_ints;      /* interrupt enable mask */
116   int int_pending;      /* status of the int* pin (connected to TMS9900) */
95*****************************************************************************/
11796
118   /* PIO registers */
119   int pio_direction;      /* direction register for PIO */
97#include <math.h>
98#include "emu.h"
12099
121   /* current PIO output (to be masked with pio_direction) */
122   int pio_output;
100#include "tms9901.h"
123101
124   /* mirrors used for INT7*-INT15* */
125   int pio_direction_mirror;
126   int pio_output_mirror;
127
128   /* clock registers */
129   // MESS timer, used to emulate the decrementer register
130   emu_timer *timer;
131
132   // clock interval, loaded in decrementer when it reaches 0.
133   // 0 means decrementer off
134   int clockinvl;
135   // when we go into timer mode, the decrementer is copied there to allow to read it reliably
136   int latchedtimer;
137
138   // TMS9901 current mode
139   // 0 = so-called interrupt mode (read interrupt state, write interrupt enable mask)
140   // 1 = clock mode (read/write clock interval)
141   int mode9901;
142
143   // tms9901 clock rate (PHI* pin, normally connected to TMS9900 Phi3*)
144   // decrementer rate equates PHI* rate/64 (e.g. 46875Hz for a 3MHz clock)
145   // (warning: 3MHz on a tms9900 is equivalent to 12MHz on a tms9995 or tms99000)
146   double clock_rate;
147
148   /* Pointer to interface */
149   const tms9901_interface *intf;
150};
151
152
153/***************************************************************************
154    INLINE FUNCTIONS
155***************************************************************************/
156
157INLINE tms9901_t *get_token(device_t *device)
158{
159   assert(device != NULL);
160   assert(device->type() == TMS9901);
161   return (tms9901_t *) downcast<legacy_device_base *>(device)->token();
162}
163
164102/*
165    return the number of the first (i.e. least significant) non-zero bit among
166    the 16 first bits
103    Constructor
167104*/
168static int find_first_bit(int value)
105tms9901_device::tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
106: device_t(mconfig, TMS9901, "TMS9901 Programmable System Interface", tag, owner, clock)
169107{
170   int bit = 0;
171
172   if (! value)
173      return -1;
174
175   while (! (value & 1))
176   {
177      value >>= 1;   /* try next bit */
178      bit++;
179   }
180   return bit;
181108}
182109
183110/*
184111    should be called after any change to int_state or enabled_ints.
185112*/
186static void tms9901_field_interrupts(device_t *device)
113void tms9901_device::field_interrupts(void)
187114{
188115   int current_ints;
189   tms9901_t *tms = get_token(device);
190116
191117   /* int_state: state of lines int1-int15 */
192   current_ints = tms->int_state;
193   if (tms->clockinvl != 0)
118   current_ints = m_int_state;
119   if (m_clock_register != 0)
194120   {   /* if timer is enabled, INT3 pin is overriden by timer */
195      if (tms->timer_int_pending)
121      if (m_timer_int_pending)
122      {
196123         current_ints |= TMS9901_INT3;
124      }
197125      else
198         current_ints &= ~ TMS9901_INT3;
126         current_ints &= ~TMS9901_INT3;
199127   }
200128
201129   /* enabled_ints: enabled interrupts */
202130   /* mask out all int pins currently set as output */
203   current_ints &= tms->enabled_ints & (~ tms->pio_direction_mirror);
131   current_ints &= m_enabled_ints & (~m_pio_direction_mirror);
204132
205133   // Check whether we have a new state. For systems that use level-triggered
206134   // interrupts it should not do any harm if the line is re-asserted
207135   // but we may as well avoid this.
208   if (current_ints == tms->old_int_state)
136   if (current_ints == m_old_int_state)
209137      return;
210138
211   tms->old_int_state = current_ints;
139   m_old_int_state = current_ints;
212140
213141   if (current_ints)
214142   {
215143      // find which interrupt tripped us:
144      // the number of the first (i.e. least significant) non-zero bit among
145      // the 16 first bits
216146      // we simply look for the first bit set to 1 in current_ints... */
217      int level = find_first_bit(current_ints);
147      int level = 0;
218148
219      tms->int_pending = TRUE;
220
221      if (tms->intf->interrupt_callback)
222         (*tms->intf->interrupt_callback)(device, 1, level);
149      while ((current_ints & 1)==0)
150      {
151         current_ints >>= 1;   /* try next bit */
152         level++;
153      }
154      m_int_pending = true;
155      if (!m_interrupt.isnull())
156         m_interrupt(level, 1);  // the offset carries the IC0-3 level
223157   }
224158   else
225159   {
226      tms->int_pending = FALSE;
227
228      if (tms->intf->interrupt_callback)
229         (*tms->intf->interrupt_callback)(device, 0, 0xf);
160      m_int_pending = false;
161      if (!m_interrupt.isnull())
162         m_interrupt(0xf, 0);  //Spec: INTREQ*=1 <=> IC0,1,2,3 = 1111
230163   }
231164}
232165
233
234166/*
235167    function which should be called by the driver when the state of an INTn*
236168    pin changes (only required if the pin is set up as an interrupt pin)
r14513r14514
240172
241173    0<=pin_number<=15
242174*/
243void tms9901_set_single_int(device_t *device, int pin_number, int state)
175void tms9901_device::set_single_int(int pin_number, int state)
244176{
245   tms9901_t *tms = get_token(device);
246177   /* remember new state of INTn* pin state */
247   if (state)
248      tms->int_state |= 1 << pin_number;
178   if (state==ASSERT_LINE)
179      m_int_state |= 1 << pin_number;
249180   else
250      tms->int_state &= ~ (1 << pin_number);
181      m_int_state &= ~(1 << pin_number);
251182
252183   /* we do not need to always call this function - time for an optimization */
253   tms9901_field_interrupts(device);
184   field_interrupts();
254185}
255186
256
257187/*
258    This call-back is called by the MESS timer system when the decrementer
259    reaches 0.
188    load the content of m_clock_register into the decrementer
260189*/
261static TIMER_CALLBACK(decrementer_callback)
190void tms9901_device::timer_reload(void)
262191{
263   device_t *device = (device_t *) ptr;
264   tms9901_t *tms = get_token(device);
265
266   tms->timer_int_pending = TRUE;         /* decrementer interrupt requested */
267
268   tms9901_field_interrupts(device);
269}
270
271/*
272    load the content of clockinvl into the decrementer
273*/
274static void tms9901_timer_reload(device_t *device)
275{
276   tms9901_t *tms = get_token(device);
277   if (tms->clockinvl)
192   if (m_clock_register != 0)
278193   {   /* reset clock interval */
279      tms->timer->adjust(attotime::from_double((double) tms->clockinvl / (tms->clock_rate / 64.)), 0, attotime::from_double((double) tms->clockinvl / (tms->clock_rate / 64.)));
194      m_decrementer_value = m_clock_register;
195      m_decrementer->enable(true);
280196   }
281197   else
282198   {   /* clock interval == 0 -> no timer */
283      tms->timer->enable(0);
199      m_decrementer->enable(false);
284200   }
285201}
286202
287
288
289203/*----------------------------------------------------------------
290204    TMS9901 CRU interface.
291205----------------------------------------------------------------*/
r14513r14514
294208    Read a 8 bit chunk from tms9901.
295209
296210    signification:
297    bit 0: mode9901
298    if (mode9901 == 0)
211    bit 0: m_clock_mode
212    if (m_clock_mode == false)
299213     bit 1-15: current status of the INT1*-INT15* pins
300214    else
301215     bit 1-14: current timer value
r14513r14514
303217
304218    bit 16-31: current status of the P0-P15 pins (quits timer mode, too...)
305219*/
306READ8_DEVICE_HANDLER ( tms9901_cru_r )
220READ8_MEMBER( tms9901_device::read )
307221{
308222   int answer;
309   tms9901_t *tms = get_token(device);
310223
311224   offset &= 0x003;
312225
313226   switch (offset)
314227   {
315228   case 0:
316      if (tms->mode9901)
229      if (m_clock_mode)
317230      {   /* clock mode */
318         answer = ((tms->latchedtimer & 0x7F) << 1) | 0x01;
231         answer = ((m_clock_read_register & 0x7F) << 1) | 0x01;
319232      }
320233      else
321234      {   /* interrupt mode */
322         answer = ((~ tms->int_state) & tms->supported_int_mask) & 0xFF;
235         // m_int_state stores the INTx values, which are inverted to the pin levels (INTx*)
236         answer = ((~m_int_state) & m_supported_int_mask) & 0xFF;
323237
324         if (tms->intf->read_handlers[0])
325            answer |= (* tms->intf->read_handlers[0])(device, 0);
238         if (!m_read_block.isnull())
239            answer |= m_read_block(TMS9901_CB_INT7);
326240
327         answer &= ~ tms->pio_direction_mirror;
328         answer |= (tms->pio_output_mirror & tms->pio_direction_mirror) & 0xFF;
241         answer &= ~ m_pio_direction_mirror;
242         answer |= (m_pio_output_mirror & m_pio_direction_mirror) & 0xFF;
329243      }
330244      break;
331245   case 1:
332      if (tms->mode9901)
246      if (m_clock_mode)
333247      {   /* clock mode */
334         answer = (tms->latchedtimer & 0x3F80) >> 7;
335         if (! tms->int_pending)
248         answer = (m_clock_read_register & 0x3F80) >> 7;
249         if (!m_int_pending)
336250            answer |= 0x80;
337251      }
338252      else
339253      {   /* interrupt mode */
340         answer = ((~ tms->int_state) & tms->supported_int_mask) >> 8;
254         answer = ((~m_int_state) & m_supported_int_mask) >> 8;
341255
342         if (tms->intf->read_handlers[1])
343            answer |= (* tms->intf->read_handlers[1])(device, 1);
256         if (!m_read_block.isnull())
257            answer |= m_read_block(TMS9901_INT8_INT15);
344258
345         answer &= ~ (tms->pio_direction_mirror >> 8);
346         answer |= (tms->pio_output_mirror & tms->pio_direction_mirror) >> 8;
259         answer &= ~ (m_pio_direction_mirror >> 8);
260         answer |= (m_pio_output_mirror & m_pio_direction_mirror) >> 8;
347261      }
348262      break;
349263   case 2:
350264      /* exit timer mode */
351265      // MZ: See comments at the beginning. I'm pretty sure this is not correct.
352      // tms->mode9901 = 0;
266      // m_clock_mode = false;
353267
354      answer = (tms->intf->read_handlers[2]) ? (* tms->intf->read_handlers[2])(device, 2) : 0;
268      if (!m_read_block.isnull())
269         answer = m_read_block(TMS9901_P0_P7);
270      else
271         answer = 0;
355272
356      answer &= ~ tms->pio_direction;
357      answer |= (tms->pio_output & tms->pio_direction) & 0xFF;
273      answer &= ~ m_pio_direction;
274      answer |= (m_pio_output & m_pio_direction) & 0xFF;
358275
359276      break;
360   default:
361      /* exit timer mode */
362      // MZ: dito
363      // tms->mode9901 = 0;
277   case 3:
278      // MZ: see above
279      // m_clock_mode = false;        // exit timer mode
280      if (!m_read_block.isnull())
281         answer = m_read_block(TMS9901_P8_P15);
282      else
283         answer = 0;
364284
365      answer = (tms->intf->read_handlers[3]) ? (* tms->intf->read_handlers[3])(device, 3) : 0;
285      answer &= ~ (m_pio_direction >> 8);
286      answer |= (m_pio_output & m_pio_direction) >> 8;
366287
367      answer &= ~ (tms->pio_direction >> 8);
368      answer |= (tms->pio_output & tms->pio_direction) >> 8;
369
370288      break;
371289   }
372290
r14513r14514
377295    Write 1 bit to tms9901.
378296
379297    signification:
380    bit 0: write mode9901
381    if (mode9901 == 0)
298    bit 0: write m_clock_mode
299    if (!m_clock_mode)
382300     bit 1-15: write interrupt mask register
383301    else
384302     bit 1-14: write timer period
r14513r14514
386304
387305    bit 16-31: set output state of P0-P15 (and set them as output pin) (quit timer mode, too...)
388306*/
389WRITE8_DEVICE_HANDLER ( tms9901_cru_w )
307WRITE8_MEMBER ( tms9901_device::write )
390308{
391   tms9901_t *tms = get_token(device);
392
393309   data &= 1;   /* clear extra bits */
394310   offset &= 0x01F;
395
396311   switch (offset)
397312   {
398313   case 0x00:   /* write to mode bit */
399      if (tms->mode9901 != data)
314      if (data == 0)
400315      {
401         tms->mode9901 = data;
402
403         if (tms->mode9901)
404         {
405            /* we are switching to clock mode: latch the current value of
406                the decrementer register */
407            if (tms->clockinvl)
408               tms->latchedtimer = ceil(tms->timer->remaining().as_double() * (tms->clock_rate / 64.));
409            else
410               tms->latchedtimer = 0;      /* timer inactive... */
411         }
316         /* we are quitting clock mode */
317         m_clock_mode = false;
318      }
319      else
320      {
321         m_clock_mode = true;
322         // we are switching to clock mode: latch the current value of
323         // the decrementer register
324         if (m_clock_register != 0)
325            m_clock_read_register = m_decrementer_value;
412326         else
413         {
414            /* we are quitting clock mode */
415         }
327            m_clock_read_register = 0;      /* timer inactive... */
416328      }
417329      break;
418330   case 0x01:
r14513r14514
429341   case 0x0C:
430342   case 0x0D:
431343   case 0x0E:
432      /*
433            write one bit to 9901 (bits 1-14)
434
435            mode9901==0 ?  Disable/Enable an interrupt
436                        :  Bit in clock interval
437        */
438      /* offset is the index of the modified bit of register (-> interrupt number -1) */
439      if (tms->mode9901)
344      // write one bit to 9901 (bits 1-14)
345      //
346      // m_clock_mode==false ?  Disable/Enable an interrupt
347      // :  Bit in clock interval
348      //
349      // offset is the index of the modified bit of register (-> interrupt number -1)
350      if (m_clock_mode)
440351      {   /* modify clock interval */
441352         int mask = 1 << ((offset & 0x0F) - 1);   /* corresponding mask */
442353
443354         if (data)
444            tms->clockinvl |= mask;      /* set bit */
355            m_clock_register |= mask;      /* set bit */
445356         else
446            tms->clockinvl &= ~ mask;      /* clear bit */
357            m_clock_register &= ~mask;      /* clear bit */
447358
448         /* reset clock timer */
449         tms9901_timer_reload(device);
359         /* reset clock timer (page 8) */
360         timer_reload();
450361      }
451362      else
452363      {   /* modify interrupt enable mask */
453364         int mask = 1 << (offset & 0x0F);   /* corresponding mask */
454365
455366         if (data)
456            tms->enabled_ints |= mask;      /* set bit */
367            m_enabled_ints |= mask;      /* set bit */
457368         else
458            tms->enabled_ints &= ~ mask;      /* unset bit */
369            m_enabled_ints &= ~mask;      /* unset bit */
459370
460371         if (offset == 3)
461            tms->timer_int_pending = FALSE;   /* SBO 3 clears pending timer interrupt (??) */
372            m_timer_int_pending = false;   /* SBO 3 clears pending timer interrupt (??) */
462373
463         tms9901_field_interrupts(device);      /* changed interrupt state */
374         field_interrupts();      /* changed interrupt state */
464375      }
465376      break;
466377   case 0x0F:
467      if (tms->mode9901)
468      {   /* in clock mode, this is the soft reset bit */
469         if (! data)
470         {   /* TMS9901 soft reset */
471            /* all output pins are input pins again */
472            tms->pio_direction = 0;
473            tms->pio_direction_mirror = 0;
378      if (m_clock_mode)
379      {   /* in clock mode this is the soft reset bit */
380         if (!data)
381         {   // TMS9901 soft reset (RST2*)
382            // Spec: "Writing a 0 to bit 15 while in the clock mode executes a soft reset on the I/O pins.
383            // [...] RST2* will program all ports to the input mode"
384            m_pio_direction = 0;
385            m_pio_direction_mirror = 0;
474386
475            tms->enabled_ints = 0;   /* right??? */
387            // "RST1* (power-up reset) will reset all mask bits low."
388            // Spec is not clear on whether the mask bits are also reset by RST2*
389            // TODO: Check on a real machine. (I'd guess from the text they are not touched)
390            m_enabled_ints = 0;
476391         }
477392      }
478393      else
479394      {   /* modify interrupt enable mask */
480395         if (data)
481            tms->enabled_ints |= 0x4000;      /* set bit */
396            m_enabled_ints |= 0x4000;      /* set bit */
482397         else
483            tms->enabled_ints &= ~0x4000;      /* unset bit */
398            m_enabled_ints &= ~0x4000;      /* unset bit */
484399
485         tms9901_field_interrupts(device);      /* changed interrupt state */
400         field_interrupts();      /* changed interrupt state */
486401      }
487402      break;
488403   case 0x10:
r14513r14514
501416   case 0x1D:
502417   case 0x1E:
503418   case 0x1F:
504      {
505         int pin = offset & 0x0F;
506         int mask = (1 << pin);
419      int pin = offset & 0x0F;
420      int mask = (1 << pin);
507421
508         /* exit timer mode */
509         // MZ: dito
510         // tms->mode9901 = 0;
422      // MZ: see above - I think this is wrong
423      // m_clock_mode = false; // exit timer mode
511424
512         tms->pio_direction |= mask;         /* set up as output pin */
425      m_pio_direction |= mask;         /* set up as output pin */
513426
514         if (data)
515            tms->pio_output |= mask;
516         else
517            tms->pio_output &= ~ mask;
427      if (data)
428         m_pio_output |= mask;
429      else
430         m_pio_output &= ~mask;
518431
519         if (pin >= 7)
520         {   /* pins P7-P15 are mirrored as INT15*-INT7* */
521            int pin2 = 22 - pin;
522            int mask2 = (1 << pin2);
432      if (pin >= 7)
433      {   /* pins P7-P15 are mirrored as INT15*-INT7* */
434         int pin2 = 22 - pin;
435         int mask2 = (1 << pin2);
523436
524            tms->pio_direction_mirror |= mask2;   /* set up as output pin */
437         m_pio_direction_mirror |= mask2;   /* set up as output pin */
525438
526            if (data)
527               tms->pio_output_mirror |= mask2;
528            else
529               tms->pio_output_mirror &= ~ mask2;
530         }
531
532         if (tms->intf->write_handlers[pin] != NULL)
533            (* tms->intf->write_handlers[pin])(device, pin, data);
439         if (data)
440            m_pio_output_mirror |= mask2;
441         else
442            m_pio_output_mirror &= ~ mask2;
534443      }
535444
445      if (!m_write_line[pin].isnull())
446         (m_write_line[pin])(data);
447
536448      break;
537449   }
538450}
539451
540/*-------------------------------------------------
541    DEVICE_STOP( tms9901 )
542-------------------------------------------------*/
543
544static DEVICE_STOP( tms9901 )
452/*
453    Timer callback
454    Decrementer counts down the value set in clock mode; when it reaches 0,
455    raises an interrupt and resets to the start value
456    The decrementer works as long as the clock_register contains a non-zero value.
457*/
458void tms9901_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
545459{
546   tms9901_t *tms = get_token(device);
547
548   if (tms->timer)
460   if (id==DECREMENTER) // we have only that one
549461   {
550      tms->timer->reset();   /* FIXME - timers should only be allocated once */
551      tms->timer = NULL;
462      m_decrementer_value--;
463      if (m_decrementer_value<=0)
464      {
465         m_timer_int_pending = true;         // decrementer interrupt requested
466         field_interrupts();
467         m_decrementer_value = m_clock_register;
468      }
552469   }
553470}
554471
555472/*-------------------------------------------------
556    DEVICE_RESET( tms9901 )
473    DEVICE_STOP( tms9901 )
557474-------------------------------------------------*/
558475
559static DEVICE_RESET( tms9901 )
476void tms9901_device::device_stop(void)
560477{
561   tms9901_t *tms = get_token(device);
562
563   tms->timer_int_pending = 0;
564   tms->enabled_ints = 0;
565
566   tms->pio_direction = 0;
567   tms->pio_direction_mirror = 0;
568   tms->pio_output = tms->pio_output_mirror = 0;
569
570   tms->old_int_state = -1;
571   tms9901_field_interrupts(device);
572
573   tms->mode9901 = 0;
574
575   tms->clockinvl = 0;
576   tms9901_timer_reload(device);
577
578478}
579479
580
581480/*-------------------------------------------------
582    DEVICE_START( tms9901 )
481    DEVICE_RESET( tms9901 )
583482-------------------------------------------------*/
584483
585static DEVICE_START( tms9901 )
484void tms9901_device::device_reset(void)
586485{
587   tms9901_t *tms = get_token(device);
486   m_timer_int_pending = false;
487   m_enabled_ints = 0;
588488
589   assert(device != NULL);
590   assert(device->tag() != NULL);
591   assert(device->static_config() != NULL);
489   m_pio_direction = 0;
490   m_pio_direction_mirror = 0;
491   m_pio_output = m_pio_output_mirror = 0;
592492
593   tms->intf = (const tms9901_interface*)device->static_config();
493   m_int_state = 0;
494   m_old_int_state = -1;
495   field_interrupts();
594496
595   tms->timer = device->machine().scheduler().timer_alloc(FUNC(decrementer_callback), (void *) device);
497   m_clock_mode = false;
596498
597   tms->supported_int_mask = tms->intf->supported_int_mask;
598
599   tms->clock_rate = tms->intf->clock_rate;
600
601   tms->int_state = 0;
499   m_clock_register = 0;
500   timer_reload();
602501}
603502
604503
605
606504/*-------------------------------------------------
607    DEVICE_GET_INFO( tms9901 )
505    DEVICE_START( tms9901 )
608506-------------------------------------------------*/
609507
610DEVICE_GET_INFO( tms9901 )
508void tms9901_device::device_start(void)
611509{
612   switch (state)
613   {
614      /* --- the following bits of info are returned as 64-bit signed integers --- */
615      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(tms9901_t);            break;
616      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
510   const tms9901_interface *intf = reinterpret_cast<const tms9901_interface *>(static_config());
511   m_supported_int_mask = intf->interrupt_mask;
617512
618      /* --- the following bits of info are returned as pointers to data or functions --- */
619      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(tms9901);   break;
620      case DEVINFO_FCT_STOP:                     info->stop  = DEVICE_STOP_NAME (tms9901);   break;
621      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(tms9901);   break;
513   m_decrementer = timer_alloc(DECREMENTER);
514   m_decrementer->adjust(attotime::from_hz(clock() / 64.), 0, attotime::from_hz(clock() / 64.));
515   m_decrementer->enable(false);
622516
623      /* --- the following bits of info are returned as NULL-terminated strings --- */
624      case DEVINFO_STR_NAME:                     strcpy(info->s, "TMS9901");               break;
625      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "TMS9901");               break;
626      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                  break;
627      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
628      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
517   m_read_block.resolve(intf->read_handler, *this);
518
519   for (int i=0; i < 16; i++)
520   {
521      m_write_line[i].resolve(intf->write_handler[i], *this);
629522   }
523
524   m_interrupt.resolve(intf->interrupt_callback, *this);
630525}
631526
632DEFINE_LEGACY_DEVICE(TMS9901, tms9901);
527const device_type TMS9901 = &device_creator<tms9901_device>;
src/mess/machine/tms9901.h
r14513r14514
1/*
2    tms9901.h: header file for tms9901.c
1/****************************************************************************
32
3    TMS9901 Programmable System Interface
4    See tms9901.c for documentation
5
46    Raphael Nabet
5*/
7    Michael Zapf
8
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
613#ifndef __TMS9901_H__
714#define __TMS9901_H__
815
16#include "emu.h"
17
18extern const device_type TMS9901;
19
920/***************************************************************************
1021    MACROS
1122***************************************************************************/
1223
13DECLARE_LEGACY_DEVICE(TMS9901, tms9901);
14
1524/* Masks for the interrupts levels available on TMS9901 */
1625#define TMS9901_INT1 0x0002
1726#define TMS9901_INT2 0x0004
18#define TMS9901_INT3 0x0008      /* overriden by the timer interrupt */
27#define TMS9901_INT3 0x0008      // overriden by the timer interrupt
1928#define TMS9901_INT4 0x0010
2029#define TMS9901_INT5 0x0020
2130#define TMS9901_INT6 0x0040
r14513r14514
2938#define TMS9901_INTE 0x4000
3039#define TMS9901_INTF 0x8000
3140
41enum
42{
43   TMS9901_CB_INT7 = 0,
44   TMS9901_INT8_INT15 = 1,
45   TMS9901_P0_P7 = 2,
46   TMS9901_P8_P15 = 3
47};
48
3249/***************************************************************************
33    TYPE DEFINITIONS
50    CLASS DEFINITION
3451***************************************************************************/
3552
36typedef void (*tms9901_int_callback_func)(device_t *device, int intreq, int ic);
37#define TMS9901_INT_CALLBACK(name)   void name(device_t *device, int intreq, int ic )
53typedef struct _tms9901_interface
54{
55   int             interrupt_mask;         // a bit for each input pin whose state is always notified to the TMS9901 core
56   devcb_read8       read_handler;         // 4*8 bits, to be selected using the offset (0-3)
57   devcb_write_line   write_handler[16];      // 16 Pn outputs
58   devcb_write8      interrupt_callback;    // called when interrupt bus state changes
59} tms9901_interface;
3860
39typedef struct _tms9901_interface tms9901_interface;
40struct _tms9901_interface
61class tms9901_device : public device_t
4162{
42   int supported_int_mask;   /* a bit for each input pin whose state is always notified to the TMS9901 core */
43   read8_device_func  read_handlers[4];     /* 4*8 bits */
44   write8_device_func write_handlers[16]; /* 16 Pn outputs */
45   tms9901_int_callback_func interrupt_callback; /* called when interrupt bus state changes */
46   double clock_rate;
47};
63public:
64   tms9901_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
4865
66   void set_single_int(int pin_number, int state);
4967
50/***************************************************************************
51    FUNCTION PROTOTYPES
52***************************************************************************/
68   DECLARE_READ8_MEMBER( read );
69   DECLARE_WRITE8_MEMBER( write );
5370
54void tms9901_set_single_int(device_t *device, int pin_number, int state);
71private:
72   static const device_timer_id DECREMENTER = 0;
5573
56READ8_DEVICE_HANDLER ( tms9901_cru_r );
57WRITE8_DEVICE_HANDLER( tms9901_cru_w );
74   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void timer_reload(void);
76   void field_interrupts(void);
5877
78   void device_start(void);
79   void device_stop(void);
80   void device_reset(void);
81
82   /* interrupt registers */
83   // mask:  bit #n is set if pin #n is supported as an interrupt pin,
84   // i.e. the driver sends a notification whenever the pin state changes
85   // setting these bits is not required, but it saves you the trouble of
86   // saving the state of interrupt pins and feeding it to the port read
87   // handlers again
88   int m_supported_int_mask;
89   int m_int_state;         // state of the int1-int15 lines (must be inverted when queried)
90   int m_old_int_state;      // stores the previous value to avoid useless INT line assertions
91   int m_enabled_ints;         // interrupt enable mask
92
93   bool m_int_pending;         // status of the int* pin (connected to TMS9900)
94   bool m_timer_int_pending;   // timer int pending (overrides int3 pin if timer enabled)
95
96   // PIO registers
97   int m_pio_direction;      // direction register for PIO
98
99   // current PIO output (to be masked with pio_direction)
100   int m_pio_output;
101
102   // mirrors used for INT7*-INT15*
103   int m_pio_direction_mirror;
104   int m_pio_output_mirror;
105
106   // =======================================================================
107
108   // TMS9901 clock mode
109   // false = so-called interrupt mode (read interrupt state, write interrupt enable mask)
110   // true = clock mode (read/write clock interval)
111   bool m_clock_mode;
112
113   // MESS timer, used to emulate the decrementer register
114   emu_timer *m_decrementer;
115
116   // clock interval, loaded in decrementer when it reaches 0.
117   // 0 means decrementer off
118   int m_clock_register;
119
120   // Current decrementer value
121   int m_decrementer_value;
122
123   // when we go into timer mode, the decrementer is copied there to allow to read it reliably
124   int m_clock_read_register;
125
126   // =======================================================================
127
128   // Callbacks
129   devcb_resolved_read8      m_read_block;
130   devcb_resolved_write_line   m_write_line[16];
131   devcb_resolved_write8      m_interrupt;  // also delivers the interrupt level
132};
133
59134/***************************************************************************
60135    DEVICE CONFIGURATION MACROS
61136***************************************************************************/
62137
63#define MCFG_TMS9901_ADD(_tag, _intrf) \
64   MCFG_DEVICE_ADD(_tag, TMS9901, 0) \
138#define MCFG_TMS9901_ADD(_tag, _intrf, _rate) \
139   MCFG_DEVICE_ADD(_tag, TMS9901, _rate) \
65140   MCFG_DEVICE_CONFIG(_intrf)
66141
67142#endif /* __TMS9901_H__ */
src/mess/machine/tms9902.c
r14513r14514
1/*
1/****************************************************************************
2
23    TMS9902 Asynchronous Communication Controller
34
45    TMS9902 is an asynchronous serial controller for use with the TI990 and
r14513r14514
3132
3233    Raphael Nabet, 2003
3334    Michael Zapf, 2011
34*/
35    February 2012: Rewritten as class
3536
36#include <math.h>
37#include "emu.h"
37*****************************************************************************/
3838
39#include <math.h>
3940#include "tms9902.h"
4041
4142#define VERBOSE 1
4243#define LOG logerror
4344
45enum
46{
47   DECTIMER,
48   RECVTIMER,
49   SENDTIMER
50};
51
4452// Polling frequency. We use a much higher value to allow for line state changes
4553// happening between character transmissions (which happen in parallel in real
4654// communications but which must be serialized here)
4755#define POLLING_FREQ 20000
4856
49/***************************************************************************
50    TYPE DEFINITIONS
51***************************************************************************/
5257
53typedef struct _tms9902_t tms9902_t;
54struct _tms9902_t
58/*
59    Constructor
60*/
61tms9902_device::tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
62: device_t(mconfig, TMS9902, "TMS9902 Asynchronous Communication Controller", tag, owner, clock)
5563{
56   /* driver-specific configuration */
57   /* tms9902 clock rate (PHI* pin, normally connected to TMS9900 Phi3*) */
58   // Official range is 2MHz-3.3MHz.  Some tms9902s were sold as "MP9214", and
59   // were tested for speeds up to 4MHz, provided the clk4m control bit is set.
60   // (warning: 3MHz on a tms9900 is equivalent to 12MHz on a tms9995 or tms99000)
61   double clock_rate;
62
63   /* Modes */
64   bool LDCTRL;      // Load control register
65   bool LDIR;         // Load interval register
66   bool LRDR;         // Load receive data register
67   bool LXDR;         // Load transmit data register
68   bool TSTMD;         // Test mode
69
70   /* output pin */
71   bool RTSON;         // RTS-on request
72
73   /* transmitter registers */
74   bool BRKON;         // BRK-on request
75   bool BRKout;      // indicates the current BRK state
76
77   UINT8 XBR;         // transmit buffer register
78   UINT8 XSR;         // transmit shift register
79
80   /* receiver registers */
81   UINT8 RBR;         // Receive buffer register
82
83   /* Interrupt enable flags */
84   bool DSCENB;      // Data set change interrupt enable
85   bool RIENB;         // Receiver interrupt enable
86   bool XBIENB;      // Tansmit buffer interrupt enable
87   bool TIMENB;      // Timer interrupt enable
88
89   /*
90        Rate registers. The receive bit rate calculates as
91        bitrate = clock1 / (2 * (8 ^ RDV8) * RDR)
92        (similarly for transmit)
93
94        where clock1 = clock_rate / (CLK4M? 4:3)
95    */
96   UINT16 RDR;         // Receive data rate
97   bool RDV8;         // Receive data rate divider
98   UINT16 XDR;         // Transmit data rate
99   bool XDV8;         // Transmit data rate divider
100
101   /* Status flags */
102   bool INT;         // mirrors /INT output line, inverted
103   bool DSCH;         // Data set status change
104
105   bool CTSin;         // Inverted /CTS input (i.e. CTS)
106   bool DSRin;         // Inverted /DSR input (i.e. DSR)
107   bool RTSout;      // Current inverted /RTS line state (i.e. RTS)
108
109   bool TIMELP;      // Timer elapsed
110   bool TIMERR;      // Timer error
111
112   bool XSRE;         // Transmit shift register empty
113   bool XBRE;         // Transmit buffer register empty
114   bool RBRL;         // Receive buffer register loaded
115
116   bool RIN;         // State of the RIN pin
117   bool RSBD;         // Receive start bit detect
118   bool RFBD;         // Receive full bit detect
119   bool RFER;         // Receive framing error
120   bool ROVER;         // Receiver overflow
121   bool RPER;         // Receive parity error
122
123   UINT8 RCL;         // Character length
124   bool ODDP;
125   bool PENB;
126   UINT8 STOPB;
127   bool CLK4M;      // /PHI input divide select
128
129   UINT8 TMR;      /* interval timer */
130
131   /* clock registers */
132   emu_timer *dectimer;         /* MESS timer, used to emulate the decrementer register */
133   emu_timer *recvtimer;
134   emu_timer *sendtimer;
135
136   // This value is the ratio of data input versus the poll rate. The
137   // data source should deliver data bytes at every 1/baudpoll call.
138   // This is to ensure that data is delivered at a rate that is expected
139   // from the emulated program.
140   double baudpoll;
141
142   /* Pointer to interface */
143   const tms9902_interface *intf;
144};
145
146static void initiate_transmit(device_t *device);
147static void send_break(device_t *device, bool state);
148static DEVICE_RESET( tms9902 );
149
150/***************************************************************************
151    INLINE FUNCTIONS
152***************************************************************************/
153
154INLINE tms9902_t *get_token(device_t *device)
155{
156   assert(device != NULL);
157   assert(device->type() == TMS9902);
158   return (tms9902_t *) downcast<legacy_device_base *>(device)->token();
15964}
16065
161
16266/*
16367    should be called after any change to int_state or enabled_ints.
16468*/
165static void field_interrupts(device_t *device)
69void tms9902_device::field_interrupts()
16670{
167   tms9902_t *tms9902 = get_token(device);
168   bool new_int = (tms9902->DSCH && tms9902->DSCENB)
169                     || (tms9902->RBRL && tms9902->RIENB)
170                     || (tms9902->XBRE && tms9902->XBIENB)
171                     || (tms9902->TIMELP && tms9902->TIMENB);
71   bool new_int = (m_DSCH && m_DSCENB)
72                     || (m_RBRL && m_RIENB)
73                     || (m_XBRE && m_XBIENB)
74                     || (m_TIMELP && m_TIMENB);
17275// if (VERBOSE>3) LOG("TMS9902: interrupt flags (DSCH = %02x, DSCENB = %02x), (RBRL = %02x, RIENB = %02x), (XBRE = %02x, XBIENB = %02x), (TIMELP = %02x, TIMENB = %02x)\n",
173//  tms9902->DSCH, tms9902->DSCENB, tms9902->RBRL, tms9902->RIENB, tms9902->XBRE, tms9902->XBIENB, tms9902->TIMELP, tms9902->TIMENB);
174   if (new_int != tms9902->INT)
76//  m_DSCH, m_DSCENB, m_RBRL, m_RIENB, m_XBRE, m_XBIENB, m_TIMELP, m_TIMENB);
77   if (new_int != m_INT)
17578   {
17679      // Only consider edges
177      tms9902->INT = new_int;
178      if (VERBOSE>3) LOG("TMS9902: /INT = %s\n", (tms9902->INT)? "asserted" : "cleared");
179      if (tms9902->intf->int_callback)
180         (*tms9902->intf->int_callback)(device, tms9902->INT? ASSERT_LINE : CLEAR_LINE);
80      m_INT = new_int;
81      if (VERBOSE>3) LOG("TMS9902: /INT = %s\n", (m_INT)? "asserted" : "cleared");
82      int_callback(m_INT? ASSERT_LINE : CLEAR_LINE);
18183   }
18284}
18385
r14513r14514
18587    Called whenever the incoming CTS* line changes. This should be called by
18688    the device that contains the UART.
18789*/
188void tms9902_rcv_cts(device_t *device, line_state state)
90void tms9902_device::rcv_cts(line_state state)
18991{
190   tms9902_t *tms9902 = get_token(device);
191   bool previous = tms9902->CTSin;
92   bool previous = m_CTSin;
19293
19394   // CTSin is an internal register of the TMS9902 with positive logic
194   tms9902->CTSin = (state==ASSERT_LINE);
95   m_CTSin = (state==ASSERT_LINE);
19596
19697   if (VERBOSE>3) LOG("TMS9902: CTS* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
19798
198   if (tms9902->CTSin != previous)
99   if (m_CTSin != previous)
199100   {
200      tms9902->DSCH = true;
201      field_interrupts(device);
101      m_DSCH = true;
102      field_interrupts();
202103
203104      // If CTS becomes asserted and we have been sending
204      if (state==ASSERT_LINE && tms9902->RTSout)
105      if (state==ASSERT_LINE && m_RTSout)
205106      {
206107         // and if the byte buffer is empty
207         if (tms9902->XBRE)
108         if (m_XBRE)
208109         {
209110            // and we want to have a BRK, send it
210            if (tms9902->BRKON)
211               send_break(device, true);
111            if (m_BRKON) send_break(true);
212112         }
213113         else
214114         {
215115            // Buffer is not empty, we can send it
216116            // If the shift register is empty, transfer the data
217            if (tms9902->XSRE && !tms9902->BRKout)
117            if (m_XSRE && !m_BRKout)
218118            {
219               initiate_transmit(device);
119               initiate_transmit();
220120            }
221121         }
222122      }
223123   }
224124   else
225125   {
226      tms9902->DSCH = false;
126      m_DSCH = false;
227127      if (VERBOSE>4) LOG("TMS9902: no change in CTS line, no interrupt.");
228128   }
229129}
230130
231void tms9902_clock(device_t *device, bool state)
131void tms9902_device::set_clock(bool state)
232132{
233   tms9902_t *tms9902 = get_token(device);
234133   if (state)
235      tms9902->recvtimer->adjust(attotime::from_msec(1), 0, attotime::from_hz(POLLING_FREQ));
134      m_recvtimer->adjust(attotime::from_msec(1), 0, attotime::from_hz(POLLING_FREQ));
236135   else
237      tms9902->recvtimer->reset();
136      m_recvtimer->reset();
238137}
239138
240139/*
241140    Called whenever the incoming DSR* line changes. This should be called by
242141    the device that contains the UART.
243142*/
244void tms9902_rcv_dsr(device_t *device, line_state state)
143void tms9902_device::rcv_dsr(line_state state)
245144{
246   tms9902_t *tms9902 = get_token(device);
247   bool previous = tms9902->DSRin;
145   bool previous = m_DSRin;
248146   if (VERBOSE>3) LOG("TMS9902: DSR* = %s\n", (state==ASSERT_LINE)? "asserted" : "cleared");
249   tms9902->DSRin = (state==ASSERT_LINE);
147   m_DSRin = (state==ASSERT_LINE);
250148
251   if (tms9902->DSRin != previous)
149   if (m_DSRin != previous)
252150   {
253      tms9902->DSCH = true;
254      field_interrupts(device);
151      m_DSCH = true;
152      field_interrupts();
255153   }
256154   else
257155   {
258      tms9902->DSCH = false;
156      m_DSCH = false;
259157      if (VERBOSE>4) LOG("TMS9902: no change in DSR line, no interrupt.");
260158   }
261159}
r14513r14514
265163    the device that contains the UART. Unlike the real thing, we deliver
266164    complete bytes in one go.
267165*/
268void tms9902_rcv_data(device_t *device, UINT8 data)
166void tms9902_device::rcv_data(UINT8 data)
269167{
270   tms9902_t *tms9902 = get_token(device);
271
272168   // Put the received byte into the 1-byte receive buffer
273   tms9902->RBR = data;
169   m_RBR = data;
274170
275171   // Clear last errors
276   tms9902->RFER = false;
277   tms9902->RPER = false;
172   m_RFER = false;
173   m_RPER = false;
278174
279   if (!tms9902->RBRL)
175   if (!m_RBRL)
280176   {
281177      // Receive buffer was empty
282      tms9902->RBRL = true;
283      tms9902->ROVER = false;
178      m_RBRL = true;
179      m_ROVER = false;
284180      if (VERBOSE>3) LOG("TMS9902: Receive buffer loaded with byte %02x\n", data);
285      field_interrupts(device);
181      field_interrupts();
286182   }
287183   else
288184   {
289185      // Receive buffer was full
290      tms9902->ROVER = true;
186      m_ROVER = true;
291187      if (VERBOSE>1) LOG("TMS9902: Receive buffer still loaded; overflow error\n");
292188   }
293189}
r14513r14514
300196    as if it occurred here.
301197    The flag is reset by the next correctly received byte.
302198*/
303void tms9902_rcv_framing_error(device_t *device)
199void tms9902_device::rcv_framing_error()
304200{
305   tms9902_t *tms9902 = get_token(device);
306201   if (VERBOSE>2) LOG("TMS9902: Detected framing error\n");
307   tms9902->RFER = true;
202   m_RFER = true;
308203}
309204
310205/*
r14513r14514
313208    as if it occurred here.
314209    The flag is reset by the next correctly received byte.
315210*/
316void tms9902_rcv_parity_error(device_t *device)
211void tms9902_device::rcv_parity_error()
317212{
318   tms9902_t *tms9902 = get_token(device);
319213   if (VERBOSE>2) LOG("TMS9902: Detected parity error\n");
320   tms9902->RPER = true;
214   m_RPER = true;
321215}
322216
323217/*
r14513r14514
326220    of low level on the RIN pin which makes the chip re-synchronize on the
327221    next rising edge.
328222*/
329void tms9902_rcv_break(device_t *device, bool value)
223void tms9902_device::rcv_break(bool value)
330224{
331225   if (VERBOSE>2) LOG("TMS9902: Receive BREAK=%d (no effect)\n", value? 1:0);
332226}
333227
334228//------------------------------------------------
335229
336
337230/*
338    This call-back is called by the MESS timer system when the decrementer
339    reaches 0.
231    Timer callback
340232*/
341static TIMER_CALLBACK(decrementer_callback)
233void tms9902_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
342234{
343   device_t *device = (device_t *) ptr;
344   tms9902_t *tms9902 = get_token(device);
235   switch (id)
236   {
237   // This call-back is called by the MESS timer system when the decrementer
238   // reaches 0.
239   case DECTIMER:
240      m_TIMERR = m_TIMELP;
241      break;
345242
346   if (tms9902->TIMELP)
347      tms9902->TIMERR = true;
348   else
349      tms9902->TIMELP = false;
350}
243   //  Callback for the autonomous operations of the chip. This is normally
244   //  controlled by an external clock of 3-4 MHz, internally divided by 3 or 4,
245   //  depending on CLK4M. With this timer, reception of characters becomes
246   //  possible.
247   case RECVTIMER:
248      rcv_callback(ASSERT_LINE);
249      break;
351250
352/*
353    Callback for the autonomous operations of the chip. This is normally
354    controlled by an external clock of 3-4 MHz, internally divided by 3 or 4,
355    depending on CLK4M. With this timer, reception of characters becomes
356    possible.
357    We deliver the baudpoll value which allows the callback function to know
358    when the next data byte shall be delivered.
359*/
360static TIMER_CALLBACK(op_callback)
361{
362   device_t *device = (device_t *) ptr;
363   tms9902_t *tms9902 = get_token(device);
251   case SENDTIMER:
252      // Byte has been sent
253      m_XSRE = true;
364254
365   if (tms9902->intf->rcv_callback)
366      (*tms9902->intf->rcv_callback)(device, tms9902->baudpoll);
367}
368
369static TIMER_CALLBACK(sender_callback)
370{
371   device_t *device = (device_t *) ptr;
372   tms9902_t *tms9902 = get_token(device);
373   // Byte has been sent
374   tms9902->XSRE = true;
375
376   // In the meantime, the CPU may have pushed a new byte into the XBR
377   // so we loop until all data are transferred
378   if (!tms9902->XBRE && tms9902->CTSin)
379   {
380      initiate_transmit(device);
255      // In the meantime, the CPU may have pushed a new byte into the XBR
256      // so we loop until all data are transferred
257      if (!m_XBRE && m_CTSin)
258      {
259         initiate_transmit();
260      }
261      break;
381262   }
382263}
383264
384265/*
385266    load the content of clockinvl into the decrementer
386267*/
387static void reload_interval_timer(device_t *device)
268void tms9902_device::reload_interval_timer()
388269{
389   tms9902_t *tms9902 = get_token(device);
390   if (tms9902->TMR)
270   if (m_TMR)
391271   {   /* reset clock interval */
392      tms9902->dectimer->adjust(
393                  attotime::from_double((double) tms9902->TMR / (tms9902->clock_rate / ((tms9902->CLK4M) ? 4. : 3.) / 64.)),
272      m_dectimer->adjust(
273                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)),
394274                  0,
395                  attotime::from_double((double) tms9902->TMR / (tms9902->clock_rate / ((tms9902->CLK4M) ? 4. : 3.) / 64.)));
275                  attotime::from_double((double) m_TMR / (m_clock_rate / ((m_CLK4M) ? 4. : 3.) / 64.)));
396276   }
397277   else
398278   {   /* clock interval == 0 -> no timer */
399      tms9902->dectimer->enable(0);
279      m_dectimer->enable(0);
400280   }
401281}
402282
403static void send_break(device_t *device, bool state)
283void tms9902_device::send_break(bool state)
404284{
405   tms9902_t *tms9902 = get_token(device);
406
407   if (state != tms9902->BRKout)
285   if (state != m_BRKout)
408286   {
409      tms9902->BRKout = state;
287      m_BRKout = state;
410288      if (VERBOSE>2) LOG("TMS9902: Sending BREAK=%d\n", state? 1:0);
411289
412290      // Signal BRK (on/off) to the remote site
413      if (tms9902->intf->ctrl_callback)
414         (*tms9902->intf->ctrl_callback)(device, EXCEPT, BRK, state? 1:0);
291      ctrl_callback((EXCEPT | BRK), state? 1:0);
415292   }
416293}
417294
295/*
296    Baudpoll value allows the callback function to know when the next data byte shall be delivered.
297*/
298double tms9902_device::get_baudpoll()
299{
300   return m_baudpoll;
301}
302
418303// ==========================================================================
419304
420305/*
r14513r14514
429314
430315    CLK4M RDV8 RDR9 RDR8 | RDR7 RDR6 RDR5 RDR4 | RDR3 RDR2 RDR1 RDR0
431316*/
432static void set_receive_data_rate(device_t *device)
317void tms9902_device::set_receive_data_rate()
433318{
434   tms9902_t *tms9902 = get_token(device);
435   int value = (tms9902->CLK4M? 0x800 : 0) | (tms9902->RDV8? 0x400 : 0) | tms9902->RDR;
319   int value = (m_CLK4M? 0x800 : 0) | (m_RDV8? 0x400 : 0) | m_RDR;
436320   if (VERBOSE>3) LOG("TMS9902: receive rate = %04x\n", value);
437321
438322   // Calculate the ratio between receive baud rate and polling frequency
439   double fint = tms9902->clock_rate / ((tms9902->CLK4M) ? 4.0 : 3.0);
440   double baud = fint / (2.0 * ((tms9902->RDV8)? 8:1) * tms9902->RDR);
323   double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
324   double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
441325
442326   // We assume 10 bit per character (7 data usually add 1 parity; 1 start, 1 stop)
443327   // This value represents the ratio of data inputs of one poll.
444328   // Thus the callback function should add up this value on each poll
445329   // and deliver a data input not before it sums up to 1.
446   tms9902->baudpoll = (double)(baud / (10*POLLING_FREQ));
447   if (VERBOSE>3) LOG ("TMS9902: baudpoll = %lf\n", tms9902->baudpoll);
330   m_baudpoll = (double)(baud / (10*POLLING_FREQ));
331   if (VERBOSE>3) LOG ("TMS9902: baudpoll = %lf\n", m_baudpoll);
448332
449   if (tms9902->intf->ctrl_callback)
450      (*tms9902->intf->ctrl_callback)(device, CONFIG, RATERECV, value);
333   m_last_config_value = value;
334   ctrl_callback(CONFIG, RATERECV);
451335}
452336
453337/*
454338    Sets the data rate for the sender part. If a remote UART is attached,
455339    propagate this setting.
456340*/
457static void set_transmit_data_rate(device_t *device)
341void tms9902_device::set_transmit_data_rate()
458342{
459   tms9902_t *tms9902 = get_token(device);
460   int value = (tms9902->CLK4M? 0x800 : 0) | (tms9902->XDV8? 0x400 : 0) | tms9902->XDR;
343   int value = (m_CLK4M? 0x800 : 0) | (m_XDV8? 0x400 : 0) | m_XDR;
461344   if (VERBOSE>3) LOG("TMS9902: set transmit rate = %04x\n", value);
462   if (tms9902->intf->ctrl_callback)
463      (*tms9902->intf->ctrl_callback)(device, CONFIG, RATEXMIT, value);
345   m_last_config_value = value;
346   ctrl_callback(CONFIG, RATEXMIT);
464347}
465348
466static void set_stop_bits(device_t *device)
349void tms9902_device::set_stop_bits()
467350{
468   tms9902_t *tms9902 = get_token(device);
469   int value = tms9902->STOPB;
351   int value = m_STOPB;
470352   if (VERBOSE>3) LOG("TMS9902: set stop bits = %02x\n", value);
471   if (tms9902->intf->ctrl_callback)
472      (*tms9902->intf->ctrl_callback)(device, CONFIG, STOPBITS, value);
353   m_last_config_value = value;
354   ctrl_callback(CONFIG, STOPBITS);
473355}
474356
475static void set_data_bits(device_t *device)
357void tms9902_device::set_data_bits()
476358{
477   tms9902_t *tms9902 = get_token(device);
478   int value = tms9902->RCL;
359   int value = m_RCL;
479360   if (VERBOSE>3) LOG("TMS9902: set data bits = %02x\n", value);
480   if (tms9902->intf->ctrl_callback)
481      (*tms9902->intf->ctrl_callback)(device, CONFIG, DATABITS, value);
361   m_last_config_value = value;
362   ctrl_callback(CONFIG, DATABITS);
482363}
483364
484static void set_parity(device_t *device)
365void tms9902_device::set_parity()
485366{
486   tms9902_t *tms9902 = get_token(device);
487   int value = (tms9902->PENB? 2:0) | (tms9902->ODDP? 1:0);
367   int value = (m_PENB? 2:0) | (m_ODDP? 1:0);
488368   if (VERBOSE>3) LOG("TMS9902: set parity = %02x\n", value);
489   if (tms9902->intf->ctrl_callback)
490      (*tms9902->intf->ctrl_callback)(device, CONFIG, PARITY, value);
369   m_last_config_value = value;
370   ctrl_callback(CONFIG, PARITY);
491371}
492372
493static void transmit_line_state(device_t *device)
373void tms9902_device::transmit_line_state()
494374{
495   tms9902_t *tms9902 = get_token(device);
496
497375   // 00ab cdef = setting line RTS=a, CTS=b, DSR=c, DCD=d, DTR=e, RI=f
498376   // The 9902 only outputs RTS and BRK
499   if (VERBOSE>3) LOG("TMS9902: transmitting line state (only RTS) = %02x\n", (tms9902->RTSout)? 1:0);
500
501   if (tms9902->intf->ctrl_callback)
502      (*tms9902->intf->ctrl_callback)(device, LINES, RTS, (tms9902->RTSout)? RTS : 0);
377   if (VERBOSE>3) LOG("TMS9902: transmitting line state (only RTS) = %02x\n", (m_RTSout)? 1:0);
378   m_last_config_value = (m_RTSout)? RTS : 0;
379   ctrl_callback(LINES, RTS);
503380}
504381
505static void set_rts(device_t *device, line_state state)
382void tms9902_device::set_rts(line_state state)
506383{
507   tms9902_t *tms9902 = get_token(device);
508384   bool lstate = (state==ASSERT_LINE);
509385
510   if (lstate != tms9902->RTSout)
386   if (lstate != m_RTSout)
511387   {
512388      // Signal RTS to the modem
513389      if (VERBOSE>3) LOG("TMS9902: Set RTS=%d\n", lstate? 1:0);
514      tms9902->RTSout = lstate;
515      transmit_line_state(device);
390      m_RTSout = lstate;
391      transmit_line_state();
516392   }
517393}
518394
395int tms9902_device::get_config_value()
396{
397   return m_last_config_value;
398}
399
519400// ==========================================================================
520401
521static void initiate_transmit(device_t *device)
402void tms9902_device::initiate_transmit()
522403{
523   tms9902_t *tms9902 = get_token(device);
524
525   if (tms9902->BRKON && tms9902->CTSin)
404   if (m_BRKON && m_CTSin)
526405      /* enter break mode */
527      send_break(device, true);
406      send_break(true);
528407   else
529408   {
530      if (!tms9902->RTSON && (!tms9902->CTSin || (tms9902->XBRE && !tms9902->BRKout)))
409      if (!m_RTSON && (!m_CTSin || (m_XBRE && !m_BRKout)))
531410         /* clear RTS output */
532         set_rts(device, CLEAR_LINE);
411         set_rts(CLEAR_LINE);
533412      else
534413      {
535414         if (VERBOSE>5) LOG("TMS9902: transferring XBR to XSR; XSRE=false, XBRE=true\n");
536         tms9902->XSR = tms9902->XBR;
537         tms9902->XSRE = false;
538         tms9902->XBRE = true;
415         m_XSR = m_XBR;
416         m_XSRE = false;
417         m_XBRE = true;
539418
540         field_interrupts(device);
419         field_interrupts();
541420
542         if (VERBOSE>4) LOG("TMS9902: transmit XSR=%02x, RCL=%02x\n", tms9902->XSR, tms9902->RCL);
421         if (VERBOSE>4) LOG("TMS9902: transmit XSR=%02x, RCL=%02x\n", m_XSR, m_RCL);
543422
544         if (tms9902->intf->xmit_callback)
545            (*tms9902->intf->xmit_callback)(device, tms9902->XSR & (0xff >> (3-tms9902->RCL)));
423         xmit_callback(0, m_XSR & (0xff >> (3-m_RCL)));
546424
547425         // Should store that somewhere (but the CPU is fast enough, can afford to recalc :-) )
548         double fint = tms9902->clock_rate / ((tms9902->CLK4M) ? 4.0 : 3.0);
549         double baud = fint / (2.0 * ((tms9902->RDV8)? 8:1) * tms9902->RDR);
426         double fint = m_clock_rate / ((m_CLK4M) ? 4.0 : 3.0);
427         double baud = fint / (2.0 * ((m_RDV8)? 8:1) * m_RDR);
550428
551429         // Time for transmitting 10 bit (8 bit + start + stop)
552         tms9902->sendtimer->adjust(attotime::from_hz(baud/10.0));
430         m_sendtimer->adjust(attotime::from_hz(baud/10.0));
553431      }
554432   }
555433}
r14513r14514
573451    bit 13-15: not emulated, normally used for diagnostics
574452    bit 16: RBINT (RBRL&RIENB)
575453*/
576READ8_DEVICE_HANDLER( tms9902_cru_r )
454READ8_MEMBER( tms9902_device::cruread )
577455{
578456   UINT8 answer = 0;
579   tms9902_t *tms9902 = get_token(device);
580457
581458   offset &= 0x0003;
582459
583460   switch (offset)
584461   {
585462   case 3: // Bits 31-24
586      if (tms9902->INT) answer |= 0x80;
587      if (tms9902->LDCTRL || tms9902->LDIR || tms9902->LRDR || tms9902->LXDR || tms9902->BRKON) answer |= 0x40;
588      if (tms9902->DSCH) answer |= 0x20;
589      if (tms9902->CTSin) answer |= 0x10;
590      if (tms9902->DSRin) answer |= 0x08;
591      if (tms9902->RTSout) answer |= 0x04;
592      if (tms9902->TIMELP)  answer |= 0x02;
593      if (tms9902->TIMERR)  answer |= 0x01;
463      if (m_INT) answer |= 0x80;
464      if (m_LDCTRL || m_LDIR || m_LRDR || m_LXDR || m_BRKON) answer |= 0x40;
465      if (m_DSCH) answer |= 0x20;
466      if (m_CTSin) answer |= 0x10;
467      if (m_DSRin) answer |= 0x08;
468      if (m_RTSout) answer |= 0x04;
469      if (m_TIMELP)  answer |= 0x02;
470      if (m_TIMERR)  answer |= 0x01;
594471      break;
595472
596473   case 2: // Bits 23-16
597      if (tms9902->XSRE) answer |= 0x80;
598      if (tms9902->XBRE) answer |= 0x40;
599      if (tms9902->RBRL) answer |= 0x20;
600      if (tms9902->DSCH && tms9902->DSCENB) answer |= 0x10;
601      if (tms9902->TIMELP && tms9902->TIMENB) answer |= 0x08;
602      if (tms9902->XBRE && tms9902->XBIENB) answer |= 0x02;
603      if (tms9902->RBRL && tms9902->RIENB) answer |= 0x01;
474      if (m_XSRE) answer |= 0x80;
475      if (m_XBRE) answer |= 0x40;
476      if (m_RBRL) answer |= 0x20;
477      if (m_DSCH && m_DSCENB) answer |= 0x10;
478      if (m_TIMELP && m_TIMENB) answer |= 0x08;
479      if (m_XBRE && m_XBIENB) answer |= 0x02;
480      if (m_RBRL && m_RIENB) answer |= 0x01;
604481      break;
605482
606483   case 1: // Bits 15-8
607      if (tms9902->RIN) answer |= 0x80;
608      if (tms9902->RSBD) answer |= 0x40;
609      if (tms9902->RFBD) answer |= 0x20;
610      if (tms9902->RFER) answer |= 0x10;
611      if (tms9902->ROVER) answer |= 0x08;
612      if (tms9902->RPER) answer |= 0x04;
613      if (tms9902->RPER || tms9902->RFER || tms9902->ROVER) answer |= 0x02;
484      if (m_RIN) answer |= 0x80;
485      if (m_RSBD) answer |= 0x40;
486      if (m_RFBD) answer |= 0x20;
487      if (m_RFER) answer |= 0x10;
488      if (m_ROVER) answer |= 0x08;
489      if (m_RPER) answer |= 0x04;
490      if (m_RPER || m_RFER || m_ROVER) answer |= 0x02;
614491      break;
615492
616493   case 0: // Bits 7-0
617      answer = tms9902->RBR;
494      answer = m_RBR;
618495      break;
619496   }
620497   if (VERBOSE>7) LOG("TMS9902: Reading flag bits %d - %d = %02x\n", ((offset+1)*8-1), offset*8, answer);
r14513r14514
637514      *reg &= ~bits;
638515}
639516
640static void reset_uart(device_t *device)
517void tms9902_device::reset_uart()
641518{
642   tms9902_t *tms9902 = get_token(device);
643519   if (VERBOSE>1) LOG("TMS9902: resetting\n");
644520
645521   /*  disable all interrupts */
646   tms9902->DSCENB = false;   // Data Set Change Interrupt Enable
647   tms9902->TIMENB = false;   // Timer Interrupt Enable
648   tms9902->XBIENB = false;   // Transmit Buffer Interrupt Enable
649   tms9902->RIENB = false;      // Read Buffer Interrupt Enable
522   m_DSCENB = false;   // Data Set Change Interrupt Enable
523   m_TIMENB = false;   // Timer Interrupt Enable
524   m_XBIENB = false;   // Transmit Buffer Interrupt Enable
525   m_RIENB = false;      // Read Buffer Interrupt Enable
650526
651527   /* initialize transmitter */
652   tms9902->XBRE = true;      // Transmit Buffer Register Empty
653   tms9902->XSRE = true;      // Transmit Shift Register Empty
528   m_XBRE = true;      // Transmit Buffer Register Empty
529   m_XSRE = true;      // Transmit Shift Register Empty
654530
655531   /* initialize receiver */
656   tms9902->RBRL = false;      // Read Buffer Register Loaded
532   m_RBRL = false;      // Read Buffer Register Loaded
657533
658534   /* clear RTS */
659   tms9902->RTSON = false;      // Request-to-send on (flag)
660   tms9902->RTSout = true;      // Note we are doing this to ensure the state is sent to the interface
661   set_rts(device, CLEAR_LINE);
662   tms9902->RTSout = false;   // what we actually want
535   m_RTSON = false;      // Request-to-send on (flag)
536   m_RTSout = true;      // Note we are doing this to ensure the state is sent to the interface
537   set_rts(CLEAR_LINE);
538   m_RTSout = false;   // what we actually want
663539
664540   /* set all register load flags to 1 */
665   tms9902->LDCTRL = true;
666   tms9902->LDIR = true;
667   tms9902->LRDR = true;
668   tms9902->LXDR = true;
541   m_LDCTRL = true;
542   m_LDIR = true;
543   m_LRDR = true;
544   m_LXDR = true;
669545
670546   /* clear break condition */
671   tms9902->BRKON = false;
672   tms9902->BRKout = false;
547   m_BRKON = false;
548   m_BRKout = false;
673549
674   field_interrupts(device);
550   m_TMR = 0;
551   m_STOPB = 0;
552   m_RCL = 0;
553   m_XDR = 0;
554   m_RDR = 0;
555   m_RBR = 0;
556   m_XBR = 0;
557   m_XSR = 0;
558
559   field_interrupts();
675560}
676561
677562/*
678563    TMS9902 CRU write
679564*/
680WRITE8_DEVICE_HANDLER( tms9902_cru_w )
565WRITE8_MEMBER( tms9902_device::cruwrite )
681566{
682   tms9902_t *tms9902 = get_token(device);
683
684567   data &= 1;   /* clear extra bits */
685568
686569   offset &= 0x1F;
r14513r14514
690573   {
691574      UINT16 mask = (1 << offset);
692575
693      if (tms9902->LDCTRL)
576      if (m_LDCTRL)
694577      {   // Control Register mode. Values written to bits 0-7 are copied
695578         // into the control register.
696579         switch (offset)
697580         {
698581         case 0:
699            set_bits8(&tms9902->RCL, 0x01, (data!=0));
582            set_bits8(&m_RCL, 0x01, (data!=0));
700583            // we assume that bits are written in increasing order
701584            // so we do not transmit the data bits twice
702585            // (will fail when bit 1 is written first)
703586            break;
704587         case 1:
705            set_bits8(&tms9902->RCL, 0x02, (data!=0));
706            set_data_bits(device);
588            set_bits8(&m_RCL, 0x02, (data!=0));
589            set_data_bits();
707590            break;
708591         case 2:
709592            break;
710593         case 3:
711            tms9902->CLK4M = (data!=0);
594            m_CLK4M = (data!=0);
712595            break;
713596         case 4:
714            tms9902->ODDP = (data!=0);
597            m_ODDP = (data!=0);
715598            // we also assume that the parity type is set before the parity enable
716599            break;
717600         case 5:
718            tms9902->PENB = (data!=0);
719            set_parity(device);
601            m_PENB = (data!=0);
602            set_parity();
720603            break;
721604         case 6:
722            set_bits8(&tms9902->STOPB, 0x01, (data!=0));
605            set_bits8(&m_STOPB, 0x01, (data!=0));
723606            break;
724607         case 7:
725            set_bits8(&tms9902->STOPB, 0x02, (data!=0));
608            set_bits8(&m_STOPB, 0x02, (data!=0));
726609            // When bit 7 is written the control register mode is automatically terminated.
727            tms9902->LDCTRL = false;
728            set_stop_bits(device);
610            m_LDCTRL = false;
611            set_stop_bits();
729612            break;
730613         default:
731614            if (VERBOSE>1) LOG("tms9902: Invalid control register address %d\n", offset);
732615         }
733616      }
734      else if (tms9902->LDIR)
617      else if (m_LDIR)
735618      {   // Interval Register mode. Values written to bits 0-7 are copied
736619         // into the interval register.
737620         if (offset <= 7)
738621         {
739            set_bits8(&tms9902->TMR, mask, (data!=0));
622            set_bits8(&m_TMR, mask, (data!=0));
740623
741624            if (offset == 7)
742625            {
743               reload_interval_timer(device);
626               reload_interval_timer();
744627               // When bit 7 is written the interval register mode is automatically terminated.
745               tms9902->LDIR = false;
628               m_LDIR = false;
746629            }
747630         }
748631      }
749      else if (tms9902->LRDR || tms9902->LXDR)
632      else if (m_LRDR || m_LXDR)
750633      {
751         if (tms9902->LRDR)
634         if (m_LRDR)
752635         {   // Receive rate register mode. Values written to bits 0-10 are copied
753636            // into the receive rate register.
754637            if (offset < 10)
755638            {
756               set_bits16(&tms9902->RDR, mask, (data!=0));
639               set_bits16(&m_RDR, mask, (data!=0));
757640            }
758641            else
759642            {
760643               // When bit 10 is written the receive register mode is automatically terminated.
761               tms9902->RDV8 = (data!=0);
762               tms9902->LRDR = false;
763               set_receive_data_rate(device);
644               m_RDV8 = (data!=0);
645               m_LRDR = false;
646               set_receive_data_rate();
764647            }
765648         }
766         if (tms9902->LXDR)
649         if (m_LXDR)
767650         {
768651            // The transmit rate register can be set together with the receive rate register.
769652            if (offset < 10)
770653            {
771               set_bits16(&tms9902->XDR, mask, (data!=0));
654               set_bits16(&m_XDR, mask, (data!=0));
772655            }
773656            else
774657            {
775658               // Note that the transmit rate register is NOT terminated when
776659               // writing bit 10. This must be done by unsetting bit 11.
777               tms9902->XDV8 = (data!=0);
778               set_transmit_data_rate(device);
660               m_XDV8 = (data!=0);
661               set_transmit_data_rate();
779662            }
780663         }
781664      }
r14513r14514
784667         // written to bits 0-7 are transferred into the transmit buffer register.
785668         if (offset <= 7)
786669         {
787            set_bits8(&tms9902->XBR, mask, (data!=0));
670            set_bits8(&m_XBR, mask, (data!=0));
788671
789672            if (offset == 7)
790673            {   /* transmit */
791               tms9902->XBRE = false;
674               m_XBRE = false;
792675               // Spec: When the transmitter is active, the contents of the Transmit
793676               // Buffer Register are transferred to the Transmit Shift Register
794677               // each time the previous character has been completely transmitted
795678               // We need to check XSRE=true as well, as the implementation
796679               // makes use of a timed transmission, during which XSRE=false
797               if (tms9902->XSRE && tms9902->RTSout && tms9902->CTSin && !tms9902->BRKout)
680               if (m_XSRE && m_RTSout && m_CTSin && !m_BRKout)
798681               {
799                  initiate_transmit(device);
682                  initiate_transmit();
800683               }
801684            }
802685         }
r14513r14514
806689   switch (offset)
807690   {
808691      case 11:
809         tms9902->LXDR = (data!=0);
692         m_LXDR = (data!=0);
810693         break;
811694      case 12:
812         tms9902->LRDR = (data!=0);
695         m_LRDR = (data!=0);
813696         break;
814697      case 13:
815         tms9902->LDIR = (data!=0);
698         m_LDIR = (data!=0);
816699         // Spec: Each time LDIR is reset the contents of the Interval
817700         // Register are loaded into the Interval Timer, thus restarting
818701         // the timer.
819702         if (data==0)
820            reload_interval_timer(device);
703            reload_interval_timer();
821704         break;
822705      case 14:
823         tms9902->LDCTRL = (data!=0);
706         m_LDCTRL = (data!=0);
824707         break;
825708      case 15:
826         tms9902->TSTMD = (data!=0); // Test mode not implemented
709         m_TSTMD = (data!=0); // Test mode not implemented
827710         break;
828711      case 16:
829712         if (data!=0)
830713         {
831            tms9902->RTSON = true;
832            set_rts(device, ASSERT_LINE);
833            if (tms9902->CTSin)
714            m_RTSON = true;
715            set_rts(ASSERT_LINE);
716            if (m_CTSin)
834717            {
835               if (tms9902->XSRE && !tms9902->XBRE && !tms9902->BRKout)
836                  initiate_transmit(device);
837               else if (tms9902->BRKON)
838                  send_break(device, true);
718               if (m_XSRE && !m_XBRE && !m_BRKout)
719                  initiate_transmit();
720               else if (m_BRKON)
721                  send_break(true);
839722            }
840723         }
841724         else
842725         {
843            tms9902->RTSON = false;
844            if (tms9902->XBRE && tms9902->XSRE && !tms9902->BRKout)
726            m_RTSON = false;
727            if (m_XBRE && m_XSRE && !m_BRKout)
845728            {
846               set_rts(device, CLEAR_LINE);
729               set_rts(CLEAR_LINE);
847730            }
848731         }
849732         return;
850733      case 17:
851         if (VERBOSE>3) LOG("TMS9902: set BRKON=%d; BRK=%d\n", data, tms9902->BRKout? 1:0);
852         tms9902->BRKON = (data!=0);
853         if (tms9902->BRKout && data==0)
734         if (VERBOSE>3) LOG("TMS9902: set BRKON=%d; BRK=%d\n", data, m_BRKout? 1:0);
735         m_BRKON = (data!=0);
736         if (m_BRKout && data==0)
854737         {
855738            // clear BRK
856            tms9902->BRKout = false;
857            if ((!tms9902->XBRE) && tms9902->CTSin)
739            m_BRKout = false;
740            if ((!m_XBRE) && m_CTSin)
858741            {
859742               /* transmit next byte */
860               initiate_transmit(device);
743               initiate_transmit();
861744            }
862            else if (!tms9902->RTSON)
745            else if (!m_RTSON)
863746            {
864747               /* clear RTS */
865               set_rts(device, CLEAR_LINE);
748               set_rts(CLEAR_LINE);
866749            }
867750         }
868         else if (tms9902->XBRE && tms9902->XSRE && tms9902->RTSout && tms9902->CTSin)
751         else if (m_XBRE && m_XSRE && m_RTSout && m_CTSin)
869752         {
870            send_break(device, (data!=0));
753            send_break(data!=0);
871754         }
872755         return;
873756      case 18:
874757         // Receiver Interrupt Enable
875758         // According to spec, (re)setting this flag clears the RBRL flag
876759         // (the only way to clear the flag!)
877         tms9902->RIENB = (data!=0);
878         tms9902->RBRL = false;
760         m_RIENB = (data!=0);
761         m_RBRL = false;
879762         if (VERBOSE>4) LOG("TMS9902: set RBRL=0, set RIENB=%d\n", data);
880         field_interrupts(device);
763         field_interrupts();
881764         return;
882765      case 19:
883766         /* Transmit Buffer Interrupt Enable */
884         tms9902->XBIENB = (data!=0);
767         m_XBIENB = (data!=0);
885768         if (VERBOSE>4) LOG("TMS9902: set XBIENB=%d\n", data);
886         field_interrupts(device);
769         field_interrupts();
887770         return;
888771      case 20:
889772         /* Timer Interrupt Enable */
890         tms9902->TIMENB = (data!=0);
891         tms9902->TIMELP = false;
892         tms9902->TIMERR = false;
893         field_interrupts(device);
773         m_TIMENB = (data!=0);
774         m_TIMELP = false;
775         m_TIMERR = false;
776         field_interrupts();
894777         return;
895778      case 21:
896779         /* Data Set Change Interrupt Enable */
897         tms9902->DSCENB = (data!=0);
898         tms9902->DSCH = false;
780         m_DSCENB = (data!=0);
781         m_DSCH = false;
899782         if (VERBOSE>4) LOG("TMS9902: set DSCH=0, set DSCENB=%d\n", data);
900         field_interrupts(device);
783         field_interrupts();
901784         return;
902785      case 31:
903786         /* RESET */
904         reset_uart(device);
787         reset_uart();
905788         return;
906789      default:
907790         if (VERBOSE>1) LOG("TMS9902: Writing to undefined flag bit position %d = %01x\n", offset, data);
r14513r14514
912795    DEVICE_STOP( tms9902 )
913796-------------------------------------------------*/
914797
915static DEVICE_STOP( tms9902 )
798void tms9902_device::device_stop()
916799{
917   tms9902_t *tms9902 = get_token(device);
918
919   if (tms9902->dectimer)
800   if (m_dectimer)
920801   {
921      tms9902->dectimer->reset();
922      tms9902->dectimer = NULL;
802      m_dectimer->reset();
803      m_dectimer = NULL;
923804   }
924805}
925806
r14513r14514
927808    DEVICE_RESET( tms9902 )
928809-------------------------------------------------*/
929810
930static DEVICE_RESET( tms9902 )
811void tms9902_device::device_reset()
931812{
932   reset_uart(device);
813   reset_uart();
933814}
934815
935
936816/*-------------------------------------------------
937817    DEVICE_START( tms9902 )
938818-------------------------------------------------*/
939819
940static DEVICE_START( tms9902 )
820void tms9902_device::device_start()
941821{
942   tms9902_t *tms9902 = get_token(device);
822   const tms9902_interface *intf = reinterpret_cast<const tms9902_interface *>(static_config());
943823
944   assert(device != NULL);
945   assert(device->tag() != NULL);
946   assert(device->static_config() != NULL);
824   m_clock_rate = clock();
947825
948   tms9902->intf = (const tms9902_interface*)device->static_config();
826   int_callback.resolve(intf->int_callback, *this);
827   rcv_callback.resolve(intf->rcv_callback, *this);
828   xmit_callback.resolve(intf->xmit_callback, *this);
829   ctrl_callback.resolve(intf->ctrl_callback, *this);
949830
950   tms9902->clock_rate = tms9902->intf->clock_rate;
951
952   tms9902->dectimer = device->machine().scheduler().timer_alloc(FUNC(decrementer_callback), (void *) device);
953   tms9902->recvtimer = device->machine().scheduler().timer_alloc(FUNC(op_callback), (void *) device);
954   tms9902->sendtimer = device->machine().scheduler().timer_alloc(FUNC(sender_callback), (void *) device);
831   m_dectimer = timer_alloc(DECTIMER);
832   m_recvtimer = timer_alloc(RECVTIMER);
833   m_sendtimer = timer_alloc(SENDTIMER);
955834}
956835
836const device_type TMS9902 = &device_creator<tms9902_device>;
957837
958
959/*-------------------------------------------------
960    DEVICE_GET_INFO( tms9902 )
961-------------------------------------------------*/
962
963DEVICE_GET_INFO( tms9902 )
964{
965   switch (state)
966   {
967      /* --- the following bits of info are returned as 64-bit signed integers --- */
968      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(tms9902_t);            break;
969      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
970
971      /* --- the following bits of info are returned as pointers to data or functions --- */
972      case DEVINFO_FCT_START:                     info->start = DEVICE_START_NAME(tms9902);   break;
973      case DEVINFO_FCT_STOP:                     info->stop  = DEVICE_STOP_NAME (tms9902);   break;
974      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME(tms9902);   break;
975
976      /* --- the following bits of info are returned as NULL-terminated strings --- */
977      case DEVINFO_STR_NAME:                     strcpy(info->s, "TMS9902");               break;
978      case DEVINFO_STR_FAMILY:                  strcpy(info->s, "TMS9902");               break;
979      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                  break;
980      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);               break;
981      case DEVINFO_STR_CREDITS:                  /* Nothing */                        break;
982   }
983}
984
985DEFINE_LEGACY_DEVICE(TMS9902, tms9902);
src/mess/machine/tms9902.h
r14513r14514
1#ifndef __TMS9902_H__
2#define __TMS9902_H__
1/****************************************************************************
32
4/***************************************************************************
5    MACROS
6***************************************************************************/
3    TMS9902 Asynchronous Communication Controller
4    See tms9902.c for documentation
75
8DECLARE_LEGACY_DEVICE(TMS9902, tms9902);
6    Michael Zapf
97
10/***************************************************************************
11    TYPE DEFINITIONS
12***************************************************************************/
8    February 2012: Rewritten as class
139
14typedef void (*tms9902_int_callback_func)(device_t *device, int state);
15#define TMS9902_INT_CALLBACK(name)   void name(device_t *device, int state )
10*****************************************************************************/
1611
17typedef void (*tms9902_rcv_callback_func)(device_t *device, double baudpoll);
18#define TMS9902_RCV_CALLBACK(name)   void name(device_t *device, double baudpoll)
12#ifndef __TMS9902_H__
13#define __TMS9902_H__
1914
20typedef void (*tms9902_xmit_callback_func)(device_t *device, int data);
21#define TMS9902_XMIT_CALLBACK(name)   void name(device_t *device, int data )
15#include "emu.h"
2216
23typedef void (*tms9902_ctrl_callback_func)(device_t *device, int type, int param, int value);
24#define TMS9902_CTRL_CALLBACK(name)   void name(device_t *device, int type, int param, int value )
25
2617// Serial control protocol values
2718#define TYPE_TMS9902 0x01
2819
r14513r14514
4940#define DTR 0x02
5041#define RI  0x01
5142
52typedef struct _tms9902_interface tms9902_interface;
53struct _tms9902_interface
43extern const device_type TMS9902;
44
45typedef struct _tms9902_interface
5446{
55   double clock_rate;                     /* clock rate (2MHz-3.3MHz, with 4MHz overclocking) */
56   tms9902_int_callback_func  int_callback;   /* called when interrupt pin state changes */
57   tms9902_rcv_callback_func  rcv_callback;   /* called when a character is received */
58   tms9902_xmit_callback_func xmit_callback;   /* called when a character is transmitted */
59   tms9902_ctrl_callback_func ctrl_callback;   /* called for configuration and line changes */
60};
47   devcb_write_line   int_callback;
48   devcb_write_line   rcv_callback;
49   devcb_write8      xmit_callback;
50   devcb_write8      ctrl_callback;
51} tms9902_interface;
6152
53class tms9902_device : public device_t
54{
55public:
56   tms9902_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
57   void   set_clock(bool state);
6258
63/***************************************************************************
64    FUNCTION PROTOTYPES
65***************************************************************************/
59   void   rcv_cts(line_state state);
60   void   rcv_dsr(line_state state);
61   void   rcv_data(UINT8 data);
62   void   rcv_break(bool value);
63   void   rcv_framing_error();
64   void   rcv_parity_error();
6665
67/*
68    Functions called by the modem.
69*/
70void tms9902_rcv_cts(device_t *device, line_state state);
71void tms9902_rcv_dsr(device_t *device, line_state state);
72void tms9902_rcv_data(device_t *device, UINT8 data);
66   double   get_baudpoll();
7367
74/*
75    Functions to set internal exception states, according to the states
76    as received by the serial bridge
77*/
78void tms9902_rcv_framing_error(device_t *device);
79void tms9902_rcv_parity_error(device_t *device);
80void tms9902_rcv_break(device_t *device, bool value);
68   int      get_config_value();
8169
82/*
83    Control functions
84*/
85void tms9902_clock(device_t *device, bool state);
70   DECLARE_READ8_MEMBER( cruread );
71   DECLARE_WRITE8_MEMBER( cruwrite );
8672
87READ8_DEVICE_HANDLER ( tms9902_cru_r );
88WRITE8_DEVICE_HANDLER( tms9902_cru_w );
73protected:
74   void   device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
75   void   device_start();
76   void   device_reset();
77   void   device_stop();
8978
79private:
80   void   field_interrupts();
81   void   reload_interval_timer();
82   void   send_break(bool state);
83   void   set_receive_data_rate();
84   void   set_transmit_data_rate();
85   void   set_stop_bits();
86   void   set_data_bits();
87   void   set_parity();
88   void   transmit_line_state();
89   void   set_rts(line_state state);
90   void   initiate_transmit();
91   void   reset_uart();
92
93   devcb_resolved_write_line      int_callback;
94   devcb_resolved_write_line      rcv_callback;
95   devcb_resolved_write8         xmit_callback;
96   devcb_resolved_write8         ctrl_callback;      // needs to be used with get_config_value
97
98   // tms9902 clock rate (PHI* pin, normally connected to TMS9900 Phi3*)
99   // Official range is 2MHz-3.3MHz.  Some tms9902s were sold as "MP9214", and
100   // were tested for speeds up to 4MHz, provided the clk4m control bit is set.
101   // (warning: 3MHz on a tms9900 is equivalent to 12MHz on a tms9995 or tms99000)
102   double   m_clock_rate;
103
104   /* Modes */
105   bool   m_LDCTRL;      // Load control register
106   bool   m_LDIR;         // Load interval register
107   bool   m_LRDR;         // Load receive data register
108   bool   m_LXDR;         // Load transmit data register
109   bool   m_TSTMD;         // Test mode
110
111   /* output pin */
112   bool   m_RTSON;         // RTS-on request
113
114   /* transmitter registers */
115   bool   m_BRKON;         // BRK-on request
116   bool   m_BRKout;      // indicates the current BRK state
117
118   UINT8   m_XBR;         // transmit buffer register
119   UINT8   m_XSR;         // transmit shift register
120
121   /* receiver registers */
122   UINT8   m_RBR;         // Receive buffer register
123
124   /* Interrupt enable flags */
125   bool   m_DSCENB;      // Data set change interrupt enable
126   bool   m_RIENB;         // Receiver interrupt enable
127   bool   m_XBIENB;      // Tansmit buffer interrupt enable
128   bool   m_TIMENB;      // Timer interrupt enable
129
130   /*
131        Rate registers. The receive bit rate calculates as
132        bitrate = clock1 / (2 * (8 ^ RDV8) * RDR)
133        (similarly for transmit)
134
135        where clock1 = clock_rate / (CLK4M? 4:3)
136    */
137   UINT16   m_RDR;         // Receive data rate
138   bool   m_RDV8;         // Receive data rate divider
139   UINT16   m_XDR;         // Transmit data rate
140   bool   m_XDV8;         // Transmit data rate divider
141
142   /* Status flags */
143   bool   m_INT;         // mirrors /INT output line, inverted
144   bool   m_DSCH;         // Data set status change
145
146   bool   m_CTSin;         // Inverted /CTS input (i.e. CTS)
147   bool   m_DSRin;         // Inverted /DSR input (i.e. DSR)
148   bool   m_RTSout;      // Current inverted /RTS line state (i.e. RTS)
149
150   bool   m_TIMELP;      // Timer elapsed
151   bool   m_TIMERR;      // Timer error
152
153   bool   m_XSRE;         // Transmit shift register empty
154   bool   m_XBRE;         // Transmit buffer register empty
155   bool   m_RBRL;         // Receive buffer register loaded
156
157   bool   m_RIN;         // State of the RIN pin
158   bool   m_RSBD;         // Receive start bit detect
159   bool   m_RFBD;         // Receive full bit detect
160   bool   m_RFER;         // Receive framing error
161   bool   m_ROVER;         // Receiver overflow
162   bool   m_RPER;         // Receive parity error
163
164   UINT8   m_RCL;         // Character length
165   bool   m_ODDP;
166   bool   m_PENB;
167   UINT8   m_STOPB;
168   bool   m_CLK4M;      // /PHI input divide select
169
170   UINT8   m_TMR;      /* interval timer */
171
172   /* clock registers */
173   emu_timer *m_dectimer;         /* MESS timer, used to emulate the decrementer register */
174   emu_timer *m_recvtimer;
175   emu_timer *m_sendtimer;
176
177   // This value is the ratio of data input versus the poll rate. The
178   // data source should deliver data bytes at every 1/baudpoll call.
179   // This is to ensure that data is delivered at a rate that is expected
180   // from the emulated program.
181   double   m_baudpoll;
182
183   // Caches the last configuration setting (used with the ctrl_callback)
184   int      m_last_config_value;
185};
186
90187/***************************************************************************
91188    DEVICE CONFIGURATION MACROS
92189***************************************************************************/
93190
94#define MCFG_TMS9902_ADD(_tag, _intrf) \
95   MCFG_DEVICE_ADD(_tag, TMS9902, 0) \
191#define MCFG_TMS9902_ADD(_tag, _intrf, _clock) \
192   MCFG_DEVICE_ADD(_tag, TMS9902, _clock) \
96193   MCFG_DEVICE_CONFIG(_intrf)
97194
98195#endif /* __TMS9902_H__ */
src/mess/machine/ti99/sgcpu.c
r14513r14514
1/***************************************************************************
2
3    SGCPU board
4    including decoder/mapper
5
6    This component implements the address decoder and mapper logic from the
7    SGCPU expansion card (inofficially called TI-99/4P).
8
9    AMA/B/C: According to the manual, the 19-bit address extension is not
10    supported by the SGCPU, due to some cards not checking these lines (like
11    the Horizon Ramdisk)
12
13    Michael Zapf, October 2010
14
15    TODO: Check for wait states
16***************************************************************************/
17
18#include "emu.h"
19#include "peribox.h"
20#include "sgcpu.h"
21#include "videowrp.h"
22#include "sound/sn76496.h"
23#include "imagedev/cassette.h"
24
25typedef struct _sgcpu_state
26{
27   /* Pointer to ROM0 */
28   UINT16 *rom0;
29
30   /* Pointer to DSR ROM */
31   UINT16 *dsr;
32
33   /* Pointer to ROM6, first bank */
34   UINT16 *rom6a;
35
36   /* Pointer to ROM6, second bank */
37   UINT16 *rom6b;
38
39   /* AMS RAM (1 Mib) */
40   UINT16 *ram;
41
42   /* Scratch pad ram (1 KiB) */
43   UINT16 *scratchpad;
44
45   /* True if SGCPU DSR is enabled */
46   int internal_dsr;
47
48   /* True if SGCPU rom6 is enabled */
49   int internal_rom6;
50
51   /* Offset to the ROM6 bank. */
52   int rom6_bank;
53
54   /* TRUE when mapper is active */
55   int map_mode;
56
57   /* TRUE when mapper registers are accessible */
58   int access_mapper;
59
60   UINT8   lowbyte;
61   UINT8   highbyte;
62   UINT8   latch;
63
64   /* Mapper registers */
65   UINT8 mapper[16];
66
67   device_t *cpu;
68   device_t *soundchip;
69   device_t *video;
70   cassette_image_device *cassette;
71   device_t *peribox;
72
73   int keyCol;
74   int alphaLockLine;
75
76} sgcpu_state;
77
78
79INLINE sgcpu_state *get_safe_token(device_t *device)
80{
81   assert(device != NULL);
82   assert(device->type() == SGCPU);
83
84   return (sgcpu_state *)downcast<legacy_device_base *>(device)->token();
85}
86
87
88/***************************************************************************
89   AMS Memory implementation
90***************************************************************************/
91
92#define MAP_CRU_BASE 0x0f00
93#define SAMS_CRU_BASE 0x1e00
94
95/*
96    CRU write (there is no read here)
97*/
98WRITE8_DEVICE_HANDLER( sgcpu_cru_w )
99{
100   sgcpu_state *sgcpu = get_safe_token(device);
101   int addroff = (offset + 0x0400)<<1;
102
103   if ((addroff & 0xff00)==MAP_CRU_BASE)
104   {
105      if ((addroff & 0x000e)==0) sgcpu->internal_dsr = data;
106      if ((addroff & 0x000e)==2) sgcpu->internal_rom6 = data;
107      if ((addroff & 0x000e)==4)
108      {
109         ti99_peb_senila(sgcpu->peribox, data);
110      }
111      if ((addroff & 0x000e)==6)
112      {
113         ti99_peb_senilb(sgcpu->peribox, data);
114      }
115      // TODO: more CRU bits? 8=Fast timing / a=KBENA
116      return;
117   }
118   if ((addroff & 0xff00)==SAMS_CRU_BASE)
119   {
120      if ((addroff & 0x000e)==0) sgcpu->access_mapper = data;
121      if ((addroff & 0x000e)==2) sgcpu->map_mode = data;
122      return;
123   }
124
125   // No match - pass to peribox
126   ti99_peb_cru_w(sgcpu->peribox, addroff, data);
127}
128
129READ8_DEVICE_HANDLER( sgcpu_cru_r )
130{
131   sgcpu_state *sgcpu = get_safe_token(device);
132   UINT8 value = 0;
133   int addroff = (offset + 0x0080)<<4;
134
135   ti99_peb_cru_rz(sgcpu->peribox, addroff, &value);
136   return value;
137}
138
139/*
140    Memory read. The SAMS card has two address areas: The memory is at locations
141    0x2000-0x3fff and 0xa000-0xffff, and the mapper area is at 0x4000-0x401e
142    (only even addresses).
143*/
144static READ16_DEVICE_HANDLER( sgcpu_samsmem_r )
145{
146   sgcpu_state *sgcpu = get_safe_token(device);
147   UINT32 address = 0;
148   int addroff = offset << 1;
149
150   // select memory expansion
151   if (sgcpu->map_mode)
152      address = (sgcpu->mapper[(addroff>>12) & 0x000f] << 12) + (addroff & 0x0fff);
153   else // transparent mode
154      address = addroff;
155
156   return sgcpu->ram[address>>1];
157}
158
159/*
160    Memory write
161*/
162static WRITE16_DEVICE_HANDLER( sgcpu_samsmem_w )
163{
164   sgcpu_state *sgcpu = get_safe_token(device);
165   UINT32 address = 0;
166   int addroff = offset << 1;
167
168   // select memory expansion
169   if (sgcpu->map_mode)
170      address = (sgcpu->mapper[(addroff>>12) & 0x000f] << 12) + (addroff & 0x0fff);
171   else // transparent mode
172      address = addroff;
173
174   COMBINE_DATA(&sgcpu->ram[address>>1]);
175}
176
177/***************************************************************************
178    Internal datamux; similar to TI-99/4A
179***************************************************************************/
180
181static READ16_DEVICE_HANDLER( sgcpu_datamux_r )
182{
183   sgcpu_state *sgcpu = get_safe_token(device);
184
185   UINT8 hbyte = 0;
186   UINT16 addroff = (offset << 1);
187
188   ti99_peb_data_rz(sgcpu->peribox, addroff+1, &sgcpu->latch);
189   sgcpu->lowbyte = sgcpu->latch;
190
191   // Takes three cycles
192   device_adjust_icount(sgcpu->cpu, -3);
193
194   ti99_peb_data_rz(sgcpu->peribox, addroff, &hbyte);
195   sgcpu->highbyte = hbyte;
196
197   // Takes three cycles
198   device_adjust_icount(sgcpu->cpu, -3);
199
200   // use the latch and the currently read byte and put it on the 16bit bus
201//  printf("read  address = %04x, value = %04x, memmask = %4x\n", addroff,  (hbyte<<8) | sgcpu->latch, mem_mask);
202   return (hbyte<<8) | sgcpu->latch ;
203}
204
205/*
206    Write access.
207    TODO: use the 16-bit expansion in the box for suitable cards
208*/
209static WRITE16_DEVICE_HANDLER( sgcpu_datamux_w )
210{
211   sgcpu_state *sgcpu = get_safe_token(device);
212   UINT16 addroff = (offset << 1);
213//  printf("write address = %04x, value = %04x, memmask = %4x\n", addroff, data, mem_mask);
214
215   // read more about the datamux in datamux.c
216
217   // byte-only transfer, high byte
218   // we use the previously read low byte to complete
219   if (mem_mask == 0xff00)
220      data = data | sgcpu->lowbyte;
221
222   // byte-only transfer, low byte
223   // we use the previously read high byte to complete
224   if (mem_mask == 0x00ff)
225      data = data | (sgcpu->highbyte << 8);
226
227   // Write to the PEB
228   ti99_peb_data_w(sgcpu->peribox, addroff+1, data & 0xff);
229
230   // Takes three cycles
231   device_adjust_icount(sgcpu->cpu,-3);
232
233   // Write to the PEB
234   ti99_peb_data_w(sgcpu->peribox, addroff, (data>>8) & 0xff);
235
236   // Takes three cycles
237   device_adjust_icount(sgcpu->cpu,-3);
238}
239
240/*
241    Read access.
242*/
243READ16_DEVICE_HANDLER( sgcpu_r )
244{
245   sgcpu_state *sgcpu = get_safe_token(device);
246   if (sgcpu->cpu == NULL) return 0;
247
248   int addroff = offset << 1;
249   UINT16 zone = addroff & 0xe000;
250   UINT16 value = 0;
251
252   if (zone==0x0000)
253   {
254      // ROM0
255      value = sgcpu->rom0[(addroff & 0x1fff)>>1];
256      return value;
257   }
258   if (zone==0x2000 || zone==0xa000 || zone==0xc000 || zone==0xe000)
259   {
260      value = sgcpu_samsmem_r(device, offset, mem_mask);
261      return value;
262   }
263
264   if (zone==0x4000)
265   {
266      if (sgcpu->internal_dsr)
267      {
268         value = sgcpu->dsr[(addroff & 0x1fff)>>1];
269         return value;
270      }
271      else
272      {
273         if (sgcpu->access_mapper && ((addroff & 0xffe0)==0x4000))
274         {
275            value = sgcpu->mapper[offset & 0x000f]<<8;
276            return value;
277         }
278      }
279   }
280
281   if (zone==0x6000 && sgcpu->internal_rom6)
282   {
283      if (sgcpu->rom6_bank==0)
284         value = sgcpu->rom6a[(addroff & 0x1fff)>>1];
285      else
286         value = sgcpu->rom6b[(addroff & 0x1fff)>>1];
287
288      return value;
289   }
290
291   // Scratch pad RAM and sound
292   // speech is in peribox
293   // groms are in hsgpl in peribox
294   if (zone==0x8000)
295   {
296      if ((addroff & 0xfff0)==0x8400)
297      {
298         value = 0;
299         return value;
300      }
301      if ((addroff & 0xfc00)==0x8000)
302      {
303         value = sgcpu->scratchpad[(addroff & 0x03ff)>>1];
304         return value;
305      }
306      // Video: 8800, 8802
307      if ((addroff & 0xfffd)==0x8800)
308      {
309         value = ti_v9938_r16(sgcpu->video, offset, mem_mask);
310         return value;
311      }
312   }
313
314   // If we are here, check the peribox via the datamux
315   // catch-all for unmapped zones
316   value = sgcpu_datamux_r(device, offset, mem_mask);
317   return value;
318}
319
320/*
321    Write access.
322*/
323WRITE16_DEVICE_HANDLER( sgcpu_w )
324{
325   sgcpu_state *sgcpu = get_safe_token(device);
326   if (sgcpu->cpu == NULL) return;
327
328//  device_adjust_icount(sgcpu->cpu, -4);
329
330   int addroff = offset << 1;
331   UINT16 zone = addroff & 0xe000;
332
333   if (zone==0x0000)
334   {
335      // ROM0
336      logerror("SGCPU: ignoring ROM write access at %04x\n", addroff);
337      return;
338   }
339
340   if (zone==0x2000 || zone==0xa000 || zone==0xc000 || zone==0xe000)
341   {
342      sgcpu_samsmem_w(device, offset, data, mem_mask);
343      return;
344   }
345
346   if (zone==0x4000)
347   {
348      if (sgcpu->internal_dsr)
349      {
350         logerror("SGCPU: ignoring DSR write access at %04x\n", addroff);
351         return;
352      }
353      else
354      {
355         if (sgcpu->access_mapper && ((addroff & 0xffe0)==0x4000))
356         {
357            COMBINE_DATA(&sgcpu->mapper[offset & 0x000f]);
358            return;
359         }
360      }
361   }
362
363   if (zone==0x6000 && sgcpu->internal_rom6)
364   {
365      sgcpu->rom6_bank = offset & 0x0001;
366      return;
367   }
368
369   // Scratch pad RAM and sound
370   // speech is in peribox
371   // groms are in hsgpl in peribox
372   if (zone==0x8000)
373   {
374      if ((addroff & 0xfff0)==0x8400)
375      {
376         sn76496_w(sgcpu->soundchip, 0, (data >> 8) & 0xff);
377         return;
378      }
379      if ((addroff & 0xfc00)==0x8000)
380      {
381         COMBINE_DATA(&sgcpu->scratchpad[(addroff & 0x03ff)>>1]);
382         return;
383      }
384      // Video: 8C00, 8C02
385      if ((addroff & 0xfffd)==0x8c00)
386      {
387         ti_v9938_w16(sgcpu->video, offset, data, mem_mask);
388         return;
389      }
390   }
391
392   // If we are here, check the peribox via the datamux
393   // catch-all for unmapped zones
394   sgcpu_datamux_w(device, offset, data, mem_mask);
395}
396
397/***************************************************************************
398    Control lines
399****************************************************************************/
400/*
401    Handles the EXTINT line. This one is directly connected to the 9901.
402*/
403WRITE_LINE_DEVICE_HANDLER( card_extint )
404{
405   tms9901_set_single_int(device->machine().device("tms9901"), 1, state);
406}
407
408WRITE_LINE_DEVICE_HANDLER( card_notconnected )
409{
410   logerror("Not connected line set ... sorry\n");
411}
412
413WRITE_LINE_DEVICE_HANDLER( card_ready )
414{
415   logerror("READY line set ... not yet connected, level=%02x\n", state);
416}
417
418/*
419    set the state of int2 (called by the v9938)
420*/
421void tms9901_sg_set_int2(device_t *, v99x8_device &tms, int state)
422{
423   tms9901_set_single_int(tms.machine().device("tms9901"), 2, state);
424}
425
426/***************************************************************************
427    Keyboard/tape control
428****************************************************************************/
429static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3" };
430
431/*
432    Called by the 9901 core whenever the state of INTREQ and IC0-3 changes
433*/
434static TMS9901_INT_CALLBACK( tms9901_interrupt_callback )
435{
436   if (intreq)
437   {
438      // On TI99, TMS9900 IC0-3 lines are not connected to TMS9901,
439      // but hard-wired to force level 1 interrupts
440      cputag_set_input_line_and_vector(device->machine(), "maincpu", 0, ASSERT_LINE, 1);
441   }
442   else
443   {
444      cputag_set_input_line(device->machine(), "maincpu", 0, CLEAR_LINE);
445   }
446}
447
448
449/*
450    Read pins INT3*-INT7* of TI99's 9901.
451
452    signification:
453     (bit 1: INT1 status)
454     (bit 2: INT2 status)
455     bit 3-7: keyboard status bits 0 to 4
456*/
457static READ8_DEVICE_HANDLER( sgcpu_R9901_0 )
458{
459   device_t *dev = device->machine().device("sgcpu_board");
460   sgcpu_state *sgcpu = get_safe_token(dev);
461   int answer;
462
463   answer = ((input_port_read(device->machine(), keynames[sgcpu->keyCol >> 1]) >> ((sgcpu->keyCol & 1) * 8)) << 3) & 0xF8;
464
465   if (sgcpu->alphaLockLine == FALSE)
466      answer &= ~(input_port_read(device->machine(), "ALPHA") << 3);
467
468   return answer;
469}
470
471/*
472    Read pins INT8*-INT15* of TI99's 9901.
473     bit 0-2: keyboard status bits 5 to 7
474     bit 3: tape input mirror
475     bit 5-7: weird, not emulated
476*/
477static READ8_DEVICE_HANDLER( sgcpu_R9901_1 )
478{
479   device_t *dev = device->machine().device("sgcpu_board");
480   sgcpu_state *sgcpu = get_safe_token(dev);
481   int answer;
482
483   if (sgcpu->keyCol == 7)
484      answer = 0x07;
485   else
486   {
487      answer = ((input_port_read(device->machine(), keynames[sgcpu->keyCol >> 1]) >> ((sgcpu->keyCol & 1) * 8)) >> 5) & 0x07;
488   }
489   return answer;
490}
491
492
493/*
494    Read pins P8-P15 of TI99's 9901.
495     bit 26: high
496     bit 27: tape input
497*/
498static READ8_DEVICE_HANDLER( sgcpu_R9901_3 )
499{
500   device_t *dev = device->machine().device("sgcpu_board");
501   sgcpu_state *sgcpu = get_safe_token(dev);
502   int answer = 4;   /* on systems without handset, the pin is pulled up to avoid spurious interrupts */
503   if ((sgcpu->cassette)->input() > 0)
504      answer |= 8;
505   return answer;
506}
507
508/*
509    WRITE key column select (P2-P4)
510*/
511static WRITE8_DEVICE_HANDLER( sgcpu_KeyC )
512{
513   device_t *dev = device->machine().device("sgcpu_board");
514   sgcpu_state *sgcpu = get_safe_token(dev);
515   if (data)
516      sgcpu->keyCol |= 1 << (offset-2);
517   else
518      sgcpu->keyCol &= ~(1 << (offset-2));
519}
520
521/*
522    WRITE alpha lock line - TI99/4a only (P5)
523*/
524static WRITE8_DEVICE_HANDLER( sgcpu_AlphaW )
525{
526   device_t *dev = device->machine().device("sgcpu_board");
527   sgcpu_state *sgcpu = get_safe_token(dev);
528   sgcpu->alphaLockLine = data;
529}
530
531/*
532    command CS1 (only) tape unit motor (P6)
533*/
534static WRITE8_DEVICE_HANDLER( sgcpu_CS_motor )
535{
536   device_t *dev = device->machine().device("sgcpu_board");
537   sgcpu_state *sgcpu = get_safe_token(dev);
538   sgcpu->cassette->change_state(data ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
539}
540
541/*
542    audio gate (P8)
543
544    Set to 1 before using tape: this enables the mixing of tape input sound
545    with computer sound.
546
547    We do not really need to emulate this as the tape recorder generates sound
548    on its own.
549*/
550static WRITE8_DEVICE_HANDLER( sgcpu_audio_gate )
551{
552}
553
554/*
555    tape output (P9)
556    I think polarity is correct, but don't take my word for it.
557*/
558static WRITE8_DEVICE_HANDLER( sgcpu_CS_output )
559{
560   device_t *dev = device->machine().device("sgcpu_board");
561   sgcpu_state *sgcpu = get_safe_token(dev);
562   sgcpu->cassette->output(data ? +1 : -1);
563}
564
565/* TMS9901 setup. The callback functions pass a reference to the TMS9901 as device. */
566const tms9901_interface tms9901_wiring_ti99_4p =
567{
568   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
569
570   {   /* read handlers */
571      sgcpu_R9901_0,
572      sgcpu_R9901_1,
573      NULL,
574      sgcpu_R9901_3
575   },
576
577   {   /* write handlers */
578      NULL,
579      NULL,
580      sgcpu_KeyC,
581      sgcpu_KeyC,
582      sgcpu_KeyC,
583      sgcpu_AlphaW,
584      sgcpu_CS_motor,
585      NULL,
586      sgcpu_audio_gate,
587      sgcpu_CS_output,
588      NULL,
589      NULL,
590      NULL,
591      NULL,
592      NULL,
593      NULL
594   },
595
596   /* interrupt handler */
597   tms9901_interrupt_callback,
598
599   /* clock rate = 3MHz */
600   3000000.
601};
602
603
604/***************************************************************************
605    DEVICE LIFECYCLE FUNCTIONS
606***************************************************************************/
607
608static DEVICE_START( sgcpu )
609{
610//  // printf("Starting mapper\n");
611   sgcpu_state *sgcpu = get_safe_token(device);
612   sgcpu->cpu = device->machine().device("maincpu");
613   sgcpu->peribox = device->machine().device("peribox");
614   sgcpu->soundchip = device->machine().device("soundgen");
615   sgcpu->video = device->machine().device("video");
616   sgcpu->cassette = device->machine().device<cassette_image_device>(CASSETTE_TAG);
617
618   assert(sgcpu->cpu && sgcpu->peribox && sgcpu->soundchip && sgcpu->video && sgcpu->cassette);
619
620   sgcpu->ram = (UINT16*)malloc(0x080000);
621   sgcpu->scratchpad = (UINT16*)malloc(0x0400);
622
623   UINT16 *rom = (UINT16*)device->machine().region("maincpu")->base();
624   sgcpu->rom0  = rom + 0x2000;
625   sgcpu->dsr   = rom + 0x6000;
626   sgcpu->rom6a = rom + 0x3000;
627   sgcpu->rom6b = rom + 0x7000;
628}
629
630static DEVICE_STOP( sgcpu )
631{
632   // printf("Stopping SGCPU\n");
633   sgcpu_state *sgcpu = get_safe_token(device);
634   free(sgcpu->ram);
635   free(sgcpu->scratchpad);
636}
637
638static DEVICE_RESET( sgcpu )
639{
640   logerror("Resetting SGCPU\n");
641}
642
643static const char DEVTEMPLATE_SOURCE[] = __FILE__;
644
645#define DEVTEMPLATE_ID(p,s)             p##sgcpu##s
646#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET
647#define DEVTEMPLATE_NAME                "SGCPU Board"
648#define DEVTEMPLATE_FAMILY              "Peripheral Expansion"
649#include "devtempl.h"
650
651DEFINE_LEGACY_DEVICE( SGCPU, sgcpu );
src/mess/machine/ti99/sgcpu.h
r14513r14514
1#ifndef __SGCPU__
2#define __SGCPU__
3
4#include "machine/tms9901.h"
5#include "video/v9938.h"
6
7void tms9901_sg_set_int2(device_t *, v99x8_device &tms, int state);
8
9READ16_DEVICE_HANDLER( sgcpu_r );
10WRITE16_DEVICE_HANDLER( sgcpu_w );
11
12READ8_DEVICE_HANDLER( sgcpu_cru_r );
13WRITE8_DEVICE_HANDLER( sgcpu_cru_w );
14
15WRITE_LINE_DEVICE_HANDLER( card_extint );
16WRITE_LINE_DEVICE_HANDLER( card_notconnected );
17WRITE_LINE_DEVICE_HANDLER( card_ready );
18
19/* device interface */
20DECLARE_LEGACY_DEVICE( SGCPU, sgcpu );
21
22#define MCFG_SGCPUB_ADD(_tag )         \
23   MCFG_DEVICE_ADD(_tag, SGCPU, 0)
24
25extern const tms9901_interface tms9901_wiring_ti99_4p;
26
27#endif
src/mess/machine/ti99/tiboard.c
r14513r14514
1/***************************************************************************
2    TI-99/4(A) and TI-99/8 main board
3
4    Michael Zapf, October 2010
5    TODO: Find out whether device can be determined more easily in CRU functions
6***************************************************************************/
7
8#include "emu.h"
9#include "tiboard.h"
10#include "gromport.h"
11#include "video/tms9928a.h"
12#include "handset.h"
13#include "mecmouse.h"
14#include "imagedev/cassette.h"
15#include "mapper8.h"
16
17typedef struct _tiboard_state
18{
19   /* Mode: Which console do we have? */
20   int            mode;
21
22   /* Keyboard support */
23   int            keyCol;
24   int            alphaLockLine;
25
26   /* Devices */
27   device_t   *cpu;
28   device_t   *video;
29   device_t  *tms9901;
30   device_t   *peribox;
31   device_t   *sound;
32
33   device_t   *handset;
34   device_t   *mecmouse;
35   device_t   *gromport;
36
37} tiboard_state;
38
39static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3" };
40static const char *const keynames8[] = {
41      "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5", "KEY6", "KEY7",
42      "KEY8", "KEY9", "KEY10", "KEY11", "KEY12", "KEY13", "KEY14", "KEY15"
43   };
44
45INLINE tiboard_state *get_safe_token(device_t *device)
46{
47   assert(device != NULL);
48   assert(device->type() == TIBOARD);
49   return (tiboard_state *)downcast<legacy_device_base *>(device)->token();
50}
51
52INLINE const tiboard_config *get_config(device_t *device)
53{
54   assert(device != NULL);
55   assert(device->type() == TIBOARD);
56
57   return (const tiboard_config *) downcast<const legacy_device_base *>(device)->inline_config();
58}
59
60/****************************************************************************
61    TMS9901 and attached devices handling
62****************************************************************************/
63/*
64    VBL interrupt  (mmm...  actually, it happens when the beam enters the lower
65    border, so it is not a genuine VBI, but who cares ?)
66*/
67INTERRUPT_GEN( ti99_vblank_interrupt )
68{
69   device_t *dev = device->machine().device("ti_board");
70   tiboard_state *board = get_safe_token(dev);
71
72   if (board->handset != NULL)
73      ti99_handset_task(board->handset);
74   if (board->mecmouse != NULL)
75      mecmouse_poll(board->mecmouse);
76}
77
78/*
79    TI99/4x-specific tms9901 I/O handlers
80
81    See mess/machine/tms9901.c for generic tms9901 CRU handlers.
82
83    BTW, although TMS9900 is generally big-endian, it is little endian as far as CRU is
84    concerned. (i.e. bit 0 is the least significant)
85
86KNOWN PROBLEMS:
87    * a read or write to bits 16-31 causes TMS9901 to quit timer mode.  The problem is:
88      on a TI99/4A, any memory access causes a dummy CRU read.  Therefore, TMS9901 can quit
89      timer mode even though the program did not explicitely ask... and THIS is impossible
90      to emulate efficiently (we would have to check every memory operation).
91*/
92/*
93    TMS9901 interrupt handling on a TI99/4(a).
94
95    TI99/4(a) uses the following interrupts:
96    INT1: external interrupt (used by RS232 controller, for instance)
97    INT2: VDP interrupt
98    TMS9901 timer interrupt (overrides INT3)
99    INT12: handset interrupt (only on a TI-99/4 with the handset prototypes)
100
101    Three (occasionally four) interrupts are used by the system (INT1, INT2,
102    timer, and INT12 on a TI-99/4 with remote handset prototypes), out of 15/16
103    possible interrupts.  Keyboard pins can be used as interrupt pins, too, but
104    this is not emulated (it's a trick, anyway, and I don't know any program
105    which uses it).
106
107    When an interrupt line is set (and the corresponding bit in the interrupt mask is set),
108    a level 1 interrupt is requested from the TMS9900.  This interrupt request lasts as long as
109    the interrupt pin and the revelant bit in the interrupt mask are set.
110
111    TIMER interrupts are kind of an exception, since they are not associated with an external
112    interrupt pin, and I guess it takes a write to the 9901 CRU bit 3 ("SBO 3") to clear
113    the pending interrupt (or am I wrong once again ?).
114
115nota:
116    All interrupt routines notify (by software) the TMS9901 of interrupt
117    recognition (with a "SBO n").  However, unless I am missing something, this
118    has absolutely no consequence on the TMS9901 (except for the TIMER
119    interrupt routine), and interrupt routines would work fine without this
120    SBO instruction.  This is quite weird.  Maybe the interrupt recognition
121    notification is needed on TMS9985, or any other weird variant of TMS9900
122    (how about the TI-99 development system connected to a TI990/10?).
123*/
124
125/*
126    set the state of int2 (called by the tms9928 core)
127*/
128void tms9901_set_int2(device_t *device, int state)
129{
130   device_t *dev = device->machine().device("ti_board");
131   tiboard_state *board = get_safe_token(dev);
132   tms9901_set_single_int(board->tms9901, 2, state);
133}
134
135/*
136    set the state of int2 (called by the v9938 core)
137*/
138void tms9901_set_int2_v9938(device_t *, v99x8_device &tms, int state)
139{
140   tms9901_set_single_int(tms.machine().device("tms9901"), 2, state);
141}
142
143
144/*
145    Called by the 9901 core whenever the state of INTREQ and IC0-3 changes
146*/
147static TMS9901_INT_CALLBACK( tms9901_interrupt_callback )
148{
149   if (intreq)
150   {
151      // On TI99, TMS9900 IC0-3 lines are not connected to TMS9901,
152      // but hard-wired to force level 1 interrupts
153      cputag_set_input_line_and_vector(device->machine(), "maincpu", 0, ASSERT_LINE, 1);   /* interrupt it, baby */
154   }
155   else
156   {
157      cputag_set_input_line(device->machine(), "maincpu", 0, CLEAR_LINE);
158   }
159}
160
161/*
162    ti99_handset_set_ack()
163    Handler for tms9901 P0 pin (handset data acknowledge)
164*/
165static WRITE8_DEVICE_HANDLER( ti99_handset_set_ack )
166{
167   device_t *dev = device->machine().device("ti_board");
168   tiboard_state *board = get_safe_token(dev);
169
170   if (board->handset != NULL)
171      ti99_handset_set_acknowledge(board->handset, data);
172}
173
174/*
175    Read pins INT3*-INT7* of TI99's 9901.
176
177    signification:
178     (bit 1: INT1 status)
179     (bit 2: INT2 status)
180     bit 3-7: keyboard status bits 0 to 4
181*/
182static READ8_DEVICE_HANDLER( ti99_R9901_0 )
183{
184   int answer = 0;
185   device_t *dev = device->machine().device("ti_board");
186   tiboard_state *board = get_safe_token(dev);
187
188   if (board->keyCol == 7)
189   {
190      if (board->handset != NULL) answer = (ti99_handset_poll_bus(board->handset) << 3);
191      answer |= 0x80;
192   }
193   else
194   {
195      if ((board->mecmouse != NULL) && (board->keyCol == 6))
196      {
197         answer = mecmouse_get_values(board->mecmouse);
198      }
199      else
200         answer = ((input_port_read(device->machine(), keynames[board->keyCol >> 1]) >> ((board->keyCol & 1) * 8)) << 3) & 0xF8;
201   }
202   return answer;
203}
204
205/*
206    Read pins INT3*-INT7* of TI99's 9901.
207
208    signification:
209     (bit 1: INT1 status)
210     (bit 2: INT2 status)
211     bit 3-7: keyboard status bits 0 to 4
212*/
213static READ8_DEVICE_HANDLER( ti99_R9901_0a )
214{
215   int answer;
216   device_t *dev = device->machine().device("ti_board");
217   tiboard_state *board = get_safe_token(dev);
218
219   if ((board->mecmouse != NULL) && (board->keyCol == 7))
220      answer = mecmouse_get_values(board->mecmouse);
221   else
222      answer = ((input_port_read(device->machine(), keynames[board->keyCol >> 1]) >> ((board->keyCol & 1) * 8)) << 3) & 0xF8;
223
224   if (board->alphaLockLine == FALSE)
225      answer &= ~(input_port_read(device->machine(), "ALPHA") << 3);
226
227   return answer;
228}
229
230
231/*
232    Read pins INT8*-INT15* of TI99's 9901.
233
234    signification:
235     bit 0-2: keyboard status bits 5 to 7
236     bit 3: tape input mirror
237     (bit 4: IR remote handset interrupt)
238     bit 5-7: weird, not emulated
239*/
240static READ8_DEVICE_HANDLER( ti99_R9901_1 )
241{
242   int answer;
243   device_t *dev = device->machine().device("ti_board");
244   tiboard_state *board = get_safe_token(dev);
245
246   if (board->keyCol == 7)
247      answer = 0x07;
248   else
249   {
250      answer = ((input_port_read(device->machine(), keynames[board->keyCol >> 1]) >> ((board->keyCol & 1) * 8)) >> 5) & 0x07;
251   }
252
253   /* we don't take CS2 into account, as CS2 is a write-only unit */
254   //  if ((device->machine().device<cassette_image_device>(CASSETTE_TAG))->input() > 0)
255   //      answer |= 8;
256
257   return answer;
258}
259
260/*
261    Read pins P0-P7 of TI99's 9901.
262
263     bit 1: handset data clock pin
264*/
265static READ8_DEVICE_HANDLER( ti99_R9901_2 )
266{
267   device_t *dev = device->machine().device("ti_board");
268   tiboard_state *board = get_safe_token(dev);
269
270   if (board->handset != NULL)
271      if (ti99_handset_get_clock(board->handset)) return 2;
272   return 0;
273}
274
275/*
276    Read pins P8-P15 of TI99's 9901. (99/4)
277
278     bit 26: IR handset interrupt
279     bit 27: tape input
280*/
281static READ8_DEVICE_HANDLER( ti99_R9901_3 )
282{
283   int answer = 4;
284   device_t *dev = device->machine().device("ti_board");
285   tiboard_state *board = get_safe_token(dev);
286
287   /* on systems without handset, the pin is pulled up to avoid spurious interrupts */
288   if (board->handset != NULL)
289      if (ti99_handset_get_int(board->handset)) answer = 0;
290
291   /* we don't take CS2 into account, as CS2 is a write-only unit */
292   if ((device->machine().device<cassette_image_device>(CASSETTE_TAG))->input() > 0)
293      answer |= 8;
294
295   return answer;
296}
297
298/*
299    Read pins P8-P15 of TI99's 9901. (TI-99/8)
300
301     bit 26: high
302     bit 27: tape input
303*/
304static READ8_DEVICE_HANDLER( ti99_8_R9901_3 )
305{
306   int answer = 4;   /* on systems without handset, the pin is pulled up to avoid spurious interrupts */
307   if ((device->machine().device<cassette_image_device>(CASSETTE_TAG))->input() > 0)
308      answer |= 8;
309   return answer;
310}
311
312/*
313    WRITE key column select (P2-P4), TI-99/4
314*/
315static WRITE8_DEVICE_HANDLER( ti99_KeyC )
316{
317   device_t *dev = device->machine().device("ti_board");
318   tiboard_state *board = get_safe_token(dev);
319
320   int index=5;
321
322   if (board->mode==TI994A)
323      index=6;
324
325   if (data)
326      board->keyCol |= 1 << (offset-2);
327   else
328      board->keyCol &= ~ (1 << (offset-2));
329
330   if (board->mecmouse != NULL)
331      mecmouse_select(board->mecmouse, board->keyCol, index , index+1);
332}
333
334/*
335    WRITE alpha lock line - TI99/4a only (P5)
336*/
337static WRITE8_DEVICE_HANDLER( ti99_AlphaW )
338{
339   device_t *dev = device->machine().device("ti_board");
340   tiboard_state *board = get_safe_token(dev);
341   board->alphaLockLine = data;
342}
343
344/*
345    command CS1 tape unit motor (P6)
346*/
347static WRITE8_DEVICE_HANDLER( ti99_CS1_motor )
348{
349   cassette_image_device *img = device->machine().device<cassette_image_device>(CASSETTE_TAG);
350   img->change_state(data ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
351}
352
353/*
354    command CS2 tape unit motor (P7)
355*/
356static WRITE8_DEVICE_HANDLER( ti99_CS2_motor )
357{
358   cassette_image_device *img = device->machine().device<cassette_image_device>(CASSETTE2_TAG);
359   img->change_state(data ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
360}
361
362/*
363    command CS1 (only) tape unit motor (P6)
364*/
365static WRITE8_DEVICE_HANDLER( ti99_CS_motor )
366{
367   cassette_image_device *img = device->machine().device<cassette_image_device>(CASSETTE_TAG);
368   img->change_state(data ? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
369}
370
371/*
372    audio gate (P8)
373
374    Set to 1 before using tape: this enables the mixing of tape input sound
375    with computer sound.
376
377    We do not really need to emulate this as the tape recorder generates sound
378    on its own.
379*/
380static WRITE8_DEVICE_HANDLER( ti99_audio_gate )
381{
382}
383
384/*
385    tape output (P9)
386    I think polarity is correct, but don't take my word for it.
387*/
388static WRITE8_DEVICE_HANDLER( ti99_CS12_output )
389{
390   device->machine().device<cassette_image_device>(CASSETTE_TAG)->output(data ? +1 : -1);
391   device->machine().device<cassette_image_device>(CASSETTE2_TAG)->output(data ? +1 : -1);
392}
393
394/*
395    tape output (P9)
396    I think polarity is correct, but don't take my word for it.
397*/
398static WRITE8_DEVICE_HANDLER( ti99_CS_output )
399{
400   device->machine().device<cassette_image_device>(CASSETTE_TAG)->output(data ? +1 : -1);
401}
402
403/*
404    WRITE key column select (P0-P3)
405*/
406static WRITE8_DEVICE_HANDLER( ti99_8_KeyC )
407{
408   device_t *dev = device->machine().device("ti_board");
409   tiboard_state *board = get_safe_token(dev);
410
411   if (data)
412      board->keyCol |= 1 << offset;
413   else
414      board->keyCol &= ~ (1 << offset);
415
416   if (board->mecmouse != NULL)
417      mecmouse_select(board->mecmouse, board->keyCol, 14, 15);
418}
419
420static WRITE8_DEVICE_HANDLER( ti99_8_CRUS )
421{
422   device_t *mapper = device->machine().device("mapper");
423   mapper8_CRUS_w(mapper, 0, data);
424}
425
426static WRITE8_DEVICE_HANDLER( ti99_8_PTGEN )
427{
428   device_t *mapper = device->machine().device("mapper");
429   mapper8_PTGEN_w(mapper, 0, data);
430}
431
432/*
433    Read pins INT3*-INT7* of TI99's 9901.
434
435    signification:
436     (bit 1: INT1 status)
437     (bit 2: INT2 status)
438     bits 3-4: unused?
439     bit 5: ???
440     bit 6-7: keyboard status bits 0 through 1
441*/
442static READ8_DEVICE_HANDLER( ti99_8_R9901_0 )
443{
444   int answer;
445   device_t *dev = device->machine().device("ti_board");
446   tiboard_state *board = get_safe_token(dev);
447
448   if (board->mecmouse != NULL && (board->keyCol == 15))
449   {
450      answer = mecmouse_get_values8(board->mecmouse, 0);
451   }
452   else
453   {
454      answer = (input_port_read(device->machine(), keynames8[board->keyCol]) << 6) & 0xC0;
455   }
456
457   return answer;
458}
459
460
461/*
462    Read pins INT8*-INT15* of TI99's 9901.
463
464    signification:
465     bit 0-2: keyboard status bits 2 to 4
466     bit 3: tape input mirror
467     (bit 4: IR remote handset interrupt)
468     bit 5-7: weird, not emulated
469*/
470static READ8_DEVICE_HANDLER( ti99_8_R9901_1 )
471{
472   device_t *dev = device->machine().device("ti_board");
473   tiboard_state *board = get_safe_token(dev);
474   int answer;
475
476   if (board->mecmouse != NULL && (board->keyCol == 15))
477   {
478      answer = mecmouse_get_values8(board->mecmouse, 1);
479   }
480   else
481   {
482      answer = (input_port_read(device->machine(), keynames8[board->keyCol]) >> 2) & 0x07;
483   }
484
485   return answer;
486}
487
488/*
489    Read pins P0-P7 of TI99's 9901.
490
491     bit 1: handset data clock pin
492*/
493static READ8_DEVICE_HANDLER( ti99_8_R9901_2 )
494{
495   return 0;
496}
497
498
499/* TMS9901 setup. The callback functions pass a reference to the TMS9901 as device. */
500const tms9901_interface tms9901_wiring_ti99_4 =
501{
502   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
503
504   {   /* read handlers */
505      ti99_R9901_0,
506      ti99_R9901_1,
507      ti99_R9901_2,
508      ti99_R9901_3
509   },
510
511   {   /* write handlers */
512      ti99_handset_set_ack,
513      NULL,
514      ti99_KeyC,
515      ti99_KeyC,
516      ti99_KeyC,
517      NULL,
518      ti99_CS1_motor,
519      ti99_CS2_motor,
520      ti99_audio_gate,
521      ti99_CS12_output,
522      NULL,
523      NULL,
524      NULL,
525      NULL,
526      NULL,
527      NULL
528   },
529
530   /* interrupt handler */
531   tms9901_interrupt_callback,
532
533   /* clock rate = 3MHz */
534   3000000.
535};
536
537/* TMS9901 setup. The callback functions pass a reference to the TMS9901 as device. */
538const tms9901_interface tms9901_wiring_ti99_4a =
539{
540   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
541
542   {   /* read handlers */
543      ti99_R9901_0a,
544      ti99_R9901_1,
545      ti99_R9901_2,
546      ti99_R9901_3
547   },
548
549   {   /* write handlers */
550      NULL,
551      NULL,
552      ti99_KeyC,
553      ti99_KeyC,
554      ti99_KeyC,
555      ti99_AlphaW,
556      ti99_CS1_motor,
557      ti99_CS2_motor,
558      ti99_audio_gate,
559      ti99_CS12_output,
560      NULL,
561      NULL,
562      NULL,
563      NULL,
564      NULL,
565      NULL
566   },
567
568   /* interrupt handler */
569   tms9901_interrupt_callback,
570
571   /* clock rate = 3MHz */
572   3000000.
573};
574
575const tms9901_interface tms9901_wiring_ti99_8 =
576{
577   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
578
579   {   /* read handlers */
580      ti99_8_R9901_0,
581      ti99_8_R9901_1,
582      ti99_8_R9901_2,
583      ti99_8_R9901_3
584   },
585
586   {   /* write handlers */
587      ti99_8_KeyC,
588      ti99_8_KeyC,
589      ti99_8_KeyC,
590      ti99_8_KeyC,
591      ti99_8_CRUS,
592      ti99_8_PTGEN,
593      ti99_CS_motor,
594      NULL,
595      ti99_audio_gate,
596      ti99_CS_output,
597      NULL,
598      NULL,
599      NULL,
600      NULL,
601      NULL,
602      NULL
603   },
604
605   /* interrupt handler */
606   tms9901_interrupt_callback,
607
608   /* clock rate = 3.58MHz??? 2.68MHz??? */
609   /*3579545.*/2684658.75
610};
611
612/*
613    Handles the EXTINT line. This one is directly connected to the 9901 (/INT1).
614*/
615WRITE_LINE_DEVICE_HANDLER( console_extint )
616{
617   tms9901_set_single_int(device->machine().device("tms9901"), 1, state);
618}
619
620WRITE_LINE_DEVICE_HANDLER( console_notconnected )
621{
622   logerror("Setting a not connected line ... ignored\n");
623}
624
625WRITE_LINE_DEVICE_HANDLER( console_ready )
626{
627   logerror("READY line set ... not yet connected, level=%02x\n", state);
628}
629
630/***************************************************************************
631    DEVICE LIFECYCLE FUNCTIONS
632***************************************************************************/
633
634static DEVICE_START( tiboard )
635{
636   tiboard_state *board = get_safe_token(device);
637   logerror("Starting TI-99 board\n");
638   board->tms9901 = device->siblingdevice("tms9901");
639   board->gromport = device->siblingdevice("gromport");
640}
641
642static DEVICE_STOP( tiboard )
643{
644   logerror("Stopping TI-99 board\n");
645}
646
647static DEVICE_RESET( tiboard )
648{
649   tiboard_state *board = get_safe_token(device);
650   const tiboard_config* conf = (const tiboard_config*)get_config(device);
651   board->mode = conf->mode;
652
653   /* clear keyboard interface state (probably overkill, but can't harm) */
654   board->keyCol = 0;
655   board->alphaLockLine = 0;
656   board->handset = NULL;
657   board->mecmouse = NULL;
658
659   if (board->mode == TI994)
660   {
661      /* reset handset */
662      tms9901_set_single_int(board->tms9901, 12, 0);
663
664      if (input_port_read(device->machine(), "HCI") & HCI_IR)
665         board->handset = device->siblingdevice("handset");
666   }
667
668   if (input_port_read(device->machine(), "HCI") & HCI_MECMOUSE)
669      board->mecmouse = device->siblingdevice("mecmouse");
670
671   if (board->mode != TI998)
672   {
673      set_gk_switches(board->gromport, 1, input_port_read(device->machine(), "GKSWITCH1"));
674      set_gk_switches(board->gromport, 2, input_port_read(device->machine(), "GKSWITCH2"));
675      set_gk_switches(board->gromport, 3, input_port_read(device->machine(), "GKSWITCH3"));
676      set_gk_switches(board->gromport, 4, input_port_read(device->machine(), "GKSWITCH4"));
677      set_gk_switches(board->gromport, 5, input_port_read(device->machine(), "GKSWITCH5"));
678   }
679}
680
681static const char DEVTEMPLATE_SOURCE[] = __FILE__;
682
683#define DEVTEMPLATE_ID(p,s)             p##tiboard##s
684#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
685#define DEVTEMPLATE_NAME                "TI-99 system board"
686#define DEVTEMPLATE_FAMILY              "Internal component"
687#include "devtempl.h"
688
689DEFINE_LEGACY_DEVICE( TIBOARD, tiboard );
src/mess/machine/ti99/tiboard.h
r14513r14514
1#ifndef __TIBOARD__
2#define __TIBOARD__
3
4#include "machine/tms9901.h"
5#include "video/v9938.h"
6
7void tms9901_set_int2(device_t *device, int state);
8void tms9901_set_int2_v9938(device_t *, v99x8_device &tms, int state);
9
10extern const tms9901_interface tms9901_wiring_ti99_4;
11extern const tms9901_interface tms9901_wiring_ti99_4a;
12extern const tms9901_interface tms9901_wiring_ti99_8;
13
14typedef struct _tiboard_config
15{
16   int mode;
17} tiboard_config;
18
19enum
20{
21   TI994 = 1,
22   TI994A,
23   TI998
24};
25
26/* device interface */
27DECLARE_LEGACY_DEVICE( TIBOARD, tiboard );
28
29WRITE_LINE_DEVICE_HANDLER( console_extint );
30WRITE_LINE_DEVICE_HANDLER( console_notconnected );
31WRITE_LINE_DEVICE_HANDLER( console_ready );
32
33INTERRUPT_GEN( ti99_vblank_interrupt );
34
35#define MCFG_TI994_BOARD_ADD(_tag )         \
36   MCFG_DEVICE_ADD(_tag, TIBOARD, 0) \
37   MCFG_DEVICE_CONFIG_DATA32( tiboard_config, mode, TI994)
38
39#define MCFG_TI994A_BOARD_ADD(_tag )         \
40   MCFG_DEVICE_ADD(_tag, TIBOARD, 0) \
41   MCFG_DEVICE_CONFIG_DATA32( tiboard_config, mode, TI994A)
42
43#define MCFG_TI998_BOARD_ADD(_tag )         \
44   MCFG_DEVICE_ADD(_tag, TIBOARD, 0)   \
45   MCFG_DEVICE_CONFIG_DATA32( tiboard_config, mode, TI998)
46
47#endif
src/mess/machine/ti99/grom.c
r14513r14514
2525    MO  = Mode. 1=address counter access, 0=data access
2626    GS* = GROM select. 0=select, 1=deselect
2727
28
29    GROMs are slow ROM devices, which are
30    interfaced via a 8-bit data bus, and include an internal address pointer
31    which is incremented after each read.  This implies that accesses are
32    faster when reading consecutive bytes, although the address pointer can be
33    read and written at any time.
34
35    They are generally used to store programs in GPL (Graphic Programming
36    Language: a proprietary, interpreted language - the interpreter takes most
37    of a TI99/4(a) CPU ROMs).  They can used to store large pieces of data,
38    too.
39
40    Both TI-99/4 and TI-99/4a include three GROMs, with some start-up code,
41    system routines and TI-Basic.  TI99/4 includes an additional Equation
42    Editor.  According to the preliminary schematics found on ftp.whtech.com,
43    TI-99/8 includes the three standard GROMs and 16 GROMs for the UCSD
44    p-system.  TI99/2 does not include GROMs at all, and was not designed to
45    support any, although it should be relatively easy to create an expansion
46    card with the GPL interpreter and a /4a cartridge port.
47
48The simple way:
49
50    Each GROM is logically 8kb long.
51
52    Communication with GROM is done with 4 memory-mapped locations.  You can read or write
53    a 16-bit address pointer, and read data from GROMs.  One register allows to write data, too,
54    which would support some GRAMs, but AFAIK TI never built such GRAMs (although docs from TI
55    do refer to this possibility).
56
57    Since address are 16-bit long, you can have up to 8 GROMs.  So, a cartridge may
58    include up to 5 GROMs.  (Actually, there is a way more GROMs can be used - see below...)
59
60    The address pointer is incremented after each GROM operation, but it will always remain
61    within the bounds of the currently selected GROM (e.g. after 0x3fff comes 0x2000).
62
63Some details:
64
65    Original TI-built GROM are 6kb long, but take 8kb in address space.  The extra 2kb can be
66    read, and follow the following formula:
67        GROM[0x1800+offset] = GROM[0x0800+offset] | GROM[0x1000+offset];
68    (sounds like address decoding is incomplete - we are lucky we don't burn any silicon when
69    doing so...)
70
71    Needless to say, some hackers simulated 8kb GRAMs and GROMs with normal RAM/PROM chips and
72    glue logic.
73
74GROM ports:
75
76    When accessing the GROMs registers, 8 address bits (cpu_addr & 0x03FC) may
77    be used as a port number, which permits the use of up to 256 independent
78    GROM ports, with 64kb of address space in each.  TI99/4(a) ROMs can take
79    advantage of the first 16 ports: it will look for GPL programs in every
80    GROM of the 16 first ports.  Additionally, while the other 240 ports cannot
81    contain standard GROMs with GPL code, they may still contain custom GROMs
82    with data.
83
84    Note, however, that the TI99/4(a) console does not decode the page number, so console GROMs
85    occupy the first 24kb of EVERY port, and cartridge GROMs occupy the next 40kb of EVERY port
86    (with the exception of one demonstration from TI that implements several distinct ports).
87    (Note that the TI99/8 console does have the required decoder.)  Fortunately, GROM drivers
88    have a relatively high impedance, and therefore extension cards can use TTL drivers to impose
89    another value on the data bus with no risk of burning the console GROMs.  This hack permits
90    the addition of additionnal GROMs in other ports, with the only side effect that whenever
91    the address pointer in port N is altered, the address pointer of console GROMs in port 0
92    is altered, too.  Overriding the system GROMs with custom code is possible, too (some hackers
93    have done so), but I would not recommended it, as connecting such a device to a TI99/8 might
94    burn some drivers.
95
96    The p-code card (-> UCSD Pascal system) contains 8 GROMs, all in port 16.  This port is not
97    in the range recognized by the TI ROMs (0-15), and therefore it is used by the p-code DSR ROMs
98    as custom data.  Additionally, some hackers used the extra ports to implement "GRAM" devices.
99
28100    Every GROM has an internal ID which represents the high-order three
29101    address bits. The address counter can be set to any value from 0
30102    to 0xffff; the GROM will only react if selected and if the current
r14513r14514
42114    returns the MSB, the second (and all following accesses) return the LSB.
43115
44116    Michael Zapf, August 2010
117    January 2012: rewritten as class
45118
46119***************************************************************************/
47120
48121#include "emu.h"
49122#include "grom.h"
50#include "datamux.h"
51123
52/* ti99_grom_state: GROM state */
53struct _ti99grom_state
54{
55   /* Address pointer. */
56   // This value is always expected to be in the range 0x0000 - 0xffff, even
57   // when this GROM is not addressed.
58   UINT16 address;
124#define LOG logerror
125#define VERBOSE 1
59126
60   /* GROM data buffer. */
61   UINT8 buffer;
62
63   /* Internal flip-flop. Used when retrieving the address counter. */
64   UINT8 raddr_LSB;
65
66   /* Internal flip-flops. Used when writing the address counter.*/
67   UINT8 waddr_LSB;
68
69   /* ID of this GROM. */
70   UINT8 ident;
71
72   /* Pointer to the memory region contained in this GROM. */
73   UINT8 *memptr;
74
75   /* GROM size. May be 0x1800 or 0x2000. */
76   // If the GROM has only 6 KiB, the remaining 2 KiB are filled with a
77   // specific byte pattern which is created by a logical OR of lower
78   // regions
79   int extended;
80
81   /* Determines whether this is a GRAM. */
82   int writable;
83
84   /* Determines whether there is a rollover at the end of the address space. */
85   int rollover;
86
87   /* Ready callback. This line is usually connected to the READY pin of the CPU. */
88   devcb_resolved_write_line gromready;
89};
90typedef struct _ti99grom_state ti99grom_state;
91
92INLINE ti99grom_state *get_safe_token(device_t *device)
93{
94   assert(device != NULL);
95   assert(device->type() == GROM);
96
97   return (ti99grom_state *)downcast<legacy_device_base *>(device)->token();
98}
99
100INLINE const ti99grom_config *get_config(device_t *device)
101{
102   assert(device != NULL);
103   assert(device->type() == GROM);
104
105   return (const ti99grom_config *) downcast<const legacy_device_base *>(device)->inline_config();
106}
107
108127/*
109    Indicates whether this chip will react on the next read/write data
110    access. We do not have a tri-state handling on the read handlers, so this
111    serves to avoid the read access.
128    Constructor.
112129*/
113static int is_selected(device_t *chip)
130ti99_grom_device::ti99_grom_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
131: bus8z_device(mconfig, GROM, "TI-99 GROM device", tag, owner, clock)
114132{
115   ti99grom_state *grom = get_safe_token(chip);
116   return (((grom->address >> 13)&0x07)==grom->ident);
117133}
118134
119135/*
r14513r14514
121137    defined by the offset (0 or 1). This is the enhanced read function with
122138    Z state.
123139*/
124READ8Z_DEVICE_HANDLER( ti99grom_rz )
140READ8Z_MEMBER( ti99_grom_device::readz )
125141{
126   ti99grom_state *grom = get_safe_token(device);
127
128142   if (offset & 2)
129143   {
130144      // GROMs generally answer the address read request
r14513r14514
133147
134148      /* When reading, reset the hi/lo flag byte for writing. */
135149      /* TODO: Verify this with a real machine. */
136      grom->waddr_LSB = FALSE;
150      m_waddr_LSB = false;
137151
138152      /* Address reading is done in two steps; first, the high byte */
139153      /* is transferred, then the low byte. */
140      if (grom->raddr_LSB)
154      if (m_raddr_LSB)
141155      {
142156         /* second pass */
143         *value = grom->address & 0x00ff;
144         grom->raddr_LSB = FALSE;
157         *value = m_address & 0x00ff;
158         m_raddr_LSB = false;
145159      }
146160      else
147161      {
148162         /* first pass */
149         *value = (grom->address & 0xff00)>>8;
150         grom->raddr_LSB = TRUE;
163         *value = (m_address & 0xff00)>>8;
164         m_raddr_LSB = true;
151165      }
152166   }
153167   else
154168   {
155      if (((grom->address >> 13)&0x07)==grom->ident)
169      if (((m_address >> 13)&0x07)==m_ident)
156170      {
157         /* GROMs are buffered. Data is retrieved from a buffer, */
158         /* while the buffer is replaced with the next cell content. */
159         *value = grom->buffer;
171         // GROMs are buffered. Data is retrieved from a buffer,
172         // while the buffer is replaced with the next cell content.
173         *value = m_buffer;
160174
161         /* Get next value, put it in buffer. Note that the GROM */
162         /* wraps at 8K boundaries. */
163         UINT16 addr = grom->address-(grom->ident<<13);
175         // Get next value, put it in buffer. Note that the GROM
176         // wraps at 8K boundaries.
177         UINT16 addr = m_address-(m_ident<<13);
164178
165         if (!grom->extended && ((grom->address&0x1fff)>=0x1800))
166            grom->buffer = grom->memptr[addr-0x1000] | grom->memptr[addr-0x0800];
179         if (!m_extended && ((m_address&0x1fff)>=0x1800))
180            m_buffer = m_memptr[addr-0x1000] | m_memptr[addr-0x0800];
167181         else
168            grom->buffer = grom->memptr[addr];
182            m_buffer = m_memptr[addr];
169183      }
170      /* Note that all GROMs update their address counter. */
171      if (grom->rollover)
172         grom->address = ((grom->address + 1) & 0x1FFF) | (grom->address & 0xE000);
184      // Note that all GROMs update their address counter.
185      if (m_rollover)
186         m_address = ((m_address + 1) & 0x1FFF) | (m_address & 0xE000);
173187      else
174         grom->address = (grom->address + 1)&0xFFFF;
188         m_address = (m_address + 1)&0xFFFF;
175189
176      /* Reset the read and write address flipflops. */
177      grom->raddr_LSB = grom->waddr_LSB = FALSE;
190      // Reset the read and write address flipflops.
191      m_raddr_LSB = m_waddr_LSB = false;
178192   }
179193}
180194
181195/*
182    Reading from the chip. Represents an access with M=1, GS*=0. The MO bit is
183    defined by the offset (0 or 1).
184*/
185#ifdef UNUSED_FUNCTION
186READ8_DEVICE_HANDLER( ti99grom_r )
187{
188   UINT8 reply = 0;
189   ti99grom_rz(device, offset, &reply);
190   return reply;
191}
192#endif
193
194
195/*
196196    Writing to the chip. Represents an access with M=0, GS*=0. The MO bit is
197197    defined by the offset (0 or 1).
198198*/
199WRITE8_DEVICE_HANDLER( ti99grom_w )
199WRITE8_MEMBER( ti99_grom_device::write )
200200{
201   ti99grom_state *grom = get_safe_token(device);
202201   if (offset & 2)
203202   {
204203      /* write GROM address */
205204      /* see comments above */
206      grom->raddr_LSB = FALSE;
205      m_raddr_LSB = false;
207206
208207      /* Implements the internal flipflop. */
209208      /* The Editor/Assembler manuals says that the current address */
210209      /* plus one is returned. This effect is properly emulated */
211210      /* by using a read-ahead buffer. */
212      if (grom->waddr_LSB)
211      if (m_waddr_LSB)
213212      {
214213         /* Accept low byte (2nd write) */
215         grom->address = (grom->address & 0xFF00) | data;
214         m_address = (m_address & 0xFF00) | data;
216215         /* Setting the address causes a new prefetch */
217         if (is_selected(device))
216         if (is_selected())
218217         {
219            grom->buffer = grom->memptr[grom->address-(grom->ident<<13)];
218            m_buffer = m_memptr[m_address-(m_ident<<13)];
220219         }
221         grom->waddr_LSB = FALSE;
220         m_waddr_LSB = false;
222221      }
223222      else
224223      {
225224         /* Accept high byte (1st write). Do not advance the address conter. */
226         grom->address = (data << 8) | (grom->address & 0xFF);
227         grom->waddr_LSB = TRUE;
225         m_address = (data << 8) | (m_address & 0xFF);
226         m_waddr_LSB = true;
228227         return;
229228      }
230229   }
231230   else
232231   {
233232      /* write GRAM data */
234      if ((((grom->address >> 13)&0x07)==grom->ident) && grom->writable)
233      if ((((m_address >> 13)&0x07)==m_ident) && m_writable)
235234      {
236235         UINT16 write_addr;
237236         // We need to rewind by 1 because the read address has already advanced.
238237         // However, do not change the address counter!
239         if (grom->rollover)
240            write_addr = ((grom->address + 0x1fff) & 0x1FFF) | (grom->address & 0xE000);
238         if (m_rollover)
239            write_addr = ((m_address + 0x1fff) & 0x1FFF) | (m_address & 0xE000);
241240         else
242            write_addr = (grom->address - 1) & 0xFFFF;
241            write_addr = (m_address - 1) & 0xFFFF;
243242
244         // UINT16 addr = grom->address-(grom->ident<<13);
245         if (grom->extended || ((grom->address&0x1fff)<0x1800))
246            grom->memptr[write_addr-(grom->ident<<13)] = data;
243         // UINT16 addr = m_address-(m_ident<<13);
244         if (m_extended || ((m_address&0x1fff)<0x1800))
245            m_memptr[write_addr-(m_ident<<13)] = data;
247246      }
248      grom->raddr_LSB = grom->waddr_LSB = FALSE;
247      m_raddr_LSB = m_waddr_LSB = false;
249248   }
250249
251   if (grom->rollover)
252      grom->address = ((grom->address + 1) & 0x1FFF) | (grom->address & 0xE000);
250   if (m_rollover)
251      m_address = ((m_address + 1) & 0x1FFF) | (m_address & 0xE000);
253252   else
254      grom->address = (grom->address + 1) & 0xFFFF;
253      m_address = (m_address + 1) & 0xFFFF;
255254}
256255
257256/***************************************************************************
258257    DEVICE FUNCTIONS
259258***************************************************************************/
260259
261static DEVICE_START( ti99grom )
260void ti99_grom_device::device_start(void)
262261{
263   ti99grom_state *grom = get_safe_token(device);
264   const ti99grom_config* gromconf = (const ti99grom_config*)get_config(device);
265
266   grom->writable = gromconf->writeable;
267   grom->ident = gromconf->ident;
268
269   grom->extended = (gromconf->size==0x1800)? TRUE : FALSE;
270
271   grom->rollover = gromconf->rollover;
272
273   devcb_write_line ready = DEVCB_LINE(gromconf->ready);
274   grom->gromready.resolve(ready, *device);
262   m_gromready.resolve(m_ready, *this);
275263   // Test
276   // grom->gromready(ASSERT_LINE);
264   // m_gromready(ASSERT_LINE);
277265}
278266
279static DEVICE_STOP( ti99grom )
267void ti99_grom_device::device_config_complete(void)
280268{
269   const ti99grom_config *conf = reinterpret_cast<const ti99grom_config *>(static_config());
270
271   if ( conf != NULL )
272   {
273      *static_cast<ti99grom_config *>(this) = *conf;
274   }
281275}
282276
283static DEVICE_RESET( ti99grom )
277void ti99_grom_device::device_reset(void)
284278{
285   const ti99grom_config* gromconf = (const ti99grom_config*)get_config(device);
286   ti99grom_state *grom = get_safe_token(device);
287   grom->address = 0;
288   grom->raddr_LSB = FALSE;
289   grom->waddr_LSB = FALSE;
290   grom->buffer = 0;
279   m_address = 0;
280   m_raddr_LSB = false;
281   m_waddr_LSB = false;
282   m_buffer = 0;
291283
292   /* Try to get the pointer to the memory contents from the parent */
293   if (gromconf->region==NULL)
294      grom->memptr = (*gromconf->get_memory)(device->owner());
295   else
296      grom->memptr = device->machine().region(gromconf->region)->base();
284   m_memptr = owner()->subregion(m_regionname)->base();
285   assert (m_memptr!=NULL);
297286
298   //  TODO: Check whether this may be 0 for console GROMs.
299   //  assert (grom->memptr!=NULL);
300
301   grom->memptr += gromconf->offset;
302
303   // TODO: If the GROM is a subdevice of another device, and a region
304   // is specified, we need to first determine the device tag and concatenate
305   // the GROM region behind this device tag; otherwise the region will not be
306   // found. May be done with an astring.
287   m_memptr += m_offset_reg;
307288}
308289
309static const char DEVTEMPLATE_SOURCE[] = __FILE__;
310
311#define DEVTEMPLATE_ID(p,s)             p##ti99grom##s
312#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
313#define DEVTEMPLATE_NAME                "tmc0430"
314#define DEVTEMPLATE_FAMILY              "TTL"
315#include "devtempl.h"
316
317DEFINE_LEGACY_DEVICE( GROM, ti99grom );
290const device_type GROM = &device_creator<ti99_grom_device>;
src/mess/machine/ti99/grom.h
r14513r14514
11/***************************************************************************
2
23    GROM emulation
4    See grom.c for documentation,
35
4    TI99/4x hardware supports GROMs.  GROMs are slow ROM devices, which are
5    interfaced via a 8-bit data bus, and include an internal address pointer
6    which is incremented after each read.  This implies that accesses are
7    faster when reading consecutive bytes, although the address pointer can be
8    read and written at any time.
6    Michael Zapf
97
10    They are generally used to store programs in GPL (Graphic Programming
11    Language: a proprietary, interpreted language - the interpreter takes most
12    of a TI99/4(a) CPU ROMs).  They can used to store large pieces of data,
13    too.
8    February 2012: Rewritten as class
149
15    Both TI-99/4 and TI-99/4a include three GROMs, with some start-up code,
16    system routines and TI-Basic.  TI99/4 includes an additional Equation
17    Editor.  According to the preliminary schematics found on ftp.whtech.com,
18    TI-99/8 includes the three standard GROMs and 16 GROMs for the UCSD
19    p-system.  TI99/2 does not include GROMs at all, and was not designed to
20    support any, although it should be relatively easy to create an expansion
21    card with the GPL interpreter and a /4a cartridge port.
10***************************************************************************/
11#ifndef __TI99GROM__
12#define __TI99GROM__
2213
23The simple way:
14#include "ti99defs.h"
2415
25    Each GROM is logically 8kb long.
16typedef struct _ti99grom_config
17{
18   bool            m_writable;
19   int             m_ident;
20   const char         *m_regionname;
21   offs_t            m_offset_reg;
22   // If the GROM has only 6 KiB, the remaining 2 KiB are filled with a
23   // specific byte pattern which is created by a logical OR of lower
24   // regions
25   bool            m_extended;
26   bool            m_rollover;   // some GRAM simulations do not implement rollover
27   devcb_write_line   m_ready;
28} ti99grom_config;
2629
27    Communication with GROM is done with 4 memory-mapped locations.  You can read or write
28    a 16-bit address pointer, and read data from GROMs.  One register allows to write data, too,
29    which would support some GRAMs, but AFAIK TI never built such GRAMs (although docs from TI
30    do refer to this possibility).
30#define GROM_CONFIG(name) \
31   const ti99grom_config(name) =
3132
32    Since address are 16-bit long, you can have up to 8 GROMs.  So, a cartridge may
33    include up to 5 GROMs.  (Actually, there is a way more GROMs can be used - see below...)
3433
35    The address pointer is incremented after each GROM operation, but it will always remain
36    within the bounds of the currently selected GROM (e.g. after 0x3fff comes 0x2000).
34extern const device_type GROM;
3735
38Some details:
36/*
37    ti99_grom. For bus8z_device see ti99defs.h
38*/
39class ti99_grom_device : public bus8z_device, ti99grom_config
40{
41public:
42   ti99_grom_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
43   DECLARE_READ8Z_MEMBER(readz);
44   DECLARE_WRITE8_MEMBER(write);
3945
40    Original TI-built GROM are 6kb long, but take 8kb in address space.  The extra 2kb can be
41    read, and follow the following formula:
42        GROM[0x1800+offset] = GROM[0x0800+offset] | GROM[0x1000+offset];
43    (sounds like address decoding is incomplete - we are lucky we don't burn any silicon when
44    doing so...)
46private:
47   /* Address pointer. */
48   // This value is always expected to be in the range 0x0000 - 0xffff, even
49   // when this GROM is not addressed.
50   UINT16 m_address;
4551
46    Needless to say, some hackers simulated 8kb GRAMs and GROMs with normal RAM/PROM chips and
47    glue logic.
52   /* GROM data buffer. */
53   UINT8 m_buffer;
4854
49GPL ports:
55   /* Internal flip-flop. Used when retrieving the address counter. */
56   bool m_raddr_LSB;
5057
51    When accessing the GROMs registers, 8 address bits (cpu_addr & 0x03FC) may
52    be used as a port number, which permits the use of up to 256 independent
53    GROM ports, with 64kb of address space in each.  TI99/4(a) ROMs can take
54    advantage of the first 16 ports: it will look for GPL programs in every
55    GROM of the 16 first ports.  Additionally, while the other 240 ports cannot
56    contain standard GROMs with GPL code, they may still contain custom GROMs
57    with data.
58   /* Internal flip-flops. Used when writing the address counter.*/
59   bool m_waddr_LSB;
5860
59    Note, however, that the TI99/4(a) console does not decode the page number, so console GROMs
60    occupy the first 24kb of EVERY port, and cartridge GROMs occupy the next 40kb of EVERY port
61    (with the exception of one demonstration from TI that implements several distinct ports).
62    (Note that the TI99/8 console does have the required decoder.)  Fortunately, GROM drivers
63    have a relatively high impedance, and therefore extension cards can use TTL drivers to impose
64    another value on the data bus with no risk of burning the console GROMs.  This hack permits
65    the addition of additionnal GROMs in other ports, with the only side effect that whenever
66    the address pointer in port N is altered, the address pointer of console GROMs in port 0
67    is altered, too.  Overriding the system GROMs with custom code is possible, too (some hackers
68    have done so), but I would not recommended it, as connecting such a device to a TI99/8 might
69    burn some drivers.
61   /* Pointer to the memory region contained in this GROM. */
62   UINT8 *m_memptr;
7063
71    The p-code card (-> UCSD Pascal system) contains 8 GROMs, all in port 16.  This port is not
72    in the range recognized by the TI ROMs (0-15), and therefore it is used by the p-code DSR ROMs
73    as custom data.  Additionally, some hackers used the extra ports to implement "GRAM" devices.
64   /* Ready callback. This line is usually connected to the READY pin of the CPU. */
65   devcb_resolved_write_line m_gromready;
7466
67   /* Indicates whether this device will react on the next read/write data access. */
68   int is_selected()
69   {
70      return (((m_address >> 13)&0x07)==m_ident);
71   }
7572
76***************************************************************************/
73   void device_start(void);
74   void device_reset(void);
75   void device_config_complete(void);
76};
7777
78#define READ8Z_DEVICE_HANDLER(name)      void name(ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT8 *value)
7978
80typedef struct _ti99grom_config
81{
82   int                  writeable;
83   int                ident;
84   const char            *region;
85   UINT8               *(*get_memory)(device_t *device);
86   offs_t               offset;
87   int                size;
88   int                rollover;   // some GRAM simulations do not implement rollover
89   write_line_device_func   ready;
90} ti99grom_config;
91
92typedef struct _grom_callback
93{
94   devcb_write_line ready;
95} grom_callback;
96
97WRITE8_DEVICE_HANDLER( ti99grom_w );
98READ8Z_DEVICE_HANDLER( ti99grom_rz );
99
100DECLARE_LEGACY_DEVICE( GROM, ti99grom );
101
102#define MCFG_GROM_ADD(_tag, _id, _region, _offset, _size, _ready)   \
79#define MCFG_GROM_ADD(_tag, _config)   \
10380   MCFG_DEVICE_ADD(_tag, GROM, 0)   \
104   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, writeable, FALSE) \
105   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, ident, _id) \
106   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, region, _region) \
107   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, offset, _offset) \
108   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, size, _size) \
109   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, rollover, FALSE) \
110   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, ready, _ready)
81   MCFG_DEVICE_CONFIG(_config)
11182
112#define MCFG_GROM_ADD_P(_tag, _id, _memptr, _offset, _size, _ready)   \
113   MCFG_DEVICE_ADD(_tag, GROM, 0)   \
114   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, writeable, FALSE) \
115   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, ident, _id) \
116   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, get_memory, _memptr) \
117   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, region, NULL) \
118   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, offset, _offset) \
119   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, size, _size) \
120   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, rollover, FALSE) \
121   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, ready, _ready)
122
123#define MCFG_GRAM_ADD(_tag, _id, _region, _offset, _size, _rollover, _ready)      \
124   MCFG_DEVICE_ADD(_tag, GROM, 0)                        \
125   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, writeable, TRUE)   \
126   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, ident, _id)      \
127   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, region, _region) \
128   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, offset, _offset)    \
129   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, size, _size)   \
130   MCFG_DEVICE_CONFIG_DATA32(ti99grom_config, rollover, _rollover) \
131   MCFG_DEVICE_CONFIG_DATAPTR(ti99grom_config, ready, _ready)
132
133
83#endif
src/mess/machine/ti99/gromport.c
r14513r14514
11/**********************************************************************
22
3    gromport.c
3    Gromport: the cartridge port of the TI-99/4, TI-99/4A, and
4    TI-99/8 console. The name refers to the main intended application
5    scenario, that is, to host cartridges with GROMs. (The other port
6    of the console is called I/O port.)
47
5*********************************************************************/
8    Over the years, many different kinds of cartridges appeared; the
9    usual cartridge layout contains GROMs and possibly a ROM circuit
10    for time-critical code (since the GROMs were utterly slow).
611
7#include "emu.h"
8#include "gromport.h"
9#include "grom.h"
10#include "pool.h"
11#include "unzip.h"
12#include "corestr.h"
13#include "xmlfile.h"
14#include "machine/ram.h"
12    Texas Instruments kept control over all software development by
13    reserving GROMs and their programming to them and licensees.
1514
16/* start of multicart */
17#define TAG_PCB      "pcb"
15    Interestingly, the OS designers planned for an alternative -
16    cartridges which can be run with EPROMs only - which provided
17    a means for third-party authors to create cartridges without
18    TI license. The last action of TI before their retreat from the
19    home computer market was to disable this alternative.
1820
19DECLARE_LEGACY_IMAGE_DEVICE(MULTICARTSLOT, multicartslot);
21    Nevertheless, the majority of consoles runs with different kinds of
22    cartridges; most recent developments by third party include
23    multi-bank cartridges with storage space up to 128 KiB.
2024
21/***************************************************************************
22    TYPE DEFINITIONS
23***************************************************************************/
25    The implementation as shown here goes beyond the simple port
26    by realizing a "multi-cartridge extender". This is a somewhat
27    mythical device which was never available for the normal
28    customer, but there are reports of the existance of such a device
29    in development labs or demonstrations.
2430
25enum _multicart_load_flags
26{
27   MULTICART_FLAGS_DONT_LOAD_RESOURCES   = 0x00,
28   MULTICART_FLAGS_LOAD_RESOURCES      = 0x01
29};
30typedef enum _multicart_load_flags multicart_load_flags;
31    The interesting thing about this is that the OS of the console
32    fully supports this multi-cartridge extender, providing a selection
33    option on the screen to switch between different plugged-in
34    cartridges.
3135
32enum _multicart_resource_type
33{
34   MULTICART_RESOURCE_TYPE_INVALID,
35   MULTICART_RESOURCE_TYPE_ROM,
36   MULTICART_RESOURCE_TYPE_RAM
37};
38typedef enum _multicart_resource_type multicart_resource_type;
36    The switching is possible by decoding address lines that are reserved
37    for GROM access. GROMs are accessed via four separate addresses
38    9800, 9802, 9C00, 9C02. The addressing scheme looks like this:
3939
40typedef struct _multicart_resource multicart_resource;
41struct _multicart_resource
42{
43   const char *            id;
44   const char *            filename;
45   multicart_resource *      next;
46   multicart_resource_type      type;
47   UINT32                  length;
48   void *                  ptr;
49};
40    1001 1Wxx xxxx xxM0        W = write(1), read(0), M = address(1), data(0)
5041
42    This leaves 8 bits (256 options) which are not decoded inside the
43    console. As the complete address is routed to the port, some circuit
44    just needs to decode the xxx lines and turn on the respective slot.
5145
52typedef struct _multicart_socket multicart_socket;
53struct _multicart_socket
54{
55   const char *            id;
56   multicart_socket *         next;
57   const multicart_resource *   resource;
58   void *                  ptr;
59};
46    One catch must be considered: Some cartridges contain ROMs which are
47    directly accessed and not via ports. This means that the ROMs must
48    be activated according to the slot that is selected.
6049
50    Another issue: Each GROM contains an own address counter and an ID.
51    According to the ID the GROM only delivers data if the address counter
52    is within the ID area (0 = 0000-1fff, 1=2000-3fff ... 7=e000-ffff).
53    Thus it is essential that all GROMs stay in sync with their address
54    counters. We have to route all address settings to all slots and their
55    GROMs, even when the slot has not been selected before. The selected
56    just shows its effect when data is read. In this case, only the
57    data from the selected slot will be delivered.
6158
62typedef struct _multicart_private multicart_private;
59    This may be considered as a design flaw within the complete cartridge system
60    which eventually led to TI not manufacturing that device for the broad
61    market.
6362
64typedef struct _multicart_t multicart_t;
65struct _multicart_t
66{
67   const multicart_resource *   resources;
68   const multicart_socket *   sockets;
69   const char *            pcb_type;
70   const char *            gamedrv_name; /* need this to find the path to the nvram files */
71   multicart_private *         data;
72};
63    --------
7364
74typedef struct _multicartslot_t multicartslot_t;
75struct _multicartslot_t
76{
77   device_t *pcb_device;
78   multicart_t *mc;
79};
65    This implementation also contains the GRAMKracker, a popular device
66    with persistent RAM and a cartridge slot. With it, cartridge contents
67    can be read, stored in its RAM, and modified. Moreover, the
68    console GROMs can be loaded into the device and overrun the data
69    delivered by the console GROMs. (Take care when changing something
70    in this emulation - this overrun is emulated by the sequence in which
71    the devices on the datamux are executed.)
8072
73    --------
8174
82typedef struct _multicartslot_pcb_type multicartslot_pcb_type;
83struct _multicartslot_pcb_type
84{
85   const char *               name;
86   device_type                  devtype;
87};
75    The second part of this file contains the implementation of the
76    cartridge image format (RPK).
8877
78    Michael Zapf, 2012
8979
90typedef struct _multicartslot_config multicartslot_config;
91struct _multicartslot_config
92{
93   const char *               extensions;
94   device_start_func            device_start;
95   device_image_load_func         device_load;
96   device_image_unload_func      device_unload;
97   multicartslot_pcb_type         pcb_types[16];
98};
80*********************************************************************/
9981
100enum _multicart_open_error
101{
102   MCERR_NONE,
103   MCERR_NOT_MULTICART,
104   MCERR_CORRUPT,
105   MCERR_OUT_OF_MEMORY,
106   MCERR_XML_ERROR,
107   MCERR_INVALID_FILE_REF,
108   MCERR_ZIP_ERROR,
109   MCERR_MISSING_RAM_LENGTH,
110   MCERR_INVALID_RAM_SPEC,
111   MCERR_UNKNOWN_RESOURCE_TYPE,
112   MCERR_INVALID_RESOURCE_REF,
113   MCERR_INVALID_FILE_FORMAT,
114   MCERR_MISSING_LAYOUT,
115   MCERR_NO_PCB_OR_RESOURCES
116};
117typedef enum _multicart_open_error multicart_open_error;
82#include "gromport.h"
83#include "ti99defs.h"
11884
119const char *multicart_error_text(multicart_open_error error);
85#define VERBOSE 1
86#define LOG logerror
12087
121/***************************************************************************
122    PROTOTYPES
123***************************************************************************/
88#define GROM_AREA 0x9800
89#define GROM_MASK 0xf800
90#define AUTO -1
12491
125/* opens a multicart */
126multicart_open_error multicart_open(emu_options &options, const char *filename, const char *drvname, multicart_load_flags load_flags, multicart_t **cart);
92#define CARTGROM_TAG "grom_contents"
93#define CARTROM_TAG "rom_contents"
94#define CARTROM2_TAG "rom2_contents"
12795
128/* closes a multicart */
129void multicart_close(emu_options &options, multicart_t *cart);
96#define GROM3_TAG "grom3"
97#define GROM4_TAG "grom4"
98#define GROM5_TAG "grom5"
99#define GROM6_TAG "grom6"
100#define GROM7_TAG "grom7"
130101
131/* accesses the PCB associated with this cartslot */
132device_t *cartslot_get_pcb(device_t *device);
133
134/* accesses a particular socket */
135void *cartslot_get_socket(device_t *device, const char *socket_name);
136
137/* accesses a particular socket; gets the length of the associated resource */
138int cartslot_get_resource_length(device_t *device, const char *socket_name);
139
140#define DECLARE_LEGACY_CART_SLOT_DEVICE(name, basename) _DECLARE_LEGACY_DEVICE(name, basename, basename##_device, legacy_cart_slot_device_base)
141#define DEFINE_LEGACY_CART_SLOT_DEVICE(name, basename) _DEFINE_LEGACY_DEVICE(name, basename, basename##_device, legacy_cart_slot_device_base)
142
143#define MCFG_MULTICARTSLOT_ADD(_tag)                              \
144   MCFG_DEVICE_ADD(_tag, MULTICARTSLOT, 0)                           \
145
146#define MCFG_MULTICARTSLOT_MODIFY(_tag)                              \
147   MCFG_DEVICE_MODIFY(_tag)                           \
148
149#define MCFG_MULTICARTSLOT_PCBTYPE(_index, _pcb_type_name, _pcb_devtype)         \
150   MCFG_DEVICE_CONFIG_DATAPTR_ARRAY_MEMBER(multicartslot_config, pcb_types, _index, multicartslot_pcb_type, name, _pcb_type_name) \
151   MCFG_DEVICE_CONFIG_DATAPTR_ARRAY_MEMBER(multicartslot_config, pcb_types, _index, multicartslot_pcb_type, devtype, _pcb_devtype)
152
153#define MCFG_MULTICARTSLOT_START(_start)                              \
154   MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_start, DEVICE_START_NAME(_start))
155
156#define MCFG_MULTICARTSLOT_LOAD(_load)                              \
157   MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_load, DEVICE_IMAGE_LOAD_NAME(_load))
158
159#define MCFG_MULTICARTSLOT_UNLOAD(_unload)                           \
160   MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, device_unload, DEVICE_IMAGE_UNLOAD_NAME(_unload))
161
162#define MCFG_MULTICARTSLOT_EXTENSION_LIST(_extensions)                  \
163   MCFG_DEVICE_CONFIG_DATAPTR(multicartslot_config, extensions, _extensions)
164
165
166// ======================> device_cart_slot_interface
167
168// class representing interface-specific live cart_slot
169class device_cart_slot_interface : public device_interface
102enum
170103{
171public:
172   // construction/destruction
173   device_cart_slot_interface(const machine_config &mconfig, device_t &device);
174   virtual ~device_cart_slot_interface();
104   PCB_STANDARD=1,
105   PCB_PAGED,
106   PCB_MINIMEM,
107   PCB_SUPER,
108   PCB_MBX,
109   PCB_PAGED379I,
110   PCB_PAGEDCRU,
111   PCB_GKRACKER
175112};
176113
114#define GKSWITCH1_TAG "GKSWITCH1"
115#define GKSWITCH2_TAG "GKSWITCH2"
116#define GKSWITCH3_TAG "GKSWITCH3"
117#define GKSWITCH4_TAG "GKSWITCH4"
118#define GKSWITCH5_TAG "GKSWITCH5"
177119
178// ======================> legacy_cart_slot_device
120/*******************************************
121    Cartridge PCB types
122*******************************************/
179123
180// legacy_cart_slot_device is a legacy_device_base with a cart_slot interface
181class legacy_cart_slot_device_base :   public legacy_device_base,
182                              public device_cart_slot_interface
124cartridge_pcb_device::cartridge_pcb_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock)
125: bus8z_device(mconfig, type, name, tag, owner, clock)
183126{
184protected:
185   // construction/destruction
186   legacy_cart_slot_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, UINT32 clock, device_get_config_func get_config);
187
188public:
189   using legacy_device_base::get_legacy_int;
190   using legacy_device_base::get_legacy_fct;
191   using legacy_device_base::get_legacy_ptr;
192
193   // device_cart_slot_interface overrides
194};
195
196/***************************************************************************
197    TYPE DEFINITIONS
198***************************************************************************/
199
200struct _multicart_private
201{
202   object_pool *   pool;
203   zip_file *      zip;
204};
205
206
207typedef struct _multicart_load_state multicart_load_state;
208struct _multicart_load_state
209{
210   multicart_t *         multicart;
211   zip_file *         zip;
212   xml_data_node *         layout_xml;
213   xml_data_node *         resources_node;
214   xml_data_node *         pcb_node;
215   multicart_resource *      resources;
216   multicart_socket *      sockets;
217};
218
219static const char error_text[14][30] =
220{
221   "no error",
222   "not a multicart",
223   "module definition corrupt",
224   "out of memory",
225   "xml format error",
226   "invalid file reference",
227   "zip file error",
228   "missing ram length",
229   "invalid ram specification",
230   "unknown resource type",
231   "invalid resource reference",
232   "invalid file format",
233   "missing layout",
234   "no pcb or resource found"
235};
236
237const char *multicart_error_text(multicart_open_error error)
238{
239   return error_text[(int)error];
127   if (VERBOSE>7) LOG("cartridge_pcb_device: common initialization\n");
128   for (int i=0; i < 5; i++) m_grom[i] = 0;
129   m_rom_ptr = 0;
130   m_rom2_ptr = 0;
131   m_ram_ptr = 0;
132   m_grom_size = 0;
133   m_rom_size = 0;
134   m_ram_size = 0;
240135}
241136
242/***************************************************************************
243    INLINE FUNCTIONS
244***************************************************************************/
245
246INLINE multicartslot_t *get_token(device_t *device)
137void cartridge_pcb_device::device_start(void)
247138{
248   assert(device != NULL);
249   assert(device->type() == MULTICARTSLOT);
250   return (multicartslot_t *) downcast<legacy_device_base *>(device)->token();
251139}
252140
253
254INLINE const multicartslot_config *get_config(const device_t *device)
141cartridge_pcb_standard_device::cartridge_pcb_standard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
142: cartridge_pcb_device(mconfig, TICARTPCBSTD, "Standard cartridge PCB", tag, owner, clock)
255143{
256   assert(device != NULL);
257   assert(device->type() == MULTICARTSLOT);
258   return (const multicartslot_config *) downcast<const legacy_device_base *>(device)->inline_config();
259144}
260145
261
262/***************************************************************************
263    IMPLEMENTATION
264***************************************************************************/
265
266/*-------------------------------------------------
267    cartslot_get_pcb
268-------------------------------------------------*/
269
270device_t *cartslot_get_pcb(device_t *device)
146cartridge_pcb_paged_device::cartridge_pcb_paged_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
147: cartridge_pcb_device(mconfig, TICARTPCBPAG, "Paged cartridge PCB", tag, owner, clock)
271148{
272   multicartslot_t *cart = get_token(device);
273   return cart->pcb_device;
274149}
275150
276
277/*-------------------------------------------------
278    cartslot_get_socket
279-------------------------------------------------*/
280
281void *cartslot_get_socket(device_t *device, const char *socket_name)
151cartridge_pcb_minimem_device::cartridge_pcb_minimem_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
152: cartridge_pcb_device(mconfig, TICARTPCBMIN, "MiniMemory cartridge PCB", tag, owner, clock)
282153{
283   multicartslot_t *cart = get_token(device);
284   device_image_interface *image = dynamic_cast<device_image_interface *>(device);
285   void *result = NULL;
286
287   if (cart->mc != NULL)
288   {
289      const multicart_socket *socket;
290      for (socket = cart->mc->sockets; socket != NULL; socket = socket->next)
291      {
292         if (!strcmp(socket->id, socket_name))
293            break;
294      }
295      result = socket ? socket->ptr : NULL;
296   }
297   else if (socket_name[0] == '\0')
298   {
299      result = image->ptr();
300   }
301   return result;
302154}
303155
304/*-------------------------------------------------
305    cartslot_get_resource_length
306-------------------------------------------------*/
307
308int cartslot_get_resource_length(device_t *device, const char *socket_name)
156cartridge_pcb_superspace_device::cartridge_pcb_superspace_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
157: cartridge_pcb_device(mconfig, TICARTPCBMIN, "SuperSpace II cartridge PCB", tag, owner, clock)
309158{
310   multicartslot_t *cart = get_token(device);
311   int result = 0;
312
313   if (cart->mc != NULL)
314   {
315      const multicart_socket *socket;
316
317      for (socket = cart->mc->sockets; socket != NULL; socket = socket->next)
318      {
319         if (!strcmp(socket->id, socket_name)) {
320            break;
321         }
322      }
323      if (socket != NULL)
324         result = socket->resource->length;
325   }
326   else
327      result = 0;
328
329   return result;
330159}
331/*-------------------------------------------------
332    identify_pcb
333-------------------------------------------------*/
334160
335static const multicartslot_pcb_type *identify_pcb(device_image_interface &image)
161cartridge_pcb_mbx_device::cartridge_pcb_mbx_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
162: cartridge_pcb_device(mconfig, TICARTPCBMIN, "MBX cartridge PCB", tag, owner, clock)
336163{
337   const multicartslot_config *config = get_config(&image.device());
338   astring pcb_name;
339   const multicartslot_pcb_type *pcb_type = NULL;
340   multicart_t *mc;
341   int i;
342
343   if (image.exists())
344   {
345      /* try opening this as if it were a multicart */
346      multicart_open_error me = multicart_open(image.device().machine().options(), image.filename(), image.device().machine().system().name, MULTICART_FLAGS_DONT_LOAD_RESOURCES, &mc);
347      if (me == MCERR_NONE)
348      {
349         /* this was a multicart - read from it */
350         pcb_name.cpy(mc->pcb_type);
351         multicart_close(image.device().machine().options(), mc);
352      }
353      else
354      {
355         if (me != MCERR_NOT_MULTICART)
356            fatalerror("multicart error: %s", multicart_error_text(me));
357      }
358
359      /* look for PCB type with matching name */
360      for (i = 0; (i < ARRAY_LENGTH(config->pcb_types)) && (config->pcb_types[i].name != NULL); i++)
361      {
362         if ((config->pcb_types[i].name[0] == '\0') || !strcmp(pcb_name, config->pcb_types[i].name))
363         {
364            pcb_type = &config->pcb_types[i];
365            break;
366         }
367      }
368
369      /* check for unknown PCB type */
370      if ((mc != NULL) && (pcb_type == NULL))
371         fatalerror("Unknown PCB type \"%s\"", pcb_name.cstr());
372   }
373   else
374   {
375      /* no device loaded; use the default */
376      pcb_type = (config->pcb_types[0].name != NULL) ? &config->pcb_types[0] : NULL;
377   }
378   return pcb_type;
379164}
380165
381/*-------------------------------------------------
382    DEVICE_IMAGE_GET_DEVICES(cartslot)
383-------------------------------------------------*/
384static DEVICE_IMAGE_GET_DEVICES(multicartslot)
166cartridge_pcb_paged379i_device::cartridge_pcb_paged379i_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
167: cartridge_pcb_device(mconfig, TICARTPCBPGI, "Paged379i cartridge PCB", tag, owner, clock)
385168{
386   const multicartslot_pcb_type *pcb_type;
387   device_t *device = &image.device();
388
389   pcb_type = identify_pcb(image);
390   if (pcb_type != NULL)
391   {
392      image_add_device_with_subdevices(device,pcb_type->devtype,TAG_PCB,0);
393   }
169   m_rom_page = get_paged379i_bank(15);
394170}
395171
396
397/*-------------------------------------------------
398    find_file
399-------------------------------------------------*/
400
401static const zip_file_header *find_file(zip_file *zip, const char *filename)
172cartridge_pcb_pagedcru_device::cartridge_pcb_pagedcru_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
173: cartridge_pcb_device(mconfig, TICARTPCBPGC, "PagedCRU cartridge PCB", tag, owner, clock)
402174{
403   const zip_file_header *header;
404   for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
405   {
406      if (!core_stricmp(header->filename, filename))
407         return header;
408   }
409   return NULL;
410175}
411176
412static const zip_file_header *find_file_crc(zip_file *zip, const char *filename, UINT32 crc)
177cartridge_pcb_gramkracker_device::cartridge_pcb_gramkracker_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
178: cartridge_pcb_device(mconfig, TICARTPCBGRK, "GRAM Kracker cartridge PCB", tag, owner, clock)
413179{
414   const zip_file_header *header;
415   for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
416   {
417      // if the CRC and name both match, we're good
418      // if the CRC matches and the name doesn't, we're still good
419      if ((!core_stricmp(header->filename, filename)) && (header->crc == crc))
420      {
421         return header;
422      }
423      else if (header->crc == crc)
424      {
425         return header;
426      }
427   }
428   return NULL;
180   m_grom_address = 0;   // for the GROM emulation
181   for (int i=1; i < 6; i++) m_gk_switch[i] = 0;
429182}
430183
431/*-------------------------------------------------
432    find_pcb_and_resource_nodes
433-------------------------------------------------*/
184/*****************************************************************************
185    Common methods.
186******************************************************************************/
434187
435static int find_pcb_and_resource_nodes(xml_data_node *layout_xml,
436   xml_data_node **pcb_node, xml_data_node **resource_node)
437{
438   xml_data_node *romset_node;
439   xml_data_node *configuration_node;
188/* Read function for this cartridge, CRU. */
189void cartridge_pcb_device::crureadz(offs_t offset, UINT8 *value) {  };
440190
441   *pcb_node = NULL;
442   *resource_node = NULL;
191/* Write function for this cartridge, CRU. */
192void cartridge_pcb_device::cruwrite(offs_t offset, UINT8 value) {  };
443193
444   romset_node = xml_get_sibling(layout_xml->child, "romset");
445   if (romset_node == NULL)
446      return FALSE;
447
448   configuration_node = xml_get_sibling(romset_node->child, "configuration");
449   if (configuration_node == NULL)
450      return FALSE;
451
452   *pcb_node = xml_get_sibling(configuration_node->child, "pcb");
453   if (*pcb_node == NULL)
454      return FALSE;
455
456   *resource_node = xml_get_sibling(romset_node->child, "resources");
457   if (*resource_node == NULL)
458      return FALSE;
459
460   return TRUE;
461}
462
463
464/*-------------------------------------------------
465    get_resource_type
466-------------------------------------------------*/
467
468static multicart_resource_type get_resource_type(const char *s)
194READ8Z_MEMBER( cartridge_pcb_device::gromreadz )
469195{
470   multicart_resource_type result;
471   if (!strcmp(s, "rom"))
472      result = MULTICART_RESOURCE_TYPE_ROM;
473   else if (!strcmp(s, "ram"))
474      result = MULTICART_RESOURCE_TYPE_RAM;
475   else
476      result = MULTICART_RESOURCE_TYPE_INVALID;
477   return result;
478}
479
480
481/*-------------------------------------------------
482    load_rom_resource
483-------------------------------------------------*/
484
485static multicart_open_error load_rom_resource(multicart_load_state *state, xml_data_node *resource_node,
486   multicart_resource *resource)
487{
488   const char *file, *crcstr, *sha1;
489   const zip_file_header *header;
490   zip_error ziperr;
491   UINT32 crc;
492
493   /* locate the 'file' attribute */
494   file = xml_get_attribute_string(resource_node, "file", NULL);
495   if (file == NULL)
496      return MCERR_XML_ERROR;
497
498   if (!(crcstr = xml_get_attribute_string(resource_node, "crc", NULL)))
196   for (int i=0; i < 5; i++)
499197   {
500      /* locate the file in the ZIP file */
501      header = find_file(state->zip, file);
502   }
503   else   /* CRC tag is present, use it */
504   {
505      crc = strtoul(crcstr, NULL, 16);
506      header = find_file_crc(state->zip, file, crc);
507   }
508   if (header == NULL)
509      return MCERR_INVALID_FILE_REF;
510   resource->length = header->uncompressed_length;
511
512   /* allocate bytes for this resource */
513   resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length);
514   if (resource->ptr == NULL)
515      return MCERR_OUT_OF_MEMORY;
516
517   /* and decompress it */
518   ziperr = zip_file_decompress(state->zip, resource->ptr, resource->length);
519   if (ziperr != ZIPERR_NONE)
520      return MCERR_ZIP_ERROR;
521
522   /* check SHA1 now */
523   if ((sha1 = xml_get_attribute_string(resource_node, "sha1", NULL)))
524   {
525      hash_collection actual_hashes;
526      actual_hashes.compute((const UINT8 *)resource->ptr, resource->length, hash_collection::HASH_TYPES_CRC_SHA1);
527
528      hash_collection expected_hashes;
529      expected_hashes.add_from_string(hash_collection::HASH_SHA1, sha1, strlen(sha1));
530
531      if (actual_hashes != expected_hashes)
198      if (m_grom[i] != NULL)
532199      {
533         return MCERR_INVALID_FILE_REF;
200         m_grom[i]->readz(space, offset, value, mem_mask);
534201      }
535202   }
536
537   return MCERR_NONE;
538203}
539204
540
541/*-------------------------------------------------
542    load_ram_resource
543-------------------------------------------------*/
544
545static multicart_open_error load_ram_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node,
546   multicart_resource *resource)
205WRITE8_MEMBER( cartridge_pcb_device::gromwrite )
547206{
548   const char *length_string;
549   const char *ram_type;
550   const char *ram_filename;
551
552   /* locate the 'length' attribute */
553   length_string = xml_get_attribute_string(resource_node, "length", NULL);
554   if (length_string == NULL)
555      return MCERR_MISSING_RAM_LENGTH;
556
557   /* ...and parse it */
558   resource->length = ram_device::parse_string(length_string);
559   if (resource->length <= 0)
560      return MCERR_INVALID_RAM_SPEC;
561
562   /* allocate bytes for this resource */
563   resource->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length);
564   if (resource->ptr == NULL)
565      return MCERR_OUT_OF_MEMORY;
566
567   /* Is this a persistent RAM resource? Then try to load it. */
568   ram_type = xml_get_attribute_string(resource_node, "type", NULL);
569   if (ram_type != NULL)
207   for (int i=0; i < 5; i++)
570208   {
571      if (strcmp(ram_type, "persistent")==0)
209      if (m_grom[i] != NULL)
572210      {
573         astring tmp;
574
575         /* Get the file name. */
576         ram_filename = xml_get_attribute_string(resource_node, "file", NULL);
577         if (ram_filename==NULL)
578            return MCERR_XML_ERROR;
579
580         astring ram_pathname(state->multicart->gamedrv_name, PATH_SEPARATOR, ram_filename);
581
582         /* Save the file name so that we can write the contents on unloading.
583               If the RAM resource has no filename, we know that it was volatile only. */
584         resource->filename = pool_strdup_lib(state->multicart->data->pool, ram_pathname.cstr());
585
586         if (resource->filename == NULL)
587            return MCERR_OUT_OF_MEMORY;
588
589         image_battery_load_by_name(options, resource->filename, resource->ptr, resource->length, 0x00);
211         m_grom[i]->write(space, offset, data, mem_mask);
590212      }
591      /* else this type is volatile, in which case we just have
592            a memory expansion */
593213   }
594   return MCERR_NONE;
595214}
596215
597
598/*-------------------------------------------------
599    load_resource
600-------------------------------------------------*/
601
602static multicart_open_error load_resource(emu_options &options, multicart_load_state *state, xml_data_node *resource_node,
603   multicart_resource_type resource_type)
604{
605   const char *id;
606   multicart_open_error err;
607   multicart_resource *resource;
608   multicart_resource **next_resource;
609
610   /* get the 'id' attribute; error if not present */
611   id = xml_get_attribute_string(resource_node, "id", NULL);
612   if (id == NULL)
613      return MCERR_XML_ERROR;
614
615   /* allocate memory for the resource */
616   resource = (multicart_resource *)pool_malloc_lib(state->multicart->data->pool, sizeof(*resource));
617   if (resource == NULL)
618      return MCERR_OUT_OF_MEMORY;
619   memset(resource, 0, sizeof(*resource));
620   resource->type = resource_type;
621
622   /* copy id */
623   resource->id = pool_strdup_lib(state->multicart->data->pool, id);
624   if (resource->id == NULL)
625      return MCERR_OUT_OF_MEMORY;
626
627   switch(resource->type)
628   {
629      case MULTICART_RESOURCE_TYPE_ROM:
630         err = load_rom_resource(state, resource_node, resource);
631         if (err != MCERR_NONE)
632            return err;
633         break;
634
635      case MULTICART_RESOURCE_TYPE_RAM:
636         err = load_ram_resource(options, state, resource_node, resource);
637         if (err != MCERR_NONE)
638            return err;
639         break;
640
641      default:
642         return MCERR_UNKNOWN_RESOURCE_TYPE;
643   }
644
645   /* append the resource */
646   for (next_resource = &state->resources; *next_resource; next_resource = &(*next_resource)->next)
647      ;
648   *next_resource = resource;
649
650   return MCERR_NONE;
651}
652
653
654/*-------------------------------------------------
655    load_all_resources
656-------------------------------------------------*/
657
658static multicart_open_error load_all_resources(emu_options &options, multicart_load_state *state)
659{
660   multicart_open_error err;
661   xml_data_node *resource_node;
662   multicart_resource_type resource_type;
663
664   for (resource_node = state->resources_node->child; resource_node != NULL; resource_node = resource_node->next)
665   {
666      resource_type = get_resource_type(resource_node->name);
667      if (resource_type != MULTICART_RESOURCE_TYPE_INVALID)
668      {
669         err = load_resource(options, state, resource_node, resource_type);
670         if (err != MCERR_NONE)
671            return err;
672      }
673   }
674
675   state->multicart->resources = state->resources;
676   return MCERR_NONE;
677}
678
679/*-------------------------------------------------
680    save_ram_resources. This is important for persistent RAM. All
681    resources were allocated within the memory pool of this device and will
682    be freed on multicart_close.
683-------------------------------------------------*/
684
685static multicart_open_error save_ram_resources(emu_options &options, multicart_t *cart)
686{
687   const multicart_resource *resource;
688
689   for (resource = cart->resources; resource != NULL; resource = resource->next)
690   {
691      if ((resource->type == MULTICART_RESOURCE_TYPE_RAM) && (resource->filename != NULL))
692      {
693         image_battery_save_by_name(options, resource->filename, resource->ptr, resource->length);
694      }
695   }
696   return MCERR_NONE;
697}
698
699/*-------------------------------------------------
700    load_socket
701-------------------------------------------------*/
702
703static multicart_open_error load_socket(multicart_load_state *state, xml_data_node *socket_node)
704{
705   const char *id;
706   const char *uses;
707   const multicart_resource *resource;
708   multicart_socket *socket;
709   multicart_socket **next_socket;
710
711   /* get the 'id' and 'uses' attributes; error if not present */
712   id = xml_get_attribute_string(socket_node, "id", NULL);
713   uses = xml_get_attribute_string(socket_node, "uses", NULL);
714   if ((id == NULL) || (uses == NULL))
715      return MCERR_XML_ERROR;
716
717   /* find the resource */
718   for (resource = state->multicart->resources; resource != NULL; resource = resource->next)
719   {
720      if (!strcmp(uses, resource->id))
721         break;
722   }
723   if (resource == NULL)
724      return MCERR_INVALID_RESOURCE_REF;
725
726   /* create the socket */
727   socket = (multicart_socket *)pool_malloc_lib(state->multicart->data->pool, sizeof(*socket));
728   if (socket == NULL)
729      return MCERR_OUT_OF_MEMORY;
730   memset(socket, 0, sizeof(*socket));
731   socket->resource = resource;
732   socket->ptr = resource->ptr;
733
734   /* copy id */
735   socket->id = pool_strdup_lib(state->multicart->data->pool, id);
736   if (socket->id == NULL)
737      return MCERR_OUT_OF_MEMORY;
738
739   /* which pointer should I use? */
740   if (resource->ptr != NULL)
741   {
742      /* use the resource's ptr */
743      socket->ptr = resource->ptr;
744   }
745   else
746   {
747      /* allocate bytes for this socket */
748      socket->ptr = pool_malloc_lib(state->multicart->data->pool, resource->length);
749      if (socket->ptr == NULL)
750         return MCERR_OUT_OF_MEMORY;
751
752      /* ...and clear it */
753      memset(socket->ptr, 0xCD, resource->length);
754   }
755
756   /* append the resource */
757   for (next_socket = &state->sockets; *next_socket; next_socket = &(*next_socket)->next)
758      ;
759   *next_socket = socket;
760
761   return MCERR_NONE;
762}
763
764
765/*-------------------------------------------------
766    load_all_sockets
767-------------------------------------------------*/
768
769static multicart_open_error load_all_sockets(multicart_load_state *state)
770{
771   multicart_open_error err;
772   xml_data_node *socket_node;
773
774   for (socket_node = xml_get_sibling(state->pcb_node->child, "socket"); socket_node != NULL;
775      socket_node = xml_get_sibling(socket_node->next, "socket"))
776   {
777      err = load_socket(state, socket_node);
778      if (err != MCERR_NONE)
779         return err;
780   }
781
782   state->multicart->sockets = state->sockets;
783   return MCERR_NONE;
784}
785
786
787/*-------------------------------------------------
788    multicart_open - opens a multicart
789-------------------------------------------------*/
790
791multicart_open_error multicart_open(emu_options &options, const char *filename, const char *gamedrv, multicart_load_flags load_flags, multicart_t **cart)
792{
793   multicart_open_error err;
794   zip_error ziperr;
795   object_pool *pool;
796   multicart_load_state state = {0, };
797   const zip_file_header *header;
798   const char *pcb_type;
799   char *layout_text = NULL;
800
801   /* allocate an object pool */
802   pool = pool_alloc_lib(NULL);
803   if (pool == NULL)
804   {
805      err = MCERR_OUT_OF_MEMORY;
806      goto done;
807   }
808
809   /* allocate the multicart */
810   state.multicart = (multicart_t*)pool_malloc_lib(pool, sizeof(*state.multicart));
811   if (state.multicart == NULL)
812   {
813      err = MCERR_OUT_OF_MEMORY;
814      goto done;
815   }
816   memset(state.multicart, 0, sizeof(*state.multicart));
817
818   /* allocate the multicart's private data */
819   state.multicart->data = (multicart_private*)pool_malloc_lib(pool, sizeof(*state.multicart->data));
820   if (state.multicart->data == NULL)
821   {
822      err = MCERR_OUT_OF_MEMORY;
823      goto done;
824   }
825   memset(state.multicart->data, 0, sizeof(*state.multicart->data));
826   state.multicart->data->pool = pool;
827   pool = NULL;
828
829   /* open the ZIP file */
830   ziperr = zip_file_open(filename, &state.zip);
831   if (ziperr != ZIPERR_NONE)
832   {
833      err = MCERR_NOT_MULTICART;
834      goto done;
835   }
836
837   /* find the layout.xml file */
838   header = find_file(state.zip, "layout.xml");
839   if (header == NULL)
840   {
841      err = MCERR_MISSING_LAYOUT;
842      goto done;
843   }
844
845   /* reserve space for the layout text */
846   layout_text = (char*)malloc(header->uncompressed_length + 1);
847   if (layout_text == NULL)
848   {
849      err = MCERR_OUT_OF_MEMORY;
850      goto done;
851   }
852
853   /* uncompress the layout text */
854   ziperr = zip_file_decompress(state.zip, layout_text, header->uncompressed_length);
855   if (ziperr != ZIPERR_NONE)
856   {
857      err = MCERR_ZIP_ERROR;
858      goto done;
859   }
860   layout_text[header->uncompressed_length] = '\0';
861
862   /* parse the layout text */
863   state.layout_xml = xml_string_read(layout_text, NULL);
864   if (state.layout_xml == NULL)
865   {
866      err = MCERR_XML_ERROR;
867      goto done;
868   }
869
870   /* locate the PCB node */
871   if (!find_pcb_and_resource_nodes(state.layout_xml, &state.pcb_node, &state.resources_node))
872   {
873      err = MCERR_NO_PCB_OR_RESOURCES;
874      goto done;
875   }
876
877   /* get the PCB resource_type */
878   pcb_type = xml_get_attribute_string(state.pcb_node, "type", "");
879   state.multicart->pcb_type = pool_strdup_lib(state.multicart->data->pool, pcb_type);
880   if (state.multicart->pcb_type == NULL)
881   {
882      err = MCERR_OUT_OF_MEMORY;
883      goto done;
884   }
885
886   state.multicart->gamedrv_name = pool_strdup_lib(state.multicart->data->pool, gamedrv);
887   if (state.multicart->gamedrv_name == NULL)
888   {
889      err = MCERR_OUT_OF_MEMORY;
890      goto done;
891   }
892
893   /* do we have to load resources? */
894   if (load_flags & MULTICART_FLAGS_LOAD_RESOURCES)
895   {
896      err = load_all_resources(options, &state);
897      if (err != MCERR_NONE)
898         goto done;
899
900      err = load_all_sockets(&state);
901      if (err != MCERR_NONE)
902         goto done;
903   }
904
905   err = MCERR_NONE;
906
907done:
908   if (pool != NULL)
909      pool_free_lib(pool);
910   if (state.zip != NULL)
911      zip_file_close(state.zip);
912   if (layout_text != NULL)
913      free(layout_text);
914   if (state.layout_xml != NULL)
915      xml_file_free(state.layout_xml);
916
917   if ((err != MCERR_NONE) && (state.multicart != NULL))
918   {
919      multicart_close(options, state.multicart);
920      state.multicart = NULL;
921   }
922   *cart = state.multicart;
923   return err;
924}
925
926
927/*-------------------------------------------------
928    multicart_close - closes a multicart
929-------------------------------------------------*/
930
931void multicart_close(emu_options &options, multicart_t *cart)
932{
933   save_ram_resources(options, cart);
934   pool_free_lib(cart->data->pool);
935}
936
937/*-------------------------------------------------
938    DEVICE_START( multicartslot )
939-------------------------------------------------*/
940
941static DEVICE_START( multicartslot )
942{
943   const multicartslot_config *config = get_config(device);
944
945   /* if this cartridge has a custom DEVICE_START, use it */
946   if (config->device_start != NULL)
947   {
948      (*config->device_start)(device);
949   }
950}
951
952
953/*-------------------------------------------------
954    DEVICE_IMAGE_LOAD( cartslot )
955-------------------------------------------------*/
956
957static DEVICE_IMAGE_LOAD( multicartslot )
958{
959   device_t *device = &image.device();
960   const multicartslot_config *config = get_config(device);
961
962   /* if this cartridge has a custom DEVICE_IMAGE_LOAD, use it */
963   if (config->device_load != NULL)
964      return (*config->device_load)(image);
965
966   /* otherwise try the normal route */
967   return IMAGE_INIT_PASS;
968}
969
970
971/*-------------------------------------------------
972    DEVICE_IMAGE_UNLOAD( multicartslot )
973-------------------------------------------------*/
974
975static DEVICE_IMAGE_UNLOAD( multicartslot )
976{
977   device_t *device = &image.device();
978   const multicartslot_config *config = get_config(device);
979
980   /* if this cartridge has a custom DEVICE_IMAGE_UNLOAD, use it */
981   if (config->device_unload != NULL)
982   {
983      (*config->device_unload)(image);
984      return;
985   }
986}
987/*-------------------------------------------------
988    DEVICE_GET_INFO( multicartslot )
989-------------------------------------------------*/
990
991DEVICE_GET_INFO( multicartslot )
992{
993   switch(state)
994   {
995      /* --- the following bits of info are returned as 64-bit signed integers --- */
996      case DEVINFO_INT_TOKEN_BYTES:            info->i = sizeof(multicartslot_t); break;
997      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = sizeof(multicartslot_config); break;
998      case DEVINFO_INT_IMAGE_TYPE:            info->i = IO_CARTSLOT; break;
999      case DEVINFO_INT_IMAGE_READABLE:         info->i = 1; break;
1000      case DEVINFO_INT_IMAGE_WRITEABLE:         info->i = 0; break;
1001      case DEVINFO_INT_IMAGE_CREATABLE:         info->i = 0; break;
1002      case DEVINFO_INT_IMAGE_RESET_ON_LOAD:      info->i = 1; break;
1003      case DEVINFO_INT_IMAGE_MUST_BE_LOADED:      info->i = 0; break;
1004
1005      /* --- the following bits of info are returned as pointers to functions --- */
1006      case DEVINFO_FCT_START:                  info->start = DEVICE_START_NAME(multicartslot);               break;
1007      case DEVINFO_FCT_IMAGE_LOAD:            info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(multicartslot);      break;
1008      case DEVINFO_FCT_IMAGE_UNLOAD:            info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(multicartslot);      break;
1009      case DEVINFO_FCT_IMAGE_GET_DEVICES:         info->f = (genf *) DEVICE_IMAGE_GET_DEVICES_NAME(multicartslot);   break;
1010
1011      /* --- the following bits of info are returned as NULL-terminated strings --- */
1012      case DEVINFO_STR_NAME:                  strcpy(info->s, "MultiCartslot"); break;
1013      case DEVINFO_STR_FAMILY:               strcpy(info->s, "MultiCartslot"); break;
1014      case DEVINFO_STR_SOURCE_FILE:            strcpy(info->s, __FILE__); break;
1015      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:
1016         if ( device && downcast<const legacy_image_device_base *>(device)->inline_config() && get_config(device)->extensions )
1017         {
1018            strcpy(info->s, get_config(device)->extensions);
1019         }
1020         else
1021         {
1022            strcpy(info->s, "bin");
1023         }
1024         break;
1025   }
1026}
1027
1028DEFINE_LEGACY_IMAGE_DEVICE(MULTICARTSLOT, multicartslot);
1029
1030//**************************************************************************
1031//  DEVICE CARTSLOT INTERFACE
1032//**************************************************************************
1033
1034//-------------------------------------------------
1035//  device_cart_slot_interface - constructor
1036//-------------------------------------------------
1037
1038device_cart_slot_interface::device_cart_slot_interface(const machine_config &mconfig, device_t &device)
1039   : device_interface(device)
1040{
1041}
1042
1043
1044//-------------------------------------------------
1045//  ~device_cart_slot_interface - destructor
1046//-------------------------------------------------
1047
1048device_cart_slot_interface::~device_cart_slot_interface()
1049{
1050}
1051
1052//**************************************************************************
1053//  LIVE LEGACY cart_slot DEVICE
1054//**************************************************************************
1055
1056//-------------------------------------------------
1057//  legacy_cart_slot_device_base - constructor
1058//-------------------------------------------------
1059
1060legacy_cart_slot_device_base::legacy_cart_slot_device_base(const machine_config &mconfig, device_type type, const char *tag, device_t *owner, UINT32 clock, device_get_config_func get_config)
1061   : legacy_device_base(mconfig, type, tag, owner, clock, get_config),
1062     device_cart_slot_interface(mconfig, *this)
1063{
1064}
1065
1066/* end of multicart */
1067typedef UINT8   (*read8z_device_func)  (ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT8 *value);
1068
1069/* We set the number of slots to 8, although we may have up to 16. From a
1070   logical point of view we could have 256, but the operating system only checks
1071   the first 16 banks. */
1072#define NUMBER_OF_CARTRIDGE_SLOTS 8
1073
1074#define VERBOSE 2
1075#define LOG logerror
1076
1077typedef int assmfct(device_t *);
1078
1079DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_NONE, ti99_cartridge_pcb_none);
1080DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_STD, ti99_cartridge_pcb_std);
1081DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGED, ti99_cartridge_pcb_paged);
1082DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_MINIMEM, ti99_cartridge_pcb_minimem);
1083DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_SUPER, ti99_cartridge_pcb_super);
1084DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_MBX, ti99_cartridge_pcb_mbx);
1085DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGED379I, ti99_cartridge_pcb_paged379i);
1086DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGEDCRU, ti99_cartridge_pcb_pagedcru);
1087DECLARE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_GRAMKRACKER, ti99_cartridge_pcb_gramkracker);
1088
1089DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_NONE, ti99_cartridge_pcb_none);
1090DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_STD, ti99_cartridge_pcb_std);
1091DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGED, ti99_cartridge_pcb_paged);
1092DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_MINIMEM, ti99_cartridge_pcb_minimem);
1093DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_SUPER, ti99_cartridge_pcb_super);
1094DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_MBX, ti99_cartridge_pcb_mbx);
1095DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGED379I, ti99_cartridge_pcb_paged379i);
1096DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_PAGEDCRU, ti99_cartridge_pcb_pagedcru);
1097DEFINE_LEGACY_CART_SLOT_DEVICE(TI99_CARTRIDGE_PCB_GRAMKRACKER, ti99_cartridge_pcb_gramkracker);
1098
1099/* Generic TI99 cartridge structure. */
1100struct _cartridge_t
1101{
1102   // PCB device associated to this cartridge. If NULL, the slot is empty.
1103   device_t *pcb;
1104
1105   // ROM page.
1106   int rom_page;
1107
1108   // RAM page.
1109   int ram_page;
1110
1111   // GROM buffer size.
1112   int grom_size;
1113
1114   // ROM buffer size. All banks have equal sizes.
1115   int rom_size;
1116
1117   // RAM buffer size. All banks have equal sizes.
1118   int ram_size;
1119
1120   // GROM buffer.
1121   UINT8 *grom_ptr;
1122
1123   // ROM buffer.
1124   UINT8 *rom_ptr;
1125
1126   // ROM buffer for the second bank of paged cartridges.
1127   UINT8 *rom2_ptr;
1128
1129   // RAM buffer. The persistence service is done by the cartridge system.
1130   // The RAM space is a consecutive space; all banks are in one buffer.
1131   UINT8 *ram_ptr;
1132
1133   /* GK support. */
1134   UINT16   grom_address;
1135   int      waddr_LSB;
1136};
1137typedef struct _cartridge_t cartridge_t;
1138
1139enum
1140{
1141   TI99CARTINFO_FCT_6000_R = DEVINFO_FCT_DEVICE_SPECIFIC,
1142   TI99CARTINFO_FCT_6000_W,
1143   TI99CARTINFO_FCT_CRU_R,
1144   TI99CARTINFO_FCT_CRU_W,
1145   TI99CART_FCT_ASSM,
1146   TI99CART_FCT_DISASSM
1147};
1148
1149typedef struct _ti99_multicart_state
1150{
1151   // Reserves space for all cartridges. This is also used in the legacy
1152   // cartridge system, but only for slot 0.
1153   cartridge_t cartridge[NUMBER_OF_CARTRIDGE_SLOTS];
1154
1155   // Determines which slot is currently active. This value is changed when
1156   // there are accesses to other GROM base addresses.
1157   int    active_slot;
1158
1159   // Used in order to enforce a special slot. This value is retrieved
1160   // from the dipswitch setting. A value of -1 means automatic, that is,
1161   // the grom base switch is used. Values 0 .. max refer to the
1162   // respective slot.
1163   int    fixed_slot;
1164
1165   // Holds the highest index of a cartridge being plugged in plus one.
1166   // If we only have one cartridge inserted, we don't want to get a
1167   // selection option, so we just mirror the memory contents.
1168   int    next_free_slot;
1169
1170   /* GK support. */
1171   int      gk_slot;
1172   int      gk_guest_slot;
1173
1174   /* Used to cache the switch settings. */
1175   UINT8 gk_switch[8];
1176
1177   /* Ready line */
1178   devcb_resolved_write_line ready;
1179
1180} ti99_multicart_state;
1181
1182#define AUTO -1
1183
1184/* Access to the pcb. Contained in the token of the pcb instance. */
1185struct _ti99_pcb_t
1186{
1187   /* Read function for this cartridge. */
1188   read8z_device_func read;
1189
1190   /* Write function for this cartridge. */
1191   write8_device_func write;
1192
1193   /* Read function for this cartridge, CRU. */
1194   read8z_device_func cruread;
1195
1196   /* Write function for this cartridge, CRU. */
1197   write8_device_func cruwrite;
1198
1199   /* Link up to the cartridge structure which contains this pcb. */
1200   cartridge_t *cartridge;
1201
1202   /* Function to assemble this cartridge. */
1203   assmfct   *assemble;
1204
1205   /* Function to disassemble this cartridge. */
1206   assmfct   *disassemble;
1207
1208   /* Points to the (max 5) GROM devices. */
1209   device_t *grom[5];
1210};
1211typedef struct _ti99_pcb_t ti99_pcb_t;
1212
1213INLINE ti99_multicart_state *get_safe_token(device_t *device)
1214{
1215   assert(device != NULL);
1216   assert(device->type() == TI99_MULTICART);
1217
1218   return (ti99_multicart_state *)downcast<legacy_device_base *>(device)->token();
1219}
1220
1221INLINE const ti99_multicart_config *get_config(device_t *device)
1222{
1223   assert(device != NULL);
1224   assert(device->type() == TI99_MULTICART);
1225
1226   return (const ti99_multicart_config *) downcast<const legacy_device_base *>(device)->inline_config();
1227}
1228
1229INLINE ti99_pcb_t *get_safe_pcb_token(device_t *device)
1230{
1231   assert(device != NULL);
1232   assert(device->type() == TI99_CARTRIDGE_PCB_NONE ||
1233      device->type() == TI99_CARTRIDGE_PCB_STD ||
1234      device->type() == TI99_CARTRIDGE_PCB_PAGED ||
1235      device->type() == TI99_CARTRIDGE_PCB_MINIMEM ||
1236      device->type() == TI99_CARTRIDGE_PCB_SUPER ||
1237      device->type() == TI99_CARTRIDGE_PCB_MBX ||
1238      device->type() == TI99_CARTRIDGE_PCB_PAGED379I ||
1239      device->type() == TI99_CARTRIDGE_PCB_PAGEDCRU ||
1240      device->type() == TI99_CARTRIDGE_PCB_GRAMKRACKER);
1241
1242   return (ti99_pcb_t *)downcast<legacy_device_base *>(device)->token();
1243}
1244
1245INLINE multicartslot_t *get_safe_cartslot_token(device_t *device)
1246{
1247   assert(device != NULL);
1248   assert(device->type() == MULTICARTSLOT);
1249
1250   return (multicartslot_t *)downcast<legacy_device_base *>(device)->token();
1251}
1252
1253static WRITE_LINE_DEVICE_HANDLER( cart_grom_ready )
1254{
1255   device_t *cartdev = device->owner()->owner();
1256   ti99_multicart_state *cartslots = get_safe_token(cartdev);
1257   cartslots->ready(state);
1258}
1259
1260/*
1261    The console writes the values in this array to cache them.
1262*/
1263void set_gk_switches(device_t *cartsys, int number, int value)
1264{
1265   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1266   cartslots->gk_switch[number] = value;
1267}
1268
1269static int get_gk_switch(device_t *cartsys, int number)
1270{
1271   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1272   return cartslots->gk_switch[number];
1273}
1274
1275/*
1276    Activates a slot in the multi-cartridge extender.
1277*/
1278static void cartridge_slot_set(device_t *cartsys, UINT8 slotnumber)
1279{
1280   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1281   if (VERBOSE>2)
1282      if (cartslots->active_slot != slotnumber) LOG("ti99: gromport: Setting cartslot to %d\n", slotnumber);
1283   if (cartslots->fixed_slot==AUTO)
1284      cartslots->active_slot = slotnumber;
1285   else
1286      cartslots->active_slot = cartslots->fixed_slot;
1287}
1288
1289static int slot_is_empty(device_t *cartsys, int slotnumber)
1290{
1291   cartridge_t *cart;
1292   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1293
1294   cart = &cartslots->cartridge[slotnumber];
1295
1296   if ((cart->rom_size==0) && (cart->ram_size==0) && (cart->grom_size==0))
1297      return TRUE;
1298   return FALSE;
1299}
1300
1301static void clear_slot(device_t *cartsys, int slotnumber)
1302{
1303   cartridge_t *cart;
1304   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1305
1306   cart = &cartslots->cartridge[slotnumber];
1307
1308   cart->rom_size = cart->ram_size = cart->grom_size = 0;
1309   cart->rom_ptr = NULL;
1310   cart->rom2_ptr = NULL;
1311   cart->ram_ptr = NULL;
1312}
1313
1314/*
1315    Find the index of the cartridge name. We assume the format
1316    <name><number>, i.e. the number is the longest string from the right
1317    which can be interpreted as a number.
1318*/
1319static int get_index_from_tagname(device_t *image)
1320{
1321   const char *tag = image->tag();
1322   int maxlen = strlen(tag);
1323   int i;
1324   for (i=maxlen-1; i >=0; i--)
1325      if (tag[i] < 48 || tag[i] > 57) break;
1326
1327   return atoi(tag+i+1);
1328}
1329
1330/*
1331    Common routine to assemble cartridges from resources.
1332*/
1333static cartridge_t *assemble_common(device_t *cartslot)
1334{
1335   /* Pointer to the cartridge structure. */
1336   cartridge_t *cartridge;
1337   device_t *cartsys = cartslot->owner();
1338   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1339   device_t *pcb = cartslot->subdevice("pcb");
1340   ti99_pcb_t *pcb_def;
1341
1342   void *socketcont;
1343   int reslength;
1344//  int i;
1345
1346   int slotnumber = get_index_from_tagname(cartslot)-1;
1347   assert(slotnumber>=0 && slotnumber<NUMBER_OF_CARTRIDGE_SLOTS);
1348
1349   if (VERBOSE>1) LOG("ti99: gromport: mounting cartridge in slot %d\n", slotnumber);
1350
1351   /* There is a cartridge in this slot, check the maximum slot number. */
1352   if (cartslots->next_free_slot <= slotnumber)
1353   {
1354      cartslots->next_free_slot = slotnumber+1;
1355   }
1356   cartridge = &cartslots->cartridge[slotnumber];
1357
1358   // Get the socket which is retrieved by the multicart instance in
1359   // the state of the cartslot instance.
1360   socketcont = cartslot_get_socket(cartslot, "grom_socket");
1361   reslength = cartslot_get_resource_length(cartslot, "grom_socket");
1362   if (socketcont != NULL)
1363   {
1364      cartridge->grom_ptr = (UINT8 *)socketcont;
1365//      printf("grom_socket = %lx\n", cartridge->grom_ptr);
1366      cartridge->grom_size = reslength;
1367   }
1368
1369   socketcont = cartslot_get_socket(cartslot, "rom_socket");
1370   reslength = cartslot_get_resource_length(cartslot, "rom_socket");
1371   if (socketcont != NULL)
1372   {
1373      cartridge->rom_ptr = (UINT8 *)socketcont;
1374      cartridge->rom_size = reslength;
1375   }
1376
1377   socketcont = cartslot_get_socket(cartslot, "rom2_socket");
1378//  reslength = cartslot_get_resource_length(cartslot, "rom2_socket");  /* cannot differ from rom_socket */
1379   if (socketcont != NULL)
1380   {
1381      cartridge->rom2_ptr = (UINT8 *)socketcont;
1382   }
1383
1384   socketcont = cartslot_get_socket(cartslot, "ram_socket");
1385   reslength = cartslot_get_resource_length(cartslot, "ram_socket");
1386   if (socketcont != NULL)
1387   {
1388      cartridge->ram_ptr = (UINT8 *)socketcont;
1389      cartridge->ram_size = reslength;
1390   }
1391
1392   // Register the GROMs
1393   pcb_def = get_safe_pcb_token(pcb);
1394   for (int i=0; i < 5; i++) pcb_def->grom[i]= NULL;
1395   if (cartridge->grom_size > 0)      pcb_def->grom[0] = pcb->subdevice("grom_3");
1396   if (cartridge->grom_size > 0x2000) pcb_def->grom[1] = pcb->subdevice("grom_4");
1397   if (cartridge->grom_size > 0x4000) pcb_def->grom[2] = pcb->subdevice("grom_5");
1398   if (cartridge->grom_size > 0x6000) pcb_def->grom[3] = pcb->subdevice("grom_6");
1399   if (cartridge->grom_size > 0x8000) pcb_def->grom[4] = pcb->subdevice("grom_7");
1400
1401   return cartridge;
1402}
1403
1404static void set_pointers(device_t *pcb, int index)
1405{
1406   device_t *cartsys = pcb->owner()->owner();
1407   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1408   ti99_pcb_t *pcb_def = get_safe_pcb_token(pcb);
1409
1410   pcb_def->read = (read8z_device_func)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CARTINFO_FCT_6000_R);
1411   pcb_def->write = (write8_device_func)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CARTINFO_FCT_6000_W);
1412   pcb_def->cruread = (read8z_device_func)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CARTINFO_FCT_CRU_R);
1413   pcb_def->cruwrite = (write8_device_func)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CARTINFO_FCT_CRU_W);
1414
1415    pcb_def->assemble = (assmfct *)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CART_FCT_ASSM);
1416    pcb_def->disassemble = (assmfct *)downcast<const legacy_cart_slot_device_base *>(pcb)->get_legacy_fct(TI99CART_FCT_DISASSM);
1417
1418   pcb_def->cartridge = &cartslots->cartridge[index];
1419   pcb_def->cartridge->pcb = pcb;
1420}
1421
1422216/*****************************************************************************
1423  Cartridge type: None
1424    This PCB device is just a pseudo device
1425******************************************************************************/
1426static DEVICE_START(ti99_pcb_none)
1427{
1428   /* device is ti99_cartslot:cartridge:pcb */
1429   set_pointers(device, get_index_from_tagname(device->owner())-1);
1430}
1431
1432/*****************************************************************************
1433217  Cartridge type: Standard
1434218    Most cartridges are built in this type. Every cartridge may contain
1435219    GROM between 0 and 40 KiB, and ROM with 8 KiB length, no banking.
1436220******************************************************************************/
1437221
1438static DEVICE_START(ti99_pcb_std)
222/* Read function for the standard cartridge. */
223READ8Z_MEMBER(cartridge_pcb_standard_device::readz)
1439224{
1440   /* device is ti99_cartslot:cartridge:pcb */
1441   set_pointers(device, get_index_from_tagname(device->owner())-1);
1442}
1443
1444/*
1445    Read handler for the CPU address space of the cartridge
1446    Images for this area are found in the rom_sockets.
1447*/
1448static READ8Z_DEVICE_HANDLER( read_cart_std )
1449{
1450   /* device is pcb, owner is cartslot (cartridge is only a part of the state of cartslot) */
1451   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1452   cartridge_t *cartridge = pcb->cartridge;
1453   if (cartridge->rom_ptr!=NULL)
225   if ((offset & GROM_MASK)==GROM_AREA)
226      gromreadz(space, offset, value, mem_mask);
227   else
1454228   {
1455      // For TI-99/8 we should plan for 16K cartridges. However, none was produced. Well, just ignore this thought,
1456      *value = (cartridge->rom_ptr)[offset & 0x1fff];
1457//      printf("read cartridge rom space (length %04x) %04x = %02x\n", cartridge->rom_size, offset, *value);
229      if (m_rom_ptr!=NULL)
230      {
231         // For TI-99/8 we should plan for 16K cartridges. However, none was ever produced.
232         // Well, forget about that.
233         *value = m_rom_ptr[offset & 0x1fff];
234         //      LOG("read cartridge rom space %04x = %02x\n", offset, *value);
235      }
1458236   }
1459237}
1460238
1461static WRITE8_DEVICE_HANDLER( write_cart_std )
239/* Write function for the standard cartridge. */
240WRITE8_MEMBER(cartridge_pcb_standard_device::write)
1462241{
1463   if (VERBOSE>1) LOG("ti99: gromport: Write access to cartridge ROM at address %04x ignored\n", offset);
1464}
1465
1466/*
1467    The standard cartridge assemble routine. We just call the common
1468    function here.
1469*/
1470static int assemble_std(device_t *image)
1471{
1472   assemble_common(image);
1473   return IMAGE_INIT_PASS;
1474}
1475
1476/*
1477    Removes pointers and restores the state before plugging in the
1478    cartridge.
1479    The pointer to the location after the last cartridge is adjusted.
1480    As it seems, we can use the same function for the disassembling of all
1481    cartridge types.
1482*/
1483static int disassemble_std(device_t *image)
1484{
1485   int slotnumber;
1486   int i;
1487//  cartridge_t *cart;
1488   device_t *cartsys = image->owner();
1489   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1490
1491   slotnumber = get_index_from_tagname(image)-1;
1492
1493   /* Search the highest remaining cartridge. */
1494   cartslots->next_free_slot = 0;
1495   for (i=NUMBER_OF_CARTRIDGE_SLOTS-1; i >= 0; i--)
242   // LOG("write standard\n");
243   if ((offset & GROM_MASK)==GROM_AREA)
244      gromwrite(space, offset, data, mem_mask);
245   else
1496246   {
1497      if (i != slotnumber)
1498      {
1499         if (!slot_is_empty(cartsys, i))
1500         {
1501            cartslots->next_free_slot = i+1;
1502            break;
1503         }
1504      }
247      if (VERBOSE>5) LOG("cartridge_pcb_device: Cannot write to ROM space at %04x\n", offset);
1505248   }
1506
1507   clear_slot(cartsys, slotnumber);
1508
1509   return IMAGE_INIT_PASS;
1510249}
1511250
1512251/*****************************************************************************
r14513r14514
1518257    6002 = bank 1).
1519258******************************************************************************/
1520259
1521static DEVICE_START(ti99_pcb_paged)
260/* Read function for the paged cartridge. */
261READ8Z_MEMBER(cartridge_pcb_paged_device::readz)
1522262{
1523   /* device is ti99_cartslot:cartridge:pcb */
1524   set_pointers(device, get_index_from_tagname(device->owner())-1);
1525}
1526
1527/*
1528    Read handler for the CPU address space of the cartridge
1529    Images for this area are found in the rom_sockets.
1530*/
1531static READ8Z_DEVICE_HANDLER( read_cart_paged )
1532{
1533   /* device is pcb, owner is cartslot */
1534   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1535   cartridge_t *cartridge = pcb->cartridge;
1536   UINT8 *rombank;
1537
1538   if (cartridge->rom_page==0)
1539      rombank = (UINT8*)cartridge->rom_ptr;
263   if ((offset & GROM_MASK)==GROM_AREA)
264      gromreadz(space, offset, value, mem_mask);
1540265   else
1541      rombank = (UINT8*)cartridge->rom2_ptr;
1542
1543   if (rombank==NULL)
1544266   {
1545      /* TODO: Check for consistency with the GROM memory handling. */
1546      *value = 0;
267      if (m_rom_page==0)
268      {
269         *value = m_rom_ptr[offset & 0x1fff];
270      }
271      else
272      {
273         *value = m_rom2_ptr[offset & 0x1fff];
274      }
1547275   }
1548   else
1549   {
1550      *value = rombank[offset & 0x1fff];
1551   }
1552276}
1553277
1554/*
1555    Handle paging. Extended Basic switches between ROM banks by
1556    using the value of the LSB of the address where any value
1557    is written to.
1558*/
1559static WRITE8_DEVICE_HANDLER( write_cart_paged )
278/* Write function for the paged cartridge. */
279WRITE8_MEMBER(cartridge_pcb_paged_device::write)
1560280{
1561   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1562   cartridge_t *cartridge = pcb->cartridge;
1563   cartridge->rom_page = (offset >> 1) & 1;
1564}
281   // LOG("write standard\n");
282   if ((offset & GROM_MASK)==GROM_AREA)
283      gromwrite(space, offset, data, mem_mask);
1565284
1566/*
1567    We require paged modules to have at least those two rom banks.
1568*/
1569static int assemble_paged(device_t *image)
1570{
1571   cartridge_t *cart;
1572
1573   cart = assemble_common(image);
1574   if (cart->rom_ptr==NULL)
1575   {
1576      if (VERBOSE>1) LOG("ti99: gromport: Missing ROM for paged cartridge");
1577      return IMAGE_INIT_FAIL;
1578   }
1579   if (cart->rom2_ptr==NULL)
1580   {
1581      if (VERBOSE>1) LOG("ti99: gromport: Missing second ROM for paged cartridge");
1582      return IMAGE_INIT_FAIL;
1583   }
1584
1585   return IMAGE_INIT_PASS;
285   else
286      m_rom_page = (offset >> 1) & 1;
1586287}
1587288
289
1588290/*****************************************************************************
1589291  Cartridge type: Mini Memory
1590292    GROM: 6 KiB (occupies G>6000 to G>7800)
r14513r14514
1592294    persistent RAM: 4 KiB (0x7000-0x7fff)
1593295******************************************************************************/
1594296
1595static DEVICE_START(ti99_pcb_minimem)
297/* Read function for the minimem cartridge. */
298READ8Z_MEMBER(cartridge_pcb_minimem_device::readz)
1596299{
1597   set_pointers(device, get_index_from_tagname(device->owner())-1);
1598}
300   if ((offset & GROM_MASK)==GROM_AREA)
301      gromreadz(space, offset, value, mem_mask);
1599302
1600static READ8Z_DEVICE_HANDLER( read_cart_minimem )
1601{
1602   /* device is pcb, owner is cartslot */
1603   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1604   cartridge_t *cartridge = pcb->cartridge;
1605
1606   if ((offset & 0x1000)==0x0000)
1607   {
1608      *value = (cartridge->rom_ptr==NULL)? 0 : (cartridge->rom_ptr)[offset & 0x0fff];
1609   }
1610303   else
1611304   {
1612      *value = (cartridge->ram_ptr==NULL)? 0 : (cartridge->ram_ptr)[offset & 0x0fff];
305      if ((offset & 0x1000)==0x0000)
306      {
307         *value = m_rom_ptr[offset & 0x0fff];
308      }
309      else
310      {
311         *value = m_ram_ptr[offset & 0x0fff];
312      }
1613313   }
1614314}
1615315
1616/*
1617    Mini Memory cartridge write operation. RAM is located at 0x7000.
1618*/
1619static WRITE8_DEVICE_HANDLER( write_cart_minimem )
316/* Write function for the minimem cartridge. */
317WRITE8_MEMBER(cartridge_pcb_minimem_device::write)
1620318{
1621   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1622   cartridge_t *cartridge = pcb->cartridge;
319   // LOG("write standard\n");
320   if ((offset & GROM_MASK)==GROM_AREA)
321      gromwrite(space, offset, data, mem_mask);
1623322
1624   if ((offset & 0x1000)==0x0000)
1625   {
1626      if (VERBOSE>1) LOG("ti99: gromport: Write access to cartridge ROM at address %04x ignored", offset);
1627   }
1628323   else
1629324   {
1630      if (cartridge->ram_ptr==NULL)
325      if ((offset & 0x1000)==0x0000)
1631326      {
1632         if (VERBOSE>1) LOG("ti99: gromport: No cartridge RAM at address %04x", offset);
1633         /* TODO: Check for consistency with the GROM memory handling. */
327         if (VERBOSE>1) LOG("ti99: gromport: Write access to cartridge ROM at address %04x ignored", offset);
1634328      }
1635329      else
1636330      {
1637         (cartridge->ram_ptr)[offset & 0x0fff] = data;
331         m_ram_ptr[offset & 0x0fff] = data;
1638332      }
1639333   }
1640334}
1641335
1642static int assemble_minimem(device_t *image)
1643{
1644   cartridge_t *cart;
1645
1646   cart = assemble_common(image);
1647   if (cart->grom_size==0)
1648   {
1649      if (VERBOSE>1) LOG("ti99: gromport: Missing GROM for Mini Memory");
1650      // should not fail here because there may be variations of
1651      // cartridges which do not use all parts
1652//      return IMAGE_INIT_FAIL;
1653   }
1654   if (cart->rom_size==0)
1655   {
1656      if (VERBOSE>1) LOG("ti99: gromport: Missing ROM for Mini Memory");
1657//      return IMAGE_INIT_FAIL;
1658   }
1659   if (cart->ram_size==0)
1660   {
1661      if (VERBOSE>1) LOG("ti99: gromport: Missing RAM for Mini Memory");
1662//      return IMAGE_INIT_FAIL;
1663   }
1664
1665   return IMAGE_INIT_PASS;
1666}
1667
1668336/*****************************************************************************
1669337  Cartridge type: SuperSpace II
1670338    SuperSpace cartridge type. SuperSpace is intended as a user-definable
r14513r14514
1682350    Switching the bank is done via CRU write
1683351******************************************************************************/
1684352
1685static DEVICE_START(ti99_pcb_super)
353/* Read function for the super cartridge. */
354READ8Z_MEMBER(cartridge_pcb_superspace_device::readz)
1686355{
1687   set_pointers(device, get_index_from_tagname(device->owner())-1);
356   if ((offset & GROM_MASK)==GROM_AREA)
357      gromreadz(space, offset, value, mem_mask);
358   else
359   {
360      if (m_ram_ptr==NULL)
361      {
362         *value = 0;
363      }
364      else
365      {
366         offs_t boffset = (m_ram_page << 13) | (offset & 0x1fff);
367         *value = m_ram_ptr[boffset];
368      }
369   }
1688370}
1689371
1690/*
1691    The CRU read handler. The CRU is a serial interface in the console.
1692    Using the CRU we can switch the banks in the SuperSpace cartridge.
1693*/
1694static READ8Z_DEVICE_HANDLER( read_cart_cru )
372/* Write function for the super cartridge. */
373WRITE8_MEMBER(cartridge_pcb_superspace_device::write)
1695374{
1696   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1697   cartridge_t *cartridge = pcb->cartridge;
375   if ((offset & GROM_MASK)==GROM_AREA)
376      gromwrite(space, offset, data, mem_mask);
377   else
378   {
379      if ((offset & 0x1000)==0x0000)
380      {
381         if (VERBOSE>1) LOG("ti99: gromport: Write access to cartridge ROM at address %04x ignored", offset);
382      }
383      else
384      {
385         offs_t boffset = (m_ram_page << 13) | (offset & 0x1fff);
386         m_ram_ptr[boffset] = data;
387      }
388   }
389}
390
391void cartridge_pcb_superspace_device::crureadz(offs_t offset, UINT8 *value)
392{
1698393   // offset is the bit number. The CRU base address is already divided  by 2.
1699394
1700395   // ram_page contains the bank number. We have a maximum of
r14513r14514
1718413   // BNKS1   LDCR  R1,0        Switch Banks
1719414   //         SRL   R0,1        Restore Bank Number (optional)
1720415   //         RT
1721   if (VERBOSE>1) LOG("ti99: gromport: Superspace: CRU accessed at %04x\n", offset);
1722   if ((offset & 1) == 0 || offset > 7)
416
417   // Our implementation in MESS always gets 8 bits in one go. Also, the address
418   // is twice the bit number. That is, the offset value is always a multiple
419   // of 0x10.
420
421   if (VERBOSE>2) LOG("ti99: gromport: Superspace: CRU accessed at %04x\n", offset);
422   if ((offset & 0xfff0) != 0x0800)
1723423      *value = 0;
1724424
1725425   // CRU addresses are only 1 bit wide. Bytes are transferred from LSB
1726426   // to MSB. That is, 8 bit are eight consecutive addresses. */
1727   *value = (cartridge->ram_page == (offset-1)/2);
427   *value = (m_ram_page == (offset-1)/2);
1728428}
1729429
1730static WRITE8_DEVICE_HANDLER( write_cart_cru )
430void cartridge_pcb_superspace_device::cruwrite(offs_t offset, UINT8 data)
1731431{
1732   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1733   cartridge_t *cartridge = pcb->cartridge;
1734432   // data is bit
1735433   // offset is address
1736   if (VERBOSE>1) LOG("ti99: gromport: Superspace: CRU accessed at %04x\n", offset);
1737   if (offset < 8)
434   if (VERBOSE>2) LOG("ti99: gromport: Superspace: CRU accessed at %04x\n", offset);
435
436   if (offset < 16)
1738437   {
1739438      if (data != 0)
1740         cartridge->ram_page = (offset-1)/2;
439         m_ram_page = (offset-2)/4;
1741440   }
1742441}
1743442
1744/*
1745    SuperSpace RAM read operation. RAM is located at 0x6000 to 0x7fff
1746    as 4 banks of 8 KiB each
1747*/
1748static READ8Z_DEVICE_HANDLER( read_cart_super )
1749{
1750   /* device is pcb, owner is cartslot */
1751   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1752   cartridge_t *cartridge = pcb->cartridge;
1753   offs_t boffset;
1754
1755   if (cartridge->ram_ptr==NULL)
1756   {
1757      *value = 0;
1758   }
1759   else
1760   {
1761      boffset = cartridge->ram_page * 0x2000 + (offset & 0x1fff);
1762      *value = (cartridge->ram_ptr)[boffset];
1763   }
1764}
1765
1766/*
1767    SuperSpace RAM write operation. RAM is located at 0x6000 to 0x7fff
1768    as 4 banks of 8 KiB each
1769*/
1770static WRITE8_DEVICE_HANDLER( write_cart_super )
1771{
1772   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1773   cartridge_t *cartridge = pcb->cartridge;
1774   offs_t boffset;
1775
1776   if (cartridge->ram_ptr==NULL)
1777   {
1778      if (VERBOSE>1) LOG("ti99: gromport: No cartridge RAM at address %04x", offset);
1779   }
1780   else
1781   {
1782      boffset = cartridge->ram_page * 0x2000 + (offset & 0x1fff);
1783      (cartridge->ram_ptr)[boffset] = data;
1784   }
1785}
1786
1787static int assemble_super(device_t *image)
1788{
1789   cartridge_t *cart;
1790
1791   cart = assemble_common(image);
1792   if (cart->ram_size==0)
1793   {
1794      if (VERBOSE>1) LOG("ti99: gromport: Missing RAM for SuperSpace");
1795      return IMAGE_INIT_FAIL;
1796   }
1797   return IMAGE_INIT_PASS;
1798}
1799
1800443/*****************************************************************************
1801444  Cartridge type: MBX
1802445    GROM: up to 40 KiB
r14513r14514
1809452    not emulated yet.
1810453******************************************************************************/
1811454
1812static DEVICE_START(ti99_pcb_mbx)
455/* Read function for the mbx cartridge. */
456READ8Z_MEMBER(cartridge_pcb_mbx_device::readz)
1813457{
1814   set_pointers(device, get_index_from_tagname(device->owner())-1);
1815}
1816
1817static READ8Z_DEVICE_HANDLER( read_cart_mbx )
1818{
1819   /* device is pcb, owner is cartslot */
1820   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1821   cartridge_t *cartridge = pcb->cartridge;
1822
1823   if ((offset & 0x1c00)==0x0c00)
1824   {
1825      // This is the RAM area which overrides any ROM. There is no
1826      // known banking behavior for the RAM, so we must assume that
1827      // there is only one bank.
1828      if (cartridge->ram_ptr != NULL)
1829         *value = (cartridge->ram_ptr)[offset & 0x03ff];
1830   }
458   if ((offset & GROM_MASK)==GROM_AREA)
459      gromreadz(space, offset, value, mem_mask);
1831460   else
1832461   {
1833      if (cartridge->rom_ptr != NULL)
1834         *value = (cartridge->rom_ptr)[(offset & 0x1fff)+ cartridge->rom_page*0x2000];
462      if ((offset & 0x1c00)==0x0c00)
463      {
464         // This is the RAM area which overrides any ROM. There is no
465         // known banking behavior for the RAM, so we must assume that
466         // there is only one bank.
467         if (m_ram_ptr != NULL)
468            *value = m_ram_ptr[offset & 0x03ff];
469      }
470      else
471      {
472         *value = m_rom_ptr[(offset & 0x1fff) | (m_rom_page<<13)];
473      }
1835474   }
1836475}
1837476
1838/*
1839    MBX write operation.
1840*/
1841static WRITE8_DEVICE_HANDLER( write_cart_mbx )
477/* Write function for the super cartridge. */
478WRITE8_MEMBER(cartridge_pcb_mbx_device::write)
1842479{
1843   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1844   cartridge_t *cartridge = pcb->cartridge;
1845
1846   if (offset == 0x6ffe)
480   if ((offset & GROM_MASK)==GROM_AREA)
481      gromwrite(space, offset, data, mem_mask);
482   else
1847483   {
1848      cartridge->rom_page = data & 1;
1849      return;
1850   }
484      if (offset == 0x6ffe)
485      {
486         m_rom_page = data & 1;
487         return;
488      }
1851489
1852   if ((offset & 0x1c00)==0x0c00)
1853   {
1854      if (cartridge->ram_ptr == NULL)   return;
1855      cartridge->ram_ptr[offset & 0x03ff] = data;
490      if ((offset & 0x1c00)==0x0c00)
491      {
492         if (m_ram_ptr == NULL) return;
493         m_ram_ptr[offset & 0x03ff] = data;
494      }
1856495   }
1857496}
1858497
1859
1860static int assemble_mbx(device_t *image)
1861{
1862   assemble_common(image);
1863   return IMAGE_INIT_PASS;
1864}
1865
1866498/*****************************************************************************
1867499  Cartridge type: paged379i
1868500    This cartridge consists of one 16 KiB, 32 KiB, 64 KiB, or 128 KiB EEPROM
r14513r14514
1892524    >601C            1 / 1 / 1 / 1
1893525    >601E            0 / 0 / 0 / 0
1894526
527    The paged379i cartrige does not have any GROMs.
1895528******************************************************************************/
1896529
1897static DEVICE_START(ti99_pcb_paged379i)
1898{
1899   /* device is ti99_cartslot:cartridge:pcb */
1900   set_pointers(device, get_index_from_tagname(device->owner())-1);
1901}
1902
1903530/*
1904    Read handler for the CPU address space of the cartridge
1905    Images for this area are found in the rom_sockets.
1906*/
1907static READ8Z_DEVICE_HANDLER( read_cart_paged379i )
1908{
1909   /* device is pcb, owner is cartslot */
1910   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1911   cartridge_t *cartridge = pcb->cartridge;
1912
1913   if (cartridge->rom_ptr!=NULL)
1914   {
1915      *value = (cartridge->rom_ptr)[cartridge->rom_page*0x2000 + (offset & 0x1fff)];
1916   }
1917}
1918
1919/*
1920531    Determines which bank to set, depending on the size of the ROM. This is
1921532    some magic code that actually represents different PCB versions.
1922533*/
1923static void set_paged379i_bank(cartridge_t *cartridge, int rompage)
534int cartridge_pcb_paged379i_device::get_paged379i_bank(int rompage)
1924535{
1925536   int mask = 0;
1926   if (cartridge->rom_size > 16384)
537   if (m_rom_size > 16384)
1927538   {
1928      if (cartridge->rom_size > 32768)
539      if (m_rom_size > 32768)
1929540      {
1930         if (cartridge->rom_size > 65536)
541         if (m_rom_size > 65536)
1931542            mask = 15;
1932543         else
1933544            mask = 7;
r14513r14514
1938549   else
1939550      mask = 1;
1940551
1941   cartridge->rom_page = rompage & mask;
552   return rompage & mask;
1942553}
1943554
1944/*
1945    Handle paging. We use a LS379 latch for storing the page number. On this
1946    PCB, the inverted output of the latch is used, so the order of pages
1947    is reversed. (No problem as long as the memory dump is kept exactly
1948    in the way it is stored in the EPROM.)
1949    The latch can store a value of 4 bits. We adjust the number of
1950    significant bits by the size of the memory dump (16K, 32K, 64K).
1951*/
1952static WRITE8_DEVICE_HANDLER( write_cart_paged379i )
1953{
1954   ti99_pcb_t *pcb = get_safe_pcb_token(device);
1955   cartridge_t *cartridge = pcb->cartridge;
1956555
1957   // Bits: 011x xxxx xxxb bbbx
1958   // x = don't care, bbbb = bank
1959   set_paged379i_bank(cartridge, 15 - ((offset>>1) & 15));
556/* Read function for the paged379i cartridge. */
557READ8Z_MEMBER(cartridge_pcb_paged379i_device::readz)
558{
559   if ((offset & 0xe000)==0x6000)
560      *value = m_rom_ptr[(m_rom_page<<13) | (offset & 0x1fff)];
1960561}
1961562
1962/*
1963    Paged379i modules have one EPROM dump.
1964*/
1965static int assemble_paged379i(device_t *image)
563/* Write function for the paged379i cartridge. Only used to set the bank. */
564WRITE8_MEMBER(cartridge_pcb_paged379i_device::write)
1966565{
1967   cartridge_t *cart;
1968
1969   cart = assemble_common(image);
1970   if (cart->rom_ptr==NULL)
566   // Bits: 0110 0000 000b bbbx
567   // x = don't care, bbbb = bank
568   if ((offset & 0xffe0)==0x6000)
1971569   {
1972      if (VERBOSE>1) LOG("ti99: gromport: Missing ROM for paged cartridge");
1973      return IMAGE_INIT_FAIL;
570      // Set bank
571      m_rom_page = get_paged379i_bank(15 - ((offset>>1) & 15));
1974572   }
1975   set_paged379i_bank(cart, 15);
1976   return IMAGE_INIT_PASS;
1977573}
1978574
1979575/*****************************************************************************
r14513r14514
1995591    6      >2000  = 0010 0000 0000 0000
1996592    7      >8000  = 1000 0000 0000 0000
1997593
594    No GROMs used in this type.
1998595******************************************************************************/
1999596
2000static DEVICE_START(ti99_pcb_pagedcru)
597/* Read function for the pagedcru cartridge. */
598READ8Z_MEMBER(cartridge_pcb_pagedcru_device::readz)
2001599{
2002   /* device is ti99_cartslot:cartridge:pcb */
2003   set_pointers(device, get_index_from_tagname(device->owner())-1);
600   if ((offset & 0xe000)==0x6000)
601      *value = m_rom_ptr[(m_rom_page<<13) | (offset & 0x1fff)];
2004602}
2005603
2006/*
2007    Read handler for the CPU address space of the cartridge
2008    Images for this area are found in the rom_sockets.
2009*/
2010static READ8Z_DEVICE_HANDLER( read_cart_pagedcru )
604/* Write function for the pagedcru cartridge. No effect. */
605WRITE8_MEMBER(cartridge_pcb_pagedcru_device::write)
2011606{
2012   /* device is pcb, owner is cartslot */
2013   ti99_pcb_t *pcb = get_safe_pcb_token(device);
2014   cartridge_t *cartridge = pcb->cartridge;
2015
2016   if (cartridge->rom_ptr!=NULL)
2017   {
2018      *value = (cartridge->rom_ptr)[cartridge->rom_page*0x2000 + (offset & 0x1fff)];
2019   }
2020}
2021
2022/*
2023    This type of cartridge does not support writing to the EPROM address
2024    space.
2025*/
2026static WRITE8_DEVICE_HANDLER( write_cart_pagedcru )
2027{
2028607   return;
2029608}
2030609
2031/*
2032    The CRU read handler. The CRU is a serial interface in the console.
2033    Using the CRU we can switch the banks in the SuperSpace cartridge.
2034    For documentation see the corresponding cru function above. Note we are
2035    using rom_page here.
2036*/
2037static READ8Z_DEVICE_HANDLER( read_cart_cru_paged )
610void cartridge_pcb_pagedcru_device::crureadz(offs_t offset, UINT8 *value)
2038611{
2039   ti99_pcb_t *pcb = get_safe_pcb_token(device);
2040   cartridge_t *cartridge = pcb->cartridge;
2041
2042   int page = cartridge->rom_page;
612   int page = m_rom_page;
2043613   if ((offset & 0xf800)==0x0800)
2044614   {
2045      int bit = (offset & 0x001e)/2;
615      int bit = (offset & 0x001e)>>1;
2046616      if (bit != 0)
2047617      {
2048618         page = page-(bit/2);  // 4 page flags per 8 bits
r14513r14514
2051621   }
2052622}
2053623
2054static WRITE8_DEVICE_HANDLER( write_cart_cru_paged )
624void cartridge_pcb_pagedcru_device::cruwrite(offs_t offset, UINT8 data)
2055625{
2056   ti99_pcb_t *pcb = get_safe_pcb_token(device);
2057   cartridge_t *cartridge = pcb->cartridge;
2058
2059626   if ((offset & 0xf800)==0x0800)
2060627   {
2061      int bit = (offset & 0x001e)/2;
628      int bit = (offset & 0x001e)>>1;
2062629      if (data != 0 && bit > 0)
2063630      {
2064         cartridge->rom_page = (bit-1)/2;
631         m_rom_page = (bit-1)/2;
2065632      }
2066633   }
2067634}
2068635
2069/*
2070    Pagedcru modules have one EPROM dump.
2071*/
2072static int assemble_pagedcru(device_t *image)
2073{
2074   cartridge_t *cart;
636/*****************************************************************************
637    Cartridge type: GRAMKracker
2075638
2076   cart = assemble_common(image);
2077   if (cart->rom_ptr==NULL)
2078   {
2079      if (VERBOSE>1) LOG("ti99: gromport: Missing ROM for pagedcru cartridge");
2080      return IMAGE_INIT_FAIL;
2081   }
2082   cart->rom_page = 0;
2083   return IMAGE_INIT_PASS;
2084}
639    The GK is a big cartridge that fits into the same slot as any other
640    cartridge but which offers a set of switches, an own cartridge slot,
641    and buffered RAM inside. Also, it has a built-in ROM called "LOADER".
2085642
2086/*****************************************************************************
2087  Cartridge type: gramkracker
2088******************************************************************************/
643    Its primary use is to allow the user to copy cartridge contents into the
644    buffered RAM and to modify it there. That way, ROM cartridges can be
645    customized.
2089646
2090static DEVICE_START(ti99_pcb_gramkracker)
647    The 80 KiB of the GK are allocated as follows
648    00000 - 01fff: GRAM 0
649    02000 - 03fff: GRAM 1  (alternatively, the loader GROM appears here)
650    04000 - 05fff: GRAM 2
651    06000 - 0ffff: GRAM 3-7 (alternatively, the guest GROMs appear here)
652    10000 - 11fff: RAM 1
653    12000 - 13fff: RAM 2
654
655    The GK emulates GROMs, but without a readable address counter. Thus it
656    relies on the console GROMs being still in place.
657    Also, the GK does not implement a GROM buffer.
658
659    We need to implement a complete second path of handling as it differs
660    from the multi-cart extender. Eventually, the multi-cart extender and
661    the GK should be implemented as separate slot devices.
662
663    The GK cartridge type is in fact the internal LOADER ROM. In order to use
664    the GK, the cartridge must be plugged into one slot, and the "guest"
665    cartridge of the GK must be plugged into any other slot. Also, the DIP
666    switch of the multi-cart extender must be set to "GRAM Kracker".
667*****************************************************************************/
668
669enum
2091670{
2092   /* device is ti99_cartslot:cartridge:pcb */
2093//  printf("DEVICE_START(ti99_pcb_pagedcru), tag of device=%s\n", device->tag());
2094   set_pointers(device, get_index_from_tagname(device->owner())-1);
671   CART_AUTO = 0,
672   CART_1,
673   CART_2,
674   CART_3,
675   CART_4,
676   CART_GK = 15
677};
678
679enum
680{
681   GK_OFF = 0,
682   GK_NORMAL = 1,
683   GK_GRAM0 = 0,
684   GK_OPSYS = 1,
685   GK_GRAM12 = 0,
686   GK_TIBASIC = 1,
687   GK_BANK1 = 0,
688   GK_WP = 1,
689   GK_BANK2 = 2,
690   GK_LDON = 0,
691   GK_LDOFF = 1
692};
693
694void cartridge_pcb_gramkracker_device::set_gk_guest(cartridge_device* guest)
695{
696   m_gk_guest = guest;
2095697}
2096698
2097699/*
2098    The GRAM Kracker contains one GROM and several buffered RAM chips
700    Read function for the GRAMKracker cartridge.
2099701*/
2100static int assemble_gramkracker(device_t *image)
702READ8Z_MEMBER(cartridge_pcb_gramkracker_device::readz)
2101703{
2102   cartridge_t *cart;
2103   int id,i;
704   if ((offset & GROM_MASK) == GROM_AREA)
705   {
706      // Reads from the GRAM space of the GRAM Kracker.
707      // m_gk_slot is the slot where the GK module is plugged in
708      // m_gk_guest_slot is the slot where the cartridge is plugged in
2104709
2105   device_t *cartsys = image->owner();
2106   ti99_multicart_state *cartslots = get_safe_token(cartsys);
710      // The GK does not have a readable address counter, but the console
711      // GROMs will keep our address counter up to date. That is
712      // exactly what happens in the real machine.
713      // (The console GROMs are not accessed here but directly via the datamux
714      // so we can just return without doing anything)
715      if ((offset & 0x0002)!=0) return;
2107716
2108   cart = assemble_common(image);
717      int id = ((m_grom_address & 0xe000)>>13)&0x07;
718      switch (id)
719      {
720      case 0:
721         // GRAM 0. Only return a value if switch 2 is in GRAM0 position.
722         if (m_gk_switch[2]==GK_GRAM0)
723            *value = m_ram_ptr[m_grom_address];
724         break;
725      case 1:
726         // If the loader is turned on, return loader contents.
727         if (m_gk_switch[5]==GK_LDON)
728         {
729            // The only ROM contained in the GK box is the loader
730            // Adjust the address
731            *value = m_grom_ptr[m_grom_address & 0x1fff];
732         }
733         else
734         {
735            // Loader off
736            // GRAM 1. Only return a value if switch 3 is in GRAM12 position.
737            // Otherwise, the console GROM 1 will respond (not here; it is the grom_device
738            // whose output would then not be overwritten)
739            if (m_gk_switch[3]==GK_GRAM12)
740               *value = m_ram_ptr[m_grom_address];
741         }
742         break;
743      case 2:
744         // GRAM 2. Only return a value if switch 3 is in GRAM12 position.
745         if (m_gk_switch[3]==GK_GRAM12)
746            *value = m_ram_ptr[m_grom_address];
747         break;
748      default:
749         // Cartridge space (0x6000 - 0xffff)
750         // When a cartridge is installed, it overrides the GK contents
751         // but only if it has GROMs
752         bool guest_has_grom = false;
2109753
2110   if (cart->grom_ptr==NULL)
2111   {
2112      if (VERBOSE>1) LOG("ti99: gromport: Missing loader GROM for GRAM Kracker system");
2113      return IMAGE_INIT_FAIL;
2114   }
754         if (m_gk_guest != NULL)
755         {
756            guest_has_grom = (m_gk_guest->has_grom());
757            // Note that we only have ONE real cartridge and the GK;
758            // we need not access all slots.
759            if (guest_has_grom)
760            {
761               m_gk_guest->readz(space, offset, value, mem_mask);   // read from guest
762            }
763         }
764         if (!guest_has_grom && (m_gk_switch[1]==GK_NORMAL))
765            *value = m_ram_ptr[m_grom_address];   // use the GK memory
766      }
2115767
2116   if (cart->ram_size < 81920)
2117   {
2118      if (VERBOSE>1) LOG("ti99: gromport: Missing or insufficient RAM for GRAM Kracker system");
2119      return IMAGE_INIT_FAIL;
2120   }
768      // The GK GROM emulation does not wrap at 8K boundaries.
769      m_grom_address = (m_grom_address + 1) & 0xffff;
2121770
2122   // Get slot number
2123   id = get_index_from_tagname(image);
2124   cartslots->gk_slot = id-1;
2125   cartslots->gk_guest_slot = -1;
2126   // The first cartridge that is still plugged in is the "guest" of the
2127   // GRAM Kracker
2128   for (i=0; i < NUMBER_OF_CARTRIDGE_SLOTS; i++)
771      // Reset the write address flipflop.
772      m_waddr_LSB = false;
773   }
774   else
2129775   {
2130      if (!slot_is_empty(cartsys, i) && (i != cartslots->gk_slot))
776      if (m_gk_guest != NULL)
2131777      {
2132         cartslots->gk_guest_slot = i;
2133         break;
778         // Read from the guest cartridge.
779         m_gk_guest->readz(space, offset, value, mem_mask);
2134780      }
781      else
782      {
783         // Reads from the RAM space of the GRAM Kracker.
784         if (m_gk_switch[1] == GK_OFF) return; // just don't do anything
785         switch (m_gk_switch[4])
786         {
787         // RAM is stored behind the GRAM area
788         case GK_BANK1:
789            *value = m_ram_ptr[offset+0x10000 - 0x6000];
790
791         case GK_BANK2:
792            *value = m_ram_ptr[offset+0x12000 - 0x6000];
793
794         default:
795            // Switch in middle position (WP, implies auto-select according to the page flag)
796            if (m_ram_page==0)
797               *value = m_ram_ptr[offset+0x10000 - 0x6000];
798            else
799               *value = m_ram_ptr[offset+0x12000 - 0x6000];
800         }
801      }
2135802   }
2136   return IMAGE_INIT_PASS;
2137803}
2138804
2139805/*
2140    Unplugs the GRAM Kracker. The "guest" is still plugged in, but will be
2141    treated as normal.
806    See above.
2142807*/
2143static int disassemble_gramkracker(device_t *cartslot)
808WRITE8_MEMBER(cartridge_pcb_gramkracker_device::write)
2144809{
2145   device_t *cartsys = cartslot->owner();
2146   ti99_multicart_state *cartslots = get_safe_token(cartsys);
810   // write to the guest cartridge if present
811   if (m_gk_guest != NULL)
812   {
813      m_gk_guest->write(space, offset, data, mem_mask);
814   }
2147815
2148   //int slotnumber = get_index_from_tagname(cartslot)-1;
2149   //assert(slotnumber>=0 && slotnumber<NUMBER_OF_CARTRIDGE_SLOTS);
816   if ((offset & GROM_MASK) == GROM_AREA)
817   {
818      // Writes to the GRAM space of the GRAM Kracker.
819      if ((offset & 0x0002)==0x0002)
820      {
821         // Set address
822         if (m_waddr_LSB == true)
823         {
824            // Accept low address byte (second write)
825            m_grom_address = (m_grom_address & 0xff00) | data;
826            m_waddr_LSB = false;
827         }
828         else
829         {
830            // Accept high address byte (first write)
831            m_grom_address = (m_grom_address & 0x00ff) | (data << 8);
832            m_waddr_LSB = true;
833         }
834      }
835      else
836      {
837         // Write data byte to GRAM area.
2150838
2151   cartslots->gk_slot = -1;
2152   cartslots->gk_guest_slot = -1;
839         // According to manual:
840         // Writing to GRAM 0: switch 2 set to GRAM 0 + Write protect switch (4) in 1 or 2 position
841         // Writing to GRAM 1: switch 3 set to GRAM 1-2 + Loader off (5); write prot has no effect
842         // Writing to GRAM 2: switch 3 set to GRAM 1-2 (write prot has no effect)
843         // Writing to GRAM 3-7: switch 1 set to GK_NORMAL, no cartridge inserted
844         // GK_NORMAL switch has no effect on GRAM 0-2
845         int id = ((m_grom_address & 0xe000)>>13)&0x07;
846         switch (id)
847         {
848         case 0:
849            if (m_gk_switch[2]==GK_GRAM0 && m_gk_switch[4]!=GK_WP)
850               m_ram_ptr[m_grom_address] = data;
851            break;
852         case 1:
853            if (m_gk_switch[3]==GK_GRAM12 && m_gk_switch[5]==GK_LDOFF)
854               m_ram_ptr[m_grom_address] = data;
855            break;
856         case 2:
857            if (m_gk_switch[3]==GK_GRAM12)
858               m_ram_ptr[m_grom_address] = data;
859            break;
860         default:
861            if (m_gk_switch[1]==GK_NORMAL && m_gk_guest != NULL)
862               m_ram_ptr[m_grom_address] = data;
863            break;
864         }
865         // The GK GROM emulation does not wrap at 8K boundaries.
866         m_grom_address = (m_grom_address + 1) & 0xffff;
2153867
2154   return disassemble_std(cartslot);
2155}
868         // Reset the write address flipflop.
869         m_waddr_LSB = false;
870      }
871   }
872   else
873   {
874      // Write to the RAM space of the GRAM Kracker.
875      if (m_gk_guest == NULL)
876      {
877         if (m_gk_switch[1] == GK_OFF) return; // just don't do anything
878         switch (m_gk_switch[4])
879         {
880         // RAM is stored behind the GRAM area
881         case GK_BANK1:
882            m_ram_ptr[offset+0x10000 - 0x6000] = data;
883            break;
2156884
885         case GK_BANK2:
886            m_ram_ptr[offset+0x12000 - 0x6000] = data;
887            break;
2157888
2158/*****************************************************************************
2159  Device metadata
2160******************************************************************************/
889         default:
890            // Switch in middle position (WP, implies auto-select according to the page flag)
891            // This is handled like in Extended Basic (using addresses)
892            m_ram_page = (offset >> 1) & 1;
893            break;
894         }
895      }
896   }
897}
2161898
2162899/*
2163    Get the pointer to the GROM data from the cartridge. Called by the GROM.
900    Read cartridge mapper CRU interface (Guest in GRAM Kracker)
2164901*/
2165static UINT8 *get_grom_ptr(device_t *device)
902void cartridge_pcb_gramkracker_device::crureadz(offs_t offset, UINT8 *value)
2166903{
2167   ti99_pcb_t *pcb = get_safe_pcb_token(device);
2168   return pcb->cartridge->grom_ptr;
904   if (m_gk_guest != NULL)   m_gk_guest->crureadz(offset, value);
2169905}
2170906
2171static MACHINE_CONFIG_FRAGMENT(ti99_cart_common)
2172   MCFG_GROM_ADD_P( "grom_3", 3, get_grom_ptr, 0x0000, 0x1800, cart_grom_ready )
2173   MCFG_GROM_ADD_P( "grom_4", 4, get_grom_ptr, 0x2000, 0x1800, cart_grom_ready )
2174   MCFG_GROM_ADD_P( "grom_5", 5, get_grom_ptr, 0x4000, 0x1800, cart_grom_ready )
2175   MCFG_GROM_ADD_P( "grom_6", 6, get_grom_ptr, 0x6000, 0x1800, cart_grom_ready )
2176   MCFG_GROM_ADD_P( "grom_7", 7, get_grom_ptr, 0x8000, 0x1800, cart_grom_ready )
2177MACHINE_CONFIG_END
2178
2179static DEVICE_GET_INFO(ti99_cart_common)
907/*
908    Write cartridge mapper CRU interface (Guest in GRAM Kracker)
909*/
910void cartridge_pcb_gramkracker_device::cruwrite(offs_t offset, UINT8 data)
2180911{
2181   switch(state)
2182   {
2183      /* --- the following bits of info are returned as 64-bit signed integers --- */
2184      case DEVINFO_INT_TOKEN_BYTES:
2185         info->i = sizeof(ti99_pcb_t);
2186         break;
2187      case DEVINFO_INT_INLINE_CONFIG_BYTES:
2188         info->i = 0;
2189         break;
912   if (m_gk_guest != NULL) m_gk_guest->cruwrite(offset, data);
913}
2190914
2191      case DEVINFO_PTR_MACHINE_CONFIG:
2192         info->machine_config = MACHINE_CONFIG_NAME(ti99_cart_common);
2193         break;
915/*****************************************************************************
916    A cartridge. In fact, just the empty cartridge, as the image to be loaded
917    will be included as the PCB.
918*****************************************************************************/
2194919
2195      /* --- the following bits of info are returned as pointers to functions --- */
2196      case DEVINFO_FCT_START:
2197         info->start = DEVICE_START_NAME(ti99_pcb_std);
2198         break;
2199      case DEVINFO_FCT_STOP:
2200         /* Nothing */
2201         break;
2202      case DEVINFO_FCT_RESET:
2203         /* Nothing */
2204         break;
2205      case TI99CARTINFO_FCT_6000_R:
2206         info->f = (genf *) read_cart_std; break;
920static const pcb_type pcbdefs[] =
921{
922   { PCB_STANDARD, "standard" },
923   { PCB_PAGED, "paged" },
924   { PCB_MINIMEM, "minimem" },
925   { PCB_SUPER, "super" },
926   { PCB_MBX, "mbx" },
927   { PCB_PAGED379I, "paged379i" },
928   { PCB_PAGEDCRU, "pagedcru" },
929   { PCB_GKRACKER, "gramkracker" },
930   { 0, NULL}
931};
2207932
2208      case TI99CARTINFO_FCT_6000_W:
2209         info->f = (genf *) write_cart_std; break;
933cartridge_device::cartridge_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
934: bus8z_device(mconfig, TICARTRIDGE, "TI-99 cartridge", tag, owner, clock),
935  device_image_interface(mconfig, *this),
936  m_pcb(NULL)
937{
938   m_shortname = "cartridge";
939}
2210940
2211      case TI99CARTINFO_FCT_CRU_R:
2212         /* Nothing */
2213         break;
941void cartridge_device::device_start()
942{
943   if (VERBOSE>7) LOG("cartridge_device: device_start\n");
944}
2214945
2215      case TI99CARTINFO_FCT_CRU_W:
2216         /* Nothing */
2217         break;
2218
2219      case TI99CART_FCT_ASSM:
2220         info->f = (genf *) assemble_std;
2221         break;
2222
2223      case TI99CART_FCT_DISASSM:
2224         info->f = (genf *) disassemble_std;
2225         break;
2226
2227      /* --- the following bits of info are returned as NULL-terminated strings --- */
2228      case DEVINFO_STR_NAME:
2229         strcpy(info->s, "TI99 standard cartridge pcb");
2230         break;
2231      case DEVINFO_STR_FAMILY:
2232         strcpy(info->s, "TI99 cartridge pcb");
2233         break;
2234      case DEVINFO_STR_VERSION:
2235         strcpy(info->s, "1.0");
2236         break;
2237      case DEVINFO_STR_SOURCE_FILE:
2238         strcpy(info->s, __FILE__);
2239         break;
2240      case DEVINFO_STR_CREDITS:
2241         /* Nothing */
2242         break;
2243   }
946void cartridge_device::device_stop()
947{
948   if (VERBOSE>7) LOG("cartridge_device: device_stop\n");
2244949}
2245950
2246DEVICE_GET_INFO(ti99_cartridge_pcb_none)
951void cartridge_device::device_reset()
2247952{
2248   switch(state)
953   if (m_type==PCB_GKRACKER)
2249954   {
2250      case DEVINFO_FCT_START:
2251         info->start = DEVICE_START_NAME(ti99_pcb_none);
2252         break;
2253
2254      case DEVINFO_STR_NAME:
2255         strcpy(info->s, "TI99 empty cartridge");
2256         break;
2257
2258      // Don't create GROMs
2259      case DEVINFO_PTR_MACHINE_CONFIG:
2260         break;
2261
2262      default:
2263         DEVICE_GET_INFO_CALL(ti99_cart_common);
2264         break;
955      cartridge_pcb_gramkracker_device* pcb = static_cast<cartridge_pcb_gramkracker_device*>(m_pcb);
956      pcb->m_gk_switch[1] = input_port_read(*owner(), GKSWITCH1_TAG);
957      pcb->m_gk_switch[2] = input_port_read(*owner(), GKSWITCH2_TAG);
958      pcb->m_gk_switch[3] = input_port_read(*owner(), GKSWITCH3_TAG);
959      pcb->m_gk_switch[4] = input_port_read(*owner(), GKSWITCH4_TAG);
960      pcb->m_gk_switch[5] = input_port_read(*owner(), GKSWITCH5_TAG);
2265961   }
2266962}
2267963
2268DEVICE_GET_INFO(ti99_cartridge_pcb_std)
964void cartridge_device::device_config_complete()
2269965{
2270   DEVICE_GET_INFO_CALL(ti99_cart_common);
966   if (VERBOSE>8) LOG("cartridge_device: device_config_complete\n");
967   update_names();
2271968}
2272969
2273DEVICE_GET_INFO(ti99_cartridge_pcb_paged)
970void cartridge_device::prepare_cartridge()
2274971{
2275   switch(state)
2276   {
2277      case DEVINFO_FCT_START:
2278         info->start = DEVICE_START_NAME(ti99_pcb_paged);
2279         break;
972   int rom2_length;
2280973
2281      case TI99CART_FCT_ASSM:
2282         info->f = (genf *) assemble_paged;
2283         break;
974   UINT8* grom_ptr;
2284975
2285      case TI99CARTINFO_FCT_6000_R:
2286         info->f = (genf *) read_cart_paged;
2287         break;
976   const memory_region *regg;
977   const memory_region *regr;
978   const memory_region *regr2;
2288979
2289      case TI99CARTINFO_FCT_6000_W:
2290         info->f = (genf *) write_cart_paged;
2291         break;
980   m_pcb->m_grom_size = m_rpk->get_resource_length("grom_socket");
2292981
2293      case DEVINFO_STR_NAME:
2294         strcpy(info->s, "TI99 paged cartridge pcb");
2295         break;
982   if (m_pcb->m_grom_size > 0)
983   {
984      regg = subregion("grom_contents");
985      grom_ptr = (UINT8*)m_rpk->get_contents_of_socket("grom_socket");
986      memcpy(regg->base(), grom_ptr, m_pcb->m_grom_size);
2296987
2297      default:
2298         DEVICE_GET_INFO_CALL(ti99_cart_common);
2299         break;
988      if (m_type==PCB_GKRACKER)
989      {
990         cartridge_pcb_gramkracker_device* pcb = static_cast<cartridge_pcb_gramkracker_device*>(m_pcb);
991         pcb->m_grom_ptr = grom_ptr;
992      }
993
994      // Find the GROMs and keep their pointers
995      m_pcb->m_grom[0] = static_cast<ti99_grom_device*>(subdevice(GROM3_TAG));
996      if (m_pcb->m_grom_size > 0x2000) m_pcb->m_grom[1] = static_cast<ti99_grom_device*>(subdevice(GROM4_TAG));
997      if (m_pcb->m_grom_size > 0x4000) m_pcb->m_grom[2] = static_cast<ti99_grom_device*>(subdevice(GROM5_TAG));
998      if (m_pcb->m_grom_size > 0x6000) m_pcb->m_grom[3] = static_cast<ti99_grom_device*>(subdevice(GROM6_TAG));
999      if (m_pcb->m_grom_size > 0x8000) m_pcb->m_grom[4] = static_cast<ti99_grom_device*>(subdevice(GROM7_TAG));
23001000   }
2301}
23021001
2303DEVICE_GET_INFO(ti99_cartridge_pcb_minimem)
2304{
2305   switch(state)
1002   m_pcb->m_rom_size = m_rpk->get_resource_length("rom_socket");
1003   if (m_pcb->m_rom_size > 0)
23061004   {
2307      case DEVINFO_FCT_START:
2308         info->start = DEVICE_START_NAME(ti99_pcb_minimem);
2309         break;
1005      regr = subregion("rom_contents");
1006      m_pcb->m_rom_ptr = (UINT8*)m_rpk->get_contents_of_socket("rom_socket");
1007      memcpy(regr->base(), m_pcb->m_rom_ptr, m_pcb->m_rom_size);
1008   }
23101009
2311      case TI99CART_FCT_ASSM:
2312         info->f = (genf *) assemble_minimem;
2313         break;
2314      case TI99CARTINFO_FCT_6000_R:
2315         info->f = (genf *) read_cart_minimem;
2316         break;
2317      case TI99CARTINFO_FCT_6000_W:
2318         info->f = (genf *) write_cart_minimem;
2319         break;
1010   rom2_length = m_rpk->get_resource_length("rom2_socket");
1011   if (rom2_length > 0)
1012   {
1013      // sizes do not differ between rom and rom2
1014      regr2 = subregion("rom2_contents");
1015      m_pcb->m_rom2_ptr = (UINT8*)m_rpk->get_contents_of_socket("rom2_socket");
1016      memcpy(regr2->base(), m_pcb->m_rom2_ptr, rom2_length);
1017   }
23201018
2321      case DEVINFO_STR_NAME:
2322         strcpy(info->s, "TI99 MiniMemory cartridge pcb");
2323         break;
2324
2325      default:
2326         DEVICE_GET_INFO_CALL(ti99_cart_common);
2327         break;
1019   m_pcb->m_ram_size = m_rpk->get_resource_length("ram_socket");
1020   if (m_pcb->m_ram_size > 0)
1021   {
1022      // TODO: Consider to use a region as well. If so, do not forget to memcpy.
1023      m_pcb->m_ram_ptr = (UINT8*)m_rpk->get_contents_of_socket("ram_socket");
23281024   }
23291025}
23301026
2331DEVICE_GET_INFO(ti99_cartridge_pcb_super)
1027bool cartridge_device::has_grom()
23321028{
2333   switch(state)
2334   {
2335      case DEVINFO_FCT_START:
2336         info->start = DEVICE_START_NAME(ti99_pcb_super);
2337         break;
2338
2339      case TI99CART_FCT_ASSM:
2340         info->f = (genf *) assemble_super;
2341         break;
2342      case TI99CARTINFO_FCT_6000_R:
2343         info->f = (genf *) read_cart_super;
2344         break;
2345      case TI99CARTINFO_FCT_6000_W:
2346         info->f = (genf *) write_cart_super;
2347         break;
2348      case TI99CARTINFO_FCT_CRU_R:
2349         info->f = (genf *) read_cart_cru;
2350         break;
2351      case TI99CARTINFO_FCT_CRU_W:
2352         info->f = (genf *) write_cart_cru;
2353         break;
2354
2355      // Don't create GROMs
2356      case DEVINFO_PTR_MACHINE_CONFIG:
2357         break;
2358
2359      case DEVINFO_STR_NAME:
2360         strcpy(info->s, "TI99 SuperSpace cartridge pcb");
2361         break;
2362
2363      default:
2364         DEVICE_GET_INFO_CALL(ti99_cart_common);
2365         break;
2366   }
1029   assert (m_pcb != NULL);
1030   return (m_pcb->m_grom_size>0);
23671031}
23681032
2369DEVICE_GET_INFO(ti99_cartridge_pcb_mbx)
1033void cartridge_device::set_gk_guest(cartridge_device* guest)
23701034{
2371   switch(state)
2372   {
2373      case DEVINFO_FCT_START:
2374         info->start = DEVICE_START_NAME(ti99_pcb_mbx);
2375         break;
1035   assert (m_pcb != NULL);
1036   assert (m_rpk->get_type()==PCB_GKRACKER);
23761037
2377      case TI99CART_FCT_ASSM:
2378         info->f = (genf *) assemble_mbx;
2379         break;
2380      case TI99CARTINFO_FCT_6000_R:
2381         info->f = (genf *) read_cart_mbx;
2382         break;
2383      case TI99CARTINFO_FCT_6000_W:
2384         info->f = (genf *) write_cart_mbx;
2385         break;
2386
2387      case DEVINFO_STR_NAME:
2388         strcpy(info->s, "TI99 MBX cartridge pcb");
2389         break;
2390
2391      default:
2392         DEVICE_GET_INFO_CALL(ti99_cart_common);
2393         break;
2394   }
1038   cartridge_pcb_gramkracker_device* pcb = static_cast<cartridge_pcb_gramkracker_device*>(m_pcb);
1039   pcb->set_gk_guest(guest);
23951040}
23961041
2397DEVICE_GET_INFO(ti99_cartridge_pcb_paged379i)
1042void cartridge_device::set_switch(int swtch, int val)
23981043{
2399   switch(state)
2400   {
2401      case DEVINFO_FCT_START:
2402         info->start = DEVICE_START_NAME(ti99_pcb_paged379i);
2403         break;
1044   assert (m_pcb != NULL);
1045   assert (m_rpk->get_type()==PCB_GKRACKER);
1046   cartridge_pcb_gramkracker_device* pcb = static_cast<cartridge_pcb_gramkracker_device*>(m_pcb);
1047   pcb->m_gk_switch[swtch] = val;
1048}
24041049
2405      case TI99CART_FCT_ASSM:
2406         info->f = (genf *) assemble_paged379i; break;
24071050
2408      case TI99CARTINFO_FCT_6000_R:
2409         info->f = (genf *) read_cart_paged379i;
2410         break;
1051bool cartridge_device::call_load()
1052{
1053   // File name is in m_basename
1054   // return true = error
1055   if (VERBOSE>8) LOG("cartridge_device: loading\n");
1056   rpk_reader *reader = new rpk_reader(pcbdefs);
24111057
2412      case TI99CARTINFO_FCT_6000_W:
2413         info->f = (genf *) write_cart_paged379i;
2414         break;
1058   astring ntag(tag(), ":PCB");
1059   const char* mytag = ntag.cstr();
24151060
2416      case DEVINFO_STR_NAME:
2417         strcpy(info->s, "TI99 paged379i cartridge pcb");
2418         break;
1061   gromport_device *gp = static_cast<gromport_device*>(owner());
1062   int slot = get_index_from_tagname();
24191063
2420      default:
2421         DEVICE_GET_INFO_CALL(ti99_cart_common);
2422         break;
2423   }
2424}
2425
2426DEVICE_GET_INFO(ti99_cartridge_pcb_pagedcru)
2427{
2428   switch(state)
1064   try
24291065   {
2430      case DEVINFO_FCT_START:
2431         info->start = DEVICE_START_NAME(ti99_pcb_pagedcru);
1066      m_rpk = reader->open(machine().options(), m_basename, machine().system().name);
1067      m_type = m_rpk->get_type();
1068      switch (m_type)
1069      {
1070      case PCB_STANDARD:
1071         if (VERBOSE>6) LOG("cartridge_device: standard PCB\n");
1072         m_pcb = static_cast<cartridge_pcb_standard_device*>(&machine().add_dynamic_device(*this, TICARTPCBSTD, mytag, 0));
24321073         break;
2433
2434      case TI99CART_FCT_ASSM:
2435         info->f = (genf *) assemble_pagedcru;
1074      case PCB_PAGED:
1075         if (VERBOSE>6) LOG("cartridge_device: paged PCB\n");
1076         m_pcb = static_cast<cartridge_pcb_paged_device*>(&machine().add_dynamic_device(*this, TICARTPCBPAG, mytag, 0));
24361077         break;
2437
2438      case TI99CARTINFO_FCT_6000_R:
2439         info->f = (genf *) read_cart_pagedcru;
1078      case PCB_MINIMEM:
1079         if (VERBOSE>6) LOG("cartridge_device: minimem PCB\n");
1080         m_pcb = static_cast<cartridge_pcb_minimem_device*>(&machine().add_dynamic_device(*this, TICARTPCBMIN, mytag, 0));
24401081         break;
2441
2442      case TI99CARTINFO_FCT_6000_W:
2443         info->f = (genf *) write_cart_pagedcru;
1082      case PCB_SUPER:
1083         if (VERBOSE>6) LOG("cartridge_device: superspace PCB\n");
1084         m_pcb = static_cast<cartridge_pcb_superspace_device*>(&machine().add_dynamic_device(*this, TICARTPCBSUP, mytag, 0));
24441085         break;
2445
2446      case TI99CARTINFO_FCT_CRU_R:
2447         info->f = (genf *) read_cart_cru_paged;
1086      case PCB_MBX:
1087         if (VERBOSE>6) LOG("cartridge_device: MBX PCB\n");
1088         m_pcb = static_cast<cartridge_pcb_mbx_device*>(&machine().add_dynamic_device(*this, TICARTPCBMBX, mytag, 0));
24481089         break;
2449      case TI99CARTINFO_FCT_CRU_W:
2450         info->f = (genf *) write_cart_cru_paged;
1090      case PCB_PAGED379I:
1091         if (VERBOSE>6) LOG("cartridge_device: Paged379i PCB\n");
1092         m_pcb = static_cast<cartridge_pcb_paged379i_device*>(&machine().add_dynamic_device(*this, TICARTPCBPGI, mytag, 0));
24511093         break;
2452
2453      case DEVINFO_STR_NAME:
2454         strcpy(info->s, "TI99 pagedcru cartridge pcb");
1094      case PCB_PAGEDCRU:
1095         if (VERBOSE>6) LOG("cartridge_device: PagedCRU PCB\n");
1096         m_pcb = static_cast<cartridge_pcb_pagedcru_device*>(&machine().add_dynamic_device(*this, TICARTPCBPGC, mytag, 0));
24551097         break;
2456
2457      default:
2458         DEVICE_GET_INFO_CALL(ti99_cart_common);
1098      case PCB_GKRACKER:
1099         if (VERBOSE>6) LOG("cartridge_device: GRAMKracker PCB\n");
1100         m_pcb = static_cast<cartridge_pcb_gramkracker_device*>(&machine().add_dynamic_device(*this, TICARTPCBGRK, mytag, 0));
1101         gp->set_gk_slot(slot);
24591102         break;
1103      }
1104
1105      prepare_cartridge();
1106      m_pcb->set_machine(machine()); // TODO: Feels like a hack.
1107      gp->change_slot(true, slot);
1108
1109      machine().schedule_hard_reset();  // could be configurable by DIP switch, simulating taping the RESET line
24601110   }
1111   catch (rpk_exception& err)
1112   {
1113      LOG("Failed to load cartridge '%s': %s\n", basename(), err.to_string());
1114      m_rpk = NULL;
1115      m_err = IMAGE_ERROR_INVALIDIMAGE;
1116      return true;
1117   }
1118   return false;
24611119}
24621120
2463DEVICE_GET_INFO(ti99_cartridge_pcb_gramkracker)
1121void cartridge_device::call_unload()
24641122{
2465   switch(state)
1123   if (VERBOSE>6) LOG("cartridge_device: unloading from %s\n", tag());
1124
1125   gromport_device *gp = static_cast<gromport_device*>(owner());
1126   int slot = get_index_from_tagname();
1127   if (slot == gp->m_gk_slot)
24661128   {
2467      case DEVINFO_FCT_START:
2468         info->start = DEVICE_START_NAME(ti99_pcb_gramkracker);
2469         break;
1129      gp->m_gk_slot = -1;      // Remove GRAMKracker slot index
1130      // seems to be no way to change the dip switch except by keyboard
1131   }
24701132
2471      case TI99CART_FCT_ASSM:
2472         info->f = (genf *) assemble_gramkracker;
2473         break;
1133   if (m_rpk != NULL)
1134   {
1135      // Transfer RAM contents back into socket structure
1136/*      if (m_pcb->m_ram_size > 0)
1137        {
1138            UINT8* ram_ptr;
1139            ram_ptr = (UINT8*)m_rpk->get_contents_of_socket("ram_socket");
1140            memcpy(ram_ptr, m_pcb->m_ram_ptr, m_pcb->m_ram_size);
1141        } */
1142      m_rpk->close(); // will write NVRAM contents
1143      delete m_rpk;
1144   }
24741145
2475      case TI99CART_FCT_DISASSM:
2476         info->f = (genf *) disassemble_gramkracker;
2477         break;
1146   // TODO: How can we remove the dynamically added device?
1147   m_pcb = NULL;
1148   gp->change_slot(false, get_index_from_tagname());
1149}
24781150
2479      case DEVINFO_STR_NAME:
2480         strcpy(info->s, "TI99 GRAM Kracker system");
2481         break;
1151READ8Z_MEMBER(cartridge_device::readz)
1152{
1153   assert(m_pcb != NULL);
1154   m_pcb->readz(space, offset, value, mem_mask);
1155}
24821156
2483      default:
2484         DEVICE_GET_INFO_CALL(ti99_cart_common);
2485         break;
2486   }
1157WRITE8_MEMBER(cartridge_device::write)
1158{
1159   assert(m_pcb != NULL);
1160   m_pcb->write(space, offset, data, mem_mask);
24871161}
24881162
2489/*****************************************************************************
2490  The cartridge handling of the multi-cartridge system.
2491  Every cartridge contains a PCB device. The memory handlers delegate the calls
2492  to the respective handlers of the cartridges.
2493******************************************************************************/
2494/*
2495    Initialize a cartridge. Each cartridge contains a PCB device.
2496*/
2497static DEVICE_START( ti99_cartridge )
1163void cartridge_device::crureadz(offs_t offset, UINT8 *value)
24981164{
2499   multicartslot_t *cart = get_safe_cartslot_token(device);
1165   assert(m_pcb != NULL);
1166   m_pcb->crureadz(offset, value);
1167}
25001168
2501   /* find the PCB device */
2502   cart->pcb_device = device->subdevice(TAG_PCB);
1169void cartridge_device::cruwrite(offs_t offset, UINT8 data)
1170{
1171   assert(m_pcb != NULL);
1172   m_pcb->cruwrite(offset, data);
25031173}
25041174
25051175/*
2506    Load the cartridge image files. Apart from reading, we set pointers
2507    to the image files so that during runtime we do not need search
2508    operations.
1176    Find the index of the cartridge name. We assume the format
1177    <name><number>, i.e. the number is the longest string from the right
1178    which can be interpreted as a number. Subtract 1.
25091179*/
2510static DEVICE_IMAGE_LOAD( ti99_cartridge )
1180int cartridge_device::get_index_from_tagname()
25111181{
2512   device_t *pcbdev = cartslot_get_pcb(&image.device());
2513//  device_t *cartsys = image.device().owner();
2514//  ti99_multicart_state *cartslots = get_safe_token(cartsys);
1182   const char *mytag = tag();
1183   int maxlen = strlen(mytag);
1184   int i;
1185   for (i=maxlen-1; i >=0; i--)
1186      if (mytag[i] < 48 || mytag[i] > 57) break;
25151187
2516   int result;
1188   return atoi(mytag+i+1)-1;
1189}
25171190
2518   if (pcbdev != NULL)
2519   {
2520      /* If we are here, we have a multicart. */
2521      ti99_pcb_t *pcb = get_safe_pcb_token(pcbdev);
2522      multicartslot_t *cart = get_safe_cartslot_token(&image.device());
1191static GROM_CONFIG(grom3_config)
1192{
1193   false, 3, CARTGROM_TAG, 0x0000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, gromport_device, ready_line)
1194};
1195static GROM_CONFIG(grom4_config)
1196{
1197   false, 4, CARTGROM_TAG, 0x2000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, gromport_device, ready_line)
1198};
1199static GROM_CONFIG(grom5_config)
1200{
1201   false, 5, CARTGROM_TAG, 0x4000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, gromport_device, ready_line)
1202};
1203static GROM_CONFIG(grom6_config)
1204{
1205   false, 6, CARTGROM_TAG, 0x6000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, gromport_device, ready_line)
1206};
1207static GROM_CONFIG(grom7_config)
1208{
1209   false, 7, CARTGROM_TAG, 0x8000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, gromport_device, ready_line)
1210};
25231211
2524      /* try opening this as a multicart */
2525      // This line requires that cartslot_t be included in cartslot.h,
2526      // otherwise one cannot make use of multicart handling within such a
2527      // custom LOAD function.
2528      multicart_open_error me = multicart_open(image.device().machine().options(), image.filename(), image.device().machine().system().name, MULTICART_FLAGS_LOAD_RESOURCES, &cart->mc);
2529      if (VERBOSE>1) LOG("ti99: gromport: opened %s as cartridge\n", image.filename());
2530      // Now that we have loaded the image files, let the PCB put them all
2531      // together. This means we put the images in a structure which allows
2532      // for a quick access by the memory handlers. Every PCB defines an
2533      // own assembly method.
2534      if (me == MCERR_NONE)
2535         result = pcb->assemble(image);
2536      else
2537         fatalerror("Error loading multicart: %s", multicart_error_text(me));
2538   }
2539   else
2540   {
2541      fatalerror("Error loading multicart: no pcb found.");
2542   }
2543   return result;
1212/*
1213    The GROM devices on a cartridge.
1214*/
1215static MACHINE_CONFIG_FRAGMENT( cartridge_groms )
1216   MCFG_GROM_ADD( GROM3_TAG, grom3_config )
1217   MCFG_GROM_ADD( GROM4_TAG, grom4_config )
1218   MCFG_GROM_ADD( GROM5_TAG, grom5_config )
1219   MCFG_GROM_ADD( GROM6_TAG, grom6_config )
1220   MCFG_GROM_ADD( GROM7_TAG, grom7_config )
1221MACHINE_CONFIG_END
1222
1223machine_config_constructor cartridge_device::device_mconfig_additions() const
1224{
1225   return MACHINE_CONFIG_NAME( cartridge_groms );
25441226}
25451227
25461228/*
2547    This is called when the cartridge is unplugged (or the emulator is
2548    stopped).
1229    Memory area for one cartridge.
25491230*/
2550static DEVICE_IMAGE_UNLOAD( ti99_cartridge )
1231ROM_START( cartridge_memory )
1232   ROM_REGION(0xa000, CARTGROM_TAG, 0)
1233   ROM_FILL(0x0000, 0xa000, 0x00)
1234   ROM_REGION(0x2000, CARTROM_TAG, 0)
1235   ROM_FILL(0x0000, 0x2000, 0x00)
1236   ROM_REGION(0x2000, CARTROM2_TAG, 0)
1237   ROM_FILL(0x0000, 0x2000, 0x00)
1238ROM_END
1239
1240const rom_entry *cartridge_device::device_rom_region() const
25511241{
2552   device_t *pcbdev;
2553   //ti99_multicart_state *cartslots = get_safe_token(image.device().owner());
1242   return ROM_NAME( cartridge_memory );
1243}
25541244
2555   if (downcast<legacy_device_base *>(&image.device())->token() == NULL)
2556   {
2557      // This means something went wrong during the pcb
2558      // identification (e.g. one of the cartridge files was not
2559      // found). We do not need to (and cannot) unload the cartridge.
2560      return;
2561   }
2562   pcbdev = cartslot_get_pcb(image);
1245void gromport_device::set_gk_slot(int slot)
1246{
1247   if (VERBOSE>5) LOG("gromport_device: Setting slot %d as GK slot.\n", slot);
1248   m_gk_slot = slot;
1249}
25631250
2564   if (pcbdev != NULL)
1251void gromport_device::find_gk_guest()
1252{
1253   // The first slot which contains a cartridge (not the GK) is the guest of the GK
1254   for (int i=0; i < m_numcart; i++)
25651255   {
2566      ti99_pcb_t *pcb = get_safe_pcb_token(pcbdev);
2567      multicartslot_t *cart = get_safe_cartslot_token(&image.device());
2568
2569      if (cart->mc != NULL)
1256      if (i==m_gk_slot) continue;
1257      if (m_cartridge[i]->is_available())
25701258      {
2571         /* Remove pointers and de-big-endianize RAM contents. */
2572         pcb->disassemble(image);
2573
2574         // Close the multicart; all RAM resources will be
2575         // written to disk
2576         multicart_close(image.device().machine().options(),cart->mc);
2577         cart->mc = NULL;
2578
1259         m_cartridge[m_gk_slot]->set_gk_guest(m_cartridge[i]);
1260         if (VERBOSE>5) LOG("gromport_device: Setting slot %d as guest slot.\n", i);
1261         return;
25791262      }
25801263   }
1264   m_cartridge[m_gk_slot]->set_gk_guest(NULL);
25811265}
25821266
25831267/*****************************************************************************
2584  The overall multi-cartridge slot system. It contains instances of
2585  cartridges which contain PCB devices. The memory handlers delegate the calls
2586  to the respective handlers of the cartridges.
1268    The complete multi-port cartridge system. Hosts up to four cartridges
1269    at this time.
1270*****************************************************************************/
25871271
2588  Note that the term "multi-cartridge system" and "multicart" are not the same:
2589  A "multicart" may contain multiple resources, organized on a PCB. The multi-
2590  cart system may thus host multiple multicarts.
1272gromport_device::gromport_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
1273: bus8z_device(mconfig, GROMPORT, "TI-99 multi-cartridge extender", tag, owner, clock)
1274{
1275   m_gk_slot = -1;
1276}
25911277
2592  Actually, the name of the device should be changed (however, the device name
2593  length is limited)
2594******************************************************************************/
2595
2596static READ8Z_DEVICE_HANDLER( ti99_cart_gk_rz );
2597static WRITE8_DEVICE_HANDLER( ti99_cart_gk_w );
2598static void cartridge_gram_kracker_writeg(device_t *cartsys, int offset, UINT8 data);
2599static void cartridge_gram_kracker_readg(device_t *device, UINT8 *value);
2600static READ8Z_DEVICE_HANDLER( ti99_cart_cru_gk_rz );
2601static WRITE8_DEVICE_HANDLER( ti99_cart_cru_gk_w );
2602
26031278/*
2604    Instantiation of a multicart system for the TI family.
2605*/
2606static DEVICE_START(ti99_multicart)
2607{
2608   int i;
2609   ti99_multicart_state *cartslots = get_safe_token(device);
2610   const ti99_multicart_config* cartconf = (const ti99_multicart_config*)get_config(device);
1279    Activates a slot in the multi-cartridge extender.
1280    Setting the slot is done by accessing the GROM ports using a
1281    specific address:
26111282
2612   cartslots->active_slot = 0;
2613   cartslots->next_free_slot = 0;
1283    slot 0:  0x9800 (0x9802, 0x9c00, 0x9c02)   : cartridge1
1284    slot 1:  0x9804 (0x9806, 0x9c04, 0x9c06)   : cartridge2
1285    ...
1286    slot 15: 0x983c (0x983e, 0x9c3c, 0x9c3e)   : cartridge16
26141287
2615   for (i=0; i < NUMBER_OF_CARTRIDGE_SLOTS; i++)
2616   {
2617      cartslots->cartridge[i].pcb = NULL;
2618      clear_slot(device, i);
2619   }
1288    Scheme:
26201289
2621   devcb_write_line ready = DEVCB_LINE(cartconf->ready);
1290    1001 1Wxx xxxx xxM0 (M=mode; M=0: data, M=1: address; W=write)
26221291
2623   cartslots->ready.resolve(ready, *device);
1292    The following addresses are theoretically available, but the
1293    built-in OS does not use them; i.e. cartridges will not be
1294    included in the selection list, and their features will not be
1295    found by lookup, but they could be accessed directly by user
1296    programs.
1297    slot 16: 0x9840 (0x9842, 0x9c40, 0x9c42)
1298    ...
1299    slot 255:  0x9bfc (0x9bfe, 0x9ffc, 0x9ffe)
26241300
2625   cartslots->gk_slot = -1;
2626   cartslots->gk_guest_slot = -1;
1301    Setting the GROM base should select one cartridge, but the ROMs in the
1302    CPU space must also be switched. As there is no known special mechanism
1303    we assume that by switching the GROM base, the ROM is automatically
1304    switched.
26271305
2628   // The cartslot system is initialized now. The cartridges themselves
2629   // need to check whether their parts are available.
2630}
1306    Caution: This means that cartridges which do not have at least one
1307    GROM cannot be switched with this mechanism.
26311308
2632static DEVICE_STOP(ti99_multicart)
2633{
2634//  printf("DEVICE_STOP(ti99_multicart)\n");
2635}
1309    We assume that the slot number is already calculated in the caller:
1310    slotnumber>=0 && slotnumber<=255
26361311
2637static DEVICE_RESET(ti99_multicart)
1312    NOTE: The OS will stop searching when it finds slots 1 and 2 empty.
1313    Interestingly, cartridge subroutines are found nevertheless, even when
1314    the cartridge is plugged into a higher slot.
1315*/
1316void gromport_device::set_slot(int slotnumber)
26381317{
2639   // Consider to propagate RESET to cartridges.
2640   // However, schematics do not reveal any pin for resetting a cartridge;
2641   // the reset line is an input used when plugging in a cartridge.
1318   if (VERBOSE>7)
1319      if (m_active_slot != slotnumber) LOG("ti99: gromport: Setting cartslot to %d\n", slotnumber);
26421320
2643   ti99_multicart_state *cartslots = get_safe_token(device);
2644   int slotnumber = input_port_read(device->machine(), "CARTSLOT");
2645   cartslots->fixed_slot = slotnumber-1; /* auto = -1 */
1321   if (m_fixed_slot==AUTO)
1322      m_active_slot = slotnumber;
1323   else
1324      m_active_slot = m_fixed_slot;
26461325}
26471326
2648/*
2649    Accesses the ROM regions of the cartridge for reading.
2650*/
2651READ8Z_DEVICE_HANDLER( gromportr_rz )
1327int gromport_device::get_active_slot(bool changebase, offs_t offset)
26521328{
2653   ti99_multicart_state *cartslots = get_safe_token(device);
2654   int slot = cartslots->active_slot;
2655   cartridge_t *cart;
2656
2657   // Handle GRAM Kracker
2658   // We could also consider to use a flag, but input_port_read uses
2659   // a hashtable, so this should not be too slow
2660   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
1329   int slot;
1330   if (changebase)
26611331   {
2662      ti99_cart_gk_rz(device, offset, value);
2663      return;
1332      if ((offset & GROM_MASK) == GROM_AREA)
1333      {
1334         set_slot((offset>>2) & 0x00ff);
1335      }
26641336   }
1337   slot = m_active_slot;
26651338
2666   /* Sanity check. Higher slots are always empty. */
2667   if (slot >= NUMBER_OF_CARTRIDGE_SLOTS)
2668      return;
1339   // Here we have a design flaw in the operating system of the TI.
1340   // The system iterates through all banks to check for multiple cartridges.
1341   // If we do not have a multi-cart system, each bank shows the same contents
1342   // since there is no decoding of the bank information.
1343   // This brings us into trouble here: If only slot 1 is occupied, slot 2
1344   // would deliver zeros only. This differs from slot 1, so we get a
1345   // meaningless selection option.
1346   //
1347   // If we have only one cartridge in slot 1, we pretend that we have a
1348   // single-slot system. The slot variable is forced to the first slot.
26691349
2670   // Same as above (GROM read)
2671   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
1350   if (m_fixed_slot==AUTO && m_next_free_slot==1)
26721351      slot=0;
26731352
2674   cart = &cartslots->cartridge[slot];
1353   return slot;
1354}
26751355
2676   if (!slot_is_empty(device, slot))
1356void gromport_device::change_slot(bool inserted, int index)
1357{
1358   if (inserted)
26771359   {
2678//      printf("try it on slot %d\n", slot);
2679      ti99_pcb_t *pcbdef = get_safe_pcb_token(cart->pcb);
2680      (*pcbdef->read)(cart->pcb, offset, value);
1360      if (VERBOSE>6) LOG("gromport_device: Inserted into slot %d\n", index);
1361      if (m_next_free_slot <= index)
1362         m_next_free_slot = index+1;
26811363   }
1364   else
1365   {
1366      if (VERBOSE>6) LOG("gromport_device: Removed from slot %d\n", index);
1367      for (int i=NUMBER_OF_CARTRIDGE_SLOTS-1; i>=0; i--)
1368      {
1369         if (m_cartridge[i]->is_available())
1370         {
1371            m_next_free_slot = index+1;
1372            break;
1373         }
1374      }
1375   }
1376   if (VERBOSE>6) LOG("Next free slot: %d\n", m_next_free_slot);
1377
1378   if (m_gk_slot != -1) find_gk_guest();
26821379}
26831380
2684/*
2685    Accesses the ROM regions of the cartridge for writing.
2686*/
2687WRITE8_DEVICE_HANDLER( gromportr_w )
1381/****************************************************
1382    Memory space (ROM, GROM)
1383****************************************************/
1384READ8Z_MEMBER(gromport_device::readz)
26881385{
2689   ti99_multicart_state *cartslots = get_safe_token(device);
2690   int slot = cartslots->active_slot;
2691   cartridge_t *cart;
2692
2693   // Handle GRAM Kracker
2694   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
2695      ti99_cart_gk_w(device, offset, data);
2696
2697   /* Sanity check. Higher slots are always empty. */
2698   if (slot >= NUMBER_OF_CARTRIDGE_SLOTS)
1386   // GRAMKracker support
1387   // We *could* check the DIP switch, but this would only introduce another
1388   // source of confusion when the GRAMKracker is plugged in, but the switch
1389   // is not set.
1390   // The check for the presence of the GK using the index is safer.
1391   if ((m_gk_slot != -1) /*&& (input_port_read(*this, "CARTSLOT")==CART_GK) */)
1392   {
1393      m_cartridge[m_gk_slot]->readz(space, offset, value, mem_mask);
26991394      return;
1395   }
27001396
2701   /* Same as above (READ16). */
2702   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
2703      slot=0;
1397   int slot = get_active_slot(true, offset);
27041398
2705   cart = &cartslots->cartridge[slot];
2706
2707   if (!slot_is_empty(device, slot))
1399   // If we have a GROM access, we need to send the read request to all
1400   // attached cartridges so the slot is irrelevant here. Each GROM
1401   // contains an internal address counter, and we must make sure they all stay in sync.
1402   if ((offset & GROM_MASK) == GROM_AREA)
27081403   {
2709      ti99_pcb_t *pcbdef = get_safe_pcb_token(cart->pcb);
2710      (*pcbdef->write)(cart->pcb, offset, data);
1404      for (int i=0; i < m_numcart; i++)
1405      {
1406         if (m_cartridge[i]->is_available())
1407         {
1408            UINT8 newval = *value;
1409            m_cartridge[i]->readz(space, offset, &newval, mem_mask);
1410            if (i==slot)
1411            {
1412               *value = newval;
1413            }
1414         }
1415      }
27111416   }
1417   else
1418   {
1419      if (slot < m_numcart && m_cartridge[slot]->is_available())
1420      {
1421         m_cartridge[slot]->readz(space, offset, value, mem_mask);
1422      }
1423   }
27121424}
27131425
2714/*
2715    Set the address pointer value or write to GRAM (if available).
2716*/
2717WRITE8_DEVICE_HANDLER(gromportg_w)
1426WRITE8_MEMBER(gromport_device::write)
27181427{
2719//  int slot;
2720   cartridge_t *cartridge;
2721   ti99_multicart_state *cartslots = get_safe_token(device);
2722
2723   // Set the cart slot.
2724   // 1001 1wbb bbbb bbr0
2725
2726   cartridge_slot_set(device, (UINT8)((offset>>2) & 0x00ff));
2727//  slot = cartslots->active_slot;
2728
2729//  if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
2730//      slot=0;
2731
2732   // Handle the GRAM Kracker
2733   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
1428   // GRAMKracker support
1429   if ((m_gk_slot != -1) /*&& (input_port_read(*this, "CARTSLOT")==CART_GK)*/)
27341430   {
2735      cartridge_gram_kracker_writeg(device, offset, data);
1431      m_cartridge[m_gk_slot]->write(space, offset, data, mem_mask);
27361432      return;
27371433   }
27381434
2739   // We need to send the write request to all attached cartridges
2740   // so the slot is irrelevant here. (We don't have GRAM cartridges,
2741   // anyway, so it's just used for setting the adddress)
2742   for (int j=0; j < NUMBER_OF_CARTRIDGE_SLOTS; j++)
1435   int slot = get_active_slot(true, offset);
1436
1437   // Same issue as above (read)
1438   // We don't have GRAM cartridges, anyway, so it's just used for setting the address.
1439   if ((offset & GROM_MASK) == GROM_AREA)
27431440   {
2744      cartridge = &cartslots->cartridge[j];
2745      // Send the request to all mounted GROMs
2746      if (cartridge->pcb!=NULL)
1441      for (int i=0; i < m_numcart; i++)
27471442      {
2748         for (int i=0; i < 5; i++)
1443         if (m_cartridge[i]->is_available())
27491444         {
2750            ti99_pcb_t *pcbdef = get_safe_pcb_token(cartridge->pcb);
2751            if (pcbdef->grom[i]!=NULL)
2752               ti99grom_w(pcbdef->grom[i], offset, data);
1445            m_cartridge[i]->write(space, offset, data, mem_mask);
27531446         }
27541447      }
27551448   }
1449   else
1450   {
1451      if (slot < m_numcart && m_cartridge[slot]->is_available())
1452      {
1453         //      LOG("try it on slot %d\n", slot);
1454         m_cartridge[slot]->write(space, offset, data, mem_mask);
1455      }
1456   }
27561457}
27571458
27581459/*
2759    GROM access. Nota bene: We only react to the request if there is a GROM
2760    at the selected bank with the currently selected ID. If not, just do
2761    nothing; do not return a value.
2762    (We must not return a value since all GROMs are connected in parallel, so
2763    returning a 0, for instance, will spoil the processing of other GROMs.)
1460    Called from the cartriges.
27641461*/
2765READ8Z_DEVICE_HANDLER(gromportg_rz)
1462WRITE_LINE_MEMBER(gromport_device::ready_line)
27661463{
2767   int slot;
2768   cartridge_t *cartridge;
2769   ti99_multicart_state *cartslots = get_safe_token(device);
1464   m_ready(state);
1465}
27701466
2771   // Set the cart slot. Bit pattern:
2772   // 1001 1wbb bbbb bbr0
2773
2774   cartridge_slot_set(device, (UINT8)((offset>>2) & 0x00ff));
2775
2776   // Handle the GRAM Kracker
2777   // BTW, the GK does not have a readable address counter, but the console
2778   // GROMs will keep our address counter up to date. That is
2779   // exactly what happens in the real machine.
2780   // (The console GROMs are not accessed here but directly via the datamux
2781   // so we can just return without doing anything)
2782   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
1467/****************************************************
1468    CRU interface
1469****************************************************/
1470void gromport_device::crureadz(offs_t offset, UINT8 *value)
1471{
1472   // GRAMKracker support
1473   if ((m_gk_slot != -1) /* && (input_port_read(*this, "CARTSLOT")==CART_GK)*/)
27831474   {
2784      if ((offset & 0x0002)==0) cartridge_gram_kracker_readg(device, value);
1475      m_cartridge[m_gk_slot]->crureadz(offset, value);
27851476      return;
27861477   }
27871478
2788   slot = cartslots->active_slot;
1479   int slot = get_active_slot(false, offset);
27891480
2790   // This fixes a minor issue: The selection mechanism of the console
2791   // concludes that there are multiple cartridges plugged in when it
2792   // checks the different GROM bases and finds different data. Empty
2793   // slots should return 0 at all addresses, but when we have only
2794   // one cartridge, this entails that the second cartridge is
2795   // different (all zeros), and so we get a meaningless selection
2796   // option between cartridge 1 and 1 and 1 ...
2797   //
2798   // So if next_free_slot==1, we have one cartridge in slot 0.
2799   // In that case we trick the OS to believe that the addressed
2800   // cartridge appears at all locations which causes it to assume a
2801   // standard single cartslot.
1481   /* Sanity check. Higher slots are always empty. */
1482   if (slot >= m_numcart)
1483      return;
28021484
2803   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
2804      slot=0;
2805
2806   // We need to send the read request to all cartriges so that
2807   // the internal address counters stay in sync. However, we only respect
2808   // the return of the selected cartridge.
2809   for (int j=0; j < NUMBER_OF_CARTRIDGE_SLOTS; j++)
1485   if (m_cartridge[slot]->is_available())
28101486   {
2811      // Select the cartridge which the multicart system is currently pointing at
2812      cartridge = &cartslots->cartridge[j];
2813      // Send the request to all mounted GROMs on this PCB
2814      if (cartridge->pcb!=NULL)
2815      {
2816         for (int i=0; i < 5; i++)
2817         {
2818            ti99_pcb_t *pcbdef = get_safe_pcb_token(cartridge->pcb);
2819            if (pcbdef->grom[i]!=NULL)
2820            {
2821               UINT8 newvalue = *value;
2822               ti99grom_rz(pcbdef->grom[i], offset, &newvalue);
2823               if (j==slot)
2824               {
2825                  *value = newvalue;
2826               }
2827            }
2828         }
2829      }
1487      m_cartridge[slot]->crureadz(offset, value);
28301488   }
28311489}
28321490
2833/*
2834    CRU interface to cartridges, used only by SuperSpace and Pagedcru
2835    style cartridges.
2836    The SuperSpace has a CRU-based memory mapper. It is accessed via the
2837    cartridge slot on CRU base >0800.
2838*/
2839READ8Z_DEVICE_HANDLER( gromportc_rz )
1491void gromport_device::cruwrite(offs_t offset, UINT8 data)
28401492{
2841   ti99_multicart_state *cartslots = get_safe_token(device);
2842   cartridge_t *cart;
2843
2844   int slot = cartslots->active_slot;
2845
2846   // Handle GRAM Kracker
2847   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
1493   // GRAMKracker support
1494   if ((m_gk_slot != -1) /*&& (input_port_read(*this, "CARTSLOT")==CART_GK)*/)
28481495   {
2849      ti99_cart_cru_gk_rz(device, offset, value);
1496      m_cartridge[m_gk_slot]->cruwrite(offset, data);
28501497      return;
28511498   }
28521499
1500   int slot = get_active_slot(true, offset);
1501
28531502   /* Sanity check. Higher slots are always empty. */
2854   if (slot >= NUMBER_OF_CARTRIDGE_SLOTS)
1503   if (slot >= m_numcart)
28551504      return;
28561505
2857   /* Same as above (READ16). */
2858   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
2859      slot=0;
1506   if (m_cartridge[slot]->is_available())
1507   {
1508      m_cartridge[slot]->cruwrite(offset, data);
1509   }
1510}
28601511
2861   cart = &cartslots->cartridge[slot];
1512/****************************************************
1513    Device management
1514****************************************************/
1515void gromport_device::device_start(void)
1516{
1517   // Resolve the callback line
1518   const gromport_config *intf = reinterpret_cast<const gromport_config *>(static_config());
1519   m_ready.resolve(intf->ready, *this);
28621520
2863   if (!slot_is_empty(device, slot))
1521   // Add the cartridges to the list
1522   device_iterator *iter = new device_iterator(*this, 1);
1523
1524   int i = 0;
1525   cartridge_device *cart = static_cast<cartridge_device*>(iter->first());
1526
1527   // Skip the first node (it's the gromport)
1528   cart = static_cast<cartridge_device*>(iter->next());
1529   while (cart != NULL)
28641530   {
2865      ti99_pcb_t *pcbdef = get_safe_pcb_token(cart->pcb);
2866      if (pcbdef->cruread != NULL)
2867         (*pcbdef->cruread)(cart->pcb, offset, value);
1531      m_cartridge[i++] = cart;
1532      // LOG("tag = %s\n", cart->tag());
1533      cart = static_cast<cartridge_device*>(iter->next());
28681534   }
1535   m_numcart = i;
1536   m_next_free_slot = 0;
1537   m_active_slot = 0;
28691538}
28701539
2871/*
2872    Write cartridge mapper CRU interface (SuperSpace, Pagedcru)
2873*/
2874WRITE8_DEVICE_HANDLER( gromportc_w )
1540void gromport_device::device_stop(void)
28751541{
2876   ti99_multicart_state *cartslots = get_safe_token(device);
2877   cartridge_t *cart;
1542}
28781543
2879   int slot = cartslots->active_slot;
1544void gromport_device::device_reset(void)
1545{
1546   m_active_slot = 0;
1547   m_fixed_slot = input_port_read(*this, "CARTSLOT") - 1;
1548}
28801549
2881   // Handle GRAM Kracker
2882   if ((cartslots->gk_slot != -1) && (input_port_read(device->machine(), "CARTSLOT")==CART_GK))
1550INPUT_CHANGED_MEMBER( gromport_device::gk_changed )
1551{
1552   if (VERBOSE>7) LOG("gromport: input changed %d - %d\n", (int)((UINT64)param & 0x07), newval);
1553   if (m_gk_slot != -1)
28831554   {
2884      ti99_cart_cru_gk_w(device, offset, data);
2885      return;
1555      m_cartridge[m_gk_slot]->set_switch((UINT64)param & 0x07, newval); // param is a pointer type
28861556   }
1557}
28871558
2888   /* Sanity check. Higher slots are always empty. */
2889   if (slot >= NUMBER_OF_CARTRIDGE_SLOTS)
2890      return;
1559INPUT_PORTS_START(gromport_device)
1560   PORT_START( "CARTSLOT" )
1561   PORT_DIPNAME( 0x0f, 0x00, "Multi-cartridge slot" )
1562      PORT_DIPSETTING(    0x00, "Auto" )
1563      PORT_DIPSETTING(    0x01, "Slot 1" )
1564      PORT_DIPSETTING(    0x02, "Slot 2" )
1565      PORT_DIPSETTING(    0x03, "Slot 3" )
1566      PORT_DIPSETTING(    0x04, "Slot 4" )
1567      PORT_DIPSETTING(    0x0f, "GRAM Kracker" )
28911568
2892   /* Same as above (READ16). */
2893   if (cartslots->fixed_slot==AUTO && cartslots->next_free_slot==1)
2894      slot=0;
1569   PORT_START( GKSWITCH1_TAG )
1570   PORT_DIPNAME( 0x01, 0x01, "GK switch 1" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED_MEMBER(DEVICE_SELF, gromport_device, gk_changed, 1)
1571      PORT_DIPSETTING(    0x00, "GK Off" )
1572      PORT_DIPSETTING(    0x01, DEF_STR( Normal ) )
28951573
2896   cart = &cartslots->cartridge[slot];
1574   PORT_START( GKSWITCH2_TAG )
1575   PORT_DIPNAME( 0x01, 0x01, "GK switch 2" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED_MEMBER(DEVICE_SELF, gromport_device, gk_changed, 2)
1576      PORT_DIPSETTING(    0x00, "GRAM 0" )
1577      PORT_DIPSETTING(    0x01, "Op Sys" )
28971578
2898   if (!slot_is_empty(device, slot))
2899   {
2900      ti99_pcb_t *pcbdef = get_safe_pcb_token(cart->pcb);
2901      if (pcbdef->cruwrite != NULL)
2902         (*pcbdef->cruwrite)(cart->pcb, offset, data);
2903   }
1579   PORT_START( GKSWITCH3_TAG )
1580   PORT_DIPNAME( 0x01, 0x01, "GK switch 3" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED_MEMBER(DEVICE_SELF, gromport_device, gk_changed, 3)
1581      PORT_DIPSETTING(    0x00, "GRAM 1-2" )
1582      PORT_DIPSETTING(    0x01, "TI BASIC" )
1583
1584   PORT_START( GKSWITCH4_TAG )
1585   PORT_DIPNAME( 0x03, 0x01, "GK switch 4" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED_MEMBER(DEVICE_SELF, gromport_device, gk_changed, 4)
1586      PORT_DIPSETTING(    0x00, "Bank 1" )
1587      PORT_DIPSETTING(    0x01, "W/P" )
1588      PORT_DIPSETTING(    0x02, "Bank 2" )
1589
1590   PORT_START( GKSWITCH5_TAG )
1591   PORT_DIPNAME( 0x01, 0x00, "GK switch 5" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED_MEMBER(DEVICE_SELF, gromport_device, gk_changed, 5)
1592      PORT_DIPSETTING(    0x00, "Loader On" )
1593      PORT_DIPSETTING(    0x01, "Loader Off" )
1594INPUT_PORTS_END
1595
1596static MACHINE_CONFIG_FRAGMENT(gromport_device)
1597   MCFG_DEVICE_ADD("cartridge1", TICARTRIDGE, 0)
1598   MCFG_DEVICE_ADD("cartridge2", TICARTRIDGE, 0)
1599   MCFG_DEVICE_ADD("cartridge3", TICARTRIDGE, 0)
1600   MCFG_DEVICE_ADD("cartridge4", TICARTRIDGE, 0)
1601MACHINE_CONFIG_END
1602
1603machine_config_constructor gromport_device::device_mconfig_additions() const
1604{
1605   return MACHINE_CONFIG_NAME( gromport_device );
29041606}
29051607
2906/*****************************************************************************
2907   GramKracker support
2908   The 80 KiB of the GK are allocated as follows
2909   00000 - 01fff: GRAM 0
2910   02000 - 03fff: GRAM 1  (alternatively, the loader GROM appears here)
2911   04000 - 05fff: GRAM 2
2912   06000 - 0ffff: GRAM 3-7 (alternatively, the guest GROMs appear here)
2913   10000 - 11fff: RAM 1
2914   12000 - 13fff: RAM 2
1608ioport_constructor gromport_device::device_input_ports() const
1609{
1610   return INPUT_PORTS_NAME(gromport_device);
1611}
29151612
2916   The GK emulates GROMs, but without a readable address counter. Thus it
2917   relies on the console GROMs being still in place (compare with the HSGPL).
2918   Also, the GK does not implement a GROM buffer.
2919******************************************************************************/
1613const device_type GROMPORT = &device_creator<gromport_device>;
1614const device_type TICARTRIDGE = &device_creator<cartridge_device>;
29201615
1616const device_type TICARTPCBSTD = &device_creator<cartridge_pcb_standard_device>;
1617const device_type TICARTPCBPAG = &device_creator<cartridge_pcb_paged_device>;
1618const device_type TICARTPCBMIN = &device_creator<cartridge_pcb_minimem_device>;
1619const device_type TICARTPCBSUP = &device_creator<cartridge_pcb_superspace_device>;
1620const device_type TICARTPCBMBX = &device_creator<cartridge_pcb_mbx_device>;
1621const device_type TICARTPCBPGI = &device_creator<cartridge_pcb_paged379i_device>;
1622const device_type TICARTPCBPGC = &device_creator<cartridge_pcb_pagedcru_device>;
1623const device_type TICARTPCBGRK = &device_creator<cartridge_pcb_gramkracker_device>;
1624
1625/*****************************************************************************************************************
1626******************************************************************************************************************
1627RPK format support
1628
1629    A RPK file ("rompack") contains a collection of dump files and a layout
1630    file that defines the kind of circuit board (PCB) used in the cartridge
1631    and the mapping of dumps to sockets on the board.
1632
1633Example:
1634    <?xml version="1.0" encoding="utf-8"?>
1635    <romset>
1636        <resources>
1637            <rom id="gromimage" file="ed-assmg.bin" />
1638        </resources>
1639        <configuration>
1640            <pcb type="standard">
1641                <socket id="grom_socket" uses="gromimage"/>
1642            </pcb>
1643        </configuration>
1644    </romset>
1645
1646DTD:
1647    <!ELEMENT romset (resources, configuration)>
1648    <!ELEMENT resources (rom|ram)+>
1649    <!ELEMENT rom EMPTY>
1650    <!ELEMENT ram EMPTY>
1651    <!ELEMENT configuration (pcb)>
1652    <!ELEMENT pcb (socket)+>
1653    <!ELEMENT socket EMPTY>
1654    <!ATTLIST romset version CDATA #IMPLIED>
1655    <!ATTLIST rom id ID #REQUIRED
1656    <!ATTLIST rom file CDATA #REQUIRED>
1657    <!ATTLIST rom crc CDATA #IMPLIED>
1658    <!ATTLIST rom sha1 CDATA #IMPLIED>
1659    <!ATTLIST ram id ID #REQUIRED>
1660    <!ATTLIST ram type (volatile|persistent) #IMPLIED>
1661    <!ATTLIST ram store (internal|external) #IMPLIED>
1662    <!ATTLIST ram file CDATA #IMPLIED>
1663    <!ATTLIST ram length CDATA #REQUIRED>
1664    <!ATTLIST pcb type CDATA #REQUIRED>
1665    <!ATTLIST socket id ID #REQUIRED>
1666    <!ATTLIST socket uses IDREF #REQUIRED>
1667
1668****************************************************************************/
1669
1670#include "unzip.h"
1671#include "xmlfile.h"
1672
1673/****************************************
1674    RPK class
1675****************************************/
29211676/*
2922    Reads from the GRAM space of the GRAM Kracker.
1677    Constructor.
29231678*/
2924static void cartridge_gram_kracker_readg(device_t *cartsys, UINT8 *value)
1679rpk::rpk(emu_options& options, const char* sysname)
1680   :m_options(options),
1681   m_system_name(sysname)
29251682{
2926   // gk_slot is the slot where the GK module is plugged in
2927   // gk_guest_slot is the slot where the cartridge is plugged in
1683   m_sockets.reset();
1684};
29281685
2929   cartridge_t *gromkrackerbox, *gkguestcart;
2930   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1686rpk::~rpk()
1687{
1688   if (VERBOSE>6) LOG("Destroy RPK\n");
1689}
29311690
2932   // Not null, since this is only called when there is really a GK module
2933   gromkrackerbox = &cartslots->cartridge[cartslots->gk_slot];
1691/*
1692    Deliver the contents of the socket by name of the socket.
1693*/
1694void* rpk::get_contents_of_socket(const char *socket_name)
1695{
1696   rpk_socket *socket = m_sockets.find(socket_name);
1697   if (socket==NULL) return NULL;
1698   return socket->get_contents();
1699}
29341700
2935   if ((gromkrackerbox->grom_address & 0xe000) == 0x0000)
2936   {
2937      // GRAM 0. Only return a value if switch 2 is in GRAM0 position.
2938      if (get_gk_switch(cartsys, 2)==GK_GRAM0)
2939         *value = (gromkrackerbox->ram_ptr)[gromkrackerbox->grom_address];
2940   }
1701/*
1702    Deliver the length of the contents of the socket by name of the socket.
1703*/
1704int rpk::get_resource_length(const char *socket_name)
1705{
1706   rpk_socket *socket = m_sockets.find(socket_name);
1707   if (socket==NULL) return 0;
1708   return socket->get_content_length();
1709}
29411710
2942   if ((gromkrackerbox->grom_address & 0xe000) == 0x2000)
1711void rpk::add_socket(const char* id, rpk_socket *newsock)
1712{
1713   m_sockets.append(id, *newsock);
1714}
1715
1716/*-------------------------------------------------
1717    rpk_close - closes a rpk
1718    Saves the contents of the NVRAMs and frees all memory.
1719-------------------------------------------------*/
1720
1721void rpk::close()
1722{
1723   // Save the NVRAM contents
1724   rpk_socket *socket = m_sockets.first();
1725   while (socket != NULL)
29431726   {
2944      // If the loader is turned on, return loader contents.
2945      if (get_gk_switch(cartsys, 5)==GK_LDON)
1727      if (socket->persistent_ram())
29461728      {
2947         // The only ROM contained in the GK box is the loader
2948         *value = gromkrackerbox->grom_ptr[gromkrackerbox->grom_address & 0x1fff];
1729         image_battery_save_by_name(m_options, socket->get_pathname(), socket->get_contents(), socket->get_content_length());
29491730      }
2950      else
2951      {
2952         // Loader off
2953         // GRAM 1. Only return a value if switch 3 is in GRAM12 position.
2954         // Otherwise, the console GROM 1 will respond (at another location)
2955         if (get_gk_switch(cartsys, 3)==GK_GRAM12)
2956            *value = (gromkrackerbox->ram_ptr)[gromkrackerbox->grom_address];
2957      }
1731      socket->cleanup();
1732      socket = socket->m_next;
29581733   }
1734}
29591735
2960   if ((gromkrackerbox->grom_address & 0xe000) == 0x4000)
2961   {
2962      // GRAM 2. Only return a value if switch 3 is in GRAM12 position.
2963      if (get_gk_switch(cartsys, 3)==GK_GRAM12)
2964         *value = (gromkrackerbox->ram_ptr)[gromkrackerbox->grom_address];
2965   }
1736/**************************************************************
1737    RPK socket (location in the PCB where a chip is plugged in;
1738    not a network socket)
1739***************************************************************/
29661740
2967   if (((gromkrackerbox->grom_address & 0xe000) == 0x6000) ||
2968      ((gromkrackerbox->grom_address & 0x8000) == 0x8000))
2969   {
2970      // Cartridge space
2971      // When a cartridge is installed, it overrides the GK contents
2972      // but only if it has GROMs
2973      int cartwithgrom = FALSE;
2974      gkguestcart = &cartslots->cartridge[cartslots->gk_guest_slot];
1741rpk_socket::rpk_socket(const char* id, int length, void* contents, const char *pathname)
1742: m_id(id), m_length(length), m_next(NULL), m_contents(contents), m_pathname(pathname)
1743{
1744};
29751745
2976      // Send the request to all mounted GROMs
2977      if (gkguestcart->pcb!=NULL)
1746rpk_socket::rpk_socket(const char* id, int length, void* contents)
1747: m_id(id), m_length(length), m_next(NULL), m_contents(contents), m_pathname(NULL)
1748{
1749};
1750
1751/*
1752    Locate a file in the ZIP container
1753*/
1754const zip_file_header* rpk_reader::find_file(zip_file *zip, const char *filename, UINT32 crc)
1755{
1756   const zip_file_header *header;
1757   for (header = zip_file_first_file(zip); header != NULL; header = zip_file_next_file(zip))
1758   {
1759      // We don't check for CRC == 0.
1760      if (crc != 0)
29781761      {
2979         for (int i=0; i < 5; i++)
1762         // if the CRC and name both match, we're good
1763         // if the CRC matches and the name doesn't, we're still good
1764         if (header->crc == crc)
1765            return header;
1766      }
1767      else
1768      {
1769         if (core_stricmp(header->filename, filename)==0)
29801770         {
2981            ti99_pcb_t *pcbdef = get_safe_pcb_token(gkguestcart->pcb);
2982            if (pcbdef->grom[i]!=NULL)
2983            {
2984               ti99grom_rz(pcbdef->grom[i], 0x9800, value);
2985               cartwithgrom = TRUE;
2986            }
1771            return header;
29871772         }
29881773      }
2989      if (!cartwithgrom && get_gk_switch(cartsys, 1)==GK_NORMAL)
2990      {
2991         *value = (gromkrackerbox->ram_ptr)[gromkrackerbox->grom_address];
2992      }
29931774   }
2994
2995   // The GK GROM emulation does not wrap at 8K boundaries.
2996   gromkrackerbox->grom_address = (gromkrackerbox->grom_address+1) & 0xffff;
2997
2998   /* Reset the write address flipflop. */
2999   gromkrackerbox->waddr_LSB = FALSE;
1775   return NULL;
30001776}
30011777
30021778/*
3003    Writes to the GRAM space of the GRAM Kracker.
1779    Load a rom resource and put it in a pcb socket instance.
30041780*/
3005static void cartridge_gram_kracker_writeg(device_t *cartsys, int offset, UINT8 data)
1781rpk_socket* rpk_reader::load_rom_resource(zip_file* zip, xml_data_node* rom_resource_node, const char* socketname)
30061782{
3007   // gk_slot is the slot where the GK module is plugged in
3008   // gk_guest_slot is the slot where the cartridge is plugged in
3009   cartridge_t *gramkrackerbox;
3010   ti99_multicart_state *cartslots = get_safe_token(cartsys);
1783   const char* file;
1784   const char* crcstr;
1785   const char* sha1;
1786   zip_error ziperr;
1787   UINT32 crc;
1788   int length;
1789   void* contents;
1790   const zip_file_header *header;
30111791
3012   // This is only called when there is really a GK module
3013   gramkrackerbox = &cartslots->cartridge[cartslots->gk_slot];
1792   // find the file attribute (required)
1793   file = xml_get_attribute_string(rom_resource_node, "file", NULL);
1794   if (file == NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<rom> must have a 'file' attribute");
30141795
3015   if ((offset & 0x0002)==0x0002)
1796   if (VERBOSE>6) LOG("gromport/RPK: Loading ROM contents for socket '%s' from file %s\n", socketname, file);
1797
1798   // check for crc
1799   crcstr = xml_get_attribute_string(rom_resource_node, "crc", NULL);
1800   if (crcstr==NULL)
30161801   {
3017      // Set address
3018      if (gramkrackerbox->waddr_LSB)
3019      {
3020         /* Accept low byte (2nd write) */
3021         gramkrackerbox->grom_address = (gramkrackerbox->grom_address & 0xff00) | data;
3022         gramkrackerbox->waddr_LSB = FALSE;
3023      }
3024      else
3025      {
3026         /* Accept high byte (1st write) */
3027         gramkrackerbox->grom_address = ((data<<8) & 0xff00) | (gramkrackerbox->grom_address & 0x00ff);
3028         gramkrackerbox->waddr_LSB = TRUE;
3029      }
1802      // no CRC, just find the file in the RPK
1803      header = find_file(zip, file, 0);
30301804   }
30311805   else
30321806   {
3033      // According to manual:
3034      // Writing to GRAM 0: switch 2 set to GRAM 0 + Write protect switch (4) in 1 or 2 position
3035      // Writing to GRAM 1: switch 3 set to GRAM 1-2 + Loader off (5); write prot has no effect
3036      // Writing to GRAM 2: switch 1 set to GRAM 1-2 (write prot has no effect)
3037      // Writing to GRAM 3-7: switch set to GK_NORMAL, no cartridge inserted
3038      // GK_NORMAL switch has no effect on GRAM 0-2
3039      if (((gramkrackerbox->grom_address & 0xe000)==0x0000 && get_gk_switch(cartsys, 2)==GK_GRAM0 && get_gk_switch(cartsys, 4)!=GK_WP)
3040         || ((gramkrackerbox->grom_address & 0xe000)==0x2000 && get_gk_switch(cartsys, 3)==GK_GRAM12 && get_gk_switch(cartsys, 5)==GK_LDOFF)
3041         || ((gramkrackerbox->grom_address & 0xe000)==0x4000 && get_gk_switch(cartsys, 3)==GK_GRAM12)
3042         || ((gramkrackerbox->grom_address & 0xe000)==0x6000 && get_gk_switch(cartsys, 1)==GK_NORMAL && cartslots->gk_guest_slot==-1)
3043         || ((gramkrackerbox->grom_address & 0x8000)==0x8000 && get_gk_switch(cartsys, 1)==GK_NORMAL && cartslots->gk_guest_slot==-1))
3044      {
3045         (gramkrackerbox->ram_ptr)[gramkrackerbox->grom_address] = data;
3046      }
3047      // The GK GROM emulation does not wrap at 8K boundaries.
3048      gramkrackerbox->grom_address = (gramkrackerbox->grom_address+1) & 0xffff;
1807      crc = strtoul(crcstr, NULL, 16);
1808      header = find_file(zip, file, crc);
1809   }
1810   if (header == NULL)   throw rpk_exception(RPK_INVALID_FILE_REF, "File not found or CRC check failed");
30491811
3050      /* Reset the write address flipflop. */
3051      gramkrackerbox->waddr_LSB = FALSE;
1812   length = header->uncompressed_length;
1813
1814   // Allocate storage
1815   contents = malloc(length);
1816   if (contents==NULL) throw rpk_exception(RPK_OUT_OF_MEMORY);
1817
1818   // and unzip file from the zip file
1819   ziperr = zip_file_decompress(zip, contents, length);
1820   if (ziperr != ZIPERR_NONE) throw rpk_exception(RPK_ZIP_ERROR);
1821
1822   // check for sha1
1823   sha1 = xml_get_attribute_string(rom_resource_node, "sha1", NULL);
1824   if (sha1 != NULL)
1825   {
1826      hash_collection actual_hashes;
1827      actual_hashes.compute((const UINT8 *)contents, length, hash_collection::HASH_TYPES_CRC_SHA1);
1828
1829      hash_collection expected_hashes;
1830      expected_hashes.add_from_string(hash_collection::HASH_SHA1, sha1, strlen(sha1));
1831
1832      if (actual_hashes != expected_hashes) throw rpk_exception(RPK_INVALID_FILE_REF, "SHA1 check failed");
30521833   }
1834
1835   // Create a socket instance
1836   return new rpk_socket(socketname, length, contents);
30531837}
30541838
30551839/*
3056    Reads from the RAM space of the GRAM Kracker.
1840    Load a ram resource and put it in a pcb socket instance.
30571841*/
3058static READ8Z_DEVICE_HANDLER( ti99_cart_gk_rz )
1842rpk_socket* rpk_reader::load_ram_resource(emu_options &options, xml_data_node* ram_resource_node, const char* socketname, const char* system_name)
30591843{
3060   // gk_slot is the slot where the GK module is plugged in
3061   // gk_guest_slot is the slot where the cartridge is plugged in
3062   cartridge_t *gromkrackerbox, *gkguestcart;
3063   ti99_multicart_state *cartslots = get_safe_token(device);
1844   const char* length_string;
1845   const char* ram_type;
1846   const char* ram_filename;
1847   const char* ram_pname;
1848   int length;
1849   void* contents;
30641850
3065   gromkrackerbox = &cartslots->cartridge[cartslots->gk_slot];
1851   // find the length attribute
1852   length_string = xml_get_attribute_string(ram_resource_node, "length", NULL);
1853   if (length_string == NULL) throw rpk_exception(RPK_MISSING_RAM_LENGTH);
30661854
3067   if (cartslots->gk_guest_slot != -1)
1855   // parse it
1856   char suffix = '\0';
1857   sscanf(length_string, "%u%c", &length, &suffix);
1858   switch(tolower(suffix))
30681859   {
3069      gkguestcart = &cartslots->cartridge[cartslots->gk_guest_slot];
3070      //      printf("accessing cartridge in slot %d\n", slot);
3071      ti99_pcb_t *pcbdef = get_safe_pcb_token(gkguestcart->pcb);
3072      //      printf("address=%lx, offset=%lx\n", pcbdef, offset);
3073      (*pcbdef->read)(gkguestcart->pcb, offset, value);
3074      return;
1860      case 'k': // kilobytes
1861         length *= 1024;
1862         break;
1863
1864      case 'm':
1865         /* megabytes */
1866         length *= 1024*1024;
1867         break;
1868
1869      case '\0':
1870         break;
1871
1872      default:  // failed
1873         throw rpk_exception(RPK_INVALID_RAM_SPEC);
30751874   }
3076   else
1875
1876   // Allocate memory for this resource
1877   contents = malloc(length);
1878   if (contents==NULL) throw rpk_exception(RPK_OUT_OF_MEMORY);
1879
1880   if (VERBOSE>6) LOG("gromport/RPK: Allocating RAM buffer (%d bytes) for socket '%s'\n", length, socketname);
1881
1882   ram_pname = NULL;
1883
1884   // That's it for pure RAM. Now check whether the RAM is "persistent", i.e. NVRAM.
1885   // In that case we must load it from the NVRAM directory.
1886   // The file name is given in the RPK file; the subdirectory is the system name.
1887   ram_type = xml_get_attribute_string(ram_resource_node, "type", NULL);
1888   if (ram_type != NULL)
30771889   {
3078      if (get_gk_switch(device, 1)==GK_OFF) return;
3079      if (get_gk_switch(device, 4)==GK_BANK1)
1890      if (strcmp(ram_type, "persistent")==0)
30801891      {
3081         *value = (gromkrackerbox->ram_ptr)[offset+0x10000 - 0x6000];
3082      }
3083      else
3084      {
3085         if (get_gk_switch(device, 4)==GK_BANK2)
1892         // Get the file name (required if persistent)
1893         ram_filename = xml_get_attribute_string(ram_resource_node, "file", NULL);
1894         if (ram_filename==NULL)
30861895         {
3087            *value = (gromkrackerbox->ram_ptr)[offset+0x12000 - 0x6000];
1896            free(contents);
1897            throw rpk_exception(RPK_INVALID_RAM_SPEC, "<ram type='persistent'> must have a 'file' attribute");
30881898         }
3089         else
3090         {
3091            // Write protection on; auto-bank
3092            if (gromkrackerbox->ram_page==0)
3093            {
3094               *value = (gromkrackerbox->ram_ptr)[offset+0x10000 - 0x6000];
3095            }
3096            else
3097            {
3098               *value = (gromkrackerbox->ram_ptr)[offset+0x12000 - 0x6000];
3099            }
3100         }
1899         astring ram_pathname(system_name, PATH_SEPARATOR, ram_filename);
1900         ram_pname = core_strdup(ram_pathname.cstr());
1901         // load, and fill rest with 00
1902         if (VERBOSE>6) LOG("gromport/RPK: Loading NVRAM contents from '%s'\n", ram_pname);
1903         image_battery_load_by_name(options, ram_pname, contents, length, 0x00);
31011904      }
31021905   }
1906
1907   // Create a socket instance
1908   return new rpk_socket(socketname, length, contents, ram_pname);
31031909}
31041910
3105/*
3106    Writes to the RAM space of the GRAM Kracker.
3107*/
3108static WRITE8_DEVICE_HANDLER( ti99_cart_gk_w )
1911/*-------------------------------------------------
1912    rpk_open - open a RPK file
1913    options - parameters from the settings; we need it only for the NVRAM directory
1914    system_name - name of the driver (also just for NVRAM handling)
1915-------------------------------------------------*/
1916
1917rpk* rpk_reader::open(emu_options &options, const char *filename, const char *system_name)
31091918{
3110   // gk_slot is the slot where the GK module is plugged in
3111   // gk_guest_slot is the slot where the cartridge is plugged in
3112   cartridge_t *gromkrackerbox, *gkguestcart;
3113   ti99_multicart_state *cartslots = get_safe_token(device);
1919   zip_error ziperr;
31141920
3115   gromkrackerbox = &cartslots->cartridge[cartslots->gk_slot];
1921   const zip_file_header *header;
1922   const char *pcb_type;
1923   const char *id;
1924   const char *uses_name;
1925   const char *resource_name;
31161926
3117   if (cartslots->gk_guest_slot != -1)
1927   zip_file* zipfile;
1928
1929   char *layout_text = NULL;
1930   xml_data_node *layout_xml;
1931   xml_data_node *romset_node;
1932   xml_data_node *configuration_node;
1933   xml_data_node *resources_node;
1934   xml_data_node *resource_node;
1935   xml_data_node *socket_node;
1936   xml_data_node *pcb_node;
1937
1938   rpk_socket *newsock;
1939
1940   int i;
1941
1942   rpk *newrpk = new rpk(options, system_name);
1943
1944   try
31181945   {
3119      gkguestcart = &cartslots->cartridge[cartslots->gk_guest_slot];
3120      ti99_pcb_t *pcbdef = get_safe_pcb_token(gkguestcart->pcb);
3121         (*pcbdef->write)(gkguestcart->pcb, offset, data);
3122   }
3123   else
3124   {
3125      if (get_gk_switch(device, 1)==GK_OFF)
3126         return;
1946      /* open the ZIP file */
1947      ziperr = zip_file_open(filename, &zipfile);
1948      if (ziperr != ZIPERR_NONE) throw rpk_exception(RPK_NOT_ZIP_FORMAT);
31271949
3128      if (get_gk_switch(device, 4)==GK_BANK1)
1950      /* find the layout.xml file */
1951      header = find_file(zipfile, "layout.xml", 0);
1952      if (header == NULL) throw rpk_exception(RPK_MISSING_LAYOUT);
1953
1954      /* reserve space for the layout file contents (+1 for the termination) */
1955      layout_text = (char*)malloc(header->uncompressed_length + 1);
1956      if (layout_text == NULL) throw rpk_exception(RPK_OUT_OF_MEMORY);
1957
1958      /* uncompress the layout text */
1959      ziperr = zip_file_decompress(zipfile, layout_text, header->uncompressed_length);
1960      if (ziperr != ZIPERR_NONE) throw rpk_exception(RPK_ZIP_ERROR);
1961
1962      layout_text[header->uncompressed_length] = '\0';  // Null-terminate
1963
1964      /* parse the layout text */
1965      layout_xml = xml_string_read(layout_text, NULL);
1966      if (layout_xml == NULL) throw rpk_exception(RPK_XML_ERROR);
1967
1968      // Now we work within the XML tree
1969
1970      // romset is the root node
1971      romset_node = xml_get_sibling(layout_xml->child, "romset");
1972      if (romset_node==NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "document element must be <romset>");
1973
1974      // resources is a child of romset
1975      resources_node = xml_get_sibling(romset_node->child, "resources");
1976      if (resources_node==NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<romset> must have a <resources> child");
1977
1978      // configuration is a child of romset; we're actually interested in ...
1979      configuration_node = xml_get_sibling(romset_node->child, "configuration");
1980      if (configuration_node==NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<romset> must have a <configuration> child");
1981
1982      // ... pcb, which is a child of configuration
1983      pcb_node = xml_get_sibling(configuration_node->child, "pcb");
1984      if (pcb_node==NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<configuration> must have a <pcb> child");
1985
1986      // We'll try to find the PCB type on the provided type list.
1987      pcb_type = xml_get_attribute_string(pcb_node, "type", NULL);
1988      if (pcb_type==NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<pcb> must have a 'type' attribute");
1989      if (VERBOSE>6) LOG("gromport/RPK: Cartridge says it has PCB type '%s'\n", pcb_type);
1990
1991      i=0;
1992      do
31291993      {
3130         (gromkrackerbox->ram_ptr)[offset + 0x10000 - 0x6000] = data;
3131      }
3132      else
3133      {
3134         if (get_gk_switch(device, 4)==GK_BANK2)
1994         if (strcmp(pcb_type, m_types[i].name)==0)
31351995         {
3136            (gromkrackerbox->ram_ptr)[offset + 0x12000 - 0x6000] = data;
1996            newrpk->m_type = m_types[i].id;
1997            break;
31371998         }
3138         else
1999         i++;
2000      } while (m_types[i].id != 0);
2001
2002      if (m_types[i].id==0) throw rpk_exception(RPK_UNKNOWN_PCB_TYPE);
2003
2004      // Find the sockets and load their respective resource
2005      for (socket_node = pcb_node->child;  socket_node != NULL; socket_node = socket_node->next)
2006      {
2007         if (strcmp(socket_node->name, "socket")!=0) throw rpk_exception(RPK_INVALID_LAYOUT, "<pcb> element has only <socket> children");
2008         id = xml_get_attribute_string(socket_node, "id", NULL);
2009         if (id == NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<socket> must have an 'id' attribute");
2010         uses_name = xml_get_attribute_string(socket_node, "uses", NULL);
2011         if (uses_name == NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "<socket> must have a 'uses' attribute");
2012
2013         bool found = false;
2014         // Locate the resource node
2015         for (resource_node = resources_node->child; resource_node != NULL; resource_node = resource_node->next)
31392016         {
3140            // Write protection on
3141            // This is handled like in Extended Basic
3142            gromkrackerbox->ram_page = (offset & 2)>>1;
2017            resource_name = xml_get_attribute_string(resource_node, "id", NULL);
2018            if (resource_name == NULL) throw rpk_exception(RPK_INVALID_LAYOUT, "resource node must have an 'id' attribute");
2019
2020            if (strcmp(resource_name, uses_name)==0)
2021            {
2022               // found it
2023               if (strcmp(resource_node->name, "rom")==0)
2024               {
2025                  newsock = load_rom_resource(zipfile, resource_node, id);
2026                  newrpk->add_socket(id, newsock);
2027               }
2028               else
2029               {
2030                  if (strcmp(resource_node->name, "ram")==0)
2031                  {
2032                     newsock = load_ram_resource(options, resource_node, id, system_name);
2033                     newrpk->add_socket(id, newsock);
2034                  }
2035                  else throw rpk_exception(RPK_INVALID_LAYOUT, "resource node must be <rom> or <ram>");
2036               }
2037               found = true;
2038            }
31432039         }
2040         if (!found) throw rpk_exception(RPK_INVALID_RESOURCE_REF, uses_name);
31442041      }
31452042   }
3146}
3147
3148/*
3149    Read cartridge mapper CRU interface (Guest in GRAM Kracker)
3150*/
3151static READ8Z_DEVICE_HANDLER( ti99_cart_cru_gk_rz )
3152{
3153   ti99_multicart_state *cartslots = get_safe_token(device);
3154   cartridge_t *gkguestcart;
3155
3156   if (cartslots->gk_guest_slot != -1)
2043   catch (rpk_exception &exp)
31572044   {
3158      gkguestcart = &cartslots->cartridge[cartslots->gk_guest_slot];
3159      ti99_pcb_t *pcbdef = get_safe_pcb_token(gkguestcart->pcb);
3160      if (pcbdef->cruread != NULL)
3161         (*pcbdef->cruread)(gkguestcart->pcb, offset, value);
3162   }
3163}
2045      newrpk->close();
2046      if (zipfile != NULL)      zip_file_close(zipfile);
2047      if (layout_text != NULL)   free(layout_text);
31642048
3165/*
3166    Write cartridge mapper CRU interface (Guest in GRAM Kracker)
3167*/
3168static WRITE8_DEVICE_HANDLER( ti99_cart_cru_gk_w )
3169{
3170   ti99_multicart_state *cartslots = get_safe_token(device);
3171   cartridge_t *gkguestcart;
3172
3173   if (cartslots->gk_guest_slot != -1)
3174   {
3175      gkguestcart = &cartslots->cartridge[cartslots->gk_guest_slot];
3176      ti99_pcb_t *pcbdef = get_safe_pcb_token(gkguestcart->pcb);
3177      if (pcbdef->cruwrite != NULL)
3178         (*pcbdef->cruwrite)(gkguestcart->pcb, offset, data);
2049      // rethrow the exception
2050      throw exp;
31792051   }
3180}
31812052
3182/*****************************************************************************/
3183#define TI99_CARTRIDGE_SLOT(p)  MCFG_MULTICARTSLOT_ADD(p) \
3184   MCFG_MULTICARTSLOT_EXTENSION_LIST("rpk") \
3185   MCFG_MULTICARTSLOT_PCBTYPE(0, "none", TI99_CARTRIDGE_PCB_NONE) \
3186   MCFG_MULTICARTSLOT_PCBTYPE(1, "standard", TI99_CARTRIDGE_PCB_STD) \
3187   MCFG_MULTICARTSLOT_PCBTYPE(2, "paged", TI99_CARTRIDGE_PCB_PAGED) \
3188   MCFG_MULTICARTSLOT_PCBTYPE(3, "minimem", TI99_CARTRIDGE_PCB_MINIMEM) \
3189   MCFG_MULTICARTSLOT_PCBTYPE(4, "super", TI99_CARTRIDGE_PCB_SUPER) \
3190   MCFG_MULTICARTSLOT_PCBTYPE(5, "mbx", TI99_CARTRIDGE_PCB_MBX) \
3191   MCFG_MULTICARTSLOT_PCBTYPE(6, "paged379i", TI99_CARTRIDGE_PCB_PAGED379I) \
3192   MCFG_MULTICARTSLOT_PCBTYPE(7, "pagedcru", TI99_CARTRIDGE_PCB_PAGEDCRU) \
3193   MCFG_MULTICARTSLOT_PCBTYPE(8, "gramkracker", TI99_CARTRIDGE_PCB_GRAMKRACKER) \
3194   MCFG_MULTICARTSLOT_START(ti99_cartridge) \
3195   MCFG_MULTICARTSLOT_LOAD(ti99_cartridge) \
3196   MCFG_MULTICARTSLOT_UNLOAD(ti99_cartridge)
2053   if (zipfile != NULL)      zip_file_close(zipfile);
2054   if (layout_text != NULL)   free(layout_text);
31972055
3198static MACHINE_CONFIG_FRAGMENT(ti99_multicart)
3199   TI99_CARTRIDGE_SLOT("cartridge1")
3200   TI99_CARTRIDGE_SLOT("cartridge2")
3201   TI99_CARTRIDGE_SLOT("cartridge3")
3202   TI99_CARTRIDGE_SLOT("cartridge4")
3203MACHINE_CONFIG_END
3204
3205static const char DEVTEMPLATE_SOURCE[] = __FILE__;
3206
3207#define DEVTEMPLATE_ID(p,s)             p##ti99_multicart##s
3208#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
3209#define DEVTEMPLATE_NAME                "TI99 Multi-Cartridge Extender"
3210#define DEVTEMPLATE_FAMILY              "Multi-cartridge system"
3211#include "devtempl.h"
3212
3213DEFINE_LEGACY_DEVICE(TI99_MULTICART, ti99_multicart);
2056   return newrpk;
2057}
src/mess/machine/ti99/ti99_hd.c
r14513r14514
77    The plain device is a subdevice ("drive") of this device, so we
88    get names like "mfmhd0:drive"
99
10    Michael Zapf, April 2010
10    Michael Zapf
11    April 2010
12    February 2012: Rewritten as class
1113
1214**************************************************************************/
1315
1416#include "emu.h"
1517#include "formats/imageutl.h"
1618#include "harddisk.h"
17#include "imagedev/harddriv.h"
1819#include "machine/smc92x4.h"
1920
2021#include "ti99_hd.h"
2122
22typedef struct _mfmhd
23{
24   int current_cylinder;
25   int current_head;
26   int seeking;
27   int status;
28   int id_index; /* position in track for seeking the sector; counts the sector number */
29} mfmhd_state, idehd_state;
23#define TI99HD_BLOCKNOTFOUND -1
3024
31INLINE mfmhd_state *get_safe_token(device_t *device)
25#define LOG logerror
26#define VERBOSE 0
27
28#define GAP1 16
29#define GAP2 8
30#define GAP3 15
31#define GAP4 340
32#define SYNC 13
33
34mfm_harddisk_device::mfm_harddisk_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
35: device_t(mconfig, TI99_MFMHD, "MFM Harddisk", tag, owner, clock)
3236{
33   assert(device != NULL);
34   assert(device->type() == MFMHD || device->type() == IDEHD);
37}
3538
36   return (mfmhd_state *)downcast<legacy_device_base *>(device)->token();
39ide_harddisk_device::ide_harddisk_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
40: device_t(mconfig, TI99_MFMHD, "IDE Harddisk", tag, owner, clock)
41{
3742}
3843
39#if 0
40static void dump_contents(UINT8 *buffer, int length)
44/*
45    Calculate the ident byte from the cylinder. The specification does not
46    define idents beyond cylinder 1023, but formatting programs seem to
47    continue with 0xfd for cylinders between 1024 and 2047.
48*/
49UINT8 mfm_harddisk_device::cylinder_to_ident(int cylinder)
4150{
42   for (int i=0; i < length; i+=16)
43   {
44      for (int j=0; j < 16; j+=2)
45      {
46         printf("%02x%02x ", buffer[i+j], buffer[i+j+1]);
47      }
48      printf("\n");
49   }
51   if (cylinder < 256) return 0xfe;
52   if (cylinder < 512) return 0xff;
53   if (cylinder < 768) return 0xfc;
54   if (cylinder < 1024) return 0xfd;
55   return 0xfd;
5056}
51#endif
5257
5358/*
5459    Returns the linear sector number, given the CHS data.
5560*/
56static int harddisk_chs_to_lba(hard_disk_file *hdfile, int cylinder, int head, int sector, UINT32 *lba)
61bool mfm_harddisk_device::harddisk_chs_to_lba(hard_disk_file *hdfile, int cylinder, int head, int sector, UINT32 *lba)
5762{
5863   const hard_disk_info *info;
5964
r14513r14514
6469      if (   (cylinder >= info->cylinders) ||
6570            (head >= info->heads) ||
6671            (sector >= info->sectors))
67      return FALSE;
72      return false;
6873
6974      *lba = (cylinder * info->heads + head)
7075            * info->sectors
7176            + sector;
72      return TRUE;
77      return true;
7378   }
74   return FALSE;
79   return false;
7580}
7681
7782/* Accessor functions */
78void ti99_mfm_harddisk_read_sector(device_t *harddisk, int cylinder, int head, int sector, UINT8 **buf, int *sector_length)
83void mfm_harddisk_device::read_sector(int cylinder, int head, int sector, UINT8 *buf)
7984{
8085   UINT32 lba;
81   mfmhd_state *hd = get_safe_token(harddisk);
82   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
83   hard_disk_file *file = drive->get_hard_disk_file();
86   if (VERBOSE>5) LOG("ti99_hd: read_sector(%d, %d, %d)\n", cylinder, head, sector);
87   hard_disk_file *file = m_drive->get_hard_disk_file();
8488
85   *sector_length = 256;
86   *buf = (UINT8 *)malloc(*sector_length);
87
88   if (cylinder != hd->current_cylinder)
89   if (cylinder != m_current_cylinder)
8990   {
9091      return;
9192   }
9293
9394   if (file==NULL)
9495   {
95      hd->status &= ~MFMHD_READY;
96      m_status &= ~MFMHD_READY;
9697      return;
9798   }
9899
99100   if (!harddisk_chs_to_lba(file, cylinder, head, sector, &lba))
100101   {
101      hd->status &= ~MFMHD_READY;
102      m_status &= ~MFMHD_READY;
102103      return;
103104   }
104105
105   if (!hard_disk_read(file, lba, *buf))
106   if (!hard_disk_read(file, lba, buf))
106107   {
107      hd->status &= ~MFMHD_READY;
108      m_status &= ~MFMHD_READY;
108109      return;
109110   }
110111   /* printf("ti99_hd read sector  c=%04d h=%02d s=%02d\n", cylinder, head, sector); */
111   hd->status |= MFMHD_READY;
112   m_status |= MFMHD_READY;
112113}
113114
114void ti99_mfm_harddisk_write_sector(device_t *harddisk, int cylinder, int head, int sector, UINT8 *buf, int sector_length)
115void mfm_harddisk_device::write_sector(int cylinder, int head, int sector, UINT8 *buf)
115116{
116117   UINT32 lba;
117   mfmhd_state *hd = get_safe_token(harddisk);
118   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
119   hard_disk_file *file = drive->get_hard_disk_file();
118   if (VERBOSE>5) LOG("ti99_hd: write_sector(%d, %d, %d)\n", cylinder, head, sector);
119   hard_disk_file *file = m_drive->get_hard_disk_file();
120120
121121   if (file==NULL)
122122   {
123      hd->status &= ~MFMHD_READY;
123      m_status &= ~MFMHD_READY;
124124      return;
125125   }
126126
127127   if (!harddisk_chs_to_lba(file, cylinder, head, sector, &lba))
128128   {
129      hd->status &= ~MFMHD_READY;
130      hd->status |= MFMHD_WRFAULT;
129      m_status &= ~MFMHD_READY;
130      m_status |= MFMHD_WRFAULT;
131131      return;
132132   }
133133
134134   if (!hard_disk_write(file, lba, buf))
135135   {
136      hd->status &= ~MFMHD_READY;
137      hd->status |= MFMHD_WRFAULT;
136      m_status &= ~MFMHD_READY;
137      m_status |= MFMHD_WRFAULT;
138138      return;
139139   }
140140
141   hd->status |= MFMHD_READY;
141   m_status |= MFMHD_READY;
142142}
143143
144#define TI99HD_BLOCKNOTFOUND -1
145
146144/*
147145    Searches a block containing number * byte, starting at the given
148146    position. Returns the position of the first byte of the block.
149147*/
150static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number)
148int mfm_harddisk_device::find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number)
151149{
152150   int i = start;
153151   size_t current = number;
r14513r14514
170168      return TI99HD_BLOCKNOTFOUND;
171169}
172170
171int mfm_harddisk_device::get_track_length()
172{
173   int count;
174   int size;
175   hard_disk_file *file = m_drive->get_hard_disk_file();
176   const hard_disk_info *info;
177
178   if (file==NULL) return 0;
179   info = hard_disk_get_info(file);
180
181   count = info->sectors;
182   size = info->sectorbytes/128;
183   return GAP1 + count*(SYNC+12+GAP2+SYNC+size*128+GAP3)+GAP4;
184}
185
173186/*
174187    Reads a complete track. We have to rebuild the gaps. This is basically
175188    done in the same way as in the SDF format in ti99_dsk.
r14513r14514
177190    WARNING: This function is untested! We need to create a suitable
178191    application program for the TI which makes use of it.
179192*/
180void ti99_mfm_harddisk_read_track(device_t *harddisk, int head, UINT8 **pbuffer, int *data_count)
193void mfm_harddisk_device::read_track(int head, UINT8 *trackdata)
181194{
182   mfmhd_state *hd = get_safe_token(harddisk);
183   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
184
185195   /* We assume an interleave of 3 for 32 sectors. */
186196   int step = 3;
187197   UINT32 lba;
188198   int i;
189199
190   int gap1 = 16;
191   int gap2 = 8;
192   int gap3 = 15;
193   int gap4 = 340;
194   int sync = 13;
195   int count;
196200   int size;
197201   int position = 0;
198202   int sector;
199203   int crc;
204   int count;
200205
201206   const hard_disk_info *info;
202   hard_disk_file *file = drive->get_hard_disk_file();
207   hard_disk_file *file = m_drive->get_hard_disk_file();
203208
204209   if (file==NULL)
205210   {
206      hd->status &= ~MFMHD_READY;
211      m_status &= ~MFMHD_READY;
207212      return;
208213   }
209214
r14513r14514
212217   count = info->sectors;
213218   size = info->sectorbytes/128;
214219
215   *data_count = gap1 + count*(sync+12+gap2+sync+size*128+gap3)+gap4;
216
217   UINT8 *trackdata = (UINT8*)malloc(*data_count);
218
219220   /* Write lead-in. */
220   memset(trackdata, 0x4e, gap1);
221   memset(trackdata, 0x4e, GAP1);
221222
222   position += gap1;
223   for (i=0; i < info->sectors; i++)
223   position += GAP1;
224   for (i=0; i < count; i++)
224225   {
225226      sector = (i*step) % info->sectors;
226      memset(&trackdata[position], 0x00, sync);
227      position += 10;
227      memset(&trackdata[position], 0x00, SYNC);
228      position += SYNC;
228229
229230      /* Write sync */
230231      trackdata[position++] = 0xa1;
231232
232233      /* Write IDAM */
233      trackdata[position++] = cylinder_to_ident(hd->current_cylinder);
234      trackdata[position++] = hd->current_cylinder;
234      trackdata[position++] = cylinder_to_ident(m_current_cylinder);
235      trackdata[position++] = m_current_cylinder;
235236      trackdata[position++] = head;
236237      trackdata[position++] = sector;
237238      trackdata[position++] = (size==1)? 0x00 : (0x01 << (size-1));
r14513r14514
242243      trackdata[position++] = crc & 0xff;
243244
244245      /* Write Gap2 */
245      memset(&trackdata[position], 0x4e, gap2);
246      position += gap2;
246      memset(&trackdata[position], 0x4e, GAP2);
247      position += GAP2;
247248
248249      /* Write sync */
249      memset(&trackdata[position], 0x00, sync);
250      position += sync;
250      memset(&trackdata[position], 0x00, SYNC);
251      position += SYNC;
251252      trackdata[position++] = 0xa1;
252253
253254      /* Write DAM */
r14513r14514
255256
256257      /* Locate the sector content in the image and load it. */
257258
258      if (!harddisk_chs_to_lba(file, hd->current_cylinder, head, sector, &lba))
259      if (!harddisk_chs_to_lba(file, m_current_cylinder, head, sector, &lba))
259260      {
260         hd->status &= ~MFMHD_READY;
261         free(trackdata);
261         m_status &= ~MFMHD_READY;
262262         return;
263263      }
264264
265265      if (!hard_disk_read(file, lba, &trackdata[position]))
266266      {
267         hd->status &= ~MFMHD_READY;
268         free(trackdata);
267         m_status &= ~MFMHD_READY;
269268         return;
270269      }
271270
272      position += size;
271      position += info->sectorbytes;
273272
274273      /* Set CRC16. Includes the address mark. */
275274      crc = ccitt_crc16(0xffff, &trackdata[position-size-1], size+1);
r14513r14514
281280      position += 3;
282281
283282      /* Write Gap3 */
284      memset(&trackdata[position], 0x4e, gap3);
285      position += gap3;
283      memset(&trackdata[position], 0x4e, GAP3);
284      position += GAP3;
286285   }
287286   /* Write Gap 4 */
288   memset(&trackdata[position], 0x4e, gap4);
289   position += gap4;
287   memset(&trackdata[position], 0x4e, GAP4);
288   position += GAP4;
290289
291   hd->status |= MFMHD_READY;
292
293   *pbuffer = trackdata;
294
295   // Remember to free the buffer! Except when an error has occurred,
296   // the function returns a newly allocated buffer.
297   // Errors are indicated by READY=FALSE.
290   m_status |= MFMHD_READY;
298291}
299292
300293/*
301294    Writes a track to the image. We need to isolate the sector contents.
302295    This is basically done in the same way as in the SDF format in ti99_dsk.
303296*/
304void ti99_mfm_harddisk_write_track(device_t *harddisk, int head, UINT8 *track_image, int data_count)
297void mfm_harddisk_device::write_track(int head, UINT8 *track_image, int data_count)
305298{
306299   int current_pos = 0;
307   int found;
300   bool found;
308301   // UINT8 wident;
309302   UINT8 whead = 0, wsector = 0;
310303   // UINT8 wsize;
r14513r14514
312305   int state;
313306
314307   /* Only search in the first 100 bytes for the start. */
315   int gap1 = 16;
316   int sync = 13;
317308
318309   UINT32 lba;
319   mfmhd_state *hd = get_safe_token(harddisk);
320   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
321   hard_disk_file *file = drive->get_hard_disk_file();
310   hard_disk_file *file = m_drive->get_hard_disk_file();
322311
323   /* printf("ti99_hd write track c=%d h=%d\n", hd->current_cylinder, head); */
312   /* printf("ti99_hd write track c=%d h=%d\n", m_current_cylinder, head); */
324313
325314   if (file==NULL)
326315   {
327      hd->status &= ~MFMHD_READY;
316      m_status &= ~MFMHD_READY;
328317      return;
329318   }
330319
331   current_pos = find_block(track_image, 0, 100, 0x4e, gap1);
320   current_pos = find_block(track_image, 0, 100, 0x4e, GAP1);
332321
333322   // In case of defect formats, we continue as far as possible. This
334323   // may lead to sectors not being written. */
335324   if (current_pos==TI99HD_BLOCKNOTFOUND)
336325   {
337      logerror("ti99_hd error: write track: Cannot find gap1 for cylinder %d, head %d.\n", hd->current_cylinder, head);
326      logerror("ti99_hd error: write track: Cannot find GAP1 for cylinder %d, head %d.\n", m_current_cylinder, head);
338327      /* What now? The track data are illegal, so we refuse to continue. */
339      hd->status |= MFMHD_WRFAULT;
328      m_status |= MFMHD_WRFAULT;
340329      return;
341330   }
342331
343332   /* Get behind GAP1 */
344   current_pos += gap1;
345   found = FALSE;
333   current_pos += GAP1;
334   found = false;
346335
347336   while (current_pos < data_count)
348337   {
349338      /* We must find the address block to determine the sector. */
350      int new_pos = find_block(track_image, current_pos, data_count, 0x00, sync);
339      int new_pos = find_block(track_image, current_pos, data_count, 0x00, SYNC);
351340      if (new_pos==TI99HD_BLOCKNOTFOUND)
352341      {
353342         /* Forget about the rest; we're done. */
354343         if (found) break;  /* we were already successful, so all ok */
355         logerror("ti99_hd error: write track: Cannot find sync for track %d, head %d.\n", hd->current_cylinder, head);
356         hd->status |= MFMHD_WRFAULT;
344         logerror("ti99_hd error: write track: Cannot find sync for track %d, head %d.\n", m_current_cylinder, head);
345         m_status |= MFMHD_WRFAULT;
357346         return;
358347      }
359      found = TRUE;
348      found = true;
360349
361      new_pos = new_pos + sync; /* skip sync bytes */
350      new_pos = new_pos + SYNC; /* skip sync bytes */
362351
363352      if (track_image[new_pos]==0xa1)
364353      {
r14513r14514
370359         wsector = track_image[current_pos+3];
371360         // wsize = track_image[current_pos+4];  // unused
372361
373         if (wcyl == hd->current_cylinder && whead == head)
362         if (wcyl == m_current_cylinder && whead == head)
374363         {
375364            if (!harddisk_chs_to_lba(file, wcyl, whead, wsector, &lba))
376365            {
377               hd->status |= MFMHD_WRFAULT;
366               m_status |= MFMHD_WRFAULT;
378367               return;
379368            }
380369
381370            /* Skip to the sector content. */
382            new_pos = find_block(track_image, current_pos, data_count, 0x00, sync);
383            current_pos = new_pos + sync;
371            new_pos = find_block(track_image, current_pos, data_count, 0x00, SYNC);
372            current_pos = new_pos + SYNC;
384373            if (track_image[current_pos]==0xa1)
385374            {
386375               current_pos += 2;
r14513r14514
388377               if (state==0)
389378               {
390379                  logerror("ti99_hd error: write track: Write error during formatting cylinder %d, head %d\n", wcyl, whead);
391                  hd->status |= MFMHD_WRFAULT;
380                  m_status |= MFMHD_WRFAULT;
392381                  return;
393382               }
394383               current_pos = current_pos+256+2; /* Skip contents and CRC */
r14513r14514
396385            else
397386            {
398387               logerror("ti99_hd error: write track: Cannot find DAM for cylinder %d, head %d, sector %d.\n", wcyl, whead, wsector);
399               hd->status |= MFMHD_WRFAULT;
388               m_status |= MFMHD_WRFAULT;
400389               return;
401390            }
402391         }
403392         else
404393         {
405            logerror("ti99_hd error: write track: Cylinder/head mismatch. Drive is on cyl=%d, head=%d, track data say cyl=%d, head=%d\n", hd->current_cylinder, head, wcyl, whead);
406            hd->status |= MFMHD_WRFAULT;
394            logerror("ti99_hd error: write track: Cylinder/head mismatch. Drive is on cyl=%d, head=%d, track data say cyl=%d, head=%d\n", m_current_cylinder, head, wcyl, whead);
395            m_status |= MFMHD_WRFAULT;
407396         }
408397      }
409398      else
410399      {
411         logerror("ti99_hd error: write track: Invalid track image for cyl=%d, head=%d. Cannot find any IDAM in track data.\n",  hd->current_cylinder, head);
412         hd->status |= MFMHD_WRFAULT;
400         logerror("ti99_hd error: write track: Invalid track image for cyl=%d, head=%d. Cannot find any IDAM in track data.\n",  m_current_cylinder, head);
401         m_status |= MFMHD_WRFAULT;
413402         return;
414403      }
415404   }
416405}
417406
418UINT8 ti99_mfm_harddisk_status(device_t *harddisk)
407UINT8 mfm_harddisk_device::get_status()
419408{
420409   UINT8 status = 0;
421   mfmhd_state *hd = get_safe_token(harddisk);
422   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
423   hard_disk_file *file = drive->get_hard_disk_file();
424
410   hard_disk_file *file = m_drive->get_hard_disk_file();
425411   if (file!=NULL)
426412      status |= MFMHD_READY;
427413
428   if (hd->current_cylinder==0)
414   if (m_current_cylinder==0)
429415      status |= MFMHD_TRACK00;
430416
431   if (!hd->seeking)
417   if (!m_seeking)
432418      status |= MFMHD_SEEKCOMP;
433419
434   if (hd->id_index == 0)
420   if (m_id_index == 0)
435421      status |= MFMHD_INDEX;
436422
437   hd->status = status;
423   m_status = status;
424   if (VERBOSE>5) LOG("ti99_hd: request status reply = %02x\n", status);
438425   return status;
439426}
440427
441void ti99_mfm_harddisk_seek(device_t *harddisk, int direction)
428void mfm_harddisk_device::seek(int direction)
442429{
443   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
444   mfmhd_state *hd = get_safe_token(harddisk);
445430   const hard_disk_info *info;
446   hard_disk_file *file = drive->get_hard_disk_file();
431   hard_disk_file *file = m_drive->get_hard_disk_file();
447432
448433   if (file==NULL)   return;
449434
450435   info = hard_disk_get_info(file);
451436
452   hd->seeking = TRUE;
437   m_seeking = true;
453438
454439   if (direction<0)
455440   {
456      if (hd->current_cylinder>0)
457         hd->current_cylinder--;
441      if (m_current_cylinder>0)
442         m_current_cylinder--;
458443   }
459444   else
460445   {
461      if (hd->current_cylinder < info->cylinders)
462         hd->current_cylinder++;
446      if (m_current_cylinder < info->cylinders)
447         m_current_cylinder++;
463448   }
464449
465   hd->seeking = FALSE;
450   // TODO: Requires timer
451
452   m_seeking = false;
466453}
467454
468void ti99_mfm_harddisk_get_next_id(device_t *harddisk, int head, chrn_id_hd *id)
455void mfm_harddisk_device::get_next_id(int head, chrn_id_hd *id)
469456{
470   harddisk_image_device *drive = downcast<harddisk_image_device *>(harddisk->subdevice("drive"));
471   mfmhd_state *hd = get_safe_token(harddisk);
472457   const hard_disk_info *info;
473458   hard_disk_file *file;
474459   int interleave = 3;
475460
476   file = drive->get_hard_disk_file();
461   file = m_drive->get_hard_disk_file();
462
477463   if (file==NULL)
478464   {
479      hd->status &= ~MFMHD_READY;
465      m_status &= ~MFMHD_READY;
480466      return;
481467   }
482468
483469   info = hard_disk_get_info(file);
484470
485   hd->current_head = head;
471   m_current_head = head;
486472
487473   /* TODO: implement an interleave suitable for the number of sectors in the track. */
488   hd->id_index = (hd->id_index + interleave) % info->sectors;
474   m_id_index = (m_id_index + interleave) % info->sectors;
489475
490476   /* build a new info block. */
491   id->C = hd->current_cylinder;
492   id->H = hd->current_head;
493   id->R = hd->id_index;
477   id->C = m_current_cylinder;
478   id->H = m_current_head;
479   id->R = m_id_index;
494480   id->N = 1;
495   id->data_id = hd->id_index;
481   id->data_id = m_id_index;
496482   id->flags = 0;
497483}
498484
499static DEVICE_START( mfmhd )
485void mfm_harddisk_device::device_start()
500486{
487   m_current_cylinder = 0;
488   m_current_head = 0;
501489}
502490
503static DEVICE_START( idehd )
491void mfm_harddisk_device::device_reset()
504492{
493   m_drive = static_cast<harddisk_image_device *>(subdevice("drive"));
494   m_seeking = false;
495   m_status = 0;
496   m_id_index = 0;
505497}
506498
507
508499MACHINE_CONFIG_FRAGMENT( mfmhd )
509500   MCFG_HARDDISK_ADD("drive")
510501MACHINE_CONFIG_END
r14513r14514
513504   MCFG_HARDDISK_ADD("drive")
514505MACHINE_CONFIG_END
515506
516static const char DEVTEMPLATE_SOURCE[] = __FILE__;
517507
518#define DEVTEMPLATE_ID(p,s)             p##mfmhd##s
519#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_MACHINE_CONFIG
520#define DEVTEMPLATE_NAME                "TI99 MFM Hard disk"
521#define DEVTEMPLATE_FAMILY              "Hard disks"
522#define DEVTEMPLATE_VERSION      "1.0"
523#define DEVTEMPLATE_CREDITS      "Copyright MESS Team"
524#include "devtempl.h"
508machine_config_constructor mfm_harddisk_device::device_mconfig_additions() const
509{
510   return MACHINE_CONFIG_NAME( mfmhd );
511}
525512
526#undef DEVTEMPLATE_ID
527#undef DEVTEMPLATE_FEATURES
528#undef DEVTEMPLATE_NAME
513machine_config_constructor ide_harddisk_device::device_mconfig_additions() const
514{
515   return MACHINE_CONFIG_NAME( idehd );
516}
529517
530#define DEVTEMPLATE_ID(p,s)             p##idehd##s
531#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_MACHINE_CONFIG
532#define DEVTEMPLATE_NAME                "TI99 IDE Hard disk"
533#include "devtempl.h"
534
535DEFINE_LEGACY_DEVICE(MFMHD, mfmhd);
536DEFINE_LEGACY_DEVICE(IDEHD, idehd);
518const device_type TI99_MFMHD = &device_creator<mfm_harddisk_device>;
519const device_type TI99_IDEHD = &device_creator<ide_harddisk_device>;
src/mess/machine/ti99/hsgpl.c
r14513r14514
1/*
1/****************************************************************************
2
23    SNUG HSGPL card emulation.
3    Raphael Nabet, 2003.
44
5    Rewritten as device
6    Michael Zapf, October 2010
7*/
5    The HSGPL card is a card for the peripheral expansion box which simulates
6    16 banks of 64 KiB GROM space, using flash memory to keep data persistent.
87
9#include "emu.h"
10#include "emuopts.h"
11#include "peribox.h"
12#include "hsgpl.h"
13#include "machine/at29040a.h"
14#include "ti99defs.h"
8    Main usage:
9    - store cartridges in the card for quick selection
10    - replace the console GROMs, thus making it possible to patch or enhance the
11      console operating system
12    - provide necessary ROMs for the SGCPU (aka TI-99/4P)
1513
16/*
14    (So for using the ti99_4p emulation, you MUST have a properly set up HSGPL
15    card. The best way to get this done is to prepare this card within the
16    ti99_4ev emulation, then copy the contents to the ti99_4p nvram directory.)
17
18    The card does not contain real GROM circuits, and accordingly, we do not
19    use the grom emulation. The logic on the board, mainly contained in the
20    MACH chip, simulates GROM behavior. With the possibility to upload data
21    into the simulated GROMs, we can also enjoy GRAM behavior, although real
22    GRAMs are not known to have become available ever.
23
24    Contents:
25
1726    Supports 16 banks of 8 GROMs (8kbytes each) with 16 associated banks of
1827    32kbytes (8kbytes*4) of module ROM, 2 banks of 8 GRAMs with 2 associated
1928    banks of 32 kbytes of RAM, and 512kbytes of DSR.  Roms are implemented with
r14513r14514
4150    >F RAMENA  RAMENA  Enable RAM6000 instead of ROM6000 in banks 0 and 1
4251
4352
44    Direct access ports for all memory areas (the original manual gives
45    >9880->989C and >9C80->9C9C for ROM6000, but this is incorrect):
53    Direct access ports for all memory areas (the original manual says
54    >9880->989C and >9C80->9C9C for ROM6000, but this is clearly incorrect):
4655
4756    Module bank Read    Write   Read ROM6000        Write ROM6000
4857                GROM    GROM
r14513r14514
9099    console will not start up at all. At least GROMs 0, 1, and 2 must remain
91100    active.
92101    The technical specifications are not clear enough at this point.
93*/
94102
95#define CRU_BASE 0x1B00
103    FIXME: Re-flashing the at29040 chips is only possible when they are deleted
104    before. The files must be removed from the nvram directory. May also be a bug
105    of the loader program DSRLDR3.
96106
97#define RAMSIZE       0x020000
98#define GRAMSIZE      0x020000
99#define FEEPROM_SIZE   0x80000
100#define FLROMSIZE   4*(FEEPROM_SIZE+2) + 1
107    Raphael Nabet, 2003.
101108
102#define NVVERSION 0
109    Michael Zapf
110    October 2010: Rewritten as device
111    February 2012: Rewritten as class
103112
104typedef ti99_pebcard_config hsgpl_config;
113*****************************************************************************/
105114
106typedef struct _hsgpl_state
107{
108   device_t   *dsr, *rom6, *groma, *gromb;
109   UINT8   *ram6;
110   UINT8   *gram;
111   UINT8   *flashrom;
115#include "hsgpl.h"
112116
113   int    current_grom_port;
117#define CRU_BASE 0x1B00
114118
115   int      dsr_enabled;
116   int      gram_enabled;
117   int      bank_enabled;
118   int      dsr_page;
119   int      card_enabled;
120   int      write_enabled;
121   int      supercart_enabled;
122   int      led_on;
123   int      mbx_enabled;
124   int      ram_enabled;
119#define VERBOSE 1
120#define LOG logerror
125121
126   int      flash_mode;
122#define RAMSIZE       0x020000
123#define GRAMSIZE      0x020000
127124
128   int      current_bank;
125#define DSR_EEPROM "u9_dsr"
126#define GROM_B_EEPROM "u4_grom"
127#define   GROM_A_EEPROM "u1_grom"
128#define ROM6_EEPROM "u6_rom6"
129129
130   /* GROM emulation */
131   int      raddr_LSB, waddr_LSB;
132   int      grom_address;
133
134} hsgpl_state;
135
136INLINE hsgpl_state *get_safe_token(device_t *device)
130snug_high_speed_gpl_device::snug_high_speed_gpl_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
131: ti_expansion_card_device(mconfig, TI99_HSGPL, "SNUG High-speed GPL card", tag, owner, clock)
137132{
138   assert(device != NULL);
139   assert(device->type() == HSGPL);
133   m_shortname = "ti99_hsgpl";
134}
140135
141   return (hsgpl_state *)downcast<legacy_device_base *>(device)->token();
136/*
137   Read hsgpl CRU interface. None here.
138*/
139void snug_high_speed_gpl_device::crureadz(offs_t offset, UINT8 *value)
140{
141   return;
142142}
143143
144144/*
145145    Write hsgpl CRU interface
146146*/
147static WRITE8_DEVICE_HANDLER( cru_w )
147void snug_high_speed_gpl_device::cruwrite(offs_t offset, UINT8 data)
148148{
149   hsgpl_state *card = get_safe_token(device);
150
151149   if ((offset & 0xff00)==CRU_BASE)
152150   {
153151      int bit = (offset >> 1) & 0x0f;
154152      switch (bit)
155153      {
156154      case 0:
157         card->dsr_enabled = data;
158         logerror("hsgpl: Set dsr_enabled=%x\n", data);
155         m_dsr_enabled = (data != 0);
156         if (VERBOSE>5) LOG("hsgpl: Set dsr_enabled=%x\n", data);
159157         break;
160158      case 1:
161         card->gram_enabled = data;
162         logerror("hsgpl: Set gram_enabled=%x\n", data);
159         m_gram_enabled = (data != 0);
160         if (VERBOSE>5) LOG("hsgpl: Set gram_enabled=%x\n", data);
163161         break;
164162      case 2:
165         card->bank_enabled = data;
166         logerror("hsgpl: Set bank_enabled=%x\n", data);
163         m_bank_enabled = (data != 0);
164         if (VERBOSE>5) LOG("hsgpl: Set bank_enabled=%x\n", data);
167165         break;
168166      case 3:
169167      case 4:
r14513r14514
172170      case 7:
173171      case 8:
174172         if (data)
175            card->dsr_page |= (1 << (bit-3));
173            m_dsr_page |= (1 << (bit-3));
176174         else
177            card->dsr_page &= ~(1 << (bit-3));
178         logerror("hsgpl: Set dsr_page=%d\n", card->dsr_page);
175            m_dsr_page &= ~(1 << (bit-3));
176         if (VERBOSE>5) LOG("hsgpl: Set dsr_page=%d\n", m_dsr_page);
179177         break;
180178      case 9:
181         card->card_enabled = data;
182         logerror("hsgpl: Set card_enabled=%x\n", data);
179         m_card_enabled = data;
180         if (VERBOSE>5) LOG("hsgpl: Set card_enabled=%x\n", data);
183181         break;
184182      case 10:
185         card->write_enabled = data;
186         logerror("hsgpl: Set write_enabled=%x\n", data);
183         m_write_enabled = data;
184         if (VERBOSE>5) LOG("hsgpl: Set write_enabled=%x\n", data);
187185         break;
188186      case 11:
189         card->supercart_enabled = data;
190         logerror("hsgpl: Set supercart_enabled=%x\n", data);
187         m_supercart_enabled = data;
188         if (VERBOSE>5) LOG("hsgpl: Set supercart_enabled=%x\n", data);
191189         break;
192190      case 12:
193         card->led_on = data;
194         logerror("hsgpl: Set led_on=%x\n", data);
191         m_led_on = data;
192         if (VERBOSE>5) LOG("hsgpl: Set led_on=%x\n", data);
195193         break;
196194      case 13:
197195         break;
198196      case 14:
199         card->mbx_enabled = data;
200         logerror("hsgpl: Set mbx_enabled=%x\n", data);
197         m_mbx_enabled = data;
198         if (VERBOSE>5) LOG("hsgpl: Set mbx_enabled=%x\n", data);
201199         break;
202200      case 15:
203         card->ram_enabled = data;
204         logerror("hsgpl: Set ram_enabled=%x\n", data);
201         m_ram_enabled = data;
202         if (VERBOSE>5) LOG("hsgpl: Set ram_enabled=%x\n", data);
205203         break;
206204      }
207205   }
208206}
209207
210208/*
211    GROM read. This is somewhat different to the original TI GROMs, so we
212    partly reimplement it here. Anyway, the card does not use GROMs but makes
213    use of a special decoder chip (MACH) which emulates the GROM functions.
209    Memory read
214210*/
215static READ8Z_DEVICE_HANDLER ( hsgpl_grom_rz )
211READ8Z_MEMBER(snug_high_speed_gpl_device::readz)
216212{
213   if ((offset & 0x7e000)==0x74000)
214   {
215      dsrspace_readz(space, offset & 0xffff, value, mem_mask);
216   }
217
218   if ((offset & 0x7e000)==0x76000)
219   {
220      cartspace_readz(space, offset & 0xffff, value, mem_mask);
221   }
222
223   // 1001 1wbb bbbb bba0
224   if ((offset & 0x7fc01)==0x79800)
225   {
226      grom_readz(space, offset & 0xffff, value, mem_mask);
227   }
228}
229
230/*
231    Memory write
232*/
233WRITE8_MEMBER(snug_high_speed_gpl_device::write)
234{
235   if ((offset & 0x7e000)==0x76000)
236   {
237      cartspace_write(space, offset & 0xffff, data, mem_mask);
238   }
239
240   // 1001 1wbb bbbb bba0
241   if ((offset & 0x7fc01)==0x79c00)
242   {
243      grom_write(space, offset & 0xffff, data, mem_mask);
244   }
245}
246
247/*
248    Specific read access: dsrspace
249*/
250void snug_high_speed_gpl_device::dsrspace_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask)
251{
252   if (m_dsr_enabled)
253   {
254      *value = m_dsr_eeprom->read(space, (offset & 0x1fff) | (m_dsr_page<<13), mem_mask);
255      if (VERBOSE>7) LOG("hsgpl: read dsr %04x[%02x] -> %02x\n", offset, m_dsr_page, *value);
256   }
257}
258
259/*
260    Specific read access: cartspace
261*/
262void snug_high_speed_gpl_device::cartspace_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask)
263{
264   if (!m_card_enabled || m_flash_mode)
265   {
266      if (VERBOSE>6) LOG("hsgpl cart read ignored (enable=%02x)\n", m_card_enabled);
267      return;
268   }
269
270   if ((m_current_grom_port < 2) && (m_ram_enabled))
271   {
272      *value = m_ram6_memory[(offset & 0x1fff) | (m_current_bank<<13) | (m_current_grom_port<<15)];
273      if (VERBOSE>7) LOG("hsgpl cart ram read %04x -> %02x\n", offset, *value);
274      return;
275   }
276
277   if (m_current_grom_port < 16)
278   {
279      *value = m_rom6_eeprom->read(space, (offset & 0x1fff) | (m_current_bank<<13) | (m_current_grom_port<<15), mem_mask);
280      if (VERBOSE>7) LOG("hsgpl cart read %04x -> %02x\n", offset, *value);
281   }
282   else
283   {
284      if (m_current_grom_port==32 || m_current_grom_port==33)
285      {
286         *value = m_ram6_memory[(offset & 0x1fff) | (m_current_bank<<13) | ((m_current_grom_port-32)<<15)];
287      }
288      else
289      {
290         if (VERBOSE>1) LOG("unknown 0x6000 port\n");
291      }
292   }
293}
294
295/*
296    Specific read access: grom
297    Although we have a complete emulation of a GROM circuit, we need to re-implement
298    it here - which is indeed closer to reality, since the real HSGPL also
299    emulates GROM instead of using proper ones.
300*/
301void snug_high_speed_gpl_device::grom_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask)
302{
217303   int port;
218   hsgpl_state *card = get_safe_token(device);
219304   //activedevice_adjust_icount(-4);
220305
221306   // 1001 10bb bbbb bba0
222   port = card->current_grom_port = (offset & 0x3fc) >> 2;
307   port = m_current_grom_port = (offset & 0x3fc) >> 2;
223308
224309   if (offset & 2)
225310   {   // Read GPL address. This must be available even when the rest
226311      // of the card is offline (card_enabled=0).
227      card->waddr_LSB = FALSE;
312      m_waddr_LSB = false;
228313
229      if (card->raddr_LSB)
314      if (m_raddr_LSB)
230315      {
231         *value = ((card->grom_address + 1) & 0xff);
232         card->raddr_LSB = FALSE;
316         *value = ((m_grom_address + 1) & 0xff);
317         m_raddr_LSB = false;
233318      }
234319      else
235320      {
236         *value = ((card->grom_address + 1) >> 8) & 0xff;
237         card->raddr_LSB = TRUE;
321         *value = ((m_grom_address + 1) >> 8) & 0xff;
322         m_raddr_LSB = true;
238323      }
239324   }
240325   else
241326   {   /* read GPL data */
242327      // It is not clear what effect a CRDENA=0 really has.
243328      // At least GROMs 0-2 must remain visible, or the console will lock up.
244      if (card->card_enabled || card->grom_address < 0x6000)
329      if (m_card_enabled || m_grom_address < 0x6000)
245330      {
246         if ((port < 2) && (card->gram_enabled))
331         if ((port < 2) && (m_gram_enabled))
247332         {
248            *value = card->gram[card->grom_address + 0x10000*port];
333            *value = m_gram_memory[m_grom_address | (port<<16)];
249334         }
250335         else
251336         {
252337            if (port < 8)
253338            {
254               if (!card->flash_mode)
339               if (!m_flash_mode)
255340               {
256                  *value = at29c040a_r(card->groma, card->grom_address + 0x10000*port);
341                  *value = m_grom_a_eeprom->read(space, m_grom_address | (port<<16), mem_mask);
257342               }
258343            }
259344            else
260345            {
261346               if (port < 16)
262347               {
263                  *value = at29c040a_r(card->gromb, card->grom_address + 0x10000*(port-8));
348                  *value = m_grom_b_eeprom->read(space, m_grom_address | ((port-8)<<16), mem_mask);
264349               }
265350               else
266351               {
267352                  if (port < 24)
268353                  {
269                     *value = at29c040a_r(card->dsr, card->grom_address + 0x10000*(port-16));
354                     // 9840-985c
355                     // DSR banks 0-63 (8 KiB per bank, 8 banks per port)
356                     *value = m_dsr_eeprom->read(space, m_grom_address | ((port-16)<<16), mem_mask);
270357                  }
271358                  else
272359                  {
273360                     if (port < 32)
274361                     {
275                        /* The HSGPL manual says 32-47, but this is incorrect */
276                        *value = at29c040a_r(card->rom6, card->grom_address + 0x10000*(port-24));
362                        // 9860-987c (ports 24-31)
363                        // Each ROM6 is available as 4 (sub)banks (switchable via 6000, 6002, 6004, 6006)
364                        // Accordingly, each port has two complete sets
365                        *value = m_rom6_eeprom->read(space, m_grom_address | ((port-24)<<16), mem_mask);
277366                     }
278367                     else
279368                     {
369                        // 9880, 9884
280370                        if (port==32 || port==33)
281371                        {
282                           *value = card->gram[card->grom_address + 0x10000*(port-32)];
372                           *value = m_gram_memory[m_grom_address | ((port-32)<<16)];
283373                        }
284374                        else
285375                        {
286376                           if (port==48 || port==49)
287377                           {
288                              *value = card->ram6[card->grom_address];
378//                                      *value = m_ram6_memory[m_grom_address];
379                              *value = m_ram6_memory[m_grom_address | ((port-48)<<16)];
289380                           }
290381                           else
291382                           {
292                              logerror("hsgpl: Attempt to read from undefined port 0x%0x; ignored.\n", port);
383                              if (VERBOSE>2) LOG("hsgpl: Attempt to read from undefined port 0x%0x; ignored.\n", port);
293384                           }
294385                        }
295386                     }
r14513r14514
300391      }
301392      // The address auto-increment should be done even when the card is
302393      // offline
303      card->grom_address++;
304      card->raddr_LSB = card->waddr_LSB = FALSE;
394      m_grom_address++;
395      m_raddr_LSB = m_waddr_LSB = false;
305396   }
306397}
307398
308399/*
309    GROM write
400    Specific write access: cartspace
310401*/
311static WRITE8_DEVICE_HANDLER ( hsgpl_grom_w )
402void snug_high_speed_gpl_device::cartspace_write(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask)
312403{
404   if (m_card_enabled || m_flash_mode)
405   {
406      if (VERBOSE>2) LOG("hsgpl cart write ignored: card_enabled=%02x\n", m_card_enabled);
407      return;
408   }
409
410   if (VERBOSE>7) LOG("hsgpl cart write %04x -> %02x\n", offset, data);
411
412   if (m_bank_enabled)
413   {
414      m_current_bank = (offset>>1) & 3;
415      if (VERBOSE>7) LOG("hsgpl cart select bank %02x\n", m_current_bank);
416      return;      /* right??? */
417   }
418
419   if ((m_mbx_enabled) && (offset==0x6ffe))
420   {   /* MBX: mapper at 0x6ffe */
421      m_current_bank = data & 0x03;
422      return;
423   }
424
425   // MBX: RAM in 0x6c00-0x6ffd (it is unclear whether the MBX RAM area is
426   // enabled/disabled by the wriena bit).  I guess RAM is unpaged, but it is
427   // not implemented
428   if ((m_write_enabled) || ((m_mbx_enabled) && ((offset & 0xfc00)==0x6c00)))
429   {
430      if ((m_current_grom_port < 2) && (m_ram_enabled))
431      {
432         m_ram6_memory[(offset & 0x1fff) | (m_current_bank<<13) | (m_current_grom_port<<15) ] = data;
433      }
434      else
435      {   // keep in mind that these lines are also reached for port < 2
436         // and !ram_enabled
437         if (m_current_grom_port < 16)
438         {
439         // feeprom is normally written to using GPL ports, and I don't know
440         // writing through >6000 page is enabled
441/*
442            at29c040a_w(feeprom_rom6, 1 + 2*offset + 0x2000*hsgpl.cur_bank + 0x8000*port, data);
443            at29c040a_w(feeprom_rom6, 2*offset + 0x2000*hsgpl.cur_bank + 0x8000*port, data >> 8);
444*/
445         }
446         else
447         {
448            if (m_current_grom_port==32 || m_current_grom_port==33)
449            {
450               m_ram6_memory[(offset & 0x1fff) | (m_current_bank<<13) | ((m_current_grom_port-32)<<15)] = data;
451            }
452            else
453            {
454               if (VERBOSE>1) LOG("unknown 0x6000 port\n");
455            }
456         }
457      }
458   }
459}
460
461/*
462    Specific write access: grom_write
463*/
464void snug_high_speed_gpl_device::grom_write(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask)
465{
313466   int port;
314   hsgpl_state *card = get_safe_token(device);
315467
316468   //activedevice_adjust_icount(-4);
317469
318470   // 1001 11bb bbbb bba0
319   port = card->current_grom_port = (offset & 0x3fc) >> 2;
471   port = m_current_grom_port = (offset & 0x3fc) >> 2;
320472
321473   if (offset & 2)
322474   {   // Write GPL address. This must be available even when the rest
323475      // of the card is offline (card_enabled=0).
324      card->raddr_LSB = FALSE;
476      m_raddr_LSB = false;
325477
326      if (card->waddr_LSB)
478      if (m_waddr_LSB)
327479      {
328         card->grom_address = (card->grom_address & 0xFF00) | data;
329         card->waddr_LSB = FALSE;
480         m_grom_address = (m_grom_address & 0xFF00) | data;
481         m_waddr_LSB = false;
330482      }
331483      else
332484      {
333         card->grom_address = (data << 8) | (card->grom_address & 0xFF);
334         card->waddr_LSB = TRUE;
485         m_grom_address = (data << 8) | (m_grom_address & 0xFF);
486         m_waddr_LSB = true;
335487      }
336488   }
337489   else
338490   {
339491      // It is not clear what effect a CRDENA=0 really has.
340492      // At least GROMs 0-2 must remain visible, or the console will lock up.
341      if (card->card_enabled || card->grom_address < 0x6000)
493      if (m_card_enabled || m_grom_address < 0x6000)
342494      {
343495         /* write GPL data */
344         if (card->write_enabled)
496         if (m_write_enabled)
345497         {
346            if ((port < 2) && (card->gram_enabled))
498            if ((port < 2) && (m_gram_enabled))
347499            {
348               card->gram[card->grom_address + 0x10000*port] = data;
500               m_gram_memory[m_grom_address | (port<<16)] = data;
349501            }
350502            else
351503            {
352504               if (port < 8)
353505               {
354                  at29c040a_w(card->groma, card->grom_address + 0x10000*port, data);
506                  m_grom_a_eeprom->write(space, m_grom_address | (port<<16), data, mem_mask);
355507               }
356508               else
357509               {
358510                  if (port < 16)
359511                  {
360                     at29c040a_w(card->gromb, card->grom_address + 0x10000*(port-8), data);
512                     m_grom_b_eeprom->write(space, m_grom_address | ((port-8)<<16), data, mem_mask);
361513                  }
362514                  else
363515                  {
364516                     if (port < 24)
365517                     {
366                        at29c040a_w(card->dsr, card->grom_address + 0x10000*(port-16), data);
518                        m_dsr_eeprom->write(space, m_grom_address | ((port-16)<<16), data, mem_mask);
367519                     }
368520                     else
369521                     {
370522                        if (port < 32)
371523                        {
372                           /* The HSGPL manual says 32-47, but this is incorrect */
373                           at29c040a_w(card->rom6, card->grom_address + 0x10000*(port-24), data);
524                           m_rom6_eeprom->write(space, m_grom_address | ((port-24)<<16), data, mem_mask);
374525                        }
375526                        else
376527                        {
377528                           if (port==32 || port==33)
378529                           {
379                              card->gram[card->grom_address + 0x10000*(port-32)] = data;
530                              m_gram_memory[m_grom_address | ((port-32)<<16)] = data;
380531                           }
381532                           else
382533                           {
383534                              if (port==48 || port==49)
384535                              {
385                                 card->ram6[card->grom_address] = data;
536//                                          m_ram6_memory[m_grom_address] = data;
537                                 m_ram6_memory[m_grom_address | ((port-48)<<16)] = data;
386538                              }
387539                              else
388540                              {
389                                 logerror("hsgpl: Attempt to write to undefined port; ignored.\n");
541                                 if (VERBOSE>1) LOG("hsgpl: Attempt to write to undefined port; ignored.\n");
390542                              }
391543                           }
392544                        }
r14513r14514
398550      }
399551      // The address auto-increment should be done even when the card is
400552      // offline
401      card->grom_address++;
402      card->raddr_LSB = card->waddr_LSB = FALSE;
553      m_grom_address++;
554      m_raddr_LSB = m_waddr_LSB = false;
403555   }
404556}
405557
406static READ8Z_DEVICE_HANDLER ( hsgpl_dsrspace_rz )
407{
408   hsgpl_state *card = get_safe_token(device);
409   if (card->dsr_enabled)
410   {
411      *value = at29c040a_r(card->dsr, (offset & 0x1fff) + 0x2000 * card->dsr_page);
412//      logerror("hsgpl: read dsr %04x[%02x] -> %02x\n", offset, card->dsr_page, *value);
413   }
414}
415558
416static READ8Z_DEVICE_HANDLER ( hsgpl_cartspace_rz )
559void snug_high_speed_gpl_device::device_start()
417560{
418   hsgpl_state *card = get_safe_token(device);
419
420   if (!card->card_enabled || card->flash_mode)
421   {
422//      logerror("hsgpl cart read ignored (enable=%02x)\n", card->card_enabled);
423      return;
424   }
425
426   int port = card->current_grom_port;
427
428   if ((port < 2) && (card->ram_enabled))
429   {
430      *value = card->ram6[(offset & 0x1fff) + 0x2000*card->current_bank + 0x8000*port];
431//      logerror("hsgpl cart ram read %04x -> %02x\n", offset, *value);
432      return;
433   }
434
435   if (port < 16)
436   {
437      *value = at29c040a_r(card->rom6, (offset & 0x1fff) + 0x2000*card->current_bank + 0x8000*port);
438//      logerror("hsgpl cart read %04x -> %02x\n", offset, *value);
439   }
440   else
441   {
442      if (port==32 || port==33)
443      {
444         *value = card->ram6[(offset & 0x1fff) + 0x2000*card->current_bank + 0x8000*(port-32)];
445      }
446      else
447      {
448         logerror("unknown 0x6000 port\n");
449      }
450   }
561   m_ram6_memory = (UINT8*)malloc(RAMSIZE);
562   m_gram_memory = (UINT8*)malloc(GRAMSIZE);
451563}
452564
453static WRITE8_DEVICE_HANDLER ( hsgpl_cartspace_w )
565void snug_high_speed_gpl_device::device_reset()
454566{
455   hsgpl_state *card = get_safe_token(device);
567   if (VERBOSE>5) LOG("hsgpl: reset\n");
568   m_dsr_enabled = false;
569   m_gram_enabled = false;
570   m_bank_enabled = true;   // important, assumed to be enabled by default (won't start up otherwise!)
571   m_dsr_page = 0;
572   m_card_enabled = true;   // important, assumed to be enabled by default
573   m_write_enabled = false;
574   m_supercart_enabled = false;
575   m_led_on = false;
576   m_mbx_enabled = false;
577   m_ram_enabled = false;
578   m_flash_mode = (input_port_read(*this, "HSGPLMODE")==0);
456579
457   if (!card->card_enabled || card->flash_mode)
458   {
459      logerror("hsgpl cart write ignored: card_enabled=%02x\n", card->card_enabled);
460      return;
461   }
580   m_current_grom_port = 0;
581   m_current_bank = 0;
462582
463   int port = card->current_grom_port;
464//  logerror("hsgpl cart write %04x -> %02x\n", offset, data);
465
466   if (card->bank_enabled)
467   {
468      card->current_bank = (offset>>1) & 3;
469//      logerror("hsgpl cart select bank %02x\n", card->current_bank);
470      return;      /* right??? */
471   }
472
473   if ((card->mbx_enabled) && (offset==0x6ffe))
474   {   /* MBX: mapper at 0x6ffe */
475      card->current_bank = data & 0x03;
476      return;
477   }
478
479   // MBX: RAM in 0x6c00-0x6ffd (it is unclear whether the MBX RAM area is
480   // enabled/disabled by the wriena bit).  I guess RAM is unpaged, but it is
481   // not implemented
482   if ((card->write_enabled) || ((card->mbx_enabled) && ((offset & 0xfc00)==0x6c00)))
483   {
484      if ((port < 2) && (card->ram_enabled))
485      {
486         card->ram6[(offset & 0x1fff) + 0x2000*card->current_bank + 0x8000*port ] = data;
487      }
488      else
489      {   // keep in mind that these lines are also reached for port < 2
490         // and !ram_enabled
491         if (port < 16)
492         {
493         // feeprom is normally written to using GPL ports, and I don't know
494         // writing through >6000 page is enabled
495/*
496            at29c040a_w(feeprom_rom6, 1 + 2*offset + 0x2000*hsgpl.cur_bank + 0x8000*port, data);
497            at29c040a_w(feeprom_rom6, 2*offset + 0x2000*hsgpl.cur_bank + 0x8000*port, data >> 8);
498*/
499         }
500         else
501         {
502            if (port==32 || port==33)
503            {
504               card->ram6[(offset & 0x1fff) + 0x2000*card->current_bank + 0x8000*(port-32)] = data;
505            }
506            else
507            {
508               logerror("unknown 0x6000 port\n");
509            }
510         }
511      }
512   }
583   m_waddr_LSB = false;
584   m_raddr_LSB = false;
585   m_grom_address = 0;
513586}
514587
515/*
516    Memory read
517*/
518static READ8Z_DEVICE_HANDLER ( data_rz )
588void snug_high_speed_gpl_device::device_config_complete(void)
519589{
520   if ((offset & 0x7e000)==0x74000)
521   {
522      hsgpl_dsrspace_rz(device, offset & 0xffff, value);
523   }
524
525   if ((offset & 0x7e000)==0x76000)
526   {
527      hsgpl_cartspace_rz(device, offset & 0xffff, value);
528   }
529
530   // 1001 1wbb bbbb bba0
531   if ((offset & 0x7fc01)==0x79800)
532   {
533      hsgpl_grom_rz(device, offset & 0xffff, value);
534   }
590   m_dsr_eeprom = subdevice<at29040a_device>(DSR_EEPROM);
591   m_rom6_eeprom = subdevice<at29040a_device>(ROM6_EEPROM);
592   m_grom_a_eeprom = subdevice<at29040a_device>(GROM_A_EEPROM);
593   m_grom_b_eeprom = subdevice<at29040a_device>(GROM_B_EEPROM);
535594}
536595
537/*
538    Memory write
539*/
540static WRITE8_DEVICE_HANDLER ( data_w )
596void snug_high_speed_gpl_device::device_stop()
541597{
542   if ((offset & 0x7e000)==0x76000)
543   {
544      hsgpl_cartspace_w(device, offset & 0xffff, data);
545   }
546
547   // 1001 1wbb bbbb bba0
548   if ((offset & 0x7fc01)==0x79c00)
549   {
550      hsgpl_grom_w(device, offset & 0xffff, data);
551   }
598   free(m_ram6_memory);
599   free(m_gram_memory);
552600}
553601
554static const ti99_peb_card hsgpl_card =
555{
556   data_rz,
557   data_w,
558   NULL,
559   cru_w,
560   NULL, NULL,   NULL, NULL
561};
602// Flash setting is used to flash an empty HSGPL DSR ROM
603INPUT_PORTS_START( ti99_hsgpl)
604   PORT_START( "HSGPLMODE" )
605   PORT_DIPNAME( 0x01, 0x01, "HSGPL mode" )
606      PORT_DIPSETTING(    0x00, "Flash" )
607      PORT_DIPSETTING(    0x01, "Normal" )
608INPUT_PORTS_END
562609
563static DEVICE_START( hsgpl )
564{
565   logerror("HSGPL start\n");
566   hsgpl_state *card = get_safe_token(device);
567   card->dsr = device->subdevice("u9_dsr");
568   card->rom6 = device->subdevice("u6_rom6");
569   card->groma = device->subdevice("u4_grom");
570   card->gromb = device->subdevice("u1_grom");
610MACHINE_CONFIG_FRAGMENT( ti99_hsgpl )
611   MCFG_AT29040A_ADD( DSR_EEPROM )
612   MCFG_AT29040A_ADD( GROM_B_EEPROM )
613   MCFG_AT29040A_ADD( GROM_A_EEPROM )
614   MCFG_AT29040A_ADD( ROM6_EEPROM )
615MACHINE_CONFIG_END
571616
572   /* We need this for the NVRAM; at this point we cannot query the settings yet. */
573   card->flashrom = (UINT8*)malloc(FLROMSIZE);
574}
575
576static DEVICE_STOP( hsgpl )
617machine_config_constructor snug_high_speed_gpl_device::device_mconfig_additions() const
577618{
578   hsgpl_state *card = get_safe_token(device);
579   logerror("hsgpl: stop\n");
580   free(card->flashrom);
581   if (card->ram6)
582   {
583      free(card->ram6);
584      free(card->gram);
585   }
619   return MACHINE_CONFIG_NAME( ti99_hsgpl );
586620}
587621
588static DEVICE_RESET( hsgpl )
622ioport_constructor snug_high_speed_gpl_device::device_input_ports() const
589623{
590   logerror("hsgpl: reset\n");
591   hsgpl_state *card = get_safe_token(device);
592
593   /* If the card is selected in the menu, register the card */
594   if (input_port_read(device->machine(), "EXTCARD") & (EXT_HSGPL_ON | EXT_HSGPL_FLASH))
595   {
596      device_t *peb = device->owner();
597      int success = mount_card(peb, device, &hsgpl_card, get_pebcard_config(device)->slot);
598      if (!success)
599      {
600         logerror("hsgpl: Failed to mount.\n");
601         return;
602      }
603
604      card->grom_address = 0;
605      card->raddr_LSB = 0;
606      card->waddr_LSB = 0;
607      card->current_grom_port = 0;
608      card->current_bank = 0;
609
610      card->dsr_enabled = FALSE;
611      card->gram_enabled = FALSE;
612      card->bank_enabled = TRUE;      // important, assumed to be enabled by default
613      card->dsr_page = 0;
614      card->card_enabled = TRUE;      // important, assumed to be enabled by default
615      card->write_enabled = FALSE;
616      card->supercart_enabled = FALSE;
617      card->led_on = FALSE;
618      card->mbx_enabled = FALSE;
619      card->ram_enabled = FALSE;
620
621      card->flash_mode = (input_port_read(device->machine(), "EXTCARD") & EXT_HSGPL_FLASH);
622      if (card->flash_mode) logerror("hsgpl: flash mode\n");
623      else logerror("hsgpl: full mode\n");
624
625      if (card->ram6==NULL)
626      {
627         card->ram6 = (UINT8*)malloc(RAMSIZE);
628         card->gram = (UINT8*)malloc(GRAMSIZE);
629      }
630   }
624   return INPUT_PORTS_NAME(ti99_hsgpl);
631625}
632/*
633static DEVICE_NVRAM( hsgpl )
634{
635    // Called between START and RESET
636    hsgpl_state *card = get_safe_token(device);
637    astring hsname(device->machine().system().name, PATH_SEPARATOR, "hsgpl.nv");
638    file_error filerr;
639626
640    if (read_or_write==0)
641    {
642        logerror("hsgpl: device nvram load %s\n", hsname.cstr());
643
644        emu_file nvfile(device->machine().options().nvram_directory(), OPEN_FLAG_READ);
645        filerr = nvfile.open(hsname.cstr());
646        if (filerr != FILERR_NONE)
647        {
648            logerror("hsgpl: Could not restore NVRAM\n");
649            return;
650        }
651
652        if (nvfile.read(card->flashrom, FLROMSIZE) != FLROMSIZE)
653        {
654            logerror("hsgpl: Error loading NVRAM; unexpected EOF\n");
655            return;
656        }
657
658        if (card->flashrom[0] != NVVERSION)
659        {
660            logerror("hsgpl: Wrong NVRAM image version: %d\n", card->flashrom[0]);
661            return;
662        }
663    }
664    else
665    {
666        logerror("hsgpl: device nvram save %s\n", hsname.cstr());
667        // Check dirty flags
668        if (at29c040a_is_dirty(card->dsr) ||
669            at29c040a_is_dirty(card->rom6) ||
670            at29c040a_is_dirty(card->groma) ||
671            at29c040a_is_dirty(card->gromb))
672        {
673            card->flashrom[0] = NVVERSION;
674
675            emu_file nvfile(device->machine().options().nvram_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS);
676            filerr = nvfile.open(hsname.cstr());
677            if (filerr != FILERR_NONE)
678            {
679                logerror("hsgpl: Could not save NVRAM\n");
680            }
681            else
682            {
683                if (nvfile.write(card->flashrom, FLROMSIZE) != FLROMSIZE)
684                {
685                    logerror("hsgpl: Error while saving contents of NVRAM.\n");
686                }
687            }
688            return;
689        }
690        else
691        {
692            logerror("hsgpl: No changes on card, leaving nvram file unchanged.\n");
693        }
694    }
695}
696*/
697/*
698    Get the pointer to the memory data from the HSGPL card. Called by the FEEPROM.
699*/
700static UINT8 *get_mem_ptr(device_t *device)
701{
702   hsgpl_state *card = get_safe_token(device);
703   return card->flashrom;
704}
705
706MACHINE_CONFIG_FRAGMENT( hsgpl )
707   MCFG_AT29C040_ADD_P( "u9_dsr",  get_mem_ptr, 0x000001)
708   MCFG_AT29C040_ADD_P( "u4_grom", get_mem_ptr, 0x080003)
709   MCFG_AT29C040_ADD_P( "u1_grom", get_mem_ptr, 0x100005)
710   MCFG_AT29C040_ADD_P( "u6_rom6", get_mem_ptr, 0x180007)
711MACHINE_CONFIG_END
712
713static const char DEVTEMPLATE_SOURCE[] = __FILE__;
714
715#define DEVTEMPLATE_ID(p,s)             p##hsgpl##s
716#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
717#define DEVTEMPLATE_NAME                "SNUG High-Speed GPL card"
718#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
719#include "devtempl.h"
720
721DEFINE_LEGACY_DEVICE( HSGPL, hsgpl );
627const device_type TI99_HSGPL = &device_creator<snug_high_speed_gpl_device>;
src/mess/machine/ti99/gromport.h
r14513r14514
11/*********************************************************************
22
33    gromport.h
4    See grompoprt.c for documentation
45
5    TI99 family cartridge management
6    Michael Zapf
67
8    February 2012: Rewritten as class
9
710*********************************************************************/
811
912#ifndef __TI99CART_H__
1013#define __TI99CART_H__
1114
15#include "emu.h"
1216#include "ti99defs.h"
17#include "grom.h"
18#include "image.h"
1319
14typedef struct _ti99_multicart_config
20extern const device_type GROMPORT;
21extern const device_type TICARTRIDGE;
22extern const device_type TICARTPCBSTD;
23extern const device_type TICARTPCBPAG;
24extern const device_type TICARTPCBMIN;
25extern const device_type TICARTPCBSUP;
26extern const device_type TICARTPCBMBX;
27extern const device_type TICARTPCBPGI;
28extern const device_type TICARTPCBPGC;
29extern const device_type TICARTPCBGRK;
30
31/* We set the number of slots to 4, although we may have up to 16. From a
32   logical point of view we could have 256, but the operating system only checks
33   the first 16 banks. */
34#define NUMBER_OF_CARTRIDGE_SLOTS 4
35
36#define GROMPORT_CONFIG(name) \
37   const gromport_config(name) =
38
39typedef struct _gromport_config
1540{
16   write_line_device_func   ready;
41   devcb_write_line   ready;
42} gromport_config;
1743
18} ti99_multicart_config;
44class cartridge_pcb_device;
45class rpk;
1946
20typedef struct _gromport_callback
47/*****************************************************************************
48    The base class of a cartridge
49    Cartridges are implemented as subdevices of the cartridge slot. They
50    are always present as devices, irrespective of the fact that they are
51    plugged in. Plugging in, however, means that there is a PCB and sizes
52    for ROM or RAM.
53*****************************************************************************/
54class cartridge_device : public bus8z_device, public device_image_interface
2155{
22   devcb_write_line ready;
23} gromport_callback;
56   friend class gromport_device;
57   friend class cartridge_pcb_device;
2458
25void set_gk_switches(device_t *cartsys, int number, int value);
59public:
60   cartridge_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
2661
27/* GROM part */
28WRITE8_DEVICE_HANDLER( gromportg_w );
29READ8Z_DEVICE_HANDLER( gromportg_rz );
62   DECLARE_READ8Z_MEMBER(readz);
63   DECLARE_WRITE8_MEMBER(write);
3064
31/* ROM part */
32WRITE8_DEVICE_HANDLER( gromportr_w );
33READ8Z_DEVICE_HANDLER( gromportr_rz );
65   bool is_available() { return m_pcb != NULL; }
66   bool has_grom();
67   void set_switch(int swtch, int val);   // forwarding to GK
3468
35/* CRU part */
36WRITE8_DEVICE_HANDLER( gromportc_w );
37READ8Z_DEVICE_HANDLER( gromportc_rz );
69   virtual void crureadz(offs_t offset, UINT8 *value);
70   virtual void cruwrite(offs_t offset, UINT8 data);
3871
39DECLARE_LEGACY_DEVICE(TI99_MULTICART, ti99_multicart);
72   // Image handling: implementation of methods which are abstract in the parent
73   bool call_load();
74   void call_unload();
4075
41#define MCFG_TI99_GROMPORT_ADD(_tag, _ready) \
42   MCFG_DEVICE_ADD(_tag, TI99_MULTICART, 0) \
43   MCFG_DEVICE_CONFIG_DATAPTR(ti99_multicart_config, ready, _ready)
76   iodevice_t image_type() const { return IO_CARTSLOT; }
77   bool is_readable()  const         { return true; }
78   bool is_writeable() const         { return false; }
79   bool is_creatable() const         { return false; }
80   bool must_be_loaded() const       { return false; }
81   bool is_reset_on_load() const      { return false; }
82   const char *image_interface() const { return "ti99_cart"; }   // what is this about?
83   const char *file_extensions() const { return "rpk"; }
84   const option_guide *create_option_guide() const { return NULL; }
85   machine_config_constructor device_mconfig_additions() const;
4486
87protected:
88   void device_start(void);
89   void device_stop(void);
90   void device_reset(void);
91   void device_config_complete(void);
92   const rom_entry *device_rom_region() const;
93
94private:
95   void prepare_cartridge();
96   int get_index_from_tagname();
97   UINT8* get_grom_ptr();
98   void set_gk_guest(cartridge_device *guest);
99
100   int m_type;
101
102   // PCB device associated to this cartridge. If NULL, the slot is empty.
103   cartridge_pcb_device *m_pcb;
104
105   // RPK which is associated to this cartridge
106   rpk   *m_rpk;
107};
108
109/*****************************************************************************
110    The class of a cartridge PCB
111*****************************************************************************/
112
113class cartridge_pcb_device : public bus8z_device
114{
115   friend class cartridge_device;
116   friend class gromport_device;
117
118public:
119   cartridge_pcb_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
120
121   virtual DECLARE_READ8Z_MEMBER(readz) =0;
122   virtual DECLARE_WRITE8_MEMBER(write) =0;
123
124   /* Read function for this cartridge, CRU. */
125   virtual void crureadz(offs_t offset, UINT8 *value);
126
127   /* Write function for this cartridge, CRU. */
128   virtual void cruwrite(offs_t offset, UINT8 data);
129
130protected:
131   void device_start(void);
132   DECLARE_READ8Z_MEMBER( gromreadz );
133   DECLARE_WRITE8_MEMBER( gromwrite );
134   void set_gk_guest(int slot);
135
136   /* Points to the (max 5) GROM devices. */
137   ti99_grom_device *m_grom[5];
138
139   // ROM buffer.
140   UINT8 *m_rom_ptr;
141
142   // ROM buffer for the second bank of paged cartridges.
143   UINT8 *m_rom2_ptr;
144
145   // RAM buffer. The persistence service is done by the cartridge system.
146   // The RAM space is a consecutive space; all banks are in one buffer.
147   UINT8 *m_ram_ptr;
148
149   // ROM page.
150   int m_rom_page;
151
152   // RAM page.
153   int m_ram_page;
154
155   // GROM buffer size. Depends of the size of the resource loaded into "grom_socket".
156   int m_grom_size;
157
158   // ROM buffer size. All banks have equal sizes.
159   int m_rom_size;
160
161   // RAM buffer size. All banks have equal sizes.
162   int m_ram_size;
163};
164
165class cartridge_pcb_standard_device : public cartridge_pcb_device
166{
167public:
168   cartridge_pcb_standard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
169   DECLARE_READ8Z_MEMBER(readz);
170   DECLARE_WRITE8_MEMBER(write);
171};
172
173class cartridge_pcb_paged_device : public cartridge_pcb_device
174{
175public:
176   cartridge_pcb_paged_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
177   DECLARE_READ8Z_MEMBER(readz);
178   DECLARE_WRITE8_MEMBER(write);
179};
180
181class cartridge_pcb_minimem_device : public cartridge_pcb_device
182{
183public:
184   cartridge_pcb_minimem_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
185   DECLARE_READ8Z_MEMBER(readz);
186   DECLARE_WRITE8_MEMBER(write);
187};
188
189class cartridge_pcb_superspace_device : public cartridge_pcb_device
190{
191public:
192   cartridge_pcb_superspace_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
193   DECLARE_READ8Z_MEMBER(readz);
194   DECLARE_WRITE8_MEMBER(write);
195   void crureadz(offs_t offset, UINT8 *value);
196   void cruwrite(offs_t offset, UINT8 data);
197};
198
199class cartridge_pcb_mbx_device : public cartridge_pcb_device
200{
201public:
202   cartridge_pcb_mbx_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
203   DECLARE_READ8Z_MEMBER(readz);
204   DECLARE_WRITE8_MEMBER(write);
205};
206
207class cartridge_pcb_paged379i_device : public cartridge_pcb_device
208{
209public:
210   cartridge_pcb_paged379i_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
211   DECLARE_READ8Z_MEMBER(readz);
212   DECLARE_WRITE8_MEMBER(write);
213private:
214   int get_paged379i_bank(int rompage);
215};
216
217class cartridge_pcb_pagedcru_device : public cartridge_pcb_device
218{
219public:
220   cartridge_pcb_pagedcru_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
221   DECLARE_READ8Z_MEMBER(readz);
222   DECLARE_WRITE8_MEMBER(write);
223   void crureadz(offs_t offset, UINT8 *value);
224   void cruwrite(offs_t offset, UINT8 data);
225};
226
227class cartridge_pcb_gramkracker_device : public cartridge_pcb_device
228{
229   friend class cartridge_device;
230
231public:
232   cartridge_pcb_gramkracker_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
233   DECLARE_READ8Z_MEMBER(readz);
234   DECLARE_WRITE8_MEMBER(write);
235   void crureadz(offs_t offset, UINT8 *value);
236   void cruwrite(offs_t offset, UINT8 data);
237
238   void set_gk_guest(cartridge_device *guest);
239   void set_switch(int swtch, int val);
240
241private:
242   // GROM emulation in GK
243   bool   m_waddr_LSB;
244   UINT16   m_grom_address;
245   UINT8*   m_grom_ptr;
246
247   int    m_gk_switch[6];         // Used to cache the switch settings.
248
249   // GRAMKracker guest slot
250   cartridge_device *m_gk_guest;
251};
252
253/*****************************************************************************
254    The overall cartridge system
255*****************************************************************************/
256
257class gromport_device : public bus8z_device
258{
259   friend class cartridge_device;
260
261public:
262   gromport_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
263   DECLARE_READ8Z_MEMBER(readz);
264   DECLARE_WRITE8_MEMBER(write);
265   DECLARE_WRITE_LINE_MEMBER(ready_line);
266
267   void crureadz(offs_t offset, UINT8 *value);
268   void cruwrite(offs_t offset, UINT8 value);
269
270   DECLARE_INPUT_CHANGED_MEMBER( gk_changed );
271
272protected:
273   void device_start(void);
274   void device_stop(void);
275   void device_reset(void);
276   machine_config_constructor device_mconfig_additions() const;
277   ioport_constructor device_input_ports() const;
278
279private:
280   void set_slot(int slot);
281   int get_active_slot(bool changebase, offs_t offset);
282   void change_slot(bool inserted, int index);
283   void set_gk_slot(int slot);
284   void find_gk_guest();
285   void crureadz_gk(offs_t offset, UINT8 *value);
286   void cruwrite_gk(offs_t offset, UINT8 value);
287
288   // Points to all cartridges. This is faster than iterating the list
289   // of subdevices on each memory access.
290   cartridge_device*   m_cartridge[NUMBER_OF_CARTRIDGE_SLOTS];
291
292   // Number of available cartridge positions
293   int    m_numcart;
294
295   // Determines which slot is currently active. This value is changed when
296   // there are accesses to other GROM base addresses.
297   int    m_active_slot;
298
299   // Used in order to enforce a special slot. This value is retrieved
300   // from the dipswitch setting. A value of -1 means automatic, that is,
301   // the grom base switch is used. Values 0 .. max refer to the
302   // respective slot.
303   int    m_fixed_slot;
304
305   // Holds the highest index of a cartridge being plugged in plus one.
306   // If we only have one cartridge inserted, we don't want to get a
307   // selection option, so we just mirror the memory contents.
308   int    m_next_free_slot;
309
310   /* GK support. */
311   int      m_gk_slot;
312
313   /* Ready line */
314   devcb_resolved_write_line m_ready;
315};
316
317typedef struct _pcb_type
318{
319   int id;
320   const char* name;
321} pcb_type;
322
323/*************************************************************************
324    RPK support
325*************************************************************************/
326
327class rpk_socket
328{
329   friend class simple_list<rpk_socket>;
330   friend class rpk;
331
332public:
333   rpk_socket(const char *id, int length, void *contents);
334   rpk_socket(const char *id, int length, void *contents, const char *pathname);
335
336   const char*      id() { return m_id; }
337   int          get_content_length() { return m_length; }
338   void*         get_contents() { return m_contents; }
339   bool         persistent_ram() { return m_pathname != NULL; }
340   const char*      get_pathname() { return m_pathname; }
341   void         cleanup() { if (m_contents != NULL) free(m_contents); }
342
343private:
344   const char      *m_id;
345   UINT32         m_length;
346   rpk_socket      *m_next;
347   void         *m_contents;
348   const char      *m_pathname;
349};
350
351class rpk_reader
352{
353public:
354   rpk_reader(const pcb_type *types)
355   : m_types(types) { };
356
357   rpk             *open(emu_options &options, const char *filename, const char *system_name);
358
359private:
360   const zip_file_header*   find_file(zip_file *zip, const char *filename, UINT32 crc);
361   rpk_socket*            load_rom_resource(zip_file* zip, xml_data_node* rom_resource_node, const char* socketname);
362   rpk_socket*          load_ram_resource(emu_options &options, xml_data_node* ram_resource_node, const char* socketname, const char* system_name);
363   const pcb_type*         m_types;
364};
365
366class rpk
367{
368   friend class rpk_reader;
369public:
370   rpk(emu_options& options, const char* sysname);
371   ~rpk();
372
373   int         get_type(void) { return m_type; }
374   void*      get_contents_of_socket(const char *socket_name);
375   int         get_resource_length(const char *socket_name);
376   void      close();
377
378private:
379   emu_options&         m_options;      // need this to find the path to the nvram files
380   int                  m_type;
381   const char*            m_system_name;   // need this to find the path to the nvram files
382   tagged_list<rpk_socket>   m_sockets;
383
384   void add_socket(const char* id, rpk_socket *newsock);
385};
386
387enum _rpk_open_error
388{
389   RPK_OK,
390   RPK_NOT_ZIP_FORMAT,
391   RPK_CORRUPT,
392   RPK_OUT_OF_MEMORY,
393   RPK_XML_ERROR,
394   RPK_INVALID_FILE_REF,
395   RPK_ZIP_ERROR,
396   RPK_MISSING_RAM_LENGTH,
397   RPK_INVALID_RAM_SPEC,
398   RPK_UNKNOWN_RESOURCE_TYPE,
399   RPK_INVALID_RESOURCE_REF,
400   RPK_INVALID_LAYOUT,
401   RPK_MISSING_LAYOUT,
402   RPK_NO_PCB_OR_RESOURCES,
403   RPK_UNKNOWN_PCB_TYPE
404};
405typedef enum _rpk_open_error rpk_open_error;
406
407static const char error_text[15][30] =
408{
409   "No error",
410   "Not a RPK (zip) file",
411   "Module definition corrupt",
412   "Out of memory",
413   "XML format error",
414   "Invalid file reference",
415   "Zip file error",
416   "Missing RAM length",
417   "Invalid RAM specification",
418   "Unknown resource type",
419   "Invalid resource reference",
420   "layout.xml not valid",
421   "Missing layout",
422   "No pcb or resource found",
423   "Unknown pcb type"
424};
425
426class rpk_exception
427{
428public:
429   rpk_exception(rpk_open_error value): m_err(value), m_detail(NULL) { };
430   rpk_exception(rpk_open_error value, const char* detail): m_err(value), m_detail(detail) { };
431
432   const char* to_string()
433   {
434      if (m_detail==NULL) return error_text[(int)m_err];
435      astring errormsg(error_text[(int)m_err], ": ", m_detail);
436      return core_strdup(errormsg.cstr());
437   }
438
439private:
440   rpk_open_error m_err;
441   const char* m_detail;
442};
443
444/*************************************************************************
445    Macros
446*************************************************************************/
447
448#define MCFG_TI99_GROMPORT_ADD(_tag, _intf) \
449   MCFG_DEVICE_ADD(_tag, GROMPORT, 0)   \
450   MCFG_DEVICE_CONFIG(_intf)
451
45452#endif /* __TI99CART_H__ */
src/mess/machine/ti99/ti99_hd.h
r14513r14514
1/****************************************************************************
2
3    Hard disk support
4    See ti99_hd.c for documentation
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
112#ifndef __TI99_HD__
213#define __TI99_HD__
314
r14513r14514
213#include "emu.h"
3#include "machine/smc92x4.h"
14#include "imagedev/harddriv.h"
415
r14513r14514
1021
1122#define IDEHD_0 "idehd0"
1223
13/* typedef struct _mfmhd_config mfmhd_config;
14struct _mfmhd_config
24extern const device_type TI99_MFMHD;
25extern const device_type TI99_IDEHD;
26
27/*
28    Needed to adapt to higher cylinder numbers. Floppies do not have such
29    high numbers.
30*/
31typedef struct chrn_id_hd
1532{
16    device_start_func           device_start;
17    device_image_load_func          device_load;
18    device_image_unload_func        device_unload;
19}; */
33   UINT16 C;
34   UINT8 H;
35   UINT8 R;
36   UINT8 N;
37   int data_id;         // id for read/write data command
38   unsigned long flags;
39} chrn_id_hd;
2040
21/* Accessor functions */
22void ti99_mfm_harddisk_read_sector(device_t *harddisk, int cylinder, int head, int sector, UINT8 **buf, int *sector_length);
23void ti99_mfm_harddisk_write_sector(device_t *harddisk, int cylinder, int head, int sector, UINT8 *buf, int sector_length);
24void ti99_mfm_harddisk_read_track(device_t *harddisk, int head, UINT8 **buffer, int *data_count);
25void ti99_mfm_harddisk_write_track(device_t *harddisk, int head, UINT8 *buffer, int data_count);
26UINT8 ti99_mfm_harddisk_status(device_t *harddisk);
27void ti99_mfm_harddisk_seek(device_t *harddisk, int direction);
28void ti99_mfm_harddisk_get_next_id(device_t *harddisk, int head, chrn_id_hd *id);
41class mfm_harddisk_device : public device_t
42{
43public:
44   mfm_harddisk_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
2945
30DECLARE_LEGACY_DEVICE(MFMHD, mfmhd);
46   void   read_sector(int cylinder, int head, int sector, UINT8 *buf);
47   void   write_sector(int cylinder, int head, int sector, UINT8 *buf);
48   void   read_track(int head, UINT8 *buffer);
49   void   write_track(int head, UINT8 *buffer, int data_count);
50   UINT8   get_status();
51   void   seek(int direction);
52   void   get_next_id(int head, chrn_id_hd *id);
53   int      get_track_length();
3154
32DECLARE_LEGACY_DEVICE(IDEHD, idehd);
55protected:
56   void   device_start();
57   void   device_reset();
58   machine_config_constructor device_mconfig_additions() const;
3359
60private:
61   int    find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number);
62   UINT8   cylinder_to_ident(int cylinder);
63   bool   harddisk_chs_to_lba(hard_disk_file *hdfile, int cylinder, int head, int sector, UINT32 *lba);
64
65   int    m_current_cylinder;
66   int    m_current_head;
67   bool   m_seeking;
68   int    m_status;
69   int    m_id_index; /* position in track for seeking the sector; counts the sector number */
70
71   harddisk_image_device *m_drive;
72};
73
74class ide_harddisk_device : public device_t
75{
76public:
77   ide_harddisk_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
78protected:
79   void   device_start() { };
80   void   device_reset() { };
81   machine_config_constructor device_mconfig_additions() const;
82};
83
3484#define MCFG_MFMHD_3_DRIVES_ADD()         \
35   MCFG_DEVICE_ADD(MFMHD_0, MFMHD, 0)      \
36   MCFG_DEVICE_ADD(MFMHD_1, MFMHD, 0)      \
37   MCFG_DEVICE_ADD(MFMHD_2, MFMHD, 0)      \
38   MCFG_DEVICE_ADD(IDEHD_0, IDEHD, 0)
85   MCFG_DEVICE_ADD(MFMHD_0, TI99_MFMHD, 0)      \
86   MCFG_DEVICE_ADD(MFMHD_1, TI99_MFMHD, 0)      \
87   MCFG_DEVICE_ADD(MFMHD_2, TI99_MFMHD, 0)
88
89#define MCFG_IDEHD_DRIVE_ADD()         \
90   MCFG_DEVICE_ADD(MFMHD_0, TI99_IDEHD, 0)
91
3992#endif
4093
41
42/*
43    MCFG_MFMHD_START(mfmhd)             \
44    MCFG_MFMHD_LOAD(mfmhd)              \
45    MCFG_MFMHD_UNLOAD(mfmhd)
46*/
src/mess/machine/ti99/genboard.c
r14513r14514
11/***************************************************************************
2    Geneve 9640 system board
2    Geneve 9640 mapper and more components
33
4    This file contains 3 classes:
5    - mapper: main function of the Gate Array on the Geneve board. Maps logical
6        memory accesses to a wider address space using map registers.
7    - keyboard: an implementation of a XT-style keyboard. This should be dropped
8        and replaced by a proper XT keyboard implementation.
9    - mouse: an implementation of an Atari-style mouse connected to the v9938.
10
411    Onboard SRAM configuration:
512    There is an adjustable SRAM configuration on board, representing the
613    various enhancements by users.
r14513r14514
129136    remaining time will be used to produce wait states.
130137
131138    Michael Zapf, October 2011
139    February 2012: rewritten as class, restructured
140
132141***************************************************************************/
133142
134#include "emu.h"
135143#include "genboard.h"
136#include "video/v9938.h"
137#include "videowrp.h"
138#include "sound/sn76496.h"
139144#include "machine/mm58274c.h"
140#include "peribox.h"
141145
142#define KEYQUEUESIZE 256
143#define MAXKEYMSGLENGTH 10
144#define KEYAUTOREPEATDELAY 30
145#define KEYAUTOREPEATRATE 6
146#define CYCLE 333      // 333 ns for CLKOUT
147#define VIDEOWAIT 1      // timer ID
146148
147#define CYCLE 333         // 333 ns for CLKOUT
148
149#define SRAM_SIZE 384*1024   // maximum SRAM expansion on-board
150#define DRAM_SIZE 512*1024
151
152149#define VERBOSE 1
153150#define LOG logerror
154151
155typedef struct _genboard_state
152geneve_mapper_device::geneve_mapper_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
153: device_t(mconfig, GENEVE_MAPPER, "Geneve Gate Array", tag, owner, clock)
156154{
157   /* Mouse support */
158   int      last_mx;
159   int      last_my;
160
161   /* Joystick support */
162   int    joySel;
163
164   /* Keyboard support */
165   UINT8   keyQueue[KEYQUEUESIZE];
166   int    keyQueueHead;
167   int    keyQueueLen;
168   bool   keyInBuf;
169   int    keyReset;
170   UINT32   keyStateSave[4];
171   int    keyNumLockState;
172   int    keyCtrlState;
173   int    keyAltState;
174   int    keyRealShiftState;
175   int    keyFakeShiftState;
176   int    keyFakeUnshiftState;
177   int    keyAutoRepeatKey;
178   int    keyAutoRepeatTimer;
179
180   /* Mode flags */
181   bool   palvideo;      // uncertain, could not be verified with real system
182   bool   capslock;
183   bool   keyboard_clock;
184   bool   keep_keybuf;
185   bool   extra_waitstates;
186
187   bool   genmod;
188   bool   turbo;
189   bool   timode;
190
191   /* GROM simulation */
192   int      grom_address;
193   bool   gromraddr_LSB;
194   bool   gromwaddr_LSB;
195
196   /* Memory */
197   UINT8   *sram;
198   UINT8   *dram;
199   UINT8   *eprom;
200   int      sram_mask, sram_val;
201
202   /* Mapper */
203   bool   geneve_mode;
204   bool   direct_mode;
205   bool   cartridge_size_8K;
206   bool   cartridge_secondpage;
207   bool   cartridge6_writable;
208   bool   cartridge7_writable;
209   int      map[8];
210
211   int      line_count;
212
213   /* Devices */
214   device_t   *cpu;
215   device_t   *video;
216   device_t   *tms9901;
217   device_t   *clock;
218   device_t   *peribox;
219   device_t   *sound;
220
221   /* VDP Waitstate timer */
222   bool      video_waitstates;
223   emu_timer   *video_waittimer;
224   bool      video_waiting;
225
226} genboard_state;
227
228static const UINT8 keyboard_mf1_code[0xe] =
229{
230   /* extended keys that are equivalent to non-extended keys */
231   0x1c,   /* keypad enter */
232   0x1d,   /* right control */
233   0x38,   /* alt gr */
234   // extra codes are 0x5b for Left Windows, 0x5c for Right Windows, 0x5d
235   // for Menu, 0x5e for power, 0x5f for sleep, 0x63 for wake, but I doubt
236   // any Geneve program would take advantage of these. */
237
238   // extended key that is equivalent to a non-extended key
239   // with shift off
240   0x35,   /* pad slash */
241
242   // extended keys that are equivalent to non-extended keys
243   // with numlock off
244   0x47,   /* home */
245   0x48,   /* up */
246   0x49,   /* page up */
247   0x4b,   /* left */
248   0x4d,   /* right */
249   0x4f,   /* end */
250   0x50,   /* down */
251   0x51,   /* page down */
252   0x52,   /* insert */
253   0x53   /* delete */
254};
255
256static void poll_keyboard(device_t *device);
257static void poll_mouse(device_t *device);
258static void signal_when_key_available(genboard_state *board);
259static UINT8 get_recent_key(device_t *device);
260
261static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5", "KEY6", "KEY7" };
262
263INLINE genboard_state *get_safe_token(device_t *device)
264{
265   assert(device != NULL);
266   assert(device->type() == GENBOARD);
267
268   return (genboard_state *)downcast<legacy_device_base *>(device)->token();
155   m_eprom = NULL;
269156}
270157
271158/*
272159    Callback for VDP waitstate. Not really needed, just for completeness.
273160*/
274static TIMER_CALLBACK(video_wait_callback)
161void geneve_mapper_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
275162{
276   device_t *device = (device_t *)ptr;
277   genboard_state *board = get_safe_token(device);
278   board->video_waiting = false;
163   // Just remove that flag again.
164   m_video_waiting = false;
279165}
280166
281/*
282    scanline interrupt
283*/
284TIMER_DEVICE_CALLBACK( geneve_hblank_interrupt )
167INPUT_CHANGED_MEMBER( geneve_mapper_device::gm_changed )
285168{
286   device_t *dev = timer.machine().device("geneve_board");
287   int scanline = param;
288   //genboard_state *board = get_safe_token(dev);
169   int number = (int)((UINT64)param&0x03);
170   int value = newval;
289171
290   timer.machine().device<v9938_device>("video_v9938")->interrupt();
291   //board->line_count++;
292   if (scanline == 0) // was 262
293   {
294      //board->line_count = 0;
295      poll_keyboard(dev);
296      poll_mouse(dev);
297   }
298}
299
300void set_gm_switches(device_t *board, int number, int value)
301{
302172   if (number==1)
303173   {
304174      // Turbo switch. May be changed at any time.
305      if (VERBOSE>0) LOG("genboard / genmod: Setting turbo flag to %d\n", value);
306      ((genboard_state*)board)->turbo = (value!=0);
175      if (VERBOSE>0) LOG("geneve / genmod: Setting turbo flag to %d\n", value);
176      m_turbo = (value!=0);
307177   }
308178   else
309179   {
310180      // TIMode switch. Causes reset when changed.
311      if (VERBOSE>0) LOG("genboard / genmod: Setting timode flag to %d\n", value);
312      ((genboard_state*)board)->timode = (value!=0);
313      board->machine().schedule_hard_reset();
181      if (VERBOSE>0) LOG("geneve / genmod: Setting timode flag to %d\n", value);
182      m_timode = (value!=0);
183      machine().schedule_hard_reset();
314184   }
315185}
186
316187/****************************************************************************
317188    GROM simulation. The Geneve board simulated GROM circuits within its gate
318189    array.
r14513r14514
323194    within the gate array. Unlike with real GROMs, no address wrapping occurs,
324195    and the complete 64K space is available.
325196*/
326static READ8_DEVICE_HANDLER( read_grom )
197READ8_MEMBER( geneve_mapper_device::read_grom )
327198{
328   genboard_state *board = get_safe_token(device);
329199   UINT8 reply;
330200   if (offset & 0x0002)
331201   {
332202      // GROM address handling
333      board->gromwaddr_LSB = false;
203      m_gromwaddr_LSB = false;
334204
335      if (board->gromraddr_LSB)
205      if (m_gromraddr_LSB)
336206      {
337         reply = board->grom_address & 0xff;
338         board->gromraddr_LSB = false;
207         reply = m_grom_address & 0xff;
208         m_gromraddr_LSB = false;
339209      }
340210      else
341211      {
342         reply = (board->grom_address >> 8) & 0xff;
343         board->gromraddr_LSB = true;
212         reply = (m_grom_address >> 8) & 0xff;
213         m_gromraddr_LSB = true;
344214      }
345215   }
346216   else
r14513r14514
348218      // GROM data handling
349219      // GROMs are stored in pages 38..3f
350220      int page = 0x38;
351      reply = board->dram[page*0x2000 + board->grom_address];
352      board->grom_address = (board->grom_address + 1) & 0xffff;
353      board->gromraddr_LSB = board->gromwaddr_LSB = false;
221      reply = m_dram[(page<<13) + m_grom_address];
222      m_grom_address = (m_grom_address + 1) & 0xffff;
223      m_gromraddr_LSB = m_gromwaddr_LSB = false;
354224   }
355225   return reply;
356226}
r14513r14514
359229    Simulates GROM. The real Geneve does not use GROMs but simulates them
360230    within the gate array.
361231*/
362static WRITE8_DEVICE_HANDLER( write_grom )
232WRITE8_MEMBER( geneve_mapper_device::write_grom )
363233{
364   genboard_state *board = get_safe_token(device);
365234   if (offset & 0x0002)
366235   {
367236      // set address
368      board->gromraddr_LSB = false;
369      if (board->gromwaddr_LSB)
237      m_gromraddr_LSB = false;
238      if (m_gromwaddr_LSB)
370239      {
371         board->grom_address = (board->grom_address & 0xff00) | data;
372         board->grom_address = (board->grom_address + 1) & 0xffff;
373         board->gromwaddr_LSB = false;
240         m_grom_address = (m_grom_address & 0xff00) | data;
241         m_grom_address = (m_grom_address + 1) & 0xffff;
242         m_gromwaddr_LSB = false;
374243      }
375244      else
376245      {
377         board->grom_address = (board->grom_address & 0x00ff) | ((UINT16)data<<8);
378         board->gromwaddr_LSB = true;
246         m_grom_address = (m_grom_address & 0x00ff) | ((UINT16)data<<8);
247         m_gromwaddr_LSB = true;
379248      }
380249   }
381250   else
382251   {   // write GPL data
383252      // The Geneve GROM simulator allows for GROM writing (verified with a real system)
384253      int page = 0x38;
385      board->dram[page*0x2000 + board->grom_address] = data;
254      m_dram[(page<<13) + m_grom_address] = data;
386255
387      board->grom_address = (board->grom_address + 1) & 0xffff;
388      board->gromraddr_LSB = board->gromwaddr_LSB = false;
256      m_grom_address = (m_grom_address + 1) & 0xffff;
257      m_gromraddr_LSB = m_gromwaddr_LSB = false;
389258   }
390259}
391260
r14513r14514
406275
407276*/
408277
409static void start_video_wait(device_t *device, int ws)
278void geneve_mapper_device::start_video_wait(int ws)
410279{
411   genboard_state *board = get_safe_token(device);
412   board->video_waittimer->adjust(attotime::from_nsec(CYCLE * ws));
413   board->video_waiting = true;
280   m_video_waittimer->adjust(attotime::from_nsec(CYCLE * ws));
281   m_video_waiting = true;
414282}
415283
416static void do_wait(device_t *device, int min)
284void geneve_mapper_device::do_wait(int min)
417285{
418286   long ws;
419   genboard_state *board = get_safe_token(device);
420287
421288   // Extra waitstates?
422   if (board->extra_waitstates && min < 2) min = 2;
289   if (m_extra_waitstates && min < 2) min = 2;
423290
424   if (board->video_waiting) {
425      attotime left = board->video_waittimer->remaining();
291   if (m_video_waiting) {
292      attotime left = m_video_waittimer->remaining();
426293      ws = (left.as_attoseconds()/ATTOSECONDS_PER_NANOSECOND) / CYCLE;
427294   }
428295   else ws = 0;
429296
430297   // Now wait for the remaining time or for at least the min time
431   device_adjust_icount(board->cpu, -4 * ((ws < min)? min : ws));
298   device_adjust_icount(m_cpu, -4 * ((ws < min)? min : ws));
432299}
433300
434READ8_DEVICE_HANDLER( geneve_r )
301/************************************************************************
302    Called by the address map
303************************************************************************/
304
305READ8_MEMBER( geneve_mapper_device::readm )
435306{
436   genboard_state *board = get_safe_token(device);
437307   UINT8 value = 0;
438308   int page;
439309   UINT32   physaddr;
440310
441311   // Premature access. The CPU reads the start vector before RESET.
442   if (board->peribox==NULL) return 0;
312   if (m_eprom==NULL) return 0;
443313
444   if (board->geneve_mode)
314   if (m_geneve_mode)
445315   {
446316      // TODO: shortcut offset & 0xffc0 = 0xf100
447317      if ((offset & 0xfff5)==0xf100)
r14513r14514
453323         // 1111 0001 0000 1000
454324         // 1111 0001 0000 1010
455325
456         // 1 WS is always added; any pending video waitstates are canceled
457         board->video_waiting = false;
458         do_wait(device, 1);
326         // 1 WS is always added; any pending video wait states are canceled
327         m_video_waiting = false;
328         do_wait(1);
459329
460         // Initialize waitstate timer
461         // Create 16 waitstates (2 more than expected, experimenting)
462         if (board->video_waitstates)
463            start_video_wait(device, 16);
330         // Initialize wait state timer
331         // Create 16 wait states (2 more than expected, experimenting)
332         if (m_video_waitstates)
333            start_video_wait(16);
464334
465         gen_v9938_rz(board->video, offset, &value);
335         m_video->readz(space, offset, &value, mem_mask);
466336         return value;
467337      }
468338      if ((offset & 0xfff8)==0xf110)
469339      {
470340         // mapper
471         value = board->map[offset & 0x0007];
341         value = m_map[offset & 0x0007];
472342
473343         // Add appropriate number of waitstates
474344         // 1 WS is added at least
475         do_wait(device, 1);
345         do_wait(1);
476346         return value;
477347      }
478348      if ((offset & 0xfff8) == 0xf118)
479349      {
480350         // key
481         value = get_recent_key(device);
482         do_wait(device, 1);
351         value = m_keyboard->get_recent_key();
352         do_wait(1);
483353         return value;
484354      }
485355      if ((offset & 0xfff0)==0xf130)
r14513r14514
487357         // clock
488358         // tests on the real machine showed that
489359         // upper nibble is 0xf (probably because of the location at 0xf130?)
490         value = mm58274c_r(board->clock, offset & 0x000f) | 0xf0;
491         do_wait(device, 1);
360         value = mm58274c_r(m_clock, offset & 0x000f) | 0xf0;
361         do_wait(1);
492362         return value;
493363      }
494364   }
r14513r14514
497367      if ((offset & 0xfff8)==0x8000)
498368      {
499369         // mapper
500         value = board->map[offset & 0x0007];
501         do_wait(device, 1);
370         value = m_map[offset & 0x0007];
371         do_wait(1);
502372         return value;
503373      }
504374      if ((offset & 0xfff8)== 0x8008)
505375      {
506376         // key
507         value = get_recent_key(device);
508         do_wait(device, 1);
377         value = m_keyboard->get_recent_key();
378         do_wait(1);
509379         return value;
510380      }
511381      if ((offset & 0xfff0)==0x8010)
r14513r14514
518388         // Obscure, needs more investigation. We might as well ignore this,
519389         // as the high nibble is obviously undefined and takes some past
520390         // value floating around.
521         value = mm58274c_r(board->clock, offset & 0x000f);
391         value = mm58274c_r(m_clock, offset & 0x000f);
522392         value |= ((offset & 0x000f)==0x000f)? 0x20 : 0x10;
523393
524         do_wait(device, 1);
394         do_wait(1);
525395         return value;
526396      }
527397      if ((offset & 0xfc01)==0x8800)
r14513r14514
529399         // video
530400         // ++++ ++-- ---- ---+
531401         // 1000 1000 0000 00x0
532         //  device_adjust_icount(board->cpu, -4);
402         //  device_adjust_icount(m_cpu, -4);
533403
534404         // 1 WS is always added; any pending video waitstates are canceled
535         board->video_waiting = false;
536         do_wait(device, 1);
405         m_video_waiting = false;
406         do_wait(1);
537407
538408         // Initialize waitstate timer
539409         // Create 14 waitstates (+2, see above)
540         if (board->video_waitstates)
541            start_video_wait(device, 16);
410         if (m_video_waitstates)
411            start_video_wait(16);
542412
543         gen_v9938_rz(board->video, offset, &value);
413         m_video->readz(space, offset, &value, mem_mask);
544414         return value;
545415      }
546416      if ((offset & 0xfc01)==0x9000)
r14513r14514
549419         // ++++ ++-- ---- ---+
550420         // 1001 0000 0000 0000
551421         // We need to add the address prefix bits
552         ti99_peb_data_rz(board->peribox, offset | ((board->genmod)? 0x170000 : 0x070000), &value);
422         m_peribox->readz(space, offset | ((m_genmod)? 0x170000 : 0x070000), &value, mem_mask);
553423
554         do_wait(device, 1);
424         do_wait(1);
555425         return value;
556426      }
557427      if ((offset & 0xfc01)==0x9800)
r14513r14514
559429         // grom simulation
560430         // ++++ ++-- ---- ---+
561431         // 1001 1000 0000 00x0
562         value = read_grom(device, offset);
432         value = read_grom(space, offset, mem_mask);
563433
564         do_wait(device, 1);
434         do_wait(1);
565435         return value;
566436      }
567437   }
568438
569439   page = (offset & 0xe000) >> 13;
570440
571   if (board->direct_mode)
441   if (m_direct_mode)
572442   {
573443      physaddr = 0x1e0000; // points to boot eprom
574444   }
575445   else
576446   {
577      if (!board->geneve_mode && page==3)
447      if (!m_geneve_mode && page==3)
578448      {
579449         // Cartridge paging in TI mode
580450         // See also cartridge type "paged" in gromport.h
581451         // value 0x36 = 0 0110 110x xxxx xxxx xxxx (page 1)
582452         // value 0x37 = 0 0110 111x xxxx xxxx xxxx (page 2)
583453         // Only use this if there are 2*8 KiB cartridge ROM
584         if (!board->cartridge_size_8K && board->cartridge_secondpage) physaddr = 0x06e000;
454         if (m_cartridge_size==0x4000 && m_cartridge_secondpage) physaddr = 0x06e000;
585455         else physaddr = 0x06c000;
586456      }
587457      else
588458      {
589         physaddr = (board->map[page] << 13);
459         physaddr = (m_map[page] << 13);
590460      }
591461   }
592462
593463   physaddr |= (offset & 0x1fff);
594464
595   if (!board->genmod)
465   if (!m_genmod)
596466   {
597467      // Standard Geneve
598468      if ((physaddr & 0x180000)==0x000000)
599469      {
600470         // DRAM. One wait state.
601         // device_adjust_icount(board->cpu, -4);
602         do_wait(device, 1);
471         // device_adjust_icount(m_cpu, -4);
472         do_wait(1);
603473
604         value = board->dram[physaddr & 0x07ffff];
474         value = m_dram[physaddr & 0x07ffff];
605475//          printf("dram read physaddr = %06x logaddr = %04x value = %02x\n", physaddr, offset, value);
606476         return value;
607477      }
r14513r14514
609479      if ((physaddr & 0x180000)==0x080000)
610480      {
611481         // On-board memory expansion for standard Geneve (never used)
612         // device_adjust_icount(board->cpu, -4);
613         do_wait(device, 1);
482         // device_adjust_icount(m_cpu, -4);
483         do_wait(1);
614484         return 0;
615485      }
616486
r14513r14514
618488      {
619489         // 1 111. ..xx xxxx xxxx xxxx on-board eprom (16K)
620490         // mirrored for f0, f2, f4, ...; f1, f3, f5, ...
621         value = board->eprom[physaddr & 0x003fff];
622//          printf("eprom read physaddr = %06x logaddr = %04x value = %02x\n", physaddr, offset, value);
623         do_wait(device, 1);
491         value = m_eprom[physaddr & 0x003fff];
492         if (VERBOSE>8) LOG("eprom read physaddr = %06x logaddr = %04x value = %02x\n", physaddr, offset, value);
493         do_wait(1);
624494         return value;
625495      }
626496
627497      if ((physaddr & 0x180000)==0x180000)
628498      {
629         if ((physaddr & board->sram_mask)==board->sram_val)
499         if ((physaddr & m_sram_mask)==m_sram_val)
630500         {
631            value = board->sram[physaddr & ~board->sram_mask];
501            value = m_sram[physaddr & ~m_sram_mask];
632502         }
633503         // Return in any case
634504//          printf("sram read physaddr = %06x logaddr = %04x value = %02x\n", physaddr, offset, value);
635         do_wait(device, 0);
505         do_wait(0);
636506         return value;
637507      }
638508
r14513r14514
640510      //   0x000000-0x07ffff for the stock Geneve (AMC,AMB,AMA,A0 ...,A15)
641511      //   0x000000-0x1fffff for the GenMod.(AME,AMD,AMC,AMB,AMA,A0 ...,A15)
642512      // Add a wait state
643      do_wait(device, 1);
513      do_wait(1);
644514
645515      physaddr = (physaddr & 0x0007ffff);  // 19 bit address (with AMA..AMC)
646      ti99_peb_data_rz(board->peribox, physaddr, &value);
516      m_peribox->readz(space, physaddr, &value, mem_mask);
647517      return value;
648518   }
649519   else
650520   {
651521      // GenMod mode
652      if ((board->timode) && ((physaddr & 0x180000)==0x000000))
522      if ((m_timode) && ((physaddr & 0x180000)==0x000000))
653523      {
654524         // DRAM. One wait state.
655         value = board->dram[physaddr & 0x07ffff];
656         if (!board->turbo) do_wait(device, 1);
525         value = m_dram[physaddr & 0x07ffff];
526         if (!m_turbo) do_wait(1);
657527         return value;
658528      }
659529
r14513r14514
661531      {
662532         // 1 111. ..xx xxxx xxxx xxxx on-board eprom (16K)
663533         // mirrored for f0, f2, f4, ...; f1, f3, f5, ...
664         value = board->eprom[physaddr & 0x003fff];
665         do_wait(device, 0);
534         value = m_eprom[physaddr & 0x003fff];
535         do_wait(0);
666536         return value;
667537      }
668538      // Route everything else to the P-Box
669539      physaddr = (physaddr & 0x001fffff);  // 21 bit address for Genmod
670540
671      if (!board->turbo) do_wait(device, 1);
541      if (!m_turbo) do_wait(1);
672542      // Check: Are waitstates completely turned off for turbo mode, or
673543      // merely the waitstates for DRAM memory access and box access?
674544
675      ti99_peb_data_rz(board->peribox, physaddr, &value);
545      m_peribox->readz(space, physaddr, &value, mem_mask);
676546      return value;
677547   }
678548}
r14513r14514
695565
696566*/
697567
698WRITE8_DEVICE_HANDLER( geneve_w )
568WRITE8_MEMBER( geneve_mapper_device::writem )
699569{
700   genboard_state *board = get_safe_token(device);
701570   UINT32   physaddr;
702571   int page;
703572
704   if (board->peribox==NULL) return;
573   if (m_peribox==NULL) return;   // see above: prevent premature access
705574
706   if (board->geneve_mode)
575   if (m_geneve_mode)
707576   {
708577      if ((offset & 0xfff1)==0xf100)
709578      {
710579         // video
711580         // ++++ ++++ ++++ ---+
712581         // 1111 0001 0000 .cc0
713         // device_adjust_icount(board->cpu, -4);
582         // device_adjust_icount(m_cpu, -4);
714583
715         gen_v9938_w(board->video, offset, data);
584         m_video->write(space, offset, data, mem_mask);
716585
717586         // 1 WS is always added; any pending video waitstates are canceled
718         board->video_waiting = false;
719         do_wait(device, 1);
587         m_video_waiting = false;
588         do_wait(1);
720589
721590         // Initialize waitstate timer
722591         // Create 14 waitstates (+3, experimenting)
723         if (board->video_waitstates)
724            start_video_wait(device, 17);
592         if (m_video_waitstates)
593            start_video_wait(17);
725594
726595         return;
727596      }
728597      if ((offset & 0xfff8)==0xf110)
729598      {
730599         // mapper
731         board->map[offset & 0x0007] = data;
732         do_wait(device, 1);
600         m_map[offset & 0x0007] = data;
601         do_wait(1);
733602         return;
734603      }
735604      if ((offset & 0xfff1)==0xf120)
736605      {
737606         // sound
738607         // ++++ ++++ ++++ ---+
739         sn76496_w(board->sound, 0, data);
608         m_sound->write(space, 0, data, mem_mask);
740609
741         // Add 24 waitstates. This is an approximation, as the
610         // Add 24 waitstates. This is an average value, as the
742611         // waitstate generation seems to depend on an external timer of
743612         // the sound chip
744         device_adjust_icount(board->cpu, -4 * 24);
613         device_adjust_icount(m_cpu, -4 * 24);
745614         return;
746615      }
747616      if ((offset & 0xfff0)==0xf130)
748617      {
749618         // clock
750619         // ++++ ++++ ++++ ----
751         mm58274c_w(board->clock, offset & 0x00f, data);
752         do_wait(device, 1);
620         mm58274c_w(m_clock, offset & 0x00f, data);
621         do_wait(1);
753622         return;
754623      }
755624   }
r14513r14514
759628      if ((offset & 0xfff8)==0x8000)
760629      {
761630         // mapper
762         board->map[offset & 0x0007] = data;
763         do_wait(device, 1);
631         m_map[offset & 0x0007] = data;
632         do_wait(1);
764633         return;
765634      }
766635      // No key write at 8008
767636      if ((offset & 0xfff0)==0x8010)
768637      {
769638         // clock
770         mm58274c_w(board->clock, offset & 0x00f, data);
771         do_wait(device, 1);
639         mm58274c_w(m_clock, offset & 0x00f, data);
640         do_wait(1);
772641         return;
773642      }
774643      if ((offset & 0xfc01)==0x8400)
r14513r14514
777646         // ++++ ++-- ---- ---+
778647         // 1000 0100 0000 0000
779648
780         sn76496_w(board->sound, 0, data);
649         m_sound->write(space, 0, data, mem_mask);
781650         // Add 24 waitstates. This is an approximation, as the
782651         // waitstate generation seems to depend on an external timer of
783652         // the sound chip
784         device_adjust_icount(board->cpu, -4 * 24);
653         device_adjust_icount(m_cpu, -4 * 24);
785654         return;
786655      }
787656      if ((offset & 0xfc01)==0x8c00)
r14513r14514
789658         // video
790659         // ++++ ++-- ---- ---+
791660         // 1000 1100 0000 00c0
792         // device_adjust_icount(board->cpu, -4);
661         // device_adjust_icount(m_cpu, -4);
793662         // Initialize waitstate timer
794         gen_v9938_w(board->video, offset, data);
663         m_video->write(space, offset, data, mem_mask);
795664
796665         // 1 WS is always added; any pending video waitstates are canceled
797         board->video_waiting = false;
798         do_wait(device, 1);
666         m_video_waiting = false;
667         do_wait(1);
799668
800669         // Initialize waitstate timer
801670         // Create 14 waitstates (+3)
802         if (board->video_waitstates)
803            start_video_wait(device, 17);
671         if (m_video_waitstates)
672            start_video_wait(17);
804673
805674         return;
806675      }
r14513r14514
810679         // ++++ ++-- ---- ---+
811680         // 1001 0100 0000 0000
812681         // We need to add the address prefix bits
813         ti99_peb_data_w(board->peribox, offset | ((board->genmod)? 0x170000 : 0x070000), data);
814         do_wait(device, 1);
682         m_peribox->write(space, offset | ((m_genmod)? 0x170000 : 0x070000), data, mem_mask);
683         do_wait(1);
815684         return;
816685      }
817686      if ((offset & 0xfc01)==0x9c00)
r14513r14514
819688         // grom simulation
820689         // ++++ ++-- ---- ---+
821690         // 1001 1100 0000 00c0
822         write_grom(device, offset, data);
823         do_wait(device, 1);
691         write_grom(space, offset, data, mem_mask);
692         do_wait(1);
824693         return;
825694      }
826695   }
827696
828697   page = (offset & 0xe000) >> 13;
829   if (board->direct_mode)
698   if (m_direct_mode)
830699   {
831700      physaddr = 0x1e0000; // points to boot eprom
832701   }
833702   else
834703   {
835      if (!board->geneve_mode && page==3)
704      if (!m_geneve_mode && page==3)
836705      {
837         if (!board->cartridge_size_8K)
706         if (m_cartridge_size==0x4000)
838707         {
839708            // Writing to 0x6000 selects page 1,
840709            // writing to 0x6002 selects page 2
841            board->cartridge_secondpage = ((offset & 0x0002)!=0);
842            do_wait(device, 1);
710            m_cartridge_secondpage = ((offset & 0x0002)!=0);
711            do_wait(1);
843712            return;
844713         }
845714         else
846715         {
847716            // writing into cartridge rom space (no bankswitching)
848            if ((((offset & 0x1000)==0x0000) && !board->cartridge6_writable)
849               || (((offset & 0x1000)==0x1000) && !board->cartridge7_writable))
717            if ((((offset & 0x1000)==0x0000) && !m_cartridge6_writable)
718               || (((offset & 0x1000)==0x1000) && !m_cartridge7_writable))
850719            {
851720               if (VERBOSE>0) LOG("genboard: Writing to protected cartridge space %04x ignored\n", offset);
852721               return;
r14513r14514
857726         }
858727      }
859728      else
860         physaddr = (board->map[page] << 13);
729         physaddr = (m_map[page] << 13);
861730   }
862731
863732   physaddr |= offset & 0x1fff;
864733
865734//  printf("write physaddr = %06x logaddr = %04x value = %02x\n", physaddr, offset, data);
866735
867   if (!board->genmod)
736   if (!m_genmod)
868737   {
869738      if ((physaddr & 0x180000)==0x000000)
870739      {
871740         // DRAM write. One wait state. (only for normal Geneve)
872         board->dram[physaddr & 0x07ffff] = data;
873         do_wait(device, 1);
741         m_dram[physaddr & 0x07ffff] = data;
742         do_wait(1);
874743         return;
875744      }
876745
877746      if ((physaddr & 0x180000)==0x080000)
878747      {
879748         // On-board memory expansion for standard Geneve (never used)
880         do_wait(device, 1);
749         do_wait(1);
881750         return;
882751      }
883752
r14513r14514
886755         // 1 111. ..xx xxxx xxxx xxxx on-board eprom (16K)
887756         // mirrored for f0, f2, f4, ...; f1, f3, f5, ...
888757         // Ignore EPROM write
889         do_wait(device, 1);
758         do_wait(1);
890759         return;
891760      }
892761
893762      if ((physaddr & 0x180000)==0x180000)
894763      {
895         if ((physaddr & board->sram_mask)==board->sram_val)
764         if ((physaddr & m_sram_mask)==m_sram_val)
896765         {
897            board->sram[physaddr & ~board->sram_mask] = data;
766            m_sram[physaddr & ~m_sram_mask] = data;
898767         }
899         do_wait(device, 0);
768         do_wait(0);
900769         // Return in any case
901770         return;
902771      }
r14513r14514
905774
906775      // only AMA, AMB, AMC are used; AMD and AME are not used
907776      physaddr = (physaddr & 0x0007ffff);  // 19 bit address
908      ti99_peb_data_w(board->peribox, physaddr, data);
909      do_wait(device, 1);
777      m_peribox->write(space, physaddr, data, mem_mask);
778      do_wait(1);
910779   }
911780   else
912781   {
913782      // GenMod mode
914      if ((board->timode) && ((physaddr & 0x180000)==0x000000))
783      if ((m_timode) && ((physaddr & 0x180000)==0x000000))
915784      {
916785         // DRAM. One wait state.
917         board->dram[physaddr & 0x07ffff] = data;
918         if (!board->turbo) do_wait(device, 1);
786         m_dram[physaddr & 0x07ffff] = data;
787         if (!m_turbo) do_wait(1);
919788         return;
920789      }
921790
r14513r14514
924793         // 1 111. ..xx xxxx xxxx xxxx on-board eprom (16K)
925794         // mirrored for f0, f2, f4, ...; f1, f3, f5, ...
926795         // Ignore EPROM write
927         if (!board->turbo) do_wait(device, 1);
796         if (!m_turbo) do_wait(1);
928797         return;
929798      }
930799      // Route everything else to the P-Box
931800      physaddr = (physaddr & 0x001fffff);  // 21 bit address for Genmod
932      ti99_peb_data_w(board->peribox, physaddr, data);
933      if (!board->turbo) do_wait(device, 1);
801      m_peribox->write(space, physaddr, data, mem_mask);
802      if (!m_turbo) do_wait(1);
934803   }
935804}
936805
937/****************************************************************************
938    CRU handling
939*****************************************************************************/
806void geneve_mapper_device::device_start()
807{
808   if (VERBOSE>0) LOG("geneve: Starting Geneve mapper\n");
809   // Get pointers
810   m_peribox = machine().device<bus8z_device>(PERIBOX_TAG);
811   m_keyboard = machine().device<geneve_keyboard_device>(GKEYBOARD_TAG);
812   m_video = machine().device<bus8z_device>(VIDEO_SYSTEM_TAG);
813   m_sound = machine().device<bus8z_device>(TISOUND_TAG);
814   m_clock = machine().device(GCLOCK_TAG);
815   m_cpu = machine().device("maincpu");
940816
941#define CRU_CONTROL_BASE 0x1ee0
942#define CRU_SSTEP_BASE 0x13c0
817   // This is a preliminary setting; the initial context switch occurs before
818   // device_reset. Luckily, the reset vector at >0000/>0002 is the same for
819   // all variants of the boot eprom.
820   m_eprom = machine().region("maincpu")->base();
943821
944WRITE8_DEVICE_HANDLER ( geneve_cru_w )
822   m_sram = machine().region(SRAM_TAG)->base();
823   m_dram = machine().region(DRAM_TAG)->base();
824
825   m_geneve_mode = false;
826   m_direct_mode = true;
827
828   // Used for generating the VDP waitstates. These are created in the
829   // Gate Array on the board and only affect external memory accesses.
830   // Therefore we cannot use the usual adjust_icount
831   m_video_waittimer = timer_alloc(VIDEOWAIT);
832}
833
834void geneve_mapper_device::device_reset()
945835{
946   genboard_state *board = get_safe_token(device);
836   if (VERBOSE>0) LOG("geneve: Resetting mapper\n");
947837
948   int addroff = offset << 1;
949   bool rising_edge = false;
950   bool falling_edge = false;
838   m_extra_waitstates = false;
839   m_video_waitstates = true;
840   m_video_waiting = false;
951841
952   // Single step
953   // 13c0 - 13fe: 0001 0011 11xx xxx0
954   if ((addroff & 0xffc0) == CRU_SSTEP_BASE)
955   {
956      int bit = (addroff & 0x003e)>>1;
957      if (VERBOSE>0) LOG("genboard: Single step not implemented; bit %d set to %d\n", bit, data);
958      return;
959   }
842   m_geneve_mode =false;
843   m_direct_mode = true;
844   m_cartridge_size = 0x4000;
845   m_cartridge_secondpage = false;
846   m_cartridge6_writable = false;
847   m_cartridge7_writable = false;
960848
961   if ((addroff & 0xffe0) == CRU_CONTROL_BASE)
849   // Clear map
850   for (int i=0; i < 8; i++) m_map[i] = 0;
851
852   m_genmod = false;
853
854   if (input_port_read(machine(), "MODE")==0)
962855   {
963      int bit = (addroff & 0x001e)>>1;
964      switch (bit)
856      switch (input_port_read(machine(), "BOOTROM"))
965857      {
966      case 5:
967         board->palvideo = (data!=0);
858      case GENEVE_098:
859         if (VERBOSE>0) LOG("geneve: Using 0.98 boot eprom\n");
860         m_eprom = machine().region("maincpu")->base() + 0x4000;
968861         break;
969      case 7:
970         board->capslock = (data!=0);
862      case GENEVE_100:
863         if (VERBOSE>0) LOG("geneve: Using 1.00 boot eprom\n");
864         m_eprom = machine().region("maincpu")->base();
971865         break;
972      case 8:
973         rising_edge = (!board->keyboard_clock && (data!=0));
974         board->keyboard_clock = (data!=0);
975         if (rising_edge)
976            signal_when_key_available(board);
977         break;
978      case 9:
979         rising_edge = (!board->keep_keybuf && (data!=0));
980         falling_edge = (board->keep_keybuf && (data==0));
981         board->keep_keybuf = (data!=0);
982
983         if (rising_edge)
984            signal_when_key_available(board);
985         else
986         {
987            if (falling_edge)
988            {
989               if (board->keyQueueLen != 0)
990               {
991                  board->keyQueueHead = (board->keyQueueHead + 1) % KEYQUEUESIZE;
992                  board->keyQueueLen--;
993               }
994               /* clear keyboard interrupt */
995               tms9901_set_single_int(board->tms9901, 8, CLEAR_LINE);
996               board->keyInBuf = false;
997            }
998         }
999         break;
1000      case 10:
1001         board->geneve_mode = (data!=0);
1002         break;
1003      case 11:
1004         board->direct_mode = (data!=0);
1005         break;
1006      case 12:
1007         board->cartridge_size_8K = (data!=0);
1008         break;
1009      case 13:
1010         board->cartridge6_writable = (data!=0);
1011         break;
1012      case 14:
1013         board->cartridge7_writable = (data!=0);
1014         break;
1015      case 15:
1016         board->extra_waitstates = (data==0);  // let's use the inverse semantics
1017         break;
1018      default:
1019         if (VERBOSE>0) LOG("genboard: set CRU address %04x=%02x ignored\n", addroff, data);
1020         break;
1021866      }
1022867   }
1023868   else
1024869   {
1025      ti99_peb_cru_w(board->peribox, addroff, data);
870      if (VERBOSE>0) LOG("geneve: Using GenMod modification\n");
871      m_eprom = machine().region("maincpu")->base() + 0x8000;
872      if (m_eprom[0] != 0xf0)
873      {
874         fatalerror("genboard: GenMod boot rom missing.\n");
875      }
876      m_genmod = true;
877      m_turbo = ((input_port_read(machine(), "GENMODDIPS") & GM_TURBO)!=0);
878      m_timode = ((input_port_read(machine(), "GENMODDIPS") & GM_TIM)!=0);
1026879   }
1027}
1028880
1029
1030READ8_DEVICE_HANDLER ( geneve_cru_r )
1031{
1032   UINT8 value = 0;
1033   genboard_state *board = get_safe_token(device);
1034   int addroff = offset << 4;
1035
1036   // Single step
1037   // 13c0 - 13fe: 0001 0011 11xx xxx0
1038   if ((addroff & 0xffc0) == CRU_SSTEP_BASE)
881   switch (input_port_read(machine(), "SRAM"))
1039882   {
1040      int bit = (addroff & 0x003e)>>1;
1041      if (VERBOSE>0) LOG("genboard: Single step not implemented; attempting to read bit %d\n", bit);
1042      return value;
883/*  1 100. .... .... .... .... on-board sram (128K) -+
884    1 101. .... .... .... .... on-board sram (128K) -+-- maximum SRAM expansion
885    1 1100 .... .... .... .... on-board sram (64K) --+
886    1 1101 0... .... .... .... on-board sram (32K) - additional 32 KiB required for MDOS 2.50s and higher
887    1 1101 1... .... .... .... on-board sram (32K) - standard setup
888*/
889   case 0: // 32 KiB
890      m_sram_mask =   0x1f8000;
891      m_sram_val =   0x1d8000;
892      break;
893   case 1: // 64 KiB
894      m_sram_mask =   0x1f0000;
895      m_sram_val =   0x1d0000;
896      break;
897   case 2: // 384 KiB (actually 512 KiB, but the EPROM masks the upper 128 KiB)
898      m_sram_mask =   0x180000;
899      m_sram_val =    0x180000;
900      break;
1043901   }
1044
1045   // TMS9995-internal CRU locations (1ee0-1efe) are handled within the 99xxcore
1046   // implementation (read_single_cru), so we don't arrive here
1047
1048   // Propagate the CRU access to external devices
1049   ti99_peb_cru_rz(board->peribox, addroff, &value);
1050   return value;
1051902}
1052903
904const device_type GENEVE_MAPPER = &device_creator<geneve_mapper_device>;
905
1053906/****************************************************************************
1054907    Keyboard support
1055*****************************************************************************/
908****************************************************************************/
1056909
1057UINT8 get_recent_key(device_t *device)
910static const char *const KEYNAMES[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5", "KEY6", "KEY7" };
911
912static const UINT8 MF1_CODE[0xe] =
1058913{
1059   genboard_state *board = get_safe_token(device);
1060   if (board->keyInBuf)
1061      return board->keyQueue[board->keyQueueHead];
1062   else
1063      return 0;
1064}
914   /* extended keys that are equivalent to non-extended keys */
915   0x1c,   /* keypad enter */
916   0x1d,   /* right control */
917   0x38,   /* alt gr */
918   // extra codes are 0x5b for Left Windows, 0x5c for Right Windows, 0x5d
919   // for Menu, 0x5e for power, 0x5f for sleep, 0x63 for wake, but I doubt
920   // any Geneve program would take advantage of these. */
1065921
1066static void signal_when_key_available(genboard_state *board)
922   // extended key that is equivalent to a non-extended key
923   // with shift off
924   0x35,   /* pad slash */
925
926   // extended keys that are equivalent to non-extended keys
927   // with numlock off
928   0x47,   /* home */
929   0x48,   /* up */
930   0x49,   /* page up */
931   0x4b,   /* left */
932   0x4d,   /* right */
933   0x4f,   /* end */
934   0x50,   /* down */
935   0x51,   /* page down */
936   0x52,   /* insert */
937   0x53   /* delete */
938};
939
940geneve_keyboard_device::geneve_keyboard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
941: device_t(mconfig, GENEVE_KEYBOARD, "Geneve XT-style keyboard", tag, owner, clock)
1067942{
1068   // if keyboard reset is not asserted, and key clock is enabled, and key
1069   // buffer clear is disabled, and key queue is not empty. */
1070   if ((!board->keyReset) && (board->keyboard_clock) && (board->keep_keybuf) && (board->keyQueueLen != 0))
1071   {
1072      tms9901_set_single_int(board->tms9901, 8, ASSERT_LINE);
1073      board->keyInBuf = true;
1074   }
1075943}
1076944
1077INLINE void post_in_keyQueue(genboard_state *board, int keycode)
945void geneve_keyboard_device::post_in_key_queue(int keycode)
1078946{
1079   board->keyQueue[(board->keyQueueHead + board->keyQueueLen) % KEYQUEUESIZE] = keycode;
1080   board->keyQueueLen++;
947   m_key_queue[(m_key_queue_head + m_key_queue_length) % KEYQUEUESIZE] = keycode;
948   m_key_queue_length++;
949
950   if (VERBOSE>5) LOG("geneve: posting keycode %02x\n", keycode);
1081951}
1082952
1083static void poll_keyboard(device_t *device)
953void geneve_keyboard_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
1084954{
1085   genboard_state *board = get_safe_token(device);
955   poll();
956}
1086957
958void geneve_keyboard_device::poll()
959{
1087960   UINT32 keystate;
1088961   UINT32 key_transitions;
1089962   int i, j;
1090963   int keycode;
1091964   int pressed;
965   if (VERBOSE>8) LOG("geneve: poll keyboard\n");
966   if (m_key_reset) return;
1092967
1093   if (board->keyReset)
1094      return;
1095
1096968   /* Poll keyboard */
1097   for (i = 0; (i < 4) && (board->keyQueueLen <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)); i++)
969   for (i = 0; (i < 4) && (m_key_queue_length <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)); i++)
1098970   {
1099      keystate = input_port_read(device->machine(), keynames[2*i]) | (input_port_read(device->machine(), keynames[2*i + 1]) << 16);
1100      key_transitions = keystate ^ board->keyStateSave[i];
971      keystate = input_port_read(*this, KEYNAMES[2*i]) | (input_port_read(*this, KEYNAMES[2*i + 1]) << 16);
972      key_transitions = keystate ^ m_key_state_save[i];
1101973      if (key_transitions)
1102974      {
1103         for (j = 0; (j < 32) && (board->keyQueueLen <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)); j++)
975         for (j = 0; (j < 32) && (m_key_queue_length <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)); j++)
1104976         {
1105977            if ((key_transitions >> j) & 1)
1106978            {
1107979               keycode = (i << 5) | j;
1108980               pressed = ((keystate >> j) & 1);
1109981               if (pressed)
1110                  board->keyStateSave[i] |= (1 << j);
982                  m_key_state_save[i] |= (1 << j);
1111983               else
1112                  board->keyStateSave[i] &= ~ (1 << j);
984                  m_key_state_save[i] &= ~ (1 << j);
1113985
1114986               /* Update auto-repeat */
1115987               if (pressed)
1116988               {
1117                  board->keyAutoRepeatKey = keycode;
1118                  board->keyAutoRepeatTimer = KEYAUTOREPEATDELAY+1;
989                  m_key_autorepeat_key = keycode;
990                  m_key_autorepeat_timer = KEYAUTOREPEATDELAY+1;
1119991               }
1120               else /*if (keycode == board->keyAutoRepeatKey)*/
1121                  board->keyAutoRepeatKey = 0;
992               else /*if (keycode == m_key_autorepeat_key)*/
993                  m_key_autorepeat_key = 0;
1122994
1123995               // Release Fake Shift/Unshift if another key is pressed
1124996               // We do so if a key is released, though it is actually
1125997               // required only if it is a modifier key
1126998               /*if (pressed)*/
1127999               //{
1128               if (board->keyFakeShiftState)
1000               if (m_key_fake_shift_state)
11291001               {
11301002                  /* Fake shift release */
1131                  post_in_keyQueue(board, 0xe0);
1132                  post_in_keyQueue(board, 0xaa);
1133                  board->keyFakeShiftState = 0;
1003                  post_in_key_queue(0xe0);
1004                  post_in_key_queue(0xaa);
1005                  m_key_fake_shift_state = false;
11341006               }
1135               if (board->keyFakeUnshiftState)
1007               if (m_key_fake_unshift_state)
11361008               {
11371009                  /* Fake shift press */
1138                  post_in_keyQueue(board, 0xe0);
1139                  post_in_keyQueue(board, 0x2a);
1140                  board->keyFakeUnshiftState = 0;
1010                  post_in_key_queue(0xe0);
1011                  post_in_key_queue(0x2a);
1012                  m_key_fake_unshift_state = false;
11411013               }
11421014               //}
11431015
11441016               /* update shift and numlock state */
11451017               if ((keycode == 0x2a) || (keycode == 0x36))
1146                  board->keyRealShiftState = board->keyRealShiftState + (pressed ? +1 : -1);
1018                  m_key_real_shift_state = m_key_real_shift_state + (pressed ? +1 : -1);
11471019               if ((keycode == 0x1d) || (keycode == 0x61))
1148                  board->keyCtrlState = board->keyCtrlState + (pressed ? +1 : -1);
1020                  m_key_ctrl_state = m_key_ctrl_state + (pressed ? +1 : -1);
11491021               if ((keycode == 0x38) || (keycode == 0x62))
1150                  board->keyAltState = board->keyAltState + (pressed ? +1 : -1);
1022                  m_key_alt_state = m_key_alt_state + (pressed ? +1 : -1);
11511023               if ((keycode == 0x45) && pressed)
1152                  board->keyNumLockState = ! board->keyNumLockState;
1024                  m_key_numlock_state = !m_key_numlock_state;
11531025
11541026               if ((keycode >= 0x60) && (keycode < 0x6e))
11551027               {   /* simpler extended keys */
r14513r14514
11601032                     /* Handle shift state */
11611033                     if (keycode == 0x63)
11621034                     {   /* non-shifted key */
1163                        if (board->keyRealShiftState)
1035                        if (m_key_real_shift_state!=0)
11641036                           /* Fake shift unpress */
1165                           board->keyFakeUnshiftState = 1;
1037                           m_key_fake_unshift_state = true;
11661038                     }
11671039                     else /*if (keycode >= 0x64)*/
11681040                     {   /* non-numlock mode key */
1169                        if (board->keyNumLockState & ! board->keyRealShiftState)
1041                        if (m_key_numlock_state & (m_key_real_shift_state==0))
11701042                           /* Fake shift press if numlock is active */
1171                           board->keyFakeShiftState = 1;
1172                        else if ((! board->keyNumLockState) & board->keyRealShiftState)
1043                           m_key_fake_shift_state = true;
1044                        else if ((!m_key_numlock_state) & (m_key_real_shift_state!=0))
11731045                           /* Fake shift unpress if shift is down */
1174                           board->keyFakeUnshiftState = 1;
1046                           m_key_fake_unshift_state = true;
11751047                     }
11761048
1177                     if (board->keyFakeShiftState)
1049                     if (m_key_fake_shift_state)
11781050                     {
1179                        post_in_keyQueue(board, 0xe0);
1180                        post_in_keyQueue(board, 0x2a);
1051                        post_in_key_queue(0xe0);
1052                        post_in_key_queue(0x2a);
11811053                     }
11821054
1183                     if (board->keyFakeUnshiftState)
1055                     if (m_key_fake_unshift_state)
11841056                     {
1185                        post_in_keyQueue(board, 0xe0);
1186                        post_in_keyQueue(board, 0xaa);
1057                        post_in_key_queue(0xe0);
1058                        post_in_key_queue(0xaa);
11871059                     }
11881060                  }
11891061
1190                  keycode = keyboard_mf1_code[keycode-0x60];
1191                  if (! pressed)
1192                     keycode |= 0x80;
1193                  post_in_keyQueue(board, 0xe0);
1194                  post_in_keyQueue(board, keycode);
1062                  keycode = MF1_CODE[keycode-0x60];
1063                  if (!pressed) keycode |= 0x80;
1064                  post_in_key_queue(0xe0);
1065                  post_in_key_queue(keycode);
11951066               }
11961067               else if (keycode == 0x6e)
11971068               {   /* emulate Print Screen / System Request (F13) key */
11981069                  /* this is a bit complex, as Alt+PrtScr -> SysRq */
11991070                  /* Additionally, Ctrl+PrtScr involves no fake shift press */
1200                  if (board->keyAltState)
1071                  if (m_key_alt_state!=0)
12011072                  {
12021073                     /* SysRq */
12031074                     keycode = 0x54;
1204                     if (! pressed)
1205                        keycode |= 0x80;
1206                     post_in_keyQueue(board, keycode);
1075                     if (!pressed) keycode |= 0x80;
1076                     post_in_key_queue(keycode);
12071077                  }
12081078                  else
12091079                  {
12101080                     /* Handle shift state */
1211                     if (pressed && (! board->keyRealShiftState) && (! board->keyCtrlState))
1081                     if (pressed && (m_key_real_shift_state==0) && (m_key_ctrl_state==0))
12121082                     {   /* Fake shift press */
1213                        post_in_keyQueue(board, 0xe0);
1214                        post_in_keyQueue(board, 0x2a);
1215                        board->keyFakeShiftState = 1;
1083                        post_in_key_queue(0xe0);
1084                        post_in_key_queue(0x2a);
1085                        m_key_fake_shift_state = true;
12161086                     }
12171087
12181088                     keycode = 0x37;
1219                     if (! pressed)
1220                        keycode |= 0x80;
1221                     post_in_keyQueue(board, 0xe0);
1222                     post_in_keyQueue(board, keycode);
1089                     if (!pressed) keycode |= 0x80;
1090                     post_in_key_queue(0xe0);
1091                     post_in_key_queue(keycode);
12231092                  }
12241093               }
12251094               else if (keycode == 0x6f)
r14513r14514
12291098                  // repeat or release.
12301099                  if (pressed)
12311100                  {
1232                     if (board->keyCtrlState)
1101                     if (m_key_ctrl_state!=0)
12331102                     {
1234                        post_in_keyQueue(board, 0xe0);
1235                        post_in_keyQueue(board, 0x46);
1236                        post_in_keyQueue(board, 0xe0);
1237                        post_in_keyQueue(board, 0xc6);
1103                        post_in_key_queue(0xe0);
1104                        post_in_key_queue(0x46);
1105                        post_in_key_queue(0xe0);
1106                        post_in_key_queue(0xc6);
12381107                     }
12391108                     else
12401109                     {
1241                        post_in_keyQueue(board, 0xe1);
1242                        post_in_keyQueue(board, 0x1d);
1243                        post_in_keyQueue(board, 0x45);
1244                        post_in_keyQueue(board, 0xe1);
1245                        post_in_keyQueue(board, 0x9d);
1246                        post_in_keyQueue(board, 0xc5);
1110                        post_in_key_queue(0xe1);
1111                        post_in_key_queue(0x1d);
1112                        post_in_key_queue(0x45);
1113                        post_in_key_queue(0xe1);
1114                        post_in_key_queue(0x9d);
1115                        post_in_key_queue(0xc5);
12471116                     }
12481117                  }
12491118               }
12501119               else
12511120               {
1252                  if (! pressed)
1253                     keycode |= 0x80;
1254                  post_in_keyQueue(board, keycode);
1121                  if (!pressed) keycode |= 0x80;
1122                  post_in_key_queue(keycode);
12551123               }
1256               signal_when_key_available(board);
1124               signal_when_key_available();
12571125            }
12581126         }
12591127      }
12601128   }
12611129
12621130   /* Handle auto-repeat */
1263   if ((board->keyQueueLen <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)) && board->keyAutoRepeatKey && (--board->keyAutoRepeatTimer == 0))
1131   if ((m_key_queue_length <= (KEYQUEUESIZE-MAXKEYMSGLENGTH)) && (m_key_autorepeat_key!=0) && (--m_key_autorepeat_timer == 0))
12641132   {
1265      if ((board->keyAutoRepeatKey >= 0x60) && (board->keyAutoRepeatKey < 0x6e))
1133      if ((m_key_autorepeat_key >= 0x60) && (m_key_autorepeat_key < 0x6e))
12661134      {
1267         post_in_keyQueue(board, 0xe0);
1268         post_in_keyQueue(board, keyboard_mf1_code[board->keyAutoRepeatKey-0x60]);
1135         post_in_key_queue(0xe0);
1136         post_in_key_queue(MF1_CODE[m_key_autorepeat_key-0x60]);
12691137      }
1270      else if (board->keyAutoRepeatKey == 0x6e)
1138      else if (m_key_autorepeat_key == 0x6e)
12711139      {
1272         if (board->keyAltState)
1273            post_in_keyQueue(board, 0x54);
1140         if (m_key_alt_state!=0)
1141            post_in_key_queue(0x54);
12741142         else
12751143         {
1276            post_in_keyQueue(board, 0xe0);
1277            post_in_keyQueue(board, 0x37);
1144            post_in_key_queue(0xe0);
1145            post_in_key_queue(0x37);
12781146         }
12791147      }
1280      else if (board->keyAutoRepeatKey == 0x6f)
1148      else if (m_key_autorepeat_key == 0x6f)
12811149         ;
12821150      else
12831151      {
1284         post_in_keyQueue(board, board->keyAutoRepeatKey);
1152         post_in_key_queue(m_key_autorepeat_key);
12851153      }
1286      signal_when_key_available(board);
1287      board->keyAutoRepeatTimer = KEYAUTOREPEATRATE;
1154      signal_when_key_available();
1155      m_key_autorepeat_timer = KEYAUTOREPEATRATE;
12881156   }
12891157}
12901158
1291/****************************************************************************
1292    Mouse support
1293****************************************************************************/
1294
1295static void poll_mouse(device_t *device)
1159UINT8 geneve_keyboard_device::get_recent_key()
12961160{
1297   genboard_state *board = get_safe_token(device);
1298
1299   int new_mx, new_my;
1300   int delta_x, delta_y, buttons;
1301
1302   buttons = input_port_read(device->machine(), "MOUSE0");
1303   new_mx = input_port_read(device->machine(), "MOUSEX");
1304   new_my = input_port_read(device->machine(), "MOUSEY");
1305
1306   /* compute x delta */
1307   delta_x = new_mx - board->last_mx;
1308
1309   /* check for wrap */
1310   if (delta_x > 0x80)
1311      delta_x = 0x100-delta_x;
1312   if  (delta_x < -0x80)
1313      delta_x = -0x100-delta_x;
1314
1315   board->last_mx = new_mx;
1316
1317   /* compute y delta */
1318   delta_y = new_my - board->last_my;
1319
1320   /* check for wrap */
1321   if (delta_y > 0x80)
1322      delta_y = 0x100-delta_y;
1323   if  (delta_y < -0x80)
1324      delta_y = -0x100-delta_y;
1325
1326   board->last_my = new_my;
1327
1328   video_update_mouse(board->video, delta_x, delta_y, buttons & 3);
1161   if (m_key_in_buffer) return m_key_queue[m_key_queue_head];
1162   else return 0;
13291163}
13301164
1331/****************************************************************************
1332    TMS9901 and attached devices handling
1333****************************************************************************/
1334
1335/*
1336    Called by the 9901 core whenever the state of INTREQ and IC0-3 changes
1337*/
1338static TMS9901_INT_CALLBACK( tms9901_interrupt_callback )
1165void geneve_keyboard_device::signal_when_key_available()
13391166{
1340   /* INTREQ is connected to INT1 (IC0-3 are not connected) */
1341   cputag_set_input_line(device->machine(), "maincpu", 0, intreq? ASSERT_LINE : CLEAR_LINE);
1167   // if keyboard reset is not asserted, and key clock is enabled, and key
1168   // buffer clear is disabled, and key queue is not empty. */
1169   if ((!m_key_reset) && (m_keyboard_clock) && (m_keep_keybuf) && (m_key_queue_length != 0))
1170   {
1171      if (VERBOSE>6) LOG("geneve: signalling key available\n");
1172      m_interrupt(ASSERT_LINE);
1173      m_key_in_buffer = true;
1174   }
13421175}
13431176
1344/*
1345    Read pins INT3*-INT7* of Geneve 9901.
1346
1347    signification:
1348     (bit 1: INT1 status)
1349     (bit 2: INT2 status)
1350     bit 3-7: joystick status
1351*/
1352static READ8_DEVICE_HANDLER( R9901_0 )
1177WRITE_LINE_MEMBER( geneve_keyboard_device::clock_control )
13531178{
1354   device_t *dev = device->machine().device("geneve_board");
1355   genboard_state *board = get_safe_token(dev);
1356   int answer;
1357   answer = input_port_read(device->machine(), "JOY") >> (board->joySel * 8);
1358   return answer;
1179   bool rising_edge = (!m_keyboard_clock && (state==ASSERT_LINE));
1180   m_keyboard_clock = (state==ASSERT_LINE);
1181   if (VERBOSE>5) LOG("geneve: keyboard clock_control state=%d\n", m_keyboard_clock);
1182   if (rising_edge)
1183      signal_when_key_available();
13591184}
13601185
1361/*
1362    Read pins INT8*-INT15* of Geneve 9901.
1363
1364    signification:
1365     (bit 0: keyboard interrupt)
1366     bit 1: unused
1367     bit 2: mouse right button
1368     (bit 3: clock interrupt)
1369     (bit 4: INTB from PE-bus)
1370     bit 5 & 7: used as output
1371     bit 6: unused
1372*/
1373static READ8_DEVICE_HANDLER( R9901_1 )
1186WRITE_LINE_MEMBER( geneve_keyboard_device::send_scancodes )
13741187{
1375   int answer;
1376   answer = (input_port_read(device->machine(), "MOUSE0") & 4) ^ 4;
1377   return answer;
1378}
1188   bool rising_edge = (!m_keep_keybuf && (state==ASSERT_LINE));
1189   bool falling_edge = (m_keep_keybuf && (state==CLEAR_LINE));
1190   m_keep_keybuf = (state==ASSERT_LINE);
13791191
1380/*
1381    Read pins P0-P7 of Geneve 9901.
1382*/
1383static READ8_DEVICE_HANDLER( R9901_2 )
1384{
1385   return 0;
1192   if (rising_edge) signal_when_key_available();
1193   else
1194   {
1195      if (falling_edge)
1196      {
1197         if (m_key_queue_length != 0)
1198         {
1199            m_key_queue_head = (m_key_queue_head + 1) % KEYQUEUESIZE;
1200            m_key_queue_length--;
1201         }
1202         /* clear keyboard interrupt */
1203         m_interrupt(CLEAR_LINE);
1204         m_key_in_buffer = false;
1205      }
1206   }
13861207}
13871208
1388/*
1389    Read pins P8-P15 of Geneve 9901.
1390    bit 4: mouse right button
1391*/
1392static READ8_DEVICE_HANDLER( R9901_3 )
1209WRITE_LINE_MEMBER( geneve_keyboard_device::reset_line )
13931210{
1394   int answer = 0;
1211   m_key_reset = !(state==ASSERT_LINE);
13951212
1396   if (! (input_port_read(device->machine(), "MOUSE0") & 4))
1397      answer |= 0x10;
1398
1399   return answer;
1213   if (m_key_reset)
1214   {
1215      /* reset -> clear keyboard key queue, but not geneve key buffer */
1216      m_key_queue_length = (m_key_in_buffer)? 1 : 0;
1217      m_key_queue_head = 0;
1218      memset(m_key_state_save, 0, sizeof(m_key_state_save));
1219      m_key_numlock_state = false;
1220      m_key_ctrl_state = 0;
1221      m_key_alt_state = 0;
1222      m_key_real_shift_state = 0;
1223      m_key_fake_shift_state = false;
1224      m_key_fake_unshift_state = false;
1225      m_key_autorepeat_key = 0;
1226   }
14001227}
14011228
1402/*
1403    Write PE bus reset line
1404*/
1405static WRITE8_DEVICE_HANDLER( W9901_PE_bus_reset )
1229void geneve_keyboard_device::device_config_complete()
14061230{
1407   if (VERBOSE>0) LOG("genboard: PE bus reset request, ignoring.\n");
1231   // Resolve the callback lines to the console
1232   const geneve_keyboard_config *intf = reinterpret_cast<const geneve_keyboard_config *>(static_config());
1233   m_interrupt.resolve(intf->interrupt, *this);
14081234}
14091235
1410/*
1411    Write VDP reset line
1412*/
1413static WRITE8_DEVICE_HANDLER( W9901_VDP_reset )
1236void geneve_keyboard_device::device_start()
14141237{
1415   if (VERBOSE>0) LOG("genboard: Video reset request, ignoring.\n");
1238   if (VERBOSE>2) LOG("geneve: Keyboard started\n");
1239   m_timer = timer_alloc(0);
14161240}
14171241
1418/*
1419    Write joystick select line
1420*/
1421static WRITE8_DEVICE_HANDLER( W9901_joySel )
1242void geneve_keyboard_device::device_reset()
14221243{
1423   device_t *dev = device->machine().device("geneve_board");
1424   genboard_state *board = get_safe_token(dev);
1425   board->joySel = data;
1244   reset_line(CLEAR_LINE);
1245   m_key_queue_length = 0;
1246   m_key_reset = true;
1247   m_keyboard_clock = false;
1248   m_key_in_buffer = false;
1249   m_keep_keybuf = false;
1250   m_timer->adjust(attotime::from_usec(1), 0, attotime::from_hz(120));
14261251}
14271252
1428static WRITE8_DEVICE_HANDLER( W9901_keyboardReset )
1429{
1430   device_t *dev = device->machine().device("geneve_board");
1431   genboard_state *board = get_safe_token(dev);
1253INPUT_PORTS_START( genkeys )
1254   PORT_START("KEY0")   /* IN3 */
1255   PORT_BIT ( 0x0001, 0x0000, IPT_UNUSED )    /* unused scancode 0 */
1256   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Esc") PORT_CODE(KEYCODE_ESC) /* Esc                       01  81 */
1257   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("1 !") PORT_CODE(KEYCODE_1) /* 1                           02  82 */
1258   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("2 @") PORT_CODE(KEYCODE_2) /* 2                           03  83 */
1259   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("3 #") PORT_CODE(KEYCODE_3) /* 3                           04  84 */
1260   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("4 $") PORT_CODE(KEYCODE_4) /* 4                           05  85 */
1261   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("5 %") PORT_CODE(KEYCODE_5) /* 5                           06  86 */
1262   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("6 ^") PORT_CODE(KEYCODE_6) /* 6                           07  87 */
1263   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("7 &") PORT_CODE(KEYCODE_7) /* 7                           08  88 */
1264   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("8 *") PORT_CODE(KEYCODE_8) /* 8                           09  89 */
1265   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("9 (") PORT_CODE(KEYCODE_9) /* 9                           0A  8A */
1266   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("0 )") PORT_CODE(KEYCODE_0) /* 0                           0B  8B */
1267   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("- _") PORT_CODE(KEYCODE_MINUS) /* -                           0C  8C */
1268   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("= +") PORT_CODE(KEYCODE_EQUALS) /* =                          0D  8D */
1269   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Backspace") PORT_CODE(KEYCODE_BACKSPACE) /* Backspace                 0E  8E */
1270   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Tab") PORT_CODE(KEYCODE_TAB) /* Tab                       0F  8F */
14321271
1433   board->keyReset = !data;
1272   PORT_START("KEY1")   /* IN4 */
1273   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Q") PORT_CODE(KEYCODE_Q) /* Q                         10  90 */
1274   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("W") PORT_CODE(KEYCODE_W) /* W                         11  91 */
1275   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("E") PORT_CODE(KEYCODE_E) /* E                         12  92 */
1276   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("R") PORT_CODE(KEYCODE_R) /* R                         13  93 */
1277   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("T") PORT_CODE(KEYCODE_T) /* T                         14  94 */
1278   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Y") PORT_CODE(KEYCODE_Y) /* Y                         15  95 */
1279   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("U") PORT_CODE(KEYCODE_U) /* U                         16  96 */
1280   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("I") PORT_CODE(KEYCODE_I) /* I                         17  97 */
1281   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("O") PORT_CODE(KEYCODE_O) /* O                         18  98 */
1282   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("P") PORT_CODE(KEYCODE_P) /* P                         19  99 */
1283   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("[ {") PORT_CODE(KEYCODE_OPENBRACE) /* [                           1A  9A */
1284   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("] }") PORT_CODE(KEYCODE_CLOSEBRACE) /* ]                          1B  9B */
1285   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Enter") PORT_CODE(KEYCODE_ENTER) /* Enter                     1C  9C */
1286   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L-Ctrl") PORT_CODE(KEYCODE_LCONTROL) /* Left Ctrl                 1D  9D */
1287   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("A") PORT_CODE(KEYCODE_A) /* A                         1E  9E */
1288   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("S") PORT_CODE(KEYCODE_S) /* S                         1F  9F */
14341289
1435   if (board->keyReset)
1436   {
1437      /* reset -> clear keyboard key queue, but not geneve key buffer */
1438      board->keyQueueLen = (board->keyInBuf)? 1 : 0;
1439      memset(board->keyStateSave, 0, sizeof(board->keyStateSave));
1440      board->keyNumLockState = 0;
1441      board->keyCtrlState = 0;
1442      board->keyAltState = 0;
1443      board->keyRealShiftState = 0;
1444      board->keyFakeShiftState = 0;
1445      board->keyFakeUnshiftState = 0;
1446      board->keyAutoRepeatKey = 0;
1447   }
1448   /*else
1449        poll_keyboard(space->machine());*/
1450}
1290   PORT_START("KEY2")   /* IN5 */
1291   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("D") PORT_CODE(KEYCODE_D) /* D                         20  A0 */
1292   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F") PORT_CODE(KEYCODE_F) /* F                         21  A1 */
1293   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("G") PORT_CODE(KEYCODE_G) /* G                         22  A2 */
1294   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("H") PORT_CODE(KEYCODE_H) /* H                         23  A3 */
1295   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("J") PORT_CODE(KEYCODE_J) /* J                         24  A4 */
1296   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("K") PORT_CODE(KEYCODE_K) /* K                         25  A5 */
1297   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L") PORT_CODE(KEYCODE_L) /* L                         26  A6 */
1298   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("; :") PORT_CODE(KEYCODE_COLON) /* ;                           27  A7 */
1299   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("' \"") PORT_CODE(KEYCODE_QUOTE) /* '                          28  A8 */
1300   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("` ~") PORT_CODE(KEYCODE_TILDE) /* `                           29  A9 */
1301   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L-Shift") PORT_CODE(KEYCODE_LSHIFT) /* Left Shift                 2A  AA */
1302   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("\\ |") PORT_CODE(KEYCODE_BACKSLASH) /* \                          2B  AB */
1303   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Z") PORT_CODE(KEYCODE_Z) /* Z                         2C  AC */
1304   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("X") PORT_CODE(KEYCODE_X) /* X                         2D  AD */
1305   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("C") PORT_CODE(KEYCODE_C) /* C                         2E  AE */
1306   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("V") PORT_CODE(KEYCODE_V) /* V                         2F  AF */
14511307
1452/*
1453    Write external mem cycles (0=long, 1=short)
1454*/
1455static WRITE8_DEVICE_HANDLER( W9901_ext_mem_wait_states )
1456{
1457   if (VERBOSE>0) LOG("genboard: external bus wait states set to %d, not implemented yet.\n", data);
1458}
1308   PORT_START("KEY3")   /* IN6 */
1309   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("B") PORT_CODE(KEYCODE_B) /* B                         30  B0 */
1310   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("N") PORT_CODE(KEYCODE_N) /* N                         31  B1 */
1311   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("M") PORT_CODE(KEYCODE_M) /* M                         32  B2 */
1312   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME(", <") PORT_CODE(KEYCODE_COMMA) /* ,                           33  B3 */
1313   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME(". >") PORT_CODE(KEYCODE_STOP) /* .                            34  B4 */
1314   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("/ ?") PORT_CODE(KEYCODE_SLASH) /* /                           35  B5 */
1315   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("R-Shift") PORT_CODE(KEYCODE_RSHIFT) /* Right Shift                36  B6 */
1316   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP * (PrtScr)") PORT_CODE(KEYCODE_ASTERISK   ) /* Keypad *  (PrtSc)          37  B7 */
1317   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Alt") PORT_CODE(KEYCODE_LALT) /* Left Alt                 38  B8 */
1318   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Space") PORT_CODE(KEYCODE_SPACE) /* Space                     39  B9 */
1319   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Caps") PORT_CODE(KEYCODE_CAPSLOCK) /* Caps Lock                   3A  BA */
1320   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F1") PORT_CODE(KEYCODE_F1) /* F1                          3B  BB */
1321   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F2") PORT_CODE(KEYCODE_F2) /* F2                          3C  BC */
1322   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F3") PORT_CODE(KEYCODE_F3) /* F3                          3D  BD */
1323   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F4") PORT_CODE(KEYCODE_F4) /* F4                          3E  BE */
1324   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F5") PORT_CODE(KEYCODE_F5) /* F5                          3F  BF */
14591325
1460/*
1461    Write vdp wait cycles (1=add 14 cycles, 0=add none)
1462    see above for waitstate handling
1463*/
1464static WRITE8_DEVICE_HANDLER( W9901_VDP_wait_states )
1465{
1466   device_t *dev = device->machine().device("geneve_board");
1467   genboard_state *board = get_safe_token(dev);
1468   if (VERBOSE>1) LOG("genboard: vdp wait states set to %d\n", data);
1469   board->video_waitstates = (data!=0);
1470}
1326   PORT_START("KEY4")   /* IN7 */
1327   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F6") PORT_CODE(KEYCODE_F6) /* F6                          40  C0 */
1328   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F7") PORT_CODE(KEYCODE_F7) /* F7                          41  C1 */
1329   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F8") PORT_CODE(KEYCODE_F8) /* F8                          42  C2 */
1330   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F9") PORT_CODE(KEYCODE_F9) /* F9                          43  C3 */
1331   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F10") PORT_CODE(KEYCODE_F10) /* F10                       44  C4 */
1332   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("NumLock") PORT_CODE(KEYCODE_NUMLOCK) /* Num Lock                  45  C5 */
1333   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("ScrLock (F14)") PORT_CODE(KEYCODE_SCRLOCK) /* Scroll Lock             46  C6 */
1334   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 7 (Home)") PORT_CODE(KEYCODE_7_PAD      ) /* Keypad 7  (Home)           47  C7 */
1335   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 8 (Up)") PORT_CODE(KEYCODE_8_PAD      ) /* Keypad 8  (Up arrow)       48  C8 */
1336   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 9 (PgUp)") PORT_CODE(KEYCODE_9_PAD      ) /* Keypad 9  (PgUp)           49  C9 */
1337   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP -") PORT_CODE(KEYCODE_MINUS_PAD) /* Keypad -                   4A  CA */
1338   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 4 (Left)") PORT_CODE(KEYCODE_4_PAD      ) /* Keypad 4  (Left arrow)     4B  CB */
1339   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 5") PORT_CODE(KEYCODE_5_PAD) /* Keypad 5                   4C  CC */
1340   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 6 (Right)") PORT_CODE(KEYCODE_6_PAD      ) /* Keypad 6  (Right arrow)    4D  CD */
1341   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP +") PORT_CODE(KEYCODE_PLUS_PAD) /* Keypad +                    4E  CE */
1342   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 1 (End)") PORT_CODE(KEYCODE_1_PAD      ) /* Keypad 1  (End)            4F  CF */
14711343
1472/* tms9901 setup */
1473const tms9901_interface tms9901_wiring_geneve =
1474{
1475   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INT8 | TMS9901_INTB | TMS9901_INTC,   /* only input pins whose state is always known */
1344   PORT_START("KEY5")   /* IN8 */
1345   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 2 (Down)") PORT_CODE(KEYCODE_2_PAD      ) /* Keypad 2  (Down arrow)     50  D0 */
1346   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 3 (PgDn)") PORT_CODE(KEYCODE_3_PAD      ) /* Keypad 3  (PgDn)           51  D1 */
1347   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 0 (Ins)") PORT_CODE(KEYCODE_0_PAD      ) /* Keypad 0  (Ins)            52  D2 */
1348   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP . (Del)") PORT_CODE(KEYCODE_DEL_PAD      ) /* Keypad .  (Del)            53  D3 */
1349   PORT_BIT ( 0x0030, 0x0000, IPT_UNUSED )
1350   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(84/102)\\") PORT_CODE(KEYCODE_BACKSLASH2) /* Backslash 2             56  D6 */
1351   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)F11") PORT_CODE(KEYCODE_F11) /* F11                      57  D7 */
1352   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)F12") PORT_CODE(KEYCODE_F12) /* F12                      58  D8 */
1353   PORT_BIT ( 0xfe00, 0x0000, IPT_UNUSED )
14761354
1477   {   /* read handlers */
1478      R9901_0,               // INTA, VDPint, Joystick
1479      R9901_1,               // Keyb, Mouse, Clock, INTB
1480      R9901_2,               // -
1481      R9901_3                  // Mouse right button
1482   },
1355   PORT_START("KEY6")   /* IN9 */
1356   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)KP Enter") PORT_CODE(KEYCODE_ENTER_PAD) /* PAD Enter                 60  e0 */
1357   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)R-Control") PORT_CODE(KEYCODE_RCONTROL) /* Right Control             61  e1 */
1358   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)ALTGR") PORT_CODE(KEYCODE_RALT) /* ALTGR                     64  e4 */
14831359
1484   {   /* write handlers */
1485      W9901_PE_bus_reset,
1486      W9901_VDP_reset,
1487      W9901_joySel,
1488      NULL,
1489      NULL,
1490      NULL,
1491      W9901_keyboardReset,
1492      W9901_ext_mem_wait_states,
1493      NULL,
1494      W9901_VDP_wait_states,
1495      NULL,
1496      NULL,
1497      NULL,
1498      NULL,
1499      NULL,
1500      NULL
1501   },
1360   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)KP /") PORT_CODE(KEYCODE_SLASH_PAD) /* PAD Slash                 62  e2 */
15021361
1503   /* interrupt handler */
1504   tms9901_interrupt_callback,
1362   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Home") PORT_CODE(KEYCODE_HOME) /* Home                       66  e6 */
1363   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Up") PORT_CODE(KEYCODE_UP) /* Up                          67  e7 */
1364   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Page Up") PORT_CODE(KEYCODE_PGUP) /* Page Up                 68  e8 */
1365   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Left") PORT_CODE(KEYCODE_LEFT) /* Left                        69  e9 */
1366   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Right") PORT_CODE(KEYCODE_RIGHT) /* Right                     6a  ea */
1367   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)End") PORT_CODE(KEYCODE_END) /* End                      6b  eb */
1368   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Down") PORT_CODE(KEYCODE_DOWN) /* Down                        6c  ec */
1369   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Page Down") PORT_CODE(KEYCODE_PGDN) /* Page Down                 6d  ed */
1370   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Insert") PORT_CODE(KEYCODE_INSERT) /* Insert                     6e  ee */
1371   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Delete") PORT_CODE(KEYCODE_DEL) /* Delete                        6f  ef */
15051372
1506   /* clock rate = 3MHz */
1507   3000000.
1508};
1373   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)PrtScr (F13)") PORT_CODE(KEYCODE_PRTSCR) /* Print Screen             63  e3 */
1374   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Pause (F15)") PORT_CODE(KEYCODE_PAUSE) /* Pause                      65  e5 */
15091375
1510/*
1511    inta is connected to both tms9901 IRQ1 line and to tms9995 INT4/EC line.
1512*/
1513WRITE_LINE_DEVICE_HANDLER( board_inta )
1376   PORT_START("KEY7")   /* IN10 */
1377   PORT_BIT ( 0xffff, 0x0000, IPT_UNUSED )
1378#if 0
1379   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Print Screen") PORT_CODE(KEYCODE_PRTSCR) /* Print Screen alternate        77  f7 */
1380   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Left Win") /* Left Win                    7d  fd */
1381   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Right Win") /* Right Win                  7e  fe */
1382   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Menu") /* Menu                        7f  ff */
1383#endif
1384INPUT_PORTS_END
1385
1386ioport_constructor geneve_keyboard_device::device_input_ports() const
15141387{
1515   tms9901_set_single_int(device->machine().device("tms9901"), 1, state);
1516   cputag_set_input_line(device->machine(), "maincpu", 1, state);
1388   return INPUT_PORTS_NAME( genkeys );
15171389}
15181390
1519/*
1520    intb is connected to tms9901 IRQ12 line.
1521*/
1522WRITE_LINE_DEVICE_HANDLER( board_intb )
1523{
1524   tms9901_set_single_int(device->machine().device("tms9901"), 12, state);
1391const device_type GENEVE_KEYBOARD = &device_creator<geneve_keyboard_device>;
15251392
1526}
1393/****************************************************************************
1394    Mouse support
1395****************************************************************************/
15271396
1528WRITE_LINE_DEVICE_HANDLER( board_ready )
1397geneve_mouse_device::geneve_mouse_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
1398: device_t(mconfig, GENEVE_MOUSE, "Geneve mouse", tag, owner, clock)
15291399{
1530   if (VERBOSE>0) LOG("genboard: READY line set ... not yet connected, level=%02x\n", state);
15311400}
15321401
1533/*
1534    set the state of int2 (called by the v9938 core)
1535*/
1536void tms9901_gen_set_int2(device_t *, v99x8_device &tms, int state)
1402line_state geneve_mouse_device::left_button()
15371403{
1538   tms9901_set_single_int(tms.machine().device("tms9901"), 2, state);
1404   return ((input_port_read(*this, "MOUSE0") & 0x04)!=0)? ASSERT_LINE : CLEAR_LINE;
15391405}
15401406
1541/***************************************************************************
1542    DEVICE LIFECYCLE FUNCTIONS
1543***************************************************************************/
1544
1545static const mm58274c_interface geneve_mm58274c_interface =
1407void geneve_mouse_device::poll()
15461408{
1547   1,   /*  mode 24*/
1548   0   /*  first day of week */
1549};
1409   int new_mx, new_my;
1410   int delta_x, delta_y, buttons;
15501411
1551static MACHINE_CONFIG_FRAGMENT( genboard )
1552   MCFG_MM58274C_ADD("mm58274c", geneve_mm58274c_interface)
1553MACHINE_CONFIG_END
1412   buttons = input_port_read(*this, "MOUSE0");
1413   new_mx = input_port_read(*this, "MOUSEX");
1414   new_my = input_port_read(*this, "MOUSEY");
15541415
1555static DEVICE_START( genboard )
1556{
1557   if (VERBOSE>0) LOG("genboard: Starting Geneve board\n");
1558   genboard_state *board = get_safe_token(device);
1416   /* compute x delta */
1417   delta_x = new_mx - m_last_mx;
15591418
1560   board->video = device->siblingdevice("video");
1561   board->tms9901 = device->siblingdevice("tms9901");
1562   board->peribox = device->siblingdevice("peribox");
1563   board->cpu = device->siblingdevice("maincpu");
1564   board->sound = device->siblingdevice("soundgen");
1419   /* check for wrap */
1420   if (delta_x > 0x80)
1421      delta_x = 0x100-delta_x;
1422   if  (delta_x < -0x80)
1423      delta_x = -0x100-delta_x;
15651424
1566   board->clock = device->subdevice("mm58274c");
1425   m_last_mx = new_mx;
15671426
1568   board->sram = (UINT8*)malloc(SRAM_SIZE);
1569   board->dram = (UINT8*)malloc(DRAM_SIZE);
1570   board->eprom = device->machine().region("maincpu")->base();
1571   assert(board->video && board->tms9901);
1572   assert(board->peribox && board->cpu);
1573   assert(board->sound && board->clock);
1574   assert(board->eprom);
1575   board->geneve_mode = false;
1576   board->direct_mode = true;
1427   /* compute y delta */
1428   delta_y = new_my - m_last_my;
15771429
1578   // Used for generating the VDP waitstates. These are created in the
1579   // Gate Array on the board and only affect external memory accesses.
1580   // Therefore we cannot use the usual adjust_icount
1581   board->video_waittimer = device->machine().scheduler().timer_alloc(FUNC(video_wait_callback), (void *)device);
1430   /* check for wrap */
1431   if (delta_y > 0x80)
1432      delta_y = 0x100-delta_y;
1433   if  (delta_y < -0x80)
1434      delta_y = -0x100-delta_y;
15821435
1583   memset(board->sram, 0x00, SRAM_SIZE);
1584   memset(board->dram, 0x00, DRAM_SIZE);
1585}
1436   m_last_my = new_my;
15861437
1587static DEVICE_STOP( genboard )
1588{
1589   if (VERBOSE>0) LOG("genboard: Stopping Geneve board\n");
1590   genboard_state *board = get_safe_token(device);
1591   free(board->sram);
1592   free(board->dram);
1438   // only middle and right button go to V9938
1439   m_v9938->update_mouse_state(delta_x, delta_y, buttons & 0x03);
15931440}
15941441
1595static DEVICE_RESET( genboard )
1596{
1597   genboard_state *board = get_safe_token(device);
1598   if (VERBOSE>0) LOG("genboard: Resetting Geneve board\n");
1599   board->joySel = 0;
1600   board->keyQueueHead = board->keyQueueLen = 0;
1601   memset(board->keyStateSave, 0, sizeof(board->keyStateSave));
1602   board->keyNumLockState = 0;
1603   board->keyCtrlState = 0;
1604   board->keyAltState = 0;
1605   board->keyRealShiftState = 0;
1606   board->keyFakeShiftState = 0;
1607   board->keyFakeUnshiftState = 0;
1608   board->keyAutoRepeatKey = 0;
1609   board->keyInBuf = false;
1610   board->keyReset = 1;
1611   board->last_mx = 0;
1612   board->last_my = 0;
1613   board->line_count = 0;
1442INPUT_PORTS_START( genmouse )
1443   PORT_START("MOUSEX") /* Mouse - X AXIS */
1444      PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
16141445
1615   board->palvideo = false;
1616   board->capslock = false;
1617   board->keyboard_clock = false;
1618   board->keep_keybuf = false;
1619   board->extra_waitstates = false;
1620   board->video_waitstates = true;
1621   board->video_waiting = false;
1446   PORT_START("MOUSEY") /* Mouse - Y AXIS */
1447      PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
16221448
1623   board->geneve_mode =false;
1624   board->direct_mode = true;
1625   board->cartridge_size_8K = false;
1626   board->cartridge_secondpage = false;
1627   board->cartridge6_writable = false;
1628   board->cartridge7_writable = false;
1629   for (int i=0; i < 8; i++) board->map[i] = 0;
1449   PORT_START("MOUSE0") /* mouse buttons */
1450      PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1) PORT_NAME("Left mouse button")
1451      PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON2) PORT_NAME("Right mouse button")
1452      PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON3) PORT_NAME("Middle mouse button")
1453INPUT_PORTS_END
16301454
1631   board->genmod = false;
1632   if (input_port_read(device->machine(), "MODE")==0)
1633   {
1634      switch (input_port_read(device->machine(), "BOOTROM"))
1635      {
1636      case GENEVE_098:
1637         if (VERBOSE>0) LOG("genboard: Using 0.98 boot eprom\n");
1638         board->eprom = device->machine().region("maincpu")->base() + 0x4000;
1639         break;
1640      case GENEVE_100:
1641         if (VERBOSE>0) LOG("genboard: Using 1.00 boot eprom\n");
1642         board->eprom = device->machine().region("maincpu")->base();
1643         break;
1644      }
1645   }
1646   else
1647   {
1648      if (VERBOSE>0) LOG("genboard: Using GenMod modification\n");
1649      board->eprom = device->machine().region("maincpu")->base() + 0x8000;
1650      if (board->eprom[0] != 0xf0)
1651      {
1652         fatalerror("genboard: GenMod boot rom missing.\n");
1653      }
1654      board->genmod = true;
1655      board->turbo = ((input_port_read(device->machine(), "GENMODDIPS") & GM_TURBO)!=0);
1656      board->timode = ((input_port_read(device->machine(), "GENMODDIPS") & GM_TIM)!=0);
1657   }
1455void geneve_mouse_device::device_start()
1456{
1457   m_v9938 = machine().device<v9938_device>(V9938_TAG);
1458}
16581459
1659   switch (input_port_read(device->machine(), "SRAM"))
1660   {
1661/*  1 100. .... .... .... .... on-board sram (128K) -+
1662    1 101. .... .... .... .... on-board sram (128K) -+-- maximum SRAM expansion
1663    1 1100 .... .... .... .... on-board sram (64K) --+
1664    1 1101 0... .... .... .... on-board sram (32K) - additional 32 KiB required for MDOS 2.50s and higher
1665    1 1101 1... .... .... .... on-board sram (32K) - standard setup
1666*/
1667   case 0: // 32 KiB
1668      board->sram_mask =   0x1f8000;
1669      board->sram_val =   0x1d8000;
1670      break;
1671   case 1: // 64 KiB
1672      board->sram_mask =   0x1f0000;
1673      board->sram_val =   0x1d0000;
1674      break;
1675   case 2: // 384 KiB (actually 512 KiB, but the EPROM masks the upper 128 KiB)
1676      board->sram_mask =   0x180000;
1677      board->sram_val =   0x180000;
1678      break;
1679   }
1460void geneve_mouse_device::device_reset()
1461{
1462   m_last_mx = 0;
1463   m_last_my = 0;
16801464}
16811465
1682static const char DEVTEMPLATE_SOURCE[] = __FILE__;
1466ioport_constructor geneve_mouse_device::device_input_ports() const
1467{
1468   return INPUT_PORTS_NAME( genmouse );
1469}
16831470
1684#define DEVTEMPLATE_ID(p,s)             p##genboard##s
1685#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_MACHINE_CONFIG
1686#define DEVTEMPLATE_NAME                "Geneve system board"
1687#define DEVTEMPLATE_FAMILY              "Internal component"
1688#include "devtempl.h"
1471const device_type GENEVE_MOUSE = &device_creator<geneve_mouse_device>;
16891472
1690DEFINE_LEGACY_DEVICE( GENBOARD, genboard );
src/mess/machine/ti99/hsgpl.h
r14513r14514
1/*
1/****************************************************************************
2
23    SNUG HSGPL card emulation.
4    See hsgpl.c for documentation
5
36    Raphael Nabet, 2003.
47
5    Rewritten as device
6    Michael Zapf, 2010
7*/
8    Michael Zapf
9    October 2010: Rewritten as device
10    February 2012: Rewritten as class
811
9/* device interface */
12*****************************************************************************/
13
1014#ifndef __HSGPL__
1115#define __HSGPL__
1216
13DECLARE_LEGACY_DEVICE( HSGPL, hsgpl );
17#include "emu.h"
18#include "ti99defs.h"
19#include "peribox.h"
20#include "machine/at29040a.h"
21
22extern const device_type TI99_HSGPL;
23
24class snug_high_speed_gpl_device : public ti_expansion_card_device
25{
26public:
27   snug_high_speed_gpl_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
28   DECLARE_READ8Z_MEMBER(readz);
29   DECLARE_WRITE8_MEMBER(write);
30
31   void   crureadz(offs_t offset, UINT8 *value);
32   void   cruwrite(offs_t offset, UINT8 data);
33
34protected:
35   void device_start(void);
36   void device_reset(void);
37   void device_stop(void);
38   void device_config_complete(void);
39   ioport_constructor device_input_ports() const;
40
41   machine_config_constructor device_mconfig_additions() const;
42
43private:
44   at29040a_device* m_dsr_eeprom;
45   at29040a_device* m_rom6_eeprom;
46   at29040a_device* m_grom_a_eeprom;
47   at29040a_device* m_grom_b_eeprom;
48
49   UINT8*         m_ram6_memory;
50   UINT8*         m_gram_memory;
51
52   void         dsrspace_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask);
53   void         cartspace_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask);
54   void         grom_readz(address_space& space, offs_t offset, UINT8* value, UINT8 mem_mask);
55
56   void         cartspace_write(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask);
57   void         grom_write(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask);
58
59   bool         m_dsr_enabled;
60   bool         m_gram_enabled;
61   bool         m_bank_enabled;
62   int            m_dsr_page;
63   bool         m_card_enabled;
64   bool         m_write_enabled;
65   bool         m_supercart_enabled;
66   bool         m_led_on;
67   bool         m_mbx_enabled;
68   bool         m_ram_enabled;
69   bool         m_flash_mode;
70
71   int            m_current_grom_port;
72   int            m_current_bank;
73
74   // GROM emulation
75   bool         m_waddr_LSB;
76   bool         m_raddr_LSB;
77   int            m_grom_address;
78};
79
1480#endif
1581
src/mess/machine/ti99/genboard.h
r14513r14514
1/****************************************************************************
2
3    Geneve main board components.
4    See genboard.c for documentation.
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
111#ifndef __GENBOARD__
212#define __GENBOARD__
313
4#include "machine/tms9901.h"
14#include "emu.h"
15#include "ti99defs.h"
516#include "video/v9938.h"
617
7enum
18extern const device_type GENEVE_MOUSE;
19extern const device_type GENEVE_KEYBOARD;
20extern const device_type GENEVE_MAPPER;
21
22/*****************************************************************************/
23
24class geneve_mouse_device : public device_t
825{
9   MDIP1 = 0x01,
10   MDIP2 = 0x02,
11   MDIP3 = 0x04,
12   MDIP4 = 0x08,
13   MDIP5 = 0x10,
14   MDIP6 = 0x20,
15   MDIP7 = 0x40,
16   MDIP8 = 0x80
26public:
27   geneve_mouse_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
28   void poll();
29   line_state   left_button();   // left button is not connected to the V9938 but to a TMS9901 pin
30
31protected:
32   void device_start();
33   void device_reset();
34   ioport_constructor device_input_ports() const;
35
36private:
37   v9938_device*   m_v9938;
38   int            m_last_mx;
39   int            m_last_my;
1740};
1841
19#define GENMOD 0x01
42#define MCFG_GENEVE_MOUSE_ADD(_tag )   \
43   MCFG_DEVICE_ADD(_tag, GENEVE_MOUSE, 0)
2044
21READ8_DEVICE_HANDLER( geneve_r );
22WRITE8_DEVICE_HANDLER( geneve_w );
23READ8_DEVICE_HANDLER( geneve_cru_r );
24WRITE8_DEVICE_HANDLER( geneve_cru_w );
45/*****************************************************************************/
2546
26extern const tms9901_interface tms9901_wiring_geneve;
27void tms9901_gen_set_int2(device_t *, v99x8_device &tms, int state);
28void set_gm_switches(device_t *board, int number, int value);
47typedef struct _geneve_keyboard_config
48{
49   devcb_write_line   interrupt;
50} geneve_keyboard_config;
2951
30TIMER_DEVICE_CALLBACK( geneve_hblank_interrupt );
52#define GENEVE_KEYBOARD_CONFIG(name) \
53   const geneve_keyboard_config(name) =
3154
32/* device interface */
33DECLARE_LEGACY_DEVICE( GENBOARD, genboard );
3455
35WRITE_LINE_DEVICE_HANDLER( board_inta );
36WRITE_LINE_DEVICE_HANDLER( board_intb );
37WRITE_LINE_DEVICE_HANDLER( board_ready );
56#define KEYQUEUESIZE 256
57#define MAXKEYMSGLENGTH 10
58#define KEYAUTOREPEATDELAY 30
59#define KEYAUTOREPEATRATE 6
3860
39enum
61class geneve_keyboard_device : public device_t
4062{
41   GENEVE_098 = 0,
42   GENEVE_100 = 1
63public:
64   geneve_keyboard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
65   DECLARE_WRITE_LINE_MEMBER( reset_line );
66   DECLARE_WRITE_LINE_MEMBER( send_scancodes );
67   DECLARE_WRITE_LINE_MEMBER( clock_control );
68   UINT8 get_recent_key();
69
70protected:
71   void                  device_start();
72   void                  device_reset();
73   void                  device_config_complete();
74   ioport_constructor         device_input_ports() const;
75   devcb_resolved_write_line   m_interrupt;   // Keyboard interrupt to console
76   void                  device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
77
78private:
79   void   post_in_key_queue(int keycode);
80   void   signal_when_key_available();
81   void   poll();
82
83   bool   m_key_reset;
84   int      m_key_queue_length;
85   UINT8   m_key_queue[KEYQUEUESIZE];
86   int    m_key_queue_head;
87   bool   m_key_in_buffer;
88   UINT32   m_key_state_save[4];
89   bool   m_key_numlock_state;
90
91   int    m_key_ctrl_state;
92   int    m_key_alt_state;
93   int    m_key_real_shift_state;
94
95   bool   m_key_fake_shift_state;
96   bool   m_key_fake_unshift_state;
97
98   int    m_key_autorepeat_key;
99   int    m_key_autorepeat_timer;
100
101   bool   m_keep_keybuf;
102   bool   m_keyboard_clock;
103
104   emu_timer*      m_timer;
43105};
44106
45#define MCFG_GENEVE_BOARD_ADD(_tag )         \
46   MCFG_DEVICE_ADD(_tag, GENBOARD, 0)
107#define MCFG_GENEVE_KEYBOARD_ADD(_tag, _intf )   \
108   MCFG_DEVICE_ADD(_tag, GENEVE_KEYBOARD, 0)   \
109   MCFG_DEVICE_CONFIG(_intf)
47110
111/*****************************************************************************/
112
113class geneve_mapper_device : public device_t
114{
115public:
116   geneve_mapper_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
117   void set_geneve_mode(bool geneve) { m_geneve_mode = geneve; }
118   void set_direct_mode(bool direct) { m_direct_mode = direct; }
119   void set_cartridge_size(int size) { m_cartridge_size = size; }
120   void set_cartridge_writable(int base, bool write)
121   {
122      if (base==0x6000)  m_cartridge6_writable = write;
123      else  m_cartridge7_writable = write;
124   }
125   void set_video_waitstates(bool wait) { m_video_waitstates = wait; }
126   void set_extra_waitstates(bool wait) { m_extra_waitstates = wait; }
127   void start_video_wait(int ws);
128   void do_wait(int min);
129
130   DECLARE_READ8_MEMBER( readm );
131   DECLARE_WRITE8_MEMBER( writem );
132
133   DECLARE_INPUT_CHANGED_MEMBER( gm_changed );
134
135protected:
136   void   device_start();
137   void   device_reset();
138
139private:
140   // GROM simulation
141   bool   m_gromwaddr_LSB;
142   bool   m_gromraddr_LSB;
143   int      m_grom_address;
144   DECLARE_READ8_MEMBER( read_grom );
145   DECLARE_WRITE8_MEMBER( write_grom );
146   void   device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
147
148   // wait states
149   emu_timer   *m_video_waittimer;
150   bool      m_video_waiting;
151   bool      m_video_waitstates;
152   bool      m_extra_waitstates;
153
154   // Mapper function
155   bool   m_geneve_mode;
156   bool   m_direct_mode;
157   int      m_cartridge_size;
158   bool   m_cartridge_secondpage;
159   bool   m_cartridge6_writable;
160   bool   m_cartridge7_writable;
161   int      m_map[8];
162
163   // Genmod modifications
164   bool   m_turbo;
165   bool   m_genmod;
166   bool   m_timode;
167
168   int      m_sram_mask;
169   int      m_sram_val;
170
171   // Devices
172   device_t*      m_clock;
173   device_t*      m_cpu;
174   geneve_keyboard_device*   m_keyboard;
175   bus8z_device*   m_video;
176   bus8z_device*   m_peribox;
177   bus8z_device*   m_sound;
178   UINT8*         m_eprom;
179   UINT8*         m_sram;
180   UINT8*         m_dram;
181};
182
183#define MCFG_GENEVE_MAPPER_ADD(_tag )   \
184   MCFG_DEVICE_ADD(_tag, GENEVE_MAPPER, 0)
48185#endif
src/mess/machine/ti99/ti_fdc.c
r14513r14514
1/*
2    TI Standard Floppy Disk Controller
3    September 2010
1/****************************************************************************
42
3    TI-99 Standard Floppy Disk Controller Card
4    Based on WD1771
5    Single Density, Double-sided
6
57    Michael Zapf
6*/
8    September 2010
9    January 2012: rewritten as class (MZ)
10
11****************************************************************************/
12
713#include "emu.h"
814#include "peribox.h"
915#include "ti_fdc.h"
1016#include "machine/wd17xx.h"
1117#include "formats/ti99_dsk.h"
12#include "imagedev/flopdrv.h"
1318
14#define CRU_BASE 0x1100
19#define LOG logerror
20#define VERBOSE 1
1521
1622#define fdc_IRQ 1
1723#define fdc_DRQ 2
1824
19#define fdc_region "fdc_region"
25#define TI_FDC_TAG "ti_dssd_controller"
2026
21typedef ti99_pebcard_config ti99_fdc_config;
27#define FDC_TAG "wd1771"
2228
23typedef struct _ti99_fdc_state
24{
25   /* Holds the status of the DRQ and IRQ lines. */
26   int               DRQ_IRQ_status;
29#define MOTOR_TIMER 1
2730
28   /* When TRUE, card is accessible. Indicated by a LED. */
29   int               selected;
30
31   /* Used for GenMod. */
32   int               select_mask;
33   int               select_value;
34
35   /* When TRUE, keeps DVENA high. */
36   int               strobe_motor;
37
38   /* Signal DVENA. When TRUE, makes some drive turning. */
39   line_state         DVENA;
40
41   /* When TRUE the CPU is halted while DRQ/IRQ are true. */
42   int               hold;
43
44   /* Indicates which drive has been selected. Values are 0, 1, 2, and 4. */
45   // 000 = no drive
46   // 001 = drive 1
47   // 010 = drive 2
48   // 100 = drive 3
49   int               DSEL;
50
51   /* Signal SIDSEL. 0 or 1, indicates the selected head. */
52   int               SIDSEL;
53
54   /* count 4.23s from rising edge of motor_on */
55   emu_timer         *motor_on_timer;
56
57   /* Link to the FDC1771 controller on the board. */
58   device_t      *controller;
59
60   /* DSR ROM */
61   UINT8            *rom;
62
63   /* Callback lines to the main system. */
64   ti99_peb_connect   lines;
65
66} ti99_fdc_state;
67
68/* Those defines are required because the WD17xx DEVICE_START implementation
69assumes that the floppy devices are either at root level or at the parent
70level. Our floppy devices, however, are at the grandparent level as seen from
71the controller. */
72
73#define PFLOPPY_0 ":peribox:floppy0"
74#define PFLOPPY_1 ":peribox:floppy1"
75#define PFLOPPY_2 ":peribox:floppy2"
76
77INLINE ti99_fdc_state *get_safe_token(device_t *device)
31const wd17xx_interface ti_wd17xx_interface =
7832{
79   assert(device != NULL);
80   assert(device->type() == TIFDC);
33   DEVCB_NULL,
34   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_fdc_device, intrq_w),
35   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_fdc_device, drq_w),
36   { PFLOPPY_0, PFLOPPY_1, PFLOPPY_2, NULL }
37};
8138
82   return (ti99_fdc_state *)downcast<legacy_device_base *>(device)->token();
39ti_fdc_device::ti_fdc_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
40: ti_expansion_card_device(mconfig, TI99_FDC, "TI-99 Standard DSSD Floppy Controller", tag, owner, clock)
41{
42   m_shortname = "ti99_fdc";
8343}
8444
8545/*
86    Call this when the state of DSKhold or DRQ/IRQ or DVENA change
87
88    Emulation is faulty because the CPU is actually stopped in the midst of
89    instruction, at the end of the memory access
90
91    TODO: This has to be replaced by the proper READY handling that is already
92    prepared here. (Requires READY handling by the CPU.)
46    callback called at the end of DVENA pulse
9347*/
94static void fdc_handle_hold(device_t *device)
48void ti_fdc_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
9549{
96   ti99_fdc_state *card = get_safe_token(device);
97   line_state state;
98
99   if (card->hold && (!card->DRQ_IRQ_status) && (card->DVENA==ASSERT_LINE))
100      state = ASSERT_LINE;
101   else
102      state = CLEAR_LINE;
103
104   // TODO: use READY
105   cputag_set_input_line(device->machine(), "maincpu", INPUT_LINE_HALT, state);
50   m_DVENA = CLEAR_LINE;
51   handle_hold();
10652}
10753
108/*
109    Resets the drive geometry. This is required because the heuristic of
110    the default implementation sets the drive geometry to the geometry
111    of the medium.
112*/
113static void set_geometry(device_t *drive, floppy_type_t type)
54READ8Z_MEMBER(ti_fdc_device::readz)
11455{
115   if (drive!=NULL)
116      floppy_drive_set_geometry(drive, type);
117   else
118      logerror("ti99/FDC: Drive not found\n");
56   if (m_selected)
57   {
58      if ((offset & m_select_mask)==m_select_value)
59      {
60         // only use the even addresses from 1ff0 to 1ff6.
61         // Note that data is inverted.
62         // 0101 1111 1111 0xx0
63         UINT8 reply = 0;
64
65         if ((offset & 0x1ff9)==0x1ff0)
66         {
67            reply = wd17xx_r(m_controller, (offset >> 1)&0x03);
68         }
69         else
70         {
71            reply = m_dsrrom[offset & 0x1fff];
72         }
73         *value = reply;
74         if (VERBOSE>5) LOG("ti_fdc: %04x -> %02x\n", offset & 0xffff, *value);
75      }
76   }
11977}
12078
121static void set_all_geometries(device_t *device, floppy_type_t type)
79WRITE8_MEMBER(ti_fdc_device::write)
12280{
123   set_geometry(device->machine().device(PFLOPPY_0), type);
124   set_geometry(device->machine().device(PFLOPPY_1), type);
125   set_geometry(device->machine().device(PFLOPPY_2), type);
81   if (m_selected)
82   {
83      if ((offset & m_select_mask)==m_select_value)
84      {
85         if (VERBOSE>5) LOG("ti_fdc: %04x <- %02x\n", offset & 0xffff, data);
86         // only use the even addresses from 1ff8 to 1ffe.
87         // Note that data is inverted.
88         // 0101 1111 1111 1xx0
89         if ((offset & 0x1ff9)==0x1ff8)
90         {
91            wd17xx_w(m_controller, (offset >> 1)&0x03, data);
92         }
93      }
94   }
12695}
12796
128
129/*************************************************************************/
130
13197/*
13298    The CRU read handler.
13399    bit 0: HLD pin
r14513r14514
137103    bit 6: always 1
138104    bit 7: selected side
139105*/
140static READ8Z_DEVICE_HANDLER( cru_r )
106void ti_fdc_device::crureadz(offs_t offset, UINT8 *value)
141107{
142   ti99_fdc_state *card = get_safe_token(device);
143
144   if ((offset & 0xff00)==CRU_BASE)
108   if ((offset & 0xff00)==m_cru_base)
145109   {
146110      int addr = offset & 0x07;
147111      UINT8 reply = 0;
r14513r14514
150114         // deliver bits 0-7
151115         // TODO: HLD pin
152116         // The DVENA state is returned inverted
153         if (card->DVENA==ASSERT_LINE) reply |= ((card->DSEL)<<1);
117         if (m_DVENA==ASSERT_LINE) reply |= ((m_DSEL)<<1);
154118         else reply |= 0x10;
155119         reply |= 0x40;
156         if (card->SIDSEL) reply |= 0x80;
120         if (m_SIDSEL) reply |= 0x80;
157121      }
158122      *value = reply;
159123   }
160124}
161125
162static WRITE8_DEVICE_HANDLER( cru_w )
126void ti_fdc_device::cruwrite(offs_t offset, UINT8 data)
163127{
164   ti99_fdc_state *card = get_safe_token(device);
165128   int drive, drivebit;
166129
167   if ((offset & 0xff00)==CRU_BASE)
130   if ((offset & 0xff00)==m_cru_base)
168131   {
169132      int bit = (offset >> 1) & 0x07;
170133      switch (bit)
171134      {
172135      case 0:
173136         /* (De)select the card. Indicated by a LED on the board. */
174         card->selected = data;
137         m_selected = (data!=0);
138         if (VERBOSE>4) LOG("ti_fdc: Map DSR = %d\n", m_selected);
175139         break;
176140      case 1:
177141         /* Activate motor */
178         if (data && !card->strobe_motor)
142         if (data && !m_strobe_motor)
179143         {   /* on rising edge, set motor_running for 4.23s */
180            card->DVENA = ASSERT_LINE;
181            fdc_handle_hold(device);
182            card->motor_on_timer->adjust(attotime::from_msec(4230));
144            m_DVENA = ASSERT_LINE;
145            handle_hold();
146            m_motor_on_timer->adjust(attotime::from_msec(4230));
183147         }
184         card->strobe_motor = data;
148         m_strobe_motor = (data!=0);
185149         break;
186150
187151      case 2:
r14513r14514
191155         // OR the motor stops rotating - rotates for 4.23s after write
192156         // to CRU bit 1
193157         // This is not emulated and could cause the TI99 to lock up
194         card->hold = data;
195         fdc_handle_hold(device);
158         m_hold = (data != 0);
159         handle_hold();
196160         break;
197161
198162      case 3:
r14513r14514
208172
209173         if (data)
210174         {
211            if (!(card->DSEL & drivebit))         /* select drive */
175            if ((m_DSEL & drivebit)!=0)         /* select drive */
212176            {
213               if (card->DSEL != 0)
214                  logerror("ti_fdc: Multiple drives selected, %02x\n", card->DSEL);
215               card->DSEL |= drivebit;
216               wd17xx_set_drive(card->controller, drive);
177               if (m_DSEL != 0)
178                  LOG("ti_fdc: Multiple drives selected, %02x\n", m_DSEL);
179               m_DSEL |= drivebit;
180               wd17xx_set_drive(m_controller, drive);
217181               /*wd17xx_set_side(DSKside);*/
218182            }
219183         }
220184         else
221            card->DSEL &= ~drivebit;
185            m_DSEL &= ~drivebit;
222186         break;
223187
224188      case 7:
225189         /* Select side of disk (bit 7) */
226         card->SIDSEL = data;
227         wd17xx_set_side(card->controller, data);
190         m_SIDSEL = data;
191         wd17xx_set_side(m_controller, data);
228192         break;
229193      }
230194   }
231195}
232196
233197/*
234    Read a byte from the ROM or from the controller
198    Call this when the state of DSKhold or DRQ/IRQ or DVENA change
199
200    Emulation is faulty because the CPU is actually stopped in the midst of
201    instruction, at the end of the memory access
202
203    TODO: This has to be replaced by the proper READY handling that is already
204    prepared here. (Requires READY handling by the CPU.)
235205*/
236static READ8Z_DEVICE_HANDLER( data_r )
206void ti_fdc_device::handle_hold()
237207{
238   ti99_fdc_state *card = get_safe_token(device);
208   line_state state;
239209
240   if (card->selected)
241   {
242      if ((offset & card->select_mask)==card->select_value)
243      {
244         // only use the even addresses from 1ff0 to 1ff6.
245         // Note that data is inverted.
246         // 0101 1111 1111 0xx0
247         UINT8 reply = 0;
210   if (m_hold && !m_DRQ && !m_IRQ && (m_DVENA==ASSERT_LINE))
211      state = ASSERT_LINE;
212   else
213      state = CLEAR_LINE;
248214
249         if ((offset & 0x1ff9)==0x1ff0)
250            reply = wd17xx_r(card->controller, (offset >> 1)&0x03);
251         else
252            reply = card->rom[offset & 0x1fff];
253
254         *value = reply;
255      }
256   }
215   // TODO: use READY
216   cputag_set_input_line(machine(), "maincpu", INPUT_LINE_HALT, state);
257217}
258218
259219/*
260    Write a byte to the controller.
220    Resets the drive geometry. This is required because the heuristic of
221    the default implementation sets the drive geometry to the geometry
222    of the medium.
261223*/
262static WRITE8_DEVICE_HANDLER( data_w )
224void ti_fdc_device::set_geometry(device_t *drive, floppy_type_t type)
263225{
264   ti99_fdc_state *card = get_safe_token(device);
265   if (card->selected)
266   {
267      if ((offset & card->select_mask)==card->select_value)
268      {
269         // only use the even addresses from 1ff8 to 1ffe.
270         // Note that data is inverted.
271         // 0101 1111 1111 1xx0
272         if ((offset & 0x1ff9)==0x1ff8)
273            wd17xx_w(card->controller, (offset >> 1)&0x03, data);
274      }
275   }
226   // This assertion may fail when the names of the floppy devices change.
227   // Unfortunately, the wd17xx device assumes the floppy drives at root
228   // level, so we use an explicitly qualified tag. See peribox.h.
229   assert (drive!=NULL);
230   floppy_drive_set_geometry(drive, type);
276231}
277232
233void ti_fdc_device::set_all_geometries(floppy_type_t type)
234{
235   set_geometry(machine().device(PFLOPPY_0), type);
236   set_geometry(machine().device(PFLOPPY_1), type);
237   set_geometry(machine().device(PFLOPPY_2), type);
238}
239
278240/*
279241    Callback, called from the controller chip whenever DRQ/IRQ state change
280242*/
281static WRITE_LINE_DEVICE_HANDLER( ti_fdc_intrq_w )
243WRITE_LINE_MEMBER( ti_fdc_device::intrq_w )
282244{
283   device_t *carddev = device->owner();
284   ti99_fdc_state *card = get_safe_token(carddev);
245   if (VERBOSE>8) LOG("ti_fdc: set irq = %02x\n", state);
246   m_IRQ = (state==ASSERT_LINE);
247   // Note that INTB is actually not used in the TI-99 family. But the
248   // controller asserts the line nevertheless, probably intended for
249   // use in another planned TI system
250   m_slot->set_intb(state);
285251
286   if (state==ASSERT_LINE)
287   {
288      // Note that INTB is actually not used in the TI-99 family. But the
289      // controller asserts the line nevertheless
290      card->DRQ_IRQ_status |= fdc_IRQ;
291   }
292   else
293   {
294      card->DRQ_IRQ_status &= ~fdc_IRQ;
295   }
296   // INTB* is active low; gets inverted on the board
297   card->lines.intb(state);
298   fdc_handle_hold(carddev);
252   handle_hold();
299253}
300254
301static WRITE_LINE_DEVICE_HANDLER( ti_fdc_drq_w )
255WRITE_LINE_MEMBER( ti_fdc_device::drq_w )
302256{
303   device_t *carddev = device->owner();
304   ti99_fdc_state *card = get_safe_token(carddev);
305
306   if (state)
307      card->DRQ_IRQ_status |= fdc_DRQ;
308   else
309      card->DRQ_IRQ_status &= ~fdc_DRQ;
310
311   fdc_handle_hold(carddev);
257   if (VERBOSE>8) LOG("ti_fdc: set drq = %02x\n", state);
258   m_DRQ = (state == ASSERT_LINE);
259   handle_hold();
312260}
313261
314/*
315    callback called at the end of DVENA pulse
316*/
317static TIMER_CALLBACK(motor_on_timer_callback)
262void ti_fdc_device::device_start(void)
318263{
319   device_t *device = (device_t *)ptr;
320   ti99_fdc_state *card = get_safe_token(device);
321   card->DVENA = CLEAR_LINE;
322   fdc_handle_hold(device);
323}
264   if (VERBOSE>5) LOG("ti_fdc: TI FDC start\n");
265   m_dsrrom = subregion(DSRROM)->base();
266   m_motor_on_timer = timer_alloc(MOTOR_TIMER);
267   m_controller = subdevice(FDC_TAG);
324268
325const wd17xx_interface ti_wd17xx_interface =
326{
327   DEVCB_NULL,
328   DEVCB_LINE(ti_fdc_intrq_w),
329   DEVCB_LINE(ti_fdc_drq_w),
330   { PFLOPPY_0, PFLOPPY_1, PFLOPPY_2, NULL }
331};
332
333static const ti99_peb_card fdc_card =
334{
335   data_r,
336   data_w,
337   cru_r,
338   cru_w,
339
340   NULL, NULL,   NULL, NULL
341};
342
343static DEVICE_START( ti99_fdc )
344{
345   ti99_fdc_state *card = get_safe_token(device);
346
347   /* Resolve the callbacks to the PEB */
348   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
349   card->lines.ready.resolve(topeb->ready, *device);
350   card->lines.intb.resolve(topeb->intb, *device);
351
352   card->motor_on_timer = device->machine().scheduler().timer_alloc(FUNC(motor_on_timer_callback), (void *)device);
353
354   astring region(device->tag(), ":", fdc_region);
355   card->rom = device->machine().region(region.cstr())->base();
356   card->controller = device->subdevice("fd1771");
269   m_cru_base = 0x1100;
357270}
358271
359static DEVICE_STOP( ti99_fdc )
272void ti_fdc_device::device_reset(void)
360273{
361   logerror("ti99_fdc: stop\n");
362}
363
364static DEVICE_RESET( ti99_fdc )
365{
366   ti99_fdc_state *card = get_safe_token(device);
367
368   /* If the card is selected in the menu, register the card */
369   if (input_port_read(device->machine(), "DISKCTRL") == DISK_TIFDC)
274   if (VERBOSE>5) LOG("ti_fdc: TI FDC reset\n");
275   m_DSEL = 0;
276   m_SIDSEL = 0;
277   m_DVENA = CLEAR_LINE;
278   m_strobe_motor = false;
279   if (m_genmod)
370280   {
371      device_t *peb = device->owner();
372      int success = mount_card(peb, device, &fdc_card, get_pebcard_config(device)->slot);
373      if (!success) return;
374
375      card->DSEL = 0;
376      card->SIDSEL = 0;
377      card->DVENA = CLEAR_LINE;
378      card->strobe_motor = 0;
379
380      card->select_mask = 0x7e000;
381      card->select_value = 0x74000;
382
383      if (input_port_read(device->machine(), "MODE")==GENMOD)
384      {
385         // GenMod card modification
386         card->select_mask = 0x1fe000;
387         card->select_value = 0x174000;
388      }
389
390      ti99_set_80_track_drives(FALSE);
391
392      floppy_type_t type = FLOPPY_STANDARD_5_25_DSDD_40;
393      set_all_geometries(device, type);
281      m_select_mask = 0x1fe000;
282      m_select_value = 0x174000;
394283   }
395   // TODO: Check that:    floppy_mon_w(w->drive, CLEAR_LINE);
396}
284   else
285   {
286      m_select_mask = 0x7e000;
287      m_select_value = 0x74000;
288   }
289   m_DRQ = false;
290   m_IRQ = false;
291   m_hold = false;
292   m_selected = false;
397293
398#if 0
399static WRITE_LINE_DEVICE_HANDLER( ti99_fdc_ready )
400{
401   // Caution: The device pointer passed to this function is the calling
402   // device. That is, if we want *this* device, we need to take the owner.
403   ti99_fdc_state *card = get_safe_token(device->owner());
404   card->lines.ready(state);
294   ti99_set_80_track_drives(FALSE);
295   floppy_type_t type = FLOPPY_STANDARD_5_25_DSDD_40;
296   set_all_geometries(type);
405297}
406#endif
407298
408MACHINE_CONFIG_FRAGMENT( ti99_fdc )
409   MCFG_FD1771_ADD("fd1771", ti_wd17xx_interface )
299MACHINE_CONFIG_FRAGMENT( ti_fdc )
300   MCFG_FD1771_ADD(FDC_TAG, ti_wd17xx_interface )
410301MACHINE_CONFIG_END
411302
412ROM_START( ti99_fdc )
413   ROM_REGION(0x2000, fdc_region, 0)
414   ROM_LOAD_OPTIONAL("disk.bin", 0x0000, 0x2000, CRC(8f7df93f) SHA1(ed91d48c1eaa8ca37d5055bcf67127ea51c4cad5)) /* TI disk DSR ROM */
303ROM_START( ti_fdc )
304   ROM_REGION(0x2000, DSRROM, 0)
305   ROM_LOAD("disk.bin", 0x0000, 0x2000, CRC(8f7df93f) SHA1(ed91d48c1eaa8ca37d5055bcf67127ea51c4cad5)) /* TI disk DSR ROM */
415306ROM_END
416307
417static const char DEVTEMPLATE_SOURCE[] = __FILE__;
308machine_config_constructor ti_fdc_device::device_mconfig_additions() const
309{
310   return MACHINE_CONFIG_NAME( ti_fdc );
311}
418312
419#define DEVTEMPLATE_ID(p,s)             p##ti99_fdc##s
420#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
421#define DEVTEMPLATE_NAME                "TI Floppy Disk Controller Card"
422#define DEVTEMPLATE_SHORTNAME           "tifdc"
423#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
424#include "devtempl.h"
313const rom_entry *ti_fdc_device::device_rom_region() const
314{
315   return ROM_NAME( ti_fdc );
316}
425317
426DEFINE_LEGACY_DEVICE( TIFDC, ti99_fdc );
318const device_type TI99_FDC = &device_creator<ti_fdc_device>;
427319
src/mess/machine/ti99/p_code.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 P-Code Card emulation.
3    Michael Zapf, July 2009
44
55    The P-Code card is part of the UCSD p-System support for the TI-99
66    computer family. This system is a comprehensive development system for
r14513r14514
6262    rest of the ROM space the ports become invisible when the card is
6363    deactivated.
6464
65    Emulation information:
65    Michael Zapf
6666
67    The P-Code card switch is mapped to a DIP switch in the configuration
68    menu. If the switch is turned on, the system assumes that ROM dumps are
69    present called "pcode_r0.bin" (4 KiB), "pcode_r1.bin" (8KiB),
70    "pcode_g0.bin" (64 KiB). The switch is turned off by default. Check the
71    switch position if the Master Title Screen does not appear as expected.
72
67    July 2009: First version
7368    September 2010: Rewritten as device
74*/
75#include "emu.h"
76#include "peribox.h"
77#include "p_code.h"
78#include "grom.h"
69    February 2012: Rewritten as class
7970
80#define GROMBASE 0x5bfc
81#define PCODE_CRU_BASE 0x1f00
71*****************************************************************************/
8272
83#define pcode_region "pcode_region"
84#define grom_offset 0x3000
73#include "p_code.h"
8574
86typedef ti99_pebcard_config ti99_pcoden_config;
75#define PCODE_GROM_TAG "pcode_grom"
76#define PCODE_ROM_TAG "pcode_rom"
8777
88typedef struct _ti99_pcoden_state
89{
90   int selected;
78#define PGROM0_TAG "grom0"
79#define PGROM1_TAG "grom1"
80#define PGROM2_TAG "grom2"
81#define PGROM3_TAG "grom3"
82#define PGROM4_TAG "grom4"
83#define PGROM5_TAG "grom5"
84#define PGROM6_TAG "grom6"
85#define PGROM7_TAG "grom7"
9186
92   int bank_select; // 0 or 1
87#define GROMMASK 0x1ffd
88#define GROMREAD 0x1bfc
89#define GROMWRITE 0x1ffc
9390
94   // Pointer to the pcode ROM data. We have three sections of the ROM code:
95   // 4K from one chip, 2 banks with 4K each from the second chip.
96   UINT8 *rom0;
97   UINT8 *rom1;
98   UINT8 *rom2;
99   UINT8 *grom;
91#define ACTIVE_TAG "ACTIVE"
10092
101   device_t *gromdev[8];
93#define LOG logerror
94#define VERBOSE 1
10295
103   ti99_peb_connect      lines;
104
105} ti99_pcoden_state;
106
107INLINE ti99_pcoden_state *get_safe_token(device_t *device)
96ti_pcode_card_device::ti_pcode_card_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
97: ti_expansion_card_device(mconfig, TI99_P_CODE, "TI-99 P-Code Card", tag, owner, clock)
10898{
109   assert(device != NULL);
110   assert(device->type() == PCODEN);
111
112   return (ti99_pcoden_state *)downcast<legacy_device_base *>(device)->token();
99   m_shortname = "ti99_pcode";
113100}
114101
115/*************************************************************************/
116
117/*
118    The CRU write handler. The CRU is a serial interface in the console.
119    Using the CRU we can switch the banks in the p-code card
120    Bit 0 = activate card
121    Bit 4 = select second bank of high ROM.
122    Note that the CRU address is created from address lines A8, A13, and A14
123    so bit 0 is at 0x1f00, but bit 4 is at 0x1f80 (bit 7 would be 0x1f86
124    but it is not used)
125*/
126static WRITE8_DEVICE_HANDLER( cru_w )
102READ8Z_MEMBER( ti_pcode_card_device::readz )
127103{
128   ti99_pcoden_state *card = get_safe_token(device);
129
130   if ((offset & 0xff00)==PCODE_CRU_BASE)
104   if (m_switch && m_selected && ((offset & m_select_mask)==m_select_value))
131105   {
132      int addr = offset & 0x00ff;
133
134      if (addr==0)
135         card->selected = data;
136
137      if (addr==0x80) // Bit 4 is on address line 8
138         card->bank_select = data;
139   }
140}
141
142/*
143    Read a byte in pcode DSR space
144*/
145static READ8Z_DEVICE_HANDLER( data_r )
146{
147   ti99_pcoden_state *card = get_safe_token(device);
148
149   if (card->selected)
150   {
151      if ((offset & 0x7e000)==0x74000)
106      // GROM access
107      if ((offset & GROMMASK)==GROMREAD)
152108      {
153         if ((offset & 0xfffd)==GROMBASE)
154         {
155            for (int i=0; i < 8; i++)
156               ti99grom_rz(card->gromdev[i], offset, value);
157            return;
158         }
159
109         for (int i=0; i < 8; i++) m_grom[i]->readz(space, offset, value, mem_mask);
110         if (VERBOSE>5) LOG("ti99_pcode: read from grom %04x: %02x\n", offset&0xffff, *value);
111      }
112      else
113      {
160114         if ((offset & 0x1000) == 0x0000)
161115         {
162116            /* Accesses ROM 4732 (4K) */
163            *value = card->rom0[offset & 0x0fff];
117            // 0000 xxxx xxxx xxxx
118            *value = m_rom[offset & 0x0fff];
119            if (VERBOSE>5) LOG("ti99_pcode: read from rom %04x: %02x\n", offset&0xffff, *value);
164120         }
165121         else
122         {
166123            // Accesses ROM 4764 (2*4K)
167         // We have two banks here which are activated according
168         // to the setting of CRU bit 4
169         *value = (card->bank_select==0)? card->rom1[offset & 0x0fff] : card->rom2[offset & 0x0fff];
124            // We have two banks here which are activated according
125            // to the setting of CRU bit 4
126            // Bank 0 is the ROM above
127            // 0001 xxxx xxxx xxxx   Bank 1
128            // 0010 xxxx xxxx xxxx   Bank 2
129            *value = m_rom[(m_bank_select<<12) | (offset & 0x0fff)];
130            if (VERBOSE>5) LOG("ti99_pcode: read from rom %04x (%02x): %02x\n", offset&0xffff, m_bank_select, *value);
131         }
170132      }
171133   }
172134}
173135
174136/*
175    Write a byte in P-Code ROM space. This is only useful for setting the
137    Write a byte in P-Code ROM space. This is only used for setting the
176138    GROM address.
177139*/
178static WRITE8_DEVICE_HANDLER( data_w )
140WRITE8_MEMBER( ti_pcode_card_device::write )
179141{
180   ti99_pcoden_state *card = get_safe_token(device);
181   if (card->selected)
142   if (m_switch && m_selected)
182143   {
183      if ((offset & 0x7fffd)==(GROMBASE|0x70400))
144      if ((offset & m_select_mask)==m_select_value)
184145      {
185         for (int i=0; i < 8; i++)
186            ti99grom_w(card->gromdev[i], offset, data);
146         if (VERBOSE>5) LOG("ti99_pcode: write to address %04x: %02x\n", offset & 0xffff, data);
147         // 0101 1111 1111 11x0
148         if ((offset & GROMMASK) == GROMWRITE)
149         {
150            for (int i=0; i < 8; i++) m_grom[i]->write(space, offset, data, mem_mask);
151         }
187152      }
188153   }
189154}
190155
191static const ti99_peb_card pcode_ncard =
156/*
157    Common READY* line from the GROMs.
158    At this time we do not emulate GROM READY* since the CPU emulation does
159    not yet process READY*. If it did, however, we would have to do a similar
160    handling as in peribox (with INTA*): The common READY* line is a logical
161    AND of all single READY lines. If any GROM pulls it down, the line goes
162    down, and only if all GROMs release it, it pulls up again. We should think
163    about a general solution.
164*/
165WRITE_LINE_MEMBER( ti_pcode_card_device::ready_line )
192166{
193   data_r,
194   data_w,
195   NULL,
196   cru_w,
197
198   NULL, NULL,   NULL, NULL
199};
200
201static DEVICE_START( ti99_pcoden )
202{
203   ti99_pcoden_state *pcode = get_safe_token(device);
204
205   /* Resolve the callbacks to the PEB */
206   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
207   pcode->lines.ready.resolve(topeb->ready, *device);
167   m_slot->set_ready(state);
208168}
209169
210static DEVICE_STOP( ti99_pcoden )
170/*
171    CRU read handler. The P-Code card does not offer CRU read lines, so
172    we just ignore any request. (Note that CRU lines are not like memory; you
173    may be able to write to them, but not necessarily read them again.)
174*/
175void ti_pcode_card_device::crureadz(offs_t offset, UINT8 *value)
211176{
212   logerror("ti99_pcode: stop\n");
177   // Nothing here.
178   return;
213179}
214180
215static DEVICE_RESET( ti99_pcoden )
181/*
182    The CRU write handler.
183    Bit 0 = activate card
184    Bit 4 = select second bank of high ROM.
185
186    Somewhat uncommon, the CRU address is created from address lines
187    A8, A13, and A14 so bit 0 is at 0x1f00, but bit 4 is at 0x1f80. Accordingly,
188    bit 7 would be 0x1f86 but it is not used.
189*/
190void ti_pcode_card_device::cruwrite(offs_t offset, UINT8 data)
216191{
217   logerror("ti99_pcode: reset\n");
218   ti99_pcoden_state *pcode = get_safe_token(device);
219
220   /* If the card is selected in the menu, register the card */
221   if (input_port_read(device->machine(), "EXTCARD") & EXT_PCODE)
192   if ((offset & 0xff00)==m_cru_base)
222193   {
223      device_t *peb = device->owner();
224      int success = mount_card(peb, device, &pcode_ncard, get_pebcard_config(device)->slot);
225      if (!success) return;
194      int addr = offset & 0x00ff;
226195
227      astring region(device->tag(), ":", pcode_region);
196      if (addr==0)
197         m_selected = (data != 0);
228198
229      pcode->rom0 = device->machine().region(region.cstr())->base();
230      pcode->rom1 = pcode->rom0 + 0x1000;
231      pcode->rom2 = pcode->rom0 + 0x2000;
232      pcode->grom = pcode->rom0 + 0x3000;
233      pcode->bank_select = 0;
234      pcode->selected = 0;
235
236      astring gromname;
237      for (int i=0; i < 8; i++)
199      if (addr==0x80) // Bit 4 is on address line 8
238200      {
239         gromname.printf("grom_%d", i);
240         pcode->gromdev[i] = device->subdevice(gromname.cstr());
201         m_bank_select = (data+1);   // we're calling this bank 1 and bank 2
202         if (VERBOSE>5) LOG("ti99_pcode: select rom bank %d\n", m_bank_select);
241203      }
242204   }
243205}
244206
245static WRITE_LINE_DEVICE_HANDLER( pcode_ready )
207static GROM_CONFIG(pgrom0_config)
246208{
247   // Caution: The device pointer passed to this function is the calling
248   // device. That is, if we want *this* device, we need to take the owner.
249   ti99_pcoden_state *pcode = get_safe_token(device->owner());
250   pcode->lines.ready(state);
209   false, 0, PCODE_GROM_TAG, 0x0000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
210};
211static GROM_CONFIG(pgrom1_config)
212{
213   false, 1, PCODE_GROM_TAG, 0x2000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
214};
215static GROM_CONFIG(pgrom2_config)
216{
217   false, 2, PCODE_GROM_TAG, 0x4000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
218};
219static GROM_CONFIG(pgrom3_config)
220{
221   false, 3, PCODE_GROM_TAG, 0x6000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
222};
223static GROM_CONFIG(pgrom4_config)
224{
225   false, 4, PCODE_GROM_TAG, 0x8000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
226};
227static GROM_CONFIG(pgrom5_config)
228{
229   false, 5, PCODE_GROM_TAG, 0xa000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
230};
231static GROM_CONFIG(pgrom6_config)
232{
233   false, 6, PCODE_GROM_TAG, 0xc000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
234};
235static GROM_CONFIG(pgrom7_config)
236{
237   false, 7, PCODE_GROM_TAG, 0xe000, 0x1800, false, DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_pcode_card_device, ready_line)
238};
239
240void ti_pcode_card_device::device_start()
241{
242   m_cru_base = 0x1f00;
243   m_grom[0] = static_cast<ti99_grom_device*>(subdevice(PGROM0_TAG));
244   m_grom[1] = static_cast<ti99_grom_device*>(subdevice(PGROM1_TAG));
245   m_grom[2] = static_cast<ti99_grom_device*>(subdevice(PGROM2_TAG));
246   m_grom[3] = static_cast<ti99_grom_device*>(subdevice(PGROM3_TAG));
247   m_grom[4] = static_cast<ti99_grom_device*>(subdevice(PGROM4_TAG));
248   m_grom[5] = static_cast<ti99_grom_device*>(subdevice(PGROM5_TAG));
249   m_grom[6] = static_cast<ti99_grom_device*>(subdevice(PGROM6_TAG));
250   m_grom[7] = static_cast<ti99_grom_device*>(subdevice(PGROM7_TAG));
251   m_rom = subregion(PCODE_ROM_TAG)->base();
251252}
252253
253/*
254    Get the pointer to the GROM data from the P-Code card. Called by the GROM.
255*/
256static UINT8 *get_grom_ptr(device_t *device)
254void ti_pcode_card_device::device_reset()
257255{
258   ti99_pcoden_state *pcode = get_safe_token(device);
259   return pcode->grom;
256   if (m_genmod)
257   {
258      m_select_mask = 0x1fe000;
259      m_select_value = 0x174000;
260   }
261   else
262   {
263      m_select_mask = 0x7e000;
264      m_select_value = 0x74000;
265   }
266   m_bank_select = 1;
267   m_selected = false;
268
269   m_switch = input_port_read(*this, ACTIVE_TAG);
260270}
261271
262MACHINE_CONFIG_FRAGMENT( ti99_pcoden )
263   MCFG_GROM_ADD_P( "grom_0", 0, get_grom_ptr, 0x0000, 0x1800, pcode_ready )
264   MCFG_GROM_ADD_P( "grom_1", 1, get_grom_ptr, 0x2000, 0x1800, pcode_ready )
265   MCFG_GROM_ADD_P( "grom_2", 2, get_grom_ptr, 0x4000, 0x1800, pcode_ready )
266   MCFG_GROM_ADD_P( "grom_3", 3, get_grom_ptr, 0x6000, 0x1800, pcode_ready )
267   MCFG_GROM_ADD_P( "grom_4", 4, get_grom_ptr, 0x8000, 0x1800, pcode_ready )
268   MCFG_GROM_ADD_P( "grom_5", 5, get_grom_ptr, 0xa000, 0x1800, pcode_ready )
269   MCFG_GROM_ADD_P( "grom_6", 6, get_grom_ptr, 0xc000, 0x1800, pcode_ready )
270   MCFG_GROM_ADD_P( "grom_7", 7, get_grom_ptr, 0xe000, 0x1800, pcode_ready )
272void ti_pcode_card_device::device_config_complete()
273{
274}
275
276INPUT_CHANGED_MEMBER( ti_pcode_card_device::switch_changed )
277{
278   if (VERBOSE>7) LOG("ti_pcode_card_device: switch changed to %d\n", newval);
279   m_switch = (newval != 0);
280}
281
282
283MACHINE_CONFIG_FRAGMENT( ti99_pcode )
284   MCFG_GROM_ADD( PGROM0_TAG, pgrom0_config )
285   MCFG_GROM_ADD( PGROM1_TAG, pgrom1_config )
286   MCFG_GROM_ADD( PGROM2_TAG, pgrom2_config )
287   MCFG_GROM_ADD( PGROM3_TAG, pgrom3_config )
288   MCFG_GROM_ADD( PGROM4_TAG, pgrom4_config )
289   MCFG_GROM_ADD( PGROM5_TAG, pgrom5_config )
290   MCFG_GROM_ADD( PGROM6_TAG, pgrom6_config )
291   MCFG_GROM_ADD( PGROM7_TAG, pgrom7_config )
271292MACHINE_CONFIG_END
272293
273ROM_START( ti99_pcoden )
274   ROM_REGION(0x13000, pcode_region, 0)
275   ROM_LOAD_OPTIONAL("pcode_r0.bin", 0x0000, 0x1000, CRC(3881d5b0) SHA1(a60e0468bb15ff72f97cf6e80979ca8c11ed0426)) /* TI P-Code card rom4732 */
276   ROM_LOAD_OPTIONAL("pcode_r1.bin", 0x1000, 0x2000, CRC(46a06b8b) SHA1(24e2608179921aef312cdee6f455e3f46deb30d0)) /* TI P-Code card rom4764 */
277   ROM_LOAD_OPTIONAL("pcode_g0.bin", 0x3000, 0x10000, CRC(541b3860) SHA1(7be77c216737334ae997753a6a85136f117affb7)) /* TI P-Code card groms */
294INPUT_PORTS_START( ti99_pcode )
295   PORT_START( ACTIVE_TAG )
296   PORT_DIPNAME( 0x01, 0x00, "P-Code activation switch" ) PORT_CHANGED_MEMBER(DEVICE_SELF, ti_pcode_card_device, switch_changed, 0)
297      PORT_DIPSETTING( 0x00, DEF_STR( Off ) )
298      PORT_DIPSETTING( 0x01, DEF_STR( On ) )
299INPUT_PORTS_END
300
301ROM_START( ti99_pcode )
302   ROM_REGION(0x10000, PCODE_GROM_TAG, 0)
303   ROM_LOAD("pcode_g0.bin", 0x0000, 0x10000, CRC(541b3860) SHA1(7be77c216737334ae997753a6a85136f117affb7)) /* TI P-Code card groms */
304   ROM_REGION(0x3000, PCODE_ROM_TAG, 0)
305   ROM_LOAD("pcode_r0.bin", 0x0000, 0x1000, CRC(3881d5b0) SHA1(a60e0468bb15ff72f97cf6e80979ca8c11ed0426)) /* TI P-Code card rom4732 */
306   ROM_LOAD("pcode_r1.bin", 0x1000, 0x2000, CRC(46a06b8b) SHA1(24e2608179921aef312cdee6f455e3f46deb30d0)) /* TI P-Code card rom4764 */
278307ROM_END
279308
280static const char DEVTEMPLATE_SOURCE[] = __FILE__;
309machine_config_constructor ti_pcode_card_device::device_mconfig_additions() const
310{
311   return MACHINE_CONFIG_NAME( ti99_pcode );
312}
281313
282#define DEVTEMPLATE_ID(p,s)             p##ti99_pcoden##s
283#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
284#define DEVTEMPLATE_NAME                "TI99 P-Code Card"
285#define DEVTEMPLATE_SHORTNAME           "ti99pcode"
286#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
287#include "devtempl.h"
314const rom_entry *ti_pcode_card_device::device_rom_region() const
315{
316   return ROM_NAME( ti99_pcode );
317}
288318
289DEFINE_LEGACY_DEVICE(PCODEN, ti99_pcoden);
319ioport_constructor ti_pcode_card_device::device_input_ports() const
320{
321   return INPUT_PORTS_NAME( ti99_pcode );
322}
323
324const device_type TI99_P_CODE = &device_creator<ti_pcode_card_device>;
325
src/mess/machine/ti99/tn_usbsm.c
r14513r14514
1/*
1/****************************************************************************
2
23    Thierry Nouspikel's USB-SmartMedia card emulation
34
45    This card features three USB ports (two host and one device) and a
r14513r14514
2829
2930    Raphael Nabet, 2004.
3031
31    Rewritten as device
32    Michael Zapf, September 2010
33*/
34#include "emu.h"
35#include "peribox.h"
36#include "tn_usbsm.h"
37#include "cpu/tms9900/tms9900.h"
38#include "machine/strata.h"
39#include "machine/smartmed.h"
32    Michael Zapf
33    September 2010: Rewritten as device
34    February 2012: Rewritten as class
4035
41#define CRU_BASE 0x1600
36    FIXME: Completely untested and likely to be broken
4237
43typedef ti99_pebcard_config tn_usbsm_config;
38*****************************************************************************/
4439
45typedef struct _tn_usbsm_state
46{
47   device_t *smartmedia;
48   device_t *strata;
40#include "tn_usbsm.h"
41#include "machine/smartmed.h"
42#include "machine/strata.h"
4943
50   int    selected;
44#define BUFFER_TAG "ram"
5145
52   /* Used for GenMod */
53   int    select_mask;
54   int      select_value;
55
56   int      feeprom_page;
57   int      sram_page;
58   int      cru_register;
59   int      tms9995_mode;
60
61   UINT16   input_latch;
62
63   UINT16   output_latch;
64   UINT16   *ram;
65
66   /* Callback lines to the main system. */
67   ti99_peb_connect   lines;
68
69} tn_usbsm_state;
70
7146enum
7247{
73   cru_reg_io_regs_enable = 0x02,
74   cru_reg_int_enable = 0x04,
75   cru_reg_sm_enable = 0x08,
76   cru_reg_feeprom_write_enable = 0x10
48   IO_REGS_ENABLE = 0x02,
49   INT_ENABLE = 0x04,
50   SM_ENABLE = 0x08,
51   FEEPROM_WRITE_ENABLE = 0x10
7752};
7853
79INLINE tn_usbsm_state *get_safe_token(device_t *device)
54nouspikel_usb_smartmedia_device::nouspikel_usb_smartmedia_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
55: ti_expansion_card_device(mconfig, TI99_USBSM, "Nouspikel USB/Smartmedia card", tag, owner, clock)
8056{
81   assert(device != NULL);
82   assert(device->type() == USBSMART);
83
84   return (tn_usbsm_state *)downcast<legacy_device_base *>(device)->token();
57   m_shortname = "ti99_usbsm";
8558}
8659
8760/*
8861    demultiplexed read in USB-SmartMedia DSR space
8962*/
90static UINT16 usbsm_mem_16_r(device_t *device, offs_t offset)
63UINT16 nouspikel_usb_smartmedia_device::usbsm_mem_16_r(offs_t offset)
9164{
9265   UINT16 reply = 0;
93   tn_usbsm_state *card = get_safe_token(device);
9466
9567   if (offset < 0x2800)
9668   {   /* 0x4000-0x4fff range */
97      if ((card->cru_register & cru_reg_io_regs_enable) && (offset >= 0x27f8))
69      if ((m_cru_register & IO_REGS_ENABLE) && (offset >= 0x27f8))
9870      {   /* SmartMedia interface */
9971         if (offset == 0)
100            reply = smartmedia_data_r(card->smartmedia) << 8;
72            reply = smartmedia_data_r(m_smartmedia) << 8;
10173      }
10274      else
10375      {   /* FEEPROM */
104         if (!(card->cru_register & cru_reg_feeprom_write_enable))
105            reply = strataflash_16_r(card->strata, offset);
76         if (!(m_cru_register & FEEPROM_WRITE_ENABLE))
77            reply = strataflash_16_r(m_strata, offset);
10678      }
10779   }
10880   else
10981   {   /* 0x5000-0x5fff range */
110      if ((card->cru_register & cru_reg_io_regs_enable) && (offset >= 0x2ff8))
82      if ((m_cru_register & IO_REGS_ENABLE) && (offset >= 0x2ff8))
11183      {   /* USB controller */
11284      }
11385      else
11486      {   /* SRAM */
115         reply = card->ram[card->sram_page*0x800+(offset-0x2800)];
87         reply = m_ram[m_sram_page*0x800+(offset-0x2800)];
11688      }
11789   }
11890   return reply;
r14513r14514
12193/*
12294    demultiplexed write in USB-SmartMedia DSR space
12395*/
124static void usbsm_mem_16_w(device_t *device, offs_t offset, UINT16 data)
96void nouspikel_usb_smartmedia_device::usbsm_mem_16_w(offs_t offset, UINT16 data)
12597{
126   tn_usbsm_state *card = get_safe_token(device);
127
12898   if (offset < 0x2800)
12999   {   /* 0x4000-0x4fff range */
130      if ((card->cru_register & cru_reg_io_regs_enable) && (offset >= 0x27f8))
100      if ((m_cru_register & IO_REGS_ENABLE) && (offset >= 0x27f8))
131101      {   /* SmartMedia interface */
132102         switch (offset & 3)
133103         {
134104         case 0:
135            smartmedia_data_w(card->smartmedia, data >> 8);
105            smartmedia_data_w(m_smartmedia, data >> 8);
136106            break;
137107         case 1:
138            smartmedia_address_w(card->smartmedia, data >> 8);
108            smartmedia_address_w(m_smartmedia, data >> 8);
139109            break;
140110         case 2:
141            smartmedia_command_w(card->smartmedia, data >> 8);
111            smartmedia_command_w(m_smartmedia, data >> 8);
142112            break;
143113         case 3:
144114            /* bogus, don't use(?) */
r14513r14514
147117      }
148118      else
149119      {   /* FEEPROM */
150         if (card->cru_register & cru_reg_feeprom_write_enable)
151            strataflash_16_w(card->strata, offset, data);
120         if (m_cru_register & FEEPROM_WRITE_ENABLE)
121            strataflash_16_w(m_strata, offset, data);
152122      }
153123   }
154124   else
155125   {   /* 0x5000-0x5fff range */
156      if ((card->cru_register & cru_reg_io_regs_enable) && (offset >= 0x2ff8))
126      if ((m_cru_register & IO_REGS_ENABLE) && (offset >= 0x2ff8))
157127      {   /* USB controller */
158128      }
159129      else
160130      {   /* SRAM */
161         card->ram[card->sram_page*0x800+(offset-0x2800)] = data;
131         m_ram[m_sram_page*0x800+(offset-0x2800)] = data;
162132      }
163133   }
164134}
r14513r14514
166136/*
167137    CRU read
168138*/
169static READ8Z_DEVICE_HANDLER( cru_rz )
139void nouspikel_usb_smartmedia_device::crureadz(offs_t offset, UINT8 *value)
170140{
171   tn_usbsm_state *card = get_safe_token(device);
172
173   if ((offset & 0xff00)==CRU_BASE)
141   if ((offset & 0xff00)==m_cru_base)
174142   {
175143      UINT8 reply = 0;
176      device_t *smartmedia = card->smartmedia;
177144      offset &= 3;
178145
179146      if (offset == 0)
r14513r14514
193160         //             1: Card absent or not protected.
194161
195162         reply = 0x33;
196         if (!smartmedia_present(smartmedia))
163         if (!smartmedia_present(m_smartmedia))
197164            reply |= 0xc0;
198         else if (!smartmedia_protected(smartmedia))
165         else if (!smartmedia_protected(m_smartmedia))
199166            reply |= 0x80;
200167      }
201168      *value = reply;
r14513r14514
205172/*
206173    CRU write
207174*/
208static WRITE8_DEVICE_HANDLER( cru_w )
175void nouspikel_usb_smartmedia_device::cruwrite(offs_t offset, UINT8 data)
209176{
210   tn_usbsm_state *card = get_safe_token(device);
211
212   if ((offset & 0xff00)==CRU_BASE)
177   if ((offset & 0xff00)==m_cru_base)
213178   {
214179      int bit = (offset >> 1) & 0x1f;
215180
216181      switch (bit)
217182      {
218183      case 0:
219         card->selected = data;
184         m_selected = data;
220185         break;
221186
222187      case 1:         /* enable I/O registers */
r14513r14514
224189      case 3:         /* enable SmartMedia card */
225190      case 4:         /* enable FEEPROM writes (and disable reads) */
226191         if (data)
227            card->cru_register |= 1 << bit;
192            m_cru_register |= 1 << bit;
228193         else
229            card->cru_register &= ~ (1 << bit);
194            m_cru_register &= ~ (1 << bit);
230195         break;
231196      case 5:         /* FEEPROM page */
232197      case 6:
r14513r14514
240205      case 14:
241206      case 15:
242207         if (data)
243            card->feeprom_page |= 1 << (bit-5);
208            m_feeprom_page |= 1 << (bit-5);
244209         else
245            card->feeprom_page &= ~ (1 << (bit-5));
210            m_feeprom_page &= ~ (1 << (bit-5));
246211         break;
247212
248213      case 16:      /* SRAM page */
r14513r14514
254219      case 22:
255220      case 23:
256221         if (data)
257            card->sram_page |= 1 << (bit-16);
222            m_sram_page |= 1 << (bit-16);
258223         else
259            card->sram_page &= ~ (1 << (bit-16));
224            m_sram_page &= ~ (1 << (bit-16));
260225         break;
261226      }
262227   }
r14513r14514
266231    Memory read
267232    TODO: Check whether AMA/B/C is actually checked
268233*/
269static READ8Z_DEVICE_HANDLER( data_rz )
234READ8Z_MEMBER(nouspikel_usb_smartmedia_device::readz)
270235{
271   tn_usbsm_state *card = get_safe_token(device);
272
273   if (((offset & card->select_mask)==card->select_value) && card->selected)
236   if (((offset & m_select_mask)==m_select_value) && m_selected)
274237   {
275      if (card->tms9995_mode ? (!(offset & 1)) : (offset & 1))
238      if (m_tms9995_mode ? (!(offset & 1)) : (offset & 1))
276239      {   /* first read triggers 16-bit read cycle */
277         card->input_latch = usbsm_mem_16_r(device, (offset >> 1)&0xffff);
240         m_input_latch = usbsm_mem_16_r((offset >> 1)&0xffff);
278241      }
279242
280243      /* return latched input */
281      *value = ((offset & 1) ? (card->input_latch) : (card->input_latch >> 8)) & 0xff;
244      *value = ((offset & 1) ? (m_input_latch) : (m_input_latch >> 8)) & 0xff;
282245   }
283246}
284247
285248/*
286249    Memory write. The controller is 16 bit, so we need to demultiplex again.
287250*/
288static WRITE8_DEVICE_HANDLER( data_w )
251WRITE8_MEMBER(nouspikel_usb_smartmedia_device::write)
289252{
290   tn_usbsm_state *card = get_safe_token(device);
291
292   if (((offset & card->select_mask)==card->select_value) && card->selected)
253   if (((offset & m_select_mask)==m_select_value) && m_selected)
293254   {
294255      /* latch write */
295256      if (offset & 1)
296         card->output_latch = (card->output_latch & 0xff00) | data;
257         m_output_latch = (m_output_latch & 0xff00) | data;
297258      else
298         card->output_latch = (card->output_latch & 0x00ff) | (data << 8);
259         m_output_latch = (m_output_latch & 0x00ff) | (data << 8);
299260
300      if ((card->tms9995_mode)? (offset & 1) : (!(offset & 1)))
261      if ((m_tms9995_mode)? (offset & 1) : (!(offset & 1)))
301262      {   /* second write triggers 16-bit write cycle */
302         usbsm_mem_16_w(device, (offset >> 1)&0xffff, card->output_latch);
263         usbsm_mem_16_w((offset >> 1)&0xffff, m_output_latch);
303264      }
304265   }
305266}
306267
307/**************************************************************************/
308
309static const ti99_peb_card tn_usbsm_card =
268void nouspikel_usb_smartmedia_device::device_start()
310269{
311   data_rz, data_w,            // memory access read/write
312   cru_rz, cru_w,               // CRU access
313   NULL, NULL,                  // SENILA/B access
314   NULL, NULL                  // 16 bit access (none here)
315};
316
317static DEVICE_START( tn_usbsm )
318{
319   tn_usbsm_state *card = get_safe_token(device);
320   card->ram = auto_alloc_array(device->machine(), UINT16, 0x100000/2);
321   card->smartmedia = device->subdevice("smartmedia");
322   card->strata = device->subdevice("strata");
270   m_ram = (UINT16*)subregion(BUFFER_TAG)->base();
271   /* auto_alloc_array(device->machine(), UINT16, 0x100000/2); */
272   m_smartmedia = subdevice("smartmedia");
273   m_strata = subdevice("strata");
323274}
324275
325static DEVICE_STOP( tn_usbsm )
276void nouspikel_usb_smartmedia_device::device_reset()
326277{
327}
278   m_feeprom_page = 0;
279   m_sram_page = 0;
280   m_cru_register = 0;
281   // m_tms9995_mode = (device->type()==TMS9995);
328282
329static DEVICE_RESET( tn_usbsm )
330{
331   tn_usbsm_state *card = get_safe_token(device);
332   /* Register the card */
333   device_t *peb = device->owner();
334
335   if (input_port_read(device->machine(), "HDCTRL") & HD_USB)
283   if (m_genmod)
336284   {
337      int success = mount_card(peb, device, &tn_usbsm_card, get_pebcard_config(device)->slot);
338      if (!success) return;
285      m_select_mask = 0x1fe000;
286      m_select_value = 0x174000;
287   }
288   else
289   {
290      m_select_mask = 0x7e000;
291      m_select_value = 0x74000;
292   }
293   m_selected = false;
339294
340      card->feeprom_page = 0;
341      card->sram_page = 0;
342      card->cru_register = 0;
343      card->tms9995_mode = (device->type()==TMS9995);
295   m_cru_base = input_port_read(*this, "CRUUSBSM");
296}
344297
345      card->select_mask = 0x7e000;
346      card->select_value = 0x74000;
298ROM_START( tn_usbsm )
299   ROM_REGION16_BE(0x80000, BUFFER_TAG, 0)  /* RAM buffer 512 KiB */
300   ROM_FILL(0x0000, 0x80000, 0x0000)
301ROM_END
347302
348      if (input_port_read(device->machine(), "MODE")==GENMOD)
349      {
350         // GenMod card modification
351         card->select_mask = 0x1fe000;
352         card->select_value = 0x174000;
353      }
354   }
355}
303INPUT_PORTS_START( tn_usbsm )
304   PORT_START( "CRUUSBSM" )
305   PORT_DIPNAME( 0x1f00, 0x1600, "USB/Smartmedia CRU base" )
306      PORT_DIPSETTING( 0x1000, "1000" )
307      PORT_DIPSETTING( 0x1100, "1100" )
308      PORT_DIPSETTING( 0x1200, "1200" )
309      PORT_DIPSETTING( 0x1300, "1300" )
310      PORT_DIPSETTING( 0x1400, "1400" )
311      PORT_DIPSETTING( 0x1500, "1500" )
312      PORT_DIPSETTING( 0x1600, "1600" )
313      PORT_DIPSETTING( 0x1700, "1700" )
314      PORT_DIPSETTING( 0x1800, "1800" )
315      PORT_DIPSETTING( 0x1900, "1900" )
316      PORT_DIPSETTING( 0x1a00, "1A00" )
317      PORT_DIPSETTING( 0x1b00, "1B00" )
318      PORT_DIPSETTING( 0x1c00, "1C00" )
319      PORT_DIPSETTING( 0x1d00, "1D00" )
320      PORT_DIPSETTING( 0x1e00, "1E00" )
321      PORT_DIPSETTING( 0x1f00, "1F00" )
322INPUT_PORTS_END
356323
357324MACHINE_CONFIG_FRAGMENT( tn_usbsm )
358325   MCFG_SMARTMEDIA_ADD( "smartmedia" )
359326   MCFG_STRATAFLASH_ADD( "strata" )
360327MACHINE_CONFIG_END
361328
362static const char DEVTEMPLATE_SOURCE[] = __FILE__;
329machine_config_constructor nouspikel_usb_smartmedia_device::device_mconfig_additions() const
330{
331   return MACHINE_CONFIG_NAME( tn_usbsm );
332}
363333
364#define DEVTEMPLATE_ID(p,s)             p##tn_usbsm##s
365#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
366#define DEVTEMPLATE_NAME                "Nouspikel USB/Smartmedia card"
367#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
368#include "devtempl.h"
334const rom_entry *nouspikel_usb_smartmedia_device::device_rom_region() const
335{
336   return ROM_NAME( tn_usbsm );
337}
369338
370DEFINE_LEGACY_DEVICE( USBSMART, tn_usbsm );
339ioport_constructor nouspikel_usb_smartmedia_device::device_input_ports() const
340{
341   return INPUT_PORTS_NAME(tn_usbsm);
342}
371343
344const device_type TI99_USBSM = &device_creator<nouspikel_usb_smartmedia_device>;
345
src/mess/machine/ti99/ti32kmem.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 32 KiB Memory Expansion Card
3*/
4    This is a fairly simple memory expansion for the TI-99/4A, adding
5    unbuffered 32 KiB. Yet it was a very popular card since it was
6    required for any kind of advanced programming beyond the console BASIC.
7
8    As a peripheral box card, it is connected via the 8-bit mutiplexed data bus.
9    Later, modifications of the console became increasingly popular which
10    avoided the bus multiplex so that the full 16bit access was possible.
11    This helped to noticeably speed up the system.
12
13    The memory is available on the addresses
14
15    0x2000 - 0x3fff ("low memory")
16    0xa000 - 0xffff ("high memory")
17
18    The console TI BASIC is not able to access the memory expansion, but
19    Extended Basic (available as a cartridge) makes use of it.
20
21    Michael Zapf
22    February 2012: Rewritten as class
23
24*****************************************************************************/
25
426#include "emu.h"
527#include "peribox.h"
628#include "ti32kmem.h"
729
8typedef ti99_pebcard_config ti99_mem32k_config;
30#define RAMREGION "ram"
931
10typedef struct _ti99_mem32k_state
32ti_32k_expcard_device::ti_32k_expcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
33: ti_expansion_card_device(mconfig, TI_32KMEM, "TI-99 32KiB memory expansion card", tag, owner, clock)
1134{
12   UINT8 *memory;
35   m_shortname = "ti99_32k";
36}
1337
14} ti99_mem32k_state;
15
16INLINE ti99_mem32k_state *get_safe_token(device_t *device)
38READ8Z_MEMBER(ti_32k_expcard_device::readz)
1739{
18   assert(device != NULL);
19   assert(device->type() == TI32KMEM);
20
21   return (ti99_mem32k_state *)downcast<legacy_device_base *>(device)->token();
40   switch((offset & 0xe000)>>13)
41   {
42   case 1:
43      *value = m_ram_ptr[offset & 0x1fff];
44      break;
45   case 5:
46      *value = m_ram_ptr[(offset & 0x1fff) | 0x2000];
47      break;
48   case 6:
49      *value = m_ram_ptr[(offset & 0x1fff) | 0x4000];
50      break;
51   case 7:
52      *value = m_ram_ptr[(offset & 0x1fff) | 0x6000];
53      break;
54   default:
55      break;
56   }
2257}
2358
24/*
25    Memory read
26*/
27static READ8Z_DEVICE_HANDLER( mem32k_rz )
59WRITE8_MEMBER(ti_32k_expcard_device::write)
2860{
29   ti99_mem32k_state *card = get_safe_token(device);
30   if (((offset & 0x7e000)==0x72000) || ((offset & 0x7e000)==0x7a000) || ((offset & 0x7c000)==0x7c000))
61   switch((offset & 0xe000)>>13)
3162   {
32      if ((offset & 0xffff) < 0xa000)
33         *value = card->memory[offset & 0x1fff];
34      else
35         *value = card->memory[offset & 0x7fff];
63   case 1:
64      m_ram_ptr[offset & 0x1fff] = data;
65      break;
66   case 5:
67      m_ram_ptr[(offset & 0x1fff) | 0x2000] = data;
68      break;
69   case 6:
70      m_ram_ptr[(offset & 0x1fff) | 0x4000] = data;
71      break;
72   case 7:
73      m_ram_ptr[(offset & 0x1fff) | 0x6000] = data;
74      break;
75   default:
76      break;
3677   }
3778}
3879
3980/*
40    Memory write
81    32K memory expansion does not have a CRU interface
4182*/
42static WRITE8_DEVICE_HANDLER( mem32k_w )
83void ti_32k_expcard_device::crureadz(offs_t offset, UINT8 *value)
4384{
44   ti99_mem32k_state *card = get_safe_token(device);
45   if (((offset & 0x7e000)==0x72000) || ((offset & 0x7e000)==0x7a000) || ((offset & 0x7c000)==0x7c000))
46   {
47      if ((offset & 0xffff) < 0xa000)
48         card->memory[offset & 0x1fff] = data;
49      else
50         card->memory[offset & 0x7fff] = data;
51   }
85   return;
5286}
5387
54/**************************************************************************/
55
56static const ti99_peb_card ti32k_card =
88void ti_32k_expcard_device::cruwrite(offs_t offset, UINT8 value)
5789{
58   mem32k_rz, mem32k_w,         // memory access read/write
59   NULL, NULL,                  // CRU access (none here)
60   NULL, NULL,                  // SENILA/B access (none here)
61   NULL, NULL                  // 16 bit access (none here)
62};
63
64static DEVICE_START( ti99_mem32k )
65{
66   ti99_mem32k_state *card = get_safe_token(device);
67   card->memory = NULL;
90   return;
6891}
6992
70static DEVICE_STOP( ti99_mem32k )
93void ti_32k_expcard_device::device_start(void)
7194{
72   ti99_mem32k_state *card = get_safe_token(device);
73   if (card->memory) free(card->memory);
95   m_ram_ptr = subregion(RAMREGION)->base();
96   m_cru_base = 0;
7497}
7598
76static DEVICE_RESET( ti99_mem32k )
99ROM_START( ti_exp_32k )
100   ROM_REGION(0x8000, RAMREGION, 0)
101   ROM_FILL(0x0000, 0x8000, 0x00)
102ROM_END
103
104const rom_entry *ti_32k_expcard_device::device_rom_region() const
77105{
78   ti99_mem32k_state *card = get_safe_token(device);
79   /* Register the card */
80   device_t *peb = device->owner();
81
82   if (input_port_read(device->machine(), "RAM")==RAM_TI32_EXT)
83   {
84      int success = mount_card(peb, device, &ti32k_card, get_pebcard_config(device)->slot);
85      if (!success) return;
86
87      if (card->memory==NULL) card->memory = (UINT8*)malloc(32768);
88   }
106   return ROM_NAME( ti_exp_32k );
89107}
90108
91static const char DEVTEMPLATE_SOURCE[] = __FILE__;
109const device_type TI_32KMEM = &device_creator<ti_32k_expcard_device>;
92110
93#define DEVTEMPLATE_ID(p,s)             p##ti99_mem32k##s
94#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
95#define DEVTEMPLATE_NAME                "TI99 32KiB Memory Expansion Card"
96#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
97#include "devtempl.h"
98
99DEFINE_LEGACY_DEVICE( TI32KMEM, ti99_mem32k );
100
src/mess/machine/ti99/ti_fdc.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Standard Floppy Disk Controller Card
3    Based on WD1771
4    Single Density, Double-sided
5    Michael Zapf, September 2010
6*/
4    See ti_fdc.c for documentation
75
8/* device interface */
6    Michael Zapf
7
8    September 2010
9    January 2012: rewritten as class (MZ)
10
11****************************************************************************/
12
913#ifndef __TIFDC__
1014#define __TIFDC__
1115
12DECLARE_LEGACY_DEVICE( TIFDC, ti99_fdc );
16#include "ti99defs.h"
17#include "imagedev/flopdrv.h"
18
19extern const device_type TI99_FDC;
20
21class ti_fdc_device : public ti_expansion_card_device
22{
23public:
24   ti_fdc_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   DECLARE_READ8Z_MEMBER(readz);
26   DECLARE_WRITE8_MEMBER(write);
27
28   DECLARE_WRITE_LINE_MEMBER( intrq_w );
29   DECLARE_WRITE_LINE_MEMBER( drq_w );
30
31   void crureadz(offs_t offset, UINT8 *value);
32   void cruwrite(offs_t offset, UINT8 value);
33
34protected:
35   void device_start(void);
36   void device_reset(void);
37   const rom_entry *device_rom_region() const;
38   machine_config_constructor device_mconfig_additions() const;
39
40private:
41   void handle_hold(void);
42   void set_all_geometries(floppy_type_t type);
43   void set_geometry(device_t *drive, floppy_type_t type);
44   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
45
46
47   // Holds the status of the DRQ and IRQ lines.
48   bool      m_DRQ, m_IRQ;
49
50   // When TRUE, keeps DVENA high.
51   bool      m_strobe_motor;
52
53   // Signal DVENA. When TRUE, makes some drive turning.
54   line_state   m_DVENA;
55
56   // When TRUE the CPU is halted while DRQ/IRQ are true.
57   bool      m_hold;
58
59   // Indicates which drive has been selected. Values are 0, 1, 2, and 4.
60   // 000 = no drive
61   // 001 = drive 1
62   // 010 = drive 2
63   // 100 = drive 3
64   int         m_DSEL;
65
66   // Signal SIDSEL. 0 or 1, indicates the selected head.
67   int         m_SIDSEL;
68
69   // count 4.23s from rising edge of motor_on
70   emu_timer*   m_motor_on_timer;
71
72   // Link to the FDC1771 controller on the board.
73   device_t*   m_controller;
74
75   // DSR ROM
76   UINT8*      m_dsrrom;
77};
78
1379#endif
80
src/mess/machine/ti99/evpc.c
r14513r14514
1/*
1/****************************************************************************
22    SNUG Enhanced Video Processor Card (evpc)
33    based on v9938 (may also be equipped with v9958)
44    Can be used with TI-99/4A as an add-on card; internal VDP must be removed
55
66    The SGCPU ("TI-99/4P") only runs with EVPC.
77    Michael Zapf
8    Rewritten as device
9    October 2010
10*/
11#include "emu.h"
12#include "peribox.h"
13#include "evpc.h"
14#include "emuopts.h"
158
16#define evpc_region "evpc_region"
9    October 2010: Rewritten as device
10    February 2012: Rewritten as class
1711
18#define EVPC_CRU_BASE 0x1400
12    FIXME: Locks up on startup when HFDC is present. This can be avoided
13    by using another controller (like bwg) or doing a soft reset.
1914
20typedef ti99_pebcard_config ti99_evpc_config;
15*****************************************************************************/
2116
22typedef struct _evpc_palette
23{
24   UINT8      read_index, write_index, mask;
25   int       read;
26   int       state;
27   struct { UINT8 red, green, blue; } color[0x100];
28   //int dirty;
29} evpc_palette;
17#include "evpc.h"
3018
31typedef struct _ti99_evpc_state
32{
33   int                selected;
34   UINT8               *dsrrom;
35   int                RAMEN;
36   int                dsr_page;
37   UINT8               *novram;   /* NOVRAM area */
38   evpc_palette         palette;
39   ti99_peb_connect      lines;
19#define EVPC_CRU_BASE 0x1400
20#define VERBOSE 1
21#define LOG logerror
4022
41} ti99_evpc_state;
23#define NOVRAM_SIZE 256
4224
43INLINE ti99_evpc_state *get_safe_token(device_t *device)
25snug_enhanced_video_device::snug_enhanced_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
26: ti_expansion_card_device(mconfig, TI99_EVPC, "SNUG Enhanced Video Processor Card", tag, owner, clock),
27  device_nvram_interface(mconfig, *this)
4428{
45   assert(device != NULL);
46   assert(device->type() == EVPC);
47
48   return (ti99_evpc_state *)downcast<legacy_device_base *>(device)->token();
29   m_shortname = "ti99_evpc";
4930}
5031
51static int get_evpc_switch(device_t *device, int number)
32//-------------------------------------------------
33//  nvram_default - called to initialize NVRAM to
34//  its default state
35//-------------------------------------------------
36
37void snug_enhanced_video_device::nvram_default()
5238{
53   switch (number)
54   {
55   case 0:
56      return input_port_read(device->machine(), "EVPC-SW1");
57   case 1:
58      return input_port_read(device->machine(), "EVPC-SW3");
59   case 2:
60      return input_port_read(device->machine(), "EVPC-SW4");
61   case 3:
62      return input_port_read(device->machine(), "EVPC-SW8");
63   default:
64      logerror("evpc: Invalid switch index %02x\n", number);
65      return 0;
66   }
39   memset(m_novram, 0, NOVRAM_SIZE);
6740}
6841
69/*************************************************************************/
42//-------------------------------------------------
43//  nvram_read - called to read NVRAM from the
44//  .nv file
45//-------------------------------------------------
7046
71/*
72    The CRU read handler. The CRU is a serial interface in the console.
73
74    Read EVPC CRU interface (dip switches)
75    0: Video timing (PAL/NTSC)
76    1: -
77    2: charset
78    3: RAM shift
79    4: -
80    5: -
81    6: -
82    7: DIP or NOVRAM
83    Logic is inverted
84*/
85static READ8Z_DEVICE_HANDLER( cru_rz )
47void snug_enhanced_video_device::nvram_read(emu_file &file)
8648{
87   if ((offset & 0xff00)==EVPC_CRU_BASE)
88   {
89      int bit = (offset >> 1) & 0x0f;
90      if (bit == 0)
91         *value = ~(get_evpc_switch(device, 0) | (get_evpc_switch(device, 1)<<2) | (get_evpc_switch(device, 2)<<3) | (get_evpc_switch(device, 3)<<7));
92   }
49   file.read(m_novram, NOVRAM_SIZE);
9350}
9451
95/*
96    The CRU write handler. The CRU is a serial interface in the console.
97*/
98static WRITE8_DEVICE_HANDLER( cru_w )
99{
100   ti99_evpc_state *card = get_safe_token(device);
52//-------------------------------------------------
53//  nvram_write - called to write NVRAM to the
54//  .nv file
55//-------------------------------------------------
10156
102   if ((offset & 0xff00)==EVPC_CRU_BASE)
103   {
104      int bit = (offset >> 1) & 0x0f;
105      switch (bit)
106      {
107      case 0:
108         card->selected = data;
109         break;
110
111      case 1:
112         if (data)
113            card->dsr_page |= 1;
114         else
115            card->dsr_page &= ~1;
116         break;
117
118      case 2:
119         break;
120
121      case 3:
122         card->RAMEN = data;
123         break;
124
125      case 4:
126         if (data)
127            card->dsr_page |= 4;
128         else
129            card->dsr_page &= ~4;
130         break;
131
132      case 5:
133         if (data)
134            card->dsr_page |= 2;
135         else
136            card->dsr_page &= ~2;
137         break;
138
139      case 6:
140         break;
141
142      case 7:
143         break;
144      }
145   }
57void snug_enhanced_video_device::nvram_write(emu_file &file)
58{
59   file.write(m_novram, NOVRAM_SIZE);
14660}
14761
62
14863/*
14964    Read a byte in evpc DSR space
15065    0x4000 - 0x5eff   DSR (paged)
15166    0x5f00 - 0x5fef   NOVRAM
15267    0x5ff0 - 0x5fff   Palette
15368*/
154static READ8Z_DEVICE_HANDLER( data_rz )
69READ8Z_MEMBER(snug_enhanced_video_device::readz)
15570{
156   ti99_evpc_state *card = get_safe_token(device);
157
158   if (card->selected)
71   if (m_selected)
15972   {
160      if (card->dsrrom==NULL)
73      if ((offset & m_select_mask)==m_select_value)
16174      {
162         logerror("evpc: no dsrrom\n");
163         return;
164      }
165
166      if ((offset & 0x7e000)==0x74000)
167      {
168         if ((offset & 0x1ff0)==0x1ff0)
75         if ((offset & 0x1ff0)==0x1ff0)            // Palette control
16976         {
170            /* PALETTE */
17177            switch (offset & 0x000f)
17278            {
17379            case 0:
17480               /* Palette Read Address Register */
175               *value = card->palette.write_index;
81               *value = m_palette.write_index;
17682               break;
17783
17884            case 2:
17985               /* Palette Read Color Value */
180               if (card->palette.read)
86               if (m_palette.read)
18187               {
182                  switch (card->palette.state)
88                  switch (m_palette.state)
18389                  {
18490                  case 0:
185                     *value = card->palette.color[card->palette.read_index].red;
91                     *value = m_palette.color[m_palette.read_index].red;
18692                     break;
18793                  case 1:
188                     *value = card->palette.color[card->palette.read_index].green;
94                     *value = m_palette.color[m_palette.read_index].green;
18995                     break;
19096                  case 2:
191                     *value = card->palette.color[card->palette.read_index].blue;
97                     *value = m_palette.color[m_palette.read_index].blue;
19298                     break;
19399                  }
194                  card->palette.state++;
195                  if (card->palette.state == 3)
100                  m_palette.state++;
101                  if (m_palette.state == 3)
196102                  {
197                     card->palette.state = 0;
198                     card->palette.read_index++;
103                     m_palette.state = 0;
104                     m_palette.read_index++;
199105                  }
200106               }
201107               break;
202108
203109            case 4:
204110               /* Palette Read Pixel Mask */
205               *value = card->palette.mask;
111               *value = m_palette.mask;
206112               break;
207113            case 6:
208114               /* Palette Read Address Register for Color Value */
209               if (card->palette.read)
115               if (m_palette.read)
210116                  *value = 0;
211117               else
212118                  *value = 3;
r14513r14514
217123         {
218124            if ((offset & 0x1f00)==0x1f00)
219125            {
220               /* NOVRAM */
221               if (card->RAMEN)
126               if (m_RAMEN)  // NOVRAM hides DSR
222127               {
223                  *value = card->novram[offset & 0x00ff];
128                  *value = m_novram[offset & 0x00ff];
224129               }
225               else
130               else  // DSR
226131               {
227                  *value = card->dsrrom[(offset&0x1fff) + card->dsr_page*0x2000];
132                  *value = m_dsrrom[(offset&0x1fff) | (m_dsr_page<<13)];
228133               }
229134            }
230135            else
231136            {
232               *value = card->dsrrom[(offset&0x1fff) + card->dsr_page*0x2000];
137               *value = m_dsrrom[(offset&0x1fff) | (m_dsr_page<<13)];
233138            }
234139         }
235140      }
r14513r14514
242147    0x5f00 - 0x5fef   NOVRAM
243148    0x5ff0 - 0x5fff   Palette
244149*/
245static WRITE8_DEVICE_HANDLER( data_w )
150WRITE8_MEMBER(snug_enhanced_video_device::write)
246151{
247   ti99_evpc_state *card = get_safe_token(device);
248   if (card->selected)
152   if (m_selected)
249153   {
250      if ((offset & 0x7e000)==0x74000)
154      if ((offset & m_select_mask)==m_select_value)
251155      {
252156         if ((offset & 0x1ff0)==0x1ff0)
253157         {
254158            /* PALETTE */
255            logerror("palette write, offset=%d\n, data=%d", offset-0x5ff0, data);
159            if (VERBOSE>5) LOG("palette write %04x <- %02x\n", offset&0xffff, data);
256160            switch (offset & 0x000f)
257161            {
258            case 8:
162            case 0x08:
259163               /* Palette Write Address Register */
260               logerror("EVPC palette address write (for write access)\n");
261               card->palette.write_index = data;
262               card->palette.state = 0;
263               card->palette.read = 0;
164               if (VERBOSE>5) LOG("EVPC palette address write (for write access)\n");
165               m_palette.write_index = data;
166               m_palette.state = 0;
167               m_palette.read = 0;
264168               break;
265169
266            case 10:
170            case 0x0a:
267171               /* Palette Write Color Value */
268               logerror("EVPC palette color write\n");
269               if (!card->palette.read)
172               if (VERBOSE>5) LOG("EVPC palette color write\n");
173               if (!m_palette.read)
270174               {
271                  switch (card->palette.state)
175                  switch (m_palette.state)
272176                  {
273177                  case 0:
274                     card->palette.color[card->palette.write_index].red = data;
178                     m_palette.color[m_palette.write_index].red = data;
275179                     break;
276180                  case 1:
277                     card->palette.color[card->palette.write_index].green = data;
181                     m_palette.color[m_palette.write_index].green = data;
278182                     break;
279183                  case 2:
280                     card->palette.color[card->palette.write_index].blue = data;
184                     m_palette.color[m_palette.write_index].blue = data;
281185                     break;
282186                  }
283                  card->palette.state++;
284                  if (card->palette.state == 3)
187                  m_palette.state++;
188                  if (m_palette.state == 3)
285189                  {
286                     card->palette.state = 0;
287                     card->palette.write_index++;
190                     m_palette.state = 0;
191                     m_palette.write_index++;
288192                  }
289193                  //evpc_palette.dirty = 1;
290194               }
291195               break;
292196
293            case 12:
197            case 0x0c:
294198               /* Palette Write Pixel Mask */
295               logerror("EVPC palette mask write\n");
296               card->palette.mask = data;
199               if (VERBOSE>5) LOG("EVPC palette mask write\n");
200               m_palette.mask = data;
297201               break;
298202
299            case 14:
203            case 0x0e:
300204               /* Palette Write Address Register for Color Value */
301               logerror("EVPC palette address write (for read access)\n");
302               card->palette.read_index = data;
303               card->palette.state = 0;
304               card->palette.read = 1;
205               if (VERBOSE>5) LOG("EVPC palette address write (for read access)\n");
206               m_palette.read_index = data;
207               m_palette.state = 0;
208               m_palette.read = 1;
305209               break;
306210            }
307211         }
r14513r14514
309213         {
310214            if ((offset & 0x1f00)==0x1f00)
311215            {
312               if (card->RAMEN)
216               if (m_RAMEN)
313217               {
314218                  // NOVRAM
315                  card->novram[offset & 0x00ff] = data;
219                  m_novram[offset & 0x00ff] = data;
316220               }
317221            }
318222         }
r14513r14514
320224   }
321225}
322226
323static const ti99_peb_card evpc_card =
227/*
228    The CRU read handler. Read EVPC DIP switches
229    0: Video timing (PAL/NTSC)
230    1: -
231    2: charset
232    3: RAM shift
233    4: -
234    5: -
235    6: -
236    7: DIP or NOVRAM
237    Logic is inverted
238*/
239void snug_enhanced_video_device::crureadz(offs_t offset, UINT8 *value)
324240{
325   data_rz,
326   data_w,
327   cru_rz,
328   cru_w,
241   if ((offset & 0xff00)==EVPC_CRU_BASE)
242   {
243      if ((offset & 0x00f0)==0) // offset 0 delivers bits 0-7 (address 00-0f)
244      {
245         *value = ~(input_port_read(*this, "EVPC-SW1") | (input_port_read(*this, "EVPC-SW3")<<2)
246            | (input_port_read(*this, "EVPC-SW4")<<3) | (input_port_read(*this, "EVPC-SW8")<<7));
247      }
248   }
249}
329250
330   NULL, NULL,   NULL, NULL
331};
332
333static DEVICE_START( ti99_evpc )
251/*
252    The CRU write handler.
253    Bit 0: Turn on DSR ROM
254    Bit 1: DSR page select (bit 0)
255    Bit 2: -
256    Bit 3: RAM enable
257    Bit 4: DSR page select (bit 2)
258    Bit 5: DSR page select (bit 1)
259    Bit 6: -
260    Bit 7: -
261*/
262void snug_enhanced_video_device::cruwrite(offs_t offset, UINT8 data)
334263{
335   ti99_evpc_state *card = get_safe_token(device);
336   card->novram = (UINT8*)malloc(256); // need that already now for NVRAM handling
264   if ((offset & 0xff00)==EVPC_CRU_BASE)
265   {
266      int bit = (offset >> 1) & 0x0f;
267      switch (bit)
268      {
269      case 0:
270         m_selected = (data!=0);
271         if (VERBOSE>4) LOG("evpc: Map DSR = %d\n", m_selected);
272         break;
337273
338   /* Resolve the callbacks to the PEB */
339   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
340   card->lines.ready.resolve(topeb->ready, *device);
274      case 1:
275         if (data!=0)
276            m_dsr_page |= 1;
277         else
278            m_dsr_page &= ~1;
279         break;
280
281      case 3:
282         m_RAMEN = (data!=0);
283         break;
284
285      case 4:
286         if (data!=0)
287            m_dsr_page |= 4;
288         else
289            m_dsr_page &= ~4;
290         break;
291
292      case 5:
293         if (data!=0)
294            m_dsr_page |= 2;
295         else
296            m_dsr_page &= ~2;
297         break;
298
299      case 2:
300      case 6:
301      case 7:
302         break;
303      }
304   }
341305}
342306
343static DEVICE_STOP( ti99_evpc )
307void snug_enhanced_video_device::device_start()
344308{
345   logerror("ti99_evpc: stop\n");
346   ti99_evpc_state *card = get_safe_token(device);
347   free(card->novram);
309   m_dsrrom = subregion(DSRROM)->base();
310   m_novram = (UINT8*)malloc(NOVRAM_SIZE);
348311}
349312
350static DEVICE_RESET( ti99_evpc )
313void snug_enhanced_video_device::device_reset()
351314{
352   logerror("ti99_evpc: reset\n");
353   ti99_evpc_state *card = get_safe_token(device);
315   if (VERBOSE>5) LOG("evpc: reset\n");
316   m_select_mask = 0x7e000;
317   m_select_value = 0x74000;
318   m_dsr_page = 0;
319   m_RAMEN = false;
320}
354321
355   /* If the card is selected in the menu, register the card */
356   device_t *peb = device->owner();
357   int success = mount_card(peb, device, &evpc_card, get_pebcard_config(device)->slot);
358   if (!success)
359   {
360      logerror("evpc: Could not mount card.\n");
361      return;
362   }
363   card->RAMEN = 0;
364   card->dsr_page = 0;
322void snug_enhanced_video_device::device_stop()
323{
324   free(m_novram);
325}
365326
366   astring region(device->tag(), ":", evpc_region);
327ROM_START( ti99_evpc )
328   ROM_REGION(0x10000, DSRROM, 0)
329   ROM_LOAD("evpcdsr.bin", 0, 0x10000, CRC(a062b75d) SHA1(6e8060f86e3bb9c36f244d88825e3fe237bfe9a9)) /* evpc DSR ROM */
330ROM_END
367331
368   card->dsrrom = device->machine().region(region.cstr())->base();
369}
370332/*
371static DEVICE_NVRAM( ti99_evpc )
372{
373    // Called between START and RESET
374    ti99_evpc_state *card = get_safe_token(device);
375    astring hsname(device->machine().system().name, PATH_SEPARATOR, "evpc.nv");
376    file_error filerr;
333    Input ports for the EPVC
334*/
335INPUT_PORTS_START( ti99_evpc )
336   PORT_START( "EVPC-SW1" )
337   PORT_DIPNAME( 0x01, 0x00, "EVPC video mode" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
338      PORT_DIPSETTING(    0x00, "PAL" )
339      PORT_DIPSETTING(    0x01, "NTSC" )
377340
378    if (read_or_write==0)
379    {
380        logerror("evpc: device nvram load %s\n", hsname.cstr());
341   PORT_START( "EVPC-SW3" )
342   PORT_DIPNAME( 0x01, 0x00, "EVPC charset" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
343      PORT_DIPSETTING(    0x00, DEF_STR( International ))
344      PORT_DIPSETTING(    0x01, DEF_STR( German ))
381345
382        emu_file nvfile(device->machine().options().nvram_directory(), OPEN_FLAG_READ);
383        filerr = nvfile.open(hsname.cstr());
384        if (filerr == FILERR_NONE)
385        {
386            if (nvfile.read(card->novram, 256) != 256)
387                logerror("evpc: NOVRAM load error\n");
388        }
389    }
390    else
391    {
392        logerror("evpc: device nvram save %s\n", hsname.cstr());
393        emu_file nvfile(device->machine().options().nvram_directory(), OPEN_FLAG_WRITE | OPEN_FLAG_CREATE | OPEN_FLAG_CREATE_PATHS);
394        filerr = nvfile.open(hsname.cstr());
346   PORT_START( "EVPC-SW4" )
347   PORT_DIPNAME( 0x01, 0x00, "EVPC VDP RAM" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
348      PORT_DIPSETTING(    0x00, "shifted" )
349      PORT_DIPSETTING(    0x01, "not shifted" )
395350
396        if (filerr == FILERR_NONE)
397        {
398            if (nvfile.write(card->novram, 256) != 256)
399                logerror("evpc: NOVRAM save error\n");
400        }
401    }
351   PORT_START( "EVPC-SW8" )
352   PORT_DIPNAME( 0x01, 0x00, "EVPC Configuration" )
353      PORT_DIPSETTING(    0x00, "DIP" )
354      PORT_DIPSETTING(    0x01, "NOVRAM" )
355INPUT_PORTS_END
356
357const rom_entry *snug_enhanced_video_device::device_rom_region() const
358{
359   return ROM_NAME( ti99_evpc );
402360}
403*/
404MACHINE_CONFIG_FRAGMENT( ti99_evpc )
405MACHINE_CONFIG_END
406361
407ROM_START( ti99_evpc )
408   ROM_REGION(0x10000, evpc_region, 0)
409   ROM_LOAD_OPTIONAL("evpcdsr.bin", 0, 0x10000, CRC(a062b75d) SHA1(6e8060f86e3bb9c36f244d88825e3fe237bfe9a9)) /* evpc DSR ROM */
410ROM_END
362ioport_constructor snug_enhanced_video_device::device_input_ports() const
363{
364   return INPUT_PORTS_NAME(ti99_evpc);
365}
411366
412static const char DEVTEMPLATE_SOURCE[] = __FILE__;
367const device_type TI99_EVPC = &device_creator<snug_enhanced_video_device>;
413368
414#define DEVTEMPLATE_ID(p,s)             p##ti99_evpc##s
415#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
416#define DEVTEMPLATE_NAME                "SNUG Enhanced Video Processor Card"
417#define DEVTEMPLATE_SHORTNAME           "snugvdc"
418#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
419#include "devtempl.h"
420
421DEFINE_LEGACY_DEVICE(EVPC, ti99_evpc);
src/mess/machine/ti99/p_code.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 P-Code Card emulation.
3    Michael Zapf, July 2009
4    See p_code.c for documentation
45
6    Michael Zapf
7    July 2009
58    Revised July 2010
6*/
9    February 2012: Rewritten as class
710
8#ifndef __PCODEN__
9#define __PCODEN__
11*****************************************************************************/
1012
11DECLARE_LEGACY_DEVICE( PCODEN, ti99_pcoden );
13#ifndef __PCODE__
14#define __PCODE__
15
16#include "emu.h"
17#include "peribox.h"
18#include "grom.h"
19
20extern const device_type TI99_P_CODE;
21
22class ti_pcode_card_device : public ti_expansion_card_device
23{
24public:
25   ti_pcode_card_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
26   DECLARE_READ8Z_MEMBER(readz);
27   DECLARE_WRITE8_MEMBER(write);
28   void crureadz(offs_t offset, UINT8 *value);
29   void cruwrite(offs_t offset, UINT8 data);
30
31   DECLARE_WRITE_LINE_MEMBER( ready_line );
32   DECLARE_INPUT_CHANGED_MEMBER( switch_changed );
33
34protected:
35   void device_start();
36   void device_reset();
37   void device_config_complete();
38   const rom_entry *device_rom_region() const;
39   machine_config_constructor device_mconfig_additions() const;
40   ioport_constructor device_input_ports() const;
41
42private:
43   ti99_grom_device*   m_grom[8];
44   UINT8*            m_rom;
45   int               m_bank_select;
46   bool            m_switch;
47};
48
1249#endif
src/mess/machine/ti99/videowrp.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/4(A) and /8 Video subsystem
34    This device actually wraps the naked video chip implementation
45
r14513r14514
67    based on v9938 (may also be equipped with v9958)
78    Can be used with TI-99/4A as an add-on card; internal VDP must be removed
89
9    The SGCPU ("TI-99/4P") only runs with EVPC.
10    The SGCPU ("TI-99/4P") only runs with EVPC
1011
11    Michael Zapf, October 2010
12*/
12    We also include a class wrapper for the sound chip here.
1313
14    Michael Zapf
15
16    October 2010
17    February 2012: Rewritten as class
18
19*****************************************************************************/
20
1421#include "emu.h"
1522#include "videowrp.h"
23#include "sound/sn76496.h"
1624
17typedef struct _ti99_video_state
25/*
26    Constructors
27*/
28ti_video_device::ti_video_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock)
29: bus8z_device(mconfig, type, name, tag, owner, clock)
1830{
19   address_space   *space;
20   device_t   *cpu;
21   int            chip;
22   v9938_device *v9938;
23} ti99_video_state;
31}
2432
25INLINE ti99_video_state *get_safe_token(device_t *device)
33ti_std_video_device::ti_std_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
34   : ti_video_device(mconfig, TI994AVIDEO, "Video subsystem", tag, owner, clock)
2635{
27   assert(device != NULL);
28   assert(device->type() == TIVIDEO);
36}
2937
30   return (ti99_video_state *)downcast<legacy_device_base *>(device)->token();
38ti_std8_video_device::ti_std8_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
39   : ti_video_device(mconfig, TI998VIDEO, "Video subsystem", tag, owner, clock)
40{
3141}
3242
33INLINE const ti99_video_config *get_config(device_t *device)
43ti_exp_video_device::ti_exp_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
44   : ti_video_device(mconfig, V9938VIDEO, "Video subsystem", tag, owner, clock)
3445{
35   assert(device != NULL);
36   assert(device->type() == TIVIDEO);
46}
3747
38   return (const ti99_video_config *) downcast<const legacy_device_base *>(device)->inline_config();
48/*****************************************************************************/
49/*
50    Illegal accesses; just take a wait state. Used by TI-99/4A standard or with EVPC
51*/
52
53READ16_MEMBER( ti_video_device::noread )
54{
55   device_adjust_icount(m_cpu, -4);
56   return 0;
3957}
4058
59WRITE16_MEMBER( ti_video_device::nowrite )
60{
61   device_adjust_icount(m_cpu, -4);
62}
4163
4264/*****************************************************************************/
4365
66// TODO: device_adjust_icount(video->cpu, -4);
67
4468/*
45    Memory read (TI-99/4(A))
69    Memory access (TI-99/4(A))
4670*/
47READ16_DEVICE_HANDLER( ti_tms991x_r16 )
71READ16_MEMBER( ti_std_video_device::read16 )
4872{
49   ti99_video_state *video = get_safe_token(device);
50   tms9928a_device *tms9928a = device->machine().device<tms9928a_device>( TMS9928A_TAG );
51//  device_adjust_icount(video->cpu, -4);
52
5373   if (offset & 1)
5474   {   /* read VDP status */
55      return ((int) tms9928a->register_read(*(video->space), 0)) << 8;
75      return ((int) m_tms9928a->register_read(*(this->m_space), 0)) << 8;
5676   }
5777   else
5878   {   /* read VDP RAM */
59      return ((int) tms9928a->vram_read(*(video->space), 0)) << 8;
79      return ((int) m_tms9928a->vram_read(*(this->m_space), 0)) << 8;
6080   }
6181}
6282
63/*
64    Memory read (TI-99/8). Makes use of the Z memory handler.
65*/
66READ8Z_DEVICE_HANDLER( ti8_tms991x_rz )
83WRITE16_MEMBER( ti_std_video_device::write16 )
6784{
68   ti99_video_state *video = get_safe_token(device);
69   tms9928a_device *tms9928a = device->machine().device<tms9928a_device>( TMS9928A_TAG );
70//  device_adjust_icount(video->cpu, -4);
71
72   if (offset & 2)
73   {   /* read VDP status */
74      *value = tms9928a->register_read(*(video->space), 0);
85   if (offset & 1)
86   {   /* write VDP address */
87      m_tms9928a->register_write(*(this->m_space), 0, (data >> 8) & 0xff);
7588   }
7689   else
77   {   /* read VDP RAM */
78      *value = tms9928a->vram_read(*(video->space), 0);
90   {   /* write VDP data */
91      m_tms9928a->vram_write(*(this->m_space), 0, (data >> 8) & 0xff);
7992   }
8093}
8194
95/******************************************************************************/
96
8297/*
83    Memory read (EVPC)
98    Memory access (TI-99/8)
99    Makes use of the Z memory handler.
84100*/
85READ16_DEVICE_HANDLER( ti_v9938_r16 )
101READ8Z_MEMBER( ti_std8_video_device::readz )
86102{
87   ti99_video_state *video = get_safe_token(device);
88//  device_adjust_icount(video->cpu, -4);
89
90   if (offset & 1)
91   {   /* read VDP status */
92      return ((int) video->v9938->status_r()) << 8;
103   if (offset & 2)
104   {       /* read VDP status */
105      *value = m_tms9928a->register_read(*(this->m_space), 0);
93106   }
94107   else
95   {   /* read VDP RAM */
96      return ((int) video->v9938->vram_r()) << 8;
108   {       /* read VDP RAM */
109      *value = m_tms9928a->vram_read(*(this->m_space), 0);
97110   }
98111}
99112
100/*
101    Memory write
102*/
103WRITE16_DEVICE_HANDLER( ti_tms991x_w16 )
113WRITE8_MEMBER( ti_std8_video_device::write )
104114{
105   ti99_video_state *video = get_safe_token(device);
106   tms9928a_device *tms9928a = device->machine().device<tms9928a_device>( TMS9928A_TAG );
107//  device_adjust_icount(video->cpu, -4);
108
109   if (offset & 1)
115   if (offset & 2)
110116   {   /* write VDP address */
111      tms9928a->register_write(*(video->space), 0, (data >> 8) & 0xff);
117      m_tms9928a->register_write(*(this->m_space), 0, data);
112118   }
113119   else
114120   {   /* write VDP data */
115      tms9928a->vram_write(*(video->space), 0, (data >> 8) & 0xff);
121      m_tms9928a->vram_write(*(this->m_space), 0, data);
116122   }
117123}
118124
125/*****************************************************************************/
126
119127/*
120    Memory write (TI-99/8)
128    Memory access (EVPC) via 16 bit bus
121129*/
122WRITE8_DEVICE_HANDLER( ti8_tms991x_w )
130READ16_MEMBER( ti_exp_video_device::read16 )
123131{
124   ti99_video_state *video = get_safe_token(device);
125   tms9928a_device *tms9928a = device->machine().device<tms9928a_device>( TMS9928A_TAG );
126//  device_adjust_icount(video->cpu, -4);
127
128   if (offset & 2)
129   {   /* write VDP address */
130      tms9928a->register_write(*(video->space), 0, data);
132   if (offset & 1)
133   {   /* read VDP status */
134      return ((int) m_v9938->status_r()) << 8;
131135   }
132136   else
133   {   /* write VDP data */
134      tms9928a->vram_write(*(video->space), 0, data);
137   {   /* read VDP RAM */
138      return ((int) m_v9938->vram_r()) << 8;
135139   }
136140}
137141
138/*
139    Memory write (EVPC)
140*/
141WRITE16_DEVICE_HANDLER ( ti_v9938_w16 )
142WRITE16_MEMBER( ti_exp_video_device::write16 )
142143{
143   ti99_video_state *video = get_safe_token(device);
144//  device_adjust_icount(video->cpu, -4);
145
146144   switch (offset & 3)
147145   {
148146   case 0:
149147      /* write VDP data */
150      video->v9938->vram_w((data >> 8) & 0xff);
148      m_v9938->vram_w((data >> 8) & 0xff);
151149      break;
152150   case 1:
153151      /* write VDP address */
154      video->v9938->command_w((data >> 8) & 0xff);
152      m_v9938->command_w((data >> 8) & 0xff);
155153      break;
156154   case 2:
157155      /* write VDP palette */
158      video->v9938->palette_w((data >> 8) & 0xff);
156      m_v9938->palette_w((data >> 8) & 0xff);
159157      break;
160158   case 3:
161159      /* write VDP register pointer (indirect access) */
162      video->v9938->register_w((data >> 8) & 0xff);
160      m_v9938->register_w((data >> 8) & 0xff);
163161      break;
164162   }
165163}
166164
165/******************************************************************************/
166
167167/*
168    Video write (Geneve)
168    Video read (Geneve) via 8 bit bus
169169*/
170WRITE8_DEVICE_HANDLER ( gen_v9938_w )
170READ8Z_MEMBER( ti_exp_video_device::readz )
171171{
172   ti99_video_state *video = get_safe_token(device);
173//  device_adjust_icount(video->cpu, -4);
172   if (offset & 2)
173   {   /* read VDP status */
174      *value = m_v9938->status_r();
175   }
176   else
177   {   /* read VDP RAM */
178      *value = m_v9938->vram_r();
179   }
180}
174181
182/*
183    Video write (Geneve)
184*/
185WRITE8_MEMBER( ti_exp_video_device::write )
186{
175187   switch (offset & 6)
176188   {
177189   case 0:
178190      /* write VDP data */
179      video->v9938->vram_w(data);
191      m_v9938->vram_w(data);
180192      break;
181193   case 2:
182194      /* write VDP address */
183      video->v9938->command_w(data);
195      m_v9938->command_w(data);
184196      break;
185197   case 4:
186198      /* write VDP palette */
187      video->v9938->palette_w(data);
199      m_v9938->palette_w(data);
188200      break;
189201   case 6:
190202      /* write VDP register pointer (indirect access) */
191      video->v9938->register_w(data);
203      m_v9938->register_w(data);
192204      break;
193205   }
194206}
195207
196/*
197    Video read (Geneve).
198*/
199READ8Z_DEVICE_HANDLER( gen_v9938_rz )
200{
201   ti99_video_state *video = get_safe_token(device);
202//  device_adjust_icount(video->cpu, -4);
208/**************************************************************************/
209// Interfacing to mouse attached to v9938
203210
204   if (offset & 2)
205   {   /* read VDP status */
206      *value = video->v9938->status_r();
207   }
208   else
209   {   /* read VDP RAM */
210      *value = video->v9938->vram_r();
211   }
211void ti_exp_video_device::video_update_mouse(int delta_x, int delta_y, int buttons)
212{
213   m_v9938->update_mouse_state(delta_x, delta_y, buttons & 3);
212214}
213215
214READ16_DEVICE_HANDLER ( ti_video_rnop )
216/**************************************************************************/
217
218void ti_video_device::device_start(void)
215219{
216   ti99_video_state *video = get_safe_token(device);
217   device_adjust_icount(video->cpu, -4);
218   return 0;
220   m_cpu = machine().device("maincpu");
221   m_space = m_cpu->memory().space(AS_PROGRAM);
222   m_tms9928a = static_cast<tms9928a_device*>(machine().device(TMS9928A_TAG));
219223}
220224
221WRITE16_DEVICE_HANDLER ( ti_video_wnop )
225void ti_exp_video_device::device_start(void)
222226{
223   ti99_video_state *video = get_safe_token(device);
224   device_adjust_icount(video->cpu, -4);
227   m_cpu = machine().device("maincpu");
228   m_space = m_cpu->memory().space(AS_PROGRAM);
229   m_v9938 = static_cast<v9938_device*>(machine().device(V9938_TAG));
225230}
226/**************************************************************************/
227// Interfacing to mouse attached to v9938
228231
229void video_update_mouse( device_t *device, int delta_x, int delta_y, int buttons)
232void ti_video_device::device_reset(void)
230233{
231   ti99_video_state *video = get_safe_token(device);
232   // TODO: V9938 to be devicified
233   if (video->chip==TI_V9938)
234      video->v9938->update_mouse_state(delta_x, delta_y, buttons & 3);
235234}
236235
237
238236/**************************************************************************/
239237
240static DEVICE_START( ti99_video )
238ti_sound_system_device::ti_sound_system_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
239: bus8z_device(mconfig, TISOUND, "TI sound chip wrapper", tag, owner, clock)
241240{
242   ti99_video_state *video = get_safe_token(device);
243   const ti99_video_config* conf = (const ti99_video_config*)get_config(device);
244
245   video->cpu = device->machine().device("maincpu");
246   video->space = device->machine().device("maincpu")->memory().space(AS_PROGRAM);
247   video->chip = conf->chip;
248
249   if (video->chip == TI_V9938)
250   {
251      astring temp(device->tag(), "_v9938");
252      video->v9938 = device->machine().device<v9938_device>(temp);
253      assert(video->v9938 != NULL);
254   }
255241}
256242
257static DEVICE_STOP( ti99_video )
243WRITE8_MEMBER( ti_sound_system_device::write )
258244{
245   sn76496_w(m_sound_chip, offset, data);
259246}
260247
261static DEVICE_RESET( ti99_video )
248void ti_sound_system_device::device_start(void)
262249{
263   const ti99_video_config* conf = (const ti99_video_config*)get_config(device);
264   if (conf->chip!=TI_TMS991X)
265   {
266//      running_machine &machine = device->machine();
267//      int memsize = (input_port_read(machine, "V9938-MEM")==0)? 0x20000 : 0x30000;
268//
269//      v9938_init(machine, 0, *machine.primary_screen, machine.primary_screen->default_bitmap(),
270//          MODEL_V9938, memsize, conf->callback);
271//      v9938_reset(0);
272   }
250   m_sound_chip = machine().device(TISOUNDCHIP_TAG);
273251}
274252
275static const char DEVTEMPLATE_SOURCE[] = __FILE__;
253/**************************************************************************/
276254
277#define DEVTEMPLATE_ID(p,s)             p##ti99_video##s
278#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
279#define DEVTEMPLATE_NAME                "TI-99/x Video subsystem"
280#define DEVTEMPLATE_FAMILY              "Internal device"
281#include "devtempl.h"
282
283DEFINE_LEGACY_DEVICE( TIVIDEO, ti99_video );
284
255const device_type TI994AVIDEO = &device_creator<ti_std_video_device>;
256const device_type TI998VIDEO = &device_creator<ti_std8_video_device>;
257const device_type V9938VIDEO = &device_creator<ti_exp_video_device>;
258const device_type TISOUND = &device_creator<ti_sound_system_device>;
src/mess/machine/ti99/tn_usbsm.h
r14513r14514
1/*
1/****************************************************************************
2
23    Nouspikel USB / SmartMedia interface card
3    Michael Zapf, September 2010
4*/
4    See tn_usmsm.c for documentation
55
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
612#ifndef __USBSMART__
713#define __USBSMART__
814
9DECLARE_LEGACY_DEVICE( USBSMART, tn_usbsm );
15#include "emu.h"
16#include "ti99defs.h"
17#include "peribox.h"
18
19extern const device_type TI99_USBSM;
20
21class nouspikel_usb_smartmedia_device : public ti_expansion_card_device
22{
23public:
24   nouspikel_usb_smartmedia_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   DECLARE_READ8Z_MEMBER(readz);
26   DECLARE_WRITE8_MEMBER(write);
27
28   void   crureadz(offs_t offset, UINT8 *value);
29   void   cruwrite(offs_t offset, UINT8 value);
30
31protected:
32   void device_start(void);
33   void device_reset(void);
34   const rom_entry *device_rom_region() const;
35   machine_config_constructor device_mconfig_additions() const;
36   ioport_constructor device_input_ports() const;
37
38private:
39   UINT16      usbsm_mem_16_r(offs_t offset);
40   void      usbsm_mem_16_w(offs_t offset, UINT16 data);
41   device_t*   m_smartmedia;
42   device_t*   m_strata;
43
44   int         m_feeprom_page;
45   int         m_sram_page;
46   int         m_cru_register;
47   bool      m_tms9995_mode;
48
49   UINT16      m_input_latch;
50   UINT16      m_output_latch;
51   UINT16*      m_ram;
52};
53
1054#endif
src/mess/machine/ti99/speech8.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/8 Speech synthesizer subsystem
34
45    The TI-99/8 contains a speech synthesizer inside the console, so we cannot
56    reuse the spchsyn implementation of the P-Box speech synthesizer.
7    Accordingly, this is not a ti_expansion_card_device.
68
7    Note that this subsystem also contains the speech roms.
8*/
9#include "emu.h"
9    Michael Zapf
10    February 2012: Rewritten as class
11
12*****************************************************************************/
13
1014#include "speech8.h"
11#include "sound/tms5220.h"
1215#include "sound/wave.h"
1316
1417#define TMS5220_ADDRESS_MASK 0x3FFFFUL   /* 18-bit mask for tms5220 address */
15#define speech8_region "speech8_region"
1618
17typedef struct _ti99_speech8_state
19#define VERBOSE 1
20#define LOG logerror
21
22#define SPEECHSYN_TAG "speechsyn"
23#define SPEECHROM_TAG "speechrom"
24
25/****************************************************************************/
26
27ti998_spsyn_device::ti998_spsyn_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
28: bus8z_device(mconfig, TI99_SPEECH8, "TI-99/8 Speech synthesizer (onboard)", tag, owner, clock)
1829{
19   device_t         *vsp;
20   UINT8               *speechrom_data;      /* pointer to speech ROM data */
21   int                load_pointer;         /* which 4-bit nibble will be affected by load address */
22   int                ROM_bits_count;            /* current bit position in ROM */
23   UINT32               speechROMaddr;            /* 18 bit pointer in ROM */
24   UINT32               speechROMlen;         /* length of data pointed by speechrom_data, from 0 to 2^18 */
30   m_shortname = "ti99_speech8";
31}
2532
26} ti99_speech8_state;
33/*
34    Memory read
35*/
36READ8Z_MEMBER( ti998_spsyn_device::readz )
37{
38   if ((offset & m_select_mask)==m_select_value)
39   {
40      device_adjust_icount(machine().device("maincpu"),-(18+3));      /* this is just a minimum, it can be more */
41      *value = tms5220_status_r(m_vsp, offset) & 0xff;
42   }
43}
2744
28INLINE ti99_speech8_state *get_safe_token(device_t *device)
45/*
46    Memory write
47*/
48WRITE8_MEMBER( ti998_spsyn_device::write )
2949{
30   assert(device != NULL);
31   assert(device->type() == TISPEECH8);
50   if ((offset & m_select_mask)==(m_select_value | 0x0400))
51   {
52      device_adjust_icount(machine().device("maincpu"),-(54+3));      /* this is just an approx. minimum, it can be much more */
3253
33   return (ti99_speech8_state *)downcast<legacy_device_base *>(device)->token();
54      /* RN: the stupid design of the tms5220 core means that ready is cleared */
55      /* when there are 15 bytes in FIFO.  It should be 16.  Of course, if */
56      /* it were the case, we would need to store the value on the bus, */
57      /* which would be more complex. */
58      if (!tms5220_readyq_r(m_vsp))
59      {
60         attotime time_to_ready = attotime::from_double(tms5220_time_to_ready(m_vsp));
61         int cycles_to_ready = machine().device<cpu_device>("maincpu")->attotime_to_cycles(time_to_ready);
62         if (VERBOSE>8) LOG("time to ready: %f -> %d\n", time_to_ready.as_double(), (int) cycles_to_ready);
63
64         device_adjust_icount(machine().device("maincpu"),-cycles_to_ready);
65         machine().scheduler().timer_set(attotime::zero, FUNC_NULL);
66      }
67      tms5220_data_w(m_vsp, offset, data);
68   }
3469}
3570
36/*****************************************************************************/
71/****************************************************************************
72    Callbacks from TMS5220
3773
74    TODO: Convert to methods as soon as TMS5220 has been converted
75*****************************************************************************/
3876/*
3977    Read 'count' bits serially from speech ROM
4078*/
41static int ti99_spchroms_read(device_t *vspdev, int count)
79static int ti998_spchroms_read(device_t *vspdev, int count)
4280{
43   device_t *device = vspdev->owner();
44   ti99_speech8_state *spsys = get_safe_token(device);
81   ti998_spsyn_device *adapter = static_cast<ti998_spsyn_device*>(vspdev->owner());
4582
4683   int val;
4784
48   if (spsys->load_pointer)
85   if (adapter->m_load_pointer)
4986   {   /* first read after load address is ignored */
50      spsys->load_pointer = 0;
87      adapter->m_load_pointer = 0;
5188      count--;
5289   }
5390
54   if (spsys->speechROMaddr < spsys->speechROMlen)
91   if (adapter->m_sprom_address < adapter->m_sprom_length)
5592   {
56      if (count < spsys->ROM_bits_count)
93      if (count < adapter->m_rombits_count)
5794      {
58         spsys->ROM_bits_count -= count;
59         val = (spsys->speechrom_data[spsys->speechROMaddr] >> spsys->ROM_bits_count) & (0xFF >> (8 - count));
95         adapter->m_rombits_count -= count;
96         val = (adapter->m_speechrom[adapter->m_sprom_address] >> adapter->m_rombits_count) & (0xFF >> (8 - count));
6097      }
6198      else
6299      {
63         val = ((int)spsys->speechrom_data[spsys->speechROMaddr]) << 8;
100         val = ((int)adapter->m_speechrom[adapter->m_sprom_address]) << 8;
64101
65         spsys->speechROMaddr = (spsys->speechROMaddr + 1) & TMS5220_ADDRESS_MASK;
102         adapter->m_sprom_address = (adapter->m_sprom_address + 1) & TMS5220_ADDRESS_MASK;
66103
67         if (spsys->speechROMaddr < spsys->speechROMlen)
68            val |= spsys->speechrom_data[spsys->speechROMaddr];
104         if (adapter->m_sprom_address < adapter->m_sprom_length)
105            val |= adapter->m_speechrom[adapter->m_sprom_address];
69106
70         spsys->ROM_bits_count += 8 - count;
107         adapter->m_rombits_count += 8 - count;
71108
72         val = (val >> spsys->ROM_bits_count) & (0xFF >> (8 - count));
109         val = (val >> adapter->m_rombits_count) & (0xFF >> (8 - count));
73110      }
74111   }
75112   else
r14513r14514
81118/*
82119    Write an address nibble to speech ROM
83120*/
84static void ti99_spchroms_load_address(device_t *vspdev, int data)
121static void ti998_spchroms_load_address(device_t *vspdev, int data)
85122{
86   device_t *device = vspdev->owner();
87   ti99_speech8_state *spsys = get_safe_token(device);
123   ti998_spsyn_device *adapter = static_cast<ti998_spsyn_device*>(vspdev->owner());
124
88125   // tms5220 data sheet says that if we load only one 4-bit nibble, it won't work.
89126   // This code does not care about this.
90   spsys->speechROMaddr = ((spsys->speechROMaddr & ~(0xf << spsys->load_pointer))
91      | (((unsigned long) (data & 0xf)) << spsys->load_pointer) ) & TMS5220_ADDRESS_MASK;
92   spsys->load_pointer += 4;
93   spsys->ROM_bits_count = 8;
127   adapter->m_sprom_address = ((adapter->m_sprom_address & ~(0xf << adapter->m_load_pointer))
128      | (((unsigned long) (data & 0xf)) << adapter->m_load_pointer) ) & TMS5220_ADDRESS_MASK;
129   adapter->m_load_pointer += 4;
130   adapter->m_rombits_count = 8;
94131}
95132
96133/*
97134    Perform a read and branch command
98135*/
99static void ti99_spchroms_read_and_branch(device_t *vspdev)
136static void ti998_spchroms_read_and_branch(device_t *vspdev)
100137{
101   device_t *device = vspdev->owner();
102   ti99_speech8_state *spsys = get_safe_token(device);
138   ti998_spsyn_device *adapter = static_cast<ti998_spsyn_device*>(vspdev->owner());
103139   // tms5220 data sheet says that if more than one speech ROM (tms6100) is present,
104140   // there is a bus contention.  This code does not care about this. */
105   if (spsys->speechROMaddr < spsys->speechROMlen-1)
106      spsys->speechROMaddr = (spsys->speechROMaddr & 0x3c000UL)
107         | (((((unsigned long) spsys->speechrom_data[spsys->speechROMaddr]) << 8)
108         | spsys->speechrom_data[spsys->speechROMaddr+1]) & 0x3fffUL);
109   else if (spsys->speechROMaddr == spsys->speechROMlen-1)
110      spsys->speechROMaddr = (spsys->speechROMaddr & 0x3c000UL)
111         | ((((unsigned long) spsys->speechrom_data[spsys->speechROMaddr]) << 8) & 0x3fffUL);
141   if (adapter->m_sprom_address < adapter->m_sprom_length-1)
142      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL)
143         | (((((unsigned long) adapter->m_speechrom[adapter->m_sprom_address]) << 8)
144         | adapter->m_speechrom[adapter->m_sprom_address+1]) & 0x3fffUL);
145   else if (adapter->m_sprom_address == adapter->m_sprom_length-1)
146      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL)
147         | ((((unsigned long) adapter->m_speechrom[adapter->m_sprom_address]) << 8) & 0x3fffUL);
112148   else
113      spsys->speechROMaddr = (spsys->speechROMaddr & 0x3c000UL);
149      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL);
114150
115   spsys->ROM_bits_count = 8;
151   adapter->m_rombits_count = 8;
116152}
117153
118154/*****************************************************************************/
119
120155/*
121    Memory read
156    Callback interface instance
122157*/
123READ8Z_DEVICE_HANDLER( ti998spch_rz )
158static const tms5220_interface ti99_8_tms5200interface =
124159{
125   ti99_speech8_state *spsys = get_safe_token(device);
160   DEVCB_NULL,                  // no IRQ callback
161   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti998_spsyn_device, speech8_ready),
162   ti998_spchroms_read,            // speech ROM read handler
163   ti998_spchroms_load_address,      // speech ROM load address handler
164   ti998_spchroms_read_and_branch   // speech ROM read and branch handler
165};
126166
127   if ((offset & 0xfc01)==0x9000)
128   {
129      device_adjust_icount(device->machine().device("maincpu"),-(18+3));      /* this is just a minimum, it can be more */
130      *value = tms5220_status_r(spsys->vsp, offset) & 0xff;
131   }
132}
167/**************************************************************************/
133168
134/*
135    Memory write
136*/
137WRITE8_DEVICE_HANDLER( ti998spch_w )
169WRITE_LINE_MEMBER( ti998_spsyn_device::speech8_ready )
138170{
139   ti99_speech8_state *spsys = get_safe_token(device);
140
141   if ((offset & 0xfc01)==0x9400)
142   {
143      device_adjust_icount(device->machine().device("maincpu"),-(54+3));      /* this is just an approx. minimum, it can be much more */
144
145      /* RN: the stupid design of the tms5220 core means that ready is cleared */
146      /* when there are 15 bytes in FIFO.  It should be 16.  Of course, if */
147      /* it were the case, we would need to store the value on the bus, */
148      /* which would be more complex. */
149      if (!tms5220_readyq_r(spsys->vsp))
150      {
151         attotime time_to_ready = attotime::from_double(tms5220_time_to_ready(spsys->vsp));
152         int cycles_to_ready = device->machine().device<cpu_device>("maincpu")->attotime_to_cycles(time_to_ready);
153         logerror("time to ready: %f -> %d\n", time_to_ready.as_double(), (int) cycles_to_ready);
154
155         device_adjust_icount(device->machine().device("maincpu"),-cycles_to_ready);
156         device->machine().scheduler().timer_set(attotime::zero, FUNC_NULL);
157      }
158      tms5220_data_w(spsys->vsp, offset, data);
159   }
171   //ti99_speech8_state *spsys = get_safe_token(device->owner());
172   if (VERBOSE>4) LOG("speech8: READY called by VSP; no forwarding implemented yet\n");
160173}
161174
162/**************************************************************************/
163
164static DEVICE_START( ti99_speech8 )
175void ti998_spsyn_device::device_start()
165176{
166   ti99_speech8_state *spsys = get_safe_token(device);
167   /* Resolve the callbacks to the PEB */
168   spsys->vsp = device->subdevice("speechsyn");
169177}
170178
171static DEVICE_STOP( ti99_speech8 )
179void ti998_spsyn_device::device_config_complete()
172180{
181   m_vsp = subdevice(SPEECHSYN_TAG);
173182}
174183
175static DEVICE_RESET( ti99_speech8 )
184void ti998_spsyn_device::device_reset()
176185{
177   ti99_speech8_state *spsys = get_safe_token(device);
186   m_speechrom = subregion(SPEECHROM_TAG)->base();
187   m_sprom_length = subregion(SPEECHROM_TAG)->bytes();
188   m_sprom_address = 0;
189   m_load_pointer = 0;
190   m_rombits_count = 0;
178191
179   astring region(device->tag(), ":", speech8_region);
180
181   spsys->speechrom_data = device->machine().region(region.cstr())->base();
182   spsys->speechROMlen = device->machine().region(region.cstr())->bytes();
183   spsys->speechROMaddr = 0;
184   spsys->load_pointer = 0;
185   spsys->ROM_bits_count = 0;
192   m_select_mask = 0xfc01;
193   m_select_value = 0x9000;
194   if (VERBOSE>4) LOG("speech8: reset\n");
186195}
187196
188static WRITE_LINE_DEVICE_HANDLER( speech8_ready )
189{
190   //ti99_speech8_state *spsys = get_safe_token(device->owner());
191   logerror("speech8: READY called by VSP\n");
192}
193
194/*
195    TMS5220 speech synthesizer
196    Note that in the real hardware, the predecessor TMC0285 was used.
197*/
198static const tms5220_interface ti99_4x_tms5200interface =
199{
200   DEVCB_NULL,               /* no IRQ callback */
201   DEVCB_LINE(speech8_ready),
202   ti99_spchroms_read,            /* speech ROM read handler */
203   ti99_spchroms_load_address,      /* speech ROM load address handler */
204   ti99_spchroms_read_and_branch   /* speech ROM read and branch handler */
205};
206
207MACHINE_CONFIG_FRAGMENT( ti99_speech8 )
197// Unlike the TI-99/4A, the 99/8 uses the TMS5220
198MACHINE_CONFIG_FRAGMENT( ti998_speech )
208199   MCFG_SPEAKER_STANDARD_MONO("mono")
209
210   MCFG_SOUND_ADD("speechsyn", TMS5220, 680000L)
211   MCFG_SOUND_CONFIG(ti99_4x_tms5200interface)
200   MCFG_SOUND_ADD(SPEECHSYN_TAG, TMS5220, 680000L)
201   MCFG_SOUND_CONFIG(ti99_8_tms5200interface)
212202   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50)
213203MACHINE_CONFIG_END
214204
215ROM_START( ti99_speech8 )
216   ROM_REGION(0x8000, speech8_region, 0)
205ROM_START( ti998_speech )
206   ROM_REGION(0x8000, SPEECHROM_TAG, 0)
217207   ROM_LOAD_OPTIONAL("spchrom.bin", 0x0000, 0x8000, BAD_DUMP CRC(58b155f7) SHA1(382292295c00dff348d7e17c5ce4da12a1d87763)) /* system speech ROM */
218208ROM_END
219209
220static const char DEVTEMPLATE_SOURCE[] = __FILE__;
210machine_config_constructor ti998_spsyn_device::device_mconfig_additions() const
211{
212   return MACHINE_CONFIG_NAME( ti998_speech );
213}
221214
222#define DEVTEMPLATE_ID(p,s)             p##ti99_speech8##s
223#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_MACHINE_CONFIG
224#define DEVTEMPLATE_NAME                "TI-99/8 Speech Synthesizer"
225#define DEVTEMPLATE_SHORTNAME           "ti998sp"
226#define DEVTEMPLATE_FAMILY              "Internal subsystem"
227#include "devtempl.h"
215const rom_entry *ti998_spsyn_device::device_rom_region() const
216{
217   return ROM_NAME( ti998_speech );
218}
219const device_type TI99_SPEECH8 = &device_creator<ti998_spsyn_device>;
228220
229DEFINE_LEGACY_DEVICE( TISPEECH8, ti99_speech8 );
230
src/mess/machine/ti99/ti32kmem.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 32K memory expansion
3    Michael Zapf, September 2010
4*/
4    See ti32kmem.c for documentation
55
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
612#ifndef __TI32K__
713#define __TI32K__
814
9DECLARE_LEGACY_DEVICE( TI32KMEM, ti99_mem32k );
15#include "ti99defs.h"
16
17extern const device_type TI_32KMEM;
18
19class ti_32k_expcard_device : public ti_expansion_card_device
20{
21public:
22   ti_32k_expcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
23   DECLARE_READ8Z_MEMBER(readz);
24   DECLARE_WRITE8_MEMBER(write);
25
26   void crureadz(offs_t offset, UINT8 *value);
27   void cruwrite(offs_t offset, UINT8 value);
28
29protected:
30   void device_start();
31   const rom_entry *device_rom_region() const;
32private:
33   UINT8*   m_ram_ptr;
34};
35
1036#endif
src/mess/machine/ti99/tn_ide.c
r14513r14514
1/*
1/****************************************************************************
2
23    Thierry Nouspikel's IDE card emulation
34
45    This card is just a prototype.  It has been designed by Thierry Nouspikel,
r14513r14514
1617
1718    Raphael Nabet, 2002-2004.
1819
19    Rewritten as device
20    Michael Zapf, September 2010
20    Michael Zapf
21    September 2010: Rewritten as device
22    February 2012: Rewritten as class
2123
22    FIXME: idecont assumes that there is only one drive that is called
23    "harddisk" at the root level
24*/
24    FIXME: Completely untested and likely to be broken
25
26*****************************************************************************/
27
2528#include "emu.h"
2629#include "peribox.h"
2730#include "machine/idectrl.h"
28#include "machine/rtc65271.h"
29#include "machine/idectrl.h"
30#include "cpu/tms9900/tms9900.h"
3131#include "tn_ide.h"
3232
3333#define CRU_BASE 0x1000
3434
35typedef ti99_pebcard_config tn_ide_config;
35#define BUFFER_TAG "ram"
3636
37enum
38{   /* 0xff for 2 mbytes, 0x3f for 512kbytes, 0x03 for 32 kbytes */
39   page_mask = /*0xff*/0x3f
40};
37/* previously 0xff */
38#define PAGE_MASK 0x3f
4139
4240enum
4341{
r14513r14514
4947   cru_reg_reset = 0x80
5048};
5149
52typedef struct _tn_ide_state
50nouspikel_ide_interface_device::nouspikel_ide_interface_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
51: ti_expansion_card_device(mconfig, TI99_IDE, "Nouspikel IDE interface card", tag, owner, clock)
5352{
54   int    selected;
55
56   /* Used for GenMod */
57   int      select_mask;
58   int      select_value;
59
60   rtc65271_device      *rtc;
61   device_t      *ide;
62
63   int      ide_irq;
64   int      clk_irq;
65   int    sram_enable;
66   int    sram_enable_dip;
67   int    cru_register;
68   int      cur_page;
69
70   int      tms9995_mode;
71
72   UINT16   input_latch;
73   UINT16   output_latch;
74
75   UINT8   *ram;
76
77   /* Callback lines to the main system. */
78   ti99_peb_connect   lines;
79
80} tn_ide_state;
81
82INLINE tn_ide_state *get_safe_token(device_t *device)
83{
84   assert(device != NULL);
85   assert(device->type() == TNIDE);
86
87   return (tn_ide_state *)downcast<legacy_device_base *>(device)->token();
53   m_shortname = "ti99_ide";
8854}
8955
90
9156/*
9257    CRU read
9358*/
94static READ8Z_DEVICE_HANDLER( cru_rz )
59void nouspikel_ide_interface_device::crureadz(offs_t offset, UINT8 *value)
9560{
96   tn_ide_state *card = get_safe_token(device);
9761   UINT8 reply = 0;
98   if ((offset & 0xff00)==CRU_BASE)
62   if ((offset & 0xff00)==m_cru_base)
9963   {
10064      int bit = (offset >> 4) & 7;
10165
10266      if (bit==0)
10367      {
104         reply = card->cru_register & 0x30;
68         reply = m_cru_register & 0x30;
10569         reply |= 8;   /* IDE bus IORDY always set */
106         if (!card->clk_irq)
70         if (!m_clk_irq)
10771            reply |= 4;
108         if (card->sram_enable_dip)
72         if (m_sram_enable_dip)
10973            reply |= 2;
110         if (!card->ide_irq)
74         if (!m_ide_irq)
11175            reply |= 1;
11276      }
11377      *value = reply;
r14513r14514
11781/*
11882    CRU write
11983*/
120static WRITE8_DEVICE_HANDLER( cru_w )
84void nouspikel_ide_interface_device::cruwrite(offs_t offset, UINT8 data)
12185{
122   tn_ide_state *card = get_safe_token(device);
123
124   if ((offset & 0xff00)==CRU_BASE)
86   if ((offset & 0xff00)==m_cru_base)
12587   {
12688      int bit = (offset >>1) & 7;
12789      switch (bit)
12890      {
129      case 0:         /* turn card on: handled by core */
130         card->selected = data;
91      case 0:
92         m_selected = (data!=0);
13193
132      case 1:         /* enable SRAM or registers in 0x4000-0x40ff */
133         card->sram_enable = data;
94      case 1:         // enable SRAM or registers in 0x4000-0x40ff
95         m_sram_enable = (data!=0);
13496         break;
13597
136      case 2:         /* enable SRAM page switching */
137      case 3:         /* force SRAM page 0 */
138      case 4:         /* enable SRAM in 0x6000-0x7000 ("RAMBO" mode) */
139      case 5:         /* write-protect RAM */
140      case 6:         /* irq and reset enable */
141      case 7:         /* reset drive */
142         if (data)
143            card->cru_register |= 1 << bit;
98      case 2:         // enable SRAM page switching
99      case 3:         // force SRAM page 0
100      case 4:         // enable SRAM in 0x6000-0x7000 ("RAMBO" mode)
101      case 5:         // write-protect RAM
102      case 6:         // irq and reset enable
103      case 7:         // reset drive
104         if (data!=0)
105            m_cru_register |= 1 << bit;
144106         else
145            card->cru_register &= ~(1 << bit);
107            m_cru_register &= ~(1 << bit);
146108
147109         if (bit == 6)
148            card->lines.inta((card->cru_register & cru_reg_int_en) && card->ide_irq);
110            m_slot->set_inta((m_cru_register & cru_reg_int_en) && m_ide_irq);
149111
150112         if ((bit == 6) || (bit == 7))
151            if ((card->cru_register & cru_reg_int_en) && !(card->cru_register & cru_reg_reset))
152               card->ide->reset();
113            if ((m_cru_register & cru_reg_int_en) && !(m_cru_register & cru_reg_reset))
114               m_ide->reset();
153115         break;
154116      }
155117   }
r14513r14514
158120/*
159121    Memory read
160122*/
161static READ8Z_DEVICE_HANDLER( data_rz )
123READ8Z_MEMBER(nouspikel_ide_interface_device::readz)
162124{
163   tn_ide_state *card = get_safe_token(device);
164125   UINT8 reply = 0;
165126
166   if (((offset & card->select_mask)==card->select_value) && card->selected)
127   if (((offset & m_select_mask)==m_select_value) && m_selected)
167128   {
168129      int addr = offset & 0x1fff;
169130
170      if ((addr <= 0xff) && (card->sram_enable == card->sram_enable_dip))
131      if ((addr <= 0xff) && (m_sram_enable == m_sram_enable_dip))
171132      {   /* registers */
172133         switch ((addr >> 5) & 0x3)
173134         {
174135         case 0:      /* RTC RAM */
175136            if (addr & 0x80)
176137               /* RTC RAM page register */
177               reply = card->rtc->xram_r(*memory_nonspecific_space(device->machine()),(addr & 0x1f) | 0x20);
138               reply = m_rtc->xram_r(*memory_nonspecific_space(machine()),(addr & 0x1f) | 0x20);
178139            else
179140               /* RTC RAM read */
180               reply = card->rtc->xram_r(*memory_nonspecific_space(device->machine()),addr);
141               reply = m_rtc->xram_r(*memory_nonspecific_space(machine()),addr);
181142            break;
182143         case 1:      /* RTC registers */
183144            if (addr & 0x10)
184145               /* register data */
185               reply = card->rtc->rtc_r(*memory_nonspecific_space(device->machine()),1);
146               reply = m_rtc->rtc_r(*memory_nonspecific_space(machine()),1);
186147            else
187148               /* register select */
188               reply = card->rtc->rtc_r(*memory_nonspecific_space(device->machine()),0);
149               reply = m_rtc->rtc_r(*memory_nonspecific_space(machine()),0);
189150            break;
190151         case 2:      /* IDE registers set 1 (CS1Fx) */
191            if (card->tms9995_mode ? (!(addr & 1)) : (addr & 1))
152            if (m_tms9995_mode ? (!(addr & 1)) : (addr & 1))
192153            {   /* first read triggers 16-bit read cycle */
193               card->input_latch = (! (addr & 0x10)) ? ide_bus_r(card->ide, 0, (addr >> 1) & 0x7) : 0;
154               m_input_latch = (! (addr & 0x10)) ? ide_bus_r(m_ide, 0, (addr >> 1) & 0x7) : 0;
194155            }
195156
196157            /* return latched input */
197158            /*reply = (addr & 1) ? input_latch : (input_latch >> 8);*/
198159            /* return latched input - bytes are swapped in 2004 IDE card */
199            reply = ((addr & 1) ? (card->input_latch >> 8) : card->input_latch) & 0xff;
160            reply = ((addr & 1) ? (m_input_latch >> 8) : m_input_latch) & 0xff;
200161            break;
201162         case 3:      /* IDE registers set 2 (CS3Fx) */
202            if (card->tms9995_mode ? (!(addr & 1)) : (addr & 1))
163            if (m_tms9995_mode ? (!(addr & 1)) : (addr & 1))
203164            {   /* first read triggers 16-bit read cycle */
204               card->input_latch = (! (addr & 0x10)) ? ide_bus_r(card->ide, 1, (addr >> 1) & 0x7) : 0;
165               m_input_latch = (! (addr & 0x10)) ? ide_bus_r(m_ide, 1, (addr >> 1) & 0x7) : 0;
205166            }
206167
207168            /* return latched input */
208169            /*reply = (addr & 1) ? input_latch : (input_latch >> 8);*/
209170            /* return latched input - bytes are swapped in 2004 IDE card */
210            reply = ((addr & 1) ? (card->input_latch >> 8) : card->input_latch) & 0xff;
171            reply = ((addr & 1) ? (m_input_latch >> 8) : m_input_latch) & 0xff;
211172            break;
212173         }
213174      }
214175      else
215176      {   /* sram */
216         if ((card->cru_register & cru_reg_page_0) || (addr >= 0x1000))
217            reply = card->ram[addr+0x2000 * card->cur_page];
177         if ((m_cru_register & cru_reg_page_0) || (addr >= 0x1000))
178            reply = m_ram[addr+0x2000 * m_cur_page];
218179         else
219            reply = card->ram[addr];
180            reply = m_ram[addr];
220181      }
221182      *value = reply;
222183   }
r14513r14514
225186/*
226187    Memory write. The controller is 16 bit, so we need to demultiplex again.
227188*/
228static WRITE8_DEVICE_HANDLER( data_w )
189WRITE8_MEMBER(nouspikel_ide_interface_device::write)
229190{
230   tn_ide_state *card = get_safe_token(device);
231
232   if (((offset & card->select_mask)==card->select_value) && card->selected)
191   if (((offset & m_select_mask)==m_select_value) && m_selected)
233192   {
234      if (card->cru_register & cru_reg_page_switching)
193      if (m_cru_register & cru_reg_page_switching)
235194      {
236         card->cur_page = (offset >> 1) & page_mask;
195         m_cur_page = (offset >> 1) & PAGE_MASK;
237196      }
238197
239198      int addr = offset & 0x1fff;
240199
241      if ((addr <= 0xff) && (card->sram_enable == card->sram_enable_dip))
200      if ((addr <= 0xff) && (m_sram_enable == m_sram_enable_dip))
242201      {   /* registers */
243202         switch ((addr >> 5) & 0x3)
244203         {
245204         case 0:      /* RTC RAM */
246205            if (addr & 0x80)
247206               /* RTC RAM page register */
248               card->rtc->xram_w(*memory_nonspecific_space(device->machine()),(addr & 0x1f) | 0x20, data);
207               m_rtc->xram_w(*memory_nonspecific_space(machine()),(addr & 0x1f) | 0x20, data);
249208            else
250209               /* RTC RAM write */
251               card->rtc->xram_w(*memory_nonspecific_space(device->machine()),addr, data);
210               m_rtc->xram_w(*memory_nonspecific_space(machine()),addr, data);
252211            break;
253212         case 1:      /* RTC registers */
254213            if (addr & 0x10)
255214               /* register data */
256               card->rtc->rtc_w(*memory_nonspecific_space(device->machine()),1, data);
215               m_rtc->rtc_w(*memory_nonspecific_space(machine()),1, data);
257216            else
258217               /* register select */
259               card->rtc->rtc_w(*memory_nonspecific_space(device->machine()),0, data);
218               m_rtc->rtc_w(*memory_nonspecific_space(machine()),0, data);
260219            break;
261220         case 2:      /* IDE registers set 1 (CS1Fx) */
262221/*
263222                if (addr & 1)
264                    card->output_latch = (card->output_latch & 0xff00) | data;
223                    m_output_latch = (m_output_latch & 0xff00) | data;
265224                else
266                    card->output_latch = (card->output_latch & 0x00ff) | (data << 8);
225                    m_output_latch = (m_output_latch & 0x00ff) | (data << 8);
267226*/
268227            /* latch write - bytes are swapped in 2004 IDE card */
269228            if (addr & 1)
270               card->output_latch = (card->output_latch & 0x00ff) | (data << 8);
229               m_output_latch = (m_output_latch & 0x00ff) | (data << 8);
271230            else
272               card->output_latch = (card->output_latch & 0xff00) | data;
231               m_output_latch = (m_output_latch & 0xff00) | data;
273232
274            if (card->tms9995_mode ? (addr & 1) : (!(addr & 1)))
233            if (m_tms9995_mode ? (addr & 1) : (!(addr & 1)))
275234            {   /* second write triggers 16-bit write cycle */
276               ide_bus_w(card->ide, 0, (addr >> 1) & 0x7, card->output_latch);
235               ide_bus_w(m_ide, 0, (addr >> 1) & 0x7, m_output_latch);
277236            }
278237            break;
279238         case 3:      /* IDE registers set 2 (CS3Fx) */
280239/*
281240                if (addr & 1)
282                    card->output_latch = (card->output_latch & 0xff00) | data;
241                    m_output_latch = (m_output_latch & 0xff00) | data;
283242                else
284                    card->output_latch = (card->output_latch & 0x00ff) | (data << 8);
243                    m_output_latch = (m_output_latch & 0x00ff) | (data << 8);
285244*/
286245            /* latch write - bytes are swapped in 2004 IDE card */
287246            if (addr & 1)
288               card->output_latch = (card->output_latch & 0x00ff) | (data << 8);
247               m_output_latch = (m_output_latch & 0x00ff) | (data << 8);
289248            else
290               card->output_latch = (card->output_latch & 0xff00) | data;
249               m_output_latch = (m_output_latch & 0xff00) | data;
291250
292            if (card->tms9995_mode ? (addr & 1) : (!(addr & 1)))
251            if (m_tms9995_mode ? (addr & 1) : (!(addr & 1)))
293252            {   /* second write triggers 16-bit write cycle */
294               ide_bus_w(card->ide, 1, (addr >> 1) & 0x7, card->output_latch);
253               ide_bus_w(m_ide, 1, (addr >> 1) & 0x7, m_output_latch);
295254            }
296255            break;
297256         }
298257      }
299258      else
300259      {   /* sram */
301         if (! (card->cru_register & cru_reg_wp))
260         if (! (m_cru_register & cru_reg_wp))
302261         {
303            if ((card->cru_register & cru_reg_page_0) || (addr >= 0x1000))
304               card->ram[addr+0x2000 * card->cur_page] = data;
262            if ((m_cru_register & cru_reg_page_0) || (addr >= 0x1000))
263               m_ram[addr+0x2000 * m_cur_page] = data;
305264            else
306               card->ram[addr] = data;
265               m_ram[addr] = data;
307266         }
308267      }
309268   }
310269}
311270
312/**************************************************************************/
313
314static const ti99_peb_card tn_ide_card =
271void nouspikel_ide_interface_device::do_inta(int state)
315272{
316   data_rz, data_w,            // memory access read/write
317   cru_rz, cru_w,               // CRU access
318   NULL, NULL,                  // SENILA/B access
319   NULL, NULL                  // 16 bit access (none here)
320};
273   m_slot->set_inta(state);
274}
321275
322276/*
323277    ti99_ide_interrupt()
r14513r14514
325279*/
326280static void ide_interrupt_callback(device_t *device, int state)
327281{
328   tn_ide_state *card = get_safe_token(device->owner());
329   card->ide_irq = state;
330   if (card->cru_register & cru_reg_int_en)
331      card->lines.inta(state);
282   nouspikel_ide_interface_device *card  = downcast<nouspikel_ide_interface_device *>(device->owner());
283   card->m_ide_irq = state;
284   if (card->m_cru_register & cru_reg_int_en)
285      card->do_inta(state);
332286}
333287
334288/*
335289    clk_interrupt_callback()
336290    clock interrupt callback
337291*/
338static WRITE_LINE_DEVICE_HANDLER(clock_interrupt_callback)
292WRITE_LINE_MEMBER(nouspikel_ide_interface_device::clock_interrupt_callback)
339293{
340   tn_ide_state *card = get_safe_token(device->owner());
341   card->clk_irq = state;
342   card->lines.inta(state);
294   m_clk_irq = (state!=0);
295   m_slot->set_inta(state);
343296}
344297
345
346static DEVICE_START( tn_ide )
298void nouspikel_ide_interface_device::device_start()
347299{
348   tn_ide_state *card = get_safe_token(device);
349   card->rtc = device->subdevice<rtc65271_device>("ide_rtc");
350   card->ide = device->subdevice("ide");
300   m_rtc = subdevice<rtc65271_device>("ide_rtc");
301   m_ide = subdevice("ide");
351302
352   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
353   card->lines.inta.resolve(topeb->inta, *device);
354
355   card->ram = auto_alloc_array(device->machine(), UINT8, 0x080000);
356   card->sram_enable_dip = 0;
303   m_ram = subregion(BUFFER_TAG)->base();
304   m_sram_enable_dip = false; // TODO: what is this?
357305}
358306
359static DEVICE_STOP( tn_ide )
307void nouspikel_ide_interface_device::device_reset()
360308{
361}
309   m_cur_page = 0;
310   m_sram_enable = false;
311   m_cru_register = 0;
362312
363static DEVICE_RESET( tn_ide )
364{
365   tn_ide_state *card = get_safe_token(device);
366   device_t *peb = device->owner();
367
368   if (input_port_read(device->machine(), "HDCTRL") & HD_IDE)
313   if (m_genmod)
369314   {
370      int success = mount_card(peb, device, &tn_ide_card, get_pebcard_config(device)->slot);
371      if (!success) return;
315      m_select_mask = 0x1fe000;
316      m_select_value = 0x174000;
317   }
318   else
319   {
320      m_select_mask = 0x7e000;
321      m_select_value = 0x74000;
322   }
323   m_selected = false;
372324
373      card->cur_page = 0;
374      card->sram_enable = 0;
375      card->cru_register = 0;
325   m_cru_base = input_port_read(*this, "CRUIDE");
376326
377      card->select_mask = 0x7e000;
378      card->select_value = 0x74000;
379
380      if (input_port_read(device->machine(), "MODE")==GENMOD)
381      {
382         // GenMod card modification
383         card->select_mask = 0x1fe000;
384         card->select_value = 0x174000;
385      }
386
387      card->tms9995_mode =  (device->type()==TMS9995);
388   }
327   // m_tms9995_mode =  (device->type()==TMS9995);
389328}
390329
391330static const rtc65271_interface ide_rtc_cfg =
392331{
393   DEVCB_LINE(clock_interrupt_callback)
332   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, nouspikel_ide_interface_device, clock_interrupt_callback)
394333};
395334
396335MACHINE_CONFIG_FRAGMENT( tn_ide )
397336   MCFG_RTC65271_ADD( "ide_rtc", ide_rtc_cfg )
398337   MCFG_IDE_CONTROLLER_ADD( "ide", ide_interrupt_callback, ide_image_devices, "hdd", NULL )
399//   MCFG_IDE_CONTROLLER_REGIONS(":peribox:idehd0:drive", NULL)
338//  MCFG_IDE_CONTROLLER_REGIONS(":peribox:idehd0:drive", NULL)
400339MACHINE_CONFIG_END
401340
402static const char DEVTEMPLATE_SOURCE[] = __FILE__;
341ROM_START( tn_ide )
342   ROM_REGION(0x80000, BUFFER_TAG, 0)  /* RAM buffer 512 KiB */
343   ROM_FILL(0x0000, 0x80000, 0x00)
344ROM_END
403345
404#define DEVTEMPLATE_ID(p,s)             p##tn_ide##s
405#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
406#define DEVTEMPLATE_NAME                "Nouspikel IDE controller card"
407#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
408#include "devtempl.h"
346INPUT_PORTS_START( tn_ide )
347   PORT_START( "CRUIDE" )
348   PORT_DIPNAME( 0x1f00, 0x1000, "IDE CRU base" )
349      PORT_DIPSETTING( 0x1000, "1000" )
350      PORT_DIPSETTING( 0x1100, "1100" )
351      PORT_DIPSETTING( 0x1200, "1200" )
352      PORT_DIPSETTING( 0x1300, "1300" )
353      PORT_DIPSETTING( 0x1400, "1400" )
354      PORT_DIPSETTING( 0x1500, "1500" )
355      PORT_DIPSETTING( 0x1600, "1600" )
356      PORT_DIPSETTING( 0x1700, "1700" )
357      PORT_DIPSETTING( 0x1800, "1800" )
358      PORT_DIPSETTING( 0x1900, "1900" )
359      PORT_DIPSETTING( 0x1a00, "1A00" )
360      PORT_DIPSETTING( 0x1b00, "1B00" )
361      PORT_DIPSETTING( 0x1c00, "1C00" )
362      PORT_DIPSETTING( 0x1d00, "1D00" )
363      PORT_DIPSETTING( 0x1e00, "1E00" )
364      PORT_DIPSETTING( 0x1f00, "1F00" )
365INPUT_PORTS_END
409366
410DEFINE_LEGACY_DEVICE( TNIDE, tn_ide );
367machine_config_constructor nouspikel_ide_interface_device::device_mconfig_additions() const
368{
369   return MACHINE_CONFIG_NAME( tn_ide );
370}
411371
372const rom_entry *nouspikel_ide_interface_device::device_rom_region() const
373{
374   return ROM_NAME( tn_ide );
375}
376
377ioport_constructor nouspikel_ide_interface_device::device_input_ports() const
378{
379   return INPUT_PORTS_NAME(tn_ide);
380}
381
382const device_type TI99_IDE = &device_creator<nouspikel_ide_interface_device>;
383
src/mess/machine/ti99/samsmem.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 SuperAMS Memory Expansion Card. Uses a 74LS612 memory mapper.
4    The card can be equipped with up to 1 MiB of static CMOS memory; it is
5    not buffered, however.
36
4    TODO: Test this device with some emulated program!
5*/
6#include "emu.h"
7#include "peribox.h"
8#include "samsmem.h"
7    SAMS organizes memory in 4 KiB blocks which are mapped into the address
8    space by a memory mapper. The mapper can be configured via a sequence of
9    addresses at 4000, 4002, ..., 401e, which correspond to memory locations
10    0000-0fff, 1000-1fff, ..., f000-ffff.
911
10typedef ti99_pebcard_config ti99_samsmem_config;
12    Michael Zapf
1113
12typedef struct _ti99_samsmem_state
13{
14   UINT8   mapper[16];
15   int    map_mode;
16   int      access_mapper;
17   UINT8   *memory;
14    February 2012: Rewritten as class
1815
19} ti99_samsmem_state;
16*****************************************************************************/
2017
21INLINE ti99_samsmem_state *get_safe_token(device_t *device)
22{
23   assert(device != NULL);
24   assert(device->type() == SAMSMEM);
18#include "samsmem.h"
19#define RAMREGION "ram"
2520
26   return (ti99_samsmem_state *)downcast<legacy_device_base *>(device)->token();
27}
28
2921#define SAMS_CRU_BASE 0x1e00
3022
31/*
32    CRU write (there is no read here)
33*/
34static WRITE8_DEVICE_HANDLER( samsmem_cru_w )
23#define VERBOSE 1
24#define LOG logerror
25
26sams_memory_expansion_device::sams_memory_expansion_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
27: ti_expansion_card_device(mconfig, TI99_SAMSMEM, "SuperAMS memory expansion card", tag, owner, clock)
3528{
36   ti99_samsmem_state *card = get_safe_token(device);
37   if ((offset & 0xff00)==SAMS_CRU_BASE)
38   {
39//      printf("cru address %04x = %02x\n", offset&0xffff, data);
40      if ((offset & 0x000e)==0) card->access_mapper = data;
41      if ((offset & 0x000e)==2) card->map_mode = data;
42   }
29   m_shortname = "ti99_sams";
4330}
4431
4532/*
r14513r14514
4734    0x2000-0x3fff and 0xa000-0xffff, and the mapper area is at 0x4000-0x401e
4835    (only even addresses).
4936*/
50static READ8Z_DEVICE_HANDLER( samsmem_rz )
37READ8Z_MEMBER(sams_memory_expansion_device::readz)
5138{
52   ti99_samsmem_state *card = get_safe_token(device);
53   UINT32 address = 0;
39   int base = 0;
5440
55   if (card->access_mapper && ((offset & 0xe000)==0x4000))
41   if (m_access_mapper && ((offset & 0xe000)==0x4000))
5642   {
57      // select the mapper circuit
58      *value = card->mapper[(offset>>1)&0x000f];
43      *value = m_mapper[(offset>>1)&0x000f];
5944   }
6045
61   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xc000)==0xc000))
46   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xe000)==0xc000) || ((offset & 0xe000)==0xe000))
6247   {
63      // select memory expansion
64      if (card->map_mode)
65         address = (card->mapper[(offset & 0xf000)>>12] << 12) + (offset & 0x0fff);
66      else // transparent mode
67         address = offset & 0xffff;
68      *value = card->memory[address];
48      if (!m_map_mode)
49      {
50         // transparent mode
51         *value = m_ram[offset & 0xffff];
52      }
53      else
54      {
55         base = (m_mapper[(offset & 0xf000)>>12] << 12);
56         *value = m_ram[base | (offset & 0x0fff)];
57      }
6958   }
7059}
7160
72/*
73    Memory write
74*/
75static WRITE8_DEVICE_HANDLER( samsmem_w )
61WRITE8_MEMBER(sams_memory_expansion_device::write)
7662{
77   ti99_samsmem_state *card = get_safe_token(device);
78   UINT32 address = 0;
63   int base = 0;
7964
80   if (card->access_mapper && ((offset & 0xe000)==0x4000))
65   if (m_access_mapper && ((offset & 0xe000)==0x4000))
8166   {
82      // select the mapper circuit
83      card->mapper[(offset>>1)&0x000f] = data;
84//      printf("mapper %d = %02x\n", (offset >> 1)&0xf, data);
67      m_mapper[(offset>>1)&0x000f] = data;
8568   }
86   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xc000)==0xc000))
69
70   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xe000)==0xc000) || ((offset & 0xe000)==0xe000))
8771   {
88      // select memory expansion
89      if (card->map_mode)
72      if (!m_map_mode)
9073      {
91         address = (card->mapper[(offset & 0xf000)>>12] << 12) + (offset & 0x0fff);
92//          printf("map mode, address = %04x\n", address);
74         // transparent mode
75         m_ram[offset & 0xffff] = data;
9376      }
94      else // transparent mode
77      else
9578      {
96         address = offset & 0xffff;
97//          printf("direct mode, address = %04x\n", address);
79         base = (m_mapper[(offset & 0xf000)>>12] << 12);
80         m_ram[base | (offset & 0x0fff)] = data;
9881      }
99      card->memory[address] = data;
10082   }
10183}
10284
103/**************************************************************************/
104
105static const ti99_peb_card samsmem_card =
85/*
86    CRU read. None here.
87*/
88void sams_memory_expansion_device::crureadz(offs_t offset, UINT8 *value)
10689{
107   samsmem_rz, samsmem_w,         // memory access read/write
108   NULL, samsmem_cru_w,         // CRU access (no read here)
109   NULL, NULL,                  // SENILA/B access (none here)
110   NULL, NULL                  // 16 bit access (none here)
111};
112
113static DEVICE_START( ti99_samsmem )
114{
115   ti99_samsmem_state *card = get_safe_token(device);
116   card->memory=NULL;
90   return;
11791}
11892
119static DEVICE_STOP( ti99_samsmem )
93/*
94    CRU write. Turns on the mapper and allows to change it.
95*/
96void sams_memory_expansion_device::cruwrite(offs_t offset, UINT8 data)
12097{
121   ti99_samsmem_state *card = get_safe_token(device);
122   if (card->memory) free(card->memory);
123}
124
125static DEVICE_RESET( ti99_samsmem )
126{
127   ti99_samsmem_state *card = get_safe_token(device);
128   /* Register the card */
129   device_t *peb = device->owner();
130
131   if (input_port_read(device->machine(), "RAM")==RAM_SUPERAMS1024)
98   if ((offset & 0xff00)==SAMS_CRU_BASE)
13299   {
133      int success = mount_card(peb, device, &samsmem_card, get_pebcard_config(device)->slot);
134      if (!success) return;
100      if (VERBOSE>7) LOG("cru address %04x = %02x\n", offset&0xffff, data);
135101
136      if (card->memory==NULL)
137      {
138         // Allocate 1 MiB (check whether we want to allocate this on demand)
139         card->memory = (UINT8*)malloc(0x100000);
140      }
141      card->map_mode = FALSE;
142      card->access_mapper = FALSE;
143      for (int i=0; i < 16; i++) card->mapper[i]=0;
102      if ((offset & 0x000e)==0) m_access_mapper = (data!=0);
103      if ((offset & 0x000e)==2) m_map_mode = (data!=0);
144104   }
145105}
146106
147static const char DEVTEMPLATE_SOURCE[] = __FILE__;
148107
149#define DEVTEMPLATE_ID(p,s)             p##ti99_samsmem##s
150#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
151#define DEVTEMPLATE_NAME                "TI99 SuperAMS Memory Expansion Card"
152#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
153#include "devtempl.h"
108ROM_START( sams_card )
109   ROM_REGION(0x100000, RAMREGION, 0)
110   ROM_FILL(0x0000, 0x100000, 0x00)
111ROM_END
154112
155DEFINE_LEGACY_DEVICE( SAMSMEM, ti99_samsmem );
113void sams_memory_expansion_device::device_start()
114{
115   if (VERBOSE>5) LOG("SuperAMS: start\n");
116   m_ram = subregion(RAMREGION)->base();
117}
156118
119void sams_memory_expansion_device::device_reset()
120{
121   if (VERBOSE>5) LOG("SuperAMS: reset\n");
122   // Resetting values
123   m_map_mode = false;
124   m_access_mapper = false;
125   for (int i=0; i < 16; i++) m_mapper[i] = 0;
126}
127
128const rom_entry *sams_memory_expansion_device::device_rom_region() const
129{
130   return ROM_NAME( sams_card );
131}
132
133const device_type TI99_SAMSMEM = &device_creator<sams_memory_expansion_device>;
src/mess/machine/ti99/evpc.h
r14513r14514
1/*
1/****************************************************************************
2
23    SNUG Enhanced Video Processor Card (evpc)
4    See evpc.c for documentation.
35
46    Michael Zapf
5    Rewritten as device
6    October 2010
7*/
8/* device interface */
7
8    October 2010: Rewritten as device
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
913#ifndef __EVPC__
1014#define __EVPC__
1115
12DECLARE_LEGACY_DEVICE( EVPC, ti99_evpc );
16#include "emu.h"
17#include "ti99defs.h"
18#include "peribox.h"
19#include "dinvram.h"
20
21extern const device_type TI99_EVPC;
22
23typedef struct _evpc_palette
24{
25   UINT8      read_index, write_index, mask;
26   int       read;
27   int       state;
28   struct { UINT8 red, green, blue; } color[0x100];
29   //int dirty;
30} evpc_palette;
31
32class snug_enhanced_video_device : public ti_expansion_card_device, public device_nvram_interface
33{
34public:
35   snug_enhanced_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
36   DECLARE_READ8Z_MEMBER(readz);
37   DECLARE_WRITE8_MEMBER(write);
38
39   void   crureadz(offs_t offset, UINT8 *value);
40   void   cruwrite(offs_t offset, UINT8 data);
41
42protected:
43   void device_start(void);
44   void device_reset(void);
45   void device_stop(void);
46
47   const rom_entry *device_rom_region() const;
48   ioport_constructor device_input_ports() const;
49
50   void nvram_default();
51   void nvram_read(emu_file &file);
52   void nvram_write(emu_file &file);
53
54private:
55   UINT8*         m_dsrrom;
56   bool         m_RAMEN;
57   int          m_dsr_page;
58   UINT8*         m_novram;   /* NOVRAM area */
59   evpc_palette   m_palette;
60};
61
1362#endif
src/mess/machine/ti99/datamux.c
r14513r14514
6060
6161    We integrate the 16 bit memory expansion in this datamux component
6262    (pretending that the memory expansion was soldered on top of the datamux)
63
64    January 2012: Rewritten as class
65
6366***************************************************************************/
6467
6568#include "emu.h"
66#include "peribox.h"
6769#include "datamux.h"
6870
69#define MAXDEV 10
70
71typedef struct _attached_device
71/*
72    Constructor
73*/
74ti99_datamux_device::ti99_datamux_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
75: device_t(mconfig, DATAMUX, "Databus multiplexer", tag, owner, clock)
7276{
73   /* The device. */
74   device_t   *device;
75
76   /* Address bits involved */
77   UINT16         address_mask;
78
79   /* Value of the address bits which are involved for selecting for read. */
80   UINT16         select;
81
82   /* Value of the address bits which are involved for selecting for write. */
83   UINT16         write_select;
84
85   /* Read access. */
86   databus_read_function read_byte;
87
88   /* Write access. */
89   databus_write_function write_byte;
90
91   /* Setting which determined if this device is mounted or not. */
92   const char   *setting;
93
94   /* Bits which must be set in the setting. */
95   UINT8      set;
96
97   /* Bits which must not be set in the setting. */
98   UINT8      unset;
99} attached_device;
100
101typedef struct _datamux_state
102{
103   /* All devices that are attached to the 8-bit bus. */
104   attached_device component[MAXDEV];
105
106   /* Latch which stores the first byte */
107   UINT8 latch;
108
109   // Intermediate storage which holds the low byte of the previous read cycle.
110   // In reality, the TMS9900 adds the low byte to the 16 bit memory word
111   // from the previous read-before-write cycle, but this is not yet handled
112   // by the TMS9900 emulation. TODO: rewrite TMS9900
113   UINT8 lowbyte;
114   UINT8 highbyte;
115
116   /* Memory expansion (internal, 16 bit). */
117   UINT16 *ram16b;
118
119   /* Use the memory expansion? */
120   int use32k;
121
122   /* Management function */
123   int devindex;
124} datamux_state;
125
126
127INLINE datamux_state *get_safe_token(device_t *device)
128{
129   assert(device != NULL);
130   assert(device->type() == DMUX);
131
132   return (datamux_state *)downcast<legacy_device_base *>(device)->token();
13377}
13478
135static void dmux_mount_device(device_t *dmuxdev, device_t *busdevice, UINT16 address_mask, UINT16 address_bits, UINT16 write_sel, databus_read_function read, databus_write_function write)
136{
137   datamux_state *dmux = get_safe_token(dmuxdev);
138   int index = dmux->devindex++;
139   if (index < MAXDEV)
140   {
141      if (dmux->component[index].device == NULL)
142      {
143         dmux->component[index].device = busdevice;
144         dmux->component[index].address_mask = address_mask;
145         dmux->component[index].select = address_bits;
146         dmux->component[index].write_select = write_sel;
147         dmux->component[index].read_byte = read;
148         dmux->component[index].write_byte = write;
149         // printf("registering the device at position %d\n", index);
150      }
151   }
152   else
153   {
154      logerror("ti99_dmux: Tried to mount too many devices to the DMUX. (Just a bug.)\n");
155   }
156}
79#define LOG logerror
15780
15881/***************************************************************************
15982    DEVICE ACCESSOR FUNCTIONS
r14513r14514
16588    a delay, and then the second access (each one with possibly many attached
16689    devices)
16790*/
168READ16_DEVICE_HANDLER( ti99_dmux_r )
91READ16_MEMBER( ti99_datamux_device::read )
16992{
17093   UINT8 hbyte = 0;
171   datamux_state *dmux = get_safe_token(device);
17294   UINT16 addr = (offset << 1);
17395
17496   // Looks ugly, but this is close to the real thing. If the 16bit
r14513r14514
17698   // space, just respond to the memory access and don't bother the
17799   // datamux in any way. In particular, do not make the datamux insert wait
178100   // states.
179   if (dmux->use32k)
101   if (m_use32k)
180102   {
181103      UINT16 base = 0;
182104      if ((addr & 0xe000)==0x2000) base = 0x1000;
r14513r14514
184106
185107      if (base != 0)
186108      {
187         UINT16 reply = dmux->ram16b[offset-base];
109         UINT16 reply = m_ram16b[offset-base];
188110         // Store the prefetch
189         dmux->lowbyte = reply & 0x00ff;
190         dmux->highbyte = (reply & 0xff00)>>8;
111         m_lowbyte = reply & 0x00ff;
112         m_highbyte = (reply & 0xff00)>>8;
191113         return reply;
192114      }
193115   }
194116
195   for (int i=0; i < MAXDEV; i++)
117   attached_device *dev = m_devices.first();
118
119   // Reading the odd address first (addr+1)
120   while (dev != NULL)
196121   {
197      attached_device *adev = &dmux->component[i];
198      if (dmux->component[i].device != NULL)
122      if (((addr+1) & dev->m_config->address_mask)==dev->m_config->select)
199123      {
200         if (adev->read_byte != NULL)
201         {
202            if (((addr+1) & adev->address_mask)==adev->select)
203            {
204               // put on the latch
205               adev->read_byte(adev->device, addr+1, &dmux->latch);
206               dmux->lowbyte = dmux->latch;
207            }
208         }
209         // hope we don't have two devices answering...
210         // consider something like a logical OR and maybe some artificial smoke
124         // Cast to the bus8z_device (see ti99defs.h)
125         bus8z_device *devz = static_cast<bus8z_device *>(dev->m_device);
126         devz->readz(*m_space, addr+1, &m_latch);
127         m_lowbyte = m_latch;
211128      }
129      // hope we don't have two devices answering...
130      // consider something like a logical OR and maybe some artificial smoke
131      dev = dev->m_next;
212132   }
213133
214134   // Takes three cycles
215   device_adjust_icount(device->machine().device("maincpu"),-3);
135   device_adjust_icount(m_cpu,-3);
136   dev = m_devices.first();
216137
217   for (int i=0; i < MAXDEV; i++)
138   // Reading the even address now (addr)
139   while (dev != NULL)
218140   {
219      attached_device *adev = &dmux->component[i];
220      if (dmux->component[i].device != NULL)
141      if (dev->m_config->write_select != 0xffff) // write-only
221142      {
222         if (adev->read_byte != NULL)
143         if ((addr & dev->m_config->address_mask)==dev->m_config->select)
223144         {
224            if ((addr & adev->address_mask)==adev->select)
225            {
226               adev->read_byte(adev->device, addr, &hbyte);
227               dmux->highbyte = hbyte;
228            }
145            bus8z_device *devz = static_cast<bus8z_device *>(dev->m_device);
146            devz->readz(*m_space, addr, &hbyte);
147            m_highbyte = hbyte;
229148         }
230149      }
150      dev = dev->m_next;
231151   }
232152
233153   // Takes three cycles
234   device_adjust_icount(device->machine().device("maincpu"),-3);
154   device_adjust_icount(m_cpu,-3);
235155
236156   // use the latch and the currently read byte and put it on the 16bit bus
237   return (hbyte<<8) | dmux->latch ;
157   return (hbyte<<8) | m_latch ;
238158}
239159
240160/*
241161    Write access.
242162*/
243WRITE16_DEVICE_HANDLER( ti99_dmux_w )
163WRITE16_MEMBER( ti99_datamux_device::write )
244164{
245   datamux_state *dmux = get_safe_token(device);
246165   UINT16 addr = (offset << 1);
247166
248167   // The handling of byte writing is done in MESS using mem_mask.
r14513r14514
259178   // Until the TMS9900 emulation is corrected in this way, we are using
260179   // the DMUX in place of the internal buffer.
261180
262//  printf("write addr=%04x memmask=%04x value=%04x\n", addr, mem_mask, data);
181//  LOG("write addr=%04x memmask=%04x value=%04x\n", addr, mem_mask, data);
263182   if (mem_mask == 0xff00)
264      data = data | dmux->lowbyte;
183      data = data | m_lowbyte;
265184
266185   if (mem_mask == 0x00ff)
267      data = data | (dmux->highbyte << 8);
186      data = data | (m_highbyte << 8);
268187
269188   // Handle the internal 32K expansion
270   if (dmux->use32k)
189   if (m_use32k)
271190   {
272191      if (addr>=0x2000 && addr<0x4000)
273192      {
274         dmux->ram16b[offset-0x1000] = data; // index 0000 - 0fff
193         m_ram16b[offset-0x1000] = data; // index 0000 - 0fff
275194         return;
276195      }
277196      if (addr>=0xa000)
278197      {
279         dmux->ram16b[offset-0x4000] = data; // index 1000 - 4fff
198         m_ram16b[offset-0x4000] = data; // index 1000 - 4fff
280199         return;
281200      }
282201   }
283202
284   for (int i=0; i < MAXDEV; i++)
203   attached_device *dev = m_devices.first();
204   while (dev != NULL)
285205   {
286      attached_device *adev = &dmux->component[i];
287      if (dmux->component[i].device != NULL)
206      if (((addr+1) & dev->m_config->address_mask)==(dev->m_config->select | dev->m_config->write_select))
288207      {
289         if (adev->write_byte != NULL)
290         {
291            if (((addr+1) & adev->address_mask)==(adev->select|adev->write_select))
292            {
293               // write the byte from the lower 8 lines
294               adev->write_byte(adev->device, addr+1, data & 0xff);
295            }
296         }
208         bus8z_device *devz = static_cast<bus8z_device *>(dev->m_device);
209         devz->write(*m_space, addr+1, data & 0xff);
297210      }
211      dev = dev->m_next;
298212   }
299213
300214   // Takes three cycles
301   device_adjust_icount(device->machine().device("maincpu"),-3);
215   device_adjust_icount(m_cpu,-3);
216   dev = m_devices.first();
302217
303   for (int i=0; i < MAXDEV; i++)
218   while (dev != NULL)
304219   {
305      attached_device *adev = &dmux->component[i];
306      if (dmux->component[i].device != NULL)
220      if ((addr & dev->m_config->address_mask)==(dev->m_config->select | dev->m_config->write_select))
307221      {
308         if (adev->write_byte != NULL)
309         {
310            if ((addr & adev->address_mask)==(adev->select|adev->write_select))
311            {
312               // write the byte from the upper 8 lines
313               adev->write_byte(adev->device, addr, (data>>8) & 0xff);
314            }
315         }
222         // write the byte from the upper 8 lines
223         bus8z_device *devz = static_cast<bus8z_device *>(dev->m_device);
224         devz->write(*m_space, addr, (data>>8) & 0xff);
316225      }
226      dev = dev->m_next;
317227   }
318228
319229   // Takes three cycles
320   device_adjust_icount(device->machine().device("maincpu"),-3);
230   device_adjust_icount(m_cpu,-3);
321231}
322232
323233/* CRU space is not involved in the dmux. */
r14513r14514
326236    DEVICE LIFECYCLE FUNCTIONS
327237***************************************************************************/
328238
329static DEVICE_START( datamux )
239void ti99_datamux_device::device_start(void)
330240{
331   datamux_state *dmux = get_safe_token(device);
332   dmux->ram16b = NULL;
241   m_ram16b = NULL;
333242}
334243
335static DEVICE_STOP( datamux )
244void ti99_datamux_device::device_stop(void)
336245{
337   datamux_state *dmux = get_safe_token(device);
338   if (dmux->ram16b) free(dmux->ram16b);
246   if (m_ram16b) free(m_ram16b);
339247}
340248
341static DEVICE_RESET( datamux )
249void ti99_datamux_device::device_reset(void)
342250{
343   datamux_state *dmux = get_safe_token(device);
344   dmux->use32k = (input_port_read(device->machine(), "RAM")==RAM_TI32_INT)? TRUE : FALSE;
251   const dmux_device_list_entry *list = reinterpret_cast<const dmux_device_list_entry *>(static_config());
345252
346   dmux->devindex = 0;
347   int i = 0;
348   int done=FALSE;
253   m_cpu = machine().device("maincpu");
254   m_space = m_cpu->memory().space(AS_PROGRAM);
349255
350   for (int index = 0; index < MAXDEV; index++)
351   {
352      dmux->component[index].device = NULL;
353   }
256   m_devices.reset(); // clear the list
257   m_use32k = (input_port_read(machine(), "RAM")==1)? TRUE : FALSE;
354258
355   if (dmux->ram16b==NULL) dmux->ram16b = (UINT16*)malloc(32768);
356   bus_device *cons = (bus_device*)device->static_config();
357   while (!done)
259   // better use a region?
260   if (m_ram16b==NULL) m_ram16b = (UINT16*)malloc(32768);
261
262   // Now building the list of active devices at this databus multiplex.
263   // We allow for turning off devices according to configuration switch settings.
264   // In particular, the HSGPL card cannot function unless the console GROMs are
265   // removed.
266   if ( list != NULL )
358267   {
359      if (cons[i].name != NULL)
268      bool done = false;
269      for (int i=0; !done; i++)
360270      {
361         device_t *dev = device->machine().device(cons[i].name);
362         if (dev!=NULL)
271         if (list[i].name == NULL)
363272         {
273            done = true;
274         }
275         else
276         {
364277            UINT32 set = 0;
365            if (cons[i].setting!=NULL)
366               set = input_port_read(device->machine(), cons[i].setting);
367
368            if (((set & cons[i].set)==cons[i].set)&&((set & cons[i].unset)==0))
278            bool active_device = true;
279            if (list[i].setting!=NULL)
369280            {
370               dmux_mount_device(device, dev, cons[i].address_mask, cons[i].select_pattern, cons[i].write_select, cons[i].read, cons[i].write);
371               logerror("ti99_datamux: Device %s mounted at index %d.\n", cons[i].name, i);
281               set = input_port_read(machine(), list[i].setting);
282               active_device = ((set & list[i].set)==list[i].set) && ((set & list[i].unset)==0);
372283            }
284            if (active_device)
285            {
286               device_t *dev = machine().device(list[i].name);
287               if (dev != NULL)
288               {
289                  attached_device *ad = new attached_device(dev, list[i]);
290                  m_devices.append(*ad);
291                  LOG("ti99_datamux: Device %s mounted at index %d.\n", list[i].name, i);
292               }
293               else
294               {
295                  LOG("ti99_datamux: Device %s not found.\n", list[i].name);
296               }
297            }
373298            else
374299            {
375               logerror("ti99_datamux: Device %s not mounted due to configuration setting %s.\n", cons[i].name, cons[i].setting);
300               LOG("ti99_datamux: Device %s not mounted due to configuration setting %s.\n", list[i].name, list[i].setting);
376301            }
377302         }
378         else
379            logerror("ti99_datamux: Device %s not found.\n", cons[i].name);
380303      }
381      else done=TRUE;
382      i++;
383304   }
305   LOG("Datamux device count = %d\n", m_devices.count());
384306}
385307
386static const char DEVTEMPLATE_SOURCE[] = __FILE__;
387
388#define DEVTEMPLATE_ID(p,s)             p##datamux##s
389#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET
390#define DEVTEMPLATE_NAME                "TI-99 Databus 16/8 Multiplexer"
391#define DEVTEMPLATE_FAMILY              "Internal devices"
392#include "devtempl.h"
393
394DEFINE_LEGACY_DEVICE( DMUX, datamux );
308const device_type DATAMUX = &device_creator<ti99_datamux_device>;
src/mess/machine/ti99/videowrp.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/4(A) and /8 Video subsystem
3    This device actually wraps the naked video chip implementation
4    Michael Zapf, October 2010
5*/
4    See videowrp.c for documentation
65
6    Michael Zapf
7    October 2010
8    January 2012: Rewritten as class
9
10*****************************************************************************/
11
712#ifndef __TIVIDEO__
813#define __TIVIDEO__
914
1015#include "video/tms9928a.h"
1116#include "video/v9938.h"
17#include "ti99defs.h"
1218
13#define TMS9928A_TAG   "tms9928a"
14#define SCREEN_TAG      "screen"
19class ti_video_device : public bus8z_device
20{
21public:
22   DECLARE_READ16_MEMBER(noread);
23   DECLARE_WRITE16_MEMBER(nowrite);
1524
16#define READ8Z_DEVICE_HANDLER(name)      void name(ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT8 *value)
25protected:
26   address_space   *m_space;
27   device_t      *m_cpu;
28   tms9928a_device *m_tms9928a;
1729
18#define TI_TMS991X 1
19#define TI_V9938 2
30   /* Constructor */
31   ti_video_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
32   virtual void device_start(void);
33   virtual void device_reset(void);
34   virtual DECLARE_READ8Z_MEMBER(readz) { };
35   virtual DECLARE_WRITE8_MEMBER(write) { };
36};
2037
21typedef struct _ti99_video_config
38/*
39    Connected to the high 8 bits of the 16 bit data bus of the TI-99/4A, D0-D7
40*/
41class ti_std_video_device : public ti_video_device
2242{
23   int                     chip;
43public:
44   ti_std_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
45   DECLARE_READ16_MEMBER(read16);
46   DECLARE_WRITE16_MEMBER(write16);
47};
2448
25} ti99_video_config;
49/*
50    Connected to the multiplexed 8 bit data bus of the TI-99/8
51*/
52class ti_std8_video_device : public ti_video_device
53{
54public:
55   ti_std8_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
56   DECLARE_READ8Z_MEMBER(readz);
57   DECLARE_WRITE8_MEMBER(write);
58};
2659
27READ16_DEVICE_HANDLER( ti_tms991x_r16 );
28READ8Z_DEVICE_HANDLER( ti8_tms991x_rz );
29WRITE16_DEVICE_HANDLER( ti_tms991x_w16 );
30WRITE8_DEVICE_HANDLER( ti8_tms991x_w );
31READ16_DEVICE_HANDLER( ti_video_rnop );
32WRITE16_DEVICE_HANDLER( ti_video_wnop );
33READ16_DEVICE_HANDLER( ti_v9938_r16 );
34WRITE16_DEVICE_HANDLER( ti_v9938_w16 );
35READ8Z_DEVICE_HANDLER( gen_v9938_rz );
36WRITE8_DEVICE_HANDLER( gen_v9938_w );
60/*
61    Used in the EVPC and Geneve
62*/
63class ti_exp_video_device : public ti_video_device
64{
65public:
66   ti_exp_video_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
67   void video_update_mouse(int delta_x, int delta_y, int buttons);
68   DECLARE_READ8Z_MEMBER(readz);
69   DECLARE_WRITE8_MEMBER(write);
70   DECLARE_READ16_MEMBER(read16);
71   DECLARE_WRITE16_MEMBER(write16);
3772
38void video_update_mouse( device_t *device, int delta_x, int delta_y, int buttons);
73protected:
74   void         device_start(void);
75   v9938_device   *m_v9938;
76};
3977
40DECLARE_LEGACY_DEVICE( TIVIDEO, ti99_video );
78/*
79    Sound device wrapper (required until sn74296 is converted to a modern device);
80    let's hitchhike here - it is not required to create yet another file
81    that will be dropped later.
82*/
83class ti_sound_system_device : public bus8z_device
84{
85public:
86   ti_sound_system_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
87   // Cannot read from sound; just ignore silently
88   DECLARE_READ8Z_MEMBER(readz) { };
89   DECLARE_WRITE8_MEMBER(write);
4190
91protected:
92   void device_start(void);
93
94private:
95   device_t *m_sound_chip;
96};
97
98/****************************************************************************/
99
42100#define MCFG_TI_TMS991x_ADD_NTSC(_tag, _chip, _tmsparam)   \
43   MCFG_DEVICE_ADD(_tag, TIVIDEO, 0)                              \
44   MCFG_DEVICE_CONFIG_DATA32(ti99_video_config, chip, TI_TMS991X)         \
101   MCFG_DEVICE_ADD(_tag, TI994AVIDEO, 0)                              \
45102   MCFG_TMS9928A_ADD( TMS9928A_TAG, _chip, _tmsparam )                \
46103   MCFG_TMS9928A_SCREEN_ADD_NTSC( SCREEN_TAG )                      \
47104   MCFG_SCREEN_UPDATE_DEVICE( TMS9928A_TAG, tms9928a_device, screen_update )
48105
49#define MCFG_TI_TMS991x_ADD_PAL(_tag, _chip, _screen_update, _tmsparam)      \
50   MCFG_DEVICE_ADD(_tag, TIVIDEO, 0)                              \
51   MCFG_DEVICE_CONFIG_DATA32(ti99_video_config, chip, TI_TMS991X)         \
106#define MCFG_TI_TMS991x_ADD_PAL(_tag, _chip, _tmsparam)      \
107   MCFG_DEVICE_ADD(_tag, TI994AVIDEO, 0)                              \
52108   MCFG_TMS9928A_ADD( TMS9928A_TAG, _chip, _tmsparam )                  \
53109   MCFG_TMS9928A_SCREEN_ADD_PAL( SCREEN_TAG )                        \
54110   MCFG_SCREEN_UPDATE_DEVICE( TMS9928A_TAG, tms9928a_device, screen_update )
55111
56#define MCFG_TI_V9938_ADD(_tag, _rate, _screen, _blank, _x, _y, _int)      \
57   MCFG_DEVICE_ADD(_tag, TIVIDEO, 0)                              \
58   MCFG_DEVICE_CONFIG_DATA32(ti99_video_config, chip, TI_V9938)         \
59   MCFG_V9938_ADD(_tag "_v9938", _screen, 0x20000)                     \
60   MCFG_V99X8_INTERRUPT_CALLBACK_STATIC(_int)                        \
112#define MCFG_TI998_ADD_NTSC(_tag, _chip, _tmsparam)   \
113   MCFG_DEVICE_ADD(_tag, TI998VIDEO, 0)                              \
114   MCFG_TMS9928A_ADD( TMS9928A_TAG, _chip, _tmsparam )                \
115   MCFG_TMS9928A_SCREEN_ADD_NTSC( SCREEN_TAG )                      \
116   MCFG_SCREEN_UPDATE_DEVICE( TMS9928A_TAG, tms9928a_device, screen_update )
117
118#define MCFG_TI998_ADD_PAL(_tag, _chip, _tmsparam)      \
119   MCFG_DEVICE_ADD(_tag, TI998VIDEO, 0)                              \
120   MCFG_TMS9928A_ADD( TMS9928A_TAG, _chip, _tmsparam )                  \
121   MCFG_TMS9928A_SCREEN_ADD_PAL( SCREEN_TAG )                        \
122   MCFG_SCREEN_UPDATE_DEVICE( TMS9928A_TAG, tms9928a_device, screen_update )
123
124#define MCFG_TI_V9938_ADD(_tag, _rate, _screen, _blank, _x, _y, _devtag, _class, _int)      \
125   MCFG_DEVICE_ADD(_tag, V9938VIDEO, 0)                              \
126   MCFG_V9938_ADD(V9938_TAG, _screen, 0x20000)                     \
127   MCFG_V99X8_INTERRUPT_CALLBACK_DEVICE(_devtag, _class, _int)         \
61128   MCFG_SCREEN_ADD(_screen, RASTER)                              \
62129   MCFG_SCREEN_REFRESH_RATE(_rate)                                 \
63   MCFG_SCREEN_UPDATE_DEVICE(_tag "_v9938", v9938_device, screen_update)   \
130   MCFG_SCREEN_UPDATE_DEVICE(V9938_TAG, v9938_device, screen_update)   \
64131   MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(_blank))               \
65132   MCFG_SCREEN_SIZE(_x, _y)                                    \
66133   MCFG_SCREEN_VISIBLE_AREA(0, _x - 1, 0, _y - 1)                     \
67134   MCFG_PALETTE_LENGTH(512)                                    \
68135   MCFG_PALETTE_INIT(v9938)
69136
137#define MCFG_TI_SOUND_ADD(_tag)         \
138   MCFG_DEVICE_ADD(_tag, TISOUND, 0)
139
140extern const device_type TI994AVIDEO;
141extern const device_type TI998VIDEO;
142extern const device_type V9938VIDEO;
143extern const device_type TISOUND;
144
70145#endif /* __TIVIDEO__ */
71146
src/mess/machine/ti99/speech8.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/8 Speech Synthesizer
3    Michael Zapf, October 2010
4*/
4    See speech8.c for documentation
55
6    Michael Zapf
7    October 2010
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
612#ifndef __TISPEECH8__
713#define __TISPEECH8__
814
15#include "emu.h"
916#include "ti99defs.h"
17#include "sound/tms5220.h"
1018
11READ8Z_DEVICE_HANDLER( ti998spch_rz );
12WRITE8_DEVICE_HANDLER( ti998spch_w );
19extern const device_type TI99_SPEECH8;
1320
14DECLARE_LEGACY_DEVICE( TISPEECH8, ti99_speech8 );
21class ti998_spsyn_device : public bus8z_device
22{
23public:
24   ti998_spsyn_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   DECLARE_READ8Z_MEMBER(readz);
26   DECLARE_WRITE8_MEMBER(write);
1527
16#define MCFG_TISPEECH_ADD(_tag)      \
17   MCFG_DEVICE_ADD(_tag, TISPEECH8, 0)
28   void crureadz(offs_t offset, UINT8 *value) { };
29   void cruwrite(offs_t offset, UINT8 value) { };
30
31   DECLARE_WRITE_LINE_MEMBER( speech8_ready );
32
33   UINT8         *m_speechrom;         // pointer to speech ROM data
34   int          m_load_pointer;         // which 4-bit nibble will be affected by load address
35   int          m_rombits_count;      // current bit position in ROM
36   UINT32         m_sprom_address;      // 18 bit pointer in ROM
37   UINT32         m_sprom_length;         // length of data pointed by speechrom_data, from 0 to 2^18
38
39   int            m_select_mask;
40   int            m_select_value;
41
42protected:
43   void         device_start();
44   void         device_reset(void);
45   const rom_entry *device_rom_region() const;
46   machine_config_constructor device_mconfig_additions() const;
47   void         device_config_complete();
48
49private:
50   device_t      *m_vsp;
51};
52
53#define MCFG_TISPEECH8_ADD(_tag)      \
54   MCFG_DEVICE_ADD(_tag, TI99_SPEECH8, 0)
1855#endif
src/mess/machine/ti99/hfdc.c
r14513r14514
1/*
1/****************************************************************************
2
23    Myarc Hard and Floppy Disk Controller
4    based on SMC9234
5    High Density, Double-sided
6
7    FIXME: HFDC does not work at CRU addresses other than 1100
8    (test shows
9        hfdc: reado 41f5 (00): 00
10        hfdc: reado 41f4 (00): 00 -> wrong rom page? (should be (02)))
11
312    Michael Zapf
4*/
5#include "emu.h"
6#include "peribox.h"
7#include "ti_fdc.h"
8#include "machine/wd17xx.h"
9#include "formats/ti99_dsk.h"
13
14    February 2012: Rewritten as class
15
16*****************************************************************************/
17
18#include "hfdc.h"
1019#include "imagedev/flopdrv.h"
11#include "ti99_hd.h"
1220#include "machine/mm58274c.h"
13#include "hfdc.h"
21#include "formats/ti99_dsk.h"      // Format
1422
15#define CRU_BASE 0x1100
23#define BUFFER "ram"
24#define FDC_TAG "hdc9234"
25#define CLOCK_TAG "mm58274c"
1626
17#define hfdc_region "hfdc_region"
27#define MOTOR_TIMER 1
1828
1929#define HFDC_MAX_FLOPPY 4
2030#define HFDC_MAX_HARD 4
r14513r14514
2838#define VERBOSE 1
2939#define LOG logerror
3040
31typedef ti99_pebcard_config ti99_hfdc_config;
32
33typedef struct _ti99_hfdc_state
41myarc_hfdc_device::myarc_hfdc_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
42: ti_expansion_card_device(mconfig, TI99_HFDC, "Myarc Hard and Floppy Disk Controller", tag, owner, clock)
3443{
35   /* When TRUE, card is accessible. Indicated by a LED. */
36   int               selected;
37
38   /* Card select mask. Required to support Genmod. */
39   int               select_mask;
40
41   /* Card select mask. Required to support Genmod. */
42   int               select_value;
43
44   /* When TRUE, triggers motor monoflop. */
45   int               strobe_motor;
46
47   /* Clock divider bit 0. Unused in this emulation. */
48   int             CD0;
49
50   /* Clock divider bit 1. Unused in this emulation. */
51   int               CD1;
52
53   /* count 4.23s from rising edge of motor_on */
54   emu_timer         *motor_on_timer;
55
56   /* Link to the HDC9234 controller on the board. */
57   device_t      *controller;
58
59   /* Link to the clock chip on the board. */
60   device_t      *clock;
61
62   /* Determines whether we have access to the CRU bits. */
63   int             cru_select;
64
65   /* IRQ state */
66   int             irq;
67
68   /* DMA in Progress state */
69   int             dip;
70
71   /* Output 1 latch */
72   UINT8            output1_latch;
73
74   /* Output 2 latch */
75   UINT8            output2_latch;
76
77   /* Connected floppy drives. */
78   device_t      *floppy_unit[HFDC_MAX_FLOPPY];
79
80   /* Connected harddisk drives. */
81   device_t      *harddisk_unit[HFDC_MAX_HARD];
82
83   /* DMA address latch */
84   UINT32            dma_address;
85
86   /* DSR ROM */
87   UINT8            *rom;
88
89   /* ROM banks. */
90   int             rom_offset;
91
92   /* HFDC RAM */
93   UINT8            *ram;
94
95   /* RAM banks */
96   int             ram_offset[4];
97
98   /* Callback lines to the main system. */
99   ti99_peb_connect   lines;
100
101} ti99_hfdc_state;
102
103INLINE ti99_hfdc_state *get_safe_token(device_t *device)
104{
105   assert(device != NULL);
106   assert(device->type() == HFDC);
107
108   return (ti99_hfdc_state *)downcast<legacy_device_base *>(device)->token();
44   m_shortname = "ti99_hfdc";
10945}
11046
11147/*
112    Resets the drive geometry. This is required because the heuristic of
113    the default implementation sets the drive geometry to the geometry
114    of the medium.
115*/
116static void set_geometry(device_t *drive, floppy_type_t type)
117{
118   if (drive!=NULL)
119      floppy_drive_set_geometry(drive, type);
120   else
121      if (VERBOSE>0) LOG("ti99: HFDC: Drive not found\n");
122}
123
124static void set_all_geometries(device_t *device, floppy_type_t type)
125{
126   set_geometry(device->siblingdevice(FLOPPY_0), type);
127   set_geometry(device->siblingdevice(FLOPPY_1), type);
128   set_geometry(device->siblingdevice(FLOPPY_2), type);
129}
130
131/*************************************************************************/
132
133/*
134    Read disk CRU interface
135    HFDC manual p. 44
136
137    CRU Select=0
138    xx00          Disk controller interrupt
139    xx02          Motor status
140    xx04          DMA in progress
141
142    Note that the cru line settings on the board do not match the
143    description in the manual. 02 and 04 swap their positions (checked with
144    board and schematics). Similarly, the dip switches
145    for the drive configurations are not correctly enumerated in the manual.
146
147    CRU Select=1
148    xx00          Switch 4 status \_ drive 2 config
149    xx02          Switch 3 status /
150    xx04          Switch 2 status \_ drive 1 config
151    xx06          Switch 1 status /
152    xx08          Switch 8 status \_ drive 4 config
153    xx0A          Switch 7 status /
154    xx0C          Switch 6 status \_ drive 3 config
155    xx0E          Switch 5 status /
156
157    config: 00 = 9/16/18 sectors, 40 tracks, 16 msec
158        10 = 9/16/18 sectors, 40 tracks, 8 msec
159        01 = 9/16/18 sectors, 80 tracks, 2 msec
160        11 = 36 sectors, 80 tracks, 2 msec
161*/
162static READ8Z_DEVICE_HANDLER( cru_r )
163{
164   ti99_hfdc_state *card = get_safe_token(device);
165   UINT8 reply;
166
167   if ((offset & 0xff00)==CRU_BASE)
168   {
169      if ((offset & 0x00ff)==0)  /* CRU bits 0-7 */
170      {
171         /* CRU bits */
172         if (card->cru_select)
173         {
174            // DIP switches.  Logic levels are inverted (on->0, off->1).  CRU
175            // bit order is the reverse of DIP-switch order, too (dip 1 -> bit 7,
176            // dip 8 -> bit 0).
177            // MZ: 00 should not be used since there is a bug in the
178            // DSR of the HFDC which causes problems with SD disks
179            // (controller tries DD and then fails to fall back to SD) */
180            reply = ~(input_port_read(device->machine(), "HFDCDIP"));
181         }
182         else
183         {
184            reply = 0;
185
186            if (card->irq)         reply |= 0x01;
187            if (card->dip)         reply |= 0x02;
188            if (card->strobe_motor)   reply |= 0x04;
189         }
190         *value = reply;
191      }
192      else /* CRU bits 8+ */
193         *value = 0;
194   }
195}
196
197/*
198    Write disk CRU interface
199    HFDC manual p. 43
200
201    CRU rel. address    Definition                      Active
202    00                  Device Service Routine Select   HIGH
203    02                  Reset to controller chip        LOW
204    04                  Floppy Motor on / CD0           HIGH
205    06                  ROM page select 0 / CD1
206    08                  ROM page select 1 / CRU input select
207    12/4/6/8/A          RAM page select at 0x5400
208    1C/E/0/2/4          RAM page select at 0x5800
209    26/8/A/C/E          RAM page select at 0x5C00
210
211    RAM bank select: bank 0..31; 12 = LSB (accordingly for other two areas)
212    ROM bank select: bank 0..3; 06 = MSB, 07 = LSB
213    Bit number = (CRU_rel_address - base_address)/2
214    CD0 and CD1 are Clock Divider selections for the Floppy Data Separator (FDC9216)
215*/
216static WRITE8_DEVICE_HANDLER( cru_w )
217{
218   ti99_hfdc_state *card = get_safe_token(device);
219
220   if ((offset & 0xff00)==CRU_BASE)
221   {
222      int bit = (offset >> 1) & 0x1f;
223
224      if (bit >= 9 && bit < 24)
225      {
226         if (data)
227            // we leave index 0 unchanged; modify indexes 1-3
228            card->ram_offset[(bit-4)/5] |= 0x0400 << ((bit-9)%5);
229         else
230            card->ram_offset[(bit-4)/5] &= ~(0x0400 << ((bit-9)%5));
231
232         return;
233      }
234
235      switch (bit)
236      {
237      case 0:
238         card->selected = data;
239         break;
240
241      case 1:
242         if (!data) smc92x4_reset(card->controller);  // active low
243         break;
244
245      case 2:
246         card->CD0 = data;
247         if (data && !card->strobe_motor)
248         {
249            card->motor_on_timer->adjust(attotime::from_msec(4230));
250         }
251         card->strobe_motor = data;
252         break;
253
254      case 3:
255         card->CD1 = data;
256         if (data) card->rom_offset |= 0x2000;
257         else card->rom_offset &= ~0x2000;
258         break;
259
260      case 4:
261         card->cru_select = data;
262         if (data) card->rom_offset |= 0x1000;
263         else card->rom_offset &= ~0x1000;
264         break;
265
266      default:
267         if (VERBOSE>1) LOG("ti99: HFDC: Attempt to set undefined CRU bit %d\n", bit);
268      }
269   }
270}
271
272/*
27348    read a byte in disk DSR space
27449    HFDC manual, p. 44
27550    Memory map as seen by the 99/4A PEB
r14513r14514
28459    0x5800 - 0x5bff static RAM page any of 32 pages
28560    0x5c00 - 0x5fff static RAM page any of 32 pages
28661*/
287
288static READ8Z_DEVICE_HANDLER( data_r )
62READ8Z_MEMBER(myarc_hfdc_device::readz)
28963{
290   ti99_hfdc_state *card = get_safe_token(device);
291
292   if (card->selected && ((offset & card->select_mask)==card->select_value))
64   if (m_selected && ((offset & m_select_mask)==m_select_value))
29365   {
29466      /* DSR region */
29567      if ((offset & 0x1000)==0x0000)
r14513r14514
29971            // Tape: 4fc0...4fcf
30072            if ((offset & 0x1ff0)==TAPE_ADDR)
30173            {
302               if (VERBOSE>0) LOG("ti99: HFDC: Tape support not available (access to address %04x)\n", offset);
74               if (VERBOSE>0) LOG("ti99: HFDC: Tape support not available (access to address %04x)\n", offset&0xffff);
30375               return;
30476            }
30577
r14513r14514
30779            // read: 0100 1111 1101 xx00
30880            if ((offset & 0x1ff3)==HDC_R_ADDR)
30981            {
310               *value = smc92x4_r(card->controller, (offset>>2)&1);
82               *value = m_hdc9234->read(space, (offset>>2)&1, mem_mask);
83               if (VERBOSE>7) LOG("hfdc: read %04x: %02x\n",  offset & 0xffff, *value);
31184               return;
31285            }
31386
31487            if ((offset & 0x1fe1)==CLK_ADDR)
31588            {
316               *value = mm58274c_r(card->clock, (offset & 0x001e) >> 1);
89               *value = mm58274c_r(m_clock, (offset & 0x001e) >> 1);
90               if (VERBOSE>7) LOG("hfdc: read from clock address %04x: %02x\n", offset & 0xffff, *value);
31791               return;
31892            }
31993         }
32094         else
32195         {
32296            // ROM
323            *value = card->rom[card->rom_offset + (offset & 0x0fff)];
97            *value = m_dsrrom[(m_rom_page << 12) | (offset & 0x0fff)];
98            if (VERBOSE>7) LOG("hfdc: reado %04x (%02x): %02x\n",  offset & 0xffff, m_rom_page, *value);
32499            return;
325100         }
326101      }
r14513r14514
333108         // 0101 10xx xxxx xxxx  bank 2
334109         // 0101 11xx xxxx xxxx  bank 3
335110         int bank = (offset & 0x0c00) >> 10;
336         *value = card->ram[card->ram_offset[bank] + (offset & 0x03ff)];
111         *value = m_buffer_ram[(m_ram_page[bank]<<10) | (offset & 0x03ff)];
112         if (VERBOSE>7) LOG("hfdc: read %04x (%02x): %02x\n", offset & 0xffff, m_ram_page[bank], *value);
337113      }
338114   }
339115}
r14513r14514
341117/*
342118    Write a byte to the controller.
343119*/
344static WRITE8_DEVICE_HANDLER( data_w )
120WRITE8_MEMBER( myarc_hfdc_device::write )
345121{
346   ti99_hfdc_state *card = get_safe_token(device);
347   if (card->selected && ((offset & card->select_mask)==card->select_value))
122   if (m_selected && ((offset & m_select_mask)==m_select_value))
348123   {
349124      // Tape: 4fc0...4fcf
350125      if ((offset & 0x1ff0)==TAPE_ADDR)
351126      {
352         if (VERBOSE>0) LOG("ti99: HFDC: Tape support not available (access to address %04x)\n", offset);
127         if (VERBOSE>0) LOG("ti99: HFDC: Tape support not available (access to address %04x)\n", offset & 0xffff);
353128         return;
354129      }
355130
r14513r14514
357132      // write: 0100 1111 1101 xx10
358133      if ((offset & 0x1ff3)==HDC_W_ADDR)
359134      {
360         smc92x4_w(card->controller, (offset>>2)&1, data);
135         if (VERBOSE>7) LOG("hfdc: write to controller address %04x: %02x\n", offset & 0xffff, data);
136         m_hdc9234->write(space, (offset>>2)&1, data, mem_mask);
361137         return;
362138      }
363139
364140      if ((offset & 0x1fe1)==CLK_ADDR)
365141      {
366         mm58274c_w(card->clock, (offset & 0x001e) >> 1, data);
142         if (VERBOSE>7) LOG("hfdc: write to clock address %04x: %02x\n", offset & 0xffff, data);
143         mm58274c_w(m_clock, (offset & 0x001e) >> 1, data);
367144         return;
368145      }
369146
r14513r14514
375152         // 0101 10xx xxxx xxxx  bank 2
376153         // 0101 11xx xxxx xxxx  bank 3
377154         int bank = (offset & 0x0c00) >> 10;
378         card->ram[card->ram_offset[bank] + (offset & 0x03ff)] = data;
155         if (VERBOSE>7) LOG("hfdc: WRITE %04x (%02x): %02x\n", (offset & 0xffff), m_ram_page[bank], data);
156         m_buffer_ram[(m_ram_page[bank]<<10) | (offset & 0x03ff)] = data;
379157      }
380158   }
381159}
382160
383/*
384    Called whenever the state of the sms9234 interrupt pin changes.
385*/
386static WRITE_LINE_DEVICE_HANDLER( intrq_w )
161void myarc_hfdc_device::crureadz(offs_t offset, UINT8 *value)
387162{
388   // Note that the callback functions deliver the calling device, which is
389   // the controller in this case.
390   ti99_hfdc_state *card = get_safe_token(device->owner());
391   card->irq = state;
163   UINT8 reply;
164   if ((offset & 0xff00)==m_cru_base)
165   {
166      if ((offset & 0x00ff)==0)  /* CRU bits 0-7 */
167      {
168         /* CRU bits */
169         if (m_cru_select)
170         {
171            // DIP switches.  Logic levels are inverted (on->0, off->1).  CRU
172            // bit order is the reverse of DIP-switch order, too (dip 1 -> bit 7,
173            // dip 8 -> bit 0).
174            // MZ: 00 should not be used since there is a bug in the
175            // DSR of the HFDC which causes problems with SD disks
176            // (controller tries DD and then fails to fall back to SD) */
177            reply = ~(input_port_read(*this, "HFDCDIP"));
178         }
179         else
180         {
181            reply = 0;
392182
393   // Set INTA* (active low)
394   // Signal from SMC is active high, INTA* is active low; board inverts signal
395   card->lines.inta(state);
183            if (m_irq)         reply |= 0x01;
184            if (m_dip)         reply |= 0x02;
185            if (m_motor_running)   reply |= 0x04;
186         }
187         *value = reply;
188      }
189      else /* CRU bits 8+ */
190         *value = 0;
191      if (VERBOSE>7) LOG("hfdc: reading from CRU %04x: %02x\n", offset, *value);
192   }
396193}
397194
398195/*
399    Called whenever the state of the sms9234 DMA in progress changes.
196    Write disk CRU interface
197    HFDC manual p. 43
198
199    CRU rel. address    Definition                      Active
200    00                  Device Service Routine Select   HIGH
201    02                  Reset to controller chip        LOW
202    04                  Floppy Motor on / CD0           HIGH
203    06                  ROM page select 0 / CD1
204    08                  ROM page select 1 / CRU input select
205    12/4/6/8/A          RAM page select at 0x5400
206    1C/E/0/2/4          RAM page select at 0x5800
207    26/8/A/C/E          RAM page select at 0x5C00
208
209    RAM bank select: bank 0..31; 12 = LSB (accordingly for other two areas)
210    ROM bank select: bank 0..3; 06 = MSB, 07 = LSB
211    Bit number = (CRU_rel_address - base_address)/2
212    CD0 and CD1 are Clock Divider selections for the Floppy Data Separator (FDC9216)
400213*/
401static WRITE_LINE_DEVICE_HANDLER( dip_w )
214void myarc_hfdc_device::cruwrite(offs_t offset, UINT8 data)
402215{
403   ti99_hfdc_state *card = get_safe_token(device->owner());
404   card->dip = state;
405}
406
407// device ok
408static WRITE8_DEVICE_HANDLER( auxbus_out )
409{
410   ti99_hfdc_state *card = get_safe_token(device->owner());
411   switch (offset)
216   if ((offset & 0xff00)==m_cru_base)
412217   {
413   case INPUT_STATUS:
414      if (VERBOSE>1) LOG("ti99: HFDC: Invalid operation: S0=S1=0, but tried to write (expected: read drive status)\n");
415      break;
218      if (VERBOSE>7) LOG("hfdc: writing to CRU %04x: %02x\n", offset, data);
219      int bit = (offset >> 1) & 0x1f;
416220
417   case OUTPUT_DMA_ADDR:
418      /* Value is dma address byte. Shift previous contents to the left. */
419      card->dma_address = ((card->dma_address << 8) + (data&0xff))&0xffffff;
420      break;
221      if (bit >= 9 && bit < 24)
222      {
223         if (data)
224            // we leave index 0 unchanged; modify indices 1-3
225            m_ram_page[(bit-4)/5] |= 1 << ((bit-9)%5);
226         else
227            m_ram_page[(bit-4)/5] &= ~(1 << ((bit-9)%5));
421228
422   case OUTPUT_OUTPUT1:
423      /* value is output1 */
424      card->output1_latch = data;
425      break;
229         return;
230      }
426231
427   case OUTPUT_OUTPUT2:
428      /* value is output2 */
429      card->output2_latch = data;
430      break;
431   }
432}
232      switch (bit)
233      {
234      case 0:
235         m_selected = (data!=0);
236         if (VERBOSE>7) LOG("hfdc: selected = %d\n", m_selected);
237         break;
433238
434static int line_to_index(UINT8 line, int max)
435{
436   // Calculate the binary logarithm of the given 2-multiple
437   int index = 0;
438   while (index < max)
439   {
440      if ((line & 0x01)!=0)
239      case 1:
240         if (data==0) m_hdc9234->reset();  // active low
441241         break;
442      else
443      {
444         line = line >> 1;
445         index++;
446      }
447   }
448   if (index==max) return -1;
449   return index;
450}
451242
452/*
453    Select the HFDC floppy disk unit
454    This function implements the selection logic on the PCB (outside the
455    controller chip)
456    (Callback function)
457*/
243      case 2:
244         m_CD0 = data;
245         if (data != 0) m_motor_running = true;
246         else
247         {
248            if (m_motor_running)
249            {
250               m_motor_on_timer->adjust(attotime::from_msec(4230));
251            }
252         }
253         break;
458254
459static device_t *current_floppy(device_t *controller)
460{
461   int disk_unit = -1;
462   ti99_hfdc_state *card = get_safe_token(controller->owner());
255      case 3:
256         m_CD1 = data;
257         if (data!=0) m_rom_page |= 2;
258         else m_rom_page &= 0xfd;
259         break;
463260
464   disk_unit = line_to_index(card->output1_latch & 0x0f, HFDC_MAX_FLOPPY);
261      case 4:
262         m_cru_select = (data!=0);
263         if (data!=0) m_rom_page |= 1;
264         else m_rom_page &= 0xfe;
265         break;
465266
466   if (disk_unit<0)
467   {
468      if (VERBOSE>1) LOG("ti99: HFDC: No unit selected\n");
469      return NULL;
267      default:
268         if (VERBOSE>1) LOG("ti99: HFDC: Attempt to set undefined CRU bit %d\n", bit);
269      }
470270   }
471
472   return card->floppy_unit[disk_unit];
473271}
474272
475/*
476    Select the HFDC hard disk unit
477    This function implements the selection logic on the PCB (outside the
478    controller chip)
479    The HDC9234 allows up to four drives to be selected by the select lines.
480    This is not enough for controlling four floppy drives and three hard
481    drives, so the user programmable outputs are used to select the floppy
482    drives. This selection happens completely outside of the controller,
483    so we must implement it here.
484*/
485273
486static device_t *current_harddisk(device_t *controller)
274int myarc_hfdc_device::slog2(int value)
487275{
488   ti99_hfdc_state *card = get_safe_token(controller->owner());
489
490   int disk_unit = -1;
491   disk_unit = line_to_index((card->output1_latch>>4)&0x0f, HFDC_MAX_HARD);
492
493   /* The HFDC does not use disk_unit=0. The first HD has index 1. */
494
495   if (disk_unit>=0)
496      return card->harddisk_unit[disk_unit];
497   else
498      return NULL;
276   int i=-1;
277   while (value!=0)
278   {
279      value >>= 1;
280      i++;
281   }
282   return i;
499283}
500284
501static READ_LINE_DEVICE_HANDLER( auxbus_in )
285READ8_MEMBER( myarc_hfdc_device::auxbus_in )
502286{
503   ti99_hfdc_state *card = get_safe_token(device->owner());
504   device_t *drive;
505287   UINT8 reply = 0;
288   int index = 0;
506289
507   if (card->output1_latch==0)
290   if ((m_output1_latch & 0xf0)==0)
508291   {
292      if (VERBOSE>4) LOG("hfdc: no drive selected, returning 0\n");
293      // No HD and no floppy
509294      return 0; /* is that the correct default value? */
510295   }
511296
512   /* If a floppy is selected, we have one line set among the four programmable outputs. */
513   if ((card->output1_latch & 0x0f)!=0)
297   // If a floppy is selected, we have one line set among the four programmable outputs.
298   // Floppy selected <=> latch & 0x10 != 0; floppy number in last four bits
299   if ((m_output1_latch & 0x10)!=0)
514300   {
515      drive = current_floppy(device);
301      index = slog2(m_output1_latch & 0x0f);
302      if (index==-1) return 0;
516303
517304      /* Get floppy status. */
518      if (floppy_drive_get_flag_state(drive, FLOPPY_DRIVE_INDEX) == FLOPPY_DRIVE_INDEX)
305      if (floppy_drive_get_flag_state(m_floppy_unit[index], FLOPPY_DRIVE_INDEX) == FLOPPY_DRIVE_INDEX)
519306         reply |= DS_INDEX;
520      if (floppy_tk00_r(drive) == CLEAR_LINE)
307      if (floppy_tk00_r(m_floppy_unit[index]) == CLEAR_LINE)
521308         reply |= DS_TRK00;
522      if (floppy_wpt_r(drive) == CLEAR_LINE)
309      if (floppy_wpt_r(m_floppy_unit[index]) == CLEAR_LINE)
523310         reply |= DS_WRPROT;
524311
525312      /* if (image_exists(disk_img)) */
526313
527314      reply |= DS_READY;  /* Floppies don't have a READY line; line is pulled up */
528315      reply |= DS_SKCOM;  /* Same here. */
316      if (VERBOSE>4) LOG("hfdc: floppy selected, returning status %02x\n", reply);
529317   }
530   else /* one of the first four lines must be selected */
318   else // one of the first three lines must be selected
531319   {
532320      UINT8 state;
533      drive = current_harddisk(device);
534      state = ti99_mfm_harddisk_status(drive);
321      index = slog2((m_output1_latch>>4) & 0x0f)-1;
322      mfm_harddisk_device *hd = static_cast<mfm_harddisk_device *>(m_harddisk_unit[index]);
323      state = hd->get_status();
324
535325      if (state & MFMHD_TRACK00)      reply |= DS_TRK00;
536326      if (state & MFMHD_SEEKCOMP)      reply |= DS_SKCOM;
537327      if (state & MFMHD_WRFAULT)      reply |= DS_WRFAULT;
538328      if (state & MFMHD_INDEX)      reply |= DS_INDEX;
539329      if (state & MFMHD_READY)      reply |= DS_READY;
330      if (VERBOSE>4) LOG("hfdc: hd selected, returning status %02x\n", reply);
540331   }
541332
542333   return reply;
543334}
544335
336WRITE8_MEMBER( myarc_hfdc_device::auxbus_out )
337{
338   int index;
339   device_t* selected_drive = NULL;
340
341   switch (offset)
342   {
343   case INPUT_STATUS:
344      if (VERBOSE>1) LOG("ti99: HFDC: Invalid operation: S0=S1=0, but tried to write (expected: read drive status)\n");
345      break;
346
347   case OUTPUT_DMA_ADDR:
348      /* Value is dma address byte. Shift previous contents to the left. */
349      m_dma_address = ((m_dma_address << 8) + (data&0xff))&0xffffff;
350      break;
351
352   case OUTPUT_OUTPUT1:
353      // value is output1
354      // The HFDC interprets the value as follows:
355      // WDS = Winchester Drive System, old name for hard disk
356      // +------+------+------+------+------+------+------+------+
357      // | WDS3 | WDS2 | WDS1 | DSKx | x=4  | x=3  | x=2  | x=1  |
358      // +------+------+------+------+------+------+------+------+
359      // Accordingly, drive 0 is always the floppy; selected by the low nibble
360      if (data & 0x10)
361      {
362         // Floppy selected
363         index = slog2(data & 0x0f);
364         if (index>=0) selected_drive = m_floppy_unit[index];
365      }
366      else
367      {
368         // HD selected
369         index = slog2((data>>4) & 0x0f);
370         if (index>=0) selected_drive = m_harddisk_unit[index-1];
371      }
372
373      m_hdc9234->connect_drive(selected_drive);
374      m_output1_latch = data;
375      break;
376
377   case OUTPUT_OUTPUT2:
378      /* value is output2 */
379      m_output2_latch = data;
380      break;
381   }
382}
383
545384/*
546385    Read a byte from buffer in DMA mode
547386*/
548static UINT8 hfdc_dma_read_callback(device_t *device)
387READ8_MEMBER( myarc_hfdc_device::read_buffer )
549388{
550   ti99_hfdc_state *card = get_safe_token(device->owner());
551   UINT8 value = card->ram[card->dma_address & 0x7fff];
552   card->dma_address++;
389   UINT8 value = m_buffer_ram[m_dma_address & 0x7fff];
390   m_dma_address++;
553391   return value;
554392}
555393
556394/*
557395    Write a byte to buffer in DMA mode
558396*/
559static void hfdc_dma_write_callback(device_t *device, UINT8 data)
397WRITE8_MEMBER( myarc_hfdc_device::write_buffer )
560398{
561   ti99_hfdc_state *card = get_safe_token(device->owner());
562   card->ram[card->dma_address & 0x7fff] = data;
563   card->dma_address++;
399   m_buffer_ram[m_dma_address & 0x7fff] = data;
400   m_dma_address++;
564401}
565402
566403/*
567    callback called at the end of the motor movement
404    Called whenever the state of the sms9234 interrupt pin changes.
568405*/
569static TIMER_CALLBACK(motor_on_timer_callback)
406WRITE_LINE_MEMBER( myarc_hfdc_device::intrq_w )
570407{
571   // currently no activity here
408   m_irq = state;
409
410   // Set INTA*
411   // Signal from SMC is active high, INTA* is active low; board inverts signal
412   // Anyway, we keep with ASSERT_LINE and CLEAR_LINE
413   m_slot->set_inta(state);
572414}
573415
416
574417/*
575    Preliminary MFM harddisk interface.
418    Called whenever the state of the sms9234 DMA in progress changes.
576419*/
577static void hfdc_harddisk_get_next_id(device_t *controller, int head, chrn_id_hd *id)
420WRITE_LINE_MEMBER( myarc_hfdc_device::dip_w )
578421{
579   device_t *harddisk = current_harddisk(controller);
580   if (harddisk != NULL)
581      ti99_mfm_harddisk_get_next_id(harddisk, head, id);
422   m_dip = state;
582423}
583424
584static void hfdc_harddisk_seek(device_t *controller, int direction)
425/*
426    Callback called at the end of DVENA pulse
427*/
428void myarc_hfdc_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
585429{
586   device_t *harddisk = current_harddisk(controller);
587   if (harddisk != NULL)
588      ti99_mfm_harddisk_seek(harddisk, direction);
430   m_motor_running = false;
431   if (VERBOSE>6) LOG("hfdc: motor off\n");
589432}
590433
591static void hfdc_harddisk_read_sector(device_t *controller, int cylinder, int head, int sector, UINT8 **buf, int *sector_length)
434const smc92x4_interface ti99_smc92x4_interface =
592435{
593   device_t *harddisk = current_harddisk(controller);
594   if (harddisk != NULL)
595      ti99_mfm_harddisk_read_sector(harddisk, cylinder, head, sector, buf, sector_length);
596}
436   FALSE,      /* do not use the full track layout */
437   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, intrq_w),
438   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, dip_w),
439   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, auxbus_out),
440   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, auxbus_in),
441   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, read_buffer),
442   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, myarc_hfdc_device, write_buffer)
443};
597444
598static void hfdc_harddisk_write_sector(device_t *controller, int cylinder, int head, int sector, UINT8 *buf, int sector_length)
445static const mm58274c_interface floppy_mm58274c_interface =
599446{
600   device_t *harddisk = current_harddisk(controller);
601   if (harddisk != NULL)
602      ti99_mfm_harddisk_write_sector(harddisk, cylinder, head, sector, buf, sector_length);
603}
447   1,   /*  mode 24*/
448   0   /*  first day of week */
449};
604450
605static void hfdc_harddisk_read_track(device_t *controller, int head, UINT8 **buffer, int *data_count)
606{
607   device_t *harddisk = current_harddisk(controller);
608   if (harddisk != NULL)
609      ti99_mfm_harddisk_read_track(harddisk, head, buffer, data_count);
610}
451MACHINE_CONFIG_FRAGMENT( ti99_hfdc )
452   MCFG_SMC92X4_ADD(FDC_TAG, ti99_smc92x4_interface )
453   MCFG_MM58274C_ADD(CLOCK_TAG, floppy_mm58274c_interface)
454MACHINE_CONFIG_END
611455
612static void hfdc_harddisk_write_track(device_t *controller, int head, UINT8 *buffer, int data_count)
613{
614   device_t *harddisk = current_harddisk(controller);
615   if (harddisk != NULL)
616      ti99_mfm_harddisk_write_track(harddisk, head, buffer, data_count);
617}
456ROM_START( ti99_hfdc )
457   ROM_REGION(0x4000, DSRROM, 0)
458   ROM_LOAD("hfdc.bin", 0x0000, 0x4000, CRC(66fbe0ed) SHA1(11df2ecef51de6f543e4eaf8b2529d3e65d0bd59)) /* HFDC disk DSR ROM */
459   ROM_REGION(0x8000, BUFFER, 0)  /* HFDC RAM buffer 32 KiB */
460   ROM_FILL(0x0000, 0x8000, 0x00)
461ROM_END
618462
619const smc92x4_interface ti99_smc92x4_interface =
620{
621   FALSE,      /* do not use the full track layout */
622   DEVCB_LINE(intrq_w),
623   DEVCB_LINE(dip_w),
624   DEVCB_HANDLER(auxbus_out),
625   DEVCB_LINE(auxbus_in),
463INPUT_PORTS_START( ti99_hfdc )
464   PORT_START( "CRUHFDC" )
465   PORT_DIPNAME( 0x1f00, 0x1100, "HFDC CRU base" )
466      PORT_DIPSETTING( 0x1000, "1000" )
467      PORT_DIPSETTING( 0x1100, "1100" )
468      PORT_DIPSETTING( 0x1200, "1200" )
469      PORT_DIPSETTING( 0x1300, "1300" )
470      PORT_DIPSETTING( 0x1400, "1400" )
471      PORT_DIPSETTING( 0x1500, "1500" )
472      PORT_DIPSETTING( 0x1600, "1600" )
473      PORT_DIPSETTING( 0x1700, "1700" )
474      PORT_DIPSETTING( 0x1800, "1800" )
475      PORT_DIPSETTING( 0x1900, "1900" )
476      PORT_DIPSETTING( 0x1a00, "1A00" )
477      PORT_DIPSETTING( 0x1b00, "1B00" )
478      PORT_DIPSETTING( 0x1c00, "1C00" )
479      PORT_DIPSETTING( 0x1d00, "1D00" )
480      PORT_DIPSETTING( 0x1e00, "1E00" )
481      PORT_DIPSETTING( 0x1f00, "1F00" )
626482
627   current_floppy,
628   hfdc_dma_read_callback,
629   hfdc_dma_write_callback,
483   PORT_START( "HFDCDIP" )
484   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" )
485      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
486      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
487      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
488      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
630489
631   /* Preliminary MFM harddisk interface. */
632   hfdc_harddisk_get_next_id,
633   hfdc_harddisk_seek,
634   hfdc_harddisk_read_sector,
635   hfdc_harddisk_write_sector,
636   hfdc_harddisk_read_track,
637   hfdc_harddisk_write_track
638};
490   PORT_START( "DRVSPD" )
491   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" )
492      PORT_CONFSETTING( 0x00, "No delay")
493      PORT_CONFSETTING( 0x01, "Realistic")
494INPUT_PORTS_END
639495
640static const ti99_peb_card hfdc_card =
496void myarc_hfdc_device::device_start()
641497{
642   data_r,
643   data_w,
644   cru_r,
645   cru_w,
498   if (VERBOSE>5) LOG("hfdc: start\n");
499   m_hdc9234 = static_cast<smc92x4_device *>(subdevice(FDC_TAG));
500   m_clock = subdevice(CLOCK_TAG);
501   m_dsrrom = subregion(DSRROM)->base();
502   m_buffer_ram = subregion(BUFFER)->base();
646503
647   NULL, NULL,   NULL, NULL
648};
504   m_motor_on_timer = timer_alloc(MOTOR_TIMER);
649505
650static DEVICE_START( ti99_hfdc )
651{
652   ti99_hfdc_state *card = get_safe_token(device);
653
654   /* Resolve the callbacks to the PEB */
655   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
656   card->lines.inta.resolve(topeb->inta, *device);
657506   // The HFDC does not use READY; it has on-board RAM for DMA
658
659   card->motor_on_timer = device->machine().scheduler().timer_alloc(FUNC(motor_on_timer_callback), (void *)device);
660   card->ram = NULL;
661   card->ram_offset[0] = 0x2000; // static bank
662   card->controller = device->subdevice("smc92x4");
663   card->clock = device->subdevice("mm58274c");
664   astring region(device->tag(), ":", hfdc_region);
665   card->rom = device->machine().region(region.cstr())->base();
666507}
667508
668static DEVICE_STOP( ti99_hfdc )
509void myarc_hfdc_device::device_reset()
669510{
670   ti99_hfdc_state *card = get_safe_token(device);
671   if (card->ram) free(card->ram);
672}
673
674static DEVICE_RESET( ti99_hfdc )
675{
676   ti99_hfdc_state *card = get_safe_token(device);
677   static const char *const flopname[] = {FLOPPY_0, FLOPPY_1, FLOPPY_2, FLOPPY_3};
678   static const char *const hardname[] = {MFMHD_0, MFMHD_1, MFMHD_2};
679
680   /* If the card is selected in the menu, register the card */
681   if (input_port_read(device->machine(), "DISKCTRL") == DISK_HFDC)
511   if (VERBOSE>5) LOG("hfdc: reset\n");
512   if (m_genmod)
682513   {
683      device_t *peb = device->owner();
684      int success = mount_card(peb, device, &hfdc_card, get_pebcard_config(device)->slot);
685      if (!success) return;
514      m_select_mask = 0x1fe000;
515      m_select_value = 0x174000;
516   }
517   else
518   {
519      m_select_mask = 0x7e000;
520      m_select_value = 0x74000;
521   }
686522
687      card->select_mask = 0x7e000;
688      card->select_value = 0x74000;
523   m_cru_base = input_port_read(*this, "CRUHFDC");
689524
690      if (input_port_read(device->machine(), "MODE")==GENMOD)
691      {
692         // GenMod card modification
693         if (VERBOSE>0) LOG("ti99: HFDC: Configuring for GenMod\n");
694         card->select_mask = 0x1fe000;
695         card->select_value = 0x174000;
696      }
525   // Resetting values
526   m_rom_page = 0;
697527
698      // Allocate 32 KiB for on-board buffer memory
699      if (card->ram==NULL)
700      {
701         card->ram = (UINT8*)malloc(32768);
702//          memset(card->ram, 0, 32768);
703      }
528   m_ram_page[0] = 0x08;   // static page 0x08
529   for (int i=1; i < 4; i++) m_ram_page[i] = 0;
530   m_dma_address = 0;
531   m_output1_latch = m_output2_latch = 0;
532   m_dip = m_irq = false;
533   m_cru_select = false;
534   m_CD0 = m_CD1 = 0;
535   m_motor_running = false;
536   m_selected = false;
704537
705      if (input_port_read(device->machine(), "HFDCDIP")&0x55)
706         ti99_set_80_track_drives(TRUE);
707      else
708         ti99_set_80_track_drives(FALSE);
538   // Find the floppies and hard disks
539   m_floppy_unit[0] = m_slot->get_drive(FLOPPY_0);
540   m_floppy_unit[1] = m_slot->get_drive(FLOPPY_1);
541   m_floppy_unit[2] = m_slot->get_drive(FLOPPY_2);
542   m_floppy_unit[3] = m_slot->get_drive(FLOPPY_3);
709543
710      smc92x4_set_timing(card->controller, input_port_read(device->machine(), "DRVSPD"));
544   m_harddisk_unit[0] = m_slot->get_drive(MFMHD_0);
545   m_harddisk_unit[1] = m_slot->get_drive(MFMHD_1);
546   m_harddisk_unit[2] = m_slot->get_drive(MFMHD_2);
711547
712      // Connect floppy drives to controller. Note that *this* is the
713      // controller, not the controller chip. The pcb contains a select
714      // logic.
715      int i;
716      for (i = 0; i < 4; i++)
717      {
718         card->floppy_unit[i] = device->siblingdevice(flopname[i]);
548   if (input_port_read(*this, "HFDCDIP")&0x55)
549      ti99_set_80_track_drives(TRUE);
550   else
551      ti99_set_80_track_drives(FALSE);
719552
720         if (card->floppy_unit[i]!=NULL)
721         {
722            floppy_drive_set_controller(card->floppy_unit[i], device);
723            //          floppy_drive_set_index_pulse_callback(floppy_unit[i], smc92x4_index_pulse_callback);
724            floppy_drive_set_rpm(card->floppy_unit[i], 300.);
725         }
726         else
727         {
728            if (VERBOSE>0) LOG("ti99: HFDC: Image %s is null\n", flopname[i]);
729         }
730      }
553   m_hdc9234->set_timing(input_port_read(*this, "DRVSPD")!=0);
731554
732      /* In the HFDC ROM, WDSx selects drive x; drive 0 is not used */
733      for (i = 1; i < 4; i++)
555   floppy_type_t floptype = FLOPPY_STANDARD_5_25_DSHD;
556   // Again, need to re-adjust the floppy geometries
557   for (int i=0; i < HFDC_MAX_FLOPPY; i++)
558   {
559      if (m_floppy_unit[i] != NULL)
734560      {
735         card->harddisk_unit[i] = device->siblingdevice(hardname[i-1]);
561         //  floppy_drive_set_controller(card->floppy_unit[i], device);
562         //  floppy_drive_set_index_pulse_callback(floppy_unit[i], smc92x4_index_pulse_callback);
563         floppy_drive_set_geometry(m_floppy_unit[i], floptype);
736564      }
737      card->harddisk_unit[0] = NULL;
738
739      floppy_type_t flop = FLOPPY_STANDARD_5_25_DSHD;
740      set_all_geometries(device, flop);
741
742      card->strobe_motor = 0;
743      card->ram_offset[1] = card->ram_offset[2] = card->ram_offset[3] = 0;
744      card->rom_offset = 0;
745565   }
566
567   if (VERBOSE>2) LOG("hfdc: CRU base = %04x\n", m_cru_base);
746568   // TODO: Check how to make use of   floppy_mon_w(w->drive, CLEAR_LINE);
747569}
748570
749static const mm58274c_interface floppy_mm58274c_interface =
571machine_config_constructor myarc_hfdc_device::device_mconfig_additions() const
750572{
751   1,   /*  mode 24*/
752   0   /*  first day of week */
753};
573   return MACHINE_CONFIG_NAME( ti99_hfdc );
574}
754575
755MACHINE_CONFIG_FRAGMENT( ti99_hfdc )
756   MCFG_SMC92X4_ADD("smc92x4", ti99_smc92x4_interface )
757   MCFG_MM58274C_ADD("mm58274c", floppy_mm58274c_interface)
758MACHINE_CONFIG_END
576const rom_entry *myarc_hfdc_device::device_rom_region() const
577{
578   return ROM_NAME( ti99_hfdc );
579}
759580
760ROM_START( ti99_hfdc )
761   ROM_REGION(0x4000, hfdc_region, 0)
762   ROM_LOAD_OPTIONAL("hfdc.bin", 0x0000, 0x4000, CRC(66fbe0ed) SHA1(11df2ecef51de6f543e4eaf8b2529d3e65d0bd59)) /* HFDC disk DSR ROM */
763ROM_END
581ioport_constructor myarc_hfdc_device::device_input_ports() const
582{
583   return INPUT_PORTS_NAME(ti99_hfdc);
584}
764585
765static const char DEVTEMPLATE_SOURCE[] = __FILE__;
766
767#define DEVTEMPLATE_ID(p,s)             p##ti99_hfdc##s
768#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
769#define DEVTEMPLATE_NAME                "Myarc Hard and Floppy Disk Controller Card"
770#define DEVTEMPLATE_SHORTNAME           "myarchfdc"
771#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
772#include "devtempl.h"
773
774DEFINE_LEGACY_DEVICE( HFDC, ti99_hfdc );
775
586const device_type TI99_HFDC = &device_creator<myarc_hfdc_device>;
src/mess/machine/ti99/ti99defs.h
r14513r14514
1/*
1/****************************************************************************
2
23    Common definitions for TI family
3*/
4   Should be included in each component.   
45
6   Michael Zapf
7   
8*****************************************************************************/
9
510#ifndef __TI99DEFS__
611#define __TI99DEFS__
712
8#define region_grom "cons_grom"
9#define READ8Z_DEVICE_HANDLER(name)      void name(ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT8 *value)
10#define READ16Z_DEVICE_HANDLER(name)      void name(ATTR_UNUSED device_t *device, ATTR_UNUSED offs_t offset, UINT16 *value)
13// TI-99/4(A)
14#define region_grom    "cons_grom"
15#define region_grom_cart    "cart_grom"
16#define TMS9901_TAG    "tms9901"
17#define TIBOARD_TAG    "ti_board"
18#define DATAMUX_TAG    "datamux_16_8"
19#define VIDEO_SYSTEM_TAG "video"
20#define TMS9928A_TAG   "tms9928a"
21#define V9938_TAG       "v9938"
22#define SCREEN_TAG      "screen"
23#define TISOUNDCHIP_TAG "soundchip"
24#define TISOUND_TAG    "tisound"
25#define GROMPORT_TAG   "gromport"
26#define GROM0_TAG      "console_grom_0"
27#define GROM1_TAG      "console_grom_1"
28#define GROM2_TAG      "console_grom_2"
29#define PERIBOX_TAG      "peb"
30#define MECMOUSE_TAG   "mecmouse"
31#define HANDSET_TAG      "handset"
1132
12#define GENMOD 0x01
33// TI-99/8
34#define SRAM_TAG      "sram8"
35#define DRAM_TAG      "dram8"
36#define MAPPER_TAG       "mapper"
37#define SPEECH_TAG      "speech"
1338
14enum
15{
16   GM_TURBO = 1,
17   GM_TIM = 2,
18   GM_NEVER = 42
19};
39// Geneve
40#define   GKEYBOARD_TAG   "gkeyboard"
41#define GMAPPER_TAG      "gmapper"
42#define GMOUSE_TAG      "gmouse"
43#define GCLOCK_TAG      "mm58274c"
2044
21enum
22{
23   RAM_NONE = 0,
24   RAM_TI32_INT,
25   RAM_TI32_EXT,
26   RAM_SUPERAMS1024,
27   RAM_FOUNDATION128,
28   RAM_FOUNDATION512,
29   RAM_MYARC128,
30   RAM_MYARC512,
31   RAM_99_4P,
32   RAM_99_8
33};
45#define READ16Z_MEMBER(name)         void name(ATTR_UNUSED address_space &space, ATTR_UNUSED offs_t offset,  ATTR_UNUSED UINT16 *value, ATTR_UNUSED UINT16 mem_mask)
46#define DECLARE_READ16Z_MEMBER(name)    void name(ATTR_UNUSED address_space &space, ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT16 *value, ATTR_UNUSED UINT16 mem_mask = 0xffff)
47#define READ8Z_MEMBER(name)            void name(ATTR_UNUSED address_space &space, ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT8 *value, ATTR_UNUSED UINT8 mem_mask)
48#define DECLARE_READ8Z_MEMBER(name)      void name(ATTR_UNUSED address_space &space, ATTR_UNUSED offs_t offset, ATTR_UNUSED UINT8 *value, ATTR_UNUSED UINT8 mem_mask = 0xff)
3449
35enum
36{
37   DISK_NONE = 0,
38   DISK_TIFDC,
39   DISK_BWG,
40   DISK_HFDC
41};
50#define GENMOD 0x01
4251
43enum
52/*
53   Base class for all devices that have a special read method. This read
54   methods differs from the standard READ8/16 by allowing for a "high-impedance"
55   state "Z" which means that the bus lines are not changed. That way, we can
56   built busses with parallel devices as in the real machine, and only the
57   active device changes the bus lines.
58*/
59class bus8z_device : public device_t
4460{
45   HD_NONE = 0,
46   HD_IDE,
47   HD_WHTECH,
48   HD_USB = 4
61public:
62   bus8z_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock)
63   : device_t(mconfig, type, name, tag, owner, clock) { }
64   virtual DECLARE_READ8Z_MEMBER(readz) =0;
65   virtual DECLARE_WRITE8_MEMBER(write) =0;
4966};
5067
51enum
68class bus16z_device : device_t
5269{
53   SERIAL_NONE = 0,
54   SERIAL_TI
70public:
71   virtual DECLARE_READ16Z_MEMBER(read16z) =0;
72   virtual DECLARE_WRITE16_MEMBER(write16) =0;
5573};
5674
57enum
58{
59   EXT_NONE = 0,
60   EXT_HSGPL_FLASH = 1,
61   EXT_HSGPL_ON = 2,
62   EXT_PCODE = 4
63};
75/****************************************************************************
76   Constants
77****************************************************************************/
6478
6579enum
6680{
67   HCI_NONE = 0,
68   HCI_MECMOUSE = 1,
69   HCI_IR = 4
81   TI994,
82   TI994A
7083};
7184
7285enum
7386{
74   CART_AUTO = 0,
75   CART_1,
76   CART_2,
77   CART_3,
78   CART_4,
79   CART_GK = 15
87   GM_TURBO = 1,
88   GM_TIM = 2
8089};
8190
8291enum
8392{
84   GK_OFF = 0,
85   GK_NORMAL = 1,
86   GK_GRAM0 = 0,
87   GK_OPSYS = 1,
88   GK_GRAM12 = 0,
89   GK_TIBASIC = 1,
90   GK_BANK1 = 0,
91   GK_WP = 1,
92   GK_BANK2 = 2,
93   GK_LDON = 0,
94   GK_LDOFF = 1
93   GENEVE_098 = 0,
94   GENEVE_100 = 1
9595};
9696
9797#endif
src/mess/machine/ti99/samsmem.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 SuperAMS memory expansion
3    Michael Zapf, September 2010
4*/
4    See samsmem.c for documentation
55
6    Michael Zapf
7    September 2010
8
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
613#ifndef __SAMSMEM__
714#define __SAMSMEM__
815
9DECLARE_LEGACY_DEVICE( SAMSMEM, ti99_samsmem );
16#include "emu.h"
17#include "peribox.h"
18#include "ti99defs.h"
19
20extern const device_type TI99_SAMSMEM;
21
22class sams_memory_expansion_device : public ti_expansion_card_device
23{
24public:
25   sams_memory_expansion_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
26   DECLARE_READ8Z_MEMBER(readz);
27   DECLARE_WRITE8_MEMBER(write);
28
29   void crureadz(offs_t offset, UINT8 *value);
30   void cruwrite(offs_t offset, UINT8 data);
31
32protected:
33   void device_start(void);
34   void device_reset(void);
35   const rom_entry *device_rom_region(void) const;
36
37private:
38   UINT8*   m_ram;
39   int      m_mapper[16];
40   bool   m_map_mode;
41   bool   m_access_mapper;
42};
1043#endif
src/mess/machine/ti99/ti_rs232.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 RS232 and Parallel interface card
34
45    TI99 RS232 card ('rs232')
r14513r14514
9394
9495    The protocol changes back to normal mode after transmitting the control
9596    sequence.
96*/
9797
98#include "emu.h"
99#include "peribox.h"
100#include "machine/tms9902.h"
98    Michael Zapf
99    February 2012: Rewritten as class
100
101*****************************************************************************/
102
101103#include "ti_rs232.h"
102104
103#define CRU_BASE 0x1300
104105#define SENILA_0_BIT 0x80
105106#define SENILA_1_BIT 0x40
106107
107#define ser_region "ser_region"
108
109108#define RECV_MODE_NORMAL 1
110109#define RECV_MODE_ESC 2
111110#define RECV_MODE_ESC_LINES 3
r14513r14514
113112#define VERBOSE 1
114113#define LOG logerror
115114
116// Second card: Change CRU base to 0x1500
117// #define CRU_BASE 0x1500
115#define ESC 0x1b
118116
119typedef ti99_pebcard_config ti_rs232_config;
120
121typedef struct _ti_rs232_state
117ti_rs232_pio_device::ti_rs232_pio_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
118: ti_expansion_card_device(mconfig, TI99_RS232, "TI-99 RS232/PIO interface", tag, owner, clock)
122119{
123   device_t *uart[2];
124   device_t *serdev[2], *piodev;
120   m_shortname = "ti99_rs232";
121}
125122
126   int    selected;
127   int      select_mask;
128   int      select_value;
129123
130   UINT8   signals[2]; // Latches the state of the output lines for UART0/UART1
131   int      recv_mode[2]; // May be NORMAL or ESC
124/**************************************************************************/
125/* Ports */
132126
133   // Baud rate management
134   // not part of the real device, but required for the connection to the
135   // real UART
136   double time_hold[2];
127ti_rs232_attached_device::ti_rs232_attached_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
128: device_t(mconfig, TI99_RS232_DEV, "Serial attached device", tag, owner, clock),
129  device_image_interface(mconfig, *this)
130{
131}
137132
138   // PIO flags
139   int pio_direction;      // a.k.a. PIOOC pio in output mode if 0
140   int pio_handshakeout;
141   int pio_handshakein;
142   int pio_spareout;
143   int pio_sparein;
144   int flag0;            // spare
145   int led;            // a.k.a. flag3
146   int pio_out_buffer;
147   int pio_in_buffer;
148   int pio_readable;
149   int pio_writable;
150   int pio_write;         // 1 if image is to be written to
133ti_pio_attached_device::ti_pio_attached_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
134: device_t(mconfig, TI99_PIO_DEV, "Parallel attached device", tag, owner, clock),
135  device_image_interface(mconfig, *this)
136{
137}
151138
152   UINT8   *rom;
153
154   /* Keeps the state of the SENILA line. */
155   UINT8   senila;
156
157   /* Keeps the value put on the bus when SENILA becomes active. */
158   UINT8   ila;
159
160   /* Callback lines to the main system. */
161   ti99_peb_connect   lines;
162
163} ti_rs232_state;
164
165#define ESC 0x1b
166
167INLINE ti_rs232_state *get_safe_token(device_t *device)
139void ti_rs232_attached_device::device_start()
168140{
169   assert(device != NULL);
170   assert(device->type() == TIRS232);
141}
171142
172   return (ti_rs232_state *)downcast<legacy_device_base *>(device)->token();
143void ti_pio_attached_device::device_start()
144{
173145}
174146
175static void incoming_dtr(device_t *device, line_state value);
176static void output_line_state(device_t *tms9902, int mask, UINT8 value);
177
178
179/**************************************************************************/
180/* Ports */
181
182static DEVICE_START( ti99_rs232_serdev )
147void ti_rs232_attached_device::device_config_complete()
183148{
149   update_names();
184150}
185151
186static DEVICE_START( ti99_piodev )
152void ti_pio_attached_device::device_config_complete()
187153{
154   update_names();
188155}
189156
190157/*
r14513r14514
192159    <name><number>, i.e. the number is the longest string from the right
193160    which can be interpreted as a number.
194161*/
195static int get_index_from_tagname(device_t *image)
162int ti_rs232_attached_device::get_index_from_tagname()
196163{
197   const char *tag = image->tag();
198   int maxlen = strlen(tag);
164   const char *mytag = tag();
165   int maxlen = strlen(mytag);
199166   int i;
200167   for (i=maxlen-1; i >=0; i--)
201      if (tag[i] < 48 || tag[i] > 57) break;
168      if (mytag[i] < 48 || mytag[i] > 57) break;
202169
203   return atoi(tag+i+1);
170   return atoi(mytag+i+1);
204171}
205172
206173/*
207174    Initialize rs232 unit and open image
208175*/
209static DEVICE_IMAGE_LOAD( ti99_rs232_serdev )
176bool ti_rs232_attached_device::call_load()
210177{
211   device_t *tms9902 = NULL;
178   tms9902_device* tms9902 = NULL;
179//  ti_rs232_pio_device* card = static_cast<ti_rs232_pio_device*>(owner());
212180
213   // TODO: strcmp does not work this way (wrong constant or wrong function)
214   int devnumber = get_index_from_tagname(&image.device());
181   int devnumber = get_index_from_tagname();
215182   if (devnumber==0)
216183   {
217      tms9902 = image.device().siblingdevice("tms9902_0");
184      tms9902 = siblingdevice<tms9902_device>("tms9902_0");
218185      // Turn on polling
219      tms9902_clock(tms9902, true);
186      tms9902->set_clock(true);
220187   }
221188   else if (devnumber==1)
222189   {
223      tms9902 = image.device().siblingdevice("tms9902_1");
190      tms9902 = siblingdevice<tms9902_device>("tms9902_1");
224191      // Turn on polling
225      tms9902_clock(tms9902, true);
192      tms9902->set_clock(true);
226193   }
227194   else
228195   {
229196      LOG("ti99/rs232: Could not find device tag number\n");
230      return IMAGE_INIT_FAIL;
197      return true;
231198   }
232199
233   incoming_dtr(tms9902, image? ASSERT_LINE : CLEAR_LINE);
200   // The following line may cause trouble in the init phase
201   // card->incoming_dtr(devnumber, (m_file!=NULL)? ASSERT_LINE : CLEAR_LINE);
234202
235   return IMAGE_INIT_PASS;
203   return false;  // OK
236204}
237205
238static DEVICE_IMAGE_UNLOAD( ti99_rs232_serdev )
206void ti_rs232_attached_device::call_unload()
239207{
240   device_t *tms9902 = NULL;
241   int devnumber = get_index_from_tagname(&image.device());
208   tms9902_device* tms9902 = NULL;
209
210   int devnumber = get_index_from_tagname();
242211   if (devnumber==0)
243212   {
244      tms9902 = image.device().siblingdevice("tms9902_0");
213      tms9902 = siblingdevice<tms9902_device>("tms9902_0");
245214      // Turn off polling
246      tms9902_clock(tms9902, false);
215      tms9902->set_clock(false);
247216   }
248217   else if (devnumber==1)
249218   {
250      tms9902 = image.device().siblingdevice("tms9902_1");
219      tms9902 = siblingdevice<tms9902_device>("tms9902_1");
251220      // Turn off polling
252      tms9902_clock(tms9902, false);
221      tms9902->set_clock(false);
253222   }
254223}
255224
256225/*
257226    Initialize pio unit and open image
258227*/
259static DEVICE_IMAGE_LOAD( ti99_piodev )
228bool ti_pio_attached_device::call_load()
260229{
261   device_t *carddev = image.device().owner();
262   ti_rs232_state *card = get_safe_token(carddev);
230   ti_rs232_pio_device* card = static_cast<ti_rs232_pio_device*>(owner());
263231
232   /* tell whether the image is readable */
233   card->m_pio_readable = !has_been_created();
264234   /* tell whether the image is writable */
265   card->pio_readable = !image.has_been_created();
266   /* tell whether the image is writable */
267   card->pio_writable = !image.is_readonly();
235   card->m_pio_writable = !is_readonly();
268236
269   if (card->pio_write && card->pio_writable)
270      card->pio_handshakein = 0;   /* receiver ready */
237   if (card->m_pio_write && card->m_pio_writable)
238      card->m_pio_handshakein = false;   /* receiver ready */
271239   else
272      card->pio_handshakein = 1;
240      card->m_pio_handshakein = true;
273241
274   return IMAGE_INIT_PASS;
242   return false;  // OK
275243}
276244
277245/*
278246    close a pio image
279247*/
280static DEVICE_IMAGE_UNLOAD( ti99_piodev )
248void ti_pio_attached_device::call_unload()
281249{
282   device_t *carddev = image.device().owner();
283   ti_rs232_state *card = get_safe_token(carddev);
250   ti_rs232_pio_device* card = static_cast<ti_rs232_pio_device*>(owner());
284251
285   card->pio_writable = 0;
286   card->pio_handshakein = 1;   /* receiver not ready */
287   card->pio_sparein = 0;
252   card->m_pio_writable = false;
253   card->m_pio_handshakein = true;   /* receiver not ready */
254   card->m_pio_sparein = false;
288255}
289256
290257/****************************************************************************/
291258
292259/*
293260    CRU read
294    TODO: Check read operations
295261*/
296static READ8Z_DEVICE_HANDLER( cru_rz )
262void ti_rs232_pio_device::crureadz(offs_t offset, UINT8 *value)
297263{
298   ti_rs232_state *card = get_safe_token(device);
299
300   if ((offset & 0xff00)==CRU_BASE)
264   if ((offset & 0xff00)==m_cru_base)
301265   {
302266      if ((offset & 0x00c0)==0x0000)
303267      {
304268         UINT8 reply = 0x00;
305         if (card->pio_direction)
306            reply |= 0x02;
307         if (card->pio_handshakein)
308            reply |= 0x04;
309         if (card->pio_sparein)
310            reply |= 0x08;
311         if (card->flag0)
312            reply |= 0x10;
269         if (m_pio_direction_in)       reply |= 0x02;
270         if (m_pio_handshakein)         reply |= 0x04;
271         if (m_pio_sparein)            reply |= 0x08;
272         if (m_flag0)               reply |= 0x10;
313273         // The CTS line is realized as CRU bits
314274         // Mind that this line is handled as an output going to the remote CTS
315         if (card->signals[0] & CTS)
316            reply |= 0x20;
317         if (card->signals[1] & CTS)
318            reply |= 0x40;
319         if (card->led)
320            reply |= 0x80;
275         if ((m_signals[0] & CTS)!=0)   reply |= 0x20;
276         if ((m_signals[1] & CTS)!=0)   reply |= 0x40;
277         if (m_led)                  reply |= 0x80;
321278         *value = reply;
322279         return;
323280      }
324281      if ((offset & 0x00c0)==0x0040)
325282      {
326         *value = tms9902_cru_r(card->uart[0], offset>>4);
283         *value = m_uart[0]->cruread(*m_space, offset>>4, 0xff);
327284         return;
328285      }
329286      if ((offset & 0x00c0)==0x0080)
330287      {
331         *value = tms9902_cru_r(card->uart[1], offset>>1);
288         *value = m_uart[1]->cruread(*m_space, offset>>1, 0xff);
332289         return;
333290      }
334291   }
r14513r14514
337294/*
338295    CRU write
339296*/
340static WRITE8_DEVICE_HANDLER( cru_w )
297void ti_rs232_pio_device::cruwrite(offs_t offset, UINT8 data)
341298{
342   ti_rs232_state *card = get_safe_token(device);
343
344   if ((offset & 0xff00)==CRU_BASE)
299   if ((offset & 0xff00)==m_cru_base)
345300   {
346301      if ((offset & 0x00c0)==0x0040)
347302      {
348         tms9902_cru_w(card->uart[0], offset>>1, data);
303         m_uart[0]->cruwrite(*m_space, offset>>1, data, 0xff);
349304         return;
350305      }
351306      if ((offset & 0x00c0)==0x0080)
352307      {
353         tms9902_cru_w(card->uart[1], offset>>1, data);
308         m_uart[1]->cruwrite(*m_space, offset>>1, data, 0xff);
354309         return;
355310      }
356311
357      device_image_interface *image = dynamic_cast<device_image_interface *>(card->piodev);
312      device_image_interface *image = dynamic_cast<device_image_interface *>(m_piodev);
313
358314      int bit = (offset & 0x00ff)>>1;
359315      switch (bit)
360316      {
361317      case 0:
362         card->selected = data;
318         m_selected = (data!=0);
363319         break;
364320
365321      case 1:
366         card->pio_direction = data;
322         m_pio_direction_in = (data!=0);
367323         break;
368324
369325      case 2:
370         if (data != card->pio_handshakeout)
326         if ((data!=0) != m_pio_handshakeout)
371327         {
372            card->pio_handshakeout = data;
373            if (card->pio_write && card->pio_writable && (!card->pio_direction))
328            m_pio_handshakeout = (data!=0);
329            if (m_pio_write && m_pio_writable && (!m_pio_direction_in))
374330            {   /* PIO in output mode */
375               if (!card->pio_handshakeout)
331               if (!m_pio_handshakeout)
376332               {   /* write data strobe */
377333                  /* write data and acknowledge */
378                  UINT8 buf = card->pio_out_buffer;
334                  UINT8 buf = m_pio_out_buffer;
379335                  int ret = image->fwrite(&buf, 1);
380336                  if (ret)
381                     card->pio_handshakein = 1;
337                     m_pio_handshakein = 1;
382338               }
383339               else
384340               {
385341                  /* end strobe */
386342                  /* we can write some data: set receiver ready */
387                  card->pio_handshakein = 0;
343                  m_pio_handshakein = 0;
388344               }
389345            }
390            if ((!card->pio_write) && card->pio_readable /*&& pio_direction*/)
346            if ((!m_pio_write) && m_pio_readable /*&& pio_direction_in*/)
391347            {   /* PIO in input mode */
392               if (!card->pio_handshakeout)
348               if (!m_pio_handshakeout)
393349               {   /* receiver ready */
394350                  /* send data and strobe */
395351                  UINT8 buf;
396352                  if (image->fread(&buf, 1))
397                     card->pio_in_buffer = buf;
398                  card->pio_handshakein = 0;
353                     m_pio_in_buffer = buf;
354                  m_pio_handshakein = 0;
399355               }
400356               else
401357               {
402358                  /* data acknowledge */
403359                  /* we can send some data: set transmitter ready */
404                  card->pio_handshakein = 1;
360                  m_pio_handshakein = 1;
405361               }
406362            }
407363         }
408364         break;
409365
410366      case 3:
411         card->pio_spareout = data;
367         m_pio_spareout = (data!=0);
412368         break;
413369
414370      case 4:
415         card->flag0 = data;
371         m_flag0 = (data!=0);
416372         break;
417373
418374      case 5:
419375         // Set the CTS line for RS232/1
420         LOG("TI-RS232/1: Setting CTS* via CRU to %d\n", data);
421         output_line_state(card->uart[0], CTS, (data==0)? CTS : 0);
376         if (VERBOSE>5) LOG("TI-RS232/1/3: Setting CTS* via CRU to %d\n", data);
377         output_line_state(0, CTS, (data==0)? CTS : 0);
422378         break;
423379
424380      case 6:
425381         // Set the CTS line for RS232/2
426         LOG("TI-RS232/2: Setting CTS* via CRU to %d\n", data);
427         output_line_state(card->uart[1], CTS, (data==0)? CTS : 0);
382         if (VERBOSE>5) LOG("TI-RS232/2/4: Setting CTS* via CRU to %d\n", data);
383         output_line_state(1, CTS, (data==0)? CTS : 0);
428384         break;
429385
430386      case 7:
431         card->led = data;
387         m_led = (data!=0);
432388         break;
433389      }
434390      return;
r14513r14514
438394/*
439395    Memory read
440396*/
441static READ8Z_DEVICE_HANDLER( data_rz )
397READ8Z_MEMBER( ti_rs232_pio_device::readz )
442398{
443   ti_rs232_state *card = get_safe_token(device);
444
445   if (card->senila)
399   if (m_senila==ASSERT_LINE)
446400   {
447401      if (VERBOSE>3) LOG("ti99/rs232: Sensing ILA\n");
448      *value = card->ila;
402      *value = m_ila;
449403      // The card ROM must be unselected, or we get two values
450404      // on the data bus
451405
452406      // Not sure whether this is correct; there is no software that makes
453407      // use of it
454      card->ila = 0;
408      m_ila = 0;
455409   }
456   if (((offset & card->select_mask)==card->select_value) && card->selected)
410   if (((offset & m_select_mask)==m_select_value) && m_selected)
457411   {
458412      if ((offset & 0x1000)==0x0000)
459413      {
460         *value = card->rom[offset&0x0fff];
414         *value = m_dsrrom[offset&0x0fff];
461415      }
462416      else
463417      {
464         *value = card->pio_direction ? card->pio_in_buffer : card->pio_out_buffer;
418         *value = m_pio_direction_in ? m_pio_in_buffer : m_pio_out_buffer;
465419      }
466420   }
467421}
r14513r14514
469423/*
470424    Memory write
471425*/
472static WRITE8_DEVICE_HANDLER( data_w )
426WRITE8_MEMBER( ti_rs232_pio_device::write )
473427{
474   ti_rs232_state *card = get_safe_token(device);
475
476   if (((offset & card->select_mask)==card->select_value) && card->selected)
428   if (((offset & m_select_mask)==m_select_value) && m_selected)
477429   {
478430      if ((offset & 0x1001)==0x1000)
479431      {
480         card->pio_out_buffer = data;
432         m_pio_out_buffer = data;
481433      }
482434   }
483435}
484436
485437/**************************************************************************/
486438
487/*
488    Propagates the /INT signal of the UARTs to the /INT line of the pbox.
489*/
490static TMS9902_INT_CALLBACK( int_callback )
491{
492   device_t *carddev = device->owner();
493   ti_rs232_state *card = get_safe_token(carddev);
494   int senila_bit = (device==card->uart[0])? SENILA_0_BIT : SENILA_1_BIT;
495439
496   if (state==FALSE)
497      card->ila |= senila_bit;
498   else
499      card->ila &= ~senila_bit;
500
501   card->lines.inta(state);
502}
503
504440// ==========================================================
505441
506442/*
507443    The DTR line of the interface card is wired to the CTS and DSR
508444    of the UART.
509445*/
510static void incoming_dtr(device_t *tms9902, line_state value)
446void ti_rs232_pio_device::incoming_dtr(int uartind, line_state value)
511447{
512   ti_rs232_state *card = get_safe_token(tms9902->owner());
513   int uartind = (tms9902==card->uart[0])? 0 : 1;
514
515448   if (VERBOSE>2) LOG("TI-RS232/%d: incoming DTR = %d\n", uartind+1, (value==ASSERT_LINE)? 1:0);
516449
517   tms9902_rcv_cts(tms9902, value);
518   tms9902_rcv_dsr(tms9902, value);
450   m_uart[uartind]->rcv_cts(value);
451   m_uart[uartind]->rcv_dsr(value);
519452}
520453
521454/*
522455    Data transmission
523456*/
524static void transmit_data(device_t *tms9902, UINT8 value)
457void ti_rs232_pio_device::transmit_data(int uartind, UINT8 value)
525458{
526459   UINT8 buf = value;
527   ti_rs232_state *card = get_safe_token(tms9902->owner());
528   int uartind = (tms9902==card->uart[0])? 0 : 1;
529460
530461   device_image_interface *serial;
531   serial = dynamic_cast<device_image_interface *>(card->serdev[uartind]);
462   serial = dynamic_cast<device_image_interface *>(m_serdev[uartind]);
532463   if (!serial->exists())
533464   {
534465      if (VERBOSE>1) LOG("TI-RS232/%d: No serial output attached\n", uartind+1);
r14513r14514
583514      +12V 6 -|       |-( 6) <--- DSR
584515        nc 4 -----------( 8) <--- DCD
585516*/
586static UINT8 map_lines_out(device_t *carddev, int uartind, UINT8 value)
517UINT8 ti_rs232_pio_device::map_lines_out(int uartind, UINT8 value)
587518{
588519   UINT8 ret = 0;
589   int mapping = input_port_read(carddev->machine(), "SERIALMAP");
520   int mapping = input_port_read(*this, "SERIALMAP");
590521
591522   //    00ab cdef = setting line RTS=a, CTS=b, DSR=c, DCD=d, DTR=e, RI=f
592523
r14513r14514
655586   return ret;
656587}
657588
658static UINT8 map_lines_in(device_t *carddev, int uartind, UINT8 value)
589UINT8 ti_rs232_pio_device::map_lines_in(int uartind, UINT8 value)
659590{
660591   UINT8 ret = 0;
661   int mapping = input_port_read(carddev->machine(), "SERIALMAP");
592   int mapping = input_port_read(*this, "SERIALMAP");
662593
663594   //    00ab cdef = setting line RTS=a, CTS=b, DSR=c, DCD=d, DTR=e, RI=f
664595
r14513r14514
750681    serial bridge must stop delivering data bytes until the handshake opens the
751682    channel again.
752683*/
753static void receive_data_or_line_state(device_t *tms9902, double baudpoll)
684void ti_rs232_pio_device::receive_data_or_line_state(int uartind)
754685{
755   ti_rs232_state *card = get_safe_token(tms9902->owner());
756686   device_image_interface *serial;
757687   UINT8 buffer;
758   int uartind = (tms9902==card->uart[0])? 0 : 1;
759688   int len = 0;
760689
761   serial = dynamic_cast<device_image_interface *>(card->serdev[uartind]);
690   serial = dynamic_cast<device_image_interface *>(m_serdev[uartind]);
762691
763692   if (!serial->exists())
764693   {
r14513r14514
766695      return;
767696   }
768697
698   double baudpoll = m_uart[uartind]->get_baudpoll();
699
769700   // If more than the minimum waiting time since the last data byte has
770701   // elapsed, we can get a new value.
771   if (card->time_hold[uartind] > 1.0)
702   if (m_time_hold[uartind] > 1.0)
772703   {
773704      len = serial->fread(&buffer, 1);
774705      if (len==0) return;
r14513r14514
776707   else
777708   {
778709      // number of polls has not yet elapsed; we have to wait.
779      card->time_hold[uartind] += baudpoll;
710      m_time_hold[uartind] += baudpoll;
780711      return;
781712   }
782713
783714   // No config parameters here, only data or line setting
784   switch (card->recv_mode[uartind])
715   switch (m_recv_mode[uartind])
785716   {
786717   case RECV_MODE_NORMAL:
787718      if (buffer==0x1b)
788719      {
789720         if (VERBOSE>2) LOG("TI-RS232/%d: received: %c <%02x>, switch to ESC mode\n", uartind+1, buffer, buffer);
790         card->recv_mode[uartind] = RECV_MODE_ESC;
721         m_recv_mode[uartind] = RECV_MODE_ESC;
791722      }
792723      else
793724      {
794725         if (VERBOSE>3) LOG("TI-RS232/%d: received: %c <%02x>, pass to UART\n", uartind+1, buffer, buffer);
795         tms9902_rcv_data(tms9902, buffer);
796         card->time_hold[uartind] = 0.0;
726         m_uart[uartind]->rcv_data(buffer);
727         m_time_hold[uartind] = 0.0;
797728      }
798729      break;
799730   case RECV_MODE_ESC:
800731      if (buffer==0x1b)
801732      {
802         card->recv_mode[uartind] = RECV_MODE_NORMAL;
733         m_recv_mode[uartind] = RECV_MODE_NORMAL;
803734         if (VERBOSE>2) LOG("TI-RS232/%d: leaving ESC mode, received: %c <%02x>, pass to UART\n", uartind+1, buffer, buffer);
804         tms9902_rcv_data(tms9902, buffer);
805         card->time_hold[uartind] = 0.0;
735         m_uart[uartind]->rcv_data(buffer);
736         m_time_hold[uartind] = 0.0;
806737      }
807738      else
808739      {
r14513r14514
811742         if (buffer != 1)
812743         {
813744            LOG("TI-RS232/%d: expected length 1 but got %02x, leaving ESC mode.\n", uartind+1, buffer);
814            card->recv_mode[uartind] = RECV_MODE_NORMAL;
745            m_recv_mode[uartind] = RECV_MODE_NORMAL;
815746         }
816747         else
817            card->recv_mode[uartind] = RECV_MODE_ESC_LINES;
748            m_recv_mode[uartind] = RECV_MODE_ESC_LINES;
818749      }
819750      break;
820751
r14513r14514
824755      if (buffer & EXCEPT)
825756      {
826757         // Exception states: BRK, FRMERR, PARERR
827         tms9902_rcv_break(tms9902, ((buffer & BRK)!=0));
758         m_uart[uartind]->rcv_break(((buffer & BRK)!=0));
828759
829         if (buffer & FRMERR)
830            tms9902_rcv_framing_error(tms9902);
831         if (buffer & PARERR)
832            tms9902_rcv_parity_error(tms9902);
760         if (buffer & FRMERR)   m_uart[uartind]->rcv_framing_error();
761         if (buffer & PARERR)   m_uart[uartind]->rcv_parity_error();
833762      }
834763      else
835764      {
836         buffer = map_lines_in(tms9902->owner(), uartind, buffer);
765         buffer = map_lines_in(uartind, buffer);
837766         if (VERBOSE>2) LOG("TI-RS232/%d: received (remapped) <%02x> in ESC mode\n", uartind+1, buffer);
838767
839768         // The DTR line on the RS232 connector of the board is wired to both the
840769         // CTS and the DSR pin of the TMS9902
841770         // Apart from the data line, DTR is the only input line
842         incoming_dtr(tms9902,  (buffer & DTR)? ASSERT_LINE : CLEAR_LINE);
771         incoming_dtr(uartind,  (buffer & DTR)? ASSERT_LINE : CLEAR_LINE);
843772      }
844773
845      card->recv_mode[uartind] = RECV_MODE_NORMAL;
774      m_recv_mode[uartind] = RECV_MODE_NORMAL;
846775      break;
847776
848777   default:
849      LOG("TI-RS232/%d: unknown mode: %d\n", uartind+1, card->recv_mode[uartind]);
778      if (VERBOSE>1) LOG("TI-RS232/%d: unknown mode: %d\n", uartind+1, m_recv_mode[uartind]);
850779   }
851780}
852781
853static TMS9902_RCV_CALLBACK( rcv_callback )
854{
855   // Called from the UART when it wants to receive a character
856   // However, characters are not passed to it at this point
857   // Instead, we check for signal line change or data transmission
858   // and call the respective function
859   receive_data_or_line_state(device, baudpoll);
860}
861
862static TMS9902_XMIT_CALLBACK( xmit_callback )
863{
864   transmit_data(device, (UINT8)data);
865}
866
867782/*
868783    Control operations like configuration or line changes
869784*/
870static void configure_interface(device_t *tms9902, int type, int value)
785void ti_rs232_pio_device::configure_interface(int uartind, int type, int value)
871786{
872   ti_rs232_state *card = get_safe_token(tms9902->owner());
873787   UINT8 bufctrl[4];
874788   device_image_interface *serial;
875   int uartind = (tms9902==card->uart[0])? 0 : 1;
876789   UINT8 esc = ESC;
877790
878   serial = dynamic_cast<device_image_interface *>(card->serdev[uartind]);
791   serial = dynamic_cast<device_image_interface *>(m_serdev[uartind]);
879792
880793   if (!serial->exists())
881794   {
r14513r14514
928841   serial->fwrite(bufctrl, bufctrl[0]+1);
929842}
930843
931static void set_bit(device_t *device, int uartind, int line, int value)
844void ti_rs232_pio_device::set_bit(int uartind, int line, int value)
932845{
933   ti_rs232_state *card = get_safe_token(device);
934846   if (VERBOSE>5)
935847   {
936848      switch (line)
937849      {
938      case CTS: LOG("TI-RS232/%d: set CTS(out)=%s\n", uartind+1, (value)? "asserted" : "cleared"); break;
939      case DCD: LOG("TI-RS232/%d: set DCD(out)=%s\n", uartind+1, (value)? "asserted" : "cleared"); break;
940      case BRK: LOG("TI-RS232/%d: set BRK(out)=%s\n", uartind+1, (value)? "asserted" : "cleared"); break;
850      case CTS: LOG("TI-RS232/%d: set CTS(out)=%s\n", uartind+1, (value!=0)? "asserted" : "cleared"); break;
851      case DCD: LOG("TI-RS232/%d: set DCD(out)=%s\n", uartind+1, (value!=0)? "asserted" : "cleared"); break;
852      case BRK: LOG("TI-RS232/%d: set BRK(out)=%s\n", uartind+1, (value!=0)? "asserted" : "cleared"); break;
941853      }
942854   }
943855
944   if (value)
945      card->signals[uartind] |= line;
946   else
947      card->signals[uartind] &= ~line;
856   if (value!=0)   m_signals[uartind] |= line;
857   else         m_signals[uartind] &= ~line;
948858}
949859
950860/*
951861   Line changes
952862*/
953static void output_exception(device_t *tms9902, int param, UINT8 value)
863void ti_rs232_pio_device::output_exception(int uartind, int param, UINT8 value)
954864{
955   ti_rs232_state *card = get_safe_token(tms9902->owner());
956865   device_image_interface *serial;
957866   UINT8 bufctrl[2];
958867   UINT8 esc = ESC;
959868
960   int uartind = (tms9902==card->uart[0])? 0 : 1;
869   serial = dynamic_cast<device_image_interface *>(m_serdev[uartind]);
961870
962   serial = dynamic_cast<device_image_interface *>(card->serdev[uartind]);
963
964871   if (!serial->exists())
965872   {
966873      if (VERBOSE>1) LOG("TI-RS232/%d: No serial output attached\n", uartind+1);
r14513r14514
979886/*
980887   Line changes
981888*/
982static void output_line_state(device_t *tms9902, int mask, UINT8 value)
889void ti_rs232_pio_device::output_line_state(int uartind, int mask, UINT8 value)
983890{
984   ti_rs232_state *card = get_safe_token(tms9902->owner());
985891   device_image_interface *serial;
986892   UINT8 bufctrl[2];
987893   UINT8 esc = ESC;
988894
989   int uartind = (tms9902==card->uart[0])? 0 : 1;
895   serial = dynamic_cast<device_image_interface *>(m_serdev[uartind]);
990896
991   serial = dynamic_cast<device_image_interface *>(card->serdev[uartind]);
992
993897   if (!serial->exists())
994898   {
995899      if (VERBOSE>1) LOG("TI-RS232/%d: No serial output attached\n", uartind+1);
r14513r14514
1002906   bufctrl[0] = 1;
1003907
1004908   // The CTS line (coming from a CRU bit) is connected to the CTS pin
1005   if (mask & CTS) set_bit(tms9902->owner(), uartind, CTS, value & CTS);
909   if (mask & CTS) set_bit(uartind, CTS, value & CTS);
1006910
1007911   // The RTS line (from 9902) is connected to the DCD pin
1008   if (mask & RTS) set_bit(tms9902->owner(), uartind, DCD, value & RTS);
912   if (mask & RTS) set_bit(uartind, DCD, value & RTS);
1009913
1010914   // The DSR pin is hardwired to +5V
1011   set_bit(tms9902->owner(), uartind, DSR, 1);
915   set_bit(uartind, DSR, 1);
1012916
1013917   // As of here, the lines are set according to the schematics of the
1014918   // serial interface.
r14513r14514
1016920   // Now translate the signals of the board to those of a DTE-like device
1017921   // so that we can pass the signal to the real PC serial interface
1018922   // (can be imagined as if we emulated the cable)
1019   bufctrl[1] = map_lines_out(tms9902->owner(), uartind, card->signals[uartind]);
923   bufctrl[1] = map_lines_out(uartind, m_signals[uartind]);
1020924   serial->fwrite(bufctrl, 2);
1021925}
1022926
1023static TMS9902_CTRL_CALLBACK( ctrl_callback )
927/***********************************************************************
928    callbacks
929************************************************************************/
930/*
931    Propagates the /INT signal of the UARTs to the /INT line of the pbox.
932*/
933WRITE_LINE_MEMBER( ti_rs232_pio_device::int0_callback )
1024934{
1025   switch (type)
1026   {
1027   case CONFIG:
1028      configure_interface(device, param, value); break;
1029   case EXCEPT:
1030      output_exception(device, param, value); break;
1031   default:
1032      output_line_state(device, param, value);
1033   }
1034}
935   int senila_bit = SENILA_0_BIT;
1035936
937   if (state==ASSERT_LINE)   m_ila |= senila_bit;
938   else m_ila &= ~senila_bit;
1036939
1037static WRITE_LINE_DEVICE_HANDLER( senila )
1038{
1039   // put the value on the data bus. We store it in a state variable.
1040   ti_rs232_state *card = get_safe_token(device);
1041   if (VERBOSE>3) LOG("TI-RS232/1-2: Setting SENILA=%d\n", state);
1042   card->senila = state;
940   m_slot->set_inta(state);
1043941}
1044942
1045static const tms9902_interface tms9902_params =
943WRITE_LINE_MEMBER( ti_rs232_pio_device::int1_callback )
1046944{
1047   3000000.,            /* clock rate (3MHz) */
1048   int_callback,         /* called when interrupt pin state changes */
1049   rcv_callback,         /* called when a character is received */
1050   xmit_callback,         /* called when a character is transmitted */
1051   ctrl_callback
1052};
945   int senila_bit = SENILA_1_BIT;
1053946
1054static const ti99_peb_card tirs232_card =
1055{
1056   data_rz, data_w,            // memory access read/write
1057   cru_rz, cru_w,               // CRU access
1058   senila, NULL,               // SENILA/B access
1059   NULL, NULL                  // 16 bit access (none here)
1060};
947   if (state==ASSERT_LINE)   m_ila |= senila_bit;
948   else m_ila &= ~senila_bit;
1061949
950   m_slot->set_inta(state);
951}
952
1062953/*
1063    Defines the serial serdev.
954    Called from the UART when it wants to receive a character
955    However, characters are not passed to it at this point
956    Instead, we check for signal line change or data transmission
957    and call the respective function
1064958*/
1065DEVICE_GET_INFO( ti99_rs232_serdev )
959WRITE_LINE_MEMBER( ti_rs232_pio_device::rcv0_callback )
1066960{
1067   switch ( state )
1068   {
1069      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = 0;                        break;
1070      case DEVINFO_INT_IMAGE_TYPE:               info->i = IO_SERIAL;                                      break;
1071      case DEVINFO_INT_IMAGE_READABLE:            info->i = 1;                                               break;
1072      case DEVINFO_INT_IMAGE_WRITEABLE:         info->i = 1;                                               break;
1073      case DEVINFO_INT_IMAGE_CREATABLE:          info->i = 1;                                               break;
961   receive_data_or_line_state(0);
962}
1074963
1075      case DEVINFO_FCT_START:                      info->start = DEVICE_START_NAME( ti99_rs232_serdev );              break;
1076//      case DEVINFO_FCT_RESET:                     info->reset = DEVICE_RESET_NAME( ti99_rs232_serdev );           break;
1077      case DEVINFO_FCT_IMAGE_LOAD:              info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( ti99_rs232_serdev );    break;
1078      case DEVINFO_FCT_IMAGE_UNLOAD:              info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME( ti99_rs232_serdev );    break;
1079      case DEVINFO_STR_NAME:                      strcpy( info->s, "TI99 RS232 attached serial device");                            break;
1080      case DEVINFO_STR_FAMILY:                    strcpy(info->s, "Peripheral expansion");                            break;
1081      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:       strcpy(info->s, "");                                           break;
1082      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                              break;
1083      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                           break;
1084      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");       break;
1085   }
964WRITE_LINE_MEMBER( ti_rs232_pio_device::rcv1_callback )
965{
966   receive_data_or_line_state(1);
1086967}
1087968
1088/*
1089    Defines the PIO (parallel IO) serdev
1090*/
1091DEVICE_GET_INFO( ti99_piodev )
969WRITE8_MEMBER( ti_rs232_pio_device::xmit0_callback )
1092970{
1093   switch ( state )
1094   {
1095      case DEVINFO_INT_INLINE_CONFIG_BYTES:      info->i = 0;                        break;
1096      case DEVINFO_INT_IMAGE_TYPE:               info->i = IO_PARALLEL;                                      break;
1097      case DEVINFO_INT_IMAGE_READABLE:            info->i = 1;                                               break;
1098      case DEVINFO_INT_IMAGE_WRITEABLE:         info->i = 1;                                               break;
1099      case DEVINFO_INT_IMAGE_CREATABLE:          info->i = 1;                                               break;
971   transmit_data(0, data);
972}
1100973
1101      case DEVINFO_FCT_START:                      info->start = DEVICE_START_NAME( ti99_piodev );              break;
1102      case DEVINFO_FCT_IMAGE_LOAD:              info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( ti99_piodev );    break;
1103      case DEVINFO_FCT_IMAGE_UNLOAD:              info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(ti99_piodev );  break;
1104      case DEVINFO_STR_NAME:                      strcpy( info->s, "TI99 PIO attached device");                            break;
1105      case DEVINFO_STR_FAMILY:                    strcpy(info->s, "Peripheral expansion");                            break;
1106      case DEVINFO_STR_IMAGE_FILE_EXTENSIONS:       strcpy(info->s, "");                                           break;
1107      case DEVINFO_STR_VERSION:                  strcpy(info->s, "1.0");                              break;
1108      case DEVINFO_STR_SOURCE_FILE:               strcpy(info->s, __FILE__);                           break;
1109      case DEVINFO_STR_CREDITS:                  strcpy(info->s, "Copyright the MESS Team");       break;
974WRITE8_MEMBER( ti_rs232_pio_device::xmit1_callback )
975{
976   transmit_data(1, data);
977}
978
979void ti_rs232_pio_device::ctrl_callback(int uartind, int offset, UINT8 data)
980{
981   if ((offset & CONFIG)!=0)
982   {
983      // We cannot pass the configuration data as they need more than 8 bits.
984      // Could be done by a write16 function as well.
985      configure_interface(uartind, data, m_uart[uartind]->get_config_value());
1110986   }
987   else
988   {
989      if ((offset & EXCEPT)!=0)
990      {
991         output_exception(uartind, offset & ~EXCEPT, data);
992      }
993      else
994      {
995         output_line_state(uartind, offset, data);
996      }
997   }
1111998}
1112999
1113DECLARE_LEGACY_IMAGE_DEVICE(TI99_RS232, ti99_rs232_serdev);
1114DEFINE_LEGACY_IMAGE_DEVICE(TI99_RS232, ti99_rs232_serdev);
1115DECLARE_LEGACY_IMAGE_DEVICE(TI99_PIO, ti99_piodev);
1116DEFINE_LEGACY_IMAGE_DEVICE(TI99_PIO, ti99_piodev);
1117
1118static DEVICE_START( ti_rs232 )
1000WRITE8_MEMBER( ti_rs232_pio_device::ctrl0_callback )
11191001{
1120   ti_rs232_state *card = get_safe_token(device);
1121   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
1002   ctrl_callback(0, offset, data);
1003}
11221004
1123   astring region(device->tag(), ":", ser_region);
1124   card->rom = device->machine().region(region.cstr())->base();
1125   card->lines.inta.resolve(topeb->inta, *device);
1126   // READY and INTB are not used
1127   card->uart[0] = device->subdevice("tms9902_0");
1128   card->uart[1] = device->subdevice("tms9902_1");
1129   card->serdev[0] = device->subdevice("serdev0");
1130   card->serdev[1] = device->subdevice("serdev1");
1131   card->piodev = device->subdevice("piodev");
1005WRITE8_MEMBER( ti_rs232_pio_device::ctrl1_callback )
1006{
1007   ctrl_callback(1, offset, data);
11321008}
11331009
1134static DEVICE_STOP( ti_rs232 )
1010static const tms9902_interface tms9902_params0 =
11351011{
1012   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, int0_callback),         /* called when interrupt pin state changes */
1013   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, rcv0_callback),         /* called when a character is received */
1014   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, xmit0_callback),         /* called when a character is transmitted */
1015   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, ctrl0_callback)
1016};
1017
1018static const tms9902_interface tms9902_params1 =
1019{
1020   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, int1_callback),         /* called when interrupt pin state changes */
1021   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, rcv1_callback),         /* called when a character is received */
1022   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, xmit1_callback),         /* called when a character is transmitted */
1023   DEVCB_DEVICE_MEMBER(DEVICE_SELF_OWNER, ti_rs232_pio_device, ctrl1_callback)
1024};
1025
1026void ti_rs232_pio_device::device_start()
1027{
1028   m_dsrrom = subregion(DSRROM)->base();
1029   m_uart[0] = subdevice<tms9902_device>("tms9902_0");
1030   m_uart[1] = subdevice<tms9902_device>("tms9902_1");
1031   m_serdev[0] = subdevice<ti_rs232_attached_device>("serdev0");
1032   m_serdev[1] = subdevice<ti_rs232_attached_device>("serdev1");
1033   m_piodev = subdevice<ti_pio_attached_device>("piodev");
11361034}
11371035
1138static DEVICE_RESET( ti_rs232 )
1036void ti_rs232_pio_device::device_reset()
11391037{
1140   ti_rs232_state *card = get_safe_token(device);
1141   /* Register the card */
1142   device_t *peb = device->owner();
1038   m_pio_direction_in = false;
1039   m_pio_handshakeout = false;
1040   m_pio_spareout = false;
1041   m_flag0 = false;
11431042
1144   if (input_port_read(device->machine(), "SERIAL")==SERIAL_TI)
1145   {
1146      int success = mount_card(peb, device, &tirs232_card, get_pebcard_config(device)->slot);
1147      if (!success) return;
1043   set_bit(0, CTS, 0);
1044   set_bit(1, CTS, 0);
11481045
1149      card->pio_direction = 0;
1150      card->pio_handshakeout = 0;
1151      card->pio_spareout = 0;
1152      card->flag0 = 0;
1046   m_led = false;
1047   m_pio_write = true;
1048   m_recv_mode[0] = RECV_MODE_NORMAL;
1049   m_recv_mode[1] = RECV_MODE_NORMAL;
11531050
1154      set_bit(device, 0, CTS, 0);
1155      set_bit(device, 1, CTS, 0);
1051   if (m_genmod)
1052   {
1053      m_select_mask = 0x1fe000;
1054      m_select_value = 0x174000;
1055   }
1056   else
1057   {
1058      m_select_mask = 0x7e000;
1059      m_select_value = 0x74000;
1060   }
11561061
1157      card->led = 0;
1158      card->pio_write = 1;
1159      card->recv_mode[0] = RECV_MODE_NORMAL;
1160      card->recv_mode[1] = RECV_MODE_NORMAL;
1062   m_space = machine().device("maincpu")->memory().space(AS_IO);
1063   m_selected = false;
11611064
1162      card->select_mask = 0x7e000;
1163      card->select_value = 0x74000;
1065   m_cru_base = (input_port_read(*this, "CRURS232")==0)? 0x1300 : 0x1500;
11641066
1165      card->time_hold[0] = card->time_hold[1] = 0.0;
1067   m_time_hold[0] = m_time_hold[1] = 0.0;
11661068
11671069      // The GenMod modification changes the address bus width of the Geneve.
11681070      // All peripheral cards need to be manually modified to properly decode
11691071      // the wider address. The next lines perform this soldering job
11701072      // automagically.
1171      if (input_port_read(device->machine(), "MODE")==GENMOD)
1172      {
1173         // GenMod card modification
1174         card->select_mask = 0x1fe000;
1175         card->select_value = 0x174000;
1176      }
1177   }
1073      /* if (input_port_read(device->machine(), "MODE")==GENMOD)
1074        {
1075            // GenMod card modification
1076            card->select_mask = 0x1fe000;
1077            card->select_value = 0x174000;
1078        }*/
11781079}
11791080
11801081static MACHINE_CONFIG_FRAGMENT( ti_rs232 )
1181   MCFG_TMS9902_ADD("tms9902_0", tms9902_params)
1182   MCFG_TMS9902_ADD("tms9902_1", tms9902_params)
1183   MCFG_DEVICE_ADD("serdev0", TI99_RS232, 0)
1184   MCFG_DEVICE_ADD("serdev1", TI99_RS232, 0)
1185   MCFG_DEVICE_ADD("piodev", TI99_PIO, 0)
1082   MCFG_TMS9902_ADD("tms9902_0", tms9902_params0, 3000000)
1083   MCFG_TMS9902_ADD("tms9902_1", tms9902_params1, 3000000)
1084   MCFG_DEVICE_ADD("serdev0", TI99_RS232_DEV, 0)
1085   MCFG_DEVICE_ADD("serdev1", TI99_RS232_DEV, 0)
1086   MCFG_DEVICE_ADD("piodev", TI99_PIO_DEV, 0)
11861087MACHINE_CONFIG_END
11871088
11881089ROM_START( ti_rs232 )
1189   ROM_REGION(0x1000, ser_region, 0)
1190   ROM_LOAD_OPTIONAL("rs232.bin", 0x0000, 0x1000, CRC(eab382fb) SHA1(ee609a18a21f1a3ddab334e8798d5f2a0fcefa91)) /* TI rs232 DSR ROM */
1090   ROM_REGION(0x1000, DSRROM, 0)
1091   ROM_LOAD("rs232.bin", 0x0000, 0x1000, CRC(eab382fb) SHA1(ee609a18a21f1a3ddab334e8798d5f2a0fcefa91)) /* TI rs232 DSR ROM */
11911092ROM_END
11921093
1193static const char DEVTEMPLATE_SOURCE[] = __FILE__;
1094INPUT_PORTS_START( ti_rs232 )
1095   PORT_START(   "CRURS232"   )
1096   PORT_DIPNAME( 0x01, 0x00, "TI-RS232 CRU base" )
1097      PORT_DIPSETTING(   0x00, "1300" )
1098      PORT_DIPSETTING(   0x00, "1500" )
11941099
1195#define DEVTEMPLATE_ID(p,s)             p##ti_rs232##s
1196#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_ROM_REGION | DT_HAS_MACHINE_CONFIG
1197#define DEVTEMPLATE_NAME                "TI99 RS232/PIO interface card"
1198#define DEVTEMPLATE_SHORTNAME           "ti99rs232"
1199#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
1200#include "devtempl.h"
1100   PORT_START( "SERIALMAP" )
1101   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" )
1102      PORT_CONFSETTING(    0x00, "6-20" )
1103      PORT_CONFSETTING(    0x01, "8-20" )
1104      PORT_CONFSETTING(    0x02, "5-20" )
1105INPUT_PORTS_END
12011106
1202DEFINE_LEGACY_DEVICE( TIRS232, ti_rs232 );
1107machine_config_constructor ti_rs232_pio_device::device_mconfig_additions() const
1108{
1109   return MACHINE_CONFIG_NAME( ti_rs232 );
1110}
12031111
1112const rom_entry *ti_rs232_pio_device::device_rom_region() const
1113{
1114   return ROM_NAME( ti_rs232 );
1115}
1116
1117ioport_constructor ti_rs232_pio_device::device_input_ports() const
1118{
1119   return INPUT_PORTS_NAME(ti_rs232);
1120}
1121
1122const device_type TI99_RS232 = &device_creator<ti_rs232_pio_device>;
1123const device_type TI99_RS232_DEV = &device_creator<ti_rs232_attached_device>;
1124const device_type TI99_PIO_DEV = &device_creator<ti_pio_attached_device>;
1125
src/mess/machine/ti99/tn_ide.h
r14513r14514
1/*
1/****************************************************************************
2
23    Nouspikel IDE controller card
3    Michael Zapf, September 2010
4*/
4    See tn_ide.c for documentation
55
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
612#ifndef __TNIDE__
713#define __TNIDE__
814
9DECLARE_LEGACY_DEVICE( TNIDE, tn_ide );
15#include "emu.h"
16#include "ti99defs.h"
17#include "machine/rtc65271.h"
18
19extern const device_type TI99_IDE;
20
21class nouspikel_ide_interface_device : public ti_expansion_card_device
22{
23public:
24   nouspikel_ide_interface_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   DECLARE_READ8Z_MEMBER(readz);
26   DECLARE_WRITE8_MEMBER(write);
27
28   void   crureadz(offs_t offset, UINT8 *value);
29   void   cruwrite(offs_t offset, UINT8 value);
30
31   void   do_inta(int state);
32   bool   m_ide_irq;
33   int    m_cru_register;
34
35   DECLARE_WRITE_LINE_MEMBER(clock_interrupt_callback);
36
37protected:
38   void device_start(void);
39   void device_reset(void);
40   const rom_entry *device_rom_region() const;
41   machine_config_constructor device_mconfig_additions() const;
42   ioport_constructor device_input_ports() const;
43
44private:
45   rtc65271_device*   m_rtc;
46   device_t*         m_ide;
47
48   bool   m_clk_irq;
49   bool   m_sram_enable;
50   bool   m_sram_enable_dip;
51   int      m_cur_page;
52
53   bool   m_tms9995_mode;
54
55   UINT16   m_input_latch;
56   UINT16   m_output_latch;
57
58   UINT8   *m_ram;
59};
60
1061#endif
src/mess/machine/ti99/peribox.c
r14513r14514
1/*
2    Peripheral expansion bus support.
1/****************************************************************************
32
3    Peripheral expansion system
4
45    The ti-99/4, ti-99/4a, ti computer 99/8, myarc geneve, and snug sgcpu
56    99/4p systems all feature a bus connector that enables the connection of
67    extension cards.  (Although the hexbus is the preferred bus to add
r14513r14514
1112    While a few extension cards connect to the side bus connector of the
1213    ti-99/4(a) console directly, most extension cards were designed to be
1314    inserted in a PEB instead.  The PEB (Peripheral Expansion Box) is a big box
14    with an alimentation, a few bus drivers, and several card slots, that
15    with power supply, a few bus drivers, and several card slots, that
1516    connects to the ti-99/4(a) side port.  The reason for using a PEB is that
1617    daisy-chaining many modules caused the system to be unreliable due to the
1718    noise produced by the successive contacts.  (As a matter of fact, TI
r14513r14514
2122    the introduction of the PEB, probably because TI wanted the speech
2223    synthesizer to be a cheap extension, and the PEB was not cheap.)
2324
24    June 2010: Reimplemented using device structure (MZ) (obsoletes 99_peb.c)
2525
26Slots:
26    =================##  connection cable to console
27                     ||
28                     ||
29    +--------------+----+----+----+----+----+----+----+----+------------+
30    |              | S  | S  | S  | S  | S  | S  | S  | S  :            |
31    |              | L  | L  | L  | L  | L  | L  | L  | L  :            |
32    |  Power       | O  | O  | O  | O  | O  | O  | O  | O  : Floppy     |
33    |  unit        | T  | T  | T  | T  | T  | T  | T  | T  | drive      |
34    |  and         |    |    |    |    |    |    |    |    | compartment|
35    |  ventilation | 1  | 2  | 3  | 4  | 5  | 6  | 7  | 8  |            |
36    |              |    |    |    |    |    |    |    |    | 1 full hgt |
37    |              |    |    |    |    |    |    |    |    | or         |
38    |              |    |    |    |    |    |    |    |    | 2 "slim"   |
39    |              |    |    |    |    |    |    |LED |LED |            |
40    |              +----+----+----+----+----+----+-|--+-|--+            |
41    |              :     seen from above           |    |  |            |
42    +--------------+-------------------------------O----O---------------+
2743
44    All slots are connected in parallel with all signal lines. The cards
45    must be equipped with bus drivers (244/245) and are usually activated
46    by turning a CRU bit on (except for the memory expansions which are
47    always active).
48
49    This is emulation in exactly the same way. The data coming from the
50    console is propagated to all slots, and each card decides whether to
51    react or not. Similarly, for read operations, all cards are checked,
52    and only the active cards actually put a value on the data bus
53    (we do this using the bus8z_device; see ti99defs.h).
54
55    Slot 1 is usually reserved for the "Flex cable interface" when connecting
56    a TI-99/4(A)/8 console. Also, the Geneve is put into slot 1. We therefore
57    do not offer peb:slot1. Slot 8 is usually used for floppy controllers
58    as there is a passthrough for cables.
59
60
61    Slots:
62
2863          REAR
2964     +8V  1||2   +8V
3065     GND  3||4   READY
r14513r14514
6196        < from box to console
6297        > from console into box
6398
64READYA  <    System ready (goes to READY, 10K pull-up to +5V) A low level puts the cpu on hold.
65RESET*  >    System reset (active low)
66SCLK    nc   System clock (not connected in interface card)
67LCP*    nc   CPU indicator 1=TI99 0=2nd generation (not connected in interface card)
68BOOTPG* nc   ?
69AUDIO   <    Input audio (to AUDIOIN in console)
70RDBENA* <    Active low: enable flex cable data bus drivers (1K pull-up)
71PCBEN   H    PCB enable for burn-in (always High)
72HOLD*   H    Active low CPU hold request (always High)
73IAQHDA  nc   IAQ [or] HOLDA (logical or)
74SENILA* H(>) Interrupt level A sense enable (always High)
75SENILB* H(>) Interrupt level B sense enable (always High)
76INTA*   <    Interrupt level A (active low, goes to EXTINT*)
77INTB*   nc   Interrupt level B (not used)
78LOAD*   nc   Unmaskable interrupt (not carried by interface cable/card)
79D0-D7   <>   Data bus (D0 most significant)
80A0-A15  >    Address bus (A0 most sig; A15 also used as CRUOUT)
81AMA/B/C H    Extra address bits (always high for TI-99/4x)
82CLKOUT* >    Inverted PHI3 clock, from TIM9904 clock generator
83CRUCLK* >    Inverted CRU clock, from TMS9900 CRUCLK pin
84DBIN    >    Active high = read memory. Drives the data bus buffers.
85WE*     >    Write Enable pulse (derived from TMS9900 WE* pin)
86CRUIN   <    CRU input bit to TMS9900
87MEMEN*  >    Memory access enable (active low)
99    READYA  <    System ready (goes to READY, 10K pull-up to +5V) A low level puts the cpu on hold.
100    RESET*  >    System reset (active low)
101    SCLK    nc   System clock (not connected in interface card)
102    LCP*    nc   CPU indicator 1=TI99 0=2nd generation (not connected in interface card)
103    BOOTPG* nc   ?
104    AUDIO   <    Input audio (to AUDIOIN in console)
105    RDBENA* <    Active low: enable flex cable data bus drivers (1K pull-up)
106    PCBEN   H    PCB enable for burn-in (always High)
107    HOLD*   H    Active low CPU hold request (always High)
108    IAQHDA  nc   IAQ [or] HOLDA (logical or)
109    SENILA* H(>) Interrupt level A sense enable (always High)
110    SENILB* H(>) Interrupt level B sense enable (always High)
111    INTA*   <    Interrupt level A (active low, goes to EXTINT*)
112    INTB*   nc   Interrupt level B (not used)
113    LOAD*   nc   Unmaskable interrupt (not carried by interface cable/card)
114    D0-D7   <>   Data bus (D0 most significant)
115    A0-A15  >    Address bus (A0 most sig; A15 also used as CRUOUT)
116    AMA/B/C H    Extra address bits (always high for TI-99/4x, but used with SGCPU and Geneve)
117    CLKOUT* >    Inverted PHI3 clock, from TIM9904 clock generator
118    CRUCLK* >    Inverted CRU clock, from TMS9900 CRUCLK pin
119    DBIN    >    Active high = read memory. Drives the data bus buffers.
120    WE*     >    Write Enable pulse (derived from TMS9900 WE* pin)
121    CRUIN   <    CRU input bit to TMS9900
122    MEMEN*  >    Memory access enable (active low)
88123
124    The SENILx lines are somewhat obscure, since there have never been hardware
125    or software that made practical use of them. The intended use can be guessed
126    from some few traditional cards (like the TI RS232) which indeed have
127    connections.
89128
90The obscure SENILx lines:
129    With SENILA* asserted (low), a value shall be put on the data bus,
130    representing the interrupt status bits. It can also be used to determine
131    the source of the interrupt: The RS232 card (in its standard configuration)
132    uses the data bus bits 0 and 1 for its two UARTs, while in the second
133    configuration, it uses bits 4 and 5.
91134
92With SENILA* going low, a value shall be put on the data bus,
93representing the interrupt status bits. It can also be used to determine
94the source of the interrupt: The RS232 card (in its standard configuration)
95uses the data bus bits 0 and 1 for its two UARTs, while in the second
96configuration, it uses bits 4 and 5. Still note that the TI-99/4x does not make
97use of SENILA*.
135    SENILB* / INTB* was planned to be used with disk controllers. The PHP1240 disk
136    controller puts the value of INTB* on D0 when SENILB* gets active (low) which
137    reflects the INTRQ output pin of the WD1771. This signal is not used, however.
138    Instead, the disk controller combines DRQ and IRQ and makes use of a READY/HOLD
139    control of the CPU.
98140
99SENILB* / INTB* was planned to be used with disk controllers. The PHP1240 disk
100controller puts the value of INTB* on D0 when SENILB* gets active (low) which
101reflects the INTRQ output pin of the WD1771. This signal is not used, however.
102Instead, the disk controller combines DRQ and IRQ and makes use of a READY/HOLD
103control of the CPU.
141    Obviously, SENILA* and SENILB* should never be active at the same time, and
142    neither should any memory access to a card be active at the same time, for in
143    both cases, data bus lines may be set to different levels simultaneously. One
144    possible application case is to turn off all cards in the box, assert SENILA*,
145    and then do a read access in the memory area of any card in the P-Box (e.g.
146    0x4000-0x5fff). Another possiblity is that the currently active card simply
147    does not respond to a certain memory access, and in this case the status bits
148    can be read.
104149
105Obviously, SENILA* and SENILB* should never be active at the same time, and
106neither should any memory access to a card be active at the same time, for in
107both cases, data bus lines may be set to different levels simultaneously. One
108possible application case is to turn off all cards in the box, lower SENILA*,
109and then do a read access in the memory area of any card in the P-Box (e.g.
1100x4000-0x5fff). Another possiblity is that the currently active card simply
111does not respond to a certain memory access, and in this case the status bits
112can be read.
150    Also note that the SENILx lines access all cards in parallel, meaning that there
151    must be an agreement which cards may use which bits on the data bus. The lines
152    do not depend on the card being active at that time.
113153
114Also note that the SENILx lines access all cards in parallel, meaning that there
115must be an agreement which cards may use which bits on the data bus. The lines
116do not depend on the card being active at that time.
117*/
154    Emulation architecture
118155
156    console ---- peribox --+ [-- slot1 (always occupied by console connector) ]
157                           |
158                           +--- slot2 --- card
159                           |
160                           +--- slot3 --- card
161                           :
162                           +--- slot8 --- card (usually some floppy controller)
163
164    We have an instance of peribox_device which contains 7 slots
165    (peribox_slot_device) which are subclasses of device_slot_interface.
166    Each slot may host one of several cards (ti_expansion_card_device),
167    which are subclassed from device_slot_card_interface.
168
169    For bus8z_device consult ti99defs.h
170    ---------------------
171
172    June 2010: Reimplemented using device structure (MZ) (obsoletes 99_peb.c)
173    January 2012: Reimplemented as class (MZ)
174
175*****************************************************************************/
176
119177#include "emu.h"
120178#include "peribox.h"
121179
122/* Expansion cards */
123#include "p_code.h"
180// The cards
181#include "ti32kmem.h"
124182#include "ti_fdc.h"
183#include "bwg.h"
125184#include "hfdc.h"
126#include "bwg.h"
127#include "ti_rs232.h"
185#include "p_code.h"
186#include "myarcmem.h"
187#include "samsmem.h"
188#include "tn_ide.h"
128189#include "tn_usbsm.h"
129#include "tn_ide.h"
190#include "evpc.h"
130191#include "hsgpl.h"
192#include "ti_rs232.h"
131193#include "spchsyn.h"
132#include "evpc.h"
133194#include "memex.h"
134195
135#include "imagedev/flopdrv.h"
136#include "imagedev/harddriv.h"
137196#include "formats/ti99_dsk.h"
138#include "ti99_hd.h"
139197
198#define LOG logerror
199#define VERBOSE 1
200
201#define PEBSLOT2 "slot2"
202#define PEBSLOT3 "slot3"
203#define PEBSLOT4 "slot4"
204#define PEBSLOT5 "slot5"
205#define PEBSLOT6 "slot6"
206#define PEBSLOT7 "slot7"
207#define PEBSLOT8 "slot8"
208
209// TODO: Connect other lines (think about how to propagate to the controller)
140210static const floppy_interface ti99_4_floppy_interface =
141211{
142   DEVCB_NULL,
143   DEVCB_NULL,
144   DEVCB_NULL,
145   DEVCB_NULL,
146   DEVCB_NULL,
147   FLOPPY_STANDARD_5_25_DSHD,
148   LEGACY_FLOPPY_OPTIONS_NAME(ti99),
149   NULL,
150   NULL
212   DEVCB_DEVICE_LINE_MEMBER( DEVICE_SELF_OWNER, peribox_device, indexhole ),   // Index hole (floppy out, line)
213   DEVCB_NULL,                        // Motor (floppy in, line)
214   DEVCB_NULL,                        // Track 0 (out, line)
215   DEVCB_NULL,                        // Write protect (out, line)
216   DEVCB_NULL,                        // Ready (out, line)
217   FLOPPY_STANDARD_5_25_DSHD,            // type
218   LEGACY_FLOPPY_OPTIONS_NAME(ti99),      // image formats
219   NULL,                           // interface
220   NULL                           // display info
151221};
152222
153typedef struct _ti99_peb_slot
223peribox_device::peribox_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
224: bus8z_device(mconfig, PERIBOX, "Peripheral expansion box", tag, owner, clock)
154225{
155   device_t      *card;
156   const ti99_peb_card   *intf;
157} ti99_peb_slot;
158
159typedef struct _ti99_peb_state
160{
161   ti99_peb_slot         slot[MAXSLOTS];
162
163   // The next three members are used to emulate the collective lines
164   // If any card goes L, L is propagated. We are using flags.
165   UINT32               inta_state;
166   UINT32               intb_state;
167   UINT32               ready_state;
168
169   int                  highest;
170
171   /* Address bits that are preset to 1 (AMA/AMB/AMC) */
172   int                  address_prefix;
173
174   // Callback to the main system
175   ti99_peb_connect      lines;
176
177} ti99_peb_state;
178
179INLINE ti99_peb_state *get_safe_token(device_t *device)
180{
181   assert(device != NULL);
182   assert(device->type() == PBOX4 || device->type() == PBOX4A || device->type() == PBOX8 || device->type() == PBOXEV || device->type() == PBOXSG || device->type() == PBOXGEN);
183
184   return (ti99_peb_state *)downcast<legacy_device_base *>(device)->token();
226   for (int i=2; i <= 8; i++) m_slot[i] = NULL;
185227}
186228
187INLINE const ti99_peb_config *get_config(device_t *device)
229READ8Z_MEMBER(peribox_device::readz)
188230{
189   assert(device != NULL);
190   assert(device->type() == PBOX4 || device->type() == PBOX4A || device->type() == PBOX8 || device->type() == PBOXEV || device->type() == PBOXSG || device->type() == PBOXGEN);
191
192   return (const ti99_peb_config *) downcast<const legacy_device_base *>(device)->inline_config();
193}
194
195/*
196    Callbacks
197    /INTA is active low
198    Input lines are active low; ASSERT_LINE means LOW in this case.
199*/
200static WRITE_LINE_DEVICE_HANDLER( inta )
201{
202   int slot = get_pebcard_config(device)->slot;
203   ti99_peb_state *peb = get_safe_token(device->owner());
204   if (state==CLEAR_LINE)
231   for (int i=2; i <= 8; i++)
205232   {
206      // The flags are stored as inverted (inta_state=0 if all lines are H)
207      // Unset the L flag of this slot
208      peb->inta_state &= ~(1 << slot);
233      if (m_slot[i]!=NULL) m_slot[i]->readz(space, offset | m_address_prefix, value, mem_mask);
209234   }
210   else
211   {
212      // Set the L flag of this slot
213      peb->inta_state |= (1 << slot);
214   }
215   // Call back the main system, setting the line to L if any PEB card set it to L
216   // This is the case when inta_state is not 0
217   peb->lines.inta((peb->inta_state!=0)? ASSERT_LINE : CLEAR_LINE);
218235}
219236
220static WRITE_LINE_DEVICE_HANDLER( intb )
237WRITE8_MEMBER(peribox_device::write)
221238{
222   int slot = get_pebcard_config(device)->slot;
223   ti99_peb_state *peb = get_safe_token(device->owner());
224   if (state==CLEAR_LINE)
225      peb->intb_state &= ~(1 << slot);
226   else
227      peb->intb_state |= (1 << slot);
228   peb->lines.intb((peb->intb_state!=0)? ASSERT_LINE : CLEAR_LINE);
229}
230
231static WRITE_LINE_DEVICE_HANDLER( ready )
232{
233   int slot = get_pebcard_config(device)->slot;
234   ti99_peb_state *peb = get_safe_token(device->owner());
235   if (state==CLEAR_LINE)
236      peb->ready_state &= ~(1 << slot);
237   else
238      peb->ready_state |= (1 << slot);
239   peb->lines.ready((peb->ready_state!=0)? ASSERT_LINE : CLEAR_LINE);
240}
241
242/*
243    Declares the callbacks above that are called from the cards. This instance
244    is passed to the cards with MCFG_PBOXCARD_ADD and must be handled as a
245    static_config.
246*/
247const peb_callback_if peb_callback =
248{
249   DEVCB_LINE( inta ),
250   DEVCB_LINE( intb ),
251   DEVCB_LINE( ready )
252};
253
254/*
255    Mount the PEB card. Although possible, this is not done by the box itself;
256    instead, the card must call this function on the box. The advantage is
257    that we can assign the same slot to different cards which are selected
258    by dip switches. Only the card which is selected will register itself; the
259    other cards are stillborn.
260    Returns TRUE when the card could be mounted.
261*/
262int mount_card(device_t *device, device_t *cardptr, const ti99_peb_card *cardintf, int slotindex)
263{
264   ti99_peb_state *peb = get_safe_token(device);
265   if ((slotindex > 0) &&  (slotindex < MAXSLOTS))
239   for (int i=2; i <= 8; i++)
266240   {
267      if (peb->slot[slotindex].card == NULL)
268      {
269         peb->slot[slotindex].card = cardptr;
270         peb->slot[slotindex].intf = cardintf;
271         if (slotindex > peb->highest) peb->highest = slotindex;
272         logerror("ti99_peb: registering the card at slot index %d\n", slotindex);
273         return TRUE;
274      }
275      else
276      {
277         logerror("ti99_peb: Tried to mount a second card in slot %d\n", slotindex);
278      }
241      if (m_slot[i]!=NULL) m_slot[i]->write(space, offset | m_address_prefix, data, mem_mask);
279242   }
280   else
281      logerror("ti99_peb: Invalid slot number %d\n", slotindex);
282   return FALSE;
283243}
284244
285void unmount_card(device_t *device, int slotindex)
245void peribox_device::crureadz(offs_t offset, UINT8 *value)
286246{
287   ti99_peb_state *peb = get_safe_token(device);
288   peb->slot[slotindex].card = NULL;
289}
290
291/***************************************************************************
292    Memory and CRU access
293***************************************************************************/
294
295READ8Z_DEVICE_HANDLER( ti99_peb_data_rz )
296{
297   ti99_peb_state *peb = get_safe_token(device);
298   int i;
299   for (i=1; i <= peb->highest; i++)
247   for (int i=2; i <= 8; i++)
300248   {
301      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_read_data))
302         (*peb->slot[i].intf->card_read_data)(peb->slot[i].card, peb->address_prefix | offset, value);
249      if (m_slot[i]!=NULL) m_slot[i]->crureadz(offset, value);
303250   }
304251}
305252
306WRITE8_DEVICE_HANDLER( ti99_peb_data_w )
253void peribox_device::cruwrite(offs_t offset, UINT8 data)
307254{
308   ti99_peb_state *peb = get_safe_token(device);
309   int i;
310   for (i=1; i <= peb->highest; i++)
255   for (int i=2; i <= 8; i++)
311256   {
312      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_write_data))
313         (*peb->slot[i].intf->card_write_data)(peb->slot[i].card, peb->address_prefix | offset, data);
257      if (m_slot[i]!=NULL) m_slot[i]->cruwrite(offset, data);
314258   }
315259}
316260
317READ8Z_DEVICE_HANDLER( ti99_peb_cru_rz )
261/*
262    And here are finally the two mythical lines SENILA* and SENILB*; mythical
263    since there is no report of any software that ever used them.
264*/
265WRITE_LINE_MEMBER(peribox_device::senila)
318266{
319   ti99_peb_state *peb = get_safe_token(device);
320   int i;
321   for (i=1; i <= peb->highest; i++)
267   for (int i=2; i <= 8; i++)
322268   {
323      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_read_cru))
324         (*peb->slot[i].intf->card_read_cru)(peb->slot[i].card, offset, value);
269      if (m_slot[i]!=NULL) m_slot[i]->senila(state);
325270   }
326271}
327272
328WRITE8_DEVICE_HANDLER( ti99_peb_cru_w )
273WRITE_LINE_MEMBER(peribox_device::senilb)
329274{
330   ti99_peb_state *peb = get_safe_token(device);
331   int i;
332   for (i=1; i <= peb->highest; i++)
275   for (int i=2; i <= 8; i++)
333276   {
334      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_write_cru))
335         (*peb->slot[i].intf->card_write_cru)(peb->slot[i].card, offset, data);
277      if (m_slot[i]!=NULL) m_slot[i]->senilb(state);
336278   }
337279}
338280
339281/*
340    For SGCPU
282    The Genmod modification is only of interest for the Geneve. It requires
283    to modify the decoding of each single card.
341284*/
342READ16Z_DEVICE_HANDLER( ti99_peb_data16_rz )
285INPUT_CHANGED_MEMBER( peribox_device::genmod_changed )
343286{
344   ti99_peb_state *peb = get_safe_token(device);
345   int i;
346   for (i=1; i <= peb->highest; i++)
347   {
348      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_read_data16))
349         (*peb->slot[i].intf->card_read_data16)(peb->slot[i].card, peb->address_prefix | offset, value);
350   }
287   set_genmod(newval==1);
351288}
352289
353/*
354    For SGCPU
355*/
356WRITE16_DEVICE_HANDLER( ti99_peb_data16_w )
290void peribox_device::set_genmod(bool set)
357291{
358   ti99_peb_state *peb = get_safe_token(device);
359   int i;
360   for (i=1; i <= peb->highest; i++)
292   for (int i=2; i <= 8; i++)
361293   {
362      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->card_write_data16))
363         (*peb->slot[i].intf->card_write_data16)(peb->slot[i].card, peb->address_prefix | offset, data);
294      if (m_slot[i]!=NULL) m_slot[i]->set_genmod(set);
364295   }
365296}
366297
367298/*
368    SENILx
369    TODO: Check whether device is the correct pointer (seems to be wrong here,
370    the device is usually the caller)
299    The INTA*, INTB*, and READY* lines are connected to each PEB card and are
300    pulled up when inactive. If any card asserts the line (pulling down), the
301    line state goes down. So we must keep a record which cards pull down the
302    line.
371303*/
372WRITE_LINE_DEVICE_HANDLER( ti99_peb_senila )
304void peribox_device::inta_join(int slot, int state)
373305{
374   ti99_peb_state *peb = get_safe_token(device);
375   int i;
376   for (i=1; i <= peb->highest; i++)
377   {
378      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->senila))
379         (*peb->slot[i].intf->senila)(peb->slot[i].card, state);
380   }
306   if (VERBOSE>7) LOG("Peribox propagating INTA from slot %d to console: %d\n", slot, state);
307   if (state==ASSERT_LINE)
308      m_inta_flag |= (1 << slot);
309   else
310      m_inta_flag &= ~(1 << slot);
311
312   m_console_inta((m_inta_flag != 0)? ASSERT_LINE : CLEAR_LINE);
381313}
382314
383WRITE_LINE_DEVICE_HANDLER( ti99_peb_senilb )
315void peribox_device::intb_join(int slot, int state)
384316{
385   ti99_peb_state *peb = get_safe_token(device);
386   int i;
387   for (i=1; i <= peb->highest; i++)
388   {
389      if ((peb->slot[i].card != NULL) && (*peb->slot[i].intf->senilb))
390         (*peb->slot[i].intf->senilb)(peb->slot[i].card, state);
391   }
317   if (VERBOSE>7) LOG("Peribox propagating INTB from slot %d to console: %d\n", slot, state);
318   if (state==ASSERT_LINE)
319      m_intb_flag |= (1 << slot);
320   else
321      m_intb_flag &= ~(1 << slot);
322
323   m_console_intb((m_intb_flag != 0)? ASSERT_LINE : CLEAR_LINE);
392324}
393325
394/***************************************************************************
395    DEVICE FUNCTIONS
396***************************************************************************/
397
398static DEVICE_START( ti99_peb )
326void peribox_device::ready_join(int slot, int state)
399327{
400   ti99_peb_state *peb = get_safe_token(device);
401   const ti99_peb_config* pebconf = (const ti99_peb_config*)get_config(device);
328   if (VERBOSE>7) LOG("Peribox propagating READY from slot %d to console: %d\n", slot, state);
329   if (state==ASSERT_LINE)
330      m_ready_flag |= (1 << slot);
331   else
332      m_ready_flag &= ~(1 << slot);
402333
403   for (int slotindex=0; slotindex < MAXSLOTS; slotindex++)
404      unmount_card(device, slotindex);
334   m_console_ready((m_ready_flag != 0)? ASSERT_LINE : CLEAR_LINE);
335}
405336
406   /* Resolve the callbacks to the console */
407   devcb_write_line inta = DEVCB_LINE(pebconf->inta);
408   devcb_write_line intb = DEVCB_LINE(pebconf->intb);
409   devcb_write_line ready = DEVCB_LINE(pebconf->ready);
337void peribox_device::set_slot_loaded(int slot, peribox_slot_device* slotdev)
338{
339   if (VERBOSE>6)
340   {
341      if (slotdev!=NULL) LOG("Setting slot %d to device %s\n", slot, slotdev->tag());
342      else LOG("Setting slot %d to EMPTY\n", slot);
343   }
344   m_slot[slot] = slotdev;
345}
410346
411   peb->lines.ready.resolve(ready, *device);
412   peb->lines.inta.resolve(inta, *device);
413   peb->lines.intb.resolve(intb, *device);
347void peribox_device::device_start(void)
348{
349   if (VERBOSE>7) LOG("Peribox started\n");
414350
415   peb->address_prefix = (pebconf->amx << 16);
351   floppy_drive_set_rpm(subdevice(FLOPPY_0), 300.);
352   floppy_drive_set_rpm(subdevice(FLOPPY_1), 300.);
353   floppy_drive_set_rpm(subdevice(FLOPPY_2), 300.);
354   floppy_drive_set_rpm(subdevice(FLOPPY_3), 300.);
416355}
417356
418static DEVICE_STOP( ti99_peb )
357void peribox_device::device_config_complete()
419358{
420   logerror("ti99_peb: stop\n");
421}
359   // Resolve the callback lines to the console
360   const peribox_config *intf = reinterpret_cast<const peribox_config *>(static_config());
361   assert (intf != NULL);
362   m_console_inta.resolve(intf->inta, *this);
363   m_console_intb.resolve(intf->intb, *this);
364   m_console_ready.resolve(intf->ready, *this);
422365
423static DEVICE_RESET( ti99_peb )
424{
425   ti99_peb_state *peb = get_safe_token(device);
426   logerror("ti99_peb: reset\n");
427   peb->inta_state = 0;
428   peb->intb_state = 0;
429   peb->ready_state = 0;
430   peb->highest = 0;
431   for (int i=0; i < MAXSLOTS; i++) unmount_card(device, i);
366   m_inta_flag = 0;
367   m_intb_flag = 0;
368   m_ready_flag = 0;
369
370   // The TI-99/4(A) Flex Cable Interface (slot 1) pulls up the AMA/AMB/AMC lines to 1/1/1.
371   m_address_prefix = intf->prefix;
432372}
433373
434#define MCFG_PBOXCARD_ADD( _tag, _type, _slot ) \
435   MCFG_DEVICE_ADD(_tag, _type, 0) \
436   MCFG_DEVICE_CONFIG_DATA32( ti99_pebcard_config, slot, _slot ) \
437   MCFG_DEVICE_CONFIG( peb_callback )
374SLOT_INTERFACE_START( peribox_slot )
375   SLOT_INTERFACE("32kmem", TI_32KMEM)
376   SLOT_INTERFACE("myarcmem", TI99_MYARCMEM)
377   SLOT_INTERFACE("samsmem", TI99_SAMSMEM)
378   SLOT_INTERFACE("pcode", TI99_P_CODE)
379   SLOT_INTERFACE("hsgpl", TI99_HSGPL)
380   SLOT_INTERFACE("tirs232", TI99_RS232)
381   SLOT_INTERFACE("speech", TI99_SPEECH)
382SLOT_INTERFACE_END
438383
439/*
440    We're emulating a "super box" with more than 8 slots. The original
441    box had 8 slots, but that's a bit too limiting.
442    This may become organized differently in future versions; for now
443    we'll go this way.
384SLOT_INTERFACE_START( peribox_slot6 )
385   SLOT_INTERFACE("ide", TI99_IDE)
386   SLOT_INTERFACE("usbsm", TI99_USBSM)
387   SLOT_INTERFACE("tirs232", TI99_RS232)
388   SLOT_INTERFACE("speech", TI99_SPEECH)
389SLOT_INTERFACE_END
444390
445    Slot 0 is the "Flex cable interface" / SGCPU / Geneve
446    We assign the same slot number for some cards that cannot be used
447    in parallel anyway. The config switch will determine which one to use.
448    Future works:
449    // WHTech SCSI controller
450    // MCFG_PBOXCARD_ADD("SCSI", 4, scsi_intf)
391SLOT_INTERFACE_START( peribox_slot7 )
392   SLOT_INTERFACE("ide", TI99_IDE)
393   SLOT_INTERFACE("usbsm", TI99_USBSM)
394   SLOT_INTERFACE("bwg", TI99_BWG)
395   SLOT_INTERFACE("hfdc", TI99_HFDC)
396SLOT_INTERFACE_END
451397
452    // Horizon Ramdisk
453    // MCFG_PBOXCARD_ADD("HRD", 10, ramdisk_intf)
454*/
398SLOT_INTERFACE_START( peribox_slot8 )
399   SLOT_INTERFACE("tifdc", TI99_FDC)
400   SLOT_INTERFACE("bwg", TI99_BWG)
401   SLOT_INTERFACE("hfdc", TI99_HFDC)
402SLOT_INTERFACE_END
455403
456static MACHINE_CONFIG_FRAGMENT( ti99_peb )
457   MCFG_PBOXCARD_ADD( "mem_ti32k",    TI32KMEM, 1 )
458   MCFG_PBOXCARD_ADD( "mem_sams1m",   SAMSMEM,  1 )
459   MCFG_PBOXCARD_ADD( "mem_myarc512",   MYARCMEM, 1 )
460   MCFG_PBOXCARD_ADD( "speech",      TISPEECH, 2 )
461   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 3 )
462   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     4 )
463   MCFG_PBOXCARD_ADD( "hsgpl",       HSGPL,    5 )
464   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  7 )
465   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     8 )
466   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     8 )
467   MCFG_PBOXCARD_ADD( "bwg",         BWG,     8 )
404MACHINE_CONFIG_FRAGMENT( peribox_device )
405   MCFG_PERIBOX_SLOT_ADD( PEBSLOT2, peribox_slot )
406   MCFG_PERIBOX_SLOT_ADD( PEBSLOT3, peribox_slot )
407   MCFG_PERIBOX_SLOT_ADD( PEBSLOT4, peribox_slot )
408   MCFG_PERIBOX_SLOT_ADD( PEBSLOT5, peribox_slot )
409   MCFG_PERIBOX_SLOT_ADD( PEBSLOT6, peribox_slot6 )
410   MCFG_PERIBOX_SLOT_ADD( PEBSLOT7, peribox_slot7 )
411   MCFG_PERIBOX_SLOT_ADD( PEBSLOT8, peribox_slot8 )
468412
469413   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
470414   MCFG_MFMHD_3_DRIVES_ADD()
471415MACHINE_CONFIG_END
472416
473static MACHINE_CONFIG_FRAGMENT( ti994a_peb )
474   MCFG_PBOXCARD_ADD( "mem_ti32k",    TI32KMEM, 1 )
475   MCFG_PBOXCARD_ADD( "mem_sams1m",   SAMSMEM,  1 )
476   MCFG_PBOXCARD_ADD( "mem_myarc512",   MYARCMEM, 1 )
477   MCFG_PBOXCARD_ADD( "speech",      TISPEECH, 2 )
478   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 3 )
479   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     4 )
480   MCFG_PBOXCARD_ADD( "hsgpl",       HSGPL,    5 )
481   MCFG_PBOXCARD_ADD( "p_code_card",   PCODEN,   6 )
482   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  7 )
483   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     8 )
484   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     8 )
485   MCFG_PBOXCARD_ADD( "bwg",         BWG,     8 )
417machine_config_constructor peribox_device::device_mconfig_additions() const
418{
419   return MACHINE_CONFIG_NAME( peribox_device );
420}
486421
487   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
488   MCFG_MFMHD_3_DRIVES_ADD()
489MACHINE_CONFIG_END
422/****************************************************************************
423    A variant of the box used for the Geneve.
424*****************************************************************************/
490425
491static MACHINE_CONFIG_FRAGMENT( ti99ev_peb )
492   MCFG_PBOXCARD_ADD( "mem_ti32k",    TI32KMEM, 1 )
493   MCFG_PBOXCARD_ADD( "mem_sams1m",   SAMSMEM,  1 )
494   MCFG_PBOXCARD_ADD( "mem_myarc512",   MYARCMEM, 1 )
495   MCFG_PBOXCARD_ADD( "speech",      TISPEECH, 2 )
496   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 3 )
497   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     4 )
498   MCFG_PBOXCARD_ADD( "hsgpl",       HSGPL,    5 )
499   MCFG_PBOXCARD_ADD( "p_code_card",   PCODEN,   6 )
500   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  7 )
501   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     8 )
502   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     8 )
503   MCFG_PBOXCARD_ADD( "bwg",         BWG,     8 )
504   MCFG_PBOXCARD_ADD( "evpc",         EVPC,     9 )
426peribox_gen_device::peribox_gen_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
427: peribox_device(mconfig, tag, owner, clock)
428{
429};
505430
431SLOT_INTERFACE_START( peribox_slotg )
432   SLOT_INTERFACE("memex", TI99_MEMEX)
433   SLOT_INTERFACE("myarcmem", TI99_MYARCMEM)
434   SLOT_INTERFACE("samsmem", TI99_SAMSMEM)
435   SLOT_INTERFACE("pcode", TI99_P_CODE)
436   SLOT_INTERFACE("tirs232", TI99_RS232)
437   SLOT_INTERFACE("speech", TI99_SPEECH)
438SLOT_INTERFACE_END
439
440MACHINE_CONFIG_FRAGMENT( peribox_gen_device )
441   MCFG_PERIBOX_SLOT_ADD( PEBSLOT2, peribox_slotg )
442   MCFG_PERIBOX_SLOT_ADD( PEBSLOT3, peribox_slotg )
443   MCFG_PERIBOX_SLOT_ADD( PEBSLOT4, peribox_slotg )
444   MCFG_PERIBOX_SLOT_ADD( PEBSLOT5, peribox_slotg )
445   MCFG_PERIBOX_SLOT_ADD( PEBSLOT6, peribox_slot6 )
446   MCFG_PERIBOX_SLOT_ADD( PEBSLOT7, peribox_slot7 )
447   MCFG_PERIBOX_SLOT_ADD( PEBSLOT8, peribox_slot8 )
448
506449   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
507450   MCFG_MFMHD_3_DRIVES_ADD()
508451MACHINE_CONFIG_END
509452
453machine_config_constructor peribox_gen_device::device_mconfig_additions() const
454{
455   return MACHINE_CONFIG_NAME( peribox_gen_device );
456}
510457
511static MACHINE_CONFIG_FRAGMENT( ti998_peb )
512   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 1 )
513   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     2 )
514   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  3 )
515   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     8 )
516   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     8 )
517   MCFG_PBOXCARD_ADD( "bwg",         BWG,     8 )
458/****************************************************************************
459    A variant of the box used for the SGCPU (aka TI-99/4P).
460*****************************************************************************/
518461
519   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
520   MCFG_MFMHD_3_DRIVES_ADD()
521MACHINE_CONFIG_END
462peribox_sg_device::peribox_sg_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
463: peribox_device(mconfig, tag, owner, clock)
464{
465};
522466
523static MACHINE_CONFIG_FRAGMENT( ti99sg_peb )
524   MCFG_PBOXCARD_ADD( "speech",      TISPEECH, 1 )
525   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 2 )
526   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     3 )
527   MCFG_PBOXCARD_ADD( "hsgpl",       HSGPL,    4 )
528   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  6 )
529   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     7 )
530   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     7 )
531   MCFG_PBOXCARD_ADD( "bwg",         BWG,     7 )
532   MCFG_PBOXCARD_ADD( "evpc",         EVPC,     8 )
467SLOT_INTERFACE_START( peribox_slotp )
468   SLOT_INTERFACE("pcode", TI99_P_CODE)
469   SLOT_INTERFACE("tirs232", TI99_RS232)
470   SLOT_INTERFACE("speech", TI99_SPEECH)
471SLOT_INTERFACE_END
533472
473SLOT_INTERFACE_START( peribox_ev_slot )
474   SLOT_INTERFACE("evpc", TI99_EVPC)
475SLOT_INTERFACE_END
476
477SLOT_INTERFACE_START( peribox_hs_slot )
478   SLOT_INTERFACE("hsgpl", TI99_HSGPL)
479SLOT_INTERFACE_END
480
481MACHINE_CONFIG_FRAGMENT( peribox_sg_device )
482   MCFG_PERIBOX_SLOT_ADD_DEF( PEBSLOT2, peribox_ev_slot, "evpc" )
483   MCFG_PERIBOX_SLOT_ADD_DEF( PEBSLOT3, peribox_hs_slot, "hsgpl" )
484   MCFG_PERIBOX_SLOT_ADD( PEBSLOT4, peribox_slotp )
485   MCFG_PERIBOX_SLOT_ADD( PEBSLOT5, peribox_slotp )
486   MCFG_PERIBOX_SLOT_ADD( PEBSLOT6, peribox_slot6 )
487   MCFG_PERIBOX_SLOT_ADD( PEBSLOT7, peribox_slot7 )
488   MCFG_PERIBOX_SLOT_ADD( PEBSLOT8, peribox_slot7 )
489
534490   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
535491   MCFG_MFMHD_3_DRIVES_ADD()
536492MACHINE_CONFIG_END
537493
538static MACHINE_CONFIG_FRAGMENT( geneve_peb )
539   MCFG_PBOXCARD_ADD( "memex",         GENMEMEX, 1 )
540   MCFG_PBOXCARD_ADD( "speech",      TISPEECH, 2 )
541   MCFG_PBOXCARD_ADD( "usbsmart",      USBSMART, 3 )
542   MCFG_PBOXCARD_ADD( "ide",         TNIDE,     4 )
543   MCFG_PBOXCARD_ADD( "rs232_card",   TIRS232,  6 )
544   MCFG_PBOXCARD_ADD( "ti_fdc",      TIFDC,     7 )
545   MCFG_PBOXCARD_ADD( "hfdc",         HFDC,     7 )
546   MCFG_PBOXCARD_ADD( "bwg",         BWG,     7 )
494machine_config_constructor peribox_sg_device::device_mconfig_additions() const
495{
496   return MACHINE_CONFIG_NAME( peribox_sg_device );
497}
547498
499
500/****************************************************************************
501    Another variant of the box; used for the TI with EVPC. The EVPC is
502    obviously required.
503*****************************************************************************/
504
505peribox_ev_device::peribox_ev_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
506: peribox_device(mconfig, tag, owner, clock)
507{
508};
509
510MACHINE_CONFIG_FRAGMENT( peribox_ev_device )
511   MCFG_PERIBOX_SLOT_ADD_DEF( PEBSLOT2, peribox_ev_slot, "evpc" )
512   MCFG_PERIBOX_SLOT_ADD( PEBSLOT3, peribox_slot )
513   MCFG_PERIBOX_SLOT_ADD( PEBSLOT4, peribox_slot )
514   MCFG_PERIBOX_SLOT_ADD( PEBSLOT5, peribox_slot )
515   MCFG_PERIBOX_SLOT_ADD( PEBSLOT6, peribox_slot6 )
516   MCFG_PERIBOX_SLOT_ADD( PEBSLOT7, peribox_slot7 )
517   MCFG_PERIBOX_SLOT_ADD( PEBSLOT8, peribox_slot8 )
518
548519   MCFG_LEGACY_FLOPPY_4_DRIVES_ADD(ti99_4_floppy_interface)
549520   MCFG_MFMHD_3_DRIVES_ADD()
550521MACHINE_CONFIG_END
551522
552static const char DEVTEMPLATE_SOURCE[] = __FILE__;
523machine_config_constructor peribox_ev_device::device_mconfig_additions() const
524{
525   return MACHINE_CONFIG_NAME( peribox_ev_device );
526}
553527
554#define DEVTEMPLATE_ID(p,s)             p##ti99_peb##s
555#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_MACHINE_CONFIG | DT_HAS_INLINE_CONFIG
556#define DEVTEMPLATE_NAME                "TI-99 Peripheral Expansion System (99/4)"
557#define DEVTEMPLATE_FAMILY              "External devices"
558#include "devtempl.h"
528/***************************************************************************
529    Floppy interface
530****************************************************************************/
559531
560#define DEVTEMPLATE_DERIVED_ID(p,s)      p##ti994a_peb##s
561#define DEVTEMPLATE_DERIVED_FEATURES   DT_HAS_MACHINE_CONFIG
562#define DEVTEMPLATE_DERIVED_NAME      "TI-99 Peripheral Expansion System (99/4A)"
563#include "devtempl.h"
532// TODO: won't be called unless motor_on is triggered
533WRITE_LINE_MEMBER( peribox_device::indexhole )
534{
535   if (VERBOSE>4) LOG("peribox_device: indexhole\n");
536}
564537
565#define DEVTEMPLATE_DERIVED_ID(p,s)      p##ti998_peb##s
566#define DEVTEMPLATE_DERIVED_FEATURES   DT_HAS_MACHINE_CONFIG
567#define DEVTEMPLATE_DERIVED_NAME      "TI-99 Peripheral Expansion System (99/8)"
568#include "devtempl.h"
538/***************************************************************************
539    Implementation of a slot within the box.
540****************************************************************************/
569541
570#define DEVTEMPLATE_DERIVED_ID(p,s)      p##ti99ev_peb##s
571#define DEVTEMPLATE_DERIVED_FEATURES   DT_HAS_MACHINE_CONFIG
572#define DEVTEMPLATE_DERIVED_NAME      "TI-99 Peripheral Expansion System (99/4a with EVPC)"
573#include "devtempl.h"
542int peribox_slot_device::get_index_from_tagname()
543{
544   const char *mytag = tag();
545   int maxlen = strlen(mytag);
546   int i;
547   for (i=maxlen-1; i >=0; i--)
548      if (mytag[i] < 48 || mytag[i] > 57) break;
574549
575#define DEVTEMPLATE_DERIVED_ID(p,s)      p##ti99sg_peb##s
576#define DEVTEMPLATE_DERIVED_FEATURES   DT_HAS_MACHINE_CONFIG
577#define DEVTEMPLATE_DERIVED_NAME      "TI99 Peripheral Expansion System (SGCPU)"
578#include "devtempl.h"
550   return atoi(mytag+i+1);
551}
579552
580#define DEVTEMPLATE_DERIVED_ID(p,s)      p##geneve_peb##s
581#define DEVTEMPLATE_DERIVED_FEATURES   DT_HAS_MACHINE_CONFIG
582#define DEVTEMPLATE_DERIVED_NAME      "TI99 Peripheral Expansion System (Geneve)"
583#include "devtempl.h"
553peribox_slot_device::peribox_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
554: bus8z_device(mconfig, PERIBOX_SLOT, "TI P-Box slot", tag, owner, clock), device_slot_interface(mconfig, *this)
555{
556}
584557
585DEFINE_LEGACY_DEVICE( PBOX4, ti99_peb );
586DEFINE_LEGACY_DEVICE( PBOX4A, ti994a_peb );
587DEFINE_LEGACY_DEVICE( PBOXEV, ti99ev_peb );
588DEFINE_LEGACY_DEVICE( PBOX8, ti998_peb );
589DEFINE_LEGACY_DEVICE( PBOXSG, ti99sg_peb );
590DEFINE_LEGACY_DEVICE( PBOXGEN, geneve_peb );
558READ8Z_MEMBER(peribox_slot_device::readz)
559{
560   m_card->readz(space, offset, value, mem_mask);
561}
562
563WRITE8_MEMBER(peribox_slot_device::write)
564{
565   m_card->write(space, offset, data, mem_mask);
566}
567
568void peribox_slot_device::crureadz(offs_t offset, UINT8 *value)
569{
570   m_card->crureadz(offset, value);
571}
572
573void peribox_slot_device::cruwrite(offs_t offset, UINT8 data)
574{
575   m_card->cruwrite(offset, data);
576}
577
578void peribox_slot_device::senila(int state)
579{
580   m_card->set_senila(state);
581}
582
583void peribox_slot_device::senilb(int state)
584{
585   m_card->set_senilb(state);
586}
587
588/*
589    Genmod support
590*/
591void peribox_slot_device::set_genmod(bool set)
592{
593   m_card->m_genmod = set;
594}
595
596void peribox_slot_device::device_start(void)
597{
598   if (VERBOSE>7) LOG("Peribox slot started\n");
599}
600
601void peribox_slot_device::device_config_complete()
602{
603   m_slotnumber = get_index_from_tagname();
604   device_t *carddev = first_subdevice();
605   peribox_device *peb = static_cast<peribox_device*>(owner());
606   if (carddev != NULL)
607   {
608      peb->set_slot_loaded(m_slotnumber, this);
609      m_card = static_cast<ti_expansion_card_device*>(carddev);
610   }
611   else
612   {
613      peb->set_slot_loaded(m_slotnumber, NULL);
614      m_card = NULL;
615   }
616}
617
618/*
619    Delivers the drives that are installed in the box. Called from a card.
620*/
621device_t* peribox_slot_device::get_drive(const char* name)
622{
623   return owner()->subdevice(name);
624}
625
626/*
627    These methods are called from the expansion cards. They add the
628    slot number to identify the slot to the box.
629*/
630WRITE_LINE_MEMBER( peribox_slot_device::set_inta )
631{
632   peribox_device *peb = static_cast<peribox_device*>(owner());
633   peb->inta_join(m_slotnumber, state);
634}
635
636WRITE_LINE_MEMBER( peribox_slot_device::set_intb )
637{
638   peribox_device *peb = static_cast<peribox_device*>(owner());
639   peb->intb_join(m_slotnumber, state);
640}
641
642WRITE_LINE_MEMBER( peribox_slot_device::set_ready )
643{
644   peribox_device *peb = static_cast<peribox_device*>(owner());
645   peb->ready_join(m_slotnumber, state);
646}
647
648/***************************************************************************/
649const device_type PERIBOX = &device_creator<peribox_device>;
650const device_type PERIBOX_SLOT = &device_creator<peribox_slot_device>;
651
652const device_type PERIBOX_EV = &device_creator<peribox_ev_device>;
653const device_type PERIBOX_SG = &device_creator<peribox_sg_device>;
654const device_type PERIBOX_GEN = &device_creator<peribox_gen_device>;
655
656
src/mess/machine/ti99/datamux.h
r14513r14514
1/****************************************************************************
2
3    TI-99/4(A) databus multiplexer circuit
4    See datamux.c for documentation
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
112#ifndef __DMUX__
213#define __DMUX__
314
4typedef void (*databus_read_function)(device_t *device, offs_t offset, UINT8 *value);
5typedef void (*databus_write_function)(device_t *device, offs_t offset, UINT8 value);
15#include "ti99defs.h"
616
7/* device interface */
8DECLARE_LEGACY_DEVICE( DMUX, datamux );
17extern const device_type DATAMUX;
918
10READ16_DEVICE_HANDLER(  ti99_dmux_r );
11WRITE16_DEVICE_HANDLER( ti99_dmux_w );
19/*
20    Device that is attached to this datamux.
21    The configuration setting is used for certain configurations
22    where devices may only be used if others are turned off. In particular,
23    if the HGSPL expansion card is used, the GROMs in the console must be
24    removed.
25*/
26typedef struct _dmux_device_list_entry
27{
28   const char            *name;            // Name of the device (used for looking up the device)
29   UINT16               select;            // State of the address line bits when addressing this device
30   UINT16               address_mask;      // Bits of the address bus used to address this device
31   UINT16               write_select;      // Bits set when doing write accesses to this device (ffff = write-only)
32   const char            *setting;         // configuration switch that may have an effect for the presence of this device
33   UINT8               set;            // bits that must be set for this switch so that this device is present
34   UINT8               unset;            // bits that must be reset for this switch so that this device is present
35} dmux_device_list_entry;
1236
13typedef struct _bus_device
37/*
38    Device list of this datamux.
39*/
40class attached_device
1441{
15   const char            *name;
16   UINT16               address_mask;
17   UINT16               select_pattern;
18   UINT16               write_select;
19   databus_read_function   read;
20   databus_write_function   write;
21   const char            *setting;
22   UINT8               set;
23   UINT8               unset;
24} bus_device;
42   friend class simple_list<attached_device>;
43   friend class ti99_datamux_device;
2544
45public:
46   attached_device(device_t *busdevice, const dmux_device_list_entry &entry)
47   :   m_device(busdevice), m_config(&entry) { };
48
49private:
50   attached_device               *m_next;
51   device_t                  *m_device;      // the actual device
52   const dmux_device_list_entry   *m_config;
53};
54
55/*
56    Main class
57*/
58class ti99_datamux_device : public device_t
59{
60public:
61   ti99_datamux_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
62   DECLARE_READ16_MEMBER( read );
63   DECLARE_WRITE16_MEMBER( write );
64
65protected:
66   /* Constructor */
67   virtual void device_start(void);
68   virtual void device_stop(void);
69   virtual void device_reset(void);
70
71private:
72   /* All devices that are attached to the 8-bit bus. */
73   simple_list<attached_device> m_devices;
74
75   /* Latch which stores the first byte */
76   UINT8 m_latch;
77
78   // Intermediate storage which holds the low byte of the previous read cycle.
79   // In reality, the TMS9900 adds the low byte to the 16 bit memory word
80   // from the previous read-before-write cycle, but this is not yet handled
81   // by the TMS9900 emulation. TODO: rewrite TMS9900
82   UINT8 m_lowbyte;
83   UINT8 m_highbyte;
84
85   /* Memory expansion (internal, 16 bit). */
86   UINT16 *m_ram16b;
87
88   /* Use the memory expansion? */
89   int m_use32k;
90
91   /* Reference to the CPU; avoid lookups. */
92   device_t *m_cpu;
93
94   /* Reference to the address space, maybe unnecessary. */
95   address_space   *m_space;
96};
97
98/******************************************************************************/
99
26100#define MCFG_DMUX_ADD(_tag, _devices)         \
27   MCFG_DEVICE_ADD(_tag, DMUX, 0) \
101   MCFG_DEVICE_ADD(_tag, DATAMUX, 0) \
28102   MCFG_DEVICE_CONFIG( _devices )
29103#endif
104
src/mess/machine/ti99/hfdc.h
r14513r14514
1/*
1/****************************************************************************
2
23    Myarc Hard and Floppy Disk Controller
3    Based on SMC9234
4    High Density, Double-sided
4    See hfdc.c for documentation
5
56    Michael Zapf, September 2010
6*/
77
8/* device interface */
8    January 2012: rewritten as class
9
10****************************************************************************/
11
912#ifndef __HFDC__
1013#define __HFDC__
1114
12DECLARE_LEGACY_DEVICE( HFDC, ti99_hfdc );
15#include "emu.h"
16#include "peribox.h"
17#include "ti99defs.h"
18#include "ti99_hd.h"
19#include "machine/smc92x4.h"
20
21#define HFDC_MAX_FLOPPY 4
22#define HFDC_MAX_HARD 4
23
24extern const device_type TI99_HFDC;
25
26class myarc_hfdc_device : public ti_expansion_card_device
27{
28public:
29   myarc_hfdc_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
30   DECLARE_READ8Z_MEMBER(readz);
31   DECLARE_WRITE8_MEMBER(write);
32   void crureadz(offs_t offset, UINT8 *value);
33   void cruwrite(offs_t offset, UINT8 value);
34
35   DECLARE_WRITE_LINE_MEMBER( intrq_w );
36   DECLARE_WRITE_LINE_MEMBER( drq_w );
37   DECLARE_WRITE_LINE_MEMBER( dip_w );
38   DECLARE_READ8_MEMBER( auxbus_in );
39   DECLARE_WRITE8_MEMBER( auxbus_out );
40   DECLARE_READ8_MEMBER( read_buffer );
41   DECLARE_WRITE8_MEMBER( write_buffer );
42
43protected:
44   void device_start(void);
45   void device_reset(void);
46   const rom_entry *device_rom_region() const;
47   machine_config_constructor device_mconfig_additions() const;
48   ioport_constructor device_input_ports() const;
49   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
50
51
52private:
53   // Calculates a simple version of a binary logarithm
54   int          slog2(int value);
55
56   // When true, triggers motor monoflop.
57   bool         m_trigger_motor;
58
59   // When true, motor monoflop is high
60   bool         m_motor_running;
61
62   /* Clock divider bit 0. Unused in this emulation. */
63   int          m_CD0;
64
65   /* Clock divider bit 1. Unused in this emulation. */
66   int            m_CD1;
67
68   /* count 4.23s from rising edge of motor_on */
69   emu_timer*      m_motor_on_timer;
70
71   // Link to the HDC9234 controller on the board. In fact, the proper name
72   // is HDC 9234, the manufacturer is Standard Microsystems Corp.
73   smc92x4_device*   m_hdc9234;
74
75   /* Link to the clock chip on the board. */
76   device_t*      m_clock;
77
78   /* Determines whether we have access to the CRU bits. */
79   bool         m_cru_select;
80
81   /* IRQ state */
82   bool         m_irq;
83
84   /* DMA in Progress state */
85   bool         m_dip;
86
87   /* Output 1 latch */
88   UINT8         m_output1_latch;
89
90   /* Output 2 latch */
91   UINT8         m_output2_latch;
92
93   /* Connected floppy drives. */
94   device_t*      m_floppy_unit[HFDC_MAX_FLOPPY];
95
96   /* Connected harddisk drives. */
97   device_t*      m_harddisk_unit[HFDC_MAX_HARD];
98
99   /* DMA address latch */
100   UINT32         m_dma_address;
101
102   // Device Service Routine ROM
103   UINT8*         m_dsrrom;
104
105   // ROM banks.
106   int          m_rom_page;
107
108   // HFDC RAM
109   UINT8*         m_buffer_ram;
110
111   // RAM page registers
112   int          m_ram_page[4];
113};
114
115
13116#endif
src/mess/machine/ti99/myarcmem.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Myarc memory expansion
34
4    Note that the DSR part is still missing
5    TODO: Test this device with some emulated program!
6*/
7#include "emu.h"
8#include "peribox.h"
5    The card features 128 KiB of RAM, not buffered. In the TI-99/4A address
6    space, RAM is located at 2000-3fff and a000-ffff (32 KiB). Using the CRU
7    interface, four banks of 32K each can be made visible in this area.
8
9    We also emulate the 512 KiB version of this card; it works in the same way
10    but offers two more CRU bits for a total of 16 banks of 32 KiB RAM.
11
12    Beside the RAM, the card also contains ROM with a DSR (Device service
13    routine, TI's term for the firmware). This 8 KiB ROM allows for
14    testing and partitioning the card, and it introduces new devices which
15    are make available to BASIC programs, like a RAMdisk device.
16
17    There also was a 32 KiB version which did not contain a DSR; thus, it was
18    equivalent to a standard TI 32 KiB memory expansion and is not emulated
19    here.
20
21    The firmware to be used with this card is a version that is tailored to
22    work with Myarc Extended Basic II.
23
24    Michael Zapf
25    February 2012: rewritten as a class, adding DSR support
26
27****************************************************************************/
928#include "myarcmem.h"
1029
11typedef ti99_pebcard_config ti99_myarcmem_config;
30#define RAMREGION "ram"
1231
13typedef struct _ti99_myarcmem_state
14{
15   int    bank;
16   UINT8   *memory;
32#define VERBOSE 0
33#define LOG logerror
1734
18} ti99_myarcmem_state;
35/* This card has two CRU bases where it answers. */
36#define MYARCMEM_CRU_BASE1 0x1000
37#define MYARCMEM_CRU_BASE2 0x1900
1938
20INLINE ti99_myarcmem_state *get_safe_token(device_t *device)
39enum
2140{
22   assert(device != NULL);
23   assert(device->type() == MYARCMEM);
41   SIZE_128,
42   SIZE_512
43};
2444
25   return (ti99_myarcmem_state *)downcast<legacy_device_base *>(device)->token();
45myarc_memory_expansion_device::myarc_memory_expansion_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
46: ti_expansion_card_device(mconfig, TI99_MYARCMEM, "Myarc Memory expansion card MEXP-1", tag, owner, clock)
47{
48   m_shortname = "ti99_myarcmem";
2649}
2750
28#define MYARCMEM_CRU_BASE1 0x1000
29#define MYARCMEM_CRU_BASE2 0x1900
51int myarc_memory_expansion_device::get_base(int offset)
52{
53   int base = 0;
54   if (m_size == SIZE_128)
55   {
56      base = ((m_bank & 0x03) << 15);
57   }
58   else
59   {
60      base = (m_bank << 15);
61   }
62   base |= (offset & 0x1fff);
63   return base;
64}
3065
3166/*
32    CRU write (is there a read?)
67    Memory read access.
68    RAM is at 2000-3fff, a000-ffff;
69    ROM is at 4000-5fff (if CRU bit 0 is set)
3370*/
34static WRITE8_DEVICE_HANDLER( myarcmem_cru_w )
71READ8Z_MEMBER(myarc_memory_expansion_device::readz)
3572{
36   ti99_myarcmem_state *card = get_safe_token(device);
37   if (((offset & 0xff00)==MYARCMEM_CRU_BASE1)||((offset & 0xff00)==MYARCMEM_CRU_BASE2))
73   int base = get_base(offset);
74
75   switch((offset & 0xe000)>>13)
3876   {
39      if ((offset & 0x001e)==0)
40      {
41         logerror("Activate Myarc memory expansion DSR ROM. Not available.\n");
42      }
43      else
44      {
45         // offset / 2 = cru bit
46         // bit 1 = 1; bit 2 = 2; bit 3 = 4; bit 4 = 8
47         UINT8 mask = 1<<((offset>>1)-1);
48         if (data==0)
49            card->bank &= ~mask;
50         else
51         {
52            card->bank |= mask;
53         }
54      }
77   case 1:
78      *value = m_ram[base];
79      break;
80   case 2:
81      if (m_selected) *value = m_dsrrom[offset & 0x1fff];
82      break;
83   case 5:
84      *value = m_ram[base | 0x2000];
85      break;
86   case 6:
87      *value = m_ram[base | 0x4000];
88      break;
89   case 7:
90      *value = m_ram[base | 0x6000];
91      break;
92   default:
93      break;
5594   }
5695}
5796
5897/*
59    Memory read. The memory is at locations 0x2000-0x3fff and 0xa000-0xffff
98    Memory write access. DSRROM does not allow writing.
6099*/
61static READ8Z_DEVICE_HANDLER( myarcmem_rz )
100WRITE8_MEMBER(myarc_memory_expansion_device::write)
62101{
63   ti99_myarcmem_state *card = get_safe_token(device);
64   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xc000)==0xc000))
102   int base = get_base(offset);
103
104   switch((offset & 0xe000)>>13)
65105   {
66      if (offset < 0xa000)
67         *value = card->memory[offset-0x2000 + card->bank*32768];
68      else
69         *value = card->memory[offset-0x8000 + card->bank*32768];
106   case 1:
107      m_ram[base] = data;
108      break;
109   case 5:
110      m_ram[base | 0x2000] = data;
111      break;
112   case 6:
113      m_ram[base | 0x4000] = data;
114      break;
115   case 7:
116      m_ram[base | 0x6000] = data;
117      break;
118   default:
119      break;
70120   }
71121}
72122
73123/*
74    Memory write
124    CRU read. None here.
75125*/
76static WRITE8_DEVICE_HANDLER( myarcmem_w )
126void myarc_memory_expansion_device::crureadz(offs_t offset, UINT8 *value)
77127{
78   ti99_myarcmem_state *card = get_safe_token(device);
79   if (((offset & 0xe000)==0x2000) || ((offset & 0xe000)==0xa000) || ((offset & 0xc000)==0xc000))
128   return;
129}
130
131/*
132    CRU write. Bit 0 turns on the DSR (firmware), bits 1-3 are used to select
133    one of several 32K RAM banks.
134
135    Select bits
136        1000 = DSRROM seen on 4000-5fff (128, 512K)
137        1002 = bit 0 of RAM bank value (128, 512K)
138        1004 = bit 1 of RAM bank value (128, 512K)
139        1006 = bit 2 of RAM bank value (512K)
140        1008 = bit 3 of RAM bank value (512K)
141*/
142void myarc_memory_expansion_device::cruwrite(offs_t offset, UINT8 data)
143{
144   if (((offset & 0xff00)==MYARCMEM_CRU_BASE1)||((offset & 0xff00)==MYARCMEM_CRU_BASE2))
80145   {
81      if (offset < 0xa000)
82         card->memory[offset-0x2000 + card->bank*32768] = data;
146      if ((offset & 0x000e)==0)
147      {
148         // Turn on/off DSR
149         m_selected = (data!=0);
150      }
83151      else
84         card->memory[offset-0x8000 + card->bank*32768] = data;
152      {
153         // xxxx xxxx xxxx 0010
154         // xxxx xxxx xxxx 0100
155         // xxxx xxxx xxxx 0110
156         // xxxx xxxx xxxx 1000
157         int bankbit = 1 << (((offset & 0x000e)>>1)-1);
158
159         if (data==0)
160            m_bank &= ~bankbit;
161         else
162            m_bank |= bankbit;
163      }
85164   }
86165}
87166
88/**************************************************************************/
89167
90static const ti99_peb_card myarcmem_card =
168INPUT_PORTS_START( myarc_exp )
169   PORT_START( "SIZE" )
170   PORT_CONFNAME( 0x01, SIZE_512, "Myarc memory card size" )
171      PORT_CONFSETTING( SIZE_128, "128 KiB")
172      PORT_CONFSETTING( SIZE_512, "512 KiB")
173INPUT_PORTS_END
174
175ROM_START( myarc_exp )
176   ROM_REGION(0x2000, DSRROM, 0)
177   ROM_LOAD("myarc512k_xb2.bin", 0x0000, 0x2000, CRC(41fbb96d) SHA1(4dc7fdfa46842957bcbb0cf2c37764e4bb6d877a)) /* DSR for Ramdisk etc. */
178   ROM_REGION(0x80000, RAMREGION, 0)
179   ROM_FILL(0x0000, 0x80000, 0x00)
180ROM_END
181
182void myarc_memory_expansion_device::device_start()
91183{
92   myarcmem_rz, myarcmem_w,         // memory access read/write
93   NULL, myarcmem_cru_w,         // CRU access (no read here)
94   NULL, NULL,                  // SENILA/B access (none here)
95   NULL, NULL                  // 16 bit access (none here)
96};
184   if (VERBOSE>5) LOG("myarc memexp: start\n");
185   m_dsrrom = subregion(DSRROM)->base();
186   m_ram = subregion(RAMREGION)->base();
187}
97188
98static DEVICE_START( ti99_myarcmem )
189void myarc_memory_expansion_device::device_reset()
99190{
100   ti99_myarcmem_state *card = get_safe_token(device);
101   card->memory=NULL;
191   if (VERBOSE>5) LOG("myarc memexp: reset\n");
192   m_size = input_port_read(*this, "SIZE");
193
194   // Resetting values
195   m_bank = 0;
196   m_selected = false;
102197}
103198
104static DEVICE_STOP( ti99_myarcmem )
199const rom_entry *myarc_memory_expansion_device::device_rom_region() const
105200{
106   ti99_myarcmem_state *card = get_safe_token(device);
107   if (card->memory) free(card->memory);
201   return ROM_NAME( myarc_exp );
108202}
109203
110static DEVICE_RESET( ti99_myarcmem )
204ioport_constructor myarc_memory_expansion_device::device_input_ports() const
111205{
112   ti99_myarcmem_state *card = get_safe_token(device);
113   /* Register the card */
114   device_t *peb = device->owner();
115
116   if (input_port_read(device->machine(), "RAM")==RAM_MYARC512)
117   {
118      int success = mount_card(peb, device, &myarcmem_card, get_pebcard_config(device)->slot);
119      if (!success) return;
120
121      if (card->memory==NULL)
122      {
123         // Allocate 512 KiB
124         card->memory = (UINT8*)malloc(0x080000);
125      }
126      card->bank = 0;
127   }
206   return INPUT_PORTS_NAME(myarc_exp);
128207}
129208
130static const char DEVTEMPLATE_SOURCE[] = __FILE__;
131
132#define DEVTEMPLATE_ID(p,s)             p##ti99_myarcmem##s
133#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
134#define DEVTEMPLATE_NAME                "TI99 Myarc/Foundation Memory Expansion Card"
135#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
136#include "devtempl.h"
137
138DEFINE_LEGACY_DEVICE( MYARCMEM, ti99_myarcmem );
139
209const device_type TI99_MYARCMEM = &device_creator<myarc_memory_expansion_device>;
src/mess/machine/ti99/memex.c
r14513r14514
1/*
1/***************************************************************************
2
23    Geneve "Memex" memory expansion
3    Michael Zapf, February 2011
4*/
4    may be used together with the GenMod feature to expand the memory to the
5    full 2 MiB range.
56
6#include "emu.h"
7#include "peribox.h"
7    Michael Zapf
8    February 2011
9    February 2012: rewritten as class
10
11****************************************************************************/
12
813#include "memex.h"
914
10typedef ti99_pebcard_config gen_memex_config;
15#define MEMEX_SIZE 0x200000
16#define RAMREGION "ram"
1117
12typedef struct _gen_memex_state
18#define VERBOSE 1
19#define LOG logerror
20
21enum
1322{
14   UINT8   *memory;
15   UINT8   dip_switch[8];
16   int      genmod;
17} gen_memex_state;
23   MDIP1 = 0x01,
24   MDIP2 = 0x02,
25   MDIP3 = 0x04,
26   MDIP4 = 0x08,
27   MDIP5 = 0x10,
28   MDIP6 = 0x20,
29   MDIP7 = 0x40,
30   MDIP8 = 0x80
31};
1832
19INLINE gen_memex_state *get_safe_token(device_t *device)
33geneve_memex_device::geneve_memex_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
34: ti_expansion_card_device(mconfig, TI99_MEMEX, "Geneve memory expansion card", tag, owner, clock)
2035{
21   assert(device != NULL);
22   assert(device->type() == GENMEMEX);
23
24   return (gen_memex_state *)downcast<legacy_device_base *>(device)->token();
36   m_shortname = "ti99_memex";
2537}
2638
27static int access_enabled(gen_memex_state *card, offs_t offset)
39bool geneve_memex_device::access_enabled(offs_t offset)
2840{
2941   // 1 0111 .... .... .... .... p-box address block 0xxx ... fxxx
3042   // first two bits are AME, AMD bits available on Genmod only
r14513r14514
3850   // SW2: "off" locks
3951   //      10xxx010
4052   //      10111010   also locked when "on"
41   if (page == 0xba) return FALSE;
42   if ((page & 0xc7)==0x82 && card->dip_switch[1]==FALSE)
43      return FALSE;
53   if (page == 0xba) return false;
54   if ((page & 0xc7)==0x82 && m_dip_switch[1]==false)
55   {
56      if (VERBOSE>8) LOG("geneve: memex blocks page %02x; dip1=%d\n", page,  m_dip_switch[1]);
57      return false;
58   }
4459
4560   // SW3: 111010xx    0=enabled 1=locked out
4661   // SW4: 111011xx
r14513r14514
5267   index = ((page >> 2)&0x3f);
5368   if (index >= 0x3a && index <= 0x3f)
5469   {
55      if (card->dip_switch[index - 0x38]==0) return TRUE;
70      if (m_dip_switch[index - 0x38]==0) return true;
5671      else
5772      {
58         return FALSE;
73         return false;
5974      }
6075   }
61   return TRUE;
76   return true;
6277}
6378
6479/*
r14513r14514
6984    Note that the incomplete decoding of the standard Geneve must be
7085    considered.
7186*/
72static READ8Z_DEVICE_HANDLER( memex_rz )
87READ8Z_MEMBER( geneve_memex_device::readz )
7388{
74   gen_memex_state *card = get_safe_token(device);
75
7689   /* If not Genmod, add the upper two address bits 10 */
77   if (!card->genmod) offset |= 0x100000;
90   if (!m_genmod) offset |= 0x100000;
7891
7992   // The card is accessed for all addresses in the address space
80   if (access_enabled(card, offset))
93   if (access_enabled(offset))
8194   {
82      *value = card->memory[offset];
95      *value = m_ram[offset];
8396   }
8497}
8598
8699/*
87100    Memory write
88101*/
89static WRITE8_DEVICE_HANDLER( memex_w )
102WRITE8_MEMBER( geneve_memex_device::write )
90103{
91   gen_memex_state *card = get_safe_token(device);
92
93104   /* If not Genmod, add the upper two address bits 10 */
94   if (!card->genmod) offset |= 0x100000;
105   if (!m_genmod) offset |= 0x100000;
95106
96107   // The card is accessed for all addresses in the address space
97   if (access_enabled(card, offset))
108   if (access_enabled(offset))
98109   {
99      card->memory[offset] = data;
110      m_ram[offset] = data;
100111   }
101112}
102113
103114/**************************************************************************/
104115
105static const ti99_peb_card memex_card =
116void geneve_memex_device::device_start()
106117{
107   memex_rz, memex_w,            // memory access read/write
108   NULL, NULL,                  // CRU access (none here)
109   NULL, NULL,                  // SENILA/B access (none here)
110   NULL, NULL                  // 16 bit access (none here)
111};
118   m_ram = subregion(RAMREGION)->base();
119}
112120
113static DEVICE_START( gen_memex )
121void geneve_memex_device::device_reset()
114122{
115   gen_memex_state *card = get_safe_token(device);
116   card->memory=NULL;
123   UINT8 dips = input_port_read(*this, "MEMEXDIPS");
124   if (VERBOSE>5) LOG("geneve: memex dips = %02x\n", dips);
125   for (int i=0; i < 8; i++)
126   {
127      m_dip_switch[i] = ((dips & 0x01)!=0x00);
128      dips = dips >> 1;
129   }
117130}
118131
119static DEVICE_STOP( gen_memex )
132INPUT_PORTS_START( memex )
133   PORT_START( "MEMEXDIPS" )
134   PORT_DIPNAME( MDIP1, MDIP1, "MEMEX SW1" )
135      PORT_DIPSETTING( 0x00, "LED half-bright for 0 WS")
136      PORT_DIPSETTING( MDIP1, "LED full-bright")
137   PORT_DIPNAME( MDIP2, 0x00, "MEMEX SW2" )
138      PORT_DIPSETTING( 0x00, "Lock out all BA mirrors")
139      PORT_DIPSETTING( MDIP2, "Lock out page BA only")
140   PORT_DIPNAME( MDIP3, 0x00, "MEMEX SW3" )
141      PORT_DIPSETTING( 0x00, "Enable pages E8-EB")
142      PORT_DIPSETTING( MDIP3, "Lock out pages E8-EB")
143   PORT_DIPNAME( MDIP4, 0x00, "MEMEX SW4" )
144      PORT_DIPSETTING( 0x00, "Enable pages EC-EF")
145      PORT_DIPSETTING( MDIP4, "Lock out pages EC-EF")
146   PORT_DIPNAME( MDIP5, 0x00, "MEMEX SW5" )
147      PORT_DIPSETTING( 0x00, "Enable pages F0-F3")
148      PORT_DIPSETTING( MDIP5, "Lock out pages F0-F3")
149   PORT_DIPNAME( MDIP6, 0x00, "MEMEX SW6" )
150      PORT_DIPSETTING( 0x00, "Enable pages F4-F7")
151      PORT_DIPSETTING( MDIP6, "Lock out pages F4-F7")
152   PORT_DIPNAME( MDIP7, 0x00, "MEMEX SW7" )
153      PORT_DIPSETTING( 0x00, "Enable pages F8-FB")
154      PORT_DIPSETTING( MDIP7, "Lock out pages F8-FB")
155   PORT_DIPNAME( MDIP8, 0x00, "MEMEX SW8" )
156      PORT_DIPSETTING( 0x00, "Enable pages FC-FF")
157      PORT_DIPSETTING( MDIP8, "Lock out pages FC-FF")
158INPUT_PORTS_END
159
160ROM_START( memex )
161   ROM_REGION(MEMEX_SIZE, RAMREGION, 0)
162   ROM_FILL(0x000000, MEMEX_SIZE, 0x00)
163ROM_END
164
165ioport_constructor geneve_memex_device::device_input_ports() const
120166{
121   gen_memex_state *card = get_safe_token(device);
122   if (card->memory) free(card->memory);
167   return INPUT_PORTS_NAME( memex );
123168}
124169
125static DEVICE_RESET( gen_memex )
170const rom_entry *geneve_memex_device::device_rom_region() const
126171{
127   gen_memex_state *card = get_safe_token(device);
128   /* Register the card */
129   device_t *peb = device->owner();
130
131   if (input_port_read(device->machine(), "EXTRAM")==1)
132   {
133      int success = mount_card(peb, device, &memex_card, get_pebcard_config(device)->slot);
134      if (!success) return;
135
136      if (card->memory==NULL)
137      {
138         // Allocate 2 MiB
139         card->memory = (UINT8*)malloc(0x200000);
140      }
141
142      UINT8 dips = input_port_read(device->machine(), "MEMEXDIPS");
143      for (int i=0; i < 8; i++)
144      {
145         card->dip_switch[i] = ((dips & 0x01)!=0x00);
146         dips = dips >> 1;
147      }
148
149      card->genmod = input_port_read(device->machine(), "MODE");
150   }
172   return ROM_NAME( memex );
151173}
152174
153static const char DEVTEMPLATE_SOURCE[] = __FILE__;
154
155#define DEVTEMPLATE_ID(p,s)             p##gen_memex##s
156#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
157#define DEVTEMPLATE_NAME                "Geneve Memex Memory Expansion Card"
158#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
159#include "devtempl.h"
160
161DEFINE_LEGACY_DEVICE( GENMEMEX, gen_memex );
162
175const device_type TI99_MEMEX = &device_creator<geneve_memex_device>;
src/mess/machine/ti99/mapper8.c
r14513r14514
3636    [2] TI-99/8 Graphics Programming Language interpreter
3737
3838    Michael Zapf, October 2010
39    February 2012: Rewritten as class
3940
4041***************************************************************************/
4142
42#include "emu.h"
43#include "peribox.h"
4443#include "mapper8.h"
4544
46#define MAXLOGDEV 20
47#define MAXPHYSDEV 10
45#define VERBOSE 1
46#define LOG logerror
4847
49#define SRAM_SIZE 2048
50
51#define DRAM_SIZE 65536
52
53#define MAPPER_CRU_BASE 0x2700
54
55/* Pseudo devices which are not implemented by a proper device. We use these
56constants in the read/write functions. */
48// Pseudo devices which are not implemented by a proper device. We use these
49// constants in the read/write functions.
5750#define MAP8_SRAM (void*)1L
5851#define MAP8_ROM0 (void*)2L
5952#define MAP8_ROM1 (void*)3L
r14513r14514
6154#define MAP8_DRAM (void*)5L
6255#define MAP8_INTS (void*)6L
6356
64typedef struct _log_addressed_device
57ti998_mapper_device::ti998_mapper_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
58: bus8z_device(mconfig, MAPPER8, "TI-99/8 Memory mapper", tag, owner, clock)
6559{
66   /* The device. */
67   device_t   *device;
60}
6861
69   /* Mode. */
70   int            mode;
62/***************************************************************************
63    CRU access
64***************************************************************************/
7165
72   /* Stop. If true, stop the address decoder here and do not look for parallel devices. */
73   int            stop;
66#define MAPPER_CRU_BASE 0x2700
7467
75   /* Address bits involved */
76   UINT16         address_mask;
68void ti998_mapper_device::crureadz(offs_t offset, UINT8 *value)
69{
70   if (VERBOSE>8) LOG("mapper8: read CRU %04x ignored\n", offset);
71   // Nothing here.
72}
7773
78   /* Value of the address bits which are involved for selecting for read. */
79   UINT16         select;
74/*
75    CRU handling. We handle the internal device at CRU address 0x2700 via
76    this mapper component.
77*/
78void ti998_mapper_device::cruwrite(offs_t offset, UINT8 data)
79{
80   if ((offset & 0xff00)==MAPPER_CRU_BASE)
81   {
82      int bit = (offset & 0xff)>>1;
83      switch (bit)
84      {
85      case 0:
86         // Turn on/off the internal DSR
87         m_dsr_selected = (data!=0);
88         if (VERBOSE>7) LOG("mapper8: DSR select = %d\n", data);
89         break;
90      case 1:
91         if (VERBOSE>2) LOG("mapper8: System reset by CRU request\n");
92         machine().schedule_soft_reset();
93         break;
94      }
95   }
96}
8097
81   /* Value of the address bits which are involved for selecting for write. */
82   UINT16         write_select;
98void ti998_mapper_device::CRUS_set(bool state)
99{
100   if (VERBOSE>7) LOG("mapper8: set CRUS=%d\n", state);
101   m_CRUS = state;
102}
83103
84   /* Read access. */
85   mapper_read_function read_byte;
104/*
105    Note that PTGEN is negative logic. We invert these semantics here.
106*/
107void ti998_mapper_device::PTGE_set(bool state)
108{
109   if (VERBOSE>7) LOG("mapper8: set PTGEN=%d\n", state? 1:0);
110   m_PTGE = state;
111}
86112
87   /* Write access. */
88   mapper_write_function write_byte;
113/***************************************************************************
114    Access by address map
115***************************************************************************/
89116
90} log_addressed_device;
91
92typedef struct _phys_addressed_device
117/*
118    This method is called via the address map.
119*/
120READ8_MEMBER( ti998_mapper_device::readm )
93121{
94   /* The device. */
95   device_t   *device;
122   UINT8 value = 0;
123   bool found = false;
96124
97   /* Stop. If true, stop the address decoder here and do not look for parallel devices. */
98   int            stop;
125   found = search_logically_addressed_r(space, offset, &value, mem_mask);
99126
100   /* Address bits involved */
101   UINT32         address_mask;
127   if (!found)
128   {
129      // In that case, the address decoder could not find a suitable device.
130      // This means the logical address is transformed by the mapper.
131      // NOTE: Use "+", not OR. The offset is not a prefix.
132      UINT32   pas_address = m_pas_offset[(offset & 0xf000)>>12] + (offset & 0xfff);
102133
103   /* Value of the address bits which are involved for selecting for read. */
104   UINT32         select;
134      // So now let's do the same as above with physical addresses
135      search_physically_addressed_r(space, pas_address, &value, mem_mask);
136   }
137   return value;
138}
105139
106   /* Read access. */
107   mapper_read_function read_byte;
108
109   /* Write access. */
110   mapper_write_function write_byte;
111
112} phys_addressed_device;
113
114typedef struct _mapper8_state
140WRITE8_MEMBER( ti998_mapper_device::writem )
115141{
116   /* All devices that are attached to the 16-bit address bus. */
117   log_addressed_device logcomp[MAXLOGDEV];
142   bool found = false;
118143
119   /* All devices that are attached to the 24-bit mapped address bus. */
120   phys_addressed_device physcomp[MAXPHYSDEV];
144   // Look for components responding to the logical address
145   found = search_logically_addressed_w(space, offset, data, mem_mask);
121146
122   /* The I/O port. */
123   device_t   *ioport;
147   if (!found)
148   {
149      // In that case, the address decoder could not find a suitable device.
150      // This means the logical address is transformed by the mapper.
151      // NOTE: Use "+", not OR. The offset is not a prefix.
152      UINT32 pas_address = m_pas_offset[(offset & 0xf000)>>12] + (offset & 0xfff);
124153
125   /* Select bit for the internal DSR. */
126   int   dsr_selected;
127
128   /* 99/4A compatibility mode. Name is taken from the spec. If 1, 99/4A compatibility is active. */
129   int   CRUS;
130
131   /* P-Code mode. Name is taken from the spec. If 0, P-Code libraries are available. */
132   // May be read as "Pascal and Text-to-speech GROM libraries ENable"
133   // Note: this is negative logic. GROMs are present only for PTGEN=0
134   int PTGEN;
135
136   /* Address mapper registers. Each offset is selected by the first 4 bits
137       of the logical address. */
138   UINT32   pas_offset[16];
139
140   /* SRAM area of the system. Directly connected to the address decoder. */
141   UINT8   *sram;
142
143   /* DRAM area of the system. Connected to the mapped address bus. */
144   UINT8   *dram;
145
146   /* ROM area of the system. Directly connected to the address decoder. */
147   UINT8   *rom;
148
149   /* Points to the next free slot in the list. */
150   int logindex, physindex;
151
152} mapper8_state;
153
154
155INLINE mapper8_state *get_safe_token(device_t *device)
156{
157   assert(device != NULL);
158   assert(device->type() == MAPPER8);
159
160   return (mapper8_state *)downcast<legacy_device_base *>(device)->token();
154      // So now let's do the same as above with physical addresses
155      search_physically_addressed_w(space, pas_address, data, mem_mask);
156   }
161157}
162158
163static void mapper_mount_logical_device(device_t *mapperdev, device_t *busdevice, int mode, int stop, UINT16 address_mask, UINT16 address_bits, UINT16 write_sel, mapper_read_function read, mapper_write_function write)
159/***************************************************************************
160    Indirect calls (mapper calls itself)
161***************************************************************************/
162/*
163    This method is called by the mapper itself for
164    f870 (NATIVE): mapper: ignore
165    8810 (TI99EM): mapper: ignore
166    ff4000 (PHYSIC): DSR
167*/
168READ8Z_MEMBER( ti998_mapper_device::readz )
164169{
165   mapper8_state *mapper = get_safe_token(mapperdev);
166   int index = mapper->logindex++;
167   if (index < MAXLOGDEV)
170   if ((offset & 0xffe000)==0xff4000)
168171   {
169      if (mapper->logcomp[index].device == NULL)
172      if (m_dsr_selected)
170173      {
171         mapper->logcomp[index].device = busdevice;
172         mapper->logcomp[index].mode = mode;
173         mapper->logcomp[index].stop = stop;
174
175         mapper->logcomp[index].address_mask = address_mask;
176         mapper->logcomp[index].select = address_bits;
177         mapper->logcomp[index].write_select = write_sel;
178         mapper->logcomp[index].read_byte = read;
179         mapper->logcomp[index].write_byte = write;
180         logerror("ti99_8: registering a device at logical position %d\n", index);
174         //  Starts at 0x4000 in the image
175         *value = m_rom[0x4000 | (offset & 0x1fff)];
176         if (VERBOSE>7) LOG("mapper8: (DSR)  %04x -> %02x\n", offset, *value);
181177      }
182178   }
183179   else
184180   {
185      logerror("ti99_8_mapper: Tried to mount too many logical devices to the mapper. (Actually a bug.)\n");
181      if (((offset & 0xfff0)==0xf870 && m_CRUS==false)||(((offset & 0xfff0)==0x8810 && m_CRUS==true)))
182      {
183         if (VERBOSE>4) LOG("mapper8: read access to mapper ignored: %04x\n", offset);
184      }
186185   }
187186}
188187
189static void mapper_mount_physical_device(device_t *mapperdev, device_t *busdevice, int stop, UINT32 address_mask, UINT32 address_bits, mapper_read_function read, mapper_write_function write)
188/*
189    This method is called by the mapper itself for
190    ff4000 (PHYSIC): DSR. ignore
191
192*/
193WRITE8_MEMBER( ti998_mapper_device::write )
190194{
191   mapper8_state *mapper = get_safe_token(mapperdev);
192   int index = mapper->physindex++;
193   if (index < MAXPHYSDEV)
195   if ((offset & 0xffe000)==0xff4000)
194196   {
195      if (mapper->physcomp[index].device == NULL)
197      if (VERBOSE>4) LOG("mapper8: Write access to DSR space %06x ignored\n", offset);
198   }
199   else
200   {
201      if (((offset & 0xfff0)==0xf870 && m_CRUS==false)||(((offset & 0xfff0)==0x8810 && m_CRUS==true)))
196202      {
197         mapper->physcomp[index].device = busdevice;
198         mapper->physcomp[index].stop = stop;
199         mapper->physcomp[index].address_mask = address_mask;
200         mapper->physcomp[index].select = address_bits;
201         mapper->physcomp[index].read_byte = read;
202         mapper->physcomp[index].write_byte = write;
203         logerror("ti99_8: registering a device at physical position %d\n", index);
203         mapwrite(offset, data);
204204      }
205205   }
206   else
206}
207
208/*
209    Reconfigure mapper. Writing to this address copies the values in the
210    SRAM into the mapper and vice versa.
211    Format:
212    0000 bbbl; bbb=bank, l=load
213*/
214void ti998_mapper_device::mapwrite(int offset, UINT8 data)
215{
216   if ((data & 0xf0)==0x00)
207217   {
208      logerror("ti99_8_mapper: Tried to mount too many phys devices to the mapper. (Actually a bug.)\n");
218      int bankindx = (data & 0x0e)>>1;
219      if (data & 1)
220      {
221         if (VERBOSE>7) LOG("mapper8: load mapper from SRAM, bank %d\n", bankindx);
222         // Load from SRAM
223         // In reality the CPU is put on HOLD during this transfer
224         for (int i=0; i < 16; i++)
225         {
226            int ptr = (bankindx << 6);
227            m_pas_offset[i] =   (m_sram[(i<<2) + ptr] << 24) | (m_sram[(i<<2)+ ptr+1] << 16)
228            | (m_sram[(i<<2) + ptr+2] << 8) | (m_sram[(i<<2) + ptr+3]);
229         }
230      }
231      else
232      {
233         if (VERBOSE>7) LOG("mapper8: store mapper to SRAM, bank %d\n", bankindx);
234         // Store in SRAM
235         for (int i=0; i < 16; i++)
236         {
237            int ptr = (bankindx << 6);
238            m_sram[(i<<2) + ptr]    =  (m_pas_offset[i] >> 24)& 0xff;
239            m_sram[(i<<2) + ptr +1] =  (m_pas_offset[i] >> 16)& 0xff;
240            m_sram[(i<<2) + ptr +2] =  (m_pas_offset[i] >> 8)& 0xff;
241            m_sram[(i<<2) + ptr +3] =  (m_pas_offset[i])& 0xff;
242         }
243      }
209244   }
210245}
211246
212247/***************************************************************************
213    DEVICE ACCESSOR FUNCTIONS
248    Lookup methods.
214249***************************************************************************/
215250
216static int check_logically_addressed_r( device_t *device, offs_t offset, UINT8 *value)
251bool ti998_mapper_device::search_logically_addressed_r(address_space& space, offs_t offset, UINT8 *value, UINT8 mem_mask )
217252{
218   mapper8_state *mapper = get_safe_token(device);
219   int found = FALSE;
220   // Look for components responding to the logical address
221   for (int i=0; i < MAXLOGDEV; i++)
253   bool found = false;
254   log_addressed_device *dev = m_logcomp.first();
255
256   if (VERBOSE>8) LOG("mapper8: offset=%04x; CRUS=%d, PTGEN=%d\n", offset, m_CRUS? 1:0, m_PTGE? 0:1);
257   while (dev != NULL)
222258   {
223      log_addressed_device *adev = &mapper->logcomp[i];
224      if (adev->device != NULL)
259      // Check the mode
260      if (((dev->m_config->mode == NATIVE) && (m_CRUS==false))
261         || ((dev->m_config->mode == TI99EM) && (m_CRUS==true))
262         || ((dev->m_config->mode == PATGEN) && (m_PTGE==true)))
225263      {
226         // Check for the mode
227         if (adev->mode == mapper->CRUS || (mapper->CRUS==0 && adev->mode==1 && mapper->PTGEN==FALSE))
264         if ((offset & dev->m_config->address_mask)==dev->m_config->select_pattern)
228265         {
229            if ((offset & adev->address_mask)==adev->select)
266            if (dev->m_device == MAP8_SRAM)
230267            {
231               if (adev->device == MAP8_SRAM)
268               *value = m_sram[offset & ~dev->m_config->address_mask];
269               if (VERBOSE>7) LOG("mapper8: (SRAM) %04x -> %02x\n", offset, *value);
270            }
271            else
272            {
273               if (dev->m_device == MAP8_ROM0)
232274               {
233                  *value = mapper->sram[offset & ~adev->address_mask];
234                  found = TRUE;
275                  // Starts at 0000
276                  *value = m_rom[offset & ~dev->m_config->address_mask];
277                  if (VERBOSE>7) LOG("mapper8: (ROM)  %04x -> %02x\n", offset, *value);
235278               }
236279               else
237280               {
238                  if (adev->device == MAP8_ROM0)
239                  {
240                     // Starts at 0000
241                     *value = mapper->rom[offset & ~adev->address_mask];
242                     found = TRUE;
243                  }
244                  else
245                  {
246                     // device
247                     if (adev->read_byte != NULL)
248                     {
249                        adev->read_byte(adev->device, offset, value);
250                        found = TRUE;
251                     }
252                  }
281                  // device
282                  bus8z_device *bdev = static_cast<bus8z_device*>(dev->m_device);
283                  bdev->readz(space, offset, value, mem_mask);
284                  if (VERBOSE>7) LOG("mapper8: (dev %s)  %04x -> %02x\n", dev->m_config->name, offset, *value);
253285               }
254286            }
287            found = true;
288            if (dev->m_config->stop==STOP) break;
255289         }
256290      }
257      if (found && adev->stop) break;
291      dev = dev->m_next;
258292   }
259293   return found;
260294}
261295
262static int check_logically_addressed_w( device_t *device, offs_t offset, UINT8 data)
296bool ti998_mapper_device::search_logically_addressed_w(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask )
263297{
264   mapper8_state *mapper = get_safe_token(device);
265   int found = FALSE;
298   bool found = false;
299   log_addressed_device *dev = m_logcomp.first();
266300
267   for (int i=0; i < MAXLOGDEV; i++)
301   while (dev != NULL)
268302   {
269      log_addressed_device *adev = &mapper->logcomp[i];
270      if (adev->device != NULL)
303      // Check the mode
304      if (((dev->m_config->mode == NATIVE) && (m_CRUS==false))
305         || ((dev->m_config->mode == TI99EM) && (m_CRUS==true))
306         || ((dev->m_config->mode == PATGEN) && (m_PTGE==true)))
271307      {
272         // Check for the mode
273         if (adev->mode == mapper->CRUS ||  (mapper->CRUS==0 && adev->mode==1 && mapper->PTGEN==FALSE))
308
309         if ((offset & dev->m_config->address_mask)==(dev->m_config->select_pattern | dev->m_config->write_select))
274310         {
275            if ((offset & adev->address_mask)==(adev->select | adev->write_select))
311            if (dev->m_device == MAP8_SRAM)
276312            {
277               if (adev->device == MAP8_SRAM)
313               m_sram[offset & ~dev->m_config->address_mask] = data;
314               if (VERBOSE>7) LOG("mapper8: (SRAM) %04x <- %02x\n", offset, data);
315            }
316            else
317            {
318               if (dev->m_device == MAP8_ROM0)
278319               {
279                  mapper->sram[offset & ~adev->address_mask] = data;
280                  found = TRUE;
320                  if (VERBOSE>7) LOG("mapper8: (ROM)  %04x <- %02x (ignored)\n", offset, data);
281321               }
282322               else
283323               {
284                  if (adev->device == MAP8_ROM0)
285                  {
286                     found = TRUE;
287                  }
288                  else
289                  {
290                     if (adev->write_byte != NULL)
291                     {
292                        adev->write_byte(adev->device, offset, data);
293                        found = TRUE;
294                     }
295                  }
324                  // device
325                  bus8z_device *bdev = static_cast<bus8z_device*>(dev->m_device);
326                  bdev->write(space, offset, data, mem_mask);
327                  if (VERBOSE>7) LOG("mapper8: (dev %s)  %04x <- %02x\n", dev->m_config->name, offset, data);
296328               }
297329            }
330            found = true;
331            if (dev->m_config->stop==STOP) break;
298332         }
299333      }
300      if (found && adev->stop) break;
334      dev = dev->m_next;
301335   }
302336   return found;
303337}
304338
305/*
306    Read access.
307*/
308READ8_DEVICE_HANDLER( ti99_mapper8_r )
339
340void ti998_mapper_device::search_physically_addressed_r( address_space& space, offs_t pas_address, UINT8 *value, UINT8 mem_mask )
309341{
310   UINT8 value = 0;
311   mapper8_state *mapper = get_safe_token(device);
312   int found = FALSE;
342   phys_addressed_device *dev = m_physcomp.first();
313343
314   if (mapper->sram==NULL)  // not initialized yet
315      return 0;
316
317   found = check_logically_addressed_r(device, offset, &value);
318
319   if (!found)
344   while (dev != NULL)
320345   {
321      // In that case, the address decoder could not find a suitable device.
322      // This means the logical address is transformed by the mapper.
323      UINT32   pas_address = mapper->pas_offset[(offset & 0xf000)>>12] + (offset & 0xfff);
324
325      // So now let's do the same as above with physical addresses
326      for (int i=0; i < MAXPHYSDEV; i++)
346      if ((pas_address & dev->m_config->address_mask)==dev->m_config->select_pattern)
327347      {
328         phys_addressed_device *adev = &mapper->physcomp[i];
329         if (adev->device != NULL)
348         if (dev->m_device == MAP8_DRAM)
330349         {
331            if ((pas_address & adev->address_mask)==adev->select)
350            *value = m_dram[pas_address & ~dev->m_config->address_mask];
351            if (VERBOSE>3) LOG("mapper8: (DRAM) %06x -> %02x\n", pas_address, *value);
352         }
353         else
354         {
355            if (dev->m_device == MAP8_ROM1)
332356            {
333               if (adev->device == MAP8_DRAM)
357               // Starts at 2000 in the image, 8K
358               *value = m_rom[0x2000 | (pas_address & 0x1fff)];
359               if (VERBOSE>3) LOG("mapper8: (ROM) %06x -> %02x\n", pas_address, *value);
360            }
361            else
362            {
363               if (dev->m_device == MAP8_ROM1A)
334364               {
335                  value = mapper->dram[pas_address & ~adev->address_mask];
365                  // Starts at 6000 in the image, 8K
366                  *value = m_rom[0x6000 | (pas_address & 0x1fff)];
367                  if (VERBOSE>3) LOG("mapper8: (ROM)  %06x -> %02x\n", pas_address, *value);
336368               }
337369               else
338370               {
339                  if (adev->device == MAP8_ROM1)
371                  if (dev->m_device == MAP8_INTS)
340372                  {
341                     // Starts at 2000 in the image, 8K
342                     value = mapper->rom[0x2000 + (pas_address & 0x1fff)];
373                     // Interrupt sense
374                     if (VERBOSE>1) LOG("ti99_8: ILSENSE not implemented.\n");
343375                  }
344376                  else
345377                  {
346                     if (adev->device == MAP8_ROM1A)
347                     {
348                        // Starts at 6000 in the image, 8K
349                        value = mapper->rom[0x6000 + (pas_address & 0x1fff)];
350                     }
351                     else
352                     {
353                        if (adev->device == MAP8_INTS)
354                        {
355                           // Interrupt sense
356                           logerror("ti99_8: ILSENSE not implemented.\n");
357                        }
358                        else
359                        {
360                           // devices
361                           if (adev->read_byte != NULL)
362                           {
363                              adev->read_byte(adev->device, pas_address, &value);
364                           }
365                        }
366                     }
378                     // devices
379                     bus8z_device *bdev = static_cast<bus8z_device*>(dev->m_device);
380                     bdev->readz(space, pas_address, value, mem_mask);
381                     if (VERBOSE>7) LOG("mapper8: (dev %s)  %06x -> %02x\n", dev->m_config->name, pas_address, *value);
367382                  }
368383               }
369               if (adev->stop) break;
370384            }
371385         }
386         if (dev->m_config->stop==STOP) break;
372387      }
388      dev = dev->m_next;
373389   }
374
375   return value;
376390}
377391
378/*
379    Write access.
380*/
381WRITE8_DEVICE_HANDLER( ti99_mapper8_w )
392void ti998_mapper_device::search_physically_addressed_w( address_space& space, offs_t pas_address, UINT8 data, UINT8 mem_mask )
382393{
383   mapper8_state *mapper = get_safe_token(device);
384   int found = FALSE;
385   if (mapper->sram==NULL)
386      return;
394   phys_addressed_device *dev = m_physcomp.first();
387395
388   // Look for components responding to the logical address
389   found = check_logically_addressed_w(device, offset, data);
390
391   if (!found)
396   while (dev != NULL)
392397   {
393      // In that case, the address decoder could not find a suitable device.
394      // This means the logical address is transformed by the mapper.
395      UINT32 pas_address = mapper->pas_offset[(offset & 0xf000)>>12] + (offset & 0xfff);
396
397      // So now let's do the same as above with physical addresses
398      for (int i=0; i < MAXPHYSDEV; i++)
398      if ((pas_address & dev->m_config->address_mask)==(dev->m_config->select_pattern | dev->m_config->write_select))
399399      {
400         phys_addressed_device *adev = &mapper->physcomp[i];
401         if (adev->device != NULL)
400         if (dev->m_device == MAP8_DRAM)
402401         {
403            if ((pas_address & adev->address_mask)==adev->select)
402            m_dram[pas_address & ~dev->m_config->address_mask] = data;
403            if (VERBOSE>3) LOG("mapper8: (DRAM) %06x <- %02x\n", pas_address, data);
404         }
405         else
406         {
407            if (dev->m_device == MAP8_ROM1 || dev->m_device == MAP8_ROM1A)
404408            {
405               if (adev->device == MAP8_DRAM)
409               if (VERBOSE>7) LOG("mapper8: (ROM)  %06x <- %02x (ignored)\n", pas_address, data);
410            }
411            else
412            {
413               if (dev->m_device == MAP8_INTS)
406414               {
407                  mapper->dram[pas_address & ~adev->address_mask] = data;
415                  // Interrupt sense
416                  if (VERBOSE>1) LOG("ti99_8: write to ilsense ignored\n");
408417               }
409418               else
410419               {
411                  if (adev->device == MAP8_ROM1 || adev->device == MAP8_ROM1A)
412                  {
413                     logerror("ti99_8: write to rom1 ignored\n");
414                  }
415                  else
416                  {
417                     if (adev->device == MAP8_INTS)
418                     {
419                        // Interrupt sense
420                        logerror("ti99_8: write to ilsense ignored\n");
421                     }
422                     else
423                     {
424                        // cartridge port (aka grom port) or peribox
425                        if (adev->write_byte != NULL)
426                        {
427                           adev->write_byte(adev->device, pas_address, data);
428                        }
429                     }
430                  }
420                  // devices
421                  bus8z_device *bdev = static_cast<bus8z_device*>(dev->m_device);
422                  if (VERBOSE>7) LOG("mapper8: (dev %s)  %06x <- %02x\n", dev->m_config->name, pas_address, data);
423                  bdev->write(space, pas_address, data, mem_mask);
431424               }
432               if (adev->stop) break;
433425            }
434426         }
427         if (dev->m_config->stop==STOP) break;
435428      }
429      dev = dev->m_next;
436430   }
437431}
438432
439/*
440    Reconfigure mapper. Writing to this address copies the values in the
441    SRAM into the mapper and vice versa.
442    Format:
443    0000 bbbl; bbb=bank, l=load
444*/
445WRITE8_DEVICE_HANDLER( ti99_mapreg_w )
446{
447   mapper8_state *mapper = get_safe_token(device);
433/***************************************************************************
434    DEVICE LIFECYCLE FUNCTIONS
435***************************************************************************/
436// String values of the pseudo constants, used in the configuration.
437static const char *const pseudodev[6] = { SRAMNAME, ROM0NAME, ROM1NAME, ROM1ANAME, DRAMNAME, INTSNAME };
448438
449   if ((data & 0xf0)==0)
450   {
451      int bankindx = (data & 0x0e)>>1;
452      if (data & 1)
453      {
454         // Load from SRAM
455         for (int i=0; i < 16; i++)
456         {
457            int ptr = (bankindx << 6);
458            mapper->pas_offset[i] =
459               (mapper->sram[i*4 + ptr] << 24)
460               + (mapper->sram[i*4 + ptr+1] << 16)
461               + (mapper->sram[i*4 + ptr+2] << 8)
462               + (mapper->sram[i*4 + ptr+3]);
463         }
464      }
465      else
466      {
467         // Store in SRAM
468         for (int i=0; i < 16; i++)
469         {
470            int ptr = (bankindx << 6);
471            mapper->sram[i*4 + ptr]    =  (mapper->pas_offset[i] >> 24)& 0xff;
472            mapper->sram[i*4 + ptr +1] =  (mapper->pas_offset[i] >> 16)& 0xff;
473            mapper->sram[i*4 + ptr +2] =  (mapper->pas_offset[i] >> 8)& 0xff;
474            mapper->sram[i*4 + ptr +3] =  (mapper->pas_offset[i])& 0xff;
475         }
476      }
477   }
478}
479
480/************************************************************************/
481
482439/*
483    Internal DSR
484*/
485READ8Z_DEVICE_HANDLER( ti998dsr_rz )
486{
487   mapper8_state *mapper = get_safe_token(device);
488   if (mapper->dsr_selected)
489   {
490      //  Starts at 0x4000 in the image
491      *value = mapper->rom[0x4000 + (offset & 0x1fff)];
492   }
493}
440    We need to do all of the configuration in device_start since we don't have all
441    required links earlier.
494442
495
496/*
497    CRU handling. We handle the internal device at CRU address 0x2700 via
498    this mapper component.
443    Note that device_reset is too late; the initial context switch occurs earlier.
499444*/
500WRITE8_DEVICE_HANDLER( mapper8c_w )
445void ti998_mapper_device::device_start()
501446{
502   mapper8_state *mapper = get_safe_token(device);
503   if ((offset & 0xff00)==MAPPER_CRU_BASE)
504   {
505      int addr = offset & 0x00ff;
506      if (addr==0)
507         mapper->dsr_selected = data;
508      if (addr==2)
509      {
510         // TODO
511         logerror("ti99_8: Hardware reset via CRU not implemented\n");
512      }
513   }
514}
447   if (VERBOSE>5) LOG("ti99_8: Starting mapper\n");
515448
516WRITE8_DEVICE_HANDLER( mapper8_CRUS_w )
517{
518   mapper8_state *mapper = get_safe_token(device);
519   mapper->CRUS = data;
520}
449   const mapper8_list_entry *entry = reinterpret_cast<const mapper8_list_entry *>(static_config());
450   m_sram = machine().region(SRAM_TAG)->base();
451   m_dram = machine().region(DRAM_TAG)->base();
452   m_rom  = machine().region("maincpu")->base();
521453
522WRITE8_DEVICE_HANDLER( mapper8_PTGEN_w )
523{
524   mapper8_state *mapper = get_safe_token(device);
525   mapper->PTGEN = data;
526}
454   // Clear the lists
455   m_logcomp.reset();
456   m_physcomp.reset();
527457
528/***************************************************************************
529    DEVICE LIFECYCLE FUNCTIONS
530***************************************************************************/
531
532static DEVICE_START( mapper8 )
533{
534   logerror("ti99_8: Starting mapper\n");
535   mapper8_state *mapper = get_safe_token(device);
536   mapper->sram = (UINT8*)malloc(SRAM_SIZE);
537   mapper->dram = (UINT8*)malloc(DRAM_SIZE);
538   mapper->rom = device->machine().region("maincpu")->base();
539
540   memset(mapper->sram, 0x00, SRAM_SIZE);
541   memset(mapper->dram, 0x00, DRAM_SIZE);
542
543   int i = 0;
544   int done=FALSE;
545
546   for (int index = 0; index < MAXLOGDEV; index++)
458   // Now building the list of active devices at this mapper.
459   // Coyping partly from datamux.c.
460   if ( entry != NULL )
547461   {
548      mapper->logcomp[index].device = NULL;
549   }
550
551   for (int index = 0; index < MAXPHYSDEV; index++)
552   {
553      mapper->physcomp[index].device = NULL;
554   }
555
556   // Need to connect the callbacks
557   mapper->ioport = device->siblingdevice("peribox");
558   assert(mapper->ioport != NULL);
559
560   mapper8_dev_config *cons = (mapper8_dev_config*)device->static_config();
561
562   mapper->logindex = 0;
563   mapper->physindex = 0;
564
565   /* String values of the pseudo constants, used in the configuration.
566    Here we replace the device names by their pointers and the pseudo device
567    names by their constants MAP8_SRAM ... MAP8_INTS. */
568   static const char *const list[6] = { SRAMNAME, ROM0NAME, ROM1NAME, ROM1ANAME, DRAMNAME, INTSNAME };
569   while (!done)
570   {
571      void *dev = NULL;
572      if (cons[i].name==NULL)
573         done = TRUE;
574      else
462      bool done = false;
463      for (int i=0; !done; i++)
575464      {
576         for (long long j=1; (j < 7) && (dev == NULL); j++)
465         if (entry[i].name == NULL)
577466         {
578            // Pseudo devices are enumerated as 1 ... 6 (see MAP8_SRAM etc.)
579            if (strcmp(cons[i].name, list[j-1])==0)
580               dev = (void *)j;
467            done = true;
581468         }
582         if (dev==NULL)
583            // Real devices use their address to be accessed
584            dev = device->machine().device(cons[i].name);
585
586         if (dev!=NULL)
469         else
587470         {
588            if (cons[i].mode != 3)
471            // Here we replace the device names by their pointers and the pseudo device
472            // names by their constants MAP8_SRAM ... MAP8_INTS. */
473
474            // To make thing efficient, we abuse the pointers with special
475            // values for the ROM and RAM. (Otherwise we would have to
476            // build complete devices for ROM and RAM.)
477            void *dev = NULL;
478            for (long long j=1; (j < 7) && (dev == NULL); j++)
589479            {
590               mapper_mount_logical_device(device, (device_t*)dev, cons[i].mode, cons[i].stop, (UINT16)cons[i].address_mask, (UINT16)cons[i].select_pattern, (UINT16)cons[i].write_select, cons[i].read, cons[i].write);
480               // Pseudo devices are enumerated as 1 ... 6 (see MAP8_SRAM etc.)
481               if (strcmp(entry[i].name, pseudodev[j-1])==0)
482                  dev = (void *)j;
591483            }
484            if (dev==NULL)
485            {
486               // This entry points to a "real" device, not to a special constant
487               dev = machine().device(entry[i].name);
488            }
489            if (dev != NULL)
490            {
491               if (entry[i].mode != PHYSIC)
492               {
493                  log_addressed_device *ad = new log_addressed_device((device_t*)dev, entry[i]);
494                  m_logcomp.append(*ad);
495                  if (VERBOSE>6) LOG("mapper8: Device %s mounted into logical address space.\n", entry[i].name);
496               }
497               else
498               {
499                  phys_addressed_device *ad = new phys_addressed_device((device_t*)dev, entry[i]);
500                  m_physcomp.append(*ad);
501                  if (VERBOSE>6) LOG("mapper8: Device %s mounted into physical address space.\n", entry[i].name);
502               }
503            }
592504            else
593505            {
594               mapper_mount_physical_device(device, (device_t*)dev, cons[i].stop, cons[i].address_mask, cons[i].select_pattern, cons[i].read, cons[i].write);
506               if (VERBOSE>1) LOG("mapper8: Device %s not found.\n", entry[i].name);
595507            }
596508         }
597         else
598            logerror("mapper8: Device %s not found.\n", cons[i].name);
599         i++;
600509      }
601510   }
602
603   mapper->dsr_selected = FALSE;
604   mapper->CRUS = TRUE;
605   mapper->PTGEN = TRUE;
511   if (VERBOSE>4) LOG("Mapper logical device count = %d\n", m_logcomp.count());
512   if (VERBOSE>4) LOG("Mapper physical device count = %d\n", m_physcomp.count());
606513}
607514
608static DEVICE_STOP( mapper8 )
515void ti998_mapper_device::device_reset()
609516{
610   logerror("ti99_8: Stopping mapper\n");
611   mapper8_state *mapper = get_safe_token(device);
612   free(mapper->sram);
613   free(mapper->dram);
614}
517   m_dsr_selected = false;
518   m_CRUS = true;
519   m_PTGE = false;
615520
616static DEVICE_RESET( mapper8 )
617{
618   logerror("ti99_8: Resetting mapper\n");
619   mapper8_state *mapper = get_safe_token(device);
620
621   mapper->dsr_selected = FALSE;
622   mapper->CRUS = TRUE;
623   mapper->PTGEN = TRUE;
521   // Clean mapper
522   for (int i=0; i < 16; i++) m_pas_offset[i] = 0;
624523}
625524
626static const char DEVTEMPLATE_SOURCE[] = __FILE__;
627
628#define DEVTEMPLATE_ID(p,s)             p##mapper8##s
629#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET
630#define DEVTEMPLATE_NAME                "TI-99/8 Address Decoder and Mapper"
631#define DEVTEMPLATE_FAMILY              "Internal devices"
632#include "devtempl.h"
633
634DEFINE_LEGACY_DEVICE( MAPPER8, mapper8 );
525const device_type MAPPER8 = &device_creator<ti998_mapper_device>;
src/mess/machine/ti99/ti_rs232.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Serial and parallel interface card
3    Michael Zapf, September 2010
4*/
4    See ti_rs232.c for documentation
55
6    Michael Zapf
7    February 2012: Rewritten as class
8
9*****************************************************************************/
10
611#ifndef __TIRS232__
712#define __TIRS232__
813
9DECLARE_LEGACY_DEVICE( TIRS232, ti_rs232 );
14#include "emu.h"
15#include "peribox.h"
16#include "ti99defs.h"
17#include "machine/tms9902.h"
18
19extern const device_type TI99_RS232;
20extern const device_type TI99_RS232_DEV;
21extern const device_type TI99_PIO_DEV;
22
23class ti_rs232_attached_device;
24class ti_pio_attached_device;
25
26class ti_rs232_pio_device : public ti_expansion_card_device
27{
28   friend class ti_pio_attached_device;
29   friend class ti_rs232_attached_device;
30
31public:
32   ti_rs232_pio_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
33   DECLARE_READ8Z_MEMBER(readz);
34   DECLARE_WRITE8_MEMBER(write);
35
36   void crureadz(offs_t offset, UINT8 *value);
37   void cruwrite(offs_t offset, UINT8 data);
38
39   DECLARE_WRITE_LINE_MEMBER( int0_callback );
40   DECLARE_WRITE_LINE_MEMBER( int1_callback );
41   DECLARE_WRITE_LINE_MEMBER( rcv0_callback );
42   DECLARE_WRITE_LINE_MEMBER( rcv1_callback );
43   DECLARE_WRITE8_MEMBER( xmit0_callback );
44   DECLARE_WRITE8_MEMBER( xmit1_callback );
45   DECLARE_WRITE8_MEMBER( ctrl0_callback );
46   DECLARE_WRITE8_MEMBER( ctrl1_callback );
47   DECLARE_WRITE_LINE_MEMBER( senila );
48
49protected:
50   void device_start(void);
51   void device_reset(void);
52   const rom_entry *device_rom_region(void) const;
53   machine_config_constructor device_mconfig_additions() const;
54   ioport_constructor device_input_ports() const;
55
56private:
57   void      incoming_dtr(int uartind, line_state value);
58   void      transmit_data(int uartind, UINT8 value);
59   UINT8      map_lines_out(int uartind, UINT8 value);
60   UINT8      map_lines_in(int uartind, UINT8 value);
61   void      receive_data_or_line_state(int uartind);
62   void      set_bit(int uartind, int line, int value);
63
64   void      configure_interface(int uartind, int type, int value);
65   void      output_line_state(int uartind, int mask, UINT8 value);
66   void      output_exception(int uartind, int param, UINT8 value);
67   void      ctrl_callback(int uartind, int type, UINT8 data);
68
69   tms9902_device*            m_uart[2];
70   ti_rs232_attached_device*   m_serdev[2];
71   ti_pio_attached_device*      m_piodev;
72   address_space*            m_space;
73   UINT8*                  m_dsrrom;
74
75   UINT8   m_signals[2];   // Latches the state of the output lines for UART0/UART1
76   int      m_recv_mode[2];    // May be NORMAL or ESC
77
78   // Baud rate management
79   // not part of the real device, but required for the connection to the
80   // real UART
81   double   m_time_hold[2];
82
83   // PIO flags
84   bool   m_pio_direction_in;      // a.k.a. PIOOC pio in output mode if 0
85   bool   m_pio_handshakeout;
86   bool   m_pio_handshakein;
87   bool   m_pio_spareout;
88   bool   m_pio_sparein;
89   bool   m_flag0;            // spare
90   bool   m_led;            // a.k.a. flag3
91   int      m_pio_out_buffer;
92   int      m_pio_in_buffer;
93   bool   m_pio_readable;
94   bool   m_pio_writable;
95   bool   m_pio_write;         // true if image is to be written to
96
97   /* Keeps the value put on the bus when SENILA becomes active. */
98   UINT8   m_ila;
99};
100
101/****************************************************************************/
102
103/*
104    Defines the serial serdev. "TI99 RS232 attached serial device"
105*/
106class ti_rs232_attached_device : public device_t, public device_image_interface
107{
108public:
109   ti_rs232_attached_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
110
111   iodevice_t image_type() const { return IO_SERIAL; }
112   bool is_readable()  const         { return true; }
113   bool is_writeable() const         { return true; }
114   bool is_creatable() const         { return true; }
115   bool must_be_loaded() const       { return false; }
116   bool is_reset_on_load() const      { return false; }
117   const char *image_interface() const { return ""; }
118   const char *file_extensions() const { return ""; }
119   const option_guide *create_option_guide() const { return NULL; }
120
121protected:
122   void   device_start(void);
123   bool   call_load();
124   void   call_unload();
125   void   device_config_complete();
126
127private:
128   int get_index_from_tagname();
129};
130
131/*
132    Defines the PIO (parallel IO) "TI99 PIO attached device"
133*/
134class ti_pio_attached_device : public device_t, public device_image_interface
135{
136public:
137   ti_pio_attached_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
138
139   iodevice_t image_type() const { return IO_PARALLEL; }
140   bool is_readable()  const         { return true; }
141   bool is_writeable() const         { return true; }
142   bool is_creatable() const         { return true; }
143   bool must_be_loaded() const       { return false; }
144   bool is_reset_on_load() const      { return false; }
145   const char *image_interface() const { return ""; }
146   const char *file_extensions() const { return ""; }
147   const option_guide *create_option_guide() const { return NULL; }
148
149protected:
150   void   device_start(void);
151   bool   call_load();
152   void   call_unload();
153   void   device_config_complete();
154};
155
10156#endif
src/mess/machine/ti99/peribox.h
r14513r14514
1/****************************************************************************
2
3    Peripheral expansion box
4    See peribox.c for documentation
5
6    Michael Zapf
7
8    February 2012: Rewritten as class
9
10*****************************************************************************/
11
112#ifndef __PBOX__
213#define __PBOX__
3/*
4    header file for ti99_peb
5*/
6#include "p_code.h"
7#include "ti32kmem.h"
8#include "samsmem.h"
9#include "myarcmem.h"
14
1015#include "ti99defs.h"
1116
12/*
13    inta: callback called when the state of INTA changes (may be NULL)
14    intb: callback called when the state of INTB changes (may be NULL)
15    ready: callback called when the state of INTB changes (may be NULL)
16    amx: Determines whether the AMA, AMB, and AMC address bus extension lines
17         must be preset to some value. The standard TI Flex Cable Interface
18         locks these valuies to 1. The Geneve, however, can drive these lines.
19*/
17extern const device_type PERIBOX;
18extern const device_type PERIBOX_SLOT;
2019
21typedef struct _ti99_peb_config
20extern const device_type PERIBOX_EV;
21extern const device_type PERIBOX_SG;
22extern const device_type PERIBOX_GEN;
23
24#define DSRROM "dsrrom"
25
26typedef struct _peribox_config
2227{
23   write_line_device_func   inta;
24   write_line_device_func   intb;
25   write_line_device_func   ready;
26   int                  amx;
27} ti99_peb_config;
28   devcb_write_line   inta;
29   devcb_write_line   intb;
30   devcb_write_line   ready;
31   int               prefix;
32} peribox_config;
2833
29/*
30    Callback interface. The callback functions are in the console and are
31    accessed from the PEB.
32*/
34#define PERIBOX_CONFIG(name) \
35   const peribox_config(name) =
3336
34#define MAXSLOTS 16
37class ti_expansion_card_device;
38class peribox_slot_device;
3539
36/*
37    This interface is used by the PEB to communicate with the expansion cards.
38    It actually forwards the accesses from the main system.
39*/
40typedef struct _ti99_peb_card_interface
40/*****************************************************************************
41    The overall Peripheral Expansion Box.
42    See ti99defs.h for bus8z_device
43******************************************************************************/
44
45class peribox_device : public bus8z_device
4146{
42   void         (*card_read_data)(device_t *card, offs_t offset, UINT8 *value);
43   void         (*card_write_data)(device_t *card, offs_t offset, UINT8 value);
47   friend class peribox_slot_device;
48public:
49   peribox_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
4450
45   void         (*card_read_cru)(device_t *card, offs_t offset, UINT8 *value);
46   void         (*card_write_cru)(device_t *card, offs_t offset, UINT8 value);
51   // Next six methods are called from the console
52   DECLARE_READ8Z_MEMBER(readz);
53   DECLARE_WRITE8_MEMBER(write);
54   void crureadz(offs_t offset, UINT8 *value);
55   void cruwrite(offs_t offset, UINT8 value);
56   DECLARE_WRITE_LINE_MEMBER(senila);
57   DECLARE_WRITE_LINE_MEMBER(senilb);
4758
48   void         (*senila)(device_t *card, int value);
49   void         (*senilb)(device_t *card, int value);
59   // Floppy interface
60   DECLARE_WRITE_LINE_MEMBER( indexhole );
5061
51   /* For SGCPU only ("expansion bus") */
52   void         (*card_read_data16)(device_t *card, offs_t offset, UINT16 *value);
53   void         (*card_write_data16)(device_t *card, offs_t offset, UINT16 value);
62   // Genmod support
63   DECLARE_INPUT_CHANGED_MEMBER( genmod_changed );
64   void set_genmod(bool set);
5465
55} ti99_peb_card;
66protected:
67   void device_start(void);
68   void device_config_complete(void);
5669
70   virtual machine_config_constructor device_mconfig_additions() const;
71
72   // Next three methods call back the console
73   devcb_resolved_write_line m_console_inta;   // INTA line (Box to console)
74   devcb_resolved_write_line m_console_intb;   // INTB line
75   devcb_resolved_write_line m_console_ready;   // READY line
76
77   void set_slot_loaded(int slot, peribox_slot_device* slotdev);
78   peribox_slot_device *m_slot[9];      // for the sake of simplicity we donate the first two positions (0,1)
79
80   // Propagators for the slot signals. All signals are active low, and
81   // if any one slot asserts the line, the joint line is asserted.
82   void inta_join(int slot, int state);
83   void intb_join(int slot, int state);
84   void ready_join(int slot, int state);
85
86   int m_inta_flag;
87   int m_intb_flag;
88   int m_ready_flag;
89
90   int m_address_prefix;
91};
92
93/************************************************************************
94    Specific Box compositions
95************************************************************************/
5796/*
58    Called from the expansion cards. These are the callbacks from the cards to
59    the PEB (which forwards them to the main system).
97    Variation for EVPC. We'd like to offer the EVPC slot device only if
98    we started the ti99_4ev driver.
6099*/
61typedef struct _peb_callback_if
100class peribox_ev_device : public peribox_device
62101{
63   devcb_write_line   inta;
64   devcb_write_line   intb;
65   devcb_write_line   ready;
102public:
103   peribox_ev_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
66104
67} peb_callback_if;
105protected:
106   machine_config_constructor device_mconfig_additions() const;
107};
68108
69109/*
70    Used to call back the main system. Also imported by the cards.
71    This structure holds the resolved callbacks within the PEB and the cards.
110    Variation for SGCPU (TI-99/4P). We put the EVPC and the HSGPL in slots 2 and 3.
72111*/
73typedef struct _peb_connect
112class peribox_sg_device : public peribox_device
74113{
75   devcb_resolved_write_line inta;
76   devcb_resolved_write_line intb;
77   devcb_resolved_write_line ready;
114public:
115   peribox_sg_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
78116
79} ti99_peb_connect;
117protected:
118   machine_config_constructor device_mconfig_additions() const;
119};
80120
81121/*
82    Accessor functions from the console. Note that we make use of the enhanced
83    read handler.
122    Variation for Geneve.
84123*/
85READ8Z_DEVICE_HANDLER( ti99_peb_data_rz );
86WRITE8_DEVICE_HANDLER( ti99_peb_data_w );
124class peribox_gen_device : public peribox_device
125{
126public:
127   peribox_gen_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
87128
88READ8Z_DEVICE_HANDLER( ti99_peb_cru_rz );
89WRITE8_DEVICE_HANDLER( ti99_peb_cru_w );
129protected:
130   machine_config_constructor device_mconfig_additions() const;
131};
90132
91WRITE_LINE_DEVICE_HANDLER( ti99_peb_senila );
92WRITE_LINE_DEVICE_HANDLER( ti99_peb_senilb );
133/*****************************************************************************
134    A single slot in the box.
135******************************************************************************/
93136
137class peribox_slot_device : public bus8z_device, public device_slot_interface
138{
139public:
140   peribox_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
94141
95/*
96    For SGCPU only. Actually the SGCPU uses an external cable to add the
97    8 data lines that are not included in the PEB
98*/
99READ16Z_DEVICE_HANDLER( ti99_peb_data16_rz );
100WRITE16_DEVICE_HANDLER( ti99_peb_data16_w );
142   // Called from the box (direction to card)
143   DECLARE_READ8Z_MEMBER(readz);
144   DECLARE_WRITE8_MEMBER(write);
145   DECLARE_WRITE_LINE_MEMBER(senila);
146   DECLARE_WRITE_LINE_MEMBER(senilb);
101147
102/*
103    Value passed to the cards
104*/
105typedef struct _ti99_pebcard_config
106{
107   int slot;
108} ti99_pebcard_config;
148   // Called from the card (direction to box)
149   DECLARE_WRITE_LINE_MEMBER( set_inta );
150   DECLARE_WRITE_LINE_MEMBER( set_intb );
151   DECLARE_WRITE_LINE_MEMBER( set_ready );
109152
110INLINE const ti99_pebcard_config *get_pebcard_config(device_t *device)
153   void crureadz(offs_t offset, UINT8 *value);
154   void cruwrite(offs_t offset, UINT8 value);
155
156   // called from the box itself
157   void set_genmod(bool set);
158
159   device_t*   get_drive(const char* name);
160
161protected:
162   void device_start(void);
163   void device_config_complete(void);
164
165private:
166   int get_index_from_tagname();
167   ti_expansion_card_device *m_card;
168   int m_slotnumber;
169};
170
171
172/*****************************************************************************
173    The parent class for all expansion cards.
174******************************************************************************/
175
176class ti_expansion_card_device : public bus8z_device, public device_slot_card_interface
111177{
112   assert(device != NULL);
113   return (const ti99_pebcard_config *) downcast<const legacy_device_base *>(device)->inline_config();
114}
178   friend class peribox_slot_device;
115179
116/*
117    Management functions
118*/
119int mount_card(device_t *device, device_t *cardptr, const ti99_peb_card *card, int slotindex);
120void unmount_card(device_t *device, int slotindex);
180public:
181   ti_expansion_card_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock)
182   : bus8z_device(mconfig, type, name, tag, owner, clock),
183   device_slot_card_interface(mconfig, *this)
184   {
185      m_slot = static_cast<peribox_slot_device*>(owner);
186      m_senila = CLEAR_LINE;
187      m_senilb = CLEAR_LINE;
188      m_genmod = false;
189   }
121190
191   virtual void crureadz(offs_t offset, UINT8 *value) =0;
192   virtual void cruwrite(offs_t offset, UINT8 data) =0;
122193
123/* device interface */
124DECLARE_LEGACY_DEVICE( PBOX4, ti99_peb );
125DECLARE_LEGACY_DEVICE( PBOX4A, ti994a_peb );
126DECLARE_LEGACY_DEVICE( PBOXEV, ti99ev_peb );
127DECLARE_LEGACY_DEVICE( PBOX8, ti998_peb );
128DECLARE_LEGACY_DEVICE( PBOXSG, ti99sg_peb );
129DECLARE_LEGACY_DEVICE( PBOXGEN, geneve_peb );
194   void   set_senila(int state) { m_senila = state; }
195   void   set_senilb(int state) { m_senilb = state; }
130196
131#define MCFG_PBOX4_ADD(_tag, _inta, _intb, _ready)         \
132   MCFG_DEVICE_ADD(_tag, PBOX4, 0)                     \
133   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
134   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
135   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)      \
136   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x07)
197protected:
198   peribox_slot_device *m_slot;      // using a link to the slot for callbacks
199   int   m_senila;
200   int   m_senilb;
137201
138#define MCFG_PBOX4A_ADD(_tag, _inta, _intb, _ready)         \
139   MCFG_DEVICE_ADD(_tag, PBOX4A, 0)                     \
140   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
141   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
142   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)      \
143   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x07)
202   // When TRUE, card is accessible. Indicated by a LED.
203   bool   m_selected;
144204
145#define MCFG_PBOXEV_ADD(_tag, _inta, _intb, _ready)         \
146   MCFG_DEVICE_ADD(_tag, PBOXEV, 0)                     \
147   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
148   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
149   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)      \
150   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x07)
205   // When TRUE, GenMod is selected.
206   bool   m_genmod;
151207
152#define MCFG_PBOX8_ADD(_tag, _inta, _intb, _ready)         \
153   MCFG_DEVICE_ADD(_tag, PBOX8, 0)                     \
154   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
155   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
156   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)      \
157   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x07)
208   // CRU base. Used to configure the address by which a card is selected.
209   int    m_cru_base;
158210
159#define MCFG_PBOXSG_ADD(_tag, _inta, _intb, _ready)         \
160   MCFG_DEVICE_ADD(_tag, PBOXSG, 0)                     \
161   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
162   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
163   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)   \
164   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x07)
211   // Used to decide whether this card has been selected.
212   int      m_select_mask;
213   int      m_select_value;
214};
165215
166#define MCFG_PBOXGEN_ADD(_tag, _inta, _intb, _ready)         \
167   MCFG_DEVICE_ADD(_tag, PBOXGEN, 0)                     \
168   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, inta, _inta)      \
169   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, intb, _intb)      \
170   MCFG_DEVICE_CONFIG_DATAPTR(ti99_peb_config, ready, _ready)   \
171   MCFG_DEVICE_CONFIG_DATA32(ti99_peb_config, amx, 0x00)
216#define MCFG_PERIBOX_ADD(_tag, _config) \
217   MCFG_DEVICE_ADD(_tag, PERIBOX, 0) \
218   MCFG_DEVICE_CONFIG( _config )
172219
220#define MCFG_PERIBOX_EV_ADD(_tag, _config) \
221   MCFG_DEVICE_ADD(_tag, PERIBOX_EV, 0) \
222   MCFG_DEVICE_CONFIG( _config )
223
224#define MCFG_PERIBOX_SG_ADD(_tag, _config) \
225   MCFG_DEVICE_ADD(_tag, PERIBOX_SG, 0) \
226   MCFG_DEVICE_CONFIG( _config )
227
228#define MCFG_PERIBOX_GEN_ADD(_tag, _config) \
229   MCFG_DEVICE_ADD(_tag, PERIBOX_GEN, 0) \
230   MCFG_DEVICE_CONFIG( _config )
231
232#define MCFG_PERIBOX_SLOT_ADD(_tag, _slot_intf) \
233   MCFG_DEVICE_ADD(_tag, PERIBOX_SLOT, 0) \
234   MCFG_DEVICE_SLOT_INTERFACE(_slot_intf, NULL, NULL)
235
236#define MCFG_PERIBOX_SLOT_ADD_DEF(_tag, _slot_intf, _default) \
237   MCFG_DEVICE_ADD(_tag, PERIBOX_SLOT, 0) \
238   MCFG_DEVICE_SLOT_INTERFACE(_slot_intf, _default, NULL)
239
240/*
241    The following defines are required because the WD17xx DEVICE_START implementation
242    assumes that the floppy devices are either at root level or at the parent
243    level. Our floppy devices, however, are at the grandparent level as seen from
244    the controller.
245*/
246#define PFLOPPY_0 ":peb:floppy0"
247#define PFLOPPY_1 ":peb:floppy1"
248#define PFLOPPY_2 ":peb:floppy2"
249#define PFLOPPY_3 ":peb:floppy3"
250
173251#endif /* __PBOX__ */
src/mess/machine/ti99/mecmouse.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Mechatronic mouse with adapter
3    Michael Zapf, October 2010
44
55    The Mechatronic mouse is connected to the joystick port and occupies
66    both joystick select lines and the switch lines. From these five
r14513r14514
4242    Michael Zapf, 2008-01-22
4343
4444    2010-10-22 Rewriten as device
45*/
45    February 2012: Rewritten as class
4646
47#include "emu.h"
47*****************************************************************************/
48
4849#include "mecmouse.h"
4950
50typedef struct _ti99_mecmouse_state
51{
52   int select;
53   int read_y;
54   int x;
55   int y;
56   int x_buf;
57   int y_buf;
58   int last_mx;
59   int last_my;
51#define POLL_TIMER 1
6052
61} ti99_mecmouse_state;
62
63INLINE ti99_mecmouse_state *get_safe_token(device_t *device)
53mecmouse_device::mecmouse_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
54: device_t(mconfig, MECMOUSE, "Mechatronics mouse", tag, owner, clock)
6455{
65   assert(device != NULL);
66   assert(device->type() == MECMOUSE);
67
68   return (ti99_mecmouse_state *)downcast<legacy_device_base *>(device)->token();
6956}
7057
71void mecmouse_select(device_t *device, int selnow, int stick1, int stick2)
58void mecmouse_device::select(int selnow, int stick1, int stick2)
7259{
73   ti99_mecmouse_state *mouse = get_safe_token(device);
74
7560   if (selnow == stick2) {
76      if (mouse->select == stick1) {
77         if (!mouse->read_y)
61      if (m_select == stick1) {
62         if (!m_read_y)
7863         {
7964            /* Sample x motion. */
80            if (mouse->x < -4)
81               mouse->x_buf = -4;
82            else if (mouse->x > 3)
83               mouse->x_buf = 3;
65            if (m_x < -4)
66               m_x_buf = -4;
67            else if (m_x > 3)
68               m_x_buf = 3;
8469            else
85               mouse->x_buf = mouse->x;
86            mouse->x -= mouse->x_buf;
87            mouse->x_buf = (mouse->x_buf-1) & 7;
70               m_x_buf = m_x;
71            m_x -= m_x_buf;
72            m_x_buf = (m_x_buf-1) & 7;
8873         }
8974         else
9075         {
9176            /* Sample y motion. */
92            if (mouse->y < -4)
93               mouse->y_buf = -4;
94            else if (mouse->y > 3)
95               mouse->y_buf = 3;
77            if (m_y < -4)
78               m_y_buf = -4;
79            else if (m_y > 3)
80               m_y_buf = 3;
9681            else
97               mouse->y_buf = mouse->y;
98            mouse->y -= mouse->y_buf;
99            mouse->y_buf = (mouse->y_buf-1) & 7;
82               m_y_buf = m_y;
83            m_y -= m_y_buf;
84            m_y_buf = (m_y_buf-1) & 7;
10085         }
10186      }
102      mouse->select = selnow;
87      m_select = selnow;
10388   }
10489   else if (selnow == stick1)
10590   {
106      if (mouse->select == stick2)
91      if (m_select == stick2)
10792      {
10893         /* Swap the axes. */
109         mouse->read_y = !mouse->read_y;
94         m_read_y = !m_read_y;
11095      }
111      mouse->select = selnow;
96      m_select = selnow;
11297   }
11398   else
11499   {
115100      /* Reset the axis toggle when the mouse is deselected */
116      mouse->read_y = 0;
101      m_read_y = 0;
117102   }
118103}
119104
120void mecmouse_poll(device_t *device)
105void mecmouse_device::poll()
121106{
122   ti99_mecmouse_state *mouse = get_safe_token(device);
123
124107   int new_mx, new_my;
125108   int delta_x, delta_y;
126109
127   new_mx = input_port_read(device->machine(), "MOUSEX");
128   new_my = input_port_read(device->machine(), "MOUSEY");
110   new_mx = input_port_read(machine(), "MOUSEX");
111   new_my = input_port_read(machine(), "MOUSEY");
129112
130113   /* compute x delta */
131   delta_x = new_mx - mouse->last_mx;
114   delta_x = new_mx - m_last_mx;
132115
133116   /* check for wrap */
134117   if (delta_x > 0x80)
r14513r14514
139122   /* Prevent unplausible values at startup. */
140123   if (delta_x > 100 || delta_x<-100) delta_x = 0;
141124
142   mouse->last_mx = new_mx;
125   m_last_mx = new_mx;
143126
144127   /* compute y delta */
145   delta_y = new_my - mouse->last_my;
128   delta_y = new_my - m_last_my;
146129
147130   /* check for wrap */
148131   if (delta_y > 0x80)
r14513r14514
152135
153136   if (delta_y > 100 || delta_y<-100) delta_y = 0;
154137
155   mouse->last_my = new_my;
138   m_last_my = new_my;
156139
157140   /* update state */
158   mouse->x += delta_x;
159   mouse->y += delta_y;
141   m_x += delta_x;
142   m_y += delta_y;
160143}
161144
162int mecmouse_get_values(device_t *device)
145int mecmouse_device::get_values()
163146{
164   ti99_mecmouse_state *mouse = get_safe_token(device);
165147   int answer;
166   int buttons = input_port_read(device->machine(), "MOUSE0") & 3;
167   answer = (mouse->read_y ? mouse->y_buf : mouse->x_buf) << 4;
148   int buttons = input_port_read(machine(), "MOUSE0") & 3;
149   answer = (m_read_y ? m_y_buf : m_x_buf) << 4;
168150
169151   if ((buttons & 1)==0)
170152      /* action button */
r14513r14514
178160/*
179161    Variation for TI-99/8
180162*/
181int mecmouse_get_values8(device_t *device, int mode)
163int mecmouse_device::get_values8(int mode)
182164{
183   ti99_mecmouse_state *mouse = get_safe_token(device);
184165   int answer;
185   int buttons = input_port_read(device->machine(), "MOUSE0") & 3;
166   int buttons = input_port_read(machine(), "MOUSE0") & 3;
186167
187168   if (mode == 0)
188169   {
189      answer = ((mouse->read_y ? mouse->y_buf : mouse->x_buf) << 7) & 0x80;
170      answer = ((m_read_y ? m_y_buf : m_x_buf) << 7) & 0x80;
190171      if ((buttons & 1)==0)
191172         /* action button */
192173         answer |= 0x40;
193174   }
194175   else
195176   {
196      answer = ((mouse->read_y ? mouse->y_buf : mouse->x_buf) << 1) & 0x03;
177      answer = ((m_read_y ? m_y_buf : m_x_buf) << 1) & 0x03;
197178      if ((buttons & 2)==0)
198179         /* home button */
199180         answer |= 0x04;
r14513r14514
201182   return answer;
202183}
203184
204/**************************************************************************/
185void mecmouse_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
186{
187   // only one timer here
188   poll();
189}
205190
206static DEVICE_START( ti99_mecmouse )
191void mecmouse_device::device_start(void)
207192{
193   m_poll_timer = timer_alloc(POLL_TIMER);
194   m_poll_timer->adjust(attotime::from_hz(clock()), 0, attotime::from_hz(clock()));
208195}
209196
210static DEVICE_RESET( ti99_mecmouse )
197void mecmouse_device::device_reset(void)
211198{
212   ti99_mecmouse_state *mouse = get_safe_token(device);
213   mouse->select = 0;
214   mouse->read_y = 0;
215   mouse->x = 0;
216   mouse->y = 0;
217   mouse->last_mx = 0;
218   mouse->last_my = 0;
199   m_poll_timer->enable(true);
200   m_select = 0;
201   m_read_y = 0;
202   m_x = 0;
203   m_y = 0;
204   m_last_mx = 0;
205   m_last_my = 0;
219206}
220207
221static const char DEVTEMPLATE_SOURCE[] = __FILE__;
222
223#define DEVTEMPLATE_ID(p,s)             p##ti99_mecmouse##s
224#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_RESET
225#define DEVTEMPLATE_NAME                "Mechatronics mouse"
226#define DEVTEMPLATE_FAMILY              "Human-computer interface"
227#include "devtempl.h"
228
229DEFINE_LEGACY_DEVICE( MECMOUSE, ti99_mecmouse );
230
208const device_type MECMOUSE = &device_creator<mecmouse_device>;
src/mess/machine/ti99/myarcmem.h
r14513r14514
1/*
2    TI-99 Myarc and Foundation memory expansion
1/****************************************************************************
2
3    TI-99 Myarc memory expansion
4    See myarcmem.c for documentation
5
36    Michael Zapf, September 2010
4*/
7    February 2012: Rewritten as class
58
9*****************************************************************************/
10
611#ifndef __MYARCMEM__
712#define __MYARCMEM__
813
9DECLARE_LEGACY_DEVICE( MYARCMEM, ti99_myarcmem );
14#include "emu.h"
15#include "peribox.h"
16#include "ti99defs.h"
17
18extern const device_type TI99_MYARCMEM;
19
20class myarc_memory_expansion_device : public ti_expansion_card_device
21{
22public:
23   myarc_memory_expansion_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
24   DECLARE_READ8Z_MEMBER(readz);
25   DECLARE_WRITE8_MEMBER(write);
26
27   void crureadz(offs_t offset, UINT8 *value);
28   void cruwrite(offs_t offset, UINT8 data);
29
30protected:
31   void device_start(void);
32   void device_reset(void);
33   const rom_entry *device_rom_region(void) const;
34   ioport_constructor device_input_ports() const;
35
36private:
37   int      get_base(int offset);
38   UINT8*   m_ram;
39   UINT8*   m_dsrrom;
40   int    m_bank;
41   int      m_size;
42};
43
1044#endif
src/mess/machine/ti99/bwg.c
r14513r14514
1/*
1/*******************************************************************************
22    SNUG BwG Disk Controller
33    Based on WD1773
44    Double Density, Double-sided
r14513r14514
1010    * Support an additional floppy drive, for a total of 4 floppies.
1111
1212    Reference:
13    * BwG Disketten-Controller: Beschreibung der DSR
13    * BwG Disketten-Controller: Beschreibung der DSR (Description of the DSR)
1414        <http://home.t-online.de/home/harald.glaab/snug/bwg.pdf>
1515
16    +------------------------+
17    |   32 KiB EPROM         | --- 1 of 4 pages--> 4000  +------------------+
18    |                        |                           |   DSR space      |
19    +------------------------+                           |   (Driver)       |
20    |   2 KiB  RAM           | --- 1 of 2 pages--> 5c00  +------------------+
21    +------------------------+                           |   RAM buffer     |
22                                                   5fe0  +------------------+
23                                                         |   RTC or WD1773  |
24                                                   5fff  +------------------+
25
1626    Michael Zapf, September 2010
27    January 2012: rewritten as class (MZ)
28*******************************************************************************/
1729
18    FIXME: IO error 50, CALLs ok
19*/
20
2130#include "emu.h"
2231#include "peribox.h"
2332#include "bwg.h"
r14513r14514
2635#include "imagedev/flopdrv.h"
2736#include "machine/mm58274c.h"
2837
29#define CRU_BASE 0x1100
38#define LOG logerror
39#define VERBOSE 1
3040
31#define bwg_IRQ 1
32#define bwg_DRQ 2
41#define MOTOR_TIMER 1
42#define FDC_TAG "wd1773"
43#define CLOCK_TAG "mm58274c"
3344
34#define bwg_region "bwg_region"
45#define BUFFER "ram"
3546
36typedef ti99_pebcard_config ti99_bwg_config;
37
38typedef struct _ti99_bwg_state
47snug_bwg_device::snug_bwg_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
48: ti_expansion_card_device(mconfig, TI99_BWG, "SNUG BwG Floppy Controller", tag, owner, clock)
3949{
40   /* Holds the status of the DRQ and IRQ lines. */
41   int               DRQ_IRQ_status;
50   m_shortname = "ti99_bwg";
51}
4252
43   /* When TRUE, card is accessible. Indicated by a LED. */
44   int               selected;
45
46   /* Used for GenMod. */
47   int               select_mask;
48   int               select_value;
49
50   /* When TRUE, keeps DVENA high. */
51   int               strobe_motor;
52
53   /* Signal DVENA. When TRUE, makes some drive turning. */
54   line_state         DVENA;
55
56   /* When TRUE the CPU is halted while DRQ/IRQ are true. */
57   int               hold;
58
59   /* Indicates which drive has been selected. Values are 0, 1, 2, and 4. */
60   // 000 = no drive
61   // 001 = drive 1
62   // 010 = drive 2
63   // 100 = drive 3
64   int               DSEL;
65
66   /* Signal SIDSEL. 0 or 1, indicates the selected head. */
67   int               SIDE;
68
69   /* ROM and RAM offset. */
70   int               rom_offset, ram_offset;
71
72   /* Indicates whether the clock is mapped into the address space. */
73   int               rtc_enabled;
74
75   /* count 4.23s from rising edge of motor_on */
76   emu_timer         *motor_on_timer;
77
78   /* Link to the FDC1771 controller on the board. */
79   device_t      *controller;
80
81   /* Link to the real-time clock on the board. */
82   device_t      *clock;
83
84   /* DSR ROM */
85   UINT8            *rom;
86
87   /* On-board RAM */
88   UINT8            *ram;
89
90   /* DIP swiches */
91   UINT8            dip1, dip2, dip34;
92
93   /* Callback lines to the main system. */
94   ti99_peb_connect   lines;
95
96} ti99_bwg_state;
97
98INLINE ti99_bwg_state *get_safe_token(device_t *device)
53/*
54    Callback called at the end of DVENA pulse
55*/
56void snug_bwg_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
9957{
100   assert(device != NULL);
101   assert(device->type() == BWG);
102
103   return (ti99_bwg_state *)downcast<legacy_device_base *>(device)->token();
58   m_DVENA = CLEAR_LINE;
59   handle_hold();
10460}
10561
10662/*
10763    Call this when the state of DSKhold or DRQ/IRQ or DVENA change
64    Also see ti_fdc.
10865
10966    Emulation is faulty because the CPU is actually stopped in the midst of
11067    instruction, at the end of the memory access
r14513r14514
11269    TODO: This has to be replaced by the proper READY handling that is already
11370    prepared here. (Requires READY handling by the CPU.)
11471*/
115static void bwg_handle_hold(device_t *device)
72void snug_bwg_device::handle_hold()
11673{
117   ti99_bwg_state *card = get_safe_token(device);
11874   line_state state;
11975
120   if (card->hold && (!card->DRQ_IRQ_status) && (card->DVENA==ASSERT_LINE))
76   if (m_hold && !m_DRQ && !m_IRQ && (m_DVENA==ASSERT_LINE))
12177      state = ASSERT_LINE;
12278   else
12379      state = CLEAR_LINE;
12480
12581   // TODO: use READY
126   cputag_set_input_line(device->machine(), "maincpu", INPUT_LINE_HALT, state);
82   cputag_set_input_line(machine(), "maincpu", INPUT_LINE_HALT, state);
12783}
12884
12985/*
130    Resets the drive geometry. This is required because the heuristic of
131    the default implementation sets the drive geometry to the geometry
132    of the medium.
86    Callback, called from the controller chip whenever DRQ/IRQ state change
13387*/
134static void set_geometry(device_t *drive, floppy_type_t type)
88WRITE_LINE_MEMBER( snug_bwg_device::intrq_w )
13589{
136   if (drive!=NULL)
137      floppy_drive_set_geometry(drive, type);
138   else
139      logerror("ti99/BwG: Drive not found\n");
140}
90   if (VERBOSE>8) LOG("bwg: set irq = %02x\n", state);
91   m_IRQ = (state==ASSERT_LINE);
14192
142/* Those defines are required because the WD17xx DEVICE_START implementation
143assumes that the floppy devices are either at root level or at the parent
144level. Our floppy devices, however, are at the grandparent level as seen from
145the controller. */
93   // Note that INTB is actually not used in the TI-99 family. But the
94   // controller asserts the line nevertheless, probably intended for
95   // use in another planned TI system
96   m_slot->set_intb(state);
14697
147#define PFLOPPY_0 ":peribox:floppy0"
148#define PFLOPPY_1 ":peribox:floppy1"
149#define PFLOPPY_2 ":peribox:floppy2"
150#define PFLOPPY_3 ":peribox:floppy3"
98   handle_hold();
99}
151100
152static void set_all_geometries(device_t *device, floppy_type_t type)
101WRITE_LINE_MEMBER( snug_bwg_device::drq_w )
153102{
154   set_geometry(device->machine().device(PFLOPPY_0), type);
155   set_geometry(device->machine().device(PFLOPPY_1), type);
156   set_geometry(device->machine().device(PFLOPPY_2), type);
157   set_geometry(device->machine().device(PFLOPPY_3), type);
103   if (VERBOSE>8) LOG("bwg: set drq = %02x\n", state);
104   m_DRQ = (state==ASSERT_LINE);
105   handle_hold();
158106}
159107
108/*
109    Read a byte
110    4000 - 5bff: ROM (4 banks)
160111
161/*************************************************************************/
112    rtc disabled:
113    5c00 - 5fef: RAM
114    5ff0 - 5fff: Controller (f0 = status, f2 = track, f4 = sector, f6 = data)
162115
163/*
164    Callback, called from the controller chip whenever DRQ/IRQ state change
116    rtc enabled:
117    5c00 - 5fdf: RAM
118    5fe0 - 5fff: Clock (even addr)
165119*/
166static WRITE_LINE_DEVICE_HANDLER( ti_bwg_intrq_w )
120READ8Z_MEMBER(snug_bwg_device::readz)
167121{
168   device_t *carddev = device->owner();
169   ti99_bwg_state *card = get_safe_token(carddev);
170
171   if (state)
122   if (m_selected)
172123   {
173      card->DRQ_IRQ_status |= bwg_IRQ;
124      if ((offset & m_select_mask)==m_select_value)
125      {
126         // 010x xxxx xxxx xxxx
127         if ((offset & 0x1c00)==0x1c00)
128         {
129            // ...1 11xx xxxx xxxx
130            if (m_rtc_enabled)
131            {
132               if ((offset & 0x03e1)==0x03e0)
133               {
134                  // .... ..11 111x xxx0
135                  *value = mm58274c_r(m_clock, (offset & 0x001e) >> 1);
136               }
137               else
138               {
139                  *value = m_buffer_ram[(m_ram_page<<10) | (offset & 0x03ff)];
140               }
141            }
142            else
143            {
144               if ((offset & 0x03f9)==0x03f0)
145               {
146                  // .... ..11 1111 0xx0
147                  // Note that the value is inverted again on the board,
148                  // so we can drop the inversion
149                  *value = wd17xx_r(m_controller, (offset >> 1)&0x03);
150               }
151               else
152               {
153                  *value = m_buffer_ram[(m_ram_page<<10) | (offset & 0x03ff)];
154               }
155            }
156         }
157         else
158         {
159            *value = m_dsrrom[(m_rom_page<<13) | (offset & 0x1fff)];
160            if (VERBOSE>7) LOG("bwg read dsr: %04x -> %02x\n", offset, *value);
161         }
162      }
174163   }
175   else
176   {
177      card->DRQ_IRQ_status &= ~bwg_IRQ;
178   }
179   // Note that INTB is actually not used in the TI-99 family. But the
180   // controller asserts the line nevertheless
181   // INTB* is active low.
182   card->lines.intb(state);
183   bwg_handle_hold(carddev);
184164}
185165
186static WRITE_LINE_DEVICE_HANDLER( ti_bwg_drq_w )
166/*
167    Resets the drive geometry. This is required because the heuristic of
168    the default implementation sets the drive geometry to the geometry
169    of the medium.
170*/
171void snug_bwg_device::set_geometry(device_t *drive, floppy_type_t type)
187172{
188   device_t *carddev = device->owner();
189   ti99_bwg_state *card = get_safe_token(carddev);
173   // This assertion may fail when the names of the floppy devices change.
174   // Unfortunately, the wd17xx device assumes the floppy drives at root
175   // level, so we use an explicitly qualified tag. See peribox.h.
176   assert(drive != NULL);
177   floppy_drive_set_geometry(drive, type);
178}
190179
191   if (state)
192      card->DRQ_IRQ_status |= bwg_DRQ;
193   else
194      card->DRQ_IRQ_status &= ~bwg_DRQ;
195
196   bwg_handle_hold(carddev);
180void snug_bwg_device::set_all_geometries(floppy_type_t type)
181{
182   set_geometry(machine().device(PFLOPPY_0), type);
183   set_geometry(machine().device(PFLOPPY_1), type);
184   set_geometry(machine().device(PFLOPPY_2), type);
197185}
198186
199187/*
200    callback called at the end of DVENA pulse
188    Write a byte
189    4000 - 5bff: ROM, ignore write (4 banks)
190
191    rtc disabled:
192    5c00 - 5fef: RAM
193    5ff0 - 5fff: Controller (f8 = command, fa = track, fc = sector, fe = data)
194
195    rtc enabled:
196    5c00 - 5fdf: RAM
197    5fe0 - 5fff: Clock (even addr)
201198*/
202static TIMER_CALLBACK(motor_on_timer_callback)
199WRITE8_MEMBER(snug_bwg_device::write)
203200{
204   device_t *device = (device_t *)ptr;
205   ti99_bwg_state *card = get_safe_token(device);
206   card->DVENA = CLEAR_LINE;
207   bwg_handle_hold(device);
201   if (m_selected)
202   {
203      if ((offset & m_select_mask)==m_select_value)
204      {
205         // 010x xxxx xxxx xxxx
206         if ((offset & 0x1c00)==0x1c00)
207         {
208            // ...1 11xx xxxx xxxx
209            if (m_rtc_enabled)
210            {
211               if ((offset & 0x03e1)==0x03e0)
212               {
213                  // .... ..11 111x xxx0
214                  mm58274c_w(m_clock, (offset & 0x001e) >> 1, data);
215               }
216               else
217               {
218                  m_buffer_ram[(m_ram_page<<10) | (offset & 0x03ff)] = data;
219               }
220            }
221            else
222            {
223               if ((offset & 0x03f9)==0x03f8)
224               {
225                  // .... ..11 1111 1xx0
226                  // Note that the value is inverted again on the board,
227                  // so we can drop the inversion
228                  wd17xx_w(m_controller, (offset >> 1)&0x03, data);
229               }
230               else
231               {
232                  m_buffer_ram[(m_ram_page<<10) | (offset & 0x03ff)] = data;
233               }
234            }
235         }
236      }
237   }
208238}
209239
210#if 0
211static WRITE_LINE_DEVICE_HANDLER( ti99_bwg_ready )
212{
213   // Caution: The device pointer passed to this function is the calling
214   // device. That is, if we want *this* device, we need to take the owner.
215   ti99_bwg_state *card = get_safe_token(device->owner());
216   card->lines.ready(state);
217}
218#endif
219
220240/*
221241    CRU read handler. *=inverted.
222242    bit 0: DSK4 connected*
r14513r14514
228248    bit 6: Dip 3
229249    bit 7: Dip 4
230250*/
231static READ8Z_DEVICE_HANDLER( cru_r )
251void snug_bwg_device::crureadz(offs_t offset, UINT8 *value)
232252{
233   ti99_bwg_state *card = get_safe_token(device);
234253   UINT8 reply = 0;
235254
236   if ((offset & 0xff00)==CRU_BASE)
255   if ((offset & 0xff00)==m_cru_base)
237256   {
238257      if ((offset & 0x00ff)==0)
239258      {
r14513r14514
243262         reply = 0x00;
244263
245264         // DIP switches. Note that a closed switch means 0
246         // xx01 1111   11 = nur dsk1; 10 = 1+2, 01=1/2/3, 00=1-4
247         if (card->dip1) reply |= 0x10;
248         if (card->dip2) reply |= 0x20;
249         reply |= (card->dip34 << 6);
265         // xx01 1111   11 = only dsk1; 10 = 1+2, 01=1/2/3, 00=1-4
266
267         if (m_dip1 != 0) reply |= 0x10;
268         if (m_dip2 != 0) reply |= 0x20;
269         reply |= (m_dip34 << 6);
250270         *value = ~reply;
251271      }
252272      else
r14513r14514
254274   }
255275}
256276
257static WRITE8_DEVICE_HANDLER( cru_w )
277void snug_bwg_device::cruwrite(offs_t offset, UINT8 data)
258278{
259   ti99_bwg_state *card = get_safe_token(device);
260279   int drive, drivebit;
261280
262   if ((offset & 0xff00)==CRU_BASE)
281   if ((offset & 0xff00)==m_cru_base)
263282   {
264283      int bit = (offset >> 1) & 0x0f;
265284      switch (bit)
266285      {
267286      case 0:
268287         /* (De)select the card. Indicated by a LED on the board. */
269         card->selected = data;
288         m_selected = (data != 0);
289         if (VERBOSE>4) LOG("bwg: Map DSR = %d\n", m_selected);
270290         break;
271291
272292      case 1:
273293         /* Activate motor */
274         if (data && !card->strobe_motor)
294         if (data && !m_strobe_motor)
275295         {   /* on rising edge, set motor_running for 4.23s */
276            card->DVENA = ASSERT_LINE;
277            bwg_handle_hold(device);
278            card->motor_on_timer->adjust(attotime::from_msec(4230));
296            m_DVENA = ASSERT_LINE;
297            handle_hold();
298            m_motor_on_timer->adjust(attotime::from_msec(4230));
279299         }
280         card->strobe_motor = data;
300         m_strobe_motor = (data != 0);
281301         break;
282302
283303      case 2:
r14513r14514
287307         // OR the motor stops rotating - rotates for 4.23s after write
288308         // to CRU bit 1
289309         // This is not emulated and could cause the TI99 to lock up
290         card->hold = data;
291         bwg_handle_hold(device);
310         m_hold = (data != 0);
311         handle_hold();
292312         break;
293313
294314      case 4:
r14513r14514
300320         drive = (bit == 8) ? 3 : (bit - 4);      /* drive # (0-3) */
301321         drivebit = 1<<drive;
302322
303         if (data)
323         if (data != 0)
304324         {
305            if (!(card->DSEL & drivebit))         /* select drive */
325            if ((m_DSEL & drivebit) == 0)         /* select drive */
306326            {
307               if (card->DSEL != 0)
308                  logerror("ti99/BwG: Multiple drives selected, %02x\n", card->DSEL);
309               card->DSEL |= drivebit;
310               wd17xx_set_drive(card->controller, drive);
327               if (m_DSEL != 0)
328                  logerror("bwg: Multiple drives selected, %02x\n", m_DSEL);
329               m_DSEL |= drivebit;
330               wd17xx_set_drive(m_controller, drive);
311331            }
312332         }
313333         else
314            card->DSEL &= ~drivebit;
334            m_DSEL &= ~drivebit;
315335         break;
316336
317337      case 7:
318338         /* Select side of disk (bit 7) */
319         card->SIDE = data;
320         wd17xx_set_side(card->controller, card->SIDE);
339         m_SIDE = data;
340         wd17xx_set_side(m_controller, m_SIDE);
321341         break;
322342
323343      case 10:
324344         /* double density enable (active low) */
325         wd17xx_dden_w(card->controller, data ? ASSERT_LINE : CLEAR_LINE);
345         wd17xx_dden_w(m_controller, (data != 0) ? ASSERT_LINE : CLEAR_LINE);
326346         break;
327347
328348      case 11:
329349         /* EPROM A13 */
330         if (data)
331            card->rom_offset |= 0x2000;
350         if (data != 0)
351            m_rom_page |= 1;
332352         else
333            card->rom_offset &= ~0x2000;
353            m_rom_page &= 0xfe;  // 11111110
334354         break;
335355
336356      case 13:
337357         /* RAM A10 */
338         if (data)
339            card->ram_offset = 0x0400;
340         else
341            card->ram_offset = 0x0000;
358         m_ram_page = data;
342359         break;
343360
344361      case 14:
345362         /* Override FDC with RTC (active high) */
346         card->rtc_enabled = data;
363         m_rtc_enabled = (data != 0);
347364         break;
348365
349366      case 15:
350367         /* EPROM A14 */
351         if (data)
352            card->rom_offset |= 0x4000;
368         if (data != 0)
369            m_rom_page |= 2;
353370         else
354            card->rom_offset &= ~0x4000;
371            m_rom_page &= 0xfd; // 11111101
355372         break;
356373
357374      case 3:
r14513r14514
363380   }
364381}
365382
366/*
367    Read a byte
368    4000 - 5bff: ROM (4 banks)
369
370    rtc disabled:
371    5c00 - 5fef: RAM
372    5ff0 - 5fff: Controller (f0 = status, f2 = track, f4 = sector, f6 = data)
373
374    rtc enabled:
375    5c00 - 5fdf: RAM
376    5fe0 - 5fff: Clock (even addr)
377*/
378
379static READ8Z_DEVICE_HANDLER( data_r )
383void snug_bwg_device::device_start(void)
380384{
381   ti99_bwg_state *card = get_safe_token(device);
382
383   if (card->selected)
384   {
385      if ((offset & card->select_mask)==card->select_value)
386      {
387         // 010x xxxx xxxx xxxx
388         if ((offset & 0x1c00)==0x1c00)
389         {
390            // ...1 11xx xxxx xxxx
391            if (card->rtc_enabled)
392            {
393               if ((offset & 0x03e1)==0x03e0)
394               {
395                  // .... ..11 111x xxx0
396                  *value = mm58274c_r(card->clock, (offset & 0x001e) >> 1);
397               }
398               else
399               {
400                  *value = card->ram[card->ram_offset + (offset&0x03ff)];
401               }
402            }
403            else
404            {
405               if ((offset & 0x03f9)==0x03f0)
406               {
407                  // .... ..11 1111 0xx0
408                  // Note that the value is inverted again on the board,
409                  // so we can drop the inversion
410                  *value = wd17xx_r(card->controller, (offset >> 1)&0x03);
411               }
412               else
413               {
414                  *value = card->ram[card->ram_offset + (offset&0x03ff)];
415               }
416            }
417         }
418         else
419         {
420            *value = card->rom[card->rom_offset + (offset & 0x1fff)];
421         }
422      }
423   }
385   if (VERBOSE>5) LOG("bwg: BWG start\n");
386   m_dsrrom = subregion(DSRROM)->base();
387   m_buffer_ram = subregion(BUFFER)->base();
388   m_motor_on_timer = timer_alloc(MOTOR_TIMER);
389   m_controller = subdevice(FDC_TAG);
390   m_clock = subdevice(CLOCK_TAG);
391   m_cru_base = 0x1100;
424392}
425393
426/*
427    Write a byte
428    4000 - 5bff: ROM, ignore write (4 banks)
429
430    rtc disabled:
431    5c00 - 5fef: RAM
432    5ff0 - 5fff: Controller (f8 = command, fa = track, fc = sector, fe = data)
433
434    rtc enabled:
435    5c00 - 5fdf: RAM
436    5fe0 - 5fff: Clock (even addr)
437*/
438static WRITE8_DEVICE_HANDLER( data_w )
394void snug_bwg_device::device_reset()
439395{
440   ti99_bwg_state *card = get_safe_token(device);
396   if (VERBOSE>5) LOG("bwg: BWG reset\n");
441397
442   if (card->selected)
398   if (m_genmod)
443399   {
444      if ((offset & card->select_mask)==card->select_value)
445      {
446         // 010x xxxx xxxx xxxx
447         if ((offset & 0x1c00)==0x1c00)
448         {
449            // ...1 11xx xxxx xxxx
450            if (card->rtc_enabled)
451            {
452               if ((offset & 0x03e1)==0x03e0)
453               {
454                  // .... ..11 111x xxx0
455                  mm58274c_w(card->clock, (offset & 0x001e) >> 1, data);
456               }
457               else
458               {
459                  card->ram[card->ram_offset + (offset&0x03ff)] = data;
460               }
461            }
462            else
463            {
464               if ((offset & 0x03f9)==0x03f8)
465               {
466                  // .... ..11 1111 1xx0
467                  // Note that the value is inverted again on the board,
468                  // so we can drop the inversion
469                  wd17xx_w(card->controller, (offset >> 1)&0x03, data);
470               }
471               else
472               {
473                  card->ram[card->ram_offset + (offset&0x03ff)] = data;
474               }
475            }
476         }
477      }
400      m_select_mask = 0x1fe000;
401      m_select_value = 0x174000;
478402   }
479}
480
481static DEVICE_START( ti99_bwg )
482{
483   ti99_bwg_state *card = get_safe_token(device);
484
485   /* Resolve the callbacks to the PEB */
486   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
487   card->lines.ready.resolve(topeb->ready, *device);
488   card->lines.intb.resolve(topeb->intb, *device);
489
490   card->motor_on_timer = device->machine().scheduler().timer_alloc(FUNC(motor_on_timer_callback), (void *)device);
491   card->controller = device->subdevice("wd1773");
492   card->clock = device->subdevice("mm58274c");
493   astring region(device->tag(), ":", bwg_region);
494   card->rom = device->machine().region(region.cstr())->base();
495   card->ram = NULL;
496}
497
498static DEVICE_STOP( ti99_bwg )
499{
500   ti99_bwg_state *card = get_safe_token(device);
501   logerror("ti99_bwg: stop\n");
502   if (card->ram) free(card->ram);
503}
504
505static const ti99_peb_card bwg_card =
506{
507   data_r,
508   data_w,
509   cru_r,
510   cru_w,
511   NULL, NULL,   NULL, NULL
512};
513
514static DEVICE_RESET( ti99_bwg )
515{
516   ti99_bwg_state *card = get_safe_token(device);
517
518   /* If the card is selected in the menu, register the card */
519   if (input_port_read(device->machine(), "DISKCTRL") == DISK_BWG)
403   else
520404   {
521      device_t *peb = device->owner();
522      int success = mount_card(peb, device, &bwg_card, get_pebcard_config(device)->slot);
523      if (!success) return;
405      m_select_mask = 0x7e000;
406      m_select_value = 0x74000;
407   }
524408
525      // Allocate 2 KiB for on-board buffer memory (6116 chip)
526      if (card->ram==NULL)
527      {
528         card->ram = (UINT8*)malloc(2048);
529         memset(card->ram, 0, 2048);
530      }
409   m_strobe_motor = false;
410   m_DVENA = CLEAR_LINE;
411   ti99_set_80_track_drives(FALSE);
412   floppy_type_t type = FLOPPY_STANDARD_5_25_DSDD_40;
413   set_all_geometries(type);
414   m_DRQ = false;
415   m_IRQ = false;
416   m_hold = false;
417   m_rtc_enabled = false;
418   m_selected = false;
531419
532      card->dip1 = input_port_read(device->machine(), "BWGDIP1");
533      card->dip2 = input_port_read(device->machine(), "BWGDIP2");
534      card->dip34 = input_port_read(device->machine(), "BWGDIP34");
420   m_dip1 = input_port_read(*this, "BWGDIP1");
421   m_dip2 = input_port_read(*this, "BWGDIP2");
422   m_dip34 = input_port_read(*this, "BWGDIP34");
535423
536      card->DSEL = 0;
537      card->SIDE = 0;
538      card->DVENA = CLEAR_LINE;
539      card->strobe_motor = 0;
540
541      ti99_set_80_track_drives(FALSE);
542
543      floppy_type_t flop = FLOPPY_STANDARD_5_25_DSDD_40;
544      set_all_geometries(device, flop);
545
546      card->strobe_motor = 0;
547      card->ram_offset = 0;
548      card->rom_offset = 0;
549      card->rtc_enabled = 0;
550
551      card->select_mask = 0x7e000;
552      card->select_value = 0x74000;
553
554      if (input_port_read(device->machine(), "MODE")==GENMOD)
555      {
556         // GenMod card modification
557         card->select_mask = 0x1fe000;
558         card->select_value = 0x174000;
559      }
560
561      wd17xx_dden_w(card->controller, CLEAR_LINE);   // Correct?
562   }
424   m_rom_page = 0;
425   m_ram_page = 0;
563426}
564427
565const wd17xx_interface ti_wd17xx_interface =
428const wd17xx_interface bwg_wd17xx_interface =
566429{
567430   DEVCB_NULL,
568   DEVCB_LINE(ti_bwg_intrq_w),
569   DEVCB_LINE(ti_bwg_drq_w),
431   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, snug_bwg_device, intrq_w),
432   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, snug_bwg_device, drq_w),
570433   { PFLOPPY_0, PFLOPPY_1, PFLOPPY_2, PFLOPPY_3 }
571434};
572435
r14513r14514
576439   0   /*  first day of week */
577440};
578441
579MACHINE_CONFIG_FRAGMENT( ti99_bwg )
580   MCFG_WD1773_ADD("wd1773", ti_wd17xx_interface )
581   MCFG_MM58274C_ADD("mm58274c", floppy_mm58274c_interface)
442INPUT_PORTS_START( bwg_fdc )
443   PORT_START( "BWGDIP1" )
444   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" )
445      PORT_DIPSETTING( 0x00, "6 ms")
446      PORT_DIPSETTING( 0x01, "20 ms")
447
448   PORT_START( "BWGDIP2" )
449   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" )
450      PORT_DIPSETTING( 0x00, "Hide")
451      PORT_DIPSETTING( 0x01, "Show")
452
453   PORT_START( "BWGDIP34" )
454   PORT_DIPNAME( 0x03, 0x00, "BwG drives" )
455      PORT_DIPSETTING( 0x00, "DSK1 only")
456      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
457      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
458      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
459INPUT_PORTS_END
460
461MACHINE_CONFIG_FRAGMENT( bwg_fdc )
462   MCFG_WD1773_ADD(FDC_TAG, bwg_wd17xx_interface )
463   MCFG_MM58274C_ADD(CLOCK_TAG, floppy_mm58274c_interface)
582464MACHINE_CONFIG_END
583465
584ROM_START( ti99_bwg )
585   ROM_REGION(0x8000, bwg_region, 0)
586   ROM_LOAD_OPTIONAL("bwg.bin", 0x0000, 0x8000, CRC(06f1ec89) SHA1(6ad77033ed268f986d9a5439e65f7d391c4b7651)) /* BwG disk DSR ROM */
466ROM_START( bwg_fdc )
467   ROM_REGION(0x8000, DSRROM, 0)
468   ROM_LOAD("bwg.bin", 0x0000, 0x8000, CRC(06f1ec89) SHA1(6ad77033ed268f986d9a5439e65f7d391c4b7651)) /* BwG disk DSR ROM */
469   ROM_REGION(0x0800, BUFFER, 0)  /* BwG RAM buffer */
470   ROM_FILL(0x0000, 0x0400, 0x00)
587471ROM_END
588472
589static const char DEVTEMPLATE_SOURCE[] = __FILE__;
473machine_config_constructor snug_bwg_device::device_mconfig_additions() const
474{
475   return MACHINE_CONFIG_NAME( bwg_fdc );
476}
590477
591#define DEVTEMPLATE_ID(p,s)             p##ti99_bwg##s
592#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_ROM_REGION | DT_HAS_INLINE_CONFIG | DT_HAS_MACHINE_CONFIG
593#define DEVTEMPLATE_NAME                "SNUG BwG Floppy Disk Controller Card"
594#define DEVTEMPLATE_SHORTNAME           "snugfdc"
595#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
596#include "devtempl.h"
478const rom_entry *snug_bwg_device::device_rom_region() const
479{
480   return ROM_NAME( bwg_fdc );
481}
597482
598DEFINE_LEGACY_DEVICE( BWG, ti99_bwg );
483ioport_constructor snug_bwg_device::device_input_ports() const
484{
485   return INPUT_PORTS_NAME(bwg_fdc);
486}
599487
488const device_type TI99_BWG = &device_creator<snug_bwg_device>;
489
src/mess/machine/ti99/memex.h
r14513r14514
1/*
1/****************************************************************************
2
23    Geneve "Memex" memory expansion
4    See memex.c for documentation
5
36    Michael Zapf, February 2011
4*/
7    February 2012: Rewritten as class
58
9*****************************************************************************/
10
611#ifndef __MEMEXMEM__
712#define __MEMEXMEM__
813
9DECLARE_LEGACY_DEVICE( GENMEMEX, gen_memex );
14#include "emu.h"
15#include "ti99defs.h"
16#include "peribox.h"
17
18extern const device_type TI99_MEMEX;
19
20class geneve_memex_device : public ti_expansion_card_device
21{
22public:
23   geneve_memex_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
24   DECLARE_READ8Z_MEMBER(readz);
25   DECLARE_WRITE8_MEMBER(write);
26
27   void crureadz(offs_t offset, UINT8 *value)  { };
28   void cruwrite(offs_t offset, UINT8 value)  { };
29
30protected:
31   void device_start();
32   void device_reset();
33   ioport_constructor device_input_ports() const;
34   const rom_entry *device_rom_region() const;
35
36private:
37   bool   access_enabled(offs_t offset);
38
39   UINT8*   m_ram;
40   UINT8   m_dip_switch[8];
41   int      m_genmod;
42};
43
1044#endif
src/mess/machine/ti99/mapper8.h
r14513r14514
1/****************************************************************************
2
3    TI-99/8 Address decoder and mapper
4
5    See mapper8.c for documentation
6
7    Michael Zapf
8
9    February 2012: Rewritten as class
10
11*****************************************************************************/
12
113#ifndef __MAPPER8__
214#define __MAPPER8__
315
4typedef void (*mapper_read_function)(device_t *device, offs_t offset, UINT8 *value);
5typedef void (*mapper_write_function)(device_t *device, offs_t offset, UINT8 value);
16#include "emu.h"
17#include "peribox.h"
18#include "ti99defs.h"
619
7typedef struct _mapper8_dev_config
8{
9   const char            *name;
10   int                  mode;
11   int                  stop;
12   UINT32               address_mask;
13   UINT32               select_pattern;
14   UINT32               write_select;
15   mapper_read_function   read;
16   mapper_write_function   write;
17} mapper8_dev_config;
20extern const device_type MAPPER8;
1821
22#define NATIVE 0
23#define TI99EM 1
24#define PATGEN 2
25#define PHYSIC 3
26#define CONT 0
27#define STOP 1
28
1929#define SRAMNAME "SRAM"
2030#define ROM0NAME "ROM0"
2131#define ROM1NAME "ROM1"
r14513r14514
2333#define INTSNAME "INTS"
2434#define DRAMNAME "DRAM"
2535
26READ8_DEVICE_HANDLER( ti99_mapper8_r );
27WRITE8_DEVICE_HANDLER( ti99_mapper8_w );
28WRITE8_DEVICE_HANDLER( ti99_mapreg_w );
29WRITE8_DEVICE_HANDLER( mapper8_CRUS_w );
30WRITE8_DEVICE_HANDLER( mapper8_PTGEN_w );
31READ8Z_DEVICE_HANDLER( ti998dsr_rz );
32WRITE8_DEVICE_HANDLER( mapper8c_w );
36#define SRAM_SIZE 2048
37#define DRAM_SIZE 65536
3338
34/* device interface */
35DECLARE_LEGACY_DEVICE( MAPPER8, mapper8 );
39typedef struct _mapper8_list_entry
40{
41   const char*   name;            // Name of the device (used for looking up the device)
42   int         mode;            // Mode of the system which applies to this entry
43   int         stop;            // Mapper shall stop searching for a matching device when this entry applies
44   UINT32      select_pattern;      // State of the address line bits when addressing this device
45   UINT32      address_mask;      // Bits of the address bus used to address this device
46   UINT32      write_select;      // Additional bits set when doing write accesses to this device
47} mapper8_list_entry;
3648
49/*
50    Device list of the mapper.
51*/
52class log_addressed_device
53{
54   friend class simple_list<log_addressed_device>;
55   friend class ti998_mapper_device;
56
57public:
58   log_addressed_device(device_t *busdevice, const mapper8_list_entry &entry)
59   :   m_device(busdevice), m_config(&entry) { };
60
61private:
62   log_addressed_device      *m_next;      // needed for simple_list
63   device_t               *m_device;      // the actual device
64   const mapper8_list_entry   *m_config;
65};
66
67/*
68    Device list of the mapper.
69*/
70class phys_addressed_device
71{
72   friend class simple_list<phys_addressed_device>;
73   friend class ti998_mapper_device;
74
75public:
76   phys_addressed_device(device_t *busdevice, const mapper8_list_entry &entry)
77   :   m_device(busdevice), m_config(&entry) { };
78
79private:
80   phys_addressed_device      *m_next;      // needed for simple_list
81   device_t               *m_device;      // the actual device
82   const mapper8_list_entry   *m_config;
83};
84
85/*
86    Main class
87*/
88class ti998_mapper_device : public bus8z_device
89{
90public:
91   ti998_mapper_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
92   DECLARE_READ8_MEMBER( readm);      // used from address map
93   DECLARE_WRITE8_MEMBER( writem );   // used from address map
94
95   DECLARE_READ8Z_MEMBER( readz );
96   DECLARE_WRITE8_MEMBER( write );
97
98   void crureadz(offs_t offset, UINT8 *value);
99   void cruwrite(offs_t offset, UINT8 data);
100
101   void CRUS_set(bool state);
102   void PTGE_set(bool state);
103
104protected:
105   void device_start(void);
106   void device_reset(void);
107
108private:
109   bool search_logically_addressed_r(address_space& space, offs_t offset, UINT8 *value, UINT8 mem_mask );
110   bool search_logically_addressed_w(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask );
111   void search_physically_addressed_r(address_space& space, offs_t offset, UINT8 *value, UINT8 mem_mask );
112   void search_physically_addressed_w(address_space& space, offs_t offset, UINT8 data, UINT8 mem_mask );
113   void mapwrite(int offset, UINT8 data);
114
115   // All devices that are attached to the 16-bit address bus.
116   simple_list<log_addressed_device> m_logcomp;
117
118   // All devices that are attached to the 24-bit mapped address bus.
119   simple_list<phys_addressed_device> m_physcomp;
120
121   // Select bit for the internal DSR.
122   bool   m_dsr_selected;
123
124   // 99/4A compatibility mode. Name is taken from the spec. If 1, 99/4A compatibility is active.
125   bool   m_CRUS;
126
127   // P-Code mode. Name is taken from the spec. If 0, P-Code libraries are available.
128   // May be read as "Pascal and Text-to-speech GROM libraries Enable (Negative)"
129   // Note: this is negative logic. GROMs are present only for PTGEN=0
130   // We use PTGE as the inverse signal.
131   bool   m_PTGE;
132
133   // Address mapper registers. Each offset is selected by the first 4 bits
134   // of the logical address.
135   UINT32   m_pas_offset[16];
136
137   // SRAM area of the system. Directly connected to the address decoder.
138   UINT8   *m_sram;
139
140   // DRAM area of the system. Connected to the mapped address bus.
141   UINT8   *m_dram;
142
143   // ROM area of the system. Directly connected to the address decoder.
144   UINT8   *m_rom;
145};
146
147
37148#define MCFG_MAPPER8_ADD(_tag, _devices)         \
38149   MCFG_DEVICE_ADD(_tag, MAPPER8, 0) \
39150   MCFG_DEVICE_CONFIG( _devices )
src/mess/machine/ti99/mecmouse.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Mechatronic mouse with adapter
3    Michael Zapf, October 2010
4*/
4    See mecmouse.c for documentation
55
6    Michael Zapf
7    October 2010
8    January 2012: rewritten as class
9
10*****************************************************************************/
11
612#ifndef __MECMOUSE__
713#define __MECMOUSE__
814
9void mecmouse_select(device_t *device, int selnow, int stick1, int stick2);
10void mecmouse_poll(device_t *device);
11int mecmouse_get_values(device_t *device);
12int mecmouse_get_values8(device_t *device, int mode);
15#include "emu.h"
1316
14#define MCFG_MECMOUSE_ADD(_tag )   \
15   MCFG_DEVICE_ADD(_tag, MECMOUSE, 0)
17extern const device_type MECMOUSE;
1618
17DECLARE_LEGACY_DEVICE( MECMOUSE, ti99_mecmouse );
19class mecmouse_device : public device_t
20{
21public:
22   mecmouse_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
23   void select(int selnow, int stick1, int stick2);
24   int get_values();
25   int get_values8(int mode);
26
27protected:
28   void device_start(void);
29   void device_reset(void);
30
31private:
32   void poll(void);
33   int m_select;
34   int m_read_y;
35   int m_x;
36   int m_y;
37   int m_x_buf;
38   int m_y_buf;
39   int m_last_mx;
40   int m_last_my;
41   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
42
43   emu_timer   *m_poll_timer;
44};
45
46#define MCFG_MECMOUSE_ADD(_tag, _clock )   \
47   MCFG_DEVICE_ADD(_tag, MECMOUSE, _clock)
48
1849#endif
src/mess/machine/ti99/bwg.h
r14513r14514
1/*
1/****************************************************************************
2
23    SNUG BwG Disk Controller
34    Based on WD1770
45    Double Density, Double-sided
6
57    Michael Zapf, September 2010
6*/
8    February 2012: Rewritten as class
79
8/* device interface */
10*****************************************************************************/
11
912#ifndef __BWG__
1013#define __BWG__
1114
12DECLARE_LEGACY_DEVICE( BWG, ti99_bwg );
15#include "ti99defs.h"
16#include "imagedev/flopdrv.h"
17
18extern const device_type TI99_BWG;
19
20class snug_bwg_device : public ti_expansion_card_device
21{
22public:
23   snug_bwg_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
24   DECLARE_READ8Z_MEMBER(readz);
25   DECLARE_WRITE8_MEMBER(write);
26
27   DECLARE_WRITE_LINE_MEMBER( intrq_w );
28   DECLARE_WRITE_LINE_MEMBER( drq_w );
29
30   void crureadz(offs_t offset, UINT8 *value);
31   void cruwrite(offs_t offset, UINT8 value);
32
33protected:
34   void device_start(void);
35   void device_reset(void);
36   const rom_entry *device_rom_region() const;
37   machine_config_constructor device_mconfig_additions() const;
38   ioport_constructor device_input_ports() const;
39
40private:
41   void handle_hold(void);
42   void set_all_geometries(floppy_type_t type);
43   void set_geometry(device_t *drive, floppy_type_t type);
44   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
45
46   // Holds the status of the DRQ and IRQ lines.
47   bool         m_DRQ, m_IRQ;
48
49   // DIP switch state
50   int            m_dip1, m_dip2, m_dip34;
51
52   // Page selection for ROM and RAM
53   int          m_ram_page;  // 0-1
54   int          m_rom_page;  // 0-3
55
56   // When TRUE the CPU is halted while DRQ/IRQ are true.
57   bool         m_hold;
58
59   /* Indicates whether the clock is mapped into the address space. */
60   bool         m_rtc_enabled;
61
62   /* When TRUE, keeps DVENA high. */
63   bool         m_strobe_motor;
64
65   // Signal DVENA. When TRUE, makes some drive turning.
66   line_state      m_DVENA;
67
68   /* Indicates which drive has been selected. Values are 0, 1, 2, and 4. */
69   // 000 = no drive
70   // 001 = drive 1
71   // 010 = drive 2
72   // 100 = drive 3
73   int            m_DSEL;
74
75   /* Signal SIDSEL. 0 or 1, indicates the selected head. */
76   int            m_SIDE;
77
78   // Link to the FDC1771 controller on the board.
79   device_t*      m_controller;
80
81   // Link to the real-time clock on the board.
82   device_t*      m_clock;
83
84   // count 4.23s from rising edge of motor_on
85   emu_timer*      m_motor_on_timer;
86
87   // DSR ROM
88   UINT8*         m_dsrrom;
89
90   // Buffer RAM
91   UINT8*         m_buffer_ram;
92};
93
1394#endif
src/mess/machine/ti99/handset.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/4 Handset support (TI99/4 only)
34
45    The ti99/4 was intended to support some so-called "IR remote handsets".
r14513r14514
1920
2021    Michael Zapf
2122    2010-10-24 Rewriten as device
22*/
2323
24#include "emu.h"
24    January 2012: Rewritten as class
25
26*****************************************************************************/
27
2528#include "handset.h"
2629#include "machine/tms9901.h"
2730
28#define MAX_HANDSETS 4
31#define LOG logerror
32#define VERBOSE 1
2933
30typedef struct _ti99_handset_state
31{
32   int       buf;
33   int       buflen;
34   int       clock;
35   int       ack;
36   UINT8      previous_joy[MAX_HANDSETS];
37   UINT8      previous_key[MAX_HANDSETS];
38
39   emu_timer      *timer;
40   device_t   *console9901;
41
42} ti99_handset_state;
43
4434static const char *const joynames[2][4] =
4535{
4636   { "JOY0", "JOY2", "JOY4", "JOY6" },      // X axis
r14513r14514
4939
5040static const char *const keynames[] = { "KP0", "KP1", "KP2", "KP3", "KP4" };
5141
52INLINE ti99_handset_state *get_safe_token(device_t *device)
42ti99_handset_device::ti99_handset_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
43: device_t(mconfig, HANDSET, "TI-99/4 IR handset", tag, owner, clock)
5344{
54   assert(device != NULL);
55   assert(device->type() == HANDSET);
56
57   return (ti99_handset_state *)downcast<legacy_device_base *>(device)->token();
5845}
5946
60INLINE const ti99_handset_config *get_config(device_t *device)
61{
62   assert(device != NULL);
63   assert(device->type() == HANDSET);
47#define POLL_TIMER 1
48#define DELAY_TIMER 2
6449
65   return (const ti99_handset_config *) downcast<const legacy_device_base *>(device)->inline_config();
66}
67
6850/*****************************************************************************/
6951
7052/*
71    ti99_handset_poll_bus()
72
73    Poll the current state of the 4-bit data bus that goes from the I/R
74    receiver to the tms9901.
75*/
76int ti99_handset_poll_bus(device_t *device)
77{
78   ti99_handset_state *handset = get_safe_token(device);
79   return (handset->buf & 0xf);
80}
81
82int ti99_handset_get_clock(device_t *device)
83{
84   ti99_handset_state *handset = get_safe_token(device);
85   return handset->clock;
86}
87
88int ti99_handset_get_int(device_t *device)
89{
90   ti99_handset_state *handset = get_safe_token(device);
91   return handset->buflen==3;
92}
93
94/*
95    ti99_handset_ack_callback()
96
9753    Handle data acknowledge sent by the ti-99/4 handset ISR (through tms9901
9854    line P0).  This function is called by a delayed timer 30us after the state
9955    of P0 is changed, because, in one occasion, the ISR asserts the line before
10056    it reads the data, so we need to delay the acknowledge process.
10157*/
102static TIMER_CALLBACK(ti99_handset_ack_callback)
58void ti99_handset_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
10359{
104   device_t *device = (device_t *)ptr;
105   ti99_handset_state *handset = get_safe_token(device);
60   if (id==DELAY_TIMER)
61   {
62      m_clock = !m_clock;
63      m_buf >>= 4;
64      m_buflen--;
10665
107   handset->clock = !handset->clock;
108   handset->buf >>= 4;
109   handset->buflen--;
66      // Clear the INT12 line
67      m_interrupt(CLEAR_LINE);
11068
111   tms9901_set_single_int(handset->console9901, 12, 0);
69      if (m_buflen == 1)
70      {
71         // Unless I am missing something, the third and last nibble of the
72         // message is not acknowledged by the DSR in any way, and the first nibble
73         // of next message is not requested for either, so we need to decide on
74         // our own when we can post a new event.  Currently, we wait for 1000us
75         // after the DSR acknowledges the second nybble.
76         m_delay_timer->adjust(attotime::from_usec(1000));
77      }
11278
113   if (handset->buflen == 1)
79      if (m_buflen == 0)
80         /* See if we need to post a new event */
81         do_task();
82   }
83   else
11484   {
115      // Unless I am missing something, the third and last nybble of the
116      // message is not acknowledged by the DSR in any way, and the first nybble
117      // of next message is not requested for either, so we need to decide on
118      // our own when we can post a new event.  Currently, we wait for 1000us
119      // after the DSR acknowledges the second nybble.
120      handset->timer->adjust(attotime::from_usec(1000));
85      // Poll timer
86      do_task();
12187   }
122
123   if (handset->buflen == 0)
124      /* See if we need to post a new event */
125      ti99_handset_task(device);
12688}
12789
12890/*
129    ti99_handset_set_ack()
13091    Handler for tms9901 P0 pin (handset data acknowledge)
13192*/
132void ti99_handset_set_acknowledge(device_t *device, UINT8 data)
93void ti99_handset_device::set_acknowledge(int data)
13394{
134   ti99_handset_state *handset = get_safe_token(device);
135   if (handset->buflen && (data != handset->ack))
95   if ((m_buflen !=0) && (data != m_ack))
13696   {
137      handset->ack = data;
138      if (data == handset->clock)
97      m_ack = data;
98      if (data == m_clock)
13999         /* I don't know what the real delay is, but 30us apears to be enough */
140         handset->timer->adjust(attotime::from_usec(30));
100      m_delay_timer->adjust(attotime::from_usec(30));
141101   }
142102}
143103
144104/*
145    ti99_handset_post_message()
105    post_message()
146106
147107    Post a 12-bit message: trigger an interrupt on the tms9901, and store the
148108    message in the I/R receiver buffer so that the handset ISR will read this
r14513r14514
150110
151111    message: 12-bit message to post (only the 12 LSBits are meaningful)
152112*/
153static void ti99_handset_post_message(device_t *device, int message)
113void ti99_handset_device::post_message(int message)
154114{
155   ti99_handset_state *handset = get_safe_token(device);
156115   /* post message and assert interrupt */
157   handset->clock = 1;
158   handset->buf = ~message;
159   handset->buflen = 3;
160
161   tms9901_set_single_int(handset->console9901, 12, 1);
116   m_clock = 1;
117   m_buf = ~message;
118   m_buflen = 3;
119   m_interrupt(ASSERT_LINE);
162120}
163121
164122/*
165    ti99_handset_poll_keyboard()
123    poll_keyboard()
166124    Poll the current state of one given handset keypad.
167125    num: number of the keypad to poll (0-3)
168126    Returns TRUE if the handset state has changed and a message was posted.
169127*/
170static int ti99_handset_poll_keyboard(device_t *device, int num)
128bool ti99_handset_device::poll_keyboard(int num)
171129{
172   ti99_handset_state *handset = get_safe_token(device);
173130   UINT32 key_buf;
174131   UINT8 current_key;
175132   int i;
176133
177134   /* read current key state */
178   key_buf = ( input_port_read(device->machine(), keynames[num]) | (input_port_read(device->machine(), keynames[num + 1]) << 16) ) >> (4*num);
135   key_buf = (input_port_read(machine(), keynames[num]) | (input_port_read(machine(), keynames[num + 1]) << 16) ) >> (4*num);
179136
180137   // If a key was previously pressed, this key was not shift, and this key is
181138   // still down, then don't change the current key press.
182   if (handset->previous_key[num] && (handset->previous_key[num] != 0x24)
183      && (key_buf & (1 << (handset->previous_key[num] & 0x1f))))
139   if (previous_key[num] !=0 && (previous_key[num] != 0x24)
140      && (key_buf & (1 << (previous_key[num] & 0x1f))))
184141   {
185142      /* check the shift modifier state */
186      if (((handset->previous_key[num] & 0x20) != 0) == ((key_buf & 0x0008) != 0))
143      if (((previous_key[num] & 0x20) != 0) == ((key_buf & 0x0008) != 0))
187144         /* the shift modifier state has not changed */
188      return FALSE;
145         return false;
189146      else
190147      {
191148         // The shift modifier state has changed: we need to update the
192149         // keyboard state
193150         if (key_buf & 0x0008)
194151         {   /* shift has been pressed down */
195            handset->previous_key[num] = current_key = handset->previous_key[num] | 0x20;
152            previous_key[num] = current_key = previous_key[num] | 0x20;
196153         }
197154         else
198         {   /* shift has been pressed down */
199            handset->previous_key[num] = current_key = handset->previous_key[num] & ~0x20;
155         {
156            previous_key[num] = current_key = previous_key[num] & ~0x20;
200157         }
201158         /* post message */
202         ti99_handset_post_message(device, (((unsigned) current_key) << 4) | (num << 1));
203
204         return TRUE;
159         post_message((((unsigned)current_key) << 4) | (num << 1));
160         return true;
205161      }
206162
207163   }
r14513r14514
223179      }
224180   }
225181
226   if (current_key != handset->previous_key[num])
182   if (current_key != previous_key[num])
227183   {
228      handset->previous_key[num] = current_key;
184      previous_key[num] = current_key;
229185
230186      /* post message */
231      ti99_handset_post_message(device, (((unsigned) current_key) << 4) | (num << 1));
232
233      return TRUE;
187      post_message((((unsigned) current_key) << 4) | (num << 1));
188      return true;
234189   }
235   return FALSE;
190   return false;
236191}
237192
238193/*
239    ti99_handset_poll_joystick()
194    poll_joystick()
240195
241196    Poll the current state of one given handset joystick.
242197    num: number of the joystick to poll (0-3)
243198    Returns TRUE if the handset state has changed and a message was posted.
244199*/
245static int ti99_handset_poll_joystick(device_t *device, int num)
200bool ti99_handset_device::poll_joystick(int num)
246201{
247   ti99_handset_state *handset = get_safe_token(device);
248
249202   UINT8 current_joy;
250203   int current_joy_x, current_joy_y;
251204   int message;
252
253205   /* read joystick position */
254   current_joy_x = input_port_read(device->machine(), joynames[0][num]);
255   current_joy_y = input_port_read(device->machine(), joynames[1][num]);
206   current_joy_x = input_port_read(machine(), joynames[0][num]);
207   current_joy_y = input_port_read(machine(), joynames[1][num]);
256208
257209   /* compare with last saved position */
258210   current_joy = current_joy_x | (current_joy_y << 4);
259   if (current_joy != handset->previous_joy[num])
211   if (current_joy != previous_joy[num])
260212   {
261213      /* save position */
262      handset->previous_joy[num] = current_joy;
214      previous_joy[num] = current_joy;
263215
264216      /* transform position to signed quantity */
265217      current_joy_x -= 7;
r14513r14514
299251      message |= (num << 1) | 0x1;
300252
301253      /* post message */
302      ti99_handset_post_message(device, message);
303      return TRUE;
254      post_message(message);
255      return true;
304256   }
305   return FALSE;
257   return false;
306258}
307259
308260/*
309261    ti99_handset_task()
310262    Manage handsets, posting an event if the state of any handset has changed.
311263*/
312void ti99_handset_task(device_t *device)
264void ti99_handset_device::do_task()
313265{
314266   int i;
315   ti99_handset_state *handset = get_safe_token(device);
316267
317   if (handset->buflen == 0)
268   if (m_buflen == 0)
318269   {
319270      /* poll every handset */
320271      for (i=0; i < MAX_HANDSETS; i++)
321         if (ti99_handset_poll_joystick(device, i)) return;
272         if (poll_joystick(i)==true) return;
322273      for (i=0; i < MAX_HANDSETS; i++)
323         if (ti99_handset_poll_keyboard(device, i)) return;
274         if (poll_keyboard(i)==true) return;
324275   }
325   else if (handset->buflen == 3)
276   else if (m_buflen == 3)
326277   {   /* update messages after they have been posted */
327      if (handset->buf & 1)
278      if (m_buf & 1)
328279      {   /* keyboard */
329         ti99_handset_poll_keyboard(device, (~(handset->buf >> 1)) & 0x3);
280         poll_keyboard((~(m_buf >> 1)) & 0x3);
330281      }
331282      else
332283      {   /* joystick */
333         ti99_handset_poll_joystick(device, (~(handset->buf >> 1)) & 0x3);
284         poll_joystick((~(m_buf >> 1)) & 0x3);
334285      }
335286   }
336287}
337288
338
339/**************************************************************************/
340
341static DEVICE_START( ti99_handset )
289void ti99_handset_device::device_start(void)
342290{
343   ti99_handset_state *handset = get_safe_token(device);
344   handset->timer = device->machine().scheduler().timer_alloc(FUNC(ti99_handset_ack_callback), (void *)device);
291   const ti99_handset_intf *intf = reinterpret_cast<const ti99_handset_intf *>(static_config());
292
293   m_interrupt.resolve(intf->interrupt, *this);
294   m_delay_timer = timer_alloc(DELAY_TIMER);
295   m_poll_timer = timer_alloc(POLL_TIMER);
296   m_poll_timer->adjust(attotime::from_hz(clock()), 0, attotime::from_hz(clock()));
345297}
346298
347static DEVICE_RESET( ti99_handset )
299void ti99_handset_device::device_reset(void)
348300{
349   const ti99_handset_config* conf = (const ti99_handset_config*)get_config(device);
350   ti99_handset_state *handset = get_safe_token(device);
351   handset->console9901 = device->machine().device(conf->sysintf);
352   handset->buf = 0;
353   handset->buflen = 0;
354   handset->clock = 0;
355   handset->ack = 0;
301   if (VERBOSE>5) LOG("ti99_handset_device reset\n");
302   m_delay_timer->enable(true);
303   m_poll_timer->enable(true);
304   m_buf = 0;
305   m_buflen = 0;
306   m_clock = 0;
307   m_ack = 0;
356308}
357309
358static const char DEVTEMPLATE_SOURCE[] = __FILE__;
359
360#define DEVTEMPLATE_ID(p,s)             p##ti99_handset##s
361#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
362#define DEVTEMPLATE_NAME                "TI-99/4 Handset"
363#define DEVTEMPLATE_FAMILY              "Human-Computer Interface"
364#include "devtempl.h"
365
366DEFINE_LEGACY_DEVICE( HANDSET, ti99_handset );
367
310const device_type HANDSET = &device_creator<ti99_handset_device>;
src/mess/machine/ti99/spchsyn.c
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Speech synthesizer
34
45    We emulate the Speech Synthesizer plugged onto a P-Box adapter. The original
r14513r14514
1011    predecessor of the TMS5220 which was used in other commercial products.
1112
1213    Note that this adapter also contains the speech roms.
13*/
14#include "emu.h"
15#include "peribox.h"
14
15    Michael Zapf
16
17    February 2012: Rewritten as class
18
19*****************************************************************************/
20
1621#include "spchsyn.h"
17#include "sound/tms5220.h"
1822#include "sound/wave.h"
1923
2024#define TMS5220_ADDRESS_MASK 0x3FFFFUL   /* 18-bit mask for tms5220 address */
21#define speech_region "speech_region"
2225
23typedef ti99_pebcard_config ti99_speech_config;
26#define VERBOSE 1
27#define LOG logerror
2428
25typedef struct _ti99_speech_state
29#define SPEECHROM_TAG "speechrom"
30
31/****************************************************************************/
32
33ti_speech_synthesizer_device::ti_speech_synthesizer_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
34: ti_expansion_card_device(mconfig, TI99_SPEECH, "TI-99 Speech synthesizer (on adapter card)", tag, owner, clock)
2635{
27   device_t            *vsp;
28   UINT8               *speechrom_data;      /* pointer to speech ROM data */
29   int                load_pointer;         /* which 4-bit nibble will be affected by load address */
30   int                ROM_bits_count;            /* current bit position in ROM */
31   UINT32               speechROMaddr;            /* 18 bit pointer in ROM */
32   UINT32               speechROMlen;         /* length of data pointed by speechrom_data, from 0 to 2^18 */
33   ti99_peb_connect      lines;
36   m_shortname = "ti99_speech";
37}
3438
35   int                  select_mask;
36   int                  select_value;
37} ti99_speech_state;
39/*
40    Memory read
41*/
42READ8Z_MEMBER( ti_speech_synthesizer_device::readz )
43{
44   if ((offset & m_select_mask)==m_select_value)
45   {
46      device_adjust_icount(machine().device("maincpu"),-(18+3));      /* this is just a minimum, it can be more */
47      *value = tms5220_status_r(m_vsp, offset) & 0xff;
48   }
49}
3850
39INLINE ti99_speech_state *get_safe_token(device_t *device)
51/*
52    Memory write
53*/
54WRITE8_MEMBER( ti_speech_synthesizer_device::write )
4055{
41   assert(device != NULL);
42   assert(device->type() == TISPEECH);
56   if ((offset & m_select_mask)==(m_select_value | 0x0400))
57   {
58      device_adjust_icount(machine().device("maincpu"),-(54+3));      /* this is just an approx. minimum, it can be much more */
4359
44   return (ti99_speech_state *)downcast<legacy_device_base *>(device)->token();
60      /* RN: the stupid design of the tms5220 core means that ready is cleared */
61      /* when there are 15 bytes in FIFO.  It should be 16.  Of course, if */
62      /* it were the case, we would need to store the value on the bus, */
63      /* which would be more complex. */
64      if (!tms5220_readyq_r(m_vsp))
65      {
66         attotime time_to_ready = attotime::from_double(tms5220_time_to_ready(m_vsp));
67         int cycles_to_ready = machine().device<cpu_device>("maincpu")->attotime_to_cycles(time_to_ready);
68         if (VERBOSE>8) LOG("time to ready: %f -> %d\n", time_to_ready.as_double(), (int) cycles_to_ready);
69
70         device_adjust_icount(machine().device("maincpu"),-cycles_to_ready);
71         machine().scheduler().timer_set(attotime::zero, FUNC_NULL);
72      }
73      tms5220_data_w(m_vsp, offset, data);
74   }
4575}
4676
47/*****************************************************************************/
77/****************************************************************************
78    Callbacks from TMS5220
4879
80    TODO: Convert to methods as soon as TMS5220 has been converted
81*****************************************************************************/
4982/*
5083    Read 'count' bits serially from speech ROM
5184*/
5285static int ti99_spchroms_read(device_t *vspdev, int count)
5386{
54   device_t *device = vspdev->owner();
55   ti99_speech_state *adapter = get_safe_token(device);
87   ti_speech_synthesizer_device *adapter = static_cast<ti_speech_synthesizer_device*>(vspdev->owner());
5688
5789   int val;
5890
59   if (adapter->load_pointer)
91   if (adapter->m_load_pointer)
6092   {   /* first read after load address is ignored */
61      adapter->load_pointer = 0;
93      adapter->m_load_pointer = 0;
6294      count--;
6395   }
6496
65   if (adapter->speechROMaddr < adapter->speechROMlen)
97   if (adapter->m_sprom_address < adapter->m_sprom_length)
6698   {
67      if (count < adapter->ROM_bits_count)
99      if (count < adapter->m_rombits_count)
68100      {
69         adapter->ROM_bits_count -= count;
70         val = (adapter->speechrom_data[adapter->speechROMaddr] >> adapter->ROM_bits_count) & (0xFF >> (8 - count));
101         adapter->m_rombits_count -= count;
102         val = (adapter->m_speechrom[adapter->m_sprom_address] >> adapter->m_rombits_count) & (0xFF >> (8 - count));
71103      }
72104      else
73105      {
74         val = ((int)adapter->speechrom_data[adapter->speechROMaddr]) << 8;
106         val = ((int)adapter->m_speechrom[adapter->m_sprom_address]) << 8;
75107
76         adapter->speechROMaddr = (adapter->speechROMaddr + 1) & TMS5220_ADDRESS_MASK;
108         adapter->m_sprom_address = (adapter->m_sprom_address + 1) & TMS5220_ADDRESS_MASK;
77109
78         if (adapter->speechROMaddr < adapter->speechROMlen)
79            val |= adapter->speechrom_data[adapter->speechROMaddr];
110         if (adapter->m_sprom_address < adapter->m_sprom_length)
111            val |= adapter->m_speechrom[adapter->m_sprom_address];
80112
81         adapter->ROM_bits_count += 8 - count;
113         adapter->m_rombits_count += 8 - count;
82114
83         val = (val >> adapter->ROM_bits_count) & (0xFF >> (8 - count));
115         val = (val >> adapter->m_rombits_count) & (0xFF >> (8 - count));
84116      }
85117   }
86118   else
r14513r14514
94126*/
95127static void ti99_spchroms_load_address(device_t *vspdev, int data)
96128{
97   device_t *device = vspdev->owner();
98   ti99_speech_state *adapter = get_safe_token(device);
129   ti_speech_synthesizer_device *adapter = static_cast<ti_speech_synthesizer_device*>(vspdev->owner());
130
99131   // tms5220 data sheet says that if we load only one 4-bit nibble, it won't work.
100132   // This code does not care about this.
101   adapter->speechROMaddr = ((adapter->speechROMaddr & ~(0xf << adapter->load_pointer))
102      | (((unsigned long) (data & 0xf)) << adapter->load_pointer) ) & TMS5220_ADDRESS_MASK;
103   adapter->load_pointer += 4;
104   adapter->ROM_bits_count = 8;
133   adapter->m_sprom_address = ((adapter->m_sprom_address & ~(0xf << adapter->m_load_pointer))
134      | (((unsigned long) (data & 0xf)) << adapter->m_load_pointer) ) & TMS5220_ADDRESS_MASK;
135   adapter->m_load_pointer += 4;
136   adapter->m_rombits_count = 8;
105137}
106138
107139/*
r14513r14514
109141*/
110142static void ti99_spchroms_read_and_branch(device_t *vspdev)
111143{
112   device_t *device = vspdev->owner();
113   ti99_speech_state *adapter = get_safe_token(device);
144   ti_speech_synthesizer_device *adapter = static_cast<ti_speech_synthesizer_device*>(vspdev->owner());
114145   // tms5220 data sheet says that if more than one speech ROM (tms6100) is present,
115146   // there is a bus contention.  This code does not care about this. */
116   if (adapter->speechROMaddr < adapter->speechROMlen-1)
117      adapter->speechROMaddr = (adapter->speechROMaddr & 0x3c000UL)
118         | (((((unsigned long) adapter->speechrom_data[adapter->speechROMaddr]) << 8)
119         | adapter->speechrom_data[adapter->speechROMaddr+1]) & 0x3fffUL);
120   else if (adapter->speechROMaddr == adapter->speechROMlen-1)
121      adapter->speechROMaddr = (adapter->speechROMaddr & 0x3c000UL)
122         | ((((unsigned long) adapter->speechrom_data[adapter->speechROMaddr]) << 8) & 0x3fffUL);
147   if (adapter->m_sprom_address < adapter->m_sprom_length-1)
148      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL)
149         | (((((unsigned long) adapter->m_speechrom[adapter->m_sprom_address]) << 8)
150         | adapter->m_speechrom[adapter->m_sprom_address+1]) & 0x3fffUL);
151   else if (adapter->m_sprom_address == adapter->m_sprom_length-1)
152      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL)
153         | ((((unsigned long) adapter->m_speechrom[adapter->m_sprom_address]) << 8) & 0x3fffUL);
123154   else
124      adapter->speechROMaddr = (adapter->speechROMaddr & 0x3c000UL);
155      adapter->m_sprom_address = (adapter->m_sprom_address & 0x3c000UL);
125156
126   adapter->ROM_bits_count = 8;
157   adapter->m_rombits_count = 8;
127158}
128159
129/*****************************************************************************/
160/****************************************************************************/
130161
131162/*
132    Memory read
163    Callback interface instance
133164*/
134static READ8Z_DEVICE_HANDLER( speech_rz )
165static const tms5220_interface ti99_4x_tms5200interface =
135166{
136   ti99_speech_state *adapter = get_safe_token(device);
167   DEVCB_NULL,                  // no IRQ callback
168   DEVCB_DEVICE_LINE_MEMBER(DEVICE_SELF_OWNER, ti_speech_synthesizer_device, speech_ready),
169   ti99_spchroms_read,            // speech ROM read handler
170   ti99_spchroms_load_address,      // speech ROM load address handler
171   ti99_spchroms_read_and_branch   // speech ROM read and branch handler
172};
137173
138   if ((offset & adapter->select_mask)==adapter->select_value)
139   {
140      device_adjust_icount(device->machine().device("maincpu"),-(18+3));      /* this is just a minimum, it can be more */
141      *value = tms5220_status_r(adapter->vsp, offset) & 0xff;
142   }
143}
174/****************************************************************************/
144175
145/*
146    Memory write
147*/
148static WRITE8_DEVICE_HANDLER( speech_w )
176WRITE_LINE_MEMBER( ti_speech_synthesizer_device::speech_ready )
149177{
150   ti99_speech_state *adapter = get_safe_token(device);
151
152   if ((offset & adapter->select_mask)==(adapter->select_value | 0x0400))
153   {
154      device_adjust_icount(device->machine().device("maincpu"),-(54+3));      /* this is just an approx. minimum, it can be much more */
155
156      /* RN: the stupid design of the tms5220 core means that ready is cleared */
157      /* when there are 15 bytes in FIFO.  It should be 16.  Of course, if */
158      /* it were the case, we would need to store the value on the bus, */
159      /* which would be more complex. */
160      if (!tms5220_readyq_r(adapter->vsp))
161      {
162         attotime time_to_ready = attotime::from_double(tms5220_time_to_ready(adapter->vsp));
163         int cycles_to_ready = device->machine().device<cpu_device>("maincpu")->attotime_to_cycles(time_to_ready);
164         logerror("time to ready: %f -> %d\n", time_to_ready.as_double(), (int) cycles_to_ready);
165
166         device_adjust_icount(device->machine().device("maincpu"),-cycles_to_ready);
167         device->machine().scheduler().timer_set(attotime::zero, FUNC_NULL);
168      }
169      tms5220_data_w(adapter->vsp, offset, data);
170   }
178   m_slot->set_ready(state);
171179}
172180
173/**************************************************************************/
174
175static const ti99_peb_card speech_adapter_card =
181void ti_speech_synthesizer_device::device_start()
176182{
177   speech_rz, speech_w,         // memory access read/write
178   NULL, NULL,                  // CRU access (none here)
179   NULL, NULL,                  // SENILA/B access (none here)
180   NULL, NULL                  // 16 bit access (none here)
181};
182
183static DEVICE_START( ti99_speech )
184{
185   ti99_speech_state *adapter = get_safe_token(device);
186   /* Resolve the callbacks to the PEB */
187   peb_callback_if *topeb = (peb_callback_if *)device->static_config();
188   adapter->lines.ready.resolve(topeb->ready, *device);
189   adapter->vsp = device->subdevice("speechsyn");
190183}
191184
192static DEVICE_STOP( ti99_speech )
185void ti_speech_synthesizer_device::device_config_complete()
193186{
187   m_vsp = subdevice("speechsyn");
194188}
195189
196static DEVICE_RESET( ti99_speech )
190void ti_speech_synthesizer_device::device_reset()
197191{
198   ti99_speech_state *adapter = get_safe_token(device);
199   /* Register the adapter */
200   device_t *peb = device->owner();
192   m_speechrom = subregion(SPEECHROM_TAG)->base();
193   m_sprom_length = subregion(SPEECHROM_TAG)->bytes();
194   m_sprom_address = 0;
195   m_load_pointer = 0;
196   m_rombits_count = 0;
201197
202   if (input_port_read(device->machine(), "SPEECH"))
198   if (m_genmod)
203199   {
204      int success = mount_card(peb, device, &speech_adapter_card, get_pebcard_config(device)->slot);
205      if (!success)
206      {
207         logerror("speech: Could not mount the speech synthesizer\n");
208         return;
209      }
210
211      astring region(device->tag(), ":", speech_region);
212
213      adapter->speechrom_data = device->machine().region(region.cstr())->base();
214      adapter->speechROMlen = device->machine().region(region.cstr())->bytes();
215      adapter->speechROMaddr = 0;
216      adapter->load_pointer = 0;
217      adapter->ROM_bits_count = 0;
218
219      adapter->select_mask = 0x7fc01;
220      adapter->select_value = 0x79000;
221
222      if (input_port_read(device->machine(), "MODE")==GENMOD)
223      {
224         // GenMod card modification
225         adapter->select_mask = 0x1ffc01;
226         adapter->select_value = 0x179000;
227      }
200      m_select_mask = 0x1ffc01;
201      m_select_value = 0x179000;
228202   }
203   else
204   {
205      m_select_mask = 0x7fc01;
206      m_select_value = 0x79000;
207   }
229208}
230209
231static WRITE_LINE_DEVICE_HANDLER( speech_ready )
232{
233   ti99_speech_state *adapter = get_safe_token(device->owner());
234   logerror("speech: READY called by VSP\n");
235   adapter->lines.ready(state);
236}
237
238/*
239    TMS5220 speech synthesizer
240    Note that in the real hardware, the predecessor TMC0285 was used.
241*/
242static const tms5220_interface ti99_4x_tms5200interface =
243{
244   DEVCB_NULL,               /* no IRQ callback */
245   DEVCB_LINE(speech_ready),
246   ti99_spchroms_read,            /* speech ROM read handler */
247   ti99_spchroms_load_address,      /* speech ROM load address handler */
248   ti99_spchroms_read_and_branch   /* speech ROM read and branch handler */
249};
250
251210MACHINE_CONFIG_FRAGMENT( ti99_speech )
252211   MCFG_SPEAKER_STANDARD_MONO("mono")
253
254212   MCFG_SOUND_ADD("speechsyn", TMC0285, 680000L)
255213   MCFG_SOUND_CONFIG(ti99_4x_tms5200interface)
256214   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50)
257215MACHINE_CONFIG_END
258216
259217ROM_START( ti99_speech )
260   ROM_REGION(0x8000, speech_region, 0)
218   ROM_REGION(0x8000, SPEECHROM_TAG, 0)
261219   ROM_LOAD_OPTIONAL("spchrom.bin", 0x0000, 0x8000, CRC(58b155f7) SHA1(382292295c00dff348d7e17c5ce4da12a1d87763)) /* system speech ROM */
262220ROM_END
263221
264static const char DEVTEMPLATE_SOURCE[] = __FILE__;
222machine_config_constructor ti_speech_synthesizer_device::device_mconfig_additions() const
223{
224   return MACHINE_CONFIG_NAME( ti99_speech );
225}
265226
266#define DEVTEMPLATE_ID(p,s)             p##ti99_speech##s
267#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG | DT_HAS_ROM_REGION | DT_HAS_MACHINE_CONFIG
268#define DEVTEMPLATE_NAME                "TI-99 Speech Synthesizer"
269#define DEVTEMPLATE_SHORTNAME           "ti99spsyn"
270#define DEVTEMPLATE_FAMILY              "Peripheral expansion"
271#include "devtempl.h"
272
273DEFINE_LEGACY_DEVICE( TISPEECH, ti99_speech );
274
227const rom_entry *ti_speech_synthesizer_device::device_rom_region() const
228{
229   return ROM_NAME( ti99_speech );
230}
231const device_type TI99_SPEECH = &device_creator<ti_speech_synthesizer_device>;
src/mess/machine/ti99/handset.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99/4 handset
4    See handset.c for documentation.
5
36    Michael Zapf, October 2010
4*/
7    February 2012: Rewritten as class
58
9*****************************************************************************/
10
611#ifndef __HANDSET__
712#define __HANDSET__
813
9typedef struct _ti99_handset_config
14#include "emu.h"
15
16#define MAX_HANDSETS 4
17
18extern const device_type HANDSET;
19
20typedef struct _ti99_handset_intf
1021{
11   const char            *sysintf;
12} ti99_handset_config;
22   devcb_write_line   interrupt;
23} ti99_handset_intf;
1324
25class ti99_handset_device : public device_t
26{
27public:
28   ti99_handset_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
29   void set_acknowledge(int data);
1430
15void ti99_handset_task(device_t *device);
16void ti99_handset_set_acknowledge(device_t *device, UINT8 data);
17int ti99_handset_poll_bus(device_t *device);
18int ti99_handset_get_clock(device_t *device);
19int ti99_handset_get_int(device_t *device);
31   // Poll the current state of the 4-bit data bus that goes from the I/R
32   // receiver to the tms9901.
33   int poll_bus()   { return m_buf & 0xf; }
34   int get_clock() { return m_clock; }
35   int get_int()   { return m_buflen==3; }
2036
21#define MCFG_HANDSET_ADD(_tag, _sysintf )   \
22   MCFG_DEVICE_ADD(_tag, HANDSET, 0) \
23   MCFG_DEVICE_CONFIG_DATAPTR(ti99_handset_config, sysintf, _sysintf)
37protected:
38   void device_start(void);
39   void device_reset(void);
40   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
41   void do_task();
42   void post_message(int message);
43   bool poll_keyboard(int num);
44   bool poll_joystick(int num);
2445
25DECLARE_LEGACY_DEVICE( HANDSET, ti99_handset );
46   int      m_ack;
47   int      m_clock;
48   int      m_buf;
49   int      m_buflen;
50   UINT8   previous_joy[MAX_HANDSETS];
51   UINT8   previous_key[MAX_HANDSETS];
52
53   devcb_resolved_write_line   m_interrupt;
54
55   emu_timer *m_delay_timer;
56   emu_timer *m_poll_timer;
57};
58
59#define MCFG_HANDSET_ADD(_tag, _intf, _clock )   \
60   MCFG_DEVICE_ADD(_tag, HANDSET, _clock)   \
61   MCFG_DEVICE_CONFIG(_intf)
62
63#define TI99_HANDSET_INTERFACE(name)   \
64   const ti99_handset_intf(name) =
65
2666#endif
src/mess/machine/ti99/spchsyn.h
r14513r14514
1/*
1/****************************************************************************
2
23    TI-99 Speech Synthesizer
4    See spchsyn.c for documentation
5
36    Michael Zapf, October 2010
4*/
7    February 2012: Rewritten as class
58
9*****************************************************************************/
10
611#ifndef __TISPEECH__
712#define __TISPEECH__
813
9DECLARE_LEGACY_DEVICE( TISPEECH, ti99_speech );
14#include "emu.h"
15#include "peribox.h"
16#include "sound/tms5220.h"
17
18extern const device_type TI99_SPEECH;
19
20class ti_speech_synthesizer_device : public ti_expansion_card_device
21{
22public:
23   ti_speech_synthesizer_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
24   DECLARE_READ8Z_MEMBER(readz);
25   DECLARE_WRITE8_MEMBER(write);
26
27   void crureadz(offs_t offset, UINT8 *value) { };
28   void cruwrite(offs_t offset, UINT8 value) { };
29
30   DECLARE_WRITE_LINE_MEMBER( speech_ready );
31
32   UINT8         *m_speechrom;         // pointer to speech ROM data
33   int          m_load_pointer;         // which 4-bit nibble will be affected by load address
34   int          m_rombits_count;      // current bit position in ROM
35   UINT32         m_sprom_address;      // 18 bit pointer in ROM
36   UINT32         m_sprom_length;         // length of data pointed by speechrom_data, from 0 to 2^18
37
38protected:
39   void         device_start();
40   void         device_reset(void);
41   const rom_entry *device_rom_region() const;
42   machine_config_constructor device_mconfig_additions() const;
43   void         device_config_complete();
44
45private:
46   device_t      *m_vsp;
47};
48
1049#endif
src/mess/machine/at29040a.c
r14513r14514
88
99    Raphael Nabet 2003
1010
11    Rewritten as device
12    Michael Zapf, September 2010
11    September 2010: Rewritten as device
12    February 2012: Rewritten as class
1313*/
1414
15#include "emu.h"
1615#include "at29040a.h"
1716
17#define VERBOSE 2
18#define LOG logerror
19
1820#define FEEPROM_SIZE        0x80000
1921#define SECTOR_SIZE         0x00100
2022#define BOOT_BLOCK_SIZE     0x04000
r14513r14514
2325#define SECTOR_ADDRESS_MASK 0x7ff00
2426#define BYTE_ADDRESS_MASK   0x000ff
2527
26/*
27    at29c40a state
28#define PRG_TIMER 1
2829
29    Command states (s_cmd_0 is the initial state):
30    s_cmd_0: default state
31    s_cmd_1: state after writing aa to 5555
32    s_cmd_2: state after writing 55 to 2aaa
30#define VERSION 0
3331
34    Programming states (s_programming_0 is the initial state):
35    s_programming_0: default state
36    s_programming_1: a program and enable/disable lock command  has been
37        executed, but programming has not actually started.
38    s_programming_2: the programming buffer is being written to
39    s_programming_3: the programming buffer is being burnt to flash ROM
32/*
33    Constructor.
4034*/
41enum  s_cmd_t
35at29040a_device::at29040a_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
36: device_t(mconfig, AT29040A, "ATMEL 29040A 512K*8 FEEPROM", tag, owner, clock),
37  device_nvram_interface(mconfig, *this)
4238{
43   s_cmd_0 = 0x0,
44   s_cmd_1 = 0x1,
45   s_cmd_2 = 0x2
46};
39}
4740
48enum  s_pgm_t
49{
50   s_pgm_0 = 0x0,
51   s_pgm_1 = 0x1,
52   s_pgm_2 = 0x2,
53   s_pgm_3 = 0x3
54};
41//-------------------------------------------------
42//  nvram_default - called to initialize NVRAM to
43//  its default state
44//-------------------------------------------------
5545
56
57typedef struct _at29c040a_state
46void at29040a_device::nvram_default()
5847{
59   UINT8      *memptr;
48   memset(m_eememory, 0, FEEPROM_SIZE+2);
49}
6050
61   int       s_lower_bbl;      /* set when lower boot block lockout is enabled */
62   int       s_higher_bbl;      /* set when upper boot block lockout is enabled */
63   int       s_sdp;            /* set when in software data protect mode */
51//-------------------------------------------------
52//  nvram_read - called to read NVRAM from the
53//  .nv file
54//-------------------------------------------------
6455
65   int       s_id_mode;         /* set when in chip id mode */
66   s_cmd_t      s_cmd;            /* command state */
67   int       s_enabling_bbl;      /* set when a boot block lockout command is expecting its parameter */
68   int       s_cmd_0x80_flag;   /* set if 0x80 command has just been executed (some command require this prefix) */
69   s_pgm_t    s_pgm;            /* programming state */
70   int       s_enabling_sdb;      /* set when a sdp enable command is in progress */
71   int       s_disabling_sdb;   /* set when a sdp disable command is in progress */
72   int       s_dirty;         /* set when the memory contents should be set */
73   UINT8      toggle_bit;
74   UINT8      programming_buffer[SECTOR_SIZE];
75   int       programming_last_offset;
76   emu_timer   *programming_timer;
77} at29c040a_state;
78
79INLINE at29c040a_state *get_safe_token(device_t *device)
56void at29040a_device::nvram_read(emu_file &file)
8057{
81   assert(device != NULL);
82   assert(device->type() == AT29C040A);
83
84   return (at29c040a_state *)downcast<legacy_device_base *>(device)->token();
58   file.read(m_eememory, FEEPROM_SIZE+2);
8559}
8660
87INLINE const at29c040a_config *get_config(device_t *device)
61//-------------------------------------------------
62//  nvram_write - called to write NVRAM to the
63//  .nv file
64//-------------------------------------------------
65
66void at29040a_device::nvram_write(emu_file &file)
8867{
89   assert(device != NULL);
90   assert(device->type() == AT29C040A);
91
92   return (const at29c040a_config *) downcast<const legacy_device_base *>(device)->inline_config();
68   m_eememory[0] = VERSION;
69   file.write(m_eememory, FEEPROM_SIZE+2);
9370}
9471
9572/*
9673    programming timer callback
9774*/
98static TIMER_CALLBACK(at29c040a_programming_timer_callback)
75void at29040a_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
9976{
100   device_t *device = (device_t *)ptr;
101   at29c040a_state *feeprom = get_safe_token(device);
102
103   switch (feeprom->s_pgm)
77   switch (m_pgm)
10478   {
105   case s_pgm_1:
79   case PGM_1:
10680      /* programming cycle timeout */
107      feeprom->s_pgm = s_pgm_0;
81      if (VERBOSE>7) LOG("at29040a: Programming cycle timeout\n");
82      m_pgm = PGM_0;
10883      break;
10984
110   case s_pgm_2:
85   case PGM_2:
11186      /* programming cycle start */
112      feeprom->s_pgm = s_pgm_3;
87      if (VERBOSE>7) LOG("at29040a: Sector write start\n");
88      m_pgm = PGM_3;
11389      /* max delay 10ms, typical delay 5 to 7 ms */
114      feeprom->programming_timer->adjust(attotime::from_msec(5));
90      m_programming_timer->adjust(attotime::from_msec(5));
11591      break;
11692
117   case s_pgm_3:
93   case PGM_3:
11894      /* programming cycle end */
119      memcpy(feeprom->memptr + 2 + (feeprom->programming_last_offset & ~0xff), feeprom->programming_buffer, SECTOR_SIZE);
95      memcpy(m_eememory + 2 + (m_programming_last_offset & ~0xff), m_programming_buffer, SECTOR_SIZE);
96      if (VERBOSE>7) LOG("at29040a: Sector write completed at location %04x + 2\n", (m_programming_last_offset & ~0xff));
97      if (m_enabling_sdb)
98      {
99         m_sdp = true;
100      }
101      if (m_disabling_sdb)
102      {
103         m_sdp = false;
104      }
105      if (VERBOSE>7) LOG("at29040a: Software data protection = %d\n", m_sdp);
120106
121      if (feeprom->s_enabling_sdb)
122         feeprom->s_sdp = TRUE;
107      m_pgm = PGM_0;
108      m_enabling_sdb = false;
109      m_disabling_sdb = false;
123110
124      if (feeprom->s_disabling_sdb)
125         feeprom->s_sdp = FALSE;
126
127      feeprom->s_pgm = s_pgm_0;
128      feeprom->s_enabling_sdb = FALSE;
129      feeprom->s_disabling_sdb = FALSE;
130
131      feeprom->s_dirty = TRUE;
132111      break;
133112
134113   default:
135      logerror("internal error in %s %d\n", __FILE__, __LINE__);
114      if (VERBOSE>1) LOG("internal error in %s %d\n", __FILE__, __LINE__);
136115      break;
137116   }
138117}
139118
140int at29c040a_is_dirty(device_t *device)
119void at29040a_device::sync_flags()
141120{
142   at29c040a_state *feeprom = get_safe_token(device);
143   return feeprom->s_dirty;
144}
121   if (m_lower_bbl) m_eememory[1] |= 0x04;
122   else m_eememory[1] &= ~0x04;
145123
146static void sync_flags(device_t *device)
147{
148   at29c040a_state *feeprom = get_safe_token(device);
149   if (feeprom->s_lower_bbl) feeprom->memptr[1] |= 0x04;
150   else feeprom->memptr[1] &= ~0x04;
124   if (m_higher_bbl) m_eememory[1] |= 0x02;
125   else m_eememory[1] &= ~0x02;
151126
152   if (feeprom->s_higher_bbl) feeprom->memptr[1] |= 0x02;
153   else feeprom->memptr[1] &= ~0x02;
154
155   if (feeprom->s_sdp) feeprom->memptr[1] |= 0x01;
156   else feeprom->memptr[1] &= ~0x01;
127   if (m_sdp) m_eememory[1] |= 0x01;
128   else m_eememory[1] &= ~0x01;
157129}
158130
159131/*
160132    read a byte from FEEPROM
161133*/
162READ8_DEVICE_HANDLER( at29c040a_r )
134READ8_MEMBER( at29040a_device::read )
163135{
164   at29c040a_state *feeprom = get_safe_token(device);
165136   int reply;
166137
167138   offset &= ADDRESS_MASK;
168139
169140   /* reading in the midst of any command sequence cancels it (right???) */
170   feeprom->s_cmd = s_cmd_0;
171   feeprom->s_cmd_0x80_flag = FALSE;
172   feeprom->s_higher_bbl = TRUE;
173   sync_flags(device);
141   m_cmd = CMD_0;
142   m_long_sequence = false;
143   // m_higher_bbl = true;         // who says that?
174144
145   sync_flags();
146
175147   /* reading before the start of a programming cycle cancels it (right???) */
176   if (feeprom->s_pgm == s_pgm_1)
148   if (m_pgm == PGM_1)
177149   {
178      // attempty to access a locked out boot block: cancel programming
150      // attempt to access a locked out boot block: cancel programming
179151      // command if necessary
180      feeprom->s_pgm = s_pgm_0;
181      feeprom->s_enabling_sdb = FALSE;
182      feeprom->s_disabling_sdb = FALSE;
183      feeprom->programming_timer->adjust(attotime::never);
152      m_pgm = PGM_0;
153      m_enabling_sdb = false;
154      m_disabling_sdb = false;
155      m_programming_timer->adjust(attotime::never);
184156   }
185157
186
187   if (feeprom->s_id_mode)
158   if (m_id_mode)
188159   {
189160      switch (offset)
190161      {
191      case 0:
192         reply = 0x1f;
162      case 0x00000:
163         reply = 0x1f;      // Manufacturer code
193164         break;
194165
195      case 1:
196         reply = 0xa4;
166      case 0x00001:
167         reply = 0xa4;      // Device code
197168         break;
198169
199      case 2:
200         reply = feeprom->s_lower_bbl ? 0xff : 0xfe;
170      case 0x00002:
171         reply = m_lower_bbl? 0xff : 0xfe;
201172         break;
202173
203174      case 0x7fff2:
204         reply = feeprom->s_higher_bbl ? 0xff : 0xfe;
175         reply = m_higher_bbl? 0xff : 0xfe;
205176         break;
206177
207178      default:
r14513r14514
209180         break;
210181      }
211182   }
212   else if ((feeprom->s_pgm == s_pgm_2) || (feeprom->s_pgm == s_pgm_3))
183   else if ((m_pgm == PGM_2) || (m_pgm == PGM_3))
213184   {
214      if (feeprom->s_pgm == s_pgm_2)
215      {   /* data polling starts the programming cycle (right???) */
216         feeprom->s_pgm = s_pgm_3;
185      if (m_pgm == PGM_2)
186      {   // DATA* polling starts the programming cycle (right???)
187         m_pgm = PGM_3;
217188         /* max delay 10ms, typical delay 5 to 7 ms */
218         feeprom->programming_timer->adjust(attotime::from_msec(5));
189         m_programming_timer->adjust(attotime::from_msec(5));
219190      }
220191
221      reply = feeprom->toggle_bit;
222      feeprom->toggle_bit ^= 0x02;
223      if ((offset == feeprom->programming_last_offset) && (! (feeprom->programming_buffer[feeprom->programming_last_offset & 0xff] & 0x01)))
192      reply = m_toggle_bit? 0x02 : 0x00;
193      m_toggle_bit = !m_toggle_bit;
194
195      if ((offset == m_programming_last_offset) && (! (m_programming_buffer[m_programming_last_offset & 0xff] & 0x01)))
224196         reply |= 0x01;
225197   }
226198   else
227      reply = feeprom->memptr[offset+2];
199      reply = m_eememory[offset+2];
200
201   if (VERBOSE>7) LOG("at29040a: %05x -> %02x\n", offset, reply);
202
228203   return reply;
229204}
230205
231206/*
232207    Write a byte to FEEPROM
233208*/
234WRITE8_DEVICE_HANDLER( at29c040a_w )
209WRITE8_MEMBER( at29040a_device::write )
235210{
236   at29c040a_state *feeprom = get_safe_token(device);
237
238211   offset &= ADDRESS_MASK;
212   if (VERBOSE>7) LOG("at29040a: %05x <- %02x\n", offset, data);
239213
240214   /* The special CFI commands assume a smaller address space according */
241215   /* to the specification ("address format A14-A0") */
242216   offs_t cfi_offset = offset & 0x7fff;
243217
244   if (feeprom->s_enabling_bbl)
218   if (m_enabling_bbl)
245219   {
246      feeprom->s_enabling_bbl = FALSE;
220      if (VERBOSE>7) LOG("at29040a: Enabling boot block lockout\n");
221      m_enabling_bbl = false;
247222
248223      if ((offset == 0x00000) && (data == 0x00))
249224      {
250         feeprom->s_lower_bbl = TRUE;
251         feeprom->s_dirty = TRUE;
252         sync_flags(device);
225         if (VERBOSE>7) LOG("at29040a: Enabling lower boot block lockout\n");
226         m_lower_bbl = true;
227         sync_flags();
253228         return;
254229      }
255      else if ((offset == 0x7ffff) && (data == 0xff))
230      else
256231      {
257         feeprom->s_higher_bbl = TRUE;
258         feeprom->s_dirty = TRUE;
259         sync_flags(device);
260         return;
232         if ((offset == 0x7ffff) && (data == 0xff))
233         {
234            if (VERBOSE>7) LOG("at29040a: Enabling higher boot block lockout\n");
235            m_higher_bbl = true;
236            sync_flags();
237            return;
238         }
239         else
240         {
241            if (VERBOSE>1) LOG("at29040a: Invalid boot block specification: %05x/%02x\n", offset, data);
242         }
261243      }
262244   }
263245
264   switch (feeprom->s_cmd)
246   switch (m_cmd)
265247   {
266   case s_cmd_0:
248   case CMD_0:
267249      if ((cfi_offset == 0x5555) && (data == 0xaa))
268         feeprom->s_cmd = s_cmd_1;
250      {
251         if (VERBOSE>7) LOG("at29040a: Command sequence started\n");
252         m_cmd = CMD_1;
253      }
269254      else
270255      {
271         feeprom->s_cmd = s_cmd_0;
272         feeprom->s_cmd_0x80_flag = FALSE;
256         m_cmd = CMD_0;
257         m_long_sequence = false;
273258      }
274259      break;
275260
276   case s_cmd_1:
261   case CMD_1:
277262      if ((cfi_offset == 0x2aaa) && (data == 0x55))
278         feeprom->s_cmd = s_cmd_2;
263      {
264         m_cmd = CMD_2;
265      }
279266      else
280267      {
281         feeprom->s_cmd = s_cmd_0;
282         feeprom->s_cmd_0x80_flag = FALSE;
268         m_cmd = CMD_0;
269         m_long_sequence = false;
270         if (VERBOSE>7) LOG("at29040a: Command sequence aborted\n");
283271      }
284272      break;
285273
286   case s_cmd_2:
274   case CMD_2:
287275      if (cfi_offset == 0x5555)
288276      {
289         /* exit programming mode */
290         feeprom->s_pgm = s_pgm_0;
291         feeprom->s_enabling_sdb = FALSE;
292         feeprom->s_disabling_sdb = FALSE;
293         feeprom->programming_timer->adjust(attotime::never);
277         if (!m_long_sequence)
278            if (VERBOSE>7) LOG("at29040a: Command sequence completed\n");
294279
280         m_pgm = PGM_0;
281         m_enabling_sdb = false;
282         m_disabling_sdb = false;
283         m_programming_timer->adjust(attotime::never);
284
295285         /* process command */
296286         switch (data)
297287         {
298288         case 0x10:
299289            /*  Software chip erase */
300            if (feeprom->s_cmd_0x80_flag)
290            if (m_long_sequence)
301291            {
302               if (feeprom->s_lower_bbl || feeprom->s_higher_bbl)
303                  logerror("If the boot block lockout feature has been enabled, the 6-byte software chip erase algorithm will not function.\n");
292               if (m_lower_bbl || m_higher_bbl)
293               {
294                  if (VERBOSE>1) LOG("at29040a: Chip erase sequence deactivated due to previous boot block lockout.\n");
295               }
304296               else
305297               {
306                  memset(feeprom->memptr+2, 0xff, 524288);
307                  feeprom->s_dirty = TRUE;
298                  if (VERBOSE>7) LOG("at29040a: Erase chip\n");
299                  memset(m_eememory+2, 0xff, FEEPROM_SIZE);
308300               }
309301            }
310302            break;
311303
312304         case 0x20:
313305            /* Software data protection disable */
314            if (feeprom->s_cmd_0x80_flag)
306            if (VERBOSE>7) LOG("at29040a: Software data protection disable\n");
307            // The complete sequence is aa-55-80-aa-55-20
308            // so we need a 80 before, else the sequence is invalid
309            if (m_long_sequence)
315310            {
316               feeprom->s_pgm = s_pgm_1;
317               feeprom->s_disabling_sdb = TRUE;
311               m_pgm = PGM_1;
312               m_disabling_sdb = true;
318313               /* set command timeout (right???) */
319               //feeprom->programming_timer->adjust(attotime::from_usec(150), id, 0.);
314               //m_programming_timer->adjust(attotime::from_usec(150), id, 0.);
320315            }
321316            break;
322317
323318         case 0x40:
324319            /* Boot block lockout enable */
325            if (feeprom->s_cmd_0x80_flag)
326               feeprom->s_enabling_bbl = TRUE;
320            // Complete sequence is aa-55-80-aa-55-40
321            if (VERBOSE>7) LOG("at29040a: Boot block lockout enable\n");
322            if (m_long_sequence) m_enabling_bbl = true;
327323            break;
328324
325         case 0x80:
326            m_long_sequence = true;
327            break;
328
329329         case 0x90:
330330            /* Software product identification entry */
331            feeprom->s_id_mode = TRUE;
331            if (VERBOSE>7) LOG("at29040a: Identification mode (start)\n");
332            m_id_mode = true;
332333            break;
333334
334335         case 0xa0:
335336            /* Software data protection enable */
336            feeprom->s_pgm = s_pgm_1;
337            feeprom->s_enabling_sdb = TRUE;
337            if (VERBOSE>7) LOG("at29040a: Software data protection enable\n");
338            m_pgm = PGM_1;
339            m_enabling_sdb = true;
338340            /* set command timeout (right???) */
339            //feeprom->programming_timer->adjust(attotime::from_usec(150), id, 0.);
341            //m_programming_timer->adjust(attotime::from_usec(150), id, 0.);
340342            break;
341343
342344         case 0xf0:
343345            /* Software product identification exit */
344            feeprom->s_id_mode = FALSE;
346            if (VERBOSE>7) LOG("at29040a: Identification mode (end)\n");
347            m_id_mode = false;
345348            break;
346349         }
347         feeprom->s_cmd = s_cmd_0;
348         feeprom->s_cmd_0x80_flag = FALSE;
349         if (data == 0x80)
350            feeprom->s_cmd_0x80_flag = TRUE;
350         m_cmd = CMD_0;
351         if (data != 0x80) m_long_sequence = false;
351352
352353         /* return, because we don't want to write the EEPROM with the command byte */
353354         return;
354355      }
355356      else
356357      {
357         feeprom->s_cmd = s_cmd_0;
358         feeprom->s_cmd_0x80_flag = FALSE;
358         m_cmd = CMD_0;
359         m_long_sequence = false;
359360      }
360361   }
361   if ((feeprom->s_pgm == s_pgm_2)
362         && ((offset & ~0xff) != (feeprom->programming_last_offset & ~0xff)))
362   if ((m_pgm == PGM_2)
363         && ((offset & ~0xff) != (m_programming_last_offset & ~0xff)))
363364   {
364365      /* cancel current programming cycle */
365      feeprom->s_pgm = s_pgm_0;
366      feeprom->s_enabling_sdb = FALSE;
367      feeprom->s_disabling_sdb = FALSE;
368      feeprom->programming_timer->adjust(attotime::never);
366      if (VERBOSE>7) LOG("at29040a: invalid sector change (from %05x to %05x); cancel programming cycle\n",(offset & ~0xff), (m_programming_last_offset & ~0xff));
367      m_pgm = PGM_0;
368      m_enabling_sdb = false;
369      m_disabling_sdb = false;
370      m_programming_timer->adjust(attotime::never);
369371   }
370372
371   if (((feeprom->s_pgm == s_pgm_0) && !feeprom->s_sdp)
372         || (feeprom->s_pgm == s_pgm_1))
373   if (((m_pgm == PGM_0) && !m_sdp)  // write directly
374      || (m_pgm == PGM_1))        // write after unlocking
373375   {
374      if (((offset < BOOT_BLOCK_SIZE) && feeprom->s_lower_bbl)
375         || ((offset >= FEEPROM_SIZE-BOOT_BLOCK_SIZE) && feeprom->s_higher_bbl))
376      if (((offset < BOOT_BLOCK_SIZE) && m_lower_bbl)
377         || ((offset >= FEEPROM_SIZE-BOOT_BLOCK_SIZE) && m_higher_bbl))
376378      {
377         // attempty to access a locked out boot block: cancel programming
379         // attempt to access a locked out boot block: cancel programming
378380         // command if necessary
379         feeprom->s_pgm = s_pgm_0;
380         feeprom->s_enabling_sdb = FALSE;
381         feeprom->s_disabling_sdb = FALSE;
381         if (VERBOSE>7) LOG("at29040a: attempt to access a locked out boot block: offset = %05x, lowblock=%d, highblock=%d\n", offset, m_lower_bbl, m_higher_bbl);
382
383         m_pgm = PGM_0;
384         m_enabling_sdb = false;
385         m_disabling_sdb = false;
382386      }
383387      else
384388      {   /* enter programming mode */
385         memset(feeprom->programming_buffer, 0xff, SECTOR_SIZE);
386         feeprom->s_pgm = s_pgm_2;
389         if (VERBOSE>7) LOG("at29040a: enter programming mode (m_pgm=%d)\n", m_pgm);
390         memset(m_programming_buffer, 0xff, SECTOR_SIZE);
391         m_pgm = PGM_2;
387392      }
388393   }
389   if (feeprom->s_pgm == s_pgm_2)
394   if (m_pgm == PGM_2)
390395   {
391396      /* write data to programming buffer */
392      feeprom->programming_buffer[offset & 0xff] = data;
393      feeprom->programming_last_offset = offset;
394      feeprom->programming_timer->adjust(attotime::from_usec(150));
397      if (VERBOSE>7) LOG("at29040a: Write data to programming buffer\n");
398      m_programming_buffer[offset & 0xff] = data;
399      m_programming_last_offset = offset;
400      m_programming_timer->adjust(attotime::from_usec(150));   // next byte must be written before the timer expires
395401   }
396402}
397403
398static DEVICE_START( at29c040a )
404void at29040a_device::device_start(void)
399405{
400   at29c040a_state *feeprom = get_safe_token(device);
401   feeprom->programming_timer = device->machine().scheduler().timer_alloc(FUNC(at29c040a_programming_timer_callback), (void *)device);
406   m_programming_buffer = (UINT8*)malloc(SECTOR_SIZE);
407   m_programming_timer = timer_alloc(PRG_TIMER);
408
409   m_eememory = (UINT8*)malloc(FEEPROM_SIZE+2);
402410}
403411
404static DEVICE_STOP( at29c040a )
412void at29040a_device::device_stop(void)
405413{
406
414   free(m_programming_buffer);
415   free(m_eememory);
407416}
408417
409static DEVICE_RESET( at29c040a )
418void at29040a_device::device_reset(void)
410419{
411   at29c040a_state *feeprom = get_safe_token(device);
412   const at29c040a_config* atconf = (const at29c040a_config*)get_config(device);
413   feeprom->memptr = (*atconf->get_memory)(device->owner()) + atconf->offset;
414
415   if (feeprom->memptr[0] != 0)
420   if (m_eememory[0] != VERSION)
416421   {
417      logerror("AT29040A: Version mismatch; expected 0 but found %x for %s\n", feeprom->memptr[0], device->tag());
418      return;
422      if (VERBOSE>1) LOG("AT29040A: Warning: Version mismatch; expected %d but found %d for %s. Resetting.\n", VERSION, m_eememory[0], tag());
423      m_eememory[0] = 0;
424      m_eememory[1] = 0;
419425   }
420426
421   feeprom->s_lower_bbl = (feeprom->memptr[1] >> 2) & 1;
422   feeprom->s_higher_bbl = (feeprom->memptr[1] >> 1) & 1;
423   feeprom->s_sdp = feeprom->memptr[1] & 1;
424   feeprom->s_dirty = FALSE;
425}
427   m_lower_bbl =   ((m_eememory[1] & 0x04)!=0);
428   m_higher_bbl =   ((m_eememory[1] & 0x02)!=0);
429   m_sdp =       ((m_eememory[1] & 0x01)!=0);
426430
427static const char DEVTEMPLATE_SOURCE[] = __FILE__;
431   if (VERBOSE>7) LOG("at29040a (%s): LowerBBL = %d, HigherBBL = %d, SoftDataProt = %d\n", tag(), m_lower_bbl, m_higher_bbl, m_sdp);
428432
429#define DEVTEMPLATE_ID(p,s)             p##at29c040a##s
430#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_STOP | DT_HAS_RESET | DT_HAS_INLINE_CONFIG
431#define DEVTEMPLATE_NAME                "ATMEL 29c040a"
432#define DEVTEMPLATE_FAMILY              "Flash Memory"
433#include "devtempl.h"
433   m_id_mode = false;
434   m_cmd = CMD_0;
435   m_enabling_bbl = false;
436   m_long_sequence = false;
437   m_pgm = PGM_0;
438   m_enabling_sdb = false;
439   m_disabling_sdb = false;
440   m_toggle_bit = false;
441   m_programming_last_offset = 0;
442}
434443
435DEFINE_LEGACY_DEVICE( AT29C040A, at29c040a );
444const device_type AT29040A = &device_creator<at29040a_device>;
src/mess/machine/at29040a.h
r14513r14514
11/*
22    ATMEL 29040a
33
4    Rewritten as device
5    Michael Zapf, September 2010
4    Michael Zapf
5    September 2010: Rewritten as device
6    February 2012: Rewritten as class
67*/
78
8/* device interface */
99#ifndef __AT29040__
1010#define __AT29040__
1111
12typedef struct _at29c040a_config
12#include "emu.h"
13
14extern const device_type AT29040A;
15
16/*
17    at29c40a state
18
19    Command states (CMD_0 is the initial state):
20    CMD_0: default state
21    CMD_1: state after writing aa to 5555
22    CMD_2: state after writing 55 to 2aaa
23
24    Programming states (s_programming_0 is the initial state):
25    PGM_0: default state
26    PGM_1: a program and enable/disable lock command has been executed, but programming has not actually started.
27    PGM_2: the programming buffer is being written to
28    PGM_3: the programming buffer is being burnt to flash ROM
29*/
30enum  s_cmd_t
1331{
14   UINT8               *(*get_memory)(device_t *device);
15   offs_t               offset;
32   CMD_0 = 0x0,
33   CMD_1 = 0x1,
34   CMD_2 = 0x2
35};
1636
17} at29c040a_config;
37enum  s_pgm_t
38{
39   PGM_0 = 0x0,
40   PGM_1 = 0x1,
41   PGM_2 = 0x2,
42   PGM_3 = 0x3
43};
1844
19READ8_DEVICE_HANDLER( at29c040a_r );
20WRITE8_DEVICE_HANDLER( at29c040a_w );
45class at29040a_device : public device_t, public device_nvram_interface
46{
47public:
48   at29040a_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
49   DECLARE_READ8_MEMBER( read );
50   DECLARE_WRITE8_MEMBER( write );
2151
22int at29c040a_is_dirty(device_t *device);
52protected:
53   void device_start(void);
54   void device_reset(void);
55   void device_stop(void);
56   void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
2357
24DECLARE_LEGACY_DEVICE( AT29C040A, at29c040a );
58   void nvram_default();
59   void nvram_read(emu_file &file);
60   void nvram_write(emu_file &file);
2561
26#define MCFG_AT29C040_ADD_P(_tag, _memptr, _offset)   \
27   MCFG_DEVICE_ADD(_tag, AT29C040A, 0)   \
28   MCFG_DEVICE_CONFIG_DATAPTR(at29c040a_config, get_memory, _memptr) \
29   MCFG_DEVICE_CONFIG_DATA32(at29c040a_config, offset, _offset)
62private:
63   void      sync_flags(void);
3064
65   UINT8*      m_eememory;
66
67   bool      m_lower_bbl;      /* set when lower boot block lockout is enabled */
68   bool      m_higher_bbl;      /* set when upper boot block lockout is enabled */
69   bool      m_sdp;            /* set when in software data protect mode */
70
71   bool      m_id_mode;         /* set when in chip id mode */
72   s_cmd_t      m_cmd;            /* command state */
73   bool      m_enabling_bbl;      /* set when a boot block lockout command is expecting its parameter */
74   bool      m_long_sequence;   /* set if 0x80 command has just been executed (some command require this prefix) */
75   s_pgm_t    m_pgm;            /* programming state */
76   bool      m_enabling_sdb;      /* set when a sdp enable command is in progress */
77   bool      m_disabling_sdb;   /* set when a sdp disable command is in progress */
78   bool      m_dirty;         /* set when the memory contents should be set */
79   bool      m_toggle_bit;      // indicates flashing in progress (toggles for each query)
80   UINT8*      m_programming_buffer;
81   int       m_programming_last_offset;
82   emu_timer*   m_programming_timer;
83};
84
85#define MCFG_AT29040A_ADD(_tag )   \
86   MCFG_DEVICE_ADD(_tag, AT29040A, 0)
87
3188#endif
3289
src/mess/machine/smc92x4.c
r14513r14514
11/*
2    HDC9224 and HDC9234 Hard and Floppy Disk Controller (HFDC)
2    HDC9224 and HDC9234 Hard and Floppy Disk Controller
33
44    This controller handles MFM and FM encoded floppy disks and hard disks.
55    The SMC9224 is used in some DEC systems.  The HDC9234 is used in the
r14513r14514
1616*/
1717
1818#include "emu.h"
19#include "formats/imageutl.h"
20#include "imagedev/flopdrv.h"
21#include "imagedev/harddriv.h"
22#include "harddisk.h"
23
2419#include "smc92x4.h"
2520
2621/***************************************************************************
r14513r14514
134129
135130#define MAX_SECTOR_LEN 256
136131
137#define FORMAT_LONG FALSE
132#define FORMAT_LONG false
138133
139134#define ERROR   0
140135#define DONE   1
141136#define AGAIN   2
142137#define UNDEF   3
143138
144typedef struct _smc92x4_state
139enum
145140{
146   devcb_resolved_write_line   out_intrq_func;      /* INT line */
147   devcb_resolved_write_line   out_dip_func;      /* DMA in progress line */
148   devcb_resolved_write8      out_auxbus_func;   /* AB0-7 lines (using S0,S1 as address) */
149   devcb_resolved_read_line   in_auxbus_func;      /* AB0-7 lines (only single case) */
141   DATA_TIMER,
142   READ_TIMER,
143   WRITE_TIMER,
144   SEEK_TIMER,
145   TRACK_TIMER
146};
150147
151   UINT8 output1;      /* UDC-internal register "output1" */
152   UINT8 output2;      /* UDC-internal register "output2" */
153
154   int   selected_drive_type;
155   // We need to store the types, although this is not the case in the
156   // real hardware. The reason is that poll_drives wants to select the
157   // drives without knowing their kinds. In reality this does not matter,
158   // since we only check for the seek_complete line, but in our case,
159   // floppy and harddrive are pretty different, and we need to know which
160   // one to check.
161   int   types[4];
162   int   head_load_delay_enable;
163   int   register_pointer;
164
165   UINT8 register_r[12];
166   UINT8 register_w[12];
167
168   /* timers to delay execution/completion of commands */
169   emu_timer *timer_data, *timer_rs, *timer_ws, *timer_seek, *timer_track;
170
171   /* Flag which determines whether to use realistic timing. */
172   int use_real_timing;
173
174   /* Required to store the current iteration within restore. */
175   int seek_count;
176
177   /* Recent command. */
178   UINT8 command;
179
180   /* Stores the step direction. */
181   int step_direction;
182
183   /* Stores the buffered flag. */
184   int buffered;
185
186   /* Stores the recent id field. */
187   chrn_id_hd recent_id;
188
189   /* Indicates whether an ID has been found. */
190   int found_id;
191
192   /* Indicates whether we are already past the seek_id phase in read/write sector. */
193   int after_seek;
194
195   /* Determines whether the command will be continued. */
196   int to_be_continued;
197
198   /* Pointer to interface */
199   const smc92x4_interface *intf;
200
201} smc92x4_state;
202
203148/*
204149    Step rates in microseconds for MFM. This is set in the mode register,
205150    bits 0-2. Single density doubles all values.
r14513r14514
242187#define VERBOSE 1
243188#define LOG logerror
244189
245/***************************************************************************
246    PROTOTYPES
247***************************************************************************/
248
249static void smc92x4_process_after_callback(device_t *device);
250static void data_transfer_read(device_t *device, chrn_id_hd id, int transfer_enable);
251static void data_transfer_write(device_t *device, chrn_id_hd id, int deldata, int redcur, int precomp, int write_long);
252
253#if 0
254static void dump_contents(UINT8 *buffer, int length)
190smc92x4_device::smc92x4_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
191: device_t(mconfig, SMC92X4, "SMC 9224/9234 Hard/Floppy Disk Controller", tag, owner, clock)
255192{
256   for (int i=0; i < length; i+=16)
257   {
258      for (int j=0; j < 16; j+=2)
259      {
260         printf("%02x%02x ", buffer[i+j], buffer[i+j+1]);
261      }
262      printf("\n");
263   }
264193}
265#endif
266194
267INLINE smc92x4_state *get_safe_token(device_t *device)
195int smc92x4_device::image_is_single_density()
268196{
269   assert(device != NULL);
270   return (smc92x4_state *)downcast<legacy_device_base *>(device)->token();
271}
272
273
274static int image_is_single_density(device_t *current_floppy)
275{
276   floppy_image_legacy *image = flopimg_get_image(current_floppy);
197   floppy_image_legacy *image = flopimg_get_image(m_drive);
277198   return (floppy_get_track_size(image, 0, 0)<4000);
278199}
279200
280static int controller_set_to_single_density(device_t *device)
201bool smc92x4_device::in_single_density_mode()
281202{
282   smc92x4_state *w = get_safe_token(device);
283   return ((w->register_w[MODE]&MO_DENSITY)!=0);
203   return ((m_register_w[MODE]&MO_DENSITY)!=0);
284204}
285205
286static void copyid(chrn_id id1, chrn_id_hd *id2)
206void smc92x4_device::copyid(chrn_id id1, chrn_id_hd *id2)
287207{
288208   id2->C = id1.C & 0xff;
289209   id2->H = id1.H;
r14513r14514
296216/*
297217    Set IRQ
298218*/
299static void smc92x4_set_interrupt(device_t *device)
219void smc92x4_device::set_interrupt()
300220{
301   smc92x4_state *w = get_safe_token(device);
302   if ((w->register_r[INT_STATUS] & ST_INTPEND) == 0)
221   if ((m_register_r[INT_STATUS] & ST_INTPEND) == 0)
303222   {
304      w->register_r[INT_STATUS] |= ST_INTPEND;
305      w->out_intrq_func(ASSERT_LINE);
223      m_register_r[INT_STATUS] |= ST_INTPEND;
224      m_out_intrq(ASSERT_LINE);
306225   }
307226}
308227
309228/*
310    Set DMA in progress
311*/
312static void smc92x4_set_dip(device_t *device, int value)
313{
314   smc92x4_state *w = get_safe_token(device);
315   w->out_dip_func(value);
316}
317
318/*
319229    Assert Command Done status bit, triggering interrupts as needed
320230*/
321static void set_command_done(device_t *device, int flags)
231void smc92x4_device::set_command_done(int flags)
322232{
323   //assert(! (w->status & ST_DONE))
324   smc92x4_state *w = get_safe_token(device);
325   if (VERBOSE>7) LOG("smc92x4 command %02x done, flags=%02x\n", w->command, flags);
233   //assert(! (m_status & ST_DONE))
234   if (VERBOSE>7) LOG("smc92x4 command %02x done, flags=%02x\n", m_command, flags);
326235
327   w->register_r[INT_STATUS] |= ST_DONE;
328   w->register_r[INT_STATUS] &= ~ST_TERMCOD; /* clear the previously set flags */
329   w->register_r[INT_STATUS] |= flags;
236   m_register_r[INT_STATUS] |= ST_DONE;
237   m_register_r[INT_STATUS] &= ~ST_TERMCOD; /* clear the previously set flags */
238   m_register_r[INT_STATUS] |= flags;
330239
331240   /* sm92x4 spec, p. 6 */
332   if (w->register_w[INT_COMM_TERM] & TC_INTDONE)
333      smc92x4_set_interrupt(device);
241   if (m_register_w[INT_COMM_TERM] & TC_INTDONE)
242      set_interrupt();
334243}
335244
336245/*
337246    Preserve previously set termination code
338247*/
339static void set_command_done(device_t *device)
248void smc92x4_device::set_command_done()
340249{
341   //assert(! (w->status & ST_DONE))
342   smc92x4_state *w = get_safe_token(device);
343   if (VERBOSE>7) LOG("smc92x4 command %02x done\n", w->command);
250   //assert(! (m_status & ST_DONE))
251   if (VERBOSE>7) LOG("smc92x4 command %02x done\n", m_command);
344252
345   w->register_r[INT_STATUS] |= ST_DONE;
253   m_register_r[INT_STATUS] |= ST_DONE;
346254
347255   /* sm92x4 spec, p. 6 */
348   if (w->register_w[INT_COMM_TERM] & TC_INTDONE)
349      smc92x4_set_interrupt(device);
256   if (m_register_w[INT_COMM_TERM] & TC_INTDONE)
257      set_interrupt();
350258}
351259
352260/*
353261    Clear IRQ
354262*/
355static void smc92x4_clear_interrupt(device_t *device)
263void smc92x4_device::clear_interrupt()
356264{
357   smc92x4_state *w = get_safe_token(device);
358   if ((w->register_r[INT_STATUS] & ST_INTPEND) != 0)
359   {
360      w->out_intrq_func(CLEAR_LINE);
361   }
265   if ((m_register_r[INT_STATUS] & ST_INTPEND) != 0)
266      m_out_intrq(CLEAR_LINE);
362267}
363268
364269/*
365270    Sets the DMA address on the external counter. This counter is attached
366271    to the auxiliary bus on the PCB.
367272*/
368static void set_dma_address(device_t *device, int pos2316, int pos1508, int pos0700)
273void smc92x4_device::set_dma_address(int pos2316, int pos1508, int pos0700)
369274{
370   smc92x4_state *w = get_safe_token(device);
371   w->out_auxbus_func(OUTPUT_DMA_ADDR, w->register_r[pos2316]);
372   w->out_auxbus_func(OUTPUT_DMA_ADDR, w->register_r[pos1508]);
373   w->out_auxbus_func(OUTPUT_DMA_ADDR, w->register_r[pos0700]);
275   m_out_auxbus(OUTPUT_DMA_ADDR, m_register_r[pos2316]);
276   m_out_auxbus(OUTPUT_DMA_ADDR, m_register_r[pos1508]);
277   m_out_auxbus(OUTPUT_DMA_ADDR, m_register_r[pos0700]);
374278}
375279
376static void dma_add_offset(device_t *device, int offset)
280void smc92x4_device::dma_add_offset(int offset)
377281{
378   smc92x4_state *w = get_safe_token(device);
379   int dma_address = (w->register_w[DMA23_16]<<16) + (w->register_w[DMA15_8]<<8) + w->register_w[DMA7_0];
282   int dma_address = (m_register_w[DMA23_16]<<16) + (m_register_w[DMA15_8]<<8) + m_register_w[DMA7_0];
380283   dma_address += offset;
381284
382   w->register_w[DMA23_16] = w->register_r[DMA23_16] = (dma_address & 0xff0000)>>16;
383   w->register_w[DMA15_8]  = w->register_r[DMA15_8]  = (dma_address & 0x00ff00)>>8;
384   w->register_w[DMA7_0]   = w->register_r[DMA7_0]   = (dma_address & 0x0000ff);
285   m_register_w[DMA23_16] = m_register_r[DMA23_16] = (dma_address & 0xff0000)>>16;
286   m_register_w[DMA15_8]  = m_register_r[DMA15_8]  = (dma_address & 0x00ff00)>>8;
287   m_register_w[DMA7_0]   = m_register_r[DMA7_0]   = (dma_address & 0x0000ff);
385288}
386289
387290/*
r14513r14514
390293    both the hard and floppy drives during S0=S1=0 and STB*=0 times via the
391294    auxiliary bus.
392295*/
393static void sync_status_in(device_t *device)
296void smc92x4_device::sync_status_in()
394297{
395298   UINT8 prev;
396   smc92x4_state *w = get_safe_token(device);
299   prev = m_register_r[DRIVE_STATUS];
300   m_register_r[DRIVE_STATUS] = m_in_auxbus(0);
397301
398   prev = w->register_r[DRIVE_STATUS];
399   w->register_r[DRIVE_STATUS] = w->in_auxbus_func();
400
401302   /* Raise interrupt if ready changes. TODO: Check this more closely. */
402   if (((w->register_r[DRIVE_STATUS] & DS_READY) != (prev & DS_READY))
403      & (w->register_r[INT_STATUS] & ST_RDYCHNG))
303   if (((m_register_r[DRIVE_STATUS] & DS_READY) != (prev & DS_READY))
304      & (m_register_r[INT_STATUS] & ST_RDYCHNG))
404305   {
405      smc92x4_set_interrupt(device);
306      set_interrupt();
406307   }
407308}
408309
r14513r14514
410311    Push the output registers over the auxiliary bus. It is expected that
411312    the PCB contains latches to store the values.
412313*/
413static void sync_latches_out(device_t *device)
314void smc92x4_device::sync_latches_out()
414315{
415   smc92x4_state *w = get_safe_token(device);
416   w->output1 = (w->output1 & 0xf0) | (w->register_w[RETRY_COUNT]&0x0f);
417
418   w->out_auxbus_func(OUTPUT_OUTPUT1, w->output1);
419   w->out_auxbus_func(OUTPUT_OUTPUT2, w->output2);
316   m_output1 = (m_output1 & 0xf0) | (m_register_w[RETRY_COUNT]&0x0f);
317   m_out_auxbus(OUTPUT_OUTPUT1, m_output1);
318   m_out_auxbus(OUTPUT_OUTPUT2, m_output2);
420319}
421320
422321/*************************************************************
r14513r14514
424323*************************************************************/
425324#if 0
426325/* setup a timed data request - data request will be triggered in a few usecs time */
427static void smc92x4_timed_data_request(device_t *device)
326void smc92x4_device::timed_data_request()
428327{
429   smc92x4_state *w = get_safe_token(device);
430   int time = controller_set_to_single_density(device)? 128 : 32;
328   int time = in_single_density_mode()? 128 : 32;
431329
432   if (!w->use_real_timing)
330   if (!m_use_real_timing)
433331      time = 1;
434332
435333   /* set new timer */
436   w->timer_data->adjust(attotime::from_usec(time));
334   m_timer_data->adjust(attotime::from_usec(time));
437335}
438336#endif
439337
440338/* setup a timed read sector - read sector will be triggered in a few usecs time */
441static void smc92x4_timed_sector_read_request(device_t *device)
339void smc92x4_device::timed_sector_read_request()
442340{
443   smc92x4_state *w = get_safe_token(device);
444341   int time=0;
445342
446   /* set new timer */
447   /* Average time from sector to sector. */
448   if (w->selected_drive_type & TYPE_FLOPPY)
449      time = (controller_set_to_single_density(device))? 30000 : 15000;
343   // set new timer
344   // Average time from sector to sector.
345   if (m_selected_drive_type & TYPE_FLOPPY)
346      time = (in_single_density_mode())? 30000 : 15000;
450347   else
451348      time = 1000;
452349
453   if (!w->use_real_timing)
350   if (!m_use_real_timing)
454351      time = 1;
455352
456   w->timer_rs->adjust(attotime::from_usec(time));
457   w->to_be_continued = TRUE;
353   m_timer_rs->adjust(attotime::from_usec(time));
354   m_to_be_continued = true;
458355}
459356
460357/* setup a timed write sector - write sector will be triggered in a few usecs time */
461static void smc92x4_timed_sector_write_request(device_t *device)
358void smc92x4_device::timed_sector_write_request()
462359{
463   smc92x4_state *w = get_safe_token(device);
464360   int time=0;
465361
466362   /* Average time from sector to sector. */
467   if (w->selected_drive_type & TYPE_FLOPPY)
468      time = (controller_set_to_single_density(device))? 30000 : 15000;
363   if (m_selected_drive_type & TYPE_FLOPPY)
364      time = (in_single_density_mode())? 30000 : 15000;
469365   else
470366      time = 1000;
471367
472   if (!w->use_real_timing)
368   if (!m_use_real_timing)
473369      time = 1;
474370
475   w->timer_ws->adjust(attotime::from_usec(time));
476   w->to_be_continued = TRUE;
371   m_timer_ws->adjust(attotime::from_usec(time));
372   m_to_be_continued = true;
477373}
478374
479375/*
480376    Set up a timed track read/write
481377*/
482static void smc92x4_timed_track_request(device_t *device)
378void smc92x4_device::timed_track_request()
483379{
484   smc92x4_state *w = get_safe_token(device);
485380   int time = 0;
486381
487   if (w->selected_drive_type & TYPE_FLOPPY)
382   if (m_selected_drive_type & TYPE_FLOPPY)
488383      time = TRACKTIME_FLOPPY;
489384   else
490385      time = TRACKTIME_HD;
491386
492   if (!w->use_real_timing)
387   if (!m_use_real_timing)
493388      time = 1;
494389
495   w->timer_track->adjust(attotime::from_usec(time));
390   m_timer_track->adjust(attotime::from_usec(time));
496391
497   w->to_be_continued = TRUE;
392   m_to_be_continued = true;
498393}
499394
500395/*
501396    Set up a timed track seek
502397*/
503static void smc92x4_timed_seek_request(device_t *device)
398void smc92x4_device::timed_seek_request()
504399{
505   smc92x4_state *w = get_safe_token(device);
506400   int time = 0;
507401
508   int index = w->register_w[MODE] & MO_STEPRATE;
509   int fm = controller_set_to_single_density(device);
402   int index = m_register_w[MODE] & MO_STEPRATE;
403   int fm = in_single_density_mode();
510404
511405   /* Get seek time. */
512   if ((w->selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY8)
406   if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY8)
513407      time = step_flop8[index];
514408
515   else if ((w->selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY5)
409   else if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_FLOPPY5)
516410      time = step_flop5[index];
517411   else
518412      time = step_hd[index];
r14513r14514
520414   if (fm)
521415      time = time * 2;
522416
523   if (!w->use_real_timing)
417   if (!m_use_real_timing)
524418   {
525419      if (VERBOSE>5) LOG("smc92x4 info: Disk access without delays\n");
526420      time = 1;
527421   }
528
529   w->timer_seek->adjust(attotime::from_usec(time));
530   w->to_be_continued = TRUE;
422   m_timer_seek->adjust(attotime::from_usec(time));
423   m_to_be_continued = true;
531424}
532425
533#if 0
534static TIMER_CALLBACK(smc92x4_data_callback)
426void smc92x4_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
535427{
536/*  device_t *device = (device_t *)ptr;
537    smc92x4_state *w = get_safe_token(device); */
538}
539#endif
428   int transfer_enabled;
429   int deldata;
430   int redcur;
431   int precomp;
432   int write_long;
433   mfm_harddisk_device *harddisk;
540434
541static TIMER_CALLBACK(smc92x4_read_sector_callback)
542{
543   device_t *device = (device_t *)ptr;
544   smc92x4_state *w = get_safe_token(device);
545   int transfer_enabled = w->command & 0x01;
435   switch (id)
436   {
437   case DATA_TIMER:
438      // Not implemented yet
439      break;
440   case READ_TIMER:
441      transfer_enabled = m_command & 0x01;
442      // Now read the sector.
443      data_transfer_read(m_recent_id, transfer_enabled);
444      sync_status_in();
445      break;
446   case WRITE_TIMER:
447      deldata = m_command & 0x10;
448      redcur =  m_command & 0x08;
449      precomp = m_command & 0x07;
450      write_long = ((m_register_w[MODE]& MO_CRCECC)==0x40);
546451
547   /* Now read the sector. */
548   data_transfer_read(device, w->recent_id, transfer_enabled);
549   sync_status_in(device);
550   smc92x4_process_after_callback(device);
551}
452      if (deldata)
453         if (VERBOSE>0) LOG("smc92x4 warn: Write deleted data mark not supported. Writing normal mark.\n");
552454
553static TIMER_CALLBACK(smc92x4_write_sector_callback)
554{
555   device_t *device = (device_t *)ptr;
556   smc92x4_state *w = get_safe_token(device);
557   int deldata = w->command & 0x10;
558   int redcur =  w->command & 0x08;
559   int precomp = w->command & 0x07;
560   int write_long = ((w->register_w[MODE]& MO_CRCECC)==0x40);
455      // Now write the sector.
456      data_transfer_write(m_recent_id, deldata, redcur, precomp, write_long);
457      sync_status_in();
458      break;
459   case SEEK_TIMER:
460      // Callback for seek request.
461      /*  int buffered = ((m_register_w[MODE] & MO_STEPRATE)==0); */
462      /*  int buffered = (m_command & 0x01); */
561463
562   if (deldata)
563      if (VERBOSE>0) LOG("smc92x4 warn: Write deleted data mark not supported. Writing normal mark.\n");
564
565   /* Now write the sector. */
566   data_transfer_write(device, w->recent_id, deldata, redcur, precomp, write_long);
567   sync_status_in(device);
568   smc92x4_process_after_callback(device);
569}
570
571/*
572    Callback for track access. Nothing interesting here, just used for
573    delaying.
574*/
575static TIMER_CALLBACK(smc92x4_track_callback)
576{
577   device_t *device = (device_t *)ptr;
578   sync_status_in(device);
579   smc92x4_process_after_callback(device);
580}
581
582/*
583    Callback for seek request.
584*/
585static TIMER_CALLBACK(smc92x4_seek_callback)
586{
587   device_t *device = (device_t *)ptr;
588   smc92x4_state *w = get_safe_token(device);
589   device_t *current_floppy = NULL;
590/*  int buffered = ((w->register_w[MODE] & MO_STEPRATE)==0); */
591/*  int buffered = (w->command & 0x01); */
592
593   if (w->selected_drive_type & TYPE_FLOPPY)
594   {
595      current_floppy = (*w->intf->current_floppy)(device);
596      if (current_floppy==NULL)
464      if (m_selected_drive_type & TYPE_FLOPPY)
597465      {
598         if (VERBOSE>0) LOG("smc92x4 error: seek callback: no floppy\n");
599         w->register_r[INT_STATUS] |= ST_TC_SEEKERR;
466         if (m_drive==NULL)
467         {
468            if (VERBOSE>0) LOG("smc92x4 error: seek callback: no floppy\n");
469            m_register_r[INT_STATUS] |= ST_TC_SEEKERR;
470         }
471         else
472         {
473            if (VERBOSE>5) LOG("smc92x4 step %s direction %d\n", m_drive->tag(), m_step_direction);
474            floppy_drive_seek(m_drive, m_step_direction);
475         }
600476      }
601477      else
602478      {
603         if (VERBOSE>5) LOG("smc92x4 step %s direction %d\n", current_floppy->tag(), w->step_direction);
604         floppy_drive_seek(current_floppy, w->step_direction);
479         if (VERBOSE>6) LOG("smc92x4 step harddisk direction %d\n", m_step_direction);
480         harddisk = static_cast<mfm_harddisk_device *>(m_drive);
481         harddisk->seek(m_step_direction);
605482      }
483      sync_status_in();
484      break;
485   case TRACK_TIMER:
486      // Callback for track access. Nothing interesting here, just used for
487      // delaying.
488      break;
606489   }
607   else
608   {
609      if (VERBOSE>6) LOG("smc92x4 step harddisk direction %d\n", w->step_direction);
610      (*w->intf->mfmhd_seek)(device, w->step_direction);
611   }
612   sync_status_in(device);
613
614   smc92x4_process_after_callback(device);
490   process_after_callback();
615491}
616492
493
617494/*********************************************************************
618495    Common functions
619496*********************************************************************/
r14513r14514
623500    define idents beyond cylinder 1023, but formatting programs seem to
624501    continue with 0xfd for cylinders between 1024 and 2047.
625502*/
626UINT8 cylinder_to_ident(int cylinder)
503UINT8 smc92x4_device::cylinder_to_ident(int cylinder)
627504{
628505   if (cylinder < 256) return 0xfe;
629506   if (cylinder < 512) return 0xff;
r14513r14514
636513    Calculate the offset from the ident. This is only useful for AT drives;
637514    these drives cannot have more than 1024 cylinders.
638515*/
639static int ident_to_cylinder(UINT8 ident)
516int smc92x4_device::ident_to_cylinder(UINT8 ident)
640517{
641518   switch (ident)
642519   {
r14513r14514
651528/*
652529    Common function to set the read registers from the recently read id.
653530*/
654static void update_id_regs(device_t *device, chrn_id_hd id)
531void smc92x4_device::update_id_regs(chrn_id_hd id)
655532{
656533   // Flags for current head register. Note that the sizes are not in
657534   // sequence (128, 256, 512, 1024). This is only interesting for AT
658535   // mode.
659536   static const UINT8 sizeflag[] = { 0x60, 0x00, 0x20, 0x40 };
660   smc92x4_state *w = get_safe_token(device);
661537
662   w->register_r[CURRENT_CYLINDER] = id.C & 0xff;
538   m_register_r[CURRENT_CYLINDER] = id.C & 0xff;
539   m_register_r[CURRENT_HEAD] = id.H & 0x0f;
663540
664   w->register_r[CURRENT_HEAD] = id.H & 0x0f;
665
666541   if (id.flags & BAD_SECTOR)
667      w->register_r[CURRENT_HEAD] |= 0x80;
542      m_register_r[CURRENT_HEAD] |= 0x80;
668543
669   if ((w->selected_drive_type & DRIVE_TYPE) == TYPE_AT)
670      w->register_r[CURRENT_HEAD] |= sizeflag[id.N];
544   if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
545      m_register_r[CURRENT_HEAD] |= sizeflag[id.N];
671546   else
672      w->register_r[CURRENT_HEAD] |= ((id.C & 0x700)>>4);
547      m_register_r[CURRENT_HEAD] |= ((id.C & 0x700)>>4);
673548
674   w->register_r[CURRENT_IDENT] = cylinder_to_ident(id.C);
549   m_register_r[CURRENT_IDENT] = cylinder_to_ident(id.C);
675550}
676551
677552/*
678553    Common procedure: read_id_field (as described in the specification)
679554*/
680static void read_id_field(device_t *device, int *steps, int *direction, chrn_id_hd *id)
555void smc92x4_device::read_id_field(int *steps, int *direction, chrn_id_hd *id)
681556{
682   smc92x4_state *w = get_safe_token(device);
683557   int des_cylinder, cur_cylinder;
684   int found = FALSE;
685   sync_latches_out(device);
686   sync_status_in(device);
558   bool found = false;
559   mfm_harddisk_device *harddisk;
687560
561   sync_latches_out();
562   sync_status_in();
563
688564   // Set command termination code. The error code is set first, and
689565   // on success, it is cleared.
690   w->register_r[INT_STATUS] |= ST_TC_RDIDERR;
566   m_register_r[INT_STATUS] |= ST_TC_RDIDERR;
691567
692568   /* Try to find an ID field. */
693   if (w->selected_drive_type & TYPE_FLOPPY)
569   if (m_selected_drive_type & TYPE_FLOPPY)
694570   {
695571      chrn_id idflop;
696      device_t *disk_img = (*w->intf->current_floppy)(device);
697      if (flopimg_get_image(disk_img) == NULL)
572      if (flopimg_get_image(m_drive) == NULL)
698573      {
699574         if (VERBOSE>2) LOG("smc92x4 warn: No disk in drive\n");
700         w->register_r[CHIP_STATUS] |= CS_SYNCERR;
575         m_register_r[CHIP_STATUS] |= CS_SYNCERR;
701576         return;
702577      }
703578
704579      /* Check whether image and controller are set to the same density. */
705      if ((image_is_single_density(disk_img) && controller_set_to_single_density(device))
706         || (!image_is_single_density(disk_img) && !controller_set_to_single_density(device))
707         )
580      if ((image_is_single_density() && in_single_density_mode())
581         || (!image_is_single_density() && !in_single_density_mode()))
708582      {
709         found = floppy_drive_get_next_id(disk_img, w->register_w[DESIRED_HEAD]&0x0f, &idflop);
583         found = floppy_drive_get_next_id(m_drive, m_register_w[DESIRED_HEAD]&0x0f, &idflop);
710584         copyid(idflop, id); /* Need to use bigger values for HD, but we don't use separate variables here */
711585      }
712586      else
713587      {
714588         if (VERBOSE>2) LOG("smc92x4 warn: Controller and medium density do not match.\n");
715589      }
716      sync_status_in(device);
590      sync_status_in();
717591
718592      if (!found)
719593      {
720594         if (VERBOSE>1) LOG("smc92x4 error: read_id_field (floppy): sync error\n");
721         w->register_r[CHIP_STATUS] |= CS_SYNCERR;
595         m_register_r[CHIP_STATUS] |= CS_SYNCERR;
722596         return;
723597      }
724598   }
725599   else
726600   {
727      (*w->intf->mfmhd_get_next_id)(device, w->register_w[DESIRED_HEAD]&0x0f, id);
728      sync_status_in(device);
729      if (!(w->register_r[DRIVE_STATUS]& DS_READY))
601      harddisk = static_cast<mfm_harddisk_device *>(m_drive);
602
603      harddisk->get_next_id(m_register_w[DESIRED_HEAD]&0x0f, id);
604      sync_status_in();
605      if (!(m_register_r[DRIVE_STATUS]& DS_READY))
730606      {
731607         if (VERBOSE>1) LOG("smc92x4 error: read_id_field (harddisk): sync error\n");
732         w->register_r[CHIP_STATUS] |= CS_SYNCERR;
608         m_register_r[CHIP_STATUS] |= CS_SYNCERR;
733609         return;
734610      }
735611   }
736612
737   w->register_r[CHIP_STATUS] &= ~CS_SYNCERR;
613   m_register_r[CHIP_STATUS] &= ~CS_SYNCERR;
738614
739615   /* Update the registers. */
740   update_id_regs(device, *id);
616   update_id_regs(*id);
741617
742618   if (id->flags & BAD_CRC)
743619   {
744      w->register_r[CHIP_STATUS] |= CS_CRCERR;
620      m_register_r[CHIP_STATUS] |= CS_CRCERR;
745621      return;
746622   }
747623
748624   /* Calculate the steps. */
749   if ((w->selected_drive_type & DRIVE_TYPE) == TYPE_AT)
625   if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
750626   {
751627      /* Note: the spec says CURRENT_REGISTER, but that seems wrong. */
752      des_cylinder = ((w->register_r[DATA] & 0x03)<<8) | w->register_w[DESIRED_CYLINDER];
753      cur_cylinder = ident_to_cylinder(w->register_r[CURRENT_IDENT]) + w->register_r[CURRENT_CYLINDER];
628      des_cylinder = ((m_register_r[DATA] & 0x03)<<8) | m_register_w[DESIRED_CYLINDER];
629      cur_cylinder = ident_to_cylinder(m_register_r[CURRENT_IDENT]) + m_register_r[CURRENT_CYLINDER];
754630   }
755631   else
756632   {
757      des_cylinder = ((w->register_w[DESIRED_HEAD] & 0x70)<<4) | w->register_w[DESIRED_CYLINDER];
758      cur_cylinder = ((w->register_r[CURRENT_HEAD] & 0x70)<<4) | w->register_r[CURRENT_CYLINDER];
633      des_cylinder = ((m_register_w[DESIRED_HEAD] & 0x70)<<4) | m_register_w[DESIRED_CYLINDER];
634      cur_cylinder = ((m_register_r[CURRENT_HEAD] & 0x70)<<4) | m_register_r[CURRENT_CYLINDER];
759635   }
760636
761637   if (des_cylinder >= cur_cylinder)
r14513r14514
771647
772648   if (VERBOSE>6) LOG("smc92x4 seek required: %d steps\n", *steps);
773649
774   w->register_r[INT_STATUS] &= ~ST_TC_RDIDERR;
775   return;
650   m_register_r[INT_STATUS] &= ~ST_TC_RDIDERR;
776651}
777652
778653/*
779654    Common procedure: verify (as described in the specification)
780655*/
781static int verify(device_t *device, chrn_id_hd *id, int check_sector)
656int smc92x4_device::verify(chrn_id_hd *id, bool check_sector)
782657{
783   smc92x4_state *w = get_safe_token(device);
784658   int maxtry = 132;  /* approx. 33792/(256*32) */
785659   int pass = 0;
786   int found = FALSE;
787   int foundsect = FALSE;
660   int found = false;
661   int foundsect = false;
788662   int des_cylinder = 0;
663   mfm_harddisk_device *harddisk;
789664
790665   // Set command termination code. The error code is set first, and
791666   // on success, it is cleared.
792   w->register_r[INT_STATUS] |= ST_TC_SEEKERR;
667   m_register_r[INT_STATUS] |= ST_TC_SEEKERR;
793668
794669   while (pass < maxtry && !foundsect)
795670   {
796671      pass++;
797672      /* Try to find an ID field. */
798      if (w->selected_drive_type & TYPE_FLOPPY)
673      if (m_selected_drive_type & TYPE_FLOPPY)
799674      {
800675         chrn_id idflop;
801         device_t *disk_img = (*w->intf->current_floppy)(device);
802         found = floppy_drive_get_next_id(disk_img, w->register_w[DESIRED_HEAD]&0x0f, &idflop);
676         found = floppy_drive_get_next_id(m_drive, m_register_w[DESIRED_HEAD]&0x0f, &idflop);
803677         copyid(idflop, id);
804678         if (/* pass==1 && */!found)
805679         {
806            w->register_r[CHIP_STATUS] |= CS_SYNCERR;
680            m_register_r[CHIP_STATUS] |= CS_SYNCERR;
807681            if (VERBOSE>1) LOG("smc92x4 error: verify (floppy): sync error\n");
808682            return ERROR;
809683         }
810684      }
811685      else
812686      {
813         (*w->intf->mfmhd_get_next_id)(device, w->register_w[DESIRED_HEAD]&0x0f, id);
814         sync_status_in(device);
815         if (!(w->register_r[DRIVE_STATUS]& DS_READY))
687         harddisk = static_cast<mfm_harddisk_device *>(m_drive);
688         harddisk->get_next_id(m_register_w[DESIRED_HEAD]&0x0f, id);
689         sync_status_in();
690         if (!(m_register_r[DRIVE_STATUS]& DS_READY))
816691         {
817692            if (VERBOSE>1) LOG("smc92x4 error: verify (harddisk): sync error\n");
818            w->register_r[CHIP_STATUS] |= CS_SYNCERR;
693            m_register_r[CHIP_STATUS] |= CS_SYNCERR;
819694            return ERROR;
820695         }
821696      }
822697
823      w->register_r[CHIP_STATUS] &= ~CS_SYNCERR;
698      m_register_r[CHIP_STATUS] &= ~CS_SYNCERR;
824699
825700      /* Compare with the desired sector ID. */
826      if ((w->selected_drive_type & DRIVE_TYPE) == TYPE_AT)
701      if ((m_selected_drive_type & DRIVE_TYPE) == TYPE_AT)
827702      {
828703         /* Note: the spec says CURRENT_CYLINDER, but that seems wrong. */
829         des_cylinder = ((w->register_r[DATA] & 0x03)<<8) | w->register_w[DESIRED_CYLINDER];
704         des_cylinder = ((m_register_r[DATA] & 0x03)<<8) | m_register_w[DESIRED_CYLINDER];
830705      }
831706      else
832707      {
833         des_cylinder = ((w->register_w[DESIRED_HEAD] & 0x70)<<4) | w->register_w[DESIRED_CYLINDER];
708         des_cylinder = ((m_register_w[DESIRED_HEAD] & 0x70)<<4) | m_register_w[DESIRED_CYLINDER];
834709      }
835      if (VERBOSE>6) LOG("smc92x4 check id: current = (%d,%d,%d), required = (%d,%d,%d)\n", id->C, id->H, id->R, des_cylinder, w->register_w[DESIRED_HEAD] & 0x0f, w->register_w[DESIRED_SECTOR]);
710      if (VERBOSE>6) LOG("smc92x4 check id: current = (%d,%d,%d), required = (%d,%d,%d)\n", id->C, id->H, id->R, des_cylinder, m_register_w[DESIRED_HEAD] & 0x0f, m_register_w[DESIRED_SECTOR]);
836711      if ((des_cylinder == id->C)
837         && ((w->register_w[DESIRED_HEAD] & 0x0f) == id->H))
712         && ((m_register_w[DESIRED_HEAD] & 0x0f) == id->H))
838713      {
839         if (!check_sector ||  (w->register_w[DESIRED_SECTOR] == id->R))
840            foundsect = TRUE;
714         if (!check_sector ||  (m_register_w[DESIRED_SECTOR] == id->R))
715            foundsect = true;
841716      }
842717   }
843718   if (!foundsect)
844719   {
845      w->register_r[CHIP_STATUS] |= CS_COMPERR;
846      if (VERBOSE>1) LOG("smc92x4 error: verify: sector not found, seek error (desired cyl=%d/sec=%d, current cyl=%d/sec=%d)\n", des_cylinder, w->register_w[DESIRED_SECTOR], id->C, id->R);
720      m_register_r[CHIP_STATUS] |= CS_COMPERR;
721      if (VERBOSE>1) LOG("smc92x4 error: verify: sector not found, seek error (desired cyl=%d/sec=%d, current cyl=%d/sec=%d)\n", des_cylinder, m_register_w[DESIRED_SECTOR], id->C, id->R);
847722      return ERROR;
848723   }
849724
850   w->register_r[INT_STATUS] &= ~ST_TC_SEEKERR;
725   m_register_r[INT_STATUS] &= ~ST_TC_SEEKERR;
851726   return DONE;
852727}
853728
854729/*
855730    Common procedure: data_transfer(read) (as described in the specification)
856731*/
857static void data_transfer_read(device_t *device, chrn_id_hd id, int transfer_enable)
732void smc92x4_device::data_transfer_read(chrn_id_hd id, int transfer_enable)
858733{
859734   int i, retry, sector_len;
860   smc92x4_state *w = get_safe_token(device);
861   device_t *current_floppy = NULL;
735   mfm_harddisk_device *harddisk;
862736
863737   int sector_data_id;
864738   UINT8 *buf;
865739
866   sync_latches_out(device);
867   sync_status_in(device);
740   sync_latches_out();
741   sync_status_in();
868742
869   current_floppy = (*w->intf->current_floppy)(device);
870
871743   // Set command termination code. The error code is set first, and
872744   // on success, it is cleared.
873   w->register_r[INT_STATUS] |= ST_TC_DATAERR;
745   m_register_r[INT_STATUS] |= ST_TC_DATAERR;
874746
875747   // Save the value. Note that the retry count is only in the first four
876748   // bits, and it is stored in one's complement. In this implementation
877749   // we don't work with it.
878   retry = w->register_w[RETRY_COUNT];
750   retry = m_register_w[RETRY_COUNT];
879751
880752   /* We are already at the correct sector (found by the verify sequence) */
881753
882754   /* Find the data sync mark. Easy, we assume it has been found. */
883755
884756   if (id.flags & ID_FLAG_DELETED_DATA)
885      w->register_r[CHIP_STATUS] |= CS_DELDATA;
757      m_register_r[CHIP_STATUS] |= CS_DELDATA;
886758   else
887      w->register_r[CHIP_STATUS] &= ~CS_DELDATA;
759      m_register_r[CHIP_STATUS] &= ~CS_DELDATA;
888760
889761   /* Found. Now update the current cylinder/head registers. */
890   update_id_regs(device, id);
762   update_id_regs(id);
891763
892764   /* Initiate the DMA. We assume the DMARQ is positive. */
893   w->register_r[INT_STATUS] &= ~ST_OVRUN;
765   m_register_r[INT_STATUS] &= ~ST_OVRUN;
894766
895767   sector_len = 1 << (id.N+7);
896768   sector_data_id = id.data_id;
769   buf = (UINT8 *)malloc(sector_len);
897770
898   if (w->selected_drive_type & TYPE_FLOPPY)
771   if (m_selected_drive_type & TYPE_FLOPPY)
899772   {
900      buf = (UINT8 *)malloc(sector_len);
901      floppy_drive_read_sector_data(current_floppy, id.H, sector_data_id, buf, sector_len);
773      floppy_drive_read_sector_data(m_drive, id.H, sector_data_id, buf, sector_len);
902774   }
903775   else
904776   {
905      /* buf is allocated within the function, and sector_len is also set */
906      (*w->intf->mfmhd_read_sector)(device, id.C, id.H, id.R, &buf, &sector_len);
777      // TODO: Should we get the sector length from the harddisk?
778      harddisk = static_cast<mfm_harddisk_device *>(m_drive);
779      harddisk->read_sector(id.C, id.H, id.R, buf);
907780   }
908   sync_status_in(device);
781   sync_status_in();
909782
910783   if (transfer_enable)
911784   {
912785      /* Copy via DMA into controller RAM. */
913      set_dma_address(device, DMA23_16, DMA15_8, DMA7_0);
786      set_dma_address(DMA23_16, DMA15_8, DMA7_0);
914787
915      smc92x4_set_dip(device, TRUE);
788      m_out_dip(ASSERT_LINE);
916789      for (i=0; i < sector_len; i++)
917790      {
918         (*w->intf->dma_write_callback)(device, buf[i]);
791         m_out_dma(0, buf[i]);
919792      }
920      smc92x4_set_dip(device, FALSE);
793      m_out_dip(CLEAR_LINE);
921794   }
922795   free(buf);
923796
924797   /* Check CRC. We assume everything is OK, no retry required. */
925   w->register_r[CHIP_STATUS] &= ~CS_RETREQ;
926   w->register_r[CHIP_STATUS] &= ~CS_CRCERR;
927   w->register_r[CHIP_STATUS] &= ~CS_ECCATT;
798   m_register_r[CHIP_STATUS] &= ~CS_RETREQ;
799   m_register_r[CHIP_STATUS] &= ~CS_CRCERR;
800   m_register_r[CHIP_STATUS] &= ~CS_ECCATT;
928801
929802   /* Update the DMA registers. */
930   if (transfer_enable)
931      dma_add_offset(device, sector_len);
803   if (transfer_enable) dma_add_offset(sector_len);
932804
933805   /* Decrement sector count. */
934   w->register_w[SECTOR_COUNT] = (w->register_w[SECTOR_COUNT]-1)&0xff;
806   m_register_w[SECTOR_COUNT] = (m_register_w[SECTOR_COUNT]-1)&0xff;
935807
936808   /* Clear the error bits. */
937   w->register_r[INT_STATUS] &= ~ST_TC_DATAERR;
809   m_register_r[INT_STATUS] &= ~ST_TC_DATAERR;
938810
939   if (w->register_w[SECTOR_COUNT] == 0)
811   if (m_register_w[SECTOR_COUNT] == 0)
940812      return;
941813
942814   /* Else this is a multi-sector operation. */
943   w->register_w[DESIRED_SECTOR] =
944   w->register_r[DESIRED_SECTOR] =
945      (w->register_w[DESIRED_SECTOR]+1) & 0xff;
815   m_register_w[DESIRED_SECTOR] =
816   m_register_r[DESIRED_SECTOR] =
817      (m_register_w[DESIRED_SECTOR]+1) & 0xff;
946818
947819   /* Reinitialize retry count. */
948   w->register_w[RETRY_COUNT] = retry;
820   m_register_w[RETRY_COUNT] = retry;
949821}
950822
951823/*
952824    Common procedure: data_transfer(write) (as described in the specification)
953825*/
954static void data_transfer_write(device_t *device, chrn_id_hd id, int deldata, int redcur, int precomp, int write_long)
826void smc92x4_device::data_transfer_write(chrn_id_hd id, int deldata, int redcur, int precomp, bool write_long)
955827{
956828   int retry, i, sector_len;
957   smc92x4_state *w = get_safe_token(device);
958   device_t *current_floppy = NULL;
959829   UINT8 *buf;
960830   int sector_data_id;
831   mfm_harddisk_device *harddisk;
832   sync_latches_out();
833   sync_status_in();
961834
962   sync_latches_out(device);
963   sync_status_in(device);
964
965   current_floppy = (*w->intf->current_floppy)(device);
966
967835   // Set command termination code. The error code is set first, and
968836   // on success, it is cleared.
969   w->register_r[INT_STATUS] |= ST_TC_DATAERR;
837   m_register_r[INT_STATUS] |= ST_TC_DATAERR;
970838
971839   // Save the value. Note that the retry count is only in the first four
972840   // bits, and it is stored in one's complement. In this implementation
973841   // we don't work with it.
974   retry = w->register_w[RETRY_COUNT];
842   retry = m_register_w[RETRY_COUNT];
975843
976844   /* Initiate the DMA. We assume the DMARQ is positive. */
977   w->register_r[INT_STATUS] &= ~ST_OVRUN;
845   m_register_r[INT_STATUS] &= ~ST_OVRUN;
978846
979847   sector_len = 1 << (id.N+7);
980848   sector_data_id = id.data_id;
r14513r14514
982850   buf = (UINT8 *)malloc(sector_len);
983851
984852   /* Copy via DMA from controller RAM. */
985   set_dma_address(device, DMA23_16, DMA15_8, DMA7_0);
853   set_dma_address(DMA23_16, DMA15_8, DMA7_0);
986854
987   smc92x4_set_dip(device, TRUE);
855   m_out_dip(ASSERT_LINE);
988856   for (i=0; i<sector_len; i++)
989857   {
990      buf[i] = (*w->intf->dma_read_callback)(device);
858      buf[i] = m_in_dma(0);
991859   }
992   smc92x4_set_dip(device, FALSE);
860   m_out_dip(CLEAR_LINE);
993861
994862   if (write_long)
995863   {
996864      if (VERBOSE>1) LOG("smc92x4 warn: write sector: Write_long not supported. Performing a normal write.\n");
997865   }
998866
999   if (w->selected_drive_type & TYPE_FLOPPY)
867   if (m_selected_drive_type & TYPE_FLOPPY)
1000868   {
1001      floppy_drive_write_sector_data(current_floppy, id.H, sector_data_id, (char *) buf, sector_len, FALSE);
869      floppy_drive_write_sector_data(m_drive, id.H, sector_data_id, (char *) buf, sector_len, false);
1002870   }
1003871   else
1004872   {
1005      (*w->intf->mfmhd_write_sector)(device, id.C, id.H, id.R, buf, sector_len);
873      harddisk = static_cast<mfm_harddisk_device *>(m_drive);
874      harddisk->write_sector(id.C, id.H, id.R, buf);
1006875   }
1007876   free(buf);
1008   sync_status_in(device);
877   sync_status_in();
1009878
1010   w->register_r[CHIP_STATUS] &= ~CS_RETREQ;
1011   w->register_r[CHIP_STATUS] &= ~CS_CRCERR;
1012   w->register_r[CHIP_STATUS] &= ~CS_ECCATT;
879   m_register_r[CHIP_STATUS] &= ~CS_RETREQ;
880   m_register_r[CHIP_STATUS] &= ~CS_CRCERR;
881   m_register_r[CHIP_STATUS] &= ~CS_ECCATT;
1013882
1014883   /* Update the DMA registers. */
1015   dma_add_offset(device, sector_len);
884   dma_add_offset(sector_len);
1016885
1017886   /* Decrement sector count. */
1018   w->register_w[SECTOR_COUNT] = (w->register_w[SECTOR_COUNT]-1)&0xff;
887   m_register_w[SECTOR_COUNT] = (m_register_w[SECTOR_COUNT]-1)&0xff;
1019888
1020889   /* Clear the error bits. */
1021   w->register_r[INT_STATUS] &= ~ST_TC_DATAERR;
890   m_register_r[INT_STATUS] &= ~ST_TC_DATAERR;
1022891
1023   if (w->register_w[SECTOR_COUNT] == 0)
1024      return;
892   if (m_register_w[SECTOR_COUNT] == 0) return;
1025893
1026894   /* Else this is a multi-sector operation. */
1027   w->register_w[DESIRED_SECTOR] =
1028   w->register_r[DESIRED_SECTOR] =
1029      (w->register_w[DESIRED_SECTOR]+1) & 0xff;
895   m_register_w[DESIRED_SECTOR] =   m_register_r[DESIRED_SECTOR] = (m_register_w[DESIRED_SECTOR]+1) & 0xff;
1030896
1031897   /* Reinitialize retry count. */
1032   w->register_w[RETRY_COUNT] = retry;
898   m_register_w[RETRY_COUNT] = retry;
1033899}
1034900
1035static void read_sectors_continue(device_t *device);
1036static void write_sectors_continue(device_t *device);
1037
1038901/*
1039902    Read sectors physical / logical. Physical means that the first, the
1040903    second, the third sector appearing under the head will be read. These
1041904    sectors are usually not in logical sequence. The ordering depends on
1042905    the interleave pattern.
1043906*/
1044static void read_write_sectors(device_t *device)
907void smc92x4_device::read_write_sectors()
1045908{
1046   smc92x4_state *w = get_safe_token(device);
909   int logical = m_command & 0x04;
910   int implied_seek_disabled = m_command & 0x02; /* for read */
911   int write = (m_command & 0x80);
1047912
1048   int logical = w->command & 0x04;
1049   int implied_seek_disabled = w->command & 0x02; /* for read */
1050   int write = (w->command & 0x80);
913   m_after_seek = false;
1051914
1052   w->after_seek = FALSE;
915   m_to_be_continued = false;
1053916
1054   w->to_be_continued = FALSE;
1055
1056917   if (write) /* write sectors */
1057918   {
1058      logical = w->command & 0x20;
1059      implied_seek_disabled = w->command & 0x40;
919      logical = m_command & 0x20;
920      implied_seek_disabled = m_command & 0x40;
1060921   }
1061922
1062923   if (!logical)
1063      implied_seek_disabled = FALSE;
924      implied_seek_disabled = false;
1064925
1065   if (!w->found_id)
926   if (!m_found_id)
1066927   {
1067928      /* First start. */
1068      read_id_field(device, &w->seek_count, &w->step_direction, &w->recent_id);
929      read_id_field(&m_seek_count, &m_step_direction, &m_recent_id);
1069930   }
1070931
1071   if ((w->register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
932   if ((m_register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
1072933   {
1073      w->found_id = TRUE;
934      m_found_id = true;
1074935      /* Perform the seek for the cylinder. */
1075      if (!implied_seek_disabled && w->seek_count > 0)
936      if (!implied_seek_disabled && m_seek_count > 0)
1076937      {
1077         smc92x4_timed_seek_request(device);
938         timed_seek_request();
1078939      }
1079940      else
1080941      {
1081         if (write)
1082            write_sectors_continue(device);
1083         else
1084            read_sectors_continue(device);
942         if (write) write_sectors_continue();
943         else read_sectors_continue();
1085944      }
1086
1087945   }
1088946   else
1089947   {
1090      set_command_done(device, ST_TC_RDIDERR);
948      set_command_done(ST_TC_RDIDERR);
1091949   }
1092950}
1093951
1094952
1095static void read_sectors_continue(device_t *device)
953void smc92x4_device::read_sectors_continue()
1096954{
1097   smc92x4_state *w = get_safe_token(device);
1098
1099   int check_sector = TRUE; /* always check the first sector */
955   bool check_sector = true; /* always check the first sector */
1100956   int state = AGAIN;
1101   int logical = w->command & 0x04;
957   int logical = m_command & 0x04;
1102958
1103959   /* Needed for the two ways of re-entry: during the seek process, and during sector read */
1104   if (!w->after_seek)
960   if (!m_after_seek)
1105961   {
1106962      if (VERBOSE>7) LOG("smc92x4 continue with sector read\n");
1107      w->seek_count--;
1108      if (w->seek_count > 0)
963      m_seek_count--;
964      if (m_seek_count > 0)
1109965      {
1110         read_write_sectors(device);
966         read_write_sectors();
1111967         return;
1112968      }
1113      w->after_seek = TRUE;
969      m_after_seek = true;
1114970   }
1115971   else
1116972   {
1117973      /* we are here after the sector read. */
1118      w->to_be_continued = FALSE;
1119      if (w->register_r[INT_STATUS] & ST_TERMCOD)
974      m_to_be_continued = false;
975      if (m_register_r[INT_STATUS] & ST_TERMCOD)
1120976      {
1121         if (VERBOSE>1) LOG("smc92x4 error: data error during sector read: INTSTATUS=%02x\n", w->register_r[INT_STATUS]);
1122         set_command_done(device, ST_TC_DATAERR);
977         if (VERBOSE>1) LOG("smc92x4 error: data error during sector read: INTSTATUS=%02x\n", m_register_r[INT_STATUS]);
978         set_command_done(ST_TC_DATAERR);
1123979         return;
1124980      }
1125981   }
1126982
1127   w->to_be_continued = FALSE;
983   m_to_be_continued = false;
1128984
1129985   /* Wait for SEEK_COMPLETE. We assume the signal has appeared. */
1130986
1131   if (w->register_w[SECTOR_COUNT] > 0 /* && !(w->register_r[DRIVE_STATUS] & DS_INDEX) */)
987   if (m_register_w[SECTOR_COUNT] > 0 /* && !(m_register_r[DRIVE_STATUS] & DS_INDEX) */)
1132988   {
1133989      /* Call the verify sequence. */
1134      state = verify(device, &w->recent_id, check_sector);
990      state = verify(&m_recent_id, check_sector);
1135991      if (state==ERROR)
1136992      {
1137993         // TODO: set command done?
r14513r14514
1141997
1142998      /* For read physical, only verify the first sector. */
1143999      if (!logical)
1144         check_sector = FALSE;
1000         check_sector = false;
11451001
1146      if (w->recent_id.flags & BAD_SECTOR)
1002      if (m_recent_id.flags & BAD_SECTOR)
11471003      {
11481004         if (VERBOSE>0) LOG("smc92x4 error: Bad sector, seek error\n");
1149         set_command_done(device, ST_TC_SEEKERR);
1005         set_command_done(ST_TC_SEEKERR);
11501006      }
11511007      else
11521008      {
1153         smc92x4_timed_sector_read_request(device);
1009         timed_sector_read_request();
11541010      }
11551011   }
11561012   else
11571013   {
1158      set_command_done(device, ST_TC_SUCCESS);
1014      set_command_done(ST_TC_SUCCESS);
11591015      if (VERBOSE>7) LOG("smc92x4 read sector command done\n");
11601016   }
11611017}
11621018
1163static void write_sectors_continue(device_t *device)
1019void smc92x4_device::write_sectors_continue()
11641020{
1165   smc92x4_state *w = get_safe_token(device);
1166
1167   int check_sector = TRUE; /* always check the first sector */
1021   bool check_sector = true; /* always check the first sector */
11681022   int state = AGAIN;
1169   int logical = w->command & 0x20;
1023   int logical = m_command & 0x20;
11701024
11711025   /* Needed for the two ways of re-entry: during the seek process, and during sector write */
1172   if (!w->after_seek)
1026   if (!m_after_seek)
11731027   {
1174      w->seek_count--;
1175      if (w->seek_count > 0)
1028      m_seek_count--;
1029      if (m_seek_count > 0)
11761030      {
1177         read_write_sectors(device);
1031         read_write_sectors();
11781032         return;
11791033      }
1180      w->after_seek = TRUE;
1034      m_after_seek = true;
11811035   }
11821036   else
11831037   {
11841038      /* we are here after the sector write. */
1185      w->to_be_continued = FALSE;
1186      if (w->register_r[INT_STATUS] & ST_TERMCOD)
1039      m_to_be_continued = false;
1040      if (m_register_r[INT_STATUS] & ST_TERMCOD)
11871041      {
11881042         if (VERBOSE>0) LOG("smc92x4 error: data error during sector write\n");
1189         set_command_done(device, ST_TC_DATAERR);
1043         set_command_done(ST_TC_DATAERR);
11901044         return;
11911045      }
11921046   }
11931047
1194   w->to_be_continued = FALSE;
1048   m_to_be_continued = false;
11951049
1196   if ((w->register_w[RETRY_COUNT] & 0xf0)!= 0xf0)
1050   if ((m_register_w[RETRY_COUNT] & 0xf0)!= 0xf0)
11971051      if (VERBOSE>1) LOG("smc92x4 warn: RETRY_COUNT in write sector should be set to 0. Ignored.\n");
11981052
11991053   /* Wait for SEEK_COMPLETE. We assume the signal has appeared. */
1200   if (w->register_w[SECTOR_COUNT] > 0)
1054   if (m_register_w[SECTOR_COUNT] > 0)
12011055   {
12021056      /* Call the verify sequence. */
1203      state = verify(device, &w->recent_id, check_sector);
1057      state = verify(&m_recent_id, check_sector);
12041058      if (state==ERROR)
12051059      {
12061060         if (VERBOSE>0) LOG("smc92x4 error: verify error during sector write\n");
r14513r14514
12091063
12101064      /* For write physical, only verify the first sector. */
12111065      if (!logical)
1212         check_sector = FALSE;
1066         check_sector = false;
12131067
12141068/*      printf("smc92x4 write sectors CYL=%02x, HEA=%02x, SEC=%02x, MOD=%02x, CNT=%02x, TRY=%02x\n",
1215            w->register_w[DESIRED_CYLINDER],
1216            w->register_w[DESIRED_HEAD],
1217            w->register_w[DESIRED_SECTOR],
1218            w->register_w[MODE],
1219            w->register_w[SECTOR_COUNT],
1220            w->register_w[RETRY_COUNT]); */
1221      smc92x4_timed_sector_write_request(device);
1069            m_register_w[DESIRED_CYLINDER],
1070            m_register_w[DESIRED_HEAD],
1071            m_register_w[DESIRED_SECTOR],
1072            m_register_w[MODE],
1073            m_register_w[SECTOR_COUNT],
1074            m_register_w[RETRY_COUNT]); */
1075      timed_sector_write_request();
12221076   }
12231077   else
12241078   {
1225      set_command_done(device, ST_TC_SUCCESS);
1079      set_command_done(ST_TC_SUCCESS);
12261080      if (VERBOSE>7) LOG("smc92x4 write sector command done\n");
12271081   }
12281082}
r14513r14514
12341088/*
12351089    Handle the restore command
12361090*/
1237static void restore_drive(device_t *device)
1091void smc92x4_device::restore_drive()
12381092{
1239   smc92x4_state *w = get_safe_token(device);
1240
12411093   /* TODO: int_after_seek_complete required for buffered seeks */
1242   sync_status_in(device);
1094   sync_status_in();
12431095
1244   if (w->seek_count>=4096 || !(w->register_r[DRIVE_STATUS] & DS_READY))
1096   if (m_seek_count>=4096 || !(m_register_r[DRIVE_STATUS] & DS_READY))
12451097   {
12461098      if (VERBOSE>0) LOG("smc92x4 error: seek error in restore\n");
1247      w->register_r[INT_STATUS] |= ST_TC_SEEKERR;
1099      m_register_r[INT_STATUS] |= ST_TC_SEEKERR;
12481100      return;
12491101   }
12501102
1251   if (w->register_r[DRIVE_STATUS] & DS_TRK00)
1103   if (m_register_r[DRIVE_STATUS] & DS_TRK00)
12521104   {
1253      w->register_r[INT_STATUS] |= ST_TC_SUCCESS;
1105      m_register_r[INT_STATUS] |= ST_TC_SUCCESS;
12541106      /* Issue interrupt */
1255      smc92x4_set_interrupt(device);
1107      set_interrupt();
12561108      return;
12571109   }
12581110
1259   w->step_direction = -1;
1260   smc92x4_timed_seek_request(device);
1111   m_step_direction = -1;
1112   timed_seek_request();
12611113}
12621114
1263static void restore_continue(device_t *device)
1115void smc92x4_device::restore_continue()
12641116{
1265   smc92x4_state *w = get_safe_token(device);
1117   m_seek_count++;
12661118
1267   w->seek_count++;
1268
12691119   /* Next iteration */
1270   restore_drive(device);
1120   restore_drive();
12711121
1272   w->to_be_continued = FALSE;
1122   m_to_be_continued = false;
12731123}
12741124
12751125/*
12761126    Handle the step command. Note that the CURRENT_CYLINDER register is not
12771127    updated (this would break the format procedure).
12781128*/
1279static void step_in_out(device_t *device)
1129void smc92x4_device::step_in_out()
12801130{
1281   smc92x4_state *w = get_safe_token(device);
1282   int direction = (w->command & 0x02)? -1 : +1;
1131   int direction = (m_command & 0x02)? -1 : +1;
12831132   // bit 0: 0 -> command ends after last seek pulse, 1 -> command
12841133   // ends when the drive asserts the seek complete pin
1285   int buffered = w->command & 0x01;
1134   int buffered = m_command & 0x01;
12861135
1287   w->step_direction = direction;
1288   w->buffered = buffered;
1136   m_step_direction = direction;
1137   m_buffered = (buffered!=0);
12891138
1290   smc92x4_timed_seek_request(device);
1139   timed_seek_request();
12911140   if (VERBOSE>6) LOG("smc92x4 waiting for drive step\n");
12921141}
12931142
1294static void step_continue(device_t *device)
1143void smc92x4_device::step_continue()
12951144{
1296   smc92x4_state *w = get_safe_token(device);
12971145   if (VERBOSE>7) LOG("smc92x4 step continue\n");
1298   w->to_be_continued = FALSE;
1299   set_command_done(device,  ST_TC_SUCCESS);
1146   m_to_be_continued = false;
1147   set_command_done(ST_TC_SUCCESS);
13001148}
13011149
1302
1303static void drive_select(device_t *device, int drive);
1304
13051150/*
13061151    Poll drives
13071152    This command is used to find out which drive has complete a buffered
13081153    seek (RESTORE, SEEK IN/OUT with BUFFERED set to one)
13091154*/
1310static void poll_drives(device_t *device)
1155void smc92x4_device::poll_drives()
13111156{
13121157   int mask = 0x08;
1313   smc92x4_state *w = get_safe_token(device);
13141158   int i;
1315   int flags = w->command & 0x0f;
1159   int flags = m_command & 0x0f;
13161160
13171161/* Spec is unclear: Do we continue to poll the drives after we have checked each
13181162one for the first time? We are not interested in locking up the emulator, so
r14513r14514
13221166      if (flags & mask)
13231167      {
13241168         /* Poll drive */
1325         drive_select(device, i|((w->types[i])<<2));
1326         sync_latches_out(device);
1327         sync_status_in(device);
1328         if (w->register_r[DRIVE_STATUS] & DS_SKCOM) return;
1169         drive_select(i|((m_types[i])<<2));
1170         if (m_register_r[DRIVE_STATUS] & DS_SKCOM) return;
13291171      }
13301172      mask = mask>>1;
13311173   }
13321174}
13331175
1334static void drive_select(device_t *device, int driveparm)
1176void smc92x4_device::drive_select(int driveparm)
13351177{
1336   smc92x4_state *w = get_safe_token(device);
1178   m_output1 = (0x10 << (driveparm & 0x03)) | (m_register_w[RETRY_COUNT]&0x0f);
1179   m_selected_drive_type = (driveparm>>2) & 0x03;
1180   m_head_load_delay_enable = (driveparm>>4)&0x01;
13371181
1338   w->output1 = (0x10 << (driveparm & 0x03)) | (w->register_w[RETRY_COUNT]&0x0f);
1339   w->selected_drive_type = (driveparm>>2) & 0x03;
1340   w->head_load_delay_enable = (driveparm>>4)&0x01;
1341
13421182   // We need to store the type of the drive for the poll_drives command
13431183   // to be able to correctly select the device (floppy or hard disk).
1344   w->types[driveparm&0x03] = w->selected_drive_type;
1184   m_types[driveparm&0x03] = m_selected_drive_type;
13451185
13461186   // Copy the DMA registers to registers CURRENT_HEAD, CURRENT_CYLINDER,
13471187   // and CURRENT_IDENT. This is required during formatting (p. 14) as the
13481188   // format command reuses the registers for formatting parameters.
1349   w->register_r[CURRENT_HEAD] = w->register_r[DMA7_0];
1350   w->register_r[CURRENT_CYLINDER] = w->register_r[DMA15_8];
1351   w->register_r[CURRENT_IDENT] = w->register_r[DMA23_16];
1189   m_register_r[CURRENT_HEAD] = m_register_r[DMA7_0];
1190   m_register_r[CURRENT_CYLINDER] = m_register_r[DMA15_8];
1191   m_register_r[CURRENT_IDENT] = m_register_r[DMA23_16];
13521192
1353   sync_latches_out(device);
1354   sync_status_in(device);
1193   sync_latches_out();
1194   sync_status_in();
13551195}
13561196
13571197/*
13581198    Command SEEK/READID
13591199*/
1360static void seek_read_id(device_t *device)
1200void smc92x4_device::seek_read_id()
13611201{
1362   smc92x4_state *w = get_safe_token(device);
1363   int step_enable = (w->command & 0x04);
1364   int verify_id = (w->command & 0x01);
1365   int wait = (w->command & 0x02);
1202   int step_enable = (m_command & 0x04);
1203   int verify_id = (m_command & 0x01);
1204   int wait = (m_command & 0x02);
13661205
1367   w->to_be_continued = FALSE;
1206   m_to_be_continued = false;
13681207
1369   if (!w->found_id)
1208   if (!m_found_id)
13701209   {
13711210      /* First start. */
1372      read_id_field(device, &w->seek_count, &w->step_direction, &w->recent_id);
1373      w->found_id = TRUE;
1211      read_id_field(&m_seek_count, &m_step_direction, &m_recent_id);
1212      m_found_id = true;
13741213   }
13751214
1376   if ((w->register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
1215   if ((m_register_r[INT_STATUS] & ST_TC_DATAERR) == ST_TC_SUCCESS)
13771216   {
13781217      /* Perform the seek for the cylinder. */
1379      if (step_enable && w->seek_count > 0)
1218      if (step_enable && m_seek_count > 0)
13801219      {
1381         smc92x4_timed_seek_request(device);
1220         timed_seek_request();
13821221      }
13831222      else
13841223      {
r14513r14514
13871226
13881227         if (verify_id)
13891228         {
1390            verify(device, &w->recent_id, TRUE);
1229            verify(&m_recent_id, true);
13911230         }
13921231      }
13931232   }
13941233   else
13951234   {
1396      set_command_done(device, ST_TC_RDIDERR);
1235      set_command_done(ST_TC_RDIDERR);
13971236   }
13981237}
13991238
1400static void seek_read_id_continue(device_t *device)
1239void smc92x4_device::seek_read_id_continue()
14011240{
1402   smc92x4_state *w = get_safe_token(device);
1403   w->seek_count--;
1404   seek_read_id(device);
1405   w->to_be_continued = FALSE;
1241   m_seek_count--;
1242   seek_read_id();
1243   m_to_be_continued = false;
14061244}
14071245
14081246/*
r14513r14514
14801318    The formats are determined by setting the flag in the smc92x4
14811319    interface structure.
14821320*/
1483static void format_floppy_track(device_t *device, int flags)
1321void smc92x4_device::format_floppy_track(int flags)
14841322{
1485   smc92x4_state *w = get_safe_token(device);
1486   device_t *current_floppy = NULL;
1487
14881323   floppy_image_legacy *floppy;
14891324   int i,index,j, exp_size;
14901325   int gap0, gap1, gap2, gap3, gap4, sync1, sync2, count, size, fm;
r14513r14514
14981333   /* Determine the track size. We cannot allow different sizes in this design. */
14991334   int data_count = 0;
15001335
1501   sync_status_in(device);
1336   sync_status_in();
15021337
1503   current_floppy = (*w->intf->current_floppy)(device);
1504   floppy = flopimg_get_image(current_floppy);
1338   floppy = flopimg_get_image(m_drive);
15051339
15061340   if (floppy != NULL)
15071341      data_count = floppy_get_track_size(floppy, 0, 0);
15081342
15091343   if (data_count==0)
15101344   {
1511      if (controller_set_to_single_density(device))
1345      if (in_single_density_mode())
15121346         data_count = TRKSIZE_SD;
15131347      else
15141348         data_count = TRKSIZE_DD;
r14513r14514
15171351   /* Build buffer */
15181352   buffer = (UINT8*)malloc(data_count);
15191353
1520   fm = controller_set_to_single_density(device);
1354   fm = in_single_density_mode();
15211355
1522   sync2 = (~w->register_w[DESIRED_CYLINDER])&0xff;
1523   gap2 = (-w->register_w[DMA23_16])&0xff;
1524   count = (~w->register_w[SECTOR_COUNT])&0xff;
1525   size = (~w->register_w[RETRY_COUNT])&0xff;
1356   sync2 = (~m_register_w[DESIRED_CYLINDER])&0xff;
1357   gap2 = (-m_register_w[DMA23_16])&0xff;
1358   count = (~m_register_w[SECTOR_COUNT])&0xff;
1359   size = (~m_register_w[RETRY_COUNT])&0xff;
15261360   gap_byte = (fm)? 0xff : 0x4e;
15271361
1528   if (w->intf->full_track_layout)
1362   if (m_full_track_layout)
15291363   {
15301364      /* Including the index AM. */
1531      gap0 = (-w->register_w[DMA7_0])&0xff;
1532      gap1 = (-w->register_w[DMA15_8])&0xff;
1533      gap3 = (-w->register_w[DESIRED_SECTOR])&0xff;
1365      gap0 = (-m_register_w[DMA7_0])&0xff;
1366      gap1 = (-m_register_w[DMA15_8])&0xff;
1367      gap3 = (-m_register_w[DESIRED_SECTOR])&0xff;
15341368      gap4 = (fm)? 247 : 598;
15351369      pre_gap = gap_byte;
15361370      sync1 = sync2;
r14513r14514
15581392      if (VERBOSE>0) LOG("smc92x4 warn: The current track length in the image (%d) does not match the new track length (%d). Keeping the old length. This will break the image (sorry).\n", data_count, exp_size);
15591393
15601394   /* use the backup registers set up during drive_select */
1561   set_dma_address(device, CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);
1395   set_dma_address(CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);
15621396
15631397   memset(&buffer[index], pre_gap, gap0);
15641398   index += gap0;
15651399
1566   if (w->intf->full_track_layout)
1400   if (m_full_track_layout)
15671401   {
15681402      /* Create the Index AM */
15691403      memset(&buffer[index], 0x00, sync1);
r14513r14514
15911425
15921426      buffer[index++] = 0xfe;
15931427
1594      smc92x4_set_dip(device, TRUE);
1595//      if (!fm) curr_ident = (*w->intf->dma_read_callback)(device);
1596      if (!fm) (*w->intf->dma_read_callback)(device);
1597      curr_cyl = (*w->intf->dma_read_callback)(device);
1598      curr_head = (*w->intf->dma_read_callback)(device);
1599      curr_sect = (*w->intf->dma_read_callback)(device);
1600      curr_size = (*w->intf->dma_read_callback)(device);
1601      smc92x4_set_dip(device, FALSE);
1428      m_out_dip(ASSERT_LINE);
1429//      if (!fm) curr_ident = m_in_dma();
1430      if (!fm) m_in_dma(0);
1431      curr_cyl = m_in_dma(0);
1432      curr_head = m_in_dma(0);
1433      curr_sect = m_in_dma(0);
1434      curr_size = m_in_dma(0);
1435      m_out_dip(CLEAR_LINE);
16021436
16031437      buffer[index++] = curr_cyl;
16041438      buffer[index++] = curr_head;
r14513r14514
16061440      buffer[index++] = curr_size;
16071441
16081442      if (j==0)
1609         if (VERBOSE>6) LOG("current_floppy=%s, format track %d, head %d\n",  current_floppy->tag(), curr_cyl, curr_head);
1443         if (VERBOSE>6) LOG("current_floppy=%s, format track %d, head %d\n",  m_drive->tag(), curr_cyl, curr_head);
16101444
16111445      /* Calculate CRC16 (5 bytes for ID) */
16121446      crc = ccitt_crc16(0xffff, &buffer[index-5], 5);
r14513r14514
16431477   memset(&buffer[index], gap_byte, gap4);
16441478   index += gap4;
16451479
1646   floppy_drive_write_track_data_info_buffer(current_floppy, w->register_w[DESIRED_HEAD]&0x0f, (char *)buffer, &data_count);
1480   floppy_drive_write_track_data_info_buffer(m_drive, m_register_w[DESIRED_HEAD]&0x0f, (char *)buffer, &data_count);
16471481   free(buffer);
1648   sync_status_in(device);
1482   sync_status_in();
16491483}
16501484
16511485/*
r14513r14514
16551489
16561490    For more explanations see the comments to format_floppy_track.
16571491*/
1658static void format_harddisk_track(device_t *device, int flags)
1492void smc92x4_device::format_harddisk_track(int flags)
16591493{
1660   smc92x4_state *w = get_safe_token(device);
16611494   int i,index,j;
16621495   int gap1, gap2, gap3, gap4, sync, count, size, gap_byte, crc;
16631496   UINT8 curr_ident, curr_cyl, curr_head, curr_sect, curr_size;
r14513r14514
16651498   int normal_data_mark = flags & 0x10;
16661499   int data_count=0;
16671500
1501   mfm_harddisk_device *harddisk = static_cast<mfm_harddisk_device *>(m_drive);
1502
16681503   UINT8 *buffer;
16691504
1670   sync_status_in(device);
1505   sync_status_in();
16711506
16721507   /* Build buffer */
1673//  gap0 = (-w->register_w[DMA7_0])&0xff;
1674   gap1 = (-w->register_w[DMA15_8])&0xff;
1675   gap2 = (-w->register_w[DMA23_16])&0xff;
1676   gap3 = (-w->register_w[DESIRED_SECTOR])&0xff;
1508//  gap0 = (-m_register_w[DMA7_0])&0xff;
1509   gap1 = (-m_register_w[DMA15_8])&0xff;
1510   gap2 = (-m_register_w[DMA23_16])&0xff;
1511   gap3 = (-m_register_w[DESIRED_SECTOR])&0xff;
16771512   gap4 = 340;
16781513
1679   sync = (~w->register_w[DESIRED_CYLINDER])&0xff;
1680   count = (~w->register_w[SECTOR_COUNT])&0xff;
1681   size = (~w->register_w[RETRY_COUNT])&0xff;
1514   sync = (~m_register_w[DESIRED_CYLINDER])&0xff;
1515   count = (~m_register_w[SECTOR_COUNT])&0xff;
1516   size = (~m_register_w[RETRY_COUNT])&0xff;
16821517
16831518   data_count = gap1 + count*(sync+12+gap2+sync+size*128+gap3)+gap4;
16841519
16851520   buffer = (UINT8*)malloc(data_count);
16861521
16871522   index = 0;
1688//  fm = controller_set_to_single_density(device);
1689   controller_set_to_single_density(device);
16901523   gap_byte = 0x4e;
16911524
16921525   /* use the backup registers set up during drive_select */
1693   set_dma_address(device, CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);
1526   set_dma_address(CURRENT_IDENT, CURRENT_CYLINDER, CURRENT_HEAD);
16941527
16951528   for (i=0; i < gap1; i++) buffer[index++] = gap_byte;
16961529
r14513r14514
17001533      for (i=0; i < sync; i++) buffer[index++] = 0x00;
17011534      buffer[index++] = 0xa1;
17021535
1703      smc92x4_set_dip(device, TRUE);
1704      curr_ident = (*w->intf->dma_read_callback)(device);
1705      curr_cyl = (*w->intf->dma_read_callback)(device);
1706      curr_head = (*w->intf->dma_read_callback)(device);
1707      curr_sect = (*w->intf->dma_read_callback)(device);
1708      curr_size = (*w->intf->dma_read_callback)(device);
1709      smc92x4_set_dip(device, FALSE);
1536      m_out_dip(ASSERT_LINE);
1537      curr_ident = m_in_dma(0);
1538      curr_cyl = m_in_dma(0);
1539      curr_head = m_in_dma(0);
1540      curr_sect = m_in_dma(0);
1541      curr_size = m_in_dma(0);
1542      m_out_dip(CLEAR_LINE);
17101543
17111544      buffer[index++] = curr_ident;
17121545      buffer[index++] = curr_cyl;
r14513r14514
17431576   /* GAP 4 */
17441577   for (i=0; i < gap4; i++) buffer[index++] = gap_byte;
17451578
1746   (*w->intf->mfmhd_write_track)(device, w->register_w[DESIRED_HEAD]&0x0f, buffer, data_count);
1579   // Now write the whole track
1580   harddisk->write_track(m_register_w[DESIRED_HEAD]&0x0f, buffer, data_count);
1581
17471582   free(buffer);
1748   sync_status_in(device);
1583   sync_status_in();
17491584}
17501585
17511586/*
r14513r14514
17661601    format or generally to the PC99 format between controller and format.
17671602    Format is the image is always PC99.)
17681603*/
1769static void read_floppy_track(device_t *device, int transfer_only_ids)
1604void smc92x4_device::read_floppy_track(bool transfer_only_ids)
17701605{
1771   smc92x4_state *w = get_safe_token(device);
1772   device_t *current_floppy = (*w->intf->current_floppy)(device);
1773
17741606   floppy_image_legacy *floppy;
17751607   /* Determine the track size. We cannot allow different sizes in this design. */
17761608   int data_count = 0;
17771609   int i;
17781610   UINT8 *buffer;
17791611
1780   sync_latches_out(device);
1612   sync_latches_out();
17811613
1782   floppy = flopimg_get_image(current_floppy);
1614   floppy = flopimg_get_image(m_drive);
17831615
17841616   /* Determine the track size. We cannot allow different sizes in this design. */
17851617   if (floppy != NULL)
r14513r14514
17871619
17881620   if (data_count==0)
17891621   {
1790      if (controller_set_to_single_density(device))
1622      if (in_single_density_mode())
17911623         data_count = TRKSIZE_SD;
17921624      else
17931625         data_count = TRKSIZE_DD;
r14513r14514
17951627
17961628   buffer = (UINT8*)malloc(data_count);
17971629
1798   floppy_drive_read_track_data_info_buffer( current_floppy, w->register_w[DESIRED_HEAD]&0x0f, (char *)buffer, &data_count);
1799   sync_status_in(device);
1630   floppy_drive_read_track_data_info_buffer(m_drive, m_register_w[DESIRED_HEAD]&0x0f, (char *)buffer, &data_count);
1631   sync_status_in();
18001632
18011633   // Transfer the buffer to the external memory. We assume the memory
18021634   // pointer has been set appropriately in the registers.
1803   set_dma_address(device, DMA23_16, DMA15_8, DMA7_0);
1635   set_dma_address(DMA23_16, DMA15_8, DMA7_0);
18041636
18051637   if (transfer_only_ids)
18061638   {
18071639      if (VERBOSE>1) LOG("smc92x4 warn: read track: Ignoring transfer-only-ids. Reading complete track.\n");
18081640   }
18091641
1810   smc92x4_set_dip(device, TRUE);
1642   m_out_dip(ASSERT_LINE);
18111643   for (i=0; i < data_count; i++)
18121644   {
1813      (*w->intf->dma_write_callback)(device, buffer[i]);
1645      m_out_dma(0, buffer[i]);
18141646   }
1815   smc92x4_set_dip(device, FALSE);
1647   m_out_dip(CLEAR_LINE);
18161648
18171649   free(buffer);
18181650}
18191651
1820static void read_harddisk_track(device_t *device, int transfer_only_ids)
1652void smc92x4_device::read_harddisk_track(bool transfer_only_ids)
18211653{
1822   smc92x4_state *w = get_safe_token(device);
1823
18241654   /* Determine the track size. We cannot allow different sizes in this design. */
18251655   int i;
18261656   UINT8 *buffer;
18271657   int data_count=0;
1658   mfm_harddisk_device *harddisk = static_cast<mfm_harddisk_device *>(m_drive);
18281659
1829   sync_latches_out(device);
1660   sync_latches_out();
18301661
1662   data_count = harddisk->get_track_length();
1663   buffer = (UINT8*)malloc(data_count);
1664
18311665   /* buffer and data_count are allocated and set by the function. */
1832   (*w->intf->mfmhd_read_track)(device, w->register_w[DESIRED_HEAD]&0x0f, &buffer, &data_count);
1833   sync_status_in(device);
1666   harddisk->read_track(m_register_w[DESIRED_HEAD]&0x0f, buffer);
1667   sync_status_in();
18341668
1835   if (!(w->register_r[DRIVE_STATUS] & DS_READY))
1669   if (!(m_register_r[DRIVE_STATUS] & DS_READY))
18361670   {
18371671      if (VERBOSE>0) LOG("smc92x4 error: read harddisk track failed.\n");
1838      /* no buffer was allocated */
18391672   }
18401673
18411674   // Transfer the buffer to the external memory. We assume the memory
18421675   // pointer has been set appropriately in the registers.
1843   set_dma_address(device, DMA23_16, DMA15_8, DMA7_0);
1676   set_dma_address(DMA23_16, DMA15_8, DMA7_0);
18441677
18451678   if (transfer_only_ids)
18461679   {
18471680      if (VERBOSE>1) LOG("smc92x4 warn: read track: Ignoring transfer-only-ids. Reading complete track.\n");
18481681   }
18491682
1850   smc92x4_set_dip(device, TRUE);
1683   m_out_dip(ASSERT_LINE);
18511684   for (i=0; i < data_count; i++)
18521685   {
1853      (*w->intf->dma_write_callback)(device, buffer[i]);
1686      m_out_dma(0, buffer[i]);
18541687   }
1855   smc92x4_set_dip(device, FALSE);
1688   m_out_dip(CLEAR_LINE);
18561689
18571690   free(buffer);
18581691}
18591692
18601693
1861static void read_track(device_t *device)
1694void smc92x4_device::read_track()
18621695{
1863   smc92x4_state *w = get_safe_token(device);
1864   int transfer_only_ids = w->command & 0x01;
1696   int transfer_only_ids = m_command & 0x01;
18651697
1866   if (w->selected_drive_type & TYPE_FLOPPY)
1698   if (m_selected_drive_type & TYPE_FLOPPY)
18671699   {
1868      read_floppy_track(device, transfer_only_ids);
1700      read_floppy_track(transfer_only_ids);
18691701   }
18701702   else
18711703   {
1872      read_harddisk_track(device, transfer_only_ids);
1704      read_harddisk_track(transfer_only_ids);
18731705   }
1874
1875   smc92x4_timed_track_request(device);
1706   timed_track_request();
18761707}
18771708
1878static void read_track_continue(device_t *device)
1709void smc92x4_device::read_track_continue()
18791710{
1880   smc92x4_state *w = get_safe_token(device);
1881   w->to_be_continued = FALSE;
1882   set_command_done(device,  ST_TC_SUCCESS);
1711   m_to_be_continued = false;
1712   set_command_done(ST_TC_SUCCESS);
18831713}
18841714
1885static void format_track(device_t *device)
1715void smc92x4_device::format_track()
18861716{
1887   smc92x4_state *w = get_safe_token(device);
1888   int flags = w->command & 0x1f;
1717   int flags = m_command & 0x1f;
18891718
1890   if (w->selected_drive_type & TYPE_FLOPPY)
1891      format_floppy_track(device, flags);
1719   if (m_selected_drive_type & TYPE_FLOPPY)
1720      format_floppy_track(flags);
18921721   else
1893      format_harddisk_track(device, flags);
1722      format_harddisk_track(flags);
18941723
1895   smc92x4_timed_track_request(device);
1724   timed_track_request();
18961725}
18971726
1898static void format_track_continue(device_t *device)
1727void smc92x4_device::format_track_continue()
18991728{
1900   smc92x4_state *w = get_safe_token(device);
1901   w->to_be_continued = FALSE;
1902   set_command_done(device,  ST_TC_SUCCESS);
1729   m_to_be_continued = false;
1730   set_command_done(ST_TC_SUCCESS);
19031731}
19041732
19051733/*
19061734    Continue to process after callback
19071735*/
1908static void smc92x4_process_after_callback(device_t *device)
1736void smc92x4_device::process_after_callback()
19091737{
1910   smc92x4_state *w = get_safe_token(device);
1911   UINT8 opcode = w->command;
1738   UINT8 opcode = m_command;
19121739   if (opcode >= 0x02 && opcode <= 0x03)
19131740   {
1914      restore_continue(device);
1741      restore_continue();
19151742   }
19161743   else if (opcode >= 0x04 && opcode <= 0x07)
19171744   {
1918      step_continue(device);
1745      step_continue();
19191746   }
19201747   else if (opcode >= 0x50 && opcode <= 0x57)
19211748   {
1922      seek_read_id_continue(device);
1749      seek_read_id_continue();
19231750   }
19241751   else if ((opcode >= 0x58 && opcode <= 0x59)||(opcode >= 0x5C && opcode <= 0x5f))
19251752   {
1926      read_sectors_continue(device);
1753      read_sectors_continue();
19271754   }
19281755   else if (opcode >= 0x5a && opcode <= 0x5b)
19291756   {
1930      read_track_continue(device);
1757      read_track_continue();
19311758   }
19321759   else if (opcode >= 0x60 && opcode <= 0x7f)
19331760   {
1934      format_track_continue(device);
1761      format_track_continue();
19351762   }
19361763   else if (opcode >= 0x80)
19371764   {
1938      write_sectors_continue(device);
1765      write_sectors_continue();
19391766   }
19401767   else
19411768   {
r14513r14514
19461773/*
19471774    Process a command
19481775*/
1949static void smc92x4_process_command(device_t *device, UINT8 opcode)
1776void smc92x4_device::process_command(UINT8 opcode)
19501777{
1951   smc92x4_state *w = get_safe_token(device);
1952
19531778   if (VERBOSE>6) LOG("smc92x4 process command %02x\n", opcode);
1954   if (w->to_be_continued)
1779   if (m_to_be_continued)
19551780   {
1956      if (VERBOSE>1) LOG("smc92x4 warn: previous command %02x not complete\n", w->command);
1781      if (VERBOSE>1) LOG("smc92x4 warn: previous command %02x not complete\n", m_command);
19571782   }
19581783
19591784   /* Reset DONE and BAD_SECTOR. */
1960   w->register_r[INT_STATUS] &= ~(ST_DONE | ST_BADSECT);
1785   m_register_r[INT_STATUS] &= ~(ST_DONE | ST_BADSECT);
19611786
1962   /* Reset interrupt line (not explicitly mentioned in spec, but seems
1963    reasonable) */
1964   smc92x4_clear_interrupt(device);
1965   w->register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);
1787   // Reset interrupt line (not explicitly mentioned in spec, but seems reasonable
1788   clear_interrupt();
1789   m_register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);
19661790
1967   w->command = opcode;
1968   w->found_id = FALSE;
1969   w->seek_count = 0;
1791   m_command = opcode;
1792   m_found_id = false;
1793   m_seek_count = 0;
19701794
19711795   if (opcode == 0x00)
19721796   {
19731797      /* RESET */
19741798      /* same effect as the RST* pin being active */
19751799      if (VERBOSE>0) LOG("smc92x4 info: reset command\n");
1976      smc92x4_reset(device);
1800      reset();
19771801   }
19781802   else if (opcode == 0x01)
19791803   {
19801804      /* DESELECT DRIVE */
19811805      /* done when no drive is in use */
19821806      if (VERBOSE>2) LOG("smc92x4 info: drdeselect command\n");
1983      w->output1 &= ~(OUT1_DRVSEL3|OUT1_DRVSEL2|OUT1_DRVSEL1|OUT1_DRVSEL0);
1984      w->output2 |= OUT2_DRVSEL3_;
1807      m_output1 &= ~(OUT1_DRVSEL3|OUT1_DRVSEL2|OUT1_DRVSEL1|OUT1_DRVSEL0);
1808      m_output2 |= OUT2_DRVSEL3_;
19851809      /* sync the latches on the PCB */
1986      sync_latches_out(device);
1987      sync_status_in(device);
1810      sync_latches_out();
1811      sync_status_in();
19881812   }
19891813   else if (opcode >= 0x02 && opcode <= 0x03)
19901814   {
r14513r14514
19921816      // bit 0: 0 -> command ends after last seek pulse, 1 -> command
19931817      // ends when the drive asserts the seek complete pin
19941818      if (VERBOSE>2) LOG("smc92x4 info: restore command %X\n", opcode);
1995      restore_drive(device);
1819      restore_drive();
19961820   }
19971821   else if (opcode >= 0x04 && opcode <= 0x07)
19981822   {
19991823      /* STEP IN/OUT ONE CYLINDER */
20001824      if (VERBOSE>2) LOG("smc92x4 info: step in/out command %X\n", opcode);
2001      step_in_out(device);
1825      step_in_out();
20021826   }
20031827   else if (opcode >= 0x08 && opcode <= 0x0f)
20041828   {
r14513r14514
20091833   {
20101834      /* POLLDRIVE */
20111835      if (VERBOSE>2) LOG("smc92x4 info: polldrive command %X\n", opcode);
2012      poll_drives(device);
1836      poll_drives();
20131837   }
20141838   else if (opcode >= 0x20 && opcode <= 0x3f)
20151839   {
20161840      /* DRIVE SELECT */
20171841      if (VERBOSE>2) LOG("smc92x4 info: drselect command %X\n", opcode);
2018      drive_select(device, opcode&0x1f);
1842      drive_select(opcode&0x1f);
20191843   }
20201844   else if (opcode >= 0x40 && opcode <= 0x4f)
20211845   {
20221846      /* SETREGPTR */
20231847      if (VERBOSE>2) LOG("smc92x4 info: setregptr command %X\n", opcode);
2024      w->register_pointer = opcode & 0xf;
1848      m_register_pointer = opcode & 0xf;
20251849      // Spec does not say anything about the effect of setting an
20261850      // invalid value (only "care should be taken")
2027      if (w->register_pointer > 10)
1851      if (m_register_pointer > 10)
20281852      {
2029         if (VERBOSE>1) LOG("smc92x4 error: set register pointer: Invalid register number: %d. Setting to 10.\n", w->register_pointer);
2030         w->register_pointer = 10;
1853         if (VERBOSE>1) LOG("smc92x4 error: set register pointer: Invalid register number: %d. Setting to 10.\n", m_register_pointer);
1854         m_register_pointer = 10;
20311855      }
20321856   }
20331857   else if (opcode >= 0x50 && opcode <= 0x57)
20341858   {
20351859      /* SEEK/READ ID */
20361860      if (VERBOSE>2) LOG("smc92x4 seekreadid command %X\n", opcode);
2037      seek_read_id(device);
1861      seek_read_id();
20381862   }
20391863   else if ((opcode >= 0x58 && opcode <= 0x59)
20401864      || (opcode >= 0x5C && opcode <= 0x5f)
r14513r14514
20421866   {
20431867      /* READ/WRITE SECTORS PHYSICAL/LOGICAL */
20441868      if (VERBOSE>2) LOG("smc92x4 info: read/write sector command %X\n", opcode);
2045      read_write_sectors(device);
1869      read_write_sectors();
20461870   }
20471871   else if (opcode >= 0x5A && opcode <= 0x5b)
20481872   {
20491873      /* READ TRACK */
20501874      if (VERBOSE>2) LOG("smc92x4 info: read track command %X\n", opcode);
2051      read_track(device);
1875      read_track();
20521876   }
20531877   else if (opcode >= 0x60 && opcode <= 0x7f)
20541878   {
20551879      /* FORMAT TRACK */
20561880      if (VERBOSE>2) LOG("smc92x4 info: format track command %X\n", opcode);
2057      format_track(device);
1881      format_track();
20581882   }
20591883   else
20601884   {
20611885      if (VERBOSE>0) LOG("smc92x4 error: Invalid command %x, ignored\n", opcode);
20621886   }
20631887
2064   if (!w->to_be_continued)
2065      set_command_done(device);
1888   if (!m_to_be_continued)
1889      set_command_done();
20661890}
20671891
20681892/***************************************************************************
r14513r14514
20731897    Read a byte of data from a smc92x4 controller
20741898    The address (offset) encodes the C/D* line (command and /data)
20751899*/
2076READ8_DEVICE_HANDLER( smc92x4_r )
1900READ8_MEMBER( smc92x4_device::read )
20771901{
20781902   UINT8 reply = 0;
2079   smc92x4_state *w = get_safe_token(device);
20801903
20811904   if ((offset & 1) == 0)
20821905   {
20831906      /* data register */
2084      reply = w->register_r[w->register_pointer];
2085      if (VERBOSE>6) LOG("smc92x4 register_r[%d] -> %02x\n", w->register_pointer, reply);
1907      reply = m_register_r[m_register_pointer];
1908      if (VERBOSE>6) LOG("smc92x4 register_r[%d] -> %02x\n", m_register_pointer, reply);
20861909      /* Autoincrement until DATA is reached. */
2087      if (w->register_pointer < DATA)
2088         w->register_pointer++;
1910      if (m_register_pointer < DATA)
1911         m_register_pointer++;
20891912   }
20901913   else
20911914   {
20921915      /* status register */
2093      reply = w->register_r[INT_STATUS];
1916      reply = m_register_r[INT_STATUS];
20941917      // Spec (p.3) : The interrupt pin is reset to its inactive state
20951918      // when the UDC interrupt status register is read.
20961919      if (VERBOSE>6) LOG("smc92x4 interrupt status read = %02x\n", reply);
2097      smc92x4_clear_interrupt(device);
1920      clear_interrupt();
20981921      /* Clear the bits due to int status register read. */
2099      w->register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);
1922      m_register_r[INT_STATUS] &= ~(ST_INTPEND | ST_RDYCHNG);
21001923   }
21011924   return reply;
21021925}
r14513r14514
21051928    Write a byte to a smc99x4 controller
21061929    The address (offset) encodes the C/D* line (command and /data)
21071930*/
2108WRITE8_DEVICE_HANDLER( smc92x4_w )
1931WRITE8_MEMBER( smc92x4_device::write )
21091932{
2110   smc92x4_state *w = get_safe_token(device);
21111933   data &= 0xff;
21121934
21131935   if ((offset & 1) == 0)
21141936   {
21151937      /* data register */
2116      if (VERBOSE>6) LOG("smc92x4 register_w[%d] <- %X\n", w->register_pointer, data);
2117      w->register_w[w->register_pointer] = data;
1938      if (VERBOSE>6) LOG("smc92x4 register_w[%d] <- %X\n", m_register_pointer, data);
1939      m_register_w[m_register_pointer] = data;
21181940
21191941      // The DMA registers and the sector register for read and
21201942      // write are identical.
2121      if (w->register_pointer < DESIRED_HEAD)
2122         w->register_r[w->register_pointer] = data;
1943      if (m_register_pointer < DESIRED_HEAD)
1944         m_register_r[m_register_pointer] = data;
21231945
21241946      /* Autoincrement until DATA is reached. */
2125      if (w->register_pointer < DATA)
2126         w->register_pointer++;
1947      if (m_register_pointer < DATA)
1948         m_register_pointer++;
21271949   }
21281950   else
2129   {
2130      /* command register */
2131      smc92x4_process_command(device, data);
2132   }
1951      process_command(data);   // command register
21331952}
21341953
21351954
r14513r14514
21381957    DEVICE FUNCTIONS
21391958***************************************************************************/
21401959
2141static DEVICE_START( smc92x4 )
1960void smc92x4_device::device_start()
21421961{
2143   smc92x4_state *w = get_safe_token(device);
2144   assert(device->static_config() != NULL);
1962   const smc92x4_interface *intf = reinterpret_cast<const smc92x4_interface *>(static_config());
21451963
2146   w->intf = (const smc92x4_interface*)device->static_config();
2147   w->out_intrq_func.resolve(w->intf->out_intrq_func, *device);
2148   w->out_dip_func.resolve(w->intf->out_dip_func, *device);
2149   w->out_auxbus_func.resolve(w->intf->out_auxbus_func, *device);
2150   w->in_auxbus_func.resolve(w->intf->in_auxbus_func, *device);
1964   m_out_intrq.resolve(intf->out_intrq, *this);
1965   m_out_dip.resolve(intf->out_dip, *this);
1966   m_out_auxbus.resolve(intf->out_auxbus, *this);
1967   m_in_auxbus.resolve(intf->in_auxbus, *this);
1968   m_out_dma.resolve(intf->dma_write, *this);
1969   m_in_dma.resolve(intf->dma_read, *this);
21511970
2152   /* allocate timers */
2153   /* w->timer_data = device->machine().scheduler().timer_alloc(FUNC(smc92x4_data_callback), (void *)device); */
2154   w->timer_rs = device->machine().scheduler().timer_alloc(FUNC(smc92x4_read_sector_callback), (void *)device);
2155   w->timer_ws = device->machine().scheduler().timer_alloc(FUNC(smc92x4_write_sector_callback), (void *)device);
2156   w->timer_track = device->machine().scheduler().timer_alloc(FUNC(smc92x4_track_callback), (void *)device);
2157   w->timer_seek = device->machine().scheduler().timer_alloc(FUNC(smc92x4_seek_callback), (void *)device);
1971   m_full_track_layout = intf->full_track_layout;
21581972
2159   w->use_real_timing = TRUE;
1973   // allocate timers
1974   // m_timer_data = timer_alloc(DATA_TIMER);
1975   m_timer_rs = timer_alloc(READ_TIMER);
1976   m_timer_ws = timer_alloc(WRITE_TIMER);
1977   m_timer_track = timer_alloc(TRACK_TIMER);
1978   m_timer_seek = timer_alloc(SEEK_TIMER);
1979
1980   m_use_real_timing = true;
21601981}
21611982
2162static DEVICE_RESET( smc92x4 )
1983void smc92x4_device::device_reset()
21631984{
2164   smc92x4_state *w = get_safe_token(device);
2165   smc92x4_clear_interrupt(device);
2166   smc92x4_set_dip(device, FALSE);
1985   clear_interrupt();
1986   m_out_dip(CLEAR_LINE);
21671987
2168   for (int i=0; i<11; i++)
2169      w->register_r[i] = w->register_w[i] = 0;
1988   for (int i=0; i<=11; i++)
1989      m_register_r[i] = m_register_w[i] = 0;
1990
1991   m_to_be_continued = false;
21701992}
21711993
2172void smc92x4_reset(device_t *device)
1994void smc92x4_device::set_timing(bool realistic)
21731995{
2174   DEVICE_RESET_CALL( smc92x4 );
1996   m_use_real_timing = realistic;
1997   if (VERBOSE>0) LOG("smc92x4: use realistic timing: %02x\n", realistic);
21751998}
21761999
2177void smc92x4_set_timing(device_t *device, int realistic)
2000void smc92x4_device::connect_drive(device_t *drive)
21782001{
2179   smc92x4_state *w = get_safe_token(device);
2180   w->use_real_timing = realistic;
2181   if (VERBOSE>0) LOG("smc92x4: use realistic timing: %02x\n", realistic);
2002   m_drive = drive;
2003   if (VERBOSE>3)
2004   {
2005      if (drive==NULL) LOG("smc92x4: Unselect all drives\n");
2006      else LOG("smc92x4: Connect drive %s\n", drive->tag());
2007   }
21822008}
21832009
2184/***************************************************************************
2185    DEVICE GETINFO
2186***************************************************************************/
2010void smc92x4_device::reset()
2011{
2012   device_reset();
2013}
21872014
2188static const char DEVTEMPLATE_SOURCE[] = __FILE__;
2189
2190#define DEVTEMPLATE_ID(p,s)            p##smc92x4##s
2191#define DEVTEMPLATE_FEATURES            DT_HAS_START | DT_HAS_RESET
2192#define DEVTEMPLATE_NAME            "SMC92x4"
2193#define DEVTEMPLATE_FAMILY            "SMC92x4"
2194#define DEVTEMPLATE_VERSION            "1.0"
2195#define DEVTEMPLATE_CREDITS            "Copyright MESS Team"
2196#include "devtempl.h"
2197
2198DEFINE_LEGACY_DEVICE(SMC92X4, smc92x4);
2015const device_type SMC92X4 = &device_creator<smc92x4_device>;
src/mess/machine/smc92x4.h
r14513r14514
33#ifndef __SMC92X4_H__
44#define __SMC92X4_H__
55
6DECLARE_LEGACY_DEVICE(SMC92X4, smc92x4);
6#include "ti99/ti99_hd.h"
7#include "imagedev/flopdrv.h"
8#include "formats/imageutl.h"
9#include "imagedev/harddriv.h"
10#include "harddisk.h"
711
812#define INPUT_STATUS   0x00
913#define OUTPUT_DMA_ADDR 0x01
r14513r14514
1923#define BAD_SECTOR   0x1000
2024#define BAD_CRC      0x2000
2125
26extern const device_type SMC92X4;
2227/*
2328    Definition of bits in the Disk-Status register
2429*/
r14513r14514
3136#define DS_READY   0x02      /* drive ready bit */
3237#define DS_WRFAULT   0x01      /* write fault */
3338
34/*
35    Needed to adapt to higher cylinder numbers. Floppies do not have such
36    high numbers.
37*/
38typedef struct chrn_id_hd
39{
40   UINT16 C;
41   UINT8 H;
42   UINT8 R;
43   UINT8 N;
44   int data_id;         // id for read/write data command
45   unsigned long flags;
46} chrn_id_hd;
47
4839typedef struct _smc92x4_interface
4940{
50   /* Disk format support. This flag allows to choose between the full
51       FM/MFM format and an abbreviated track layout. The difference results
52       from legal variations of the layout. This is not part of
53       the smc92x4 specification, but it allows to keep the image format
54       simple without too much case checking. Should be removed as soon as
55       the respective disk formats support the full format. */
41   // Disk format support. This flag allows to choose between the full
42   // FM/MFM format and an abbreviated track layout. The difference results
43   // from legal variations of the layout. This is not part of
44   // the smc92x4 specification, but it allows to keep the image format
45   // simple without too much case checking. Should be removed as soon as
46   // the respective disk formats support the full format.
5647   int full_track_layout;
5748
58   /* Interrupt line. To be connected with the controller PCB. */
59   devcb_write_line out_intrq_func;
49   // Interrupt line. To be connected with the controller PCB.
50   devcb_write_line out_intrq;
6051
61   /* DMA in progress line. To be connected with the controller PCB. */
62   devcb_write_line out_dip_func;
52   // DMA in progress line. To be connected with the controller PCB.
53   devcb_write_line out_dip;
6354
64   /* Auxiliary Bus. These 8 lines need to be connected to external latches
65    and to a counter circuitry which works together with the external RAM.
66    We use the S0/S1 lines as address lines.
67    */
68   devcb_write8 out_auxbus_func;
55   // Auxiliary Bus. These 8 lines need to be connected to external latches
56   // and to a counter circuitry which works together with the external RAM.
57   // We use the S0/S1 lines as address lines.
58   devcb_write8   out_auxbus;
6959
70   /* Auxiliary Bus. This is only used for S0=S1=0, so we need no address
71    lines and consider this line just as a line with 256 states. */
72   devcb_read_line in_auxbus_func;
60   // Auxiliary Bus. This is only used for S0=S1=0.
61   devcb_read8    in_auxbus;
7362
74   /* Get the currently selected floppy disk. This is determined by the
75       circuitry on the controller board, not within the controller itself.
76       The device is this controller.
77    */
78   device_t *(*current_floppy)(device_t *device);
63   // Callback to read the contents of the external RAM via the data bus.
64   // Note that the address must be set and automatically increased
65   // by external circuitry.
66   devcb_read8    dma_read;
7967
80   /* Callback to read the contents of the external RAM via the data bus.
81       Note that the address must be set and automatically increased
82       by external circuitry. */
83   UINT8 (*dma_read_callback)(device_t *device);
68   // Callback to write the contents of the external RAM via the data bus.
69   // Note that the address must be set and automatically increased
70   // by external circuitry. */
71   devcb_write8   dma_write;
8472
85   /* Callback to write the contents of the external RAM via the data bus.
86       Note that the address must be set and automatically increased
87       by external circuitry. */
88   void (*dma_write_callback)(device_t *device, UINT8 data);
73} smc92x4_interface;
8974
90   /* Preliminary MFM hard disk interface. Gets next id. */
91   void (*mfmhd_get_next_id)(device_t *device, int head, chrn_id_hd *id);
9275
93   /* Preliminary MFM hard disk interface. Performs a seek. */
94   void (*mfmhd_seek)(device_t *device, int direction);
76class smc92x4_device : public device_t
77{
78public:
79   smc92x4_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
9580
96   /* Preliminary MFM hard disk interface. Reads a sector. */
97   void (*mfmhd_read_sector)(device_t *device, int cylinder, int head, int sector, UINT8 **buf, int *sector_length);
81   DECLARE_READ8_MEMBER( read );
82   DECLARE_WRITE8_MEMBER( write );
9883
99   /* Preliminary MFM hard disk interface. Writes a sector. */
100   void (*mfmhd_write_sector)(device_t *device, int cylinder, int head, int sector, UINT8 *buf, int sector_length);
84   // Used to turn off the delays.
85   void set_timing(bool realistic);
10186
102   /* Preliminary MFM hard disk interface. Reads a track. */
103   void (*mfmhd_read_track)(device_t *device, int head, UINT8 **buffer, int *data_count);
87   // Used to reconfigure the drive connections. Drive selection is done
88   // using the select lines and maybe also the user-programmable outputs.
89   void connect_drive(device_t *drive);
10490
105   /* Preliminary MFM hard disk interface. Writes a track. */
106   void (*mfmhd_write_track)(device_t *device, int head, UINT8 *buffer, int data_count);
107} smc92x4_interface;
91   void reset();
10892
109void smc92x4_reset(device_t *device);
93protected:
94   void device_start(void);
95   void device_reset(void);
11096
111/* Used to turn off the delays. */
112void smc92x4_set_timing(device_t *device, int realistic);
97private:
98   devcb_resolved_write_line   m_out_intrq;   // INT line
99   devcb_resolved_write_line   m_out_dip;      // DMA in progress line
100   devcb_resolved_write8      m_out_auxbus;   // AB0-7 lines (using S0,S1 as address)
101   devcb_resolved_read8      m_in_auxbus;   // AB0-7 lines (S0=S1=0)
102   devcb_resolved_read8      m_in_dma;      // DMA read access to the cache buffer
103   devcb_resolved_write8      m_out_dma;      // DMA write access to the cache buffer
113104
114/* Generic function to translate between cylinders and idents */
115UINT8 cylinder_to_ident(int cylinder);
105   UINT8 m_output1;      // internal register "output1"
106   UINT8 m_output2;      // internal register "output2"
116107
117READ8_DEVICE_HANDLER( smc92x4_r );
118WRITE8_DEVICE_HANDLER( smc92x4_w );
108   void   set_dip(line_state value);
109   int    image_is_single_density();
110   bool   in_single_density_mode();
111   void   copyid(chrn_id id1, chrn_id_hd *id2);
112   void   set_interrupt();
113   void   clear_interrupt();
114   void   set_command_done(int flags);
115   void   set_command_done();
116   void   set_dma_address(int pos2316, int pos1508, int pos0700);
117   void   dma_add_offset(int offset);
118   void   sync_status_in();
119   void   sync_latches_out();
120   void   timed_sector_read_request();
121   void   timed_sector_write_request();
122   void   timed_track_request();
123   void   timed_seek_request();
124   void   device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
125   UINT8   cylinder_to_ident(int cylinder);
126   int      ident_to_cylinder(UINT8 ident);
127   void   update_id_regs(chrn_id_hd id);
128   void   read_id_field(int *steps, int *direction, chrn_id_hd *id);
129   int      verify(chrn_id_hd *id, bool check_sector);
130   void   data_transfer_read(chrn_id_hd id, int transfer_enable);
131   void   data_transfer_write(chrn_id_hd id, int deldata, int redcur, int precomp, bool write_long);
132   void   read_write_sectors();
133   void   read_sectors_continue();
134   void   write_sectors_continue();
119135
136   void   restore_drive();
137   void   restore_continue();
138
139   void   step_in_out();
140   void   step_continue();
141
142   void   poll_drives();
143
144   void   drive_select(int driveparm);
145
146   void   seek_read_id();
147   void   seek_read_id_continue();
148
149   void   format_track();
150   void   format_track_continue();
151   void   format_floppy_track(int flags);
152   void   format_harddisk_track(int flags);
153
154   void   read_track();
155   void   read_track_continue();
156   void   read_floppy_track(bool transfer_only_ids);
157   void   read_harddisk_track(bool transfer_only_ids);
158
159   void   process_after_callback();
160   void   process_command(UINT8 opcode);
161
162   int   m_selected_drive_type;
163   // We need to store the types, although this is not the case in the
164   // real hardware. The reason is that poll_drives wants to select the
165   // drives without knowing their kinds. In reality this does not matter,
166   // since we only check for the seek_complete line, but in our case,
167   // floppy and harddrive are pretty different, and we need to know which
168   // one to check.
169   int   m_types[4];
170   int   m_head_load_delay_enable;
171   int   m_register_pointer;
172
173   UINT8 m_register_r[12];
174   UINT8 m_register_w[12];
175
176   /* timers to delay execution/completion of commands */
177   emu_timer *m_timer_data, *m_timer_rs, *m_timer_ws, *m_timer_seek, *m_timer_track;
178
179   /* Flag which determines whether to use realistic timing. */
180   bool m_use_real_timing;
181
182   /* Required to store the current iteration within restore. */
183   int m_seek_count;
184
185   /* Recent command. */
186   UINT8 m_command;
187
188   /* Stores the step direction. */
189   int m_step_direction;
190
191   /* Stores the buffered flag. */
192   bool m_buffered;
193
194   /* Stores the recent id field. */
195   chrn_id_hd m_recent_id;
196
197   /* Indicates whether an ID has been found. */
198   bool m_found_id;
199
200   /* Indicates whether we are already past the seek_id phase in read/write sector. */
201   bool m_after_seek;
202
203   /* Determines whether the command will be continued. */
204   bool m_to_be_continued;
205
206   bool m_full_track_layout;
207
208   // Link to attached drive (hard / floppy disk)
209   // Hard disks must be instances of mfmhd_device; floppy disks are the common drives.
210   // We expect the embedding board to replace the drive according to the
211   // select lines.
212   device_t   *m_drive;
213};
214
120215#define MCFG_SMC92X4_ADD(_tag, _intrf) \
121216   MCFG_DEVICE_ADD(_tag, SMC92X4, 0) \
122217   MCFG_DEVICE_CONFIG(_intrf)
src/mess/mess_dev.lst
r14513r14514
110110KB_KEYTRONIC
111111LUXOR_55_10828
112112LUXOR_55_21046
113HFDC
114113NUBUS_48GC
115114NUBUS_824GC
116115NUBUS_CB264
r14513r14514
125124PC1512_KEYBOARD
126125S1410
127126SED1330
128BWG
129EVPC
130127TF20
131TISPEECH8
132PCODEN
133TIRS232
134TISPEECH
135TIFDC
136128UPD7220
137129VIC1112
138130VIC1211A
r14513r14514
151143VICTOR9K_KEYBOARD
152144TANDY2K_KEYBOARD
153145WANGPC_KEYBOARD
146C64_IEEE488
147TI99_FDC
148TI99_BWG
149TI99_HFDC
150TI99_P_CODE
151TI99_MYARCMEM
152TI99_EVPC
153TI99_RS232
154TI99_SPEECH
155TI99_SPEECH8
src/mess/mess.mak
r14513r14514
16321632   $(MESS_MACHINE)/990_tap.o   \
16331633   $(MESS_MACHINE)/ti990.o      \
16341634   $(MESS_MACHINE)/at29040a.o   \
1635   $(MESS_MACHINE)/ti99/hsgpl.o   \
1636   $(MESS_MACHINE)/ti99/p_code.o   \
1637   $(MESS_MACHINE)/ti99/crubus.o   \
16381635   $(MESS_MACHINE)/ti99/datamux.o   \
1639   $(MESS_MACHINE)/ti99/peribox.o   \
1640   $(MESS_MACHINE)/ti99/grom.o   \
1641   $(MESS_MACHINE)/ti99/gromport.o   \
1642   $(MESS_MACHINE)/ti99/ti32kmem.o   \
1643   $(MESS_MACHINE)/ti99/samsmem.o   \
1644   $(MESS_MACHINE)/ti99/memex.o   \
1645   $(MESS_MACHINE)/ti99/myarcmem.o   \
1646   $(MESS_MACHINE)/ti99/ti_fdc.o   \
1647   $(MESS_MACHINE)/ti99/hfdc.o   \
1648   $(MESS_MACHINE)/ti99/bwg.o   \
1649   $(MESS_MACHINE)/ti99/ti_rs232.o   \
1650   $(MESS_MACHINE)/ti99/tn_usbsm.o   \
1651   $(MESS_MACHINE)/ti99/tn_ide.o   \
1652   $(MESS_MACHINE)/ti99/spchsyn.o  \
1653   $(MESS_MACHINE)/ti99/evpc.o    \
16541636   $(MESS_MACHINE)/ti99/videowrp.o \
1655   $(MESS_MACHINE)/ti99/mapper8.o  \
1656   $(MESS_MACHINE)/ti99/speech8.o  \
1637   $(MESS_MACHINE)/ti99/grom.o   \
1638   $(MESS_MACHINE)/ti99/gromport.o  \
16571639   $(MESS_MACHINE)/ti99/mecmouse.o  \
16581640   $(MESS_MACHINE)/ti99/handset.o  \
1659   $(MESS_MACHINE)/ti99/sgcpu.o  \
1660   $(MESS_MACHINE)/ti99/genboard.o  \
1661   $(MESS_MACHINE)/ti99/tiboard.o  \
1641   $(MESS_MACHINE)/ti99/peribox.o  \
1642   $(MESS_MACHINE)/ti99/ti32kmem.o  \
1643   $(MESS_MACHINE)/ti99/ti_fdc.o  \
1644   $(MESS_MACHINE)/ti99/bwg.o  \
1645   $(MESS_MACHINE)/ti99/hfdc.o  \
16621646   $(MESS_MACHINE)/ti99/ti99_hd.o   \
1647   $(MESS_MACHINE)/ti99/p_code.o \
1648   $(MESS_MACHINE)/ti99/myarcmem.o \
1649   $(MESS_MACHINE)/ti99/samsmem.o \
1650   $(MESS_MACHINE)/ti99/tn_ide.o \
1651   $(MESS_MACHINE)/ti99/tn_usbsm.o \
1652   $(MESS_MACHINE)/ti99/evpc.o   \
1653   $(MESS_MACHINE)/ti99/hsgpl.o   \
1654   $(MESS_MACHINE)/ti99/ti_rs232.o   \
1655   $(MESS_MACHINE)/ti99/spchsyn.o   \
1656   $(MESS_MACHINE)/ti99/speech8.o   \
1657   $(MESS_MACHINE)/ti99/mapper8.o   \
1658   $(MESS_MACHINE)/ti99/genboard.o   \
1659   $(MESS_MACHINE)/ti99/memex.o   \
16631660   $(MESS_MACHINE)/strata.o   \
16641661   $(MESS_MACHINE)/990_dk.o   \
16651662   $(MESS_DRIVERS)/ti990_4.o   \
16661663   $(MESS_DRIVERS)/ti99_4x.o   \
16671664   $(MESS_DRIVERS)/ti99_4p.o   \
1665   $(MESS_DRIVERS)/ti99_8.o   \
16681666   $(MESS_DRIVERS)/geneve.o   \
16691667   $(MESS_DRIVERS)/tm990189.o   \
1670   $(MESS_DRIVERS)/ti99_8.o   \
16711668   $(MESS_VIDEO)/911_vdt.o      \
16721669   $(MESS_VIDEO)/733_asr.o      \
16731670   $(MESS_DRIVERS)/ti990_10.o   \
src/mess/drivers/ti99_8.c
r14513r14514
1/*
2    MESS Driver for TI-99/8 Computer.
3    Raphael Nabet, 2003.
4*/
5/*
6    TI-99/8 preliminary info:
1/****************************************************************************
72
3    The MESS TI-99/8 emulation driver
4
5    The TI-99/8 was the envisaged successor to the TI-99/4A but never passed
6    its prototype state. Only a few consoles were built. The ROMs were not
7    even finalized, so the few available consoles may have different
8    operating system versions.
9
10    There is some preliminary info:
11
812Name: Texas Instruments Computer TI-99/8 (no "Home")
913
1014References:
r14513r14514
186190- SAVE and OLD MINIMEM do not work properly in XB II. It seems as if the
187191  mapper shadows the NVRAM of the cartridge. You will lose the contents when
188192  you turn off the machine.
189===========================================================================
190193
191*/
194    Raphael Nabet, 2003.
195    Michael Zapf
192196
197    February 2012: Rewritten as class
198
199*****************************************************************************/
200
201#define ADDRESS_MAP_MODERN
202
193203#include "emu.h"
194204#include "cpu/tms9900/tms9900.h"
195205#include "sound/sn76496.h"
196//#include "video/tms9928a.h"
197206#include "sound/wave.h"
198207#include "machine/tms9901.h"
199208#include "imagedev/cassette.h"
200209
201#include "machine/ti99/tiboard.h"
202
203210#include "machine/ti99/videowrp.h"
204211#include "machine/ti99/speech8.h"
205212
r14513r14514
210217#include "machine/ti99/gromport.h"
211218#include "machine/ti99/mecmouse.h"
212219
220#define VERBOSE 1
221#define LOG logerror
213222
214class ti99_8_state : public driver_device
223class ti99_8 : public driver_device
215224{
216225public:
217   ti99_8_state(const machine_config &mconfig, device_type type, const char *tag)
226   ti99_8(const machine_config &mconfig, device_type type, const char *tag)
218227      : driver_device(mconfig, type, tag) { }
219228
229   // CRU (Communication Register Unit) handling
230   DECLARE_READ8_MEMBER(cruread);
231   DECLARE_WRITE8_MEMBER(cruwrite);
232
233   // Forwarding interrupts to the CPU or CRU
234   DECLARE_WRITE_LINE_MEMBER( console_ready );
235   DECLARE_WRITE_LINE_MEMBER( set_tms9901_INT2 );
236   DECLARE_WRITE_LINE_MEMBER( extint );
237   DECLARE_WRITE_LINE_MEMBER( notconnected );
238
239   // Connections with the system interface TMS9901
240   DECLARE_READ8_MEMBER(read_by_9901);
241   DECLARE_WRITE_LINE_MEMBER(keyC0);
242   DECLARE_WRITE_LINE_MEMBER(keyC1);
243   DECLARE_WRITE_LINE_MEMBER(keyC2);
244   DECLARE_WRITE_LINE_MEMBER(keyC3);
245   DECLARE_WRITE_LINE_MEMBER(CRUS);
246   DECLARE_WRITE_LINE_MEMBER(PTGEN);
247   DECLARE_WRITE_LINE_MEMBER(audio_gate);
248   DECLARE_WRITE_LINE_MEMBER(cassette_output);
249   DECLARE_WRITE_LINE_MEMBER(cassette_motor);
250   DECLARE_WRITE8_MEMBER(tms9901_interrupt);
251
252   // Some values to keep
253   device_t         *m_cpu;
254   tms9901_device      *m_tms9901;
255   gromport_device      *m_gromport;
256   peribox_device      *m_peribox;
257   mecmouse_device    *m_mecmouse;
258   ti998_mapper_device   *m_mapper;
259
260private:
261   /* Keyboard support */
262   void         set_key(int number, int data);
263   int            m_keyboard_column;
264   int            m_alphalock_line;
220265};
221266
222267
223268/*
224    Memory map - see description above
269    Memory map. We have a configurable mapper, so we need to delegate the
270    job to the mapper completely.
225271*/
226
227static ADDRESS_MAP_START(ti99_8_memmap, AS_PROGRAM, 8)
228   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE("mapper", ti99_mapper8_r, ti99_mapper8_w )
272static ADDRESS_MAP_START(memmap, AS_PROGRAM, 8, ti99_8)
273   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE(MAPPER_TAG, ti998_mapper_device, readm, writem )
229274ADDRESS_MAP_END
230275
231
232276/*
233277    CRU map - see description above
234278    The TMS9901 is fully decoded according to the specification, so we only
r14513r14514
237281    TODO: Check whether the CRU address space goes up to 7fff (15 bit)
238282*/
239283
240static ADDRESS_MAP_START(ti99_8_cru_map, AS_IO, 8)
241   AM_RANGE(0x0000, 0x0003) AM_DEVREAD("tms9901", tms9901_cru_r)
242   AM_RANGE(0x0000, 0x02ff) AM_DEVREAD("crubus", ti99_crubus_r )
284static ADDRESS_MAP_START(crumap, AS_IO, 8, ti99_8)
285   AM_RANGE(0x0000, 0x0003) AM_DEVREAD(TMS9901_TAG, tms9901_device, read)
286   AM_RANGE(0x0000, 0x02ff) AM_READ(cruread)
243287
244   AM_RANGE(0x0000, 0x001f) AM_DEVWRITE("tms9901", tms9901_cru_w)
245   AM_RANGE(0x0000, 0x17ff) AM_DEVWRITE("crubus", ti99_crubus_w )
288   AM_RANGE(0x0000, 0x001f) AM_DEVWRITE(TMS9901_TAG, tms9901_device, write)
289   AM_RANGE(0x0000, 0x17ff) AM_WRITE(cruwrite)
246290ADDRESS_MAP_END
247291
248292/* ti99/8 : 54-key keyboard */
249293static INPUT_PORTS_START(ti99_8)
250   PORT_START( "DISKCTRL" )
251   PORT_CONFNAME( 0x07, 0x00, "Disk controller" )
252      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
253      PORT_CONFSETTING(    0x01, "TI SD Floppy Controller" )
254      PORT_CONFSETTING(    0x02, "SNUG BwG Controller" )
255      PORT_CONFSETTING(    0x03, "Myarc HFDC" )
256
257   PORT_START( "HDCTRL" )
258   PORT_CONFNAME( 0x03, 0x00, "HD controller" )
259      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
260      PORT_CONFSETTING(    0x01, "Nouspikel IDE Controller" )
261//      PORT_CONFSETTING(    0x02, "WHTech SCSI Controller" )
262   PORT_CONFNAME( 0x08, 0x00, "USB-SM card" )
294   PORT_START( "MECMOUSE" )
295   PORT_CONFNAME( 0x01, 0x00, "Mechatronics Mouse" )
263296      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
264      PORT_CONFSETTING(    0x08, DEF_STR( On ) )
297      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
265298
266   PORT_START( "SERIAL" )
267   PORT_CONFNAME( 0x03, 0x00, "Serial/Parallel interface" )
268      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
269      PORT_CONFSETTING(    0x01, "TI RS-232 card" )
270
271   PORT_START( "SERIALMAP" )
272   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" ) PORT_CONDITION( "SERIAL", 0x03, PORTCOND_NOTEQUALS, 0x00 )
273      PORT_CONFSETTING(    0x00, "6-20" )
274      PORT_CONFSETTING(    0x01, "8-20" )
275      PORT_CONFSETTING(    0x02, "5-20" )
276
277   PORT_START( "HCI" )
278   PORT_CONFNAME( 0x01, 0x00, "Mouse support" )
279      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
280      PORT_CONFSETTING(    0x01, "Mechatronics Mouse" )
281
282   PORT_START( "CARTSLOT" )
283   PORT_DIPNAME( 0x0f, 0x00, "Cartridge slot" )
284      PORT_DIPSETTING(    0x00, "Auto" )
285      PORT_DIPSETTING(    0x01, "Slot 1" )
286      PORT_DIPSETTING(    0x02, "Slot 2" )
287      PORT_DIPSETTING(    0x03, "Slot 3" )
288      PORT_DIPSETTING(    0x04, "Slot 4" )
289
290   PORT_START( "BWGDIP1" )
291   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
292      PORT_DIPSETTING( 0x00, "6 ms")
293      PORT_DIPSETTING( 0x01, "20 ms")
294
295   PORT_START( "BWGDIP2" )
296   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
297      PORT_DIPSETTING( 0x00, "Hide")
298      PORT_DIPSETTING( 0x01, "Show")
299
300   PORT_START( "BWGDIP34" )
301   PORT_DIPNAME( 0x03, 0x00, "BwG drives" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
302      PORT_DIPSETTING( 0x00, "DSK1 only")
303      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
304      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
305      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
306
307   PORT_START( "HFDCDIP" )
308   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
309      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
310      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
311      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
312      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
313
314   PORT_START( "DRVSPD" )
315   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
316      PORT_CONFSETTING( 0x00, "No delay")
317      PORT_CONFSETTING( 0x01, "Realistic")
318
319   // We do not want to show this setting; makes only sense for Geneve
320   PORT_START( "MODE" )
321   PORT_CONFNAME( 0x01, 0x00, "Ext. cards modification" ) PORT_CONDITION( "HFDCDIP", 0xff, PORTCOND_EQUALS, GM_NEVER )
322      PORT_CONFSETTING(    0x00, "Standard" )
323      PORT_CONFSETTING(    GENMOD, "GenMod" )
324
325299   /* 3 ports for mouse */
326300   PORT_START("MOUSEX") /* Mouse - X AXIS */
327301   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
r14513r14514
445419   PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_BUTTON1/*, "(2FIRE)", CODE_NONE, OSD_JOY2_FIRE, 0*/) PORT_PLAYER(2)
446420INPUT_PORTS_END
447421
422/*****************************************************************************
423    Components
424******************************************************************************/
448425
449/* F4 Character Displayer */
450static const gfx_layout ti99_7_charlayout =
426static GROM_CONFIG(grom0_config)
451427{
452   8, 7,               /* 8 x 7 characters */
453   96,               /* 96 characters */
454   1,               /* 1 bits per pixel */
455   { 0 },               /* no bitplanes */
456   /* x offsets */
457   { 0, 1, 2, 3, 4, 5, 6, 7 },
458   /* y offsets */
459   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8 },
460   8*7               /* every char takes 7 bytes */
428   false, 0, region_grom, 0x0000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_8, console_ready)
461429};
462430
463static const gfx_layout ti99_8_charlayout =
431static GROM_CONFIG(grom1_config)
464432{
465   8, 8,               /* 8 x 8 characters */
466   64,               /* 64 characters */
467   1,               /* 1 bits per pixel */
468   { 0 },               /* no bitplanes */
469   /* x offsets */
470   { 0, 1, 2, 3, 4, 5, 6, 7 },
471   /* y offsets */
472   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
473   8*8               /* every char takes 8 bytes */
433   false, 1, region_grom, 0x2000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_8, console_ready)
474434};
475435
476static const gfx_layout ti99_c_charlayout =
436static GROM_CONFIG(grom2_config)
477437{
478   8, 8,               /* 8 x 8 characters */
479   1,               /* 1 characters */
480   1,               /* 1 bits per pixel */
481   { 0 },               /* no bitplanes */
482   /* x offsets */
483   { 0, 1, 2, 3, 4, 5, 6, 7 },
484   /* y offsets */
485   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
486   8*8               /* every char takes 8 bytes */
438   false, 2, region_grom, 0x4000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_8, console_ready)
487439};
488440
489static const gfx_layout ti99_24_charlayout =
441static GROMPORT_CONFIG(console_cartslot)
490442{
491   24, 24,               /* 24 x 24 characters */
492   1,               /* 1 characters */
493   1,               /* 1 bits per pixel */
494   { 0 },               /* no bitplanes */
495   /* x offsets */
496   { 0, 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135 },
497   /* y offsets */
498   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8, 24*8, 25*8, 26*8, 27*8, 28*8, 29*8, 30*8, 31*8, 48*8, 49*8, 50*8, 51*8, 52*8, 53*8, 54*8, 55*8 },
499   24*24               /* every char takes 72 bytes */
443   DEVCB_DRIVER_LINE_MEMBER(ti99_8, console_ready)
500444};
501445
502static GFXDECODE_START( ti99_8 )
503   GFXDECODE_ENTRY( region_grom, 0x050d, ti99_8_charlayout, 0, 8 )   // large
504   GFXDECODE_ENTRY( region_grom, 0x070d, ti99_7_charlayout, 0, 8 ) // small
505   GFXDECODE_ENTRY( region_grom, 0x09b4, ti99_24_charlayout, 0, 8 )// TI logo
506   GFXDECODE_ENTRY( region_grom, 0x09fc, ti99_c_charlayout, 0, 8 )   // (c)
507GFXDECODE_END
446static PERIBOX_CONFIG( peribox_conf )
447{
448   DEVCB_DRIVER_LINE_MEMBER(ti99_8, extint),         // INTA
449   DEVCB_DRIVER_LINE_MEMBER(ti99_8, notconnected),      // INTB
450   DEVCB_DRIVER_LINE_MEMBER(ti99_8, console_ready),   // READY
451   0x70000                                    // Address bus prefix (AMA/AMB/AMC)
452};
508453
454READ8_MEMBER( ti99_8::cruread )
455{
456//  if (VERBOSE>6) LOG("read access to CRU address %04x\n", offset << 4);
457   UINT8 value = 0;
509458
510static WRITE_LINE_DEVICE_HANDLER(set_int2)
459   // Similar to the bus8z_devices, just let the mapper, the gromport, and the p-box
460   // decide whether they want to change the value at the CRU address
461   // Also, we translate the bit addresses to base addresses
462   m_mapper->crureadz(offset<<4, &value);
463   m_gromport->crureadz(offset<<4, &value);
464   m_peribox->crureadz(offset<<4, &value);
465
466   if (VERBOSE>8) LOG("ti99_8: CRU %04x -> %02x\n", offset<<4, value);
467   return value;
468}
469
470WRITE8_MEMBER( ti99_8::cruwrite )
511471{
512   tms9901_set_int2(device->machine().device("ti_board"), state );
472   if (VERBOSE>8) LOG("ti99_8: CRU %04x <- %x\n", offset<<1, data);
473   m_mapper->cruwrite(offset<<1, data);
474   m_gromport->cruwrite(offset<<1, data);
475   m_peribox->cruwrite(offset<<1, data);
513476}
514477
515static TMS9928A_INTERFACE(ti99_8_tms9928a_interface)
478/***************************************************************************
479    TI99/8-specific tms9901 I/O handlers
480    These methods are callbacks from the TMS9901 system interface. That is,
481    they deliver the values queried via the TMS9901, and they represent
482    console functions which are under control of the TMS9901 (like the
483    keyboard column selection.)
484***************************************************************************/
485
486static const char *const keynames8[] = {
487   "KEY0", "KEY1", "KEY2", "KEY3", "KEY4", "KEY5", "KEY6", "KEY7",
488   "KEY8", "KEY9", "KEY10", "KEY11", "KEY12", "KEY13", "KEY14", "KEY15"
489};
490
491READ8_MEMBER( ti99_8::read_by_9901 )
516492{
493   int answer=0;
494
495   switch (offset & 0x03)
496   {
497   case TMS9901_CB_INT7:
498      // Read pins INT3*-INT7* of TI99's 9901.
499      //
500      // (bit 1: INT1 status)
501      // (bit 2: INT2 status)
502      // bits 3-4: unused?
503      // bit 5: ???
504      // bit 6-7: keyboard status bits 0 through 1
505      if (m_mecmouse != NULL && (m_keyboard_column == 15))
506      {
507         answer = m_mecmouse->get_values8(0);
508      }
509      else
510      {
511         answer = (input_port_read(machine(), keynames8[m_keyboard_column]) << 6) & 0xC0;
512      }
513      break;
514
515   case TMS9901_INT8_INT15:
516      // Read pins INT8*-INT15* of TI99's 9901.
517      //
518      // bit 0-2: keyboard status bits 2 to 4
519      // bit 3: tape input mirror
520      // (bit 4: IR remote handset interrupt)
521      // bit 5-7: weird, not emulated
522      if (m_mecmouse != NULL && (m_keyboard_column == 15))
523      {
524         answer = m_mecmouse->get_values8(1);
525      }
526      else
527      {
528         answer = (input_port_read(machine(), keynames8[m_keyboard_column]) >> 2) & 0x07;
529      }
530      break;
531
532   case TMS9901_P0_P7:
533      // Read pins P0-P7 of TI99's 9901. None here.
534      break;
535
536   case TMS9901_P8_P15:
537      // Read pins P8-P15 of TI99's 9901. (TI-99/8)
538      //
539      // bit 26: high
540      // bit 27: tape input
541      int answer = 4;
542      if ((machine().device<cassette_image_device>(CASSETTE_TAG))->input() > 0)
543         answer |= 8;
544      break;
545   }
546   return answer;
547}
548
549/*
550    WRITE key column select (P2-P4), TI-99/8
551*/
552void ti99_8::set_key(int number, int data)
553{
554   if (data!=0)   m_keyboard_column |=  1 << number;
555   else         m_keyboard_column &= ~(1 << number);
556
557   if (m_mecmouse != NULL)
558      m_mecmouse->select(m_keyboard_column, 14, 15);
559}
560
561WRITE_LINE_MEMBER( ti99_8::keyC0 )
562{
563   set_key(0, state);
564}
565
566WRITE_LINE_MEMBER( ti99_8::keyC1 )
567{
568   set_key(1, state);
569}
570
571WRITE_LINE_MEMBER( ti99_8::keyC2 )
572{
573   set_key(2, state);
574}
575
576WRITE_LINE_MEMBER( ti99_8::keyC3 )
577{
578   set_key(3, state);
579}
580
581/*
582    Set 99/4A compatibility mode (CRUS=1)
583*/
584WRITE_LINE_MEMBER( ti99_8::CRUS )
585{
586   m_mapper->CRUS_set(state==ASSERT_LINE);
587}
588
589/*
590    Set mapper /PTGEN. This is negative logic; we use PTGE as the positive logic signal.
591*/
592WRITE_LINE_MEMBER( ti99_8::PTGEN )
593{
594   m_mapper->PTGE_set(state==CLEAR_LINE);
595}
596
597/*
598    Control cassette tape unit motor (P6)
599*/
600WRITE_LINE_MEMBER( ti99_8::cassette_motor )
601{
602   cassette_image_device *img = machine().device<cassette_image_device>(CASSETTE_TAG);
603   img->change_state(state==ASSERT_LINE? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
604}
605
606/*
607    Audio gate (P8)
608    Set to 1 before using tape: this enables the mixing of tape input sound
609    with computer sound.
610    We do not really need to emulate this as the tape recorder generates sound
611    on its own.
612*/
613WRITE_LINE_MEMBER( ti99_8::audio_gate )
614{
615}
616
617/*
618    Tape output (P9)
619    I think polarity is correct, but don't take my word for it.
620*/
621WRITE_LINE_MEMBER( ti99_8::cassette_output )
622{
623   machine().device<cassette_image_device>(CASSETTE_TAG)->output(state==ASSERT_LINE? +1 : -1);
624}
625
626WRITE8_MEMBER( ti99_8::tms9901_interrupt )
627{
628   // offset contains the interrupt level (0-15)
629   if (data==ASSERT_LINE)
630   {
631      // The TMS9901 should normally be connected with the CPU by 5 wires:
632      // INTREQ* and IC0-IC3. The last four lines deliver the interrupt level.
633      // On the TI-99 systems these IC lines are not used; the input lines
634      // at the CPU are hardwired to level 1.
635      if (VERBOSE>8) LOG("mapper8: tms9901 asserts interrupt\n");
636      device_execute(m_cpu)->set_input_line_and_vector(0, ASSERT_LINE, 1);
637   }
638   else
639   {
640      if (VERBOSE>8) LOG("mapper8: tms9901 clears interrupt\n");
641      device_execute(m_cpu)->set_input_line(0, CLEAR_LINE);
642   }
643}
644
645const tms9901_interface tms9901_wiring_ti99_8 =
646{
647   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,
648
649   // read handler
650   DEVCB_DRIVER_MEMBER(ti99_8, read_by_9901),
651
652   // write handlers
653   {
654      DEVCB_DRIVER_LINE_MEMBER(ti99_8, keyC0),
655      DEVCB_DRIVER_LINE_MEMBER(ti99_8, keyC1),
656      DEVCB_DRIVER_LINE_MEMBER(ti99_8, keyC2),
657      DEVCB_DRIVER_LINE_MEMBER(ti99_8, keyC3),
658      DEVCB_DRIVER_LINE_MEMBER(ti99_8, CRUS),
659      DEVCB_DRIVER_LINE_MEMBER(ti99_8, PTGEN),
660      DEVCB_DRIVER_LINE_MEMBER(ti99_8, cassette_motor),
661      DEVCB_NULL,
662      DEVCB_DRIVER_LINE_MEMBER(ti99_8, audio_gate),
663      DEVCB_DRIVER_LINE_MEMBER(ti99_8, cassette_output),
664      DEVCB_NULL,
665      DEVCB_NULL,
666      DEVCB_NULL,
667      DEVCB_NULL,
668      DEVCB_NULL,
669      DEVCB_NULL
670   },
671
672   DEVCB_DRIVER_MEMBER(ti99_8, tms9901_interrupt)
673};
674
675/*****************************************************************************/
676
677/*
678    set the state of TMS9901's INT2 (called by the tms9928 core)
679*/
680WRITE_LINE_MEMBER( ti99_8::set_tms9901_INT2 )
681{
682   if (VERBOSE>6) LOG("ti99_8: VDP int 2 on tms9901, level=%02x\n", state);
683   m_tms9901->set_single_int(2, state);
684}
685
686/***********************************************************
687    Links to external devices
688***********************************************************/
689
690
691WRITE_LINE_MEMBER( ti99_8::console_ready )
692{
693   if (VERBOSE>6) LOG("ti99_8: READY line set ... not yet connected, level=%02x\n", state);
694}
695
696WRITE_LINE_MEMBER( ti99_8::extint )
697{
698   if (VERBOSE>6) LOG("ti99_8: EXTINT level = %02x\n", state);
699   if (m_tms9901 != NULL)
700      m_tms9901->set_single_int(1, state);
701}
702
703WRITE_LINE_MEMBER( ti99_8::notconnected )
704{
705   if (VERBOSE>6) LOG("ti99_8: Setting a not connected line ... ignored\n");
706}
707
708static TMS9928A_INTERFACE(ti99_8_tms9118a_interface)
709{
517710   SCREEN_TAG,
518711   0x4000,
519   DEVCB_LINE(set_int2)
712   DEVCB_DRIVER_LINE_MEMBER(ti99_8, set_tms9901_INT2)
520713};
521714
715/*****************************************************************************/
716
522717static const struct tms9995reset_param ti99_8_processor_config =
523718{
524719   1,   /* enable automatic wait state generation */
r14513r14514
537732    care must be taken that only one device actually responds. In the case of
538733    GROMs, each chip has an internal address counter and an ID, and the chip
539734    only responds when the ID and the most significant 3 bits match.
735
736    NATIVE <-> CRUS=0
737    TI99EM <-> CRUS=1
738
739    PATGEN <-> PTGEN=1
740
741    CONT: Mapper continues iterating through devices
742    STOP: Mapper stops iterating when found
743
744    Access to the mapper registers is done directly in the mapper, not via
745    this list.
540746*/
541static const mapper8_dev_config mapper_devices[] =
747static const mapper8_list_entry mapper_devices[] =
542748{
543749   // TI-99/8 mode
544   { SRAMNAME,         0, 1, 0xf800, 0xf000, 0x0000, NULL,          NULL },
545   { "soundgen",      0, 1, 0xfff0, 0xf800, 0x0000, NULL,            sn76496_w },
546   { "video",         0, 1, 0xfffd, 0xf810, 0x0000, ti8_tms991x_rz,   ti8_tms991x_w },
547   { "speech",       0, 1, 0xfff0, 0xf820, 0x0000, ti998spch_rz,    ti998spch_w },
548   { "grom_0",        0, 0, 0xfff1, 0xf830, 0x0000, ti99grom_rz,      ti99grom_w },
549   { "grom_1",       0, 0, 0xfff1, 0xf830, 0x0000, ti99grom_rz,      ti99grom_w },
550   { "grom_2",         0, 0, 0xfff1, 0xf830, 0x0000, ti99grom_rz,     ti99grom_w },
551   { "gromport",      0, 0, 0xfff1, 0xf830, 0x0400, gromportg_rz,      gromportg_w, },
552//  { "grom_tts_0",     2, 0, 0xfff1, 0xf840, 0x0000, 0, 0, ti99grom_rz,    ti99grom_w },  // up to 6 GROMs; no good dumps known
553//  { "grom_pcode1_0",  2, 0, 0xfff1, 0xf850, 0x0000, 0, 0, ti99grom_rz,    ti99grom_w },  // up to 6 GROMs; no good dumps known
554//  { "grom_pcode2_0",  2, 0, 0xfff1, 0xf860, 0x0000, 0, 0, ti99grom_rz,    ti99grom_w },  // up to 6 GROMs; no good dumps known
555   { "mapper",       0, 1, 0xfff0, 0xf870, 0x0000, NULL,         ti99_mapreg_w },
750   { SRAMNAME,         NATIVE, STOP, 0xf000, 0xf800, 0x0000   },   // f000-f7ff
751   { TISOUND_TAG,     NATIVE, STOP, 0xf800, 0xfff0, 0x0000   },   // f800-f80f
752   { VIDEO_SYSTEM_TAG,   NATIVE, STOP, 0xf810, 0xfffd, 0x0000   },   // f810,2 (unlike 99/4A, no different read/write ports)
753   { SPEECH_TAG,      NATIVE, STOP, 0xf820, 0xfff0, 0x0000   },   // f820-f82f
556754
557   // TI-99/4A mode
558   // GROM: 1001 1000 0000 xxa0
559   { ROM0NAME,         1, 1, 0xe000, 0x0000, 0x0000, NULL,          NULL },
560   { "grom_0",        1, 0, 0xfff1, 0x9800, 0x0400, ti99grom_rz,     ti99grom_w },
561   { "grom_1",        1, 0, 0xfff1, 0x9800, 0x0400, ti99grom_rz,     ti99grom_w },
562   { "grom_2",        1, 0, 0xfff1, 0x9800, 0x0400, ti99grom_rz,     ti99grom_w },
563   { "gromport",      1, 0, 0xfff1, 0x9800, 0x0400, gromportg_rz,      gromportg_w, },
755   { GROM0_TAG,       NATIVE, CONT, 0xf830, 0xfff1, 0x0000   },   // f830-f83e (4 banks), no different read/write ports
756   { GROM1_TAG,      NATIVE, CONT, 0xf830, 0xfff1, 0x0000   },
757   { GROM2_TAG,      NATIVE, CONT, 0xf830, 0xfff1, 0x0000   },
758   { GROMPORT_TAG,    NATIVE, CONT, 0xf830, 0xfff1, 0x0000   },
564759
565   { "mapper",       1, 1, 0xfff0, 0x8810, 0x0000, NULL,            ti99_mapreg_w },
566   { "soundgen",      1, 1, 0xfff0, 0x8400, 0x0000, NULL,           sn76496_w },
567   { "video",         1, 1, 0xfffd, 0x8800, 0x0400, ti8_tms991x_rz,   ti8_tms991x_w },
568   { "speech",       1, 1, 0xfff0, 0x9000, 0x0400, ti998spch_rz,    ti998spch_w },
569   { SRAMNAME,         1, 1, 0xf800, 0x8000, 0x0000, NULL,            NULL }, // write at the end; soundgen must be found earlier
760//  { GROMTTS0_TAG,     PATGEN, CONT, 0xf840, 0xfff1, 0x0000    },  // up to 6 GROMs; no good dumps known
761//  { GROMPC10_TAG,     PATGEN, CONT, 0xf850, 0xfff1, 0x0000    },  // up to 6 GROMs; no good dumps known
762//  { GROMPC20_TAG,     PATGEN, CONT, 0xf860, 0xfff1, 0x0000    },  // up to 6 GROMs; no good dumps known
763   { MAPPER_TAG,      NATIVE, STOP, 0xf870, 0xfff0, 0x0000   },
570764
571   // Physical (need to pack this into here as well)
572   { DRAMNAME,       3, 1, 0xff0000, 0x000000, 0x000000, NULL, NULL },
573   { "mapper",         3, 0, 0xffe000, 0xff4000, 0x000000, ti998dsr_rz, NULL },   // DSR
574   { "gromport",      3, 1, 0xffe000, 0xff6000, 0x000000, gromportr_rz, gromportr_w  },
575   { "gromport",      3, 1, 0xffe000, 0xff8000, 0x000000, gromportr_rz, gromportr_w  },
576   { ROM1NAME,       3, 1, 0xffe000, 0xffa000, 0x000000, NULL, NULL },
577   { ROM1ANAME,      3, 1, 0xffe000, 0xffc000, 0x000000, NULL, NULL },
578   { INTSNAME,       3, 1, 0xfffff0, 0xffe000, 0x000000, NULL, NULL },
579   { "peribox",      3, 1, 0x000000, 0x000000, 0x000000, ti99_peb_data_rz, ti99_peb_data_w },
765   // TI-99/4A mode (CRUS=1)
766   // GROMs: According to the spec, the 99/8 supports up to 4 GROM libraries
767   // (99/4A supports 256 libraries)
768   // at 9800, 9804, 9808, 980c. Address counter access is at 9802,6,a,e. Write access +0400.
769   { ROM0NAME,         TI99EM, STOP, 0x0000, 0xe000, 0x0000   },   // 0000-1fff
580770
581   { NULL, 0, 0, 0, 0, 0, NULL, NULL  }
771   { GROM0_TAG,       TI99EM, CONT, 0x9800, 0xfff1, 0x0400   },   // 9800,2,4,...e/9c00,2,4,...e
772   { GROM1_TAG,       TI99EM, CONT, 0x9800, 0xfff1, 0x0400   },   // dto.
773   { GROM2_TAG,       TI99EM, CONT, 0x9800, 0xfff1, 0x0400   },   // dto. (GROMs are connected in parallel,
774   { GROMPORT_TAG,    TI99EM, CONT, 0x9800, 0xfff1, 0x0400   },   // dto.  use internal address counter and id)
775
776   { TISOUND_TAG,     TI99EM, STOP, 0x8400, 0xfff0, 0x0000   },   // 8400-840f
777   { VIDEO_SYSTEM_TAG,   TI99EM, STOP, 0x8800, 0xfffd, 0x0400   },   // 8800,8802 / 8c00,8c02
778   { SPEECH_TAG,      TI99EM, STOP, 0x9000, 0xfff0, 0x0400   },   // 9000-900f / 9400-940f
779   { SRAMNAME,         TI99EM, STOP, 0x8000, 0xf800, 0x0000   },   // 8000-87ff; must follow the sound generator
780   { MAPPER_TAG,      TI99EM, STOP, 0x8810, 0xfff0, 0x0000   },
781
782   // Physical (need to pack this in here as well to keep config simple)
783   { DRAMNAME,       PHYSIC, STOP, 0x000000, 0xff0000, 0x000000   },   // 000000-00ffff 64 KiB DRAM
784   { MAPPER_TAG,      PHYSIC, CONT, 0xff4000, 0xffe000, 0x000000   },   // ff4000-ff5fff Internal DSR
785   { GROMPORT_TAG,      PHYSIC, STOP, 0xff6000, 0xffe000, 0x000000   },   // ff6000-ff7fff Cartridge ROM space
786   { GROMPORT_TAG,      PHYSIC, STOP, 0xff8000, 0xffe000, 0x000000   },   // ff8000-ff9fff Cartridge ROM space
787   { ROM1NAME,       PHYSIC, STOP, 0xffa000, 0xffe000, 0x000000   },   // ffa000-ffbfff ROM1
788   { ROM1ANAME,      PHYSIC, STOP, 0xffc000, 0xffe000, 0x000000   },   // ffc000-ffdfff ROM1
789   { INTSNAME,       PHYSIC, STOP, 0xffe000, 0xfffff0, 0x000000   },   // ffe000-ffe00f Interrupt level sense
790   { PERIBOX_TAG,      PHYSIC, STOP, 0x000000, 0x000000, 0x000000   },   // Peripheral Expansion Box
791
792   { NULL, 0, 0, 0, 0, 0  }
582793};
583794
584static const cruconf_device cru_devices[] =
795MACHINE_START( ti99_8 )
585796{
586   { "mapper",    0xfff0, 0x2700, NULL, mapper8c_w },
587   { "gromport",   0xf800, 0x0800, gromportc_rz, gromportc_w },            // Check: Is this really limited?
588   { "peribox",   0x0000, 0x0000, ti99_peb_cru_rz, ti99_peb_cru_w },    // Peribox needs all addresses
589   { NULL,       0, 0, NULL, NULL }
590};
797   ti99_8 *driver = machine.driver_data<ti99_8>();
798   driver->m_cpu = machine.device("maincpu");
799   driver->m_tms9901 = static_cast<tms9901_device*>(machine.device(TMS9901_TAG));
800   driver->m_gromport = static_cast<gromport_device*>(machine.device(GROMPORT_TAG));
801   driver->m_peribox = static_cast<peribox_device*>(machine.device(PERIBOX_TAG));
802   driver->m_mapper = static_cast<ti998_mapper_device*>(machine.device(MAPPER_TAG));
803   driver->m_peribox->senila(CLEAR_LINE);
804   driver->m_peribox->senilb(CLEAR_LINE);
805}
591806
592807MACHINE_RESET( ti99_8 )
593808{
809   ti99_8 *driver = machine.driver_data<ti99_8>();
810
811   if (input_port_read(machine, "MECMOUSE")==0x01)
812      driver->m_mecmouse = static_cast<mecmouse_device*>(machine.device(MECMOUSE_TAG));
813   else
814      driver->m_mecmouse = NULL;
594815}
595816
596static MACHINE_CONFIG_START( ti99_8_60hz, ti99_8_state )
817static MACHINE_CONFIG_START( ti99_8_60hz, ti99_8 )
597818   /* basic machine hardware */
598819   /* TMS9995-MP9537 CPU @ 10.7 MHz */
599820   MCFG_CPU_ADD("maincpu", TMS9995, 10738635)
600821   MCFG_CPU_CONFIG(ti99_8_processor_config)
601   MCFG_CPU_PROGRAM_MAP(ti99_8_memmap)
602   MCFG_CPU_IO_MAP(ti99_8_cru_map)
603   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
604
822   MCFG_CPU_PROGRAM_MAP(memmap)
823   MCFG_CPU_IO_MAP(crumap)
824   MCFG_MACHINE_START( ti99_8 )
605825   MCFG_MACHINE_RESET( ti99_8 )
606826
607   /* video hardware */
608   MCFG_TI_TMS991x_ADD_NTSC("video", TMS9118, ti99_8_tms9928a_interface)
827   /* Video hardware */
828   MCFG_TI998_ADD_NTSC(VIDEO_SYSTEM_TAG, TMS9118, ti99_8_tms9118a_interface)
609829
610   MCFG_GFXDECODE(ti99_8)
830   /* Main board */
831   MCFG_TMS9901_ADD( TMS9901_TAG, tms9901_wiring_ti99_8, 2684658.75 )
832   MCFG_MAPPER8_ADD( MAPPER_TAG, mapper_devices )
833   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
611834
612   /* sound hardware */
835   /* Peripheral expansion box */
836   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
837
838   /* Sound hardware */
613839   MCFG_SPEAKER_STANDARD_MONO("mono")
840   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN76496, 3579545) /* 3.579545 MHz */
841   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
842   MCFG_TI_SOUND_ADD( TISOUND_TAG )
843
844   /* Cassette drives */
845   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
614846   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
615847   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
616   MCFG_SOUND_ADD("soundgen", SN76496, 3579545)   /* 3.579545 MHz */
617   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
618848
619   MCFG_TISPEECH_ADD("speech")
849   /* Console GROMs */
850   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
851   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
852   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
620853
621   MCFG_TI998_BOARD_ADD( "ti_board" )
622   MCFG_MAPPER8_ADD( "mapper", mapper_devices )
854   /* Devices */
855   MCFG_TISPEECH8_ADD(SPEECH_TAG)
623856
624   /* tms9901 */
625   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_8)
626
627   MCFG_CRUBUS_ADD( "crubus", cru_devices)
628
629   MCFG_GROM_ADD( "grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
630   MCFG_GROM_ADD( "grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
631   MCFG_GROM_ADD( "grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
632
633   MCFG_PBOX8_ADD( "peribox", console_extint, console_notconnected, console_ready )
634   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
635
636   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
637   MCFG_MECMOUSE_ADD( "mecmouse" )
857   /* Optional user controller */
858   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 60 )
638859MACHINE_CONFIG_END
639860
640861
641static MACHINE_CONFIG_START( ti99_8_50hz, ti99_8_state )
862static MACHINE_CONFIG_START( ti99_8_50hz, ti99_8 )
642863   /* basic machine hardware */
643864   /* TMS9995-MP9537 CPU @ 10.7 MHz */
644865   MCFG_CPU_ADD("maincpu", TMS9995, 10738635)
645866   MCFG_CPU_CONFIG(ti99_8_processor_config)
646   MCFG_CPU_PROGRAM_MAP(ti99_8_memmap)
647   MCFG_CPU_IO_MAP(ti99_8_cru_map)
648   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
649
867   MCFG_CPU_PROGRAM_MAP(memmap)
868   MCFG_CPU_IO_MAP(crumap)
869   MCFG_MACHINE_START( ti99_8 )
650870   MCFG_MACHINE_RESET( ti99_8 )
651871
652   /* video hardware */
653   MCFG_TI_TMS991x_ADD_PAL("video", TMS9129, ti99_8, ti99_8_tms9928a_interface)
872   /* Video hardware */
873   MCFG_TI998_ADD_PAL(VIDEO_SYSTEM_TAG, TMS9129, ti99_8_tms9118a_interface)
654874
655   MCFG_GFXDECODE(ti99_8)
875   /* Main board */
876   MCFG_TMS9901_ADD( TMS9901_TAG, tms9901_wiring_ti99_8, 2684658.75 )
877   MCFG_MAPPER8_ADD( MAPPER_TAG, mapper_devices )
878   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
656879
657   /* sound hardware */
880   /* Peripheral expansion box */
881   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
882
883   /* Sound hardware */
658884   MCFG_SPEAKER_STANDARD_MONO("mono")
885   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN76496, 3579545)   /* 3.579545 MHz */
886   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
887   MCFG_TI_SOUND_ADD( TISOUND_TAG )
888
889   /* Cassette drives */
890   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
659891   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
660892   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
661   MCFG_SOUND_ADD("soundgen", SN76496, 3579545)   /* 3.579545 MHz */
662   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
663893
664   MCFG_TISPEECH_ADD("speech")
894   /* Console GROMs */
895   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
896   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
897   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
665898
666   MCFG_TI998_BOARD_ADD( "ti_board" )
667   MCFG_MAPPER8_ADD( "mapper", mapper_devices )
899   /* Devices */
900   MCFG_TISPEECH8_ADD(SPEECH_TAG)
668901
669   /* tms9901 */
670   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_8)
671
672   MCFG_CRUBUS_ADD( "crubus", cru_devices)
673
674   MCFG_GROM_ADD( "grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
675   MCFG_GROM_ADD( "grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
676   MCFG_GROM_ADD( "grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
677
678   MCFG_PBOX8_ADD( "peribox", console_extint, console_notconnected, console_ready )
679   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
680
681   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
682   MCFG_MECMOUSE_ADD( "mecmouse" )
902   /* Optional user controller */
903   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 50 )
683904MACHINE_CONFIG_END
684905
685906/*
r14513r14514
694915   ROM_REGION(0x10000, region_grom, 0)
695916   ROM_LOAD("998grom.bin", 0x0000, 0x6000, CRC(c63806bc) SHA1(cbfa8b04b4aefbbd9a713c54267ad4dd179c13a3))   /* system GROMs */
696917
918   /* Built-in RAM */
919   ROM_REGION(SRAM_SIZE, SRAM_TAG, 0)
920   ROM_FILL(0x0000, SRAM_SIZE, 0x00)
921
922   ROM_REGION(DRAM_SIZE, DRAM_TAG, 0)
923   ROM_FILL(0x0000, DRAM_SIZE, 0x00)
697924ROM_END
698925
699926#define rom_ti99_8e rom_ti99_8
src/mess/drivers/tm990189.c
r14513r14514
106106   UINT8 m_rs232_rts;
107107   emu_timer *m_rs232_input_timer;
108108   UINT8 m_bogus_read_save;
109};
110109
110   DECLARE_WRITE_LINE_MEMBER(usr9901_led0_w);
111   DECLARE_WRITE_LINE_MEMBER(usr9901_led1_w);
112   DECLARE_WRITE_LINE_MEMBER(usr9901_led2_w);
113   DECLARE_WRITE_LINE_MEMBER(usr9901_led3_w);
114   DECLARE_WRITE8_MEMBER(usr9901_interrupt_callback);
111115
116   DECLARE_WRITE8_MEMBER(sys9901_interrupt_callback);
117   DECLARE_READ8_MEMBER(sys9901_r);
118   DECLARE_WRITE_LINE_MEMBER(sys9901_digitsel0_w);
119   DECLARE_WRITE_LINE_MEMBER(sys9901_digitsel1_w);
120   DECLARE_WRITE_LINE_MEMBER(sys9901_digitsel2_w);
121   DECLARE_WRITE_LINE_MEMBER(sys9901_digitsel3_w);
112122
123   DECLARE_WRITE_LINE_MEMBER(sys9901_segment0_w);
124   DECLARE_WRITE_LINE_MEMBER(sys9901_segment1_w);
125   DECLARE_WRITE_LINE_MEMBER(sys9901_segment2_w);
126   DECLARE_WRITE_LINE_MEMBER(sys9901_segment3_w);
127   DECLARE_WRITE_LINE_MEMBER(sys9901_segment4_w);
128   DECLARE_WRITE_LINE_MEMBER(sys9901_segment5_w);
129   DECLARE_WRITE_LINE_MEMBER(sys9901_segment6_w);
130   DECLARE_WRITE_LINE_MEMBER(sys9901_segment7_w);
113131
132   DECLARE_WRITE_LINE_MEMBER(sys9901_dsplytrgr_w);
133   DECLARE_WRITE_LINE_MEMBER(sys9901_shiftlight_w);
134   DECLARE_WRITE_LINE_MEMBER(sys9901_spkrdrive_w);
135   DECLARE_WRITE_LINE_MEMBER(sys9901_tapewdata_w);
136
137   DECLARE_WRITE8_MEMBER( xmit_callback );
138
139private:
140   void draw_digit(void);
141   void led_set(int number, bool state);
142   void segment_set(int offset, bool state);
143   void digitsel(int offset, bool state);
144};
145
146
114147#define displayena_duration attotime::from_usec(4500)   /* Can anyone confirm this? 74LS123 connected to C=0.1uF and R=100kOhm */
115148
116149
r14513r14514
194227    Supports EIA and TTY terminals, and an optional 9918 controller.
195228*/
196229
197INLINE void draw_digit(tm990189_state *state)
230void tm990189_state::draw_digit()
198231{
199   state->m_segment_state[state->m_digitsel] |= ~state->m_segment;
232   m_segment_state[m_digitsel] |= ~m_segment;
200233}
201234
202235
r14513r14514
225258   }
226259}
227260
228
229261/*
230262    tms9901 code
231263*/
232264
233static TMS9901_INT_CALLBACK ( usr9901_interrupt_callback )
265WRITE8_MEMBER( tm990189_state::usr9901_interrupt_callback )
234266{
235   tm990189_state *state = device->machine().driver_data<tm990189_state>();
236   state->m_ic_state = ic & 7;
237
238   if (!state->m_load_state)
239      field_interrupts(device->machine());
267   m_ic_state = offset & 7;  // offset = IC0..IC3 (interrupt level)
268   if (!m_load_state)
269      field_interrupts(machine());
240270}
241271
242static WRITE8_DEVICE_HANDLER( usr9901_led_w )
272void tm990189_state::led_set(int offset, bool state)
243273{
244   tm990189_state *state = device->machine().driver_data<tm990189_state>();
245   if (data)
246      state->m_LED_state |= (1 << offset);
274   if (state)
275      m_LED_state |= (1 << offset);
247276   else
248      state->m_LED_state &= ~(1 << offset);
277      m_LED_state &= ~(1 << offset);
249278}
250279
251static TMS9901_INT_CALLBACK( sys9901_interrupt_callback )
280WRITE_LINE_MEMBER( tm990189_state::usr9901_led0_w )
252281{
253   (void)ic;
282   led_set(0, state);
283}
254284
255   tms9901_set_single_int(device->machine().device("tms9901_0"), 5, intreq);
285WRITE_LINE_MEMBER( tm990189_state::usr9901_led1_w )
286{
287   led_set(1, state);
256288}
257289
258static READ8_DEVICE_HANDLER( sys9901_r0 )
290WRITE_LINE_MEMBER( tm990189_state::usr9901_led2_w )
259291{
260   tm990189_state *state = device->machine().driver_data<tm990189_state>();
292   led_set(2, state);
293}
294
295WRITE_LINE_MEMBER( tm990189_state::usr9901_led3_w )
296{
297   led_set(3, state);
298}
299
300WRITE8_MEMBER( tm990189_state::sys9901_interrupt_callback )
301{
302   machine().device<tms9901_device>("tms9901_0")->set_single_int(5, (data!=0)? ASSERT_LINE:CLEAR_LINE);
303}
304
305READ8_MEMBER( tm990189_state::sys9901_r )
306{
261307   UINT8 data = 0;
262   static const char *const keynames[] = { "LINE0", "LINE1", "LINE2", "LINE3", "LINE4", "LINE5", "LINE6", "LINE7", "LINE8" };
308   if (offset == TMS9901_CB_INT7)
309   {
310      static const char *const keynames[] = { "LINE0", "LINE1", "LINE2", "LINE3", "LINE4", "LINE5", "LINE6", "LINE7", "LINE8" };
263311
264   /* keyboard read */
265   if (state->m_digitsel < 9)
266      data |= input_port_read(device->machine(), keynames[state->m_digitsel]) << 1;
312      /* keyboard read */
313      if (m_digitsel < 9)
314         data |= input_port_read(machine(), keynames[m_digitsel]) << 1;
267315
268   /* tape input */
269   if (state->m_cass->input() > 0.0)
270      data |= 0x40;
316      /* tape input */
317      if (m_cass->input() > 0.0)
318         data |= 0x40;
319   }
271320
272321   return data;
273322}
274323
275static WRITE8_DEVICE_HANDLER( sys9901_digitsel_w )
324void tm990189_state::digitsel(int offset, bool state)
276325{
277   tm990189_state *state = device->machine().driver_data<tm990189_state>();
278   if (data)
279      state->m_digitsel |= 1 << offset;
326   if (state)
327      m_digitsel |= 1 << offset;
280328   else
281      state->m_digitsel &= ~ (1 << offset);
329      m_digitsel &= ~ (1 << offset);
282330}
283331
284static WRITE8_DEVICE_HANDLER( sys9901_segment_w )
332WRITE_LINE_MEMBER( tm990189_state::sys9901_digitsel0_w )
285333{
286   tm990189_state *state = device->machine().driver_data<tm990189_state>();
287   if (data)
288      state->m_segment |= 1 << (offset-4);
334   digitsel(0, state);
335}
336WRITE_LINE_MEMBER( tm990189_state::sys9901_digitsel1_w )
337{
338   digitsel(1, state);
339}
340WRITE_LINE_MEMBER( tm990189_state::sys9901_digitsel2_w )
341{
342   digitsel(2, state);
343}
344WRITE_LINE_MEMBER( tm990189_state::sys9901_digitsel3_w )
345{
346   digitsel(3, state);
347}
348
349
350void tm990189_state::segment_set(int offset, bool state)
351{
352   if (state)
353      m_segment |= 1 << offset;
289354   else
290355   {
291      state->m_segment &= ~ (1 << (offset-4));
292      if ((state->m_displayena_timer->remaining() > attotime::zero)  && (state->m_digitsel < 10))
293         draw_digit(state);
356      m_segment &= ~ (1 << offset);
357      if ((m_displayena_timer->remaining() > attotime::zero)  && (m_digitsel < 10))
358         draw_digit();
294359   }
295360}
296361
297static WRITE8_DEVICE_HANDLER( sys9901_dsplytrgr_w )
362WRITE_LINE_MEMBER( tm990189_state::sys9901_segment0_w )
298363{
299   tm990189_state *state = device->machine().driver_data<tm990189_state>();
300   if ((!data) && (state->m_digitsel < 10))
364   segment_set(0, state);
365}
366WRITE_LINE_MEMBER( tm990189_state::sys9901_segment1_w )
367{
368   segment_set(1, state);
369}
370WRITE_LINE_MEMBER( tm990189_state::sys9901_segment2_w )
371{
372   segment_set(2, state);
373}
374WRITE_LINE_MEMBER( tm990189_state::sys9901_segment3_w )
375{
376   segment_set(3, state);
377}
378WRITE_LINE_MEMBER( tm990189_state::sys9901_segment4_w )
379{
380   segment_set(4, state);
381}
382WRITE_LINE_MEMBER( tm990189_state::sys9901_segment5_w )
383{
384   segment_set(5, state);
385}
386WRITE_LINE_MEMBER( tm990189_state::sys9901_segment6_w )
387{
388   segment_set(6, state);
389}
390WRITE_LINE_MEMBER( tm990189_state::sys9901_segment7_w )
391{
392   segment_set(7, state);
393}
394
395WRITE_LINE_MEMBER( tm990189_state::sys9901_dsplytrgr_w )
396{
397   if ((!state) && (m_digitsel < 10))
301398   {
302      state->m_displayena_timer->reset(displayena_duration);
303      draw_digit(state);
399      m_displayena_timer->reset(displayena_duration);
400      draw_digit();
304401   }
305402}
306403
307static WRITE8_DEVICE_HANDLER( sys9901_shiftlight_w )
404WRITE_LINE_MEMBER( tm990189_state::sys9901_shiftlight_w )
308405{
309   tm990189_state *state = device->machine().driver_data<tm990189_state>();
310   if (data)
311      state->m_LED_state |= 0x10;
406   if (state)
407      m_LED_state |= 0x10;
312408   else
313      state->m_LED_state &= ~0x10;
409      m_LED_state &= ~0x10;
314410}
315411
316static WRITE8_DEVICE_HANDLER( sys9901_spkrdrive_w )
412WRITE_LINE_MEMBER( tm990189_state::sys9901_spkrdrive_w )
317413{
318   device_t *speaker = device->machine().device(SPEAKER_TAG);
319   speaker_level_w(speaker, data);
414   device_t *speaker = machine().device(SPEAKER_TAG);
415   speaker_level_w(speaker, state);
320416}
321417
322static WRITE8_DEVICE_HANDLER( sys9901_tapewdata_w )
418WRITE_LINE_MEMBER( tm990189_state::sys9901_tapewdata_w )
323419{
324   device->machine().device<cassette_image_device>(CASSETTE_TAG)->output(data ? +1.0 : -1.0);
420   machine().device<cassette_image_device>(CASSETTE_TAG)->output(state ? +1.0 : -1.0);
325421}
326422
327423/*
r14513r14514
335431   device_image_interface *image = (device_image_interface *)(ptr);
336432   if (/*state->m_rs232_rts &&*/ /*(mame_ftell(state->m_rs232_fp) < mame_fsize(state->m_rs232_fp))*/1)
337433   {
434      tms9902_device* tms9902 = static_cast<tms9902_device*>(machine.device("tms9902"));
338435      if (image->fread(&buf, 1) == 1)
339         tms9902_rcv_data(machine.device("tms9902"), buf);
436         tms9902->rcv_data(buf);
340437   }
341438}
342439
r14513r14514
346443static DEVICE_IMAGE_LOAD( tm990_189_rs232 )
347444{
348445   tm990189_state *state = image.device().machine().driver_data<tm990189_state>();
349   tms9902_rcv_dsr(image.device().machine().device("tms9902"), ASSERT_LINE);
446   tms9902_device* tms9902 = static_cast<tms9902_device*>(image.device().machine().device("tms9902"));
447   tms9902->rcv_dsr(ASSERT_LINE);
350448   state->m_rs232_input_timer = image.device().machine().scheduler().timer_alloc(FUNC(rs232_input_callback), (void*)image);
351449   state->m_rs232_input_timer->adjust(attotime::zero, 0, attotime::from_msec(10));
352450   return IMAGE_INIT_PASS;
r14513r14514
358456static DEVICE_IMAGE_UNLOAD( tm990_189_rs232 )
359457{
360458   tm990189_state *state = image.device().machine().driver_data<tm990189_state>();
361   tms9902_rcv_dsr(image.device().machine().device("tms9902"), CLEAR_LINE);
459   tms9902_device* tms9902 = static_cast<tms9902_device*>(image.device().machine().device("tms9902"));
460   tms9902->rcv_dsr(CLEAR_LINE);
362461
363462   state->m_rs232_input_timer->reset();   /* FIXME - timers should only be allocated once */
364463}
r14513r14514
406505{
407506    tm990189_state *state = device->machine().driver_data<tm990189_state>();
408507    state->m_rs232_rts = RTS;
409    tms9902_set_cts(device, RTS);
508    tms9902->set_cts(RTS);
410509} */
411510
412static TMS9902_XMIT_CALLBACK( xmit_callback )
511WRITE8_MEMBER( tm990189_state::xmit_callback )
413512{
414   tm990189_state *state = device->machine().driver_data<tm990189_state>();
415513   UINT8 buf = data;
416
417   if (state->m_rs232_fp)
418      state->m_rs232_fp->fwrite(&buf, 1);
514   if (m_rs232_fp)   m_rs232_fp->fwrite(&buf, 1);
419515}
420516
421517/*
422518    External instruction decoding
423519*/
424
425520WRITE8_MEMBER( tm990189_state::ext_instr_decode )
426521{
427522   int ext_op_ID;
r14513r14514
540635{
541636   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INT3 | TMS9901_INT4 | TMS9901_INT5 | TMS9901_INT6,   /* only input pins whose state is always known */
542637
543   {   /* read handlers */
544      NULL
545      /*usr9901_r0,
546        usr9901_r1,
547        usr9901_r2,
548        usr9901_r3*/
549   },
638   /* Read handler. Covers all input lines (see tms9901.h) */
639   DEVCB_NULL,
550640
551   {   /* write handlers */
552      usr9901_led_w,
553      usr9901_led_w,
554      usr9901_led_w,
555      usr9901_led_w/*,
556        usr9901_w,
557        usr9901_w,
558        usr9901_w,
559        usr9901_w,
560        usr9901_w,
561        usr9901_w,
562        usr9901_w,
563        usr9901_w,
564        usr9901_w,
565        usr9901_w,
566        usr9901_w,
567        usr9901_w*/
641   /* write handlers */
642   {
643      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, usr9901_led0_w),
644      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, usr9901_led1_w),
645      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, usr9901_led2_w),
646      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, usr9901_led3_w),
647      DEVCB_NULL,
648      DEVCB_NULL,
649      DEVCB_NULL,
650      DEVCB_NULL,
651      DEVCB_NULL,
652      DEVCB_NULL,
653      DEVCB_NULL,
654      DEVCB_NULL,
655      DEVCB_NULL,
656      DEVCB_NULL,
657      DEVCB_NULL,
658      DEVCB_NULL
568659   },
569660
570661   /* interrupt handler */
571   usr9901_interrupt_callback,
572
573   /* clock rate = 2MHz */
574   2000000.
662   DEVCB_DRIVER_MEMBER(tm990189_state, usr9901_interrupt_callback)
575663};
576664
577665/* system tms9901 setup */
r14513r14514
579667{
580668   0,   /* only input pins whose state is always known */
581669
582   {   /* read handlers */
583      sys9901_r0,
584      NULL,
585      NULL,
586      NULL
587   },
670   /* Read handler. Covers all input lines (see tms9901.h) */
671   DEVCB_DRIVER_MEMBER(tm990189_state, sys9901_r),
588672
589   {   /* write handlers */
590      sys9901_digitsel_w,
591      sys9901_digitsel_w,
592      sys9901_digitsel_w,
593      sys9901_digitsel_w,
594      sys9901_segment_w,
595      sys9901_segment_w,
596      sys9901_segment_w,
597      sys9901_segment_w,
598      sys9901_segment_w,
599      sys9901_segment_w,
600      sys9901_segment_w,
601      sys9901_segment_w,
602      sys9901_dsplytrgr_w,
603      sys9901_shiftlight_w,
604      sys9901_spkrdrive_w,
605      sys9901_tapewdata_w
673   /* write handlers */
674   {
675      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_digitsel0_w),
676      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_digitsel1_w),
677      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_digitsel2_w),
678      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_digitsel3_w),
679      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment0_w),
680      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment1_w),
681      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment2_w),
682      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment3_w),
683      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment4_w),
684      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment5_w),
685      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment6_w),
686      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_segment7_w),
687      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_dsplytrgr_w),
688      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_shiftlight_w),
689      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_spkrdrive_w),
690      DEVCB_DRIVER_LINE_MEMBER(tm990189_state, sys9901_tapewdata_w)
606691   },
607692
608693   /* interrupt handler */
609   sys9901_interrupt_callback,
610
611   /* clock rate = 2MHz */
612   2000000.
694   DEVCB_DRIVER_MEMBER(tm990189_state, sys9901_interrupt_callback)
613695};
614696
615697/*
r14513r14514
642724// MZ: needs to be fixed once the RS232 support is complete
643725static const tms9902_interface tms9902_params =
644726{
645   2000000.,            /* clock rate (2MHz) */
646   NULL,/*int_callback,*/   /* called when interrupt pin state changes */
647   NULL,/*rcv_callback,*/   /* called when a character shall be received  */
648   xmit_callback,         /* called when a character is transmitted */
649   NULL               /* called for setting interface parameters and line states */
727   DEVCB_NULL,            /*int_callback,*/   /* called when interrupt pin state changes */
728   DEVCB_NULL,            /*rcv_callback,*/   /* called when a character shall be received  */
729   DEVCB_DRIVER_MEMBER(tm990189_state, xmit_callback),         /* called when a character is transmitted */
730   DEVCB_NULL            /* called for setting interface parameters and line states */
650731};
651732
652733static ADDRESS_MAP_START( tm990_189_memmap, AS_PROGRAM, 8, tm990189_state )
r14513r14514
723804*/
724805
725806static ADDRESS_MAP_START( tm990_189_cru_map, AS_IO, 8, tm990189_state )
726   AM_RANGE(0x0000, 0x003f) AM_DEVREAD_LEGACY("tms9901_0", tms9901_cru_r)      /* user I/O tms9901 */
727   AM_RANGE(0x0040, 0x006f) AM_DEVREAD_LEGACY("tms9901_1", tms9901_cru_r)      /* system I/O tms9901 */
728   AM_RANGE(0x0080, 0x00cf) AM_DEVREAD_LEGACY("tms9902", tms9902_cru_r)      /* optional tms9902 */
807   AM_RANGE(0x0000, 0x003f) AM_DEVREAD("tms9901_0", tms9901_device, read)      /* user I/O tms9901 */
808   AM_RANGE(0x0040, 0x006f) AM_DEVREAD("tms9901_1", tms9901_device, read)      /* system I/O tms9901 */
809   AM_RANGE(0x0080, 0x00cf) AM_DEVREAD("tms9902", tms9902_device, cruread)      /* optional tms9902 */
729810
730   AM_RANGE(0x0000, 0x01ff) AM_DEVWRITE_LEGACY("tms9901_0", tms9901_cru_w)   /* user I/O tms9901 */
731   AM_RANGE(0x0200, 0x03ff) AM_DEVWRITE_LEGACY("tms9901_1", tms9901_cru_w)   /* system I/O tms9901 */
732   AM_RANGE(0x0400, 0x05ff) AM_DEVWRITE_LEGACY("tms9902", tms9902_cru_w)   /* optional tms9902 */
811   AM_RANGE(0x0000, 0x01ff) AM_DEVWRITE("tms9901_0", tms9901_device, write)   /* user I/O tms9901 */
812   AM_RANGE(0x0200, 0x03ff) AM_DEVWRITE("tms9901_1", tms9901_device, write)   /* system I/O tms9901 */
813   AM_RANGE(0x0400, 0x05ff) AM_DEVWRITE("tms9902", tms9902_device, cruwrite)   /* optional tms9902 */
733814
734815   AM_RANGE(0x0800, 0x1fff) AM_WRITE(ext_instr_decode)   /* external instruction decoding (IDLE, RSET, CKON, CKOF, LREX) */
735816ADDRESS_MAP_END
r14513r14514
762843
763844   /* Devices */
764845   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
765   MCFG_TMS9901_ADD("tms9901_0", usr9901reset_param)
766   MCFG_TMS9901_ADD("tms9901_1", sys9901reset_param)
767   MCFG_TMS9902_ADD("tms9902", tms9902_params)
846   MCFG_TMS9901_ADD("tms9901_0", usr9901reset_param, 2000000)
847   MCFG_TMS9901_ADD("tms9901_1", sys9901reset_param, 2000000)
848   MCFG_TMS9902_ADD("tms9902", tms9902_params, 2000000)
768849   MCFG_TM990_189_RS232_ADD("rs232")
769850   MCFG_TIMER_ADD_PERIODIC("display_timer", display_callback, attotime::from_hz(30))
770851MACHINE_CONFIG_END
r14513r14514
794875
795876   /* Devices */
796877   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
797   MCFG_TMS9901_ADD("tms9901_0", usr9901reset_param)
798   MCFG_TMS9901_ADD("tms9901_1", sys9901reset_param)
799   MCFG_TMS9902_ADD("tms9902", tms9902_params)
878   MCFG_TMS9901_ADD("tms9901_0", usr9901reset_param, 2000000)
879   MCFG_TMS9901_ADD("tms9901_1", sys9901reset_param, 2000000)
880   MCFG_TMS9902_ADD("tms9902", tms9902_params,   2000000)
800881   MCFG_TM990_189_RS232_ADD("rs232")
801882   MCFG_TIMER_ADD_PERIODIC("display_timer", display_callback, attotime::from_hz(30))
802883MACHINE_CONFIG_END
src/mess/drivers/geneve.c
r14513r14514
197197    keyboard will be cleared as soon as it is buffered by the gate array.
198198*/
199199
200#define ADDRESS_MAP_MODERN
201
200202#include "emu.h"
201203#include "cpu/tms9900/tms9900.h"
202#include "machine/ti99/genboard.h"
203204#include "machine/tms9901.h"
204#include "machine/ti99/videowrp.h"
205#include "machine/mm58274c.h"
205206#include "sound/sn76496.h"
207
208#include "machine/ti99/genboard.h"
206209#include "machine/ti99/peribox.h"
210#include "machine/ti99/videowrp.h"
207211
212#define VERBOSE 1
213#define LOG logerror
208214
209class geneve_state : public driver_device
215#define SRAM_SIZE 384*1024   // maximum SRAM expansion on-board
216#define DRAM_SIZE 512*1024
217
218class geneve : public driver_device
210219{
211220public:
212   geneve_state(const machine_config &mconfig, device_type type, const char *tag)
221   geneve(const machine_config &mconfig, device_type type, const char *tag)
213222      : driver_device(mconfig, type, tag) { }
214223
224   // CRU (Communication Register Unit) handling
225   DECLARE_READ8_MEMBER(cruread);
226   DECLARE_WRITE8_MEMBER(cruwrite);
227
228   // Connections with the system interface TMS9901
229   DECLARE_READ8_MEMBER(read_by_9901);
230   DECLARE_WRITE_LINE_MEMBER(peripheral_bus_reset);
231   DECLARE_WRITE_LINE_MEMBER(VDP_reset);
232   DECLARE_WRITE_LINE_MEMBER(joystick_select);
233   DECLARE_WRITE_LINE_MEMBER(extbus_wait_states);
234   DECLARE_WRITE_LINE_MEMBER(video_wait_states);
235   DECLARE_WRITE8_MEMBER(tms9901_interrupt);
236
237   WRITE_LINE_MEMBER( keyboard_interrupt );
238
239   geneve_keyboard_device*   m_keyboard;
240   geneve_mouse_device*   m_mouse;
241   tms9901_device*         m_tms9901;
242   geneve_mapper_device*   m_mapper;
243   peribox_device*         m_peribox;
244   device_t*            m_cpu;
245
246   WRITE_LINE_MEMBER( inta );
247   WRITE_LINE_MEMBER( intb );
248   WRITE_LINE_MEMBER( ready );
249
250   void   set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state);
251
252   line_state   m_inta;
253   line_state   m_intb;
254   line_state   m_int2;
255   line_state   m_keyint;
256   line_state   m_video_wait; // reflects the line to the mapper for CRU query
257private:
258   int      m_joystick_select;
259   // Some values to keep. Rest is on the geneve_mapper.
215260};
216261
217262
218263/*
219    memory map
264    Memory map
220265*/
221266
222static ADDRESS_MAP_START(memmap, AS_PROGRAM, 8)
223   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE("geneve_board", geneve_r, geneve_w)
267static ADDRESS_MAP_START(memmap, AS_PROGRAM, 8, geneve)
268   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE(GMAPPER_TAG, geneve_mapper_device, readm, writem)
224269ADDRESS_MAP_END
225270
226271/*
r14513r14514
231276    TODO: Check whether A0-A2 are available for CRU addressing since those
232277    bits are usually routed through the mapper first.
233278*/
234static ADDRESS_MAP_START(cru_map, AS_IO, 8)
235   AM_RANGE(0x0000, 0x0003) AM_DEVREAD("tms9901", tms9901_cru_r)
236   AM_RANGE(0x0000, 0x0fff) AM_DEVREAD("geneve_board", geneve_cru_r)
279static ADDRESS_MAP_START(crumap, AS_IO, 8, geneve)
280   AM_RANGE(0x0000, 0x0003) AM_DEVREAD(TMS9901_TAG, tms9901_device, read)
281   AM_RANGE(0x0000, 0x0fff) AM_READ( cruread )
237282
238   AM_RANGE(0x0000, 0x001f) AM_DEVWRITE("tms9901", tms9901_cru_w)
239   AM_RANGE(0x0000, 0x7fff) AM_DEVWRITE("geneve_board", geneve_cru_w)
283   AM_RANGE(0x0000, 0x001f) AM_DEVWRITE(TMS9901_TAG, tms9901_device, write)
284   AM_RANGE(0x0000, 0x7fff) AM_WRITE( cruwrite )
240285ADDRESS_MAP_END
241286
242static INPUT_CHANGED( gm_changed )
243{
244   device_t *board = field.machine().device("geneve_board");
245   set_gm_switches(board, (UINT8)((UINT64)param&0x03), newval);
246}
247
248287/*
249288    Input ports, used by machine code for keyboard and joystick emulation.
250289*/
r14513r14514
253292static INPUT_PORTS_START(geneve)
254293
255294   PORT_START( "MODE" )
256   PORT_CONFNAME( 0x01, 0x00, "Operating mode" )
295   PORT_CONFNAME( 0x01, 0x00, "Operating mode" ) PORT_CHANGED_MEMBER(PERIBOX_TAG, peribox_device, genmod_changed, 0)
257296      PORT_CONFSETTING(    0x00, "Standard" )
258297      PORT_CONFSETTING(    GENMOD, "GenMod" )
259298
260299   PORT_START( "BOOTROM" )
261   PORT_CONFNAME( 0x01, 0x00, "Boot ROM" ) PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, 0x00 )
262      PORT_CONFSETTING(    0x00, "Version 0.98" )
263      PORT_CONFSETTING(    0x01, "Version 1.00" )
300   PORT_CONFNAME( 0x01, GENEVE_098, "Boot ROM" ) PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, 0x00 )
301      PORT_CONFSETTING( GENEVE_098, "Version 0.98" )
302      PORT_CONFSETTING( GENEVE_100, "Version 1.00" )
264303
265   PORT_START( "SPEECH" )
266   PORT_CONFNAME( 0x01, 0x00, "Speech synthesizer" )
267      PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
268      PORT_CONFSETTING( 0x01, DEF_STR( On ) )
269
270304   PORT_START( "SRAM" )
271305   PORT_CONFNAME( 0x03, 0x01, "Onboard SRAM" ) PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, 0x00 )
272306      PORT_CONFSETTING( 0x00, "32 KiB" )
273307      PORT_CONFSETTING( 0x01, "64 KiB" )
274308      PORT_CONFSETTING( 0x02, "384 KiB" )
275309
276   PORT_START( "EXTRAM" )
277   PORT_CONFNAME( 0x01, 0x01, "Memory expansion" )
278      PORT_CONFSETTING( 0x00, DEF_STR( None ) )
279      PORT_CONFSETTING( 0x01, "Memex 2 MiB" )
280
281// The BwG controller fails to boot on the Geneve. To be checked.
282   PORT_START( "DISKCTRL" )
283   PORT_CONFNAME( 0x07, 0x03, "Disk controller" )
284      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
285      PORT_CONFSETTING(    0x01, "TI SD Floppy Controller" )
286//      PORT_CONFSETTING(    0x02, "SNUG BwG Controller" )
287      PORT_CONFSETTING(    0x03, "Myarc HFDC" )
288//      PORT_CONFSETTING(    0x04, "Corcomp" )
289
290   PORT_START( "BWGDIP1" )
291   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
292      PORT_DIPSETTING( 0x00, "6 ms")
293      PORT_DIPSETTING( 0x01, "20 ms")
294
295   PORT_START( "BWGDIP2" )
296   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
297      PORT_DIPSETTING( 0x00, "Hide")
298      PORT_DIPSETTING( 0x01, "Show")
299
300   PORT_START( "BWGDIP34" )
301   PORT_DIPNAME( 0x03, 0x00, "BwG drives" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
302      PORT_DIPSETTING( 0x00, "DSK1 only")
303      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
304      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
305      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
306
307   PORT_START( "HDCTRL" )
308   PORT_CONFNAME( 0x03, 0x00, "HD controller" )
309      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
310//      PORT_CONFSETTING(    0x01, "Nouspikel IDE Controller" )
311//      PORT_CONFSETTING(    0x02, "WHTech SCSI Controller" )
312   PORT_CONFNAME( 0x08, 0x00, "USB-SM card" )
313      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
314      PORT_CONFSETTING(    0x08, DEF_STR( On ) )
315
316   PORT_START( "SERIAL" )
317   PORT_CONFNAME( 0x03, 0x00, "Serial/Parallel interface" )
318      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
319      PORT_CONFSETTING(    0x01, "TI RS-232 card" )
320
321   PORT_START( "SERIALMAP" )
322   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" ) PORT_CONDITION( "SERIAL", 0x03, PORTCOND_NOTEQUALS, 0x00 )
323      PORT_CONFSETTING(    0x00, "6-20" )
324      PORT_CONFSETTING(    0x01, "8-20" )
325      PORT_CONFSETTING(    0x02, "5-20" )
326
327   PORT_START( "MEMEXDIPS" )
328   PORT_DIPNAME( MDIP1, MDIP1, "MEMEX SW1" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
329      PORT_DIPSETTING( 0x00, "LED half-bright for 0 WS")
330      PORT_DIPSETTING( MDIP1, "LED full-bright")
331   PORT_DIPNAME( MDIP2, 0x00, "MEMEX SW2" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
332      PORT_DIPSETTING( 0x00, "Lock out all BA mirrors")
333      PORT_DIPSETTING( MDIP2, "Lock out page BA only")
334   PORT_DIPNAME( MDIP3, 0x00, "MEMEX SW3" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
335      PORT_DIPSETTING( 0x00, "Enable pages E8-EB")
336      PORT_DIPSETTING( MDIP3, "Lock out pages E8-EB")
337   PORT_DIPNAME( MDIP4, 0x00, "MEMEX SW4" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
338      PORT_DIPSETTING( 0x00, "Enable pages EC-EF")
339      PORT_DIPSETTING( MDIP4, "Lock out pages EC-EF")
340   PORT_DIPNAME( MDIP5, 0x00, "MEMEX SW5" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
341      PORT_DIPSETTING( 0x00, "Enable pages F0-F3")
342      PORT_DIPSETTING( MDIP5, "Lock out pages F0-F3")
343   PORT_DIPNAME( MDIP6, 0x00, "MEMEX SW6" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
344      PORT_DIPSETTING( 0x00, "Enable pages F4-F7")
345      PORT_DIPSETTING( MDIP6, "Lock out pages F4-F7")
346   PORT_DIPNAME( MDIP7, 0x00, "MEMEX SW7" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
347      PORT_DIPSETTING( 0x00, "Enable pages F8-FB")
348      PORT_DIPSETTING( MDIP7, "Lock out pages F8-FB")
349   PORT_DIPNAME( MDIP8, 0x00, "MEMEX SW8" ) PORT_CONDITION( "EXTRAM", 0x01, PORTCOND_EQUALS, 0x01 )
350      PORT_DIPSETTING( 0x00, "Enable pages FC-FF")
351      PORT_DIPSETTING( MDIP8, "Lock out pages FC-FF")
352
353   PORT_START( "HFDCDIP" )
354   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
355      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
356      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
357      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
358      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
359
360   PORT_START( "V9938-MEM" )
361   PORT_CONFNAME( 0x01, 0x00, "V9938 RAM size" )
362      PORT_CONFSETTING(   0x00, "128 KiB" )
363      PORT_CONFSETTING(   0x01, "192 KiB" )
364
365310   PORT_START( "GENMODDIPS" )
366   PORT_DIPNAME( GM_TURBO, 0x00, "Genmod Turbo mode") PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, GENMOD ) PORT_CHANGED( gm_changed, (void *)1)
311   PORT_DIPNAME( GM_TURBO, 0x00, "Genmod Turbo mode") PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, GENMOD ) PORT_CHANGED_MEMBER(GMAPPER_TAG, geneve_mapper_device, gm_changed, 1)
367312      PORT_CONFSETTING( 0x00, DEF_STR( Off ))
368313      PORT_CONFSETTING( GM_TURBO, DEF_STR( On ))
369   PORT_DIPNAME( GM_TIM, GM_TIM, "Genmod TI mode") PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, GENMOD ) PORT_CHANGED( gm_changed, (void *)2)
314   PORT_DIPNAME( GM_TIM, GM_TIM, "Genmod TI mode") PORT_CONDITION( "MODE", 0x01, PORTCOND_EQUALS, GENMOD ) PORT_CHANGED_MEMBER(GMAPPER_TAG, geneve_mapper_device, gm_changed, 2)
370315      PORT_CONFSETTING( 0x00, DEF_STR( Off ))
371316      PORT_CONFSETTING( GM_TIM, DEF_STR( On ))
372317
373   PORT_START( "DRVSPD" )
374   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
375      PORT_CONFSETTING( 0x00, "No delay")
376      PORT_CONFSETTING( 0x01, "Realistic")
377
378318   PORT_START("JOY")   /* col 1: "wired handset 1" (= joystick 1) */
379319      PORT_BIT(0x0080, IP_ACTIVE_LOW, IPT_JOYSTICK_UP/*, "(1UP)", CODE_NONE, OSD_JOY_UP*/) PORT_PLAYER(1)
380320      PORT_BIT(0x0040, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN/*, "(1DOWN)", CODE_NONE, OSD_JOY_DOWN, 0*/) PORT_PLAYER(1)
r14513r14514
389329      PORT_BIT(0x1000, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT/*, "(2LEFT)", CODE_NONE, OSD_JOY2_LEFT, 0*/) PORT_PLAYER(2)
390330      PORT_BIT(0x0800, IP_ACTIVE_LOW, IPT_BUTTON1/*, "(2FIRE)", CODE_NONE, OSD_JOY2_FIRE, 0*/) PORT_PLAYER(2)
391331      PORT_BIT(0x0700, IP_ACTIVE_HIGH, IPT_UNUSED)
332INPUT_PORTS_END
392333
393   PORT_START("MOUSEX") /* Mouse - X AXIS */
394   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
334/****************************************************************************
335    CRU handling
336*****************************************************************************/
395337
396   PORT_START("MOUSEY") /* Mouse - Y AXIS */
397   PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_Y) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
338#define CRU_CONTROL_BASE 0x1ee0
339#define CRU_SSTEP_BASE 0x13c0
398340
399   PORT_START("MOUSE0") /* mouse buttons */
400   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_BUTTON1) PORT_NAME("mouse button 1")
401   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON2) PORT_NAME("mouse button 2")
402   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON3) PORT_NAME("mouse button 3")
341WRITE8_MEMBER ( geneve::cruwrite )
342{
343   int addroff = offset << 1;
403344
404   PORT_START("KEY0")   /* IN3 */
405   PORT_BIT ( 0x0001, 0x0000, IPT_UNUSED )    /* unused scancode 0 */
406   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Esc") PORT_CODE(KEYCODE_ESC) /* Esc                       01  81 */
407   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("1 !") PORT_CODE(KEYCODE_1) /* 1                           02  82 */
408   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("2 @") PORT_CODE(KEYCODE_2) /* 2                           03  83 */
409   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("3 #") PORT_CODE(KEYCODE_3) /* 3                           04  84 */
410   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("4 $") PORT_CODE(KEYCODE_4) /* 4                           05  85 */
411   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("5 %") PORT_CODE(KEYCODE_5) /* 5                           06  86 */
412   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("6 ^") PORT_CODE(KEYCODE_6) /* 6                           07  87 */
413   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("7 &") PORT_CODE(KEYCODE_7) /* 7                           08  88 */
414   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("8 *") PORT_CODE(KEYCODE_8) /* 8                           09  89 */
415   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("9 (") PORT_CODE(KEYCODE_9) /* 9                           0A  8A */
416   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("0 )") PORT_CODE(KEYCODE_0) /* 0                           0B  8B */
417   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("- _") PORT_CODE(KEYCODE_MINUS) /* -                           0C  8C */
418   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("= +") PORT_CODE(KEYCODE_EQUALS) /* =                          0D  8D */
419   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Backspace") PORT_CODE(KEYCODE_BACKSPACE) /* Backspace                 0E  8E */
420   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Tab") PORT_CODE(KEYCODE_TAB) /* Tab                       0F  8F */
345   // Single step
346   // 13c0 - 13fe: 0001 0011 11xx xxx0
347   if ((addroff & 0xffc0) == CRU_SSTEP_BASE)
348   {
349      int bit = (addroff & 0x003e)>>1;
350      if (VERBOSE>0) LOG("geneve: Single step not implemented; bit %d set to %d\n", bit, data);
351      return;
352   }
421353
422   PORT_START("KEY1")   /* IN4 */
423   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Q") PORT_CODE(KEYCODE_Q) /* Q                         10  90 */
424   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("W") PORT_CODE(KEYCODE_W) /* W                         11  91 */
425   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("E") PORT_CODE(KEYCODE_E) /* E                         12  92 */
426   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("R") PORT_CODE(KEYCODE_R) /* R                         13  93 */
427   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("T") PORT_CODE(KEYCODE_T) /* T                         14  94 */
428   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Y") PORT_CODE(KEYCODE_Y) /* Y                         15  95 */
429   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("U") PORT_CODE(KEYCODE_U) /* U                         16  96 */
430   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("I") PORT_CODE(KEYCODE_I) /* I                         17  97 */
431   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("O") PORT_CODE(KEYCODE_O) /* O                         18  98 */
432   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("P") PORT_CODE(KEYCODE_P) /* P                         19  99 */
433   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("[ {") PORT_CODE(KEYCODE_OPENBRACE) /* [                           1A  9A */
434   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("] }") PORT_CODE(KEYCODE_CLOSEBRACE) /* ]                          1B  9B */
435   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Enter") PORT_CODE(KEYCODE_ENTER) /* Enter                     1C  9C */
436   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L-Ctrl") PORT_CODE(KEYCODE_LCONTROL) /* Left Ctrl                 1D  9D */
437   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("A") PORT_CODE(KEYCODE_A) /* A                         1E  9E */
438   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("S") PORT_CODE(KEYCODE_S) /* S                         1F  9F */
354   if ((addroff & 0xffe0) == CRU_CONTROL_BASE)
355   {
356      int bit = (addroff & 0x001e)>>1;
357      switch (bit)
358      {
359      case 5:
360         // No one really cares...
361         // m_palvideo = (data!=0);
362         break;
363      case 7:
364         // m_capslock = (data!=0);
365         break;
366      case 8:
367         m_keyboard->clock_control((data!=0)? ASSERT_LINE : CLEAR_LINE);
368         break;
369      case 9:
370         m_keyboard->send_scancodes((data!=0)? ASSERT_LINE : CLEAR_LINE);
371         break;
372      case 10:
373         m_mapper->set_geneve_mode(data!=0);
374         break;
375      case 11:
376         m_mapper->set_direct_mode(data!=0);
377         break;
378      case 12:
379         m_mapper->set_cartridge_size((data!=0)? 0x2000 : 0x4000);
380         break;
381      case 13:
382         m_mapper->set_cartridge_writable(0x6000, (data!=0));
383         break;
384      case 14:
385         m_mapper->set_cartridge_writable(0x7000, (data!=0));
386         break;
387      case 15:
388         m_mapper->set_extra_waitstates(data==0);  // let's use the inverse semantics
389         break;
390      default:
391         if (VERBOSE>0) LOG("geneve: set CRU address %04x=%02x ignored\n", addroff, data);
392         break;
393      }
394   }
395   else
396   {
397      m_peribox->cruwrite(addroff, data);
398   }
399}
439400
440   PORT_START("KEY2")   /* IN5 */
441   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("D") PORT_CODE(KEYCODE_D) /* D                         20  A0 */
442   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F") PORT_CODE(KEYCODE_F) /* F                         21  A1 */
443   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("G") PORT_CODE(KEYCODE_G) /* G                         22  A2 */
444   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("H") PORT_CODE(KEYCODE_H) /* H                         23  A3 */
445   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("J") PORT_CODE(KEYCODE_J) /* J                         24  A4 */
446   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("K") PORT_CODE(KEYCODE_K) /* K                         25  A5 */
447   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L") PORT_CODE(KEYCODE_L) /* L                         26  A6 */
448   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("; :") PORT_CODE(KEYCODE_COLON) /* ;                           27  A7 */
449   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("' \"") PORT_CODE(KEYCODE_QUOTE) /* '                          28  A8 */
450   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("` ~") PORT_CODE(KEYCODE_TILDE) /* `                           29  A9 */
451   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("L-Shift") PORT_CODE(KEYCODE_LSHIFT) /* Left Shift                 2A  AA */
452   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("\\ |") PORT_CODE(KEYCODE_BACKSLASH) /* \                          2B  AB */
453   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Z") PORT_CODE(KEYCODE_Z) /* Z                         2C  AC */
454   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("X") PORT_CODE(KEYCODE_X) /* X                         2D  AD */
455   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("C") PORT_CODE(KEYCODE_C) /* C                         2E  AE */
456   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("V") PORT_CODE(KEYCODE_V) /* V                         2F  AF */
401READ8_MEMBER( geneve::cruread )
402{
403   UINT8 value = 0;
404   int addroff = offset << 4;
457405
458   PORT_START("KEY3")   /* IN6 */
459   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("B") PORT_CODE(KEYCODE_B) /* B                         30  B0 */
460   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("N") PORT_CODE(KEYCODE_N) /* N                         31  B1 */
461   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("M") PORT_CODE(KEYCODE_M) /* M                         32  B2 */
462   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME(", <") PORT_CODE(KEYCODE_COMMA) /* ,                           33  B3 */
463   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME(". >") PORT_CODE(KEYCODE_STOP) /* .                            34  B4 */
464   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("/ ?") PORT_CODE(KEYCODE_SLASH) /* /                           35  B5 */
465   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("R-Shift") PORT_CODE(KEYCODE_RSHIFT) /* Right Shift                36  B6 */
466   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP * (PrtScr)") PORT_CODE(KEYCODE_ASTERISK   ) /* Keypad *  (PrtSc)          37  B7 */
467   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Alt") PORT_CODE(KEYCODE_LALT) /* Left Alt                 38  B8 */
468   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Space") PORT_CODE(KEYCODE_SPACE) /* Space                     39  B9 */
469   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Caps") PORT_CODE(KEYCODE_CAPSLOCK) /* Caps Lock                   3A  BA */
470   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F1") PORT_CODE(KEYCODE_F1) /* F1                          3B  BB */
471   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F2") PORT_CODE(KEYCODE_F2) /* F2                          3C  BC */
472   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F3") PORT_CODE(KEYCODE_F3) /* F3                          3D  BD */
473   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F4") PORT_CODE(KEYCODE_F4) /* F4                          3E  BE */
474   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F5") PORT_CODE(KEYCODE_F5) /* F5                          3F  BF */
406   // Single step
407   // 13c0 - 13fe: 0001 0011 11xx xxx0
408   if ((addroff & 0xffc0) == CRU_SSTEP_BASE)
409   {
410      int bit = (addroff & 0x003e)>>1;
411      if (VERBOSE>0) LOG("geneve: Single step not implemented; attempting to read bit %d\n", bit);
412      return value;
413   }
475414
476   PORT_START("KEY4")   /* IN7 */
477   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F6") PORT_CODE(KEYCODE_F6) /* F6                          40  C0 */
478   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F7") PORT_CODE(KEYCODE_F7) /* F7                          41  C1 */
479   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F8") PORT_CODE(KEYCODE_F8) /* F8                          42  C2 */
480   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F9") PORT_CODE(KEYCODE_F9) /* F9                          43  C3 */
481   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("F10") PORT_CODE(KEYCODE_F10) /* F10                       44  C4 */
482   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("NumLock") PORT_CODE(KEYCODE_NUMLOCK) /* Num Lock                  45  C5 */
483   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("ScrLock (F14)") PORT_CODE(KEYCODE_SCRLOCK) /* Scroll Lock             46  C6 */
484   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 7 (Home)") PORT_CODE(KEYCODE_7_PAD      ) /* Keypad 7  (Home)           47  C7 */
485   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 8 (Up)") PORT_CODE(KEYCODE_8_PAD      ) /* Keypad 8  (Up arrow)       48  C8 */
486   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 9 (PgUp)") PORT_CODE(KEYCODE_9_PAD      ) /* Keypad 9  (PgUp)           49  C9 */
487   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP -") PORT_CODE(KEYCODE_MINUS_PAD) /* Keypad -                   4A  CA */
488   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 4 (Left)") PORT_CODE(KEYCODE_4_PAD      ) /* Keypad 4  (Left arrow)     4B  CB */
489   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 5") PORT_CODE(KEYCODE_5_PAD) /* Keypad 5                   4C  CC */
490   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 6 (Right)") PORT_CODE(KEYCODE_6_PAD      ) /* Keypad 6  (Right arrow)    4D  CD */
491   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP +") PORT_CODE(KEYCODE_PLUS_PAD) /* Keypad +                    4E  CE */
492   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 1 (End)") PORT_CODE(KEYCODE_1_PAD      ) /* Keypad 1  (End)            4F  CF */
415   // TMS9995-internal CRU locations (1ee0-1efe) are handled within the 99xxcore
416   // implementation (read_single_cru), so we don't arrive here
493417
494   PORT_START("KEY5")   /* IN8 */
495   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 2 (Down)") PORT_CODE(KEYCODE_2_PAD      ) /* Keypad 2  (Down arrow)     50  D0 */
496   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 3 (PgDn)") PORT_CODE(KEYCODE_3_PAD      ) /* Keypad 3  (PgDn)           51  D1 */
497   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP 0 (Ins)") PORT_CODE(KEYCODE_0_PAD      ) /* Keypad 0  (Ins)            52  D2 */
498   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("KP . (Del)") PORT_CODE(KEYCODE_DEL_PAD      ) /* Keypad .  (Del)            53  D3 */
499   PORT_BIT ( 0x0030, 0x0000, IPT_UNUSED )
500   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(84/102)\\") PORT_CODE(KEYCODE_BACKSLASH2) /* Backslash 2             56  D6 */
501   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)F11") PORT_CODE(KEYCODE_F11) /* F11                      57  D7 */
502   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)F12") PORT_CODE(KEYCODE_F12) /* F12                      58  D8 */
503   PORT_BIT ( 0xfe00, 0x0000, IPT_UNUSED )
418   // Propagate the CRU access to external devices
419   m_peribox->crureadz(addroff, &value);
420   return value;
421}
504422
505   PORT_START("KEY6")   /* IN9 */
506   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)KP Enter") PORT_CODE(KEYCODE_ENTER_PAD) /* PAD Enter                 60  e0 */
507   PORT_BIT(0x0002, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)R-Control") PORT_CODE(KEYCODE_RCONTROL) /* Right Control             61  e1 */
508   PORT_BIT(0x0004, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)ALTGR") PORT_CODE(KEYCODE_RALT) /* ALTGR                     64  e4 */
423/***********************************************************************
424    CRU callbacks
425***********************************************************************/
509426
510   PORT_BIT(0x0008, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)KP /") PORT_CODE(KEYCODE_SLASH_PAD) /* PAD Slash                 62  e2 */
427READ8_MEMBER( geneve::read_by_9901 )
428{
429   int answer = 0;
430   int joy = 0;
431   switch (offset & 0x03)
432   {
433   case TMS9901_CB_INT7:
434      // Read pins INT3*-INT7* of Geneve 9901.
435      //
436      // bit 1: INTA status
437      // bit 2: INT2 status
438      // bit 3-7: joystick status
511439
512   PORT_BIT(0x0010, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Home") PORT_CODE(KEYCODE_HOME) /* Home                       66  e6 */
513   PORT_BIT(0x0020, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Up") PORT_CODE(KEYCODE_UP) /* Up                          67  e7 */
514   PORT_BIT(0x0040, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Page Up") PORT_CODE(KEYCODE_PGUP) /* Page Up                 68  e8 */
515   PORT_BIT(0x0080, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Left") PORT_CODE(KEYCODE_LEFT) /* Left                        69  e9 */
516   PORT_BIT(0x0100, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Right") PORT_CODE(KEYCODE_RIGHT) /* Right                     6a  ea */
517   PORT_BIT(0x0200, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)End") PORT_CODE(KEYCODE_END) /* End                      6b  eb */
518   PORT_BIT(0x0400, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Cursor Down") PORT_CODE(KEYCODE_DOWN) /* Down                        6c  ec */
519   PORT_BIT(0x0800, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Page Down") PORT_CODE(KEYCODE_PGDN) /* Page Down                 6d  ed */
520   PORT_BIT(0x1000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Insert") PORT_CODE(KEYCODE_INSERT) /* Insert                     6e  ee */
521   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Delete") PORT_CODE(KEYCODE_DEL) /* Delete                        6f  ef */
440      // negative logic
441      if (m_inta==CLEAR_LINE) answer |= 0x02;
442      if (m_int2==CLEAR_LINE) answer |= 0x04;
522443
523   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)PrtScr (F13)") PORT_CODE(KEYCODE_PRTSCR) /* Print Screen             63  e3 */
524   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("(101)Pause (F15)") PORT_CODE(KEYCODE_PAUSE) /* Pause                      65  e5 */
444      joy = input_port_read(machine(), "JOY");
445      if (m_joystick_select==0) joy = joy & 0xff;
446      else joy = (joy>>8) & 0xff;
447      answer |= joy;
525448
526   PORT_START("KEY7")   /* IN10 */
527   PORT_BIT ( 0xffff, 0x0000, IPT_UNUSED )
528#if 0
529   PORT_BIT(0x0001, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Print Screen") PORT_CODE(KEYCODE_PRTSCR) /* Print Screen alternate        77  f7 */
530   PORT_BIT(0x2000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Left Win") /* Left Win                    7d  fd */
531   PORT_BIT(0x4000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Right Win") /* Right Win                  7e  fe */
532   PORT_BIT(0x8000, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Menu") /* Menu                        7f  ff */
533#endif
449      break;
534450
535INPUT_PORTS_END
451   case TMS9901_INT8_INT15:
452      // Read pins INT8*-INT15* of Geneve 9901.
453      //
454      // bit 0: keyboard interrupt
455      // bit 1: unused
456      // bit 2: mouse left button
457      // (bit 3: clock interrupt)
458      // bit 4: INTB from PE-bus
459      // bit 5 & 7: used as output
460      // bit 6: unused
461      if (m_keyint==CLEAR_LINE) answer |= 0x01;
462      if (m_mouse->left_button()==CLEAR_LINE) answer |= 0x04;
463      // TODO: add clock interrupt
464      if (m_intb==CLEAR_LINE) answer |= 0x10;
465      if (m_video_wait==ASSERT_LINE) answer |= 0x20;
466      // TODO: PAL pin 5
467      if (VERBOSE>8) LOG("geneve: INT15-8 = %02x\n", answer);
468      break;
536469
470   case TMS9901_P0_P7:
471      // Read pins P0-P7 of TMS9901. All pins are configured as outputs, so nothing here.
472      break;
473
474   case TMS9901_P8_P15:
475      // Read pins P8-P15 of TMS 9901.
476      // bit 4: mouse left button
477      // video wait is an output; no input possible here
478      if (m_intb==CLEAR_LINE) answer |= 0x04;      // mirror from above
479      // TODO: 0x08 = real-time clock int
480      if (m_mouse->left_button()==CLEAR_LINE) answer |= 0x10;   // mirror from above
481      if (m_keyint==CLEAR_LINE) answer |= 0x40;
482
483      // Joystick up (mirror of bit 7)
484      joy = input_port_read(machine(), "JOY");
485      if (m_joystick_select==0) joy = joy & 0xff;
486      else joy = (joy>>8) & 0xff;
487      if ((joy & 0x80)==0x00) answer |= 0x80;
488
489      break;
490   }
491   return answer;
492}
493
494/*
495    Write PE bus reset line
496*/
497WRITE_LINE_MEMBER( geneve::peripheral_bus_reset )
498{
499   if (VERBOSE>0) LOG("geneve: peripheral bus reset request; not implemented yet.\n");
500}
501
502/*
503    Write VDP reset line
504*/
505WRITE_LINE_MEMBER( geneve::VDP_reset )
506{
507   if (VERBOSE>0) LOG("geneve: Video reset request; not implemented yet.\n");
508}
509
510/*
511    Write joystick select line
512*/
513WRITE_LINE_MEMBER( geneve::joystick_select )
514{
515   m_joystick_select = (state==ASSERT_LINE)? 1:0;
516}
517
518/*
519    Write external mem cycles (0=long, 1=short)
520*/
521WRITE_LINE_MEMBER( geneve::extbus_wait_states )
522{
523   if (VERBOSE>0) LOG("geneve: external bus wait states set to %d, not implemented yet.\n", state);
524}
525
526/*
527    Write vdp wait cycles (1=add 14 cycles, 0=add none)
528    see above for waitstate handling
529*/
530WRITE_LINE_MEMBER( geneve::video_wait_states )
531{
532   if (VERBOSE>1) LOG("geneve: vdp wait states set to %d\n", state);
533   m_mapper->set_video_waitstates(state==ASSERT_LINE);
534   m_video_wait = (state!=0)? ASSERT_LINE : CLEAR_LINE;
535}
536
537/*
538    Called by the 9901 core whenever the state of INTREQ and IC0-3 changes.
539    As with the TI-99/4A, the interrupt level is delivered as the offset,
540    but again it is ignored.
541*/
542WRITE8_MEMBER( geneve::tms9901_interrupt )
543{
544   /* INTREQ is connected to INT1 (IC0-3 are not connected) */
545   device_execute(m_cpu)->set_input_line(0, data);
546}
547
548/* tms9901 setup */
549const tms9901_interface tms9901_wiring_geneve =
550{
551   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INT8 | TMS9901_INTB | TMS9901_INTC,   /* only input pins whose state is always known */
552
553   // read handler
554   DEVCB_DRIVER_MEMBER(geneve, read_by_9901),
555
556   {   /* write handlers */
557      DEVCB_DRIVER_LINE_MEMBER(geneve, peripheral_bus_reset),
558      DEVCB_DRIVER_LINE_MEMBER(geneve, VDP_reset),
559      DEVCB_DRIVER_LINE_MEMBER(geneve, joystick_select),
560      DEVCB_NULL,
561      DEVCB_NULL,
562      DEVCB_NULL,
563      DEVCB_DEVICE_LINE_MEMBER(GKEYBOARD_TAG, geneve_keyboard_device, reset_line),
564      DEVCB_DRIVER_LINE_MEMBER(geneve, extbus_wait_states),
565      DEVCB_NULL,
566      DEVCB_DRIVER_LINE_MEMBER(geneve, video_wait_states),
567      DEVCB_NULL,
568      DEVCB_NULL,
569      DEVCB_NULL,
570      DEVCB_NULL,
571      DEVCB_NULL,
572      DEVCB_NULL
573   },
574
575   /* interrupt handler */
576   DEVCB_DRIVER_MEMBER(geneve, tms9901_interrupt)
577};
578
579/*******************************************************************
580    Signal lines
581*******************************************************************/
582/*
583    inta is connected to both tms9901 IRQ1 line and to tms9995 INT4/EC line.
584*/
585WRITE_LINE_MEMBER( geneve::inta )
586{
587   m_inta = (state!=0)? ASSERT_LINE : CLEAR_LINE;
588   m_tms9901->set_single_int(1, state);
589   device_execute(m_cpu)->set_input_line(1, state);
590}
591
592/*
593    intb is connected to tms9901 IRQ12 line.
594*/
595WRITE_LINE_MEMBER( geneve::intb )
596{
597   m_intb = (state!=0)? ASSERT_LINE : CLEAR_LINE;
598   m_tms9901->set_single_int(12, state);
599}
600
601WRITE_LINE_MEMBER( geneve::ready )
602{
603   if (VERBOSE>0) LOG("geneve: READY line set ... not yet connected, level=%02x\n", state);
604}
605
606/*
607    set the state of int2 (called by the v9938 core)
608*/
609void geneve::set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state)
610{
611   m_int2 = (state!=0)? ASSERT_LINE : CLEAR_LINE;
612   m_tms9901->set_single_int(2, state);
613}
614
615/*
616    Interrupt from the keyboard.
617*/
618WRITE_LINE_MEMBER( geneve::keyboard_interrupt )
619{
620   m_keyint = (state!=0)? ASSERT_LINE : CLEAR_LINE;
621   m_tms9901->set_single_int(8, state);
622}
623
624/*
625    scanline interrupt
626*/
627TIMER_DEVICE_CALLBACK( geneve_hblank_interrupt )
628{
629   int scanline = param;
630   geneve *driver = timer.machine().driver_data<geneve>();
631
632   timer.machine().device<v9938_device>(V9938_TAG)->interrupt();
633
634   if (scanline == 0) // was 262
635   {
636      // TODO
637      // The technical docs do not say anything about the way the mouse
638      // is queried. It sounds plausible that the mouse is sampled once
639      // per vertical interrupt; however, the mouse sometimes shows jerky
640      // behaviour. Maybe we should use an autonomous timer with a higher
641      // rate? -> to be checked
642      driver->m_mouse->poll();
643   }
644}
645
537646static const struct tms9995reset_param geneve_processor_config =
538647{
539648   0,   /* disable automatic wait state generation */
r14513r14514
541650   0   /* no MP9537 mask */
542651};
543652
653static const mm58274c_interface geneve_mm58274c_interface =
654{
655   1,   /*  mode 24*/
656   0   /*  first day of week */
657};
544658
659static GENEVE_KEYBOARD_CONFIG( geneve_keyb_conf )
660{
661   DEVCB_DRIVER_LINE_MEMBER(geneve, keyboard_interrupt)
662};
663
664static PERIBOX_CONFIG( peribox_conf )
665{
666   DEVCB_DRIVER_LINE_MEMBER(geneve, inta),         // INTA
667   DEVCB_DRIVER_LINE_MEMBER(geneve, intb),         // INTB
668   DEVCB_DRIVER_LINE_MEMBER(geneve, ready),      // READY
669   0x00000                                 // Address bus prefix (Mapper will produce prefixes)
670};
671
545672static DRIVER_INIT( geneve )
546673{
547674}
548675
549676static MACHINE_START( geneve )
550677{
678   geneve *driver = machine.driver_data<geneve>();
679   driver->m_tms9901 = static_cast<tms9901_device*>(machine.device(TMS9901_TAG));
680   driver->m_mapper = static_cast<geneve_mapper_device*>(machine.device(GMAPPER_TAG));
681   driver->m_keyboard = static_cast<geneve_keyboard_device*>(machine.device(GKEYBOARD_TAG));
682   driver->m_peribox = static_cast<peribox_device*>(machine.device(PERIBOX_TAG));
683   driver->m_mouse =  static_cast<geneve_mouse_device*>(machine.device(GMOUSE_TAG));
684   driver->m_cpu = machine.device("maincpu");
551685}
552686
553687/*
r14513r14514
555689*/
556690static MACHINE_RESET( geneve )
557691{
692   geneve *driver = machine.driver_data<geneve>();
693   driver->m_inta = CLEAR_LINE;   // flag reflecting the INTA line
694   driver->m_intb = CLEAR_LINE;   // flag reflecting the INTB line
695   driver->m_int2 = CLEAR_LINE;   // flag reflecting the INT2 line
696   driver->m_keyint = CLEAR_LINE;
697
698   driver->m_peribox->set_genmod(input_port_read(machine, "MODE")==GENMOD);
558699}
559700
560static MACHINE_CONFIG_START( geneve_60hz, geneve_state )
701static MACHINE_CONFIG_START( geneve_60hz, geneve )
561702   /* basic machine hardware */
562703   /* TMS9995 CPU @ 12.0 MHz */
563704   MCFG_CPU_ADD("maincpu", TMS9995, 12000000)
564705   MCFG_CPU_CONFIG(geneve_processor_config)
565706   MCFG_CPU_PROGRAM_MAP(memmap)
566   MCFG_CPU_IO_MAP(cru_map)
567   MCFG_TIMER_ADD_SCANLINE("scantimer", geneve_hblank_interrupt, "screen", 0, 1) /* 262.5 in 60Hz, 312.5 in 50Hz */
707   MCFG_CPU_IO_MAP(crumap)
568708
569709   MCFG_MACHINE_START( geneve )
570710   MCFG_MACHINE_RESET( geneve )
r14513r14514
573713   /* FIXME: (MZ) Lowered the screen rate to 30 Hz. This is a quick hack to
574714    restore normal video speed for V9938-based systems until the V9938 implementation
575715    is properly fixed. */
576   MCFG_TI_V9938_ADD("video", 30, "screen", 2500, 512+32, (212+28)*2, tms9901_gen_set_int2)
716   MCFG_TI_V9938_ADD(VIDEO_SYSTEM_TAG, 30, SCREEN_TAG, 2500, 512+32, (212+28)*2, DEVICE_SELF, geneve, set_tms9901_INT2_from_v9938)
717   MCFG_TIMER_ADD_SCANLINE("scantimer", geneve_hblank_interrupt, SCREEN_TAG, 0, 1) /* 262.5 in 60Hz, 312.5 in 50Hz */
577718
719   /* Main board components */
720   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_geneve, 3000000)
721   MCFG_GENEVE_MAPPER_ADD( GMAPPER_TAG )
722   MCFG_MM58274C_ADD(GCLOCK_TAG, geneve_mm58274c_interface)
723
724   /* Peripheral expansion box (Geneve composition) */
725   MCFG_PERIBOX_GEN_ADD( PERIBOX_TAG, peribox_conf )
726
578727   /* sound hardware */
579728   MCFG_SPEAKER_STANDARD_MONO("mono")
580   MCFG_SOUND_ADD("soundgen", SN76496, 3579545)   /* 3.579545 MHz */
729   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN76496, 3579545)   /* 3.579545 MHz */
581730   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
731   MCFG_TI_SOUND_ADD( TISOUND_TAG )
582732
583   /* Peripheral Box. Take the Geneve combo. */
584   MCFG_PBOXGEN_ADD( "peribox", board_inta, board_intb, board_ready )
585
586   /* tms9901 */
587   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_geneve)
588
589   /* Main board. Slight mods for the GenMod system. */
590   MCFG_GENEVE_BOARD_ADD("geneve_board")
733   /* User interface devices */
734   MCFG_GENEVE_KEYBOARD_ADD( GKEYBOARD_TAG, geneve_keyb_conf )
735   MCFG_GENEVE_MOUSE_ADD( GMOUSE_TAG )
591736MACHINE_CONFIG_END
592737
593738/*
594739    ROM loading
595
596    Note that we use the same ROMset for 50Hz and 60Hz versions.
597740*/
598741
599742ROM_START(geneve)
600743   /*CPU memory space*/
601744   ROM_REGION(0xc000, "maincpu", 0)
602   ROM_LOAD("genbt100.bin", 0x0000, 0x4000, CRC(8001e386) SHA1(b44618b54dabac3882543e18555d482b299e0109)) /* CPU ROMs */
603   ROM_LOAD_OPTIONAL("genbt098.bin", 0x4000, 0x4000, CRC(b2e20df9) SHA1(2d5d09177afe97d63ceb3ad59b498b1c9e2153f7)) /* CPU ROMs */
604   ROM_LOAD_OPTIONAL("gnmbt100.bin", 0x8000, 0x4000, CRC(19b89479) SHA1(6ef297eda78dc705946f6494e9d7e95e5216ec47)) /* CPU ROMs */
745   ROM_LOAD("genbt100.bin", 0x0000, 0x4000, CRC(8001e386) SHA1(b44618b54dabac3882543e18555d482b299e0109)) /* CPU ROMs v1.0 */
746   ROM_LOAD_OPTIONAL("genbt098.bin", 0x4000, 0x4000, CRC(b2e20df9) SHA1(2d5d09177afe97d63ceb3ad59b498b1c9e2153f7)) /* CPU ROMs v0.98 */
747   ROM_LOAD_OPTIONAL("gnmbt100.bin", 0x8000, 0x4000, CRC(19b89479) SHA1(6ef297eda78dc705946f6494e9d7e95e5216ec47)) /* CPU ROMs GenMod */
748
749   ROM_REGION(SRAM_SIZE, SRAM_TAG, 0)
750   ROM_FILL(0x0000, SRAM_SIZE, 0)
751
752   ROM_REGION(DRAM_SIZE, DRAM_TAG, 0)
753   ROM_FILL(0x0000, DRAM_SIZE, 0)
605754ROM_END
606755
607/*ROM_START(genmod)
608    ROM_REGION(0x8000, "maincpu", 0)
609    ROM_LOAD("gnmbt100.bin", 0x0000, 0x4000, CRC(19b89479) SHA1(6ef297eda78dc705946f6494e9d7e95e5216ec47))
610    ROM_LOAD_OPTIONAL("genbt090.bin", 0x4000, 0x4000, CRC(b2e20df9) SHA1(2d5d09177afe97d63ceb3ad59b498b1c9e2153f7))
611ROM_END
612*/
613756/*    YEAR  NAME      PARENT    COMPAT  MACHINE      INPUT    INIT       COMPANY     FULLNAME */
614757COMP( 1987,geneve,   0,      0,      geneve_60hz,  geneve,  geneve,      "Myarc",   "Geneve 9640" , 0)
615//COMP( 1990,genmod,   geneve,  0,      genmod_60hz,  geneve,  geneve,  "Myarc",    "Geneve 9640 (with Genmod modification)" , 0)
616
src/mess/drivers/ti99_4p.c
r14513r14514
1/*
2    SNUG SGCPU (a.k.a. 99/4p) system (preliminary)
1/****************************************************************************
32
3    SNUG SGCPU (a.k.a. 99/4p) system
4
45    This system is a reimplementation of the old ti99/4a console.  It is known
56    both as the 99/4p ("peripheral box", since the system is a card to be
67    inserted in the peripheral box, instead of a self contained console), and
r14513r14514
2122    GROMs, video or sound: instead, it relies on the HSGPL and EVPC cards to
2223    do the job.
2324
24    TODO:
25    * Test the system? Call Debug, Call XB16.
26    * Implement MEM8 timings.
27
28    2009-11-15
29    This driver found to be hopelessly broken.
30    1. Fixed crash in DRIVER_INIT - was trying to set up GROM when this model doesn't have any.
31    2. Fixed crash in MACHINE_RESET - new cart system depends on GROM.
32    3. Fixed crash when drawing the lower border - screen size changed to the same as ti99_4ev.
33    Now, it produces a black screen.
34    If you use memory view in the debugger, it crashes at 6000 and some higher addresses. This is
35    because the memory map has references to GROM handlers.
36
37    2010-06-19
38    MZ: Driver fixed
39    Important: The SGCPU card relies on a properly set up HSGPL flash memory
25    IMPORTANT: The SGCPU card relies on a properly set up HSGPL flash memory
4026    card; without, it will immediately lock up. It is impossible to set it up
4127    from here (a bootstrap problem; you cannot start without the HSGPL).
42    The best chance is to start a ti99_4ev, activate the
43    HSGPL, and go through the setup process there. Copy the hsgpl.nv into this
28    The best chance is to start a ti99_4ev with a plugged-in HSGPL
29    and go through the setup process there. Copy the nvram files of the hsgpl into this
4430    driver's nvram subdirectory. The contents will be directly usable for the SGCPU.
45*/
4631
32    Michael Zapf
33
34    February 2012: Rewritten as class
35
36*****************************************************************************/
37
38#define ADDRESS_MAP_MODERN
39
4740#include "emu.h"
4841#include "cpu/tms9900/tms9900.h"
4942#include "sound/wave.h"
r14513r14514
5548
5649#include "imagedev/cassette.h"
5750#include "machine/ti99/videowrp.h"
58#include "machine/ti99/sgcpu.h"
5951#include "machine/ti99/peribox.h"
6052
53#define TMS9901_TAG "tms9901"
54#define SGCPU_TAG "sgcpu"
6155
62class ti99_4p_state : public driver_device
56#define SAMSMEM_TAG "samsmem"
57#define PADMEM_TAG "padmem"
58
59#define VERBOSE 1
60#define LOG logerror
61
62class ti99_4p : public driver_device
6363{
6464public:
65   ti99_4p_state(const machine_config &mconfig, device_type type, const char *tag)
65   ti99_4p(const machine_config &mconfig, device_type type, const char *tag)
6666      : driver_device(mconfig, type, tag) { }
6767
68};
68   DECLARE_WRITE_LINE_MEMBER( console_ready );
69   DECLARE_WRITE_LINE_MEMBER( extint );
70   DECLARE_WRITE_LINE_MEMBER( notconnected );
6971
72   DECLARE_READ16_MEMBER( memread );
73   DECLARE_WRITE16_MEMBER( memwrite );
7074
71static ADDRESS_MAP_START(memmap, AS_PROGRAM, 16)
72   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE("sgcpu_board", sgcpu_r, sgcpu_w )
73ADDRESS_MAP_END
75   DECLARE_READ16_MEMBER( samsmem_read );
76   DECLARE_WRITE16_MEMBER( samsmem_write );
7477
75static ADDRESS_MAP_START(cru_map, AS_IO, 8)
76   AM_RANGE(0x0000, 0x007f) AM_DEVREAD("tms9901", tms9901_cru_r)
77   AM_RANGE(0x0080, 0x01ff) AM_DEVREAD("sgcpu_board", sgcpu_cru_r )
78   // CRU (Communication Register Unit) handling
79   DECLARE_READ8_MEMBER( cruread );
80   DECLARE_WRITE8_MEMBER( cruwrite );
81   DECLARE_READ8_MEMBER( read_by_9901 );
82   DECLARE_WRITE_LINE_MEMBER(keyC0);
83   DECLARE_WRITE_LINE_MEMBER(keyC1);
84   DECLARE_WRITE_LINE_MEMBER(keyC2);
85   DECLARE_WRITE_LINE_MEMBER(cs_motor);
86   DECLARE_WRITE_LINE_MEMBER(audio_gate);
87   DECLARE_WRITE_LINE_MEMBER(cassette_output);
88   DECLARE_WRITE8_MEMBER(tms9901_interrupt);
89   DECLARE_WRITE_LINE_MEMBER(handset_ack);
90   DECLARE_WRITE_LINE_MEMBER(alphaW);
7891
79   AM_RANGE(0x0000, 0x03ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
80   AM_RANGE(0x0400, 0x0fff) AM_DEVWRITE("sgcpu_board", sgcpu_cru_w )
81ADDRESS_MAP_END
92   void set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state);
8293
83/*
84    Input ports, used by machine code for TI keyboard and joystick emulation.
94   device_t*            m_cpu;
95   ti_sound_system_device*   m_sound;
96   ti_exp_video_device*   m_video;
97   cassette_image_device*   m_cassette;
98   peribox_device*         m_peribox;
99   tms9901_device*         m_tms9901;
85100
86    Since the keyboard microcontroller is not emulated, we use the TI99/4a 48-key keyboard,
87    plus two optional joysticks.
88*/
101   // Pointer to ROM0
102   UINT16 *m_rom0;
89103
90static INPUT_PORTS_START(ti99_4p)
104   // Pointer to DSR ROM
105   UINT16 *m_dsr;
91106
92   PORT_START( "SPEECH" )
93   PORT_CONFNAME( 0x01, 0x01, "Speech synthesizer" )
94      PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
95      PORT_CONFSETTING( 0x01, DEF_STR( On ) )
107   // Pointer to ROM6, first bank
108   UINT16 *m_rom6a;
96109
97   PORT_START( "DISKCTRL" )
98   PORT_CONFNAME( 0x07, 0x03, "Disk controller" )
99      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
100      PORT_CONFSETTING(    0x01, "TI SD Floppy Controller" )
101      PORT_CONFSETTING(    0x02, "SNUG BwG Controller" )
102      PORT_CONFSETTING(    0x03, "Myarc HFDC" )
103//      PORT_CONFSETTING(    0x04, "Corcomp" )
110   // Pointer to ROM6, second bank
111   UINT16 *m_rom6b;
104112
105   PORT_START( "HDCTRL" )
106   PORT_CONFNAME( 0x03, 0x00, "HD controller" )
107      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
108//      PORT_CONFSETTING(    0x01, "Nouspikel IDE Controller" )
109//      PORT_CONFSETTING(    0x02, "WHTech SCSI Controller" )
110   PORT_CONFNAME( 0x08, 0x00, "USB-SM card" )
111      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
112      PORT_CONFSETTING(    0x08, DEF_STR( On ) )
113   // AMS RAM (1 Mib)
114   UINT16 *m_ram;
113115
114   PORT_START( "SERIAL" )
115   PORT_CONFNAME( 0x03, 0x00, "Serial/Parallel interface" )
116      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
117      PORT_CONFSETTING(    0x01, "TI RS-232 card" )
116   // Scratch pad ram (1 KiB)
117   UINT16 *m_scratchpad;
118118
119   PORT_START( "SERIALMAP" )
120   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" ) PORT_CONDITION( "SERIAL", 0x03, PORTCOND_NOTEQUALS, 0x00 )
121      PORT_CONFSETTING(    0x00, "6-20" )
122      PORT_CONFSETTING(    0x01, "8-20" )
123      PORT_CONFSETTING(    0x02, "5-20" )
119private:
120   DECLARE_READ16_MEMBER( datamux_read );
121   DECLARE_WRITE16_MEMBER( datamux_write );
122   void   set_key(int number, int data);
123   int      m_keyboard_column;
124   int      m_alphalock_line;
124125
125   PORT_START( "EXTCARD" )
126   PORT_CONFNAME( 0x03, 0x02, "HSGPL extension" )
127      PORT_CONFSETTING(    0x02, DEF_STR( On ) )
126   // True if SGCPU DSR is enabled
127   bool m_internal_dsr;
128128
129   // We do not want to show this setting; makes only sense for Geneve
130   PORT_START( "MODE" )
131   PORT_CONFNAME( 0x01, 0x00, "Ext. cards modification" ) PORT_CONDITION( "HFDCDIP", 0xff, PORTCOND_EQUALS, GM_NEVER )
132      PORT_CONFSETTING(    0x00, "Standard" )
133      PORT_CONFSETTING(    GENMOD, "GenMod" )
129   // True if SGCPU rom6 is enabled
130   bool m_internal_rom6;
134131
135   PORT_START( "BWGDIP1" )
136   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
137      PORT_DIPSETTING( 0x00, "6 ms")
138      PORT_DIPSETTING( 0x01, "20 ms")
132   // Offset to the ROM6 bank.
133   int m_rom6_bank;
139134
140   PORT_START( "BWGDIP2" )
141   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
142      PORT_DIPSETTING( 0x00, "Hide")
143      PORT_DIPSETTING( 0x01, "Show")
135   // TRUE when mapper is active
136   bool m_map_mode;
144137
145   PORT_START( "BWGDIP34" )
146   PORT_DIPNAME( 0x03, 0x00, "BwG drives" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
147      PORT_DIPSETTING( 0x00, "DSK1 only")
148      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
149      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
150      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
138   // TRUE when mapper registers are accessible
139   bool m_access_mapper;
151140
152   PORT_START( "HFDCDIP" )
153   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
154      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
155      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
156      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
157      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
141   UINT8   m_lowbyte;
142   UINT8   m_highbyte;
143   UINT8   m_latch;
158144
159   PORT_START( "V9938-MEM" )
160   PORT_CONFNAME( 0x01, 0x00, "V9938 RAM size" )
161      PORT_CONFSETTING(   0x00, "128 KiB" )
162      PORT_CONFSETTING(   0x01, "192 KiB" )
145   /* Mapper registers */
146   UINT8 m_mapper[16];
147};
163148
164   PORT_START( "DRVSPD" )
165   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
166      PORT_CONFSETTING( 0x00, "No delay")
167      PORT_CONFSETTING( 0x01, "Realistic")
149static ADDRESS_MAP_START(memmap, AS_PROGRAM, 16, ti99_4p)
150   AM_RANGE(0x0000, 0xffff) AM_READWRITE( memread, memwrite )
151ADDRESS_MAP_END
168152
169   PORT_START( "EVPC-SW1" )
170   PORT_DIPNAME( 0x01, 0x00, "EVPC video mode" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
171      PORT_DIPSETTING(    0x00, "PAL" )
172      PORT_DIPSETTING(    0x01, "NTSC" )
153static ADDRESS_MAP_START(cru_map, AS_IO, 8, ti99_4p)
154   AM_RANGE(0x0000, 0x003f) AM_DEVREAD(TMS9901_TAG, tms9901_device, read)
155   AM_RANGE(0x0000, 0x01ff) AM_READ( cruread )
173156
174   PORT_START( "EVPC-SW3" )
175   PORT_DIPNAME( 0x01, 0x00, "EVPC charset" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
176      PORT_DIPSETTING(    0x00, DEF_STR( International ))
177      PORT_DIPSETTING(    0x01, DEF_STR( German ))
157   AM_RANGE(0x0000, 0x01ff) AM_DEVWRITE(TMS9901_TAG, tms9901_device, write)
158   AM_RANGE(0x0000, 0x0fff) AM_WRITE( cruwrite )
159ADDRESS_MAP_END
178160
179   PORT_START( "EVPC-SW4" )
180   PORT_DIPNAME( 0x01, 0x00, "EVPC VDP RAM" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
181      PORT_DIPSETTING(    0x00, "shifted" )
182      PORT_DIPSETTING(    0x01, "not shifted" )
161/*
162    Input ports, used by machine code for TI keyboard and joystick emulation.
183163
184   PORT_START( "EVPC-SW8" )
185   PORT_DIPNAME( 0x01, 0x00, "EVPC Configuration" )
186      PORT_DIPSETTING(    0x00, "DIP" )
187      PORT_DIPSETTING(    0x01, "NOVRAM" )
164    Since the keyboard microcontroller is not emulated, we use the TI99/4a 48-key keyboard,
165    plus two optional joysticks.
166*/
188167
168static INPUT_PORTS_START(ti99_4p)
189169   /* 3 ports for mouse */
190170   PORT_START("MOUSEX") /* Mouse - X AXIS */
191171      PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
r14513r14514
277257
278258INPUT_PORTS_END
279259
260/*
261    Memory access
262*/
263READ16_MEMBER( ti99_4p::memread )
264{
265   int addroff = offset << 1;
266   if (m_rom0 == NULL) return 0;   // premature access
267
268   UINT16 zone = addroff & 0xe000;
269   UINT16 value = 0;
270
271   if (zone==0x0000)
272   {
273      // ROM0
274      value = m_rom0[(addroff & 0x1fff)>>1];
275      return value;
276   }
277   if (zone==0x2000 || zone==0xa000 || zone==0xc000 || zone==0xe000)
278   {
279      value = samsmem_read(space, offset, mem_mask);
280      return value;
281   }
282
283   if (zone==0x4000)
284   {
285      if (m_internal_dsr)
286      {
287         value = m_dsr[(addroff & 0x1fff)>>1];
288         return value;
289      }
290      else
291      {
292         if (m_access_mapper && ((addroff & 0xffe0)==0x4000))
293         {
294            value = m_mapper[offset & 0x000f]<<8;
295            return value;
296         }
297      }
298   }
299
300   if (zone==0x6000 && m_internal_rom6)
301   {
302      if (m_rom6_bank==0)
303         value = m_rom6a[(addroff & 0x1fff)>>1];
304      else
305         value = m_rom6b[(addroff & 0x1fff)>>1];
306
307      return value;
308   }
309
310   // Scratch pad RAM and sound
311   // speech is in peribox
312   // groms are in hsgpl in peribox
313   if (zone==0x8000)
314   {
315      if ((addroff & 0xfff0)==0x8400)   // cannot read from sound
316      {
317         value = 0;
318         return value;
319      }
320      if ((addroff & 0xfc00)==0x8000)
321      {
322         value = m_scratchpad[(addroff & 0x03ff)>>1];
323         return value;
324      }
325      // Video: 8800, 8802
326      if ((addroff & 0xfffd)==0x8800)
327      {
328         value = m_video->read16(space, offset, mem_mask);
329         return value;
330      }
331   }
332
333   // If we are here, check the peribox via the datamux
334   // catch-all for unmapped zones
335   value = datamux_read(space, offset, mem_mask);
336   return value;
337}
338
339WRITE16_MEMBER( ti99_4p::memwrite )
340{
341//  device_adjust_icount(m_cpu, -4);
342
343   int addroff = offset << 1;
344   UINT16 zone = addroff & 0xe000;
345
346   if (zone==0x0000)
347   {
348      // ROM0
349      if (VERBOSE>4) LOG("sgcpu: ignoring ROM write access at %04x\n", addroff);
350      return;
351   }
352
353   if (zone==0x2000 || zone==0xa000 || zone==0xc000 || zone==0xe000)
354   {
355      samsmem_write(space, offset, data, mem_mask);
356      return;
357   }
358
359   if (zone==0x4000)
360   {
361      if (m_internal_dsr)
362      {
363         if (VERBOSE>4) LOG("sgcpu: ignoring DSR write access at %04x\n", addroff);
364         return;
365      }
366      else
367      {
368         if (m_access_mapper && ((addroff & 0xffe0)==0x4000))
369         {
370            COMBINE_DATA(&m_mapper[offset & 0x000f]);
371            return;
372         }
373      }
374   }
375
376   if (zone==0x6000 && m_internal_rom6)
377   {
378      m_rom6_bank = offset & 0x0001;
379      return;
380   }
381
382   // Scratch pad RAM and sound
383   // speech is in peribox
384   // groms are in hsgpl in peribox
385   if (zone==0x8000)
386   {
387      if ((addroff & 0xfff0)==0x8400)      //sound write
388      {
389         m_sound->write(space, 0, (data >> 8) & 0xff);
390         return;
391      }
392      if ((addroff & 0xfc00)==0x8000)
393      {
394         COMBINE_DATA(&m_scratchpad[(addroff & 0x03ff)>>1]);
395         return;
396      }
397      // Video: 8C00, 8C02
398      if ((addroff & 0xfffd)==0x8c00)
399      {
400         m_video->write16(space, offset, data, mem_mask);
401         return;
402      }
403   }
404
405   // If we are here, check the peribox via the datamux
406   // catch-all for unmapped zones
407   datamux_write(space, offset, data, mem_mask);
408}
409
410/***************************************************************************
411    Internal datamux; similar to TI-99/4A. However, here we have just
412    one device, the peripheral box, so it is much simpler.
413***************************************************************************/
414
415READ16_MEMBER( ti99_4p::datamux_read )
416{
417   UINT8 hbyte = 0;
418   UINT16 addroff = (offset << 1);
419
420   m_peribox->readz(space, addroff+1, &m_latch, mem_mask);
421   m_lowbyte = m_latch;
422
423   // Takes three cycles
424   device_adjust_icount(m_cpu, -3);
425
426   m_peribox->readz(space, addroff, &hbyte, mem_mask);
427   m_highbyte = hbyte;
428
429   // Takes three cycles
430   device_adjust_icount(m_cpu, -3);
431
432   // use the latch and the currently read byte and put it on the 16bit bus
433//  printf("read  address = %04x, value = %04x, memmask = %4x\n", addroff,  (hbyte<<8) | sgcpu->latch, mem_mask);
434   return (hbyte<<8) | m_latch ;
435}
436
437/*
438    Write access.
439    TODO: use the 16-bit expansion in the box for suitable cards
440*/
441WRITE16_MEMBER( ti99_4p::datamux_write )
442{
443   UINT16 addroff = (offset << 1);
444//  printf("write address = %04x, value = %04x, memmask = %4x\n", addroff, data, mem_mask);
445
446   // read more about the datamux in datamux.c
447
448   // byte-only transfer, high byte
449   // we use the previously read low byte to complete
450   if (mem_mask == 0xff00)
451      data = data | m_lowbyte;
452
453   // byte-only transfer, low byte
454   // we use the previously read high byte to complete
455   if (mem_mask == 0x00ff)
456      data = data | (m_highbyte << 8);
457
458   // Write to the PEB
459   m_peribox->write(space, addroff+1, data & 0xff);
460
461   // Takes three cycles
462   device_adjust_icount(m_cpu,-3);
463
464   // Write to the PEB
465   m_peribox->write(space, addroff, (data>>8) & 0xff);
466
467   // Takes three cycles
468   device_adjust_icount(m_cpu,-3);
469}
470
471/***************************************************************************
472   CRU interface
473***************************************************************************/
474
475#define MAP_CRU_BASE 0x0f00
476#define SAMS_CRU_BASE 0x1e00
477
478/*
479    CRU write
480*/
481WRITE8_MEMBER( ti99_4p::cruwrite )
482{
483   int addroff = offset<<1;
484
485   if ((addroff & 0xff00)==MAP_CRU_BASE)
486   {
487      if ((addroff & 0x000e)==0)   m_internal_dsr = data;
488      if ((addroff & 0x000e)==2)   m_internal_rom6 = data;
489      if ((addroff & 0x000e)==4)   m_peribox->senila((data!=0)? ASSERT_LINE : CLEAR_LINE);
490      if ((addroff & 0x000e)==6)   m_peribox->senilb((data!=0)? ASSERT_LINE : CLEAR_LINE);
491      // TODO: more CRU bits? 8=Fast timing / a=KBENA
492      return;
493   }
494   if ((addroff & 0xff00)==SAMS_CRU_BASE)
495   {
496      if ((addroff & 0x000e)==0) m_access_mapper = data;
497      if ((addroff & 0x000e)==2) m_map_mode = data;
498      return;
499   }
500
501   // No match - pass to peribox
502   m_peribox->cruwrite(addroff, data);
503}
504
505READ8_MEMBER( ti99_4p::cruread )
506{
507   UINT8 value = 0;
508   m_peribox->crureadz(offset<<4, &value);
509   return value;
510}
511
512/***************************************************************************
513   AMS Memory implementation
514***************************************************************************/
515
516/*
517    Memory read. The SAMS card has two address areas: The memory is at locations
518    0x2000-0x3fff and 0xa000-0xffff, and the mapper area is at 0x4000-0x401e
519    (only even addresses).
520*/
521READ16_MEMBER( ti99_4p::samsmem_read )
522{
523   UINT32 address = 0;
524   int addroff = offset << 1;
525
526   // select memory expansion
527   if (m_map_mode)
528      address = (m_mapper[(addroff>>12) & 0x000f] << 12) + (addroff & 0x0fff);
529   else // transparent mode
530      address = addroff;
531
532   return m_ram[address>>1];
533}
534
535/*
536    Memory write
537*/
538WRITE16_MEMBER( ti99_4p::samsmem_write )
539{
540   UINT32 address = 0;
541   int addroff = offset << 1;
542
543   // select memory expansion
544   if (m_map_mode)
545      address = (m_mapper[(addroff>>12) & 0x000f] << 12) + (addroff & 0x0fff);
546   else // transparent mode
547      address = addroff;
548
549   COMBINE_DATA(&m_ram[address>>1]);
550}
551
552/***************************************************************************
553    Keyboard/tape control
554****************************************************************************/
555static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3" };
556
557READ8_MEMBER( ti99_4p::read_by_9901 )
558{
559   int answer=0;
560
561   switch (offset & 0x03)
562   {
563   case TMS9901_CB_INT7:
564      // Read pins INT3*-INT7* of TI99's 9901.
565      //
566      // signification:
567      // (bit 1: INT1 status)
568      // (bit 2: INT2 status)
569      // bit 3-7: keyboard status bits 0 to 4
570      answer = ((input_port_read(machine(), keynames[m_keyboard_column >> 1]) >> ((m_keyboard_column & 1) * 8)) << 3) & 0xF8;
571      if (m_alphalock_line==false)
572         answer &= ~(input_port_read(*this, "ALPHA") << 3);
573      break;
574
575   case TMS9901_INT8_INT15:
576      // Read pins INT8*-INT15* of TI99's 9901.
577      // bit 0-2: keyboard status bits 5 to 7
578      // bit 3: tape input mirror
579      // bit 5-7: weird, not emulated
580      if (m_keyboard_column == 7)   answer = 0x07;
581      else            answer = ((input_port_read(machine(), keynames[m_keyboard_column >> 1]) >> ((m_keyboard_column & 1) * 8)) >> 5) & 0x07;
582      break;
583
584   case TMS9901_P0_P7:
585      break;
586
587   case TMS9901_P8_P15:
588      // Read pins P8-P15 of TI99's 9901.
589      // bit 26: high
590      // bit 27: tape input
591      answer = 4;
592      if (m_cassette->input() > 0) answer |= 8;
593      break;
594   }
595   return answer;
596}
597
598/*
599    WRITE key column select (P2-P4)
600*/
601void ti99_4p::set_key(int number, int data)
602{
603   if (data!=0)   m_keyboard_column |= 1 << number;
604   else         m_keyboard_column &= ~(1 << number);
605}
606
607WRITE_LINE_MEMBER( ti99_4p::keyC0 )
608{
609   set_key(0, state);
610}
611
612WRITE_LINE_MEMBER( ti99_4p::keyC1 )
613{
614   set_key(1, state);
615}
616
617WRITE_LINE_MEMBER( ti99_4p::keyC2 )
618{
619   set_key(2, state);
620}
621
622/*
623    WRITE alpha lock line (P5)
624*/
625WRITE_LINE_MEMBER( ti99_4p::alphaW )
626{
627   m_alphalock_line = state;
628}
629
630/*
631    command CS1 (only) tape unit motor (P6)
632*/
633WRITE_LINE_MEMBER( ti99_4p::cs_motor )
634{
635   m_cassette->change_state((state!=0)? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
636}
637
638/*
639    audio gate (P8)
640
641    Set to 1 before using tape: this enables the mixing of tape input sound
642    with computer sound.
643
644    We do not really need to emulate this as the tape recorder generates sound
645    on its own.
646*/
647WRITE_LINE_MEMBER( ti99_4p::audio_gate )
648{
649}
650
651/*
652    tape output (P9)
653*/
654WRITE_LINE_MEMBER( ti99_4p::cassette_output )
655{
656   m_cassette->output((state!=0)? +1 : -1);
657}
658
659WRITE8_MEMBER( ti99_4p::tms9901_interrupt )
660{
661   // offset contains the interrupt level (0-15)
662   if (data==ASSERT_LINE)
663   {
664      // The TMS9901 should normally be connected with the CPU by 5 wires:
665      // INTREQ* and IC0-IC3. The last four lines deliver the interrupt level.
666      // On the TI-99 systems these IC lines are not used; the input lines
667      // at the CPU are hardwired to level 1.
668      device_execute(m_cpu)->set_input_line_and_vector(0, ASSERT_LINE, 1);
669   }
670   else
671   {
672      device_execute(m_cpu)->set_input_line(0, CLEAR_LINE);
673   }
674}
675
676/* TMS9901 setup. The callback functions pass a reference to the TMS9901 as device. */
677const tms9901_interface tms9901_wiring_ti99_4p =
678{
679   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
680
681   // read handler
682   DEVCB_DRIVER_MEMBER(ti99_4p, read_by_9901),
683
684   {   // write handlers
685      DEVCB_NULL,
686      DEVCB_NULL,
687      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, keyC0),
688      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, keyC1),
689      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, keyC2),
690      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, alphaW),
691      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, cs_motor),
692      DEVCB_NULL,
693      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, audio_gate),
694      DEVCB_DRIVER_LINE_MEMBER(ti99_4p, cassette_output),
695      DEVCB_NULL,
696      DEVCB_NULL,
697      DEVCB_NULL,
698      DEVCB_NULL,
699      DEVCB_NULL,
700      DEVCB_NULL
701   },
702
703   /* interrupt handler */
704   DEVCB_DRIVER_MEMBER(ti99_4p, tms9901_interrupt)
705};
706
707/***************************************************************************
708    Control lines
709****************************************************************************/
710
711WRITE_LINE_MEMBER( ti99_4p::console_ready )
712{
713   if (VERBOSE>6) LOG("READY line set ... not yet connected, level=%02x\n", state);
714}
715
716WRITE_LINE_MEMBER( ti99_4p::extint )
717{
718   if (VERBOSE>6) LOG("EXTINT level = %02x\n", state);
719   if (m_tms9901 != NULL)
720      m_tms9901->set_single_int(1, state);
721}
722
723WRITE_LINE_MEMBER( ti99_4p::notconnected )
724{
725   if (VERBOSE>6) LOG("Setting a not connected line ... ignored\n");
726}
727
728/*****************************************************************************/
729
730static PERIBOX_CONFIG( peribox_conf )
731{
732   DEVCB_DRIVER_LINE_MEMBER(ti99_4p, extint),         // INTA
733   DEVCB_DRIVER_LINE_MEMBER(ti99_4p, notconnected),   // INTB
734   DEVCB_DRIVER_LINE_MEMBER(ti99_4p, console_ready),   // READY
735   0x70000                                    // Address bus prefix (AMA/AMB/AMC)
736};
737
280738DRIVER_INIT( ti99_4p )
281739{
282740}
283741
284742MACHINE_START( ti99_4p )
285743{
744   ti99_4p *driver = machine.driver_data<ti99_4p>();
745
746   driver->m_cpu = machine.device("maincpu");
747   driver->m_peribox = static_cast<peribox_device*>(machine.device(PERIBOX_TAG));
748   driver->m_sound = static_cast<ti_sound_system_device*>(machine.device(TISOUND_TAG));
749   driver->m_video = static_cast<ti_exp_video_device*>(machine.device(VIDEO_SYSTEM_TAG));
750   driver->m_cassette = static_cast<cassette_image_device*>(machine.device(CASSETTE_TAG));
751   driver->m_tms9901 = static_cast<tms9901_device*>(machine.device(TMS9901_TAG));
752
753   driver->m_ram = (UINT16*)machine.region(SAMSMEM_TAG)->base();
754   driver->m_scratchpad = (UINT16*)machine.region(PADMEM_TAG)->base();
755
756   driver->m_peribox->senila(CLEAR_LINE);
757   driver->m_peribox->senilb(CLEAR_LINE);
758
759   UINT16 *rom = (UINT16*)machine.region("maincpu")->base();
760   driver->m_rom0  = rom + 0x2000;
761   driver->m_dsr   = rom + 0x6000;
762   driver->m_rom6a = rom + 0x3000;
763   driver->m_rom6b = rom + 0x7000;
286764}
287765
288766/*
767    set the state of int2 (called by the v9938)
768*/
769void ti99_4p::set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state)
770{
771   m_tms9901->set_single_int(2, state);
772}
773
774/*
289775    Reset the machine.
290776*/
291777MACHINE_RESET( ti99_4p )
292778{
293   tms9901_set_single_int(machine.device("tms9901"), 12, 0);
779   ti99_4p *driver = machine.driver_data<ti99_4p>();
780   driver->m_tms9901->set_single_int(12, 0);
294781}
295782
296783TIMER_DEVICE_CALLBACK( ti99_4p_hblank_interrupt )
297784{
298   timer.machine().device<v9938_device>("video_v9938")->interrupt();
785   timer.machine().device<v9938_device>(V9938_TAG)->interrupt();
299786}
300787
301788/*
302789    Machine description.
303790*/
304static MACHINE_CONFIG_START( ti99_4p_60hz, ti99_4p_state )
791static MACHINE_CONFIG_START( ti99_4p_60hz, ti99_4p )
305792   /* basic machine hardware */
306793   /* TMS9900 CPU @ 3.0 MHz */
307794   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
308795   MCFG_CPU_PROGRAM_MAP(memmap)
309796   MCFG_CPU_IO_MAP(cru_map)
310   MCFG_TIMER_ADD_SCANLINE("scantimer", ti99_4p_hblank_interrupt, "screen", 0, 1)   /* 262.5 in 60Hz, 312.5 in 50Hz */
797   MCFG_MACHINE_START( ti99_4p )
311798
312799   /* video hardware */
313800   /* FIXME: (MZ) Lowered the screen rate to 30 Hz. This is a quick hack to
314801    restore normal video speed for V9938-based systems until the V9938 implementation
315802    is properly fixed. */
316   MCFG_TI_V9938_ADD("video", 30, "screen", 2500, 512+32, (212+28)*2, tms9901_sg_set_int2)
803   MCFG_TI_V9938_ADD(VIDEO_SYSTEM_TAG, 30, SCREEN_TAG, 2500, 512+32, (212+28)*2, DEVICE_SELF, ti99_4p, set_tms9901_INT2_from_v9938)
804   MCFG_TIMER_ADD_SCANLINE("scantimer", ti99_4p_hblank_interrupt, SCREEN_TAG, 0, 1)
317805
318   MCFG_MACHINE_START( ti99_4p )
319   MCFG_MACHINE_RESET( ti99_4p )
806   /* tms9901 */
807   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4p, 3000000)
320808
809   /* Peripheral expansion box (SGCPU composition) */
810   MCFG_PERIBOX_SG_ADD( PERIBOX_TAG, peribox_conf )
811
321812   /* sound hardware */
322813   MCFG_SPEAKER_STANDARD_MONO("mono")
323   MCFG_SOUND_ADD("dac", DAC, 0)
324   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.20)
325   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
326   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
327   MCFG_SOUND_ADD("soundgen", SN76496, 3579545)   /* 3.579545 MHz */
814   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
328815   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
816   MCFG_TI_SOUND_ADD( TISOUND_TAG )
329817
330   /* tms9901 */
331   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4p)
818   /* Cassette drives */
819   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
332820
333   /* devices */
334   MCFG_PBOXSG_ADD( "peribox", card_extint, card_notconnected, card_ready )
335   MCFG_SGCPUB_ADD( "sgcpu_board" )
336   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
821   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
822   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
337823MACHINE_CONFIG_END
338824
339825
r14513r14514
342828   ROM_REGION16_BE(0x10000, "maincpu", 0)
343829   ROM_LOAD16_BYTE("sgcpu_hb.bin", 0x0000, 0x8000, CRC(aa100730) SHA1(35e585b2dcd3f2a0005bebb15ede6c5b8c787366) ) /* system ROMs */
344830   ROM_LOAD16_BYTE("sgcpu_lb.bin", 0x0001, 0x8000, CRC(2a5dc818) SHA1(dec141fe2eea0b930859cbe1ebd715ac29fa8ecb) ) /* system ROMs */
831   ROM_REGION16_BE(0x080000, SAMSMEM_TAG, 0)
832   ROM_FILL(0x000000, 0x080000, 0x0000)
833   ROM_REGION16_BE(0x0400, PADMEM_TAG, 0)
834   ROM_FILL(0x000000, 0x0400, 0x0000)
345835ROM_END
346836
347837/*    YEAR  NAME      PARENT   COMPAT   MACHINE      INPUT    INIT      COMPANY     FULLNAME */
src/mess/drivers/ti99_4x.c
r14513r14514
1/*
1/****************************************************************************
2
23    MESS Driver for TI-99/4 and TI-99/4A Home Computers.
34    Raphael Nabet, 1999-2003.
45
5    see machine/ti99_4x.c for some details and references
6*/
7
8/*
96    TI99/4 info:
107
118    Similar to TI99/4a, except for the following:
r14513r14514
1613    Historical notes: TI made several last minute design changes.
1714    * TI99/4 prototypes had an extra port for an I/R joystick and keypad interface.
1815    * early TI99/4 prototypes were designed for a tms9985, not a tms9900.
19*/
2016
17    Emulation architecture:
18    (also see datamux.c, peribox.c)
19
20              +---- video (upper 8 bits of databus)
21              |
22    CPU       |
23    TMS9900 ==##== datamux --------+------ peribox ----------- [8] slots with peripheral devices
24       |      ||   (16->8)         |
25       |      ||                   +------ Console GROMs (0, 1, 2)
26       |      ##=== Console ROM    |
27    TMS9901   ||                   +------ gromport (cartridge port)
28     (I/O)    ##=== 256 byte RAM   |
29              ||                   +------ sound
30              ##=== 32KiB RAM
31                    unofficial mod
32                    (16 bit)
33
34    Michael Zapf
35
36    February 2012: Rewritten as class
37
38*****************************************************************************/
39
40#define ADDRESS_MAP_MODERN
41
2142#include "emu.h"
2243#include "cpu/tms9900/tms9900.h"
2344#include "sound/sn76496.h"
r14513r14514
2748#include "machine/tms9901.h"
2849#include "imagedev/cassette.h"
2950
30#include "machine/ti99/tiboard.h"
31
3251#include "machine/ti99/videowrp.h"
33
34#include "machine/ti99/peribox.h"
35#include "machine/ti99/crubus.h"
3652#include "machine/ti99/datamux.h"
3753#include "machine/ti99/grom.h"
3854#include "machine/ti99/gromport.h"
3955#include "machine/ti99/mecmouse.h"
4056#include "machine/ti99/handset.h"
57#include "machine/ti99/peribox.h"
4158
59#define LOG logerror
60#define VERBOSE 1
4261
43class ti99_4x_state : public driver_device
62/*
63    The console.
64*/
65class ti99_4x : public driver_device
4466{
4567public:
46   ti99_4x_state(const machine_config &mconfig, device_type type, const char *tag)
68   ti99_4x(const machine_config &mconfig, device_type type, const char *tag)
4769      : driver_device(mconfig, type, tag) { }
4870
71   // CRU (Communication Register Unit) handling
72   DECLARE_READ8_MEMBER(cruread);
73   DECLARE_WRITE8_MEMBER(cruwrite);
74
75   // Forwarding interrupts to the CPU or CRU
76   DECLARE_WRITE_LINE_MEMBER( console_ready );
77   DECLARE_WRITE_LINE_MEMBER( set_tms9901_INT2 );
78   DECLARE_WRITE_LINE_MEMBER( set_tms9901_INT12 );
79   void set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state);
80   DECLARE_WRITE_LINE_MEMBER( extint );
81   DECLARE_WRITE_LINE_MEMBER( notconnected );
82
83   // Some values to keep
84   int               m_mode;
85   device_t         *m_cpu;
86   tms9901_device      *m_tms9901;
87   gromport_device      *m_gromport;
88   peribox_device      *m_peribox;
89   mecmouse_device    *m_mecmouse;
90   ti99_handset_device *m_handset;
91
92   // Connections with the system interface TMS9901
93   DECLARE_READ8_MEMBER(read_by_9901);
94   DECLARE_WRITE_LINE_MEMBER(keyC0);
95   DECLARE_WRITE_LINE_MEMBER(keyC1);
96   DECLARE_WRITE_LINE_MEMBER(keyC2);
97   DECLARE_WRITE_LINE_MEMBER(cs1_motor);
98   DECLARE_WRITE_LINE_MEMBER(audio_gate);
99   DECLARE_WRITE_LINE_MEMBER(cassette_output);
100   DECLARE_WRITE8_MEMBER(tms9901_interrupt);
101   DECLARE_WRITE_LINE_MEMBER(handset_ack);
102   DECLARE_WRITE_LINE_MEMBER(cs2_motor);
103   DECLARE_WRITE_LINE_MEMBER(alphaW);
104
105private:
106   void            set_key(int number, int data);
107   int               m_keyboard_column;
108   int               m_alphalock_line;
49109};
50110
51
52111/*
53112    Memory map
54113*/
55static ADDRESS_MAP_START(memmap, AS_PROGRAM, 16)
114static ADDRESS_MAP_START(memmap, AS_PROGRAM, 16, ti99_4x)
56115   ADDRESS_MAP_GLOBAL_MASK(0xffff)
57116   AM_RANGE(0x0000, 0x1fff) AM_ROM                              /*system ROM*/
58117   AM_RANGE(0x8000, 0x80ff) AM_MIRROR(0x0300) AM_RAM         /*RAM PAD, mirrored 4 times*/
59   AM_RANGE(0x8800, 0x8bff) AM_DEVREADWRITE("video", ti_tms991x_r16, ti_video_wnop)   /*vdp read*/
60   AM_RANGE(0x8C00, 0x8fff) AM_DEVREADWRITE("video", ti_video_rnop, ti_tms991x_w16)   /*vdp write*/
61   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE("datamux_16_8", ti99_dmux_r, ti99_dmux_w)
118   AM_RANGE(0x8800, 0x8bff) AM_DEVREADWRITE(VIDEO_SYSTEM_TAG, ti_std_video_device, read16, nowrite)   /*vdp read*/
119   AM_RANGE(0x8C00, 0x8fff) AM_DEVREADWRITE(VIDEO_SYSTEM_TAG, ti_std_video_device, noread, write16)   /*vdp write*/
120   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE(DATAMUX_TAG, ti99_datamux_device, read, write)
62121ADDRESS_MAP_END
63122
64static ADDRESS_MAP_START(memmap_4ev, AS_PROGRAM, 16)
123static ADDRESS_MAP_START(memmap_4ev, AS_PROGRAM, 16, ti99_4x)
65124   ADDRESS_MAP_GLOBAL_MASK(0xffff)
66125   AM_RANGE(0x0000, 0x1fff) AM_ROM                              /*system ROM*/
67126   AM_RANGE(0x8000, 0x80ff) AM_MIRROR(0x0300) AM_RAM         /*RAM PAD, mirrored 4 times*/
68   AM_RANGE(0x8800, 0x8bff) AM_DEVREADWRITE("video", ti_v9938_r16, ti_video_wnop)   /*vdp read*/
69   AM_RANGE(0x8C00, 0x8fff) AM_DEVREADWRITE("video", ti_video_rnop, ti_v9938_w16)   /*vdp write*/
70   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE("datamux_16_8", ti99_dmux_r, ti99_dmux_w)
127   AM_RANGE(0x8800, 0x8bff) AM_DEVREADWRITE(VIDEO_SYSTEM_TAG, ti_exp_video_device, read16, nowrite)   /*vdp read*/
128   AM_RANGE(0x8C00, 0x8fff) AM_DEVREADWRITE(VIDEO_SYSTEM_TAG, ti_exp_video_device, noread, write16)   /*vdp write*/
129   AM_RANGE(0x0000, 0xffff) AM_DEVREADWRITE(DATAMUX_TAG, ti99_datamux_device, read, write)
71130ADDRESS_MAP_END
72131
73132/*
r14513r14514
78137    The TMS9901 is incompletely decoded
79138    ---0 00xx xxcc ccc0
80139    causing 16 mirrors (0000, 0040, 0080, 00c0, ... , 03c0)
81    Note that the CRU write address here is shifted 1 right (counting bit positions)
82    while the CRU reads address 8 bits in one go
140
141    Reading is done by transfering 8 successive bits, so addresses refer to
142    8 bit groups; writing, however, is done using output lines. The CRU base
143    address in the ti99 systems is twice the bit address:
144
145    (base=0, bit=0x10) == (base=0x20,bit=0)
146
147    Read: 0000 - 003f translates to base addresses 0000 - 03fe
148          0000 - 01ff is the complete CRU address space 0000 - 1ffe (for TMS9900)
149
150    Write:0000 - 01ff corresponds to bit 0 of base address 0000 - 03fe
83151*/
84static ADDRESS_MAP_START(cru_map, AS_IO, 8)
85   AM_RANGE(0x0000, 0x003f) AM_DEVREAD("tms9901", tms9901_cru_r)
86   AM_RANGE(0x0000, 0x01ff) AM_DEVREAD("crubus", ti99_crubus_r )
152static ADDRESS_MAP_START(cru_map, AS_IO, 8, ti99_4x)
153   AM_RANGE(0x0000, 0x003f) AM_DEVREAD(TMS9901_TAG, tms9901_device, read)
154   AM_RANGE(0x0000, 0x01ff) AM_READ(cruread)
87155
88   AM_RANGE(0x0000, 0x01ff) AM_DEVWRITE("tms9901", tms9901_cru_w)
89   AM_RANGE(0x0000, 0x0fff) AM_DEVWRITE("crubus", ti99_crubus_w )
156   AM_RANGE(0x0000, 0x01ff) AM_DEVWRITE(TMS9901_TAG, tms9901_device, write)
157   AM_RANGE(0x0000, 0x0fff) AM_WRITE(cruwrite)
90158ADDRESS_MAP_END
91159
92/*
93    Input ports, used by machine code for TI keyboard and joystick emulation.
94*/
95160
96static INPUT_CHANGED( gk_changed )
97{
98   device_t *cartsys = field.machine().device("gromport");
99   set_gk_switches(cartsys, (UINT8)((UINT64)param&0x07), newval);
100}
101
161/*****************************************************************************
162    Input ports
163 ****************************************************************************/
102164/* TI99/4a: 48-key keyboard, plus two optional joysticks (2 shift keys) */
103165static INPUT_PORTS_START(ti99_4a)
104166
105167   PORT_START( "RAM" )   /* config */
106   PORT_CONFNAME( 0x07, 0x01, "RAM extension" )
107      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
108      PORT_CONFSETTING(    0x01, "Console 32 KiB (16 bit)" )
109      PORT_CONFSETTING(    0x02, "TI 32 KiB card" )
110      PORT_CONFSETTING(    0x03, "Super AMS 1MiB" )
111      PORT_CONFSETTING(    0x07, "Myarc 512 KiB" )
112
113   PORT_START( "SPEECH" )
114   PORT_CONFNAME( 0x01, 0x01, "Speech synthesizer" )
115      PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
116      PORT_CONFSETTING( 0x01, DEF_STR( On ) )
117
118   PORT_START( "DISKCTRL" )
119   PORT_CONFNAME( 0x07, 0x03, "Disk controller" )
120      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
121      PORT_CONFSETTING(    0x01, "TI SD Floppy Controller" )
122      PORT_CONFSETTING(    0x02, "SNUG BwG Controller" )
123      PORT_CONFSETTING(    0x03, "Myarc HFDC" )
124
125   PORT_START( "HDCTRL" )
126   PORT_CONFNAME( 0x03, 0x00, "HD controller" )
127      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
128      PORT_CONFSETTING(    0x01, "Nouspikel IDE Controller" )
129//      PORT_CONFSETTING(    0x02, "WHTech SCSI Controller" )
130   PORT_CONFNAME( 0x04, 0x00, "USB-SM card" )
168   PORT_CONFNAME( 0x01, 0x09, "Console 32 KiB RAM upgrade (16 bit)" )
131169      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
132      PORT_CONFSETTING(    0x04, DEF_STR( On ) )
170      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
133171
134   PORT_START( "SERIAL" )
135   PORT_CONFNAME( 0x03, 0x00, "Serial/Parallel interface" )
136      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
137      PORT_CONFSETTING(    0x01, "TI RS-232 card" )
138
139   PORT_START( "SERIALMAP" )
140   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" ) PORT_CONDITION( "SERIAL", 0x03, PORTCOND_NOTEQUALS, 0x00 )
141      PORT_CONFSETTING(    0x00, "6-20" )
142      PORT_CONFSETTING(    0x01, "8-20" )
143      PORT_CONFSETTING(    0x02, "5-20" )
144
145   /* Flash setting is used to flash an empty HSGPL DSR ROM */
146   PORT_START( "EXTCARD" )
147   PORT_CONFNAME( 0x03, 0x00, "HSGPL extension" )
172   PORT_START( "GROMENA" )
173   PORT_CONFNAME( 0x01, 0x01, "Console GROMs" )
148174      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
149      PORT_CONFSETTING(    0x01, "Flash" )
150      PORT_CONFSETTING(    0x02, DEF_STR( On ) )
151   PORT_CONFNAME( 0x04, 0x00, "P-Code card" )
152      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
153      PORT_CONFSETTING(    0x04, DEF_STR( On ) )
175      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
154176
155   PORT_START( "HCI" )
156   PORT_CONFNAME( 0x01, 0x00, "Mouse support" )
177   PORT_START( "MECMOUSE" )
178   PORT_CONFNAME( 0x01, 0x00, "Mechatronics Mouse" )
157179      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
158      PORT_CONFSETTING(    0x01, "Mechatronics Mouse" )
180      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
181/*
182    // We do not want to show this setting; makes only sense for Geneve
183    PORT_START( "MODE" )
184    PORT_CONFNAME( 0x01, 0x00, "Ext. cards modification" ) PORT_CONDITION( "HFDCDIP", 0x0f, PORTCOND_EQUALS, GM_NEVER )
185        PORT_CONFSETTING(    0x00, "Standard" )
186        PORT_CONFSETTING(    GENMOD, "GenMod" )
187*/
159188
160   // We do not want to show this setting; makes only sense for Geneve
161   PORT_START( "MODE" )
162   PORT_CONFNAME( 0x01, 0x00, "Ext. cards modification" ) PORT_CONDITION( "HFDCDIP", 0x0f, PORTCOND_EQUALS, GM_NEVER )
163      PORT_CONFSETTING(    0x00, "Standard" )
164      PORT_CONFSETTING(    GENMOD, "GenMod" )
165
166   PORT_START( "CARTSLOT" )
167   PORT_DIPNAME( 0x0f, CART_AUTO, "Cartridge slot" )
168      PORT_DIPSETTING(    CART_AUTO, "Auto" )
169      PORT_DIPSETTING(    CART_1, "Slot 1" )
170      PORT_DIPSETTING(    CART_2, "Slot 2" )
171      PORT_DIPSETTING(    CART_3, "Slot 3" )
172      PORT_DIPSETTING(    CART_4, "Slot 4" )
173      PORT_DIPSETTING(    CART_GK, "GRAM Kracker" )
174
175   /* GRAM Kracker Support */
176   PORT_START( "GKSWITCH1" )
177   PORT_DIPNAME( 0x01, GK_NORMAL, "GK switch 1" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f )  PORT_CHANGED( gk_changed, (void *)1)
178      PORT_DIPSETTING(    GK_OFF, "GK Off" )
179      PORT_DIPSETTING(    GK_NORMAL, DEF_STR( Normal ) )
180
181   PORT_START( "GKSWITCH2" )
182   PORT_DIPNAME( 0x01, GK_OPSYS, "GK switch 2" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f )  PORT_CHANGED( gk_changed, (void *)2)
183      PORT_DIPSETTING(    GK_GRAM0, "GRAM 0" )
184      PORT_DIPSETTING(    GK_OPSYS, "Op Sys" )
185
186   PORT_START( "GKSWITCH3" )
187   PORT_DIPNAME( 0x01, GK_TIBASIC, "GK switch 3" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f )  PORT_CHANGED( gk_changed, (void *)3)
188      PORT_DIPSETTING(    GK_GRAM12, "GRAM 1-2" )
189      PORT_DIPSETTING(    GK_TIBASIC, "TI BASIC" )
190
191   PORT_START( "GKSWITCH4" )
192   PORT_DIPNAME( 0x03, GK_WP, "GK switch 4" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f )  PORT_CHANGED( gk_changed, (void *)4)
193      PORT_DIPSETTING(    GK_BANK1, "Bank 1" )
194      PORT_DIPSETTING(    GK_WP, "W/P" )
195      PORT_DIPSETTING(    GK_BANK2, "Bank 2" )
196
197   PORT_START( "GKSWITCH5" )
198   PORT_DIPNAME( 0x01, GK_LDON, "GK switch 5" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f )  PORT_CHANGED( gk_changed, (void *)5)
199      PORT_DIPSETTING(    GK_LDON, "Loader On" )
200      PORT_DIPSETTING(    GK_LDOFF, "Loader Off" )
201
202   PORT_START( "BWGDIP1" )
203   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
204      PORT_DIPSETTING( 0x00, "6 ms")
205      PORT_DIPSETTING( 0x01, "20 ms")
206
207   PORT_START( "BWGDIP2" )
208   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
209      PORT_DIPSETTING( 0x00, "Hide")
210      PORT_DIPSETTING( 0x01, "Show")
211
212   PORT_START( "BWGDIP34" )
213   PORT_DIPNAME( 0x03, 0x00, "BwG drives" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
214      PORT_DIPSETTING( 0x00, "DSK1 only")
215      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
216      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
217      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
218
219   PORT_START( "HFDCDIP" )
220   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
221      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
222      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
223      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
224      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
225
226   PORT_START( "DRVSPD" )
227   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
228      PORT_CONFSETTING( 0x00, "No delay")
229      PORT_CONFSETTING( 0x01, "Realistic")
230
231189   /* 3 ports for mouse */
232190   PORT_START("MOUSEX") /* Mouse - X AXIS */
233191      PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
r14513r14514
321279
322280INPUT_PORTS_END
323281
324/*
325    Input ports for the TI-99/4A with EPVC
326    Same as above, but with some additional dip switches
327*/
328static INPUT_PORTS_START(ti99_4ev)
329   PORT_INCLUDE(ti99_4a)
330   PORT_START( "EVPC-SW1" )
331   PORT_DIPNAME( 0x01, 0x00, "EVPC video mode" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
332      PORT_DIPSETTING(    0x00, "PAL" )
333      PORT_DIPSETTING(    0x01, "NTSC" )
334
335   PORT_START( "EVPC-SW3" )
336   PORT_DIPNAME( 0x01, 0x00, "EVPC charset" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
337      PORT_DIPSETTING(    0x00, DEF_STR( International ))
338      PORT_DIPSETTING(    0x01, DEF_STR( German ))
339
340   PORT_START( "EVPC-SW4" )
341   PORT_DIPNAME( 0x01, 0x00, "EVPC VDP RAM" ) PORT_CONDITION( "EVPC-SW8", 0x01, PORTCOND_EQUALS, 0x00 )
342      PORT_DIPSETTING(    0x00, "shifted" )
343      PORT_DIPSETTING(    0x01, "not shifted" )
344
345   PORT_START( "EVPC-SW8" )
346   PORT_DIPNAME( 0x01, 0x00, "EVPC Configuration" )
347      PORT_DIPSETTING(    0x00, "DIP" )
348      PORT_DIPSETTING(    0x01, "NOVRAM" )
349
350   PORT_START( "V9938-MEM" )
351   PORT_CONFNAME( 0x01, 0x00, "V9938 RAM size" )
352      PORT_CONFSETTING(   0x00, "128 KiB" )
353      PORT_CONFSETTING(   0x01, "192 KiB" )
354
355INPUT_PORTS_END
356
282/* TI99/4: 41-key keyboard, plus two optional joysticks  (2 space keys) */
357283#define JOYSTICK_DELTA         10
358284#define JOYSTICK_SENSITIVITY   100
359285
360/* TI99/4: 41-key keyboard, plus two optional joysticks  (2 space keys) */
361286static INPUT_PORTS_START(ti99_4)
362287
363288   PORT_START( "RAM" )   /* config */
364   PORT_CONFNAME( 0x07, 0x01, "RAM extension" )
365      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
366      PORT_CONFSETTING(    0x01, "Console 32 KiB (16 bit)" )
367      PORT_CONFSETTING(    0x02, "TI 32 KiB card" )
368      PORT_CONFSETTING(    0x03, "Super AMS 1MiB" )
369      PORT_CONFSETTING(    0x07, "Myarc 512 KiB" )
289   PORT_CONFNAME( 0x01, 0x09, "Console 32 KiB RAM upgrade (16 bit)" )
290      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
291      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
370292
371   PORT_START( "SPEECH" )
372   PORT_CONFNAME( 0x01, 0x01, "Speech synthesizer" )
373      PORT_CONFSETTING( 0x00, DEF_STR( Off ) )
374      PORT_CONFSETTING( 0x01, DEF_STR( On ) )
375
376   PORT_START( "DISKCTRL" )
377   PORT_CONFNAME( 0x07, 0x03, "Disk controller" )
378      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
379      PORT_CONFSETTING(    0x01, "TI SD Floppy Controller" )
380      PORT_CONFSETTING(    0x02, "SNUG BwG Controller" )
381      PORT_CONFSETTING(    0x03, "Myarc HFDC" )
382//      PORT_CONFSETTING(    0x04, "Corcomp" )
383
384   PORT_START( "HDCTRL" )
385   PORT_CONFNAME( 0x03, 0x00, "HD controller" )
386      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
387      PORT_CONFSETTING(    0x01, "Nouspikel IDE Controller" )
388//      PORT_CONFSETTING(    0x02, "WHTech SCSI Controller" )
389   PORT_CONFNAME( 0x08, 0x00, "USB-SM card" )
293   PORT_START( "GROMENA" )
294   PORT_CONFNAME( 0x01, 0x01, "Console GROMs" )
390295      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
391      PORT_CONFSETTING(    0x08, DEF_STR( On ) )
296      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
392297
393   PORT_START( "SERIAL" )
394   PORT_CONFNAME( 0x03, 0x00, "Serial/Parallel interface" )
395      PORT_CONFSETTING(    0x00, DEF_STR( None ) )
396      PORT_CONFSETTING(    0x01, "TI RS-232 card" )
397
398   PORT_START( "SERIALMAP" )
399   PORT_CONFNAME( 0x03, 0x00, "Serial cable pin mapping" ) PORT_CONDITION( "SERIAL", 0x03, PORTCOND_NOTEQUALS, 0x00 )
400      PORT_CONFSETTING(    0x00, "6-20" )
401      PORT_CONFSETTING(    0x01, "8-20" )
402      PORT_CONFSETTING(    0x02, "5-20" )
403
404   PORT_START( "EXTCARD" )
405   PORT_CONFNAME( 0x03, 0x00, "HSGPL extension" )
298   PORT_START( "MECMOUSE" )
299   PORT_CONFNAME( 0x01, 0x00, "Mechatronics Mouse" )
406300      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
407      PORT_CONFSETTING(    0x01, "Flash" )
408      PORT_CONFSETTING(    0x02, DEF_STR( On ) )
301      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
409302
410   PORT_START( "HCI" )
411   PORT_CONFNAME( 0x01, 0x00, "Mouse support" )
303   PORT_START( "HANDSET" )
304   PORT_CONFNAME( 0x01, 0x00, "IR Handset" )
412305      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
413      PORT_CONFSETTING(    0x01, "Mechatronics Mouse" )
414   PORT_CONFNAME( 0x04, 0x00, "IR Handset" )
415      PORT_CONFSETTING(    0x00, DEF_STR( Off ) )
416      PORT_CONFSETTING(    0x04, DEF_STR( On ) )
306      PORT_CONFSETTING(    0x01, DEF_STR( On ) )
417307
418   PORT_START( "CARTSLOT" )
419   PORT_DIPNAME( 0x0f, 0x00, "Cartridge slot" )
420      PORT_DIPSETTING(    0x00, "Auto" )
421      PORT_DIPSETTING(    0x01, "Slot 1" )
422      PORT_DIPSETTING(    0x02, "Slot 2" )
423      PORT_DIPSETTING(    0x03, "Slot 3" )
424      PORT_DIPSETTING(    0x04, "Slot 4" )
425      PORT_DIPSETTING(    0x0f, "GRAM Kracker" )
426
427   /* GRAM Kracker Support */
428   PORT_START( "GKSWITCH1" )
429   PORT_DIPNAME( 0x01, 0x01, "GK switch 1" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED( gk_changed, (void *)1)
430      PORT_DIPSETTING(    0x00, "GK Off" )
431      PORT_DIPSETTING(    0x01, DEF_STR( Normal ) )
432
433   PORT_START( "GKSWITCH2" )
434   PORT_DIPNAME( 0x01, 0x01, "GK switch 2" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED( gk_changed, (void *)2)
435      PORT_DIPSETTING(    0x00, "GRAM 0" )
436      PORT_DIPSETTING(    0x01, "Op Sys" )
437
438   PORT_START( "GKSWITCH3" )
439   PORT_DIPNAME( 0x01, 0x01, "GK switch 3" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED( gk_changed, (void *)3)
440      PORT_DIPSETTING(    0x00, "GRAM 1-2" )
441      PORT_DIPSETTING(    0x01, "TI BASIC" )
442
443   PORT_START( "GKSWITCH4" )
444   PORT_DIPNAME( 0x03, 0x01, "GK switch 4" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED( gk_changed, (void *)4)
445      PORT_DIPSETTING(    0x00, "Bank 1" )
446      PORT_DIPSETTING(    0x01, "W/P" )
447      PORT_DIPSETTING(    0x02, "Bank 2" )
448
449   PORT_START( "GKSWITCH5" )
450   PORT_DIPNAME( 0x01, 0x00, "GK switch 5" ) PORT_CONDITION( "CARTSLOT", 0x0f, PORTCOND_EQUALS, 0x0f ) PORT_CHANGED( gk_changed, (void *)5)
451      PORT_DIPSETTING(    0x00, "Loader On" )
452      PORT_DIPSETTING(    0x01, "Loader Off" )
453
454   PORT_START( "BWGDIP1" )
455   PORT_DIPNAME( 0x01, 0x00, "BwG step rate" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
456      PORT_DIPSETTING( 0x00, "6 ms")
457      PORT_DIPSETTING( 0x01, "20 ms")
458
459   PORT_START( "BWGDIP2" )
460   PORT_DIPNAME( 0x01, 0x00, "BwG date/time display" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
461      PORT_DIPSETTING( 0x00, "Hide")
462      PORT_DIPSETTING( 0x01, "Show")
463
464   PORT_START( "BWGDIP34" )
465   PORT_DIPNAME( 0x03, 0x00, "BwG drives" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x02 )
466      PORT_DIPSETTING( 0x00, "DSK1 only")
467      PORT_DIPSETTING( 0x01, "DSK1-DSK2")
468      PORT_DIPSETTING( 0x02, "DSK1-DSK3")
469      PORT_DIPSETTING( 0x03, "DSK1-DSK4")
470
471   PORT_START( "HFDCDIP" )
472   PORT_DIPNAME( 0xff, 0x55, "HFDC drive config" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
473      PORT_DIPSETTING( 0x00, "40 track, 16 ms")
474      PORT_DIPSETTING( 0xaa, "40 track, 8 ms")
475      PORT_DIPSETTING( 0x55, "80 track, 2 ms")
476      PORT_DIPSETTING( 0xff, "80 track HD, 2 ms")
477
478   // We do not want to show this setting; makes only sense for Geneve
479   PORT_START( "MODE" )
480   PORT_CONFNAME( 0x01, 0x00, "Ext. cards modification" ) PORT_CONDITION( "HFDCDIP", 0x0f, PORTCOND_EQUALS, GM_NEVER )
481      PORT_CONFSETTING(    0x00, "Standard" )
482      PORT_CONFSETTING(    GENMOD, "GenMod" )
483
484   PORT_START( "DRVSPD" )
485   PORT_CONFNAME( 0x01, 0x01, "Floppy and HD speed" ) PORT_CONDITION( "DISKCTRL", 0x07, PORTCOND_EQUALS, 0x03 )
486      PORT_CONFSETTING( 0x00, "No delay")
487      PORT_CONFSETTING( 0x01, "Realistic")
488
489308   /* 3 ports for mouse */
490309   PORT_START("MOUSEX") /* Mouse - X AXIS */
491310      PORT_BIT( 0xff, 0x00, IPT_TRACKBALL_X) PORT_SENSITIVITY(100) PORT_KEYDELTA(0) PORT_PLAYER(1)
r14513r14514
688507
689508INPUT_PORTS_END
690509
691/* F4 Character Displayer */
692static const gfx_layout ti99_6_charlayout =
510/*****************************************************************************
511    Components
512******************************************************************************/
513
514static GROM_CONFIG(grom0_config)
693515{
694   8, 6,               /* 8 x 6 characters */
695   64,               /* 64 characters */
696   1,               /* 1 bits per pixel */
697   { 0 },               /* no bitplanes */
698   /* x offsets */
699   { 0, 1, 2, 3, 4, 5, 6, 7 },
700   /* y offsets */
701   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8 },
702   8*6               /* every char takes 6 bytes */
516   false, 0, region_grom, 0x0000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_4x, console_ready)
703517};
704518
705static const gfx_layout ti99_7_charlayout =
519static GROM_CONFIG(grom1_config)
706520{
707   8, 7,               /* 8 x 7 characters */
708   95,               /* 95 characters */
709   1,               /* 1 bits per pixel */
710   { 0 },               /* no bitplanes */
711   /* x offsets */
712   { 0, 1, 2, 3, 4, 5, 6, 7 },
713   /* y offsets */
714   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8 },
715   8*7               /* every char takes 7 bytes */
521   false, 1, region_grom, 0x2000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_4x, console_ready)
716522};
717523
718static const gfx_layout ti99_8_charlayout =
524static GROM_CONFIG(grom2_config)
719525{
720   8, 8,               /* 8 x 8 characters */
721   64,               /* 64 characters */
722   1,               /* 1 bits per pixel */
723   { 0 },               /* no bitplanes */
724   /* x offsets */
725   { 0, 1, 2, 3, 4, 5, 6, 7 },
726   /* y offsets */
727   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
728   8*8               /* every char takes 8 bytes */
526   false, 2, region_grom, 0x4000, 0x1800, false, DEVCB_DRIVER_LINE_MEMBER(ti99_4x, console_ready)
729527};
730528
731static const gfx_layout ti99_c_charlayout =
529static GROMPORT_CONFIG(console_cartslot)
732530{
733   8, 8,               /* 8 x 8 characters */
734   1,               /* 1 characters */
735   1,               /* 1 bits per pixel */
736   { 0 },               /* no bitplanes */
737   /* x offsets */
738   { 0, 1, 2, 3, 4, 5, 6, 7 },
739   /* y offsets */
740   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8 },
741   8*8               /* every char takes 8 bytes */
531   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, console_ready)
742532};
743533
744static const gfx_layout ti99_24_charlayout =
534static PERIBOX_CONFIG( peribox_conf )
745535{
746   24, 24,               /* 24 x 24 characters */
747   1,               /* 1 characters */
748   1,               /* 1 bits per pixel */
749   { 0 },               /* no bitplanes */
750   /* x offsets */
751   { 0, 1, 2, 3, 4, 5, 6, 7, 64, 65, 66, 67, 68, 69, 70, 71, 128, 129, 130, 131, 132, 133, 134, 135 },
752   /* y offsets */
753   { 0*8, 1*8, 2*8, 3*8, 4*8, 5*8, 6*8, 7*8, 24*8, 25*8, 26*8, 27*8, 28*8, 29*8, 30*8, 31*8, 48*8, 49*8, 50*8, 51*8, 52*8, 53*8, 54*8, 55*8 },
754   24*24               /* every char takes 72 bytes */
536   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, extint),         // INTA
537   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, notconnected),   // INTB
538   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, console_ready),   // READY
539   0x70000                                    // Address bus prefix (AMA/AMB/AMC)
755540};
756541
757static GFXDECODE_START( ti99 )
758   GFXDECODE_ENTRY( region_grom, 0x0466, ti99_8_charlayout, 0, 8 )
759   GFXDECODE_ENTRY( region_grom, 0x0666, ti99_6_charlayout, 0, 8 )
760GFXDECODE_END
542READ8_MEMBER( ti99_4x::cruread )
543{
544//  if (VERBOSE>6) LOG("read access to CRU address %04x\n", offset << 4);
545   UINT8 value = 0;
761546
762static GFXDECODE_START( ti99a )
763   GFXDECODE_ENTRY( region_grom, 0x04b4, ti99_8_charlayout, 0, 8 )   // large
764   GFXDECODE_ENTRY( region_grom, 0x06b4, ti99_7_charlayout, 0, 8 ) // small
765   GFXDECODE_ENTRY( region_grom, 0x0950, ti99_24_charlayout, 0, 8 )// TI logo
766   GFXDECODE_ENTRY( region_grom, 0x0998, ti99_c_charlayout, 0, 8 )   // (c)
767GFXDECODE_END
547   // Similar to the bus8z_devices, just let the gromport and the p-box
548   // decide whether they want to change the value at the CRU address
549   // Also, we translate the bit addresses to base addresses
550   m_gromport->crureadz(offset<<4, &value);
551   m_peribox->crureadz(offset<<4, &value);
768552
769static GFXDECODE_START( ti99b )
770   GFXDECODE_ENTRY( region_grom, 0x04b4, ti99_8_charlayout, 2, 2 )
771   GFXDECODE_ENTRY( region_grom, 0x06b4, ti99_7_charlayout, 2, 2 )
772   GFXDECODE_ENTRY( region_grom, 0x0950, ti99_24_charlayout, 2, 2 )
773   GFXDECODE_ENTRY( region_grom, 0x0998, ti99_c_charlayout, 2, 2 )
774GFXDECODE_END
553   return value;
554}
775555
776static WRITE_LINE_DEVICE_HANDLER(set_int2)
556WRITE8_MEMBER( ti99_4x::cruwrite )
777557{
778   tms9901_set_int2(device->machine().device("ti_board"), state );
558//  if (VERBOSE>6) LOG("write access to CRU address %04x\n", offset << 1);
559   m_gromport->cruwrite(offset<<1, data);
560   m_peribox->cruwrite(offset<<1, data);
779561}
780562
563/***************************************************************************
564    TI99/4x-specific tms9901 I/O handlers
565
566    See mess/machine/tms9901.c for generic tms9901 CRU handlers.
567
568    TMS9901 interrupt handling on a TI99/4(a).
569
570    TI99/4(a) uses the following interrupts:
571    INT1: external interrupt (used by RS232 controller, for instance)
572    INT2: VDP interrupt
573    TMS9901 timer interrupt (overrides INT3)
574    INT12: handset interrupt (only on a TI-99/4 with the handset prototypes)
575
576    Three (occasionally four) interrupts are used by the system (INT1, INT2,
577    timer, and INT12 on a TI-99/4 with remote handset prototypes), out of 15/16
578    possible interrupts.  Keyboard pins can be used as interrupt pins, too, but
579    this is not emulated (it's a trick, anyway, and I don't know any program
580    which uses it).
581
582    When an interrupt line is set (and the corresponding bit in the interrupt mask is set),
583    a level 1 interrupt is requested from the TMS9900.  This interrupt request lasts as long as
584    the interrupt pin and the revelant bit in the interrupt mask are set.
585
586***************************************************************************/
587
588
589static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3" };
590static const char *const keynames8[] = { "KEY0", "KEY1", "KEY2", "KEY3",
591      "KEY4", "KEY5", "KEY6", "KEY7",   "KEY8", "KEY9", "KEY10", "KEY11",
592      "KEY12", "KEY13", "KEY14", "KEY15"
593};
594
595READ8_MEMBER( ti99_4x::read_by_9901 )
596{
597   int answer=0;
598   int col =0;
599
600   switch (offset & 0x03)
601   {
602   case TMS9901_CB_INT7:
603      //
604      // Read pins INT3*-INT7* of TI99's 9901.
605      // bit 1: INT1 status (interrupt; not set at this place)
606      // bit 2: INT2 status (interrupt; not set at this place)
607      // bit 3-7: keyboard status bits 0 to 4
608      //
609      if (m_mode==TI994)
610      {
611         col = 6;
612         if ((m_handset != NULL) && (m_keyboard_column == 7))
613         {
614            answer = (m_handset->poll_bus() << 3);
615            answer |= 0x80;
616         }
617      }
618      else
619      {
620         col = 7;
621         if (m_alphalock_line==false)
622         {
623            answer &= ~(input_port_read(machine(), "ALPHA") << 3);
624         }
625      }
626
627      if ((m_mecmouse != NULL) && (m_keyboard_column == col))
628      {
629         answer = m_mecmouse->get_values();
630      }
631      else
632      {
633         answer = ((input_port_read(machine(), keynames[m_keyboard_column >> 1]) >> ((m_keyboard_column & 1) * 8)) << 3) & 0xF8;
634      }
635
636      break;
637
638   case TMS9901_INT8_INT15:
639      if (m_keyboard_column == 7)
640      {
641         answer = 0x07;
642      }
643      else
644      {
645         answer = ((input_port_read(machine(), keynames[m_keyboard_column >> 1]) >> ((m_keyboard_column & 1) * 8)) >> 5) & 0x07;
646      }
647      break;
648
649   case TMS9901_P0_P7:
650      if (m_handset != NULL)
651      {
652         if (m_handset->get_clock()) answer |= 2;
653      }
654      break;
655
656   case TMS9901_P8_P15:
657      answer = 4;
658      /* on systems without handset, the pin is pulled up to avoid spurious interrupts */
659      if (m_handset != NULL)
660      {
661         if (m_handset->get_int()) answer = 0;
662      }
663      /* we don't take CS2 into account, as CS2 is a write-only unit */
664      if ((machine().device<cassette_image_device>(CASSETTE_TAG))->input() > 0)
665      {
666         answer |= 8;
667      }
668      break;
669   }
670   return answer;
671}
672
673/*
674    Handler for tms9901 P0 pin (handset data acknowledge)
675*/
676WRITE_LINE_MEMBER( ti99_4x::handset_ack )
677{
678   if (m_handset != NULL)
679   {
680      if (VERBOSE>7) LOG("ti99_4x: Set handset ack %d\n", state);
681      m_handset->set_acknowledge(state);
682   }
683}
684
685/*
686    WRITE key column select (P2-P4), TI-99/4
687*/
688void ti99_4x::set_key(int number, int data)
689{
690   int index = (m_mode==TI994A)? 6 : 5;
691
692   if (data != 0)
693      m_keyboard_column |= 1 << number;
694   else
695      m_keyboard_column &= ~ (1 << number);
696
697   if (m_mecmouse != NULL)
698      m_mecmouse->select(m_keyboard_column, index, index+1);
699}
700
701WRITE_LINE_MEMBER( ti99_4x::keyC0 )
702{
703   set_key(0, state);
704}
705
706WRITE_LINE_MEMBER( ti99_4x::keyC1 )
707{
708   set_key(1, state);
709}
710
711WRITE_LINE_MEMBER( ti99_4x::keyC2 )
712{
713   set_key(2, state);
714}
715
716/*
717    Select alpha lock line - TI99/4a only (P5)
718*/
719WRITE_LINE_MEMBER( ti99_4x::alphaW )
720{
721   m_alphalock_line = state;
722}
723
724/*
725    Control CS1 tape unit motor (P6)
726*/
727WRITE_LINE_MEMBER( ti99_4x::cs1_motor )
728{
729   cassette_image_device *img = machine().device<cassette_image_device>(CASSETTE_TAG);
730   img->change_state(state==ASSERT_LINE? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
731}
732
733/*
734    Control CS2 tape unit motor (P7)
735*/
736WRITE_LINE_MEMBER( ti99_4x::cs2_motor )
737{
738   cassette_image_device *img = machine().device<cassette_image_device>(CASSETTE2_TAG);
739   img->change_state(state==ASSERT_LINE? CASSETTE_MOTOR_ENABLED : CASSETTE_MOTOR_DISABLED,CASSETTE_MASK_MOTOR);
740}
741
742/*
743    Audio gate (P8)
744    Set to 1 before using tape: this enables the mixing of tape input sound
745    with computer sound.
746    We do not really need to emulate this as the tape recorder generates sound
747    on its own.
748*/
749WRITE_LINE_MEMBER( ti99_4x::audio_gate )
750{
751}
752
753/*
754    Tape output (P9)
755    I think polarity is correct, but don't take my word for it.
756*/
757WRITE_LINE_MEMBER( ti99_4x::cassette_output )
758{
759   machine().device<cassette_image_device>(CASSETTE_TAG)->output(state==ASSERT_LINE? +1 : -1);
760   machine().device<cassette_image_device>(CASSETTE2_TAG)->output(state==ASSERT_LINE? +1 : -1);
761}
762
763WRITE8_MEMBER( ti99_4x::tms9901_interrupt )
764{
765   // offset contains the interrupt level (0-15)
766   if (data==ASSERT_LINE)
767   {
768      // The TMS9901 should normally be connected with the CPU by 5 wires:
769      // INTREQ* and IC0-IC3. The last four lines deliver the interrupt level.
770      // On the TI-99 systems these IC lines are not used; the input lines
771      // at the CPU are hardwired to level 1.
772      device_execute(m_cpu)->set_input_line_and_vector(0, ASSERT_LINE, 1);
773   }
774   else
775   {
776      device_execute(m_cpu)->set_input_line(0, CLEAR_LINE);
777   }
778}
779
780/*****************************************************************************/
781
782/*
783    set the state of TMS9901's INT2 (called by the tms9928 core)
784*/
785WRITE_LINE_MEMBER( ti99_4x::set_tms9901_INT2 )
786{
787   if (VERBOSE>6) LOG("ti99_4x: VDP int 2 on tms9901, level=%02x\n", state);
788   m_tms9901->set_single_int(2, state);
789}
790
791void ti99_4x::set_tms9901_INT2_from_v9938(v99x8_device &vdp, int state)
792{
793   m_tms9901->set_single_int(2, state);
794}
795
796/*
797    set the state of TMS9901's INT12 (called by the handset prototype of TI-99/4)
798*/
799WRITE_LINE_MEMBER( ti99_4x::set_tms9901_INT12)
800{
801   if (m_handset!=NULL) m_tms9901->set_single_int(12, state);
802}
803
804/***********************************************************
805    Links to external devices
806***********************************************************/
807
808
809WRITE_LINE_MEMBER( ti99_4x::console_ready )
810{
811   if (VERBOSE>6) LOG("READY line set ... not yet connected, level=%02x\n", state);
812}
813
814WRITE_LINE_MEMBER( ti99_4x::extint )
815{
816   if (VERBOSE>6) LOG("EXTINT level = %02x\n", state);
817   if (m_tms9901 != NULL)
818      m_tms9901->set_single_int(1, state);
819}
820
821WRITE_LINE_MEMBER( ti99_4x::notconnected )
822{
823   if (VERBOSE>6) LOG("Setting a not connected line ... ignored\n");
824}
825
826/*****************************************************************************/
827
781828static TMS9928A_INTERFACE(ti99_4_tms9928a_interface)
782829{
783830   SCREEN_TAG,
784831   0x4000,
785   DEVCB_LINE(set_int2)
832   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, set_tms9901_INT2)
786833};
787834
788/*
789    Format:
790    Name, address bus mask, select pattern, write select bit(s), read8z function, write8 function, Condition name, bits which must be set, bits which must not be set
835static TI99_HANDSET_INTERFACE(ti99_4_handset_interface)
836{
837   DEVCB_DRIVER_LINE_MEMBER(ti99_4x, set_tms9901_INT12)
838};
791839
792    Multiple devices may have the same select pattern; as in the real hardware,
793    care must be taken that only one device actually responds. In the case of
794    GROMs, each chip has an internal address counter and an ID, and the chip
795    only responds when the ID and the most significant 3 bits match.
796    If the HSGPL extension is active, the console GROMs should be removed. In
797    reality, the chips should be pulled out of their sockets. Here we simulate
798    this by a condition.
799*/
800static const bus_device dmux_devices[] =
840/* TMS9901 setup. */
841const tms9901_interface tms9901_wiring_ti99_4 =
801842{
802   { "console_grom_0",     0xfc01, 0x9800, 0x0400, ti99grom_rz,  ti99grom_w, "EXTCARD", 0x00, 0x02 },
803   { "console_grom_1",     0xfc01, 0x9800, 0x0400, ti99grom_rz,  ti99grom_w, "EXTCARD", 0x00, 0x02 },
804   { "console_grom_2",     0xfc01, 0x9800, 0x0400, ti99grom_rz,  ti99grom_w, "EXTCARD", 0x00, 0x02 },
805   { "soundgen",         0xfc01, 0x8400, 0x0000, NULL,         sn76496_w, NULL, 0, 0 },
806   { "gromport",         0xfc01, 0x9800, 0x0400, gromportg_rz, gromportg_w,  NULL, 0, 0 },
807   { "gromport",         0xe000, 0x6000, 0x0000, gromportr_rz, gromportr_w,  NULL, 0, 0 },
808   { "peribox",          0x0000, 0x0000, 0x0000, ti99_peb_data_rz, ti99_peb_data_w, 0, 0 },  // Peribox needs all addresses
809   { NULL, 0, 0, 0, NULL, NULL, 0, 0  }
843   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,   /* only input pins whose state is always known */
844
845   // read handler
846   DEVCB_DRIVER_MEMBER(ti99_4x, read_by_9901),
847
848   // write handlers
849   {
850      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, handset_ack),
851      DEVCB_NULL,
852      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC0),
853      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC1),
854      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC2),
855      DEVCB_NULL,
856      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cs1_motor),
857      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cs2_motor),
858      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, audio_gate),
859      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cassette_output),
860      DEVCB_NULL,
861      DEVCB_NULL,
862      DEVCB_NULL,
863      DEVCB_NULL,
864      DEVCB_NULL,
865      DEVCB_NULL
866   },
867
868   // interrupt handler
869   DEVCB_DRIVER_MEMBER(ti99_4x, tms9901_interrupt)
810870};
811871
812static const cruconf_device cru_devices[] =
872const tms9901_interface tms9901_wiring_ti99_4a =
813873{
814   { "gromport", 0xf800, 0x0800, gromportc_rz, gromportc_w },            // Check: Is this really limited?
815   { "peribox",  0x0000, 0x0000, ti99_peb_cru_rz, ti99_peb_cru_w },    // Peribox needs all addresses
816   { NULL, 0, 0, NULL, NULL }
874   TMS9901_INT1 | TMS9901_INT2 | TMS9901_INTC,
875
876   // read handler
877   DEVCB_DRIVER_MEMBER(ti99_4x, read_by_9901),
878
879   // write handlers
880   {
881      DEVCB_NULL,
882      DEVCB_NULL,
883      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC0),
884      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC1),
885      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, keyC2),
886      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, alphaW),
887      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cs1_motor),
888      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cs2_motor),
889      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, audio_gate),
890      DEVCB_DRIVER_LINE_MEMBER(ti99_4x, cassette_output),
891      DEVCB_NULL,
892      DEVCB_NULL,
893      DEVCB_NULL,
894      DEVCB_NULL,
895      DEVCB_NULL,
896      DEVCB_NULL
897   },
898
899   DEVCB_DRIVER_MEMBER(ti99_4x, tms9901_interrupt)
817900};
818901
819MACHINE_RESET( ti99_4 )
902/*
903    Devices attached to the databus multiplexer. We cannot solve this with
904    the common address maps since the multiplexer also inserts wait states
905    that we want to emulate properly. Also, devices may reside on the same
906    memory locations (like GROMs) and select themselves according to some
907    inner state (e.g. GROMs have an own address counter and a given address
908    area).
909*/
910static const dmux_device_list_entry dmux_devices[] =
820911{
912   { GROM0_TAG,     0x9800, 0xfc01, 0x0400, "GROMENA", 0x01, 0x00 },
913   { GROM1_TAG,     0x9800, 0xfc01, 0x0400, "GROMENA", 0x01, 0x00 },
914   { GROM2_TAG,     0x9800, 0xfc01, 0x0400, "GROMENA", 0x01, 0x00 },
915   { TISOUND_TAG,   0x8400, 0xfc01, 0x0000, NULL, 0, 0 },
916   { GROMPORT_TAG,  0x9800, 0xfc01, 0x0400, NULL, 0, 0 },
917   { GROMPORT_TAG,  0x6000, 0xe000, 0x0000, NULL, 0, 0 },
918   { PERIBOX_TAG,   0x0000, 0x0000, 0x0000, NULL, 0, 0 },  // Peribox needs all addresses
919   { NULL, 0, 0, 0, NULL, 0, 0  }
920};
921
922/******************************************************************************
923    Machine definitions
924******************************************************************************/
925
926MACHINE_START( ti99_4 )
927{
928   ti99_4x *driver = machine.driver_data<ti99_4x>();
929   driver->m_mode = TI994;
930
931   driver->m_cpu = machine.device("maincpu");
932   driver->m_tms9901 = static_cast<tms9901_device*>(machine.device(TMS9901_TAG));
933
934   driver->m_gromport = static_cast<gromport_device*>(machine.device(GROMPORT_TAG));
935
936   driver->m_peribox = static_cast<peribox_device*>(machine.device(PERIBOX_TAG));
937
938   driver->m_peribox->senila(CLEAR_LINE);
939   driver->m_peribox->senilb(CLEAR_LINE);
821940}
822941
823MACHINE_RESET( ti99_4a )
942MACHINE_RESET( ti99_4 )
824943{
944   ti99_4x *driver = machine.driver_data<ti99_4x>();
945   if (input_port_read(machine, "HANDSET")==0x01)
946      driver->m_handset = static_cast<ti99_handset_device*>(machine.device(HANDSET_TAG));
947   else
948      driver->m_handset = NULL;
949
950   if (input_port_read(machine, "MECMOUSE")==0x01)
951      driver->m_mecmouse = static_cast<mecmouse_device*>(machine.device(MECMOUSE_TAG));
952   else
953      driver->m_mecmouse = NULL;
825954}
826955
827static MACHINE_CONFIG_START( ti99_4_60hz, ti99_4x_state )
828   /* basic machine hardware */
829   /* TMS9900 CPU @ 3.0 MHz */
956/*
957    TI-99/4 - the predecessor of the more popular TI-99/4A
958*/
959static MACHINE_CONFIG_START( ti99_4_60hz, ti99_4x )
830960   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
831961   MCFG_CPU_PROGRAM_MAP(memmap)
832962   MCFG_CPU_IO_MAP(cru_map)
833   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
834
963   MCFG_MACHINE_START( ti99_4 )
835964   MCFG_MACHINE_RESET( ti99_4 )
836965
837   /* video hardware */
838   // MCFG_FRAGMENT_ADD(tms9928a)
839   // MCFG_SCREEN_MODIFY("screen")
840   // MCFG_SCREEN_REFRESH_RATE(60)
841   // MCFG_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
966   MCFG_TI_TMS991x_ADD_NTSC(VIDEO_SYSTEM_TAG, TMS9118, ti99_4_tms9928a_interface)
842967
843   MCFG_TI_TMS991x_ADD_NTSC("video", TMS9118, ti99_4_tms9928a_interface)
844   MCFG_GFXDECODE(ti99)
968   /* Main board */
969   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4, 3000000)
970   MCFG_DMUX_ADD( DATAMUX_TAG, dmux_devices )
971   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
845972
973   /* Peripheral expansion box */
974   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
975
846976   /* sound hardware */
847977   MCFG_SPEAKER_STANDARD_MONO("mono")
848   MCFG_SOUND_ADD("soundgen", SN94624, 3579545/8)   /* 3.579545 MHz */
978   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
849979   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
980   MCFG_TI_SOUND_ADD( TISOUND_TAG )
850981
982   /* Cassette drives */
983   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
984   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
985
851986   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
852987   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
853988
854   MCFG_TI994_BOARD_ADD( "ti_board" )
989   /* GROM devices */
990   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
991   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
992   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
855993
856   /* tms9901 */
857   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4)
994   /* User controller */
995   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 60 )
996   MCFG_HANDSET_ADD( HANDSET_TAG, ti99_4_handset_interface, 60 )
858997
859   MCFG_CRUBUS_ADD( "crubus", cru_devices )
860   MCFG_DMUX_ADD( "datamux_16_8", dmux_devices )
861998
862   MCFG_GROM_ADD( "console_grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
863   MCFG_GROM_ADD( "console_grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
864   MCFG_GROM_ADD( "console_grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
865
866   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
867
868   MCFG_PBOX4_ADD( "peribox", console_extint, console_notconnected, console_ready )
869
870   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
871   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
872
873   MCFG_MECMOUSE_ADD( "mecmouse" )
874   MCFG_HANDSET_ADD( "handset", "tms9901" )
875999MACHINE_CONFIG_END
8761000
877static MACHINE_CONFIG_START( ti99_4_50hz, ti99_4x_state )
878   /* basic machine hardware */
879   /* TMS9900 CPU @ 3.0 MHz */
1001static MACHINE_CONFIG_START( ti99_4_50hz, ti99_4x )
8801002   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
8811003   MCFG_CPU_PROGRAM_MAP(memmap)
8821004   MCFG_CPU_IO_MAP(cru_map)
883   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
884
1005   MCFG_MACHINE_START( ti99_4 )
8851006   MCFG_MACHINE_RESET( ti99_4 )
8861007
8871008   /* video hardware */
888   MCFG_TI_TMS991x_ADD_PAL("video", TMS9929, ti99_4, ti99_4_tms9928a_interface)
1009   MCFG_TI_TMS991x_ADD_PAL(VIDEO_SYSTEM_TAG, TMS9929, ti99_4_tms9928a_interface)
8891010
890   MCFG_GFXDECODE(ti99)
1011   /* main board */
1012   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4, 3000000)
1013   MCFG_DMUX_ADD( DATAMUX_TAG, dmux_devices )
1014   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
8911015
1016   /* Peripheral expansion box */
1017   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
1018
8921019   /* sound hardware */
8931020   MCFG_SPEAKER_STANDARD_MONO("mono")
894   MCFG_SOUND_ADD("soundgen", SN94624, 3579545/8)   /* 3.579545 MHz */
1021   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
8951022   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
1023   MCFG_TI_SOUND_ADD( TISOUND_TAG )
8961024
1025   /* Cassette drives */
1026   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1027   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1028
8971029   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
8981030   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
8991031
900   MCFG_TI994_BOARD_ADD( "ti_board" )
1032   /* GROM devices */
1033   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
1034   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
1035   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
9011036
902   /* tms9901 */
903   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4)
1037   /* User controller */
1038   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 50 )
1039   MCFG_HANDSET_ADD( HANDSET_TAG, ti99_4_handset_interface, 50 )
1040MACHINE_CONFIG_END
9041041
905   MCFG_CRUBUS_ADD( "crubus", cru_devices )
906   MCFG_DMUX_ADD( "datamux_16_8", dmux_devices )
1042/*
1043    TI-99/4A - replaced the 99/4
1044*/
9071045
908   MCFG_GROM_ADD( "console_grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
909   MCFG_GROM_ADD( "console_grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
910   MCFG_GROM_ADD( "console_grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
1046MACHINE_START( ti99_4a )
1047{
1048   ti99_4x *driver = machine.driver_data<ti99_4x>();
1049   driver->m_mode = TI994A;
9111050
912   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
1051   driver->m_cpu = machine.device("maincpu");
1052   driver->m_tms9901 = static_cast<tms9901_device*>(machine.device(TMS9901_TAG));
9131053
914   MCFG_PBOX4_ADD( "peribox", console_extint, console_notconnected, console_ready )
1054   driver->m_mecmouse = static_cast<mecmouse_device*>(machine.device(MECMOUSE_TAG));
1055   driver->m_gromport = static_cast<gromport_device*>(machine.device(GROMPORT_TAG));
1056   driver->m_peribox = static_cast<peribox_device*>(machine.device(PERIBOX_TAG));
9151057
916   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
917   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1058   driver->m_peribox->senila(CLEAR_LINE);
1059   driver->m_peribox->senilb(CLEAR_LINE);
1060}
9181061
919   MCFG_MECMOUSE_ADD( "mecmouse" )
920   MCFG_HANDSET_ADD( "handset", "tms9901" )
921MACHINE_CONFIG_END
1062MACHINE_RESET( ti99_4a )
1063{
1064   ti99_4x *driver = machine.driver_data<ti99_4x>();
1065   if (input_port_read(machine, "MECMOUSE")==0x01)
1066      driver->m_mecmouse = static_cast<mecmouse_device*>(machine.device(MECMOUSE_TAG));
1067   else
1068      driver->m_mecmouse = NULL;
1069}
9221070
923static MACHINE_CONFIG_START( ti99_4a_60hz, ti99_4x_state )
924   /* basic machine hardware */
925   /* TMS9900 CPU @ 3.0 MHz */
1071static MACHINE_CONFIG_START( ti99_4a_60hz, ti99_4x )
9261072   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
9271073   MCFG_CPU_PROGRAM_MAP(memmap)
9281074   MCFG_CPU_IO_MAP(cru_map)
929   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
930
1075   MCFG_MACHINE_START( ti99_4a )
9311076   MCFG_MACHINE_RESET( ti99_4a )
9321077
933   /* video hardware */
934   MCFG_TI_TMS991x_ADD_NTSC("video", TMS9918A, ti99_4_tms9928a_interface)
935   MCFG_GFXDECODE(ti99a)
1078   /* Video hardware */
1079   MCFG_TI_TMS991x_ADD_NTSC(VIDEO_SYSTEM_TAG, TMS9918A, ti99_4_tms9928a_interface)
9361080
1081   /* Main board */
1082   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4a, 3000000)
1083   MCFG_DMUX_ADD( DATAMUX_TAG, dmux_devices )
1084   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
1085
1086   /* Peripheral expansion box */
1087   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
1088
9371089   /* sound hardware */
9381090   MCFG_SPEAKER_STANDARD_MONO("mono")
939   MCFG_SOUND_ADD("soundgen", SN94624, 3579545/8)   /* 3.579545 MHz */
1091   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
9401092   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
1093   MCFG_TI_SOUND_ADD( TISOUND_TAG )
9411094
1095   /* Cassette drives */
1096   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1097   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1098
9421099   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
9431100   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
9441101
945   MCFG_TI994A_BOARD_ADD( "ti_board" )
1102   /* GROM devices */
1103   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
1104   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
1105   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
9461106
947   /* tms9901 */
948   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4a)
949
950   MCFG_CRUBUS_ADD( "crubus", cru_devices )
951   MCFG_DMUX_ADD( "datamux_16_8", dmux_devices )
952
953   MCFG_GROM_ADD( "console_grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
954   MCFG_GROM_ADD( "console_grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
955   MCFG_GROM_ADD( "console_grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
956
957   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
958
959   MCFG_PBOX4A_ADD( "peribox", console_extint, console_notconnected, console_ready )
960
961   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
962   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
963
964   MCFG_MECMOUSE_ADD( "mecmouse" )
1107   /* User controller */
1108   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 60 )
9651109MACHINE_CONFIG_END
9661110
967static MACHINE_CONFIG_START( ti99_4a_50hz, ti99_4x_state )
968   /* basic machine hardware */
969   /* TMS9900 CPU @ 3.0 MHz */
1111static MACHINE_CONFIG_START( ti99_4a_50hz, ti99_4x )
9701112   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
9711113   MCFG_CPU_PROGRAM_MAP(memmap)
9721114   MCFG_CPU_IO_MAP(cru_map)
973   MCFG_CPU_VBLANK_INT("screen", ti99_vblank_interrupt)
974
1115   MCFG_MACHINE_START( ti99_4a )
9751116   MCFG_MACHINE_RESET( ti99_4a )
9761117
977   /* video hardware */
978   MCFG_TI_TMS991x_ADD_PAL("video", TMS9929A, ti99_4, ti99_4_tms9928a_interface)
979   MCFG_GFXDECODE(ti99a)
1118   /* Video hardware */
1119   MCFG_TI_TMS991x_ADD_PAL(VIDEO_SYSTEM_TAG, TMS9929A, ti99_4_tms9928a_interface)
9801120
1121   /* Main board */
1122   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4a, 3000000)
1123   MCFG_DMUX_ADD( DATAMUX_TAG, dmux_devices )
1124   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
1125
1126   /* Peripheral expansion box */
1127   MCFG_PERIBOX_ADD( PERIBOX_TAG, peribox_conf )
1128
9811129   /* sound hardware */
9821130   MCFG_SPEAKER_STANDARD_MONO("mono")
983   MCFG_SOUND_ADD("soundgen", SN94624, 3579545/8)   /* 3.579545 MHz */
1131   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
9841132   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
1133   MCFG_TI_SOUND_ADD( TISOUND_TAG )
9851134
1135   /* Cassette drives */
1136   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1137   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1138
9861139   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
9871140   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
9881141
989   MCFG_TI994A_BOARD_ADD( "ti_board" )
1142   /* GROM devices */
1143   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
1144   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
1145   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
9901146
991   /* tms9901 */
992   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4a)
1147   /* User controller */
1148   MCFG_MECMOUSE_ADD( MECMOUSE_TAG, 50 )
1149MACHINE_CONFIG_END
9931150
994   MCFG_CRUBUS_ADD( "crubus", cru_devices )
995   MCFG_DMUX_ADD( "datamux_16_8", dmux_devices )
9961151
997   MCFG_GROM_ADD( "console_grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
998   MCFG_GROM_ADD( "console_grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
999   MCFG_GROM_ADD( "console_grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
1000
1001   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
1002
1003   MCFG_PBOX4A_ADD( "peribox", console_extint, console_notconnected, console_ready )
1004
1005   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1006   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1007
1008   MCFG_MECMOUSE_ADD( "mecmouse" )
1009MACHINE_CONFIG_END
1010
10111152TIMER_DEVICE_CALLBACK( ti99_4ev_hblank_interrupt )
10121153{
1013   timer.machine().device<v9938_device>("video_v9938")->interrupt();
1154   timer.machine().device<v9938_device>(V9938_TAG)->interrupt();
10141155}
10151156
1016static MACHINE_CONFIG_START( ti99_4ev_60hz, ti99_4x_state )
1017   /* basic machine hardware */
1018   /* TMS9900 CPU @ 3.0 MHz */
1157/*
1158    TI-99/4A with 80-column support. Actually a separate expansion card (EVPC),
1159    replacing the console video processor.
1160*/
1161static MACHINE_CONFIG_START( ti99_4ev_60hz, ti99_4x )
10191162   MCFG_CPU_ADD("maincpu", TMS9900, 3000000)
10201163   MCFG_CPU_PROGRAM_MAP(memmap_4ev)
10211164   MCFG_CPU_IO_MAP(cru_map)
1022   MCFG_TIMER_ADD_SCANLINE("scantimer", ti99_4ev_hblank_interrupt, "screen", 0, 1)   /* 262.5 in 60Hz, 312.5 in 50Hz */
1165   MCFG_MACHINE_START( ti99_4a )
10231166
1024   MCFG_MACHINE_RESET( ti99_4a )
1167   /* Video hardware */
1168   // FIXME: (MZ) Lowered the screen rate to 30 Hz. This is a quick hack to
1169   // restore normal video speed for V9938-based systems until the V9938 implementation
1170   // is properly fixed.
1171   MCFG_TI_V9938_ADD(VIDEO_SYSTEM_TAG, 30, SCREEN_TAG, 2500, 512+32, (212+28)*2, DEVICE_SELF, ti99_4x, set_tms9901_INT2_from_v9938)
1172   MCFG_TIMER_ADD_SCANLINE("scantimer", ti99_4ev_hblank_interrupt, SCREEN_TAG, 0, 1)
10251173
1026   /* video hardware */
1027   /* FIXME: (MZ) Lowered the screen rate to 30 Hz. This is a quick hack to
1028    restore normal video speed for V9938-based systems until the V9938 implementation
1029    is properly fixed. */
1030   MCFG_TI_V9938_ADD("video", 30, "screen", 2500, 512+32, (212+28)*2, tms9901_set_int2_v9938)
1174   /* Main board */
1175   MCFG_TMS9901_ADD(TMS9901_TAG, tms9901_wiring_ti99_4a, 3000000)
1176   MCFG_DMUX_ADD( DATAMUX_TAG, dmux_devices )
1177   MCFG_TI99_GROMPORT_ADD( GROMPORT_TAG, console_cartslot )
10311178
1032   MCFG_GFXDECODE(ti99b)
1179   /* Peripheral expansion box */
1180   MCFG_PERIBOX_EV_ADD( PERIBOX_TAG, peribox_conf )
10331181
10341182   /* sound hardware */
10351183   MCFG_SPEAKER_STANDARD_MONO("mono")
1036   MCFG_SOUND_ADD("soundgen", SN94624, 3579545/8)   /* 3.579545 MHz */
1184   MCFG_SOUND_ADD(TISOUNDCHIP_TAG, SN94624, 3579545/8)   /* 3.579545 MHz */
10371185   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.75)
1186   MCFG_TI_SOUND_ADD( TISOUND_TAG )
10381187
1188   /* Cassette drives */
1189   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1190   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1191
10391192   MCFG_SOUND_WAVE_ADD(WAVE_TAG, CASSETTE_TAG)
10401193   MCFG_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.25)
10411194
1042   MCFG_TI994A_BOARD_ADD( "ti_board" )
1195   /* GROM devices */
1196   MCFG_GROM_ADD( GROM0_TAG, grom0_config )
1197   MCFG_GROM_ADD( GROM1_TAG, grom1_config )
1198   MCFG_GROM_ADD( GROM2_TAG, grom2_config )
1199MACHINE_CONFIG_END
10431200
1044   MCFG_TMS9901_ADD("tms9901", tms9901_wiring_ti99_4a)
10451201
1046   MCFG_CRUBUS_ADD( "crubus", cru_devices )
1047   MCFG_DMUX_ADD( "datamux_16_8", dmux_devices )
1048
1049   MCFG_GROM_ADD( "console_grom_0", 0, region_grom, 0x0000, 0x1800, console_ready )
1050   MCFG_GROM_ADD( "console_grom_1", 1, region_grom, 0x2000, 0x1800, console_ready )
1051   MCFG_GROM_ADD( "console_grom_2", 2, region_grom, 0x4000, 0x1800, console_ready )
1052
1053   MCFG_TI99_GROMPORT_ADD( "gromport", console_ready )
1054
1055   MCFG_PBOXEV_ADD( "peribox", console_extint, console_notconnected, console_ready )
1056
1057   MCFG_CASSETTE_ADD( CASSETTE_TAG, default_cassette_interface )
1058   MCFG_CASSETTE_ADD( CASSETTE2_TAG, default_cassette_interface )
1059MACHINE_CONFIG_END
1060
1061/*
1202/*****************************************************************************
10621203    ROM loading
10631204    Note that we use the same ROMset for 50Hz and 60Hz versions.
10641205    ROMs for peripheral equipment have been moved to the respective files.
1065*/
1206******************************************************************************/
1207#define rom_ti99_4e rom_ti99_4
1208#define rom_ti99_4ae rom_ti99_4a
10661209
10671210ROM_START(ti99_4)
10681211   /*CPU memory space*/
r14513r14514
10741217   ROM_REGION(0x10000, region_grom, 0)
10751218   ROM_LOAD("u500.bin", 0x0000, 0x1800, CRC(aa757e13) SHA1(4658d3d01c0131c283a30cebd12e76754d41a84a)) /* system GROM 0 */
10761219   ROM_LOAD("u501.bin", 0x2000, 0x1800, CRC(c863e460) SHA1(6d849a76011273a069a98ed0c3feaf13831c942f)) /* system GROM 1 */
1077   ROM_LOAD("u502.bin", 0x4000, 0x1800, CRC(b0eda548) SHA1(725e3f26f8c819f356e4bb405b4102b5ae1e0e70)) /* system GROM 1 */
1220   ROM_LOAD("u502.bin", 0x4000, 0x1800, CRC(b0eda548) SHA1(725e3f26f8c819f356e4bb405b4102b5ae1e0e70)) /* system GROM 2 */
10781221ROM_END
10791222
10801223ROM_START(ti99_4a)
r14513r14514
10971240   ROM_LOAD("994agr38.bin", 0x0000, 0x6000, CRC(bdd9f09b) SHA1(9b058a55d2528d2a6a69d7081aa296911ed7c0de)) /* system GROMs */
10981241ROM_END
10991242
1100#define rom_ti99_4e rom_ti99_4
1101#define rom_ti99_4ae rom_ti99_4a
1102
11031243/*    YEAR  NAME      PARENT   COMPAT   MACHINE      INPUT    INIT      COMPANY             FULLNAME */
11041244COMP( 1979, ti99_4,   0,      0,      ti99_4_60hz,  ti99_4,   0,   "Texas Instruments", "TI99/4 Home Computer (US)" , 0)
11051245COMP( 1980, ti99_4e,  ti99_4,  0,      ti99_4_50hz,  ti99_4,  0,   "Texas Instruments", "TI99/4 Home Computer (Europe)" , 0)
11061246COMP( 1981, ti99_4a,  0,      0,      ti99_4a_60hz, ti99_4a, 0,   "Texas Instruments", "TI99/4A Home Computer (US)" , 0)
11071247COMP( 1981, ti99_4ae, ti99_4a, 0,      ti99_4a_50hz, ti99_4a, 0,   "Texas Instruments", "TI99/4A Home Computer (Europe)" , 0)
1108COMP( 1994, ti99_4ev, ti99_4a, 0,      ti99_4ev_60hz,ti99_4ev, 0, "Texas Instruments", "TI99/4A Home Computer with EVPC" , 0)
1248COMP( 1994, ti99_4ev, ti99_4a, 0,      ti99_4ev_60hz,ti99_4a, 0, "Texas Instruments", "TI99/4A Home Computer with EVPC" , 0)

Previous 15994 Revisions Next


© 1998-2018 The MESS Team