The Minimal Instruction Set Computer (MISC) in Java

45
The Minimal Instruction Set Computer (MISC) in Java 1

description

The Minimal Instruction Set Computer (MISC) in Java. MISC is a Java simulation of a simple CPU The architecture makes use of 4 byte words - PowerPoint PPT Presentation

Transcript of The Minimal Instruction Set Computer (MISC) in Java

Page 1: The Minimal Instruction Set Computer (MISC) in  Java

1

The Minimal Instruction Set Computer (MISC) in Java

Page 2: The Minimal Instruction Set Computer (MISC) in  Java

2

• MISC is a Java simulation of a simple CPU• The architecture makes use of 4 byte words• In the simulation the contents of a register as

well as the contents of a byte in memory are modeled by an object containing a character array of 8 bytes

Page 3: The Minimal Instruction Set Computer (MISC) in  Java

3

• Each bit is then modeled by the presence of either the character ‘1’ or the character ‘0’ in a particular position in one of these 8 byte arrays.

• The registers are packaged together in an array named “reg”. The index of the array identifies the particular register.

Page 4: The Minimal Instruction Set Computer (MISC) in  Java

4

• Registers:

• register name decimal index binary code• identification in reg array of index• • unused reg[0] "00000000"• • general purpose• • A reg[1] "00000001"• B reg[2] "00000010"• C reg[3] "00000011"• D reg[4] "00000100"•

Page 5: The Minimal Instruction Set Computer (MISC) in  Java

5

• Registers, cont’d.:

• register name decimal index binary code• identification in reg array of index

• memory offsets• • codeoffset reg[5] "00000101"• dataoffset reg[6] "00000110"• unused1 reg[7] "00000111"• unused2 reg[8] "00001000"• unused3 reg[9] "00001001"• • flag reg[10] "00001010"•

Page 6: The Minimal Instruction Set Computer (MISC) in  Java

6

• Registers, cont’d.:

• register name decimal index binary code• identification in reg array of index

• control unit registers• • instruction reg[11] "00001011"• operand1 reg[12] "00001100"• operand2 reg[13] "00001101"• extra reg[14] "00001110"•

• ALU registers• • aluinreg1 reg[15] "00001111"• aluinreg2 reg[16] "00010000"• aluoutreg reg[17] "00010001"

Page 7: The Minimal Instruction Set Computer (MISC) in  Java

7

• The memory is also implemented in the simulation as an array

• Each element of the array is a machine word• The index of the array represents the offset into

the memory, counting by 4 byte words.• • Memory: array name•

memory[]

Page 8: The Minimal Instruction Set Computer (MISC) in  Java

8

General Remarks on Machine Instruction Execution

• The general rules for both move and arithmetic instructions are these:– A register or a memory variable can be a

destination.– A constant, a register, or a memory variable can be

a source.– Memory to memory operations are not allowed.

Page 9: The Minimal Instruction Set Computer (MISC) in  Java

9

• After a program is loaded the machine takes control of execution (by means of the takeControl() method called from the Osystem)

• The machine steps through the contents of the code segment until it encounters an empty (“00000000”) instruction byte

Page 10: The Minimal Instruction Set Computer (MISC) in  Java

10

• Execution starts with the value 0 in the code offset register and takes the contents of 4 contiguous bytes of code memory and puts them into the instruction, operand1, operand2, and extra registers, reg[11], reg[12], reg[13], and reg[14], respectively

• After the retrieval of each instruction and before its execution, the code offset is incremented for the next retrieval.

Page 11: The Minimal Instruction Set Computer (MISC) in  Java

11

The Machine Instruction Set

• The MOVE Instruction• assembly instruction method in simulation machine instruction• MOVE register, register void moveDestRegSrcReg() “10000001”• MOVE memory, register void moveToMemFromReg() “10000010”• MOVE register, memory void movetoregfrommem() “10000011”• MOVE memory, constant void movetomemfromconst() “10000100”• MOVE register, constant void movetoregfromconst() “10000101”

Page 12: The Minimal Instruction Set Computer (MISC) in  Java

12

• The ADD Instruction• assembly instruction method in simulation machine instruction • ADD register, register void addDestRegSrcReg() “10000110”• ADD memory, registervoid addToMemFromReg() “10000111”• ADD register, memoryvoid addToRegFromMem() “10001000”• ADD memory, constant void addToMemFromConst() “10001001”• ADD register, constant void addToRegFromConst() “10001010”

Page 13: The Minimal Instruction Set Computer (MISC) in  Java

13

• The SUB Instruction• assembly instruction method in simulation machine instruction • SUB register, register void subDestRegSrcReg() “10001011”• SUB memory, register void subFromMemSrcReg() “10001100”• SUB register, memory void subFromRegSrcMem() “10001101”• SUB memory, constant void subFromMemSrcConst() “10001110”• SUB register, constant void subFromRegSrcConst() “10001111”

Page 14: The Minimal Instruction Set Computer (MISC) in  Java

14

• The JUMP Instruction• assembly instruction method in simulation machine instruction • JMP unsigned integer void jumpUnconditional() “10010000”• JPOS unsigned integer void jumpOnPositive() “10010001”• JNEG unsigned integer void jumpOnNegative() “10010010”• JZERO unsigned integer void jumpOnZero() “10010011”• JOVER unsigned integer void jumpOnOverflow() “10010100”

• • The unsigned integer parameter in operand1 is to be

interpreted as an offset into code memory• It has to be treated as unsigned, and in order to work

correctly it has to fall on a 4 byte instruction boundary

Page 15: The Minimal Instruction Set Computer (MISC) in  Java

15

General Remarks on the Form of Machine Language

• In a line of machine language code the instruction comes first, followed by the destination operand, followed by the source operand

• This is followed by an extra space which does not yet have a designated use

• If the line of code contains a data declaration rather than an instruction, the first item will be the initial value, and the remaining three spaces will be unused

Page 16: The Minimal Instruction Set Computer (MISC) in  Java

16

• The data portion of the program file comes first and then the program code itself

• The program will be loaded at offset 0, data first, code second

• Words 0-7 are reserved for data variables and will be filled with 0’s if there are not that many variables

Page 17: The Minimal Instruction Set Computer (MISC) in  Java

17

• That means that a single program can have a maximum of 8 memory variables

• The code segment of a machine language program begins at offset 8

• Jump instructions in the code will have to be written with operands incremented by 8.

Page 18: The Minimal Instruction Set Computer (MISC) in  Java

18

• There is also an upper limit on the total size of a program

• No program, including data, can be longer than 31 lines

• The source file signals termination with a row of asterisks

Page 19: The Minimal Instruction Set Computer (MISC) in  Java

19

• Inside the machine, the takeControl() method stops if it encounters an instruction which is all zeros

• In memory, a program has to be followed by at least one word where the first byte is zeros

• If need be, this will be the 32nd line, meaning a maximum of 31 lines for a program, or up to 8 variables and up to 23 lines of code

Page 20: The Minimal Instruction Set Computer (MISC) in  Java

20

An Example Machine Language Program

• The example is a machine language program that sums the first 10 integers

• The machine language alone with artificial line breaks and segment labels follows

• The *’s are used on input to detect the end of the program.

Page 21: The Minimal Instruction Set Computer (MISC) in  Java

21

• data segment• • 00001011000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000• 00000000000000000000000000000000

Page 22: The Minimal Instruction Set Computer (MISC) in  Java

22

• code segment• • 10000101000001000000000100000000• 10000111000000010000010000000000• 10001010000001000000000100000000• 10000011000000110000000000000000• 10001011000000110000010000000000• 10010001000010010000000000000000• ********************************

Page 23: The Minimal Instruction Set Computer (MISC) in  Java

23

The Example Program Data Segment with Assembly Language Guide

• /.DATA///

• 00001011 00000000 00000000 00000000 • /LOOPLIM/X0B//• loop limit offset 0, value 11• • 00000000 00000000 00000000 00000000• /ACCUM/X00//• accumulator offset 1, value 0

Page 24: The Minimal Instruction Set Computer (MISC) in  Java

24

• Registers can only contain 8 bits, so memory variables are limited to 8 bits

• Memory variables have to occur on word boundaries in order to be addressable

• Therefore, 3 bytes are wasted for every variable• Only the live code is shown below.• The 6 additional lines of 4 groups of 8 zeros which

are part of the source file are not repeated.

Page 25: The Minimal Instruction Set Computer (MISC) in  Java

25

The Example Program Code Segment with Assembly Language Guide

• /.CODE///

• 10000101 00000100 00000001 00000000 • /MOVE/D/X01/• move reg D, const 1• movetoregfromconst 4, 1• • /.LABEL/LOOPTOP//• • 10000111 00000001 00000100 00000000• /ADD/ACCUM/D/• (LABEL) add data offset 1, reg D• addtomemfromreg 1, 4

Page 26: The Minimal Instruction Set Computer (MISC) in  Java

26

• 10001010 00000100 00000001 00000000• /ADD/D/X01/• add reg D, 1• addtoregfromconst 4, 1• • 10000011 00000011 00000000 00000000• /MOVE/C/LOOPLIM/• move reg C, data offset 0• movetoregfrommem 3, 0

Page 27: The Minimal Instruction Set Computer (MISC) in  Java

27

• 10001011 00000011 00000100 00000000• /SUB/C/D/• sub reg C, reg D• subtractdestregsrcreg 3, 4

• 10010001 00001001 00000000 00000000 • /JPOS/LOOPTOP//• Since space is reserved for 8 variables, the first instruction comes at word 8.• jump on positive to “LABEL”• jumponpositive 9• • ******** ******** ******** ********• /.END///

Page 28: The Minimal Instruction Set Computer (MISC) in  Java

28

Running the Simulation and Using the Operating System Commands

• The simulation consists of 4 java files: MachineWord.java, Machine.java, Osystem.java, and MachineOSProgram.java

• Assuming all of the files are in the same directory, compiling and running MachineOSProgram.java will set MISC in motion

• When it is running, it presents a simple command line prompt in a DOS window. The operating system has only 3 commands, rpf, dmc, and exit

Page 29: The Minimal Instruction Set Computer (MISC) in  Java

29

rpf

• = run program file• Upon entering this command the user is prompted

for the name of the program (machine language) file to run.

• Note that machine language files have to be simple text files and that when prompted for the file the O/S expects a name with the .txt extension

• It will seemingly “accept” files without the extension, but it will not work correctly

Page 30: The Minimal Instruction Set Computer (MISC) in  Java

30

dmc

• = dump machine contents• Upon entering this command the user is prompted for

the name of the output file to create• In this file the system will put the contents of the

machine after a program run• Notice that this operating system in effect has no I/O

capabilities. You only know what the program did by looking at the hardware contents afterwards.

• Note that the output file specified should also be a text file with a .txt extension.

Page 31: The Minimal Instruction Set Computer (MISC) in  Java

31

exit

• = quit the simulation. (Technically this isn’t even really a command…)

• Note that a text file named “showfile” may show up in the directory where you run the simulation. This is simply a debugging tool

• It shows the contents of the machine at a certain point in a run

• This is caused by a call to the showStuff() method• The call can be placed at various locations in the

simulation code to capture the machine’s contents at that point.

Page 32: The Minimal Instruction Set Computer (MISC) in  Java

32

A Summary of the Structure of the Java Simulation by Class, Constructor, and Method

• Listed below are the component classes that make up the simulation

• Complete html documentation for the simulation code is available

• In this summary, important information is emphasized without exhaustively commenting on all aspects of the classes or mentioning all instance variables, constructors, or methods of the classes.

Page 33: The Minimal Instruction Set Computer (MISC) in  Java

33

MachineByte

• This is a container for an array of 8 characters• Each character is either a 1 or a 0, so this

represents a byte in the machine simulation

Page 34: The Minimal Instruction Set Computer (MISC) in  Java

34

MachineWord

• This is a container for an array of 4 MachineByte objects

• In the machine architecture 1 addressable word equals 4 bytes

Page 35: The Minimal Instruction Set Computer (MISC) in  Java

35

Machine

• This is the heart of the simulation and its contents can be broken down into several categories

• As explained in greater detail above, the hardware of the machine, its registers and memory, are simulated by elements of arrays of the necessary type

• These are declared and constructed in Machine

Page 36: The Minimal Instruction Set Computer (MISC) in  Java

36

• Machine has a general purpose method that may be useful for debugging, showStuff()

• This shows the complete contents of the machine, including the registers

• This method exists “on the side” and can be used to figure out what is going on with the simulation

• It is not intended for use as part of your solution to a programming assignment, except as a debugging tool

Page 37: The Minimal Instruction Set Computer (MISC) in  Java

37

• Machine has some special purpose methods, which do not support general machine language instructions

• Instead, they can be used by the Osystem to do I/O. They are:

• loadWordToOffset()• getWordFromOffset()

Page 38: The Minimal Instruction Set Computer (MISC) in  Java

38

• Machine has some methods which contain the logic for executing a machine language program. These are:

• totalReset()• resetOffsets()• takeControl()

Page 39: The Minimal Instruction Set Computer (MISC) in  Java

39

• takeControl() is the most fundamental of the execution methods

• It is called by the Osystem after a program is loaded

Page 40: The Minimal Instruction Set Computer (MISC) in  Java

40

• This method contains the built-in logic of – incrementing the codeoffset register– checking the contents of that location in memory – and executing the method that implements the

machine language instruction corresponding to the binary code found there

Page 41: The Minimal Instruction Set Computer (MISC) in  Java

41

• Machine has methods that implement the machine language move, add, subtract, and jump instructions

• It also has some helper methods that support arithmetic

• One helps with integer arithmetic when the machine contents are in binary form

• Another sets the flag register to agree with the outcome of an arithmetic operation

Page 42: The Minimal Instruction Set Computer (MISC) in  Java

42

Osystem

• This has a constructor in which a copy of the Machine is constructed

• It also contains two methods:– runProgramFile()– dumpMemoryContents()

• runProgramFile() loads a program from an external file and turns execution over to the machine

• dumpMemoryContents() stores the current contents of the machine’s memory into an external file

Page 43: The Minimal Instruction Set Computer (MISC) in  Java

43

MachineOSProgram

• This is a program containing a main() method• It is the simulation driver• In it a copy of the Osystem is constructed• The rest of the program is basically a loop which

prompts and checks to see whether the user is entering Osystem commands and file names to go with them

• It also supports an exit command, which is not an Osystem command, but the input which causes the MachineOSProgram to stop looping.

Page 44: The Minimal Instruction Set Computer (MISC) in  Java

44

MyTerminalIO

• This is just my implementation of a simple class that supports input to a program running in a command prompt.

Page 45: The Minimal Instruction Set Computer (MISC) in  Java

45

The End