Skip to content
This repository
tree: f2f90da81e
Fetching contributors…

Cannot retrieve contributors at this time

file 427 lines (404 sloc) 10.741 kb
1 2 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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
package main

import (
"fmt"
)

var (
c *Cpu
pc uint16
)

func immediateAddress() int {
val, _ := Ram.Read(pc - 1)
return int(val)
}

func absoluteAddress() (result int) {
// Switch to an int (or more appropriately uint16) since we
// will overflow when shifting the high byte
high, _ := Ram.Read(pc + 1)
low, _ := Ram.Read(pc)

return (int(high) << 8) + int(low)
}

func zeroPageAddress() int {
res, _ := Ram.Read(pc)

return int(res)
}

func indirectAbsoluteAddress() (result int) {
high, _ := Ram.Read(pc + 1)
low, _ := Ram.Read(pc)

result = (int(high) << 8) + int(low)
pc++
return
}

func absoluteIndexedAddress(index Word) (result int) {
// Switch to an int (or more appropriately uint16) since we
// will overflow when shifting the high byte
high, _ := Ram.Read(pc + 1)
low, _ := Ram.Read(pc)

return (int(high) << 8) + int(low) + int(index)
}

func zeroPageIndexedAddress(index Word) int {
location, _ := Ram.Read(pc)
return int(location + index)
}

func indexedIndirectAddress() int {
location, _ := Ram.Read(pc)
location = location + c.X

// Switch to an int (or more appropriately uint16) since we
// will overflow when shifting the high byte
high, _ := Ram.Read(location + 1)
low, _ := Ram.Read(location)

return (int(high) << 8) + int(low)
}

func indirectIndexedAddress() int {
location, _ := Ram.Read(pc)

// Switch to an int (or more appropriately uint16) since we
// will overflow when shifting the high byte
high, _ := Ram.Read(location + 1)
low, _ := Ram.Read(location)

return (int(high) << 8) + int(low) + int(c.Y)
}

func relativeAddress() int {
return 0
}

func accumulatorAddress() int {
return 0
}

func Disassemble(opcode Word, cpu *Cpu, p uint16) {
c = cpu
pc = p

fmt.Printf("0x%X: 0x%X ", pc-1, opcode)

switch opcode {
// ADC
case 0x69:
fmt.Printf("ADC $%X\n", immediateAddress())
case 0x65:
fmt.Printf("ADC $%X\n", zeroPageAddress())
case 0x75:
fmt.Printf("ADC $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x6D:
fmt.Printf("ADC $%X\n", absoluteAddress())
case 0x7D:
fmt.Printf("ADC $%X,X\n", absoluteIndexedAddress(c.X))
case 0x79:
fmt.Printf("ADC $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0x61:
fmt.Printf("ADC ($%X,X)\n", indexedIndirectAddress())
case 0x71:
fmt.Printf("ADC ($%X),Y\n", indirectIndexedAddress())
// LDA
case 0xA9:
fmt.Printf("LDA $%X\n", immediateAddress())
case 0xA5:
fmt.Printf("LDA $%X\n", zeroPageAddress())
case 0xB5:
fmt.Printf("LDA $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xAD:
fmt.Printf("LDA $%X\n", absoluteAddress())
case 0xBD:
fmt.Printf("LDA $%X,X\n", absoluteIndexedAddress(c.X))
case 0xB9:
fmt.Printf("LDA $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0xA1:
fmt.Printf("LDA ($%X,X)\n", indexedIndirectAddress())
case 0xB1:
fmt.Printf("LDA ($%X),Y\n", indirectIndexedAddress())
// LDX
case 0xA2:
fmt.Printf("LDX $%X\n", immediateAddress())
case 0xA6:
fmt.Printf("LDX $%X\n", zeroPageAddress())
case 0xB6:
fmt.Printf("LDX $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xAE:
fmt.Printf("LDX $%X\n", absoluteAddress())
case 0xBE:
fmt.Printf("LDX $%X,Y\n", absoluteIndexedAddress(c.Y))
// LDY
case 0xA0:
fmt.Printf("LDY $%X\n", immediateAddress())
case 0xA4:
fmt.Printf("LDY $%X\n", zeroPageAddress())
case 0xB4:
fmt.Printf("LDY $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xAC:
fmt.Printf("LDY $%X\n", absoluteAddress())
case 0xBC:
fmt.Printf("LDY $%X,X\n", absoluteIndexedAddress(c.X))
// STA
case 0x85:
fmt.Printf("STA $%X\n", zeroPageAddress())
case 0x95:
fmt.Printf("STA $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x8D:
fmt.Printf("STA $%X\n", absoluteAddress())
case 0x9D:
fmt.Printf("STA $%X,X\n", absoluteIndexedAddress(c.X))
case 0x99:
fmt.Printf("STA $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0x81:
fmt.Printf("STA ($%X,X)\n", indexedIndirectAddress())
case 0x91:
fmt.Printf("STA ($%X),Y\n", indirectIndexedAddress())
// STX
case 0x86:
fmt.Printf("STX $%X\n", zeroPageAddress())
case 0x96:
fmt.Printf("STX $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x8E:
fmt.Printf("STX $%X\n", absoluteAddress())
// STY
case 0x84:
fmt.Printf("STY $%X\n", zeroPageAddress())
case 0x94:
fmt.Printf("STY $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x8C:
fmt.Printf("STY $%X\n", absoluteAddress())
// JMP
case 0x4C:
fmt.Printf("JMP $%X\n", absoluteAddress())
case 0x6C:
fmt.Printf("JMP $%X\n", indirectAbsoluteAddress())
// JSR
case 0x20:
fmt.Printf("JSR $%X\n", absoluteAddress())
// Register Instructions
case 0xAA:
fmt.Println("TAX")
case 0x8A:
fmt.Println("TXA")
case 0xCA:
fmt.Println("DEX")
case 0xE8:
fmt.Println("INX")
case 0xA8:
fmt.Println("TAY")
case 0x98:
fmt.Println("TYA")
case 0x88:
fmt.Println("DEY")
case 0xC8:
fmt.Println("INY")
// Branch Instructions
case 0x10:
fmt.Printf("BPL $%X\n", immediateAddress())
case 0x30:
fmt.Printf("BMI $%X\n", immediateAddress())
case 0x50:
fmt.Printf("BVC $%X\n", immediateAddress())
case 0x70:
fmt.Printf("BVS $%X\n", immediateAddress())
case 0x90:
fmt.Printf("BCC $%X\n", immediateAddress())
case 0xB0:
fmt.Printf("BCS $%X\n", immediateAddress())
case 0xD0:
fmt.Printf("BNE $%X\n", immediateAddress())
case 0xF0:
fmt.Printf("BEQ $%X\n", immediateAddress())
// CMP
case 0xC9:
fmt.Printf("CMP $%X\n", immediateAddress())
case 0xC5:
fmt.Printf("CMP $%X\n", zeroPageAddress())
case 0xD5:
fmt.Printf("CMP $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xCD:
fmt.Printf("CMP $%X\n", absoluteAddress())
case 0xDD:
fmt.Printf("CMP $%X,X\n", absoluteIndexedAddress(c.X))
case 0xD9:
fmt.Printf("CMP $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0xC1:
fmt.Printf("CMP ($%X,X)\n", indexedIndirectAddress())
case 0xD1:
fmt.Printf("CMP ($%X),Y\n", c.indirectIndexedAddress())
// CPX
case 0xE0:
fmt.Printf("CPX $%X\n", immediateAddress())
case 0xE4:
fmt.Printf("CPX $%X\n", zeroPageAddress())
case 0xEC:
fmt.Printf("CPX $%X\n", absoluteAddress())
// CPY
case 0xC0:
fmt.Printf("CPY $%X\n", immediateAddress())
case 0xC4:
fmt.Printf("CPY $%X\n", zeroPageAddress())
case 0xCC:
fmt.Printf("CPY $%X\n", absoluteAddress())
// SBC
case 0xE9:
fmt.Printf("SBC $%X\n", immediateAddress())
case 0xE5:
fmt.Printf("SBC $%X\n", zeroPageAddress())
case 0xF5:
fmt.Printf("SBC $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xED:
fmt.Printf("SBC $%X\n", absoluteAddress())
case 0xFD:
fmt.Printf("SBC $%X,X\n", absoluteIndexedAddress(c.X))
case 0xF9:
fmt.Printf("SBC $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0xE1:
fmt.Printf("SBC ($%X,X)\n", indexedIndirectAddress())
case 0xF1:
fmt.Printf("SBC ($%X),Y\n", indirectIndexedAddress())
// Flag Instructions
case 0x18:
fmt.Println("CLC")
case 0x38:
fmt.Println("SEC")
case 0x58:
fmt.Println("CLI")
case 0x78:
fmt.Println("SEI")
case 0xB8:
fmt.Println("CLV")
case 0xD8:
fmt.Println("CLD")
case 0xF8:
fmt.Println("SED")
// Stack instructions
case 0x9A:
fmt.Println("TXS")
case 0xBA:
fmt.Println("TSX")
case 0x48:
fmt.Println("PHA")
case 0x68:
fmt.Println("PLA")
case 0x08:
fmt.Println("PHP")
case 0x28:
fmt.Println("PLP")
// AND
case 0x29:
fmt.Printf("AND $%X\n", immediateAddress())
case 0x25:
fmt.Printf("AND $%X\n", zeroPageAddress())
case 0x35:
fmt.Printf("AND $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x2d:
fmt.Printf("AND $%X\n", absoluteAddress())
case 0x3d:
fmt.Printf("AND $%X,X\n", absoluteIndexedAddress(c.X))
case 0x39:
fmt.Printf("AND $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0x21:
fmt.Printf("AND ($%X,X)\n", indexedIndirectAddress())
case 0x31:
fmt.Printf("AND ($%X),Y\n", indirectIndexedAddress())
// ORA
case 0x09:
fmt.Printf("ORA $%X\n", immediateAddress())
case 0x05:
fmt.Printf("ORA $%X\n", zeroPageAddress())
case 0x15:
fmt.Printf("ORA $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x0d:
fmt.Printf("ORA $%X\n", absoluteAddress())
case 0x1d:
fmt.Printf("ORA $%X,X\n", absoluteIndexedAddress(c.X))
case 0x19:
fmt.Printf("ORA $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0x01:
fmt.Printf("ORA ($%X,X)\n", indexedIndirectAddress())
case 0x11:
fmt.Printf("ORA ($%X),Y\n", indirectIndexedAddress())
// EOR
case 0x49:
fmt.Printf("EOR $%X\n", immediateAddress())
case 0x45:
fmt.Printf("EOR $%X\n", zeroPageAddress())
case 0x55:
fmt.Printf("EOR $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x4d:
fmt.Printf("EOR $%X\n", absoluteAddress())
case 0x5d:
fmt.Printf("EOR $%X,X\n", absoluteIndexedAddress(c.X))
case 0x59:
fmt.Printf("EOR $%X,Y\n", absoluteIndexedAddress(c.Y))
case 0x41:
fmt.Printf("EOR ($%X,X)\n", indexedIndirectAddress())
case 0x51:
fmt.Printf("EOR ($%X),Y\n", indirectIndexedAddress())
// DEC
case 0xc6:
fmt.Printf("DEC $%X\n", zeroPageAddress())
case 0xd6:
fmt.Printf("DEC $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xce:
fmt.Printf("DEC $%X\n", absoluteAddress())
case 0xde:
fmt.Printf("DEC $%X,X\n", absoluteIndexedAddress(c.X))
// INC
case 0xe6:
fmt.Printf("INC $%X\n", zeroPageAddress())
case 0xf6:
fmt.Printf("INC $%X,X\n", zeroPageIndexedAddress(c.X))
case 0xee:
fmt.Printf("INC $%X\n", absoluteAddress())
case 0xfe:
fmt.Printf("INC $%X,X\n", absoluteIndexedAddress(c.X))
// BRK
case 0x00:
fmt.Println("BRK")
// RTI
case 0x40:
fmt.Println("RTI")
// RTS
case 0x60:
fmt.Println("RTS")
// NOP
case 0xea:
fmt.Println("NOP")
// LSR
case 0x4a:
fmt.Println("LSR A")
case 0x46:
fmt.Printf("LSR $%X\n", zeroPageAddress())
case 0x56:
fmt.Printf("LSR $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x4e:
fmt.Printf("LSR $%X\n", absoluteAddress())
case 0x5e:
fmt.Printf("LSR $%X,X\n", absoluteIndexedAddress(c.X))
// ASL
case 0x0a:
fmt.Println("ASL A")
case 0x06:
fmt.Printf("ASL $%X\n", zeroPageAddress())
case 0x16:
fmt.Printf("ASL $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x0e:
fmt.Printf("ASL $%X\n", absoluteAddress())
case 0x1e:
fmt.Printf("ASL $%X,X\n", absoluteIndexedAddress(c.X))
// ROL
case 0x2a:
fmt.Println("ROL A")
case 0x26:
fmt.Printf("ROL $%X\n", zeroPageAddress())
case 0x36:
fmt.Printf("ROL $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x2e:
fmt.Printf("ROL $%X\n", absoluteAddress())
case 0x3e:
fmt.Printf("ROL $%X,X\n", absoluteIndexedAddress(c.X))
// ROR
case 0x6a:
fmt.Println("ROR A")
case 0x66:
fmt.Printf("ROR $%X\n", zeroPageAddress())
case 0x76:
fmt.Printf("ROR $%X,X\n", zeroPageIndexedAddress(c.X))
case 0x6e:
fmt.Printf("ROR $%X\n", absoluteAddress())
case 0x7e:
fmt.Printf("ROR $%X,X\n", absoluteIndexedAddress(c.X))
// BIT
case 0x24:
fmt.Printf("BIT $%X\n", zeroPageAddress())
case 0x2c:
fmt.Printf("BIT $%X\n", absoluteAddress())
}
}
Something went wrong with that request. Please try again.