Class: Mos6502::Cpu

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mos6502/cpu.rb,
lib/mos6502/cpu_ops.rb,
lib/mos6502/cpu_instructions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_pc: 0x600, code: nil) ⇒ Cpu

Returns a new instance of Cpu.



22
23
24
25
26
27
# File 'lib/mos6502/cpu.rb', line 22

def initialize(initial_pc: 0x600, code: nil)
  @initial_pc = initial_pc
  @status = CpuFlags.new
  load!(code)
  @instructions = instructions
end

Instance Attribute Details

#aObject (readonly)

Returns the value of attribute a.



17
18
19
# File 'lib/mos6502/cpu.rb', line 17

def a
  @a
end

#pcObject (readonly)

Returns the value of attribute pc.



17
18
19
# File 'lib/mos6502/cpu.rb', line 17

def pc
  @pc
end

#spObject (readonly)

Returns the value of attribute sp.



17
18
19
# File 'lib/mos6502/cpu.rb', line 17

def sp
  @sp
end

#xObject (readonly)

Returns the value of attribute x.



17
18
19
# File 'lib/mos6502/cpu.rb', line 17

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



17
18
19
# File 'lib/mos6502/cpu.rb', line 17

def y
  @y
end

Instance Method Details

#dump_memory(start, length) ⇒ Object



57
58
59
# File 'lib/mos6502/cpu.rb', line 57

def dump_memory(start, length)
  @memory.dump(start, length)
end

#inspectObject



61
62
63
64
65
66
67
# File 'lib/mos6502/cpu.rb', line 61

def inspect
  format(
    'a: 0x%02x, x: 0x%02x, y: 0x%02x, sp: 0x%02x, ' \
    'pc: 0x%04x, op: 0x%02x, status: 0b%08b',
    @a, @x, @y, @sp, @pc, @memory.get(@pc), @status.encode
  )
end

#load!(code = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mos6502/cpu.rb', line 34

def load!(code = nil)
  reset!
  return if code.nil?

  loc = @pc
  code = code.bytes if code.respond_to?(:bytes)
  code.each do |b|
    @memory.set(loc, b)
    loc += 1
  end
end

#load_image(image = nil, start = 0) ⇒ Object



46
47
48
49
50
# File 'lib/mos6502/cpu.rb', line 46

def load_image(image = nil, start = 0)
  return if image.nil?

  @memory.load(image, start)
end

#load_image!(image = nil, start = 0) ⇒ Object



52
53
54
55
# File 'lib/mos6502/cpu.rb', line 52

def load_image!(image = nil, start = 0)
  reset!
  load_image(image, start)
end

#stepObject



29
30
31
32
# File 'lib/mos6502/cpu.rb', line 29

def step
  inst = next_byte
  @instructions[inst].call
end