Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SwiftでCPUを作る

freddi(Yuki Aki)
May 11, 2020
500

 SwiftでCPUを作る

freddi(Yuki Aki)

May 11, 2020
Tweet

Transcript

  1. nand2tetris • ޙ൒ʹͳΔʹͭΕͯ಺༰َ͕சʹͳΔ • ࢒Γͷήʔτ → CPU → ίϯϐϡʔλ →

    VM → ίϯύΠϥ … • ࠷ऴతʹ͸ςτϦε͕ग़དྷ্͕ΔΒ͍͠ • https://www.nand2tetris.org/
  2. SwiftͰnand2tetris • ͱΓ͋͑ͣҎԼͷറΓͰ΍ͬͯΈͨ • NANDήʔτɾDFFҎ֎Ͱ if ΍ switch ͱ͍ͬͨ৚݅จݪଇېࢭ •

    NANDͰϋʔυ΢ΣΞΛશ෦࣮૷͢ΔͨΊ • ͨͩ͠ɺग़ྗͷϏοτ਺Λଟ͘͢ΔͨΊͱ͔͸Մೳ • ϓϦϛςΟϒͳ Boolܕ Ͱ͸ͳ͘ enum ʹΑΔ஋Ͱૢ࡞ • ͜ͷ੍໿Ͱ1ΫϩοΫ2ඵͷΫιίϯϐϡʔλʹɾɾɾ
  3. 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 )
  4. 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
  5. SwiftͰnand2tetris (ΞηϯϒϦ) • ͜͜·Ͱ࡞Δͱ RAM: 4KB, CPU: 16bitͷίϯϐϡʔλ͕࡞Εͨ • ࢼͯ͠ΈͨΒ

    2ඵ / ΫϩοΫͩͬͨ • 0.5 Hz • ϫΠͷMacbook 2.4 GHz • 48ԯഒ஗͍
  6. Swiftͷenum͸BoolΑΓগʑ஗͍ • CPU͸ Bit ԋࢉΛ Bool Ͱ͸ͳ͘ enum ʹ͍ͨͤ͠Ͱ஗͍ •

    Bool͸Swiftͷ಺෦Ͱ͸ 1bit ͷ Integer (ͩͬͨ͸ͣ) • enum ͷ΄͏͕αΠζ΋ܭࢉίετ͕ඇৗʹେ͖͍ʁ • RAMΛstructʹͯ͠ίϯύΠϧ͢ΔͱίϯύΠϧ͕ऴΘΒͳ͍ • ࠓޙͷ՝୊
  7. Mac͸ϝϞϦ͕ࢮ͵ͱTrackPad͕๫૸͢Δ • ྫͷίϯύΠϥͷόά • ੜ͖ͯΔ∞ϓνϓνͷΑ͏ͳײ֮ • ͳΜ͔ɺੜ͖ͯΔΑ͏Ͱ͔Θ͍͍ͷͰͥͻ • ͭ͡͸Կճ͔Θ͟ͱ΍ָͬͯ͠Μͩ •

    ΍Γ͍ͨํ͸ԼهϦϯΫ͔Βɺͨͩ͠೤๫૸΋͋ΔͷͰࣗݾ੹೚Ͱ https://github.com/freddi-kit/Swift2Tetris/commit/ 84c42b8da965608a5c7152842e168f5219427732#r39052324