initial upstream import
[drnoksnes] / sa1.cpp
1 /*
2  * Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  *
4  * (c) Copyright 1996 - 2001 Gary Henderson (gary.henderson@ntlworld.com) and
5  *                           Jerremy Koot (jkoot@snes9x.com)
6  *
7  * Super FX C emulator code 
8  * (c) Copyright 1997 - 1999 Ivar (ivar@snes9x.com) and
9  *                           Gary Henderson.
10  * Super FX assembler emulator code (c) Copyright 1998 zsKnight and _Demo_.
11  *
12  * DSP1 emulator code (c) Copyright 1998 Ivar, _Demo_ and Gary Henderson.
13  * C4 asm and some C emulation code (c) Copyright 2000 zsKnight and _Demo_.
14  * C4 C code (c) Copyright 2001 Gary Henderson (gary.henderson@ntlworld.com).
15  *
16  * DOS port code contains the works of other authors. See headers in
17  * individual files.
18  *
19  * Snes9x homepage: http://www.snes9x.com
20  *
21  * Permission to use, copy, modify and distribute Snes9x in both binary and
22  * source form, for non-commercial purposes, is hereby granted without fee,
23  * providing that this license information and copyright notice appear with
24  * all copies and any derived work.
25  *
26  * This software is provided 'as-is', without any express or implied
27  * warranty. In no event shall the authors be held liable for any damages
28  * arising from the use of this software.
29  *
30  * Snes9x is freeware for PERSONAL USE only. Commercial users should
31  * seek permission of the copyright holders first. Commercial use includes
32  * charging money for Snes9x or software derived from Snes9x.
33  *
34  * The copyright holders request that bug fixes and improvements to the code
35  * should be forwarded to them so everyone can benefit from the modifications
36  * in future versions.
37  *
38  * Super NES and Super Nintendo Entertainment System are trademarks of
39  * Nintendo Co., Limited and its subsidiary companies.
40  */
41 #include "snes9x.h"
42 #include "ppu.h"
43 #include "cpuexec.h"
44
45 #include "sa1.h"
46
47 static void S9xSA1CharConv2 ();
48 static void S9xSA1DMA ();
49 static void S9xSA1ReadVariableLengthData (bool8 inc, bool8 no_shift);
50
51 void S9xSA1Init ()
52 {
53     SA1.NMIActive = FALSE;
54     SA1.IRQActive = FALSE;
55     SA1.WaitingForInterrupt = FALSE;
56     SA1.Waiting = FALSE;
57     SA1.Flags = 0;
58     SA1.Executing = FALSE;
59     memset (&Memory.FillRAM [0x2200], 0, 0x200);
60     Memory.FillRAM [0x2200] = 0x20;
61     Memory.FillRAM [0x2220] = 0x00;
62     Memory.FillRAM [0x2221] = 0x01;
63     Memory.FillRAM [0x2222] = 0x02;
64     Memory.FillRAM [0x2223] = 0x03;
65     Memory.FillRAM [0x2228] = 0xff;
66     SA1.op1 = 0;
67     SA1.op2 = 0;
68     SA1.arithmetic_op = 0;
69     SA1.sum = 0;
70     SA1.overflow = FALSE;
71 }
72
73 void S9xSA1Reset ()
74 {
75     SA1Registers.PB = 0;
76     SA1Registers.PC = Memory.FillRAM [0x2203] |
77                       (Memory.FillRAM [0x2204] << 8);
78     SA1Registers.D.W = 0;
79     SA1Registers.DB = 0;
80     SA1Registers.SH = 1;
81     SA1Registers.SL = 0xFF;
82     SA1Registers.XH = 0;
83     SA1Registers.YH = 0;
84     SA1Registers.P.W = 0;
85
86     SA1.ShiftedPB = 0;
87     SA1.ShiftedDB = 0;
88     SA1SetFlags (MemoryFlag | IndexFlag | IRQ | Emulation);
89     SA1ClearFlags (Decimal);
90
91     SA1.WaitingForInterrupt = FALSE;
92     SA1.PC = NULL;
93     SA1.PCBase = NULL;
94     S9xSA1SetPCBase (SA1Registers.PC);
95     //SA1.S9xOpcodes = S9xSA1OpcodesM1X1; // unused
96
97     S9xSA1UnpackStatus();
98     //S9xSA1FixCycles (); // unused
99     SA1.Executing = TRUE;
100     SA1.BWRAM = Memory.SRAM;
101     Memory.FillRAM [0x2225] = 0;
102 }
103
104 void S9xSA1SetBWRAMMemMap (uint8 val)
105 {
106     int c;
107
108     if (val & 0x80)
109     {
110         for (c = 0; c < 0x400; c += 16)
111         {
112             SA1.Map [c + 6] = SA1.Map [c + 0x806] = (uint8 *) CMemory::MAP_BWRAM_BITMAP2;
113             SA1.Map [c + 7] = SA1.Map [c + 0x807] = (uint8 *) CMemory::MAP_BWRAM_BITMAP2;
114             SA1.WriteMap [c + 6] = SA1.WriteMap [c + 0x806] = (uint8 *) CMemory::MAP_BWRAM_BITMAP2;
115             SA1.WriteMap [c + 7] = SA1.WriteMap [c + 0x807] = (uint8 *) CMemory::MAP_BWRAM_BITMAP2;
116         }
117         SA1.BWRAM = Memory.SRAM + (val & 0x7f) * 0x2000 / 4;
118     }
119     else
120     {
121         for (c = 0; c < 0x400; c += 16)
122         {
123             SA1.Map [c + 6] = SA1.Map [c + 0x806] = (uint8 *) CMemory::MAP_BWRAM;
124             SA1.Map [c + 7] = SA1.Map [c + 0x807] = (uint8 *) CMemory::MAP_BWRAM;
125             SA1.WriteMap [c + 6] = SA1.WriteMap [c + 0x806] = (uint8 *) CMemory::MAP_BWRAM;
126             SA1.WriteMap [c + 7] = SA1.WriteMap [c + 0x807] = (uint8 *) CMemory::MAP_BWRAM;
127         }
128         SA1.BWRAM = Memory.SRAM + (val & 7) * 0x2000;
129     }
130 }
131
132 void S9xFixSA1AfterSnapshotLoad ()
133 {
134     SA1.ShiftedPB = (uint32) SA1Registers.PB << 16;
135     SA1.ShiftedDB = (uint32) SA1Registers.DB << 16;
136
137     S9xSA1SetPCBase (SA1.ShiftedPB + SA1Registers.PC);
138     S9xSA1UnpackStatus ();
139     //S9xSA1FixCycles (); // unused
140     SA1.VirtualBitmapFormat = (Memory.FillRAM [0x223f] & 0x80) ? 2 : 4;
141     Memory.BWRAM = Memory.SRAM + (Memory.FillRAM [0x2224] & 7) * 0x2000;
142     S9xSA1SetBWRAMMemMap (Memory.FillRAM [0x2225]);
143
144     SA1.Waiting = (Memory.FillRAM [0x2200] & 0x60) != 0;
145     SA1.Executing = !SA1.Waiting;
146 }
147
148 uint8 S9xSA1GetByte (uint32 address)
149 {
150     uint8 *GetAddress = SA1.Map [(address >> MEMMAP_SHIFT) & MEMMAP_MASK];
151     if (GetAddress >= (uint8 *) CMemory::MAP_LAST)
152         return (*(GetAddress + (address & 0xffff)));
153
154     switch ((int) GetAddress)
155     {
156     case CMemory::MAP_PPU:
157         return (S9xGetSA1 (address & 0xffff));
158     case CMemory::MAP_LOROM_SRAM:
159     case CMemory::MAP_SA1RAM:
160         return (*(Memory.SRAM + (address & 0xffff)));
161     case CMemory::MAP_BWRAM:
162         return (*(SA1.BWRAM + ((address & 0x7fff) - 0x6000)));
163     case CMemory::MAP_BWRAM_BITMAP:
164         address -= 0x600000;
165         if (SA1.VirtualBitmapFormat == 2)
166             return ((Memory.SRAM [(address >> 2) & 0xffff] >> ((address & 3) << 1)) & 3);
167         else
168             return ((Memory.SRAM [(address >> 1) & 0xffff] >> ((address & 1) << 2)) & 15);
169     case CMemory::MAP_BWRAM_BITMAP2:
170         address = (address & 0xffff) - 0x6000;
171         if (SA1.VirtualBitmapFormat == 2)
172             return ((SA1.BWRAM [(address >> 2) & 0xffff] >> ((address & 3) << 1)) & 3);
173         else
174             return ((SA1.BWRAM [(address >> 1) & 0xffff] >> ((address & 1) << 2)) & 15);
175
176     case CMemory::MAP_DEBUG:
177     default:
178 #ifdef DEBUGGER
179 //      printf ("R(B) %06x\n", address);
180 #endif
181
182         return (0);
183     }
184 }
185
186 uint16 S9xSA1GetWord (uint32 address)
187 {
188     return (S9xSA1GetByte (address) | (S9xSA1GetByte (address + 1) << 8));
189 }
190
191 void S9xSA1SetByte (uint8 byte, uint32 address)
192 {
193     uint8 *Setaddress = SA1.WriteMap [(address >> MEMMAP_SHIFT) & MEMMAP_MASK];
194
195     if (Setaddress >= (uint8 *) CMemory::MAP_LAST)
196     {
197         *(Setaddress + (address & 0xffff)) = byte;
198         return;
199     }
200
201     switch ((int) Setaddress)
202     {
203     case CMemory::MAP_PPU:
204         S9xSetSA1 (byte, address & 0xffff);
205         return;
206     case CMemory::MAP_SA1RAM:
207     case CMemory::MAP_LOROM_SRAM:
208         *(Memory.SRAM + (address & 0xffff)) = byte;
209         return;
210     case CMemory::MAP_BWRAM:
211         *(SA1.BWRAM + ((address & 0x7fff) - 0x6000)) = byte;
212         return;
213     case CMemory::MAP_BWRAM_BITMAP:
214         address -= 0x600000;
215         if (SA1.VirtualBitmapFormat == 2)
216         {
217             uint8 *ptr = &Memory.SRAM [(address >> 2) & 0xffff];
218             *ptr &= ~(3 << ((address & 3) << 1));
219             *ptr |= (byte & 3) << ((address & 3) << 1);
220         }
221         else
222         {
223             uint8 *ptr = &Memory.SRAM [(address >> 1) & 0xffff];
224             *ptr &= ~(15 << ((address & 1) << 2));
225             *ptr |= (byte & 15) << ((address & 1) << 2);
226         }
227         break;
228     case CMemory::MAP_BWRAM_BITMAP2:
229         address = (address & 0xffff) - 0x6000;
230         if (SA1.VirtualBitmapFormat == 2)
231         {
232             uint8 *ptr = &SA1.BWRAM [(address >> 2) & 0xffff];
233             *ptr &= ~(3 << ((address & 3) << 1));
234             *ptr |= (byte & 3) << ((address & 3) << 1);
235         }
236         else
237         {
238             uint8 *ptr = &SA1.BWRAM [(address >> 1) & 0xffff];
239             *ptr &= ~(15 << ((address & 1) << 2));
240             *ptr |= (byte & 15) << ((address & 1) << 2);
241         }
242     default:
243         return;
244     }
245 }
246
247 void S9xSA1SetWord (uint16 Word, uint32 address)
248 {
249     S9xSA1SetByte ((uint8) Word, address);
250     S9xSA1SetByte ((uint8) (Word >> 8), address + 1);
251 }
252
253 void S9xSA1SetPCBase (uint32 address)
254 {
255     uint8 *GetAddress = SA1.Map [(address >> MEMMAP_SHIFT) & MEMMAP_MASK];
256     if (GetAddress >= (uint8 *) CMemory::MAP_LAST)
257     {
258         SA1.PCBase = GetAddress;
259         SA1.PC = GetAddress + (address & 0xffff);
260         return;
261     }
262
263     switch ((int) GetAddress)
264     {
265     case CMemory::MAP_PPU:
266         SA1.PCBase = Memory.FillRAM - 0x2000;
267         SA1.PC = SA1.PCBase + (address & 0xffff);
268         return;
269         
270     case CMemory::MAP_CPU:
271         SA1.PCBase = Memory.FillRAM - 0x4000;
272         SA1.PC = SA1.PCBase + (address & 0xffff);
273         return;
274         
275     case CMemory::MAP_DSP:
276         SA1.PCBase = Memory.FillRAM - 0x6000;
277         SA1.PC = SA1.PCBase + (address & 0xffff);
278         return;
279         
280     case CMemory::MAP_SA1RAM:
281     case CMemory::MAP_LOROM_SRAM:
282         SA1.PCBase = Memory.SRAM;
283         SA1.PC = SA1.PCBase + (address & 0xffff);
284         return;
285
286     case CMemory::MAP_BWRAM:
287         SA1.PCBase = SA1.BWRAM - 0x6000;
288         SA1.PC = SA1.PCBase + (address & 0xffff);
289         return;
290     case CMemory::MAP_HIROM_SRAM:
291         SA1.PCBase = Memory.SRAM - 0x6000;
292         SA1.PC = SA1.PCBase + (address & 0xffff);
293         return;
294
295     case CMemory::MAP_DEBUG:
296 #ifdef DEBUGGER
297         printf ("SBP %06x\n", address);
298 #endif
299         
300     default:
301     case CMemory::MAP_NONE:
302         SA1.PCBase = Memory.RAM;
303         SA1.PC = Memory.RAM + (address & 0xffff);
304         return;
305     }
306 }
307
308 void S9xSA1ExecuteDuringSleep ()
309 {
310 #if 0
311     if (SA1.Executing)
312     {
313         while (CPU.Cycles < CPU.NextEvent)
314         {
315             S9xSA1MainLoop ();
316             CPU.Cycles += TWO_CYCLES * 2;
317         }
318     }
319 #endif
320 }
321
322 void S9xSetSA1MemMap (uint32 which1, uint8 map)
323 {
324     int c;
325     int start = which1 * 0x100 + 0xc00;
326     int start2 = which1 * 0x200;
327
328     if (which1 >= 2)
329         start2 += 0x400;
330
331     for (c = 0; c < 0x100; c += 16)
332     {
333         uint8 *block = &Memory.ROM [(map & 7) * 0x100000 + (c << 12)];
334         int i;
335
336         for (i = c; i < c + 16; i++)
337             Memory.Map [start + i] = SA1.Map [start + i] = block;
338     }
339     
340     for (c = 0; c < 0x200; c += 16)
341     {
342         uint8 *block = &Memory.ROM [(map & 7) * 0x100000 + (c << 11) - 0x8000];
343         int i;
344
345         for (i = c + 8; i < c + 16; i++)
346             Memory.Map [start2 + i] = SA1.Map [start2 + i] = block;
347     }
348 }
349
350 uint8 S9xGetSA1 (uint32 address)
351 {
352 //      printf ("R: %04x\n", address);
353     switch (address)
354     {
355     case 0x2300:
356         return ((uint8) ((Memory.FillRAM [0x2209] & 0x5f) | 
357                  (CPU.IRQActive & (SA1_IRQ_SOURCE | SA1_DMA_IRQ_SOURCE))));
358     case 0x2301:
359         return ((Memory.FillRAM [0x2200] & 0xf) |
360                 (Memory.FillRAM [0x2301] & 0xf0));
361     case 0x2306:
362         return ((uint8)  SA1.sum);
363     case 0x2307:
364         return ((uint8) (SA1.sum >>  8));
365     case 0x2308:
366         return ((uint8) (SA1.sum >> 16));
367     case 0x2309:
368         return ((uint8) (SA1.sum >> 24));
369     case 0x230a:
370         return ((uint8) (SA1.sum >> 32));
371     case 0x230c:
372         return (Memory.FillRAM [0x230c]);
373     case 0x230d:
374     {
375         uint8 byte = Memory.FillRAM [0x230d];
376
377         if (Memory.FillRAM [0x2258] & 0x80)
378         {
379             S9xSA1ReadVariableLengthData (TRUE, FALSE);
380         }
381         return (byte);
382     }
383     default:    
384 #ifndef __GP32__    
385         printf ("R: %04x\n", address);
386 #endif  
387         break;
388     }
389     return (Memory.FillRAM [address]);
390 }
391
392 void S9xSetSA1 (uint8 byte, uint32 address)
393 {
394 //printf ("W: %02x -> %04x\n", byte, address);
395     switch (address)
396     {
397     case 0x2200:
398         SA1.Waiting = (byte & 0x60) != 0;
399 //      SA1.Executing = !SA1.Waiting && SA1.S9xOpcodes;
400
401         if (!(byte & 0x20) && (Memory.FillRAM [0x2200] & 0x20))
402         {
403             S9xSA1Reset ();
404         }
405         if (byte & 0x80)
406         {
407             Memory.FillRAM [0x2301] |= 0x80;
408             if (Memory.FillRAM [0x220a] & 0x80)
409             {
410                 SA1.Flags |= IRQ_PENDING_FLAG;
411                 SA1.IRQActive |= SNES_IRQ_SOURCE;
412                 SA1.Executing = !SA1.Waiting;// && SA1.S9xOpcodes; // unused
413             }
414         }
415         if (byte & 0x10)
416         {
417             Memory.FillRAM [0x2301] |= 0x10;
418 #ifdef DEBUGGER
419                 printf ("###SA1 NMI\n");
420 #endif
421             if (Memory.FillRAM [0x220a] & 0x10)
422             {
423             }
424         }
425         break;
426
427     case 0x2201:
428         if (((byte ^ Memory.FillRAM [0x2201]) & 0x80) &&
429             (Memory.FillRAM [0x2300] & byte & 0x80))
430         {
431             S9xSetIRQ (SA1_IRQ_SOURCE);
432         }
433         if (((byte ^ Memory.FillRAM [0x2201]) & 0x20) &&
434             (Memory.FillRAM [0x2300] & byte & 0x20))
435         {
436             S9xSetIRQ (SA1_DMA_IRQ_SOURCE);
437         }
438         break;
439     case 0x2202:
440         if (byte & 0x80)
441         {
442             Memory.FillRAM [0x2300] &= ~0x80;
443             S9xClearIRQ (SA1_IRQ_SOURCE);
444         }
445         if (byte & 0x20)
446         {
447             Memory.FillRAM [0x2300] &= ~0x20;
448             S9xClearIRQ (SA1_DMA_IRQ_SOURCE);
449         }
450         break;
451     case 0x2203:
452 //      printf ("SA1 reset vector: %04x\n", byte | (Memory.FillRAM [0x2204] << 8));
453         break;
454     case 0x2204:
455 //      printf ("SA1 reset vector: %04x\n", (byte << 8) | Memory.FillRAM [0x2203]);
456         break;
457
458     case 0x2205:
459 //      printf ("SA1 NMI vector: %04x\n", byte | (Memory.FillRAM [0x2206] << 8));
460         break;
461     case 0x2206:
462 //      printf ("SA1 NMI vector: %04x\n", (byte << 8) | Memory.FillRAM [0x2205]);
463         break;
464
465     case 0x2207:
466 //      printf ("SA1 IRQ vector: %04x\n", byte | (Memory.FillRAM [0x2208] << 8));
467         break;
468     case 0x2208:
469 //      printf ("SA1 IRQ vector: %04x\n", (byte << 8) | Memory.FillRAM [0x2207]);
470         break;
471
472     case 0x2209:
473         Memory.FillRAM [0x2209] = byte;
474         if (byte & 0x80)
475             Memory.FillRAM [0x2300] |= 0x80;
476
477         if (byte & Memory.FillRAM [0x2201] & 0x80)
478         {
479             S9xSetIRQ (SA1_IRQ_SOURCE);
480         }
481         break;
482     case 0x220a:
483         if (((byte ^ Memory.FillRAM [0x220a]) & 0x80) &&
484             (Memory.FillRAM [0x2301] & byte & 0x80))
485         {
486             SA1.Flags |= IRQ_PENDING_FLAG;
487             SA1.IRQActive |= SNES_IRQ_SOURCE;
488 //          SA1.Executing = !SA1.Waiting;
489         }
490         if (((byte ^ Memory.FillRAM [0x220a]) & 0x40) &&
491             (Memory.FillRAM [0x2301] & byte & 0x40))
492         {
493             SA1.Flags |= IRQ_PENDING_FLAG;
494             SA1.IRQActive |= TIMER_IRQ_SOURCE;
495 //          SA1.Executing = !SA1.Waiting;
496         }
497         if (((byte ^ Memory.FillRAM [0x220a]) & 0x20) &&
498             (Memory.FillRAM [0x2301] & byte & 0x20))
499         {
500             SA1.Flags |= IRQ_PENDING_FLAG;
501             SA1.IRQActive |= DMA_IRQ_SOURCE;
502 //          SA1.Executing = !SA1.Waiting;
503         }
504         if (((byte ^ Memory.FillRAM [0x220a]) & 0x10) &&
505             (Memory.FillRAM [0x2301] & byte & 0x10))
506         {
507 #ifdef DEBUGGER
508             printf ("###SA1 NMI\n");
509 #endif
510         }
511         break;
512     case 0x220b:
513         if (byte & 0x80)
514         {
515             SA1.IRQActive &= ~SNES_IRQ_SOURCE;
516             Memory.FillRAM [0x2301] &= ~0x80;
517         }
518         if (byte & 0x40)
519         {
520             SA1.IRQActive &= ~TIMER_IRQ_SOURCE;
521             Memory.FillRAM [0x2301] &= ~0x40;
522         }
523         if (byte & 0x20)
524         {
525             SA1.IRQActive &= ~DMA_IRQ_SOURCE;
526             Memory.FillRAM [0x2301] &= ~0x20;
527         }
528         if (byte & 0x10)
529         {
530             // Clear NMI
531             Memory.FillRAM [0x2301] &= ~0x10;
532         }
533         if (!SA1.IRQActive)
534             SA1.Flags &= ~IRQ_PENDING_FLAG;
535         break;
536     case 0x220c:
537 //      printf ("SNES NMI vector: %04x\n", byte | (Memory.FillRAM [0x220d] << 8));
538         break;
539     case 0x220d:
540 //      printf ("SNES NMI vector: %04x\n", (byte << 8) | Memory.FillRAM [0x220c]);
541         break;
542
543     case 0x220e:
544 //      printf ("SNES IRQ vector: %04x\n", byte | (Memory.FillRAM [0x220f] << 8));
545         break;
546     case 0x220f:
547 //      printf ("SNES IRQ vector: %04x\n", (byte << 8) | Memory.FillRAM [0x220e]);
548         break;
549
550     case 0x2210:
551 #if 0
552         printf ("Timer %s\n", (byte & 0x80) ? "linear" : "HV");
553         printf ("Timer H-IRQ %s\n", (byte & 1) ? "enabled" : "disabled");
554         printf ("Timer V-IRQ %s\n", (byte & 2) ? "enabled" : "disabled");
555 #endif
556         break;
557     case 0x2211:
558         printf ("Timer reset\n");
559         break;
560     case 0x2212:
561 #ifndef __GP32__        
562         printf ("H-Timer %04x\n", byte | (Memory.FillRAM [0x2213] << 8));
563 #endif  
564         break;
565     case 0x2213:
566 #ifndef __GP32__            
567         printf ("H-Timer %04x\n", (byte << 8) | Memory.FillRAM [0x2212]);
568 #endif          
569         break;
570     case 0x2214:
571 #ifndef __GP32__            
572         printf ("V-Timer %04x\n", byte | (Memory.FillRAM [0x2215] << 8));
573 #endif          
574         break;
575     case 0x2215:
576 #ifndef __GP32__            
577         printf ("V-Timer %04x\n", (byte << 8) | Memory.FillRAM [0x2214]);
578 #endif          
579         break;
580     case 0x2220:
581     case 0x2221:
582     case 0x2222:
583     case 0x2223:
584         S9xSetSA1MemMap (address - 0x2220, byte);
585 //      printf ("MMC: %02x\n", byte);
586         break;
587     case 0x2224:
588 //      printf ("BWRAM image SNES %02x -> 0x6000\n", byte);
589         Memory.BWRAM = Memory.SRAM + (byte & 7) * 0x2000;
590         break;
591     case 0x2225:
592 //      printf ("BWRAM image SA1 %02x -> 0x6000 (%02x)\n", byte, Memory.FillRAM [address]);
593         if (byte != Memory.FillRAM [address])
594             S9xSA1SetBWRAMMemMap (byte);
595         break;
596     case 0x2226:
597 //      printf ("BW-RAM SNES write %s\n", (byte & 0x80) ? "enabled" : "disabled");
598         break;
599     case 0x2227:
600 //      printf ("BW-RAM SA1 write %s\n", (byte & 0x80) ? "enabled" : "disabled");
601         break;
602
603     case 0x2228:
604 //      printf ("BW-RAM write protect area %02x\n", byte);
605         break;
606     case 0x2229:
607 //      printf ("I-RAM SNES write protect area %02x\n", byte);
608         break;
609     case 0x222a:
610 //      printf ("I-RAM SA1 write protect area %02x\n", byte);
611         break;
612     case 0x2230:
613 #if 0
614         printf ("SA1 DMA %s\n", (byte & 0x80) ? "enabled" : "disabled");
615         printf ("DMA priority %s\n", (byte & 0x40) ? "DMA" : "SA1");
616         printf ("DMA %s\n", (byte & 0x20) ? "char conv" : "normal");
617         printf ("DMA type %s\n", (byte & 0x10) ? "BW-RAM -> I-RAM" : "SA1 -> I-RAM");
618         printf ("DMA distination %s\n", (byte & 4) ? "BW-RAM" : "I-RAM");
619         printf ("DMA source %s\n", DMAsource [byte & 3]);
620 #endif
621         break;
622     case 0x2231:
623         if (byte & 0x80)
624             SA1.in_char_dma = FALSE;
625 #if 0
626         printf ("CHDEND %s\n", (byte & 0x80) ? "complete" : "incomplete");
627         printf ("DMA colour mode %d\n", byte & 3);
628         printf ("virtual VRAM width %d\n", (byte >> 2) & 7);
629 #endif
630         break;
631     case 0x2232:
632     case 0x2233:
633     case 0x2234:
634         Memory.FillRAM [address] = byte;
635 #if 0
636         printf ("DMA source start %06x\n", 
637                 Memory.FillRAM [0x2232] | (Memory.FillRAM [0x2233] << 8) |
638                 (Memory.FillRAM [0x2234] << 16));
639 #endif
640         break;
641     case 0x2235:
642         Memory.FillRAM [address] = byte;
643         break;
644     case 0x2236:
645         Memory.FillRAM [address] = byte;
646         if ((Memory.FillRAM [0x2230] & 0xa4) == 0x80)
647         {
648             // Normal DMA to I-RAM
649             S9xSA1DMA ();
650         }
651         else
652         if ((Memory.FillRAM [0x2230] & 0xb0) == 0xb0)
653         {
654             Memory.FillRAM [0x2300] |= 0x20;
655             if (Memory.FillRAM [0x2201] & 0x20)
656                 S9xSetIRQ (SA1_DMA_IRQ_SOURCE);
657             SA1.in_char_dma = TRUE;
658         }
659         break;
660     case 0x2237:
661         Memory.FillRAM [address] = byte;
662         if ((Memory.FillRAM [0x2230] & 0xa4) == 0x84)
663         {
664             // Normal DMA to BW-RAM
665             S9xSA1DMA ();
666         }
667 #if 0
668         printf ("DMA dest address %06x\n", 
669                 Memory.FillRAM [0x2235] | (Memory.FillRAM [0x2236] << 8) |
670                 (Memory.FillRAM [0x2237] << 16));
671 #endif
672         break;
673     case 0x2238:
674     case 0x2239:
675         Memory.FillRAM [address] = byte;
676 #if 0
677         printf ("DMA length %04x\n", 
678                 Memory.FillRAM [0x2238] | (Memory.FillRAM [0x2239] << 8));
679 #endif
680         break;
681     case 0x223f:
682         SA1.VirtualBitmapFormat = (byte & 0x80) ? 2 : 4;
683         //printf ("virtual VRAM depth %d\n", (byte & 0x80) ? 2 : 4);
684         break;
685
686     case 0x2240:    case 0x2241:    case 0x2242:    case 0x2243:
687     case 0x2244:    case 0x2245:    case 0x2246:    case 0x2247:
688     case 0x2248:    case 0x2249:    case 0x224a:    case 0x224b:
689     case 0x224c:    case 0x224d:    case 0x224e:
690 #if 0
691         if (!(SA1.Flags & TRACE_FLAG))
692         {
693             TraceSA1 ();
694             Trace ();
695         }
696 #endif
697         Memory.FillRAM [address] = byte;
698         break;
699
700     case 0x224f:
701         Memory.FillRAM [address] = byte;
702         if ((Memory.FillRAM [0x2230] & 0xb0) == 0xa0)
703         {
704             // Char conversion 2 DMA enabled
705             memmove (&Memory.ROM [CMemory::MAX_ROM_SIZE - 0x10000] + SA1.in_char_dma * 16,
706                      &Memory.FillRAM [0x2240], 16);
707             SA1.in_char_dma = (SA1.in_char_dma + 1) & 7;
708             if ((SA1.in_char_dma & 3) == 0)
709             {
710                 S9xSA1CharConv2 ();
711             }
712         }
713         break;
714     case 0x2250:
715         if (byte & 2)
716             SA1.sum = 0;
717         SA1.arithmetic_op = byte & 3;
718         break;
719     
720     case 0x2251:
721         SA1.op1 = (SA1.op1 & 0xff00) | byte;
722         break;
723     case 0x2252:
724         SA1.op1 = (SA1.op1 & 0xff) | (byte << 8);
725         break;
726     case 0x2253:
727         SA1.op2 = (SA1.op2 & 0xff00) | byte;
728         break;
729     case 0x2254:
730         SA1.op2 = (SA1.op2 & 0xff) | (byte << 8);
731         switch (SA1.arithmetic_op)
732         {
733         case 0: // multiply
734             SA1.sum = SA1.op1 * SA1.op2;
735             break;
736         case 1: // divide
737             if (SA1.op2 == 0)
738                 SA1.sum = SA1.op1 << 16;
739             else
740             {
741                 SA1.sum = (SA1.op1 / (int) ((uint16) SA1.op2)) |
742                           ((SA1.op1 % (int) ((uint16) SA1.op2)) << 16);
743             }
744             break;
745         case 2:
746         default: // cumulative sum
747             SA1.sum += SA1.op1 * SA1.op2;
748             if (SA1.sum & ((int64) 0xffffff << 32))
749                 SA1.overflow = TRUE;
750             break;
751         }
752         break;
753     case 0x2258:    // Variable bit-field length/auto inc/start.
754         Memory.FillRAM [0x2258] = byte;
755         S9xSA1ReadVariableLengthData (TRUE, FALSE);
756         return;
757     case 0x2259:
758     case 0x225a:
759     case 0x225b:    // Variable bit-field start address
760         Memory.FillRAM [address] = byte;
761         // XXX: ???
762         SA1.variable_bit_pos = 0;
763         S9xSA1ReadVariableLengthData (FALSE, TRUE);
764         return;
765     default:
766 //      printf ("W: %02x->%04x\n", byte, address);
767         break;
768     }
769     if (address >= 0x2200 && address <= 0x22ff)
770         Memory.FillRAM [address] = byte;
771 }
772
773 static void S9xSA1CharConv2 ()
774 {
775     uint32 dest = Memory.FillRAM [0x2235] | (Memory.FillRAM [0x2236] << 8);
776     uint32 offset = (SA1.in_char_dma & 7) ? 0 : 1;
777     int depth = (Memory.FillRAM [0x2231] & 3) == 0 ? 8 :
778                 (Memory.FillRAM [0x2231] & 3) == 1 ? 4 : 2;
779     int bytes_per_char = 8 * depth;
780     uint8 *p = &Memory.FillRAM [0x3000] + dest + offset * bytes_per_char;
781     uint8 *q = &Memory.ROM [CMemory::MAX_ROM_SIZE - 0x10000] + offset * 64;
782
783     switch (depth)
784     {
785     case 2:
786         break;
787     case 4:
788         break;
789     case 8:
790         for (int l = 0; l < 8; l++, q += 8)
791         {
792             for (int b = 0; b < 8; b++)
793             {
794                 uint8 r = *(q + b);
795                 *(p +  0) = (*(p +  0) << 1) | ((r >> 0) & 1);
796                 *(p +  1) = (*(p +  1) << 1) | ((r >> 1) & 1);
797                 *(p + 16) = (*(p + 16) << 1) | ((r >> 2) & 1);
798                 *(p + 17) = (*(p + 17) << 1) | ((r >> 3) & 1);
799                 *(p + 32) = (*(p + 32) << 1) | ((r >> 4) & 1);
800                 *(p + 33) = (*(p + 33) << 1) | ((r >> 5) & 1);
801                 *(p + 48) = (*(p + 48) << 1) | ((r >> 6) & 1);
802                 *(p + 49) = (*(p + 49) << 1) | ((r >> 7) & 1);
803             }
804             p += 2;
805         }
806         break;
807     }
808 }
809
810 static void S9xSA1DMA ()
811 {
812     uint32 src =  Memory.FillRAM [0x2232] |
813                  (Memory.FillRAM [0x2233] << 8) |
814                  (Memory.FillRAM [0x2234] << 16);
815     uint32 dst =  Memory.FillRAM [0x2235] |
816                  (Memory.FillRAM [0x2236] << 8) |
817                  (Memory.FillRAM [0x2237] << 16);
818     uint32 len =  Memory.FillRAM [0x2238] |
819                  (Memory.FillRAM [0x2239] << 8);
820
821     uint8 *s;
822     uint8 *d;
823
824     switch (Memory.FillRAM [0x2230] & 3)
825     {
826     case 0: // ROM
827         s = SA1.Map [(src >> MEMMAP_SHIFT) & MEMMAP_MASK];
828         if (s >= (uint8 *) CMemory::MAP_LAST)
829             s += (src & 0xffff);
830         else
831             s = Memory.ROM + (src & 0xffff);
832         break;
833     case 1: // BW-RAM
834         src &= CPU.Memory_SRAMMask;
835         len &= CPU.Memory_SRAMMask;
836         s = Memory.SRAM + src;
837         break;
838     default:
839     case 2:
840         src &= 0x3ff;
841         len &= 0x3ff;
842         s = &Memory.FillRAM [0x3000] + src;
843         break;
844     }
845
846     if (Memory.FillRAM [0x2230] & 4)
847     {
848         dst &= CPU.Memory_SRAMMask;
849         len &= CPU.Memory_SRAMMask;
850         d = Memory.SRAM + dst;
851     }
852     else
853     {
854         dst &= 0x3ff;
855         len &= 0x3ff;
856         d = &Memory.FillRAM [0x3000] + dst;
857     }
858     memmove (d, s, len);
859     Memory.FillRAM [0x2301] |= 0x20;
860     
861     if (Memory.FillRAM [0x220a] & 0x20)
862     {
863         SA1.Flags |= IRQ_PENDING_FLAG;
864         SA1.IRQActive |= DMA_IRQ_SOURCE;
865 //      SA1.Executing = !SA1.Waiting;
866     }
867 }
868
869 void S9xSA1ReadVariableLengthData (bool8 inc, bool8 no_shift)
870 {
871     uint32 addr =  Memory.FillRAM [0x2259] |
872                   (Memory.FillRAM [0x225a] << 8) |
873                   (Memory.FillRAM [0x225b] << 16);
874     uint8 shift = Memory.FillRAM [0x2258] & 15;
875
876     if (no_shift)
877         shift = 0;
878     else
879     if (shift == 0)
880         shift = 16;
881
882     uint8 s = shift + SA1.variable_bit_pos;
883
884     if (s >= 16)
885     {
886         addr += (s >> 4) << 1;
887         s &= 15;
888     }
889     uint32 data = S9xSA1GetWord (addr) |
890                   (S9xSA1GetWord (addr + 2) << 16);
891
892     data >>= s;
893     Memory.FillRAM [0x230c] = (uint8) data;
894     Memory.FillRAM [0x230d] = (uint8) (data >> 8);
895     if (inc)
896     {
897         SA1.variable_bit_pos = (SA1.variable_bit_pos + shift) & 15;
898         Memory.FillRAM [0x2259] = (uint8) addr;
899         Memory.FillRAM [0x225a] = (uint8) (addr >> 8);
900         Memory.FillRAM [0x225b] = (uint8) (addr >> 16);
901     }
902 }