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

17,737 views

Published on

Published in: Education, Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
17,737
On SlideShare
0
From Embeds
0
Number of Embeds
14,350
Actions
Shares
0
Downloads
32
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

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

  1. 1. Implementing  MicroThread  /   Coroutine  via  esp-­‐swap  for   Danmaku  Shooting  Games Yoh  Okuno  /  @nokuno   #x86opti  
  2. 2. 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/  
  3. 3. Overview1.  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  
  4. 4. 1.  Introduction  to  Danmaku   Shooting  Games  
  5. 5. TOBIMARISA:  Dojin  Danmaku  Shooting•  Full-­‐scratch  C++  program  with  20,000  lines  
  6. 6. DEMOVideo:  http://www.youtube.com/watch?v=UBCnQJbrZxU   Source:  https://github.com/nokuno/tbm  
  7. 7. Structure  of  Danmaku  Shooting  Games•  Nested  sequences  of  actions   Game  Start Title Normal Wait Enemies Loop  Until  Beat  or  Time  up Stage  1 Sub  Boss Spell  1 Circle  Shot Stage  2 Enemies Normal Move Stage  3 Spell  2 WaitRetry Ending Main  Boss Special  Shot Last  Spell Game  End
  8. 8. 2.  Using  MicroThread  /  Coroutine   for  Danmaku  Shooting  Games  
  9. 9. How  to  Implement  Danmaku  Shooting  Games?•  Toy  example:  Enemy  class  and  main  loop   Main  Loop  ( FPS) !"
  10. 10. How  to  Implement  Danmaku  Shooting  Games?•  How  about  nesting?  •  Manually  code?  •  No!   –  Too  many  states   –  Too  many  counts   –  Undebuggable
  11. 11. Handle  nested  sequences  via  MicroThread       Natural  Description    of  Sequence      
  12. 12. 3.  Implementation  of    MicroThread  /  Coroutine  
  13. 13. 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   eip espProgram Heap Stack0x100000c63 0x100100080 0x7fff5lfe808
  14. 14. 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   eip esp m_espProgram Heap MyStack Stack0x100000c63 0x100100080 0x7fff5lfe808
  15. 15. Components  of  MicroThread•  There  are  2  files:   –  microthread.h   –  microthread.cc  •  There  are  2  main  functions:   –  MicroThread::SwitchThread()   –  MicroThread::Start()
  16. 16. Header:  microthread.h Main  Functions Main  Variables
  17. 17. MicroThread::SwitchThread() Source:  microthread.cc Reserve  registers Swap  esp  (stack  pointer) Restore  registers
  18. 18. MicroThread::Start() Source:  microthread.cc•  Initialize   –  Allocate  a  buffer  for  our  own  stack  (default:  32KB)   –  Set  stack  pointer  to  the  end  of  buffer  
  19. 19. Source:  microthread.ccReserve  registersSwap  esp  and  call  CallBackRestore  esp  and  set  flagRestore  registers
  20. 20. Other  Functions//  use  in  enemies//  use  in  main  loop
  21. 21. 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
  22. 22. Any  Questions?https://github.com/nokuno/nokuno/tree/master/cc/x86

×