How To Build A Computer Tutorial

Computer Science
96 followers
#electrical #electricalengineering #computerscience
This is a tutorial on how to build a computer from scratch using logic gates in a digital logic simulation. We explore adding and subtracting with an ALU, storing bits with latches, registers, and flip flops, and writing opcode to control the CPU. The software is Sebastian Lague’s digital logic simulation.
Check out my channel to see my other video similar to this on a stack architecture CPU
Software- Digital Logic Simulation by Sebastian Lague
Welcome to the first podcast of Fullzore where we explore computer architecture on the transistor
level. In this episode we're going to be building our own CPU from scratch in a digital logic
simulation. So enjoy the next hour of computer architecture. Okay so welcome to this episode
of Fullzore and this is going to be a really long episode. We're going to make a computer
and I'm going to show you all step-by-step how to set up all the logic gates properly in order
to make this computer. So we are in the digital logic simulation by Sebastian Legue. I think
that's how you say his name. And we are just going to go right into it. So new project going to call
it Fullzore. And then first thing is we only have one gate right now which is the NAND gate.
And I'm going to show you how it works real quick. So when both the inputs are off the output is on
but if both the inputs are on the output is off. And this one gate can make a whole computer by
itself. And I mean that's how the simulation works and that's exactly what we're about to do.
But all you need is this one gate which is kind of cool. So this is the NAND gate. So if we
remove one of these and then hook up both or one input to both of the inputs in the NAND gate
we get a NOT gate. So we're going to save that. And if you don't want to watch this full video
because it's probably going to be pretty long. You can just follow each of my series that I have in
a playlist about how to make a computer. But this is just going to be a one full episode. So
and we're not going to make this optimal. I'm just going to make this so I can explain it the best
way possible. So if we have a NAND gate. Wait. I don't think I saved it. My bad.
There we go. So we have a NAND and a NOT. Then we make an AND gate. So if both the inputs are on
the output is on which is going to be useful. Now if we NOT both the inputs first then go into
an AND gate we get an OR gate. So the output turns on if either or or both the inputs are on.
And then we need to make an OR gate too. Just like the NAND gate we need to make the opposite
of the OR gate. Just like that we get an OR gate. And then now we got to go into these two different
gates called the XOR gate which is what the name Fullzore is named after. So if we take four NANDs
and go into this configuration right here we get a special type of gate. And now the output is on
if either or of the inputs are on but not if both. If both of the inputs are on the output is off.
And then there's multiple ways to make this gate but this is the way that I remember how to make
it and it's called an XOR gate. And then like the other ones we need to make a an XNOR gate. So just
the inverse of the XOR gate. So these seven gates are all the gates that are considered primary
gates you can say and they're used to make computers. So a computer is defined as an
electronic device for storing and processing data typically in binary form and it has instructions
given to it in order to do the computation. So that's exactly what we're going to do. And
processing data is a key word. A way computers can process data is by adding and subtracting.
So we're going to make the adder first and then so the way you make an adder is you're
going to make something called a half adder first. So now we can name these inputs.
Make it a little easier. That's good. So this is a half adder right here. So 1 plus 0 is 1.
1 plus 1 is 2. So in binary you can watch my actual adder video for more clarification.
But in our base 10 system when we add numbers and the number goes over 10 we have to carry the 1.
But in binary if the number goes over 1 we have to carry the 1. So that's exactly what we're
doing here. So 1 plus 1 gives us 2 which would light up this gate so we can carry the output
kind of going forward. So this is called a half adder. So if we have two half adders
and an OR gate we can make a full adder. I'll put carry. And this is what we call a full adder
right here. So 1 plus 1 is 2. And then this is the other carry input so we can chain these together
because just like base 10 system if you add two numbers your carry 10 can make a third column
in the top right row. So that's what this carry input is right here. So we have to do it like
this. And it works because 1 plus 1 is 2 and then plus 1 is 3 which gives us the answer right here.
So this is called a full adder. So if we chain these together which this is going to be a 4-bit
CPU also we can get a full adder. So this is it. So let's see if it works real quick. We'll have
2 plus 2. I gotta carry these together my bad guys. There we go. So 2 plus 2 is 4 which works.
2 plus 3 is 5. We can go with 6 plus 6 is 12. So it looks like it does work. So this is our
adder you can say. So we'll save the chip real quick for adder. There we go. So now we need to
make the full ALU which would also involve a subtraction part. So actually we're just
going to manipulate this adder to make the full ALU. Or actually we're not. Actually never mind
again because I don't really feel like redoing this all. So the way we make a subtractor and
you can look at my subtraction video too again for more clarification. But all we need to do
is put these XOR gates into the first 4 inputs just like this. Then we need a button
to turn on subtraction mode. And then we gotta hook it up in a special way. So let me wire this
up real quick. There we go. So we have the adder 2. And then if we put the subtractor on it kind of
inverts everything. Oh wait I forgot this one crucial part. I'm sorry. Let me save this real
quick. So if we go back to this we need to make an input right here. So we don't have to use this
carry input when we're adding because you can't carry anything on the first column of when you're
adding just like base 10. But subtracting it's crucial because it actually does a little trick
for us. So now when we turn subtract mode on we get the carry output becomes high when it equals
zero. So this is also kind of a zero output and it's also the carry output. So I guess we can call
it both. Which is good. So if the two numbers are equal to each other and they're equal and
subtract them gives a zero. We have a flag trigger right here that we can send to other parts of the
CPU. But it still works. We can still add. We've subtract mode on. So 2 minus 0 is 2. 2 minus 2
is off. 2 minus 1 is 1. And so it works. And again if you want more detail on how exactly
this mechanism works I have a full subtraction video. But this right here we're just kind of
trying to speed around how to make a computer. So it's not really too important right now.
But this is our ALU. So now we need to make a memory storage device for our ALU. So this is the
most basic way to store information. And it's just called an SR latch. So notice how the outputs are
going back into the inputs. So it can almost remember what state it's in. Which is kind of
cool. So I mean we can have well this is it's called SR latch reset and reset. So I have this
is reset and then this is set. Since when we turn this one on it makes the top output go high.
And we'll call this one Q. And this one will be negative Q. Since it's the inverse of what
Q is. Which could come in handy a little bit later. We should call it SR. So we have an SR
latch. The two outputs. And then we add these two AND gates. And we invert one of them. We get a data
latch. So if the store signal is high we store the output into Q. Just like this. And that's kind of
it. So this kind of simplifies the SR latch. So we can store data. But we can only store it if the
store signal is high. Just like this. So say the chip is a D latch. So we have four of these. We
can kind of make a D latch register. Which can come in handy. But sometimes we need like a clock
pulse D latch. And I'll show that in a second. But right now we're just going to make a D latch
register. All we care about is Q. So it doesn't really matter. Okay so but the problem with the
D latch is that if the store signal is high we store data. So if we connect the output up to our
ALU. And we feed the ALU new information. And the store signal is high. It's just going to like kind
of keep cycling through and through. So I don't know if that made sense. But I can show this real
quick. Since it's pretty easy. Oh wait my bad. So I'm just going to use this NOT gate to make this high.
If this is high and we go back into our ALU. It's going to glitch out. So we need to make a special
type of gate. But I'm trying to make this fast so I can show y'all. See and then it glitches out kind
of adding it together. So we need to make a way where it will only store one number for a high
signal. Then we have to wait for a next clock pulse to store another number. So we need almost a
clock sensitive register. Which that's what I'm calling it. Because this one is not clock sensitive.
We would almost need to make a pulse really quickly in order to store the data at the correct
moment. Which is impossible in the simulation since we don't have pulses you can say. At least
pulses that don't work. So we're going to make a clock one. In which we can do that with pure
logic gates. Thank goodness. Oh my bad. So we can do it with two delashes actually. So data
and then clock. And this is called a master slave delash. And it's called that because
this one controls this one. So the master and the slave I guess you can say. But that's the
technique we're using. And we'll use this in another area too in the future. So we have the
data is going to go to here. There you go. See. And so when the clock pulses
every clock pulse it changes the data. Let's wire it a little differently. So it goes on the
leading edge instead of the falling edge. And that's simple. We just got to reverse these two.
Now it does it on the leading edge instead of the falling edge of the clock. Which is perfect. This
is exactly what we want. So we're going to call this a master slave delash. And then if we do the
same technique we did with the normal one. Just get four of these. We can make a little register
with it. We can call this a clock. So let's see if it works real quick. Yep. We're good. So we're
going to call this master slave delash register. There we go. So now if we have the ALU. And then
our new and improved register. And which this is going to be the CPU now. Oh I always do this man.
Now we can add it relatively easily just by pulsing keep pulsing the clock. And we're just
going to add by one. So this works really well actually. So we can kind of start building our
CPU. And the design I'm going to use for the architecture is called an accumulator CPU.
And it's called that because this main register right here is called our accumulator. So and
it's going to store the output and feed it back into the input. This right here is the main focus
of this architecture that we are using. And so we can use a normal register right here since it
doesn't need to be clock sensitive. And I know I didn't say optimization but this does help out
on transistor count and complexity of the circuit as you've seen. And then we can add a little
SR latch right here. So if it's on this will make subtract mode high and vice versa. So I
can label this. And that's going to come in handy in the future. So cool. Now we need to make a
control unit. So we have so many modules and so many things we can do now. We can either store
into this register. We can store into the accumulator. We can make the ALU add or subtract.
And oh yeah this is called the instruction register too. So we can label it. Kind of
forgot the name for a second. So we have so many things we can do. We need a control unit that can
control each of these components separately and we can put it in the code which one we want to
activate. So I'm just going to save this right here and this will be our CPU. And a control unit
is pretty simple to make. You just need a whole bunch of AND gates. So we're going to make a
four AND gate thing real quick because that's going to help us when making the control unit.
So now the output is only on if all of the AND gates are on. So we're going to call this a four
AND. So we take five of these and we're going to have five main instructions or um yeah we'll have
five main instructions. And all we need to do is make a little decoder. So it's going to decode a
four bit binary number into individual outputs. So as you can see if um one is the input we get
the first one on just like that. And we're going to call these different things because they're
do different things in our computer. So this one's going to be accumulator store. This one's
going to be instruction register store. Add, subtract, and display. And I'll show you what
display is. It's super simple so that's why I didn't go over it. So if we have two then we're
going to need this output and it's like this for all of them. So it gets kind of repetitive and it
uses a lot of wires but it is crucial in being able to decide what we want to do next.
There we go. Looks like it um works well. Cool. So this is our control unit. And then if we go
back to our CPU we can just easily add it in right here. And then we can also add in our
display unit right here. So display unit is just going to save what the output was so we can
display the information to the user. So easy. Um subtract. Let's go with this one. So we're just
going to have to hook these up to these little inputs right here. And then we have this SR latch
so it can kind of remember it's in subtract mode for multiple clock cycles in case we just want to
keep subtracting. Okay so cool. We made some progress with this. Now all we need is our
instructions, our code, and we need to make a program counter so we know what clock cycle we're
on. So let's make the program counter real quick. So this is going to be made using a JK flip flop
and we're going to use the same master slave technique that we've been using. So this is a
pretty complex little circuit. It's going to use eight NAND gates and it's more like a um a T flip
flop than a JK flip flop because every clock cycle we're going to um add one or flip flop the number
and we won't have any other inputs to really affect that because um the CPU won't have the
jump ability just to keep things simple since we're already at 32 minutes in this video and
that's kind of absurd.
Okay so um a lot is happening right here it's kind of these two D latches you can say um together
in series and this is the master this is the slave because this one controls this one just
like we said before but a unique thing is that um the outputs on the far NAND gates go back to the
inputs on the first NAND gates and this allows us to give us this toggle cycle just like this
which is very useful in adding numbers by one instead of using a full ALU and register combo
which can be a little too much sometimes. So I'm gonna call this a T flip flop and then if we chain
these together we can get a little added. You see and all we're doing is adding by one and this is
going to be our program counter. Yeah we saved it um now we go back to our CPU which is right here
and we can add this all the way over here. We can also add the clock module too which is a built-in
module. Okay so now um we're going to need to build another decoder to decode the program counter
and we are going to need to build an instruction set and then we are done with the computer. So
we're just going to build um we're going to build our decoder first and we're just going to kind of
since the control unit is a decoder we're just going to use it again but I want to make a new
module just to keep things a little um neat and we'll only have we'll have five so we're only
going to have um five clock cycles or five instructions just to keep things again simple.
We'll call it a 4-2 decoder. So we go back to the CPU and then we can add this decoder
just to the program counter and now we can choose which instruction set we want to be visible.
So I'm going to save this and now we're going to make our instructions called the opcode
um and then we're going to have to use something called a tri-state buffer. So we're going to use
a bus uh an instruction bus for this and the buffer allows us to write on the bus without
corrupting the data from other units. So we need to make some like memory type thing with it um
and yeah we need we need more eight we need eight so
so this right here just allows us to put this 8-bit thing back into one just to make um life
a little easier because we are using an 8 opcode where the bottom four is for numbers so we can
store in the register and the top four is for the control unit so we know what to control.
Um we do need our enable pin so we can enable these buffers to output their data. So the way
this works real quick is on you can see these are black and these are gray. The gray represents a
zero and the black represents neither a zero or a one. So we display something on here the buffer
prevents it from being activated until we enable the enable pin and then it can be activated.
So pretty straightforward. I'm gonna call this an opcode chip so if you go back to our CPU
and then we can make an eight pin bus. So right here there we go just like that and
then we can connect all of these straight up to the bus. Move this a little bit
and then we can hook this up back to the program counter so we can enable each one at different
times in the clock cycle and that's what the whole use case of this was. Everything's coming
back to itself there we go oh cool um and then if I play the clock we just run through the cycle
and it does display all four bits so we can have uh theoretically 16 of these but for this clock
cycle we only have five so it's going to be a little short um on the timing of some of these.
So now we have to split up this to the control unit and then some to the instruction register.
So all we need to do is um we need we need a splitter right so eight to one we need two of
these and these are going to be the top eight and then these are going to be the bottom. So cool I
mean this is the CPU uh let's save it what that was about 45 minutes um but we made a permit we
did it we actually made a primitive CPU so that's kind of cool um yeah so let's um let's save so the
instructions is going to be two so we can see we save that and then we're going actually I'm going
to put this into subtract mode real quick which is one two three four four we are in subtract mode
so that's cool um the number is 112 what 13 13 we're going to save it right yes and then we're
going to save the accumulator real quick so now we're storing zero in that and then we can store
yeah so we kind of like just reset everything display is what one two three four five display
is five so yeah everything's reset now um we get out of this so we're not like kind of manipulating
it okay and now what we're just going to do a very simple program which we're going to store
five into the instruction register and then we're going to store it into the accumulator
going to go into add mode um which is what one two three and then we're going to
store two into the instruction register and then we're going to uh display the data
which is one two three four I always forget these numbers right and then it's just going to kind of
keep repeat doing this but what is this so we have five plus two five plus two should be seven
should be seven so we should see seven oh that was kind of freaky which we might just manually
do this by hand we do have a little glitch in this um app I don't know why it exists but it
does where sometimes you have to just manually do the clock for it to work so we have um
you need to store that first uh yeah so we store this in the instruction register
and then we um store yeah let's let's wipe everything clean real quick
okay so yeah let's let's redo this code I kind of
messed it up a little bit but that's all right that's all right so five there we go actually
first we're just going to put this on add mode and then we're going to store five and then we're
going to store five into the accumulator and then we're going to store two back onto the um this and
then we're going to display the data okay simple so now we're going to add mode oh why did this
store it's interesting interesting that we stored this number but it's fine
but yeah so we um did it I don't know why it didn't this is weird this is this is weird as hell
okay yeah now we store five and then we have um we have five coming out of the
this register correct yeah so we save that then we have two which is ten oh we did have seven
it just didn't display seven for some reason but yeah this is um it works it does work so we can
get it to do what we want and then if we um add two to um this we can see what we get there yeah
so cool it does work um and yeah that's it this is how to make a computer in under an hour
long video but this is all the basics so thank you for watching um I hope that
you watched this whole video if you did please leave a comment and let me know you did because
awesome um but if you have any questions about this please let me know all the glitches you
have to trust me in this one all the glitches that we've seen was part of the simulation
not part of the computer and in the future we're going to make a computer out of pure transistors
in simulation and in real life like these baby computers I have right here that's it for this
video thank you for watching um please subscribe the more subscribers we get the better my life
becomes and the more I can focus on doing this content because no one else is doing this on
youtube it's only me it's all my begging out the way and I'll see y'all later
