Creating New Machines in CPU Sim

A Tour Using the Wombat 1

To demonstrate how to use CPU Sim to create a new hypothetical machine or CPU, we will outline the construction of the "Wombat1" machine used in the first tour. We will only demonstrate some of the principles of constructing a new machine since completing the whole machine here, especially all the microinstruction and machine instructions, would be somewhat time consuming.

Overview of the Wombat1 architecture

The machine we will now construct, the Wombat1, is a single-accumulator machine using six registers: acc (the accumulator), pc (the program counter), ir (the instruction register), the mar (memory address register), mdr (the memory data register), and status (the status register). The basic structure of the Wombat1 is shown in Figure 4. The status register is 3 bits wide, the pc and mar are 12 bits wide and the other three registers are 16 bits wide. The Wombat1 also has a main memory (RAM) consisting of 128 bytes each individually addressable. The arrows in the figure indicate the movement of data by the microinstructions.

In the Wombat1, the pc stores the address of the main memory location that contains the next instruction to be executed. In the fetch sequence (the first part of every machine cycle), the next instruction to be executed is copied into the ir , where the instruction is decoded. Then the instruction is executed, which completes the machine cycle. This is followed repeatedly by more machine cycles, each consisting again of the execution of the fetch sequence followed by the execution of a machine instruction.

In the Wombat1, all computations are done in the acc . The mar and mdr are the registers through which data is transferred to and from the main memory.


Figure 4. Diagram of data movement in the Wombat1

There are 12 machine instructions for the Wombat1, each associated with a 4-bit opcode. These instructions are: HALT (stop), READ (get input from the user), WRITE (send output to the user), LOAD (transfer data from the main memory to the acc), STORE (transfer data from the acc to the main memory), ADD (add a value from the main memory to the value in the acc, putting the result in the acc), SUBTRACT, MULTIPLY, DIVIDE (all similar to ADD), JMPZ (if the value in the acc is 0, jump to a new location to obtain the next instruction to be executed), JMPN (if the value in the acc is less than 0, jump to a new location to obtain the next instruction to be executed), JUMP (jump to a new location unconditionally).

Editing the Wombat1 components

To create the Wombat1, choose "New machine" from the File menu. The default new machine has no registers, no memory, and no machine instructions.

To edit a machine, it is always best to start at the lowest level, namely the basic hardware components. So choose "Hardware Modules..." from the Modify menu. The dialog box that appears (see Figure 5) allows you to edit the registers, register arrays, condition bits, and memory of a machine. You can add new components and modify or delete existing components. To modify a component, first select the type of the component in the popup menu labeled "Type of Module:" at the top of the dialog. This action causes the table in the center of the dialog box to display the parameters associated with all existing components of that type. These parameters can all be edited. The Wombat1 computer needs the six registers mentioned above, so select "Register" as the type of module and then click the "New" button six times. Finally edit the name and width of each of the six registers so that they match the values shown in Figure 5.

The next piece of hardware to edit is the machine's main memory. To do so, select "RAM" from the popup menu at the top of the Edit Modules dialog, click "New" and set the new component's name to "Main", its length to 128, and its cell size to 8.

Finally, the Wombat1 needs to specify a condition bit for halting and so select "ConditionBit" from the popup menu at the top of the dialog. Click "New" and then set the name of the condition bit to "halt-bit", the register to the "status" register, the bit to 0, and check the "halt" box.


Figure 5. The dialog for editing the hardware modules.

The meanings of all the parameters of the hardware components are described in more detail in the help section The Simulated Hardware Units

Close the Edit Modules dialog box by clicking the "OK" button.

Editing the Wombat1 Microinstructions

Now we need to construct the necessary microinstructions that will be used to implement the machine instructions. There are 7 transfer, 4 arithmetic, 2 test, 1 increment, 1 decode, 2 io, 2 memory access, and 1 set condition-bit microinstructions that need to be created. The 7 transfer microinstructions are displayed by arrows between the registers in Figure 4 above.

To see how to create the transfer microinstructions, choose the menu item "Microinstructions..." from the Modify menu and then choose "TransferRtoR" in the popup menu labeled "Type of Microinstruction" at the top of the dialog that appears (see Figure 6). "TransferRtoR" type microinstructions are for transferring a contiguous set of bits from one register to another register. To create the first transfer microinstruction, click the "New" button. We will edit this first microinstruction so that it transfers the contents of the pc to the mar. Click in the table cell with the "?" in it (in the column headed "name") and type in "pc->mar" (you can give the microinstructions any name you want, but it helps to choose something descriptive). Select the "pc" as the "source" register and the "mar" as the "destination" register, with bit 0 as the start bit for both registers and give numBits the value 12. This means that we want to transfer all 12 bits between the registers. Now this microinstruction is complete.

If you want to create the remaining 6 transfer microinstructions in the Wombat1, continue adding new microinstructions in the same manner (see Figure 6 for a complete list). The microinstructions of the other types needed for the Wombat1 can be created similarly by first selecting the appropriate type of microinstruction in the popup menu at the top of the dialog and then creating the microinstructions of that type. We will not go through the details in this tutorial. When you are finished, close the Edit Microinstructions dialog box by clicking "OK".


Figure 6. The dialog for editing microinstructions.

The detailed descriptions of all the parameters displayed in the microinstruction dialog boxes can be found in the section Specifications of the Microinstructions.

Editing the Wombat1 Fetch Sequence

Once all the microinstructions have been created, the fetch sequence can be constructed. To do so, choose "Fetch Sequence" from the Modify menu, which will bring up a dialog for editing the fetch sequence (see Figure 7). The fetch sequence is a list of microinstructions that are executed at the beginning of every machine cycle. The default sequence is empty. The current fetch sequence list is displayed in the left scroll box entitled "Implementation". To add microinstructions to it, first display the microinstructions of the type you want in the tree of microinstructions in the right side of the dialog by clicking on the dial to the right of the folder corresponding to the desired type. Then highlight the microinstruction you want by clicking on it. (Note: If you realize you need to create or modify a microinstruction before inserting it in the fetch sequence, you don't need to close this dialog; instead you can just double-click on the tree of microinstructions on the right and the dialog for editing and creating microinstructions will appear.) Next, to specify where the new microinstruction is to be inserted into the fetch sequence, click on the microinstruction in the left (Implementation) scroll box before which you want to insert the new microinstruction. Finally, click "<<insert<<". You can reorder the microinstructions in the implementation list on the left by dragging them up or down to a new position.


Figure 7. The dialog for editing the fetch sequence.

To remove a microinstruction from the fetch sequence, click on it in the left scroll box to highlight it, and then click ">>delete".

At the bottom of the list of all microinstructions on the right (not shown in Figure 7), there is a microinstruction named "Comment" shown in italics. When executed, this microinstruction does nothing. Instead, this microinstruction can be used to insert comments into the fetch sequence implementation. To add it, first select it and then press "<<insert<<". A new Comment microinstruction is added to the implementation. You can now edit the comment in the implementation list by double-clicking it (or pressing F2). Comments are colored gray and use an italics font to distinguish them from regular microinstructions.

When you are done, click "OK".

Editing the Wombat1 Machine Instructions

Now the 12 machine instructions of the Wombat1 need to be created. Choose "Machine Instructions..." from the Modify menu. The dialog box that appears (see Figure 8) allows you to edit machine instructions, including the name, opcode, the format of the instruction, and the list of microinstructions that form the execution sequence of each instruction. The name that you specify for a machine instruction is used in assembly language programs to execute that instruction. You specify the opcode in hexadecimal notation. The execution sequence of each instruction is edited using the right side of the dialog box (in the same way the fetch sequence is edited, as described above). First select the instruction you wish to edit on the left and then insert, delete, or move the microinstructions implementing the machine instruction on the right. Figure 8 shows the 12 machine instructions for the Wombat1 CPU.


Figure 8. The dialog for editing machine instructions.

The format of each instruction is specified by a list of fields. For example, the "add" instruction uses two fields: a 4-bit "op" field and a 12-bit "addr" field. The "stop" instruction uses a 4-bit "op" field and a 12-bit "unused" field.

To edit the fields, click on the "Edit fields..." button to bring up a new dialog box (see Figure 9). Each field can have a variety of properties, as shown in the figure.


Figure 9. The dialog for editing the fields of machine instructions.

As in other such dialog boxes, if you wish to create a new field, click "New" and then edit the parameters of that field. When you are finished editing the fields and machine instructions, click "OK" in each dialog.

The detailed descriptions of all the parameters displayed in the machine instruction dialog box and the fields dialog box can be found in the Machine Instructions help section.

The complete Wombat1

At this point, if you had created all the microinstructions, machine instructions and fetch sequence, the machine would be complete. You can at any time go back and change any part of the machine. To save the machine to a file, choose "Save machine" or "Save machine as..." from the File menu. CPU Sim saves the details of the machine as text in an XML file.

To see all the details of the machine in a more user-friendly format than XML, choose "Save machine in HTML..." from the File menu. You will be asked to type in the name of the HTML file into which the data will be saved. This file can be opened and viewed with any web browser. An HTML file displaying the details of the Wombat1 machine is included with CPU Sim in a file named "Wombat1.html".

Now let's see how you would create an assembly language program for the Wombat1. Select "New text" from the Windows menu. The window that appears is a simple text editor window that allows you to type in, save, and print an assembly language program. For example, you could type in the following short program, which reads in two integers and writes out the difference.

   read		;input an integer into the acc
   store x	;store it in the location x
   read		;input an integer
   store y	;store it in location y
   load x	;load x into the accumulator
   subtract y	;subtract y from the accumulator
   write	;output the difference
   stop		;end the program 
x: .data 2 0	;the storage location for x 
y: .data 2 0	;the storage location for y

The assembly language names for the machine instructions are just the names you gave to the machine instructions when you created them. The ".data" statements are pseudoinstructions that tell the assembler to allocate memory for the variables x and y. Each of these data statements causes the assembler to allocate two bytes and initialize them to 0. Anything after a semicolon in a line (up to the end of the line) is a comment that is ignored by the assembler. For more details on the grammar of assembly language programs in CPU Sim, see the section Syntax of Assembly Language.

Once you have typed in an assembly language program, you can save it to a file by choosing "Save text" or "Save text as..." from the File menu. You can also assemble it into machine language and load and run it by choosing the appropriate item from the Execute menu. (Note: If you have not constructed the complete Wombat1 at this time, then you will get an error message if you try to assemble the program above.)

The section Running Programs in CPU Sim describes how you go about loading previously-created machines and programs and running the programs on the machines.