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 |
# File 'lib/lc3spec/lc3.rb', line 17 def initialize # Logging @logger = Logger.new(STDERR) @logger.level = Logger::ERROR #@logger.level = Logger::DEBUG @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
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/lc3spec/lc3.rb', line 162 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
186 187 188 |
# File 'lib/lc3spec/lc3.rb', line 186 def clear_breakpoints clear_breakpoint :all end |
#close ⇒ Object
239 240 241 242 |
# File 'lib/lc3spec/lc3.rb', line 239 def close @output.close @server.close end |
#continue ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/lc3spec/lc3.rb', line 136 def continue @io.puts 'continue' parse_until_print_registers self end |
#file(filename) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/lc3spec/lc3.rb', line 103 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
99 100 101 |
# File 'lib/lc3spec/lc3.rb', line 99 def get_address(label) @labels[label.upcase.to_s] end |
#get_memory(addr) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/lc3spec/lc3.rb', line 67 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
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/lc3spec/lc3.rb', line 190 def get_output out = '' # There is no signal that tells the GUI that output is ready... # FIXME: This is a bug waiting to happen until @output.ready? sleep(0.01) end while @output.ready? out << @output.readpartial(1024) end out.gsub("\n\n--- halting the LC-3 ---\n\n", '') end |
#get_register(reg) ⇒ Object
40 41 42 |
# File 'lib/lc3spec/lc3.rb', line 40 def get_register(reg) @registers[reg] end |
#initialize_lc3sim ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/lc3spec/lc3.rb', line 206 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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/lc3spec/lc3.rb', line 244 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
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/lc3spec/lc3.rb', line 144 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lc3spec/lc3.rb', line 76 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lc3spec/lc3.rb', line 44 def set_register(reg, val) reg = reg.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
128 129 130 131 132 133 134 |
# File 'lib/lc3spec/lc3.rb', line 128 def step @io.puts 'step' parse_until_print_registers self end |