Skip to content

Instantly share code, notes, and snippets.

@denpatin
Created September 28, 2017 22:51
Show Gist options
  • Save denpatin/5d049d8ae4e0dbcd6dc4899c3b8cfaf2 to your computer and use it in GitHub Desktop.
Save denpatin/5d049d8ae4e0dbcd6dc4899c3b8cfaf2 to your computer and use it in GitHub Desktop.
Simple CPU
# Main class for emulating the simplest CPU
class CPU
attr_reader :al, :ah, :ax
attr_writer :registers
def initialize
@al = 0
@ah = 0
@ax = 0
end
def al=(value)
@al = value.nil? ? @ax & 255 : value
@ax = (@ah << 8) + @al
end
def ah=(value)
@ah = value.nil? ? (@ax & 65_280) >> 8 : value
@ax = (@ah << 8) + @al
end
def ax=(value)
@ax = value.nil? ? (@ah << 8) + @al : value
@al = @ax & 255
@ah = (@ax & 65_280) >> 8
end
def mov(from, to)
new_hash = { to => from }
p new_hash
registers.merge!(new_hash)
end
def registers
@registers = { al: al, ah: ah, ax: ax }
end
end
cpu = CPU.new
# cpu.al = 5
# cpu.ah = 2
cpu.ax = 517
puts cpu.registers
cpu.mov(50, :al)
puts cpu.registers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment