Fun

Build a MIPS-simulator

Based on python

Build a MIPS-simulator

GitHub release (latest by date) PyPI

Introduction

MIPS-Simulator runs MIPS32 programs. Unlike real simulator, which assembles the instructions into machine code and executes them, MIPS-Simulator just parse the instructions and runs them using python code. In other words, MIPS-simulator actually interprets mips instructions.

example.png

Note

  • This project is in its early stage, which means some functions may not supported yet.

Install

  • With pip :pip3 install fengyong
  • With src : Clone or fork this project, then build it with python3 setup.py install

Usage

Shell

After install, you can run a mips shell with mips-shell command.

Assemble

from fengyong import Assembler

instructions = """
j 10000
add $s0,$a1,$t7
sw $s1,10($s2)"""

machine_code = Assembler.encode(instructions)
print(machine_code.bin)
print(machine_code.value_base(16))

DisAssemble

from fengyong import DisAssembler
from fengyong import  RegData

machine_code = RegData("0x8002710af820ae51000a")
instructions = DisAssembler.decode(machine_code)
print(instructions)

Simulate - RUN!

  • Run mips instruction in line:
    from fengyong import Simulator
    
    Simulator.run_line("addi $s0, $s1, 10")
    
  • Run asm file:
    from fengyong import Simulator
    
    Simulator.run_file("../test/drings.asm")
    

Simulate - Debug

  • Set the register data
    from fengyong import Registers
    from fengyong import RegData
    
    Registers.reg_set("$s0",RegData(100))
    
  • Get the register data
    from fengyong import Registers
    
    res = Registers.reg_get("$s0")
    
    # print all the "s" registers
    Registers.print("s")
    
    # print all registers
    Registers.print()
    

Example

Contributing

Everyone is welcomed to contribute!

To contribute, you may want to read the README to find out what we are doing here.

Dev info

Here are some other informations for developers:

This project mainly contains two seperate part : Assembler and Simulator

Assembler

  • class Assembler : Encodes mips instructions to machine code.
  • class DisAssembler : Decodes machine code to mips instructions.
  • Depends on class RegData, misc.static.

Simulator

  • class Interpreter : Parse and run mips instructions.
  • class Simulator : Run the mips instructions.
  • Depends on class RegData,class Memory,class Registers,misc.static.
  • Supported mips instructions:add,addi,and,andi,beq,bgez,bgtz,blez,bltz,div,j,lui,lw,mfhi,mflo,mult,noop,or,ori,sll,sllv,slt,slti,srl,srlv,sub,sw,xor,xori

Dependency

  • class RegData : [Key of this project] Deals with all kinds of numbers and number_length stuffs.
  • class Memory : Simulates sparse memory (singleton mode).
  • class Registers : Simulates registers (singleton mode).
  • misc.static : Stores the static dictionaries.

Unit test

See unit_test

本文总字数: 2264