Class: Badline::CPU
- 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
-
#a ⇒ Object
Returns the value of attribute a.
-
#boundary_crossed ⇒ Object
readonly
Returns the value of attribute boundary_crossed.
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#irq ⇒ Object
Returns the value of attribute irq.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#nmi ⇒ Object
Returns the value of attribute nmi.
-
#program_counter ⇒ Object
Returns the value of attribute program_counter.
-
#stack_pointer ⇒ Object
Returns the value of attribute stack_pointer.
-
#status ⇒ Object
Returns the value of attribute status.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Attributes inherited from Cycleable
Instance Method Summary collapse
-
#initialize(memory = nil, debug: false) ⇒ CPU
constructor
A new instance of CPU.
- #inspect ⇒ Object
- #p ⇒ Object
- #p=(new_value) ⇒ Object
- #reset! ⇒ Object
- #step! ⇒ Object
Methods included from Traps
Methods included from InstructionSet
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
Methods included from IntegerHelper
#bcd, #bcd_to_i, #format16, #format8, #high_byte, #low_byte, #signed_int8, #uint16
Methods inherited from Cycleable
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
#a ⇒ Object
Returns the value of attribute a.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def a @a end |
#boundary_crossed ⇒ Object (readonly)
Returns the value of attribute boundary_crossed.
13 14 15 |
# File 'lib/badline/cpu.rb', line 13 def boundary_crossed @boundary_crossed end |
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
13 14 15 |
# File 'lib/badline/cpu.rb', line 13 def instructions @instructions end |
#irq ⇒ Object
Returns the value of attribute irq.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def irq @irq end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
13 14 15 |
# File 'lib/badline/cpu.rb', line 13 def memory @memory end |
#nmi ⇒ Object
Returns the value of attribute nmi.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def nmi @nmi end |
#program_counter ⇒ Object
Returns the value of attribute program_counter.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def program_counter @program_counter end |
#stack_pointer ⇒ Object
Returns the value of attribute stack_pointer.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def stack_pointer @stack_pointer end |
#status ⇒ Object
Returns the value of attribute status.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def status @status end |
#x ⇒ Object
Returns the value of attribute x.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
14 15 16 |
# File 'lib/badline/cpu.rb', line 14 def y @y end |
Instance Method Details
#inspect ⇒ Object
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 |
#p ⇒ Object
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 |