Previous 15994 Revisions Next

r5570 Thursday 3rd September, 2009 at 23:21:35 UTC by MooglyGuy
Added missing files
[src/mame/machine]snesrtc.c* snessdd1.c*

src/mame/machine/snessdd1.c
r0r5570
1static UINT8 sdd1_read(running_machine* machine, UINT16 addr);
2
3typedef struct //Input Manager
4{
5   running_machine* machine;
6   UINT32 byte_ptr;
7   UINT8 bit_count;
8} SDD1_IM;
9
10static SDD1_IM* SDD1_IM_ctor(running_machine* machine)
11{
12   SDD1_IM* newclass = (SDD1_IM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_IM));
13   newclass->machine = machine;
14   return newclass;
15}
16
17static void SDD1_IM_prepareDecomp(SDD1_IM* this, UINT32 in_buf)
18{
19   this->byte_ptr = in_buf;
20   this->bit_count = 4;
21}
22
23static UINT8 SDD1_IM_getCodeword(SDD1_IM* this, const UINT8 code_len)
24{
25   UINT8 codeword;
26
27   codeword = sdd1_read(this->machine, this->byte_ptr) << this->bit_count;
28
29   ++this->bit_count;
30
31   if (codeword & 0x80)
32   {
33      codeword |= sdd1_read(this->machine, this->byte_ptr + 1) >> (9 - this->bit_count);
34      this->bit_count += code_len;
35   }
36
37   if (this->bit_count & 0x08)
38   {
39      this->byte_ptr++;
40      this->bit_count &= 0x07;
41   }
42
43   return codeword;
44}
45
46typedef struct //Golomb-Code Decoder
47{
48   running_machine* machine;
49   SDD1_IM* IM;
50} SDD1_GCD;
51
52static SDD1_GCD* SDD1_GCD_ctor(running_machine* machine, SDD1_IM* associatedIM)
53{
54   SDD1_GCD* newclass = (SDD1_GCD*)auto_alloc_array(machine, UINT8, sizeof(SDD1_GCD));
55   newclass->machine = machine;
56   newclass->IM = associatedIM;
57   return newclass;
58}
59
60static void SDD1_GCD_getRunCount(SDD1_GCD* this, UINT8 code_num, UINT8* MPScount, UINT8* LPSind)
61{
62   const UINT8 run_count[] =
63   {
64      0x00, 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x00,
65      0x07, 0x03, 0x05, 0x01, 0x06, 0x02, 0x04, 0x00,
66      0x0f, 0x07, 0x0b, 0x03, 0x0d, 0x05, 0x09, 0x01,
67      0x0e, 0x06, 0x0a, 0x02, 0x0c, 0x04, 0x08, 0x00,
68      0x1f, 0x0f, 0x17, 0x07, 0x1b, 0x0b, 0x13, 0x03,
69      0x1d, 0x0d, 0x15, 0x05, 0x19, 0x09, 0x11, 0x01,
70      0x1e, 0x0e, 0x16, 0x06, 0x1a, 0x0a, 0x12, 0x02,
71      0x1c, 0x0c, 0x14, 0x04, 0x18, 0x08, 0x10, 0x00,
72      0x3f, 0x1f, 0x2f, 0x0f, 0x37, 0x17, 0x27, 0x07,
73      0x3b, 0x1b, 0x2b, 0x0b, 0x33, 0x13, 0x23, 0x03,
74      0x3d, 0x1d, 0x2d, 0x0d, 0x35, 0x15, 0x25, 0x05,
75      0x39, 0x19, 0x29, 0x09, 0x31, 0x11, 0x21, 0x01,
76      0x3e, 0x1e, 0x2e, 0x0e, 0x36, 0x16, 0x26, 0x06,
77      0x3a, 0x1a, 0x2a, 0x0a, 0x32, 0x12, 0x22, 0x02,
78      0x3c, 0x1c, 0x2c, 0x0c, 0x34, 0x14, 0x24, 0x04,
79      0x38, 0x18, 0x28, 0x08, 0x30, 0x10, 0x20, 0x00,
80      0x7f, 0x3f, 0x5f, 0x1f, 0x6f, 0x2f, 0x4f, 0x0f,
81      0x77, 0x37, 0x57, 0x17, 0x67, 0x27, 0x47, 0x07,
82      0x7b, 0x3b, 0x5b, 0x1b, 0x6b, 0x2b, 0x4b, 0x0b,
83      0x73, 0x33, 0x53, 0x13, 0x63, 0x23, 0x43, 0x03,
84      0x7d, 0x3d, 0x5d, 0x1d, 0x6d, 0x2d, 0x4d, 0x0d,
85      0x75, 0x35, 0x55, 0x15, 0x65, 0x25, 0x45, 0x05,
86      0x79, 0x39, 0x59, 0x19, 0x69, 0x29, 0x49, 0x09,
87      0x71, 0x31, 0x51, 0x11, 0x61, 0x21, 0x41, 0x01,
88      0x7e, 0x3e, 0x5e, 0x1e, 0x6e, 0x2e, 0x4e, 0x0e,
89      0x76, 0x36, 0x56, 0x16, 0x66, 0x26, 0x46, 0x06,
90      0x7a, 0x3a, 0x5a, 0x1a, 0x6a, 0x2a, 0x4a, 0x0a,
91      0x72, 0x32, 0x52, 0x12, 0x62, 0x22, 0x42, 0x02,
92      0x7c, 0x3c, 0x5c, 0x1c, 0x6c, 0x2c, 0x4c, 0x0c,
93      0x74, 0x34, 0x54, 0x14, 0x64, 0x24, 0x44, 0x04,
94      0x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08,
95      0x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00,
96   };
97
98   UINT8 codeword = SDD1_IM_getCodeword(this->IM, code_num);
99
100   if (codeword & 0x80)
101   {
102      *LPSind = 1;
103      *MPScount = run_count[codeword >> (code_num ^ 0x07)];
104   }
105   else
106   {
107      *MPScount = (1 << code_num);
108   }
109}
110
111typedef struct // Bits Generator
112{
113   running_machine* machine;
114   UINT8 code_num;
115   UINT8 MPScount;
116   UINT8 LPSind;
117   SDD1_GCD* GCD;
118} SDD1_BG;
119
120static SDD1_BG* SDD1_BG_ctor(running_machine* machine, SDD1_GCD* associatedGCD, UINT8 code)
121{
122   SDD1_BG* newclass = (SDD1_BG*)auto_alloc_array(machine, UINT8, sizeof(SDD1_BG));
123   newclass->machine = machine;
124   newclass->code_num = code;
125   newclass->GCD = associatedGCD;
126   return newclass;
127}
128
129static void SDD1_BG_prepareDecomp(SDD1_BG* this)
130{
131   this->MPScount = 0;
132   this->LPSind = 0;
133}
134
135static UINT8 SDD1_BG_getBit(SDD1_BG* this, UINT8* endOfRun)
136{
137   UINT8 bit;
138
139   if (!(this->MPScount || this->LPSind))
140   {
141      SDD1_GCD_getRunCount(this->GCD, this->code_num, &(this->MPScount), &(this->LPSind));
142   }
143
144   if (this->MPScount)
145   {
146      bit = 0;
147      this->MPScount--;
148   }
149   else
150   {
151      bit = 1;
152      this->LPSind = 0;
153   }
154
155   if (this->MPScount || this->LPSind)
156   {
157      (*endOfRun) = 0;
158   }
159   else
160   {
161      (*endOfRun) = 1;
162   }
163
164   return bit;
165}
166
167
168typedef struct
169{
170   UINT8 code_num;
171   UINT8 nextIfMPS;
172   UINT8 nextIfLPS;
173} SDD1_PEM_state;
174
175SDD1_PEM_state SDD1_PEM_evolution_table[33] =
176{
177   { 0,25,25},
178   { 0, 2, 1},
179   { 0, 3, 1},
180   { 0, 4, 2},
181   { 0, 5, 3},
182   { 1, 6, 4},
183   { 1, 7, 5},
184   { 1, 8, 6},
185   { 1, 9, 7},
186   { 2,10, 8},
187   { 2,11, 9},
188   { 2,12,10},
189   { 2,13,11},
190   { 3,14,12},
191   { 3,15,13},
192   { 3,16,14},
193   { 3,17,15},
194   { 4,18,16},
195   { 4,19,17},
196   { 5,20,18},
197   { 5,21,19},
198   { 6,22,20},
199   { 6,23,21},
200   { 7,24,22},
201   { 7,24,23},
202   { 0,26, 1},
203   { 1,27, 2},
204   { 2,28, 4},
205   { 3,29, 8},
206   { 4,30,12},
207   { 5,31,16},
208   { 6,32,18},
209   { 7,24,22}
210};
211
212typedef struct
213{
214   UINT8 status;
215   UINT8 MPS;
216} SDD1_PEM_ContextInfo;
217
218typedef struct //Probability Estimation Module
219{
220   running_machine* machine;
221   SDD1_PEM_ContextInfo contextInfo[32];
222   SDD1_BG* BG[8];
223} SDD1_PEM;
224
225static SDD1_PEM* SDD1_PEM_ctor(running_machine* machine,
226                        SDD1_BG* associatedBG0, SDD1_BG* associatedBG1,
227                        SDD1_BG* associatedBG2, SDD1_BG* associatedBG3,
228                        SDD1_BG* associatedBG4, SDD1_BG* associatedBG5,
229                        SDD1_BG* associatedBG6, SDD1_BG* associatedBG7)
230{
231   SDD1_PEM* newclass = (SDD1_PEM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_PEM));
232   newclass->machine = machine;
233   newclass->BG[0] = associatedBG0;
234   newclass->BG[1] = associatedBG1;
235   newclass->BG[2] = associatedBG2;
236   newclass->BG[3] = associatedBG3;
237   newclass->BG[4] = associatedBG4;
238   newclass->BG[5] = associatedBG5;
239   newclass->BG[6] = associatedBG6;
240   newclass->BG[7] = associatedBG7;
241   return newclass;
242}
243
244static void SDD1_PEM_prepareDecomp(SDD1_PEM* this)
245{
246   UINT8 i;
247   for(i = 0; i < 32; i++)
248   {
249      this->contextInfo[i].status = 0;
250      this->contextInfo[i].MPS = 0;
251   }
252}
253
254static UINT8 SDD1_PEM_getBit(SDD1_PEM* this, UINT8 context)
255{
256   UINT8 endOfRun;
257   UINT8 bit;
258
259   SDD1_PEM_ContextInfo *pContInfo = &(this->contextInfo)[context];
260   UINT8 currStatus = pContInfo->status;
261   SDD1_PEM_state* pState = &(SDD1_PEM_evolution_table[currStatus]);
262   UINT8 currentMPS = pContInfo->MPS;
263
264   bit = SDD1_BG_getBit(this->BG[pState->code_num], &endOfRun);
265
266   if (endOfRun)
267   {
268      if (bit)
269      {
270         if (!(currStatus & 0xfe))
271         {
272            (pContInfo->MPS) ^= 0x01;
273         }
274         pContInfo->status = pState->nextIfLPS;
275      }
276      else
277      {
278         pContInfo->status = pState->nextIfMPS;
279      }
280   }
281
282   return bit ^ currentMPS;
283}
284
285typedef struct
286{
287   running_machine* machine;
288   UINT8 bitplanesInfo;
289   UINT8 contextBitsInfo;
290   UINT8 bit_number;
291   UINT8 currBitplane;
292   UINT16 prevBitplaneBits[8];
293   SDD1_PEM* PEM;
294} SDD1_CM;
295
296static SDD1_CM* SDD1_CM_ctor(running_machine* machine, SDD1_PEM* associatedPEM)
297{
298   SDD1_CM* newclass = (SDD1_CM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_CM));
299   newclass->machine = machine;
300   newclass->PEM = associatedPEM;
301   return newclass;
302}
303
304static void SDD1_CM_prepareDecomp(SDD1_CM* this, UINT32 first_byte)
305{
306   INT32 i = 0;
307   this->bitplanesInfo = sdd1_read(this->machine, first_byte) & 0xc0;
308   this->contextBitsInfo = sdd1_read(this->machine, first_byte) & 0x30;
309   this->bit_number = 0;
310   for (i = 0; i < 8; i++)
311   {
312      this->prevBitplaneBits[i] = 0;
313   }
314   switch(this->bitplanesInfo)
315   {
316      case 0x00:
317         this->currBitplane = 1;
318         break;
319      case 0x40:
320         this->currBitplane = 7;
321         break;
322      case 0x80:
323         this->currBitplane = 3;
324         break;
325   }
326}
327
328static UINT8 SDD1_CM_getBit(SDD1_CM* this)
329{
330   UINT8 currContext;
331   UINT16 *context_bits;
332   UINT8 bit = 0;
333
334   switch (this->bitplanesInfo)
335   {
336      case 0x00:
337         this->currBitplane ^= 0x01;
338         break;
339      case 0x40:
340         this->currBitplane ^= 0x01;
341         if (!(this->bit_number & 0x7f))
342         {
343            this->currBitplane = ((this->currBitplane + 2) & 0x07);
344         }
345         break;
346      case 0x80:
347         this->currBitplane ^= 0x01;
348         if (!(this->bit_number & 0x7f))
349         {
350            this->currBitplane ^= 0x02;
351         }
352         break;
353      case 0xc0:
354         this->currBitplane = this->bit_number & 0x07;
355         break;
356   }
357
358   context_bits = &(this->prevBitplaneBits)[this->currBitplane];
359
360   currContext = (this->currBitplane & 0x01) << 4;
361   switch (this->contextBitsInfo)
362   {
363      case 0x00:
364         currContext |= ((*context_bits & 0x01c0) >> 5) | (*context_bits & 0x0001);
365         break;
366      case 0x10:
367          currContext |= ((*context_bits & 0x0180) >> 5) | (*context_bits & 0x0001);
368         break;
369      case 0x20:
370         currContext |= ((*context_bits & 0x00c0) >> 5) | (*context_bits & 0x0001);
371         break;
372      case 0x30:
373         currContext |= ((*context_bits & 0x0180) >> 5) | (*context_bits & 0x0003);
374         break;
375   }
376
377   bit = SDD1_PEM_getBit(this->PEM, currContext);
378
379   *context_bits <<= 1;
380   *context_bits |= bit;
381
382   this->bit_number++;
383
384   return bit;
385}
386
387typedef struct
388{
389   running_machine* machine;
390   UINT8 bitplanesInfo;
391   UINT16 length;
392   UINT8* buffer;
393   SDD1_CM* CM;
394} SDD1_OL;
395
396static SDD1_OL* SDD1_OL_ctor(running_machine* machine, SDD1_CM* associatedCM)
397{
398   SDD1_OL* newclass = (SDD1_OL*)auto_alloc_array(machine, UINT8, sizeof(SDD1_OL));
399   newclass->machine = machine;
400   newclass->CM = associatedCM;
401   return newclass;
402}
403
404static void SDD1_OL_prepareDecomp(SDD1_OL* this, UINT32 first_byte, UINT16 out_len, UINT8 *out_buf)
405{
406   this->bitplanesInfo = sdd1_read(this->machine, first_byte) & 0xc0;
407   this->length = out_len;
408   this->buffer = out_buf;
409}
410
411static void SDD1_OL_launch(SDD1_OL* this)
412{
413   UINT8 i;
414   UINT8 register1, register2;
415
416   switch(this->bitplanesInfo)
417   {
418      case 0x00:
419      case 0x40:
420      case 0x80:
421         i = 1;
422         do
423         {   // if length == 0, we output 2^16 bytes
424            if(!i)
425            {
426               *(this->buffer++) = register2;
427               i = ~i;
428            }
429            else
430            {
431               for(register1 = register2 = 0, i = 0x80; i; i >>= 1)
432               {
433                  if(SDD1_CM_getBit(this->CM))
434                  {
435                     register1 |= i;
436                  }
437                  if(SDD1_CM_getBit(this->CM))
438                  {
439                     register2 |= i;
440                  }
441               }
442               *(this->buffer++) = register1;
443            }
444         } while(--(this->length));
445         break;
446      case 0xc0:
447         do
448         {
449            for(register1 = 0, i = 0x01; i; i <<= 1)
450            {
451               if(SDD1_CM_getBit(this->CM))
452               {
453                  register1 |= i;
454               }
455            }
456            *(this->buffer++) = register1;
457         } while(--(this->length));
458         break;
459   }
460}
461
462typedef struct
463{
464   running_machine* machine;
465   SDD1_IM* IM;
466   SDD1_GCD* GCD;
467   SDD1_BG* BG0;   SDD1_BG* BG1;   SDD1_BG* BG2;    SDD1_BG* BG3;
468   SDD1_BG* BG4;   SDD1_BG* BG5;   SDD1_BG* BG6;    SDD1_BG* BG7;
469   SDD1_PEM* PEM;
470   SDD1_CM* CM;
471   SDD1_OL* OL;
472} SDD1emu;
473
474static SDD1emu* SDD1emu_ctor(running_machine *machine)
475{
476   SDD1emu* newclass = (SDD1emu*)auto_alloc_array(machine, UINT8, sizeof(SDD1emu));
477   newclass->machine = machine;
478
479   newclass->IM = SDD1_IM_ctor(machine);
480   newclass->GCD = SDD1_GCD_ctor(machine, newclass->IM);
481   newclass->BG0 = SDD1_BG_ctor(machine, newclass->GCD, 0);
482   newclass->BG1 = SDD1_BG_ctor(machine, newclass->GCD, 1);
483   newclass->BG2 = SDD1_BG_ctor(machine, newclass->GCD, 2);
484   newclass->BG3 = SDD1_BG_ctor(machine, newclass->GCD, 3);
485   newclass->BG4 = SDD1_BG_ctor(machine, newclass->GCD, 4);
486   newclass->BG5 = SDD1_BG_ctor(machine, newclass->GCD, 5);
487   newclass->BG6 = SDD1_BG_ctor(machine, newclass->GCD, 6);
488   newclass->BG7 = SDD1_BG_ctor(machine, newclass->GCD, 7);
489   newclass->PEM = SDD1_PEM_ctor(machine, newclass->BG0, newclass->BG1, newclass->BG2, newclass->BG3,
490                                 newclass->BG4, newclass->BG5, newclass->BG6, newclass->BG7);
491   newclass->CM = SDD1_CM_ctor(machine, newclass->PEM);
492   newclass->OL = SDD1_OL_ctor(machine, newclass->CM);
493   return newclass;
494}
495
496static void SDD1emu_decompress(SDD1emu* this, UINT32 in_buf, UINT16 out_len, UINT8 *out_buf)
497{
498   SDD1_IM_prepareDecomp(this->IM, in_buf);
499   SDD1_BG_prepareDecomp(this->BG0);
500   SDD1_BG_prepareDecomp(this->BG1);
501   SDD1_BG_prepareDecomp(this->BG2);
502   SDD1_BG_prepareDecomp(this->BG3);
503   SDD1_BG_prepareDecomp(this->BG4);
504   SDD1_BG_prepareDecomp(this->BG5);
505   SDD1_BG_prepareDecomp(this->BG6);
506   SDD1_BG_prepareDecomp(this->BG7);
507   SDD1_PEM_prepareDecomp(this->PEM);
508   SDD1_CM_prepareDecomp(this->CM, in_buf);
509   SDD1_OL_prepareDecomp(this->OL, in_buf, out_len, out_buf);
510
511   SDD1_OL_launch(this->OL);
512}
513
514typedef struct
515{
516   UINT8 sdd1_enable;   // channel bit-mask
517   UINT8 xfer_enable;   // channel bit-mask
518   UINT32 mmc[4];      // memory map controller ROM indices
519
520   struct
521   {
522      UINT32 addr;   // $43x2-$43x4 -- DMA transfer address
523      UINT16 size;   // $43x5-$43x6 -- DMA transfer size
524   } dma[8];
525
526   SDD1emu* sdd1emu;
527   struct
528   {
529      UINT8 *data;   // pointer to decompressed S-DD1 data (65536 bytes)
530      UINT16 offset;   // read index into S-DD1 decompression buffer
531      UINT32 size;   // length of data buffer; reads decrement counter, set ready to false at 0
532      UINT8 ready;   // 1 when data[] is valid; 0 to invoke sdd1emu.decompress()
533   } buffer;
534} _snes_sdd1_t;
535
536_snes_sdd1_t snes_sdd1;
537
538static void sdd1_init(running_machine* machine)
539{
540   snes_sdd1.sdd1emu = SDD1emu_ctor(machine);
541}
542
543static UINT8 sdd1_mmio_read(running_machine* machine, UINT16 addr)
544{
545   addr &= 0xffff;
546
547   switch(addr)
548   {
549      case 0x4804:
550         return (snes_sdd1.mmc[0] >> 20) & 7;
551      case 0x4805:
552         return (snes_sdd1.mmc[1] >> 20) & 7;
553      case 0x4806:
554         return (snes_sdd1.mmc[2] >> 20) & 7;
555      case 0x4807:
556         return (snes_sdd1.mmc[3] >> 20) & 7;
557   }
558
559   return 0xff;
560}
561
562static void sdd1_mmio_write(running_machine* machine, UINT16 addr, UINT8 data)
563{
564   addr &= 0xffff;
565
566   if((addr & 0x4380) == 0x4300)
567   {
568      UINT8 channel = (addr >> 4) & 7;
569      switch(addr & 15)
570      {
571         case 2:
572            snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0xffff00) + (data <<  0);
573            break;
574         case 3:
575            snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0xff00ff) + (data <<  8);
576            break;
577         case 4:
578            snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0x00ffff) + (data << 16);
579            break;
580
581         case 5:
582            snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr &   0xff00) + (data <<  0);
583            break;
584         case 6:
585            snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr &   0x00ff) + (data <<  8);
586            break;
587      }
588      snes_ram[addr] = data;
589      return;
590   }
591
592   switch(addr)
593   {
594      case 0x4800:
595         snes_sdd1.sdd1_enable = data;
596         break;
597      case 0x4801:
598         snes_sdd1.xfer_enable = data;
599         break;
600
601      case 0x4804:
602         snes_sdd1.mmc[0] = (data & 7) << 20;
603         break;
604      case 0x4805:
605         snes_sdd1.mmc[1] = (data & 7) << 20;
606         break;
607      case 0x4806:
608         snes_sdd1.mmc[2] = (data & 7) << 20;
609         break;
610      case 0x4807:
611         snes_sdd1.mmc[3] = (data & 7) << 20;
612         break;
613   }
614}
615
616static UINT8 sdd1_read(running_machine* machine, UINT16 addr)
617{
618   if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable)
619   {
620      // at least one channel has S-DD1 decompression enabled...
621      UINT32 i;
622      for(i = 0; i < 8; i++)
623      {
624         if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable & (1 << i))
625         {
626            // S-DD1 always uses fixed transfer mode, so address will not change during transfer
627            if(addr == snes_sdd1.dma[i].addr)
628            {
629               UINT8 data;
630               if(!snes_sdd1.buffer.ready)
631               {
632                  UINT8 temp;
633                  // first byte read for channel performs full decompression.
634                  // this really should stream byte-by-byte, but it's not necessary since the size is known
635                  snes_sdd1.buffer.offset = 0;
636                  snes_sdd1.buffer.size = snes_sdd1.dma[i].size ? snes_sdd1.dma[i].size : 65536;
637
638                  // sdd1emu calls this function; it needs to access uncompressed data;
639                  // so temporarily disable decompression mode for decompress() call.
640                  temp = snes_sdd1.sdd1_enable;
641                  snes_sdd1.sdd1_enable = 0;
642                  SDD1emu_decompress(snes_sdd1.sdd1emu, addr, snes_sdd1.buffer.size, snes_sdd1.buffer.data);
643                  snes_sdd1.sdd1_enable = temp;
644
645                  snes_sdd1.buffer.ready = 1;
646               }
647
648               // fetch a decompressed byte; once buffer is depleted, disable channel and invalidate buffer
649               data = snes_sdd1.buffer.data[(UINT16)snes_sdd1.buffer.offset++];
650               if(snes_sdd1.buffer.offset >= snes_sdd1.buffer.size)
651               {
652                  snes_sdd1.buffer.ready = 0;
653                  snes_sdd1.xfer_enable &= ~(1 << i);
654               }
655
656               return data;
657            } // address matched
658         } // channel enabled
659      } // channel loop
660   } // S-DD1 decompressor enabled
661
662   return memory_read_byte(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), snes_sdd1.mmc[(addr >> 20) & 3] + (addr & 0x0fffff));
663}
664
665static void sdd1_reset(running_machine *machine)
666{
667}
src/mame/machine/snesrtc.c
r0r5570
1enum
2{
3   RTCM_Ready,
4   RTCM_Command,
5   RTCM_Read,
6   RTCM_Write
7};
8
9typedef struct
10{
11   UINT8 rtc_ram[13];
12   INT32 rtc_mode;
13   INT8 rtc_index;
14} _snes_rtc_t;
15
16_snes_rtc_t snes_rtc;
17
18const UINT8 srtc_months[12] =
19{
20   31, 28, 31,
21   30, 31, 30,
22   31, 31, 30,
23   31, 30, 31
24};
25
26static void srtc_update_time(running_machine *machine)
27{
28   mame_system_time curtime, *systime = &curtime;
29   mame_get_current_datetime(machine, &curtime);
30   snes_rtc.rtc_ram[0] = systime->local_time.second % 10;
31   snes_rtc.rtc_ram[1] = systime->local_time.second / 10;
32   snes_rtc.rtc_ram[2] = systime->local_time.minute % 10;
33   snes_rtc.rtc_ram[3] = systime->local_time.minute / 10;
34   snes_rtc.rtc_ram[4] = systime->local_time.hour % 10;
35   snes_rtc.rtc_ram[5] = systime->local_time.hour / 10;
36   snes_rtc.rtc_ram[6] = systime->local_time.mday % 10;
37   snes_rtc.rtc_ram[7] = systime->local_time.mday / 10;
38   snes_rtc.rtc_ram[8] = systime->local_time.month;
39   snes_rtc.rtc_ram[9] = (systime->local_time.year - 1900) % 10;
40   snes_rtc.rtc_ram[10] = ((systime->local_time.year - 1900) / 10) % 10;
41   snes_rtc.rtc_ram[11] = (systime->local_time.year - 1900) / 100;
42   snes_rtc.rtc_ram[12] = systime->local_time.weekday % 7;
43}
44
45// Returns day-of-week for specified date
46// e.g. 0 = Sunday, 1 = Monday, ... 6 = Saturday
47// Usage: weekday(2008, 1, 1) returns the weekday of January 1st, 2008
48static UINT8 srtc_weekday(UINT32 year, UINT32 month, UINT32 day)
49{
50   UINT32 y = 1900, m = 1;   // Epoch is 1900-01-01
51   UINT32 sum = 0;         // Number of days passed since epoch
52
53   year = MAX(1900, year);
54   month = MAX(1, MIN(12, month));
55   day = MAX(1, MIN(31, day));
56
57   while(y < year)
58   {
59      UINT8 leapyear = 0;
60      if((y % 4) == 0)
61      {
62         leapyear = 1;
63         if((y % 100) == 0 && (y % 400) != 0)
64         {
65            leapyear = 0;
66         }
67      }
68      sum += leapyear ? 366 : 365;
69      y++;
70   }
71
72   while(m < month)
73   {
74      UINT32 days = srtc_months[m - 1];
75      if(days == 28)
76      {
77         UINT8 leapyear = 0;
78         if((y % 4) == 0)
79         {
80            leapyear = 1;
81            if((y % 100) == 0 && (y % 400) != 0)
82            {
83               leapyear = 0;
84            }
85         }
86         days += leapyear ? 1 : 0;
87      }
88      sum += days;
89      m++;
90   }
91
92   sum += day - 1;
93   return (sum + 1) % 7; // 1900-01-01 was a Monday
94}
95
96static UINT8 srtc_mmio_read(running_machine *machine, UINT16 addr)
97{
98   addr &= 0xffff;
99
100   if(addr == 0x2800)
101   {
102      if(snes_rtc.rtc_mode != RTCM_Read)
103      {
104         return 0x00;
105      }
106
107      if(snes_rtc.rtc_index < 0)
108      {
109         srtc_update_time(machine);
110         snes_rtc.rtc_index++;
111         return 0x0f;
112      }
113      else if(snes_rtc.rtc_index > 12)
114      {
115         snes_rtc.rtc_index = -1;
116         return 0x0f;
117      }
118      else
119      {
120         return snes_rtc.rtc_ram[snes_rtc.rtc_index++];
121      }
122   }
123
124   return 0xff;
125}
126
127static void srtc_mmio_write(running_machine *machine, UINT16 addr, UINT8 data)
128{
129   addr &= 0xffff;
130
131   if(addr == 0x2801)
132   {
133      data &= 0x0f;   // Only the low four bits are used
134
135      if(data == 0x0d)
136      {
137         snes_rtc.rtc_mode = RTCM_Read;
138         snes_rtc.rtc_index = -1;
139         return;
140      }
141
142      if(data == 0x0e)
143      {
144         snes_rtc.rtc_mode = RTCM_Command;
145         return;
146      }
147
148      if(data == 0x0f)
149      {
150         return;   // Unknown behaviour
151      }
152
153      if(snes_rtc.rtc_mode == RTCM_Write)
154      {
155         if(snes_rtc.rtc_index >= 0 && snes_rtc.rtc_index < 12)
156         {
157            snes_rtc.rtc_ram[snes_rtc.rtc_index++] = data;
158
159            if(snes_rtc.rtc_index == 12)
160            {
161               // Day of week is automatically calculated and written
162               UINT8 day   = snes_rtc.rtc_ram[6] + snes_rtc.rtc_ram[7] * 10;
163               UINT8 month = snes_rtc.rtc_ram[8];
164               UINT8 year  = snes_rtc.rtc_ram[9] + snes_rtc.rtc_ram[10] * 10 + snes_rtc.rtc_ram[11] * 100;
165               year += 1000;
166
167               snes_rtc.rtc_ram[snes_rtc.rtc_index++] = srtc_weekday(year, month, day);
168            }
169         }
170      }
171      else if(snes_rtc.rtc_mode == RTCM_Command)
172      {
173         if(data == 0)
174         {
175            snes_rtc.rtc_mode = RTCM_Write;
176            snes_rtc.rtc_index = 0;
177         }
178         else if(data == 4)
179         {
180            UINT8 i;
181            snes_rtc.rtc_mode = RTCM_Ready;
182            snes_rtc.rtc_index = -1;
183            for(i = 0; i < 13; i++)
184            {
185               snes_rtc.rtc_ram[i] = 0;
186            }
187         }
188         else
189         {
190            // Unknown behaviour
191            snes_rtc.rtc_mode = RTCM_Ready;
192         }
193      }
194   }
195}
196
197static void srtc_reset(running_machine *machine)
198{
199   snes_rtc.rtc_mode = RTCM_Read;
200   snes_rtc.rtc_index = -1;
201   srtc_update_time(machine);
202}
203

Previous 15994 Revisions Next


© 1998-2018 The MESS Team