CPUとは何か / What's a CPU

CPUとは何か / What's a CPU

PHPカンファレンス北海道 2019のLT資料です。
動画:
https://youtu.be/XT2HZvOjZRs

参考:
CPUの創りかた
https://www.amazon.co.jp/dp/4839909865

40575ebf9c2f4a6e3bcb68630f446a3b?s=128

HASEGAWA Tomoki

September 21, 2019
Tweet

Transcript

  1. ௕୩઒ஐر $16ͱ͸Կ͔

  2. ௕୩઒ஐر !UPN[PI

  3. ϥΠϑϫʔΫ ςοΫΧϯϑΝϨϯεӡӦࢀՃ  8FCJ04ΞϓϦ։ൃ Ϗʔϧ  $16 ϨτϩήʔϜػ ిࢠ޻࡞ 


    αοΧʔ؍ઓ 
 ϨϯλϧΧʔτϨʔε ʜ σδλϧαʔΧεגࣜձࣾ ෭ஂ௕$50 !UPN[PI
  4. 8FC%FWFMPQNFOUXJUI 8&"3&)*3*/( IUUQXXXEHDJSDVTDPN ౦ژදࢀಓ

  5. ೥݄೔ʢ೔ʣʙ೔ʢՐ ɾॕʣ εϙϯαʔืूதʂʂ 
 ։࠵ܾఆ

  6. ࠓ೔ͷςʔϚ

  7. ௕୩઒ஐر $16ͱ͸Կ͔

  8. ௕୩઒ஐر $16ͱ͸Կ͔ ʘͭͷࢹ఺͔Βݟͨ$16ʗ

  9. ୊ࡐ$16

  10. ୊ࡐ$16 5%

  11. ॻ੶$16ͷ૑Γ͔ͨ

  12. ॻ੶$16ͷ૑Γ͔ͨ ʘ໊ஶʗ

  13. Ͱ͸ૣ଎ʜ

  14. ϓϩάϥϜ࣮ߦ؀ڥ ࢹ఺ ͱͯ͠ͷ$16

  15. Έͳ͞Μ͓ೃછΈ

  16. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠ͷ$16 wϚγϯޠϓϩάϥϜͷ࣮ߦ؀ڥ

  17. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠ͷ$16 wϚγϯޠϓϩάϥϜͷ࣮ߦ؀ڥ wVTSCJOQIQϚγϯޠϓϩάϥϜ

  18. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠ͷ$16 wϚγϯޠϓϩάϥϜͷ࣮ߦ؀ڥ wVTSCJOQIQϚγϯޠϓϩάϥϜ w$ݴޠ‎χʔϞχοΫ‎Ϛγϯޠ ίϯύΠϧ Ξηϯϒϧ

  19. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠$16 Λݟͨ࣌ͷߟ͑ํ

  20. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠$16 wϓϩάϥϜΛ࣮ߦͯ͘͠ΕΔଘࡏ Λݟͨ࣌ͷߟ͑ํ

  21. ϓϩάϥϜ࣮ߦ؀ڥͱͯ͠$16 wϓϩάϥϜΛ࣮ߦͯ͘͠ΕΔଘࡏ Λݟͨ࣌ͷߟ͑ํ wࣗ෼ͷ࢖͏ػೳͷΈͷཧղͰ0,
 ʢ͢΂ͯͷ໋ྩɾ࢓༷Λ஌Δඞཁ͸ແ͍ʣ

  22. $16ͱ͸Կ͔ ̘̥̪

  23. ΤϛϡϨʔτର৅ ࢹ఺ ͱͯ͠ͷ$16

  24. ΤϛϡϨʔτର৅ͱͯ͠ͷ$16 wΤϛϡϨʔλͷ$16෦෼

  25. ΤϛϡϨʔτର৅ͱͯ͠ͷ$16 w$16ͷ;Δ·͍Λιϑτ΢ΣΞͱͯ͠ ࣮૷ wΤϛϡϨʔλͷ$16෦෼

  26. 1)1Ͱ࣮૷ͯ͠ΈΔ

  27. 5%ͷϚγϯޠ Y

  28. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y
  29. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y "%%" *N
  30. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y "Ϩδελʹ*Nͷ஋Λ଍͢ "%%" *N
  31. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y ""  "Ϩδελʹ*Nͷ஋Λ଍͢ "%%" *N
  32. public function executeInstruction() {

  33. public function executeInstruction() { $byte = $this->ram[$this->pc];

  34. public function executeInstruction() { $byte = $this->ram[$this->pc]; 0QDPEF  

      *Nʢ0QFSBOE    
  35. $opcode = $byte >> 4; $im = $byte & 0b00001111;

    public function executeInstruction() { $byte = $this->ram[$this->pc]; 0QDPEF     *Nʢ0QFSBOE    
  36. $opcode = $byte >> 4; $im = $byte & 0b00001111;

    public function executeInstruction() { switch ($opcode){ case 0b0000: // ADD A, Im $this->a += $im; break; case 0b0001: } $byte = $this->ram[$this->pc];
  37. $opcode = $byte >> 4; $im = $byte & 0b00001111;

    public function executeInstruction() { switch ($opcode){ case 0b0000: // ADD A, Im $this->a += $im; break; case 0b0001: } $this->pc++; } $byte = $this->ram[$this->pc];
  38. ΤϛϡϨʔτର৅ͱͯ͠$16 Λݟͨ࣌ͷߟ͑ํ

  39. ΤϛϡϨʔτର৅ͱͯ͠$16 Λݟͨ࣌ͷߟ͑ํ wର৅$16༻ʹॻ͔Εͨ
 શͯͷϓϩάϥϜΛಈ࡞ͤ͞Δଘࡏ

  40. ΤϛϡϨʔτର৅ͱͯ͠$16 w$16࢓༷ͷ׬શͳཧղ Λݟͨ࣌ͷߟ͑ํ wର৅$16༻ʹॻ͔Εͨ
 શͯͷϓϩάϥϜΛಈ࡞ͤ͞Δଘࡏ

  41. $16ͱ͸Կ͔ ̘̥̪

  42. ిؾճ࿏ ࢹ఺ ͱͯ͠ͷ$16

  43. $16Λϋʔυ΢ΣΞͱ࣮ͯ͠૷
 ͢Δ৔߹ͷߟ͑ํ

  44. None
  45. 5%ͷ$16໋ྩ

  46. Կ͔ͱԿ͔Λ଍ͯ͠
 Ͳ͔͜ʹసૹ͢Δ 5%ͷ$16໋ྩ

  47. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ
  48. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ
  49. */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$ Ϩδελ

    Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  50. */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$ Ϩδελ

    Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  51. */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$ Ϩδελ

    Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  52. */ ϙʔτ " Ϩδελ # Ϩδελ 065 ϙʔτ 1$ Ϩδελ

    Ճࢉػ ϝϞϦ ηϨΫλ 0QDPEF     *Nʢ0QFSBOE     ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  53. */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ 0QDPEF     *Nʢ0QFSBOE     ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  54. */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  55. */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  56. */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ 1$

    Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔
  57. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ
  58. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ͦΕΛͲ͏ܾΊΔʁ
  59. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ͦΕΛͲ͏ܾΊΔʁ ͦΕ໋͕ͦ͜ྩͳͷͩʂ
  60. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ͦΕΛͲ͏ܾΊΔʁ ͦΕ໋͕ͦ͜ྩͳͷͩʂ 0QDPEF     *Nʢ0QFSBOE    
  61. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ 0QDPEF     *Nʢ0QFSBOE    
  62. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ ্ҐCJU */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065

    ϙʔτ 1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ 0QDPEF     *Nʢ0QFSBOE    
  63. ԿͱԿΛ଍ͯ͠Ͳ͜ʹసૹ͢Δ͔ ্ҐCJU */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065

    ϙʔτ 1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ 0QDPEF     *Nʢ0QFSBOE    
  64. ্ҐCJU */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ
  65. ্ҐCJU */ ϙʔτ ԼҐCJU " Ϩδελ # Ϩδελ 065 ϙʔτ

    1$ Ϩδελ Ճࢉػ ϝϞϦ ηϨΫλ ΫϩοΫ
  66. ిؾճ࿏ͱͯ͠$16 Λݟͨ࣌ͷߟ͑ํ

  67. ిؾճ࿏ͱͯ͠$16 wΫϩοΫʹैͬͯঢ়ଶભҠ͢Δճ࿏ͱͯ͠ $16໋ྩΛදݱͨ͠ଘࡏ Λݟͨ࣌ͷߟ͑ํ

  68. ిؾճ࿏ͱͯ͠$16 wΫϩοΫʹैͬͯঢ়ଶભҠ͢Δճ࿏ͱͯ͠ $16໋ྩΛදݱͨ͠ଘࡏ Λݟͨ࣌ͷߟ͑ํ w࢓༷ͱ෺ཧͷύζϧΛղ͍ͯ$16࢓༷Λ ࡞Δ

  69. $16ͱ͸Կ͔ ̘̥̪

  70. ·ͱΊ

  71. ·ͱΊ w$16ͱݴͬͯ΋ͲΜͳཱ৔ͰͦΕΛ
 ݟΔ͔ʹΑͬͯݟ͑ํ͕ҧ͏ʂ

  72. ·ͱΊ w$16ͱݴͬͯ΋ͲΜͳཱ৔ͰͦΕΛ
 ݟΔ͔ʹΑͬͯݟ͑ํ͕ҧ͏ʂ wΤϛϡϨʔλ͸೉͍͠΋ͷͰ͸ͳ͍Ͷʂ

  73. ·ͱΊ w$16ͱݴͬͯ΋ͲΜͳཱ৔ͰͦΕΛ
 ݟΔ͔ʹΑͬͯݟ͑ํ͕ҧ͏ʂ wΤϛϡϨʔλ͸೉͍͠΋ͷͰ͸ͳ͍Ͷʂ w$16ࣗ࡞΋໘ന͍Αʂ

  74. ·ͱΊ w$16ͱݴͬͯ΋ͲΜͳཱ৔ͰͦΕΛ
 ݟΔ͔ʹΑͬͯݟ͑ํ͕ҧ͏ʂ wΤϛϡϨʔλ͸೉͍͠΋ͷͰ͸ͳ͍Ͷʂ w$16ࣗ࡞΋໘ന͍Αʂ w΍ͬͯΈΑ͏ʂ

  75. None
  76. ͖͍࣋ͬͯͯΔͷͰ ࠙਌ձͱ͔Ͱ੠͔͚͍ͯͩ͘͞

  77. ͓ΘΓ !UPN[PI

  78. ༨ஊ

  79. 8IBUNBLFXJUI1)1

  80. None
  81. None
  82. None
  83. None
  84. None
  85. 5%ͷϓϩάϥϜ

  86. ϚγϯޠͰॻ͔ΕͨϓϩάϥϜ

  87. ϚγϯޠͰॻ͔ΕͨϓϩάϥϜ YG     

  88. Y  .07"  "ϨδελʹΛసૹ Y  "%%"  "ϨδελʹΛ଍͢

    Y  .07# " #Ϩδελʹ"Ϩδελͷ ஋Λసૹ Y  065# #Ϩδελͷ஋Λ*0
 ϙʔτʹग़ྗ Y G +.1Y Yʹδϟϯϓ ϚγϯޠͰॻ͔ΕͨϓϩάϥϜ
  89. $16ͷ૑Γ͔ͨ ʘ໊ஶʗ

  90. IUUQTXXXBNB[PODPKQEQ

  91. 5%ͷ໋ྩ͸શͯ
 ଍͠ࢉసૹ

  92. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y "Ϩδελʹ*Nͷ஋Λ଍͢ "%%" *N
  93. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y ""  "Ϩδελʹ*Nͷ஋Λ଍͢ "%%" *N
  94. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y "Ϩδελʹ#Ϩδελͷ஋Λ
 సૹ͢Δ .07" #
  95. 5%ͷϚγϯޠ 0QDPEF     *Nʢ0QFSBOE   

     Y "#  "Ϩδελʹ#Ϩδελͷ஋Λ
 సૹ͢Δ .07" #
  96. 5%ͷ໋ྩηοτ

  97. Ϛγϯޠ ໋ྩ ʢχʔϞχοΫʣ ಈ࡞ 0QDPEF ʢਐʣ 0QDPEF ʢਐʣ "%%" *N

    "Ϩδελʹ*NΛ଍͢ C Y .07" # #Ϩδελͷ஋Λ"Ϩδελʹసૹ͢Δ C Y */" *0ϙʔτ͔Β"Ϩδελʹ஋ΛಡΈࠐΉ C Y .07" *N "Ϩδελʹ*Nͷ஋Λసૹ͢Δ C Y +.1*N *Nʹδϟϯϓ͢Δ C YG
  98. Ϛγϯޠ ໋ྩ ʢχʔϞχοΫʣ ಈ࡞ 0QDPEF ʢਐʣ 0QDPEF ʢਐʣ "%%" *N

    "Ϩδελʹ*NΛ଍͢ C Y .07" # #Ϩδελͷ஋Λ"Ϩδελʹసૹ͢Δ C Y */" *0ϙʔτ͔Β"Ϩδελʹ஋ΛಡΈࠐΉ C Y .07" *N "Ϩδελʹ*Nͷ஋Λసૹ͢Δ C Y +.1*N *Nʹδϟϯϓ͢Δ C YG શ໋ྩ
  99. $16ͷΤϛϡϨʔλ࣮૷
 ʙॳظԽฤʙ

  100. class Cpu { public function __construct() { $this->ram = array_fill(0,

    0b1111, 0); $this->pc = $this->a = $this->b = 0; } private $ram; private $pc; private $a; private $b; $16ͷΫϥεఆٛͱॳظԽ
  101. ͓ΘΓ !UPN[PI