Visit MythSim.org to download the mythsim simulator. Note that you will need to have Java installed. The mythsim site has more information on this. Also take a look at the manual section at the mythsim site.
Once mythsim has been installed, you will have a file called mythsim.jar. Selecting this file will launch Mythsim, the Java-based simulator. In the initial window, select the File menu to load files. To run any "program" you must have two files, a microcode file and a memory file. The default extensions for these are .ucode for the microcode file, and .mem for the memory file. Actually any text files will work. Be sure to first load the microcode file, and second load the memory file, otherwise you will get a screenful of errors. The format of these two files is described further below.
Once these files are loaded, you will want to select Plugins to watch the execution. You can watch the datapath, observe line by line as the microcode is executed, and watch line by line as the contents of main memory is retrieved and used.
Mythsim allows us to invent an assembly-language of our choosing, and then implement this assembly language within our simulated CPU. Typically the assembly language we create will have elements that any assembly language would have in it, such as add, subtract, logical operations (and, or, not, xor), shift, multiply, memory read, and memory write. We could, however, come up with unique instructions, such as ADD3 ri; which would implement ri = ri + 3; (ADD3 ri is of questionble usefulness.)
By doing this we learn the nuts-and-bolts of how to control the datapath using a control unit of our own devising, which in turn should give a greater appreciation of programming language implementation and efficiency issues. This also helps us to understand how a computer actually works, "under the hood."
Note that the microprogrammed control unit could instead be equivalently rendered as a hard-wired control unit. The hard-wired control unit would be faster, however less flexible.
As mentioned above, each "program" is made up of two text files, the microcode file and the memory file. The microprogram controlling how the CPU behaves is comes from the microcode file. The program to run, essentially an assembly language program, comes from the memory file. The microprogram implements the instruction set used in the assembly language program.
Microcode
A sample microcode file is:
// ========= FETCH =========
fetch0: a_sel=7, b_sel=7, alu_sel=AND, r6_write, mar_sel=LOAD;
fetch1: a_sel=6, c_in, alu_sel=ADDA, r7_write, ir0_sel=LOAD, read, if wait then goto fetch1 endif;
fetch2: a_sel=7, b_sel=7, alu_sel=AND, r6_write, mar_sel=LOAD;
fetch3: a_sel=6, c_in, alu_sel=ADDA, r7_write, ir1_sel=LOAD, read, if wait then goto fetch3 endif;// ========= OPCODES =========
switch: goto opcode[IR_OPCODE];
opcode[0]: goto fetch0; // no_op
opcode[1]: ri_sel, rj_sel, rk_sel, alu_sel=ADD, goto fetch0; // add (ri <- rj + rk)
opcode[2]: result_sel=IR_CONST8, ri_sel, goto fetch0; // load immediate (ri <- ir_const8)
opcode[3]: goto opcode[3]; // halt
Notes:
- Comments begin with //
- labels are followed by a colon ':' as a delimiter. It is not necessary to have a label on every line.
- Items within each instruction (after the label) are separated by commas ','. A semi-colon ';' is used as the end of instruction delimiter.
- Items listed in each instruction represent setting that particular control line to 1. So for instance
opcode[1]: ri_sel, cin;
would represent setting the ri_sel control line and setting the cin control line to 1. All lines not listed are assumed to be 0.- Blank lines between instructions are allowed.
- A typical microprogram will have three sections:
- The instruction fetch portion, usually about 4 lines (as shown above)
- The opcode selection portion, where each opcode will correspond to exactly one line. This is implemented as a jump table, where the opcode retrieved from the instruction is the offset from the current PC value.
- The opcode extension section. Operations that require more than a single microinstruction must have the remaining instructions given below the opcode jump table. Typically once each instruction is completed it will then branch back up to the beginning of instruction fetch again (e.g. fetch0). There is no opcode extension section in the above example, as all opcodes can be implemented in a single microinstruction.
- The halt instruction is implemented as an unconditional branch to itself.
- Each microcode instruction can be represented as binary values, where each control line that has been set is a 1, and the rest are 0. This can be seen in the MicroMemory Display Plugin within Mythsim.
Memory
A sample memory file corresponding to the above microprogram file is:
Top section of program before the % can be used for program comments. Sum immediate constants into r0. % // Begin code section // LOAD_IMMEDIATE r0<- 3 0: 3 // values can be decimal.. 1: 000010 00 // .. or binary // LOAD_IMMEDIATE r1<- 2 2: 2 3: 000010 01 // ADD r0 <- r0 + r1 4: 00 01 0000 // binary values can have spaces 5: 000001 00 // HALT 10: 0 // Should now have 5 in r0 11: 000011 00 // End
Notes:
- Comments can be written at the top of the file before the %, which marks the beginning of the code.
- In-line comments can be given using //
- Lines of code must have sequential line numbers given as labels, beginning with the first line at 0. Labels must be followed by a colon ':' as a delimiter.
- Instruction values may be given as either decimal or binary values. Binary values can include spaces to make them more legible.
- Instructions are 16 bits, however memory is byte-addressable, so it takes 2 bytes to make a single instruction. The low-order byte is given first in memory, then the high-order byte. This means that if in memory at addresses 4 and 5 we have the bytes:
00010000
00000100
then the corresponding instruction is:
00000100 00010000
Where the right-most bit is the least-significant bit. The fact that each instruction is comprised of 2 bytes is why each instruction is shown as a pair of values, where each value is stored in a single byte.- Blank lines are allowed.