import java.text.*; import java.util.*; import java.io.*; public class STM{ public static final int NUMREGS = 16, // Number of registers RAMSIZE = 65536, // Size of RAM VASPACE = 262144; // 18 bit virtual address space public int NPAGES, // Number of pages NFRAMES, // Number of frames PAGESIZE; // Size of page public int mem[] = new int[RAMSIZE]; // RAM public int regs[] = new int[NUMREGS]; // Registers public int debug = 0; public boolean error = false; public PTE pageTable[]; // Page table public int clock = 0; // Global clock. public int PageFault = -1; // PageFault is the page that has faulted; // -1 if no page fault. // Convert a relative address to a physical address. Check that it // lies within bounds // Note: This is considered part of the "hardware" of the virtual machine. // You may not change it in any of the projects. // Compute the physical address from the virtual address and the page table. // The procedure returns -1 to indicate a page fault. public int physAddress(int reladd, boolean write) { int page, frame; if ((reladd >= 0) && (reladd < VASPACE)) { PageFault = -1; page = reladd / PAGESIZE; frame = pageTable[page].frame; pageTable[page].rbit = true; if (write) pageTable[page].mbit = true; if (debug == 2) { if (write) System.out.println("Writing to page " + page); else System.out.println("Reading from page " + page); } pageTable[page].timestamp = clock; if (frame == -1) { PageFault = page; return(-1); } else return (frame*PAGESIZE + reladd % PAGESIZE); } else { error = true; System.out.println("Virtual address " + reladd + " out of range "); return(0); } } // Display STM instruction for level-2 debugging public void showInst(int op, int ra, int ad, int rb, int rc, int rd) { final String codes[] = // Abbreviations for opcodes { "LOA", "STO", "CPR", "LOI", "STI", "ADD", "SUB", "MUL", "DIV", "ICR", "DCR", "GTR", "JMP", "IFZ", "JMI", "TRP" }; String o = "Instruction 0x" + Integer.toString(regs[0],16) + " : " + codes[op]; switch(op) { case 0: case 1: case 13: o = o + " R" + ra + " Address 0x" + Integer.toString(ad, 16); break; case 12: o = o + " Address 0x" + Integer.toString(ad, 16); break; case 2: case 3: case 4: o = o + " R" + ra + " R" + rb; break; case 5: case 6: case 7: case 11: o = o + " R" + ra + " R" + rb + " R" + rc; break; case 8: o = o + " R" + ra + " R" + rb + " R" + rc + " R" + rd; break; case 9: case 10: case 14: o = o + " R" + ra; break; } System.out.println(o); } // Execute an STM instruction. This is what does the actual work of // carrying out the STM code. // Note: This is considered part of the "hardware" of the virtual machine. // You may not change it in any of the projects. public boolean execInst(int op, int ra, int ad, int rb, int rc, int rd) { boolean trap = false, jump = false; int pa; switch (op) { case 0: pa = physAddress(ad,false); if (PageFault == -1) regs[ra] = mem[pa]; break; case 1: pa = physAddress(ad,true); if (PageFault == -1) mem[pa] = regs[ra]; break; case 2: regs[ra] = regs[rb]; break; case 3: pa = physAddress(regs[rb],false); if (PageFault == -1) regs[ra] = mem[pa]; break; case 4: pa = physAddress(regs[ra],true); if (PageFault == -1) mem[pa] = regs[rb]; break; case 5: regs[rc] = regs[ra] + regs[rb]; break; case 6: regs[rc] = regs[ra] - regs[rb]; break; case 7: regs[rc] = regs[ra] * regs[rb]; break; case 8: if (regs[rb] == 0) { System.out.println("Arithmetic error: Divide by zero"); error = true; } else { regs[rc] = regs[ra] / regs[rb]; regs[rd] = regs[ra] % regs[rb]; } break; case 9: regs[ra]++; break; case 10: regs[ra]--; break; case 11: if (regs[ra] > regs[rb]) regs[rc] = 1; else regs[rc] = 0; break; case 12: regs[0] = ad; jump = true; break; case 13: if (regs[ra] == 0) {regs[0] = ad; jump = true;} break; case 14: regs[0] = regs[ra]; ; jump = true; break; case 15: trap = true; break; } clock++; if (!jump && (PageFault == -1)) regs[0]++; return(trap); } // Constructor for STM public STM(int NF, int dbg) { NFRAMES = NF; PAGESIZE = RAMSIZE / NFRAMES; NPAGES = VASPACE / PAGESIZE; pageTable = new PTE[NPAGES]; debug = dbg; if (debug == 2) System.out.println("Page size " + PAGESIZE + "; " + NPAGES + " pages;" + NFRAMES + " frames"); } }