Created
September 28, 2013 15:22
-
-
Save practicingruby/6743093 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # NOTE: Maybe have a factory to split objects between "Immediate" mode and | |
| # everything else | |
| class Cell | |
| def initialize(processor, mode) | |
| @processor = processor | |
| @mode = mode | |
| compute_address | |
| end | |
| def value | |
| @processor.memory[@address] | |
| end | |
| def value=(e) | |
| raise NotImplementedError if ["IM", "#", "@"].include?(@mode) | |
| @processor.memory[@address] = e | |
| end | |
| private | |
| def int16(bytes) | |
| bytes.pack("c*").unpack("v").first | |
| end | |
| def compute_address | |
| return if ["IM", "#", "@"].include?(@mode) | |
| # FIXME: Another kind of pointless case statement | |
| @address = case @mode | |
| when "ZP" | |
| @processor.memory.shift | |
| when "ZX" | |
| (@processor.memory.shift + @processor.reg.x) % 256 | |
| when "AB" | |
| int16(@processor.memory.shift(2)) | |
| when "AY" | |
| int16(@processor.memory.shift(2) + @processor.reg.y) | |
| when "IX" | |
| m = @processor.memory.shift | |
| l = @processor.memory[m + processor.reg.x] | |
| h = @processor.memory[m + processor.reg.x + 1] | |
| int16([l, h]) | |
| when "IY" | |
| m = @processor.memory.shift | |
| l = @processor.memory[m] | |
| h = @processor.memory[m + 1] | |
| int16([l,h]) + y | |
| else | |
| raise NotImplementedError, @mode | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment