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