Class: LC3
Overview
Class to provide access to LC-3 simulator instance
Instance Attribute Summary collapse
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #clear_breakpoint(addr) ⇒ Object
- #clear_breakpoints ⇒ Object
- #close ⇒ Object
- #continue ⇒ Object
- #file(filename) ⇒ Object
- #get_address(label) ⇒ Object
- #get_memory(addr) ⇒ Object
- #get_output ⇒ Object
- #get_register(reg) ⇒ Object
-
#initialize ⇒ LC3
constructor
A new instance of LC3.
- #initialize_lc3sim ⇒ Object
- #inspect ⇒ Object
- #set_breakpoint(addr) ⇒ Object
- #set_memory(addr, val) ⇒ Object
- #set_register(reg, val) ⇒ Object
- #step ⇒ Object
Methods included from LC3Spec::Helpers
#is_absolute_path?, #normalize_to_i, #normalize_to_s
Constructor Details
#initialize ⇒ LC3
Returns a new instance of LC3.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/lc3spec/lc3.rb', line 17 def initialize # Logging @logger = Logger.new(STDERR) if ENV['LC3DEBUG'] @logger.level = Logger::DEBUG else @logger.level = Logger::ERROR end @logger.formatter = proc do |severity, datetime, progname, msg| "#{severity}: #{msg}\n" end # Registers @registers = {} [:R0, :R1, :R2, :R3, :R4, :R5, :R6, :R7, :PC, :IR, :PSR].each do |reg| @registers[reg] = 'x0000' end @registers[:CC] = 'ZERO' # Memory @memory = Hash.new('x0000') @labels = {} initialize_lc3sim end |
Instance Attribute Details
#logger ⇒ Object
Returns the value of attribute logger.
15 16 17 |
# File 'lib/lc3spec/lc3.rb', line 15 def logger @logger end |
Instance Method Details
#clear_breakpoint(addr) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/lc3spec/lc3.rb', line 166 def clear_breakpoint(addr) if addr == :all @io.puts 'break clear all' return self end addr = addr.upcase.to_s if addr.respond_to? :upcase if @labels.include? addr @io.puts "break clear #{addr}" else @io.puts "break clear #{normalize_to_s(addr)}" end sleep(0.01) while @io.ready? msg = @io.readline parse_msg msg.strip end self end |
#clear_breakpoints ⇒ Object
190 191 192 |
# File 'lib/lc3spec/lc3.rb', line 190 def clear_breakpoints clear_breakpoint :all end |
#close ⇒ Object
247 248 249 250 |
# File 'lib/lc3spec/lc3.rb', line 247 def close @output.close @server.close end |
#continue ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/lc3spec/lc3.rb', line 140 def continue @io.puts 'continue' parse_until_print_registers self end |
#file(filename) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/lc3spec/lc3.rb', line 107 def file(filename) @io.puts "file #{filename}" # Need to encounter 2 TOCODEs before we're done tocode_counter = 2 while tocode_counter > 0 msg = @io.readline parse_msg msg.strip if msg =~ /^TOCODE/ tocode_counter -= 1 end # ignore warning about no symbols next if msg =~ /WARNING: No symbols/ # don't ignore other errors break if msg =~ /^ERR/ end self end |
#get_address(label) ⇒ Object
103 104 105 |
# File 'lib/lc3spec/lc3.rb', line 103 def get_address(label) @labels[label.upcase.to_s] end |
#get_memory(addr) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/lc3spec/lc3.rb', line 71 def get_memory(addr) if addr.respond_to?(:upcase) label_addr = get_address(addr) addr = label_addr unless label_addr.nil? end @memory[normalize_to_i(addr)] end |
#get_output ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/lc3spec/lc3.rb', line 194 def get_output out = '' # There is no signal that tells the GUI that output is ready... # FIXME: This is a bug waiting to happen retries = 10 until @output.ready? sleep(0.1) retries -= 1 break if retries <= 0 end while @output.ready? out << @output.readpartial(1024) end out.gsub("\n\n--- halting the LC-3 ---\n\n", '') end |
#get_register(reg) ⇒ Object
43 44 45 46 |
# File 'lib/lc3spec/lc3.rb', line 43 def get_register(reg) reg = reg.to_s.upcase.to_sym # Ruby 1.8 doesn't support Symbol#upcase @registers[reg] end |
#initialize_lc3sim ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/lc3spec/lc3.rb', line 214 def initialize_lc3sim # Start lc3sim instance @io = IO.popen(%w(lc3sim -gui), 'r+') begin # Port for output server @port = (rand * 1000 + 5000).to_i # Start server to get output @server = TCPServer.new @port rescue Errno::EADDRINUSE retry end th = Thread.new do @output = @server.accept end # Initialize lc3sim with port number @io.puts @port # Wait for lc3sim to connect with our server th.join # Read LC-3 initialization messages (mostly loading lc3os) parse_until_print_registers # Clear all the welcome messages from lc3sim output while @output.ready? @output.readpartial 1024 end end |
#inspect ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/lc3spec/lc3.rb', line 252 def inspect registers = @registers.map { |k, v| "#{k}=#{v}" }.join(' ') addr_to_label = @labels.invert memory_header = "%18s ADDR VALUE" % "label" memory = @memory.map do |addr, value| "%18s %s %s" % [(addr_to_label[addr] or ''), normalize_to_s(addr), value] end.join("\n") #[memory_header, memory, registers].join("\n") registers end |
#set_breakpoint(addr) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lc3spec/lc3.rb', line 148 def set_breakpoint(addr) addr = addr.upcase.to_s if addr.respond_to? :upcase if @labels.include? addr @io.puts "break set #{addr}" else @io.puts "break set #{normalize_to_s(addr)}" end sleep(0.01) while @io.ready? msg = @io.readline parse_msg msg.strip end self end |
#set_memory(addr, val) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/lc3spec/lc3.rb', line 80 def set_memory(addr, val) # addr may be memory address or label if addr.respond_to?(:upcase) and @labels.include?(addr.upcase.to_s) # Is a label addr = addr.upcase.to_s else # Not a label addr = normalize_to_s(addr) end @io.puts("memory #{addr} #{normalize_to_s(val)}") loop do msg = @io.readline parse_msg msg.strip break if msg =~ /^ERR|CODE/ end self end |
#set_register(reg, val) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lc3spec/lc3.rb', line 48 def set_register(reg, val) reg = reg.to_s.upcase # Don't use ! version because it doesn't work for symbols unless @registers.keys.include? reg.to_sym raise "Invalid register: #{reg.to_s}" end if val.nil? raise "Invalid register value for #{reg.to_s}: #{val}" end @io.puts "register #{reg.to_s} #{normalize_to_s(val)}" loop do msg = @io.readline parse_msg msg.strip break if msg =~ /^TOCODE/ end self end |
#step ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/lc3spec/lc3.rb', line 132 def step @io.puts 'step' parse_until_print_registers self end |