Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Last active December 23, 2015 06:39
Show Gist options
  • Select an option

  • Save practicingruby/6595781 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/6595781 to your computer and use it in GitHub Desktop.
module Vintage
class Processor
OPCODES = { 0xa9 => :LDA, 0x8D => :STA }
def initialize
@acc = 0
@memory = {}
end
attr_reader :acc, :memory
def run(codes)
loop do
return if codes.empty?
op = OPCODES[codes.shift]
case op
when :LDA
@acc = codes.shift
when :STA
@memory[int16(codes.shift(2))] = @acc
else
raise NotImplementedError
end
end
end
private
def int16(bytes)
bytes.pack("c*").unpack("v")
end
end
end
processor = Vintage::Processor.new
processor.run([0xA9, 0x01, 0x8D, 0x00, 0x02, 0xA9, 0x05, 0x8d, 0x01, 0x02])
p processor.acc
p processor.memory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment