YouTube2Text

How do computers work? (from scratch, no prior knowledge needed) — Transcript

by Milen Patel · 122,012 words · 16,693 segments · language en · Watch on YouTube

Full transcript

  1. 0:00So, in this video, I'd like to try and
  2. 0:03explain just exactly how a computer
  3. 0:06works. And I know that sounds like an
  4. 0:08incredibly open-ended and difficult
  5. 0:10question to answer. And I'm going to go
  6. 0:12ahead and guess that the length of this
  7. 0:14video will make that self-evident that
  8. 0:17it's not any trivial question in itself.
  9. 0:19But, uh, this is something that I
  10. 0:22wondered a lot growing up. And, uh, even
  11. 0:25when I got to college, I knew I was
  12. 0:27going to study computer science. But it
  13. 0:29wasn't until halfway through my degree
  14. 0:30where I took the course which answered
  15. 0:33this question and I found everything I
  16. 0:36learned really interesting. Ended up
  17. 0:38TAing that course for a few semesters
  18. 0:40afterwards and uh find the really
  19. 0:43low-level stuff of computing to be
  20. 0:44fascinating. And I've been surprised at
  21. 0:48uh a the lack of direct resources online
  22. 0:51that can answer this question for just
  23. 0:54an average person who's curious and b
  24. 0:57how many uh friends I have working
  25. 1:01technical jobs or who studied computer
  26. 1:04science or related degree in college and
  27. 1:07are not too sure how a computer actually
  28. 1:09works. And I think um there's a lot of
  29. 1:14benefits to be held from this knowledge.
  30. 1:16Uh if you are just curious like I am, I
  31. 1:19want to cater this video to be
  32. 1:21completely digestible even if you have
  33. 1:23no mathematical or technical background.
  34. 1:26And I also want this video to be useful
  35. 1:29for someone who might already be uh
  36. 1:31doing an engineering relating job
  37. 1:33because uh a lot of these lower level
  38. 1:35principles might actually affect how you
  39. 1:37structure code or just uh lay out your
  40. 1:40programs at a higher level. Um so in
  41. 1:43this video I'd like to start with no
  42. 1:46assumption of what a computer work how a
  43. 1:49computer works. uh just basic
  44. 1:52mathematics will be assumed which I'm
  45. 1:54assuming if you clicked on this video
  46. 1:55you have a somewhat of an understanding
  47. 1:57of um we're just going to be starting
  48. 1:59simple with like addition, subtraction,
  49. 2:01multiplication, division uh and like
  50. 2:04exponents. I think that's probably all
  51. 2:06the background you need to be able to
  52. 2:09follow along for this entire video. So
  53. 2:11I'm going to answer this question by
  54. 2:14making a CPU from scratch. Um
  55. 2:18and then we're going to go ahead take
  56. 2:20that processor uh which can run one
  57. 2:23program and build an operating system or
  58. 2:26just explain kind of at a higher level
  59. 2:28how an operating system can be built for
  60. 2:31our processor uh and what an operating
  61. 2:33system actually is because that's also a
  62. 2:35great question in itself. Um but without
  63. 2:38getting into too much detail now the
  64. 2:40computer you're watching this on even if
  65. 2:42it's a phone uh is is doing multiple
  66. 2:44things at once. And if you're like me,
  67. 2:47uh you probably have this YouTube video
  68. 2:48open, iMessage in your background, um
  69. 2:51and maybe a few other tabs behind those
  70. 2:53two. And uh it's a really interesting
  71. 2:56question cuz we're going to get to a
  72. 2:58point in this video where you design a
  73. 2:59CPU and it can execute one program. And
  74. 3:02a really common and good follow-up
  75. 3:05question is how can my computer run
  76. 3:07multiple programs at the same time? Um,
  77. 3:10and so we're going to go ahead and dive
  78. 3:12into what that looks like as well
  79. 3:14because it's a it's interesting to know
  80. 3:15how a computer works. Um, but I also
  81. 3:17want to make it pragmatic and get to
  82. 3:19something more modern uh, which is an
  83. 3:22operating system. So, uh, I also would
  84. 3:25like this video to be useful um, even if
  85. 3:28you don't watch it all the way through.
  86. 3:30I have no idea how long this is going to
  87. 3:32be. I am guessing it will not be short
  88. 3:34though. And what I would hope is um for
  89. 3:39those of you who don't watch it all the
  90. 3:41way through, which is the vast majority,
  91. 3:42I'm assuming, uh the individual segments
  92. 3:46uh will be of value themselves. And
  93. 3:49maybe hopefully you learn something
  94. 3:51interesting along the way. Even if you
  95. 3:53didn't take it all the way, you learned
  96. 3:54something. Um and what we're actually
  97. 3:57going to start at is one of the most
  98. 4:00interesting topics I think personally uh
  99. 4:02that anyone can know. And if you only
  100. 4:05watch this first segment of the video
  101. 4:06and nothing else, you've at least
  102. 4:08learned something really interesting.
  103. 4:09Um, so I'm going to start by uh
  104. 4:14explaining how computers represent
  105. 4:16numbers and letters. Uh, how do we how
  106. 4:19can computers count? How can they add
  107. 4:21numbers? Um,
  108. 4:23and you've probably heard of something
  109. 4:25along the lines of computers speak in
  110. 4:28zeros and ones. And uh, this isn't
  111. 4:31nonsense. It's actually very true.
  112. 4:32Computers don't really recognize or
  113. 4:35process numbers the way we do. They only
  114. 4:38operate on zeros and ones. And once we
  115. 4:42uh understand how computers represent
  116. 4:44numbers, it's going to make sense why
  117. 4:46they do it like this. Um without getting
  118. 4:50into too much specifics now, but also
  119. 4:52without being incredibly vague, uh I'll
  120. 4:55say when we design computers, um they're
  121. 4:57built with electricity. And so we can
  122. 5:00represent the absence of electricity
  123. 5:01with a zero and the presence of
  124. 5:03electricity with a one. Um and with this
  125. 5:06it it kind of gives us a lot of
  126. 5:08flexibility to well if we can represent
  127. 5:10anything in zeros and ones and we know
  128. 5:12how to represent zeros and ones with
  129. 5:13electricity or the absence of it. Then
  130. 5:15we can kind of build on top of that and
  131. 5:17design an entire computer. Um, and you
  132. 5:19may be wondering, well, why don't we
  133. 5:21just use a little a little bit of
  134. 5:23electricity represents uh a one, a
  135. 5:26little bit more than that represents a
  136. 5:27two, and so on and so on until the the
  137. 5:30maximum of electricity we could have
  138. 5:31represents a 10, and then computers
  139. 5:33could just work with the the numbers we
  140. 5:35know. Um, but from an engineering
  141. 5:38perspective, it's not that easy to
  142. 5:40implement. Um, I don't want to get into
  143. 5:41the specifics of electricity because
  144. 5:43that's also uh probably a little too low
  145. 5:45level for what I want to accomplish in
  146. 5:46this video, but um, it turns out
  147. 5:50electricity is is very inconsistent in
  148. 5:52in in I guess flickers might be a better
  149. 5:54word for what I'm trying to say. Um, so
  150. 5:56we find it easier just to say have a
  151. 5:58certain threshold of of electricity,
  152. 6:00quote unquote, and anything below that
  153. 6:02we interpret as a zero, and anything
  154. 6:03above that threshold we interpret as a
  155. 6:05one. Um, and again, it's not going to
  156. 6:08make perfect sense what I'm saying. If
  157. 6:10you're already a little bit confused, it
  158. 6:12should be confusing cuz I'm kind of
  159. 6:14jumping jumping ahead of myself here.
  160. 6:17So, I just wanted to give some context
  161. 6:19for why we uh represent numbers with
  162. 6:22just zeros and ones in computers. So,
  163. 6:24let's go ahead and understand how
  164. 6:25computers represent numbers. Um, so I'm
  165. 6:28going to clear this screen here that
  166. 6:29you've been staring at for 5 minutes
  167. 6:31now. And let's start with uh a quick
  168. 6:35recap of how we represent numbers. So if
  169. 6:38I put the number 724 on the screen,
  170. 6:42intuitively you know exactly what this
  171. 6:44is. I mean it's a number you can
  172. 6:45rationalize. You can imagine say 724
  173. 6:49uh pencils in your hand or something
  174. 6:50like that. But how do we actually uh how
  175. 6:54did we learn this? When you were in
  176. 6:55elementary school and learning how to
  177. 6:57count, this number wasn't always easy to
  178. 6:59you. In fact, the first time you saw
  179. 7:00this, it probably made no sense to you,
  180. 7:02but you're older now, you're smarter,
  181. 7:03and you know how to interpret this. Um,
  182. 7:06so I'm going to redumb this back down to
  183. 7:08our elementary school level and uh see
  184. 7:11how we can build this number by looking
  185. 7:14at each of the digit positions. So, we
  186. 7:16know this right here is the ones place.
  187. 7:19That's probably not surprising. Um, we
  188. 7:22know this is called the 10's place. And
  189. 7:24then we know this is called the
  190. 7:25hundred's place.
  191. 7:29And uh this is something you know and
  192. 7:32you don't remember when you learned it,
  193. 7:33but you know that you learned it at some
  194. 7:35point. And we know that if we keep
  195. 7:36going, we have the thousand's place and
  196. 7:38then the 10,000's place, then the
  197. 7:40hundred thousand's place and the
  198. 7:42million's place, the 10 million's place,
  199. 7:44the 100 million's place, and so on and
  200. 7:46so on. Um
  201. 7:49and so
  202. 7:51what exactly do each of these places
  203. 7:53mean? And how can we take the values
  204. 7:56from each of these places and and
  205. 7:57generate a composite number? Um, so it
  206. 8:01turns out that to to get the value of a
  207. 8:03number, uh, we have to, and this is
  208. 8:06going to sound painfully obvious and
  209. 8:07almost a little bit confusing because of
  210. 8:09how obvious and true it is to you, but
  211. 8:11I'm saying it cuz it's going to make
  212. 8:12sense when we look at the computer
  213. 8:14version. um to get the value of a
  214. 8:17number, we could look at the value of
  215. 8:19each of the digits places and kind of
  216. 8:23sum them all up together. So, we know in
  217. 8:25the ones place, this four uh is just
  218. 8:29going to be whatever value is in that
  219. 8:31digit spot times the weight of that
  220. 8:33spot. So, right here we have 4 * 1,
  221. 8:36which equals 4. And then over here, we
  222. 8:41have a two and it's in the 10's place.
  223. 8:44So that's going to be time 10 and that's
  224. 8:46going to be 20. And then over here we
  225. 8:50have a [snorts] seven in the hundred's
  226. 8:52place. So we multiply it by the weight
  227. 8:54of that place which is a 100 and we have
  228. 8:57700. And then we can as I said we could
  229. 9:00add all these values together. So once
  230. 9:02we compute the weight of each digit
  231. 9:04position we can go ahead and add those
  232. 9:06uh those scaled weights together. So
  233. 9:09then we have 700 + 20 + 4. And I'm going
  234. 9:14to stack those numbers on top of each
  235. 9:16other because that's kind of how we
  236. 9:16learn how to add numbers. Um, oops, not
  237. 9:2024. Uh, and we know exactly that there's
  238. 9:23only one non-zero digit in each
  239. 9:25position. So it's just going to carry
  240. 9:27right down. Um, and so we kind of
  241. 9:30started with 724 and ended with 724. Um,
  242. 9:34and that's that's nothing too crazy. Um,
  243. 9:38but let's go ahead and and structure
  244. 9:39that out a little more. So, what we saw
  245. 9:41here is that 724
  246. 9:44is equal to 7 * 100 +
  247. 9:502 * 10 +
  248. 9:544 * 1. And where did that come from?
  249. 9:58Just to recap, it is for each digit
  250. 10:01position, we take the value that is in
  251. 10:04that that spot and then multiply it by
  252. 10:07the weight of the position. So we know
  253. 10:09the very first digit position is the
  254. 10:11one's place, so its weight is one. Then
  255. 10:13the next one is the 10's place, so its
  256. 10:14weight is 10. And then the next one's
  257. 10:16the hundred's place, so its weight is
  258. 10:18100. Um, and then if we were to expand,
  259. 10:22so we've kind of taken our number 724
  260. 10:24and written written it out in this
  261. 10:26expanded way. Um, and hopefully I've
  262. 10:29convinced you that this is true and
  263. 10:31correct, and it it is. Um, unless I've
  264. 10:33made some error, but let's look at an
  265. 10:36even uh more alternative way uh to to to
  266. 10:41write out this expanded form. And all
  267. 10:44I'm really going to do is uh replace the
  268. 10:47weights um of the position. So, let me
  269. 10:51write this out and then we'll kind of uh
  270. 10:54revisit what I'm trying to say.
  271. 11:00Okay, so I told you um that this is
  272. 11:04going to run on basic math. Uh and the
  273. 11:07exponent is probably as complicated as
  274. 11:09we need to get for you to understand
  275. 11:10everything about how a computer works.
  276. 11:13Um everything loosely put everything uh
  277. 11:16that I'm going to teach you in this
  278. 11:17video. Um so just to recall that uh 10
  279. 11:22uh to the 1 is is 10 10^ squared uh when
  280. 11:28we have a a number raised to an exponent
  281. 11:30it just means we multiply the number by
  282. 11:31itself that many times. Um so 10 by 10
  283. 11:36is equal to 100. And then if we had 10
  284. 11:39cubed we multiply 10 by itself three
  285. 11:42times. um and that's going to be a
  286. 11:45th00and.
  287. 11:47And then uh one caveat to that is um
  288. 11:51there's this rule that says anything
  289. 11:53raised to the zero power is equal one.
  290. 11:55Um and uh so that means that when we
  291. 11:59have 10^ the 0, it's going to equal one.
  292. 12:01Um, that's just something you probably
  293. 12:03learned in algebra 2, uh, from high
  294. 12:06school, which is probably so far out of
  295. 12:08your memory at this point, but just just
  296. 12:10take it to be the truth that, uh,
  297. 12:12anything raised to the power of zero,
  298. 12:14we're just going to say equals 1. Um so
  299. 12:19what I've done here is I've rewritten
  300. 12:20our expression uh in this kind of
  301. 12:24exponentiated form where each component
  302. 12:27in our sum is some number multiplied by
  303. 12:3110 raised to some power. Um and we also
  304. 12:36have it that uh the powers are
  305. 12:39increasing. So we start with the zero
  306. 12:41power then we go to power one and then
  307. 12:43we go to power two. Uh, and that that's
  308. 12:47funny enough and that that's actually uh
  309. 12:50not surprising or the more you think
  310. 12:52about it, the more unsurprising it
  311. 12:54should be because uh 10^ the 0 is 1 and
  312. 12:57so that's the one's place and then uh 10
  313. 13:00to the^ of 1 is 10 and that's the 10's
  314. 13:03place and then 10 to the^ of two is 100
  315. 13:05and that's what we call the hundred's
  316. 13:06place. Um so all we've really done here
  317. 13:09is is taken our number 724 and in write
  318. 13:13it out in this expanded form. Um and
  319. 13:17that is all we will recap about the
  320. 13:20numbers that you and I know. So now that
  321. 13:23we know uh or now that I've shown you
  322. 13:25just an alternate way to consider the
  323. 13:27numbers we know um we have the context
  324. 13:30uh to go ahead and move on and learn how
  325. 13:33computers count numbers. So this is how
  326. 13:35we count numbers. And it turns out
  327. 13:37computers do it very similarly. So all
  328. 13:39of this right here is called decimal.
  329. 13:43That is our human way of counting
  330. 13:45numbers. And it's also something that's
  331. 13:47called base 10.
  332. 13:50And what does that mean? Base 10 is
  333. 13:53worth uh worth probably discussing a
  334. 13:55little bit before I move on and and jump
  335. 13:56to the computer equivalent. But um one
  336. 14:00way to interpret base 10 is that it
  337. 14:02means that for any digit position in a
  338. 14:05number, there are 10 different values
  339. 14:07that could go into that digit spot. So
  340. 14:10uh in our 724,
  341. 14:13um let's say I pick this digit position
  342. 14:14right here. We could either have uh a
  343. 14:18zero, a one, a two, or all the way up to
  344. 14:21a nine. Um
  345. 14:24so we could have 10 different numbers
  346. 14:27could go in this spot. And if you're
  347. 14:28wondering why it's not nine, it's
  348. 14:30because uh one through nine is nine
  349. 14:33numbers and then the digit zero lets us
  350. 14:35add one and so we have 10. So we
  351. 14:38actually have it that in uh any digit
  352. 14:41position um there can be 10 different
  353. 14:43values in base 10. And uh let's also see
  354. 14:47what happens when we're counting. So for
  355. 14:49counting in base 10 i.e. The only way
  356. 14:52that you and I know how to count for
  357. 14:54now, um, we start with the number zero,
  358. 14:57it becomes one, and then two, three, and
  359. 15:01then we're going to go all the way up
  360. 15:03and we get to nine. And each time here,
  361. 15:06we're just adding one. So, we're just
  362. 15:08plus one, plus one, um, all the way
  363. 15:10down. We get to eight, we add + one, we
  364. 15:12get to nine. Um, and we've used all 10
  365. 15:16available values that can fit in this
  366. 15:18digit position. So when we want to add
  367. 15:20one to nine um we're stuck. We can't do
  368. 15:24that in one digit position. So we have
  369. 15:26this idea of rolling over um where we we
  370. 15:30reset this digit back to zero and then
  371. 15:33carry carry uh a one over to the next
  372. 15:37digit place. Um and so if we take these
  373. 15:40numbers and and write it out in our
  374. 15:42expanded form, then we know 9 is just
  375. 15:44equal to or let's start with 10. 10 is
  376. 15:47equal to 1 * 10 uh + 0 *
  377. 15:541. Um oops. And then if we have 9 um
  378. 15:58then we have zero in the 10's position
  379. 16:01and a nine in the ones position. Um, and
  380. 16:05so it's kind of curious that when we we
  381. 16:07get to the maximum value in a current
  382. 16:10digit position and we want to add one to
  383. 16:12that, then we roll over the current
  384. 16:14digit position back to a zero and then
  385. 16:18add one to the next digit position. This
  386. 16:21kind of makes sense because what we're
  387. 16:23doing here is we are when we have this
  388. 16:27nine here, um, we know that we want to
  389. 16:30add one more to our collective value. So
  390. 16:32what we could do is subtract nine from
  391. 16:35this spot and then add one from this
  392. 16:38spot and that gets us the exact
  393. 16:40difference. Um that that is the same as
  394. 16:43accomplishing uh a net just addition of
  395. 16:46one. We took all the value from the uh
  396. 16:50the digit position to the right and then
  397. 16:52reset it to a zero and then added one to
  398. 16:55the next digit position to the left and
  399. 16:57this is the same number. Um,
  400. 17:01so that's how we kind of carry over and
  401. 17:03and we kind of see this happen when we
  402. 17:04add numbers. So if I go to a quick
  403. 17:06example where we add numbers, let's say
  404. 17:08we have um 18, we want to add three.
  405. 17:12Then uh we have 18 + 18 + 3. We start in
  406. 17:17the rightmost digit position and we add
  407. 17:19the two numbers together. So an 8 + 3 is
  408. 17:21an 11. And you remember from elementary
  409. 17:24school that you put the one down here,
  410. 17:26the remainder, and then you add the
  411. 17:28carryover up here. And um then you do 1
  412. 17:31plus one is two. And that's that is the
  413. 17:33uh kind of exact same logic that we're
  414. 17:35showing in this in this uh extended
  415. 17:38form. But obviously when you were
  416. 17:40younger, there was no no reasonable way
  417. 17:42for you to understand this version of
  418. 17:44things. You kind of just learn this rule
  419. 17:45that you uh you kind of take what you
  420. 17:48have. if if you if a digit position
  421. 17:51exceeds 10, you put the remainder down
  422. 17:53below and then add one to the carryover.
  423. 17:57Um, and so that is uh just some more
  424. 18:02context that we're going to see this
  425. 18:03come back very shortly. So we saw that
  426. 18:05base 10 is this idea that we can have 10
  427. 18:09different values in any digit position.
  428. 18:12And now we're going to come and uh learn
  429. 18:15what base 2 is. So we said humans speak
  430. 18:18in base 10. Um so this decimal
  431. 18:23is synonymous with base 10.
  432. 18:26And now I'm going to make this claim
  433. 18:28that computers uh operate in base 2. And
  434. 18:33this is called binary.
  435. 18:37And so what does this actually mean? Um
  436. 18:41well we said in base 10 what that means
  437. 18:43is that in any digit position you can
  438. 18:45have 10 different values. And so what we
  439. 18:49can translate that to in base 2 is then
  440. 18:51in any digit position we can have two
  441. 18:53different values. And those two
  442. 18:55different values are zero and one. Um
  443. 19:00and that is just exactly where the idea
  444. 19:03that computers only speak in zero and
  445. 19:05one come from. Um but let's go ahead and
  446. 19:08see what that means. So, um, I'm going
  447. 19:12to give us a binary number. So, we're
  448. 19:14going to start with the binary number
  449. 19:161011.
  450. 19:17Um, and this is a binary number. This is
  451. 19:20this is not to be confused with the
  452. 19:22number 1,11.
  453. 19:25This is uh which is in base 10. Um, so I
  454. 19:28will distinguish it by subsetting it
  455. 19:30with this two. Um, so I will try to make
  456. 19:33the distinction as clear as possible. If
  457. 19:35we are speaking about a decimal number,
  458. 19:38i.e. a number that you and I are
  459. 19:39familiar with, um, I'll write a 10 next
  460. 19:42to it. So, if instead I wanted to say, I
  461. 19:45guess our example above, we had the
  462. 19:46number 724.
  463. 19:48So, from now on, I would say 724 base 10
  464. 19:52just so you uh don't get confused. But
  465. 19:55in general, it's for I'm not going to
  466. 19:57try to give you a a complicated example.
  467. 19:59Um, so if there's just zeros and ones,
  468. 20:02it's probably safe for you to assume
  469. 20:04that I'm talking about base 2. And if
  470. 20:06there's other digits, then it's the base
  471. 20:0810 that we're so familiar with. Um,
  472. 20:12and let's go ahead and I'm also going to
  473. 20:15rewrite our expanded form of 724. So,
  474. 20:19let me move this down. Um, and for 724,
  475. 20:23we said it was 7 * 10^ 2
  476. 20:27+ 2 * 10 1st power + 4 * 10 0 power.
  477. 20:34because we start uh with the lowest
  478. 20:36power of 10 and then we just keep adding
  479. 20:38one each time. Um and so now we're I'm
  480. 20:41I'm kind of we're kind of curious here.
  481. 20:43Uh how can we take this binary value and
  482. 20:46translate it back to a decimal value?
  483. 20:50What does 1011 mean in binary? Like what
  484. 20:54number is that to us? Um and it turns
  485. 20:58out we're just going to follow the same
  486. 21:00exact expansion rules. So, uh, we're
  487. 21:03going to sum up for each digit position,
  488. 21:07um, the value in that position, which is
  489. 21:09either a one or a zero. And then instead
  490. 21:12of 10 raised to some power, we're going
  491. 21:16to use two raised to some power because
  492. 21:19this was for base 10, but now we're
  493. 21:22going to be using base 2. So it'll be
  494. 21:24we're going to start with um the one
  495. 21:27spot is going to be two to the zero and
  496. 21:30then instead of the 10 spot being
  497. 21:32afterwards uh we're going to have the
  498. 21:34two spot which is a little interesting
  499. 21:36cuz you know in our in our current uh
  500. 21:39number we would have um in our in our
  501. 21:41binary system that we're so comfortable
  502. 21:42with we start with the ones position and
  503. 21:45then go to the 10's position and then
  504. 21:47the hundred's position. Um, and now
  505. 21:50we're going to start with the one's
  506. 21:51position and then go to the two's
  507. 21:53position cuz that's 2 to the first
  508. 21:55power. And then after that position, we
  509. 21:58have two to the second power, which is
  510. 21:59four. Um, and after that would be 8. And
  511. 22:04so if I come to our original decimal
  512. 22:06example, we notice that between each of
  513. 22:08these, we're multiplying by 10. And now
  514. 22:11we're in each of these, we're
  515. 22:13multiplying by two. Um, we're looking at
  516. 22:15increasing powers of two, which is is is
  517. 22:18pretty interesting. Um, so let me go
  518. 22:21ahead and erase that. And if this is
  519. 22:23feeling a little bit confusing, I really
  520. 22:25hope it it it should it shouldn't feel
  521. 22:27intuitive yet. Um, I've said a lot of
  522. 22:30random things so far in this video. Um,
  523. 22:33and now is where we're actually start
  524. 22:35tying them together. So, let me spill
  525. 22:38the answer for you on this one, and then
  526. 22:40we'll we'll walk through and see what it
  527. 22:41means. So,
  528. 22:43um, let's start with the rightmost digit
  529. 22:45position. Uh, cuz we start with, uh, 2
  530. 22:49to the^ of 0. Um, so I'm going to do 2
  531. 22:52to the^ 0. And what do we multiply it
  532. 22:54by? We multiply it by the one that's
  533. 22:56there. And then we have that digit
  534. 22:58position done. Now we come on to this
  535. 23:00digit position. Uh, we have uh, we're
  536. 23:03not on the 2 to the^ of zero. Now we're
  537. 23:06on 2 to the^ of one. And then we
  538. 23:08multiply that by whatever is in that
  539. 23:10digit position, which also just happens
  540. 23:11to be a one. Um, let me move this over
  541. 23:14here. Um, and then we then move on to
  542. 23:19this digit position, which is the 2 to
  543. 23:23the^ of two position. And what's there
  544. 23:26is multiplied by a zero. And then we
  545. 23:29come on to the very last digit position.
  546. 23:31And that one is uh the next power to
  547. 23:33use. We use powers 0, one, and two. So
  548. 23:36now we use the power of three um and we
  549. 23:38multiply it by whatever is there. So now
  550. 23:42we have this expression
  551. 23:44um and you might need to pause here and
  552. 23:46just read this out loud to yourself to
  553. 23:48make sure that you understand exactly
  554. 23:50how I derived this long form expression.
  555. 23:53Uh it may not be clear what exactly I'm
  556. 23:55doing but how I got this expression
  557. 23:57should be clear. Um
  558. 24:00and you should also be able to
  559. 24:02understand that uh in base 10 i.e. top
  560. 24:05row binary that or decimal sorry that
  561. 24:07we're used to. Uh each digit position is
  562. 24:11some increasing power of 10. That's the
  563. 24:13weight for each digit position. Now that
  564. 24:16we're in base 2, each digit position,
  565. 24:19the weight of that position is some
  566. 24:21power of two. Um and let's go ahead now.
  567. 24:25Uh if you need to pause and and just run
  568. 24:28through what I explained, do that now.
  569. 24:31But let's go ahead and uh expand this
  570. 24:34out. Um so we know that uh we have 1 and
  571. 24:38then 2 to the^ of 3. So 2 cubed is 2 * 2
  572. 24:43* 2 and 2 * 2 is 4. So * 2 is 8. So we
  573. 24:48have 1 * 8. And then we have 2 ^ 2 um
  574. 24:52which is 2 * 2 which is just equal to 4.
  575. 24:56And we multiply that by 0.
  576. 24:58Um and then we have 2 to the 1st power
  577. 25:01which is the easy one. And we all know
  578. 25:02that's just two. Any number to the first
  579. 25:06power is just saying multiply that
  580. 25:08number by by itself uh once not two not
  581. 25:12two times that's squared. So we just
  582. 25:14have it once then it's just that number
  583. 25:16itself. Um and then we also said earlier
  584. 25:19that anything to the power of zero is
  585. 25:21just one. So now we have this expression
  586. 25:25and now we're this should look pretty
  587. 25:27comfortable. Uh no exponents here. So 8
  588. 25:30* 1 is 8. Uh 0 * 4 is 0. 2 * 1 is 2. And
  589. 25:371 * 1 is 1. And that's just 8 + 2 which
  590. 25:40is 10 + 1 which is 11. So and that's
  591. 25:44base that's uh base 10 11. So the the
  592. 25:48number the 11 we're comfortable with. So
  593. 25:50now I can tell you that this number 1011
  594. 25:53in binary is just 11. Um
  595. 25:57and that is a totally different way of
  596. 26:00representing a number uh that we are
  597. 26:02already familiar with. Um and this is
  598. 26:04really interesting because we only had
  599. 26:07zeros and ones to work with, but we were
  600. 26:09able to map that back to a number that
  601. 26:12we know perfectly. Uh that doesn't need
  602. 26:14to just use zeros and ones. Um so let's
  603. 26:17go ahead and look at uh one more
  604. 26:19conversion example. Um and uh let's do
  605. 26:24the number um 1 0
  606. 26:28uh 01. Um and kind of go through the
  607. 26:33same process again. So uh we know that
  608. 26:36the weighted positions let's start with
  609. 26:38that actually. Um let me make this a
  610. 26:41little bit wider so we can write on top
  611. 26:43of them. 1 0 0 1 that's base 2. And we
  612. 26:47want to know what this equals in base
  613. 26:4810. Um and so if we start from the right
  614. 26:51we know that this is the 2 to the 0ero
  615. 26:53position then this is 2 to the 1 then
  616. 26:55this is 2^ 2ar and this is 2 ^ 3. Um and
  617. 26:59that's just for our reference. Um so we
  618. 27:02can go ahead and generate this uh this
  619. 27:04long form expression where we know if we
  620. 27:07start from the very right we know that
  621. 27:09that is
  622. 27:111 * 2 0 and then we have a 0 * 2 the 1.
  623. 27:17Um and the the 0ero comes from the fact
  624. 27:21that that's what's in this digit
  625. 27:23position and that's also where the one
  626. 27:25comes from here. And then for the next
  627. 27:27digit position we have a 0 * 2 ^ of two.
  628. 27:32And then for the very last digit
  629. 27:33position we have 1 * 2 ^ of 3. Um so
  630. 27:38then we can go ahead and combine those.
  631. 27:40We add those all up. Um so this 1 0 0 1
  632. 27:44in base 2 is equal to 1 * 2 cubed + 0 *
  633. 27:502^ 2 + 0 * 2 the 1st power + 1 * 2 0
  634. 27:59power. Um, and this time, uh, I could
  635. 28:04expand this all out, but there's
  636. 28:05something interesting to note here, and
  637. 28:07that's that we have two examples of us
  638. 28:09multiplying a number by zero. And from
  639. 28:13basic numbers, uh, as you also learn in
  640. 28:16school, anything times 0 is just zero.
  641. 28:18Um, so there's no point in us doing
  642. 28:21these expansions of converting 2 ^2 to 4
  643. 28:24uh, just to multiply by zero. If we see
  644. 28:26that it's being multiplied by a zero,
  645. 28:28let's just ignore it because we know
  646. 28:29it's not going to add any any values to
  647. 28:31our final sum. And that's that that
  648. 28:33that's going to save us some time later.
  649. 28:35Uh if I gave you a super long binary
  650. 28:37number that was like say a 100 digits
  651. 28:39wide and told you to convert it um
  652. 28:42then you know this would this would be
  653. 28:44kind of uh a big time saving um for all
  654. 28:48those zeros that you could drop out. Uh
  655. 28:50so then we can simplify this expression.
  656. 28:53Uh we dropped out those zeros. We just
  657. 28:55have these two terms left. And then we
  658. 28:58know that 2 cubed is 8. So that's 1 * 8.
  659. 29:02And then uh remember that anything to
  660. 29:03the power of 0 is just 1. So then we
  661. 29:05have 8 + 1 which is 9. So and that's 9
  662. 29:10in the decimal we know. So this 1 0 0 1
  663. 29:13in base 2 is equal to 9 in the number
  664. 29:16system that we know. And this is uh
  665. 29:20really cool. Um, I remember when I first
  666. 29:23learned this, I was kind of mind blown
  667. 29:26uh at the fact that we could take these
  668. 29:29random se this seemingly random sequence
  669. 29:31of zeros and ones and then map it back
  670. 29:33to a number that we are so familiar
  671. 29:35with. Um, and
  672. 29:40that uh should make it clear how we go
  673. 29:43from binary the zeros and ones to
  674. 29:47decimal which is a number system we
  675. 29:49speak. Let's look at one other example.
  676. 29:51What if I gave you this this big number
  677. 29:54right here in binary? That's not the
  678. 29:56number 10,000. That is 1 0 0 0 in
  679. 30:00binary. But I would like to know what is
  680. 30:03this in base 10. Um and
  681. 30:07uh well, we know let's just go ahead and
  682. 30:09count our positions and then generate
  683. 30:11this expression. Um so we know our digit
  684. 30:13positions start at 0 1 2 3 and four. So
  685. 30:17we have uh that it equals 1 * 2 4 and
  686. 30:22then + 0 * 2 3 + 0 * 2 ^ 2 + 0 * 2 ^ of
  687. 30:331 + 0 * 2 ^ of 0. And from the rule we
  688. 30:38learned in the example before, whenever
  689. 30:40we're multiplying something by zero, we
  690. 30:42can just get rid of that term cuz it's
  691. 30:43going to equal zero. That's exactly
  692. 30:45what's happening in all of this
  693. 30:47expression except for this one digit
  694. 30:49position. And then we just know that uh
  695. 30:512 to the 4 is equal to 2 * 2 * 2 * 2.
  696. 30:56And that's going to be uh 2 * 2 is 4.
  697. 31:00Multiply by 2 again you get 8. And then
  698. 31:01one more time you get 16. So this number
  699. 31:04right here uh 1 0 0 0 in decimal is
  700. 31:09actually going to be equal to 16 or
  701. 31:12sorry 1 0 0 0 in binary is going to be
  702. 31:15equal to 16 in decimal um and uh luckily
  703. 31:21this is a very popular concept. So if
  704. 31:24you're feeling a little confused here um
  705. 31:27on anything I've shown that's totally
  706. 31:29fine. Just go ahead and Google it. watch
  707. 31:31another YouTube video if you need to.
  708. 31:33Um, I do not want to do more examples
  709. 31:36just because this video could drag on
  710. 31:37forever. Okay, so now we have learned up
  711. 31:42to this point how to take a binary
  712. 31:44number and translate it back into the
  713. 31:48base 10, i.e. decimal number that we're
  714. 31:51more familiar with. So, what I'd like to
  715. 31:53do now is the exact opposite process. If
  716. 31:56I have a number like 14, how can I get
  717. 32:00it to binary? And we're going to almost
  718. 32:02take the inverse of the exact process we
  719. 32:05we had before. And why are we going to
  720. 32:08learn this? It's going to be because um
  721. 32:11we're going to want to program our
  722. 32:13computer eventually. And we're going to
  723. 32:14want to be able to store numbers in our
  724. 32:17computer. And it's going to be useful
  725. 32:20then, in fact imperative that we know
  726. 32:22how to take the number we want to put
  727. 32:23into our computer and convert it to
  728. 32:25binary so we can load it into the
  729. 32:26language that the computer speaks. So uh
  730. 32:29we've been working with this number 724.
  731. 32:33So let me go ahead and give us the
  732. 32:35expanded version one more time. So we
  733. 32:37said this was 7 * 10 ^ of 2 + 2 * 10 1st
  734. 32:43power + 4 * 10th power. [snorts]
  735. 32:49And
  736. 32:51I'm also going to go ahead and bring up
  737. 32:53this binary example from above. Uh where
  738. 32:56we had let's just say 1 0 1 1.
  739. 33:00And I'm going to sub subscript this with
  740. 33:03two because it's binary. And we said
  741. 33:05this is equal to 1 * 2 to the 3 + 0 * 2
  742. 33:12^ 2 + 1 * 2 1st power plus uh sorry 1 *
  743. 33:202 to the zeroth power. [snorts] And if
  744. 33:23we notice something here um everything
  745. 33:26on our right hand side is decimal
  746. 33:30numbers the ones we're very familiar
  747. 33:31with. And we know that we take this this
  748. 33:34left-hand side which is unfamiliar to us
  749. 33:36and we're able to expand it out in a
  750. 33:39form that is actually only containing
  751. 33:41decimal numbers. [snorts]
  752. 33:44And this is the exact kind of logic
  753. 33:46we're going to take to get a number from
  754. 33:49binary to decimal. So if I have the
  755. 33:52number 724 and want to convert it to to
  756. 33:55binary then if there's a way where I
  757. 33:58could write it out in this expanded form
  758. 34:01as uh numbers zeros and 1* 2 to some
  759. 34:05power then we know we can just read the
  760. 34:08the multiplicate the multiplicative
  761. 34:10coefficients left to right to know our
  762. 34:12number. Um in this case it's easy. we
  763. 34:15have we have a one and then a zero and a
  764. 34:17one and a one and this is exactly how we
  765. 34:19end up with this number over here. So
  766. 34:22this is a little abstract um and this is
  767. 34:24probably a little more complicated than
  768. 34:27what we just learned. Uh it's my
  769. 34:29personal opinion that taking a a binary
  770. 34:32number and converting it to decimal is
  771. 34:34easier than me asking you to take a
  772. 34:37decimal number and convert it to binary.
  773. 34:39But let's go ahead and do this by
  774. 34:42working through an example. Um so let's
  775. 34:46take the number 15. 15 in base 10 i.e.
  776. 34:50just the regular 15 we're familiar with.
  777. 34:53What exactly is that in decimal or in
  778. 34:55binary? Um so question mark base 2. What
  779. 35:00we want to end up with is some
  780. 35:02expression where we start with 2 ^ 0 and
  781. 35:06work up to some power of two and
  782. 35:09multiply each of those terms by either a
  783. 35:11zero or a one. uh until we have some
  784. 35:14equivalent value that sums up to 15. And
  785. 35:19as we kind of saw earlier with this
  786. 35:20binary conversion, we only really need
  787. 35:22to care about the powers of two that are
  788. 35:24multiplied by one. I in our example
  789. 35:27here, this one uh since it's multiplied
  790. 35:29by zero, it drops out. So we kind of
  791. 35:32know that uh I'm going to move this out
  792. 35:35the way. Uh this is really just equal to
  793. 35:38uh 2 ^ 3 + 2 ^ of 1 + 2 ^ of 0. Um and
  794. 35:45I'm gonna make this interesting claim uh
  795. 35:47that any number in decimal any number
  796. 35:50that you can think of can be expressed
  797. 35:53as a sum like this. And what do I mean
  798. 35:56this? I mean a sum of powers of two. And
  799. 36:00there is a exactly one way to represent
  800. 36:03each number accordingly. Um
  801. 36:06and I can show you some examples. So if
  802. 36:08we have the number one in decimal, then
  803. 36:11we know that two to the 0ero is just
  804. 36:13one. And then let's say we have the
  805. 36:15number two in decimal. Well, this is
  806. 36:17easy as well. We know 2 to the^ of 1 is
  807. 36:19just two. Uh but let's look at let's
  808. 36:21look at h what happens when we have
  809. 36:23three. Um well, we know how to represent
  810. 36:26one and two in binary. Uh and we can
  811. 36:29just add those together because they sum
  812. 36:31to three. So we would just add their
  813. 36:32binary representations together and we'd
  814. 36:34be left with 2 1 + 2 0 and then four is
  815. 36:39easy uh because we would know that 2 ^
  816. 36:43of 2 is 4. Um and then we'll do one more
  817. 36:47which is five uh and we know that uh 5
  818. 36:52is equal to 4 + 1. So that's 2 ^ of 2 +
  819. 36:562 ^ of 0. And now uh for each of these
  820. 37:00let's uh go ahead and convert it back to
  821. 37:02binary. Um so let's start with this this
  822. 37:05five because I think it's the most
  823. 37:07interesting case. And we know that the
  824. 37:09uh we can't skip any digit positions.
  825. 37:11I.e. the rightmost digit position in our
  826. 37:13binary number has to start at 2 to the
  827. 37:150. The power has to be zero. And then we
  828. 37:18have to keep working up from there until
  829. 37:19we we have all the powers that we need
  830. 37:21in our sum. And that's the same way when
  831. 37:23you're when you're building out a number
  832. 37:24in decimal. You can't skip the 10's
  833. 37:26place or the hundreds place just because
  834. 37:28the value in that digit position is
  835. 37:29zero. You have to explicitly write a
  836. 37:32zero to make it apparent that there is
  837. 37:34no value in the 10's place and then the
  838. 37:36next value is the hundred's place. So in
  839. 37:38our example here what we would need to
  840. 37:41do is um I'm going to bring out in this
  841. 37:45long form. So we we know we have this 2
  842. 37:46to the two. We don't have any uh thing
  843. 37:49multiplied by 2 to the first power. So
  844. 37:51we do is we just leave it as a zero. We
  845. 37:53make the coefficient zero because that
  846. 37:55means it multiplies out to nothing. And
  847. 37:57then we have 1 * 2 to the 0 power. And
  848. 38:00this is the exact form from earlier
  849. 38:02where it was pretty easy from there just
  850. 38:04to read the coefficients left to right.
  851. 38:061 0 1. And now we have our decimal or
  852. 38:09our binary number. Sorry. So we took our
  853. 38:11five and uh got it to a binary
  854. 38:15representation. And if you don't believe
  855. 38:17me, well, the evidence is on the screen.
  856. 38:19But if you want to verify for yourself,
  857. 38:22pose the counter question. What is the
  858. 38:25decimal representation of the value of
  859. 38:27the binary value 101? And if you expand
  860. 38:30it out like I've already done, you're
  861. 38:31just going to get five. And uh let's do
  862. 38:34this for the example of 42. So we have
  863. 38:37all we have is 2 ^2. But we know we have
  864. 38:40to get all the way back down to the
  865. 38:41power of zero. 2 to the^ of 0. we just
  866. 38:44add in 0 * each of those terms. So we
  867. 38:46have 1 * 2 ^ of 2 and then we don't want
  868. 38:49to add anything else. So we just do 0
  869. 38:51for the first power of two and zero for
  870. 38:54the zeroth power of two. And then we
  871. 38:56know we just read these coefficients
  872. 38:58left to right which is 1 0 0. So that's
  873. 39:01how we represent four in binary. And in
  874. 39:05our example here, we have it easy cuz we
  875. 39:08have every power of two uh in the entire
  876. 39:12range included. So we know that's 1 * 2
  877. 39:14^ of 1 + 1 * 2 ^ of 0. And that's just
  878. 39:18going to be equal to 1 1 in binary. And
  879. 39:21we'll just go ahead and finish out what
  880. 39:22we have here. Uh we know that 2 ^ of 1
  881. 39:26is equal to 1 * 2 ^ of 1. Then we don't
  882. 39:28want to add anything else. We just
  883. 39:30multiply the remaining terms by zero.
  884. 39:32And there's only one remaining term for
  885. 39:34us to get back to the power of zero. So,
  886. 39:35we're just left with one zero. Uh, and
  887. 39:37that's supposed to be subscripted with
  888. 39:39the two.
  889. 39:41And last but not least, uh, this one's
  890. 39:43trivial. Um, we've already worked our
  891. 39:46way back down to 2 to the^ of 0. So, we
  892. 39:49just have that one term and that's just
  893. 39:51equal to one. Um, so hopefully you
  894. 39:54notice a little bit of a pattern here.
  895. 39:55uh where I have been able to convince
  896. 39:57you at least that the first five numbers
  897. 39:59in decimal are are uh able to be
  898. 40:04expressed as a sum of twos raised to
  899. 40:09various powers. And if we can figure out
  900. 40:12for any number what that sum is, then
  901. 40:14I've shown you it's pretty easy once you
  902. 40:16have that to expand it out and then get
  903. 40:18the binary representation.
  904. 40:20[snorts]
  905. 40:21So let's try a uh a little bit more
  906. 40:23difficult example. Um uh let's try 13.
  907. 40:29And
  908. 40:30>> what we're going to do is we want to
  909. 40:32find the sums of twos to various powers
  910. 40:36that sum up to 13. And we're going to do
  911. 40:38this by starting uh with the biggest
  912. 40:41power of two that is that can uh
  913. 40:43contribute to our sum. So uh I'll go
  914. 40:46ahead and and write this table out over
  915. 40:48here. We know 2 to the^ of 0 is one. 2
  916. 40:52to the^ of one is two. Then 2^2 is 4. 2
  917. 40:57cubed is 8. 2 to the 4 is 16. 2 5th is
  918. 41:0232. And I'll do one more. 2 6 is equal
  919. 41:06to 64.
  920. 41:08And what we want to do is just start
  921. 41:11with the biggest uh x such that 2 to the
  922. 41:14x is less than our target value of 13.
  923. 41:17And that's going to happen uh right
  924. 41:20here. 2 ^ of 3 is 8 which is the largest
  925. 41:23value uh that is less than or equal to
  926. 41:27our target value. So we know that uh 2
  927. 41:30cubed is going to be in this expression
  928. 41:32and then when we have uh we kind of have
  929. 41:36um 13 on this side and we have eight on
  930. 41:40this side and so that means we need to
  931. 41:41get five with some remaining
  932. 41:43combinations of entries on this list. So
  933. 41:47we'll do the same process for five. And
  934. 41:49if we look at this list, uh we know that
  935. 41:52one is less than five, 2 is less than
  936. 41:54five, four is less than five, 8 is not
  937. 41:56less than five. So this two 2^2 is the
  938. 42:01biggest power of two, which is less than
  939. 42:04or equal to our remainder, which is
  940. 42:05five. Our remaining sum, should I say.
  941. 42:08So then I'll add our two squared in. And
  942. 42:10now we have a four. And so we know that
  943. 42:1313, we have 13 on this left side and we
  944. 42:15need to get 13 on the right hand side or
  945. 42:17else they're not equal. And we have 12
  946. 42:19on the right hand side. So we just need
  947. 42:20to get one more. And that's easy enough
  948. 42:23because we know that 2 to the^ of 0 is
  949. 42:25one. So I'll just add that in. And now
  950. 42:29we've uh done the exact task that we we
  951. 42:32we said was the difficult part, which is
  952. 42:34expressing our target number of 13 as a
  953. 42:37sum of various powers of two. So now
  954. 42:40let's go ahead and uh I should have
  955. 42:42demarcated this with a base 10. So you
  956. 42:44know I'm working with the decimal
  957. 42:45number. Let's go ahead and and bring
  958. 42:47this out to the expanded form where we
  959. 42:48add in the ones and zeros and the
  960. 42:50missing powers of two. So we would know
  961. 42:53we have 1 * 2 ^ 3 + 1 * 2 ^ of 2. We
  962. 42:58don't have any term with 2 ^ of 1. So we
  963. 43:01multiply it by zero. So it doesn't
  964. 43:02actually add anything. And then lastly
  965. 43:04we do have 2 to the^ of zero. So we will
  966. 43:06put a one there. And from there, it's
  967. 43:09pretty easy. We just read left to right
  968. 43:11what the coefficients are. And we have
  969. 43:14uh our binary equivalent. So 13, the
  970. 43:18decimal version of 13, the number that
  971. 43:19you and I know is 13 is equal to 1101 in
  972. 43:22binary. And you can expand this out,
  973. 43:25which is kind of what we have right here
  974. 43:27to prove that in fact uh I am not lying
  975. 43:29to you. It is 13. So let's try one more
  976. 43:33example of that just because I think
  977. 43:34it's a little complicated. Um,
  978. 43:38let's go ahead and do uh let's say 22. I
  979. 43:43just picked it off the top of my head.
  980. 43:44Hopefully it works out to be somewhat
  981. 43:46pretty. Um, so we have to find the uh
  982. 43:50the sums of powers of two again. And I'm
  983. 43:53going to go a little bit faster this
  984. 43:54time. So I'm just going to look at our
  985. 43:55list and I can see that 16 is the
  986. 43:58biggest value here that is is less than
  987. 44:00or equal to 22. So I know we have a two
  988. 44:02to the four here and let's keep things
  989. 44:04easy. So, I have 22 = 16 + uh we know
  990. 44:09we're going to need something else cuz
  991. 44:10the left hand side does not equal the
  992. 44:12right hand side. And 22 - 16 is 6. So,
  993. 44:16we're trying to get a six now. Um, and
  994. 44:19four is the next candidate here in terms
  995. 44:21of largest power of two that doesn't
  996. 44:23exceed our target of six. So, I'll add a
  997. 44:26two squared into our sum and that's
  998. 44:28going to be equal to four. And now we
  999. 44:31have 22 on the left hand side and 20 on
  1000. 44:33the right hand side. So all we need is
  1001. 44:35to get two uh to our expression. And
  1002. 44:38that's easy. We know 2 to the^ of 1 is
  1003. 44:40just two. And now we know that the 22 is
  1004. 44:44equal to 22. So we've we've con we can
  1005. 44:46convince ourselves that this uh
  1006. 44:48left-hand expression here is equal to
  1007. 44:51the right hand side of the expression.
  1008. 44:53And so then what we can do is we can go
  1009. 44:55ahead and write this out in the expanded
  1010. 44:56form which you should be comfortable
  1011. 44:58with now hopefully or it should at least
  1012. 45:00be becoming apparent to you. And that's
  1013. 45:02equal to 1 * 2 ^ 4. There's no 2 3. So
  1014. 45:06we just write a zero as the coefficient
  1015. 45:08because it multiplies away. Uh and
  1016. 45:10that's because anything multiplied by
  1017. 45:120ero is just zero. So this this term we
  1018. 45:14just added contributes nothing to our
  1019. 45:16sum. And then we have a 2 ^2. So we'll
  1020. 45:19add the 2^2 in. And we have a 2 to the^
  1021. 45:22of 1. And we do not have a 2 to the^ of
  1022. 45:240. But we need to go all the way down
  1023. 45:26from the largest power of two, which is
  1024. 45:28four, all the way down to zero. So we
  1025. 45:31stop like this. And then we can we can
  1026. 45:35take these coefficients and just read
  1027. 45:36them left to right. So that's 1 0 1 1 0.
  1028. 45:41And that means that 22 in decimal is
  1029. 45:44equal to the binary number 1 0 1 1 0.
  1030. 45:49Cool.
  1031. 45:50So, you've now uh gotten a pretty high
  1032. 45:55level but also complete understanding of
  1033. 45:57what binary is. I've shown you how to
  1034. 46:01take a binary number, convert it to
  1035. 46:03decimal so you can make sense of it and
  1036. 46:06also how to take a decimal number and
  1037. 46:07convert it to binary. And there is one
  1038. 46:10final thing that we will want to do and
  1039. 46:13be able to understand about binary
  1040. 46:14numbers and that is how do we add them?
  1041. 46:17And I talked a little bit about this
  1042. 46:19earlier when we we talked about decimal,
  1043. 46:21but let's go ahead and and recall our
  1044. 46:23decimal example. Let's say I have 74 +
  1045. 46:2839.
  1046. 46:30Well, we keep we start at the rightmost
  1047. 46:33digit position and we keep adding the
  1048. 46:37values in the corresponding digit
  1049. 46:39position. So, we'd add this 49 together
  1050. 46:41and we'd write the sum of them below.
  1051. 46:44So, let me actually pick a better
  1052. 46:45example. Uh let's do uh 782
  1053. 46:52plus um 531.
  1054. 46:57So in our example, we'd start with this
  1055. 46:58rightmost digit position. And we just
  1056. 47:00know 2+ 1 is 3. So we write it down
  1057. 47:03here. And we move on to the next digit
  1058. 47:04position, which is 8 + 3. And 8 + 3 is
  1059. 47:0811. Which you should remember this means
  1060. 47:10we have carryover. So we put the
  1061. 47:12remainder, which is 1, down here. And
  1062. 47:16then we carry over a one up here. And
  1063. 47:18then now when we get to the next digit
  1064. 47:20position, we add 7 + 5 which is 12. But
  1065. 47:23we also have to factor in this carryover
  1066. 47:25which makes it 13. And that's exactly
  1067. 47:28what we do. So for decimal, what we did
  1068. 47:32is we started at the rightmost digit
  1069. 47:33position and for each digit position, we
  1070. 47:37added the corresponding numbers for a
  1071. 47:39and b. If I'm letting this be A and this
  1072. 47:41be B, our two operands, and I'm going
  1073. 47:43from right to left until we've added all
  1074. 47:45of the digit positions together, and you
  1075. 47:47have your sum. And it turns out binary
  1076. 47:50is going to be
  1077. 47:52exactly identical to this. So, um let's
  1078. 47:56see, we have our 22 and we also learned
  1079. 48:00that uh let's take 22 and add uh another
  1080. 48:05random number to it and see what the
  1081. 48:07result is. So, I'm going to take 22,
  1082. 48:08which we know is 1 0 1 1 0. And let me
  1083. 48:12pick another number. Uh, so we don't
  1084. 48:14have to reconvince ourselves that this
  1085. 48:15is true. Um, and I'm going to come all
  1086. 48:18the way back up here. And we said that,
  1087. 48:22uh, 1 0 0 1 is equal to 9. So, let's do
  1088. 48:28this. 1 0 0 1 is equal to 9. So,
  1089. 48:32intuitively, we're adding 22 + 9. And
  1090. 48:35these are both the decimal numbers we're
  1091. 48:36so comfortable with. We know that's
  1092. 48:38equal to 31. Uh and you know, maybe you
  1093. 48:41have to to verify that yourself, but you
  1094. 48:43can use your calculator here. Um so
  1095. 48:45we're going to start right to left. And
  1096. 48:48before I start this, you might have a
  1097. 48:49quick question. And that's uh the two
  1098. 48:52operands we have, this a and b, this 22
  1099. 48:55and 9, do not have the same number of
  1100. 48:57digit positions. So what do we do here?
  1101. 49:01And there's something beautiful you
  1102. 49:03learn that carries over and is just as
  1103. 49:05true now. So let's go back to our
  1104. 49:06decimal word for our decimal world for a
  1105. 49:08second. And if I gave you the number 762
  1106. 49:12arbitrarily chosen, then you know that
  1107. 49:16this is equal to the same number as
  1108. 49:180762,
  1109. 49:20which is equal to the same number as
  1110. 49:2100762.
  1111. 49:23And more generally, uh any amount of
  1112. 49:26zeros before our number are equal to the
  1113. 49:28original number. And that's because we
  1114. 49:30just ignore ignore any leading zeros.
  1115. 49:34And why is this uh why can we do this if
  1116. 49:37that's not clear? Um and that's because
  1117. 49:40if we expand this 762 in our exponent
  1118. 49:43way, we know we have 7 * 10 2 power + 6
  1119. 49:47* 10 1st power + 2 * 10 to the 0 power.
  1120. 49:52And if we add any number of twos, all
  1121. 49:55we're doing is adding 0 * 10 to some
  1122. 49:58increasing power. And we already
  1123. 50:01discussed why 0 * any number is equal to
  1124. 50:03zero. So this 0 * 10 the cubed is just
  1125. 50:07going to drop out. And if we had the
  1126. 50:08same thing again where we had a 0 * 10
  1127. 50:104, that's just going to drop out. So any
  1128. 50:13amount of leading zeros actually has no
  1129. 50:16impact on the total value of our number.
  1130. 50:19And so what I'm trying to get at and
  1131. 50:20took a long way of explaining is that
  1132. 50:22you're able to add number you're able to
  1133. 50:24add zeros to the start of a number
  1134. 50:25without changing its value. So what I'm
  1135. 50:28going to say in binary is that if we
  1136. 50:30have this number in binary, it's going
  1137. 50:32to be the equivalent of this number in
  1138. 50:35binary where we add one one or one zero.
  1139. 50:38Or we could add any number of zeros
  1140. 50:41and it's going to be the equivalent
  1141. 50:43value in binary. So, I'm just going to
  1142. 50:45go ahead and take that logic and
  1143. 50:48make our two operants in this addition
  1144. 50:50problem uh have the same number of digit
  1145. 50:55positions. Just like that, I added a
  1146. 50:58zero to the front of our number, but did
  1147. 51:00that does not change the uh numerical
  1148. 51:03interpretation of that value.
  1149. 51:06So,
  1150. 51:10going to take a sip of water here. Let's
  1151. 51:12go ahead and add these two numbers
  1152. 51:13together then. So we have a 0 and a 1,
  1153. 51:18which 0 + 1 is 1. And then we have 1
  1154. 51:20plus 0, which is 1. 1 + 0, which is 1. 1
  1155. 51:24+ 0, which is 1. And 1 plus 0, which is
  1156. 51:271. Um, and this is not that interesting.
  1157. 51:32Uh, we didn't actually see carryover, so
  1158. 51:34I kind of picked a bad example in
  1159. 51:35hindsight, but we'll do another. Don't
  1160. 51:37worry. Um I am going to claim that this
  1161. 51:39number in binary plus this number in
  1162. 51:41binary equals this number in binary. Um
  1163. 51:44and our if we did everything right, we
  1164. 51:46should hope that this number is actually
  1165. 51:48equal to 31. So let's go ahead and uh
  1166. 51:50convince ourselves that that's the case.
  1167. 51:52So uh we start with 2 ^ of 0 1 2 3 4. So
  1168. 51:57we have 1 * 2 ^ 4 + 1 * 2 ^ of 3 + 1 * 2
  1169. 52:04^ of 2 + 1 * 2 1st power + 1 * 2 to the
  1170. 52:09zero power. And that's cuz we have one
  1171. 52:10in all these digit positions which is
  1172. 52:12just equal to 2 4 which is 16 + 8 + 4 +
  1173. 52:192 + 1. And 16 + 8 is 24 + 4 is 28 + 2 is
  1174. 52:2630 + 1 is 31. And uh sure enough this is
  1175. 52:31the exact result we were expecting.
  1176. 52:34Um but what if I wanted to give a more
  1177. 52:36nuanced example uh one where we had
  1178. 52:39carryover. Well um let's do exactly
  1179. 52:42that. Let's go ahead and do 101
  1180. 52:46plus one one. Um, and I will go ahead
  1181. 52:50and spill this out for you that this is
  1182. 52:54uh five
  1183. 52:56plus
  1184. 52:597. Um, and we would hope that the result
  1185. 53:02is then 12. And it'd be a good exercise
  1186. 53:05for you probably to pause this video and
  1187. 53:07convince yourself that those two numbers
  1188. 53:09are uh five and seven. But uh we we need
  1189. 53:13not do that for the sake of time. So,
  1190. 53:15uh, we add this 1 + 1 and we may be
  1191. 53:18convinced to write a two down here cuz,
  1192. 53:20you know, there's no, it doesn't sum the
  1193. 53:22more than 10. We don't carry over. But
  1194. 53:24remember, in in binary, we only have
  1195. 53:26zeros and ones to work with. So, versus
  1196. 53:29decimal where we carry over when we're
  1197. 53:31going from 9 back to to zero to 10. Uh,
  1198. 53:35in binary, we carry over uh when we're
  1199. 53:38going from one to two, i.e. we roll it
  1200. 53:40back to a zero. So, we put this
  1201. 53:42remainder down here. um we put a zero
  1202. 53:45down below and then and then carry a one
  1203. 53:48over and now we have 1 plus zero which
  1204. 53:51is one but we have another one that we
  1205. 53:52have to add the carryover and we're at
  1206. 53:54two and we have the same issue as
  1207. 53:56before. So we put the the carryover we
  1208. 53:59put a zero below and then carry over
  1209. 54:00once again. Um
  1210. 54:03>> and now we have 1 plus 1 + 1 which is
  1211. 54:07three. And we're going to go ahead and
  1212. 54:10it's it's interesting. We we put the
  1213. 54:12remainder uh when we modulo by two. So 3
  1214. 54:17/ 2, the remainder is just then a one
  1215. 54:21and then we carry over a one here. And
  1216. 54:25uh this is our final result. And uh this
  1217. 54:28is actually equal to the bi to the uh
  1218. 54:30number 12. You can verify this yourself
  1219. 54:32for it to be true. So basically when
  1220. 54:35we're adding binary numbers, if we have
  1221. 54:37a zero and a one in a position, it
  1222. 54:40becomes zero. If we have a one and a
  1223. 54:42zero in a position, sorry, I misspoke.
  1224. 54:44If we have a zero and a one, it's a one.
  1225. 54:46A one and a zero, it's a one. A zero and
  1226. 54:49a zero sums to a zero. And then if you
  1227. 54:52have a one and a one, we know this is
  1228. 54:54binary two. And so that means we put a
  1229. 54:57zero over here because the two divided
  1230. 54:59by two, there's no remainder. And so we
  1231. 55:01just carry over a one. And then the uh
  1232. 55:04interesting case that we can't
  1233. 55:05generalize from the remainder of these
  1234. 55:06is if we have a one and a one and a one
  1235. 55:08and that means we have three and we know
  1236. 55:10that the binary representation of three
  1237. 55:12is one one but what we do uh to get that
  1238. 55:15if you don't understand is uh we have
  1239. 55:18the three we take the remainder when you
  1240. 55:20divide it by two 3id two has a remainder
  1241. 55:22of one so we put the one below and then
  1242. 55:24we uh carry over the extra one and uh
  1243. 55:27these are kind of the four patterns you
  1244. 55:30need to understand um when you're adding
  1245. 55:33binary numbers.
  1246. 55:35Awesome. Um and this is now uh has
  1247. 55:41become clear how you can take binary
  1248. 55:43numbers and convert between decimal
  1249. 55:47either binary to decimal or decimal to
  1250. 55:49binary and how you can add binary
  1251. 55:52numbers together. Okay. So now you have
  1252. 55:55uh learned all but one of the
  1253. 55:58fundamental tricks of binary. So, we
  1254. 56:00learn how to go from decimal to binary.
  1255. 56:04Then we learn how to go from binary to
  1256. 56:06decimal.
  1257. 56:08And then we learn how to add binary.
  1258. 56:13But we have yet to learn how to subtract
  1259. 56:15binary.
  1260. 56:19And that is the last thing we need to do
  1261. 56:22before we have mastered binary.
  1262. 56:24So what we would like to understand is
  1263. 56:29how can we subtract two numbers in
  1264. 56:30binary and it does follow a very similar
  1265. 56:34process to the subtraction that we know.
  1266. 56:36But it turns out there's a better way to
  1267. 56:37do it. But before we can even look at
  1268. 56:39subtraction, you may have another
  1269. 56:41question that you wondered at some
  1270. 56:42point. And that's how do we represent
  1271. 56:44negative numbers?
  1272. 56:46And there is a multitude of ways for us
  1273. 56:50to represent negative numbers, but the
  1274. 56:52one we're going to go with is a very
  1275. 56:53popular one called TW's complement.
  1276. 57:02[snorts] And what this is is a very
  1277. 57:05simple rule where all you do is
  1278. 57:09interpret the leftmost digit in your
  1279. 57:12binary number to have
  1280. 57:16a negative waiting. Now, this is
  1281. 57:18confusing and it's going to require some
  1282. 57:20digestion,
  1283. 57:22but what we need to agree on now uh is
  1284. 57:25how many digits each of our binary
  1285. 57:27numbers are going to have. So, in
  1286. 57:29decimal we have uh say the number 7 or
  1287. 57:32it could be 656
  1288. 57:34or 10,432
  1289. 57:37and this has one digit, this has three
  1290. 57:40digits and this has five digits. And we
  1291. 57:42also saw some binary examples. uh we had
  1292. 57:451 one which we saw was three and that's
  1293. 57:48two digits and we had something more
  1294. 57:50complicated uh where we could have like
  1295. 57:52a 1 0 1 0 1 and that's going to be five
  1296. 57:54digits. Um so binary and decimal have
  1297. 57:57this property in common where you could
  1298. 57:59represent a number and it could take any
  1299. 58:02number of digits to represent that
  1300. 58:04number. Um for two's complement it gets
  1301. 58:08a little tricky with this variable
  1302. 58:09notation. So, we actually want to agree
  1303. 58:12on a fixed width for every number. And
  1304. 58:16this may seem like a restriction, and it
  1305. 58:18is. Um, computers uh aren't able just to
  1306. 58:21arbitrarily represent any digit of any
  1307. 58:24length. Um, and this is one of the
  1308. 58:26design choices that will become more
  1309. 58:27apparent why that's the case as we
  1310. 58:29design our computer. But for now uh what
  1311. 58:32I would like to do is let's just make
  1312. 58:34this assumption or write this on the
  1313. 58:36screen that every number in binary. So
  1314. 58:40every binary number
  1315. 58:46uh shall use
  1316. 58:50four digits.
  1317. 58:53So if our number that we're trying to
  1318. 58:55represent in binary is less than four
  1319. 58:57digits then we just pad it with zeros.
  1320. 58:59Okay, that's easy. Uh, but what if we
  1321. 59:01have a number that we want to represent
  1322. 59:03that's more than five digits? We saw
  1323. 59:04some examples earlier. Sorry, more than
  1324. 59:06four digits. Um, we saw some examples of
  1325. 59:08these numbers earlier and we're actually
  1326. 59:10just going to say our computer is unable
  1327. 59:12to represent them. We have a upper bound
  1328. 59:15uh on the range of values we can
  1329. 59:18represent. Um, and that's true. If if in
  1330. 59:21decimal I said you can only use three
  1331. 59:22digits. um then the largest number you
  1332. 59:26could represent is 999 and the smallest
  1333. 59:29you could represent is zero and that
  1334. 59:32means there's actually 1,000 distinct
  1335. 59:35values you can represent in this range.
  1336. 59:37Um,
  1337. 59:39and we're going to make the same
  1338. 59:41restriction on binary. And I'm going to
  1339. 59:43start this off with four. Uh, just so we
  1340. 59:46can learn how to do this choose
  1341. 59:48complement. And it turns out in our
  1342. 59:50computer, uh, we're going to use eight.
  1343. 59:53In our computer, we're going to restrict
  1344. 59:54every number in binary that we represent
  1345. 59:57to be exactly eight digits. And this is
  1346. 59:59just one of many design choices that a
  1347. 1:00:02uh, implementer of a computer has at
  1348. 1:00:04their liberty. So, more modern
  1349. 1:00:06computers, you may have heard of 32bit
  1350. 1:00:08or 64-bit computers. Um, those are two
  1351. 1:00:11very popular representations. Uh, and
  1352. 1:00:14that is kind of the same rule. If you've
  1353. 1:00:16heard those numbers before, what that
  1354. 1:00:17means is internally that computer is you
  1355. 1:00:21can say representing every number is
  1356. 1:00:23exactly either 32 or 64 bits. Um, you
  1357. 1:00:28may be wondering, hey, this is kind of a
  1358. 1:00:30a pretty tight restriction. Like with
  1359. 1:00:31four binary digits, we can't represent
  1360. 1:00:34that many numbers. Um, and it's true.
  1361. 1:00:36With 32 or 64, however, you are able to
  1362. 1:00:39represent very large numbers and numbers
  1363. 1:00:43larger than most people need to
  1364. 1:00:45represent for computational purposes.
  1365. 1:00:47And there are actually uh techniques
  1366. 1:00:49that we can use to get around this to
  1367. 1:00:50represent even larger numbers. Um, but
  1368. 1:00:53those are uh implemented at a higher
  1369. 1:00:55level. So I'm not going to say that each
  1370. 1:00:59binary number shall use four digits. Um
  1371. 1:01:02so uh we can't represent the number in
  1372. 1:01:05our scheme 1 011 cuz that's five digits.
  1373. 1:01:08Um cool. So I uh said uh we have this
  1374. 1:01:14number one 011 and we know this is equal
  1375. 1:01:18to let's just I picked this number out
  1376. 1:01:19of nowhere. It's a four-digit binary
  1377. 1:01:21number. We can expand this out as 1 * 2
  1378. 1:01:25^ of 3 + 0 * 2 ^ of 2 + 1 * 2 ^ of 1 + 1
  1379. 1:01:35plus sorry 1 * 2 ^ of 0 and what 2's
  1380. 1:01:40complement says is we take the most
  1381. 1:01:43significant digit position i.e. the left
  1382. 1:01:45one and its weight is multiplied by
  1383. 1:01:48negative one. So now instead of this
  1384. 1:01:50being a positive 2 cubed, instead of
  1385. 1:01:53this digit having weight 8, it's
  1386. 1:01:55actually going to have weightg.
  1387. 1:01:58So what this means is we have uh this -1
  1388. 1:02:03* whatever is inside our representation
  1389. 1:02:06of that digit position. Um + 0 * 2^ 2 +
  1390. 1:02:111 * 2 ^ of 1 + 1 * 2 ^ of 0. And so that
  1391. 1:02:16means that we have8
  1392. 1:02:18+ 0 + 2 + 1. And that means we have -8 +
  1393. 1:02:272 + 1. Sorry, I don't know why I wrote a
  1394. 1:02:29six. And that's going to be equal to -5.
  1395. 1:02:33Um, and this is should be a little bit
  1396. 1:02:35confusing because you're you should be
  1397. 1:02:36wondering, hey, why isn't this equal to
  1398. 1:02:3811? And why did we choose it to be like
  1399. 1:02:41this? Um so those are two loaded
  1400. 1:02:45questions. So I want to make sure
  1401. 1:02:47understand first is how we can interpret
  1402. 1:02:49numbers in two's complement. Um and that
  1403. 1:02:52is that this uh most significant digit
  1404. 1:02:55position is going to have weight
  1405. 1:02:56negative uh negative value. And this
  1406. 1:03:01actually means that uh for our
  1407. 1:03:02four-digit values, whenever we have a
  1408. 1:03:05one in this most significant digit
  1409. 1:03:06position, the leftmost one, our number
  1410. 1:03:08is going to be negative. Um cuz this has
  1411. 1:03:11the most weight and uh these all have
  1412. 1:03:14less weight than it. So as long as this
  1413. 1:03:16one's this leftmost digits a one, that
  1414. 1:03:18means our number is negative. Um and
  1415. 1:03:21then similarly if that's a zero then we
  1416. 1:03:24have the original logic before uh where
  1417. 1:03:26we're adding sums of weights of twos
  1418. 1:03:29that are multiplied by either one or
  1419. 1:03:30zero and so that means the number must
  1420. 1:03:32be positive. Um
  1421. 1:03:37so let's do one more example. Uh let's
  1422. 1:03:40say I had one one one. So we have
  1423. 1:03:47- 1 for this leftmost digit position
  1424. 1:03:49time 2 ^ of 3 and then we have + 2^2 + 2
  1425. 1:03:55the 1 + 2 0 and so that is -8 + 4 + 2 +
  1426. 1:04:021 and that's going to be equal to uh -1
  1427. 1:04:07if we sum these all up. And so this is
  1428. 1:04:09interesting because you know in in what
  1429. 1:04:11we learned about less than an hour ago
  1430. 1:04:13we had uh this would be equal to uh 8 +
  1431. 1:04:174 which is 12 + 2 which is 14 + 1 which
  1432. 1:04:19is 15. Uh this should be 15 but now it's
  1433. 1:04:21negative 1. And that's a little bit
  1434. 1:04:23confusing. Um but this is actually how
  1435. 1:04:26we're going to build our computer. So
  1436. 1:04:28you should make sure this is clear to
  1437. 1:04:30you. This isn't something I would
  1438. 1:04:31recommend skipping over. uh our computer
  1439. 1:04:34is going to use two's complement which
  1440. 1:04:36is the exact binary we just learned with
  1441. 1:04:38this one caveat that
  1442. 1:04:42uh any weighted value i.e. The leftmost
  1443. 1:04:46digit position has weight negative and
  1444. 1:04:50this lets us represent negative numbers.
  1445. 1:04:52Uh we just saw how to represent negative
  1446. 1:04:54one. Now so what I'd like to show next
  1447. 1:04:57uh well I will tell you that addition
  1448. 1:05:01works out the exact same. So let's say I
  1449. 1:05:04had -1 and I want to add -2. So uh if I
  1450. 1:05:12wanted to or let's say we have uh -5 and
  1451. 1:05:16negative 1 and we want to add those
  1452. 1:05:17together then we have -5 + -1 which is
  1453. 1:05:21equal to -6 and we know these binary
  1454. 1:05:24representations are 1 0 1 1 and then 1
  1455. 1:05:28one1. So we can do the same arithmetic
  1456. 1:05:31as before. So 1 + 1 we put a zero below
  1457. 1:05:34and carry a one over here. Then 11 one
  1458. 1:05:36one we have a one below and a one
  1459. 1:05:39carries over. And then one 0 one we have
  1460. 1:05:41a zero below and a one carries over. And
  1461. 1:05:46uh then we have 111 which is you know
  1462. 1:05:48just three 1. Um and we've kind of made
  1463. 1:05:52a interesting remark. Uh let's go ahead
  1464. 1:05:55and ignore
  1465. 1:05:59this leftmost digit position because we
  1466. 1:06:00said we're only working with four bit
  1467. 1:06:02values now. Fourdigit values. And let's
  1468. 1:06:04figure out what 1 0 1 0 in two's
  1469. 1:06:07complement binary equals. And that's
  1470. 1:06:09going to be equal -2 ^ 3 plus we skip 2
  1471. 1:06:15^ of two. We do have 2 the^ of 1 and we
  1472. 1:06:18skip 2 to the^ of 0. And that's going to
  1473. 1:06:20be equal to8 + 2 which is just equal to
  1474. 1:06:23-6. And so that's that's that's
  1475. 1:06:26beautiful. We had a -5 here ag -1. We
  1476. 1:06:30added them together and we still got the
  1477. 1:06:32-6.
  1478. 1:06:34This does mean however that there are uh
  1479. 1:06:38some binary values that fit in four
  1480. 1:06:42digits but this if we add them together
  1481. 1:06:44it can't actually fit in four digits. So
  1482. 1:06:47let's let's let's look at an example of
  1483. 1:06:48that. Let's say I have the binary number
  1484. 1:06:51011 1 and I will tell you that this is 1
  1485. 1:06:54+ 2 + 4 which is 7. So we're doing 7 + 7
  1486. 1:06:58and we would expect 14 to come out. So
  1487. 1:07:01let's do the arithmetic. 11 1 is zero.
  1488. 1:07:0411 one is one with a carryover. 11 one
  1489. 1:07:07is one with a carryover. And then 1 0 0
  1490. 1:07:10is just one.
  1491. 1:07:12Um and this should equal 14. But if we
  1492. 1:07:16actually expand this out, uh we well we
  1493. 1:07:19already know there's some danger because
  1494. 1:07:20this leftmost bit is this leftmost digit
  1495. 1:07:23is a one. Which means as I said earlier
  1496. 1:07:26that the number is actually negative. We
  1497. 1:07:28know this can't be the case because 14's
  1498. 1:07:29positive. But let's go ahead and and
  1499. 1:07:31expand it out. Uh we have -2 cubed + 2^
  1500. 1:07:362 + 2 1 and that's -8 + 4 + 2
  1501. 1:07:44and that's going to be equal to -2 which
  1502. 1:07:47is not equal to 14.
  1503. 1:07:49So I'm going to take a sip of water
  1504. 1:07:50here.
  1505. 1:07:57This is interesting.
  1506. 1:08:01Because we have this two's complement
  1507. 1:08:03rule, there are actually ranges of the
  1508. 1:08:07values we can represent in terms of four
  1509. 1:08:11binary digits
  1510. 1:08:14and it's just a limitation and it's
  1511. 1:08:17something we're have to be careful of
  1512. 1:08:18when we're designing our computer. Our
  1513. 1:08:20computer which I said will operate on
  1514. 1:08:228bit values, eightdigit binary values.
  1515. 1:08:26If we add two numbers that individually
  1516. 1:08:30may be able to be represented in eight
  1517. 1:08:32digits, uh it's possible their sums not
  1518. 1:08:34able to be represented in eight digits.
  1519. 1:08:36And we have this issue and this is
  1520. 1:08:37called overflowing computers. You may
  1521. 1:08:40have heard of it before. Um but it it
  1522. 1:08:43means that we can't represent a number
  1523. 1:08:45in the number of digit positions that we
  1524. 1:08:47have. And that's just something to be
  1525. 1:08:49aware of. um I will try ignore all of
  1526. 1:08:52those cases and and hide things away so
  1527. 1:08:54that we we don't have to worry about it
  1528. 1:08:56because I don't want to get too
  1529. 1:08:57technical here. But uh I just wanted to
  1530. 1:09:01make it something that we aware of. And
  1531. 1:09:03then there's one other really
  1532. 1:09:04interesting uh property of two's
  1533. 1:09:07complement and that is to negate a
  1534. 1:09:10number.
  1535. 1:09:14So uh in in in decimal it's easy. If we
  1536. 1:09:17have 10 and we want to negate it, we
  1537. 1:09:18just write -10. And if we have -6 and
  1538. 1:09:21and we want to negate it, we just write
  1539. 1:09:23six. And it's easy because we just have
  1540. 1:09:25this negative sign that we can just
  1541. 1:09:26throw in front of our number. And we are
  1542. 1:09:29learning that you can't do the same in
  1543. 1:09:31binary. There is no just negative sign
  1544. 1:09:34we can throw on cuz we don't have a
  1545. 1:09:35negative sign. All we have is zeros and
  1546. 1:09:37ones to work with. And I mean that
  1547. 1:09:39literally.
  1548. 1:09:40So there is a really cool property. You
  1549. 1:09:43do not need to understand why this
  1550. 1:09:44works, but it turns out to negate a
  1551. 1:09:47number, you invert you invert
  1552. 1:09:52all the digit positions
  1553. 1:09:59and then you add one to the result.
  1554. 1:10:05So, uh let's come up here. Um we had
  1555. 1:10:09seven which we know is is actually let's
  1556. 1:10:11pick let's pick this example 1 0 1 0 we
  1557. 1:10:13know is -6. So -6 in in decimal is 1 0 1
  1558. 1:10:180 uh in this two's complement. And uh we
  1559. 1:10:21want to figure out what the negated
  1560. 1:10:23value is we know it's six but how do we
  1561. 1:10:25get that now? And what I'm saying is
  1562. 1:10:27step one is you invert all the digit
  1563. 1:10:29positions. So your ones become zeros and
  1564. 1:10:32your zeros become ones. So we end up
  1565. 1:10:34with one becomes 0, a 0 becomes a 1, a
  1566. 1:10:36one becomes a zero, and a zero becomes a
  1567. 1:10:39one and then we add one to the result.
  1568. 1:10:42So we have 0 1 0 1 and we just add one.
  1569. 1:10:46And this is the binary addition we
  1570. 1:10:48already learned how to do. So we know a
  1571. 1:10:49one and a one is a zero with a
  1572. 1:10:51carryover. And then we have 1 0 0 which
  1573. 1:10:53is 1, 1 0 which is 1 and 0 0 which is 0.
  1574. 1:10:57And this should be the negation of this.
  1575. 1:11:01And we know this is equal to -6. So this
  1576. 1:11:03should equal six. And let's go ahead and
  1577. 1:11:05see if that's true. We have 2 to the^ of
  1578. 1:11:08two, 2 to the^ of 1, and then nothing
  1579. 1:11:10for 2 to the^ of 0, uh, which is 4 + 2,
  1580. 1:11:13which is six. And so that's that's
  1581. 1:11:16really cool. Um, so I showed you how to
  1582. 1:11:19negate a number in two's complement. But
  1583. 1:11:21but why did I do that? And that's
  1584. 1:11:24because if I scrolled back up, uh, I
  1585. 1:11:26said we learned how to do addition in
  1586. 1:11:27binary, but we did not learn how to do
  1587. 1:11:29subtraction. How can I do a? We know how
  1588. 1:11:32to do a plus b. And how do I do a minus
  1589. 1:11:35b? Interesting question. And uh it turns
  1590. 1:11:38out you already know how to do it based
  1591. 1:11:40on what I just showed you. Because a
  1592. 1:11:42minus b is equal to a + b.
  1593. 1:11:48And I know how to I showed you how to
  1594. 1:11:50negate a number. I showed you how to
  1595. 1:11:52take some b and make it negative b. And
  1596. 1:11:54then I already showed you how to add two
  1597. 1:11:56numbers together. So, it turns out you
  1598. 1:11:58actually do know how to subtract. Um,
  1599. 1:12:01and this is pretty powerful. So, let's
  1600. 1:12:03say I wanted to do 6 minus 2. Well, we
  1601. 1:12:06know that six in decimal is equal to 0 1
  1602. 1:12:101 0 in our two's complent binary and two
  1603. 1:12:15um in in in
  1604. 1:12:18uh decimal I will go ahead and just tell
  1605. 1:12:21you is this number right here. So, I
  1606. 1:12:24want to do 6 minus 2. And to do that,
  1607. 1:12:26I'm going to do 6 + -2. So the first we
  1608. 1:12:29need to do is is is what is -2 uh in in
  1609. 1:12:33binary. And so what we do is we take our
  1610. 1:12:35representation of two. The first thing
  1611. 1:12:37we do, I'm going to scroll up here. We
  1612. 1:12:39invert all the digit positions. And then
  1613. 1:12:41we add one. So 0 0 1 0 becomes 1 1 0 1.
  1614. 1:12:45And then we add one to it. And we are
  1615. 1:12:49left with
  1616. 1:12:511 1 0. And you can convince yourself
  1617. 1:12:53this is true, but I'm not going to for
  1618. 1:12:55the sake of time. And then what we want
  1619. 1:12:57to do is 6 to get 6 - 2. Uh, I'll write
  1620. 1:13:02this out. We want 6 - 2. Uh, we're going
  1621. 1:13:04to do 6 + -2. And we now have 6. And we
  1622. 1:13:08now have -2. And we know how to add. So
  1623. 1:13:11we'll just add them. So we'll do 0 1 1 0
  1624. 1:13:14+ 1 1 0. And let's go ahead and add. And
  1625. 1:13:18so that's zero here, a zero here, a one
  1626. 1:13:22with a carryover, and a zero with a
  1627. 1:13:24carryover. But we said we were working
  1628. 1:13:26with fourdigit positions. We just look
  1629. 1:13:28at the four that we have, and we would
  1630. 1:13:31hope that this number is equal to four.
  1631. 1:13:32And it turns out this is this is it
  1632. 1:13:34because it is uh we have 2 the^ of 0, 2
  1633. 1:13:37the^ of 1, and 2 the^ of 2. We just have
  1634. 1:13:39a 2 to the^ of two, which is 2 * 2,
  1635. 1:13:42which you should know is four by now. So
  1636. 1:13:46you uh and I know we just went a little
  1637. 1:13:48quickly and I only gave one example. The
  1638. 1:13:51website I will link uh I will link a
  1639. 1:13:53website below that I've written and it
  1640. 1:13:55has more examples and is written with a
  1641. 1:13:58little bit more detail. So if any of
  1642. 1:13:59this is confusing to you uh you can just
  1643. 1:14:02go to that website and get a more
  1644. 1:14:04complete understanding and some examples
  1645. 1:14:06may help work out the gaps. So now uh up
  1646. 1:14:10to this point let's recap what we've
  1647. 1:14:11learned. We have learned uh how to well
  1648. 1:14:15first off we recaped decimal but I'm not
  1649. 1:14:18going to include that because
  1650. 1:14:19intuitively you already knew that we
  1651. 1:14:21learned how to go from decimal to
  1652. 1:14:23binary.
  1653. 1:14:27We learned how to go from binary to
  1654. 1:14:29decimal and we actually did that first.
  1655. 1:14:34We learned how to negate. We learned
  1656. 1:14:37two's complement. So how do we represent
  1657. 1:14:39negative numbers?
  1658. 1:14:42We then learned how to add binary
  1659. 1:14:46numbers.
  1660. 1:14:49And lastly, we learned how a a little
  1661. 1:14:52trick that lets us subtract binary
  1662. 1:14:54numbers.
  1663. 1:14:59And this is all you need to know about
  1664. 1:15:02how computers represent numbers. So even
  1665. 1:15:05if you stop watching this video right
  1666. 1:15:06now, you have walked away with a a very
  1667. 1:15:09interesting uh lesson. And that is how
  1668. 1:15:12do computers represent numbers? And you
  1669. 1:15:15may not understand why it needs to be
  1670. 1:15:17done like this yet because we haven't
  1671. 1:15:18got any further. But at least you
  1672. 1:15:20understand what's happening under the
  1673. 1:15:21hood. And when you type in uh when
  1674. 1:15:24you're typing in 5 + 7 to your
  1675. 1:15:26calculator, it's not doing 5 + 7. It's
  1676. 1:15:28doing uh 1 0 1 plus some other binary
  1677. 1:15:32string. And it's pretty interesting. Um,
  1678. 1:15:37so if you've made it this far, you have
  1679. 1:15:39learned what in my subjective opinion I
  1680. 1:15:42would say is one of the two most
  1681. 1:15:44difficult components of this video. Uh,
  1682. 1:15:46I think understanding this binary
  1683. 1:15:48representation is very difficult and
  1684. 1:15:51we're going to come back eventually and
  1685. 1:15:52there's going to be one other really
  1686. 1:15:54difficult concept uh, which is how we
  1687. 1:15:56can store memory on computers. Um, and I
  1688. 1:16:00think if you can power through both of
  1689. 1:16:02those, then you will have a full
  1690. 1:16:03understanding. And I think everything
  1691. 1:16:05else in this video will be easier than
  1692. 1:16:07those two topics. So, even though we're
  1693. 1:16:09not halfway done with this video,
  1694. 1:16:11metaphorically and in terms of
  1695. 1:16:12difficulty, I would argue we are halfway
  1696. 1:16:14through the difficulty.
  1697. 1:16:16So, what we're going to do next is learn
  1698. 1:16:20about something called a logic gate.
  1699. 1:16:22And, uh, it's going to have nothing to
  1700. 1:16:24do with what we just did. what I just
  1701. 1:16:26did. I'm gonna ask that you mentally
  1702. 1:16:28push aside and then we're immediately
  1703. 1:16:30going to bring it back together. Uh and
  1704. 1:16:32and and so I'm I've taught you binary.
  1705. 1:16:35I'm now going to teach you something
  1706. 1:16:36called logicates which are independent
  1707. 1:16:38of binary. And then we're going to bring
  1708. 1:16:40it together and see how these two topics
  1709. 1:16:42relate.
  1710. 1:16:44Okay? So I'm going to ask that you
  1711. 1:16:46mentally put everything that we just did
  1712. 1:16:48aside in your head. Ignore it all. and
  1713. 1:16:52we're going to learn a new concept and
  1714. 1:16:55it's going to seem a little bit familiar
  1715. 1:16:58as it probably should and you'll see why
  1716. 1:17:00they are familiar and similar in nature.
  1717. 1:17:04But don't worry, uh, what we just
  1718. 1:17:06learned is not going to be abandoned.
  1719. 1:17:07Binary, I didn't put that in there just
  1720. 1:17:09to torture you. I put that in because
  1721. 1:17:11it's going to be a very real concept
  1722. 1:17:13that is almost fundamental to the
  1723. 1:17:15entirety of this video.
  1724. 1:17:17and we're going to tie these things
  1725. 1:17:19called logic gates back together with
  1726. 1:17:21binary afterwards. So, let's go ahead
  1727. 1:17:23and uh see what a logic gate is. And I
  1728. 1:17:28actually think that you may find uh the
  1729. 1:17:30content in this section pretty
  1730. 1:17:32interesting. Not to say binary is
  1731. 1:17:33uninteresting. I just think that the
  1732. 1:17:35majority of people will find it a little
  1733. 1:17:37bit tedious or boring the more specific
  1734. 1:17:40you go into it. Let us start with the
  1735. 1:17:44first logic gate which is called the AND
  1736. 1:17:46gate
  1737. 1:17:48and it looks like this.
  1738. 1:17:52Uh I will zoom in and draw this a little
  1739. 1:17:55bit better. So we will say we have a and
  1740. 1:17:59b and then we will say the outputs y. So
  1741. 1:18:02a logic gate takes in two inputs and
  1742. 1:18:07puts out an output this and gate
  1743. 1:18:09specifically. So what I will say is that
  1744. 1:18:13the output y
  1745. 1:18:16is a function of a and b. And please do
  1746. 1:18:20not let this scare you if it's giving
  1747. 1:18:22you uh PTSD to your high school algebra
  1748. 1:18:25classes. It's not as difficult as it
  1749. 1:18:28seems. And we're also going to add this
  1750. 1:18:30rule that a is either a zero or a is
  1751. 1:18:35equal to one. And we're going to do the
  1752. 1:18:37same thing for b. B is going to be
  1753. 1:18:39either a zero or it's going to be equal
  1754. 1:18:41to one. And we're going to do the same
  1755. 1:18:43thing for Y.
  1756. 1:18:45Y will either be a zero or it will be a
  1757. 1:18:48one. So our function here Y is a
  1758. 1:18:54function. Y will be the output of this
  1759. 1:18:56gate and its inputs to the gate are
  1760. 1:18:59going to be A and B. So whatever A and B
  1761. 1:19:02are will determine what Y is. and a and
  1762. 1:19:06b can only be either zero or one. So
  1763. 1:19:09there's not that many combinations. So
  1764. 1:19:11there's two values that we have that are
  1765. 1:19:14inputs to this function. And there's two
  1766. 1:19:16different values that each of those
  1767. 1:19:17inputs could have. So we're actually
  1768. 1:19:19going to we could build out a table to
  1769. 1:19:21represent each of the inputs and look at
  1770. 1:19:23what the output's going to be. So I'm
  1771. 1:19:26going to uh erase this
  1772. 1:19:31and make this truth table. So we have a
  1773. 1:19:35and b as our inputs and then I'll add
  1774. 1:19:38this column for y which is the output.
  1775. 1:19:40So uh our our four different possible
  1776. 1:19:43cases are a and b are both zero. Maybe a
  1777. 1:19:47is a one and b is a zero. Maybe a is a
  1778. 1:19:50zero but b is a one. Or maybe they're
  1779. 1:19:52both ones. And and this is uh this is
  1780. 1:19:55all the possible input combinations
  1781. 1:19:57there are. If you don't believe me,
  1782. 1:19:59please try invent another one and I
  1783. 1:20:02guarantee that you will not be able to
  1784. 1:20:03do so.
  1785. 1:20:06Now, I'm just going to tell you what the
  1786. 1:20:08ANDgate does. Uh, you may have guessed,
  1787. 1:20:11but it outputs a one if both of the
  1788. 1:20:13inputs are one and otherwise it outputs
  1789. 1:20:16a zero. So, it's an ANDgate because we
  1790. 1:20:19say it outputs one if A and B are one.
  1791. 1:20:23Otherwise, it's going to output a zero.
  1792. 1:20:25So in our example here, when a and b are
  1793. 1:20:27both ones, y is going to be equal to
  1794. 1:20:29one. And in any other case, y is going
  1795. 1:20:31to be equal to zero.
  1796. 1:20:34And that's because the definition of an
  1797. 1:20:36andgate is that it only outputs one if
  1798. 1:20:39both of the inputs are one.
  1799. 1:20:43Okay, congratulations. You've learned
  1800. 1:20:45your first logic gate. We've learned
  1801. 1:20:47that logic gates take in a a certain
  1802. 1:20:50number of inputs, but they will always
  1803. 1:20:52only output one number. I'll tell you
  1804. 1:20:55that now. We're going to look at other
  1805. 1:20:56logicates. Some may take in two inputs,
  1806. 1:20:58some may only take in one input, but
  1807. 1:20:59they're always only going to output one
  1808. 1:21:01value. And that one value is going to
  1809. 1:21:04always be determined strictly based the
  1810. 1:21:06inputs. So us having an A set to one and
  1811. 1:21:10B set to one means Y will always be set
  1812. 1:21:13to one for an ANDgate. And this property
  1813. 1:21:15is what we call determinism.
  1814. 1:21:20And it's a scary sounding word, but to
  1815. 1:21:22be deterministic, it means that the
  1816. 1:21:24output y is is strictly dependent and
  1817. 1:21:28can be determined based on the value of
  1818. 1:21:30the inputs a and b. Um, and that's just
  1819. 1:21:33a word that if you curiously read around
  1820. 1:21:35online, you will probably come across.
  1821. 1:21:37So, I thought it was worth mentioning.
  1822. 1:21:39Let's go ahead and look at some more
  1823. 1:21:40other logic gates that we have. We have
  1824. 1:21:42the ANDgate. Now, let's go ahead and
  1825. 1:21:44look at the ORgate.
  1826. 1:21:49So the orgate looks like this.
  1827. 1:21:54We have our two inputs still.
  1828. 1:21:57We have a b and y. Uh and it looks
  1829. 1:22:00different because it has this little
  1830. 1:22:01curvature on the inside. Uh but this is
  1831. 1:22:03a orgate. So let's do the same thing as
  1832. 1:22:05before. Uh let's build this truth table
  1833. 1:22:07out.
  1834. 1:22:10And we have the same uh same conditions
  1835. 1:22:13that our inputs and outputs are all
  1836. 1:22:15binary. I.e. either either zeros or
  1837. 1:22:17ones.
  1838. 1:22:19So this table will be the exact same
  1839. 1:22:21before up till now. And I will tell you
  1840. 1:22:24that the orgate uh you should hopefully
  1841. 1:22:26be able to guess the andgate outputs one
  1842. 1:22:29if a if and a and b are both one i.e
  1843. 1:22:33both of its inputs are one. The orgate
  1844. 1:22:35is going to output a one as long as
  1845. 1:22:37either of its inputs are one. So if a or
  1846. 1:22:40b is a one then we're going to output a
  1847. 1:22:42one. So uh this is going to happen not
  1848. 1:22:45in our first case. Neither of the inputs
  1849. 1:22:47are one. So we output nothing. We output
  1850. 1:22:49a zero. In the remainder of the cases at
  1851. 1:22:52least a or b one of them if not both is
  1852. 1:22:56a one. So that's alga output a one.
  1853. 1:23:00And it doesn't require there's nothing
  1854. 1:23:02wrong with both of them being one. It
  1855. 1:23:03just says as long as either of them is a
  1856. 1:23:05one I will output a one. And that's what
  1857. 1:23:08the orgate does.
  1858. 1:23:10Now we're going to look at the exclusive
  1859. 1:23:12orgate. And this is called XOR. Exor
  1860. 1:23:17exclusive or. And it looks like our
  1861. 1:23:19orgate, but it has this extra line.
  1862. 1:23:28And this is going to be the exclusive
  1863. 1:23:30ORgate. And what this one's going to do
  1864. 1:23:32is it's going to output one as long as
  1865. 1:23:35either A or B is a one. but is with the
  1866. 1:23:39added condition that both of them can't
  1867. 1:23:41be a one. So if A and B are both one,
  1868. 1:23:44then it's going to output a zero. But if
  1869. 1:23:46just one of them is a one, then it'll
  1870. 1:23:47output a one.
  1871. 1:23:49So let's draw this truth table out.
  1872. 1:23:560 0 1 0 0 1. And it doesn't really
  1873. 1:23:59matter for a truth table what order we
  1874. 1:24:01put everything in as long as we have all
  1875. 1:24:03the cases. So AB both zero there. It
  1876. 1:24:07means there's no neither of them are
  1877. 1:24:08zero. So we up a zero. Then a is one and
  1878. 1:24:11b is zero. So uh either of them is one
  1879. 1:24:14but not both of them. So it's a one. And
  1880. 1:24:16the same can be said for the next case.
  1881. 1:24:18But in this final case, a and b are both
  1882. 1:24:22one. So we say that the output's going
  1883. 1:24:24to be a zero cuz it has to be exclusive.
  1884. 1:24:26Either one one of them can be a one but
  1885. 1:24:28not both of them. And this is called the
  1886. 1:24:31uh exclusive orgate. So in English this
  1887. 1:24:33may translate to exactly one of a or b
  1888. 1:24:37is equal to one.
  1889. 1:24:40And what you're going to kind of see
  1890. 1:24:41here uh and I've kind of intuitively
  1891. 1:24:43hinted it but I'm going to now make it
  1892. 1:24:44more explicit is that a lot of times we
  1893. 1:24:46will interpret a zero to be false and a
  1894. 1:24:50one to be truthful. Um so we can kind of
  1895. 1:24:54give some of these values use these
  1896. 1:24:56truth truth values if you could say. So
  1897. 1:24:59if I looked at the output of some logic
  1898. 1:25:01gate um then if it's a one the output of
  1899. 1:25:04that gate is saying true. So the
  1900. 1:25:06condition based on the inputs is being
  1901. 1:25:09met. And if it's a zero it means the
  1902. 1:25:10condition is not being met. So the
  1903. 1:25:11output's false. Um and that's just just
  1904. 1:25:14something worth putting out there. So
  1905. 1:25:16we've seen the ANDgate, the OR gate, the
  1906. 1:25:19exclusive orgate. Let's do a different
  1907. 1:25:22type of gate and this is going to be
  1908. 1:25:24called the inverter gate.
  1909. 1:25:27And this is interesting because it only
  1910. 1:25:30has one input. So I mentioned this
  1911. 1:25:32earlier. I said logic gates have a
  1912. 1:25:34variable number of inputs but always
  1913. 1:25:37only have one output. And that's exactly
  1914. 1:25:39what we have here. So our truth table uh
  1915. 1:25:42this is the knockgate. And all the
  1916. 1:25:44knockgate does is just invert its input.
  1917. 1:25:47So if you throw in a zero, it's going to
  1918. 1:25:48output a one. And if you throw in a one,
  1919. 1:25:50it's going to output a zero. So let's go
  1920. 1:25:53ahead and build a truth table for this.
  1921. 1:25:56Well, this is easy. The A can either be
  1922. 1:25:57zero or one. And then we just invert its
  1923. 1:25:59value. So if we throw in a zero, we get
  1924. 1:26:01out a one. And if we throw in a one, we
  1925. 1:26:02get out a zero. And this is how the
  1926. 1:26:04inverter gate works.
  1927. 1:26:08So we've seen four gates and there's one
  1928. 1:26:11other gate we should discuss and this
  1929. 1:26:12gate is going to be a little bit
  1930. 1:26:13interesting because it has a a pretty
  1931. 1:26:15unique feature about it.
  1932. 1:26:18And this is going to be called the NAND
  1933. 1:26:20gate.
  1934. 1:26:22So it is the output of the NAND of the
  1935. 1:26:26and AND gate negated. So it means not
  1936. 1:26:29and so as long as not A and B are both
  1937. 1:26:33one, this is going to output a one. So
  1938. 1:26:35it's literally going to be the opposite
  1939. 1:26:37of our ANDgate. Our ANDgate only outputs
  1940. 1:26:39one if A and B are both set to one. But
  1941. 1:26:41our NANDgate is going to ensure it's
  1942. 1:26:44going to output one. It's going to tell
  1943. 1:26:46you if not A and B are both set to one.
  1944. 1:26:50So, our NANDgate looks like our ANDgate,
  1945. 1:26:53but it has this little circle
  1946. 1:26:54afterwards, and it's going to take in
  1947. 1:26:57our our same two familiar inputs. I'm
  1948. 1:26:59also just arbitrarily calling these A
  1949. 1:27:01and B. They can be called whatever. And
  1950. 1:27:03in fact, when you see these logic,
  1951. 1:27:04they're usually not labeled letters. I'm
  1952. 1:27:06just trying to make it clear that we
  1953. 1:27:07have our our inputs on the left and then
  1954. 1:27:10our output on the right. Um, and that's
  1955. 1:27:12what you should really capture here. So,
  1956. 1:27:14let's build our truth table. We have A,
  1957. 1:27:17B, and Y.
  1958. 1:27:25And we are going to say this means not
  1959. 1:27:28and so we will out output a one as long
  1960. 1:27:30as the condition met. And the condition
  1961. 1:27:32is that not both A and B are one. So
  1962. 1:27:34that happens in our first three cases
  1963. 1:27:36and not the last case. In the last case,
  1964. 1:27:39A and B are both one. Um which means
  1965. 1:27:43that A and B is true. But we want the
  1966. 1:27:46negated version of that. we we we say
  1967. 1:27:48not A and B. Uh so we output at zero.
  1968. 1:27:52So there's this really interesting
  1969. 1:27:54thing. I'm going to make two pretty
  1970. 1:27:56shocking claims. So the first is we can
  1971. 1:27:58build our entire computer out of these
  1972. 1:28:01logic gates right here. The computer
  1973. 1:28:04we're going to design is with one very
  1974. 1:28:07minor exception is going to be built
  1975. 1:28:09entirely out of logic gates. We are
  1976. 1:28:12going to be able to build storage using
  1977. 1:28:14logic gates. We're going to be able to
  1978. 1:28:15add numbers with logic gates, subtract
  1979. 1:28:17them, and do everything that a CPU can.
  1980. 1:28:20It turns out your computer that you're
  1981. 1:28:21watching this on is is generally
  1982. 1:28:24speaking built out of just logic gates.
  1983. 1:28:26The one exception uh for our computer is
  1984. 1:28:29going to be this thing called the clock,
  1985. 1:28:31which will just synchronize everything.
  1986. 1:28:33Our clock is just going to alternate
  1987. 1:28:35between 0 and one at a fixed pace. And
  1988. 1:28:38every other component in our computer is
  1989. 1:28:39going to look at that clock to know what
  1990. 1:28:41time it is. Generally speaking,
  1991. 1:28:44the clock's not going to be built with
  1992. 1:28:45logic gates. And we're actually not
  1993. 1:28:47going to study how the clock is built
  1994. 1:28:48cuz it's going to be built out of
  1995. 1:28:49electricity. But, uh, logic gates, uh,
  1996. 1:28:53back to the claim, can can be used to
  1997. 1:28:55build our entire computer. So, we're
  1998. 1:28:57going to take these very, very simple
  1999. 1:28:59logic gates and we can chain them
  2000. 1:29:01together as in the output of one can
  2001. 1:29:05become the input of another and we can
  2002. 1:29:06build these very complicated circuits.
  2003. 1:29:09And and the the goal of this video is to
  2004. 1:29:12build a circuit that's complicated
  2005. 1:29:13enough to to be an entire computer. And
  2006. 1:29:16this sounds like a crazy ambitious goal,
  2007. 1:29:18but it's it's really cool. And this was
  2008. 1:29:20one of the most fascinating things for
  2009. 1:29:22me when I learned this is that we can
  2010. 1:29:24build an entire computer out of some
  2011. 1:29:26logic gate, which seems pretty trivial.
  2012. 1:29:29And I'm going to make another claim. I'm
  2013. 1:29:32not going to back this one up, but it
  2014. 1:29:33turns out that this nan gate right here,
  2015. 1:29:36it's called the universal gate.
  2016. 1:29:41And any of these other logicates can be
  2017. 1:29:45built out of nan gates uh chained
  2018. 1:29:47together. And we're going to see what
  2019. 1:29:49chain together means shortly. But what
  2020. 1:29:51that means, if you can take that to be
  2021. 1:29:52the truth, is that you can actually
  2022. 1:29:54build an entire computer just out of
  2023. 1:29:57NAND gates, which is crazy. And that is
  2024. 1:30:00uh to my understanding actually how most
  2025. 1:30:02computers are implemented. You have just
  2026. 1:30:03have a lot of nan gates.
  2027. 1:30:06And how are we going to build a computer
  2028. 1:30:08out of chains together? Uh well, I kind
  2029. 1:30:10of mentioned this earlier when we were
  2030. 1:30:11discussing binary. I said in computers,
  2031. 1:30:14we either have electricity on a wire or
  2032. 1:30:16we don't. If we have no electricity on a
  2033. 1:30:19wire, we'll treat that as a zero. And if
  2034. 1:30:21we do have electricity on that wire,
  2035. 1:30:23we're going to treat that as a one. And
  2036. 1:30:26these these these logicates can be built
  2037. 1:30:29physically. We're not going to see how
  2038. 1:30:31they're built physically because that
  2039. 1:30:32also deals with electricity which I'm
  2040. 1:30:35not well equipped to teach you about
  2041. 1:30:36because I barely understand myself. But
  2042. 1:30:39uh we can understand at at a level at a
  2043. 1:30:42higher level that will just treat the
  2044. 1:30:44presence of electricity as a one in the
  2045. 1:30:45absence of it as a zero and build these
  2046. 1:30:48these these uh logic gates together and
  2047. 1:30:51chain them together in such a way that
  2048. 1:30:53if we did have real electricity and a
  2049. 1:30:54way of building these logic gates both
  2050. 1:30:56of which we do uh it could it could run
  2051. 1:30:59an entire computer and that's actually
  2052. 1:31:02something I've done. I've built the
  2053. 1:31:04computer that we're going to study uh
  2054. 1:31:06with wires in the logic gates that you
  2055. 1:31:08can buy online. And that's something I
  2056. 1:31:10hope to demonstrate later on in this
  2057. 1:31:12video. But let's get back to our goal
  2058. 1:31:15here cuz I've I've made this crazy claim
  2059. 1:31:17that we're going to build an entire
  2060. 1:31:19computer out of Logicate. So I that's
  2061. 1:31:21going to be what the next uh next big
  2062. 1:31:24segment of this video is about. How do
  2063. 1:31:26we actually go about doing this? Um, and
  2064. 1:31:29to do that, I want to start by
  2065. 1:31:30explaining how we can chain these logic
  2066. 1:31:32gates together. So, let's explain how we
  2067. 1:31:35could we could build a NANDgate by
  2068. 1:31:37chaining two of the other logic gates we
  2069. 1:31:39have together. So, uh, as a reminder,
  2070. 1:31:42the NANDgate just takes whatever the
  2071. 1:31:44output of the ANDgate is and inverts it.
  2072. 1:31:47It negates it. So, if the ANDgate
  2073. 1:31:49outputs a one, then we we output a zero.
  2074. 1:31:52And if it if the ANDgate outputs a a
  2075. 1:31:55zero, we should output a one.
  2076. 1:31:57And uh if if I asked you which two gates
  2077. 1:32:00we could combine um the answer would be
  2078. 1:32:04we could combine an AND gate and a
  2079. 1:32:07inverter gate together to build this
  2080. 1:32:09circuit out. So let's go ahead and see
  2081. 1:32:11uh what this does. I will spill the
  2082. 1:32:13answer for you and then we will we will
  2083. 1:32:15verify that things are true. So I'm
  2084. 1:32:18going to take our A and B which are our
  2085. 1:32:20inputs. I'm going to feed them into this
  2086. 1:32:23and ANDgate. And then I'm going to feed
  2087. 1:32:25the output of that and AND gate into a
  2088. 1:32:29notgate. This gate that we saw down
  2089. 1:32:31here. And then I'm going to let that be
  2090. 1:32:33the output Y. And so I have this
  2091. 1:32:36circuit. And this is this is pretty big.
  2092. 1:32:37What we just did is we we took the
  2093. 1:32:39output of one gate and fed it in as the
  2094. 1:32:41input of another gate. And it's it's a
  2095. 1:32:44small example, but we're we're going to
  2096. 1:32:45see more complicated ones very quickly.
  2097. 1:32:48So let's go ahead and convince ourselves
  2098. 1:32:51that this is actually equivalent to our
  2099. 1:32:54NAND gate from over here. And the way we
  2100. 1:32:56can do that is we can build a truth
  2101. 1:32:58table uh for what we have here. And what
  2102. 1:33:00we have here is called a circuit. A
  2103. 1:33:03circuit is what happens when you chain
  2104. 1:33:05logic gates together. And by chaining
  2105. 1:33:08logic gates together, we can provide we
  2106. 1:33:10can build circuits that accomplish more
  2107. 1:33:13interesting things. So these are these
  2108. 1:33:16these these uh logic gates on their own
  2109. 1:33:19don't do very much but by chaining them
  2110. 1:33:21together we could actually have them do
  2111. 1:33:23really interesting things. Uh and I mean
  2112. 1:33:26the end goal is chaining them together
  2113. 1:33:28in such a way that we build an entire
  2114. 1:33:29computer. Uh and that is totally
  2115. 1:33:31possible as I will try to convince you
  2116. 1:33:34over the next few hours. So let's go
  2117. 1:33:36ahead and build out our truth table. We
  2118. 1:33:39have two inputs and one output.
  2119. 1:33:42And let us see uh we know our our output
  2120. 1:33:45cases are as follows. I'll change up the
  2121. 1:33:49order here a little bit for the second
  2122. 1:33:50and third rows. So when a and b are both
  2123. 1:33:53zero, well we have a is zero here, a
  2124. 1:33:56zero here. 0 and 0 is zero cuz not both
  2125. 1:34:00of them are one. And then we invert it
  2126. 1:34:02and uh produce a one. So we end up with
  2127. 1:34:05a one as the output of our our circuit
  2128. 1:34:07when the inputs are both zero and zero.
  2129. 1:34:09And and let me change color here just so
  2130. 1:34:11it's easier to follow. Then let's
  2131. 1:34:12suppose we have zero and one. Then the
  2132. 1:34:16endgate's still going to output a zero
  2133. 1:34:18because not both the inputs are one. But
  2134. 1:34:20when we invert it, it becomes a one. So
  2135. 1:34:22uh this is a one here. And if I flip
  2136. 1:34:25this uh if I say that B is going to if A
  2137. 1:34:30is going to be sorry I did that
  2138. 1:34:31backwards actually. Um, I showed you,
  2139. 1:34:35let's see, I showed you 0 1. Oh, no, no,
  2140. 1:34:37I did that correctly. Now I'm going to
  2141. 1:34:39show you one zero. And we know the
  2142. 1:34:40output of the endgate is going to be
  2143. 1:34:42unaffected. It's still going to be a
  2144. 1:34:43zero. But then when we invert it, it
  2145. 1:34:44becomes a one. And lastly, uh, if we
  2146. 1:34:48have a one and a one, then what happens
  2147. 1:34:50is our AND gate outputs a one, but when
  2148. 1:34:53we invert it, it becomes a zero. And so
  2149. 1:34:55we we have this truth table right here.
  2150. 1:34:58And
  2151. 1:35:00I'm going to replace this with white now
  2152. 1:35:02that we're done drawing.
  2153. 1:35:08What we would expect is that if this
  2154. 1:35:10circuit is equal to the nan gate, then
  2155. 1:35:12the truth table should be identical. And
  2156. 1:35:15if I scroll out, we can see that the
  2157. 1:35:17truth tables are identical. Uh you can
  2158. 1:35:19compare each of the four rows against
  2159. 1:35:20each other. Um I know I've swapped the
  2160. 1:35:22order of the middle two, which I
  2161. 1:35:23shouldn't have in hindsight cuz it's a
  2162. 1:35:25little confusing now. Uh, but I was
  2163. 1:35:27trying to illustrate the point that the
  2164. 1:35:28truth table doesn't matter what order
  2165. 1:35:30the inputs are in as long as you cover
  2166. 1:35:31all the possible inputs. Um, but in
  2167. 1:35:35general, I've shown you how to build a
  2168. 1:35:37circuit by chaining logic gates together
  2169. 1:35:39and that you can verify that two
  2170. 1:35:41circuits are equal by showing that their
  2171. 1:35:45truth tables are identical for every
  2172. 1:35:47pair of input combinations.
  2173. 1:35:49So this function, this circuit is a
  2174. 1:35:52function of A and B and it's going to
  2175. 1:35:55equal Y. And so is our NANDgate. And it
  2176. 1:35:58turns out they're actually they're the
  2177. 1:36:00same function um because they always
  2178. 1:36:02equal the same value. And so this is
  2179. 1:36:05this is pretty interesting. And uh we
  2180. 1:36:09could every time we we could avoid using
  2181. 1:36:11the nan gate just by by drawing this.
  2182. 1:36:13But it turns out that we use a nan gate
  2183. 1:36:14enough where we we want to give it its
  2184. 1:36:16own gate. um because it it kind of when
  2185. 1:36:18we have these big circuits that chain a
  2186. 1:36:20lot together, it it becomes a little bit
  2187. 1:36:22more convenient to have uh simplified
  2188. 1:36:25ways to represent things we use
  2189. 1:36:27frequently. And we will see this later
  2190. 1:36:29on. We'll design some interesting
  2191. 1:36:30circuits and then we will just uh demark
  2192. 1:36:33them as a new type of of of gate or
  2193. 1:36:35maybe we'll say uh a triangle represents
  2194. 1:36:38this circuit underneath the hood. Um but
  2195. 1:36:40but don't think about that now. That's
  2196. 1:36:42something that uh is more of a future
  2197. 1:36:43concern. Let's try another circuit. So
  2198. 1:36:46you've seen um a circuit now that has
  2199. 1:36:49two inputs and
  2200. 1:36:53one output. And so for logic gates, I I
  2201. 1:36:56made a claim. I said a logic gate has uh
  2202. 1:36:59at least one input
  2203. 1:37:02and it always has exactly one output. A
  2204. 1:37:06circuit has at least one input,
  2205. 1:37:10but it may have multiple outputs. And
  2206. 1:37:12we'll see this later um when we actually
  2207. 1:37:15try to build more meaningful circuits.
  2208. 1:37:22Let's try build something uh and let me
  2209. 1:37:25draw a circuit and then we will build a
  2210. 1:37:28truth table for it and and analyze that
  2211. 1:37:30truth table and see if we can derive
  2212. 1:37:32some meaning for that circuit.
  2213. 1:38:08Okay, this is our circuit. I'm going to
  2214. 1:38:11take a sip of water while you look at
  2215. 1:38:12that. And I would like you to notice
  2216. 1:38:14that there's three inputs.
  2217. 1:38:19and one output. And uh we have a
  2218. 1:38:24inverter a notgate right here. Then we
  2219. 1:38:27have two ANDgates over here. And then we
  2220. 1:38:29have an OR gate over here.
  2221. 1:38:33That's just because uh you know it may
  2222. 1:38:35actually be useful to screenshot this as
  2223. 1:38:36you follow along or just you can Google
  2224. 1:38:38logic circuits and it'll be there. It's
  2225. 1:38:40also in the website I have linked below.
  2226. 1:38:43Um but we have a a circuit now and uh
  2227. 1:38:47this may seem like a random circuit but
  2228. 1:38:48it actually does something a little bit
  2229. 1:38:49interesting. Um so [snorts] I will spoil
  2230. 1:38:53the fun and say that this is a selector
  2231. 1:38:56circuit. So if you put some value of A
  2232. 1:38:59and B uh put some values in A and B then
  2233. 1:39:03C serves as a selector where if C is
  2234. 1:39:07zero then the output so I'll write this
  2235. 1:39:10out. If C is zero, then
  2236. 1:39:14Y is going to equal to A. And if C is
  2237. 1:39:17one,
  2238. 1:39:19then Y will equal B. So C effectively
  2239. 1:39:24controls what flows through as the
  2240. 1:39:26output of this. So I'm going to move
  2241. 1:39:28this over here. And let's verify this by
  2242. 1:39:32building a truth table for our circuit.
  2243. 1:39:35So this truth table is going to be a
  2244. 1:39:37little bit longer. There's actually
  2245. 1:39:38gonna be eight different values we can
  2246. 1:39:40pass in. Um, so I'm going to come here.
  2247. 1:39:45We have eight different values we can
  2248. 1:39:47pass in. Our output's still y. And uh, I
  2249. 1:39:51will write out all the eight values. Uh,
  2250. 1:39:53it may be interesting to you where I'm
  2251. 1:39:55getting these values from. Uh, but
  2252. 1:39:59you you need not worry about it. Um, I'm
  2253. 1:40:02just you'll get more comfortable
  2254. 1:40:04enumerating all these values out the
  2255. 1:40:05more you do do it on your own. Um,
  2256. 1:40:22it's getting a little messy because I
  2257. 1:40:23have not the best handwriting. Um,
  2258. 1:40:29okay. So, uh, believe me, please when I
  2259. 1:40:31say that these are all the possible
  2260. 1:40:32values of A, B, and C. If you do not
  2261. 1:40:34believe me that I challenge you to pause
  2262. 1:40:36this and try to generate a a
  2263. 1:40:40new uh combination of A, B, and C that's
  2264. 1:40:42not in this table. And if you're curious
  2265. 1:40:44where this this comes from, why how do I
  2266. 1:40:47know how many are in how many different
  2267. 1:40:48values there are? Well, if we're looking
  2268. 1:40:50at A, B, and C stacked next to each
  2269. 1:40:52other, um they're all either they're
  2270. 1:40:56binary values. They're binary digits.
  2271. 1:40:58So, there's two different values that
  2272. 1:41:00can go into each of these digit
  2273. 1:41:02positions. And so I multiply the
  2274. 1:41:04different possibilities together to
  2275. 1:41:06figure out the total number of
  2276. 1:41:07combinations. So in that case there's
  2277. 1:41:09eight. Uh and that's because um for each
  2278. 1:41:12of the position we can either choose a
  2279. 1:41:14zero or one uh 01. And then we multiply
  2280. 1:41:17that by the number of ways there are to
  2281. 1:41:19pick out the remaining digits. Um and we
  2282. 1:41:21can apply this process over and over.
  2283. 1:41:24Um,
  2284. 1:41:26but I'm not going to get into that cuz I
  2285. 1:41:27think it's a little mathy and uh that's
  2286. 1:41:29actually a study of combinotaurics,
  2287. 1:41:32which is probably a little too uh
  2288. 1:41:35technical for what we're getting into
  2289. 1:41:36today. But let's go ahead and trace
  2290. 1:41:39through this. So, I'm going to switch my
  2291. 1:41:41pen back to red and I'm going to make it
  2292. 1:41:42a little bit bigger here so it's more
  2293. 1:41:44readable. Our first example um I'm going
  2294. 1:41:47to zoom out here is I'm saying if A and
  2295. 1:41:50B are both zero and
  2296. 1:41:53my claim is that if C is zero which or C
  2297. 1:41:57is one then our output should be B which
  2298. 1:42:01is zero in this case. Um but it's a
  2299. 1:42:03little hard to distinguish because
  2300. 1:42:04they're actually both zero. But let's
  2301. 1:42:06let's verify that it's not one for sure
  2302. 1:42:08and that'll have you partially
  2303. 1:42:09convinced. So, uh, what we do right
  2304. 1:42:12here, um, is we have C, which is one.
  2305. 1:42:15And so, that means that we invert it.
  2306. 1:42:17It's going to be a zero here, and it's
  2307. 1:42:19going to be a one here. And then we have
  2308. 1:42:21these carried through from our our
  2309. 1:42:23original logic gates. And so, for the
  2310. 1:42:25first gate, we know 0 and 0 is zero. And
  2311. 1:42:280 and one for the second logic gate,
  2312. 1:42:30still zero. And so, we have two zeros
  2313. 1:42:32flowing into an orgate, which means the
  2314. 1:42:33output is zero. Um, so we traced through
  2315. 1:42:37the circuit and we our claim is verified
  2316. 1:42:39so far. Uh, C is one and the output was
  2317. 1:42:42equal to B. But you should be confused
  2318. 1:42:45because how do I know that it wasn't
  2319. 1:42:47equal to A? Um, and or just it just
  2320. 1:42:50happened to be zero independently of
  2321. 1:42:52what B was picked. Um,
  2322. 1:42:55and uh, that's a great question. And
  2323. 1:42:58let's actually toggle whatever. Let's
  2324. 1:43:01set B to a one and see what changes. So,
  2325. 1:43:04I'm going to erase this. Um,
  2326. 1:43:08and everything I've left on the screen
  2327. 1:43:10so far is un is unaffected by our
  2328. 1:43:12changing of B from a zero to a one. But
  2329. 1:43:14now, if I set B to a one, then this AND
  2330. 1:43:16gate over here is going to output a one,
  2331. 1:43:19which means this or gate's going to
  2332. 1:43:21output a one. Uh, and and now we we do
  2333. 1:43:24have a little more substance to our
  2334. 1:43:25claim. So, we have the case here of uh A
  2335. 1:43:29is zero, B is one, and C is one, which
  2336. 1:43:32is over here. And we know this output's
  2337. 1:43:34going to be one. Um, so I'm going to
  2338. 1:43:36keep going through these examples. But
  2339. 1:43:38what should be a little bit apparent by
  2340. 1:43:40by now uh is
  2341. 1:43:43if you look at these and gates,
  2342. 1:43:46we know that for an ANDgate to output
  2343. 1:43:48one, both of its inputs need to be one.
  2344. 1:43:51And C goes to both of these AND gates,
  2345. 1:43:53but one of them gets the regular C and
  2346. 1:43:55one of them gets the opposite of C, the
  2347. 1:43:57inverted value of C. So we know that the
  2348. 1:43:59one that's getting the the uh we know
  2349. 1:44:03one of these and gates can never fire
  2350. 1:44:05because by virtue of C getting C going
  2351. 1:44:09to one gate and not C going to the other
  2352. 1:44:11gate then
  2353. 1:44:13the gate that gets the zero version of C
  2354. 1:44:16whether that's the inverted version or
  2355. 1:44:17not can never output a one because it
  2356. 1:44:19it's both of its inputs can't be one. So
  2357. 1:44:21for example if I if I set C to one here
  2358. 1:44:24then this is going to be a zero. So this
  2359. 1:44:26this gate can never output anything but
  2360. 1:44:28a zero. So this gate effectively gets
  2361. 1:44:31silenced and then the value of B can
  2362. 1:44:34just flow through this gate and it gets
  2363. 1:44:36to this orgate where it's just going to
  2364. 1:44:38go through because the orgate has a zero
  2365. 1:44:40here. Um and and it may take a little
  2366. 1:44:42bit of time for you to rationalize this.
  2367. 1:44:44So So pause it. I I would recommend you
  2368. 1:44:46pause things actually and fill out the
  2369. 1:44:49rest of the circuit for yourself
  2370. 1:44:50mentally or pause it as I work through
  2371. 1:44:53some of the cases. So now let's look the
  2372. 1:44:55example um and I I miswrote this here.
  2373. 1:44:58There should be 0 0 0 where everything
  2374. 1:45:00is zero. So uh 0 0 and then we have a
  2375. 1:45:05zero here which means a one comes here
  2376. 1:45:07and a one comes and a zero goes there.
  2377. 1:45:09So in this case uh both and gates output
  2378. 1:45:12a zero. So our y is also going to be a
  2379. 1:45:14zero. Um [snorts] and we kind of have
  2380. 1:45:16this rule where we said if if c is zero
  2381. 1:45:18then y is equal to a. And that that's
  2382. 1:45:20still the truth because C is zero and
  2383. 1:45:22and we have Y equal to A which is still
  2384. 1:45:24zero.
  2385. 1:45:29Okay. Uh
  2386. 1:45:32now let's look at a more interesting
  2387. 1:45:34case. So if we have A is zero but B is
  2388. 1:45:38one then let's look at the first case
  2389. 1:45:41where uh C is zero. So if C is zero then
  2390. 1:45:45this is going to be a zero. So this gets
  2391. 1:45:47cut off cuz we can't output anything but
  2392. 1:45:49with a zero. And then this is a one. Uh
  2393. 1:45:51but we know one and zero is zero. But
  2394. 1:45:53this is still a's value flowing through
  2395. 1:45:55cuz if a was a one, a one would get
  2396. 1:45:57through. And a is a zero and a zero goes
  2397. 1:45:59through. So a's value is is flowing
  2398. 1:46:01through here. That's what I'm hoping you
  2399. 1:46:02can see. And then we have a zero or a
  2400. 1:46:05zero which is is a zero which A's value
  2401. 1:46:07still flows through because if it was a
  2402. 1:46:08one then that's sufficient for this
  2403. 1:46:10orgate to activate. So we have C is zero
  2404. 1:46:14and then in our case A goes through
  2405. 1:46:16which is is kind of what we would hope
  2406. 1:46:17to see. And I am going to uh quickly
  2407. 1:46:22work through the remaining cases
  2408. 1:46:29because I said I would.
  2409. 1:46:32So let's do uh A is 1 and B is zero. A
  2410. 1:46:37is one, B is zero. Uh the first case
  2411. 1:46:39will start with C is zero. So then a one
  2412. 1:46:42goes through here, a zero comes through
  2413. 1:46:43here, a 0 and 0 is 0, one and one is
  2414. 1:46:46one, and then a one or one is one. And
  2415. 1:46:48you can verify this. This meets what we
  2416. 1:46:50would expect of the rule. And now if we
  2417. 1:46:52flip the value of C to get the next row
  2418. 1:46:54in the table, then we have a C is one,
  2419. 1:46:58this is zero, this is one, um, and then
  2420. 1:47:02we have a zero coming through here, a
  2421. 1:47:04zero coming through here, which means we
  2422. 1:47:05output a zero. And this this rule still
  2423. 1:47:07holds. And now let's look at the very
  2424. 1:47:10last case. Uh when both the inputs are
  2425. 1:47:13one. So if both the inputs are one and C
  2426. 1:47:16is a zero. Um start with that case. Then
  2427. 1:47:20this is a one. This is a zero. This
  2428. 1:47:22outputs a zero. This outputs a one. And
  2429. 1:47:24that means we output a one. I'm going
  2430. 1:47:26intentionally fast here just because uh
  2431. 1:47:29I don't want to waste too much time.
  2432. 1:47:32Our very last case we switch C to a one.
  2433. 1:47:34And now we have a zero going to this
  2434. 1:47:36gate which is effectively cutting it
  2435. 1:47:37off. And we have a one going to this
  2436. 1:47:39gate which is letting B's value flow
  2437. 1:47:40through. And we have zero or one which
  2438. 1:47:42is one. And now we've completed this
  2439. 1:47:45truth table. And let me erase what I
  2440. 1:47:48have.
  2441. 1:47:51And uh
  2442. 1:47:54hopefully you can you can verify this
  2443. 1:47:56and see that uh this claim I've made
  2444. 1:47:59actually does hold. Um if C is a zero,
  2445. 1:48:02then we let the value of A flow through.
  2446. 1:48:04And if C is a one, then we let the value
  2447. 1:48:06of B flow through. So we've built what
  2448. 1:48:08we called a selector. This is called a
  2449. 1:48:10selector. And hopefully my goal in
  2450. 1:48:13showing you this was not to waste your
  2451. 1:48:15time, uh, but to show you a circuit that
  2452. 1:48:17we are going to revisit in the future,
  2453. 1:48:20um, but also show you that we can chain
  2454. 1:48:22these logic gates together to do pretty
  2455. 1:48:24interesting things. Um, this is a a
  2456. 1:48:27somewhat of a trivial task, but it's
  2457. 1:48:29it's actually not that trivial. It's
  2458. 1:48:31it's it's trivial because I gave you the
  2459. 1:48:33solution, but if I asked you to invent
  2460. 1:48:35this on your own, it might not have been
  2461. 1:48:37apparent as to how we how we do this.
  2462. 1:48:40Um,
  2463. 1:48:42so yeah, this is a a a brief overview of
  2464. 1:48:45circuits and how we can chain logic
  2465. 1:48:47dates together and build circuits. Okay,
  2466. 1:48:51so what we've now accomplished is I
  2467. 1:48:54clear my screen here. I showed you what
  2468. 1:48:57a logic gate is. I showed you what a
  2469. 1:49:00truth table is and how to derive a truth
  2470. 1:49:03table by looking at either a circuit or
  2471. 1:49:04a logicate. And then I showed you how
  2472. 1:49:07you could compose logicates together to
  2473. 1:49:10build what we call circuits that
  2474. 1:49:13encapsulate more meaningful behaviors.
  2475. 1:49:16And I also made this crazy claim that
  2476. 1:49:18we're going to build an entire computer
  2477. 1:49:20out of just logic gates.
  2478. 1:49:24So before we even did that, we talked
  2479. 1:49:27about binary
  2480. 1:49:29And what I would like to do now, as I
  2481. 1:49:32promised, I would like to bridge those
  2482. 1:49:34two different concepts together. So, we
  2483. 1:49:36discussed binary
  2484. 1:49:39and we discussed logic gates. Um,
  2485. 1:49:44but I haven't yet told you how they
  2486. 1:49:46relate to one another. That was a bad
  2487. 1:49:48arrow.
  2488. 1:49:50And what I'm going to do in this part of
  2489. 1:49:53the video is is bridge this together by
  2490. 1:49:57showing how we can build a circuit, i.e.
  2491. 1:50:00chain some series of logic gates
  2492. 1:50:03together to build a circuit that can add
  2493. 1:50:06binary numbers. And this is going to be
  2494. 1:50:08cool because
  2495. 1:50:12we have a way of going from the decimal
  2496. 1:50:14numbers we know to binary numbers. And
  2497. 1:50:16then I'll build I'll we'll end with a
  2498. 1:50:18circuit that can add two binary numbers
  2499. 1:50:22together.
  2500. 1:50:23And in the real world, these logic gates
  2501. 1:50:26act incredibly quickly. If you change
  2502. 1:50:29the inputs on a on an ANDgate, for
  2503. 1:50:30example, the output will will settle.
  2504. 1:50:34It's what we call propagate very
  2505. 1:50:36quickly. And in this way,
  2506. 1:50:40if we uh assume that there's no time for
  2507. 1:50:43us to convert from decimal to binary,
  2508. 1:50:45which is obviously not true. But if we
  2509. 1:50:48just assume that we have a long list of
  2510. 1:50:51binary numbers and we wanted to add them
  2511. 1:50:54all together, then doing it with a
  2512. 1:50:57calculator, I guess say we also have a
  2513. 1:50:59decimal equivalent, then adding those
  2514. 1:51:01numbers up with a calculator or
  2515. 1:51:03manually, should I say, adding those
  2516. 1:51:05numbers up manually as a human is going
  2517. 1:51:07to take some time. Whereas we could
  2518. 1:51:09build this circuit that that outputs the
  2519. 1:51:12value once you put the numbers in almost
  2520. 1:51:14immediately.
  2521. 1:51:16And these logic gates operate very
  2522. 1:51:18quickly and this will be a useful
  2523. 1:51:20circuit because adding numbers is
  2524. 1:51:23something we would like to automate as a
  2525. 1:51:25computer. At the end of the day, you may
  2526. 1:51:27have heard some saying that a computer
  2527. 1:51:28is a glorified calculator. And the
  2528. 1:51:31further along you get in this video, the
  2529. 1:51:33more convinced you may become that that
  2530. 1:51:34is the reality. And it is true.
  2531. 1:51:38Computers operate on numbers and they
  2532. 1:51:39are able to perform arithmetic on
  2533. 1:51:41numbers incredibly quickly. And in in
  2534. 1:51:44that sense, a computer really is just a
  2535. 1:51:45glorified glorified calculator. But
  2536. 1:51:48let's start with the simplest case.
  2537. 1:51:53I previously said when we learned two's
  2538. 1:51:55complement that we had to assume that
  2539. 1:51:57every number in binary is going to be
  2540. 1:51:59the same number of digits. I also then
  2541. 1:52:02said that our computer is going to be an
  2542. 1:52:048bit computer, meaning that we use eight
  2543. 1:52:07digits to represent every binary number.
  2544. 1:52:09And I I should have mentioned this
  2545. 1:52:11earlier, but I' i've been I've been
  2546. 1:52:12trying to avoid it just in hopes of
  2547. 1:52:14trying to avoid throwing too many words
  2548. 1:52:16at you. Uh bit is just a zero or one.
  2549. 1:52:19It's synony synonymous with a binary
  2550. 1:52:21digit uh bit. And we talked about how
  2551. 1:52:24most modern a lot of modern computers
  2552. 1:52:26use 32 bits or 64 bits or even 128 or
  2553. 1:52:30256 to represent their numbers. We're
  2554. 1:52:34going to build an eightbit computer
  2555. 1:52:36because it's enough for us to represent
  2556. 1:52:39some numbers uh and build these circuits
  2557. 1:52:41out without using incredible number of
  2558. 1:52:43logic gates. But but at the end of us
  2559. 1:52:45building the computer, it'll be
  2560. 1:52:47incredibly easy for you to understand
  2561. 1:52:49how we could scale it up to be a 32 or
  2562. 1:52:5164bit computer, something perhaps, let's
  2563. 1:52:54say, more modern. And the reason I won't
  2564. 1:52:56do it is because uh I am drawing these
  2565. 1:52:58logic gates out by hand and and drawing
  2566. 1:53:01something 64 times every time we want to
  2567. 1:53:03have a number. This will make more sense
  2568. 1:53:05later is painful compared to eight which
  2569. 1:53:07is still inherently repetitive but
  2570. 1:53:09bearable. So let's start by building a
  2571. 1:53:13circuit that does one bit addition. So
  2572. 1:53:18one bit addition means we're adding
  2573. 1:53:20binary numbers that are just one digit
  2574. 1:53:22position. And and so this is pretty
  2575. 1:53:24easy. If we're just adding two numbers,
  2576. 1:53:26we know that the the two uh if we're
  2577. 1:53:28doing the addition tables, we could have
  2578. 1:53:30a 0 plus 0, a 0 plus a 1, a 1 + a zero,
  2579. 1:53:36or a 1 plus a 1. And I'm setting these
  2580. 1:53:39up in the way we we normally do
  2581. 1:53:40addition. And so we know the output in
  2582. 1:53:42this case is a zero. Uh in this case is
  2583. 1:53:45a one. In this case is a one. And in
  2584. 1:53:47this case, we have a zero here, but we
  2585. 1:53:49carry over a one. And so the output's 1
  2586. 1:53:52zero. And this makes sense. We're we're
  2587. 1:53:54what we're saying here is that 0 + 0 is
  2588. 1:53:560. And then here we're saying 0 + 1 is
  2589. 1:53:591. And then here we're saying 1 plus 0
  2590. 1:54:01is 1. And here we're saying 1 plus 1 is
  2591. 1:54:042. Because in in binary this is uh 2 the
  2592. 1:54:071st + 2 to the 0, which is just 2 to the
  2593. 1:54:091st, which is just two.
  2594. 1:54:12So we'd like to build a simple circuit
  2595. 1:54:16that can do this arithmetic for us. So
  2596. 1:54:19if we set a to be one number and b to be
  2597. 1:54:22the other, we should get the output.
  2598. 1:54:24[snorts] And what we've noticed is that
  2599. 1:54:27we have two inputs here. So let's try do
  2600. 1:54:30this in reverse. We previously looked at
  2601. 1:54:32a circuit and then built a truth table
  2602. 1:54:34for it. Let's in this case start with a
  2603. 1:54:37truth table just we have our our
  2604. 1:54:39consolidation and then try guess what
  2605. 1:54:41circuit might match it. So in our case,
  2606. 1:54:44our two operands, operand meaning the
  2607. 1:54:46numbers that we're adding together are a
  2608. 1:54:48and b. And we'll let out our output y be
  2609. 1:54:52our sum. And for [snorts] now, we're
  2610. 1:54:55going to ignore this carry bit. We're
  2611. 1:54:56just going to say
  2612. 1:54:58the the trivial uh onedigit result. And
  2613. 1:55:04so in that in that case, I'm going to
  2614. 1:55:06kind of draw a red circle on this cuz
  2615. 1:55:07it's something we're going to have to
  2616. 1:55:08revisit. um because when we do add one
  2617. 1:55:10and a one, the output's not going to be
  2618. 1:55:12zero, but we're just going to design our
  2619. 1:55:14circuit to only have one output for now.
  2620. 1:55:16And we'll come back and and and add a
  2621. 1:55:18second output that could be used to
  2622. 1:55:20represent that carry bit. So if our
  2623. 1:55:23truth table may look something like this
  2624. 1:55:28and we know the output in each of this
  2625. 1:55:30case is a is as follows. And so what
  2626. 1:55:36we'd like to do is build a circuit that
  2627. 1:55:39that encapsulates this truth table. And
  2628. 1:55:43now what you may be recognizing if you
  2629. 1:55:46paid close enough attention is that this
  2630. 1:55:49truth table over here is is uh very
  2631. 1:55:53similar to the one I had for the
  2632. 1:55:55exclusive orgate. And it's not just that
  2633. 1:55:58it's very similar, it's actually the
  2634. 1:56:00case that they are identical.
  2635. 1:56:02And this is uh something that works out
  2636. 1:56:05just conveniently. And this exclusive
  2637. 1:56:08orgate can be used uh to do our our
  2638. 1:56:10little bit of a one bit addition here.
  2639. 1:56:13Um
  2640. 1:56:15so as it as it turns out uh if I let A
  2641. 1:56:19and B be our inputs
  2642. 1:56:22then we could feed it into this
  2643. 1:56:25exclusive orgate
  2644. 1:56:27and the output Y uh is is holds. And so
  2645. 1:56:32this does our our addition for us. You
  2646. 1:56:34could fill this truth table out again if
  2647. 1:56:35you don't believe me, but that's pretty
  2648. 1:56:37cool. Um I know we made this huge
  2649. 1:56:40oversimplification. We're looking at
  2650. 1:56:41onedigit numbers, but it's cool that
  2651. 1:56:44this one gate can be used to perform uh
  2652. 1:56:47this one singledigit position worth of
  2653. 1:56:49arithmetic.
  2654. 1:56:51But what we're missing out here is that
  2655. 1:56:53we have in the in this last case where
  2656. 1:56:55this red is on the screen, we have it's
  2657. 1:56:58possible that if we're adding a onedigit
  2658. 1:57:02number, two one-digit numbers, that our
  2659. 1:57:04output could be two digits. So if we're
  2660. 1:57:06adding two one-digit numbers, a
  2661. 1:57:09one-digit number and a one-digit number,
  2662. 1:57:10our output doesn't always have to be one
  2663. 1:57:12digit. It could be two digits as we saw
  2664. 1:57:15here. So what we need to do is update
  2665. 1:57:17this circuit to accommodate for this. So
  2666. 1:57:21what we could say
  2667. 1:57:23in each of these examples that only have
  2668. 1:57:26one bit of output. I talked earlier
  2669. 1:57:28about how we can pad with zeros. So I'm
  2670. 1:57:31just going to pad each of these
  2671. 1:57:32scenarios with a different zero. I'm
  2672. 1:57:34going to do a little bit of separation
  2673. 1:57:36here. And now let's say we have a
  2674. 1:57:38circuit that takes in two inputs and
  2675. 1:57:40produces two outputs. The two outputs
  2676. 1:57:44will be the two bit positions. And what
  2677. 1:57:46we're going to do is we're going to call
  2678. 1:57:47this one the sum bit.
  2679. 1:57:50Sorry, I wrote that weird. This is the
  2680. 1:57:52sum bit and this is the carry bit. And
  2681. 1:57:56uh I'm just going to happen to call it
  2682. 1:57:57that accordingly. Um so what I'd like to
  2683. 1:58:00do then is update our truth table and
  2684. 1:58:03and add in these additional values. So
  2685. 1:58:05our carry bit and our sum bit. And it's
  2686. 1:58:07just going to be the exact same values
  2687. 1:58:09that we had as before.
  2688. 1:58:11Um
  2689. 1:58:14but the interesting thing is now that
  2690. 1:58:15we're able to represent correctly the
  2691. 1:58:18the value of what happens when we add 1
  2692. 1:58:21plus 1. We can represent two. And that's
  2693. 1:58:24because as we talked about earlier, we
  2694. 1:58:25can't represent two with one binary
  2695. 1:58:27digit cuz that's just not possible. And
  2696. 1:58:30you also may be wondering what about
  2697. 1:58:32this two's complement? Uh let's just
  2698. 1:58:34ignore it for now. Ignore's complement
  2699. 1:58:35because we're we're just going to assume
  2700. 1:58:37that we're working just with positive
  2701. 1:58:38numbers. So ignore that rule where you
  2702. 1:58:40multiply the leftmost digit by the
  2703. 1:58:43negative one of its [clears throat]
  2704. 1:58:44weight. And then over here, let me just
  2705. 1:58:46write a note that uh we're going to say
  2706. 1:58:48S stands for the sum position and C
  2707. 1:58:50stands for the carry position. So now
  2708. 1:58:53what we want to do is build a circuit
  2709. 1:58:55that has two outputs. One for the carry
  2710. 1:58:58bit and one for the sum bit. And
  2711. 1:59:01[snorts]
  2712. 1:59:02well, we already knew how to build the
  2713. 1:59:04sum bit. We said the sum or the carry
  2714. 1:59:06the sum bit, sorry. Uh, we already knew
  2715. 1:59:09how to build this out and I' I've made a
  2716. 1:59:11mistake here. There should be a zero. We
  2717. 1:59:13already knew that the sum bit is going
  2718. 1:59:15to be equal to the exclusive orgate. So,
  2719. 1:59:17let's recreate that. I shouldn't have
  2720. 1:59:19erased it, but I did. Uh, just cuz I
  2721. 1:59:22wanted to have us a fresh slate for us.
  2722. 1:59:24Um, and let's say that the exclusive
  2723. 1:59:28orbit right here gets the input from A
  2724. 1:59:32and it gets the input from B. And this
  2725. 1:59:36is going to output. Let's put this over
  2726. 1:59:38here so we can get it in the left to
  2727. 1:59:40right. This is going to output the sum
  2728. 1:59:42bit. And then now let's look at this
  2729. 1:59:45carry bit. If we look at the carry bit,
  2730. 1:59:47it always outputs zero except for when a
  2731. 1:59:50and b are both one. And if you uh pause
  2732. 1:59:54pause for a second here and try to think
  2733. 1:59:56of what gate this reminds you of. And
  2734. 1:59:59the answer is that this is equal to the
  2735. 2:00:01AND gate. This has the same exact truth
  2736. 2:00:03table as our AND gate up here, the
  2737. 2:00:05[snorts] very first gate we talked
  2738. 2:00:07about. And this works out really
  2739. 2:00:09convenient uh because it means that we
  2740. 2:00:10can just steal the AND gate and use it
  2741. 2:00:12to represent this uh C
  2742. 2:00:15this carry bit position. So if I just
  2743. 2:00:17have an AND gate, then the uh output
  2744. 2:00:22sorry let me write this over here. So we
  2745. 2:00:25have the carry bit here um and let's
  2746. 2:00:28have the sum here. here. So that let's
  2747. 2:00:29just read it left to right as we we did
  2748. 2:00:31up here. But let's go ahead and chain
  2749. 2:00:35these inputs in. So when A and B are
  2750. 2:00:39both to one, then we should have this
  2751. 2:00:41carry. And I apologize because uh I
  2752. 2:00:47accidentally inverted this CNSS,
  2753. 2:00:49which I apologize for confusion. Uh
  2754. 2:00:51that's the summit bit the one that we
  2755. 2:00:53saw we could already use the exclusive
  2756. 2:00:54or and then we wanted to add this carry
  2757. 2:00:56functionality which we saw the andgate
  2758. 2:00:58lets us do
  2759. 2:01:00and I uh now you have built a circuit
  2760. 2:01:04that uh takes two one bit i.e e onedigit
  2761. 2:01:09binary values and outputs the sum of
  2762. 2:01:12them and I'm not going to trace through
  2763. 2:01:14every single example but uh I will do
  2764. 2:01:17one let's say we are adding one and one
  2765. 2:01:20then we can look at this gate we see a
  2766. 2:01:23and b are going into both of these gates
  2767. 2:01:25so one exclusive or one is zero but then
  2768. 2:01:29one and one is one um and so we have
  2769. 2:01:32this this this uh carry so our result we
  2770. 2:01:35know is in the order of we read it left
  2771. 2:01:37to right which is carry sum and so we
  2772. 2:01:39have a one for carry and a zero for sum
  2773. 2:01:42and if we interpret this as a binary
  2774. 2:01:44value then we have 1 zero which is two.
  2775. 2:01:47So this is really interesting. We've
  2776. 2:01:49we've learned about binary we thought it
  2777. 2:01:51was useless but uh we actually then
  2778. 2:01:54learned about logic circuits and built a
  2779. 2:01:57logic circuit that can add two one-digit
  2780. 2:02:01binary numbers together.
  2781. 2:02:03And this is really interesting
  2782. 2:02:06and I will let you suggest that you
  2783. 2:02:09pause this video and trace this circuit
  2784. 2:02:11through every case and make sure you
  2785. 2:02:13really understand what's happening here
  2786. 2:02:15because what's really happening is that
  2787. 2:02:17if we look at this uh there's one way to
  2788. 2:02:19justify this truth this this circuit is
  2789. 2:02:21by the truth table. But what I like to
  2790. 2:02:23do is I like to have an intuitive
  2791. 2:02:25understanding for why these circuits
  2792. 2:02:26work the way they do. And I think I kind
  2793. 2:02:28of did a a what I would hope was an
  2794. 2:02:31explanatory job with the selector
  2795. 2:02:32circuit we previously looked at. But for
  2796. 2:02:35this example, let's try to do a little
  2797. 2:02:36bit of rationing. Um there's only going
  2798. 2:02:39to be a carry if we are adding one and
  2799. 2:02:41one. That's the only case there's a
  2800. 2:02:43carry. So if there in any other case the
  2801. 2:02:45carry zero. So we can use an andgate
  2802. 2:02:47kind of to represent the carry logic. So
  2803. 2:02:50whenever there's a one and a one then
  2804. 2:02:52this and get triggered and that's the
  2805. 2:02:53only scenario in which we carry. And
  2806. 2:02:55then for this exclusive or, what that's
  2807. 2:02:58saying is if we're adding zero and zero,
  2808. 2:03:02then we don't want anything to be
  2809. 2:03:03outputed for the sum position. And if
  2810. 2:03:05we're adding a one and a one, we also
  2811. 2:03:07don't want anything to be included for
  2812. 2:03:09the sum position because there's going
  2813. 2:03:10to be a carryover. So we only want
  2814. 2:03:12something to be added if there's just
  2815. 2:03:14one one of the if if one of the two
  2816. 2:03:16inputs is a one. Exactly one of the two
  2817. 2:03:18inputs is a one. And that is just the
  2818. 2:03:20definition of the exclusive orgate. So
  2819. 2:03:22this works out perfectly.
  2820. 2:03:25So, we've built uh this circuit and it
  2821. 2:03:28turns out this circuit has a pretty uh
  2822. 2:03:32it's famous enough where it has its own
  2823. 2:03:34name and this is called the half adder.
  2824. 2:03:41And this is cool because if we're adding
  2825. 2:03:45two binary numbers 0 1 0 1 0 or 1 1 0 1
  2826. 2:03:501 then this circuit lets us add
  2827. 2:03:55each of the corresponding digit
  2828. 2:03:57positions together. Right?
  2829. 2:04:00And when we're adding numbers in binary
  2830. 2:04:02or decimal for that case we do it as we
  2831. 2:04:05can think of we actually divide up the
  2832. 2:04:07work. We look at each digit position at
  2833. 2:04:09a time. We start with the rightmost
  2834. 2:04:11digit position, do its work, and then
  2835. 2:04:14put the sum below and then do the next
  2836. 2:04:16digit position and put the sum below.
  2837. 2:04:17And this right here, this circuit is
  2838. 2:04:20effectively doing that for one digit
  2839. 2:04:23position.
  2840. 2:04:25Now, there is a little bit of a
  2841. 2:04:26shortcoming to this. What we'd like to
  2842. 2:04:29do is to be able to have these half
  2843. 2:04:31adders. If we want to add to in our
  2844. 2:04:34example, we have two five bit values,
  2845. 2:04:36fivedigit values. If we wanted to add
  2846. 2:04:39them together, we would like to use five
  2847. 2:04:41half adders and put them next to each
  2848. 2:04:44other. And so we could write in all the
  2849. 2:04:46operands values on uh each of the A
  2850. 2:04:49lines and then all the other ones on
  2851. 2:04:51each of the B lines um and and have a
  2852. 2:04:54circuit that could just produce the
  2853. 2:04:55output immediately.
  2854. 2:04:58But
  2855. 2:04:59this uh this unfortunately is missing
  2856. 2:05:03something. And I I I would actually take
  2857. 2:05:05it as a challenge to pause the video and
  2858. 2:05:07try think about why we can't do what
  2859. 2:05:09I've just described. Because in theory,
  2860. 2:05:12we've built a circuit that can add one
  2861. 2:05:14digit position. So why can't we just
  2862. 2:05:17have a bunch of these circuits next to
  2863. 2:05:19each other? Why couldn't we place five
  2864. 2:05:20of these next to each other and let that
  2865. 2:05:22be used to add the five-digit positions?
  2866. 2:05:27And I will spoil it now. The issue is
  2867. 2:05:31exactly what happens when we carry over.
  2868. 2:05:34So let's look here.
  2869. 2:05:37Let's do our little example. So we have
  2870. 2:05:40a one and a zero. If we fed this into
  2871. 2:05:43our into our half adder, then we would
  2872. 2:05:46get the output 01. So we'd have a one
  2873. 2:05:50for the sum and a zero we would carry
  2874. 2:05:52over. And so that works out fine. And
  2875. 2:05:56now if we do the input 1 one if we
  2876. 2:05:58suppose we had a half adder that did
  2877. 2:06:00this this computation we know it would
  2878. 2:06:02output a zero for the sum bit. So a zero
  2879. 2:06:04would go down here and then a one would
  2880. 2:06:07go up top for the carry bit. And now the
  2881. 2:06:10issue comes when we run into this digit
  2882. 2:06:12position here because we have three
  2883. 2:06:14values we need to add for this digit
  2884. 2:06:16position but our half adder only adds
  2885. 2:06:19two different values.
  2886. 2:06:22So there's an issue in that we output
  2887. 2:06:24the correct number of values because for
  2888. 2:06:26each digit position we have one we have
  2889. 2:06:28two outputs. We need to know what goes
  2890. 2:06:31down here and if we carry over to the
  2891. 2:06:33next position.
  2892. 2:06:36But our fatal shortcoming is that we are
  2893. 2:06:40missing the scenario for when there is
  2894. 2:06:42carryover. We actually have three digits
  2895. 2:06:44we want to be adding.
  2896. 2:06:47And this is something that we are not
  2897. 2:06:50accounting for in our circuit. So maybe
  2898. 2:06:54you guessed by the name, but half adder
  2899. 2:06:57uh lets us do half the adding logic, but
  2900. 2:06:59what we're looking for is a circuit
  2901. 2:07:00called a full adder, which lets us do
  2902. 2:07:03the exact logic. So we'd like to do is
  2903. 2:07:05we'd like to have our A and B, but we'd
  2904. 2:07:08also like to add a new line for carry
  2905. 2:07:11in. And so what we'll do is we're going
  2906. 2:07:16to have to build a new circuit that can
  2907. 2:07:18kind of handle this logic. So why don't
  2908. 2:07:20we try start with the truth table for
  2909. 2:07:22this. We have a b these are our two one
  2910. 2:07:26bit values and there could be a possible
  2911. 2:07:29carry in. And I'm not going to use a uh
  2912. 2:07:32I want to distinguish because our output
  2913. 2:07:34over here we had a C and now we're going
  2914. 2:07:36to have a C as the input. And that's
  2915. 2:07:37because there's a carry in and a carry
  2916. 2:07:39out and they're fundamentally different
  2917. 2:07:41as you know. So our output now is going
  2918. 2:07:44to be carry out. I almost wrote in
  2919. 2:07:47and then the sum. So we still have two
  2920. 2:07:50outputs but now we have three inputs.
  2921. 2:07:52So, let me uh go silent for a moment as
  2922. 2:07:54I populate this truth table.
  2923. 2:07:58[snorts]
  2924. 2:08:22Okay. So now let's take this truth table
  2925. 2:08:25and think about what this means. So now
  2926. 2:08:27we're adding effectively because of the
  2927. 2:08:29carry in bit we're adding three three
  2928. 2:08:33one-digit values.
  2929. 2:08:39And again we're still ignoring choose
  2930. 2:08:41complement. Uh we'll come back to this
  2931. 2:08:43later, but let's not over complicate
  2932. 2:08:45things now.
  2933. 2:08:47And let's see what happens. So uh I'm
  2934. 2:08:50going to go ahead and uh it's it's easy
  2935. 2:08:53to do these quickly because uh we're
  2936. 2:08:56working with zeros and ones. So 0 plus 0
  2937. 2:08:58plus 0 is these are all binary numbers.
  2938. 2:09:00But remember that uh 0 in binary is
  2939. 2:09:04equal to 0 in base 10 and one in binary
  2940. 2:09:07is equal to one in base 10. So we can
  2941. 2:09:09kind of ignore we don't need to convert
  2942. 2:09:11between binary because we don't need to
  2943. 2:09:13convert these binary operants these
  2944. 2:09:15binary inputs to our circuit to decimal
  2945. 2:09:18because for 0 and one they're equivalent
  2946. 2:09:20which just works out perfectly for us
  2947. 2:09:21because we don't have to overthink but
  2948. 2:09:23it can be tricky to sometimes wrap your
  2949. 2:09:26head around that. So we're adding 0 plus
  2950. 2:09:280 plus 0. So we know the output's just
  2951. 2:09:30going to be zero. Um
  2952. 2:09:33i.e. uh there's there's we're adding
  2953. 2:09:37zero. we need to represent that in two
  2954. 2:09:39digits. The output zero. The uh if we're
  2955. 2:09:42adding a zero and a zero in a digit
  2956. 2:09:44position, but the carry in is one, then
  2957. 2:09:47we know we output a one cuz that's what
  2958. 2:09:49we sum. And then there's no carry over
  2959. 2:09:50to the next digit position. We know we
  2960. 2:09:52just we just throw a one down below. So
  2961. 2:09:54that's fine. Then let's look what
  2962. 2:09:56happens if we're adding our A and B,
  2963. 2:09:58which are zero and one in the next row.
  2964. 2:10:01And then first off, we have no
  2965. 2:10:03carryover. So if we're just adding a
  2966. 2:10:04zero and a 1, then we sum a one with no
  2967. 2:10:08carry out. But if we have a carry in,
  2968. 2:10:12then we have 1 + 1, which is two. And we
  2969. 2:10:16know we put the remainder when you
  2970. 2:10:18divide by two below. So two divides into
  2971. 2:10:21two exactly. So there's no remainder.
  2972. 2:10:23And then we do have a carry out. So in
  2973. 2:10:25this case, we have a one for the carry
  2974. 2:10:26out, but a zero for the sum. And and
  2975. 2:10:29maybe rework that for yourself if it's
  2976. 2:10:30unclear what's happening. But you may
  2977. 2:10:32need to go back to the earlier part of
  2978. 2:10:34the video where I showed you how to do
  2979. 2:10:36addition of binary numbers.
  2980. 2:10:40Let's move on to the next case. Uh which
  2981. 2:10:42is when you have a one and a zero. And
  2982. 2:10:45the first one there is no carry-in. And
  2983. 2:10:48this is kind of what we looked at
  2984. 2:10:50before. You just have a one carries down
  2985. 2:10:52and there's no carryover. Uh so this is
  2986. 2:10:54a zero and a one. But if you do have a
  2987. 2:10:56carry-in then uh you have 1 plus 1 which
  2988. 2:11:00is two and two divides perfectly into
  2989. 2:11:02two. So the remainder is zero and then
  2990. 2:11:04we carry over a one. So we have a one
  2991. 2:11:06here.
  2992. 2:11:08And then the last case is last two cases
  2993. 2:11:11are when we're adding a one and a one.
  2994. 2:11:13So if we don't have any carry in then 1
  2995. 2:11:16+ 1 we already know is two. So the
  2996. 2:11:18output is that we carry out over once
  2997. 2:11:20and then put a zero below. And then the
  2998. 2:11:23uh edge case that is is bringing us here
  2999. 2:11:26uh is this 1 one one. Um and that means
  3000. 2:11:291 plus 1 plus 1 is three. And that means
  3001. 2:11:32we put the remainder down below. So
  3002. 2:11:34three would the remainder 1 divided by
  3003. 2:11:36two is one. And we have the carryover.
  3004. 2:11:38Um and so we have we have three up here.
  3005. 2:11:40This is actually three in base 10. And
  3006. 2:11:43you know this makes sense because this
  3007. 2:11:44is in binary. We have 11 one two in base
  3008. 2:11:472 which is equal to three in base 10. if
  3009. 2:11:50you do the conversion.
  3010. 2:11:55Cool. So, we have our truth table. Now,
  3011. 2:11:57let's think about how we can actually
  3012. 2:11:59build this circuit. [snorts]
  3013. 2:12:01And so, what I'd like to do for this is
  3014. 2:12:05go ahead and I think this is a difficult
  3015. 2:12:08one to build because we have three
  3016. 2:12:10inputs, which is more than two. So, we
  3017. 2:12:12have eight different cases we need to
  3018. 2:12:14handle and we have two outputs. But I
  3019. 2:12:17would like to take a logical approach to
  3020. 2:12:19building this. Not just spill out the
  3021. 2:12:21answer and let you prove it yourself,
  3022. 2:12:22but kind of give the answer um build up
  3023. 2:12:27and explain why it is we're doing what
  3024. 2:12:30we are doing. And so the way I would
  3025. 2:12:32think about this is
  3026. 2:12:35uh let's look at each of the outputs
  3027. 2:12:37separately. We could build a separate
  3028. 2:12:40out a separate circuit for each output.
  3029. 2:12:42We could think of building a separate
  3030. 2:12:43circuit for each output. Um and that's
  3031. 2:12:46because
  3032. 2:12:48uh they are two different outputs. We
  3033. 2:12:50could build two different circuits. Uh
  3034. 2:12:53one circuit that takes a b and c in and
  3035. 2:12:55outputs c out and another circuit that
  3036. 2:12:57takes a b and c in and outputs s. But uh
  3037. 2:13:00this would be inefficient because uh
  3038. 2:13:03computers are have to be physically
  3039. 2:13:06built. This computer that we're going to
  3040. 2:13:07build has to add numbers. They have to
  3041. 2:13:09be physically built and logicates cost
  3042. 2:13:12money in the real world to buy. uh they
  3043. 2:13:15are incredibly small. Your computer has
  3044. 2:13:17trillions of them, but they do incur
  3045. 2:13:20some cost. And so we would like to
  3046. 2:13:22design a circuit that minimizes the
  3047. 2:13:24number of logic gates used. When we're
  3048. 2:13:26building our computer, we would like to
  3049. 2:13:27minimize the amount of logic gates used.
  3050. 2:13:30And uh an easy way to minimize those is
  3051. 2:13:32to try combine the logic for these
  3052. 2:13:34circuits. Don't build a separate circuit
  3053. 2:13:36for the carry out and the sum. Just just
  3054. 2:13:38do it all together.
  3055. 2:13:42Okay, let's start by thinking about the
  3056. 2:13:45sum bit. So if we stack this in the uh
  3057. 2:13:49vertical kind of notation that you're
  3058. 2:13:52used to and that we've kind of seen
  3059. 2:13:54throughout this video multiple times now
  3060. 2:13:56we're interested we know this is the sum
  3061. 2:13:58bit. We know this is the carry carry out
  3062. 2:14:00bit and the carry in bit comes up top
  3063. 2:14:04and this is per bitwise operation. We're
  3064. 2:14:06just looking at one vertical digit
  3065. 2:14:08position. So let's figure out each of
  3066. 2:14:12the definitions in which the sum bit is
  3067. 2:14:15going to equal one. So if we ignore this
  3068. 2:14:18carryin bit then we know the sum is
  3069. 2:14:22going to equal one as long as either a
  3070. 2:14:25or b equals 1 but not both of them
  3071. 2:14:28because if so if neither of them equal
  3072. 2:14:30one then we we output nothing. uh in the
  3073. 2:14:32case where it's a 10 or a 01 then we
  3074. 2:14:35just output a one for the sum but if
  3075. 2:14:37they're both one then we actually output
  3076. 2:14:39a zero and then the carry out is going
  3077. 2:14:41to be what's equal to one. So in the
  3078. 2:14:45case where carry in equals zero. So
  3079. 2:14:47let's let's try make a rule here. So if
  3080. 2:14:50carry in equals zero then we want
  3081. 2:14:54exactly one of a or b to be true then
  3082. 2:14:59then sum is going to be exactly a
  3083. 2:15:02exclusive or b and uh recall what
  3084. 2:15:06exclusive or is from earlier. Exclusive
  3085. 2:15:08or says output a one if either a and or
  3086. 2:15:12b is true but not both of them. So this
  3087. 2:15:15is exactly what we're going for because
  3088. 2:15:16if A and B are both zero, the exclusive
  3089. 2:15:19or of those two values is going to be
  3090. 2:15:21zero. If A and B are going to be one,
  3091. 2:15:24they're both one, then the exclusive or
  3092. 2:15:26is going to be zero, which is synonymous
  3093. 2:15:28with the sum in these two cases. And for
  3094. 2:15:30any of the other two cases, we have one
  3095. 2:15:33of A or B is equal to one and the other
  3096. 2:15:35is equal to zero. And that's going to
  3097. 2:15:37output a one, which is what we'd expect.
  3098. 2:15:39So we've kind of built this rule out
  3099. 2:15:41where if the carry in is zero then the
  3100. 2:15:44sum bit is equal to a exclusive or b and
  3101. 2:15:47we have the exclusive or gate. So we can
  3102. 2:15:49kind of build that out. Uh but what
  3103. 2:15:52happens we need to complete the rule
  3104. 2:15:54then if the carry in is one.
  3105. 2:15:58So then if if I clean up the screen a
  3106. 2:16:00little bit here but if carry in is equal
  3107. 2:16:03to one then what is the sum going to be?
  3108. 2:16:05And for this one, we can actually
  3109. 2:16:07probably get a little more context out
  3110. 2:16:08of looking at the truth table. So, uh,
  3111. 2:16:11in this row, carry is one. In this row,
  3112. 2:16:13carry is one. And we have four rows
  3113. 2:16:16where Krion is equal to one. And we can
  3114. 2:16:19see that in this case, uh, sum is one
  3115. 2:16:23because and a and b are both ones. In
  3116. 2:16:26this case, sum is zero and only one of a
  3117. 2:16:30and b is a is a one. Over here we have
  3118. 2:16:33sum is zero and it's the same condition.
  3119. 2:16:35So it looks like when it's just one of a
  3120. 2:16:37or b i i.e if we have a exclusive or b
  3121. 2:16:41then sum's going to be zero and then if
  3122. 2:16:45we have not that then it looks like the
  3123. 2:16:47sum's going to be one. So we can
  3124. 2:16:49actually draw this rule up where if if
  3125. 2:16:51the carrying is one then s is going to
  3126. 2:16:55equal to not so I'll do like negative to
  3127. 2:16:59represent a negation
  3128. 2:17:01ax or b
  3129. 2:17:04and uh this is probably a good time to
  3130. 2:17:08pause this and and and verify this is
  3131. 2:17:10true that is if you're interested in in
  3132. 2:17:13knowing where this full adder circuit
  3133. 2:17:15that I'm I'm going to design comes from.
  3134. 2:17:17if you don't really need to understand
  3135. 2:17:18that technically to move on, but I just
  3136. 2:17:20thought your inner curiosity might be
  3137. 2:17:22satisfied if if you had a more complete
  3138. 2:17:24answer. So let's let's let's build this
  3139. 2:17:27out as a circuit then. So our our inputs
  3140. 2:17:30um and this is just for uh just for S.
  3141. 2:17:33So if our inputs are a b and carry n
  3142. 2:17:39then well if c in equals zero I'm going
  3143. 2:17:43to go ahead and spoil the most efficient
  3144. 2:17:46way to do this. It is probably a good
  3145. 2:17:48practice for you to try implement this
  3146. 2:17:50on your own but uh I will go ahead and
  3147. 2:17:53uh spoil this. So what you actually end
  3148. 2:17:56up doing uh is for the first part we
  3149. 2:17:58will use an exclusive ore which isn't
  3150. 2:18:02that surprising because exclusive or was
  3151. 2:18:04there and then we're going to actually
  3152. 2:18:07output this into another exclusive or
  3153. 2:18:12and the output of that exclusive ore is
  3154. 2:18:14going to be equal to y which is you know
  3155. 2:18:16let's not even call it y let's just call
  3156. 2:18:18it the sum that's what it is and the
  3157. 2:18:21other input of the exclusive ore is
  3158. 2:18:24going to be this right here this carry-
  3159. 2:18:26in. And so what this what this says is
  3160. 2:18:28if carry-in equals zero. So if carry-in
  3161. 2:18:31equals zero, then the output of this
  3162. 2:18:34exclusive or right here is just going to
  3163. 2:18:36be whatever flows in. So whatever flows
  3164. 2:18:38in uh will just flow straight through.
  3165. 2:18:41And that's because if we put a zero on
  3166. 2:18:43the top line, then zero exclusive or
  3167. 2:18:45zero is zero. And if we put a one up
  3168. 2:18:48here, then zero exclusive or one is one.
  3169. 2:18:51So uh if C in if the carry in is zero
  3170. 2:18:54then whatever comes in here will be the
  3171. 2:18:56output. And so what is it that comes in
  3172. 2:18:58here? It's a exclusive or b. So that's
  3173. 2:19:01what we've built out right here. We've
  3174. 2:19:03shown that when the carry in is zero
  3175. 2:19:05then the output is equal to this gate
  3176. 2:19:07and this gate is a exclusive or b.
  3177. 2:19:14Now if carryen is one let's see what
  3178. 2:19:16happens. Krion is one then the output
  3179. 2:19:20here will be the opposite of whatever
  3180. 2:19:22comes in because if if if we have a zero
  3181. 2:19:24coming in here then zero exclusive or
  3182. 2:19:27one is a one but if we have a one in
  3183. 2:19:29here one exclusive or one well that's
  3184. 2:19:32both inputs are being on and that's a
  3185. 2:19:33zero and so it kind of functions as a as
  3186. 2:19:36almost a negator in this context and uh
  3187. 2:19:39we know that whatever so we we know
  3188. 2:19:41that's the the negated of whatever is
  3189. 2:19:43coming in here and we already showed
  3190. 2:19:44that whatever coming in here is is A
  3191. 2:19:46exclusive or B and uh as it turns out
  3192. 2:19:49this is this is the most efficient way
  3193. 2:19:52to implement this in terms of minimizing
  3194. 2:19:55the number of gates that are being used.
  3195. 2:19:58So that's how we derive the sum bit.
  3196. 2:20:01What we're then interested in is the
  3197. 2:20:02carry out bit. So I'm going to erase
  3198. 2:20:04what we have right here.
  3199. 2:20:08And again, if you need to pause the
  3200. 2:20:11video and and convince yourself this is
  3201. 2:20:13true, then now is a great time to do
  3202. 2:20:15that. We have the sum bit. Let's look at
  3203. 2:20:17this carry out bit. We have this carry
  3204. 2:20:20out bit which is going off in a few
  3205. 2:20:23interesting cases.
  3206. 2:20:25And so we intuitively know that we're if
  3207. 2:20:29we're if we're stacking numbers, we can
  3208. 2:20:30look at it this this visual approach
  3209. 2:20:32again where we have our AB
  3210. 2:20:35and then we're adding them and then
  3211. 2:20:37we're also adding a carry-in. So, so
  3212. 2:20:40then if we look at this right here, if A
  3213. 2:20:43and B
  3214. 2:20:46together
  3215. 2:20:48sum to anything greater than one,
  3216. 2:20:51then we're going to have to carry out.
  3217. 2:20:53So, if if A and B are both set to one,
  3218. 2:20:56then we're for sure carrying out. And it
  3219. 2:20:58doesn't even matter what the carry in is
  3220. 2:21:00in this case because we know uh if the
  3221. 2:21:02carry-in is zero, then we output 1 zero.
  3222. 2:21:05And if the carry-in is one, then we
  3223. 2:21:07output 1. But in both of those cases,
  3224. 2:21:09we're carrying out. So for for
  3225. 2:21:13determining carrying out, if if A and B
  3226. 2:21:18are both equal to one, if A and B are
  3227. 2:21:22both equal one,
  3228. 2:21:26then we uh we know that carry out is
  3229. 2:21:28going to equal one. And there's not the
  3230. 2:21:31only case here. So uh that would cover
  3231. 2:21:34in our in our truth table that would
  3232. 2:21:37cover these two cases right here. A and
  3233. 2:21:40B are both one and we carry out. But
  3234. 2:21:42notice that uh we have two other cases
  3235. 2:21:44where A and B are not both one but we
  3236. 2:21:47still carry out. And what happens here?
  3237. 2:21:50And that's what happens when we only
  3238. 2:21:52have one of A and B. So a or b but we
  3239. 2:21:56also carry in cuz then we have 1 plus 1
  3240. 2:21:59effectively which which is a carry out 1
  3241. 2:22:01zero. So then if a exclusive or b
  3242. 2:22:09i i.e if there's just one of a or b is
  3243. 2:22:12set and
  3244. 2:22:14carry in is equal to one then we carry
  3245. 2:22:17out. So these are our our two conditions
  3246. 2:22:20for when we actually carry out and uh
  3247. 2:22:24let's go ahead now and and see how we
  3248. 2:22:26can build this up with logic gates. So
  3249. 2:22:29we we it's obviously we're going to need
  3250. 2:22:30some and gates. So uh let's start with
  3251. 2:22:33this first rule. If A and B both equal
  3252. 2:22:35to one, then we carry out. So I'm going
  3253. 2:22:38to add an AND gate down here. And I'm
  3254. 2:22:40just going to feed whatever A and B's
  3255. 2:22:43values are
  3256. 2:22:45into this gate. And uh we will start by
  3257. 2:22:48claiming that this is the carry out. Uh
  3258. 2:22:52so we know that if a and b are both
  3259. 2:22:54equal to one then we do carry out. But
  3260. 2:22:55there's another case uh it could either
  3261. 2:22:58be that or if we have a exclusive or b
  3262. 2:23:02and a carry in then we also carry out.
  3263. 2:23:05So there's actually two cases. So let's
  3264. 2:23:07let's implement the second case. Um and
  3265. 2:23:11I'm going to extend this line just cuz
  3266. 2:23:13it's going to get a little messy here if
  3267. 2:23:14I don't. Uh the second case is we need
  3268. 2:23:17to know if there's a exclusive or b
  3269. 2:23:19which we have right here. So we can
  3270. 2:23:21reuse this logic gate and this is what I
  3271. 2:23:23was talking about with optimizations.
  3272. 2:23:25So if we need a x or b so I'll just
  3273. 2:23:28bring this down. Um and we have a
  3274. 2:23:30carry-in. So we can just bring this down
  3275. 2:23:33too. And
  3276. 2:23:36this is another and gate. Uh then we're
  3277. 2:23:38saying that this should also mean we
  3278. 2:23:40carry out. But we want to carry out when
  3279. 2:23:42either of these lines are a one. So
  3280. 2:23:44let's join them with an orgate. We have
  3281. 2:23:46a gate that already handles this. This
  3282. 2:23:48gate will trigger if either of the
  3283. 2:23:52inputs is a one and that's an and not an
  3284. 2:23:55or. Here is an orgate. And we can call
  3285. 2:23:59this carry out. And this uh as it turns
  3286. 2:24:04out is what we call a full adder.
  3287. 2:24:09And I've done a better job of drawing
  3288. 2:24:11this up here. So let me go ahead and
  3289. 2:24:16bring it down
  3290. 2:24:18and let's just look at this drawing
  3291. 2:24:21which is not as as painful to look at as
  3292. 2:24:23the one I I have provided. Uh so I will
  3293. 2:24:27respectfully
  3294. 2:24:28uh move this out of the way.
  3295. 2:24:31Okay. And I'm also going to do a little
  3296. 2:24:34cleanup here just because I'd like this
  3297. 2:24:36to be referenceable later in case I need
  3298. 2:24:38to. But this circuit right here is a
  3299. 2:24:41full adder. And uh this is a another
  3300. 2:24:45great opportunity. Let me go ahead and
  3301. 2:24:46erase this to convince yourself that
  3302. 2:24:49this is true. Uh if you would like the
  3303. 2:24:51exercise, go through take this circuit,
  3304. 2:24:54I'll zoom out the perfect amount. Uh
  3305. 2:24:56pause this video and for each of the
  3306. 2:24:58rows you see in this truth table,
  3307. 2:25:01compute what the output of the circuit
  3308. 2:25:02should be and verify that it's equal to
  3309. 2:25:04what you see on the right hand side over
  3310. 2:25:06here. If it's not, then there's
  3311. 2:25:08something wrong with your logic or I
  3312. 2:25:10made a mistake in this video, which I
  3313. 2:25:12will hopefully correct in the
  3314. 2:25:13description or someone will point out in
  3315. 2:25:15the comments if if that if someone ever
  3316. 2:25:17watches it. And
  3317. 2:25:20what we have is this really cool circuit
  3318. 2:25:23which if we're doing some sort of binary
  3319. 2:25:26addition,
  3320. 2:25:29then can correctly do the addition for
  3321. 2:25:32each one of these these vertical
  3322. 2:25:35positions. So for each digit position,
  3323. 2:25:38this full adder can perform the correct
  3324. 2:25:40addition. And where we're going to go
  3325. 2:25:43with this is we're actually going to
  3326. 2:25:44build a circuit that can add uh 8 bit
  3327. 2:25:47numbers together because we said our
  3328. 2:25:49computer is 8 bits. So let's let's get
  3329. 2:25:50back to the realm of our computer.
  3330. 2:25:53And that circuit's going to work by us
  3331. 2:25:55effectively having eight full adders
  3332. 2:25:58together. And we're actually going to
  3333. 2:25:59chain them together because the carry
  3334. 2:26:02out from this add adder right here
  3335. 2:26:04should become the carry into this adder.
  3336. 2:26:06And then the carry out from this adder
  3337. 2:26:08should become the carry into this adder.
  3338. 2:26:10And then the carry out from that adder
  3339. 2:26:11should be the carry into this adder. And
  3340. 2:26:13so on and so on. And it might be a
  3341. 2:26:16little bit vague what I'm trying to say,
  3342. 2:26:17but hopefully at a high level you
  3343. 2:26:18understand that the the carry out from
  3344. 2:26:22this position right here becomes the
  3345. 2:26:24carry in over here. And then the carry
  3346. 2:26:26out for this sum becomes the carry-in
  3347. 2:26:29over here. And we've built this
  3348. 2:26:31generalized circuit which can handle any
  3349. 2:26:34of the possible input combinations and
  3350. 2:26:36correctly give us the the sum bit and
  3351. 2:26:38the carry out bit. And now I hopefully
  3352. 2:26:42convinced you that we've been able to
  3353. 2:26:44tie together the first fundamental
  3354. 2:26:46concepts we learned in this video. Uh
  3355. 2:26:49that being binary and logic gates. We
  3356. 2:26:52now see a bridge between these seemingly
  3357. 2:26:54distinct concepts and we're going to
  3358. 2:26:57work on drawing them together. And so
  3359. 2:26:59we're actually going to work on building
  3360. 2:27:00our first part of the computer which is
  3361. 2:27:02called the ALU, the arithmetic and logic
  3362. 2:27:05unit. And that's going to be responsible
  3363. 2:27:07for adding and subtracting numbers. So
  3364. 2:27:10[snorts] I will uh go ahead and move on.
  3365. 2:27:14Now's the last chance to pause this and
  3366. 2:27:16just make sure you understand everything
  3367. 2:27:18that's on the screen. But we'll move on
  3368. 2:27:20and see how we can build on top of these
  3369. 2:27:22full adders and combine them together to
  3370. 2:27:24build a circuit that can do more
  3371. 2:27:27non-trivial addition because right now
  3372. 2:27:29this is trivial. This adds uh this adds
  3373. 2:27:32technically it's meant to add one digit
  3374. 2:27:34position together but it could add three
  3375. 2:27:36binary numbers three binary digits
  3376. 2:27:38should I say and and tell you what the
  3377. 2:27:40output's going to be. And you should
  3378. 2:27:42also be able to convince yourself that
  3379. 2:27:44uh for any three binary digits, the sum
  3380. 2:27:47of those is going to be somewhere in the
  3381. 2:27:50range of 0 0 which is zero or 1 one
  3382. 2:27:54which is three in binary uh
  3383. 2:27:58three in decimal one in binary. And you
  3384. 2:28:00should be able to see that this fits
  3385. 2:28:02within two different uh bit positions.
  3386. 2:28:06So there should be no doubt in your mind
  3387. 2:28:08that for whatever values the inputs are,
  3388. 2:28:11we are able to correctly represent the
  3389. 2:28:12output without any loss of memory or
  3390. 2:28:16information representation.
  3391. 2:28:18Okay. So before we actually do the uh
  3392. 2:28:21expand this out to multiple bit
  3393. 2:28:23addition, let's go ahead and and give
  3394. 2:28:25some notation so it makes sense. So I
  3395. 2:28:28guess the example we just built out, we
  3396. 2:28:30did something along the lines of A and B
  3397. 2:28:34and we get from that a sum and then we
  3398. 2:28:38also possibly get a carry out carry out.
  3399. 2:28:44So there's there's there's two inputs
  3400. 2:28:46and two outputs for each digit position.
  3401. 2:28:49And this happens in regular addition as
  3402. 2:28:52well as as well as binary edition. So if
  3403. 2:28:55I give you two numbers, say 74 and 83,
  3404. 2:29:02then we could say that for this digit
  3405. 2:29:04position right here, the sum is seven.
  3406. 2:29:08And then there's no carryover. So we'll
  3407. 2:29:09just say the carry the carryover is
  3408. 2:29:11zero. And then for this digit position
  3409. 2:29:15right here, the sum is five and the
  3410. 2:29:18carryover is a one, which comes down.
  3411. 2:29:21And the same thing happens in binary. So
  3412. 2:29:24if I gave you one zero and then one one
  3413. 2:29:28then here when we're adding this digit
  3414. 2:29:30position we sum a one and then there's
  3415. 2:29:33no carryover. So we can implicitly write
  3416. 2:29:35a zero for the carry. And here we have a
  3417. 2:29:38sum and a carry which comes down.
  3418. 2:29:43So that's just a little bit of recap. So
  3419. 2:29:46now let's say we want to have two bit
  3420. 2:29:49numbers we want to add. That's what I
  3421. 2:29:51said we would build up to now. So let's
  3422. 2:29:53give some notation. So A and B are now
  3423. 2:29:56our two operandines are two bits. So
  3424. 2:29:59let's let's give them indexes to make
  3425. 2:30:02this more apparent what's happening. So
  3426. 2:30:03we can say we have A2 and A1 and we're
  3427. 2:30:08adding it to B2 and B1.
  3428. 2:30:11And then from this we would get a sum
  3429. 2:30:14down here. And we're actually going to
  3430. 2:30:16get two sum bits. So let's have a way of
  3431. 2:30:18distinguishing them. And I think it
  3432. 2:30:20makes sense naturally to give them the
  3433. 2:30:22same indexes that we did for the the
  3434. 2:30:24operands. So we get a sum one and then
  3435. 2:30:28we get a possible carry over one. And
  3436. 2:30:31then for this digit position, we get a
  3437. 2:30:34sum two and possibly a carryover from
  3438. 2:30:39two. And if that's the case, then we we
  3439. 2:30:41carry it down. And again, carryover is
  3440. 2:30:44either going to be a zero or one. So it
  3441. 2:30:46makes sense that if we have the zero and
  3442. 2:30:48then some binary digits, it's the same
  3443. 2:30:50thing as just the binary digits that we
  3444. 2:30:53saw earlier.
  3445. 2:30:55And so
  3446. 2:30:57what this means, and I'll go ahead and
  3447. 2:30:59erase this
  3448. 2:31:01to stop the screen from getting too
  3449. 2:31:02crowded, is we are going to effectively
  3450. 2:31:05use two of these full adders we've
  3451. 2:31:08described and
  3452. 2:31:12chain them together with these operands
  3453. 2:31:14as so. And so what we're going to see is
  3454. 2:31:16that the uh first full adder is going to
  3455. 2:31:20take this a1 and b1 as its operands. And
  3456. 2:31:23then we're going to treat the sum output
  3457. 2:31:26as someone. And then what we're actually
  3458. 2:31:28going to do with the carry out this C
  3459. 2:31:31this carry out uh that we have right
  3460. 2:31:34here is we're going to feed it into the
  3461. 2:31:36carry in of the second h full adder. And
  3462. 2:31:40the second full adder is going to be
  3463. 2:31:42operating on A2 and B2.
  3464. 2:31:45So we can kind of see how these get
  3465. 2:31:47chained together.
  3466. 2:31:49And so why don't we go ahead and try
  3467. 2:31:51build that. And luckily because I'm
  3468. 2:31:53drawing on my iPad, I can just duplicate
  3469. 2:31:57this right here. And I'm going to paste
  3470. 2:32:00it twice below.
  3471. 2:32:03And we will give the proper notation. So
  3472. 2:32:06let's not this I have up there.
  3473. 2:32:10And
  3474. 2:32:13I'm going to copy this again and paste
  3475. 2:32:16it one more time right here.
  3476. 2:32:19Okay. So now what I'm saying and I think
  3477. 2:32:22this will make a little more sense now
  3478. 2:32:25is
  3479. 2:32:26let me move this over here.
  3480. 2:32:30Okay. So if I have A1 and B1 I want to
  3481. 2:32:35use this
  3482. 2:32:39half adder and let me actually yeah I'm
  3483. 2:32:42going to use this this full adder.
  3484. 2:32:45Sorry, not half adder, full adder to do
  3485. 2:32:48the A1 plus B1. So what I do is I'm
  3486. 2:32:51going to feed in A1 and B1. And this is
  3487. 2:32:56the the the
  3488. 2:32:58rightmost digit of the two numbers we're
  3489. 2:33:00trying to to operate on. And now you
  3490. 2:33:04might be wondering, this is a good
  3491. 2:33:05question. What do we do for carry in?
  3492. 2:33:08Because for this rightmost digit
  3493. 2:33:09position, we're never going to have a
  3494. 2:33:11carry in. I mean, if if we think about
  3495. 2:33:13it, that's where we start our addition
  3496. 2:33:14at. So, how could there already be
  3497. 2:33:17something before that to which we could
  3498. 2:33:18have carried into? And the answer is
  3499. 2:33:20there's nothing. There's no way we could
  3500. 2:33:22ever have a carry in on the rightmost
  3501. 2:33:23digit position. But we do know is that
  3502. 2:33:27what we're effectively doing is a1 + b1
  3503. 2:33:29plus the carry in.
  3504. 2:33:32That if if we're doing this plus this
  3505. 2:33:36plus this, and we know that the carry in
  3506. 2:33:37is zero, then we're really just left
  3507. 2:33:39with a1 plus b1.
  3508. 2:33:43And so what we're going to do is just
  3509. 2:33:45hardwire this right here to be a zero
  3510. 2:33:50because there's no way we could have
  3511. 2:33:51carried into our first bit of addition.
  3512. 2:33:54And then that's going to let us use this
  3513. 2:33:56this this full adder we have to do
  3514. 2:33:58exactly what we want. So now let me go
  3515. 2:34:00ahead and erase what we have marked for
  3516. 2:34:03these outputs. And let's say that the
  3517. 2:34:06output of this is going to be S1. and
  3518. 2:34:10then the carry out. So this is carry
  3519. 2:34:14one. We're going to feed that in to the
  3520. 2:34:18carry in of this next full adder.
  3521. 2:34:24And and visually that is the arrow we
  3522. 2:34:27see right let me erase these. That is
  3523. 2:34:30the arrow we see right here. When we add
  3524. 2:34:33A1 and B1 we get the sum one down below.
  3525. 2:34:35And then we may have a carry out a
  3526. 2:34:38carryover from S1 from that first
  3527. 2:34:41edition into the second edition. And
  3528. 2:34:43that's exactly what this arrow
  3529. 2:34:44represents.
  3530. 2:34:46So now for this second full adder, we
  3531. 2:34:49will feed in not a1 and a b1 but a2 and
  3532. 2:34:53b2. So now I'll throw in a2
  3533. 2:34:57and b2. And from this we get
  3534. 2:35:02sum two and then carry two. So now you
  3535. 2:35:07can see here that if we have and let me
  3536. 2:35:10go ahead and erase this cuz I think it's
  3537. 2:35:12a little complicated. If we had two
  3538. 2:35:16operands we wanted to add and both of
  3539. 2:35:18those operands were two binary numbers,
  3540. 2:35:20two binary digits, then we could simply
  3541. 2:35:23plug them into this circuit right here
  3542. 2:35:25and we would get the sum as something we
  3543. 2:35:27could read from S1 and S2 combined with
  3544. 2:35:32C2. And I should not have erased that.
  3545. 2:35:35And this C1, it's not that it's useless,
  3546. 2:35:37but it doesn't actually show up in our
  3547. 2:35:39output. We use it as part of the
  3548. 2:35:41computation, but we only care about
  3549. 2:35:43these three for our output because these
  3550. 2:35:45are what we actually read and that's
  3551. 2:35:46what we want to know. We want to know
  3552. 2:35:47the sum of the value. We don't really
  3553. 2:35:49care about where the carryover happen
  3554. 2:35:51intermediately and internally. We just
  3555. 2:35:53need to know that we make sure that we
  3556. 2:35:54deal with it correctly when it does
  3557. 2:35:55happen.
  3558. 2:35:57So let's go ahead and plug in a little
  3559. 2:35:59bit of an example here. Uh just to make
  3560. 2:36:01sure that this works. Suppose I wanted
  3561. 2:36:03to add one zero. Sorry, I wanted to add
  3562. 2:36:07the binary numbers 1 1 and 0 1. And this
  3563. 2:36:12in binary is going to be equal to 3 + 1.
  3564. 2:36:18So 3 sub in base 10 + one in base 10.
  3565. 2:36:22And we know we should expect four back
  3566. 2:36:23in base 10. And we can do this
  3567. 2:36:26arithmetic manually. And we would see
  3568. 2:36:29that we know the 1 + 1 is a zero, but we
  3569. 2:36:33carry over a 1. And then we know that
  3570. 2:36:35this 1 plus one also a zero but we carry
  3571. 2:36:38over again another one and then that one
  3572. 2:36:40drops down. So we can kind of see that
  3573. 2:36:43we would expect this to be s1 this is
  3574. 2:36:46sub2 and this is the c2. And then we
  3575. 2:36:49know that this is we know how a1 and a2
  3576. 2:36:52are structured based on this diagram
  3577. 2:36:53over here.
  3578. 2:36:55And uh sure enough uh we if we plug this
  3579. 2:36:59into our circuit we expect to get that.
  3580. 2:37:01So let's go ahead and do this. So I'm
  3581. 2:37:03going to plug in
  3582. 2:37:07to our circuit. We have a one and a one.
  3583. 2:37:11This is the A1 and B1. And then a one
  3584. 2:37:13and a zero for the A2 and B2. And we
  3585. 2:37:17have to start with this circuit on the
  3586. 2:37:18right because you notice the circuit on
  3587. 2:37:20the left. One of the inputs
  3588. 2:37:23comes from this line over here. And
  3589. 2:37:25that's actually the output of the
  3590. 2:37:26circuit on the right. So we'll have to
  3591. 2:37:27start by doing this first. So we have
  3592. 2:37:30one exclusive or one which is a zero.
  3593. 2:37:33And then we have to this right here we
  3594. 2:37:36have zero exclusive or zero which we
  3595. 2:37:39know is just a zero. So sum one I'm
  3596. 2:37:43saying a zero. And that that checks up
  3597. 2:37:44with what we have here. And then I'm
  3598. 2:37:47saying let's come down to this andgate
  3599. 2:37:49over here. I am feeding that a one and a
  3600. 2:37:51one. So I'm going to output a one from
  3601. 2:37:53that andgate. And then over here I have
  3602. 2:37:57a zero and a zero going into this gate.
  3603. 2:38:00So this is going to output a zero, but
  3604. 2:38:02they feed into an orgate. So we're going
  3605. 2:38:04to have a one or zero. And that means
  3606. 2:38:06we're going to output a one. And so that
  3607. 2:38:08means that carry one should be one. And
  3608. 2:38:11this makes sense because we have a one
  3609. 2:38:13up here. And that's carry one. You know,
  3610. 2:38:15this should be carry one and this should
  3611. 2:38:17be carry two.
  3612. 2:38:20So then we bring this one all the way
  3613. 2:38:21around here. And we can do the same
  3614. 2:38:24calculation for this full ladder. So we
  3615. 2:38:29have one exclusive or zero which is a
  3616. 2:38:31one. And then we have
  3617. 2:38:35this one exclusive or one going into S2
  3618. 2:38:39which means we're going to output a zero
  3619. 2:38:41cuz one exclusive or one is zero cuz
  3620. 2:38:43both the inputs are set. And that checks
  3621. 2:38:45out with what we have here. And then we
  3622. 2:38:48come down to this and gate over here and
  3623. 2:38:50we have one and zero which means we
  3624. 2:38:52output a zero. And then this and gate
  3625. 2:38:54down below is one and one which means we
  3626. 2:38:56output a one and then when we or those
  3627. 2:38:59we get a one and this is exactly what we
  3628. 2:39:02see. So this is really interesting. We
  3629. 2:39:06have the two full adders chained
  3630. 2:39:09together and now we're able to do two
  3631. 2:39:12bit addition. We're able to add numbers
  3632. 2:39:14that are two bits in length. But one
  3633. 2:39:16thing you may be noticing is that this
  3634. 2:39:18is pretty ugly to look at. We're having
  3635. 2:39:20a lot of logic gates and it's not
  3636. 2:39:24visually appealing to look at. And I
  3637. 2:39:25kind of hinted at this earlier, but we
  3638. 2:39:27can kind of abstract this away. So
  3639. 2:39:30instead of drawing this ugly circuit
  3640. 2:39:32out, let's find a way that we can just
  3641. 2:39:35represent the full adder as something
  3642. 2:39:37easier. And let's just give it a square.
  3643. 2:39:40Let's just say that if we have a square,
  3644. 2:39:42sorry, a rectangle with full adder FA
  3645. 2:39:46written in it, we just know it means a
  3646. 2:39:48full adder. And what we really need to
  3647. 2:39:49show is the inputs and outputs cuz we
  3648. 2:39:52now understand how the full adder works
  3649. 2:39:53internally. But when we use it to build
  3650. 2:39:55more complicated circuits, we don't
  3651. 2:39:57necessarily need to see all the logic
  3652. 2:39:59gates inside of it. We just need to be
  3653. 2:40:00able to represent what goes into it and
  3654. 2:40:02what comes out of it. And so we know
  3655. 2:40:04that for a full adder the inputs are an
  3656. 2:40:07A, a B and the carry in. And then the
  3657. 2:40:11outputs are a sum bit and a carry out
  3658. 2:40:13bit. And all you need to understand is
  3659. 2:40:17that this full adder I've this this this
  3660. 2:40:19rectangle which I'm claiming to be a
  3661. 2:40:22full adder. If we really wanted to see
  3662. 2:40:24what was inside this rectangle, it would
  3663. 2:40:26be this right here, a full adder.
  3664. 2:40:31So if we assume this diagram to be true,
  3665. 2:40:33and this is something we're allowed to
  3666. 2:40:34do. We're we're building our computer.
  3667. 2:40:36So we don't want to have just logicates
  3668. 2:40:38littered everywhere. What we're going to
  3669. 2:40:40like to do is build out individual
  3670. 2:40:41components of our computer and then
  3671. 2:40:44abstract them away in these rectangles.
  3672. 2:40:46So what I'd like to do now is if you
  3673. 2:40:48understand or at least can believe that
  3674. 2:40:50this is going to be used to represent a
  3675. 2:40:52full adder, let's rebuild the circuit
  3676. 2:40:54that I've drawn up here and show how
  3677. 2:40:56much easier it is. And to do that, all
  3678. 2:40:58we need is another full ladder,
  3679. 2:41:02which I will copy and paste.
  3680. 2:41:08And all we now need to do is update our
  3681. 2:41:10inputs and outputs. So, we know that
  3682. 2:41:12we're feeding in a zero here because we
  3683. 2:41:14don't carry in to our first full ladder.
  3684. 2:41:17And then we know that this is A1 and B1.
  3685. 2:41:19And we know that this output is S1.
  3686. 2:41:22And then we feed this carry over into
  3687. 2:41:26the carry in of the next full ladder.
  3688. 2:41:30And then we're calling this A2 and B2.
  3689. 2:41:33And then from there we get the sum two
  3690. 2:41:35and carry two. And look how much easier
  3691. 2:41:38this is to visualize compared to the
  3692. 2:41:40mess of logic gates we had above.
  3693. 2:41:43So this is pretty interesting. We've now
  3694. 2:41:47learned how to do two bit addition. So
  3695. 2:41:49we started with one bit addition. We've
  3696. 2:41:51done two bit addition, but now I'd go
  3697. 2:41:53ahead and like to show you how our
  3698. 2:41:55computer's going to do addition. And it
  3699. 2:41:57turns out it's nothing new. What I said
  3700. 2:42:00earlier is that our computer is an 8bit
  3701. 2:42:02computer. 8bit computer, CPU, central
  3702. 2:42:06processing unit. And what this means is
  3703. 2:42:09we operate we operate
  3704. 2:42:13on 8bit values.
  3705. 2:42:18And to elaborate on that, every value we
  3706. 2:42:22store in our computer is going to be
  3707. 2:42:24eight binary digits long. Nothing more
  3708. 2:42:27and nothing less. And this makes this
  3709. 2:42:29makes things easier. As you will
  3710. 2:42:31eventually see, if every single
  3711. 2:42:34component of our computer is expecting
  3712. 2:42:37values to come in eight bits, exactly
  3713. 2:42:40eight digits, then we can standardize a
  3714. 2:42:43lot of components of our computer and it
  3715. 2:42:45makes ensures a lot of consistency
  3716. 2:42:47between the different components. And
  3717. 2:42:49this eight, as I also mentioned earlier,
  3718. 2:42:51is just a random number. Modern
  3719. 2:42:53computers can use 32 or 64. And we'll
  3720. 2:42:56we'll we're going to build an 8-bit
  3721. 2:42:57computer. And then we'll eventually talk
  3722. 2:42:58about how we can scale up to 32 or 64.
  3723. 2:43:01And it turns out it's not that
  3724. 2:43:02difficult. But what I'd like to do then
  3725. 2:43:05is build a circuit that adds 8 bit
  3726. 2:43:08numbers. So what would this look like?
  3727. 2:43:11Every input to our circuit is going to
  3728. 2:43:14take in A1 through A8 and then
  3729. 2:43:19A B1 through B8. So now all of our
  3730. 2:43:23operands, we can still we're just
  3731. 2:43:24building a circuit that just adds
  3732. 2:43:26numbers.
  3733. 2:43:27But we're going to go ahead
  3734. 2:43:30and make sure that those numbers are all
  3735. 2:43:33eight bits. They all have eight binary
  3736. 2:43:34digits. And so if you understand what we
  3737. 2:43:37did up here, then it turns out the
  3738. 2:43:39process for eight bits is not any more
  3739. 2:43:41complicated. Instead of two full adders
  3740. 2:43:45for adding two bit numbers, we're just
  3741. 2:43:47going to use eight full adders for
  3742. 2:43:49adding eight bit numbers.
  3743. 2:43:51And this ends up making things look
  3744. 2:43:54pretty easy. So I'll go ahead and give
  3745. 2:43:56the expanded notation just so we have
  3746. 2:43:58something to work with. But in general,
  3747. 2:43:59if we're adding two 8-bit numbers, what
  3748. 2:44:01it looks like in the graphical form that
  3749. 2:44:03you and I are both used to is we'd have
  3750. 2:44:0688 and then our A value has eight
  3751. 2:44:10different bits in it.
  3752. 2:44:17And our B value has eight different bits
  3753. 2:44:19in it.
  3754. 2:44:22And if you are a computer person, you
  3755. 2:44:25may be wondering why through this point
  3756. 2:44:27in the video I've been doing
  3757. 2:44:35one indexing. I've been starting at
  3758. 2:44:37index one and going onwards. Uh even
  3759. 2:44:39though you know that in computer science
  3760. 2:44:42we start at index zero and move onwards.
  3761. 2:44:44And the reason I'm doing this is because
  3762. 2:44:46at the start of this video I said I
  3763. 2:44:47wanted it to be understandable for
  3764. 2:44:48everybody. Even if you didn't have a
  3765. 2:44:50computer science background and to those
  3766. 2:44:54of us who do not have a computer science
  3767. 2:44:55background, it is my belief that
  3768. 2:44:58indexing starting it based on one is
  3769. 2:45:00more intuitive to understand compared to
  3770. 2:45:03indexing based at zero. It's one of the
  3771. 2:45:06things that I mentally had to adjust to
  3772. 2:45:08when I started learning computer
  3773. 2:45:09science. So, I figured it'd be easier if
  3774. 2:45:11I just didn't make the viewer
  3775. 2:45:13additionally bear that burden of having
  3776. 2:45:15to mentally shift everything over by one
  3777. 2:45:17as they watch. So, for those of you who
  3778. 2:45:19do have a computer science background, I
  3779. 2:45:22think it's not too painful for you to
  3780. 2:45:24mentally put the zero indexing away and
  3781. 2:45:26go back to one indexing for this video.
  3782. 2:45:28And for those of you who have no
  3783. 2:45:30computer science background, you don't
  3784. 2:45:31even understand what I'm talking about.
  3785. 2:45:33And this is intuitively how you would
  3786. 2:45:36represent things to begin with. [snorts]
  3787. 2:45:38Cool. So, these are our two operands.
  3788. 2:45:41We're just looking at addition. And for
  3789. 2:45:43now, we are assuming that they are
  3790. 2:45:45positive numbers. We will after we build
  3791. 2:45:48an addition, we will come back and look
  3792. 2:45:49at subtraction and and negative numbers.
  3793. 2:45:52But we would expect then you when we're
  3794. 2:45:54doing this addition, you'd start with
  3795. 2:45:55the A1 and B1. You get a sum one and
  3796. 2:45:58then you get a carry one. And then you
  3797. 2:46:01would add those A2 B2 and the carry one
  3798. 2:46:04together and you get a sum two and a
  3799. 2:46:06carry two. And then you'd add A3 V3 and
  3800. 2:46:10the previous carryover and you would get
  3801. 2:46:12sum three and carry three. And this
  3802. 2:46:15process would repeat
  3803. 2:46:31and then the one interesting is the very
  3804. 2:46:33last addition. We take the carry 8 and
  3805. 2:46:38if it exists or not we bring it down. So
  3806. 2:46:42this is the the addition we have and
  3807. 2:46:46all we need to do is chain eight of
  3808. 2:46:49these full adders together. So I will
  3809. 2:46:53start by drawing that out. And this is
  3810. 2:46:55not going to be a pretty drawing because
  3811. 2:46:56I don't have good handwriting. So I am
  3812. 2:46:58just going to go silent for a minute as
  3813. 2:47:00I draw this out because I find that the
  3814. 2:47:01more I try talk while drawing the more
  3815. 2:47:04errors I make.
  3816. 2:47:11So, we start with a full ladder and
  3817. 2:47:14we're going to feed in A1
  3818. 2:47:17and B1. And then we actually feed in
  3819. 2:47:20this hard-coded zero for the carry-in.
  3820. 2:47:23And then we will take a another full
  3821. 2:47:25adder.
  3822. 2:47:28And I probably drew that a little too
  3823. 2:47:30close together.
  3824. 2:47:32And from the output of this one, we get
  3825. 2:47:37I'm going to bring this down here just
  3826. 2:47:40so it's easier to read. We get the sum
  3827. 2:47:43one and then the carry one is going to
  3828. 2:47:47come over here.
  3829. 2:47:49And
  3830. 2:47:51for this one, we will feed in A2 and B2.
  3831. 2:47:58And this is still a full adder. and we
  3832. 2:48:01get sum two coming out
  3833. 2:48:06and then we're gonna have the out the
  3834. 2:48:08carryover feed into the next full adder.
  3835. 2:48:10So then we have another full adder
  3836. 2:48:14and we're going to feed into here A3 and
  3837. 2:48:18B3
  3838. 2:48:21and don't forget about the carry out
  3839. 2:48:25that's going to come in. And then from
  3840. 2:48:27here we get sum three.
  3841. 2:48:32And I'm going to move this over before I
  3842. 2:48:34run out of room.
  3843. 2:48:38And then
  3844. 2:48:40what we're going to do is we're going to
  3845. 2:48:41have another full ladder.
  3846. 2:48:47And that's going to take in A4
  3847. 2:48:52and B4
  3848. 2:48:54and the carry out from the previous
  3849. 2:48:57adder.
  3850. 2:48:59And then we're going to have the sum for
  3851. 2:49:03and the carry for. Then we have another
  3852. 2:49:05full adder.
  3853. 2:49:10We feed into this one A5 and B5.
  3854. 2:49:16And then we also feed in the carry out
  3855. 2:49:20from the previous full adder. And we now
  3856. 2:49:24get sum five in the next carry out.
  3857. 2:49:28And then we will have another full
  3858. 2:49:30adder.
  3859. 2:49:31I know this is getting repetitive and
  3860. 2:49:33that's because it it is. We've designed
  3861. 2:49:34this so that we don't have to build out
  3862. 2:49:36this massive circuit that handles every
  3863. 2:49:38possible value of the two combinations
  3864. 2:49:41of 8bit inputs. We've built this
  3865. 2:49:44something simple circuit, this full
  3866. 2:49:45adder that lets us reuse everything. And
  3867. 2:49:49once again, we feed in A6 and B6.
  3868. 2:49:53And then we also take the carryover from
  3869. 2:49:56the last one.
  3870. 2:49:58And
  3871. 2:50:00from here, we output S6.
  3872. 2:50:04And we are getting so close.
  3873. 2:50:08We have a full adder.
  3874. 2:50:11We get an A7 and B7
  3875. 2:50:15and the carryover from this last one
  3876. 2:50:21and we get S7. And now we have our last
  3877. 2:50:24full adder
  3878. 2:50:27where we take in A8 and
  3879. 2:50:32B8.
  3880. 2:50:35we get this S8.
  3881. 2:50:37And then we're also going to get this
  3882. 2:50:41carryover from eight. And the last thing
  3883. 2:50:44we need to do is make sure that we chain
  3884. 2:50:46this carry in. Cool.
  3885. 2:50:50And I'm not even actually allowed to
  3886. 2:50:51zoom out that far on my my iPad without
  3887. 2:50:54it complaining. So I'm going to hold it
  3888. 2:50:57here and let's look at what we have.
  3889. 2:51:00We've now built a circuit where we can
  3890. 2:51:02put in any two 8digit values into our
  3891. 2:51:07input lines and we'd be able to output
  3892. 2:51:09their sum. And I'm not going to work
  3893. 2:51:12through an example because I'd probably
  3894. 2:51:14take me just as long as the entire video
  3895. 2:51:16up till now to look at each of the logic
  3896. 2:51:18gates for each of these full address.
  3897. 2:51:20But you should be able to convince
  3898. 2:51:22yourself this is true. And this is
  3899. 2:51:24something that's really novel. We've
  3900. 2:51:25built something simple and we've chained
  3901. 2:51:28it together to build something more
  3902. 2:51:29complicated.
  3903. 2:51:31And this is a fundamental principle of
  3904. 2:51:33computer science. It's called
  3905. 2:51:34abstraction. We've built the circuit
  3906. 2:51:37that's a full adder and we then we've
  3907. 2:51:39abstracted away by just drawing it as a
  3908. 2:51:41rectangle and then we're building
  3909. 2:51:43something on top of it. And that
  3910. 2:51:45abstraction actually lets us lets us add
  3911. 2:51:48uh 8 bit numbers. So we've we've taken
  3912. 2:51:51these logic gates which on their own are
  3913. 2:51:54pretty trivial and have now forced them
  3914. 2:51:57in arranged them is probably a better
  3915. 2:51:59word should I say we've arranged them in
  3916. 2:52:01such a way that the logic gates do
  3917. 2:52:03something useful to us and you may be
  3918. 2:52:07wondering this is this is cool and all
  3919. 2:52:08but how does this actually happen in the
  3920. 2:52:10real world? Well, I mentioned this
  3921. 2:52:12earlier, but uh all of these lines I'm
  3922. 2:52:15drawing, you can actually think of them
  3923. 2:52:17as wires. And the fundamental property
  3924. 2:52:20you need to understand of wires is that
  3925. 2:52:22they conduct very fast. So if I have a
  3926. 2:52:25long wire,
  3927. 2:52:29assume there's no electricity on it. If
  3928. 2:52:32I put electricity on it on this end,
  3929. 2:52:35then we will say almost instantly this
  3930. 2:52:38is a big asterisk
  3931. 2:52:41the electricity will appear on this side
  3932. 2:52:43because wires conduct electricity.
  3933. 2:52:47And we're saying almost because nothing
  3934. 2:52:49in this world is immediate. And there
  3935. 2:52:51actually is a little bit of delay
  3936. 2:52:53between when we put electricity on a
  3937. 2:52:55wire and when it's received on the other
  3938. 2:52:57end, you know, cuz electrons actually
  3939. 2:52:59have to move across the wire.
  3940. 2:53:04So what you can actually believe in and
  3941. 2:53:06and kind of use to understand this is
  3942. 2:53:08that all the lines I've drawn in these
  3943. 2:53:11above diagrams and up until now are
  3944. 2:53:13physical wires.
  3945. 2:53:15And this is not an exaggeration. This is
  3946. 2:53:17this is the truth. And if I I also said
  3947. 2:53:22that we can build these logic gates
  3948. 2:53:23physically. You can buy these little
  3949. 2:53:24chips that have them built in. And so
  3950. 2:53:27one such chip has it looks like this.
  3951. 2:53:32And it has many inputs and many outputs.
  3952. 2:53:37But I could then put electricity on this
  3953. 2:53:41line and this line this line and
  3954. 2:53:45I by electricity I mean I'd either put
  3955. 2:53:47electricity or not put electricity. This
  3956. 2:53:50is a high level. Again there's a little
  3957. 2:53:52bit different in the in the world of
  3958. 2:53:53physics but this is how we will
  3959. 2:53:54understand it and how I think it's all
  3960. 2:53:56you need to understand it as. And if I
  3961. 2:53:59put electricity on the wire we say that
  3962. 2:54:01it's a one. There is a truth truthful
  3963. 2:54:04value on the wire a one. And if there's
  3964. 2:54:06no electricity, we interpret that value
  3965. 2:54:08to be a zero. So then this chip that
  3966. 2:54:11that does exist internally does
  3967. 2:54:13something. It somehow manipulates the
  3968. 2:54:16electricity in the real world to
  3969. 2:54:17function like a logic gate to this
  3970. 2:54:19output. And I have this chip. And so if
  3971. 2:54:23I genuinely put electricity on this wire
  3972. 2:54:27and didn't put electricity on this wire,
  3973. 2:54:29then there would be no electricity on
  3974. 2:54:31this output wire. And then if I did put
  3975. 2:54:34electricity on both the input wires,
  3976. 2:54:36then there would be electricity on the
  3977. 2:54:38output wire. And we're not going to
  3978. 2:54:40study how this works in the real world.
  3979. 2:54:43We're not going to study how you can
  3980. 2:54:44build these logicates.
  3981. 2:54:46But it is it is really possible. And
  3982. 2:54:49it's it turns out it's not too difficult
  3983. 2:54:50to actually build them. And there's a
  3984. 2:54:53lot of resources online which uh I'd be
  3985. 2:54:55happy to link if if someone requests.
  3986. 2:54:59But what we do want to understand for
  3987. 2:55:01the sake of this video is that when I'm
  3988. 2:55:03drawing these lines, they're actually
  3989. 2:55:04wires. I'm I'm drawing something out
  3990. 2:55:07that could really exist in the real
  3991. 2:55:08world. And these lines are wires. And
  3992. 2:55:11when I say put a one or put a zero on a
  3993. 2:55:14on on a line or comes in or out of a
  3994. 2:55:17logic gate, I'm really saying we either
  3995. 2:55:19have electricity or don't have
  3996. 2:55:20electricity. But but thinking about it
  3997. 2:55:23at the level of ones and zeros is
  3998. 2:55:25certainly easier than thinking about it
  3999. 2:55:27at the level of electricity or no
  4000. 2:55:30electricity. Electrons or no electrons.
  4001. 2:55:33So I'm going to stick to this
  4002. 2:55:34understanding where I say zeros and
  4003. 2:55:36ones. But really you should understand
  4004. 2:55:38that this is actually something that
  4005. 2:55:40could exist in the real world and uh
  4006. 2:55:43something we we'll talk about more more
  4007. 2:55:45later on down the line.
  4008. 2:55:52Okay, so we've now built an 8bit adder.
  4009. 2:55:55We built an 8bit adder,
  4010. 2:56:00but there's an issue here, and I I've
  4011. 2:56:01mentioned it a few times.
  4012. 2:56:05How does subtraction work?
  4013. 2:56:10And
  4014. 2:56:12it may seem to you like we will need to
  4015. 2:56:15destroy what we have and rebuild the
  4016. 2:56:19entire computer or the entire adding
  4017. 2:56:22logic circuit. But it turns out another
  4018. 2:56:25really interesting takeaway uh at least
  4019. 2:56:28what I would call interesting is that we
  4020. 2:56:30are actually 95% of the way there with
  4021. 2:56:32what we have up top to being able to do
  4022. 2:56:34subtraction.
  4023. 2:56:36And the reason is is remember what we
  4024. 2:56:39said earlier. We said that if we wanted
  4025. 2:56:41to do a minus b, it is really the
  4026. 2:56:45equivalent of doing a +
  4027. 2:56:48b. And so we know this circuit can do a
  4028. 2:56:52plus negative b. But the issue is we we
  4029. 2:56:55have b feed being fed in as all of our
  4030. 2:56:58inputs. We don't have negative b.
  4031. 2:57:02And how then if we have all these B
  4032. 2:57:04inputs, can we actually make them
  4033. 2:57:08negative B? So what I'm going to do here
  4034. 2:57:10is pause this video and redraw this
  4035. 2:57:13circuit in a slightly more optimal and
  4036. 2:57:16more condensed way and at least in a way
  4037. 2:57:18that I can I can show everything on my
  4038. 2:57:20screen at once and then we will I will
  4039. 2:57:23uh cut back and we can pick up and
  4040. 2:57:24answer this question. Okay, so I have
  4041. 2:57:29drawn this in and let me go ahead
  4042. 2:57:31[clears throat] and erase these bars on
  4043. 2:57:32the side. And let's do a little recap of
  4044. 2:57:35what I have here. And you've also
  4045. 2:57:36noticed that for about the first time in
  4046. 2:57:39this video, I've decided to use
  4047. 2:57:40something besides the color white. So
  4048. 2:57:42you're welcome. And
  4049. 2:57:45basically, I wanted to color code things
  4050. 2:57:47because we have a lot going on here. And
  4051. 2:57:49I'd also before I even start to explain
  4052. 2:57:51this would like to appreciate or hope
  4053. 2:57:53that you can appreciate how much less
  4054. 2:57:56painful this is now that we've
  4055. 2:57:58abstracted these logic gates out of the
  4056. 2:58:00way. And it's still pretty messy. So we
  4057. 2:58:02we can do more eventually. We'll
  4058. 2:58:04abstract this entire thing. But um as we
  4059. 2:58:08have it right now, there's this is this
  4060. 2:58:09is fairly reasonable to look at. And
  4061. 2:58:12hopefully with the color coding and the
  4062. 2:58:13explanation I'll give in the next few
  4063. 2:58:14minutes, we can make sense of this.
  4064. 2:58:16[snorts]
  4065. 2:58:17So, what I have up top are our eight A
  4066. 2:58:20inputs and our eight B inputs. And I'm
  4067. 2:58:24drawing these little circles because uh
  4068. 2:58:26you'll you'll see this notation if you
  4069. 2:58:27ever end up watching some other videos
  4070. 2:58:30where uh this this represents an input
  4071. 2:58:32almost. And we can either put in a zero
  4072. 2:58:36or a one to that input. And it's just a
  4073. 2:58:38notation. And I personally think it's a
  4074. 2:58:41reasonable notation. And if we were to
  4075. 2:58:43do this in the real world, then this
  4076. 2:58:45circle would probably just be some
  4077. 2:58:47source of electricity or not. And you
  4078. 2:58:49know, we we'd connect electricity there
  4079. 2:58:51and go through that wire into the
  4080. 2:58:53corresponding full ladder. [snorts]
  4081. 2:58:55But I have all the A inputs highlighted
  4082. 2:58:58in red and or orange if if that is
  4083. 2:59:01orange to you. And I'm actually not sure
  4084. 2:59:03what it is to me. I I'll stick with the
  4085. 2:59:05orange. So I have all the orange
  4086. 2:59:06connected to all the A's and those go
  4087. 2:59:09into the A lines of the full adder. And
  4088. 2:59:12it doesn't really matter if we we
  4089. 2:59:13commute A or B for the full ladder. It
  4090. 2:59:15doesn't really make a difference because
  4091. 2:59:16A plus B is the same as B plus A.
  4092. 2:59:20But more interestingly,
  4093. 2:59:22I have or equal equally interesting. I
  4094. 2:59:25guess there's not really anything more
  4095. 2:59:26remarkable about it. I have all the B's
  4096. 2:59:29in blue going into the B inputs of each
  4097. 2:59:32of these eight full adders.
  4098. 2:59:34And then I have in gray here the carry
  4099. 2:59:39out from each outer adder going into the
  4100. 2:59:43carry in of the next adder. And the one
  4101. 2:59:46exception to that is the very last carry
  4102. 2:59:49out from the very last full adder. And
  4103. 2:59:52that comes down here. And so uh the
  4104. 2:59:55greens are the sum bits of each of the
  4105. 2:59:58full adders.
  4106. 3:00:00And sorry I don't know if I was just
  4107. 3:00:03saying half adder. I meant to be saying
  4108. 3:00:04full ladder. If I was, I apologize. And
  4109. 3:00:07the we know that the carry out from the
  4110. 3:00:09last full ladder is special because it
  4111. 3:00:12it is not just carry out, but it's part
  4112. 3:00:14of the the output of our sum. Whereas
  4113. 3:00:17all the other previous carry outs, we we
  4114. 3:00:19need them to do the computation, but we
  4115. 3:00:21don't really care what they are. We just
  4116. 3:00:23care what the result is. And that last
  4117. 3:00:25carry out is is technically part of the
  4118. 3:00:27result.
  4119. 3:00:28And the only other interesting thing is
  4120. 3:00:32this this over here where I have this
  4121. 3:00:35carry into the very first full ladder.
  4122. 3:00:37And we said that's just going to be a
  4123. 3:00:39zero. So I kind of written this zero in
  4124. 3:00:40quotations.
  4125. 3:00:43And uh I I've I've given it some special
  4126. 3:00:45attention. That's because we're going to
  4127. 3:00:47see that it it may not actually always
  4128. 3:00:49be the case that we want it to be zero.
  4129. 3:00:51And I also wrote S9 in quotes down here
  4130. 3:00:55because it's it's really equal to the
  4131. 3:00:57carry out of the eighth full adder. So
  4132. 3:00:59we could say carry 8, but I wanted to
  4133. 3:01:03make it apparent now that you know even
  4134. 3:01:05though this is the carry out when we use
  4135. 3:01:07it, it actually does become part of the
  4136. 3:01:10product, the result of the sum.
  4137. 3:01:13And so I think it it makes sense to
  4138. 3:01:14treat it like the rest of the result
  4139. 3:01:16bits. But I I'll leave it in quotations
  4140. 3:01:18from this diagram for now so you can
  4141. 3:01:20understand that is it is a little bit
  4142. 3:01:22different than the rest and where it
  4143. 3:01:23comes from. But when it comes to
  4144. 3:01:25interpreting the output, it's the same.
  4145. 3:01:27So now you can see we have this circuit
  4146. 3:01:29where we could put in our A values and
  4147. 3:01:31then our B values and then we'd be able
  4148. 3:01:33to read the output right here. And you
  4149. 3:01:35could understand all the logic gates if
  4150. 3:01:36you really wanted to. You could take
  4151. 3:01:38this yourself and draw out all the logic
  4152. 3:01:40gates, but that's that's not necessary
  4153. 3:01:42for what we have going on right here.
  4154. 3:01:44Now, what we really said that we wanted
  4155. 3:01:46to do was figure out how to do this
  4156. 3:01:48subtraction with the our circuit that we
  4157. 3:01:50have here. And we know that this circuit
  4158. 3:01:53can do a plus b. And the issue is we
  4159. 3:01:56want to do a minus b. And how could we
  4160. 3:01:59do that? Well, I I invoked the rule that
  4161. 3:02:01we learned at the start of this video,
  4162. 3:02:03which was a minus b is equal to a +
  4163. 3:02:07negative b.
  4164. 3:02:09So, what we really want to be able to do
  4165. 3:02:11here is
  4166. 3:02:13take the inputs. We'd like to have the
  4167. 3:02:14same circuit. We'd like to be able to
  4168. 3:02:16use the same circuit for addition and
  4169. 3:02:18subtraction and have a way of telling it
  4170. 3:02:20when we're adding and when we're
  4171. 3:02:21subtracting. And when we're subtracting,
  4172. 3:02:24we don't want to feed the B's. We want
  4173. 3:02:26to put our our B input up here, but we
  4174. 3:02:28don't want these wires just to feed
  4175. 3:02:30directly into these gates cuz we want
  4176. 3:02:32negative B to feed into each of these
  4177. 3:02:34adders.
  4178. 3:02:36And so, let's think about how we can
  4179. 3:02:38accomplish that. And I'm going to write
  4180. 3:02:40down what we learned earlier. Given B or
  4181. 3:02:44given A or B or C any variable
  4182. 3:02:48to get negative B
  4183. 3:02:52what do we do? We do we invert every bit
  4184. 3:02:56in B. So invert
  4185. 3:03:00the bits [snorts] bit meaning just a
  4186. 3:03:02digit position
  4187. 3:03:05in B. And more formally put,
  4188. 3:03:09let ones become zeros
  4189. 3:03:13and let zeros become one.
  4190. 3:03:18And once we do that, there's one other
  4191. 3:03:20thing we have to do. If you remember,
  4192. 3:03:22that's what we have to add one to the
  4193. 3:03:23result. So add one to the result.
  4194. 3:03:30And that's that's decimal one or binary
  4195. 3:03:33one. They're equivalent. whatever one
  4196. 3:03:35you want to conceptualize. Um, and so if
  4197. 3:03:39we had
  4198. 3:03:40if we had this number one zero one1 and
  4199. 3:03:44we wanted to negate it, then we do 0 1 0
  4200. 3:03:510. So that's our step one. Then we add
  4201. 3:03:54one to that for step two. So we do 0 1 0
  4202. 3:03:570 plus 0 0 1. And we're left with this
  4203. 3:04:00result right here.
  4204. 3:04:02And you might be wondering something
  4205. 3:04:04very valid and that's I said up until
  4206. 3:04:07this point we we've been building these
  4207. 3:04:09full adders that only work on positive
  4208. 3:04:12numbers.
  4209. 3:04:14And that's true but it actually works
  4210. 3:04:16out with negative numbers as well.
  4211. 3:04:19And this is true because I very earlier
  4212. 3:04:23in the video I showed you how to do
  4213. 3:04:25addition and subtraction in binary. And
  4214. 3:04:28we learned that addition for negative
  4215. 3:04:30numbers is the same as positive numbers
  4216. 3:04:33for two's complement representation. And
  4217. 3:04:36subtraction also works out as long as we
  4218. 3:04:38can just negate the number. So this
  4219. 3:04:40circuit, we've only been using it with
  4220. 3:04:42positive numbers, but it turns out we
  4221. 3:04:44can use it to add negative numbers as
  4222. 3:04:46well.
  4223. 3:04:47And
  4224. 3:04:49>> so now we're going to learn how we can
  4225. 3:04:54now that I've kind of I kind of shaken
  4226. 3:04:55that up a little bit because I've been
  4227. 3:04:56ignoring all these negative numbers, but
  4228. 3:04:58it is the case that this works with that
  4229. 3:05:00that it does work with negative numbers.
  4230. 3:05:02You could actually plug in an example
  4231. 3:05:04maybe maybe not on the 8 bit one, but on
  4232. 3:05:06our our two bit example, our two-bit
  4233. 3:05:09adder when we just had two full adders
  4234. 3:05:11trained together and and see that it
  4235. 3:05:12actually does work out. And what we're
  4236. 3:05:15interested in though, back to the topic,
  4237. 3:05:17is how can we do subtraction? And so we
  4238. 3:05:20have B and we want to get negative B. So
  4239. 3:05:22what we really saw is we could take all
  4240. 3:05:25of the bits from B for step one and just
  4241. 3:05:29flip them. And how do we do that here?
  4242. 3:05:33And what we can recall is that we
  4243. 3:05:35actually have a logicate that does
  4244. 3:05:37exactly this. And it's our good old
  4245. 3:05:39inverter. The inverter
  4246. 3:05:43looks like this. And if you feed it in a
  4247. 3:05:45zero, you're going to get a one. And if
  4248. 3:05:46you feed it in a one, you're going to
  4249. 3:05:48get a zero. And
  4250. 3:05:51oops, let me erase that.
  4251. 3:05:54This is exactly what we'd like. So to
  4252. 3:05:57accomplish the first step, what we can
  4253. 3:05:58really do is is have our inputs of B and
  4254. 3:06:01just feed each of them through an
  4255. 3:06:03inverter whenever we want to subtract.
  4256. 3:06:06That's the issue. We we want to be able
  4257. 3:06:08to distinguish when we're adding and
  4258. 3:06:10subtracting. But let's just assume that
  4259. 3:06:12we're just going to use this for
  4260. 3:06:13subtracting. So we'd we'd end up
  4261. 3:06:16inverting all of these. And so I guess
  4262. 3:06:19I'll just draw an arrow and I do some uh
  4263. 3:06:23some sort of demarcation that we'd
  4264. 3:06:25invert. And that handles the first step.
  4265. 3:06:29And then the second step is adding a one
  4266. 3:06:32to the result. And now let's think about
  4267. 3:06:35this because this is this is a little
  4268. 3:06:36bit weirder. And
  4269. 3:06:40how could we do this with the paradigm
  4270. 3:06:42that we have up here? Uh this is a good
  4271. 3:06:44good challenge question. And it turns
  4272. 3:06:47out to add one, we could just feed in a
  4273. 3:06:50carry-in of one to the very first
  4274. 3:06:52addition because we know that when we
  4275. 3:06:55have uh for some digit position when
  4276. 3:06:57we're doing addition, if we have a to
  4277. 3:07:00the x and then b to the x position, then
  4278. 3:07:03we also take in a carry in at the x
  4279. 3:07:06position and we output a sum at the x
  4280. 3:07:09position and a carry out at the x
  4281. 3:07:12position. And this carry in effectively
  4282. 3:07:15just adds another value to our step. And
  4283. 3:07:19what we've been saying so far, if we
  4284. 3:07:21come back up here, is that when we're
  4285. 3:07:24doing these 8bit addition, then we
  4286. 3:07:27actually are sure that we're never going
  4287. 3:07:30to have a carry in the first time
  4288. 3:07:31because we have nothing else to carry in
  4289. 3:07:33from. But if we actually just set a
  4290. 3:07:35carry-in to be one, then we're
  4291. 3:07:37effectively adding one to our overall
  4292. 3:07:39result. And so this actually works out
  4293. 3:07:42perfectly. And so you might be
  4294. 3:07:44wondering, well, this this adds one to
  4295. 3:07:47our result, but we're trying to just add
  4296. 3:07:49one to B. But doing those are
  4297. 3:07:51synonymous. If I do um if I do a plus a
  4298. 3:07:56+ b, so if I have all these a's and then
  4299. 3:08:00I have all these b's, but I want to do
  4300. 3:08:03neg a minus b, then I first off, I
  4301. 3:08:06invert all of these. And I'm just using
  4302. 3:08:09this bar up top. Uh this this this
  4303. 3:08:11notation of of this bar on top of B just
  4304. 3:08:15means the opposite of B. Um
  4305. 3:08:20and if we just throw a one up here, then
  4306. 3:08:22this is the same thing as doing and now
  4307. 3:08:26this becomes addition. A a a plus
  4308. 3:08:30[snorts] and then we could have done uh
  4309. 3:08:33bb
  4310. 3:08:36the negated version plus a one. So if we
  4311. 3:08:39did that sum and then this outer sum,
  4312. 3:08:43then it's the same as just just doing it
  4313. 3:08:45all at once because a plus we're
  4314. 3:08:48effectively saying here that a + b + 1
  4315. 3:08:52is just the same thing as a + b + 1,
  4316. 3:08:57which could just be however order you
  4317. 3:08:59want to permute it because addition has
  4318. 3:09:01this this commutive property you learned
  4319. 3:09:03about when you when you were way
  4320. 3:09:04younger.
  4321. 3:09:06And this is a really powerful result.
  4322. 3:09:09This lets us mean that when we want to
  4323. 3:09:12do subtraction, we could just set this
  4324. 3:09:14carry in which we know is going to be a
  4325. 3:09:16zero for addition to be a one and it
  4326. 3:09:18adds one to our result. And so we found
  4327. 3:09:22a way effectively using our circuit of
  4328. 3:09:26full adders that we originally designed
  4329. 3:09:28for addition to be able to do
  4330. 3:09:29subtraction. And when we want to do
  4331. 3:09:31subtraction, we're going to do instead
  4332. 3:09:33of a minus b, we're going to do a plus
  4333. 3:09:35negative b. And we have b's as the input
  4334. 3:09:39over here. And to make it negative b,
  4335. 3:09:41we're going to take the two-step
  4336. 3:09:42process. The first of which is inverting
  4337. 3:09:44all of b's bits from ones to zeros or
  4338. 3:09:46zeros to one. And the second step is
  4339. 3:09:49adding a one to the result. And we can
  4340. 3:09:51do both of that with the circuit we
  4341. 3:09:53already have minus a few additions of
  4342. 3:09:55inverters.
  4343. 3:09:57So what we're going to go ahead and do
  4344. 3:10:00now is there's one final step and that's
  4345. 3:10:02that we need to distinguish when we're
  4346. 3:10:04adding or subtracting because when we're
  4347. 3:10:07adding we don't want we don't want these
  4348. 3:10:10values to be inverted
  4349. 3:10:13and when we are adding we also want this
  4350. 3:10:16carry-in to be a zero but when we're
  4351. 3:10:19subtracting we want the carry-in to be a
  4352. 3:10:21one and when we're subtracting we want
  4353. 3:10:23all of B's bits to be inverted.
  4354. 3:10:26So, how can we actually do that? And
  4355. 3:10:28what we're going to do is add another
  4356. 3:10:31input to our circuit. And that's just
  4357. 3:10:33going to be the addition subtraction
  4358. 3:10:35flag. So, I'll make this a circle for
  4359. 3:10:36consistency. And I'll just write this is
  4360. 3:10:40a slash s. And what I'll say is that if
  4361. 3:10:43if this is zero, so if zero, then
  4362. 3:10:48addition, and if one, then we'll do
  4363. 3:10:51subtraction. That's just how we're
  4364. 3:10:53designing this. And then we'll be sure
  4365. 3:10:55for the rest of this of this uh to make
  4366. 3:10:58sure that we adhere to that
  4367. 3:10:59consistently.
  4368. 3:11:01And
  4369. 3:11:02then what we want to do is how do we
  4370. 3:11:04deal with well this this first part's
  4371. 3:11:06easy for the inverting the adding the
  4372. 3:11:09one. It's easy. It's just the value of
  4373. 3:11:12this because if we are adding then we
  4374. 3:11:14want no carry into the first full adder.
  4375. 3:11:17And if we're subtracting, we want to
  4376. 3:11:18carry into the first full adder so that
  4377. 3:11:20we have the effect of one, the effective
  4378. 3:11:23addition of one that we needed for the
  4379. 3:11:24binary conversion. But this this
  4380. 3:11:27inverting part is a little difficult. If
  4381. 3:11:29we have this line
  4382. 3:11:31and we have a value, let's just look at
  4383. 3:11:34B1, then how can we use both of those to
  4384. 3:11:37get the right B1 that should go into the
  4385. 3:11:40gate or the right, I guess, uh, modified
  4386. 3:11:44B1 or not that should go into the gate.
  4387. 3:11:46And so what we really have is
  4388. 3:11:50if as is set so if as equals 1 then let
  4389. 3:11:56b then we want to invert so we should
  4390. 3:11:59invert b1
  4391. 3:12:03and if as is zero meaning we want to add
  4392. 3:12:07then we leave b1 alone
  4393. 3:12:10and how can we do this? Well, a a simple
  4394. 3:12:14simple gate. We can do one gate and
  4395. 3:12:16that's the exclusive orgate. And let's
  4396. 3:12:20see what that looks like. So if we feed
  4397. 3:12:22in B1 here and then this AS line, then
  4398. 3:12:27let's look at happens for each of the
  4399. 3:12:28input cases. So if as is one, then we
  4400. 3:12:32want to invert B1. So if B1 is zero,
  4401. 3:12:35then we would we would hope to get a one
  4402. 3:12:37out and one exclusive or zero is one. So
  4403. 3:12:40that is correct. And similarly, if B1 is
  4404. 3:12:43is is one, we'd hope to get a zero out.
  4405. 3:12:46And one exclusive or one is a zero. So
  4406. 3:12:49So this works.
  4407. 3:12:52And then if as is zero, we would want B1
  4408. 3:12:56to to come out exactly as it is. So if
  4409. 3:12:59as is zero, if B1 is zero, then zero
  4410. 3:13:02exclusive or zero is zero. So that
  4411. 3:13:05flowed through. And if it's one, then we
  4412. 3:13:08know one exclusive or zero is one. And
  4413. 3:13:11so that is exactly how this behaves. And
  4414. 3:13:14if you have a little bit of trouble
  4415. 3:13:16conceptualizing that, go back earlier to
  4416. 3:13:18when we looked at the truth table for
  4417. 3:13:20the exclusive orgate or you can just
  4418. 3:13:22Google it now. But this is just a
  4419. 3:13:24property of the gate and it it works out
  4420. 3:13:26very conveniently that we can use it for
  4421. 3:13:28this this task that we have right here.
  4422. 3:13:30And then it turns out this is uh we were
  4423. 3:13:33looking just at B1, but we can do this
  4424. 3:13:35for each of the B's.
  4425. 3:13:37And that's exactly what we'll do. We
  4426. 3:13:40will have an exclusive or gate for each
  4427. 3:13:42of these B's and we will exclusive or
  4428. 3:13:47the corresponding B with the same AS
  4429. 3:13:50line that we have up here. So I for the
  4430. 3:13:53sake of drawing am going to pause and
  4431. 3:13:56update this diagram to have that in
  4432. 3:13:58there. Okay. So I've updated the diagram
  4433. 3:14:02now and I was running out of color. So I
  4434. 3:14:04have this lightish purple and basically
  4435. 3:14:08all that changed in the past millisecond
  4436. 3:14:10is I have exclusive ored each of the B
  4437. 3:14:16gates with the value from this as line
  4438. 3:14:20this addition subtraction line before
  4439. 3:14:23feeding it into the B input of the full
  4440. 3:14:25adders. And so what this means now is
  4441. 3:14:29that if we want to add, we load in our
  4442. 3:14:31values to A, load in our values to B,
  4443. 3:14:34and then we can look and get the result
  4444. 3:14:36on the S lines down below. And if we
  4445. 3:14:40would like to subtract, we do the same
  4446. 3:14:42thing. We put in the A, we put in the B,
  4447. 3:14:47and then we set this AS line to be one.
  4448. 3:14:51So whenever this as line is zero, it's
  4449. 3:14:54just going to add this circuit is just
  4450. 3:14:56going to add whatever is on the input
  4451. 3:14:57lines. Even if we don't have any
  4452. 3:14:59meaningful value, it's it's a circuit.
  4453. 3:15:01The logic gates are going to do what
  4454. 3:15:02they do. And we're going to get some
  4455. 3:15:04output that we can read on these S
  4456. 3:15:05lines. And if we set this S line, this
  4457. 3:15:08AS line to one, then it's going to
  4458. 3:15:11subtract whatever values happen to be on
  4459. 3:15:13the A line and the B line and come into
  4460. 3:15:17this S line as the output.
  4461. 3:15:20And
  4462. 3:15:22so this is the extent of difficulty
  4463. 3:15:24we're going to need for this video. And
  4464. 3:15:28computers can do uh multiplication and
  4465. 3:15:30division, but it's a bit more tedious
  4466. 3:15:33and involved of a process. And I don't
  4467. 3:15:36think there's as much not to say there's
  4468. 3:15:38not benefit, but I think this suffices
  4469. 3:15:42for understanding the rest of the
  4470. 3:15:44fundamental concepts of computing.
  4471. 3:15:47So there is no plan for me to show how
  4472. 3:15:50subtraction or multiplication division
  4473. 3:15:52work but there is plenty of resources
  4474. 3:15:54online that you can feel free to look up
  4475. 3:15:57if you are curious about it. It is uh
  4476. 3:16:00interesting because we then have to
  4477. 3:16:02without going into too much detail you
  4478. 3:16:04have to update how you represent
  4479. 3:16:05numbers. For instance, we can only
  4480. 3:16:07represent whole numbers. But if I
  4481. 3:16:09divided seven by two, the output's 3.5.
  4482. 3:16:13But we have no way in binary b up till
  4483. 3:16:16now what I've taught you of representing
  4484. 3:16:18this.5. And so you kind of have to go
  4485. 3:16:20back and and revisit your representation
  4486. 3:16:23of numbers. But we're going to stick
  4487. 3:16:24with this two's complement. And so now
  4488. 3:16:27we're back and we have this we have this
  4489. 3:16:31circuit that can add. But there's a
  4490. 3:16:32little bit of an issue. And it I think
  4491. 3:16:34I've kind of made it not super clear
  4492. 3:16:36until this point until I was working
  4493. 3:16:38through, but that is these are logic
  4494. 3:16:41gates. And so they're always just going
  4495. 3:16:43to operate based on whatever the inputs
  4496. 3:16:44are. So if we don't really have any
  4497. 3:16:48meaningful values on these A lines or B
  4498. 3:16:50lines, there's still going to be some
  4499. 3:16:52value in the S line. But that what that
  4500. 3:16:54also means is if we did want to add two
  4501. 3:16:56numbers and we put them in the A lines
  4502. 3:16:58and B lines, then the output's going to
  4503. 3:17:00correctly come out on these S lines down
  4504. 3:17:03below, but as soon as we change any of
  4505. 3:17:06these inputs, the output may or may not
  4506. 3:17:08change. And that's that's that's
  4507. 3:17:11actually problematic. Uh we'd like to be
  4508. 3:17:13able to to save the result. And
  4509. 3:17:17let's say I'd like to to add two
  4510. 3:17:19numbers, put them I put them on the A
  4511. 3:17:21lines and B lines and I get the result.
  4512. 3:17:23And then let's say I want to take that
  4513. 3:17:24result and and add it to something else.
  4514. 3:17:27Then right now there's not really a
  4515. 3:17:29clear way of me doing that. And that's
  4516. 3:17:31kind of as we're going to get into the
  4517. 3:17:33more uh different components of
  4518. 3:17:35computers and how we can actually make
  4519. 3:17:38this this this adder and subtractor do
  4520. 3:17:41meaningful work rather than us having to
  4521. 3:17:44manually put electricity or at our level
  4522. 3:17:47put zeros and ones onto each of these
  4523. 3:17:49input lines. And what we've built right
  4524. 3:17:52here is 95. Again, we're not fully done.
  4525. 3:17:55We're actually going to revisit this one
  4526. 3:17:57more time, but it's not going to be to
  4527. 3:17:59add anything too more complicated. But
  4528. 3:18:02what we've built right here is called
  4529. 3:18:04the ALU. Stands for the arithmetic logic
  4530. 3:18:07unit. And every computer has one of
  4531. 3:18:10these. Uh the computer you're watching
  4532. 3:18:12this on has one. And it's responsible
  4533. 3:18:13for doing doing the arithmetic. And I
  4534. 3:18:17mentioned this earlier, but computers
  4535. 3:18:18are effectively glorified calculators.
  4536. 3:18:22So, this is actually probably the uh
  4537. 3:18:24heart of most modern computers. And
  4538. 3:18:26obviously, your computers are
  4539. 3:18:28implemented in ways that make these run
  4540. 3:18:31incredibly fast and uh can do a lot more
  4541. 3:18:34than what I've shown uh more than just
  4542. 3:18:36the addition of subtraction. But this is
  4543. 3:18:38just to illustrate the concept. And what
  4544. 3:18:41we'd like to do, as I' I've kind of
  4545. 3:18:42hinted now, is be able to take the
  4546. 3:18:45result of an addition or subtraction we
  4547. 3:18:47do on this circuit and save it.
  4548. 3:18:50But we know with these logic gates that
  4549. 3:18:52the moment we change one of their
  4550. 3:18:53inputs, the output may or may not change
  4551. 3:18:56depending on what the gate is. But in
  4552. 3:18:58general, it's it's safe to assume that
  4553. 3:19:00once you change the input to a logic
  4554. 3:19:02gate or a circuit, if you change any of
  4555. 3:19:04the inputs, it's not necessarily going
  4556. 3:19:06to be the case that the output stays the
  4557. 3:19:07same. So, if I have these A lines set to
  4558. 3:19:10some value and I have these B lines set
  4559. 3:19:12to some value, I would need to in the
  4560. 3:19:15real world wait a small amount of time
  4561. 3:19:17for these logic gates to do their work
  4562. 3:19:19and settle. And then that's something
  4563. 3:19:22we'll talk about more later. But then
  4564. 3:19:24once we we give it some time to settle
  4565. 3:19:26and it do all the electricity flows
  4566. 3:19:28through and everything, we we'd like to
  4567. 3:19:30be able to take this output and save it.
  4568. 3:19:32And this is a what I mentioned earlier
  4569. 3:19:36in the video at the very start. This is
  4570. 3:19:38going to be the second hardest concept
  4571. 3:19:39we taught in the video. The first most
  4572. 3:19:42difficult I in my opinion was learning
  4573. 3:19:44binary which you made it past and this
  4574. 3:19:46is the fullest you'll learn binary. But
  4575. 3:19:49the second concept is how can we save
  4576. 3:19:51numbers with logic gates. And so I'm
  4577. 3:19:54going to scroll down because this is
  4578. 3:19:55going to be a new segment of the video.
  4579. 3:19:58So everything we've looked at so far had
  4580. 3:20:00this property that I talked about
  4581. 3:20:01earlier called determinism
  4582. 3:20:06determinism.
  4583. 3:20:09And what this meant is that the output
  4584. 3:20:12of a circuit dependent strictly on the
  4585. 3:20:15input values. And this is not really
  4586. 3:20:17that complicated or it probably sounds a
  4587. 3:20:20little bit obvious to you because
  4588. 3:20:21obviously these outputs just determine
  4589. 3:20:23on the inputs because they all chain
  4590. 3:20:24into each other. But let me give you a
  4591. 3:20:27circuit that actually breaks that
  4592. 3:20:29principle. And it's something we haven't
  4593. 3:20:31seen before. But uh what happens if I
  4594. 3:20:34take this simple orgate? So this is the
  4595. 3:20:36orgate. And I have some input. Let's
  4596. 3:20:39just call this X.
  4597. 3:20:41And let's take the output of this orgate
  4598. 3:20:44and actually feed it back in as the
  4599. 3:20:46other input. And this is really weird.
  4600. 3:20:49We haven't seen this before. We've seen
  4601. 3:20:51the output of logic gates go into the
  4602. 3:20:53input of other logic gates, but we
  4603. 3:20:56haven't seen the output of a logic gate
  4604. 3:20:58come back into itself. And this is going
  4605. 3:21:01to break this idea of determinism
  4606. 3:21:03because let's assume that when I turn
  4607. 3:21:07this on, I have a zero on this line. And
  4608. 3:21:09then let's assume that there's a zero
  4609. 3:21:11coming out. So I have zeros all around.
  4610. 3:21:14So I have if I make this input output,
  4611. 3:21:17if I make a truth table, actually that's
  4612. 3:21:18what we'd like to do.
  4613. 3:21:20So let's call this this input X and this
  4614. 3:21:23output Y. Then I've shown right here
  4615. 3:21:26that we could have a zero for X and that
  4616. 3:21:28we have a one for Y. But now let's look
  4617. 3:21:31at what happens if we set this X to be a
  4618. 3:21:35one.
  4619. 3:21:36So if I set X to be a one, then we have
  4620. 3:21:40the inputs 0 and one feeding into an O
  4621. 3:21:42orgate. And an O orgate turns on as long
  4622. 3:21:44as either of the inputs is one. So then
  4623. 3:21:46the output of this orgate becomes a one
  4624. 3:21:49and something else happens at the same
  4625. 3:21:52exact time is this output becomes a one
  4626. 3:21:54but it's also the input to this orgate
  4627. 3:21:56which means it's this input is now
  4628. 3:21:58effectively a one as well and what this
  4629. 3:22:02means is that this this orgate is now
  4630. 3:22:06forever going to output a one because we
  4631. 3:22:10have it outputting a one now which means
  4632. 3:22:12it's inputting a one on this top line
  4633. 3:22:14and that means that it's guaranteed to
  4634. 3:22:16be getting in a one forever. And what I
  4635. 3:22:19mean by that is you convince yourself
  4636. 3:22:20this is true by changing this input x
  4637. 3:22:23back to zero. And if I change it back to
  4638. 3:22:26zero, then notice that the two inputs
  4639. 3:22:28are a zero and a one, which still ores
  4640. 3:22:31to one.
  4641. 3:22:33And this might take a little bit to wrap
  4642. 3:22:35your head around because it it is fairly
  4643. 3:22:38confusing and I I struggle with this
  4644. 3:22:40when I learn the concept. And it's not
  4645. 3:22:42very intuitive to take the output of a
  4646. 3:22:44gate and feed it back into a gate. But
  4647. 3:22:47it turns out that when we do that, we
  4648. 3:22:48have some really powerful properties
  4649. 3:22:50that we can achieve. And so if you can
  4650. 3:22:54kind of understand that what's happening
  4651. 3:22:55right now, uh let me prove to you that
  4652. 3:22:58this is unlike anything we've seen. If
  4653. 3:22:59that's not already clear, because now we
  4654. 3:23:02have a zero uh a
  4655. 3:23:05sorry, this this is correctly
  4656. 3:23:06incorrectly written. When we powered
  4657. 3:23:08this on, we had a zero going into x and
  4658. 3:23:09we had a zero coming out. But now we
  4659. 3:23:12have a zero and then we so we started
  4660. 3:23:15with zero going in and zero coming out
  4661. 3:23:18and then we made it a one going in and
  4662. 3:23:20we had a one coming out and now we've
  4663. 3:23:22set x back to zero and the output is
  4664. 3:23:26still a one. And you may notice that
  4665. 3:23:29these [snorts] first and third entries
  4666. 3:23:31in this truth table contradict each
  4667. 3:23:32other. And what I said about determinism
  4668. 3:23:35earlier is that a circuit is
  4669. 3:23:37deterministic if the outputs depend
  4670. 3:23:39strictly on the inputs. And clearly this
  4671. 3:23:42is not the case because we have this
  4672. 3:23:44output one
  4673. 3:23:47and it happens sometimes when x is one
  4674. 3:23:50and sometimes when x is zero. So this
  4675. 3:23:52output one isn't depending just on
  4676. 3:23:54whatever the input x is at the time.
  4677. 3:23:56There's clearly something else going on.
  4678. 3:23:58And the difference here is that the
  4679. 3:24:00output depends on not just the input but
  4680. 3:24:04also previous values that were emitted
  4681. 3:24:06by the circuit. And so this is what we
  4682. 3:24:08call a sequential circuit.
  4683. 3:24:13This is sequential logic. Sequential
  4684. 3:24:15logic does not have this property of
  4685. 3:24:17determinism that made things work out
  4686. 3:24:19well. And as we've seen, this isn't an
  4687. 3:24:22invalid circuit. We've done something
  4688. 3:24:24valid here. We've just taken the output
  4689. 3:24:25and feeding it back into one of the
  4690. 3:24:27inputs. But a truth table doesn't
  4691. 3:24:29actually work. And we're going to have
  4692. 3:24:31these things called timing diagrams that
  4693. 3:24:34that that better illustrate this
  4694. 3:24:36property. So let's try create a timing
  4695. 3:24:38diagram to explain what we saw here.
  4696. 3:24:45So I'm going to erase these again. So
  4697. 3:24:48this timing diagram is literally going
  4698. 3:24:51to be a graph with
  4699. 3:24:55time is the x axis and then we'll say a
  4700. 3:25:01uh we we'll have separate lines for the
  4701. 3:25:04x and y. So oops did not mean to erase
  4702. 3:25:09that
  4703. 3:25:11x and y. And we assume that they both
  4704. 3:25:15start off as zero. So we'll we'll we'll
  4705. 3:25:17let this be uh the the zeros line and
  4706. 3:25:21then we'll let the one for y be up here
  4707. 3:25:24and we'll do the same thing for x. And
  4708. 3:25:26so x and y are different but they both
  4709. 3:25:28progress in time together. So we want to
  4710. 3:25:30see how x and y change as we move
  4711. 3:25:34forward in time.
  4712. 3:25:36So we started off with zero and zero and
  4713. 3:25:40then I eventually at some point changed
  4714. 3:25:44this x to be a one and at the same exact
  4715. 3:25:47time this y became a one. So I set x to
  4716. 3:25:51be a one and then y became a one and
  4717. 3:25:53then we just left it like that for a
  4718. 3:25:55little bit.
  4719. 3:25:57And you may be wondering
  4720. 3:26:00uh I said earlier that these logic gates
  4721. 3:26:02have a little bit of delay in the real
  4722. 3:26:03world. And so if I set X to be one,
  4723. 3:26:05there should be a little bit of delay
  4724. 3:26:06before Y becomes one. And that's that's
  4725. 3:26:08the truth. But we we won't worry about
  4726. 3:26:11that delay for this part. Uh we don't
  4727. 3:26:12want to over complicate too much. So
  4728. 3:26:15let's just assume that this logic gate
  4729. 3:26:17works instantaneously. As soon as one of
  4730. 3:26:19the input changes, the output
  4731. 3:26:21immediately reflects what the correct
  4732. 3:26:23value of that logic gate should be. So
  4733. 3:26:26then now we have one and one. But the
  4734. 3:26:29difference is that I noticed that when
  4735. 3:26:31we brought this x back down to zero
  4736. 3:26:34that y stayed as a one and then time
  4737. 3:26:38continued and they both just stayed
  4738. 3:26:39where they were at. And it turns out no
  4739. 3:26:41matter how many times we bring this up
  4740. 3:26:43or down this y is just going to stay at
  4741. 3:26:46one.
  4742. 3:26:47And so this is what we call a timing
  4743. 3:26:50diagram.
  4744. 3:26:54This right here shows the state of a
  4745. 3:26:57sequential circuit as we pass through
  4746. 3:26:59time. And we saw the issue with the
  4747. 3:27:02truth table is that a truth table does
  4748. 3:27:03not complete the full picture for a
  4749. 3:27:05non-deterministic circuit. But a tying
  4750. 3:27:08diagram does let us see everything
  4751. 3:27:09that's happening. And so this is
  4752. 3:27:12something that we will see uh
  4753. 3:27:14increasingly in the next segment of the
  4754. 3:27:16video. But what we'd like to now do is
  4755. 3:27:19I'd like to show you how you can build a
  4756. 3:27:21sequential circuit that actually lets
  4757. 3:27:23you store a value. So we talked about
  4758. 3:27:26this this 8 bit adder we have here and
  4759. 3:27:29that we wanted to be able to store the
  4760. 3:27:30outputs that are are coming out onto
  4761. 3:27:33these output lines and we're going to
  4762. 3:27:35dumb this down. We we didn't build this
  4763. 3:27:37this 8 bit adder all at once. We started
  4764. 3:27:40with a one bit adder and then scaled up.
  4765. 3:27:42So we're gonna do the same thing. We're
  4766. 3:27:43going to try to build a circuit that is
  4767. 3:27:45sequential that can save one bit of
  4768. 3:27:48data. And we'd like to design this
  4769. 3:27:51circuit so that we can choose when we we
  4770. 3:27:53save the value um and update its value
  4771. 3:27:56afterwards. And so what we're actually
  4772. 3:27:59going to do is I'm going to scroll all
  4773. 3:28:02the way back up to one of the first
  4774. 3:28:06things I showed you, and that was this
  4775. 3:28:08selector gate right here. And I'm going
  4776. 3:28:11to copy this
  4777. 3:28:13down below
  4778. 3:28:20and this will go right here. And this
  4779. 3:28:25selector circuit is deterministic.
  4780. 3:28:29The output depended strictly on the
  4781. 3:28:32inputs. And so uh if you happen to not
  4782. 3:28:35see the earlier part of the video, then
  4783. 3:28:36I'll just go ahead and quickly
  4784. 3:28:37summarize. Our selector circuit takes
  4785. 3:28:40two inputs A and B
  4786. 3:28:43and the output of that circuit will be
  4787. 3:28:46the A input if C is zero
  4788. 3:28:50I.e. If C is zero, then we let whatever
  4789. 3:28:52A is flow through. And if C is one, then
  4790. 3:28:56we let whatever B is flow through. And I
  4791. 3:28:59originally showed you this this circuit
  4792. 3:29:01earlier in the video to show you uh a
  4793. 3:29:04motivation as to how you could build
  4794. 3:29:06more interesting circuits by chaining
  4795. 3:29:08logic gates together. And this was the
  4796. 3:29:10first example I showed. But it turns out
  4797. 3:29:12we can uh we can we can kind of build
  4798. 3:29:15something very interesting by
  4799. 3:29:18by making this selector
  4800. 3:29:20non-deterministic.
  4801. 3:29:22And so let's see just how we can do
  4802. 3:29:25that. And what we saw when we did this
  4803. 3:29:28orgate earlier, we started with the
  4804. 3:29:30ordinary orgate which looks like this.
  4805. 3:29:34And we made it sequential
  4806. 3:29:36by doing something like this where we
  4807. 3:29:38took the output and wired it back in.
  4808. 3:29:41And then we had our one input and this
  4809. 3:29:43was simultaneously this y line was an
  4810. 3:29:45output and it is an input over here. So
  4811. 3:29:50we're going to be able to do something
  4812. 3:29:52very similar
  4813. 3:29:54with this selector.
  4814. 3:29:56So I'll keep the original selector up
  4815. 3:29:58there for reference.
  4816. 3:30:00And
  4817. 3:30:03then I will bring this one down.
  4818. 3:30:07And all I'm gonna do is with this Y, I'm
  4819. 3:30:11going to get rid of our input A here
  4820. 3:30:14and feed it back in.
  4821. 3:30:17And I don't know why I drew that so far
  4822. 3:30:18back. So I'll redraw that.
  4823. 3:30:21And now let's see what happens. So our
  4824. 3:30:26inputs are kind of weird. We have this
  4825. 3:30:28this C and B. Uh it doesn't really
  4826. 3:30:30matter what they're called. Well, why
  4827. 3:30:31don't we just uh make this easier and
  4828. 3:30:33rename this A because we no longer have
  4829. 3:30:35an A input. And so we have a A input and
  4830. 3:30:38a C input. And I'll keep the C as C
  4831. 3:30:41because later it's going to stand for
  4832. 3:30:42the word clock, but we don't really know
  4833. 3:30:44what that is yet or why. So I'll I'll
  4834. 3:30:47leave that there just so when it is
  4835. 3:30:48introduced later, it's not as confusing
  4836. 3:30:50to understand. Uh but for now, let's
  4837. 3:30:52just think of it as the control line
  4838. 3:30:54because that's kind of what we saw in
  4839. 3:30:55the selector. The C is what controls
  4840. 3:30:57which the inputs goes through. And then
  4841. 3:30:59this Y stays the same. So let's see what
  4842. 3:31:02happens. Let's suppose we start our
  4843. 3:31:04circuit and C is a zero.
  4844. 3:31:09Then we know that when C is a zero, it
  4845. 3:31:12lets the value up top flow through. And
  4846. 3:31:14let's also assume when we start the
  4847. 3:31:15circuit, everything is a zero. So this Y
  4848. 3:31:18is also a zero. So we have a Y coming
  4849. 3:31:20out and then we have a Y over here,
  4850. 3:31:22which means and you know the Y flows
  4851. 3:31:24through. So the if we uh even try to
  4852. 3:31:26change this a to a one let's say then uh
  4853. 3:31:29the other output the other input to this
  4854. 3:31:31and gates a zero. So this this output of
  4855. 3:31:34this and gate is always going to be z is
  4856. 3:31:36zero regardless of what a is and then we
  4857. 3:31:39have the zero ored with the zero and so
  4858. 3:31:43we're always going to output a zero. But
  4859. 3:31:45then let's suppose we have our zero
  4860. 3:31:48here. So we have our zero on y still.
  4861. 3:31:52And let's let's go back to zero A. And
  4862. 3:31:55now let's suppose we flip C from 0 to
  4863. 3:31:58one. And now what this means is that
  4864. 3:32:00whatever's on A's value should flow
  4865. 3:32:02through. So [snorts]
  4866. 3:32:04we have this we have this zero going
  4867. 3:32:06through and it's just going to propagate
  4868. 3:32:08out. And it's also we already had a
  4869. 3:32:09zero. So it's going to propagate in. And
  4870. 3:32:11that's the same thing as before.
  4871. 3:32:14But things get a little bit interesting
  4872. 3:32:17when we actually take this a and make it
  4873. 3:32:21a one. And that's not the only thing we
  4874. 3:32:23need to make this interesting cuz we
  4875. 3:32:24know when we make a a one then this is
  4876. 3:32:26going to be a one which means this is
  4877. 3:32:28automatically going to be a one and this
  4878. 3:32:31is the same one as above. So all that's
  4879. 3:32:34really changed right now is is is
  4880. 3:32:35nothing. We've seen that when C was
  4881. 3:32:38started off as zero, then we actually
  4882. 3:32:41weren't able to change the output at all
  4883. 3:32:43because it was just the the input the
  4884. 3:32:47output was flowing through in a circular
  4885. 3:32:49fashion. But now in C's one, we let this
  4886. 3:32:52a flow through and it kind of controls
  4887. 3:32:55this entire circuit because it flows
  4888. 3:32:57through and it's flowing back around
  4889. 3:32:59here.
  4890. 3:33:00But let's look at what happens the
  4891. 3:33:03moment we go from a uh from c being one
  4892. 3:33:06to zero. And this is where things get a
  4893. 3:33:09little bit tricky to rationalize.
  4894. 3:33:12But what we see here is that we start
  4895. 3:33:16off we have a one coming out here which
  4896. 3:33:19is a one up here. And I'm just actually
  4897. 3:33:22going to erase that cuz it's the same
  4898. 3:33:23one. It's the same wire. And we have C
  4899. 3:33:26being one. And the moment we go from C
  4900. 3:33:28being one to zero,
  4901. 3:33:30then what happens is that this becomes a
  4902. 3:33:34zero and this becomes a one. And we know
  4903. 3:33:38that the output of 0 and one is zero
  4904. 3:33:41here and the output of one and one is
  4905. 3:33:43one here. And so we have this one come
  4906. 3:33:46through and then the output of that
  4907. 3:33:47orgate is still going to be a one. And
  4908. 3:33:51this line doesn't change at all. And
  4909. 3:33:55that's uh a little it may may take a
  4910. 3:33:57little bit to understand that but
  4911. 3:33:59nothing crazy has happened. But if we
  4912. 3:34:01change a to zero
  4913. 3:34:04then nothing is going to change either
  4914. 3:34:06because we already know that this and
  4915. 3:34:08gate is always going to output a zero.
  4916. 3:34:10And we know that when c is equal to zero
  4917. 3:34:13the uh whatever value is on this line is
  4918. 3:34:15going to flow through. So what this
  4919. 3:34:17pretty much means is we have two
  4920. 3:34:18observations is when C is zero
  4921. 3:34:23output doesn't change output doesn't
  4922. 3:34:26change
  4923. 3:34:29when C is one.
  4924. 3:34:35The output
  4925. 3:34:37follows A. And I'm using follows as
  4926. 3:34:41synonymous with equals. The output is
  4927. 3:34:42just going to be whatever A is. And the
  4928. 3:34:45interesting thing that we saw is that
  4929. 3:34:49when C goes from 1 to zero,
  4930. 3:34:55C goes from 1 to zero, then we actually
  4931. 3:35:01freeze whatever the output is at that
  4932. 3:35:03exact time. We kind of and the output at
  4933. 3:35:06that exact time was whatever on was on
  4934. 3:35:08the value of A. So when C goes from one
  4935. 3:35:11to zero, we lock in the value of A. we
  4936. 3:35:14lock in the value of a
  4937. 3:35:17and it's not going to change
  4938. 3:35:20the entire whenever c is zero as we
  4939. 3:35:22already dictated because we set it up
  4940. 3:35:23here we have a rule that the output
  4941. 3:35:25doesn't change when c is zero. So when C
  4942. 3:35:27is one, we have this output following
  4943. 3:35:29whatever A is. And then the really
  4944. 3:35:31interesting thing is the moment we go
  4945. 3:35:33from C being one to zero, we kind of
  4946. 3:35:35latch in A's value.
  4947. 3:35:38And this is very interesting because now
  4948. 3:35:41we can freely change A and the output
  4949. 3:35:45doesn't change because of our rule up
  4950. 3:35:46here. So in an interesting way, this is
  4951. 3:35:50actually storing data because what we
  4952. 3:35:53have happening is if I wanted to store a
  4953. 3:35:56value on this line Y, then what you
  4954. 3:35:59could use, what you could infer from
  4955. 3:36:01these three rules is that you would need
  4956. 3:36:04to set the value of C to one to let the
  4957. 3:36:07output equal whatever A is. Set A to
  4958. 3:36:10whatever you want it to be. And so then
  4959. 3:36:12the output currently equals A, which is
  4960. 3:36:14what you wanted it wanted the value to
  4961. 3:36:16save. And then you set the C from one to
  4962. 3:36:19zero and that locks in the value. And so
  4963. 3:36:21the output is still the exact same thing
  4964. 3:36:24that you had as a but now you can change
  4965. 3:36:26A freely and the output is still
  4966. 3:36:28captured. And this is really useful.
  4967. 3:36:31And what we've done now is we've we've
  4968. 3:36:34effectively stored data. And I think
  4969. 3:36:37this this diagram gets a little bit
  4970. 3:36:39confusing. So the same way we
  4971. 3:36:41represented a full adder with a
  4972. 3:36:43rectangle, let's go ahead and represent
  4973. 3:36:46the generic selector as so. So we're
  4974. 3:36:49going to call this selector notation.
  4975. 3:36:52And
  4976. 3:36:54we have a b and then uh we can call it c
  4977. 3:36:59for your your your choice or s for your
  4978. 3:37:02selector. I'll just call it call it c.
  4979. 3:37:05And then you have your output y. And all
  4980. 3:37:08this is saying is this is our original
  4981. 3:37:09selector before we had the output going
  4982. 3:37:11back to the input. So that right here
  4983. 3:37:13represents this. These are the same A,
  4984. 3:37:16B, and C and Y as before. And so what
  4985. 3:37:20we've done now, and I think a more
  4986. 3:37:22intuitive way to represent this is we've
  4987. 3:37:26taken
  4988. 3:37:28I don't know why I made that so
  4989. 3:37:29vertical.
  4990. 3:37:32We've taken this A output and gotten rid
  4991. 3:37:35of it. So we just have this B and this
  4992. 3:37:37clock, this carry, this choice, and our
  4993. 3:37:42output Y
  4994. 3:37:44is still going to be an output, but it's
  4995. 3:37:46also going to serve as an input. And you
  4996. 3:37:48know the rule selector
  4997. 3:37:51is that we've seen that we kind of have
  4998. 3:37:53this above. So when C is zero, the
  4999. 3:37:55output doesn't change. When C is one,
  5000. 3:37:58the output's going to follow, and I'm
  5001. 3:38:00just going to replace this with A. It's
  5002. 3:38:02going to follow whatever A is. And then
  5003. 3:38:05the interesting case is when we go from
  5004. 3:38:07C to one, I've shown you that we
  5005. 3:38:08actually lock in a value. And we've come
  5006. 3:38:11to a point in the video, I've been
  5007. 3:38:13drawing on this screen for about 3 hours
  5008. 3:38:16that my my regressive handwriting skills
  5009. 3:38:20are are getting a little too complicated
  5010. 3:38:22to follow for you. We have these ones
  5011. 3:38:24and zeros diagrammed all over these
  5012. 3:38:26these these logic gates, and it's it's
  5013. 3:38:28not very intuitive what's happening
  5014. 3:38:29anymore.
  5015. 3:38:30And we're actually, as we saw earlier
  5016. 3:38:33with sequential logic, we have to rely a
  5017. 3:38:34lot more on timing diagrams.
  5018. 3:38:37So, it is time for me to actually switch
  5019. 3:38:40over to some computer software and make
  5020. 3:38:43this a lot easier for you to understand.
  5021. 3:38:46So, let's look at this exact same
  5022. 3:38:47example before, but let's let's migrate
  5023. 3:38:49it over to computer software. So, I'm
  5024. 3:38:51going to switch screens here now on this
  5025. 3:38:53new software. And as you can see, our
  5026. 3:38:55logic gates are actually drawn somewhat
  5027. 3:38:57visibly coherent.
  5028. 3:39:02And I've built the original selector
  5029. 3:39:04that we've seen. And I'd like to show
  5030. 3:39:06you that the selector works as we
  5031. 3:39:08expect. So what we have right here is
  5032. 3:39:11I'm going to set our our C is zero. So I
  5033. 3:39:14guess I should first distinguish
  5034. 3:39:15distinguish that this this dark green
  5035. 3:39:18represents a zero and this this lighter
  5036. 3:39:20green represents a one in the software
  5037. 3:39:22I'm using. The software I'm using is
  5038. 3:39:24actually called digital. The website
  5039. 3:39:26that I link below that I wrote with one
  5040. 3:39:29of my great friends is all of our
  5041. 3:39:32graphics are built in digital and you
  5042. 3:39:33can actually follow along. There's
  5043. 3:39:34instructions there. I won't explain
  5044. 3:39:36exactly how to do everything I do in
  5045. 3:39:38digital, but you will see me build out
  5046. 3:39:40in digital.
  5047. 3:39:41And
  5048. 3:39:44for now, all you should need to know,
  5049. 3:39:46and I'll walk through everything as we
  5050. 3:39:47go, is that this this light green is a
  5051. 3:39:48one and this dark green is a zero. So in
  5052. 3:39:51our first case, we can see that C is a
  5053. 3:39:53zero. And the rule we said earlier for a
  5054. 3:39:56regular selector is that if c is equal
  5055. 3:39:59to zero then y should follow follow
  5056. 3:40:02whatever a is. And we see this
  5057. 3:40:04happening. You can kind of see and I
  5058. 3:40:05think it's easier to visualize now the
  5059. 3:40:07this this this a just flowing through.
  5060. 3:40:10And you can see b's value just gets cut
  5061. 3:40:13off by this and gate because its value
  5062. 3:40:14is zero. And this is far more intuitive
  5063. 3:40:17to see on the software once you can
  5064. 3:40:18recognize the the lighter green being a
  5065. 3:40:22one and this dark green being a zero.
  5066. 3:40:25And then we can see that if I if I
  5067. 3:40:28toggle C, then the output should follow
  5068. 3:40:30B. And that's exactly what happens. So
  5069. 3:40:32the output follows B no matter what A
  5070. 3:40:36is. And we've we've built a a our our
  5071. 3:40:40selector circuit. And another cool thing
  5072. 3:40:43the software does is it will let me
  5073. 3:40:46generate a truth table automatically. So
  5074. 3:40:48we don't have to do everything by hand.
  5075. 3:40:50So you can see
  5076. 3:40:52for this this circuit we have right here
  5077. 3:40:55that the truth table that I I have drawn
  5078. 3:40:58out previously in the video is equal to
  5079. 3:41:00the same truth table that's being shown
  5080. 3:41:01right here. And I won't switch back to
  5081. 3:41:04my drawing because it's going to be a
  5082. 3:41:06pain, but you can go back in the video
  5083. 3:41:08and see yourself that's true. And then
  5084. 3:41:11the last thing I showed you before I
  5085. 3:41:14before I switched to this view was that
  5086. 3:41:17there's a a notation the to draw a
  5087. 3:41:20selector. So, and we said it was
  5088. 3:41:22represented with this right here. So,
  5089. 3:41:24let's go ahead and see what that looks
  5090. 3:41:26like. So, I'm going to copy
  5091. 3:41:31this over
  5092. 3:41:34and
  5093. 3:41:37we will see what the output looks like.
  5094. 3:41:39So I have our A and let me call this A2
  5095. 3:41:43so we don't get it confused. Then we
  5096. 3:41:46have B. I'll call it B2. And then we can
  5097. 3:41:50also have our C2. And these are the
  5098. 3:41:52same. This is the same circuit before.
  5099. 3:41:54And and we're going to have our
  5100. 3:41:57Y2. And another thing about the circuit
  5101. 3:41:59is we represent our inputs with these
  5102. 3:42:01squares. And you can see you saw me
  5103. 3:42:02previously clicking them. And our
  5104. 3:42:04outputs are going to be these circles.
  5105. 3:42:09So now if I if I play this, let's
  5106. 3:42:11convince ourselves this is true. So C2
  5107. 3:42:13is zero. So the whatever we set A, it
  5108. 3:42:16should flow through and that should
  5109. 3:42:18happen regardless of what regardless of
  5110. 3:42:21what B is and we see that happening. And
  5111. 3:42:24then if I toggle B, then nothing should
  5112. 3:42:26happen. And that's the case. And then if
  5113. 3:42:29I flip to C, flip C to one, then we see
  5114. 3:42:33B flows through. And toggling B lets
  5115. 3:42:35that value flow through. and toggling A
  5116. 3:42:38has no effect. And hopefully you that is
  5117. 3:42:42enough to convince you that these are
  5118. 3:42:44are are equivalent circuits. And I can
  5119. 3:42:49actually delete this right here and do
  5120. 3:42:51the same truth table generation. And if
  5121. 3:42:55you go back uh less time this time, just
  5122. 3:42:57a minute, you'll see that these true
  5123. 3:42:58these two truth tables are the same.
  5124. 3:43:01And so now what we want to do is do what
  5125. 3:43:05I showed earlier where we took the
  5126. 3:43:08output and chain that to the input. So
  5127. 3:43:11I'm going to rename this. I'm going to
  5128. 3:43:14rename everything accordingly. And this
  5129. 3:43:17A goes away. But let's just call this
  5130. 3:43:19this B and let's call this C. And let's
  5131. 3:43:23actually just we said A when I was
  5132. 3:43:25drawing on the screen. So let's be
  5133. 3:43:26consistent here.
  5134. 3:43:28And let's have this output feed into the
  5135. 3:43:32input. So now if I do this then the
  5136. 3:43:36point I was trying to make earlier which
  5137. 3:43:37I think may have been a little hard to
  5138. 3:43:39visualize without seeing it in this
  5139. 3:43:40manner is that whenever C is zero the
  5140. 3:43:45output is not going to change because
  5141. 3:43:48whenever C is zero we let this top input
  5142. 3:43:51flow through the output. And then
  5143. 3:43:53whenever C is one, the output's just
  5144. 3:43:56going to be whatever the input is,
  5145. 3:43:58whatever this value A is because a
  5146. 3:44:00selector behaves such that when the C is
  5147. 3:44:02equal to one, we let the bottom input
  5148. 3:44:04flow to the output. And
  5149. 3:44:08the interesting thing was then when we
  5150. 3:44:11toggled C from one to zero and what we
  5151. 3:44:14said is going to happen is that at the
  5152. 3:44:16moment we toggle C from one to zero,
  5153. 3:44:18it's going to go from letting this
  5154. 3:44:20bottom input flow through, it's going to
  5155. 3:44:22go from letting the bottom input flowing
  5156. 3:44:24through to letting the top input flow
  5157. 3:44:26through. And the top input is actually
  5158. 3:44:29equal to the bottom input whenever C is
  5159. 3:44:31one because it's all the same circuit as
  5160. 3:44:33you've been able to see. So what's going
  5161. 3:44:35to happen is I'm going to toggle this to
  5162. 3:44:37zero and this value is going to start
  5163. 3:44:39coming through and this value is a one
  5164. 3:44:42and it's going to trap this one in this
  5165. 3:44:44loop right here. We have a little bit of
  5166. 3:44:45a feedback loop happening and I know
  5167. 3:44:48this is a little difficult to understand
  5168. 3:44:50but uh it should be it should be this is
  5169. 3:44:54what I explained is the other one of two
  5170. 3:44:57hardest concepts to understand in the
  5171. 3:44:59video is what I'm showing you right now.
  5172. 3:45:01But let's see what happens. So we
  5173. 3:45:03toggled this C from one to zero. And
  5174. 3:45:06this is what you would expect because
  5175. 3:45:09the input at that time we toggled C from
  5176. 3:45:121 to zero was a one up here. And so when
  5177. 3:45:14we actually toggle C back to zero, we
  5178. 3:45:16just let this one flow through. And now
  5179. 3:45:18it doesn't matter what we set A to
  5180. 3:45:20because it's we have this rule that
  5181. 3:45:22whenever C is zero, we just let this top
  5182. 3:45:24input flow through. And so we've
  5183. 3:45:26actually trapped the value in here. And
  5184. 3:45:28if we wanted to trap a uh a one a zero
  5185. 3:45:31in here, then what we need to do is have
  5186. 3:45:34C go from one back to zero. So in order
  5187. 3:45:36to do that, we have to set C back to
  5188. 3:45:38one. And then we would set whatever
  5189. 3:45:40value we want on A. And then to trap it,
  5190. 3:45:42we'd set C from one back to zero. And
  5191. 3:45:45the the remarkable thing that's
  5192. 3:45:47happening here is that we trap the value
  5193. 3:45:49on A at the exact moment that A C goes
  5194. 3:45:53from one to zero. And let me show you
  5195. 3:45:55exactly what I mean. So I am going to
  5196. 3:45:58add another cool thing this software has
  5197. 3:46:00is the timing diagram we saw from before
  5198. 3:46:03and I'm going to bring this back in case
  5199. 3:46:05I need to use a lot of space. But we
  5200. 3:46:08start off with C. These are all zeros.
  5201. 3:46:10Uh we see the two inputs are zeros and
  5202. 3:46:12the outputs are zero. If I toggle one,
  5203. 3:46:15if I toggle this A, nothing happens
  5204. 3:46:16because C is zero. And then when C is
  5205. 3:46:20one, you can see on this diagram right
  5206. 3:46:22here that the value of Y is just equal
  5207. 3:46:25to whatever the value of A is. And then
  5208. 3:46:29suppose we wanted to lock in a one as
  5209. 3:46:31the output. I need to have C go from the
  5210. 3:46:33one it is right now to the zero. And you
  5211. 3:46:38can see that C goes from one to zero.
  5212. 3:46:42And now it's at this my claim is that
  5213. 3:46:44it's at this exact moment that we lock
  5214. 3:46:46in the value because now whenever I
  5215. 3:46:48change a c is going to stay at that
  5216. 3:46:51original value that it was at. And now
  5217. 3:46:54suppose I wanted to lock a zero in. We
  5218. 3:46:57said it only happens we can only lock a
  5219. 3:47:00value in when this C goes from one to
  5220. 3:47:02zero. So what I need to do is bring C
  5221. 3:47:04back up to one. And as soon as we do
  5222. 3:47:06that we can see that the output starts
  5223. 3:47:08updating. It follows our input A. But to
  5224. 3:47:11lock it in, I need to bring the C back
  5225. 3:47:12down to a zero. And now you can see the
  5226. 3:47:15C, we have this falling clock edge. We
  5227. 3:47:18have a rising clock edge here and a
  5228. 3:47:19falling clock edge here. And the falling
  5229. 3:47:21clock edge is what actually latches in
  5230. 3:47:23that value. So now I toggle A and
  5231. 3:47:26nothing changes.
  5232. 3:47:28So
  5233. 3:47:30the good news is that if you follow me,
  5234. 3:47:34you're halfway through the complexity.
  5235. 3:47:37We're going to turn this up one more
  5236. 3:47:39notch and then we're going to be done.
  5237. 3:47:41So the good news is you're halfway. And
  5238. 3:47:43the bad news is that it gets a little
  5239. 3:47:45bit more complicated than this. So if
  5240. 3:47:47you do find yourself confused by this,
  5241. 3:47:50this is not something that you should
  5242. 3:47:52let stop you from proceeding. You should
  5243. 3:47:54check out the website that's linked in
  5244. 3:47:55the description that I wrote. It's it's
  5245. 3:47:57meant to illustrate this example a lot
  5246. 3:47:59more detail.
  5247. 3:48:00And you should just draw through this
  5248. 3:48:03and play with it by hand until you're
  5249. 3:48:05able to rationalize what's happening.
  5250. 3:48:08Personally, when I learned this, I found
  5251. 3:48:09this incredibly difficult and was not
  5252. 3:48:11able to wrap my mind around it for days.
  5253. 3:48:13It took me multiple sessions of opening
  5254. 3:48:15up my computer and drawing things
  5255. 3:48:17through before I finally had this aha
  5256. 3:48:19moment and everything snapped. So, don't
  5257. 3:48:22be discouraged if it takes you some
  5258. 3:48:23time. And
  5259. 3:48:26once you do feel like you have an
  5260. 3:48:28understanding of everything I've spoken
  5261. 3:48:30about this circuit that we have on
  5262. 3:48:31screen, then you can continue is that
  5263. 3:48:35this is a something called a positive
  5264. 3:48:38latch. And what that means is that the
  5265. 3:48:40output's only ever going to change when
  5266. 3:48:43C is one. When C is positive, the
  5267. 3:48:46output's going to change.
  5268. 3:48:48Interestingly enough, we could actually
  5269. 3:48:50just negate C and we'd get something
  5270. 3:48:53called a negative latch. And that's the
  5271. 3:48:56same behavior is that the output of the
  5272. 3:48:59Y is only going to change when C is
  5273. 3:49:01zero. So on the positive latch that we
  5274. 3:49:04just saw, you lock in a value when the
  5275. 3:49:06the C line goes from a one to a zero.
  5276. 3:49:10And on the negative, it's the opposite.
  5277. 3:49:12You lock in the value when the C goes
  5278. 3:49:14from a zero to a one. So, let's actually
  5279. 3:49:18look at what that is. And I have
  5280. 3:49:21inverter here. And I probably shouldn't
  5281. 3:49:23have erased this. Let me
  5282. 3:49:27do this.
  5283. 3:49:29And now let's look at our timing
  5284. 3:49:31diagram. So
  5285. 3:49:34C is zero now. And previously we said
  5286. 3:49:39that when C is zero, the output's going
  5287. 3:49:42to follow this in this this top input
  5288. 3:49:45which is equal to the output. So it's
  5289. 3:49:47never going to change. But now we see
  5290. 3:49:49we're toggling A and it is changing. And
  5291. 3:49:51that's because when you invert this C
  5292. 3:49:54line to a selector, also known as a
  5293. 3:49:56multipplexer, it just inverts the
  5294. 3:49:59behavior. So the original selector let
  5295. 3:50:02when C is zero, it lets this top input
  5296. 3:50:04flow through. And when C is one, it lets
  5297. 3:50:07this bottom input flow through. But now
  5298. 3:50:09that we've inverted it, it's the
  5299. 3:50:10opposite. When C is zero, we let this
  5300. 3:50:12bottom input flow through. And then when
  5301. 3:50:14it's one, we let this top input flow
  5302. 3:50:16through. And so if I want to set a
  5303. 3:50:18value, I need to get it so that we we
  5304. 3:50:21have the value coming through. And then
  5305. 3:50:22I want to pick the output to be this
  5306. 3:50:25one. And we do that by setting C to be
  5307. 3:50:27one. So to set a value, I have my A.
  5308. 3:50:30Let's say suppose I want to store a one.
  5309. 3:50:33Then I need to put this this this C to
  5310. 3:50:35one. And now we're selecting this top
  5311. 3:50:37line. And now as much as I toggle A,
  5312. 3:50:39nothing happens. And then if I
  5313. 3:50:43set C again, then notice now that the
  5314. 3:50:45output follows A. And then at the moment
  5315. 3:50:49where I set C back to one, we start
  5316. 3:50:52picking this top input again. And so now
  5317. 3:50:53the output's never going to change. And
  5318. 3:50:57so this is called a negative latch
  5319. 3:51:00because it latches onto the value
  5320. 3:51:03whenever we go from
  5321. 3:51:09zero to one. So it's it's latching on
  5322. 3:51:14whenever we have this this input C being
  5323. 3:51:19one. And it turns out that there is a
  5324. 3:51:23shortcoming with what I've described
  5325. 3:51:24here. So this is a very interesting
  5326. 3:51:29combination of circuits and not that
  5327. 3:51:31many logic gates that actually enabled
  5328. 3:51:33us to to persist information. But the
  5329. 3:51:37issue is that if I go back to this this
  5330. 3:51:41original uh positive latch instead of
  5331. 3:51:45the one we were just looking at, the
  5332. 3:51:49output is changing
  5333. 3:51:53whenever C is one. And
  5334. 3:51:57it works out that if we're trying to
  5335. 3:51:58store a value, it's fine cuz we know
  5336. 3:52:00that whenever C is is falling from one
  5337. 3:52:04back to zero, it it it stores the value
  5338. 3:52:06in and then it's not going to change.
  5339. 3:52:08But we'd like that to be true until the
  5340. 3:52:11next time C falls from one to zero. But
  5341. 3:52:14that's not the case because as you can
  5342. 3:52:16see right here,
  5343. 3:52:19the outputs uh going from 0 to one. And
  5344. 3:52:22now we have we have this uh we have this
  5345. 3:52:25this output changing before there's
  5346. 3:52:27another falling edge. And so it's hard
  5347. 3:52:30to give full motivation as to why we
  5348. 3:52:32need this behavior.
  5349. 3:52:34But it turns out as something that will
  5350. 3:52:37become more clear as we design more
  5351. 3:52:39parts of this computer
  5352. 3:52:41is that if we can have information
  5353. 3:52:45only change at one specific time, then
  5354. 3:52:49it gives us more time to actually look
  5355. 3:52:51at this value. So we know that on our in
  5356. 3:52:54our current circuit that whenever C
  5357. 3:52:58falls from one to zero, it's safe to
  5358. 3:53:01assume that the output is going to be
  5359. 3:53:03the same. until C goes from 0 to one.
  5360. 3:53:08And this is this may seem fine. You're
  5361. 3:53:10saying, "Hey, look, this is a lot of
  5362. 3:53:11time, but what we'd actually like to do
  5363. 3:53:13is make this as long as possible."
  5364. 3:53:15Because I mentioned that these logic
  5365. 3:53:16gates take time to settle. So, we'd like
  5366. 3:53:18our output to be able to be used in
  5367. 3:53:21other parts of our computer for as long
  5368. 3:53:23as possible before we lose that stored
  5369. 3:53:26in value. So what we'd like to do and
  5370. 3:53:29what we're going to do next is update
  5371. 3:53:32this circuit such that it only
  5372. 3:53:37the output Y only updates every time C
  5373. 3:53:42goes from one to zero.
  5374. 3:53:45And this is going to be very useful
  5375. 3:53:49because then it means until the next
  5376. 3:53:52time C falls from 1 to zero that value Y
  5377. 3:53:57is going to be the last latched on
  5378. 3:53:59value. And you can see that's not the
  5379. 3:54:02case here because in between this C
  5380. 3:54:04going from 1 to zero and the next time C
  5381. 3:54:06goes from 1 to zero, we have our output
  5382. 3:54:09Y changing.
  5383. 3:54:11And so this circuit that we have right
  5384. 3:54:13here is called a latch. as I've
  5385. 3:54:15mentioned and we're going to build
  5386. 3:54:17something called a flipflop which has
  5387. 3:54:20this desirable property that the
  5388. 3:54:21output's only going to change when C
  5389. 3:54:23goes from one to zero. Build this. I'm
  5390. 3:54:27going to spoil the solution because it
  5391. 3:54:29is something that would be incredibly
  5392. 3:54:31difficult to come up with on our own.
  5393. 3:54:34And once I spoil the solution, we'll go
  5394. 3:54:36ahead and convince ourselves that it is
  5395. 3:54:38true.
  5396. 3:54:39So, the solution
  5397. 3:54:41is that we're going to take a positive
  5398. 3:54:43latch, which we have right here, and
  5399. 3:54:46feed it into a negative latch. And let's
  5400. 3:54:50go ahead and see what I mean by that.
  5401. 3:54:51So, I'm going to copy this positive
  5402. 3:54:53latch we have here, and I'm going bring
  5403. 3:54:55this over here. And what I'm saying is
  5404. 3:54:59that I'm going to take the output from
  5405. 3:55:01this first latch and make that be the
  5406. 3:55:04out the input into this other latch.
  5407. 3:55:08do.
  5408. 3:55:11So I take the output from this latch and
  5409. 3:55:15I'm going to make it be the input into
  5410. 3:55:17this latch. And then we we still want
  5411. 3:55:19the same property where we have one A,
  5412. 3:55:21one C, and the outputs the Y. So we we
  5413. 3:55:23have two C's here. So I'm going to get
  5414. 3:55:26rid of this. And I said we take a
  5415. 3:55:29positive latch, which we have here, and
  5416. 3:55:31feed it into a negative latch. And the
  5417. 3:55:33way just a few minutes ago that I took a
  5418. 3:55:35positive latch and made it into a
  5419. 3:55:37negative latch was was by inverting the
  5420. 3:55:39C line. Because we saw that inverting
  5421. 3:55:41the C line has this nice consequence
  5422. 3:55:45that it it flips the behavior of the
  5423. 3:55:47latch making it a positive to a negative
  5424. 3:55:51or vice versa.
  5425. 3:55:54Um, in our in our default case, the
  5426. 3:55:56software I'm using, it's a positive
  5427. 3:55:58latch by default, meaning that the input
  5428. 3:56:02the output follows the input whenever
  5429. 3:56:04C's value is positive. And if we invert
  5430. 3:56:07it, then we have the opposite behavior
  5431. 3:56:09where the output only follows C's input
  5432. 3:56:13when C is negative, zero.
  5433. 3:56:18Cool. So let's go ahead and
  5434. 3:56:22use our timing diagram that we have
  5435. 3:56:24automatically generated
  5436. 3:56:26to verify that this property holds
  5437. 3:56:31that the output Y is only ever going to
  5438. 3:56:34update at the exact moment C goes from 1
  5439. 3:56:37to zero. And the value that it'll latch
  5440. 3:56:41in at that moment that C goes from 1 to
  5441. 3:56:43zero is whatever value is on A. So let's
  5442. 3:56:46say we want to latch in a one here. So C
  5443. 3:56:49is zero. So I'm going to show the
  5444. 3:56:51output's not changing even when we
  5445. 3:56:52toggle A. That's correct. We have our
  5446. 3:56:54our input toggling, but there's no
  5447. 3:56:57falling edge. There's no C from one to
  5448. 3:56:59zero. So we don't change. Now,
  5449. 3:57:03um I'm going to set C to one. And we see
  5450. 3:57:07that C the output Y still doesn't
  5451. 3:57:10change. And this this is different than
  5452. 3:57:11what we saw before. But we saw before
  5453. 3:57:13the output y would have changed here.
  5454. 3:57:16But now as I keep toggling a nothing is
  5455. 3:57:18going to change because we are waiting
  5456. 3:57:20for this falling edge of c to happen. So
  5457. 3:57:23let's actually do that. The moment I
  5458. 3:57:25toggle c from one to zero we would
  5459. 3:57:28expect this y to latch in the value
  5460. 3:57:30finally.
  5461. 3:57:32And that's exactly what we see. And now
  5462. 3:57:34I toggle
  5463. 3:57:36a and nothing happens. And to to do the
  5464. 3:57:40cycle one more time, let's we've shown
  5465. 3:57:42ourselves once that this this y value
  5466. 3:57:44only updates when C falls. It has a
  5467. 3:57:46falling edge. Let's do that one more
  5468. 3:57:48time. Let's get back to that point and
  5469. 3:57:50say I want to replace it with a zero
  5470. 3:57:51now. So I bring C back up and I can show
  5471. 3:57:55you that toggling A has no effect. And
  5472. 3:57:57then I set A to what I want it to be.
  5473. 3:57:59And now the moment that C goes from one
  5474. 3:58:01to zero, we should be able to hope that
  5475. 3:58:03this Y value locks in the zero. And
  5476. 3:58:07that's exactly what happens.
  5477. 3:58:09And so
  5478. 3:58:12this is the exact circuit that we call a
  5479. 3:58:16flip-flop.
  5480. 3:58:18So let's try give a more reasonable and
  5481. 3:58:20intuitive explanation as to why this
  5482. 3:58:22behavior happens.
  5483. 3:58:24And this is something that you could
  5484. 3:58:27derive your on your own just by pausing
  5485. 3:58:29this video and playing around and trying
  5486. 3:58:31to offer up and justify the behavior of
  5487. 3:58:35the logicates that you just saw. But
  5488. 3:58:37I'll go ahead and spoil that out for
  5489. 3:58:39those of you who don't want to
  5490. 3:58:40rationalize on your own like myself
  5491. 3:58:42would have not wanted to. So
  5492. 3:58:46what we have here when we start this is
  5493. 3:58:49C is zero.
  5494. 3:58:51So whenever C is zero, the output here
  5495. 3:58:57is not going to change because we're
  5496. 3:58:59locking in this top value. So in order
  5497. 3:59:02to for us to get a value from A to Y, we
  5498. 3:59:05know that we have to get A onto this
  5499. 3:59:07line at some point or else it can't even
  5500. 3:59:09make it over here. So the only way to
  5501. 3:59:11get A onto the line is to toggle this C
  5502. 3:59:16positively. And now we can have A
  5503. 3:59:18getting onto this line over here.
  5504. 3:59:21But then the issue is that we also have
  5505. 3:59:23to we want to get A over here. And
  5506. 3:59:26because this is a negated clock, the
  5507. 3:59:28only way we can get a to come over here
  5508. 3:59:31is if we we get a
  5509. 3:59:35zero
  5510. 3:59:36onto this line because then a one would
  5511. 3:59:39come onto this line and let this value
  5512. 3:59:40through. And the only way we can then do
  5513. 3:59:43that is we now have the value on this
  5514. 3:59:46intermediate wire that connects the two.
  5515. 3:59:49And it's the exact moment that I toggle
  5516. 3:59:52C that this value gets trapped in this
  5517. 3:59:55wire right here. Because when I toggle
  5518. 3:59:58C, then this original latch that we're
  5519. 4:00:00so comfortable with now, the output of
  5520. 4:00:03it is just going to be this top line,
  5521. 4:00:04which is a one. So C goes down and then
  5522. 4:00:08this one is going to be locked on here
  5523. 4:00:12even if I toggle A. So if I do that, I
  5524. 4:00:16toggle C down. Then notice how me
  5525. 4:00:18toggling A has no effect on this middle
  5526. 4:00:20line. This value is is locked into this
  5527. 4:00:23intermediate wire. And
  5528. 4:00:28the last thing
  5529. 4:00:32is that at that same time that we had C
  5530. 4:00:38going from this one letting this value
  5531. 4:00:42through to a zero, we also had something
  5532. 4:00:44interesting happening over here because
  5533. 4:00:47we we trap the one on this line. But
  5534. 4:00:50then having this clock become zero means
  5535. 4:00:53that this line becomes one and that we
  5536. 4:00:56actually let this bottom input through.
  5537. 4:00:59So now that in simultaneously what
  5538. 4:01:01happens is we trapped the value of a on
  5539. 4:01:04this intermediate wire and we now let
  5540. 4:01:07our gate output what that intermediate
  5541. 4:01:09wire is.
  5542. 4:01:11And I know that probably sounds fuzzy.
  5543. 4:01:15So, so take the time to justify this on
  5544. 4:01:17your own, but that's the high level of
  5545. 4:01:19what's happening here. And I'm again not
  5546. 4:01:21going to spend too much time dwelling on
  5547. 4:01:23this because the article that I wrote,
  5548. 4:01:26which is linked in the description, goes
  5549. 4:01:28through a lot more. So, I will say that
  5550. 4:01:31you are 95% of the way done with
  5551. 4:01:34learning the second hardest concept in
  5552. 4:01:37this video. There's one final thing we
  5553. 4:01:39need to do. And so recall that I taught
  5554. 4:01:42you what a latch is and it was a
  5555. 4:01:44positive latch. And then I showed you
  5556. 4:01:46that inverting the clock makes it a
  5557. 4:01:48negative latch.
  5558. 4:01:50So what we've actually built here is not
  5559. 4:01:53a flip-flop, but it's a negative
  5560. 4:01:56flip-flop because the value is only ever
  5561. 4:01:58locked in on the falling edge. When C
  5562. 4:02:02becomes negative, i.e. it goes from one
  5563. 4:02:05to zero uh a more negative value then
  5564. 4:02:09the value is latched in. And so this is
  5565. 4:02:11incredibly useful. But what if we wanted
  5566. 4:02:13to have the opposite behavior where we
  5567. 4:02:16wanted the value to get latched in when
  5568. 4:02:19C goes from 0 to one? It turns out that
  5569. 4:02:23we just
  5570. 4:02:26negate our order of operations. So right
  5571. 4:02:28now we have a positive latch being fed
  5572. 4:02:31into a negative latch. If we just have a
  5573. 4:02:33negative latch being fed into a positive
  5574. 4:02:35latch, then the output is that our our
  5575. 4:02:40our intuition remains the same except
  5576. 4:02:43the output y only ever changes on a
  5577. 4:02:47rising edge when C is becoming positive.
  5578. 4:02:50So let me go ahead and update this to
  5579. 4:02:53show that. So I get rid of this negator.
  5580. 4:02:57So now we have regular C coming into
  5581. 4:02:59this selector. So, this is a positive
  5582. 4:03:01latch, but we need to make this one
  5583. 4:03:03negative. And so, I'm going to do that
  5584. 4:03:05by bringing this down here.
  5585. 4:03:09And I'm going to add a negator.
  5586. 4:03:16Let me rotate this
  5587. 4:03:20like so.
  5588. 4:03:22And now you can see that this this first
  5589. 4:03:26this first latch is inverted meaning
  5590. 4:03:29meaning it's a negative latch and then
  5591. 4:03:31this one's the positive latch.
  5592. 4:03:34So now what happens is that the value of
  5593. 4:03:38C only ever changes on the rising clock
  5594. 4:03:41edge. This is because when C is zero we
  5595. 4:03:43have this value on the intermediate
  5596. 4:03:45wire. We have this A coming to the
  5597. 4:03:47intermediate wire. And the exact moment
  5598. 4:03:49that we toggle C from a zero to a one,
  5599. 4:03:52then this becomes a zero over here. So
  5600. 4:03:57we trap in this current value of A in
  5601. 4:04:01this feedback loop up top. So we're
  5602. 4:04:03trapped in the value of a onto this
  5603. 4:04:05line. And at the same time, this becomes
  5604. 4:04:08a one. So instead of selecting the
  5605. 4:04:10feedback loop to output, we select the
  5606. 4:04:13value of this intermediate line to
  5607. 4:04:14output, which is the value of a. And so
  5608. 4:04:17we'd expect that when I toggle this from
  5609. 4:04:20zero to one that the y becomes a one.
  5610. 4:04:22That's exactly what happens. And now
  5611. 4:04:25when I toggle this, the output is not
  5612. 4:04:27going to change because we've locked in
  5613. 4:04:30the original value of a onto this wire.
  5614. 4:04:32And it's not going to change because of
  5615. 4:04:34this this latch right here.
  5616. 4:04:38Cool. So this is something very
  5617. 4:04:40powerful.
  5618. 4:04:42And I'll prove it one more time that the
  5619. 4:04:44output only changes when C hits a rising
  5620. 4:04:47edge. And this right here is incredibly
  5621. 4:04:51powerful. A latch did let us store
  5622. 4:04:54values, but this flip-flop is going to
  5623. 4:04:56let us store values in in a in a way
  5624. 4:04:58that modern computers actually do do
  5625. 4:05:00things. And it's very important for us
  5626. 4:05:03that we only update the value that we're
  5627. 4:05:05storing on a specific clock edge, either
  5628. 4:05:08the rising one or falling one.
  5629. 4:05:12because that maximizes the amount of
  5630. 4:05:14time that the output is stable that this
  5631. 4:05:16this value is going to be just whatever
  5632. 4:05:19we intend it to be versus we saw in a
  5633. 4:05:22latch that half the time whenever
  5634. 4:05:25depending on which type of latch you're
  5635. 4:05:27looking at but for either when C is zero
  5636. 4:05:30or C is one depending on the type of
  5637. 4:05:32latch the output is just going to follow
  5638. 4:05:34whatever the input is and that's not as
  5639. 4:05:35useful to us because we would we would
  5640. 4:05:37like to persist the value Y for as long
  5641. 4:05:40as possible.
  5642. 4:05:41So, now that we've we've seen how to
  5643. 4:05:45build this flip-flop, let's actually put
  5644. 4:05:48it to use. So, in the previous section
  5645. 4:05:51of this video, we built an alou. And our
  5646. 4:05:53motivation for even doing any of what I
  5647. 4:05:55was talking about is that we'd like to
  5648. 4:05:58be able to save the value of the output
  5649. 4:06:01of our of our adder. So, if I if I add
  5650. 4:06:04two values, I'd like to be able to save
  5651. 4:06:06them. And what we have right here is a
  5652. 4:06:09circuit that lets us save one value. But
  5653. 4:06:15what happens if we want a circuit that
  5654. 4:06:17lets us save two values? And it's pretty
  5655. 4:06:19easy. All we have to do is just have two
  5656. 4:06:23flip-flops.
  5657. 4:06:24And what that means is that if we have a
  5658. 4:06:28value A1 and A2 we want to persist, then
  5659. 4:06:31we could feed A1 into the first
  5660. 4:06:33flip-flop, A2 into the second flip-flop,
  5661. 4:06:36and then feed the same C control line
  5662. 4:06:39into both of those. And then at the
  5663. 4:06:41exact moment we go from a r a rising
  5664. 4:06:44clock edge, then we latch in both of
  5665. 4:06:46those values. And the two outputs are Y1
  5666. 4:06:48and Y2 effectively save the value of the
  5667. 4:06:51ALU. And before I want to move on, I
  5668. 4:06:55wanted to add this in and that is a
  5669. 4:06:57little bit of notation. So we saw when
  5670. 4:06:59we built our full adders that it was
  5671. 4:07:01pretty difficult to conceptualize what
  5672. 4:07:03was happening. We had so many of them.
  5673. 4:07:05So we abstracted away as a little
  5674. 4:07:06rectangle. And then not too long ago, I
  5675. 4:07:10showed you how to abstract away the
  5676. 4:07:13selector that we built with this
  5677. 4:07:14trapezoid shape. And it turns out
  5678. 4:07:18because this flip-flop we've built is so
  5679. 4:07:20common, it's called a D flip-flop.
  5680. 4:07:22Actually, there's there's different
  5681. 4:07:23variants of flip-flops, but the one
  5682. 4:07:25we'll use for for our computer, we only
  5683. 4:07:27need D flip-flops.
  5684. 4:07:29And we can abstract it away with this
  5685. 4:07:32rectangle.
  5686. 4:07:34And what we can see is that takes in a
  5687. 4:07:38D, which stands for data because that's
  5688. 4:07:40the data that we're going to be saving.
  5689. 4:07:42takes in a C, which is this clock
  5690. 4:07:45signal. And we've we've I I've kind of
  5691. 4:07:48beat around the bush here, but we'll see
  5692. 4:07:50as soon as we finish the next section
  5693. 4:07:52why what this clock exactly is and what
  5694. 4:07:54it's going to do. And then we get two
  5695. 4:07:56outputs on the right side. Uh it's
  5696. 4:07:58called Q and not Q. So, traditionally,
  5697. 4:08:01if you do decide to research into
  5698. 4:08:02flip-flops more or even latches, you'll
  5699. 4:08:04find that Q is the common symbol to
  5700. 4:08:07denote the uh stored value. And and in
  5701. 4:08:11this software I'm using, we get Q and
  5702. 4:08:14not Q because in some certain cases it
  5703. 4:08:16could be useful to have the inverted
  5704. 4:08:18value. But what I really want to show is
  5705. 4:08:20that these two are equivalent. So we
  5706. 4:08:23know that the value should only update
  5707. 4:08:24when C goes from 0 to one. So if I want
  5708. 4:08:28to lock in a one, what I would need to
  5709. 4:08:30do is is put in a one on my input line.
  5710. 4:08:33And then the exact moment that C goes
  5711. 4:08:34from 0 to one, we should see the one co
  5712. 4:08:36the the value of a get locked in over
  5713. 4:08:38here. And that's what we see with this
  5714. 4:08:40circuit. We already we already saw that.
  5715. 4:08:42And to prove that these are similar or
  5716. 4:08:44actually identical, we'll do the same.
  5717. 4:08:45And we see the same behavior here.
  5718. 4:08:48And then similarly, we know that if we
  5719. 4:08:50toggle a while uh at any other time, the
  5720. 4:08:55data on the output line's not going to
  5721. 4:08:56change. So if I wanted to write a zero,
  5722. 4:08:58I would need to let the clock go back
  5723. 4:09:00down to zero and then the clock put the
  5724. 4:09:02clock from 0 to one. So I do that and
  5725. 4:09:05then this has the same exact behavior.
  5726. 4:09:08So, this this right here will be used to
  5727. 4:09:10represent a flip-flop and it'll make our
  5728. 4:09:13our analysis easier and it'll save me
  5729. 4:09:16time when I'm building these circuits
  5730. 4:09:18having to draw these selectors out every
  5731. 4:09:20single time. So, you can kind of see
  5732. 4:09:22this already makes it easier than
  5733. 4:09:24drawing this and these are already
  5734. 4:09:26easier than drawing all the logic gates.
  5735. 4:09:28So, it's really nice to have these
  5736. 4:09:29layers of abstraction.
  5737. 4:09:31So now what I'd like to do is we talked
  5738. 4:09:34about we wanted to save the value that
  5739. 4:09:36our adder can have. And the issue is I I
  5740. 4:09:40drew my adder on on my uh iPad, but we
  5741. 4:09:44don't have an adder built here. So I'm
  5742. 4:09:46going to stop this and we're actually
  5743. 4:09:49going to build an adder. Uh we're going
  5744. 4:09:51to build exactly what we had before and
  5745. 4:09:53then proceed from there. And if I come
  5746. 4:09:57down to this arithmetic section, you can
  5747. 4:10:00see that there is a circuit called an
  5748. 4:10:01adder. And this should look very
  5749. 4:10:03familiar. You see there's an A and a B.
  5750. 4:10:06And then the CI is the carry in we're
  5751. 4:10:08very familiar with. And then the output
  5752. 4:10:09we get is a S for the sum bit and a CO
  5753. 4:10:12for the carry out bit. And what I'm
  5754. 4:10:16going to go ahead and do is I'm going to
  5755. 4:10:17pause the video and then build our full
  5756. 4:10:19arithmetic logic unit and then we will
  5757. 4:10:22we will come back and and go from there.
  5758. 4:10:24Okay, so I've cut back and I know this
  5759. 4:10:27looks like a lot, but this is exactly
  5760. 4:10:30what we had earlier. We have our series
  5761. 4:10:32of A inputs and they get wired into each
  5762. 4:10:35of the A inputs of the full adders. We
  5763. 4:10:38have eight A's, eight B's, eight full
  5764. 4:10:41adders and eight outputs. We have all of
  5765. 4:10:44the B's and we have them exclusive or
  5766. 4:10:47with our add subtract flag. And then we
  5767. 4:10:50also have that add subtract flag going
  5768. 4:10:52into the first carry-in of the rightmost
  5769. 4:10:55full ladder.
  5770. 4:10:57And before we proceed to add um our
  5771. 4:11:01flip-flops to this, let's let's make
  5772. 4:11:04sure this works. So this is two and this
  5773. 4:11:07is two. And we expect that 2 + 2 is four
  5774. 4:11:10and this is four in binary. And if we
  5775. 4:11:14just have 0 over here plus two, we just
  5776. 4:11:16get two. That makes sense. or 0 + 1. And
  5777. 4:11:20if we had something like 3 + 1, we would
  5778. 4:11:25expect 1 2 4. And we can verify that
  5779. 4:11:28subtraction still works. Let's suppose
  5780. 4:11:30we had four and we wanted to get rid of
  5781. 4:11:32one. So, we toggle our subtraction. And
  5782. 4:11:35then this is 1 + 2, which is three. I
  5783. 4:11:38know I went fast with the binary
  5784. 4:11:39conversions there. I've done it enough
  5785. 4:11:40where it's almost second nature. But if
  5786. 4:11:43you wanted to pause and go through the
  5787. 4:11:46sections I showed slower, you would be
  5788. 4:11:48able to verify that this is all true.
  5789. 4:11:50And now that we've done that, we have
  5790. 4:11:53the issue as we can see and we talked
  5791. 4:11:56about where changing the output changing
  5792. 4:11:59the input immediately changes the
  5793. 4:12:00output. And
  5794. 4:12:03this is problematic because we would
  5795. 4:12:04like to capture the output of our adder
  5796. 4:12:07subtractor, our ALU, arithmetic logic
  5797. 4:12:09unit, which I'll call the ALU here
  5798. 4:12:11onwards. as if I haven't already done
  5799. 4:12:13that in the video earlier, but we just
  5800. 4:12:16learned that we have this flip-flop that
  5801. 4:12:19we can use that can let us accomplish
  5802. 4:12:20exactly that. So, I'm going to stop this
  5803. 4:12:22simulation and
  5804. 4:12:25going to zoom out a little bit here
  5805. 4:12:29and let's use the flip-flop that was
  5806. 4:12:32already provided to us. And I'm not
  5807. 4:12:35going to skip the video this time
  5808. 4:12:36because I think there is some true
  5809. 4:12:38benefit to to seeing this.
  5810. 4:12:43And I don't want to just keep confusing
  5811. 4:12:45everyone by by skipping back and forth.
  5812. 4:12:48Wow, this is not my nicest performance.
  5813. 4:12:50Oh, maybe this is exactly what I did
  5814. 4:12:52want.
  5815. 4:12:54Yes, this is what I want. Okay, so what
  5816. 4:12:57we going to have is we know that each of
  5817. 4:13:00our flip-flops can store one bit. So,
  5818. 4:13:04just as we had each of our full adders
  5819. 4:13:06could add one bit, we're just going to
  5820. 4:13:07have and our solution to add eight bits
  5821. 4:13:10was to have eight full adders. If we
  5822. 4:13:13have a flip-flop that can store one bit,
  5823. 4:13:15our solution to storing eight bits is to
  5824. 4:13:17have eight of these. And we're all going
  5825. 4:13:20to feed them the same
  5826. 4:13:22uh clock input line because we have some
  5827. 4:13:26value on our output from our ALOU and we
  5828. 4:13:29want to trap each of those bits at the
  5829. 4:13:31same exact time because collectively
  5830. 4:13:34each of those bits is just a zero and a
  5831. 4:13:36one. But if we look at all of them eight
  5832. 4:13:38in a row, we know we can actually
  5833. 4:13:39interpret a human readable number from
  5834. 4:13:42this. So I'm going to create a new input
  5835. 4:13:45that is going to be our clock.
  5836. 4:13:49And that's going to get fed into every
  5837. 4:13:51single one of these. I'm actually going
  5838. 4:13:52to bring this down.
  5839. 4:13:55And we're going to have eight of these.
  5840. 4:13:58So
  5841. 4:14:00I will
  5842. 4:14:04have eight.
  5843. 4:14:09And let me bring this up a little bit. I
  5844. 4:14:13think we may need to bring this down
  5845. 4:14:14later. And then we're going to get rid
  5846. 4:14:16of our outputs here because they're
  5847. 4:14:17actually no longer outputs. We will be
  5848. 4:14:19needing them in feeding in each of these
  5849. 4:14:24logic gates. So I'm going to delete
  5850. 4:14:27this. And let's go ahead and wire in
  5851. 4:14:31each of the data the D lines for our
  5852. 4:14:35adder.
  5853. 4:14:37So this is easy based on the
  5854. 4:14:39construction I have. We can just bring
  5855. 4:14:41these all the way down.
  5856. 4:14:49And I admittedly did not need to bring
  5857. 4:14:51it so far down. I thought it'd be a
  5858. 4:14:53little more complicated. Um, but now we
  5859. 4:14:56can feed this clock in to each of these.
  5860. 4:15:02And on this software, you'll notice that
  5861. 4:15:06when you have two wires overlapping each
  5862. 4:15:08other, it doesn't mean they're the same
  5863. 4:15:10wire. They can be distinct. They are the
  5864. 4:15:13same wire if you have this dot. So you
  5865. 4:15:15can see right here this wire is
  5866. 4:15:17physically connected to this wire but
  5867. 4:15:18there's no dot at this intersection. So
  5868. 4:15:20they're actually two different wires.
  5869. 4:15:23Okay. And then we have that. So let me
  5870. 4:15:26then come down here and let's take our
  5871. 4:15:29outputs of these and use them
  5872. 4:15:32accordingly.
  5873. 4:15:34So let's see if it's 90. Nope. 270.
  5874. 4:15:39And I will have eight of these in a row.
  5875. 4:15:59>> Okay. And then we should be able to And
  5876. 4:16:03we want to look at the Q, not the
  5877. 4:16:06inverted Q.
  5878. 4:16:12Because as I mentioned earlier, we
  5879. 4:16:14actually don't care about the inverted
  5880. 4:16:16queue for
  5881. 4:16:18the computer we're building.
  5882. 4:16:26Okay. So now I'm going to run this
  5883. 4:16:27circuit and let's let's see how we can
  5884. 4:16:29actually use this. So let's say I wanted
  5885. 4:16:33to add the numbers 1 + 1 and we would
  5886. 4:16:36expect two. So I'm going to put in a one
  5887. 4:16:39and a one. And our input our add
  5888. 4:16:42subtract line is zero. So we're going to
  5889. 4:16:44be adding and you can kind of see by
  5890. 4:16:47looking at these lines up here that we
  5891. 4:16:48have we have a one or a two as our
  5892. 4:16:51result. Sorry. But we want to capture
  5893. 4:16:54that result because the moment I start
  5894. 4:16:56changing this input, you can see the
  5895. 4:16:58output starts changing and that's not
  5896. 4:17:00good. We want to be able to save what we
  5897. 4:17:01have. So we know then from here that the
  5898. 4:17:06whatever the input lines are, they only
  5899. 4:17:08update to the output lines at the exact
  5900. 4:17:10moment this clock goes from 0 to one. So
  5901. 4:17:12if I set this from 0 to one, we should
  5902. 4:17:14expect to see this bit be set cuz that
  5903. 4:17:17represents two. And that's exactly what
  5904. 4:17:19we see happen. And now if I keep
  5905. 4:17:22changing this input, you can see that
  5906. 4:17:23the outputs of this of the ALU are
  5907. 4:17:25changing, but they're not going to get
  5908. 4:17:27trapped as the stored value. The stored
  5909. 4:17:29value is not going to update until the
  5910. 4:17:31next time the clock goes from zero to
  5911. 4:17:32one. So I'll keep changing this. Nothing
  5912. 4:17:34happens. Set the clock to low. I can see
  5913. 4:17:37even changing things has no input. And
  5914. 4:17:39then now suppose I wanted to latch a new
  5915. 4:17:41value in. Suppose I wanted to do uh 4
  5916. 4:17:44minus one. And I would get the output of
  5917. 4:17:50three.
  5918. 4:17:52And
  5919. 4:17:54if we do that and propagate this
  5920. 4:17:56through, then we Oh, it looks like I Oh,
  5921. 4:18:02I messed up something. I did four and
  5922. 4:18:04one, but I had the add line. So, that's
  5923. 4:18:07fine. I will fix this. Let me set the
  5924. 4:18:09subtract line. And then the moment the
  5925. 4:18:11clock goes from low to high, now we can
  5926. 4:18:13see this this three gets latched in
  5927. 4:18:15here. And changing the inputs has no
  5928. 4:18:17effect. And so this is very very useful
  5929. 4:18:21because we're now actually able to store
  5930. 4:18:23values. And that's that's something that
  5931. 4:18:25we'd like to do in our computer. Our
  5932. 4:18:27computer actually needs to store values.
  5933. 4:18:29We have this effectively calculator up
  5934. 4:18:31here, but if we can't save anything, it
  5935. 4:18:33it's not very useful. And what we've
  5936. 4:18:36actually built here is not just eight
  5937. 4:18:38flip-flops. This entire thing is what we
  5938. 4:18:41call a register. So a register is a
  5939. 4:18:46abstraction over a flip-flop. It's
  5940. 4:18:48multiple flip-flops together, but we
  5941. 4:18:50don't we don't perceive them as
  5942. 4:18:52individual flip-flops. We looked at them
  5943. 4:18:53we look at them in aggregate. And each
  5944. 4:18:55of these flip-flops get in their own
  5945. 4:18:58inputs, but they all have the same clock
  5946. 4:18:59line and then their outputs are all read
  5947. 4:19:02together. And if you've done a little
  5948. 4:19:05bit of computer programming before,
  5949. 4:19:07specifically in the language C, you may
  5950. 4:19:09or may not be familiar with the keyword
  5951. 4:19:12register. And if you're defining a a
  5952. 4:19:14variable in a programming language,
  5953. 4:19:16which if you have no idea what I'm
  5954. 4:19:17talking about, don't worry about it. If
  5955. 4:19:20you define a variable in a programming
  5956. 4:19:21language C and you mark it with the
  5957. 4:19:23keyword register, then it instructs the
  5958. 4:19:26compiler to try store that value in a
  5959. 4:19:29register instead of in your computer's
  5960. 4:19:31RAM, which is what we'll come up to
  5961. 4:19:33next. And this is because these
  5962. 4:19:35registers are very very fast to read and
  5963. 4:19:37write from. You can see they they
  5964. 4:19:40operate as soon as the clock goes from 0
  5965. 4:19:43to one and then their values are
  5966. 4:19:45immediately persistable. What we're
  5967. 4:19:47actually going to do next in this video
  5968. 4:19:48is build up RAM for our computer so we
  5969. 4:19:50can save values that we add into
  5970. 4:19:52different spots in memory. And you'll
  5971. 4:19:54see when we actually go to use it that
  5972. 4:19:56it takes more time to interact with
  5973. 4:19:59memory from RAM. And that's because you
  5974. 4:20:01would have to figure out where in RAM
  5975. 4:20:03you want to go and then pull that value
  5976. 4:20:05back out and then put it into a
  5977. 4:20:07register. [snorts] So there's a few more
  5978. 4:20:09steps involved. But before we build RAM,
  5979. 4:20:12I'd like to show what would we could do
  5980. 4:20:14by taking the output of our register and
  5981. 4:20:18then chaining it as the input to one of
  5982. 4:20:21the arithmetic logic unit sides. So I'm
  5983. 4:20:25going to stop our program here and I'm
  5984. 4:20:27going to copy this cuz I I'd like to
  5985. 4:20:29keep it. And what I'm going to do is
  5986. 4:20:32destroy these and destroy
  5987. 4:20:38I just want to destroy. That's not
  5988. 4:20:40possible. Okay. But I'm going to take
  5989. 4:20:44our outputs, our register value, and
  5990. 4:20:46make it the input to the ALU. And what
  5991. 4:20:49that's going to do is that the it means
  5992. 4:20:51we're always going to be adding or
  5993. 4:20:53subtracting the current value that's
  5994. 4:20:56stored in the register with whatever our
  5995. 4:20:58input is. So now we only have one input
  5996. 4:21:00value and we can add or subtract
  5997. 4:21:02whatever value in the register is. We'd
  5998. 4:21:04pulse the clock and then it update the
  5999. 4:21:06register with the result and we could do
  6000. 4:21:07another addition on that. So let's build
  6001. 4:21:10this and then we'll see what I actually
  6002. 4:21:11mean here.
  6003. 4:21:24Okay. So now something interesting
  6004. 4:21:26happens if I if I start this circuit.
  6005. 4:21:29And it's it's weird. We only have we
  6006. 4:21:31have one we have one one input here. And
  6007. 4:21:36I've I've done a poor job of designing
  6008. 4:21:39this. But you can see that our our
  6009. 4:21:41register is also outputting a zero right
  6010. 4:21:44now.
  6011. 4:21:45And what I can do though is let's say I
  6012. 4:21:49add one here. Then we're we have since a
  6013. 4:21:53register is storing zero that means our
  6014. 4:21:55our input a side is zero and then we're
  6015. 4:21:58adding a one and so we can see here the
  6016. 4:22:00result is a one and if I want to pulse
  6017. 4:22:03that in clock then we can trap that
  6018. 4:22:06value in the register so I do that and
  6019. 4:22:09now you can see that the register is
  6020. 4:22:10outputting a one
  6021. 4:22:12and let me actually pause this one more
  6022. 4:22:15time because I think it could benefit
  6023. 4:22:18from
  6024. 4:22:20some actually labeled outputs.
  6025. 4:22:28Okay, this is a better representation.
  6026. 4:22:31But so we started off with we have a
  6027. 4:22:33zero on our output which is also our
  6028. 4:22:35input A. So now we add we want to add
  6029. 4:22:37one to that and I'll pulse the clock
  6030. 4:22:40from low to high. And now we get a one
  6031. 4:22:42as our output. And notice if I keep one
  6032. 4:22:45on this circuit, then now we have our
  6033. 4:22:47one here and we're we're keeping it as a
  6034. 4:22:50one here. So then the next time this
  6035. 4:22:52clock goes from low to high, I should
  6036. 4:22:53expect to see 1 plus 1, which is two
  6037. 4:22:55being on the output of the register. And
  6038. 4:22:57we see that happen. And now I have two
  6039. 4:22:59here and two is our first input and one
  6040. 4:23:02is our second input. So I should see
  6041. 4:23:04three, which means this and this should
  6042. 4:23:05be set the next time the clock goes from
  6043. 4:23:07low to high. That's exactly what
  6044. 4:23:09happens. And if I keep pulsing this
  6045. 4:23:12clock, we have three. and then I'll go
  6046. 4:23:14to four and then five and then six and
  6047. 4:23:18then seven and then eight and it would
  6048. 4:23:22keep on going.
  6049. 4:23:23But what we have is a circuit that lets
  6050. 4:23:27us count. And this is going to be
  6051. 4:23:30useful.
  6052. 4:23:32It's a sake of illustration for now, but
  6053. 4:23:34later in the computer we're actually
  6054. 4:23:36going to revisit this. We we are going
  6055. 4:23:40to have a part of our computer which
  6056. 4:23:44just incrementally counts and it's
  6057. 4:23:46counting up and up by one at a time to
  6058. 4:23:49know what instruction i.e. what work it
  6059. 4:23:52needs to do next. But that's not
  6060. 4:23:54something we need to worry about for
  6061. 4:23:55now. Uh I just wanted to put this in to
  6062. 4:23:58maybe give a little bit of context about
  6063. 4:23:59our our alou. And we can also count back
  6064. 4:24:02down from zero. So we have eight. If I
  6065. 4:24:04toggle the subtract line, now we have
  6066. 4:24:06eight. And every time I pulse the clock,
  6067. 4:24:08we're going to be subtracting one from
  6068. 4:24:09the value. So I go to seven and then six
  6069. 4:24:13and then five 4 3 2
  6070. 4:24:191 and then we're back at zero. And
  6071. 4:24:22interestingly enough, if I subtract one
  6072. 4:24:24again, then
  6073. 4:24:260 - 1 should be negative 1. And this is
  6074. 4:24:29actually the binary representation using
  6075. 4:24:312's complement of negative 1. So this is
  6076. 4:24:34very interesting.
  6077. 4:24:36So now what we'd like to do is
  6078. 4:24:39operate on multiple values. So we would
  6079. 4:24:43like ideally to be able to perform large
  6080. 4:24:46and more complex expressions. And what I
  6081. 4:24:49mean by that is right now we can either
  6082. 4:24:51do a + b or a minus b. And I'm not
  6083. 4:24:55suggesting that we're going to do
  6084. 4:24:56multiplication or division because I
  6085. 4:24:57said that's probably out of the scope
  6086. 4:24:58for our video because of YouTube's
  6087. 4:25:00upload limit. But what we would like to
  6088. 4:25:03do is a + b minus c plus d. Something
  6089. 4:25:07that involves more variables than just
  6090. 4:25:09two. And the only way we can do this
  6091. 4:25:12with our alou is if we had a certain way
  6092. 4:25:15to store memory beyond one register
  6093. 4:25:18because we're always going to only have
  6094. 4:25:20two inputs to our alou. And what we'd
  6095. 4:25:22like to do is just be able to control
  6096. 4:25:23what those inputs are based on looking
  6097. 4:25:26up values in memory. And to bridge this
  6098. 4:25:29gap, we're going to build what we call
  6099. 4:25:30RAM. And you are probably familiar with
  6100. 4:25:33RAM independent of how well you know
  6101. 4:25:34computers. And RAM stands for random
  6102. 4:25:37access memory.
  6103. 4:25:39And so the way RAM works is that we will
  6104. 4:25:44be able to give it an address and get
  6105. 4:25:46back a value. And we'll either be able
  6106. 4:25:49to give an address and get back a value.
  6107. 4:25:52And that's when we want to read from our
  6108. 4:25:53RAM or we give it an address and a new
  6109. 4:25:56value. and the the RAM unit will update
  6110. 4:26:00whatever is in memory at that address.
  6111. 4:26:03And what I mean by address is we
  6112. 4:26:05effectively have a table of values. So
  6113. 4:26:08what I'm going to do is switch back to
  6114. 4:26:10my drawing screen for the next segment
  6115. 4:26:12of this video so we can make some sense
  6116. 4:26:13of this and then we'll come back here
  6117. 4:26:15and see if we can build it out in
  6118. 4:26:16hardware.
  6119. 4:26:18Okay, so we're back to the iPad. Let's
  6120. 4:26:20recap where we were. So, we had
  6121. 4:26:23um this ALU, arithmetic logic unit, and
  6122. 4:26:27you'll often see it denoted in this
  6123. 4:26:29V-shaped form, and it kind of means
  6124. 4:26:31input one, input two, and then your
  6125. 4:26:36output. And I know we're ignoring some
  6126. 4:26:37stuff like the add subtract flag, but
  6127. 4:26:40that's just for sake of demonstration.
  6128. 4:26:42And
  6129. 4:26:43we had a our output of our ALOU coming
  6130. 4:26:47back around into a register
  6131. 4:26:55and then that would actually feed into
  6132. 4:26:56one of the outputs or back into one of
  6133. 4:26:58the inputs and then this other input was
  6134. 4:27:00controlled by us. And what this let us
  6135. 4:27:04do and there's also this like clock
  6136. 4:27:05signal or latch signal for the register
  6137. 4:27:07that I'm also ignoring just for the sake
  6138. 4:27:09of visualizing. What this let us do is
  6139. 4:27:12keep operating on one value that was
  6140. 4:27:15persistently being stored in this
  6141. 4:27:17register. So the output of the alou we'd
  6142. 4:27:21freeze it and then we could do something
  6143. 4:27:23um that we call that add any term our
  6144. 4:27:25result and then we could say if I want
  6145. 4:27:27to add one I could throw in a one here
  6146. 4:27:29or if I wanted to subtract two I could
  6147. 4:27:31throw in a negative -2 here and then I'd
  6148. 4:27:33let that propagate through and capture
  6149. 4:27:36the output in the new register. And the
  6150. 4:27:39issue with this that we're seeing is
  6151. 4:27:40well there's no issue with it but it's
  6152. 4:27:42kind of simple and that we'd like to be
  6153. 4:27:44able to do more. So instead of having
  6154. 4:27:46one value um that we can save, what if
  6155. 4:27:48we had like a whole ton of values over
  6156. 4:27:50here and we had like several values like
  6157. 4:27:54value one and two and three and keeps
  6158. 4:27:58going and then at any point we could say
  6159. 4:28:00hey I want to take uh whatever is at
  6160. 4:28:02like this value three and feed it into
  6161. 4:28:05our AOU do some work and then maybe take
  6162. 4:28:08the output and then uh have that output
  6163. 4:28:10go back and we write it into say like
  6164. 4:28:12address one or something. Um, and this
  6165. 4:28:15is kind of abstract. So, what we're
  6166. 4:28:17going to do is figure out like what this
  6167. 4:28:19actually means. So, for now, what I'm
  6168. 4:28:21actually gonna say is let's throw away
  6169. 4:28:23all of our thinking of the AOU and just
  6170. 4:28:26focus on this thing over here because
  6171. 4:28:29what we want is a way to be able to
  6172. 4:28:31store data. And I talked about this just
  6173. 4:28:33a few minutes ago, but um, yeah, there's
  6174. 4:28:37going to be some replication here, but
  6175. 4:28:38let's try to figure out what this this
  6176. 4:28:40part actually means here. And
  6177. 4:28:44the first thing you'll see is we're
  6178. 4:28:46talking about storing values. And your
  6179. 4:28:48mind's probably jumping, hopefully it's
  6180. 4:28:50jumping straight to registers. Like a
  6181. 4:28:52like if we want to store a bunch of
  6182. 4:28:53values, let's just have a bunch of
  6183. 4:28:54registers. And that would be the correct
  6184. 4:28:56intuition. So let's say um we want to
  6185. 4:29:01have several registers storing values.
  6186. 4:29:04But before we even get there, let's just
  6187. 4:29:06figure out like what this might look
  6188. 4:29:07like on a table. So we we have several
  6189. 4:29:10values that are all unique and so we
  6190. 4:29:12want like a table to represent like what
  6191. 4:29:15the state of this memory unit looks like
  6192. 4:29:17at any given time. And so we can have
  6193. 4:29:20this like this list of entries. So like
  6194. 4:29:22maybe the first entry has like value 0 0
  6195. 4:29:260 and the second entry has value 0 1 1 0
  6196. 4:29:30and the third entry has values 1 0 1 0
  6197. 4:29:33and the fourth value maybe it's uh 1 0 0
  6198. 4:29:360 and so on. These are just random
  6199. 4:29:39values and it actually turns out not to
  6200. 4:29:41get ahead of myself when you plug in
  6201. 4:29:43your computer for the first time and
  6202. 4:29:44start it up the RAM takes on any number
  6203. 4:29:47of values. the actual values specified
  6204. 4:29:49in memory are kind of just subjective.
  6205. 4:29:51Um, we usually reason about them
  6206. 4:29:53starting off as all zeros when you first
  6207. 4:29:55plug in a computer. And I don't want to
  6208. 4:29:56get too lost in specifics, but that is
  6209. 4:29:59an interesting thing. Um, if you like
  6210. 4:30:01plug in a RAM unit and then try inspect
  6211. 4:30:04the values right away, you'll see like
  6212. 4:30:06sometimes they're all over the place.
  6213. 4:30:08Um, and so let's say in this
  6214. 4:30:10hypothetical we have three, five
  6215. 4:30:13different values here. Um, we need a way
  6216. 4:30:15to assign each of these values some
  6217. 4:30:17unique identifier. Um, when I'm when I
  6218. 4:30:20want to read like this value right here,
  6219. 4:30:23that's a bad arrow. It's kind of Let's
  6220. 4:30:25say I wanted to read this value right
  6221. 4:30:26here. I need a way to tell the RAM unit,
  6222. 4:30:29hey, I want that that second value in
  6223. 4:30:31that list. And I either want to read
  6224. 4:30:33read what that value is or write
  6225. 4:30:35something new into that position. Um,
  6226. 4:30:38and so what what is that like? We how do
  6227. 4:30:41we actually represent that? And that is
  6228. 4:30:43what we call an address which is pretty
  6229. 4:30:45straightforward if we like how we can
  6230. 4:30:48reason about the addresses is just what
  6231. 4:30:49position we are in our list of numbers.
  6232. 4:30:52So whereas this this right hand side has
  6233. 4:30:55the value at a particular address the
  6234. 4:30:58left hand side specifies what the
  6235. 4:31:00address actually is. And we'll start
  6236. 4:31:02counting from address zero. And so we
  6237. 4:31:04can say this first value has address
  6238. 4:31:06zero. This second value has address one
  6239. 4:31:09and so on. And this can keep going. Um,
  6240. 4:31:13and so now it's easy for us to ask the
  6241. 4:31:16RAM, hey, give me uh RAM, give me what's
  6242. 4:31:19at this address 2, and it would
  6243. 4:31:22correctly return one 0 1 0. Or I could
  6244. 4:31:25say, hey, write some value into address
  6245. 4:31:28one. And that value is 0000 0. And uh
  6246. 4:31:32the RAM after that operation would write
  6247. 4:31:34that correct value into this address.
  6248. 4:31:43So um
  6249. 4:31:46that's how we represent things. RAM is a
  6250. 4:31:48series of addresses and values. And you
  6251. 4:31:51can kind of notice that there's there's
  6252. 4:31:52two things that vary here. Um the first
  6253. 4:31:55and kind of obvious one is is hinted by
  6254. 4:31:57here and I say a dot dot and how many
  6255. 4:32:00values do we actually store in RAM. So I
  6256. 4:32:02guess our our tunables to say our
  6257. 4:32:05parameters when we're designing our RAM
  6258. 4:32:07system is number one
  6259. 4:32:09uh how many addresses
  6260. 4:32:16and the second thing is for any given
  6261. 4:32:18address and this might not be apparent
  6262. 4:32:20but it might be more subtle. Uh right
  6263. 4:32:22now I just said each address has four
  6264. 4:32:24bits like that's how at each address the
  6265. 4:32:27value that we store there is four bits.
  6266. 4:32:30um that's arbitrary. I just did that
  6267. 4:32:32because that's easy to draw out. But uh
  6268. 4:32:34what is the actual like width of the
  6269. 4:32:36values at the address? So um bits of
  6270. 4:32:40values. So uh for a given address,
  6271. 4:32:48how many bits are we storing there?
  6272. 4:32:57So for modern computers, this ends up
  6273. 4:32:59being uh larger numbers um millions and
  6274. 4:33:04millions into billions of addresses and
  6275. 4:33:06each value is is a 64-bit address or
  6276. 4:33:09even some computers it can be even
  6277. 4:33:11higher 128 bits. Um I'm actually going
  6278. 4:33:13to get to the point where we we build up
  6279. 4:33:15a fully functioning RAM in the same
  6280. 4:33:17software I just came from. So I'm going
  6281. 4:33:19to keep the value small here because you
  6282. 4:33:22can learn the concept without getting
  6283. 4:33:23lost in the uh specifics of trying to
  6284. 4:33:26make it one to one with the computer
  6285. 4:33:27we're going to build. So I'm going to
  6286. 4:33:28build a simple version of RAM that says
  6287. 4:33:32uh let's say it's going to put the
  6288. 4:33:34number we'll say four different
  6289. 4:33:36addresses and then for also each address
  6290. 4:33:38will store a four bit value. Um these
  6291. 4:33:40two numbers do not have to be the same
  6292. 4:33:42and I'm just doing that because it'll
  6293. 4:33:43make our construction simpler here. So
  6294. 4:33:46if I relook at this table now given this
  6295. 4:33:49um we have addresses
  6296. 4:33:560 1 2 and three and then in terms of the
  6297. 4:34:00values let's just make up some random
  6298. 4:34:02values.
  6299. 4:34:09Uh let's just say this is the state of
  6300. 4:34:11our RAM at any time. Um,
  6301. 4:34:14and now let's get into the construction.
  6302. 4:34:17So, we kind of suspected we were going
  6303. 4:34:19to have registers to hold these actual
  6304. 4:34:21values, and that would be correct.
  6305. 4:34:23That's what we're going to get to. Um,
  6306. 4:34:25so let's say we have a register for the
  6307. 4:34:28first value, and then register for the
  6308. 4:34:32second value
  6309. 4:34:35and a register for the third value,
  6310. 4:34:38and then finally a register for the
  6311. 4:34:40fourth value.
  6312. 4:34:42kind of made my spacing wheel too big
  6313. 4:34:44here. But um
  6314. 4:34:48let's now talk about before we actually
  6315. 4:34:49get into construction. I got a little
  6316. 4:34:50bit of my a little bit ahead of myself
  6317. 4:34:52there. Um the two operations we have to
  6318. 4:34:55RAM. So this this table diagrams the
  6319. 4:34:58internal state of RAM and how we think
  6320. 4:35:00about it. And we know that like under
  6321. 4:35:02the hood once we build this out it's all
  6322. 4:35:04going to be a mess of logic gates. But
  6323. 4:35:07this is the the the values that are
  6324. 4:35:09underlying underlying held in those
  6325. 4:35:12logic gates. Um so RAM has two
  6326. 4:35:14operations
  6327. 4:35:16and they're surprisingly
  6328. 4:35:18straightforward. Um and you could
  6329. 4:35:20probably also deduce it by now. So the
  6330. 4:35:22first one um operations.
  6331. 4:35:27The first one is read
  6332. 4:35:30and it takes in an address
  6333. 4:35:35and then your output is going to be a
  6334. 4:35:37value. So in our example, if I called
  6335. 4:35:40this read function and said I want to
  6336. 4:35:43read what's at address 2, then the RAM,
  6337. 4:35:46if you told it that, it would give you,
  6338. 4:35:48if it's built correctly, it should give
  6339. 4:35:50you um 0000.
  6340. 4:35:54So that's the first half of RAM, being
  6341. 4:35:56able to read the values that exist. And
  6342. 4:35:58then if we could only read values, that
  6343. 4:36:00would be cool. But uh part of the thing
  6344. 4:36:02we wanted to motivate in our previous
  6345. 4:36:03example was being able to take the
  6346. 4:36:05output from our arithmetic logic and
  6347. 4:36:06possibly write it back into one of these
  6348. 4:36:08values. And so you could probably guess
  6349. 4:36:11the second thing we want to do, the
  6350. 4:36:12second operation we'd want to expose is
  6351. 4:36:14called write.
  6352. 4:36:16Um and write takes in a value that you
  6353. 4:36:19want to write into RAM. and then the
  6354. 4:36:21address
  6355. 4:36:23that you want to write that that value
  6356. 4:36:25into. And there's there's really no
  6357. 4:36:27output here. Um it just kind of it
  6358. 4:36:30happens. You don't get anything from RAM
  6359. 4:36:31and return really. Um so in our example
  6360. 4:36:35if I called this write function and I
  6361. 4:36:38said hey write the value uh one one one
  6362. 4:36:42into address let's say address two then
  6363. 4:36:45what we'd expect to happen is that in
  6364. 4:36:48this address we'd end up with one one
  6365. 4:36:50one and then if I came back and called
  6366. 4:36:52the same read operation we did earlier I
  6367. 4:36:55should expect not to get 0000 0 I'd
  6368. 4:36:58expect to get 11 one1 because that's the
  6369. 4:37:00new value we just persisted in
  6370. 4:37:02So these are the two basic operations of
  6371. 4:37:04RAM. But it turns out that's also the
  6372. 4:37:06complete operations of RAM. There is
  6373. 4:37:08truly nothing more to it than the
  6374. 4:37:09ability to read from a certain address
  6375. 4:37:11or write a particular value into a
  6376. 4:37:13particular address. And I'm going to
  6377. 4:37:17clear this out.
  6378. 4:37:20We are going to start with the read
  6379. 4:37:23because the read and the right. Well,
  6380. 4:37:25the write's a little more complicated
  6381. 4:37:26than the read, but you kind of if you
  6382. 4:37:28understand how reading works, writing is
  6383. 4:37:31not too much more complicated. Um, so
  6384. 4:37:34we're going to start with the read. Uh,
  6385. 4:37:36and now with that proper context, I'll
  6386. 4:37:39jump back to these four registers here.
  6387. 4:37:41So, let's say this is our RAM.
  6388. 4:37:45I'm just drawing around here. Um let's
  6389. 4:37:48say for the read operation we need to
  6390. 4:37:50specify an input which is our address
  6391. 4:37:53and that would go into this RAM and out
  6392. 4:37:55of RAM we'd expect to get some 4bit
  6393. 4:37:57value back. Um and now here's where
  6394. 4:38:00things get a little tricky. How do we
  6395. 4:38:02actually do this? So we understand how
  6396. 4:38:04these registers work and we understand
  6397. 4:38:05that they hold values. So let me do a
  6398. 4:38:09little bit of erasing.
  6399. 4:38:13move these over.
  6400. 4:38:16And let me also show the values that are
  6401. 4:38:19in each of these registers.
  6402. 4:38:44These are all just fictitious values.
  6403. 4:38:47Okay. So the first thing is how do we uh
  6404. 4:38:52capture this input. So these addresses 0
  6405. 4:38:541 and 2 and three are just they're
  6406. 4:38:56they're base 10 values. Um so I can
  6407. 4:38:59denote that here to keep it clear. Base
  6408. 4:39:0210 recall is just how we reason about
  6409. 4:39:03numbers as humans. But we also learned
  6410. 4:39:06uh several times now that computers
  6411. 4:39:07don't really understand these values.
  6412. 4:39:09computers especially the one we're
  6413. 4:39:10building think in base two. So our
  6414. 4:39:13addresses are actually have to come in
  6415. 4:39:15as base two values. So let's let's for
  6416. 4:39:18the sake of uh understanding here um
  6417. 4:39:20let's go ahead and make our add a new
  6418. 4:39:23column to our table which just shows the
  6419. 4:39:25address in binary. So address and then
  6420. 4:39:28in base 2 and then this address that
  6421. 4:39:30we've been reasoning about is in base
  6422. 4:39:3110. And so we have four different values
  6423. 4:39:34and uh I'll just spill out the answer
  6424. 4:39:36because it's not too complicated.
  6425. 4:39:38um we're just counting in binary here.
  6426. 4:39:41So these are the binary values and you
  6427. 4:39:45can see to encode these four different
  6428. 4:39:46values we need two bits. So when we're
  6429. 4:39:50thinking about address into our RAM unit
  6430. 4:39:52we're looking for two binary inputs. So
  6431. 4:39:56that will actually be the representation
  6432. 4:39:58of our input here. So we'd expect uh on
  6433. 4:40:01our input
  6434. 4:40:04um
  6435. 4:40:06some value that is two bits and
  6436. 4:40:11I'm going to bring that up here and
  6437. 4:40:12you'll see why shortly. And um this will
  6438. 4:40:17be human for now. Eventually we'll
  6439. 4:40:19automate this entire thing when we
  6440. 4:40:20finish the construction of our computer.
  6441. 4:40:22These will be values that we can
  6442. 4:40:23specify. So I can write 01 and uh I' I'd
  6443. 4:40:28hope to get uh this
  6444. 4:40:33second register. So this one right here.
  6445. 4:40:35Um I'd want that output to flow through.
  6446. 4:40:39So
  6447. 4:40:40um
  6448. 4:40:42that's how we represent our input. And
  6449. 4:40:44then we know our output. Since we're
  6450. 4:40:45storing four bit values, the output,
  6451. 4:40:51we'd expect to just be four bits.
  6452. 4:40:58And so if the user specifies 01 as their
  6453. 4:41:01input, um we'd want the values in this
  6454. 4:41:04register to flow into this into our
  6455. 4:41:08output representation. And then if I
  6456. 4:41:10switch that input, let's say to 0 0,
  6457. 4:41:14we'd immediately want the values that
  6458. 4:41:16are in this register to flow out to
  6459. 4:41:18here. So let's figure out this first
  6460. 4:41:22arrow that I've been drawing over here
  6461. 4:41:25based on whatever the input is. How do
  6462. 4:41:27we collectly correctly select uh the
  6463. 4:41:30right register that we want? And so
  6464. 4:41:32we're going to learn uh a new type of
  6465. 4:41:34circuit. It's not too complicated, but
  6466. 4:41:36it has a a word to it. Um it's called a
  6467. 4:41:39decoder. So a decoder says for a given
  6468. 4:41:43input we will decode that in spec like
  6469. 4:41:46pick exactly one output. Um and so what
  6470. 4:41:51does that look like? Let's start by
  6471. 4:41:53giving each of these registers their
  6472. 4:41:55addresses up top. So we start with 0 0.
  6473. 4:41:58Here's 0 1. Here's one zero. And here's
  6474. 4:42:01one one. And that'll just help us for
  6475. 4:42:03referencing. And let's also
  6476. 4:42:10extend these wires that capture the
  6477. 4:42:13input all the way down here. This will
  6478. 4:42:16make sense shortly. Um, and recall that,
  6479. 4:42:19you know, if I if the user puts a one
  6480. 4:42:20here, then no matter where you fork
  6481. 4:42:23from, you're getting a one on this value
  6482. 4:42:25because it's a wire. So, it just carries
  6483. 4:42:26that value uh as far as it can. So,
  6484. 4:42:31um, let's start with this register 0 0.
  6485. 4:42:37I'm going to spill out the answer to the
  6486. 4:42:38first one and then we're going to figure
  6487. 4:42:40out why it works and then go from there.
  6488. 4:42:42So, what I will say here is that we want
  6489. 4:42:46some we want this register to be
  6490. 4:42:48selected when both of our inputs are
  6491. 4:42:50zeros. And so, the way I'm going to do
  6492. 4:42:52that is I'm going to invert both of the
  6493. 4:42:55signals.
  6494. 4:42:56Remember, this is the not gate.
  6495. 4:42:59And then I'm going to feed it
  6496. 4:43:02into an andgate.
  6497. 4:43:05And now we have something interesting.
  6498. 4:43:07So I I you're probably confused how I
  6499. 4:43:09got here. Let me prove to you that this
  6500. 4:43:11works first. So if both of our inputs
  6501. 4:43:14are zero, we're going to get a zero here
  6502. 4:43:15and a zero here. When we invert it, we
  6503. 4:43:18get ones because the inversion of a zero
  6504. 4:43:20is a one. And then for an ANDgate, it
  6505. 4:43:23only spits out a one if both the inputs
  6506. 4:43:25are ones, which happens in this case.
  6507. 4:43:27And we get a one here. And you can kind
  6508. 4:43:30of see that for any other combination of
  6509. 4:43:31values, the output of this this on this
  6510. 4:43:34line at the end here is going to be a
  6511. 4:43:36zero. Um, so for example, if I threw in
  6512. 4:43:39the input, if the user had specified the
  6513. 4:43:41input one zero, then what would happen
  6514. 4:43:44here is this top inverter would spit out
  6515. 4:43:46a one, this bottom inverter would spit
  6516. 4:43:48out a zero. And then one and zero. Since
  6517. 4:43:51neither since not both of the inputs are
  6518. 4:43:52ones, we'd spit out a zero here. And
  6519. 4:43:55I'll let you uh work backwards. And if
  6520. 4:43:57you want to trace through every possible
  6521. 4:43:59uh case of the four input combinations,
  6522. 4:44:02you'll see that the only time that this
  6523. 4:44:05line right here gets a one is if both of
  6524. 4:44:07the inputs are zero. Um there's another
  6525. 4:44:10way to think about this too is work
  6526. 4:44:13backwards. So we if we want a one on
  6527. 4:44:16this output line, then we can we can
  6528. 4:44:19work from right to left. So we'd say if
  6529. 4:44:21this and AND gate outputs a one it means
  6530. 4:44:23that both of the inputs must have been
  6531. 4:44:25one by the definition of the AND gate.
  6532. 4:44:27And then for this inverter up here if
  6533. 4:44:29the output is one then the input must
  6534. 4:44:32have been the opposite of one which is
  6535. 4:44:34zero. And the same would follow for our
  6536. 4:44:36our gate down here. And then you can
  6537. 4:44:38kind of see that the only time that we
  6538. 4:44:41could possibly end with a one over here
  6539. 4:44:42is if both of the inputs are zero, which
  6540. 4:44:44I think is a pretty neat way of thinking
  6541. 4:44:46about this.
  6542. 4:44:49So that would correctly let us pick out
  6543. 4:44:51this first input. And then let's try go
  6544. 4:44:54do the same for this next input. So this
  6545. 4:44:56next input, we'd only want we'd have
  6546. 4:44:58some line here. We only want this line
  6547. 4:45:01to have a one if the user has zero one
  6548. 4:45:03on their inputs here. So what I'll do is
  6549. 4:45:08we'd want something very similar to what
  6550. 4:45:10we have above, but maybe we drop some
  6551. 4:45:12inversions. So, we'd want this to be a
  6552. 4:45:16zero and this to be a one. So, what I'll
  6553. 4:45:21do is I'll invert this zero
  6554. 4:45:23and then this one would just feed
  6555. 4:45:25through and we'll just funnel it into an
  6556. 4:45:27AND gate. And we have this wire out
  6557. 4:45:30here. And so now you can kind of see
  6558. 4:45:32again that the only way this can be a
  6559. 4:45:35one is if both the inputs of this
  6560. 4:45:37ANDgate are one. And that means that for
  6561. 4:45:40this inverter up here, if the output of
  6562. 4:45:42the inverter is one, that it means the
  6563. 4:45:43input must have been zero. And for this
  6564. 4:45:45line right here, it just it just pulls
  6565. 4:45:47in from this uh this input from the
  6566. 4:45:50user. The only way that could be one is
  6567. 4:45:52if the user had specified one there. And
  6568. 4:45:55you can kind of see that's exactly what
  6569. 4:45:56we have going on here, which is pretty
  6570. 4:45:58interesting. And
  6571. 4:46:01we can then do the same for the next
  6572. 4:46:02case.
  6573. 4:46:04So, uh, now we want to do one zero. So
  6574. 4:46:08we only want this output to be a one if
  6575. 4:46:10our input looks like this. So one zero
  6576. 4:46:15means that this should be a one and this
  6577. 4:46:19right here should be a zero. So we can
  6578. 4:46:21invert this one and then feed both of
  6579. 4:46:24these into the end gate like we've been
  6580. 4:46:26doing above.
  6581. 4:46:28And you'll see that it's kind of just
  6582. 4:46:30the uh the this top one and this bottom
  6583. 4:46:33one are just reflections of each other.
  6584. 4:46:35And that kind of follows from the fact
  6585. 4:46:36that the 01 flipped is just one zero.
  6586. 4:46:40And last but not least, uh our very our
  6587. 4:46:43fourth and final register. We'd only
  6588. 4:46:45want that line to spit out a one if both
  6589. 4:46:49the inputs are one. And this one's
  6590. 4:46:51straightforward. So if if we if we want
  6591. 4:46:53both the inputs to be one, we'll just
  6592. 4:46:55and them directly.
  6593. 4:46:58So the only way this can be a one is if
  6594. 4:47:00both the inputs are one, which should
  6595. 4:47:01imply that user had selected one one.
  6596. 4:47:05So this is pretty neat. What this means
  6597. 4:47:07is that for every value we put in here,
  6598. 4:47:12we will get only exactly and only one of
  6599. 4:47:16these four output lines will be turned
  6600. 4:47:19on and that is really neat. So if I
  6601. 4:47:23specify 0 0 then this will turn on and
  6602. 4:47:28the rest of them will turn off. If I
  6603. 4:47:30specify 01, you'll see that
  6604. 4:47:34this one turns on and the rest of them
  6605. 4:47:36turn off. And that keeps going. For one,
  6606. 4:47:40zero, we'd see off, off, on, off. And
  6607. 4:47:44last but not least, if we had one, one,
  6608. 4:47:47it'd be off, off, off, on. And so we've
  6609. 4:47:51kind of built this selectoresque
  6610. 4:47:54circuit that says for no matter what the
  6611. 4:47:58input is for the input we will correctly
  6612. 4:48:01turn on only one of these output gates.
  6613. 4:48:04And that'll pretty much be our cue for
  6614. 4:48:05the register to then say hey if for each
  6615. 4:48:10register it can say at any point in time
  6616. 4:48:12if its corresponding selection line is
  6617. 4:48:15on then it wants to pass its value to
  6618. 4:48:18the output over here.
  6619. 4:48:20And this is pretty interesting, but what
  6620. 4:48:22does it actually mean uh to now pass the
  6621. 4:48:24output? That's where things get tricky.
  6622. 4:48:26So, we've dealt with the input side. For
  6623. 4:48:28the input side, uh we have this, it's
  6624. 4:48:30not connected to our registers yet, but
  6625. 4:48:32we have this circuit that lets us say,
  6626. 4:48:34uh lets each register kind of know if it
  6627. 4:48:37should be pushing its value to this
  6628. 4:48:39output line. Now, we need to figure out
  6629. 4:48:41how to look at that signal um i.e. look
  6630. 4:48:44at that line and if it is set, i.e. it
  6631. 4:48:47is one. Then how do we actually push the
  6632. 4:48:49the value in the register at that
  6633. 4:48:51current time to the output lines?
  6634. 4:48:58And so
  6635. 4:49:00let's start with our bottom register. It
  6636. 4:49:02turns out
  6637. 4:49:04that this is pretty straightforward to
  6638. 4:49:07do. Um my drawing is going to get messy
  6639. 4:49:09here, but we're quickly going to move on
  6640. 4:49:11and build this in our our digital logic
  6641. 4:49:13simulator. So, uh it shouldn't be too
  6642. 4:49:16much of a concern here. So, recall that
  6643. 4:49:19our register um has output lines
  6644. 4:49:24and how I choose to visualize this is up
  6645. 4:49:26to me, but um it has its output lines
  6646. 4:49:30and pretty much uh what I'm going to do
  6647. 4:49:34is I'm going to take this signal and and
  6648. 4:49:37it with either each of the output lines.
  6649. 4:49:40So, what I mean by that, let me draw it
  6650. 4:49:42out.
  6651. 4:50:06Now what does this mean? So what do the
  6652. 4:50:09outputs here look like? If our selection
  6653. 4:50:12line is a zero then we can guarantee
  6654. 4:50:16that all of the output lines will be
  6655. 4:50:18zero because the these are and gates and
  6656. 4:50:22we know that the nature of an andgate
  6657. 4:50:23says that if any of the inputs are zero
  6658. 4:50:25then the output for sure will be zero.
  6659. 4:50:28So this is interesting because it means
  6660. 4:50:30for this we can verify that if our if
  6661. 4:50:32our current register is not selected
  6662. 4:50:34i.e. this line is a zero then the output
  6663. 4:50:36of these four andgates down here will be
  6664. 4:50:38zero. Now, let's see what happens if we
  6665. 4:50:41actually were to flip this to a one.
  6666. 4:50:45Then we'd have ones. And I've kind of
  6667. 4:50:48alternated uh putting these on the top
  6668. 4:50:50and the bottom, which is my fault here.
  6669. 4:50:52At least each of these inputs the
  6670. 4:50:54ANDgate there would be one of the inputs
  6671. 4:50:55would be a one. And then the other input
  6672. 4:50:59is just the value from that
  6673. 4:51:00corresponding bit position in the
  6674. 4:51:02register. And so when that happens we
  6675. 4:51:04know that the output of the AND gate is
  6676. 4:51:06just going to be whatever the out the
  6677. 4:51:08the uh other input is. So for here this
  6678. 4:51:10zero will flow through here this one
  6679. 4:51:13will flow through here this zero will
  6680. 4:51:15flow through and here this one will flow
  6681. 4:51:17through. And so now we can see that when
  6682. 4:51:20the the selection line is a one then the
  6683. 4:51:23output of these registers the output of
  6684. 4:51:25these and gates is just going to be
  6685. 4:51:26whatever is in the register itself. And
  6686. 4:51:29when that uh selection line is a zero,
  6687. 4:51:31the outputs will be all zeros. So this
  6688. 4:51:34is pretty interesting
  6689. 4:51:36um because it then means we can now we
  6690. 4:51:39know that uh if we extend this to all
  6691. 4:51:41four registers
  6692. 4:51:43um we we can kind of get the uh the same
  6693. 4:51:46behavior where we have these these four
  6694. 4:51:50gates that spit out values.
  6695. 4:51:54I'm not going to reconstruct the same
  6696. 4:51:55circuit. I don't have enough space here,
  6697. 4:51:57but uh
  6698. 4:52:02I can kind of box these in together. How
  6699. 4:52:05about that? Oops. That should not
  6700. 4:52:10Okay,
  6701. 4:52:13so this is this is this the first
  6702. 4:52:15register because I kind of have them
  6703. 4:52:17close together. This is the second
  6704. 4:52:18register,
  6705. 4:52:20third register. And then we already have
  6706. 4:52:22our our fourth register down here. uh
  6707. 4:52:25that's just for distinction.
  6708. 4:52:27So if each register builds this property
  6709. 4:52:29up then we know that for at any point in
  6710. 4:52:33time um three of the four registers
  6711. 4:52:37their output will all be zero and then
  6712. 4:52:39that last register whatever one it is
  6713. 4:52:41will have the correct values uh being
  6714. 4:52:44pushed through i.e. whatever is stored
  6715. 4:52:45in that specific register and so then we
  6716. 4:52:48kind of want to join these all together
  6717. 4:52:50to the actual output. So let's look at
  6718. 4:52:53the uh the rightmost bit only for each
  6719. 4:52:57register.
  6720. 4:52:59So we'd want
  6721. 4:53:01that rightmost bit from here to flow to
  6722. 4:53:03this one. Um and the same for the rest
  6723. 4:53:06of them.
  6724. 4:53:08And recall what I just said a second ago
  6725. 4:53:11where for any particular address that
  6726. 4:53:14the user specifies on that input line,
  6727. 4:53:17three of those four lines, we don't know
  6728. 4:53:19which one. It depends on whatever the
  6729. 4:53:21selected register is. Three of those
  6730. 4:53:23four lines are guaranteed to be zeros.
  6731. 4:53:26Um, and that's because only uh only one
  6732. 4:53:30of the selection lines over here will be
  6733. 4:53:33true at any time. And so that means for
  6734. 4:53:35the remaining three that their gates are
  6735. 4:53:37just going to get blocked as we just
  6736. 4:53:39learned a few minutes ago. And so then
  6737. 4:53:41it means that the last one it either
  6738. 4:53:43outputs a zero or one. And that's the
  6739. 4:53:46one that we want to flow through. So uh
  6740. 4:53:48how we simplify that we can't just have
  6741. 4:53:50four of these lines going into
  6742. 4:53:53um into the output line is we can ore
  6743. 4:53:56them together because when we or these
  6744. 4:53:59if zero or zero or zero the ore of the
  6745. 4:54:03three that are off is always going to be
  6746. 4:54:04zero and then if we or that with the
  6747. 4:54:07correct one that we do want to pass
  6748. 4:54:08through the value will just flow through
  6749. 4:54:11because zero or x is just going to equal
  6750. 4:54:15x. Um, so let's kind of show what that
  6751. 4:54:19looks like. So
  6752. 4:54:23for this specific one right here, um, we
  6753. 4:54:26could take a
  6754. 4:54:30orgate and feed in the rightmost bit
  6755. 4:54:33from each of our registers.
  6756. 4:54:39And that will correctly let us capture
  6757. 4:54:41prop push through the uh correct
  6758. 4:54:44rightmost bit position of all the
  6759. 4:54:46registers to the output one. And that
  6760. 4:54:48that handles the rightmost bit. Uh and
  6761. 4:54:51my drawing as I said is getting bad. And
  6762. 4:54:53we just do the same for all the other
  6763. 4:54:55bit positions. So this second from right
  6764. 4:54:58would just we'd ore all of them together
  6765. 4:55:01from all of our input registers.
  6766. 4:55:04And that kind of captures our output.
  6767. 4:55:07So, this it turns out is the
  6768. 4:55:09fundamentals of RAM and how we build
  6769. 4:55:12reading RAM. Um, I've kind of gone a
  6770. 4:55:15little bit fast here and I've like to
  6771. 4:55:17keep it everything correct and thorough.
  6772. 4:55:20And so, we're going to jump back and
  6773. 4:55:21actually build this in our logic
  6774. 4:55:23software.
  6775. 4:55:40Okay, so I built the uh very first half
  6776. 4:55:42which was the selector. So there's none
  6777. 4:55:43of the register logic, no registers or
  6778. 4:55:46output flowing through. But um this was
  6779. 4:55:49the very first part where we said uh
  6780. 4:55:50based on whatever the input is, we'd
  6781. 4:55:52expect only one of these four outputs to
  6782. 4:55:55be true. Um and we'd expect it to match
  6783. 4:55:58the input. So we can kind of see that
  6784. 4:55:59when we have 0 0 the very first one
  6785. 4:56:02turns on and uh the next one would be
  6786. 4:56:0501. So if I keep this as a zero and turn
  6787. 4:56:08this to one we'd expect only this one to
  6788. 4:56:11be on and the remaining three to be off.
  6789. 4:56:13We can see that that happens. And then
  6790. 4:56:16the next input was 1 zero. So that we'd
  6791. 4:56:20expect if I flipped this on and this
  6792. 4:56:22off, we expect this one to turn on and
  6793. 4:56:25the remaining three to turn off. So
  6794. 4:56:27that's true. And last but not least was
  6795. 4:56:301 one. So if both of our inputs are on,
  6796. 4:56:33we'd expect just this one to be on. And
  6797. 4:56:35that seems to be true. So it looks like
  6798. 4:56:37this first part is all good. So I'll go
  6799. 4:56:39ahead and continue with the building
  6800. 4:56:41there.
  6801. 4:56:56reconstructing what we had diagrammed on
  6802. 4:56:59my iPad uh in this software. In some
  6803. 4:57:02respects, it is cleaner than what I was
  6804. 4:57:04drawing on my screen and in some
  6805. 4:57:06respects it's a lot uglier. Namely, uh
  6806. 4:57:09over here this gets messy. Um so, let's
  6807. 4:57:12actually walk through and see what's
  6808. 4:57:14happening here. But before I start the
  6809. 4:57:16simulation, one thing I'd like to call
  6810. 4:57:18out is that uh instead of using actual
  6811. 4:57:20registers, i.e. making a bunch of
  6812. 4:57:23flip-flops next to each other,
  6813. 4:57:26um I'm just using inputs. And that way
  6814. 4:57:27we can easily change the value that's in
  6815. 4:57:30RAM just by clicking ourselves before we
  6816. 4:57:32actually go and um implement the uh
  6817. 4:57:35writing the writing part of our RAM, the
  6818. 4:57:37second half. So I'm going to go ahead
  6819. 4:57:39and start the simulation.
  6820. 4:57:41And this is our address selecting line.
  6821. 4:57:43So right now we can see it's 0 0 and the
  6822. 4:57:47coloring here is pretty interesting. Um
  6823. 4:57:49you know we can see some things light up
  6824. 4:57:50and it kind of corresponds to this this
  6825. 4:57:52selector part here. So first off we
  6826. 4:57:54correctly see that only uh this first
  6827. 4:57:56line gets selected and we can see these
  6828. 4:57:58and gates are all fed uh ones versus the
  6829. 4:58:02others all of theirs are fed zero which
  6830. 4:58:04means they're all outputting zero
  6831. 4:58:06independent of the value that's stored
  6832. 4:58:08you know in in each of these RAM uh in
  6833. 4:58:10each of these registers. But this first
  6834. 4:58:12one's different um correctly since it's
  6835. 4:58:14the address we're selecting is giving
  6836. 4:58:16the data in these registers the ability
  6837. 4:58:19to flow through. And so basically this
  6838. 4:58:22is our output line. And what I'd expect
  6839. 4:58:24to happen if I turn on this rightmost
  6840. 4:58:26bit position is that the output should
  6841. 4:58:28show that rightmost bit position. And uh
  6842. 4:58:31that's exactly what happens. So um how
  6843. 4:58:36do we make sense of the uh the mess
  6844. 4:58:38that's going on over here? So, we know
  6845. 4:58:40that uh these are our registers, our
  6846. 4:58:43four registers. They're kind of just uh
  6847. 4:58:44inputs right now, but they will become
  6848. 4:58:46registers not too long from now. And
  6849. 4:58:48then we also had our decoder over here.
  6850. 4:58:50Uh this decodes the input address
  6851. 4:58:53selection and we'll select uh one at a
  6852. 4:58:57time of the correct quote unquote
  6853. 4:58:59registers. And then we then join the
  6854. 4:59:03value that's in each of these registers
  6855. 4:59:05with the value of the decoding signal to
  6856. 4:59:08determine which value should flow
  6857. 4:59:10through each of our four output lines.
  6858. 4:59:13And this happens at the per register
  6859. 4:59:15level. And then what we have to do is we
  6860. 4:59:18have um an output line for each bit
  6861. 4:59:21position of each register is we actually
  6862. 4:59:23need to go join them uh to get the
  6863. 4:59:26output for our for the RAM unit as a
  6864. 4:59:28whole. So if I look at this this uh
  6865. 4:59:31let's say this rightmost bit position
  6866. 4:59:34we're seeing an orgate whose input is
  6867. 4:59:37two more the whose input is the output
  6868. 4:59:40of two other orgates. So this is just
  6869. 4:59:42kind of a four-way orgate. Um if any of
  6870. 4:59:45these four inputs are true uh the value
  6871. 4:59:49one will flow through and the output
  6872. 4:59:51overall will be a truth. The software
  6873. 4:59:53I'm using, as far as I know, does not
  6874. 4:59:55have the easy ability to do um or gates
  6875. 4:59:59that accept multiple inputs. So, this is
  6876. 5:00:01kind of how I've gone the show around,
  6877. 5:00:02but that's fine. It doesn't make our
  6878. 5:00:03logic too much worse here. Um and so, if
  6879. 5:00:07this output corresponds to the rightmost
  6880. 5:00:10bit position, what I'd expect is that
  6881. 5:00:12the rightmost bit position from each of
  6882. 5:00:14our four registers should be the four
  6883. 5:00:16inputs to this. And that's kind of what
  6884. 5:00:19we see. So if I turn this on, we can
  6885. 5:00:21kind of see, hey, this green over here
  6886. 5:00:23flows to this one. And then this line
  6887. 5:00:27right here flows to that top register,
  6888. 5:00:29the top output of the second register,
  6889. 5:00:31which hooks into its rightmost bit
  6890. 5:00:33position. Similarly, if I look at our
  6891. 5:00:35third register, here's the rightmost bit
  6892. 5:00:36position. Here's that output. If I trace
  6893. 5:00:39it up, it comes into that third ore. And
  6894. 5:00:42last but not least is our uh fourth
  6895. 5:00:46register. Here's the rightmost bit
  6896. 5:00:48position output. If I follow that line,
  6897. 5:00:51which gets a little tricky to do, I can
  6898. 5:00:53see it follows into the fourth bit
  6899. 5:00:56position. And so because of this the the
  6900. 5:00:59decoding properties combined with our
  6901. 5:01:01andgate uh little trick there, we know
  6902. 5:01:04that at any time um three of the four
  6903. 5:01:08inputs to this quad orgate is guaranteed
  6904. 5:01:11to be off. Um which means the fourth
  6905. 5:01:15value will effectively flow through. So
  6906. 5:01:18if it's a zero, the entire circuit will
  6907. 5:01:20output a zero. And if it's a one coming
  6908. 5:01:22in here, that value will just flow
  6909. 5:01:24through. Uh and this is pretty neat. So
  6910. 5:01:27now let's suppose I put in the value um
  6911. 5:01:32this value into the second register. So
  6912. 5:01:33that's what is that? That's 2 + 4.
  6913. 5:01:35That's the value six here. And if I then
  6914. 5:01:38switch the address selection part of the
  6915. 5:01:40RAM to read what's in this second
  6916. 5:01:42register, then I'd expect that value to
  6917. 5:01:44get reflected over here. So the address
  6918. 5:01:46for this register is 0 0. The address
  6919. 5:01:49for this one is 01. So to switch it, I
  6920. 5:01:51just turn the second one on. And now we
  6921. 5:01:54correctly see that the uh the output of
  6922. 5:01:56our RAM um corresponds with the register
  6923. 5:01:59denoted by our input address selection,
  6924. 5:02:02which is pretty cool. And if I then turn
  6925. 5:02:04that back off and go and select uh
  6926. 5:02:06address zero, then I get that original
  6927. 5:02:08value um correctly outputed here.
  6928. 5:02:11Similarly, if I were to select uh the
  6929. 5:02:13[snorts] third register, the register
  6930. 5:02:15whose the address in RAM uh
  6931. 5:02:18corresponding to one zero, this third
  6932. 5:02:20one down here, we can see its output is
  6933. 5:02:22all zeros because the value of that
  6934. 5:02:24register is all zeros. If I flipped it
  6935. 5:02:27one bit at a time to turn all of them
  6936. 5:02:29on, we then go ahead and you can see
  6937. 5:02:31that the output turns on. And uh just
  6938. 5:02:34for sake of completion, let's go with
  6939. 5:02:37this for our last our last register. And
  6940. 5:02:40this is address 11 one in RAM. So if I
  6941. 5:02:43select one one on our input select our
  6942. 5:02:45input address selector I get the uh
  6943. 5:02:48exact value outputed here. So this is
  6944. 5:02:52pretty cool. And we notice that even as
  6945. 5:02:54I select uh different addresses
  6946. 5:03:00that the values in RAM are held but
  6947. 5:03:03we're just only letting one of them flow
  6948. 5:03:05through at a time. So this is pretty
  6949. 5:03:07cool. We have four different 4-bit
  6950. 5:03:10values. They just exist as is. And based
  6951. 5:03:14on how we specify an address in our
  6952. 5:03:17control input, we get to read the value
  6953. 5:03:20of RAM for that given address at any
  6954. 5:03:22time on this output line.
  6955. 5:03:25It's pretty powerful. Um, the next thing
  6956. 5:03:29we'd like to do was we can now read from
  6957. 5:03:31RAM, but we'd actually like to be able
  6958. 5:03:34to write in values into RAM. So, what
  6959. 5:03:36I've been doing for this demonstration
  6960. 5:03:37is just uh toggling the values here and
  6961. 5:03:40they're just kind of input units. But we
  6962. 5:03:42really know that these uh this shouldn't
  6963. 5:03:44this shouldn't be a you know four input
  6964. 5:03:46units. It should just be a four bit
  6965. 5:03:48register which boils down to four
  6966. 5:03:50flip-flops. So, what I'm going to go
  6967. 5:03:52ahead and do is uh we what we want to do
  6968. 5:03:56next is actually replace each of the
  6969. 5:04:00inputs that I've mainly spent with uh
  6970. 5:04:03flip-flops. So, I'll actually show the
  6971. 5:04:05first one. Um, let me get rid of these.
  6972. 5:04:09And if I go over here in the tool, this
  6973. 5:04:11is less relevant. Uh, we want to D
  6974. 5:04:13flip-flop because that's what we learned
  6975. 5:04:14about previously. Go ahead and rotate
  6976. 5:04:17it.
  6977. 5:04:20Oh, wrong way.
  6978. 5:04:25And that's one bit position. We know
  6979. 5:04:28just a register is just uh a four-bit
  6980. 5:04:32register is just four
  6981. 5:04:34flip-flops next to each other. And then
  6982. 5:04:36I'll hook up the wiring. So recall that
  6983. 5:04:39uh in our digital software, we get the
  6984. 5:04:42output of the register is denoted Q and
  6985. 5:04:44we also get not Q which is the inverted
  6986. 5:04:46value. We only care about the value as
  6987. 5:04:48is. So we'll just stick with Q. And
  6988. 5:04:53cool. So now we've replaced
  6989. 5:04:56um instead of selectors, we've used
  6990. 5:04:58actual flip-flops.
  6991. 5:05:00So I'll go ahead and do that for the
  6992. 5:05:02rest of them.
  6993. 5:05:09I'm ahead and replace each of our
  6994. 5:05:10selectors with actual flip-flops. And
  6995. 5:05:13recall that when we chain flip-flops
  6996. 5:05:15together, just stack them next to each
  6997. 5:05:17other, we get um registers. So now the
  6998. 5:05:22next thing is how do we actually go
  6999. 5:05:25ahead and make it such that we can write
  7000. 5:05:26values in. So we have the reading of of
  7001. 5:05:29our RAM working. How can we actually
  7002. 5:05:30write a value into one of these
  7003. 5:05:32registers? So let's start just by saying
  7004. 5:05:35that uh these over here can be the four
  7005. 5:05:40bits. So if I were to want to uh you
  7006. 5:05:42know oops it's not going to let me run
  7007. 5:05:44this. If I were to want to uh write in a
  7008. 5:05:48new value to a particular address, then
  7009. 5:05:52what I want to do is be able to put
  7010. 5:05:53punch in that address here and then
  7011. 5:05:55punch in that value here and then have
  7012. 5:05:58that um be the signal that tells our RAM
  7013. 5:06:03unit to actually go ahead and hey like
  7014. 5:06:04do this right operation. So there's two
  7015. 5:06:07things to note here. The first one is
  7016. 5:06:09that if these are our if this is the
  7017. 5:06:11input for um reading a value in to RAM,
  7018. 5:06:15then we can hook this up to each of our
  7019. 5:06:18registers pretty much like the only way
  7020. 5:06:20to replace a value in a register is to
  7021. 5:06:22specify it on this on these four lines
  7022. 5:06:24just by our construction here. So uh the
  7023. 5:06:28same way we can loop this this right bit
  7024. 5:06:30into the the input line uh for each of
  7025. 5:06:34our right bits of our four registers
  7026. 5:06:38and I won't do that right now for the
  7027. 5:06:40sake of uh time. I do that for each bit
  7028. 5:06:42position. So that's observation number
  7029. 5:06:43one. So each of these registers whenever
  7030. 5:06:46it is going to update its value it's
  7031. 5:06:48just going to look at whatever has been
  7032. 5:06:49set by the user and right now that's
  7033. 5:06:52just going to be set by the humans.
  7034. 5:06:53Eventually we'll get to the point that
  7035. 5:06:55the computer sets the values itself
  7036. 5:06:56because you know the computer is fully
  7037. 5:06:58autonomous in that sense but that's uh
  7038. 5:07:01we're still a little bit of ways away
  7039. 5:07:02from that. And that's the first
  7040. 5:07:05observation. So I can go ahead and do
  7041. 5:07:07that wiring. The second observation is
  7042. 5:07:10uh I know we've talked about registers a
  7043. 5:07:12little bit and we're a little bit of
  7044. 5:07:13back but and we're going pretty quickly
  7045. 5:07:14here and it's that even if we hook in
  7046. 5:07:17the input here so like if I had you know
  7047. 5:07:20each of these four bit positions lined
  7048. 5:07:22up here um the only way to actually
  7049. 5:07:27get uh the value the register to accept
  7050. 5:07:30the new value is by that clock signal.
  7051. 5:07:33Um, so recall there has to be like a a
  7052. 5:07:36clock edge, a transition from low to
  7053. 5:07:38high or high to low depending on what
  7054. 5:07:40flip-flop you're using. Um, and so we're
  7055. 5:07:43going to add one more input here. And
  7056. 5:07:46that's going to be
  7057. 5:07:49uh kind of this I guess we'll call this
  7058. 5:07:50like this this trigger operation. Um, so
  7059. 5:07:54if if I wanted to write a new value into
  7060. 5:07:57a particular address, I'd punch in the
  7061. 5:07:59address here would punch in the value
  7062. 5:08:02here and then because RAM needs a uh
  7063. 5:08:05because registers need a clock edge
  7064. 5:08:07rising or falling to be triggered, I'd
  7065. 5:08:10have to then punch this. And at the
  7066. 5:08:12exact moment I punch this and it
  7067. 5:08:14triggers a transition from high to low
  7068. 5:08:16or low to high, the correct register uh
  7069. 5:08:18would hold in that value. So we said
  7070. 5:08:21that for reading in the data input for
  7071. 5:08:23each of these registers it's going to
  7072. 5:08:25come from these lines. So we can hook
  7073. 5:08:26these input lines to each of the
  7074. 5:08:28registers. But we can't hook the clock
  7075. 5:08:31line um into each of the registers. And
  7076. 5:08:34the reason is is if we did that then the
  7077. 5:08:37moment we punch this clock line the
  7078. 5:08:39values here would get written into every
  7079. 5:08:42single register. But when we punch the
  7080. 5:08:45clock line, we only actually want to
  7081. 5:08:47write the write into the register that's
  7082. 5:08:50uh being referenced by this address
  7083. 5:08:52setting line right here. So recall that
  7084. 5:08:55we built the decoder to actually solve
  7085. 5:08:57this problem already. So we can kind of
  7086. 5:08:59reuse that. We know that at the exit the
  7087. 5:09:02output of each of these andgates that we
  7088. 5:09:04have on this vertical um it'll only
  7089. 5:09:07light up if the address selected by the
  7090. 5:09:09user here is quote unquote picking this
  7091. 5:09:12register. And that was for reading, but
  7092. 5:09:14we're going to use the same logic for
  7093. 5:09:15writing. So what we'll actually do is
  7094. 5:09:18take advantage of that same principle
  7095. 5:09:19that we have with the each of these and
  7096. 5:09:20gates. And that will and the decoder
  7097. 5:09:24output with this this uh clock line and
  7098. 5:09:27feed that as the clock line into each of
  7099. 5:09:29these registers. And what that'll allow
  7100. 5:09:31us to accomplish is that when we punch
  7101. 5:09:34this clock line, it won't actually
  7102. 5:09:36replace the value in all of these
  7103. 5:09:37registers. it'll only replace the value
  7104. 5:09:40in the register whose ANDgate uh here
  7105. 5:09:44i.e. the output from the decoder
  7106. 5:09:46corresponds to the input set on these
  7107. 5:09:48lines right here. So uh I think
  7108. 5:09:51demonstrations will make this clear. Let
  7109. 5:09:53me go ahead and wire this all up.
  7110. 5:10:08gone ahead and finishing up the wiring.
  7111. 5:10:10And uh a few things are going to look
  7112. 5:10:12different now. So I've kind of moved
  7113. 5:10:14some of the stuff around from when I cut
  7114. 5:10:16the last video to make it fit. But um
  7115. 5:10:18you'll see that this is our value input
  7116. 5:10:21line. So when we want to write a new
  7117. 5:10:23value, we set the value here. This is
  7118. 5:10:25the same. when we want to read or write
  7119. 5:10:27to a register in RAM a specific address,
  7120. 5:10:31we punch in that address on these two
  7121. 5:10:33bits.
  7122. 5:10:34And uh last but not least, you'll see
  7123. 5:10:37that this is the clock signal. So uh in
  7124. 5:10:41our diagrams, we're using positive D
  7125. 5:10:43flip-flops. So that means that this
  7126. 5:10:46register will latch in a value uh on a
  7127. 5:10:50rising edge of that clock signal. So
  7128. 5:10:52whenever the C line goes from zero to
  7129. 5:10:54one, uh that's the exact moment that the
  7130. 5:10:57register will actually look at what's on
  7131. 5:10:59its input lines, these D lines, and hold
  7132. 5:11:02that and record that as the new value
  7133. 5:11:04that it persists. And so if I zoom out a
  7134. 5:11:07little bit, we can see that this is our
  7135. 5:11:09input value. So when we want to write a
  7136. 5:11:11new value, we place it here. And you can
  7137. 5:11:13kind of see that uh it is simply fed in
  7138. 5:11:17uh left to right the same way on all
  7139. 5:11:19four of our registers. So since these
  7140. 5:11:23four lines are the only way to specify a
  7141. 5:11:26new value, we can just hook each bit
  7142. 5:11:29position of the register, i.e. each bit,
  7143. 5:11:32each latch um or sorry each flip-flop
  7144. 5:11:36to the correct uh position on the line
  7145. 5:11:38here. And then we said we could not do
  7146. 5:11:41the same for the clock signal because if
  7147. 5:11:43we did that same thing for the clock
  7148. 5:11:44signal, then the moment we punched it
  7149. 5:11:47and went from a low edge to a high edge,
  7150. 5:11:50uh, all of these registers would read in
  7151. 5:11:53the new value that's on these four
  7152. 5:11:54lines, which would write to every single
  7153. 5:11:56address on our RAM when we really only
  7154. 5:11:58want to write to the address denoted by
  7155. 5:12:00the user selection here. So that's
  7156. 5:12:02enough talking. Let me go ahead and run
  7157. 5:12:04this circuit and turn it on and let's
  7158. 5:12:06see what happens. So the first thing to
  7159. 5:12:08note is that if I look at the output of
  7160. 5:12:10each of the registers,
  7161. 5:12:13then you can see they're all all zeros.
  7162. 5:12:16So that's just the way that uh registers
  7163. 5:12:18uh initialize in this software, which
  7164. 5:12:21means that if all the values in our RAM
  7165. 5:12:23are zero at every address position, then
  7166. 5:12:25no matter what address position I put in
  7167. 5:12:27here, uh the output's always going to be
  7168. 5:12:29zero. But that's not very interesting.
  7169. 5:12:32What we want to do is if we know
  7170. 5:12:34everything's at uh zero, let's go ahead
  7171. 5:12:37and write a value to a specific
  7172. 5:12:39register. And uh we'd expect that when
  7173. 5:12:41we select and selected that register,
  7174. 5:12:43the uh output would be captured here or
  7175. 5:12:45the value of that register after we
  7176. 5:12:46perform the write instruction. So let's
  7177. 5:12:49write this second register. This
  7178. 5:12:51register is the first one in our our
  7179. 5:12:54RAM. So that's address zero. This one is
  7180. 5:12:57address one in our RAM. So I'm going to
  7181. 5:12:58punch in address one. And then let's say
  7182. 5:13:01we want to write all ones to it. Uh 11
  7183. 5:13:05one1 in binary. Um which I believe is
  7184. 5:13:0715. 8 + 4 is 12 + 2 + 1. Yep, that's 15.
  7185. 5:13:11So we have 15. We want to write 15, the
  7186. 5:13:14value 15 into address one. So we've
  7187. 5:13:17specified those two things. And what we
  7188. 5:13:19now need is a rising clock edge. So I'm
  7189. 5:13:21going to punch this. And the exact
  7190. 5:13:23moment I punch this, two interesting
  7191. 5:13:25things are going to happen. First the
  7192. 5:13:27value 1111 should be captured in this
  7193. 5:13:29register. So you'd expect to see these
  7194. 5:13:31four lines light up. And then since uh
  7195. 5:13:35we are selecting this address still and
  7196. 5:13:37not changing that this output line is
  7197. 5:13:40all is still going to be reading what's
  7198. 5:13:42in this register. So we'd expect 111 to
  7199. 5:13:45now be the new value in the register. So
  7200. 5:13:47if our implementation is correct, we'd
  7201. 5:13:49expect 1111 to appear on the output
  7202. 5:13:51line. So let's go ahead and trigger a
  7203. 5:13:54rising clock edge to see that. And it
  7204. 5:13:58everything looks correct to me. We have
  7205. 5:14:011111 as the value in the register and
  7206. 5:14:03all ones
  7207. 5:14:05um on our output line. So let's do two
  7208. 5:14:08things. Let's clear this just for
  7209. 5:14:09visibility sake. It doesn't really
  7210. 5:14:11matter. We don't have to because we know
  7211. 5:14:13that the next time uh the values on this
  7212. 5:14:16line are going to be useful is the next
  7213. 5:14:18time our clock signal goes from low to
  7214. 5:14:20high. And it's at high right now. So I'm
  7215. 5:14:22also going to push it back to low. And
  7216. 5:14:24that's also not going to do anything
  7217. 5:14:25because of the discussion we had on
  7218. 5:14:27flip-flops earlier. But now if I go back
  7219. 5:14:30to address zero, it correctly has zero.
  7220. 5:14:33That's where we started. And in fact,
  7221. 5:14:35all the other addresses are still going
  7222. 5:14:36to spit out zero. What would be
  7223. 5:14:39interesting is if I specify address one,
  7224. 5:14:41I'd expect to see 1111 on the output. So
  7225. 5:14:44let's do that. And we can now see that
  7226. 5:14:46we are correctly selecting the right
  7227. 5:14:48output. So this looks good. Um let's go
  7228. 5:14:51ahead and punch in values to
  7229. 5:14:54uh the other registers just to make
  7230. 5:14:56sure. So we'll start with this one uh
  7231. 5:14:57register zero uh the first address spot.
  7232. 5:15:00So 0 0. We already have 0 0 punched in
  7233. 5:15:02here. And let's say we just want to
  7234. 5:15:03write the value one to it. Then as soon
  7235. 5:15:06as I push this, we'd see a rising clock
  7236. 5:15:09edge 0 to one. And we'd expect that
  7237. 5:15:11value to get latched in here. And then
  7238. 5:15:13since we're still selecting one, um
  7239. 5:15:15since we're still selecting the address
  7240. 5:15:16zero, we'd expect to see that on the
  7241. 5:15:18output side as well. So, let's go ahead
  7242. 5:15:19and do that. I see one and then I see
  7243. 5:15:23one. So, that looks good to me. Um,
  7244. 5:15:27let's now I'll undo this and unset that.
  7245. 5:15:30Let's write a value into the third
  7246. 5:15:31register. Oh, so we can actually say I
  7247. 5:15:33need to check that we haven't broken
  7248. 5:15:34anything. So, we know we wrote one here
  7249. 5:15:36and one one here. So, if I switch to
  7250. 5:15:39this address, I still see 1111.
  7251. 5:15:42And uh let's go ahead and uh write to
  7252. 5:15:45our last our third right of storage to
  7253. 5:15:47make sure. Um, so that's address 1 zero.
  7254. 5:15:50Let's say I want to spit in uh 1 one.
  7255. 5:15:54Then I'll trigger that rising clock
  7256. 5:15:55edge. We see that as soon as that
  7257. 5:15:57happens, 1 one gets written into the
  7258. 5:15:59register. And we also see that being
  7259. 5:16:01reflected on the output line. And we can
  7260. 5:16:04go ahead and do this for the final
  7261. 5:16:06register um by let's say we want to
  7262. 5:16:10write this value into it. Uh we specify
  7263. 5:16:12address 11 one and
  7264. 5:16:18that is interesting. So I wasn't
  7265. 5:16:21expecting it uh to actually read in any
  7266. 5:16:24value. Um
  7267. 5:16:27so let me see what's happening here.
  7268. 5:16:32My mistake here. So I I misspoke. What I
  7269. 5:16:34originally said is that uh when the only
  7270. 5:16:38way we can actually write this value
  7271. 5:16:40into a new register is when this goes
  7272. 5:16:43from zero to one. Uh I actually have a
  7273. 5:16:45clarification to make that uh that's
  7274. 5:16:48actually not the case. Um ideally after
  7275. 5:16:51we write a value into a register, we
  7276. 5:16:52should put that this uh read signal back
  7277. 5:16:55down. The reason is is let's say I
  7278. 5:16:57wanted to write a new value into address
  7279. 5:16:59111.
  7280. 5:17:01Um, let's say I wanted to write the
  7281. 5:17:02value 1111 into this final register down
  7282. 5:17:06here. So, if I switch to this address
  7283. 5:17:11and this is already high, it's going to
  7284. 5:17:13cause a right to happen. And I'll prove
  7285. 5:17:15that to you. Let me switch to address
  7286. 5:17:17one one. And we can see that the right
  7287. 5:17:19happens because that's the value being
  7288. 5:17:20reflected here. But, uh, that's actually
  7289. 5:17:22not what we want to happen. And at first
  7290. 5:17:24this appears to be a violation of what I
  7291. 5:17:26said that uh latches or sorry flip-flops
  7292. 5:17:28only write hold in a new value um when
  7293. 5:17:31you go from a low to high clock signal
  7294. 5:17:33but that's actually uh we have not
  7295. 5:17:35violated that that is still true. So if
  7296. 5:17:37I punch this back down let's say we want
  7297. 5:17:39to write this value into that register.
  7298. 5:17:41Um and
  7299. 5:17:44when I
  7300. 5:17:46uh what's interesting is when I even if
  7301. 5:17:49this line is set to high when I go and
  7302. 5:17:51push the right address of 1 one in what
  7303. 5:17:54happens is that that triggers this and
  7304. 5:17:56gate here. Um so this uh this the
  7305. 5:18:00selector will then get the right value
  7306. 5:18:03to see hey one is set and one is set. So
  7307. 5:18:05it comes through and uh originally this
  7308. 5:18:08AND gate was outputting a zero but then
  7309. 5:18:09it outputs a one and it goes from a low
  7310. 5:18:11to a high and that output of that AND
  7311. 5:18:13gate is what we're feeding into the
  7312. 5:18:14clock signal not actually just the clock
  7313. 5:18:17signal itself. So that's why that
  7314. 5:18:19happens. Um so we should be careful that
  7315. 5:18:23uh once we write a value in we should
  7316. 5:18:25push this signal back to low. Um, and
  7317. 5:18:28that way when I swap to address one one,
  7318. 5:18:31this time I see that the value on the
  7319. 5:18:33input line isn't uh getting written to
  7320. 5:18:37immediately. In fact, it's not until I
  7321. 5:18:39click this and trigger the rising edge
  7322. 5:18:40that that happens. And the the nuance is
  7323. 5:18:43then that now that I've done my right, I
  7324. 5:18:44should push this back down to avoid uh
  7325. 5:18:46the issue I just described happening
  7326. 5:18:50over the ALOU arithmetic logic unit that
  7327. 5:18:53we built uh probably about an hour ago.
  7328. 5:18:56And I made a few modifications. So first
  7329. 5:18:59it was adding on 8 bit numbers and
  7330. 5:19:02therefore putting out 8 bit results.
  7331. 5:19:04Since our RAM only stores 4bit values,
  7332. 5:19:07I've go ahead and cut this ALU down to
  7333. 5:19:10four bit values only for both inputs and
  7334. 5:19:13outputs. And then the second thing I've
  7335. 5:19:16done is I've made both the inputs to
  7336. 5:19:19alus be registers and just let the
  7337. 5:19:21output be asis. And uh basically the
  7338. 5:19:25reason I've done this is because what
  7339. 5:19:27we'd like to do is connect the ALOU and
  7340. 5:19:31the ALU and the RAM together. And so
  7341. 5:19:34what that might look like is I might
  7342. 5:19:36load one value from a particular RAM
  7343. 5:19:38address into this register. Then put on
  7344. 5:19:41a different address in the selector,
  7345. 5:19:43load that value into this part of RAM
  7346. 5:19:46into this register. And then I let the
  7347. 5:19:48arithmetic happen. And then maybe I want
  7348. 5:19:50to take the output and then write that
  7349. 5:19:52back into a particular address in RAM.
  7350. 5:19:56And so um I' I've made each of these
  7351. 5:20:00inputs be registers so we can read from
  7352. 5:20:02different parts of RAM uh without
  7353. 5:20:04worrying about like arithmetic
  7354. 5:20:05happening. Um and then we'll deal with
  7355. 5:20:07the output of this and how we can like
  7356. 5:20:09that together. So uh here's the output
  7357. 5:20:12of our RAM. If we want to be able to
  7358. 5:20:14read from RAM into uh positions in this
  7359. 5:20:18ALU, then what I could do is I could
  7360. 5:20:21wire each of these output positions into
  7361. 5:20:24the input positions of the registers
  7362. 5:20:26that represent the input to the ALU. So
  7363. 5:20:29we'll call this the A register and the
  7364. 5:20:30the B register. Um and so if I did that,
  7365. 5:20:34you know, I have a full connection of
  7366. 5:20:35these four to these four inputs and then
  7367. 5:20:37these four to these four inputs. And if
  7368. 5:20:40I wanted to load a value, what I could
  7369. 5:20:42do is I could, you know, punch in the
  7370. 5:20:43address here. The value would come up.
  7371. 5:20:45It'd read into both these registers, but
  7372. 5:20:47then because of the clock command, it
  7373. 5:20:49wouldn't actually write into those
  7374. 5:20:50registers until we'd actually pulse one
  7375. 5:20:52of those. Um, so that would work and
  7376. 5:20:55that would be fine. But the issue is
  7377. 5:20:58that as we design this computer, there's
  7378. 5:21:01going to be more components. And what
  7379. 5:21:03we're doing is a onetoone mapping of
  7380. 5:21:06outputs to inputs that we know are going
  7381. 5:21:08to exist. But let's say the RAM like
  7382. 5:21:11there's other things that might want to
  7383. 5:21:12read from RAM. Then this would require
  7384. 5:21:14us to also extend not only wires from
  7385. 5:21:17here to here to here. Uh but also from
  7386. 5:21:20here to you know maybe somewhere else
  7387. 5:21:21conceptually on this on this figure
  7388. 5:21:23board. Um and both of you both these uh
  7389. 5:21:28like this gets do the same result. Like
  7390. 5:21:29there's nothing wrong with this in terms
  7391. 5:21:31of functional correctness. This will
  7392. 5:21:33behave as expected. But you can already
  7393. 5:21:35see here you know simple things require
  7394. 5:21:37a lot of wires and uh it's it's it's
  7395. 5:21:40pushing the design limits with a lot of
  7396. 5:21:42connections happening. Um
  7397. 5:21:45similarly you know like the output of
  7398. 5:21:48this uh alou if we want to write that to
  7399. 5:21:50a specific position in RAM the easy
  7400. 5:21:53solution is to take these outputs and
  7401. 5:21:55you know kind of wire them into the
  7402. 5:21:57inputs here. But then you know what if
  7403. 5:21:59something else in the computer wants to
  7404. 5:22:01write write into RAM it also would then
  7405. 5:22:02have to you know wire something to these
  7406. 5:22:05same lines um that gets risky you know
  7407. 5:22:08you don't want to be writing uh
  7408. 5:22:11different values on different lines at
  7409. 5:22:13the same time by accident um and so this
  7410. 5:22:17generally becomes a like design
  7411. 5:22:18bottleneck here. So what we do is
  7412. 5:22:22introduce the concept of a bus.
  7413. 5:22:25And what a bus is in our example here,
  7414. 5:22:29it'd be pretty much it's fairly
  7415. 5:22:31straightforward. It just be four control
  7416. 5:22:35lines.
  7417. 5:22:40And then for every component that wants
  7418. 5:22:42to interact with another component of
  7419. 5:22:44the computer, instead of wiring those
  7420. 5:22:46two components to touch each other
  7421. 5:22:47directly, we just wire those two
  7422. 5:22:50components to touch the bus. And then um
  7423. 5:22:54if I want to read a value into this
  7424. 5:22:56register, I would have the output uh of
  7425. 5:22:59the RAM write some value onto the bus
  7426. 5:23:02and then this register would just read
  7427. 5:23:03whatever value is on the bus. Similarly,
  7428. 5:23:05this register could connect to the bus
  7429. 5:23:07too. And that way instead of having a
  7430. 5:23:09onetoone pairing um between everything
  7431. 5:23:12that wants to connect to each other,
  7432. 5:23:14each component just needs to have one
  7433. 5:23:16connection to the bus and that's all. So
  7434. 5:23:18it's you know it's it becomes like a one
  7435. 5:23:21uh connection per component rather than
  7436. 5:23:24something that in the worst case could
  7437. 5:23:25be exponential if every component wanted
  7438. 5:23:28to connect to every other component. And
  7439. 5:23:30this may seem trivial in the context of
  7440. 5:23:33connecting RAM and the ALOU, but uh you
  7441. 5:23:37know now that we've once we finished
  7442. 5:23:38these two components, we're going to go
  7443. 5:23:40ahead and add a few more simpler ones to
  7444. 5:23:42the computer. Simpler in terms of
  7445. 5:23:44understanding them, but still critical
  7446. 5:23:46for correctness and actually making this
  7447. 5:23:48computer run independently without us
  7448. 5:23:50needing to punch everything. Um so as
  7449. 5:23:53long as you can agree or understand
  7450. 5:23:54what's happening here, then um that
  7451. 5:23:58makes sense. So then similarly you know
  7452. 5:24:00like if we wanted the output of RAM to
  7453. 5:24:03be input uh possibly to each of our
  7454. 5:24:05operands for the ALOU we just write this
  7455. 5:24:08output of RAM onto the bus. We connect
  7456. 5:24:10RAM to the output of RAM to the bus and
  7457. 5:24:12then we connect each of these input
  7458. 5:24:14registers to the bus as well. Um and
  7459. 5:24:16that works uh you know we could
  7460. 5:24:19literally hardcode these wires as is. Uh
  7461. 5:24:22and then similarly you would want to say
  7462. 5:24:25if we want to then write the output of
  7463. 5:24:27possibly an arithmetic computation into
  7464. 5:24:30RAM we just connect our output here to
  7465. 5:24:32the bus and then we could connect our
  7466. 5:24:34our our input over here to the bus as
  7467. 5:24:36well. And that way you know if there was
  7468. 5:24:38we had some result here and we wanted to
  7469. 5:24:40write it. We'd you know let this value
  7470. 5:24:41write to the bus. We we'd tell hey RAM
  7471. 5:24:43RAM needs to stop writing onto the bus.
  7472. 5:24:46Um this value can write it it's what it
  7473. 5:24:48wants onto the bus. And then uh this
  7474. 5:24:52value over here can can
  7475. 5:24:55read what's on the bus and then you know
  7476. 5:24:57we punch in the right address and you
  7477. 5:24:59know pulse this clock and that'd be
  7478. 5:25:01fine. So the only thing to be careful
  7479. 5:25:03about here um anything can read from the
  7480. 5:25:06bus. Reading from the bus is free. So
  7481. 5:25:08literally to connect these registers to
  7482. 5:25:10the bus I just wired them in as is. But
  7483. 5:25:13the issue is we have to be careful to
  7484. 5:25:17ensure that only at most one thing is
  7485. 5:25:20writing to the bus at a given time. It
  7486. 5:25:22is totally fine if nothing is writing to
  7487. 5:25:24the bus. But we'd uh we cannot have two
  7488. 5:25:27things writing to the bus. The reason
  7489. 5:25:29this cannot happen is because you could
  7490. 5:25:32short your entire circuit. So what
  7491. 5:25:35happens if this wants to write a one to
  7492. 5:25:39this wire of the bus and this is still
  7493. 5:25:41just connected to the bus without any
  7494. 5:25:42disconnecting capability. Uh and this
  7495. 5:25:44writes a zero to the bus. So what
  7496. 5:25:48happens is if you're writing a one to
  7497. 5:25:50the one to a wire and a zero to a wire
  7498. 5:25:53well the wire as we learned earlier can
  7499. 5:25:55only have a zero or a one but you are
  7500. 5:25:57simultaneously trying to write a one and
  7501. 5:25:59a zero to the wire at the same time. Um,
  7502. 5:26:03this this is bad. This can't happen.
  7503. 5:26:05It's going to actually, you know, it
  7504. 5:26:06would it would break your computer. So,
  7505. 5:26:09I actually don't know the the physics
  7506. 5:26:11behind this too well about electricity
  7507. 5:26:13itself. So, I won't try to speak to this
  7508. 5:26:15too much. Um, I believe this is just
  7509. 5:26:17called a short circuit. You know,
  7510. 5:26:19connecting high to low directly with
  7511. 5:26:21electricity uh will cause like a surge
  7512. 5:26:23of electricity to flow through uh and
  7513. 5:26:26it'll just like blow your computer. Um,
  7514. 5:26:28I'm not going to say anything more than
  7515. 5:26:29that because honestly I don't
  7516. 5:26:30understand. But, um, that's what we need
  7517. 5:26:33to be careful about. Uh, the issue that
  7518. 5:26:36I just described only applies when two
  7519. 5:26:37things try to write onto the bus at the
  7520. 5:26:39same time. I said that they only apply
  7521. 5:26:41when you try to write onto the bus uh,
  7522. 5:26:44and not when you read from the bus. And
  7523. 5:26:45that's because when you're reading from
  7524. 5:26:47the bus, you're not like these
  7525. 5:26:48components aren't actually putting out a
  7526. 5:26:50zero or a one. It's just reading what's
  7527. 5:26:52on the wire connecting to it. So uh
  7528. 5:26:54reading from the bus is fine because
  7529. 5:26:55you're not actually putting a value out
  7530. 5:26:57there. The issue is when you put
  7531. 5:26:58possibly contradicting information onto
  7532. 5:27:00a wire. So how do we get around this is
  7533. 5:27:04something called a in this case um we
  7534. 5:27:07will what we'd like to do is simply uh
  7535. 5:27:10if we know something is about to write
  7536. 5:27:13onto the bus uh that and we want we
  7537. 5:27:15would actually want to you know
  7538. 5:27:16establish that connection with the
  7539. 5:27:18outputs onto the wires. Um, but if
  7540. 5:27:21something is that something is in a
  7541. 5:27:23state where it could write to the bus in
  7542. 5:27:24the future, but it doesn't want to right
  7543. 5:27:26now, we need to disconnect it
  7544. 5:27:28physically. So, uh, one way is, you
  7545. 5:27:30know, simply destroy the wires and then
  7546. 5:27:32reinstrument them when you do actually
  7547. 5:27:34want to ride on the bus. That's pretty
  7548. 5:27:35painful to do. So, instead, what we have
  7549. 5:27:38is, uh, turns out this is a common
  7550. 5:27:39enough thing that we can just use
  7551. 5:27:42something called a
  7552. 5:27:44switch. um
  7553. 5:27:49switch. Oh, there's a whole section for
  7554. 5:27:51them. And what the switch will let us do
  7555. 5:27:53is it'll literally, you can kind of see
  7556. 5:27:54this in this diagram. It might be a
  7557. 5:27:56little small. Um uh if I click it, uh
  7558. 5:28:00it'll, you know, connect the two ends of
  7559. 5:28:02the wire. So, here's your switch. You'd
  7560. 5:28:03have something like this and then this.
  7561. 5:28:06So, in this current state, these two
  7562. 5:28:08wires are not connected. But then if I
  7563. 5:28:10was writing the simulation, I clicked
  7564. 5:28:11it, it drop this down and it connected
  7565. 5:28:13two wires. And so that's pretty much
  7566. 5:28:16what we're going to use here. When we
  7567. 5:28:17don't want this or this to be writing
  7568. 5:28:19onto the bus, uh, then we disconnect it.
  7569. 5:28:22And when we do want to write its value
  7570. 5:28:23onto the bus, we'd click it and it would
  7571. 5:28:25drop down and establish a full
  7572. 5:28:26connection. So let me go ahead and wire
  7573. 5:28:29this up and then we'll come back and see
  7574. 5:28:30what that actually looks like in
  7575. 5:28:32practice.
  7576. 5:28:51Okay, so I finished wiring things up. A
  7577. 5:28:54few changes I've made. So, first off, we
  7578. 5:28:56had a lot of extra space here. Uh, I I
  7579. 5:28:58condensed things in. No functional
  7580. 5:29:00changes there, but just wanted to make
  7581. 5:29:02things so I had to do less navigation
  7582. 5:29:04and zooming around.
  7583. 5:29:06Second thing you'll see is I've
  7584. 5:29:07implemented the bus right here. It's
  7585. 5:29:09just four ordinary wires. The A and B
  7586. 5:29:12registers to our ALU read in into each
  7587. 5:29:16of their corresponding data bits from
  7588. 5:29:18this bus.
  7589. 5:29:21Similarly, the output writes onto the
  7590. 5:29:24bus, but it's gated behind these
  7591. 5:29:26switches we've talked about. Therefore,
  7592. 5:29:29we don't risk two different components
  7593. 5:29:30running onto the bus at the same time,
  7594. 5:29:32breaking our circuit and leading to an
  7595. 5:29:34invalid state. And I've also updated
  7596. 5:29:38RAM. So, the output of RAM also writes
  7597. 5:29:42onto bus. And similarly, it's gated
  7598. 5:29:45behind these switches for the
  7599. 5:29:46affformentioned reasons. And on top of
  7600. 5:29:49that, for when we want to write read
  7601. 5:29:51values uh from the bus into a specific
  7602. 5:29:54part of RAM, I've now connected our
  7603. 5:29:56input lines which we previously had as
  7604. 5:29:58toggles up here uh onto the bus. And
  7605. 5:30:02then other thing I've done, I've added
  7606. 5:30:06outputs to our bus so we can easily see
  7607. 5:30:08the value on the bus. It's already
  7608. 5:30:10apparent by like the dark green versus
  7609. 5:30:12light green. I just think sometimes
  7610. 5:30:14having this explicit signal is easier to
  7611. 5:30:18uh see, especially if if you're watching
  7612. 5:30:21this on a phone or something. And then
  7613. 5:30:24um the last changes I've made are up
  7614. 5:30:26here. So the first one is for being able
  7615. 5:30:29to uh write stuff onto the bus myself
  7616. 5:30:32manually. I have these input lines and
  7617. 5:30:35they're also connected by switches
  7618. 5:30:37because that's writing onto the bus
  7619. 5:30:38which has to be gated against. And uh
  7620. 5:30:41this is because when we turn on our
  7621. 5:30:43computer, all of the registers,
  7622. 5:30:45flip-flops in this software will
  7623. 5:30:48initialize to zero. So everything in RAM
  7624. 5:30:51will be zero. Everything in the ALOU
  7625. 5:30:52will be zero. So if we wanted to move
  7626. 5:30:54values from RAM to the ALOU, do some
  7627. 5:30:56math and then send them back to
  7628. 5:30:57different address in RAM. It would be uh
  7629. 5:30:59inconsequential
  7630. 5:31:01um because every value is zero. So when
  7631. 5:31:04we turn on the computer or start the
  7632. 5:31:06simulation, I'll use this these manual
  7633. 5:31:08toggles to write some values into
  7634. 5:31:10specific addresses of RAM. And that way
  7635. 5:31:12we can actually move them onto the bus
  7636. 5:31:14and then into a register of the ALOU op
  7637. 5:31:17one of the ALOU operand registers. And
  7638. 5:31:19then we'll actually have some sensical
  7639. 5:31:20data about rather than dealing with a
  7640. 5:31:22bunch of zero plus zeros. And the last
  7641. 5:31:25thing I've done is uh you probably
  7642. 5:31:28noticed that we have a decent number of
  7643. 5:31:29signals now. Uh signals, flags, however
  7644. 5:31:32you want to call it, but um basically we
  7645. 5:31:34have several registers. So you know we
  7646. 5:31:36need a a flag to write into the register
  7647. 5:31:38A, a flag to write into register B. For
  7648. 5:31:41our ALOU, we have an add subtract flag.
  7649. 5:31:43Then we also have a flag to update a
  7650. 5:31:45specific address in RAM. Then we have
  7651. 5:31:47two inputs um for when we want to select
  7652. 5:31:49a specific address in RAM. So I grouped
  7653. 5:31:52all of these up here. And I also wanted
  7654. 5:31:54to call out that uh or highlight doubly
  7655. 5:31:57so that these switches which control
  7656. 5:31:59whether or not we write onto RAM are
  7657. 5:32:01right now something that we'll have to
  7658. 5:32:02click and toggle. Uh if we use a
  7659. 5:32:04different unit, we could you know kind
  7660. 5:32:06of add that functionality uh as
  7661. 5:32:08something we toggle here. Uh maybe I'll
  7662. 5:32:10do that afterwards. But what I wanted to
  7663. 5:32:12highlight is that um these these two
  7664. 5:32:16areas or three areas we write onto RAM
  7665. 5:32:18plus these are all that we have to
  7666. 5:32:20control. And once we get this
  7667. 5:32:23demonstration working, uh, a natural
  7668. 5:32:25next question is like, well, okay, these
  7669. 5:32:27things are all punched by humans. So
  7670. 5:32:28like, how do we actually get the
  7671. 5:32:29computer to know like a how is it, how
  7672. 5:32:32could it be possible the computer could
  7673. 5:32:33even know to punch certain things? Like
  7674. 5:32:36what is the computer meant to be doing
  7675. 5:32:37when it's turned on? And b, if the
  7676. 5:32:39computer had some instruction uh about
  7677. 5:32:42what it should be doing, then how could
  7678. 5:32:44we actually rearchitect this to make it
  7679. 5:32:47such that the computer itself can be the
  7680. 5:32:49one pushing these buttons? Um, and I I
  7681. 5:32:52say that lightly because it doesn't
  7682. 5:32:53actually look at that in practice, but
  7683. 5:32:54uh the computer is not physically going
  7684. 5:32:56out and pushing these. It just it's
  7685. 5:32:58wired into control. That's the output of
  7686. 5:33:00some other decision-making part of our
  7687. 5:33:02computer that we'll get to. Um, but I
  7688. 5:33:05digress there. So now what I'm going to
  7689. 5:33:07do is go ahead and power this on. Uh, as
  7690. 5:33:09I kind of mentioned, you know,
  7691. 5:33:11everything is is all zeroed out. So if
  7692. 5:33:13I, you know, if I pulse pulse any
  7693. 5:33:16address in RAM, you can see that the
  7694. 5:33:18output here is zero. And then the other
  7695. 5:33:20thing that's actually new here is we've
  7696. 5:33:22seen in this software that you know zero
  7697. 5:33:24uh is this dark green and this brighter
  7698. 5:33:27green is denotes a one and we see that
  7699. 5:33:30on all of our wires and stuff but now
  7700. 5:33:31for the first time we're seeing gray
  7701. 5:33:33here. Uh and what is gray here? Gray
  7702. 5:33:36would be the absence of zero or one. So
  7703. 5:33:38now that we uh have things that connect
  7704. 5:33:40to the bus conditionally if nothing is
  7705. 5:33:43writing to the bus at any time then
  7706. 5:33:44there is simply just no values on the
  7707. 5:33:46bus which is fine. It's just a different
  7708. 5:33:49state that we uh we think about. You
  7709. 5:33:51know, we're used to our our wires and
  7710. 5:33:53all of our uh logic gates taking in
  7711. 5:33:55strictly zeros or ones. And that is true
  7712. 5:33:57and that still has to hold true. Um but
  7713. 5:34:00you know, it's not actually you can have
  7714. 5:34:02a wire. You can imagine this. It's not
  7715. 5:34:04that difficult a concept. If you just
  7716. 5:34:06had a physical wire that you held in
  7717. 5:34:07your hand that was not connected to
  7718. 5:34:09anything on either end, that would have
  7719. 5:34:11neither a zero or one uh no amount of
  7720. 5:34:14electricity on it. So that's what we're
  7721. 5:34:16seeing here. But uh if I actually want
  7722. 5:34:18to put some values on the bus manually
  7723. 5:34:19specified then uh you can see I can
  7724. 5:34:22toggle these switches which connects
  7725. 5:34:24it'll connect you know here's my input
  7726. 5:34:26line to the this line of the bus and
  7727. 5:34:27then you can see that you know since
  7728. 5:34:29we've now written values onto the bus
  7729. 5:34:31anything that connects to the bus and
  7730. 5:34:33the bus itself now has zeros or ones on
  7731. 5:34:35it. So let's say we want to uh write
  7732. 5:34:38some values into RAM. Now let's say I
  7733. 5:34:41just want to do something simple like in
  7734. 5:34:43address zero of our RAM I want to write
  7735. 5:34:45the value one. In address two of the RAM
  7736. 5:34:49I want to write the value two. And then
  7737. 5:34:51I want to add that one in that two. I'd
  7738. 5:34:54expect a three. And then I want to write
  7739. 5:34:56that back to a different address in RAM.
  7740. 5:34:58Um how can we do this? So first let's
  7741. 5:35:01use this manual hack I have. Let's write
  7742. 5:35:03one onto the bus. And let's actually
  7743. 5:35:05write uh that that value one. Let's read
  7744. 5:35:09that from the bus into the RAM address
  7745. 5:35:11zero. So we already have address zero
  7746. 5:35:13selected. So that's not that
  7747. 5:35:14interesting. And then all I need to do
  7748. 5:35:16now is pulse this line for writing into
  7749. 5:35:18RAM. And recall when we go from a low to
  7750. 5:35:21a high, RAM will catch that value. You
  7751. 5:35:23can kind of see that happening
  7752. 5:35:24internally. You can see, you know, uh
  7753. 5:35:26we're not letting RAM write onto the
  7754. 5:35:28bus, but if we did, it's trying to write
  7755. 5:35:29the value one. So that's cool. That
  7756. 5:35:31seems to work. Now we want to write the
  7757. 5:35:33value uh three two into actually let's
  7758. 5:35:36let's write an interesting value. Let's
  7759. 5:35:37write the value three um into the next
  7760. 5:35:40address in RAM. So I I put the value
  7761. 5:35:42three onto the bus and let's write that
  7762. 5:35:43into address 01. And um I messed up uh
  7763. 5:35:48it already actually captured the value
  7764. 5:35:51as you can see. Recall what we talked
  7765. 5:35:52about 20 minutes ago. I should have let
  7766. 5:35:54this value fall back to zero after I
  7767. 5:35:56wrote into the RAM, but I didn't. Um so
  7768. 5:35:59I fixed that now. put the the values in
  7769. 5:36:00RAM. And now let me go ahead and
  7770. 5:36:04disconnect our manual input from the bus
  7771. 5:36:07and connect the output of the RAM unit
  7772. 5:36:10onto the bus. And so now I can see if I
  7773. 5:36:13select address zero on the RAM selector,
  7774. 5:36:16we have the value one in address zero.
  7775. 5:36:19And if I select address one, we have the
  7776. 5:36:21value three. So we can see that we've
  7777. 5:36:23correctly written these values into RAM.
  7778. 5:36:25And now that we've written values into
  7779. 5:36:27RAM, let's get those into the ALOU. So
  7780. 5:36:29let's put the value one into this
  7781. 5:36:32register and then the value two into
  7782. 5:36:34this register and we'd hope to see a
  7783. 5:36:36three. So I have the value one written
  7784. 5:36:38on the bus because I have address zero
  7785. 5:36:39selected. And now what I need to do is
  7786. 5:36:42toggle the line to write into register
  7787. 5:36:44A. So I'll do that. And now register A
  7788. 5:36:46has the value one. I'll put that low.
  7789. 5:36:49Now I want to write uh the register I
  7790. 5:36:51want to take what's in address two of
  7791. 5:36:53RAM and write it into this register. So
  7792. 5:36:55let's put address two of RAM onto this
  7793. 5:36:57bus. At address two, we have the value
  7794. 5:36:59three. And I'll now post the line for
  7795. 5:37:02writing into register B. And when I do
  7796. 5:37:04that, you can see there's now this three
  7797. 5:37:05there. And so, um, now what I'm going to
  7798. 5:37:09do, I've done writing stuff from RAM
  7799. 5:37:11into these registers. So, I'm going to
  7800. 5:37:13disconnect our RAM output from the bus.
  7801. 5:37:16And now what we can see happening is
  7802. 5:37:19that our ALU, uh, well, this was
  7803. 5:37:21happening before I disconnected the bus.
  7804. 5:37:22I just didn't want to forget to do that.
  7805. 5:37:24Our ALOU has the output of three + one.
  7806. 5:37:29And we can see that it's trying to write
  7807. 5:37:31four onto the bus. We disconnected it
  7808. 5:37:33though. So let's connect the output of
  7809. 5:37:35the ALU onto the bus. And we see that
  7810. 5:37:38the value four is out there. And let's
  7811. 5:37:40write that back into a register. Let's
  7812. 5:37:42let's say let's write it into the
  7813. 5:37:44original address uh zero address zero in
  7814. 5:37:47RAM. Um I'll do that by setting the
  7815. 5:37:50correct address uh field. And then I
  7816. 5:37:53will pulse the flag to write into RAM.
  7817. 5:37:56And that should be done. So if I now
  7818. 5:37:58disconnect our ALOU from writing onto
  7819. 5:38:00the bus and let RAM write its value back
  7820. 5:38:02onto the bus. I can now see that with
  7821. 5:38:04address zero selected, the value four is
  7822. 5:38:07what's stored in there. Uh and if I
  7823. 5:38:09pulse back to the other address we set,
  7824. 5:38:10we'd expect a three, which we see 1 + 2.
  7825. 5:38:13Um and so yeah, this looks functionally
  7826. 5:38:15complete. Uh and this is pretty
  7827. 5:38:17interesting because now we're doing
  7828. 5:38:19complex arithmetic. you know we have
  7829. 5:38:21four different values we can be
  7830. 5:38:22operating with. So we can do a plus b
  7831. 5:38:26minus c plus d whatever we want to do by
  7832. 5:38:29you know simply moving values from RAM
  7833. 5:38:31into these corresponding registers
  7834. 5:38:33setting the add or subtract flag if we
  7835. 5:38:34want to do subtraction letting the ALU
  7836. 5:38:37do its computation writing that value
  7837. 5:38:39back onto the bus and then reading it
  7838. 5:38:40from the bus back into RAM. And you can
  7839. 5:38:42kind of see we we did something very
  7840. 5:38:43sequential here. you know, this is very
  7841. 5:38:45reproducible. Um, you know, to to write
  7842. 5:38:48a value onto the uh to read a value from
  7843. 5:38:51the bus into RAM, you have to, you know,
  7844. 5:38:53specify the address plus the flag. Then,
  7845. 5:38:55if you wanted to read a value, write a
  7846. 5:38:57value from RAM onto the bus, you need to
  7847. 5:38:58connect the the uh RAM output to the
  7848. 5:39:02bus, select the right address, and then
  7849. 5:39:04it'll be on the bus. And then, we wanted
  7850. 5:39:07to read the value from the bus into one
  7851. 5:39:08of these registers, we just do the same
  7852. 5:39:10thing. We just post that flag. Uh, it's
  7853. 5:39:12pretty interesting. So the last thing I
  7854. 5:39:14want to show here is I said you can't
  7855. 5:39:16write zero and one uh to the bus at the
  7856. 5:39:19same time else you cause a short. So
  7857. 5:39:23we have the value uh let's look at this
  7858. 5:39:25rightmost the rightmost bus position has
  7859. 5:39:27a one on it. Let's what see what happens
  7860. 5:39:29if I put a zero on it and then try
  7861. 5:39:32connect this zero to the bus. And when
  7862. 5:39:34that happens what you're going to see is
  7863. 5:39:35that we're connecting a zero and the one
  7864. 5:39:37and I guarantee you the software is not
  7865. 5:39:39going to like it. Perfect. Yeah. So more
  7866. 5:39:42than one output is active on a wire
  7867. 5:39:44causing a short circuit. Cool. So I was
  7868. 5:39:46right that when you try to write a
  7869. 5:39:48concurrent zero and a one onto a
  7870. 5:39:50specific wire, it's called a short
  7871. 5:39:51circuit. Um cool. So
  7872. 5:39:56that uh now concludes our ALU and our
  7873. 5:39:59RAM sections. Uh this is probably the
  7874. 5:40:02trickiest part. Uh it is a tricky part
  7875. 5:40:06to reason about holistically. you know,
  7876. 5:40:08you get into a a lot of logic gates, but
  7877. 5:40:10um the rest of the stuff we the next few
  7878. 5:40:13things we'll do are far simpler and uh
  7879. 5:40:15are just going to iterate on uh be
  7880. 5:40:17things actually reductions of what we've
  7881. 5:40:18already seen. So, the next little bit of
  7882. 5:40:20the video should be a bit chiller.
  7883. 5:40:24So, one of the things that you might
  7884. 5:40:26have noticed that can be kind of
  7885. 5:40:27annoying during this process was uh
  7886. 5:40:29trying to go from binary values to
  7887. 5:40:31figure out what the actual decimal value
  7888. 5:40:33was uh for a particular thing. So, you
  7889. 5:40:36know, if we were feeding an address to
  7890. 5:40:38our RAM, uh, we kind of had to like peek
  7891. 5:40:40at these four lines and see which vendor
  7892. 5:40:42were set to then know what the actual
  7893. 5:40:44value was. And that's kind of how we
  7894. 5:40:45were sanity checking. Similarly, if we
  7895. 5:40:47wanted to know what was stored in the
  7896. 5:40:48registers for the ALOU, the A and B
  7897. 5:40:50registers, we kind of have to look at
  7898. 5:40:52these four lines or these four lines and
  7899. 5:40:54and figure out what's happening uh what
  7900. 5:40:56the actual value is. And you know this
  7901. 5:40:59is 4bit values so it's not too much
  7902. 5:41:02effort to do it but it can be kind of
  7903. 5:41:03annoying to go to have to do that so
  7904. 5:41:04much um especially when you're you know
  7905. 5:41:07kind of early onto this and still bring
  7906. 5:41:08your intuition. So what if we had a way
  7907. 5:41:12to actually just visualize the numbers?
  7908. 5:41:14You know, we know computers
  7909. 5:41:16operate in base 2 binary, but you know,
  7910. 5:41:20us humans when we're debugging these
  7911. 5:41:21machines or or trying to inspect the
  7912. 5:41:23internal state at any particular time.
  7913. 5:41:25Be nice to be able to see like, hey,
  7914. 5:41:27like what values on the bus, uh, and I
  7915. 5:41:30could see that, you know, just as digits
  7916. 5:41:32without having to, you know, scroll in
  7917. 5:41:34to the bus and look at each of the wires
  7918. 5:41:36and, you know, start counting powers of
  7919. 5:41:38two and adding them together to figure
  7920. 5:41:40this out. And um for this we have
  7921. 5:41:43something called a seven segment
  7922. 5:41:44display. So I'm going to hop over here.
  7923. 5:41:47Um and this is something that you can
  7924. 5:41:50see takes in a binary input up here i.e.
  7925. 5:41:53machine readable values and it'll it'll
  7926. 5:41:56draw out the actual uh decimal value
  7927. 5:41:59that we can see that we can easily
  7928. 5:42:01reason about. And some of you may have
  7929. 5:42:04seen this some of you may have claimed
  7930. 5:42:05to have never seen this before but I
  7931. 5:42:06guarantee that you actually have seen
  7932. 5:42:08this. If I come over here, uh this is a
  7933. 5:42:11scoreboard, a picture of a scoreboard
  7934. 5:42:13probably that you maybe have seen in
  7935. 5:42:14your high school. And you can see that
  7936. 5:42:17actually everything is a seven seg seven
  7937. 5:42:19segment display. So why do we call it a
  7938. 5:42:22seven segment display? If you look at
  7939. 5:42:24each of these numbers, let's just pick
  7940. 5:42:26this one here arbitrarily. You can see
  7941. 5:42:28that there's um seven different lines
  7942. 5:42:32that make up the display. So there's
  7943. 5:42:34four up here and then three in the
  7944. 5:42:36bottom. 4 + 3 7. And
  7945. 5:42:41um by turning on or off certain
  7946. 5:42:43combinations of these individual lights,
  7947. 5:42:46we can create the illusion of drawing
  7948. 5:42:48the number. And I wouldn't even say it's
  7949. 5:42:50an illusion. We actually just are
  7950. 5:42:51drawing the number. And uh this is a
  7951. 5:42:54pretty simple way, you know, without
  7952. 5:42:55having to get a whole LCD display, which
  7953. 5:42:58we'll talk about later. Um, it's an easy
  7954. 5:43:00way to draw numbers in a way that this
  7955. 5:43:03is significantly easier to see that this
  7956. 5:43:04is a two rather than trying to like do
  7957. 5:43:06some binary to decimal conversions and
  7958. 5:43:09reason about that yourself. And there
  7959. 5:43:12are things like there's seven segment
  7960. 5:43:14display, there's higher number segment
  7961. 5:43:16displays um, and those can do more
  7962. 5:43:19complicated things and maybe show
  7963. 5:43:21symbols like the division symbol. Um,
  7964. 5:43:24for the sake of illustration here, we're
  7965. 5:43:25going to keep it to seven segment
  7966. 5:43:27displays. And we're also going to keep
  7967. 5:43:30it down to just one single digit. So,
  7968. 5:43:33uh, we know that our bus here is the one
  7969. 5:43:35we've built is actually four bits. So,
  7970. 5:43:37there's two to the four different val
  7971. 5:43:40uh, different numbers. Sorry, there's 2
  7972. 5:43:42to the^ of four different values that we
  7973. 5:43:45could write on the bus. 2 to the^ of
  7974. 5:43:47four is 16, which means we can represent
  7975. 5:43:50with these four bits numbers 0 through
  7976. 5:43:5115. So, it turns out digits 10 through
  7977. 5:43:5415 require two of these displays if we
  7978. 5:43:57wanted to. Um, the logic is the exact
  7979. 5:44:00same. It just requires more effort. And
  7980. 5:44:02so, we're going to keep it simple and
  7981. 5:44:03say that like our display for now will
  7982. 5:44:05only show the numbers 0 through 9. And
  7983. 5:44:08uh you'll understand that if we wanted
  7984. 5:44:10to have uh we could put two of these
  7985. 5:44:12next to each other if we wanted to
  7986. 5:44:14handle two digits, but it just requires
  7987. 5:44:16a lot more work uh and doesn't teach you
  7988. 5:44:18anything that you wouldn't learn uh by
  7989. 5:44:20doing the one seg the one individual
  7990. 5:44:22digit display. And so this is what this
  7991. 5:44:25chip looks like in real life. You can
  7992. 5:44:27kind of see there's uh several pins on
  7993. 5:44:29the bottom here. And turns out if you
  7994. 5:44:32write a one or put power these are
  7995. 5:44:35synonymous things uh to one of these
  7996. 5:44:37specific pins then it'll light up a
  7997. 5:44:39corresponding part of the display. And
  7998. 5:44:42so if I look at a diagram uh that I
  7999. 5:44:45found online for what one of these these
  8000. 5:44:47physical units that you can buy what
  8001. 5:44:49that pin structure looks like, you kind
  8002. 5:44:51of see that uh you know we have these
  8003. 5:44:54these individual lights labeled A, B, C,
  8004. 5:44:57D, E, F, and G. It kind of wraps
  8005. 5:44:59clockwise with the G being the last one.
  8006. 5:45:01And they each correspond to a specific
  8007. 5:45:04uh input pin. So if I put one here and
  8008. 5:45:07zeros for the rest of these, we'd see
  8009. 5:45:10just this one light up. And you're also
  8010. 5:45:12probably wondering, uh, well, we have
  8011. 5:45:14inputs A through G. That makes sense.
  8012. 5:45:16What is this DP and ground? Um,
  8013. 5:45:20in this specific unit, DP will let you
  8014. 5:45:23hit that decimal point. And then ground
  8015. 5:45:26is just effectively you need to give it
  8016. 5:45:28power. Um, so it can complete the
  8017. 5:45:30circuit internally. We don't really have
  8018. 5:45:31to worry about that for the sake of what
  8019. 5:45:33we're building here. Um, but let's
  8020. 5:45:36actually see like what each number looks
  8021. 5:45:38like visually. So you can see we can
  8022. 5:45:41represent numbers zero through nine. And
  8023. 5:45:45to turn to represent the number uh zero,
  8024. 5:45:47we turn on A, B, C, D, E, and F. And
  8025. 5:45:52we'd leave G off. And for the number
  8026. 5:45:54one, uh we just turn on B and C and
  8027. 5:45:57leave everything else off. And you can
  8028. 5:45:59see that we can kind of just draw out
  8029. 5:46:00every number and pretty easily see what
  8030. 5:46:03we need to turn on to get each digit
  8031. 5:46:05position ready. So, what I've done is
  8032. 5:46:09gone ahead and made that into a table.
  8033. 5:46:12Um, we have our 10 different numbers
  8034. 5:46:14that we're going to be capable of
  8035. 5:46:15drawing with our singledigit display.
  8036. 5:46:18And, uh, you can see decimal numbers
  8037. 5:46:21zero and then I have that in binary 2.
  8038. 5:46:24And you can see that, you know, for
  8039. 5:46:26zero, uh, you'd turn everything on
  8040. 5:46:28except G. And if I tab back here, you
  8041. 5:46:30can kind of see that's the same source
  8042. 5:46:32of truth we have here. We can check one
  8043. 5:46:34more as well. uh let's say the digit
  8044. 5:46:36seven, you'd only turn on A, B, and C.
  8045. 5:46:38So if I look here at digit seven, we can
  8046. 5:46:41see that only A, B, and C are on, and
  8047. 5:46:43the rest are off. And so this probably
  8048. 5:46:44looks familiar. This is the truth table
  8049. 5:46:46that we looked at way earlier in this
  8050. 5:46:48video. Um, and what I'd like to do is
  8051. 5:46:52actually go back to our software and and
  8052. 5:46:55build something out that'll let you
  8053. 5:46:58specify your 4bit binary value and it'll
  8054. 5:47:02correctly light up the correct segments
  8055. 5:47:04of the display. Um, so if I tab back
  8056. 5:47:07here, I can see that my software that
  8057. 5:47:11I'm using actually lets me have this.
  8058. 5:47:13Uh, so I've wired in our display and it
  8059. 5:47:16has uh, you know, pins for each of the
  8060. 5:47:20values. And so I can kind of see here's
  8061. 5:47:22that A, here's B, C, D,
  8062. 5:47:27E, F, G, and then the bottom right here
  8063. 5:47:31is the decimal point. We're not really
  8064. 5:47:32going to use it, so we don't really have
  8065. 5:47:34to worry about it for now. But uh what
  8066. 5:47:36I'd like to do is go from this truth
  8067. 5:47:40table to logic gates that actually
  8068. 5:47:42correctly light up the display. So let's
  8069. 5:47:44see if I look at uh this position A
  8070. 5:47:48here. Um position A, then I can see that
  8071. 5:47:52what numbers on the input line should
  8072. 5:47:55turn position A on. You can see that's
  8073. 5:47:57numbers one, two, or sorry, that's
  8074. 5:47:59number zero, two, three, five, six, 7,
  8075. 5:48:068, and 9, which is kind of what we see
  8076. 5:48:08in this table here. If I look at the
  8077. 5:48:09column for a, when should a be turned
  8078. 5:48:12on? It looks like it's for every number
  8079. 5:48:14except one and four. So then what I'd
  8080. 5:48:17want to do is if I had, you know, the
  8081. 5:48:19four uh binary inputs coming down from
  8082. 5:48:23the bus, if that's what we wanted to
  8083. 5:48:24display at any time, then I need to do
  8084. 5:48:27some logic where like if the value of of
  8085. 5:48:30the four bits represents any of those
  8086. 5:48:32numbers, then this input line should
  8087. 5:48:34turn on. And we do that for each of the
  8088. 5:48:37each of the different segment uh pills
  8089. 5:48:39that we have on our display here. And it
  8090. 5:48:42turns out we've already done something
  8091. 5:48:44pretty similar to this. We built this
  8092. 5:48:45decoder earlier for RAM where we have
  8093. 5:48:48something uh it's easier because there's
  8094. 5:48:50only two bits of addressing space for
  8095. 5:48:52our RAM. But basically we know that
  8096. 5:48:55whatever value the user puts on RAM uh
  8097. 5:48:58we can correctly select the right index
  8098. 5:49:01uh in our RAM. So you know uh it's 0 0
  8099. 5:49:04right now and we see this first position
  8100. 5:49:06lights up. When I put in one, I see that
  8101. 5:49:08second position lights up. And if I put
  8102. 5:49:11in the maximum index, that's 2 + 1 3.
  8103. 5:49:14And I can see 0 1 2 3. Uh index number
  8104. 5:49:18three lights up. So we've already built
  8105. 5:49:19out this decoder logic earlier. So we're
  8106. 5:49:22literally just going to do the same
  8107. 5:49:23thing except this time we have to extend
  8108. 5:49:25it uh to be four bits, which is a little
  8109. 5:49:27more complicated. So, what I'm going to
  8110. 5:49:29do is bring our bus further down
  8111. 5:49:34and
  8112. 5:49:36build this off to the side here.
  8113. 5:49:39So, I'm going to actually pause the
  8114. 5:49:41video or just speed through this part um
  8115. 5:49:44and and we'll we'll talk through the
  8116. 5:49:46output.
  8117. 5:50:03Okay, so I've gone ahead and built this
  8118. 5:50:05decoder and uh basically what I have I
  8119. 5:50:08detach it from the bus so it's its own
  8120. 5:50:10unit. Right now I have a 4bit input and
  8121. 5:50:13for each of those bits um I actually
  8122. 5:50:16carry that input bit down and then also
  8123. 5:50:19I carry a negated version of it all the
  8124. 5:50:20way down. Um, and the reason I do that
  8125. 5:50:23is because if I jump over to our RAM,
  8126. 5:50:26basically what we had to do for our RAM
  8127. 5:50:28whenever we wanted to negate a line was
  8128. 5:50:30to manually add an inverter.
  8129. 5:50:33And uh this means we end up using a lot
  8130. 5:50:36several inverters when really if we just
  8131. 5:50:38used one inverter up here to create a
  8132. 5:50:41forked version of or two one per line,
  8133. 5:50:44two different inverters to create a
  8134. 5:50:45forked variant that inverts each of the
  8135. 5:50:47two input bits, then we could have just
  8136. 5:50:49had our inputs pulled in from these
  8137. 5:50:50hypothetical lines directly. So, not
  8138. 5:50:52only does it use fewer logic gates to do
  8139. 5:50:54it the way we've done here, I'd also
  8140. 5:50:56argue it actually makes things easier to
  8141. 5:50:58read because we kind of know this means,
  8142. 5:51:00you know, uh, leftmost bit and then the
  8143. 5:51:03inverse of the leftmost bit, and then
  8144. 5:51:05bit position number two, the inverse of
  8145. 5:51:07that, bit position number three, the
  8146. 5:51:09inverse of that, the rightmost bit
  8147. 5:51:11position, the inverse of that. And so,
  8148. 5:51:12you can kind of see then, you know, uh,
  8149. 5:51:15this would correspond to zero, then one,
  8150. 5:51:17two, and so on. So for zero, what I'm
  8151. 5:51:19doing is I'm looking for 0 0 0 which is
  8152. 5:51:22the inverted of all of the bits. So 0 0
  8153. 5:51:29and zero. And you can kind of see that
  8154. 5:51:31you know uh if I were to turn this
  8155. 5:51:33circuit on and all the inputs were zero
  8156. 5:51:37then you can see all the inverted lines
  8157. 5:51:39turn on and then that triggers this this
  8158. 5:51:41and gate where it's looking for the the
  8159. 5:51:44this bit positions off this bit
  8160. 5:51:46positions off this bit positions off and
  8161. 5:51:48this bit position is off and that's why
  8162. 5:51:49we can see exactly one output lights up
  8163. 5:51:52and we've decoded this input. Um, and so
  8164. 5:51:56similarly, if I turn this off just for
  8165. 5:51:57visualization, let's jump to the next
  8166. 5:51:59one, which we'd expect to line up with
  8167. 5:52:01the 00001. So, we can see there's the
  8168. 5:52:05input in inverted of the first bit, the
  8169. 5:52:08inverted of the second bit, the inverted
  8170. 5:52:10of the third bit, and then the regular
  8171. 5:52:14of the fourth bit. And that would give
  8172. 5:52:15us that's saying 0 0 1. All four of
  8173. 5:52:21those have to be true. So now if I
  8174. 5:52:22powered this on and turn this to one,
  8175. 5:52:24we'd see the same thing. And you kind of
  8176. 5:52:26continue that logic all the way down.
  8177. 5:52:28And we'll verify this is correct by
  8178. 5:52:29counting through. So we tested zero and
  8179. 5:52:31one. Now if I plug in two, we'd expect
  8180. 5:52:33this third output to turn on. That's
  8181. 5:52:36case. If I go to three, we'd expect this
  8182. 5:52:39to come on. Cool. If I go to four, I'd
  8183. 5:52:42expect the next one to come on. Then
  8184. 5:52:45five. Cool. Then six.
  8185. 5:52:50then seven
  8186. 5:52:52and then
  8187. 5:52:54eight and the last one is nine. Cool. So
  8188. 5:52:58it looks like zero through nine work
  8189. 5:53:00correctly. And so now that I have an
  8190. 5:53:02output line for each of the numbers, um
  8191. 5:53:05I can feed this into our seven segment
  8192. 5:53:07display. So this simple one right here,
  8193. 5:53:10this bit turns on. This is the A in our
  8194. 5:53:13diagram. So if I come here uh I now want
  8195. 5:53:17to pull that to be I want to ore all I
  8196. 5:53:21would like to logically ore together all
  8197. 5:53:23the numbers for which this a should be
  8198. 5:53:25on which is 0 to so 0 2 3 5 6 7 8 9. So
  8199. 5:53:31we will draw lines from all of those
  8200. 5:53:34numbers that I just listed out. We will
  8201. 5:53:36or them all together using several
  8202. 5:53:38orgates and then feed it into this line
  8203. 5:53:40right here. So, I'll go ahead and do
  8204. 5:53:43that and then we'll uh come back.
  8205. 5:53:50Okay. So, I've gone ahead and built out
  8206. 5:53:53everything for all the logic for
  8207. 5:53:55deciding if we turn on a this top line
  8208. 5:53:57up here. And then for all the other
  8209. 5:53:59inputs to seven segment display, I've
  8210. 5:54:01just wired it to this thing right here,
  8211. 5:54:03which is called ground. It just means
  8212. 5:54:05zero. It's a logical zero. So, it means
  8213. 5:54:07all the other segments will be off. And
  8214. 5:54:09if I jump back to our table that we
  8215. 5:54:11started with over here, I can see that
  8216. 5:54:13we'd expect this A button to be on for
  8217. 5:54:16everything except numbers one and four.
  8218. 5:54:20So, let's go ahead and pulse through
  8219. 5:54:22each of the input combinations and
  8220. 5:54:25verify that this works. So, we start off
  8221. 5:54:27with zero. And this is on. That's good.
  8222. 5:54:30Now, I'm going to put one, and we'd
  8223. 5:54:31expect it to turn off. That looks good.
  8224. 5:54:34Two should be on. Three should be on.
  8225. 5:54:39Four should be off. That looks good.
  8226. 5:54:42Five should be on. Six should be on.
  8227. 5:54:45Seven should be on.
  8228. 5:54:47Eight should be on. And nine should be
  8229. 5:54:49on. So this looks totally good. And
  8230. 5:54:52we've done this together just by oring
  8231. 5:54:55together everything except the two
  8232. 5:54:57values for which this segment should be
  8233. 5:54:59off. So that means if if one or sorry if
  8234. 5:55:03zero or two or three or five or six or
  8235. 5:55:09seven or eight or nine is selected by
  8236. 5:55:13the user by the input then we should
  8237. 5:55:15turn on that one. So this is kind of the
  8238. 5:55:17logic uh and we'll go ahead and do this
  8239. 5:55:19for the remaining ones.
  8240. 5:55:38Okay, so we have built this out. You can
  8241. 5:55:40see there's a lot of logic gates. Uh I
  8242. 5:55:42probably could have done this more
  8243. 5:55:43elegantly in hindsight, but uh should be
  8244. 5:55:47working. Um, so you can kind of see for
  8245. 5:55:50each of these uh what we did for this
  8246. 5:55:52this A kind of repeated for B and C, D,
  8247. 5:55:57E, F, and G oring a bunch of stuff
  8248. 5:56:00together based on the input lines and
  8249. 5:56:02then feeding them around to this uh
  8250. 5:56:04seven segment display. And also that
  8251. 5:56:07control bit for the dot we just hardcode
  8252. 5:56:10to zero because we're not going to use
  8253. 5:56:11it here. So let's go ahead and
  8254. 5:56:15make sure that this works. So, I'll go
  8255. 5:56:17one and then I'll count to two. That
  8256. 5:56:20looks good. The next is three. That
  8257. 5:56:22looks good. Then four. That looks good.
  8258. 5:56:26Then five. That looks good. Then six.
  8259. 5:56:29That looks good. Seven looks good.
  8260. 5:56:33Eight looks good. And last but not least
  8261. 5:56:35is nine. So we can see that 0 through
  8262. 5:56:37nine work as expected. And then uh our
  8263. 5:56:41logic gets set up so they work for 0
  8264. 5:56:43through 9 correctly. We said we're not
  8265. 5:56:45going to consider multi-digit
  8266. 5:56:48uh numbers for now, but just out of
  8267. 5:56:50curiosity, what happens if I tried to
  8268. 5:56:52put in 10? And you can kind of see
  8269. 5:56:55uh just by chance, you're this one lit
  8270. 5:56:57up nothing. Um 11
  8271. 5:57:0212
  8272. 5:57:04Okay. Yeah. So,
  8273. 5:57:07um I was expecting gibberish output, but
  8274. 5:57:09that actually works. So, yeah, we can
  8275. 5:57:11see this this works as expected. you
  8276. 5:57:12didn't have to bear the pain of what I
  8277. 5:57:14went through to make these logic gates,
  8278. 5:57:15but you probably saw in the sped up
  8279. 5:57:17version. And you might be wondering
  8280. 5:57:19yourself, this seems kind of
  8281. 5:57:20inefficient. Um, you know, for everyone
  8282. 5:57:23who ever wants to use one of these
  8283. 5:57:24displays, they'd have to rebuild the
  8284. 5:57:26same exact circuit themselves, which
  8285. 5:57:28kind of sounds interesting and uh
  8286. 5:57:30redundant. And you can imagine there's
  8287. 5:57:33other times where we'd want to replicate
  8288. 5:57:36similar behavior because what we've
  8289. 5:57:38effectively built here is a lookup
  8290. 5:57:40table. So we we clearly specified here
  8291. 5:57:45for every possible input here is the
  8292. 5:57:48exact output we would want. Um and it
  8293. 5:57:53turns out this is a very common thing
  8294. 5:57:55and we'll actually come to it again not
  8295. 5:57:56too long from now on our computer uh
  8296. 5:57:58that there is a chip for this. So I'm
  8297. 5:58:01going to tab over here um
  8298. 5:58:05and there is something called ROM read
  8299. 5:58:08only memory. So let's read the
  8300. 5:58:10definition from Wikipedia. Readonly
  8301. 5:58:12memory is a type of nonvolatile memory
  8302. 5:58:15used in computers and other electronic
  8303. 5:58:17devices. Data stored in ROM cannot be
  8304. 5:58:19electronically modified after the
  8305. 5:58:21manufacturer of the memory device.
  8306. 5:58:23Readon memory is useful for storing
  8307. 5:58:26software that is rarely changed during
  8308. 5:58:27the life of the system. Software
  8309. 5:58:29applications such as video games for
  8310. 5:58:31programmable devices can be distributed
  8311. 5:58:32as plug-in cartridges containing ROMs.
  8312. 5:58:35So uh let's not get this confused with
  8313. 5:58:38RAM. RAM stands for random access
  8314. 5:58:40memory. I don't actually think I
  8315. 5:58:42explained why it's called that. Uh RAM
  8316. 5:58:44is called random access memory because
  8317. 5:58:46in our implementation we can look at any
  8318. 5:58:50specific address in RAM just by punching
  8319. 5:58:53in a value. So it supports random
  8320. 5:58:55lookups. Uh this might seem intuitive
  8321. 5:58:57but earlier versions of of memory were
  8322. 5:59:00actually sequentially accessed. So, if I
  8323. 5:59:02as if I read address one, I would have
  8324. 5:59:05to read address two next, and it'd have
  8325. 5:59:07to uh circle all the way back around the
  8326. 5:59:10address space for me to be able to read
  8327. 5:59:12address zero if I'm at address one
  8328. 5:59:14already. So, that's where RAM comes
  8329. 5:59:16from. ROM, read only memory, is pretty
  8330. 5:59:19different. Um, the second thing that we
  8331. 5:59:22should break down in this definition is
  8332. 5:59:23what is nonvolatile memory. So RAM is
  8333. 5:59:26considered volatile because when you
  8334. 5:59:28unplug and restart your computer,
  8335. 5:59:32RAM starts off being zeroed out or has
  8336. 5:59:35some random values. So the the values we
  8337. 5:59:37put in these registers are not lock are
  8338. 5:59:39not persisted. You can see that if I
  8339. 5:59:40punch in a value and then stop the
  8340. 5:59:42computer and start the simulation again,
  8341. 5:59:44that value is gone. Um, and I guess the
  8342. 5:59:48uh analogy to your computer is if you're
  8343. 5:59:50typing on a word document, if you
  8344. 5:59:53command S or control S if you're on
  8345. 5:59:55Windows to save it, that writes it to
  8346. 5:59:57your hard drive and that's that's
  8347. 5:59:58stored. Until you do that, the text that
  8348. 6:00:01you've you have pending in your editor
  8349. 6:00:03is just sitting in RAM of your computer.
  8350. 6:00:05And if you were to unplug it and plug it
  8351. 6:00:07back in, you lose that data. So, you've
  8352. 6:00:10probably seen this and dealt with the
  8353. 6:00:11frustration before of computer shuts
  8354. 6:00:13down or loses power, someone trips over
  8355. 6:00:15the cord, and you lose all of the state
  8356. 6:00:17of your computer. And that's because all
  8357. 6:00:19those things are stored in RAM, which is
  8358. 6:00:21significantly faster to access than from
  8359. 6:00:23disk. Uh, but has this trade-off of, you
  8360. 6:00:26know, not being persistent. But I
  8361. 6:00:29digress. Um, so ROM is effectively a big
  8362. 6:00:33lookup table. And something interesting
  8363. 6:00:36about ROM is that it says it cannot be
  8364. 6:00:38modified after the manufacturer of the
  8365. 6:00:41memory device. So if I actually wanted
  8366. 6:00:44um to build a readonly memory for what
  8367. 6:00:47we' built right here, I'd need to find a
  8368. 6:00:50manufacturer for ROM and effectively
  8369. 6:00:52send them this table and they would
  8370. 6:00:54manufacture a physical chip um that
  8371. 6:00:58would look like this. and you would plug
  8372. 6:01:00in your whenever you plug in your inputs
  8373. 6:01:02on some of these pins, it's guaranteed
  8374. 6:01:04to to light up the correct pins on the
  8375. 6:01:06output. Uh, which is pretty cool, but
  8376. 6:01:07you know, unfortunately,
  8377. 6:01:10uh, that's kind of limited. You know,
  8378. 6:01:11what if we accidentally made a mistake
  8379. 6:01:13in this table, got that ROM chip, and,
  8380. 6:01:17uh, realized that there was an issue,
  8381. 6:01:19we'd be out of luck. You can't do
  8382. 6:01:21anything about it. You'd have to go to
  8383. 6:01:22the manufacturer and and get a new one.
  8384. 6:01:25So, this is kind of inefficient. So what
  8385. 6:01:27came after ROMs was called PROMs which
  8386. 6:01:30stands for programmable ROM. And let's
  8387. 6:01:34read the definition from Wikipedia. It
  8388. 6:01:36says a PROM is a form of digital memory
  8389. 6:01:38where the contents can be changed once
  8390. 6:01:41after manufacturer of the device. So
  8391. 6:01:44basically what this means is uh I mean
  8392. 6:01:48it's exactly what it says. you now get
  8393. 6:01:50the ROM chip that we kind of have here.
  8394. 6:01:52But then there's also this burner and it
  8395. 6:01:55lets you specify the values as you want
  8396. 6:01:57them. So rather than having to spend
  8397. 6:01:59that data sheet to the manufacturer, the
  8398. 6:02:01manufacturer will just give you the ROM
  8399. 6:02:02and you can burn in the values yourself.
  8400. 6:02:05But if we look back to the definition,
  8401. 6:02:07uh it can say it says can be changed
  8402. 6:02:10once after manufacturer. So this kind of
  8403. 6:02:12still has the same mistake. If you
  8404. 6:02:14accidentally punch in the wrong value at
  8405. 6:02:15a specific for a specific input
  8406. 6:02:17combination, your chip's useless. Uh,
  8407. 6:02:19and you'd have to throw it away and get
  8408. 6:02:21a new one. So, that's also not optimal.
  8409. 6:02:23Um, so what came after that was called
  8410. 6:02:26EPROM. Stands for erasable programmable
  8411. 6:02:30readonly memory. And so, we're not going
  8412. 6:02:32to read this long definition, but what
  8413. 6:02:34it is is I'll show you a picture of it.
  8414. 6:02:36Uh, it looks very similar to the
  8415. 6:02:38readonly memory we saw earlier, but it
  8416. 6:02:40has this exposed element here. And it
  8417. 6:02:43turns out that if you uh shine UV light
  8418. 6:02:48onto this exposed element for enough
  8419. 6:02:50time, usually it's like 15 or 20
  8420. 6:02:52minutes, it'll wipe the entire contents
  8421. 6:02:54of the ROM uh out. It'll replace
  8422. 6:02:57everything with zeros, which means you
  8423. 6:02:59could then take a a programmer and do
  8424. 6:03:01the same kind of burning that we had for
  8425. 6:03:03uh programmable ROMs. So, this is pretty
  8426. 6:03:06cool. Um it now means that if we
  8427. 6:03:08accidentally plug in a wrong value, we
  8428. 6:03:10don't have to throw away our entire
  8429. 6:03:11trip. we can actually uh just redo it
  8430. 6:03:14ourselves. We can clear out the entire
  8431. 6:03:16contents of the chip and then redo it
  8432. 6:03:17ourselves and then it's still ROM at the
  8433. 6:03:19end of the day. So once it's in my
  8434. 6:03:21computer and I have it programmed, if I
  8435. 6:03:23keep powering the computer on and off,
  8436. 6:03:25the values in this lookup table will
  8437. 6:03:27still exist. So that's cool, but
  8438. 6:03:30um it turns out that's still suboptimal.
  8439. 6:03:33As I just said, it takes 15 to 20
  8440. 6:03:35minutes, I think, uh to clear out these
  8441. 6:03:37erros. It also requires you to pull it
  8442. 6:03:39out of your computer, which is not
  8443. 6:03:40ideal. So the the latest thing we have
  8444. 6:03:43um is called an EROM. Stands for
  8445. 6:03:46electronically erasable programmable
  8446. 6:03:48readonly memory. So let's build up from
  8447. 6:03:50what we have. We know what a ROM is.
  8448. 6:03:52That's just a basic lookup table.
  8449. 6:03:54Programmable means that we can program
  8450. 6:03:56it ourselves using this programmer tool.
  8451. 6:03:58Erasable means that we are now able to
  8452. 6:04:00clear the values of it um if we
  8453. 6:04:03accidentally put in a different an
  8454. 6:04:04incorrect value or just want to change
  8455. 6:04:06it for our own reasons. Perhaps you want
  8456. 6:04:07to use it in a different computer or
  8457. 6:04:09something. And the last thing is
  8458. 6:04:10electronically erasable. And that means
  8459. 6:04:13that instead of having to pull it out of
  8460. 6:04:15your machine and expose it to UV light
  8461. 6:04:16for 20 minutes, there's simply uh a line
  8462. 6:04:19on this that if you write uh if you put
  8463. 6:04:22one on, it'll clear out the entire
  8464. 6:04:23contents of the ROM. Um so this is
  8465. 6:04:26pretty useful. And
  8466. 6:04:29it turns out um we're going to need this
  8467. 6:04:31in our computer again. So if I come back
  8468. 6:04:34here then all this is to say these logic
  8469. 6:04:38gates are functionally correct and work.
  8470. 6:04:40But we could also just have used a um a
  8471. 6:04:44some form of one of these variants of
  8472. 6:04:46ROM ideally probably just like a EROM
  8473. 6:04:48and uh program this table in uh using by
  8474. 6:04:52burning the the physical values. So I
  8475. 6:04:55literally burn in for this specific
  8476. 6:04:57input 000000. These would be the outputs
  8477. 6:05:00that the ROM should output. Now, I do
  8478. 6:05:02that for all of the uh 10 different
  8479. 6:05:03values. And that way, our wiring would
  8480. 6:05:06be a lot easier. Instead of having all
  8481. 6:05:08this right here, decoders uh feeding
  8482. 6:05:10into all this mess of logic gates, we'd
  8483. 6:05:12have the outputs here feeding into a ROM
  8484. 6:05:14chip, and then the outputs of the ROM
  8485. 6:05:16chip would just feed directly into the
  8486. 6:05:17seven segment display.
  8487. 6:05:19So, that's pretty cool. Um, and that's
  8488. 6:05:22all we'll talk about for ROMs for now,
  8489. 6:05:24but hold on to what we just learned
  8490. 6:05:25because it turns out ROMs are going to
  8491. 6:05:27be the the glue that irons this computer
  8492. 6:05:30together. and lets us go from the human
  8493. 6:05:32having to punch specific control lines
  8494. 6:05:35to the computer knowing what it should
  8495. 6:05:36do when based on some predetermined
  8496. 6:05:39human instruction that exists somewhere
  8497. 6:05:41in the computer's uh computer's mind. Um
  8498. 6:05:45so the last thing I'm going to do here
  8499. 6:05:46is right now this is connected to inputs
  8500. 6:05:49that we're just toggling manually.
  8501. 6:05:52Um I'm going to connect this to the bus
  8502. 6:05:54so we can actually just see what values
  8503. 6:05:56on the bus at any times uh for values
  8504. 6:05:58that are under or equal to 9. But uh I
  8505. 6:06:03will do one other thing where I'm
  8506. 6:06:04actually uh going to hook that into a
  8507. 6:06:06register. So the issue with just
  8508. 6:06:09displaying the values of the bus at any
  8509. 6:06:11time is we saw that when we were moving
  8510. 6:06:13values in between uh RAM and the ALOU uh
  8511. 6:06:16the state of the bus is changing pretty
  8512. 6:06:18constantly. But if we just wanted to
  8513. 6:06:20load something from RAM into the ALOU,
  8514. 6:06:22do an operation, and then see what the
  8515. 6:06:24result of it is on our display, uh it'd
  8516. 6:06:26be easier if we uh could only display
  8517. 6:06:30the value that's on the bus at a
  8518. 6:06:32particular time. So the way we get
  8519. 6:06:33around that is we just have our bus uh
  8520. 6:06:37read in. We build a new register like
  8521. 6:06:38we've done for the A registers here and
  8522. 6:06:40the B register here. We just have
  8523. 6:06:42another register and um it has its own
  8524. 6:06:45control line. So right into the like we
  8525. 6:06:48could say like the display register
  8526. 6:06:50and whenever we pulse that line uh the
  8527. 6:06:53register will latch whatever is on the
  8528. 6:06:55bus and then that value will be locked
  8529. 6:06:57into the display until the next time we
  8530. 6:06:59change it. So that way even if the bus
  8531. 6:07:00starts changing immediately and we just
  8532. 6:07:02want to keep the value there. Um we
  8533. 6:07:04don't have to you know have the user be
  8534. 6:07:07seeing a bunch of intermediate values
  8535. 6:07:09that are effectively nonsensical to
  8536. 6:07:11them. And this would be pretty cool
  8537. 6:07:13because uh you know one of the first
  8538. 6:07:15programs we're going to write for our
  8539. 6:07:16computer is just counting by twos. So
  8540. 6:07:17we'd like to see like 0 2 4 6 8. And
  8541. 6:07:21turns out if we didn't have a way to
  8542. 6:07:23selectively decide when we'd want to
  8543. 6:07:25display the value on the bus to the user
  8544. 6:07:28uh I guess to the human um it'd be a lot
  8545. 6:07:30of gibberish and you would see a lot
  8546. 6:07:31more numbers being displayed than the
  8547. 6:07:33the counting you'd expect even though
  8548. 6:07:35your computer's working. but you just
  8549. 6:07:37wouldn't really know um what to what to
  8550. 6:07:40interpret versus what's a scratch value
  8551. 6:07:42being used temporarily. So, let me go
  8552. 6:07:44ahead and wire that up.
  8553. 6:07:50Okay, so I've pulled the bus down a
  8554. 6:07:52little further, added new a new
  8555. 6:07:54register, 4bit register, and uh the
  8556. 6:07:58clock signal for that register traces
  8557. 6:08:00this wire all the way here, which is now
  8558. 6:08:03w right into the out register. So
  8559. 6:08:05whenever we pulse that from low to high
  8560. 6:08:07uh whatever value is on the bus will get
  8561. 6:08:09latched into this register and will be
  8562. 6:08:11shown onto this display. And I've also
  8563. 6:08:14prepared this by loading uh two values
  8564. 6:08:17into RAM. So at address 0 we have the
  8565. 6:08:20value we have the binary value 000 which
  8566. 6:08:23is two and then at address one uh we
  8567. 6:08:26have the value 00001 which is just one.
  8568. 6:08:30Um so we can do we have two and one. uh
  8569. 6:08:33let's load those from RAM into the ALOU
  8570. 6:08:36and then do the arithmetic and then
  8571. 6:08:38capture the result onto the uh output
  8572. 6:08:41register and expect it to be displayed.
  8573. 6:08:43So what I'll do is I will connect the
  8574. 6:08:46RAM to the bus. Uh let's start with
  8575. 6:08:49address zero. So we can see we have uh
  8576. 6:08:52the value two written onto the bus.
  8577. 6:08:54Let's write that into register A. Now
  8578. 6:08:57let's switch to the second address in
  8579. 6:08:59RAM and let's write that into register
  8580. 6:09:04B.
  8581. 6:09:05And now we can see that we have two in
  8582. 6:09:07this register and one in this register.
  8583. 6:09:10And
  8584. 6:09:12um
  8585. 6:09:15that all looks good.
  8586. 6:09:18And
  8587. 6:09:21let's go ahead and disconnect this
  8588. 6:09:24from the bus.
  8589. 6:09:27and connect these two connect the output
  8590. 6:09:30of the alou. So now we can see the
  8591. 6:09:32output of the uh RAM which was doing 2 +
  8592. 6:09:361 is now three. That's binary 0011.
  8593. 6:09:40And what I'll do oops uh doesn't really
  8594. 6:09:44matter what address we're selecting on
  8595. 6:09:45RAM. What I'll do is I'll pulse this uh
  8596. 6:09:48right to our register. So I'll set the
  8597. 6:09:50clock input on this register from zero
  8598. 6:09:52to one which is triggers a rising edge
  8599. 6:09:55which means the register will latch in
  8600. 6:09:57the input value which is 0011
  8601. 6:09:59corresponding to binary 3 and we'd hope
  8602. 6:10:01to see that on our display. So let's go
  8603. 6:10:04ahead and uh set this high. Congrats. We
  8604. 6:10:09see three. I'll put this down low. So um
  8605. 6:10:14now let's just take this a little bit
  8606. 6:10:16further. So we have the output of three
  8607. 6:10:18onto the bus. Let's let's do uh 3 + 1.
  8608. 6:10:22So we have value three on the bus. Let's
  8609. 6:10:24read that back into the reg the a
  8610. 6:10:26register which has two
  8611. 6:10:30and I can now see four is written on the
  8612. 6:10:32bus. That's 0 1 0 0 which is binary
  8613. 6:10:35four. So if I pulse this flag
  8614. 6:10:38I can now see four is written on the
  8615. 6:10:40bus. If I do the same thing I can keep
  8616. 6:10:43just adding by one. So the steps we take
  8617. 6:10:46now that we've gotten our computer into
  8618. 6:10:47this state is to read the output of the
  8619. 6:10:50ALOU which is on the bus into register
  8620. 6:10:52A. Step one, read into register A. Step
  8621. 6:10:56two is simply just update the out the
  8622. 6:10:58out register.
  8623. 6:11:01And now we're at five. And so I can just
  8624. 6:11:02keep doing these two things.
  8625. 6:11:16And you'll see that the uh the register
  8626. 6:11:18just keeps counting up on the on the
  8627. 6:11:19display. And if I then disconnect the
  8628. 6:11:22output of the ALOU from the bus, you
  8629. 6:11:25know, it doesn't affect our display. Um
  8630. 6:11:26it still just holds on to that that last
  8631. 6:11:28value. So our display is is is kind of
  8632. 6:11:30stateful in a sense. It is a register
  8633. 6:11:33which is then connected to a readonly
  8634. 6:11:35memory that uh forks into a seven
  8635. 6:11:39segment display.
  8636. 6:11:41Cool. Um
  8637. 6:11:43that's all we will do for now and let's
  8638. 6:11:45jump back to my iPad.
  8639. 6:11:49So we're back here and let's diagram
  8640. 6:11:52where we left off uh at at a more
  8641. 6:11:54abstract level rather than showing
  8642. 6:11:55specific logic gs. Um so we can just
  8643. 6:11:58reason at a higher level. So we have
  8644. 6:12:00this bus
  8645. 6:12:06and in the demonstrations we just built
  8646. 6:12:10we had a 4-bit bus. Our computer as I
  8647. 6:12:14said earlier is going to be an 8bit
  8648. 6:12:16computer. The reason I was doing four
  8649. 6:12:18bits in the demo is you could imagine
  8650. 6:12:21that the complexity would just increase
  8651. 6:12:23exponentially every time you add a more
  8652. 6:12:25a new bit. So you know for each register
  8653. 6:12:28there'd be a lot more control lines um
  8654. 6:12:30and RAM would be a lot more complicated.
  8655. 6:12:33So the the issue is for the sake of time
  8656. 6:12:36it made more sense for me to work with
  8657. 6:12:38four bit buses and four-bit values but
  8658. 6:12:41the concepts are still the same for 8
  8659. 6:12:42bits. You just imagine we literally
  8660. 6:12:44extend everything. So all of our
  8661. 6:12:45registers are 8 bits and um
  8662. 6:12:50we will also increase our memory
  8663. 6:12:52addresses for RAM to four bits. But
  8664. 6:12:54let's just uh start with the bus. So
  8665. 6:13:02six
  8666. 6:13:047
  8667. 6:13:068. Okay, so we have eight lines and uh
  8668. 6:13:12this is going to be our bus.
  8669. 6:13:16And so every component will connect to
  8670. 6:13:18the bus from here on out. And the next
  8671. 6:13:21thing we had was the alou. So I'll draw
  8672. 6:13:24that.
  8673. 6:13:33And the inputs to the alou were actually
  8674. 6:13:36two registers. So we'll call this
  8675. 6:13:38register
  8676. 6:13:40A
  8677. 6:13:43and its output fed into the first
  8678. 6:13:45operand of our ALU. and then register B
  8679. 6:13:50was our second operand and it both these
  8680. 6:13:54always just feed straight into the ALOU
  8681. 6:13:56but then they can also read values in
  8682. 6:13:59from the bus. So these are both
  8683. 6:14:02connected to the bus and
  8684. 6:14:08and then we had uh an add subtract line
  8685. 6:14:27and then the output of the ALO wasn't
  8686. 6:14:30directly connected to the bus because
  8687. 6:14:32recall only one thing can be written to
  8688. 6:14:33the bus at a time. Um so it's actually
  8689. 6:14:37going to be backed by something we call
  8690. 6:14:40a tri-state buffer. So a tri-state
  8691. 6:14:43buffer um pretty much in the examples we
  8692. 6:14:46were showing we were using a switch. So
  8693. 6:14:48I'd manually have to click and it would
  8694. 6:14:50either physically disconnect or connect
  8695. 6:14:53uh each bit position of a particular
  8696. 6:14:55component. In our example, that was
  8697. 6:14:57either the ALOU output or the RAM output
  8698. 6:15:00to the bus. Um, rather than us having to
  8699. 6:15:02click, uh, it'd be nice if we could just
  8700. 6:15:04have an input signal and that would
  8701. 6:15:06determine if something was connected or
  8702. 6:15:08not. So, uh, let's see here.
  8703. 6:15:14So, the tri-state buffer is kind of
  8704. 6:15:16denoted, let me move over here to some
  8705. 6:15:18crash base. It's kind of denoted like
  8706. 6:15:20this. It's denoted like this. And I know
  8707. 6:15:22this gets confusing because our inverter
  8708. 6:15:24gate is denoted like this. Uh but that
  8709. 6:15:27that circle kind of makes a world of
  8710. 6:15:28difference. And we have some input A and
  8711. 6:15:33then we have this control line B and
  8712. 6:15:37then the output of that is C. So pretty
  8713. 6:15:40much what we can say is
  8714. 6:15:44if B
  8715. 6:15:47is one and there's obviously inverted
  8716. 6:15:50versions of this. Let's just say if B is
  8717. 6:15:51high then
  8718. 6:15:54then C will equal A. I.e. the value of A
  8719. 6:15:58will just flow on through.
  8720. 6:16:02Then if B
  8721. 6:16:05is zero it does not mean that C is zero.
  8722. 6:16:11It does not mean that C is one. It means
  8723. 6:16:13that C is just disconnected from the
  8724. 6:16:16from the bus.
  8725. 6:16:21So it's not an output of 01. It
  8726. 6:16:23literally means no output, the absence
  8727. 6:16:25of an output. And so that'll physically
  8728. 6:16:27disconnect us in the same way that we
  8729. 6:16:29saw in the software um
  8730. 6:16:33that uh clicking that switch would
  8731. 6:16:34physically d disconnect the wire. So you
  8732. 6:16:37can imagine like it's kind of the same
  8733. 6:16:39thing we saw um
  8734. 6:16:45here. This is what the software looked
  8735. 6:16:46like. And so pretty much whenever we
  8736. 6:16:48have B is being one um then when B is
  8737. 6:16:52one the thing will fall and it'll let
  8738. 6:16:53the it'll let A connect to C and then
  8739. 6:16:56whenever B is zero it'll pick this thing
  8740. 6:16:58up and it'll be physically disconnected
  8741. 6:17:00and that's how we'll guarantee that uh
  8742. 6:17:02only one thing is writing to the bus at
  8743. 6:17:04a time. And this is nice because it now
  8744. 6:17:07adds a new toggle for us. You know we've
  8745. 6:17:09talked about the toggles we have for
  8746. 6:17:11writing values into registers or putting
  8747. 6:17:13something out on the bus. And here's a
  8748. 6:17:15new one we have uh for physically
  8749. 6:17:16connecting or disconnecting us from the
  8750. 6:17:18bus. So, let me go ahead and clean this
  8751. 6:17:21up
  8752. 6:17:26and let's add this in here.
  8753. 6:17:40And so that means we actually have a new
  8754. 6:17:42control line now. And we'll call this
  8755. 6:17:45ALU out.
  8756. 6:17:49And pretty much when this is set, the
  8757. 6:17:51output of the ALU will be written onto
  8758. 6:17:53the bus. And when it's not set, i.e.
  8759. 6:17:55it's zero, it'll be physically
  8760. 6:17:56disconnected. So I'm also going to do
  8761. 6:17:58one other thing here is I'm going to
  8762. 6:17:59start highlighting all of the control
  8763. 6:18:00lines we have because we kind of have a
  8764. 6:18:02good number of them now. So let's just
  8765. 6:18:04uh keep track of this. Uh the add
  8766. 6:18:06subtract one is one we have.
  8767. 6:18:11And then we also have control lines for
  8768. 6:18:18um
  8769. 6:18:21register A in
  8770. 6:18:27register B in.
  8771. 6:18:35So let me highlight those as well.
  8772. 6:18:44Cool. So that's our ALOU. And then after
  8773. 6:18:48we had the ALU, we went and built RAM.
  8774. 6:18:50So we have pretty much a register that
  8775. 6:18:55reads uh an address from the bus. So our
  8776. 6:18:59bus is 8 bits. Uh we're going to make
  8777. 6:19:01our addressing space four bits. So even
  8778. 6:19:04though the bus is an 8- bit bus,
  8779. 6:19:09we will actually say that our memory
  8780. 6:19:10addresses in our computer are all four
  8781. 6:19:13bits. And this is a design choice. We
  8782. 6:19:17are designing this ourselves. We
  8783. 6:19:19actually could make them any number of
  8784. 6:19:20bits. More bits means more address
  8785. 6:19:22spaces, which means that your computer
  8786. 6:19:24can have more working memory. Um, but
  8787. 6:19:27we'll see that when we need to do an
  8788. 6:19:29operation, we need to specify an
  8789. 6:19:31address. And so we are going to get to a
  8790. 6:19:34point where we need to say something
  8791. 6:19:35like load the address from load the
  8792. 6:19:39value from some address in RAM onto the
  8793. 6:19:41bus and we need to encode that signal
  8794. 6:19:44onto the bus itself. So if our address
  8795. 6:19:46was 8 bits then there's no way that we
  8796. 6:19:48could fit in a operation that says to
  8797. 6:19:51read from RAM whatever signal that may
  8798. 6:19:53be however we would like to encode that
  8799. 6:19:55and the actual address. So we decide
  8800. 6:19:57that uh this is just by decision that
  8801. 6:20:00we'll say four bits are going to be for
  8802. 6:20:02addressing and then maybe four bits
  8803. 6:20:05later down the road will be used for
  8804. 6:20:06saying what we want to do with that
  8805. 6:20:08address. Um
  8806. 6:20:11so yeah so we have something we'll build
  8807. 6:20:14a register uh in this register we
  8808. 6:20:17previously in our software
  8809. 6:20:19representation just called it the RAM
  8810. 6:20:21address selector. We're going to give it
  8811. 6:20:23a bit more uh a slightly changed name,
  8812. 6:20:26but the effect is still the same. We're
  8813. 6:20:28now going to call this the memory
  8814. 6:20:30address register. Uh it's short for uh M
  8815. 6:20:35is the acronym.
  8816. 6:20:38And we can just know that it stands for
  8817. 6:20:41memory
  8818. 6:20:44address
  8819. 6:20:46register.
  8820. 6:20:49And so it is a 4bit register.
  8821. 6:20:54So let me actually show that a little
  8822. 6:20:57bit better.
  8823. 6:21:00So I will say a four up here. And all of
  8824. 6:21:03our other registers that we've worked
  8825. 6:21:05with so far are 8 bit registers. So let
  8826. 6:21:08me make that clear too.
  8827. 6:21:13And you know this is an eight bit value.
  8828. 6:21:16The output of the ALU is an 8bit value.
  8829. 6:21:17So it'll write to all four uh all eight
  8830. 6:21:22sorry all eight lines of the bus. The
  8831. 6:21:23memory address register will just read
  8832. 6:21:25in the memory the bits from the
  8833. 6:21:27rightmost four address lines. So it
  8834. 6:21:30won't actually connect to all four of
  8835. 6:21:31the address all eight of the bits uh on
  8836. 6:21:34the bus because it's our memory
  8837. 6:21:36addresses are only four bits. So then
  8838. 6:21:38there's kind of a choice of like which
  8839. 6:21:39four bits are we going to actually use
  8840. 6:21:42on the bus whenever some other component
  8841. 6:21:44wants to put out a memory address for
  8842. 6:21:45RAM to operate on. And just by design,
  8843. 6:21:48again, we could have picked any four
  8844. 6:21:50bits, but uh just by design, we're going
  8845. 6:21:53to go with the rightmost four bits. And
  8846. 6:21:54these are called the least significant
  8847. 6:21:56bits. And the leftmost four bits would
  8848. 6:21:58be the the most significant bits or the
  8849. 6:22:00highest significant bits. So we have our
  8850. 6:22:03memory address register hooked in to the
  8851. 6:22:06least four significant bits of the bus.
  8852. 6:22:09And this is always just reading in. So,
  8853. 6:22:12let me actually uh update that
  8854. 6:22:14representation slightly so it's not
  8855. 6:22:15ambiguous in the future because we're
  8856. 6:22:17going to be adding several more
  8857. 6:22:19components here.
  8858. 6:22:29Uh versus these two registers
  8859. 6:22:34are both reading the full eight bits.
  8860. 6:22:40And let me clean up this right here.
  8861. 6:22:43Cool. And then there's also another uh
  8862. 6:22:46control signal here. You know, the um
  8863. 6:22:50the
  8864. 6:22:51we we're turning this into a register.
  8865. 6:22:53It was just a selector in our original
  8866. 6:22:55thing, but we're now making this encode
  8867. 6:22:57the value in register. And we know that
  8868. 6:22:59for all registers, uh they need some
  8869. 6:23:00control line to determine when they
  8870. 6:23:02should read the value that's on the bus
  8871. 6:23:03into their uh bit positions. So, there's
  8872. 6:23:06a new signal called
  8873. 6:23:09um we'll just call it M in.
  8874. 6:23:18And let me highlight that so we can keep
  8875. 6:23:19track of all of our signals, our control
  8876. 6:23:21lines. And then this memory address RAM
  8877. 6:23:25is always looking into
  8878. 6:23:30what value is here because recall that
  8879. 6:23:35RAM has two operations read and write
  8880. 6:23:37and the only thing uh the for both of
  8881. 6:23:41those operations it needs to work with a
  8882. 6:23:42specific memory address. So you're
  8883. 6:23:44either reading a specific memory address
  8884. 6:23:46or if you're writing a value in you need
  8885. 6:23:48to know which address you're writing
  8886. 6:23:49that into. So RAM has will look at this
  8887. 6:23:52memory address uh register to determine
  8888. 6:23:54what to do and then
  8889. 6:23:57um it'll also need to uh read in from
  8890. 6:24:01the bus directly too. So
  8891. 6:24:08and so RAM when it wants to write a
  8892. 6:24:12value onto the bus we also know that
  8893. 6:24:13happens. um it has uh so we're use the
  8894. 6:24:17same sort of uh tri-state buffer that we
  8895. 6:24:19talked about previously. And so the
  8896. 6:24:21output of RAM
  8897. 6:24:23can be written onto the bus
  8898. 6:24:25conditionally
  8899. 6:24:27based on whether or not this output line
  8900. 6:24:29is set. And so we have an output line
  8901. 6:24:32here and we will call this one RAM out.
  8902. 6:24:36So if it's set, RAM will write its value
  8903. 6:24:38onto the bus. And if it's not set, then
  8904. 6:24:40RAM will not write anything onto the
  8905. 6:24:42bus. It won't write zeros onto the bus.
  8906. 6:24:44literally would be physically
  8907. 6:24:45disconnected from the bus. And so this
  8908. 6:24:47will be enough for reading in values. If
  8909. 6:24:49you wanted to read in a value, uh if you
  8910. 6:24:51wanted to read a value from RAM, you'd
  8911. 6:24:53put that address onto the least four
  8912. 6:24:55significant bits. Then you'd punch this
  8913. 6:24:58uh MAR in and so it would latch it into
  8914. 6:25:01the register. Then RAM's always looking
  8915. 6:25:03at whatever that memory address line is
  8916. 6:25:05and it's trying to write the values
  8917. 6:25:07here. And then we'd punch this RAM out
  8918. 6:25:09and the value would come out onto the
  8919. 6:25:10bus. Uh, and we need to make sure that
  8920. 6:25:13in between us putting the address on the
  8921. 6:25:15bus and us punching this RAM out, uh,
  8922. 6:25:17whatever is writing the address onto the
  8923. 6:25:19bus needs to stop writing it onto the
  8924. 6:25:20bus. Or else we'd have a, uh, short
  8925. 6:25:22circuit because two things will be
  8926. 6:25:24writing onto the bus. The RAM would be
  8927. 6:25:26trying to write its output onto the bus
  8928. 6:25:28and whatever is writing the memory
  8929. 6:25:29address to read is writing onto the bus.
  8930. 6:25:32So that covers reading from RAM. Then
  8931. 6:25:34the last thing is uh, writing into RAM.
  8932. 6:25:38So if we wanted to load a value into RAM
  8933. 6:25:41or write a value into the RAM, we need
  8934. 6:25:43to specify the address. So we'd put the
  8935. 6:25:45address onto the bus, we latch it into
  8936. 6:25:48the memory address register, then we'd
  8937. 6:25:50stop putting the address onto the bus,
  8938. 6:25:52but then we'd put the value that we want
  8939. 6:25:54to write in at that particular address
  8940. 6:25:56onto the bus. And so RAM needs to always
  8941. 6:25:59be listening to what's on the bus. And
  8942. 6:26:02it's RAM operates in our uh in our
  8943. 6:26:05actual computer. It's going to operate
  8944. 6:26:07on eight bit values. So I'll denote this
  8945. 6:26:10with an eight. And so it reads all eight
  8946. 6:26:12bits from the bus. And uh the only thing
  8947. 6:26:16we then need to add was the punch button
  8948. 6:26:18we had that would perform the right
  8949. 6:26:21operation that we saw in our in our
  8950. 6:26:23circuitry. So I'll add one more signal
  8951. 6:26:26called RAM in
  8952. 6:26:30and that is used to write some value
  8953. 6:26:33into RAM. Cool. So we uh started with
  8954. 6:26:36our ALU then we built RAM and the last
  8955. 6:26:38thing we just did was build this uh
  8956. 6:26:40display register. So in our our
  8957. 6:26:43contrived example we had in the digital
  8958. 6:26:45software it looked at four bit values
  8959. 6:26:47and only outputed one number uh one
  8960. 6:26:50digit position. In reality, we're going
  8961. 6:26:52to uh let uh this this uh display
  8962. 6:26:58display any number of values. You
  8963. 6:26:59understand the concept. We're not going
  8964. 6:27:00to go and reprogram that ROM, but you
  8965. 6:27:04can, you know, we uh have a
  8966. 6:27:07register that looks at the entire bus
  8967. 6:27:10and
  8968. 6:27:12that's going to connect to a
  8969. 6:27:21an 8 bit register. So a register
  8970. 6:27:25and then I'll denote it with an eight
  8971. 6:27:28and we're going to call this the uh
  8972. 6:27:31display register um or the output
  8973. 6:27:33register. So
  8974. 6:27:36uh that means that we have a
  8975. 6:27:37corresponding signal here a new line. Um
  8976. 6:27:42we'll call this display
  8977. 6:27:45in. So display register in. So whenever
  8978. 6:27:49that hits a rising edge from 0 to one,
  8979. 6:27:51we will latch in whatever values on the
  8980. 6:27:53bus. And we'll just need to be careful
  8981. 6:27:56to make sure that there there is a bus.
  8982. 6:27:58And then we know from what we just built
  8983. 6:28:00that there would be a ROM that looks at
  8984. 6:28:04this 8bit signal and then outputs some
  8985. 6:28:09number of control lines. And those
  8986. 6:28:12control lines will get consumed by
  8987. 6:28:15several
  8988. 6:28:18uh of these seven segment displays next
  8989. 6:28:20to each other.
  8990. 6:28:29Pardon my bad drawings here.
  8991. 6:28:33And tada. This is kind of what we've
  8992. 6:28:36built uh on the software uh just
  8993. 6:28:39conceptually laid out here. And so let's
  8994. 6:28:42actually now talk about um some of the
  8995. 6:28:44operations our computer is capable of
  8996. 6:28:46doing. So the first one is let's talk
  8997. 6:28:50about how you would load a value um how
  8998. 6:28:54you would read a value from RAM. So to
  8999. 6:28:57read a value from RAM
  9000. 6:28:59we have a few steps we want to take. So
  9001. 6:29:02uh read
  9002. 6:29:05from
  9003. 6:29:07RAM. The first thing we want to do is
  9004. 6:29:10put an address onto the bus. So in order
  9005. 6:29:13to read a particular value
  9006. 6:29:16um onto from RAM, we'd actually need to
  9007. 6:29:19address it. And so we need to get that
  9008. 6:29:21value into the memory address register.
  9009. 6:29:23So the first thing you do, I don't know
  9010. 6:29:25why I denoted this with a one. Um we'll
  9011. 6:29:27just say read from RAM. First thing you
  9012. 6:29:28do is uh put the address on the bus.
  9013. 6:29:36Then the second thing you do is once you
  9014. 6:29:38put the address onto the bus um you'd
  9015. 6:29:41put uh you'd go MAR in uh you'd toggle
  9016. 6:29:45it. So you you know you'd let it go from
  9017. 6:29:47low to high. And then the third thing
  9018. 6:29:50you do is at that point uh the RAM is
  9019. 6:29:53always looking at this memory address
  9020. 6:29:54register. So it' immediately look up the
  9021. 6:29:57value that's in that address and try put
  9022. 6:29:59it right here. But it's blocked on this
  9023. 6:30:01RAM out that's disconnected. So before
  9024. 6:30:03we can put that value of RAM out onto
  9025. 6:30:05the bus, we need to uh stop writing the
  9026. 6:30:08address onto the bus. So stop
  9027. 6:30:12writing
  9028. 6:30:15address
  9029. 6:30:17onto bus. And then finally, we could
  9030. 6:30:21then put RAM out
  9031. 6:30:27uh from low to high.
  9032. 6:30:31And at that point we would uh have
  9033. 6:30:34successfully
  9034. 6:30:37loaded a value from RAM. And now let's
  9035. 6:30:39talk about how we'd write to RAM. So if
  9036. 6:30:43you want to write to RAM,
  9037. 6:30:50the first thing you do is you put the
  9038. 6:30:52address that you want to write into onto
  9039. 6:30:54the bus. So put target
  9040. 6:30:58address
  9041. 6:31:01on bus
  9042. 6:31:03and then mar in toggle. So you want to
  9043. 6:31:06read that into this memory address
  9044. 6:31:08register over here.
  9045. 6:31:15Then now that we've loaded the address,
  9046. 6:31:17we want to stop writing
  9047. 6:31:23the address onto the bus. It's looking
  9048. 6:31:25pretty similar to the read so far.
  9049. 6:31:29And now we need to put the actual value
  9050. 6:31:32that we want to write into the address
  9051. 6:31:34that's now captured here onto the bus.
  9052. 6:31:36So since we've captured the address into
  9053. 6:31:38the memory address register, it's not
  9054. 6:31:40imperative if it's not imperative that
  9055. 6:31:42we keep that value on the bus, which is
  9056. 6:31:44why how we actually free up the bus to
  9057. 6:31:46then hold the 8 bit value that is the
  9058. 6:31:49value that we'd like to write into that
  9059. 6:31:51particular address stored in the M. So
  9060. 6:31:54now we can say put value on bus
  9061. 6:32:00and then once we've done that the only
  9062. 6:32:02thing we need to do is pulse this RAM in
  9063. 6:32:05signal and that'll latch the value into
  9064. 6:32:07the corresponding register. So RAM in
  9065. 6:32:11would need to go from low to high and
  9066. 6:32:14just like that we've uh written to RAM.
  9067. 6:32:17So cool.
  9068. 6:32:20And so what I'd like to do now is
  9069. 6:32:22actually delete everything we just drew
  9070. 6:32:24over the past 10 minutes.
  9071. 6:32:33And let's actually start getting back to
  9072. 6:32:35the concrete part of this
  9073. 6:32:36implementation. And what we're going to
  9074. 6:32:38do for the remainder part of this
  9075. 6:32:41segment of finishing off the CPU is
  9076. 6:32:44tying things together. And it may not
  9077. 6:32:46seem like we're done yet. And it's
  9078. 6:32:48because we're not. we're kind of in this
  9079. 6:32:49weird state where we have memory and an
  9080. 6:32:51alou, but like no way to really tie it
  9081. 6:32:53together or understanding of like why
  9082. 6:32:55we'd want to tie it together. Um, so up
  9083. 6:32:59until this point, like we started off
  9084. 6:33:01with with binary and like all of that
  9085. 6:33:03logic clearly stacked on top of each
  9086. 6:33:05other on top of each other until we got
  9087. 6:33:07to the point of a register. Then when we
  9088. 6:33:09got past the register, we kind of had to
  9089. 6:33:11split where we like went and built this
  9090. 6:33:12ALOU, then we went and built this RAM,
  9091. 6:33:14and then now we kind of recently tied
  9092. 6:33:16those concepts back together. And
  9093. 6:33:20unfortunately to bring this all together
  9094. 6:33:21as a whole requires us to go into some
  9095. 6:33:24ambiguity. It can't clearly stack. there
  9096. 6:33:26kind of has to be some amount of uh take
  9097. 6:33:29this for what it's worth and then when I
  9098. 6:33:31can tie components back together it'll
  9099. 6:33:33make sense in the end but I I think
  9100. 6:33:35there's a way that we can do this uh
  9101. 6:33:38that minimizes the number of head
  9102. 6:33:40scratching that happens along the way so
  9103. 6:33:43um what we we described some possible
  9104. 6:33:46operations let's take one of those and
  9105. 6:33:48try to build it out a little bit so what
  9106. 6:33:51we're going to say now is uh we're going
  9107. 6:33:54to build a lot of operations ations um
  9108. 6:33:57commands we can call them for our
  9109. 6:33:59computer and each of these commands is
  9110. 6:34:01going to do something very small. Maybe
  9111. 6:34:03load a value from RAM into a register uh
  9112. 6:34:06can be one and then maybe one can be uh
  9113. 6:34:10doing an addition and writing that value
  9114. 6:34:11back into RAM can be another instruction
  9115. 6:34:14or maybe one instructions taking what's
  9116. 6:34:16in a register and writing it onto our
  9117. 6:34:18seven segment display. Um and the idea
  9118. 6:34:22is if we have these different
  9119. 6:34:23instructions defined and the computer
  9120. 6:34:26knows how to implement each of those
  9121. 6:34:28instructions i.e it knows actually what
  9122. 6:34:30to do which of these lines to punch at
  9123. 6:34:32which time and in what order then us as
  9124. 6:34:36humans can create programs by stacking
  9125. 6:34:39these instructions together which is
  9126. 6:34:41pretty cool. So, what I'm going to do is
  9127. 6:34:46excuse me there. Um, we're going to
  9128. 6:34:49create another register. Yes, one more.
  9129. 6:34:51And we're going to call this the
  9130. 6:34:52instruction register.
  9131. 6:35:05And for now, don't worry about uh how a
  9132. 6:35:09value gets into this register. But let's
  9133. 6:35:12just say that this register
  9134. 6:35:15uh tells us what we what the what
  9135. 6:35:17instruction our computer should do next.
  9136. 6:35:21And then let's suppose and I'm going to
  9137. 6:35:24do this actually in a different color
  9138. 6:35:25because this is is is conceptual. Um
  9139. 6:35:34there's some part of the computer that
  9140. 6:35:36actually looks at what's in this
  9141. 6:35:38instruction register and it's the brains
  9142. 6:35:41uh we can think of it as the brains that
  9143. 6:35:43will actually go and punch each of these
  9144. 6:35:46other lines that go and do something at
  9145. 6:35:49different times.
  9146. 6:35:51Um and we're going to finally make sense
  9147. 6:35:52of how that looks like really shortly.
  9148. 6:35:54I.e. now. So uh our instruction register
  9149. 6:35:58is going to be an 8 bit register
  9150. 6:36:01and here's the format of the instruction
  9151. 6:36:04register. So let's uh let's come over
  9152. 6:36:05here.
  9153. 6:36:12The instruction register it's an 8 bit
  9154. 6:36:14register
  9155. 6:36:17and we're going to split it into two
  9156. 6:36:19components. So it'll be one, two, three,
  9157. 6:36:22four, five, six, seven, eight. We're
  9158. 6:36:24gonna split it down the middle. So the
  9159. 6:36:27the first four bits, the left four bits
  9160. 6:36:30are going to denote the uh instruction.
  9161. 6:36:36And then for some instructions, we might
  9162. 6:36:39have an argument that we need to pass
  9163. 6:36:40in. And so we'll use the last four bits
  9164. 6:36:43for denoting the argument.
  9165. 6:36:48So the first instruction we're going to
  9166. 6:36:51build, so this is our instruction
  9167. 6:36:52register. our in we're gonna build a
  9168. 6:36:55table of instructions
  9169. 6:37:01and
  9170. 6:37:02um basically what I'm saying is
  9171. 6:37:08we have four bits to note what
  9172. 6:37:10instruction we're going to execute which
  9173. 6:37:12means we have two to the four different
  9174. 6:37:14values can fit into that instruction uh
  9175. 6:37:17instruction part of the instruction
  9176. 6:37:18register which means we have 16
  9177. 6:37:23different instructions.
  9178. 6:37:26That's how many instructions we'll build
  9179. 6:37:28up to. That's as many as our computer
  9180. 6:37:30supports. You'd imagine if this
  9181. 6:37:32instruction register was more than 8
  9182. 6:37:34bits, we could have a lot more
  9183. 6:37:36instructions. For example, if it was a 9
  9184. 6:37:38bit register and we put an extra bit
  9185. 6:37:40towards that instruction component, we
  9186. 6:37:42could have 32 uh 32 different
  9187. 6:37:44instructions. For this case, we'll start
  9188. 6:37:46with uh 16 different instructions. and
  9189. 6:37:48we will talk a little bit about how this
  9190. 6:37:50computer scales up to like more modern
  9191. 6:37:51computer. Um, and we're going to build
  9192. 6:37:54out up to 16 different instructions. It
  9193. 6:37:56turns out we don't even actually need to
  9194. 6:37:57use all of these to to get a functioning
  9195. 6:37:59computer. Um, and then argument. So,
  9196. 6:38:03what actually do we mean by argument?
  9197. 6:38:05So, let's actually figure out what this
  9198. 6:38:06means just by building our first
  9199. 6:38:08instruction. So, I'm going to define
  9200. 6:38:09what our first instruction is. Our first
  9201. 6:38:12instruction
  9202. 6:38:13is going to be called load a
  9203. 6:38:20oops
  9204. 6:38:21lda. Um let's actually put this into
  9205. 6:38:24table format. So I will say short name,
  9206. 6:38:34full name,
  9207. 6:38:40op code,
  9208. 6:38:44which I'll explain in a second, and then
  9209. 6:38:47we'll give it a description.
  9210. 6:38:53So basically um instruction is
  9211. 6:38:57synonymous with op code and then for any
  9212. 6:39:01instruction that has an argument we call
  9213. 6:39:03it the operand
  9214. 6:39:06or it can be also just called arg for
  9215. 6:39:10short. You'll hear these used
  9216. 6:39:12interchangeably. Um so let's let's
  9217. 6:39:16define our our first instruction that
  9218. 6:39:18we're going to implement from scratch is
  9219. 6:39:19called load A. Short name is just LDA.
  9220. 6:39:23Its full name is just load A. These are
  9221. 6:39:27invented by us. This is the point where
  9222. 6:39:29we are just inventing these
  9223. 6:39:30instructions. Different computers have
  9224. 6:39:32different instructions and that's
  9225. 6:39:33something we'll also talk about and is
  9226. 6:39:35really interesting. Our simple computer
  9227. 6:39:37we're building right here has just
  9228. 6:39:39enough instructions to show off the
  9229. 6:39:41fundamentals of a computer and write non
  9230. 6:39:44or I guess you could say non-trivial
  9231. 6:39:46programs uh that the computer can
  9232. 6:39:48operate. Our op code, let's just give it
  9233. 6:39:51the number one for now. 00001.
  9234. 6:39:54And what load A does is it is going to
  9235. 6:39:59load into the register A of our
  9236. 6:40:02computer. Recall register A is this uh
  9237. 6:40:05left hand side of the operand
  9238. 6:40:10and what it's going to load into A is
  9239. 6:40:15RAM
  9240. 6:40:18at the address specified by the ARG.
  9241. 6:40:24So if you've programmed before, you know
  9242. 6:40:25those uh these these brackets uh are
  9243. 6:40:29kind of indexing into a structure. I'm
  9244. 6:40:32making this video for people who have
  9245. 6:40:33not programmed before. So what the
  9246. 6:40:36brackets just mean is it means go to
  9247. 6:40:39whatever's left of the brackets. So
  9248. 6:40:41that's RAM and then this position in
  9249. 6:40:45RAM. So it's saying pretty much whatever
  9250. 6:40:48the argument is, if the argument is two,
  9251. 6:40:51whatever the second value is at RAM,
  9252. 6:40:52whatever RAM at address value two is,
  9253. 6:40:55take that, load that from RAM and spit
  9254. 6:40:57it into register A. So why do we care
  9255. 6:41:00about this instruction? Well, in order
  9256. 6:41:03for us to addition, we're going to want
  9257. 6:41:05a value in register A and B. So the very
  9258. 6:41:08first thing we need to do is get a value
  9259. 6:41:09in register A. Um, and we're going to
  9260. 6:41:12say that that value is probably going to
  9261. 6:41:14live somewhere in RAM. So what this
  9262. 6:41:16instruction will do is take some value
  9263. 6:41:18in RAM, throw it on the bus, and then
  9264. 6:41:21register A will need to at the right
  9265. 6:41:23time read in that value from the bus.
  9266. 6:41:33And so
  9267. 6:41:35if I throw in O1,
  9268. 6:41:40then the four bits that we have
  9269. 6:41:42remaining are the operand, the argument,
  9270. 6:41:45and that's going to tell us which
  9271. 6:41:46address from RAM we actually want to
  9272. 6:41:49load into register A. And so I have four
  9273. 6:41:52bits to do that. But what's really
  9274. 6:41:55interesting is that when we designed
  9275. 6:41:57RAM, I also said that the memory address
  9276. 6:42:00register is going to be four bits, which
  9277. 6:42:03means those four bits, even though the
  9278. 6:42:05rest of our computer is centrally
  9279. 6:42:06designed to be eight bits, those four
  9280. 6:42:08bits are enough to fully index every
  9281. 6:42:11spot in RAM. And so I know when I
  9282. 6:42:13earlier I designed RAM and I said, "Hey,
  9283. 6:42:15it's kind of arbitrary. We're making uh
  9284. 6:42:17the memory addresses four bits in RAM."
  9285. 6:42:19It actually works out perfectly because
  9286. 6:42:22it means for all of our instructions
  9287. 6:42:23that we build here on out that need to
  9288. 6:42:26reference a specific address in RAM,
  9289. 6:42:28they can do so using those four bits. So
  9290. 6:42:31our instructions can be fully complete.
  9291. 6:42:33Um, which is also really nice. And it
  9292. 6:42:37we'll see for the most part that uh our
  9293. 6:42:40instructions usually if they have
  9294. 6:42:42operands, arguments, they're going to be
  9295. 6:42:44addresses into memory. But that's not
  9296. 6:42:47always the case. But uh it's designed
  9297. 6:42:49around this. So there is something it's
  9298. 6:42:52not just coincidence that we're
  9299. 6:42:53designing
  9300. 6:42:55our uh instructions to use four bits for
  9301. 6:42:58arguments and then the RAM has four-bit
  9302. 6:43:01addresses. This is not coincidence. This
  9303. 6:43:03is very much a a choice made with high
  9304. 6:43:06agency. And this is again something that
  9305. 6:43:09we designed for our own computer. So you
  9306. 6:43:11could imagine, you know, maybe if we had
  9307. 6:43:13a a 20 bit uh bus, maybe we'd have like
  9308. 6:43:1810 bits uh could be used for the memory
  9309. 6:43:20address register, which means our our
  9310. 6:43:22instructions might be 10 bit operands
  9311. 6:43:25and then maybe four bit instructions.
  9312. 6:43:29Um so there that there is something
  9313. 6:43:30pretty cool to be said about this. So uh
  9314. 6:43:33basically if we throw in this value,
  9315. 6:43:37let's let's try consider an actual
  9316. 6:43:39value.
  9317. 6:43:41If we throw into the instruction
  9318. 6:43:43register the value 01
  9319. 6:43:47010
  9320. 6:43:49um a few hours ago this would have
  9321. 6:43:52looked like nonsense to you. Uh maybe
  9322. 6:43:55one hour ago you would be able to derive
  9323. 6:43:57the number out of this and now we're
  9324. 6:43:59actually saying hey this this isn't just
  9325. 6:44:01a number in binary that we want to
  9326. 6:44:02convert to decimal. There's more to it
  9327. 6:44:05than this. This is actually first off
  9328. 6:44:07it's it's two different numbers and the
  9329. 6:44:10two numbers are not just one and two. Uh
  9330. 6:44:13there there's actually it's an
  9331. 6:44:14instruction. We're saying execute some
  9332. 6:44:18instruction whose ID is one and that
  9333. 6:44:22instruction takes an argument and the
  9334. 6:44:23value to that argument is two. So if I
  9335. 6:44:26come back over here uh to our scratch
  9336. 6:44:29base um I have 00001 0 0 1 0 was what I
  9337. 6:44:35talked about. So this means load a we're
  9338. 6:44:39saying load a value at this address in
  9339. 6:44:43RAM into register A and this is address
  9340. 6:44:47two
  9341. 6:44:48um
  9342. 6:44:500 1 2 uh however you want to denote that
  9343. 6:44:53third index position or just two um if
  9344. 6:44:58you want to convert that from a binary
  9345. 6:44:59number back to a decimal number. So,
  9346. 6:45:01let's go ahead and see the exact
  9347. 6:45:04specific steps we need
  9348. 6:45:11to make this instruction happen. So, I'm
  9349. 6:45:14going to go a little bit slow here. You
  9350. 6:45:15might be able to figure this out faster
  9351. 6:45:16on your own, but
  9352. 6:45:20the first thing we're going to want to
  9353. 6:45:22do
  9354. 6:45:24is
  9355. 6:45:25get this 0010
  9356. 6:45:29and look up whatever value there is in
  9357. 6:45:30RAM.
  9358. 6:45:32So this instruction register has the
  9359. 6:45:35values that we are interested in. And so
  9360. 6:45:38in order for us to get it to the memory
  9361. 6:45:40address register, this instruction
  9362. 6:45:42register actually has to be able to
  9363. 6:45:44write its value onto RAM. So it can do
  9364. 6:45:48that. And
  9365. 6:45:53I will denote that here. There's
  9366. 6:45:55something interesting. Um, it only needs
  9367. 6:45:58to write
  9368. 6:46:00those right for bits out. It only it
  9369. 6:46:02only needs to write its argument onto
  9370. 6:46:03the bus. It does not need to write all
  9371. 6:46:06eight bits onto the bus.
  9372. 6:46:08Because the uh the left four bits are
  9373. 6:46:11the operation code, the op code of the
  9374. 6:46:13instruction that we're trying to to uh
  9375. 6:46:15in uh that we're hoping the computer
  9376. 6:46:17will execute. And the control is the
  9377. 6:46:20only part of this computer that makes
  9378. 6:46:22sense of the op code, the instruction
  9379. 6:46:23code. The only the control part of our
  9380. 6:46:26computer, which for now is just us as a
  9381. 6:46:28human brain. It's our we are the control
  9382. 6:46:30of the computer. um
  9383. 6:46:34we care about that op code because it
  9384. 6:46:36tells us what we should be doing. No
  9385. 6:46:37other part of the computer cares about
  9386. 6:46:38it. So uh when the instruction register
  9387. 6:46:41writes its value onto the bus, it only
  9388. 6:46:43would ever need to write the address
  9389. 6:46:45onto the bus. And you can kind of see
  9390. 6:46:47that motivated in the case here where if
  9391. 6:46:49we have this uh 0000 one 0 one 0 uh we
  9392. 6:46:54only need these to end up in the memory
  9393. 6:46:57address register. So
  9394. 6:46:59uh that's why we will have that and
  9395. 6:47:01that'll actually be a new signal um and
  9396. 6:47:05we will call that uh
  9397. 6:47:09instruction register out.
  9398. 6:47:16So assuming uh let's say our steps to
  9399. 6:47:20implement this let's let's talk this out
  9400. 6:47:22very clearly.
  9401. 6:47:27So our our step zero and slash our
  9402. 6:47:30assumption
  9403. 6:47:33um is that we are we are assuming the
  9404. 6:47:36instruction register
  9405. 6:47:38has the right
  9406. 6:47:42command in it.
  9407. 6:47:47So we aren't going to worry about how
  9408. 6:47:49this 0000 one 0 0 1 0 got into the
  9409. 6:47:52instruction register yet. We'll get
  9410. 6:47:54there in a bit. Um let's just assume for
  9411. 6:47:56now that we some human came and punched
  9412. 6:47:59that value into the instruction
  9413. 6:48:00register. So that's our assumption. So
  9414. 6:48:04now let's actually walk through the
  9415. 6:48:05specific steps. So the first thing we
  9416. 6:48:07need to do is put the instruction
  9417. 6:48:10register argument onto the bus at 00 1
  9418. 6:48:140. So we'd set instruction register out
  9419. 6:48:21must equal one. And then
  9420. 6:48:25after that after that instruction
  9421. 6:48:28register out is set to one, the value of
  9422. 6:48:32the argument 00 1 0 will be on the bus
  9423. 6:48:34and we need to let these this m in line
  9424. 6:48:38go from 0 to one. So it's everything
  9425. 6:48:40we'll assume is zero. We want that M
  9426. 6:48:43inline to go from zero to one. And what
  9427. 6:48:47that'll do is uh latch in the uh the
  9428. 6:48:52value um
  9429. 6:48:56from the bus into RAM. So we'd say
  9430. 6:49:01m a r in
  9431. 6:49:06goes to one. So it's it's interesting
  9432. 6:49:08because there's a strict ordering
  9433. 6:49:10requirement here.
  9434. 6:49:12um the instruction register has to put
  9435. 6:49:13its value onto the bus before the memory
  9436. 6:49:15address register can read it in. And
  9437. 6:49:17that might seem like a trivial thing,
  9438. 6:49:19but this is actually really important
  9439. 6:49:20and we're going to have to make sure
  9440. 6:49:21that we're very careful about that.
  9441. 6:49:26In general, uh we've talked about this
  9442. 6:49:28already. It seems trivial, but I'll
  9443. 6:49:31restate it again because I think it
  9444. 6:49:32can't be understated. uh if something
  9445. 6:49:35wants to read a value from the bus then
  9446. 6:49:37the component that it's reading the
  9447. 6:49:39value from has to have that have to have
  9448. 6:49:41has to have its value already written
  9449. 6:49:44onto the bus
  9450. 6:49:46uh before the another component can read
  9451. 6:49:47it in. Um so we we kind of view this
  9452. 6:49:51ideally this is atomic
  9453. 6:49:56in reality has to be sequential. So
  9454. 6:49:59ideally we'd say, hey, at the same time
  9455. 6:50:01that we write the instruction registers
  9456. 6:50:02on the on its value onto the bus, the
  9457. 6:50:04memory address registers to read it in.
  9458. 6:50:06Um, and that's how we'd like to think
  9459. 6:50:08about this. That's how we'd like to
  9460. 6:50:09think of putting a value from one
  9461. 6:50:10component to another. Uh, in reality, we
  9462. 6:50:13know we'll have to do it sequentially.
  9463. 6:50:16And so we'll also come back. I'm going
  9464. 6:50:17to leave this written on the screen. Um,
  9465. 6:50:21and I'll also highlight it so we don't
  9466. 6:50:24forget to come back to it in a timely
  9467. 6:50:25manner.
  9468. 6:50:28So once that happens the value from RAM
  9469. 6:50:31is in. So then what I can do is we can
  9470. 6:50:34say okay well now the value uh from the
  9471. 6:50:38instruction register well the
  9472. 6:50:39instruction register outline
  9473. 6:50:42and the mar inline are both not
  9474. 6:50:45necessary anymore. So they can go back
  9475. 6:50:47to zero. We can reset those components
  9476. 6:50:48to back to what they were. And so this
  9477. 6:50:50means nothing's writing onto the bus.
  9478. 6:50:53Um, and it means we've s we've loaded
  9479. 6:50:55our value. Uh, the 0010 has made its way
  9480. 6:51:00into the memory address register. And
  9481. 6:51:02what that means is that RAM has looked
  9482. 6:51:05up that value because RAM is always
  9483. 6:51:07looking at what's in the contents of the
  9484. 6:51:08memory address register and it has the
  9485. 6:51:11output right here. So then what we'd
  9486. 6:51:13like to do is write that value onto the
  9487. 6:51:16bus.
  9488. 6:51:17And then to get it into register A, we
  9489. 6:51:19can use our register A in line.
  9490. 6:51:22So we'd like to transport the value from
  9491. 6:51:25RAM onto the bus into register A. So the
  9492. 6:51:29two control lines we use to do that are
  9493. 6:51:31RAM out. RAM out will put the value onto
  9494. 6:51:34the bus and then after the value is
  9495. 6:51:36written onto the bus, register A can
  9496. 6:51:39read it in.
  9497. 6:51:43So I will say RAM
  9498. 6:51:46out
  9499. 6:51:48and then
  9500. 6:51:51register register A in. So RAM out
  9501. 6:51:56equals 1 and register A equals 1. And
  9502. 6:51:58then we kind of the same thing where
  9503. 6:52:00ideally these are atomic. Um but it's
  9504. 6:52:03important because the moment we set this
  9505. 6:52:05RAM outline
  9506. 6:52:08it connects it to bus to the bus and its
  9507. 6:52:10values will be on the bus and then only
  9508. 6:52:13based on what we know about registers
  9509. 6:52:15only when we set a in from low to high
  9510. 6:52:19only at the exact moment that rising
  9511. 6:52:21edge happens are we actually going to
  9512. 6:52:23latch in a value to register a.
  9513. 6:52:26So
  9514. 6:52:28once that's done, the value uh that we
  9515. 6:52:32wanted to get into register A is done.
  9516. 6:52:35And so we've kind of fulfilled this
  9517. 6:52:36contract right here. Register A now
  9518. 6:52:38equals whatever was in RAM at the
  9519. 6:52:40address specified by the argument to the
  9520. 6:52:42instruction, which in our hypothetical
  9521. 6:52:44example is uh 00 1 0. And so once that's
  9522. 6:52:48done, I can now say uh we can reset both
  9523. 6:52:52those lines as well.
  9524. 6:53:02Okay. So, uh this is pretty interesting.
  9525. 6:53:05Um that doesn't seem that complicated,
  9526. 6:53:07but let's look at it at a slightly
  9527. 6:53:09different view. Uh where instead of
  9528. 6:53:11listing it out in a series of steps,
  9529. 6:53:14let's um look at it in terms of clock
  9530. 6:53:17edges. So
  9531. 6:53:22what I want to do is we said
  9532. 6:53:26um
  9533. 6:53:28we can have all the signals we used
  9534. 6:53:30here. So IR out,
  9535. 6:53:34M A R in,
  9536. 6:53:38then RAM out, and register
  9537. 6:53:44A in.
  9538. 6:53:46And each of these starts off at low. And
  9539. 6:53:51then to execute the instructions, they
  9540. 6:53:53said the first thing that needs to
  9541. 6:53:54happen is instruction register needs to
  9542. 6:53:57write its value out. So it needs to go
  9543. 6:53:59from zero to one and then at some point
  9544. 6:54:03after uh it goes from one uh this memory
  9545. 6:54:07address register can also go from zero
  9546. 6:54:10to one. And because this is after this
  9547. 6:54:14we know that the value on the bus will
  9548. 6:54:15be correct. And then we said okay now
  9549. 6:54:18that that's done uh both of these values
  9550. 6:54:21can can kind of at the same time we can
  9551. 6:54:24reset them to zero and that continues.
  9552. 6:54:26And then the whole time this register
  9553. 6:54:29this RAM out and a in lines are off. And
  9554. 6:54:32then now that the value uh is the
  9555. 6:54:36address has been loaded into MAR, we we
  9556. 6:54:38can we know the value is looked up in
  9557. 6:54:40RAM. So we wanted to get the value from
  9558. 6:54:42RAM's output into register A. So what we
  9559. 6:54:45did was we pulsed on this RAM out kept
  9560. 6:54:49it up and then at some point shortly
  9561. 6:54:51after that we can uh punch register A in
  9562. 6:54:53and that'll latch in the value and then
  9563. 6:54:57uh once that was done our operation was
  9564. 6:54:59done so we just reset both these lines
  9565. 6:55:01to zero and I can carry these because
  9566. 6:55:04these top two lines nothing changes so
  9567. 6:55:06you can kind of see like there's there's
  9568. 6:55:09uh two timing signals of interest here
  9569. 6:55:12um And in order for a like this
  9570. 6:55:16represents a transaction of information
  9571. 6:55:18from one component to another via the
  9572. 6:55:20bus and this represents another
  9573. 6:55:22transaction of information from one
  9574. 6:55:24component to another via the bus. And
  9575. 6:55:27what we'll find I'll spill this out is
  9576. 6:55:29as we build more of these instructions a
  9577. 6:55:32lot of the implementation is going to
  9578. 6:55:34just be moving data from one component
  9579. 6:55:36to another using the bus. So,
  9580. 6:55:40what actually turns out happens is we're
  9581. 6:55:44going to introduce a another component
  9582. 6:55:46to all of this called the clock. And I'm
  9583. 6:55:50going to draw the clock up here.
  9584. 6:55:56And basically what the clock is doing,
  9585. 6:56:00it's a physical uh component to the
  9586. 6:56:02computer. And it's uh you don't need to
  9587. 6:56:06worry about how it build how it's built,
  9588. 6:56:08but at a fixed rate, it'll just keep
  9589. 6:56:10going from 0 to one back to zero.
  9590. 6:56:14And
  9591. 6:56:16this is pretty cool. I'm not drawing
  9592. 6:56:18this, so it's fixed rate, but uh ideally
  9593. 6:56:21this is a fixed rate. And the same way
  9594. 6:56:24every component's kind of hooked in uh
  9595. 6:56:27to the bus, uh the clock is also pretty
  9596. 6:56:30central to the computer. even though
  9597. 6:56:31I've draw I've drawn it a little bit a
  9598. 6:56:34little far out here.
  9599. 6:56:36Um, but basically what we saw here is uh
  9600. 6:56:43is is something similar to this. We had
  9601. 6:56:45components you know we had a a a zero to
  9602. 6:56:48a one here and then one to zero here and
  9603. 6:56:51then a zero to one of interest here and
  9604. 6:56:53a zero to one of interest here. So, uh,
  9605. 6:56:57let's see how we can actually use the
  9606. 6:56:59clock to, um, aid in some of what we
  9607. 6:57:03just did manually.
  9608. 6:57:07So, it turns out there's something
  9609. 6:57:10slightly interesting
  9610. 6:57:12about what we did here, and that when
  9611. 6:57:15you're writing a value onto the bus, you
  9612. 6:57:17can do it whenever because we're kind of
  9613. 6:57:20making this guarantee that when we
  9614. 6:57:21start, nothing is writing onto the bus.
  9615. 6:57:23And then we're also making guarantee
  9616. 6:57:24that like when we are done riding onto
  9617. 6:57:26the bus, we stop writing on before we
  9618. 6:57:27start putting the next thing on. There's
  9619. 6:57:29clearly this nonzero gap here. So to
  9620. 6:57:33write a value onto the bus is safe to
  9621. 6:57:36read a value in from the bus is when
  9622. 6:57:38things get tricky because we want to
  9623. 6:57:39make sure the value is written out onto
  9624. 6:57:41the bus before. So pretty much this
  9625. 6:57:45clock here. And then the other thing is
  9626. 6:57:47when we do need to read in, we can only
  9627. 6:57:48read in on these rising clock edges. So
  9628. 6:57:53uh what happens is you know when the
  9629. 6:57:55clock is low we can put in we can put
  9630. 6:57:58the value out on the bus any time from
  9631. 6:58:00here to here. Let's say like from here
  9632. 6:58:04to like right before anytime here it's
  9633. 6:58:06fair game to read out to the bus. And
  9634. 6:58:09then at the exact moment we go from 0 to
  9635. 6:58:12one, we want to feed in.
  9636. 6:58:15But um we can take this a step further
  9637. 6:58:19and actually say well it'd be nice to
  9638. 6:58:20just throw the in signal and the out
  9639. 6:58:23signal at the same time. Uh because
  9640. 6:58:25that's kind of what we want to do. We
  9641. 6:58:27know we want to write from component A
  9642. 6:58:30out to onto the bus and then have some
  9643. 6:58:32component B latch that value in. So uh
  9644. 6:58:35but you know the issue is we can't do
  9645. 6:58:37that at the exact same time because of
  9646. 6:58:39the issue we described. you know there's
  9647. 6:58:41uh it's unclear if the value would be on
  9648. 6:58:43the bus at the exact time that the
  9649. 6:58:45register latches it in. But if we
  9650. 6:58:48actually uh let's take the example of
  9651. 6:58:50this M in uh if we actually and this
  9652. 6:58:54signal with the clock
  9653. 6:59:01and let that be the signal that goes
  9654. 6:59:02into the register then it turns out we
  9655. 6:59:05can throw both these values. So we can
  9656. 6:59:07throw both um the out and the in to both
  9657. 6:59:13equal one here. And so the out still
  9658. 6:59:16goes onto the bus. The value goes onto
  9659. 6:59:18the bus totally fine. In our in our case
  9660. 6:59:20that was um the instruction register its
  9661. 6:59:23last for this 000 goes out to the bus
  9662. 6:59:26just fine. And the in because it's ended
  9663. 6:59:30with uh this this clock signal stays as
  9664. 6:59:33a zero. And that stays as a zero until
  9665. 6:59:35the exact moment we hit this rising
  9666. 6:59:37edge. And at that point this input uh
  9667. 6:59:41both inputs of this endgate become a
  9668. 6:59:42one. And so we latch in at this exact
  9669. 6:59:44moment the uh the
  9670. 6:59:50rising um signal. We latch in the exact
  9671. 6:59:53value that's on the bus. So we've
  9672. 6:59:55accomplished what we want. So then now
  9673. 6:59:57when we're up here uh we can just you
  9674. 6:59:59know be sure that we uh unset these
  9675. 7:00:02signals. So we can, you know, make sure
  9676. 7:00:04that we put both them back to zero. And
  9677. 7:00:07then when we get down here, we can do
  9678. 7:00:10the same thing for uh RAM out and
  9679. 7:00:13register A in. And we could set both of
  9680. 7:00:16these to one. So out and in, RAM out and
  9681. 7:00:21register A in can both equal one. And as
  9682. 7:00:23long as we have register A's in signal
  9683. 7:00:27anded with the clock,
  9684. 7:00:31then we can take advantage of the same
  9685. 7:00:32trick. So let me go ahead and for all of
  9686. 7:00:35our in signals, let me go ahead and and
  9687. 7:00:39them with the clock so that we can make
  9688. 7:00:41sure that's apparent for the rest of
  9689. 7:00:43this.
  9690. 7:00:45Okay, so I've gone ahead and for each of
  9691. 7:00:48our in signals, I've ended them with the
  9692. 7:00:50clock. So for our memory address
  9693. 7:00:52register reading into RAM, reading into
  9694. 7:00:55the A register, reading into the B
  9695. 7:00:58register
  9696. 7:01:00or reading into the display register
  9697. 7:01:04and
  9698. 7:01:06all of these are now ended with the
  9699. 7:01:08clock. So what this means is our clock
  9700. 7:01:11signal uh we can also pay attention to
  9701. 7:01:15that in our control brains and us as
  9702. 7:01:18humans as we're punching these lines we
  9703. 7:01:20can kind of do is react to the fact that
  9704. 7:01:23the clock is going up and down and being
  9705. 7:01:26sure that we set lines appropriately. So
  9706. 7:01:29if I jump back to our original example
  9707. 7:01:31here of how we constructed this load a
  9708. 7:01:33operation. Let's say we have our clock
  9709. 7:01:36and it's just always going from low to
  9710. 7:01:38high.
  9711. 7:01:41Then basically what needs to happen is
  9712. 7:01:45while the clock is low, we need to set
  9713. 7:01:48IR out um equals to one.
  9714. 7:01:53Let me
  9715. 7:01:57and m a r in equal to one. And then we
  9716. 7:02:02aren't changing the clock. the clock
  9717. 7:02:04just exists and change its value on its
  9718. 7:02:06own. But at some point, the clock will
  9719. 7:02:08go from zero to one. And that'll have
  9720. 7:02:10the effect of actually latching in the
  9721. 7:02:12value into the memory address register
  9722. 7:02:14because
  9723. 7:02:16the me the the line for the memory
  9724. 7:02:18address register in was one. But because
  9725. 7:02:20we're adding it with the clock, the
  9726. 7:02:22actual input that doesn't that goes to
  9727. 7:02:24the register doesn't actually hit that
  9728. 7:02:26rising edge until the clock hits a
  9729. 7:02:28rising edge, which is pretty neat.
  9730. 7:02:32And then when the clock is high, we can
  9731. 7:02:35pretty much set these things back to
  9732. 7:02:36zero.
  9733. 7:02:52And [snorts] then when the clock falls
  9734. 7:02:54low again, we can kind of reset and
  9735. 7:02:56prepare our next signals, which was RAM
  9736. 7:02:58out.
  9737. 7:03:02equals 1 and a in equals 1. And then the
  9738. 7:03:06next moment we have this rising clock
  9739. 7:03:08edge, it'll latch the value from RAM
  9740. 7:03:10that's written onto the bus into the A
  9741. 7:03:11register. And so then when that we're at
  9742. 7:03:14the rising clock signal and I guess for
  9743. 7:03:16just indefinitely, we can reset all of
  9744. 7:03:18our signals back to zero. So instruction
  9745. 7:03:21register out or sorry RAM out
  9746. 7:03:29equals Z and A in equals Z. So this is
  9747. 7:03:33cool because now what it kind of means
  9748. 7:03:36is our control here is just going to be
  9749. 7:03:38listening to this clock signal and it's
  9750. 7:03:41going to need to for every instruction
  9751. 7:03:43set various control lines right before
  9752. 7:03:46or right after a rising or falling clock
  9753. 7:03:48edge.
  9754. 7:03:50And
  9755. 7:03:52this is convenient because it means the
  9756. 7:03:54uh speed of the computer is now bound to
  9757. 7:03:56the clock. So you can kind of see for
  9758. 7:03:59this load a instruction we needed two
  9759. 7:04:01clock cycles. Clock cycle meaning we
  9760. 7:04:04needed to go from low to high back to
  9761. 7:04:07low to high. So we hit this like low
  9762. 7:04:10high cycle uh alternation two times and
  9763. 7:04:13that's enough to actually implement the
  9764. 7:04:16entire instruction. So that's pretty
  9765. 7:04:18cool.
  9766. 7:04:19Um before we go ahead and extend this to
  9767. 7:04:22our second instruction, let's actually
  9768. 7:04:24take this one step further. And uh one
  9769. 7:04:28thing I've left ambiguous until now is
  9770. 7:04:31how we get this instruction into the
  9771. 7:04:34instruction register.
  9772. 7:04:36And
  9773. 7:04:37there's something interesting to be said
  9774. 7:04:39about the fact that our instructions are
  9775. 7:04:41all eight bits because that goes
  9776. 7:04:44handinhand with everything else. you
  9777. 7:04:46know all of our operants the ALU are 8
  9778. 7:04:49bits the output register takes an 8bit
  9779. 7:04:51value the bus itself is 8 bits and uh
  9780. 7:04:55interestingly each value in RAM is 8
  9781. 7:04:58bits so what we actually do
  9782. 7:05:03um is store the instruction itself in
  9783. 7:05:06RAM
  9784. 7:05:08and then read that from RAM into the
  9785. 7:05:10instruction register and basically the
  9786. 7:05:13way our computer will execute is it'll
  9787. 7:05:16look when we power it on, it'll look at
  9788. 7:05:20whatever is in address zero of RAM and
  9789. 7:05:23treat that not as a number or a value,
  9790. 7:05:26but rather as an instruction
  9791. 7:05:29and
  9792. 7:05:31it'll load that value from RAM into this
  9793. 7:05:34instruction register
  9794. 7:05:36and then the computer, the control
  9795. 7:05:38branch that we still have left ambiguous
  9796. 7:05:41will go ahead and actually execute that
  9797. 7:05:43con that instruction.
  9798. 7:05:45And once that instruction is fully done,
  9799. 7:05:47we'll then look at the next index in
  9800. 7:05:49RAM. So we start with the first item in
  9801. 7:05:51RAM, treat that 8 bit value as an
  9802. 7:05:53instruction, execute it, and then look
  9803. 7:05:56at the second item in RAM, treat that as
  9804. 7:05:59an 8bit instruction, and execute it. So
  9805. 7:06:02in order to keep track of where we are
  9806. 7:06:06in our execution process, we're going to
  9807. 7:06:08have a new instruction, a new register
  9808. 7:06:11called the program counter. So I I've
  9809. 7:06:14extended the bus a little bit and recall
  9810. 7:06:17that our addresses to RAM are all four
  9811. 7:06:19bits. So uh that means we can only index
  9812. 7:06:22using the least four significant bits
  9813. 7:06:25here. And we're going to have a new
  9814. 7:06:27register called the program
  9815. 7:06:32counter
  9816. 7:06:34and that is a 4bit register.
  9817. 7:06:40And
  9818. 7:06:42there's also going to be uh so I guess
  9819. 7:06:46it can put its value onto the bus
  9820. 7:06:59and we will call that signal um
  9821. 7:07:04PC out.
  9822. 7:07:13So this program counter is going to
  9823. 7:07:15start off as zero
  9824. 7:07:18and it's going to point us to the first
  9825. 7:07:19item in RAM. And then once we execute
  9826. 7:07:21that instruction, we'd like to increment
  9827. 7:07:23this program counter to become one. And
  9828. 7:07:26once we execute, once we increment to to
  9829. 7:07:28be one, then uh we'll look up whatever
  9830. 7:07:30is in that address at one in RAM, treat
  9831. 7:07:33that as an instruction, get it into our
  9832. 7:07:35instruction register, and then execute
  9833. 7:07:38it. And once we do that, we've executed
  9834. 7:07:40the uh instructions at address zero and
  9835. 7:07:42at address one. So we'll increment this
  9836. 7:07:44program counter again to two. And we'll
  9837. 7:07:47do the same thing. And so basically,
  9838. 7:07:49this program counter
  9839. 7:07:52um is going to be incrementing. it's
  9840. 7:07:55when it should be counting up from zero.
  9841. 7:07:56And you might be wondering how we do
  9842. 7:07:58that. You know, this is just a register
  9843. 7:08:00on itself. But uh we actually talked
  9844. 7:08:03about this earlier when we constructed
  9845. 7:08:04our ALOU. We showed how you could uh if
  9846. 7:08:07you loaded some value into the left
  9847. 7:08:10operand and then a fixed value of one
  9848. 7:08:13into the second operand and the output
  9849. 7:08:15will always be the the operand plus one.
  9850. 7:08:18And so you can kind of feed that output
  9851. 7:08:20and read that back into register A and
  9852. 7:08:23it'll keep incrementing register A every
  9853. 7:08:25time you latch in a new value because we
  9854. 7:08:28fix the value of register B to be one.
  9855. 7:08:30The output of the ALU will always be A +
  9856. 7:08:32one. So that's one way you can uh
  9857. 7:08:35implement this counter. It turns out in
  9858. 7:08:37an actual computer it's not really uh
  9859. 7:08:39pragmatic. There's a faster way uh
  9860. 7:08:42faster meaning fewer logic gates to uh
  9861. 7:08:45build out a a counter. We're not going
  9862. 7:08:48to deal with that for the sake of this
  9863. 7:08:49video because I don't think the
  9864. 7:08:50complexity justifies the divergence
  9865. 7:08:52here. But let's just say you already
  9866. 7:08:55know how to build this counter. So we're
  9867. 7:08:57going to suppose that this counter
  9868. 7:08:58mechanism
  9869. 7:09:00already exists. And I'm going to denote
  9870. 7:09:02that uh with a better circle
  9871. 7:09:07and we're going to call this like a plus
  9872. 7:09:09one. You can imagine that could be a
  9873. 7:09:11whole alou that takes the whatever
  9874. 7:09:13whatever values in the program counter
  9875. 7:09:15uh adds one to it as a fixed second
  9876. 7:09:18operand and then feeds that back in. And
  9877. 7:09:22we're going to have this same um
  9878. 7:09:25kind of program counter in signal
  9879. 7:09:30that'll kind of read in. And we can also
  9880. 7:09:33go ahead and end it with the clock
  9881. 7:09:35because we've done that for all of our
  9882. 7:09:36other signals. So I'll say
  9883. 7:09:58so
  9884. 7:10:00now we have two new signals. But now
  9885. 7:10:03what happens is
  9886. 7:10:05we need to think about we know what
  9887. 7:10:07happens uh for this load a instruction
  9888. 7:10:09we've designed. We know what happens uh
  9889. 7:10:12in terms of what the control logic needs
  9890. 7:10:14to do once that operation command is in
  9891. 7:10:19the instruction register.
  9892. 7:10:21But now we need to take a few steps back
  9893. 7:10:28and actually figure out how we get this
  9894. 7:10:31instruction from RAM into the
  9895. 7:10:34instruction register.
  9896. 7:10:36So we know that there's a value in the
  9897. 7:10:39program counter at any given time. And
  9898. 7:10:41so what we'd like to do is look up treat
  9899. 7:10:44that as an address in RAM. Look up that
  9900. 7:10:47value from RAM and get it into the
  9901. 7:10:48instruction register. And since
  9902. 7:10:50everything's hooked into the bus, you
  9903. 7:10:52might be able to guess already what we
  9904. 7:10:54do here. The first thing we'd like to do
  9905. 7:10:57is program counter out. So read that
  9906. 7:11:00onto the bus. Push that onto the
  9907. 7:11:01[snorts] bus. And then also pulse uh or
  9908. 7:11:05set memory address register in. And then
  9909. 7:11:08at the exact moment we have a rising
  9910. 7:11:10clock edge because we've added the M in
  9911. 7:11:12signal with the clock uh the value on
  9912. 7:11:15the bus which will be the program
  9913. 7:11:16counter address will get locked into
  9914. 7:11:18memory address register.
  9915. 7:11:20So that's our first clock cycle. Then
  9916. 7:11:22our second clock cycle, we'd like to
  9917. 7:11:24take the value that's in that address at
  9918. 7:11:27RAM, which is here because RAM RAM is
  9919. 7:11:30always looking up the value that's in
  9920. 7:11:31the memory address register, put that
  9921. 7:11:33onto the bus. So we'd set RAM out, and
  9922. 7:11:36then to read it into the instruction
  9923. 7:11:38register, we'd actually need a new line,
  9924. 7:11:41uh, a new mechanism that can read from
  9925. 7:11:44the bus into this instruction register.
  9926. 7:11:46So, we're going to hook in the
  9927. 7:11:48instruction register to the bus
  9928. 7:11:52and
  9929. 7:11:58we are going to uh kind of have that be
  9930. 7:12:03denoted uh accordingly.
  9931. 7:12:09And that means we have a new line that
  9932. 7:12:11we care about which is uh instruction
  9933. 7:12:14register in. And we're obviously going
  9934. 7:12:15to end that with the clock because we've
  9935. 7:12:17done that for all of our input signals
  9936. 7:12:18so far.
  9937. 7:12:40Cool. So basically uh we just need to
  9938. 7:12:44add two more clock cycles here. One is
  9939. 7:12:49to get the value from the program
  9940. 7:12:51counter out and read it into the memory
  9941. 7:12:52address register. That takes one clock
  9942. 7:12:54cycle. And then the second clock cycle
  9943. 7:12:56is putting the value from RAM out onto
  9944. 7:12:58the bus and reading it into the
  9945. 7:13:00instruction register. So we can come
  9946. 7:13:03back to our original diagram here.
  9947. 7:13:06And this we know is what happens once we
  9948. 7:13:09have a value in the instruction
  9949. 7:13:10register. But we're interested in uh the
  9950. 7:13:12full steps of equations of how we get
  9951. 7:13:13the value into the instruction register.
  9952. 7:13:15So what we do is we set
  9953. 7:13:22the program counter out and m in to both
  9954. 7:13:25be one. So PC out equals 1. M in equals
  9955. 7:13:321. Then we'll do that you know at some
  9956. 7:13:34point here. Hold that value. Hold those
  9957. 7:13:37two. And then on the rising clock edge,
  9958. 7:13:39uh, that'll have the effect of moving
  9959. 7:13:41the value in the program counter into
  9960. 7:13:43the memory address register. And so then
  9961. 7:13:45we're looking up the the RAM is always
  9962. 7:13:46looking at the MAR. And so we have the
  9963. 7:13:48value at that address ready. So we'll
  9964. 7:13:51put that value onto the bus with RAM out
  9965. 7:13:53and read it in with this instruction
  9966. 7:13:56register in.
  9967. 7:14:13And then as soon as there's that rising
  9968. 7:14:15clock edge, we will then have the value
  9969. 7:14:18uh latched in the instruction register.
  9970. 7:14:21And then we can actually start the
  9971. 7:14:23execution of the load a instruction that
  9972. 7:14:26we've designed here. So we can literally
  9973. 7:14:27just connect these two.
  9974. 7:14:31And so this is the full timing sequence
  9975. 7:14:34of how we implemented that instruction.
  9976. 7:14:41And so you can kind of see that this
  9977. 7:14:42takes one, two, three, four, four clock
  9978. 7:14:48cycles.
  9979. 7:14:50Four clock cycles
  9980. 7:14:53to implement this instruction.
  9981. 7:14:56And
  9982. 7:14:58it turns out um that is also something
  9983. 7:15:03that we will design. It'll be a design
  9984. 7:15:05choice. We are going to give all of our
  9985. 7:15:07instructions five clock cycles to
  9986. 7:15:10execute. And that is because we will see
  9987. 7:15:14that the longest of the instructions
  9988. 7:15:16that we design, the longest instruction
  9989. 7:15:20takes five clock cycles and so we need
  9990. 7:15:23to kid to that longest instruction.
  9991. 7:15:25There are some computers that could have
  9992. 7:15:27variable length clock cycles per
  9993. 7:15:28instruction. For us, it becomes easier
  9994. 7:15:31to understand and reason about if we
  9995. 7:15:32just say every five clock cycles
  9996. 7:15:36is synonymous with the computer having
  9997. 7:15:38executed one instruction.
  9998. 7:15:41So for this uh command, we've used four
  9999. 7:15:45clock cycles. This last clock cycle
  10000. 7:15:47would just be wasted. Like it'd be a no
  10001. 7:15:51operation, no wasted. And that's fine.
  10002. 7:15:54It's trivial. Um, but it also turns out,
  10003. 7:15:58we'll talk about this later, but more
  10004. 7:15:59modern computers can actually execute uh
  10005. 7:16:02entire instructions in a single clock
  10006. 7:16:04cycle. And it turns out uh that you
  10007. 7:16:07also, you would think, hey, if we just
  10008. 7:16:10uh turn up the clock frequency. So right
  10009. 7:16:13now we have this if we just made the
  10010. 7:16:15clock go from high to low faster
  10011. 7:16:18that we could uh actually you know we
  10012. 7:16:21could just crank that the width of those
  10013. 7:16:23intervals and have them approach zero
  10014. 7:16:25and that would let our computer execute
  10015. 7:16:27instructions faster and faster and you
  10016. 7:16:30know that give the the illusion of it
  10017. 7:16:31being just as fast as if every
  10018. 7:16:34instruction had used one clock cycle.
  10019. 7:16:35But it turns out you can't actually push
  10020. 7:16:37these to zero because
  10021. 7:16:40uh there's a delay here. there's a delay
  10022. 7:16:43between everything you you the line you
  10023. 7:16:44set and then it actually being received.
  10024. 7:16:47So every time I say like this
  10025. 7:16:48instruction like program counter out uh
  10026. 7:16:51there's actually a slight delay in the
  10027. 7:16:53logic gates like uh in the logic gates
  10028. 7:16:56like physically connecting the program
  10029. 7:16:58counter to the bus has a a delay it's
  10030. 7:17:01very small delay uh but it turns out you
  10031. 7:17:04have to wait for the longest delay of
  10032. 7:17:06all components before you can pulse that
  10033. 7:17:09clock again. So something like the ALU
  10034. 7:17:11where if we put a value in register A
  10035. 7:17:12and B, there's a lot of logic gates in
  10036. 7:17:14the ALU. You kind of have to wait for if
  10037. 7:17:16each logic gate takes like uh a
  10038. 7:17:18millisecond, we have 50 in the slowest
  10039. 7:17:21route, um then you have to wait 50
  10040. 7:17:24milliseconds for the output of the ALU
  10041. 7:17:26to be considered valid, which means your
  10042. 7:17:28clock cycle, if you have something that
  10043. 7:17:29depends on the result of the ALU output,
  10044. 7:17:33has to be at least 50 milliseconds. So
  10045. 7:17:36we can crank these pretty far and it
  10046. 7:17:38appears that the computer runs fast but
  10047. 7:17:40there is a true limitation and it also
  10048. 7:17:42turns out modern computers have that
  10049. 7:17:44clock running really really quickly.
  10050. 7:17:48So we built the first uh implementation
  10051. 7:17:52of load a but we've actually forgot one
  10052. 7:17:54subtle thing and that once this load a
  10053. 7:17:57operation is done we will correctly have
  10054. 7:18:00the value uh denoted in the argument
  10055. 7:18:03part of the instruction
  10056. 7:18:06in register A. But our program counter
  10057. 7:18:10is still going to be pulsed at zero
  10058. 7:18:14which means if we repeat the same
  10059. 7:18:15process we're going to execute the same
  10060. 7:18:17instruction. we'd like to do is actually
  10061. 7:18:20increment the program counter so that
  10062. 7:18:23when we repeat these same set of
  10063. 7:18:24instructions to get the value from the
  10064. 7:18:26program counter uh stored at RAM into
  10065. 7:18:29the instruction register we have the
  10066. 7:18:30next instruction not the same one. So
  10067. 7:18:32there's one subtle thing and that
  10068. 7:18:34there's this program counter incin flag
  10069. 7:18:37uh that'll effectively increment it and
  10070. 7:18:41once we use the uh we only use the
  10071. 7:18:43program counter value
  10072. 7:18:45at this very first step. So actually at
  10073. 7:18:48any other step along here between here
  10074. 7:18:52to the next instruction, we can just
  10075. 7:18:54pulse that flag on any of the rising
  10076. 7:18:56edges and it'll have the effect of
  10077. 7:18:58incrementing the program counter so that
  10078. 7:19:00when we get to the next instruction,
  10079. 7:19:02it's already ready. So I could just pick
  10080. 7:19:04any uh any of these rising clock edges
  10081. 7:19:07to do this and say you know program
  10082. 7:19:10counter inc uh equals 1.
  10083. 7:19:16And uh I'll also call this I will enable
  10084. 7:19:19this program counter increment uh
  10085. 7:19:23because it turns out we'll eventually
  10086. 7:19:24want to be able to read actually a value
  10087. 7:19:26into the program counter directly and
  10088. 7:19:28that'll be called program counter in. So
  10089. 7:19:30I don't want to get this confused with
  10090. 7:19:32uh being able to increment the program
  10091. 7:19:34counter.
  10092. 7:19:38So what we're going to go ahead and do
  10093. 7:19:40now is actually define all of the
  10094. 7:19:43remaining instructions from this
  10095. 7:19:44computer, discuss them at a high level,
  10096. 7:19:47and then after we do that, we'll finish
  10097. 7:19:51out by tying together what this actual
  10098. 7:19:53control brains is. And once we have that
  10099. 7:19:56done, we've uh completed the computer,
  10100. 7:19:58which is a crazy thing to say. So, we're
  10101. 7:20:00not quite there yet. Let's keep going.
  10102. 7:20:03Um, so our second instruction is going
  10103. 7:20:07to be called add.
  10104. 7:20:12And, uh, the full name is also just kind
  10105. 7:20:15of addition. Uh, this is just for our
  10106. 7:20:17our readability here.
  10107. 7:20:20The op code will be two. the next number
  10108. 7:20:25and what add will do it's slightly
  10109. 7:20:28different uh you know we've we've done
  10110. 7:20:30addition in the ALOU and you kind of
  10111. 7:20:32know how that works and how you kind of
  10112. 7:20:33punch a value onto the bus hook it into
  10113. 7:20:35some registers and
  10114. 7:20:38uh kind of latch that value in. But what
  10115. 7:20:41AD does is it says register A
  10116. 7:20:47will equal whatever is in register A
  10117. 7:20:54plus
  10118. 7:20:55RAM at the arg specified.
  10119. 7:20:59So there is an argument to load a. So if
  10120. 7:21:02I uh call load a
  10121. 7:21:07with
  10122. 7:21:10uh address 3 as the argument, it'll take
  10123. 7:21:14it'll go to RAM. It'll say, "Hey,
  10124. 7:21:17whatever is in address 3, look it up,
  10125. 7:21:21put it in register B, then take the
  10126. 7:21:24output of that and feed it back into
  10127. 7:21:27register A. And that will have the
  10128. 7:21:29effect of adding whatever is in RAM at
  10129. 7:21:33the specified argument into uh back into
  10130. 7:21:38register A. So it'll be A equals A plus
  10131. 7:21:41uh whatever we put in B and what we put
  10132. 7:21:44in B is that that address and RAM.
  10133. 7:21:47Okay.
  10134. 7:21:49So that's how we do addition. Then you'd
  10135. 7:21:52imagine uh the logical equivalent to
  10136. 7:21:54that is subtraction.
  10137. 7:21:57So sub
  10138. 7:22:02and you could probably guess its op code
  10139. 7:22:04is whatever comes next which is three.
  10140. 7:22:06And you could also probably guess that
  10141. 7:22:08the effect is register A equals register
  10142. 7:22:12A. So we take we're always operating on
  10143. 7:22:15whatever is in register A and we'll
  10144. 7:22:17subtract whatever is in RAM at argu.
  10145. 7:22:21So, it's the exact same thing we've done
  10146. 7:22:23before, except uh when we actually get
  10147. 7:22:27the result of the LU, we'll punch this
  10148. 7:22:28add subtract line. When we set it to a
  10149. 7:22:31one, uh as we learned earlier in this
  10150. 7:22:33video, it'll have the effect of doing
  10151. 7:22:35subtraction.
  10152. 7:22:36Our next instruction
  10153. 7:22:41is called STA
  10154. 7:22:44and that stands for store
  10155. 7:22:47A.
  10156. 7:22:50And this has the op code of 0 1 0 0.
  10157. 7:22:57That's four in in decimal. And what
  10158. 7:23:01store a does is it lets us take the
  10159. 7:23:03value
  10160. 7:23:05that uh is in a and write it into RAM.
  10161. 7:23:09So we we talked a lot about reading from
  10162. 7:23:11RAM, but uh writing into RAM is also
  10163. 7:23:14important. And the register A is kind of
  10164. 7:23:17the design of our computer here. We
  10165. 7:23:19actually don't have any uh commands in
  10166. 7:23:22the default instruction set that let you
  10167. 7:23:26specifically do something with register
  10168. 7:23:27B. Everything that is done with register
  10169. 7:23:30B is usually implicitly defined as part
  10170. 7:23:31of one of these operations that works
  10171. 7:23:33from register A. That's just by design.
  10172. 7:23:35We could totally have these equivalent
  10173. 7:23:36arguments for we could have you know for
  10174. 7:23:40this AD we could have a add B which is
  10175. 7:23:43the same same operation but for register
  10176. 7:23:45B. So it' be like register B equals
  10177. 7:23:46register B plus some argument into RAM.
  10178. 7:23:50We just don't have that. uh and you know
  10179. 7:23:53we actually could easily build it and
  10180. 7:23:55that's something uh you could leave as
  10181. 7:23:56an exercise to you afterwards but I
  10182. 7:24:00digress. Store a uh it'll say RAM
  10183. 7:24:06at whatever argument you specify it will
  10184. 7:24:09treat that argument as an address
  10185. 7:24:13uh equals whatever is in register A. So
  10186. 7:24:16to implement this one, what would happen
  10187. 7:24:19is once we get the instruction in the
  10188. 7:24:21instruction register, we put the least
  10189. 7:24:23four significant bits on the bus that op
  10190. 7:24:25uh that memory address
  10191. 7:24:28and we could hook that into the memory
  10192. 7:24:30address register. And then what we need
  10193. 7:24:33to do is write register A uh have a way
  10194. 7:24:36for writing register A's value out onto
  10195. 7:24:38the bus and then we can read that value
  10196. 7:24:42by pulsing uh RAM in. And since we've
  10197. 7:24:45already loaded the address that we want
  10198. 7:24:47to write into into the memory address
  10199. 7:24:49register, uh the moment we pulse that
  10200. 7:24:51RAM in and have a rising clock edge,
  10201. 7:24:53it'll lock the value from register A in
  10202. 7:24:57to the correct uh component on the bus.
  10203. 7:25:00And so you might be noticing, hey, we
  10204. 7:25:03don't have a way for to actually write
  10205. 7:25:05register A or B onto the bus. So, uh
  10206. 7:25:08we'll go ahead and and do that. So, we
  10207. 7:25:11could do
  10208. 7:25:14two.
  10209. 7:25:15And so we can call that uh A out. And
  10210. 7:25:20then we can do the same thing
  10211. 7:25:24if we needed to for B.
  10212. 7:25:31And this is the the power, you know,
  10213. 7:25:32this is the power of the bus. we can
  10214. 7:25:34just decide to connect things and it's
  10215. 7:25:36just as easy as connecting them versus
  10216. 7:25:38for if we had to connect it to every
  10217. 7:25:39other component, you know, we'd have to
  10218. 7:25:41connect it to the the RAM in signal and
  10219. 7:25:44then or it with a bunch of other things
  10220. 7:25:46that already connected to the RAM in
  10221. 7:25:47signal. So the bus really makes things a
  10222. 7:25:49lot easier here.
  10223. 7:25:54Okay,
  10224. 7:25:56so that's story. The next is called LDI.
  10225. 7:26:01That stands for load immediate
  10226. 7:26:07and its op code is you could probably
  10227. 7:26:09guess 4 + 1 is five. And [snorts]
  10228. 7:26:12this one's a little bit different. So up
  10229. 7:26:15until now, every argument here
  10230. 7:26:19has every instruction has had an
  10231. 7:26:21argument which is interesting in itself.
  10232. 7:26:24Uh we will see that that's not always
  10233. 7:26:26the case.
  10234. 7:26:28But uh
  10235. 7:26:31whenever there has been an argument,
  10236. 7:26:33it's been an address into RAM. This time
  10237. 7:26:37the argument will actually be treated as
  10238. 7:26:39a literal number. And what it'll do is
  10239. 7:26:42it'll load whatever the argument is into
  10240. 7:26:47register A.
  10241. 7:26:56So this contrasts well with our very
  10242. 7:26:59first command, our very first
  10243. 7:27:01instruction load A. And you can see the
  10244. 7:27:03difference here is load A will take the
  10245. 7:27:06argument and interpret it as an address
  10246. 7:27:08in memory and go look up whatever is in
  10247. 7:27:10that address of memory in RAM and feed
  10248. 7:27:13that into register A versus load
  10249. 7:27:15immediate will just take whatever the
  10250. 7:27:17argument is, interpret it as a number
  10251. 7:27:19itself and load that into RAM. So if I
  10252. 7:27:21had load immediate with uh 011,
  10253. 7:27:27this will load the immediate 011 into
  10254. 7:27:29register A, which means register A will
  10255. 7:27:31have the value of 1 + 2 + 4, which is 7.
  10256. 7:27:36And it does mean that load A the you can
  10257. 7:27:40only put in a value that's representable
  10258. 7:27:42in four bits. So the the biggest value
  10259. 7:27:44you could put in is 1 1 1, which would
  10260. 7:27:47be 1 plus 2 + 4 + 8. 8 + 4 is 12 + 2 is
  10261. 7:27:5114 + 1's 15. So any number 0 through 15
  10262. 7:27:55you can load in immediately
  10263. 7:27:59uh to register A. And so why would we
  10264. 7:28:00want to do this is because let's say I
  10265. 7:28:03want to load the value five into
  10266. 7:28:06register A. If we just had load A, we'd
  10267. 7:28:09have to waste two instructions. We have
  10268. 7:28:11to waste two of our slots in RAM. We'd
  10269. 7:28:13have to waste one of them to h to house
  10270. 7:28:15the load a instruction and then the
  10271. 7:28:19argument to the load a instruction would
  10272. 7:28:20point to a different spot in RAM which
  10273. 7:28:22would house a literal value five.
  10274. 7:28:25So this uses two spots in RAM to load a
  10275. 7:28:28value uh to load a value into register
  10276. 7:28:30A. But with a load immediate we could
  10277. 7:28:31just do it with one. And RAM is a
  10278. 7:28:33constrained resource here. So we only
  10279. 7:28:36have uh four addresses. So we have two
  10280. 7:28:37to the four um like 16 different spots
  10281. 7:28:42in RAM which is not a lot to work with.
  10282. 7:28:44So we want to use it as efficiently as
  10283. 7:28:45possible. So that's why we have this.
  10284. 7:28:48So that's a little bit different. And so
  10285. 7:28:52now we've seen
  10286. 7:28:54uh
  10287. 7:28:56four examples where the ARG is a data
  10288. 7:28:59address in RAM.
  10289. 7:29:06Now we've seen one where arg is a
  10290. 7:29:09literal.
  10291. 7:29:10There's a literal number and well let's
  10292. 7:29:14see what comes next. The next
  10293. 7:29:17instruction is called
  10294. 7:29:20JC. That means
  10295. 7:29:24jump
  10296. 7:29:26if carry.
  10297. 7:29:31And here's our op code for it.
  10298. 7:29:39Oh, sorry. I jumped ahead of myself
  10299. 7:29:41here.
  10300. 7:29:44Next instruction is called JMP for jump.
  10301. 7:29:48It just stands for jump.
  10302. 7:29:52And
  10303. 7:29:54this is an interesting one. What this
  10304. 7:29:57says is all it'll do
  10305. 7:30:02is set the program counter
  10306. 7:30:08equal to the argument. So
  10307. 7:30:11this is super interesting and it might
  10308. 7:30:13not seem that crazy at first, but recall
  10309. 7:30:16our program counter starts at zero and
  10310. 7:30:18then goes to one and goes to two. And
  10311. 7:30:20pretty much it tells us
  10312. 7:30:24uh what instruction what position in RAM
  10313. 7:30:27we should look at to figure out what the
  10314. 7:30:29next instruction our computer should
  10315. 7:30:31execute is. And this has the effect of
  10316. 7:30:33just walking down RAM top to bottom. But
  10317. 7:30:36what jump lets you do is it lets you
  10318. 7:30:38jump back to a a start. So let's say my
  10319. 7:30:43RAM had some value. Uh let's say like my
  10320. 7:30:46RAM was organized. So at address zero we
  10321. 7:30:48had a load immediate
  10322. 7:30:51three and then we had a add
  10323. 7:30:59um
  10324. 7:31:01add uh we could address six and then at
  10325. 7:31:05address six and RAM we had the the
  10326. 7:31:06literal value
  10327. 7:31:09like one.
  10328. 7:31:12Then what would happen is our computer
  10329. 7:31:15would uh load an immediate three into
  10330. 7:31:19register A and then add the value one to
  10331. 7:31:22it so it have the value four. And if we
  10332. 7:31:25wanted to keep doing this over and over,
  10333. 7:31:27we'd have to keep adding uh the same add
  10334. 7:31:31command
  10335. 7:31:33over and over in RAM. Uh, and what we'd
  10336. 7:31:38find is that we could only do it uh oh
  10337. 7:31:40so many times before we run out of space
  10338. 7:31:42and RAM. But what's kind of neat here
  10339. 7:31:46is if I have a jump command
  10340. 7:31:50back to address one, then when when our
  10341. 7:31:54RAM finishes this first ad instruction,
  10342. 7:31:56it'll then the program counter will
  10343. 7:31:58increment from one to two. We'll get
  10344. 7:31:59here. We'll see that the value in RAM
  10345. 7:32:02address 2, its interpretable value says
  10346. 7:32:05jump back to address one. And so it'll
  10347. 7:32:09reset the program counter back to one.
  10348. 7:32:13And so our PC will now point back to
  10349. 7:32:15one. And then we execute the next
  10350. 7:32:16instruction and we'll load the value
  10351. 7:32:19address one into the instruction
  10352. 7:32:20register. And that value is the
  10353. 7:32:22operation to add uh whatever's at
  10354. 7:32:24address six back to the register. And so
  10355. 7:32:27this would just increment the A register
  10356. 7:32:29by one again. And then we do that, come
  10357. 7:32:31back here, and we kind of keep bouncing
  10358. 7:32:33in this loop. Um, and so our program
  10359. 7:32:36would kind of never finish. And the
  10360. 7:32:37value in the A register would just keep
  10361. 7:32:39increasing by one, and it it just be
  10362. 7:32:41counting up, which is pretty
  10363. 7:32:42interesting.
  10364. 7:32:45So jump lets you change the fundamental
  10365. 7:32:48control execution scheme of your
  10366. 7:32:49computer.
  10367. 7:32:56Okay, we can take jump a little further.
  10368. 7:32:59Uh, I'm actually going to
  10369. 7:33:06I'm going to go ahead and erase this
  10370. 7:33:16and I will clean this up here.
  10371. 7:33:23And we have four more instructions left
  10372. 7:33:26to build out.
  10373. 7:33:29The first we have two more jump related
  10374. 7:33:32instructions.
  10375. 7:33:37JC, which stands for jump if carry
  10376. 7:33:47and the op code is as you could guess
  10377. 7:33:50011.
  10378. 7:33:53And what this says is if the last
  10379. 7:33:58result, if the last operation that
  10380. 7:34:01happened in the ALU uh carried i.e
  10381. 7:34:04there's that overflow then we can jump
  10382. 7:34:07to a specific address in memory
  10383. 7:34:09otherwise you go on. So
  10384. 7:34:16if carry
  10385. 7:34:18lag equals 1,
  10386. 7:34:23PC equals arg
  10387. 7:34:27no
  10388. 7:34:30and
  10389. 7:34:34When we talked about our ALOU a long
  10390. 7:34:36time ago,
  10391. 7:34:38we built this by chaining a bunch of uh
  10392. 7:34:40full adders together and the carry out
  10393. 7:34:44of one adder became the carry in of
  10394. 7:34:47another adder. But then recall for the
  10395. 7:34:49leftmost adder, there was a carry out
  10396. 7:34:51flag and
  10397. 7:34:54if that flag was a one it meant we
  10398. 7:34:56overflowed, meaning we couldn't fit the
  10399. 7:34:58value into eight bits.
  10400. 7:35:01And so we've kind of ignored that in our
  10401. 7:35:03alou here. But what we can actually do
  10402. 7:35:06is just uh build a build a new register
  10403. 7:35:10that uh looks at this carry flag. Um so
  10404. 7:35:15it can take the uh it can be called the
  10405. 7:35:18flags register and it'll whenever we do
  10406. 7:35:22an alou operation we'll look and say hey
  10407. 7:35:25did this last carry did the last the
  10408. 7:35:27leftmost full adder carry out and if it
  10409. 7:35:30did then we know that the last
  10410. 7:35:32arithmetic operation that happened in
  10411. 7:35:34the alou overflowed assuming we're
  10412. 7:35:36working with positive numbers we'll just
  10413. 7:35:38keep it simple for now and if it did we
  10414. 7:35:41should latch that value into this flags
  10415. 7:35:43register
  10416. 7:35:44So,
  10417. 7:35:46what I'll do over here is I'll have a
  10418. 7:35:51flags register
  10419. 7:35:54and it's going to be two bits.
  10420. 7:35:57And
  10421. 7:36:01what we'll need to do is also add a new
  10422. 7:36:05control line for flags in. So this will
  10423. 7:36:09read uh it'll interpret whatever values
  10424. 7:36:13the state of the alou and and and latch
  10425. 7:36:17that value in. And of course
  10426. 7:36:21I need to end that with the clock.
  10427. 7:36:38So why is this two bits if uh we encode
  10428. 7:36:42if the carry out is one bit? Well, we'll
  10429. 7:36:44see when we come back to the next
  10430. 7:36:45instruction. But uh the other thing
  10431. 7:36:49that's interesting to know is if the uh
  10432. 7:36:53output of the ALU is zero. So we'll have
  10433. 7:36:56one flag for measuring if the ALU
  10434. 7:36:59carried uh if there was overflow carry
  10435. 7:37:02out I guess.
  10436. 7:37:05and then one flag for measuring if the
  10437. 7:37:08last ALU operation was a zero. And you
  10438. 7:37:11can detect if the last ALU operation was
  10439. 7:37:13a zero by looking at all the output
  10440. 7:37:14registers and uh saying that they are
  10441. 7:37:18all zero. So you would invert each one
  10442. 7:37:20and then add them all together. So the
  10443. 7:37:23each bit position like bit position one
  10444. 7:37:26not and bit position two not and bit
  10445. 7:37:30position three not and so on and so on.
  10446. 7:37:33So you'd assert that they're all zero.
  10447. 7:37:35Uh so you could invert all of those and
  10448. 7:37:38add them together and that would pretty
  10449. 7:37:40much be your your zero flag. I won't
  10450. 7:37:42show that logic here because I'm trying
  10451. 7:37:44to avoid draw drawing individual logic
  10452. 7:37:46gates. But let's see why this jump carry
  10453. 7:37:50command might be useful. So in our
  10454. 7:37:52original example we have here um we
  10455. 7:37:55started with the the number three.
  10456. 7:37:58I guess if we looked at the state of the
  10457. 7:38:00aa register, it'd be three and then four
  10458. 7:38:02and five and six so on. And then
  10459. 7:38:06eventually because our register is eight
  10460. 7:38:08bits, we'd uh we'd get to the maximum
  10461. 7:38:12value that we can store in 8 bits. So 2
  10462. 7:38:15to the 8, let me use my calculator right
  10463. 7:38:17here. 2 to the 8
  10464. 7:38:20is 256 which means we can store values 0
  10465. 7:38:24through 255
  10466. 7:38:26assuming we're just looking at positive
  10467. 7:38:27numbers only but let's say we are for
  10468. 7:38:29now. So
  10469. 7:38:32uh your computer were to get to 250
  10470. 7:38:36and then 251 2523
  10471. 7:38:40254
  10472. 7:38:43255.
  10473. 7:38:44And if we tried to add the number one to
  10474. 7:38:47it again, we have the binary. This is
  10475. 7:38:50what the 255 looks like in binary. If we
  10476. 7:38:53tried to add one to it,
  10477. 7:38:56what actually happens is pretty
  10478. 7:38:58interesting where you can kind of see
  10479. 7:39:09that it'll roll all the way back to zero
  10480. 7:39:12and then we'll have carried out. So, uh,
  10481. 7:39:15if we let this keep running this
  10482. 7:39:17program, what happens is we'd start at
  10483. 7:39:20three, count to 255, and then go back to
  10484. 7:39:22zero and keep counting from zero, back
  10485. 7:39:26up to 1 to 2 to 3 to 4, all the way back
  10486. 7:39:28up to 255. So, pretty much what happen
  10487. 7:39:30is we just keep counting 255 and then
  10488. 7:39:31resetting to zero. Maybe you once you
  10489. 7:39:34hit 255, you don't actually want to uh
  10490. 7:39:37count any further. So, uh, we could
  10491. 7:39:40restructure this program and say,
  10492. 7:39:44um,
  10493. 7:39:45what I'll do here is we could say
  10494. 7:39:56if once you every time you do an
  10495. 7:39:58addition, if you carried uh, then maybe
  10496. 7:40:01you could jump to like address 10 or
  10497. 7:40:04somewhere further away in memory. Um
  10498. 7:40:07otherwise you would jump back to address
  10499. 7:40:10one and keep doing another arithmetic.
  10500. 7:40:13Um and so that way you know every time
  10501. 7:40:15you do an addition you don't try to do
  10502. 7:40:17another one right away by jumping back
  10503. 7:40:19to that same ad operation. You check hey
  10504. 7:40:21did the last result overflow. If it did
  10505. 7:40:24we can stop counting we're at 255 and
  10506. 7:40:27end the program by jumping somewhere
  10507. 7:40:29else in memory.
  10508. 7:40:31So that's just a little bit of
  10509. 7:40:32motivation for that.
  10510. 7:40:38And I said we uh added two flags to our
  10511. 7:40:42register because we wanted to know if
  10512. 7:40:44you could carry out. We also wanted to
  10513. 7:40:46know if the last ALOU result was a zero.
  10514. 7:40:48So we'll go ahead and build that out
  10515. 7:40:50too. And that's going to be called JZ.
  10516. 7:40:54That means jump if zero.
  10517. 7:41:00So, uh, that's going to have the op code
  10518. 7:41:02of 1 0 0. And it pretty much says if the
  10519. 7:41:07zero flag
  10520. 7:41:11equals one, then the program counter can
  10521. 7:41:16equal the argument, i.e. we jump to a
  10522. 7:41:17new address. Otherwise, we don't do
  10523. 7:41:19anything.
  10524. 7:41:22And uh well, you kind of already know
  10525. 7:41:23how that one works. It's the same thing
  10526. 7:41:25except uh you know we need to look at
  10527. 7:41:27the value in the uh the flags
  10528. 7:41:31uh the flags register. So you know the
  10529. 7:41:33first bit position is for carry and you
  10530. 7:41:36could say the second bit position is for
  10531. 7:41:38zero. So we just need to make sure we're
  10532. 7:41:40looking at the right one.
  10533. 7:41:48Okay. Our next instruction
  10534. 7:41:52you might have noticed is we've now
  10535. 7:41:54inter we have instructions to interface
  10536. 7:41:55with every part of this computer except
  10537. 7:41:58for one and that's the display that we
  10538. 7:42:00talked about just uh just fairly
  10539. 7:42:02recently.
  10540. 7:42:03So let's have an instruction to actually
  10541. 7:42:05write uh whatever value is in the A
  10542. 7:42:09register since that's so critical to our
  10543. 7:42:11computer
  10544. 7:42:13um onto the bus and then latch that into
  10545. 7:42:16the output register. So, we're going to
  10546. 7:42:19call this command out.
  10547. 7:42:25It's just
  10548. 7:42:27be display. Uh, they're synonymous here.
  10549. 7:42:30It's op code. Uh, we'll just jump around
  10550. 7:42:34here. We can give it whatever. Let's
  10551. 7:42:36just, uh, throw it 11 1 arbitrarily. We
  10552. 7:42:39can kind of choose what our operation
  10553. 7:42:40codes are. We've been sequential
  10554. 7:42:44up to this point, but there's no harm in
  10555. 7:42:48using the address space however we'd
  10556. 7:42:50like or sorry, using that four bit space
  10557. 7:42:53however we'd like to define our
  10558. 7:42:54instructions. And so if we say formally
  10559. 7:42:57what the
  10560. 7:42:59uh out command does, it's going to set
  10561. 7:43:02our output register equal to register A,
  10562. 7:43:07which will then have the effect of
  10563. 7:43:09displaying
  10564. 7:43:12whatever is in register A onto this
  10565. 7:43:15seven segment display because the way we
  10566. 7:43:17designed that readonly memory was to
  10567. 7:43:19just look at whatever is in this uh
  10568. 7:43:21display register. And so you can imagine
  10569. 7:43:23to implement this command is pretty
  10570. 7:43:25straightforward. You just put the value
  10571. 7:43:26of register A onto the bus with a out.
  10572. 7:43:29And at the same time, you'd have display
  10573. 7:43:31in. And since display ins and with the
  10574. 7:43:33clock, as is every in signal, once you
  10575. 7:43:36have a rising clock edge, it'll hook the
  10576. 7:43:38value into that register. And then ROM
  10577. 7:43:40is just a constant lookup table. So,
  10578. 7:43:42it's always just going to be looking up
  10579. 7:43:43whatever is it's always going to be
  10580. 7:43:45reacting to what's in the register.
  10581. 7:43:46It'll look up. It'll say, "Hey, this
  10582. 7:43:48value is on the register. Like, let's
  10583. 7:43:49just display it on the screen."
  10584. 7:43:54And last but not least is called halt.
  10585. 7:44:06We'll give it op code 1111. And what
  10586. 7:44:09halt will do is it'll physically stop
  10587. 7:44:11the computer.
  10588. 7:44:15So if the computer sees a halt signal,
  10589. 7:44:18if the if we uh have some address in our
  10590. 7:44:21program counter, we look up whatever is
  10591. 7:44:23in that address in RAM, put that in the
  10592. 7:44:25instruction register, and our control
  10593. 7:44:26brain sees that it's a halt, it'll stop.
  10594. 7:44:29It literally will just break like it it
  10595. 7:44:31the computer will not execute anything
  10596. 7:44:33further.
  10597. 7:44:35Um, and that'll be the end of execution
  10598. 7:44:38here. So these are all the instructions
  10599. 7:44:40our toy computer has.
  10600. 7:44:44And
  10601. 7:44:46what's uh a few things to of of note
  10602. 7:44:48here. So we saw that for the first few
  10603. 7:44:50arguments we for the first few
  10604. 7:44:51instructions we built out there were all
  10605. 7:44:54arguments. The arguments were all data
  10606. 7:44:56addresses and RAM. Then we saw an
  10607. 7:44:58instruction who had an argument but the
  10608. 7:44:59argument was a literal number. And then
  10609. 7:45:02for the three jump instructions, the
  10610. 7:45:05argument
  10611. 7:45:06was an instruction address,
  10612. 7:45:13but the instruction address also lived
  10613. 7:45:15in RAM.
  10614. 7:45:18And so there's something interesting to
  10615. 7:45:20be said that RAM has both our data and
  10616. 7:45:23our instructions.
  10617. 7:45:25And the only like the computer doesn't
  10618. 7:45:28really care which is which. It's
  10619. 7:45:29strictly just going to do what you tell
  10620. 7:45:30it to do. So you could actually write uh
  10621. 7:45:34an instruction
  10622. 7:45:36in one position. Uh let's say if I wrote
  10623. 7:45:39an instruction uh at address 4 in RAM
  10624. 7:45:42and then if I called load a on address
  10625. 7:45:464, then it would still work. It would
  10626. 7:45:49interpret whatever the value of the
  10627. 7:45:50address is
  10628. 7:45:54as a literal for the load A. But then
  10629. 7:45:56when we're our program counter equals
  10630. 7:45:58that address, it treats this
  10631. 7:45:59instruction. So it's pretty cool that uh
  10632. 7:46:02it might be weird to wrap your head
  10633. 7:46:03around but conceptually
  10634. 7:46:06values in RAM can be both literals data
  10635. 7:46:11literals or instructions. It just
  10636. 7:46:14depends on the context of how it's how
  10637. 7:46:16that value is being accessed.
  10638. 7:46:18Whenever that value is being accessed
  10639. 7:46:20through the program counter
  10640. 7:46:23or one of these yeah through program
  10641. 7:46:24counter it's definitely going to be a
  10642. 7:46:27treat it as an instruction. But then in
  10643. 7:46:29a lot of these other contexts, if it's
  10644. 7:46:31like an argument to an instruction, it's
  10645. 7:46:33going to be treated as a a literal
  10646. 7:46:35number, which is pretty cool.
  10647. 7:46:38And then we saw down here that there is
  10648. 7:46:41no argument for our final two
  10649. 7:46:43instructions. So those write four bits
  10650. 7:46:46are actually kind of wasteless. You
  10651. 7:46:47could set them whatever you want.
  10652. 7:46:50Another thing to note is we have four
  10653. 7:46:52bits of our op code. So that should be
  10654. 7:46:5416 possible different instructions. But
  10655. 7:46:57if I count out what I have here, I have
  10656. 7:46:581 2 3 4 5 6 7 8 I have 10 different
  10657. 7:47:01instructions. So we are not using our
  10658. 7:47:04instruction space fully. So we could
  10659. 7:47:07define more instructions. It turns out
  10660. 7:47:09this set of instructions is nice because
  10661. 7:47:11it lets us uh kind of define simple
  10662. 7:47:14enough pro like complete enough programs
  10663. 7:47:15on its own. But you know we talked about
  10664. 7:47:18very earlier like hey like our load a
  10665. 7:47:20add and subtract all simply operate on
  10666. 7:47:22register A. Maybe I could use some of
  10667. 7:47:24those extra op codes to build load B or
  10668. 7:47:26add B or subtract B instructions.
  10669. 7:47:29Similarly, our out command will only put
  10670. 7:47:31whatever is in register A onto the seven
  10671. 7:47:34segment display. Maybe I want a way of
  10672. 7:47:36having uh an an out for B. Uh and you
  10673. 7:47:40could imagine extending this computer in
  10674. 7:47:42ways on your own. You could have a
  10675. 7:47:44second output display and that second
  10676. 7:47:46output display could have its own
  10677. 7:47:47command where you could write in the
  10678. 7:47:48value of register B to that uh display
  10679. 7:47:52based on you know
  10680. 7:47:55whatever the argument is. Uh so there
  10681. 7:47:57there's different ways you can extend
  10682. 7:47:59this.
  10683. 7:48:01So now we've defined our instructions.
  10684. 7:48:04Um we walk through load A in great
  10685. 7:48:06detail and we'll do that for some of the
  10686. 7:48:08others. But the last thing we need to do
  10687. 7:48:12here is tie it together with this
  10688. 7:48:15control brains.
  10689. 7:48:18And it turns out we know everything we
  10690. 7:48:21need to tie this computer together and
  10691. 7:48:23explain how this control logic works.
  10692. 7:48:26The very last thing I'll do that I will
  10693. 7:48:27spill out to make it a little bit
  10694. 7:48:29easier. Well, obviously because I'm
  10695. 7:48:30going to explain. But [clears throat] um
  10696. 7:48:33we discussed this instruction. The very
  10697. 7:48:34last one we discussed was halt. And what
  10698. 7:48:38halt should do is it should stop the
  10699. 7:48:39computer. And so you can pretty much see
  10700. 7:48:41that when we designed our the only one
  10701. 7:48:43we walked through in good detail was
  10702. 7:48:45this load a we looked at the timing
  10703. 7:48:47diagram and the in the rising and
  10704. 7:48:49falling edges of the clock. Um pretty
  10705. 7:48:52much you could see that we were making
  10706. 7:48:53every decision based on and making every
  10707. 7:48:56setup i.e. setting different lines you
  10708. 7:48:59know saying this component read its
  10709. 7:49:00value in from the bus and this other
  10710. 7:49:02while this other component is putting
  10711. 7:49:04its value onto the bus. All that was
  10712. 7:49:06based around the clock. And so it turns
  10713. 7:49:09out if a very simple way to actually
  10714. 7:49:11halt the computer and prevent it. Uh
  10715. 7:49:14there's two things you could think of.
  10716. 7:49:16Um my mind when I went to this was
  10717. 7:49:18actually just stopping any instruction
  10718. 7:49:20from getting in the program counter. So
  10719. 7:49:22if the program counter if the program
  10720. 7:49:24counter could never update, then no
  10721. 7:49:26instruction, you know, could be
  10722. 7:49:28executed. But then there's actually like
  10723. 7:49:30something incorrect about that and that
  10724. 7:49:32there's still a value in the program
  10725. 7:49:33counter. So that value cannot can be
  10726. 7:49:35interpreted as an instruction and it
  10727. 7:49:37can't execute. And so that wouldn't
  10728. 7:49:38work. Nor would blocking the program
  10729. 7:49:40counter from writing out onto the bus
  10730. 7:49:42work because uh basically we we always
  10731. 7:49:45say that if a component is reading in
  10732. 7:49:46from the bus, there has to be something
  10733. 7:49:47on the bus. You get into a weird state
  10734. 7:49:49if you have a component that tries to
  10735. 7:49:51read on the bus and there is physically
  10736. 7:49:52nothing on it. I.e. every other
  10737. 7:49:54component that could write onto it.
  10738. 7:49:56Namely, the program counter here is
  10739. 7:49:58actually disconnected. So you might want
  10740. 7:50:00to think let's stop the program counter
  10741. 7:50:02from being able to write its value onto
  10742. 7:50:04the bus and therefore when we try you
  10743. 7:50:06know looked up that address and ram and
  10744. 7:50:08treat as instruction it it won't work
  10745. 7:50:10but the behavior is somewhat undefined
  10746. 7:50:12here um what's easier it turns out and I
  10747. 7:50:16think the only solution for the sake of
  10748. 7:50:18our simplistic computer is just blocking
  10749. 7:50:21the clock but we discussed earlier that
  10750. 7:50:24the clock is always just going up and
  10751. 7:50:26down like it's a physical circuit and
  10752. 7:50:28that's not something we can necessarily
  10753. 7:50:30control. So the way we circumvent this
  10754. 7:50:33is by actually all these components that
  10755. 7:50:36depend on the clock, they don't quite
  10756. 7:50:38depend on just the clock. We'll actually
  10757. 7:50:40and it with some signal. Um and
  10758. 7:50:44basically whenever we set that signal,
  10759. 7:50:47it'll if it is set, it'll prevent the uh
  10760. 7:50:51the clock from actually making any
  10761. 7:50:53progress. So it'll force it to either
  10762. 7:50:54always stay low or always stay high. It
  10763. 7:50:57doesn't really matter which we let it
  10764. 7:50:58at. Um,
  10765. 7:51:00but what we can do here and how we can
  10766. 7:51:02build that circuit is so our instruction
  10767. 7:51:05is called halt. So I'll just call it
  10768. 7:51:07halt. But it'll just be quite literally
  10769. 7:51:09the input to a logicate. We have this
  10770. 7:51:11halt sign and this clock sign. And we
  10771. 7:51:13also probably want uh or let's and them
  10772. 7:51:17together.
  10773. 7:51:20Um, what I was going to say is we
  10774. 7:51:22probably want it such that if our halt
  10775. 7:51:23line is zero, everything is allowed to
  10776. 7:51:26run as normal. But then the when we set
  10777. 7:51:28our halt line to one is when we block
  10778. 7:51:29the clock. So uh what I'll actually do
  10779. 7:51:33is instead of hooking in the halt line
  10780. 7:51:35directly, I'll invert it and then feed
  10781. 7:51:38it in here. And then this output is the
  10782. 7:51:41clock that goes to the rest of the
  10783. 7:51:42computer. And so it turns out that this
  10784. 7:51:46then is another circuit for us to use.
  10785. 7:51:49And I'll kind of move this diagram up
  10786. 7:51:52here
  10787. 7:51:54and we can shift it all down a little
  10788. 7:51:55bit.
  10789. 7:51:58Cool. So what this has the effect of
  10790. 7:52:01doing is what normally in normal
  10791. 7:52:03execution this hot line is zero which
  10792. 7:52:05means we the inverted value is one and
  10793. 7:52:07so the output of this and gate is just
  10794. 7:52:10whatever the value of the clock is which
  10795. 7:52:11is this circuit that's always just
  10796. 7:52:12pulsing between zero and one so it's
  10797. 7:52:14frequently changing it's volatile. Now,
  10798. 7:52:16if we were to flip the halt sign to one,
  10799. 7:52:18the inverse would be zero. And hopefully
  10800. 7:52:20you know by now that anything ended with
  10801. 7:52:23zero is just going to give zero itself,
  10802. 7:52:25which blocks the clock from making any
  10803. 7:52:26progress. And so that when we actually
  10804. 7:52:29get to it and figure out how to
  10805. 7:52:30implement the halt instruction, all that
  10806. 7:52:32instruction has to do is flip this halt
  10807. 7:52:35line to true and it'll stop. It'll
  10808. 7:52:37actually just break anything from making
  10809. 7:52:38further progress. the computer gets into
  10810. 7:52:41a quite literally rec uh unreoverable
  10811. 7:52:44state and nothing can make progress.
  10812. 7:52:49So now we can tie this all together and
  10813. 7:52:53figure out how our control works. And I
  10814. 7:52:55can guarantee that
  10815. 7:52:58you could reason about this yourself
  10816. 7:53:00based on everything we've learned in the
  10817. 7:53:02video so far. There is nothing I'm going
  10818. 7:53:04to introduce here that isn't already
  10819. 7:53:05known to you and that's pretty powerful.
  10820. 7:53:08Um, that being said, I remember when I
  10821. 7:53:10was learning this, this was insanely
  10822. 7:53:12bizarre to wrap my head around and I had
  10823. 7:53:15to have it spilled out to me. The entire
  10824. 7:53:17time I was building the computer, I
  10825. 7:53:18could understand how each component
  10826. 7:53:20works. And it was like, oh, that's how
  10827. 7:53:21the ALOU works. Oh, that's how a
  10828. 7:53:22register works. That's how a counter
  10829. 7:53:24works. That's how RAM work. All makes
  10830. 7:53:26sense. And then when it gets to tying it
  10831. 7:53:28together, you're kind of like, okay,
  10832. 7:53:29well, I understand how each of these
  10833. 7:53:30components work individually, but I
  10834. 7:53:33understand how you can punch lines to
  10835. 7:53:34kind of have them communicate with each
  10836. 7:53:36other via this bus, but how can the
  10837. 7:53:38computer do it all itself? And, you
  10838. 7:53:39know, how do we remove the human element
  10839. 7:53:41of this from the loop, assuming that we
  10840. 7:53:43have some uh program specified in RAM?
  10841. 7:53:46Um, so let's tie it together right now.
  10842. 7:53:48And I'd say this is probably the crux of
  10843. 7:53:50the past eight hours of this video. Um,
  10844. 7:53:55it's crazy for it to be the crux because
  10845. 7:53:57we're not going to build anything you
  10846. 7:53:59already have not seen, but it is very
  10847. 7:54:02powerful to see how something you have
  10848. 7:54:03already seen can be reused again in a
  10849. 7:54:06totally different manner to bring so
  10850. 7:54:08much more complexity to the system. So,
  10851. 7:54:10let's think about so far we've been
  10852. 7:54:12serving as the control. Let's been
  10853. 7:54:14thinking about um what we've been taking
  10854. 7:54:17in as input to the control. So us input
  10855. 7:54:21um
  10856. 7:54:22and
  10857. 7:54:24basically there's a few things. So
  10858. 7:54:25number one is the op code from the
  10859. 7:54:27instruction register. This you know this
  10860. 7:54:30this line right here this arrow. Um
  10861. 7:54:32whatever the op code is in that
  10862. 7:54:34instruction register we use that to
  10863. 7:54:37denote what instruction we're executing
  10864. 7:54:38in. So our inputs are you know this op
  10865. 7:54:41code
  10866. 7:54:43and that's four bits.
  10867. 7:54:47And then we also said that we were
  10868. 7:54:49designing this computer such that oops
  10869. 7:54:52some weird coloring happened here. Um
  10870. 7:54:56such that each instruction that we
  10871. 7:54:59defined in our table took five clock
  10872. 7:55:00cycles to add. And when we walked
  10873. 7:55:03through the load a instruction, you
  10874. 7:55:04know, we went through the little timing
  10875. 7:55:05diagram and showed, hey, we'd set this
  10876. 7:55:07here and you know, clear it here and
  10877. 7:55:08then set these next thing here and clear
  10878. 7:55:10it here. Um and so we were internally
  10879. 7:55:13keeping track of our clock cycles. Um,
  10880. 7:55:17turns out we're going to want to have a
  10881. 7:55:19counter. And so we can we want to count,
  10882. 7:55:21you know, just from
  10883. 7:55:23uh zero to to one um or sorry 0 through
  10884. 7:55:28five, I guess, 0 through four here. So
  10885. 7:55:30what we do is um
  10886. 7:55:34we have to represent five values. So
  10887. 7:55:36we'll build a simple counter on the side
  10888. 7:55:40here. And the counter we build is going
  10889. 7:55:43to be very similar to the program
  10890. 7:55:45counter up here. Except the difference
  10891. 7:55:48is is it's always just going to be
  10892. 7:55:50counting one to five. 0 to four to be
  10893. 7:55:53correct here. It's going to count in
  10894. 7:55:55cycles of five and then reset on its own
  10895. 7:55:57based on the clock at any given time. So
  10896. 7:56:00instead of it needing to be explicitly
  10897. 7:56:01set by anything, we already know that
  10898. 7:56:04each instruction is going to use five
  10899. 7:56:05clock cycles. when we get to clock cycle
  10900. 7:56:08number five and do our work, we should
  10901. 7:56:10reset to zero because now we're counting
  10902. 7:56:12for the the start of the next
  10903. 7:56:13instruction. So, we kind of have this
  10904. 7:56:16counter circuit
  10905. 7:56:18um counter and it has this same plus one
  10906. 7:56:25except it's input signal is just always
  10907. 7:56:28going to be the clock.
  10908. 7:56:33So I'll highlight that too for sake of
  10909. 7:56:36consistency.
  10910. 7:56:40So the output for our counter our
  10911. 7:56:43counter is actually going to be three
  10912. 7:56:44bits. The reason is um we need to count
  10913. 7:56:48each instruction takes five steps. We
  10914. 7:56:49need to count 1 2 3 4 5. If we do that
  10915. 7:56:52in binary, you know, it starts with 0 0
  10916. 7:56:550
  10917. 7:56:581 0 0 1 0 0 1 1 and then 1 0 0. These
  10918. 7:57:05are the uh five different values we use
  10919. 7:57:07to denote whatever step we're currently
  10920. 7:57:09executing of the current instruction
  10921. 7:57:11we're on. So not different instructions.
  10922. 7:57:13Instruction is an entire one of these
  10923. 7:57:15commands right here. But to implement
  10924. 7:57:17each of these instructions, we use five
  10925. 7:57:19steps. and five steps would be five
  10926. 7:57:22clock cycles here because we're counting
  10927. 7:57:24up one every clock cycle. And so this is
  10928. 7:57:27three bits. So, uh that was a little bit
  10929. 7:57:31of a side tangent, but that is to say
  10930. 7:57:35that uh this counter goes into our um
  10931. 7:57:40you know, it's part of our control
  10932. 7:57:41brains. You know, we'll just call it
  10933. 7:57:43like step number represents the step
  10934. 7:57:45number. What step are we on of the
  10935. 7:57:47current instruction? So each time we're
  10936. 7:57:49setting lines, it matters how far along
  10937. 7:57:51we are in our current instruction. So
  10938. 7:57:53that's also something we've been
  10939. 7:57:54interpreting as an input to our control.
  10940. 7:57:57And we haven't explicitly designed this
  10941. 7:57:58as a counter. But you know, when we've
  10942. 7:58:00been doing it, we've been looking at the
  10943. 7:58:02timing sequences, looking at, hey, the
  10944. 7:58:04clock's low and it's about to go high or
  10945. 7:58:06vice versa and then setting control
  10946. 7:58:07lines appropriately. as we shift the uh
  10947. 7:58:10discretion away from us mainly punching
  10948. 7:58:13lines uh into the computer, the computer
  10949. 7:58:16needs a way of knowing what step number
  10950. 7:58:18is on. Is it on one two three four five?
  10951. 7:58:20And so the easiest way to do that is
  10952. 7:58:22just to have a register that is a
  10953. 7:58:23counter and then feed that as input to
  10954. 7:58:26our control. And our control then can
  10955. 7:58:27say rather than us as a human on step
  10956. 7:58:30five, we could just look at what the
  10957. 7:58:31output is on that step number line to
  10958. 7:58:34figure out where we are in terms of
  10959. 7:58:35execution of the current instruction. So
  10960. 7:58:39that's the second step or sorry second
  10961. 7:58:42input step number
  10962. 7:58:45and that's three bits
  10963. 7:58:49and then the last thing that might you
  10964. 7:58:52might think that's all but recall the uh
  10965. 7:58:56two jump the two conditional jump
  10966. 7:58:58statements um those perform a jump
  10967. 7:59:02operation based on the last output of
  10968. 7:59:06the alou
  10969. 7:59:07which is stored in this flags register.
  10970. 7:59:09So that flags register we also need to
  10971. 7:59:12know what's in it for us to make our
  10972. 7:59:14decisions. Um so that's also something
  10973. 7:59:16we've been considering as input when
  10974. 7:59:18we've been implementing the control
  10975. 7:59:20ourselves. And so you know we have the
  10976. 7:59:22zero flag which is one bit and then the
  10977. 7:59:26carry flag which is one bit.
  10978. 7:59:30And so these are all the inputs we've
  10979. 7:59:33needed to then look at a timing diagram
  10980. 7:59:35and figure out what inputs uh or what
  10981. 7:59:38control things need to be set at any
  10982. 7:59:39given time. So what are the outputs now?
  10983. 7:59:46So any step we've been looking at, hey,
  10984. 7:59:49this is the instruction in the
  10985. 7:59:50instruction register. Here's what step
  10986. 7:59:52number we're on. And for two of our
  10987. 7:59:54instructions, our conditional jump uh
  10988. 7:59:55instructions, we're also looking at this
  10989. 7:59:58flag register. we for the most part we
  10990. 8:00:00aren't really looking at it but we you
  10991. 8:00:01know we are for two instructions. So it
  10992. 8:00:02turns out for the bulk of them we ignore
  10993. 8:00:04them but you know we can't just turn
  10994. 8:00:06things on and off for what our control
  10995. 8:00:08looks at when it's the brains. Uh so
  10996. 8:00:11we'll have to just have the input be
  10997. 8:00:12fixed all the time. You know you
  10998. 8:00:13couldn't pick up a wire and throw it
  10999. 8:00:14away. That defeat the purpose because
  11000. 8:00:16then you'd have a human be intervening.
  11001. 8:00:18um the control itself will just know
  11002. 8:00:21like if you're doing a load a
  11003. 8:00:23instruction or a subtract instruction or
  11004. 8:00:26something of the sort and it doesn't
  11005. 8:00:28matter what's in those flag uh in that
  11006. 8:00:30in that flag register it has no effect
  11007. 8:00:32on the output of uh how you implement
  11008. 8:00:35that instruction.
  11009. 8:00:36So then that's what we've been looking
  11010. 8:00:39at. I'm calling that as our input to
  11011. 8:00:40make each of the decisions as to what
  11012. 8:00:43outputs we set. And so outputs are
  11013. 8:00:46pretty much uh if I've drawn this
  11014. 8:00:47correctly and completely should just be
  11015. 8:00:50everything that's yellow here. So you
  11016. 8:00:53know we've been serving as the human but
  11017. 8:00:55ideally the control would look at the
  11018. 8:00:57state of the machine based on these
  11019. 8:00:59inputs and then just know what outputs
  11020. 8:01:02to set. So let's actually go ahead and
  11021. 8:01:05enumerate over all of our outputs. Um
  11022. 8:01:08and then we can you know tie it together
  11023. 8:01:10in a more concrete place. So we just
  11024. 8:01:12discussed the halt line that'll stop the
  11025. 8:01:15clock from executing. So the moment the
  11026. 8:01:17computer puts that out, it's actually
  11027. 8:01:18doomed. Uh the moment it puts that out
  11028. 8:01:21and um means the clock cannot make any
  11029. 8:01:23further progress, which means the state
  11030. 8:01:26of the machine just freezes pretty much.
  11031. 8:01:28Um then uh looking below it, we have the
  11032. 8:01:33memory address register in I'll just
  11033. 8:01:35call that, you know,
  11034. 8:01:37mar in. I don't want to write the full
  11035. 8:01:39thing out. After that uh we had RAM in
  11036. 8:01:44which reads the value
  11037. 8:01:47uh on the bus
  11038. 8:01:49into the whatever address is stored in
  11039. 8:01:51the memory address register. Then we
  11040. 8:01:53have RAM out. And to recall what RAM out
  11041. 8:01:57does is whatever address is stored in
  11042. 8:02:00the memory address register, it'll
  11043. 8:02:01always be looked up in RAM and that
  11044. 8:02:04value will always be on this side of the
  11045. 8:02:06line. When we toggle RAM out, it'll just
  11046. 8:02:08let that value flow to the bus. So in
  11047. 8:02:10short, RAM outputs whatever value is in
  11048. 8:02:13RAM at the address denoted by the memory
  11049. 8:02:14address register onto the bus.
  11050. 8:02:17Then we have uh this instruction
  11051. 8:02:19register instruction register out
  11052. 8:02:22because sometimes we want to jump to a
  11053. 8:02:24specific address. So we need to get that
  11054. 8:02:26argument of the address or anytime we
  11055. 8:02:28just need to put the argument uh for an
  11056. 8:02:30instruction that has an argument onto
  11057. 8:02:31the bus you know we write instruction
  11058. 8:02:33register out
  11059. 8:02:36and then we also have instruction
  11060. 8:02:38register in because we obviously need to
  11061. 8:02:39get whatever instruction we're executing
  11062. 8:02:40from RAM into that register. So IR in
  11063. 8:02:46and
  11064. 8:02:49after that um we can jump to the top of
  11065. 8:02:52our computer.
  11066. 8:02:54Uh for the ALOU we have register A in
  11067. 8:02:57and then register A out. Register A in
  11068. 8:02:59will just latch whatever is on the bus
  11069. 8:03:01into register A and A out will write
  11070. 8:03:03whatever is on the in register A to the
  11071. 8:03:05bus.
  11072. 8:03:07So we have A in and I'll denote that A
  11073. 8:03:10out.
  11074. 8:03:12Then we have
  11075. 8:03:14the uh alou out. Whatever the output is,
  11076. 8:03:17whatever the current output of the alou
  11077. 8:03:18is, write that onto the bus.
  11078. 8:03:21AL out.
  11079. 8:03:25Then we have uh this add subtract line.
  11080. 8:03:29If we're doing it for any instruction
  11081. 8:03:31that's not an add or a subtract, it
  11082. 8:03:32doesn't matter what it is because we
  11083. 8:03:33don't care what's what the output of the
  11084. 8:03:35ALU is. We're not going to interpret it.
  11085. 8:03:37Um, for add and subtract is the only
  11086. 8:03:39time the value on this line matters.
  11087. 8:03:41When we're adding, we just leave it as a
  11088. 8:03:43zero. When we're subtracting, uh, is the
  11089. 8:03:45only time we'd ever need to set it to a
  11090. 8:03:47one.
  11091. 8:03:51So, we can just call that sub uh, sub
  11092. 8:03:54because it's always going to be zero.
  11093. 8:03:56The only time we need to turn it on is
  11094. 8:03:57for doing subtraction. So, let's just
  11095. 8:03:58call it sub subtraction. Um then we have
  11096. 8:04:04uh B register in and B register out. It
  11097. 8:04:09turns out for the instructions we've
  11098. 8:04:11defined here and you'll see this as we
  11099. 8:04:13I'll go through and we'll implement each
  11100. 8:04:14of these instructions
  11101. 8:04:16B does not ever actually need to write
  11102. 8:04:18onto the bus. If we were to expand the
  11103. 8:04:21computer then it would. So I'll actually
  11104. 8:04:23for the sake of keeping this diagram as
  11105. 8:04:25minimal as possible erase that. But you
  11106. 8:04:28can imagine we could add more
  11107. 8:04:29instructions here. We talked about this
  11108. 8:04:31like not too long ago. You know the out
  11109. 8:04:33instruction for example writes whatever
  11110. 8:04:35is in the register A to the seven
  11111. 8:04:38segment display. We could have a similar
  11112. 8:04:39command for outb just takes whatever's
  11113. 8:04:42in the register B and writes that to the
  11114. 8:04:44seven segment display. So uh it's not
  11115. 8:04:46like it's useless but for the
  11116. 8:04:48instruction set we've defined here it's
  11117. 8:04:50actually not necessary. So I'll just
  11118. 8:04:52keep it out for uh simplicity. But we do
  11119. 8:04:54have BN.
  11120. 8:04:56I'll denote that be I and then we're
  11121. 8:04:59just talking about the output register.
  11122. 8:05:00So we should uh talk about that. Uh the
  11123. 8:05:03output register in it's called display
  11124. 8:05:05in here. I'll just call it oi. Output
  11125. 8:05:07register in. Then um I I should have
  11126. 8:05:12started up here. We have the program
  11127. 8:05:14counter. So we we want program counter
  11128. 8:05:17in if we're going to read a value uh or
  11129. 8:05:20increment the program counter. Sorry.
  11130. 8:05:22and then uh program counter out
  11131. 8:05:28for writing that program counter uh
  11132. 8:05:30value onto the bus.
  11133. 8:05:36So we have counter
  11134. 8:05:41increment
  11135. 8:05:44and then counter out.
  11136. 8:05:50And as I'm writing this, I realize we
  11137. 8:05:53have a little mistake here where uh we
  11138. 8:05:57know for our jump instructions. What the
  11139. 8:06:00jump instruction does is it tells the
  11140. 8:06:02computer, hey, like if the program
  11141. 8:06:03counter says the next instruction
  11142. 8:06:05executes at address five,
  11143. 8:06:07ignore it and jump to actually this
  11144. 8:06:09other address. So right now we actually
  11145. 8:06:12can't support that because our PC inc
  11146. 8:06:15will actually only increment the program
  11147. 8:06:17counter. But we also want
  11148. 8:06:20um a way to read a value from the bus
  11149. 8:06:23into the program counter. So you can
  11150. 8:06:25imagine we could just update our little
  11151. 8:06:27circuit here because it's just a little
  11152. 8:06:28alou. um and one line is doing the
  11153. 8:06:32increment. Uh another line could
  11154. 8:06:34actually just read a value into the
  11155. 8:06:36underlying register that's the operand
  11156. 8:06:38of the alou. And so we can call that um
  11157. 8:06:41J. We'll call that jump because that's
  11158. 8:06:44the only time we ever need to to write a
  11159. 8:06:46direct value into the program counter.
  11160. 8:06:49So sorry for the confusion there. We
  11161. 8:06:51actually weren't fully complete. Jump.
  11162. 8:06:54And then the very last output we might
  11163. 8:06:56ever set uh is back here. Whenever we do
  11164. 8:06:59an add or subtract, if we do an alou
  11165. 8:07:02operation, we need to update the flags
  11166. 8:07:04register because if there's an add or
  11167. 8:07:06subtract and then there's either a jump
  11168. 8:07:07carry or a jump zero afterwards, uh that
  11169. 8:07:10means we would need to have known what
  11170. 8:07:12the output of the last ALOU operation
  11171. 8:07:14was. And so basically whenever we do an
  11172. 8:07:16ALOU operation we will terminate it or
  11173. 8:07:18just along the process look at the
  11174. 8:07:21output and conditionally set these jump
  11175. 8:07:23and carry flags which we talked about
  11176. 8:07:24how to build those sep those um
  11177. 8:07:27circuits. They're relatively simple.
  11178. 8:07:30And so um these are all of our inputs
  11179. 8:07:33and all of our outputs and I've kind of
  11180. 8:07:35listed them out and we've talked about
  11181. 8:07:37these uh so I'll stop explaining them
  11182. 8:07:38because I think most of these should be
  11183. 8:07:40clear by now. But the uh the crux of all
  11184. 8:07:45of this is I claimed uh several times
  11185. 8:07:48now that you already know how to build
  11186. 8:07:50this control. And the reason is is
  11187. 8:07:54because the input
  11188. 8:07:57i.e. the combination of whatever
  11189. 8:07:58instruction you're executing, what step
  11190. 8:08:01of that instruction you're on, and then
  11191. 8:08:04maybe the ALU flags will tell you
  11192. 8:08:07exactly
  11193. 8:08:09everything you need to know to determine
  11194. 8:08:11which output lines to set. So basically
  11195. 8:08:14we know that output
  11196. 8:08:19is clearly some function of the input
  11197. 8:08:23and we also know that each input
  11198. 8:08:26combination has a well- definfined
  11199. 8:08:28output combination because we've defined
  11200. 8:08:30every instruction
  11201. 8:08:32and we know that the program counter uh
  11202. 8:08:35we also know that each instruction our
  11203. 8:08:36counter over here is only going to go up
  11204. 8:08:38to five up to one 0 0 uh binary Okay.
  11205. 8:08:44And then we know that the zero flag and
  11206. 8:08:47carry flag can either be zeros or ones.
  11207. 8:08:50And so basically every time there's an
  11208. 8:08:53input, the output will be the same. So
  11209. 8:08:56output is always the same for the same
  11210. 8:09:00input. So f of input equals output will
  11211. 8:09:03always be true. Which means we basically
  11212. 8:09:06have a deterministic input output
  11213. 8:09:08sequencing here. The output can entirely
  11214. 8:09:11be computed strictly looking at these
  11215. 8:09:13input flags. And for a given combination
  11216. 8:09:16of input flags, the output will never
  11217. 8:09:19change. It'll always be the same. And so
  11218. 8:09:22how we can encode this is the same way
  11219. 8:09:25we built out the seven segment display.
  11220. 8:09:28The readon memory that took in that we
  11221. 8:09:31built earlier took in a value on a
  11222. 8:09:34register denoted by eight bits and
  11223. 8:09:37figured out which of the output lines
  11224. 8:09:40for these four dis for I mean we built a
  11225. 8:09:42one segment display or one unit of a
  11226. 8:09:44seven segment display figured out which
  11227. 8:09:46of the seven lines need to be set and so
  11228. 8:09:48we had like a eight input um followed by
  11229. 8:09:52I think that's four five six seven
  11230. 8:09:54dimension output um and we said hey you
  11231. 8:09:57So depending on what the input is for
  11232. 8:09:59each of the input values we know oh
  11233. 8:10:02sorry this also not uh this was whatever
  11234. 8:10:05we could fit in in
  11235. 8:10:08two digits I think it was four bits
  11236. 8:10:10sorry but points taken here that um I'll
  11237. 8:10:14say four or eight I don't remember off
  11238. 8:10:15the top of my head but the point is
  11239. 8:10:17taken that for this readonly memory it's
  11240. 8:10:19core service was if you have a
  11241. 8:10:22effectively would like to build a lookup
  11242. 8:10:24table so for any for every combination
  11243. 8:10:27of inputs has a clear output and then
  11244. 8:10:31for any input combination the output
  11245. 8:10:32should always be the same. Those two
  11246. 8:10:35properties are what we discussed that
  11247. 8:10:36made a readonly memory appropriate here.
  11248. 8:10:39And so it turns out our control
  11249. 8:10:43is none other
  11250. 8:10:48than
  11251. 8:10:52a read only memory. It is a ROM.
  11252. 8:10:56and it takes in
  11253. 8:10:594 + 3 + 2, that's nine inputs.
  11254. 8:11:05And then it has however many outputs we
  11255. 8:11:08have right here. And
  11256. 8:11:12this is pretty powerful because it means
  11257. 8:11:14if we set up this control ROM correctly,
  11258. 8:11:17it means that the computer can be fully
  11259. 8:11:20autonomous. And for every instruction,
  11260. 8:11:23it'll start, it'll take the value in the
  11261. 8:11:26program counter,
  11262. 8:11:27look it up in RAM, get it in the
  11263. 8:11:30instruction register itself, and then
  11264. 8:11:32the control knows what values in the
  11265. 8:11:34instru what instruction we're executing
  11266. 8:11:36on. Uh, it also has this counter that's
  11267. 8:11:38telling it what step of the instruction
  11268. 8:11:39we're on. And then it can keep setting
  11269. 8:11:41the correct outputs to execute the
  11270. 8:11:43current command. And then since we have
  11271. 8:11:46a line that increments the program
  11272. 8:11:47counter when the current instruction is
  11273. 8:11:49finally done, we can just repeat the
  11274. 8:11:51same process because now our program
  11275. 8:11:53counter points to the next address in
  11276. 8:11:54memory. And this is the crux of of tying
  11277. 8:11:58the computer together. You have now gone
  11278. 8:12:01from individual components to actually a
  11279. 8:12:04full computer that's uh fully
  11280. 8:12:06independent and can execute instructions
  11281. 8:12:09in comma uh sequentially and has the
  11282. 8:12:12ability to do complex logic where it's
  11283. 8:12:14not just instru uh not just executing
  11284. 8:12:17instructions top down but it has the
  11285. 8:12:19ability to jump to other addresses in
  11286. 8:12:21memory i.e interrupt its own control
  11287. 8:12:23flow. And not only can it jump anywhere
  11288. 8:12:26in memory, it can also do conditional
  11289. 8:12:28jumping, which means your programs that
  11290. 8:12:30you can publish into this computer can
  11291. 8:12:32have some some amount of logic that
  11292. 8:12:34requires uh a forking control. So if do
  11293. 8:12:38some operation, if the output of that
  11294. 8:12:40operation was this, do something else or
  11295. 8:12:43continue on the current path. Um, so
  11296. 8:12:46this is pretty cool, but you're probably
  11297. 8:12:48scratching your head a little bit where
  11298. 8:12:50uh we walk through the load a command.
  11299. 8:12:51And so we we looked at a timing diagram,
  11300. 8:12:53but we we didn't actually see what the
  11301. 8:12:55inputs and outputs would look like for
  11302. 8:12:56this control ROM. And we also have a
  11303. 8:12:58whole bunch of other instructions we
  11304. 8:12:59haven't talked at at all. So to tie this
  11305. 8:13:02together, what I'm actually going to do
  11306. 8:13:04is we're going to go build out this ROM
  11307. 8:13:07um and look at what the output should be
  11308. 8:13:10for every possible input combination.
  11309. 8:13:13And you've probably seen enough of me
  11310. 8:13:15drawing to know that my handwriting is
  11311. 8:13:17bad. So I'm going to switch to my
  11312. 8:13:18computer and we're just going to do it
  11313. 8:13:19on a Google Sheets and keep it simple.
  11314. 8:13:21um in neat there. But um once we do
  11315. 8:13:26that, we'll jump back to this one more
  11316. 8:13:28time and we can reiterate how this all
  11317. 8:13:30sums together. But this is pretty
  11318. 8:13:32powerful. Um we are now like I think
  11319. 8:13:35just like shy north of eight hours of
  11320. 8:13:38this video and if you did some
  11321. 8:13:40handwaving or if you somehow understand
  11322. 8:13:42how you could set up the control
  11323. 8:13:43entirely by yourself, you know how a
  11324. 8:13:46computer works. Now this is actually it.
  11325. 8:13:48This is the true fundamentals.
  11326. 8:13:49Obviously, this is a toy computer, but
  11327. 8:13:51this is a fully functioning independent
  11328. 8:13:53computer. Uh, and that's pretty
  11329. 8:13:55powerful, but we're not quite done yet
  11330. 8:13:57because I haven't explained how this
  11331. 8:13:58control ROM works. So, what we're going
  11332. 8:14:00to do next, uh, I said we'll swap to my
  11333. 8:14:02computer. We'll build out what the
  11334. 8:14:03inputs and outputs for this ROM should
  11335. 8:14:05look like, and then, um, we'll come back
  11336. 8:14:08here, tie it all together, and then
  11337. 8:14:11we'll talk about how we go from like
  11338. 8:14:12this to an actual more modern computer.
  11339. 8:14:14Okay. So what I've gone ahead and done
  11340. 8:14:17is constructed a table that shows what
  11341. 8:14:20our hypothetical RAM would look like.
  11342. 8:14:23And so let me walk through the structure
  11343. 8:14:25here. So first um our inputs are going
  11344. 8:14:30to be uh this column through this column
  11345. 8:14:35and then our outputs are going to be
  11346. 8:14:36this column these columns. So remember
  11347. 8:14:39our input we had the first op code which
  11348. 8:14:42was the leftmost four bits of the um
  11349. 8:14:48instruction register that tells us which
  11350. 8:14:50argument we're currently executing or
  11351. 8:14:52should be executing. Then we have the
  11352. 8:14:54step number. We had that special counter
  11353. 8:14:57that was uh specifically for the control
  11354. 8:15:00that we just added in last minute that
  11355. 8:15:02counts zero and then up to uh it counts
  11356. 8:15:05up to four in binary or realistically
  11357. 8:15:09five um
  11358. 8:15:13sorry yeah five different operations
  11359. 8:15:14zero through four and then that's here.
  11360. 8:15:18Then we also had uh the zero flag and
  11361. 8:15:20the carry flag for our two conditional
  11362. 8:15:24jump instructions where we need to see
  11363. 8:15:25that. So these are our inputs. Recall
  11364. 8:15:28this is still binary. It's a ROM. So all
  11365. 8:15:31of our inputs are going to be zeros and
  11366. 8:15:32ones as will all of our outputs.
  11367. 8:15:36Then our outputs were all the uh control
  11368. 8:15:39lines that we had highlighted yellow in
  11369. 8:15:40our original diagram. And uh we'll walk
  11370. 8:15:44through this again real quick. So I can
  11371. 8:15:45make this a little bigger for now. Um,
  11372. 8:15:48I've uh added like acronyms here just
  11373. 8:15:51because it'll be easier to say rather
  11374. 8:15:53than having to specify, you know, memory
  11375. 8:15:55address register in, you know, MI is
  11376. 8:15:57probably easier to say once we once we
  11377. 8:16:00work through these because these will
  11378. 8:16:01get a bit repetitive. But HLT is halt.
  11379. 8:16:04Remember, this stops the clock. MI
  11380. 8:16:06stands for latch a value from the bus
  11381. 8:16:08into the memory address register. That's
  11382. 8:16:10a four-bit register. recall cuz all of
  11383. 8:16:12our instructions and arguments are or so
  11384. 8:16:15all of our RAM addresses and arguments
  11385. 8:16:18all of our addresses are just four bits.
  11386. 8:16:21RA stands for RAM in reads whatever
  11387. 8:16:24value is on the bus into RAM at whatever
  11388. 8:16:28value address is denoted in the memory
  11389. 8:16:30address register. RAM out, it'll say
  11390. 8:16:33whatever the uh memory address register
  11391. 8:16:35is, that's an address. Look up that
  11392. 8:16:38value in RAM and write it out onto the
  11393. 8:16:39bus. Instruction register out IO. Take
  11394. 8:16:43the value that is in the instruction
  11395. 8:16:45register and put it onto the bus. I I is
  11396. 8:16:48instruction register in. So take
  11397. 8:16:50whatever value is on the bus and read it
  11398. 8:16:52into the instruction register. 8 bit
  11399. 8:16:54register. By the way, our arguments, our
  11400. 8:16:56op codes are four bits and our arguments
  11401. 8:16:58are four bits. But the register itself
  11402. 8:16:59is eight bits because an instruction has
  11403. 8:17:01an op code and may be an argument. So 4
  11404. 8:17:03plus 4 means we need at least eight bits
  11405. 8:17:05to represent a maximally utilized
  11406. 8:17:08instruction.
  11407. 8:17:09AI stands for read a 8 bit value from
  11408. 8:17:13the bus into the A register. AO says A
  11409. 8:17:17out. It say put whatever 8 bit value is
  11410. 8:17:20in the A register out onto the bus.
  11411. 8:17:23EO um stands for ALU out. I actually am
  11412. 8:17:28not sure why it is EO. Um, I think, you
  11413. 8:17:31know, we already have AO used, so uh I
  11414. 8:17:35think they they could have found a
  11415. 8:17:36better acronym. This was the way I was
  11416. 8:17:37taught, so I'll stick with it just
  11417. 8:17:39because my mind is so wired to say EO
  11418. 8:17:42that if I tried to change that letter,
  11419. 8:17:44I'd probably accidentally still say EO,
  11420. 8:17:46and that would confuse you. Um, SU is
  11421. 8:17:50subtract. This is the line on the ALU
  11422. 8:17:52that dictates if the output is either
  11423. 8:17:54going to be register A plus register B
  11424. 8:17:57or if it's register A reg minus register
  11425. 8:17:59B. Um BI stands for B in read whatever 8
  11426. 8:18:05bit value is onto the B on the bus into
  11427. 8:18:07the B register.
  11428. 8:18:10Oi stands for output register in. So
  11429. 8:18:14whatever value is in the output register
  11430. 8:18:17or sorry whatever value is on the bus
  11431. 8:18:19read it into the output register. Then
  11432. 8:18:22CE this stands for counter enable which
  11433. 8:18:26is synonymous to incrementing the
  11434. 8:18:27program counter. So it'll add one to
  11435. 8:18:30whatever values in the program counter
  11436. 8:18:32and because of overflow we talked about
  11437. 8:18:33this earlier the program counter is a
  11438. 8:18:354-bit register. So the highest value we
  11439. 8:18:37get to is 1111. And because of what we
  11440. 8:18:40discussed earlier, when you call program
  11441. 8:18:42counter enable at that maximal value, it
  11442. 8:18:45doesn't crash the computer or anything
  11443. 8:18:46because of overflow, it'll actually
  11444. 8:18:48reset it to 000000. Which means if
  11445. 8:18:52somehow you have a a program that spans
  11446. 8:18:54your entire memory, when you get to the
  11447. 8:18:56end of it, then it'll actually
  11448. 8:18:58effectively restart it by going back to
  11449. 8:19:00address zero and starting again from the
  11450. 8:19:02top. Um, which you know, if you don't
  11451. 8:19:05have a halt instruction, that is what
  11452. 8:19:07will happen. You should probably end for
  11453. 8:19:09correctness your programs with a halt
  11454. 8:19:11instruction. But, you know, I'm not here
  11455. 8:19:13to tell you what to do and we haven't
  11456. 8:19:15made any programs yet. We'll we'll talk
  11457. 8:19:16about that a little bit too. CO stands
  11458. 8:19:19for program counter out. So, this takes
  11459. 8:19:21whatever uh four bit value in the
  11460. 8:19:23program counter and uh writes it onto
  11461. 8:19:26the bus.
  11462. 8:19:28J stands for jump. that takes uh
  11463. 8:19:31whatever uh four-bit value, the
  11464. 8:19:33rightmost four-bit value uh four-bit
  11465. 8:19:36values plural, sorry, uh reads those in
  11466. 8:19:40and that's useful for jump instructions.
  11467. 8:19:42As I'm actually looking at the diagram I
  11468. 8:19:44drew, I realized my diagram makes it
  11469. 8:19:46appear as though it's reading eight bits
  11470. 8:19:47from the bus. That's a uh incorrect
  11471. 8:19:50statement on my part. I apologize. That
  11472. 8:19:51should be four bits. And last but not
  11473. 8:19:54least is FI which is the flags register
  11474. 8:19:57that we discussed that looks at
  11475. 8:19:59continually [snorts]
  11476. 8:20:00uh looks at the value of the output of
  11477. 8:20:03the ALOU and and determines if the
  11478. 8:20:05output is either carried overflowed or
  11479. 8:20:08if that value is equal to zero and then
  11480. 8:20:10whenever we do an ALU operation we need
  11481. 8:20:11to pulse that to latch the value in.
  11482. 8:20:14That was a mouthful and that's very
  11483. 8:20:15similar to what we discussed. Um, and so
  11484. 8:20:19basically now what we're going to do is,
  11485. 8:20:24uh, implement all of our instructions
  11486. 8:20:26and we're going to start with load A. So
  11487. 8:20:29from the table we originally had, we
  11488. 8:20:31said load A's op code was 00001.
  11489. 8:20:34And so if I look at the inputs,
  11490. 8:20:37001
  11491. 8:20:39um, and you might be wondering, well,
  11492. 8:20:41hey, hey, what's up with, you know, all
  11493. 8:20:43the outputs up until there? So basically
  11494. 8:20:46what's interesting here is we just know
  11495. 8:20:48that those are never going to be hit
  11496. 8:20:51because assuming the user puts in a
  11497. 8:20:53correct program there's no instruction
  11498. 8:20:55we define that has the op code 000000.
  11499. 8:20:59So conceptually that should never end up
  11500. 8:21:01in the instruction register itself. But
  11501. 8:21:04uh we discussed a property of the ROM
  11502. 8:21:06and that it's for every input
  11503. 8:21:08combination there must be a
  11504. 8:21:09deterministic output. Uh and it turns
  11505. 8:21:12out we can still guarantee that. It just
  11506. 8:21:14doesn't matter what it is because we
  11507. 8:21:15know that like the input will never be
  11508. 8:21:17this because this is a malformed op
  11509. 8:21:19code. Uh the output never really matters
  11510. 8:21:22because it it conceptually should not be
  11511. 8:21:23reachable. You know, it'd be user error
  11512. 8:21:25if the user put in an instruction into
  11513. 8:21:29RAM uh and had the op code 000000.
  11514. 8:21:33That'd be a bad instruction. But if that
  11515. 8:21:35happens, it doesn't really matter what
  11516. 8:21:37happens because we don't support that
  11517. 8:21:38use case. So pretty much the output for
  11518. 8:21:41all of these lines does not matter. Um,
  11519. 8:21:43and what we'll default to is that if a
  11520. 8:21:46line is not set in this diagram, uh,
  11521. 8:21:48there's going to be a lot more zeros
  11522. 8:21:49than ones at times, most things will not
  11523. 8:21:52be set. So, I'm just going to say if I
  11524. 8:21:54leave something blank, it means zero for
  11525. 8:21:56these first uh, entries. Actually, you
  11526. 8:21:59could actually totally get by
  11527. 8:22:02and uh, press in uh, ones if you wanted
  11528. 8:22:05to as well. The only thing you just want
  11529. 8:22:07to be a little careful of is if you you
  11530. 8:22:08don't want to trigger this halt line
  11531. 8:22:10because we saw earlier that if you did
  11532. 8:22:13pulse that then the program would stop.
  11533. 8:22:15Um it wouldn't matter cuz again we don't
  11534. 8:22:18support the user putting in this op code
  11535. 8:22:20but you know if we wanted to be a little
  11536. 8:22:21bit defensive. This is not the uh the
  11537. 8:22:24worst thing to do here. So um I'm not
  11538. 8:22:27going to like read this out. Uh we just
  11539. 8:22:29kind of know that like hey these like
  11540. 8:22:31these addresses like this is not
  11541. 8:22:33reachable. Um so I you know I could
  11542. 8:22:36denote that here but most a lot of
  11543. 8:22:38things you'll see are unreachable. Um
  11544. 8:22:43are you know there's we don't define all
  11545. 8:22:45of our op codes as we discussed earlier.
  11546. 8:22:47You know we have two to the four I think
  11547. 8:22:49that's 16 possible op codes represented
  11548. 8:22:52in four bits and we just do not have 16
  11549. 8:22:54instructions and that's totally fine. So
  11550. 8:22:56a good amount of our space here is
  11551. 8:22:57actually going to be unreachable um with
  11552. 8:23:00a technical asterisk that you could
  11553. 8:23:02reach it if you just put in a incorrect
  11554. 8:23:03program or incorrect instruction into
  11555. 8:23:06RAM. But um we start with
  11556. 8:23:11uh this part is reachable. Sorry, that's
  11557. 8:23:13not true. We start with uh op code 00001
  11558. 8:23:18which was this load a. And uh we'll also
  11559. 8:23:21assume that our our flags uh we're just
  11560. 8:23:24going to ignore those for now because we
  11561. 8:23:25know it doesn't matter for this
  11562. 8:23:26instruction. So it turns out as we
  11563. 8:23:30discussed uh the very first thing that
  11564. 8:23:32happened for load A is we actually don't
  11565. 8:23:35even know we're executing the load A
  11566. 8:23:37yet. So,
  11567. 8:23:40um, what we actually need to do is get,
  11568. 8:23:44uh, because it's the first step of the
  11569. 8:23:46instruction, that's how we start, our
  11570. 8:23:48step counter starts at zero, and that's
  11571. 8:23:50how we know we're executing a new
  11572. 8:23:52instruction. And so, we don't even know
  11573. 8:23:54what we're executing yet. So, basically,
  11574. 8:23:56we need to take the instruction
  11575. 8:23:59um, we need to figure out what the
  11576. 8:24:00instruction is and get it into the
  11577. 8:24:01instruction register. And the way we do
  11578. 8:24:03that is we start with the program
  11579. 8:24:04counter because the program counter has
  11580. 8:24:06the address of the instruction that
  11581. 8:24:07we're about to execute. So what we
  11582. 8:24:09actually do to start our instruction is
  11583. 8:24:12put the program counter value onto the
  11584. 8:24:14bus and then at the same time read that
  11585. 8:24:17into the memory address register.
  11586. 8:24:20And so then this is what happens on our
  11587. 8:24:22first clock cycle. The clock will go
  11588. 8:24:24from low to high. Uh we set these values
  11589. 8:24:28um you know this is our output. So our
  11590. 8:24:30steps our step counter our little
  11591. 8:24:32incrementer circuit that connects just
  11592. 8:24:34to the control will start at zero. And
  11593. 8:24:37so we know that's the start of an
  11594. 8:24:38instruction. So our a our uh control
  11595. 8:24:41should just output these two lines
  11596. 8:24:43because this is what we need to do to
  11597. 8:24:44get the value from the program counter
  11598. 8:24:46into the memory address register so we
  11599. 8:24:48can look it up in RAM. And then the
  11600. 8:24:50clock is low this entire time. The clock
  11601. 8:24:53will then go from 0 to one which will
  11602. 8:24:55latch in the value to the memory address
  11603. 8:24:57register. And then because our our
  11604. 8:24:59little incrementer is connected to the
  11605. 8:25:02clock, it'll have the side effect of
  11606. 8:25:03automat automatically incrementing it,
  11607. 8:25:05which means we're we end up at the row
  11608. 8:25:07below here because our op code's not
  11609. 8:25:10changing and we're ignoring these flags
  11610. 8:25:11for now. They're not changing. Um, and
  11611. 8:25:14so that's pretty neat. And so then we're
  11612. 8:25:16looking, okay, when this we know the
  11613. 8:25:18first step was getting the address from
  11614. 8:25:20the program counter into their memory
  11615. 8:25:22address register. Now that we've done
  11616. 8:25:24that, uh, we want to take the output
  11617. 8:25:26from RAM because that's the actual
  11618. 8:25:28instruction we want to execute and then
  11619. 8:25:30put it into the instruction register. So
  11620. 8:25:32what I do is RAM out
  11621. 8:25:36and instruction register in.
  11622. 8:25:40And um, we're going to do one other
  11623. 8:25:43thing here. We're just going to choose.
  11624. 8:25:45We talked about this earlier. At some
  11625. 8:25:47step in each instruction, we need to
  11626. 8:25:50increment our program counter so that we
  11627. 8:25:52get to the next instruction. We're going
  11628. 8:25:54to do that right here.
  11629. 8:25:56Um, we're going to choose the second
  11630. 8:25:58step. It could have happened at any of
  11631. 8:26:00the next steps after this before the
  11632. 8:26:01counter over here resets to zero. I'd
  11633. 8:26:04rather just get it done early so we
  11634. 8:26:06don't forget about it. Um, and then so
  11635. 8:26:09what what effectively happens is the
  11636. 8:26:11clock will pulse. it'll go from low to
  11637. 8:26:13high and that'll take the value that's
  11638. 8:26:15on on the bus which is the instruction
  11639. 8:26:17we actually want to execute and it's
  11640. 8:26:19going to latch it into the instruction
  11641. 8:26:21register and then also as a byproduct of
  11642. 8:26:23what we defined uh we've incremented the
  11643. 8:26:25program counter but uh that that's a
  11644. 8:26:27different thing than this this is this
  11645. 8:26:28is a step counter used to used as a
  11646. 8:26:31counter just for control the program
  11647. 8:26:33counter is actually keeping track of
  11648. 8:26:35what instruction the computer should
  11649. 8:26:36execute next um but that has no side
  11650. 8:26:39effect because we actually don't look at
  11651. 8:26:41the program counter until we get to the
  11652. 8:26:43next instruction. It's useless for us
  11653. 8:26:45for now, for this instruction too
  11654. 8:26:47specifically. That's not true for some
  11655. 8:26:49of the jump instructions uh that
  11656. 8:26:51actually need to modify the program
  11657. 8:26:52counter, but we will get there. So now
  11658. 8:26:56that we've loaded the value into load A,
  11659. 8:26:59we've loaded load A into the instruction
  11660. 8:27:01register. Sorry. Now, uh the control
  11661. 8:27:04unit would get this as its input. Um
  11662. 8:27:07this would be the op code. And so it it
  11663. 8:27:09finally know after these first two clock
  11664. 8:27:11cycles, hey, this is the the op code
  11665. 8:27:14line is actually saying
  11666. 8:27:16um what instruction we're going to be
  11667. 8:27:18executing. And that's pretty cool. Um it
  11668. 8:27:23turns out as we did kind of hinted at,
  11669. 8:27:25it's pretty explicit now though that for
  11670. 8:27:27the first two steps of every
  11671. 8:27:29instruction, the op code is actually not
  11672. 8:27:32going to be matter. It's not going to
  11673. 8:27:33matter. We know that the op code is
  11674. 8:27:36because it's looking at the instruction
  11675. 8:27:37register. we haven't put the right
  11676. 8:27:39instruction into the instruction
  11677. 8:27:40register. So the op code is nonsense. So
  11678. 8:27:42it actually it can ignore it and it will
  11679. 8:27:44for the first uh every time you know our
  11680. 8:27:47step number is 0000 or 001. We know that
  11681. 8:27:50for every instruction we need to do the
  11682. 8:27:52lookup of the program counter value in
  11683. 8:27:54RAM and then get that into the
  11684. 8:27:56instruction register. But once that
  11685. 8:27:58happens, we get to step 010. That's
  11686. 8:28:01binary 2. Um and now we have the op code
  11687. 8:28:06here. And so now the control knows we're
  11688. 8:28:08executing a load A. And so what did load
  11689. 8:28:11A need to do? It needed to take the
  11690. 8:28:14argument of the instruction
  11691. 8:28:18um and look that up in RAM and then
  11692. 8:28:21write that into register A. So we
  11693. 8:28:23discussed this earlier. So I'm going to
  11694. 8:28:25go you know a little quickly here. But
  11695. 8:28:27you know we need to take our our
  11696. 8:28:29instruction uh is eight bits. The left
  11697. 8:28:31four bits are this 00001 that denote the
  11698. 8:28:33load A. The right four bits are actually
  11699. 8:28:36the address that we of RAM that we want
  11700. 8:28:38to lo whose value we want to load into
  11701. 8:28:40register A. So what we do is we want to
  11702. 8:28:42put the instruction register out onto
  11703. 8:28:45the bus.
  11704. 8:28:51And then
  11705. 8:28:56we also want to read that value into the
  11706. 8:29:00memory address register.
  11707. 8:29:06And so then we the the clock will pulse
  11708. 8:29:08from low to high. The clock's low here
  11709. 8:29:10goes to high. Um and it'll latch that
  11710. 8:29:13value from the a uh from the the
  11711. 8:29:15rightmost 4 bit to the instruction
  11712. 8:29:17register into the memory address
  11713. 8:29:18register which is great. Um
  11714. 8:29:23because uh we've you know kind of looked
  11715. 8:29:26up that value now. Um oops sorry I have
  11716. 8:29:30a typo here. This should be instruction
  11717. 8:29:32register out. We take whatever values in
  11718. 8:29:34the instruction register, the right four
  11719. 8:29:35bits, put them out and read that into
  11720. 8:29:37the memory address register. And then
  11721. 8:29:40the very last thing we need to do is
  11722. 8:29:43take whatever value we get from RAM. So
  11723. 8:29:45put RAM's value back onto the bus and
  11724. 8:29:48then read it into the A register. Um,
  11725. 8:29:51and once we've done this, the clock
  11726. 8:29:53pulses, we then looked at that value
  11727. 8:29:54from RAM and stored it in the A
  11728. 8:29:55register. And we've actually completed
  11729. 8:29:58the load A. So this is the original
  11730. 8:29:59timing diagram. we drew by hand and we
  11731. 8:30:02were originally just punching those
  11732. 8:30:03values by hand saying hey you know you
  11733. 8:30:05need to put like a in to high at the
  11734. 8:30:07same time you put RAM out to high and
  11735. 8:30:09then wait for a rising clock edge so the
  11736. 8:30:11value gets latched in because of the way
  11737. 8:30:13we've defined this in a in the uh ROM
  11738. 8:30:16readon memory we don't have to do that
  11739. 8:30:18anymore the computer will do it for us
  11740. 8:30:20because we have now programmed you know
  11741. 8:30:22hey when you know you're executing a
  11742. 8:30:23load a and you're on the third step then
  11743. 8:30:26this is what you need to do and then
  11744. 8:30:28when you're on the fourth step this is
  11745. 8:30:29what you need to do. Um, and it turns
  11746. 8:30:32out for load A, we got it done in four
  11747. 8:30:34steps and we said all of our
  11748. 8:30:36instructions take five steps. So for the
  11749. 8:30:38very last step, uh, one Z on the program
  11750. 8:30:42counter, um, on the step counter, uh, we
  11751. 8:30:45don't do anything. We actually just zero
  11752. 8:30:47zero it out. Um,
  11753. 8:30:49and then we also know uh that so that
  11754. 8:30:53concludes our load a we've executed our
  11755. 8:30:56first instruction here and we we
  11756. 8:30:58specified the microode. You can see two
  11757. 8:31:01things I want to talk about here. Um the
  11758. 8:31:03first is we say first of four copies.
  11759. 8:31:06You might be wondering well why is that
  11760. 8:31:07true? you know we we discussed uh you
  11761. 8:31:11know for this step for these two it
  11762. 8:31:13doesn't matter for these these first
  11763. 8:31:14instructions the opaque doesn't matter
  11764. 8:31:16but we know hey for the first two steps
  11765. 8:31:18the instructions will always be the same
  11766. 8:31:19and then afterwards we actually resolve
  11767. 8:31:21the instruction which is a load a and so
  11768. 8:31:23we've done the the two the two clock
  11769. 8:31:25cycles worth of work to go look up some
  11770. 8:31:27value in RAM and get that into the A
  11771. 8:31:29register uh so why do we need to do this
  11772. 8:31:31four times and it turns out uh I told
  11773. 8:31:34you to ignore the zero and carry flags
  11774. 8:31:37uh and I part of me wanted to ignore
  11775. 8:31:38them for the entire video, but I didn't
  11776. 8:31:40think it'd be fair because it wouldn't
  11777. 8:31:41explain how conditional logic worked.
  11778. 8:31:43But
  11779. 8:31:45um
  11780. 8:31:48this logic, this load a instruction
  11781. 8:31:52should be the same. We would want to
  11782. 8:31:54take these same steps no matter what the
  11783. 8:31:56value is in our flags register. But we
  11784. 8:32:00actually don't know what the value in
  11785. 8:32:01the flags register is. Maybe some
  11786. 8:32:03operation happened uh some add or
  11787. 8:32:05subtract happened before this load a and
  11788. 8:32:08the carry flag is zero and the load flag
  11789. 8:32:11is one. Um well those technically are
  11790. 8:32:14input to the control logic. And so that
  11791. 8:32:18actually means when when the let's say
  11792. 8:32:20the zero flag is set to one, we aren't
  11793. 8:32:23over here and we aren't here in our
  11794. 8:32:25lookup address. We're somewhere way
  11795. 8:32:26further down. um but it still would
  11796. 8:32:29resolve to a load a um and similarly if
  11797. 8:32:31if the carry flag was one you know it
  11798. 8:32:34wouldn't change what we do here cuz we
  11799. 8:32:36don't care about it but because ROM
  11800. 8:32:39doesn't really know that ROM just like a
  11801. 8:32:40naive lookup table it doesn't we have to
  11802. 8:32:44program that four times effectively
  11803. 8:32:45because hey there's two there's two one
  11804. 8:32:48bit flags so your combinations of of of
  11805. 8:32:52the two uh zero flag and carry flag is
  11806. 8:32:54either 001
  11807. 8:32:56one 1 zero or 1 one. So for each of
  11808. 8:33:00these four combinations
  11809. 8:33:03of the flags inputs to the ROM, we'd
  11810. 8:33:05want the same out output because the
  11811. 8:33:07flags just don't matter for load A. So
  11812. 8:33:11it turns out we have four four copies of
  11813. 8:33:13load A. Um and so if I if I jumped to
  11814. 8:33:17let me first off replace these back with
  11815. 8:33:19zero.
  11816. 8:33:22If uh and if anyone ever watches this,
  11817. 8:33:24I'm happy to
  11818. 8:33:27uh include this sheet. I'm fairly
  11819. 8:33:29confident no one will ever watch this,
  11820. 8:33:31but especially not this far in, but who
  11821. 8:33:34knows? Hopefully, I'll be wrong. Um
  11822. 8:33:38if I actually go where were to jump uh
  11823. 8:33:41to the row in this table, uh it it
  11824. 8:33:46you can see that it exists. way further
  11825. 8:33:50down here would be if the zero flag is
  11826. 8:33:53zero and the carry flag is one then this
  11827. 8:33:57would be our same instruction obviously
  11828. 8:33:58the op code doesn't matter for the first
  11829. 8:33:59two clock cycles but then these three
  11830. 8:34:01would represent the execution of load a
  11831. 8:34:05uh if that if that flag was set so it
  11832. 8:34:08turns out we actually need to specify
  11833. 8:34:10that this microode four times here um so
  11834. 8:34:14you know what I've defined here
  11835. 8:34:19will need to get defined here. And you
  11836. 8:34:22can literally just paste it. And then
  11837. 8:34:24here's the the third time where zero
  11838. 8:34:27flag is one and carry flag is zero.
  11839. 8:34:32And then
  11840. 8:34:34here's the very last one where both the
  11841. 8:34:36zero flag and the carry flag are one. So
  11842. 8:34:39that's why uh yeah, that's where this
  11843. 8:34:41this first of four copies come from. And
  11844. 8:34:43then over here we'll just take some
  11845. 8:34:45notes and then we'll recap we'll recap
  11846. 8:34:47what we did here. So the very first
  11847. 8:34:49thing we did is we said the program
  11848. 8:34:51counter has the instru the address of
  11849. 8:34:54the instruction we're trying to execute.
  11850. 8:34:55So we need to go look up that value in
  11851. 8:34:58RAM. So the value in the program counter
  11852. 8:35:00is not the instruction itself. It's the
  11853. 8:35:02address of the instruction. So what we
  11854. 8:35:03want to do is we want to get the program
  11855. 8:35:05counter to the memory address register
  11856. 8:35:07in the first step. Then what we do is we
  11857. 8:35:10have that value. It's looked up in RAM.
  11858. 8:35:12we take the um the RAM and put it into
  11859. 8:35:17the instruction register. Then we also
  11860. 8:35:20incremented the program counter. So
  11861. 8:35:22program counter equals program counter +
  11862. 8:35:24one. Then the third thing we did was now
  11863. 8:35:27that we know that we're executing a load
  11864. 8:35:29A, we put the uh address component
  11865. 8:35:33of the um of the instruction onto RAM
  11866. 8:35:39onto uh onto the bus and read it into
  11867. 8:35:43the memory address register and then
  11868. 8:35:45that looks up the actual uh argument in
  11869. 8:35:48memory. And so the last thing we do is
  11870. 8:35:50we say RAM the output of RAM goes into
  11871. 8:35:54uh register A and then we said we only
  11872. 8:35:56so that after that fourth clock cycle we
  11873. 8:35:59execute our instruction. Uh so we do
  11874. 8:36:01nothing um no op it's a wasted cycle for
  11875. 8:36:04the fifth one. Uh we just say you know
  11876. 8:36:06every clock cycle takes five
  11877. 8:36:08instructions. So that's the first thing
  11878. 8:36:09of discourse I want to talk about. The
  11879. 8:36:11second thing is our next op code is 00 1
  11880. 8:36:170
  11881. 8:36:18but there's actually because our input
  11882. 8:36:21uh just counts sequentially. This is
  11883. 8:36:22just counting as if it as though it was
  11884. 8:36:24binary. Um 0 0 1 0 doesn't start to
  11885. 8:36:27here. So we have
  11886. 8:36:30three rows that uh are in between this
  11887. 8:36:34the load a instruction and then our add
  11888. 8:36:36instruction which comes next. Um, and it
  11889. 8:36:41turns out those are also just
  11890. 8:36:42unreachable. And here's why. We
  11891. 8:36:45discussed our step counter that counted
  11892. 8:36:47from zero up until four and then reset.
  11893. 8:36:50So, we know that based on our
  11894. 8:36:52construction of the step counter, when
  11895. 8:36:54it goes from 1 0 0, which is binary 4,
  11896. 8:36:57and we increment it, uh, it'll it'll
  11897. 8:36:59carry back over to zero, it'll reset to
  11898. 8:37:010 0. If you were counting in regular
  11899. 8:37:03binary, what you get next is not zero.
  11900. 8:37:06you'd get 101 which is five because
  11901. 8:37:08that's what comes after four. But we
  11902. 8:37:10know that because of the way the counter
  11903. 8:37:11is constructed uh that will never be
  11904. 8:37:13possible. So these three addresses right
  11905. 8:37:16here are not reachable. And so the same
  11906. 8:37:18way we said that these over here are not
  11907. 8:37:21reachable. So it doesn't matter what the
  11908. 8:37:22output is of the ROM. The same goes for
  11909. 8:37:25these addresses right here. And we can
  11910. 8:37:27actually take uh this a little further
  11911. 8:37:30and say that these are definitively not
  11912. 8:37:31reachable because we know that
  11913. 8:37:36um it's not possible for our step
  11914. 8:37:38counter to reach five. As soon as it
  11915. 8:37:40reaches four and it tries to increment,
  11916. 8:37:42it'll roll back to zero based on our
  11917. 8:37:44construction the circuit. And that's
  11918. 8:37:46different than what we had up here. This
  11919. 8:37:48is technically reachable if uh someone
  11920. 8:37:50tries to interpret someone tries to
  11921. 8:37:53execute an instruction with op code
  11922. 8:37:55000000 because we can't stop what they
  11923. 8:37:57put in the RAM. It just wouldn't do
  11924. 8:37:59anything. So that's the load a
  11925. 8:38:01instruction. So the next instruction is
  11926. 8:38:04add. Recall that add um takes has an
  11927. 8:38:08argument. The argument is an address in
  11928. 8:38:10memory. And what it does is it'll say
  11929. 8:38:12register A equals register A plus
  11930. 8:38:16whatever is in RAM at the uh address
  11931. 8:38:19specified by the argument. So um the
  11932. 8:38:22first thing we need to do though we
  11933. 8:38:24don't know it's our first clock cycle.
  11934. 8:38:26We don't know that we're executing an ad
  11935. 8:38:28yet or the computer doesn't know that.
  11936. 8:38:30So it actually needs to take whatever's
  11937. 8:38:31in the program counter look that up as
  11938. 8:38:33the instruction and get that into the
  11939. 8:38:35instruction register and then the input
  11940. 8:38:37to the uh the readonly memory would
  11941. 8:38:39actually you know or the readon memory
  11942. 8:38:41that is control would be able to see the
  11943. 8:38:44op code over here and derive semantic
  11944. 8:38:47interpretation of the instruction that
  11945. 8:38:50the user i.e. uh I guess the machine uh
  11946. 8:38:53wants to execute. So the the first two
  11947. 8:38:56steps are actually identical uh between
  11948. 8:38:58what we did in load A and what we're
  11949. 8:39:00going to do in add. So the first thing
  11950. 8:39:02we need to do is get the address that is
  11951. 8:39:05stored in the program counter out onto
  11952. 8:39:06the bus and read that into the memory
  11953. 8:39:08address register. So we'll read into the
  11954. 8:39:10memory address register and then we will
  11955. 8:39:12also put the uh program counter out.
  11956. 8:39:16[snorts]
  11957. 8:39:18And then now that we have the uh address
  11958. 8:39:22of the instruction we're trying to
  11959. 8:39:23execute in the memory address register,
  11960. 8:39:25that value is looked up in RAM. Um
  11961. 8:39:27because that's RAM's always just
  11962. 8:39:28observing what's in that register and
  11963. 8:39:30putting it out. So we actually want to
  11964. 8:39:31put that value onto the bus RAM out and
  11965. 8:39:34then get it into the instruction
  11966. 8:39:36register. Instruction register in. And
  11967. 8:39:40now uh what we've done is we've
  11968. 8:39:42effectively taken the uh we've
  11969. 8:39:44effectively taken the found instruction
  11970. 8:39:47we're trying to execute and gotten it
  11971. 8:39:48into the instruction register. So now
  11972. 8:39:51the actual op code that our um control
  11973. 8:39:54sees is 00 1 0 because that is the
  11974. 8:39:57operation code. Uh that's how we denoted
  11975. 8:39:59add in the table we constructed earlier.
  11976. 8:40:02Again that was our decision here. Um and
  11977. 8:40:05so now that we've fetched the
  11978. 8:40:07instruction, we actually need to execute
  11979. 8:40:09it. So recall that add um I can add this
  11980. 8:40:13over here. You know, add
  11981. 8:40:16is saying register a
  11982. 8:40:20equals register a plus whatever is in
  11983. 8:40:23RAM at uh the argument.
  11984. 8:40:32So what we need to do is take the
  11985. 8:40:33argument that's in the instruction
  11986. 8:40:35register uh and look it up in RAM and
  11987. 8:40:38then we can put that into the ALOU. So
  11988. 8:40:40what we need to do is write the
  11989. 8:40:41instruction register out onto the bus
  11990. 8:40:44and feed that back into the memory
  11991. 8:40:46address register. Uh recall that when we
  11992. 8:40:48put the instruction register out onto
  11993. 8:40:50the bus, it only puts out the rightmost
  11994. 8:40:52four bits, the argument. Uh and then
  11995. 8:40:54RAM, the memory address register is four
  11996. 8:40:57bits. So there's a perfect match there.
  11997. 8:40:59Now we've have the uh RAM that's looked
  11998. 8:41:01up the value. So let's put that out onto
  11999. 8:41:03the bus for our next in our next clock
  12000. 8:41:05cycle. And uh because our operation says
  12001. 8:41:10register A equals register A plus RAM
  12002. 8:41:12arg uh our two operants of the ALU are
  12003. 8:41:14register A and register B. So we kind of
  12004. 8:41:17just want to put the value in register B
  12005. 8:41:19because we're adding something to
  12006. 8:41:20register A. So why would we change
  12007. 8:41:22what's in register A? um you know that's
  12008. 8:41:25our first operand is already correctly
  12009. 8:41:27wired in to the ALOU. So uh we will
  12010. 8:41:31write that value from that we're putting
  12011. 8:41:34out from RAM uh into the B register
  12012. 8:41:37and then uh now that we've actually
  12013. 8:41:40moved the argument into the B register
  12014. 8:41:43we have our correct arguments to the ALU
  12015. 8:41:46in register A and B. So the output is of
  12016. 8:41:49the alou after through this clock cycle
  12017. 8:41:51is equal to this uh register a plus ram
  12018. 8:41:55of arg this right hand side of this
  12019. 8:41:57expression. So what we need to do is
  12020. 8:41:59take the output of the alou and write it
  12021. 8:42:01back into register a and so we can do
  12022. 8:42:04that with um alou out this eo signal. Uh
  12023. 8:42:10take the output of the alou um and then
  12024. 8:42:13we want to read it into the a register.
  12025. 8:42:16So it will read a in. And there's
  12026. 8:42:18something else that we uh need to do
  12027. 8:42:20here. Um not to execute this
  12028. 8:42:23instruction. This technically would
  12029. 8:42:24correctly execute this instruction, but
  12030. 8:42:28um we also need to update our flag
  12031. 8:42:31register because we need to know our our
  12032. 8:42:34guarantee for our jump zero and jump
  12033. 8:42:36carry instructions is it's looking uh
  12034. 8:42:39it's making a decision based on the last
  12035. 8:42:42ALU operation we performed. So now we're
  12036. 8:42:44performing an operation. We don't know
  12037. 8:42:46when a jump a conditional jump
  12038. 8:42:47expression if it ever exists will happen
  12039. 8:42:51next in the instruction. We don't know
  12040. 8:42:52you know which of the following
  12041. 8:42:53instructions will actually look at this
  12042. 8:42:55flag but we play it safe just by always
  12043. 8:42:57updating the flags. And we discussed the
  12044. 8:42:59circuits for checking those flags. And
  12045. 8:43:01so those are always looking at the
  12046. 8:43:02output of the ALOU and always feeding in
  12047. 8:43:04as the input to this flags register. So
  12048. 8:43:06all we actually need to do is pulse
  12049. 8:43:08flags in um to get that there. So uh
  12050. 8:43:13that implements our add instruction. But
  12051. 8:43:16we've actually forgotten one thing and
  12052. 8:43:18that after we execute this instruction
  12053. 8:43:21uh the way it's it's executed right here
  12054. 8:43:24the control logic uh the program sorry
  12055. 8:43:27the computer itself would actually
  12056. 8:43:28execute the same instruction again
  12057. 8:43:30afterwards and it keep getting stuck on
  12058. 8:43:32this instruction because we have not
  12059. 8:43:33incremented our program counter. And so
  12060. 8:43:36we could do that at any of these clock
  12061. 8:43:38cycles. Uh any of them work. uh we
  12062. 8:43:41always are just going to do it early so
  12063. 8:43:43we don't forget like I I just did right
  12064. 8:43:45here. Um so we can walk through and type
  12065. 8:43:48out what we did. So the very first thing
  12066. 8:43:50we did was we took the value in the
  12067. 8:43:51program counter and got it the memory
  12068. 8:43:53address register. Then we took whatever
  12069. 8:43:55was in RAM at that address and put it in
  12070. 8:43:58the instruction register and then we
  12071. 8:43:59also incremented the program counter.
  12072. 8:44:03Afterwards we took the uh address of the
  12073. 8:44:06instruction register
  12074. 8:44:08to the memory address um register. So we
  12075. 8:44:11wanted to look up the argument in RAM
  12076. 8:44:13and then we put RAM to register B
  12077. 8:44:17and then we actually performed um you
  12078. 8:44:20know ALU to register A
  12079. 8:44:24and then also update flags.
  12080. 8:44:29And so that uh completes our our ad
  12081. 8:44:32instruction. There's a few things I want
  12082. 8:44:33to talk about before we move on. So
  12083. 8:44:35first is this ad instruction actually
  12084. 8:44:38used all five clock cycles meaningly.
  12085. 8:44:42Our load a recall the last one we were
  12086. 8:44:44able to do in four clock cycles. So that
  12087. 8:44:45fifth clock cycle went wasted. Here we
  12088. 8:44:48were actually able to get it done in
  12089. 8:44:49five clock cycles. Um and it turns out
  12090. 8:44:51every instruction we created in our uh
  12091. 8:44:54instruction set earlier
  12092. 8:44:57um
  12093. 8:44:59can be executed in five or fewer
  12094. 8:45:00instructions. This is the lengthiest of
  12095. 8:45:02them. So we just forced like hey because
  12096. 8:45:05you know all these instructions are the
  12097. 8:45:08longest instruction uses five clock
  12098. 8:45:09cycles. We just designed this such that
  12099. 8:45:12uh every instruction will take five
  12100. 8:45:14clock cycles. And you could imagine
  12101. 8:45:16that's not super efficient. We don't
  12102. 8:45:18want to waste clock cycles uh especially
  12103. 8:45:20as we're trying to speed up our
  12104. 8:45:21computer. So we talked about making the
  12105. 8:45:24computer run faster by making our clock
  12106. 8:45:26just move faster. But then there is some
  12107. 8:45:27limitation there because we have to you
  12108. 8:45:29know wait for logic gates to settle
  12109. 8:45:30whenever we change inputs to them. Um so
  12110. 8:45:33the other natural thing to do is okay
  12111. 8:45:35maybe like make instructions use fewer
  12112. 8:45:37clock cycles. There's there's two things
  12113. 8:45:39to make the computer faster. You make
  12114. 8:45:40the clock run faster or you make the
  12115. 8:45:42instructions more efficient in
  12116. 8:45:44themselves. Um so we could have designed
  12117. 8:45:46this computer uh to you know as soon as
  12118. 8:45:49that load a instruction was done to move
  12119. 8:45:50on to the next instruction.
  12120. 8:45:53We just for sake of simplicity uh aren't
  12121. 8:45:55going to do that. Um, it's actually not
  12122. 8:45:57that trivial. All you need to have the
  12123. 8:45:58instruction do is just move on to the
  12124. 8:46:01next instruction address, like reset
  12125. 8:46:03that uh the the controls uh internal
  12126. 8:46:07step counter, and you know, that kind of
  12127. 8:46:10have the effect. But yeah, we're not
  12128. 8:46:11going to worry about that for now. Um,
  12129. 8:46:13the second thing I want to talk about
  12130. 8:46:14before we move on to the next
  12131. 8:46:16instruction is the fact that these two
  12132. 8:46:18instructions uh that we've done, load A
  12133. 8:46:20and AD, look pretty similar. In
  12134. 8:46:23particular, uh we can actually see that
  12135. 8:46:25the first two steps are identical. Um
  12136. 8:46:28and that's because we have to figure out
  12137. 8:46:31what instruction we're executing before
  12138. 8:46:33we can actually make the you know
  12139. 8:46:35personalized or conditional logic of
  12140. 8:46:38executing that instruction. Um it turns
  12141. 8:46:41out this will be true for every
  12142. 8:46:43instruction. the first two clock cycles
  12143. 8:46:45will always be the same
  12144. 8:46:48um to figure out what the instruction
  12145. 8:46:49actually is before we can go about
  12146. 8:46:51executing it. And this is called a fetch
  12147. 8:46:54decode execute cycle. So we kind of see
  12148. 8:46:57that like the first two clock cycles are
  12149. 8:47:00used to fetch the instruction and then
  12150. 8:47:04the decoding would be actually deriving
  12151. 8:47:06the uh like you know we have the
  12152. 8:47:09instruction what is it actually trying
  12153. 8:47:10to do and then executing it would
  12154. 8:47:12actually just be executing it. We don't
  12155. 8:47:14really have a decode process here
  12156. 8:47:15because the way we've set up our control
  12157. 8:47:17logic it's it's just kind of
  12158. 8:47:19deterministically responding to the
  12159. 8:47:20input. So decoding it is effectively
  12160. 8:47:22just looking it up in the ROM, but you
  12161. 8:47:25don't really see that on the timing
  12162. 8:47:26sequence because it happens so quickly.
  12163. 8:47:28Um, so you kind of see like the fetching
  12164. 8:47:30is clear and then the executing is clear
  12165. 8:47:33and then the decoding is kind of
  12166. 8:47:34inherently implicit given to us for free
  12167. 8:47:37because we're using a readonly memory.
  12168. 8:47:39The decoding is actually taking whatever
  12169. 8:47:42uh instruction or inputs we have to the
  12170. 8:47:44control logic and looking at the
  12171. 8:47:46corresponding outputs which is just a uh
  12172. 8:47:48readon memory read operation. Yeah. So
  12173. 8:47:51our decoding our fetching involves
  12174. 8:47:53actually moving data between registers.
  12175. 8:47:55Our decoding is just a lookup in ROM and
  12176. 8:47:57then our execution is actually the
  12177. 8:47:59interesting part where we perform the
  12178. 8:48:00operations. Um and the third interesting
  12179. 8:48:04thing uh before we move on to the next
  12180. 8:48:05instruction is um I've also said for our
  12181. 8:48:10ad instruction this is the first of four
  12182. 8:48:11copies and that is because uh our zero
  12183. 8:48:15and carry flags
  12184. 8:48:17don't change what we do for our ad
  12185. 8:48:19operation but we do need a handle that
  12186. 8:48:21those values might be different. So
  12187. 8:48:23especially after this uh like let's
  12188. 8:48:25suppose we do this add operation um and
  12189. 8:48:28then there's a load a afterwards
  12190. 8:48:30because we set the flags register in
  12191. 8:48:32this ad operation uh it doesn't affect a
  12192. 8:48:35load a operation the load a operation
  12193. 8:48:36doesn't care what's in those flags but
  12194. 8:48:38it does mean that the input to the ROM
  12195. 8:48:40can change uh and so that's why we had
  12196. 8:48:42the we talked about copying this load a
  12197. 8:48:44four times uh we actually have to do the
  12198. 8:48:46same thing for add um and we'll see that
  12199. 8:48:49that also is true for most other
  12200. 8:48:50instructions. So yeah, that's uh that's
  12201. 8:48:54our add instruction. Let's move on to
  12202. 8:48:56the subtract instruction which is very
  12203. 8:48:59similar to the add instruction with the
  12204. 8:49:01difference being that you know instead
  12205. 8:49:04of register A equals
  12206. 8:49:07instead of register A
  12207. 8:49:11equaling register A
  12208. 8:49:14plus RAM of arg ram at the address
  12209. 8:49:17specified by the argument. Uh it's the
  12210. 8:49:19exact opposite. We just subtract. And so
  12211. 8:49:21you can kind of imagine before we even
  12212. 8:49:23spill this out that the uh the
  12213. 8:49:26instructions, the microode that we're
  12214. 8:49:28putting out for of the ROM are going to
  12215. 8:49:29be very similar to this ad with the only
  12216. 8:49:32difference being that we're going to
  12217. 8:49:33have to just uh set the ad subtract line
  12218. 8:49:36in our ALOU. So I'm actually just going
  12219. 8:49:40to copy over everything we've done.
  12220. 8:49:47And all we have to do is make one very
  12221. 8:49:49trivial choice change, sorry. Um,
  12222. 8:49:54which is we want to set the subtract
  12223. 8:49:57line
  12224. 8:50:01right here. So basically everything
  12225. 8:50:05stays the same
  12226. 8:50:10and
  12227. 8:50:12we are uh just telling the ALU because
  12228. 8:50:15the ALU is always looking at the it's
  12229. 8:50:17always just taking whatever the inputs
  12230. 8:50:19are which are these values in the
  12231. 8:50:21register and it's always just doing an
  12232. 8:50:22add or subtract on them and that's
  12233. 8:50:24always available on the output line. So
  12234. 8:50:26when we put the output onto the bus and
  12235. 8:50:28necessarily read it back into register
  12236. 8:50:29A, uh, we can pretty much just get the
  12237. 8:50:33output we want by flipping the add
  12238. 8:50:34subtract line before we reinterpret the
  12239. 8:50:36value of the ALOU. So because the ALOU
  12240. 8:50:39is just always responding to the add or
  12241. 8:50:40subtract line, we can just flip it. And
  12242. 8:50:42there's no issue with timing here that
  12243. 8:50:44the line is not really a register. So,
  12244. 8:50:46it's safe for us to just uh flip the
  12245. 8:50:49line and then read that value pretty
  12246. 8:50:51quickly afterwards because it's always
  12247. 8:50:53going to be, you know, it's not
  12248. 8:50:56dependent on a clock cycle to like latch
  12249. 8:50:58in that line. Like, we don't need to
  12250. 8:50:59tell the ALU before we do an add or
  12251. 8:51:01subtract, we're going to do an add or
  12252. 8:51:02subtract. We can just tell it as it uh
  12253. 8:51:05does the computation to consider
  12254. 8:51:08whatever is on that line. Um,
  12255. 8:51:13and yeah, everything else is exactly the
  12256. 8:51:15same. Uh and then similar to what we
  12257. 8:51:17described before, this is replicated
  12258. 8:51:19four times in memory or sorry four times
  12259. 8:51:22in this readon memory.
  12260. 8:51:25Okay, let's hop on to the next
  12261. 8:51:27instruction
  12262. 8:51:29store a. So
  12263. 8:51:32um this will take an argument and it'll
  12264. 8:51:36basically say RAM at the argument should
  12265. 8:51:39equal whatever is in register A. So
  12266. 8:51:42basically we need to take whatever
  12267. 8:51:43values in register A, get it on the bus
  12268. 8:51:45and throw it into RAM. Um it lets us
  12269. 8:51:47move data. We've talked about moving
  12270. 8:51:49data from RAM into the ALOU. We are now
  12271. 8:51:52going to talk about moving data from the
  12272. 8:51:54ALOU into RAM. And because everything is
  12273. 8:51:56connected to this this central bus unit,
  12274. 8:51:58uh we have all the control signals uh
  12275. 8:52:00available to implement that uh
  12276. 8:52:02instruction with what we have defined
  12277. 8:52:04here. So uh let's start. First, as
  12278. 8:52:08usual, we don't know what we're
  12279. 8:52:10executing. So, we need to look up the
  12280. 8:52:11instruction uh based on the value that's
  12281. 8:52:13in the program counter. So, we put the
  12282. 8:52:16uh the address, we put the value from
  12283. 8:52:19the program counter out onto the bus and
  12284. 8:52:20read it into the memory address
  12285. 8:52:21register.
  12286. 8:52:25And then we take the uh output of RAM,
  12287. 8:52:28read that into the instruction register.
  12288. 8:52:30And then we also increment our program
  12289. 8:52:32counter so that uh we will move on to
  12290. 8:52:34the next instruction uh by the time that
  12291. 8:52:36the our internal control uh step counter
  12292. 8:52:40goes from four to zero or from five to
  12293. 8:52:43back to zero.
  12294. 8:52:47And now that we've uh that's our fetch
  12295. 8:52:49process again that's the same for all
  12296. 8:52:51three instructions we've implemented so
  12297. 8:52:53far. Uh what we now need to do is take
  12298. 8:52:56the argument of our instruction that's
  12299. 8:52:59telling us what address we'd like to
  12300. 8:53:01write the value into and get that into
  12301. 8:53:03the memory address register. So I'll
  12302. 8:53:05I'll read into the memory address
  12303. 8:53:07register and what I'm reading is I'm
  12304. 8:53:10putting the value uh of the instruction
  12305. 8:53:12registers argument out onto the bus the
  12306. 8:53:14right the right uh rightmost four bits
  12307. 8:53:17sorry. And then this one's pretty simple
  12308. 8:53:21because now that we've loaded the uh
  12309. 8:53:22address into the memory address
  12310. 8:53:24register, what we'll do is we put the a
  12311. 8:53:26register a value onto the bus, read it
  12312. 8:53:30into RAM,
  12313. 8:53:32and that's quite literally all we have
  12314. 8:53:34to do. And then we have a wasted clock
  12315. 8:53:36cycle afterwards because we've we've uh
  12316. 8:53:38we've already finished our instruction.
  12317. 8:53:40So I'll copy over uh our explanations
  12318. 8:53:44here. And then what we did here was
  12319. 8:53:47address of IR to the memory instruction
  12320. 8:53:50register. And then all we do here is uh
  12321. 8:53:53register A's
  12322. 8:53:55value
  12323. 8:53:58reg a into RAM. And after that uh we
  12324. 8:54:02finished our we finished implementing
  12325. 8:54:04what we said the store a instruction
  12326. 8:54:05would do. That's pretty straightforward.
  12327. 8:54:08Um let's move on to our next instruction
  12328. 8:54:11which is this load immediate. So recall
  12329. 8:54:14uh that load immediate was the first
  12330. 8:54:17instruction we saw where the argument
  12331. 8:54:19was not an address in RAM an address in
  12332. 8:54:21memory sorry uh so not a miss but
  12333. 8:54:24instead the argument was an intended to
  12334. 8:54:26be interpreted as an immediate binary
  12335. 8:54:28value. So uh what the argument what the
  12336. 8:54:32instruction does is it takes register A
  12337. 8:54:35and it writes uh into register A
  12338. 8:54:38whatever the argument to the instruction
  12339. 8:54:39is. So this one's actually easier for us
  12340. 8:54:42to implement internally because we get
  12341. 8:54:44to skip having to go through RAM. Uh we
  12342. 8:54:46get to skip that indirection and instead
  12343. 8:54:48just take whatever value the argument
  12344. 8:54:50is, treat it as a binary number, not as
  12345. 8:54:53an address, and uh throw that into the A
  12346. 8:54:56register. So let me go ahead and copy.
  12347. 8:54:58We already know the first two
  12348. 8:55:00instructions are the same. I don't want
  12349. 8:55:01to keep repeating that, so I won't
  12350. 8:55:03anymore. Um
  12351. 8:55:06now all we have to do for this one to
  12352. 8:55:08implement it once we know it once we
  12353. 8:55:09know we're uh executing a load immediate
  12354. 8:55:12um all we need to do is take the
  12355. 8:55:13argument that's stored in the
  12356. 8:55:15instruction register put it onto the bus
  12357. 8:55:18and then also just read it into the A
  12358. 8:55:20register um and then we're done. So then
  12359. 8:55:26IR to register A is what we're doing
  12360. 8:55:28here. And then our last two clock cycles
  12361. 8:55:30are wasted.
  12362. 8:55:33there no ops and I can denote that for
  12363. 8:55:35the ones above as well.
  12364. 8:55:38Um,
  12365. 8:55:39and this one's interesting because it's
  12366. 8:55:42so simple to execute. Uh, we actually
  12367. 8:55:44have two wasted clock cycles. So only
  12368. 8:55:46three of the allocated five clock cycles
  12369. 8:55:48here are being used meaningfully and the
  12370. 8:55:50others are are are useless. So you can
  12371. 8:55:52imagine that's it's pretty inefficient.
  12372. 8:55:53You know, we have like a 60% utilization
  12373. 8:55:56as measured by clock cycles for actually
  12374. 8:55:59doing work that we need to. Um, and
  12375. 8:56:01yeah, that's a that's a bad that's a bad
  12376. 8:56:03one. And the reason we have two instead
  12377. 8:56:06of one like the previous instructions
  12378. 8:56:08that had a wasted one is because we get
  12379. 8:56:10a skip uh skip a RAM lookout a RAM
  12380. 8:56:12lookup sorry because rather than the
  12381. 8:56:14argument being a address that we needed
  12382. 8:56:17to go decode in RAM which would take an
  12383. 8:56:18extra clock cycle to do we can just we
  12384. 8:56:21got to circumvent that for free just by
  12385. 8:56:22being able to use the argument literally
  12386. 8:56:25without having any indirection through
  12387. 8:56:26memory. So you can kind of see that uh
  12388. 8:56:29because of that looking up a value in
  12389. 8:56:31RAM is slow. So operating with these
  12390. 8:56:34registers is really fast but then
  12391. 8:56:36operating with RAM is a is a is
  12392. 8:56:39meaningfully slower. Uh and so it turns
  12393. 8:56:43out uh modern computers will have many
  12394. 8:56:45registers not just two that feed into
  12395. 8:56:47the ALOU but you know a set of registers
  12396. 8:56:49for you to use for scratch work and
  12397. 8:56:51there will be dedicated instructions for
  12398. 8:56:54reading and writing values into each of
  12399. 8:56:56those registers. And that makes it
  12400. 8:56:58easier for the it makes it faster for
  12401. 8:57:00the computer to implement instructions
  12402. 8:57:03and do work because it can skip every
  12403. 8:57:06time you're operating with uh a register
  12404. 8:57:08value instead of a RAM value. It's
  12405. 8:57:10faster to access that value so the
  12406. 8:57:12computer can execute the instruction
  12407. 8:57:13faster which overall improves the uh
  12408. 8:57:16performance of whatever program you're
  12409. 8:57:18running.
  12410. 8:57:20If you've written code before, then
  12411. 8:57:24certain programming languages, maybe
  12412. 8:57:25you've seen it, have a keyword. I'm
  12413. 8:57:27thinking about C specifically here, uh,
  12414. 8:57:29called register. And so you can kind of
  12415. 8:57:31tell the computer like, hey, this value
  12416. 8:57:33is going to be worked with a lot. Um, so
  12417. 8:57:36put it in a register. Uh, and you know
  12418. 8:57:38the there's something called a compiler
  12419. 8:57:40which will take that English code you've
  12420. 8:57:43written and translate it into these
  12421. 8:57:45machine code instructions that we bu
  12422. 8:57:46we're implementing right now. Uh, and
  12423. 8:57:49maybe that's a hint to that program
  12424. 8:57:51saying, "Hey, don't put this value in
  12425. 8:57:52RAM. Put this value in a register." But,
  12426. 8:57:55uh, I get ahead of myself here.
  12427. 8:57:57Um, let's talk about jump next. So,
  12428. 8:58:01jump's a little interesting because the
  12429. 8:58:03argument to jump and I guess recall
  12430. 8:58:05whatever all jump does is rather than
  12431. 8:58:08the program counter executing
  12432. 8:58:10sequentially, it just keeps counting uh,
  12433. 8:58:13from address one, treat that as an
  12434. 8:58:14instruction, execute it. then it'll say
  12435. 8:58:16look at address two treat that as an
  12436. 8:58:17instruction and execute it. Then look at
  12437. 8:58:19address three treat it as an instruction
  12438. 8:58:21and execute it. Uh instead
  12439. 8:58:24jump lets us tell the pro lets us tell
  12440. 8:58:26the program counter to like not break it
  12441. 8:58:28lets us break that sequential ordering
  12442. 8:58:30and say hey if maybe I'm on instruction
  12443. 8:58:33five right now don't go to instruction
  12444. 8:58:35six uh jump to maybe instruction 10 or
  12445. 8:58:38zero or any of them. So all it's doing
  12446. 8:58:41to implement that is just setting the PC
  12447. 8:58:44equal to the argument. The argument is
  12448. 8:58:47uh an address, but it's not an address
  12449. 8:58:49we need to go look up. It's an address
  12450. 8:58:51we just need to change control execution
  12451. 8:58:53to after this current instruction. So uh
  12452. 8:58:56our first two clock cycles for the
  12453. 8:58:58decode part are the exact same.
  12454. 8:59:02And then uh this one's also pretty
  12455. 8:59:05straightforward where all we need to do
  12456. 8:59:07is take the instruction registers
  12457. 8:59:10argument, the instructions argument
  12458. 8:59:12which is in the instruction register,
  12459. 8:59:14put it out onto the bus and then read it
  12460. 8:59:16in to the program counter
  12461. 8:59:20which we have this J line for. Um and
  12462. 8:59:23that's all. And then our last two clock
  12463. 8:59:25cycles are wasted.
  12464. 8:59:28So all this does is say program counter
  12465. 8:59:31equal uh sorry instruction register to
  12466. 8:59:35program counter. Um and there's
  12467. 8:59:38something interesting here and that this
  12468. 8:59:40instruction touches the program counter
  12469. 8:59:41twice because first we actually
  12470. 8:59:44increment it because that's what we've
  12471. 8:59:46been doing for every other instruction
  12472. 8:59:47and then we update the value that's in
  12473. 8:59:49here. So it turns out because it's a
  12474. 8:59:52jump instruction and
  12475. 8:59:55um we know that the program incrementing
  12476. 9:00:00the program counter is useless because
  12477. 9:00:02we're changing control flow to go
  12478. 9:00:03somewhere else immediately afterwards.
  12479. 9:00:05Uh it it's possibly a little redundant
  12480. 9:00:07to be incrementing the program counter
  12481. 9:00:08here, but um we could so we're
  12482. 9:00:12conceptually we could avoid incrementing
  12483. 9:00:13the program counter here. Um but uh the
  12484. 9:00:17thing is we know we don't know until
  12485. 9:00:20after this clock cycle because this is
  12486. 9:00:22the fetch that we're executing a jump.
  12487. 9:00:24So it's we don't we can't know that
  12488. 9:00:26we're executing a jump. So we can't
  12489. 9:00:28decide to skip incrementing the program
  12490. 9:00:31counter here. So if we had designed our
  12491. 9:00:34implementations for all these
  12492. 9:00:35instructions to increment the program
  12493. 9:00:37counter at the very end of your
  12494. 9:00:39instructions implementation
  12495. 9:00:41then we could safely omit it here like
  12496. 9:00:43we did on the fifth clock cycle. then we
  12497. 9:00:45could emit it here. Um,
  12498. 9:00:48because we're choosing to do it at the
  12499. 9:00:49start, we can't make that mission, but
  12500. 9:00:51it also doesn't break anything because
  12501. 9:00:52it's safe to increment the program
  12502. 9:00:54counter because it happens before we
  12503. 9:00:56update the value uh that we're jumping
  12504. 9:00:58to. So, what would be bad is if the user
  12505. 9:01:00was saying if the user had jumped to
  12506. 9:01:02address 5 and we wrote address 5 into
  12507. 9:01:05the program counter and then afterwards
  12508. 9:01:07we incremented it. That'd be bad because
  12509. 9:01:08it'd actually end up jumping them to six
  12510. 9:01:10because that's what the value in the
  12511. 9:01:12program counter would be when the CPU
  12512. 9:01:14goes to execute the next instruction. Uh
  12513. 9:01:17we don't have to worry about that
  12514. 9:01:18because you can do whatever you want to
  12515. 9:01:20the value in the program counter if
  12516. 9:01:21you're going to eventually write in the
  12517. 9:01:23intended value afterwards. So uh just
  12518. 9:01:26wanted to have a little bit of
  12519. 9:01:27discussion there.
  12520. 9:01:29Um, this instruction as is the case for
  12521. 9:01:32load immediate and store a also has to
  12522. 9:01:34be repeated four times and it's
  12523. 9:01:36identical for each of the combinations
  12524. 9:01:38of the zero flag and the carry flag. Up
  12525. 9:01:41would be jump carry. So recall that jump
  12526. 9:01:44carry is the same thing as jump. We only
  12527. 9:01:46perform the uh updating the program
  12528. 9:01:49counter. I we only do anything if the
  12529. 9:01:51carry flag is one. So this in in
  12530. 9:01:54interpretation means if the last ALOU
  12531. 9:01:57output had a carry out on the most
  12532. 9:02:00significant bit of the ALOU then we will
  12533. 9:02:03perform this jump otherwise we will do
  12534. 9:02:06nothing. So our fetch and decode are the
  12535. 9:02:09exact same
  12536. 9:02:14and also copy over this for just
  12537. 9:02:17completeness.
  12538. 9:02:20Now, if we know we're going to jump,
  12539. 9:02:24it's pretty straightforward. All we know
  12540. 9:02:26we have to do is uh put the instruction
  12541. 9:02:29register out and read it into the
  12542. 9:02:30program counter. And that way you know
  12543. 9:02:32that uh when you get to the next
  12544. 9:02:34instruction, i.e. when this our internal
  12545. 9:02:37counter for the step number uh internal
  12546. 9:02:40counter meaning internal to the control
  12547. 9:02:41logic
  12548. 9:02:43resets back to zero it'll go and fetch
  12549. 9:02:46the PC value which is now the address
  12550. 9:02:49that we intended to jump to. So uh it
  12551. 9:02:52just sets us up for executing the next
  12552. 9:02:54instruction correctly. And
  12553. 9:02:57for jump carry though, uh we want to
  12554. 9:03:00know if the carry flag was set from our
  12555. 9:03:03ALU's flags register. And that is why we
  12556. 9:03:07need this context to know what control
  12557. 9:03:10lines to set, which is why we have these
  12558. 9:03:14uh this carry flag here included as one
  12559. 9:03:16of the inputs to the control logic ROM.
  12560. 9:03:19And that's because we as we were doing
  12561. 9:03:21this as humans, we know that we're
  12562. 9:03:23making a conditional decision. Hey,
  12563. 9:03:25based on the value in the carry
  12564. 9:03:27register, we will make a decision about
  12565. 9:03:30whether or not to actually jump. When
  12566. 9:03:31we're automating this and letting the
  12567. 9:03:33computer make the decisions itself, we
  12568. 9:03:35need to feed this as input to the read
  12569. 9:03:37only memory. So
  12570. 9:03:40for this specific
  12571. 9:03:43uh instruction, it actually does matter
  12572. 9:03:45what the zero sorry what the carry flag
  12573. 9:03:47is. And uh it doesn't matter what the
  12574. 9:03:50zero flag is, but similar to the way we
  12575. 9:03:53had to repeat all of our previous
  12576. 9:03:55instructions four times based on the
  12577. 9:03:57combination of the zero flag and carry
  12578. 9:03:59flags. We're also going to have to
  12579. 9:04:00repeat this for four times because
  12580. 9:04:02there's four combinations of the zero
  12581. 9:04:04flag and carry flag. But it turns out
  12582. 9:04:06that the value of the carry flag uh
  12583. 9:04:08actually influences what we do here. So
  12584. 9:04:12it's not going to be a straight copy
  12585. 9:04:14four times across the ROM to account for
  12586. 9:04:16all four possible permutations. Um it
  12587. 9:04:19will appear for all four permutations,
  12588. 9:04:21but the microode will actually be
  12589. 9:04:22different based on the value of the the
  12590. 9:04:24carry flag here. So in this case, the
  12591. 9:04:27carry flag is zero. So we actually don't
  12592. 9:04:30do anything. Um and so we just do
  12593. 9:04:33nothing for the we waste the the
  12594. 9:04:35remaining three clock cycles of this
  12595. 9:04:37instruction.
  12596. 9:04:38Now if I jumped to uh the spot in the
  12597. 9:04:43RAM address in the ROM address table
  12598. 9:04:45where the carry flag was one. If this
  12599. 9:04:47was a one here then we would do the
  12600. 9:04:49jump. So we'd have the instruction
  12601. 9:04:51register write its value into the
  12602. 9:04:52program counter.
  12603. 9:04:56But in this case it's not actually. Uh
  12604. 9:04:58so we'll undo that. I would say let's go
  12605. 9:05:00hunt for the correct address that would
  12606. 9:05:03match up to this for the other
  12607. 9:05:04permutations. But the the sheet I'm
  12608. 9:05:06making with is non-exhaustive and and it
  12609. 9:05:08doesn't have that. Um so instead let's
  12610. 9:05:11actually uh look to jump zero implement
  12611. 9:05:14that and then we actually will look at
  12612. 9:05:16all the permutations. So jump zero just
  12613. 9:05:18as a reminder what the instruction does
  12614. 9:05:20is the same thing as jump carry but our
  12615. 9:05:22condition to make the jump is if the
  12616. 9:05:24last alou operation equal zero. Um, and
  12617. 9:05:28recall we implemented this by looking at
  12618. 9:05:31the output of the ALOU and oring
  12619. 9:05:34together the inverse of all of the
  12620. 9:05:36output bits. So if all of the um output
  12621. 9:05:40bits
  12622. 9:05:42are are all zeros, sorry, we and them
  12623. 9:05:44all together. So we we if all the output
  12624. 9:05:46bits are zero, then the inverse of all
  12625. 9:05:48of them would be ones across the board.
  12626. 9:05:51And then if we and those together, the
  12627. 9:05:53only way for that output to be one is if
  12628. 9:05:56all of those inputs are one, which
  12629. 9:05:59pre-inverter means all of the inputs are
  12630. 9:06:01zero. Um,
  12631. 9:06:04so the fetch and decode process is the
  12632. 9:06:06exact same.
  12633. 9:06:11But, uh, now we have to look at the zero
  12634. 9:06:13flag. And the zero flag uh, the
  12635. 9:06:16interpretation is could be a little
  12636. 9:06:19misleading here. the zero flag uh if
  12637. 9:06:22it's zero it means the last alou output
  12638. 9:06:25was not equal to zero. It's only if it's
  12639. 9:06:28set to one does it mean that the last
  12640. 9:06:29ALU output was actually zero. In this
  12641. 9:06:32case uh the zero flag is zero which
  12642. 9:06:34means the last ALU operation was not a
  12643. 9:06:37zero. So we don't actually do the jump.
  12644. 9:06:39So we also similar to the jump carry we
  12645. 9:06:42just saw we end up wasting all the
  12646. 9:06:44remaining clock cycles.
  12647. 9:06:47Um but let's go see a more interesting
  12648. 9:06:49case. So this is how jump zero behaves
  12649. 9:06:51if the zero flag is zero. Uh we're kind
  12650. 9:06:55of interested in the what the output
  12651. 9:06:57looks like if the zero flag is one.
  12652. 9:07:00And so I would copy this.
  12653. 9:07:11Here's another jump zero. So our first
  12654. 9:07:13two instructions will be the same. Our
  12655. 9:07:15zero flag is still zero. So we actually
  12656. 9:07:18do nothing. So we still waste the
  12657. 9:07:20remaining three clock cycles.
  12658. 9:07:22Not as interesting. Let's keep going.
  12659. 9:07:26Here's Oh, here's another jump zero. And
  12660. 9:07:29we know it's jump zero because the op
  12661. 9:07:31code is one 0 0. That's how we defined
  12662. 9:07:33in the table. So I'll copy over the
  12663. 9:07:35first two clock cycles of the fetch
  12664. 9:07:37process. Oh, those are gone. Um,
  12665. 9:07:46so these two are always the same. This
  12666. 9:07:48time though, the zero flag is one. So we
  12667. 9:07:50perform the jump. And so what that means
  12668. 9:07:53is we actually take the argument stored
  12669. 9:07:54in the instruction register, throw it
  12670. 9:07:56out onto the bus, and then read it into
  12671. 9:07:58the program counter, and then we waste
  12672. 9:08:00the last two clock cycles. So this says
  12673. 9:08:03PC
  12674. 9:08:05PC uh instruction register to PC. Uh put
  12675. 9:08:10the argument instruction register out
  12676. 9:08:11into the program counter. Um and there's
  12677. 9:08:14something interesting because it may not
  12678. 9:08:17be very apparent but it is also probably
  12679. 9:08:19worth calling out. Uh this right here is
  12680. 9:08:21the same micro code for the regular jump
  12681. 9:08:24instruction that unconditionally makes
  12682. 9:08:26the jump. So uh every time we are making
  12683. 9:08:30a jump carry or jump zero and the
  12684. 9:08:32correct flag is one the microode
  12685. 9:08:35uh i.e how we uh implement the
  12686. 9:08:38instruction is called microode uh
  12687. 9:08:40setting each of these lines output
  12688. 9:08:42lines. Um the microode the control
  12689. 9:08:45output is always the same and so it's
  12690. 9:08:49based on uh the inputs that actually
  12691. 9:08:52determine which of the uh which of these
  12692. 9:08:55control lines will be set for the
  12693. 9:08:56output. Um, so it means we do replicate
  12694. 9:08:58this this instruction four times like
  12695. 9:09:01every other instruction is replicated to
  12696. 9:09:02handle for every permutation of the zero
  12697. 9:09:05and carry flags. But unlike every other
  12698. 9:09:07instruction we've seen so far that
  12699. 9:09:09ignores it, this one actually has to
  12700. 9:09:11look at those flags to make a uh
  12701. 9:09:13well-informed decision to know what it
  12702. 9:09:15should or should not do. So that's
  12703. 9:09:16pretty interesting because this is kind
  12704. 9:09:18of how we make decisions in our in our
  12705. 9:09:20control. um you know everything else
  12706. 9:09:24everything else in terms of instructions
  12707. 9:09:26we discussed so far uh it's easy to tell
  12708. 9:09:28the control what to do strictly based on
  12709. 9:09:30you know for hey this op code represents
  12710. 9:09:33this instruction the steps are always
  12711. 9:09:34the same but then for the the jump carry
  12712. 9:09:37and jump zero instructions
  12713. 9:09:39uh they aren't necessarily the same uh
  12714. 9:09:41and we'd actually need to look at it so
  12715. 9:09:43as a human we said uh I'm going to look
  12716. 9:09:45at what's in the flags register to
  12717. 9:09:46determine if I make this jump uh and we
  12718. 9:09:49how we bridge that gap of us making a
  12719. 9:09:52decision conditionally uh was feeding it
  12720. 9:09:54as input to the readonly memory and
  12721. 9:09:57generating different outputs based on
  12722. 9:09:59the different input combinations. And so
  12723. 9:10:01because a readonly memory has to be we
  12724. 9:10:03have to fully map out every input and
  12725. 9:10:05output combination we can pretty much
  12726. 9:10:07say hey for each of these input
  12727. 9:10:09combinations this is what you should do.
  12728. 9:10:11So we are we give the readonly memory a
  12729. 9:10:13very verbose yet complete and necessary
  12730. 9:10:17uh set of [snorts] input output
  12731. 9:10:19combinations to handle for this. Um so
  12732. 9:10:21for every other instruction if we wanted
  12733. 9:10:23to add more conditional instructions to
  12734. 9:10:25this computer uh you kind of have to
  12735. 9:10:27find a way to incorporate that logic uh
  12736. 9:10:30into the readonly memory by adding in an
  12737. 9:10:32extra input um that represents the
  12738. 9:10:36decision state for what you're making
  12739. 9:10:38that instructionally do. And then it'd
  12740. 9:10:41also have to be replicated however many
  12741. 9:10:43times. Um, cool. So that's interesting.
  12742. 9:10:46Uh, let's see if I
  12743. 9:10:49So the remaining jump zero one 0 0. Um,
  12744. 9:10:53the jump flag is set. So we also perform
  12745. 9:10:56the the jump operation. So I can
  12746. 9:10:59actually literally just copy the
  12747. 9:11:01implementation for jump because it's the
  12748. 9:11:03same.
  12749. 9:11:10Cool. Okay. So, that's how we implement
  12750. 9:11:13our conditional logic onto our final two
  12751. 9:11:16instructions. Uh, I know this has
  12752. 9:11:18probably gotten a little bit repetitive,
  12753. 9:11:20but I wanted to be as complete as
  12754. 9:11:21possible because I part of the video is
  12755. 9:11:23I do not want to leave out otherwise
  12756. 9:11:24meaningful information as I explain
  12757. 9:11:26things from scratch. Uh, so I want to
  12758. 9:11:28minimize the amount of handwaving I do
  12759. 9:11:30until we have the barebone CPU working.
  12760. 9:11:33And then as we talk about how this
  12761. 9:11:34translates to a more modern CPU, more
  12762. 9:11:37modern computer, uh then I'll do a
  12763. 9:11:39little more handwaving. But
  12764. 9:11:41um our second to last instruction was
  12765. 9:11:44out and all it does is put the value in
  12766. 9:11:47register A into the output displays
  12767. 9:11:49register. And that has the effect of
  12768. 9:11:52updating the seven segment display to
  12769. 9:11:55show whatever's in the A register. Even
  12770. 9:11:57uh in future instructions change the
  12771. 9:11:59value of the A register. um it won't
  12772. 9:12:02update the display explicitly. So the
  12773. 9:12:04display is not always just hardcoded to
  12774. 9:12:06the A register. Remember we built a new
  12775. 9:12:07register uh and the display is hardcoded
  12776. 9:12:10to that register. Um
  12777. 9:12:13so our fetch decode execute cycle starts
  12778. 9:12:16with fetch. We do the same thing that
  12779. 9:12:19we've done for every instruction.
  12780. 9:12:21Oh well that's interesting.
  12781. 9:12:30Okay. Um, and for this one, it's also
  12782. 9:12:33pretty trivial. All we need to do is get
  12783. 9:12:35the value in the A register into the
  12784. 9:12:37displays register. So, I'm going to put
  12785. 9:12:39the A register out onto the bus and then
  12786. 9:12:42I'm going to read it into the uh display
  12787. 9:12:45register.
  12788. 9:12:51And uh once we do that, uh I'll mark
  12789. 9:12:54that as register a into display reg. um
  12790. 9:12:59our remaining two clock cycles can be
  12791. 9:13:01wasted here. So that's a pretty simple
  12792. 9:13:03instruction. And then this brings us
  12793. 9:13:07um to the halt instruction which is the
  12794. 9:13:10last one of them all but worthy of
  12795. 9:13:12instruction worthy of discussion. Uh and
  12796. 9:13:15for both the output and the display
  12797. 9:13:18instructions obviously these are
  12798. 9:13:19repeated four times uh based on whatever
  12799. 9:13:21the inputs uh for the zero flag and
  12800. 9:13:24carry flag are don't affect what these
  12801. 9:13:26operations do. But we still need to
  12802. 9:13:28define them because that is input to the
  12803. 9:13:30ROM. Um, I display should do the same
  12804. 9:13:33thing uh if if the zero flag is one and
  12805. 9:13:36the carry flag is zero as as it should
  12806. 9:13:38as if both of those flags are zero. But
  12807. 9:13:40we actually need to go specify that in
  12808. 9:13:42the ROM um for completeness. So our halt
  12809. 9:13:47instruction is pretty interesting where
  12810. 9:13:50all we have to do is set the halt line.
  12811. 9:13:55And
  12812. 9:13:56recall that the moment we do this
  12813. 9:14:00because the halt line is is connected to
  12814. 9:14:03the clock, the halt line is inverted and
  12815. 9:14:06then connected to the clock. So the
  12816. 9:14:08moment we set halt one, the inverted
  12817. 9:14:11value of halt is zero. And since the
  12818. 9:14:14clock is anded with this, it means the
  12819. 9:14:17clock output that we connect to the rest
  12820. 9:14:19of the computer is just going to get
  12821. 9:14:21stuck at zero because zero and anything
  12822. 9:14:23anded with zero is zero. So even if the
  12823. 9:14:26clock itself is going high to low, high
  12824. 9:14:27to low, uh when we end it with this
  12825. 9:14:29inverted halt signal, it'll block that
  12826. 9:14:32from actually carrying through out of
  12827. 9:14:33the endgate. Um and so as soon as we get
  12828. 9:14:37here, the computer cannot make any
  12829. 9:14:39progress. So it actually doesn't matter
  12830. 9:14:41what we specify for the remaining two
  12831. 9:14:43clock cycles here because not none of
  12832. 9:14:45it's conceptually possible for it to get
  12833. 9:14:47executed. The only way to get the
  12834. 9:14:50computer out of this state would be to
  12835. 9:14:52physically turn it off, turn it back on,
  12836. 9:14:54reprogram uh your memory and then things
  12837. 9:14:57will be good to go again. Um, cool. So
  12838. 9:15:00that concludes us implementing each of
  12839. 9:15:03the instructions. What I'd like to do
  12840. 9:15:05next is uh define a program with the
  12841. 9:15:10instructions we've created.
  12842. 9:15:13So I'll switch over to this other tab
  12843. 9:15:15here. And what I've copied over is all
  12844. 9:15:18of our instructions and their op codes.
  12845. 9:15:20We know what they do. So I don't carry
  12846. 9:15:21over a description. And then uh a
  12847. 9:15:23conceptual layout of RAM
  12848. 9:15:26um where here's our 16 different
  12849. 9:15:28addresses starting from zero uh in
  12850. 9:15:31decimal. And then this is the binary
  12851. 9:15:33representation i.e. if we wanted to you
  12852. 9:15:35know lo access that value either read or
  12853. 9:15:39writing into it. Uh this is what we need
  12854. 9:15:40to put in the memory address register.
  12855. 9:15:42So it's just the the binary equivalence.
  12856. 9:15:44And what I'd like to do is let's write a
  12857. 9:15:47simple program that just
  12858. 9:15:50starts with 15 uh and keeps counting
  12859. 9:15:53down by one and displaying that on the
  12860. 9:15:56uh our seven segment display and then
  12861. 9:15:58once we hit zero we stop. Uh it's a
  12862. 9:16:00trivial program and you'll probably see
  12863. 9:16:02why I pick 15 very very shortly. Um but
  12864. 9:16:07if we think about what we need to do to
  12865. 9:16:09get this program in RAM, there's a few
  12866. 9:16:10things. First, we need to you know for
  12867. 9:16:13our our primary loop, it's going to be
  12868. 9:16:16doing subtraction by one each time.
  12869. 9:16:18Assuming we get the value 15 into the a
  12870. 9:16:21register and we get the value one uh
  12871. 9:16:23somewhere else in memory then we can
  12872. 9:16:25keep calling the subtract instruction
  12873. 9:16:27with wherever the one resides in memory
  12874. 9:16:30and then we know every time after we do
  12875. 9:16:32the subtract instruction we want to
  12876. 9:16:34write the display to write to the output
  12877. 9:16:36display and then we check if we're at
  12878. 9:16:38zero or not. Uh if we're not at zero we
  12879. 9:16:41could jump back to our same uh
  12880. 9:16:42subtraction instruction. uh otherwise if
  12881. 9:16:45we are at zero we need to jump and and
  12882. 9:16:48have this um halt command. So let's kind
  12883. 9:16:52of think about the very first thing we
  12884. 9:16:54need to do. Um the first thing we'd want
  12885. 9:16:57to do is get this the value one
  12886. 9:17:00somewhere in memory so that we can
  12887. 9:17:02always use that as our thing that we're
  12888. 9:17:04subtracting from. And if I if we were to
  12889. 9:17:06count down from two, we would need the
  12890. 9:17:08value two somewhere in memory. And
  12891. 9:17:10that's because the subtract instruction
  12892. 9:17:12uh the operand is not the value you want
  12893. 9:17:15to subtract from whatever's in the A
  12894. 9:17:16register. It's the address to something
  12895. 9:17:18in memory and that value at that address
  12896. 9:17:22is what you want to subtract from the A
  12897. 9:17:23register. So uh the first thing I want
  12898. 9:17:26to do is actually get um a load
  12899. 9:17:29immediate of a one into the A register.
  12900. 9:17:32So
  12901. 9:17:34the reason I'm doing this uh is that in
  12902. 9:17:37the only way we can write something to
  12903. 9:17:39RAM is with our store a instruction. So
  12904. 9:17:42if we want to get the value one
  12905. 9:17:44somewhere into RAM, we'd actually first
  12906. 9:17:46need to get that value into the A
  12907. 9:17:48register and then we can write it from
  12908. 9:17:50the A register into RAM. And so the load
  12909. 9:17:52immediate is what does exactly that. It
  12910. 9:17:54treats the argument as a literal 4bit
  12911. 9:17:57integer and then it'll put that into the
  12912. 9:17:59A register. So uh we can comment this
  12913. 9:18:03you know register a equals 1 and then uh
  12914. 9:18:06the actual RAM value the actual value we
  12915. 9:18:09put in RAM to do this is the load
  12916. 9:18:11immediate argument. So 0 1 01 and then
  12917. 9:18:14the argument here I'll do a space to
  12918. 9:18:16separate the the op code the operation
  12919. 9:18:19code the instruction code and then the
  12920. 9:18:21argument to that instruction just for
  12921. 9:18:23our readability purpose. Um, and the
  12922. 9:18:26argument we want to load into the val
  12923. 9:18:28into the A register is just one. So now
  12924. 9:18:31that we've loaded the A register with
  12925. 9:18:33the value one, we want to store that
  12926. 9:18:35somewhere in memory. And here's where we
  12927. 9:18:36get interesting. So like our computer
  12928. 9:18:38will start at address zero in terms of
  12929. 9:18:40execution when we power it up. The
  12930. 9:18:42program counter will always start at
  12931. 9:18:43zero just by assumption. Um, we kind of
  12932. 9:18:46you get to use RAM however we want. So
  12933. 9:18:49it's kind of us up to us to figure out
  12934. 9:18:51where we want to put the uh the the
  12935. 9:18:54constant one in memory and we designed
  12936. 9:18:56the program knowing that okay if we
  12937. 9:18:58store the constant one in memory at this
  12938. 9:19:00address then every time we do our
  12939. 9:19:01subtraction instruction this is the
  12940. 9:19:03address we need to feed it. What I like
  12941. 9:19:06to do and what you'll probably see if
  12942. 9:19:07you poke into this further um since RAM
  12943. 9:19:10can contain both instructions and data
  12944. 9:19:13people generally like to separate the
  12945. 9:19:15two. Uh but it's not actually a strict
  12946. 9:19:17requirement. It's totally up to you. And
  12947. 9:19:18I think this is where some amount of
  12948. 9:19:20like creative expression comes into
  12949. 9:19:22play. Uh cuz you'll also see that
  12950. 9:19:24there's probably a different way to
  12951. 9:19:25implement the same exact program. You
  12952. 9:19:27might actually have a different thought
  12953. 9:19:28of how you can get this countdown
  12954. 9:19:30program. Um and I'd encourage you to try
  12955. 9:19:32it out. And we'll we'll show you how to
  12956. 9:19:34try it out shortly, but I'm going to go
  12957. 9:19:36down the route I have. So I've loaded
  12958. 9:19:38the register uh a with the value one.
  12959. 9:19:41And I'm going to say that uh address 15
  12960. 9:19:44is where we're going to store that into.
  12961. 9:19:45So, what I'd like to do then is store uh
  12962. 9:19:48the value in the A register into address
  12963. 9:19:51um 15. And so, what that does is it says
  12964. 9:19:56mem 15= 1.
  12965. 9:20:00And I guess I'll just use RAM at address
  12966. 9:20:0215als 1 uh for the comment here. And
  12967. 9:20:04then the actual binary value that we
  12968. 9:20:07need to load into RAM at address one to
  12969. 9:20:09signal that instruction to the computer
  12970. 9:20:11is the store a command which is 010 and
  12971. 9:20:15then our argument is 1111.
  12972. 9:20:18Okay. So basically when we power up the
  12973. 9:20:21computer if we had if we ran uh ran the
  12974. 9:20:24the instructions uh this in this value
  12975. 9:20:28followed by this value the output is
  12976. 9:20:30that uh the value in RAM at this address
  12977. 9:20:33would look like this. Um if we let that
  12978. 9:20:36program run but that's not what we want
  12979. 9:20:38to do. That's just getting started. So
  12980. 9:20:40now that we have uh this this value
  12981. 9:20:42address that we're going to say so like
  12982. 9:20:44this will just be like a hard-coded one
  12983. 9:20:47is kind of how we will the the value I
  12984. 9:20:49mean we don't specify that our program
  12985. 9:20:50will put that in there. Um there's an
  12986. 9:20:53alternative where we could have skipped
  12987. 9:20:55these two steps if we had just punched
  12988. 9:20:58the value
  12989. 9:21:01the uh this value one into that RAM
  12990. 9:21:03value to begin with. Uh but I'm just
  12991. 9:21:05going to work with just what we can
  12992. 9:21:07express with instructions for now. Um,
  12993. 9:21:10so now that we've uh gotten our one,
  12994. 9:21:13which might still a little bit be might
  12995. 9:21:15still be a little bit ambiguous as to
  12996. 9:21:16why we've done these two instructions,
  12997. 9:21:17but you'll see very shortly. Um, we
  12998. 9:21:20simply want to start with the value 15
  12999. 9:21:22and count down from it. So, basically,
  13000. 9:21:26uh, what we need to do is load the value
  13001. 9:21:2815 into the A register.
  13002. 9:21:31So, load immediate 1111. Uh, ooh, this
  13003. 9:21:35is actually, um, Oh, that's fine. That's
  13004. 9:21:38fine. So it what's interesting here is
  13005. 9:21:401111 in this uh instruction is an
  13006. 9:21:43argument that actually points to this
  13007. 9:21:44address in memory because that's how we
  13008. 9:21:46interpret the argument for the store
  13009. 9:21:48instruction. But as we discussed earlier
  13010. 9:21:50the 1111 in this load immediate argument
  13011. 9:21:53is actually referring to the literal
  13012. 9:21:55binary value um 15 if you were to
  13013. 9:21:58translate that from binary back to
  13014. 9:21:59decimal. So uh the effect of this
  13015. 9:22:02instruction is that register a will now
  13016. 9:22:03equal 15. And so the actual RAM value
  13017. 9:22:06that we need to put in to encode that
  13018. 9:22:08instruction is load immediate 0 1 01 and
  13019. 9:22:11then 11 one1.
  13020. 9:22:13Okay. So now we have 15. Um we need to
  13021. 9:22:16think about what the user expected
  13022. 9:22:18behavior of this program is. And what
  13023. 9:22:20that is is that they should see the
  13024. 9:22:22number 15 and then they should see the
  13025. 9:22:24number 14 and then 13 and it should go
  13026. 9:22:26all the way down until they see the
  13027. 9:22:27number three then the number two then
  13028. 9:22:29the number one and then the number zero
  13029. 9:22:31and then it stops. Let's not think about
  13030. 9:22:32the end conditioner yet. But uh let us
  13031. 9:22:35think that the fact that we have 15 in
  13032. 9:22:37the A register, let's go ahead and show
  13033. 9:22:38that to display. So all we'll do is an
  13034. 9:22:41out for our next instruction. Um and
  13035. 9:22:43recall that the argument is useless. So
  13036. 9:22:46to encode that in binary
  13037. 9:22:48um we will say
  13038. 9:22:52uh
  13039. 9:22:54the operation code for the out
  13040. 9:22:56instruction is 1 1 0 and then the
  13041. 9:22:58argument does not matter. There's no
  13042. 9:22:59argument for this instruction. And as we
  13043. 9:23:01discussed earlier when we looked at how
  13044. 9:23:03we implemented the ROM over here since
  13045. 9:23:05we handle um
  13046. 9:23:08like yeah it doesn't actually matter
  13047. 9:23:10what the uh argument is here. This could
  13048. 9:23:12be anything. We just actually don't even
  13049. 9:23:14use it um for for our our implementation
  13050. 9:23:18of the microode. So it doesn't even
  13051. 9:23:19really matter. Uh I'll just keep it
  13052. 9:23:21zeros for for clarity. Um okay. So now
  13053. 9:23:24we're going to say display 15. Okay. So
  13054. 9:23:28now we've displayed 15. we got to get to
  13055. 9:23:3114. So all we need to do since we have
  13056. 9:23:3415 in the A register is subtract one.
  13057. 9:23:37And recall that by the time we get to
  13058. 9:23:39this instruction right here, by the time
  13059. 9:23:40our program counter equals the value
  13060. 9:23:42four, we were guaranteed to have a
  13061. 9:23:45hard-coded one at RAM address 15. So all
  13062. 9:23:49we need to do is subtract one from the A
  13063. 9:23:51register. So we could just do sub and
  13064. 9:23:54then recall the argument is the address
  13065. 9:23:57uh which contains the value you want to
  13066. 9:23:58subtract. So we're going to do 15 here,
  13067. 9:24:00which is binary 1111. Um, and so this
  13068. 9:24:04has the effect of of a equals a minus
  13069. 9:24:07one. And so the binary value that we
  13070. 9:24:10need to put into RAM to encode that
  13071. 9:24:11instruction. Uh, the operation code for
  13072. 9:24:14sub is 0011. And our argument is address
  13073. 9:24:1715. So 1 2 3 4. Cool. Um, so now that
  13074. 9:24:21we've subtracted one, we are at 14. So
  13075. 9:24:25let's go ahead and show that to the
  13076. 9:24:26user. So it's the same thing we've done
  13077. 9:24:28up here. Um so we will say uh display a
  13078. 9:24:35um and then
  13079. 9:24:38uh the operation code is 11 1 0 0. So
  13080. 9:24:42now that we've subtracted a value and
  13081. 9:24:44shown it to the user, we need to make
  13082. 9:24:47the decision. Do we go subtract another
  13083. 9:24:49value and repeat this process again and
  13084. 9:24:51show that value to the user? um or are
  13085. 9:24:55we at zero and should we end the
  13086. 9:24:57program? So naively what you might think
  13087. 9:25:00is uh let's just ignore the termination
  13088. 9:25:03condition of the program is all we once
  13089. 9:25:06we have gotten to the state all we'd
  13090. 9:25:08like to keep doing is subtracting the
  13091. 9:25:09val subtracting one from whatever's in
  13092. 9:25:11the A register and then showing that to
  13093. 9:25:13the user and subtracting one from the A
  13094. 9:25:15register and then showing that to the
  13095. 9:25:16user. Um, and so what we what you might
  13096. 9:25:20be tempted to do here is just jump uh to
  13097. 9:25:22this address 4, which is 0 1 0 0. And
  13098. 9:25:26that would work. Uh, but this wouldn't
  13099. 9:25:28stop correctly. When we got to zero, uh,
  13100. 9:25:30this would jump right back to this
  13101. 9:25:32instruction. And then you get a weird
  13102. 9:25:34result in the ALOU because, you know,
  13103. 9:25:35you're subtracting
  13104. 9:25:37uh, one from zero, so you'd get negative
  13105. 9:25:39one. Um, but we said we wanted to stop
  13106. 9:25:42the program at zero. So what we'd
  13107. 9:25:44actually need to do is check uh if we
  13108. 9:25:46have a zero from our last result, which
  13109. 9:25:48is the exact purpose of this jump carry
  13110. 9:25:50instruction. And if we hit if we do, we
  13111. 9:25:53should if we jump carry to zero, uh we
  13112. 9:25:56can jump to an address that will end the
  13113. 9:25:59program uh by just having the halt
  13114. 9:26:02instruction at that address. So what I
  13115. 9:26:04mean by that is let's put a halt
  13116. 9:26:07instruction just somewhere random in
  13117. 9:26:08memory. Um
  13118. 9:26:13and there's no argument to the
  13119. 9:26:14instruction and so this is just halt. Uh
  13120. 9:26:18and then basically what we can then do
  13121. 9:26:20here is like if we uh jump if our if the
  13122. 9:26:24result of the last ALU operation i.e.
  13123. 9:26:26the subtract at address 4 was a zero to
  13124. 9:26:29this address 10 which is binary 1 0 1 0.
  13125. 9:26:33Um and so if we think about where we
  13126. 9:26:37start here, uh we display 15, subtract
  13127. 9:26:40one. So we just and then we have 14 and
  13128. 9:26:42then we display 14. This jump carry 14
  13129. 9:26:45is not equal to zero. So it just
  13130. 9:26:47continues uh execution. And so then we
  13131. 9:26:50know that if we make it to this address
  13132. 9:26:527 that it means we actually are not done
  13133. 9:26:55with our program. So we should jump back
  13134. 9:26:57up to here to do the subtraction uh
  13135. 9:26:59again. So I can just jump to address 0 1
  13136. 9:27:030 0. Um so basically uh check if done
  13137. 9:27:10and then otherwise
  13138. 9:27:12um loop. We're pretty much doing a loop
  13139. 9:27:14here. You know we loop back to the top
  13140. 9:27:16where our loop is a pretty
  13141. 9:27:17straightforward just uh subtract by one.
  13142. 9:27:20So the last thing I need to do is go
  13143. 9:27:21ahead and specify what the RAM values
  13144. 9:27:23for both of these instructions are. So
  13145. 9:27:25this jump carry is 0 1 1 and then our
  13146. 9:27:28argument is 1 0 1 0 and then regular
  13147. 9:27:30jump has the operation code 0 1 1 0 and
  13148. 9:27:34then our argument is 0100.
  13149. 9:27:37So um if you believe what I'm saying
  13150. 9:27:40then let's walk through our program. We
  13151. 9:27:43start we load the value one into
  13152. 9:27:45register A and then we store that at
  13153. 9:27:47address 15 in RAM. Then we load the
  13154. 9:27:50value 15 into register A. Show that to
  13155. 9:27:52the user. So the user then sees 15. We
  13156. 9:27:55subtract one from the register and then
  13157. 9:27:59display that user. So the user sees 14.
  13158. 9:28:02We check if the result was zero. 14 is
  13159. 9:28:04not zero. So we move on to this
  13160. 9:28:06instruction. This jump just says go back
  13161. 9:28:09to right here. And so then we have 14
  13162. 9:28:12and then we subtract one from it. So we
  13163. 9:28:13get 13. We show 13 to the user. 13 does
  13164. 9:28:16not equal to zero. So we continue. And
  13165. 9:28:18then we jump back up here. And now we
  13166. 9:28:21have 13. We subtract one to get 12 in
  13167. 9:28:23the A register. Show 12 to the user. 12
  13168. 9:28:26does not equal zero. And so we jump back
  13169. 9:28:28up here. And the process will continue
  13170. 9:28:30until we eventually get to uh having one
  13171. 9:28:33in the A register. Then we subtract one
  13172. 9:28:35from it to get zero. We show the zero to
  13173. 9:28:38the user. Then we check if the last
  13174. 9:28:40result was zero. It was this time. So we
  13175. 9:28:42jump to this address in memory. And this
  13176. 9:28:45address in memory just has to halt the
  13177. 9:28:46program. So nothing continues
  13178. 9:28:48afterwards. So, I've invented this
  13179. 9:28:50hypothetical program. Um, but like how
  13180. 9:28:53can we actually verify that it's
  13181. 9:28:54correct? Uh, it turns out I've built the
  13182. 9:28:56simulator for the computer we've
  13183. 9:28:58designed. So, we can go go ahead and
  13184. 9:29:00punch this program, this this value into
  13185. 9:29:02RAM, um, and let it run and see if the
  13186. 9:29:06behavior is exactly what we expect. So,
  13187. 9:29:08let me go ahead and tab out and let's do
  13188. 9:29:10that. Uh, a few years ago when I first
  13189. 9:29:13built this computer, I guess 6 years ago
  13190. 9:29:15now, uh, I also built a simulator
  13191. 9:29:17afterwards. This is publicly available
  13192. 9:29:19if you want to play around with it. Um
  13193. 9:29:21there's instructions on how to set it up
  13194. 9:29:23and how to use it here. Um I won't focus
  13195. 9:29:26too much on that. We'll just show how
  13196. 9:29:27how do you actually play around with it
  13197. 9:29:29to show what we the program we just
  13198. 9:29:31made. Um so what I've gone through and
  13199. 9:29:34done is uh let's look at the simulator
  13200. 9:29:36itself. So at any point you can see the
  13201. 9:29:40contents of RAM. Uh this is the
  13202. 9:29:42addressing and you can just click on any
  13203. 9:29:44value to change it and it'll show you
  13204. 9:29:45every time. There's like kind of a a log
  13205. 9:29:47here of every every time something about
  13206. 9:29:49the computer changes, it is shown here.
  13207. 9:29:52So if I just set this value to zero, you
  13208. 9:29:53can see memory address 15 changed to
  13209. 9:29:55zero. Uh or if I punch it to one, memory
  13210. 9:29:57address 15 changed to one. Um and so
  13211. 9:30:01this is RAM. And then you can see for
  13212. 9:30:04you can see the value that's on the bus
  13213. 9:30:06at any time. You can see this value
  13214. 9:30:08that's in the A register or the B
  13215. 9:30:10register. And you can see the output of
  13216. 9:30:12the ALU at any time. And then you can
  13217. 9:30:14also see what's in the instruction
  13218. 9:30:16register, what's in the program counter,
  13219. 9:30:18what's in the output displays register,
  13220. 9:30:21what's in the memory address register,
  13221. 9:30:23and then recall that each instruction on
  13222. 9:30:26our uh computer takes five clock cycles
  13223. 9:30:28to execute. So you can see what uh each
  13224. 9:30:31of the steps are. And then it'll also
  13225. 9:30:33show you for each of the steps
  13226. 9:30:36um what control lines like what the
  13227. 9:30:38output of the control lines uh are. and
  13228. 9:30:41then also what the value of our carry
  13229. 9:30:43flag and zero flag are. Um there's a few
  13230. 9:30:46other things. We don't need to touch on
  13231. 9:30:47all of it. Uh and you can also see
  13232. 9:30:49here's the value of the seven segment
  13233. 9:30:50display. Um the fictitious one we
  13234. 9:30:53constructed with just one of these
  13235. 9:30:54units, but I explained you could
  13236. 9:30:56probably reason through how the same
  13237. 9:30:58logic can be expanded for chaining
  13238. 9:31:00several seven segment displays together
  13239. 9:31:02to show uh multi-digit numbers rather
  13240. 9:31:05than the zero through nine that we were
  13241. 9:31:07fairly constrained to. And then what
  13242. 9:31:09I've done is I've gone ahead and punched
  13243. 9:31:12in our entire program. There is one
  13244. 9:31:14thing I caught. Um I used jump carry
  13245. 9:31:17here. Uh but our end condition is we
  13246. 9:31:19don't want to end when our result
  13247. 9:31:21carries because it doesn't make sense
  13248. 9:31:22because we're counting down. We want to
  13249. 9:31:24end the program when we hit a zero as
  13250. 9:31:26the last output of our our ALU
  13251. 9:31:28operation. So this should not be a jump
  13252. 9:31:30carry. This should be a jump zero.
  13253. 9:31:32Apologies for the confusion. And then
  13254. 9:31:34the jump zero uh operation code is 10.
  13255. 9:31:38So, uh, I fixed that. And then you can
  13256. 9:31:40see I've kind of, um, incorporated that,
  13257. 9:31:45uh, in the program here. There's one
  13258. 9:31:47other cool thing, uh, that this computer
  13259. 9:31:50simulator has, and that is, uh, there's
  13260. 9:31:53this button I built called analyze
  13261. 9:31:55program. And you can kind of see that
  13262. 9:31:56the contents of RAM are nonsensical. Um,
  13263. 9:31:59but if I click this analyze program,
  13264. 9:32:01it'll actually go through and show you
  13265. 9:32:03what each of the instructions are, um,
  13266. 9:32:06or what the decimal value of that
  13267. 9:32:09address in RAM is. It has to do both
  13268. 9:32:11because the computer is naive. Uh, it
  13269. 9:32:14can't know if you're using a certain
  13270. 9:32:16address to denote an instruction or to
  13271. 9:32:18hold a hold a value. Um, and it turns
  13272. 9:32:21out any particular address can actually
  13273. 9:32:23be both. Um, but that's why I do that.
  13274. 9:32:27So you can see that our instructions
  13275. 9:32:29here we have load a a store a 15 a load
  13276. 9:32:33immediate 15 an out subtract 15 and out
  13277. 9:32:37a jump zero to address 10 a jump to
  13278. 9:32:40address 4 two empty instructions a halt
  13279. 9:32:45and then um at the very end we have
  13280. 9:32:48nothing down here and our address 15 is
  13281. 9:32:51zero because we have not run the program
  13282. 9:32:53yet. the first two instructions would
  13283. 9:32:55get the value one into RAM. Um, so this
  13284. 9:32:58is identical to what we uh created over
  13285. 9:33:00here. So that looks good. Let's go ahead
  13286. 9:33:03and toggle the clock and uh we can walk
  13287. 9:33:07through the very first instruction super
  13288. 9:33:09closely. So uh the very first thing we
  13289. 9:33:12have is this load immediate one. Let me
  13290. 9:33:14jump back to our other sheet and we can
  13291. 9:33:17see what our our
  13292. 9:33:19uh instructions our micro code should be
  13293. 9:33:22for this load immediate. So um the first
  13294. 9:33:26thing that every instruction should do
  13295. 9:33:29is get look up the value of the program
  13296. 9:33:31counter in RAM and get that uh get that
  13297. 9:33:34value. So we want to put the value of
  13298. 9:33:36the program counter onto the bus and
  13299. 9:33:38then read that into the memory address
  13300. 9:33:39register. If I tab over here, I can see
  13301. 9:33:42since we're on the first step here, uh,
  13302. 9:33:44those exact things are set. The program
  13303. 9:33:46counter is going out onto the bus. The
  13304. 9:33:48memory address register is reading that
  13305. 9:33:49in. Our clock is low right now. So, if I
  13306. 9:33:52pulse the clock from high, from low to
  13307. 9:33:54high, um, we should see that happen. Uh,
  13308. 9:33:57it's kind of uninteresting because all
  13309. 9:33:58the values are zero. The program counter
  13310. 9:34:00is zero. So, we're kind of putting zero
  13311. 9:34:02on the bus here. And, um, the memory
  13312. 9:34:04address register is already zero, so
  13313. 9:34:06when we read it in, uh, nothing really
  13314. 9:34:08changes. Um,
  13315. 9:34:11so, uh, let me go ahead and pulse the
  13316. 9:34:14clock from low to high. Nothing really
  13317. 9:34:16happens. But then when I let the clock
  13318. 9:34:18go back down, um, we are, we should end
  13319. 9:34:20up on step two. And we should see that
  13320. 9:34:22the next set of operations is getting
  13321. 9:34:25that output of RAM onto the bus and
  13322. 9:34:28reading it into the instruction
  13323. 9:34:29register, looking up the instruction
  13324. 9:34:31effectively, and then also incrementing
  13325. 9:34:32our program counter. So uh let me go
  13326. 9:34:35ahead and put the clock down and you can
  13327. 9:34:38see exactly those three lines are set.
  13328. 9:34:40Uh we increment our program counter and
  13329. 9:34:42then we are look taking whatever value
  13330. 9:34:44we got from RAM which should be this 01
  13331. 9:34:48and putting it into the instruction
  13332. 9:34:50register. And so there's this other
  13333. 9:34:51thing about this uh simulator where this
  13334. 9:34:54button right here will let us highlight
  13335. 9:34:57uh whatever the address in the memory
  13336. 9:34:59address register is on the RAM. So
  13337. 9:35:02memory address register has zero. So we
  13338. 9:35:04can see zero is highlighted uh as we
  13339. 9:35:06trace through the program. It's
  13340. 9:35:08interesting. You'll see that uh move a
  13341. 9:35:11lot. Um and so that now we we have done
  13342. 9:35:14the second step here. So what I need to
  13343. 9:35:17do is get the clock to be low again. So
  13344. 9:35:19I'll post uh back down. And the next
  13345. 9:35:22thing the very last thing we need to do
  13346. 9:35:24uh for load immediate is take the
  13347. 9:35:26argument because now we've decoded the
  13348. 9:35:27instruction at this point. We know it's
  13349. 9:35:29load immediate. So we know the argument
  13350. 9:35:31just needs to go out onto the bus, which
  13351. 9:35:33is what the instruction out does, and
  13352. 9:35:35read it into the A register. So
  13353. 9:35:39um let's go ahead and do that. And you
  13354. 9:35:42can see that on that rising clock edge,
  13355. 9:35:45the value gets latched in. So the
  13356. 9:35:47program counter is now one. And then our
  13357. 9:35:50instruction register has the uh value
  13358. 9:35:53and address zero, which is the
  13359. 9:35:54instruction we're trying to execute. And
  13360. 9:35:56that looks great. And so if I toggle the
  13361. 9:35:59clock, uh, we fall back low. And we can
  13362. 9:36:01see that our next set of operations that
  13363. 9:36:03we're going to do is exactly that. Move
  13364. 9:36:04the argument from the instruction
  13365. 9:36:06register, which is this 00001,
  13366. 9:36:08uh, into the A register. So when I pulse
  13367. 9:36:10the clock from low to high, this O1, uh,
  13368. 9:36:13should go into register A. Uh, and we
  13369. 9:36:16can see that's on the bus already, too.
  13370. 9:36:18So more interesting non-zero values to
  13371. 9:36:20work with. We can make some sense of it
  13372. 9:36:22now.
  13373. 9:36:24And cool. So the clock went from low to
  13374. 9:36:26high. And um you can see that the bus
  13375. 9:36:30now has the bus still has the same value
  13376. 9:36:32but register A has that immediate one
  13377. 9:36:34in. And then uh interestingly enough the
  13378. 9:36:37ALU is just always adding or subtracting
  13379. 9:36:39what's in the two registers A and B. So
  13380. 9:36:42we don't care about the ALU output for
  13381. 9:36:44this instruction but it is a correct
  13382. 9:36:45sanity check that like 0 + 1 is one. um
  13383. 9:36:49we know that and then we know from here
  13384. 9:36:52that after three clock cycles we've
  13385. 9:36:55implemented we've done all we need to do
  13386. 9:36:56for the load immediate instruction. So
  13387. 9:36:58the last two clock cycles are kind of
  13388. 9:36:59wasted. Um
  13389. 9:37:02and there's also some detail here. This
  13390. 9:37:05uh log will pretty much show you
  13391. 9:37:06everything that we just said. So like we
  13392. 9:37:08just had the rising clock edge where we
  13393. 9:37:10read the a registers value in from the
  13394. 9:37:12bus. Um, so that that happens as well.
  13395. 9:37:14And that's just a nice English
  13396. 9:37:16interpretation of what the computer is
  13397. 9:37:17doing with each clock edge. So if I
  13398. 9:37:20toggle the clock to low, um, you can see
  13399. 9:37:22it's a falling clock edge. Step counter
  13400. 9:37:24updated the four. Since it's a load
  13401. 9:37:26immediate, we've done our
  13402. 9:37:27implementation, so nothing happens. And,
  13403. 9:37:30um,
  13404. 9:37:32oops, I accidentally reset our SAP. Uh,
  13405. 9:37:34I clicked the reset button. So we've
  13406. 9:37:36we've gone back to to reset the state of
  13407. 9:37:38our computer. If I just toggle it to get
  13408. 9:37:40back to where we were. Um,
  13409. 9:37:42there's there's nothing crazy to it. So,
  13410. 9:37:45we're at step count five. We hit the
  13411. 9:37:47rise and clock edge and when I fall when
  13412. 9:37:50I fall, we're done with our instruction.
  13413. 9:37:52So, now we go our internal counter used
  13414. 9:37:54for the control resets its step count to
  13415. 9:37:57one. And now we know we're on the next
  13416. 9:37:59instruction. And uh that next
  13417. 9:38:01instruction, if I analyze our program,
  13418. 9:38:04was the storing that value into address
  13419. 9:38:0715. So, I'm not going to walk through
  13420. 9:38:08each of the five clock cycles here. What
  13421. 9:38:10I will do is I'll pulse the clock five
  13422. 9:38:12times until we get back to step counter
  13423. 9:38:14one. And what you'd expect to see if
  13424. 9:38:16everything's correct is that afterwards
  13425. 9:38:18the value in RAM at this address 1111
  13426. 9:38:22should equal um the value in the A
  13427. 9:38:25register which is just one. So, let's go
  13428. 9:38:28ahead. I'll pulse the clock five times.
  13429. 9:38:33And you can see that that's exactly
  13430. 9:38:35what's happened. the value in the uh
  13431. 9:38:38address 15 of our RAM is now that one.
  13432. 9:38:41Um so that looks good. Then
  13433. 9:38:45so I'm gonna say this looks good. This
  13434. 9:38:49looks good. Um now we need to load the
  13435. 9:38:52value 15 into register A. Let me pulse
  13436. 9:38:54the clock five more times to do that.
  13437. 9:38:59And so now we can see register A has
  13438. 9:39:01this value 1111. And then that looks
  13439. 9:39:05good. Then we display it to the user. So
  13440. 9:39:06let's pulse the clock five times. And
  13441. 9:39:08we'd expect the value 15 to be visible
  13442. 9:39:10here.
  13443. 9:39:13Uh, cool. What I should have done when I
  13444. 9:39:15built the simulator is add an option uh
  13445. 9:39:18not to just toggle the clock, but maybe
  13446. 9:39:19to toggle the clock five times or
  13447. 9:39:21through five clock cycles so I don't
  13448. 9:39:22have to, you know, keep punching it. But
  13449. 9:39:25uh, it's fine. It gets the job done, I
  13450. 9:39:26guess. Um, so that looks good.
  13451. 9:39:29Now we get into the the loop part here
  13452. 9:39:31where we subtract one display it and
  13453. 9:39:34then after that we either repeat that
  13454. 9:39:36that those two instructions or we end
  13455. 9:39:38the program. So uh basically what we're
  13456. 9:39:41going to do is we're going to do the
  13457. 9:39:43sub. So this should end with uh a being
  13458. 9:39:45one less than 15. So uh 1111 is 15. We'd
  13459. 9:39:51expect 11 1 0 to represent 14. And then
  13460. 9:39:54since we're doing 15 minus one, we'd
  13461. 9:39:56also expect that the value one should
  13462. 9:39:58probably get loaded into V register uh
  13463. 9:40:00to do the subtraction. So let me pulse
  13464. 9:40:03the clock five times.
  13465. 9:40:06And you can see both these things are
  13466. 9:40:07true. So we have 11 1 in the A register
  13467. 9:40:11which is 14. Uh we accomplish that by
  13468. 9:40:14loading one into the B register and
  13469. 9:40:15subtracting it. And uh that looks good.
  13470. 9:40:20And then
  13471. 9:40:22now we check this display a. So we
  13472. 9:40:24should expect 14 to just end up on the
  13473. 9:40:26display register because we've done our
  13474. 9:40:28subtraction. Uh we've done our
  13475. 9:40:29subtraction. So we should show this to
  13476. 9:40:30the user. So let's pulse the clock
  13477. 9:40:34through this next instruction. And you
  13478. 9:40:36can see that's the exact case. We have
  13479. 9:40:3814. And now uh let's do a little bit of
  13480. 9:40:40checking. Um now we should expect uh
  13481. 9:40:44this jump zero to not to be a noop. uh
  13482. 9:40:47we should not end up jumping to this
  13483. 9:40:49address right here. So if I pulse the
  13484. 9:40:52clock five times uh what the what should
  13485. 9:40:54happen afterwards is the program counter
  13486. 9:40:57should equal 0 uh 1111.
  13487. 9:41:01And that 011
  13488. 9:41:03is going to tell us to just jump back up
  13489. 9:41:05and repeat the loop. So let me pulse the
  13490. 9:41:07clock five times.
  13491. 9:41:12And we can see our program counter is
  13492. 9:41:14011 as we expect. So we uh did nothing
  13493. 9:41:18with this jump zero instruction. And
  13494. 9:41:20then this this 011 0 or sorry this 0111
  13495. 9:41:27011. Yes. Sorry. I was like did I say
  13496. 9:41:29one too many ones? Uh is just a jump
  13497. 9:41:31back up to
  13498. 9:41:34uh our
  13499. 9:41:36our subtract loop. So I'd expect the
  13500. 9:41:39program counter to go to um
  13501. 9:41:43address 4 afterwards of this. Let's
  13502. 9:41:46pulse it and see ourselves.
  13503. 9:41:49And we can see exactly that our program,
  13504. 9:41:50our program counter is address 4. And so
  13505. 9:41:53then we repeat this uh this loop cycle.
  13506. 9:41:56So I also built an auto play because I
  13507. 9:41:57don't want to trace through this entire
  13508. 9:41:58program. I built an auto play that'll
  13509. 9:42:01just keep pulsing the clock like really
  13510. 9:42:03fast, low, and high. Uh and you can
  13511. 9:42:05control the speed here.
  13512. 9:42:07Um and basically I'm just going to crank
  13513. 9:42:09the speed all the way up. And we should
  13514. 9:42:10see that we just count down all the way
  13515. 9:42:13to zero.
  13516. 9:42:20one and we get to zero and we end up
  13517. 9:42:24executing this halt instruction which
  13518. 9:42:27remember it just blocks the program from
  13519. 9:42:29doing anything further. So you can kind
  13520. 9:42:30of see if I scroll to the bottom of the
  13521. 9:42:32logs here. Um, we set this halt line and
  13522. 9:42:35then there's a rising edge of the clock,
  13523. 9:42:36but because the clock is connected to
  13524. 9:42:39the value of the halt line, it actually
  13525. 9:42:41blocks the the clock the true clock
  13526. 9:42:43value from propagating out to the rest
  13527. 9:42:45of the computer. And so that's how we
  13528. 9:42:47block execution.
  13529. 9:42:49Okay, so this is pretty cool. We wrote a
  13530. 9:42:52simple program uh and it actually does
  13531. 9:42:54something. uh it it it we chain together
  13532. 9:42:58these very simple operations
  13533. 9:43:01and uh use that to build a program that
  13534. 9:43:03does something. And this is cool because
  13535. 9:43:06not only like we started from scratch
  13536. 9:43:08and that we implemented these micro
  13537. 9:43:10instructions ourselves and each one of
  13538. 9:43:13those required like very granular uh
  13539. 9:43:16changes to the state of our system. But
  13540. 9:43:19then when you cycle change uh bundle a
  13541. 9:43:22few of those changes into what we called
  13542. 9:43:23an instruction, the instruction can do
  13543. 9:43:25something a little bit powerful. Not
  13544. 9:43:27really that meaningful on its own, but
  13545. 9:43:29something just a little bit like one
  13546. 9:43:31action of like move this value from here
  13547. 9:43:33to here or you know show this value on
  13548. 9:43:36the display or or maybe jump to this
  13549. 9:43:38other instruction instead of executing
  13550. 9:43:39the one that's below me. Um, and so on
  13551. 9:43:42on their own, each of those instructions
  13552. 9:43:44are kind of trivial, but then when we
  13553. 9:43:46chain them together, we were actually
  13554. 9:43:48able to write a program
  13555. 9:43:50um that actually does something
  13556. 9:43:52interesting and and has a termination
  13557. 9:43:54condition. And what's also pretty cool
  13558. 9:43:56about it is like that program is encoded
  13559. 9:43:59with literally just zeros and ones. And
  13560. 9:44:02a few hours ago, this would have been
  13561. 9:44:04gibberish to you. Uh this this these
  13562. 9:44:07combinations of zeros and ones would
  13563. 9:44:08have had no intrinsic value. But it
  13564. 9:44:11turns out for this specific computer
  13565. 9:44:13we've designed, this exact ordering of
  13566. 9:44:16zeros and ones produces a program that
  13567. 9:44:18does something very interesting to us.
  13568. 9:44:21We've constructed is trivial, but it
  13569. 9:44:23paints a very interesting uh portrait of
  13570. 9:44:26the capabilities of our computer. And
  13571. 9:44:29what I'd like to do is you can probably
  13572. 9:44:31imagine the full set of what we can
  13573. 9:44:33really do with these eight or so
  13574. 9:44:35instructions is fairly limited,
  13575. 9:44:37especially because we only have 16
  13576. 9:44:39different spots in RAM. So the programs
  13577. 9:44:41you write are inherently constrained um
  13578. 9:44:45by the the limitations of how we've
  13579. 9:44:47constructed our computer. More modern
  13580. 9:44:49computers have a lot more RAM and a lot
  13581. 9:44:51more instructions, and we'll talk about
  13582. 9:44:53that shortly. But, uh, I'd like to try
  13583. 9:44:56build out something that does something
  13584. 9:44:58a little more complicated than counting
  13585. 9:45:01down from 15. Let's see what the most
  13586. 9:45:03complicated thing we can do with this
  13587. 9:45:05instruction set that we have is. So, um,
  13588. 9:45:08let's go over here to Wikipedia. So, a
  13589. 9:45:11sequence, just as a quick definition,
  13590. 9:45:13uh, a sequence in mathematics, a
  13591. 9:45:16sequence is a sequence of integers. Uh,
  13592. 9:45:19that's kind of a circular definition,
  13593. 9:45:21but it's literally just a list of
  13594. 9:45:22numbers. And um there's one sequence in
  13595. 9:45:26in particular that's fairly interesting
  13596. 9:45:29and it's called the Fibonacci sequence.
  13597. 9:45:31So let's read the definition. In
  13598. 9:45:33mathematics, the Fibonacci sequence is a
  13599. 9:45:36sequence in which each element is the
  13600. 9:45:39sum of the two elements that precede it.
  13601. 9:45:42So basically we start with the number
  13602. 9:45:45zero and one and then every element
  13603. 9:45:48after that is just the sum of the
  13604. 9:45:51previous two elements. So here we have 0
  13605. 9:45:54and one. The sum of that is one. So
  13606. 9:45:56that's our next number. And then if we
  13607. 9:45:58have 1 + 1 as the last two elements, the
  13608. 9:46:01sum of those is two. And then the last
  13609. 9:46:03two elements becomes one and two. The
  13610. 9:46:05sum of which is three. And then the last
  13611. 9:46:08two elements are now two and three. The
  13612. 9:46:10sum of which becomes five. And you can
  13613. 9:46:12see that it keeps going. We have 3 + 5
  13614. 9:46:15is 8. 8 + 5 is 13. 13 + 8 is 21. 21 + 13
  13615. 9:46:20is 34. And uh it keeps going. It grows
  13616. 9:46:24unbounded. And what I'd like to do is
  13617. 9:46:27build a program on our computer that can
  13618. 9:46:30actually count out the Fibonacci
  13619. 9:46:31sequence for us. And it turns out the
  13620. 9:46:34instruction set that we have with our
  13621. 9:46:36computer is totally capable of doing
  13622. 9:46:38exactly this. Um and let's also tab out
  13623. 9:46:42and think about uh something more
  13624. 9:46:44abstract in mathematics and computer
  13625. 9:46:47science. Uh like what is an algorithm? I
  13626. 9:46:50remember when I was learning
  13627. 9:46:51computation, kept on hearing talks about
  13628. 9:46:53algorithms. There's classes you take
  13629. 9:46:55with the computer science degree.
  13630. 9:46:57There's one that most people take called
  13631. 9:46:59data structures and algorithms. Um, and
  13632. 9:47:01I remember like we we kind of talk about
  13633. 9:47:04like you know like what are social media
  13634. 9:47:05algorithms? Like like what's what's the
  13635. 9:47:08logic behind that? Um, and an algorithm
  13636. 9:47:11has a surprisingly simple definition.
  13637. 9:47:14It is a process or set of rules to be
  13638. 9:47:17followed in calculations or other
  13639. 9:47:18problems solving operations, especially
  13640. 9:47:20by a computer. I like the Wikipedia
  13641. 9:47:22definition a little bit better. So, I'm
  13642. 9:47:23going to go over here. Um, Wikipedia
  13643. 9:47:27says, "In mathematics and computer
  13644. 9:47:29science, an algorithm is a finite
  13645. 9:47:32sequence of mathematically rigorous
  13646. 9:47:34instructions typically used to solve a
  13647. 9:47:36class of specific problems or to perform
  13648. 9:47:38a computation.
  13649. 9:47:41Algorithms are used as specifications
  13650. 9:47:42for performing calculations and data
  13651. 9:47:44processing. More advanced algorithms can
  13652. 9:47:47use conditionals to divert the code
  13653. 9:47:49execution through various routes and
  13654. 9:47:52deduce valid inferences. So this is an
  13655. 9:47:54interesting definition. Uh I think it's
  13656. 9:47:57a more interesting one for a few
  13657. 9:47:58reasons. Um first off we saw that uh we
  13658. 9:48:03just performed a computation of what we
  13659. 9:48:05decided earlier. the counting down was
  13660. 9:48:07just a series of computations. And then
  13661. 9:48:11uh that was a form of data processing,
  13662. 9:48:13but like that's not really what the
  13663. 9:48:14definition is poking at here. But what's
  13664. 9:48:16also interesting is uh it says advanced
  13665. 9:48:19algorithms can divert execution through
  13666. 9:48:21various routes. And
  13667. 9:48:24um this is kind of the branching we
  13668. 9:48:26discovered with our our jump zero and
  13669. 9:48:29our jump carry instructions that allow
  13670. 9:48:31us to introduce some like decision
  13671. 9:48:33making into our code. So what I want to
  13672. 9:48:36do is write an algorithm for
  13673. 9:48:38implementing this Fibonacci sequence and
  13674. 9:48:41then go ahead and actually implement it.
  13675. 9:48:43Um I think a good starting point is
  13676. 9:48:46coming back to this very simple program
  13677. 9:48:48that we had earlier and and writing an
  13678. 9:48:50algorithm for it. So uh if we if we
  13679. 9:48:53wrote an algorithm for this program uh
  13680. 9:48:55what would it look like about exactly
  13681. 9:48:58what we did to implement our our
  13682. 9:49:01instructions here? um we have to be very
  13683. 9:49:04concrete and formal with what we said
  13684. 9:49:07and uh specify everything very clearly.
  13685. 9:49:09We have to tell the computer because the
  13686. 9:49:10computer uh especially the one we built
  13687. 9:49:13is kind of quote unquote dumb and it'll
  13688. 9:49:15only tell you it'll only do exactly what
  13689. 9:49:17you tell it to do. So what we kind of
  13690. 9:49:19did is we said um we had some variable
  13691. 9:49:22called the counter. We'll just say like
  13692. 9:49:24let counter equals 15. And in the
  13693. 9:49:28algorithm, the algorithm is kind of
  13694. 9:49:29independent of the hardware of the
  13695. 9:49:31computer itself. When we went to
  13696. 9:49:33implement this algorithm in machine
  13697. 9:49:35code, uh our counter was kind of the A
  13698. 9:49:38register. That was kind of the register
  13699. 9:49:39that held the primary state of the
  13700. 9:49:42program at any given time. And we pretty
  13701. 9:49:45much said while the counter was greater
  13702. 9:49:48than or equal to um sorry while the
  13703. 9:49:51counter is greater than zero
  13704. 9:49:54what we did is we subtracted one from
  13705. 9:49:56the counter. So we said counter equals
  13706. 9:49:58counter minus one and then display
  13707. 9:50:01counter
  13708. 9:50:03and basically this this syntax I'm using
  13709. 9:50:06here is an indentation. So like every
  13710. 9:50:08time this condition is true, everything
  13711. 9:50:10that's executed after it or everything
  13712. 9:50:12that's indented after it will be
  13713. 9:50:13executed and then it'll check the
  13714. 9:50:16condition again. It'll come back up
  13715. 9:50:17here. Um that's similar to the the
  13716. 9:50:19jumping we have in our code. And then
  13717. 9:50:22once we get out of this while loop, so
  13718. 9:50:24well after the only way we can get out
  13719. 9:50:26of this while loop is if the counter is
  13720. 9:50:28not greater than zero. And since we're
  13721. 9:50:30counting down by one, that means it
  13722. 9:50:32necessarily is equal to zero. And so
  13723. 9:50:34that means once we get past here, we
  13724. 9:50:36just end and we're done. So this is a
  13725. 9:50:38very formal specification
  13726. 9:50:41um of our counter program. Let's try
  13727. 9:50:43doing for the Fibonacci sequence. So let
  13728. 9:50:45me clear it out here.
  13729. 9:50:47So in general we said the Fibonacci
  13730. 9:50:50sequence like for any number it is the
  13731. 9:50:53sum of the previous two numbers. So like
  13732. 9:50:57if we let uh let's define like f of i to
  13733. 9:51:01be the i
  13734. 9:51:05uh number in the Fibonacci sequence.
  13735. 9:51:09So we can pretty much say that uh let's
  13736. 9:51:13say it's zero. We've been learning
  13737. 9:51:14everything starting with zero. So f of
  13738. 9:51:16zero is zero. The first number in the
  13739. 9:51:19sequence is zero and the second number
  13740. 9:51:21in the sequence is one. And then for
  13741. 9:51:23every other number, we pretty much say
  13742. 9:51:25that f of n is equal to the sum the nth
  13743. 9:51:29fibonacci number is equal to the sum of
  13744. 9:51:32the number behind it and the number
  13745. 9:51:34that's two behind it.
  13746. 9:51:38And so this is kind of what we call a
  13747. 9:51:39recursive definition where the value of
  13748. 9:51:42the Fibonacci sequence, the value of f
  13749. 9:51:44of n depends on recursive calls to f of
  13750. 9:51:48n. Um, so that makes sense for like us
  13751. 9:51:50to rebound English, you know, this is
  13752. 9:51:52just saying start with the numbers 0 and
  13753. 9:51:54one and then after that for any other
  13754. 9:51:57arbitrary position in the Fibonacci
  13755. 9:51:59sequence, go figure out what the two
  13756. 9:52:01Fibonacci numbers before are and then
  13757. 9:52:03add those together and that tells you
  13758. 9:52:05what the current Fibonacci number is.
  13759. 9:52:07Assuming Ed is the nth number in the
  13760. 9:52:10sequence that you're interested about.
  13761. 9:52:12Um, our computer we've built here
  13762. 9:52:14obviously can't deduce that. uh we kind
  13763. 9:52:16of have to spill that out in a slightly
  13764. 9:52:18different way. So what we're going to do
  13765. 9:52:22um is let's go figure out how we can
  13766. 9:52:24actually build this. So we know that we
  13767. 9:52:26have these instructions to work with and
  13768. 9:52:29we also have uh spots in data memory. So
  13769. 9:52:31I'm going to go ahead and clear out our
  13770. 9:52:33old program here.
  13771. 9:52:36And basically
  13772. 9:52:39I'm going to define I'm going to reserve
  13773. 9:52:41two spots in memory. Let's just do the
  13774. 9:52:43last two spots in RAM to represent um
  13775. 9:52:46like
  13776. 9:52:54the f of n minus one and the f of n
  13777. 9:52:57minus2 here. So like the last two values
  13778. 9:53:00in RAM will always represent uh the last
  13779. 9:53:02two numbers in the Fibonacci sequence.
  13780. 9:53:04And then if we can get those correctly,
  13781. 9:53:07uh if we can always guarantee that to be
  13782. 9:53:09true, then this means to find the next
  13783. 9:53:12Fibonacci sequence. Well, to start, we
  13784. 9:53:14need to load the values like zero and
  13785. 9:53:17one into RAM here. And you can imagine
  13786. 9:53:19that's pretty straightforward to do. But
  13787. 9:53:21then afterwards to get the next
  13788. 9:53:23Fibonacci number, all we need to do is
  13789. 9:53:25add the values at these two addresses
  13790. 9:53:27together. That gives you the next
  13791. 9:53:29Fibonacci number. You can display that.
  13792. 9:53:32And then once you have the next
  13793. 9:53:33Fibonacci number, the n minus one's
  13794. 9:53:36number becomes the n minus2 number. So
  13795. 9:53:39you can replace this number right here
  13796. 9:53:42with what's there because we no longer
  13797. 9:53:43care about this one for the next
  13798. 9:53:44Fibonacci sequence uh number. So we'd
  13799. 9:53:48like pretty much say that this becomes a
  13800. 9:53:50one and then this value here becomes
  13801. 9:53:54what the result of the alou was which
  13802. 9:53:56also happens to be one because that is
  13803. 9:53:58what 0 plus one is. And so let's do one
  13804. 9:54:01more practice of that. Um to get if this
  13805. 9:54:04is our n minus2 and n minus one numbers
  13806. 9:54:07then the current the f of n i i.e. the
  13807. 9:54:10next Fibonacci sequence number would be
  13808. 9:54:121 + one. So we could add these two
  13809. 9:54:14values together. Um we get the value
  13810. 9:54:17two. We can store that somewhere else in
  13811. 9:54:20RAM temporarily. And then this number
  13812. 9:54:23would need to we want to throw this away
  13813. 9:54:25and shift this one up. And then now
  13814. 9:54:28write our two into this spot right here.
  13815. 9:54:32And so now we have one two as our last
  13816. 9:54:34two numbers in the Fibonacci sequence.
  13817. 9:54:36And we obviously want to display that to
  13818. 9:54:37the user in there as well. Um
  13819. 9:54:41and so now if we do that one more time
  13820. 9:54:43just for sake of tying it together
  13821. 9:54:45because we worked with zeros and one so
  13822. 9:54:46maybe the shifting is not clear. But we
  13823. 9:54:49would to get uh if we have these as our
  13824. 9:54:51last two numbers in the Fibonacci
  13825. 9:54:52sequence for wherever our program's at,
  13826. 9:54:54we add these two values in RAM together
  13827. 9:54:56using the add instruction. Um and then
  13828. 9:55:00we'd get three as the result. We can
  13829. 9:55:02store that somewhere arbitrary in
  13830. 9:55:03memory. And then we need to wipe this
  13831. 9:55:06one away and shift this two up and then
  13832. 9:55:10bring this three down because that's now
  13833. 9:55:12our last Fibonacci number. Um, so if we
  13834. 9:55:15kind of think about how we want to
  13835. 9:55:17specify this as an as an algorithm, um,
  13836. 9:55:21let's just call this value like a and
  13837. 9:55:25this value b. And a and b are always the
  13838. 9:55:29last two numbers in the Fibonacci
  13839. 9:55:31sequence. So what we'd want to do is
  13840. 9:55:34pretty much start by getting the values
  13841. 9:55:37zero and one into these spots and rams.
  13842. 9:55:41Um because this is uh the the first two
  13843. 9:55:44numbers in the Fibonacci sequence,
  13844. 9:55:47we kind of know that RAM is all zeroed
  13845. 9:55:49out. So the value here will already be
  13846. 9:55:51zero because of like how our program
  13847. 9:55:53instantiates. Like we're just going to
  13848. 9:55:55say that RAM is all zeros.
  13849. 9:55:57But we need to get a one into here. So
  13850. 9:56:00that's pretty straightforward. Um
  13851. 9:56:04like pretty much we can say a is already
  13852. 9:56:06zero. Then we can say B becomes one. And
  13853. 9:56:10then what we'd like to do is uh
  13854. 9:56:16add at any step the two values together.
  13855. 9:56:19So we make a new value called C and C is
  13856. 9:56:23equal to A plus B.
  13857. 9:56:25And then we can display C to the user
  13858. 9:56:29because that's the next Fibonacci
  13859. 9:56:30number. And then we need to say that uh
  13860. 9:56:33A becomes B and B becomes C. And that's
  13861. 9:56:37how we kind of shift up one here. And um
  13862. 9:56:41after that we can pretty much jump back
  13863. 9:56:45to uh jump back to uh the C= A plus B
  13864. 9:56:51instruction. Um and so what we do here
  13865. 9:56:54is we we get our first two Fibonacci
  13866. 9:56:56values and then we begin a cycle of
  13867. 9:56:59calculating uh the next Fibonacci
  13868. 9:57:02number. So our every time we loop
  13869. 9:57:04through and jump back up to this, we're
  13870. 9:57:06trying to find the next Fibonacci
  13871. 9:57:07sequence, assuming A and B are the last
  13872. 9:57:10two Fibonacci numbers we worked with. So
  13873. 9:57:12by definition of the Fibonacci sequence,
  13874. 9:57:14we can trivially compute the next value
  13875. 9:57:16just by adding A and B together. And
  13876. 9:57:19then that's the next Fibonacci number.
  13877. 9:57:21We display that to the user. And then
  13878. 9:57:24now that we have three Fibonacci
  13879. 9:57:25numbers, we throw away the oldest. The
  13880. 9:57:28oldest is A. And so then B becomes the
  13881. 9:57:31older one. Now, so uh what was
  13882. 9:57:34previously in the Bcomes assigned to A
  13883. 9:57:37and then what was in B is now our newest
  13884. 9:57:41Fibonacci number which is the one we
  13885. 9:57:42just calculated up here. So we can set
  13886. 9:57:44that equal to the value of C. Um and so
  13887. 9:57:47this is pretty straightforward. Uh let's
  13888. 9:57:51actually go ahead and this is like kind
  13889. 9:57:53of our formal algorithm. Um so I guess
  13890. 9:57:56like if we wouldn't make this fully
  13891. 9:57:58algorithm uh we could say like uh wall
  13892. 9:58:02like one is equal to one. So just this
  13893. 9:58:07is wall one is always equal to one. So
  13894. 9:58:09this will always be true
  13895. 9:58:11and
  13896. 9:58:15this is kind of what the uh the the code
  13897. 9:58:17would look like. So this would keep
  13898. 9:58:18counting Fibonacci sequence numbers
  13899. 9:58:20forever. We start with uh like you can
  13900. 9:58:23say like let a start with zero, let b
  13901. 9:58:26start with one and then wall one equals
  13902. 9:58:28one is just kind of a little hack around
  13903. 9:58:32like the algorithm specification to say
  13904. 9:58:34like do this over and over. And what we
  13905. 9:58:36do over and over is add our previous two
  13906. 9:58:37Fibonacci numbers together.
  13907. 9:58:40Show that to the user and then update
  13908. 9:58:43what our last two Fibonacci numbers are.
  13909. 9:58:45So throw away that now that we have
  13910. 9:58:46three numbers A, B, and C. uh shift it
  13911. 9:58:50so that what was previous like uh what's
  13912. 9:58:53currently B and C becomes into A and B.
  13913. 9:58:55So we set A to B and then B to C. So
  13914. 9:59:00let's think about how we can implement
  13915. 9:59:02this using our instruction set that we
  13916. 9:59:04have available to us on this computer.
  13917. 9:59:06So we already know by assumption just
  13918. 9:59:09that the value in uh in a uh is zero. So
  13919. 9:59:14what we need to do is get the value one
  13920. 9:59:16here. So I'm going to load an immediate
  13921. 9:59:18one
  13922. 9:59:20um into the A register. So this just
  13923. 9:59:22says let me not bold here. Register a
  13924. 9:59:25equals 1. And then
  13925. 9:59:29uh
  13926. 9:59:30afterwards we can go ahead and get
  13927. 9:59:34straight into the um branching logic. So
  13928. 9:59:40what that would look like is we need to
  13929. 9:59:43uh sorry we actually need to take the
  13930. 9:59:44value we have and store it into register
  13931. 9:59:47uh store it into this address value. So
  13932. 9:59:50we take the one that we have and store
  13933. 9:59:52that into address 15. So this would just
  13934. 9:59:55say like b equals 1.
  13935. 10:00:00And then now what we can do is go ahead
  13936. 10:00:02and start the sequence of adding these
  13937. 10:00:05two numbers together. So to add two
  13938. 10:00:07values in RAM, we need to load one of
  13939. 10:00:09the values into the A register and then
  13940. 10:00:12load one of the other and then do an add
  13941. 10:00:14operation with uh the second value. So
  13942. 10:00:17what we can do is load a with 14
  13943. 10:00:21and then so it just says like register a
  13944. 10:00:25equals a. Uh there's a differentiation
  13945. 10:00:28here. Uh register A, you already know
  13946. 10:00:29what register A is. The A down here is
  13947. 10:00:32just a variable that we are calling. uh
  13948. 10:00:34variable meaning uh we will say whatever
  13949. 10:00:36is in this uh we're going to call a and
  13950. 10:00:39that value can change it can vary so
  13951. 10:00:42it's called a variable um apologies I
  13952. 10:00:45probably use that several times without
  13953. 10:00:46necessarily clarifying what a variable
  13954. 10:00:48is but uh forgive me for that and then
  13955. 10:00:52now we can add whatever is in 15
  13956. 10:00:56so then it pretty much says that
  13957. 10:00:58register A is equal to A plus B so
  13958. 10:01:02that's exactly what we want and And that
  13959. 10:01:03that's a plus b is the c value we've
  13960. 10:01:06described here. So let's denote let's
  13961. 10:01:09let c equal this equal um address 13 in
  13962. 10:01:13RAM. So let's go ahead and just uh we
  13963. 10:01:16want to take that value uh the output
  13964. 10:01:18here and store it in C. But before we do
  13965. 10:01:20that we want to show this to the user.
  13966. 10:01:22So we can do an out here and then we can
  13967. 10:01:25store the value C into the correct
  13968. 10:01:27memory address that we want which is
  13969. 10:01:291101.
  13970. 10:01:32And
  13971. 10:01:34um the ordering between these two
  13972. 10:01:36operations actually does not matter.
  13973. 10:01:38It's kind of up to us that's how we want
  13974. 10:01:39it. Uh so you know you could have done
  13975. 10:01:41you could have stored it and then
  13976. 10:01:42written the out. Uh and the reason we
  13977. 10:01:44can do that is because uh store a and
  13978. 10:01:48out neither of those change what's in
  13979. 10:01:51the A register. They just do some action
  13980. 10:01:53based on what's in the A register. So
  13981. 10:01:55this add instruction over here actually
  13982. 10:01:58mutates the value in the A register. The
  13983. 10:02:00following two have no effect to the A
  13984. 10:02:02register. They're independent operations
  13985. 10:02:04we'd like to do based on whatever was
  13986. 10:02:06the outcome of this ad result here. So
  13987. 10:02:08the ordering is trivial. It's it's not
  13988. 10:02:10perceivable to the human and does not
  13989. 10:02:12affect the runtime correctness of our
  13990. 10:02:14program. Um assuming everything else is
  13991. 10:02:16right here. Um so now we have our like
  13992. 10:02:20last three uh Fibonacci numbers like A
  13993. 10:02:23is the oldest, B is the second like in
  13994. 10:02:27the middle and then C is the newest. Um
  13995. 10:02:31and so what we need to do then is kind
  13996. 10:02:33of throw away this A, move the value in
  13997. 10:02:35B into A and then move the value from C
  13998. 10:02:38into B. So let's start by uh taking the
  13999. 10:02:42value here and then getting it into
  14000. 10:02:44here. So all we can need to do to get
  14001. 10:02:46the value from B into A is do a load A
  14002. 10:02:51uh of 15. So get the value that's in B
  14003. 10:02:53into the A register and then a store A
  14004. 10:02:55where we store it into this address. Uh
  14005. 10:02:57so that's how we move the value that's
  14006. 10:02:59how we effectively move the value which
  14007. 10:03:01overwrites the A with B. Um so what we
  14008. 10:03:05do to do that is a load A5.
  14009. 10:03:09So we say like register a equals b and
  14010. 10:03:12then uh we can store a into 14 and that
  14011. 10:03:17just says a equals what was like a is
  14012. 10:03:21now what b uh is and then the last thing
  14013. 10:03:24we need to do is get the value from c
  14014. 10:03:28into the into the b address bot. So I
  14015. 10:03:31can load into register a this address
  14016. 10:03:3413. So it says register A equals C. And
  14017. 10:03:38then I can use the same trick that we
  14018. 10:03:40did to overwrite A uh to overwrite B. So
  14019. 10:03:43I can store that into 15.
  14020. 10:03:56Um and then the last thing we need to do
  14021. 10:03:59to continue this loop is to we've so
  14022. 10:04:01this is calculated given Fibonacci
  14023. 10:04:04numbers A and B. Um everything we've
  14024. 10:04:07done here
  14025. 10:04:09uh
  14026. 10:04:13calculates
  14027. 10:04:15the next Fibonacci number. So now that
  14028. 10:04:18we've calculated one Fibonacci number,
  14029. 10:04:20let's just jump back up here and jump to
  14030. 10:04:23instruction two and execute the next uh
  14031. 10:04:26the same set of operations to figure out
  14032. 10:04:29the next Fibonacci number. So I can just
  14033. 10:04:31jump to two
  14034. 10:04:33uh one zero
  14035. 10:04:37and then this says continue. Um so if I
  14036. 10:04:40kind of map that out, these are our
  14037. 10:04:42instructions.
  14038. 10:04:44Um we can kind of see that we let a we
  14039. 10:04:47get this first one for free. This a is
  14040. 10:04:49equal to zero. We get that for free just
  14041. 10:04:51because we already know that RAM
  14042. 10:04:53defaults to zero values. So then the
  14043. 10:04:56next one that we have to do ourselves is
  14044. 10:04:57getting the value letting B equal one.
  14045. 10:04:59And that's exactly what we're doing um
  14046. 10:05:02in these two instructions right here.
  14047. 10:05:03We're getting the value one into
  14048. 10:05:05register A and then writing that into B.
  14049. 10:05:08That's how we do this. We have to do two
  14050. 10:05:09instructions because of how restrictive
  14051. 10:05:11our instructions that we have defined
  14052. 10:05:13are. But they exist like there's enough
  14053. 10:05:15instructions to do what we want to do.
  14054. 10:05:17And then you can kind of see that uh
  14055. 10:05:20this is our loop part. But so C equals A
  14056. 10:05:23plus B is implemented right here because
  14057. 10:05:26we load A into one of the ALU operands
  14058. 10:05:28and then B into the other ALOU operands
  14059. 10:05:30and we add those together and we get the
  14060. 10:05:33value C and then we store that uh into
  14061. 10:05:36the value C. Sorry. So this is these
  14062. 10:05:39three instructions implement C equals A
  14063. 10:05:41plus B. Then the display C is trivially
  14064. 10:05:44implemented with the out instruction.
  14065. 10:05:47And then
  14066. 10:05:50these four instructions implement the
  14067. 10:05:53shifting um of replacing
  14068. 10:05:57um A and B uh with B and C.
  14069. 10:06:02And this um program if we let it run
  14070. 10:06:06will count Fibonacci numbers for us. So,
  14071. 10:06:09what I've already gone ahead and done is
  14072. 10:06:11kind of loaded some of these values into
  14073. 10:06:13RAM. Let me go ahead and clear
  14074. 10:06:16uh this because that's not correct.
  14075. 10:06:20And if I reset everything here, let's go
  14076. 10:06:22ahead and run the analyze program and
  14077. 10:06:25see what we have. And so, uh, if I'm
  14078. 10:06:28matching up what is over here with over
  14079. 10:06:30here, I see a load immediate one, a
  14080. 10:06:33store a 15, a load a 14, an ad with 15,
  14081. 10:06:37a store with 13, a display, a load a
  14082. 10:06:41with 15, a store a with 14, a load a of
  14083. 10:06:4613, and a store a with 15, and then a
  14084. 10:06:48jump back up to two. So, this is the
  14085. 10:06:51correct program. I didn't specify the
  14086. 10:06:52RAM values here, uh, but they are
  14087. 10:06:55punched in here. Um,
  14088. 10:06:58so let's go ahead and just let it rip.
  14089. 10:07:00Uh, I'll autoplay this, but uh, the
  14090. 10:07:03Fibonacci sequence, we can I'll type out
  14091. 10:07:05some of the numbers so we know what to
  14092. 10:07:06expect. Um, we know that we start with
  14093. 10:07:08zero and one. So, we'd expect to see
  14094. 10:07:10like one and then, uh, like a a two and
  14095. 10:07:15then three, five, five, 8, 13, 21, 34,
  14096. 10:07:2455.
  14097. 10:07:2589. Uh, and so on and so on. Um, so
  14098. 10:07:28let's just let it rip and see what
  14099. 10:07:30happens. Uh, one, that looks good. Two,
  14100. 10:07:34that looks good. Three looks good.
  14101. 10:07:38Five looks good.
  14102. 10:07:41Eight looks good.
  14103. 10:07:4313 looks good.
  14104. 10:07:4621.
  14105. 10:07:4834 looks good. 55 looks good. 89 looks
  14106. 10:07:54good. And um what we're kind of doing is
  14107. 10:07:58is verifying the program's correctness
  14108. 10:07:59just by letting it run and see. Uh
  14109. 10:08:01that's not the most sophisticated
  14110. 10:08:04method. Uh but you know, for the sake of
  14111. 10:08:06not boring and I can already verify that
  14112. 10:08:08it does look correct. Uh that's fine. Um
  14113. 10:08:12so
  14114. 10:08:14what's interesting here is I don't have
  14115. 10:08:15any end condition. I intentionally did
  14116. 10:08:18not add a halt here uh just for fun
  14117. 10:08:21because I want to see like kind of what
  14118. 10:08:22happens with our computer. So we know
  14119. 10:08:24that we operate on 8bit values and the
  14120. 10:08:28biggest value uh we can represent in 8
  14121. 10:08:30bits um is the value 1111 1111 and
  14122. 10:08:36that's equal to 1 + 2 + 4 + 8 + 16 + 32
  14123. 10:08:42+ 64 + 128. So 255
  14124. 10:08:46if every bit position is enabled that
  14125. 10:08:49gives us the binary string of all ones
  14126. 10:08:52and that is the maximum value we can
  14127. 10:08:54represent here and that's 255. So what's
  14128. 10:08:57interesting is our program
  14129. 10:09:00will work up to 233. If I continue it uh
  14130. 10:09:04there should be no issue but then the
  14131. 10:09:05next value after 233 is 144 + 233 which
  14132. 10:09:10gets you 377.
  14133. 10:09:12But we actually can't represent that in
  14134. 10:09:14eight bits. Um, and so it turns out what
  14135. 10:09:18happens is there's like overflow. But
  14136. 10:09:20the computer we saw when we built the
  14137. 10:09:22ALOU, uh, if you have an ALOU operation
  14138. 10:09:26that can't be stored in eight bits,
  14139. 10:09:29it'll only capture the rightmost eight
  14140. 10:09:31bits there. So,
  14141. 10:09:34um, what this means is that our program
  14142. 10:09:36will not break. the computer will not
  14143. 10:09:37somehow explode or anything crazy like
  14144. 10:09:40that, but we won't see 233. Uh we will
  14145. 10:09:43go from 144 to 233 to something that is
  14146. 10:09:48less than 377. Um and it's unclear just
  14147. 10:09:51based on whatever the binary string is,
  14148. 10:09:53the rightmost 8 bits of 377. Um what
  14149. 10:09:57that is. So if I let this rip a little
  14150. 10:09:59bit longer, uh let's go ahead and see
  14151. 10:10:02exactly that.
  14152. 10:10:0789 144 233 and this is where we should
  14153. 10:10:13fail. 121 is what we just happen to oops
  14154. 10:10:16happen to get back to. Um and the the
  14155. 10:10:18computer won't break. It's just a matter
  14156. 10:10:20of fact that we say like for our ALU the
  14157. 10:10:23way it is designed is such that it
  14158. 10:10:25assumes that the result is always
  14159. 10:10:27storeable in uh an 8bit value but that's
  14160. 10:10:31not guaranteeable. Um and we have the
  14161. 10:10:34carry flag for detecting this entirely.
  14162. 10:10:37So when we did go from
  14163. 10:10:40um 233 to 377, if I had paused the
  14164. 10:10:44program, we would have seen that the
  14165. 10:10:45carry flag was set. So what we could
  14166. 10:10:47have done is recognize that hey, this is
  14167. 10:10:49the largest Fibonacci value our computer
  14168. 10:10:52can handle. And so we could have done uh
  14169. 10:10:54similar to how we used the jump zero to
  14170. 10:10:56end the program in the previous uh
  14171. 10:10:59program uh in the counting sequence.
  14172. 10:11:02What we could have done is used a jump
  14173. 10:11:03carry to end the program here when we
  14174. 10:11:05know we get to the biggest value that is
  14175. 10:11:07represented uh representable. I just
  14176. 10:11:10chose not to do that because RAM was
  14177. 10:11:11running out of space. Um
  14178. 10:11:14but you know, you can imagine if this
  14179. 10:11:16was something more defensible, uh you
  14180. 10:11:18would probably want to do that. Um but I
  14181. 10:11:21think this is probably one of the more
  14182. 10:11:23complicated programs our simple computer
  14183. 10:11:25can make. Um, but I'd also like to
  14184. 10:11:28remark on the fact that
  14185. 10:11:30uh this is pretty powerful. Like we saw
  14186. 10:11:32a combination of zeros and ones for a
  14187. 10:11:34counting program. And it turns out when
  14188. 10:11:37you interpret those as instructions for
  14189. 10:11:38this computer, they do something
  14190. 10:11:40interesting. And now we're seeing
  14191. 10:11:41something pretty interesting here where
  14192. 10:11:43this combination of zeros and ones does
  14193. 10:11:46something totally unrelated that's also,
  14194. 10:11:48you know, a little more powerful and
  14195. 10:11:50very interesting. So this is really
  14196. 10:11:53cool. um like something that was just a
  14197. 10:11:56few hours ago nonsense to you is now
  14198. 10:11:59something that you can you know you can
  14199. 10:12:00look at each of these positions in RAM
  14200. 10:12:02and figure out what the what the
  14201. 10:12:05interpretation is and if RAM was loaded
  14202. 10:12:08to that value on some hypothetical
  14203. 10:12:10computer uh that's what the computer
  14204. 10:12:12would execute. Um,
  14205. 10:12:16so that technically concludes the
  14206. 10:12:18explanation of how we built this
  14207. 10:12:20computer and everything should be clear
  14208. 10:12:22from a like ground up like and we'll
  14209. 10:12:24recap everything we've done eventually,
  14210. 10:12:26but um it it wouldn't be right for me
  14211. 10:12:29just to end here. Like I think there's a
  14212. 10:12:31lot of questioning around like we've
  14213. 10:12:33invented this computer but like why do
  14214. 10:12:35we design the instructions the way they
  14215. 10:12:36are and like why do we architect it the
  14216. 10:12:38way we architected it and like how did I
  14217. 10:12:41know to build this computer? Um, these
  14218. 10:12:44are all interesting questions and I
  14219. 10:12:47actually built this computer physically.
  14220. 10:12:49So, uh, the simulator I have here is
  14221. 10:12:52cool and all, but I was very interested
  14222. 10:12:54when I learned this in school. And I
  14223. 10:12:56found the series online of someone who
  14224. 10:12:58has a full instructional set, which I'll
  14225. 10:13:00link because it's an awesome series that
  14226. 10:13:02I highly recommend watching. And in this
  14227. 10:13:04series, uh, Ben Eer, his name is, uh,
  14228. 10:13:08builds this computer, this conceptual
  14229. 10:13:10computer, uh, from scratch. So, uh, I
  14230. 10:13:13actually built one and I can show it
  14231. 10:13:16here. This is what mine looks like in
  14232. 10:13:18person. Um, it's a bit messy and there's
  14233. 10:13:20a lot going on. So, let's let me give a
  14234. 10:13:23few, uh, few bits few minutes of context
  14235. 10:13:25to explain what's happening here. So, if
  14236. 10:13:28you look just on the display, uh, you
  14237. 10:13:30can see this program is just doing
  14238. 10:13:32counting down. Um, my video, I guess I
  14239. 10:13:35just happen to start it at the number
  14240. 10:13:3671. And then by the time it loops
  14241. 10:13:39around, uh, looks like it's down to like
  14242. 10:13:41something like what was that? 59. Um,
  14243. 10:13:44but there's a lot of components here.
  14244. 10:13:46So, let's break it down.
  14245. 10:13:49The first is this clock up here. Um, and
  14246. 10:13:53I guess as you as I walk through this,
  14247. 10:13:55you'll see that the way this is
  14248. 10:13:56physically built, i.e. the layout of all
  14249. 10:13:58the components is virtually identical to
  14250. 10:14:01what I drew on my iPad uh throughout the
  14251. 10:14:04video. And we'll see that like that also
  14252. 10:14:07came from somewhere. Um, I guess the
  14253. 10:14:09ordering of the components physically
  14254. 10:14:10doesn't matter, but I I kept the
  14255. 10:14:12continuity there to help uh make the
  14256. 10:14:14understanding easier. So, this is the
  14257. 10:14:17clock module. Uh, you can kind of see
  14258. 10:14:20this is the value of the clock and it's
  14259. 10:14:22just constantly pulsing. This thing up
  14260. 10:14:25here is a little dial and if I were to
  14261. 10:14:27twist it, I could increase or decrease
  14262. 10:14:29the uh speed of the clock. And then this
  14263. 10:14:32clock is just always pulsing up and
  14264. 10:14:34down. um kind of like the uh you know
  14265. 10:14:37the autoplay mode I had in our
  14266. 10:14:38simulator, but you don't always want to
  14267. 10:14:41auto play. Sometimes you want to step
  14268. 10:14:43through specific high low clock cycles.
  14269. 10:14:45So there's a switch over here and if you
  14270. 10:14:48switch that it turns off the automatic
  14271. 10:14:50clock and lets you just pulse the clock
  14272. 10:14:52using this button right here. Um so
  14273. 10:14:54every time you push the button it'll go
  14274. 10:14:56from low to high and vice versa. So
  14275. 10:14:57that's pretty cool. That's the clock.
  14276. 10:15:00Here's our program counter right here.
  14277. 10:15:02You can kind of see it's counting up. uh
  14278. 10:15:04if you repeat those binary numbers and
  14279. 10:15:06then it jumps uh back down. So it's
  14280. 10:15:08probably like the subtracting loop is a
  14281. 10:15:11loop. We saw that in our subtracting
  14282. 10:15:12program. And then these are the flags
  14283. 10:15:15registers. So uh the wiring gets tricky
  14284. 10:15:18to trace here, but uh you can kind of
  14285. 10:15:20see that there's this these wires here.
  14286. 10:15:22Uh this is a register chip and that's
  14287. 10:15:24the it seems to be the output of the
  14288. 10:15:26register chip and the input on top comes
  14289. 10:15:28from this wire right here. Um and this
  14290. 10:15:31is the ALU. So let's talk about that
  14291. 10:15:33next. This is the mass of ALOU. We have
  14292. 10:15:37uh the A register right here. And these
  14293. 10:15:39lights show the value of the A register.
  14294. 10:15:41And then we have the
  14295. 10:15:43um B register should be right here. And
  14296. 10:15:48then this light right here. Uh I can't
  14297. 10:15:51quite recall if it's showing whatever's
  14298. 10:15:52on the B register or the output of the
  14299. 10:15:54ALU. And then you can kind of see that
  14300. 10:15:57this thing in the middle is the bus. So
  14301. 10:15:59the same way, here's our connection of
  14302. 10:16:00the ALU to the bus, the B register to
  14303. 10:16:03the bus. Here's the ALU to the bus.
  14304. 10:16:05Here's the A register to the bus. And
  14305. 10:16:07here's the program counter to the bus.
  14306. 10:16:08And then these lights up here show you
  14307. 10:16:11the value in binary that's on the bus at
  14308. 10:16:13any given time.
  14309. 10:16:15Below the ALOU, we have our output
  14310. 10:16:17display. So we have four seven segment
  14311. 10:16:19displays chained together. This is the
  14312. 10:16:21readonly memory and this is the
  14313. 10:16:23register. Um so the value in the
  14314. 10:16:26register feeds into this read only
  14315. 10:16:27memory which decodes which lines it
  14316. 10:16:29should set on each of the displays to
  14317. 10:16:31show the value that we want. Um and
  14318. 10:16:34similarly that also connects uh into the
  14319. 10:16:36bus.
  14320. 10:16:38So then let's jump over here. This is
  14321. 10:16:42our memory address register the 4bit
  14322. 10:16:44memory address register and that reads
  14323. 10:16:45in from the bus. And um this is our
  14324. 10:16:49entire RAM unit right here. And then
  14325. 10:16:52this would be the uh output of the RAM
  14326. 10:16:54unit. And then there's something
  14327. 10:16:56interesting where in our simulator we
  14328. 10:16:58were kind of just literally clicking
  14329. 10:16:59values to punch in updates to the
  14330. 10:17:01memory. Uh in real world, we actually
  14331. 10:17:03have these these manual switches up
  14332. 10:17:05here. And I can swap. Uh so if I if I if
  14333. 10:17:08this light is green, it means the
  14334. 10:17:10computer's in execution mode. If I flip
  14335. 10:17:12the switch, uh, it'll actually instead
  14336. 10:17:14of looking at the value in the memory
  14337. 10:17:16address register, um, or the value on
  14338. 10:17:18the bus, I can use these these blue
  14339. 10:17:21switches over here to punch in a
  14340. 10:17:23specific address and then punch in a
  14341. 10:17:25specific value. And then if I pulse this
  14342. 10:17:27button over here, it'll latch that value
  14343. 10:17:29into a specific address in RAM. Um,
  14344. 10:17:32these are really small and honestly a
  14345. 10:17:34very big pain to use. Um, but that is
  14346. 10:17:37how I loaded the program into memory. I
  14347. 10:17:39kind of have to flip the switch so it's
  14348. 10:17:41on human program mode and then pulse
  14349. 10:17:44through cycle through every address that
  14350. 10:17:45was used uh for my program, load the
  14351. 10:17:48value and then punch it in and then swap
  14352. 10:17:50the address and rinse and repeat. And
  14353. 10:17:52then once that's I could flip the switch
  14354. 10:17:54back uh and then also flip the clock to
  14355. 10:17:56automatic mode and just let it rip. Um
  14356. 10:18:00this will show you the output. Uh so for
  14357. 10:18:02whatever address is on the memory
  14358. 10:18:04address register, the ALU is always
  14359. 10:18:06looking it up. So this is just always
  14360. 10:18:08showing the output. You can kind of see
  14361. 10:18:09like every time the value changes on the
  14362. 10:18:12input side, the value on the output side
  14363. 10:18:14changes as well. Unsurprisingly,
  14364. 10:18:17um here is our instruction register. So
  14365. 10:18:20the instruction register is eight bits,
  14366. 10:18:21but the left four bits are the argument
  14367. 10:18:24and the right four bits, sorry, the left
  14368. 10:18:25four bits are the operation code, i.e.
  14369. 10:18:28which instruction we're executing at any
  14370. 10:18:29moment in time. And the right four bits
  14371. 10:18:32are the uh argument to that instruction.
  14372. 10:18:35So I used separate color lights to
  14373. 10:18:37denote the two. Um but you can see it's
  14374. 10:18:39just backed by register and that also
  14375. 10:18:41connects to the bus. And then here is
  14376. 10:18:44the meat uh of our computer, the brains
  14377. 10:18:47that tie it all together. And that's the
  14378. 10:18:49control logic. So this is the control
  14379. 10:18:51logic. These are readon memory chips
  14380. 10:18:53that I literally have to take apart out
  14381. 10:18:55of the computer and program and then
  14382. 10:18:56punch them back in here. If we look at
  14383. 10:18:58what the inputs are here, um you can see
  14384. 10:19:03that um we have the uh four bits of the
  14385. 10:19:09instruction register. So the operation
  14386. 10:19:11code and then we also have this
  14387. 10:19:13five-step uh counter that represents
  14388. 10:19:16which step of the current instruction
  14389. 10:19:17we're on. The way it's implemented in
  14390. 10:19:19person is a little bit different than
  14391. 10:19:21the way I explained it. Uh but the
  14392. 10:19:22concept is roughly the same or it's it's
  14393. 10:19:24it's fundamentally the same. So it
  14394. 10:19:26doesn't matter. I didn't want to
  14395. 10:19:27introduce this technically uses a
  14396. 10:19:29different type of counter. And the way
  14397. 10:19:30it's visualized is whatever step you're
  14398. 10:19:32on, uh, that light will be off. The
  14399. 10:19:35clock's going so fast that you can't
  14400. 10:19:36really tell. It just kind of looks like
  14401. 10:19:37they're flicker. But if I were to tune
  14402. 10:19:39down the clock speed, you'd watch a left
  14403. 10:19:42to right pattern and then it' reset uh
  14404. 10:19:44in terms of which one is the quote
  14405. 10:19:46unquote off. And then the uh the value
  14406. 10:19:49from the flags register is also hooked
  14407. 10:19:52in here. Um, and then we can see that
  14408. 10:19:56these yellow wires are just going
  14409. 10:19:57everywhere because that's the the
  14410. 10:19:59control output like when we say like RAM
  14411. 10:20:02out or memory address in or program
  14412. 10:20:04counter increment like that has to go to
  14413. 10:20:06each of the components. So you can see
  14414. 10:20:07like some of these yellow wires going to
  14415. 10:20:09the program counter, some going to the
  14416. 10:20:10memory address register control, uh some
  14417. 10:20:13to the ALU operands, uh some to the
  14418. 10:20:15output display, and then for debugging
  14419. 10:20:18purposes, I also duplicated it so I
  14420. 10:20:21could actually see at any step what
  14421. 10:20:23lines were being set by the control
  14422. 10:20:25logic. So that's right here. Um can't
  14423. 10:20:27really tell, but there's a label. So
  14424. 10:20:29like this leftmost light is on if maybe
  14425. 10:20:32like the program counter increment line
  14426. 10:20:33is on. um in the simulator we built that
  14427. 10:20:36roughly controls to being able to
  14428. 10:20:38visualize this right here.
  14429. 10:20:40So this is the computer. Um I built it
  14430. 10:20:43over the pandemic. And the reason I even
  14431. 10:20:46came across this is I I learned about it
  14432. 10:20:48in school for a class I took for my
  14433. 10:20:49computer science degree.
  14434. 10:20:51And uh the class uh we didn't build this
  14435. 10:20:55computer as part of the class. But we
  14436. 10:20:56did learn how this computer architecture
  14437. 10:20:58worked uh as a significant portion of
  14438. 10:21:01the class. But the professor of that
  14439. 10:21:04class is also not the one who invented
  14440. 10:21:05this computer. Um this computer has a
  14441. 10:21:08name. I've probably let it slip a few
  14442. 10:21:10times and maybe you could have seen it
  14443. 10:21:12already over here in the naming. This
  14444. 10:21:14computer is called SAP 1 SAP1. Um, let's
  14445. 10:21:19go ahead and Google it.
  14446. 10:21:22Go straight to Wikipedia because we've
  14447. 10:21:24been using that. And this says, "The
  14448. 10:21:26simplest possible computer is a
  14449. 10:21:29simplified computer architecture
  14450. 10:21:30designed for educational purposes and
  14451. 10:21:33described in the book digital computer
  14452. 10:21:35electronics by Albert Paul Molino. Um
  14453. 10:21:39the SAP architecture serves an as
  14454. 10:21:42uh for building and analyzing complex
  14455. 10:21:44logical systems with digital
  14456. 10:21:46electronics. So this is an old book. Um,
  14457. 10:21:50and then, uh, interestingly enough, this
  14458. 10:21:51Wikipedia also talks about, uh, Ben Eer,
  14459. 10:21:54the individual whose video I watched to
  14460. 10:21:57learn how to build my computer here. Um,
  14461. 10:22:01but let's actually, uh, go ahead. If
  14462. 10:22:04you, Turns out, if you Google the book
  14463. 10:22:06name, um, it's old enough that it's just
  14464. 10:22:09online,
  14465. 10:22:12uh, on the, uh, archive. Um, and I mean,
  14466. 10:22:15you can see this is kind of an old book
  14467. 10:22:16here. Uh it seems as though this came
  14468. 10:22:19from 1993 or at least this version of
  14469. 10:22:22the book. Um so this is old here but uh
  14470. 10:22:25what's really cool is this book kind of
  14471. 10:22:28does a lot of what we did in the video
  14472. 10:22:29from scratch which is taking no
  14473. 10:22:31understanding of how computation works
  14474. 10:22:33and building a fully functioning
  14475. 10:22:35computer that demystifies a lot of the
  14476. 10:22:37blackbox of like what is happening on
  14477. 10:22:39your phone or on your computer under the
  14478. 10:22:41screen or like in the internals. So if I
  14479. 10:22:43skim through the chapters here before he
  14480. 10:22:46even gets to talking about the computer
  14481. 10:22:48architecture um you see it it starts
  14482. 10:22:51with number systems and that's where
  14483. 10:22:53this video started. We started with you
  14484. 10:22:54know binary how do we convert from
  14485. 10:22:56binary to decimal um and you can kind of
  14486. 10:22:59see that a lot of that is here like
  14487. 10:23:00decimal to binary and vice versa. And
  14488. 10:23:03then after that we talked about logic
  14489. 10:23:05gates you know we we know about the
  14490. 10:23:06inverter gate the orgate andgate um the
  14491. 10:23:10some of these other logic gates we also
  14492. 10:23:11covered like we know about NAND or the
  14493. 10:23:14exclusive orgate because we needed them
  14494. 10:23:15to all build this computer.
  14495. 10:23:18Then uh we talked about some of these
  14496. 10:23:21without naming them. Um but like
  14497. 10:23:23multiplexers we came across for our
  14498. 10:23:24computer and same for here uh like some
  14499. 10:23:29of our implementation our intuition for
  14500. 10:23:31implementing the ROM was using actually
  14501. 10:23:33some of products I just didn't formally
  14502. 10:23:34say it cuz I didn't want to bog down
  14503. 10:23:36with terms um and once we had an
  14504. 10:23:39understanding of logic gates we went
  14505. 10:23:40ahead and built the ALU uh and that was
  14506. 10:23:42able to support bi binary addition
  14507. 10:23:45subtraction and we learned about we
  14508. 10:23:47started with half adders and then
  14509. 10:23:49chained those together um or we got half
  14510. 10:23:51adders and we chain those with full
  14511. 10:23:53adders to build our ALU.
  14512. 10:23:56Um, and then once we had the ALU
  14513. 10:23:58working, we moved into sequential logic.
  14514. 10:24:01Everything we had been looking up until
  14515. 10:24:02that point was deterministic. Uh,
  14516. 10:24:04sequential logic was flip-flops and
  14517. 10:24:06latches and how we actually hold data.
  14518. 10:24:08And you can kind of see that's like a
  14519. 10:24:10fundamental necessity to implement our
  14520. 10:24:12computer because that's just like the
  14521. 10:24:13crux of RAM and register is the need to
  14522. 10:24:16be able to persist data. um that was
  14523. 10:24:19possibly the output of some other
  14524. 10:24:21deterministic circuit. After that uh you
  14525. 10:24:24know these are some of the other
  14526. 10:24:25registers. We didn't really go into too
  14527. 10:24:27much there but then we talked about
  14528. 10:24:28memories. We talked about uh RAM for
  14529. 10:24:31sure. We talked about readonly memories
  14530. 10:24:33and then we also talked about the
  14531. 10:24:35history of some of the readonly memory
  14532. 10:24:36types. We talked about programmable
  14533. 10:24:38readonly memories and electronically
  14534. 10:24:40programmable readonly memories and then
  14535. 10:24:42EROMs
  14536. 10:24:44and that was like the foundation of all
  14537. 10:24:46and then SAP one is the computer we
  14538. 10:24:49actually built and that's where our
  14539. 10:24:51context cuts off. The book keeps going
  14540. 10:24:53and there's a lot more here. Um I've
  14541. 10:24:56never read this book but it's probably
  14542. 10:24:59pretty great. Um, it's very impressive.
  14543. 10:25:01But the reason it's called SAP one
  14544. 10:25:04instead of just SAP, SAP standing for
  14545. 10:25:05simplest possible, uh, is that the
  14546. 10:25:08author finishes the computer we
  14547. 10:25:10constructed and then builds two more
  14548. 10:25:12variants that are just more complicated
  14549. 10:25:14and get you closer to more modern
  14550. 10:25:16computers. And we'll talk about some of
  14551. 10:25:19those changes at a high level. Um but if
  14552. 10:25:22we actually jump to the chapter uh
  14553. 10:25:24starting on page 140 for SAP one
  14554. 10:25:27um
  14555. 10:25:29here is what we see uh and you know
  14556. 10:25:31skimming this I didn't read this too
  14557. 10:25:33closely either but uh the architecture
  14558. 10:25:35is is pretty identical like we have the
  14559. 10:25:37program counter we have memory address
  14560. 10:25:38register we have a RAM um and you can
  14561. 10:25:41see the diagram is even identical to the
  14562. 10:25:43one we designed when constructing our
  14563. 10:25:45computer like you have two operants to
  14564. 10:25:47the ALU we had a program counter we had
  14565. 10:25:49a memory address register hooking into
  14566. 10:25:51RAM and then we had an instruction
  14567. 10:25:52register down here uh with the readonly
  14568. 10:25:54memory to decode uh whatever the input
  14569. 10:25:57is to it and then an output register
  14570. 10:25:59with a display. Um and one thing that I
  14571. 10:26:03do find interesting is if we hop down to
  14572. 10:26:05the instruction set of the computer uh
  14573. 10:26:07the author here only implemented few
  14574. 10:26:10instructions. So there's quite literally
  14575. 10:26:12only a load a an add subtract an out and
  14576. 10:26:16then a halt. We have implemented uh far
  14577. 10:26:18more instructions
  14578. 10:26:20than what is presented here and some of
  14579. 10:26:23them are very interesting. Specifically,
  14580. 10:26:25the jump instructions and the
  14581. 10:26:26conditional jumping instructions are
  14582. 10:26:29what allowed us to build out uh both the
  14583. 10:26:31the counting down program and the
  14584. 10:26:33Fibonacci sequence. But what's cool is
  14585. 10:26:35that we were able to implement those
  14586. 10:26:37instructions without making any real
  14587. 10:26:39changes to the hardware. Like we didn't
  14588. 10:26:41add new components that aren't in this
  14589. 10:26:42architecture specified up here. you
  14590. 10:26:45know, we were just able to manipulate
  14591. 10:26:47with incremental changes to what we had
  14592. 10:26:49already built uh to get the computer uh
  14593. 10:26:51the instructions that we wanted. So,
  14594. 10:26:54this is super interesting. Um I don't
  14595. 10:26:57think it's probably worth reading the
  14596. 10:26:58entire book here, especially since we've
  14597. 10:27:00covered most of what in my opinion is
  14598. 10:27:02the fundamentals. Um, but I felt like
  14599. 10:27:05credit was due because I did not invent
  14600. 10:27:07this computer and uh it's very
  14601. 10:27:09impressive that this computer exists and
  14602. 10:27:12I'd say it's very effective um for its
  14603. 10:27:15goal of building like educational
  14604. 10:27:17purposes and explaining how computers
  14605. 10:27:18work from scratch. The computer we
  14606. 10:27:21designed is great for educational
  14607. 10:27:24purposes that we as we just talked
  14608. 10:27:25about, but it's still pretty abstract
  14609. 10:27:28and there's a pretty big gap if I ended
  14610. 10:27:30the video right here of like how does
  14611. 10:27:32this actually scale to a modern
  14612. 10:27:34computer. So, what I want to use the
  14613. 10:27:36rest of the video for is showing like
  14614. 10:27:38what a lot of this looks like in
  14615. 10:27:39actuality and how we take this
  14616. 10:27:41hypothetical hypothetical computer we
  14617. 10:27:43invented and scale it up to something
  14618. 10:27:45more modern. Um, I want to start by
  14619. 10:27:48taking a look at the very first iPhone.
  14620. 10:27:51Uh, I remember when the original iPhone
  14621. 10:27:54came out and the first time I played
  14622. 10:27:55around with it, I was amazed. And I
  14623. 10:27:58think iPhones are interesting to look at
  14624. 10:27:59because they have a lot of computing
  14625. 10:28:02constraints, uh, but are still pack an
  14626. 10:28:05impressive amount of technology into
  14627. 10:28:06something that fits in the size of your
  14628. 10:28:08hand. um you know they're striking
  14629. 10:28:10things like battery life and then
  14630. 10:28:13limited storage but then also pack a lot
  14631. 10:28:15of rich features like very high quality
  14632. 10:28:18displays and they appear to be very
  14633. 10:28:19responsive and you can do several things
  14634. 10:28:22at the same time. You can take a call
  14635. 10:28:24while you're browsing the internet and
  14636. 10:28:26uh for that I think iPhones are pretty
  14637. 10:28:28interesting. So, I'm going to hop over
  14638. 10:28:30to the uh
  14639. 10:28:32the Wikipedia for the first generation
  14640. 10:28:35iPhone, and I'm going to make a claim
  14641. 10:28:38that this iPhone and every computer
  14642. 10:28:41iPhone out there has a processor that is
  14643. 10:28:44not too fundamentally different from
  14644. 10:28:46what we've designed. You can imagine
  14645. 10:28:49that these phones are obviously capable
  14646. 10:28:50of a lot more. uh the only output our
  14647. 10:28:53toy computer has is a simple seven
  14648. 10:28:55segment display versus you know iPhones
  14649. 10:28:58have an entire screen they can draw to.
  14650. 10:29:00So I want to compare the instruction set
  14651. 10:29:02of the computer we built with that of
  14652. 10:29:04the iPhone um and see like what about
  14653. 10:29:07that instruction set is different that
  14654. 10:29:08lets like lets us scale up from a toy
  14655. 10:29:11computer to something that's that's
  14656. 10:29:12actually tangible. Um so looking at this
  14657. 10:29:16Wikipedia page there's a few things that
  14658. 10:29:18stand out. The first is um this iPhone
  14659. 10:29:22has the original iPhone has 128
  14660. 10:29:25megabytes of RAM. So what is a megabyte?
  14661. 10:29:29Let's start off with a bite. We know
  14662. 10:29:30what a bite is. A bite is just eight
  14663. 10:29:32bits. And um our our RAM that we
  14664. 10:29:37designed had two to the four uh
  14665. 10:29:39different slots. We had four bit
  14666. 10:29:40addressing. So we had 16 bytes of RAM in
  14667. 10:29:43our computer. Now a kilobyte is a
  14668. 10:29:47thousand bytes. the hence the kilo
  14669. 10:29:49prefix. A megabyte is then a thousand
  14670. 10:29:52kilobytes which equals a million bytes.
  14671. 10:29:55So we have roughly I'm saying roughly
  14672. 10:29:58here 128 million bytes of address base
  14673. 10:30:03in our RAM compared to the 16 that our
  14674. 10:30:05toy computer had. So two things come to
  14675. 10:30:08mind. Um in one dimension this is a lot
  14676. 10:30:12of memory. You know, we saw that the
  14677. 10:30:14programs we built used almost the
  14678. 10:30:16entirety of the memory of the SAP
  14679. 10:30:17computer, but that's not going to be as
  14680. 10:30:20much of a concern if we have 128 million
  14681. 10:30:23uh slots of RAM. The second thing uh is
  14682. 10:30:27that the same amount of RAM, we could
  14683. 10:30:30argue that it's a lot relative to the
  14684. 10:30:31computer we've constructed, but it's
  14685. 10:30:33also trivial when you compare it to to
  14686. 10:30:36more modern phones. um or the fact that
  14687. 10:30:39if I take a picture on my iPhone and I
  14688. 10:30:41took one right before this just for sake
  14689. 10:30:42of illustration the size of that picture
  14690. 10:30:45is 2.5 megabytes which is a lot you know
  14691. 10:30:49this on one hand we want to say this is
  14692. 10:30:51a lot of memory but given how much
  14693. 10:30:53memory is scaled up in modern phones
  14694. 10:30:55this is no longer a lot and your phone
  14695. 10:30:57has uh several gigabytes of RAM which is
  14696. 10:31:00an order higher uh on on the dimension
  14697. 10:31:02scale a gigabyte would be uh thousand
  14698. 10:31:05megabytes Um, scrolling down though,
  14699. 10:31:09that's just one thing I wanted to call
  14700. 10:31:10out. Scrolling down, there's a lot of
  14701. 10:31:13um, a lot more technology packed into
  14702. 10:31:15this phone. So, for example, we
  14703. 10:31:17obviously know there's a touchcreen, but
  14704. 10:31:19then there's also a microphone, which
  14705. 10:31:21you need to make calls. Then there's
  14706. 10:31:24Wi-Fi and Bluetooth. Wi-Fi for accessing
  14707. 10:31:26the internet, Bluetooth for connecting
  14708. 10:31:28to other devices like headsets. Um, and
  14709. 10:31:31our computer that we designed doesn't
  14710. 10:31:33have any of these. So, if I'm making the
  14711. 10:31:36claim that the processor of the iPhone
  14712. 10:31:38is not too fundamentally different than
  14713. 10:31:40the processor we described, uh, one of
  14714. 10:31:43the questions I would eventually like to
  14715. 10:31:44answer is how do we bridge the gap and
  14716. 10:31:47get these things like how do we get
  14717. 10:31:49internet connectivity? How can we even
  14718. 10:31:50draw to a screen? How do we know when a
  14719. 10:31:52user touches on the screen? Um, and
  14720. 10:31:55these are questions that I want to get
  14721. 10:31:56to in the remainder of this video. But
  14722. 10:31:58let's keep poking a little bit further
  14723. 10:32:00and let's actually look at the processor
  14724. 10:32:02that's on this phone. Um, so the second
  14725. 10:32:05sentence here says it features a Samsung
  14726. 10:32:07S5L8900
  14727. 10:32:09SOC. Um, and SOC just stands for system
  14728. 10:32:13on chip and it generally means that you
  14729. 10:32:15have a lot of functionality baked into
  14730. 10:32:17one chip rather than having uh several
  14731. 10:32:20isolated components. So one chip could
  14732. 10:32:23have the CPU, i.e. everything we've
  14733. 10:32:26built thus far in the video and maybe uh
  14734. 10:32:29the networking chip, the chip needed to
  14735. 10:32:31like make internet requests versus more
  14736. 10:32:34traditional computers would have these
  14737. 10:32:36things be totally separate and they just
  14738. 10:32:38be connected via wires. Um so let's read
  14739. 10:32:41into the the system on chip that this
  14740. 10:32:43early iPhone used. And um
  14741. 10:32:47we can a few things stand out here. Um
  14742. 10:32:51obviously these Wikipedias are very
  14743. 10:32:52informationrich. The first thing I
  14744. 10:32:54wanted to call out is that let's read
  14745. 10:32:56from here. The default clock rate of the
  14746. 10:32:59CPU is normally 666 MHz but has been
  14747. 10:33:02lowered by Apple to about 12 412 MHz. So
  14748. 10:33:08what is a megahertz? Let's start with
  14749. 10:33:10what a hertz is. A hertz is just a unit
  14750. 10:33:13of measurement for frequency. And
  14751. 10:33:15roughly speaking or actually precisely
  14752. 10:33:17speaking uh a hertz just means a cycle
  14753. 10:33:19per second. So in the computer we built
  14754. 10:33:23you know the the clock we can crank it
  14755. 10:33:25up or down to make the computer run
  14756. 10:33:26faster. And I think roughly in its uh in
  14757. 10:33:30the maximal video I showed demonstration
  14758. 10:33:33it was probably running at like two or
  14759. 10:33:35three hertz. Uh I think we we're just
  14760. 10:33:37going through a few few clock cycles
  14761. 10:33:39every second. Um a megahertz actually is
  14762. 10:33:43equ one megahertz is equivalent to a
  14763. 10:33:45million hertz. So, what this is saying
  14764. 10:33:48is that um the chip itself that's used
  14765. 10:33:51in the original iPhone can can hit
  14766. 10:33:53roughly 666 million clock cycles per
  14767. 10:33:56second. Um and Apple has toned that down
  14768. 10:34:00to about 412 uh million clock cycles per
  14769. 10:34:03second. A logical question might be why
  14770. 10:34:05would they ever lower this? You would
  14771. 10:34:07assume that by making the clock run
  14772. 10:34:09faster, it can execute more instructions
  14773. 10:34:12in the same amount of time. So there is
  14774. 10:34:14an incentive to make the phone feel as
  14775. 10:34:16fast as possible by cranking up the the
  14776. 10:34:19clock frequency. But it turns out uh
  14777. 10:34:22there's nothing free, no free lunch. And
  14778. 10:34:24that applies to computation as well. And
  14779. 10:34:27that phones are constrained and one of
  14780. 10:34:30the obvious constraints is battery life.
  14781. 10:34:33Yes, cranking up the clock frequency
  14782. 10:34:34will let you do more instructions per
  14783. 10:34:36unit time, but uh it also means that the
  14784. 10:34:40computer is going to be doing overall
  14785. 10:34:42more work per unit time, which means it
  14786. 10:34:44has to consume more battery to do so per
  14787. 10:34:46unit time. These logic gates at the end
  14788. 10:34:48of the day that we're challen
  14789. 10:34:52you can crank the clock frequency up,
  14790. 10:34:54but it's going to eat at battery. So,
  14791. 10:34:56there's a happy medium where you want to
  14792. 10:34:58push it lower low enough where it's not
  14793. 10:35:01uh too slow for the user. Um,
  14794. 10:35:05at the point where we can try and
  14795. 10:35:06maximize battery life for the phones.
  14796. 10:35:09Um, but that's just something I wanted
  14797. 10:35:11to call out. The other thing which isn't
  14798. 10:35:13labeled explicitly here is our SAP used
  14799. 10:35:16five clock cycles per instruction.
  14800. 10:35:20more modern processors, including likely
  14801. 10:35:22this one right here, even though it's no
  14802. 10:35:24longer considered modern by any
  14803. 10:35:25standard. Modern in the scale of
  14804. 10:35:27computing. How about that? Um, they
  14805. 10:35:30actually beat this. They can do one
  14806. 10:35:31instruction per clock cycle. And
  14807. 10:35:35uh I don't want to make things too
  14808. 10:35:36confusing because this might not even be
  14809. 10:35:38uh easy to wrap your head around, but a
  14810. 10:35:40lot of modern processors can actually do
  14811. 10:35:42multiple instructions per clock cycle.
  14812. 10:35:45Um, so that's pretty interesting. But
  14813. 10:35:48let's continue and in and looking at the
  14814. 10:35:50CPU itself. So
  14815. 10:35:53um the first thing is that uh let's just
  14816. 10:35:56read the start of the sentence. And
  14817. 10:35:58there's a lot of tech like technical
  14818. 10:35:59terms going on here. So I'm just going
  14819. 10:36:02to read these first uh five or six
  14820. 10:36:03words. The SL S5 L8900 contains a 32bit
  14821. 10:36:10ARM V6 compatible CPU core. What does
  14822. 10:36:15this mean? So, let's start with the
  14823. 10:36:1732-bit. Um, our computer that we built
  14824. 10:36:21was an 8-bit computer, which means
  14825. 10:36:23fundamentally every value we reason
  14826. 10:36:26about is 8 bits. So, all of our
  14827. 10:36:28instructions were 8 bits. The bus itself
  14828. 10:36:30was 8 bits and every value in RAM was 8
  14829. 10:36:34bits. But we saw how this could be
  14830. 10:36:35pretty limiting because it meant you had
  14831. 10:36:37four bits to represent all your
  14832. 10:36:39instructions, which is not that many
  14833. 10:36:40different instructions. That's 16
  14834. 10:36:42different instructions. And then every
  14835. 10:36:45uh instruction that took an argument
  14836. 10:36:46used four bits to represent the argument
  14837. 10:36:49which meant you were pretty limited in
  14838. 10:36:51how much you could express. 32 bits uh
  14839. 10:36:55is a lot more. So if I uh 2 to the 4 our
  14840. 10:36:59our 8bit computer um had 256 different
  14841. 10:37:02values it could represent. Uh it's if we
  14842. 10:37:06raise this to the 30 32nd power this is
  14843. 10:37:08incomprehensibly large. Um that would be
  14844. 10:37:124 bill294,967,296
  14845. 10:37:18uh you know different values we can
  14846. 10:37:19represent in in RAM alone specifically.
  14847. 10:37:23Um, so a lot bigger. But then again,
  14848. 10:37:27this might feel big relative to the
  14849. 10:37:29computer we've constructed, which is 8
  14850. 10:37:30bits, but a lot of modern computers
  14851. 10:37:33actually use more than this 64-bit
  14852. 10:37:35architectures or 128-bit architectures.
  14853. 10:37:39But as we saw with the case with um
  14854. 10:37:42clocks, you might think like, hey, why
  14855. 10:37:44would we not just try to scale this up
  14856. 10:37:46and use as much as possible? But phones
  14857. 10:37:48aren't just constrained by battery,
  14858. 10:37:50they're also constrained by memory. So,
  14859. 10:37:53um, if a a computer that uses 64-bit
  14860. 10:37:56architecture
  14861. 10:37:58versus a computer that uses 32-bit
  14862. 10:38:00architecture, well, in a 64-bit
  14863. 10:38:02architecture, each instruction is 64
  14864. 10:38:04bits. But in a 32-bit architecture, each
  14865. 10:38:07instruction is 32bits. So, with the same
  14866. 10:38:10amount of space, you can represent twice
  14867. 10:38:12as many instructions on a 32-bit
  14868. 10:38:14architecture compared to a 64-bit
  14869. 10:38:16architecture. So there's there's also
  14870. 10:38:17this trade-off of like more higher bit
  14871. 10:38:20architectures mean you can be more
  14872. 10:38:22expressive and you have more data
  14873. 10:38:24available or more memory available to
  14874. 10:38:26work with but it also comes at the cost
  14875. 10:38:28at like an increased cost of complexity.
  14876. 10:38:30Uh which pairs poorly with the fact that
  14877. 10:38:32just phones are inherently constrained.
  14878. 10:38:35Um but what is ARMv6? Uh and that's an
  14879. 10:38:40interesting question. Um let's Google
  14880. 10:38:42it.
  14881. 10:38:44So ARM
  14882. 10:38:47Let's read this together. ARM is a
  14883. 10:38:50family of risk instruction set
  14884. 10:38:52architectures for computer processors.
  14885. 10:38:54Okay. Well, so ARM is not a single
  14886. 10:38:57instruction set architecture. It's a
  14887. 10:38:59family of instruction set architectures.
  14888. 10:39:01But what does this risk mean? Risk
  14889. 10:39:04stands for reduced instruction set
  14890. 10:39:06computer. And if we read the definition
  14891. 10:39:08here, it's an architecture designed to
  14892. 10:39:10simplify the individual instructions
  14893. 10:39:12given to the computer to accomplish
  14894. 10:39:14tasks.
  14895. 10:39:16Um and what this means is that a reduced
  14896. 10:39:18instruction set computer might require
  14897. 10:39:20more machine code to accomplish a task
  14898. 10:39:22because the individual instructions
  14899. 10:39:24perform simpler operations. So risk
  14900. 10:39:27generally is something that you see a
  14901. 10:39:28lot of uh phones or just resource
  14902. 10:39:32constrained devices such as like
  14903. 10:39:33embedded systems use uh because they're
  14904. 10:39:36they're generally more compact uh but
  14905. 10:39:39that also means they're less expressive
  14906. 10:39:40and less powerful compared to to
  14907. 10:39:42non-risk architectures. Um, but
  14908. 10:39:48due to their low cost, let's jump back
  14909. 10:39:49to the ARM Wikipedia. Due to their low
  14910. 10:39:52cost, low power consumption, and low
  14911. 10:39:53heat generation, ARM processors are
  14912. 10:39:56useful for light portable batterypowered
  14913. 10:39:58devices including smartphones, laptops,
  14914. 10:40:01and tablet computers as well as embedded
  14915. 10:40:04systems. So, ARM is very intentional.
  14916. 10:40:07the architectures, the instruction set
  14917. 10:40:09architectures that they introduced are
  14918. 10:40:11specifically meant for resource
  14919. 10:40:13constraint environments and they
  14920. 10:40:15explicitly call out smartphones as being
  14921. 10:40:17one such relevant use cases. Um so if
  14922. 10:40:21ARM is a family of instruction set
  14923. 10:40:23architectures,
  14924. 10:40:24let's see how many there actually are.
  14925. 10:40:26If I scroll down here, you can see uh
  14926. 10:40:29things obviously or things maybe you
  14927. 10:40:30would have guessed started with ARM v1
  14928. 10:40:32and then we went to ARMv2, V3, V4 and it
  14929. 10:40:36keeps going and these these keep coming
  14930. 10:40:38out. I guess the uh I guess the latest
  14931. 10:40:41is ARM ARMv9. Um but these
  14932. 10:40:45architectures, these instruction set
  14933. 10:40:47architectures are all technically
  14934. 10:40:49different uh but are somewhat related to
  14935. 10:40:52each other and they're usually
  14936. 10:40:53compatible. And the original iPhone uses
  14937. 10:40:57an ARM v6 architecture. And this is the
  14938. 10:41:00actual instruction set uh that the
  14939. 10:41:03iPhone uses. So, this original iPhone, I
  14940. 10:41:06guess just any of them, um, if you're
  14941. 10:41:09running any application or doing
  14942. 10:41:11anything, the code for, let's say, like
  14943. 10:41:13if you were running Twitter on your
  14944. 10:41:16original iPhone first generation, um,
  14945. 10:41:20all the code that builds up the Twitter
  14946. 10:41:22app is just a combination of ARM v6
  14947. 10:41:25instructions and
  14948. 10:41:28as is the case for everything that your
  14949. 10:41:30phone does. So all the code that
  14950. 10:41:32represents the ability for you to send
  14951. 10:41:34texts or make phone calls or uh browse
  14952. 10:41:37the web all boils down to ARM v6 uh
  14953. 10:41:40instructions. So you might think like
  14954. 10:41:43hey like this must mean that like ARM v6
  14955. 10:41:47must have a ton of instructions and you
  14956. 10:41:49know maybe there's an instruction to uh
  14957. 10:41:52send an internet request out or an
  14958. 10:41:54instruction to make a phone call. Uh but
  14959. 10:41:57it turns out when we actually look at
  14960. 10:41:58the architecture uh we don't see any of
  14961. 10:42:00that. So what I'd like to do is actually
  14962. 10:42:03take a stab and go ahead and look at the
  14963. 10:42:06architecture. And I also want to keep
  14964. 10:42:09things uh not too abstract. I guess
  14965. 10:42:13everything we've done so far has been
  14966. 10:42:14pretty abstract. I've showed you the
  14967. 10:42:16computer we built, but we weren't able
  14968. 10:42:17to verify that in the video itself
  14969. 10:42:19because I don't have access to that old
  14970. 10:42:20computer. Um, so what I do have is
  14971. 10:42:25something called a Raspberry Pi Pico.
  14972. 10:42:29And it is a small little microprocessor.
  14973. 10:42:33It's like this. It's about the size of
  14974. 10:42:35my pinky. Um, and something interesting
  14975. 10:42:39about it is that its CPU also runs uh
  14976. 10:42:43the same instruction set as the original
  14977. 10:42:45iPhone. So this this also runs ARM v6.
  14978. 10:42:50So, what I'd like to do is let's go
  14979. 10:42:52ahead and take a look at the uh ARMv6
  14980. 10:42:56instruction set. It gets very
  14981. 10:42:58complicated. Let's just take a poke at a
  14982. 10:43:00high level and then let's actually write
  14983. 10:43:02some code for ARMv6 and run it on this
  14984. 10:43:05processor and and see if we can actually
  14985. 10:43:08get some code to execute that does
  14986. 10:43:10something that we can see with our own
  14987. 10:43:11eyes. gone ahead and found the technical
  14988. 10:43:15reference manual, which is pretty much
  14989. 10:43:17just a guide for the processor that this
  14990. 10:43:20Raspberry Pi Pico has. And um just you
  14991. 10:43:24can see how old it is. This was
  14992. 10:43:26published in 2009, had two
  14993. 10:43:28modifications, and hasn't really been
  14994. 10:43:30updated since because we're on to much
  14995. 10:43:32more modern processors, but this is
  14996. 10:43:35still a great example. So, we're not
  14997. 10:43:37going to go too deep into this, but I do
  14998. 10:43:39want to poke at the instruction set. Um
  14999. 10:43:41let's start with the introduction about
  15000. 10:43:43the processor.
  15001. 10:43:45The Cortex M0 processor is a very low
  15002. 10:43:48gate count highly energyefficient
  15003. 10:43:50processor that is intended for
  15004. 10:43:52microcontroller and deeply embedded
  15005. 10:43:54applications that require an area
  15006. 10:43:57optimized processor. So very low gate
  15007. 10:44:00count. Gate count is literally referring
  15008. 10:44:02to the logic gates that we use to build
  15009. 10:44:04our computers. Generally speaking, the
  15010. 10:44:07more logic gates a computer has, the
  15011. 10:44:09more complexity and more battery
  15012. 10:44:11consumption uh you can expect it to
  15013. 10:44:13have. So having a low gate count is
  15014. 10:44:16especially enticing for um these like
  15015. 10:44:19constrained environments and that goes
  15016. 10:44:21hand inhand with being highly energy
  15017. 10:44:23efficient. Um, it is designed to use as
  15018. 10:44:25little as little electricity as
  15019. 10:44:28possible, which makes perfect sense for
  15020. 10:44:30a phone. Uh, where, you know, especially
  15021. 10:44:32back in 2008 when this first generation
  15022. 10:44:34iPhone was released, uh, 2007, sorry.
  15023. 10:44:38Um, discontinued in 2008. Uh, you know,
  15024. 10:44:41you could imagine batteries, they're not
  15025. 10:44:43even great today. Um, so they're quite
  15026. 10:44:46quite worse back then. But, um, let's
  15027. 10:44:51jump back to the docs. Uh so let's look
  15028. 10:44:53at this and
  15029. 10:44:55see
  15030. 10:44:57the instruction set summary. So
  15031. 10:45:01let's read this. This processor
  15032. 10:45:03implements the ARMv6 thumb instruction
  15033. 10:45:05set. And ARMv6 is the architecture we
  15034. 10:45:09saw earlier. And the thumb means that uh
  15035. 10:45:13it's actually a even more compact. ARMv6
  15036. 10:45:16is already designed to be pretty
  15037. 10:45:18minimal. Thumb makes it even more
  15038. 10:45:20minimal by reducing a number of
  15039. 10:45:22instructions that are always 30 32-bit
  15040. 10:45:26on general ARMv6 architectures down into
  15041. 10:45:2916. So our computer had straight 8-bit
  15042. 10:45:33instructions. This computer has a
  15043. 10:45:35combination of 16-bit instructions and
  15044. 10:45:3732-bit instructions. And this is pretty
  15045. 10:45:39interesting because our computer was
  15046. 10:45:42designed to assume that everything we
  15047. 10:45:44worked with was always 8 bits. But
  15048. 10:45:46that's not strictly the case. Uh this
  15049. 10:45:48computer is a great counter example
  15050. 10:45:50here. Instructions aren't always the
  15051. 10:45:52same number of bits. They can actually
  15052. 10:45:53vary. You could imagine some
  15053. 10:45:55instructions that don't have any
  15054. 10:45:57arguments or have minimal arguments need
  15055. 10:45:59not use 32 full bits. Um and so that's
  15056. 10:46:03there's some use cases where we can get
  15057. 10:46:05away with 16 bits or just fewer than 32
  15058. 10:46:07bits. Um
  15059. 10:46:10and
  15060. 10:46:13if we scroll down, we can actually see
  15061. 10:46:15the instructions. So, I'll just scroll
  15062. 10:46:16all the way to the bottom. And you can
  15063. 10:46:19see there's actually not a ridiculous
  15064. 10:46:20number of instructions. I want to say
  15065. 10:46:22there's like something around like 50 if
  15066. 10:46:25I'm guessing here. And let's kind of
  15067. 10:46:28comb through and see. So,
  15068. 10:46:31ARM in general and I guess a lot of more
  15069. 10:46:34just instruction sets in general operate
  15070. 10:46:36with many more registers than our
  15071. 10:46:38computer. Our computer had an A register
  15072. 10:46:41and a B register, but really we only had
  15073. 10:46:44instructions to operate on the A
  15074. 10:46:45register. And a lot of the instructions
  15075. 10:46:47that touch the B register were just to
  15076. 10:46:49satisfy A register operations or things
  15077. 10:46:52we'd like to do with the A register.
  15078. 10:46:55Registers are really fast to access. As
  15079. 10:46:57you saw, they literally just existed.
  15080. 10:46:59you know, registers, you could literally
  15081. 10:47:00put a value on the bus immediately or
  15082. 10:47:02read a value in from the bus immediately
  15083. 10:47:04versus RAM requires you to kind of put
  15084. 10:47:06an address in the memory address
  15085. 10:47:07register and decode that, which is just
  15086. 10:47:10slower. So, RAM is still fast, don't get
  15087. 10:47:12me wrong, but registers are even faster.
  15088. 10:47:15So, for that reason, we generally like
  15089. 10:47:17to work with registers just because
  15090. 10:47:18they're really quick and easy to read
  15091. 10:47:20and write from and don't require that
  15092. 10:47:22much like clock that many clock cycles
  15093. 10:47:24to do to interact with. So ARM has a lot
  15094. 10:47:28more registers and they're general
  15095. 10:47:30purpose registers meaning you can do
  15096. 10:47:31whatever you want with them. Uh but it
  15097. 10:47:34just gives you more room for scratch
  15098. 10:47:35work. So when we built our Fibonacci
  15099. 10:47:37program we had a lot of intermediate
  15100. 10:47:39values like we like to store the
  15101. 10:47:41previous Fibonacci number and the the
  15102. 10:47:44second to last Fibonacci number. And it
  15103. 10:47:46would have been nice if we could have
  15104. 10:47:47held those in registers, but instead we
  15105. 10:47:49kind of had to keep on reserving spots
  15106. 10:47:51in memory and writing those values to
  15107. 10:47:53those addresses in memory when we wanted
  15108. 10:47:55to update them and then reading them
  15109. 10:47:57back into the A register when we needed
  15110. 10:47:58to operate with them. Um, you can
  15111. 10:48:01imagine if we just had more registers,
  15112. 10:48:02we could just denote, hey, like this
  15113. 10:48:04register will just for our program will
  15114. 10:48:06always just hold the the n minus one
  15115. 10:48:09number and this second other register
  15116. 10:48:10will hold the n minus n minus2 number.
  15117. 10:48:13Um so a lot of what the arguments you'll
  15118. 10:48:16see are actually operating on registers.
  15119. 10:48:20So
  15120. 10:48:22um if I look at this add instruction uh
  15121. 10:48:24it's saying
  15122. 10:48:26you can pretty much add the values of
  15123. 10:48:29two registers these two registers into
  15124. 10:48:32the and it'll store the result in this
  15125. 10:48:34third register. Um or you can kind of
  15126. 10:48:38see that this move instruction. It'll
  15127. 10:48:40move the value from one reg from this
  15128. 10:48:43register into this register.
  15129. 10:48:46And
  15130. 10:48:48that's kind of a bit of a different
  15131. 10:48:49mental model. In our computer, we kind
  15132. 10:48:51of only saw for the most part
  15133. 10:48:52instructions being addresses and RAM.
  15134. 10:48:55But here we're seeing that an argument
  15135. 10:48:58can actually be just another register
  15136. 10:49:00itself, like the the name of another
  15137. 10:49:02register. Um, and then you can also see
  15138. 10:49:05we still have some things that are
  15139. 10:49:06similar. So like we had a load immediate
  15140. 10:49:08that would load a immediate value into
  15141. 10:49:11our A register where the value was
  15142. 10:49:13encoded in the instruction itself. We
  15143. 10:49:15still have that something pretty similar
  15144. 10:49:17here. So you can see like this IMM just
  15145. 10:49:19means an immediate and you can see that
  15146. 10:49:21there's a register that you could want
  15147. 10:49:23to load that immediate value into. Um,
  15148. 10:49:26same same with some of our ads. So you
  15149. 10:49:29could say, you know, add the value
  15150. 10:49:32that's in a register, take that value,
  15151. 10:49:34add it to some add some immediate value,
  15152. 10:49:37constant value to it, and then store it
  15153. 10:49:38in a destination register. Um, so you
  15154. 10:49:42know, we can kind of see like there's
  15155. 10:49:43move operations to move values from
  15156. 10:49:46registers to other registers. You can
  15157. 10:49:48see there's uh this one's interesting.
  15158. 10:49:50Move any to PC. So the pro PC is per
  15159. 10:49:53program counter as we saw earlier. That
  15160. 10:49:56still exists here. We still have this
  15161. 10:49:57mental model of the program counter
  15162. 10:49:59tells us an address in memory and the
  15163. 10:50:02computer will interpret that address and
  15164. 10:50:04the value at that address in memory is
  15165. 10:50:06an instruction and try to execute it. Um
  15166. 10:50:08so we have operations for moving data
  15167. 10:50:11between registers. We have operations
  15168. 10:50:14for doing addition. Similarly we have
  15169. 10:50:17operations for doing subtraction. So you
  15170. 10:50:19can kind of see like uh you can subtract
  15171. 10:50:23um take the value in one reg in one
  15172. 10:50:26register subtract it from another
  15173. 10:50:27register and store store the result back
  15174. 10:50:29into a destination register or you could
  15175. 10:50:32have a immediate variant where you take
  15176. 10:50:34the value in a register subtracted some
  15177. 10:50:37constant value from it and write the
  15178. 10:50:38result back into a different register or
  15179. 10:50:40maybe the same register. Um
  15180. 10:50:44you can see there's like a you can
  15181. 10:50:45negate values. There's also a
  15182. 10:50:47multiplication. We never built
  15183. 10:50:49multiplication in our computer, but the
  15184. 10:50:52way we built the ALOU uh for handling
  15185. 10:50:55addition and subtraction, you can
  15186. 10:50:57imagine there's another component on
  15187. 10:50:59more modern computers that can also do
  15188. 10:51:00multiplication. What's interesting is
  15189. 10:51:03this architecture does not actually have
  15190. 10:51:06any support for division. So if you
  15191. 10:51:08wanted to divide numbers, you'd actually
  15192. 10:51:09need to write a algorithm using a
  15193. 10:51:12combination of these instructions uh to
  15194. 10:51:15to to get to to have division. you have
  15195. 10:51:18to implement division yourself. Um you
  15196. 10:51:21also have instructions to compare the
  15197. 10:51:23values uh in two registers or compare
  15198. 10:51:26the value in a register to an immediate
  15199. 10:51:28and this CPU has the same concept of
  15200. 10:51:31flags. So the same way we had this jump
  15201. 10:51:34zero and jump carry instruction on our
  15202. 10:51:37computer that looked at the flags
  15203. 10:51:39register that was determined based on
  15204. 10:51:40the last ALU operation, we have
  15205. 10:51:43something similar where you know there's
  15206. 10:51:45flags and certain instructions can
  15207. 10:51:47update the flags.
  15208. 10:51:50We also have something called logical
  15209. 10:51:52instructions which are like the logic
  15210. 10:51:55gates that we designed earlier but
  15211. 10:51:57they're actually instructions. Um and we
  15212. 10:51:59didn't have this in our computer. So
  15213. 10:52:01let's take a look at this ands andds
  15214. 10:52:04instruction. What this does is it'll
  15215. 10:52:06take two registers as arguments and
  15216. 10:52:10those are 32-bit values in each register
  15217. 10:52:12because this is a 32-bit computer and
  15218. 10:52:15you'll get a final value that it'll
  15219. 10:52:17write into a different register. And
  15220. 10:52:19what the final value is is also a 32-bit
  15221. 10:52:22number where each bit position is the
  15222. 10:52:27bitwise and of the two bits in the same
  15223. 10:52:30positions of the operand argu uh
  15224. 10:52:32registers. So uh the rightmost bit
  15225. 10:52:36position in the output will be the
  15226. 10:52:38rightmost the rightmost bit position of
  15227. 10:52:41this register bitwise and with the
  15228. 10:52:44rightmost bit position of this register.
  15229. 10:52:47um and that's like the andgate and we
  15230. 10:52:49have analogous versions for the
  15231. 10:52:51exclusive or the bitwise or um or even
  15232. 10:52:55negation.
  15233. 10:52:57Then we also have a family of
  15234. 10:52:58instructions for shifting. Um
  15235. 10:53:02we haven't really talked about shifting
  15236. 10:53:04so I'll kind of leave it alone for now
  15237. 10:53:05but we'll actually see a program where
  15238. 10:53:07it's kind of useful. Um but pretty much
  15239. 10:53:09you could imagine I can explain that
  15240. 10:53:10like shifting uh if we had like some
  15241. 10:53:14binary value uh 0 1 1 0 if we left
  15242. 10:53:17shifted it once it would actually just
  15243. 10:53:20shift all the bits over one to the left
  15244. 10:53:22and then add a new zero. So we'd end up
  15245. 10:53:24with 1 1 0 0 that'd be a left shift by
  15246. 10:53:28one. Similarly if we started with a 0 1
  15247. 10:53:301 0 and right shifted we'd end up with
  15248. 10:53:33just 011 which is the equivalent of 0
  15249. 10:53:35011.
  15250. 10:53:37So that's interesting. Um, and then we
  15251. 10:53:40also have these load instructions which
  15252. 10:53:43will let us load values from RAM into
  15253. 10:53:45registers. And uh, what's interesting,
  15254. 10:53:49we'll see this again shortly. The
  15255. 10:53:52arguments could be literal addresses in
  15256. 10:53:55memory or you could actually have that a
  15257. 10:53:58value um, stored in a different register
  15258. 10:54:01is meant to be an argument. And so you
  15259. 10:54:04should take that argument uh take that
  15260. 10:54:07register who is the argument go look up
  15261. 10:54:09its value and treat that as an address
  15262. 10:54:11in RAM. Look that up and then write that
  15263. 10:54:13back into some other register. Um so
  15264. 10:54:15these are kind of our loads and you'll
  15265. 10:54:17also see there's like there's some
  15266. 10:54:19syntax differences here. So we have
  15267. 10:54:21three arguments for this one. And uh
  15268. 10:54:24pretty much what this is saying is it's
  15269. 10:54:25saying take the at treat the value
  15270. 10:54:28that's in register RN as an address and
  15271. 10:54:31then add some immediate value to it. So
  15272. 10:54:33maybe you want to like look two
  15273. 10:54:35addresses after that. So you could add
  15274. 10:54:37two and then it'll go look up that value
  15275. 10:54:39at Rn plus two in RAM. Uh read that and
  15276. 10:54:42write that into this register. Um then
  15277. 10:54:45we also have very similar variants for
  15278. 10:54:47storing storing values in registers to
  15279. 10:54:50RAM. Um we have a push and a pop which
  15280. 10:54:53we'll ignore for now. Uh and then we
  15281. 10:54:55also have branching. So unconditional
  15282. 10:54:58just means you will always branch. So
  15283. 10:55:01that's the equivalent of our just jump.
  15284. 10:55:03You will no matter what the state of the
  15285. 10:55:04computer is, you will jump to this other
  15286. 10:55:06address. Then we also have conditional
  15287. 10:55:08ones where you know you can imagine like
  15288. 10:55:10there's like branch if two values are
  15289. 10:55:12equal or branch if value is zero and
  15290. 10:55:14that's kind of how we implemented our
  15291. 10:55:16those are the equivalents of our jump
  15292. 10:55:18carry and jump zero instructions. Um,
  15293. 10:55:21and then we have a few more down here.
  15294. 10:55:24And these are kind of a little bit weird
  15295. 10:55:25like what is a supervisor call? What is
  15296. 10:55:28an interrupt? Uh, what is an event? What
  15297. 10:55:30is the concept of sending an event and
  15298. 10:55:32waiting for an event? What is the
  15299. 10:55:33concept of yielding? We have no op,
  15300. 10:55:35which we already saw previously, which
  15301. 10:55:37just does nothing. Um,
  15302. 10:55:41and yeah, we're actually going to ignore
  15303. 10:55:42these for now. Uh there's no way to not
  15304. 10:55:46look at this processor and see a ton of
  15305. 10:55:48new stuff that we haven't already seen.
  15306. 10:55:50But that's um this is this is more
  15307. 10:55:52advanced and we're not going to probably
  15308. 10:55:54get into it uh for the scope of what
  15309. 10:55:56we're doing right now. But it is
  15310. 10:55:57possible that a separate video will
  15311. 10:55:59actually cover this specifically. So
  15312. 10:56:02this is kind of an overview of the
  15313. 10:56:03instruction set. You might be scratching
  15314. 10:56:06your head where these instructions,
  15315. 10:56:08yeah, I guess semantically they're a
  15316. 10:56:10little bit different than what we built
  15317. 10:56:11for our computer, but they're not that
  15318. 10:56:13different. Uh so like where is the
  15319. 10:56:15instruction to you know write something
  15320. 10:56:17to the display of the phone or something
  15321. 10:56:19like that? Uh and it turns out that
  15322. 10:56:21there's just not an instruction for
  15323. 10:56:22that. So you're probably even more
  15324. 10:56:25confused now wondering well how does my
  15325. 10:56:28the processor in my iPhone actually draw
  15326. 10:56:30to the screen of the iPhone? And we're
  15327. 10:56:33going to turn out uh we're going to do
  15328. 10:56:35some keep poking this a little further.
  15329. 10:56:37Turns out these instructions are
  15330. 10:56:39actually capable of doing everything
  15331. 10:56:41that we actually need to draw on the
  15332. 10:56:43display or know when the user touches
  15333. 10:56:46the screen at a particular location. And
  15334. 10:56:49I'm going to jump back to my iPad and
  15335. 10:56:51we're going to explain two concepts and
  15336. 10:56:54then we'll jump back to the computer and
  15337. 10:56:55those two concepts will actually start
  15338. 10:56:57bridging the gap of how our processor
  15339. 10:57:00can do things that aren't just mappy.
  15340. 10:57:02Like everything we see right here is
  15341. 10:57:03just kind of some variation of math or
  15342. 10:57:06moving data. So we can implement very
  15343. 10:57:08pure algorithms, but we're not seeing
  15344. 10:57:10how we translate that to very practical
  15345. 10:57:12things that we'd want a phone to do,
  15346. 10:57:13like drawing a shape or, you know,
  15347. 10:57:16showing some notification in a visual
  15348. 10:57:17manner. Um, so let's go over the iPad.
  15349. 10:57:20Uh, it's a little I don't mean to be
  15350. 10:57:22very jumpy, but there are two different
  15351. 10:57:23concepts that I like to explain. Um, and
  15352. 10:57:25then we'll be able to jump back and
  15353. 10:57:27actually write some code for our
  15354. 10:57:29Raspberry Pi Pico.
  15355. 10:57:33Okay, so I'm back on my iPad and I said
  15356. 10:57:35there's two concepts I want to teach you
  15357. 10:57:36about. Number one is called Hex. So, uh,
  15358. 10:57:41the instruction set architecture where
  15359. 10:57:43we've been working with that we
  15360. 10:57:44designed, uh, right up here was all 8
  15361. 10:57:47bits. You know, our bus was 8bit, our
  15362. 10:57:48RAM was all 8 bits, all of our registers
  15363. 10:57:50were 8 bits. Uh, but now we're looking
  15364. 10:57:52at a 32-bit system. And
  15365. 10:57:56binary totally works for 8 bits for
  15366. 10:57:59saying, "Hey, our value is 1 0 1 1 0 0
  15367. 10:58:03just fictitious number." This works for
  15368. 10:58:068 bits. It's fine. It's a little tedious
  15369. 10:58:08to write out all eight bits, but oh
  15370. 10:58:09well, we have to do it. When it's 32
  15371. 10:58:12bits, uh, that's a big pain to represent
  15372. 10:58:1532 digits for every number. And we'll
  15373. 10:58:18see that not only is this pretty ver
  15374. 10:58:20verbose, it's just not as readable. um
  15375. 10:58:22the more ones and zeros you have chained
  15376. 10:58:24together the the less the interpretate
  15377. 10:58:25the interpret interpretability becomes.
  15378. 10:58:28So instead of writing everything out in
  15379. 10:58:30binary, what we're going to see as we
  15380. 10:58:32look into this further and look at
  15381. 10:58:34actual uh code for this architecture is
  15382. 10:58:38something called hex. Um hex is base 16.
  15383. 10:58:44And recall from earlier uh we speak in
  15384. 10:58:47base 10.
  15385. 10:58:49And what this means is that there's 10
  15386. 10:58:52different values that can go into every
  15387. 10:58:54digit position. And those are zero, one,
  15388. 10:58:56two, three, four, five, six, seven,
  15389. 10:59:00eight, and nine. Then we learned about
  15390. 10:59:03base 2, which is binary. And two
  15391. 10:59:07different values can go into this slot,
  15392. 10:59:09zero and one. So I feel like if I told
  15393. 10:59:12you about base 3, base 4, or anything up
  15394. 10:59:15to base 9, you'd be able to reason about
  15395. 10:59:17how to count and it wouldn't be that
  15396. 10:59:19complicated. But when I'm saying base
  15397. 10:59:2216,
  15398. 10:59:25this has some implication that there's
  15399. 10:59:2716 different values that could go in any
  15400. 10:59:29digit position, but there's only 10
  15401. 10:59:33different digits that we know. Like
  15402. 10:59:35every number we know is made up of of
  15403. 10:59:37the the digits 0 through nine. So how
  15404. 10:59:40can we represent 11 different digits,
  15405. 10:59:43let alone 16 [snorts]
  15406. 10:59:46uh if we don't have that many digits?
  15407. 10:59:48Um, and it turns out we start using
  15408. 10:59:50numbers. So, let's count in base 16 and
  15409. 10:59:53see what it turns out, sorry, that we
  15410. 10:59:54start using letters. So, let's count in
  15411. 10:59:57base 16 and see what this looks like.
  15412. 10:59:59So, we have 0 1 2 3 4 5 and everything
  15413. 11:00:06up through nine is going to be the same.
  15414. 11:00:10We'd like to write the number 10 here,
  15415. 11:00:11but that is not onedigit position.
  15416. 11:00:13That's twodigit positions. But we have
  15417. 11:00:15to we're looking at single digit
  15418. 11:00:17positions. So if we run out of every
  15419. 11:00:19number, the next logical thing that we
  15420. 11:00:22can use is other symbols. And letters
  15421. 11:00:25are an easy symbol. We could start
  15422. 11:00:26drawing shapes, but let's just stick
  15423. 11:00:28with letters. So we're going to say A.
  15424. 11:00:33And we sort of implicitly know that A
  15425. 11:00:36maps to 10. And then B maps to 11,
  15426. 11:00:41C maps to 12,
  15427. 11:00:44D can map to 13,
  15428. 11:00:48E can map to 14,
  15429. 11:00:51and F can map to 15. And if we say that
  15430. 11:00:55these are this is the set of digits in B
  15431. 11:01:0116, that is 16 different digits. Um, and
  15432. 11:01:07it might be weird to reason about
  15433. 11:01:09numbers and letters mixed
  15434. 11:01:10interchangeably, but it pretty much
  15435. 11:01:12means let's just look at an example of a
  15436. 11:01:14a hex number. Let's say we have um
  15437. 11:01:19a e this hex number and I'll also say
  15438. 11:01:24for binary for for base 10 we just write
  15439. 11:01:26out our numbers kind of we say like 762
  15440. 11:01:29the example number for binary uh we kind
  15441. 11:01:32of prefix it sometimes with this 0B and
  15442. 11:01:35then we say all of our binary digits.
  15443. 11:01:37You'll see that for hex we prefix with
  15444. 11:01:400x.
  15445. 11:01:42So if you see zero x it means you have a
  15446. 11:01:44hex number. So let's say we have ae.
  15447. 11:01:47Well, uh we just need to figure out what
  15448. 11:01:49the contribution of each digit position
  15449. 11:01:52is to our overall sum. So if we were
  15450. 11:01:55looking in base 10, we had the number
  15451. 11:01:5756. Recall that this is 6 * 10 ^ of 0
  15452. 11:02:04and then 5 * 10 ^ of 1. Um so our
  15453. 11:02:09analogous equivalent here is this is a *
  15454. 11:02:1516 to the^ of 1 plus e * 16 ^ of 0. And
  15455. 11:02:23we know a is 10. So that's 10 * 16. And
  15456. 11:02:27then we know e is 14. And 16 to the 0 is
  15457. 11:02:30just one. So we have 160
  15458. 11:02:35and 14. when we add those together, it's
  15459. 11:02:37just 174.
  15460. 11:02:40So, this is not fundamentally too new
  15461. 11:02:42from anything we've already seen, but I
  15462. 11:02:44thought it warranted an example to show.
  15463. 11:02:46Um, and yeah, pretty much that means
  15464. 11:02:50that for example, if I just say 0x 9,
  15465. 11:02:54that's still 9. But if I say 0xd, that's
  15466. 11:02:58equivalent to the value of 13 in base
  15467. 11:03:0010. Base 10 being what we reason about
  15468. 11:03:02as humans. So we can see that with a uh
  15469. 11:03:06each digit in base 16
  15470. 11:03:10holds one of 16 different values. Which
  15471. 11:03:13means if we think about this um we know
  15472. 11:03:15that 2 to the 4 is equal to the^ of 16.
  15473. 11:03:19So one hex digit
  15474. 11:03:23equals four bits.
  15475. 11:03:27you need four bits to represent discrete
  15476. 11:03:31values for each of these uh digits in
  15477. 11:03:34the vocab. Um
  15478. 11:03:36but this makes you makes things better
  15479. 11:03:38because it means we have a 1 to4
  15480. 11:03:40mapping. So when our computer works with
  15481. 11:03:4232 bits,
  15482. 11:03:44it's actually just eight hex digits.
  15483. 11:03:47So every address in our computer is
  15484. 11:03:54eight hex digits, which plays pretty
  15485. 11:03:57well because that's the same number of
  15486. 11:03:58digits that we had to to use to reason
  15487. 11:04:00about our um our SAP computer. So that's
  15488. 11:04:05concept number one. Concept number two
  15489. 11:04:09is called MMIO.
  15490. 11:04:14And I would actually say this is the
  15491. 11:04:17crux of how the computer interacts with
  15492. 11:04:20components that are not itself. So let's
  15493. 11:04:22say that our computer um and this this
  15494. 11:04:25Raspberry Pi Pico that I'm holding in my
  15495. 11:04:27hand, which you can't see cuz I'm screen
  15496. 11:04:29sharing has um an LED on it. So there's
  15497. 11:04:34this CPU and we know everything about
  15498. 11:04:36the CPU and then there's this LED. LED
  15499. 11:04:39just stands for light emmitting diode.
  15500. 11:04:41It's literally just a light. Um,
  15501. 11:04:46and the question is, how could we
  15502. 11:04:49possibly have our processor with the
  15503. 11:04:52instructions we know turn that LED on
  15504. 11:04:55and off? And so, let's break down what
  15505. 11:04:59mmio stands for.
  15506. 11:05:04The first M stands for memory,
  15507. 11:05:06then mapped,
  15508. 11:05:09input, output.
  15509. 11:05:13So, mm IO stands for memory mapped input
  15510. 11:05:16output. What does this possibly mean?
  15511. 11:05:18That doesn't put us any closer. Um,
  15512. 11:05:21well, input output is kind of this arrow
  15513. 11:05:23here. In our case, we're just concerned
  15514. 11:05:25about output. You know, we just like
  15515. 11:05:27we'd like our CPU to be able to uh turn
  15516. 11:05:30the LED on and off, but who knows? Maybe
  15517. 11:05:32this is maybe we'd also like to be able
  15518. 11:05:34to read if the LED is on or not. Uh, so
  15519. 11:05:37there could be input. Um, and so I guess
  15520. 11:05:40the crux of this is the memory mouth
  15521. 11:05:42part. And with the instructions we saw
  15522. 11:05:46in the computer we built and in the ARM
  15523. 11:05:48V6 manual that we just combed through,
  15524. 11:05:51there's no way to possibly turn on this
  15525. 11:05:53LED or something that's just totally
  15526. 11:05:55external to the processor itself. Um,
  15527. 11:05:59and it turns out we use something called
  15528. 11:06:01a memory mapped IO which is abstracts
  15529. 11:06:03parts of memory to interact with third
  15530. 11:06:06party components. So, we're just going
  15531. 11:06:08to pick an a random address.
  15532. 11:06:11Let's just say 0x.
  15533. 11:06:19And I'm just inventing some 32-bit hex
  15534. 11:06:21address. And pretty much what happens
  15535. 11:06:24is, let's say you write a one to this
  15536. 11:06:29value in memory, then it'll turn on the
  15537. 11:06:32light.
  15538. 11:06:35And if you write a zero to this value in
  15539. 11:06:37memory, it'll turn off the light.
  15540. 11:06:42And
  15541. 11:06:44how could that be possible? Uh it turns
  15542. 11:06:46out the CPU is just designed these these
  15543. 11:06:49addresses are just determined
  15544. 11:06:51arbitrarily. There's usually some
  15545. 11:06:53consistency across like processors in
  15546. 11:06:55the world. But um the CPU will interpret
  15547. 11:06:59any attempt to write. So any storing of
  15548. 11:07:02register val at uh any storing of a
  15549. 11:07:05value to that address um instead of
  15550. 11:07:08actually storing it to that address in
  15551. 11:07:10RAM it'll have a comp connection to this
  15552. 11:07:14external component and it'll use that as
  15553. 11:07:16the signal to go and turn on that
  15554. 11:07:18component like give it power or not. So
  15555. 11:07:21this is really powerful and I think very
  15556. 11:07:23interesting um because it does mean that
  15557. 11:07:26the the implementation of the CPU has to
  15558. 11:07:28be updated but you can kind of guess how
  15559. 11:07:30this would be done. You saw how
  15560. 11:07:32instructions were decoded. So you can
  15561. 11:07:34pretty much imagine that any storing
  15562. 11:07:36instruction has some extra complexity
  15563. 11:07:39now where it'll check uh you know if you
  15564. 11:07:44do some sort of store
  15565. 11:07:47then what happens is it goes one of two
  15566. 11:07:49ways. you have to check the address and
  15567. 11:07:52it'll either write to RAM
  15568. 11:07:57or it'll you know uh connect to some
  15569. 11:08:01peripheral and we'll say operate on a
  15570. 11:08:06peripheral
  15571. 11:08:08and a peripheral here just refers to any
  15572. 11:08:12component of the computer that is
  15573. 11:08:14external but still part of the computer
  15574. 11:08:16as a whole external to the CPU um
  15575. 11:08:19something that can be interacted with
  15576. 11:08:21via the CPU. So
  15577. 11:08:24in this hypothetical example, we'd say
  15578. 11:08:27that if you write to this FFF AAA
  15579. 11:08:29address, uh a one will turn will cause
  15580. 11:08:32nothing to actually be written into that
  15581. 11:08:34address in RAM. Uh but it'll actually
  15582. 11:08:36cause the light to be turned on. Um or a
  15583. 11:08:38zero will cause it to be turned off. And
  15584. 11:08:40then we can just pick a different
  15585. 11:08:41address. Uh let's just say like fff FF
  15586. 11:08:45uh O.
  15587. 11:08:46And we'll say that if you try read any
  15588. 11:08:50read
  15589. 11:08:51will give
  15590. 11:08:54one if light on
  15591. 11:08:57zero if light off.
  15592. 11:09:01So if we wanted in the CPU in our
  15593. 11:09:04program code to know if the light was
  15594. 11:09:06on, we would literally just load we
  15595. 11:09:08perform a load of this specific address
  15596. 11:09:11in memory to a register, any register of
  15597. 11:09:14our choosing. And when the computer sees
  15598. 11:09:16that we are trying to access this
  15599. 11:09:19specific address, it won't actually go
  15600. 11:09:21look it up in RAM, it'll talk with the
  15601. 11:09:23light component, figure out if it's on
  15602. 11:09:25or not, and then put the corresponding
  15603. 11:09:27value into that register. So this is
  15604. 11:09:30super powerful because it gives the
  15605. 11:09:32illusion that we are just working with
  15606. 11:09:35memory um but we're actually now using
  15607. 11:09:38memory to interact with things that are
  15608. 11:09:40not just RAM. We've kind of violated
  15609. 11:09:42this assumption that we had previously
  15610. 11:09:44where anytime we write or write uh a
  15611. 11:09:47value to or from RAM, it would actually
  15612. 11:09:50do some operation on memory. But this is
  15613. 11:09:52not the case. In actuality, uh only
  15614. 11:09:54subsets of addresses uh will truly
  15615. 11:09:57update RAM or read from RAM and then
  15616. 11:10:00plenty of others will be used to
  15617. 11:10:02interface with external parts of the
  15618. 11:10:04computer. So if we explain um how this
  15619. 11:10:09might look so we know you know this is
  15620. 11:10:11just one example of one specific address
  15621. 11:10:14maybe let's say uh a display
  15622. 11:10:18a display on a phone um can maybe be
  15623. 11:10:21broken up into a grid of pixels.
  15624. 11:10:27I'm I'm just ignore my dimensions here.
  15625. 11:10:29Let's also just for simplicity assume
  15626. 11:10:31that um each pixel it's a black and
  15627. 11:10:34white display display. So each pixel
  15628. 11:10:39is either on
  15629. 11:10:43or off. Then what we might do is say uh
  15630. 11:10:47start with a specific address in memory.
  15631. 11:10:49Let's say just hex um
  15632. 11:10:57just this is maybe our starting address
  15633. 11:10:59and then
  15634. 11:11:02our code would say if you want to turn
  15635. 11:11:03on this pixel write on to this address
  15636. 11:11:06and then the next address uh in memory
  15637. 11:11:09would correspond to the next pixel. So
  15638. 11:11:11that'd be o o
  15639. 11:11:15a1 and then this would be a2 and a3 and
  15640. 11:11:20a4. So there'd be a like whole
  15641. 11:11:23continuous chunk if there's like let's
  15642. 11:11:25say there's 50 different pixels here.
  15643. 11:11:28There could be 50 regions in memory um
  15644. 11:11:32consecutive starting at this base
  15645. 11:11:33address of hex a z. and your code could
  15646. 11:11:38draw an image or draw a line or anything
  15647. 11:11:42by figuring out which of these pixels to
  15648. 11:11:44turn on. So if it uh were to to write
  15649. 11:11:48ones into the first five addresses,
  15650. 11:11:50you'd see like a line appear at the top
  15651. 11:11:52of your screen. And so that's how our
  15652. 11:11:55our program code can actually turn on
  15653. 11:11:59the display. Uh so this is kind of how
  15654. 11:12:01your phone display would work. And then
  15655. 11:12:04conversely there could be there's
  15656. 11:12:06several different implementation ways to
  15657. 11:12:08actually figure out when a user touches
  15658. 11:12:09on the screen. Um but perhaps one simple
  15659. 11:12:14way is just the computer can decide some
  15660. 11:12:16address. Let's say ff
  15661. 11:12:20a621
  15662. 11:12:22just making up random addresses. and uh
  15663. 11:12:25whenever your program code reads this
  15664. 11:12:28address, it'll have uh maybe encoded in
  15665. 11:12:32there the coordinates of the last place
  15666. 11:12:34the user touched the screen. So you
  15667. 11:12:37could constantly be reading from this
  15668. 11:12:38address and see whenever it changes to
  15669. 11:12:40detect if the user just touched the
  15670. 11:12:42screen somewhere. Um so this is what
  15671. 11:12:45memory mapped IO does. IO stands for
  15672. 11:12:47input output. It means pretty much
  15673. 11:12:50communicating with the external world or
  15674. 11:12:52other components that are part of this
  15675. 11:12:54computer but not physically part of the
  15676. 11:12:56CPU. And this mm memory mapped is means
  15677. 11:13:00that it it appears to the computer to
  15678. 11:13:02all be under the guise of memory read
  15679. 11:13:03and writes. Uh but in actuality your
  15680. 11:13:06processor is redirecting a subset of
  15681. 11:13:08those around uh to accomplish this
  15682. 11:13:10pretty neat behavior. So this is super
  15683. 11:13:13cool. Um and unlocks the door to do a
  15684. 11:13:16lot of things. You could imagine like um
  15685. 11:13:19the CPU, you know, can now interact with
  15686. 11:13:23not only a screen,
  15687. 11:13:25but it might also have a a some sort of
  15688. 11:13:29uh separate processor um that has
  15689. 11:13:33internet access and then you know the
  15690. 11:13:35CPU can now make internet requests. it
  15691. 11:13:37could write uh the address of a a URL in
  15692. 11:13:42some register and then this it would get
  15693. 11:13:44routed to the internet chip and then
  15694. 11:13:45maybe the internet chip could return
  15695. 11:13:46some data about like what's at that
  15696. 11:13:48internet address. So it's pretty
  15697. 11:13:50powerful. Um let's actually write a
  15698. 11:13:53program on the Raspberry Pi I have that
  15699. 11:13:56will turn on an LED using memory mapped
  15700. 11:13:59IO. So here's the microprocessor I've
  15701. 11:14:02been talking about for the past 20
  15702. 11:14:04minutes. And you'll have to bear with me
  15703. 11:14:06because my computer webcam quality is
  15704. 11:14:08not that great. But you can see this is
  15705. 11:14:10the actual CPU. So it's analogous to
  15706. 11:14:13what we just spent the past several
  15707. 11:14:14hours building. And one of the other
  15708. 11:14:17things to note is all these pins coming
  15709. 11:14:19off the bottom. And uh a natural first
  15710. 11:14:21question is like what are these? So
  15711. 11:14:24again, the the image quality is just not
  15712. 11:14:26great here. If I put this close to the
  15713. 11:14:28camera, you'll see that they're all
  15714. 11:14:30labeled. And many of them have the
  15715. 11:14:34prefix GP and then some number following
  15716. 11:14:37it. And what this actually is, these are
  15717. 11:14:40GPIO pins, which stands for general
  15718. 11:14:43purpose input output. So each of these
  15719. 11:14:46pins programmatically via memory mapped
  15720. 11:14:49IO um can either be used to read in data
  15721. 11:14:53from that pin either a zero or a one or
  15722. 11:14:55put out a value onto that pin a zero or
  15723. 11:14:58one. And this is using real electricity.
  15724. 11:15:01So what I could do is take a specific
  15725. 11:15:03pin, figure out what address in memory
  15726. 11:15:06corresponds to controlling that pin, and
  15727. 11:15:09then write a one to it. And then if I
  15728. 11:15:11have like a physical wire right here, I
  15729. 11:15:13could literally connect that pin
  15730. 11:15:17and then plug this into a separate
  15731. 11:15:18device. So maybe if I had like a light,
  15732. 11:15:20I could connect it to to this other the
  15733. 11:15:22other end of this wire right here and it
  15734. 11:15:23would turn it on. Um, and there's more
  15735. 11:15:26interesting things that would be if you
  15736. 11:15:27wanted to use a pin for outputting.
  15737. 11:15:29Maybe if we had a temperature sensor,
  15738. 11:15:31um, or some sort of other device, uh,
  15739. 11:15:34like a button or something that when
  15740. 11:15:35pressed, we could read in that signal.
  15741. 11:15:38Um, it all works with memory mapped IO.
  15742. 11:15:40So, in your code, even though you know
  15743. 11:15:42that, hey, this pin's connected to a
  15744. 11:15:44light or a button or a temperature
  15745. 11:15:46sensor, uh, it appears as though you're
  15746. 11:15:48just reading an address in memory. And
  15747. 11:15:51one of the more interesting things, um,
  15748. 11:15:53that I just happen to have with me as
  15749. 11:15:55well is an actual display
  15750. 11:15:58that, uh, has a bunch of holes for
  15751. 11:16:01various GPIO pins to connect to. And
  15752. 11:16:04there's some protocol which we won't get
  15753. 11:16:06into here, but it lets you uh via memory
  15754. 11:16:10address manipulation in the Raspberry Pi
  15755. 11:16:13write contents to a screen. So I have
  15756. 11:16:15some text drawn on the screen here. Um
  15757. 11:16:17I'm building a subway tracker, but
  15758. 11:16:19that's an independent project. And the
  15759. 11:16:21code all exists on this Raspberry Pi and
  15760. 11:16:24it'll go to the internet, figure out
  15761. 11:16:26when the subways are coming from my
  15762. 11:16:27station. Um, and then by writing certain
  15763. 11:16:31contents to various uh addresses in
  15764. 11:16:33memory, it can actually uh cause uh text
  15765. 11:16:36to appear on the display of the screen.
  15766. 11:16:38So that's pretty neat. Um, so we have
  15767. 11:16:40all these GPIO general purpose input
  15768. 11:16:43output pins. And if I actually flip this
  15769. 11:16:45around, there's one other thing that's
  15770. 11:16:47pretty interesting and it's this this
  15771. 11:16:49copper like thing up here. And above it,
  15772. 11:16:52it says LED, which stands for light
  15773. 11:16:54emmitting diode. And this is just a
  15774. 11:16:57light that we can use to turn on and off
  15775. 11:16:59in our code however we want. Um, it's
  15776. 11:17:02nice because we could, you know, write
  15777. 11:17:04to a wire and then have that wire
  15778. 11:17:06connect to a light. I just don't have
  15779. 11:17:07one. So, this computer, this
  15780. 11:17:09microprocessor, sorry, conveniently has
  15781. 11:17:12a light built in. So, what we're going
  15782. 11:17:14to do now is take a peek at figuring out
  15783. 11:17:16how we can get code to run on this um
  15784. 11:17:19that would turn on the light. And then
  15785. 11:17:21the last thing we need to bridge is that
  15786. 11:17:22there's this USB port right here. So
  15787. 11:17:25that's how I can connect it to my
  15788. 11:17:26computer and actually get code that I
  15789. 11:17:28have on my computer, machine code
  15790. 11:17:30binary, uh, onto the device. It's a
  15791. 11:17:32pretty intricate process in itself, and
  15792. 11:17:34we're not going to concern ourselves too
  15793. 11:17:35much with it here. Um, but it is worth
  15794. 11:17:37calling out like if I'm typing code on
  15795. 11:17:39my computer, how do I actually get it
  15796. 11:17:40onto this microprocessor itself? So, let
  15797. 11:17:42me go ahead and tab back to my computer.
  15798. 11:17:44So, back on my computer and I'm actually
  15799. 11:17:47going to start with the solution. And I
  15800. 11:17:49will claim that if you run this set of
  15801. 11:17:52instructions on your computer on this
  15802. 11:17:54processor that I'm holding in my hand,
  15803. 11:17:57it will boot up the computer and turn on
  15804. 11:17:59the light. Um, so let's take a look at
  15805. 11:18:01what this is. These are various
  15806. 11:18:03addresses.
  15807. 11:18:05Recall that the computer's 32-bit uh
  15808. 11:18:07addresses. And so these are actually hex
  15809. 11:18:10addresses because they're more compact
  15810. 11:18:12but equally as representative. So two,
  15811. 11:18:14three, four, five, six, seven, eight.
  15812. 11:18:18Eight hex characters in each address
  15813. 11:18:20where you know each hex character
  15814. 11:18:21represents four bits of data means 8 * 4
  15815. 11:18:24is 32. So these are equivalent to 32-bit
  15816. 11:18:27addresses. So that's consistent with our
  15817. 11:18:29expectations. And then what we have
  15818. 11:18:31right here is the literal value that is
  15819. 11:18:33stored at each of these addresses.
  15820. 11:18:36And it's also again in hex. Um but this
  15821. 11:18:39is a little uninterpretable because it's
  15822. 11:18:42we know the binary uh is not necessarily
  15823. 11:18:44intended to be interpreted as a number
  15824. 11:18:46but rather as some instruction that
  15825. 11:18:48encodes an operation with some possible
  15826. 11:18:50arguments. Um so there's semantic
  15827. 11:18:52meaning outside of the digit uh
  15828. 11:18:55numerical interpretation and that's
  15829. 11:18:57exactly what we have over here on the
  15830. 11:18:59the right column. We have uh a bunch of
  15831. 11:19:02uh instructions. So we haven't seen any
  15832. 11:19:04of these instructions because we only
  15833. 11:19:06skimmed through the uh instruction set
  15834. 11:19:08architecture but we'll give some
  15835. 11:19:09motivation as we walk through these. So
  15836. 11:19:11you can see like this is an instruction,
  15837. 11:19:13LDR is an instruction, bic's
  15838. 11:19:16instruction. You can see R1 uh and R2
  15839. 11:19:19and R3 and so on are all different
  15840. 11:19:22registers that we have accessible to us.
  15841. 11:19:24Some things look familiar like PC we
  15842. 11:19:26know is the program counter. Um and then
  15843. 11:19:29these are all instructions.
  15844. 11:19:31We can kind of see down here uh where it
  15845. 11:19:33says this word. We'll see this shortly,
  15846. 11:19:36but these are actually used to store
  15847. 11:19:38values. Um so the same way in our
  15848. 11:19:40Fibonacci program we stored values in
  15849. 11:19:43certain addresses in RAM, we do the same
  15850. 11:19:45thing here because sometimes an
  15851. 11:19:47instruction can't be uh with as many
  15852. 11:19:50bits as we have can't represent a full
  15853. 11:19:52op code and a 32-bit argument uh in the
  15854. 11:19:55constraint of a 32-bit instruction
  15855. 11:19:56itself. So we'll see similar tricks
  15856. 11:19:59where we store uh addresses in certain
  15857. 11:20:01addresses in certain part uh positions
  15858. 11:20:03in RAM or arguments that we can't encode
  15859. 11:20:06instructions themselves. Um but if I ran
  15860. 11:20:09this on the computer, it wouldn't be
  15861. 11:20:10that satisfying because we haven't
  15862. 11:20:11really explained what we've done. So
  15863. 11:20:15there's two questions. Um one is what is
  15864. 11:20:17this code doing? And two is if we
  15865. 11:20:20understand what the code is doing, how
  15866. 11:20:22did I know to write this code out? And
  15867. 11:20:25to answer those questions, I actually
  15868. 11:20:27turned to the uh documentation for this
  15869. 11:20:31processor. And at a brief glance, if I
  15870. 11:20:34look at the table of contents
  15871. 11:20:36and scroll all the way to the bottom, we
  15872. 11:20:38can see this is 640 pages. So this um
  15873. 11:20:43pretty small processor
  15874. 11:20:45somehow has a lot of complexity going on
  15875. 11:20:48uh to justify that much documentation.
  15876. 11:20:51And it turns out this is a pretty
  15877. 11:20:52featurerich chip. And we're only going
  15878. 11:20:54to touch this scratch the surface here.
  15879. 11:20:56Um, but this documentation has explains
  15880. 11:21:00everything we need to do to actually
  15881. 11:21:01turn on the LED we're interested in. So,
  15882. 11:21:04I'm not going to read everything in too
  15883. 11:21:05much granularity here, but what I will
  15884. 11:21:07do is jump around the various sections.
  15885. 11:21:09So, the very first thing we need to do,
  15886. 11:21:12um, if you read this is
  15887. 11:21:15come over to this chapter and let's read
  15888. 11:21:19this. The reset controller allows
  15889. 11:21:21software control of the resets to all
  15890. 11:21:23peripherals that are not critical to
  15891. 11:21:24boot the processor. Um, and this is a
  15892. 11:21:29lot of jargon. What this means is that
  15893. 11:21:31when we turn on the computer, uh, only a
  15894. 11:21:33subset of functionality is enabled and
  15895. 11:21:35that subset of functionality is exactly
  15896. 11:21:37what it needs to get the computer
  15897. 11:21:39booted. You can imagine that that LED is
  15898. 11:21:42not necessary for it to start up. So
  15899. 11:21:45most external things that aren't
  15900. 11:21:47strictly necessary for the computer to
  15901. 11:21:48get running are actually turned off by
  15902. 11:21:51default. So
  15903. 11:21:54there's a um particular address in
  15904. 11:21:57memory that we can write data to that
  15905. 11:21:59will let us turn things on. So the first
  15906. 11:22:02thing we need to do is tell that our
  15907. 11:22:03processor like hey we actually do want
  15908. 11:22:05to interact with these GPIO pins. Um,
  15909. 11:22:09and we can see that the documentation
  15910. 11:22:11says three registers are defined and
  15911. 11:22:12there's this reset one. This register
  15912. 11:22:15contains a bit per each peripheral that
  15913. 11:22:16can be reset. If the bit is set to one,
  15914. 11:22:18then the reset is asserted. If the bit
  15915. 11:22:20is clear, then the re the reset is
  15916. 11:22:22deasserted. So what this means is we
  15917. 11:22:24have a 32-bit register somewhere. Some
  15918. 11:22:26address in memory can be interpreted as
  15919. 11:22:28the address by which we can interface
  15920. 11:22:30with this register. And each bit
  15921. 11:22:32position in that 32-bit value lets us
  15922. 11:22:35turn on or off some peripheral.
  15923. 11:22:38If I actually come up here, I can see
  15924. 11:22:41the one we're interested in is IO bank
  15925. 11:22:43zero. Um, that's just me telling you.
  15926. 11:22:45You wouldn't have known that on your own
  15927. 11:22:47without more reading of this
  15928. 11:22:48documentation. And you can see it's the
  15929. 11:22:50uh fifth bit position um from this this
  15930. 11:22:54reset register. So, we'd like to figure
  15931. 11:22:56out where the reset register is in
  15932. 11:22:58memory. And if I scroll down here, um,
  15933. 11:23:03we can see that it says the reset
  15934. 11:23:05controller registers. So there's several
  15935. 11:23:06registers. The one we're interested in
  15936. 11:23:08is just the reset one. Start at a base
  15937. 11:23:10address of this hex value. And then
  15938. 11:23:13we're interested in this reset. So it
  15939. 11:23:15says offset 0x0. What this means is that
  15940. 11:23:19you take the base address and add an
  15941. 11:23:20offset and that gives you the true
  15942. 11:23:22destination address. In our case, we're
  15943. 11:23:24adding zero. So it means the reset
  15944. 11:23:26register is quite literally at this
  15945. 11:23:27address. If we wanted to interface with
  15946. 11:23:29this reset done register, we'd look at
  15947. 11:23:32offset hex 8. So we'd act add hex 8 to
  15948. 11:23:35this value and that would let us
  15949. 11:23:37interface with this reset done register.
  15950. 11:23:39So the very first thing we need to do is
  15951. 11:23:42write to this reset register
  15952. 11:23:44and um
  15953. 11:23:47set the bit position for IO bank zero
  15954. 11:23:49which is the fifth bit position. set it
  15955. 11:23:51to zero and that'll let us tell the
  15956. 11:23:54computer that hey like we actually do
  15957. 11:23:56want to do some IO. So let's jump back
  15958. 11:23:58to the instructions and see what the
  15959. 11:24:00first few set of instructions are doing
  15960. 11:24:02that let us accomplish this. I'm going
  15961. 11:24:04to tell you what each of these
  15962. 11:24:05instructions does. Um this very first
  15963. 11:24:08one move s is moving a literal value of
  15964. 11:24:1232 into the register R1. So after this
  15965. 11:24:16instruction runs, register 1 will have
  15966. 11:24:19the literal value 32. But let's actually
  15967. 11:24:21spell that out in binary. Uh so we can
  15968. 11:24:23see why that's happening.
  15969. 11:24:33So this is 32 in hex. Um and the reason
  15970. 11:24:3632 is interesting is because where we
  15971. 11:24:40see uh what we see is all the all the
  15972. 11:24:42bits are zero except one. And that one
  15973. 11:24:46is actually the fifth bit position from
  15974. 11:24:48the right. Uh which is interesting
  15975. 11:24:50because we know that we needed to
  15976. 11:24:52eventually find some address in memory
  15977. 11:24:54uh of this resets register and set that
  15978. 11:24:57specific bit position uh to zero. So
  15979. 11:25:01that's just a little hint at where we're
  15980. 11:25:03going. So after uh this first
  15981. 11:25:06instruction executes, this is the state
  15982. 11:25:08of the machine. The next instruction um
  15983. 11:25:11is LDR which stands for load a value
  15984. 11:25:14into a register. And we're loading a
  15985. 11:25:16value into the register 2. And where
  15986. 11:25:20what the value we're loading into is
  15987. 11:25:22actually what uh whatever the program
  15988. 11:25:24counter plus 36 is. Look at that address
  15989. 11:25:27in memory um and get the value from
  15990. 11:25:30there. If we do the math, the program
  15991. 11:25:33counter because we're executing this
  15992. 11:25:35instruction has to be at this address.
  15993. 11:25:37So if we add 36 to it, uh we get this
  15994. 11:25:40address right here. And if we come down
  15995. 11:25:42here, we actually see that that
  15996. 11:25:44corresponds to this address right here.
  15997. 11:25:47And this should look very familiar
  15998. 11:25:49because that is the base address of the
  15999. 11:25:51reset register. So that's the value we
  16000. 11:25:53actually want to uh write into. So if I
  16001. 11:25:57copy that,
  16002. 11:25:59then the state of our computer. Oops.
  16003. 11:26:03Um, after the first instruction,
  16004. 11:26:06I've done something wrong here. I' gone
  16005. 11:26:08to a weird state there. Um, fix this up.
  16006. 11:26:11So, R2 now holds the address of this
  16007. 11:26:14reset register,
  16008. 11:26:16uh, where it's memory mapped to. The
  16009. 11:26:18next thing we actually need to do is
  16010. 11:26:19read what the value in is. And so that's
  16011. 11:26:21exactly what this next instruction does
  16012. 11:26:24where LDR says load into register 3
  16013. 11:26:28whatever uh the address in register 2
  16014. 11:26:32points to. So register 2 points to the
  16015. 11:26:34address of this this resets register. So
  16016. 11:26:37we're actually going to load the
  16017. 11:26:38contents of that register into R3. So R1
  16018. 11:26:42and R2 are the same after this command.
  16019. 11:26:44R3
  16020. 11:26:47is going to look something like this.
  16021. 11:26:49It's a 32-bit value. We don't really
  16022. 11:26:52know what each of the bit positions are
  16023. 11:26:55except for the fact that we do know that
  16024. 11:26:57since the uh this LED is turned off at
  16025. 11:27:00boot, um this fifth bit position right
  16026. 11:27:03here is going to be a one. And what we'd
  16027. 11:27:06like to then do is actually rewrite that
  16028. 11:27:09value um keeping every other bit
  16029. 11:27:11position the same but flipping this one
  16030. 11:27:13to a zero. And so that's exactly what
  16031. 11:27:17this bic instruction will do. Um it'll
  16032. 11:27:21say this is the bitwise and of register
  16033. 11:27:253 and the inverse of register 1. So
  16034. 11:27:28let's break that down. Um let me go
  16035. 11:27:30ahead and we know that register one has
  16036. 11:27:34this value. The inverse of register one
  16037. 11:27:37would be if we flip every zero to a one
  16038. 11:27:39and every one to a zero. And so that
  16039. 11:27:41looks exactly like that. And then we are
  16040. 11:27:43doing um a bitwise and with register 3.
  16041. 11:27:49So what effect would this have? Let's
  16042. 11:27:50just think about this reasonably. We
  16043. 11:27:52said that register 3,
  16044. 11:27:55register 2 and register 3 are still the
  16045. 11:27:57same. Um register three, I'm just going
  16046. 11:28:00to move this out of the way so we can
  16047. 11:28:01look at these next to each other.
  16048. 11:28:03Register three, we know that we don't
  16049. 11:28:05really want to change the values in any
  16050. 11:28:07of the bit positions. So a bitwise and
  16051. 11:28:10of any either a zero or a one with a one
  16052. 11:28:14is just going to return the same value
  16053. 11:28:15that's already in x. So in every place
  16054. 11:28:18that we are bitwising bitwise and an x
  16055. 11:28:21with a one, it's just going to remain
  16056. 11:28:23the same.
  16057. 11:28:24The interesting thing is this fifth bit
  16058. 11:28:27position when we bitwise and it with
  16059. 11:28:29zero it's actually just going to turn
  16060. 11:28:31off that one bit position because
  16061. 11:28:34anything and zero through a logic gate
  16062. 11:28:37is necessarily zero. So R3 becomes this
  16063. 11:28:40register where every bit position is
  16064. 11:28:42unchanged except the fifth posit fifth
  16065. 11:28:45bit position becomes a zero and that is
  16066. 11:28:49the value that we'd like to write back
  16067. 11:28:51into this address in memory to tell our
  16068. 11:28:54CPU hey we actually uh are we actually
  16069. 11:28:57do want to interact with these GPIO pins
  16070. 11:28:59and so we've constructed the value that
  16071. 11:29:02we'd like to write back and it's in
  16072. 11:29:03register 3 but we actually need to write
  16073. 11:29:05it back to this address in memory uh for
  16074. 11:29:08it to be usable. And that's what the
  16075. 11:29:10next instruction does. It says store
  16076. 11:29:15um the contents of register 3, I what
  16077. 11:29:19we'd like to write into the resets
  16078. 11:29:20register into the address held by
  16079. 11:29:23register 2. And this is saying you can
  16080. 11:29:25just add zero to it. So it's just the
  16081. 11:29:26same as not specifying anything. So what
  16082. 11:29:29this will then do is actually um write
  16083. 11:29:33this value this binary value into the
  16084. 11:29:37resets register. And so we've if we jump
  16085. 11:29:39back to our docs here um we can see that
  16086. 11:29:43you know if the bit is set to one then
  16087. 11:29:44the resets asserted. If the bit is
  16088. 11:29:46cleared i.e set to zero then the reset
  16089. 11:29:48is deasserted. So now this means that
  16090. 11:29:51we've written this value back in and
  16091. 11:29:53have turned off the fifth fifth bit
  16092. 11:29:55position. it tells the computer that we
  16093. 11:29:57actually do want to use IO bank zero. So
  16094. 11:30:00that's our first step done. And I'm
  16095. 11:30:01actually going to clear out all the
  16096. 11:30:03instructions we've seen until now just
  16097. 11:30:04to keep things cleaner. Um so we've made
  16098. 11:30:07sense of those instructions, but when we
  16099. 11:30:10tell the computer that we want to turn
  16100. 11:30:11on IO bank zero, it actually takes some
  16101. 11:30:14time to do so. And so we need to wait
  16102. 11:30:17until the computer fully acknowledges
  16103. 11:30:18our request before we try to write to
  16104. 11:30:21the LED. If we simply wrote this value
  16105. 11:30:23of saying, "Hey, I want to use IO bank
  16106. 11:30:25zero," and then immediately turned on
  16107. 11:30:27the light, it might not actually work
  16108. 11:30:29because the computer needs to do some
  16109. 11:30:30stuff to actually enable IO bank zero.
  16110. 11:30:33And so if I come here, there's this
  16111. 11:30:36reset bit reset done register, and it's
  16112. 11:30:40the same structure where it says there's
  16113. 11:30:41a bit for each peripheral except the
  16114. 11:30:44difference is that it gets set once the
  16115. 11:30:46peripheral is out of reset. And there's
  16116. 11:30:48an explicit comment that says this
  16117. 11:30:50allows software, i.e. the bite code that
  16118. 11:30:52we are writing to wait for this status
  16119. 11:30:54bit in case the peripheral has some
  16120. 11:30:56initial initialization to do before it
  16121. 11:30:59can be used. So in our specific case
  16122. 11:31:01there is some stuff that needs to happen
  16123. 11:31:03before we can just use the uh the LED.
  16124. 11:31:06So we need to wait um for the bit
  16125. 11:31:09position five in this reset done
  16126. 11:31:11register to tell us uh that it get that
  16127. 11:31:15it we are safe to proceed. So
  16128. 11:31:19the next question is where is this
  16129. 11:31:21resets done register and if we scroll
  16130. 11:31:24back up uh oops or maybe it's down um we
  16131. 11:31:29see that it is right here we get the
  16132. 11:31:31address. So it starts at the same base
  16133. 11:31:34address that the resets register is at
  16134. 11:31:36but this time it's hex 8 further away
  16135. 11:31:39from uh the base address. So now we'd
  16136. 11:31:41expect the uh the address of this
  16137. 11:31:43register to be 400C
  16138. 11:31:47008.
  16139. 11:31:49And if I tab back to our code, we can
  16140. 11:31:52see that that is exactly somewhere in
  16141. 11:31:55memory. Uh that address is stored
  16142. 11:31:57somewhere in memory. So we can probably
  16143. 11:31:59expect our code's going to try uh try
  16144. 11:32:01access that address in memory. So let's
  16145. 11:32:03go ahead and do that. Um let's figure
  16146. 11:32:07out what this next instruction does.
  16147. 11:32:09This next instruction says uh we are
  16148. 11:32:12going to load the same fifth bit
  16149. 11:32:15position mask into R2. So that's the
  16150. 11:32:19same little cheeky hack we did to get
  16151. 11:32:22every bit zero except this fifth
  16152. 11:32:23position index. Um because we're going
  16153. 11:32:27to do the same thing where we actually
  16154. 11:32:28try wait and keep reading for that fifth
  16155. 11:32:30bit position in this new register to
  16156. 11:32:32figure out when it becomes a one. And
  16157. 11:32:34then we're going to see that we're going
  16158. 11:32:36to load uh our next instruction says
  16159. 11:32:40load into register 1 whatever is 28
  16160. 11:32:43memory addresses below this current one.
  16161. 11:32:45And that is actually right here
  16162. 11:32:48which is that exact address of this
  16163. 11:32:50register that we're interested in
  16164. 11:32:52interfacing.
  16165. 11:32:54So this is the address of the reset done
  16166. 11:32:58register.
  16167. 11:32:59Then what we do is we actually that's
  16168. 11:33:02our register one has the address that we
  16169. 11:33:04need to read from memory. So we actually
  16170. 11:33:06need to go read that value from memory
  16171. 11:33:08into another into another another
  16172. 11:33:10register sorry. So that's what our next
  16173. 11:33:12instruction does. It says load into
  16174. 11:33:15register 3 whatever the contents is at
  16175. 11:33:18the address held in register one which
  16176. 11:33:20is just the reset done register. And
  16177. 11:33:24that will look something like
  16178. 11:33:27we don't really know. It may or may not
  16179. 11:33:29be done by the time we read it. So
  16180. 11:33:33we can say maybe it looks something like
  16181. 11:33:35this. Um
  16182. 11:33:38except this one right here could be a
  16183. 11:33:40one or a zero. So I'm going to write it
  16184. 11:33:42as a question mark. We don't know if
  16185. 11:33:43it's going to be done or not. And so the
  16186. 11:33:45next thing we need to do is actually
  16187. 11:33:46read that fifth bit position and figure
  16188. 11:33:49out if it's if it's one or zero because
  16189. 11:33:51if it's one that means we are
  16190. 11:33:53initialized and we can actually turn on
  16191. 11:33:55the light. Otherwise we need to wait and
  16192. 11:33:57check again. So this tst instruction
  16193. 11:34:02um is what will do that and it's called
  16194. 11:34:04test bits. It'll do a bitwise and
  16195. 11:34:07between the two arguments. In this case
  16196. 11:34:09that's register two and register three.
  16197. 11:34:11And similarly it won't spit out a value
  16198. 11:34:14but it'll set flags the same way in our
  16199. 11:34:17CPU doing an addition wouldn't would it
  16200. 11:34:19would spit out a value but it also
  16201. 11:34:20implicitly update the flags register.
  16202. 11:34:23This CPU architecture has the same thing
  16203. 11:34:24where it has its own concept of a flags
  16204. 11:34:26register. So we already know that um bit
  16205. 11:34:30like R2 bitwise anded with R3
  16206. 11:34:35uh it's either going to be zero if the
  16207. 11:34:39um if the LED is not ready
  16208. 11:34:43or it'll be one um if it is ready
  16209. 11:34:47because this question mark would be a
  16210. 11:34:48one. So one bit wise ended with one
  16211. 11:34:50would imply that it is turned on one if
  16212. 11:34:54ready. So we make that comparison and
  16213. 11:34:57then this branch uh this this next
  16214. 11:35:01instruction says beq.n
  16215. 11:35:05and
  16216. 11:35:07this pretty much means if the if the bit
  16217. 11:35:10was a zero i.e. if the output of the
  16218. 11:35:13last instruction was a zero we aren't
  16219. 11:35:15ready yet. So we need to branch um back
  16220. 11:35:20to this address right here. So pretty
  16221. 11:35:22much we'll check what the output of the
  16222. 11:35:24last instruction was and if it is a zero
  16223. 11:35:29then we will jump to this address right
  16224. 11:35:31here which is actually the one we just
  16225. 11:35:33came up from right here. So if it is
  16226. 11:35:36zero what it'll try to do is actually
  16227. 11:35:38just read that same register again and
  16228. 11:35:40do the same bitwise and check. Um and if
  16229. 11:35:43it is a one then we just fall through
  16230. 11:35:45this instruction as expected and we end
  16231. 11:35:47up here. So pretty much what this effect
  16232. 11:35:50has is it'll keep checking the value of
  16233. 11:35:54the reset done register until it spits
  16234. 11:35:57back a one which means at this point
  16235. 11:35:59that we reach this uh this code right
  16236. 11:36:01here the the LED is ready to use.
  16237. 11:36:06Let me go ahead and now clear out this
  16238. 11:36:08code
  16239. 11:36:10since we know what it does and we're
  16240. 11:36:12getting near the end but we still
  16241. 11:36:13haven't done anything to turn on the
  16242. 11:36:14light. Um, and so now that we've told
  16243. 11:36:17the computer that we actually want to
  16244. 11:36:18use the GPIO pins, we need to go tell
  16245. 11:36:21the computer how we actually want to use
  16246. 11:36:24the LED specifically. So the LED, if you
  16247. 11:36:28read the documentation, corresponds to
  16248. 11:36:30GPIO pin number 25. And there's several
  16249. 11:36:34different ways to interface with the
  16250. 11:36:35GPIO pins besides reading or writing
  16251. 11:36:37values to them directly from code. But
  16252. 11:36:40we aren't going to get into that in this
  16253. 11:36:41video for the sake of our video. All we
  16254. 11:36:44do want to be able to do is read and
  16255. 11:36:45write values to these as if they were
  16256. 11:36:47just addressable regions in our RAM. So
  16257. 11:36:50we need to go tell the code that that's
  16258. 11:36:52exactly how we want to use these. That
  16259. 11:36:54is called SIO. Since we're single cycle
  16260. 11:36:57IO um and it's required, we need to tell
  16261. 11:37:00the CPU that for GPO GP IO pin number
  16262. 11:37:0425, we'd like to be able to interface it
  16263. 11:37:06with SIO, single cycle IO. And that'll
  16264. 11:37:10just pretty much allow us to get the
  16265. 11:37:12remainder code running that lets us
  16266. 11:37:15write uh to a particular address in
  16267. 11:37:17memory to actually turn on the LED. So
  16268. 11:37:19I'll jump back to the documentation
  16269. 11:37:22and we'll see that there is a uh a
  16270. 11:37:26register called GPIO25 control and it is
  16271. 11:37:31a control including function select and
  16272. 11:37:33overrides. So uh again I'm not going to
  16273. 11:37:36spell out how I knew to do this uh just
  16274. 11:37:38for the sake of time but we pretty much
  16275. 11:37:40just want to write the number five into
  16276. 11:37:41this register and the system will
  16277. 11:37:43interpret that number five as uh SiO. So
  16278. 11:37:47we need to figure out where this
  16279. 11:37:48register lives in memory. And we can see
  16280. 11:37:51that uh the user bank IO register start
  16281. 11:37:55at this base address. And then R number
  16282. 11:37:5825 has an offset of 0C8 or sorry 0cc. If
  16283. 11:38:05I jump back in the code, coincidentally
  16284. 11:38:07enough, this address lives right here.
  16285. 11:38:11So we'll probably expect our code to uh
  16286. 11:38:13to do that pretty pretty to read that uh
  16287. 11:38:16specific address and RAM pretty shortly.
  16288. 11:38:18Let's keep tracing what this does. So
  16289. 11:38:20this will load uh the first instruction
  16290. 11:38:22will load the literal value five um into
  16291. 11:38:27R2 because that's the value that we'd
  16292. 11:38:29like to write into this register. And
  16293. 11:38:31then the next instruction will load into
  16294. 11:38:35R3 whatever the value in RAM is at
  16295. 11:38:40program counter i.e. current instruction
  16296. 11:38:42plus 24 that turns out to be this
  16297. 11:38:44address that ends in 304 which is the
  16298. 11:38:47address that we just identified of the
  16299. 11:38:49specific GPIO25 controller. So that's
  16300. 11:38:52what R3 will look like. And then the
  16301. 11:38:55last thing we do is we'll say store
  16302. 11:38:58write whatever is in R2. So write the
  16303. 11:39:00number five to some address in memory
  16304. 11:39:03and that address in memory is held in
  16305. 11:39:06the value of R3. So this will actually
  16306. 11:39:08do the writing um write five into the
  16307. 11:39:12GPIO25
  16308. 11:39:14control line. And so this will now tell
  16309. 11:39:17the computer we intend to use uh this
  16310. 11:39:21GPIO25 which is the LED um in the normal
  16311. 11:39:24quote unquote way of being able to read
  16312. 11:39:27and write from it as if it was just
  16313. 11:39:28random or specific addresses in memory.
  16314. 11:39:31So let me go ahead and clear that code
  16315. 11:39:33out.
  16316. 11:39:37That the IO and GPIO stand for input
  16317. 11:39:41output which means that each of the pins
  16318. 11:39:44can be used to either put in put out a
  16319. 11:39:46value. So write a logical one or a
  16320. 11:39:48logical zero or it can be used to read
  16321. 11:39:51in a value. So if it's connected to some
  16322. 11:39:53circuit that outputs a value, it can
  16323. 11:39:55read in a logical one or a logical zero.
  16324. 11:39:58In our case, we need to specify that um
  16325. 11:40:01the computer doesn't know how we're
  16326. 11:40:02going to use each pin. So, we need to go
  16327. 11:40:04tell it, hey, for this GPIO pin, we're
  16328. 11:40:06going to use it either as input or
  16329. 11:40:07output. For GPIO 25 specifically, that
  16330. 11:40:11is the LED. We know that it can only be
  16331. 11:40:15used for output because it's literally
  16332. 11:40:17connected to a light. So, we can either
  16333. 11:40:18turn that light on or off. But for
  16334. 11:40:21consistency with how every other GPIO
  16335. 11:40:23pin works, we need to go tell the uh
  16336. 11:40:25processor that we intend to use use it
  16337. 11:40:27as output. So if I jump back to the docs
  16338. 11:40:30here, there is this register um GPIO
  16339. 11:40:34output enable set. And if I jump into
  16340. 11:40:37it, uh it kind of gives this implication
  16341. 11:40:40that uh it's similar to the previous
  16342. 11:40:42registers we worked with where it's one
  16343. 11:40:44register and each bit in it corresponds
  16344. 11:40:48to one of the GPIO pins. So in our case,
  16345. 11:40:51we need to go ahead and tell uh the 25th
  16346. 11:40:55bit position, i.e. the bit position that
  16347. 11:40:58corresponds with GPIO number 25
  16348. 11:41:01that we intend to use it as an output.
  16349. 11:41:03And we do that just by writing um
  16350. 11:41:07writing a logical one to it. And so we
  16351. 11:41:10need to figure out where this register
  16352. 11:41:11is. We can see they started a base
  16353. 11:41:13register of hex D followed by a bunch of
  16354. 11:41:15O's. And then this specific one ends
  16355. 11:41:18with
  16356. 11:41:20has the offset hex 024. So if we add
  16357. 11:41:23those together, we'd get D 000000
  16358. 11:41:27several zeros and then 24. So, if I jump
  16359. 11:41:30back to code, I can see that that
  16360. 11:41:31address lives right here. And what I'm
  16361. 11:41:34actually going to do is come down here
  16362. 11:41:37and clear out these three addresses
  16363. 11:41:38because we're not going to read them
  16364. 11:41:40anymore. The code that that used them
  16365. 11:41:42once has already been executed. By the
  16366. 11:41:44time we get to this top instruction, we
  16367. 11:41:46know that we're not going to read those
  16368. 11:41:47values again since we've accomplished
  16369. 11:41:49what we needed to.
  16370. 11:41:51What we will do, though, is load um
  16371. 11:41:55128 into R3.
  16372. 11:41:58So if I do that, this is what the binary
  16373. 11:42:01looks like. Notice that there's one bit
  16374. 11:42:03position on followed by seven zeros.
  16375. 11:42:06We're not done with this. Uh it kind of
  16376. 11:42:07is an arbitrary value for now, but we'll
  16377. 11:42:09come back to that. Then what we'll do is
  16378. 11:42:11we'll load
  16379. 11:42:13into register 2.
  16380. 11:42:16um whatever address is hex 20 below what
  16381. 11:42:20we're at right now, which corresponds to
  16382. 11:42:22this address ending in 308, which is the
  16383. 11:42:25address of that GPIO um output set
  16384. 11:42:30register.
  16385. 11:42:32Then what we'll do is we know we need to
  16386. 11:42:36turn on the uh 25th bit but right now
  16387. 11:42:41the bit mask we have in register R3
  16388. 11:42:44corresponds to the uh seventh bit.
  16389. 11:42:48There's notice how there's seven zeros
  16390. 11:42:49after here. Um so we end up doing is we
  16391. 11:42:52want to left shift this content. I left
  16392. 11:42:55shift means um chop off a zero at the
  16393. 11:42:59end and move it over to the right. We
  16394. 11:43:02want to left shift 18 times. And that's
  16395. 11:43:04exactly what this instruction right here
  16396. 11:43:06does. It'll say, "Hey, take register R3,
  16397. 11:43:09its contents, left shift them 18 times,
  16398. 11:43:12and then write that result back into
  16399. 11:43:13register R3." And so if I copy what R3
  16400. 11:43:19looks like after and R2 is unaffected by
  16401. 11:43:21this instruction, we can see that um
  16402. 11:43:24this actually is masking the 25th bid
  16403. 11:43:28position uh the one that corresponds to
  16404. 11:43:31our GPIO number 25. And so what we end
  16405. 11:43:34up doing in this next instruction is
  16406. 11:43:37storing
  16407. 11:43:39the value in R3 into RAM at the address
  16408. 11:43:43specified by R2. So this writes into
  16409. 11:43:47this um GPIO
  16410. 11:43:50outset register. And so now we've told
  16411. 11:43:54the computer, hey, we want to use this
  16412. 11:43:56specific register for output or this
  16413. 11:43:59specific GPIO pin for output. The very
  16414. 11:44:02last thing we can finally do now is
  16415. 11:44:04actually write a one to that pin so
  16416. 11:44:08it'll turn on the light. So what we need
  16417. 11:44:11to do is jump back to the docs. And the
  16418. 11:44:16register we actually want to write into
  16419. 11:44:18is this GPIO outset. And the structure
  16420. 11:44:23is pretty much the same where each bit
  16421. 11:44:26position corresponds to a GPIO pin. In
  16422. 11:44:30our case, the 25th pos 25th bit position
  16423. 11:44:34corresponds to the 25th um GPIO pin. And
  16424. 11:44:37if we write a one to that specific bit
  16425. 11:44:40position, it'll turn on the LED.
  16426. 11:44:45So, oh, the last thing we need to do is
  16427. 11:44:47figure out where the address of this
  16428. 11:44:48register is. We see it has the same base
  16429. 11:44:51address of hex D followed by zeros. And
  16430. 11:44:54then in this case,
  16431. 11:44:57the offset is hex14. So, we'd expect the
  16432. 11:45:01uh address to be d some number of zeros
  16433. 11:45:04ending in 14. If I tab back, that's
  16434. 11:45:07exactly what this address and uh this
  16435. 11:45:11this value and this address has right
  16436. 11:45:13here. So we see we in our next
  16437. 11:45:16instruction, we load into register 2
  16438. 11:45:18that specific address uh hex 20 below
  16439. 11:45:21where we are in the program counter.
  16440. 11:45:26And then that's all that instruction
  16441. 11:45:28does. Then this store uh does the same
  16442. 11:45:35write of this bit mask. And I apologize,
  16443. 11:45:38I deleted R3, but it actually never
  16444. 11:45:40changed from the code above. So I'll add
  16445. 11:45:41that back in.
  16446. 11:45:45It'll store the same value that just has
  16447. 11:45:47a 25th bit position set to the address
  16448. 11:45:50held in the R2 register, which is
  16449. 11:45:53specifically the GPIO out set um
  16450. 11:45:56register. And so what this will actually
  16451. 11:45:59do is after everything we've done, it'll
  16452. 11:46:01actually finally go ahead and turn on
  16453. 11:46:03the light. But after we do this, the
  16454. 11:46:06computer's
  16455. 11:46:07trivial or I guess naive is a better
  16456. 11:46:10word in the sense that it executes this
  16457. 11:46:12instruction. We want to end the program
  16458. 11:46:14here, but the program counter is just
  16459. 11:46:16going to increment and keep executing
  16460. 11:46:18what comes below it in memory. We want
  16461. 11:46:20to halt our program effectively. Just
  16462. 11:46:22leave the LED on. So, this last
  16463. 11:46:24instruction we have, I'll delete these
  16464. 11:46:26two since we don't need them. Um,
  16465. 11:46:29is just a branch, an unconditional
  16466. 11:46:31branch, meaning it'll always just branch
  16467. 11:46:33no matter what, uh, to this address
  16468. 11:46:35right here. But this address is the one
  16469. 11:46:37we're executing. So, pretty much all
  16470. 11:46:40this line will do is keep jumping to
  16471. 11:46:42itself, which means the program counter
  16472. 11:46:44can never make it further than what
  16473. 11:46:46we're at. Um, which effectively is a
  16474. 11:46:48halt. The code can never make it to any
  16475. 11:46:51other instruction in memory. So, what
  16476. 11:46:54will happen if we put this computer uh
  16477. 11:46:56this program on our on our little
  16478. 11:46:57microprocessor is it'll set up all the
  16479. 11:47:00initialization needed to turn on the
  16480. 11:47:03LED. It'll turn it on and then it'll get
  16481. 11:47:06stuck on this instruction. So, as long
  16482. 11:47:08as it has power, it'll never do anything
  16483. 11:47:10else. So, what we can go ahead and do is
  16484. 11:47:13actually put that program um onto the
  16485. 11:47:16computer. And let me switch back to my
  16486. 11:47:17camera and let's see how it runs.
  16487. 11:47:21So, I've gone ahead and installed the
  16488. 11:47:24program on this uh microcontroller. I'll
  16489. 11:47:27skip the part of how I actually
  16490. 11:47:28installed it because uh it's a pretty
  16491. 11:47:31interesting side discussion, but
  16492. 11:47:32unfortunately with the timing
  16493. 11:47:34constraints, I'm not going to be able to
  16494. 11:47:35get to it in this video. But what I
  16495. 11:47:38should expect if the program we built is
  16496. 11:47:40fully correct that as soon as I plug
  16497. 11:47:42this in via the USB port and give it
  16498. 11:47:45electricity, the only thing that we
  16499. 11:47:47should be able to perceive with our
  16500. 11:47:48eyes, there's a lot more going on. We
  16501. 11:47:50obviously can't see instructions getting
  16502. 11:47:52executed um in the same way we could
  16503. 11:47:54with the old computer because it's it's
  16504. 11:47:56just so much smaller and there's not
  16505. 11:47:57like all these these lights for our
  16506. 11:47:59human interpretability. But what I would
  16507. 11:48:02expect is the LED right here that you
  16508. 11:48:05can't see that well, that copper looking
  16509. 11:48:07uh circle should just turn on. And
  16510. 11:48:09that's all that should happen. It
  16511. 11:48:10shouldn't turn off. It should just turn
  16512. 11:48:12on. Um, and so I have power. Let's go
  16513. 11:48:16ahead and do that. I'm going to go ahead
  16514. 11:48:17and plug it in.
  16515. 11:48:20And you can see uh it just turns on. And
  16516. 11:48:23if I unplug it, it kills power to the
  16517. 11:48:25program. It's done. Obviously, it can't
  16518. 11:48:26turn on the light without electricity as
  16519. 11:48:28a source. But if I plug it back in, it
  16520. 11:48:30should rerun the exact same bite code
  16521. 11:48:32that we programmed onto it in the past
  16522. 11:48:34half hour. Uh, which means that the LED
  16523. 11:48:36should just once again turn on. And this
  16524. 11:48:39is cool. The computer will hold the
  16525. 11:48:41program forever until it's rewritten. So
  16526. 11:48:44no matter what power source I connect
  16527. 11:48:46this into, as long as it gets
  16528. 11:48:47electricity, that light will just turn
  16529. 11:48:48on. Obviously, this is a pretty trivial
  16530. 11:48:51program. Um, but we could make this more
  16531. 11:48:54interesting. For example, let's say we
  16532. 11:48:56wanted to blink the light. How would we
  16533. 11:48:57update our program to do that? One
  16534. 11:48:59natural way is just pretty much we know
  16535. 11:49:02how to turn it on and we know how to
  16536. 11:49:04turn it off by writing a zero to that
  16537. 11:49:06same register effectively. Um, turns out
  16538. 11:49:08there's actually a separate register,
  16539. 11:49:09but that's a separate discussion. Uh,
  16540. 11:49:11the the concept is fundamentally the
  16541. 11:49:13same. What we could do is just load a
  16542. 11:49:16value, a really large value into a
  16543. 11:49:18register and keep subtracting it down by
  16544. 11:49:20one and then once it gets to zero, turn
  16545. 11:49:24off the light and then start the same
  16546. 11:49:26cycle uh the same sequence where you
  16547. 11:49:28load a really high number, count it down
  16548. 11:49:30to zero one at a time and once it hits
  16549. 11:49:32zero, turn the light back on. And the
  16550. 11:49:35computer, I mean, it runs at a pretty
  16551. 11:49:37fast clock speed, but if we're dealing
  16552. 11:49:38with very large numbers, counting down
  16553. 11:49:39by one will actually take hundreds of
  16554. 11:49:42milliseconds. um which means that it
  16555. 11:49:44gives the illusion of this light
  16556. 11:49:45blinking and pretty much changing that
  16557. 11:49:47number from lower to higher will mean
  16558. 11:49:50that the it'll slow down the blink cycle
  16559. 11:49:52and if I make that countdown number
  16560. 11:49:53lower it'll make it blink faster and
  16561. 11:49:55faster. Um so unfortunately we just got
  16562. 11:49:59around to looking at this computer and
  16563. 11:50:00there's a ton of interesting stuff we
  16564. 11:50:01can do. If I had more time, I would have
  16565. 11:50:03loved to continue playing around with
  16566. 11:50:04the GPIO pins and see how we can
  16567. 11:50:07interface with external sensors or even
  16568. 11:50:10write, you know, text to a screen as
  16569. 11:50:11I've done here. But, uh, unfortunately,
  16570. 11:50:15I'm bounded by YouTube's 12-hour upload
  16571. 11:50:17limit. Um, and so this feels like the
  16572. 11:50:19best stopping place I could reach for
  16573. 11:50:21this video. I wanted to take just a
  16574. 11:50:23little bit to recap everything we've
  16575. 11:50:24done. Uh, on one hand, we culminated
  16576. 11:50:28nearly 12-hour video with just turning
  16577. 11:50:29on a light. Um, but the more you know
  16578. 11:50:33about it, the more you realize like
  16579. 11:50:34that's a trivial way to look at things
  16580. 11:50:35and there's actually so much more
  16581. 11:50:36happening under the hood. So just as a
  16582. 11:50:39recall of everything we've done, we
  16583. 11:50:41started off looking at binary. Uh,
  16584. 11:50:43humans reason in base 10. We have the
  16585. 11:50:45digits 0 through 9. Computers only have
  16586. 11:50:48the presence or absence of electricity.
  16587. 11:50:50So we dumb our we we dumb it down to
  16588. 11:50:53base 2, which leaves the only digits
  16589. 11:50:55available us to zero and one. But it
  16590. 11:50:57turns out this is just as expressive as
  16591. 11:50:59decimal. Any number that we can
  16592. 11:51:01interpret as humans can be converted to
  16593. 11:51:03binary and vice versa. There's a one to
  16594. 11:51:05one mapping of decimal numbers and
  16595. 11:51:07binary numbers. Once we had binary
  16596. 11:51:10understood, we started looking at logic
  16597. 11:51:11gates and how we can make decisions by
  16598. 11:51:14chaining logic gates together. And then
  16599. 11:51:16we started looking at how we could store
  16600. 11:51:18data. We built the concept of a
  16601. 11:51:19flip-flop and a register. So rather than
  16602. 11:51:21a circuit just taking some inputs and
  16603. 11:51:24yielding an output, we could actually
  16604. 11:51:26hold in a value. And then we started
  16605. 11:51:28figuring out how we could chain both of
  16606. 11:51:29these ideas together to build more
  16607. 11:51:30interesting things. We started off by
  16608. 11:51:32building registers and then RAM. Um we
  16609. 11:51:36also built an ALU. We built the ability
  16610. 11:51:38to add and subtract binary numbers using
  16611. 11:51:40just logic gates.
  16612. 11:51:43And then we chain it together uh by
  16613. 11:51:45adding a bus throwing in more registers
  16614. 11:51:47and then the ability to interpret uh
  16615. 11:51:50instructions or values in memory as
  16616. 11:51:53instructions and then physically with
  16617. 11:51:56more logic gates in a readonly memory
  16618. 11:51:58execute those instructions sequentially.
  16619. 11:52:01And then once we had that, we looked at
  16620. 11:52:03a more modern processor which is uh not
  16621. 11:52:06too dissimilar from the original iPhone
  16622. 11:52:09and saw how we could we looked at its
  16623. 11:52:11instruction set and then also saw how we
  16624. 11:52:13could write programs for that computer.
  16625. 11:52:15And um if we had more time, what I'd
  16626. 11:52:18like to look into next is a few things.
  16627. 11:52:21Number one, how can a computer actually
  16628. 11:52:23write to a display? I think you have a
  16629. 11:52:25highle understanding, but we didn't get
  16630. 11:52:26to see it in practice, so I think it
  16631. 11:52:28might be a little bit confusing. Number
  16632. 11:52:29two is what actually happens when you
  16633. 11:52:31power on a computer. We said that RAM's
  16634. 11:52:34kind of initialized as all zeros or
  16635. 11:52:35random memory. So how is a program
  16636. 11:52:37physically stored on this chip and how
  16637. 11:52:39is it such that when we actually give
  16638. 11:52:42the computer power, it just starts
  16639. 11:52:43executing that program. Turns out
  16640. 11:52:45there's a several really interesting
  16641. 11:52:46things that happen early and this is
  16642. 11:52:48called like the boot sequence. Um and
  16643. 11:52:51the third and last thing that I'd like
  16644. 11:52:52to look into if I had more time in this
  16645. 11:52:54video is how computers run multiple
  16646. 11:52:57programs at once. your computer, you can
  16647. 11:52:59multitask, you can have a video playing
  16648. 11:53:01um while texting someone, while browsing
  16649. 11:53:03the web. And uh the the computer we've
  16650. 11:53:06architected here can only run one
  16651. 11:53:09program at a time. Uh but computers have
  16652. 11:53:11this clever little trick that actually
  16653. 11:53:13allows them to um give the illusion of
  16654. 11:53:16running multiple programs at once by
  16655. 11:53:18quickly swapping between separate
  16656. 11:53:20programs uh in quick succession so they
  16657. 11:53:22each get a little bit of runtime. Um,
  16658. 11:53:26this video ended up being way longer
  16659. 11:53:27than I expected. I'm not sure if anyone
  16660. 11:53:29will ever watch it, but it was awesome
  16661. 11:53:32for me to make this. I am sure there's
  16662. 11:53:34some gaps in parts of the videos. It's
  16663. 11:53:36my first time doing something like this,
  16664. 11:53:37so if there's any questions, um, feel
  16665. 11:53:41free to leave a comment. I promise I'll
  16666. 11:53:43monitor the comments and respond to
  16667. 11:53:44anything that's ambiguous. And uh if
  16668. 11:53:47time permits, uh maybe one day I can get
  16669. 11:53:50around to making a follow-up video if
  16670. 11:53:52there's uh any number of people that
  16671. 11:53:54actually watch this and have interest.
  16672. 11:53:56Um I don't know if I could do a whole
  16673. 11:53:57another 12-hour video if no one's going
  16674. 11:53:59to watch it. But yeah, uh thank you
  16675. 11:54:02everyone or anyone is probably a better
  16676. 11:54:04word who made it this far. And I hope
  16677. 11:54:07you learned something. This was probably
  16678. 11:54:08the most interesting part of my computer
  16679. 11:54:10science degree and I think a lot of
  16680. 11:54:12software developers out there don't have
  16681. 11:54:14a good enough understanding of what's
  16682. 11:54:15happening at a low level. Um, but to
  16683. 11:54:17take something that's very like foreign
  16684. 11:54:19and actually break it down and see
  16685. 11:54:21what's happening down to the flow of
  16686. 11:54:22electricity is very powerful. And now
  16687. 11:54:25you know that uh this computer I hold
  16688. 11:54:27and most computers that you interface
  16689. 11:54:29with are nothing more than logic gates
  16690. 11:54:32chained together in very interesting
  16691. 11:54:33ways to manipulate electricity to do
  16692. 11:54:36very interesting things. Um yeah, thanks
  16693. 11:54:38everyone.

About this transcript

This page contains the full transcript of How do computers work? (from scratch, no prior knowledge needed) by Milen Patel, generated from the public captions YouTube serves with the video. The transcript has 122,012 words across 16,693 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.