Successfully reported this slideshow.
Your SlideShare is downloading. ×

Reading php terminal-gameboy-emulator

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Lcd
Lcd
Loading in …3
×

Check these out next

1 of 74 Ad

More Related Content

Similar to Reading php terminal-gameboy-emulator (20)

More from Tomoki Hasegawa (20)

Advertisement

Recently uploaded (20)

Reading php terminal-gameboy-emulator

  1. 1. Reading php-terminal-gameboy-emulator Digital Circus, Inc. HASEGAWA Tomki
  2. 2. HASEGAWA Tomoki @tomzoh Web / iOS App Development, Rental Kart Race, Beer, IoT crafting, … hasegawa
 tomoki Digital Circus, Inc. Vice-master CTO Tokyo, Japan Interests
  3. 3. ・ Web Development ・ Mobile Apps Development ・ Service ( ) (iOS, Android) (http://appbuilder.jp) Twitter: @tomzoh
  4. 4. Twitter: @tomzoh
  5. 5. Today’s theme Reading php-terminal-gameboy-emulator
  6. 6. Today’s theme Reading php-terminal-gameboy-emulator
  7. 7. By Transfrom JPEG version and remove white background. Original by Evan-Amos. - Media:Game-Boy-FL.jpg, Public Domain, https://commons.wikimedia.org/w/index.php?curid=37808150
  8. 8. 23, February
  9. 9. 😮
  10. 10. 😮
  11. 11. 😮 Awesome!!
  12. 12. 😮 Awesome!! Waste of talent!!
  13. 13. 😮 Awesome!! Waste of talent!! Cool!!
  14. 14. Source Files
  15. 15. Source Files It’s PHP !
  16. 16. Source Files It’s PHP ! We can read it.
  17. 17. Source Files It’s PHP ! Yes, We can! We can read it.
  18. 18. Source Files It’s PHP ! Yes, We can! Yes, We can! We can read it.
  19. 19. Today’s theme Reading php-terminal-gameboy-emulator
  20. 20. Components of GAMEBOY • CPU / Memory • Display • Sound • Buttons • Communication Port
  21. 21. Components of GAMEBOY • CPU / Memory • Display • Sound • Buttons • Communication Port
  22. 22. Buttons
  23. 23. Buttons - Keyboard.php <?php namespace GameBoy; class Keyboard { public $core; public $file; public $keyPressing = null; public $started = false; public function __construct(Core $core) { $this->core = $core; exec('stty -icanon'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() {
  24. 24. Buttons - Keyboard.php <?php namespace GameBoy; class Keyboard { public $core; public $file; public $keyPressing = null; public $started = false; public function __construct(Core $core) { $this->core = $core; exec('stty -icanon'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() {
  25. 25. How to sense buttons
  26. 26. How to sense buttons $this->file = fopen('php://stdin', 'r');
  27. 27. How to sense buttons $this->file = fopen('php://stdin', 'r'); $key = fread($this->file, 1);
  28. 28. Simple
  29. 29. CPU / Memory
  30. 30. CPU
  31. 31. SHARP LR35902 instruction set http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html 1byte instructions Prefix CB instructions 144 instructions 256 instructions+ = 400 instructions
  32. 32. SHARP LR35902 resistors http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html 15 … 8 7 … 0 A F B C H L 15 … 8 7 … 0 SP (Stack Pointer) PC (Program Counter) 8bit / 16bit resistors 16bit resistors Flag registor 7 6 5 4 3 2 1 0 Z N H C 0 0 0 0 •Z - Zero Flag •N - Subtract Flag •H - Half Carry Flag •C - Carry Flag •0 - Not uses, always zero
  33. 33. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - -
  34. 34. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Length in bytes Address LD A, d8 2 8 - - - -
  35. 35. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Duration in cyclesLength in bytes Address LD A, d8 2 8 - - - -
  36. 36. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Duration in cycles Flags affected Length in bytes Address LD A, d8 2 8 - - - -
  37. 37. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - -
  38. 38. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - PC (Program Counter)
  39. 39. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Load 0x0301 into BC resistor x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - PC (Program Counter)
  40. 40. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - PC (Program Counter)
  41. 41. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - Load 0x99 into A resistor PC (Program Counter)
  42. 42. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - PC (Program Counter)
  43. 43. Instruction in memory 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Load A resistor’s value into memory indicates with BC resistor x0 x1 x2 xE 0x 3x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Address LD A, d8 2 8 - - - - PC (Program Counter)
  44. 44. CPU - Opcode.php <?php namespace GameBoy; class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { //Do Nothing... }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) {
  45. 45. CPU - Opcode.php <?php namespace GameBoy; class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { //Do Nothing... }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { x0 x1 x2 0x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - -
  46. 46. CPU - Opcode.php <?php namespace GameBoy; class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { //Do Nothing... }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { Implementation of 0x00 NOP x0 x1 x2 0x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - -
  47. 47. CPU - Opcode.php <?php namespace GameBoy; class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { //Do Nothing... }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { x0 x1 x2 0x LD BC, d16 3 12 - - - - NOP 1 4 - - - - LD (BC), A 1 8 - - - - Implementation of 0x01 LD BC, d16
  48. 48. //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { $parentObj->registerC = $parentObj->memoryReader[$parentObj->programCounter]($parentObj, $parentObj->programCounter); $parentObj->registerB = $parentObj->memoryRead(($parentObj->programCounter + 1) & 0xFFFF); $parentObj->programCounter = ($parentObj->programCounter + 2) & 0xFFFF; }; Implementation of LD BC, d16 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Address PC (Program Counter)
  49. 49. //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { $parentObj->registerC = $parentObj->memoryReader[$parentObj->programCounter]($parentObj, $parentObj->programCounter); $parentObj->registerB = $parentObj->memoryRead(($parentObj->programCounter + 1) & 0xFFFF); $parentObj->programCounter = ($parentObj->programCounter + 2) & 0xFFFF; }; Implementation of LD BC, d16 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Address PC (Program Counter) C
  50. 50. //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { $parentObj->registerC = $parentObj->memoryReader[$parentObj->programCounter]($parentObj, $parentObj->programCounter); $parentObj->registerB = $parentObj->memoryRead(($parentObj->programCounter + 1) & 0xFFFF); $parentObj->programCounter = ($parentObj->programCounter + 2) & 0xFFFF; }; Implementation of LD BC, d16 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Address PC (Program Counter) B
  51. 51. //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { $parentObj->registerC = $parentObj->memoryReader[$parentObj->programCounter]($parentObj, $parentObj->programCounter); $parentObj->registerB = $parentObj->memoryRead(($parentObj->programCounter + 1) & 0xFFFF); $parentObj->programCounter = ($parentObj->programCounter + 2) & 0xFFFF; }; Implementation of LD BC, d16 0x0000 0x0001 0x0002 0x003 0x004 0x005 0x0301 0x01 0x01 0x03 0x3e 0x99 0x02 0x99 LD BC, d16 0x0301 LD A, d8 0x99 LD (BC), A Data Instruction Address PC (Program Counter)
  52. 52. Implementation of JP a16 x3 cx JP a16 3 16 - - - -
  53. 53. Implementation of JP a16 x3 cx JP a16 3 16 - - - - Jump to address a16
  54. 54. Implementation of JP a16 x3 cx JP a16 3 16 - - - - Jump to address a16 0x0000 0x0001 0x0002 0xc3 0x29 0x03 JP a16 0x0329 Data Instruction Address
  55. 55. Implementation of JP a16 x3 cx JP a16 3 16 - - - - Jump to address a16 0x0000 0x0001 0x0002 0xc3 0x29 0x03 JP a16 0x0329 Data Instruction Address Jump to address 0x0329
  56. 56. Implementation of JP a16 x3 cx JP a16 3 16 - - - - Jump to address a16 //JP nn //#0xC3: $this->functionsArray[] = function ($parentObj) { $parentObj->programCounter = ($parentObj->memoryRead(($parentObj->programCounter + 1) & 0xFFFF) << 8) + $parentObj->memoryReader[$parentObj->programCounter]($parentObj, $parentObj->programCounter); }; 0x0000 0x0001 0x0002 0xc3 0x29 0x03 JP a16 0x0329 Data Instruction Address Jump to address 0x0329
  57. 57. Run loop public function executeIteration() { $op = 0; while ($this->stopEmulator == 0) { $op = $this->memoryRead($this->programCounter); $this->programCounter = ($this->programCounter + 1) & 0xFFFF; $this->CPUTicks = $this->TICKTable[$op]; $this->OPCODE[$op]($this); $this->updateCore(); } }
  58. 58. Not difficult Simple, Easy
  59. 59. Let’s read php-terminal-gameboy-emulator
  60. 60. Thanks @tomzoh WE ARE HIRING Drupal / Mobile App Engineer
 Digital Circus, Inc. Tokyo, Japan @tomzoh
  61. 61. Links • https://en.wikipedia.org/wiki/Game_Boy • http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html • http://gbdev.gg8.se/wiki/articles/The_Cartridge_Header • http://gameboy.mongenel.com/dmg/asmmemmap.html
  62. 62. More FPS? PHP5.5.30 OSX Native FPS: 4 PHP7.0.1 VirtualBox FPS: 14
  63. 63. Duration in cycles table <?php namespace GameBoy; class TickTables { public static $primary = [ //Number of machine cycles for each instruction: /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F*/ 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, //0 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, //1 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, //2 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, //3
  64. 64. Duration in cycles table <?php namespace GameBoy; class TickTables { public static $primary = [ //Number of machine cycles for each instruction: /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F*/ 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, //0 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, //1 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, //2 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, //3 1 machine cycle = 4 clocks
  65. 65. Duration in cycles table <?php namespace GameBoy; class TickTables { public static $primary = [ //Number of machine cycles for each instruction: /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F*/ 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, //0 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, //1 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, //2 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, //3 1 machine cycle = 4 clocks x4 cx CALL NZ,a16 3 24/12 - - - -
  66. 66. Duration in cycles table <?php namespace GameBoy; class TickTables { public static $primary = [ //Number of machine cycles for each instruction: /* 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F*/ 1, 3, 2, 2, 1, 1, 2, 1, 5, 2, 2, 2, 1, 1, 2, 1, //0 1, 3, 2, 2, 1, 1, 2, 1, 3, 2, 2, 2, 1, 1, 2, 1, //1 2, 3, 2, 2, 1, 1, 2, 1, 2, 2, 2, 2, 1, 1, 2, 1, //2 2, 3, 2, 2, 3, 3, 3, 1, 2, 2, 2, 2, 1, 1, 2, 1, //3 1 machine cycle = 4 clocks x4 cx CALL NZ,a16 3 24/12 - - - -Call address a16 if A resistor is zero. It takes 24 clocks.
  67. 67. 5月 チケット販売開始 /トーク募集開始
 6月上旬 トーク募集〆切
 6月下旬 タイムテーブル決定 iOS Developers Conference Japan 2016 2016.08.20 https://iosdc.jp スポンサー企業さま、大絶賛募集中

×