Skip to content

Commit

Permalink
Release 0.5.5.5 - fixed FPtoFixed conversion and overflow flag assign…
Browse files Browse the repository at this point in the history
…ment. This was causing problems in BASIC816 for loops.
  • Loading branch information
dtremblay committed Jun 26, 2021
1 parent be835b9 commit 9f458a3
Show file tree
Hide file tree
Showing 24 changed files with 25,834 additions and 17,640 deletions.
291 changes: 1 addition & 290 deletions FoenixIDETester/CpuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,14 @@ public void Setup()
Assert.AreEqual(1, cpu.X.Width);
}

// LDA #$99
[TestMethod]
public void LoadAccumulatorWith99()
{
// By default the CPU must be in 6502 emulation mode
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate); // LDA Immediate
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x99); // #$99
int PC = cpu.PC;
cpu.ExecuteNext();
Assert.AreEqual(0x99, cpu.A.Value);
Assert.AreEqual(PC + 2, cpu.PC);
}
// CLC
[TestMethod]
public void ClearCarry()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
Assert.IsFalse(cpu.Flags.Carry);
}
// LDA #$99
// CLC
// ADC #$78
// -- overflow and carry are set.
// Unsigned: $99 + $78 = $11 ==> carry set
// Signed: -$67 + $78 = $11 ==> overflow false
[TestMethod]
public void LoadCheckCarrySetForOverflowAbsolute()
{
LoadAccumulatorWith99();
ClearCarry();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x78);
cpu.ExecuteNext();
Assert.AreEqual(0x11, cpu.A.Value);
Assert.IsFalse(cpu.Flags.oVerflow, "Overflow should be false");
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
}
// LDA #$99
// CLC
// ADC $56
// Unsigned: $99 + $78 = $11 => carry set
// Signed: -$67 + $78 = $11 ==> overflow false
[TestMethod]
public void LoadCheckCarrySetForOverflowDirectPage()
{
LoadAccumulatorWith99();
// Write a value that will cause an overflow in the addition - A is $99
MemMgr.RAM.WriteByte(0x56, 0x78);
ClearCarry();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_DirectPage);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x56);
cpu.ExecuteNext();
Assert.AreEqual(0x11, cpu.A.Value);
Assert.IsFalse(cpu.Flags.oVerflow, "Overflow should be false");
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
}


/*
# error reported on Foenix Forum by chibiakumas
Expand Down Expand Up @@ -414,245 +365,5 @@ public void TestTransfer()
Assert.IsFalse(cpu.Flags.Zero);
}

/**
* Taken from http://www.6502.org/tutorials/vflag.html
#1 CLC; 1 + 1 = 2, returns C = 0, V = 0
LDA #$01
ADC #$01
*/
[TestMethod]
public void TestOverflowADC1()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 1);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 1);
cpu.ExecuteNext();
Assert.AreEqual(2, cpu.A.Value);
Assert.IsFalse(cpu.Flags.Carry);
Assert.IsFalse(cpu.Flags.oVerflow);
}
/**
* Taken from http://www.6502.org/tutorials/vflag.html
#2 CLC; 1 + -1 = 0, returns C = 1, V = 0
LDA #$01
ADC #$FF
*/
[TestMethod]
public void TestOverflowADC2()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 1);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0xFF);
cpu.ExecuteNext();
Assert.AreEqual(0, cpu.A.Value);
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
Assert.IsFalse(cpu.Flags.oVerflow, "Overflow should be false");
}
/**
* Taken from http://www.6502.org/tutorials/vflag.html
#3 CLC; 127 + 1 = 128, returns C = 0, V = 1
LDA #$7F
ADC #$01
*/
[TestMethod]
public void TestOverflowADC3()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x7F);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x1);
cpu.ExecuteNext();
Assert.AreEqual(0x80, cpu.A.Value);
Assert.IsFalse(cpu.Flags.Carry, "Carry should be false");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}

/**
* Taken from http://www.6502.org/tutorials/vflag.html
#4 CLC; -128 + -1 = -129, returns C = 1, V = 1
LDA #$80
ADC #$FF
*/
[TestMethod]
public void TestOverflowADC4()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x80);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0xFF);
cpu.ExecuteNext();
Assert.AreEqual(0x7F, cpu.A.Value);
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}

/**
* Taken from http://www.6502.org/tutorials/vflag.html
#5 SEC ; 0 - 1 = -1, returns V = 0
LDA #$00
SBC #$01
*/
[TestMethod]
public void TestOverflowSBC5()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SEC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x0);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SBC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x1);
cpu.ExecuteNext();
Assert.AreEqual(0xFF, cpu.A.Value);
Assert.IsFalse(cpu.Flags.Carry, "Carry should be false");
Assert.IsFalse(cpu.Flags.oVerflow, "Overflow should be false");
}

/**
* Taken from http://www.6502.org/tutorials/vflag.html
#6 SEC ; -128 - 1 = -129, returns V = 1
LDA #$80
SBC #$01
*/
[TestMethod]
public void TestOverflowSBC6()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SEC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x80);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SBC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x1);
cpu.ExecuteNext();
Assert.AreEqual(0x7F, cpu.A.Value);
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}

/**
* Taken from http://www.6502.org/tutorials/vflag.html
#7 SEC ; 127 - -1 = 128, returns V = 1
LDA #$7F
SBC #$FF
*/
[TestMethod]
public void TestOverflowSBC7()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SEC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x7F);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SBC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0xFF);
cpu.ExecuteNext();
Assert.AreEqual(0x80, cpu.A.Value);
Assert.IsFalse(cpu.Flags.Carry, "Carry should be false");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}
/**
* Taken from http://www.6502.org/tutorials/vflag.html
#8 SEC ; Note: SEC, not CLC
LDA #$3F ; 63 + 64 + 1 = 128, returns V = 1
ADC #$40
*/
[TestMethod]
public void TestOverflowADC8()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SEC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x3F);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.ADC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x40);
cpu.ExecuteNext();
Assert.AreEqual(0x80, cpu.A.Value);
Assert.IsFalse(cpu.Flags.Carry, "Carry should be false");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}
/**
* Taken from http://www.6502.org/tutorials/vflag.html
#9 CLC ; Note: CLC, not SEC
LDA #$C0 ; -64 - 64 - 1 = -129, returns V = 1
SBC #$40
*/
[TestMethod]
public void TestOverflowSBC9()
{
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.CLC_Implied);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0xC0);
cpu.ExecuteNext();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SBC_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x40);
cpu.ExecuteNext();
Assert.AreEqual(0x7F, cpu.A.Value);
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
Assert.IsTrue(cpu.Flags.oVerflow, "Overflow should be true");
}

/**
* Error reported by Phil
* CLC
* XCE
* REP #$30
*
* SEC
* LDA #$AA
* SBC #$78
*
* Carry should be set
*/
[TestMethod]
public void TestOverflowSBC10()
{
ClearCarry();
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.XCE_Implied);
cpu.ExecuteNext();
Assert.IsFalse(cpu.Flags.Emulation);
Assert.IsTrue(cpu.Flags.Carry);

// REP #$30
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.REP_Immediate);
MemMgr.RAM.WriteByte(cpu.PC + 1, 0x30);
cpu.ExecuteNext();

Assert.AreEqual(2, cpu.A.Width);
Assert.AreEqual(2, cpu.X.Width);

// SEC
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SEC_Implied);

// LDA #$AA
MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.LDA_Immediate);
MemMgr.RAM.WriteWord(cpu.PC + 1, 0xAA);
cpu.ExecuteNext();

MemMgr.RAM.WriteByte(cpu.PC, OpcodeList.SBC_Immediate);
MemMgr.RAM.WriteWord(cpu.PC + 1, 0x78);
cpu.ExecuteNext();

Assert.AreEqual(0x32, cpu.A.Value);
Assert.IsTrue(cpu.Flags.Carry, "Carry should be true");
Assert.IsFalse(cpu.Flags.oVerflow, "Overflow should be false");
}
}
}
1 change: 1 addition & 0 deletions FoenixIDETester/FoenixIDETester.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<ItemGroup>
<Compile Include="CpuTests.cs" />
<Compile Include="MathCoproTests.cs" />
<Compile Include="OverflowTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LexerTests.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 9f458a3

Please sign in to comment.