Slide 1

Slide 1 text

Learning: Computer Architecture Using php-terminal-gameboy-emulator
 PHPで学ぶコンピュータアーキテクチャ Digital Circus, Inc. HASEGAWA Tomoki

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Slide 4

Slide 4 text

IUUQZBOEPEHJUIVCJPBSDIJWFQIQTUVEZ

Slide 5

Slide 5 text

勉強会 • 楽しみましょう! • 今年はPHPカンファレンスが、北海道、福岡、関西、東京 都開催されます。 • 普段の勉強会とはまた違った楽しみが! • 遠征ならではの楽しさも!長谷川全通予定。 • お友達がいるとより楽しいので是非PHP勉強会でお友達を 作ってみてください。

Slide 6

Slide 6 text

QIQTUVEZ IUUQTMFHBDZKPJOEJOUBMLWJFX

Slide 7

Slide 7 text

QIQTUVEZ IUUQTMFHBDZKPJOEJOUBMLWJFX

Slide 8

Slide 8 text

HASEGAWA Tomoki @tomzoh Web / Mobile App Development, Watch soccer matches, Rental Kart Racing, Beer, IoT, … hasegawa
 tomoki Digital Circus, Inc. Vice-master CTO Tokyo, Japan Interests

Slide 9

Slide 9 text

Yesterday…

Slide 10

Slide 10 text

ɾ 8FC%FWFMPQNFOU ɾ .PCJMF"QQ%FWFMPQNFOU ɾ 4FSWJDF ʢʣ ʢJ04 "OESPJE ʢIUUQBQQCVJMEFSKQʣ 5XJUUFS!UPN[PI

Slide 11

Slide 11 text

5XJUUFS!UPN[PI

Slide 12

Slide 12 text

Today’s theme Learning: Computer Architecture Using php-terminal-gameboy-emulator
 PHPで学ぶコンピュータアーキテクチャ

Slide 13

Slide 13 text

Today’s theme Learning: Computer Architecture Using php-terminal-gameboy-emulator
 PHPで学ぶコンピュータアーキテクチャ

Slide 14

Slide 14 text

#Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH 1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE

Slide 15

Slide 15 text

23, February

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Awesome!!

Slide 21

Slide 21 text

Awesome!! Waste of talent!!

Slide 22

Slide 22 text

Awesome!! Waste of talent!! Cool!!

Slide 23

Slide 23 text

Source Files

Slide 24

Slide 24 text

Source Files OMG. It’s PHP !

Slide 25

Slide 25 text

Source Files OMG. It’s PHP ! We can read it.

Slide 26

Slide 26 text

Source Files OMG. It’s PHP ! Yes, We can! We can read it.

Slide 27

Slide 27 text

Source Files OMG. It’s PHP ! Yes, We can! Yes, We can! We can read it.

Slide 28

Slide 28 text

Today’s theme Learning: Computer Architecture Using php-terminal-gameboy-emulator
 PHPで学ぶコンピュータアーキテクチャ

Slide 29

Slide 29 text

Components of GAMEBOY • CPU / Memory • Display • Sound • Buttons • Communication Port • ROM Cartridge #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH 1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE

Slide 30

Slide 30 text

Components of GAMEBOY • CPU / Memory • Display • Sound • Buttons • Communication Port • ROM Cartridge #Z5SBOTGSPN+1&(WFSTJPOBOESFNPWFXIJUFCBDLHSPVOE0SJHJOBMCZ&WBO"NPT.FEJB(BNF#PZ'-KQH 1VCMJD%PNBJO IUUQTDPNNPOTXJLJNFEJBPSHXJOEFYQIQ DVSJE

Slide 31

Slide 31 text

CPU IUUQTFOXJLJQFEJBPSHXJLJ(BNF@#PZ

Slide 32

Slide 32 text

CPU IUUQTFOXJLJQFEJBPSHXJLJ(BNF@#PZ

Slide 33

Slide 33 text

Common CPU architecture • Registers
 レジスタ • Memory Access
 メモリアクセス • Program Counter
 プログラムカウンタ • Instruction Set
 命令セット • I/O Access, Interrupt
 I/Oアクセス, 割り込み

Slide 34

Slide 34 text

Memory Access メモリアクセス • Memory stores data including program.
 メモリには命令を含むデータが格納される。 • Each data is 8bit value and has address. The address is represented as hex value. 
 それぞれのデータは8bit=1byteの値で、16進数で表現されるアドレスを持つ。 IUUQXXXNKDOOFUKQQPUBGVMMNTYNPOIUNM

Slide 35

Slide 35 text

Memory Access メモリアクセス • Memory stores data including program.
 メモリには命令を含むデータが格納される。 • Each data is 8bit value and has address. The address is represented as hex value. 
 それぞれのデータは8bit=1byteの値で、16進数で表現されるアドレスを持つ。 IUUQXXXNKDOOFUKQQPUBGVMMNTYNPOIUNM

Slide 36

Slide 36 text

Registers レジスタ • Variables in programing language.
 プログラム言語で言うところの変数。 • Stores only integer value.
 整数値のみ格納可能。 • LR35902 has 7 8bit registers and 2 16bit registers.
 LR35902は7つの8bitレジスタと2つの16ビットレジスタを持っている。 • 8bit registers of LR35902 can be used as 16bit register.
 LR35902の8bitレジスタは16ビットレジスタとしても使える。

Slide 37

Slide 37 text

SHARP LR35902 registers http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html ʜ ʜ " ' # $ ) - ʜ ʜ 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

Slide 38

Slide 38 text

Program Counter プログラムカウンタ • A special register that contains the address of the instruction being executed now.
 現在実行中の命令のアドレスを指す特殊なレジスタ。 • As each instruction gets fetched, the program counter incremented, and holds the memory address of the next instruction.
 命令が読み込まれるとプログラムカウンタの値は加算され、次の命令のアドレスを指 す。

Slide 39

Slide 39 text

Instruction Set 命令セット • Only primitive instructions.
 CPUは通常原始的な命令しか持たない。 • Instruction set: load a value into register, jump, conditional jump etc.
 レジスタに値を入れる、ジャンプする、条件付きでジャンプする、などの命令の集合。 • No functions, No loops. Some CPU including LR35902 has no multiplication / division.
 関数やループ命令は無い。LR35902を含むいくつかのCPUではかけ算や割り算命令も無 い。

Slide 40

Slide 40 text

SHARP LR35902 instruction set http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html 1byte instructions Prefix CB instructions 243 instructions 256 instructions + = 499 instructions

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

LR35902 instruction - JP / JR JP 0x5678 Jump to address 0x5678.
 0x5678番地にジャンプする。 JR C,0x20 Jump to calculated address with respect to itself if carry flag is set.
 前の命令でキャリーフラグがセットされていれば現在の番地 +0x20番地にジャンプする。

Slide 43

Slide 43 text

Assembler / Machine Code 38 27 2A 28 B6 7C B7 20 11 CD E1 1C 30 1B JR C, 0x27 LD HL, (0xB628) LD A,H OR A JR NZ, 0x11 CALL 0x1CE1 JR NC, 0x1B 38 27 2A 28 B6 7C B7 20 11 CD E1 1C 30 1B

Slide 44

Slide 44 text

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 Memory Instruction x0 x1 x2 xE 0x 3x 0x01 LD BC, d16 0x00 NOP 0x02 LD (BC), A Address 0x3e LD A, d8

Slide 45

Slide 45 text

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 Memory Instruction Load 0x0301 into BC register x0 x1 x2 xE 0x 3x 0x01 LD BC, d16 0x00 NOP 0x02 LD (BC), A Address 0x3e LD A, d8 PC (Program Counter)

Slide 46

Slide 46 text

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 Memory Instruction x0 x1 x2 xE 0x 3x 0x01 LD BC, d16 0x00 NOP 0x02 LD (BC), A Address 0x3e LD A, d8 Load 0x99 into A register PC (Program Counter)

Slide 47

Slide 47 text

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 Memory Instruction Load A register’s value into memory indicates with BC register x0 x1 x2 xE 0x 3x 0x01 LD BC, d16 0x00 NOP 0x02 LD (BC), A Address 0x3e LD A, d8 PC (Program Counter)

Slide 48

Slide 48 text

Implementation with

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

CPU - Opcode.php class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { }; //LD (BC), A //#0x02: $this->functionsArray[] = function ($parentObj) { };

Slide 52

Slide 52 text

CPU - Opcode.php class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { }; //LD (BC), A //#0x02: $this->functionsArray[] = function ($parentObj) { }; Implementation of instructions 命令の実装

Slide 53

Slide 53 text

CPU - Opcode.php class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { }; //LD (BC), A //#0x02: $this->functionsArray[] = function ($parentObj) { }; 0x00 NOP Implementation of instructions 命令の実装

Slide 54

Slide 54 text

CPU - Opcode.php class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { }; //LD (BC), A //#0x02: $this->functionsArray[] = function ($parentObj) { }; 0x01 LD BC, d16 Implementation of instructions 命令の実装

Slide 55

Slide 55 text

CPU - Opcode.php class Opcode { public $functionsArray = []; public function __construct() { //NOP //#0x00: $this->functionsArray[] = function ($parentObj) { }; //LD BC, nn //#0x01: $this->functionsArray[] = function ($parentObj) { }; //LD (BC), A //#0x02: $this->functionsArray[] = function ($parentObj) { }; 0x02 LD (BC), A Implementation of instructions 命令の実装

Slide 56

Slide 56 text

//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 0x01 0x01 0x03 0x3e 0x99 LD BC, d16 0x0301 LD A, d8 0x99 Data Instruction Address PC (Program Counter)

Slide 57

Slide 57 text

//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 0x01 0x01 0x03 0x3e 0x99 LD BC, d16 0x0301 LD A, d8 0x99 Data Instruction Address PC (Program Counter) C

Slide 58

Slide 58 text

//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 0x01 0x01 0x03 0x3e 0x99 LD BC, d16 0x0301 LD A, d8 0x99 Data Instruction Address PC (Program Counter) B

Slide 59

Slide 59 text

//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 0x01 0x01 0x03 0x3e 0x99 LD BC, d16 0x0301 LD A, d8 0x99 Data Instruction Address PC (Program Counter)

Slide 60

Slide 60 text

Implementation of JP a16 0x0000 0x0001 0x0002 0xc3 0x29 0x03 JP a16 0x0329 Data Instruction Address PC (Program Counter)

Slide 61

Slide 61 text

Implementation of JP a16 0x0000 0x0001 0x0002 0xc3 0x29 0x03 JP a16 0x0329 Data Instruction Address Jump to address 0x0329 PC (Program Counter)

Slide 62

Slide 62 text

Implementation of JP 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 PC (Program Counter)

Slide 63

Slide 63 text

Not difficult

Slide 64

Slide 64 text

Simple, Easy

Slide 65

Slide 65 text

Let’s read php-terminal-gameboy-emulator

Slide 66

Slide 66 text

Learning: Computer Architecture Using php-terminal-gameboy-emulator
 PHPで学ぶコンピュータアーキテクチャ

Slide 67

Slide 67 text

Thanks @tomzoh WE ARE HIRING Drupal / Mobile App Engineer
 Digital Circus, Inc. Tokyo, Japan @tomzoh

Slide 68

Slide 68 text

Thanks @tomzoh WE ARE HIRING Drupal / Mobile App Engineer
 Digital Circus, Inc. Tokyo, Japan

Slide 69

Slide 69 text

5月 チケット販売開始 /トーク募集開始
 6月上旬 トーク募集〆切
 6月下旬 タイムテーブル決定 iOS Developers Conference Japan 2016 2016.08.20 https://iosdc.jp スポンサー企業さま、大絶賛募集中

Slide 70

Slide 70 text

2次会 ・勉強会終了後この近くのお店で23:30ぐらいまで。 ・この後発表する方で2次会行く方もいるはず。 ・話し足りないと思ったら是非。 ・後で再度アナウンスします。

Slide 71

Slide 71 text

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(); } }

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

More FPS? PHP5.5.30 OSX Native FPS: 4 PHP7.0.1 VirtualBox FPS: 14

Slide 74

Slide 74 text

SHARP LR35902 registers http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html ʜ ʜ " ' # $ ) - ʜ ʜ 41 4UBDL1PJOUFS 1$ 1SPHSBN$PVOUFS 8bit / 16bit registers 16bit registers Flag registor ; / ) $ •Z - Zero Flag •N - Subtract Flag •H - Half Carry Flag •C - Carry Flag •0 - Not uses, always zero “stack overflow”

Slide 75

Slide 75 text

Duration in cycles table

Slide 76

Slide 76 text

Duration in cycles table

Slide 77

Slide 77 text

Duration in cycles table

Slide 78

Slide 78 text

Duration in cycles table