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
470
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
4
200
Deep Dive into "any" and "some"
freddi
3
1.3k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.4k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
100
How to develop SIL Optimizer in Swift Language
freddi
0
290
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
560
知ろう! ShazamKit
freddi
1
160
Swift Optimizing at Compiler World
freddi
1
740
Recap Pointfree Vol. 1~3
freddi
1
770
Featured
See All Featured
Practical Orchestrator
shlominoach
186
10k
Adopting Sorbet at Scale
ufuk
73
9.1k
Visualization
eitanlees
146
15k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Into the Great Unknown - MozCon
thekraken
33
1.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
The Pragmatic Product Professional
lauravandoore
32
6.3k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Writing Fast Ruby
sferik
628
61k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Become a Pro
speakerdeck
PRO
26
5k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
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Ͱ࣮͠͠·͢ •
ͦΕ͔ϦΞϧʹճ࿏ઃܭͯ͠Ҡ২͍͍͔ͯ͠