Guest User

3302_vid_transcript_hmwk3

a guest
Feb 17th, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 51.25 KB | None | 0 0
  1. Hi, I’m Carrie Ann and this is Crash Course Computer Science.
  2. 00:05
  3. So last episode, we talked about how numbers can be represented in binary. Representing
  4. 00:09
  5. Like, 00101010 is 42 in decimal.
  6. 00:13
  7. Representing and storing numbers is an important function of a computer, but the real goal is computation,
  8. 00:19
  9. or manipulating numbers in a structured and purposeful way, like adding two numbers together.
  10. 00:23
  11. These operations are handled by a computer’s Arithmetic and Logic Unit,
  12. 00:27
  13. but most people call it by its street name: the ALU.
  14. 00:29
  15. The ALU is the mathematical brain of a computer.
  16. 00:32
  17. When you understand an ALU’s design and function, you’ll understand a fundamental
  18. 00:36
  19. part of modern computers. It is THE thing that does all of the computation in a computer,
  20. 00:41
  21. so basically everything uses it.
  22. 00:43
  23. First though, look at this beauty.
  24. 00:45
  25. This is perhaps the most famous ALU ever, the Intel 74181.
  26. 00:50
  27. When it was released in 1970, it was
  28. 00:52
  29. It was the first complete ALU that fit entirely inside of a single chip -
  30. 00:57
  31. Which was a huge engineering feat at the time.
  32. 00:59
  33. So today we’re going to take those Boolean logic gates we learned about last week
  34. 01:03
  35. to build a simple ALU circuit with much of the same functionality as the 74181.
  36. 01:08
  37. And over the next few episodes we’ll use
  38. 01:10
  39. this to construct a computer from scratch. So it’s going to get a little bit complicated,
  40. 01:14
  41. but I think you guys can handle it.
  42. 01:16
  43. INTRO
  44. 01:25
  45. An ALU is really two units in one -- there’s an arithmetic unit and a logic unit.
  46. 01:30
  47. Let's start with the arithmetic unit, which is responsible for handling all numerical operations in a
  48. 01:34
  49. computer, like addition and subtraction. It also does a bunch of other simple things like
  50. 01:39
  51. add one to a number, which is called an increment operation, but we’ll talk about those later.
  52. 01:43
  53. Today, we’re going to focus on the pièce de résistance, the crème de la crème of
  54. 01:47
  55. operations that underlies almost everything else a computer does - adding two numbers together.
  56. 01:52
  57. We could build this circuit entirely out of
  58. 01:54
  59. individual transistors, but that would get confusing really fast.
  60. 01:57
  61. So instead as we talked about in Episode 3 – we can use a high-level of abstraction and build our components
  62. 02:03
  63. out of logic gates, in this case: AND, OR, NOT and XOR gates.
  64. 02:08
  65. The simplest adding circuit that we can build takes two binary digits, and adds them together.
  66. 02:12
  67. So we have two inputs, A and B, and one output, which is the sum of those two digits.
  68. 02:18
  69. Just to clarify: A, B and the output are all single bits.
  70. 02:21
  71. There are only four possible input combinations.
  72. 02:24
  73. The first three are: 0+0 = 0
  74. 02:27
  75. 1+0 = 1 0+1 = 1
  76. 02:30
  77. Remember that in binary, 1 is the same as true, and 0 is the same as false.
  78. 02:35
  79. So this set of inputs exactly matches the boolean logic of an XOR gate, and we can use it as
  80. 02:39
  81. our 1-bit adder.
  82. 02:40
  83. But the fourth input combination, 1 + 1, is a special case. 1 + 1 is 2 (obviously)
  84. 02:46
  85. but there’s no 2 digit in binary, so as we talked about last episode, the result is
  86. 02:50
  87. 0 and the 1 is carried to the next column. So the sum is really 10 in binary.
  88. 02:54
  89. Now, the output of our XOR gate is partially correct - 1 plus 1, outputs 0.
  90. 03:00
  91. But, we need an extra output wire for that carry bit.
  92. 03:03
  93. The carry bit is only “true” when the inputs are 1 AND 1, because that's the only
  94. 03:06
  95. time when the result (two) is bigger than 1 bit can store… and conveniently we have
  96. 03:10
  97. a gate for that! An AND gate, which is only true when both inputs are true, so
  98. 03:15
  99. we’ll add that to our circuit too.
  100. 03:17
  101. And that's it. This circuit is called a half adder. It’s
  102. 03:19
  103. It's not that complicated - just two logic gates - but let’s abstract away even this level
  104. 03:24
  105. of detail and encapsulate our newly minted half adder as its own component, with two
  106. 03:28
  107. inputs - bits A and B - and two outputs, the sum and the carry bits.
  108. 03:32
  109. This takes us to another level of abstraction… heh… I feel like I say that a lot.
  110. 03:36
  111. I wonder if this is going to become a thing.
  112. 03:43
  113. Anyway, If you want to add more than 1 + 1
  114. 03:46
  115. we’re going to need a “Full Adder.” That half-adder left us with a carry bit as output.
  116. 03:50
  117. That means that when we move on to the next column in a multi-column addition,
  118. 03:54
  119. and every column after that, we are going to have to add three bits together, no two.
  120. 03:59
  121. A full adder is a bit more complicated - it
  122. 04:00
  123. takes three bits as inputs: A, B and C. So the maximum possible input is 1 + 1 + 1,
  124. 04:07
  125. which equals 1 carry out 1, so we still only need two output wires: sum and carry.
  126. 04:12
  127. We can build a full adder using half adders. To do this, we use a half adder to add A plus B
  128. 04:17
  129. just like before – but then feed that result and input C into a second half adder.
  130. 04:22
  131. Lastly, we need a OR gate to check if either one of the carry bits was true.
  132. 04:27
  133. That’s it, we just made a full adder! Again,we can go up a level of abstraction and wrap
  134. 04:31
  135. up this full adder as its own component. It takes three inputs, adds them, and outputs
  136. 04:36
  137. the sum and the carry, if there is one.
  138. 04:38
  139. Armed with our new components, we can now build a circuit that takes two, 8-bit numbers
  140. 04:42
  141. – Let’s call them A and B – and adds them together.
  142. 04:44
  143. Let’s start with the very first bit of
  144. 04:46
  145. A and B, which we’ll call A0 and B0. At this point, there is no carry bit to deal
  146. 04:51
  147. with, because this is our first addition. So we can use our half adder to add these
  148. 04:55
  149. two bits together. The output is sum0. Now we want to add A1 and B1 together.
  150. 05:01
  151. It's possible there was a carry from the previous addition of A0 and B0, so this time we need
  152. 05:06
  153. to use a full adder that also inputs the carry bit. We output this result as sum1.
  154. 05:11
  155. Then, we take any carry from this full adder, and run it into the next full adder that handles
  156. 05:16
  157. A2 and B2. And we just keep doing this in a big chain until all 8 bits have been added.
  158. 05:21
  159. Notice how the carry bits ripple forward to each subsequent adder. For this reason,
  160. 05:26
  161. this is called an 8-bit ripple carry adder. Notice how our last full adder has a carry out.
  162. 05:32
  163. If there is a carry into the 9th bit, it means the sum of the two numbers is too large to fit into 8-bits.
  164. 05:36
  165. This is called an overflow.
  166. 05:37
  167. In general, an overflow occurs when the result of an addition is too large to be represented by the number of bits you are using.
  168. 05:43
  169. This can usually cause errors and unexpected behavior.
  170. 05:45
  171. Famously, the original PacMan arcade game used 8 bits to keep track of what level you were on.
  172. 05:50
  173. This meant that if you made it past level 255 – the largest number storablein 8 bits –
  174. 05:55
  175. to level 256, the ALU overflowed.
  176. 05:58
  177. This caused a bunch of errors and glitches making the level unbeatable.
  178. 06:01
  179. The bug became a rite of passage for the greatest PacMan players.
  180. 06:04
  181. So if we want to avoid overflows, we can extend our circuit with more full adders, allowing
  182. 06:09
  183. us to add 16 or 32 bit numbers. This makes overflows less likely to happen, but at the
  184. 06:14
  185. expense of more gates. An additional downside is that it takes a little bit of time for
  186. 06:19
  187. each of the carries to ripple forward.
  188. 06:21
  189. Admittedly, not very much time, electrons move pretty fast, so we’re talking about billionths of a second,
  190. 06:26
  191. but that’s enough to make a difference in today’s fast computers.
  192. 06:29
  193. For this reason, modern computers use a slightly different adding circuit called a ‘carry-look-ahead’ adder
  194. 06:35
  195. which is faster, but ultimately does exactly the same thing-- adds binary numbers.
  196. 06:39
  197. The ALU’s arithmetic unit also has circuits for other math operations
  198. 06:43
  199. and in general these 8 operations are always supported.
  200. 06:46
  201. And like our adder, these other operations are built from individual logic gates.
  202. 06:50
  203. Interestingly, you may have noticed that there are no multiply and divide operations.
  204. 06:54
  205. That's because simple ALUs don’t have a circuit for this, and instead just perform a series of additions.
  206. 06:59
  207. Let’s say you want to multiply 12 by 5.
  208. 07:01
  209. That’s the same thing as adding 12 to itself 5 times. So it would take 5 passes through
  210. 07:06
  211. the ALU to do this one multiplication. And this is how many simple processors,
  212. 07:10
  213. like those in your thermostat, TV remote, and microwave, do multiplication.
  214. 07:15
  215. It’s slow, but it gets the job done.
  216. 07:17
  217. However, fancier processors, like those in your laptop or smartphone,
  218. 07:20
  219. have arithmetic units with dedicated circuits for multiplication.
  220. 07:23
  221. And as you might expect, the circuit is more complicated than addition -- there’s no
  222. 07:27
  223. magic, it just takes a lot more logic gates – which is why less expensive processors
  224. 07:31
  225. don’t have this feature.
  226. 07:32
  227. Ok, let’s move on to the other half of the ALU: the Logic Unit.
  228. 07:36
  229. Instead of arithmetic operations, the Logic Unit performs… well...
  230. 07:39
  231. logical operations, like AND, OR and NOT, which we’ve talked about previously.
  232. 07:44
  233. It also performs simple numerical tests, like checking if a number is negative.
  234. 07:47
  235. For example, here’s a circuit that tests if the output of the ALU is zero.
  236. 07:51
  237. It does this using a bunch of OR gates to see if any of the bits are 1.
  238. 07:55
  239. Even if one single bit is 1, we know the number can’t be zero and then we use a final NOT gate to flip this
  240. 08:01
  241. input so the output is 1 only if the input number is 0.
  242. 08:05
  243. So that’s a high level overview of what makes up an ALU. We even built several of
  244. 08:09
  245. the main components from scratch, like our ripple adder.
  246. 08:11
  247. As you saw, it’s just a big bunch of logic gates connected in clever ways.
  248. 08:14
  249. Which brings us back to that ALU you admired so much at the beginning of the episode.
  250. 08:18
  251. The Intel 74181.
  252. 08:21
  253. Unlike the 8-bit ALU we made today, the 74181 could only handle 4-bit inputs,
  254. 08:26
  255. which means YOU BUILT AN ALU THAT’S LIKE
  256. 08:29
  257. TWICE AS GOOD AS THAT SUPER FAMOUS ONE. WITH YOUR MIND! Well.. sort of.
  258. 08:34
  259. We didn’t build the whole thing… but you get the idea.
  260. 08:36
  261. The 74181 used about 70 logic gates, and it couldn’t multiply or divide.
  262. 08:41
  263. But it was a huge step forward in miniaturization, opening the doors to more capable and less expensive computers.
  264. 08:47
  265. This 4-bit ALU circuit is already a lot to take in,
  266. 08:50
  267. but our 8-bit ALU would require hundreds of logic gates to fully build and engineers
  268. 08:54
  269. don’t want to see all that complexity when using an ALU, so they came up with a special
  270. 08:59
  271. symbol to wrap it all up, which looks like a big ‘V’. Just another level of abstraction!
  272. 09:09
  273. Our 8-bit ALU has two inputs, A and B, each with 8 bits. We also need a way to specify what operation the ALU should perform,
  274. 09:17
  275. for example, addition or subtraction.
  276. 09:19
  277. For that, we use a 4-bit operation code.
  278. 09:21
  279. We’ll talk about this more in a later episode, but in brief, 1000 might be the command
  280. 09:27
  281. to add, while 1100 is the command for subtract. Basically, the operation code tells the ALU
  282. 09:33
  283. what operation to perform. And the result of that operation on inputs A and B is an 8-bit output.
  284. 09:38
  285. ALUs also output a series of Flags, which are 1-bit outputs for particular states and statuses.
  286. 09:43
  287. For example, if we subtract two numbers, and the result is 0, our zero-testing circuit, the one we made earlier,
  288. 09:50
  289. sets the Zero Flag to True (1). This is useful if we are trying to determine if two numbers are are equal.
  290. 09:55
  291. If we wanted to test if A was less than B,
  292. 09:57
  293. we can use the ALU to calculate A subtract B and look to see if the Negative Flag was set to true.
  294. 10:03
  295. If it was, we know that A was smaller than B.
  296. 10:05
  297. And finally, there’s also a wire attached to the carry out on the adder we built,
  298. 10:09
  299. so if there is an overflow, we’ll know about it. This is called the Overflow Flag.
  300. 10:13
  301. Fancier ALUs will have more flags, but these three flags are universal and frequently used.
  302. 10:18
  303. In fact, we’ll be using them soon in a future episode.
  304. 10:21
  305. So now you know how your computer does all its basic mathematical operations digitally
  306. 10:25
  307. with no gears or levers required.
  308. 10:27
  309. We’re going to use this ALU when we construct our CPU two episodes from now.
  310. 10:31
  311. But before that, our computer is going to need some memory! We'll talk about that next week.
  312.  
  313.  
  314. Hi, I’m Carrie Anne and welcome to Crash Course Computer Science.
  315. 00:05
  316. So last episode, using just logic gates, we built a simple ALU, which performs arithmetic
  317. 00:11
  318. and logic operations, hence the ‘A’ and the ‘L’.
  319. 00:13
  320. But of course, there’s not much point in calculating a result only to throw it away
  321. 00:17
  322. - it would be useful to store that value somehow, and maybe even run several operations in a row.
  323. 00:22
  324. That's where computer memory comes in!
  325. 00:24
  326. If you've ever been in the middle of a long RPG campaign on your console, or slogging
  327. 00:28
  328. through a difficult level on Minesweeper on your desktop, and your dog came by, tripped
  329. 00:32
  330. and pulled the power cord out of the wall, you know the agony of losing all your progress.
  331. 00:36
  332. Condolences.
  333. 00:38
  334. But the reason for your loss is that your console, your laptop and your computers make
  335. 00:42
  336. use of Random Access Memory, or RAM, which stores things like game state - as long as
  337. 00:46
  338. the power stays on.
  339. 00:47
  340. Another type of memory, called persistent memory, can survive without power, and it’s
  341. 00:51
  342. used for different things; We'll talk about the persistence of memory in a later episode.
  343. 00:55
  344. Today, we’re going to start small - literally by building a circuit that can store one..
  345. 01:00
  346. single.. bit of information.
  347. 01:01
  348. After that, we’ll scale up, and build our very own memory module, and we’ll combine
  349. 01:05
  350. it with our ALU next time, when we finally build our very own CPU!
  351. 01:10
  352. INTRO
  353. 01:19
  354. All of the logic circuits we've discussed so far go in one direction - always flowing
  355. 01:23
  356. forward - like our 8-bit ripple adder from last episode.
  357. 01:26
  358. But we can also create circuits that loop back on themselves.
  359. 01:29
  360. Let’s try taking an ordinary OR gate, and feed the output back into one of its inputs
  361. 01:34
  362. and see what happens.
  363. 01:35
  364. First, let’s set both inputs to 0.
  365. 01:37
  366. So 0 OR 0 is 0, and so this circuit always outputs 0.
  367. 01:41
  368. If we were to flip input A to 1.
  369. 01:44
  370. 1 OR 0 is 1, so now the output of the OR gate is 1.
  371. 01:48
  372. A fraction of a second later, that loops back around into input B, so the OR gate sees that
  373. 01:52
  374. both of its inputs are now 1.
  375. 01:54
  376. 1 OR 1 is still 1, so there is no change in output.
  377. 01:58
  378. If we flip input A back to 0, the OR gate still outputs 1.
  379. 02:01
  380. So now we've got a circuit that records a “1” for us.
  381. 02:04
  382. Except, we've got a teensy tiny problem - this change is permanent!
  383. 02:07
  384. No matter how hard we try, there’s no way to get this circuit to flip back from a 1
  385. 02:12
  386. to a 0.
  387. 02:13
  388. Now let’s look at this same circuit, but with an AND gate instead.
  389. 02:16
  390. We'll start inputs A and B both at 1.
  391. 02:19
  392. 1 AND 1 outputs 1 forever.
  393. 02:21
  394. But, if we then flip input A to 0, because it’s an AND gate, the output will go to 0.
  395. 02:26
  396. So this circuit records a 0, the opposite of our other circuit.
  397. 02:29
  398. Like before, no matter what input we apply to input A afterwards, the circuit will always output 0.
  399. 02:34
  400. Now we’ve got circuits that can record both 0s and 1s.
  401. 02:38
  402. The key to making this a useful piece of memory is to combine our two circuits into what is
  403. 02:42
  404. called the AND-OR Latch.
  405. 02:44
  406. It has two inputs, a "set" input, which sets the output to a 1, and a "reset" input, which
  407. 02:48
  408. resets the output to a 0.
  409. 02:50
  410. If set and reset are both 0, the circuit just outputs whatever was last put in it.
  411. 02:54
  412. In other words, it remembers a single bit of information!
  413. 02:58
  414. Memory!
  415. 02:59
  416. This is called a “latch” because it “latches onto” a particular value and stays that way.
  417. 03:03
  418. The action of putting data into memory is called writing, whereas getting the data out
  419. 03:08
  420. is called reading.
  421. 03:09
  422. Ok, so we’ve got a way to store a single bit of information!
  423. 03:12
  424. Great!
  425. 03:13
  426. Unfortunately, having two different wires for input – set and reset – is a bit confusing.
  427. 03:18
  428. To make this a little easier to use, we really want a single wire to input data, that we
  429. 03:22
  430. can set to either 0 or 1 to store the value.
  431. 03:24
  432. Additionally, we are going to need a wire that enables the memory to be either available
  433. 03:28
  434. for writing or “locked” down --which is called the write enable line.
  435. 03:32
  436. By adding a few extra logic gates, we can build this circuit, which is called a Gated Latch
  437. 03:37
  438. since the “gate” can be opened or closed.
  439. 03:39
  440. Now this circuit is starting to get a little complicated.
  441. 03:41
  442. We don’t want to have to deal with all the individual logic gates... so as before, we’re
  443. 03:44
  444. going to bump up a level of abstraction, and put our whole Gated Latch circuit in a box
  445. 03:48
  446. -- a box that stores one bit.
  447. 03:50
  448. Let’s test out our new component!
  449. 03:52
  450. Let’s start everything at 0.
  451. 03:54
  452. If we toggle the Data wire from 0 to 1 or 1 to 0, nothing happens - the output stays at 0.
  453. 04:00
  454. That’s because the write enable wire is off, which prevents any change to the memory.
  455. 04:04
  456. So we need to “open” the “gate” by turning the write enable wire to 1.
  457. 04:07
  458. Now we can put a 1 on the data line to save the value 1 to our latch.
  459. 04:11
  460. Notice how the output is now 1.
  461. 04:14
  462. Success!
  463. 04:14
  464. We can turn off the enable line and the output stays as 1.
  465. 04:18
  466. Once again, we can toggle the value on the data line all we want, but the output will
  467. 04:21
  468. stay the same.
  469. 04:22
  470. The value is saved in memory.
  471. 04:24
  472. Now let’s turn the enable line on again use our data line to set the latch to 0.
  473. 04:29
  474. Done.
  475. 04:30
  476. Enable line off, and the output is 0.
  477. 04:32
  478. And it works!
  479. 04:33
  480. Now, of course, computer memory that only stores one bit of information isn’t very
  481. 04:37
  482. useful -- definitely not enough to run Frogger.
  483. 04:39
  484. Or anything, really.
  485. 04:41
  486. But we’re not limited to using only one latch.
  487. 04:43
  488. If we put 8 latches side-by-side, we can store 8 bits of information like an 8-bit number.
  489. 04:48
  490. A group of latches operating like this is called a register, which holds a single number,
  491. 04:53
  492. and the number of bits in a register is called its width.
  493. 04:56
  494. Early computers had 8-bit registers, then 16, 32, and today, many computers have registers
  495. 05:01
  496. that are 64-bits wide.
  497. 05:03
  498. To write to our register, we first have to enable all of the latches.
  499. 05:06
  500. We can do this with a single wire that connects to all of their enable inputs, which we set to 1.
  501. 05:11
  502. We then send our data in using the 8 data wires, and then set enable back to 0, and
  503. 05:17
  504. the 8 bit value is now saved in memory.
  505. 05:19
  506. Putting latches side-by-side works ok for a small-ish number of bits.
  507. 05:23
  508. A 64-bit register would need 64 wires running to the data pins, and 64 wires running to
  509. 05:28
  510. the outputs.
  511. 05:29
  512. Luckily we only need 1 wire to enable all the latches, but that’s still 129 wires.
  513. 05:36
  514. For 256 bits, we end up with 513 wires!
  515. 05:40
  516. The solution is a matrix!
  517. 05:42
  518. In this matrix, we don’t arrange our latches in a row, we put them in a grid.
  519. 05:46
  520. For 256 bits, we need a 16 by 16 grid of latches with 16 rows and columns of wires.
  521. 05:52
  522. To activate any one latch, we must turn on the corresponding row AND column wire.
  523. 05:56
  524. Let’s zoom in and see how this works.
  525. 05:58
  526. We only want the latch at the intersection of the two active wires to be enabled,
  527. 06:02
  528. but all of the other latches should stay disabled.
  529. 06:05
  530. For this, we can use our trusty AND gate!
  531. 06:08
  532. The AND gate will output a 1 only if the row and the column wires are both 1.
  533. 06:12
  534. So we can use this signal to uniquely select a single latch.
  535. 06:15
  536. This row/column setup connects all our latches with a single, shared, write enable wire.
  537. 06:20
  538. In order for a latch to become write enabled, the row wire, the column wire, and the write
  539. 06:24
  540. enable wire must all be 1.
  541. 06:26
  542. That should only ever be true for one single latch at any given time.
  543. 06:29
  544. This means we can use a single, shared wire for data.
  545. 06:32
  546. Because only one latch will ever be write enabled, only one will ever save the data
  547. 06:37
  548. -- the rest of the latches will simply ignore values on the data wire because they are not
  549. 06:40
  550. write enabled.
  551. 06:41
  552. We can use the same trick with a read enable wire to read the data later, to get the data
  553. 06:46
  554. out of one specific latch.
  555. 06:48
  556. This means in total, for 256 bits of memory, we only need 35 wires - 1 data wire, 1 write
  557. 06:55
  558. enable wire, 1 read enable wire, and 16 rows and columns for the selection.
  559. 06:59
  560. That’s significant wire savings!
  561. 07:01
  562. But we need a way to uniquely specify each intersection.
  563. 07:05
  564. We can think of this like a city, where you might want to meet someone at 12th avenue
  565. 07:08
  566. and 8th street -- that's an address that defines an intersection.
  567. 07:11
  568. The latch we just saved our one bit into has an address of row 12 and column 8.
  569. 07:15
  570. Since there is a maximum of 16 rows, we store the row address in a 4 bit number.
  571. 07:20
  572. 12 is 1100 in binary.
  573. 07:23
  574. We can do the same for the column address: 8 is 1000 in binary.
  575. 07:28
  576. So the address for the particular latch we just used can be written as 11001000.
  577. 07:35
  578. To convert from an address into something that selects the right row or column, we need
  579. 07:39
  580. a special component called a multiplexer -- which is the computer component with a pretty cool
  581. 07:43
  582. name at least compared to the ALU.
  583. 07:45
  584. Multiplexers come in all different sizes, but because we have 16 rows, we need a 1 to
  585. 07:50
  586. 16 multiplexer.
  587. 07:51
  588. It works like this.
  589. 07:52
  590. You feed it a 4 bit number, and it connects the input line to a corresponding output line.
  591. 07:56
  592. So if we pass in 0000, it will select the very first column for us.
  593. 08:02
  594. If we pass in 0001, the next column is selected, and so on.
  595. 08:06
  596. We need one multiplexer to handle our rows and another multiplexer to handle the columns.
  597. 08:10
  598. Ok, it’s starting to get complicated again, so let’s make our 256-bit memory its own component.
  599. 08:16
  600. Once again a new level of abstraction!
  601. 08:24
  602. It takes an 8-bit address for input - the 4 bits for the column and 4 for the row.
  603. 08:29
  604. We also need write and read enable wires.
  605. 08:32
  606. And finally, we need just one data wire, which can be used to read or write data.
  607. 08:37
  608. Unfortunately, even 256-bits of memory isn’t enough to run much of anything, so we need
  609. 08:42
  610. to scale up even more!
  611. 08:43
  612. We’re going to put them in a row.
  613. 08:45
  614. Just like with the registers.
  615. 08:46
  616. We’ll make a row of 8 of them, so we can store an 8 bit number - also known as a byte.
  617. 08:51
  618. To do this, we feed the exact same address into all 8 of our 256-bit memory components
  619. 08:57
  620. at the same time, and each one saves one bit of the number.
  621. 09:01
  622. That means the component we just made can store 256 bytes at 256 different addresses.
  623. 09:07
  624. Again, to keep things simple, we want to leave behind this inner complexity.
  625. 09:11
  626. Instead of thinking of this as a series of individual memory modules and circuits, we’ll
  627. 09:15
  628. think of it as a uniform bank of addressable memory.
  629. 09:18
  630. We have 256 addresses, and at each address, we can read or write an 8-bit value.
  631. 09:23
  632. We’re going to use this memory component next episode when we build our CPU.
  633. 09:28
  634. The way that modern computers scale to megabytes and gigabytes of memory is by doing the same
  635. 09:32
  636. thing we’ve been doing here -- keep packaging up little bundles of memory into larger, and
  637. 09:36
  638. larger, and larger arrangements.
  639. 09:37
  640. As the number of memory locations grow, our addresses have to grow as well.
  641. 09:42
  642. 8 bits hold enough numbers to provide addresses for 256 bytes of our memory, but that’s all.
  643. 09:48
  644. To address a gigabyte – or a billion bytes of memory – we need 32-bit addresses.
  645. 09:53
  646. An important property of this memory is that we can access any memory location, at any
  647. 09:58
  648. time, and in a random order.
  649. 09:59
  650. For this reason, it’s called Random-Access Memory or RAM.
  651. 10:03
  652. When you hear people talking about how much RAM a computer has - that's the computer’s memory.
  653. 10:07
  654. RAM is like a human’s short term or working memory, where you keep track of things going
  655. 10:11
  656. on right now - like whether or not you had lunch or paid your phone bill.
  657. 10:14
  658. Here’s an actual stick of RAM - with 8 memory modules soldered onto the board.
  659. 10:18
  660. If we carefully opened up one of these modules and zoomed in, The first thing you would see
  661. 10:22
  662. are 32 squares of memory.
  663. 10:23
  664. Zoom into one of those squares, and we can see each one is comprised of 4 smaller blocks.
  665. 10:28
  666. If we zoom in again, we get down to the matrix of individual bits.
  667. 10:31
  668. This is a matrix of 128 by 64 bits.
  669. 10:34
  670. That’s 8192 bits in total.
  671. 10:37
  672. Each of our 32 squares has 4 matrices, so that’s 32 thousand, 7 hundred and 68 bits.
  673. 10:43
  674. And there are 32 squares in total.
  675. 10:45
  676. So all in all, that’s roughly 1 million bits of memory in each chip.
  677. 10:49
  678. Our RAM stick has 8 of these chips, so in total, this RAM can store 8 millions bits,
  679. 10:54
  680. otherwise known as 1 megabyte.
  681. 10:56
  682. That’s not a lot of memory these days -- this is a RAM module from the 1980’s.
  683. 11:00
  684. Today you can buy RAM that has a gigabyte or more of memory - that’s billions of bytes
  685. 11:05
  686. of memory.
  687. 11:06
  688. So, today, we built a piece of SRAM - Static Random-Access Memory – which uses latches.
  689. 11:11
  690. There are other types of RAM, such as DRAM, Flash memory, and NVRAM.
  691. 11:15
  692. These are very similar in function to SRAM, but use different circuits to store the individual
  693. 11:19
  694. bits -- for example, using different logic gates, capacitors, charge traps, or memristors.
  695. 11:24
  696. But fundamentally, all of these technologies store bits of information in massively nested
  697. 11:28
  698. matrices of memory cells.
  699. 11:31
  700. Like many things in computing, the fundamental operation is relatively simple.. it’s the
  701. 11:35
  702. layers and layers of abstraction that’s mind blowing -- like a russian doll that
  703. 11:40
  704. keeps getting smaller and smaller and smaller.
  705. 11:42
  706. I’ll see you next week.
  707.  
  708.  
  709.  
  710.  
  711. 00:03
  712. Hi, I’m Carrie Anne, this is Crash Course Computer Science, and today, we’re talking about processors.
  713. 00:07
  714. Just a warning though - this is probably the most complicated episode in the series.
  715. 00:11
  716. So once you get this, you’re golden.
  717. 00:12
  718. We’ve already made a Arithmetic and Logic Unit, which takes in binary numbers and performs
  719. 00:16
  720. calculations, and we’ve made two types of computer memory: Registers -- small, linear
  721. 00:21
  722. chunks of memory, useful for storing a single value -- and then we scaled up, and made some
  723. 00:25
  724. RAM, a larger bank of memory that can store a lot of numbers located at different addresses.
  725. 00:30
  726. Now it’s time to put it all together and build ourselves the heart of any computer,
  727. 00:34
  728. but without any of the emotional baggage that comes with human hearts.
  729. 00:37
  730. For computers, this is the Central Processing Unit, most commonly called the CPU.
  731. 00:42
  732. INTRO
  733. 00:51
  734. A CPU’s job is to execute programs.
  735. 00:53
  736. Programs, like Microsoft Office, Safari, or your beloved copy of Half Life: 2, are made
  737. 00:57
  738. up of a series of individual operations, called instructions, because they “instruct”
  739. 01:02
  740. the computer what to do.
  741. 01:03
  742. If these are mathematical instructions, like add or subtract, the CPU will configure its
  743. 01:07
  744. ALU to do the mathematical operation.
  745. 01:10
  746. Or it might be a memory instruction, in which case the CPU will talk with memory
  747. 01:14
  748. to read and write values.
  749. 01:15
  750. There are a lot of parts in a CPU, so we’re going to lay it out piece by piece, building
  751. 01:19
  752. up as we go.
  753. 01:20
  754. We’ll focus on functional blocks, rather than showing every single wire.
  755. 01:23
  756. When we do connect two components with a line, this is an abstraction for all of the necessary wires.
  757. 01:28
  758. This high level view is called the microarchitecture.
  759. 01:30
  760. OK, first, we’re going to need some memory.
  761. 01:32
  762. Lets drop in the RAM module we created last episode.
  763. 01:35
  764. To keep things simple, we’ll assume it only has 16 memory locations, each containing 8 bits.
  765. 01:40
  766. Let’s also give our processor four, 8-bit memory registers, labeled A, B, C and D which
  767. 01:45
  768. will be used to temporarily store and manipulate values.
  769. 01:48
  770. We already know that data can be stored in memory as binary values
  771. 01:51
  772. and programs can be stored in memory too.
  773. 01:52
  774. We can assign an ID to each instruction supported by our CPU.
  775. 01:56
  776. In our hypothetical example, we use the first four bits to store the “operation code”,
  777. 02:00
  778. or opcode for short.
  779. 02:02
  780. The final four bits specify where the data for that operation should come from -
  781. 02:06
  782. this could be registers or an address in memory.
  783. 02:08
  784. We also need two more registers to complete our CPU.
  785. 02:11
  786. First, we need a register to keep track of where we are in a program.
  787. 02:14
  788. For this, we use an instruction address register, which as the name suggests, stores the memory
  789. 02:19
  790. address of the current instruction.
  791. 02:20
  792. And then we need the other register to store the current instruction, which we’ll call the instruction register.
  793. 02:26
  794. When we first boot up our computer, all of our registers start at 0.
  795. 02:30
  796. As an example, we’ve initialized our RAM with a simple computer program that we’ll to through today.
  797. 02:35
  798. The first phase of a CPU’s operation is called the fetch phase.
  799. 02:38
  800. This is where we retrieve our first instruction.
  801. 02:41
  802. First, we wire our Instruction Address Register to our RAM module.
  803. 02:44
  804. The register’s value is 0, so the RAM returns whatever value is stored in address 0.
  805. 02:49
  806. In this case, 0010 1110.
  807. 02:52
  808. Then this value is copied into our instruction register.
  809. 02:55
  810. Now that we’ve fetched an instruction from memory, we need to figure out what that instruction is
  811. 02:59
  812. so we can execute it.
  813. 03:00
  814. That is run it.
  815. 03:01
  816. Not kill it.
  817. 03:02
  818. This is called the decode phase.
  819. 03:04
  820. In this case the opcode, which is the first four bits, is: 0010.
  821. 03:08
  822. This opcode corresponds to the “LOAD A” instruction, which loads a value from RAM
  823. 03:13
  824. into Register A.
  825. 03:14
  826. The RAM address is the last four bits of our instruction which are 1110, or 14 in decimal.
  827. 03:19
  828. Next, instructions are decoded and interpreted by a Control Unit.
  829. 03:23
  830. Like everything else we’ve built, it too is made out of logic gates.
  831. 03:26
  832. For example, to recognize a LOAD A instruction, we need a circuit that checks if the opcode
  833. 03:31
  834. matches 0010 which we can do with a handful of logic gates.
  835. 03:35
  836. Now that we know what instruction we’re dealing with, we can go ahead and perform
  837. 03:38
  838. that instruction which is the beginning of the execute phase!
  839. 03:41
  840. Using the output of our LOAD_A checking circuit, we can turn on the RAM’s read enable line
  841. 03:45
  842. and send in address 14.
  843. 03:47
  844. The RAM retrieves the value at that address, which is 00000011, or 3 in decimal.
  845. 03:53
  846. Now, because this is a LOAD_A instruction, we want that value to only be saved into Register A
  847. 03:58
  848. and not any of the other registers.
  849. 03:59
  850. So if we connect the RAM’s data wires to our four data registers, we can use our LOAD_A
  851. 04:04
  852. check circuit to enable the write enable only for Register A.
  853. 04:07
  854. And there you have it -- we’ve successfully loaded the value at RAM address 14 into Register A.
  855. 04:12
  856. We’ve completed the instruction, so we can turn all of our wires off, and we’’re
  857. 04:16
  858. ready to fetch the next instruction in memory.
  859. 04:18
  860. To do this, we increment the Instruction Address Register by 1 which completes the execute phase.
  861. 04:23
  862. LOAD_A is just one of several possible instructions that our CPU can execute.
  863. 04:28
  864. Different instructions are decoded by different logic circuits, which configure the CPU’s
  865. 04:32
  866. components to perform that action.
  867. 04:34
  868. Looking at all those individual decode circuits is too much detail, so since we looked at one example,
  869. 04:38
  870. we’re going to go head and package them all up as a single Control Unit to keep things simple.
  871. 04:43
  872. That’s right a new level of abstraction.
  873. 04:51
  874. The Control Unit is comparable to the conductor of an orchestra, directing all of the different
  875. 04:55
  876. parts of the CPU.
  877. 04:57
  878. Having completed one full fetch/decode/execute cycle, we’re ready to start all over again,
  879. 05:02
  880. beginning with the fetch phase.
  881. 05:03
  882. The Instruction Address Register now has the value 1 in it, so the RAM gives us the value
  883. 05:07
  884. stored at address 1, which is 0001 1111.
  885. 05:12
  886. On to the decode phase!
  887. 05:13
  888. 0001 is the “LOAD B” instruction, which moves a value from RAM into Register B.
  889. 05:20
  890. The memory location this time is 1111, which is 15 in decimal.
  891. 05:24
  892. Now to the execute phase!
  893. 05:26
  894. The Control Unit configures the RAM to read address 15 and configures Register B to receive the data.
  895. 05:31
  896. Bingo, we just saved the value 00001110, or the number 14 in decimal, into Register B.
  897. 05:38
  898. Last thing to do is increment our instruction address register by 1, and we’re done with another cycle.
  899. 05:43
  900. Our next instruction is a bit different.
  901. 05:45
  902. Let’s fetch it.
  903. 05:46
  904. 1000 01 00.
  905. 05:49
  906. That opcode 1000 is an ADD instruction.
  907. 05:53
  908. Instead of an 4-bit RAM address, this instruction uses two sets of 2 bits.
  909. 05:57
  910. Remember that 2 bits can encode 4 values, so 2 bits is enough to select any one of our 4 registers.
  911. 06:02
  912. The first set of 2 bits is 01, which in this case corresponds to Register B, and 00, which is Register A.
  913. 06:09
  914. So “1000 01 00” is the instruction for adding the value in Register B into the value in register A.
  915. 06:17
  916. So to execute this instruction, we need to integrate the ALU we made in Episode 5 into our CPU.
  917. 06:23
  918. The Control Unit is responsible for selecting the right registers to pass in as inputs,
  919. 06:27
  920. and configuring the ALU to perform the right operation.
  921. 06:30
  922. For this ADD instruction, the Control Unit enables Register B and feeds its value into
  923. 06:34
  924. the first input of the ALU.
  925. 06:36
  926. It also enables Register A and feeds it into the second ALU input.
  927. 06:40
  928. As we already discussed, the ALU itself can perform several different operations, so the
  929. 06:45
  930. Control Unit must configure it to perform an ADD operation by passing in the ADD opcode.
  931. 06:50
  932. Finally, the output should be saved into Register A. But it can’t be written directly
  933. 06:54
  934. because the new value would ripple back into the ALU and then keep adding to itself.
  935. 06:58
  936. So the Control Unit uses an internal register to temporarily save the output, turn off the
  937. 07:03
  938. ALU, and then write the value into the proper destination register.
  939. 07:07
  940. In this case, our inputs were 3 and 14, and so the sum is 17, or 00010001 in binary,
  941. 07:16
  942. which is now sitting in Register A. As before, the last thing to do is increment our instruction
  943. 07:20
  944. address by 1, and another cycle is complete.
  945. 07:23
  946. Okay, so let’s fetch one last instruction: 01001101.
  947. 07:29
  948. When we decode it we see that 0100 is a STORE_A instruction, with a RAM address of 13.
  949. 07:35
  950. As usual, we pass the address to the RAM module, but instead of read-enabling the memory, we write-enable it.
  951. 07:40
  952. At the same time, we read-enable Register A. This allows us to use the data line to
  953. 07:45
  954. pass in the value stored in register A.
  955. 07:47
  956. Congrats, we just ran our first computer program!
  957. 07:50
  958. It loaded two values from memory, added them together, and then saved that sum back into memory.
  959. 07:55
  960. Of course, by me talking you through the individual steps, I was manually transitioning the CPU
  961. 08:00
  962. through its fetch, decode and execute phases.
  963. 08:03
  964. But there isn’t a mini Carrie Anne inside of every computer.
  965. 08:06
  966. So the responsibility of keeping the CPU ticking along falls to a component called the clock.
  967. 08:10
  968. As it’s name suggests, the clock triggers an electrical signal at a precise and regular interval.
  969. 08:15
  970. Its signal is used by the Control Unit to advance the internal operation of the CPU,
  971. 08:19
  972. keeping everything in lock-step - like the dude on a Roman galley drumming rhythmically
  973. 08:23
  974. at the front, keeping all the rowers synchronized... or a metronome.
  975. 08:27
  976. Of course you can’t go too fast, because even electricity takes some time to travel
  977. 08:31
  978. down wires and for the signal to settle.
  979. 08:33
  980. The speed at which a CPU can carry out each step of the fetch-decode-execute cycle is called its Clock Speed.
  981. 08:39
  982. This speed is measured in Hertz - a unit of frequency.
  983. 08:42
  984. One Hertz means one cycle per second.
  985. 08:45
  986. Given that it took me about 6 minutes to talk you through 4 instructions -- LOAD, LOAD,
  987. 08:49
  988. ADD and STORE -- that means I have an effective clock speed of roughly .03 Hertz.
  989. 08:53
  990. Admittedly, I’m not a great computer but even someone handy with math
  991. 08:57
  992. might only be able to do one calculation in their head every second or 1 Hertz.
  993. 09:01
  994. The very first, single-chip CPU was the Intel 4004, a 4-bit CPU released in 1971.
  995. 09:08
  996. It’s microarchitecture is actually pretty similar to our example CPU.
  997. 09:12
  998. Despite being the first processor of its kind, it had a mind-blowing clock speed of 740 Kilohertz
  999. 09:18
  1000. -- that’s 740 thousand cycles per second.
  1001. 09:22
  1002. You might think that’s fast, but it’s nothing compared to the processors that we use today.
  1003. 09:26
  1004. One megahertz is one million clock cycles per second, and the computer or even phone
  1005. 09:30
  1006. that you are watching this video on right now is no doubt a few gigahertz -- that's
  1007. 09:34
  1008. BILLIONs of CPU cycles every… single... second.
  1009. 09:38
  1010. Also, you may have heard of people overclocking their computers.
  1011. 09:41
  1012. This is when you modify the clock to speed up the tempo of the CPU -- like when the drummer
  1013. 09:45
  1014. speeds up when the Roman Galley needs to ram another ship.
  1015. 09:48
  1016. Chip makers often design CPUs with enough tolerance to handle a little bit of overclocking,
  1017. 09:52
  1018. but too much can either overheat the CPU, or produce gobbledygook as the signals fall behind the clock.
  1019. 09:58
  1020. And although you don’t hear very much about underclocking, it’s actually super useful.
  1021. 10:02
  1022. Sometimes it’s not necessary to run the processor at full speed...
  1023. 10:04
  1024. maybe the user has stepped away, or just not running a particularly demanding program.
  1025. 10:08
  1026. By slowing the CPU down, you can save a lot of power, which is important for computers
  1027. 10:13
  1028. that run on batteries, like laptops and smartphones.
  1029. 10:15
  1030. To meet these needs, many modern processors can increase or decrease their clock speed
  1031. 10:19
  1032. based on demand, which is called dynamic frequency scaling.
  1033. 10:23
  1034. So, with the addition of a clock, our CPU is complete.
  1035. 10:26
  1036. We can now put a box around it, and make it its own component.
  1037. 10:28
  1038. Yup.
  1039. 10:29
  1040. A new level of abstraction!
  1041. 10:37
  1042. RAM, as I showed you last episode, lies outside the CPU as its own component, and they communicate
  1043. 10:42
  1044. with each other using address, data and enable wires.
  1045. 10:45
  1046. Although the CPU we designed today is a simplified example, many of the basic mechanics we discussed
  1047. 10:50
  1048. are still found in modern processors.
  1049. 10:52
  1050. Next episode, we’re going to beef up our CPU, extending it with more instructions as
  1051. 10:56
  1052. we take our first baby steps into software.
  1053. 10:59
  1054. I’ll see you next week.
  1055.  
  1056.  
  1057.  
  1058.  
  1059. Hi, I’m Carrie Anne and this is Crash Course Computer Science!
  1060. 00:06
  1061. Last episode, we combined an ALU, control unit, some memory, and a clock together to
  1062. 00:10
  1063. make a basic, but functional Central Processing Unit – or CPU – the beating, ticking heart
  1064. 00:14
  1065. of a computer.
  1066. 00:15
  1067. We’ve done all the hard work of building many of these components from the electronic
  1068. 00:19
  1069. circuits up, and now it’s time to give our CPU some actual instructions to process!
  1070. 00:23
  1071. The thing that makes a CPU powerful is the fact that it is programmable – if you write
  1072. 00:28
  1073. a different sequence of instructions, then the CPU will perform a different task.
  1074. 00:31
  1075. So the CPU is a piece of hardware which is controlled by easy-to-modify software!
  1076. 00:36
  1077. INTRO
  1078. 00:44
  1079. Let’s quickly revisit the simple program that we stepped through last episode.
  1080. 00:48
  1081. The computer memory looked like this.
  1082. 00:50
  1083. Each address contained 8 bits of data.
  1084. 00:52
  1085. For our hypothetical CPU, the first four bits specified the operation code, or opcode, and
  1086. 00:57
  1087. the second set of four bits specified an address or registers.
  1088. 01:00
  1089. In memory address zero we have 0010 1110.
  1090. 01:04
  1091. Again, those first four bits are our opcode which corresponds to a “LOAD_A” instruction.
  1092. 01:10
  1093. This instruction reads data from a location of memory specified in those last four bits
  1094. 01:14
  1095. of the instruction and saves it into Register A. In this case, 1110, or 14 in decimal.
  1096. 01:21
  1097. So let’s not think of this of memory address 0 as “0010 1110”, but rather as the instruction
  1098. 01:27
  1099. “LOAD_A 14”.
  1100. 01:28
  1101. That’s much easier to read and understand!
  1102. 01:30
  1103. And for me to say!
  1104. 01:31
  1105. And we can do the same thing for the rest of the data in memory.
  1106. 01:35
  1107. In this case, our program is just four instructions long, and we’ve put some numbers into memory
  1108. 01:39
  1109. too, 3 and 14.
  1110. 01:41
  1111. So now let’s step through this program:
  1112. 01:42
  1113. First is LOAD_A 14, which takes the value in address 14, which is the number 3, and
  1114. 01:47
  1115. stores it into Register A.
  1116. 01:49
  1117. Then we have a “LOAD_B 15” instruction, which takes the value in memory location 15,
  1118. 01:54
  1119. which is the number 14, and saves it into Register B.
  1120. 01:56
  1121. Okay.
  1122. 01:57
  1123. Easy enough.
  1124. 01:58
  1125. But now we have an “ADD” instruction.
  1126. 02:00
  1127. This tells the processor to use the ALU to add two registers together, in this case,
  1128. 02:04
  1129. B and A are specified.
  1130. 02:06
  1131. The ordering is important, because the resulting sum is saved into the second register that’s specified.
  1132. 02:11
  1133. So in this case, the resulting sum is saved into Register A.
  1134. 02:15
  1135. And finally, our last instruction is “STORE_A 13”, which instructs the CPU to write whatever
  1136. 02:19
  1137. value is in Register A into memory location 13.
  1138. 02:23
  1139. Yesss!
  1140. 02:24
  1141. Our program adds two numbers together.
  1142. 02:26
  1143. That’s about as exciting as it gets when we only have four instructions to play with.
  1144. 02:30
  1145. So let’s add some more!
  1146. 02:31
  1147. Now we’ve got a subtract function, which like ADD, specifies two registers to operate on.
  1148. 02:35
  1149. We’ve also got a fancy new instruction called JUMP.
  1150. 02:38
  1151. As the name implies, this causes the program to “jump” to a new location.
  1152. 02:42
  1153. This is useful if we want to change the order of instructions, or choose to skip some instructions.
  1154. 02:45
  1155. For example, a JUMP 0, would cause the program to go back to the beginning.
  1156. 02:48
  1157. At a low level, this is done by writing the value specified in the last four bits into
  1158. 02:53
  1159. the instruction address register, overwriting the current value.
  1160. 02:56
  1161. We’ve also added a special version of JUMP called JUMP_NEGATIVE.
  1162. 03:00
  1163. This only jumps the program if the ALU’s negative flag is set to true.
  1164. 03:04
  1165. As we talked about in Episode 5, the negative flag is only set when the result of an arithmetic
  1166. 03:08
  1167. operation is negative.
  1168. 03:10
  1169. If the result of the arithmetic was zero or positive, the negative flag would not be set.
  1170. 03:14
  1171. So the JUMP NEGATIVE won’t jump anywhere, and the CPU will just continue on to the next instruction.
  1172. 03:19
  1173. And finally, computers need to be told when to stop processing, so we need a HALT instruction.
  1174. 03:24
  1175. Our previous program really should have looked like this to be correct, otherwise the CPU
  1176. 03:28
  1177. would have just continued on after the STORE instruction, processing all those 0’s.
  1178. 03:32
  1179. But there is no instruction with an opcode of 0, and so the computer would have crashed!
  1180. 03:36
  1181. It’s important to point out here that we’re storing both instructions and data in the
  1182. 03:40
  1183. same memory.
  1184. 03:41
  1185. There is no difference fundamentally -- it’s all just binary numbers.
  1186. 03:43
  1187. So the HALT instruction is really important because it allows us to separate the two.
  1188. 03:47
  1189. Okay, so let’s make our program a bit more interesting, by adding a JUMP.
  1190. 03:51
  1191. We’ll also modify our two starting values in memory to 1 and 1.
  1192. 03:55
  1193. Lets step through this program just as our CPU would.
  1194. 03:58
  1195. First, LOAD_A 14 loads the value 1 into Register A.
  1196. 04:01
  1197. Next, LOAD_B 15 loads the value 1 into Register B.
  1198. 04:05
  1199. As before, we ADD registers B and A together, with the sum going into Register A. 1+1 = 2,
  1200. 04:11
  1201. so now Register A has the value 2 in it (stored in binary of course)
  1202. 04:15
  1203. Then the STORE instruction saves that into memory location 13.
  1204. 04:18
  1205. Now we hit a “JUMP 2” instruction.
  1206. 04:20
  1207. This causes the processor to overwrite the value in the instruction address register,
  1208. 04:24
  1209. which is currently 4, with the new value, 2.
  1210. 04:27
  1211. Now, on the processor’s next fetch cycle, we don’t fetch HALT, instead we fetch the
  1212. 04:31
  1213. instruction at memory location 2, which is ADD B A.
  1214. 04:34
  1215. We’ve jumped!
  1216. 04:35
  1217. Register A contains the value 2, and register B contains the value 1.
  1218. 04:38
  1219. So 1+2 = 3, so now Register A has the value 3.
  1220. 04:42
  1221. We store that into memory.
  1222. 04:44
  1223. And we’ve hit the JUMP again, back to ADD B A.
  1224. 04:47
  1225. 1+3 = 4.
  1226. 04:49
  1227. So now register A has the value 4.
  1228. 04:51
  1229. See what's happening here?
  1230. 04:52
  1231. Every loop, we’re adding one.
  1232. 04:53
  1233. Its counting up!
  1234. 04:54
  1235. Cooooool.
  1236. 04:55
  1237. But notice there’s no way to ever escape.
  1238. 04:57
  1239. We’re never.. ever.. going to get to that halt instruction, because we’re always going
  1240. 05:01
  1241. to hit that JUMP.
  1242. 05:02
  1243. This is called an infinite loop – a program that runs forever… ever… ever… ever…
  1244. 05:07
  1245. ever
  1246. 05:08
  1247. To break the loop, we need a conditional jump.
  1248. 05:10
  1249. A jump that only happens if a certain condition is met.
  1250. 05:13
  1251. Our JUMP_NEGATIVE is one example of a conditional jump, but computers have other types too - like
  1252. 05:18
  1253. JUMP IF EQUAL and JUMP IF GREATER.
  1254. 05:20
  1255. So let’s make our code a little fancier and step through it.
  1256. 05:24
  1257. Just like before, the program starts by loading values from memory into registers A and B.
  1258. 05:28
  1259. In this example, the number 11 gets loaded into Register A, and 5 gets loaded into Register B.
  1260. 05:34
  1261. Now we subtract register B from register A. That’s 11 minus 5, which is 6, and so 6
  1262. 05:39
  1263. gets saved into Register A.
  1264. 05:40
  1265. Now we hit our JUMP NEGATIVE.
  1266. 05:42
  1267. The last ALU result was 6.
  1268. 05:44
  1269. That’s a positive number, so the the negative flag is false.
  1270. 05:47
  1271. That means the processor does not jump.
  1272. 05:49
  1273. So we continue on to the next instruction...
  1274. 05:51
  1275. ...which is a JUMP 2.
  1276. 05:52
  1277. No conditional on this one, so we jump to instruction 2 no matter what.
  1278. 05:56
  1279. Ok, so we’re back at our SUBTRACT Register B from Register A. 6 minus 5 equals 1.
  1280. 06:01
  1281. So 1 gets saved into register A.
  1282. 06:03
  1283. Next instruction.
  1284. 06:04
  1285. We’re back again at our JUMP NEGATIVE.
  1286. 06:06
  1287. 1 is also a positive number, so the CPU continues on to the JUMP 2, looping back around again
  1288. 06:11
  1289. to the SUBTRACT instruction.
  1290. 06:13
  1291. This time is different though.
  1292. 06:14
  1293. 1 minus 5 is negative 4.
  1294. 06:17
  1295. And so the ALU sets its negative flag to true for the first time.
  1296. 06:20
  1297. Now, when we advance to the next instruction,
  1298. 06:23
  1299. JUMP_NEGATIVE 5, the CPU executes the jump to memory location 5.
  1300. 06:27
  1301. We’re out of the infinite loop!
  1302. 06:29
  1303. Now we have a ADD B to A. Negative 4 plus 5, is positive 1, and we save that into Register A.
  1304. 06:35
  1305. Next we have a STORE instruction that saves Register A into memory address 13.
  1306. 06:39
  1307. Lastly, we hit our HALT instruction and the computer rests.
  1308. 06:43
  1309. So even though this program is only 7 instructions long, the CPU ended up executing 13 instructions,
  1310. 06:49
  1311. and that's because it looped twice internally.
  1312. 06:52
  1313. This code calculated the remainder if we divide 5 into 11, which is one.
  1314. 06:56
  1315. With a few extra lines of code, we could also keep track of how many loops we did, the count
  1316. 07:00
  1317. of which would be how many times 5 went into 11… we did two loops, so that means 5 goes
  1318. 07:05
  1319. into 11 two times... with a remainder of 1.
  1320. 07:08
  1321. And of course this code could work for any two numbers, which we can just change in memory
  1322. 07:12
  1323. to whatever we want: 7 and 81, 18 and 54, it doesn’t matter -- that’s the power
  1324. 07:17
  1325. of software!
  1326. 07:18
  1327. Software also allowed us to do something our hardware could not.
  1328. 07:21
  1329. Remember, our ALU didn’t have the functionality to divide two numbers, instead it’s the
  1330. 07:26
  1331. program we made that gave us that functionality.
  1332. 07:28
  1333. And then other programs can use our divide program to do even fancier things.
  1334. 07:32
  1335. And you know what that means.
  1336. 07:34
  1337. New levels of abstraction!
  1338. 07:41
  1339. So, our hypothetical CPU is very basic – all of its instructions are 8 bits long, with
  1340. 07:46
  1341. the opcode occupying only the first four bits.
  1342. 07:49
  1343. So even if we used every combination of 4 bits, our CPU would only be able to support
  1344. 07:54
  1345. a maximum of 16 different instructions.
  1346. 07:56
  1347. On top of that, several of our instructions used the last 4 bits to specify a memory location.
  1348. 08:01
  1349. But again, 4 bits can only encode 16 different values, meaning we can address a maximum of
  1350. 08:06
  1351. 16 memory locations - that’s not a lot to work with.
  1352. 08:10
  1353. For example, we couldn’t even JUMP to location 17, because we literally can’t fit the number
  1354. 08:15
  1355. 17 into 4 bits.
  1356. 08:16
  1357. For this reason, real, modern CPUs use two strategies.
  1358. 08:19
  1359. The most straightforward approach is just to have bigger instructions, with more bits,
  1360. 08:23
  1361. like 32 or 64 bits.
  1362. 08:25
  1363. This is called the instruction length.
  1364. 08:28
  1365. Unsurprisingly.
  1366. 08:29
  1367. The second approach is to use variable length instructions.
  1368. 08:32
  1369. For example, imagine a CPU that uses 8 bit opcodes.
  1370. 08:35
  1371. When the CPU sees an instruction that needs no extra values, like the HALT instruction,
  1372. 08:40
  1373. it can just execute it immediately.
  1374. 08:41
  1375. However, if it sees something like a JUMP instruction, it knows it must also fetch
  1376. 08:45
  1377. the address to jump to, which is saved immediately behind the JUMP instruction in memory.
  1378. 08:50
  1379. This is called, logically enough, an Immediate Value.
  1380. 08:52
  1381. In such processor designs, instructions can be any number of bytes long, which makes the
  1382. 08:56
  1383. fetch cycle of the CPU a tad more complicated.
  1384. 08:59
  1385. Now, our example CPU and instruction set is hypothetical, designed to illustrate key working
  1386. 09:04
  1387. principles.
  1388. 09:05
  1389. So I want to leave you with a real CPU example.
  1390. 09:07
  1391. In 1971, Intel released the 4004 processor.
  1392. 09:11
  1393. It was the first CPU put all into a single chip and paved the path to the intel processors
  1394. 09:15
  1395. we know and love today.
  1396. 09:17
  1397. It supported 46 instructions, shown here.
  1398. 09:19
  1399. Which was enough to build an entire working computer.
  1400. 09:22
  1401. And it used many of the instructions we’ve talked about like JUMP ADD SUBTRACT and LOAD.
  1402. 09:26
  1403. It also uses 8-bit immediate values, like we just talked about, for things like JUMPs,
  1404. 09:31
  1405. in order to address more memory.
  1406. 09:33
  1407. And processors have come a long way since 1971.
  1408. 09:36
  1409. A modern computer processor, like an Intel Core i7, has thousands of different instructions
  1410. 09:41
  1411. and instruction variants, ranging from one to fifteen bytes long.
  1412. 09:44
  1413. For example, there’s over a dozens different opcodes just for variants of ADD!
  1414. 09:48
  1415. And this huge growth in instruction set size is due in large part to extra bells and whistles
  1416. 09:52
  1417. that have been added to processor designs overtime, which we’ll talk about next episode.
  1418. 09:57
  1419. See you next week!
Add Comment
Please, Sign In to add comment