Advertisement
Mysoft

Untitled

May 23rd, 2025
102
0
18 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifdef __FB_JS__
  2.   #cmdline "-s gui -Wl '--embed-file rom --embed-file romboot'"
  3. #endif
  4.  
  5. #include "Sameboy.bi"
  6. #include "fbgfx.bi"
  7.  
  8. #ifdef __FB_WIN32__
  9.   #include "windows.bi"
  10.   #include "win\mmsystem.bi"
  11.   #include "MyTDT\minisnd.bas"
  12.   TimeBeginPeriod(1)
  13.   #include "MyTDT\GfxResize.bas"
  14.   #define UseResize  
  15. #endif
  16.  
  17. 'static shared as uint32_t bitmap(256*224-1)
  18.  
  19. 'GB_random_set_enabled(false);
  20.  
  21. dim as GB_gameboy_t ptr pgb
  22.  
  23. #define _romboot "romboot/"
  24. '#define perror(_s) printf(!"Error: '%s'\n",_s): sleep
  25.  
  26. const sFilename = "rom/Silver.gbc" 'PokeBlue-II - 2xSPD.gb"
  27. 'const sFilename = "G:\BackupH\FromData\Emu\GBC\GBC_bgb\Roms\PokeBlue-II - 2xSPD.gb"
  28.  
  29. private function rgb_encode cdecl (gb as GB_gameboy_t ptr, r as uint8_t, g as uint8_t,b as uint8_t) as uint32_t
  30.   return rgb(r,g,b)
  31. end function
  32.  
  33. 'pgb = GB_init( GB_alloc() , GB_MODEL_DMG_B ) : GB_load_boot_rom(pgb, _romboot "dmg_boot.bin")
  34. pgb = GB_init( GB_alloc() , GB_MODEL_SGB2  ) : GB_load_boot_rom(pgb, _romboot "sgb2_boot.bin")
  35. 'pgb = GB_init( GB_alloc() , GB_MODEL_CGB_E )  : GB_load_boot_rom(pgb, _romboot "cgb_boot.bin")
  36. 'pgb = GB_init( GB_alloc() , GB_MODEL_CGB_E ) : GB_load_boot_rom(pgb, _romboot "cgb_boot.bin")
  37. 'pgb = GB_init( GB_alloc() , GB_MODEL_GBP_A ) : GB_load_boot_rom(pgb, _romboot "cgb_boot.bin")
  38.  
  39. GB_set_border_mode(pgb, GB_BORDER_ALWAYS)
  40.  
  41. 'var ScrWid = GB_get_screen_width(pgb) , ScrHei = GB_get_screen_height(pgb)
  42. const ScrWid = 256 , ScrHei = 224
  43.  
  44. #ifdef UseResize
  45. gfx.PreResize()
  46. #endif
  47. screenres ScrWid,ScrHei,32
  48. #ifdef UseResize
  49. gfx.Resize(ScrWid*2,ScrHei*2)
  50. #endif
  51.  
  52. 'GB_set_vblank_callback(pgb, (GB_vblank_callback_t) vblank);
  53. GB_set_pixels_output(pgb, screenptr)
  54. GB_set_rgb_encode_callback(pgb, @rgb_encode)
  55.  
  56. 'GB_set_log_callback(&gb, log_callback)
  57. 'GB_set_async_input_callback(pgb, async_input_callback);
  58. GB_set_color_correction_mode(pgb, GB_COLOR_CORRECTION_EMULATE_HARDWARE)
  59. 'GB_set_rtc_mode(&gb, GB_RTC_MODE_ACCURATE);
  60. GB_set_emulate_joypad_bouncing(pgb, false) '// Adds too much noise
  61. GB_set_rewind_length(pgb,0)
  62.  
  63. if GB_load_rom(pgb, sFilename) then perror("Failed to load ROM"): end 1
  64.  
  65. dim as byte running = true
  66. dim as long frames = 0
  67. dim as uint64_t lltime = 0
  68.  
  69. scope 'try to grab SGB border
  70.   const ChkPix = 256*((224-144)\2)
  71.   dim as ulong lTest(ChkPix-1), N, DiffIn,DiffOut
  72.   line(0,0)-(256,224),-1,bf : sleep 50,1
  73.   dim as double dTmr = timer
  74.   screenlock : GB_run_frame(pgb)
  75.   memcpy( @lTest(0) , screenptr+(((224*256)-ChkPix)*4) , ChkPix*4 )
  76.   GB_set_turbo_mode( pgb , true , false )
  77.   GB_set_rendering_disabled( pgb , true )
  78.   for N = 0 to 599
  79.     GB_run_frame(pgb)
  80.     if N=300 then GB_set_rendering_disabled( pgb , false )
  81.     if N>300 andalso memcmp( @lTest(0) , screenptr+(((224*256)-ChkPix)*4) , ChkPix*4 ) then
  82.       memcpy( @lTest(0) , screenptr+(((224*256)-ChkPix)*4) , ChkPix*4 )      
  83.       DiffIn += 1 : DiffOut = 0
  84.     elseif DiffIn then
  85.       DiffOut += 1 ': DiffIn = 0
  86.       if DiffOut = 10 then exit for    
  87.     end if
  88.     'screenunlock : sleep 1,1 : screenlock
  89.   next N    
  90.   'printf(!"%i\n",N)  
  91.   GB_set_turbo_mode( pgb , false , false )
  92.   GB_set_rendering_disabled( pgb , false )  
  93.   GB_switch_model_and_reset( pgb , GB_MODEL_CGB_E ) : GB_load_boot_rom(pgb, _romboot "cgb_boot.bin")
  94.   'GB_switch_model_and_reset( pgb , GB_MODEL_DMG_B ) : GB_load_boot_rom(pgb, _romboot "dmg_boot.bin")
  95.   if N = 600 then GB_run_frame(pgb)
  96.   GB_set_border_mode(pgb, GB_BORDER_NEVER)
  97.   line ((256-160)\2,(224-144)\2)-step(159,143),-1,bf
  98.   screenunlock
  99.   'printf(!"%f\n",(timer-dTMR)*1000)
  100.   'printf("%i ",GB_is_cgb_in_cgb_mode(pgb))
  101. end scope  
  102.  
  103. dim shared as long iFrameSkip=0+1
  104. dim as fb.image ptr pScr = ImageCreate(160,144)
  105. GB_set_pixels_output(pgb, cast(any ptr,pScr+1))
  106.  
  107. function iScanToGameboy( iCode as long ) as long
  108.   select case iCode
  109.   case fb.SC_A , fb.SC_RIGHT : return GB_KEY_RIGHT
  110.   case fb.SC_D , fb.SC_LEFT  : return GB_KEY_LEFT
  111.   case fb.SC_W , fb.SC_UP    : return GB_KEY_UP
  112.   case fb.SC_S , fb.SC_DOWN  : return GB_KEY_DOWN
  113.   case fb.SC_Q , fb.SC_SPACE : return GB_KEY_A
  114.   case fb.SC_E , fb.SC_CONTROL  : return GB_KEY_B
  115.   case fb.SC_Z , fb.SC_ENTER: return GB_KEY_START
  116.   case fb.SC_X , fb.SC_LSHIFT , fb.SC_RSHIFT : return GB_KEY_SELECT
  117.   end select
  118.   return -1
  119. end function
  120. sub handle_buttons cdecl ( gb as GB_gameboy_t ptr )
  121.   dim as fb.event e
  122.   while screenevent( @e )
  123.     select case e.type
  124.     Case fb.EVENT_KEY_PRESS
  125.       select case e.ascii
  126.       case asc("+") : if iFrameSkip < (9+1) then iFrameSkip += 1
  127.       case asc("-") : if iFrameSkip > (0+1) then iFrameSkip -= 1
  128.       end select
  129.       var iCode = iScanToGameboy( e.scancode )
  130.       if iCode >= 0 then GB_set_key_state( gb , iCode , true )
  131.     Case fb.EVENT_KEY_RELEASE
  132.       var iCode = iScanToGameboy( e.scancode )
  133.       if iCode >= 0 then GB_set_key_state( gb , iCode , false )
  134.     end select
  135.   wend  
  136. end sub
  137.  
  138. const AudioFreq = 44100
  139. static shared as WaveOutStruct ptr pAudio
  140. pAudio = AudioOpen( AudioFreq , 16 , 2 , 2 )
  141.  
  142. sub EmitAudioSample cdecl (pgb as GB_gameboy_t ptr, sample as GB_sample_t)
  143.   const cBlkSam = AudioFreq\10
  144.   static as GB_sample_t tSample(cBlkSam*16-1)
  145.   static as long iCnt : tSample(iCnt) = sample : iCnt += 1
  146.   if iCnt >= cBlkSam then
  147.     if AudioWrite( pAudio , @tSample(0) , cBlkSam*sizeof(GB_sample_t) ) < 0 then      
  148.       iCnt -= 2
  149.     else
  150.       iCnt = 0
  151.     end if
  152.   end if
  153. end sub
  154.  
  155. GB_set_update_input_hint_callback( pgb , @handle_buttons )
  156. GB_set_turbo_mode( pgb , true , false )
  157. GB_apu_set_sample_callback( pgb , @EmitAudioSample )
  158. GB_set_sample_rate( pgb , AudioFreq )
  159. GB_set_highpass_filter_mode( pgb , GB_HIGHPASS_ACCURATE )
  160.  
  161. var dSpeed = 1/(GB_get_usual_frame_rate(pgb))
  162. var bBorder = 0
  163.  
  164. dim as double dFPS,dFpsAmount,dTMR
  165. dim as long iSkip
  166. dFPS = timer
  167. while (running)
  168.     if iSkip=0 then GB_set_rendering_disabled( pgb , true )
  169.     if iSkip>=(iFrameSkip-1) then GB_set_rendering_disabled( pgb , false ) : iSkip = -1
  170.     lltime += GB_run_frame(pgb)        
  171.     frames += 1 : iSkip += 1
  172.     if iSkip=0 then 'iSkip>=iFrameSkip then            
  173.       put( (256-160)\2 , (224-144)\2 ),pScr,pset
  174.       var dTimer = timer
  175.       if abs(dTimer-dTMR) > 1 then dTMR = dTimer
  176.       var dTemp = dTimer-dFPS
  177.       if dTemp >= .25 then
  178.         dFpsAmount = 100*((dSpeed*frames)/dTemp)
  179.         dFPS = dTimer : frames = 0
  180.         dim as zstring*32 zTemp = any
  181.         sprintf(zTemp,"%3.1f%% ",dFpsAmount)
  182.         locate 1,1 : print zTemp
  183.       end if        
  184.       #if 0
  185.         sleep 0,1
  186.       #else
  187.         if multikey(fb.SC_BACKSPACE)=0 then
  188.           while (timer-dTMR) < dSpeed
  189.             sleep 1,1
  190.           wend
  191.           dTMR += dSpeed    
  192.         else
  193.           dTMR = timer
  194.         end if
  195.       #endif
  196.      
  197.      
  198.     end if
  199.    
  200. wend
  201.  
  202. screenunlock
  203.  
  204. GB_free(pgb)
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement