Module: RubyLabs::MARSLab

Defined in:
lib/marslab.rb

Overview

MarsLab

The MARSLab module has definitions of classes and methods used in the projects for Chapter 8 of Explorations in Computing.

The module has a Ruby implementation of the MARS virtual machine used in the game of Core War. The machine implemented here conforms as close as possible to the ICWS’88 standard, as described by Mark Durham in his Introduction to Redcode (corewar.co.uk/guides.htm).

Instructions and data in a MARS program are represented by Word objects.

Each Word has an opcode and two operands (A and B). Integer data is represented by a Word where the opcode field is DAT. Other Word objects have names of executable machine instructions for opcodes.

To test a single program users can make an instance of a class named MiniMARS, passing it the name of a test program. The test machine will have a small memory, only big enough to hold the test machine, and special versions of the step, dump and other methods.

The main machine that runs one, two, or three programs simultaneously is in the module named MARS. Methods that assemble, load, and run programs are module methods, e.g. to assemble a program call MARS.assemble(foo). #– TODO add [] and []= methods to Word, use it to extract/assign bits or ranges of bits TODO define ranges for fields, e.g. @@opcode = 0..3; use to get opcode of Word, e.g. instr TODO make sure all code uses [] interface (e.g. no more instr.amode == ?#) TODO pack words into binary – will kill several birds at once – speed, trim values to 0..4095, .. TODO color a cell black when a thread halts after executing an instruction in that cell TODO MARS.dump TODO pass load address to contest (which passes it on to Warrior.load)? #++

Defined Under Namespace

Classes: MARS, MARSRuntimeException, MARSView, Memory, MiniMARS, PC, RedcodeSyntaxError, Warrior, Word

Constant Summary collapse

@@marsDirectory =

– Initializations – These are “global” vars in the outer MARSLab scope that are accessible to all the classes and modules defined inside MARSLab

File.join(File.dirname(__FILE__), '..', 'data', 'mars')
@@opcodes =
["DAT", "MOV", "ADD", "SUB", "JMP", "JMZ", "JMN", "DJN", "CMP", "SPL", "END", "SLT", "EQU"]
@@modes =
"@<#"
@@maxEntries =
3
@@displayMemSize =
4096
@@params =
{
  :maxRounds => 1000,
  :memSize => @@displayMemSize,
  :buffer => 100,
  :tracing => false,
  :pause => 0.01,
}
@@viewOptions =
{
  :cellSize => 8,
  :cellRows => 32,
  :cellCols => 128,
  :padding => 20,
  :traceSize => 10,
  :cellColor => '#DDDDDD',
}
@@drawing =
nil
@@pcs =
Array.new
@@mem =
Memory.new(@@params[:memSize])
@@entries =
Array.new

Instance Method Summary collapse

Instance Method Details

#make_test_machine(file, size = nil) ⇒ Object

Make a MiniMARS VM with a single program loaded into memory at location 0. By default the size of the memory is the number of words in the program, but a memory size can be passed as a second argument.



1223
1224
1225
1226
1227
1228
1229
1230
# File 'lib/marslab.rb', line 1223

def make_test_machine(file, size = nil)
  begin
    return MiniMARS.new(file, size)
  rescue Exception => e
    puts "Failed to make test machine: #{$!}"
    return nil
  end
end