cue/bin finally implemented
[picodrive.git] / platform / s60 / PicoDriveExe.Cpp
1 #include <stdlib.h>\r
2 #include <stdio.h>\r
3 #include <eikenv.h>\r
4 #include <e32keys.h>\r
5 #include <eikapp.h>\r
6 #include <zlib.h>\r
7 #ifdef S60V3\r
8 #include <picodrive.rsg>\r
9 #else\r
10 #include <picodriveS60.rsg>\r
11 #endif\r
12 #include <eikdoc.h>\r
13 #include <aknlistquerydialog.h>\r
14 #include <s32file.h>\r
15 #include <string.h>\r
16 #include <ctype.h>\r
17 #include <eikenv.h>\r
18 #include <sys\time.h>\r
19 \r
20 #include "PicoDriveexe.h"\r
21 #include "pico.h"\r
22 #include "unzip.h"\r
23 #include "PicoInt.h"\r
24 #include "GGenie.h"\r
25 \r
26 TInt KLineGap = 2;\r
27 static const char* KAboutText = \r
28 "This emulator uses code from\n"\r
29 "these people/projects:\n"\r
30 "\n"\r
31 "Dave\n"\r
32 "Cyclone 68000 core,\n"\r
33 "Pico emulation library\n"\r
34 "Homepage:http://www.finalburn.com\n"\r
35 "E-mail: david(at)finalburn.com\n"\r
36 "\n"\r
37 "notaz\n"\r
38 "UIQ port,Cyclone 68000 hacks,some\n"\r
39 "additional coding (see changelog).\n"\r
40 "Homepage:http://notaz.atspace.com/\n"\r
41 "E-mail: notasas(at)gmail.com\n"\r
42 "\n"\r
43 "Reesy\n"\r
44 "DrZ80, the Z80 emulator\n"\r
45 "written in ARM assembly.\n"\r
46 "Homepage: http://reesy.gp32x.de/\n"\r
47 "E-mail:drsms_reesy(at)yahoo.co.uk\n"\r
48 "\n"\r
49 "Tatsuyuki Satoh, Jarek Burczynski,\n"\r
50 "MultiArcadeMachineEmulator\n"\r
51 "development\n"\r
52 "software implementation of\n"\r
53 "Yamaha FM sound generator\n"\r
54 "\n"\r
55 "MultiArcadeMachineEmulator(MAME)\n"\r
56 "development\n"\r
57 "Texas Instruments SN76489/SN76496\n"\r
58 "programmable tone/noise generator\n"\r
59 "Homepage: http://www.mame.net/\n"\r
60 "\n"\r
61 "Additional thanks\n"\r
62 "-----------------\n"\r
63 "* Peter van Sebille for ECompXL\n"\r
64 "  and his various open-source\n"\r
65 "  Symbian project to learn from.\n"\r
66 "* Mark and Jean-loup for zlib\n"\r
67 "  library.\n"\r
68 "* Reesy for also finding some\n"\r
69 "  Cyclone bugs.\n"\r
70 "* Charles MacDonald\n"\r
71 "  (http://cgfm2.emuviews.com/)\n"\r
72 "  for old but still very useful\n"\r
73 "  info about genesis hardware.\n"\r
74 "* Stúphane Dallongeville\n"\r
75 "  for creating Gens\n"\r
76 "  (http://www.gens.ws)\n"\r
77 "*The development team behind the\n"\r
78 " Symbian GCC Improvement Project\n"\r
79 " http://www.inf.u-szeged.hu\n"\r
80 " /symbian-gcc/) for their updated\n"\r
81 " compiler tools.\n"\r
82 "* Inder for the icons.\n";\r
83 \r
84 // Picodrive prefrence uid\r
85 const TUid KPicoDrivePrefs={0x1234432E};\r
86 \r
87 // Bittable corresponding to the bitvalues for the different control actions\r
88 TUint16 KBitValTable[EPicoNoKeys]={1,2,4,8 ,64,16,32,1024,512,256,2048,128,5,9,10,6,0,0,0};\r
89 \r
90 extern"C" unsigned short *framebuff = 0;  // temporary buffer in sega native BGR format\r
91 const  int framebuffsize  = ((8+320)*(224+16))*2; // actual framebuffer size (in bytes+to support new rendering mode)\r
92 \r
93 // Colour lookuptable from BGR to RGB\r
94 unsigned short gColorMapTab[4096];\r
95 \r
96 // Scaling line table\r
97 TUint8 gColumnStepTable[320];\r
98 TUint8 gNarrowColumnStepTable[256];\r
99 unsigned short gLineTable[240];\r
100 TUint32 gLineOffsets[416];\r
101 TUint32 gFullOffset;\r
102 extern int PsndLen;\r
103 \r
104 #ifndef S60V3\r
105 GLDEF_C  TInt E32Dll(TDllReason)\r
106 {\r
107         return KErrNone;\r
108 }\r
109 #ifdef __WINS__\r
110 _LIT(KLitResourceFileName,      "z:\\system\\apps\\picodrives60\\PicoDriveS60.rsc");\r
111 #else\r
112 _LIT(KLitResourceFileName,      "PicoDriveS60.rsc");\r
113 #endif\r
114 \r
115 #endif\r
116 \r
117 #ifdef __WINS__\r
118 RHeap* gChunk;\r
119 #endif\r
120 struct Target Targ;\r
121 \r
122 #ifdef S60V3\r
123 #include "S60V3Video.inl"\r
124 #else\r
125 #include "NormalVideo.inl"\r
126 #include "InterpolateVideo.inl"\r
127 #endif\r
128 \r
129 TInt CPicoDriveUi::AsyncUpdateL(TAny* aAppUi)\r
130 {\r
131         static_cast<CPicoDriveUi*>(aAppUi)->UpdateScreen();\r
132         return 0;\r
133 }\r
134 \r
135 void CPicoDriveUi::StartAsynchUpdate() \r
136 {\r
137         TCallBack callback(AsyncUpdateL,iEikonEnv->EikAppUi());\r
138         iAsyncUpdate.Cancel();\r
139         iAsyncUpdate.Set(callback);\r
140         iAsyncUpdate.CallBack();\r
141 }\r
142 \r
143 \r
144 CPicoDriveUi::CPicoDriveUi():iIdleCallBack(CActive::EPriorityIdle),iStartUp(CActive::EPriorityIdle),\r
145 iAsyncUpdate(CActive::EPriorityStandard)\r
146 {\r
147         iCurrentScan=-1;\r
148         FramesPerSecond=60;\r
149         PicoOpt = 7;\r
150         iLastAboutPos = -1;\r
151         iFrameSkip = -1;\r
152         PsndRate = 8000;\r
153         iInterpolate = ETrue;\r
154         iSoundVolume = 6;\r
155         gFullOffset = 0;\r
156 }\r
157 \r
158 CPicoDriveUi::~CPicoDriveUi()\r
159 {\r
160         delete iKeyNames;\r
161         delete iRegNames;\r
162         if(iView)\r
163         {\r
164                 RemoveFromStack(iView);\r
165                 delete iView;\r
166         }\r
167         delete iBackBuffer;\r
168         delete iSndStream;\r
169 #ifndef S60V3\r
170         iCoeEnv->DeleteResourceFile(iResourceFileId);\r
171 #endif\r
172 \r
173 #ifdef __WINS__\r
174         if(gChunk != NULL)\r
175         {\r
176                 gChunk->Close();\r
177         }\r
178 #endif\r
179         free(framebuff);\r
180         framebuff = 0;\r
181         CloseSTDLIB();\r
182 }\r
183 \r
184 TKeyResponse CPicoDriveUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)\r
185 {\r
186         if(iCheatEnter)\r
187         {\r
188                 return iCheatDlg->OfferKeyEventL(aKeyEvent,aType);\r
189         }\r
190 \r
191         if(aType==EEventKey && aKeyEvent.iScanCode==EStdKeyBackspace)\r
192         {\r
193                 if(iPicoMenu ==ESelectSoundMenu)\r
194                 {\r
195                         if(iSndRateChanged)\r
196                         {\r
197                                 if(!UpdatePSndRate()) // Not compatible.. reset to 8000\r
198                                 {\r
199                                         PsndRate = 8000;\r
200                                         UpdatePSndRate();                                                                                       \r
201                                 }\r
202                                 \r
203                                 if(iRomLoaded)\r
204                                 {\r
205                                         sound_rerate();\r
206                                 }\r
207                         }\r
208 \r
209                         iView->Clear();\r
210                         iPicoMenu = EPicoMainMenu;\r
211                         PutMainMenu();\r
212                         return EKeyWasConsumed;\r
213                 }\r
214                 else if (iPicoMenu == ESelectControlsMenu)\r
215                 {\r
216                         iView->Clear();\r
217                         iPicoMenu = EPicoMainMenu;\r
218                         PutMainMenu();\r
219                         return EKeyWasConsumed;\r
220                 }\r
221                 else if(iPicoMenu == ESelectScrMenu)\r
222                 {\r
223                         iScrMode = iLastScrMode;\r
224                         iView->Clear();\r
225                         iPicoMenu = EPicoMainMenu;\r
226                         PutMainMenu();\r
227                         return EKeyWasConsumed;\r
228                 }\r
229                 else if(iPicoMenu == ESelectCheatMenu)\r
230                 {\r
231                         if(iCheatSelection<2)\r
232                         {\r
233                                 iView->Clear();\r
234                                 iPicoMenu = EPicoMainMenu;\r
235                                 PutMainMenu();\r
236                         }\r
237                         else\r
238                         {\r
239                                 TInt index = (iListOffset+iCheatSelection)-2;\r
240                                 TInt noFound = 0;\r
241                                 for(TInt i = 0; i < 256;i++)\r
242                                 {\r
243                                         if (Liste_GG[i].code[0] != 0)\r
244                                         {\r
245                                                 noFound++;\r
246                                                 if(noFound-1 == index)\r
247                                                 {\r
248                                                         Liste_GG[i].code[0] = 0;\r
249                                                         Liste_GG[i].active = 0;\r
250                                                         iNoCheats--;\r
251                                                         iListOffset = 0;\r
252                                                         iCheatSelection = 0;\r
253                                                         break;\r
254                                                 }\r
255                                                 \r
256                                         }\r
257                                 }\r
258 \r
259                                 iView->Clear();\r
260                                 PutCheatSelect();\r
261                         }\r
262 \r
263                         return EKeyWasConsumed;\r
264                 }\r
265 \r
266                 if(!iEmuRunning && (iPicoMenu!=EPicoMainMenu ||iCurrentScan!=-1) )\r
267                         return EKeyWasNotConsumed;\r
268                 \r
269                 if(iRomLoaded )\r
270                 {\r
271                         iEmuRunning=!iEmuRunning;\r
272                         \r
273                         if(iEmuRunning && iRomLoaded)\r
274                         {\r
275                                 iView->Clear();\r
276                                 TCallBack callback(StartEmulatorL,this);\r
277                                 iStartUp.Set(callback);\r
278                                 iStartUp.CallBack();\r
279                         }\r
280                 }\r
281                 return EKeyWasConsumed;\r
282         }\r
283 \r
284         if(!iEmuRunning)\r
285         {\r
286                 if(iCurrentScan==-1)\r
287                 {\r
288                         if(aType==EEventKey)\r
289                         {\r
290                                 \r
291                                 switch(aKeyEvent.iScanCode) // first determine bit value to change\r
292                                 {\r
293                                 case EStdKeyUpArrow:\r
294                                         switch(iPicoMenu)\r
295                                         {\r
296                                         case ESelectControlsMenu:\r
297                                                 {\r
298                                                         iCtrlSelection=!iCtrlSelection;\r
299                                                         PutControllerSelect();\r
300                                                 }break;\r
301                                         case EPicoMainMenu:\r
302                                                 if(iSelection>0)\r
303                                                         iSelection--;\r
304                                                 else iSelection=EExitPico;\r
305                                                 PutMainMenu();\r
306                                                 break;\r
307                                         case ESelectScrMenu:\r
308                                                 if(iScrMode>0)\r
309                                                         iScrMode--;\r
310                                                 else iScrMode=10;\r
311                                                 PutScreenSelect();\r
312                                                 break;\r
313                                         case ESelectCheatMenu:\r
314                                                         if(iCheatSelection>0)\r
315                                                                 iCheatSelection--;\r
316                                                         else\r
317                                                                 iCheatSelection = ELastCheatItem-1;                                             \r
318                                                         PutCheatSelect();\r
319                                                 break;\r
320                                         case EAboutPicoMenu:\r
321                                                 {\r
322                                                         iView->Clear();\r
323                                                         PutAbout();\r
324                                                 }break;\r
325                                         case ESelectSoundMenu:\r
326                                                 {\r
327                                                         if(iSndSelection>0)\r
328                                                                 iSndSelection--;\r
329                                                         else\r
330                                                                 iSndSelection = ELastSoundItem-1;                                               \r
331                                                         PutSoundSelect();\r
332                                                 }break;\r
333                                         }                                       \r
334                                 break;\r
335                                         case EStdKeyDownArrow:\r
336                                                 switch(iPicoMenu)\r
337                                                 {\r
338                                                 case ESelectControlsMenu:\r
339                                                                 {\r
340                                                                         iCtrlSelection=!iCtrlSelection;\r
341                                                                         PutControllerSelect();\r
342                                                                 }break;\r
343                                                 case EPicoMainMenu:\r
344                                                         iSelection++;\r
345                                                         if(iSelection==ELastMenuItem)\r
346                                                                 iSelection=0;\r
347                                                         PutMainMenu();\r
348                                                         break;\r
349                                                 case ESelectScrMenu:\r
350                                                         iScrMode++;\r
351                                                         if(iScrMode==11)\r
352                                                                 iScrMode=0;\r
353                                                         PutScreenSelect();\r
354                                                         break;\r
355                                                 case ESelectCheatMenu:\r
356                                                         if(iCheatSelection<(ELastCheatItem+iNoCheats-1) && iCheatSelection<6)\r
357                                                                 iCheatSelection++;\r
358                                                         else if(iCheatSelection == ELastCheatItem+4 && iListOffset<iNoCheats-5)\r
359                                                         {\r
360                                                                 iListOffset++;\r
361                                                         }\r
362                                                         else\r
363                                                         {\r
364                                                                 iListOffset = 0;\r
365                                                                 iCheatSelection = 0;                                            \r
366                                                         }\r
367                                                         PutCheatSelect();\r
368                                                 break;\r
369                                                         case EAboutPicoMenu:\r
370                                                         {\r
371                                                                 iView->Clear();\r
372                                                                 PutAbout();\r
373                                                         }break;\r
374                                                 case ESelectSoundMenu:\r
375                                                         {\r
376                                                                 iSndSelection++;\r
377                                                                 if(iSndSelection==ELastSoundItem)\r
378                                                                         iSndSelection=0;\r
379                                                                 PutSoundSelect();\r
380                                                         }break;\r
381                                                 }\r
382                                                 break;\r
383                                                 case EStdKeyDevice0:\r
384                                                 case EStdKeyDevice3:\r
385                                                         {\r
386                                                                 switch(iPicoMenu)\r
387                                                                 {\r
388                                                                 case ESelectControlsMenu:\r
389                                                                 {\r
390                                                                         if(iCtrlSelection == EConfigControls)\r
391                                                                         {\r
392                                                                                 iPicoMenu = EPicoMainMenu;\r
393                                                                                 iView->Clear();\r
394                                                                                 iCurrentScan=0;\r
395                                                                                 PutConfigKeys();\r
396                                                                         }\r
397                                                                         else\r
398                                                                         {\r
399                                                                                 iEnableSixButtons = !iEnableSixButtons;\r
400                                                                                 PicoOpt=PicoOpt^ 32;                                                                    \r
401                                                                                 \r
402                                                                                 PutControllerSelect();\r
403                                                                         }\r
404                                                                 }break;\r
405                                                                 case ESelectCheatMenu:\r
406                                                                         {\r
407                                                                                 switch(iCheatSelection)\r
408                                                                                 {\r
409                                                                                 case EAddCheat:\r
410                                                                                         {\r
411                                                                                                 TBuf8<16> cheatCode;\r
412                                                                                                 iCheatEnter = ETrue;\r
413                                                                                                 iCheatDlg = new (ELeave) CPicoAddCheatDlg(cheatCode);\r
414                                                                                                 iCheatDlg->SetMopParent(iEikonEnv->EikAppUi());            \r
415                                                                                                 TInt result = iCheatDlg->ExecuteLD(R_PICO_ADD_CHEAT);\r
416 \r
417                                                                                                 if(result == EEikBidOk)\r
418                                                                                                 {\r
419                                                                                                         for(TInt i = 0; i < 256;i++)\r
420                                                                                                         {\r
421                                                                                                                 if (Liste_GG[i].code[0] == 0)\r
422                                                                                                                 {                                                                                                               \r
423                                                                                                                         if(check_code((const char*) cheatCode.PtrZ(),i))\r
424                                                                                                                         {\r
425                                                                                                                                 decode( Liste_GG[i].code, (patch *) (&(Liste_GG[i].addr)));\r
426                                                                                                                                 \r
427                                                                                                                                 if ((Liste_GG[i].restore == 0xFFFFFFFF) && (Liste_GG[i].addr < Pico.romsize) && (iRomLoaded))\r
428                                                                                                                                 {\r
429                                                                                                                                         Liste_GG[i].restore = (unsigned int) (Pico.rom[Liste_GG[i].addr] & 0xFF);\r
430                                                                                                                                         Liste_GG[i].restore += (unsigned int) ((Pico.rom[Liste_GG[i].addr + 1] & 0xFF) << 8);\r
431                                                                                                                                 }\r
432                                                                                                                                 iNoCheats++;\r
433                                                                                                                                 Liste_GG[i].active = 1;\r
434                                                                                                                         }\r
435                                                                                                                         break; // Found position free\r
436                                                                                                                 }\r
437                                                                                                         }\r
438                                                                                                 }\r
439                                                                                                 iCheatDlg = NULL;\r
440                                                                                                 iCheatEnter = EFalse;\r
441                                                                                         }break;\r
442                                                                                 case EClearCheats:\r
443                                                                                         {\r
444                                                                                                 iNoCheats = 0;\r
445                                                                                                 for(TInt i = 0; i < 256;i++)\r
446                                                                                                 {\r
447                                                                                                         Liste_GG[i].code[0] = 0;\r
448                                                                                                         Liste_GG[i].active = 0;\r
449                                                                                                 }\r
450                                                                                         }\r
451                                                                                         break;\r
452                                                                                 default:\r
453                                                                                         {\r
454                                                                                                 TInt index = (iListOffset+iCheatSelection)-2;\r
455                                                                                                 TInt noFound = 0;\r
456                                                                                                 for(TInt i = 0; i < 256;i++)\r
457                                                                                                         {\r
458                                                                                                                 if (Liste_GG[i].code[0] != 0)\r
459                                                                                                                 {\r
460                                                                                                                         noFound++;\r
461                                                                                                                         if(noFound-1 == index)\r
462                                                                                                                         {\r
463                                                                                                                                 Liste_GG[i].active=!Liste_GG[i].active;\r
464                                                                                                                                 break;\r
465                                                                                                                         }\r
466 \r
467                                                                                                                 }\r
468                                                                                                 }\r
469                                                                                         }\r
470                                                                                         break;\r
471                                                                         \r
472                                                                                 }\r
473 \r
474                                                                                 PutCheatSelect();\r
475                                                                         }\r
476                                                                         break;\r
477                                                                 case EAboutPicoMenu:\r
478                                                                         {\r
479                                                                                 iView->Clear();\r
480                                                                                 PutAbout();\r
481                                                                         }break;\r
482                                                                 case ESelectScrMenu:\r
483                                                                         {\r
484 \r
485                                                                                 switch(iScrMode)\r
486                                                                                 {\r
487                                                                                 case 5:\r
488                                                                                         iInterpolate = !iInterpolate;\r
489                                                                                         iView->Clear();\r
490                                                                                         PutScreenSelect();\r
491                                                                                         break;\r
492                                                                                 case 6:\r
493                                                                                         iFrameSkip++;\r
494                                                                                         if(iFrameSkip == 11)\r
495                                                                                         {\r
496                                                                                                 iFrameSkip = -1;\r
497                                                                                         }\r
498                                                                                         iView->Clear();\r
499                                                                                         PutScreenSelect();\r
500                                                                                         break;\r
501                                                                                 case 7:\r
502                                                                                         PicoOpt = PicoOpt^0x40;\r
503                                                                                         iView->Clear();\r
504                                                                                         PutScreenSelect();\r
505                                                                                 break;\r
506                                                                                 case 8:\r
507                                                                                         PicoOpt = PicoOpt^0x80;\r
508                                                                                         iView->Clear();\r
509                                                                                         PutScreenSelect();\r
510                                                                                 break;\r
511                                                                                 case 9:\r
512                                                                                         PicoOpt = PicoOpt^0x10;\r
513                                                                                         iView->Clear();\r
514                                                                                         PutScreenSelect();\r
515                                                                                         break;\r
516                                                                                 case 10:\r
517                                                                                         switch(PicoRegionOverride)\r
518                                                                                         {\r
519                                                                                         case 0:\r
520                                                                                         default:\r
521                                                                                                 PicoRegionOverride = 1;\r
522                                                                                                 break;\r
523                                                                                         case 1:\r
524                                                                                                 PicoRegionOverride = 2;\r
525                                                                                                 break;\r
526                                                                                         case 2:\r
527                                                                                                 PicoRegionOverride = 4;\r
528                                                                                                 break;\r
529                                                                                         case 4:\r
530                                                                                                 PicoRegionOverride = 8;\r
531                                                                                                 break;\r
532                                                                                         case 8:\r
533                                                                                                 PicoRegionOverride = 0;\r
534                                                                                                 break;\r
535                                                                                         }\r
536 \r
537                                                                                         iView->Clear();\r
538                                                                                         PutScreenSelect();\r
539                                                                                         break;\r
540                                                                                 default:\r
541                                                                                         {\r
542                                                                                         TBitmapUtil util(iBackBuffer);\r
543                                                                                         util.Begin(TPoint(0,0));\r
544                                                                                         TSize sz=iBackBuffer->SizeInPixels();\r
545                                                                                         TInt dataSize=sz.iWidth*sz.iHeight*2;\r
546                                                                                         TPtr8 ptr(reinterpret_cast<unsigned char*>(iBackBuffer->DataAddress()),dataSize,dataSize);\r
547                                                                                         ptr.Fill(0);\r
548                                                                                         util.End();\r
549                                                                                         iPicoMenu=EPicoMainMenu;\r
550                                                                                         iView->Clear();\r
551                                                                                         PutMainMenu();\r
552                                                                                         TargetInit(); \r
553                                                                                         SaveSettingsL();\r
554                                                                                         }break;\r
555                                                                                 }                                                                       \r
556                                                                         }break;\r
557                                                                 case ESelectSoundMenu:\r
558                                                                         {\r
559                                                                                 switch(iSndSelection)\r
560                                                                                 {\r
561                                                                                 case EEnableZ80:\r
562                                                                                         PicoOpt=PicoOpt^4;\r
563                                                                                         break;\r
564                                                                                 case EEnableYM2612:\r
565                                                                                         PicoOpt=PicoOpt^1;\r
566                                                                                         break;\r
567                                                                                 case EEnableSN76496:\r
568                                                                                         PicoOpt=PicoOpt^2;\r
569                                                                                         break;\r
570                                                                                 case ESoundVolume:\r
571                                                                                         {                                                                               \r
572                                                                                                 iSoundVolume++;\r
573                                                                                                 if(iSoundVolume==11)\r
574                                                                                                 {\r
575                                                                                                         iSoundVolume=0;\r
576                                                                                                         iEnableSound=EFalse;\r
577                                                                                                 }\r
578                                                                                                 else\r
579                                                                                                 {\r
580                                                                                                         iEnableSound=ETrue;\r
581                                                                                                         iSndStream->SetVolume((iSndStream->MaxVolume()*iSoundVolume)/10);\r
582                                                                                                 }\r
583 \r
584                                                                                                 if(!iEnableSound)\r
585                                                                                                 {\r
586                                                                                                         iSndStream->Stop();\r
587                                                                                                 }\r
588                                                                                         }break;\r
589                                                                                 case ESoundRate:\r
590                                                                                         {\r
591                                                                                                 iSndRateChanged = ETrue;\r
592 \r
593                                                                                                 switch(PsndRate)\r
594                                                                                                 {\r
595                                                                                                 case 8000:\r
596                                                                                                         PsndRate = 11025;\r
597                                                                                                         break;\r
598                                                                                                 case 11025:\r
599                                                                                                         PsndRate = 16000;\r
600                                                                                                         break;\r
601                                                                                                 case 16000:\r
602                                                                                                         PsndRate = 22050;\r
603                                                                                                         break;\r
604                                                                                                 case 22050:\r
605                                                                                                         PsndRate = 8000;\r
606                                                                                                         break;\r
607                                                                                                 }                                                                                       \r
608                                                                                         }break;\r
609                                                                                 }       \r
610                                                                                 PutSoundSelect();\r
611                                                                         }break;\r
612                                                                 case EPicoMainMenu:\r
613                                                                         {\r
614                                                                                 switch(iSelection)\r
615                                                                                 {\r
616                                                                                 case EResetHw:\r
617                                                                                         {\r
618                                                                                                 if(!PicoReset(0))\r
619                                                                                                 {\r
620                                                                                                         iEmuRunning=ETrue;\r
621                                                                                                         TCallBack callback(StartEmulatorL,this);\r
622                                                                                                         iStartUp.Set(callback);\r
623                                                                                                         iStartUp.CallBack();\r
624                                                                                                 }\r
625                                                                                         }\r
626                                                                                         break;\r
627                                                                                 case ELoadState:\r
628                                                                                         {\r
629                                                                                                 if(iRomLoaded)\r
630                                                                                                 {                                                                                                       \r
631                                                                                                         saveLoadGame(1,0);                                                                                                      \r
632                                                                                                         iEmuRunning=ETrue;\r
633                                                                                                         iView->Clear();\r
634                                                                                                         TCallBack callback(StartEmulatorL,this);\r
635                                                                                                         iStartUp.Set(callback);\r
636                                                                                                         iStartUp.CallBack();                                                                            \r
637                                                                                                 }\r
638                                                                                         }\r
639                                                                                         break;\r
640                                                                                 case ESaveState:\r
641                                                                                         {                                                                                               \r
642                                                                                                 if(iRomLoaded)\r
643                                                                                                 {                                                                                                       \r
644                                                                                                         saveLoadGame(0,0);\r
645                                                                                                         iEmuRunning=ETrue;\r
646                                                                                                         iView->Clear();\r
647                                                                                                         TCallBack callback(StartEmulatorL,this);\r
648                                                                                                         iStartUp.Set(callback);\r
649                                                                                                         iStartUp.CallBack();\r
650                                                                                                         \r
651                                                                                                 }\r
652                                                                                         }\r
653                                                                                         break;\r
654                                                                                 case ELoadRom:\r
655                                                                                         {\r
656                                                                                                 TParsePtr parse(iRomName);\r
657                                                                                                 iRomName=parse.DriveAndPath();\r
658                                                                                                 if(SelectFile(iRomName))\r
659                                                                                                 {\r
660                                                                                                         SaveSettingsL();\r
661                                                                                                         EmulateExit();\r
662                                                                                                         TPtr8 ptr((unsigned char*)RomName,256);\r
663                                                                                                         ptr.Fill(0,256);\r
664                                                                                                         ptr.Copy(iRomName);\r
665                                                                                                         User::CompressAllHeaps();\r
666                                                                                                         if(EmulateInit()==0)\r
667                                                                                                         {\r
668                                                                                                                 if(Pico.m.pal)\r
669                                                                                                                 {\r
670                                                                                                                         FramesPerSecond=50;\r
671                                                                                                                 }\r
672                                                                                                                 else \r
673                                                                                                                 {\r
674                                                                                                                         FramesPerSecond=60;\r
675                                                                                                                 }\r
676                                                                                                                 \r
677                                                                                                                 SetKeyBlockMode(ENoKeyBlock);\r
678                                                                                                                 iEmuRunning=ETrue;\r
679                                                                                                                 iRomLoaded=ETrue;\r
680                                                                                                                 iView->Clear();\r
681                                                                                                                 TCallBack callback(StartEmulatorL,this);\r
682                                                                                                                 iStartUp.Set(callback);\r
683                                                                                                                 iStartUp.CallBack();\r
684                                                                                                         }\r
685                                                                                                         else\r
686                                                                                                         {\r
687                                                                                                                 iRomName=KNullDesC();\r
688                                                                                                                 iView->Clear();\r
689                                                                                                                 PutMainMenu();\r
690                                                                                                                 iRomLoaded=EFalse;\r
691                                                                                                         }\r
692                                                                                                 }\r
693                                                                                                 else\r
694                                                                                                 {\r
695                                                                                                         iView->Clear();\r
696                                                                                                         PutMainMenu();\r
697                                                                                                 }\r
698                                                                                         }\r
699                                                                                         break;//load rom\r
700                                                                                 case ESetControls:\r
701                                                                                         iPicoMenu = ESelectControlsMenu;\r
702                                                                                         PutControllerSelect();                                                                          \r
703                                                                                         break;\r
704                                                                                 case ESetScreen:\r
705                                                                                         iView->Clear();\r
706                                                                                         iLastScrMode = iScrMode; // In case of cancel\r
707                                                                                         PutScreenSelect();\r
708                                                                                         iPicoMenu=ESelectScrMenu;\r
709                                                                                         break;\r
710                                                                                 case ESelectSound:\r
711                                                                                         {                       \r
712                                                                                                 iSndRateChanged = EFalse;\r
713                                                                                                 iPicoMenu=ESelectSoundMenu;\r
714                                                                                                 PutSoundSelect();\r
715                                                                                         }break;\r
716                                                                                 case ESelectCheat:\r
717                                                                                         {\r
718                                                                                         iPicoMenu=ESelectCheatMenu;\r
719                                                                                         iListOffset = 0;\r
720                                                                                         iNoCheats = 0;\r
721                                                                                         for(TInt i = 0; i < 256;i++)\r
722                                                                                         {\r
723                                                                                                 if (Liste_GG[i].code[0] != 0)\r
724                                                                                                 {\r
725                                                                                                         iNoCheats++;\r
726                                                                                                         if ((Liste_GG[i].restore != 0xFFFFFFFF) && (Liste_GG[i].addr < Pico.romsize) && (iRomLoaded))\r
727                                                                                                         {\r
728                                                                                                                 Pico.rom[Liste_GG[i].addr] = (unsigned char)(Liste_GG[i].restore & 0xFF);\r
729                                                                                                                 Pico.rom[Liste_GG[i].addr + 1] = (unsigned char)((Liste_GG[i].restore & 0xFF00) >> 8);\r
730                                                                                                         }\r
731                                                                                                         \r
732                                                                                                 }\r
733                                                                                         }\r
734 \r
735                                                                                         PutCheatSelect();\r
736                                                                                         }\r
737                                                                                 break;\r
738                                                                                 case EAboutPico:\r
739                                                                                         {\r
740                                                                                                 iView->Clear();\r
741                                                                                                 PutAbout();\r
742                                                                                                 iPicoMenu=EAboutPicoMenu;\r
743                                                                                         }break;\r
744                                                                                 case EExitPico:\r
745                                                                                         {\r
746                                                                                                 SaveSettingsL();\r
747                                                                                                 EmulateExit();\r
748                                                                                                 Exit();\r
749                                                                                         }\r
750                                                                                         break;\r
751                                                         }\r
752                                                                 }\r
753                                                 }break;\r
754                                         }\r
755                                         break;\r
756                                 }\r
757                         }\r
758                 }\r
759                 else\r
760                 {\r
761                         if(aType == EEventKeyDown)\r
762                         {\r
763                                 if(aKeyEvent.iScanCode != EStdKeyBackspace)\r
764                                 {\r
765                                         iScanCodes[iCurrentScan]=aKeyEvent.iScanCode;\r
766                                 }\r
767                                 else\r
768                                 {\r
769                                         iScanCodes[iCurrentScan] = KErrNotFound;\r
770                                 }\r
771 \r
772                                 iCurrentScan++;\r
773                                 if(iCurrentScan==iKeyNames->Count())\r
774                                 {\r
775                                         SaveSettingsL();\r
776                                         iCurrentScan=-1;\r
777                                         iView->DrawText(_L("Done!"),TPoint(0,iFontHeight*11));\r
778                                         \r
779                                         User::After(1000000);\r
780                                         iView->Clear();\r
781                                         PutMainMenu();\r
782                                 }\r
783                                 else\r
784                                 {\r
785                                         iView->Clear();\r
786                                         PutConfigKeys();\r
787                                 }\r
788                         }\r
789                 }\r
790         }\r
791         else\r
792         {\r
793                 if((aType == EEventKeyUp || aType == EEventKeyDown))\r
794                 {                                               \r
795                         TUint16 bitVal=0;\r
796                         for(TInt loop=0;loop<16;loop++)\r
797                         {\r
798                                 if(aKeyEvent.iScanCode==iScanCodes[loop])\r
799                                 {\r
800                                         bitVal=KBitValTable[loop];\r
801                                         break;\r
802                                 }\r
803                         }\r
804                         \r
805                         if(aType == EEventKeyUp)\r
806                         {\r
807                                 iPad1=iPad1&(65535-bitVal); // remove bit\r
808                         }\r
809                         else \r
810                         {\r
811                                 iPad1=(iPad1|bitVal); // set bit\r
812                         }                       \r
813                 }\r
814 \r
815                 if(aType == EEventKey)\r
816                 {\r
817                         if(aKeyEvent.iScanCode == iScanCodes[EPicoResetKey])\r
818                         {\r
819                                 PicoReset(0);\r
820                         }\r
821 \r
822                         if(aKeyEvent.iScanCode == iScanCodes[EPicoPanLKey] && gFullOffset>0)\r
823                         {\r
824                                 gFullOffset-=24;\r
825                         }\r
826                         else if(aKeyEvent.iScanCode == iScanCodes[EPicoPanRKey]&& gFullOffset<144)\r
827                         {\r
828                                 gFullOffset+=24;\r
829                         }\r
830                 }\r
831         }\r
832         \r
833         return EKeyWasConsumed;\r
834 }\r
835 \r
836 TInt CPicoDriveUi::SelectFile(TFileName& aFileName)\r
837 {\r
838         TFileName filename=aFileName;\r
839         TInt selectedIndex=-1;\r
840         RArray<TEntry> romList;\r
841         TDriveList driveList;\r
842         TBool refresh=EFalse;\r
843         TInt lastLength=-1;\r
844         do\r
845         {\r
846                 CAknListQueryDialog* dlg = new (ELeave) CAknListQueryDialog(&selectedIndex);\r
847                 CDesCArrayFlat* list=new (ELeave) CDesCArrayFlat(5);\r
848                 refresh=EFalse;\r
849                 if(filename.Length()==0)\r
850                 {\r
851                         \r
852                         iEikonEnv->FsSession().DriveList(driveList);\r
853                         for(TInt drive=0;drive<driveList.Length();drive++)\r
854                         {\r
855                                 if(driveList[drive]!=0)\r
856                                 {\r
857                                         TBuf<16> form;\r
858                                         form.Format(_L("%c:\\"),drive+65);\r
859                                         list->AppendL(form);\r
860                                 }\r
861                         }       \r
862                 }\r
863                 else\r
864                 {\r
865                         CDir* romDir = NULL;;\r
866                         romList.Reset();\r
867                         iEikonEnv->FsSession().GetDir(filename,KEntryAttMatchMask,0,romDir);\r
868                         \r
869                         if(romDir!=NULL)\r
870                                 list->AppendL(_L(".. <DIR>"));\r
871                         \r
872                         if(romDir!=NULL && romDir->Count()>0)\r
873                         {\r
874                                 for(TInt loop=0;loop<romDir->Count();loop++)\r
875                                 {\r
876                                         const TEntry& entry=(*romDir)[loop];\r
877                                         TFileName name=entry.iName;\r
878                                         if(entry.IsDir())\r
879                                         {\r
880                                                 name.Append(_L(" <DIR>"));\r
881                                                 list->AppendL(name);\r
882                                                 romList.Append(entry);\r
883                                         }\r
884                                         else\r
885                                         {\r
886                                                 TParsePtr parse(name);\r
887                                                 \r
888                                                 if(parse.Ext().CompareF(_L(".bin"))==KErrNone ||\r
889                                                         parse.Ext().CompareF(_L(".smd"))==KErrNone ||\r
890                                                         parse.Ext().CompareF(_L(".zip"))==KErrNone)\r
891                                                 {\r
892                                                         romList.Append(entry);\r
893                                                         list->AppendL(name);\r
894                                                 }\r
895                                         }\r
896                                         \r
897                                 }\r
898                                 delete romDir;\r
899                                 romDir=NULL;\r
900                         }\r
901                         else // no files found.. or path not found.. return to\r
902                         {\r
903                                 if(filename.Length()>3) // more than a c:\ specified\r
904                                 {\r
905                                         \r
906                                         refresh=ETrue;\r
907                                         filename=KNullDesC();\r
908                                 }\r
909                                 else return EFalse;\r
910                         }\r
911                         \r
912                 }\r
913                 if(list->Count()>0)\r
914                 {\r
915                         dlg->PrepareLC(R_PICO_FILE_SELECT_DIALOG);\r
916                         dlg->SetItemTextArray(list);\r
917                         if(dlg->RunLD())\r
918                         {\r
919                                 if(filename.Length()==0)\r
920                                 {\r
921                                         TFileName driveLetter;\r
922                                         TUint8 driveL;\r
923                                         TInt countedDrives=0;\r
924                                         for(TInt drive=0;drive<driveList.Length();drive++)\r
925                                         {\r
926                                                 if(driveList[drive]!=0 && countedDrives==selectedIndex)\r
927                                                 {\r
928                                                         driveLetter.Format(_L("%c:\\"),drive+65);\r
929                                                         driveL=drive+65;\r
930                                                         break;\r
931                                                 }\r
932                                                 else if(driveList[drive]!=0) countedDrives++;\r
933                                                 \r
934                                         }\r
935                                         filename=driveLetter;\r
936                                         refresh=ETrue;                                  \r
937                                 }\r
938                                 else\r
939                                 {\r
940                                         if(selectedIndex>0)\r
941                                         {\r
942                                                 selectedIndex--;\r
943                                                 const TEntry& entry=romList[selectedIndex];\r
944                                                 TFileName name =entry.iName;\r
945                                                 if(entry.IsDir())\r
946                                                 {\r
947                                                         lastLength=aFileName.Length();\r
948                                                         filename.Append(name);\r
949                                                         filename.Append(_L("\\"));\r
950                                                         \r
951                                                         refresh=ETrue;\r
952                                                         \r
953                                                 }\r
954                                                 else\r
955                                                 {\r
956                                                         filename.Append(name);\r
957                                                         aFileName=filename;\r
958                                                         romList.Close();\r
959                                                         return ETrue;\r
960                                                 }\r
961                                         }\r
962                                         else\r
963                                         {\r
964                                                 refresh=ETrue;\r
965                                                 TInt pos=filename.Left(filename.Length()-1).LocateReverse('\\');\r
966                                                 if(pos!=KErrNotFound)\r
967                                                         filename=filename.Left(pos+1);// keep \r
968                                                 else filename=KNullDesC();\r
969                                                 // and changefilename..\r
970                                                 \r
971                                                 //return 2;// go up one.\r
972                                         }\r
973                                 }\r
974                         }\r
975                         else\r
976                         {\r
977                                 romList.Close();\r
978                                 return EFalse;\r
979                         }\r
980                 }\r
981         }while(refresh);\r
982         romList.Close();\r
983         return EFalse;\r
984 }\r
985 \r
986 void CPicoDriveUi::SaveSettingsL()\r
987 {\r
988 #ifdef S60V3\r
989         CDictionaryStore* prefs = Application()->OpenIniFileLC(iEikonEnv->FsSession());\r
990 #else\r
991         CDictionaryFileStore* prefs=CDictionaryFileStore::OpenLC(iEikonEnv->FsSession(),iAppPath,TUid::Uid(0));\r
992 #endif\r
993         ExternalizeL(*prefs);\r
994         prefs->CommitL();\r
995         CleanupStack::PopAndDestroy();//close prefs\r
996 }\r
997 \r
998 \r
999 void CPicoDriveUi::ExternalizeL(CDictionaryStore& aStore) \r
1000 {       \r
1001         RDictionaryWriteStream writeStream;\r
1002         writeStream.AssignLC(aStore, KPicoDrivePrefs);\r
1003         \r
1004         TInt loop=0;\r
1005         for(loop=0;loop<EPicoNoKeys;loop++)\r
1006         {\r
1007                 writeStream.WriteInt32L(iScanCodes[loop]);\r
1008         }\r
1009         \r
1010         writeStream.WriteInt32L(iScrMode);\r
1011         writeStream.WriteInt32L(PicoOpt);\r
1012         writeStream.WriteInt32L(iSoundVolume);\r
1013         writeStream.WriteInt32L(iFrameSkip);\r
1014         writeStream.WriteInt32L(PsndRate);\r
1015         writeStream.WriteInt32L(iInterpolate);\r
1016         writeStream.WriteInt32L(iEnableSixButtons);\r
1017         writeStream.WriteInt32L(PicoRegionOverride);\r
1018         TParsePtr parser(iRomName);\r
1019         writeStream.WriteInt32L(parser.DriveAndPath().Length());\r
1020         writeStream.WriteL(parser.DriveAndPath(),parser.DriveAndPath().Length());\r
1021         writeStream.CommitL();\r
1022         \r
1023         CleanupStack::PopAndDestroy();//writeStream\r
1024 }\r
1025 \r
1026 void CPicoDriveUi::InternalizeL(const CDictionaryStore& aStore)\r
1027 {\r
1028         if (!aStore.IsPresentL(KPicoDrivePrefs))\r
1029         {\r
1030                 iFirstStart = ETrue;\r
1031                 return;\r
1032         }\r
1033         RDictionaryReadStream   readStream;\r
1034         readStream.OpenLC(aStore, KPicoDrivePrefs);\r
1035         TInt loop=0;\r
1036         for(loop=0;loop<EPicoNoKeys;loop++)\r
1037         {\r
1038                 iScanCodes[loop] = readStream.ReadInt32L();\r
1039         }\r
1040         \r
1041         iScrMode=readStream.ReadInt32L();\r
1042         PicoOpt=readStream.ReadInt32L();\r
1043         iEnableSound = PicoOpt&3;\r
1044         iSoundVolume=readStream.ReadInt32L();\r
1045         iFrameSkip = readStream.ReadInt32L();\r
1046         PsndRate = readStream.ReadInt32L();\r
1047         iInterpolate = readStream.ReadInt32L();\r
1048         iEnableSixButtons = readStream.ReadInt32L();\r
1049         PicoRegionOverride = readStream.ReadInt32L();\r
1050 \r
1051         TInt len=0;\r
1052         TRAPD(err,len=readStream.ReadInt32L())\r
1053                 if(err==KErrNone)\r
1054                 {\r
1055                         readStream.ReadL(iRomName,len);\r
1056                 }\r
1057                 \r
1058                 CleanupStack::PopAndDestroy();//readStream\r
1059 }\r
1060 \r
1061 void CPicoDriveUi::ConstructL()\r
1062 {\r
1063 #ifdef S60V3\r
1064         CAknAppUi::BaseConstructL(CAknAppUi::EAknEnableSkin);\r
1065 #else\r
1066         BaseConstructL(ENoAppResourceFile);     \r
1067 #endif\r
1068         framebuff =  (unsigned short *) malloc(framebuffsize);\r
1069         memset(framebuff,0,framebuffsize);\r
1070 \r
1071         iFontHeight = iEikonEnv->NormalFont()->HeightInPixels()+KLineGap;\r
1072         iScanCodes[0]= EStdKeyUpArrow;                  \r
1073         iScanCodes[1]= EStdKeyDownArrow;\r
1074         iScanCodes[2]=EStdKeyLeftArrow;\r
1075         iScanCodes[3]= EStdKeyRightArrow;\r
1076         iScanCodes[6]=EStdKeyDevice0;\r
1077         iScanCodes[4]= EStdKeyDevice1;\r
1078         iScanCodes[5]=EStdKeyDevice3;\r
1079         iScanCodes[7]=0;//x\r
1080         iScanCodes[8]=0; // y\r
1081         iScanCodes[9]=0;//z\r
1082         iScanCodes[10]=0;// mode\r
1083         iScanCodes[11]= '0'; // start\r
1084 #ifdef __WINS__\r
1085         gChunk = UserHeap::ChunkHeap(&_L("ROMHEAP"),512000,16384000);\r
1086 #endif\r
1087 \r
1088 #ifndef S60V3\r
1089         TFileName name;\r
1090         iEikonEnv->RootWin().SetName(_L("PicoDrive"));\r
1091 #ifndef __WINS__\r
1092         RProcess process;\r
1093         process.Rename(_L("PicoDrive"));\r
1094         TFileName fname  =process.FileName();\r
1095         TParsePtr parser(fname);\r
1096         name.Append(parser.DriveAndPath());\r
1097 #endif\r
1098         name.Append(KLitResourceFileName());\r
1099         iAppPath=_L("C:");\r
1100         iAppPath.Append(TParsePtr(name).Path());\r
1101         iAppPath.Append(_L("PicoDriveS60.ini"));\r
1102         iResourceFileId = iCoeEnv->AddResourceFileL(name); // eb205: needs to hunt around drives\r
1103 #endif // S60V3\r
1104         iKeyNames =iEikonEnv->ReadDesCArrayResourceL(R_PICODRIVE_KEYS);\r
1105         iRegNames = iEikonEnv->ReadDesCArrayResourceL(R_PICODRIVE_REGIONS);\r
1106 \r
1107         iEikonEnv->FsSession().MkDirAll(TParsePtr(iAppPath).DriveAndPath());\r
1108 #ifdef S60V3\r
1109         CDictionaryStore* prefs = Application()->OpenIniFileLC(iEikonEnv->FsSession());\r
1110         InternalizeL(*prefs);\r
1111         CleanupStack::PopAndDestroy();//close prefs\r
1112 #else\r
1113         TRAPD(err,{CDictionaryFileStore* prefs=CDictionaryFileStore::OpenLC(iEikonEnv->FsSession(),iAppPath,TUid::Uid(0));\r
1114         InternalizeL(*prefs);\r
1115         CleanupStack::PopAndDestroy();//close prefs\r
1116         });\r
1117 #endif\r
1118         \r
1119         if(iFirstStart)\r
1120         {\r
1121                 iPicoMenu = EAboutPicoMenu;\r
1122         }\r
1123         \r
1124         iView=new (ELeave)CQPicoDriveView;\r
1125         iView->ConstructL();\r
1126         AddToStackL(iView);\r
1127         iDisplayMode =iEikonEnv->ScreenDevice()->DisplayMode();\r
1128         if(iDisplayMode != EColor64K && iDisplayMode != EColor4K)\r
1129         {\r
1130                 iDisplayMode=EColor64K;; // Also tried to switch to by the view.\r
1131         }\r
1132         CalculatePaletteTable();\r
1133         iBackBuffer= new (ELeave)CFbsBitmap;\r
1134         iBackBuffer->Create(iEikonEnv->ScreenDevice()->SizeInPixels(),iDisplayMode);\r
1135         TBitmapUtil util(iBackBuffer);\r
1136         util.Begin(TPoint(0,0));\r
1137         TSize sz=iBackBuffer->SizeInPixels();\r
1138         Targ.view = TRect(TPoint(0,0),sz);\r
1139         TInt dataSize=sz.iWidth*sz.iHeight*2;\r
1140         Targ.scanline_length = sz.iWidth*2;\r
1141         Targ.screen_offset = Targ.scanline_length*(sz.iHeight-1);\r
1142         TPtr8 ptr(reinterpret_cast<unsigned char*>(iBackBuffer->DataAddress()),dataSize,dataSize);\r
1143         ptr.Fill(0);\r
1144         util.End();\r
1145         SetKeyBlockMode(ENoKeyBlock);\r
1146         iSelection=0;\r
1147         \r
1148         iSndStream = CMdaAudioOutputStream::NewL(*this);\r
1149         iAudioSettings.Query();\r
1150         iAudioSettings.iSampleRate = TMdaAudioDataSettings::ESampleRate8000Hz;\r
1151         iAudioSettings.iChannels = TMdaAudioDataSettings::EChannelsMono;\r
1152         iAudioSettings.iFlags = 0;\r
1153         iAudioSettings.iVolume = iAudioSettings.iMaxVolume/2;\r
1154         iSndStream->Open(&iAudioSettings);\r
1155         CActiveScheduler::Start(); // wait for open\r
1156 }\r
1157 \r
1158 void CPicoDriveUi::PutAbout(TBool iOnlyRedraw)\r
1159 {\r
1160         TPtrC8 charPtr((unsigned char*)KAboutText,strlen(KAboutText));\r
1161         HBufC* credits = HBufC::NewLC(charPtr.Length());\r
1162         credits->Des().Copy(charPtr);\r
1163         \r
1164         if(iLastAboutPos>=credits->Length())\r
1165         {\r
1166                 iLastAboutPos = -1;\r
1167                 iView->Clear();\r
1168                 iPicoMenu = EPicoMainMenu;\r
1169                 PutMainMenu();\r
1170         }\r
1171         else\r
1172         {\r
1173                 if(iLastAboutPos == -1)\r
1174                         iLastAboutPos = 0;\r
1175                 iView->DrawText(_L("PicoDrive S60 Credits"),TPoint(0,0));\r
1176                 if(iOnlyRedraw && iLastAboutPos == 0)\r
1177                         iView->DrawTextInRect(*credits,TRect(0,iFontHeight*2,Targ.view.iBr.iX,Targ.view.iBr.iY),iLastAboutPos);\r
1178                 else\r
1179                         iLastAboutPos = iView->DrawTextInRect(*credits,TRect(0,iFontHeight*2,Targ.view.iBr.iX,Targ.view.iBr.iY),iLastAboutPos);\r
1180         }\r
1181         CleanupStack::PopAndDestroy(credits);\r
1182         \r
1183 }\r
1184 void CPicoDriveUi::PutMainMenu()\r
1185 {\r
1186         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1187         iView->DrawText(_L("by Dave et Co"),TPoint(0,iFontHeight*1));\r
1188         \r
1189         iView->DrawText(_L("Load ROM"),TPoint(0,iFontHeight*3),iSelection==0);\r
1190         iView->DrawText(_L("Load state"),TPoint(0,iFontHeight*4),iSelection==1);\r
1191         iView->DrawText(_L("Save state"),TPoint(0,iFontHeight*5),iSelection==2);\r
1192         iView->DrawText(_L("Configure controls"),TPoint(0,iFontHeight*6),iSelection==3);\r
1193         iView->DrawText(_L("Configure screen"),TPoint(0,iFontHeight*7),iSelection==4);\r
1194         iView->DrawText(_L("Configure sound"),TPoint(0,iFontHeight*8),iSelection==5);\r
1195         iView->DrawText(_L("Game Genie/Cheats"),TPoint(0,iFontHeight*9),iSelection==6);\r
1196 \r
1197         iView->DrawText(_L("Reset"),TPoint(0,iFontHeight*10),iSelection==7);\r
1198         \r
1199         iView->DrawText(_L("Credits"),TPoint(0,iFontHeight*11),iSelection==8);\r
1200         iView->DrawText(_L("Exit"),TPoint(0,iFontHeight*12),iSelection==9);\r
1201         if(iRomName.Length()>0)\r
1202         {\r
1203                 iView->DrawText(TParsePtr(iRomName).Name(),TPoint(0,iFontHeight*13));\r
1204         }\r
1205         else\r
1206         {\r
1207                 iView->DrawText(_L("No rom loaded"),TPoint(0,iFontHeight*13));\r
1208         }\r
1209         \r
1210 }\r
1211 \r
1212 void CPicoDriveUi::PutScreenSelect()\r
1213 {\r
1214         TInt regionIndex = 0;\r
1215 \r
1216         switch(PicoRegionOverride)\r
1217         {\r
1218         default:\r
1219                 regionIndex = 0;\r
1220                 break;\r
1221         case 1:\r
1222                 regionIndex = 1;\r
1223                 break;\r
1224         case 2:\r
1225                 regionIndex = 2;\r
1226                 break;\r
1227         case 4:\r
1228                 regionIndex = 3;\r
1229                 break;\r
1230         case 8:\r
1231                 regionIndex = 4;\r
1232                 break;\r
1233         }\r
1234 \r
1235         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1236         iView->DrawText(_L("Screen options"),TPoint(0,iFontHeight));\r
1237         iView->DrawText(_L("Portrait"),TPoint(0,iFontHeight*3),iScrMode==0);\r
1238         iView->DrawText(_L("Landscape Left"),TPoint(0,iFontHeight*4),iScrMode==1);\r
1239         iView->DrawText(_L("Landscape Right"),TPoint(0,iFontHeight*5),iScrMode==2);\r
1240         iView->DrawText(_L("Portrait stretched"),TPoint(0,iFontHeight*6),iScrMode==3);\r
1241         iView->DrawText(_L("Portrait full"),TPoint(0,iFontHeight*7),iScrMode==4);\r
1242         if(iInterpolate)\r
1243         {\r
1244         iView->DrawText(_L("Interpolate on"),TPoint(0,iFontHeight*8),iScrMode==5);\r
1245         }\r
1246         else\r
1247         {\r
1248         iView->DrawText(_L("Interpolate off"),TPoint(0,iFontHeight*8),iScrMode==5);\r
1249         }\r
1250 \r
1251         if(iFrameSkip == -1)\r
1252         {\r
1253                 iView->DrawText(_L("Frameskip auto"),TPoint(0,iFontHeight*9),iScrMode==6);\r
1254         }\r
1255         else\r
1256         {       \r
1257                 TBuf<64> skip;\r
1258                 skip.Format(_L("Frameskip %d"),iFrameSkip);\r
1259                 iView->DrawText(skip,TPoint(0,iFontHeight*9),iScrMode==6);\r
1260         }\r
1261 \r
1262         if(PicoOpt & 0x40)\r
1263         {\r
1264         iView->DrawText(_L("Accurate timing on"),TPoint(0,iFontHeight*10),iScrMode==7);\r
1265         }\r
1266         else\r
1267         {\r
1268         iView->DrawText(_L("Accurate timing off"),TPoint(0,iFontHeight*10),iScrMode==7);\r
1269         }\r
1270 \r
1271         if(PicoOpt & 0x80)\r
1272         {\r
1273         iView->DrawText(_L("Accurate sprites on"),TPoint(0,iFontHeight*11),iScrMode==8);\r
1274         }\r
1275         else\r
1276         {\r
1277         iView->DrawText(_L("Accurate sprites off"),TPoint(0,iFontHeight*11),iScrMode==8);\r
1278         }\r
1279 \r
1280         if(PicoOpt & 0x10)\r
1281         {\r
1282         iView->DrawText(_L("Alt. renderer on"),TPoint(0,iFontHeight*12),iScrMode==9);\r
1283         }\r
1284         else\r
1285         {\r
1286         iView->DrawText(_L("Alt. renderer off"),TPoint(0,iFontHeight*12),iScrMode==9);\r
1287         }\r
1288 \r
1289         iView->DrawText(iRegNames->MdcaPoint(regionIndex),TPoint(0,iFontHeight*13),iScrMode==10);\r
1290         \r
1291 }\r
1292 \r
1293 void CPicoDriveUi::PutSoundSelect()\r
1294 {\r
1295         iView->Clear();\r
1296         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1297         iView->DrawText(_L("Sound options"),TPoint(0,iFontHeight));\r
1298         if (PicoOpt&4)\r
1299                 iView->DrawText(_L("Z80 enabled"),TPoint(0,iFontHeight*3),iSndSelection==EEnableZ80);\r
1300         else\r
1301                 iView->DrawText(_L("Z80 disabled"),TPoint(0,iFontHeight*3),iSndSelection==EEnableZ80);\r
1302         if (PicoOpt&1)\r
1303                 iView->DrawText(_L("YM2612 enabled"),TPoint(0,iFontHeight*4),iSndSelection==EEnableYM2612);\r
1304         else\r
1305                 iView->DrawText(_L("YM2612 disabled"),TPoint(0,iFontHeight*4),iSndSelection==EEnableYM2612);\r
1306         \r
1307         if (PicoOpt&2)\r
1308                 iView->DrawText(_L("SN76496 enabled"),TPoint(0,iFontHeight*5),iSndSelection==EEnableSN76496);\r
1309         else\r
1310                 iView->DrawText(_L("SN76496 disabled"),TPoint(0,iFontHeight*5),iSndSelection==EEnableSN76496);\r
1311         \r
1312         \r
1313         TBuf<32> vol;\r
1314         vol.Format(_L("Volume %d"),iSoundVolume*10);\r
1315         iView->DrawText(vol,TPoint(0,iFontHeight*6),iSndSelection==ESoundVolume);\r
1316 \r
1317         vol.Format(_L("Sample rate %dKhz"),PsndRate/1000);\r
1318         iView->DrawText(vol,TPoint(0,iFontHeight*7),iSndSelection==ESoundRate);\r
1319 }\r
1320 \r
1321 \r
1322 void CPicoDriveUi::PutCheatSelect()\r
1323 {\r
1324         iView->Clear();\r
1325         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1326         iView->DrawText(_L("Cheat options"),TPoint(0,iFontHeight));\r
1327 \r
1328         iView->DrawText(_L("Add cheat"),TPoint(0,iFontHeight*3),iCheatSelection==EAddCheat);\r
1329         iView->DrawText(_L("Clear cheats"),TPoint(0,iFontHeight*4),iCheatSelection==EClearCheats);\r
1330         TInt noCheats = 0;\r
1331         TBuf<17>cheatCode;\r
1332 \r
1333         for(TInt i = iListOffset; (i < 256)&&(noCheats<5); i++)\r
1334                         {\r
1335                                 if (Liste_GG[i].code[0] != 0)\r
1336                                 {\r
1337 \r
1338                                         TRgb textColour = Liste_GG[i].active?KRgbGreen:KRgbDarkGreen;\r
1339                                         if(iCheatSelection == 2+(i-iListOffset))\r
1340                                         {\r
1341                                                 textColour = Liste_GG[i].active?KRgbRed:KRgbDarkRed;\r
1342                                         }\r
1343 \r
1344                                         TPtrC8 ptr((const unsigned char*)Liste_GG[i].code,strlen(Liste_GG[i].code));\r
1345                                         cheatCode.Copy(ptr);                                                                            \r
1346 \r
1347                                         iView->DrawText(cheatCode,TPoint(0,iFontHeight*(5+noCheats)),EFalse,textColour);\r
1348                                 \r
1349                                         noCheats++;\r
1350                                 }\r
1351                         }\r
1352 \r
1353         iView->DrawText(_L("Supports GG & Patch"),TPoint(0,iFontHeight*11));\r
1354         iView->DrawText(_L("GG: XXXX-XXXX"),TPoint(0,iFontHeight*12));\r
1355         iView->DrawText(_L("Patch: XXXXXX:YYYY"),TPoint(0,iFontHeight*13));\r
1356 }\r
1357 \r
1358 void CPicoDriveUi::PutControllerSelect()\r
1359 {\r
1360         iView->Clear();\r
1361         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1362         iView->DrawText(_L("Controller options"),TPoint(0,iFontHeight));\r
1363                 if (iEnableSixButtons)\r
1364                 iView->DrawText(_L("6 button pad"),TPoint(0,iFontHeight*3),iCtrlSelection==EControllerType);\r
1365         else\r
1366                 iView->DrawText(_L("3 button pad"),TPoint(0,iFontHeight*3),iCtrlSelection==EControllerType);\r
1367         iView->DrawText(_L("Configure keys"),TPoint(0,iFontHeight*4),iCtrlSelection==EConfigControls);\r
1368 \r
1369 \r
1370 \r
1371 }\r
1372 \r
1373 \r
1374 void CPicoDriveUi::PutConfigKeys()\r
1375 {\r
1376         iView->DrawText(_L("PicoDrive S60"),TPoint(0,0));\r
1377         iView->DrawText(_L("Configure keys"),TPoint(0,iFontHeight));\r
1378         iView->DrawText(_L("Please press:"),TPoint(0,iFontHeight*3));\r
1379         iView->DrawText(iKeyNames->MdcaPoint(iCurrentScan),TPoint(0,iFontHeight*4));\r
1380 \r
1381         iView->DrawText(_L("Press 'C' to skip this!"),TPoint(0,iFontHeight*6));\r
1382 \r
1383 }\r
1384 \r
1385 TInt CPicoDriveUi::IdleCallBackStop(TAny* /*aAppUi*/)\r
1386 {\r
1387         CActiveScheduler::Stop();\r
1388         return 0;\r
1389 }\r
1390 \r
1391 TInt CPicoDriveUi::StartEmulatorL(TAny* aAppUi)\r
1392 {\r
1393         static_cast<CPicoDriveUi*>(aAppUi)->StartEmulatorL();\r
1394         return 0;\r
1395 }\r
1396 \r
1397 void CPicoDriveUi::HandleForegroundEventL(TBool aForeground)\r
1398 {\r
1399         if(iView != NULL)\r
1400         {\r
1401                 iView->iForeground=aForeground;\r
1402         }\r
1403 \r
1404         if(!aForeground)\r
1405         {\r
1406                 if(iView != NULL)\r
1407                 {\r
1408                         iView->AbortNow(RDirectScreenAccess::ETerminateCancel);\r
1409                 }\r
1410         }\r
1411         else\r
1412         {\r
1413                 if(iView != NULL)\r
1414                 {\r
1415                 iView->Restart(RDirectScreenAccess::ETerminateCancel);\r
1416                 UpdateScreen();\r
1417                 }\r
1418 \r
1419                 SetKeyBlockMode(ENoKeyBlock);\r
1420         }\r
1421         \r
1422 }\r
1423 \r
1424 void CPicoDriveUi::UpdateScreen()\r
1425 {\r
1426         if(!iEmuRunning)\r
1427         {\r
1428                 iView->Clear();\r
1429                 if(iCurrentScan>=0)\r
1430                 {\r
1431                         PutConfigKeys();\r
1432                 }\r
1433                 else if(iPicoMenu==ESelectScrMenu)\r
1434                 {\r
1435                         PutScreenSelect();\r
1436                 }\r
1437                 else if (iPicoMenu==EAboutPicoMenu)\r
1438                 {\r
1439                         PutAbout(ETrue);\r
1440                 }\r
1441                 else if (iPicoMenu==ESelectSoundMenu)\r
1442                 {\r
1443                         PutSoundSelect();\r
1444                 }\r
1445                 else if (iPicoMenu==ESelectControlsMenu)\r
1446                 {\r
1447                         PutControllerSelect();\r
1448                 }\r
1449                 else if (iPicoMenu==ESelectCheatMenu)\r
1450                 {\r
1451                         PutCheatSelect();\r
1452                 }\r
1453                 else \r
1454                         PutMainMenu();\r
1455                 \r
1456         }\r
1457 }\r
1458 \r
1459 void CPicoDriveUi::StartEmulatorL()\r
1460 {       \r
1461         iView->Clear();\r
1462         TTime time;\r
1463         time.HomeTime();\r
1464 #ifdef S60V3\r
1465         LastSecond=(TInt)(time.Int64()/1000);//GetTickCount(); \r
1466 #else\r
1467         LastSecond=(TInt)(time.Int64()/1000).GetTInt();//GetTickCount(); \r
1468 #endif\r
1469         FramesDone=0;\r
1470         \r
1471         iSndStream->SetVolume((iSndStream->MaxVolume()*iSoundVolume)/10);\r
1472         UpdatePSndRate();\r
1473 \r
1474         while(iEmuRunning)\r
1475         {       \r
1476                 EmulateFrame();\r
1477         }\r
1478         iSndStream->Stop();\r
1479         iView->Clear();\r
1480         PutMainMenu();\r
1481         \r
1482 }\r
1483 \r
1484 /**\r
1485  * Calculates the palette table 0-4096\r
1486  */\r
1487 void CPicoDriveUi::CalculatePaletteTable()\r
1488 {\r
1489         for(TInt cram =0;cram<4096;cram++)\r
1490         {\r
1491                 if(iDisplayMode == EColor4K)\r
1492                 {\r
1493                         unsigned short high=0x111;\r
1494                         high|=(cram&0x00e)<<8; // Red\r
1495                         high|=(cram&0x0e0); // Green\r
1496                         high|=(cram&0xe00)>> 8; // Blue\r
1497                         gColorMapTab[cram] =  high;\r
1498                 }\r
1499                 else // 64K color mode\r
1500                 {\r
1501                         unsigned short high=0x0841;\r
1502                         // Convert 0000bbbb ggggrrrr\r
1503                         // to      rrrr1ggg g10bbbb1\r
1504                         high|=(cram&0x00f)<<12; // Red\r
1505                         high|=(cram&0x0f0)<< 3; // Green\r
1506                         high|=(cram&0xf00)>> 7; // Blue\r
1507                         gColorMapTab[cram] = high;\r
1508                 }\r
1509         }\r
1510 }\r
1511 \r
1512 int CPicoDriveUi::TargetInit()\r
1513 {\r
1514         PicoCram=NULL;\r
1515         TUint16 currentLine = 0;\r
1516         TReal xFactor = 1;\r
1517         TReal xNarrowFactor = 1;\r
1518         TReal yFactor = 1;\r
1519         TInt loop;\r
1520 \r
1521         memset(framebuff,0,framebuffsize);\r
1522 \r
1523         if(iScrMode==0)\r
1524         {\r
1525 #ifdef S60V3\r
1526                 xFactor = ((TReal)Targ.view.iBr.iX/(TReal)320);\r
1527                 xNarrowFactor = ((TReal)Targ.view.iBr.iX/(TReal)256);\r
1528                 \r
1529                 if(xFactor>1)\r
1530                         xFactor = 1;\r
1531                 \r
1532                 if(xNarrowFactor>1)\r
1533                         xNarrowFactor = 1;\r
1534 \r
1535                 yFactor = ((TReal)Targ.view.iBr.iY/(TReal)240);\r
1536 \r
1537                 if(yFactor>1)\r
1538                         yFactor = 1;            \r
1539                 \r
1540                 for(loop = 0;loop<256;loop++)\r
1541                 {\r
1542                         TInt line = (loop*xNarrowFactor);\r
1543                         TInt nextLine = ((loop+1)*xNarrowFactor);\r
1544                         if(line != nextLine)\r
1545                         {\r
1546                                 gNarrowColumnStepTable[loop] = 1;\r
1547                         }\r
1548                         else\r
1549                         {\r
1550                                 gNarrowColumnStepTable[loop] = 0;\r
1551                         }                               \r
1552                 }\r
1553                 \r
1554                 for(loop = 0;loop<320;loop++)\r
1555                 {\r
1556                         TInt line = (loop*xFactor);\r
1557                         TInt nextLine = ((loop+1)*xFactor);\r
1558                         if( line != nextLine) \r
1559                         {\r
1560                                 gColumnStepTable[loop] = 1;\r
1561                         }\r
1562                         else\r
1563                         {\r
1564                                 gColumnStepTable[loop] = 0;\r
1565                         }               \r
1566                 }\r
1567 \r
1568                 for(TInt loop = 0;loop<240;loop++)\r
1569                 {\r
1570                         gLineTable[loop] = currentLine;\r
1571                         if((loop*yFactor) != ((loop+1)*yFactor))\r
1572                                 currentLine++;\r
1573                 }\r
1574                 myPicoScan=EmulateScan16;\r
1575                 \r
1576 #else\r
1577                 if(iInterpolate)\r
1578                         myPicoScan=EmulateScan16_176Interpolate;\r
1579                 else\r
1580                         myPicoScan=EmulateScan16_176;\r
1581                 for(TInt loop = 0;loop<240;loop++)\r
1582                 {\r
1583                         gLineTable[loop] = currentLine;\r
1584                         if(((loop*3)/4) != (((loop+1)*3)/4))\r
1585                                 currentLine++;\r
1586                 }\r
1587 #endif\r
1588                 KBitValTable[0] = 1;\r
1589                 KBitValTable[1] = 2;\r
1590                 KBitValTable[2] = 4;\r
1591                 KBitValTable[3] = 8;\r
1592         }\r
1593 #ifdef S60V3\r
1594         else if (iScrMode == 1 || iScrMode == 2)\r
1595         {\r
1596                 xFactor = ((TReal)Targ.view.iBr.iY/(TReal)320);\r
1597                 xNarrowFactor = ((TReal)Targ.view.iBr.iY/(TReal)256);   \r
1598 \r
1599                 if(xFactor>2)\r
1600                         xFactor = 2;\r
1601                 \r
1602                 if(xNarrowFactor>2)\r
1603                         xNarrowFactor = 2;\r
1604 \r
1605                 yFactor = ((TReal)Targ.view.iBr.iX/(TReal)240);\r
1606                 if(yFactor>2)\r
1607                         yFactor = 2;\r
1608                 \r
1609                 for(loop = 0;loop<256;loop++)\r
1610                 {\r
1611                         TInt col = (loop*xNarrowFactor);\r
1612                         TInt nextCol= ((loop+1)*xNarrowFactor);\r
1613                 \r
1614                         gNarrowColumnStepTable[loop] = nextCol-col;;                                            \r
1615                 }\r
1616                 \r
1617                 for(loop = 0;loop<320;loop++)\r
1618                 {\r
1619                         TInt col = (loop*xFactor);\r
1620                         TInt nextCol= ((loop+1)*xFactor);\r
1621                 \r
1622                         gColumnStepTable[loop] = nextCol-col;;\r
1623                 }\r
1624 \r
1625                 for(TInt loop = 0;loop<240;loop++)\r
1626                 {\r
1627                         gLineTable[loop] = currentLine;\r
1628                         TInt line = (loop*yFactor);\r
1629                         TInt nextLine = ((loop+1)*yFactor);\r
1630                         currentLine+=(nextLine-line);\r
1631                 }\r
1632                 if(iScrMode == 2)\r
1633                 {\r
1634                         KBitValTable[0] = 4;\r
1635                         KBitValTable[1] = 8;\r
1636                         KBitValTable[2] = 2;\r
1637                         KBitValTable[3] = 1;\r
1638                         myPicoScan=EmulateScanFullRight16;\r
1639                 }\r
1640                 else\r
1641                 {\r
1642                         KBitValTable[0] = 8;\r
1643                         KBitValTable[1] = 4;\r
1644                         KBitValTable[2] = 1;\r
1645                         KBitValTable[3] = 2;\r
1646                         myPicoScan=EmulateScanFull16;\r
1647                 }\r
1648 #else\r
1649 else if (iScrMode == 1)\r
1650 {\r
1651                 if(iInterpolate)\r
1652                         myPicoScan=EmulateScanFull16_176Interpolate;\r
1653                 else\r
1654                         myPicoScan=EmulateScanFull16_176;\r
1655 \r
1656                 for(TInt loop = 0;loop<240;loop++)\r
1657                 {\r
1658                         gLineTable[loop] = currentLine;\r
1659                         if(((loop*3)/4) != (((loop+1)*3)/4))\r
1660                                 currentLine++;\r
1661                 }\r
1662                 KBitValTable[0] = 8;\r
1663                 KBitValTable[1] = 4;\r
1664                 KBitValTable[2] = 1;\r
1665                 KBitValTable[3] = 2;\r
1666 #endif  \r
1667         }\r
1668 #ifndef S60V3\r
1669         else if(iScrMode==2)\r
1670         {\r
1671                 if(iInterpolate)\r
1672                         myPicoScan=EmulateScanFullRight16_176Interpolate;\r
1673                 else\r
1674                         myPicoScan=EmulateScanFullRight16_176;\r
1675                 for(TInt loop = 0;loop<240;loop++)\r
1676                 {\r
1677                         gLineTable[loop] = currentLine;\r
1678                         if(((loop*3)/4) != (((loop+1)*3)/4))\r
1679                                 currentLine++;\r
1680                 }\r
1681                 KBitValTable[0] = 4;\r
1682                 KBitValTable[1] = 8;\r
1683                 KBitValTable[2] = 2;\r
1684                 KBitValTable[3] = 1;\r
1685         }\r
1686 #endif\r
1687         else \r
1688         {\r
1689 #ifdef S60V3\r
1690                 xFactor = ((TReal)Targ.view.iBr.iX/(TReal)320);\r
1691                 xNarrowFactor = ((TReal)Targ.view.iBr.iX/(TReal)256);\r
1692                 \r
1693                 if(xFactor>2)\r
1694                         xFactor = 2;\r
1695                 \r
1696                 if(xNarrowFactor>2)\r
1697                         xNarrowFactor = 2;\r
1698 \r
1699                 yFactor = ((TReal)Targ.view.iBr.iY/(TReal)240);\r
1700 \r
1701                 if(yFactor>2)\r
1702                         yFactor = 2;            \r
1703                 \r
1704                 for(loop = 0;loop<256;loop++)\r
1705                 {\r
1706                         TInt col = (loop*xNarrowFactor);\r
1707                         TInt nextCol= ((loop+1)*xNarrowFactor);\r
1708                 \r
1709                         gNarrowColumnStepTable[loop] = nextCol-col;;                                            \r
1710                 }\r
1711                 \r
1712                 for(loop = 0;loop<320;loop++)\r
1713                 {\r
1714                         TInt col = (loop*xFactor);\r
1715                         TInt nextCol= ((loop+1)*xFactor);\r
1716                 \r
1717                         gColumnStepTable[loop] = nextCol-col;;\r
1718                 }\r
1719 \r
1720                 for(TInt loop = 0;loop<240;loop++)\r
1721                 {\r
1722                         gLineTable[loop] = currentLine;\r
1723                         TInt line = (loop*yFactor);\r
1724                         TInt nextLine = ((loop+1)*yFactor);\r
1725                         currentLine+=(nextLine-line);\r
1726                 }\r
1727                 myPicoScan=EmulateStretchScan16;\r
1728 #else\r
1729                 if(iScrMode == 4)\r
1730                 {\r
1731                         myPicoScan=EmulateStretchScan16_320;\r
1732                         iInterpolate = EFalse; // not needed\r
1733                 }\r
1734                 else\r
1735                 {\r
1736                 if(iInterpolate)\r
1737                         myPicoScan=EmulateStretchScan16_176Interpolate;\r
1738                 else\r
1739                         myPicoScan=EmulateStretchScan16_176;\r
1740                 }\r
1741 \r
1742                 for(TInt loop = 0;loop<240;loop++)\r
1743                 {\r
1744                         gLineTable[loop] = currentLine;\r
1745                         if(((loop*15)/16) != (((loop+1)*15)/16))\r
1746                                 currentLine++;\r
1747                 }\r
1748 #endif\r
1749                 KBitValTable[0] = 1;\r
1750                 KBitValTable[1] = 2;\r
1751                 KBitValTable[2] = 4;\r
1752                 KBitValTable[3] = 8;\r
1753         }\r
1754         \r
1755         if(iView)\r
1756                 iView->SetRect(TRect(TPoint(0,0),iEikonEnv->ScreenDevice()->SizeInPixels()));\r
1757 #ifdef S60V3\r
1758         switch(iScrMode)\r
1759         {\r
1760         case 0:\r
1761         case 3:\r
1762         case 4:\r
1763                 iPutRect=TRect(TPoint(0,0),TSize(xFactor*320,yFactor*240));\r
1764                 iPutPoint=TPoint(Targ.view.Size().iWidth/2-iPutRect.Size().iWidth/2,Targ.view.Size().iHeight/2-iPutRect.Size().iHeight/2);\r
1765                 \r
1766                 break;\r
1767         case 1:\r
1768         case 2:\r
1769                 iPutRect=TRect(TPoint(0,0),TSize(yFactor*240,xFactor*320));\r
1770                 iPutPoint=TPoint(Targ.view.Size().iWidth/2-iPutRect.Size().iWidth/2,Targ.view.Size().iHeight/2-iPutRect.Size().iHeight/2);\r
1771                 break;\r
1772 \r
1773         }\r
1774 #else\r
1775         switch(iScrMode)\r
1776         {\r
1777         case 0:\r
1778                 iPutPoint=TPoint(0,20);\r
1779                 iPutRect=TRect(TPoint(0,0),TSize(176,168));     \r
1780                 break;\r
1781         case 1:\r
1782         case 2:\r
1783                 iPutPoint=TPoint(4,1);\r
1784                 iPutRect=TRect(TPoint(4,1),TSize(168,205));\r
1785 \r
1786                 break;\r
1787         case 3:\r
1788         case 4:\r
1789                 iPutPoint=TPoint(0,0);\r
1790                 iPutRect=TRect(TPoint(0,0),TSize(176,208));\r
1791                 break;\r
1792         }\r
1793 #endif\r
1794         CalulateLineStarts();\r
1795         return 0;\r
1796 }\r
1797 #ifdef S60V3\r
1798 #define KCenterOffset 8\r
1799 #else\r
1800 #define KCenterOffset 0\r
1801 #endif\r
1802 void CPicoDriveUi::CalulateLineStarts()\r
1803 {\r
1804         switch(iScrMode)\r
1805         {\r
1806         case 0:\r
1807         case 3:\r
1808         case 4:\r
1809                 {\r
1810                         for(TInt loop = 0;loop<Targ.view.Size().iHeight;loop++)\r
1811                         {\r
1812                                 gLineOffsets[loop] = loop*Targ.scanline_length;\r
1813                         }\r
1814                 }break;\r
1815         case 1:\r
1816                 {               \r
1817                         for(TInt loop = 0;loop<Targ.view.Size().iHeight;loop++)\r
1818                         {\r
1819                                 gLineOffsets[loop] = loop*2+Targ.screen_offset+KCenterOffset;\r
1820                         }\r
1821                 }break;\r
1822         case 2:\r
1823                 {\r
1824                         for(TInt loop = 0;loop<Targ.view.Size().iHeight;loop++)\r
1825                         {\r
1826                                 gLineOffsets[loop] = Targ.scanline_length-loop*2-KCenterOffset;\r
1827                         }\r
1828                 }break; \r
1829         }\r
1830 }\r
1831 \r
1832 bool IsZip(char *filename)\r
1833 {\r
1834         unsigned char buf[2];\r
1835         FILE *fp;\r
1836         \r
1837         if((fp=fopen(filename,"rb"))!=NULL)\r
1838         {\r
1839                 fread(buf, 2, 1, fp);\r
1840                 fclose(fp);\r
1841                 return(memcmp(buf,"PK",2)==0);\r
1842         }\r
1843         if(fp)fclose(fp);\r
1844         return false;\r
1845 }\r
1846 \r
1847 int CPicoDriveUi::EmulateInit()\r
1848 {\r
1849         FILE *f=NULL;\r
1850         EmulateExit(); // Make sure exited\r
1851         TargetInit(); // Find out where to put the screen\r
1852         PicoInit();\r
1853         // Load cartridge\r
1854         if(IsZip(RomName))\r
1855         {\r
1856                 if(CartLoadZip(RomName,&RomData,&RomSize))\r
1857                 {               \r
1858                         return 1;\r
1859                 }\r
1860         }\r
1861         else\r
1862         {\r
1863                 // Load cartridge\r
1864                 RFile file;\r
1865                 TInt error = file.Open(iEikonEnv->FsSession(),iRomName,0);\r
1866                 file.Close();\r
1867                 f=fopen(RomName,"rb"); \r
1868                 if (f==NULL) \r
1869                         return 1;\r
1870                 TInt result =PicoCartLoad(f,&RomData,&RomSize);\r
1871                 fclose(f);\r
1872                 if(result)\r
1873                         return 1; // failed to load cart\r
1874         }\r
1875         \r
1876         TParsePtr parser(iRomName);\r
1877         TPtr8 ptr(Pico.rom_name,511);\r
1878         ptr.Copy(parser.DriveAndPath());\r
1879         ptr.Append(parser.Name());\r
1880         ptr.ZeroTerminate();\r
1881 \r
1882         PicoCartInsert(RomData,RomSize);\r
1883         Load_Patch_File();\r
1884         saveLoadGame(1, 1); // load sram if any saved\r
1885 \r
1886         if(!iEnableSixButtons)\r
1887                 PicoOpt=PicoOpt& 223;\r
1888         else\r
1889                 PicoOpt=PicoOpt|32;     \r
1890         return 0;\r
1891 }\r
1892 \r
1893 void CPicoDriveUi::EmulateExit()\r
1894 {\r
1895         // Save sram if any\r
1896         if(RomData != NULL)\r
1897         {\r
1898                 if(SRam.changed)\r
1899                 {\r
1900                 saveLoadGame(0,1);\r
1901                 SRam.changed = 0;\r
1902                 }\r
1903 \r
1904                 Save_Patch_File();\r
1905                 // Remove cartridge\r
1906                 PicoCartInsert(NULL,0);\r
1907                 PicoUnloadCart(RomData);\r
1908                 RomData=NULL; RomSize=0;\r
1909         }\r
1910         PicoExit();\r
1911 }\r
1912 \r
1913 int CPicoDriveUi::InputFrame()\r
1914 {\r
1915         if(PicoOpt & 3)\r
1916         {\r
1917                 PsndOut=(short*)(iMonoSound.Ptr()+2*iCurrentSeg*PsndLen);\r
1918         }\r
1919         else\r
1920         {\r
1921                 PsndOut=NULL;\r
1922         }\r
1923 \r
1924         Patch_Codes();\r
1925 \r
1926         PicoFrame();\r
1927 \r
1928         if(PicoOpt & 3)\r
1929         {\r
1930                 iCurrentSeg++;\r
1931                 if(iCurrentSeg==6)\r
1932                 {\r
1933                         iMonoSound.SetLength(PsndLen*2*6);\r
1934                         iSndStream->WriteL(iMonoSound);\r
1935                         iCurrentSeg=0;\r
1936                 }\r
1937         }\r
1938         \r
1939         TCallBack callback(IdleCallBackStop,this);\r
1940         iIdleCallBack.Cancel();\r
1941         iIdleCallBack.Set(callback);\r
1942         iIdleCallBack.CallBack();\r
1943         CActiveScheduler::Start();\r
1944         \r
1945         \r
1946         PicoPad[0]=iPad1;\r
1947         return 0;\r
1948 }\r
1949 \r
1950 \r
1951 \r
1952 int CPicoDriveUi::EmulateFrame()\r
1953 {\r
1954         int i=0,need=2;\r
1955         if (!iRomLoaded) \r
1956                 return 1;\r
1957         // Speed throttle:\r
1958         if(iFrameSkip ==-1) // auto skipping\r
1959         {\r
1960                 int time=0,frame=0;\r
1961                 TTime newtime;\r
1962                 newtime.HomeTime();\r
1963 #ifdef S60V3\r
1964                 TInt64 tic=(newtime.Int64()/1000);\r
1965                 time=(tic-LastSecond); // This will be about 0-1000 ms\r
1966 #else\r
1967                 TInt64 tic=(newtime.Int64()/1000).GetTInt();\r
1968                 time=(tic-LastSecond).GetTInt(); // This will be about 0-1000 ms\r
1969 #endif\r
1970                 frame=time*FramesPerSecond/1000;\r
1971                 need=frame-FramesDone;\r
1972                 FramesDone=frame;\r
1973                 \r
1974                 if (FramesPerSecond>0)\r
1975                 {\r
1976                         // Carry over any >60 frame count to one second\r
1977                         while (FramesDone>=FramesPerSecond) { FramesDone-=FramesPerSecond; LastSecond+=1000; }\r
1978                 }\r
1979                 \r
1980                 if (need<=0) { \r
1981                         TTime nextTime;\r
1982                         do\r
1983                         {\r
1984                         nextTime.HomeTime();\r
1985                         }while((nextTime.Int64()-newtime.Int64())<15000);\r
1986                 }\r
1987                 if (need>10) need=10; // Limit frame skipping\r
1988         }\r
1989         else\r
1990         {\r
1991                 need = iFrameSkip+1;\r
1992         }\r
1993         PicoSkipFrame=1;\r
1994         for (i=0;i<need-1;i++) InputFrame(); // Frame skip if needed\r
1995         PicoSkipFrame=0;\r
1996 \r
1997         // Now final frame is drawn:\r
1998         TBitmapUtil util(iBackBuffer);\r
1999         util.Begin(TPoint(0,0));\r
2000         Targ.screen=(unsigned char*)iBackBuffer->DataAddress();\r
2001         \r
2002         if (Targ.screen == NULL) \r
2003         {\r
2004                 util.End();\r
2005                 return 1;\r
2006         }\r
2007         \r
2008         PicoScan=myPicoScan; // Setup scanline callback\r
2009         InputFrame();\r
2010 \r
2011 \r
2012         if(PicoOpt & 0x10) // need to render separatly\r
2013         {\r
2014                 unsigned short* framebuffptr = framebuff+2632;\r
2015 \r
2016                 if(!(Pico.video.reg[12]&1))\r
2017                 {\r
2018                         framebuffptr=framebuffptr-32;\r
2019                 }\r
2020 \r
2021                 TInt skipNext =0;\r
2022                 for(TInt loop = 0;loop<224;loop++)\r
2023                 {\r
2024                         if(skipNext == 0)\r
2025                         {\r
2026                                 skipNext = PicoScan(loop,framebuffptr);\r
2027                         }\r
2028                         else\r
2029                                 skipNext--;\r
2030                         framebuffptr+=328;\r
2031                 }\r
2032         }\r
2033         PicoScan=NULL;\r
2034         util.End();\r
2035 \r
2036         Targ.screen = NULL;\r
2037         \r
2038         iView->PutBitmap(iBackBuffer,iPutPoint,iPutRect);;\r
2039         \r
2040         return 0;\r
2041 }\r
2042 \r
2043 \r
2044 void CPicoDriveUi::MaoscOpenComplete(TInt aError)\r
2045 {\r
2046         if(aError == KErrNone)\r
2047         {\r
2048                 iSndStream->SetPriority(EPriorityMuchMore, EMdaPriorityPreferenceNone);\r
2049                 iSndStream->SetVolume((iSndStream->MaxVolume()*iSoundVolume)/10);\r
2050                 if(!UpdatePSndRate())\r
2051                 {\r
2052                         PsndRate = 8000;\r
2053                         UpdatePSndRate();\r
2054                 }\r
2055         }\r
2056         CActiveScheduler::Stop();\r
2057 }\r
2058 \r
2059 void CPicoDriveUi::MaoscBufferCopied(TInt /*aError*/, const TDesC8& /*aBuffer*/)\r
2060 {\r
2061 }\r
2062 \r
2063 void CPicoDriveUi::MaoscPlayComplete(TInt aError)\r
2064 {\r
2065         if(aError != KErrNone)\r
2066         {\r
2067                 iSndStream->SetVolume((iSndStream->MaxVolume()*iSoundVolume)/10);\r
2068                 UpdatePSndRate();\r
2069         }\r
2070 }\r
2071 \r
2072 TBool CPicoDriveUi::UpdatePSndRate()\r
2073 {\r
2074         TInt sampleRate = TMdaAudioDataSettings::ESampleRate8000Hz;\r
2075         if(PsndRate == 11025)\r
2076                 sampleRate = TMdaAudioDataSettings::ESampleRate11025Hz;\r
2077         else if (PsndRate == 16000)\r
2078                 sampleRate = TMdaAudioDataSettings::ESampleRate16000Hz;\r
2079         else if (PsndRate == 22050)\r
2080                 sampleRate = TMdaAudioDataSettings::ESampleRate22050Hz;\r
2081 \r
2082         TRAPD(err,iSndStream->SetAudioPropertiesL(sampleRate,TMdaAudioDataSettings::EChannelsMono));\r
2083         return (err == KErrNone);\r
2084 }\r
2085 \r
2086 size_t gzRead2(void *p, size_t _size, size_t _n, void *file)\r
2087 {\r
2088         return gzread(file, p, _n);\r
2089 }\r
2090 \r
2091 \r
2092 size_t gzWrite2(void *p, size_t _size, size_t _n, void *file)\r
2093 {\r
2094         return gzwrite(file, p, _n);\r
2095 }\r
2096 \r
2097 \r
2098 \r
2099 // this function is shared between both threads\r
2100 int CPicoDriveUi::saveLoadGame(int load, int sram)\r
2101 {\r
2102         int res = 0;\r
2103         \r
2104         if(!(iRomName.Length()>0)) return -1;\r
2105         \r
2106         // make save filename\r
2107         strcpy(saveFname,RomName);\r
2108         saveFname[KMaxFileName-5] = 0;\r
2109         if(saveFname[strlen(saveFname)-4] == '.') saveFname[strlen(saveFname)-4] = 0;\r
2110         strcat(saveFname, sram ? ".srm" : ".mds");\r
2111         \r
2112         if(sram) {\r
2113                 int sram_size = SRam.end-SRam.start+1;\r
2114                 if(SRam.reg_back & 4) sram_size=0x2000;\r
2115                 if(!SRam.data) return 0; // SRam forcefully disabled for this game\r
2116                 if(load) {\r
2117                         PmovFile = fopen(saveFname, "rb");\r
2118                         if(!PmovFile) return -1;\r
2119                         fread(SRam.data, 1, sram_size, (FILE *) PmovFile);\r
2120                         fclose((FILE *) PmovFile);\r
2121                 } else {\r
2122                         // sram save needs some special processing\r
2123                         // see if we have anything to save\r
2124                         for(; sram_size > 0; sram_size--)\r
2125                                 if(SRam.data[sram_size-1]) break;\r
2126                                 \r
2127                                 if(sram_size) {\r
2128                                         PmovFile = fopen(saveFname, "wb");\r
2129                                         res = fwrite(SRam.data, 1, sram_size, (FILE *) PmovFile);\r
2130                                         res = (res != sram_size) ? -1 : 0;\r
2131                                         fclose((FILE *) PmovFile);\r
2132                                 }\r
2133                 }\r
2134                 PmovFile = 0;\r
2135                 return res;\r
2136         } else {\r
2137                 // try gzip first\r
2138                 //if(currentConfig.iFlags & 0x80) {\r
2139                 strcat(saveFname, ".gz");\r
2140                 if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {\r
2141                         areaRead  = gzRead2;\r
2142                         areaWrite = gzWrite2;\r
2143                         if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);\r
2144                 } else\r
2145                         saveFname[strlen(saveFname)-3] = 0;\r
2146                 //      }\r
2147                 \r
2148                 if(!PmovFile) { // gzip failed or was disabled\r
2149                         if( (PmovFile = fopen(saveFname, load ? "rb" : "wb")) ) {\r
2150                                 areaRead  = (arearw *) fread;\r
2151                                 areaWrite = (arearw *) fwrite;\r
2152                         }\r
2153                 }\r
2154                 if(PmovFile) {\r
2155                         PmovAction = load ? 6 : 5; // load/save\r
2156                         PmovState();\r
2157                         if(areaRead == gzRead2)\r
2158                                 gzclose(PmovFile);\r
2159                         else fclose ((FILE *) PmovFile);\r
2160                         PmovFile = 0;\r
2161                 } else {\r
2162                         res = -1;\r
2163                 }\r
2164                 \r
2165                 return res;\r
2166         }\r
2167         \r
2168 }\r
2169 \r
2170 CQPicoDriveView::~CQPicoDriveView()\r
2171 {\r
2172         iDsa->Cancel();\r
2173         delete iDsa;\r
2174 }\r
2175 \r
2176 void CQPicoDriveView::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)\r
2177 {\r
2178         if(iForeground)\r
2179         {\r
2180                 iDsa->Cancel();\r
2181                 iDsa->StartL();\r
2182                 iDsa->Gc()->SetClippingRegion(iDsa->DrawingRegion());\r
2183                 iDrawingOn=ETrue;\r
2184         }\r
2185 }\r
2186 \r
2187 void CQPicoDriveView::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)\r
2188 {\r
2189         iDsa->Cancel();\r
2190         iDrawingOn=EFalse;\r
2191 }\r
2192 \r
2193 \r
2194 \r
2195 void CQPicoDriveView::Draw(const TRect& aRect) const\r
2196 {\r
2197         CWindowGc& gc=SystemGc();\r
2198         \r
2199         gc.SetBrushColor(KRgbBlack);\r
2200         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);\r
2201         gc.SetPenStyle(CGraphicsContext::ENullPen);\r
2202         gc.DrawRect(aRect);\r
2203         static_cast<CPicoDriveUi*>(iEikonEnv->EikAppUi())->StartAsynchUpdate();\r
2204 }\r
2205 \r
2206 void CQPicoDriveView::ConstructL()\r
2207 {\r
2208         CreateWindowL();\r
2209         ActivateL();\r
2210         SetFocus(ETrue);\r
2211         SetRect(TRect(TPoint(0,0),iEikonEnv->ScreenDevice()->SizeInPixels())/*iEikonEnv->EikAppUi()->ClientRect()*/);\r
2212         iDsa=CDirectScreenAccess::NewL(iEikonEnv->WsSession(),*iEikonEnv->ScreenDevice(),Window(),*this);\r
2213         iDsa->StartL();\r
2214         iDsa->Gc()->SetClippingRegion(iDsa->DrawingRegion());\r
2215         iDrawingOn=ETrue;\r
2216         if(Window().DisplayMode() != EColor4K && Window().DisplayMode() != EColor64K)\r
2217         {\r
2218                 Window().SetRequiredDisplayMode(EColor64K); // Try to set 64K color mode\r
2219         }\r
2220 }\r
2221 void CQPicoDriveView::Clear()\r
2222 {\r
2223         CBitmapContext* gc;\r
2224         if(iDrawingOn)\r
2225         {\r
2226                 gc=iDsa->Gc();\r
2227         }\r
2228         else\r
2229         {\r
2230                 ActivateGc();\r
2231                 gc=&SystemGc();\r
2232         }\r
2233         \r
2234         gc->SetBrushColor(KRgbBlack);\r
2235         gc->SetBrushStyle(CGraphicsContext::ESolidBrush);\r
2236         gc->SetPenStyle(CGraphicsContext::ENullPen);\r
2237         gc->DrawRect(Rect());   \r
2238         if(iDrawingOn)\r
2239         {\r
2240                 iDsa->ScreenDevice()->Update();\r
2241                 iEikonEnv->WsSession().Flush();\r
2242         }\r
2243         else\r
2244         {\r
2245                 DeactivateGc();\r
2246         }\r
2247 }\r
2248 \r
2249 void CQPicoDriveView::DrawText(const TDesC& aText,TPoint aPoint,TBool aHighLight,TRgb aTextColour)\r
2250 {\r
2251         CBitmapContext* gc;\r
2252         if(iDrawingOn)\r
2253         {\r
2254                 gc=iDsa->Gc();\r
2255         }\r
2256         else\r
2257         {\r
2258                 ActivateGc();\r
2259                 gc=&SystemGc();\r
2260         }\r
2261         \r
2262         gc->SetBrushColor(KRgbBlack);\r
2263         gc->SetBrushStyle(CGraphicsContext::ESolidBrush);\r
2264         if(!aHighLight)\r
2265         {\r
2266                 gc->SetPenColor(aTextColour);\r
2267         }\r
2268         else\r
2269         {\r
2270                 gc->SetPenColor(KRgbRed);\r
2271         }\r
2272         gc->SetPenStyle(CGraphicsContext::ESolidPen);\r
2273         aPoint.iY+=iEikonEnv->NormalFont()->HeightInPixels()-2;\r
2274         aPoint.iX=Size().iWidth/2-iEikonEnv->NormalFont()->TextWidthInPixels(aText)/2;\r
2275         gc->UseFont(iEikonEnv->NormalFont());\r
2276         gc->DrawText(aText,aPoint);\r
2277         gc->DiscardFont();\r
2278         \r
2279         if(iDrawingOn)\r
2280         {\r
2281                 iDsa->ScreenDevice()->Update();         \r
2282         }\r
2283         else\r
2284         {\r
2285                 DeactivateGc();\r
2286         }\r
2287 }\r
2288 \r
2289 TInt CQPicoDriveView::DrawTextInRect(const TDesC& aText,TRect aRect,TInt aStartPos)\r
2290 {\r
2291         CBitmapContext* gc;\r
2292         TInt pos = aStartPos;\r
2293         TInt len = aText.Length();\r
2294         if(iDrawingOn)\r
2295         {\r
2296                 gc=iDsa->Gc();\r
2297         }\r
2298         else\r
2299         {\r
2300                 ActivateGc();\r
2301                 gc=&SystemGc();\r
2302         }\r
2303         \r
2304         gc->SetBrushColor(KRgbBlack);\r
2305         gc->SetBrushStyle(CGraphicsContext::ESolidBrush);\r
2306         \r
2307         gc->SetPenColor(KRgbWhite);\r
2308         \r
2309         gc->SetPenStyle(CGraphicsContext::ESolidPen);\r
2310         gc->UseFont(iEikonEnv->DenseFont());\r
2311         while(pos<len && aRect.iTl.iY<aRect.iBr.iY-(iEikonEnv->DenseFont()->HeightInPixels()+3))\r
2312         {\r
2313                 TInt newline = aText.Right(len-pos).Locate('\n');\r
2314                 if(newline == KErrNotFound)\r
2315                         newline=(len-1)-pos;\r
2316                 gc->DrawText(aText.Mid(pos,newline),aRect,iEikonEnv->DenseFont()->HeightInPixels());\r
2317                 pos=pos+newline+1; // skip new line\r
2318                 aRect.iTl+=TSize(0,iEikonEnv->DenseFont()->HeightInPixels()+3);\r
2319         }\r
2320         gc->DiscardFont();\r
2321         \r
2322         if(iDrawingOn)\r
2323         {\r
2324                 iDsa->ScreenDevice()->Update();\r
2325         }\r
2326         else\r
2327         {\r
2328                 DeactivateGc();\r
2329         }\r
2330         return pos;\r
2331 }\r
2332 \r
2333 \r
2334 void CQPicoDriveView::PutBitmap(CFbsBitmap* aBitmap,TPoint aPoint,TRect aRect)\r
2335 {\r
2336         if(iDrawingOn)\r
2337         {\r
2338                 //#ifdef __WINS__\r
2339                 iDsa->Gc()->BitBlt(aPoint,aBitmap,aRect);\r
2340                 iDsa->ScreenDevice()->Update();\r
2341                 //#endif\r
2342                 //iEikonEnv->WsSession().Flush();\r
2343         }\r
2344         else\r
2345         {\r
2346                 ActivateGc();\r
2347                 CWindowGc& gc=SystemGc();\r
2348                 gc.BitBlt(aPoint,aBitmap,aRect);\r
2349                 DeactivateGc();\r
2350         }\r
2351 }\r
2352 \r
2353 \r
2354 \r
2355 void Execute()\r
2356 {\r
2357         __UHEAP_MARK;\r
2358         CTrapCleanup* cleanup = CTrapCleanup::New();\r
2359         \r
2360         // Create a eikenv\r
2361         CEikonEnv* eikenv = new CEikonEnv;\r
2362         if (!eikenv) \r
2363         {\r
2364                 return /*KErrNoMemory*/;\r
2365         }\r
2366         TRAPD(eikErr, eikenv->ConstructL());\r
2367         if (eikErr != KErrNone) \r
2368         {\r
2369                 delete eikenv;\r
2370                 return /*eikErr*/;\r
2371         }\r
2372         CPicoDriveUi* appUi = new (ELeave) CPicoDriveUi;\r
2373         if (!appUi) \r
2374         {\r
2375                 delete eikenv;\r
2376                 return /*KErrNoMemory*/;\r
2377         }\r
2378         \r
2379         TRAPD(constructErr,appUi->ConstructL());\r
2380         \r
2381         eikenv->SetAppUi(appUi);        // passing ownership of appUi to coe\r
2382         \r
2383         TInt leaveValue = KErrNone;     \r
2384         if (leaveValue != KErrNone)\r
2385         {\r
2386                 delete eikenv;\r
2387         }\r
2388         else\r
2389         {\r
2390                 // now accept request from clients (start the scheduler)\r
2391                 eikenv->ExecuteD();\r
2392                 //delete eikenv;                                                        // ExecuteD kills eikenv\r
2393                 \r
2394         }\r
2395         \r
2396         delete cleanup;\r
2397         \r
2398         __UHEAP_MARKEND;\r
2399 }\r
2400 \r
2401 //ARM build\r
2402 #ifdef S60V3\r
2403 CPicoDriveApp::CPicoDriveApp()\r
2404 {\r
2405 }\r
2406 \r
2407 CPicoDriveApp::~CPicoDriveApp()\r
2408 {\r
2409 }\r
2410 \r
2411 CApaDocument* CPicoDriveApp::CreateDocumentL()\r
2412 {\r
2413         return new (ELeave) CPicoDriveDoc(*this);\r
2414 }\r
2415 \r
2416 \r
2417 TUid CPicoDriveApp::AppDllUid()const\r
2418 {\r
2419         return TUid::Uid(0xA00007BE);\r
2420 }\r
2421 \r
2422 /**\r
2423    * From @c CApaApplication. Opens the .ini file associated with the\r
2424    * application. By default, ini files are not supported by SERIES60 \r
2425     * applications. If you want to use an ini file, either override this\r
2426     * function to base call @c CEikApplication::OpenIniFileLC, or call it\r
2427     * directly.\r
2428     * @param aFs File server session to use. Not used.\r
2429     * @return Pointer to the dictionary store object representing the\r
2430     * application's .ini file.\r
2431     */\r
2432 CDictionaryStore* CPicoDriveApp::OpenIniFileLC(RFs& aFs) const\r
2433 {\r
2434         return CEikApplication::OpenIniFileLC(aFs);\r
2435 }\r
2436 \r
2437 CPicoDriveDoc::CPicoDriveDoc(CEikApplication& aApp):CAknDocument(aApp)\r
2438 {\r
2439 }\r
2440 \r
2441 CPicoDriveDoc::~CPicoDriveDoc()\r
2442 {\r
2443 }\r
2444 \r
2445 CEikAppUi* CPicoDriveDoc::CreateAppUiL()\r
2446 {\r
2447         return new (ELeave) CPicoDriveUi;\r
2448 }\r
2449 \r
2450 #ifdef S60V3\r
2451 LOCAL_C\r
2452 #endif\r
2453 CApaApplication* NewApplication() {\r
2454         // Return pointer to newly created Application\r
2455         return new CPicoDriveApp;\r
2456 }\r
2457 #include <eikstart.h>\r
2458 #endif\r
2459 GLDEF_C TInt E32Main()\r
2460 {\r
2461 #ifdef S60V3    \r
2462         return EikStart::RunApplication(NewApplication);\r
2463 #else\r
2464         Execute();\r
2465 #endif\r
2466         return KErrNone;\r
2467 }\r
2468 #if defined(__WINS__)\r
2469 \r
2470 #ifndef S60V3\r
2471 EXPORT_C TInt WinsMain()\r
2472 {\r
2473         E32Main();\r
2474         return KErrNone;\r
2475 }\r
2476 #endif\r
2477 \r
2478 extern "C" void my_free(void* anAddress)\r
2479 {\r
2480         if(gChunk != NULL)\r
2481         {\r
2482                 gChunk->Free(anAddress);\r
2483         }\r
2484 }\r
2485 \r
2486 extern "C" void* my_malloc(int aSize)\r
2487 {\r
2488         if(gChunk != NULL)\r
2489         {\r
2490                 return gChunk->Alloc(aSize);\r
2491         }\r
2492  return NULL;\r
2493 }\r
2494 #endif\r
2495 \r