Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHPで学ぶ コンピュータアーキテクチャ / Learning: computer architecture with PHP

PHPで学ぶ コンピュータアーキテクチャ / Learning: computer architecture with PHP

PHPカンファレンス関西2016の発表資料です

HASEGAWA Tomoki

July 16, 2016
Tweet

More Decks by HASEGAWA Tomoki

Other Decks in Technology

Transcript

  1. 今風のカッコいいPHPの書き方 Modern style PHP coding. クールなライブラリやフレームワーク Cool libraries / frameworks.

    PHPをそんな風に使うのか!という驚き Exciting PHP usage. ゲームボーイの仕様の知識 Knowledge of GAMEBOY specs.
  2. 今風のカッコいいPHPの書き方 Modern style PHP coding. クールなライブラリやフレームワーク Cool libraries / frameworks.

    PHPをそんな風に使うのか!という驚き Exciting PHP usage. ゲームボーイの仕様の知識 Knowledge of GAMEBOY specs.
  3. ライフワーク: Web / iOSアプリ開発, ビール, 電子工作,
 サッカー観戦, レンタルカートレース, … 長谷川

    智希 Web / iOS App Development, Beer, IoT, Watch soccer match, Rental Kart Racing, … デジタルサーカス株式会社 副団長CTO Digital Circus, Inc. Vice-master CTO Tokyo, Japan Lifework: @tomzoh
  4. WE ARE HIRING!! Web Development Mobile App Development ( )

    (iOS, Android) http://www.dgcircus.com Omotesando, Tokyo
  5. LR35902 CPUのプログラム Program of CPU LR35902 +0 +1 +2 +3

    +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 0200 42 E0 43 E0 A4 E0 41 E0 01 E0 02 EA 00 D0 EA 01 0210 D0 3E 80 E0 40 F0 44 FE 94 20 FA 3E 03 E0 40 3E 0220 E1 E0 47 E0 48 3E E5 E0 49 21 26 FF 3E 80 32 3E 0230 FF 32 36 77 21 06 FF 3E BF 22 3E 04 77 3E 01 EA 0240 00 20 31 FF CF AF 21 FF DF 06 00 32 05 20 FC 21 0250 FF CF 0E 10 06 00 32 05 20 FC 0D 20 F9 21 FF 9F 0260 0E 20 AF 06 00 32 05 20 FC 0D 20 F9 21 FF FE 06
  6. プログラムの実行 Program execution +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0
  7. プログラムの実行 Program execution +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0
  8. プログラムの実行 Program execution AF 21 DF FF 0E 10 06

    00 32 05 +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0
  9. プログラムの実行 Program execution AF 21 DF FF 0E 10 06

    00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0
  10. プログラムの実行 Program execution AF 21 DF FF 0E 10 06

    00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 AをAでXOR HLに0xFFDFを入れる Cに0x10を入れる Bに0x00を入れる HLが指すアドレスにAの値を入れる Bを-1する Load 0xffdf into HL XOR A with A Load 0x10 into C Load 0x00 into B Load a value A has into memory address HL points at Decrement B
  11. SHARP LR35902 のレジスタ SHARP LR35902 registers ʜ ʜ " '

    # $ % & ) - ʜ ʜ 41 4UBDL1PJOUFS 1$ 1SPHSBN$PVOUFS 8bit / 16bit registers 16bit registers Flag register         ; / ) $     •Z - Zero Flag •N - Subtract Flag •H - Half Carry Flag •C - Carry Flag •0 - Not uses, always zero レジスタ: CPUが持つ、プログラム言語で言う変数 Registers: Variables in programing language.
  12. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  13. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  14. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  15. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  16. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  17. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. PC
  18. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  19. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  20. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  21. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  22. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  23. プログラムカウンタ Program Counter +0 +1 +2 +3 +4 +5 +6

    +7 +8 +9 +A +B +C +D +E +F 01E0 01 E0 85 E1 D1 C1 F1 D9 AF 21 FF DF 0E 10 06 00 01F0 32 05 20 FC 0D 20 F9 3E 0D F3 E0 0F E0 FF AF E0 現在実行中の命令のアドレスを指す特殊なレジスタ。 A special register that contains the address of the instruction being executed now. AF 21 DF FF 0E 10 06 00 32 05 XOR A LD HL, 0xFFDF LD C, 0x10 LD B, 0x00 LD (HL-), A DEC B PC
  24. LR35902 の命令 - LD LR35902 instructions - LD LD BC,

    0x1234 BCレジスタに0x1234を入れる。 Load BC register with 0x1234. LD (BC), A BCレジスタの指し示すメモリアドレスにAレジスタ の内容を入れる。 Load a value A register has into memory BC register points at.
  25. LR35902 の命令 - JP / CD LR35902 instructions - JP

    / CD JP 0x5678 0x5678番地にジャンプする。 Jump to location 0x5678. CALL 0x8181 スタックに現在のアドレスを入れて0x8181番地に ジャンプする。 Push current address to stack pointer, then jump to 0x8181. RET スタック先頭のアドレスにジャンプする。 Jump to the location of top of stack pointer.
  26. LR35902 の命令 - JP / CD LR35902 instructions - JP

    / CD JP 0x5678 0x5678番地にジャンプする。 Jump to location 0x5678. CALL 0x8181 スタックに現在のアドレスを入れて0x8181番地に ジャンプする。 Push current address to stack pointer, then jump to 0x8181. RET スタック先頭のアドレスにジャンプする。 Jump to the location of top of stack pointer. LD PC, 0x5678
  27. LR35902 の命令 - JP / CD LR35902 instructions - JP

    / CD JP 0x5678 0x5678番地にジャンプする。 Jump to location 0x5678. CALL 0x8181 スタックに現在のアドレスを入れて0x8181番地に ジャンプする。 Push current address to stack pointer, then jump to 0x8181. RET スタック先頭のアドレスにジャンプする。 Jump to the location of top of stack pointer.
  28. LR35902 の命令 - JP / CD LR35902 instructions - JP

    / CD JP 0x5678 0x5678番地にジャンプする。 Jump to location 0x5678. CALL 0x8181 スタックに現在のアドレスを入れて0x8181番地に ジャンプする。 Push current address to stack pointer, then jump to 0x8181. RET スタック先頭のアドレスにジャンプする。 Jump to the location of top of stack pointer. LD (SP), PC
 LD SP, SP+2
 LD PC, 0x8181
  29. LR35902 の命令 - JP / CD LR35902 instructions - JP

    / CD JP 0x5678 0x5678番地にジャンプする。 Jump to location 0x5678. CALL 0x8181 スタックに現在のアドレスを入れて0x8181番地に ジャンプする。 Push current address to stack pointer, then jump to 0x8181. RET スタック先頭のアドレスにジャンプする。 Jump to the location of top of stack pointer. LD (SP), PC
 LD SP, SP+2
 LD PC, 0x8181 LD PC, (SP) LD SP, SP-2
  30. ゲームボーイのハードウェアコンポネント CPU I/O Keys Sound Video Display Com Port Memory

    • RAM • Game 
 Cartridge • Video RAM Hardware components of GAMEBOY
  31. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE
  32. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE
  33. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE メモリマップドI/O Memory mapped I/O
  34. class Opcode { public static function run(Core $core, $address) {

    $function = 'opcode'.$address; return Opcode::$function($core); } public static function opcode0(Core $core) { // Do Nothing... } public static function opcode1(Core $core) { $core->registerC = $core->memoryRead($core->programCounter); $core->registerB = $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = ($core->programCounter + 2) & 0xFFFF; } Opcodeの実装 Implementation of opcodes
  35. class Opcode { public static function run(Core $core, $address) {

    $function = 'opcode'.$address; return Opcode::$function($core); } public static function opcode0(Core $core) { // Do Nothing... } public static function opcode1(Core $core) { $core->registerC = $core->memoryRead($core->programCounter); $core->registerB = $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = ($core->programCounter + 2) & 0xFFFF; } Opcodeの実装 Implementation of opcodes 0x00 NOP
  36. class Opcode { public static function run(Core $core, $address) {

    $function = 'opcode'.$address; return Opcode::$function($core); } public static function opcode0(Core $core) { // Do Nothing... } public static function opcode1(Core $core) { $core->registerC = $core->memoryRead($core->programCounter); $core->registerB = $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = ($core->programCounter + 2) & 0xFFFF; } Opcodeの実装 Implementation of opcodes 0x01 LD BC, nn
  37. public static function opcode1(Core $core) { $core->registerC =
 $core->memoryRead($core->programCounter); $core->registerB

    = 
 $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = 
 ($core->programCounter + 2) & 0xFFFF; } LC BC, d16 の実装 Implementation of LD BC, d16 PC PC + 1 0x01 0x34 0x12 LD BC, d16 0x1234 Data Address
  38. public static function opcode1(Core $core) { $core->registerC =
 $core->memoryRead($core->programCounter); $core->registerB

    = 
 $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = 
 ($core->programCounter + 2) & 0xFFFF; } LC BC, d16 の実装 Implementation of LD BC, d16 PC PC + 1 0x01 0x34 0x12 LD BC, d16 0x1234 Data Address BCレジスタに指定したアドレスのメモリの値を入れる。 Load BC register with nn.
  39. public static function opcode1(Core $core) { $core->registerC =
 $core->memoryRead($core->programCounter); $core->registerB

    = 
 $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = 
 ($core->programCounter + 2) & 0xFFFF; } LC BC, d16 の実装 Implementation of LD BC, d16 PC PC + 1 0x01 0x34 0x12 LD BC, d16 0x1234 Data Address PCを次の命令まで進める。 Move PC register to next instruction.
  40. public static function opcode1(Core $core) { $core->registerC =
 $core->memoryRead($core->programCounter); $core->registerB

    = 
 $core->memoryRead(($core->programCounter + 1) & 0xFFFF); $core->programCounter = 
 ($core->programCounter + 2) & 0xFFFF; } LC BC, d16 の実装 Implementation of LD BC, d16 PC PC + 1 0x01 0x34 0x12 LD BC, d16 0x1234 Data Address
  41. $core->memoryRead($core->programCounter); // //Main RAM, MBC RAM, GBC Main RAM, VRAM,

    etc. // //Main Core Memory public $memory = []; Core.php
  42. $core->memoryRead($core->programCounter); // //Main RAM, MBC RAM, GBC Main RAM, VRAM,

    etc. // //Main Core Memory public $memory = []; Core.php メインメモリは配列になっている。 Main memory is defined as array.

  43. $core->memoryRead($core->programCounter); // //Main RAM, MBC RAM, GBC Main RAM, VRAM,

    etc. // //Main Core Memory public $memory = []; Core.php メインメモリは配列になっている。 Main memory is defined as array.
 なぜ $core->memory[$core->programCounter] ではない? Q Why not $core->memory[$core->programCounter] ?
  44. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE
  45. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE A. メモリアクセスはそのアドレスに よって動作が違うから。 A. Because the result of memory access depends on its location. 

  46. ゲームボーイのハードウェアコンポネント CPU I/O Keys Sound Video Display Com Port Memory

    • RAM • Game 
 Cartridge • Video RAM Hardware components of GAMEBOY
  47. ゲームボーイのハードウェアコンポネント CPU I/O Keys Sound Video Display Com Port Memory

    • RAM • Game 
 Cartridge • Video RAM Hardware components of GAMEBOY CPUはキーにI/O経由でアクセスする CPU accesses keys through the I/O
  48. キー状態の取得 public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Get key status
  49. キー状態の取得 public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Get key status 標準入力を開いて… Open standard in and...

  50. キー状態の取得 public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Get key status 標準入力を開いて… Open standard in and...
 fread()で1文字取る! Get a character with fread!
  51. キー状態の取得 Get key status CPU GAMEBOY’s key ゲームボーイのキー CPU I/O

    I/O ゲームボーイソフトウェア GAMEBOY Software ゲームボーイエミュレータ PC keyboard PCのキーボード GAMEBOY Emulator PHP PHP
  52. GAMEBOYのメモリマップ Memory map of GAMEBOY $FFFF Interrupt Enable Flag I/O

    $FF80-$FFFE Zero Page - 127 bytes RAM $FF00-$FF7F Hardware I/O Registers I/O $FEA0-$FEFF Unusable Memory $FE00-$FE9F OAM - Object Attribute Memory Video RAM $E000-$FDFF Echo RAM - Reserved, Do Not Use $D000-$DFFF Internal RAM - Bank 1-7 (switchable - CGB only) RAM $C000-$CFFF Internal RAM - Bank 0 (fixed) $A000-$BFFF Cartridge RAM (If Available) Cartridge $9C00-$9FFF BG Map Data 2 Video RAM $9800-$9BFF BG Map Data 1 $8000-$97FF Character RAM $4000-$7FFF Cartridge ROM - Switchable Banks 1-xx Cartridge $0150-$3FFF Cartridge ROM - Bank 0 (fixed) $0100-$014F Cartridge Header Area $0000-$00FF Restart and Interrupt Vectors ROM IUUQXXXIVDLpOHBNFTDPN QJE #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH  1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE
  53. I/O 0xFF00 キー状態 FF00 -- JOYPAD [RW] Joypad port Bit5

    Bit4 | In order to scan the keys, output 0 into either Bit4 Bit3 DOWN START | or Bit5 of JOYPAD, wait for some time and read JOYPAD. Bit2 UP SELECT | Bits 0-3 will be set to zeroes if corresponding Bit1 LEFT B | buttons are pressed. Bits 6 and 7 are not used. Bits Bit0 RIGHT A | 0-3 are connected to input lines P10-P13. Bits 4 and 5 | are connected to ouput lines P14 and P15. IUUQGNTLPNLPOPSH(BNF#PZ5FDI4PGUXBSFIUNM キーの状態を読み取るには、0xFF00のビット4とビット5に 値を書き込み、その後ビット0〜ビット3の値を読み込む。 ビット4とビット5に書き込んだ値によってビット0〜ビッ ト3がどのキーに対応するかが変化する。 I/O 0xFF00 Key status
  54. I/O 読み込み public function memoryRead($address) { if ($address < 0x4000)

    { } elseif ($address >= 0xFF00) { switch ($address) { case 0xFF00: return 0xC0 | $this->memory[0xFF00]; break; I/O read
  55. I/O 書き込み I/O write 7 6 5 4 3 2

    1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  56. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  57. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  58. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  59. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  60. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00
  61. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  62. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  63. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  64. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  65. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  66. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  67. public function memoryWrite($address, $data) { if ($address < 0x8000) {

    //I/O Registers (GB + GBC): } elseif ($address == 0xFF00) { $this->memory[0xFF00] = ($data & 0x30) | (((($data & 0x20) == 0) ? ($this->JoyPad >> 4) : 0xF) & ((($data & 0x10) == 0) ? ($this->JoyPad & 0xF) : 0xF)); } elseif ($address == 0xFF02) { I/O 書き込み I/O write 7 6 5 4 3 2 1 0 - - P15 out P14 out P13 in P12 in P11 in P10 in - - 0 1 A B Select Start - - 1 0 → ← ↑ ↓ I/O port 0xFF00 7 6 5 4 3 2 1 0 A B Select Start → ← ↑ ↓ $this->JoyPad:
  68. セットアップ How to run $ git clone https://github.com/gabrielrcouto/ php-terminal-gameboy-emulator.git $

    cd php-terminal-gameboy-emulator $ composer install -o $ bin/php-gameboy drmario.rom
  69. アドレスバスとデータバス Data bus and address bus メモリ空間64KB 0x0000 to 0xffff

    0000000000000000 to 1111111111111111 16bits ビデオRAM8KB 0x0000 to 0x1fff 0000000000000000 to 0001111111111111 13bits
  70. エンディアン Endian PC PC + 1 0x01 0x34 0x12 LD

    BC, nn 0x1234 Data Address LD BC, 0x1234 BCレジスタに0x1234を入れる。 Load BC register with 0x1234.
  71. ストリームの非ブロックモード public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Non-blocking mode on a stream
  72. ストリームの非ブロックモード public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Non-blocking mode on a stream
  73. ストリームの非ブロックモード public function __construct(Core $core) { $this->core = $core; exec('stty

    -icanon -echo'); $this->file = fopen('php://stdin', 'r'); stream_set_blocking($this->file, false); } public function check() { $key = fread($this->file, 1); if (!empty($key)) { $this->keyDown($key); } elseif (!empty($this->keyPressing)) { $this->keyUp($this->keyPressing); } $this->keyPressing = $key; } Non-blocking mode on a stream ストリームのモードを非ブロックに設定 Set non-blocking mode on a stream.