Our first instruction

A computer executes programs by following instructions. The instructions belong to an an instruction set. As mentioned in Chapter Welcome, we will use a subset of the OR1K instruction set as the instruction set for our computer.

As a first step, we will try to build a computer with only one instruction. Although somewhat restricted, this computer will be able to

  • Read instructions stored in a memory
  • Decode each instruction
  • Take action, based on the instruction read - in this case, it means that the computer will store a value in a register

We will start with deciding on a program to run our our computer. The program will be stored in a memory, and its instructions will be read, one by one, and actions will be taken.

A program

Our computer needs a program in order to run. Before creating the program, we select an instruction to use.

From the OpenRisc Architecture page we can find the OpenRISC 100 Architecture Manual.

We look in the OpenRISC 100 Architecture Manual, and we find the instruction l.movhi rD, K on page 81. This instruction takes a 16-bit value K, and shifts it left by 16-bits, and then places the resulting value in the register rD.

Read more

Addressing a memory

We need to put our instructions in memory.

Design a memory where the program can be stored. A memory implementation in Verilog is shown in Figure 11.

module memory(clk, write_enable, address, data_in, data_out);

   parameter address_width = 32;
   parameter data_width = 32;
   parameter size = 256;

   input clk;
   input write_enable;
   input [address_width-1:0] address;
   input [data_width-1:0] data_in;
   output[data_width-1:0] data_out;

   wire clk;
   wire write_enable;
   wire [address_width-1:0] address;
   wire [data_width-1:0] data_in;

   reg [data_width-1:0] memory [0:size-1];

   initial begin
     $readmemh("memory_contents.txt", memory);
   end

   always @(posedge clk) begin
     if (write_enable == 1) 
       memory[address] <= data_in;
   end 

   assign data_out = memory[address];

endmodule    

Figure 11. A memory in Verilog.

This the Verilog view - other views are VHDL - SystemC-TLM

Create a pc that reads addresses expressed in bytes. Meaning that it increments itself with four for each instruction read. A program counter implementation in Verilog is shown in Figure 12.

module pc(clk, pc_out);

   parameter pc_width = 32;

   input clk;
   output[pc_width-1:0] pc_out;

   wire clk;
   
   reg [pc_width-1:0] pc_value = 'b0;

   always @(posedge clk)
     pc_value <= pc_value + 4;
    
   assign pc_out = pc_value;

endmodule    

Figure 12. A program counter in Verilog.

This the Verilog view - other views are VHDL - SystemC-TLM

Read more

Decoding the instruction

We must interpret the instruction. We must take actions, in the form of writing parts of the instruction - the K value - into a register. We make a register bank with a destination register selection, and a data input, and a write enable (feels good to have that).

Read more

Running the program

Here we wire the pieces together, and create a functioning computer, albeit with only one instruction! More to come, continue reading!

Read more