implementing microthread / coroutine via esp-swap for danmaku shooting games
DESCRIPTION
TRANSCRIPT
![Page 1: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/1.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/2.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/3.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/4.jpg)
1. Introduction to Danmaku Shooting Games
![Page 5: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/5.jpg)
TOBIMARISA: Dojin Danmaku Shooting • Full-‐scratch C++ program with 20,000 lines
![Page 6: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/6.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/7.jpg)
![Page 8: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/8.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/9.jpg)
2. Using MicroThread / Coroutine for Danmaku Shooting Games
![Page 10: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/10.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/11.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/12.jpg)
Handle nested sequences via MicroThread
Natural D
escription of Sequence
![Page 13: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/13.jpg)
3. Implementation of MicroThread / Coroutine
![Page 14: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/14.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/15.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/16.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/17.jpg)
Main Variables
Main Functions
Header: microthread.h
![Page 18: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/18.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/19.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/20.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/21.jpg)
Other Functions
// use in enemies
// use in main loop
![Page 22: Implementing MicroThread / Coroutine via esp-swap for Danmaku Shooting Games](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/22.jpg)
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](https://reader037.vdocuments.net/reader037/viewer/2022110115/54840ca4b47959d80c8b4a5d/html5/thumbnails/23.jpg)
Any Questions? https://github.com/nokuno/nokuno/tree/master/cc/x86