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
460
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
190
Deep Dive into "any" and "some"
freddi
3
1.3k
挑戦!ISUCON de Server-side Swift 〜タイムゾーンには気をつけろ〜
freddi
0
1.3k
意外と知られてないXcode13の新しい参照カウンタ最適化オプションの挙動
freddi
2
97
How to develop SIL Optimizer in Swift Language
freddi
0
280
SwiftコンパイラがSwiftで開発できるようになる話
freddi
3
550
知ろう! ShazamKit
freddi
1
160
Swift Optimizing at Compiler World
freddi
1
730
Recap Pointfree Vol. 1~3
freddi
1
760
Featured
See All Featured
Fireside Chat
paigeccino
34
3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
A Philosophy of Restraint
colly
203
16k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Being A Developer After 40
akosma
86
590k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Designing for Performance
lara
604
68k
Ruby is Unlike a Banana
tanoku
97
11k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
[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Ͱ࣮͠͠·͢ •
ͦΕ͔ϦΞϧʹճ࿏ઃܭͯ͠Ҡ২͍͍͔ͯ͠