UnicMinds

Processors for Kids

How to make a Program Counter using Multiplexers?

A program counter is an important component inside a processor. And to build a program counter, we will need a multiplexer, register and a ripple-carry adder. Let’s understand them one by one. 

A multiplexer is a circuit of logic gates. This circuit is what helps the CPU/processor to fetch a bit of data from memory at a given memory address. A processor has many multiplexers that control the data and address buses. 

If the selector pins have the address “00” then the input from A is sent as output to X. If the selector pins have the address “01” then the input from B is sent as output to X, and so on.

making a program counter

A multiplexer is nothing but a set of inputs connected to an output and depending on the selectors (addresses in the selector pins) one of these inputs are connected to the output. So, if you want to read 4 inputs then you will need four addresses and hence you will need 2 bits as selectors. If you want to read 8 inputs then you will need 8 addresses and hence you will need 3 bits (selectors). Similarly, if you want to read 2^N inputs then you’ll need N selector bits (basically addresses of the inputs) and you will get 2^N outputs which are the values in these addresses.

The buses that contain the inputs are called the data buses and the buses that contain the address (selectors) are called the address buses. The more wires (N) in the address bus, the more address locations (2^N) can be reached. If there are 32 wires (4 byte addresses), then 2^32 memory locations can be reached (4GB RAM).

Coming to our project, we have a multiplexer (MUX) that allows us to access a bank of memory, we have a ripple carry adder circuit that we made in the previous post, and we need a register (made of flip-flops) that can temporarily store some information. 

A register is nothing but a group of Flip-Flops that each stores 1 bit.

Start the register at 0 and increment it by 1 using the ripple carry adder. Now, for each of the register increments do the below.

  1. Fetch one instruction from memory using MUX
  2. The instruction is nothing but a pattern of bits which are decoded and the instruction is understood. This circuit is called a decoder. This is also another multiplexer itself.

There you have a Program Counter. A program counter is a register in a computer processor that contains the address (location in RAM/main-memory) of the instruction being executed at the current time. As each instruction is fetched, the program counter increases its stored value by 1. After each instruction is fetched, the program counter points to the next instruction in the sequence. When the computer restarts or is reset, the program counter normally reverts to 0. Some engineers refer to a program counter as an instruction address register or an address pointer.

CPU and memory for kids

A basic CPU configuration has Program Counter as a key component amongst other components such as ALU, MDR, CU, IR, MAR, Accumulator and Program Counter. A brief description of what all entails inside a processor today is as below.

Arithmetic Logic Unit (ALU) performs all arithmetic and logical operations on the data in which the necessary data is being supplied by the accumulator and other registers. 

Memory Data Register (MDR) acts as buffer between CPU and main memory. Register unit stores intermediate data used during the execution of the program. 

Control Unit (CU) supervises the transfer of information among the registers and the ALU and has to tell which operation has to be performed. Control units are responsible for fetching instructions, decoding the OPCODEs and providing control signals for all CPU actions. It controls all the internal operations of the CPU and decides which work is to be performed by each unit. 

Accumulator plays an important role being used to store an input or output operand during execution of the instructions. Operand is the data to be operated by the computer.

Program Counter is used to store the address of the next instruction which is to be executed. 

Instructions Register (IR) is used to hold the OPCODE of the current instruction. OPCODE specifies the given task to be performed by the computer. 

Memory Address Register (MAR) is used to store the address field of the current instruction or the data to be fetched to the CPU. 

Hope this is useful, thank you.

You may like to read: How Coding & Math Are Complementary?, Coding Rock-Paper-Scissors Game in Python, and Mario Game in Scratch.

BOOK A FREE TRIAL