Created
May 2, 2011 22:02
-
-
Save jatkins/952462 to your computer and use it in GitHub Desktop.
system simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# | |
# Author: Jacob Atkins | |
# Email: jta4j@mcs.uvawise.edu | |
# Class: CSC 4300 - Computer Architecture | |
# | |
apps = Dir["./apps/*.hack"].sort | |
@@memory = Array.new(100) | |
@@counter = 0 | |
@@i_register = "" | |
@@o_register = 0 | |
@@accumulator = 0 | |
def load_app(app) | |
f = File.open(app, "r") | |
app = f.read | |
f.close | |
app = app.split("\n") | |
count = 0 | |
app.each do |d| | |
command = d.split("//") | |
command = command[0].chomp(" ") | |
if(command != "") | |
if(command.match(/^\d{2}/)) | |
data = command.split(" ") | |
@@memory[data[0].to_i] = data[1] | |
else | |
@@memory[count] = command | |
count += 1 | |
end | |
end | |
end | |
end | |
def execute | |
co = @@memory[@@counter].split(" ") | |
while(!co[0].to_s.match(/HALT/) || co[0].nil?) | |
if(co[0].to_s.match(/READ/)) | |
update(co) | |
read | |
elsif(co[0].to_s.match(/WRIT/)) | |
update(co) | |
writ | |
elsif(co[0].to_s.match(/LOAD/)) | |
update(co) | |
load | |
elsif(co[0].to_s.match(/STOR/)) | |
update(co) | |
stor | |
elsif(co[0].to_s.match(/ADD/)) | |
update(co) | |
add | |
elsif(co[0].to_s.match(/SUB/)) | |
update(co) | |
sub | |
elsif(co[0].to_s.match(/DIV/)) | |
update(co) | |
div | |
elsif(co[0].to_s.match(/MULT/)) | |
update(co) | |
mult | |
elsif(co[0].to_s.match(/JUMP/)) | |
update(co) | |
jump | |
elsif(co[0].to_s.match(/JMPN/)) | |
update(co) | |
jmpn | |
elsif(co[0].to_s.match(/JMPZ/)) | |
update(co) | |
jmpz | |
elsif(co[0].to_s.match(/DUMP/)) | |
@@i_register = co[0] | |
dump | |
elsif(co[0].to_s.match(/MODA/)) | |
update(co) | |
moda | |
end | |
co = @@memory[@@counter].split(" ") | |
end | |
end | |
def read | |
@@memory[@@o_register] = gets.chomp.to_i | |
@@counter += 1 | |
end | |
def writ | |
puts @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def load | |
@@accumulator = @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def stor | |
@@memory[@@o_register] = @@accumulator | |
@@counter += 1 | |
end | |
def add | |
@@accumulator = @@accumulator + @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def sub | |
@@accumulator = @@accumulator - @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def div | |
@@accumulator = @@accumulator / @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def mult | |
@@accumulator = @@accumulator * @@memory[@@o_register] | |
@@counter += 1 | |
end | |
def jump | |
@@counter = @@o_register | |
end | |
def jmpn | |
if(@@accumulator < 0) | |
@@counter = @@o_register | |
end | |
end | |
def jmpz | |
if(@@accumulator == 0) | |
@@counter = @@o_register | |
end | |
end | |
def dump | |
puts | |
puts "Counter: #{@@counter}" | |
puts "Accumulator: #{@@accumulator}" | |
puts "Instruction register: #{@@i_register}" | |
puts "Operand register: #{@@o_register}" | |
puts "Memory:" | |
count = 0 | |
while count < 100 | |
puts "#{count} #{@@memory[count]}" | |
count += 1 | |
end | |
@@counter += 1 | |
end | |
def moda | |
@@memory[@@o_register] = @@accumulator | |
@@counter += 1 | |
end | |
def update(test) | |
@@i_register = test[0] | |
@@o_register = test[1].to_i | |
end | |
puts "Enter the number for the program you want to run." | |
acount = 1 #Count for apps | |
apps.each do |a| | |
puts "#{acount}) #{File.basename(a)}" | |
acount += 1 | |
end | |
r = gets.chomp | |
run = apps[r.to_i - 1] | |
load_app(run) | |
execute | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment