Previous 15994 Revisions Next

r11506 Tuesday 10th May, 2011 at 21:21:04 UTC by smf
"Why do you take this apart now? I'm trying to get us out of here and you pull both of these. Put them back together right now." --Han Solo

MDEC is now an internal device of the PlayStation CPU [smf]
PlayStation DMA uses delegates so devices can be hooked up.
device_t::siblingdevice() uses device list on mconfig() so it can work before the machine is constructed.
moved unused console code back to mess.
[src/emu]devintrf.c
[src/emu/cpu]cpu.mak
[src/emu/cpu/psx]dma.c dma.h psx.c psx.h
[src/emu/sound]spu.c
[src/mame]mame.mak
[src/mame/drivers]konamigq.c konamigv.c ksys573.c namcos12.c psx.c twinkle.c zn.c
[src/mame/includes]psx.h
[src/mame/machine]psx.c psxcard.c psxcard.h psxcd.c psxcd.h psxcddrv.c psxcddrv.h
[src/mess]mess.mak
[src/mess/drivers]psx.c*
[src/mess/machine]psxcard.c* psxcard.h* psxcd.c* psxcd.h* psxcddrv.c* psxcddrv.h*

src/mame/machine/psxcard.c
r11505r11506
1/*
2    psxcard.c - Sony PlayStation memory card device
3
4    by pSXAuthor
5    MESS conversion by R. Belmont
6*/
7
8#include "emu.h"
9#include "psxcard.h"
10#include "includes/psx.h"
11
12//
13//
14//
15
16//#define debug_card
17
18//
19//
20//
21
22const device_type PSXCARD = &device_creator<psxcard_device>;
23
24enum transfer_states
25{
26   state_illegal=0,
27   state_command,
28   state_cmdack_1,
29   state_cmdack_2,
30   state_addr_hi,
31   state_addr_lo,
32   state_read,
33   state_write,
34   state_writeack_1,
35   state_writeack_2,
36   state_writechk
37};
38
39psxcard_device::psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
40    : device_t(mconfig, PSXCARD, "Sony PSX Memory Card", tag, owner, clock)
41{
42}
43
44void psxcard_device::device_start()
45{
46   cache=new unsigned char [128*1024];
47
48   memset(cache, 0, 128*1024);
49
50   // save state registrations
51/*  save_item(NAME(pkt));
52    save_item(NAME(pkt_ptr));
53    save_item(NAME(pkt_sz));
54    save_item(NAME(pkt));
55    save_item(NAME(cache));
56    save_item(NAME(addr));
57    save_item(NAME(state));*/
58}
59
60void psxcard_device::device_reset()
61{
62   state = state_illegal;
63   addr = 0;
64}
65
66//
67//
68//
69
70bool psxcard_device::transfer(UINT8 to, UINT8 *from)
71{
72   bool ret=true;
73
74   switch (state)
75   {
76      case state_illegal:
77         if (to == 0x81)
78         {
79//              printf("CARD: begin\n");
80            state = state_command;
81            *from = 0x00;
82         }
83         else
84         {
85            ret = false;
86         }
87         break;
88
89      case state_command:
90         cmd=to;
91         *from=0x00;
92         state=state_cmdack_1;
93         break;
94
95      case state_cmdack_1:
96         //assert(to==0);
97         *from=0x5a;
98         state=state_cmdack_2;
99         break;
100
101      case state_cmdack_2:
102         //assert(to==0);
103         *from=0x5d;
104         state=state_addr_hi;
105         break;
106
107      case state_addr_hi:
108         addr=(to<<8);
109//          printf("addr_hi: %02x, addr = %x\n", to, addr);
110         *from=0;
111         state=state_addr_lo;
112         break;
113
114      case state_addr_lo:
115         addr|=(to&0xff);
116//          printf("addr_lo: %02x, addr = %x, cmd = %x\n", to, addr, cmd);
117         *from=(addr>>8);
118
119         switch (cmd)
120         {
121            case 'R':   // 0x52
122            {
123               pkt[0]=0x5c;
124               pkt[1]=0x5d;
125               pkt[2]=(addr>>8);
126               pkt[3]=(addr&0xff);
127               read_card(addr,&pkt[4]);
128               pkt[4+128]=checksum_data(&pkt[2],128+2);
129               pkt[5+128]=0x47;
130               pkt_sz=6+128;
131               pkt_ptr=0;
132               state=state_read;
133               break;
134            }
135            case 'W':   // 0x57
136            {
137               pkt[0]=addr>>8;
138               pkt[1]=addr&0xff;
139               pkt_sz=129+2;
140               pkt_ptr=2;
141               state=state_write;
142               break;
143            }
144            default:
145               state=state_illegal;
146               break;
147         }
148         break;
149
150      case state_read:
151         //assert(to==0);
152//          printf("state_read: pkt_ptr = %d, pkt_sz = %d\n", pkt_ptr, pkt_sz);
153         *from=pkt[pkt_ptr++];
154         if (pkt_ptr==pkt_sz)
155         {
156            #ifdef debug_card
157               printf("card: read finished\n");
158            #endif
159
160            state=state_illegal;
161            ret=false;
162         }
163         break;
164
165      case state_write:
166         *from=(pkt_ptr==0)?(addr&0xff):pkt[pkt_ptr-1];
167         pkt[pkt_ptr++]=to;
168         if (pkt_ptr==pkt_sz)
169            state=state_writeack_1;
170         break;
171
172      case state_writeack_1:
173         *from=0x5c;
174         state=state_writeack_2;
175         break;
176
177      case state_writeack_2:
178         *from=0x5d;
179         state=state_writechk;
180         break;
181
182      case state_writechk:
183      {
184         unsigned char chk=checksum_data(pkt,128+2);
185         if (chk==pkt[128+2])
186         {
187            #ifdef debug_card
188               printf("card: write ok\n");
189            #endif
190
191            write_card(addr,pkt+2);
192
193            *from='G';
194         } else
195         {
196            #ifdef debug_card
197               printf("card: write fail\n");
198            #endif
199
200            *from='N';
201         }
202         state=state_illegal;
203         ret=false;
204         break;
205      }
206
207      default: /*assert(0);*/ ret=false; break;
208   }
209
210   #ifdef debug_card
211//      printf("card: transfer to=%02x from=%02x ret=%c\n",to,*from,ret ? 'T' : 'F');
212   #endif
213
214   return ret;
215}
216
217void psxcard_device::read_card(const unsigned short addr, unsigned char *buf)
218{
219   #ifdef debug_card
220      printf("card: read block %d\n",addr);
221   #endif
222
223   if (addr<1024)
224   {
225      memcpy(buf,cache+(addr*128),128);
226   } else
227   {
228      memset(buf,0,128);
229   }
230}
231
232//
233//
234//
235
236void psxcard_device::write_card(const unsigned short addr, unsigned char *buf)
237{
238   #ifdef debug_card
239      printf("card: write block %d\n",addr);
240   #endif
241
242   if (addr<1024)
243   {
244      memcpy(cache+(addr*128),buf,128);
245   }
246}
247
248//
249//
250//
251
252unsigned char psxcard_device::checksum_data(const unsigned char *buf, const unsigned int sz)
253{
254   unsigned char chk=*buf++;
255   int left=sz;
256   while (--left) chk^=*buf++;
257   return chk;
258}
src/mame/machine/psxcard.h
r11505r11506
1#pragma once
2
3#ifndef _PSXCARD_
4#define _PSXCARD_
5
6#include "emu.h"
7
8#define MCFG_PSXCARD_ADD(_tag) \
9    MCFG_DEVICE_ADD(_tag, PSXCARD, 0)
10
11struct psxcard_interface
12{
13};
14
15class psxcard_device : public device_t,
16                       public psxcard_interface
17{
18public:
19    psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
20
21private:
22   unsigned char pkt[0x8b], pkt_ptr, pkt_sz, cmd, *cache;
23   unsigned short addr;
24   int state;
25
26   void read_card(const unsigned short addr, unsigned char *buf);
27   void write_card(const unsigned short addr, unsigned char *buf);
28   unsigned char checksum_data(const unsigned char *buf, const unsigned int sz);
29
30public:
31   virtual void device_start();
32   virtual void device_reset();
33
34   bool transfer(UINT8 to, UINT8 *from);
35   void mess_io(running_machine *machine, UINT8 n_data);
36private:
37};
38
39// device type definition
40extern const device_type PSXCARD;
41
42#endif
src/mame/machine/psxcd.c
r11505r11506
1#include "emu.h"
2#include "includes/psx.h"
3#include "psxcd.h"
4#include "machine/devhelpr.h"
5#include "sound/spu.h"
6#include "debugger.h"
7
8//
9//
10//
11
12//#define debug_cdrom
13//#define debug_cdrom_registers
14//#define skip_reads
15//#define dump_subheader
16//#define disable_xa_prefetch
17//#define disable_cdda_prefetch
18
19//
20//
21//
22
23enum cdrom_events
24{
25   event_cmd_complete=0,
26   event_preread_sector,
27   event_read_sector,
28   event_play_sector
29};
30
31//
32//
33//
34
35enum intr_status
36{
37   intr_nointr=0,
38   intr_dataready,
39   intr_acknowledge,
40   intr_complete,
41   intr_dataend,
42   intr_diskerror
43};
44
45enum mode_flags
46{
47   mode_double_speed=0x80,
48   mode_adpcm=0x40,
49   mode_size=0x20,
50   mode_size2=0x10,
51   mode_size_shift=4,
52   mode_size_mask=(3<<mode_size_shift),
53   mode_channel=0x08,
54   mode_report=0x04,
55   mode_autopause=0x02,
56   mode_cdda=0x01
57};
58
59enum status_f
60{
61   status_playing=0x80,
62   status_seeking=0x40,
63   status_reading=0x20,
64   status_shellopen=0x10,
65   status_seekerror=0x04,
66   status_standby=0x02,
67   status_error=0x01
68};
69
70struct subheader
71{
72   unsigned char file,
73                        channel,
74                        submode,
75                        coding;
76};
77
78enum submode_flags
79{
80   submode_eof=0x80,
81   submode_realtime=0x40,
82   submode_form=0x20,
83   submode_trigger=0x10,
84   submode_data=0x08,
85   submode_audio=0x04,
86   submode_video=0x02,
87   submode_eor=0x01
88};
89
90//
91//
92//
93
94static const unsigned int max_xa_prefetch_distance=32,
95                                       max_cdda_prefetch_distance=32;
96
97//**************************************************************************
98//  DEVICE DEFINITIONS
99//**************************************************************************
100
101const device_type PSXCD = &device_creator<psxcd_device>;
102
103void psxcd_device::static_set_devname(device_t &device, const char *devname)
104{
105   psxcd_device &psxcd = downcast<psxcd_device &>(device);
106   psxcd.m_devname = devname;
107}
108
109psxcd_device::psxcd_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
110   : device_t(mconfig, PSXCD, "PSXCD", tag, owner, clock)
111{
112}
113
114void psxcd_device::device_start()
115{
116   unsigned int sysclk=machine().device<cpu_device>("maincpu")->clock()/2;
117   start_read_delay=(sysclk/60);
118   read_sector_cycles=(sysclk/75);
119   preread_delay=(read_sector_cycles>>2)-500;
120
121   m_sysclock = sysclk;
122
123   secleft = 0;
124   secsize = 2048;
125   res_queue = NULL;
126   cur_res = NULL;
127   open = false;
128   streaming = false;
129   sechead = 0;
130   sectail = 0;
131   secin = 0;
132   secskip = 0;
133   next_read_event = NULL;
134   cbp = cmdbuf;
135   first_open = true;
136
137   status=status_standby;
138   sr=8|1;
139   res=0;
140   ir=0;
141   mode=0;
142
143   driver = NULL;
144
145   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
146   {
147      m_timers[i] = timer_alloc(i, NULL);
148      m_timerinuse[i] = false;
149   }
150}
151
152//
153//
154//
155
156void psxcd_device::device_reset()
157{
158   stop_read();
159   open=false;
160
161   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
162   {
163      m_timerinuse[i] = false;
164   }
165
166   m_cddevice = machine().device(m_devname);
167   if (m_cddevice)
168   {
169      m_cd = cd_get_cdrom_file(m_cddevice);
170
171      if (m_cd)
172      {
173//          printf("psxcd: found disc!\n");
174         driver = open_mess_drv();
175         driver->set_machine(machine());
176         driver->set_cdrom_file(m_cd);
177      }
178      else
179      {
180         driver = NULL;
181//          printf("psxcd: Found device, but no disc\n");
182      }
183   }
184   else
185   {
186      driver = NULL;
187//      printf("psxcd: Device [%s] not found!\n", m_devname);
188   }
189
190}
191
192//
193//
194//
195
196unsigned char psxcd_device::read_byte(const unsigned int addr)
197{
198   unsigned char ret = 0;
199
200   switch (addr&3)
201   {
202      case 0: ret=sr; break;
203      case 1:
204         ret=res;
205         if ((cur_res) && (rdp<cur_res->sz))
206         {
207            res=cur_res->data[rdp++];
208            sr|=(1<<5);
209         } else
210         {
211            if ((cur_res) && (cur_res->res&0x10))
212            {
213               global_free(cur_res);
214               cur_res=NULL;
215            }
216
217            sr&=~(1<<5);
218         }
219         break;
220      case 2: ret=0; break;
221      case 3: ret=ir; break;
222   }
223
224   #ifdef debug_cdrom_registers
225      printf("cdrom: read byte %08x = %02x (PC=%x)\n",addr,ret,cpu_get_pc(machine().device("maincpu")));
226   #endif
227
228   return ret;
229}
230
231//
232//
233//
234
235void psxcd_device::write_byte(const unsigned int addr, const unsigned char byte)
236{
237   #ifdef debug_cdrom_registers
238      printf("cdrom: write byte %08x = %02x (PC=%x)\n",addr,byte,cpu_get_pc(machine().device("maincpu")));
239   #endif
240
241   switch (addr&3)
242   {
243      case 0:
244         cmdmode=byte&1;
245         if (cmdmode==0)
246         {
247            cbp=cmdbuf;
248         } else
249         {
250            if (! cur_res)
251            {
252               if (cur_res) global_free(cur_res);
253
254               if (res_queue)
255               {
256                  #ifdef debug_cdrom_registers
257                     printf("cdrom: nextres\n");
258                  #endif
259
260                  cur_res=res_queue;
261                  res_queue=res_queue->next;
262                  ir=cur_res->res&0xf;
263                  rdp=0;
264
265                  if (cur_res->sz)
266                  {
267                     res=cur_res->data[rdp++];
268                     sr|=(1<<5);
269                  } else
270                  {
271                     sr&=~(1<<5);
272                  }
273               } else
274               {
275                  //ir=0;
276                  cur_res=NULL;
277               }
278            }
279            /*else
280                {
281                    if (rdp>=cur_res->sz)
282                    {
283                        sr&=~(1<<5);
284                    } else
285                    {
286                        sr|=~(1<<5);
287                        res=cur_res->data[rdp++];
288                    }
289                }
290                */
291         }
292         break;
293
294      case 1:
295         if (cmdmode==0)
296         {
297            write_command(byte);
298         }
299         break;
300
301      case 2:
302         if (cmdmode==0)
303         {
304            *cbp++=byte;
305         } else
306         {
307            // ?flush buffer?
308         }
309         break;
310
311      case 3:
312         if (byte==0x07)
313         {
314            if (cur_res)
315            {
316               global_free(cur_res);
317               cur_res=NULL;
318               sr&=~(1<<5);
319            }
320            ir=0;
321         }
322         break;
323   }
324}
325
326psxcd_device::command_info psxcd_device::cmd_table[num_commands]=
327{
328   { &psxcd_device::cdcmd_sync,            "sync"         },   // 00
329   { &psxcd_device::cdcmd_nop,            "nop"            },   // 01
330   { &psxcd_device::cdcmd_setloc,         "setloc"      },   // 02
331   { &psxcd_device::cdcmd_play,            "play"         },   // 03
332   { &psxcd_device::cdcmd_forward,      "forward"      },   // 04
333   { &psxcd_device::cdcmd_backward,      "backward"   },   // 05
334   { &psxcd_device::cdcmd_readn,         "readn"         },   // 06
335   { &psxcd_device::cdcmd_standby,      "standby"      },   // 07
336   { &psxcd_device::cdcmd_stop,            "stop"         },   // 08
337   { &psxcd_device::cdcmd_pause,         "pause"         },   // 09
338   { &psxcd_device::cdcmd_init,            "init"         },   // 0a
339   { &psxcd_device::cdcmd_mute,            "mute"         },   // 0b
340   { &psxcd_device::cdcmd_demute,         "demute"      },   // 0c
341   { &psxcd_device::cdcmd_setfilter,   "setfilter"   },   // 0d
342   { &psxcd_device::cdcmd_setmode,      "setmode"      },   // 0e
343   { &psxcd_device::cdcmd_getparam,      "getparam"   },   // 0f
344   { &psxcd_device::cdcmd_getlocl,      "getlocl"      },   // 10
345   { &psxcd_device::cdcmd_getlocp,      "getlocp"      },   // 11
346   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 12
347   { &psxcd_device::cdcmd_gettn,         "gettn"         },   // 13
348   { &psxcd_device::cdcmd_gettd,         "gettd"         },   // 14
349   { &psxcd_device::cdcmd_seekl,         "seekl"         },   // 15
350   { &psxcd_device::cdcmd_seekp,         "seekp"         },   // 16
351   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 17
352   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 18
353   { &psxcd_device::cdcmd_test,            "test"         },   // 19
354   { &psxcd_device::cdcmd_id,               "id"            },   // 1a
355   { &psxcd_device::cdcmd_reads,         "reads"         },   // 1b
356   { &psxcd_device::cdcmd_reset,         "reset"         },   // 1c
357   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 1d
358   { &psxcd_device::cdcmd_readtoc,      "readtoc"      },   // 1e
359};
360
361//
362//
363//
364
365void psxcd_device::write_command(const unsigned char byte)
366{
367   assert(byte<num_commands);
368   (this->*cmd_table[byte].func)();
369}
370
371//
372//
373//
374
375void psxcd_device::cdcmd_sync()
376{
377   #ifdef debug_cdrom
378      printf("cdrom: sync\n");
379   #endif
380
381   stop_read();
382   send_result(intr_acknowledge);
383}
384
385static int open_nops=0;
386
387void psxcd_device::cdcmd_nop()
388{
389   #ifdef debug_cdrom
390      printf("cdrom: nop\n");
391   #endif
392
393   //stop_read();
394
395   if ((! open) && (driver))
396   {
397      if (open_nops==0)
398      {
399         open=driver->read_toc();
400      } else
401      {
402         open_nops--;
403      }
404   }
405
406   send_result(intr_complete);
407}
408
409void psxcd_device::cdcmd_setloc()
410{
411   #ifdef debug_cdrom
412      printf("cdrom: setloc %08x:%08x:%08x\n",
413            cmdbuf[0],
414            cmdbuf[1],
415            cmdbuf[2]);
416   #endif
417
418   stop_read();
419
420   unsigned char l[3];
421   l[0]=bcd_to_decimal(cmdbuf[0]);
422   l[1]=bcd_to_decimal(cmdbuf[1]);
423   l[2]=bcd_to_decimal(cmdbuf[2]);
424
425   if ((l[0]>0) || (l[1]>=2))
426   {
427      loc[0]=l[0];
428      loc[1]=l[1];
429      loc[2]=l[2];
430   } else
431   {
432      printf("setloc out of range: %02d:%02d:%02d\n",
433                l[0],l[1],l[2]);
434   }
435
436   send_result(intr_complete);
437}
438
439void psxcd_device::cdcmd_play()
440{
441   #ifdef debug_cdrom
442      printf("cdrom: play %02x %02x %02x => %d\n", loc[0], loc[1], loc[2], msf_to_sector(loc));
443   #endif
444
445   curpos[0]=loc[0];
446   curpos[1]=loc[1];
447   curpos[2]=loc[2];
448
449   if ((curpos[0]==0) &&
450         (curpos[1]==0) &&
451         (curpos[2]==0))
452   {
453      send_result(intr_acknowledge);
454   } else
455   {
456      stop_read();
457      start_play();
458      send_result(intr_acknowledge);
459   }
460}
461
462void psxcd_device::cdcmd_forward()
463{
464   #ifdef debug_cdrom
465      printf("cdrom: forward\n");
466   #endif
467}
468
469void psxcd_device::cdcmd_backward()
470{
471   #ifdef debug_cdrom
472      printf("cdrom: backward\n");
473   #endif
474}
475
476void psxcd_device::cdcmd_readn()
477{
478   if (driver)
479   {
480      #ifdef debug_cdrom
481         printf("cdrom: readn\n");
482      #endif
483
484      curpos[0]=loc[0];
485      curpos[1]=loc[1];
486      curpos[2]=loc[2];
487
488      stop_read();
489      start_read();
490      send_result(intr_complete);
491   } else
492   {
493      printf("read without driver\n");
494      send_result(intr_diskerror);
495   }
496}
497
498void psxcd_device::cdcmd_standby()
499{
500   #ifdef debug_cdrom
501      printf("cdrom: standby\n");
502   #endif
503
504   stop_read();
505   send_result(intr_acknowledge);
506}
507
508void psxcd_device::cdcmd_stop()
509{
510   #ifdef debug_cdrom
511      printf("cdrom: stop\n");
512   #endif
513
514   stop_read();
515   send_result(intr_acknowledge);
516}
517
518void psxcd_device::cdcmd_pause()
519{
520   #ifdef debug_cdrom
521      printf("cdrom: pause\n");
522   #endif
523
524   stop_read();
525
526   send_result(intr_acknowledge);
527}
528
529void psxcd_device::cdcmd_init()
530{
531   #ifdef debug_cdrom
532      printf("cdrom: init\n");
533   #endif
534
535   stop_read();
536   mode=0;
537   sr|=0x10;
538
539   send_result(intr_acknowledge);
540}
541
542void psxcd_device::cdcmd_mute()
543{
544   #ifdef debug_cdrom
545      printf("cdrom: mute\n");
546   #endif
547
548   send_result(intr_acknowledge);
549}
550
551void psxcd_device::cdcmd_demute()
552{
553   #ifdef debug_cdrom
554      printf("cdrom: demute\n");
555   #endif
556
557   send_result(intr_acknowledge);
558}
559
560void psxcd_device::cdcmd_setfilter()
561{
562   #ifdef debug_cdrom
563      printf("cdrom: setfilter %08x,%08x\n",cmdbuf[0],cmdbuf[1]);
564   #endif
565
566   filter_file=cmdbuf[0];
567   filter_channel=cmdbuf[1];
568
569   send_result(intr_acknowledge);
570}
571
572void psxcd_device::cdcmd_setmode()
573{
574   #ifdef debug_cdrom
575      printf("cdrom: setmode %08x\n",cmdbuf[0]);
576   #endif
577
578   mode=cmdbuf[0];
579
580   switch ((mode&mode_size_mask)>>mode_size_shift)
581   {
582      case 1:
583         secsize=2328;
584         secskip=24;
585         break;
586
587      case 2:
588         secsize=2340;
589         secskip=12;
590         break;
591
592      default:
593         secsize=2048;
594         secskip=24;
595         break;
596   }
597
598   send_result(intr_complete);
599}
600
601void psxcd_device::cdcmd_getparam()
602{
603   unsigned char data[6]=
604   {
605      status,
606      mode,
607      filter_file,
608      filter_channel,
609      0,
610      0
611   };
612
613   #ifdef debug_cdrom
614      printf("cdrom: getparam [%02x %02x %02x %02x %02x %02x]\n",
615                           data[0],
616                           data[1],
617                           data[2],
618                           data[3],
619                           data[4],
620                           data[5]);
621   #endif
622
623   send_result(intr_complete,data,6);
624}
625
626#if 0
627static void add_loc(unsigned char *dst, const unsigned char *src1, const unsigned char *src2)
628{
629   int f=src1[2]+src2[2],
630         s=src1[1]+src2[1],
631         m=src1[0]+src2[0];
632   while (f>=75) { s++; f-=75; }
633   while (s>=60) { m++; s-=60; }
634
635   dst[0]=m;
636   dst[1]=s;
637   dst[2]=f;
638}
639#endif
640static void sub_loc(unsigned char *dst, const unsigned char *src1, const unsigned char *src2)
641{
642   int f=src1[2]-src2[2],
643         s=src1[1]-src2[1],
644         m=src1[0]-src2[0];
645   while (f<0) { s--; f+=75; }
646   while (s<0) { m--; s+=60; }
647
648   if (m<0)
649      m=s=f=0;
650
651   dst[0]=m;
652   dst[1]=s;
653   dst[2]=f;
654}
655
656void psxcd_device::cdcmd_getlocl()
657{
658   #ifdef debug_cdrom
659      printf("cdrom: getlocl\n");
660   #endif
661
662   #ifdef debug_cdrom
663      printf("cdrom: getlocl [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
664                           lastsechdr[0],
665                           lastsechdr[1],
666                           lastsechdr[2],
667                           lastsechdr[3],
668                           lastsechdr[4],
669                           lastsechdr[5],
670                           lastsechdr[6],
671                           lastsechdr[7]);
672   #endif
673
674   send_result(intr_complete,lastsechdr,8);
675}
676
677void psxcd_device::cdcmd_getlocp()
678{
679   unsigned char tloc[3],
680                        twosec[3]={ 0,2,0 };
681   sub_loc(tloc,loc,twosec);
682
683   unsigned char data[8]=
684   {
685      0x01,                     // track
686      0x01,                     // index
687      decimal_to_bcd(tloc[0]),   // min
688      decimal_to_bcd(tloc[1]),   // sec
689      decimal_to_bcd(tloc[2]),   // frame
690      decimal_to_bcd(loc[0]),   // amin
691      decimal_to_bcd(loc[1]),   // asec
692      decimal_to_bcd(loc[2])   // aframe
693   };
694
695   //unsigned char data[8]={ 2,1,0,0xff,0xff,0xff,0xff,0xff };
696
697   #ifdef debug_cdrom
698      printf("cdrom: getlocp [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
699                           data[0],
700                           data[1],
701                           data[2],
702                           data[3],
703                           data[4],
704                           data[5],
705                           data[6],
706                           data[7]);
707   #endif
708
709   send_result(intr_complete,data,8);
710}
711
712void psxcd_device::cdcmd_illegal()
713{
714   assert(0);
715}
716
717void psxcd_device::cdcmd_gettn()
718{
719   #ifdef debug_cdrom
720      printf("cdrom: gettn\n");
721   #endif
722
723   assert(driver);
724
725   unsigned char data[3]=
726   {
727      status,
728      decimal_to_bcd(driver->get_first_track()),
729      decimal_to_bcd(driver->get_num_tracks())
730   };
731
732   //stop_read();
733   send_result(intr_acknowledge,data,3);
734}
735
736void psxcd_device::cdcmd_gettd()
737{
738   unsigned char addr[3];
739   driver->get_track_address(bcd_to_decimal(cmdbuf[0]), addr);
740
741   unsigned char data[3]=
742   {
743      status,
744      decimal_to_bcd(addr[0]),
745      decimal_to_bcd(addr[1])
746   };
747
748   #ifdef debug_cdrom
749      printf("cdrom: gettd %02x [%02x %02x %02x]\n",
750                                     cmdbuf[0],
751                                     data[0],
752                                     data[1],
753                                     data[2]);
754   #endif
755
756   //stop_read();
757   send_result(intr_acknowledge,data,3);
758}
759
760void psxcd_device::cdcmd_seekl()
761{
762   #ifdef debug_cdrom
763      printf("cdrom: seekl [%02d:%02d:%02d]\n",loc[0],loc[1],loc[2]);
764   #endif
765
766   curpos[0]=loc[0];
767   curpos[1]=loc[1];
768   curpos[2]=loc[2];
769
770   send_result(intr_acknowledge);
771}
772
773void psxcd_device::cdcmd_seekp()
774{
775   #ifdef debug_cdrom
776      printf("cdrom: seekp\n");
777   #endif
778
779   curpos[0]=loc[0];
780   curpos[1]=loc[1];
781   curpos[2]=loc[2];
782
783   send_result(intr_acknowledge);
784}
785
786void psxcd_device::cdcmd_test()
787{
788   #ifdef debug_cdrom
789      printf("cdrom: test %08x\n",cmdbuf[0]);
790   #endif
791
792   static unsigned char data[4]=
793   {
794      0x95,
795      0x07,
796      0x06,
797      0xff
798   };
799
800   send_result(intr_complete,data,4);
801}
802
803void psxcd_device::cdcmd_id()
804{
805   #ifdef debug_cdrom
806      printf("cdrom: id\n");
807   #endif
808
809   if (! open)
810   {
811      if (driver)
812         open=driver->read_toc();
813   }
814
815   if (open)
816   {
817      static unsigned char gamedata[8] = { 0x08, 0x00, 0x00, 0x00, 'S', 'C', 'E', 'A' };
818      static unsigned char audiodata[8] = { 0x08, 0x90, 0x00, 0x00, 'S', 'C', 'E', 'A' };   // drops into the audio CD player.  08 80 goes to the menu.
819
820      if (cdrom_get_track_type(m_cd, 0) == CD_TRACK_AUDIO)
821      {
822         send_result(intr_acknowledge,audiodata,8);
823      }
824      else
825      {
826         send_result(intr_acknowledge,gamedata,8);
827      }
828   } else
829   {
830      status=status_error|status_shellopen;
831      send_result(intr_diskerror);
832   }
833}
834
835void psxcd_device::cdcmd_reads()
836{
837   curpos[0]=loc[0];
838   curpos[1]=loc[1];
839   curpos[2]=loc[2];
840
841   #ifdef skip_reads
842      #ifdef debug_cdrom
843         log("cdrom: reads [SKIPPING - RETURN COMPLETE]\n");
844      #endif
845
846      send_result(intr_complete);
847   #else
848      #ifdef debug_cdrom
849         printf("cdrom: reads\n");
850      #endif
851
852      stop_read();
853      start_streaming();
854   #endif
855}
856
857void psxcd_device::cdcmd_reset()
858{
859   #ifdef debug_cdrom
860      printf("cdrom: reset\n");
861   #endif
862}
863
864void psxcd_device::cdcmd_readtoc()
865{
866   #ifdef debug_cdrom
867      printf("cdrom: readtoc\n");
868   #endif
869
870   static unsigned char data[4]=
871   {
872      0x00,
873      0x00,
874      0xff,
875      0xfe
876   };
877
878   send_result(intr_complete|0x10,data,2);
879}
880
881//
882//
883//
884
885void psxcd_device::cmd_complete(command_result *res)
886{
887   bool doint=((res->res&0x10)==0);
888   #ifdef debug_cdrom
889   command_result *rf;
890   #endif
891
892   if (doint)
893   {
894      psx_irq_set(machine(), 0x0004);
895   }
896
897   add_result(res);
898
899   #ifdef debug_cdrom
900      if (doint)
901      {
902         printf("cdrom: irq [");
903         for (rf=res_queue; ((rf) && (rf->next)); rf=rf->next);
904            printf("%d ",rf->res);
905         printf("]\n");
906      }
907   #endif
908}
909
910//
911//
912//
913
914void psxcd_device::add_result(command_result *res)
915{
916   command_result *rf;
917
918   if (res_queue)
919   {
920      for (rf=res_queue; rf->next; rf=rf->next);
921      rf->next=res;
922   } else
923   {
924      res_queue=res;
925   }
926
927   res->next=NULL;
928}
929
930//
931//
932//
933
934event *psxcd_device::send_result(const unsigned int res,
935                                          const unsigned char *data,
936                                          const unsigned int sz,
937                                             const unsigned int delay)
938{
939   // Update shell open status
940
941   if (! open)
942   {
943      status=status_error|status_shellopen;
944   }
945   else
946   {
947      if (status&status_shellopen)
948         status=status_standby;
949   }
950
951   // Prepare event
952
953   event *ev=new event;
954   ev->t=delay;
955   ev->type=event_cmd_complete;
956
957   command_result *cr=(command_result *)ev->data;
958
959   cr->res=res;
960   if (sz)
961   {
962      assert(sz<sizeof(cr->data));
963      memcpy(cr->data,data,sz);
964      cr->sz=sz;
965   } else
966   {
967      cr->data[0]=status;
968      cr->sz=1;
969   }
970
971   // Avoid returning results after sector read results -
972   // delay the sector read slightly if necessary
973
974   UINT64 systime = machine().device<cpu_device>("maincpu")->total_cycles();
975   if ((next_read_event) && ((systime+ev->t)>(next_sector_t)))
976   {
977      UINT32 hz = m_sysclock / (delay + 2000);
978      next_read_event->timer->adjust(attotime::from_hz(hz), 0, attotime::never);
979
980      for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
981      {
982         if (m_eventfortimer[i] == next_read_event)
983         {
984            printf("Adjusting timer %d to %d hz]n", i, hz);
985         }
986      }
987   }
988
989   add_system_event(ev);
990   return ev;
991}
992
993//
994//
995//
996
997void psxcd_device::start_dma(UINT8 *mainram, UINT32 size)
998{
999   if ((int)size>secleft)
1000   {
1001      printf("cdrom: dma past end of sector (secleft=%d sz=%d)\n",secleft,size);
1002   }
1003
1004//  printf("cdrom: start dma %d bytes, %d remaining, secptr %p\n", size, secleft, secptr);
1005   #ifdef debug_cdrom
1006   if (size==12)
1007   {
1008      char poo[1024],*pp=poo;
1009      for (unsigned int i=0; i<12; i++)
1010      {
1011         sprintf(pp,"%02x ",secptr[i]);
1012         pp+=3;
1013      }
1014      printf("cdrom: data=%s\n",poo);
1015   }
1016   #endif
1017
1018   memcpy(mainram, secptr, size);
1019   memset(secptr, 0xff, size);
1020
1021   secptr += size;
1022   secleft -= size;
1023
1024   if (secleft<0) secleft=0;
1025   if (secleft==0) sr&=~0x40;
1026}
1027
1028//
1029//
1030//
1031
1032void psxcd_device::prefetch_next_sector()
1033{
1034   unsigned int pos=msf_to_sector(curpos);
1035   assert(driver);
1036   driver->prefetch_sector(pos);
1037}
1038
1039//
1040//
1041//
1042
1043bool psxcd_device::read_next_sector(const bool block)
1044{
1045   unsigned int pos=msf_to_sector(curpos);
1046   unsigned char *buf=&secbuf[sechead*raw_sector_size];
1047   assert(driver);
1048   assert(secin<sector_buffer_size);
1049
1050//  printf("read_next_sector: sec %d, sechead %d, raw_sector_size %d\n", pos, sechead, raw_sector_size);
1051   if (driver->read_sector(pos, buf, block))
1052   {
1053//      printf("buf contents = %02x %02x | %02x %02x\n", buf[0], buf[1], buf[0x20], buf[0x21]);
1054
1055      sechead=(sechead+1)&(sector_buffer_size-1);
1056      secin++;
1057
1058      memcpy(lastsechdr,&secbuf[raw_sector_size*sectail]+12,8);
1059
1060      return true;
1061   } else
1062   {
1063      return false;
1064   }
1065}
1066
1067//
1068//
1069//
1070
1071void psxcd_device::read_sector()
1072{
1073   next_read_event=NULL;
1074
1075   if (status & status_reading)
1076   {
1077      bool isend=false;
1078
1079      if (read_next_sector(false))
1080      {
1081         unsigned char *rawsec;
1082
1083         if ((mode&mode_adpcm) && (streaming))
1084         {
1085            rawsec=&secbuf[raw_sector_size*sectail];
1086            secptr=rawsec+24;
1087            secleft=2048;
1088         }
1089         else
1090         {
1091            rawsec=&secbuf[raw_sector_size*sectail];
1092            secptr=rawsec+secskip;
1093            secleft=secsize;
1094         }
1095
1096         secin--;
1097         sectail=(sectail+1)&(sector_buffer_size-1);
1098
1099         //
1100
1101         bool isxa=false;
1102
1103         subheader *sub=(subheader *)(rawsec+16);
1104
1105         #ifdef dump_subheader
1106            printf("cdrom: subheader file=%02x chan=%02x submode=%02x coding=%02x [%02x%02x%02x%02x]\n",
1107                  sub->file,
1108                  sub->channel,
1109                  sub->submode,
1110                  sub->coding,
1111                  rawsec[0xc],
1112                  rawsec[0xd],
1113                  rawsec[0xe],
1114                  rawsec[0xf]);
1115         #endif
1116
1117         status&=~status_playing;
1118         isxa=((mode&mode_adpcm) && (sub->submode&submode_audio));
1119
1120         if (((mode&mode_channel)==0) ||
1121               ((sub->file==filter_file) && (sub->channel==filter_channel)))
1122         {
1123            if (isxa)
1124            {
1125               if (sub->submode&submode_eof)
1126               {
1127                  isend=true;
1128                  //printf("end of file\n");
1129               }
1130
1131               #ifndef disable_xa_prefetch
1132                  unsigned int cursec=msf_to_sector(curpos);
1133
1134                  if (xa_prefetch_sector==-1)
1135                  {
1136                     xa_prefetch_sector=cursec;
1137                     machine().device<spu_device>("spu")->flush_xa();
1138                  }
1139
1140                  unsigned char *xaptr;
1141                  unsigned int xasecsz;
1142
1143                  while (1)
1144                  {
1145                     int dist=xa_prefetch_sector-cursec;
1146                     if (dist>max_xa_prefetch_distance) break;
1147
1148                     xaptr=driver->get_prefetch_sector(xa_prefetch_sector,&xasecsz);
1149                     if (! xaptr) break;
1150
1151                     switch (xasecsz)
1152                     {
1153                        case 2336:
1154                           break;
1155
1156                        default:
1157                           xaptr+=16;
1158                           break;
1159                     }
1160
1161                     subheader *xasub=(subheader *)xaptr;
1162
1163                     if ((xasub->submode&submode_audio) &&
1164                           (((mode&mode_channel)==0) ||
1165                            ((xasub->file==sub->file) &&
1166                              (xasub->channel==sub->channel))))
1167                     {
1168                        if (! machine().device<spu_device>("spu")->play_xa(xa_prefetch_sector,xaptr))
1169                           break;
1170                     }
1171                     xa_prefetch_sector++;
1172                  }
1173               #else
1174                  machine().device<spu_device>("spu")->play_xa(0,rawsec+16);
1175               #endif
1176
1177               status|=status_playing;
1178            }
1179         }
1180
1181         if ((mode&mode_autopause)==0)
1182            isend=false;
1183
1184         //
1185
1186         curpos[2]++;
1187         if (curpos[2]==75)
1188         {
1189            curpos[2]=0;
1190            curpos[1]++;
1191            if (curpos[1]==60)
1192            {
1193               curpos[1]=0;
1194               curpos[0]++;
1195            }
1196         }
1197
1198         loc[0]=curpos[0];
1199         loc[1]=curpos[1];
1200         loc[2]=curpos[2];
1201
1202         //
1203
1204         command_result *res=new command_result;
1205         res->res=isend?intr_dataend:intr_dataready;
1206         res->data[0]=status;
1207         res->sz=1;
1208
1209         sr|=0x40;
1210
1211         if ((streaming) && (isxa))
1212         {
1213            global_free(res);
1214            res=NULL;
1215         }
1216
1217         if (res)
1218         {
1219            #ifdef debug_cdrom
1220               printf("cdrom:: data ready\n");
1221            #endif
1222
1223            cmd_complete(res);
1224         }
1225      }
1226
1227      if (! isend)
1228      {
1229         unsigned int cyc=read_sector_cycles;
1230         if (mode&mode_double_speed) cyc>>=1;
1231
1232         next_sector_t+=cyc;
1233
1234         event *ev=new event;
1235         ev->t=cyc;
1236         ev->type=event_read_sector;
1237         next_read_event=ev;
1238         add_system_event(ev);
1239
1240         //read_next_sector();
1241      } else
1242      {
1243         printf("autopause xa\n");
1244         stop_read();
1245      }
1246   }
1247}
1248
1249//
1250//
1251//
1252
1253bool psxcd_device::play_cdda_sector(const unsigned int sector,
1254                                                 unsigned char *rawsec)
1255{
1256   bool isdata=true;
1257
1258   if (rawsec[0]!=0)
1259   {
1260      isdata=false;
1261   } else
1262   {
1263      for (int i=0; i<10; i++)
1264         if (rawsec[i+1]!=0xff)
1265         {
1266            isdata=false;
1267            break;
1268         }
1269   }
1270
1271   if (! isdata)
1272   {
1273      return machine().device<spu_device>("spu")->play_cdda(sector,rawsec);
1274   } else
1275   {
1276      return true;
1277   }
1278}
1279
1280//
1281//
1282//
1283
1284void psxcd_device::play_sector()
1285{
1286   next_read_event=NULL;
1287
1288   if (status&status_playing)
1289   {
1290      #ifdef disable_cdda_prefetch
1291      unsigned char *rawsec=&secbuf[raw_sector_size*sectail];
1292      #endif
1293      secin--;
1294      sectail=(sectail+1)&(sector_buffer_size-1);
1295
1296      //
1297
1298      #ifndef disable_cdda_prefetch
1299         unsigned int cursec=msf_to_sector(curpos);
1300
1301         if (cdda_prefetch_sector==-1)
1302         {
1303            cdda_prefetch_sector=cursec;
1304            machine().device<spu_device>("spu")->flush_cdda();
1305         }
1306
1307         unsigned char *cddaptr;
1308//          bool full=false;
1309         unsigned int cddasecsz;
1310
1311         while (1)
1312         {
1313            int dist=cdda_prefetch_sector-cursec;
1314            if (dist>max_cdda_prefetch_distance) break;
1315
1316            cddaptr=driver->get_prefetch_sector(cdda_prefetch_sector,&cddasecsz);
1317            if (! cddaptr) break;
1318
1319            if (! play_cdda_sector(cdda_prefetch_sector,cddaptr))
1320               break;
1321
1322            cdda_prefetch_sector++;
1323         }
1324      #else
1325         play_cdda_sector(0,rawsec);
1326      #endif
1327
1328      //
1329
1330      curpos[2]++;
1331      if (curpos[2]==75)
1332      {
1333         curpos[2]=0;
1334         curpos[1]++;
1335         if (curpos[1]==60)
1336         {
1337            curpos[1]=0;
1338            curpos[0]++;
1339         }
1340      }
1341
1342      loc[0]=curpos[0];
1343      loc[1]=curpos[1];
1344      loc[2]=curpos[2];
1345
1346      //
1347
1348      if (mode&mode_autopause)
1349      {
1350         if (msf_to_sector(loc)>=autopause_sector)
1351         {
1352            printf("autopause cdda\n");
1353
1354            command_result *res=new command_result;
1355            res->res=intr_dataend;
1356            res->data[0]=status_standby;
1357            res->sz=1;
1358            cmd_complete(res);
1359            stop_read();
1360            return;
1361         }
1362      }
1363
1364      if (mode&mode_report)
1365      {
1366         command_result *res=new command_result;
1367         res->res=intr_complete;   //dataready;
1368
1369         res->data[0]=status_playing|status_standby;
1370         res->data[1]=0x01;
1371         res->data[2]=0x80;
1372         res->data[3]=decimal_to_bcd(loc[0]);
1373         res->data[4]=decimal_to_bcd(loc[1])|0x80;
1374         res->data[5]=decimal_to_bcd(loc[2]);
1375
1376         res->sz=8;
1377
1378         cmd_complete(res);
1379      }
1380
1381      unsigned int cyc=read_sector_cycles;
1382
1383      event *ev=new event;
1384      ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1385      ev->type=event_play_sector;
1386
1387      next_sector_t+=cyc;
1388
1389      next_read_event=ev;
1390      add_system_event(ev);
1391
1392      read_next_sector();
1393   }
1394}
1395
1396
1397//
1398//
1399//
1400
1401void psxcd_device::preread_sector()
1402{
1403   next_read_event=NULL;
1404
1405   //
1406
1407   event *ev=new event;
1408
1409   unsigned int pos=msf_to_sector(curpos);
1410   unsigned char *buf=&secbuf[sechead*raw_sector_size];
1411   if (! driver->read_sector(pos,buf,false))
1412   {
1413      ev->t=(m_sysclock/60);
1414      ev->type=event_preread_sector;
1415
1416      unsigned int cyc=read_sector_cycles;
1417      if (mode&mode_double_speed) cyc>>=1;
1418      next_sector_t=ev->t+(cyc-preread_delay)+machine().device<cpu_device>("maincpu")->total_cycles();
1419   } else
1420   {
1421      memcpy(lastsechdr,buf+12,8);
1422
1423      //
1424
1425      command_result *res=new command_result;
1426      res->res=intr_complete;
1427      res->data[0]=status;
1428      res->sz=1;
1429
1430      #ifdef debug_cdrom
1431         printf("cdrom: read acknowledge\n");
1432      #endif
1433
1434      cmd_complete(res);
1435
1436      //
1437
1438      ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1439      ev->type=event_read_sector;
1440
1441      //read_next_sector();
1442   }
1443
1444   next_read_event=ev;
1445   add_system_event(ev);
1446}
1447
1448//
1449//
1450//
1451
1452void psxcd_device::start_read()
1453{
1454   #ifdef debug_cdrom
1455      printf("cdrom: start read\n");
1456   #endif
1457
1458   assert((status&(status_reading|status_playing))==0);
1459
1460   status|=status_reading;
1461
1462   secin=sechead=sectail=0;
1463   xa_prefetch_sector=-1;
1464
1465   unsigned int cyc=read_sector_cycles;
1466   if (mode&mode_double_speed) cyc>>=1;
1467
1468   INT64 systime=machine().device<cpu_device>("maincpu")->total_cycles();
1469
1470   systime+=start_read_delay;
1471
1472   event *ev=new event;
1473   ev->t=start_read_delay+preread_delay;
1474   ev->type=event_preread_sector;
1475
1476   next_sector_t=systime+cyc;
1477   next_read_event=ev;
1478   add_system_event(ev);
1479
1480   prefetch_next_sector();
1481}
1482
1483//
1484//
1485//
1486
1487void psxcd_device::start_streaming()
1488{
1489   assert(! streaming);
1490
1491   streaming=true;
1492   start_read();
1493}
1494
1495//
1496//
1497//
1498
1499void psxcd_device::start_play()
1500{
1501   #ifdef debug_cdrom
1502      printf("cdrom: start play\n");
1503   #endif
1504
1505   status|=status_playing;
1506
1507   secin=sechead=sectail=0;
1508   cdda_prefetch_sector=-1;
1509
1510   if (mode&mode_autopause)
1511   {
1512      unsigned int pos=msf_to_sector(curpos);
1513      driver->find_track(pos+150,NULL,&autopause_sector);
1514//      printf("pos=%d auto=%d\n",pos,autopause_sector);
1515   }
1516
1517   unsigned int cyc=read_sector_cycles;
1518
1519   next_sector_t=machine().device<cpu_device>("maincpu")->total_cycles()+cyc;
1520
1521   event *ev=new event;
1522   ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1523   ev->type=event_play_sector;
1524
1525   next_sector_t+=cyc;
1526
1527   next_read_event=ev;
1528   add_system_event(ev);
1529
1530   read_next_sector();
1531}
1532
1533//
1534//
1535//
1536
1537void psxcd_device::stop_read()
1538{
1539   #ifdef debug_cdrom
1540      if (status&status_reading)
1541         printf("cdrom: stop read\n");
1542   #endif
1543   status&=~(status_reading|status_playing);
1544   streaming=false;
1545
1546   if (next_read_event)
1547   {
1548      next_read_event->timer->adjust(attotime::never, 0, attotime::never);
1549      for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
1550      {
1551         if (m_timers[i] == next_read_event->timer)
1552         {
1553            m_timerinuse[i] = false;
1554            m_eventfortimer[i] = false;
1555            break;
1556         }
1557      }
1558      global_free(next_read_event);
1559      next_read_event=NULL;
1560   }
1561
1562   unsigned int sector=msf_to_sector(curpos);
1563   machine().device<spu_device>("spu")->flush_xa(sector);
1564   machine().device<spu_device>("spu")->flush_cdda(sector);
1565}
1566
1567//
1568//
1569//
1570
1571void psxcd_device::set_driver(cdrom_driver *d)
1572{
1573   char err[1024];
1574
1575   if (d)
1576   {
1577      if (d->is_usable(err,1024))
1578      {
1579         driver=d;
1580         open=false;
1581
1582         if (! first_open)
1583         {
1584            command_result *res=new command_result;
1585            res->res=intr_acknowledge;
1586            res->data[0]=status_standby;
1587            res->sz=1;
1588
1589            cmd_complete(res);
1590
1591            open_nops=10;
1592         }
1593
1594         first_open=false;
1595      }
1596   } else
1597   {
1598      driver=NULL;
1599      open=false;
1600
1601      command_result *res=new command_result;
1602      res->res=intr_acknowledge;
1603      res->data[0]=status_shellopen;
1604      res->sz=1;
1605
1606      cmd_complete(res);
1607   }
1608}
1609
1610//
1611//
1612//
1613void psxcd_device::device_timer(emu_timer &timer, device_timer_id tid, int param, void *ptr)
1614{
1615   event *ev = m_eventfortimer[tid];
1616
1617//  printf("timer %d fired, performing event type %d\n", tid, ev->type);
1618
1619   switch (ev->type)
1620   {
1621      case event_cmd_complete:
1622      {
1623         #ifdef debug_cdrom
1624            printf("cdrom:: event cmd complete\n");
1625         #endif
1626
1627         command_result *res=new command_result;
1628         memcpy(res,ev->data,sizeof(command_result));
1629         cmd_complete(res);
1630         break;
1631      }
1632
1633      case event_preread_sector:
1634         preread_sector();
1635         break;
1636
1637      case event_read_sector:
1638         read_sector();
1639         break;
1640
1641      case event_play_sector:
1642         play_sector();
1643         break;
1644   }
1645
1646   // free the timer
1647//  printf("Freeing timer %d\n", tid);
1648   m_timers[tid]->adjust(attotime::never, 0, attotime::never);
1649   m_timerinuse[tid] = false;
1650   m_eventfortimer[tid] = NULL;
1651
1652   global_free(ev);
1653}
1654
1655void psxcd_device::add_system_event(event *ev)
1656{
1657   emu_timer *timer = NULL;
1658   int tnum = -1;
1659
1660   // allocate a timer for this event
1661   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
1662   {
1663      if (!m_timerinuse[i])
1664      {
1665         tnum = i;
1666         timer = m_timers[i];
1667         m_timerinuse[i] = true;
1668         m_eventfortimer[i] = ev;
1669         break;
1670      }
1671   }
1672
1673   if (tnum == -1)
1674   {
1675      fatalerror("PSXCD: ran out of timers!\n");
1676   }
1677
1678   // ev->t is in maincpu clock cycles
1679   UINT32 hz = m_sysclock / ev->t;
1680//  printf("add_system_event: event type %d for %d hz (using timer %d)\n", ev->type, hz, tnum);
1681   timer->adjust(attotime::from_hz(hz), tnum, attotime::never);
1682
1683   // back-reference the timer from the event
1684   ev->timer = timer;
1685}
1686
src/mame/machine/psxcd.h
r11505r11506
1#ifndef _included_psxcdrom_
2#define _included_psxcdrom_
3
4#include "imagedev/chd_cd.h"
5#include "psxcddrv.h"
6
7#define MAX_PSXCD_TIMERS   (4)
8
9class event;
10
11//
12//
13//
14
15const int num_commands=0x20;
16
17//
18//
19//
20
21#define PSXCD_TAG   "psxcd"
22
23//**************************************************************************
24//  INTERFACE CONFIGURATION MACROS
25//**************************************************************************
26
27#define MCFG_PSXCD_ADD(_devname) \
28    MCFG_DEVICE_ADD(PSXCD_TAG, PSXCD, 0) \
29   MCFG_PSXCD_DEVNAME(_devname)
30
31#define MCFG_PSXCD_DEVNAME(_name) \
32   psxcd_device::static_set_devname(*device, _name); \
33
34struct psxcd_interface
35{
36};
37
38class psxcd_device : public device_t,
39                     public psxcd_interface
40{
41public:
42
43    psxcd_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
44   // inline configuration helpers
45   static void static_set_devname(device_t &device, const char *devname);
46private:
47   struct command_result
48   {
49      unsigned char data[32], sz, res;
50      command_result *next;
51   };
52
53   static const unsigned int sector_buffer_size=16, default_irq_delay=16000;   //480;  //8000; //2000<<2;
54
55   unsigned char sr,res,ir,cmdmode,
56                        cmdbuf[64],*cbp,
57                        mode,
58                        loc[3],
59                        curpos[3],
60                        secbuf[raw_sector_size*sector_buffer_size],
61                        *secptr,
62                        filter_file,
63                        filter_channel,
64                        lastsechdr[8],
65                        status;
66   int rdp,secsize,secleft,secskip,
67         sechead,sectail,
68         secin;
69   command_result *res_queue,
70                         *cur_res;
71   bool open,
72          streaming,
73          first_open;
74   event *next_read_event;
75   INT64 next_sector_t;
76   unsigned int autopause_sector,
77                      xa_prefetch_sector,
78                      cdda_prefetch_sector;
79
80   cdrom_driver *driver;
81
82   unsigned int start_read_delay,
83                      read_sector_cycles,
84                      preread_delay;
85
86   void write_command(const unsigned char byte);
87
88   struct command_info
89   {
90      void (psxcd_device::*func)();
91      const char *name;
92   };
93
94   void cdcmd_sync();
95   void cdcmd_nop();
96   void cdcmd_setloc();
97   void cdcmd_play();
98   void cdcmd_forward();
99   void cdcmd_backward();
100   void cdcmd_readn();
101   void cdcmd_standby();
102   void cdcmd_stop();
103   void cdcmd_pause();
104   void cdcmd_init();
105   void cdcmd_mute();
106   void cdcmd_demute();
107   void cdcmd_setfilter();
108   void cdcmd_setmode();
109   void cdcmd_getparam();
110   void cdcmd_getlocl();
111   void cdcmd_getlocp();
112   void cdcmd_illegal();
113   void cdcmd_gettn();
114   void cdcmd_gettd();
115   void cdcmd_seekl();
116   void cdcmd_seekp();
117   void cdcmd_test();
118   void cdcmd_id();
119   void cdcmd_reads();
120   void cdcmd_reset();
121   void cdcmd_readtoc();
122
123   static command_info cmd_table[num_commands];
124
125   void cmd_complete(command_result *res);
126   void add_result(command_result *res);
127   event *send_result(const unsigned int res,
128                               const unsigned char *data=NULL,
129                               const unsigned int sz=0,
130                               const unsigned int delay=default_irq_delay);
131
132   void start_read();
133   void start_play();
134   void start_streaming();
135   void stop_read();
136   void read_sector();
137   void prefetch_next_sector();
138   bool read_next_sector(const bool block=true);
139   bool play_cdda_sector(const unsigned int sector, unsigned char *rawsec);
140   void play_sector();
141   void preread_sector();
142
143public:
144   void set_driver(cdrom_driver *d);
145   cdrom_driver *get_driver() const { return driver; }
146
147   virtual void device_start();
148   virtual void device_reset();
149   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
150
151   void start_dma(UINT8 *mainram, UINT32 size);
152
153   unsigned char read_byte(const unsigned int addr);
154   void write_byte(const unsigned int addr, const unsigned char byte);
155
156private:
157   emu_timer *m_timer;
158   UINT32 m_sysclock;
159   const char *m_devname;
160   device_t *m_cddevice;
161   cdrom_file   *m_cd;
162   emu_timer *m_timers[MAX_PSXCD_TIMERS];
163   event *m_eventfortimer[MAX_PSXCD_TIMERS];
164   bool m_timerinuse[MAX_PSXCD_TIMERS];
165
166   void add_system_event(event *ev);
167};
168
169
170// miniature version of pSX's event class, which we emulate with device timers
171const int max_event_data=64;
172
173class event
174{
175public:
176   UINT64 t;            // expire time
177   unsigned int type;
178   unsigned char data[max_event_data];
179   emu_timer *timer;
180};
181
182
183// device type definition
184extern const device_type PSXCD;
185
186#endif
src/mame/machine/psxcddrv.c
r11505r11506
1#include "emu.h"
2#include "includes/psx.h"
3#include "psxcd.h"
4#include "psxcddrv.h"
5
6//#define debug_mess_driver
7
8//
9//
10//
11
12static const unsigned int   num_pf_sectors=32, num_pf_buffers=16;
13
14//
15//
16//
17
18cdrom_driver::cdrom_driver()
19{
20   pf_head = 0;
21   pf_tail = 0;
22   num_pf = 0;
23   pf_head_sector = 0;
24   pf_status = NULL;
25   native_sector_size = raw_sector_size;
26   last_pf_status = NULL;
27   m_cd = NULL;
28   m_machine = NULL;
29
30//  printf("cdrom_driver base class init, pf_buffer size is %d\n", num_pf_sectors*num_pf_buffers*raw_sector_size);
31
32   pf_buffer=new unsigned char [num_pf_sectors*num_pf_buffers*raw_sector_size];
33
34   timestamp_frequency = 1;
35}
36
37cdrom_driver::~cdrom_driver()
38{
39   global_free(pf_buffer);
40}
41
42//
43//
44//
45
46void cdrom_driver::set_native_sector_size(const unsigned int sz)
47{
48   native_sector_size=sz;
49}
50
51//
52//
53//
54
55void cdrom_driver::cancel_io()
56{
57   if (pf_status)
58   {
59      pf_status->cancel();
60      pf_status->release();
61   }
62
63   pf_status=NULL;
64}
65
66//
67//
68//
69
70void cdrom_driver::prefetch_sector(const unsigned int sec)
71{
72   int numpfsec=num_pf*num_pf_sectors,
73         pfsec=sec-pf_head_sector;
74
75//  printf("prefetch_sector: %d\n", sec);
76
77   if ((pfsec<0) || (pfsec>numpfsec))
78   {
79      // Sector is not in prefetch buffer, abort current prefetch
80
81      if (pf_status)
82      {
83         pf_status->cancel();
84         pf_status->release();
85      }
86
87      // Reset the buffer and begin a new prefetch
88
89      pf_head=0;
90      pf_tail=0;
91      num_pf=1;
92      pf_head_sector=sec;
93
94      last_pf_status=NULL;
95      pf_status=read_sectors(sec, num_pf_sectors, pf_buffer);
96   }
97}
98
99//
100//
101//
102
103bool cdrom_driver::is_prefetch_sector_loaded(const unsigned int sec)
104{
105   int numpfsec=num_pf*num_pf_sectors,
106         pfsec=sec-pf_head_sector;
107
108   if ((pfsec<0) || (pfsec>=numpfsec))
109   {
110      // Requested sector is not in prefetch buffer, begin a new prefetch
111
112      prefetch_sector(sec);
113      pfsec=sec-pf_head_sector;
114      numpfsec=num_pf*num_pf_sectors;
115   }
116
117   int blk=pfsec/num_pf_sectors;
118
119   if (blk>0)
120   {
121      // Discard blocks below the prefetch point
122
123      pf_head=(pf_head+blk)%num_pf_buffers;
124      pf_head_sector+=num_pf_sectors*blk;
125      pfsec-=num_pf_sectors*blk;
126      num_pf-=blk;
127      blk=0;
128   }
129
130   bool comp=((! pf_status) || (pf_status->complete()));
131
132   if (! comp)
133   {
134      INT64 curtime=m_machine->device<cpu_device>("maincpu")->total_cycles();
135
136      if (last_pf_status!=pf_status)
137      {
138         last_pf_status=pf_status;
139         pf_timeout_begin=curtime;
140      }
141
142      INT64 timeout=curtime-pf_timeout_begin;
143      int timeout_sec=(int)(timeout/timestamp_frequency);
144      if (timeout_sec>20)
145      {
146         printf("cdrom: prefetch timed out, trying again...\n");
147         num_pf=0;
148         pf_head_sector=-1;
149         prefetch_sector(sec);
150         return is_prefetch_sector_loaded(sec);
151      }
152   }
153
154   if ((num_pf<num_pf_buffers) && (comp))
155   {
156      // The prefetch buffer is not full and we are not waiting on IO,
157      // prefetch the next block
158
159      pf_tail=(pf_tail+1)%num_pf_buffers;
160      num_pf++;
161
162      int nxtsec=pf_head_sector+((num_pf-1)*num_pf_sectors);
163      unsigned char *ptr=pf_buffer+((pf_tail*num_pf_sectors)*native_sector_size);
164
165      if (pf_status)
166      {
167         pf_status->release();
168         pf_status=last_pf_status=NULL;
169      }
170
171      pf_status=read_sectors(nxtsec, num_pf_sectors, ptr);
172   }
173
174   if (blk==(num_pf-1))
175   {
176      // The sector we want is in the last block in the prefetch buffer
177      // which might still be loading, check if the sector we want is loaded
178#if 0 // we do not load async in MESS
179      INT64 trans=pf_status->bytes_transferred();
180      unsigned int secmod=pfsec%num_pf_sectors,
181                         needbytes=(secmod+1)*native_sector_size;
182
183      if (trans<needbytes)
184      {
185         // The sector is not loaded yet
186
187         return false;
188      } else
189#endif
190      {
191         // The sector is loaded
192
193         return true;
194      }
195   } else
196   {
197      // The sector is not in the last block so it must be loaded
198
199      return true;
200   }
201}
202
203//
204//
205//
206
207unsigned char *cdrom_driver::get_prefetch_sector(const unsigned int sec, unsigned int *sz)
208{
209   int numpfsec=num_pf*num_pf_sectors,
210         pfsec=sec-pf_head_sector;
211   if ((pfsec>=0) && (pfsec<numpfsec))
212   {
213      int   blk=(pf_head+(pfsec/num_pf_sectors))%num_pf_buffers,
214            off=((blk*num_pf_sectors)+(pfsec%num_pf_sectors))*native_sector_size;
215      *sz=native_sector_size;
216      return pf_buffer+off;
217   } else
218   {
219      return NULL;
220   }
221}
222
223//
224//
225//
226
227bool cdrom_driver::read_sector(const unsigned int sec, unsigned char *buf, const bool block)
228{
229   bool loaded=is_prefetch_sector_loaded(sec);
230
231//  printf("read_sector: %d (loaded=%c)\n", sec, loaded ? 'Y' : 'N');
232
233   if ((! loaded) && (block))
234   {
235      pf_status->block_until_complete();
236      loaded=true;
237   }
238
239   if (loaded)
240   {
241      unsigned int secsz=0;
242      unsigned char *ptr=get_prefetch_sector(sec,&secsz);
243      assert(ptr);
244
245//      printf("got sector %d @ %p, size %d = %02x %02x | %02x %02x\n", sec, ptr, secsz, ptr[0], ptr[1], ptr[0x20], ptr[0x21]);
246
247      if (secsz<2352)
248      {
249         // Add sector header
250
251         buf[0]=0;
252         memset(buf+1,0xff,10);
253         buf[11]=0;
254         sector_to_msf(sec,buf+12);
255         buf[12]=decimal_to_bcd(buf[12]);
256         buf[13]=decimal_to_bcd(buf[13]);
257         buf[14]=decimal_to_bcd(buf[14]);
258         buf[15]=2;
259
260         if (secsz<2336)
261         {
262            memset(buf+16,0xff,8);
263         }
264      }
265
266      switch (secsz)
267      {
268         case 2048:
269            memcpy(buf+24,ptr,2048);
270            break;
271
272         case 2336:
273            memcpy(buf+16,ptr,2336);
274            break;
275
276         case 2352:
277            memcpy(buf,ptr,raw_sector_size);
278            break;
279      }
280
281      return true;
282   } else
283   {
284      return false;
285   }
286}
287
288void cdrom_driver::set_machine(const running_machine &machine)
289{
290   m_machine = (running_machine *)&machine;
291
292   timestamp_frequency = m_machine->device<cpu_device>("maincpu")->clock();
293
294//  printf("cdrom_driver::set_machine: timestamp frequency = %d\n", timestamp_frequency);
295}
296
297/*
298    MAME/MESS driver implementation
299*/
300
301class mess_cdrom_driver : public cdrom_driver
302{
303   enum track_type
304   {
305      track_illegal=-1,
306      track_mode1_2048=0,
307      track_mode2_2352,
308      track_mode2_2336,
309      track_audio
310   };
311
312   struct toc_entry
313   {
314      unsigned int type;
315      unsigned char address[3];
316   };
317
318   int num_sectors, num_tracks;
319   toc_entry toc[100];
320   int bin_sector_size;
321   const char *err;
322
323   io_status *read_sectors(const unsigned int startsec, const unsigned int numsec, unsigned char *buf);
324
325public:
326   mess_cdrom_driver();
327   ~mess_cdrom_driver();
328
329   bool is_usable(char *error_msg, const int msglen) const
330   {
331      return true;
332   }
333
334   bool read_toc();
335   unsigned int get_first_track() const;
336   unsigned int get_num_tracks() const;
337   bool get_track_address(const unsigned int track,unsigned char *address) const;
338   tracktype get_track_type(const unsigned int track) const;
339   unsigned int find_track(const unsigned int sector, unsigned int *start_sector, unsigned int *end_sector) const;
340   cdromtype get_type() const;
341};
342
343mess_cdrom_driver::mess_cdrom_driver()
344   : err(NULL)
345{
346   for (int i=0; i<100; i++)
347      toc[i].type=track_illegal;
348   num_tracks=0;
349   num_sectors=0;
350}
351
352//
353//
354//
355
356mess_cdrom_driver::~mess_cdrom_driver()
357{
358   cancel_io();
359}
360
361//
362//
363//
364
365bool mess_cdrom_driver::read_toc()
366{
367   if (m_cd)
368   {
369      const cdrom_toc *toc = cdrom_get_toc(m_cd);
370
371      num_tracks = cdrom_get_last_track(m_cd);
372
373      switch (toc->tracks[0].trktype)
374      {
375         case CD_TRACK_MODE1: bin_sector_size=2048; break;
376         case CD_TRACK_MODE2_FORM1: bin_sector_size=2048; break;
377         case CD_TRACK_MODE2: bin_sector_size=2336; break;
378         case CD_TRACK_MODE2_FORM_MIX: bin_sector_size=2336; break;
379         default: bin_sector_size=2352; break;
380      }
381
382      set_native_sector_size(bin_sector_size);
383      num_sectors = cdrom_get_track_start(m_cd, num_tracks) + toc->tracks[num_tracks].frames;
384
385//      printf("mess_cdrom_driver: %d sectors, native size %d\n",num_sectors, bin_sector_size);
386
387      return true;
388   }
389
390   return false;
391}
392
393//
394//
395//
396
397io_status *mess_cdrom_driver::read_sectors(const unsigned int startsec, const unsigned int numsec, unsigned char *buf)
398{
399   const cdrom_toc *toc = cdrom_get_toc(m_cd);
400   UINT32 track = cdrom_get_track(m_cd, startsec);
401   UINT32 secsize = toc->tracks[track].datasize;
402
403   #ifdef debug_mess_driver
404   printf("mess: read %d sectors from %d (secsize %d)\n",numsec,startsec,secsize);
405   #endif
406
407   if ((startsec>=0)) // && ((int)startsec<num_sectors))
408   {
409//      fin->seek((INT64)startsec*(INT64)bin_sector_size);
410
411//      io_status *ios=fin->async_read(buf,numsec*bin_sector_size);
412//      if (! ios)
413      for (int i = 0; i < numsec; i++)
414      {
415//          printf("[%d/%d] Reading to pf_buffer %p at offset %d, size %d\n", i, numsec, &buf[secsize*i], secsize*i, secsize);
416         cdrom_read_data(m_cd, startsec+i, &buf[secsize*i], CD_TRACK_RAW_DONTCARE);
417      }
418
419      return NULL;
420
421//      return ios;
422   }
423   else
424   {
425      printf("mess: read sector out of range (%d, max=%d)\n",
426                startsec,
427                num_sectors);
428
429      memset(buf,0xff,numsec*bin_sector_size);
430      return NULL;
431   }
432}
433
434//
435//
436//
437
438unsigned int mess_cdrom_driver::get_first_track() const
439{
440   return 1;
441}
442
443unsigned int mess_cdrom_driver::get_num_tracks() const
444{
445//  printf("get_num_tracks = %d\n", num_tracks);
446   return num_tracks;
447}
448
449bool mess_cdrom_driver::get_track_address(const unsigned int track, unsigned char *address) const
450{
451   if ((track>=1) && ((int)track<=num_tracks))
452   {
453      UINT32 trkstart = cdrom_get_track_start(m_cd, track);
454
455      sector_to_msf(trkstart, address);
456
457      address[0] = address[0];
458      address[1] = address[1];
459      address[2] = address[2];
460
461//      printf("get_track_address %d = %02x:%02x:%02x\n", track, address[0], address[1], address[2]);
462
463      return true;
464   }
465   else
466   {
467      address[0]=address[1]=address[2]=0;
468      return false;
469   }
470}
471
472unsigned int mess_cdrom_driver::find_track(const unsigned int sector, unsigned int *start_sector, unsigned int *end_sector) const
473{
474   const cdrom_toc *toc = cdrom_get_toc(m_cd);
475   UINT32 track = cdrom_get_track(m_cd, sector);
476   int start;
477
478   start = cdrom_get_track_start(m_cd, track);
479
480   if (start_sector != NULL)
481   {
482      *start_sector = start;
483   }
484
485   if (end_sector != NULL)
486   {
487      *end_sector = start + toc->tracks[track].frames;
488   }
489
490   printf("find_track: track %d start %d end %d\n", track, start, *end_sector);
491
492   return -1;
493}
494
495//
496//
497//
498
499cdromtype mess_cdrom_driver::get_type() const
500{
501//  printf("get_type\n");
502   return cdromtype_cd;
503}
504
505//
506//
507//
508
509tracktype mess_cdrom_driver::get_track_type(const unsigned int track) const
510{
511   const cdrom_toc *toc = cdrom_get_toc(m_cd);
512
513//  printf("get_track_type %d = %d\n", track, toc->tracks[track].trktype);
514
515   switch (toc->tracks[track].trktype)
516   {
517      case CD_TRACK_MODE1: return tracktype_mode1;
518      case CD_TRACK_MODE2_RAW: return tracktype_mode2;
519      case CD_TRACK_MODE2_FORM_MIX: return tracktype_mode2;
520      case CD_TRACK_AUDIO: return tracktype_audio;
521      default: return tracktype_unknown;
522   }
523}
524
525//
526//
527//
528
529cdrom_driver *open_mess_drv()
530{
531   return new mess_cdrom_driver();
532}
533
src/mame/machine/psxcddrv.h
r11505r11506
1#ifndef _included_psxcddriver_
2#define _included_psxcddriver_
3
4const unsigned int raw_sector_size=2352;
5
6class io_status
7{
8protected:
9   virtual ~io_status() {}
10
11public:
12   io_status() { }
13
14   virtual void cancel()=0;
15   virtual void release()=0;
16   virtual bool complete() const=0;
17   virtual bool block_until_complete() const=0;
18   virtual INT64 bytes_transferred() const=0;
19};
20
21enum cdromtype
22{
23   cdromtype_cd=0,
24   cdromtype_dvd
25};
26
27enum tracktype
28{
29   tracktype_unknown=0,
30   tracktype_mode1,
31   tracktype_mode2,
32   tracktype_audio
33};
34
35class cdrom_driver
36{
37   int pf_head,
38         pf_tail,
39         num_pf,
40         pf_head_sector;
41   unsigned char *pf_buffer;
42   io_status *pf_status,
43                  *last_pf_status;
44   INT64 pf_timeout_begin;
45
46   int native_sector_size;
47
48   virtual io_status *read_sectors(const unsigned int sec, const unsigned int numsec, unsigned char *buf)=0;
49
50   bool is_prefetch_sector_loaded(const unsigned int pfsec);
51
52protected:
53   void cancel_io();
54   void set_native_sector_size(const unsigned int sz);
55   UINT32 timestamp_frequency;
56   cdrom_file   *m_cd;
57private:
58   running_machine *m_machine;
59
60public:
61   cdrom_driver();
62   virtual ~cdrom_driver();
63
64   void set_machine(const running_machine &machine);
65   void set_cdrom_file(cdrom_file *cdfile) { m_cd = cdfile; }
66
67   virtual bool is_usable(char *error_msg=NULL, const int msglen=0) const=0;
68   virtual bool read_toc()=0;
69
70   void prefetch_sector(const unsigned int sec);
71   bool read_sector(const unsigned int sec,
72                            unsigned char *buf,
73                            const bool block=true);
74   unsigned char *get_prefetch_sector(const unsigned int pfsec,
75                                                       unsigned int *sz);
76
77   virtual unsigned int get_first_track() const=0;
78   virtual unsigned int get_num_tracks() const=0;
79   virtual bool get_track_address(const unsigned int track,
80                                                 unsigned char *address) const=0;
81   virtual tracktype get_track_type(const unsigned int track) const=0;
82   virtual unsigned int find_track(const unsigned int sector,
83                                                   unsigned int *start_sector=NULL,
84                                                   unsigned int *end_sector=NULL) const=0;
85
86   virtual cdromtype get_type() const { return cdromtype_cd; }
87};
88
89//
90//
91//
92
93inline unsigned int msf_to_sector(const unsigned char *msf)
94{
95   unsigned int sec=msf[2]+(msf[1]*75)+(msf[0]*(60*75));
96   if (sec>=150)
97   {
98      return sec-150;
99   }
100   else
101   {
102      return 0;
103   }
104}
105
106inline void sector_to_msf(const unsigned int sec, unsigned char *msf)
107{
108   unsigned int s=sec+150;
109   msf[0]=s/(60*75);
110   s-=msf[0]*(60*75);
111   msf[1]=s/75;
112   s-=msf[1]*75;
113   msf[2]=s;
114}
115
116//
117//
118//
119
120inline unsigned char bcd_to_decimal(const unsigned char bcd)
121{
122   return ((bcd>>4)*10)+(bcd&0xf);
123}
124
125inline unsigned char decimal_to_bcd(const unsigned char dec)
126{
127   //assert(dec<100);
128   return ((dec/10)<<4)|(dec%10);
129}
130
131cdrom_driver *open_mess_drv();
132
133#endif
src/mame/machine/psx.c
r11505r11506
2525   }
2626}
2727
28#ifdef UNUSED_FUNCTION
29INLINE void psxwriteword( UINT32 *p_n_psxram, UINT32 n_address, UINT16 n_data )
30{
31   *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) ) = n_data;
32}
33#endif
34
35INLINE UINT16 psxreadword( UINT32 *p_n_psxram, UINT32 n_address )
36{
37   return *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) );
38}
39
4028WRITE32_HANDLER( psx_com_delay_w )
4129{
4230   psx_machine *p_psx = space->machine().driver_data<psx_state>()->m_p_psx;
r11505r11506
125113
126114/* DMA */
127115
128void psx_dma_install_read_handler( running_machine &machine, int n_channel, psx_dma_read_handler p_fn_dma_read )
129
116void psx_dma_install_read_handler( running_machine &machine, int n_channel, psx_dma_read_delegate p_fn_dma_read )
130117{
131   psxcpu_device::install_dma_read_handler( *machine.device("maincpu"), n_channel, p_fn_dma_read );
118   psxcpu_device::install_dma_read_handler( *machine.device("maincpu"), "maincpu", n_channel, p_fn_dma_read );
132119}
133120
134void psx_dma_install_write_handler( running_machine &machine, int n_channel, psx_dma_read_handler p_fn_dma_write )
121void psx_dma_install_write_handler( running_machine &machine, int n_channel, psx_dma_read_delegate p_fn_dma_write )
135122{
136   psxcpu_device::install_dma_write_handler( *machine.device("maincpu"), n_channel, p_fn_dma_write );
123   psxcpu_device::install_dma_write_handler( *machine.device("maincpu"), "maincpu", n_channel, p_fn_dma_write );
137124}
138125
139126/* Root Counters */
r11505r11506
618605   p_psx->sio[ n_port ].fn_handler = p_f_sio_handler;
619606}
620607
621/* MDEC */
622
623static const UINT32 m_p_n_mdec_zigzag[ DCTSIZE2 ] =
608static void gpu_read( psx_state *state, UINT32 n_address, INT32 n_size )
624609{
625    0,  1,  8, 16,  9,  2,  3, 10,
626   17, 24, 32, 25, 18, 11,  4,  5,
627   12, 19, 26, 33, 40, 48, 41, 34,
628   27, 20, 13,  6,  7, 14, 21, 28,
629   35, 42, 49, 56, 57, 50, 43, 36,
630   29, 22, 15, 23, 30, 37, 44, 51,
631   58, 59, 52, 45, 38, 31, 39, 46,
632   53, 60, 61, 54, 47, 55, 62, 63
633};
634
635static void mdec_cos_precalc( psx_machine *p_psx )
636{
637   psx_mdec *p_mdec = &p_psx->mdec;
638   UINT32 n_x;
639   UINT32 n_y;
640   UINT32 n_u;
641   UINT32 n_v;
642   INT32 *p_n_precalc;
643
644   p_n_precalc = p_mdec->p_n_cos_precalc;
645
646   for( n_y = 0; n_y < 8; n_y++ )
647   {
648      for( n_x = 0; n_x < 8; n_x++ )
649      {
650         for( n_v = 0; n_v < 8; n_v++ )
651         {
652            for( n_u = 0; n_u < 8; n_u++ )
653            {
654               *( p_n_precalc++ ) =
655                  ( ( p_mdec->p_n_cos[ ( n_u * 8 ) + n_x ] *
656                  p_mdec->p_n_cos[ ( n_v * 8 ) + n_y ] ) >> ( 30 - MDEC_COS_PRECALC_BITS ) );
657            }
658         }
659      }
660   }
661}
662
663static void mdec_idct( psx_machine *p_psx, INT32 *p_n_src, INT32 *p_n_dst )
664{
665   psx_mdec *p_mdec = &p_psx->mdec;
666   UINT32 n_yx;
667   UINT32 n_vu;
668   INT32 p_n_z[ 8 ];
669   INT32 *p_n_data;
670   INT32 *p_n_precalc;
671
672   p_n_precalc = p_mdec->p_n_cos_precalc;
673
674   for( n_yx = 0; n_yx < DCTSIZE2; n_yx++ )
675   {
676      p_n_data = p_n_src;
677
678      memset( p_n_z, 0, sizeof( p_n_z ) );
679
680      for( n_vu = 0; n_vu < DCTSIZE2 / 8; n_vu++ )
681      {
682         p_n_z[ 0 ] += p_n_data[ 0 ] * p_n_precalc[ 0 ];
683         p_n_z[ 1 ] += p_n_data[ 1 ] * p_n_precalc[ 1 ];
684         p_n_z[ 2 ] += p_n_data[ 2 ] * p_n_precalc[ 2 ];
685         p_n_z[ 3 ] += p_n_data[ 3 ] * p_n_precalc[ 3 ];
686         p_n_z[ 4 ] += p_n_data[ 4 ] * p_n_precalc[ 4 ];
687         p_n_z[ 5 ] += p_n_data[ 5 ] * p_n_precalc[ 5 ];
688         p_n_z[ 6 ] += p_n_data[ 6 ] * p_n_precalc[ 6 ];
689         p_n_z[ 7 ] += p_n_data[ 7 ] * p_n_precalc[ 7 ];
690         p_n_data += 8;
691         p_n_precalc += 8;
692      }
693
694      *( p_n_dst++ ) = ( p_n_z[ 0 ] + p_n_z[ 1 ] + p_n_z[ 2 ] + p_n_z[ 3 ] +
695         p_n_z[ 4 ] + p_n_z[ 5 ] + p_n_z[ 6 ] + p_n_z[ 7 ] ) >> ( MDEC_COS_PRECALC_BITS + 2 );
696   }
697}
698
699INLINE UINT16 mdec_unpack_run( UINT16 n_packed )
700{
701   return n_packed >> 10;
702}
703
704INLINE INT32 mdec_unpack_val( UINT16 n_packed )
705{
706   return ( ( (INT32)n_packed ) << 22 ) >> 22;
707}
708
709static UINT32 mdec_unpack( psx_machine *p_psx, UINT32 n_address )
710{
610   psx_machine *p_psx = state->m_p_psx;
711611   UINT32 *p_n_psxram = p_psx->p_n_psxram;
712   psx_mdec *p_mdec = &p_psx->mdec;
713   UINT8 n_z;
714   INT32 n_qscale;
715   UINT16 n_packed;
716   UINT32 n_block;
717   INT32 *p_n_block;
718   INT32 p_n_unpacked[ 64 ];
719   INT32 *p_n_q;
720612
721   p_n_q = p_mdec->p_n_quantize_uv;
722   p_n_block = p_mdec->p_n_unpacked;
723
724   for( n_block = 0; n_block < 6; n_block++ )
725   {
726      memset( p_n_unpacked, 0, sizeof( p_n_unpacked ) );
727
728      if( n_block == 2 )
729      {
730         p_n_q = p_mdec->p_n_quantize_y;
731      }
732      n_packed = psxreadword( p_n_psxram, n_address );
733      n_address += 2;
734      if( n_packed == 0xfe00 )
735      {
736         break;
737      }
738
739      n_qscale = mdec_unpack_run( n_packed );
740      p_n_unpacked[ 0 ] = mdec_unpack_val( n_packed ) * p_n_q[ 0 ];
741
742      n_z = 0;
743      for( ;; )
744      {
745         n_packed = psxreadword( p_n_psxram, n_address );
746         n_address += 2;
747
748         if( n_packed == 0xfe00 )
749         {
750            break;
751         }
752         n_z += mdec_unpack_run( n_packed ) + 1;
753         if( n_z > 63 )
754         {
755            break;
756         }
757         p_n_unpacked[ m_p_n_mdec_zigzag[ n_z ] ] = ( mdec_unpack_val( n_packed ) * p_n_q[ n_z ] * n_qscale ) / 8;
758      }
759      mdec_idct( p_psx, p_n_unpacked, p_n_block );
760      p_n_block += DCTSIZE2;
761   }
762   return n_address;
613   psx_gpu_read( state->machine(), &p_n_psxram[ n_address / 4 ], n_size );
763614}
764615
765INLINE INT32 mdec_cr_to_r( INT32 n_cr )
616static void gpu_write( psx_state *state, UINT32 n_address, INT32 n_size )
766617{
767   return ( 1435 * n_cr ) >> 10;
768}
769
770INLINE INT32 mdec_cr_to_g( INT32 n_cr )
771{
772   return ( -731 * n_cr ) >> 10;
773}
774
775INLINE INT32 mdec_cb_to_g( INT32 n_cb )
776{
777   return ( -351 * n_cb ) >> 10;
778}
779
780INLINE INT32 mdec_cb_to_b( INT32 n_cb )
781{
782   return ( 1814 * n_cb ) >> 10;
783}
784
785INLINE UINT16 mdec_clamp_r5( psx_mdec *p_mdec, INT32 n_r )
786{
787   return p_mdec->p_n_r5[ n_r + 128 + 256 ];
788}
789
790INLINE UINT16 mdec_clamp_g5( psx_mdec *p_mdec, INT32 n_g )
791{
792   return p_mdec->p_n_g5[ n_g + 128 + 256 ];
793}
794
795INLINE UINT16 mdec_clamp_b5( psx_mdec *p_mdec, INT32 n_b )
796{
797   return p_mdec->p_n_b5[ n_b + 128 + 256 ];
798}
799
800INLINE void mdec_makergb15( psx_mdec *p_mdec, UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT16 n_stp )
801{
802   p_mdec->p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = n_stp |
803      mdec_clamp_r5( p_mdec, p_n_y[ 0 ] + n_r ) |
804      mdec_clamp_g5( p_mdec, p_n_y[ 0 ] + n_g ) |
805      mdec_clamp_b5( p_mdec, p_n_y[ 0 ] + n_b );
806
807   p_mdec->p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = n_stp |
808      mdec_clamp_r5( p_mdec, p_n_y[ 1 ] + n_r ) |
809      mdec_clamp_g5( p_mdec, p_n_y[ 1 ] + n_g ) |
810      mdec_clamp_b5( p_mdec, p_n_y[ 1 ] + n_b );
811}
812
813static void mdec_yuv2_to_rgb15( psx_machine *p_psx )
814{
815   psx_mdec *p_mdec = &p_psx->mdec;
816   INT32 n_r;
817   INT32 n_g;
818   INT32 n_b;
819   INT32 n_cb;
820   INT32 n_cr;
821   INT32 *p_n_cb;
822   INT32 *p_n_cr;
823   INT32 *p_n_y;
824   UINT32 n_x;
825   UINT32 n_y;
826   UINT32 n_z;
827   UINT16 n_stp;
828   int n_address = 0;
829
830   if( ( p_mdec->n_0_command & ( 1L << 25 ) ) != 0 )
831   {
832      n_stp = 0x8000;
833   }
834   else
835   {
836      n_stp = 0x0000;
837   }
838
839   p_n_cr = &p_mdec->p_n_unpacked[ 0 ];
840   p_n_cb = &p_mdec->p_n_unpacked[ DCTSIZE2 ];
841   p_n_y = &p_mdec->p_n_unpacked[ DCTSIZE2 * 2 ];
842
843   for( n_z = 0; n_z < 2; n_z++ )
844   {
845      for( n_y = 0; n_y < 4; n_y++ )
846      {
847         for( n_x = 0; n_x < 4; n_x++ )
848         {
849            n_cr = *( p_n_cr );
850            n_cb = *( p_n_cb );
851            n_r = mdec_cr_to_r( n_cr );
852            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
853            n_b = mdec_cb_to_b( n_cb );
854
855            mdec_makergb15( p_mdec, ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
856            mdec_makergb15( p_mdec, ( n_address + 32 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
857
858            n_cr = *( p_n_cr + 4 );
859            n_cb = *( p_n_cb + 4 );
860            n_r = mdec_cr_to_r( n_cr );
861            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
862            n_b = mdec_cb_to_b( n_cb );
863
864            mdec_makergb15( p_mdec, ( n_address + 16 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
865            mdec_makergb15( p_mdec, ( n_address + 48 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
866
867            p_n_cr++;
868            p_n_cb++;
869            p_n_y += 2;
870            n_address += 4;
871         }
872         p_n_cr += 4;
873         p_n_cb += 4;
874         p_n_y += 8;
875         n_address += 48;
876      }
877      p_n_y += DCTSIZE2;
878   }
879   p_mdec->n_decoded = ( 16 * 16 ) / 2;
880}
881
882INLINE UINT16 mdec_clamp8( psx_mdec *p_mdec, INT32 n_r )
883{
884   return p_mdec->p_n_clamp8[ n_r + 128 + 256 ];
885}
886
887INLINE void mdec_makergb24( psx_mdec *p_mdec, UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT32 n_stp )
888{
889   p_mdec->p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = ( mdec_clamp8( p_mdec, p_n_y[ 0 ] + n_g ) << 8 ) | mdec_clamp8( p_mdec, p_n_y[ 0 ] + n_r );
890   p_mdec->p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = ( mdec_clamp8( p_mdec, p_n_y[ 1 ] + n_r ) << 8 ) | mdec_clamp8( p_mdec, p_n_y[ 0 ] + n_b );
891   p_mdec->p_n_output[ WORD_XOR_LE( n_address + 4 ) / 2 ] = ( mdec_clamp8( p_mdec, p_n_y[ 1 ] + n_b ) << 8 ) | mdec_clamp8( p_mdec, p_n_y[ 1 ] + n_g );
892}
893
894static void mdec_yuv2_to_rgb24( psx_machine *p_psx )
895{
896   psx_mdec *p_mdec = &p_psx->mdec;
897   INT32 n_r;
898   INT32 n_g;
899   INT32 n_b;
900   INT32 n_cb;
901   INT32 n_cr;
902   INT32 *p_n_cb;
903   INT32 *p_n_cr;
904   INT32 *p_n_y;
905   UINT32 n_x;
906   UINT32 n_y;
907   UINT32 n_z;
908   UINT32 n_stp;
909   int n_address = 0;
910
911   if( ( p_psx->mdec.n_0_command & ( 1L << 25 ) ) != 0 )
912   {
913      n_stp = 0x80008000;
914   }
915   else
916   {
917      n_stp = 0x00000000;
918   }
919
920   p_n_cr = &p_psx->mdec.p_n_unpacked[ 0 ];
921   p_n_cb = &p_psx->mdec.p_n_unpacked[ DCTSIZE2 ];
922   p_n_y = &p_psx->mdec.p_n_unpacked[ DCTSIZE2 * 2 ];
923
924   for( n_z = 0; n_z < 2; n_z++ )
925   {
926      for( n_y = 0; n_y < 4; n_y++ )
927      {
928         for( n_x = 0; n_x < 4; n_x++ )
929         {
930            n_cr = *( p_n_cr );
931            n_cb = *( p_n_cb );
932            n_r = mdec_cr_to_r( n_cr );
933            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
934            n_b = mdec_cb_to_b( n_cb );
935
936            mdec_makergb24( p_mdec, ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
937            mdec_makergb24( p_mdec, ( n_address + 48 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
938
939            n_cr = *( p_n_cr + 4 );
940            n_cb = *( p_n_cb + 4 );
941            n_r = mdec_cr_to_r( n_cr );
942            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
943            n_b = mdec_cb_to_b( n_cb );
944
945            mdec_makergb24( p_mdec, ( n_address + 24 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
946            mdec_makergb24( p_mdec, ( n_address + 72 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
947
948            p_n_cr++;
949            p_n_cb++;
950            p_n_y += 2;
951            n_address += 6;
952         }
953         p_n_cr += 4;
954         p_n_cb += 4;
955         p_n_y += 8;
956         n_address += 72;
957      }
958      p_n_y += DCTSIZE2;
959   }
960   p_psx->mdec.n_decoded = ( 24 * 16 ) / 2;
961}
962
963static void mdec0_write( running_machine &machine, UINT32 n_address, INT32 n_size )
964{
965   psx_machine *p_psx = machine.driver_data<psx_state>()->m_p_psx;
618   psx_machine *p_psx = state->m_p_psx;
966619   UINT32 *p_n_psxram = p_psx->p_n_psxram;
967   int n_index;
968620
969   verboselog( p_psx, 2, "mdec0_write( %08x, %08x )\n", n_address, n_size );
970   switch( p_psx->mdec.n_0_command >> 28 )
971   {
972   case 0x3:
973      verboselog( p_psx, 1, "mdec decode %08x %08x %08x\n", p_psx->mdec.n_0_command, n_address, n_size );
974      p_psx->mdec.n_0_address = n_address;
975      p_psx->mdec.n_0_size = n_size * 4;
976      p_psx->mdec.n_1_status |= ( 1L << 29 );
977      break;
978   case 0x4:
979      verboselog( p_psx, 1, "mdec quantize table %08x %08x %08x\n", p_psx->mdec.n_0_command, n_address, n_size );
980      n_index = 0;
981      while( n_size > 0 )
982      {
983         if( n_index < DCTSIZE2 )
984         {
985            p_psx->mdec.p_n_quantize_y[ n_index + 0 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
986            p_psx->mdec.p_n_quantize_y[ n_index + 1 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
987            p_psx->mdec.p_n_quantize_y[ n_index + 2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
988            p_psx->mdec.p_n_quantize_y[ n_index + 3 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
989         }
990         else if( n_index < DCTSIZE2 * 2 )
991         {
992            p_psx->mdec.p_n_quantize_uv[ n_index + 0 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
993            p_psx->mdec.p_n_quantize_uv[ n_index + 1 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
994            p_psx->mdec.p_n_quantize_uv[ n_index + 2 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
995            p_psx->mdec.p_n_quantize_uv[ n_index + 3 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
996         }
997         n_index += 4;
998         n_address += 4;
999         n_size--;
1000      }
1001      break;
1002   case 0x6:
1003      verboselog( p_psx, 1, "mdec cosine table %08x %08x %08x\n", p_psx->mdec.n_0_command, n_address, n_size );
1004      n_index = 0;
1005      while( n_size > 0 )
1006      {
1007         p_psx->mdec.p_n_cos[ n_index + 0 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xffff );
1008         p_psx->mdec.p_n_cos[ n_index + 1 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xffff );
1009         n_index += 2;
1010         n_address += 4;
1011         n_size--;
1012      }
1013      mdec_cos_precalc(p_psx);
1014      break;
1015   default:
1016      verboselog( p_psx, 0, "mdec unknown command %08x %08x %08x\n", p_psx->mdec.n_0_command, n_address, n_size );
1017      break;
1018   }
621   psx_gpu_write( state->machine(), &p_n_psxram[ n_address / 4 ], n_size );
1019622}
1020623
1021static void mdec1_read( running_machine &machine, UINT32 n_address, INT32 n_size )
1022{
1023   psx_machine *p_psx = machine.driver_data<psx_state>()->m_p_psx;
1024   UINT32 *p_n_psxram = p_psx->p_n_psxram;
1025   UINT32 n_this;
1026   UINT32 n_nextaddress;
1027
1028   verboselog( p_psx, 2, "mdec1_read( %08x, %08x )\n", n_address, n_size );
1029   if( ( p_psx->mdec.n_0_command & ( 1L << 29 ) ) != 0 && p_psx->mdec.n_0_size != 0 )
1030   {
1031      while( n_size > 0 )
1032      {
1033         if( p_psx->mdec.n_decoded == 0 )
1034         {
1035            if( (int)p_psx->mdec.n_0_size <= 0 )
1036            {
1037               mame_printf_debug( "ran out of data %08x\n", n_size );
1038               p_psx->mdec.n_0_size = 0;
1039               break;
1040            }
1041
1042            n_nextaddress = mdec_unpack( p_psx, p_psx->mdec.n_0_address );
1043            p_psx->mdec.n_0_size -= n_nextaddress - p_psx->mdec.n_0_address;
1044            p_psx->mdec.n_0_address = n_nextaddress;
1045
1046            if( ( p_psx->mdec.n_0_command & ( 1L << 27 ) ) != 0 )
1047            {
1048               mdec_yuv2_to_rgb15(p_psx);
1049            }
1050            else
1051            {
1052               mdec_yuv2_to_rgb24(p_psx);
1053            }
1054            p_psx->mdec.n_offset = 0;
1055         }
1056
1057         n_this = p_psx->mdec.n_decoded;
1058         if( n_this > n_size )
1059         {
1060            n_this = n_size;
1061         }
1062         p_psx->mdec.n_decoded -= n_this;
1063
1064         memcpy( (UINT8 *)p_n_psxram + n_address, (UINT8 *)p_psx->mdec.p_n_output + p_psx->mdec.n_offset, n_this * 4 );
1065         p_psx->mdec.n_offset += n_this * 4;
1066         n_address += n_this * 4;
1067         n_size -= n_this;
1068      }
1069
1070      if( (int)p_psx->mdec.n_0_size < 0 )
1071      {
1072         mame_printf_debug( "ran out of data %d\n", p_psx->mdec.n_0_size );
1073      }
1074   }
1075   else
1076   {
1077      mame_printf_debug( "mdec1_read no conversion :%08x:%08x:\n", p_psx->mdec.n_0_command, p_psx->mdec.n_0_size );
1078   }
1079   p_psx->mdec.n_1_status &= ~( 1L << 29 );
1080}
1081
1082WRITE32_HANDLER( psx_mdec_w )
1083{
1084   psx_machine *p_psx = space->machine().driver_data<psx_state>()->m_p_psx;
1085
1086   switch( offset )
1087   {
1088   case 0:
1089      verboselog( p_psx, 2, "mdec 0 command %08x\n", data );
1090      p_psx->mdec.n_0_command = data;
1091      break;
1092   case 1:
1093      verboselog( p_psx, 2, "mdec 1 command %08x\n", data );
1094      p_psx->mdec.n_1_command = data;
1095      break;
1096   }
1097}
1098
1099READ32_HANDLER( psx_mdec_r )
1100{
1101   psx_machine *p_psx = space->machine().driver_data<psx_state>()->m_p_psx;
1102
1103   switch( offset )
1104   {
1105   case 0:
1106      verboselog( p_psx, 2, "mdec 0 status %08x\n", 0 );
1107      return 0;
1108   case 1:
1109      verboselog( p_psx, 2, "mdec 1 status %08x\n", p_psx->mdec.n_1_status );
1110      return p_psx->mdec.n_1_status;
1111   }
1112   return 0;
1113}
1114
1115static void gpu_read( running_machine &machine, UINT32 n_address, INT32 n_size )
1116{
1117   psx_machine *p_psx = machine.driver_data<psx_state>()->m_p_psx;
1118   UINT32 *p_n_psxram = p_psx->p_n_psxram;
1119
1120   psx_gpu_read( machine, &p_n_psxram[ n_address / 4 ], n_size );
1121}
1122
1123static void gpu_write( running_machine &machine, UINT32 n_address, INT32 n_size )
1124{
1125   psx_machine *p_psx = machine.driver_data<psx_state>()->m_p_psx;
1126   UINT32 *p_n_psxram = p_psx->p_n_psxram;
1127
1128   psx_gpu_write( machine, &p_n_psxram[ n_address / 4 ], n_size );
1129}
1130
1131624void psx_machine_init( running_machine &machine )
1132625{
1133626   psx_machine *p_psx = machine.driver_data<psx_state>()->m_p_psx;
r11505r11506
1137630   p_psx->n_irqdata = 0;
1138631   p_psx->n_irqmask = 0;
1139632
1140   p_psx->mdec.n_0_command = 0;
1141   p_psx->mdec.n_0_address = 0;
1142   p_psx->mdec.n_0_size = 0;
1143   p_psx->mdec.n_1_command = 0;
1144   p_psx->mdec.n_1_status = 0;
1145
1146633   for( n = 0; n < 2; n++ )
1147634   {
1148635      p_psx->sio[ n ].n_status = SIO_STATUS_TX_EMPTY | SIO_STATUS_TX_RDY;
r11505r11506
1179666   {
1180667      sio_timer_adjust( p_psx, n );
1181668   }
1182
1183   mdec_cos_precalc(p_psx);
1184669}
1185670
1186671void psx_driver_init( running_machine &machine )
r11505r11506
1207692      p_psx->sio[ n ].fn_handler = NULL;
1208693   }
1209694
1210   for( n = 0; n < 256; n++ )
1211   {
1212      p_psx->mdec.p_n_clamp8[ n ] = 0;
1213      p_psx->mdec.p_n_clamp8[ n + 256 ] = n;
1214      p_psx->mdec.p_n_clamp8[ n + 512 ] = 255;
695   psx_dma_install_read_handler( machine, 2, psx_dma_read_delegate( FUNC( gpu_read ), state ) );
696   psx_dma_install_write_handler( machine, 2, psx_dma_write_delegate( FUNC( gpu_write ), state ) );
1215697
1216      p_psx->mdec.p_n_r5[ n ] = 0;
1217      p_psx->mdec.p_n_r5[ n + 256 ] = ( n >> 3 );
1218      p_psx->mdec.p_n_r5[ n + 512 ] = ( 255 >> 3 );
1219
1220      p_psx->mdec.p_n_g5[ n ] = 0;
1221      p_psx->mdec.p_n_g5[ n + 256 ] = ( n >> 3 ) << 5;
1222      p_psx->mdec.p_n_g5[ n + 512 ] = ( 255 >> 3 ) << 5;
1223
1224      p_psx->mdec.p_n_b5[ n ] = 0;
1225      p_psx->mdec.p_n_b5[ n + 256 ] = ( n >> 3 ) << 10;
1226      p_psx->mdec.p_n_b5[ n + 512 ] = ( 255 >> 3 ) << 10;
1227   }
1228
1229   psx_dma_install_read_handler( machine, 1, mdec1_read );
1230   psx_dma_install_read_handler( machine, 2, gpu_read );
1231
1232   psx_dma_install_write_handler( machine, 0, mdec0_write );
1233   psx_dma_install_write_handler( machine, 2, gpu_write );
1234
1235698   state_save_register_global( machine, p_psx->n_irqdata );
1236699   state_save_register_global( machine, p_psx->n_irqmask );
1237700   for ( n = 0; n < 3; n++ )
r11505r11506
1259722      state_save_register_item( machine, "psxsio", NULL, n, p_psx->sio[n].n_tx_bits );
1260723   }
1261724
1262   state_save_register_global( machine, p_psx->mdec.n_0_command );
1263   state_save_register_global( machine, p_psx->mdec.n_0_address );
1264   state_save_register_global( machine, p_psx->mdec.n_0_size );
1265   state_save_register_global( machine, p_psx->mdec.n_1_command );
1266   state_save_register_global( machine, p_psx->mdec.n_1_status );
1267   state_save_register_global_array( machine, p_psx->mdec.p_n_quantize_y );
1268   state_save_register_global_array( machine, p_psx->mdec.p_n_quantize_uv );
1269   state_save_register_global_array( machine, p_psx->mdec.p_n_cos );
1270
1271725   machine.save().register_postload( save_prepost_delegate(FUNC(psx_postload), p_psx ));
1272726}
src/mame/includes/psx.h
r11505r11506
3939
4040typedef void ( *psx_sio_handler )( running_machine &, int );
4141
42#define   DCTSIZE ( 8 )
43#define   DCTSIZE2 ( DCTSIZE * DCTSIZE )
44
4542#define SIO_BUF_SIZE ( 8 )
4643
4744#define SIO_STATUS_TX_RDY ( 1 << 0 )
r11505r11506
5956#define SIO_CONTROL_DSR_IENA ( 1 << 12 )
6057#define SIO_CONTROL_DTR ( 1 << 13 )
6158
62#define MDEC_COS_PRECALC_BITS ( 21 )
63
6459typedef struct _psx_root psx_root;
6560struct _psx_root
6661{
r11505r11506
9388   psx_sio_handler fn_handler;
9489};
9590
96typedef struct _psx_mdec psx_mdec;
97struct _psx_mdec
98{
99   UINT32 n_decoded;
100   UINT32 n_offset;
101   UINT16 p_n_output[ 24 * 16 ];
102
103   INT32 p_n_quantize_y[ DCTSIZE2 ];
104   INT32 p_n_quantize_uv[ DCTSIZE2 ];
105   INT32 p_n_cos[ DCTSIZE2 ];
106   INT32 p_n_cos_precalc[ DCTSIZE2 * DCTSIZE2 ];
107
108   UINT32 n_0_command;
109   UINT32 n_0_address;
110   UINT32 n_0_size;
111   UINT32 n_1_command;
112   UINT32 n_1_status;
113
114   UINT16 p_n_clamp8[ 256 * 3 ];
115   UINT16 p_n_r5[ 256 * 3 ];
116   UINT16 p_n_g5[ 256 * 3 ];
117   UINT16 p_n_b5[ 256 * 3 ];
118
119   INT32 p_n_unpacked[ DCTSIZE2 * 6 * 2 ];
120};
121
12291typedef struct _psx_machine psx_machine;
12392struct _psx_machine
12493{
r11505r11506
135104   psx_root root[3];
136105
137106   psx_sio sio[2];
138
139   psx_mdec mdec;
140107};
141108
142109typedef struct _psx_gpu psx_gpu;
r11505r11506
176143WRITE32_HANDLER( psx_irq_w );
177144READ32_HANDLER( psx_irq_r );
178145extern void psx_irq_set( running_machine &, UINT32 );
179extern void psx_dma_install_read_handler( running_machine &, int, psx_dma_read_handler );
180extern void psx_dma_install_write_handler( running_machine &, int, psx_dma_read_handler );
181WRITE32_HANDLER( psx_dma_w );
182READ32_HANDLER( psx_dma_r );
146extern void psx_dma_install_read_handler( running_machine &, int, psx_dma_read_delegate );
147extern void psx_dma_install_write_handler( running_machine &, int, psx_dma_read_delegate );
183148WRITE32_HANDLER( psx_counter_w );
184149READ32_HANDLER( psx_counter_r );
185150WRITE32_HANDLER( psx_sio_w );
r11505r11506
187152extern void psx_sio_install_handler( running_machine &, int, psx_sio_handler );
188153extern void psx_sio_input( running_machine &, int, int, int );
189154
190WRITE32_HANDLER( psx_mdec_w );
191READ32_HANDLER( psx_mdec_r );
192155extern void psx_machine_init( running_machine &machine );
193156extern void psx_driver_init( running_machine &machine );
194157
src/mame/mame.mak
r11505r11506
13201320
13211321$(MAMEOBJ)/sony.a: \
13221322   $(DRIVERS)/zn.o $(MACHINE)/znsec.o \
1323   $(DRIVERS)/taitogn.o \
1324   $(DRIVERS)/psx.o \
1325   $(MACHINE)/psxcd.o \
1326   $(MACHINE)/psxcddrv.o   \
1327   $(MACHINE)/psxcard.o   \
13281323
13291324$(MAMEOBJ)/stern.a: \
13301325   $(DRIVERS)/astinvad.o \
r11505r11506
14231418   $(DRIVERS)/taito_z.o $(VIDEO)/taito_z.o \
14241419   $(DRIVERS)/taito_o.o $(VIDEO)/taito_o.o \
14251420   $(DRIVERS)/taitoair.o $(VIDEO)/taitoair.o \
1421   $(DRIVERS)/taitogn.o \
14261422   $(DRIVERS)/taitojc.o $(VIDEO)/taitojc.o \
14271423   $(DRIVERS)/taitopjc.o $\
14281424   $(DRIVERS)/taitosj.o $(MACHINE)/taitosj.o $(VIDEO)/taitosj.o \
src/mame/drivers/psx.c
r11505r11506
1/***************************************************************************
2
3  Sony Playstaion
4  ===============
5  Preliminary driver by smf
6  Additional development by pSXAuthor and R. Belmont
7
8***************************************************************************/
9
10#include "emu.h"
11#include "cpu/psx/psx.h"
12#include "imagedev/snapquik.h"
13#include "imagedev/chd_cd.h"
14#include "includes/psx.h"
15#include "sound/spu.h"
16#include "debugger.h"
17#include "zlib.h"
18#include "machine/psxcd.h"
19#include "machine/psxcard.h"
20
21typedef struct
22{
23   UINT8 n_shiftin;
24   UINT8 n_shiftout;
25   int n_bits;
26   int n_state;
27   int n_byte;
28   int b_lastclock;
29   int b_ack;
30} pad_t;
31
32
33class psx1_state : public psx_state
34{
35public:
36   psx1_state(const machine_config &mconfig, device_type type, const char *tag)
37      : psx_state(mconfig, type, tag) { }
38
39   UINT8 *m_exe_buffer;
40   int m_exe_size;
41   pad_t m_pad[ 2 ];
42   int m_cd_param_p;
43   int m_cd_result_p;
44   int m_cd_result_c;
45   int m_cd_result_ready;
46   int m_cd_reset;
47   UINT8 m_cd_stat;
48   UINT8 m_cd_io_status;
49   UINT8 m_cd_param[8];
50   UINT8 m_cd_result[8];
51};
52
53
54#define VERBOSE_LEVEL ( 0 )
55
56INLINE void ATTR_PRINTF(3,4) verboselog( running_machine &machine, int n_level, const char *s_fmt, ... )
57{
58   if( VERBOSE_LEVEL >= n_level )
59   {
60      va_list v;
61      char buf[ 32768 ];
62      va_start( v, s_fmt );
63      vsprintf( buf, s_fmt, v );
64      va_end( v );
65      logerror( "%s: %s", machine.describe_context(), buf );
66   }
67}
68
69
70static void psxexe_conv32( UINT32 *p_uint32 )
71{
72   UINT8 *p_uint8;
73
74   p_uint8 = (UINT8 *)p_uint32;
75
76   *( p_uint32 ) = p_uint8[ 0 ] |
77      ( p_uint8[ 1 ] << 8 ) |
78      ( p_uint8[ 2 ] << 16 ) |
79      ( p_uint8[ 3 ] << 24 );
80}
81
82static int load_psxexe( device_t *cpu, unsigned char *p_n_file, int n_len )
83{
84   psx_state *state = cpu->machine().driver_data<psx_state>();
85   struct PSXEXE_HEADER
86   {
87      UINT8 id[ 8 ];
88      UINT32 text;   /* SCE only */
89      UINT32 data;   /* SCE only */
90      UINT32 pc0;
91      UINT32 gp0;      /* SCE only */
92      UINT32 t_addr;
93      UINT32 t_size;
94      UINT32 d_addr;   /* SCE only */
95      UINT32 d_size;   /* SCE only */
96      UINT32 b_addr;   /* SCE only */
97      UINT32 b_size;   /* SCE only */
98      UINT32 s_addr;
99      UINT32 s_size;
100      UINT32 SavedSP;
101      UINT32 SavedFP;
102      UINT32 SavedGP;
103      UINT32 SavedRA;
104      UINT32 SavedS0;
105      UINT8 dummy[ 0x800 - 76 ];
106   };
107
108   struct PSXEXE_HEADER *psxexe_header = (struct PSXEXE_HEADER *)p_n_file;
109
110   if( n_len >= sizeof( struct PSXEXE_HEADER ) &&
111      memcmp( psxexe_header->id, "PS-X EXE", 8 ) == 0 )
112   {
113      UINT8 *p_ram;
114      UINT8 *p_psxexe;
115      UINT32 n_stack;
116      UINT32 n_ram;
117      UINT32 n_address;
118      UINT32 n_size;
119
120      psxexe_conv32( &psxexe_header->text );
121      psxexe_conv32( &psxexe_header->data );
122      psxexe_conv32( &psxexe_header->pc0 );
123      psxexe_conv32( &psxexe_header->gp0 );
124      psxexe_conv32( &psxexe_header->t_addr );
125      psxexe_conv32( &psxexe_header->t_size );
126      psxexe_conv32( &psxexe_header->d_addr );
127      psxexe_conv32( &psxexe_header->d_size );
128      psxexe_conv32( &psxexe_header->b_addr );
129      psxexe_conv32( &psxexe_header->b_size );
130      psxexe_conv32( &psxexe_header->s_addr );
131      psxexe_conv32( &psxexe_header->s_size );
132      psxexe_conv32( &psxexe_header->SavedSP );
133      psxexe_conv32( &psxexe_header->SavedFP );
134      psxexe_conv32( &psxexe_header->SavedGP );
135      psxexe_conv32( &psxexe_header->SavedRA );
136      psxexe_conv32( &psxexe_header->SavedS0 );
137
138      /* todo: check size.. */
139
140      logerror( "psx_exe_load: pc    %08x\n", psxexe_header->pc0 );
141      logerror( "psx_exe_load: org   %08x\n", psxexe_header->t_addr );
142      logerror( "psx_exe_load: len   %08x\n", psxexe_header->t_size );
143      logerror( "psx_exe_load: sp    %08x\n", psxexe_header->s_addr );
144      logerror( "psx_exe_load: len   %08x\n", psxexe_header->s_size );
145
146      p_ram = (UINT8 *)state->m_p_n_psxram;
147      n_ram = state->m_n_psxramsize;
148
149      p_psxexe = p_n_file + sizeof( struct PSXEXE_HEADER );
150
151      n_address = psxexe_header->t_addr;
152      n_size = psxexe_header->t_size;
153      while( n_size != 0 )
154      {
155         p_ram[ BYTE4_XOR_LE( n_address ) % n_ram ] = *( p_psxexe );
156         n_address++;
157         p_psxexe++;
158         n_size--;
159      }
160
161      cpu_set_reg( cpu, PSXCPU_PC, psxexe_header->pc0 );
162      cpu_set_reg( cpu, PSXCPU_R28, psxexe_header->gp0 );
163      n_stack = psxexe_header->s_addr + psxexe_header->s_size;
164      if( n_stack != 0 )
165      {
166         cpu_set_reg( cpu, PSXCPU_R29, n_stack );
167         cpu_set_reg( cpu, PSXCPU_R30, n_stack );
168      }
169
170      return 1;
171   }
172   return 0;
173}
174
175static void cpe_set_register( device_t *cpu, int n_reg, int n_value )
176{
177   if( n_reg < 0x80 && ( n_reg % 4 ) == 0 )
178   {
179      logerror( "psx_exe_load: r%-2d   %08x\n", n_reg / 4, n_value );
180      cpu_set_reg( cpu, PSXCPU_R0 + ( n_reg / 4 ), n_value );
181   }
182   else if( n_reg == 0x80 )
183   {
184      logerror( "psx_exe_load: lo    %08x\n", n_value );
185      cpu_set_reg( cpu, PSXCPU_LO, n_value );
186   }
187   else if( n_reg == 0x84 )
188   {
189      logerror( "psx_exe_load: hi    %08x\n", n_value );
190      cpu_set_reg( cpu, PSXCPU_HI, n_value );
191   }
192   else if( n_reg == 0x88 )
193   {
194      logerror( "psx_exe_load: sr    %08x\n", n_value );
195      cpu_set_reg( cpu, PSXCPU_CP0R12, n_value );
196   }
197   else if( n_reg == 0x8c )
198   {
199      logerror( "psx_exe_load: cause %08x\n", n_value );
200      cpu_set_reg( cpu, PSXCPU_CP0R13, n_value );
201   }
202   else if( n_reg == 0x90 )
203   {
204      logerror( "psx_exe_load: pc    %08x\n", n_value );
205      cpu_set_reg( cpu, PSXCPU_PC, n_value );
206   }
207   else if( n_reg == 0x94 )
208   {
209      logerror( "psx_exe_load: prid  %08x\n", n_value );
210      cpu_set_reg( cpu, PSXCPU_CP0R15, n_value );
211   }
212   else
213   {
214      logerror( "psx_exe_load: invalid register %04x/%08x\n", n_reg, n_value );
215   }
216}
217
218static int load_cpe( device_t *cpu, unsigned char *p_n_file, int n_len )
219{
220   psx_state *state = cpu->machine().driver_data<psx_state>();
221   if( n_len >= 4 &&
222      memcmp( p_n_file, "CPE\001", 4 ) == 0 )
223   {
224      int n_offset = 4;
225
226      for( ;; )
227      {
228         if( n_offset >= n_len || p_n_file[ n_offset ] > 8 )
229         {
230            break;
231         }
232
233         switch( p_n_file[ n_offset++ ] )
234         {
235         case 0:
236            /* end of file */
237            return 1;
238         case 1:
239            /* read bytes */
240            {
241               int n_address = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
242                  ( (int)p_n_file[ n_offset + 1 ] << 8 ) |
243                  ( (int)p_n_file[ n_offset + 2 ] << 16 ) |
244                  ( (int)p_n_file[ n_offset + 3 ] << 24 );
245               int n_size = ( (int)p_n_file[ n_offset + 4 ] << 0 ) |
246                  ( (int)p_n_file[ n_offset + 5 ] << 8 ) |
247                  ( (int)p_n_file[ n_offset + 6 ] << 16 ) |
248                  ( (int)p_n_file[ n_offset + 7 ] << 24 );
249
250               UINT8 *p_ram = (UINT8 *)state->m_p_n_psxram;
251               UINT32 n_ram = state->m_n_psxramsize;
252
253               n_offset += 8;
254
255               logerror( "psx_exe_load: org   %08x\n", n_address );
256               logerror( "psx_exe_load: len   %08x\n", n_size );
257
258               while( n_size > 0 )
259               {
260                  p_ram[ BYTE4_XOR_LE( n_address ) % n_ram ] = p_n_file[ n_offset++ ];
261                  n_address++;
262                  n_size--;
263               }
264               break;
265            }
266         case 2:
267            /* run address: not tested */
268            {
269               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
270                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
271                  ( (int)p_n_file[ n_offset + 4 ] << 16 ) |
272                  ( (int)p_n_file[ n_offset + 5 ] << 24 );
273
274               n_offset += 4;
275
276               cpe_set_register( cpu, 0x90, n_value );
277               break;
278            }
279         case 3:
280            /* set reg to longword */
281            {
282               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
283                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
284               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
285                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
286                  ( (int)p_n_file[ n_offset + 4 ] << 16 ) |
287                  ( (int)p_n_file[ n_offset + 5 ] << 24 );
288
289               n_offset += 6;
290
291               cpe_set_register( cpu, n_reg, n_value );
292               break;
293            }
294         case 4:
295            /* set reg to word: not tested */
296            {
297               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
298                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
299               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
300                  ( (int)p_n_file[ n_offset + 3 ] << 8 );
301
302               n_offset += 4;
303
304               cpe_set_register( cpu, n_reg, n_value );
305               break;
306            }
307         case 5:
308            /* set reg to byte: not tested */
309            {
310               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
311                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
312               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 );
313
314               n_offset += 3;
315
316               cpe_set_register( cpu, n_reg, n_value );
317               break;
318            }
319         case 6:
320            /* set reg to 3-byte: not tested */
321            {
322               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
323                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
324               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
325                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
326                  ( (int)p_n_file[ n_offset + 4 ] << 16 );
327
328               n_offset += 5;
329
330               cpe_set_register( cpu, n_reg, n_value );
331               break;
332            }
333         case 7:
334            /* workspace: not tested */
335            n_offset += 4;
336            break;
337         case 8:
338            /* unit */
339            {
340               int n_unit = p_n_file[ n_offset + 0 ];
341
342               n_offset++;
343
344               logerror( "psx_exe_load: unit  %08x\n", n_unit );
345            }
346            break;
347         }
348      }
349   }
350   return 0;
351}
352
353static int load_psf( device_t *cpu, unsigned char *p_n_file, int n_len )
354{
355   int n_return;
356   unsigned long n_crc;
357   unsigned long n_compressed;
358   unsigned char *p_n_compressed;
359   unsigned long n_uncompressed;
360   unsigned char *p_n_uncompressed;
361
362   struct PSF_HEADER
363   {
364      unsigned char id[ 4 ];
365      UINT32 reserved_size;
366      UINT32 exe_size;
367      UINT32 exe_crc;
368   };
369
370   struct PSF_HEADER *psf_header = (struct PSF_HEADER *)p_n_file;
371
372   n_return = 0;
373
374   if( n_len >= sizeof( struct PSF_HEADER ) &&
375      memcmp( p_n_file, "PSF", 3 ) == 0 )
376   {
377      psxexe_conv32( &psf_header->reserved_size );
378      psxexe_conv32( &psf_header->exe_size );
379      psxexe_conv32( &psf_header->exe_crc );
380
381      logerror( "psx_exe_load: reserved_size %08x\n", psf_header->reserved_size );
382      logerror( "psx_exe_load: exe_size      %08x\n", psf_header->exe_size );
383      logerror( "psx_exe_load: exe_crc       %08x\n", psf_header->exe_crc );
384
385      n_compressed = psf_header->exe_size;
386      p_n_compressed = p_n_file + sizeof( struct PSF_HEADER ) + psf_header->reserved_size;
387
388      n_crc = crc32( crc32( 0L, Z_NULL, 0 ), p_n_compressed, n_compressed );
389      if( n_crc != psf_header->exe_crc )
390      {
391         logerror( "psx_exe_load: psf invalid crc\n" );
392         return 0;
393      }
394
395      n_uncompressed = 0x200000;
396      p_n_uncompressed = (unsigned char *)malloc( n_uncompressed );
397
398      if( uncompress( p_n_uncompressed, &n_uncompressed, p_n_compressed, n_compressed ) != Z_OK )
399      {
400         logerror( "psx_exe_load: psf uncompress failed\n" );
401      }
402      else if( !load_psxexe( cpu, p_n_uncompressed, n_uncompressed ) )
403      {
404         logerror( "psx_exe_load: psf load failed\n" );
405      }
406      else
407      {
408         n_return = 1;
409      }
410
411      free( p_n_uncompressed );
412   }
413   return n_return;
414}
415
416DIRECT_UPDATE_HANDLER( psx_default )
417{
418   return address;
419}
420
421DIRECT_UPDATE_HANDLER( psx_setopbase )
422{
423   psx1_state *state = machine.driver_data<psx1_state>();
424   if( address == 0x80030000 )
425   {
426      device_t *cpu = machine.device("maincpu");
427
428      machine.device("maincpu")->memory().space(AS_PROGRAM)->set_direct_update_handler(direct_update_delegate(FUNC(psx_default), &machine));
429
430      if( load_psxexe( cpu, state->m_exe_buffer, state->m_exe_size ) ||
431         load_cpe( cpu, state->m_exe_buffer, state->m_exe_size ) ||
432         load_psf( cpu, state->m_exe_buffer, state->m_exe_size ) )
433      {
434/*          DEBUGGER_BREAK; */
435
436         address = cpu_get_reg( cpu, PSXCPU_PC );
437      }
438      else
439      {
440         logerror( "psx_exe_load: invalid exe\n" );
441      }
442
443      state->m_exe_size = 0;
444      free( state->m_exe_buffer );
445   }
446   return address;
447}
448
449static QUICKLOAD_LOAD( psx_exe_load )
450{
451   psx1_state *state = image.device().machine().driver_data<psx1_state>();
452   address_space *space = image.device().machine().device( "maincpu")->memory().space( AS_PROGRAM );
453
454   state->m_exe_size = 0;
455   state->m_exe_buffer = (UINT8*)malloc( quickload_size );
456   if( state->m_exe_buffer == NULL )
457   {
458      logerror( "psx_exe_load: out of memory\n" );
459      return IMAGE_INIT_FAIL;
460   }
461   if( image.fread( state->m_exe_buffer, quickload_size ) != quickload_size )
462   {
463      free( state->m_exe_buffer );
464      return IMAGE_INIT_FAIL;
465   }
466   state->m_exe_size = quickload_size;
467   space->set_direct_update_handler(direct_update_delegate(FUNC(psx_setopbase), &image.device().machine()));
468
469   return IMAGE_INIT_PASS;
470}
471
472/* PAD emulation */
473
474#define PAD_STATE_IDLE ( 0 )
475#define PAD_STATE_LISTEN ( 1 )
476#define PAD_STATE_ACTIVE ( 2 )
477#define PAD_STATE_READ ( 3 )
478#define PAD_STATE_UNLISTEN ( 4 )
479#define PAD_STATE_MEMCARD ( 5 )
480
481#define PAD_TYPE_STANDARD ( 4 )
482#define PAD_BYTES_STANDARD ( 2 )
483
484#define PAD_CMD_START ( 0x01 )
485#define PAD_CMD_READ  ( 0x42 ) /* B */
486
487#define PAD_DATA_OK   ( 0x5a ) /* Z */
488#define PAD_DATA_IDLE ( 0xff )
489
490static TIMER_CALLBACK(psx_pad_ack)
491{
492   psx1_state *state = machine.driver_data<psx1_state>();
493   int n_port = param;
494   pad_t *pad = &state->m_pad[ n_port ];
495
496   if( pad->n_state != PAD_STATE_IDLE )
497   {
498      psx_sio_input( machine, 0, PSX_SIO_IN_DSR, pad->b_ack * PSX_SIO_IN_DSR );
499      if( !pad->b_ack )
500      {
501         pad->b_ack = 1;
502         machine.scheduler().timer_set(attotime::from_usec( 2 ), FUNC(psx_pad_ack) , n_port);
503      }
504   }
505}
506
507static void psx_pad( running_machine &machine, int n_port, int n_data )
508{
509   psx1_state *state = machine.driver_data<psx1_state>();
510   pad_t *pad = &state->m_pad[ n_port ];
511   int b_sel;
512   int b_clock;
513   int b_data;
514   int b_ack;
515   int b_ready;
516   static const char *const portnames[] = { "IN0", "IN1", "IN2", "IN3" };
517   psxcard_device *psxcard = NULL;
518
519   if (n_port == 0)
520   {
521      psxcard = machine.device<psxcard_device>("card1");
522   }
523   else
524   {
525      psxcard = machine.device<psxcard_device>("card2");
526   }
527
528   b_sel = ( n_data & PSX_SIO_OUT_DTR ) / PSX_SIO_OUT_DTR;
529   b_clock = ( n_data & PSX_SIO_OUT_CLOCK ) / PSX_SIO_OUT_CLOCK;
530   b_data = ( n_data & PSX_SIO_OUT_DATA ) / PSX_SIO_OUT_DATA;
531   b_ready = 0;
532   b_ack = 0;
533
534   if( b_sel )
535   {
536      pad->n_state = PAD_STATE_IDLE;
537   }
538
539   switch( pad->n_state )
540   {
541   case PAD_STATE_LISTEN:
542   case PAD_STATE_ACTIVE:
543   case PAD_STATE_READ:
544   case PAD_STATE_MEMCARD:
545      if( pad->b_lastclock && !b_clock )
546      {
547         psx_sio_input( machine, 0, PSX_SIO_IN_DATA, ( pad->n_shiftout & 1 ) * PSX_SIO_IN_DATA );
548         pad->n_shiftout >>= 1;
549      }
550      if( !pad->b_lastclock && b_clock )
551      {
552         pad->n_shiftin >>= 1;
553         pad->n_shiftin |= b_data << 7;
554         pad->n_bits++;
555
556         if( pad->n_bits == 8 )
557         {
558            pad->n_bits = 0;
559            b_ready = 1;
560         }
561      }
562      break;
563   }
564
565   pad->b_lastclock = b_clock;
566
567   switch( pad->n_state )
568   {
569   case PAD_STATE_IDLE:
570      if( !b_sel )
571      {
572         pad->n_state = PAD_STATE_LISTEN;
573         pad->n_shiftout = PAD_DATA_IDLE;
574         pad->n_bits = 0;
575      }
576      break;
577   case PAD_STATE_LISTEN:
578      if( b_ready )
579      {
580         if( pad->n_shiftin == PAD_CMD_START )
581         {
582            pad->n_state = PAD_STATE_ACTIVE;
583            pad->n_shiftout = ( PAD_TYPE_STANDARD << 4 ) | ( PAD_BYTES_STANDARD >> 1 );
584            b_ack = 1;
585         }
586         else if( psxcard->transfer(pad->n_shiftin, &pad->n_shiftout) )
587         {
588            pad->n_state = PAD_STATE_MEMCARD;
589            b_ack = 1;
590         }
591         else
592         {
593            pad->n_state = PAD_STATE_UNLISTEN;
594         }
595      }
596      break;
597   case PAD_STATE_MEMCARD:
598      if( b_ready )
599      {
600         if( psxcard->transfer(pad->n_shiftin, &pad->n_shiftout) )
601         {
602            b_ack = 1;
603         }
604         else
605         {
606            b_ack = 0;
607            pad->n_state = PAD_STATE_IDLE;
608         }
609      }
610      break;
611   case PAD_STATE_ACTIVE:
612      if( b_ready )
613      {
614         if( pad->n_shiftin == PAD_CMD_READ )
615         {
616            pad->n_state = PAD_STATE_READ;
617            pad->n_shiftout = PAD_DATA_OK;
618            pad->n_byte = 0;
619            b_ack = 1;
620         }
621         else
622         {
623            pad->n_state = PAD_STATE_UNLISTEN;
624         }
625      }
626      break;
627   case PAD_STATE_READ:
628      if( b_ready )
629      {
630         if( pad->n_byte < PAD_BYTES_STANDARD )
631         {
632            pad->n_shiftout = input_port_read(machine, portnames[pad->n_byte + ( n_port * PAD_BYTES_STANDARD )]);
633            pad->n_byte++;
634            b_ack = 1;
635         }
636         else
637         {
638            pad->n_state = PAD_STATE_LISTEN;
639         }
640      }
641      break;
642   }
643
644   if( b_ack )
645   {
646      pad->b_ack = 0;
647      machine.scheduler().timer_set(attotime::from_usec( 10 ), FUNC(psx_pad_ack), n_port);
648   }
649}
650
651static void psx_sio0( running_machine &machine, int n_data )
652{
653   /* todo: raise data & ack when nothing is driving it low */
654   psx_pad( machine, 0, n_data );
655   psx_pad( machine, 1, n_data ^ PSX_SIO_OUT_DTR );
656}
657
658/* ----------------------------------------------------------------------- */
659
660static void cd_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
661{
662   psxcd_device *psxcd = machine.device<psxcd_device>("psxcd");
663   UINT8 *psxram = (UINT8 *)memory_get_shared(machine, "share1");
664
665   psxcd->start_dma(psxram + n_address, n_size*4);
666}
667
668static void cd_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
669{
670   printf("cd_dma_write?!: addr %x, size %x\n", n_address, n_size);
671}
672
673static READ8_HANDLER( psx_cd_r )
674{
675   psxcd_device *psxcd = space->machine().device<psxcd_device>(PSXCD_TAG);
676
677   return psxcd->read_byte(offset);
678}
679
680static WRITE8_HANDLER( psx_cd_w )
681{
682   psxcd_device *psxcd = space->machine().device<psxcd_device>(PSXCD_TAG);
683
684   psxcd->write_byte(offset, data);
685}
686
687static ADDRESS_MAP_START( psx_map, AS_PROGRAM, 32 )
688   AM_RANGE(0x00000000, 0x001fffff) AM_RAM   AM_SHARE("share1") /* ram */
689   AM_RANGE(0x1f801800, 0x1f801803) AM_READWRITE8(psx_cd_r, psx_cd_w, 0xffffffff)
690   AM_RANGE(0x1fc00000, 0x1fc7ffff) AM_ROM AM_SHARE("share2") AM_REGION("user1", 0) /* bios */
691   AM_RANGE(0x80000000, 0x801fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
692   AM_RANGE(0x80600000, 0x807fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
693   AM_RANGE(0x9fc00000, 0x9fc7ffff) AM_ROM AM_SHARE("share2") /* bios mirror */
694   AM_RANGE(0xa0000000, 0xa01fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
695   AM_RANGE(0xbfc00000, 0xbfc7ffff) AM_ROM AM_SHARE("share2") /* bios mirror */
696   AM_RANGE(0xfffe0130, 0xfffe0133) AM_WRITENOP
697ADDRESS_MAP_END
698
699
700static MACHINE_RESET( psx )
701{
702   psx_machine_init(machine);
703   psx_sio_install_handler( machine, 0, psx_sio0 );
704
705   psx_dma_install_read_handler(machine, 3, cd_dma_read);
706   psx_dma_install_write_handler(machine, 3, cd_dma_write);
707}
708
709static DRIVER_INIT( psx )
710{
711   psx_driver_init(machine);
712}
713
714static INPUT_PORTS_START( psx )
715   PORT_START("IN0")
716   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )  PORT_PLAYER(1)
717   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )  PORT_PLAYER(1)
718   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1)
719   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )   PORT_PLAYER(1)
720   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START )  PORT_PLAYER(1)
721   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(1)
722   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(1)
723   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SELECT ) PORT_PLAYER(1)
724
725   PORT_START("IN1")
726   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 Square")
727   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 Cross")
728   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 Circle")
729   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("P1 Triangle")
730   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(1) PORT_NAME("P1 R1")
731   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(1) PORT_NAME("P1 L1")
732   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(1) PORT_NAME("P1 R2")
733   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_PLAYER(1) PORT_NAME("P1 L2")
734
735   PORT_START("IN2")
736   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )  PORT_PLAYER(2)
737   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )  PORT_PLAYER(2)
738   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
739   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )   PORT_PLAYER(2)
740   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START )  PORT_PLAYER(2)
741   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(2)
742   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(2)
743   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SELECT ) PORT_PLAYER(2)
744
745   PORT_START("IN3")
746   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 Square")
747   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 Cross")
748   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 Circle")
749   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(2) PORT_NAME("P2 Triangle")
750   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(2) PORT_NAME("P2 R1")
751   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(2) PORT_NAME("P2 L1")
752   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(2) PORT_NAME("P2 R2")
753   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_PLAYER(2) PORT_NAME("P2 L2")
754INPUT_PORTS_END
755
756static void spu_irq(device_t *device, UINT32 data)
757{
758   if (data)
759   {
760      psx_irq_set(device->machine(), 1<<9);
761   }
762}
763
764static MACHINE_CONFIG_START( psxntsc, psx1_state )
765   /* basic machine hardware */
766   MCFG_CPU_ADD( "maincpu", CXD8530AQ, XTAL_67_7376MHz )
767   MCFG_CPU_PROGRAM_MAP( psx_map)
768   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
769
770   MCFG_SCREEN_ADD("screen", RASTER)
771   MCFG_SCREEN_REFRESH_RATE( 60 )
772   MCFG_SCREEN_VBLANK_TIME(0)
773
774   MCFG_MACHINE_RESET( psx )
775
776   /* video hardware */
777   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
778   MCFG_SCREEN_SIZE( 1024, 512 )
779   MCFG_SCREEN_VISIBLE_AREA( 0, 639, 0, 479 )
780   MCFG_SCREEN_UPDATE( psx )
781
782   MCFG_PALETTE_LENGTH( 65536 )
783
784   MCFG_PALETTE_INIT( psx )
785   MCFG_VIDEO_START( psx_type2 )
786
787   /* sound hardware */
788   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
789   MCFG_SPU_ADD( "spu", XTAL_67_7376MHz/2, &spu_irq )
790   MCFG_SOUND_ROUTE( 0, "lspeaker", 1.00 )
791   MCFG_SOUND_ROUTE( 1, "rspeaker", 1.00 )
792
793   /* quickload */
794   MCFG_QUICKLOAD_ADD("quickload", psx_exe_load, "cpe,exe,psf,psx", 0)
795
796   MCFG_CDROM_ADD("cdrom")
797   MCFG_PSXCD_ADD("cdrom")
798   MCFG_PSXCARD_ADD("card1")
799   MCFG_PSXCARD_ADD("card2")
800MACHINE_CONFIG_END
801
802static MACHINE_CONFIG_START( psxpal, psx1_state )
803   /* basic machine hardware */
804   MCFG_CPU_ADD( "maincpu", CXD8530AQ, XTAL_67_7376MHz )
805   MCFG_CPU_PROGRAM_MAP( psx_map)
806   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
807
808   MCFG_SCREEN_ADD("screen", RASTER)
809   MCFG_SCREEN_REFRESH_RATE( 50 )
810   MCFG_SCREEN_VBLANK_TIME(0)
811
812   MCFG_MACHINE_RESET( psx )
813
814   /* video hardware */
815   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
816   MCFG_SCREEN_SIZE( 1024, 512 )
817   MCFG_SCREEN_VISIBLE_AREA( 0, 639, 0, 511 )
818   MCFG_SCREEN_UPDATE( psx )
819   MCFG_PALETTE_LENGTH( 65536 )
820
821   MCFG_PALETTE_INIT( psx )
822   MCFG_VIDEO_START( psx_type2 )
823
824   /* sound hardware */
825   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
826   MCFG_SPU_ADD( "spu", XTAL_67_7376MHz/2, &spu_irq )
827   MCFG_SOUND_ROUTE( 0, "lspeaker", 1.00 )
828   MCFG_SOUND_ROUTE( 1, "rspeaker", 1.00 )
829
830   /* quickload */
831   MCFG_QUICKLOAD_ADD("quickload", psx_exe_load, "cpe,exe,psf,psx", 0)
832
833   MCFG_CDROM_ADD("cdrom")
834   MCFG_PSXCD_ADD("cdrom")
835   MCFG_PSXCARD_ADD("card1")
836   MCFG_PSXCARD_ADD("card2")
837MACHINE_CONFIG_END
838
839ROM_START( psj )
840   ROM_REGION32_LE( 0x080000, "user1", 0 )
841
842   ROM_SYSTEM_BIOS( 0, "1.0J", "SCPH-1000/DTL-H1000" ) // 22091994
843   ROMX_LOAD( "ps-10j.bin",    0x0000000, 0x080000, CRC(3b601fc8) SHA1(343883a7b555646da8cee54aadd2795b6e7dd070), ROM_BIOS(1) )
844
845   ROM_SYSTEM_BIOS( 1, "1.1J", "SCPH-3000/DTL-H1000H (Version 1.1 01/22/95)" ) // 22091994
846   ROMX_LOAD( "ps-11j.bin",    0x0000000, 0x080000, CRC(3539def6) SHA1(b06f4a861f74270be819aa2a07db8d0563a7cc4e), ROM_BIOS(2) )
847
848   ROM_SYSTEM_BIOS( 2, "2.1J", "SCPH-3500 (Version 2.1 07/17/95 J)" ) // 22091994
849   ROMX_LOAD( "ps-21j.bin",    0x0000000, 0x080000, CRC(bc190209) SHA1(e38466a4ba8005fba7e9e3c7b9efeba7205bee3f), ROM_BIOS(3) )
850
851   ROM_SYSTEM_BIOS( 3, "2.2J", "SCPH-5000/DTL-H1200 (Version 2.2 12/04/95 J)" ) // 04121995
852/*  ROMX_LOAD( "ps-22j.bad",    0x0000000, 0x080000, BAD_DUMP CRC(8c93a399) SHA1(e340db2696274dda5fdc25e434a914db71e8b02b), ROM_BIOS(4) ) */
853   ROMX_LOAD( "ps-22j.bin",    0x0000000, 0x080000, CRC(24fc7e17) SHA1(ffa7f9a7fb19d773a0c3985a541c8e5623d2c30d), ROM_BIOS(4) )
854
855   ROM_SYSTEM_BIOS( 4, "2.2D", "DTL-H1100 (Version 2.2 03/06/96 D)" ) // 04121995
856   ROMX_LOAD( "ps-22d.bin",    0x0000000, 0x080000, CRC(decb22f5) SHA1(73107d468fc7cb1d2c5b18b269715dd889ecef06), ROM_BIOS(5) )
857
858   ROM_SYSTEM_BIOS( 5, "3.0J", "SCPH-5500 (Version 3.0 09/09/96 J)" ) // 04121995
859   ROMX_LOAD( "ps-30j.bin",    0x0000000, 0x080000, CRC(ff3eeb8c) SHA1(b05def971d8ec59f346f2d9ac21fb742e3eb6917), ROM_BIOS(6) )
860
861   ROM_SYSTEM_BIOS( 6, "4.0J", "SCPH-7000/SCPH-9000 (Version 4.0 08/18/97 J)" ) // 29051997
862   ROMX_LOAD( "ps-40j.bin",    0x0000000, 0x080000, CRC(ec541cd0) SHA1(77b10118d21ac7ffa9b35f9c4fd814da240eb3e9), ROM_BIOS(7) )
863
864   ROM_SYSTEM_BIOS( 7, "4.3J", "SCPH-100 (Version 4.3 03/11/00 J)" ) // 04121995
865   ROMX_LOAD( "psone-43j.bin", 0x0000000, 0x080000, CRC(f2af798b) SHA1(339a48f4fcf63e10b5b867b8c93cfd40945faf6c), ROM_BIOS(8) )
866ROM_END
867
868ROM_START( psu )
869   ROM_REGION32_LE( 0x080000, "user1", 0 )
870
871   ROM_SYSTEM_BIOS( 0, "2.0A", "DTL-H1001 (Version 2.0 05/07/95 A)" ) // 22091994
872   ROMX_LOAD( "ps-20a.bin",    0x0000000, 0x080000, CRC(55847d8c) SHA1(649895efd79d14790eabb362e94eb0622093dfb9), ROM_BIOS(1) )
873
874   ROM_SYSTEM_BIOS( 1, "2.1A", "DTL-H1101 (Version 2.1 07/17/95 A)" ) // 22091994
875   ROMX_LOAD( "ps-21a.bin",    0x0000000, 0x080000, CRC(aff00f2f) SHA1(ca7af30b50d9756cbd764640126c454cff658479), ROM_BIOS(2) )
876
877   ROM_SYSTEM_BIOS( 2, "2.2A", "SCPH-1001/DTL-H1201/DTL-H3001 (Version 2.2 12/04/95 A)" ) // 04121995
878   ROMX_LOAD( "ps-22a.bin",    0x0000000, 0x080000, CRC(37157331) SHA1(10155d8d6e6e832d6ea66db9bc098321fb5e8ebf), ROM_BIOS(3) )
879
880   ROM_SYSTEM_BIOS( 3, "3.0A", "SCPH-5501/SCPH-7003 (Version 3.0 11/18/96 A)" ) // 04121995
881   ROMX_LOAD( "ps-30a.bin",    0x0000000, 0x080000, CRC(8d8cb7e4) SHA1(0555c6fae8906f3f09baf5988f00e55f88e9f30b), ROM_BIOS(4) )
882
883   ROM_SYSTEM_BIOS( 4, "4.1A", "SCPH-7001/SCPH-7501/SCPH-7503/SCPH-9001 (Version 4.1 12/16/97 A)" ) // 04121995
884   ROMX_LOAD( "ps-41a.bin",    0x0000000, 0x080000, CRC(502224b6) SHA1(14df4f6c1e367ce097c11deae21566b4fe5647a9), ROM_BIOS(5) )
885
886   ROM_SYSTEM_BIOS( 5, "4.5A", "SCPH-101 (Version 4.5 05/25/00 A)" ) // 04121995
887   ROMX_LOAD( "psone-45a.bin", 0x0000000, 0x080000, CRC(171bdcec) SHA1(dcffe16bd90a723499ad46c641424981338d8378), ROM_BIOS(6) )
888ROM_END
889
890ROM_START( pse )
891   ROM_REGION32_LE( 0x080000, "user1", 0 )
892
893   ROM_SYSTEM_BIOS( 0, "2.0E", "DTL-H1002/SCPH-1002 (Version 2.0 05/10/95 E)" ) // 22091994
894   ROMX_LOAD( "ps-20e.bin",    0x0000000, 0x080000, CRC(9bb87c4b) SHA1(20b98f3d80f11cbf5a7bfd0779b0e63760ecc62c), ROM_BIOS(1) )
895
896   ROM_SYSTEM_BIOS( 1, "2.1E", "SCPH-1002/DTL-H1102 (Version 2.1 07/17/95 E)" ) // 22091994
897   ROMX_LOAD( "ps-21e.bin",    0x0000000, 0x080000, CRC(86c30531) SHA1(76cf6b1b2a7c571a6ad07f2bac0db6cd8f71e2cc), ROM_BIOS(2) )
898
899   ROM_SYSTEM_BIOS( 2, "2.2E", "SCPH-1002/DTL-H1202/DTL-H3002 (Version 2.2 12/04/95 E)" ) // 04121995
900   ROMX_LOAD( "ps-22e.bin",    0x0000000, 0x080000, CRC(1e26792f) SHA1(b6a11579caef3875504fcf3831b8e3922746df2c), ROM_BIOS(3) )
901
902   ROM_SYSTEM_BIOS( 3, "3.0E", "SCPH-5502/SCPH-5552 (Version 3.0 01/06/97 E)" ) // 04121995
903/*  ROMX_LOAD( "ps-30e.bad",    0x0000000, 0x080000, BAD_DUMP CRC(4d9e7c86) SHA1(f8de9325fc36fcfa4b29124d291c9251094f2e54), ROM_BIOS(4) ) */
904   ROMX_LOAD( "ps-30e.bin",    0x0000000, 0x080000, CRC(d786f0b9) SHA1(f6bc2d1f5eb6593de7d089c425ac681d6fffd3f0), ROM_BIOS(4) )
905
906   ROM_SYSTEM_BIOS( 4, "4.1E", "SCPH-7002/SCPH-7502/SCPH-9002 (Version 4.1 12/16/97 E)" ) // 04121995
907   ROMX_LOAD( "ps-41e.bin",    0x0000000, 0x080000, CRC(318178bf) SHA1(8d5de56a79954f29e9006929ba3fed9b6a418c1d), ROM_BIOS(5) )
908
909   ROM_SYSTEM_BIOS( 5, "4.4E", "SCPH-102 (Version 4.4 03/24/00 E)" ) // 04121995
910   ROMX_LOAD( "psone-44e.bin", 0x0000000, 0x080000, CRC(0bad7ea9) SHA1(beb0ac693c0dc26daf5665b3314db81480fa5c7c), ROM_BIOS(6) )
911
912   ROM_SYSTEM_BIOS( 6, "4.5E", "SCPH-102 (Version 4.5 05/25/00 E)" ) // 04121995
913   ROMX_LOAD( "psone-45e.bin", 0x0000000, 0x080000, CRC(76b880e5) SHA1(dbc7339e5d85827c095764fc077b41f78fd2ecae), ROM_BIOS(7) )
914ROM_END
915
916ROM_START( psa )
917   ROM_REGION32_LE( 0x080000, "user1", 0 )
918
919   ROM_SYSTEM_BIOS( 0, "3.0A", "SCPH-5501/SCPH-7003 (Version 3.0 11/18/96 A)" ) // 04121995
920   ROMX_LOAD( "ps-30a.bin",    0x0000000, 0x080000, CRC(8d8cb7e4) SHA1(0555c6fae8906f3f09baf5988f00e55f88e9f30b), ROM_BIOS(1) )
921
922   ROM_SYSTEM_BIOS( 1, "4.1A", "SCPH-7001/SCPH-7501/SCPH-7503/SCPH-9001 (Version 4.1 12/16/97 A)" ) // 04121995
923   ROMX_LOAD( "ps-41a.bin",    0x0000000, 0x080000, CRC(502224b6) SHA1(14df4f6c1e367ce097c11deae21566b4fe5647a9), ROM_BIOS(2) )
924ROM_END
925
926/*
927The version number & release date is stored in ascii text at the end of every bios, except for scph1000.
928There is also a BCD encoded date at offset 0x100, but this is set to 22091994 for versions prior to 2.2
929and 04121995 for all versions from 2.2 ( except Version 4.0J which is 29051997 ).
930
931Consoles not dumped:
932
933DTL-H1001H
934DTL-H3000
935SCPH-5001
936SCPH-5002
937SCPH-5003
938SCPH-5503
939SCPH-7500
940SCPH-9003
941SCPH-103
942
943Holes in version numbers:
944
945Version 2.0 J
946Version 4.1 J
947Version 4.2 J
948Version 4.4 J
949Version 4.5 J
950Version 4.0 A
951Version 4.2 A
952Version 4.3 A
953Version 4.4 A
954Version 4.0 E
955Version 4.2 E
956Version 4.3 E
957
958*/
959
960/*    YEAR  NAME    PARENT  COMPAT  MACHINE     INPUT   INIT    COMPANY                             FULLNAME                            FLAGS */
961CONS( 1994, psj,    0,      0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Japan)",         GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
962CONS( 1995, pse,    psj,    0,      psxpal,     psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Europe)",        GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
963CONS( 1995, psu,    psj,    0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (USA)",           GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
964CONS( 1995, psa,    psj,    0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Asia-Pacific)",  GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
src/mame/drivers/zn.c
r11505r11506
14471447   }
14481448}
14491449
1450static void atpsx_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
1450static void atpsx_dma_read( zn_state *state, UINT32 n_address, INT32 n_size )
14511451{
1452   zn_state *state = machine.driver_data<zn_state>();
14531452   UINT32 *p_n_psxram = state->m_p_n_psxram;
1454   device_t *ide = machine.device("ide");
1453   device_t *ide = state->machine().device("ide");
14551454
14561455   logerror("DMA read: %d bytes (%d words) to %08x\n", n_size<<2, n_size, n_address);
14571456
r11505r11506
14721471   }
14731472}
14741473
1475static void atpsx_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
1474static void atpsx_dma_write( zn_state *state, UINT32 n_address, INT32 n_size )
14761475{
14771476   logerror("DMA write from %08x for %d bytes\n", n_address, n_size<<2);
14781477}
r11505r11506
14961495   zn_machine_init(machine);
14971496
14981497   devtag_reset(machine, "ide");
1499   psx_dma_install_read_handler(machine, 5, atpsx_dma_read);
1500   psx_dma_install_write_handler(machine, 5, atpsx_dma_write);
1498   psx_dma_install_read_handler( machine, 5, psx_dma_read_delegate( FUNC( atpsx_dma_read ), machine.driver_data<zn_state>() ) );
1499   psx_dma_install_write_handler( machine, 5, psx_dma_write_delegate( FUNC( atpsx_dma_write ), machine.driver_data<zn_state>() ) );
15011500}
15021501
15031502static MACHINE_CONFIG_DERIVED( coh1000w, zn1_2mb_vram )
src/mame/drivers/namcos12.c
r11505r11506
11471147   verboselog( space->machine(), 1, "dmaoffset_w( %08x, %08x, %08x ) %08x\n", offset, data, mem_mask, state->m_n_dmaoffset );
11481148}
11491149
1150static void namcos12_rom_read( running_machine &machine, UINT32 n_address, INT32 n_size )
1150static void namcos12_rom_read( namcos12_state *state, UINT32 n_address, INT32 n_size )
11511151{
1152   namcos12_state *state = machine.driver_data<namcos12_state>();
11531152   const char *n_region;
11541153   int n_offset;
11551154
r11505r11506
11631162   {
11641163      n_region = "user2";
11651164      n_offset = state->m_n_tektagdmaoffset & 0x7fffffff;
1166      verboselog( machine, 1, "namcos12_rom_read( %08x, %08x ) tektagt %08x\n", n_address, n_size, n_offset );
1165      verboselog( state->machine(), 1, "namcos12_rom_read( %08x, %08x ) tektagt %08x\n", n_address, n_size, n_offset );
11671166   }
11681167   else if( ( state->m_n_dmaoffset >= 0x80000000 ) || ( state->m_n_dmabias == 0x1f300000 ) )
11691168   {
11701169      n_region = "user1";
11711170      n_offset = state->m_n_dmaoffset & 0x003fffff;
1172      verboselog( machine, 1, "namcos12_rom_read( %08x, %08x ) boot %08x\n", n_address, n_size, n_offset );
1171      verboselog( state->machine(), 1, "namcos12_rom_read( %08x, %08x ) boot %08x\n", n_address, n_size, n_offset );
11731172   }
11741173   else
11751174   {
11761175      n_region = "user2";
11771176      n_offset = state->m_n_dmaoffset & 0x7fffffff;
1178      verboselog( machine, 1, "namcos12_rom_read( %08x, %08x ) game %08x\n", n_address, n_size, n_offset );
1177      verboselog( state->machine(), 1, "namcos12_rom_read( %08x, %08x ) game %08x\n", n_address, n_size, n_offset );
11791178   }
11801179
1181   source = (UINT16 *) machine.region( n_region )->base();
1182   n_romleft = ( machine.region( n_region )->bytes() - n_offset ) / 4;
1180   source = (UINT16 *) state->machine().region( n_region )->base();
1181   n_romleft = ( state->machine().region( n_region )->bytes() - n_offset ) / 4;
11831182   if( n_size > n_romleft )
11841183   {
1185      verboselog( machine, 1, "namcos12_rom_read dma truncated %d to %d passed end of rom\n", n_size, n_romleft );
1184      verboselog( state->machine(), 1, "namcos12_rom_read dma truncated %d to %d passed end of rom\n", n_size, n_romleft );
11861185      n_size = n_romleft;
11871186   }
11881187
r11505r11506
11901189   n_ramleft = ( state->m_n_psxramsize - n_address ) / 4;
11911190   if( n_size > n_ramleft )
11921191   {
1193      verboselog( machine, 1, "namcos12_rom_read dma truncated %d to %d passed end of ram\n", n_size, n_ramleft );
1192      verboselog( state->machine(), 1, "namcos12_rom_read dma truncated %d to %d passed end of ram\n", n_size, n_ramleft );
11941193      n_size = n_ramleft;
11951194   }
11961195
r11505r11506
16401639static MACHINE_CONFIG_START( coh700, namcos12_state )
16411640   /* basic machine hardware */
16421641   MCFG_CPU_ADD( "maincpu", CXD8661R, XTAL_100MHz )
1643   MCFG_PSX_DMA_CHANNEL_READ( 5, namcos12_rom_read )
1644
16451642   MCFG_CPU_PROGRAM_MAP( namcos12_map)
16461643   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
16471644
1645   MCFG_PSX_DMA_CHANNEL_READ( "maincpu", 5, psx_dma_read_delegate( FUNC( namcos12_rom_read ), (namcos12_state *) owner ) )
1646
16481647   MCFG_CPU_ADD("sub", H83002, 16737350 )
16491648   MCFG_CPU_PROGRAM_MAP( s12h8rwmap)
16501649   MCFG_CPU_IO_MAP( s12h8iomap)
src/mame/drivers/konamigq.c
r11505r11506
270270
271271/* SCSI */
272272
273static void scsi_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
273static void scsi_dma_read( konamigq_state *state, UINT32 n_address, INT32 n_size )
274274{
275   konamigq_state *state = machine.driver_data<konamigq_state>();
276275   UINT32 *p_n_psxram = state->m_p_n_psxram;
277276   UINT8 *sector_buffer = state->m_sector_buffer;
278277   int i;
r11505r11506
306305   }
307306}
308307
309static void scsi_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
308static void scsi_dma_write( konamigq_state *state, UINT32 n_address, INT32 n_size )
310309{
311310}
312311
r11505r11506
365364static MACHINE_CONFIG_START( konamigq, konamigq_state )
366365   /* basic machine hardware */
367366   MCFG_CPU_ADD( "maincpu", CXD8530BQ, XTAL_67_7376MHz )
368   MCFG_PSX_DMA_CHANNEL_READ( 5, scsi_dma_read )
369   MCFG_PSX_DMA_CHANNEL_WRITE( 5, scsi_dma_write )
370367   MCFG_CPU_PROGRAM_MAP( konamigq_map )
371368   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
372369
370   MCFG_PSX_DMA_CHANNEL_READ( "maincpu", 5, psx_dma_read_delegate( FUNC( scsi_dma_read ), (konamigq_state *) owner ) )
371   MCFG_PSX_DMA_CHANNEL_WRITE( "maincpu", 5, psx_dma_write_delegate( FUNC( scsi_dma_write ), (konamigq_state *) owner ) )
372
373373   MCFG_CPU_ADD( "soundcpu", M68000, 8000000 )
374374   MCFG_CPU_PROGRAM_MAP( konamigq_sound_map)
375375   MCFG_CPU_PERIODIC_INT( irq2_line_hold, 480 )
src/mame/drivers/twinkle.c
r11505r11506
745745
746746/* SCSI */
747747
748static void scsi_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
748static void scsi_dma_read( twinkle_state *state, UINT32 n_address, INT32 n_size )
749749{
750   twinkle_state *state = machine.driver_data<twinkle_state>();
751750   UINT32 *p_n_psxram = state->m_p_n_psxram;
752751   int i;
753752   int n_this;
r11505r11506
790789   }
791790}
792791
793static void scsi_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
792static void scsi_dma_write( twinkle_state *state, UINT32 n_address, INT32 n_size )
794793{
795   twinkle_state *state = machine.driver_data<twinkle_state>();
796794   UINT32 *p_n_psxram = state->m_p_n_psxram;
797795   int i;
798796   int n_this;
r11505r11506
880878static MACHINE_CONFIG_START( twinkle, twinkle_state )
881879   /* basic machine hardware */
882880   MCFG_CPU_ADD( "maincpu", CXD8530CQ, XTAL_67_7376MHz )
883   MCFG_PSX_DMA_CHANNEL_READ( 5, scsi_dma_read )
884   MCFG_PSX_DMA_CHANNEL_WRITE( 5, scsi_dma_write )
885881   MCFG_CPU_PROGRAM_MAP( main_map )
886882   MCFG_CPU_VBLANK_INT( "mainscreen", psx_vblank )
887883
884   MCFG_PSX_DMA_CHANNEL_READ( "maincpu", 5, psx_dma_read_delegate( FUNC( scsi_dma_read ), (twinkle_state *) owner ) )
885   MCFG_PSX_DMA_CHANNEL_WRITE( "maincpu", 5, psx_dma_write_delegate( FUNC( scsi_dma_write ), (twinkle_state *) owner ) )
886
888887   MCFG_CPU_ADD("audiocpu", M68000, 32000000/2)   /* 16.000 MHz */
889888   MCFG_CPU_PROGRAM_MAP( sound_map )
890889
src/mame/drivers/konamigv.c
r11505r11506
183183
184184/* SCSI */
185185
186static void scsi_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
186static void scsi_dma_read( konamigv_state *state, UINT32 n_address, INT32 n_size )
187187{
188   konamigv_state *state = machine.driver_data<konamigv_state>();
189188   UINT32 *p_n_psxram = state->m_p_n_psxram;
190189   UINT8 *sector_buffer = state->m_sector_buffer;
191190   int i;
r11505r11506
229228   }
230229}
231230
232static void scsi_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
231static void scsi_dma_write( konamigv_state *state, UINT32 n_address, INT32 n_size )
233232{
234   konamigv_state *state = machine.driver_data<konamigv_state>();
235233   UINT32 *p_n_psxram = state->m_p_n_psxram;
236234   UINT8 *sector_buffer = state->m_sector_buffer;
237235   int i;
r11505r11506
329327static MACHINE_CONFIG_START( konamigv, konamigv_state )
330328   /* basic machine hardware */
331329   MCFG_CPU_ADD( "maincpu", CXD8530BQ, XTAL_67_7376MHz )
332   MCFG_PSX_DMA_CHANNEL_READ( 5, scsi_dma_read )
333   MCFG_PSX_DMA_CHANNEL_WRITE( 5, scsi_dma_write )
334330   MCFG_CPU_PROGRAM_MAP( konamigv_map )
335331   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
336332
333   MCFG_PSX_DMA_CHANNEL_READ( "maincpu", 5, psx_dma_read_delegate( FUNC( scsi_dma_read ), (konamigv_state *) owner ) )
334   MCFG_PSX_DMA_CHANNEL_WRITE( "maincpu", 5, psx_dma_write_delegate( FUNC( scsi_dma_write ), (konamigv_state *) owner ) )
335
337336   MCFG_MACHINE_START( konamigv )
338337   MCFG_MACHINE_RESET( konamigv )
339338
src/mame/drivers/ksys573.c
r11505r11506
10721072   }
10731073}
10741074
1075static void cdrom_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
1075static void cdrom_dma_read( ksys573_state *state, UINT32 n_address, INT32 n_size )
10761076{
1077   verboselog( machine, 2, "cdrom_dma_read( %08x, %08x )\n", n_address, n_size );
1077   verboselog( state->machine(), 2, "cdrom_dma_read( %08x, %08x )\n", n_address, n_size );
10781078//  mame_printf_debug("DMA read: address %08x size %08x\n", n_address, n_size);
10791079}
10801080
1081static void cdrom_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
1081static void cdrom_dma_write( ksys573_state *state, UINT32 n_address, INT32 n_size )
10821082{
1083   ksys573_state *state = machine.driver_data<ksys573_state>();
1084
1085   verboselog( machine, 2, "cdrom_dma_write( %08x, %08x )\n", n_address, n_size );
1083   verboselog( state->machine(), 2, "cdrom_dma_write( %08x, %08x )\n", n_address, n_size );
10861084//  mame_printf_debug("DMA write: address %08x size %08x\n", n_address, n_size);
10871085
10881086   state->m_atapi_xferbase = n_address;
10891087
1090   verboselog( machine, 2, "atapi_xfer_end: %d %d\n", state->m_atapi_xferlen, state->m_atapi_xfermod );
1088   verboselog( state->machine(), 2, "atapi_xfer_end: %d %d\n", state->m_atapi_xferlen, state->m_atapi_xfermod );
10911089
10921090   // set a transfer complete timer (Note: CYCLES_PER_SECTOR can't be lower than 2000 or the BIOS ends up "out of order")
1093   state->m_atapi_timer->adjust(machine.device<cpu_device>("maincpu")->cycles_to_attotime((ATAPI_CYCLES_PER_SECTOR * (state->m_atapi_xferlen/2048))));
1091   state->m_atapi_timer->adjust(state->machine().device<cpu_device>("maincpu")->cycles_to_attotime((ATAPI_CYCLES_PER_SECTOR * (state->m_atapi_xferlen/2048))));
10941092}
10951093
10961094static WRITE32_HANDLER( security_w )
r11505r11506
29612959static MACHINE_CONFIG_START( konami573, ksys573_state )
29622960   /* basic machine hardware */
29632961   MCFG_CPU_ADD( "maincpu", CXD8530CQ, XTAL_67_7376MHz )
2964   MCFG_PSX_DMA_CHANNEL_READ( 5, cdrom_dma_read )
2965   MCFG_PSX_DMA_CHANNEL_WRITE( 5, cdrom_dma_write )
29662962   MCFG_CPU_PROGRAM_MAP( konami573_map )
29672963   MCFG_CPU_VBLANK_INT("screen", sys573_vblank)
29682964
2965   MCFG_PSX_DMA_CHANNEL_READ( "maincpu", 5, psx_dma_read_delegate( FUNC( cdrom_dma_read ), (ksys573_state *) owner ) )
2966   MCFG_PSX_DMA_CHANNEL_WRITE( "maincpu", 5, psx_dma_write_delegate( FUNC( cdrom_dma_write ), (ksys573_state *) owner ) )
2967
29692968   MCFG_MACHINE_RESET( konami573 )
29702969
29712970   // onboard flash
src/emu/cpu/cpu.mak
r11505r11506
885885
886886ifneq ($(filter PSX,$(CPUS)),)
887887OBJDIRS += $(CPUOBJ)/psx
888CPUOBJS += $(CPUOBJ)/psx/psx.o $(CPUOBJ)/psx/gte.o $(CPUOBJ)/psx/dma.o
888CPUOBJS += $(CPUOBJ)/psx/psx.o $(CPUOBJ)/psx/gte.o $(CPUOBJ)/psx/dma.o $(CPUOBJ)/psx/mdec.o
889889DASMOBJS += $(CPUOBJ)/psx/psxdasm.o
890890endif
891891
r11505r11506
893893         $(CPUSRC)/psx/psx.h \
894894         $(CPUSRC)/psx/dma.h \
895895         $(CPUSRC)/psx/gte.h \
896         $(CPUSRC)/psx/mdec.h \
896897
897898$(CPUOBJ)/psx/gte.o:   $(CPUSRC)/psx/gte.c \
898899         $(CPUSRC)/psx/gte.h
r11505r11506
900901$(CPUOBJ)/psx/dma.o:   $(CPUSRC)/psx/dma.c \
901902         $(CPUSRC)/psx/dma.h
902903
904$(CPUOBJ)/psx/mdec.o:   $(CPUSRC)/psx/mdec.c \
905         $(CPUSRC)/psx/mdec.h
906
907
908
903909#-------------------------------------------------
904910# Mitsubishi M37702 and M37710 (based on 65C816)
905911#-------------------------------------------------
src/emu/cpu/psx/psx.h
r11505r11506
103103//  INTERFACE CONFIGURATION MACROS
104104//**************************************************************************
105105
106#define MCFG_PSX_DMA_CHANNEL_READ( channel, handler ) \
107   psxcpu_device::install_dma_read_handler( *device, channel, handler ); \
106#define MCFG_PSX_DMA_CHANNEL_READ( cputag, channel, handler ) \
107   psxcpu_device::install_dma_read_handler( *owner, cputag, channel, handler ); \
108108
109#define MCFG_PSX_DMA_CHANNEL_WRITE( channel, handler ) \
110   psxcpu_device::install_dma_write_handler( *device, channel, handler ); \
109#define MCFG_PSX_DMA_CHANNEL_WRITE( cputag, channel, handler ) \
110   psxcpu_device::install_dma_write_handler( *owner, cputag, channel, handler ); \
111111
112112
113113
r11505r11506
129129   WRITE32_MEMBER( berr_w );
130130   READ32_MEMBER( berr_r );
131131
132   static void install_dma_read_handler( device_t &device, int channel, psx_dma_read_handler handler );
133   static void install_dma_write_handler( device_t &device, int channel, psx_dma_write_handler handler );
132   static void install_dma_read_handler( device_t &device, const char *cputag, int channel, psx_dma_read_delegate handler );
133   static void install_dma_write_handler( device_t &device, const char *cputag, int channel, psx_dma_write_delegate handler );
134134
135135protected:
136136   psxcpu_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock, address_map_constructor internal_map);
src/emu/cpu/psx/dma.c
r11505r11506
3333   {
3434      psx_dma_channel *dma = &channel[ index ];
3535
36      dma->fn_read = NULL;
37      dma->fn_write = NULL;
36      dma->read_set = false;
37      dma->write_set = false;
3838   }
3939}
4040
r11505r11506
135135void psxdma_device::dma_finished( int index )
136136{
137137   psx_machine *p_psx = machine().driver_data<psx_state>()->m_p_psx;
138   UINT32 *p_n_psxram = p_psx->p_n_psxram;
138139
139   UINT32 *p_n_psxram = p_psx->p_n_psxram;
140140   psx_dma_channel *dma = &channel[ index ];
141141
142142   if( dma->n_channelcontrol == 0x01000401 && index == 2 )
r11505r11506
171171            n_address &= n_adrmask;
172172            n_nextaddress = p_n_psxram[ n_address / 4 ];
173173            n_size = n_nextaddress >> 24;
174            (*dma->fn_write)( machine(), n_address + 4, n_size );
174            dma->fn_write( n_address + 4, n_size );
175175            //FIXME:
176176            // The following conditions will cause an endless loop.
177177            // If stopping the transfer is correct I cannot judge
r11505r11506
202202   dma_finished(param);
203203}
204204
205void psxdma_device::install_read_handler( int index, psx_dma_read_handler p_fn_dma_read )
205void psxdma_device::install_read_handler( int index, psx_dma_read_delegate p_fn_dma_read )
206206{
207207   channel[ index ].fn_read = p_fn_dma_read;
208   channel[ index ].read_set = true;
208209}
209210
210void psxdma_device::install_write_handler( int index, psx_dma_read_handler p_fn_dma_write )
211void psxdma_device::install_write_handler( int index, psx_dma_read_delegate p_fn_dma_write )
211212{
212213   channel[ index ].fn_write = p_fn_dma_write;
214   channel[ index ].write_set = true;
213215}
214216
215217WRITE32_MEMBER( psxdma_device::write )
r11505r11506
258260            }
259261
260262            if( dma->n_channelcontrol == 0x01000000 &&
261               dma->fn_read != NULL )
263               dma->read_set )
262264            {
263265               verboselog( machine(), 1, "dma %d read block %08x %08x\n", index, n_address, n_size );
264               (*dma->fn_read)( machine(), n_address, n_size );
266               dma->fn_read( n_address, n_size );
265267               dma_finished( index );
266268            }
267269            else if (dma->n_channelcontrol == 0x11000000 &&   // CD DMA
268               dma->fn_read != NULL )
270               dma->read_set )
269271            {
270272               verboselog( machine(), 1, "dma %d read block %08x %08x\n", index, n_address, n_size );
271273
r11505r11506
274276               oursize = (oursize > 1) ? oursize : 1;
275277               oursize *= (dma->n_blockcontrol&0xffff);
276278
277               (*dma->fn_read)( machine(), n_address, oursize );
279               dma->fn_read( n_address, oursize );
278280               dma_finished( index );
279281            }
280282            else if( dma->n_channelcontrol == 0x01000200 &&
281               dma->fn_read != NULL )
283               dma->read_set )
282284            {
283285               verboselog( machine(), 1, "dma %d read block %08x %08x\n", index, n_address, n_size );
284               (*dma->fn_read)( machine(), n_address, n_size );
286               dma->fn_read( n_address, n_size );
285287               if( index == 1 )
286288               {
287289                  dma_start_timer( index, 26000 );
r11505r11506
292294               }
293295            }
294296            else if( dma->n_channelcontrol == 0x01000201 &&
295               dma->fn_write != NULL )
297               dma->write_set )
296298            {
297299               verboselog( machine(), 1, "dma %d write block %08x %08x\n", index, n_address, n_size );
298               (*dma->fn_write)( machine(), n_address, n_size );
300               dma->fn_write( n_address, n_size );
299301               dma_finished( index );
300302            }
301303            else if( dma->n_channelcontrol == 0x11050100 &&
302               dma->fn_write != NULL )
304               dma->write_set )
303305            {
304306               /* todo: check this is a write not a read... */
305307               verboselog( machine(), 1, "dma %d write block %08x %08x\n", index, n_address, n_size );
306               (*dma->fn_write)( machine(), n_address, n_size );
308               dma->fn_write( n_address, n_size );
307309               dma_finished( index );
308310            }
309311            else if( dma->n_channelcontrol == 0x11150100 &&
310               dma->fn_write != NULL )
312               dma->write_set )
311313            {
312314               /* todo: check this is a write not a read... */
313315               verboselog( machine(), 1, "dma %d write block %08x %08x\n", index, n_address, n_size );
314               (*dma->fn_write)( machine(), n_address, n_size );
316               dma->fn_write( n_address, n_size );
315317               dma_finished( index );
316318            }
317319            else if( dma->n_channelcontrol == 0x01000401 &&
318320               index == 2 &&
319               dma->fn_write != NULL )
321               dma->write_set )
320322            {
321323               verboselog( machine(), 1, "dma %d write linked list %08x\n",
322324                  index, dma->n_base );
src/emu/cpu/psx/dma.h
r11505r11506
55 *
66 */
77
8#if !defined(PSXDMA_H)
8#pragma once
99
10#define PSXDMA_H (1)
10#ifndef __PSXDMA_H__
11#define __PSXDMA_H__
1112
1213#include "emu.h"
1314
1415extern const device_type PSX_DMA;
1516
16typedef void ( *psx_dma_read_handler )( running_machine &, UINT32, INT32 );
17typedef void ( *psx_dma_write_handler )( running_machine &, UINT32, INT32 );
17typedef delegate<void (UINT32, INT32)> psx_dma_read_delegate;
18typedef delegate<void (UINT32, INT32)> psx_dma_write_delegate;
1819
1920typedef struct _psx_dma_channel psx_dma_channel;
2021struct _psx_dma_channel
r11505r11506
2324   UINT32 n_blockcontrol;
2425   UINT32 n_channelcontrol;
2526   emu_timer *timer;
26   psx_dma_read_handler fn_read;
27   psx_dma_write_handler fn_write;
27   psx_dma_read_delegate fn_read;
28   bool read_set;
29   psx_dma_write_delegate fn_write;
30   bool write_set;
2831   UINT32 n_ticks;
2932   UINT32 b_running;
3033};
r11505r11506
3437public:
3538   psxdma_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
3639
37   void install_read_handler( int n_channel, psx_dma_read_handler p_fn_dma_read );
38   void install_write_handler( int n_channel, psx_dma_read_handler p_fn_dma_write );
40   void install_read_handler( int n_channel, psx_dma_read_delegate p_fn_dma_read );
41   void install_write_handler( int n_channel, psx_dma_read_delegate p_fn_dma_write );
3942
4043   WRITE32_MEMBER( write );
4144   READ32_MEMBER( read );
src/emu/cpu/psx/psx.c
r11505r11506
6161 *
6262 */
6363
64#define ADDRESS_MAP_MODERN
6465#include "emu.h"
6566#include "debugger.h"
6667#include "psx.h"
6768#include "dma.h"
69#include "mdec.h"
6870#include "includes/psx.h"
6971#include "sound/spu.h"
7072
r11505r11506
15191521}
15201522
15211523// On-board RAM and peripherals
1522static ADDRESS_MAP_START( psxcpu_internal_map, AS_PROGRAM, 32 )
1523   AM_RANGE(0x00800000, 0x1effffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1524static ADDRESS_MAP_START( psxcpu_internal_map, AS_PROGRAM, 32, psxcpu_device )
1525   AM_RANGE(0x00800000, 0x1effffff) AM_READWRITE( berr_r, berr_w )
15241526   AM_RANGE(0x1f800000, 0x1f8003ff) AM_NOP /* scratchpad */
1525   AM_RANGE(0x1f800400, 0x1f800fff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1527   AM_RANGE(0x1f800400, 0x1f800fff) AM_READWRITE( berr_r, berr_w )
15261528   AM_RANGE(0x1f801004, 0x1f80101f) AM_RAM
1527   AM_RANGE(0x1f801020, 0x1f801023) AM_READWRITE(psx_com_delay_r, psx_com_delay_w)
1529   AM_RANGE(0x1f801020, 0x1f801023) AM_READWRITE_LEGACY( psx_com_delay_r, psx_com_delay_w )
15281530   AM_RANGE(0x1f801024, 0x1f80102f) AM_RAM
1529   AM_RANGE(0x1f801040, 0x1f80105f) AM_READWRITE(psx_sio_r, psx_sio_w)
1531   AM_RANGE(0x1f801040, 0x1f80105f) AM_READWRITE_LEGACY( psx_sio_r, psx_sio_w )
15301532   AM_RANGE(0x1f801060, 0x1f80106f) AM_RAM
1531   AM_RANGE(0x1f801070, 0x1f801077) AM_READWRITE(psx_irq_r, psx_irq_w)
1532   AM_RANGE(0x1f801080, 0x1f8010ff) AM_DEVREADWRITE_MODERN("dma", psxdma_device, read, write)
1533   AM_RANGE(0x1f801100, 0x1f80112f) AM_READWRITE(psx_counter_r, psx_counter_w)
1534   AM_RANGE(0x1f801810, 0x1f801817) AM_READWRITE(psx_gpu_r, psx_gpu_w)
1535   AM_RANGE(0x1f801820, 0x1f801827) AM_READWRITE(psx_mdec_r, psx_mdec_w)
1536   AM_RANGE(0x1f801c00, 0x1f801dff) AM_READWRITE16(spu_r, spu_w, 0xffffffff)
1533   AM_RANGE(0x1f801070, 0x1f801077) AM_READWRITE_LEGACY( psx_irq_r, psx_irq_w )
1534   AM_RANGE(0x1f801080, 0x1f8010ff) AM_DEVREADWRITE( "dma", psxdma_device, read, write )
1535   AM_RANGE(0x1f801100, 0x1f80112f) AM_READWRITE_LEGACY( psx_counter_r, psx_counter_w )
1536   AM_RANGE(0x1f801810, 0x1f801817) AM_READWRITE_LEGACY( psx_gpu_r, psx_gpu_w )
1537   AM_RANGE(0x1f801820, 0x1f801827) AM_DEVREADWRITE( "mdec", psxmdec_device, read, write )
1538   AM_RANGE(0x1f801c00, 0x1f801dff) AM_READWRITE16_LEGACY( spu_r, spu_w, 0xffffffff )
15371539   AM_RANGE(0x1f802020, 0x1f802033) AM_RAM /* ?? */
15381540   AM_RANGE(0x1f802040, 0x1f802043) AM_WRITENOP
1539   AM_RANGE(0x20000000, 0x7fffffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1540   AM_RANGE(0x80800000, 0x9effffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1541   AM_RANGE(0xa0800000, 0xbeffffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1542   AM_RANGE(0xc0000000, 0xfffdffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1543   AM_RANGE(0xfffe0130, 0xfffe0133) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, biu_r, biu_w)
1541   AM_RANGE(0x20000000, 0x7fffffff) AM_READWRITE( berr_r, berr_w )
1542   AM_RANGE(0x80800000, 0x9effffff) AM_READWRITE( berr_r, berr_w )
1543   AM_RANGE(0xa0800000, 0xbeffffff) AM_READWRITE( berr_r, berr_w )
1544   AM_RANGE(0xc0000000, 0xfffdffff) AM_READWRITE( berr_r, berr_w )
1545   AM_RANGE(0xfffe0130, 0xfffe0133) AM_READWRITE( biu_r, biu_w )
15441546ADDRESS_MAP_END
15451547
1546static ADDRESS_MAP_START( cxd8661r_internal_map, AS_PROGRAM, 32 )
1547   AM_RANGE(0x01000000, 0x1effffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1548static ADDRESS_MAP_START( cxd8661r_internal_map, AS_PROGRAM, 32, psxcpu_device )
1549   AM_RANGE(0x01000000, 0x1effffff) AM_READWRITE( berr_r, berr_w )
15481550   AM_RANGE(0x1f800000, 0x1f8003ff) AM_NOP /* scratchpad */
1549   AM_RANGE(0x1f800400, 0x1f800fff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1551   AM_RANGE(0x1f800400, 0x1f800fff) AM_READWRITE( berr_r, berr_w )
15501552   AM_RANGE(0x1f801004, 0x1f80101f) AM_RAM
1551   AM_RANGE(0x1f801020, 0x1f801023) AM_READWRITE(psx_com_delay_r, psx_com_delay_w)
1553   AM_RANGE(0x1f801020, 0x1f801023) AM_READWRITE_LEGACY( psx_com_delay_r, psx_com_delay_w )
15521554   AM_RANGE(0x1f801024, 0x1f80102f) AM_RAM
1553   AM_RANGE(0x1f801040, 0x1f80105f) AM_READWRITE(psx_sio_r, psx_sio_w)
1555   AM_RANGE(0x1f801040, 0x1f80105f) AM_READWRITE_LEGACY( psx_sio_r, psx_sio_w )
15541556   AM_RANGE(0x1f801060, 0x1f80106f) AM_RAM
1555   AM_RANGE(0x1f801070, 0x1f801077) AM_READWRITE(psx_irq_r, psx_irq_w)
1556   AM_RANGE(0x1f801080, 0x1f8010ff) AM_DEVREADWRITE_MODERN("dma", psxdma_device, read, write)
1557   AM_RANGE(0x1f801100, 0x1f80112f) AM_READWRITE(psx_counter_r, psx_counter_w)
1558   AM_RANGE(0x1f801810, 0x1f801817) AM_READWRITE(psx_gpu_r, psx_gpu_w)
1559   AM_RANGE(0x1f801820, 0x1f801827) AM_READWRITE(psx_mdec_r, psx_mdec_w)
1560   AM_RANGE(0x1f801c00, 0x1f801dff) AM_READWRITE16(spu_r, spu_w, 0xffffffff)
1557   AM_RANGE(0x1f801070, 0x1f801077) AM_READWRITE_LEGACY( psx_irq_r, psx_irq_w )
1558   AM_RANGE(0x1f801080, 0x1f8010ff) AM_DEVREADWRITE( "dma", psxdma_device, read, write )
1559   AM_RANGE(0x1f801100, 0x1f80112f) AM_READWRITE_LEGACY( psx_counter_r, psx_counter_w )
1560   AM_RANGE(0x1f801810, 0x1f801817) AM_READWRITE_LEGACY( psx_gpu_r, psx_gpu_w )
1561   AM_RANGE(0x1f801820, 0x1f801827) AM_DEVREADWRITE( "mdec", psxmdec_device, read, write )
1562   AM_RANGE(0x1f801c00, 0x1f801dff) AM_READWRITE16_LEGACY( spu_r, spu_w, 0xffffffff )
1563   AM_RANGE(0x1f802020, 0x1f802033) AM_RAM /* ?? */
15611564   AM_RANGE(0x1f802040, 0x1f802043) AM_WRITENOP
1562   AM_RANGE(0x20000000, 0x7fffffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1563   AM_RANGE(0x81000000, 0x9effffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1564   AM_RANGE(0xa1000000, 0xbeffffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1565   AM_RANGE(0xc0000000, 0xfffdffff) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, berr_r, berr_w)
1566   AM_RANGE(0xfffe0130, 0xfffe0133) AM_DEVREADWRITE_MODERN(DEVICE_SELF, psxcpu_device, biu_r, biu_w)
1565   AM_RANGE(0x20000000, 0x7fffffff) AM_READWRITE( berr_r, berr_w )
1566   AM_RANGE(0x81000000, 0x9effffff) AM_READWRITE( berr_r, berr_w )
1567   AM_RANGE(0xa1000000, 0xbeffffff) AM_READWRITE( berr_r, berr_w )
1568   AM_RANGE(0xc0000000, 0xfffdffff) AM_READWRITE( berr_r, berr_w )
1569   AM_RANGE(0xfffe0130, 0xfffe0133) AM_READWRITE( biu_r, biu_w )
15671570ADDRESS_MAP_END
15681571
15691572
r11505r11506
31443147{
31453148}
31463149
3147void psxcpu_device::install_dma_read_handler( device_t &device, int channel, psx_dma_read_handler handler )
3150static psxcpu_device *getcpu( device_t &device, const char *cputag )
31483151{
3149   (downcast<psxdma_device*>(device.subdevice("dma")))->install_read_handler( channel, handler );
3152   if( strcmp( cputag, DEVICE_SELF ) == 0 )
3153   {
3154      return downcast<psxcpu_device *>( &device );
3155   }
3156
3157   return downcast<psxcpu_device *>( device.siblingdevice( cputag ) );
31503158}
31513159
3152void psxcpu_device::install_dma_write_handler( device_t &device, int channel, psx_dma_write_handler handler )
3160void psxcpu_device::install_dma_read_handler( device_t &device, const char *cputag, int channel, psx_dma_read_delegate handler )
31533161{
3154   (downcast<psxdma_device*>(device.subdevice("dma")))->install_write_handler( channel, handler );
3162   psxdma_device *dma = downcast<psxdma_device *>( getcpu( device, cputag )->subdevice("dma") );
3163   dma->install_read_handler( channel, handler );
31553164}
31563165
3166void psxcpu_device::install_dma_write_handler( device_t &device, const char *cputag, int channel, psx_dma_write_delegate handler )
3167{
3168   psxdma_device *dma = downcast<psxdma_device *>( getcpu( device, cputag )->subdevice("dma") );
3169   dma->install_write_handler( channel, handler );
3170}
3171
31573172static MACHINE_CONFIG_FRAGMENT( psx )
31583173   MCFG_DEVICE_ADD("dma", PSX_DMA, 0)
3174
3175   MCFG_DEVICE_ADD("mdec", PSX_MDEC, 0)
3176   MCFG_PSX_DMA_CHANNEL_WRITE( DEVICE_SELF, 0, psx_dma_write_delegate( FUNC( psxmdec_device::dma_write ), (psxmdec_device *) device ) )
3177   MCFG_PSX_DMA_CHANNEL_READ( DEVICE_SELF, 1, psx_dma_read_delegate( FUNC( psxmdec_device::dma_read ), (psxmdec_device *) device ) )
31593178MACHINE_CONFIG_END
31603179
31613180//-------------------------------------------------
src/emu/devintrf.c
r11505r11506
452452
453453   // build a fully-qualified name
454454   astring tempstring;
455   return machine().device(siblingtag(tempstring, _tag));
455   return mconfig().devicelist().find((const char *)siblingtag(tempstring, _tag));
456456}
457457
458458
src/emu/sound/spu.c
r11505r11506
30903090
30913091// MAME I/O stuff.  This can get cleaner when machine/psx.c does.
30923092
3093static void spu_dma_read( running_machine &machine, UINT32 n_address, INT32 n_size )
3093static void spu_dma_read( spu_device *spu, UINT32 n_address, INT32 n_size )
30943094{
3095   spu_device *spu = machine.device<spu_device>("spu");
3096   UINT8 *psxram = (UINT8 *)memory_get_shared(machine, "share1");
3095   UINT8 *psxram = (UINT8 *)memory_get_shared(spu->machine(), "share1");
30973096
30983097   spu->start_dma(psxram + n_address, false, n_size*4);
30993098}
31003099
3101static void spu_dma_write( running_machine &machine, UINT32 n_address, INT32 n_size )
3100static void spu_dma_write( spu_device *spu, UINT32 n_address, INT32 n_size )
31023101{
3103   spu_device *spu = machine.device<spu_device>("spu");
3104   UINT8 *psxram = (UINT8 *)memory_get_shared(machine, "share1");
3102   UINT8 *psxram = (UINT8 *)memory_get_shared(spu->machine(), "share1");
31053103
31063104//  printf("SPU DMA write from %x, size %x\n", n_address, n_size);
31073105
r11505r11506
31193117
31203118   if (!spu->installed_dma_hooks)
31213119   {
3122      psx_dma_install_read_handler(space->machine(), 4, spu_dma_read);
3123      psx_dma_install_write_handler(space->machine(), 4, spu_dma_write);
3120      psx_dma_install_read_handler( space->machine(), 4, psx_dma_read_delegate( FUNC( spu_dma_read ), spu ) );
3121      psx_dma_install_write_handler( space->machine(), 4, psx_dma_write_delegate( FUNC( spu_dma_write ), spu ) );
31243122      spu->installed_dma_hooks = true;
31253123   }
31263124
r11505r11506
31383136
31393137   if (!spu->installed_dma_hooks)
31403138   {
3141      psx_dma_install_read_handler(space->machine(), 4, spu_dma_read);
3142      psx_dma_install_write_handler(space->machine(), 4, spu_dma_write);
3139      psx_dma_install_read_handler( space->machine(), 4, psx_dma_read_delegate( FUNC( spu_dma_read ), spu ) );
3140      psx_dma_install_write_handler( space->machine(), 4, psx_dma_write_delegate( FUNC( spu_dma_write ), spu ) );
31433141      spu->installed_dma_hooks = true;
31443142   }
31453143
src/mess/machine/psxcard.c
r0r11506
1/*
2    psxcard.c - Sony PlayStation memory card device
3
4    by pSXAuthor
5    MESS conversion by R. Belmont
6*/
7
8#include "emu.h"
9#include "psxcard.h"
10#include "includes/psx.h"
11
12//
13//
14//
15
16//#define debug_card
17
18//
19//
20//
21
22const device_type PSXCARD = &device_creator<psxcard_device>;
23
24enum transfer_states
25{
26   state_illegal=0,
27   state_command,
28   state_cmdack_1,
29   state_cmdack_2,
30   state_addr_hi,
31   state_addr_lo,
32   state_read,
33   state_write,
34   state_writeack_1,
35   state_writeack_2,
36   state_writechk
37};
38
39psxcard_device::psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
40    : device_t(mconfig, PSXCARD, "Sony PSX Memory Card", tag, owner, clock)
41{
42}
43
44void psxcard_device::device_start()
45{
46   cache=new unsigned char [128*1024];
47
48   memset(cache, 0, 128*1024);
49
50   // save state registrations
51/*  save_item(NAME(pkt));
52    save_item(NAME(pkt_ptr));
53    save_item(NAME(pkt_sz));
54    save_item(NAME(pkt));
55    save_item(NAME(cache));
56    save_item(NAME(addr));
57    save_item(NAME(state));*/
58}
59
60void psxcard_device::device_reset()
61{
62   state = state_illegal;
63   addr = 0;
64}
65
66//
67//
68//
69
70bool psxcard_device::transfer(UINT8 to, UINT8 *from)
71{
72   bool ret=true;
73
74   switch (state)
75   {
76      case state_illegal:
77         if (to == 0x81)
78         {
79//              printf("CARD: begin\n");
80            state = state_command;
81            *from = 0x00;
82         }
83         else
84         {
85            ret = false;
86         }
87         break;
88
89      case state_command:
90         cmd=to;
91         *from=0x00;
92         state=state_cmdack_1;
93         break;
94
95      case state_cmdack_1:
96         //assert(to==0);
97         *from=0x5a;
98         state=state_cmdack_2;
99         break;
100
101      case state_cmdack_2:
102         //assert(to==0);
103         *from=0x5d;
104         state=state_addr_hi;
105         break;
106
107      case state_addr_hi:
108         addr=(to<<8);
109//          printf("addr_hi: %02x, addr = %x\n", to, addr);
110         *from=0;
111         state=state_addr_lo;
112         break;
113
114      case state_addr_lo:
115         addr|=(to&0xff);
116//          printf("addr_lo: %02x, addr = %x, cmd = %x\n", to, addr, cmd);
117         *from=(addr>>8);
118
119         switch (cmd)
120         {
121            case 'R':   // 0x52
122            {
123               pkt[0]=0x5c;
124               pkt[1]=0x5d;
125               pkt[2]=(addr>>8);
126               pkt[3]=(addr&0xff);
127               read_card(addr,&pkt[4]);
128               pkt[4+128]=checksum_data(&pkt[2],128+2);
129               pkt[5+128]=0x47;
130               pkt_sz=6+128;
131               pkt_ptr=0;
132               state=state_read;
133               break;
134            }
135            case 'W':   // 0x57
136            {
137               pkt[0]=addr>>8;
138               pkt[1]=addr&0xff;
139               pkt_sz=129+2;
140               pkt_ptr=2;
141               state=state_write;
142               break;
143            }
144            default:
145               state=state_illegal;
146               break;
147         }
148         break;
149
150      case state_read:
151         //assert(to==0);
152//          printf("state_read: pkt_ptr = %d, pkt_sz = %d\n", pkt_ptr, pkt_sz);
153         *from=pkt[pkt_ptr++];
154         if (pkt_ptr==pkt_sz)
155         {
156            #ifdef debug_card
157               printf("card: read finished\n");
158            #endif
159
160            state=state_illegal;
161            ret=false;
162         }
163         break;
164
165      case state_write:
166         *from=(pkt_ptr==0)?(addr&0xff):pkt[pkt_ptr-1];
167         pkt[pkt_ptr++]=to;
168         if (pkt_ptr==pkt_sz)
169            state=state_writeack_1;
170         break;
171
172      case state_writeack_1:
173         *from=0x5c;
174         state=state_writeack_2;
175         break;
176
177      case state_writeack_2:
178         *from=0x5d;
179         state=state_writechk;
180         break;
181
182      case state_writechk:
183      {
184         unsigned char chk=checksum_data(pkt,128+2);
185         if (chk==pkt[128+2])
186         {
187            #ifdef debug_card
188               printf("card: write ok\n");
189            #endif
190
191            write_card(addr,pkt+2);
192
193            *from='G';
194         } else
195         {
196            #ifdef debug_card
197               printf("card: write fail\n");
198            #endif
199
200            *from='N';
201         }
202         state=state_illegal;
203         ret=false;
204         break;
205      }
206
207      default: /*assert(0);*/ ret=false; break;
208   }
209
210   #ifdef debug_card
211//      printf("card: transfer to=%02x from=%02x ret=%c\n",to,*from,ret ? 'T' : 'F');
212   #endif
213
214   return ret;
215}
216
217void psxcard_device::read_card(const unsigned short addr, unsigned char *buf)
218{
219   #ifdef debug_card
220      printf("card: read block %d\n",addr);
221   #endif
222
223   if (addr<1024)
224   {
225      memcpy(buf,cache+(addr*128),128);
226   } else
227   {
228      memset(buf,0,128);
229   }
230}
231
232//
233//
234//
235
236void psxcard_device::write_card(const unsigned short addr, unsigned char *buf)
237{
238   #ifdef debug_card
239      printf("card: write block %d\n",addr);
240   #endif
241
242   if (addr<1024)
243   {
244      memcpy(cache+(addr*128),buf,128);
245   }
246}
247
248//
249//
250//
251
252unsigned char psxcard_device::checksum_data(const unsigned char *buf, const unsigned int sz)
253{
254   unsigned char chk=*buf++;
255   int left=sz;
256   while (--left) chk^=*buf++;
257   return chk;
258}
src/mess/machine/psxcard.h
r0r11506
1#pragma once
2
3#ifndef _PSXCARD_
4#define _PSXCARD_
5
6#include "emu.h"
7
8#define MCFG_PSXCARD_ADD(_tag) \
9    MCFG_DEVICE_ADD(_tag, PSXCARD, 0)
10
11struct psxcard_interface
12{
13};
14
15class psxcard_device : public device_t,
16                       public psxcard_interface
17{
18public:
19    psxcard_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
20
21private:
22   unsigned char pkt[0x8b], pkt_ptr, pkt_sz, cmd, *cache;
23   unsigned short addr;
24   int state;
25
26   void read_card(const unsigned short addr, unsigned char *buf);
27   void write_card(const unsigned short addr, unsigned char *buf);
28   unsigned char checksum_data(const unsigned char *buf, const unsigned int sz);
29
30public:
31   virtual void device_start();
32   virtual void device_reset();
33
34   bool transfer(UINT8 to, UINT8 *from);
35   void mess_io(running_machine *machine, UINT8 n_data);
36private:
37};
38
39// device type definition
40extern const device_type PSXCARD;
41
42#endif
src/mess/machine/psxcd.c
r0r11506
1#include "emu.h"
2#include "includes/psx.h"
3#include "psxcd.h"
4#include "machine/devhelpr.h"
5#include "sound/spu.h"
6#include "debugger.h"
7
8//
9//
10//
11
12//#define debug_cdrom
13//#define debug_cdrom_registers
14//#define skip_reads
15//#define dump_subheader
16//#define disable_xa_prefetch
17//#define disable_cdda_prefetch
18
19//
20//
21//
22
23enum cdrom_events
24{
25   event_cmd_complete=0,
26   event_preread_sector,
27   event_read_sector,
28   event_play_sector
29};
30
31//
32//
33//
34
35enum intr_status
36{
37   intr_nointr=0,
38   intr_dataready,
39   intr_acknowledge,
40   intr_complete,
41   intr_dataend,
42   intr_diskerror
43};
44
45enum mode_flags
46{
47   mode_double_speed=0x80,
48   mode_adpcm=0x40,
49   mode_size=0x20,
50   mode_size2=0x10,
51   mode_size_shift=4,
52   mode_size_mask=(3<<mode_size_shift),
53   mode_channel=0x08,
54   mode_report=0x04,
55   mode_autopause=0x02,
56   mode_cdda=0x01
57};
58
59enum status_f
60{
61   status_playing=0x80,
62   status_seeking=0x40,
63   status_reading=0x20,
64   status_shellopen=0x10,
65   status_seekerror=0x04,
66   status_standby=0x02,
67   status_error=0x01
68};
69
70struct subheader
71{
72   unsigned char file,
73                        channel,
74                        submode,
75                        coding;
76};
77
78enum submode_flags
79{
80   submode_eof=0x80,
81   submode_realtime=0x40,
82   submode_form=0x20,
83   submode_trigger=0x10,
84   submode_data=0x08,
85   submode_audio=0x04,
86   submode_video=0x02,
87   submode_eor=0x01
88};
89
90//
91//
92//
93
94static const unsigned int max_xa_prefetch_distance=32,
95                                       max_cdda_prefetch_distance=32;
96
97//**************************************************************************
98//  DEVICE DEFINITIONS
99//**************************************************************************
100
101const device_type PSXCD = &device_creator<psxcd_device>;
102
103void psxcd_device::static_set_devname(device_t &device, const char *devname)
104{
105   psxcd_device &psxcd = downcast<psxcd_device &>(device);
106   psxcd.m_devname = devname;
107}
108
109psxcd_device::psxcd_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
110   : device_t(mconfig, PSXCD, "PSXCD", tag, owner, clock)
111{
112}
113
114void psxcd_device::device_start()
115{
116   unsigned int sysclk=machine().device<cpu_device>("maincpu")->clock()/2;
117   start_read_delay=(sysclk/60);
118   read_sector_cycles=(sysclk/75);
119   preread_delay=(read_sector_cycles>>2)-500;
120
121   m_sysclock = sysclk;
122
123   secleft = 0;
124   secsize = 2048;
125   res_queue = NULL;
126   cur_res = NULL;
127   open = false;
128   streaming = false;
129   sechead = 0;
130   sectail = 0;
131   secin = 0;
132   secskip = 0;
133   next_read_event = NULL;
134   cbp = cmdbuf;
135   first_open = true;
136
137   status=status_standby;
138   sr=8|1;
139   res=0;
140   ir=0;
141   mode=0;
142
143   driver = NULL;
144
145   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
146   {
147      m_timers[i] = timer_alloc(i, NULL);
148      m_timerinuse[i] = false;
149   }
150}
151
152//
153//
154//
155
156void psxcd_device::device_reset()
157{
158   stop_read();
159   open=false;
160
161   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
162   {
163      m_timerinuse[i] = false;
164   }
165
166   m_cddevice = machine().device(m_devname);
167   if (m_cddevice)
168   {
169      m_cd = cd_get_cdrom_file(m_cddevice);
170
171      if (m_cd)
172      {
173//          printf("psxcd: found disc!\n");
174         driver = open_mess_drv();
175         driver->set_machine(machine());
176         driver->set_cdrom_file(m_cd);
177      }
178      else
179      {
180         driver = NULL;
181//          printf("psxcd: Found device, but no disc\n");
182      }
183   }
184   else
185   {
186      driver = NULL;
187//      printf("psxcd: Device [%s] not found!\n", m_devname);
188   }
189
190}
191
192//
193//
194//
195
196unsigned char psxcd_device::read_byte(const unsigned int addr)
197{
198   unsigned char ret = 0;
199
200   switch (addr&3)
201   {
202      case 0: ret=sr; break;
203      case 1:
204         ret=res;
205         if ((cur_res) && (rdp<cur_res->sz))
206         {
207            res=cur_res->data[rdp++];
208            sr|=(1<<5);
209         } else
210         {
211            if ((cur_res) && (cur_res->res&0x10))
212            {
213               global_free(cur_res);
214               cur_res=NULL;
215            }
216
217            sr&=~(1<<5);
218         }
219         break;
220      case 2: ret=0; break;
221      case 3: ret=ir; break;
222   }
223
224   #ifdef debug_cdrom_registers
225      printf("cdrom: read byte %08x = %02x (PC=%x)\n",addr,ret,cpu_get_pc(machine().device("maincpu")));
226   #endif
227
228   return ret;
229}
230
231//
232//
233//
234
235void psxcd_device::write_byte(const unsigned int addr, const unsigned char byte)
236{
237   #ifdef debug_cdrom_registers
238      printf("cdrom: write byte %08x = %02x (PC=%x)\n",addr,byte,cpu_get_pc(machine().device("maincpu")));
239   #endif
240
241   switch (addr&3)
242   {
243      case 0:
244         cmdmode=byte&1;
245         if (cmdmode==0)
246         {
247            cbp=cmdbuf;
248         } else
249         {
250            if (! cur_res)
251            {
252               if (cur_res) global_free(cur_res);
253
254               if (res_queue)
255               {
256                  #ifdef debug_cdrom_registers
257                     printf("cdrom: nextres\n");
258                  #endif
259
260                  cur_res=res_queue;
261                  res_queue=res_queue->next;
262                  ir=cur_res->res&0xf;
263                  rdp=0;
264
265                  if (cur_res->sz)
266                  {
267                     res=cur_res->data[rdp++];
268                     sr|=(1<<5);
269                  } else
270                  {
271                     sr&=~(1<<5);
272                  }
273               } else
274               {
275                  //ir=0;
276                  cur_res=NULL;
277               }
278            }
279            /*else
280                {
281                    if (rdp>=cur_res->sz)
282                    {
283                        sr&=~(1<<5);
284                    } else
285                    {
286                        sr|=~(1<<5);
287                        res=cur_res->data[rdp++];
288                    }
289                }
290                */
291         }
292         break;
293
294      case 1:
295         if (cmdmode==0)
296         {
297            write_command(byte);
298         }
299         break;
300
301      case 2:
302         if (cmdmode==0)
303         {
304            *cbp++=byte;
305         } else
306         {
307            // ?flush buffer?
308         }
309         break;
310
311      case 3:
312         if (byte==0x07)
313         {
314            if (cur_res)
315            {
316               global_free(cur_res);
317               cur_res=NULL;
318               sr&=~(1<<5);
319            }
320            ir=0;
321         }
322         break;
323   }
324}
325
326psxcd_device::command_info psxcd_device::cmd_table[num_commands]=
327{
328   { &psxcd_device::cdcmd_sync,            "sync"         },   // 00
329   { &psxcd_device::cdcmd_nop,            "nop"            },   // 01
330   { &psxcd_device::cdcmd_setloc,         "setloc"      },   // 02
331   { &psxcd_device::cdcmd_play,            "play"         },   // 03
332   { &psxcd_device::cdcmd_forward,      "forward"      },   // 04
333   { &psxcd_device::cdcmd_backward,      "backward"   },   // 05
334   { &psxcd_device::cdcmd_readn,         "readn"         },   // 06
335   { &psxcd_device::cdcmd_standby,      "standby"      },   // 07
336   { &psxcd_device::cdcmd_stop,            "stop"         },   // 08
337   { &psxcd_device::cdcmd_pause,         "pause"         },   // 09
338   { &psxcd_device::cdcmd_init,            "init"         },   // 0a
339   { &psxcd_device::cdcmd_mute,            "mute"         },   // 0b
340   { &psxcd_device::cdcmd_demute,         "demute"      },   // 0c
341   { &psxcd_device::cdcmd_setfilter,   "setfilter"   },   // 0d
342   { &psxcd_device::cdcmd_setmode,      "setmode"      },   // 0e
343   { &psxcd_device::cdcmd_getparam,      "getparam"   },   // 0f
344   { &psxcd_device::cdcmd_getlocl,      "getlocl"      },   // 10
345   { &psxcd_device::cdcmd_getlocp,      "getlocp"      },   // 11
346   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 12
347   { &psxcd_device::cdcmd_gettn,         "gettn"         },   // 13
348   { &psxcd_device::cdcmd_gettd,         "gettd"         },   // 14
349   { &psxcd_device::cdcmd_seekl,         "seekl"         },   // 15
350   { &psxcd_device::cdcmd_seekp,         "seekp"         },   // 16
351   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 17
352   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 18
353   { &psxcd_device::cdcmd_test,            "test"         },   // 19
354   { &psxcd_device::cdcmd_id,               "id"            },   // 1a
355   { &psxcd_device::cdcmd_reads,         "reads"         },   // 1b
356   { &psxcd_device::cdcmd_reset,         "reset"         },   // 1c
357   { &psxcd_device::cdcmd_illegal,      "illegal"      },   // 1d
358   { &psxcd_device::cdcmd_readtoc,      "readtoc"      },   // 1e
359};
360
361//
362//
363//
364
365void psxcd_device::write_command(const unsigned char byte)
366{
367   assert(byte<num_commands);
368   (this->*cmd_table[byte].func)();
369}
370
371//
372//
373//
374
375void psxcd_device::cdcmd_sync()
376{
377   #ifdef debug_cdrom
378      printf("cdrom: sync\n");
379   #endif
380
381   stop_read();
382   send_result(intr_acknowledge);
383}
384
385static int open_nops=0;
386
387void psxcd_device::cdcmd_nop()
388{
389   #ifdef debug_cdrom
390      printf("cdrom: nop\n");
391   #endif
392
393   //stop_read();
394
395   if ((! open) && (driver))
396   {
397      if (open_nops==0)
398      {
399         open=driver->read_toc();
400      } else
401      {
402         open_nops--;
403      }
404   }
405
406   send_result(intr_complete);
407}
408
409void psxcd_device::cdcmd_setloc()
410{
411   #ifdef debug_cdrom
412      printf("cdrom: setloc %08x:%08x:%08x\n",
413            cmdbuf[0],
414            cmdbuf[1],
415            cmdbuf[2]);
416   #endif
417
418   stop_read();
419
420   unsigned char l[3];
421   l[0]=bcd_to_decimal(cmdbuf[0]);
422   l[1]=bcd_to_decimal(cmdbuf[1]);
423   l[2]=bcd_to_decimal(cmdbuf[2]);
424
425   if ((l[0]>0) || (l[1]>=2))
426   {
427      loc[0]=l[0];
428      loc[1]=l[1];
429      loc[2]=l[2];
430   } else
431   {
432      printf("setloc out of range: %02d:%02d:%02d\n",
433                l[0],l[1],l[2]);
434   }
435
436   send_result(intr_complete);
437}
438
439void psxcd_device::cdcmd_play()
440{
441   #ifdef debug_cdrom
442      printf("cdrom: play %02x %02x %02x => %d\n", loc[0], loc[1], loc[2], msf_to_sector(loc));
443   #endif
444
445   curpos[0]=loc[0];
446   curpos[1]=loc[1];
447   curpos[2]=loc[2];
448
449   if ((curpos[0]==0) &&
450         (curpos[1]==0) &&
451         (curpos[2]==0))
452   {
453      send_result(intr_acknowledge);
454   } else
455   {
456      stop_read();
457      start_play();
458      send_result(intr_acknowledge);
459   }
460}
461
462void psxcd_device::cdcmd_forward()
463{
464   #ifdef debug_cdrom
465      printf("cdrom: forward\n");
466   #endif
467}
468
469void psxcd_device::cdcmd_backward()
470{
471   #ifdef debug_cdrom
472      printf("cdrom: backward\n");
473   #endif
474}
475
476void psxcd_device::cdcmd_readn()
477{
478   if (driver)
479   {
480      #ifdef debug_cdrom
481         printf("cdrom: readn\n");
482      #endif
483
484      curpos[0]=loc[0];
485      curpos[1]=loc[1];
486      curpos[2]=loc[2];
487
488      stop_read();
489      start_read();
490      send_result(intr_complete);
491   } else
492   {
493      printf("read without driver\n");
494      send_result(intr_diskerror);
495   }
496}
497
498void psxcd_device::cdcmd_standby()
499{
500   #ifdef debug_cdrom
501      printf("cdrom: standby\n");
502   #endif
503
504   stop_read();
505   send_result(intr_acknowledge);
506}
507
508void psxcd_device::cdcmd_stop()
509{
510   #ifdef debug_cdrom
511      printf("cdrom: stop\n");
512   #endif
513
514   stop_read();
515   send_result(intr_acknowledge);
516}
517
518void psxcd_device::cdcmd_pause()
519{
520   #ifdef debug_cdrom
521      printf("cdrom: pause\n");
522   #endif
523
524   stop_read();
525
526   send_result(intr_acknowledge);
527}
528
529void psxcd_device::cdcmd_init()
530{
531   #ifdef debug_cdrom
532      printf("cdrom: init\n");
533   #endif
534
535   stop_read();
536   mode=0;
537   sr|=0x10;
538
539   send_result(intr_acknowledge);
540}
541
542void psxcd_device::cdcmd_mute()
543{
544   #ifdef debug_cdrom
545      printf("cdrom: mute\n");
546   #endif
547
548   send_result(intr_acknowledge);
549}
550
551void psxcd_device::cdcmd_demute()
552{
553   #ifdef debug_cdrom
554      printf("cdrom: demute\n");
555   #endif
556
557   send_result(intr_acknowledge);
558}
559
560void psxcd_device::cdcmd_setfilter()
561{
562   #ifdef debug_cdrom
563      printf("cdrom: setfilter %08x,%08x\n",cmdbuf[0],cmdbuf[1]);
564   #endif
565
566   filter_file=cmdbuf[0];
567   filter_channel=cmdbuf[1];
568
569   send_result(intr_acknowledge);
570}
571
572void psxcd_device::cdcmd_setmode()
573{
574   #ifdef debug_cdrom
575      printf("cdrom: setmode %08x\n",cmdbuf[0]);
576   #endif
577
578   mode=cmdbuf[0];
579
580   switch ((mode&mode_size_mask)>>mode_size_shift)
581   {
582      case 1:
583         secsize=2328;
584         secskip=24;
585         break;
586
587      case 2:
588         secsize=2340;
589         secskip=12;
590         break;
591
592      default:
593         secsize=2048;
594         secskip=24;
595         break;
596   }
597
598   send_result(intr_complete);
599}
600
601void psxcd_device::cdcmd_getparam()
602{
603   unsigned char data[6]=
604   {
605      status,
606      mode,
607      filter_file,
608      filter_channel,
609      0,
610      0
611   };
612
613   #ifdef debug_cdrom
614      printf("cdrom: getparam [%02x %02x %02x %02x %02x %02x]\n",
615                           data[0],
616                           data[1],
617                           data[2],
618                           data[3],
619                           data[4],
620                           data[5]);
621   #endif
622
623   send_result(intr_complete,data,6);
624}
625
626#if 0
627static void add_loc(unsigned char *dst, const unsigned char *src1, const unsigned char *src2)
628{
629   int f=src1[2]+src2[2],
630         s=src1[1]+src2[1],
631         m=src1[0]+src2[0];
632   while (f>=75) { s++; f-=75; }
633   while (s>=60) { m++; s-=60; }
634
635   dst[0]=m;
636   dst[1]=s;
637   dst[2]=f;
638}
639#endif
640static void sub_loc(unsigned char *dst, const unsigned char *src1, const unsigned char *src2)
641{
642   int f=src1[2]-src2[2],
643         s=src1[1]-src2[1],
644         m=src1[0]-src2[0];
645   while (f<0) { s--; f+=75; }
646   while (s<0) { m--; s+=60; }
647
648   if (m<0)
649      m=s=f=0;
650
651   dst[0]=m;
652   dst[1]=s;
653   dst[2]=f;
654}
655
656void psxcd_device::cdcmd_getlocl()
657{
658   #ifdef debug_cdrom
659      printf("cdrom: getlocl\n");
660   #endif
661
662   #ifdef debug_cdrom
663      printf("cdrom: getlocl [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
664                           lastsechdr[0],
665                           lastsechdr[1],
666                           lastsechdr[2],
667                           lastsechdr[3],
668                           lastsechdr[4],
669                           lastsechdr[5],
670                           lastsechdr[6],
671                           lastsechdr[7]);
672   #endif
673
674   send_result(intr_complete,lastsechdr,8);
675}
676
677void psxcd_device::cdcmd_getlocp()
678{
679   unsigned char tloc[3],
680                        twosec[3]={ 0,2,0 };
681   sub_loc(tloc,loc,twosec);
682
683   unsigned char data[8]=
684   {
685      0x01,                     // track
686      0x01,                     // index
687      decimal_to_bcd(tloc[0]),   // min
688      decimal_to_bcd(tloc[1]),   // sec
689      decimal_to_bcd(tloc[2]),   // frame
690      decimal_to_bcd(loc[0]),   // amin
691      decimal_to_bcd(loc[1]),   // asec
692      decimal_to_bcd(loc[2])   // aframe
693   };
694
695   //unsigned char data[8]={ 2,1,0,0xff,0xff,0xff,0xff,0xff };
696
697   #ifdef debug_cdrom
698      printf("cdrom: getlocp [%02x %02x %02x %02x %02x %02x %02x %02x]\n",
699                           data[0],
700                           data[1],
701                           data[2],
702                           data[3],
703                           data[4],
704                           data[5],
705                           data[6],
706                           data[7]);
707   #endif
708
709   send_result(intr_complete,data,8);
710}
711
712void psxcd_device::cdcmd_illegal()
713{
714   assert(0);
715}
716
717void psxcd_device::cdcmd_gettn()
718{
719   #ifdef debug_cdrom
720      printf("cdrom: gettn\n");
721   #endif
722
723   assert(driver);
724
725   unsigned char data[3]=
726   {
727      status,
728      decimal_to_bcd(driver->get_first_track()),
729      decimal_to_bcd(driver->get_num_tracks())
730   };
731
732   //stop_read();
733   send_result(intr_acknowledge,data,3);
734}
735
736void psxcd_device::cdcmd_gettd()
737{
738   unsigned char addr[3];
739   driver->get_track_address(bcd_to_decimal(cmdbuf[0]), addr);
740
741   unsigned char data[3]=
742   {
743      status,
744      decimal_to_bcd(addr[0]),
745      decimal_to_bcd(addr[1])
746   };
747
748   #ifdef debug_cdrom
749      printf("cdrom: gettd %02x [%02x %02x %02x]\n",
750                                     cmdbuf[0],
751                                     data[0],
752                                     data[1],
753                                     data[2]);
754   #endif
755
756   //stop_read();
757   send_result(intr_acknowledge,data,3);
758}
759
760void psxcd_device::cdcmd_seekl()
761{
762   #ifdef debug_cdrom
763      printf("cdrom: seekl [%02d:%02d:%02d]\n",loc[0],loc[1],loc[2]);
764   #endif
765
766   curpos[0]=loc[0];
767   curpos[1]=loc[1];
768   curpos[2]=loc[2];
769
770   send_result(intr_acknowledge);
771}
772
773void psxcd_device::cdcmd_seekp()
774{
775   #ifdef debug_cdrom
776      printf("cdrom: seekp\n");
777   #endif
778
779   curpos[0]=loc[0];
780   curpos[1]=loc[1];
781   curpos[2]=loc[2];
782
783   send_result(intr_acknowledge);
784}
785
786void psxcd_device::cdcmd_test()
787{
788   #ifdef debug_cdrom
789      printf("cdrom: test %08x\n",cmdbuf[0]);
790   #endif
791
792   static unsigned char data[4]=
793   {
794      0x95,
795      0x07,
796      0x06,
797      0xff
798   };
799
800   send_result(intr_complete,data,4);
801}
802
803void psxcd_device::cdcmd_id()
804{
805   #ifdef debug_cdrom
806      printf("cdrom: id\n");
807   #endif
808
809   if (! open)
810   {
811      if (driver)
812         open=driver->read_toc();
813   }
814
815   if (open)
816   {
817      static unsigned char gamedata[8] = { 0x08, 0x00, 0x00, 0x00, 'S', 'C', 'E', 'A' };
818      static unsigned char audiodata[8] = { 0x08, 0x90, 0x00, 0x00, 'S', 'C', 'E', 'A' };   // drops into the audio CD player.  08 80 goes to the menu.
819
820      if (cdrom_get_track_type(m_cd, 0) == CD_TRACK_AUDIO)
821      {
822         send_result(intr_acknowledge,audiodata,8);
823      }
824      else
825      {
826         send_result(intr_acknowledge,gamedata,8);
827      }
828   } else
829   {
830      status=status_error|status_shellopen;
831      send_result(intr_diskerror);
832   }
833}
834
835void psxcd_device::cdcmd_reads()
836{
837   curpos[0]=loc[0];
838   curpos[1]=loc[1];
839   curpos[2]=loc[2];
840
841   #ifdef skip_reads
842      #ifdef debug_cdrom
843         log("cdrom: reads [SKIPPING - RETURN COMPLETE]\n");
844      #endif
845
846      send_result(intr_complete);
847   #else
848      #ifdef debug_cdrom
849         printf("cdrom: reads\n");
850      #endif
851
852      stop_read();
853      start_streaming();
854   #endif
855}
856
857void psxcd_device::cdcmd_reset()
858{
859   #ifdef debug_cdrom
860      printf("cdrom: reset\n");
861   #endif
862}
863
864void psxcd_device::cdcmd_readtoc()
865{
866   #ifdef debug_cdrom
867      printf("cdrom: readtoc\n");
868   #endif
869
870   static unsigned char data[4]=
871   {
872      0x00,
873      0x00,
874      0xff,
875      0xfe
876   };
877
878   send_result(intr_complete|0x10,data,2);
879}
880
881//
882//
883//
884
885void psxcd_device::cmd_complete(command_result *res)
886{
887   bool doint=((res->res&0x10)==0);
888   #ifdef debug_cdrom
889   command_result *rf;
890   #endif
891
892   if (doint)
893   {
894      psx_irq_set(machine(), 0x0004);
895   }
896
897   add_result(res);
898
899   #ifdef debug_cdrom
900      if (doint)
901      {
902         printf("cdrom: irq [");
903         for (rf=res_queue; ((rf) && (rf->next)); rf=rf->next);
904            printf("%d ",rf->res);
905         printf("]\n");
906      }
907   #endif
908}
909
910//
911//
912//
913
914void psxcd_device::add_result(command_result *res)
915{
916   command_result *rf;
917
918   if (res_queue)
919   {
920      for (rf=res_queue; rf->next; rf=rf->next);
921      rf->next=res;
922   } else
923   {
924      res_queue=res;
925   }
926
927   res->next=NULL;
928}
929
930//
931//
932//
933
934event *psxcd_device::send_result(const unsigned int res,
935                                          const unsigned char *data,
936                                          const unsigned int sz,
937                                             const unsigned int delay)
938{
939   // Update shell open status
940
941   if (! open)
942   {
943      status=status_error|status_shellopen;
944   }
945   else
946   {
947      if (status&status_shellopen)
948         status=status_standby;
949   }
950
951   // Prepare event
952
953   event *ev=new event;
954   ev->t=delay;
955   ev->type=event_cmd_complete;
956
957   command_result *cr=(command_result *)ev->data;
958
959   cr->res=res;
960   if (sz)
961   {
962      assert(sz<sizeof(cr->data));
963      memcpy(cr->data,data,sz);
964      cr->sz=sz;
965   } else
966   {
967      cr->data[0]=status;
968      cr->sz=1;
969   }
970
971   // Avoid returning results after sector read results -
972   // delay the sector read slightly if necessary
973
974   UINT64 systime = machine().device<cpu_device>("maincpu")->total_cycles();
975   if ((next_read_event) && ((systime+ev->t)>(next_sector_t)))
976   {
977      UINT32 hz = m_sysclock / (delay + 2000);
978      next_read_event->timer->adjust(attotime::from_hz(hz), 0, attotime::never);
979
980      for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
981      {
982         if (m_eventfortimer[i] == next_read_event)
983         {
984            printf("Adjusting timer %d to %d hz]n", i, hz);
985         }
986      }
987   }
988
989   add_system_event(ev);
990   return ev;
991}
992
993//
994//
995//
996
997void psxcd_device::start_dma(UINT8 *mainram, UINT32 size)
998{
999   if ((int)size>secleft)
1000   {
1001      printf("cdrom: dma past end of sector (secleft=%d sz=%d)\n",secleft,size);
1002   }
1003
1004//  printf("cdrom: start dma %d bytes, %d remaining, secptr %p\n", size, secleft, secptr);
1005   #ifdef debug_cdrom
1006   if (size==12)
1007   {
1008      char poo[1024],*pp=poo;
1009      for (unsigned int i=0; i<12; i++)
1010      {
1011         sprintf(pp,"%02x ",secptr[i]);
1012         pp+=3;
1013      }
1014      printf("cdrom: data=%s\n",poo);
1015   }
1016   #endif
1017
1018   memcpy(mainram, secptr, size);
1019   memset(secptr, 0xff, size);
1020
1021   secptr += size;
1022   secleft -= size;
1023
1024   if (secleft<0) secleft=0;
1025   if (secleft==0) sr&=~0x40;
1026}
1027
1028//
1029//
1030//
1031
1032void psxcd_device::prefetch_next_sector()
1033{
1034   unsigned int pos=msf_to_sector(curpos);
1035   assert(driver);
1036   driver->prefetch_sector(pos);
1037}
1038
1039//
1040//
1041//
1042
1043bool psxcd_device::read_next_sector(const bool block)
1044{
1045   unsigned int pos=msf_to_sector(curpos);
1046   unsigned char *buf=&secbuf[sechead*raw_sector_size];
1047   assert(driver);
1048   assert(secin<sector_buffer_size);
1049
1050//  printf("read_next_sector: sec %d, sechead %d, raw_sector_size %d\n", pos, sechead, raw_sector_size);
1051   if (driver->read_sector(pos, buf, block))
1052   {
1053//      printf("buf contents = %02x %02x | %02x %02x\n", buf[0], buf[1], buf[0x20], buf[0x21]);
1054
1055      sechead=(sechead+1)&(sector_buffer_size-1);
1056      secin++;
1057
1058      memcpy(lastsechdr,&secbuf[raw_sector_size*sectail]+12,8);
1059
1060      return true;
1061   } else
1062   {
1063      return false;
1064   }
1065}
1066
1067//
1068//
1069//
1070
1071void psxcd_device::read_sector()
1072{
1073   next_read_event=NULL;
1074
1075   if (status & status_reading)
1076   {
1077      bool isend=false;
1078
1079      if (read_next_sector(false))
1080      {
1081         unsigned char *rawsec;
1082
1083         if ((mode&mode_adpcm) && (streaming))
1084         {
1085            rawsec=&secbuf[raw_sector_size*sectail];
1086            secptr=rawsec+24;
1087            secleft=2048;
1088         }
1089         else
1090         {
1091            rawsec=&secbuf[raw_sector_size*sectail];
1092            secptr=rawsec+secskip;
1093            secleft=secsize;
1094         }
1095
1096         secin--;
1097         sectail=(sectail+1)&(sector_buffer_size-1);
1098
1099         //
1100
1101         bool isxa=false;
1102
1103         subheader *sub=(subheader *)(rawsec+16);
1104
1105         #ifdef dump_subheader
1106            printf("cdrom: subheader file=%02x chan=%02x submode=%02x coding=%02x [%02x%02x%02x%02x]\n",
1107                  sub->file,
1108                  sub->channel,
1109                  sub->submode,
1110                  sub->coding,
1111                  rawsec[0xc],
1112                  rawsec[0xd],
1113                  rawsec[0xe],
1114                  rawsec[0xf]);
1115         #endif
1116
1117         status&=~status_playing;
1118         isxa=((mode&mode_adpcm) && (sub->submode&submode_audio));
1119
1120         if (((mode&mode_channel)==0) ||
1121               ((sub->file==filter_file) && (sub->channel==filter_channel)))
1122         {
1123            if (isxa)
1124            {
1125               if (sub->submode&submode_eof)
1126               {
1127                  isend=true;
1128                  //printf("end of file\n");
1129               }
1130
1131               #ifndef disable_xa_prefetch
1132                  unsigned int cursec=msf_to_sector(curpos);
1133
1134                  if (xa_prefetch_sector==-1)
1135                  {
1136                     xa_prefetch_sector=cursec;
1137                     machine().device<spu_device>("spu")->flush_xa();
1138                  }
1139
1140                  unsigned char *xaptr;
1141                  unsigned int xasecsz;
1142
1143                  while (1)
1144                  {
1145                     int dist=xa_prefetch_sector-cursec;
1146                     if (dist>max_xa_prefetch_distance) break;
1147
1148                     xaptr=driver->get_prefetch_sector(xa_prefetch_sector,&xasecsz);
1149                     if (! xaptr) break;
1150
1151                     switch (xasecsz)
1152                     {
1153                        case 2336:
1154                           break;
1155
1156                        default:
1157                           xaptr+=16;
1158                           break;
1159                     }
1160
1161                     subheader *xasub=(subheader *)xaptr;
1162
1163                     if ((xasub->submode&submode_audio) &&
1164                           (((mode&mode_channel)==0) ||
1165                            ((xasub->file==sub->file) &&
1166                              (xasub->channel==sub->channel))))
1167                     {
1168                        if (! machine().device<spu_device>("spu")->play_xa(xa_prefetch_sector,xaptr))
1169                           break;
1170                     }
1171                     xa_prefetch_sector++;
1172                  }
1173               #else
1174                  machine().device<spu_device>("spu")->play_xa(0,rawsec+16);
1175               #endif
1176
1177               status|=status_playing;
1178            }
1179         }
1180
1181         if ((mode&mode_autopause)==0)
1182            isend=false;
1183
1184         //
1185
1186         curpos[2]++;
1187         if (curpos[2]==75)
1188         {
1189            curpos[2]=0;
1190            curpos[1]++;
1191            if (curpos[1]==60)
1192            {
1193               curpos[1]=0;
1194               curpos[0]++;
1195            }
1196         }
1197
1198         loc[0]=curpos[0];
1199         loc[1]=curpos[1];
1200         loc[2]=curpos[2];
1201
1202         //
1203
1204         command_result *res=new command_result;
1205         res->res=isend?intr_dataend:intr_dataready;
1206         res->data[0]=status;
1207         res->sz=1;
1208
1209         sr|=0x40;
1210
1211         if ((streaming) && (isxa))
1212         {
1213            global_free(res);
1214            res=NULL;
1215         }
1216
1217         if (res)
1218         {
1219            #ifdef debug_cdrom
1220               printf("cdrom:: data ready\n");
1221            #endif
1222
1223            cmd_complete(res);
1224         }
1225      }
1226
1227      if (! isend)
1228      {
1229         unsigned int cyc=read_sector_cycles;
1230         if (mode&mode_double_speed) cyc>>=1;
1231
1232         next_sector_t+=cyc;
1233
1234         event *ev=new event;
1235         ev->t=cyc;
1236         ev->type=event_read_sector;
1237         next_read_event=ev;
1238         add_system_event(ev);
1239
1240         //read_next_sector();
1241      } else
1242      {
1243         printf("autopause xa\n");
1244         stop_read();
1245      }
1246   }
1247}
1248
1249//
1250//
1251//
1252
1253bool psxcd_device::play_cdda_sector(const unsigned int sector,
1254                                                 unsigned char *rawsec)
1255{
1256   bool isdata=true;
1257
1258   if (rawsec[0]!=0)
1259   {
1260      isdata=false;
1261   } else
1262   {
1263      for (int i=0; i<10; i++)
1264         if (rawsec[i+1]!=0xff)
1265         {
1266            isdata=false;
1267            break;
1268         }
1269   }
1270
1271   if (! isdata)
1272   {
1273      return machine().device<spu_device>("spu")->play_cdda(sector,rawsec);
1274   } else
1275   {
1276      return true;
1277   }
1278}
1279
1280//
1281//
1282//
1283
1284void psxcd_device::play_sector()
1285{
1286   next_read_event=NULL;
1287
1288   if (status&status_playing)
1289   {
1290      #ifdef disable_cdda_prefetch
1291      unsigned char *rawsec=&secbuf[raw_sector_size*sectail];
1292      #endif
1293      secin--;
1294      sectail=(sectail+1)&(sector_buffer_size-1);
1295
1296      //
1297
1298      #ifndef disable_cdda_prefetch
1299         unsigned int cursec=msf_to_sector(curpos);
1300
1301         if (cdda_prefetch_sector==-1)
1302         {
1303            cdda_prefetch_sector=cursec;
1304            machine().device<spu_device>("spu")->flush_cdda();
1305         }
1306
1307         unsigned char *cddaptr;
1308//          bool full=false;
1309         unsigned int cddasecsz;
1310
1311         while (1)
1312         {
1313            int dist=cdda_prefetch_sector-cursec;
1314            if (dist>max_cdda_prefetch_distance) break;
1315
1316            cddaptr=driver->get_prefetch_sector(cdda_prefetch_sector,&cddasecsz);
1317            if (! cddaptr) break;
1318
1319            if (! play_cdda_sector(cdda_prefetch_sector,cddaptr))
1320               break;
1321
1322            cdda_prefetch_sector++;
1323         }
1324      #else
1325         play_cdda_sector(0,rawsec);
1326      #endif
1327
1328      //
1329
1330      curpos[2]++;
1331      if (curpos[2]==75)
1332      {
1333         curpos[2]=0;
1334         curpos[1]++;
1335         if (curpos[1]==60)
1336         {
1337            curpos[1]=0;
1338            curpos[0]++;
1339         }
1340      }
1341
1342      loc[0]=curpos[0];
1343      loc[1]=curpos[1];
1344      loc[2]=curpos[2];
1345
1346      //
1347
1348      if (mode&mode_autopause)
1349      {
1350         if (msf_to_sector(loc)>=autopause_sector)
1351         {
1352            printf("autopause cdda\n");
1353
1354            command_result *res=new command_result;
1355            res->res=intr_dataend;
1356            res->data[0]=status_standby;
1357            res->sz=1;
1358            cmd_complete(res);
1359            stop_read();
1360            return;
1361         }
1362      }
1363
1364      if (mode&mode_report)
1365      {
1366         command_result *res=new command_result;
1367         res->res=intr_complete;   //dataready;
1368
1369         res->data[0]=status_playing|status_standby;
1370         res->data[1]=0x01;
1371         res->data[2]=0x80;
1372         res->data[3]=decimal_to_bcd(loc[0]);
1373         res->data[4]=decimal_to_bcd(loc[1])|0x80;
1374         res->data[5]=decimal_to_bcd(loc[2]);
1375
1376         res->sz=8;
1377
1378         cmd_complete(res);
1379      }
1380
1381      unsigned int cyc=read_sector_cycles;
1382
1383      event *ev=new event;
1384      ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1385      ev->type=event_play_sector;
1386
1387      next_sector_t+=cyc;
1388
1389      next_read_event=ev;
1390      add_system_event(ev);
1391
1392      read_next_sector();
1393   }
1394}
1395
1396
1397//
1398//
1399//
1400
1401void psxcd_device::preread_sector()
1402{
1403   next_read_event=NULL;
1404
1405   //
1406
1407   event *ev=new event;
1408
1409   unsigned int pos=msf_to_sector(curpos);
1410   unsigned char *buf=&secbuf[sechead*raw_sector_size];
1411   if (! driver->read_sector(pos,buf,false))
1412   {
1413      ev->t=(m_sysclock/60);
1414      ev->type=event_preread_sector;
1415
1416      unsigned int cyc=read_sector_cycles;
1417      if (mode&mode_double_speed) cyc>>=1;
1418      next_sector_t=ev->t+(cyc-preread_delay)+machine().device<cpu_device>("maincpu")->total_cycles();
1419   } else
1420   {
1421      memcpy(lastsechdr,buf+12,8);
1422
1423      //
1424
1425      command_result *res=new command_result;
1426      res->res=intr_complete;
1427      res->data[0]=status;
1428      res->sz=1;
1429
1430      #ifdef debug_cdrom
1431         printf("cdrom: read acknowledge\n");
1432      #endif
1433
1434      cmd_complete(res);
1435
1436      //
1437
1438      ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1439      ev->type=event_read_sector;
1440
1441      //read_next_sector();
1442   }
1443
1444   next_read_event=ev;
1445   add_system_event(ev);
1446}
1447
1448//
1449//
1450//
1451
1452void psxcd_device::start_read()
1453{
1454   #ifdef debug_cdrom
1455      printf("cdrom: start read\n");
1456   #endif
1457
1458   assert((status&(status_reading|status_playing))==0);
1459
1460   status|=status_reading;
1461
1462   secin=sechead=sectail=0;
1463   xa_prefetch_sector=-1;
1464
1465   unsigned int cyc=read_sector_cycles;
1466   if (mode&mode_double_speed) cyc>>=1;
1467
1468   INT64 systime=machine().device<cpu_device>("maincpu")->total_cycles();
1469
1470   systime+=start_read_delay;
1471
1472   event *ev=new event;
1473   ev->t=start_read_delay+preread_delay;
1474   ev->type=event_preread_sector;
1475
1476   next_sector_t=systime+cyc;
1477   next_read_event=ev;
1478   add_system_event(ev);
1479
1480   prefetch_next_sector();
1481}
1482
1483//
1484//
1485//
1486
1487void psxcd_device::start_streaming()
1488{
1489   assert(! streaming);
1490
1491   streaming=true;
1492   start_read();
1493}
1494
1495//
1496//
1497//
1498
1499void psxcd_device::start_play()
1500{
1501   #ifdef debug_cdrom
1502      printf("cdrom: start play\n");
1503   #endif
1504
1505   status|=status_playing;
1506
1507   secin=sechead=sectail=0;
1508   cdda_prefetch_sector=-1;
1509
1510   if (mode&mode_autopause)
1511   {
1512      unsigned int pos=msf_to_sector(curpos);
1513      driver->find_track(pos+150,NULL,&autopause_sector);
1514//      printf("pos=%d auto=%d\n",pos,autopause_sector);
1515   }
1516
1517   unsigned int cyc=read_sector_cycles;
1518
1519   next_sector_t=machine().device<cpu_device>("maincpu")->total_cycles()+cyc;
1520
1521   event *ev=new event;
1522   ev->t=next_sector_t - machine().device<cpu_device>("maincpu")->total_cycles();
1523   ev->type=event_play_sector;
1524
1525   next_sector_t+=cyc;
1526
1527   next_read_event=ev;
1528   add_system_event(ev);
1529
1530   read_next_sector();
1531}
1532
1533//
1534//
1535//
1536
1537void psxcd_device::stop_read()
1538{
1539   #ifdef debug_cdrom
1540      if (status&status_reading)
1541         printf("cdrom: stop read\n");
1542   #endif
1543   status&=~(status_reading|status_playing);
1544   streaming=false;
1545
1546   if (next_read_event)
1547   {
1548      next_read_event->timer->adjust(attotime::never, 0, attotime::never);
1549      for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
1550      {
1551         if (m_timers[i] == next_read_event->timer)
1552         {
1553            m_timerinuse[i] = false;
1554            m_eventfortimer[i] = false;
1555            break;
1556         }
1557      }
1558      global_free(next_read_event);
1559      next_read_event=NULL;
1560   }
1561
1562   unsigned int sector=msf_to_sector(curpos);
1563   machine().device<spu_device>("spu")->flush_xa(sector);
1564   machine().device<spu_device>("spu")->flush_cdda(sector);
1565}
1566
1567//
1568//
1569//
1570
1571void psxcd_device::set_driver(cdrom_driver *d)
1572{
1573   char err[1024];
1574
1575   if (d)
1576   {
1577      if (d->is_usable(err,1024))
1578      {
1579         driver=d;
1580         open=false;
1581
1582         if (! first_open)
1583         {
1584            command_result *res=new command_result;
1585            res->res=intr_acknowledge;
1586            res->data[0]=status_standby;
1587            res->sz=1;
1588
1589            cmd_complete(res);
1590
1591            open_nops=10;
1592         }
1593
1594         first_open=false;
1595      }
1596   } else
1597   {
1598      driver=NULL;
1599      open=false;
1600
1601      command_result *res=new command_result;
1602      res->res=intr_acknowledge;
1603      res->data[0]=status_shellopen;
1604      res->sz=1;
1605
1606      cmd_complete(res);
1607   }
1608}
1609
1610//
1611//
1612//
1613void psxcd_device::device_timer(emu_timer &timer, device_timer_id tid, int param, void *ptr)
1614{
1615   event *ev = m_eventfortimer[tid];
1616
1617//  printf("timer %d fired, performing event type %d\n", tid, ev->type);
1618
1619   switch (ev->type)
1620   {
1621      case event_cmd_complete:
1622      {
1623         #ifdef debug_cdrom
1624            printf("cdrom:: event cmd complete\n");
1625         #endif
1626
1627         command_result *res=new command_result;
1628         memcpy(res,ev->data,sizeof(command_result));
1629         cmd_complete(res);
1630         break;
1631      }
1632
1633      case event_preread_sector:
1634         preread_sector();
1635         break;
1636
1637      case event_read_sector:
1638         read_sector();
1639         break;
1640
1641      case event_play_sector:
1642         play_sector();
1643         break;
1644   }
1645
1646   // free the timer
1647//  printf("Freeing timer %d\n", tid);
1648   m_timers[tid]->adjust(attotime::never, 0, attotime::never);
1649   m_timerinuse[tid] = false;
1650   m_eventfortimer[tid] = NULL;
1651
1652   global_free(ev);
1653}
1654
1655void psxcd_device::add_system_event(event *ev)
1656{
1657   emu_timer *timer = NULL;
1658   int tnum = -1;
1659
1660   // allocate a timer for this event
1661   for (int i = 0; i < MAX_PSXCD_TIMERS; i++)
1662   {
1663      if (!m_timerinuse[i])
1664      {
1665         tnum = i;
1666         timer = m_timers[i];
1667         m_timerinuse[i] = true;
1668         m_eventfortimer[i] = ev;
1669         break;
1670      }
1671   }
1672
1673   if (tnum == -1)
1674   {
1675      fatalerror("PSXCD: ran out of timers!\n");
1676   }
1677
1678   // ev->t is in maincpu clock cycles
1679   UINT32 hz = m_sysclock / ev->t;
1680//  printf("add_system_event: event type %d for %d hz (using timer %d)\n", ev->type, hz, tnum);
1681   timer->adjust(attotime::from_hz(hz), tnum, attotime::never);
1682
1683   // back-reference the timer from the event
1684   ev->timer = timer;
1685}
1686
src/mess/machine/psxcd.h
r0r11506
1#ifndef _included_psxcdrom_
2#define _included_psxcdrom_
3
4#include "imagedev/chd_cd.h"
5#include "psxcddrv.h"
6
7#define MAX_PSXCD_TIMERS   (4)
8
9class event;
10
11//
12//
13//
14
15const int num_commands=0x20;
16
17//
18//
19//
20
21#define PSXCD_TAG   "psxcd"
22
23//**************************************************************************
24//  INTERFACE CONFIGURATION MACROS
25//**************************************************************************
26
27#define MCFG_PSXCD_ADD(_devname) \
28    MCFG_DEVICE_ADD(PSXCD_TAG, PSXCD, 0) \
29   MCFG_PSXCD_DEVNAME(_devname)
30
31#define MCFG_PSXCD_DEVNAME(_name) \
32   psxcd_device::static_set_devname(*device, _name); \
33
34struct psxcd_interface
35{
36};
37
38class psxcd_device : public device_t,
39                     public psxcd_interface
40{
41public:
42
43    psxcd_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
44   // inline configuration helpers
45   static void static_set_devname(device_t &device, const char *devname);
46private:
47   struct command_result
48   {
49      unsigned char data[32], sz, res;
50      command_result *next;
51   };
52
53   static const unsigned int sector_buffer_size=16, default_irq_delay=16000;   //480;  //8000; //2000<<2;
54
55   unsigned char sr,res,ir,cmdmode,
56                        cmdbuf[64],*cbp,
57                        mode,
58                        loc[3],
59                        curpos[3],
60                        secbuf[raw_sector_size*sector_buffer_size],
61                        *secptr,
62                        filter_file,
63                        filter_channel,
64                        lastsechdr[8],
65                        status;
66   int rdp,secsize,secleft,secskip,
67         sechead,sectail,
68         secin;
69   command_result *res_queue,
70                         *cur_res;
71   bool open,
72          streaming,
73          first_open;
74   event *next_read_event;
75   INT64 next_sector_t;
76   unsigned int autopause_sector,
77                      xa_prefetch_sector,
78                      cdda_prefetch_sector;
79
80   cdrom_driver *driver;
81
82   unsigned int start_read_delay,
83                      read_sector_cycles,
84                      preread_delay;
85
86   void write_command(const unsigned char byte);
87
88   struct command_info
89   {
90      void (psxcd_device::*func)();
91      const char *name;
92   };
93
94   void cdcmd_sync();
95   void cdcmd_nop();
96   void cdcmd_setloc();
97   void cdcmd_play();
98   void cdcmd_forward();
99   void cdcmd_backward();
100   void cdcmd_readn();
101   void cdcmd_standby();
102   void cdcmd_stop();
103   void cdcmd_pause();
104   void cdcmd_init();
105   void cdcmd_mute();
106   void cdcmd_demute();
107   void cdcmd_setfilter();
108   void cdcmd_setmode();
109   void cdcmd_getparam();
110   void cdcmd_getlocl();
111   void cdcmd_getlocp();
112   void cdcmd_illegal();
113   void cdcmd_gettn();
114   void cdcmd_gettd();
115   void cdcmd_seekl();
116   void cdcmd_seekp();
117   void cdcmd_test();
118   void cdcmd_id();
119   void cdcmd_reads();
120   void cdcmd_reset();
121   void cdcmd_readtoc();
122
123   static command_info cmd_table[num_commands];
124
125   void cmd_complete(command_result *res);
126   void add_result(command_result *res);
127   event *send_result(const unsigned int res,
128                               const unsigned char *data=NULL,
129                               const unsigned int sz=0,
130                               const unsigned int delay=default_irq_delay);
131
132   void start_read();
133   void start_play();
134   void start_streaming();
135   void stop_read();
136   void read_sector();
137   void prefetch_next_sector();
138   bool read_next_sector(const bool block=true);
139   bool play_cdda_sector(const unsigned int sector, unsigned char *rawsec);
140   void play_sector();
141   void preread_sector();
142
143public:
144   void set_driver(cdrom_driver *d);
145   cdrom_driver *get_driver() const { return driver; }
146
147   virtual void device_start();
148   virtual void device_reset();
149   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
150
151   void start_dma(UINT8 *mainram, UINT32 size);
152
153   unsigned char read_byte(const unsigned int addr);
154   void write_byte(const unsigned int addr, const unsigned char byte);
155
156private:
157   emu_timer *m_timer;
158   UINT32 m_sysclock;
159   const char *m_devname;
160   device_t *m_cddevice;
161   cdrom_file   *m_cd;
162   emu_timer *m_timers[MAX_PSXCD_TIMERS];
163   event *m_eventfortimer[MAX_PSXCD_TIMERS];
164   bool m_timerinuse[MAX_PSXCD_TIMERS];
165
166   void add_system_event(event *ev);
167};
168
169
170// miniature version of pSX's event class, which we emulate with device timers
171const int max_event_data=64;
172
173class event
174{
175public:
176   UINT64 t;            // expire time
177   unsigned int type;
178   unsigned char data[max_event_data];
179   emu_timer *timer;
180};
181
182
183// device type definition
184extern const device_type PSXCD;
185
186#endif
src/mess/machine/psxcddrv.c
r0r11506
1#include "emu.h"
2#include "includes/psx.h"
3#include "psxcd.h"
4#include "psxcddrv.h"
5
6//#define debug_mess_driver
7
8//
9//
10//
11
12static const unsigned int   num_pf_sectors=32, num_pf_buffers=16;
13
14//
15//
16//
17
18cdrom_driver::cdrom_driver()
19{
20   pf_head = 0;
21   pf_tail = 0;
22   num_pf = 0;
23   pf_head_sector = 0;
24   pf_status = NULL;
25   native_sector_size = raw_sector_size;
26   last_pf_status = NULL;
27   m_cd = NULL;
28   m_machine = NULL;
29
30//  printf("cdrom_driver base class init, pf_buffer size is %d\n", num_pf_sectors*num_pf_buffers*raw_sector_size);
31
32   pf_buffer=new unsigned char [num_pf_sectors*num_pf_buffers*raw_sector_size];
33
34   timestamp_frequency = 1;
35}
36
37cdrom_driver::~cdrom_driver()
38{
39   global_free(pf_buffer);
40}
41
42//
43//
44//
45
46void cdrom_driver::set_native_sector_size(const unsigned int sz)
47{
48   native_sector_size=sz;
49}
50
51//
52//
53//
54
55void cdrom_driver::cancel_io()
56{
57   if (pf_status)
58   {
59      pf_status->cancel();
60      pf_status->release();
61   }
62
63   pf_status=NULL;
64}
65
66//
67//
68//
69
70void cdrom_driver::prefetch_sector(const unsigned int sec)
71{
72   int numpfsec=num_pf*num_pf_sectors,
73         pfsec=sec-pf_head_sector;
74
75//  printf("prefetch_sector: %d\n", sec);
76
77   if ((pfsec<0) || (pfsec>numpfsec))
78   {
79      // Sector is not in prefetch buffer, abort current prefetch
80
81      if (pf_status)
82      {
83         pf_status->cancel();
84         pf_status->release();
85      }
86
87      // Reset the buffer and begin a new prefetch
88
89      pf_head=0;
90      pf_tail=0;
91      num_pf=1;
92      pf_head_sector=sec;
93
94      last_pf_status=NULL;
95      pf_status=read_sectors(sec, num_pf_sectors, pf_buffer);
96   }
97}
98
99//
100//
101//
102
103bool cdrom_driver::is_prefetch_sector_loaded(const unsigned int sec)
104{
105   int numpfsec=num_pf*num_pf_sectors,
106         pfsec=sec-pf_head_sector;
107
108   if ((pfsec<0) || (pfsec>=numpfsec))
109   {
110      // Requested sector is not in prefetch buffer, begin a new prefetch
111
112      prefetch_sector(sec);
113      pfsec=sec-pf_head_sector;
114      numpfsec=num_pf*num_pf_sectors;
115   }
116
117   int blk=pfsec/num_pf_sectors;
118
119   if (blk>0)
120   {
121      // Discard blocks below the prefetch point
122
123      pf_head=(pf_head+blk)%num_pf_buffers;
124      pf_head_sector+=num_pf_sectors*blk;
125      pfsec-=num_pf_sectors*blk;
126      num_pf-=blk;
127      blk=0;
128   }
129
130   bool comp=((! pf_status) || (pf_status->complete()));
131
132   if (! comp)
133   {
134      INT64 curtime=m_machine->device<cpu_device>("maincpu")->total_cycles();
135
136      if (last_pf_status!=pf_status)
137      {
138         last_pf_status=pf_status;
139         pf_timeout_begin=curtime;
140      }
141
142      INT64 timeout=curtime-pf_timeout_begin;
143      int timeout_sec=(int)(timeout/timestamp_frequency);
144      if (timeout_sec>20)
145      {
146         printf("cdrom: prefetch timed out, trying again...\n");
147         num_pf=0;
148         pf_head_sector=-1;
149         prefetch_sector(sec);
150         return is_prefetch_sector_loaded(sec);
151      }
152   }
153
154   if ((num_pf<num_pf_buffers) && (comp))
155   {
156      // The prefetch buffer is not full and we are not waiting on IO,
157      // prefetch the next block
158
159      pf_tail=(pf_tail+1)%num_pf_buffers;
160      num_pf++;
161
162      int nxtsec=pf_head_sector+((num_pf-1)*num_pf_sectors);
163      unsigned char *ptr=pf_buffer+((pf_tail*num_pf_sectors)*native_sector_size);
164
165      if (pf_status)
166      {
167         pf_status->release();
168         pf_status=last_pf_status=NULL;
169      }
170
171      pf_status=read_sectors(nxtsec, num_pf_sectors, ptr);
172   }
173
174   if (blk==(num_pf-1))
175   {
176      // The sector we want is in the last block in the prefetch buffer
177      // which might still be loading, check if the sector we want is loaded
178#if 0 // we do not load async in MESS
179      INT64 trans=pf_status->bytes_transferred();
180      unsigned int secmod=pfsec%num_pf_sectors,
181                         needbytes=(secmod+1)*native_sector_size;
182
183      if (trans<needbytes)
184      {
185         // The sector is not loaded yet
186
187         return false;
188      } else
189#endif
190      {
191         // The sector is loaded
192
193         return true;
194      }
195   } else
196   {
197      // The sector is not in the last block so it must be loaded
198
199      return true;
200   }
201}
202
203//
204//
205//
206
207unsigned char *cdrom_driver::get_prefetch_sector(const unsigned int sec, unsigned int *sz)
208{
209   int numpfsec=num_pf*num_pf_sectors,
210         pfsec=sec-pf_head_sector;
211   if ((pfsec>=0) && (pfsec<numpfsec))
212   {
213      int   blk=(pf_head+(pfsec/num_pf_sectors))%num_pf_buffers,
214            off=((blk*num_pf_sectors)+(pfsec%num_pf_sectors))*native_sector_size;
215      *sz=native_sector_size;
216      return pf_buffer+off;
217   } else
218   {
219      return NULL;
220   }
221}
222
223//
224//
225//
226
227bool cdrom_driver::read_sector(const unsigned int sec, unsigned char *buf, const bool block)
228{
229   bool loaded=is_prefetch_sector_loaded(sec);
230
231//  printf("read_sector: %d (loaded=%c)\n", sec, loaded ? 'Y' : 'N');
232
233   if ((! loaded) && (block))
234   {
235      pf_status->block_until_complete();
236      loaded=true;
237   }
238
239   if (loaded)
240   {
241      unsigned int secsz=0;
242      unsigned char *ptr=get_prefetch_sector(sec,&secsz);
243      assert(ptr);
244
245//      printf("got sector %d @ %p, size %d = %02x %02x | %02x %02x\n", sec, ptr, secsz, ptr[0], ptr[1], ptr[0x20], ptr[0x21]);
246
247      if (secsz<2352)
248      {
249         // Add sector header
250
251         buf[0]=0;
252         memset(buf+1,0xff,10);
253         buf[11]=0;
254         sector_to_msf(sec,buf+12);
255         buf[12]=decimal_to_bcd(buf[12]);
256         buf[13]=decimal_to_bcd(buf[13]);
257         buf[14]=decimal_to_bcd(buf[14]);
258         buf[15]=2;
259
260         if (secsz<2336)
261         {
262            memset(buf+16,0xff,8);
263         }
264      }
265
266      switch (secsz)
267      {
268         case 2048:
269            memcpy(buf+24,ptr,2048);
270            break;
271
272         case 2336:
273            memcpy(buf+16,ptr,2336);
274            break;
275
276         case 2352:
277            memcpy(buf,ptr,raw_sector_size);
278            break;
279      }
280
281      return true;
282   } else
283   {
284      return false;
285   }
286}
287
288void cdrom_driver::set_machine(const running_machine &machine)
289{
290   m_machine = (running_machine *)&machine;
291
292   timestamp_frequency = m_machine->device<cpu_device>("maincpu")->clock();
293
294//  printf("cdrom_driver::set_machine: timestamp frequency = %d\n", timestamp_frequency);
295}
296
297/*
298    MAME/MESS driver implementation
299*/
300
301class mess_cdrom_driver : public cdrom_driver
302{
303   enum track_type
304   {
305      track_illegal=-1,
306      track_mode1_2048=0,
307      track_mode2_2352,
308      track_mode2_2336,
309      track_audio
310   };
311
312   struct toc_entry
313   {
314      unsigned int type;
315      unsigned char address[3];
316   };
317
318   int num_sectors, num_tracks;
319   toc_entry toc[100];
320   int bin_sector_size;
321   const char *err;
322
323   io_status *read_sectors(const unsigned int startsec, const unsigned int numsec, unsigned char *buf);
324
325public:
326   mess_cdrom_driver();
327   ~mess_cdrom_driver();
328
329   bool is_usable(char *error_msg, const int msglen) const
330   {
331      return true;
332   }
333
334   bool read_toc();
335   unsigned int get_first_track() const;
336   unsigned int get_num_tracks() const;
337   bool get_track_address(const unsigned int track,unsigned char *address) const;
338   tracktype get_track_type(const unsigned int track) const;
339   unsigned int find_track(const unsigned int sector, unsigned int *start_sector, unsigned int *end_sector) const;
340   cdromtype get_type() const;
341};
342
343mess_cdrom_driver::mess_cdrom_driver()
344   : err(NULL)
345{
346   for (int i=0; i<100; i++)
347      toc[i].type=track_illegal;
348   num_tracks=0;
349   num_sectors=0;
350}
351
352//
353//
354//
355
356mess_cdrom_driver::~mess_cdrom_driver()
357{
358   cancel_io();
359}
360
361//
362//
363//
364
365bool mess_cdrom_driver::read_toc()
366{
367   if (m_cd)
368   {
369      const cdrom_toc *toc = cdrom_get_toc(m_cd);
370
371      num_tracks = cdrom_get_last_track(m_cd);
372
373      switch (toc->tracks[0].trktype)
374      {
375         case CD_TRACK_MODE1: bin_sector_size=2048; break;
376         case CD_TRACK_MODE2_FORM1: bin_sector_size=2048; break;
377         case CD_TRACK_MODE2: bin_sector_size=2336; break;
378         case CD_TRACK_MODE2_FORM_MIX: bin_sector_size=2336; break;
379         default: bin_sector_size=2352; break;
380      }
381
382      set_native_sector_size(bin_sector_size);
383      num_sectors = cdrom_get_track_start(m_cd, num_tracks) + toc->tracks[num_tracks].frames;
384
385//      printf("mess_cdrom_driver: %d sectors, native size %d\n",num_sectors, bin_sector_size);
386
387      return true;
388   }
389
390   return false;
391}
392
393//
394//
395//
396
397io_status *mess_cdrom_driver::read_sectors(const unsigned int startsec, const unsigned int numsec, unsigned char *buf)
398{
399   const cdrom_toc *toc = cdrom_get_toc(m_cd);
400   UINT32 track = cdrom_get_track(m_cd, startsec);
401   UINT32 secsize = toc->tracks[track].datasize;
402
403   #ifdef debug_mess_driver
404   printf("mess: read %d sectors from %d (secsize %d)\n",numsec,startsec,secsize);
405   #endif
406
407   if ((startsec>=0)) // && ((int)startsec<num_sectors))
408   {
409//      fin->seek((INT64)startsec*(INT64)bin_sector_size);
410
411//      io_status *ios=fin->async_read(buf,numsec*bin_sector_size);
412//      if (! ios)
413      for (int i = 0; i < numsec; i++)
414      {
415//          printf("[%d/%d] Reading to pf_buffer %p at offset %d, size %d\n", i, numsec, &buf[secsize*i], secsize*i, secsize);
416         cdrom_read_data(m_cd, startsec+i, &buf[secsize*i], CD_TRACK_RAW_DONTCARE);
417      }
418
419      return NULL;
420
421//      return ios;
422   }
423   else
424   {
425      printf("mess: read sector out of range (%d, max=%d)\n",
426                startsec,
427                num_sectors);
428
429      memset(buf,0xff,numsec*bin_sector_size);
430      return NULL;
431   }
432}
433
434//
435//
436//
437
438unsigned int mess_cdrom_driver::get_first_track() const
439{
440   return 1;
441}
442
443unsigned int mess_cdrom_driver::get_num_tracks() const
444{
445//  printf("get_num_tracks = %d\n", num_tracks);
446   return num_tracks;
447}
448
449bool mess_cdrom_driver::get_track_address(const unsigned int track, unsigned char *address) const
450{
451   if ((track>=1) && ((int)track<=num_tracks))
452   {
453      UINT32 trkstart = cdrom_get_track_start(m_cd, track);
454
455      sector_to_msf(trkstart, address);
456
457      address[0] = address[0];
458      address[1] = address[1];
459      address[2] = address[2];
460
461//      printf("get_track_address %d = %02x:%02x:%02x\n", track, address[0], address[1], address[2]);
462
463      return true;
464   }
465   else
466   {
467      address[0]=address[1]=address[2]=0;
468      return false;
469   }
470}
471
472unsigned int mess_cdrom_driver::find_track(const unsigned int sector, unsigned int *start_sector, unsigned int *end_sector) const
473{
474   const cdrom_toc *toc = cdrom_get_toc(m_cd);
475   UINT32 track = cdrom_get_track(m_cd, sector);
476   int start;
477
478   start = cdrom_get_track_start(m_cd, track);
479
480   if (start_sector != NULL)
481   {
482      *start_sector = start;
483   }
484
485   if (end_sector != NULL)
486   {
487      *end_sector = start + toc->tracks[track].frames;
488   }
489
490   printf("find_track: track %d start %d end %d\n", track, start, *end_sector);
491
492   return -1;
493}
494
495//
496//
497//
498
499cdromtype mess_cdrom_driver::get_type() const
500{
501//  printf("get_type\n");
502   return cdromtype_cd;
503}
504
505//
506//
507//
508
509tracktype mess_cdrom_driver::get_track_type(const unsigned int track) const
510{
511   const cdrom_toc *toc = cdrom_get_toc(m_cd);
512
513//  printf("get_track_type %d = %d\n", track, toc->tracks[track].trktype);
514
515   switch (toc->tracks[track].trktype)
516   {
517      case CD_TRACK_MODE1: return tracktype_mode1;
518      case CD_TRACK_MODE2_RAW: return tracktype_mode2;
519      case CD_TRACK_MODE2_FORM_MIX: return tracktype_mode2;
520      case CD_TRACK_AUDIO: return tracktype_audio;
521      default: return tracktype_unknown;
522   }
523}
524
525//
526//
527//
528
529cdrom_driver *open_mess_drv()
530{
531   return new mess_cdrom_driver();
532}
533
src/mess/machine/psxcddrv.h
r0r11506
1#ifndef _included_psxcddriver_
2#define _included_psxcddriver_
3
4const unsigned int raw_sector_size=2352;
5
6class io_status
7{
8protected:
9   virtual ~io_status() {}
10
11public:
12   io_status() { }
13
14   virtual void cancel()=0;
15   virtual void release()=0;
16   virtual bool complete() const=0;
17   virtual bool block_until_complete() const=0;
18   virtual INT64 bytes_transferred() const=0;
19};
20
21enum cdromtype
22{
23   cdromtype_cd=0,
24   cdromtype_dvd
25};
26
27enum tracktype
28{
29   tracktype_unknown=0,
30   tracktype_mode1,
31   tracktype_mode2,
32   tracktype_audio
33};
34
35class cdrom_driver
36{
37   int pf_head,
38         pf_tail,
39         num_pf,
40         pf_head_sector;
41   unsigned char *pf_buffer;
42   io_status *pf_status,
43                  *last_pf_status;
44   INT64 pf_timeout_begin;
45
46   int native_sector_size;
47
48   virtual io_status *read_sectors(const unsigned int sec, const unsigned int numsec, unsigned char *buf)=0;
49
50   bool is_prefetch_sector_loaded(const unsigned int pfsec);
51
52protected:
53   void cancel_io();
54   void set_native_sector_size(const unsigned int sz);
55   UINT32 timestamp_frequency;
56   cdrom_file   *m_cd;
57private:
58   running_machine *m_machine;
59
60public:
61   cdrom_driver();
62   virtual ~cdrom_driver();
63
64   void set_machine(const running_machine &machine);
65   void set_cdrom_file(cdrom_file *cdfile) { m_cd = cdfile; }
66
67   virtual bool is_usable(char *error_msg=NULL, const int msglen=0) const=0;
68   virtual bool read_toc()=0;
69
70   void prefetch_sector(const unsigned int sec);
71   bool read_sector(const unsigned int sec,
72                            unsigned char *buf,
73                            const bool block=true);
74   unsigned char *get_prefetch_sector(const unsigned int pfsec,
75                                                       unsigned int *sz);
76
77   virtual unsigned int get_first_track() const=0;
78   virtual unsigned int get_num_tracks() const=0;
79   virtual bool get_track_address(const unsigned int track,
80                                                 unsigned char *address) const=0;
81   virtual tracktype get_track_type(const unsigned int track) const=0;
82   virtual unsigned int find_track(const unsigned int sector,
83                                                   unsigned int *start_sector=NULL,
84                                                   unsigned int *end_sector=NULL) const=0;
85
86   virtual cdromtype get_type() const { return cdromtype_cd; }
87};
88
89//
90//
91//
92
93inline unsigned int msf_to_sector(const unsigned char *msf)
94{
95   unsigned int sec=msf[2]+(msf[1]*75)+(msf[0]*(60*75));
96   if (sec>=150)
97   {
98      return sec-150;
99   }
100   else
101   {
102      return 0;
103   }
104}
105
106inline void sector_to_msf(const unsigned int sec, unsigned char *msf)
107{
108   unsigned int s=sec+150;
109   msf[0]=s/(60*75);
110   s-=msf[0]*(60*75);
111   msf[1]=s/75;
112   s-=msf[1]*75;
113   msf[2]=s;
114}
115
116//
117//
118//
119
120inline unsigned char bcd_to_decimal(const unsigned char bcd)
121{
122   return ((bcd>>4)*10)+(bcd&0xf);
123}
124
125inline unsigned char decimal_to_bcd(const unsigned char dec)
126{
127   //assert(dec<100);
128   return ((dec/10)<<4)|(dec%10);
129}
130
131cdrom_driver *open_mess_drv();
132
133#endif
src/mess/drivers/psx.c
r0r11506
1/***************************************************************************
2
3  Sony Playstaion
4  ===============
5  Preliminary driver by smf
6  Additional development by pSXAuthor and R. Belmont
7
8***************************************************************************/
9
10#include "emu.h"
11#include "cpu/psx/psx.h"
12#include "imagedev/snapquik.h"
13#include "imagedev/chd_cd.h"
14#include "includes/psx.h"
15#include "sound/spu.h"
16#include "debugger.h"
17#include "zlib.h"
18#include "machine/psxcd.h"
19#include "machine/psxcard.h"
20
21typedef struct
22{
23   UINT8 n_shiftin;
24   UINT8 n_shiftout;
25   int n_bits;
26   int n_state;
27   int n_byte;
28   int b_lastclock;
29   int b_ack;
30} pad_t;
31
32
33class psx1_state : public psx_state
34{
35public:
36   psx1_state(const machine_config &mconfig, device_type type, const char *tag)
37      : psx_state(mconfig, type, tag) { }
38
39   UINT8 *m_exe_buffer;
40   int m_exe_size;
41   pad_t m_pad[ 2 ];
42   int m_cd_param_p;
43   int m_cd_result_p;
44   int m_cd_result_c;
45   int m_cd_result_ready;
46   int m_cd_reset;
47   UINT8 m_cd_stat;
48   UINT8 m_cd_io_status;
49   UINT8 m_cd_param[8];
50   UINT8 m_cd_result[8];
51};
52
53
54#define VERBOSE_LEVEL ( 0 )
55
56INLINE void ATTR_PRINTF(3,4) verboselog( running_machine &machine, int n_level, const char *s_fmt, ... )
57{
58   if( VERBOSE_LEVEL >= n_level )
59   {
60      va_list v;
61      char buf[ 32768 ];
62      va_start( v, s_fmt );
63      vsprintf( buf, s_fmt, v );
64      va_end( v );
65      logerror( "%s: %s", machine.describe_context(), buf );
66   }
67}
68
69
70static void psxexe_conv32( UINT32 *p_uint32 )
71{
72   UINT8 *p_uint8;
73
74   p_uint8 = (UINT8 *)p_uint32;
75
76   *( p_uint32 ) = p_uint8[ 0 ] |
77      ( p_uint8[ 1 ] << 8 ) |
78      ( p_uint8[ 2 ] << 16 ) |
79      ( p_uint8[ 3 ] << 24 );
80}
81
82static int load_psxexe( device_t *cpu, unsigned char *p_n_file, int n_len )
83{
84   psx_state *state = cpu->machine().driver_data<psx_state>();
85   struct PSXEXE_HEADER
86   {
87      UINT8 id[ 8 ];
88      UINT32 text;   /* SCE only */
89      UINT32 data;   /* SCE only */
90      UINT32 pc0;
91      UINT32 gp0;      /* SCE only */
92      UINT32 t_addr;
93      UINT32 t_size;
94      UINT32 d_addr;   /* SCE only */
95      UINT32 d_size;   /* SCE only */
96      UINT32 b_addr;   /* SCE only */
97      UINT32 b_size;   /* SCE only */
98      UINT32 s_addr;
99      UINT32 s_size;
100      UINT32 SavedSP;
101      UINT32 SavedFP;
102      UINT32 SavedGP;
103      UINT32 SavedRA;
104      UINT32 SavedS0;
105      UINT8 dummy[ 0x800 - 76 ];
106   };
107
108   struct PSXEXE_HEADER *psxexe_header = (struct PSXEXE_HEADER *)p_n_file;
109
110   if( n_len >= sizeof( struct PSXEXE_HEADER ) &&
111      memcmp( psxexe_header->id, "PS-X EXE", 8 ) == 0 )
112   {
113      UINT8 *p_ram;
114      UINT8 *p_psxexe;
115      UINT32 n_stack;
116      UINT32 n_ram;
117      UINT32 n_address;
118      UINT32 n_size;
119
120      psxexe_conv32( &psxexe_header->text );
121      psxexe_conv32( &psxexe_header->data );
122      psxexe_conv32( &psxexe_header->pc0 );
123      psxexe_conv32( &psxexe_header->gp0 );
124      psxexe_conv32( &psxexe_header->t_addr );
125      psxexe_conv32( &psxexe_header->t_size );
126      psxexe_conv32( &psxexe_header->d_addr );
127      psxexe_conv32( &psxexe_header->d_size );
128      psxexe_conv32( &psxexe_header->b_addr );
129      psxexe_conv32( &psxexe_header->b_size );
130      psxexe_conv32( &psxexe_header->s_addr );
131      psxexe_conv32( &psxexe_header->s_size );
132      psxexe_conv32( &psxexe_header->SavedSP );
133      psxexe_conv32( &psxexe_header->SavedFP );
134      psxexe_conv32( &psxexe_header->SavedGP );
135      psxexe_conv32( &psxexe_header->SavedRA );
136      psxexe_conv32( &psxexe_header->SavedS0 );
137
138      /* todo: check size.. */
139
140      logerror( "psx_exe_load: pc    %08x\n", psxexe_header->pc0 );
141      logerror( "psx_exe_load: org   %08x\n", psxexe_header->t_addr );
142      logerror( "psx_exe_load: len   %08x\n", psxexe_header->t_size );
143      logerror( "psx_exe_load: sp    %08x\n", psxexe_header->s_addr );
144      logerror( "psx_exe_load: len   %08x\n", psxexe_header->s_size );
145
146      p_ram = (UINT8 *)state->m_p_n_psxram;
147      n_ram = state->m_n_psxramsize;
148
149      p_psxexe = p_n_file + sizeof( struct PSXEXE_HEADER );
150
151      n_address = psxexe_header->t_addr;
152      n_size = psxexe_header->t_size;
153      while( n_size != 0 )
154      {
155         p_ram[ BYTE4_XOR_LE( n_address ) % n_ram ] = *( p_psxexe );
156         n_address++;
157         p_psxexe++;
158         n_size--;
159      }
160
161      cpu_set_reg( cpu, PSXCPU_PC, psxexe_header->pc0 );
162      cpu_set_reg( cpu, PSXCPU_R28, psxexe_header->gp0 );
163      n_stack = psxexe_header->s_addr + psxexe_header->s_size;
164      if( n_stack != 0 )
165      {
166         cpu_set_reg( cpu, PSXCPU_R29, n_stack );
167         cpu_set_reg( cpu, PSXCPU_R30, n_stack );
168      }
169
170      return 1;
171   }
172   return 0;
173}
174
175static void cpe_set_register( device_t *cpu, int n_reg, int n_value )
176{
177   if( n_reg < 0x80 && ( n_reg % 4 ) == 0 )
178   {
179      logerror( "psx_exe_load: r%-2d   %08x\n", n_reg / 4, n_value );
180      cpu_set_reg( cpu, PSXCPU_R0 + ( n_reg / 4 ), n_value );
181   }
182   else if( n_reg == 0x80 )
183   {
184      logerror( "psx_exe_load: lo    %08x\n", n_value );
185      cpu_set_reg( cpu, PSXCPU_LO, n_value );
186   }
187   else if( n_reg == 0x84 )
188   {
189      logerror( "psx_exe_load: hi    %08x\n", n_value );
190      cpu_set_reg( cpu, PSXCPU_HI, n_value );
191   }
192   else if( n_reg == 0x88 )
193   {
194      logerror( "psx_exe_load: sr    %08x\n", n_value );
195      cpu_set_reg( cpu, PSXCPU_CP0R12, n_value );
196   }
197   else if( n_reg == 0x8c )
198   {
199      logerror( "psx_exe_load: cause %08x\n", n_value );
200      cpu_set_reg( cpu, PSXCPU_CP0R13, n_value );
201   }
202   else if( n_reg == 0x90 )
203   {
204      logerror( "psx_exe_load: pc    %08x\n", n_value );
205      cpu_set_reg( cpu, PSXCPU_PC, n_value );
206   }
207   else if( n_reg == 0x94 )
208   {
209      logerror( "psx_exe_load: prid  %08x\n", n_value );
210      cpu_set_reg( cpu, PSXCPU_CP0R15, n_value );
211   }
212   else
213   {
214      logerror( "psx_exe_load: invalid register %04x/%08x\n", n_reg, n_value );
215   }
216}
217
218static int load_cpe( device_t *cpu, unsigned char *p_n_file, int n_len )
219{
220   psx_state *state = cpu->machine().driver_data<psx_state>();
221   if( n_len >= 4 &&
222      memcmp( p_n_file, "CPE\001", 4 ) == 0 )
223   {
224      int n_offset = 4;
225
226      for( ;; )
227      {
228         if( n_offset >= n_len || p_n_file[ n_offset ] > 8 )
229         {
230            break;
231         }
232
233         switch( p_n_file[ n_offset++ ] )
234         {
235         case 0:
236            /* end of file */
237            return 1;
238         case 1:
239            /* read bytes */
240            {
241               int n_address = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
242                  ( (int)p_n_file[ n_offset + 1 ] << 8 ) |
243                  ( (int)p_n_file[ n_offset + 2 ] << 16 ) |
244                  ( (int)p_n_file[ n_offset + 3 ] << 24 );
245               int n_size = ( (int)p_n_file[ n_offset + 4 ] << 0 ) |
246                  ( (int)p_n_file[ n_offset + 5 ] << 8 ) |
247                  ( (int)p_n_file[ n_offset + 6 ] << 16 ) |
248                  ( (int)p_n_file[ n_offset + 7 ] << 24 );
249
250               UINT8 *p_ram = (UINT8 *)state->m_p_n_psxram;
251               UINT32 n_ram = state->m_n_psxramsize;
252
253               n_offset += 8;
254
255               logerror( "psx_exe_load: org   %08x\n", n_address );
256               logerror( "psx_exe_load: len   %08x\n", n_size );
257
258               while( n_size > 0 )
259               {
260                  p_ram[ BYTE4_XOR_LE( n_address ) % n_ram ] = p_n_file[ n_offset++ ];
261                  n_address++;
262                  n_size--;
263               }
264               break;
265            }
266         case 2:
267            /* run address: not tested */
268            {
269               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
270                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
271                  ( (int)p_n_file[ n_offset + 4 ] << 16 ) |
272                  ( (int)p_n_file[ n_offset + 5 ] << 24 );
273
274               n_offset += 4;
275
276               cpe_set_register( cpu, 0x90, n_value );
277               break;
278            }
279         case 3:
280            /* set reg to longword */
281            {
282               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
283                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
284               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
285                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
286                  ( (int)p_n_file[ n_offset + 4 ] << 16 ) |
287                  ( (int)p_n_file[ n_offset + 5 ] << 24 );
288
289               n_offset += 6;
290
291               cpe_set_register( cpu, n_reg, n_value );
292               break;
293            }
294         case 4:
295            /* set reg to word: not tested */
296            {
297               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
298                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
299               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
300                  ( (int)p_n_file[ n_offset + 3 ] << 8 );
301
302               n_offset += 4;
303
304               cpe_set_register( cpu, n_reg, n_value );
305               break;
306            }
307         case 5:
308            /* set reg to byte: not tested */
309            {
310               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
311                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
312               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 );
313
314               n_offset += 3;
315
316               cpe_set_register( cpu, n_reg, n_value );
317               break;
318            }
319         case 6:
320            /* set reg to 3-byte: not tested */
321            {
322               int n_reg = ( (int)p_n_file[ n_offset + 0 ] << 0 ) |
323                  ( (int)p_n_file[ n_offset + 1 ] << 8 );
324               int n_value = ( (int)p_n_file[ n_offset + 2 ] << 0 ) |
325                  ( (int)p_n_file[ n_offset + 3 ] << 8 ) |
326                  ( (int)p_n_file[ n_offset + 4 ] << 16 );
327
328               n_offset += 5;
329
330               cpe_set_register( cpu, n_reg, n_value );
331               break;
332            }
333         case 7:
334            /* workspace: not tested */
335            n_offset += 4;
336            break;
337         case 8:
338            /* unit */
339            {
340               int n_unit = p_n_file[ n_offset + 0 ];
341
342               n_offset++;
343
344               logerror( "psx_exe_load: unit  %08x\n", n_unit );
345            }
346            break;
347         }
348      }
349   }
350   return 0;
351}
352
353static int load_psf( device_t *cpu, unsigned char *p_n_file, int n_len )
354{
355   int n_return;
356   unsigned long n_crc;
357   unsigned long n_compressed;
358   unsigned char *p_n_compressed;
359   unsigned long n_uncompressed;
360   unsigned char *p_n_uncompressed;
361
362   struct PSF_HEADER
363   {
364      unsigned char id[ 4 ];
365      UINT32 reserved_size;
366      UINT32 exe_size;
367      UINT32 exe_crc;
368   };
369
370   struct PSF_HEADER *psf_header = (struct PSF_HEADER *)p_n_file;
371
372   n_return = 0;
373
374   if( n_len >= sizeof( struct PSF_HEADER ) &&
375      memcmp( p_n_file, "PSF", 3 ) == 0 )
376   {
377      psxexe_conv32( &psf_header->reserved_size );
378      psxexe_conv32( &psf_header->exe_size );
379      psxexe_conv32( &psf_header->exe_crc );
380
381      logerror( "psx_exe_load: reserved_size %08x\n", psf_header->reserved_size );
382      logerror( "psx_exe_load: exe_size      %08x\n", psf_header->exe_size );
383      logerror( "psx_exe_load: exe_crc       %08x\n", psf_header->exe_crc );
384
385      n_compressed = psf_header->exe_size;
386      p_n_compressed = p_n_file + sizeof( struct PSF_HEADER ) + psf_header->reserved_size;
387
388      n_crc = crc32( crc32( 0L, Z_NULL, 0 ), p_n_compressed, n_compressed );
389      if( n_crc != psf_header->exe_crc )
390      {
391         logerror( "psx_exe_load: psf invalid crc\n" );
392         return 0;
393      }
394
395      n_uncompressed = 0x200000;
396      p_n_uncompressed = (unsigned char *)malloc( n_uncompressed );
397
398      if( uncompress( p_n_uncompressed, &n_uncompressed, p_n_compressed, n_compressed ) != Z_OK )
399      {
400         logerror( "psx_exe_load: psf uncompress failed\n" );
401      }
402      else if( !load_psxexe( cpu, p_n_uncompressed, n_uncompressed ) )
403      {
404         logerror( "psx_exe_load: psf load failed\n" );
405      }
406      else
407      {
408         n_return = 1;
409      }
410
411      free( p_n_uncompressed );
412   }
413   return n_return;
414}
415
416DIRECT_UPDATE_HANDLER( psx_default )
417{
418   return address;
419}
420
421DIRECT_UPDATE_HANDLER( psx_setopbase )
422{
423   psx1_state *state = machine.driver_data<psx1_state>();
424   if( address == 0x80030000 )
425   {
426      device_t *cpu = machine.device("maincpu");
427
428      machine.device("maincpu")->memory().space(AS_PROGRAM)->set_direct_update_handler(direct_update_delegate(FUNC(psx_default), &machine));
429
430      if( load_psxexe( cpu, state->m_exe_buffer, state->m_exe_size ) ||
431         load_cpe( cpu, state->m_exe_buffer, state->m_exe_size ) ||
432         load_psf( cpu, state->m_exe_buffer, state->m_exe_size ) )
433      {
434/*          DEBUGGER_BREAK; */
435
436         address = cpu_get_reg( cpu, PSXCPU_PC );
437      }
438      else
439      {
440         logerror( "psx_exe_load: invalid exe\n" );
441      }
442
443      state->m_exe_size = 0;
444      free( state->m_exe_buffer );
445   }
446   return address;
447}
448
449static QUICKLOAD_LOAD( psx_exe_load )
450{
451   psx1_state *state = image.device().machine().driver_data<psx1_state>();
452   address_space *space = image.device().machine().device( "maincpu")->memory().space( AS_PROGRAM );
453
454   state->m_exe_size = 0;
455   state->m_exe_buffer = (UINT8*)malloc( quickload_size );
456   if( state->m_exe_buffer == NULL )
457   {
458      logerror( "psx_exe_load: out of memory\n" );
459      return IMAGE_INIT_FAIL;
460   }
461   if( image.fread( state->m_exe_buffer, quickload_size ) != quickload_size )
462   {
463      free( state->m_exe_buffer );
464      return IMAGE_INIT_FAIL;
465   }
466   state->m_exe_size = quickload_size;
467   space->set_direct_update_handler(direct_update_delegate(FUNC(psx_setopbase), &image.device().machine()));
468
469   return IMAGE_INIT_PASS;
470}
471
472/* PAD emulation */
473
474#define PAD_STATE_IDLE ( 0 )
475#define PAD_STATE_LISTEN ( 1 )
476#define PAD_STATE_ACTIVE ( 2 )
477#define PAD_STATE_READ ( 3 )
478#define PAD_STATE_UNLISTEN ( 4 )
479#define PAD_STATE_MEMCARD ( 5 )
480
481#define PAD_TYPE_STANDARD ( 4 )
482#define PAD_BYTES_STANDARD ( 2 )
483
484#define PAD_CMD_START ( 0x01 )
485#define PAD_CMD_READ  ( 0x42 ) /* B */
486
487#define PAD_DATA_OK   ( 0x5a ) /* Z */
488#define PAD_DATA_IDLE ( 0xff )
489
490static TIMER_CALLBACK(psx_pad_ack)
491{
492   psx1_state *state = machine.driver_data<psx1_state>();
493   int n_port = param;
494   pad_t *pad = &state->m_pad[ n_port ];
495
496   if( pad->n_state != PAD_STATE_IDLE )
497   {
498      psx_sio_input( machine, 0, PSX_SIO_IN_DSR, pad->b_ack * PSX_SIO_IN_DSR );
499      if( !pad->b_ack )
500      {
501         pad->b_ack = 1;
502         machine.scheduler().timer_set(attotime::from_usec( 2 ), FUNC(psx_pad_ack) , n_port);
503      }
504   }
505}
506
507static void psx_pad( running_machine &machine, int n_port, int n_data )
508{
509   psx1_state *state = machine.driver_data<psx1_state>();
510   pad_t *pad = &state->m_pad[ n_port ];
511   int b_sel;
512   int b_clock;
513   int b_data;
514   int b_ack;
515   int b_ready;
516   static const char *const portnames[] = { "IN0", "IN1", "IN2", "IN3" };
517   psxcard_device *psxcard = NULL;
518
519   if (n_port == 0)
520   {
521      psxcard = machine.device<psxcard_device>("card1");
522   }
523   else
524   {
525      psxcard = machine.device<psxcard_device>("card2");
526   }
527
528   b_sel = ( n_data & PSX_SIO_OUT_DTR ) / PSX_SIO_OUT_DTR;
529   b_clock = ( n_data & PSX_SIO_OUT_CLOCK ) / PSX_SIO_OUT_CLOCK;
530   b_data = ( n_data & PSX_SIO_OUT_DATA ) / PSX_SIO_OUT_DATA;
531   b_ready = 0;
532   b_ack = 0;
533
534   if( b_sel )
535   {
536      pad->n_state = PAD_STATE_IDLE;
537   }
538
539   switch( pad->n_state )
540   {
541   case PAD_STATE_LISTEN:
542   case PAD_STATE_ACTIVE:
543   case PAD_STATE_READ:
544   case PAD_STATE_MEMCARD:
545      if( pad->b_lastclock && !b_clock )
546      {
547         psx_sio_input( machine, 0, PSX_SIO_IN_DATA, ( pad->n_shiftout & 1 ) * PSX_SIO_IN_DATA );
548         pad->n_shiftout >>= 1;
549      }
550      if( !pad->b_lastclock && b_clock )
551      {
552         pad->n_shiftin >>= 1;
553         pad->n_shiftin |= b_data << 7;
554         pad->n_bits++;
555
556         if( pad->n_bits == 8 )
557         {
558            pad->n_bits = 0;
559            b_ready = 1;
560         }
561      }
562      break;
563   }
564
565   pad->b_lastclock = b_clock;
566
567   switch( pad->n_state )
568   {
569   case PAD_STATE_IDLE:
570      if( !b_sel )
571      {
572         pad->n_state = PAD_STATE_LISTEN;
573         pad->n_shiftout = PAD_DATA_IDLE;
574         pad->n_bits = 0;
575      }
576      break;
577   case PAD_STATE_LISTEN:
578      if( b_ready )
579      {
580         if( pad->n_shiftin == PAD_CMD_START )
581         {
582            pad->n_state = PAD_STATE_ACTIVE;
583            pad->n_shiftout = ( PAD_TYPE_STANDARD << 4 ) | ( PAD_BYTES_STANDARD >> 1 );
584            b_ack = 1;
585         }
586         else if( psxcard->transfer(pad->n_shiftin, &pad->n_shiftout) )
587         {
588            pad->n_state = PAD_STATE_MEMCARD;
589            b_ack = 1;
590         }
591         else
592         {
593            pad->n_state = PAD_STATE_UNLISTEN;
594         }
595      }
596      break;
597   case PAD_STATE_MEMCARD:
598      if( b_ready )
599      {
600         if( psxcard->transfer(pad->n_shiftin, &pad->n_shiftout) )
601         {
602            b_ack = 1;
603         }
604         else
605         {
606            b_ack = 0;
607            pad->n_state = PAD_STATE_IDLE;
608         }
609      }
610      break;
611   case PAD_STATE_ACTIVE:
612      if( b_ready )
613      {
614         if( pad->n_shiftin == PAD_CMD_READ )
615         {
616            pad->n_state = PAD_STATE_READ;
617            pad->n_shiftout = PAD_DATA_OK;
618            pad->n_byte = 0;
619            b_ack = 1;
620         }
621         else
622         {
623            pad->n_state = PAD_STATE_UNLISTEN;
624         }
625      }
626      break;
627   case PAD_STATE_READ:
628      if( b_ready )
629      {
630         if( pad->n_byte < PAD_BYTES_STANDARD )
631         {
632            pad->n_shiftout = input_port_read(machine, portnames[pad->n_byte + ( n_port * PAD_BYTES_STANDARD )]);
633            pad->n_byte++;
634            b_ack = 1;
635         }
636         else
637         {
638            pad->n_state = PAD_STATE_LISTEN;
639         }
640      }
641      break;
642   }
643
644   if( b_ack )
645   {
646      pad->b_ack = 0;
647      machine.scheduler().timer_set(attotime::from_usec( 10 ), FUNC(psx_pad_ack), n_port);
648   }
649}
650
651static void psx_sio0( running_machine &machine, int n_data )
652{
653   /* todo: raise data & ack when nothing is driving it low */
654   psx_pad( machine, 0, n_data );
655   psx_pad( machine, 1, n_data ^ PSX_SIO_OUT_DTR );
656}
657
658/* ----------------------------------------------------------------------- */
659
660static void cd_dma_read( psxcd_device *psxcd, UINT32 n_address, INT32 n_size )
661{
662   UINT8 *psxram = (UINT8 *)memory_get_shared(psxcd->machine(), "share1");
663
664   psxcd->start_dma(psxram + n_address, n_size*4);
665}
666
667static void cd_dma_write( psxcd_device *psxcd, UINT32 n_address, INT32 n_size )
668{
669   printf("cd_dma_write?!: addr %x, size %x\n", n_address, n_size);
670}
671
672static READ8_HANDLER( psx_cd_r )
673{
674   psxcd_device *psxcd = space->machine().device<psxcd_device>(PSXCD_TAG);
675
676   return psxcd->read_byte(offset);
677}
678
679static WRITE8_HANDLER( psx_cd_w )
680{
681   psxcd_device *psxcd = space->machine().device<psxcd_device>(PSXCD_TAG);
682
683   psxcd->write_byte(offset, data);
684}
685
686static ADDRESS_MAP_START( psx_map, AS_PROGRAM, 32 )
687   AM_RANGE(0x00000000, 0x001fffff) AM_RAM   AM_SHARE("share1") /* ram */
688   AM_RANGE(0x1f801800, 0x1f801803) AM_READWRITE8(psx_cd_r, psx_cd_w, 0xffffffff)
689   AM_RANGE(0x1fc00000, 0x1fc7ffff) AM_ROM AM_SHARE("share2") AM_REGION("user1", 0) /* bios */
690   AM_RANGE(0x80000000, 0x801fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
691   AM_RANGE(0x80600000, 0x807fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
692   AM_RANGE(0x9fc00000, 0x9fc7ffff) AM_ROM AM_SHARE("share2") /* bios mirror */
693   AM_RANGE(0xa0000000, 0xa01fffff) AM_RAM AM_SHARE("share1") /* ram mirror */
694   AM_RANGE(0xbfc00000, 0xbfc7ffff) AM_ROM AM_SHARE("share2") /* bios mirror */
695   AM_RANGE(0xfffe0130, 0xfffe0133) AM_WRITENOP
696ADDRESS_MAP_END
697
698
699static MACHINE_RESET( psx )
700{
701   psx_machine_init(machine);
702   psx_sio_install_handler( machine, 0, psx_sio0 );
703
704   psx_dma_install_read_handler(machine, 3, psx_dma_read_delegate(FUNC(cd_dma_read),machine.device<psxcd_device>("psxcd")));
705   psx_dma_install_write_handler(machine, 3, psx_dma_write_delegate(FUNC(cd_dma_write),machine.device<psxcd_device>("psxcd")));
706}
707
708static DRIVER_INIT( psx )
709{
710   psx_driver_init(machine);
711}
712
713static INPUT_PORTS_START( psx )
714   PORT_START("IN0")
715   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )  PORT_PLAYER(1)
716   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )  PORT_PLAYER(1)
717   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(1)
718   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )   PORT_PLAYER(1)
719   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START )  PORT_PLAYER(1)
720   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(1)
721   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(1)
722   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SELECT ) PORT_PLAYER(1)
723
724   PORT_START("IN1")
725   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(1) PORT_NAME("P1 Square")
726   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(1) PORT_NAME("P1 Cross")
727   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(1) PORT_NAME("P1 Circle")
728   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(1) PORT_NAME("P1 Triangle")
729   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(1) PORT_NAME("P1 R1")
730   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(1) PORT_NAME("P1 L1")
731   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(1) PORT_NAME("P1 R2")
732   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_PLAYER(1) PORT_NAME("P1 L2")
733
734   PORT_START("IN2")
735   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_JOYSTICK_LEFT )  PORT_PLAYER(2)
736   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_JOYSTICK_DOWN )  PORT_PLAYER(2)
737   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_JOYSTICK_RIGHT ) PORT_PLAYER(2)
738   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_JOYSTICK_UP )   PORT_PLAYER(2)
739   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_START )  PORT_PLAYER(2)
740   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(2)
741   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_UNUSED ) PORT_PLAYER(2)
742   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_SELECT ) PORT_PLAYER(2)
743
744   PORT_START("IN3")
745   PORT_BIT( 0x80, IP_ACTIVE_LOW, IPT_BUTTON1 ) PORT_PLAYER(2) PORT_NAME("P2 Square")
746   PORT_BIT( 0x40, IP_ACTIVE_LOW, IPT_BUTTON2 ) PORT_PLAYER(2) PORT_NAME("P2 Cross")
747   PORT_BIT( 0x20, IP_ACTIVE_LOW, IPT_BUTTON3 ) PORT_PLAYER(2) PORT_NAME("P2 Circle")
748   PORT_BIT( 0x10, IP_ACTIVE_LOW, IPT_BUTTON4 ) PORT_PLAYER(2) PORT_NAME("P2 Triangle")
749   PORT_BIT( 0x08, IP_ACTIVE_LOW, IPT_BUTTON5 ) PORT_PLAYER(2) PORT_NAME("P2 R1")
750   PORT_BIT( 0x04, IP_ACTIVE_LOW, IPT_BUTTON6 ) PORT_PLAYER(2) PORT_NAME("P2 L1")
751   PORT_BIT( 0x02, IP_ACTIVE_LOW, IPT_BUTTON7 ) PORT_PLAYER(2) PORT_NAME("P2 R2")
752   PORT_BIT( 0x01, IP_ACTIVE_LOW, IPT_BUTTON8 ) PORT_PLAYER(2) PORT_NAME("P2 L2")
753INPUT_PORTS_END
754
755static void spu_irq(device_t *device, UINT32 data)
756{
757   if (data)
758   {
759      psx_irq_set(device->machine(), 1<<9);
760   }
761}
762
763static MACHINE_CONFIG_START( psxntsc, psx1_state )
764   /* basic machine hardware */
765   MCFG_CPU_ADD( "maincpu", CXD8530AQ, XTAL_67_7376MHz )
766   MCFG_CPU_PROGRAM_MAP( psx_map)
767   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
768
769   MCFG_SCREEN_ADD("screen", RASTER)
770   MCFG_SCREEN_REFRESH_RATE( 60 )
771   MCFG_SCREEN_VBLANK_TIME(0)
772
773   MCFG_MACHINE_RESET( psx )
774
775   /* video hardware */
776   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
777   MCFG_SCREEN_SIZE( 1024, 512 )
778   MCFG_SCREEN_VISIBLE_AREA( 0, 639, 0, 479 )
779   MCFG_SCREEN_UPDATE( psx )
780
781   MCFG_PALETTE_LENGTH( 65536 )
782
783   MCFG_PALETTE_INIT( psx )
784   MCFG_VIDEO_START( psx_type2 )
785
786   /* sound hardware */
787   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
788   MCFG_SPU_ADD( "spu", XTAL_67_7376MHz/2, &spu_irq )
789   MCFG_SOUND_ROUTE( 0, "lspeaker", 1.00 )
790   MCFG_SOUND_ROUTE( 1, "rspeaker", 1.00 )
791
792   /* quickload */
793   MCFG_QUICKLOAD_ADD("quickload", psx_exe_load, "cpe,exe,psf,psx", 0)
794
795   MCFG_CDROM_ADD("cdrom")
796   MCFG_PSXCD_ADD("cdrom")
797   MCFG_PSXCARD_ADD("card1")
798   MCFG_PSXCARD_ADD("card2")
799MACHINE_CONFIG_END
800
801static MACHINE_CONFIG_START( psxpal, psx1_state )
802   /* basic machine hardware */
803   MCFG_CPU_ADD( "maincpu", CXD8530AQ, XTAL_67_7376MHz )
804   MCFG_CPU_PROGRAM_MAP( psx_map)
805   MCFG_CPU_VBLANK_INT("screen", psx_vblank)
806
807   MCFG_SCREEN_ADD("screen", RASTER)
808   MCFG_SCREEN_REFRESH_RATE( 50 )
809   MCFG_SCREEN_VBLANK_TIME(0)
810
811   MCFG_MACHINE_RESET( psx )
812
813   /* video hardware */
814   MCFG_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
815   MCFG_SCREEN_SIZE( 1024, 512 )
816   MCFG_SCREEN_VISIBLE_AREA( 0, 639, 0, 511 )
817   MCFG_SCREEN_UPDATE( psx )
818   MCFG_PALETTE_LENGTH( 65536 )
819
820   MCFG_PALETTE_INIT( psx )
821   MCFG_VIDEO_START( psx_type2 )
822
823   /* sound hardware */
824   MCFG_SPEAKER_STANDARD_STEREO("lspeaker", "rspeaker")
825   MCFG_SPU_ADD( "spu", XTAL_67_7376MHz/2, &spu_irq )
826   MCFG_SOUND_ROUTE( 0, "lspeaker", 1.00 )
827   MCFG_SOUND_ROUTE( 1, "rspeaker", 1.00 )
828
829   /* quickload */
830   MCFG_QUICKLOAD_ADD("quickload", psx_exe_load, "cpe,exe,psf,psx", 0)
831
832   MCFG_CDROM_ADD("cdrom")
833   MCFG_PSXCD_ADD("cdrom")
834   MCFG_PSXCARD_ADD("card1")
835   MCFG_PSXCARD_ADD("card2")
836MACHINE_CONFIG_END
837
838ROM_START( psj )
839   ROM_REGION32_LE( 0x080000, "user1", 0 )
840
841   ROM_SYSTEM_BIOS( 0, "1.0J", "SCPH-1000/DTL-H1000" ) // 22091994
842   ROMX_LOAD( "ps-10j.bin",    0x0000000, 0x080000, CRC(3b601fc8) SHA1(343883a7b555646da8cee54aadd2795b6e7dd070), ROM_BIOS(1) )
843
844   ROM_SYSTEM_BIOS( 1, "1.1J", "SCPH-3000/DTL-H1000H (Version 1.1 01/22/95)" ) // 22091994
845   ROMX_LOAD( "ps-11j.bin",    0x0000000, 0x080000, CRC(3539def6) SHA1(b06f4a861f74270be819aa2a07db8d0563a7cc4e), ROM_BIOS(2) )
846
847   ROM_SYSTEM_BIOS( 2, "2.1J", "SCPH-3500 (Version 2.1 07/17/95 J)" ) // 22091994
848   ROMX_LOAD( "ps-21j.bin",    0x0000000, 0x080000, CRC(bc190209) SHA1(e38466a4ba8005fba7e9e3c7b9efeba7205bee3f), ROM_BIOS(3) )
849
850   ROM_SYSTEM_BIOS( 3, "2.2J", "SCPH-5000/DTL-H1200 (Version 2.2 12/04/95 J)" ) // 04121995
851/*  ROMX_LOAD( "ps-22j.bad",    0x0000000, 0x080000, BAD_DUMP CRC(8c93a399) SHA1(e340db2696274dda5fdc25e434a914db71e8b02b), ROM_BIOS(4) ) */
852   ROMX_LOAD( "ps-22j.bin",    0x0000000, 0x080000, CRC(24fc7e17) SHA1(ffa7f9a7fb19d773a0c3985a541c8e5623d2c30d), ROM_BIOS(4) )
853
854   ROM_SYSTEM_BIOS( 4, "2.2D", "DTL-H1100 (Version 2.2 03/06/96 D)" ) // 04121995
855   ROMX_LOAD( "ps-22d.bin",    0x0000000, 0x080000, CRC(decb22f5) SHA1(73107d468fc7cb1d2c5b18b269715dd889ecef06), ROM_BIOS(5) )
856
857   ROM_SYSTEM_BIOS( 5, "3.0J", "SCPH-5500 (Version 3.0 09/09/96 J)" ) // 04121995
858   ROMX_LOAD( "ps-30j.bin",    0x0000000, 0x080000, CRC(ff3eeb8c) SHA1(b05def971d8ec59f346f2d9ac21fb742e3eb6917), ROM_BIOS(6) )
859
860   ROM_SYSTEM_BIOS( 6, "4.0J", "SCPH-7000/SCPH-9000 (Version 4.0 08/18/97 J)" ) // 29051997
861   ROMX_LOAD( "ps-40j.bin",    0x0000000, 0x080000, CRC(ec541cd0) SHA1(77b10118d21ac7ffa9b35f9c4fd814da240eb3e9), ROM_BIOS(7) )
862
863   ROM_SYSTEM_BIOS( 7, "4.3J", "SCPH-100 (Version 4.3 03/11/00 J)" ) // 04121995
864   ROMX_LOAD( "psone-43j.bin", 0x0000000, 0x080000, CRC(f2af798b) SHA1(339a48f4fcf63e10b5b867b8c93cfd40945faf6c), ROM_BIOS(8) )
865ROM_END
866
867ROM_START( psu )
868   ROM_REGION32_LE( 0x080000, "user1", 0 )
869
870   ROM_SYSTEM_BIOS( 0, "2.0A", "DTL-H1001 (Version 2.0 05/07/95 A)" ) // 22091994
871   ROMX_LOAD( "ps-20a.bin",    0x0000000, 0x080000, CRC(55847d8c) SHA1(649895efd79d14790eabb362e94eb0622093dfb9), ROM_BIOS(1) )
872
873   ROM_SYSTEM_BIOS( 1, "2.1A", "DTL-H1101 (Version 2.1 07/17/95 A)" ) // 22091994
874   ROMX_LOAD( "ps-21a.bin",    0x0000000, 0x080000, CRC(aff00f2f) SHA1(ca7af30b50d9756cbd764640126c454cff658479), ROM_BIOS(2) )
875
876   ROM_SYSTEM_BIOS( 2, "2.2A", "SCPH-1001/DTL-H1201/DTL-H3001 (Version 2.2 12/04/95 A)" ) // 04121995
877   ROMX_LOAD( "ps-22a.bin",    0x0000000, 0x080000, CRC(37157331) SHA1(10155d8d6e6e832d6ea66db9bc098321fb5e8ebf), ROM_BIOS(3) )
878
879   ROM_SYSTEM_BIOS( 3, "3.0A", "SCPH-5501/SCPH-7003 (Version 3.0 11/18/96 A)" ) // 04121995
880   ROMX_LOAD( "ps-30a.bin",    0x0000000, 0x080000, CRC(8d8cb7e4) SHA1(0555c6fae8906f3f09baf5988f00e55f88e9f30b), ROM_BIOS(4) )
881
882   ROM_SYSTEM_BIOS( 4, "4.1A", "SCPH-7001/SCPH-7501/SCPH-7503/SCPH-9001 (Version 4.1 12/16/97 A)" ) // 04121995
883   ROMX_LOAD( "ps-41a.bin",    0x0000000, 0x080000, CRC(502224b6) SHA1(14df4f6c1e367ce097c11deae21566b4fe5647a9), ROM_BIOS(5) )
884
885   ROM_SYSTEM_BIOS( 5, "4.5A", "SCPH-101 (Version 4.5 05/25/00 A)" ) // 04121995
886   ROMX_LOAD( "psone-45a.bin", 0x0000000, 0x080000, CRC(171bdcec) SHA1(dcffe16bd90a723499ad46c641424981338d8378), ROM_BIOS(6) )
887ROM_END
888
889ROM_START( pse )
890   ROM_REGION32_LE( 0x080000, "user1", 0 )
891
892   ROM_SYSTEM_BIOS( 0, "2.0E", "DTL-H1002/SCPH-1002 (Version 2.0 05/10/95 E)" ) // 22091994
893   ROMX_LOAD( "ps-20e.bin",    0x0000000, 0x080000, CRC(9bb87c4b) SHA1(20b98f3d80f11cbf5a7bfd0779b0e63760ecc62c), ROM_BIOS(1) )
894
895   ROM_SYSTEM_BIOS( 1, "2.1E", "SCPH-1002/DTL-H1102 (Version 2.1 07/17/95 E)" ) // 22091994
896   ROMX_LOAD( "ps-21e.bin",    0x0000000, 0x080000, CRC(86c30531) SHA1(76cf6b1b2a7c571a6ad07f2bac0db6cd8f71e2cc), ROM_BIOS(2) )
897
898   ROM_SYSTEM_BIOS( 2, "2.2E", "SCPH-1002/DTL-H1202/DTL-H3002 (Version 2.2 12/04/95 E)" ) // 04121995
899   ROMX_LOAD( "ps-22e.bin",    0x0000000, 0x080000, CRC(1e26792f) SHA1(b6a11579caef3875504fcf3831b8e3922746df2c), ROM_BIOS(3) )
900
901   ROM_SYSTEM_BIOS( 3, "3.0E", "SCPH-5502/SCPH-5552 (Version 3.0 01/06/97 E)" ) // 04121995
902/*  ROMX_LOAD( "ps-30e.bad",    0x0000000, 0x080000, BAD_DUMP CRC(4d9e7c86) SHA1(f8de9325fc36fcfa4b29124d291c9251094f2e54), ROM_BIOS(4) ) */
903   ROMX_LOAD( "ps-30e.bin",    0x0000000, 0x080000, CRC(d786f0b9) SHA1(f6bc2d1f5eb6593de7d089c425ac681d6fffd3f0), ROM_BIOS(4) )
904
905   ROM_SYSTEM_BIOS( 4, "4.1E", "SCPH-7002/SCPH-7502/SCPH-9002 (Version 4.1 12/16/97 E)" ) // 04121995
906   ROMX_LOAD( "ps-41e.bin",    0x0000000, 0x080000, CRC(318178bf) SHA1(8d5de56a79954f29e9006929ba3fed9b6a418c1d), ROM_BIOS(5) )
907
908   ROM_SYSTEM_BIOS( 5, "4.4E", "SCPH-102 (Version 4.4 03/24/00 E)" ) // 04121995
909   ROMX_LOAD( "psone-44e.bin", 0x0000000, 0x080000, CRC(0bad7ea9) SHA1(beb0ac693c0dc26daf5665b3314db81480fa5c7c), ROM_BIOS(6) )
910
911   ROM_SYSTEM_BIOS( 6, "4.5E", "SCPH-102 (Version 4.5 05/25/00 E)" ) // 04121995
912   ROMX_LOAD( "psone-45e.bin", 0x0000000, 0x080000, CRC(76b880e5) SHA1(dbc7339e5d85827c095764fc077b41f78fd2ecae), ROM_BIOS(7) )
913ROM_END
914
915ROM_START( psa )
916   ROM_REGION32_LE( 0x080000, "user1", 0 )
917
918   ROM_SYSTEM_BIOS( 0, "3.0A", "SCPH-5501/SCPH-7003 (Version 3.0 11/18/96 A)" ) // 04121995
919   ROMX_LOAD( "ps-30a.bin",    0x0000000, 0x080000, CRC(8d8cb7e4) SHA1(0555c6fae8906f3f09baf5988f00e55f88e9f30b), ROM_BIOS(1) )
920
921   ROM_SYSTEM_BIOS( 1, "4.1A", "SCPH-7001/SCPH-7501/SCPH-7503/SCPH-9001 (Version 4.1 12/16/97 A)" ) // 04121995
922   ROMX_LOAD( "ps-41a.bin",    0x0000000, 0x080000, CRC(502224b6) SHA1(14df4f6c1e367ce097c11deae21566b4fe5647a9), ROM_BIOS(2) )
923ROM_END
924
925/*
926The version number & release date is stored in ascii text at the end of every bios, except for scph1000.
927There is also a BCD encoded date at offset 0x100, but this is set to 22091994 for versions prior to 2.2
928and 04121995 for all versions from 2.2 ( except Version 4.0J which is 29051997 ).
929
930Consoles not dumped:
931
932DTL-H1001H
933DTL-H3000
934SCPH-5001
935SCPH-5002
936SCPH-5003
937SCPH-5503
938SCPH-7500
939SCPH-9003
940SCPH-103
941
942Holes in version numbers:
943
944Version 2.0 J
945Version 4.1 J
946Version 4.2 J
947Version 4.4 J
948Version 4.5 J
949Version 4.0 A
950Version 4.2 A
951Version 4.3 A
952Version 4.4 A
953Version 4.0 E
954Version 4.2 E
955Version 4.3 E
956
957*/
958
959/*    YEAR  NAME    PARENT  COMPAT  MACHINE     INPUT   INIT    COMPANY                             FULLNAME                            FLAGS */
960CONS( 1994, psj,    0,      0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Japan)",         GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
961CONS( 1995, pse,    psj,    0,      psxpal,     psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Europe)",        GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
962CONS( 1995, psu,    psj,    0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (USA)",           GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
963CONS( 1995, psa,    psj,    0,      psxntsc,    psx,    psx,    "Sony Computer Entertainment Inc", "Sony PlayStation (Asia-Pacific)",  GAME_NOT_WORKING | GAME_IMPERFECT_SOUND | GAME_IMPERFECT_GRAPHICS )
src/mess/mess.mak
r11505r11506
526526   $(MAME_DRIVERS)/n64.o      \
527527   $(MAME_MACHINE)/aescart.o   \
528528   $(MAME_DRIVERS)/ng_aes.o   \
529   $(MAME_DRIVERS)/psx.o      \
530   $(MAME_MACHINE)/psxcd.o      \
531   $(MAME_MACHINE)/psxcddrv.o   \
532   $(MAME_MACHINE)/psxcard.o   \
529   $(MESS_DRIVERS)/psx.o      \
530   $(MESS_MACHINE)/psxcd.o      \
531   $(MESS_MACHINE)/psxcddrv.o   \
532   $(MESS_MACHINE)/psxcard.o   \
533533   $(MAME_DRIVERS)/saturn.o   \
534534   $(MAME_DRIVERS)/cps1.o   \
535535   $(MAME_VIDEO)/cps1.o   \

Previous 15994 Revisions Next


© 1998-2018 The MESS Team