Skip to content

Frog: A Calculator that can load data into registers

Before executing a program in Fish, the programmer has to do a two-step preparation:

  • He has to put his program into memory
  • He has to put his data into registers.

and subsequently the program will be executed instruction by instruction by the arriving clock edges.

In Frog, we want to get rid of the second step of this preparation, ie, “put the data into registers” step. Instead, we want to put both the data and the program into the memory. Then, the program will load the required data from memory into the registers as it executes. For this, we need a new type of instruction, called “load immediate” or LDI instruction. And, in order to distinguish this new type of instruction from the previous instructions, we need a new field in our machine code, called the “opcode field“.

1. Instruction Set

In Fish, we only loaded instructions from the memory to CPU, and the data is directly loaded into registers at the start of the program . The data was 16 bit numbers, and instructions required only 9 bits. Hence we make our CPU registers 16 bits wide and memory locations 9 bits wide.

In frog, things have changed. Both data (ie, 16 bits wide) and instructions (9 bits wide) are loaded from memory into CPU. Therefore, memory locations must be widened to 16 bits to accommodate data.

As Frog has two different instructions, it needs a single bit for the opcode field. But we will allocate four bits, instead of one bit as opcode, because in future we plan to increase the number of instructions up to 16.

1.1 ALU Instruction format

ALU instructions of frog have the same fields with fish, except the opcode field, which is new.

Note that we expect an instruction to fit into a memory location, which is 16 bits. Assume that we are using 8 registers for frog. In this case, each of the Src1, Src2 and Dst fields will take three bits, which will make 9 bits in total. If we add AluOp (4 bits) and Opcode (4 bits) on top of that, the total instruction length will become 17 bits. This will not fit into a single memory location, which is a problem..

Possible solutions are

  1. Reduce Opcode width to 3 bits. Downside: This will restrict the future expansion of our instruction set, as in this case we can have at most 8 instructions.
  2. Reduce AluOp fields to 3 bits. Downside: This will reduce the number of available ALU operations to 8.
  3. Increase the register and memory widths to 17, and use 17-bit wide data. Downside: the processor will be highly unconventional, as the memory and register widths are standardized to 8, 16, 32 bits..
  4. Use two consecutive memory locations for each ALU instructions. Downside: Two memory locations contain 32 bits, and we will use 17 of them.
  5. Reduce the width of src1, src2 and dst fields from 3 to 2 bits. Downside: This will reduce the number of registers from 8 to 4.

We choose solution number 5. Why? Because when we investigate reptile we will introduce a trick which allows us to have 8 registers while still keeping the opcode and aluop fields 4 bits wide and the instruction 16-bits long.

Also note that the order of fields are changed. This is for making you remember that when designing an instruction set order of fields in an instruction is not important..

1.2 LDI Instruction Format

2. Finite state machine and Control Unit.

In Fish, we had only one instruction: the ALU instruction. In Frog, the number of instructions is increased to two: ALU instruction and LDI instruction. Hence, when we bring down an instruction from memory into the CPU, it cannot be executed instantly, as in fish. First, it must be stored in some register and its opcode field has to be inspected to determine if it is a LDI or ALU instruction. In our design, this is achieved in the following way:

  • The opcode field of the instruction is stored in the control unit (cu in the FSM diagram below)
  • The rest of the instruction bits is stored in a special puropse register called the instruction register.

Next, frog must choose an appropriate course of action for each kind of instructions.

3. Architecture

Let us first draw the CPU without its control unit:

As can be seen from the circuit diagram, at the core of the frog processor is a fish processor. On top of fish, there is a new register, a new multiplexer, and some new connections:

  • In fish, we had a single type of instruction:

MicroCode

Address FETCH IRLD MUX LD PCINC
0x0 1 1 0 0 0
0x1 0 0 0 1 0
0x2 0 0 1 1 1
Adress Data
0x0 0x18
0x1 0x02
0x2 0x07

4. Verilog Design

Realizing Frog will be our first project in Verilog. We will take this opportunity …

4.1 How NOT to write the Frog in Verilog

When realizing a circuit idea in Verilog, students generally follow the following steps:

  1. Draw a schematic of the circuit
  2. Write a Verilog module corresponding to every major block in the schematic
  3. connect all these modules within a top module to generate the circuit they draw.

When applied to frog, this will result in approximately the following:

  1. Draw the schematic of Frog, which is Fig. …
  2. Write modules for register bank, ALU, memory and control unit, etc..
  3. Join all these modules on a top CPU module.

So, the resulting Verilog code will look like:

 

This is a very suboptimal way of writing a Verilog code, akin to programming in assembly language. The resulting code will not be necessarily wrong, but it will be very long, inefficient and very hard to debug.

4.2 How to write the Frog in Verilog

A better way of writing verilog is not to start from the schematic, but from the finite state machine which gave rise to that schematic.

 

As can be seen, there is no control unit, no register bank etc in this version of the program. All these low level details are expected to be inferred by the Verilog compiler. In other words, the programmer is only required to express the FSM in Verilog. It is the compiler’s duty to construct the schematic from this FSM. And, as compilers are much better in optimization compared to humans, the resulting circuit is likely be more efficient.

5. Assembler for Frog

Assembler with frog is almost the same with the assembler for Fish. Only the code to assemle the LDI instruction is added.

 

6. Key Concepts

  • LDI instruction
  • Opcode field
  • Instruction register
  • Control unit

7. Problems

  1. Add an instruction which performs immediate ALU operations. With this instruction, do we still need the LDI instruction?
  2. In verilog, connect the output of register 0 to a 7-segment display.
  3. This is an extension of Problem 2. Add two DIP-switches to your design. The 7-segment display will show the output of any register depending on the switch configuration.

Questions

Your email address will not be published. Required fields are marked *