| var Memory = require('../../common/memory.js'); | var Memory = require('../../common/memory.js'); | ||||
| function ADC(cpu){ | |||||
| function MATHC(cpu, m){ // To be used by both the ADC and SBC op codes. | |||||
| // m == 0 - Add | |||||
| // m == 1 - Subtract | |||||
| switch(cpu.__step){ | switch(cpu.__step){ | ||||
| case 0: | case 0: | ||||
| cpu.__mem.address = cpu.__PC; | cpu.__mem.address = cpu.__PC; | ||||
| PCUp(cpu, 1); | PCUp(cpu, 1); | ||||
| cpu.__opv = cpu.__mem.byte; | cpu.__opv = cpu.__mem.byte; | ||||
| if (cpu.__op == 0x69){ // Immediate | if (cpu.__op == 0x69){ // Immediate | ||||
| ALU(cpu, 0, cpu.__opv); | |||||
| ALU(cpu, m, cpu.__opv); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| } | } | ||||
| case 1: | case 1: | ||||
| switch (cpu.__op){ | switch (cpu.__op){ | ||||
| case 0x65: // Zero Page | case 0x65: // Zero Page | ||||
| cpu.__mem.address = cpu.__opv; | cpu.__mem.address = cpu.__opv; | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| case 0x75: // Zero Page, X | case 0x75: // Zero Page, X | ||||
| cpu.__opv = (cpu.__opv + cpu.__XR) & 0xFF; break; | cpu.__opv = (cpu.__opv + cpu.__XR) & 0xFF; break; | ||||
| case 0x75: // Zero Page, X | case 0x75: // Zero Page, X | ||||
| case 0x6D: // Absolute | case 0x6D: // Absolute | ||||
| cpu.__mem.address = cpu.__opv; | cpu.__mem.address = cpu.__opv; | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| case 0x7D: // Absolute, X | case 0x7D: // Absolute, X | ||||
| case 0x79: // Absolute, Y | case 0x79: // Absolute, Y | ||||
| cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF); | cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF); | ||||
| if (l < 255){ | if (l < 255){ | ||||
| cpu.__mem.address = cpu.__opv; | cpu.__mem.address = cpu.__opv; | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; | cpu.__op = -1; | ||||
| } | } | ||||
| break; | break; | ||||
| case 0x79: // Absolute, Y | case 0x79: // Absolute, Y | ||||
| let h = (cpu.__opv >> 8) + 1; | let h = (cpu.__opv >> 8) + 1; | ||||
| cpu.__mem.address = (cpu.__opv & 0xFF) | (h << 8); | cpu.__mem.address = (cpu.__opv & 0xFF) | (h << 8); | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| case 0x61: // Indirect, X | case 0x61: // Indirect, X | ||||
| cpu.__mem.address += 1; | cpu.__mem.address += 1; | ||||
| cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF); | cpu.__opv = (cpu.__opv & 0xFF00) | (l & 0xFF); | ||||
| if (l <= 255){ | if (l <= 255){ | ||||
| cpu.__mem.address = cpu.__opv; | cpu.__mem.address = cpu.__opv; | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| } | } | ||||
| } | } | ||||
| cpu.__opv = (cpu.__opv & 0x00FF) | (h << 8); | cpu.__opv = (cpu.__opv & 0x00FF) | (h << 8); | ||||
| } | } | ||||
| cpu.__mem.address = cpu.__opv; | cpu.__mem.address = cpu.__opv; | ||||
| ALU(cpu, 0, cpu.__mem.byte); | |||||
| ALU(cpu, m, cpu.__mem.byte); | |||||
| cpu.__op = -1; break; | cpu.__op = -1; break; | ||||
| } | } | ||||
| cpu.__step += 1; | cpu.__step += 1; | ||||
| } else { | } else { | ||||
| switch(this.__op){ | switch(this.__op){ | ||||
| case 0x69: case 0x65: case 0x75: case 0x6D: case 0x7D: case 0x79: case 0x61: case 0x71: | case 0x69: case 0x65: case 0x75: case 0x6D: case 0x7D: case 0x79: case 0x61: case 0x71: | ||||
| ADC(this); break; | |||||
| MATHC(this, 0); break; | |||||
| case 0x29: case 0x25: case 0x35: case 0x2D: case 0x3D: case 0x39: case 0x21: case 0x31: | case 0x29: case 0x25: case 0x35: case 0x2D: case 0x3D: case 0x39: case 0x21: case 0x31: | ||||
| AND(this); break; | AND(this); break; | ||||
| case 0x0A: case 0x06: case 0x16: case 0x0E: case 0x1E: | case 0x0A: case 0x06: case 0x16: case 0x0E: case 0x1E: | ||||
| case 0x60: | case 0x60: | ||||
| RTS(this); break; | RTS(this); break; | ||||
| case 0xE9: case 0xE5: case 0xF5: case 0xED: case 0xFD: case 0xF9: case 0xE1: case 0xF1: | case 0xE9: case 0xE5: case 0xF5: case 0xED: case 0xFD: case 0xF9: case 0xE1: case 0xF1: | ||||
| SBC(this); break; | |||||
| MATHC(this, 1); break; | |||||
| case 0x85: case 0x95: case 0x8D: case 0x9D: case 0x99: case 0x81: case 0x91: | case 0x85: case 0x95: case 0x8D: case 0x9D: case 0x99: case 0x81: case 0x91: | ||||
| STA(this); break; | STA(this); break; | ||||
| case 0x9A: case 0xBA: case 0x48: case 0x68: case 0x08: case 0x28: | case 0x9A: case 0xBA: case 0x48: case 0x68: case 0x08: case 0x28: |