Class: R6502::Cpu
- Inherits:
-
Object
- Object
- R6502::Cpu
- Defined in:
- lib/r6502/instr_table.rb,
lib/r6502/cpu_execution.rb,
lib/r6502/cpu_instructions.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#b ⇒ Object
Returns the value of attribute b.
-
#c ⇒ Object
Returns the value of attribute c.
-
#d ⇒ Object
Returns the value of attribute d.
-
#i ⇒ Object
Returns the value of attribute i.
-
#mem ⇒ Object
Returns the value of attribute mem.
-
#n ⇒ Object
Returns the value of attribute n.
-
#pc ⇒ Object
Returns the value of attribute pc.
-
#s ⇒ Object
Returns the value of attribute s.
-
#v ⇒ Object
Returns the value of attribute v.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
-
#z ⇒ Object
Returns the value of attribute z.
Instance Method Summary collapse
-
#adc(arg, mode) ⇒ Object
add with carry.
-
#and(arg, mode) ⇒ Object
logical and.
-
#asl(arg, mode) ⇒ Object
shift left.
- #bcc(arg, mode) ⇒ Object
- #bcs(arg, mode) ⇒ Object
- #beq(arg, mode) ⇒ Object
-
#bit(arg, mode) ⇒ Object
logical and (result discarded).
- #bmi(arg, mode) ⇒ Object
- #bne(arg, mode) ⇒ Object
- #bpl(arg, mode) ⇒ Object
- #brk(arg, mode) ⇒ Object
- #bvc(arg, mode) ⇒ Object
- #bvs(arg, mode) ⇒ Object
- #clc(arg, mode) ⇒ Object
- #cld(arg, mode) ⇒ Object
- #cli(arg, mode) ⇒ Object
- #clv(arg, mode) ⇒ Object
- #cmp(arg, mode) ⇒ Object
- #cpx(arg, mode) ⇒ Object
- #cpy(arg, mode) ⇒ Object
-
#dec(arg, mode) ⇒ Object
decrement (memory).
- #decode_arg(mode, sec_word, thd_word) ⇒ Object
-
#dex(arg, mode) ⇒ Object
decrement (x).
-
#dey(arg, mode) ⇒ Object
decrement (y).
-
#eor(arg, mode) ⇒ Object
exclusive or.
-
#inc(arg, mode) ⇒ Object
increment (memory).
- #inc_pc_by_mode(mode) ⇒ Object
-
#initialize(mem) ⇒ Cpu
constructor
A new instance of Cpu.
- #instr_mode(opcode) ⇒ Object
-
#inx(arg, mode) ⇒ Object
increment (x).
-
#iny(arg, mode) ⇒ Object
increment (y).
- #jmp(arg, mode) ⇒ Object
- #jsr(arg, mode) ⇒ Object
- #lda(arg, mode) ⇒ Object
- #ldx(arg, mode) ⇒ Object
- #ldy(arg, mode) ⇒ Object
-
#lsr(arg, mode) ⇒ Object
shift right.
-
#nop(arg, mode) ⇒ Object
no operation.
-
#ora(arg, mode) ⇒ Object
inclusive or.
- #pha(arg, mode) ⇒ Object
- #php(arg, mode) ⇒ Object
- #pla(arg, mode) ⇒ Object
- #plp(arg, mode) ⇒ Object
-
#rol(arg, mode) ⇒ Object
rotate left.
-
#ror(arg, mode) ⇒ Object
rotate right.
- #rti(arg, mode) ⇒ Object
- #rts(arg, mode) ⇒ Object
-
#sbc(arg, mode) ⇒ Object
subtract with carry.
- #sec(arg, mode) ⇒ Object
- #sed(arg, mode) ⇒ Object
- #sei(arg, mode) ⇒ Object
- #sta(arg, mode) ⇒ Object
- #step ⇒ Object
- #stx(arg, mode) ⇒ Object
- #sty(arg, mode) ⇒ Object
- #tax(arg, mode) ⇒ Object
- #tay(arg, mode) ⇒ Object
- #tsx(arg, mode) ⇒ Object
- #txa(arg, mode) ⇒ Object
- #txs(arg, mode) ⇒ Object
- #tya(arg, mode) ⇒ Object
Constructor Details
#initialize(mem) ⇒ Cpu
Returns a new instance of Cpu.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/r6502/cpu_execution.rb', line 5 def initialize(mem) @mem = mem @pc = @mem.get_word(0xfffc) @s = 0xff @x = 0x00 @y = 0x00 @a = 0x00 @c = 0 @z = 0 @i = 0 @d = 0 @b = 0 @v = 0 @n = 0 end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def a @a end |
#b ⇒ Object
Returns the value of attribute b.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def b @b end |
#c ⇒ Object
Returns the value of attribute c.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def c @c end |
#d ⇒ Object
Returns the value of attribute d.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def d @d end |
#i ⇒ Object
Returns the value of attribute i.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def i @i end |
#mem ⇒ Object
Returns the value of attribute mem.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def mem @mem end |
#n ⇒ Object
Returns the value of attribute n.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def n @n end |
#pc ⇒ Object
Returns the value of attribute pc.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def pc @pc end |
#s ⇒ Object
Returns the value of attribute s.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def s @s end |
#v ⇒ Object
Returns the value of attribute v.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def v @v end |
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/r6502/cpu_execution.rb', line 3 def y @y end |
#z ⇒ Object
Returns the value of attribute z.
4 5 6 |
# File 'lib/r6502/cpu_execution.rb', line 4 def z @z end |
Instance Method Details
#adc(arg, mode) ⇒ Object
add with carry
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/r6502/cpu_instructions.rb', line 4 def adc(arg, mode) x = @a y = mode == :imm ? arg : @mem.get(arg) if @d == 0 #normal binary mode r = x + y + @c @a = 0xff & r @v = (((0x7f&x) + (0x7f&y) + @c)>>7) ^ ((x + y + @c)>>8) @z = (r&0xff).zero? ? 1 : 0 @c = r > 255 ? 1 : 0 @n = (0x80&r)>>7 else #BCD mode ones = (0xf&x) + (0xf&y) tens = ((0xf0&x)>>4) + ((0xf0&y)>>4) r0 = ones + 10*tens + @c @c = r0 > 99 ? 1 : 0 r = r0 % 100 @z = r.zero? ? 1 : 0 @a = r + 6*((r/10).floor) @n = (0x80&@a)>>7 end inc_pc_by_mode(mode) end |
#and(arg, mode) ⇒ Object
logical and
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/r6502/cpu_instructions.rb', line 51 def and(arg, mode) case mode when :imm @a = @a & arg else @a = @a & @mem.get(arg) end @z = @a.zero? ? 1 : 0 @n = @a>>7 inc_pc_by_mode(mode) end |
#asl(arg, mode) ⇒ Object
shift left
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/r6502/cpu_instructions.rb', line 63 def asl(arg, mode) case mode when :acc r = @a<<1 @a = r&0xff @z = @a.zero? ? 1 : 0 @n = @a>>7 @c = r > 0xff ? 1 : 0 else r = @mem.get(arg)<<1 @mem.set( arg, r&0xff ) @z = r.zero? ? 1 : 0 @n = r>>7 @c = r > 0xff ? 1 : 0 end inc_pc_by_mode(mode) end |
#bcc(arg, mode) ⇒ Object
236 237 238 239 |
# File 'lib/r6502/cpu_instructions.rb', line 236 def bcc(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @c == 0 end |
#bcs(arg, mode) ⇒ Object
240 241 242 243 |
# File 'lib/r6502/cpu_instructions.rb', line 240 def bcs(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @c == 1 end |
#beq(arg, mode) ⇒ Object
244 245 246 247 |
# File 'lib/r6502/cpu_instructions.rb', line 244 def beq(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @z == 1 end |
#bit(arg, mode) ⇒ Object
logical and (result discarded)
81 82 83 84 85 86 87 88 89 |
# File 'lib/r6502/cpu_instructions.rb', line 81 def bit(arg, mode) m = @mem.get( arg ) a = @a result = a & m @z = result.zero? ? 1 : 0 @v = (m & 0x40)>>6 @n = (m & 0x80)>>7 inc_pc_by_mode(mode) end |
#bmi(arg, mode) ⇒ Object
248 249 250 251 |
# File 'lib/r6502/cpu_instructions.rb', line 248 def bmi(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @n == 1 end |
#bne(arg, mode) ⇒ Object
252 253 254 255 |
# File 'lib/r6502/cpu_instructions.rb', line 252 def bne(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @z == 0 end |
#bpl(arg, mode) ⇒ Object
256 257 258 259 |
# File 'lib/r6502/cpu_instructions.rb', line 256 def bpl(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @n == 0 end |
#brk(arg, mode) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/r6502/cpu_instructions.rb', line 457 def brk(arg, mode) @mem.set(0x0100 + @s, @pc>>8) @s -= 1 @mem.set(0x0100 + @s, (0xff&@pc)) @s -= 1 val = @n #bit 7 val = (val<<1) + @v #bit 6 val = (val<<1) + 1 #bit 5 val = (val<<1) + 1 #bit 4 break flag val = (val<<1) + @d #bit 3 val = (val<<1) + @i #bit 2 val = (val<<1) + @z #bit 1 val = (val<<1) + @c #bit 0 @mem.set(0x0100 + @s, val) @s -= 1 lo = @mem.get(0xfffe) hi = @mem.get(0xffff) @pc = (hi<<8) + lo end |
#bvc(arg, mode) ⇒ Object
260 261 262 263 |
# File 'lib/r6502/cpu_instructions.rb', line 260 def bvc(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @v == 0 end |
#bvs(arg, mode) ⇒ Object
264 265 266 267 |
# File 'lib/r6502/cpu_instructions.rb', line 264 def bvs(arg, mode) inc_pc_by_mode(:rel) @pc += arg if @v == 1 end |
#clc(arg, mode) ⇒ Object
268 269 270 271 |
# File 'lib/r6502/cpu_instructions.rb', line 268 def clc(arg, mode) @c = 0 inc_pc_by_mode(mode) end |
#cld(arg, mode) ⇒ Object
272 273 274 275 |
# File 'lib/r6502/cpu_instructions.rb', line 272 def cld(arg, mode) @d = 0 inc_pc_by_mode(mode) end |
#cli(arg, mode) ⇒ Object
276 277 278 279 |
# File 'lib/r6502/cpu_instructions.rb', line 276 def cli(arg, mode) @i = 0 inc_pc_by_mode(mode) end |
#clv(arg, mode) ⇒ Object
280 281 282 283 |
# File 'lib/r6502/cpu_instructions.rb', line 280 def clv(arg, mode) @v = 0 inc_pc_by_mode(mode) end |
#cmp(arg, mode) ⇒ Object
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/r6502/cpu_instructions.rb', line 284 def cmp(arg, mode) case mode when :imm result = @a - arg @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 else m = @mem.get( arg ) result = @a - m @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 end inc_pc_by_mode(mode) end |
#cpx(arg, mode) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/r6502/cpu_instructions.rb', line 300 def cpx(arg, mode) case mode when :imm result = @x - arg @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 else m = @mem.get( arg ) result = @x - m @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 end inc_pc_by_mode(mode) end |
#cpy(arg, mode) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/r6502/cpu_instructions.rb', line 316 def cpy(arg, mode) case mode when :imm result = @y - arg @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 else m = @mem.get( arg ) result = @y - m @c = result >= 0 ? 1 : 0 @z = result == 0 ? 1 : 0 @n = (0xff&result)>>7 end inc_pc_by_mode(mode) end |
#dec(arg, mode) ⇒ Object
decrement (memory)
91 92 93 94 95 96 97 |
# File 'lib/r6502/cpu_instructions.rb', line 91 def dec(arg, mode) r = @mem.get(arg) - 1 @mem.set( arg, r&0xff ) @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 inc_pc_by_mode(mode) end |
#decode_arg(mode, sec_word, thd_word) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/r6502/cpu_execution.rb', line 29 def decode_arg(mode, sec_word, thd_word) case mode when :imp nil when :acc nil when :imm sec_word when :zp sec_word when :zpx sec_word + @x when :zpy sec_word + @y when :rel sec_word <= 127 ? sec_word : sec_word - 256 when :abs (thd_word<<8) + sec_word when :absx (thd_word<<8) + sec_word + @x when :absy (thd_word<<8) + sec_word + @y when :ind lb = @mem.get( (thd_word<<8) + sec_word ) hb = @mem.get( (thd_word<<8) + sec_word + 1 ) (hb<<8) + lb when :indx lb = @mem.get( 0xff & (@x + sec_word) ) hb = @mem.get( 0xff & (@x + sec_word) + 1 ) @mem.get( (hb<<8) + lb ) when :indy lb = @mem.get( 0xff & sec_word ) hb = @mem.get( 0xff & sec_word + 1) addr = (hb<<8) + lb addr + @y end end |
#dex(arg, mode) ⇒ Object
decrement (x)
99 100 101 102 103 104 105 |
# File 'lib/r6502/cpu_instructions.rb', line 99 def dex(arg, mode) r = @x - 1 @x = r&0xff @z = @x.zero? ? 1 : 0 @n = (@x&0x80)>>7 inc_pc_by_mode(mode) end |
#dey(arg, mode) ⇒ Object
decrement (y)
107 108 109 110 111 112 113 |
# File 'lib/r6502/cpu_instructions.rb', line 107 def dey(arg, mode) r = @y - 1 @y = r&0xff @z = @y.zero? ? 1 : 0 @n = (@y&0x80)>>7 inc_pc_by_mode(mode) end |
#eor(arg, mode) ⇒ Object
exclusive or
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/r6502/cpu_instructions.rb', line 115 def eor(arg, mode) case mode when :imm @a = @a ^ arg else @a = @a ^ @mem.get(arg) end @z = @a.zero? ? 1 : 0 @n = (@a&0x80)>>7 inc_pc_by_mode(mode) end |
#inc(arg, mode) ⇒ Object
increment (memory)
127 128 129 130 131 132 133 |
# File 'lib/r6502/cpu_instructions.rb', line 127 def inc(arg, mode) r = (@mem.get(arg) + 1)&0xff @mem.set( arg, r ) @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 inc_pc_by_mode(mode) end |
#inc_pc_by_mode(mode) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/r6502/cpu_execution.rb', line 66 def inc_pc_by_mode(mode) case mode when :imp @pc += 1 when :acc @pc += 1 when :imm @pc += 2 when :zp @pc += 2 when :zpx @pc += 2 when :zpy @pc += 2 when :abs @pc += 3 when :absx @pc += 3 when :absy @pc += 3 when :indx @pc += 2 when :indy @pc += 2 when :rel @pc += 2 when :ind #only used by jmp, which explicitly sets the pc, @pc += 3 #so hopefully this is never used here. end end |
#instr_mode(opcode) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/r6502/instr_table.rb', line 3 def instr_mode(opcode) #adc { 0x69 => [:adc, :imm], 0x65 => [:adc, :zp], 0x75 => [:adc, :zpx], 0x6d => [:adc, :abs], 0x7d => [:adc, :absx], 0x79 => [:adc, :absy], 0x61 => [:adc, :indx], 0x71 => [:adc, :indy], #and 0x29 => [:and, :imm], 0x25 => [:and, :zp], 0x35 => [:and, :zpx], 0x2d => [:and, :abs], 0x3d => [:and, :absx], 0x39 => [:and, :absy], 0x21 => [:and, :indx], 0x31 => [:and, :indy], #asl 0x0a => [:asl, :acc], 0x06 => [:asl, :zp], 0x16 => [:asl, :zpx], 0x0e => [:asl, :abs], 0x1e => [:asl, :absx], #bit 0x24 => [:bit, :zp], 0x2c => [:bit, :abs], #bpl 0x10 => [:bpl, :rel], #bmi 0x30 => [:bmi, :rel], #bvc 0x50 => [:bvc, :rel], #bvs 0x70 => [:bvs, :rel], #bcc 0x90 => [:bcc, :rel], #bcs 0xb0 => [:bcs, :rel], #bne 0xd0 => [:bne, :rel], #beq 0xf0 => [:beq, :rel], #brk 0x00 => [:brk, :imp], #cmp 0xc9 => [:cmp, :imm], 0xc5 => [:cmp, :zp], 0xd5 => [:cmp, :zpx], 0xcd => [:cmp, :abs], 0xdd => [:cmp, :absx], 0xd9 => [:cmp, :absy], 0xc1 => [:cmp, :indx], 0xd1 => [:cmp, :indy], #cpx 0xe0 => [:cpx, :imm], 0xe4 => [:cpx, :zp], 0xec => [:cpx, :abs], #cpy 0xc0 => [:cpy, :imm], 0xc4 => [:cpy, :zp], 0xcc => [:cpy, :abs], #dec 0xc6 => [:dec, :zp], 0xd6 => [:dec, :zpx], 0xce => [:dec, :abs], 0xde => [:dec, :absx], #eor 0x49 => [:eor, :imm], 0x45 => [:eor, :zp], 0x55 => [:eor, :zpx], 0x4d => [:eor, :abs], 0x5d => [:eor, :absx], 0x59 => [:eor, :absy], 0x41 => [:eor, :indx], 0x51 => [:eor, :indy], #clc 0x18 => [:clc, :imp], #sec 0x38 => [:sec, :imp], #cli 0x58 => [:cli, :imp], #sei 0x78 => [:sei, :imp], #clv 0xb8 => [:clv, :imp], #cld 0xd8 => [:cld, :imp], #sed 0xf8 => [:sed, :imp], #inc 0xe6 => [:inc, :zp], 0xf6 => [:inc, :zpx], 0xee => [:inc, :abs], 0xfe => [:inc, :absx], #jmp, 0x4c => [:jmp, :abs], 0x6c => [:jmp, :ind], #jsr, 0x20 => [:jsr, :abs], #lda, 0xa9 => [:lda, :imm], 0xa5 => [:lda, :zp], 0xb5 => [:lda, :zpx], 0xad => [:lda, :abs], 0xbd => [:lda, :absx], 0xb9 => [:lda, :absy], 0xa1 => [:lda, :indx], 0xb1 => [:lda, :indy], #ldx, 0xa2 => [:ldx, :imm], 0xa6 => [:ldx, :zp], 0xb6 => [:ldx, :zpy], 0xae => [:ldx, :abs], 0xbe => [:ldx, :absy], #ldy, 0xa0 => [:ldy, :imm], 0xa4 => [:ldy, :zp], 0xb4 => [:ldy, :zpx], 0xac => [:ldy, :abs], 0xbc => [:ldy, :absx], #lsr, 0x4a => [:lsr, :acc], 0x46 => [:lsr, :zp], 0x56 => [:lsr, :zpx], 0x4e => [:lsr, :abs], 0x5e => [:lsr, :absx], #nop, 0xea => [:nop, :imp], #ora, 0x09 => [:ora, :imm], 0x05 => [:ora, :zp], 0x15 => [:ora, :zpx], 0x0d => [:ora, :abs], 0x1d => [:ora, :absx], 0x19 => [:ora, :absy], 0x01 => [:ora, :indx], 0x11 => [:ora, :indy], #tax, 0xaa => [:tax, :imp], #txa, 0x8a => [:txa, :imp], #dex, 0xca => [:dex, :imp], #inx, 0xe8 => [:inx, :imp], #tay, 0xa8 => [:tay, :imp], #tya, 0x98 => [:tya, :imp], #dey, 0x88 => [:dey, :imp], #iny, 0xc8 => [:iny, :imp], #rol, 0x2a => [:rol, :acc], 0x26 => [:rol, :zp], 0x36 => [:rol, :zpx], 0x2e => [:rol, :abs], 0x3e => [:rol, :absx], #ror, 0x6a => [:ror, :acc], 0x66 => [:ror, :zp], 0x76 => [:ror, :zpx], 0x6e => [:ror, :abs], 0x7e => [:ror, :absx], #rti, 0x40 => [:rti, :imp], #rts, 0x60 => [:rts, :imp], #sbc, 0xe9 => [:sbc, :imm], 0xe5 => [:sbc, :zp], 0xf5 => [:sbc, :zpx], 0xed => [:sbc, :abs], 0xfd => [:sbc, :absx], 0xf9 => [:sbc, :absy], 0xe1 => [:sbc, :indx], 0xf1 => [:sbc, :indy], #sta, 0x85 => [:sta, :zp], 0x95 => [:sta, :zpx], 0x8d => [:sta, :abs], 0x9d => [:sta, :absx], 0x99 => [:sta, :absy], 0x81 => [:sta, :indx], 0x91 => [:sta, :indy], #txs, 0x9a => [:txs, :imp], #tsx, 0xba => [:tsx, :imp], #pha, 0x48 => [:pha, :imp], #pla, 0x68 => [:pla, :imp], #php, 0x08 => [:php, :imp], #plp, 0x28 => [:plp, :imp], #stx, 0x86 => [:stx, :zp], 0x96 => [:stx, :zpy], 0x8e => [:stx, :abs], #sty, 0x84 => [:sty, :zp], 0x94 => [:sty, :zpx], 0x8c => [:sty, :abs] }[opcode] end |
#inx(arg, mode) ⇒ Object
increment (x)
135 136 137 138 139 140 141 |
# File 'lib/r6502/cpu_instructions.rb', line 135 def inx(arg, mode) r = (@x + 1)&0xff @x = r @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 inc_pc_by_mode(mode) end |
#iny(arg, mode) ⇒ Object
increment (y)
143 144 145 146 147 148 149 |
# File 'lib/r6502/cpu_instructions.rb', line 143 def iny(arg, mode) r = (@y + 1)&0xff @y = r @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 inc_pc_by_mode(mode) end |
#jmp(arg, mode) ⇒ Object
332 333 334 |
# File 'lib/r6502/cpu_instructions.rb', line 332 def jmp(arg, mode) @pc = arg end |
#jsr(arg, mode) ⇒ Object
496 497 498 499 500 501 502 |
# File 'lib/r6502/cpu_instructions.rb', line 496 def jsr( arg, mode ) @mem.set(0x0100 + @s, (@pc+2)>>8) @s -= 1 @mem.set(0x0100 + @s, 0xff&(@pc+2)) @s -= 1 @pc = arg end |
#lda(arg, mode) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/r6502/cpu_instructions.rb', line 335 def lda(arg, mode) case mode when :imm @a = arg else @a = @mem.get( arg ) end @z = @a.zero? ? 1 : 0 @n = (0x80&@a)>>7 inc_pc_by_mode(mode) end |
#ldx(arg, mode) ⇒ Object
346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/r6502/cpu_instructions.rb', line 346 def ldx(arg, mode) case mode when :imm @x = arg else @x = @mem.get( arg ) end @z = @x.zero? ? 1 : 0 @n = (0x80&@x)>>7 inc_pc_by_mode(mode) end |
#ldy(arg, mode) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/r6502/cpu_instructions.rb', line 357 def ldy(arg, mode) case mode when :imm @y = arg else @y = @mem.get( arg ) end @z = @y.zero? ? 1 : 0 @n = (0x80&@y)>>7 inc_pc_by_mode(mode) end |
#lsr(arg, mode) ⇒ Object
shift right
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/r6502/cpu_instructions.rb', line 151 def lsr(arg, mode) case mode when :acc @c = 0x01&@a @a = @a>>1 @z = @a.zero? ? 1 : 0 @n = (@a&0x80)>>7 #bit 7 will always be zero, though else v = @mem.get(arg) @c = 0x01&v r = v>>1 @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 @mem.set( arg, r ) end inc_pc_by_mode(mode) end |
#nop(arg, mode) ⇒ Object
no operation
221 222 223 |
# File 'lib/r6502/cpu_instructions.rb', line 221 def nop(arg, mode) inc_pc_by_mode(mode) end |
#ora(arg, mode) ⇒ Object
inclusive or
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/r6502/cpu_instructions.rb', line 169 def ora(arg, mode) case mode when :imm @a = @a | arg else @a = @a | @mem.get( arg ) end @z = @a.zero? ? 1 : 0 @n = (@a&0x80)>>7 inc_pc_by_mode(mode) end |
#pha(arg, mode) ⇒ Object
368 369 370 371 372 373 |
# File 'lib/r6502/cpu_instructions.rb', line 368 def pha(arg, mode) addr = 0x0100 + (0xff & @s) @mem.set( addr, @a ) @s -= 1 inc_pc_by_mode(mode) end |
#php(arg, mode) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/r6502/cpu_instructions.rb', line 382 def php(arg, mode) addr = 0x0100 + (0xff & @s) val = @n #bit 7 val = (val<<1) + @v #bit 6 val = (val<<1) + 1 #bit 5, reserved val = (val<<1) + 1 #bit 4, break val = (val<<1) + @d #bit 3 val = (val<<1) + @i #bit 2 val = (val<<1) + @z #bit 1 val = (val<<1) + @c #bit 0 @mem.set( addr, val ) @s -= 1 inc_pc_by_mode(mode) end |
#pla(arg, mode) ⇒ Object
374 375 376 377 378 379 380 381 |
# File 'lib/r6502/cpu_instructions.rb', line 374 def pla(arg, mode) addr = 0x0100 + (0xff & (@s + 1)) @a = @mem.get( addr ) @s += 1 @z = @a.zero? ? 1 : 0 @n = (0x80&@a)>>7 inc_pc_by_mode(mode) end |
#plp(arg, mode) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/r6502/cpu_instructions.rb', line 397 def plp(arg, mode) addr = 0x0100 + (0xff & (@s + 1)) val = @mem.get( addr ) @c = 0x1 & val @z = 0x1 & (val>>1) @i = 0x1 & (val>>2) @d = 0x1 & (val>>3) @b = 0x1 & (val>>4) # bit 5 @v = 0x1 & (val>>6) @n = 0x1 & (val>>7) @s += 1 inc_pc_by_mode(mode) end |
#rol(arg, mode) ⇒ Object
rotate left
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/r6502/cpu_instructions.rb', line 181 def rol(arg, mode) case mode when :acc c = @c @c = (@a&0x80)>>7 @a = 0xff & (@a<<1) | c @z = @a.zero? ? 1 : 0 @n = (@a&0x80)>>7 else val = @mem.get(arg) c = @c @c = (val&0x80)>>7 r = 0xff & (val<<1) | c @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 @mem.set(arg, r) end inc_pc_by_mode(mode) end |
#ror(arg, mode) ⇒ Object
rotate right
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/r6502/cpu_instructions.rb', line 201 def ror(arg, mode) case mode when :acc c = @c @c = @a&0x01 @a = (@a>>1) | (c<<7) @z = @a.zero? ? 1 : 0 @n = (@a&0x80)>>7 else val = @mem.get(arg) c = @c @c = val&0x01 r = (val>>1) | (c<<7) @mem.set(arg, r) @z = r.zero? ? 1 : 0 @n = (r&0x80)>>7 end inc_pc_by_mode(mode) end |
#rti(arg, mode) ⇒ Object
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/r6502/cpu_instructions.rb', line 477 def rti( arg, mode ) flags = @mem.get(0x0100 + @s + 1) @s += 1 @c = 0x1 & flags @z = 0x1 & (flags>>1) @i = 0x1 & (flags>>2) @d = 0x1 & (flags>>3) @b = 0x1 & (flags>>4) # bit 5 @v = 0x1 & (flags>>6) @n = 0x1 & (flags>>7) hi = @mem.get(0x0100 + @s + 1) @s += 1 lo = @mem.get(0x0100 + @s + 1) @s += 1 @pc = 0xffff&((hi<<8) + lo) end |
#rts(arg, mode) ⇒ Object
503 504 505 506 507 508 509 |
# File 'lib/r6502/cpu_instructions.rb', line 503 def rts( arg, mode ) lo = @mem.get(0x0100 + @s + 1) @s += 1 hi = @mem.get(0x0100 + @s + 1) @s += 1 @pc = 0xffff&((hi<<8) + lo + 1) end |
#sbc(arg, mode) ⇒ Object
subtract with carry
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/r6502/cpu_instructions.rb', line 27 def sbc(arg, mode) x = @a y = mode == :imm ? arg : @mem.get(arg) if @d == 0 #normal binary mode y = (y^0xff) r = x + y + @c @a = 0xff & r @v = (((0x7f&x) + (0x7f&y) + @c)>>7) ^ ((x + y + @c)>>8) @z = (0xff&r).zero? ? 1 : 0 @c = r > 255 ? 1 : 0 @n = (0x80&r)>>7 else #BCD mode ones = (0xf&x) - (0xf&y) tens = ((0xf0&x)>>4) - ((0xf0&y)>>4) r0 = ones + 10*tens - (1 - @c) @c = r0 >= 0 ? 1 : 0 r = r0 % 100 @z = r.zero? ? 1 : 0 @a = r + 6*((r/10).floor) @n = (0x80&@a)>>7 end inc_pc_by_mode(mode) end |
#sec(arg, mode) ⇒ Object
224 225 226 227 |
# File 'lib/r6502/cpu_instructions.rb', line 224 def sec(arg, mode) @c = 1 inc_pc_by_mode(mode) end |
#sed(arg, mode) ⇒ Object
228 229 230 231 |
# File 'lib/r6502/cpu_instructions.rb', line 228 def sed(arg, mode) @d = 1 inc_pc_by_mode(mode) end |
#sei(arg, mode) ⇒ Object
232 233 234 235 |
# File 'lib/r6502/cpu_instructions.rb', line 232 def sei(arg, mode) @i = 1 inc_pc_by_mode(mode) end |
#sta(arg, mode) ⇒ Object
411 412 413 414 |
# File 'lib/r6502/cpu_instructions.rb', line 411 def sta(arg, mode) @mem.set( arg, @a ) inc_pc_by_mode(mode) end |
#step ⇒ Object
20 21 22 23 24 25 26 27 28 |
# File 'lib/r6502/cpu_execution.rb', line 20 def step instr, mode = instr_mode( mem.get(pc) ) arg = decode_arg( mode, mem.get(pc+1), mem.get(pc+2) ) puts "instr: #{instr} at pc 0x#{pc.to_s(16)} with arg #{arg.to_i.to_s(16)}" method( instr ).call( arg, mode ) puts " a: #{a.to_s(16)} x: #{x.to_s(16)} y: #{y.to_s(16)} z: #{z.to_s(16)} n: #{n.to_s(16)} c: #{c.to_s(16)}" puts "===" end |
#stx(arg, mode) ⇒ Object
415 416 417 418 |
# File 'lib/r6502/cpu_instructions.rb', line 415 def stx(arg, mode) @mem.set( arg, @x ) inc_pc_by_mode(mode) end |
#sty(arg, mode) ⇒ Object
419 420 421 422 |
# File 'lib/r6502/cpu_instructions.rb', line 419 def sty(arg, mode) @mem.set( arg, @y ) inc_pc_by_mode(mode) end |
#tax(arg, mode) ⇒ Object
423 424 425 426 427 428 |
# File 'lib/r6502/cpu_instructions.rb', line 423 def tax(arg, mode) @x = @a @z = @x.zero? ? 1 : 0 @n = (0x80&@x)>>7 inc_pc_by_mode(mode) end |
#tay(arg, mode) ⇒ Object
429 430 431 432 433 434 |
# File 'lib/r6502/cpu_instructions.rb', line 429 def tay(arg, mode) @y = @a @z = @y.zero? ? 1 : 0 @n = (0x80&@y)>>7 inc_pc_by_mode(mode) end |
#tsx(arg, mode) ⇒ Object
435 436 437 438 439 440 |
# File 'lib/r6502/cpu_instructions.rb', line 435 def tsx(arg, mode) @x = @s @z = @x.zero? ? 1 : 0 @n = (0x80&@x)>>7 inc_pc_by_mode(mode) end |
#txa(arg, mode) ⇒ Object
441 442 443 444 445 446 |
# File 'lib/r6502/cpu_instructions.rb', line 441 def txa(arg, mode) @a = @x @z = @a.zero? ? 1 : 0 @n = (0x80&@a)>>7 inc_pc_by_mode(mode) end |
#txs(arg, mode) ⇒ Object
447 448 449 450 |
# File 'lib/r6502/cpu_instructions.rb', line 447 def txs(arg, mode) @s = @x inc_pc_by_mode(mode) end |
#tya(arg, mode) ⇒ Object
451 452 453 454 455 456 |
# File 'lib/r6502/cpu_instructions.rb', line 451 def tya(arg, mode) @a = @y @z = @a.zero? ? 1 : 0 @n = (0x80&@a)>>7 inc_pc_by_mode(mode) end |