Lab #3:

Building a Simple CPU

Phil Katz, Connie Li, Katie Stockhammer

March 28, 2004


Abstract:

In lab three, we basically implemented Von Neumann's IAS Computer, which we refer to as SSC (Simple Swarthmore Computer). We accomplished this task by first creating a state diagram based on the architecture specified in the lab assignment, then translating this machine into VHDL code using Altera software. Our computer implemented an instruction set that allowed data processing, data storage, data movement, and control for both internal RAM and external I/O device (the dips and LED's on a board).


Architecture and Instruction Set:

As mentioned before, the architecture of our Simple Swarthmore computer followed that of Von Neumann's IAS Machine. Our computer has two memories; an instruction memory and a main memory. All memory accesses go through the MAR (Memory Address Register) and data gets stored temporarily in the MBR (Memory Buffer Register). The PC (Program Counter) stores the address of the next instruction. We used conditional flags to designate overflow/underflow, swap/carry, read/write. We also allowed for input from the dip switches on a board, and output to the LED digit displays. We transformed the 8 digit binary output into a hex output on the display. We derived the instruction set directly from the lab instruction page. The SSC has a fixed length instruction set, where each instruction is 10 bits long. However, the opcodes vary in size. The opcodes fall into 4 categories, which can be seen in the following table:


4 Categories of Opcodes
GroupOpcodesOpcode FieldsAddress/Data field
ALoadX9 down to 87 downto 0
BLoadMX, StoreMX, JumpMX, JumpPlusMX, JumpCarry, JumpOFlow, Add, And, LoadNegMX, SubMX9 downto 54 downto 0
CStoreIO, LoadIO9 downto 5unused 4 downto 0
DInc, Dec, LSH, RSH, Inv, Swap, LoadMMQ, Arithmetic Right Shift, Arithmetic Left Shift9 downto 4unused 3 downto 0


State Machine Diagram and Two Examples of Execution of Instructions:


State Machine Diagram


Example of INC:

F1:
MAR <= PC : The Memory Address Register gets the address of the Next Instruction from the Program Counter
PC <= PC + 1 : The Program Counter is incremented so that it always points to the address of the Next Instruction
temp <= 000000000 : clear the all temporary values for future use
state <= F2 : move to the next state
F2:
flags(2) <= '0' : Set flag to read so that we do not accidentally write to some memory location we need
IR <= MBR : The Instruction Register gets the next instruction
state <= F3 : move to the next state
F3:
since IR( 9 downto 4 ) = 110010, substate <= INC -- set substate from opcode
state <= E1 : move to next state
E1:
temp <= acc + 1 : a temporary variable one bit longer than the accumulator gets the value of the accumator plus one
state <=E2 : move to the next state
E2:
acc <= temp( 7 downto 0 ) : the accumulator gets the first 8 bits of the temporary variable
if temp( 8 ) = 1 then flags( 0 ) <= 1 : checks for overflow in the temporary variable - if there is, the overflow flag is set
state <= F1 : return to the first fetch state for the next instruction



Example of ADD:

F1 and F2:
-- same as INC
F3:
since IR( 9 downto 5 ) = 00110, substate <= ADD -- set substate from opcode
state <= E1
E1:
MAR <= IR( 4 downto 0 ) : the Memory Address Register gets the address of the operand
state <= E2 : move to next execute state
E2:
flags( 2 ) <= 0 : set read/write flag to read so that the dMBR gets the value of th operand from RAM
E3:
temp <= ( 0 + acc ) + ( 0 + dMBR ) : temp is one bit larger than the accumulator and
dMBR - temp gets the sum of them
MBR <= dMBR
state <= E4 : move to next state
E4: --- check for overflow
	 flags(0) <= temp(8); acc <= temp (7 downto 0);    
			--if temp(8) = 1, then we have  negative overflow
			if acc(7) = '0' then
				if dMBR(7) = '0' then
					if temp(7) = '1' then
						flags(0) <= '1'; 	
						--if both are positive but  result has 1 in high bit,							--then we have positive  overflow
			state <= F1 : return to first state

Memory and I/O Modules:

We implemented the memory modules using the lpm_rom and lpm_ram_dq functions provided in VHDL. Both RAM and ROM were designed as unclocked circuits. The ROM represents the instruction memory and is 32 lines of 8 bits each. The RAM is main memory and is 32 lines of 10 bits each. The ROM is loaded from the rom.mif file that we wrote. The ouput of the ROM is a 10 bit variable which is passed to the iMBR and the output of the RAM is an 8 bit variable which is passed to the dMBR.The RAM also has a one bit variable input so that the ssc can either read or write to main memory. Addresses are read from the MAR.

The I/O or general input for the CPU is the set of DIP switches, which is 8 bits. When the switch is pushed down, the input is 0, otherwise it is 1. The ouput of the CPU is a 16 bit variable which is goes directly to the I/O light display. Since the output of the data is 8 bits, we had the output display two hexadecimal digits so that the hexadecimal digit represented by the lower four bits is displayed by the digit on the right and the other is represented by the digit on the left. If the ouput is a negative number, the decimal sign of the left digit is lit. We also used the clock divider from lab 1 to slow down the signal. We had the CPU as the top level of our program and port mapped in the clockdivider.


Our Code:

Submitted as unlinked on the lab submission page.


Our Test Programs:

Submitted as unlinked on the lab submission page.


Simulation Results:

Please see printouts submitted directly to Dr. Bruce Maxwell.


Extensions:

We wrote an assembler (it is on the unlinked code page).

We implemented a multiplication algorithm in our test programs section

We implemented 4 new instructions: LoadNegMX, SubMX, ArithmeticRightShift(ARSH), ArithmeticLeftShift(ALSH.
LoadNegMX - loads the negative of the contents of the data memory at location x into the acc.
SubMX - Subtracts the contents of memory location x from the accumulator and puts the result back into the acc.
Arithmetic Left/Right shifts - Shift contents of acc, while preserving the sign bit.