8-bit CPU control logic: Part 1

Ben Eater
16 Apr 201727:52

Summary

TLDRThe script outlines the process of programming a custom-built computer by defining and executing simple commands. It describes how to load data into a register, perform addition, and output results to a display. The video explains the binary representation of commands like 'LOAD A', 'ADD', and 'OUT', and demonstrates programming these into memory locations. It also covers the role of the program counter and the fetch cycle, providing a step-by-step guide to manually stepping through the program execution without control logic.

Takeaways

  • 💻 The script describes the process of writing and executing a simple program on a custom-built computer.
  • 📝 The program consists of three instructions: 'LOAD A 14', 'ADD 15', and 'OUT', which manipulate data in memory and output the result.
  • 🔑 The script explains that the commands are self-defined by the computer's creators, allowing for flexibility in language and functionality.
  • 🛠️ The 'LOAD A' command is represented by the binary value '0001', followed by the memory address to load from, in this case, '1110' for address 14.
  • 🔢 The 'ADD' command is assigned the binary value '0010', and it adds the contents of a specified memory address to the A register.
  • 📡 The 'OUT' command is represented by '1110' and is used to output the contents of the A register to a display.
  • 💾 Instructions are stored in memory, with the first instruction at location 0, and subsequent instructions sequentially placed.
  • 🔄 The process of executing a program involves a fetch cycle where the instruction is loaded from memory into the instruction register.
  • 🔄 The program counter (PC) is used to keep track of the current instruction being executed, incrementing with each cycle.
  • 🔍 The script provides a step-by-step guide on how to manually program the computer by setting values into memory and stepping through the execution process.

Q & A

  • What is the first instruction in the program mentioned in the script?

    -The first instruction in the program is 'LOAD A 14', which means to load the contents of memory address 14 into the A register.

  • What does the 'ADD 15' instruction in the script do?

    -The 'ADD 15' instruction is used to add the contents of memory location 15 to the A register and store the result back in the A register.

  • What is the purpose of the 'OUT' instruction in the script?

    -The 'OUT' instruction is used to output the contents of the A register to the display.

  • Why can the commands in the script be defined by the programmer?

    -The programmer can define the commands because they are building the computer from scratch, which allows for the creation of a custom instruction set.

  • How are the commands represented in memory according to the script?

    -Commands are represented in memory as binary values. For example, 'LOAD A' is represented as 0001 and the memory address to load from is appended as the next 4 bits.

  • What is the significance of memory location zero in the script?

    -Memory location zero is significant because it is where the first instruction of the program is stored, following the convention of starting memory addressing from zero.

  • How does the script describe the process of programming the computer?

    -The script describes programming the computer by placing specific binary values into memory locations to represent the desired commands and their parameters.

  • What role does the program counter play in the execution of the program according to the script?

    -The program counter keeps track of the current instruction being executed, starting at memory location zero and incrementing as each instruction is executed.

  • What is the fetch cycle as described in the script?

    -The fetch cycle is the process of loading the command from memory and putting it into the instruction register at the beginning of executing a command.

  • How does the script suggest stepping through the program execution manually?

    -The script suggests stepping through the program execution manually by manipulating control lines and using the clock pulse to simulate the actions that the control logic would perform.

  • What is the binary representation of the 'LOAD A 14' command as per the script?

    -The binary representation of the 'LOAD A 14' command is 0001 1110, where 0001 represents 'LOAD A' and 1110 represents the memory address 14.

Outlines

00:00

💻 Introduction to Programming a Custom Computer

The paragraph introduces the concept of writing a program for a custom-built computer. The program consists of three instructions: 'LOAD A 14', 'ADD 15', and 'OUT'. These commands are explained as follows: 'LOAD A 14' transfers the value from memory address 14 to the A register, 'ADD 15' adds the value from memory address 15 to the A register, and 'OUT' outputs the content of the A register. The creator of the computer has the freedom to define the language and commands, and in this case, 'LOAD A' is represented by the binary value 0001, with the following 4 bits representing the memory address. The paragraph emphasizes the flexibility in creating commands and programming them into the computer's memory.

05:03

📝 Programming and Executing a Simple Program

This section details the process of programming the computer by placing specific instructions into memory locations. The 'LOAD A 14' command is programmed into memory location 0 as 0001 1110, 'ADD 15' into location 1 as 0010 1111, and 'OUTPUT' into location 2 as 1110 0000. The paragraph explains the need for control logic to execute these instructions and the importance of having data in the memory locations referenced by the commands. The data at memory locations 14 and 15 are set to 28 and 14 in decimal, respectively, to demonstrate a simple addition operation. The paragraph concludes with a discussion on the necessity of step-by-step execution and the role of the program counter and instruction register in fetching and executing commands.

10:04

🔍 Step-by-Step Execution of a Program

The paragraph describes the step-by-step execution of a program, starting with the fetch cycle where the program counter is used to access the memory location pointed to by its value. It explains how the program counter's value is transferred to the memory address register to fetch the instruction from the corresponding memory location. The process of loading the 'LOAD A 14' command from memory location 0 into the instruction register is outlined, including the control signals used for 'RAM out' and 'instruction register in'. The paragraph provides a detailed walkthrough of the initial steps in executing a command, highlighting the importance of the clock pulse in synchronizing these operations.

Mindmap

Keywords

💡LOAD A 14

LOAD A 14 is an instruction in the script's custom programming language that signifies loading the contents from memory address 14 into the A register. This command is part of the demonstration of how a simple computer program is constructed and executed. The video uses this instruction to illustrate the basic functionality of moving data from memory to a processor register, which is a fundamental operation in computing.

💡ADD 15

ADD 15 is another instruction in the script, which means to add the contents of memory location 15 to the current value in the A register and store the result back in the A register. This command is used to demonstrate arithmetic operations within the computer's memory, showing how numbers can be manipulated and combined to perform calculations, a core function of any computer.

💡OUT

The OUT instruction, as mentioned in the script, is used to output the contents of the A register to a display. This is a crucial step in any program as it allows the results of computations to be visible or usable outside the computer's internal processes. The video uses this to show the final step in a simple program where the processed data is made available for external use.

💡Memory Address

Memory Address refers to the specific location in a computer's memory where data or instructions are stored. In the script, memory addresses like 14 and 15 are used to identify where certain data or instructions are located. Understanding memory addresses is essential for programming as it allows programmers to reference and manipulate data accurately within the computer's memory.

💡A Register

The A Register is a type of processor register used to store data temporarily during processing. In the script, the A Register is where the results of operations like loading data and arithmetic operations are held. It's a key component in the computer's architecture as it facilitates quick access and manipulation of data during program execution.

💡Mnemonic

A mnemonic is a word or phrase that represents a computer instruction. In the script, 'LOAD A' and 'ADD' are mnemonics for specific operations. Mnemonics are part of assembly languages and are used because they are easier for humans to understand and remember compared to binary codes. They are translated by an assembler into machine code that the computer can execute.

💡Binary Representation

Binary representation is the method of encoding information using only two symbols, typically 0 and 1. In the script, the command 'LOAD A 14' is represented in binary as '0001 1110'. Binary is fundamental to computer science as it is the language of computer hardware, with all data and instructions ultimately being processed in binary form.

💡Control Logic

Control logic refers to the part of a computer system that manages the flow of data and the execution of instructions. The script discusses the need for control logic to interpret and execute the programmed instructions. It's what allows a computer to follow the sequence of operations defined by a program, making it a critical component in the functioning of any computer.

💡Program Counter

The Program Counter (PC) is a register that stores the memory address of the next instruction to be executed. In the script, the program counter is used to keep track of the current position in the sequence of instructions. It's essential for the computer to know which instruction to execute next, making the program counter a key element in the execution of any program.

💡Fetch Cycle

A fetch cycle is the process of retrieving an instruction from memory. In the script, the fetch cycle is described as the initial step in executing a command, where the instruction is loaded from memory into the instruction register. This cycle is a fundamental operation in the CPU's instruction execution process, as it prepares the system to decode and execute the instruction.

Highlights

Introduction to writing and executing a program in a custom-built computer.

Explanation of the 'LOAD A 14' command to load contents of memory address 14 into register A.

Description of the 'ADD 15' command to add contents of memory location 15 to register A.

Clarification of the 'OUT' command to output the contents of register A to the display.

Discussion on the flexibility of defining computer commands and language for a custom-built computer.

The process of assigning binary values to commands like 'LOAD A' and 'ADD'.

Detailing how to program instructions into memory for execution.

The significance of memory location zero for storing the first instruction.

Binary representation of the 'LOAD A 14' command as '0001 1110'.

Binary encoding of the 'ADD' command and its associated memory address.

The decision-making process for the binary representation of the 'OUTPUT' command.

Programming the computer by setting values into memory locations.

The role of the program counter in tracking the sequence of instructions.

Setting up the program to load data from memory locations 14 and 15.

The fetch cycle's role in loading commands from memory into the instruction register.

Manual stepping through the program to understand the control logic requirements.

The necessity of control logic for the program to execute correctly.

Step-by-step demonstration of executing the 'LOAD A 14' command.

Visual representation of the program counter and memory address register interaction.

Transcripts

play00:00

Let's look at how we'd actually write a program

play00:02

and how a program would actually get executed.

play00:05

So I've written a program here that basically says "LOAD A 14"

play00:09

and so what that's saying is "load the contents of memory address 14 into the A register".

play00:16

And then it's saying "ADD 15"

play00:19

and what that means is "add the contents of memory location 15 to the A register

play00:25

and put the result in the A register".

play00:28

And then the last instruction is "OUT",

play00:30

and what that instruction means is

play00:32

"take the contents of the A register and output it to the display".

play00:36

Now you might be wondering,

play00:38

"where did these commands come from? Why does "LOAD A 14" mean that?"

play00:43

And this is actually a really cool thing, we're building this computer ourselves

play00:48

and so we get to define what the language is,

play00:51

we get to define what these commands are, so I made these up.

play00:54

And really, we can come up with any commands that we want, as long as it's

play00:58

some operation that we can actually do with this computer.

play01:01

And we can call them whatever we want,

play01:03

and we can come up with whatever we want.

play01:06

So this is just what I've come up with: this "LOAD A" command,

play01:10

and I've decided that what that means is it means to "take whatever is in

play01:14

the location that's referred to in memory and put it into the A register".

play01:18

And then I've decided that this ADD command, what it means is "take whatever is in

play01:22

this location and add it to the A register and leave the contents in the A register".

play01:26

And then the output command of course just means "take whatever is in the A register and output it".

play01:30

And so this is, we can come up with whatever commands we want. If we wanted to subtract,

play01:34

we could do that, the computer knows how to subtract,

play01:37

if we want to move things into the B register, we could have a command that does that,

play01:40

Anything that we can do with this computer, we can make a command to do that

play01:46

So then, how do we actually program these things into the computer?

play01:49

So for that, we want to put these instructions in memory,

play01:54

because the computer is going to execute programs from memory.

play01:57

And again, we're making the computer, we can make whatever choices we want to make here,

play02:00

but what I think makes sense

play02:02

is for the first instruction to go at memory location zero,

play02:06

and then the next instruction to go in location one,

play02:09

and then the last instruction here to go in location two.

play02:11

That seems to make the most sense just start at zero, one, two.

play02:16

So, the memory locations for these are going to be

play02:18

0000, 0001 and 0010

play02:24

so that's where in memory these these commands are going to go,

play02:28

but how do we actually put this command "LOAD A 14" into memory location zero?

play02:34

Because "LOAD A" is not, you know, this is an 8-bit value, "LDA" or "LOAD A" is sort of a mnemonic word

play02:44

And so again, since we're building the computer, we get to make the decision here

play02:49

of what this is actually represented as in binary.

play02:53

So, since we get to make up this command, we also get to make up what value it corresponds to, so

play02:58

I'm going to say "LDA" corresponds to 0001.

play03:03

That's "LDA" and so that will be the first 4 bits,

play03:08

and then the next 4 bits of the command are going to be the actual memory location that we're loading from,

play03:14

so in this case 14.

play03:15

And so 14 in binary is 1110.

play03:20

So this "LDA 14" command, we're actually going to put it in memory as 0001 1110,

play03:28

and that corresponds to "LOAD A 14", and again, we're just making this up,

play03:31

I could have used any 4-bit value as long as I'm consistent with that value meaning "LOAD A"

play03:39

So same thing with "ADD", of course I can't use 0001 for "ADD", I have to use something different,

play03:44

so I'll just use 0010, I'll just use 2, and so that will be the binary representation of "ADD".

play03:50

And then of course along with the "ADD" instruction we've got to tell it

play03:52

what address to add the contents from, and so that's 15, which is just all ones.

play03:59

And then "OUTPUT", again this is a new command, we get to decide what the representation is,

play04:05

I could go with 3, or I could go with anything I want, I could just go 1110

play04:10

and I think that will be what our "OUTPUT" is.

play04:14

And in this case, you know, "OUTPUT", I'm going to say this command says

play04:18

"take whatever is in the A register and output it to the output display".

play04:23

And so I don't really need to tell it anything else here so the other four bits could be anything that we choose,

play04:29

so we'll just set them all to 0, and you'll see when we actually build this output command in the logic here,

play04:37

we'll just ignore — we won't use those four bits for anything so they could really be anything.

play04:42

But of course you could imagine having a command that says "output from memory"

play04:45

and then a memory address, and it would go fetch the contents of that memory and output it.

play04:50

You could very easily define a command [for that], but we're not going to do that just yet.

play04:54

So this will be the program that we try to program in here,

play04:58

And so we can program it just by setting those values into memory, so what does that mean?

play05:02

So if we want to be in programming mode here we go to address 0

play05:08

So we're at address 0 and we want to set this: 0001 1110

play05:11

so 0001, 1110.

play05:16

And we'll program that in, and so now at address 0 we've got that command.

play05:20

So then we'll go to address 1 here and then we'll put in this

play05:26

so 0010, 1111,

play05:31

and we'll program that, so now we have our second command in memory

play05:35

and then we'll go to address 2, our last command,

play05:43

and program in our "OUTPUT" command, so that's just 1110, and then actually the rest

play05:47

don't matter but I'll set them all to zeroes and program that, so there's our "OUTPUT" command.

play05:53

So now we've actually programmed our computer, we've put the program in the computer.

play05:56

Of course it's not going to work because we don't have any control logic yet,

play05:59

but we can start to step through and see how this might actually work once we build the control logic,

play06:04

so we'll get a sense of what the control logic needs to do.

play06:08

And of course for this program to make sense, what we're saying is that we're loading the contents

play06:12

of memory location 14 and we're adding to it the contents of 15 and we're outputting that.

play06:17

So we need to put some data in address 14 and 15.

play06:20

So if I go to 14 it's got some data here

play06:24

let me put some other data, so if we go here to address location 14, which is 1110,

play06:31

and 15 which is 1111, I'm going to put some data here, let's say 0001 1100,

play06:40

and in this location we'll say 0000 1110.

play06:46

And this if we convert it to decimal is 28, and this is 14.

play06:51

And so what this program is really going to do is it's going to say "load whatever is in location 14, which

play06:56

is the number 28, into the A register, add whatever is in 15, so it's going to add 14 to that,

play07:02

and then it's going to output whatever the sum of those two numbers is.

play07:07

So at 14, so I'll program in the first number,

play07:11

and then we'll go to 15 and program in the next number.

play07:16

So we've got 14 and 15.

play07:18

And if we go back to 0, we've got our first command there.

play07:21

Let's switch that back to program, so the memory address is no longer controlled by the dip switches,

play07:26

it's controlled by the memory address register.

play07:28

So now we've got this program programmed in here, and obviously it's not doing anything, but of course

play07:33

there's no control logic, so it wouldn't do anything,

play07:36

if we had the control logic built already then it would pretty much run, but that's obviously not happening.

play07:40

So we need to kind of step through this step by step, and I need to essentially

play07:45

manipulate these control lines to do what the control logic would do if it were built, and that way, that should

play07:51

help you understand what it is we're actually trying to do when we build the control logic.

play07:55

So, to step through step by step, I'm going to stop the clock, and we can still step the clock manually

play08:00

by pushing this button,

play08:02

And so we're going to go super slow through this and go step by step.

play08:06

So the first thing that's going to happen when we start to execute a program, or really anytime we

play08:11

start to execute a command, is we need to load the command from memory and put it in the

play08:16

instruction register, because the instruction register is what tells us what command we're currently running.

play08:23

And so the beginning of executing any command is a fetch cycle, where you fetch the instruction

play08:30

from memory and put it into the instruction register.

play08:33

Now the other component that comes into play here is the program counter, which is up here,

play08:37

and the program counter is going to count through which instruction we're on, and it starts at 0, which

play08:42

is where it is right now, but once we execute the first instruction it is going to go to 1, and then 2,

play08:47

and then it would keep going 3, 4, 5 and so forth until we stopped executing our program.

play08:52

So our program counter is at 0, which is where we want to start,

play08:55

and the first instruction that we want to fetch from memory is in memory location 0.

play08:59

But in order to get to memory location zero, of course we're already there but

play09:03

we're just sort of there by accident. Really, to get there what we need to do is we need to take

play09:07

the program counter, the contents of the program counter, which is 0 in this case, but as we get

play09:11

to the next instruction you'll see it's one or two, we need to take the program counter and move that value

play09:17

into the memory address register here. So what we're going to do is

play09:21

we're going to say program counter out, so counter out (CO), which is here,

play09:26

and then memory address register in (MI), which is over here.

play09:30

So this is going to take the program counter and put its value onto the bus, and so there's a 0 on the bus,

play09:35

there's a 0 here — this is not going to look very interesting — there's a 0 in the program counter,

play09:38

there's now a 0 on the bus, and then we're going to take the 0 from the bus and put it into the memory address register.

play09:44

And so all of that is going to happen on the next clock pulse. So there's the clock pulse, and yeah that 0 just

play09:50

went over there, I'm not sure if you saw it. But no, that 0 went over there.

play09:54

It will be more interesting for the next instruction, trust me.

play09:57

So the first thing we did, so we're executing this "LDA", which is "LOAD A 14", that's the first instruction we're

play10:04

executing and the first thing we did is we said "program counter out" and "memory address register in",

play10:12

and then we pulsed the clock. So this is the first thing we did.

play10:15

The next thing we're going to do is we want to take the contents of memory, so now that this is pointing

play10:19

at 0, of course it was before but just bear with me, this is now pointing at address 0,

play10:24

so what we're seeing here is the contents of address 0, which is this here, this 0001 1110, which

play10:33

is the "LOAD A 14", so this the "LOAD A 14" command. We need to move that to the instruction register.

play10:40

And so to do that, what we want to do is turn all these things off,

play10:46

and so for the next clock cycle we want to take the "RAM out" (RO), which is this here,

play10:54

so "RAM out" and you can see the contents of RAM is now on the bus,

play10:59

and then we want to say "instruction register in" (II), which is right here.

play11:05

So we're going to say "instruction register in", and of course nothing happens yet until the clock pulse,

play11:10

which is good because that gives us time to set up and change what's going on

play11:14

in the control logic here, before the next clock pulse.

Rate This

5.0 / 5 (0 votes)

相关标签
Computer ProgrammingMemory AddressingInstruction SetBinary CommandsRegister ManipulationControl LogicCustom HardwareProgramming LogicComputer ArchitectureInstruction Execution
您是否需要英文摘要?