How CPU 1001 works?

Structure of the processor

This machine is a 32-bit processor with 10K bytes of memory. It has 32 Registers (each one is a 32-bit register), an Arithmetic Logic Unit, an Accumulator (to keep the result), a Program Counter and finally a control unit. 

Registers are used to keep the information (a kind of fast memory). They are the input feeding into Arithmetic Logic Unit (ALU). ALU is the main "computer".  It performs basic operations such as Add, Compare, Jump etc.  The result of an operation is kept in Accumulator (AC).  The logical operation (such as compare) affects a "Flag" which stored the result as 1/0 (or True/False). This flag is used in subsequent instruction to make decision. Control Unit is the "conductor" of the machine.  It directs all units to perform in harmony to achieve the desired operation. 

The processor "executes" machine code (program).  The machine codes are composed of "basic" operations.  We can write a program with these basic operations to achieve the result just like programmers write programs in a high level language (such as Java or C++).  The difference is that machine codes are more primitives hence it  takes many more instructions to get the same result as a few lines of Java.

The execution cycle

It goes like this:

1)  Read an instruction from the memory (where it stored the program) at the location specified by the Program Counter (or PC for short).
2)  Control unit decodes the instruction.
3)  Execute that instruction.

Then the PC is advanced by one to the next instruction and the cycle 1 to 3 are repeated.

What an instruction looks like?

An instruction composed of two parts: the first part is "operation code"; the second part is "the operand".  The operation code denotes the action such as "add", "mov".  The operand part contained the information used of the action.  For example, the "add" code is followed by the number of register "r1".  This means the value in the register number 1 is added with the accumulator (AC).  "mov" is followed by a register number as well. "mov r2" means get the value from the register number 2 and put it to AC. We will see more type of operand when other kind of operation codes are introduced.

We can understand the execution cycle of the CPU by seeing how it "runs" a machine code.  Let us write the first simple machine program, adding two numbers together.  Both numbers are stored in registers, let them be r1 and r2.

  mov r1
  add r2
  put r3


where "put r3" stored AC to the register number 3. This program must be translated into machine code first.  Let the encoding of operation code be:  1 mov, 2 add, 3 put.  The above program is written in code as following:

  mov r1     1,1
  add r2     2,2
  put r3     3,3

So, our first program becomes "1,1,2,2,3,3,0".  I put "0" at the end as it is "stop" instruction.

Before we can run it, we need to initialise the values in registers.  We can do that with "mvi" (move immediate value) code. Let its encoding be 4.  Suppose we want to add 10 with 20.

  mvi 10      4,10
  put r1      3,1
  mvi 20      4,20
  put r2      3,2
  mov r1      1,1
  add r2      2,2
  put r3      3,3


You can try it out using my simulator on the web at "www.cp.eng.chula.ac.th/~pjw/project/cpu1001.htm

Use your browser to visit the page and the simulator will pop up, ready to run.  You just cut and paste the machine code into the input box. Hit "run" button and it will execute instructions one-by-one (single step) and display the values of all registers.  Once you observe the results of the action from the program, you will "understand" how a CPU works.  I have tested my simulator on three popular browser: IE11, Chrome and Safari.

Enjoy.