Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SwiftでCPUを作る
Search
freddi(Yuki Aki)
May 11, 2020
4
400
SwiftでCPUを作る
freddi(Yuki Aki)
May 11, 2020
Tweet
Share
More Decks by freddi(Yuki Aki)
See All by freddi(Yuki Aki)
輝け俺のViewController 〜海外iOSカンファレンス登壇編〜
freddi
3
160
Deep Dive into "any" and "some"
freddi
3
1.2k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.3k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
93
How to develop SIL Optimizer in Swift Language
freddi
0
250
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
530
知ろう! ShazamKit
freddi
1
150
Swift Optimizing at Compiler World
freddi
1
710
Recap Pointfree Vol. 1~3
freddi
1
730
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.6k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
225
22k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Designing the Hi-DPI Web
ddemaree
278
34k
The Power of CSS Pseudo Elements
geoffreycrofte
71
5.2k
Optimising Largest Contentful Paint
csswizardry
30
2.8k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
123
18k
In The Pink: A Labor of Love
frogandcode
139
22k
How GitHub Uses GitHub to Build GitHub
holman
472
290k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.6k
What's new in Ruby 2.0
geeforr
340
31k
The Illustrated Children's Guide to Kubernetes
chrisshort
47
48k
Transcript
GWͷࣗ༝ݚڀൃදձ - 11st/May/2020 Swift ͰCPUΛ࡞Δ freddi - LINE Fukuoka
୭͓·͑ • LINE FukuokaͷiOSΤϯδχΞ • ৽ଔ2 / ࣾྺ3 • YouTubeͬͯΔͷͰݟͯ
• ΞϝϦΧొஃཱྀߦVlog
GWʹͬͨ͜ͱ
ࣗ࡞ΩʔϘʔυͷFW࿔ͬͯύϦϐʹͳΔ
ࣗ࡞ΩʔϘʔυͷFW࿔ͬͯύϦϐʹͳΔ
ຊʹҠΔ
GWʹͬͨ͜ͱ
SwiftͰCPUΛ࡞Δ
nand2tetris • 2ͭͷϓϦϛςΟϒͳήʔτͰճ࿏Λ͔ͭͬͯίϯϐϡʔλͱ͔࡞Δ • ͜͜Ͱ NANDͱDFF ͕ϓϦϛςΟϒͳήʔτ • ී௨HDLͱ͔Ͱ࡞͍ͬͯ͘ •
͜͜Ͱ࡞ΔίϯϐϡʔλHack Computerͱ໊͚ΒΕͯΔ
nand2tetris • ޙʹͳΔʹͭΕͯ༰َ͕சʹͳΔ • Γͷήʔτ → CPU → ίϯϐϡʔλ →
VM → ίϯύΠϥ … • ࠷ऴతʹςτϦε͕ग़དྷ্͕ΔΒ͍͠ • https://www.nand2tetris.org/
͜ͷຊΛࢀߟʹͨ͠
https://github.com/freddi-kit/Swift2Tetris ※ ҰԠίϯϐϡʔλ·ͰͰ͖͚ͨͲςετॻ͍ͯͳ͍ ※ ຊͰ͍͏ͱ̑ষ·Ͱ
SwiftͰnand2tetris • SwiftͰCPU·Ͱͭͬͨ͘ਓ͍ؒͳ͍Β͍͠ • ϫΠௐ 20205݄ݱࡏ • Rust1ਓ͍ͨ
SwiftͰnand2tetris • ͱΓ͋͑ͣҎԼͷറΓͰͬͯΈͨ • NANDήʔτɾDFFҎ֎Ͱ if switch ͱ͍ͬͨ݅จݪଇېࢭ •
NANDͰϋʔυΣΞΛશ෦࣮͢ΔͨΊ • ͨͩ͠ɺग़ྗͷϏοτΛଟ͘͢ΔͨΊͱ͔Մೳ • ϓϦϛςΟϒͳ Boolܕ Ͱͳ͘ enum ʹΑΔͰૢ࡞ • ͜ͷ੍Ͱ1ΫϩοΫ2ඵͷΫιίϯϐϡʔλʹɾɾɾ
SwiftͰnand2tetris (֤ήʔτ) • OR AND XOR NOT ͔ΜͨΜͩͬͨ • NOT
x = x NAND x • x AND y = NOT ( x AND y ) • x OR y = ( NOT x ) NAND ( NOT y )
SwiftͰnand2tetris (֤ήʔτ)
SwiftͰnand2tetris ((σ)ϚϧνϓϨΫα)
SwiftͰnand2tetris ((σ)ϚϧνϓϨΫα) • ݅ذͰඞཁʹͳΔͭ • ਅِදॻ͍ͯΰϦԡ͠Ͱ࣮
SwiftͰnand2tetris ((σ)ϚϧνϓϨΫα)
SwiftͰnand2tetris (ALU: ԋࢉஔ) • CPUͷதͰܭࢉͯ͘͠ΕΔύʔπ • ϚϧνϓϨΫα͋ͨΓͪΌΜͱ࣮ͯ͠ΔͱצͰ࡞ΕΔ
SwiftͰnand2tetris (ALU: ԋࢉஔ)
SwiftͰnand2tetris (ϨδελɾRAM(ϝϞϦ)) • খ͍͞Ϩδελ͔ΒͲΜͲΜαΠζΛ͛ͯ࡞͍ͬͯ͘ • ϚϧνϓϨΫα͋ͨΓͪΌΜͱ࣮ͯ͠ΔͱצͰ࡞ΕΔ • ಛʹɺΞυϨεࢦఆಡΈࠐΈͷ࣮ษڧʹͳͬͨ • ಛఆͷॻ͖ํΛ͢ΔͱίϯύΠϧ͕࣌ؒϚδͰَச
SwiftͰnand2tetris (ϨδελɾRAM(ϝϞϦ))
SwiftͰnand2tetris (CPU/Program Counter) • ࣍ͷ໋ྩΛड͚͚Δ 16bitͷCPUΛ࡞Δ • 1 1 1
a c c c c c c d d d j j j (֤ه߸ʹ 0 or 1) • c ALUʹ͛Δԋࢉॲཧ • d d d Ϩδελ/ RAM ͷσʔλ֨ೲઌ • j j j jmp ໋ྩ • ϨδελʹσʔλΛೖΕΔ͚ͩͳΒ 0 x x x x x x x x x x x x x x
SwiftͰnand2tetris (CPU/Program Counter)
SwiftͰnand2tetris (ΞηϯϒϦ) • ΞηϯϒϦݴޠΛͭͬͯ͘SwiftͰػցޠͷ༁ػΛॻ͍ͨ • ࠓ·Ͱͷ࡞ۀͰҰ൪ָͩͬͨ • ϓϩάϥϜRAMম͖ثͭͬͨ͘
Ͱ͖ͨ
SwiftͰnand2tetris (ΞηϯϒϦ) • ͜͜·Ͱ࡞Δͱ RAM: 4KB, CPU: 16bitͷίϯϐϡʔλ͕࡞Εͨ • ࢼͯ͠ΈͨΒ
2ඵ / ΫϩοΫͩͬͨ • 0.5 Hz • ϫΠͷMacbook 2.4 GHz • 48ԯഒ͍
ͭΒ͔ͬͨ͜ͱ
ςετ͕ਏ͍ʢಛʹRAMͱCPUʣ • όά͕ଟ͘ൃੜͨ͠ͷͰॻ͖͡Ίͨ • ྲྀੴʹ512BͷRAM͔Βॻ͍ͯͳ͍ • 512ͩͱ 2^8bit = 256
ݸςετέʔε͋Δ • 32k ͩͱ 2^15bit = 32768 ݸςετέʔε
ۮવSwiftίϯύΠϥͷόά౿ΈزPC࠶ىಈ • ܕνΣοΫ͕ࣦഊ͢Δͱ͖ʹίϯύΠϥ͕͢ΔϒϥΫϥόά • Swift 5.3 dev branchͰվળࡁΈ • ϝϞϦ͕ΒΕͨΒɺτϥοΫύου͕͢Δͱ͍͏ݟΛಘͨ
• ͢ΔͱͲ͜ͷϓϩάϥϜϛεͬͯΔͷ͔ڭ͑ͯ͘Εͳ͍ • ͳͷͰίϯύΠϥΤϥʔ͔Θ͔Βͣʹ̍ফඅ
ۮવSwiftίϯύΠϥͷόά౿ΈزPC࠶ىಈ ֘ͷίϛοτ https://github.com/freddi-kit/Swift2Tetris/commit/ 84c42b8da965608a5c7152842e168f5219427732#r39052324 Xcode 11.4.1Ͱ֬ೝࡁΈ
ಘͨͷɾֶΜͩ͜ͱ
͕ΜΕnand͚ͩͰίϯϐϡʔλ࡞ΕΔ • CPUͷςετશύλʔϯಈ͍ͨͷͰCPUͱݴ͑ΔͷͰ͖ͨ
ΞυϨεͰRAMͷॴΛࢀর͢Δํ๏ • ͜͜·Ͱͷͳ͔Ͱׂͱ໘ന͍෦ͩͬͨ ϝϞϦ ΞυϨε ΄͍͠
SwiftͷenumBoolΑΓগʑ͍ • CPU Bit ԋࢉΛ Bool Ͱͳ͘ enum ʹ͍ͨͤ͠Ͱ͍ •
BoolSwiftͷ෦Ͱ 1bit ͷ Integer (ͩͬͨͣ) • enum ͷ΄͏͕αΠζܭࢉίετ͕ඇৗʹେ͖͍ʁ • RAMΛstructʹͯ͠ίϯύΠϧ͢ΔͱίϯύΠϧ͕ऴΘΒͳ͍ • ࠓޙͷ՝
MacϝϞϦ͕ࢮ͵ͱTrackPad͕͢Δ • ྫͷίϯύΠϥͷόά • ੜ͖ͯΔ∞ϓνϓνͷΑ͏ͳײ֮ • ͳΜ͔ɺੜ͖ͯΔΑ͏Ͱ͔Θ͍͍ͷͰͥͻ • ͭ͡Կճ͔Θ͟ͱָͬͯ͠Μͩ •
Γ͍ͨํԼهϦϯΫ͔Βɺͨͩ͋͠ΔͷͰࣗݾͰ https://github.com/freddi-kit/Swift2Tetris/commit/ 84c42b8da965608a5c7152842e168f5219427732#r39052324
࠷ޙʹ
࠷ޙʹ • nand2tetris SwiftͰͰ͖Δʂ • Mac͕ϝϞϦ͕ࢮ͵ͱTrackPad͕͔Θ͍͍ • ྲྀੴʹ0.5zhͻͲ͍ͷͱVM·ͰΓ͍ͨHDLͰ࣮͠͠·͢ •
ͦΕ͔ϦΞϧʹճ࿏ઃܭͯ͠Ҡ২͍͍͔ͯ͠