Class: Badline::CPU

Inherits:
Cycleable show all
Includes:
InstructionSet, IntegerHelper, Traps
Defined in:
lib/badline/cpu.rb

Defined Under Namespace

Classes: InvalidOpcodeError

Constant Summary collapse

STATUS_FLAGS =
[:carry, :zero, :interrupt, :decimal, :break, 1,
:overflow, :negative].freeze

Instance Attribute Summary collapse

Attributes inherited from Cycleable

#cycles

Instance Method Summary collapse

Methods included from Traps

#install_trap

Methods included from InstructionSet

#brk, #nop

Methods included from InstructionSet::Transfer

#lda, #ldx, #ldy, #sta, #stx, #sty, #tax, #tay, #tsx, #txa, #txs, #tya

Methods included from InstructionSet::Stack

#jmp, #jsr, #pha, #php, #pla, #plp, #rti, #rts

Methods included from InstructionSet::Illegal

#alr, #anc, #ane, #arr, #dcp, #isc, #jam, #las, #lax, #lxa, #nop_nocycle, #rla, #rra, #sax, #sbx, #sha, #shx, #shy, #slo, #sre, #tas

Methods included from InstructionSet::Flag

#clc, #cld, #cli, #clv, #sec, #sed, #sei

Methods included from InstructionSet::IncDec

#dec, #dex, #dey, #inc, #inx, #iny

Methods included from InstructionSet::Branch

#bcc, #bcs, #beq, #bmi, #bne, #bpl, #bvc, #bvs

Methods included from InstructionSet::Bitwise

#and, #asl, #bit, #eor, #lsr, #ora, #rol, #ror

Methods included from InstructionSet::Arithmetic

#adc, #cmp, #cpx, #cpy, #sbc

Methods included from IntegerHelper

#bcd, #bcd_to_i, #format16, #format8, #high_byte, #low_byte, #signed_int8, #uint16

Methods inherited from Cycleable

#cycle!, #pending_write?

Constructor Details

#initialize(memory = nil, debug: false) ⇒ CPU

Returns a new instance of CPU.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/badline/cpu.rb', line 17

def initialize(memory = nil, debug: false)
  @debug = debug
  @memory = memory || Memory.new
  @status = Status.new(STATUS_FLAGS, value: 0b00100000)
  reset_registers

  @nmi = @irq = false

  @instructions = 0
  @traps = nil
  super()
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def a
  @a
end

#boundary_crossedObject (readonly)

Returns the value of attribute boundary_crossed.



13
14
15
# File 'lib/badline/cpu.rb', line 13

def boundary_crossed
  @boundary_crossed
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



13
14
15
# File 'lib/badline/cpu.rb', line 13

def instructions
  @instructions
end

#irqObject

Returns the value of attribute irq.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def irq
  @irq
end

#memoryObject (readonly)

Returns the value of attribute memory.



13
14
15
# File 'lib/badline/cpu.rb', line 13

def memory
  @memory
end

#nmiObject

Returns the value of attribute nmi.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def nmi
  @nmi
end

#program_counterObject

Returns the value of attribute program_counter.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def program_counter
  @program_counter
end

#stack_pointerObject

Returns the value of attribute stack_pointer.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def stack_pointer
  @stack_pointer
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def status
  @status
end

#xObject

Returns the value of attribute x.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def x
  @x
end

#yObject

Returns the value of attribute y.



14
15
16
# File 'lib/badline/cpu.rb', line 14

def y
  @y
end

Instance Method Details

#inspectObject



48
49
50
51
52
# File 'lib/badline/cpu.rb', line 48

def inspect
  "Cycles: #{@cycles}, PC: #{format16(program_counter)}, " \
    "SP: #{format8(stack_pointer)}, A: #{format8(a)}, X: #{format8(x)}, " \
    "Y: #{format8(y)}, P: #{format8(p)}"
end

#pObject



35
36
37
# File 'lib/badline/cpu.rb', line 35

def p
  status.value
end

#p=(new_value) ⇒ Object



39
40
41
# File 'lib/badline/cpu.rb', line 39

def p=(new_value)
  status.value = new_value
end

#reset!Object



30
31
32
33
# File 'lib/badline/cpu.rb', line 30

def reset!
  status.interrupt = true
  reset_registers
end

#step!Object



43
44
45
46
# File 'lib/badline/cpu.rb', line 43

def step!
  @loop.resume unless @instruction || @interrupt
  cycle! while @instruction || @interrupt
end