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
500
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
240
Deep Dive into "any" and "some"
freddi
3
1.4k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.5k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
130
How to develop SIL Optimizer in Swift Language
freddi
0
320
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
600
知ろう! ShazamKit
freddi
1
180
Swift Optimizing at Compiler World
freddi
2
800
Recap Pointfree Vol. 1~3
freddi
1
790
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
69
4.7k
The Pragmatic Product Professional
lauravandoore
33
6.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
28
1.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
How to Think Like a Performance Engineer
csswizardry
22
1.5k
Mobile First: as difficult as doing things right
swwweet
223
9.5k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How STYLIGHT went responsive
nonsquared
99
5.4k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
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Ͱ࣮͠͠·͢ •
ͦΕ͔ϦΞϧʹճ࿏ઃܭͯ͠Ҡ২͍͍͔ͯ͠