Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

23
Implementing MicroThread / Coroutine via espswap for Danmaku Shooting Games Yoh Okuno / @nokuno #x86opti

description

 

Transcript of Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Page 1: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Implementing  MicroThread  /  Coroutine  via  esp-­‐swap  for  Danmaku  Shooting  Games

Yoh  Okuno  /  @nokuno  

#x86opti  

Page 2: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

About  Presenter •  Name:  Yoh  Okuno  /  @nokuno  

•  R&D  Engineer  at  Yahoo!  Japan  

•  Interest:  NLP  (Natural  Language  Processing),  

Machine  Learning,  Data  Mining  and  TOUHOU  

•  Skills:  C/C++,  Java,  Python,  and  Hadoop.  

•  Website:  http://yoh.okuno.name/  

Page 3: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Overview 1.  Introduction  to  Danmaku  Shooting  Games  

–  Let’s  play!  

2.  Using  MicroThread  /  Coroutine  

–  Why  it  is  useful  for  Danmaku  Shooting  Games  

3.  Implementation  of  MicroThread  /  Coroutine  

–  How  to  implement  it  with  x86  inline  assembly  

Page 4: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

1.  Introduction  to  Danmaku  Shooting  Games  

Page 5: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

TOBIMARISA:  Dojin  Danmaku  Shooting •  Full-­‐scratch  C++  program  with  20,000  lines  

Page 6: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

DEMO Video:  http://www.youtube.com/watch?v=UBCnQJbrZxU  

Source:  https://github.com/nokuno/tbm  

Page 7: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games
Page 8: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Structure  of  Danmaku  Shooting  Games •  Nested  sequences  of  actions  

Game  Start

Game  End

Retry

Title

Stage  1

Stage  2

Stage  3

Ending

Enemies

Sub  Boss

Enemies

Main  Boss

Normal

Spell  1

Normal

Spell  2

Last  Spell

Wait

Circle  Shot

Move

Wait

Special  Shot

Loop  Until  Beat  or  Tim

e  up

Page 9: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

2.  Using  MicroThread  /  Coroutine  for  Danmaku  Shooting  Games  

Page 10: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

How  to  Implement  Danmaku  Shooting  Games? •  Toy  example:  Enemy  class  and  main  loop  

Main  Loop  (

!"FPS)

Page 11: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

How  to  Implement  Danmaku  Shooting  Games?

•  How  about  nesting?  

•  Manually  code?  

•  No!  

– Too  many  states  

– Too  many  counts  

– Undebuggable

Page 12: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Handle  nested  sequences  via  MicroThread            

Natural  D

escription    of  Sequence

Page 13: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

3.  Implementation  of    MicroThread  /  Coroutine  

Page 14: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Review  of  Memory  Space  in  Program

•  There  are  3  types  of  fields  in  memory  space  

– Program:  read-­‐only  field,  store  instructions  

– Heap:  dynamic  field  used  by  malloc/new  

– Stack:  read/write  field  to  store  local  variables  

•  In  x86,  esp  register  stores  the  stack  pointer  

Stack

esp eip

0x100000c63 0x100100080 0x7fff5lfe808

Program Heap

Page 15: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

MicroThread  with  x86  inline  assembly

•  Trick:  allocate  own  space  and  swap  esp  with  it  

•  The  idea:  

– Allocate  own  memory  for  alternative  stack  

– Swap  esp  (stack  pointer)  with  it  (m_esp)  

– Call  functional  pointer  when  it  starts  

Program Stack

m_esp

0x100000c63 0x100100080 0x7fff5lfe808

MyStack Heap

esp eip

Page 16: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Components  of  MicroThread

•  There  are  2  files:  

– microthread.h  

– microthread.cc  

•  There  are  2  main  functions:  

– MicroThread::SwitchThread()  

– MicroThread::Start()

Page 17: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Main  Variables

Main  Functions

Header:  microthread.h

Page 18: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

MicroThread::SwitchThread() Source:  microthread.cc

Reserve  registers

Restore  registers

Swap  esp  (stack  pointer)

Page 19: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

MicroThread::Start()

•  Initialize  

– Allocate  a  buffer  for  our  own  stack  (default:  32KB)  

– Set  stack  pointer  to  the  end  of  buffer  

Source:  microthread.cc

Page 20: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Reserve  registers

Restore  registers

Swap  esp  and  call  CallBack

Restore  esp  and  set  flag

Source:  microthread.cc

Page 21: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Other  Functions

//  use  in  enemies

//  use  in  main  loop

Page 22: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Conclusion •  Developed  Danmaku  Shooting  Game  

– Structure:  Nested  Sequences  of  Actions  

•  Why  MicroThread  /  Coroutine  can  contribute  

– Make  it  simple,  natural  way  

•  How  to  Implement  MicroThread  /  Coroutine  

– Swap  esp  (stack  pointer)  with  own  space

Page 23: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games

Any  Questions? https://github.com/nokuno/nokuno/tree/master/cc/x86