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
480
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
210
Deep Dive into "any" and "some"
freddi
3
1.3k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.4k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
110
How to develop SIL Optimizer in Swift Language
freddi
0
300
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
580
知ろう! ShazamKit
freddi
1
170
Swift Optimizing at Compiler World
freddi
2
770
Recap Pointfree Vol. 1~3
freddi
1
780
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Documentation Writing (for coders)
carmenintech
67
4.5k
For a Future-Friendly Web
brad_frost
176
9.5k
Rails Girls Zürich Keynote
gr2m
94
13k
How STYLIGHT went responsive
nonsquared
96
5.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Adopting Sorbet at Scale
ufuk
74
9.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
How to Ace a Technical Interview
jacobian
276
23k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
YesSQL, Process and Tooling at Scale
rocio
170
14k
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Ͱ࣮͠͠·͢ •
ͦΕ͔ϦΞϧʹճ࿏ઃܭͯ͠Ҡ২͍͍͔ͯ͠