Created
September 28, 2017 22:51
-
-
Save denpatin/5d049d8ae4e0dbcd6dc4899c3b8cfaf2 to your computer and use it in GitHub Desktop.
Simple CPU
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
# 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