workaround a problem with the harmattan gcc
[drnoksnes] / seta018.cpp
1 /*******************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  
4   (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and
5                             Jerremy Koot (jkoot@snes9x.com)
6
7   (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)
8
9   (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),
10                             funkyass (funkyass@spam.shaw.ca),
11                             Joel Yliluoma (http://iki.fi/bisqwit/)
12                             Kris Bleakley (codeviolation@hotmail.com),
13                             Matthew Kendora,
14                             Nach (n-a-c-h@users.sourceforge.net),
15                             Peter Bortas (peter@bortas.org) and
16                             zones (kasumitokoduck@yahoo.com)
17
18   C4 x86 assembler and some C emulation code
19   (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
20                             _Demo_ (_demo_@zsnes.com), and Nach
21
22   C4 C++ code
23   (c) Copyright 2003 Brad Jorsch
24
25   DSP-1 emulator code
26   (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
27                             John Weidman, neviksti (neviksti@hotmail.com),
28                             Kris Bleakley, Andreas Naive
29
30   DSP-2 emulator code
31   (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
32                      Lord Nightmare (lord_nightmare@users.sourceforge.net
33
34   OBC1 emulator code
35   (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and
36                             Kris Bleakley
37   Ported from x86 assembler to C by sanmaiwashi
38
39   SPC7110 and RTC C++ emulator code
40   (c) Copyright 2002 Matthew Kendora with research by
41                      zsKnight, John Weidman, and Dark Force
42
43   S-DD1 C emulator code
44   (c) Copyright 2003 Brad Jorsch with research by
45                      Andreas Naive and John Weidman
46  
47   S-RTC C emulator code
48   (c) Copyright 2001 John Weidman
49   
50   ST010 C++ emulator code
51   (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
52
53   Super FX x86 assembler emulator code 
54   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 
55
56   Super FX C emulator code 
57   (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
58
59
60   SH assembler code partly based on x86 assembler code
61   (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) 
62
63  
64   Specific ports contains the works of other authors. See headers in
65   individual files.
66  
67   Snes9x homepage: http://www.snes9x.com
68  
69   Permission to use, copy, modify and distribute Snes9x in both binary and
70   source form, for non-commercial purposes, is hereby granted without fee,
71   providing that this license information and copyright notice appear with
72   all copies and any derived work.
73  
74   This software is provided 'as-is', without any express or implied
75   warranty. In no event shall the authors be held liable for any damages
76   arising from the use of this software.
77  
78   Snes9x is freeware for PERSONAL USE only. Commercial users should
79   seek permission of the copyright holders first. Commercial use includes
80   charging money for Snes9x or software derived from Snes9x.
81  
82   The copyright holders request that bug fixes and improvements to the code
83   should be forwarded to them so everyone can benefit from the modifications
84   in future versions.
85  
86   Super NES and Super Nintendo Entertainment System are trademarks of
87   Nintendo Co., Limited and its subsidiary companies.
88 *******************************************************************************/
89 #include "memmap.h"
90 #include "seta.h"
91 //#include "port.h"
92
93 ST018_Regs ST018;
94
95 static int line;        // line counter
96
97 extern "C"{
98 uint8 S9xGetST018(uint32 Address)
99 {
100         uint8 t = 0;
101         uint16 address = (uint16) Address & 0xFFFF;
102
103         line++;
104
105         // these roles may be flipped
106         // op output
107         if (address == 0x3804)
108         {
109                 if (ST018.out_count)
110                 {
111                         t = (uint8) ST018.output [ST018.out_index];
112                         ST018.out_index++;
113                         if (ST018.out_count==ST018.out_index)
114                                 ST018.out_count=0;
115                 }
116                 else
117                         t = 0x81;
118         }
119         // status register
120         else if (address == 0x3800)
121                 t = ST018.status;
122         
123         printf( "ST018 R: %06X %02X\n", Address, t);
124
125         return t;
126 }
127
128 void S9xSetST018(uint8 Byte, uint32 Address)
129 {
130         uint16 address = (uint16) Address&0xFFFF;
131         static bool reset = false;
132
133         printf( "ST018 W: %06X %02X\n", Address, Byte );
134
135         line++;
136
137         if (!reset)
138         {
139                 // bootup values
140                 ST018.waiting4command = true;
141                 ST018.part_command = 0;
142                 reset = true;
143         }
144
145         Memory.SRAM[address]=Byte;
146
147         // default status for now
148         ST018.status = 0x00;
149
150         // op data goes through this address
151         if (address==0x3804)
152         {
153                 // check for new commands: 3 bytes length
154                 if(ST018.waiting4command && ST018.part_command==2)
155                 {
156                         ST018.waiting4command = false;
157                         ST018.command <<= 8;
158                         ST018.command |= Byte;
159                         ST018.in_index = 0;
160                         ST018.out_index = 0;
161                         ST018.part_command = 0; // 3-byte commands
162                         ST018.pass = 0; // data streams into the chip
163                         switch(ST018.command & 0xFFFFFF)
164                         {
165                         case 0x0100: ST018.in_count = 0; break;
166                         case 0xFF00: ST018.in_count = 0; break;
167                         default: ST018.waiting4command = true; break;
168                         }
169                 }
170                 else if(ST018.waiting4command)
171                 {
172                         // 3-byte commands
173                         ST018.part_command++;
174                         ST018.command <<= 8;
175                         ST018.command |= Byte;
176                 }
177         }
178         // extra parameters
179         else if (address==0x3802)
180         {
181                 ST018.parameters[ST018.in_index] = Byte;
182                 ST018.in_index++;
183         }
184
185         if (ST018.in_count==ST018.in_index)
186         {
187                 // Actually execute the command
188                 ST018.waiting4command = true;
189                 ST018.in_index = 0;
190                 ST018.out_index = 0;
191                 switch (ST018.command)
192                 {
193                 // hardware check?
194                 case 0x0100:
195                         ST018.waiting4command = false;
196                         ST018.pass++;
197                         if (ST018.pass==1)
198                         {
199                                 ST018.in_count = 1;
200                                 ST018.out_count = 2;
201
202                                 // Overload's research
203                                 ST018.output[0x00] = 0x81;
204                                 ST018.output[0x01] = 0x81;
205                         }
206                         else
207                         {
208                                 //ST018.in_count = 1;
209                                 ST018.out_count = 3;
210
211                                 // no reason to change this
212                                 //ST018.output[0x00] = 0x81;
213                                 //ST018.output[0x01] = 0x81;
214                                 ST018.output[0x02] = 0x81;
215
216                                 // done processing requests
217                                 if (ST018.pass==3)
218                                         ST018.waiting4command = true;
219                         }
220                         break;
221
222                 // unknown: feels like a security detection
223                 // format identical to 0x0100
224                 case 0xFF00:
225                         ST018.waiting4command = false;
226                         ST018.pass++;
227                         if (ST018.pass==1)
228                         {
229                                 ST018.in_count = 1;
230                                 ST018.out_count = 2;
231
232                                 // Overload's research
233                                 ST018.output[0x00] = 0x81;
234                                 ST018.output[0x01] = 0x81;
235                         }
236                         else
237                         {
238                                 //ST018.in_count = 1;
239                                 ST018.out_count = 3;
240
241                                 // no reason to change this
242                                 //ST018.output[0x00] = 0x81;
243                                 //ST018.output[0x01] = 0x81;
244                                 ST018.output[0x02] = 0x81;
245
246                                 // done processing requests
247                                 if (ST018.pass==3)
248                                         ST018.waiting4command = true;
249                         }
250                         break;
251                 }
252         }
253 }
254 }
255