reading php terminal-gameboy-emulator

74
Reading php-terminal-gameboy-emulator Digital Circus, Inc. HASEGAWA Tomki

Upload: tomoki-hasegawa

Post on 14-Apr-2017

2.944 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Reading php terminal-gameboy-emulator

Reading php-terminal-gameboy-emulator

Digital Circus, Inc. HASEGAWA Tomki

Page 2: Reading php terminal-gameboy-emulator

HASEGAWA Tomoki

@tomzoh

Web / iOS App Development,

Rental Kart Race, Beer, IoT crafting, …

hasegawatomoki

Digital Circus, Inc. Vice-master CTO

Tokyo, Japan

Interests

Page 3: Reading php terminal-gameboy-emulator

・Web Development

・Mobile Apps Development

・ Service

( )

(iOS, Android)

(http://appbuilder.jp)

Twitter: @tomzoh

Page 4: Reading php terminal-gameboy-emulator

Twitter: @tomzoh

Page 5: Reading php terminal-gameboy-emulator

Today’s theme

Reading php-terminal-gameboy-emulator

Page 6: Reading php terminal-gameboy-emulator

Today’s theme

Reading php-terminal-gameboy-emulator

Page 7: Reading php terminal-gameboy-emulator

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

Page 8: Reading php terminal-gameboy-emulator

23, February

Page 9: Reading php terminal-gameboy-emulator
Page 10: Reading php terminal-gameboy-emulator
Page 11: Reading php terminal-gameboy-emulator
Page 12: Reading php terminal-gameboy-emulator

😮

Page 13: Reading php terminal-gameboy-emulator

😮

Page 14: Reading php terminal-gameboy-emulator

😮

Awesome!!

Page 15: Reading php terminal-gameboy-emulator

😮

Awesome!!

Waste of talent!!

Page 16: Reading php terminal-gameboy-emulator

😮

Awesome!!

Waste of talent!! Cool!!

Page 17: Reading php terminal-gameboy-emulator

Source Files

Page 18: Reading php terminal-gameboy-emulator

Source Files

It’s PHP !

Page 19: Reading php terminal-gameboy-emulator

Source Files

It’s PHP !

We can read it.

Page 20: Reading php terminal-gameboy-emulator

Source Files

It’s PHP !

Yes, We can!

We can read it.

Page 21: Reading php terminal-gameboy-emulator

Source Files

It’s PHP !

Yes, We can!

Yes, We can!

We can read it.

Page 22: Reading php terminal-gameboy-emulator

Today’s theme

Reading php-terminal-gameboy-emulator

Page 23: Reading php terminal-gameboy-emulator

Components of GAMEBOY

• CPU / Memory • Display • Sound • Buttons • Communication Port

Page 24: Reading php terminal-gameboy-emulator

Components of GAMEBOY

• CPU / Memory • Display • Sound • Buttons • Communication Port

Page 25: Reading php terminal-gameboy-emulator

Buttons

Page 26: Reading php terminal-gameboy-emulator
Page 27: Reading php terminal-gameboy-emulator
Page 28: Reading php terminal-gameboy-emulator

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() { $key = fread($this->file, 1);

Page 29: Reading php terminal-gameboy-emulator

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() { $key = fread($this->file, 1);

Page 30: Reading php terminal-gameboy-emulator

How to sense buttons

Page 31: Reading php terminal-gameboy-emulator

How to sense buttons

$this->file = fopen('php://stdin', 'r');

Page 32: Reading php terminal-gameboy-emulator

How to sense buttons

$this->file = fopen('php://stdin', 'r');

$key = fread($this->file, 1);

Page 33: Reading php terminal-gameboy-emulator

Simple

Page 34: Reading php terminal-gameboy-emulator

CPU / Memory

Page 35: Reading php terminal-gameboy-emulator

CPU

Page 36: Reading php terminal-gameboy-emulator

SHARP LR35902 instruction set

http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html

1byte instructions Prefix CB instructions

144 instructions 256 instructions+

= 400 instructions

Page 37: Reading php terminal-gameboy-emulator

SHARP LR35902 resistors

http://www.pastraiser.com/cpu/gameboy/gameboy_opcodes.html

15 … 8 7 … 0A FB CH L

15 … 8 7 … 0SP (Stack Pointer)

PC (Program Counter)

8bit / 16bit resistors

16bit resistors

Flag registor

7 6 5 4 3 2 1 0Z 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

Page 38: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

Page 39: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -Length in bytes

Address

LD A, d8 2 8

- - - -

Page 40: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -Duration in cyclesLength in bytes

Address

LD A, d8 2 8

- - - -

Page 41: Reading php terminal-gameboy-emulator

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 3xLD 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

- - - -

Page 42: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

Page 43: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

PC (Program Counter)

Page 44: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

PC (Program Counter)

Page 45: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

PC (Program Counter)

Page 46: Reading php terminal-gameboy-emulator

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 3xLD 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)

Page 47: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

PC (Program Counter)

Page 48: Reading php terminal-gameboy-emulator

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 3xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Address

LD A, d8 2 8

- - - -

PC (Program Counter)

Page 49: Reading php terminal-gameboy-emulator
Page 50: Reading php terminal-gameboy-emulator
Page 51: Reading php terminal-gameboy-emulator

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) {

Page 52: Reading php terminal-gameboy-emulator

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

0xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Page 53: Reading php terminal-gameboy-emulator

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

0xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Page 54: Reading php terminal-gameboy-emulator

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

0xLD BC, d16

3 12 - - - -

NOP 1 4

- - - -

LD (BC), A 1 8

- - - -

Implementation of 0x01 LD BC, d16

Page 55: Reading php terminal-gameboy-emulator

//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)

Page 56: Reading php terminal-gameboy-emulator

//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

Page 57: Reading php terminal-gameboy-emulator

//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

Page 58: Reading php terminal-gameboy-emulator

//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)

Page 59: Reading php terminal-gameboy-emulator

Implementation of JP a16

x3

cxJP a16 3 16 - - - -

Page 60: Reading php terminal-gameboy-emulator

Implementation of JP a16

x3

cxJP a16 3 16 - - - -

Jump to address a16

Page 61: Reading php terminal-gameboy-emulator

Implementation of JP a16

x3

cxJP a16 3 16 - - - -

Jump to address a16

0x0000 0x0001 0x0002

0xc3 0x29 0x03

JP a16 0x0329

Data

Instruction

Address

Page 62: Reading php terminal-gameboy-emulator

Implementation of JP a16

x3

cxJP a16 3 16 - - - -

Jump to address a16

0x0000 0x0001 0x0002

0xc3 0x29 0x03

JP a16 0x0329

Data

Instruction

Address

Jump to address 0x0329

Page 63: Reading php terminal-gameboy-emulator

Implementation of JP a16

x3

cxJP 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

Page 64: Reading php terminal-gameboy-emulator

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

Page 65: Reading php terminal-gameboy-emulator

Not difficult Simple, Easy

Page 66: Reading php terminal-gameboy-emulator

Let’s read php-terminal-gameboy-emulator

Page 67: Reading php terminal-gameboy-emulator

Thanks

@tomzoh

WE ARE HIRING Drupal / Mobile App EngineerDigital Circus, Inc. Tokyo, Japan@tomzoh

Page 68: Reading php terminal-gameboy-emulator

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

Page 69: Reading php terminal-gameboy-emulator

More FPS?

PHP5.5.30 OSX Native FPS: 4

PHP7.0.1 VirtualBox FPS: 14

Page 70: Reading php terminal-gameboy-emulator

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

Page 71: Reading php terminal-gameboy-emulator

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

Page 72: Reading php terminal-gameboy-emulator

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

cxCALL NZ,a16

3 24/12 - - - -

Page 73: Reading php terminal-gameboy-emulator

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

cxCALL NZ,a16

3 24/12 - - - -Call address a16 if A resistor

is zero. It takes 24 clocks.

Page 74: Reading php terminal-gameboy-emulator

5月 チケット販売開始 /トーク募集開始6月上旬 トーク募集〆切 6月下旬 タイムテーブル決定

iOS Developers Conference Japan 2016 2016.08.20 https://iosdc.jp

スポンサー企業さま、大絶賛募集中