const expect = require('chai').expect;
const CPU = require('../src/chip/MOS6502/cpu.js');
const Mem = require('../src/memory');


describe("Testing MOS6502 CPU...", function(){
  var cpu = new CPU();
  var tick = cpu.clk();
  cpu.memory = new Mem.Memory.RAM(256);
  cpu.memory.load(0xFFFC, [0x00, 0x00]);
 
  it("Resetting (IRQ Disabled flag must be on", function(){
    cpu.reset = true;
    tick(); // One tick to handle resets.
    expect(cpu.I).to.equal(1);
    expect(cpu.PC).to.equal(0); // Test program counter set to the vector stored at 0xFFFC - 0xFFFD
  });

  describe("Testing flag set/clear calls...", function(){
    it("CLC / SEC", function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0x18, 0x38]);
      cpu.reset = true;
      tick(); // reset.
      tick(); tick(); // Two ticks to process opcode.
      expect(cpu.C).to.equal(0);
      tick(); tick();
      expect(cpu.C).to.equal(1);
    });

    it("CLI / SEI", function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0x58, 0x78]);
      cpu.reset = true;
      tick();
      tick(); tick();
      expect(cpu.I).to.equal(0);
      tick(); tick();
      expect(cpu.I).to.equal(1);
    });

    it("CLD / SED", function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0xD8, 0xF8]);
      cpu.reset = true;
      tick();
      tick(); tick();
      expect(cpu.D).to.equal(0);
      tick(); tick();
      expect(cpu.D).to.equal(1);
    });

    it("CLV", function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0xB8]);
      cpu.reset = true;
      tick();
      tick(); tick();
      expect(cpu.V).to.equal(0);
    });
  });

  describe("Testing LDA...", function(){
    it("LDA Immediate", function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0xA9, 0x01, 0xA9, 0xBB]);
      cpu.reset = true;
      tick();
      tick();
      tick();
      expect(cpu.A).to.equal(0x01);
      tick();
      expect(cpu.A).to.equal(0x01);
      tick();
      expect(cpu.A).to.equal(0xBB);
    });

    it("LDA Zero Page");
    it("LDA Zero Page, X");
    it("LDA Absolute");
    it("LDA Absolute, X");
    it("LDA Absolute, Y");
    it("LDA Indirect, X");
    it("LDA Indirect, Y");
  });

  describe("Testing ADC...", function(){
    it("ADC Immediate", function(){
      cpu.memory.clearPage(0);
      // CLC
      // CLV
      // CLD
      // CLI
      // LDA $00
      // ADC $01
      // ADC $01
      cpu.memory.load(0, [0x18, 0xB8, 0xD8, 0x58, 0xA9, 0x00, 0x69, 0x01, 0x69, 0x01]);
      cpu.reset = true;
      tick();
      // Getting through the 4 clear flag calls. Each should take 2 ticks.
      tick(); tick(); tick(); tick(); tick(); tick(); tick(); tick();
      // Next two ticks for the LDA call
      tick(); tick();
      // Now processing the adds.
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(1);
      tick();
      expect(cpu.A).to.equal(1);
      tick();
      expect(cpu.A).to.equal(2);
    });

    it("ADC Zero Page");
    it("ADC Zero Page, X");
    it("ADC Absolute");
    it("ADC Absolute, X");
    it("ADC Absolute, Y");
    it("ADC Indirect, X");
    it("ADC Indirect, Y");

    it ("ADC Test Zero flag"/*, function(){
      cpu.memory.clearPage(0);
      cpu.memory.load(0, [0x18, 0xB8, 0xD8, 0x58, 0x69, 0xFF, 0x69, 0x01]);
      // Getting through the 4 clear flag calls. Each should take 2 ticks.
      tick(); tick(); tick(); tick(); tick(); tick(); tick(); tick();
      // Now processing the adds.
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(1);
      tick();
      expect(cpu.A).to.equal(1);
      tick();
      expect(cpu.A).to.equal(2);
    }*/);
  });

  describe("Testing SBC...", function(){
    it("SBC Immediate", function(){
      cpu.memory.clearPage(0);
      // SEC
      // CLV
      // CLD
      // CLI
      // LDA $00
      // SBC $01
      // SBC $01
      cpu.memory.load(0, [0x38, 0xB8, 0xD8, 0x58, 0xA9, 0x00, 0xE9, 0x01, 0xE9, 0x01]);
      cpu.reset = true;
      tick();
      // Getting through the 1 set and 3 clear flag calls. Each should take 2 ticks.
      tick(); tick(); tick(); tick(); tick(); tick(); tick(); tick();
      // Next two ticks for the LDA call
      tick(); tick();
      // Now processing the subtractions.
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(0);
      tick();
      expect(cpu.A).to.equal(0xFF);
      tick();
      expect(cpu.A).to.equal(0xFF);
      tick();
      expect(cpu.A).to.equal(0xFE);
    });

    it("SBC Zero Page");
    it("SBC Zero Page, X");
    it("SBC Absolute");
    it("SBC Absolute, X");
    it("SBC Absolute, Y");
    it("SBC Indirect, X");
    it("SBC Indirect, Y");
  });
});