User Tools

Site Tools


notes:asm:cpu_simulator

As you break a computer down it is nothing more than a device composed of certain components to control the flow of electricity as it passes through it. This flow of electricity is in a major part manipulated by the CPU which is going to be the major focus of this project. This documentation will basically be. However, as you get more technical the electricity represents or stands for different things. As you work your way from the ground up the computer will become more complex in that the different states of electricity will stand for different things. these states will also start to create output. this output could be something like displaying something in the monitor. We will work from the ground up in our understanding. This will begin with a general breakdown of a CPU before venturing into the project implementation.

Boolean Logic and Boolean Gates

Let's begin this topic with a summation of what's to come. All a computer does is manipulate electricity with gates. Gets have 2 inputs and 1 output. The inputs are either high voltage or low voltage. The output is either high or low voltage. High voltage is commonly referred to as 1 and low voltage is commonly referred to as 0. Boolean logic is the logic inside the gates. Now we'll get a little more detailed.

Boolean logic is a very simple form of logic consisting of two variables. There are only 2 because a computer can only understand two states. These two states can have mathematical operations ( addition, multiplication) performed on them. The only difference is instead of multiplication the term AND is used. Instead of addition the term OR is used. There is also a NOT that will inverse any output. ( 0 to 1 and 1 to 0 )

The only operations the CPU understands are these 0's and 1's, this is how it makes all of its calculations

Logic 0 Logic 1
False True
Off On
Low High
No Yes
Open Switch Closed Switch

The above table show some different variables that a 0 and a 1 could stand for

Gates/Logic/circuits

Logic Pre-determined instructions that manipulate input

Gate Collection of logic with 2 inputs and 1 output

Circuits 2 or more connected gates

More or Less: A circuit breaks down in to gates, gates break down in to logic and logic breaks down in to the following tables.

  • AND

<html>

</html>

AND Logic
Input Output
x y xy
0 0 0
0 1 0
1 0 0
1 1 1

The only time an AND gate has an output of 1 is when the inputs are both 1 It literally needs both x 'AND' y to be 1's to get a 1

<html>

</html>

  • OR

<html>

</html>

OR Logic
Input Output
x y x+y
0 0 0
0 1 1
1 0 1
1 1 1

An OR gate has an output of 1 when x 'OR' y are 1's. (They can also both be 1 to have an output of 1) <html>

</html>

  • Not

<html>

</html>

NOT Logic
InputOutput
xx
01
10

The NOT gate takes a single input and simply inverses it. If a 0 is inputted it turns to a 1 and if a 1 is inputted it turns to a 0

The NOT gate can also be placed at the end of the AND, OR gates to create NAND, NOR gates

In diagrams the actual NOT gate is not showed seperately if it is after another gate, it is represented by a circle at the end of the other gate <html>

</html>

  • NAND

<html><div style=“padding-left: 1in;”></html>

NAND Logic
Input Output
x y xy
0 0 1
0 1 1
1 0 1
1 1 0

The Nand gate is the not and, it is the inverse of a and gate and is always 1 unless both inputs are 1

When these gates are combined together they create circuits. These circuits are what make up the ALU and CU of the CPU

  • Adders: Adders a simply circuits that add up binary numbers but they also have to account for a carry. It adds 0 + 1 = 1 ← simple. Now when it does 1 + 1 it must have a place to store the carry number. This can be represented by a simple circuit that has 2 inputs. Note: A circuit can have infinite inputs and outputs, only gates are limited.

Central Processing Unit (CPU) and its Components

The CPU's job is to manipulate inputted data and output it. Simple, right? It takes electricity which is the input and has it's way with it and outputs it in its new state.

CPU Major Components

  • ALU: (Arithmetic Logic Unit) Processes data
  • RA: (Register Array) Inventory of hardware registers, which are used for local on-CPU storage of data

General Requirements of the CPU

  • 1.) Fetch instruction: The CPU reads an instruction from memory
  • 2.) Interpret instruction: The instruction is decoded to determine what action is required
  • 3.) Fetch data: The execution of an instruction may require reading data from memory or an I/O module
  • 4.) Process data: The execution of an instruction may require performing some arithmetic or logical operation on data
  • 5.) Write data: The results of an execution may require writing data to memory or an I/O module

When broken down to the physical components the CPU is nothing but more than a bunch of logic gates that when combined create circuits. A logic gate is simply a physical something that takes 2 inputs and gives one output. The given output is different based on what type of gate is being used.

Memory Circuit

In order to accomplish the above 5 tasks it is necessary for the CPU to temporarily store some data. The CPU needs to remember where the last instruction came from so that it can take know where to get the next instruction from. The CPU needs internal memory for this. These areas of internal memory are called registers.

Memory and Flip-flop integration

A flip-flop is just a specific type of circuit designed for memory.

Complicated text book explanationAn obvious component to any computer is memory.. or for our project, simulated memory. Without memory there can be no computers as we know them. To create a simple 1-bit memory you need a circuit that can “remember” previous input values. The CPU cannot truly remember anything. This is why after shutting off or restarting a computer, anything that the CPU was computing is no longer there. Instead the CPU creates a circuit that has inputs and outputs, but in between the inputs and outputs is an infinite loop. No matter how many times the electrical current passes through this circuit it will continuously have the same output unless there is a change made to the input. Now if the computer is restarted or shut off all the electrical current is gone.. which would deactivate the gates. This small circuit that has an input, output and an endless loop in between is called a flip-flop SR flip-flop

Simple version A flip-flop is a circuit (series of 2 or more gates) of 2 NOR gates where 1 input from each is the other output. It simply creates a loop where the electricity doesn't change state but continues to flow. Since it can keep the same state over a period of time it is a 'memory circuit.'

Register provide internal storage for the CPU. It is where the CPU may temporarily store data because it can read this data much quicker than anywhere else on the computer.

Project Implementation

This project is being created from the ground up. It is beginning with programming for the Boolean logic. Then the gates code is working off the logic code. Then the circuit code is working off the gate code. Remember gates are built of logic and circuits are created from gates. The code for the project works in the same exact way. Coding for this project is actually simpler than it sounds, don't over complicate things as you'll see below.

Gate Code: The basic gates or 'boolops' as they're named for project purposees are and, nand, not, nor, or, xor and xnor. Remember that a gate is nothing more than a physical device that takes input, manipulates it and spits out a single output. With the project code it's just as easy to create a file for the specific gate, have some input, manipulate it and output it. The only requirement is that the manipulation does the same thing as the gate.

  • Example of AND gate. Keep in mind the input does not matter. The only thing that matters is that there are two arbitrary inputs. Their names or format are not important right now.
      if ((input1 == true) && (input2 == true))
      {
              result = true;
      }
      

This file can now be added to a library and be called by another function of the simulator any time it needs an AND gate. There are also a few circuits in this folder? Probably shouldn't be but oh well, let's discuss them. There is a full adder and a half adder circuit. Adder Circuits The adder circuits work by taking the basic gates that were created and calling them in order as the actual physical circuit is designed. The outputs are then given a name that can be called later by the other functions in the project.

This code is also how the flip-flop circuits will be created. The flip flop is a compilation of gates.. so if you create a file that calls the gates in the order they would be called in a flip-flop.. then you have coded for your flip flop circuit.

Now the register code is built up of the rest of the code. For example.

      6502 register/register.cc -> r6502.h -> g8sr.h, and.h nor.h
              g8sr.h -> ffnor.h, and.h
                      ffnor.h -> nor.h
                              nor.h -> NOR class
                      and.h -> class AND
              and.h -> class AND
              nor.h -> class NOR

Above is an example of how one of the registers breaks down in to all of the called files in the project. Each file is linked to another file until you get down in to the basic classes which is where the project began.

FAQ

1. How to connect a previous code into the register when the instruction are being set.

      The registers are instantiated in cpu/6502/interpreter.cc, there you will see:
              accum, prgcnt, status, indexx, indexy, stack
      Note these are defined globally, and because we're dealing with a multi-file
      project, the instruction-specific functions are a part of the interpreter
      code, and WILL be able to see and use these globally instantiated registers.
      Any public member functions of the reg6502 are possible on any of these
      instantiated registers (accum, status, etc.)... so you use the specific
      registers by name in the instruction functions (ADC, ADD, etc.).

2. Opcodes. All the list of them are instruction set for the register but how to properly code them so they perform operations i.e. ADC (add / carry) I seem on the 6502 of all the opcodes there but none of them make sense nor workable. could it be because it is not impented into the register yet? how to fix it?

      In cpu/6502/opcodes/AND.cc, you will see the implementation of the AND with
      accumulator instruction (6502 hex code 0x29). Notice how it makes use of the
      "accum", "status", and status flag defines (ZERO, NEGATIVE)? Those are not
      defined in this file, instead you can find them
      defined in: cpu/6502/interpreter.cc
      The interpreter.cc file is currently where everything is coming together--
      interactions with the user, figuring out exactly what needs to be done
      (ie which instruction to run), and resulting actions taking place on our CPU.

3. Shift circuits…when shifting circuits like examples in 8085 are different from 6502 because of the structure but why it is shift instead of clear?

      Shift instead of clear? I do not follow. Both processors will likely perform
      shifts in similar ways... but the resulting instructions will likely have
      very different hex codes, and have different registers.
      When you shift, bits that go off the end are lost (they do not get recycled).
      If you were to shift an 8-bit register 8 times to the right, the register
      would effectively be cleared... is that what you were asking?
notes/asm/cpu_simulator.txt · Last modified: 2011/05/18 19:30 by jhammo13