Class: MIPSTester::MIPS
- Inherits:
-
Object
- Object
- MIPSTester::MIPS
- 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
-
#mars_path ⇒ Object
readonly
MARS jar path.
Instance Method Summary collapse
-
#initialize(params = {}) ⇒ MIPSTester::MIPS
constructor
Create a new MIPSTester::MIPS object.
-
#test(file, &block) ⇒ Boolean
Run a given file in the emulator.
Constructor Details
#initialize(params = {}) ⇒ MIPSTester::MIPS
Create a new MIPSTester::MIPS object
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_path ⇒ Object (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
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..gsub(asm.path, File.basename(file)).split("\n")[0..1].join("\n") ensure asm.unlink end end |