The following is an abridged version of the C# code generated by Mips2cs for the "Arkanoid" demo. It is intended to give a flavour of the type of code that Mips2cs generates.
// This file was generated by Mips2cs using System; using System.Collections.Generic; // // Interface for Mips code to call back to C# // We pass the MipsVM object (so that memory can be accessed) as well // as registers a0 to a3 (input params). Return value will be passed // back to the MIPS in v0. // public interface MipsCallbacks { int f_10(MipsVM mips, int a0, int a1, int a2, int a3); int f_14(MipsVM mips, int a0, int a1, int a2, int a3); int f_20(MipsVM mips, int a0, int a1, int a2, int a3); int f_24(MipsVM mips, int a0, int a1, int a2, int a3); int f_28(MipsVM mips, int a0, int a1, int a2, int a3); // ... // ... int f_7c(MipsVM mips, int a0, int a1, int a2, int a3); int f_80(MipsVM mips, int a0, int a1, int a2, int a3); }; // // The MipsVM class itself // public class MipsVM { // CONSTRUCTOR public MipsVM(MipsCallbacks cb) { // Allocate 'page table'. All pages are NULL (i.e. unallocated) initially. mem = new int[65536][]; // Initialize all registers (except ra) to zero a0 = 0; a1 = 0; a2 = 0; // ... // ... v0 = 0; v1 = 0; // Initialize ra to the start address ra = 0x0040565c; // Store our reference to the callbacks object callbacks = cb; // Allocate one page for the stack mem[0xffff] = new int[16384]; // Set top_of_heap (end of code, rounded up to next 64K page) top_of_heap = 0x10000000; // Allocate the data and BSS pages InitMem(); // Initialize case table InitCaseTable(); } // MEMORY ACCESS FUNCTIONS // Read 4-byte signed integer from 4-byte aligned address public int ReadWord(int addr) { uint a = (uint)addr; return mem[a>>16][(a&0xffff)>>2]; } // Write 4-byte signed integer to 4-byte aligned address public void WriteWord(int addr, int val) { uint a = (uint)addr; mem[a>>16][(a&0xffff)>>2] = val; } // ... // SNIP other memory access functions: ReadHalf, WriteHalf, ReadByte, etc. // ... // RUN METHOD (TRAMPOLINE) public void Run() { int bb = case_table[ra]; while (true) { int bx = bb & 255; switch (bb >> 8) { case 0: // Special block numbers. switch (bb) { case 0: // Halt ra = -1; return; case 1: // Yield return; case 2: // Sbrk RunSbrk(); bb = case_table[ra]; break; case 4: v0 = callbacks.f_10(this, a0, a1, a2, a3); bb = case_table[ra]; break; case 5: v0 = callbacks.f_14(this, a0, a1, a2, a3); bb = case_table[ra]; break; // ... // ... case 32: v0 = callbacks.f_80(this, a0, a1, a2, a3); bb = case_table[ra]; break; default: throw new ApplicationException("Invalid jump to low memory"); } break; case 64: bb = Exec64(bx); break; case 65: bb = Exec65(bx); break; case 66: bb = Exec66(bx); break; case 67: bb = Exec67(bx); break; // ... // ... // ... case 192: bb = Exec192(bx); break; case 193: bb = Exec193(bx); break; default: throw new ApplicationException("Invalid jump"); } } } private int Exec64(int bx) { int z0; int z1; int z2; switch (bx) { case 0: // 0x00400020 sp = (-32 + sp); WriteWord((20 + sp), ra); goto case 1; case 1: // 0x00400030 ra = 4194360; goto case 14; case 2: // 0x00400038 goto case 3; case 3: // 0x00400040 ra = 4194376; return 49555; case 4: // 0x00400048 ra = ReadWord((20 + sp)); sp = (32 + sp); return case_table[ra]; case 5: // 0x00400058 sp = (-40 + sp); WriteWord((20 + sp), s0); s0 = 5111808; v0 = ReadByteU(5094920); WriteWord((36 + sp), ra); WriteWord((32 + sp), s3); WriteWord((28 + sp), s2); WriteWord((24 + sp), s1); if (v0 != 0) { goto case 13; } else { goto case 6; } case 6: // 0x0040007c v1 = 0; goto case 9; case 7: // 0x004000ac v1 = ReadWord((s3 + (v0 << 2))); ra = 4194500; WriteWord((-16884 + s1), v0); return case_table[v1]; case 8: // 0x004000c4 z0 = ReadWord((-16884 + s1)); v1 = (((uint)z0) < ((uint)s2) ? 1 : 0); v0 = (1 + z0); if (v1 != 0) { goto case 7; } else { goto case 9; } case 9: // 0x004000d8 v0 = 1; goto case 12; case 10: // 0x004000e8 return 0; case 11: // 0x004000f4 v0 = 1; goto case 12; case 12: // 0x004000f8 WriteByte((-16888 + s0), v0); goto case 13; case 13: // 0x004000fc ra = ReadWord((36 + sp)); s3 = ReadWord((32 + sp)); s2 = ReadWord((28 + sp)); s1 = ReadWord((24 + sp)); s0 = ReadWord((20 + sp)); sp = (40 + sp); return case_table[ra]; case 14: // 0x00400118 sp = (-24 + sp); WriteWord((20 + sp), ra); goto case 16; case 15: // 0x0040012c return 0; case 16: // 0x00400140 a0 = 5111808; v0 = ReadWord(5090196); t9 = 0; if (v0 == 0) { goto case 19; } else { goto case 17; } case 17: // 0x00400154 a0 = (-21612 + a0); if (t9 == 0) { goto case 19; } else { goto case 18; } case 18: // 0x00400160 ra = ReadWord((20 + sp)); sp = (24 + sp); return case_table[t9]; case 19: // 0x0040016c ra = ReadWord((20 + sp)); sp = (24 + sp); return case_table[ra]; case 20: // 0x0040017c sp = (-920 + sp); WriteWord((176 + sp), 4751424); WriteWord((180 + sp), 5080048); WriteWord((184 + sp), (32 + sp)); z0 = (184 + sp); WriteWord((916 + sp), ra); WriteWord((912 + sp), s8); WriteWord((908 + sp), s7); WriteWord((904 + sp), s6); WriteWord((900 + sp), s5); WriteWord((896 + sp), s4); WriteWord((892 + sp), s3); WriteWord((888 + sp), s2); WriteWord((884 + sp), s1); WriteWord((880 + sp), s0); a0 = (152 + sp); WriteWord((4 + z0), 4203820); ra = 4194788; WriteWord((8 + z0), sp); return 49342; case 21: // 0x004001e4 a0 = 20; ra = 4194804; WriteWord((156 + sp), 1); return 35196; case 22: // 0x004001f4 WriteWord((712 + sp), v0); z0 = ReadWord((712 + sp)); WriteWord(z0, 5046472); WriteWord((4 + z0), -999); WriteWord((8 + z0), -999); WriteWord((12 + z0), 0); WriteWord((16 + z0), 0); WriteWord((52 + sp), z0); a0 = 16; WriteWord((56 + sp), 0); ra = 4194868; WriteWord((156 + sp), 12); return 35196; case 23: // 0x00400234 WriteWord((4 + v0), 1); WriteWord((8 + v0), 1); z0 = ReadWord((712 + sp)); WriteWord(v0, 5045576); WriteWord((12 + v0), z0); WriteWord((56 + sp), v0); a0 = 4; ra = 4194920; WriteWord((156 + sp), 2); return 35196; case 24: // 0x00400268 WriteWord((760 + sp), v0); WriteWord(ReadWord((760 + sp)), 5047544); a0 = 16; WriteWord((60 + sp), ReadWord((760 + sp))); WriteWord((64 + sp), 0); ra = 4194968; WriteWord((156 + sp), 14); return 35196; case 25: // 0x00400298 WriteWord((788 + sp), ReadWord((52 + sp))); WriteWord((4 + v0), 1); WriteWord(v0, 5045664); WriteWord((8 + v0), 1); z0 = ReadWord((788 + sp)); z1 = ReadWord((760 + sp)); WriteWord((64 + sp), v0); WriteWord((12 + v0), z1); if (z0 == 0) { return 16953; } else { goto case 26; } case 26: // 0x004002cc a0 = (40 + sp); a1 = 5043752; WriteWord((824 + sp), ReadWord((16 + ReadWord(ReadWord((788 + sp)))))); a2 = (32 + sp); ra = 4195064; WriteWord((156 + sp), 4); return 28149; case 27: // 0x004002f8 WriteWord((16 + sp), 1); WriteWord((24 + sp), (40 + sp)); WriteWord((156 + sp), 5); a1 = ReadWord((788 + sp)); v0 = ReadWord((824 + sp)); WriteWord((20 + sp), 0); a0 = (68 + sp); a2 = 750; ra = 4195116; a3 = 500; return case_table[v0]; case 28: // 0x0040032c a0 = (-12 + ReadWord((40 + sp))); if (a0 != 5097044) { return 16950; } else { goto case 29; } case 29: // 0x00400344 a1 = ReadWord((52 + sp)); v0 = 6; if (a1 == 0) { return 16949; } else { goto case 30; } case 30: // 0x00400354 a0 = (76 + sp); ra = 4195168; WriteWord((156 + sp), v0); return 17161; case 31: // 0x00400360 a0 = ReadWord((68 + sp)); v0 = 7; if (a0 == 0) { return 17010; } else { goto case 32; } case 32: // 0x00400370 a1 = (76 + sp); ra = 4195196; WriteWord((156 + sp), v0); return 17575; case 33: // 0x0040037c z0 = ReadWord((60 + sp)); WriteWord((792 + sp), z0); if (z0 == 0) { return 17009; } else { goto case 34; } case 34: // 0x0040038c WriteByte((137 + sp), 1); a0 = ReadWord((100 + sp)); WriteWord((88 + sp), ReadWord((-32672 + gp))); a1 = ReadWord((-32664 + gp)); ra = 4195248; WriteWord((92 + sp), ReadWord((-32668 + gp))); return 48952; case 35: // 0x004003b0 WriteWord((80 + sp), v0); WriteWord((84 + sp), v0); WriteWord((780 + sp), 2); goto case 47; case 36: // 0x004003c4 a0 = 16; ra = 4195280; WriteWord((156 + sp), v0); return 35196; case 37: // 0x004003d0 a0 = ReadWord((780 + sp)); ra = 4195292; WriteWord((784 + sp), v0); return 49047; case 38: // 0x004003dc a1 = ReadWord((-32660 + gp)); ra = 4195304; a0 = v0; return 48952; case 39: // 0x004003e8 v1 = ReadWord((784 + sp)); WriteWord((804 + sp), v0); if ((8 + v1) == 0) { goto case 41; } else { goto case 40; } case 40: // 0x004003fc z0 = ReadWord((-32656 + gp)); WriteWord((8 + v1), ReadWord((804 + sp))); WriteWord((12 + v1), z0); goto case 41; case 41: // 0x0040040c a0 = ReadWord((784 + sp)); ra = 4195352; a1 = (104 + sp); return 18401; case 42: // 0x00400418 a0 = 16; ra = 4195368; WriteWord((156 + sp), 7); return 35196; case 43: // 0x00400428 if ((8 + v0) == 0) { goto case 45; } else { goto case 44; } case 44: // 0x00400434 z0 = ReadWord((-32652 + gp)); WriteWord((8 + v0), ReadWord((804 + sp))); WriteWord((12 + v0), z0); goto case 45; case 45: // 0x00400444 a0 = v0; ra = 4195408; a1 = (104 + sp); return 18401; case 46: // 0x00400450 WriteWord((780 + sp), (1 + ReadWord((780 + sp)))); goto case 47; case 47: // 0x00400460 a0 = ReadWord((-31420 + gp)); a1 = ReadWord((-32660 + gp)); ra = 4195440; return 48985; case 48: // 0x00400470 ra = 4195448; a0 = v0; return 49058; case 49: // 0x00400478 z0 = (ReadWord((780 + sp)) < (-2 + v0) ? 1 : 0); v0 = 7; if (z0 != 0) { goto case 36; } else { goto case 50; } case 50: // 0x0040048c a0 = ReadWord((792 + sp)); v1 = 7; v0 = ReadWord((8 + ReadWord(a0))); ra = 4195500; WriteWord((156 + sp), 7); return case_table[v0]; // ... // ... // ... case 255: // 0x00401128 WriteWord((528 + sp), (-2147483648 ^ ReadWord((508 + sp)))); a1 = 0; a0 = v0; ra = 4198728; WriteWord((504 + sp), v0); return 48952; } throw new ApplicationException("Invalid block number"); } private int Exec65(int bx) { int z0; int z1; switch (bx) { case 0: // 0x00401148 a0 = ReadWord((528 + sp)); ra = 4198740; a1 = v0; return 48942; case 1: // 0x00401154 a1 = ReadWord((472 + sp)); ra = 4198752; a0 = v0; return 48947; // ... // ... // ... } private int Exec66(int bx) { int z0; int z1; switch (bx) { case 0: // 0x00401fbc v1 = (104 + sp); WriteWord((568 + sp), ReadWord((572 + sp))); WriteWord((872 + sp), v1); goto case 5; case 1: // 0x00401fd0 z0 = ReadWord(ReadWord((644 + sp))); WriteWord((676 + sp), ReadWord((112 + sp))); WriteWord((720 + sp), ReadWord((36 + z0))); z0 = ReadWord((576 + sp)); a0 = ReadWord((8 + z0)); z1 = ReadWord((676 + sp)); WriteWord((748 + sp), ReadWord((12 + z0))); if (z1 == 0) { goto case 15; } else { goto case 2; } // ... // ... // ... } // ... // ... // ... // SBRK IMPLEMENTATION private void RunSbrk() { int old_top_of_heap = top_of_heap; int bytes_required = a0; while (top_of_heap - old_top_of_heap < bytes_required) { if ((top_of_heap & 0xffff) == 0) { // Need to allocate a new page before moving upwards mem[top_of_heap>>16] = new int[16384]; } // Move top_of_heap to start of next page top_of_heap += 0x00010000; top_of_heap = (int)( ((uint)top_of_heap) & 0xffff0000u ); } // If we have over-allocated, pull top_of_heap back down again top_of_heap = old_top_of_heap + bytes_required; // Return the old top of heap in v0 v0 = old_top_of_heap; } // CASE TABLE IMPLEMENTATION private void InitCaseTable() { case_table = new Dictionary<int,int>(); case_table[0x00400030] = 16385; case_table[0x00400038] = 16386; case_table[0x00400040] = 16387; case_table[0x00400048] = 16388; case_table[0x004000c4] = 16392; case_table[0x004000f4] = 16395; // ... case_table[0x004cf5d0] = 49561; case_table[0x004cf5d8] = 49562; } public void InitMem() { mem[64] = new int[16384]; mem[76] = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ... 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 774857327, 1869837359, 1936548210, 1952411764, 1915712360, 1634887012, 1601205362, 778596464, 0, 1886920737, 1025519616, 1881161021, 540024956, 2082500640, 557654128, 2013265920, 1114600814, 1668882498, 1634495520, 1197567333, 0, 1667331431, // ... }; mem[77] = new int[] { // ... }; } // PRIVATE DATA // Memory (64K pages each of 64K bytes) private int[][] mem; // Registers private int a0; private int a1; private int a2; private int a3; private int gp; private int hi; private int lo; private int ra; private int s0; // ... private int t8; private int t9; private int v0; private int v1; // One past the last byte of the heap. Used by sbrk implementation. private int top_of_heap; // Callbacks object (given to us by the caller). private MipsCallbacks callbacks; // Dictionary for basic block lookups Dictionary<int, int> case_table; }