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