-
-
Save practicingruby/6595781 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
| 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