Class: MIPSTester::MIPS

Inherits:
Object
  • Object
show all
Defined in:
lib/mips_tester.rb

Overview

Main MIPS tester class. It provides the methods to test MIPS ASMs files

Constant Summary collapse

REGISTER_REGEX =

Register validation

/^(at|v[01]|a[0-3]|s[0-7]|t\d|[2-9]|1[0-9]|2[0-5])$/
ADDRESS_REGEX =

Memory address validation

/^0x[\da-f]{8}$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ MIPSTester::MIPS

Create a new MIPSTester::MIPS object

Examples:

MIPSTester::MIPS.new :mars_path => 'path/to/mars.jar'

Raises:



35
36
37
38
# File 'lib/mips_tester.rb', line 35

def initialize(params = {})
  @mars_path = params.delete(:mars_path)
  raise MIPSMarsError.new("Provide valid Mars jar.") if not @mars_path or not File.exists? @mars_path
end

Instance Attribute Details

#mars_pathObject (readonly)

MARS jar path



27
28
29
# File 'lib/mips_tester.rb', line 27

def mars_path
  @mars_path
end

Instance Method Details

#test(file, &block) ⇒ Boolean

Run a given file in the emulator. *A provided block is mandatory*, with starter registers and expected values. A simple DSL is provided:

  • set [Hash] => set initial registers or memory addresses

  • expect [Hash] => expect values of registers or memory addresses

  • verbose! => optional, if given prints on STDOUT set registers and expected ones

Examples:

test "file.asm" do
  set :s1 => 6, '0x10010000' => 0xFF
  expect :s5 => 6
  verbose!
end

Parameters:

  • file (String)

    The path to the file to run

  • block

    The block to provide info on what to test

Returns:

  • (Boolean)

    True if the test went well, False if not.

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mips_tester.rb', line 58

def test(file, &block)
  raise MIPSFileError.new("Provide valid file.") if not file or not File.exists? file
  raise MIPSInvalidBlockError.new("Provide block.") if not block

  reset!
  
  instance_eval(&block)

  asm = Tempfile.new "temp.asm"
  asm.write prep_params if block
  asm.write File.read(file)
  asm.close

  cli = `#{["java -jar",
            @mars_path,
            @exp.empty? ? "" : @exp.keys.join(" "), 
            @addresses.empty? ? "" : [@addresses.keys.min, @addresses.keys.max].join("-"),
            "nc dec",
            asm.path].join(" ")}`
  
  begin
    results = parse_results cli
    
    puts "Expected:\n#{@exp}\nResults:\n#{results}" if @verbose
    
    return compare_hashes(@exp, results)
  rescue Exception => ex
    raise MIPSFileError.new ex.message.gsub(asm.path, File.basename(file)).split("\n")[0..1].join("\n")
  ensure
    asm.unlink
  end
end